2012-01-06 14:49:06 -08:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2012-01-05 12:20:37 +05:30
|
|
|
//
|
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Internal header for mux library.
|
|
|
|
//
|
|
|
|
// Author: Urvang (urvang@google.com)
|
|
|
|
|
|
|
|
#ifndef WEBP_MUX_MUXI_H_
|
|
|
|
#define WEBP_MUX_MUXI_H_
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "../dec/vp8i.h"
|
2012-05-21 13:40:35 +05:30
|
|
|
#include "../dec/vp8li.h"
|
2012-05-24 14:51:37 +05:30
|
|
|
#include "../webp/format_constants.h"
|
2012-01-05 12:20:37 +05:30
|
|
|
#include "../webp/mux.h"
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Defines and constants.
|
|
|
|
|
|
|
|
// Object to store metadata about images.
|
|
|
|
typedef struct {
|
|
|
|
uint32_t x_offset_;
|
|
|
|
uint32_t y_offset_;
|
|
|
|
uint32_t duration_;
|
|
|
|
uint32_t width_;
|
|
|
|
uint32_t height_;
|
|
|
|
} WebPImageInfo;
|
|
|
|
|
|
|
|
// Chunk object.
|
|
|
|
typedef struct WebPChunk WebPChunk;
|
|
|
|
struct WebPChunk {
|
|
|
|
uint32_t tag_;
|
2012-04-18 17:13:34 -07:00
|
|
|
size_t payload_size_;
|
2012-01-05 12:20:37 +05:30
|
|
|
WebPImageInfo* image_info_;
|
|
|
|
int owner_; // True if *data_ memory is owned internally.
|
|
|
|
// VP8X, Loop, and other internally created chunks
|
|
|
|
// like frame/tile are always owned.
|
|
|
|
const uint8_t* data_;
|
|
|
|
WebPChunk* next_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// MuxImage object. Store a full webp image (including frame/tile chunk, alpha
|
2012-05-21 13:40:35 +05:30
|
|
|
// chunk and VP8/VP8L chunk),
|
2012-01-05 12:20:37 +05:30
|
|
|
typedef struct WebPMuxImage WebPMuxImage;
|
|
|
|
struct WebPMuxImage {
|
|
|
|
WebPChunk* header_; // Corresponds to FRAME_ID/TILE_ID.
|
|
|
|
WebPChunk* alpha_; // Corresponds to ALPHA_ID.
|
2012-05-21 13:40:35 +05:30
|
|
|
WebPChunk* img_; // Corresponds to IMAGE_ID.
|
2012-01-05 12:20:37 +05:30
|
|
|
int is_partial_; // True if only some of the chunks are filled.
|
|
|
|
WebPMuxImage* next_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Main mux object. Stores data chunks.
|
|
|
|
struct WebPMux {
|
2012-01-13 14:46:30 +05:30
|
|
|
WebPMuxState state_;
|
2012-01-05 12:20:37 +05:30
|
|
|
WebPMuxImage* images_;
|
|
|
|
WebPChunk* iccp_;
|
|
|
|
WebPChunk* meta_;
|
|
|
|
WebPChunk* loop_;
|
|
|
|
WebPChunk* vp8x_;
|
|
|
|
|
|
|
|
WebPChunk* unknown_;
|
|
|
|
};
|
|
|
|
|
2012-05-21 13:40:35 +05:30
|
|
|
// TAG_ID enum: used to assign an ID to each type of chunk.
|
2012-01-05 12:20:37 +05:30
|
|
|
typedef enum {
|
2012-05-21 13:40:35 +05:30
|
|
|
VP8X_ID,
|
2012-01-05 12:20:37 +05:30
|
|
|
ICCP_ID,
|
|
|
|
LOOP_ID,
|
|
|
|
FRAME_ID,
|
|
|
|
TILE_ID,
|
|
|
|
ALPHA_ID,
|
|
|
|
IMAGE_ID,
|
|
|
|
META_ID,
|
|
|
|
UNKNOWN_ID,
|
2012-05-21 13:40:35 +05:30
|
|
|
NIL_ID
|
2012-01-05 12:20:37 +05:30
|
|
|
} TAG_ID;
|
|
|
|
|
2012-05-21 13:40:35 +05:30
|
|
|
// CHUNK_INDEX enum: used for indexing within 'kChunks' (defined below) only.
|
|
|
|
// Note: the reason for having two enums ('TAG_ID' and 'CHUNK_INDEX') is to
|
|
|
|
// allow two different chunks to have the same id (e.g. TAG_ID 'IMAGE_ID' can
|
|
|
|
// correspond to CHUNK_INDEX 'IDX_VP8' or 'IDX_VP8L').
|
|
|
|
typedef enum {
|
|
|
|
IDX_VP8X = 0,
|
|
|
|
IDX_ICCP,
|
|
|
|
IDX_LOOP,
|
|
|
|
IDX_FRAME,
|
|
|
|
IDX_TILE,
|
|
|
|
IDX_ALPHA,
|
|
|
|
IDX_VP8,
|
|
|
|
IDX_VP8L,
|
|
|
|
IDX_META,
|
|
|
|
IDX_UNKNOWN,
|
|
|
|
|
|
|
|
IDX_NIL,
|
|
|
|
IDX_LAST_CHUNK
|
|
|
|
} CHUNK_INDEX;
|
|
|
|
|
2012-01-05 12:20:37 +05:30
|
|
|
#define NIL_TAG 0x00000000u // To signal void chunk.
|
|
|
|
|
|
|
|
#define mktag(c1, c2, c3, c4) \
|
|
|
|
((uint32_t)c1 | (c2 << 8) | (c3 << 16) | (c4 << 24))
|
|
|
|
|
|
|
|
typedef struct {
|
2012-05-01 17:45:42 -07:00
|
|
|
const char* name;
|
|
|
|
uint32_t tag;
|
|
|
|
TAG_ID id;
|
|
|
|
uint32_t size;
|
2012-01-05 12:20:37 +05:30
|
|
|
} ChunkInfo;
|
|
|
|
|
2012-05-21 13:40:35 +05:30
|
|
|
extern const ChunkInfo kChunks[IDX_LAST_CHUNK];
|
2012-01-05 12:20:37 +05:30
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Helper functions.
|
|
|
|
|
|
|
|
static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
|
|
|
|
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE void PutLE16(uint8_t* const data, uint16_t val) {
|
|
|
|
data[0] = (val >> 0) & 0xff;
|
|
|
|
data[1] = (val >> 8) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
|
|
|
|
PutLE16(data, val);
|
|
|
|
PutLE16(data + 2, val >> 16);
|
|
|
|
}
|
|
|
|
|
2012-04-18 17:13:34 -07:00
|
|
|
static WEBP_INLINE size_t SizeWithPadding(size_t chunk_size) {
|
2012-01-13 14:46:30 +05:30
|
|
|
return CHUNK_HEADER_SIZE + ((chunk_size + 1) & ~1U);
|
|
|
|
}
|
|
|
|
|
2012-01-05 12:20:37 +05:30
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Chunk object management.
|
|
|
|
|
|
|
|
// Initialize.
|
|
|
|
void ChunkInit(WebPChunk* const chunk);
|
|
|
|
|
2012-05-21 13:40:35 +05:30
|
|
|
// Get chunk index from chunk name.
|
|
|
|
// Returns IDX_NIL if chunk name is NULL or not found.
|
|
|
|
CHUNK_INDEX ChunkGetIndexFromName(const char* const name);
|
|
|
|
|
|
|
|
// Get chunk index from chunk tag.
|
|
|
|
// Returns IDX_NIL if not found.
|
|
|
|
CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag);
|
2012-01-05 12:20:37 +05:30
|
|
|
|
|
|
|
// Get chunk id from chunk tag.
|
2012-05-21 13:40:35 +05:30
|
|
|
// Returns NIL_ID if not found.
|
2012-01-05 12:20:37 +05:30
|
|
|
TAG_ID ChunkGetIdFromTag(uint32_t tag);
|
|
|
|
|
|
|
|
// Search for nth chunk with given 'tag' in the chunk list.
|
|
|
|
// nth = 0 means "last of the list".
|
|
|
|
WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag);
|
|
|
|
|
|
|
|
// Fill the chunk with the given data & image_info.
|
2012-01-27 17:39:47 -08:00
|
|
|
WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk,
|
2012-04-18 17:13:34 -07:00
|
|
|
const uint8_t* data, size_t data_size,
|
2012-01-05 12:20:37 +05:30
|
|
|
WebPImageInfo* image_info,
|
|
|
|
int copy_data, uint32_t tag);
|
|
|
|
|
|
|
|
// Sets 'chunk' at nth position in the 'chunk_list'.
|
|
|
|
// nth = 0 has the special meaning "last of the list".
|
|
|
|
WebPMuxError ChunkSetNth(const WebPChunk* chunk, WebPChunk** chunk_list,
|
|
|
|
uint32_t nth);
|
|
|
|
|
2012-01-17 05:41:56 +00:00
|
|
|
// Releases chunk and returns chunk->next_.
|
|
|
|
WebPChunk* ChunkRelease(WebPChunk* const chunk);
|
|
|
|
|
2012-01-05 12:20:37 +05:30
|
|
|
// Deletes given chunk & returns chunk->next_.
|
|
|
|
WebPChunk* ChunkDelete(WebPChunk* const chunk);
|
|
|
|
|
|
|
|
// Size of a chunk including header and padding.
|
2012-04-18 17:13:34 -07:00
|
|
|
static WEBP_INLINE size_t ChunkDiskSize(const WebPChunk* chunk) {
|
2012-01-05 12:20:37 +05:30
|
|
|
assert(chunk->payload_size_ < MAX_CHUNK_PAYLOAD);
|
2012-01-13 14:46:30 +05:30
|
|
|
return SizeWithPadding(chunk->payload_size_);
|
2012-01-05 12:20:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Total size of a list of chunks.
|
2012-04-18 17:13:34 -07:00
|
|
|
size_t ChunksListDiskSize(const WebPChunk* chunk_list);
|
2012-01-05 12:20:37 +05:30
|
|
|
|
|
|
|
// Write out the given list of chunks into 'dst'.
|
|
|
|
uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst);
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// MuxImage object management.
|
|
|
|
|
|
|
|
// Initialize.
|
|
|
|
void MuxImageInit(WebPMuxImage* const wpi);
|
|
|
|
|
2012-01-17 05:41:56 +00:00
|
|
|
// Releases image 'wpi' and returns wpi->next.
|
|
|
|
WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi);
|
|
|
|
|
2012-01-13 20:16:52 -08:00
|
|
|
// Delete image 'wpi' and return the next image in the list or NULL.
|
|
|
|
// 'wpi' can be NULL.
|
2012-01-13 14:46:30 +05:30
|
|
|
WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi);
|
|
|
|
|
2012-01-05 12:20:37 +05:30
|
|
|
// Delete all images in 'wpi_list'.
|
|
|
|
void MuxImageDeleteAll(WebPMuxImage** const wpi_list);
|
|
|
|
|
2012-05-21 13:40:35 +05:30
|
|
|
// Count number of images matching the given tag id in the 'wpi_list'.
|
2012-01-05 12:20:37 +05:30
|
|
|
int MuxImageCount(WebPMuxImage* const wpi_list, TAG_ID id);
|
|
|
|
|
|
|
|
// Check if given ID corresponds to an image related chunk.
|
|
|
|
static WEBP_INLINE int IsWPI(TAG_ID id) {
|
|
|
|
switch (id) {
|
|
|
|
case FRAME_ID:
|
|
|
|
case TILE_ID:
|
|
|
|
case ALPHA_ID:
|
|
|
|
case IMAGE_ID: return 1;
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a reference to appropriate chunk list within an image given chunk tag.
|
|
|
|
static WEBP_INLINE WebPChunk** MuxImageGetListFromId(WebPMuxImage* wpi,
|
|
|
|
TAG_ID id) {
|
|
|
|
assert(wpi != NULL);
|
|
|
|
switch (id) {
|
|
|
|
case FRAME_ID:
|
2012-05-21 13:40:35 +05:30
|
|
|
case TILE_ID: return &wpi->header_;
|
2012-01-05 12:20:37 +05:30
|
|
|
case ALPHA_ID: return &wpi->alpha_;
|
2012-05-21 13:40:35 +05:30
|
|
|
case IMAGE_ID: return &wpi->img_;
|
2012-01-05 12:20:37 +05:30
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets 'wpi' at nth position in the 'wpi_list'.
|
|
|
|
// nth = 0 has the special meaning "last of the list".
|
|
|
|
WebPMuxError MuxImageSetNth(const WebPMuxImage* wpi, WebPMuxImage** wpi_list,
|
|
|
|
uint32_t nth);
|
|
|
|
|
2012-05-21 13:40:35 +05:30
|
|
|
// Delete nth image in the image list with given tag id.
|
2012-01-05 12:20:37 +05:30
|
|
|
WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth,
|
|
|
|
TAG_ID id);
|
|
|
|
|
2012-05-21 13:40:35 +05:30
|
|
|
// Get nth image in the image list with given tag id.
|
2012-01-05 12:20:37 +05:30
|
|
|
WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
|
|
|
|
TAG_ID id, WebPMuxImage** wpi);
|
|
|
|
|
|
|
|
// Total size of a list of images.
|
2012-04-18 17:13:34 -07:00
|
|
|
size_t MuxImageListDiskSize(const WebPMuxImage* wpi_list);
|
2012-01-05 12:20:37 +05:30
|
|
|
|
|
|
|
// Write out the given list of images into 'dst'.
|
|
|
|
uint8_t* MuxImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst);
|
|
|
|
|
2012-05-23 15:01:25 +05:30
|
|
|
// Checks if the given image list contains at least one lossless image.
|
|
|
|
int MuxHasLosslessImages(const WebPMuxImage* images);
|
|
|
|
|
2012-01-05 12:20:37 +05:30
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Helper methods for mux.
|
|
|
|
|
|
|
|
// Returns the list where chunk with given ID is to be inserted in mux.
|
|
|
|
// Return value is NULL if this chunk should be inserted in mux->images_ list
|
|
|
|
// or if 'id' is not known.
|
2012-05-23 15:01:25 +05:30
|
|
|
WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, TAG_ID id);
|
2012-01-05 12:20:37 +05:30
|
|
|
|
|
|
|
// Validates that the given mux has a single image.
|
2012-05-23 15:01:25 +05:30
|
|
|
WebPMuxError MuxValidateForImage(const WebPMux* const mux);
|
2012-01-05 12:20:37 +05:30
|
|
|
|
|
|
|
// Validates the given mux object.
|
2012-05-23 15:01:25 +05:30
|
|
|
WebPMuxError MuxValidate(const WebPMux* const mux);
|
2012-01-05 12:20:37 +05:30
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2012-02-14 13:51:41 -08:00
|
|
|
#endif /* WEBP_MUX_MUXI_H_ */
|