decode.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // This code is licensed under the same terms as WebM:
  4. // Software License Agreement: http://www.webmproject.org/license/software/
  5. // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
  6. // -----------------------------------------------------------------------------
  7. //
  8. // Main decoding functions for WebP images.
  9. //
  10. // Author: Skal (pascal.massimino@gmail.com)
  11. #ifndef WEBP_WEBP_DECODE_H_
  12. #define WEBP_WEBP_DECODE_H_
  13. #include "./types.h"
  14. #if defined(__cplusplus) || defined(c_plusplus)
  15. extern "C" {
  16. #endif
  17. #define WEBP_DECODER_ABI_VERSION 0x0200 // MAJOR(8b) + MINOR(8b)
  18. // Return the decoder's version number, packed in hexadecimal using 8bits for
  19. // each of major/minor/revision. E.g: v2.5.7 is 0x020507.
  20. WEBP_EXTERN(int) WebPGetDecoderVersion(void);
  21. // Retrieve basic header information: width, height.
  22. // This function will also validate the header and return 0 in
  23. // case of formatting error.
  24. // Pointers 'width' and 'height' can be passed NULL if deemed irrelevant.
  25. WEBP_EXTERN(int) WebPGetInfo(const uint8_t* data, size_t data_size,
  26. int* width, int* height);
  27. // Decodes WebP images pointed to by 'data' and returns RGBA samples, along
  28. // with the dimensions in *width and *height. The ordering of samples in
  29. // memory is R, G, B, A, R, G, B, A... in scan order (endian-independent).
  30. // The returned pointer should be deleted calling free().
  31. // Returns NULL in case of error.
  32. WEBP_EXTERN(uint8_t*) WebPDecodeRGBA(const uint8_t* data, size_t data_size,
  33. int* width, int* height);
  34. // Same as WebPDecodeRGBA, but returning A, R, G, B, A, R, G, B... ordered data.
  35. WEBP_EXTERN(uint8_t*) WebPDecodeARGB(const uint8_t* data, size_t data_size,
  36. int* width, int* height);
  37. // Same as WebPDecodeRGBA, but returning B, G, R, A, B, G, R, A... ordered data.
  38. WEBP_EXTERN(uint8_t*) WebPDecodeBGRA(const uint8_t* data, size_t data_size,
  39. int* width, int* height);
  40. // Same as WebPDecodeRGBA, but returning R, G, B, R, G, B... ordered data.
  41. // If the bitstream contains transparency, it is ignored.
  42. WEBP_EXTERN(uint8_t*) WebPDecodeRGB(const uint8_t* data, size_t data_size,
  43. int* width, int* height);
  44. // Same as WebPDecodeRGB, but returning B, G, R, B, G, R... ordered data.
  45. WEBP_EXTERN(uint8_t*) WebPDecodeBGR(const uint8_t* data, size_t data_size,
  46. int* width, int* height);
  47. // Decode WebP images pointed to by 'data' to Y'UV format(*). The pointer
  48. // returned is the Y samples buffer. Upon return, *u and *v will point to
  49. // the U and V chroma data. These U and V buffers need NOT be free()'d,
  50. // unlike the returned Y luma one. The dimension of the U and V planes
  51. // are both (*width + 1) / 2 and (*height + 1)/ 2.
  52. // Upon return, the Y buffer has a stride returned as '*stride', while U and V
  53. // have a common stride returned as '*uv_stride'.
  54. // Return NULL in case of error.
  55. // (*) Also named Y'CbCr. See: http://en.wikipedia.org/wiki/YCbCr
  56. WEBP_EXTERN(uint8_t*) WebPDecodeYUV(const uint8_t* data, size_t data_size,
  57. int* width, int* height,
  58. uint8_t** u, uint8_t** v,
  59. int* stride, int* uv_stride);
  60. // These five functions are variants of the above ones, that decode the image
  61. // directly into a pre-allocated buffer 'output_buffer'. The maximum storage
  62. // available in this buffer is indicated by 'output_buffer_size'. If this
  63. // storage is not sufficient (or an error occurred), NULL is returned.
  64. // Otherwise, output_buffer is returned, for convenience.
  65. // The parameter 'output_stride' specifies the distance (in bytes)
  66. // between scanlines. Hence, output_buffer_size is expected to be at least
  67. // output_stride x picture-height.
  68. WEBP_EXTERN(uint8_t*) WebPDecodeRGBAInto(
  69. const uint8_t* data, size_t data_size,
  70. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  71. WEBP_EXTERN(uint8_t*) WebPDecodeARGBInto(
  72. const uint8_t* data, size_t data_size,
  73. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  74. WEBP_EXTERN(uint8_t*) WebPDecodeBGRAInto(
  75. const uint8_t* data, size_t data_size,
  76. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  77. // RGB and BGR variants. Here too the transparency information, if present,
  78. // will be dropped and ignored.
  79. WEBP_EXTERN(uint8_t*) WebPDecodeRGBInto(
  80. const uint8_t* data, size_t data_size,
  81. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  82. WEBP_EXTERN(uint8_t*) WebPDecodeBGRInto(
  83. const uint8_t* data, size_t data_size,
  84. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  85. // WebPDecodeYUVInto() is a variant of WebPDecodeYUV() that operates directly
  86. // into pre-allocated luma/chroma plane buffers. This function requires the
  87. // strides to be passed: one for the luma plane and one for each of the
  88. // chroma ones. The size of each plane buffer is passed as 'luma_size',
  89. // 'u_size' and 'v_size' respectively.
  90. // Pointer to the luma plane ('*luma') is returned or NULL if an error occurred
  91. // during decoding (or because some buffers were found to be too small).
  92. WEBP_EXTERN(uint8_t*) WebPDecodeYUVInto(
  93. const uint8_t* data, size_t data_size,
  94. uint8_t* luma, size_t luma_size, int luma_stride,
  95. uint8_t* u, size_t u_size, int u_stride,
  96. uint8_t* v, size_t v_size, int v_stride);
  97. //------------------------------------------------------------------------------
  98. // Output colorspaces and buffer
  99. // Colorspaces
  100. // Note: the naming describes the byte-ordering of packed samples in memory.
  101. // For instance, MODE_BGRA relates to samples ordered as B,G,R,A,B,G,R,A,...
  102. // Non-capital names (e.g.:MODE_Argb) relates to pre-multiplied RGB channels.
  103. // RGB-565 and RGBA-4444 are also endian-agnostic and byte-oriented.
  104. typedef enum { MODE_RGB = 0, MODE_RGBA = 1,
  105. MODE_BGR = 2, MODE_BGRA = 3,
  106. MODE_ARGB = 4, MODE_RGBA_4444 = 5,
  107. MODE_RGB_565 = 6,
  108. // RGB-premultiplied transparent modes (alpha value is preserved)
  109. MODE_rgbA = 7,
  110. MODE_bgrA = 8,
  111. MODE_Argb = 9,
  112. MODE_rgbA_4444 = 10,
  113. // YUV modes must come after RGB ones.
  114. MODE_YUV = 11, MODE_YUVA = 12, // yuv 4:2:0
  115. MODE_LAST = 13
  116. } WEBP_CSP_MODE;
  117. // Some useful macros:
  118. static WEBP_INLINE int WebPIsPremultipliedMode(WEBP_CSP_MODE mode) {
  119. return (mode == MODE_rgbA || mode == MODE_bgrA || mode == MODE_Argb ||
  120. mode == MODE_rgbA_4444);
  121. }
  122. static WEBP_INLINE int WebPIsAlphaMode(WEBP_CSP_MODE mode) {
  123. return (mode == MODE_RGBA || mode == MODE_BGRA || mode == MODE_ARGB ||
  124. mode == MODE_RGBA_4444 || mode == MODE_YUVA ||
  125. WebPIsPremultipliedMode(mode));
  126. }
  127. static WEBP_INLINE int WebPIsRGBMode(WEBP_CSP_MODE mode) {
  128. return (mode < MODE_YUV);
  129. }
  130. //------------------------------------------------------------------------------
  131. // WebPDecBuffer: Generic structure for describing the output sample buffer.
  132. typedef struct { // view as RGBA
  133. uint8_t* rgba; // pointer to RGBA samples
  134. int stride; // stride in bytes from one scanline to the next.
  135. size_t size; // total size of the *rgba buffer.
  136. } WebPRGBABuffer;
  137. typedef struct { // view as YUVA
  138. uint8_t* y, *u, *v, *a; // pointer to luma, chroma U/V, alpha samples
  139. int y_stride; // luma stride
  140. int u_stride, v_stride; // chroma strides
  141. int a_stride; // alpha stride
  142. size_t y_size; // luma plane size
  143. size_t u_size, v_size; // chroma planes size
  144. size_t a_size; // alpha-plane size
  145. } WebPYUVABuffer;
  146. // Output buffer
  147. typedef struct {
  148. WEBP_CSP_MODE colorspace; // Colorspace.
  149. int width, height; // Dimensions.
  150. int is_external_memory; // If true, 'internal_memory' pointer is not used.
  151. union {
  152. WebPRGBABuffer RGBA;
  153. WebPYUVABuffer YUVA;
  154. } u; // Nameless union of buffer parameters.
  155. uint32_t pad[4]; // padding for later use
  156. uint8_t* private_memory; // Internally allocated memory (only when
  157. // is_external_memory is false). Should not be used
  158. // externally, but accessed via the buffer union.
  159. } WebPDecBuffer;
  160. // Internal, version-checked, entry point
  161. WEBP_EXTERN(int) WebPInitDecBufferInternal(WebPDecBuffer*, int);
  162. // Initialize the structure as empty. Must be called before any other use.
  163. // Returns false in case of version mismatch
  164. static WEBP_INLINE int WebPInitDecBuffer(WebPDecBuffer* buffer) {
  165. return WebPInitDecBufferInternal(buffer, WEBP_DECODER_ABI_VERSION);
  166. }
  167. // Free any memory associated with the buffer. Must always be called last.
  168. // Note: doesn't free the 'buffer' structure itself.
  169. WEBP_EXTERN(void) WebPFreeDecBuffer(WebPDecBuffer* buffer);
  170. //------------------------------------------------------------------------------
  171. // Enumeration of the status codes
  172. typedef enum {
  173. VP8_STATUS_OK = 0,
  174. VP8_STATUS_OUT_OF_MEMORY,
  175. VP8_STATUS_INVALID_PARAM,
  176. VP8_STATUS_BITSTREAM_ERROR,
  177. VP8_STATUS_UNSUPPORTED_FEATURE,
  178. VP8_STATUS_SUSPENDED,
  179. VP8_STATUS_USER_ABORT,
  180. VP8_STATUS_NOT_ENOUGH_DATA
  181. } VP8StatusCode;
  182. //------------------------------------------------------------------------------
  183. // Incremental decoding
  184. //
  185. // This API allows streamlined decoding of partial data.
  186. // Picture can be incrementally decoded as data become available thanks to the
  187. // WebPIDecoder object. This object can be left in a SUSPENDED state if the
  188. // picture is only partially decoded, pending additional input.
  189. // Code example:
  190. //
  191. // WebPInitDecBuffer(&buffer);
  192. // buffer.colorspace = mode;
  193. // ...
  194. // WebPIDecoder* idec = WebPINewDecoder(&buffer);
  195. // while (has_more_data) {
  196. // // ... (get additional data)
  197. // status = WebPIAppend(idec, new_data, new_data_size);
  198. // if (status != VP8_STATUS_SUSPENDED ||
  199. // break;
  200. // }
  201. //
  202. // // The above call decodes the current available buffer.
  203. // // Part of the image can now be refreshed by calling to
  204. // // WebPIDecGetRGB()/WebPIDecGetYUVA() etc.
  205. // }
  206. // WebPIDelete(idec);
  207. typedef struct WebPIDecoder WebPIDecoder;
  208. // Creates a new incremental decoder with the supplied buffer parameter.
  209. // This output_buffer can be passed NULL, in which case a default output buffer
  210. // is used (with MODE_RGB). Otherwise, an internal reference to 'output_buffer'
  211. // is kept, which means that the lifespan of 'output_buffer' must be larger than
  212. // that of the returned WebPIDecoder object.
  213. // Returns NULL if the allocation failed.
  214. WEBP_EXTERN(WebPIDecoder*) WebPINewDecoder(WebPDecBuffer* output_buffer);
  215. // This function allocates and initializes an incremental-decoder object, which
  216. // will output the RGB/A samples specified by 'csp' into a preallocated
  217. // buffer 'output_buffer'. The size of this buffer is at least
  218. // 'output_buffer_size' and the stride (distance in bytes between two scanlines)
  219. // is specified by 'output_stride'. Returns NULL if the allocation failed.
  220. WEBP_EXTERN(WebPIDecoder*) WebPINewRGB(
  221. WEBP_CSP_MODE csp,
  222. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  223. // This function allocates and initializes an incremental-decoder object, which
  224. // will output the raw luma/chroma samples into a preallocated planes. The luma
  225. // plane is specified by its pointer 'luma', its size 'luma_size' and its stride
  226. // 'luma_stride'. Similarly, the chroma-u plane is specified by the 'u',
  227. // 'u_size' and 'u_stride' parameters, and the chroma-v plane by 'v'
  228. // and 'v_size'. And same for the alpha-plane. The 'a' pointer can be pass
  229. // NULL in case one is not interested in the transparency plane.
  230. // Returns NULL if the allocation failed.
  231. WEBP_EXTERN(WebPIDecoder*) WebPINewYUVA(
  232. uint8_t* luma, size_t luma_size, int luma_stride,
  233. uint8_t* u, size_t u_size, int u_stride,
  234. uint8_t* v, size_t v_size, int v_stride,
  235. uint8_t* a, size_t a_size, int a_stride);
  236. // Deprecated version of the above, without the alpha plane.
  237. // Kept for backward compatibility.
  238. WEBP_EXTERN(WebPIDecoder*) WebPINewYUV(
  239. uint8_t* luma, size_t luma_size, int luma_stride,
  240. uint8_t* u, size_t u_size, int u_stride,
  241. uint8_t* v, size_t v_size, int v_stride);
  242. // Deletes the WebPIDecoder object and associated memory. Must always be called
  243. // if WebPINewDecoder, WebPINewRGB or WebPINewYUV succeeded.
  244. WEBP_EXTERN(void) WebPIDelete(WebPIDecoder* idec);
  245. // Copies and decodes the next available data. Returns VP8_STATUS_OK when
  246. // the image is successfully decoded. Returns VP8_STATUS_SUSPENDED when more
  247. // data is expected. Returns error in other cases.
  248. WEBP_EXTERN(VP8StatusCode) WebPIAppend(
  249. WebPIDecoder* idec, const uint8_t* data, size_t data_size);
  250. // A variant of the above function to be used when data buffer contains
  251. // partial data from the beginning. In this case data buffer is not copied
  252. // to the internal memory.
  253. // Note that the value of the 'data' pointer can change between calls to
  254. // WebPIUpdate, for instance when the data buffer is resized to fit larger data.
  255. WEBP_EXTERN(VP8StatusCode) WebPIUpdate(
  256. WebPIDecoder* idec, const uint8_t* data, size_t data_size);
  257. // Returns the RGB/A image decoded so far. Returns NULL if output params
  258. // are not initialized yet. The RGB/A output type corresponds to the colorspace
  259. // specified during call to WebPINewDecoder() or WebPINewRGB().
  260. // *last_y is the index of last decoded row in raster scan order. Some pointers
  261. // (*last_y, *width etc.) can be NULL if corresponding information is not
  262. // needed.
  263. WEBP_EXTERN(uint8_t*) WebPIDecGetRGB(
  264. const WebPIDecoder* idec, int* last_y,
  265. int* width, int* height, int* stride);
  266. // Same as above function to get a YUVA image. Returns pointer to the luma
  267. // plane or NULL in case of error. If there is no alpha information
  268. // the alpha pointer '*a' will be returned NULL.
  269. WEBP_EXTERN(uint8_t*) WebPIDecGetYUVA(
  270. const WebPIDecoder* idec, int* last_y,
  271. uint8_t** u, uint8_t** v, uint8_t** a,
  272. int* width, int* height, int* stride, int* uv_stride, int* a_stride);
  273. // Deprecated alpha-less version of WebPIDecGetYUVA(): it will ignore the
  274. // alpha information (if present). Kept for backward compatibility.
  275. static WEBP_INLINE uint8_t* WebPIDecGetYUV(
  276. const WebPIDecoder* idec, int* last_y, uint8_t** u, uint8_t** v,
  277. int* width, int* height, int* stride, int* uv_stride) {
  278. return WebPIDecGetYUVA(idec, last_y, u, v, NULL, width, height,
  279. stride, uv_stride, NULL);
  280. }
  281. // Generic call to retrieve information about the displayable area.
  282. // If non NULL, the left/right/width/height pointers are filled with the visible
  283. // rectangular area so far.
  284. // Returns NULL in case the incremental decoder object is in an invalid state.
  285. // Otherwise returns the pointer to the internal representation. This structure
  286. // is read-only, tied to WebPIDecoder's lifespan and should not be modified.
  287. WEBP_EXTERN(const WebPDecBuffer*) WebPIDecodedArea(
  288. const WebPIDecoder* idec, int* left, int* top, int* width, int* height);
  289. //------------------------------------------------------------------------------
  290. // Advanced decoding parametrization
  291. //
  292. // Code sample for using the advanced decoding API
  293. /*
  294. // A) Init a configuration object
  295. WebPDecoderConfig config;
  296. CHECK(WebPInitDecoderConfig(&config));
  297. // B) optional: retrieve the bitstream's features.
  298. CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);
  299. // C) Adjust 'config', if needed
  300. config.no_fancy = 1;
  301. config.output.colorspace = MODE_BGRA;
  302. // etc.
  303. // Note that you can also make config.output point to an externally
  304. // supplied memory buffer, provided it's big enough to store the decoded
  305. // picture. Otherwise, config.output will just be used to allocate memory
  306. // and store the decoded picture.
  307. // D) Decode!
  308. CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);
  309. // E) Decoded image is now in config.output (and config.output.u.RGBA)
  310. // F) Reclaim memory allocated in config's object. It's safe to call
  311. // this function even if the memory is external and wasn't allocated
  312. // by WebPDecode().
  313. WebPFreeDecBuffer(&config.output);
  314. */
  315. // Features gathered from the bitstream
  316. typedef struct {
  317. int width; // Width in pixels, as read from the bitstream.
  318. int height; // Height in pixels, as read from the bitstream.
  319. int has_alpha; // True if the bitstream contains an alpha channel.
  320. // Unused for now:
  321. int bitstream_version; // should be 0 for now. TODO(later)
  322. int no_incremental_decoding; // if true, using incremental decoding is not
  323. // recommended.
  324. int rotate; // TODO(later)
  325. int uv_sampling; // should be 0 for now. TODO(later)
  326. uint32_t pad[3]; // padding for later use
  327. } WebPBitstreamFeatures;
  328. // Internal, version-checked, entry point
  329. WEBP_EXTERN(VP8StatusCode) WebPGetFeaturesInternal(
  330. const uint8_t*, size_t, WebPBitstreamFeatures*, int);
  331. // Retrieve features from the bitstream. The *features structure is filled
  332. // with information gathered from the bitstream.
  333. // Returns false in case of error or version mismatch.
  334. // In case of error, features->bitstream_status will reflect the error code.
  335. static WEBP_INLINE VP8StatusCode WebPGetFeatures(
  336. const uint8_t* data, size_t data_size,
  337. WebPBitstreamFeatures* features) {
  338. return WebPGetFeaturesInternal(data, data_size, features,
  339. WEBP_DECODER_ABI_VERSION);
  340. }
  341. // Decoding options
  342. typedef struct {
  343. int bypass_filtering; // if true, skip the in-loop filtering
  344. int no_fancy_upsampling; // if true, use faster pointwise upsampler
  345. int use_cropping; // if true, cropping is applied _first_
  346. int crop_left, crop_top; // top-left position for cropping.
  347. // Will be snapped to even values.
  348. int crop_width, crop_height; // dimension of the cropping area
  349. int use_scaling; // if true, scaling is applied _afterward_
  350. int scaled_width, scaled_height; // final resolution
  351. int use_threads; // if true, use multi-threaded decoding
  352. // Unused for now:
  353. int force_rotation; // forced rotation (to be applied _last_)
  354. int no_enhancement; // if true, discard enhancement layer
  355. uint32_t pad[6]; // padding for later use
  356. } WebPDecoderOptions;
  357. // Main object storing the configuration for advanced decoding.
  358. typedef struct {
  359. WebPBitstreamFeatures input; // Immutable bitstream features (optional)
  360. WebPDecBuffer output; // Output buffer (can point to external mem)
  361. WebPDecoderOptions options; // Decoding options
  362. } WebPDecoderConfig;
  363. // Internal, version-checked, entry point
  364. WEBP_EXTERN(int) WebPInitDecoderConfigInternal(WebPDecoderConfig*, int);
  365. // Initialize the configuration as empty. This function must always be
  366. // called first, unless WebPGetFeatures() is to be called.
  367. // Returns false in case of mismatched version.
  368. static WEBP_INLINE int WebPInitDecoderConfig(WebPDecoderConfig* config) {
  369. return WebPInitDecoderConfigInternal(config, WEBP_DECODER_ABI_VERSION);
  370. }
  371. // Instantiate a new incremental decoder object with the requested
  372. // configuration. The bitstream can be passed using 'data' and 'data_size'
  373. // parameter, in which case the features will be parsed and stored into
  374. // config->input. Otherwise, 'data' can be NULL and no parsing will occur.
  375. // Note that 'config' can be NULL too, in which case a default configuration
  376. // is used.
  377. // The return WebPIDecoder object must always be deleted calling WebPIDelete().
  378. // Returns NULL in case of error (and config->status will then reflect
  379. // the error condition).
  380. WEBP_EXTERN(WebPIDecoder*) WebPIDecode(const uint8_t* data, size_t data_size,
  381. WebPDecoderConfig* config);
  382. // Non-incremental version. This version decodes the full data at once, taking
  383. // 'config' into account. Returns decoding status (which should be VP8_STATUS_OK
  384. // if the decoding was successful).
  385. WEBP_EXTERN(VP8StatusCode) WebPDecode(const uint8_t* data, size_t data_size,
  386. WebPDecoderConfig* config);
  387. #if defined(__cplusplus) || defined(c_plusplus)
  388. } // extern "C"
  389. #endif
  390. #endif /* WEBP_WEBP_DECODE_H_ */