// Copyright 2011 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// RIFF container manipulation and encoding for WebP images.10//11// Authors: Urvang ([email protected])12// Vikas ([email protected])1314#ifndef WEBP_WEBP_MUX_H_15#define WEBP_WEBP_MUX_H_1617#include "./mux_types.h"1819#ifdef __cplusplus20extern "C" {21#endif2223#define WEBP_MUX_ABI_VERSION 0x0108 // MAJOR(8b) + MINOR(8b)2425//------------------------------------------------------------------------------26// Mux API27//28// This API allows manipulation of WebP container images containing features29// like color profile, metadata, animation.30//31// Code Example#1: Create a WebPMux object with image data, color profile and32// XMP metadata.33/*34int copy_data = 0;35WebPMux* mux = WebPMuxNew();36// ... (Prepare image data).37WebPMuxSetImage(mux, &image, copy_data);38// ... (Prepare ICCP color profile data).39WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data);40// ... (Prepare XMP metadata).41WebPMuxSetChunk(mux, "XMP ", &xmp, copy_data);42// Get data from mux in WebP RIFF format.43WebPMuxAssemble(mux, &output_data);44WebPMuxDelete(mux);45// ... (Consume output_data; e.g. write output_data.bytes to file).46WebPDataClear(&output_data);47*/4849// Code Example#2: Get image and color profile data from a WebP file.50/*51int copy_data = 0;52// ... (Read data from file).53WebPMux* mux = WebPMuxCreate(&data, copy_data);54WebPMuxGetFrame(mux, 1, &image);55// ... (Consume image; e.g. call WebPDecode() to decode the data).56WebPMuxGetChunk(mux, "ICCP", &icc_profile);57// ... (Consume icc_data).58WebPMuxDelete(mux);59free(data);60*/6162// Note: forward declaring enumerations is not allowed in (strict) C and C++,63// the types are left here for reference.64// typedef enum WebPMuxError WebPMuxError;65// typedef enum WebPChunkId WebPChunkId;66typedef struct WebPMux WebPMux; // main opaque object.67typedef struct WebPMuxFrameInfo WebPMuxFrameInfo;68typedef struct WebPMuxAnimParams WebPMuxAnimParams;69typedef struct WebPAnimEncoderOptions WebPAnimEncoderOptions;7071// Error codes72typedef enum WebPMuxError {73WEBP_MUX_OK = 1,74WEBP_MUX_NOT_FOUND = 0,75WEBP_MUX_INVALID_ARGUMENT = -1,76WEBP_MUX_BAD_DATA = -2,77WEBP_MUX_MEMORY_ERROR = -3,78WEBP_MUX_NOT_ENOUGH_DATA = -479} WebPMuxError;8081// IDs for different types of chunks.82typedef enum WebPChunkId {83WEBP_CHUNK_VP8X, // VP8X84WEBP_CHUNK_ICCP, // ICCP85WEBP_CHUNK_ANIM, // ANIM86WEBP_CHUNK_ANMF, // ANMF87WEBP_CHUNK_DEPRECATED, // (deprecated from FRGM)88WEBP_CHUNK_ALPHA, // ALPH89WEBP_CHUNK_IMAGE, // VP8/VP8L90WEBP_CHUNK_EXIF, // EXIF91WEBP_CHUNK_XMP, // XMP92WEBP_CHUNK_UNKNOWN, // Other chunks.93WEBP_CHUNK_NIL94} WebPChunkId;9596//------------------------------------------------------------------------------9798// Returns the version number of the mux library, packed in hexadecimal using99// 8bits for each of major/minor/revision. E.g: v2.5.7 is 0x020507.100WEBP_EXTERN int WebPGetMuxVersion(void);101102//------------------------------------------------------------------------------103// Life of a Mux object104105// Internal, version-checked, entry point106WEBP_EXTERN WebPMux* WebPNewInternal(int);107108// Creates an empty mux object.109// Returns:110// A pointer to the newly created empty mux object.111// Or NULL in case of memory error.112static WEBP_INLINE WebPMux* WebPMuxNew(void) {113return WebPNewInternal(WEBP_MUX_ABI_VERSION);114}115116// Deletes the mux object.117// Parameters:118// mux - (in/out) object to be deleted119WEBP_EXTERN void WebPMuxDelete(WebPMux* mux);120121//------------------------------------------------------------------------------122// Mux creation.123124// Internal, version-checked, entry point125WEBP_EXTERN WebPMux* WebPMuxCreateInternal(const WebPData*, int, int);126127// Creates a mux object from raw data given in WebP RIFF format.128// Parameters:129// bitstream - (in) the bitstream data in WebP RIFF format130// copy_data - (in) value 1 indicates given data WILL be copied to the mux131// object and value 0 indicates data will NOT be copied.132// Returns:133// A pointer to the mux object created from given data - on success.134// NULL - In case of invalid data or memory error.135static WEBP_INLINE WebPMux* WebPMuxCreate(const WebPData* bitstream,136int copy_data) {137return WebPMuxCreateInternal(bitstream, copy_data, WEBP_MUX_ABI_VERSION);138}139140//------------------------------------------------------------------------------141// Non-image chunks.142143// Note: Only non-image related chunks should be managed through chunk APIs.144// (Image related chunks are: "ANMF", "VP8 ", "VP8L" and "ALPH").145// To add, get and delete images, use WebPMuxSetImage(), WebPMuxPushFrame(),146// WebPMuxGetFrame() and WebPMuxDeleteFrame().147148// Adds a chunk with id 'fourcc' and data 'chunk_data' in the mux object.149// Any existing chunk(s) with the same id will be removed.150// Parameters:151// mux - (in/out) object to which the chunk is to be added152// fourcc - (in) a character array containing the fourcc of the given chunk;153// e.g., "ICCP", "XMP ", "EXIF" etc.154// chunk_data - (in) the chunk data to be added155// copy_data - (in) value 1 indicates given data WILL be copied to the mux156// object and value 0 indicates data will NOT be copied.157// Returns:158// WEBP_MUX_INVALID_ARGUMENT - if mux, fourcc or chunk_data is NULL159// or if fourcc corresponds to an image chunk.160// WEBP_MUX_MEMORY_ERROR - on memory allocation error.161// WEBP_MUX_OK - on success.162WEBP_EXTERN WebPMuxError WebPMuxSetChunk(163WebPMux* mux, const char fourcc[4], const WebPData* chunk_data,164int copy_data);165166// Gets a reference to the data of the chunk with id 'fourcc' in the mux object.167// The caller should NOT free the returned data.168// Parameters:169// mux - (in) object from which the chunk data is to be fetched170// fourcc - (in) a character array containing the fourcc of the chunk;171// e.g., "ICCP", "XMP ", "EXIF" etc.172// chunk_data - (out) returned chunk data173// Returns:174// WEBP_MUX_INVALID_ARGUMENT - if mux, fourcc or chunk_data is NULL175// or if fourcc corresponds to an image chunk.176// WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given id.177// WEBP_MUX_OK - on success.178WEBP_EXTERN WebPMuxError WebPMuxGetChunk(179const WebPMux* mux, const char fourcc[4], WebPData* chunk_data);180181// Deletes the chunk with the given 'fourcc' from the mux object.182// Parameters:183// mux - (in/out) object from which the chunk is to be deleted184// fourcc - (in) a character array containing the fourcc of the chunk;185// e.g., "ICCP", "XMP ", "EXIF" etc.186// Returns:187// WEBP_MUX_INVALID_ARGUMENT - if mux or fourcc is NULL188// or if fourcc corresponds to an image chunk.189// WEBP_MUX_NOT_FOUND - If mux does not contain a chunk with the given fourcc.190// WEBP_MUX_OK - on success.191WEBP_EXTERN WebPMuxError WebPMuxDeleteChunk(192WebPMux* mux, const char fourcc[4]);193194//------------------------------------------------------------------------------195// Images.196197// Encapsulates data about a single frame.198struct WebPMuxFrameInfo {199WebPData bitstream; // image data: can be a raw VP8/VP8L bitstream200// or a single-image WebP file.201int x_offset; // x-offset of the frame.202int y_offset; // y-offset of the frame.203int duration; // duration of the frame (in milliseconds).204205WebPChunkId id; // frame type: should be one of WEBP_CHUNK_ANMF206// or WEBP_CHUNK_IMAGE207WebPMuxAnimDispose dispose_method; // Disposal method for the frame.208WebPMuxAnimBlend blend_method; // Blend operation for the frame.209uint32_t pad[1]; // padding for later use210};211212// Sets the (non-animated) image in the mux object.213// Note: Any existing images (including frames) will be removed.214// Parameters:215// mux - (in/out) object in which the image is to be set216// bitstream - (in) can be a raw VP8/VP8L bitstream or a single-image217// WebP file (non-animated)218// copy_data - (in) value 1 indicates given data WILL be copied to the mux219// object and value 0 indicates data will NOT be copied.220// Returns:221// WEBP_MUX_INVALID_ARGUMENT - if mux is NULL or bitstream is NULL.222// WEBP_MUX_MEMORY_ERROR - on memory allocation error.223// WEBP_MUX_OK - on success.224WEBP_EXTERN WebPMuxError WebPMuxSetImage(225WebPMux* mux, const WebPData* bitstream, int copy_data);226227// Adds a frame at the end of the mux object.228// Notes: (1) frame.id should be WEBP_CHUNK_ANMF229// (2) For setting a non-animated image, use WebPMuxSetImage() instead.230// (3) Type of frame being pushed must be same as the frames in mux.231// (4) As WebP only supports even offsets, any odd offset will be snapped232// to an even location using: offset &= ~1233// Parameters:234// mux - (in/out) object to which the frame is to be added235// frame - (in) frame data.236// copy_data - (in) value 1 indicates given data WILL be copied to the mux237// object and value 0 indicates data will NOT be copied.238// Returns:239// WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL240// or if content of 'frame' is invalid.241// WEBP_MUX_MEMORY_ERROR - on memory allocation error.242// WEBP_MUX_OK - on success.243WEBP_EXTERN WebPMuxError WebPMuxPushFrame(244WebPMux* mux, const WebPMuxFrameInfo* frame, int copy_data);245246// Gets the nth frame from the mux object.247// The content of 'frame->bitstream' is allocated using malloc(), and NOT248// owned by the 'mux' object. It MUST be deallocated by the caller by calling249// WebPDataClear().250// nth=0 has a special meaning - last position.251// Parameters:252// mux - (in) object from which the info is to be fetched253// nth - (in) index of the frame in the mux object254// frame - (out) data of the returned frame255// Returns:256// WEBP_MUX_INVALID_ARGUMENT - if mux or frame is NULL.257// WEBP_MUX_NOT_FOUND - if there are less than nth frames in the mux object.258// WEBP_MUX_BAD_DATA - if nth frame chunk in mux is invalid.259// WEBP_MUX_MEMORY_ERROR - on memory allocation error.260// WEBP_MUX_OK - on success.261WEBP_EXTERN WebPMuxError WebPMuxGetFrame(262const WebPMux* mux, uint32_t nth, WebPMuxFrameInfo* frame);263264// Deletes a frame from the mux object.265// nth=0 has a special meaning - last position.266// Parameters:267// mux - (in/out) object from which a frame is to be deleted268// nth - (in) The position from which the frame is to be deleted269// Returns:270// WEBP_MUX_INVALID_ARGUMENT - if mux is NULL.271// WEBP_MUX_NOT_FOUND - If there are less than nth frames in the mux object272// before deletion.273// WEBP_MUX_OK - on success.274WEBP_EXTERN WebPMuxError WebPMuxDeleteFrame(WebPMux* mux, uint32_t nth);275276//------------------------------------------------------------------------------277// Animation.278279// Animation parameters.280struct WebPMuxAnimParams {281uint32_t bgcolor; // Background color of the canvas stored (in MSB order) as:282// Bits 00 to 07: Alpha.283// Bits 08 to 15: Red.284// Bits 16 to 23: Green.285// Bits 24 to 31: Blue.286int loop_count; // Number of times to repeat the animation [0 = infinite].287};288289// Sets the animation parameters in the mux object. Any existing ANIM chunks290// will be removed.291// Parameters:292// mux - (in/out) object in which ANIM chunk is to be set/added293// params - (in) animation parameters.294// Returns:295// WEBP_MUX_INVALID_ARGUMENT - if mux or params is NULL.296// WEBP_MUX_MEMORY_ERROR - on memory allocation error.297// WEBP_MUX_OK - on success.298WEBP_EXTERN WebPMuxError WebPMuxSetAnimationParams(299WebPMux* mux, const WebPMuxAnimParams* params);300301// Gets the animation parameters from the mux object.302// Parameters:303// mux - (in) object from which the animation parameters to be fetched304// params - (out) animation parameters extracted from the ANIM chunk305// Returns:306// WEBP_MUX_INVALID_ARGUMENT - if mux or params is NULL.307// WEBP_MUX_NOT_FOUND - if ANIM chunk is not present in mux object.308// WEBP_MUX_OK - on success.309WEBP_EXTERN WebPMuxError WebPMuxGetAnimationParams(310const WebPMux* mux, WebPMuxAnimParams* params);311312//------------------------------------------------------------------------------313// Misc Utilities.314315// Sets the canvas size for the mux object. The width and height can be316// specified explicitly or left as zero (0, 0).317// * When width and height are specified explicitly, then this frame bound is318// enforced during subsequent calls to WebPMuxAssemble() and an error is319// reported if any animated frame does not completely fit within the canvas.320// * When unspecified (0, 0), the constructed canvas will get the frame bounds321// from the bounding-box over all frames after calling WebPMuxAssemble().322// Parameters:323// mux - (in) object to which the canvas size is to be set324// width - (in) canvas width325// height - (in) canvas height326// Returns:327// WEBP_MUX_INVALID_ARGUMENT - if mux is NULL; or328// width or height are invalid or out of bounds329// WEBP_MUX_OK - on success.330WEBP_EXTERN WebPMuxError WebPMuxSetCanvasSize(WebPMux* mux,331int width, int height);332333// Gets the canvas size from the mux object.334// Note: This method assumes that the VP8X chunk, if present, is up-to-date.335// That is, the mux object hasn't been modified since the last call to336// WebPMuxAssemble() or WebPMuxCreate().337// Parameters:338// mux - (in) object from which the canvas size is to be fetched339// width - (out) canvas width340// height - (out) canvas height341// Returns:342// WEBP_MUX_INVALID_ARGUMENT - if mux, width or height is NULL.343// WEBP_MUX_BAD_DATA - if VP8X/VP8/VP8L chunk or canvas size is invalid.344// WEBP_MUX_OK - on success.345WEBP_EXTERN WebPMuxError WebPMuxGetCanvasSize(const WebPMux* mux,346int* width, int* height);347348// Gets the feature flags from the mux object.349// Note: This method assumes that the VP8X chunk, if present, is up-to-date.350// That is, the mux object hasn't been modified since the last call to351// WebPMuxAssemble() or WebPMuxCreate().352// Parameters:353// mux - (in) object from which the features are to be fetched354// flags - (out) the flags specifying which features are present in the355// mux object. This will be an OR of various flag values.356// Enum 'WebPFeatureFlags' can be used to test individual flag values.357// Returns:358// WEBP_MUX_INVALID_ARGUMENT - if mux or flags is NULL.359// WEBP_MUX_BAD_DATA - if VP8X/VP8/VP8L chunk or canvas size is invalid.360// WEBP_MUX_OK - on success.361WEBP_EXTERN WebPMuxError WebPMuxGetFeatures(const WebPMux* mux,362uint32_t* flags);363364// Gets number of chunks with the given 'id' in the mux object.365// Parameters:366// mux - (in) object from which the info is to be fetched367// id - (in) chunk id specifying the type of chunk368// num_elements - (out) number of chunks with the given chunk id369// Returns:370// WEBP_MUX_INVALID_ARGUMENT - if mux, or num_elements is NULL.371// WEBP_MUX_OK - on success.372WEBP_EXTERN WebPMuxError WebPMuxNumChunks(const WebPMux* mux,373WebPChunkId id, int* num_elements);374375// Assembles all chunks in WebP RIFF format and returns in 'assembled_data'.376// This function also validates the mux object.377// Note: The content of 'assembled_data' will be ignored and overwritten.378// Also, the content of 'assembled_data' is allocated using malloc(), and NOT379// owned by the 'mux' object. It MUST be deallocated by the caller by calling380// WebPDataClear(). It's always safe to call WebPDataClear() upon return,381// even in case of error.382// Parameters:383// mux - (in/out) object whose chunks are to be assembled384// assembled_data - (out) assembled WebP data385// Returns:386// WEBP_MUX_BAD_DATA - if mux object is invalid.387// WEBP_MUX_INVALID_ARGUMENT - if mux or assembled_data is NULL.388// WEBP_MUX_MEMORY_ERROR - on memory allocation error.389// WEBP_MUX_OK - on success.390WEBP_EXTERN WebPMuxError WebPMuxAssemble(WebPMux* mux,391WebPData* assembled_data);392393//------------------------------------------------------------------------------394// WebPAnimEncoder API395//396// This API allows encoding (possibly) animated WebP images.397//398// Code Example:399/*400WebPAnimEncoderOptions enc_options;401WebPAnimEncoderOptionsInit(&enc_options);402// Tune 'enc_options' as needed.403WebPAnimEncoder* enc = WebPAnimEncoderNew(width, height, &enc_options);404while(<there are more frames>) {405WebPConfig config;406WebPConfigInit(&config);407// Tune 'config' as needed.408WebPAnimEncoderAdd(enc, frame, timestamp_ms, &config);409}410WebPAnimEncoderAdd(enc, NULL, timestamp_ms, NULL);411WebPAnimEncoderAssemble(enc, webp_data);412WebPAnimEncoderDelete(enc);413// Write the 'webp_data' to a file, or re-mux it further.414*/415416typedef struct WebPAnimEncoder WebPAnimEncoder; // Main opaque object.417418// Forward declarations. Defined in encode.h.419struct WebPPicture;420struct WebPConfig;421422// Global options.423struct WebPAnimEncoderOptions {424WebPMuxAnimParams anim_params; // Animation parameters.425int minimize_size; // If true, minimize the output size (slow). Implicitly426// disables key-frame insertion.427int kmin;428int kmax; // Minimum and maximum distance between consecutive key429// frames in the output. The library may insert some key430// frames as needed to satisfy this criteria.431// Note that these conditions should hold: kmax > kmin432// and kmin >= kmax / 2 + 1. Also, if kmax <= 0, then433// key-frame insertion is disabled; and if kmax == 1,434// then all frames will be key-frames (kmin value does435// not matter for these special cases).436int allow_mixed; // If true, use mixed compression mode; may choose437// either lossy and lossless for each frame.438int verbose; // If true, print info and warning messages to stderr.439440uint32_t padding[4]; // Padding for later use.441};442443// Internal, version-checked, entry point.444WEBP_EXTERN int WebPAnimEncoderOptionsInitInternal(445WebPAnimEncoderOptions*, int);446447// Should always be called, to initialize a fresh WebPAnimEncoderOptions448// structure before modification. Returns false in case of version mismatch.449// WebPAnimEncoderOptionsInit() must have succeeded before using the450// 'enc_options' object.451static WEBP_INLINE int WebPAnimEncoderOptionsInit(452WebPAnimEncoderOptions* enc_options) {453return WebPAnimEncoderOptionsInitInternal(enc_options, WEBP_MUX_ABI_VERSION);454}455456// Internal, version-checked, entry point.457WEBP_EXTERN WebPAnimEncoder* WebPAnimEncoderNewInternal(458int, int, const WebPAnimEncoderOptions*, int);459460// Creates and initializes a WebPAnimEncoder object.461// Parameters:462// width/height - (in) canvas width and height of the animation.463// enc_options - (in) encoding options; can be passed NULL to pick464// reasonable defaults.465// Returns:466// A pointer to the newly created WebPAnimEncoder object.467// Or NULL in case of memory error.468static WEBP_INLINE WebPAnimEncoder* WebPAnimEncoderNew(469int width, int height, const WebPAnimEncoderOptions* enc_options) {470return WebPAnimEncoderNewInternal(width, height, enc_options,471WEBP_MUX_ABI_VERSION);472}473474// Optimize the given frame for WebP, encode it and add it to the475// WebPAnimEncoder object.476// The last call to 'WebPAnimEncoderAdd' should be with frame = NULL, which477// indicates that no more frames are to be added. This call is also used to478// determine the duration of the last frame.479// Parameters:480// enc - (in/out) object to which the frame is to be added.481// frame - (in/out) frame data in ARGB or YUV(A) format. If it is in YUV(A)482// format, it will be converted to ARGB, which incurs a small loss.483// timestamp_ms - (in) timestamp of this frame in milliseconds.484// Duration of a frame would be calculated as485// "timestamp of next frame - timestamp of this frame".486// Hence, timestamps should be in non-decreasing order.487// config - (in) encoding options; can be passed NULL to pick488// reasonable defaults.489// Returns:490// On error, returns false and frame->error_code is set appropriately.491// Otherwise, returns true.492WEBP_EXTERN int WebPAnimEncoderAdd(493WebPAnimEncoder* enc, struct WebPPicture* frame, int timestamp_ms,494const struct WebPConfig* config);495496// Assemble all frames added so far into a WebP bitstream.497// This call should be preceded by a call to 'WebPAnimEncoderAdd' with498// frame = NULL; if not, the duration of the last frame will be internally499// estimated.500// Parameters:501// enc - (in/out) object from which the frames are to be assembled.502// webp_data - (out) generated WebP bitstream.503// Returns:504// True on success.505WEBP_EXTERN int WebPAnimEncoderAssemble(WebPAnimEncoder* enc,506WebPData* webp_data);507508// Get error string corresponding to the most recent call using 'enc'. The509// returned string is owned by 'enc' and is valid only until the next call to510// WebPAnimEncoderAdd() or WebPAnimEncoderAssemble() or WebPAnimEncoderDelete().511// Parameters:512// enc - (in/out) object from which the error string is to be fetched.513// Returns:514// NULL if 'enc' is NULL. Otherwise, returns the error string if the last call515// to 'enc' had an error, or an empty string if the last call was a success.516WEBP_EXTERN const char* WebPAnimEncoderGetError(WebPAnimEncoder* enc);517518// Deletes the WebPAnimEncoder object.519// Parameters:520// enc - (in/out) object to be deleted521WEBP_EXTERN void WebPAnimEncoderDelete(WebPAnimEncoder* enc);522523//------------------------------------------------------------------------------524525#ifdef __cplusplus526} // extern "C"527#endif528529#endif /* WEBP_WEBP_MUX_H_ */530531532