// Copyright 2012 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Demux API.10// Enables extraction of image and extended format data from WebP files.1112// Code Example: Demuxing WebP data to extract all the frames, ICC profile13// and EXIF/XMP metadata.14/*15WebPDemuxer* demux = WebPDemux(&webp_data);1617uint32_t width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);18uint32_t height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);19// ... (Get information about the features present in the WebP file).20uint32_t flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);2122// ... (Iterate over all frames).23WebPIterator iter;24if (WebPDemuxGetFrame(demux, 1, &iter)) {25do {26// ... (Consume 'iter'; e.g. Decode 'iter.fragment' with WebPDecode(),27// ... and get other frame properties like width, height, offsets etc.28// ... see 'struct WebPIterator' below for more info).29} while (WebPDemuxNextFrame(&iter));30WebPDemuxReleaseIterator(&iter);31}3233// ... (Extract metadata).34WebPChunkIterator chunk_iter;35if (flags & ICCP_FLAG) WebPDemuxGetChunk(demux, "ICCP", 1, &chunk_iter);36// ... (Consume the ICC profile in 'chunk_iter.chunk').37WebPDemuxReleaseChunkIterator(&chunk_iter);38if (flags & EXIF_FLAG) WebPDemuxGetChunk(demux, "EXIF", 1, &chunk_iter);39// ... (Consume the EXIF metadata in 'chunk_iter.chunk').40WebPDemuxReleaseChunkIterator(&chunk_iter);41if (flags & XMP_FLAG) WebPDemuxGetChunk(demux, "XMP ", 1, &chunk_iter);42// ... (Consume the XMP metadata in 'chunk_iter.chunk').43WebPDemuxReleaseChunkIterator(&chunk_iter);44WebPDemuxDelete(demux);45*/4647#ifndef WEBP_WEBP_DEMUX_H_48#define WEBP_WEBP_DEMUX_H_4950#include "./decode.h" // for WEBP_CSP_MODE51#include "./mux_types.h"5253#ifdef __cplusplus54extern "C" {55#endif5657#define WEBP_DEMUX_ABI_VERSION 0x0107 // MAJOR(8b) + MINOR(8b)5859// Note: forward declaring enumerations is not allowed in (strict) C and C++,60// the types are left here for reference.61// typedef enum WebPDemuxState WebPDemuxState;62// typedef enum WebPFormatFeature WebPFormatFeature;63typedef struct WebPDemuxer WebPDemuxer;64typedef struct WebPIterator WebPIterator;65typedef struct WebPChunkIterator WebPChunkIterator;66typedef struct WebPAnimInfo WebPAnimInfo;67typedef struct WebPAnimDecoderOptions WebPAnimDecoderOptions;6869//------------------------------------------------------------------------------7071// Returns the version number of the demux library, packed in hexadecimal using72// 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507.73WEBP_EXTERN int WebPGetDemuxVersion(void);7475//------------------------------------------------------------------------------76// Life of a Demux object7778typedef enum WebPDemuxState {79WEBP_DEMUX_PARSE_ERROR = -1, // An error occurred while parsing.80WEBP_DEMUX_PARSING_HEADER = 0, // Not enough data to parse full header.81WEBP_DEMUX_PARSED_HEADER = 1, // Header parsing complete,82// data may be available.83WEBP_DEMUX_DONE = 2 // Entire file has been parsed.84} WebPDemuxState;8586// Internal, version-checked, entry point87WEBP_EXTERN WebPDemuxer* WebPDemuxInternal(88const WebPData*, int, WebPDemuxState*, int);8990// Parses the full WebP file given by 'data'. For single images the WebP file91// header alone or the file header and the chunk header may be absent.92// Returns a WebPDemuxer object on successful parse, NULL otherwise.93static WEBP_INLINE WebPDemuxer* WebPDemux(const WebPData* data) {94return WebPDemuxInternal(data, 0, NULL, WEBP_DEMUX_ABI_VERSION);95}9697// Parses the possibly incomplete WebP file given by 'data'.98// If 'state' is non-NULL it will be set to indicate the status of the demuxer.99// Returns NULL in case of error or if there isn't enough data to start parsing;100// and a WebPDemuxer object on successful parse.101// Note that WebPDemuxer keeps internal pointers to 'data' memory segment.102// If this data is volatile, the demuxer object should be deleted (by calling103// WebPDemuxDelete()) and WebPDemuxPartial() called again on the new data.104// This is usually an inexpensive operation.105static WEBP_INLINE WebPDemuxer* WebPDemuxPartial(106const WebPData* data, WebPDemuxState* state) {107return WebPDemuxInternal(data, 1, state, WEBP_DEMUX_ABI_VERSION);108}109110// Frees memory associated with 'dmux'.111WEBP_EXTERN void WebPDemuxDelete(WebPDemuxer* dmux);112113//------------------------------------------------------------------------------114// Data/information extraction.115116typedef enum WebPFormatFeature {117WEBP_FF_FORMAT_FLAGS, // bit-wise combination of WebPFeatureFlags118// corresponding to the 'VP8X' chunk (if present).119WEBP_FF_CANVAS_WIDTH,120WEBP_FF_CANVAS_HEIGHT,121WEBP_FF_LOOP_COUNT, // only relevant for animated file122WEBP_FF_BACKGROUND_COLOR, // idem.123WEBP_FF_FRAME_COUNT // Number of frames present in the demux object.124// In case of a partial demux, this is the number125// of frames seen so far, with the last frame126// possibly being partial.127} WebPFormatFeature;128129// Get the 'feature' value from the 'dmux'.130// NOTE: values are only valid if WebPDemux() was used or WebPDemuxPartial()131// returned a state > WEBP_DEMUX_PARSING_HEADER.132// If 'feature' is WEBP_FF_FORMAT_FLAGS, the returned value is a bit-wise133// combination of WebPFeatureFlags values.134// If 'feature' is WEBP_FF_LOOP_COUNT, WEBP_FF_BACKGROUND_COLOR, the returned135// value is only meaningful if the bitstream is animated.136WEBP_EXTERN uint32_t WebPDemuxGetI(137const WebPDemuxer* dmux, WebPFormatFeature feature);138139//------------------------------------------------------------------------------140// Frame iteration.141142struct WebPIterator {143int frame_num;144int num_frames; // equivalent to WEBP_FF_FRAME_COUNT.145int x_offset, y_offset; // offset relative to the canvas.146int width, height; // dimensions of this frame.147int duration; // display duration in milliseconds.148WebPMuxAnimDispose dispose_method; // dispose method for the frame.149int complete; // true if 'fragment' contains a full frame. partial images150// may still be decoded with the WebP incremental decoder.151WebPData fragment; // The frame given by 'frame_num'. Note for historical152// reasons this is called a fragment.153int has_alpha; // True if the frame contains transparency.154WebPMuxAnimBlend blend_method; // Blend operation for the frame.155156uint32_t pad[2]; // padding for later use.157void* private_; // for internal use only.158};159160// Retrieves frame 'frame_number' from 'dmux'.161// 'iter->fragment' points to the frame on return from this function.162// Setting 'frame_number' equal to 0 will return the last frame of the image.163// Returns false if 'dmux' is NULL or frame 'frame_number' is not present.164// Call WebPDemuxReleaseIterator() when use of the iterator is complete.165// NOTE: 'dmux' must persist for the lifetime of 'iter'.166WEBP_EXTERN int WebPDemuxGetFrame(167const WebPDemuxer* dmux, int frame_number, WebPIterator* iter);168169// Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or170// previous ('iter->frame_num' - 1) frame. These functions do not loop.171// Returns true on success, false otherwise.172WEBP_EXTERN int WebPDemuxNextFrame(WebPIterator* iter);173WEBP_EXTERN int WebPDemuxPrevFrame(WebPIterator* iter);174175// Releases any memory associated with 'iter'.176// Must be called before any subsequent calls to WebPDemuxGetChunk() on the same177// iter. Also, must be called before destroying the associated WebPDemuxer with178// WebPDemuxDelete().179WEBP_EXTERN void WebPDemuxReleaseIterator(WebPIterator* iter);180181//------------------------------------------------------------------------------182// Chunk iteration.183184struct WebPChunkIterator {185// The current and total number of chunks with the fourcc given to186// WebPDemuxGetChunk().187int chunk_num;188int num_chunks;189WebPData chunk; // The payload of the chunk.190191uint32_t pad[6]; // padding for later use192void* private_;193};194195// Retrieves the 'chunk_number' instance of the chunk with id 'fourcc' from196// 'dmux'.197// 'fourcc' is a character array containing the fourcc of the chunk to return,198// e.g., "ICCP", "XMP ", "EXIF", etc.199// Setting 'chunk_number' equal to 0 will return the last chunk in a set.200// Returns true if the chunk is found, false otherwise. Image related chunk201// payloads are accessed through WebPDemuxGetFrame() and related functions.202// Call WebPDemuxReleaseChunkIterator() when use of the iterator is complete.203// NOTE: 'dmux' must persist for the lifetime of the iterator.204WEBP_EXTERN int WebPDemuxGetChunk(const WebPDemuxer* dmux,205const char fourcc[4], int chunk_number,206WebPChunkIterator* iter);207208// Sets 'iter->chunk' to point to the next ('iter->chunk_num' + 1) or previous209// ('iter->chunk_num' - 1) chunk. These functions do not loop.210// Returns true on success, false otherwise.211WEBP_EXTERN int WebPDemuxNextChunk(WebPChunkIterator* iter);212WEBP_EXTERN int WebPDemuxPrevChunk(WebPChunkIterator* iter);213214// Releases any memory associated with 'iter'.215// Must be called before destroying the associated WebPDemuxer with216// WebPDemuxDelete().217WEBP_EXTERN void WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter);218219//------------------------------------------------------------------------------220// WebPAnimDecoder API221//222// This API allows decoding (possibly) animated WebP images.223//224// Code Example:225/*226WebPAnimDecoderOptions dec_options;227WebPAnimDecoderOptionsInit(&dec_options);228// Tune 'dec_options' as needed.229WebPAnimDecoder* dec = WebPAnimDecoderNew(webp_data, &dec_options);230WebPAnimInfo anim_info;231WebPAnimDecoderGetInfo(dec, &anim_info);232for (uint32_t i = 0; i < anim_info.loop_count; ++i) {233while (WebPAnimDecoderHasMoreFrames(dec)) {234uint8_t* buf;235int timestamp;236WebPAnimDecoderGetNext(dec, &buf, ×tamp);237// ... (Render 'buf' based on 'timestamp').238// ... (Do NOT free 'buf', as it is owned by 'dec').239}240WebPAnimDecoderReset(dec);241}242const WebPDemuxer* demuxer = WebPAnimDecoderGetDemuxer(dec);243// ... (Do something using 'demuxer'; e.g. get EXIF/XMP/ICC data).244WebPAnimDecoderDelete(dec);245*/246247typedef struct WebPAnimDecoder WebPAnimDecoder; // Main opaque object.248249// Global options.250struct WebPAnimDecoderOptions {251// Output colorspace. Only the following modes are supported:252// MODE_RGBA, MODE_BGRA, MODE_rgbA and MODE_bgrA.253WEBP_CSP_MODE color_mode;254int use_threads; // If true, use multi-threaded decoding.255uint32_t padding[7]; // Padding for later use.256};257258// Internal, version-checked, entry point.259WEBP_EXTERN int WebPAnimDecoderOptionsInitInternal(260WebPAnimDecoderOptions*, int);261262// Should always be called, to initialize a fresh WebPAnimDecoderOptions263// structure before modification. Returns false in case of version mismatch.264// WebPAnimDecoderOptionsInit() must have succeeded before using the265// 'dec_options' object.266static WEBP_INLINE int WebPAnimDecoderOptionsInit(267WebPAnimDecoderOptions* dec_options) {268return WebPAnimDecoderOptionsInitInternal(dec_options,269WEBP_DEMUX_ABI_VERSION);270}271272// Internal, version-checked, entry point.273WEBP_EXTERN WebPAnimDecoder* WebPAnimDecoderNewInternal(274const WebPData*, const WebPAnimDecoderOptions*, int);275276// Creates and initializes a WebPAnimDecoder object.277// Parameters:278// webp_data - (in) WebP bitstream. This should remain unchanged during the279// lifetime of the output WebPAnimDecoder object.280// dec_options - (in) decoding options. Can be passed NULL to choose281// reasonable defaults (in particular, color mode MODE_RGBA282// will be picked).283// Returns:284// A pointer to the newly created WebPAnimDecoder object, or NULL in case of285// parsing error, invalid option or memory error.286static WEBP_INLINE WebPAnimDecoder* WebPAnimDecoderNew(287const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options) {288return WebPAnimDecoderNewInternal(webp_data, dec_options,289WEBP_DEMUX_ABI_VERSION);290}291292// Global information about the animation..293struct WebPAnimInfo {294uint32_t canvas_width;295uint32_t canvas_height;296uint32_t loop_count;297uint32_t bgcolor;298uint32_t frame_count;299uint32_t pad[4]; // padding for later use300};301302// Get global information about the animation.303// Parameters:304// dec - (in) decoder instance to get information from.305// info - (out) global information fetched from the animation.306// Returns:307// True on success.308WEBP_EXTERN int WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec,309WebPAnimInfo* info);310311// Fetch the next frame from 'dec' based on options supplied to312// WebPAnimDecoderNew(). This will be a fully reconstructed canvas of size313// 'canvas_width * 4 * canvas_height', and not just the frame sub-rectangle. The314// returned buffer 'buf' is valid only until the next call to315// WebPAnimDecoderGetNext(), WebPAnimDecoderReset() or WebPAnimDecoderDelete().316// Parameters:317// dec - (in/out) decoder instance from which the next frame is to be fetched.318// buf - (out) decoded frame.319// timestamp - (out) timestamp of the frame in milliseconds.320// Returns:321// False if any of the arguments are NULL, or if there is a parsing or322// decoding error, or if there are no more frames. Otherwise, returns true.323WEBP_EXTERN int WebPAnimDecoderGetNext(WebPAnimDecoder* dec,324uint8_t** buf, int* timestamp);325326// Check if there are more frames left to decode.327// Parameters:328// dec - (in) decoder instance to be checked.329// Returns:330// True if 'dec' is not NULL and some frames are yet to be decoded.331// Otherwise, returns false.332WEBP_EXTERN int WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec);333334// Resets the WebPAnimDecoder object, so that next call to335// WebPAnimDecoderGetNext() will restart decoding from 1st frame. This would be336// helpful when all frames need to be decoded multiple times (e.g.337// info.loop_count times) without destroying and recreating the 'dec' object.338// Parameters:339// dec - (in/out) decoder instance to be reset340WEBP_EXTERN void WebPAnimDecoderReset(WebPAnimDecoder* dec);341342// Grab the internal demuxer object.343// Getting the demuxer object can be useful if one wants to use operations only344// available through demuxer; e.g. to get XMP/EXIF/ICC metadata. The returned345// demuxer object is owned by 'dec' and is valid only until the next call to346// WebPAnimDecoderDelete().347//348// Parameters:349// dec - (in) decoder instance from which the demuxer object is to be fetched.350WEBP_EXTERN const WebPDemuxer* WebPAnimDecoderGetDemuxer(351const WebPAnimDecoder* dec);352353// Deletes the WebPAnimDecoder object.354// Parameters:355// dec - (in/out) decoder instance to be deleted356WEBP_EXTERN void WebPAnimDecoderDelete(WebPAnimDecoder* dec);357358#ifdef __cplusplus359} // extern "C"360#endif361362#endif /* WEBP_WEBP_DEMUX_H_ */363364365