2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2012-01-05 07:50:37 +01:00
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2012-01-05 07:50:37 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Internal objects and utils for mux.
|
|
|
|
//
|
|
|
|
// Authors: Urvang (urvang@google.com)
|
|
|
|
// Vikas (vikasa@google.com)
|
|
|
|
|
|
|
|
#include <assert.h>
|
2017-10-07 23:15:11 +02:00
|
|
|
#include "src/mux/muxi.h"
|
|
|
|
#include "src/utils/utils.h"
|
2012-01-05 07:50:37 +01:00
|
|
|
|
2016-10-05 09:39:08 +02:00
|
|
|
#define UNDEFINED_CHUNK_SIZE ((uint32_t)(-1))
|
2012-01-05 13:21:27 +01:00
|
|
|
|
2012-01-05 07:50:37 +01:00
|
|
|
const ChunkInfo kChunks[] = {
|
2012-07-06 08:05:36 +02:00
|
|
|
{ MKFOURCC('V', 'P', '8', 'X'), WEBP_CHUNK_VP8X, VP8X_CHUNK_SIZE },
|
|
|
|
{ MKFOURCC('I', 'C', 'C', 'P'), WEBP_CHUNK_ICCP, UNDEFINED_CHUNK_SIZE },
|
2012-11-01 23:34:46 +01:00
|
|
|
{ MKFOURCC('A', 'N', 'I', 'M'), WEBP_CHUNK_ANIM, ANIM_CHUNK_SIZE },
|
2012-10-30 20:14:10 +01:00
|
|
|
{ MKFOURCC('A', 'N', 'M', 'F'), WEBP_CHUNK_ANMF, ANMF_CHUNK_SIZE },
|
2012-07-06 08:05:36 +02:00
|
|
|
{ MKFOURCC('A', 'L', 'P', 'H'), WEBP_CHUNK_ALPHA, UNDEFINED_CHUNK_SIZE },
|
|
|
|
{ MKFOURCC('V', 'P', '8', ' '), WEBP_CHUNK_IMAGE, UNDEFINED_CHUNK_SIZE },
|
|
|
|
{ MKFOURCC('V', 'P', '8', 'L'), WEBP_CHUNK_IMAGE, UNDEFINED_CHUNK_SIZE },
|
2012-11-01 00:30:41 +01:00
|
|
|
{ MKFOURCC('E', 'X', 'I', 'F'), WEBP_CHUNK_EXIF, UNDEFINED_CHUNK_SIZE },
|
|
|
|
{ MKFOURCC('X', 'M', 'P', ' '), WEBP_CHUNK_XMP, UNDEFINED_CHUNK_SIZE },
|
2013-07-18 01:34:50 +02:00
|
|
|
{ NIL_TAG, WEBP_CHUNK_UNKNOWN, UNDEFINED_CHUNK_SIZE },
|
2012-06-11 08:54:45 +02:00
|
|
|
|
2012-08-23 09:52:59 +02:00
|
|
|
{ NIL_TAG, WEBP_CHUNK_NIL, UNDEFINED_CHUNK_SIZE }
|
2012-01-05 07:50:37 +01:00
|
|
|
};
|
|
|
|
|
2013-02-26 23:22:06 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int WebPGetMuxVersion(void) {
|
|
|
|
return (MUX_MAJ_VERSION << 16) | (MUX_MIN_VERSION << 8) | MUX_REV_VERSION;
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:50:37 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Life of a chunk object.
|
|
|
|
|
|
|
|
void ChunkInit(WebPChunk* const chunk) {
|
|
|
|
assert(chunk);
|
2012-02-18 00:13:09 +01:00
|
|
|
memset(chunk, 0, sizeof(*chunk));
|
2012-01-05 07:50:37 +01:00
|
|
|
chunk->tag_ = NIL_TAG;
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:41:56 +01:00
|
|
|
WebPChunk* ChunkRelease(WebPChunk* const chunk) {
|
2012-01-05 07:50:37 +01:00
|
|
|
WebPChunk* next;
|
|
|
|
if (chunk == NULL) return NULL;
|
|
|
|
if (chunk->owner_) {
|
2012-06-20 19:59:40 +02:00
|
|
|
WebPDataClear(&chunk->data_);
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
next = chunk->next_;
|
|
|
|
ChunkInit(chunk);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Chunk misc methods.
|
|
|
|
|
2012-05-21 10:10:35 +02:00
|
|
|
CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag) {
|
2012-01-05 07:50:37 +01:00
|
|
|
int i;
|
2012-05-02 02:45:42 +02:00
|
|
|
for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
|
2013-11-26 05:53:15 +01:00
|
|
|
if (tag == kChunks[i].tag) return (CHUNK_INDEX)i;
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
2013-07-18 01:34:50 +02:00
|
|
|
return IDX_UNKNOWN;
|
2012-05-21 10:10:35 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 08:54:45 +02:00
|
|
|
WebPChunkId ChunkGetIdFromTag(uint32_t tag) {
|
2012-05-21 10:10:35 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
|
|
|
|
if (tag == kChunks[i].tag) return kChunks[i].id;
|
|
|
|
}
|
2013-07-18 01:34:50 +02:00
|
|
|
return WEBP_CHUNK_UNKNOWN;
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
|
2012-08-23 11:58:20 +02:00
|
|
|
uint32_t ChunkGetTagFromFourCC(const char fourcc[4]) {
|
|
|
|
return MKFOURCC(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHUNK_INDEX ChunkGetIndexFromFourCC(const char fourcc[4]) {
|
|
|
|
const uint32_t tag = ChunkGetTagFromFourCC(fourcc);
|
2013-07-18 01:34:50 +02:00
|
|
|
return ChunkGetIndexFromTag(tag);
|
2012-08-23 11:58:20 +02:00
|
|
|
}
|
|
|
|
|
2012-01-05 07:50:37 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Chunk search methods.
|
|
|
|
|
|
|
|
// Returns next chunk in the chunk list with the given tag.
|
|
|
|
static WebPChunk* ChunkSearchNextInList(WebPChunk* chunk, uint32_t tag) {
|
2013-03-26 20:40:24 +01:00
|
|
|
while (chunk != NULL && chunk->tag_ != tag) {
|
2012-01-05 07:50:37 +01:00
|
|
|
chunk = chunk->next_;
|
|
|
|
}
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag) {
|
|
|
|
uint32_t iter = nth;
|
|
|
|
first = ChunkSearchNextInList(first, tag);
|
2013-03-26 20:40:24 +01:00
|
|
|
if (first == NULL) return NULL;
|
2012-01-05 07:50:37 +01:00
|
|
|
|
|
|
|
while (--iter != 0) {
|
|
|
|
WebPChunk* next_chunk = ChunkSearchNextInList(first->next_, tag);
|
|
|
|
if (next_chunk == NULL) break;
|
|
|
|
first = next_chunk;
|
|
|
|
}
|
|
|
|
return ((nth > 0) && (iter > 0)) ? NULL : first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs a pointer to 'prev_chunk->next_',
|
|
|
|
// where 'prev_chunk' is the pointer to the chunk at position (nth - 1).
|
2012-08-23 12:58:36 +02:00
|
|
|
// Returns true if nth chunk was found.
|
2012-01-05 07:50:37 +01:00
|
|
|
static int ChunkSearchListToSet(WebPChunk** chunk_list, uint32_t nth,
|
|
|
|
WebPChunk*** const location) {
|
|
|
|
uint32_t count = 0;
|
2013-03-26 20:40:24 +01:00
|
|
|
assert(chunk_list != NULL);
|
2012-01-05 07:50:37 +01:00
|
|
|
*location = chunk_list;
|
|
|
|
|
2013-03-26 20:40:24 +01:00
|
|
|
while (*chunk_list != NULL) {
|
2012-01-05 07:50:37 +01:00
|
|
|
WebPChunk* const cur_chunk = *chunk_list;
|
|
|
|
++count;
|
|
|
|
if (count == nth) return 1; // Found.
|
|
|
|
chunk_list = &cur_chunk->next_;
|
|
|
|
*location = chunk_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// *chunk_list is ok to be NULL if adding at last location.
|
|
|
|
return (nth == 0 || (count == nth - 1)) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Chunk writer methods.
|
|
|
|
|
2012-07-04 15:19:08 +02:00
|
|
|
WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,
|
2012-07-07 19:37:26 +02:00
|
|
|
int copy_data, uint32_t tag) {
|
2012-01-05 07:50:37 +01:00
|
|
|
// For internally allocated chunks, always copy data & make it owner of data.
|
2012-11-01 23:34:46 +01:00
|
|
|
if (tag == kChunks[IDX_VP8X].tag || tag == kChunks[IDX_ANIM].tag) {
|
2012-01-05 07:50:37 +01:00
|
|
|
copy_data = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChunkRelease(chunk);
|
|
|
|
|
|
|
|
if (data != NULL) {
|
2013-03-26 20:40:24 +01:00
|
|
|
if (copy_data) { // Copy data.
|
|
|
|
if (!WebPDataCopy(data, &chunk->data_)) return WEBP_MUX_MEMORY_ERROR;
|
|
|
|
chunk->owner_ = 1; // Chunk is owner of data.
|
|
|
|
} else { // Don't copy data.
|
2012-06-20 19:59:40 +02:00
|
|
|
chunk->data_ = *data;
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
chunk->tag_ = tag;
|
|
|
|
return WEBP_MUX_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:18:13 +01:00
|
|
|
WebPMuxError ChunkSetNth(WebPChunk* chunk, WebPChunk** chunk_list,
|
2012-01-05 07:50:37 +01:00
|
|
|
uint32_t nth) {
|
|
|
|
WebPChunk* new_chunk;
|
|
|
|
|
|
|
|
if (!ChunkSearchListToSet(chunk_list, nth, &chunk_list)) {
|
|
|
|
return WEBP_MUX_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2014-03-27 23:27:32 +01:00
|
|
|
new_chunk = (WebPChunk*)WebPSafeMalloc(1ULL, sizeof(*new_chunk));
|
2012-01-05 07:50:37 +01:00
|
|
|
if (new_chunk == NULL) return WEBP_MUX_MEMORY_ERROR;
|
|
|
|
*new_chunk = *chunk;
|
2013-03-26 21:18:13 +01:00
|
|
|
chunk->owner_ = 0;
|
2012-01-05 07:50:37 +01:00
|
|
|
new_chunk->next_ = *chunk_list;
|
|
|
|
*chunk_list = new_chunk;
|
|
|
|
return WEBP_MUX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Chunk deletion method(s).
|
|
|
|
|
|
|
|
WebPChunk* ChunkDelete(WebPChunk* const chunk) {
|
|
|
|
WebPChunk* const next = ChunkRelease(chunk);
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(chunk);
|
2012-01-05 07:50:37 +01:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2013-07-23 01:40:24 +02:00
|
|
|
void ChunkListDelete(WebPChunk** const chunk_list) {
|
|
|
|
while (*chunk_list != NULL) {
|
|
|
|
*chunk_list = ChunkDelete(*chunk_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:50:37 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Chunk serialization methods.
|
|
|
|
|
|
|
|
static uint8_t* ChunkEmit(const WebPChunk* const chunk, uint8_t* dst) {
|
2012-10-30 22:54:46 +01:00
|
|
|
const size_t chunk_size = chunk->data_.size;
|
2012-01-05 07:50:37 +01:00
|
|
|
assert(chunk);
|
|
|
|
assert(chunk->tag_ != NIL_TAG);
|
|
|
|
PutLE32(dst + 0, chunk->tag_);
|
2012-06-20 19:59:40 +02:00
|
|
|
PutLE32(dst + TAG_SIZE, (uint32_t)chunk_size);
|
|
|
|
assert(chunk_size == (uint32_t)chunk_size);
|
2012-10-30 22:54:46 +01:00
|
|
|
memcpy(dst + CHUNK_HEADER_SIZE, chunk->data_.bytes, chunk_size);
|
2012-06-20 19:59:40 +02:00
|
|
|
if (chunk_size & 1)
|
|
|
|
dst[CHUNK_HEADER_SIZE + chunk_size] = 0; // Add padding.
|
2012-01-05 07:50:37 +01:00
|
|
|
return dst + ChunkDiskSize(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst) {
|
2013-03-26 20:40:24 +01:00
|
|
|
while (chunk_list != NULL) {
|
2012-01-05 07:50:37 +01:00
|
|
|
dst = ChunkEmit(chunk_list, dst);
|
|
|
|
chunk_list = chunk_list->next_;
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2013-07-23 01:40:24 +02:00
|
|
|
size_t ChunkListDiskSize(const WebPChunk* chunk_list) {
|
|
|
|
size_t size = 0;
|
|
|
|
while (chunk_list != NULL) {
|
|
|
|
size += ChunkDiskSize(chunk_list);
|
|
|
|
chunk_list = chunk_list->next_;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-06-07 07:34:57 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2012-01-05 07:50:37 +01:00
|
|
|
// Life of a MuxImage object.
|
|
|
|
|
|
|
|
void MuxImageInit(WebPMuxImage* const wpi) {
|
|
|
|
assert(wpi);
|
2012-01-28 02:39:47 +01:00
|
|
|
memset(wpi, 0, sizeof(*wpi));
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 06:41:56 +01:00
|
|
|
WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi) {
|
2012-01-05 07:50:37 +01:00
|
|
|
WebPMuxImage* next;
|
|
|
|
if (wpi == NULL) return NULL;
|
|
|
|
ChunkDelete(wpi->header_);
|
|
|
|
ChunkDelete(wpi->alpha_);
|
2012-05-21 10:10:35 +02:00
|
|
|
ChunkDelete(wpi->img_);
|
2013-07-23 01:40:24 +02:00
|
|
|
ChunkListDelete(&wpi->unknown_);
|
2012-01-05 07:50:37 +01:00
|
|
|
|
|
|
|
next = wpi->next_;
|
|
|
|
MuxImageInit(wpi);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// MuxImage search methods.
|
|
|
|
|
2013-05-13 22:26:33 +02:00
|
|
|
// Get a reference to appropriate chunk list within an image given chunk tag.
|
|
|
|
static WebPChunk** GetChunkListFromId(const WebPMuxImage* const wpi,
|
|
|
|
WebPChunkId id) {
|
|
|
|
assert(wpi != NULL);
|
|
|
|
switch (id) {
|
2016-09-02 14:26:37 +02:00
|
|
|
case WEBP_CHUNK_ANMF: return (WebPChunk**)&wpi->header_;
|
2013-05-13 22:26:33 +02:00
|
|
|
case WEBP_CHUNK_ALPHA: return (WebPChunk**)&wpi->alpha_;
|
|
|
|
case WEBP_CHUNK_IMAGE: return (WebPChunk**)&wpi->img_;
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 13:46:08 +02:00
|
|
|
int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id) {
|
2012-01-05 07:50:37 +01:00
|
|
|
int count = 0;
|
2012-06-29 13:46:08 +02:00
|
|
|
const WebPMuxImage* current;
|
2012-01-05 07:50:37 +01:00
|
|
|
for (current = wpi_list; current != NULL; current = current->next_) {
|
2012-08-23 12:58:36 +02:00
|
|
|
if (id == WEBP_CHUNK_NIL) {
|
|
|
|
++count; // Special case: count all images.
|
|
|
|
} else {
|
2013-05-13 22:26:33 +02:00
|
|
|
const WebPChunk* const wpi_chunk = *GetChunkListFromId(current, id);
|
2012-08-23 12:58:36 +02:00
|
|
|
if (wpi_chunk != NULL) {
|
|
|
|
const WebPChunkId wpi_chunk_id = ChunkGetIdFromTag(wpi_chunk->tag_);
|
|
|
|
if (wpi_chunk_id == id) ++count; // Count images with a matching 'id'.
|
|
|
|
}
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs a pointer to 'prev_wpi->next_',
|
|
|
|
// where 'prev_wpi' is the pointer to the image at position (nth - 1).
|
2012-08-23 12:58:36 +02:00
|
|
|
// Returns true if nth image was found.
|
2012-01-05 07:50:37 +01:00
|
|
|
static int SearchImageToGetOrDelete(WebPMuxImage** wpi_list, uint32_t nth,
|
2012-06-11 08:54:45 +02:00
|
|
|
WebPMuxImage*** const location) {
|
2012-01-05 07:50:37 +01:00
|
|
|
uint32_t count = 0;
|
|
|
|
assert(wpi_list);
|
|
|
|
*location = wpi_list;
|
|
|
|
|
|
|
|
if (nth == 0) {
|
2012-08-23 12:58:36 +02:00
|
|
|
nth = MuxImageCount(*wpi_list, WEBP_CHUNK_NIL);
|
2012-01-05 07:50:37 +01:00
|
|
|
if (nth == 0) return 0; // Not found.
|
|
|
|
}
|
|
|
|
|
2013-03-26 20:40:24 +01:00
|
|
|
while (*wpi_list != NULL) {
|
2012-01-05 07:50:37 +01:00
|
|
|
WebPMuxImage* const cur_wpi = *wpi_list;
|
2012-08-23 12:58:36 +02:00
|
|
|
++count;
|
|
|
|
if (count == nth) return 1; // Found.
|
2012-01-05 07:50:37 +01:00
|
|
|
wpi_list = &cur_wpi->next_;
|
|
|
|
*location = wpi_list;
|
|
|
|
}
|
|
|
|
return 0; // Not found.
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// MuxImage writer methods.
|
|
|
|
|
2012-06-22 20:17:02 +02:00
|
|
|
WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list) {
|
2012-01-05 07:50:37 +01:00
|
|
|
WebPMuxImage* new_wpi;
|
|
|
|
|
2012-06-22 20:17:02 +02:00
|
|
|
while (*wpi_list != NULL) {
|
|
|
|
WebPMuxImage* const cur_wpi = *wpi_list;
|
|
|
|
if (cur_wpi->next_ == NULL) break;
|
|
|
|
wpi_list = &cur_wpi->next_;
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
|
2014-03-27 23:27:32 +01:00
|
|
|
new_wpi = (WebPMuxImage*)WebPSafeMalloc(1ULL, sizeof(*new_wpi));
|
2012-01-05 07:50:37 +01:00
|
|
|
if (new_wpi == NULL) return WEBP_MUX_MEMORY_ERROR;
|
|
|
|
*new_wpi = *wpi;
|
2012-06-22 20:17:02 +02:00
|
|
|
new_wpi->next_ = NULL;
|
|
|
|
|
|
|
|
if (*wpi_list != NULL) {
|
|
|
|
(*wpi_list)->next_ = new_wpi;
|
|
|
|
} else {
|
2012-06-21 01:02:53 +02:00
|
|
|
*wpi_list = new_wpi;
|
2012-06-22 20:17:02 +02:00
|
|
|
}
|
2012-01-05 07:50:37 +01:00
|
|
|
return WEBP_MUX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// MuxImage deletion methods.
|
|
|
|
|
2012-01-13 10:16:30 +01:00
|
|
|
WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi) {
|
2012-01-14 05:16:52 +01:00
|
|
|
// Delete the components of wpi. If wpi is NULL this is a noop.
|
2012-01-05 07:50:37 +01:00
|
|
|
WebPMuxImage* const next = MuxImageRelease(wpi);
|
2014-03-27 23:27:32 +01:00
|
|
|
WebPSafeFree(wpi);
|
2012-01-05 07:50:37 +01:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2012-08-23 12:58:36 +02:00
|
|
|
WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth) {
|
2012-01-05 07:50:37 +01:00
|
|
|
assert(wpi_list);
|
2012-08-23 12:58:36 +02:00
|
|
|
if (!SearchImageToGetOrDelete(wpi_list, nth, &wpi_list)) {
|
2012-01-05 07:50:37 +01:00
|
|
|
return WEBP_MUX_NOT_FOUND;
|
|
|
|
}
|
2012-01-13 10:16:30 +01:00
|
|
|
*wpi_list = MuxImageDelete(*wpi_list);
|
2012-01-05 07:50:37 +01:00
|
|
|
return WEBP_MUX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// MuxImage reader methods.
|
|
|
|
|
|
|
|
WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
|
2012-08-23 12:58:36 +02:00
|
|
|
WebPMuxImage** wpi) {
|
2012-01-05 07:50:37 +01:00
|
|
|
assert(wpi_list);
|
|
|
|
assert(wpi);
|
2012-08-23 12:58:36 +02:00
|
|
|
if (!SearchImageToGetOrDelete((WebPMuxImage**)wpi_list, nth,
|
2012-01-28 02:39:47 +01:00
|
|
|
(WebPMuxImage***)&wpi_list)) {
|
2012-01-05 07:50:37 +01:00
|
|
|
return WEBP_MUX_NOT_FOUND;
|
|
|
|
}
|
|
|
|
*wpi = (WebPMuxImage*)*wpi_list;
|
|
|
|
return WEBP_MUX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// MuxImage serialization methods.
|
|
|
|
|
|
|
|
// Size of an image.
|
2012-06-20 19:59:40 +02:00
|
|
|
size_t MuxImageDiskSize(const WebPMuxImage* const wpi) {
|
2012-04-19 02:13:34 +02:00
|
|
|
size_t size = 0;
|
2012-01-05 07:50:37 +01:00
|
|
|
if (wpi->header_ != NULL) size += ChunkDiskSize(wpi->header_);
|
|
|
|
if (wpi->alpha_ != NULL) size += ChunkDiskSize(wpi->alpha_);
|
2012-05-21 10:10:35 +02:00
|
|
|
if (wpi->img_ != NULL) size += ChunkDiskSize(wpi->img_);
|
2013-07-23 01:40:24 +02:00
|
|
|
if (wpi->unknown_ != NULL) size += ChunkListDiskSize(wpi->unknown_);
|
2012-01-05 07:50:37 +01:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-09-02 14:26:37 +02:00
|
|
|
// Special case as ANMF chunk encapsulates other image chunks.
|
2012-11-01 01:19:31 +01:00
|
|
|
static uint8_t* ChunkEmitSpecial(const WebPChunk* const header,
|
|
|
|
size_t total_size, uint8_t* dst) {
|
|
|
|
const size_t header_size = header->data_.size;
|
|
|
|
const size_t offset_to_next = total_size - CHUNK_HEADER_SIZE;
|
2016-09-02 14:26:37 +02:00
|
|
|
assert(header->tag_ == kChunks[IDX_ANMF].tag);
|
2012-11-01 01:19:31 +01:00
|
|
|
PutLE32(dst + 0, header->tag_);
|
|
|
|
PutLE32(dst + TAG_SIZE, (uint32_t)offset_to_next);
|
|
|
|
assert(header_size == (uint32_t)header_size);
|
|
|
|
memcpy(dst + CHUNK_HEADER_SIZE, header->data_.bytes, header_size);
|
|
|
|
if (header_size & 1) {
|
|
|
|
dst[CHUNK_HEADER_SIZE + header_size] = 0; // Add padding.
|
|
|
|
}
|
|
|
|
return dst + ChunkDiskSize(header);
|
|
|
|
}
|
|
|
|
|
2012-06-20 19:59:40 +02:00
|
|
|
uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst) {
|
2012-01-05 07:50:37 +01:00
|
|
|
// Ordering of chunks to be emitted is strictly as follows:
|
2016-09-02 14:26:37 +02:00
|
|
|
// 1. ANMF chunk (if present).
|
2012-10-30 20:14:10 +01:00
|
|
|
// 2. ALPH chunk (if present).
|
2012-05-21 10:10:35 +02:00
|
|
|
// 3. VP8/VP8L chunk.
|
2012-01-05 07:50:37 +01:00
|
|
|
assert(wpi);
|
2012-11-01 01:19:31 +01:00
|
|
|
if (wpi->header_ != NULL) {
|
|
|
|
dst = ChunkEmitSpecial(wpi->header_, MuxImageDiskSize(wpi), dst);
|
|
|
|
}
|
2012-01-05 07:50:37 +01:00
|
|
|
if (wpi->alpha_ != NULL) dst = ChunkEmit(wpi->alpha_, dst);
|
2012-05-21 10:10:35 +02:00
|
|
|
if (wpi->img_ != NULL) dst = ChunkEmit(wpi->img_, dst);
|
2013-07-23 01:40:24 +02:00
|
|
|
if (wpi->unknown_ != NULL) dst = ChunkListEmit(wpi->unknown_, dst);
|
2012-01-05 07:50:37 +01:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2012-06-20 19:59:40 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Helper methods for mux.
|
|
|
|
|
2013-07-19 20:55:09 +02:00
|
|
|
int MuxHasAlpha(const WebPMuxImage* images) {
|
2012-05-23 11:31:25 +02:00
|
|
|
while (images != NULL) {
|
2013-07-19 20:55:09 +02:00
|
|
|
if (images->has_alpha_) return 1;
|
2012-05-23 11:31:25 +02:00
|
|
|
images = images->next_;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-20 19:59:40 +02:00
|
|
|
uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size) {
|
2012-07-06 08:05:36 +02:00
|
|
|
PutLE32(data + 0, MKFOURCC('R', 'I', 'F', 'F'));
|
2012-06-20 19:59:40 +02:00
|
|
|
PutLE32(data + TAG_SIZE, (uint32_t)size - CHUNK_HEADER_SIZE);
|
|
|
|
assert(size == (uint32_t)size);
|
2012-07-06 08:05:36 +02:00
|
|
|
PutLE32(data + TAG_SIZE + CHUNK_SIZE_BYTES, MKFOURCC('W', 'E', 'B', 'P'));
|
2012-06-20 19:59:40 +02:00
|
|
|
return data + RIFF_HEADER_SIZE;
|
|
|
|
}
|
2012-01-05 07:50:37 +01:00
|
|
|
|
2012-06-11 08:54:45 +02:00
|
|
|
WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id) {
|
2012-01-05 07:50:37 +01:00
|
|
|
assert(mux != NULL);
|
2012-11-01 23:34:46 +01:00
|
|
|
switch (id) {
|
2012-06-11 08:54:45 +02:00
|
|
|
case WEBP_CHUNK_VP8X: return (WebPChunk**)&mux->vp8x_;
|
|
|
|
case WEBP_CHUNK_ICCP: return (WebPChunk**)&mux->iccp_;
|
2012-11-01 23:34:46 +01:00
|
|
|
case WEBP_CHUNK_ANIM: return (WebPChunk**)&mux->anim_;
|
2012-11-01 00:30:41 +01:00
|
|
|
case WEBP_CHUNK_EXIF: return (WebPChunk**)&mux->exif_;
|
|
|
|
case WEBP_CHUNK_XMP: return (WebPChunk**)&mux->xmp_;
|
2013-07-18 01:34:50 +02:00
|
|
|
default: return (WebPChunk**)&mux->unknown_;
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int IsNotCompatible(int feature, int num_items) {
|
|
|
|
return (feature != 0) != (num_items > 0);
|
|
|
|
}
|
|
|
|
|
2016-10-05 09:39:08 +02:00
|
|
|
#define NO_FLAG ((WebPFeatureFlags)0)
|
2012-02-15 20:54:10 +01:00
|
|
|
|
|
|
|
// Test basic constraints:
|
2012-05-21 10:10:35 +02:00
|
|
|
// retrieval, maximum number of chunks by index (use -1 to skip)
|
2012-02-15 20:54:10 +01:00
|
|
|
// and feature incompatibility (use NO_FLAG to skip).
|
|
|
|
// On success returns WEBP_MUX_OK and stores the chunk count in *num.
|
2012-05-21 10:10:35 +02:00
|
|
|
static WebPMuxError ValidateChunk(const WebPMux* const mux, CHUNK_INDEX idx,
|
2012-05-23 11:31:25 +02:00
|
|
|
WebPFeatureFlags feature,
|
2013-11-26 05:53:15 +01:00
|
|
|
uint32_t vp8x_flags,
|
2012-02-15 20:54:10 +01:00
|
|
|
int max, int* num) {
|
|
|
|
const WebPMuxError err =
|
2012-06-11 08:54:45 +02:00
|
|
|
WebPMuxNumChunks(mux, kChunks[idx].id, num);
|
2012-02-15 20:54:10 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
|
|
|
if (max > -1 && *num > max) return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
if (feature != NO_FLAG && IsNotCompatible(vp8x_flags & feature, *num)) {
|
|
|
|
return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
return WEBP_MUX_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-23 11:31:25 +02:00
|
|
|
WebPMuxError MuxValidate(const WebPMux* const mux) {
|
2012-01-05 07:50:37 +01:00
|
|
|
int num_iccp;
|
2012-11-01 00:30:41 +01:00
|
|
|
int num_exif;
|
|
|
|
int num_xmp;
|
2012-11-01 23:34:46 +01:00
|
|
|
int num_anim;
|
2012-01-05 07:50:37 +01:00
|
|
|
int num_frames;
|
|
|
|
int num_vp8x;
|
|
|
|
int num_images;
|
|
|
|
int num_alpha;
|
|
|
|
uint32_t flags;
|
|
|
|
WebPMuxError err;
|
|
|
|
|
|
|
|
// Verify mux is not NULL.
|
2012-06-07 10:15:06 +02:00
|
|
|
if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
2012-01-05 07:50:37 +01:00
|
|
|
|
|
|
|
// Verify mux has at least one image.
|
|
|
|
if (mux->images_ == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
err = WebPMuxGetFeatures(mux, &flags);
|
|
|
|
if (err != WEBP_MUX_OK) return err;
|
|
|
|
|
|
|
|
// At most one color profile chunk.
|
2012-05-21 10:10:35 +02:00
|
|
|
err = ValidateChunk(mux, IDX_ICCP, ICCP_FLAG, flags, 1, &num_iccp);
|
2012-01-05 07:50:37 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
|
|
|
|
2012-11-01 00:30:41 +01:00
|
|
|
// At most one EXIF metadata.
|
|
|
|
err = ValidateChunk(mux, IDX_EXIF, EXIF_FLAG, flags, 1, &num_exif);
|
|
|
|
if (err != WEBP_MUX_OK) return err;
|
|
|
|
|
2012-01-05 07:50:37 +01:00
|
|
|
// At most one XMP metadata.
|
2012-11-01 00:30:41 +01:00
|
|
|
err = ValidateChunk(mux, IDX_XMP, XMP_FLAG, flags, 1, &num_xmp);
|
2012-01-05 07:50:37 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
|
|
|
|
2012-11-01 23:34:46 +01:00
|
|
|
// Animation: ANIMATION_FLAG, ANIM chunk and ANMF chunk(s) are consistent.
|
|
|
|
// At most one ANIM chunk.
|
|
|
|
err = ValidateChunk(mux, IDX_ANIM, NO_FLAG, flags, 1, &num_anim);
|
2012-01-05 07:50:37 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
2012-10-30 20:14:10 +01:00
|
|
|
err = ValidateChunk(mux, IDX_ANMF, NO_FLAG, flags, -1, &num_frames);
|
2012-01-05 07:50:37 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
2012-02-15 20:54:10 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
const int has_animation = !!(flags & ANIMATION_FLAG);
|
2012-11-01 23:34:46 +01:00
|
|
|
if (has_animation && (num_anim == 0 || num_frames == 0)) {
|
2012-02-15 20:54:10 +01:00
|
|
|
return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
}
|
2012-11-01 23:34:46 +01:00
|
|
|
if (!has_animation && (num_anim == 1 || num_frames > 0)) {
|
2012-02-15 20:54:10 +01:00
|
|
|
return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
}
|
2017-05-19 20:26:37 +02:00
|
|
|
if (!has_animation) {
|
|
|
|
const WebPMuxImage* images = mux->images_;
|
|
|
|
// There can be only one image.
|
|
|
|
if (images == NULL || images->next_ != NULL) {
|
|
|
|
return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
// Size must match.
|
|
|
|
if (mux->canvas_width_ > 0) {
|
|
|
|
if (images->width_ != mux->canvas_width_ ||
|
|
|
|
images->height_ != mux->canvas_height_) {
|
|
|
|
return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify either VP8X chunk is present OR there is only one elem in
|
|
|
|
// mux->images_.
|
2012-05-21 10:10:35 +02:00
|
|
|
err = ValidateChunk(mux, IDX_VP8X, NO_FLAG, flags, 1, &num_vp8x);
|
2012-01-05 07:50:37 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
2012-05-21 10:10:35 +02:00
|
|
|
err = ValidateChunk(mux, IDX_VP8, NO_FLAG, flags, -1, &num_images);
|
2012-01-05 07:50:37 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
2012-02-15 20:54:10 +01:00
|
|
|
if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT;
|
2012-01-05 07:50:37 +01:00
|
|
|
|
|
|
|
// ALPHA_FLAG & alpha chunk(s) are consistent.
|
2017-05-19 20:26:37 +02:00
|
|
|
// Note: ALPHA_FLAG can be set when there is actually no Alpha data present.
|
2013-07-19 20:55:09 +02:00
|
|
|
if (MuxHasAlpha(mux->images_)) {
|
2012-11-09 23:37:20 +01:00
|
|
|
if (num_vp8x > 0) {
|
2013-07-19 20:55:09 +02:00
|
|
|
// VP8X chunk is present, so it should contain ALPHA_FLAG.
|
2012-11-09 23:37:20 +01:00
|
|
|
if (!(flags & ALPHA_FLAG)) return WEBP_MUX_INVALID_ARGUMENT;
|
2013-07-19 20:55:09 +02:00
|
|
|
} else {
|
|
|
|
// VP8X chunk is not present, so ALPH chunks should NOT be present either.
|
|
|
|
err = WebPMuxNumChunks(mux, WEBP_CHUNK_ALPHA, &num_alpha);
|
2012-11-09 23:37:20 +01:00
|
|
|
if (err != WEBP_MUX_OK) return err;
|
2013-07-19 20:55:09 +02:00
|
|
|
if (num_alpha > 0) return WEBP_MUX_INVALID_ARGUMENT;
|
|
|
|
}
|
2012-01-05 07:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return WEBP_MUX_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-15 20:54:10 +01:00
|
|
|
#undef NO_FLAG
|
|
|
|
|
2012-01-05 07:50:37 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|