demux.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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, // bit-wise combination of WebPFeatureFlags
  100. // corresponding to the 'VP8X' chunk (if present).
  101. WEBP_FF_CANVAS_WIDTH,
  102. WEBP_FF_CANVAS_HEIGHT,
  103. WEBP_FF_LOOP_COUNT, // only relevant for animated file
  104. WEBP_FF_BACKGROUND_COLOR, // idem.
  105. WEBP_FF_FRAME_COUNT // Number of frames present in the demux object.
  106. // In case of a partial demux, this is the number
  107. // of frames seen so far, with the last frame
  108. // possibly being partial.
  109. } WebPFormatFeature;
  110. // Get the 'feature' value from the 'dmux'.
  111. // NOTE: values are only valid if WebPDemux() was used or WebPDemuxPartial()
  112. // returned a state > WEBP_DEMUX_PARSING_HEADER.
  113. // If 'feature' is WEBP_FF_FORMAT_FLAGS, the returned value is a bit-wise
  114. // combination of WebPFeatureFlags values.
  115. // If 'feature' is WEBP_FF_LOOP_COUNT, WEBP_FF_BACKGROUND_COLOR, the returned
  116. // value is only meaningful if the bitstream is animated.
  117. WEBP_EXTERN uint32_t WebPDemuxGetI(
  118. const WebPDemuxer* dmux, WebPFormatFeature feature);
  119. //------------------------------------------------------------------------------
  120. // Frame iteration.
  121. struct WebPIterator {
  122. int frame_num;
  123. int num_frames; // equivalent to WEBP_FF_FRAME_COUNT.
  124. int x_offset, y_offset; // offset relative to the canvas.
  125. int width, height; // dimensions of this frame.
  126. int duration; // display duration in milliseconds.
  127. WebPMuxAnimDispose dispose_method; // dispose method for the frame.
  128. int complete; // true if 'fragment' contains a full frame. partial images
  129. // may still be decoded with the WebP incremental decoder.
  130. WebPData fragment; // The frame given by 'frame_num'. Note for historical
  131. // reasons this is called a fragment.
  132. int has_alpha; // True if the frame contains transparency.
  133. WebPMuxAnimBlend blend_method; // Blend operation for the frame.
  134. uint32_t pad[2]; // padding for later use.
  135. void* private_; // for internal use only.
  136. };
  137. // Retrieves frame 'frame_number' from 'dmux'.
  138. // 'iter->fragment' points to the frame on return from this function.
  139. // Setting 'frame_number' equal to 0 will return the last frame of the image.
  140. // Returns false if 'dmux' is NULL or frame 'frame_number' is not present.
  141. // Call WebPDemuxReleaseIterator() when use of the iterator is complete.
  142. // NOTE: 'dmux' must persist for the lifetime of 'iter'.
  143. WEBP_EXTERN int WebPDemuxGetFrame(
  144. const WebPDemuxer* dmux, int frame_number, WebPIterator* iter);
  145. // Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or
  146. // previous ('iter->frame_num' - 1) frame. These functions do not loop.
  147. // Returns true on success, false otherwise.
  148. WEBP_EXTERN int WebPDemuxNextFrame(WebPIterator* iter);
  149. WEBP_EXTERN int WebPDemuxPrevFrame(WebPIterator* iter);
  150. // Releases any memory associated with 'iter'.
  151. // Must be called before any subsequent calls to WebPDemuxGetChunk() on the same
  152. // iter. Also, must be called before destroying the associated WebPDemuxer with
  153. // WebPDemuxDelete().
  154. WEBP_EXTERN void WebPDemuxReleaseIterator(WebPIterator* iter);
  155. //------------------------------------------------------------------------------
  156. // Chunk iteration.
  157. struct WebPChunkIterator {
  158. // The current and total number of chunks with the fourcc given to
  159. // WebPDemuxGetChunk().
  160. int chunk_num;
  161. int num_chunks;
  162. WebPData chunk; // The payload of the chunk.
  163. uint32_t pad[6]; // padding for later use
  164. void* private_;
  165. };
  166. // Retrieves the 'chunk_number' instance of the chunk with id 'fourcc' from
  167. // 'dmux'.
  168. // 'fourcc' is a character array containing the fourcc of the chunk to return,
  169. // e.g., "ICCP", "XMP ", "EXIF", etc.
  170. // Setting 'chunk_number' equal to 0 will return the last chunk in a set.
  171. // Returns true if the chunk is found, false otherwise. Image related chunk
  172. // payloads are accessed through WebPDemuxGetFrame() and related functions.
  173. // Call WebPDemuxReleaseChunkIterator() when use of the iterator is complete.
  174. // NOTE: 'dmux' must persist for the lifetime of the iterator.
  175. WEBP_EXTERN int WebPDemuxGetChunk(const WebPDemuxer* dmux,
  176. const char fourcc[4], int chunk_number,
  177. WebPChunkIterator* iter);
  178. // Sets 'iter->chunk' to point to the next ('iter->chunk_num' + 1) or previous
  179. // ('iter->chunk_num' - 1) chunk. These functions do not loop.
  180. // Returns true on success, false otherwise.
  181. WEBP_EXTERN int WebPDemuxNextChunk(WebPChunkIterator* iter);
  182. WEBP_EXTERN int WebPDemuxPrevChunk(WebPChunkIterator* iter);
  183. // Releases any memory associated with 'iter'.
  184. // Must be called before destroying the associated WebPDemuxer with
  185. // WebPDemuxDelete().
  186. WEBP_EXTERN void WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter);
  187. //------------------------------------------------------------------------------
  188. // WebPAnimDecoder API
  189. //
  190. // This API allows decoding (possibly) animated WebP images.
  191. //
  192. // Code Example:
  193. /*
  194. WebPAnimDecoderOptions dec_options;
  195. WebPAnimDecoderOptionsInit(&dec_options);
  196. // Tune 'dec_options' as needed.
  197. WebPAnimDecoder* dec = WebPAnimDecoderNew(webp_data, &dec_options);
  198. WebPAnimInfo anim_info;
  199. WebPAnimDecoderGetInfo(dec, &anim_info);
  200. for (uint32_t i = 0; i < anim_info.loop_count; ++i) {
  201. while (WebPAnimDecoderHasMoreFrames(dec)) {
  202. uint8_t* buf;
  203. int timestamp;
  204. WebPAnimDecoderGetNext(dec, &buf, &timestamp);
  205. // ... (Render 'buf' based on 'timestamp').
  206. // ... (Do NOT free 'buf', as it is owned by 'dec').
  207. }
  208. WebPAnimDecoderReset(dec);
  209. }
  210. const WebPDemuxer* demuxer = WebPAnimDecoderGetDemuxer(dec);
  211. // ... (Do something using 'demuxer'; e.g. get EXIF/XMP/ICC data).
  212. WebPAnimDecoderDelete(dec);
  213. */
  214. typedef struct WebPAnimDecoder WebPAnimDecoder; // Main opaque object.
  215. // Global options.
  216. struct WebPAnimDecoderOptions {
  217. // Output colorspace. Only the following modes are supported:
  218. // MODE_RGBA, MODE_BGRA, MODE_rgbA and MODE_bgrA.
  219. WEBP_CSP_MODE color_mode;
  220. int use_threads; // If true, use multi-threaded decoding.
  221. uint32_t padding[7]; // Padding for later use.
  222. };
  223. // Internal, version-checked, entry point.
  224. WEBP_EXTERN int WebPAnimDecoderOptionsInitInternal(
  225. WebPAnimDecoderOptions*, int);
  226. // Should always be called, to initialize a fresh WebPAnimDecoderOptions
  227. // structure before modification. Returns false in case of version mismatch.
  228. // WebPAnimDecoderOptionsInit() must have succeeded before using the
  229. // 'dec_options' object.
  230. static WEBP_INLINE int WebPAnimDecoderOptionsInit(
  231. WebPAnimDecoderOptions* dec_options) {
  232. return WebPAnimDecoderOptionsInitInternal(dec_options,
  233. WEBP_DEMUX_ABI_VERSION);
  234. }
  235. // Internal, version-checked, entry point.
  236. WEBP_EXTERN WebPAnimDecoder* WebPAnimDecoderNewInternal(
  237. const WebPData*, const WebPAnimDecoderOptions*, int);
  238. // Creates and initializes a WebPAnimDecoder object.
  239. // Parameters:
  240. // webp_data - (in) WebP bitstream. This should remain unchanged during the
  241. // lifetime of the output WebPAnimDecoder object.
  242. // dec_options - (in) decoding options. Can be passed NULL to choose
  243. // reasonable defaults (in particular, color mode MODE_RGBA
  244. // will be picked).
  245. // Returns:
  246. // A pointer to the newly created WebPAnimDecoder object, or NULL in case of
  247. // parsing error, invalid option or memory error.
  248. static WEBP_INLINE WebPAnimDecoder* WebPAnimDecoderNew(
  249. const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options) {
  250. return WebPAnimDecoderNewInternal(webp_data, dec_options,
  251. WEBP_DEMUX_ABI_VERSION);
  252. }
  253. // Global information about the animation..
  254. struct WebPAnimInfo {
  255. uint32_t canvas_width;
  256. uint32_t canvas_height;
  257. uint32_t loop_count;
  258. uint32_t bgcolor;
  259. uint32_t frame_count;
  260. uint32_t pad[4]; // padding for later use
  261. };
  262. // Get global information about the animation.
  263. // Parameters:
  264. // dec - (in) decoder instance to get information from.
  265. // info - (out) global information fetched from the animation.
  266. // Returns:
  267. // True on success.
  268. WEBP_EXTERN int WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec,
  269. WebPAnimInfo* info);
  270. // Fetch the next frame from 'dec' based on options supplied to
  271. // WebPAnimDecoderNew(). This will be a fully reconstructed canvas of size
  272. // 'canvas_width * 4 * canvas_height', and not just the frame sub-rectangle. The
  273. // returned buffer 'buf' is valid only until the next call to
  274. // WebPAnimDecoderGetNext(), WebPAnimDecoderReset() or WebPAnimDecoderDelete().
  275. // Parameters:
  276. // dec - (in/out) decoder instance from which the next frame is to be fetched.
  277. // buf - (out) decoded frame.
  278. // timestamp - (out) timestamp of the frame in milliseconds.
  279. // Returns:
  280. // False if any of the arguments are NULL, or if there is a parsing or
  281. // decoding error, or if there are no more frames. Otherwise, returns true.
  282. WEBP_EXTERN int WebPAnimDecoderGetNext(WebPAnimDecoder* dec,
  283. uint8_t** buf, int* timestamp);
  284. // Check if there are more frames left to decode.
  285. // Parameters:
  286. // dec - (in) decoder instance to be checked.
  287. // Returns:
  288. // True if 'dec' is not NULL and some frames are yet to be decoded.
  289. // Otherwise, returns false.
  290. WEBP_EXTERN int WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec);
  291. // Resets the WebPAnimDecoder object, so that next call to
  292. // WebPAnimDecoderGetNext() will restart decoding from 1st frame. This would be
  293. // helpful when all frames need to be decoded multiple times (e.g.
  294. // info.loop_count times) without destroying and recreating the 'dec' object.
  295. // Parameters:
  296. // dec - (in/out) decoder instance to be reset
  297. WEBP_EXTERN void WebPAnimDecoderReset(WebPAnimDecoder* dec);
  298. // Grab the internal demuxer object.
  299. // Getting the demuxer object can be useful if one wants to use operations only
  300. // available through demuxer; e.g. to get XMP/EXIF/ICC metadata. The returned
  301. // demuxer object is owned by 'dec' and is valid only until the next call to
  302. // WebPAnimDecoderDelete().
  303. //
  304. // Parameters:
  305. // dec - (in) decoder instance from which the demuxer object is to be fetched.
  306. WEBP_EXTERN const WebPDemuxer* WebPAnimDecoderGetDemuxer(
  307. const WebPAnimDecoder* dec);
  308. // Deletes the WebPAnimDecoder object.
  309. // Parameters:
  310. // dec - (in/out) decoder instance to be deleted
  311. WEBP_EXTERN void WebPAnimDecoderDelete(WebPAnimDecoder* dec);
  312. #ifdef __cplusplus
  313. } // extern "C"
  314. #endif
  315. #endif // WEBP_WEBP_DEMUX_H_