OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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 // Demux API. |
| 9 // Enables extraction of image and extended format data from WebP files. |
| 10 |
| 11 // Code Example: Demuxing WebP data to extract all the frames, ICC profile |
| 12 // and EXIF/XMP metadata. |
| 13 // |
| 14 // WebPDemuxer* demux = WebPDemux(&webp_data); |
| 15 // |
| 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 // |
| 21 // // ... (Iterate over all frames). |
| 22 // WebPIterator iter; |
| 23 // if (WebPDemuxGetFrame(demux, 1, &iter)) { |
| 24 // do { |
| 25 // // ... (Consume 'iter'; e.g. Decode 'iter.fragment' with WebPDecode(), |
| 26 // // ... and get other frame properties like width, height, offsets etc. |
| 27 // // ... see 'struct WebPIterator' below for more info). |
| 28 // } while (WebPDemuxNextFrame(&iter)); |
| 29 // WebPDemuxReleaseIterator(&iter); |
| 30 // } |
| 31 // |
| 32 // // ... (Extract metadata). |
| 33 // WebPChunkIterator chunk_iter; |
| 34 // if (flags & ICCP_FLAG) WebPDemuxGetChunk(demux, "ICCP", 1, &chunk_iter); |
| 35 // // ... (Consume the ICC profile in 'chunk_iter.chunk'). |
| 36 // WebPDemuxReleaseChunkIterator(&chunk_iter); |
| 37 // if (flags & EXIF_FLAG) WebPDemuxGetChunk(demux, "EXIF", 1, &chunk_iter); |
| 38 // // ... (Consume the EXIF metadata in 'chunk_iter.chunk'). |
| 39 // WebPDemuxReleaseChunkIterator(&chunk_iter); |
| 40 // if (flags & XMP_FLAG) WebPDemuxGetChunk(demux, "XMP ", 1, &chunk_iter); |
| 41 // // ... (Consume the XMP metadata in 'chunk_iter.chunk'). |
| 42 // WebPDemuxReleaseChunkIterator(&chunk_iter); |
| 43 // WebPDemuxDelete(demux); |
| 44 |
| 45 #ifndef WEBP_WEBP_DEMUX_H_ |
| 46 #define WEBP_WEBP_DEMUX_H_ |
| 47 |
| 48 #include "./mux_types.h" |
| 49 |
| 50 #if defined(__cplusplus) || defined(c_plusplus) |
| 51 extern "C" { |
| 52 #endif |
| 53 |
| 54 #define WEBP_DEMUX_ABI_VERSION 0x0100 // MAJOR(8b) + MINOR(8b) |
| 55 |
| 56 typedef struct WebPDemuxer WebPDemuxer; |
| 57 #if !(defined(__cplusplus) || defined(c_plusplus)) |
| 58 typedef enum WebPDemuxState WebPDemuxState; |
| 59 typedef enum WebPFormatFeature WebPFormatFeature; |
| 60 #endif |
| 61 typedef struct WebPIterator WebPIterator; |
| 62 typedef struct WebPChunkIterator WebPChunkIterator; |
| 63 |
| 64 //------------------------------------------------------------------------------ |
| 65 |
| 66 // Returns the version number of the demux library, packed in hexadecimal using |
| 67 // 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507. |
| 68 WEBP_EXTERN(int) WebPGetDemuxVersion(void); |
| 69 |
| 70 //------------------------------------------------------------------------------ |
| 71 // Life of a Demux object |
| 72 |
| 73 enum WebPDemuxState { |
| 74 WEBP_DEMUX_PARSING_HEADER, // Not enough data to parse full header. |
| 75 WEBP_DEMUX_PARSED_HEADER, // Header parsing complete, data may be available. |
| 76 WEBP_DEMUX_DONE // Entire file has been parsed. |
| 77 }; |
| 78 |
| 79 // Internal, version-checked, entry point |
| 80 WEBP_EXTERN(WebPDemuxer*) WebPDemuxInternal( |
| 81 const WebPData*, int, WebPDemuxState*, int); |
| 82 |
| 83 // Parses the full WebP file given by 'data'. |
| 84 // Returns a WebPDemuxer object on successful parse, NULL otherwise. |
| 85 static WEBP_INLINE WebPDemuxer* WebPDemux(const WebPData* data) { |
| 86 return WebPDemuxInternal(data, 0, NULL, WEBP_DEMUX_ABI_VERSION); |
| 87 } |
| 88 |
| 89 // Parses the possibly incomplete WebP file given by 'data'. |
| 90 // If 'state' is non-NULL it will be set to indicate the status of the demuxer. |
| 91 // Returns a WebPDemuxer object on successful parse, NULL otherwise. |
| 92 static WEBP_INLINE WebPDemuxer* WebPDemuxPartial( |
| 93 const WebPData* data, WebPDemuxState* state) { |
| 94 return WebPDemuxInternal(data, 1, state, WEBP_DEMUX_ABI_VERSION); |
| 95 } |
| 96 |
| 97 // Frees memory associated with 'dmux'. |
| 98 WEBP_EXTERN(void) WebPDemuxDelete(WebPDemuxer* dmux); |
| 99 |
| 100 //------------------------------------------------------------------------------ |
| 101 // Data/information extraction. |
| 102 |
| 103 enum WebPFormatFeature { |
| 104 WEBP_FF_FORMAT_FLAGS, // Extended format flags present in the 'VP8X' chunk. |
| 105 WEBP_FF_CANVAS_WIDTH, |
| 106 WEBP_FF_CANVAS_HEIGHT, |
| 107 WEBP_FF_LOOP_COUNT, |
| 108 WEBP_FF_BACKGROUND_COLOR, |
| 109 WEBP_FF_FRAME_COUNT // Number of frames present in the demux object. |
| 110 // In case of a partial demux, this is the number of |
| 111 // frames seen so far, with the last frame possibly |
| 112 // being partial. |
| 113 }; |
| 114 |
| 115 // Get the 'feature' value from the 'dmux'. |
| 116 // NOTE: values are only valid if WebPDemux() was used or WebPDemuxPartial() |
| 117 // returned a state > WEBP_DEMUX_PARSING_HEADER. |
| 118 WEBP_EXTERN(uint32_t) WebPDemuxGetI( |
| 119 const WebPDemuxer* dmux, WebPFormatFeature feature); |
| 120 |
| 121 //------------------------------------------------------------------------------ |
| 122 // Frame iteration. |
| 123 |
| 124 struct WebPIterator { |
| 125 int frame_num; |
| 126 int num_frames; // equivalent to WEBP_FF_FRAME_COUNT. |
| 127 int fragment_num; |
| 128 int num_fragments; |
| 129 int x_offset, y_offset; // offset relative to the canvas. |
| 130 int width, height; // dimensions of this frame or fragment. |
| 131 int duration; // display duration in milliseconds. |
| 132 WebPMuxAnimDispose dispose_method; // dispose method for the frame. |
| 133 int complete; // true if 'fragment' contains a full frame. partial images |
| 134 // may still be decoded with the WebP incremental decoder. |
| 135 WebPData fragment; // The frame or fragment given by 'frame_num' and |
| 136 // 'fragment_num'. |
| 137 |
| 138 uint32_t pad[4]; // padding for later use. |
| 139 void* private_; // for internal use only. |
| 140 }; |
| 141 |
| 142 // Retrieves frame 'frame_number' from 'dmux'. |
| 143 // 'iter->fragment' points to the first fragment on return from this function. |
| 144 // Individual fragments may be extracted using WebPDemuxSetFragment(). |
| 145 // Setting 'frame_number' equal to 0 will return the last frame of the image. |
| 146 // Returns false if 'dmux' is NULL or frame 'frame_number' is not present. |
| 147 // Call WebPDemuxReleaseIterator() when use of the iterator is complete. |
| 148 // NOTE: 'dmux' must persist for the lifetime of 'iter'. |
| 149 WEBP_EXTERN(int) WebPDemuxGetFrame( |
| 150 const WebPDemuxer* dmux, int frame_number, WebPIterator* iter); |
| 151 |
| 152 // Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or |
| 153 // previous ('iter->frame_num' - 1) frame. These functions do not loop. |
| 154 // Returns true on success, false otherwise. |
| 155 WEBP_EXTERN(int) WebPDemuxNextFrame(WebPIterator* iter); |
| 156 WEBP_EXTERN(int) WebPDemuxPrevFrame(WebPIterator* iter); |
| 157 |
| 158 // Sets 'iter->fragment' to reflect fragment number 'fragment_num'. |
| 159 // Returns true if fragment 'fragment_num' is present, false otherwise. |
| 160 WEBP_EXTERN(int) WebPDemuxSelectFragment(WebPIterator* iter, int fragment_num); |
| 161 |
| 162 // Releases any memory associated with 'iter'. |
| 163 // Must be called before any subsequent calls to WebPDemuxGetChunk() on the same |
| 164 // iter. Also, must be called before destroying the associated WebPDemuxer with |
| 165 // WebPDemuxDelete(). |
| 166 WEBP_EXTERN(void) WebPDemuxReleaseIterator(WebPIterator* iter); |
| 167 |
| 168 //------------------------------------------------------------------------------ |
| 169 // Chunk iteration. |
| 170 |
| 171 struct WebPChunkIterator { |
| 172 // The current and total number of chunks with the fourcc given to |
| 173 // WebPDemuxGetChunk(). |
| 174 int chunk_num; |
| 175 int num_chunks; |
| 176 WebPData chunk; // The payload of the chunk. |
| 177 |
| 178 uint32_t pad[6]; // padding for later use |
| 179 void* private_; |
| 180 }; |
| 181 |
| 182 // Retrieves the 'chunk_number' instance of the chunk with id 'fourcc' from |
| 183 // 'dmux'. |
| 184 // 'fourcc' is a character array containing the fourcc of the chunk to return, |
| 185 // e.g., "ICCP", "XMP ", "EXIF", etc. |
| 186 // Setting 'chunk_number' equal to 0 will return the last chunk in a set. |
| 187 // Returns true if the chunk is found, false otherwise. Image related chunk |
| 188 // payloads are accessed through WebPDemuxGetFrame() and related functions. |
| 189 // Call WebPDemuxReleaseChunkIterator() when use of the iterator is complete. |
| 190 // NOTE: 'dmux' must persist for the lifetime of the iterator. |
| 191 WEBP_EXTERN(int) WebPDemuxGetChunk(const WebPDemuxer* dmux, |
| 192 const char fourcc[4], int chunk_number, |
| 193 WebPChunkIterator* iter); |
| 194 |
| 195 // Sets 'iter->chunk' to point to the next ('iter->chunk_num' + 1) or previous |
| 196 // ('iter->chunk_num' - 1) chunk. These functions do not loop. |
| 197 // Returns true on success, false otherwise. |
| 198 WEBP_EXTERN(int) WebPDemuxNextChunk(WebPChunkIterator* iter); |
| 199 WEBP_EXTERN(int) WebPDemuxPrevChunk(WebPChunkIterator* iter); |
| 200 |
| 201 // Releases any memory associated with 'iter'. |
| 202 // Must be called before destroying the associated WebPDemuxer with |
| 203 // WebPDemuxDelete(). |
| 204 WEBP_EXTERN(void) WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter); |
| 205 |
| 206 //------------------------------------------------------------------------------ |
| 207 |
| 208 #if defined(__cplusplus) || defined(c_plusplus) |
| 209 } // extern "C" |
| 210 #endif |
| 211 |
| 212 #endif /* WEBP_WEBP_DEMUX_H_ */ |
OLD | NEW |