demux.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Demux API.
  11. // Enables extraction of image and extended format data from WebP files.
  12. // Code Example: Demuxing WebP data to extract all the frames, ICC profile
  13. // and EXIF/XMP metadata.
  14. /*
  15. WebPDemuxer* demux = WebPDemux(&webp_data);
  16. uint32_t width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
  17. uint32_t height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
  18. // ... (Get information about the features present in the WebP file).
  19. uint32_t flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
  20. // ... (Iterate over all frames).
  21. WebPIterator iter;
  22. if (WebPDemuxGetFrame(demux, 1, &iter)) {
  23. do {
  24. // ... (Consume 'iter'; e.g. Decode 'iter.fragment' with WebPDecode(),
  25. // ... and get other frame properties like width, height, offsets etc.
  26. // ... see 'struct WebPIterator' below for more info).
  27. } while (WebPDemuxNextFrame(&iter));
  28. WebPDemuxReleaseIterator(&iter);
  29. }
  30. // ... (Extract metadata).
  31. WebPChunkIterator chunk_iter;
  32. if (flags & ICCP_FLAG) WebPDemuxGetChunk(demux, "ICCP", 1, &chunk_iter);
  33. // ... (Consume the ICC profile in 'chunk_iter.chunk').
  34. WebPDemuxReleaseChunkIterator(&chunk_iter);
  35. if (flags & EXIF_FLAG) WebPDemuxGetChunk(demux, "EXIF", 1, &chunk_iter);
  36. // ... (Consume the EXIF metadata in 'chunk_iter.chunk').
  37. WebPDemuxReleaseChunkIterator(&chunk_iter);
  38. if (flags & XMP_FLAG) WebPDemuxGetChunk(demux, "XMP ", 1, &chunk_iter);
  39. // ... (Consume the XMP metadata in 'chunk_iter.chunk').
  40. WebPDemuxReleaseChunkIterator(&chunk_iter);
  41. WebPDemuxDelete(demux);
  42. */
  43. #ifndef WEBP_WEBP_DEMUX_H_
  44. #define WEBP_WEBP_DEMUX_H_
  45. #include "./decode.h" // for WEBP_CSP_MODE
  46. #include "./mux_types.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #define WEBP_DEMUX_ABI_VERSION 0x0107 // MAJOR(8b) + MINOR(8b)
  51. // Note: forward declaring enumerations is not allowed in (strict) C and C++,
  52. // the types are left here for reference.
  53. // typedef enum WebPDemuxState WebPDemuxState;
  54. // typedef enum WebPFormatFeature WebPFormatFeature;
  55. typedef struct WebPDemuxer WebPDemuxer;
  56. typedef struct WebPIterator WebPIterator;
  57. typedef struct WebPChunkIterator WebPChunkIterator;
  58. typedef struct WebPAnimInfo WebPAnimInfo;
  59. typedef struct WebPAnimDecoderOptions WebPAnimDecoderOptions;
  60. //------------------------------------------------------------------------------
  61. // Returns the version number of the demux library, packed in hexadecimal using
  62. // 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507.
  63. WEBP_EXTERN(int) WebPGetDemuxVersion(void);
  64. //------------------------------------------------------------------------------
  65. // Life of a Demux object
  66. typedef enum WebPDemuxState {
  67. WEBP_DEMUX_PARSE_ERROR = -1, // An error occurred while parsing.
  68. WEBP_DEMUX_PARSING_HEADER = 0, // Not enough data to parse full header.
  69. WEBP_DEMUX_PARSED_HEADER = 1, // Header parsing complete,
  70. // data may be available.
  71. WEBP_DEMUX_DONE = 2 // Entire file has been parsed.
  72. } WebPDemuxState;
  73. // Internal, version-checked, entry point
  74. WEBP_EXTERN(WebPDemuxer*) WebPDemuxInternal(
  75. const WebPData*, int, WebPDemuxState*, int);
  76. // Parses the full WebP file given by 'data'. For single images the WebP file
  77. // header alone or the file header and the chunk header may be absent.
  78. // Returns a WebPDemuxer object on successful parse, NULL otherwise.
  79. static WEBP_INLINE WebPDemuxer* WebPDemux(const WebPData* data) {
  80. return WebPDemuxInternal(data, 0, NULL, WEBP_DEMUX_ABI_VERSION);
  81. }
  82. // Parses the possibly incomplete WebP file given by 'data'.
  83. // If 'state' is non-NULL it will be set to indicate the status of the demuxer.
  84. // Returns NULL in case of error or if there isn't enough data to start parsing;
  85. // and a WebPDemuxer object on successful parse.
  86. // Note that WebPDemuxer keeps internal pointers to 'data' memory segment.
  87. // If this data is volatile, the demuxer object should be deleted (by calling
  88. // WebPDemuxDelete()) and WebPDemuxPartial() called again on the new data.
  89. // This is usually an inexpensive operation.
  90. static WEBP_INLINE WebPDemuxer* WebPDemuxPartial(
  91. const WebPData* data, WebPDemuxState* state) {
  92. return WebPDemuxInternal(data, 1, state, WEBP_DEMUX_ABI_VERSION);
  93. }
  94. // Frees memory associated with 'dmux'.
  95. WEBP_EXTERN(void) WebPDemuxDelete(WebPDemuxer* dmux);
  96. //------------------------------------------------------------------------------
  97. // Data/information extraction.
  98. typedef enum WebPFormatFeature {
  99. WEBP_FF_FORMAT_FLAGS, // Extended format flags present in the 'VP8X' chunk.
  100. WEBP_FF_CANVAS_WIDTH,
  101. WEBP_FF_CANVAS_HEIGHT,
  102. WEBP_FF_LOOP_COUNT,
  103. WEBP_FF_BACKGROUND_COLOR,
  104. WEBP_FF_FRAME_COUNT // Number of frames present in the demux object.
  105. // In case of a partial demux, this is the number of
  106. // frames seen so far, with the last frame possibly
  107. // being partial.
  108. } WebPFormatFeature;
  109. // Get the 'feature' value from the 'dmux'.
  110. // NOTE: values are only valid if WebPDemux() was used or WebPDemuxPartial()
  111. // returned a state > WEBP_DEMUX_PARSING_HEADER.
  112. WEBP_EXTERN(uint32_t) WebPDemuxGetI(
  113. const WebPDemuxer* dmux, WebPFormatFeature feature);
  114. //------------------------------------------------------------------------------
  115. // Frame iteration.
  116. struct WebPIterator {
  117. int frame_num;
  118. int num_frames; // equivalent to WEBP_FF_FRAME_COUNT.
  119. int x_offset, y_offset; // offset relative to the canvas.
  120. int width, height; // dimensions of this frame.
  121. int duration; // display duration in milliseconds.
  122. WebPMuxAnimDispose dispose_method; // dispose method for the frame.
  123. int complete; // true if 'fragment' contains a full frame. partial images
  124. // may still be decoded with the WebP incremental decoder.
  125. WebPData fragment; // The frame given by 'frame_num'. Note for historical
  126. // reasons this is called a fragment.
  127. int has_alpha; // True if the frame contains transparency.
  128. WebPMuxAnimBlend blend_method; // Blend operation for the frame.
  129. uint32_t pad[2]; // padding for later use.
  130. void* private_; // for internal use only.
  131. };
  132. // Retrieves frame 'frame_number' from 'dmux'.
  133. // 'iter->fragment' points to the frame on return from this function.
  134. // Setting 'frame_number' equal to 0 will return the last frame of the image.
  135. // Returns false if 'dmux' is NULL or frame 'frame_number' is not present.
  136. // Call WebPDemuxReleaseIterator() when use of the iterator is complete.
  137. // NOTE: 'dmux' must persist for the lifetime of 'iter'.
  138. WEBP_EXTERN(int) WebPDemuxGetFrame(
  139. const WebPDemuxer* dmux, int frame_number, WebPIterator* iter);
  140. // Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or
  141. // previous ('iter->frame_num' - 1) frame. These functions do not loop.
  142. // Returns true on success, false otherwise.
  143. WEBP_EXTERN(int) WebPDemuxNextFrame(WebPIterator* iter);
  144. WEBP_EXTERN(int) WebPDemuxPrevFrame(WebPIterator* iter);
  145. // Releases any memory associated with 'iter'.
  146. // Must be called before any subsequent calls to WebPDemuxGetChunk() on the same
  147. // iter. Also, must be called before destroying the associated WebPDemuxer with
  148. // WebPDemuxDelete().
  149. WEBP_EXTERN(void) WebPDemuxReleaseIterator(WebPIterator* iter);
  150. //------------------------------------------------------------------------------
  151. // Chunk iteration.
  152. struct WebPChunkIterator {
  153. // The current and total number of chunks with the fourcc given to
  154. // WebPDemuxGetChunk().
  155. int chunk_num;
  156. int num_chunks;
  157. WebPData chunk; // The payload of the chunk.
  158. uint32_t pad[6]; // padding for later use
  159. void* private_;
  160. };
  161. // Retrieves the 'chunk_number' instance of the chunk with id 'fourcc' from
  162. // 'dmux'.
  163. // 'fourcc' is a character array containing the fourcc of the chunk to return,
  164. // e.g., "ICCP", "XMP ", "EXIF", etc.
  165. // Setting 'chunk_number' equal to 0 will return the last chunk in a set.
  166. // Returns true if the chunk is found, false otherwise. Image related chunk
  167. // payloads are accessed through WebPDemuxGetFrame() and related functions.
  168. // Call WebPDemuxReleaseChunkIterator() when use of the iterator is complete.
  169. // NOTE: 'dmux' must persist for the lifetime of the iterator.
  170. WEBP_EXTERN(int) WebPDemuxGetChunk(const WebPDemuxer* dmux,
  171. const char fourcc[4], int chunk_number,
  172. WebPChunkIterator* iter);
  173. // Sets 'iter->chunk' to point to the next ('iter->chunk_num' + 1) or previous
  174. // ('iter->chunk_num' - 1) chunk. These functions do not loop.
  175. // Returns true on success, false otherwise.
  176. WEBP_EXTERN(int) WebPDemuxNextChunk(WebPChunkIterator* iter);
  177. WEBP_EXTERN(int) WebPDemuxPrevChunk(WebPChunkIterator* iter);
  178. // Releases any memory associated with 'iter'.
  179. // Must be called before destroying the associated WebPDemuxer with
  180. // WebPDemuxDelete().
  181. WEBP_EXTERN(void) WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter);
  182. //------------------------------------------------------------------------------
  183. // WebPAnimDecoder API
  184. //
  185. // This API allows decoding (possibly) animated WebP images.
  186. //
  187. // Code Example:
  188. /*
  189. WebPAnimDecoderOptions dec_options;
  190. WebPAnimDecoderOptionsInit(&dec_options);
  191. // Tune 'dec_options' as needed.
  192. WebPAnimDecoder* dec = WebPAnimDecoderNew(webp_data, &dec_options);
  193. WebPAnimInfo anim_info;
  194. WebPAnimDecoderGetInfo(dec, &anim_info);
  195. for (uint32_t i = 0; i < anim_info.loop_count; ++i) {
  196. while (WebPAnimDecoderHasMoreFrames(dec)) {
  197. uint8_t* buf;
  198. int timestamp;
  199. WebPAnimDecoderGetNext(dec, &buf, &timestamp);
  200. // ... (Render 'buf' based on 'timestamp').
  201. // ... (Do NOT free 'buf', as it is owned by 'dec').
  202. }
  203. WebPAnimDecoderReset(dec);
  204. }
  205. const WebPDemuxer* demuxer = WebPAnimDecoderGetDemuxer(dec);
  206. // ... (Do something using 'demuxer'; e.g. get EXIF/XMP/ICC data).
  207. WebPAnimDecoderDelete(dec);
  208. */
  209. typedef struct WebPAnimDecoder WebPAnimDecoder; // Main opaque object.
  210. // Global options.
  211. struct WebPAnimDecoderOptions {
  212. // Output colorspace. Only the following modes are supported:
  213. // MODE_RGBA, MODE_BGRA, MODE_rgbA and MODE_bgrA.
  214. WEBP_CSP_MODE color_mode;
  215. int use_threads; // If true, use multi-threaded decoding.
  216. uint32_t padding[7]; // Padding for later use.
  217. };
  218. // Internal, version-checked, entry point.
  219. WEBP_EXTERN(int) WebPAnimDecoderOptionsInitInternal(
  220. WebPAnimDecoderOptions*, int);
  221. // Should always be called, to initialize a fresh WebPAnimDecoderOptions
  222. // structure before modification. Returns false in case of version mismatch.
  223. // WebPAnimDecoderOptionsInit() must have succeeded before using the
  224. // 'dec_options' object.
  225. static WEBP_INLINE int WebPAnimDecoderOptionsInit(
  226. WebPAnimDecoderOptions* dec_options) {
  227. return WebPAnimDecoderOptionsInitInternal(dec_options,
  228. WEBP_DEMUX_ABI_VERSION);
  229. }
  230. // Internal, version-checked, entry point.
  231. WEBP_EXTERN(WebPAnimDecoder*) WebPAnimDecoderNewInternal(
  232. const WebPData*, const WebPAnimDecoderOptions*, int);
  233. // Creates and initializes a WebPAnimDecoder object.
  234. // Parameters:
  235. // webp_data - (in) WebP bitstream. This should remain unchanged during the
  236. // lifetime of the output WebPAnimDecoder object.
  237. // dec_options - (in) decoding options. Can be passed NULL to choose
  238. // reasonable defaults (in particular, color mode MODE_RGBA
  239. // will be picked).
  240. // Returns:
  241. // A pointer to the newly created WebPAnimDecoder object, or NULL in case of
  242. // parsing error, invalid option or memory error.
  243. static WEBP_INLINE WebPAnimDecoder* WebPAnimDecoderNew(
  244. const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options) {
  245. return WebPAnimDecoderNewInternal(webp_data, dec_options,
  246. WEBP_DEMUX_ABI_VERSION);
  247. }
  248. // Global information about the animation..
  249. struct WebPAnimInfo {
  250. uint32_t canvas_width;
  251. uint32_t canvas_height;
  252. uint32_t loop_count;
  253. uint32_t bgcolor;
  254. uint32_t frame_count;
  255. uint32_t pad[4]; // padding for later use
  256. };
  257. // Get global information about the animation.
  258. // Parameters:
  259. // dec - (in) decoder instance to get information from.
  260. // info - (out) global information fetched from the animation.
  261. // Returns:
  262. // True on success.
  263. WEBP_EXTERN(int) WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec,
  264. WebPAnimInfo* info);
  265. // Fetch the next frame from 'dec' based on options supplied to
  266. // WebPAnimDecoderNew(). This will be a fully reconstructed canvas of size
  267. // 'canvas_width * 4 * canvas_height', and not just the frame sub-rectangle. The
  268. // returned buffer 'buf' is valid only until the next call to
  269. // WebPAnimDecoderGetNext(), WebPAnimDecoderReset() or WebPAnimDecoderDelete().
  270. // Parameters:
  271. // dec - (in/out) decoder instance from which the next frame is to be fetched.
  272. // buf - (out) decoded frame.
  273. // timestamp - (out) timestamp of the frame in milliseconds.
  274. // Returns:
  275. // False if any of the arguments are NULL, or if there is a parsing or
  276. // decoding error, or if there are no more frames. Otherwise, returns true.
  277. WEBP_EXTERN(int) WebPAnimDecoderGetNext(WebPAnimDecoder* dec,
  278. uint8_t** buf, int* timestamp);
  279. // Check if there are more frames left to decode.
  280. // Parameters:
  281. // dec - (in) decoder instance to be checked.
  282. // Returns:
  283. // True if 'dec' is not NULL and some frames are yet to be decoded.
  284. // Otherwise, returns false.
  285. WEBP_EXTERN(int) WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec);
  286. // Resets the WebPAnimDecoder object, so that next call to
  287. // WebPAnimDecoderGetNext() will restart decoding from 1st frame. This would be
  288. // helpful when all frames need to be decoded multiple times (e.g.
  289. // info.loop_count times) without destroying and recreating the 'dec' object.
  290. // Parameters:
  291. // dec - (in/out) decoder instance to be reset
  292. WEBP_EXTERN(void) WebPAnimDecoderReset(WebPAnimDecoder* dec);
  293. // Grab the internal demuxer object.
  294. // Getting the demuxer object can be useful if one wants to use operations only
  295. // available through demuxer; e.g. to get XMP/EXIF/ICC metadata. The returned
  296. // demuxer object is owned by 'dec' and is valid only until the next call to
  297. // WebPAnimDecoderDelete().
  298. //
  299. // Parameters:
  300. // dec - (in) decoder instance from which the demuxer object is to be fetched.
  301. WEBP_EXTERN(const WebPDemuxer*) WebPAnimDecoderGetDemuxer(
  302. const WebPAnimDecoder* dec);
  303. // Deletes the WebPAnimDecoder object.
  304. // Parameters:
  305. // dec - (in/out) decoder instance to be deleted
  306. WEBP_EXTERN(void) WebPAnimDecoderDelete(WebPAnimDecoder* dec);
  307. #ifdef __cplusplus
  308. } // extern "C"
  309. #endif
  310. #endif /* WEBP_WEBP_DEMUX_H_ */