Path: blob/master/thirdparty/libwebp/src/webp/demux.h
21679 views
// 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 <stddef.h>5152#include "./decode.h" // for WEBP_CSP_MODE53#include "./mux_types.h"54#include "./types.h"5556#ifdef __cplusplus57extern "C" {58#endif5960#define WEBP_DEMUX_ABI_VERSION 0x0107 // MAJOR(8b) + MINOR(8b)6162// Note: forward declaring enumerations is not allowed in (strict) C and C++,63// the types are left here for reference.64// typedef enum WebPDemuxState WebPDemuxState;65// typedef enum WebPFormatFeature WebPFormatFeature;66typedef struct WebPDemuxer WebPDemuxer;67typedef struct WebPIterator WebPIterator;68typedef struct WebPChunkIterator WebPChunkIterator;69typedef struct WebPAnimInfo WebPAnimInfo;70typedef struct WebPAnimDecoderOptions WebPAnimDecoderOptions;7172//------------------------------------------------------------------------------7374// Returns the version number of the demux library, packed in hexadecimal using75// 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507.76WEBP_EXTERN int WebPGetDemuxVersion(void);7778//------------------------------------------------------------------------------79// Life of a Demux object8081typedef enum WebPDemuxState {82WEBP_DEMUX_PARSE_ERROR = -1, // An error occurred while parsing.83WEBP_DEMUX_PARSING_HEADER = 0, // Not enough data to parse full header.84WEBP_DEMUX_PARSED_HEADER = 1, // Header parsing complete,85// data may be available.86WEBP_DEMUX_DONE = 2 // Entire file has been parsed.87} WebPDemuxState;8889// Internal, version-checked, entry point90WEBP_NODISCARD WEBP_EXTERN WebPDemuxer* WebPDemuxInternal(91const WebPData*, int, WebPDemuxState*, int);9293// Parses the full WebP file given by 'data'. For single images the WebP file94// header alone or the file header and the chunk header may be absent.95// Returns a WebPDemuxer object on successful parse, NULL otherwise.96WEBP_NODISCARD static WEBP_INLINE WebPDemuxer* WebPDemux(const WebPData* data) {97return WebPDemuxInternal(data, 0, NULL, WEBP_DEMUX_ABI_VERSION);98}99100// Parses the possibly incomplete WebP file given by 'data'.101// If 'state' is non-NULL it will be set to indicate the status of the demuxer.102// Returns NULL in case of error or if there isn't enough data to start parsing;103// and a WebPDemuxer object on successful parse.104// Note that WebPDemuxer keeps internal pointers to 'data' memory segment.105// If this data is volatile, the demuxer object should be deleted (by calling106// WebPDemuxDelete()) and WebPDemuxPartial() called again on the new data.107// This is usually an inexpensive operation.108WEBP_NODISCARD static WEBP_INLINE WebPDemuxer* WebPDemuxPartial(109const WebPData* data, WebPDemuxState* state) {110return WebPDemuxInternal(data, 1, state, WEBP_DEMUX_ABI_VERSION);111}112113// Frees memory associated with 'dmux'.114WEBP_EXTERN void WebPDemuxDelete(WebPDemuxer* dmux);115116//------------------------------------------------------------------------------117// Data/information extraction.118119typedef enum WebPFormatFeature {120WEBP_FF_FORMAT_FLAGS, // bit-wise combination of WebPFeatureFlags121// corresponding to the 'VP8X' chunk (if present).122WEBP_FF_CANVAS_WIDTH,123WEBP_FF_CANVAS_HEIGHT,124WEBP_FF_LOOP_COUNT, // only relevant for animated file125WEBP_FF_BACKGROUND_COLOR, // idem.126WEBP_FF_FRAME_COUNT // Number of frames present in the demux object.127// In case of a partial demux, this is the number128// of frames seen so far, with the last frame129// possibly being partial.130} WebPFormatFeature;131132// Get the 'feature' value from the 'dmux'.133// NOTE: values are only valid if WebPDemux() was used or WebPDemuxPartial()134// returned a state > WEBP_DEMUX_PARSING_HEADER.135// If 'feature' is WEBP_FF_FORMAT_FLAGS, the returned value is a bit-wise136// combination of WebPFeatureFlags values.137// If 'feature' is WEBP_FF_LOOP_COUNT, WEBP_FF_BACKGROUND_COLOR, the returned138// value is only meaningful if the bitstream is animated.139WEBP_EXTERN uint32_t WebPDemuxGetI(140const WebPDemuxer* dmux, WebPFormatFeature feature);141142//------------------------------------------------------------------------------143// Frame iteration.144145struct WebPIterator {146int frame_num;147int num_frames; // equivalent to WEBP_FF_FRAME_COUNT.148int x_offset, y_offset; // offset relative to the canvas.149int width, height; // dimensions of this frame.150int duration; // display duration in milliseconds.151WebPMuxAnimDispose dispose_method; // dispose method for the frame.152int complete; // true if 'fragment' contains a full frame. partial images153// may still be decoded with the WebP incremental decoder.154WebPData fragment; // The frame given by 'frame_num'. Note for historical155// reasons this is called a fragment.156int has_alpha; // True if the frame contains transparency.157WebPMuxAnimBlend blend_method; // Blend operation for the frame.158159uint32_t pad[2]; // padding for later use.160void* private_; // for internal use only.161};162163// Retrieves frame 'frame_number' from 'dmux'.164// 'iter->fragment' points to the frame on return from this function.165// Setting 'frame_number' equal to 0 will return the last frame of the image.166// Returns false if 'dmux' is NULL or frame 'frame_number' is not present.167// Call WebPDemuxReleaseIterator() when use of the iterator is complete.168// NOTE: 'dmux' must persist for the lifetime of 'iter'.169WEBP_NODISCARD WEBP_EXTERN int WebPDemuxGetFrame(170const WebPDemuxer* dmux, int frame_number, WebPIterator* iter);171172// Sets 'iter->fragment' to point to the next ('iter->frame_num' + 1) or173// previous ('iter->frame_num' - 1) frame. These functions do not loop.174// Returns true on success, false otherwise.175WEBP_NODISCARD WEBP_EXTERN int WebPDemuxNextFrame(WebPIterator* iter);176WEBP_NODISCARD WEBP_EXTERN int WebPDemuxPrevFrame(WebPIterator* iter);177178// Releases any memory associated with 'iter'.179// Must be called before any subsequent calls to WebPDemuxGetChunk() on the same180// iter. Also, must be called before destroying the associated WebPDemuxer with181// WebPDemuxDelete().182WEBP_EXTERN void WebPDemuxReleaseIterator(WebPIterator* iter);183184//------------------------------------------------------------------------------185// Chunk iteration.186187struct WebPChunkIterator {188// The current and total number of chunks with the fourcc given to189// WebPDemuxGetChunk().190int chunk_num;191int num_chunks;192WebPData chunk; // The payload of the chunk.193194uint32_t pad[6]; // padding for later use195void* private_;196};197198// Retrieves the 'chunk_number' instance of the chunk with id 'fourcc' from199// 'dmux'.200// 'fourcc' is a character array containing the fourcc of the chunk to return,201// e.g., "ICCP", "XMP ", "EXIF", etc.202// Setting 'chunk_number' equal to 0 will return the last chunk in a set.203// Returns true if the chunk is found, false otherwise. Image related chunk204// payloads are accessed through WebPDemuxGetFrame() and related functions.205// Call WebPDemuxReleaseChunkIterator() when use of the iterator is complete.206// NOTE: 'dmux' must persist for the lifetime of the iterator.207WEBP_NODISCARD WEBP_EXTERN int WebPDemuxGetChunk(const WebPDemuxer* dmux,208const char fourcc[4],209int chunk_number,210WebPChunkIterator* iter);211212// Sets 'iter->chunk' to point to the next ('iter->chunk_num' + 1) or previous213// ('iter->chunk_num' - 1) chunk. These functions do not loop.214// Returns true on success, false otherwise.215WEBP_NODISCARD WEBP_EXTERN int WebPDemuxNextChunk(WebPChunkIterator* iter);216WEBP_NODISCARD WEBP_EXTERN int WebPDemuxPrevChunk(WebPChunkIterator* iter);217218// Releases any memory associated with 'iter'.219// Must be called before destroying the associated WebPDemuxer with220// WebPDemuxDelete().221WEBP_EXTERN void WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter);222223//------------------------------------------------------------------------------224// WebPAnimDecoder API225//226// This API allows decoding (possibly) animated WebP images.227//228// Code Example:229/*230WebPAnimDecoderOptions dec_options;231WebPAnimDecoderOptionsInit(&dec_options);232// Tune 'dec_options' as needed.233WebPAnimDecoder* dec = WebPAnimDecoderNew(webp_data, &dec_options);234WebPAnimInfo anim_info;235WebPAnimDecoderGetInfo(dec, &anim_info);236for (uint32_t i = 0; i < anim_info.loop_count; ++i) {237while (WebPAnimDecoderHasMoreFrames(dec)) {238uint8_t* buf;239int timestamp;240WebPAnimDecoderGetNext(dec, &buf, ×tamp);241// ... (Render 'buf' based on 'timestamp').242// ... (Do NOT free 'buf', as it is owned by 'dec').243}244WebPAnimDecoderReset(dec);245}246const WebPDemuxer* demuxer = WebPAnimDecoderGetDemuxer(dec);247// ... (Do something using 'demuxer'; e.g. get EXIF/XMP/ICC data).248WebPAnimDecoderDelete(dec);249*/250251typedef struct WebPAnimDecoder WebPAnimDecoder; // Main opaque object.252253// Global options.254struct WebPAnimDecoderOptions {255// Output colorspace. Only the following modes are supported:256// MODE_RGBA, MODE_BGRA, MODE_rgbA and MODE_bgrA.257WEBP_CSP_MODE color_mode;258int use_threads; // If true, use multi-threaded decoding.259uint32_t padding[7]; // Padding for later use.260};261262// Internal, version-checked, entry point.263WEBP_NODISCARD WEBP_EXTERN int WebPAnimDecoderOptionsInitInternal(264WebPAnimDecoderOptions*, int);265266// Should always be called, to initialize a fresh WebPAnimDecoderOptions267// structure before modification. Returns false in case of version mismatch.268// WebPAnimDecoderOptionsInit() must have succeeded before using the269// 'dec_options' object.270WEBP_NODISCARD static WEBP_INLINE int WebPAnimDecoderOptionsInit(271WebPAnimDecoderOptions* dec_options) {272return WebPAnimDecoderOptionsInitInternal(dec_options,273WEBP_DEMUX_ABI_VERSION);274}275276// Internal, version-checked, entry point.277WEBP_NODISCARD WEBP_EXTERN WebPAnimDecoder* WebPAnimDecoderNewInternal(278const WebPData*, const WebPAnimDecoderOptions*, int);279280// Creates and initializes a WebPAnimDecoder object.281// Parameters:282// webp_data - (in) WebP bitstream. This should remain unchanged during the283// lifetime of the output WebPAnimDecoder object.284// dec_options - (in) decoding options. Can be passed NULL to choose285// reasonable defaults (in particular, color mode MODE_RGBA286// will be picked).287// Returns:288// A pointer to the newly created WebPAnimDecoder object, or NULL in case of289// parsing error, invalid option or memory error.290WEBP_NODISCARD static WEBP_INLINE WebPAnimDecoder* WebPAnimDecoderNew(291const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options) {292return WebPAnimDecoderNewInternal(webp_data, dec_options,293WEBP_DEMUX_ABI_VERSION);294}295296// Global information about the animation..297struct WebPAnimInfo {298uint32_t canvas_width;299uint32_t canvas_height;300uint32_t loop_count;301uint32_t bgcolor;302uint32_t frame_count;303uint32_t pad[4]; // padding for later use304};305306// Get global information about the animation.307// Parameters:308// dec - (in) decoder instance to get information from.309// info - (out) global information fetched from the animation.310// Returns:311// True on success.312WEBP_NODISCARD WEBP_EXTERN int WebPAnimDecoderGetInfo(313const WebPAnimDecoder* dec, WebPAnimInfo* info);314315// Fetch the next frame from 'dec' based on options supplied to316// WebPAnimDecoderNew(). This will be a fully reconstructed canvas of size317// 'canvas_width * 4 * canvas_height', and not just the frame sub-rectangle. The318// returned buffer 'buf' is valid only until the next call to319// WebPAnimDecoderGetNext(), WebPAnimDecoderReset() or WebPAnimDecoderDelete().320// Parameters:321// dec - (in/out) decoder instance from which the next frame is to be fetched.322// buf - (out) decoded frame.323// timestamp - (out) timestamp of the frame in milliseconds.324// Returns:325// False if any of the arguments are NULL, or if there is a parsing or326// decoding error, or if there are no more frames. Otherwise, returns true.327WEBP_NODISCARD WEBP_EXTERN int WebPAnimDecoderGetNext(WebPAnimDecoder* dec,328uint8_t** buf,329int* timestamp);330331// Check if there are more frames left to decode.332// Parameters:333// dec - (in) decoder instance to be checked.334// Returns:335// True if 'dec' is not NULL and some frames are yet to be decoded.336// Otherwise, returns false.337WEBP_NODISCARD WEBP_EXTERN int WebPAnimDecoderHasMoreFrames(338const WebPAnimDecoder* dec);339340// Resets the WebPAnimDecoder object, so that next call to341// WebPAnimDecoderGetNext() will restart decoding from 1st frame. This would be342// helpful when all frames need to be decoded multiple times (e.g.343// info.loop_count times) without destroying and recreating the 'dec' object.344// Parameters:345// dec - (in/out) decoder instance to be reset346WEBP_EXTERN void WebPAnimDecoderReset(WebPAnimDecoder* dec);347348// Grab the internal demuxer object.349// Getting the demuxer object can be useful if one wants to use operations only350// available through demuxer; e.g. to get XMP/EXIF/ICC metadata. The returned351// demuxer object is owned by 'dec' and is valid only until the next call to352// WebPAnimDecoderDelete().353//354// Parameters:355// dec - (in) decoder instance from which the demuxer object is to be fetched.356WEBP_NODISCARD WEBP_EXTERN const WebPDemuxer* WebPAnimDecoderGetDemuxer(357const WebPAnimDecoder* dec);358359// Deletes the WebPAnimDecoder object.360// Parameters:361// dec - (in/out) decoder instance to be deleted362WEBP_EXTERN void WebPAnimDecoderDelete(WebPAnimDecoder* dec);363364#ifdef __cplusplus365} // extern "C"366#endif367368#endif // WEBP_WEBP_DEMUX_H_369370371