demux.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "./mux_types.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. #define WEBP_DEMUX_ABI_VERSION 0x0101 // MAJOR(8b) + MINOR(8b)
  50. // Note: forward declaring enumerations is not allowed in (strict) C and C++,
  51. // the types are left here for reference.
  52. // typedef enum WebPDemuxState WebPDemuxState;
  53. // typedef enum WebPFormatFeature WebPFormatFeature;
  54. typedef struct WebPDemuxer WebPDemuxer;
  55. typedef struct WebPIterator WebPIterator;
  56. typedef struct WebPChunkIterator WebPChunkIterator;
  57. //------------------------------------------------------------------------------
  58. // Returns the version number of the demux library, packed in hexadecimal using
  59. // 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507.
  60. WEBP_EXTERN(int) WebPGetDemuxVersion(void);
  61. //------------------------------------------------------------------------------
  62. // Life of a Demux object
  63. typedef enum WebPDemuxState {
  64. WEBP_DEMUX_PARSE_ERROR = -1, // An error occurred while parsing.
  65. WEBP_DEMUX_PARSING_HEADER = 0, // Not enough data to parse full header.
  66. WEBP_DEMUX_PARSED_HEADER = 1, // Header parsing complete,
  67. // data may be available.
  68. WEBP_DEMUX_DONE = 2 // Entire file has been parsed.
  69. } WebPDemuxState;
  70. // Internal, version-checked, entry point
  71. WEBP_EXTERN(WebPDemuxer*) WebPDemuxInternal(
  72. const WebPData*, int, WebPDemuxState*, int);
  73. // Parses the full WebP file given by 'data'.
  74. // Returns a WebPDemuxer object on successful parse, NULL otherwise.
  75. static WEBP_INLINE WebPDemuxer* WebPDemux(const WebPData* data) {
  76. return WebPDemuxInternal(data, 0, NULL, WEBP_DEMUX_ABI_VERSION);
  77. }
  78. // Parses the possibly incomplete WebP file given by 'data'.
  79. // If 'state' is non-NULL it will be set to indicate the status of the demuxer.
  80. // Returns NULL in case of error or if there isn't enough data to start parsing;
  81. // and a WebPDemuxer object on successful parse.
  82. // Note that WebPDemuxer keeps internal pointers to 'data' memory segment.
  83. // If this data is volatile, the demuxer object should be deleted (by calling
  84. // WebPDemuxDelete()) and WebPDemuxPartial() called again on the new data.
  85. // This is usually an inexpensive operation.
  86. static WEBP_INLINE WebPDemuxer* WebPDemuxPartial(
  87. const WebPData* data, WebPDemuxState* state) {
  88. return WebPDemuxInternal(data, 1, state, WEBP_DEMUX_ABI_VERSION);
  89. }
  90. // Frees memory associated with 'dmux'.
  91. WEBP_EXTERN(void) WebPDemuxDelete(WebPDemuxer* dmux);
  92. //------------------------------------------------------------------------------
  93. // Data/information extraction.
  94. typedef enum WebPFormatFeature {
  95. WEBP_FF_FORMAT_FLAGS, // Extended format flags present in the 'VP8X' chunk.
  96. WEBP_FF_CANVAS_WIDTH,
  97. WEBP_FF_CANVAS_HEIGHT,
  98. WEBP_FF_LOOP_COUNT,
  99. WEBP_FF_BACKGROUND_COLOR,
  100. WEBP_FF_FRAME_COUNT // Number of frames present in the demux object.
  101. // In case of a partial demux, this is the number of
  102. // frames seen so far, with the last frame possibly
  103. // being partial.
  104. } WebPFormatFeature;
  105. // Get the 'feature' value from the 'dmux'.
  106. // NOTE: values are only valid if WebPDemux() was used or WebPDemuxPartial()
  107. // returned a state > WEBP_DEMUX_PARSING_HEADER.
  108. WEBP_EXTERN(uint32_t) WebPDemuxGetI(
  109. const WebPDemuxer* dmux, WebPFormatFeature feature);
  110. //------------------------------------------------------------------------------
  111. // Frame iteration.
  112. struct WebPIterator {
  113. int frame_num;
  114. int num_frames; // equivalent to WEBP_FF_FRAME_COUNT.
  115. int fragment_num;
  116. int num_fragments;
  117. int x_offset, y_offset; // offset relative to the canvas.
  118. int width, height; // dimensions of this frame or fragment.
  119. int duration; // display duration in milliseconds.
  120. WebPMuxAnimDispose dispose_method; // dispose method for the frame.
  121. int complete; // true if 'fragment' contains a full frame. partial images
  122. // may still be decoded with the WebP incremental decoder.
  123. WebPData fragment; // The frame or fragment given by 'frame_num' and
  124. // 'fragment_num'.
  125. int has_alpha; // True if the frame or fragment contains transparency.
  126. WebPMuxAnimBlend blend_method; // Blend operation for the frame.
  127. uint32_t pad[2]; // padding for later use.
  128. void* private_; // for internal use only.
  129. };
  130. // Retrieves frame 'frame_number' from 'dmux'.
  131. // 'iter->fragment' points to the first fragment on return from this function.
  132. // Individual fragments may be extracted using WebPDemuxSelectFragment().
  133. // Setting 'frame_number' equal to 0 will return the last frame of the image.
  134. // Returns false if 'dmux' is NULL or frame 'frame_number' is not present.
  135. // Call WebPDemuxReleaseIterator() when use of the iterator is complete.
  136. // NOTE: 'dmux' must persist for the lifetime of 'iter'.
  137. WEBP_EXTERN(int) WebPDemuxGetFrame(
  138. const WebPDemuxer* dmux, int frame_number, WebPIterator* iter);
  139. // Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or
  140. // previous ('iter->frame_num' - 1) frame. These functions do not loop.
  141. // Returns true on success, false otherwise.
  142. WEBP_EXTERN(int) WebPDemuxNextFrame(WebPIterator* iter);
  143. WEBP_EXTERN(int) WebPDemuxPrevFrame(WebPIterator* iter);
  144. // Sets 'iter->fragment' to reflect fragment number 'fragment_num'.
  145. // Returns true if fragment 'fragment_num' is present, false otherwise.
  146. WEBP_EXTERN(int) WebPDemuxSelectFragment(WebPIterator* iter, int fragment_num);
  147. // Releases any memory associated with 'iter'.
  148. // Must be called before any subsequent calls to WebPDemuxGetChunk() on the same
  149. // iter. Also, must be called before destroying the associated WebPDemuxer with
  150. // WebPDemuxDelete().
  151. WEBP_EXTERN(void) WebPDemuxReleaseIterator(WebPIterator* iter);
  152. //------------------------------------------------------------------------------
  153. // Chunk iteration.
  154. struct WebPChunkIterator {
  155. // The current and total number of chunks with the fourcc given to
  156. // WebPDemuxGetChunk().
  157. int chunk_num;
  158. int num_chunks;
  159. WebPData chunk; // The payload of the chunk.
  160. uint32_t pad[6]; // padding for later use
  161. void* private_;
  162. };
  163. // Retrieves the 'chunk_number' instance of the chunk with id 'fourcc' from
  164. // 'dmux'.
  165. // 'fourcc' is a character array containing the fourcc of the chunk to return,
  166. // e.g., "ICCP", "XMP ", "EXIF", etc.
  167. // Setting 'chunk_number' equal to 0 will return the last chunk in a set.
  168. // Returns true if the chunk is found, false otherwise. Image related chunk
  169. // payloads are accessed through WebPDemuxGetFrame() and related functions.
  170. // Call WebPDemuxReleaseChunkIterator() when use of the iterator is complete.
  171. // NOTE: 'dmux' must persist for the lifetime of the iterator.
  172. WEBP_EXTERN(int) WebPDemuxGetChunk(const WebPDemuxer* dmux,
  173. const char fourcc[4], int chunk_number,
  174. WebPChunkIterator* iter);
  175. // Sets 'iter->chunk' to point to the next ('iter->chunk_num' + 1) or previous
  176. // ('iter->chunk_num' - 1) chunk. These functions do not loop.
  177. // Returns true on success, false otherwise.
  178. WEBP_EXTERN(int) WebPDemuxNextChunk(WebPChunkIterator* iter);
  179. WEBP_EXTERN(int) WebPDemuxPrevChunk(WebPChunkIterator* iter);
  180. // Releases any memory associated with 'iter'.
  181. // Must be called before destroying the associated WebPDemuxer with
  182. // WebPDemuxDelete().
  183. WEBP_EXTERN(void) WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter);
  184. //------------------------------------------------------------------------------
  185. #ifdef __cplusplus
  186. } // extern "C"
  187. #endif
  188. #endif /* WEBP_WEBP_DEMUX_H_ */