demux.h 16 KB

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