Initial release
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
AM_CPPFLAGS += -I$(top_builddir) -I$(top_srcdir)
|
||||
lib_LTLIBRARIES = libwebpmux.la
|
||||
|
||||
libwebpmux_la_SOURCES =
|
||||
libwebpmux_la_SOURCES += anim_encode.c
|
||||
libwebpmux_la_SOURCES += animi.h
|
||||
libwebpmux_la_SOURCES += muxedit.c
|
||||
libwebpmux_la_SOURCES += muxi.h
|
||||
libwebpmux_la_SOURCES += muxinternal.c
|
||||
libwebpmux_la_SOURCES += muxread.c
|
||||
|
||||
libwebpmuxinclude_HEADERS =
|
||||
libwebpmuxinclude_HEADERS += ../webp/mux.h
|
||||
libwebpmuxinclude_HEADERS += ../webp/mux_types.h
|
||||
libwebpmuxinclude_HEADERS += ../webp/types.h
|
||||
noinst_HEADERS =
|
||||
noinst_HEADERS += ../webp/format_constants.h
|
||||
|
||||
libwebpmux_la_LIBADD = ../libwebp.la
|
||||
libwebpmux_la_LDFLAGS = -no-undefined -version-info 4:2:1 -lm
|
||||
libwebpmuxincludedir = $(includedir)/webp
|
||||
pkgconfig_DATA = libwebpmux.pc
|
||||
+1837
File diff suppressed because it is too large
Load Diff
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Internal header for animation related functions.
|
||||
//
|
||||
// Author: Hui Su (huisu@google.com)
|
||||
|
||||
#ifndef WEBP_MUX_ANIMI_H_
|
||||
#define WEBP_MUX_ANIMI_H_
|
||||
|
||||
#include "src/webp/mux.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Picks the optimal rectangle between two pictures, starting with initial
|
||||
// values of offsets and dimensions that are passed in. The initial
|
||||
// values will be clipped, if necessary, to make sure the rectangle is
|
||||
// within the canvas. "use_argb" must be true for both pictures.
|
||||
// Parameters:
|
||||
// prev_canvas, curr_canvas - (in) two input pictures to compare.
|
||||
// is_lossless, quality - (in) encoding settings.
|
||||
// x_offset, y_offset, width, height - (in/out) rectangle between the two
|
||||
// input pictures.
|
||||
// Returns true on success.
|
||||
int WebPAnimEncoderRefineRect(const struct WebPPicture* const prev_canvas,
|
||||
const struct WebPPicture* const curr_canvas,
|
||||
int is_lossless, float quality,
|
||||
int* const x_offset, int* const y_offset,
|
||||
int* const width, int* const height);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // WEBP_MUX_ANIMI_H_
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
prefix=@prefix@
|
||||
exec_prefix=@exec_prefix@
|
||||
libdir=@libdir@
|
||||
includedir=@includedir@
|
||||
|
||||
Name: libwebpmux
|
||||
Description: Library for manipulating the WebP graphics format container
|
||||
Version: @PACKAGE_VERSION@
|
||||
Requires.private: libwebp >= 0.2.0
|
||||
Cflags: -I${includedir}
|
||||
Libs: -L${libdir} -l@webp_libname_prefix@webpmux
|
||||
Libs.private: -lm
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
#define APSTUDIO_READONLY_SYMBOLS
|
||||
#include "winres.h"
|
||||
#undef APSTUDIO_READONLY_SYMBOLS
|
||||
|
||||
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
|
||||
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
|
||||
|
||||
VS_VERSION_INFO VERSIONINFO
|
||||
FILEVERSION 1,0,6,0
|
||||
PRODUCTVERSION 1,0,6,0
|
||||
FILEFLAGSMASK 0x3fL
|
||||
#ifdef _DEBUG
|
||||
FILEFLAGS 0x1L
|
||||
#else
|
||||
FILEFLAGS 0x0L
|
||||
#endif
|
||||
FILEOS 0x40004L
|
||||
FILETYPE 0x2L
|
||||
FILESUBTYPE 0x0L
|
||||
BEGIN
|
||||
BLOCK "StringFileInfo"
|
||||
BEGIN
|
||||
BLOCK "040904b0"
|
||||
BEGIN
|
||||
VALUE "CompanyName", "Google, Inc."
|
||||
VALUE "FileDescription", "libwebpmux DLL"
|
||||
VALUE "FileVersion", "1.6.0"
|
||||
VALUE "InternalName", "libwebpmux.dll"
|
||||
VALUE "LegalCopyright", "Copyright (C) 2025"
|
||||
VALUE "OriginalFilename", "libwebpmux.dll"
|
||||
VALUE "ProductName", "WebP Image Muxer"
|
||||
VALUE "ProductVersion", "1.6.0"
|
||||
END
|
||||
END
|
||||
BLOCK "VarFileInfo"
|
||||
BEGIN
|
||||
VALUE "Translation", 0x409, 1200
|
||||
END
|
||||
END
|
||||
|
||||
#endif // English (United States) resources
|
||||
Vendored
+669
@@ -0,0 +1,669 @@
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Set and delete APIs for mux.
|
||||
//
|
||||
// Authors: Urvang (urvang@google.com)
|
||||
// Vikas (vikasa@google.com)
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "src/dec/vp8_dec.h"
|
||||
#include "src/mux/muxi.h"
|
||||
#include "src/utils/utils.h"
|
||||
#include "src/webp/format_constants.h"
|
||||
#include "src/webp/mux.h"
|
||||
#include "src/webp/mux_types.h"
|
||||
#include "src/webp/types.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Life of a mux object.
|
||||
|
||||
static void MuxInit(WebPMux* const mux) {
|
||||
assert(mux != NULL);
|
||||
memset(mux, 0, sizeof(*mux));
|
||||
mux->canvas_width = 0; // just to be explicit
|
||||
mux->canvas_height = 0;
|
||||
}
|
||||
|
||||
WebPMux* WebPNewInternal(int version) {
|
||||
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_MUX_ABI_VERSION)) {
|
||||
return NULL;
|
||||
} else {
|
||||
WebPMux* const mux = (WebPMux*)WebPSafeMalloc(1ULL, sizeof(WebPMux));
|
||||
if (mux != NULL) MuxInit(mux);
|
||||
return mux;
|
||||
}
|
||||
}
|
||||
|
||||
// Delete all images in 'wpi_list'.
|
||||
static void DeleteAllImages(WebPMuxImage** const wpi_list) {
|
||||
while (*wpi_list != NULL) {
|
||||
*wpi_list = MuxImageDelete(*wpi_list);
|
||||
}
|
||||
}
|
||||
|
||||
static void MuxRelease(WebPMux* const mux) {
|
||||
assert(mux != NULL);
|
||||
DeleteAllImages(&mux->images);
|
||||
ChunkListDelete(&mux->vp8x);
|
||||
ChunkListDelete(&mux->iccp);
|
||||
ChunkListDelete(&mux->anim);
|
||||
ChunkListDelete(&mux->exif);
|
||||
ChunkListDelete(&mux->xmp);
|
||||
ChunkListDelete(&mux->unknown);
|
||||
}
|
||||
|
||||
void WebPMuxDelete(WebPMux* mux) {
|
||||
if (mux != NULL) {
|
||||
MuxRelease(mux);
|
||||
WebPSafeFree(mux);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Helper method(s).
|
||||
|
||||
// Handy MACRO, makes MuxSet() very symmetric to MuxGet().
|
||||
#define SWITCH_ID_LIST(INDEX, LIST) \
|
||||
do { \
|
||||
if (idx == (INDEX)) { \
|
||||
err = ChunkAssignData(&chunk, data, copy_data, tag); \
|
||||
if (err == WEBP_MUX_OK) { \
|
||||
err = ChunkSetHead(&chunk, (LIST)); \
|
||||
if (err != WEBP_MUX_OK) ChunkRelease(&chunk); \
|
||||
} \
|
||||
return err; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static WebPMuxError MuxSet(WebPMux* const mux, uint32_t tag,
|
||||
const WebPData* const data, int copy_data) {
|
||||
WebPChunk chunk;
|
||||
WebPMuxError err = WEBP_MUX_NOT_FOUND;
|
||||
const CHUNK_INDEX idx = ChunkGetIndexFromTag(tag);
|
||||
assert(mux != NULL);
|
||||
if (IsWPI(kChunks[idx].id)) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
|
||||
ChunkInit(&chunk);
|
||||
SWITCH_ID_LIST(IDX_VP8X, &mux->vp8x);
|
||||
SWITCH_ID_LIST(IDX_ICCP, &mux->iccp);
|
||||
SWITCH_ID_LIST(IDX_ANIM, &mux->anim);
|
||||
SWITCH_ID_LIST(IDX_EXIF, &mux->exif);
|
||||
SWITCH_ID_LIST(IDX_XMP, &mux->xmp);
|
||||
SWITCH_ID_LIST(IDX_UNKNOWN, &mux->unknown);
|
||||
return err;
|
||||
}
|
||||
#undef SWITCH_ID_LIST
|
||||
|
||||
// Create data for frame given image data, offsets and duration.
|
||||
static WebPMuxError CreateFrameData(int width, int height,
|
||||
const WebPMuxFrameInfo* const info,
|
||||
WebPData* const frame) {
|
||||
uint8_t* frame_bytes;
|
||||
const size_t frame_size = kChunks[IDX_ANMF].size;
|
||||
|
||||
assert(width > 0 && height > 0 && info->duration >= 0);
|
||||
assert(info->dispose_method == (info->dispose_method & 1));
|
||||
// Note: assertion on upper bounds is done in PutLE24().
|
||||
|
||||
frame_bytes = (uint8_t*)WebPSafeMalloc(1ULL, frame_size);
|
||||
if (frame_bytes == NULL) return WEBP_MUX_MEMORY_ERROR;
|
||||
|
||||
PutLE24(frame_bytes + 0, info->x_offset / 2);
|
||||
PutLE24(frame_bytes + 3, info->y_offset / 2);
|
||||
|
||||
PutLE24(frame_bytes + 6, width - 1);
|
||||
PutLE24(frame_bytes + 9, height - 1);
|
||||
PutLE24(frame_bytes + 12, info->duration);
|
||||
frame_bytes[15] =
|
||||
(info->blend_method == WEBP_MUX_NO_BLEND ? 2 : 0) |
|
||||
(info->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ? 1 : 0);
|
||||
|
||||
frame->bytes = frame_bytes;
|
||||
frame->size = frame_size;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
// Outputs image data given a bitstream. The bitstream can either be a
|
||||
// single-image WebP file or raw VP8/VP8L data.
|
||||
// Also outputs 'is_lossless' to be true if the given bitstream is lossless.
|
||||
static WebPMuxError GetImageData(const WebPData* const bitstream,
|
||||
WebPData* const image, WebPData* const alpha,
|
||||
int* const is_lossless) {
|
||||
WebPDataInit(alpha); // Default: no alpha.
|
||||
if (bitstream->size < TAG_SIZE ||
|
||||
memcmp(bitstream->bytes, "RIFF", TAG_SIZE)) {
|
||||
// It is NOT webp file data. Return input data as is.
|
||||
*image = *bitstream;
|
||||
} else {
|
||||
// It is webp file data. Extract image data from it.
|
||||
const WebPMuxImage* wpi;
|
||||
WebPMux* const mux = WebPMuxCreate(bitstream, 0);
|
||||
if (mux == NULL) return WEBP_MUX_BAD_DATA;
|
||||
wpi = mux->images;
|
||||
assert(wpi != NULL && wpi->img != NULL);
|
||||
*image = wpi->img->data;
|
||||
if (wpi->alpha != NULL) {
|
||||
*alpha = wpi->alpha->data;
|
||||
}
|
||||
WebPMuxDelete(mux);
|
||||
}
|
||||
*is_lossless = VP8LCheckSignature(image->bytes, image->size);
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
static WebPMuxError DeleteChunks(WebPChunk** chunk_list, uint32_t tag) {
|
||||
WebPMuxError err = WEBP_MUX_NOT_FOUND;
|
||||
assert(chunk_list);
|
||||
while (*chunk_list) {
|
||||
WebPChunk* const chunk = *chunk_list;
|
||||
if (chunk->tag == tag) {
|
||||
*chunk_list = ChunkDelete(chunk);
|
||||
err = WEBP_MUX_OK;
|
||||
} else {
|
||||
chunk_list = &chunk->next;
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static WebPMuxError MuxDeleteAllNamedData(WebPMux* const mux, uint32_t tag) {
|
||||
const WebPChunkId id = ChunkGetIdFromTag(tag);
|
||||
assert(mux != NULL);
|
||||
if (IsWPI(id)) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
return DeleteChunks(MuxGetChunkListFromId(mux, id), tag);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Set API(s).
|
||||
|
||||
WebPMuxError WebPMuxSetChunk(WebPMux* mux, const char fourcc[4],
|
||||
const WebPData* chunk_data, int copy_data) {
|
||||
uint32_t tag;
|
||||
WebPMuxError err;
|
||||
if (mux == NULL || fourcc == NULL || chunk_data == NULL ||
|
||||
chunk_data->bytes == NULL || chunk_data->size > MAX_CHUNK_PAYLOAD) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
tag = ChunkGetTagFromFourCC(fourcc);
|
||||
|
||||
// Delete existing chunk(s) with the same 'fourcc'.
|
||||
err = MuxDeleteAllNamedData(mux, tag);
|
||||
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
|
||||
|
||||
// Add the given chunk.
|
||||
return MuxSet(mux, tag, chunk_data, copy_data);
|
||||
}
|
||||
|
||||
// Creates a chunk from given 'data' and sets it as 1st chunk in 'chunk_list'.
|
||||
static WebPMuxError AddDataToChunkList(const WebPData* const data,
|
||||
int copy_data, uint32_t tag,
|
||||
WebPChunk** chunk_list) {
|
||||
WebPChunk chunk;
|
||||
WebPMuxError err;
|
||||
ChunkInit(&chunk);
|
||||
err = ChunkAssignData(&chunk, data, copy_data, tag);
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
err = ChunkSetHead(&chunk, chunk_list);
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
return WEBP_MUX_OK;
|
||||
Err:
|
||||
ChunkRelease(&chunk);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Extracts image & alpha data from the given bitstream and then sets wpi.alpha
|
||||
// and wpi.img appropriately.
|
||||
static WebPMuxError SetAlphaAndImageChunks(const WebPData* const bitstream,
|
||||
int copy_data,
|
||||
WebPMuxImage* const wpi) {
|
||||
int is_lossless = 0;
|
||||
WebPData image, alpha;
|
||||
WebPMuxError err = GetImageData(bitstream, &image, &alpha, &is_lossless);
|
||||
const int image_tag =
|
||||
is_lossless ? kChunks[IDX_VP8L].tag : kChunks[IDX_VP8].tag;
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
if (alpha.bytes != NULL) {
|
||||
err = AddDataToChunkList(&alpha, copy_data, kChunks[IDX_ALPHA].tag,
|
||||
&wpi->alpha);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
}
|
||||
err = AddDataToChunkList(&image, copy_data, image_tag, &wpi->img);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
return MuxImageFinalize(wpi) ? WEBP_MUX_OK : WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxSetImage(WebPMux* mux, const WebPData* bitstream,
|
||||
int copy_data) {
|
||||
WebPMuxImage wpi;
|
||||
WebPMuxError err;
|
||||
|
||||
if (mux == NULL || bitstream == NULL || bitstream->bytes == NULL ||
|
||||
bitstream->size > MAX_CHUNK_PAYLOAD) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (mux->images != NULL) {
|
||||
// Only one 'simple image' can be added in mux. So, remove present images.
|
||||
DeleteAllImages(&mux->images);
|
||||
}
|
||||
|
||||
MuxImageInit(&wpi);
|
||||
err = SetAlphaAndImageChunks(bitstream, copy_data, &wpi);
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
|
||||
// Add this WebPMuxImage to mux.
|
||||
err = MuxImagePush(&wpi, &mux->images);
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
|
||||
// All is well.
|
||||
return WEBP_MUX_OK;
|
||||
|
||||
Err: // Something bad happened.
|
||||
MuxImageRelease(&wpi);
|
||||
return err;
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxPushFrame(WebPMux* mux, const WebPMuxFrameInfo* info,
|
||||
int copy_data) {
|
||||
WebPMuxImage wpi;
|
||||
WebPMuxError err;
|
||||
|
||||
if (mux == NULL || info == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
|
||||
if (info->id != WEBP_CHUNK_ANMF) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
|
||||
if (info->bitstream.bytes == NULL ||
|
||||
info->bitstream.size > MAX_CHUNK_PAYLOAD) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (mux->images != NULL) {
|
||||
const WebPMuxImage* const image = mux->images;
|
||||
const uint32_t image_id = (image->header != NULL)
|
||||
? ChunkGetIdFromTag(image->header->tag)
|
||||
: WEBP_CHUNK_IMAGE;
|
||||
if (image_id != info->id) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT; // Conflicting frame types.
|
||||
}
|
||||
}
|
||||
|
||||
MuxImageInit(&wpi);
|
||||
err = SetAlphaAndImageChunks(&info->bitstream, copy_data, &wpi);
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
assert(wpi.img != NULL); // As SetAlphaAndImageChunks() was successful.
|
||||
|
||||
{
|
||||
WebPData frame;
|
||||
const uint32_t tag = kChunks[IDX_ANMF].tag;
|
||||
WebPMuxFrameInfo tmp = *info;
|
||||
tmp.x_offset &= ~1; // Snap offsets to even.
|
||||
tmp.y_offset &= ~1;
|
||||
if (tmp.x_offset < 0 || tmp.x_offset >= MAX_POSITION_OFFSET ||
|
||||
tmp.y_offset < 0 || tmp.y_offset >= MAX_POSITION_OFFSET ||
|
||||
(tmp.duration < 0 || tmp.duration >= MAX_DURATION) ||
|
||||
tmp.dispose_method != (tmp.dispose_method & 1)) {
|
||||
err = WEBP_MUX_INVALID_ARGUMENT;
|
||||
goto Err;
|
||||
}
|
||||
err = CreateFrameData(wpi.width, wpi.height, &tmp, &frame);
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
// Add frame chunk (with copy_data = 1).
|
||||
err = AddDataToChunkList(&frame, 1, tag, &wpi.header);
|
||||
WebPDataClear(&frame); // frame owned by wpi.header now.
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
}
|
||||
|
||||
// Add this WebPMuxImage to mux.
|
||||
err = MuxImagePush(&wpi, &mux->images);
|
||||
if (err != WEBP_MUX_OK) goto Err;
|
||||
|
||||
// All is well.
|
||||
return WEBP_MUX_OK;
|
||||
|
||||
Err: // Something bad happened.
|
||||
MuxImageRelease(&wpi);
|
||||
return err;
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxSetAnimationParams(WebPMux* mux,
|
||||
const WebPMuxAnimParams* params) {
|
||||
WebPMuxError err;
|
||||
uint8_t data[ANIM_CHUNK_SIZE];
|
||||
const WebPData anim = {data, ANIM_CHUNK_SIZE};
|
||||
|
||||
if (mux == NULL || params == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
if (params->loop_count < 0 || params->loop_count >= MAX_LOOP_COUNT) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
// Delete any existing ANIM chunk(s).
|
||||
err = MuxDeleteAllNamedData(mux, kChunks[IDX_ANIM].tag);
|
||||
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
|
||||
|
||||
// Set the animation parameters.
|
||||
PutLE32(data, params->bgcolor);
|
||||
PutLE16(data + 4, params->loop_count);
|
||||
return MuxSet(mux, kChunks[IDX_ANIM].tag, &anim, 1);
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxSetCanvasSize(WebPMux* mux, int width, int height) {
|
||||
WebPMuxError err;
|
||||
if (mux == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
if (width < 0 || height < 0 || width > MAX_CANVAS_SIZE ||
|
||||
height > MAX_CANVAS_SIZE) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
if ((width * height) == 0 && (width | height) != 0) {
|
||||
// one of width / height is zero, but not both -> invalid!
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
// If we already assembled a VP8X chunk, invalidate it.
|
||||
err = MuxDeleteAllNamedData(mux, kChunks[IDX_VP8X].tag);
|
||||
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
|
||||
|
||||
mux->canvas_width = width;
|
||||
mux->canvas_height = height;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Delete API(s).
|
||||
|
||||
WebPMuxError WebPMuxDeleteChunk(WebPMux* mux, const char fourcc[4]) {
|
||||
if (mux == NULL || fourcc == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
return MuxDeleteAllNamedData(mux, ChunkGetTagFromFourCC(fourcc));
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxDeleteFrame(WebPMux* mux, uint32_t nth) {
|
||||
if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
return MuxImageDeleteNth(&mux->images, nth);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Assembly of the WebP RIFF file.
|
||||
|
||||
static WebPMuxError GetFrameInfo(const WebPChunk* const frame_chunk,
|
||||
int* const x_offset, int* const y_offset,
|
||||
int* const duration) {
|
||||
const WebPData* const data = &frame_chunk->data;
|
||||
const size_t expected_data_size = ANMF_CHUNK_SIZE;
|
||||
assert(frame_chunk != NULL);
|
||||
assert(frame_chunk->tag == kChunks[IDX_ANMF].tag);
|
||||
if (data->size != expected_data_size) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
|
||||
*x_offset = 2 * GetLE24(data->bytes + 0);
|
||||
*y_offset = 2 * GetLE24(data->bytes + 3);
|
||||
*duration = GetLE24(data->bytes + 12);
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
static WebPMuxError GetImageInfo(const WebPMuxImage* const wpi,
|
||||
int* const x_offset, int* const y_offset,
|
||||
int* const duration, int* const width,
|
||||
int* const height) {
|
||||
const WebPChunk* const frame_chunk = wpi->header;
|
||||
WebPMuxError err;
|
||||
assert(wpi != NULL);
|
||||
assert(frame_chunk != NULL);
|
||||
|
||||
// Get offsets and duration from ANMF chunk.
|
||||
err = GetFrameInfo(frame_chunk, x_offset, y_offset, duration);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
// Get width and height from VP8/VP8L chunk.
|
||||
if (width != NULL) *width = wpi->width;
|
||||
if (height != NULL) *height = wpi->height;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
// Returns the tightest dimension for the canvas considering the image list.
|
||||
static WebPMuxError GetAdjustedCanvasSize(const WebPMux* const mux,
|
||||
int* const width, int* const height) {
|
||||
WebPMuxImage* wpi = NULL;
|
||||
assert(mux != NULL);
|
||||
assert(width != NULL && height != NULL);
|
||||
|
||||
wpi = mux->images;
|
||||
assert(wpi != NULL);
|
||||
assert(wpi->img != NULL);
|
||||
|
||||
if (wpi->next != NULL) {
|
||||
int max_x = 0, max_y = 0;
|
||||
// if we have a chain of wpi's, header is necessarily set
|
||||
assert(wpi->header != NULL);
|
||||
// Aggregate the bounding box for animation frames.
|
||||
for (; wpi != NULL; wpi = wpi->next) {
|
||||
int x_offset = 0, y_offset = 0, duration = 0, w = 0, h = 0;
|
||||
const WebPMuxError err =
|
||||
GetImageInfo(wpi, &x_offset, &y_offset, &duration, &w, &h);
|
||||
const int max_x_pos = x_offset + w;
|
||||
const int max_y_pos = y_offset + h;
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
if (x_offset >= MAX_POSITION_OFFSET || y_offset >= MAX_POSITION_OFFSET) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (max_x_pos > max_x) max_x = max_x_pos;
|
||||
if (max_y_pos > max_y) max_y = max_y_pos;
|
||||
}
|
||||
*width = max_x;
|
||||
*height = max_y;
|
||||
} else {
|
||||
// For a single image, canvas dimensions are same as image dimensions.
|
||||
*width = wpi->width;
|
||||
*height = wpi->height;
|
||||
}
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
// VP8X format:
|
||||
// Total Size : 10,
|
||||
// Flags : 4 bytes,
|
||||
// Width : 3 bytes,
|
||||
// Height : 3 bytes.
|
||||
static WebPMuxError CreateVP8XChunk(WebPMux* const mux) {
|
||||
WebPMuxError err = WEBP_MUX_OK;
|
||||
uint32_t flags = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
uint8_t data[VP8X_CHUNK_SIZE];
|
||||
const WebPData vp8x = {data, VP8X_CHUNK_SIZE};
|
||||
const WebPMuxImage* images = NULL;
|
||||
|
||||
assert(mux != NULL);
|
||||
images = mux->images; // First image.
|
||||
if (images == NULL || images->img == NULL ||
|
||||
images->img->data.bytes == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
// If VP8X chunk(s) is(are) already present, remove them (and later add new
|
||||
// VP8X chunk with updated flags).
|
||||
err = MuxDeleteAllNamedData(mux, kChunks[IDX_VP8X].tag);
|
||||
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
|
||||
|
||||
// Set flags.
|
||||
if (mux->iccp != NULL && mux->iccp->data.bytes != NULL) {
|
||||
flags |= ICCP_FLAG;
|
||||
}
|
||||
if (mux->exif != NULL && mux->exif->data.bytes != NULL) {
|
||||
flags |= EXIF_FLAG;
|
||||
}
|
||||
if (mux->xmp != NULL && mux->xmp->data.bytes != NULL) {
|
||||
flags |= XMP_FLAG;
|
||||
}
|
||||
if (images->header != NULL) {
|
||||
if (images->header->tag == kChunks[IDX_ANMF].tag) {
|
||||
// This is an image with animation.
|
||||
flags |= ANIMATION_FLAG;
|
||||
}
|
||||
}
|
||||
if (MuxImageCount(images, WEBP_CHUNK_ALPHA) > 0) {
|
||||
flags |= ALPHA_FLAG; // Some images have an alpha channel.
|
||||
}
|
||||
|
||||
err = GetAdjustedCanvasSize(mux, &width, &height);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
if (width <= 0 || height <= 0) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
if (width > MAX_CANVAS_SIZE || height > MAX_CANVAS_SIZE) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (mux->canvas_width != 0 || mux->canvas_height != 0) {
|
||||
if (width > mux->canvas_width || height > mux->canvas_height) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
width = mux->canvas_width;
|
||||
height = mux->canvas_height;
|
||||
}
|
||||
|
||||
if (flags == 0 && mux->unknown == NULL) {
|
||||
// For simple file format, VP8X chunk should not be added.
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
if (MuxHasAlpha(images)) {
|
||||
// This means some frames explicitly/implicitly contain alpha.
|
||||
// Note: This 'flags' update must NOT be done for a lossless image
|
||||
// without a VP8X chunk!
|
||||
flags |= ALPHA_FLAG;
|
||||
}
|
||||
|
||||
PutLE32(data + 0, flags); // VP8X chunk flags.
|
||||
PutLE24(data + 4, width - 1); // canvas width.
|
||||
PutLE24(data + 7, height - 1); // canvas height.
|
||||
|
||||
return MuxSet(mux, kChunks[IDX_VP8X].tag, &vp8x, 1);
|
||||
}
|
||||
|
||||
// Cleans up 'mux' by removing any unnecessary chunks.
|
||||
static WebPMuxError MuxCleanup(WebPMux* const mux) {
|
||||
int num_frames;
|
||||
int num_anim_chunks;
|
||||
|
||||
// If we have an image with a single frame, and its rectangle
|
||||
// covers the whole canvas, convert it to a non-animated image
|
||||
// (to avoid writing ANMF chunk unnecessarily).
|
||||
WebPMuxError err = WebPMuxNumChunks(mux, kChunks[IDX_ANMF].id, &num_frames);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
if (num_frames == 1) {
|
||||
WebPMuxImage* frame = NULL;
|
||||
err = MuxImageGetNth((const WebPMuxImage**)&mux->images, 1, &frame);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
// We know that one frame does exist.
|
||||
assert(frame != NULL);
|
||||
if (frame->header != NULL &&
|
||||
((mux->canvas_width == 0 && mux->canvas_height == 0) ||
|
||||
(frame->width == mux->canvas_width &&
|
||||
frame->height == mux->canvas_height))) {
|
||||
assert(frame->header->tag == kChunks[IDX_ANMF].tag);
|
||||
ChunkDelete(frame->header); // Removes ANMF chunk.
|
||||
frame->header = NULL;
|
||||
num_frames = 0;
|
||||
}
|
||||
}
|
||||
// Remove ANIM chunk if this is a non-animated image.
|
||||
err = WebPMuxNumChunks(mux, kChunks[IDX_ANIM].id, &num_anim_chunks);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
if (num_anim_chunks >= 1 && num_frames == 0) {
|
||||
err = MuxDeleteAllNamedData(mux, kChunks[IDX_ANIM].tag);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
}
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
// Total size of a list of images.
|
||||
static size_t ImageListDiskSize(const WebPMuxImage* wpi_list) {
|
||||
size_t size = 0;
|
||||
while (wpi_list != NULL) {
|
||||
size += MuxImageDiskSize(wpi_list);
|
||||
wpi_list = wpi_list->next;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
// Write out the given list of images into 'dst'.
|
||||
static uint8_t* ImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst) {
|
||||
while (wpi_list != NULL) {
|
||||
dst = MuxImageEmit(wpi_list, dst);
|
||||
wpi_list = wpi_list->next;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxAssemble(WebPMux* mux, WebPData* assembled_data) {
|
||||
size_t size = 0;
|
||||
uint8_t* data = NULL;
|
||||
uint8_t* dst = NULL;
|
||||
WebPMuxError err;
|
||||
|
||||
if (assembled_data == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
// Clean up returned data, in case something goes wrong.
|
||||
memset(assembled_data, 0, sizeof(*assembled_data));
|
||||
|
||||
if (mux == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
// Finalize mux.
|
||||
err = MuxCleanup(mux);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
err = CreateVP8XChunk(mux);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
// Allocate data.
|
||||
size = ChunkListDiskSize(mux->vp8x) + ChunkListDiskSize(mux->iccp) +
|
||||
ChunkListDiskSize(mux->anim) + ImageListDiskSize(mux->images) +
|
||||
ChunkListDiskSize(mux->exif) + ChunkListDiskSize(mux->xmp) +
|
||||
ChunkListDiskSize(mux->unknown) + RIFF_HEADER_SIZE;
|
||||
|
||||
data = (uint8_t*)WebPSafeMalloc(1ULL, size);
|
||||
if (data == NULL) return WEBP_MUX_MEMORY_ERROR;
|
||||
|
||||
// Emit header & chunks.
|
||||
dst = MuxEmitRiffHeader(data, size);
|
||||
dst = ChunkListEmit(mux->vp8x, dst);
|
||||
dst = ChunkListEmit(mux->iccp, dst);
|
||||
dst = ChunkListEmit(mux->anim, dst);
|
||||
dst = ImageListEmit(mux->images, dst);
|
||||
dst = ChunkListEmit(mux->exif, dst);
|
||||
dst = ChunkListEmit(mux->xmp, dst);
|
||||
dst = ChunkListEmit(mux->unknown, dst);
|
||||
assert(dst == data + size);
|
||||
|
||||
// Validate mux.
|
||||
err = MuxValidate(mux);
|
||||
if (err != WEBP_MUX_OK) {
|
||||
WebPSafeFree(data);
|
||||
data = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
// Finalize data.
|
||||
assembled_data->bytes = data;
|
||||
assembled_data->size = size;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Vendored
+240
@@ -0,0 +1,240 @@
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Internal header for mux library.
|
||||
//
|
||||
// Author: Urvang (urvang@google.com)
|
||||
|
||||
#ifndef WEBP_MUX_MUXI_H_
|
||||
#define WEBP_MUX_MUXI_H_
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "src/dec/vp8i_dec.h"
|
||||
#include "src/dec/vp8li_dec.h"
|
||||
#include "src/webp/format_constants.h"
|
||||
#include "src/webp/mux.h"
|
||||
#include "src/webp/mux_types.h"
|
||||
#include "src/webp/types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Defines and constants.
|
||||
|
||||
#define MUX_MAJ_VERSION 1
|
||||
#define MUX_MIN_VERSION 6
|
||||
#define MUX_REV_VERSION 0
|
||||
|
||||
// Chunk object.
|
||||
typedef struct WebPChunk WebPChunk;
|
||||
struct WebPChunk {
|
||||
uint32_t tag;
|
||||
int owner; // True if *data memory is owned internally.
|
||||
// VP8X, ANIM, and other internally created chunks
|
||||
// like ANMF are always owned.
|
||||
WebPData data;
|
||||
WebPChunk* next;
|
||||
};
|
||||
|
||||
// MuxImage object. Store a full WebP image (including ANMF chunk, ALPH
|
||||
// chunk and VP8/VP8L chunk),
|
||||
typedef struct WebPMuxImage WebPMuxImage;
|
||||
struct WebPMuxImage {
|
||||
WebPChunk* header; // Corresponds to WEBP_CHUNK_ANMF.
|
||||
WebPChunk* alpha; // Corresponds to WEBP_CHUNK_ALPHA.
|
||||
WebPChunk* img; // Corresponds to WEBP_CHUNK_IMAGE.
|
||||
WebPChunk* unknown; // Corresponds to WEBP_CHUNK_UNKNOWN.
|
||||
int width;
|
||||
int height;
|
||||
int has_alpha; // Through ALPH chunk or as part of VP8L.
|
||||
int is_partial; // True if only some of the chunks are filled.
|
||||
WebPMuxImage* next;
|
||||
};
|
||||
|
||||
// Main mux object. Stores data chunks.
|
||||
struct WebPMux {
|
||||
WebPMuxImage* images;
|
||||
WebPChunk* iccp;
|
||||
WebPChunk* exif;
|
||||
WebPChunk* xmp;
|
||||
WebPChunk* anim;
|
||||
WebPChunk* vp8x;
|
||||
|
||||
WebPChunk* unknown;
|
||||
int canvas_width;
|
||||
int canvas_height;
|
||||
};
|
||||
|
||||
// CHUNK_INDEX enum: used for indexing within 'kChunks' (defined below) only.
|
||||
// Note: the reason for having two enums ('WebPChunkId' and 'CHUNK_INDEX') is to
|
||||
// allow two different chunks to have the same id (e.g. WebPChunkId
|
||||
// 'WEBP_CHUNK_IMAGE' can correspond to CHUNK_INDEX 'IDX_VP8' or 'IDX_VP8L').
|
||||
typedef enum {
|
||||
IDX_VP8X = 0,
|
||||
IDX_ICCP,
|
||||
IDX_ANIM,
|
||||
IDX_ANMF,
|
||||
IDX_ALPHA,
|
||||
IDX_VP8,
|
||||
IDX_VP8L,
|
||||
IDX_EXIF,
|
||||
IDX_XMP,
|
||||
IDX_UNKNOWN,
|
||||
|
||||
IDX_NIL,
|
||||
IDX_LAST_CHUNK
|
||||
} CHUNK_INDEX;
|
||||
|
||||
#define NIL_TAG 0x00000000u // To signal void chunk.
|
||||
|
||||
typedef struct {
|
||||
uint32_t tag;
|
||||
WebPChunkId id;
|
||||
uint32_t size;
|
||||
} ChunkInfo;
|
||||
|
||||
extern const ChunkInfo kChunks[IDX_LAST_CHUNK];
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chunk object management.
|
||||
|
||||
// Initialize.
|
||||
void ChunkInit(WebPChunk* const chunk);
|
||||
|
||||
// Get chunk index from chunk tag. Returns IDX_UNKNOWN if not found.
|
||||
CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag);
|
||||
|
||||
// Get chunk id from chunk tag. Returns WEBP_CHUNK_UNKNOWN if not found.
|
||||
WebPChunkId ChunkGetIdFromTag(uint32_t tag);
|
||||
|
||||
// Convert a fourcc string to a tag.
|
||||
uint32_t ChunkGetTagFromFourCC(const char fourcc[4]);
|
||||
|
||||
// Get chunk index from fourcc. Returns IDX_UNKNOWN if given fourcc is unknown.
|
||||
CHUNK_INDEX ChunkGetIndexFromFourCC(const char fourcc[4]);
|
||||
|
||||
// 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.
|
||||
WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,
|
||||
int copy_data, uint32_t tag);
|
||||
|
||||
// Sets 'chunk' as the only element in 'chunk_list' if it is empty.
|
||||
// On success ownership is transferred from 'chunk' to the 'chunk_list'.
|
||||
WebPMuxError ChunkSetHead(WebPChunk* const chunk, WebPChunk** const chunk_list);
|
||||
// Sets 'chunk' at last position in the 'chunk_list'.
|
||||
// On success ownership is transferred from 'chunk' to the 'chunk_list'.
|
||||
// *chunk_list also points towards the last valid element of the initial
|
||||
// *chunk_list.
|
||||
WebPMuxError ChunkAppend(WebPChunk* const chunk, WebPChunk*** const chunk_list);
|
||||
|
||||
// Releases chunk and returns chunk->next.
|
||||
WebPChunk* ChunkRelease(WebPChunk* const chunk);
|
||||
|
||||
// Deletes given chunk & returns chunk->next.
|
||||
WebPChunk* ChunkDelete(WebPChunk* const chunk);
|
||||
|
||||
// Deletes all chunks in the given chunk list.
|
||||
void ChunkListDelete(WebPChunk** const chunk_list);
|
||||
|
||||
// Returns size of the chunk including chunk header and padding byte (if any).
|
||||
static WEBP_INLINE size_t SizeWithPadding(size_t chunk_size) {
|
||||
assert(chunk_size <= MAX_CHUNK_PAYLOAD);
|
||||
return CHUNK_HEADER_SIZE + ((chunk_size + 1) & ~1U);
|
||||
}
|
||||
|
||||
// Size of a chunk including header and padding.
|
||||
static WEBP_INLINE size_t ChunkDiskSize(const WebPChunk* chunk) {
|
||||
const size_t data_size = chunk->data.size;
|
||||
return SizeWithPadding(data_size);
|
||||
}
|
||||
|
||||
// Total size of a list of chunks.
|
||||
size_t ChunkListDiskSize(const WebPChunk* chunk_list);
|
||||
|
||||
// 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);
|
||||
|
||||
// Releases image 'wpi' and returns wpi->next.
|
||||
WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi);
|
||||
|
||||
// Delete image 'wpi' and return the next image in the list or NULL.
|
||||
// 'wpi' can be NULL.
|
||||
WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi);
|
||||
|
||||
// Count number of images matching the given tag id in the 'wpi_list'.
|
||||
// If id == WEBP_CHUNK_NIL, all images will be matched.
|
||||
int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id);
|
||||
|
||||
// Update width/height/has_alpha info from chunks within wpi.
|
||||
// Also remove ALPH chunk if not needed.
|
||||
int MuxImageFinalize(WebPMuxImage* const wpi);
|
||||
|
||||
// Check if given ID corresponds to an image related chunk.
|
||||
static WEBP_INLINE int IsWPI(WebPChunkId id) {
|
||||
switch (id) {
|
||||
case WEBP_CHUNK_ANMF:
|
||||
case WEBP_CHUNK_ALPHA:
|
||||
case WEBP_CHUNK_IMAGE:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Pushes 'wpi' at the end of 'wpi_list'.
|
||||
WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list);
|
||||
|
||||
// Delete nth image in the image list.
|
||||
WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth);
|
||||
|
||||
// Get nth image in the image list.
|
||||
WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
|
||||
WebPMuxImage** wpi);
|
||||
|
||||
// Total size of the given image.
|
||||
size_t MuxImageDiskSize(const WebPMuxImage* const wpi);
|
||||
|
||||
// Write out the given image into 'dst'.
|
||||
uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Helper methods for mux.
|
||||
|
||||
// Checks if the given image list contains at least one image with alpha.
|
||||
int MuxHasAlpha(const WebPMuxImage* images);
|
||||
|
||||
// Write out RIFF header into 'data', given total data size 'size'.
|
||||
uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size);
|
||||
|
||||
// Returns the list where chunk with given ID is to be inserted in mux.
|
||||
WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id);
|
||||
|
||||
// Validates the given mux object.
|
||||
WebPMuxError MuxValidate(const WebPMux* const mux);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // WEBP_MUX_MUXI_H_
|
||||
+561
@@ -0,0 +1,561 @@
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Internal objects and utils for mux.
|
||||
//
|
||||
// Authors: Urvang (urvang@google.com)
|
||||
// Vikas (vikasa@google.com)
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "src/mux/muxi.h"
|
||||
#include "src/utils/utils.h"
|
||||
#include "src/webp/format_constants.h"
|
||||
#include "src/webp/mux.h"
|
||||
#include "src/webp/mux_types.h"
|
||||
#include "src/webp/types.h"
|
||||
|
||||
#define UNDEFINED_CHUNK_SIZE ((uint32_t)(-1))
|
||||
|
||||
const ChunkInfo kChunks[] = {
|
||||
{MKFOURCC('V', 'P', '8', 'X'), WEBP_CHUNK_VP8X, VP8X_CHUNK_SIZE},
|
||||
{MKFOURCC('I', 'C', 'C', 'P'), WEBP_CHUNK_ICCP, UNDEFINED_CHUNK_SIZE},
|
||||
{MKFOURCC('A', 'N', 'I', 'M'), WEBP_CHUNK_ANIM, ANIM_CHUNK_SIZE},
|
||||
{MKFOURCC('A', 'N', 'M', 'F'), WEBP_CHUNK_ANMF, ANMF_CHUNK_SIZE},
|
||||
{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},
|
||||
{MKFOURCC('E', 'X', 'I', 'F'), WEBP_CHUNK_EXIF, UNDEFINED_CHUNK_SIZE},
|
||||
{MKFOURCC('X', 'M', 'P', ' '), WEBP_CHUNK_XMP, UNDEFINED_CHUNK_SIZE},
|
||||
{NIL_TAG, WEBP_CHUNK_UNKNOWN, UNDEFINED_CHUNK_SIZE},
|
||||
|
||||
{NIL_TAG, WEBP_CHUNK_NIL, UNDEFINED_CHUNK_SIZE}};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int WebPGetMuxVersion(void) {
|
||||
return (MUX_MAJ_VERSION << 16) | (MUX_MIN_VERSION << 8) | MUX_REV_VERSION;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Life of a chunk object.
|
||||
|
||||
void ChunkInit(WebPChunk* const chunk) {
|
||||
assert(chunk);
|
||||
memset(chunk, 0, sizeof(*chunk));
|
||||
chunk->tag = NIL_TAG;
|
||||
}
|
||||
|
||||
WebPChunk* ChunkRelease(WebPChunk* const chunk) {
|
||||
WebPChunk* next;
|
||||
if (chunk == NULL) return NULL;
|
||||
if (chunk->owner) {
|
||||
WebPDataClear(&chunk->data);
|
||||
}
|
||||
next = chunk->next;
|
||||
ChunkInit(chunk);
|
||||
return next;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chunk misc methods.
|
||||
|
||||
CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag) {
|
||||
int i;
|
||||
for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
|
||||
if (tag == kChunks[i].tag) return (CHUNK_INDEX)i;
|
||||
}
|
||||
return IDX_UNKNOWN;
|
||||
}
|
||||
|
||||
WebPChunkId ChunkGetIdFromTag(uint32_t tag) {
|
||||
int i;
|
||||
for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
|
||||
if (tag == kChunks[i].tag) return kChunks[i].id;
|
||||
}
|
||||
return WEBP_CHUNK_UNKNOWN;
|
||||
}
|
||||
|
||||
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);
|
||||
return ChunkGetIndexFromTag(tag);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chunk search methods.
|
||||
|
||||
// Returns next chunk in the chunk list with the given tag.
|
||||
static WebPChunk* ChunkSearchNextInList(WebPChunk* chunk, uint32_t tag) {
|
||||
while (chunk != NULL && chunk->tag != tag) {
|
||||
chunk = chunk->next;
|
||||
}
|
||||
return chunk;
|
||||
}
|
||||
|
||||
WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag) {
|
||||
uint32_t iter = nth;
|
||||
first = ChunkSearchNextInList(first, tag);
|
||||
if (first == NULL) return NULL;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chunk writer methods.
|
||||
|
||||
WebPMuxError ChunkAssignData(WebPChunk* chunk, const WebPData* const data,
|
||||
int copy_data, uint32_t tag) {
|
||||
// For internally allocated chunks, always copy data & make it owner of data.
|
||||
if (tag == kChunks[IDX_VP8X].tag || tag == kChunks[IDX_ANIM].tag) {
|
||||
copy_data = 1;
|
||||
}
|
||||
|
||||
ChunkRelease(chunk);
|
||||
|
||||
if (data != NULL) {
|
||||
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.
|
||||
chunk->data = *data;
|
||||
}
|
||||
}
|
||||
chunk->tag = tag;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
WebPMuxError ChunkSetHead(WebPChunk* const chunk,
|
||||
WebPChunk** const chunk_list) {
|
||||
WebPChunk* new_chunk;
|
||||
|
||||
assert(chunk_list != NULL);
|
||||
if (*chunk_list != NULL) {
|
||||
return WEBP_MUX_NOT_FOUND;
|
||||
}
|
||||
|
||||
new_chunk = (WebPChunk*)WebPSafeMalloc(1ULL, sizeof(*new_chunk));
|
||||
if (new_chunk == NULL) return WEBP_MUX_MEMORY_ERROR;
|
||||
*new_chunk = *chunk;
|
||||
chunk->owner = 0;
|
||||
new_chunk->next = NULL;
|
||||
*chunk_list = new_chunk;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
WebPMuxError ChunkAppend(WebPChunk* const chunk,
|
||||
WebPChunk*** const chunk_list) {
|
||||
WebPMuxError err;
|
||||
assert(chunk_list != NULL && *chunk_list != NULL);
|
||||
|
||||
if (**chunk_list == NULL) {
|
||||
err = ChunkSetHead(chunk, *chunk_list);
|
||||
} else {
|
||||
WebPChunk* last_chunk = **chunk_list;
|
||||
while (last_chunk->next != NULL) last_chunk = last_chunk->next;
|
||||
err = ChunkSetHead(chunk, &last_chunk->next);
|
||||
if (err == WEBP_MUX_OK) *chunk_list = &last_chunk->next;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chunk deletion method(s).
|
||||
|
||||
WebPChunk* ChunkDelete(WebPChunk* const chunk) {
|
||||
WebPChunk* const next = ChunkRelease(chunk);
|
||||
WebPSafeFree(chunk);
|
||||
return next;
|
||||
}
|
||||
|
||||
void ChunkListDelete(WebPChunk** const chunk_list) {
|
||||
while (*chunk_list != NULL) {
|
||||
*chunk_list = ChunkDelete(*chunk_list);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chunk serialization methods.
|
||||
|
||||
static uint8_t* ChunkEmit(const WebPChunk* const chunk, uint8_t* dst) {
|
||||
const size_t chunk_size = chunk->data.size;
|
||||
assert(chunk);
|
||||
// Do not check chunk->tag != NIL as NIL_TAG could have been read from a file.
|
||||
PutLE32(dst + 0, chunk->tag);
|
||||
PutLE32(dst + TAG_SIZE, (uint32_t)chunk_size);
|
||||
assert(chunk_size == (uint32_t)chunk_size);
|
||||
memcpy(dst + CHUNK_HEADER_SIZE, chunk->data.bytes, chunk_size);
|
||||
if (chunk_size & 1) dst[CHUNK_HEADER_SIZE + chunk_size] = 0; // Add padding.
|
||||
return dst + ChunkDiskSize(chunk);
|
||||
}
|
||||
|
||||
uint8_t* ChunkListEmit(const WebPChunk* chunk_list, uint8_t* dst) {
|
||||
while (chunk_list != NULL) {
|
||||
dst = ChunkEmit(chunk_list, dst);
|
||||
chunk_list = chunk_list->next;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Life of a MuxImage object.
|
||||
|
||||
void MuxImageInit(WebPMuxImage* const wpi) {
|
||||
assert(wpi);
|
||||
memset(wpi, 0, sizeof(*wpi));
|
||||
}
|
||||
|
||||
WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi) {
|
||||
WebPMuxImage* next;
|
||||
if (wpi == NULL) return NULL;
|
||||
// There should be at most one chunk of 'header', 'alpha', 'img' but we call
|
||||
// ChunkListDelete to be safe
|
||||
ChunkListDelete(&wpi->header);
|
||||
ChunkListDelete(&wpi->alpha);
|
||||
ChunkListDelete(&wpi->img);
|
||||
ChunkListDelete(&wpi->unknown);
|
||||
|
||||
next = wpi->next;
|
||||
MuxImageInit(wpi);
|
||||
return next;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// MuxImage search methods.
|
||||
|
||||
// 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) {
|
||||
case WEBP_CHUNK_ANMF:
|
||||
return (WebPChunk**)&wpi->header;
|
||||
case WEBP_CHUNK_ALPHA:
|
||||
return (WebPChunk**)&wpi->alpha;
|
||||
case WEBP_CHUNK_IMAGE:
|
||||
return (WebPChunk**)&wpi->img;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int MuxImageCount(const WebPMuxImage* wpi_list, WebPChunkId id) {
|
||||
int count = 0;
|
||||
const WebPMuxImage* current;
|
||||
for (current = wpi_list; current != NULL; current = current->next) {
|
||||
if (id == WEBP_CHUNK_NIL) {
|
||||
++count; // Special case: count all images.
|
||||
} else {
|
||||
const WebPChunk* const wpi_chunk = *GetChunkListFromId(current, id);
|
||||
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'.
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Outputs a pointer to 'prev_wpi->next',
|
||||
// where 'prev_wpi' is the pointer to the image at position (nth - 1).
|
||||
// Returns true if nth image was found.
|
||||
static int SearchImageToGetOrDelete(WebPMuxImage** wpi_list, uint32_t nth,
|
||||
WebPMuxImage*** const location) {
|
||||
uint32_t count = 0;
|
||||
assert(wpi_list);
|
||||
*location = wpi_list;
|
||||
|
||||
if (nth == 0) {
|
||||
nth = MuxImageCount(*wpi_list, WEBP_CHUNK_NIL);
|
||||
if (nth == 0) return 0; // Not found.
|
||||
}
|
||||
|
||||
while (*wpi_list != NULL) {
|
||||
WebPMuxImage* const cur_wpi = *wpi_list;
|
||||
++count;
|
||||
if (count == nth) return 1; // Found.
|
||||
wpi_list = &cur_wpi->next;
|
||||
*location = wpi_list;
|
||||
}
|
||||
return 0; // Not found.
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// MuxImage writer methods.
|
||||
|
||||
WebPMuxError MuxImagePush(const WebPMuxImage* wpi, WebPMuxImage** wpi_list) {
|
||||
WebPMuxImage* new_wpi;
|
||||
|
||||
while (*wpi_list != NULL) {
|
||||
WebPMuxImage* const cur_wpi = *wpi_list;
|
||||
if (cur_wpi->next == NULL) break;
|
||||
wpi_list = &cur_wpi->next;
|
||||
}
|
||||
|
||||
new_wpi = (WebPMuxImage*)WebPSafeMalloc(1ULL, sizeof(*new_wpi));
|
||||
if (new_wpi == NULL) return WEBP_MUX_MEMORY_ERROR;
|
||||
*new_wpi = *wpi;
|
||||
new_wpi->next = NULL;
|
||||
|
||||
if (*wpi_list != NULL) {
|
||||
(*wpi_list)->next = new_wpi;
|
||||
} else {
|
||||
*wpi_list = new_wpi;
|
||||
}
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// MuxImage deletion methods.
|
||||
|
||||
WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi) {
|
||||
// Delete the components of wpi. If wpi is NULL this is a noop.
|
||||
WebPMuxImage* const next = MuxImageRelease(wpi);
|
||||
WebPSafeFree(wpi);
|
||||
return next;
|
||||
}
|
||||
|
||||
WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth) {
|
||||
assert(wpi_list);
|
||||
if (!SearchImageToGetOrDelete(wpi_list, nth, &wpi_list)) {
|
||||
return WEBP_MUX_NOT_FOUND;
|
||||
}
|
||||
*wpi_list = MuxImageDelete(*wpi_list);
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// MuxImage reader methods.
|
||||
|
||||
WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
|
||||
WebPMuxImage** wpi) {
|
||||
assert(wpi_list);
|
||||
assert(wpi);
|
||||
if (!SearchImageToGetOrDelete((WebPMuxImage**)wpi_list, nth,
|
||||
(WebPMuxImage***)&wpi_list)) {
|
||||
return WEBP_MUX_NOT_FOUND;
|
||||
}
|
||||
*wpi = (WebPMuxImage*)*wpi_list;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// MuxImage serialization methods.
|
||||
|
||||
// Size of an image.
|
||||
size_t MuxImageDiskSize(const WebPMuxImage* const wpi) {
|
||||
size_t size = 0;
|
||||
if (wpi->header != NULL) size += ChunkDiskSize(wpi->header);
|
||||
if (wpi->alpha != NULL) size += ChunkDiskSize(wpi->alpha);
|
||||
if (wpi->img != NULL) size += ChunkDiskSize(wpi->img);
|
||||
if (wpi->unknown != NULL) size += ChunkListDiskSize(wpi->unknown);
|
||||
return size;
|
||||
}
|
||||
|
||||
// Special case as ANMF chunk encapsulates other image chunks.
|
||||
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;
|
||||
assert(header->tag == kChunks[IDX_ANMF].tag);
|
||||
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);
|
||||
}
|
||||
|
||||
uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst) {
|
||||
// Ordering of chunks to be emitted is strictly as follows:
|
||||
// 1. ANMF chunk (if present).
|
||||
// 2. ALPH chunk (if present).
|
||||
// 3. VP8/VP8L chunk.
|
||||
assert(wpi);
|
||||
if (wpi->header != NULL) {
|
||||
dst = ChunkEmitSpecial(wpi->header, MuxImageDiskSize(wpi), dst);
|
||||
}
|
||||
if (wpi->alpha != NULL) dst = ChunkEmit(wpi->alpha, dst);
|
||||
if (wpi->img != NULL) dst = ChunkEmit(wpi->img, dst);
|
||||
if (wpi->unknown != NULL) dst = ChunkListEmit(wpi->unknown, dst);
|
||||
return dst;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Helper methods for mux.
|
||||
|
||||
int MuxHasAlpha(const WebPMuxImage* images) {
|
||||
while (images != NULL) {
|
||||
if (images->has_alpha) return 1;
|
||||
images = images->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t* MuxEmitRiffHeader(uint8_t* const data, size_t size) {
|
||||
PutLE32(data + 0, MKFOURCC('R', 'I', 'F', 'F'));
|
||||
PutLE32(data + TAG_SIZE, (uint32_t)size - CHUNK_HEADER_SIZE);
|
||||
assert(size == (uint32_t)size);
|
||||
PutLE32(data + TAG_SIZE + CHUNK_SIZE_BYTES, MKFOURCC('W', 'E', 'B', 'P'));
|
||||
return data + RIFF_HEADER_SIZE;
|
||||
}
|
||||
|
||||
WebPChunk** MuxGetChunkListFromId(const WebPMux* mux, WebPChunkId id) {
|
||||
assert(mux != NULL);
|
||||
switch (id) {
|
||||
case WEBP_CHUNK_VP8X:
|
||||
return (WebPChunk**)&mux->vp8x;
|
||||
case WEBP_CHUNK_ICCP:
|
||||
return (WebPChunk**)&mux->iccp;
|
||||
case WEBP_CHUNK_ANIM:
|
||||
return (WebPChunk**)&mux->anim;
|
||||
case WEBP_CHUNK_EXIF:
|
||||
return (WebPChunk**)&mux->exif;
|
||||
case WEBP_CHUNK_XMP:
|
||||
return (WebPChunk**)&mux->xmp;
|
||||
default:
|
||||
return (WebPChunk**)&mux->unknown;
|
||||
}
|
||||
}
|
||||
|
||||
static int IsNotCompatible(int feature, int num_items) {
|
||||
return (feature != 0) != (num_items > 0);
|
||||
}
|
||||
|
||||
#define NO_FLAG ((WebPFeatureFlags)0)
|
||||
|
||||
// Test basic constraints:
|
||||
// retrieval, maximum number of chunks by index (use -1 to skip)
|
||||
// and feature incompatibility (use NO_FLAG to skip).
|
||||
// On success returns WEBP_MUX_OK and stores the chunk count in *num.
|
||||
static WebPMuxError ValidateChunk(const WebPMux* const mux, CHUNK_INDEX idx,
|
||||
WebPFeatureFlags feature, uint32_t vp8x_flags,
|
||||
int max, int* num) {
|
||||
const WebPMuxError err = WebPMuxNumChunks(mux, kChunks[idx].id, num);
|
||||
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;
|
||||
}
|
||||
|
||||
WebPMuxError MuxValidate(const WebPMux* const mux) {
|
||||
int num_iccp;
|
||||
int num_exif;
|
||||
int num_xmp;
|
||||
int num_anim;
|
||||
int num_frames;
|
||||
int num_vp8x;
|
||||
int num_images;
|
||||
int num_alpha;
|
||||
uint32_t flags;
|
||||
WebPMuxError err;
|
||||
|
||||
// Verify mux is not NULL.
|
||||
if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
|
||||
// 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.
|
||||
err = ValidateChunk(mux, IDX_ICCP, ICCP_FLAG, flags, 1, &num_iccp);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
// At most one EXIF metadata.
|
||||
err = ValidateChunk(mux, IDX_EXIF, EXIF_FLAG, flags, 1, &num_exif);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
// At most one XMP metadata.
|
||||
err = ValidateChunk(mux, IDX_XMP, XMP_FLAG, flags, 1, &num_xmp);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
// 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);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
err = ValidateChunk(mux, IDX_ANMF, NO_FLAG, flags, -1, &num_frames);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
{
|
||||
const int has_animation = !!(flags & ANIMATION_FLAG);
|
||||
if (has_animation && (num_anim == 0 || num_frames == 0)) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
if (!has_animation && (num_anim == 1 || num_frames > 0)) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verify either VP8X chunk is present OR there is only one elem in
|
||||
// mux->images.
|
||||
err = ValidateChunk(mux, IDX_VP8X, NO_FLAG, flags, 1, &num_vp8x);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
err = ValidateChunk(mux, IDX_VP8, NO_FLAG, flags, -1, &num_images);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
|
||||
// ALPHA_FLAG & alpha chunk(s) are consistent.
|
||||
// Note: ALPHA_FLAG can be set when there is actually no Alpha data present.
|
||||
if (MuxHasAlpha(mux->images)) {
|
||||
if (num_vp8x > 0) {
|
||||
// VP8X chunk is present, so it should contain ALPHA_FLAG.
|
||||
if (!(flags & ALPHA_FLAG)) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
} else {
|
||||
// VP8X chunk is not present, so ALPH chunks should NOT be present either.
|
||||
err = WebPMuxNumChunks(mux, WEBP_CHUNK_ALPHA, &num_alpha);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
if (num_alpha > 0) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
#undef NO_FLAG
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Vendored
+572
@@ -0,0 +1,572 @@
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Read APIs for mux.
|
||||
//
|
||||
// Authors: Urvang (urvang@google.com)
|
||||
// Vikas (vikasa@google.com)
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "src/dec/vp8_dec.h"
|
||||
#include "src/mux/muxi.h"
|
||||
#include "src/utils/utils.h"
|
||||
#include "src/webp/format_constants.h"
|
||||
#include "src/webp/mux.h"
|
||||
#include "src/webp/mux_types.h"
|
||||
#include "src/webp/types.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Helper method(s).
|
||||
|
||||
// Handy MACRO.
|
||||
#define SWITCH_ID_LIST(INDEX, LIST) \
|
||||
do { \
|
||||
if (idx == (INDEX)) { \
|
||||
const WebPChunk* const chunk = \
|
||||
ChunkSearchList((LIST), nth, kChunks[(INDEX)].tag); \
|
||||
if (chunk) { \
|
||||
*data = chunk->data; \
|
||||
return WEBP_MUX_OK; \
|
||||
} else { \
|
||||
return WEBP_MUX_NOT_FOUND; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static WebPMuxError MuxGet(const WebPMux* const mux, CHUNK_INDEX idx,
|
||||
uint32_t nth, WebPData* const data) {
|
||||
assert(mux != NULL);
|
||||
assert(idx != IDX_LAST_CHUNK);
|
||||
assert(!IsWPI(kChunks[idx].id));
|
||||
WebPDataInit(data);
|
||||
|
||||
SWITCH_ID_LIST(IDX_VP8X, mux->vp8x);
|
||||
SWITCH_ID_LIST(IDX_ICCP, mux->iccp);
|
||||
SWITCH_ID_LIST(IDX_ANIM, mux->anim);
|
||||
SWITCH_ID_LIST(IDX_EXIF, mux->exif);
|
||||
SWITCH_ID_LIST(IDX_XMP, mux->xmp);
|
||||
assert(idx != IDX_UNKNOWN);
|
||||
return WEBP_MUX_NOT_FOUND;
|
||||
}
|
||||
#undef SWITCH_ID_LIST
|
||||
|
||||
// Fill the chunk with the given data (includes chunk header bytes), after some
|
||||
// verifications.
|
||||
static WebPMuxError ChunkVerifyAndAssign(WebPChunk* chunk, const uint8_t* data,
|
||||
size_t data_size, size_t riff_size,
|
||||
int copy_data) {
|
||||
uint32_t chunk_size;
|
||||
WebPData chunk_data;
|
||||
|
||||
// Correctness checks.
|
||||
if (data_size < CHUNK_HEADER_SIZE) return WEBP_MUX_NOT_ENOUGH_DATA;
|
||||
chunk_size = GetLE32(data + TAG_SIZE);
|
||||
if (chunk_size > MAX_CHUNK_PAYLOAD) return WEBP_MUX_BAD_DATA;
|
||||
|
||||
{
|
||||
const size_t chunk_disk_size = SizeWithPadding(chunk_size);
|
||||
if (chunk_disk_size > riff_size) return WEBP_MUX_BAD_DATA;
|
||||
if (chunk_disk_size > data_size) return WEBP_MUX_NOT_ENOUGH_DATA;
|
||||
}
|
||||
|
||||
// Data assignment.
|
||||
chunk_data.bytes = data + CHUNK_HEADER_SIZE;
|
||||
chunk_data.size = chunk_size;
|
||||
return ChunkAssignData(chunk, &chunk_data, copy_data, GetLE32(data + 0));
|
||||
}
|
||||
|
||||
int MuxImageFinalize(WebPMuxImage* const wpi) {
|
||||
const WebPChunk* const img = wpi->img;
|
||||
const WebPData* const image = &img->data;
|
||||
const int is_lossless = (img->tag == kChunks[IDX_VP8L].tag);
|
||||
int w, h;
|
||||
int vp8l_has_alpha = 0;
|
||||
const int ok =
|
||||
is_lossless
|
||||
? VP8LGetInfo(image->bytes, image->size, &w, &h, &vp8l_has_alpha)
|
||||
: VP8GetInfo(image->bytes, image->size, image->size, &w, &h);
|
||||
assert(img != NULL);
|
||||
if (ok) {
|
||||
// Ignore ALPH chunk accompanying VP8L.
|
||||
if (is_lossless && (wpi->alpha != NULL)) {
|
||||
ChunkDelete(wpi->alpha);
|
||||
wpi->alpha = NULL;
|
||||
}
|
||||
wpi->width = w;
|
||||
wpi->height = h;
|
||||
wpi->has_alpha = vp8l_has_alpha || (wpi->alpha != NULL);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int MuxImageParse(const WebPChunk* const chunk, int copy_data,
|
||||
WebPMuxImage* const wpi) {
|
||||
const uint8_t* bytes = chunk->data.bytes;
|
||||
size_t size = chunk->data.size;
|
||||
const uint8_t* const last = (bytes == NULL) ? NULL : bytes + size;
|
||||
WebPChunk subchunk;
|
||||
size_t subchunk_size;
|
||||
WebPChunk** unknown_chunk_list = &wpi->unknown;
|
||||
ChunkInit(&subchunk);
|
||||
|
||||
assert(chunk->tag == kChunks[IDX_ANMF].tag);
|
||||
assert(!wpi->is_partial);
|
||||
|
||||
// ANMF.
|
||||
{
|
||||
const size_t hdr_size = ANMF_CHUNK_SIZE;
|
||||
const WebPData temp = {bytes, hdr_size};
|
||||
// Each of ANMF chunk contain a header at the beginning. So, its size should
|
||||
// be at least 'hdr_size'.
|
||||
if (size < hdr_size) goto Fail;
|
||||
if (ChunkAssignData(&subchunk, &temp, copy_data, chunk->tag) !=
|
||||
WEBP_MUX_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
if (ChunkSetHead(&subchunk, &wpi->header) != WEBP_MUX_OK) goto Fail;
|
||||
wpi->is_partial = 1; // Waiting for ALPH and/or VP8/VP8L chunks.
|
||||
|
||||
// Rest of the chunks.
|
||||
subchunk_size = ChunkDiskSize(&subchunk) - CHUNK_HEADER_SIZE;
|
||||
bytes += subchunk_size;
|
||||
size -= subchunk_size;
|
||||
|
||||
while (bytes != last) {
|
||||
ChunkInit(&subchunk);
|
||||
if (ChunkVerifyAndAssign(&subchunk, bytes, size, size, copy_data) !=
|
||||
WEBP_MUX_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
switch (ChunkGetIdFromTag(subchunk.tag)) {
|
||||
case WEBP_CHUNK_ALPHA:
|
||||
if (wpi->alpha != NULL) goto Fail; // Consecutive ALPH chunks.
|
||||
if (ChunkSetHead(&subchunk, &wpi->alpha) != WEBP_MUX_OK) goto Fail;
|
||||
wpi->is_partial = 1; // Waiting for a VP8 chunk.
|
||||
break;
|
||||
case WEBP_CHUNK_IMAGE:
|
||||
if (wpi->img != NULL) goto Fail; // Only 1 image chunk allowed.
|
||||
if (ChunkSetHead(&subchunk, &wpi->img) != WEBP_MUX_OK) goto Fail;
|
||||
if (!MuxImageFinalize(wpi)) goto Fail;
|
||||
wpi->is_partial = 0; // wpi is completely filled.
|
||||
break;
|
||||
case WEBP_CHUNK_UNKNOWN:
|
||||
if (wpi->is_partial) {
|
||||
goto Fail; // Encountered an unknown chunk
|
||||
// before some image chunks.
|
||||
}
|
||||
if (ChunkAppend(&subchunk, &unknown_chunk_list) != WEBP_MUX_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
goto Fail;
|
||||
}
|
||||
subchunk_size = ChunkDiskSize(&subchunk);
|
||||
bytes += subchunk_size;
|
||||
size -= subchunk_size;
|
||||
}
|
||||
if (wpi->is_partial) goto Fail;
|
||||
return 1;
|
||||
|
||||
Fail:
|
||||
ChunkRelease(&subchunk);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Create a mux object from WebP-RIFF data.
|
||||
|
||||
WebPMux* WebPMuxCreateInternal(const WebPData* bitstream, int copy_data,
|
||||
int version) {
|
||||
size_t riff_size;
|
||||
uint32_t tag;
|
||||
const uint8_t* end;
|
||||
WebPMux* mux = NULL;
|
||||
WebPMuxImage* wpi = NULL;
|
||||
const uint8_t* data;
|
||||
size_t size;
|
||||
WebPChunk chunk;
|
||||
// Stores the end of the chunk lists so that it is faster to append data to
|
||||
// their ends.
|
||||
WebPChunk** chunk_list_ends[WEBP_CHUNK_NIL + 1] = {NULL};
|
||||
ChunkInit(&chunk);
|
||||
|
||||
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_MUX_ABI_VERSION)) {
|
||||
return NULL; // version mismatch
|
||||
}
|
||||
if (bitstream == NULL) return NULL;
|
||||
|
||||
data = bitstream->bytes;
|
||||
size = bitstream->size;
|
||||
|
||||
if (data == NULL) return NULL;
|
||||
if (size < RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE) return NULL;
|
||||
if (GetLE32(data + 0) != MKFOURCC('R', 'I', 'F', 'F') ||
|
||||
GetLE32(data + CHUNK_HEADER_SIZE) != MKFOURCC('W', 'E', 'B', 'P')) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mux = WebPMuxNew();
|
||||
if (mux == NULL) return NULL;
|
||||
|
||||
tag = GetLE32(data + RIFF_HEADER_SIZE);
|
||||
if (tag != kChunks[IDX_VP8].tag && tag != kChunks[IDX_VP8L].tag &&
|
||||
tag != kChunks[IDX_VP8X].tag) {
|
||||
goto Err; // First chunk should be VP8, VP8L or VP8X.
|
||||
}
|
||||
|
||||
riff_size = GetLE32(data + TAG_SIZE);
|
||||
if (riff_size > MAX_CHUNK_PAYLOAD) goto Err;
|
||||
|
||||
// Note this padding is historical and differs from demux.c which does not
|
||||
// pad the file size.
|
||||
riff_size = SizeWithPadding(riff_size);
|
||||
// Make sure the whole RIFF header is available.
|
||||
if (riff_size < RIFF_HEADER_SIZE) goto Err;
|
||||
if (riff_size > size) goto Err;
|
||||
// There's no point in reading past the end of the RIFF chunk. Note riff_size
|
||||
// includes CHUNK_HEADER_SIZE after SizeWithPadding().
|
||||
if (size > riff_size) {
|
||||
size = riff_size;
|
||||
}
|
||||
|
||||
end = data + size;
|
||||
data += RIFF_HEADER_SIZE;
|
||||
size -= RIFF_HEADER_SIZE;
|
||||
|
||||
wpi = (WebPMuxImage*)WebPSafeMalloc(1ULL, sizeof(*wpi));
|
||||
if (wpi == NULL) goto Err;
|
||||
MuxImageInit(wpi);
|
||||
|
||||
// Loop over chunks.
|
||||
while (data != end) {
|
||||
size_t data_size;
|
||||
WebPChunkId id;
|
||||
if (ChunkVerifyAndAssign(&chunk, data, size, riff_size, copy_data) !=
|
||||
WEBP_MUX_OK) {
|
||||
goto Err;
|
||||
}
|
||||
data_size = ChunkDiskSize(&chunk);
|
||||
id = ChunkGetIdFromTag(chunk.tag);
|
||||
switch (id) {
|
||||
case WEBP_CHUNK_ALPHA:
|
||||
if (wpi->alpha != NULL) goto Err; // Consecutive ALPH chunks.
|
||||
if (ChunkSetHead(&chunk, &wpi->alpha) != WEBP_MUX_OK) goto Err;
|
||||
wpi->is_partial = 1; // Waiting for a VP8 chunk.
|
||||
break;
|
||||
case WEBP_CHUNK_IMAGE:
|
||||
if (ChunkSetHead(&chunk, &wpi->img) != WEBP_MUX_OK) goto Err;
|
||||
if (!MuxImageFinalize(wpi)) goto Err;
|
||||
wpi->is_partial = 0; // wpi is completely filled.
|
||||
PushImage:
|
||||
// Add this to mux->images list.
|
||||
if (MuxImagePush(wpi, &mux->images) != WEBP_MUX_OK) goto Err;
|
||||
MuxImageInit(wpi); // Reset for reading next image.
|
||||
break;
|
||||
case WEBP_CHUNK_ANMF:
|
||||
if (wpi->is_partial) goto Err; // Previous wpi is still incomplete.
|
||||
if (!MuxImageParse(&chunk, copy_data, wpi)) goto Err;
|
||||
ChunkRelease(&chunk);
|
||||
goto PushImage;
|
||||
default: // A non-image chunk.
|
||||
if (wpi->is_partial) {
|
||||
goto Err; // Encountered a non-image chunk before
|
||||
// getting all chunks of an image.
|
||||
}
|
||||
if (chunk_list_ends[id] == NULL) {
|
||||
chunk_list_ends[id] =
|
||||
MuxGetChunkListFromId(mux, id); // List to add this chunk.
|
||||
}
|
||||
if (ChunkAppend(&chunk, &chunk_list_ends[id]) != WEBP_MUX_OK) goto Err;
|
||||
if (id == WEBP_CHUNK_VP8X) { // grab global specs
|
||||
if (data_size < CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE) goto Err;
|
||||
mux->canvas_width = GetLE24(data + 12) + 1;
|
||||
mux->canvas_height = GetLE24(data + 15) + 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
data += data_size;
|
||||
size -= data_size;
|
||||
ChunkInit(&chunk);
|
||||
}
|
||||
|
||||
// Incomplete image.
|
||||
if (wpi->is_partial) goto Err;
|
||||
|
||||
// Validate mux if complete.
|
||||
if (MuxValidate(mux) != WEBP_MUX_OK) goto Err;
|
||||
|
||||
MuxImageDelete(wpi);
|
||||
return mux; // All OK;
|
||||
|
||||
Err: // Something bad happened.
|
||||
ChunkRelease(&chunk);
|
||||
MuxImageDelete(wpi);
|
||||
WebPMuxDelete(mux);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Get API(s).
|
||||
|
||||
// Validates that the given mux has a single image.
|
||||
static WebPMuxError ValidateForSingleImage(const WebPMux* const mux) {
|
||||
const int num_images = MuxImageCount(mux->images, WEBP_CHUNK_IMAGE);
|
||||
const int num_frames = MuxImageCount(mux->images, WEBP_CHUNK_ANMF);
|
||||
|
||||
if (num_images == 0) {
|
||||
// No images in mux.
|
||||
return WEBP_MUX_NOT_FOUND;
|
||||
} else if (num_images == 1 && num_frames == 0) {
|
||||
// Valid case (single image).
|
||||
return WEBP_MUX_OK;
|
||||
} else {
|
||||
// Frame case OR an invalid mux.
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the canvas width, height and flags after validating that VP8X/VP8/VP8L
|
||||
// chunk and canvas size are valid.
|
||||
static WebPMuxError MuxGetCanvasInfo(const WebPMux* const mux, int* width,
|
||||
int* height, uint32_t* flags) {
|
||||
int w, h;
|
||||
uint32_t f = 0;
|
||||
WebPData data;
|
||||
assert(mux != NULL);
|
||||
|
||||
// Check if VP8X chunk is present.
|
||||
if (MuxGet(mux, IDX_VP8X, 1, &data) == WEBP_MUX_OK) {
|
||||
if (data.size < VP8X_CHUNK_SIZE) return WEBP_MUX_BAD_DATA;
|
||||
f = GetLE32(data.bytes + 0);
|
||||
w = GetLE24(data.bytes + 4) + 1;
|
||||
h = GetLE24(data.bytes + 7) + 1;
|
||||
} else {
|
||||
const WebPMuxImage* const wpi = mux->images;
|
||||
// Grab user-forced canvas size as default.
|
||||
w = mux->canvas_width;
|
||||
h = mux->canvas_height;
|
||||
if (w == 0 && h == 0 && ValidateForSingleImage(mux) == WEBP_MUX_OK) {
|
||||
// single image and not forced canvas size => use dimension of first frame
|
||||
assert(wpi != NULL);
|
||||
w = wpi->width;
|
||||
h = wpi->height;
|
||||
}
|
||||
if (wpi != NULL) {
|
||||
if (wpi->has_alpha) f |= ALPHA_FLAG;
|
||||
}
|
||||
}
|
||||
if (w * (uint64_t)h >= MAX_IMAGE_AREA) return WEBP_MUX_BAD_DATA;
|
||||
|
||||
if (width != NULL) *width = w;
|
||||
if (height != NULL) *height = h;
|
||||
if (flags != NULL) *flags = f;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxGetCanvasSize(const WebPMux* mux, int* width, int* height) {
|
||||
if (mux == NULL || width == NULL || height == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
return MuxGetCanvasInfo(mux, width, height, NULL);
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxGetFeatures(const WebPMux* mux, uint32_t* flags) {
|
||||
if (mux == NULL || flags == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
return MuxGetCanvasInfo(mux, NULL, NULL, flags);
|
||||
}
|
||||
|
||||
static uint8_t* EmitVP8XChunk(uint8_t* const dst, int width, int height,
|
||||
uint32_t flags) {
|
||||
const size_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
|
||||
assert(width >= 1 && height >= 1);
|
||||
assert(width <= MAX_CANVAS_SIZE && height <= MAX_CANVAS_SIZE);
|
||||
assert(width * (uint64_t)height < MAX_IMAGE_AREA);
|
||||
PutLE32(dst, MKFOURCC('V', 'P', '8', 'X'));
|
||||
PutLE32(dst + TAG_SIZE, VP8X_CHUNK_SIZE);
|
||||
PutLE32(dst + CHUNK_HEADER_SIZE, flags);
|
||||
PutLE24(dst + CHUNK_HEADER_SIZE + 4, width - 1);
|
||||
PutLE24(dst + CHUNK_HEADER_SIZE + 7, height - 1);
|
||||
return dst + vp8x_size;
|
||||
}
|
||||
|
||||
// Assemble a single image WebP bitstream from 'wpi'.
|
||||
static WebPMuxError SynthesizeBitstream(const WebPMuxImage* const wpi,
|
||||
WebPData* const bitstream) {
|
||||
uint8_t* dst;
|
||||
|
||||
// Allocate data.
|
||||
const int need_vp8x = (wpi->alpha != NULL);
|
||||
const size_t vp8x_size = need_vp8x ? CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE : 0;
|
||||
const size_t alpha_size = need_vp8x ? ChunkDiskSize(wpi->alpha) : 0;
|
||||
// Note: No need to output ANMF chunk for a single image.
|
||||
const size_t size =
|
||||
RIFF_HEADER_SIZE + vp8x_size + alpha_size + ChunkDiskSize(wpi->img);
|
||||
uint8_t* const data = (uint8_t*)WebPSafeMalloc(1ULL, size);
|
||||
if (data == NULL) return WEBP_MUX_MEMORY_ERROR;
|
||||
|
||||
// There should be at most one alpha chunk and exactly one img chunk.
|
||||
assert(wpi->alpha == NULL || wpi->alpha->next == NULL);
|
||||
assert(wpi->img != NULL && wpi->img->next == NULL);
|
||||
|
||||
// Main RIFF header.
|
||||
dst = MuxEmitRiffHeader(data, size);
|
||||
|
||||
if (need_vp8x) {
|
||||
dst = EmitVP8XChunk(dst, wpi->width, wpi->height, ALPHA_FLAG); // VP8X.
|
||||
dst = ChunkListEmit(wpi->alpha, dst); // ALPH.
|
||||
}
|
||||
|
||||
// Bitstream.
|
||||
dst = ChunkListEmit(wpi->img, dst);
|
||||
assert(dst == data + size);
|
||||
|
||||
// Output.
|
||||
bitstream->bytes = data;
|
||||
bitstream->size = size;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxGetChunk(const WebPMux* mux, const char fourcc[4],
|
||||
WebPData* chunk_data) {
|
||||
CHUNK_INDEX idx;
|
||||
if (mux == NULL || fourcc == NULL || chunk_data == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
idx = ChunkGetIndexFromFourCC(fourcc);
|
||||
assert(idx != IDX_LAST_CHUNK);
|
||||
if (IsWPI(kChunks[idx].id)) { // An image chunk.
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
} else if (idx != IDX_UNKNOWN) { // A known chunk type.
|
||||
return MuxGet(mux, idx, 1, chunk_data);
|
||||
} else { // An unknown chunk type.
|
||||
const WebPChunk* const chunk =
|
||||
ChunkSearchList(mux->unknown, 1, ChunkGetTagFromFourCC(fourcc));
|
||||
if (chunk == NULL) return WEBP_MUX_NOT_FOUND;
|
||||
*chunk_data = chunk->data;
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
}
|
||||
|
||||
static WebPMuxError MuxGetImageInternal(const WebPMuxImage* const wpi,
|
||||
WebPMuxFrameInfo* const info) {
|
||||
// Set some defaults for unrelated fields.
|
||||
info->x_offset = 0;
|
||||
info->y_offset = 0;
|
||||
info->duration = 1;
|
||||
info->dispose_method = WEBP_MUX_DISPOSE_NONE;
|
||||
info->blend_method = WEBP_MUX_BLEND;
|
||||
// Extract data for related fields.
|
||||
info->id = ChunkGetIdFromTag(wpi->img->tag);
|
||||
return SynthesizeBitstream(wpi, &info->bitstream);
|
||||
}
|
||||
|
||||
static WebPMuxError MuxGetFrameInternal(const WebPMuxImage* const wpi,
|
||||
WebPMuxFrameInfo* const frame) {
|
||||
const int is_frame = (wpi->header->tag == kChunks[IDX_ANMF].tag);
|
||||
const WebPData* frame_data;
|
||||
if (!is_frame) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
assert(wpi->header != NULL); // Already checked by WebPMuxGetFrame().
|
||||
// Get frame chunk.
|
||||
frame_data = &wpi->header->data;
|
||||
if (frame_data->size < kChunks[IDX_ANMF].size) return WEBP_MUX_BAD_DATA;
|
||||
// Extract info.
|
||||
frame->x_offset = 2 * GetLE24(frame_data->bytes + 0);
|
||||
frame->y_offset = 2 * GetLE24(frame_data->bytes + 3);
|
||||
{
|
||||
const uint8_t bits = frame_data->bytes[15];
|
||||
frame->duration = GetLE24(frame_data->bytes + 12);
|
||||
frame->dispose_method =
|
||||
(bits & 1) ? WEBP_MUX_DISPOSE_BACKGROUND : WEBP_MUX_DISPOSE_NONE;
|
||||
frame->blend_method = (bits & 2) ? WEBP_MUX_NO_BLEND : WEBP_MUX_BLEND;
|
||||
}
|
||||
frame->id = ChunkGetIdFromTag(wpi->header->tag);
|
||||
return SynthesizeBitstream(wpi, &frame->bitstream);
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxGetFrame(const WebPMux* mux, uint32_t nth,
|
||||
WebPMuxFrameInfo* frame) {
|
||||
WebPMuxError err;
|
||||
WebPMuxImage* wpi;
|
||||
|
||||
if (mux == NULL || frame == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
// Get the nth WebPMuxImage.
|
||||
err = MuxImageGetNth((const WebPMuxImage**)&mux->images, nth, &wpi);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
|
||||
// Get frame info.
|
||||
if (wpi->header == NULL) {
|
||||
return MuxGetImageInternal(wpi, frame);
|
||||
} else {
|
||||
return MuxGetFrameInternal(wpi, frame);
|
||||
}
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxGetAnimationParams(const WebPMux* mux,
|
||||
WebPMuxAnimParams* params) {
|
||||
WebPData anim;
|
||||
WebPMuxError err;
|
||||
|
||||
if (mux == NULL || params == NULL) return WEBP_MUX_INVALID_ARGUMENT;
|
||||
|
||||
err = MuxGet(mux, IDX_ANIM, 1, &anim);
|
||||
if (err != WEBP_MUX_OK) return err;
|
||||
if (anim.size < kChunks[WEBP_CHUNK_ANIM].size) return WEBP_MUX_BAD_DATA;
|
||||
params->bgcolor = GetLE32(anim.bytes);
|
||||
params->loop_count = GetLE16(anim.bytes + 4);
|
||||
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
// Get chunk index from chunk id. Returns IDX_NIL if not found.
|
||||
static CHUNK_INDEX ChunkGetIndexFromId(WebPChunkId id) {
|
||||
int i;
|
||||
for (i = 0; kChunks[i].id != WEBP_CHUNK_NIL; ++i) {
|
||||
if (id == kChunks[i].id) return (CHUNK_INDEX)i;
|
||||
}
|
||||
return IDX_NIL;
|
||||
}
|
||||
|
||||
// Count number of chunks matching 'tag' in the 'chunk_list'.
|
||||
// If tag == NIL_TAG, any tag will be matched.
|
||||
static int CountChunks(const WebPChunk* const chunk_list, uint32_t tag) {
|
||||
int count = 0;
|
||||
const WebPChunk* current;
|
||||
for (current = chunk_list; current != NULL; current = current->next) {
|
||||
if (tag == NIL_TAG || current->tag == tag) {
|
||||
count++; // Count chunks whose tags match.
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
WebPMuxError WebPMuxNumChunks(const WebPMux* mux, WebPChunkId id,
|
||||
int* num_elements) {
|
||||
if (mux == NULL || num_elements == NULL) {
|
||||
return WEBP_MUX_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (IsWPI(id)) {
|
||||
*num_elements = MuxImageCount(mux->images, id);
|
||||
} else {
|
||||
WebPChunk* const* chunk_list = MuxGetChunkListFromId(mux, id);
|
||||
const CHUNK_INDEX idx = ChunkGetIndexFromId(id);
|
||||
*num_elements = CountChunks(*chunk_list, kChunks[idx].tag);
|
||||
}
|
||||
|
||||
return WEBP_MUX_OK;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Reference in New Issue
Block a user