OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Demux API. | 10 // Demux API. |
(...skipping 30 matching lines...) Expand all Loading... |
41 WebPDemuxReleaseChunkIterator(&chunk_iter); | 41 WebPDemuxReleaseChunkIterator(&chunk_iter); |
42 if (flags & XMP_FLAG) WebPDemuxGetChunk(demux, "XMP ", 1, &chunk_iter); | 42 if (flags & XMP_FLAG) WebPDemuxGetChunk(demux, "XMP ", 1, &chunk_iter); |
43 // ... (Consume the XMP metadata in 'chunk_iter.chunk'). | 43 // ... (Consume the XMP metadata in 'chunk_iter.chunk'). |
44 WebPDemuxReleaseChunkIterator(&chunk_iter); | 44 WebPDemuxReleaseChunkIterator(&chunk_iter); |
45 WebPDemuxDelete(demux); | 45 WebPDemuxDelete(demux); |
46 */ | 46 */ |
47 | 47 |
48 #ifndef WEBP_WEBP_DEMUX_H_ | 48 #ifndef WEBP_WEBP_DEMUX_H_ |
49 #define WEBP_WEBP_DEMUX_H_ | 49 #define WEBP_WEBP_DEMUX_H_ |
50 | 50 |
| 51 #include "./decode.h" // for WEBP_CSP_MODE |
51 #include "./mux_types.h" | 52 #include "./mux_types.h" |
52 | 53 |
53 #ifdef __cplusplus | 54 #ifdef __cplusplus |
54 extern "C" { | 55 extern "C" { |
55 #endif | 56 #endif |
56 | 57 |
57 #define WEBP_DEMUX_ABI_VERSION 0x0101 // MAJOR(8b) + MINOR(8b) | 58 #define WEBP_DEMUX_ABI_VERSION 0x0107 // MAJOR(8b) + MINOR(8b) |
58 | 59 |
59 // Note: forward declaring enumerations is not allowed in (strict) C and C++, | 60 // Note: forward declaring enumerations is not allowed in (strict) C and C++, |
60 // the types are left here for reference. | 61 // the types are left here for reference. |
61 // typedef enum WebPDemuxState WebPDemuxState; | 62 // typedef enum WebPDemuxState WebPDemuxState; |
62 // typedef enum WebPFormatFeature WebPFormatFeature; | 63 // typedef enum WebPFormatFeature WebPFormatFeature; |
63 typedef struct WebPDemuxer WebPDemuxer; | 64 typedef struct WebPDemuxer WebPDemuxer; |
64 typedef struct WebPIterator WebPIterator; | 65 typedef struct WebPIterator WebPIterator; |
65 typedef struct WebPChunkIterator WebPChunkIterator; | 66 typedef struct WebPChunkIterator WebPChunkIterator; |
| 67 typedef struct WebPAnimInfo WebPAnimInfo; |
| 68 typedef struct WebPAnimDecoderOptions WebPAnimDecoderOptions; |
66 | 69 |
67 //------------------------------------------------------------------------------ | 70 //------------------------------------------------------------------------------ |
68 | 71 |
69 // Returns the version number of the demux library, packed in hexadecimal using | 72 // Returns the version number of the demux library, packed in hexadecimal using |
70 // 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507. | 73 // 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507. |
71 WEBP_EXTERN(int) WebPGetDemuxVersion(void); | 74 WEBP_EXTERN(int) WebPGetDemuxVersion(void); |
72 | 75 |
73 //------------------------------------------------------------------------------ | 76 //------------------------------------------------------------------------------ |
74 // Life of a Demux object | 77 // Life of a Demux object |
75 | 78 |
76 typedef enum WebPDemuxState { | 79 typedef enum WebPDemuxState { |
77 WEBP_DEMUX_PARSE_ERROR = -1, // An error occurred while parsing. | 80 WEBP_DEMUX_PARSE_ERROR = -1, // An error occurred while parsing. |
78 WEBP_DEMUX_PARSING_HEADER = 0, // Not enough data to parse full header. | 81 WEBP_DEMUX_PARSING_HEADER = 0, // Not enough data to parse full header. |
79 WEBP_DEMUX_PARSED_HEADER = 1, // Header parsing complete, | 82 WEBP_DEMUX_PARSED_HEADER = 1, // Header parsing complete, |
80 // data may be available. | 83 // data may be available. |
81 WEBP_DEMUX_DONE = 2 // Entire file has been parsed. | 84 WEBP_DEMUX_DONE = 2 // Entire file has been parsed. |
82 } WebPDemuxState; | 85 } WebPDemuxState; |
83 | 86 |
84 // Internal, version-checked, entry point | 87 // Internal, version-checked, entry point |
85 WEBP_EXTERN(WebPDemuxer*) WebPDemuxInternal( | 88 WEBP_EXTERN(WebPDemuxer*) WebPDemuxInternal( |
86 const WebPData*, int, WebPDemuxState*, int); | 89 const WebPData*, int, WebPDemuxState*, int); |
87 | 90 |
88 // Parses the full WebP file given by 'data'. | 91 // Parses the full WebP file given by 'data'. For single images the WebP file |
| 92 // header alone or the file header and the chunk header may be absent. |
89 // Returns a WebPDemuxer object on successful parse, NULL otherwise. | 93 // Returns a WebPDemuxer object on successful parse, NULL otherwise. |
90 static WEBP_INLINE WebPDemuxer* WebPDemux(const WebPData* data) { | 94 static WEBP_INLINE WebPDemuxer* WebPDemux(const WebPData* data) { |
91 return WebPDemuxInternal(data, 0, NULL, WEBP_DEMUX_ABI_VERSION); | 95 return WebPDemuxInternal(data, 0, NULL, WEBP_DEMUX_ABI_VERSION); |
92 } | 96 } |
93 | 97 |
94 // Parses the possibly incomplete WebP file given by 'data'. | 98 // Parses the possibly incomplete WebP file given by 'data'. |
95 // If 'state' is non-NULL it will be set to indicate the status of the demuxer. | 99 // If 'state' is non-NULL it will be set to indicate the status of the demuxer. |
96 // Returns NULL in case of error or if there isn't enough data to start parsing; | 100 // Returns NULL in case of error or if there isn't enough data to start parsing; |
97 // and a WebPDemuxer object on successful parse. | 101 // and a WebPDemuxer object on successful parse. |
98 // Note that WebPDemuxer keeps internal pointers to 'data' memory segment. | 102 // Note that WebPDemuxer keeps internal pointers to 'data' memory segment. |
(...skipping 28 matching lines...) Expand all Loading... |
127 // returned a state > WEBP_DEMUX_PARSING_HEADER. | 131 // returned a state > WEBP_DEMUX_PARSING_HEADER. |
128 WEBP_EXTERN(uint32_t) WebPDemuxGetI( | 132 WEBP_EXTERN(uint32_t) WebPDemuxGetI( |
129 const WebPDemuxer* dmux, WebPFormatFeature feature); | 133 const WebPDemuxer* dmux, WebPFormatFeature feature); |
130 | 134 |
131 //------------------------------------------------------------------------------ | 135 //------------------------------------------------------------------------------ |
132 // Frame iteration. | 136 // Frame iteration. |
133 | 137 |
134 struct WebPIterator { | 138 struct WebPIterator { |
135 int frame_num; | 139 int frame_num; |
136 int num_frames; // equivalent to WEBP_FF_FRAME_COUNT. | 140 int num_frames; // equivalent to WEBP_FF_FRAME_COUNT. |
137 int fragment_num; | |
138 int num_fragments; | |
139 int x_offset, y_offset; // offset relative to the canvas. | 141 int x_offset, y_offset; // offset relative to the canvas. |
140 int width, height; // dimensions of this frame or fragment. | 142 int width, height; // dimensions of this frame. |
141 int duration; // display duration in milliseconds. | 143 int duration; // display duration in milliseconds. |
142 WebPMuxAnimDispose dispose_method; // dispose method for the frame. | 144 WebPMuxAnimDispose dispose_method; // dispose method for the frame. |
143 int complete; // true if 'fragment' contains a full frame. partial images | 145 int complete; // true if 'fragment' contains a full frame. partial images |
144 // may still be decoded with the WebP incremental decoder. | 146 // may still be decoded with the WebP incremental decoder. |
145 WebPData fragment; // The frame or fragment given by 'frame_num' and | 147 WebPData fragment; // The frame given by 'frame_num'. Note for historical |
146 // 'fragment_num'. | 148 // reasons this is called a fragment. |
147 int has_alpha; // True if the frame or fragment contains transparency. | 149 int has_alpha; // True if the frame contains transparency. |
148 WebPMuxAnimBlend blend_method; // Blend operation for the frame. | 150 WebPMuxAnimBlend blend_method; // Blend operation for the frame. |
149 | 151 |
150 uint32_t pad[2]; // padding for later use. | 152 uint32_t pad[2]; // padding for later use. |
151 void* private_; // for internal use only. | 153 void* private_; // for internal use only. |
152 }; | 154 }; |
153 | 155 |
154 // Retrieves frame 'frame_number' from 'dmux'. | 156 // Retrieves frame 'frame_number' from 'dmux'. |
155 // 'iter->fragment' points to the first fragment on return from this function. | 157 // 'iter->fragment' points to the frame on return from this function. |
156 // Individual fragments may be extracted using WebPDemuxSelectFragment(). | |
157 // Setting 'frame_number' equal to 0 will return the last frame of the image. | 158 // Setting 'frame_number' equal to 0 will return the last frame of the image. |
158 // Returns false if 'dmux' is NULL or frame 'frame_number' is not present. | 159 // Returns false if 'dmux' is NULL or frame 'frame_number' is not present. |
159 // Call WebPDemuxReleaseIterator() when use of the iterator is complete. | 160 // Call WebPDemuxReleaseIterator() when use of the iterator is complete. |
160 // NOTE: 'dmux' must persist for the lifetime of 'iter'. | 161 // NOTE: 'dmux' must persist for the lifetime of 'iter'. |
161 WEBP_EXTERN(int) WebPDemuxGetFrame( | 162 WEBP_EXTERN(int) WebPDemuxGetFrame( |
162 const WebPDemuxer* dmux, int frame_number, WebPIterator* iter); | 163 const WebPDemuxer* dmux, int frame_number, WebPIterator* iter); |
163 | 164 |
164 // Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or | 165 // Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or |
165 // previous ('iter->frame_num' - 1) frame. These functions do not loop. | 166 // previous ('iter->frame_num' - 1) frame. These functions do not loop. |
166 // Returns true on success, false otherwise. | 167 // Returns true on success, false otherwise. |
167 WEBP_EXTERN(int) WebPDemuxNextFrame(WebPIterator* iter); | 168 WEBP_EXTERN(int) WebPDemuxNextFrame(WebPIterator* iter); |
168 WEBP_EXTERN(int) WebPDemuxPrevFrame(WebPIterator* iter); | 169 WEBP_EXTERN(int) WebPDemuxPrevFrame(WebPIterator* iter); |
169 | 170 |
170 // Sets 'iter->fragment' to reflect fragment number 'fragment_num'. | |
171 // Returns true if fragment 'fragment_num' is present, false otherwise. | |
172 WEBP_EXTERN(int) WebPDemuxSelectFragment(WebPIterator* iter, int fragment_num); | |
173 | |
174 // Releases any memory associated with 'iter'. | 171 // Releases any memory associated with 'iter'. |
175 // Must be called before any subsequent calls to WebPDemuxGetChunk() on the same | 172 // Must be called before any subsequent calls to WebPDemuxGetChunk() on the same |
176 // iter. Also, must be called before destroying the associated WebPDemuxer with | 173 // iter. Also, must be called before destroying the associated WebPDemuxer with |
177 // WebPDemuxDelete(). | 174 // WebPDemuxDelete(). |
178 WEBP_EXTERN(void) WebPDemuxReleaseIterator(WebPIterator* iter); | 175 WEBP_EXTERN(void) WebPDemuxReleaseIterator(WebPIterator* iter); |
179 | 176 |
180 //------------------------------------------------------------------------------ | 177 //------------------------------------------------------------------------------ |
181 // Chunk iteration. | 178 // Chunk iteration. |
182 | 179 |
183 struct WebPChunkIterator { | 180 struct WebPChunkIterator { |
(...skipping 25 matching lines...) Expand all Loading... |
209 // Returns true on success, false otherwise. | 206 // Returns true on success, false otherwise. |
210 WEBP_EXTERN(int) WebPDemuxNextChunk(WebPChunkIterator* iter); | 207 WEBP_EXTERN(int) WebPDemuxNextChunk(WebPChunkIterator* iter); |
211 WEBP_EXTERN(int) WebPDemuxPrevChunk(WebPChunkIterator* iter); | 208 WEBP_EXTERN(int) WebPDemuxPrevChunk(WebPChunkIterator* iter); |
212 | 209 |
213 // Releases any memory associated with 'iter'. | 210 // Releases any memory associated with 'iter'. |
214 // Must be called before destroying the associated WebPDemuxer with | 211 // Must be called before destroying the associated WebPDemuxer with |
215 // WebPDemuxDelete(). | 212 // WebPDemuxDelete(). |
216 WEBP_EXTERN(void) WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter); | 213 WEBP_EXTERN(void) WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter); |
217 | 214 |
218 //------------------------------------------------------------------------------ | 215 //------------------------------------------------------------------------------ |
| 216 // WebPAnimDecoder API |
| 217 // |
| 218 // This API allows decoding (possibly) animated WebP images. |
| 219 // |
| 220 // Code Example: |
| 221 /* |
| 222 WebPAnimDecoderOptions dec_options; |
| 223 WebPAnimDecoderOptionsInit(&dec_options); |
| 224 // Tune 'dec_options' as needed. |
| 225 WebPAnimDecoder* dec = WebPAnimDecoderNew(webp_data, &dec_options); |
| 226 WebPAnimInfo anim_info; |
| 227 WebPAnimDecoderGetInfo(dec, &anim_info); |
| 228 for (uint32_t i = 0; i < anim_info.loop_count; ++i) { |
| 229 while (WebPAnimDecoderHasMoreFrames(dec)) { |
| 230 uint8_t* buf; |
| 231 int timestamp; |
| 232 WebPAnimDecoderGetNext(dec, &buf, ×tamp); |
| 233 // ... (Render 'buf' based on 'timestamp'). |
| 234 // ... (Do NOT free 'buf', as it is owned by 'dec'). |
| 235 } |
| 236 WebPAnimDecoderReset(dec); |
| 237 } |
| 238 const WebPDemuxer* demuxer = WebPAnimDecoderGetDemuxer(dec); |
| 239 // ... (Do something using 'demuxer'; e.g. get EXIF/XMP/ICC data). |
| 240 WebPAnimDecoderDelete(dec); |
| 241 */ |
| 242 |
| 243 typedef struct WebPAnimDecoder WebPAnimDecoder; // Main opaque object. |
| 244 |
| 245 // Global options. |
| 246 struct WebPAnimDecoderOptions { |
| 247 // Output colorspace. Only the following modes are supported: |
| 248 // MODE_RGBA, MODE_BGRA, MODE_rgbA and MODE_bgrA. |
| 249 WEBP_CSP_MODE color_mode; |
| 250 int use_threads; // If true, use multi-threaded decoding. |
| 251 uint32_t padding[7]; // Padding for later use. |
| 252 }; |
| 253 |
| 254 // Internal, version-checked, entry point. |
| 255 WEBP_EXTERN(int) WebPAnimDecoderOptionsInitInternal( |
| 256 WebPAnimDecoderOptions*, int); |
| 257 |
| 258 // Should always be called, to initialize a fresh WebPAnimDecoderOptions |
| 259 // structure before modification. Returns false in case of version mismatch. |
| 260 // WebPAnimDecoderOptionsInit() must have succeeded before using the |
| 261 // 'dec_options' object. |
| 262 static WEBP_INLINE int WebPAnimDecoderOptionsInit( |
| 263 WebPAnimDecoderOptions* dec_options) { |
| 264 return WebPAnimDecoderOptionsInitInternal(dec_options, |
| 265 WEBP_DEMUX_ABI_VERSION); |
| 266 } |
| 267 |
| 268 // Internal, version-checked, entry point. |
| 269 WEBP_EXTERN(WebPAnimDecoder*) WebPAnimDecoderNewInternal( |
| 270 const WebPData*, const WebPAnimDecoderOptions*, int); |
| 271 |
| 272 // Creates and initializes a WebPAnimDecoder object. |
| 273 // Parameters: |
| 274 // webp_data - (in) WebP bitstream. This should remain unchanged during the |
| 275 // lifetime of the output WebPAnimDecoder object. |
| 276 // dec_options - (in) decoding options. Can be passed NULL to choose |
| 277 // reasonable defaults (in particular, color mode MODE_RGBA |
| 278 // will be picked). |
| 279 // Returns: |
| 280 // A pointer to the newly created WebPAnimDecoder object, or NULL in case of |
| 281 // parsing error, invalid option or memory error. |
| 282 static WEBP_INLINE WebPAnimDecoder* WebPAnimDecoderNew( |
| 283 const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options) { |
| 284 return WebPAnimDecoderNewInternal(webp_data, dec_options, |
| 285 WEBP_DEMUX_ABI_VERSION); |
| 286 } |
| 287 |
| 288 // Global information about the animation.. |
| 289 struct WebPAnimInfo { |
| 290 uint32_t canvas_width; |
| 291 uint32_t canvas_height; |
| 292 uint32_t loop_count; |
| 293 uint32_t bgcolor; |
| 294 uint32_t frame_count; |
| 295 uint32_t pad[4]; // padding for later use |
| 296 }; |
| 297 |
| 298 // Get global information about the animation. |
| 299 // Parameters: |
| 300 // dec - (in) decoder instance to get information from. |
| 301 // info - (out) global information fetched from the animation. |
| 302 // Returns: |
| 303 // True on success. |
| 304 WEBP_EXTERN(int) WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec, |
| 305 WebPAnimInfo* info); |
| 306 |
| 307 // Fetch the next frame from 'dec' based on options supplied to |
| 308 // WebPAnimDecoderNew(). This will be a fully reconstructed canvas of size |
| 309 // 'canvas_width * 4 * canvas_height', and not just the frame sub-rectangle. The |
| 310 // returned buffer 'buf' is valid only until the next call to |
| 311 // WebPAnimDecoderGetNext(), WebPAnimDecoderReset() or WebPAnimDecoderDelete(). |
| 312 // Parameters: |
| 313 // dec - (in/out) decoder instance from which the next frame is to be fetched. |
| 314 // buf - (out) decoded frame. |
| 315 // timestamp - (out) timestamp of the frame in milliseconds. |
| 316 // Returns: |
| 317 // False if any of the arguments are NULL, or if there is a parsing or |
| 318 // decoding error, or if there are no more frames. Otherwise, returns true. |
| 319 WEBP_EXTERN(int) WebPAnimDecoderGetNext(WebPAnimDecoder* dec, |
| 320 uint8_t** buf, int* timestamp); |
| 321 |
| 322 // Check if there are more frames left to decode. |
| 323 // Parameters: |
| 324 // dec - (in) decoder instance to be checked. |
| 325 // Returns: |
| 326 // True if 'dec' is not NULL and some frames are yet to be decoded. |
| 327 // Otherwise, returns false. |
| 328 WEBP_EXTERN(int) WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec); |
| 329 |
| 330 // Resets the WebPAnimDecoder object, so that next call to |
| 331 // WebPAnimDecoderGetNext() will restart decoding from 1st frame. This would be |
| 332 // helpful when all frames need to be decoded multiple times (e.g. |
| 333 // info.loop_count times) without destroying and recreating the 'dec' object. |
| 334 // Parameters: |
| 335 // dec - (in/out) decoder instance to be reset |
| 336 WEBP_EXTERN(void) WebPAnimDecoderReset(WebPAnimDecoder* dec); |
| 337 |
| 338 // Grab the internal demuxer object. |
| 339 // Getting the demuxer object can be useful if one wants to use operations only |
| 340 // available through demuxer; e.g. to get XMP/EXIF/ICC metadata. The returned |
| 341 // demuxer object is owned by 'dec' and is valid only until the next call to |
| 342 // WebPAnimDecoderDelete(). |
| 343 // |
| 344 // Parameters: |
| 345 // dec - (in) decoder instance from which the demuxer object is to be fetched. |
| 346 WEBP_EXTERN(const WebPDemuxer*) WebPAnimDecoderGetDemuxer( |
| 347 const WebPAnimDecoder* dec); |
| 348 |
| 349 // Deletes the WebPAnimDecoder object. |
| 350 // Parameters: |
| 351 // dec - (in/out) decoder instance to be deleted |
| 352 WEBP_EXTERN(void) WebPAnimDecoderDelete(WebPAnimDecoder* dec); |
219 | 353 |
220 #ifdef __cplusplus | 354 #ifdef __cplusplus |
221 } // extern "C" | 355 } // extern "C" |
222 #endif | 356 #endif |
223 | 357 |
224 #endif /* WEBP_WEBP_DEMUX_H_ */ | 358 #endif /* WEBP_WEBP_DEMUX_H_ */ |
OLD | NEW |