// 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// Internal header for mux library.10//11// Author: Urvang ([email protected])1213#ifndef WEBP_MUX_MUXI_H_14#define WEBP_MUX_MUXI_H_1516#include <assert.h>17#include <stdlib.h>1819#include "src/dec/vp8i_dec.h"20#include "src/dec/vp8li_dec.h"21#include "src/webp/format_constants.h"22#include "src/webp/mux.h"23#include "src/webp/mux_types.h"24#include "src/webp/types.h"2526#ifdef __cplusplus27extern "C" {28#endif2930//------------------------------------------------------------------------------31// Defines and constants.3233#define MUX_MAJ_VERSION 134#define MUX_MIN_VERSION 635#define MUX_REV_VERSION 03637// Chunk object.38typedef struct WebPChunk WebPChunk;39struct WebPChunk {40uint32_t tag;41int owner; // True if *data memory is owned internally.42// VP8X, ANIM, and other internally created chunks43// like ANMF are always owned.44WebPData data;45WebPChunk* next;46};4748// MuxImage object. Store a full WebP image (including ANMF chunk, ALPH49// chunk and VP8/VP8L chunk),50typedef struct WebPMuxImage WebPMuxImage;51struct WebPMuxImage {52WebPChunk* header; // Corresponds to WEBP_CHUNK_ANMF.53WebPChunk* alpha; // Corresponds to WEBP_CHUNK_ALPHA.54WebPChunk* img; // Corresponds to WEBP_CHUNK_IMAGE.55WebPChunk* unknown; // Corresponds to WEBP_CHUNK_UNKNOWN.56int width;57int height;58int has_alpha; // Through ALPH chunk or as part of VP8L.59int is_partial; // True if only some of the chunks are filled.60WebPMuxImage* next;61};6263// Main mux object. Stores data chunks.64struct WebPMux {65WebPMuxImage* images;66WebPChunk* iccp;67WebPChunk* exif;68WebPChunk* xmp;69WebPChunk* anim;70WebPChunk* vp8x;7172WebPChunk* unknown;73int canvas_width;74int canvas_height;75};7677// CHUNK_INDEX enum: used for indexing within 'kChunks' (defined below) only.78// Note: the reason for having two enums ('WebPChunkId' and 'CHUNK_INDEX') is to79// allow two different chunks to have the same id (e.g. WebPChunkId80// 'WEBP_CHUNK_IMAGE' can correspond to CHUNK_INDEX 'IDX_VP8' or 'IDX_VP8L').81typedef enum {82IDX_VP8X = 0,83IDX_ICCP,84IDX_ANIM,85IDX_ANMF,86IDX_ALPHA,87IDX_VP8,88IDX_VP8L,89IDX_EXIF,90IDX_XMP,91IDX_UNKNOWN,9293IDX_NIL,94IDX_LAST_CHUNK95} CHUNK_INDEX;9697#define NIL_TAG 0x00000000u // To signal void chunk.9899typedef struct {100uint32_t tag;101WebPChunkId id;102uint32_t size;103} ChunkInfo;104105extern const ChunkInfo kChunks[IDX_LAST_CHUNK];106107//------------------------------------------------------------------------------108// Chunk object management.109110// Initialize.111void ChunkInit(WebPChunk* const chunk);112113// Get chunk index from chunk tag. Returns IDX_UNKNOWN if not found.114CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag);115116// Get chunk id from chunk tag. Returns WEBP_CHUNK_UNKNOWN if not found.117WebPChunkId ChunkGetIdFromTag(uint32_t tag);118119// Convert a fourcc string to a tag.120uint32_t ChunkGetTagFromFourCC(const char fourcc[4]);121122// Get chunk index from fourcc. Returns IDX_UNKNOWN if given fourcc is unknown.123CHUNK_INDEX ChunkGetIndexFromFourCC(const char fourcc[4]);124125// Search for nth chunk with given 'tag' in the chunk list.126// nth = 0 means "last of the list".127WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag);128129// Fill the chunk with the given data.130WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,131int copy_data, uint32_t tag);132133// Sets 'chunk' as the only element in 'chunk_list' if it is empty.134// On success ownership is transferred from 'chunk' to the 'chunk_list'.135WebPMuxError ChunkSetHead(WebPChunk* const chunk, WebPChunk** const chunk_list);136// Sets 'chunk' at last position in the 'chunk_list'.137// On success ownership is transferred from 'chunk' to the 'chunk_list'.138// *chunk_list also points towards the last valid element of the initial139// *chunk_list.140WebPMuxError ChunkAppend(WebPChunk* const chunk, WebPChunk*** const chunk_list);141142// Releases chunk and returns chunk->next.143WebPChunk* ChunkRelease(WebPChunk* const chunk);144145// Deletes given chunk & returns chunk->next.146WebPChunk* ChunkDelete(WebPChunk* const chunk);147148// Deletes all chunks in the given chunk list.149void ChunkListDelete(WebPChunk** const chunk_list);150151// Returns size of the chunk including chunk header and padding byte (if any).152static WEBP_INLINE size_t SizeWithPadding(size_t chunk_size) {153assert(chunk_size <= MAX_CHUNK_PAYLOAD);154return CHUNK_HEADER_SIZE + ((chunk_size + 1) & ~1U);155}156157// Size of a chunk including header and padding.158static WEBP_INLINE size_t ChunkDiskSize(const WebPChunk* chunk) {159const size_t data_size = chunk->data.size;160return SizeWithPadding(data_size);161}162163// Total size of a list of chunks.164size_t ChunkListDiskSize(const WebPChunk* chunk_list);165166// Write out the given list of chunks into 'dst'.167uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst);168169//------------------------------------------------------------------------------170// MuxImage object management.171172// Initialize.173void MuxImageInit(WebPMuxImage* const wpi);174175// Releases image 'wpi' and returns wpi->next.176WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi);177178// Delete image 'wpi' and return the next image in the list or NULL.179// 'wpi' can be NULL.180WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi);181182// Count number of images matching the given tag id in the 'wpi_list'.183// If id == WEBP_CHUNK_NIL, all images will be matched.184int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id);185186// Update width/height/has_alpha info from chunks within wpi.187// Also remove ALPH chunk if not needed.188int MuxImageFinalize(WebPMuxImage* const wpi);189190// Check if given ID corresponds to an image related chunk.191static WEBP_INLINE int IsWPI(WebPChunkId id) {192switch (id) {193case WEBP_CHUNK_ANMF:194case WEBP_CHUNK_ALPHA:195case WEBP_CHUNK_IMAGE: return 1;196default: return 0;197}198}199200// Pushes 'wpi' at the end of 'wpi_list'.201WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list);202203// Delete nth image in the image list.204WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth);205206// Get nth image in the image list.207WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,208WebPMuxImage** wpi);209210// Total size of the given image.211size_t MuxImageDiskSize(const WebPMuxImage* const wpi);212213// Write out the given image into 'dst'.214uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst);215216//------------------------------------------------------------------------------217// Helper methods for mux.218219// Checks if the given image list contains at least one image with alpha.220int MuxHasAlpha(const WebPMuxImage* images);221222// Write out RIFF header into 'data', given total data size 'size'.223uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size);224225// Returns the list where chunk with given ID is to be inserted in mux.226WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id);227228// Validates the given mux object.229WebPMuxError MuxValidate(const WebPMux* const mux);230231//------------------------------------------------------------------------------232233#ifdef __cplusplus234} // extern "C"235#endif236237#endif // WEBP_MUX_MUXI_H_238239240