Add support for lossless in mux:

- Separate out 'CHUNK_INDEX' from 'TAG_ID' (this is to help with the
  situation where two different tags - "VP8 " and "VP8L" can have the
  same TAG_ID -> IMAGE_ID).
- Some internal methods now take 'CHUNK_INDEX' param instea of 'TAG_ID'
  as appropriate.
- Add kChunks[] entry for lossless.
- Rename WebPMuxImage.vp8_ --> WebPMuxImage.img_
- SetImage() and AddFrame/Tile() infer whether the bitstream is a
  lossless one based on LOSSLESS_MAGIC_BYTE. The correct tag is stored
  based on this.

Also, handle the case when GetVP8Info/GetVP8LInfo() fails.

Change-Id: I6b3bc9555cedb791b43f743b5a7770958864bb05
This commit is contained in:
Urvang Joshi 2012-05-21 13:40:35 +05:30
parent 831bd13168
commit 8d77dc29e1
5 changed files with 242 additions and 164 deletions

View File

@ -60,31 +60,31 @@ void WebPMuxDelete(WebPMux* const mux) {
// Helper method(s). // Helper method(s).
// Handy MACRO, makes MuxSet() very symmetric to MuxGet(). // Handy MACRO, makes MuxSet() very symmetric to MuxGet().
#define SWITCH_ID_LIST(ID, LIST) \ #define SWITCH_ID_LIST(INDEX, LIST) \
if (id == (ID)) { \ if (idx == (INDEX)) { \
err = ChunkAssignDataImageInfo(&chunk, data, size, \ err = ChunkAssignDataImageInfo(&chunk, data, size, \
image_info, \ image_info, \
copy_data, kChunks[(ID)].tag); \ copy_data, kChunks[(INDEX)].tag); \
if (err == WEBP_MUX_OK) { \ if (err == WEBP_MUX_OK) { \
err = ChunkSetNth(&chunk, (LIST), nth); \ err = ChunkSetNth(&chunk, (LIST), nth); \
} \ } \
return err; \ return err; \
} }
static WebPMuxError MuxSet(WebPMux* const mux, TAG_ID id, uint32_t nth, static WebPMuxError MuxSet(WebPMux* const mux, CHUNK_INDEX idx, uint32_t nth,
const uint8_t* data, size_t size, const uint8_t* data, size_t size,
WebPImageInfo* image_info, int copy_data) { WebPImageInfo* image_info, int copy_data) {
WebPChunk chunk; WebPChunk chunk;
WebPMuxError err = WEBP_MUX_NOT_FOUND; WebPMuxError err = WEBP_MUX_NOT_FOUND;
assert(mux != NULL); assert(mux != NULL);
assert(!IsWPI(id)); assert(!IsWPI(kChunks[idx].id));
ChunkInit(&chunk); ChunkInit(&chunk);
SWITCH_ID_LIST(VP8X_ID, &mux->vp8x_); SWITCH_ID_LIST(IDX_VP8X, &mux->vp8x_);
SWITCH_ID_LIST(ICCP_ID, &mux->iccp_); SWITCH_ID_LIST(IDX_ICCP, &mux->iccp_);
SWITCH_ID_LIST(LOOP_ID, &mux->loop_); SWITCH_ID_LIST(IDX_LOOP, &mux->loop_);
SWITCH_ID_LIST(META_ID, &mux->meta_); SWITCH_ID_LIST(IDX_META, &mux->meta_);
if (id == UNKNOWN_ID && size > TAG_SIZE) { if (idx == IDX_UNKNOWN && size > TAG_SIZE) {
// For raw-data unknown chunk, the first four bytes should be the tag to be // For raw-data unknown chunk, the first four bytes should be the tag to be
// used for the chunk. // used for the chunk.
err = ChunkAssignDataImageInfo(&chunk, data + TAG_SIZE, size - TAG_SIZE, err = ChunkAssignDataImageInfo(&chunk, data + TAG_SIZE, size - TAG_SIZE,
@ -99,12 +99,12 @@ static WebPMuxError MuxSet(WebPMux* const mux, TAG_ID id, uint32_t nth,
static WebPMuxError MuxAddChunk(WebPMux* const mux, uint32_t nth, uint32_t tag, static WebPMuxError MuxAddChunk(WebPMux* const mux, uint32_t nth, uint32_t tag,
const uint8_t* data, size_t size, const uint8_t* data, size_t size,
WebPImageInfo* image_info, int copy_data) { WebPImageInfo* image_info, int copy_data) {
const TAG_ID id = ChunkGetIdFromTag(tag); const CHUNK_INDEX idx = ChunkGetIndexFromTag(tag);
assert(mux != NULL); assert(mux != NULL);
assert(size <= MAX_CHUNK_PAYLOAD); assert(size <= MAX_CHUNK_PAYLOAD);
if (id == NIL_ID) return WEBP_MUX_INVALID_PARAMETER; if (idx == IDX_NIL) return WEBP_MUX_INVALID_PARAMETER;
return MuxSet(mux, id, nth, data, size, image_info, copy_data); return MuxSet(mux, idx, nth, data, size, image_info, copy_data);
} }
static void InitImageInfo(WebPImageInfo* const image_info) { static void InitImageInfo(WebPImageInfo* const image_info) {
@ -113,17 +113,19 @@ static void InitImageInfo(WebPImageInfo* const image_info) {
} }
// Creates WebPImageInfo object and sets offsets, dimensions and duration. // Creates WebPImageInfo object and sets offsets, dimensions and duration.
// Dimensions calculated from passed VP8 image data. // Dimensions calculated from passed VP8/VP8L image data.
static WebPImageInfo* CreateImageInfo(uint32_t x_offset, uint32_t y_offset, static WebPImageInfo* CreateImageInfo(uint32_t x_offset, uint32_t y_offset,
uint32_t duration, uint32_t duration,
const uint8_t* data, size_t size) { const uint8_t* data, size_t size,
int is_lossless) {
int width; int width;
int height; int height;
WebPImageInfo* image_info = NULL; WebPImageInfo* image_info = NULL;
if (!VP8GetInfo(data, size, size, &width, &height)) { const int ok = is_lossless ?
return NULL; VP8LGetInfo(data, size, &width, &height) :
} VP8GetInfo(data, size, size, &width, &height);
if (!ok) return NULL;
image_info = (WebPImageInfo*)malloc(sizeof(WebPImageInfo)); image_info = (WebPImageInfo*)malloc(sizeof(WebPImageInfo));
if (image_info != NULL) { if (image_info != NULL) {
@ -146,7 +148,7 @@ static WebPMuxError CreateDataFromImageInfo(const WebPImageInfo* image_info,
assert(size); assert(size);
assert(image_info); assert(image_info);
*size = kChunks[is_frame ? FRAME_ID : TILE_ID].size; *size = kChunks[is_frame ? IDX_FRAME : IDX_TILE].size;
*data = (uint8_t*)malloc(*size); *data = (uint8_t*)malloc(*size);
if (*data == NULL) return WEBP_MUX_MEMORY_ERROR; if (*data == NULL) return WEBP_MUX_MEMORY_ERROR;
@ -162,13 +164,20 @@ static WebPMuxError CreateDataFromImageInfo(const WebPImageInfo* image_info,
return WEBP_MUX_OK; return WEBP_MUX_OK;
} }
static int IsLosslessData(const WebPData* const image) {
return (image->size_ >= 1 && image->bytes_[0] == LOSSLESS_MAGIC_BYTE);
}
// Outputs image data given data from a webp file (including RIFF header). // Outputs image data given data from a webp file (including RIFF header).
// Also outputs 'is_lossless' to be true if the given bitstream is lossless.
static WebPMuxError GetImageData(const uint8_t* data, size_t size, static WebPMuxError GetImageData(const uint8_t* data, size_t size,
WebPData* const image, WebPData* const alpha) { WebPData* const image, WebPData* const alpha,
int* const is_lossless) {
if (size < TAG_SIZE || memcmp(data, "RIFF", TAG_SIZE)) { if (size < TAG_SIZE || memcmp(data, "RIFF", TAG_SIZE)) {
// It is NOT webp file data. Return input data as is. // It is NOT webp file data. Return input data as is.
image->bytes_ = data; image->bytes_ = data;
image->size_ = size; image->size_ = size;
*is_lossless = IsLosslessData(image);
return WEBP_MUX_OK; return WEBP_MUX_OK;
} else { } else {
// It is webp file data. Extract image data from it. // It is webp file data. Extract image data from it.
@ -178,9 +187,9 @@ static WebPMuxError GetImageData(const uint8_t* data, size_t size,
if (mux == NULL || mux_state != WEBP_MUX_STATE_COMPLETE) { if (mux == NULL || mux_state != WEBP_MUX_STATE_COMPLETE) {
return WEBP_MUX_BAD_DATA; return WEBP_MUX_BAD_DATA;
} }
err = WebPMuxGetImage(mux, image, alpha); err = WebPMuxGetImage(mux, image, alpha);
WebPMuxDelete(mux); WebPMuxDelete(mux);
*is_lossless = IsLosslessData(image);
return err; return err;
} }
} }
@ -201,21 +210,22 @@ static WebPMuxError DeleteChunks(WebPChunk** chunk_list, uint32_t tag) {
} }
static WebPMuxError MuxDeleteAllNamedData(WebPMux* const mux, static WebPMuxError MuxDeleteAllNamedData(WebPMux* const mux,
const char* const tag) { const char* const name) {
const TAG_ID id = ChunkGetIdFromName(tag); const CHUNK_INDEX idx = ChunkGetIndexFromName(name);
const TAG_ID id = kChunks[idx].id;
WebPChunk** chunk_list; WebPChunk** chunk_list;
if (mux == NULL || tag == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (mux == NULL || name == NULL) return WEBP_MUX_INVALID_ARGUMENT;
if (IsWPI(id)) return WEBP_MUX_INVALID_ARGUMENT; if (IsWPI(id)) return WEBP_MUX_INVALID_ARGUMENT;
chunk_list = GetChunkListFromId(mux, id); chunk_list = GetChunkListFromId(mux, id);
if (chunk_list == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (chunk_list == NULL) return WEBP_MUX_INVALID_ARGUMENT;
return DeleteChunks(chunk_list, kChunks[id].tag); return DeleteChunks(chunk_list, kChunks[idx].tag);
} }
static WebPMuxError DeleteLoopCount(WebPMux* const mux) { static WebPMuxError DeleteLoopCount(WebPMux* const mux) {
return MuxDeleteAllNamedData(mux, kChunks[LOOP_ID].name); return MuxDeleteAllNamedData(mux, kChunks[IDX_LOOP].name);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -230,14 +240,18 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
WebPMuxImage wpi; WebPMuxImage wpi;
WebPData image; WebPData image;
const int has_alpha = (alpha_data != NULL && alpha_size != 0); const int has_alpha = (alpha_data != NULL && alpha_size != 0);
int is_lossless;
int image_tag;
if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) { if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
// If given data is for a whole webp file, extract only the VP8 data from it. // If given data is for a whole webp file,
err = GetImageData(data, size, &image, NULL); // extract only the VP8/VP8L data from it.
err = GetImageData(data, size, &image, NULL, &is_lossless);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
image_tag = is_lossless ? kChunks[IDX_VP8L].tag : kChunks[IDX_VP8].tag;
// Delete the existing images. // Delete the existing images.
MuxImageDeleteAll(&mux->images_); MuxImageDeleteAll(&mux->images_);
@ -247,7 +261,7 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
if (has_alpha) { // Add alpha chunk. if (has_alpha) { // Add alpha chunk.
ChunkInit(&chunk); ChunkInit(&chunk);
err = ChunkAssignDataImageInfo(&chunk, alpha_data, alpha_size, NULL, err = ChunkAssignDataImageInfo(&chunk, alpha_data, alpha_size, NULL,
copy_data, kChunks[ALPHA_ID].tag); copy_data, kChunks[IDX_ALPHA].tag);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ChunkSetNth(&chunk, &wpi.alpha_, 1); err = ChunkSetNth(&chunk, &wpi.alpha_, 1);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
@ -256,9 +270,9 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
// Add image chunk. // Add image chunk.
ChunkInit(&chunk); ChunkInit(&chunk);
err = ChunkAssignDataImageInfo(&chunk, image.bytes_, image.size_, NULL, err = ChunkAssignDataImageInfo(&chunk, image.bytes_, image.size_, NULL,
copy_data, kChunks[IMAGE_ID].tag); copy_data, image_tag);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ChunkSetNth(&chunk, &wpi.vp8_, 1); err = ChunkSetNth(&chunk, &wpi.img_, 1);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
// Add this image to mux. // Add this image to mux.
@ -279,7 +293,7 @@ WebPMuxError WebPMuxSetMetadata(WebPMux* const mux,
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
// Add the given metadata chunk. // Add the given metadata chunk.
return MuxSet(mux, META_ID, 1, data, size, NULL, copy_data); return MuxSet(mux, IDX_META, 1, data, size, NULL, copy_data);
} }
WebPMuxError WebPMuxSetColorProfile(WebPMux* const mux, WebPMuxError WebPMuxSetColorProfile(WebPMux* const mux,
@ -296,7 +310,7 @@ WebPMuxError WebPMuxSetColorProfile(WebPMux* const mux,
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
// Add the given ICCP chunk. // Add the given ICCP chunk.
return MuxSet(mux, ICCP_ID, 1, data, size, NULL, copy_data); return MuxSet(mux, IDX_ICCP, 1, data, size, NULL, copy_data);
} }
WebPMuxError WebPMuxSetLoopCount(WebPMux* const mux, uint32_t loop_count) { WebPMuxError WebPMuxSetLoopCount(WebPMux* const mux, uint32_t loop_count) {
@ -310,12 +324,12 @@ WebPMuxError WebPMuxSetLoopCount(WebPMux* const mux, uint32_t loop_count) {
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
// Add the given loop count. // Add the given loop count.
data = (uint8_t*)malloc(kChunks[LOOP_ID].size); data = (uint8_t*)malloc(kChunks[IDX_LOOP].size);
if (data == NULL) return WEBP_MUX_MEMORY_ERROR; if (data == NULL) return WEBP_MUX_MEMORY_ERROR;
PutLE32(data, loop_count); PutLE32(data, loop_count);
err = MuxAddChunk(mux, 1, kChunks[LOOP_ID].tag, data, err = MuxAddChunk(mux, 1, kChunks[IDX_LOOP].tag, data,
kChunks[LOOP_ID].size, NULL, 1); kChunks[IDX_LOOP].size, NULL, 1);
free(data); free(data);
return err; return err;
} }
@ -333,16 +347,20 @@ static WebPMuxError MuxAddFrameTileInternal(
WebPImageInfo* image_info = NULL; WebPImageInfo* image_info = NULL;
uint8_t* frame_tile_data = NULL; uint8_t* frame_tile_data = NULL;
size_t frame_tile_data_size = 0; size_t frame_tile_data_size = 0;
const int is_frame = (tag == kChunks[FRAME_ID].tag) ? 1 : 0; const int is_frame = (tag == kChunks[IDX_FRAME].tag) ? 1 : 0;
const int has_alpha = (alpha_data != NULL && alpha_size != 0); const int has_alpha = (alpha_data != NULL && alpha_size != 0);
int is_lossless;
int image_tag;
if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) { if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
// If given data is for a whole webp file, extract only the VP8 data from it. // If given data is for a whole webp file,
err = GetImageData(data, size, &image, NULL); // extract only the VP8/VP8L data from it.
err = GetImageData(data, size, &image, NULL, &is_lossless);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
image_tag = is_lossless ? kChunks[IDX_VP8L].tag : kChunks[IDX_VP8].tag;
ChunkInit(&chunk); ChunkInit(&chunk);
MuxImageInit(&wpi); MuxImageInit(&wpi);
@ -350,7 +368,7 @@ static WebPMuxError MuxAddFrameTileInternal(
if (has_alpha) { if (has_alpha) {
// Add alpha chunk. // Add alpha chunk.
err = ChunkAssignDataImageInfo(&chunk, alpha_data, alpha_size, NULL, err = ChunkAssignDataImageInfo(&chunk, alpha_data, alpha_size, NULL,
copy_data, kChunks[ALPHA_ID].tag); copy_data, kChunks[IDX_ALPHA].tag);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ChunkSetNth(&chunk, &wpi.alpha_, 1); err = ChunkSetNth(&chunk, &wpi.alpha_, 1);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
@ -359,7 +377,7 @@ static WebPMuxError MuxAddFrameTileInternal(
// Create image_info object. // Create image_info object.
image_info = CreateImageInfo(x_offset, y_offset, duration, image_info = CreateImageInfo(x_offset, y_offset, duration,
image.bytes_, image.size_); image.bytes_, image.size_, is_lossless);
if (image_info == NULL) { if (image_info == NULL) {
MuxImageRelease(&wpi); MuxImageRelease(&wpi);
return WEBP_MUX_MEMORY_ERROR; return WEBP_MUX_MEMORY_ERROR;
@ -367,15 +385,15 @@ static WebPMuxError MuxAddFrameTileInternal(
// Add image chunk. // Add image chunk.
err = ChunkAssignDataImageInfo(&chunk, image.bytes_, image.size_, image_info, err = ChunkAssignDataImageInfo(&chunk, image.bytes_, image.size_, image_info,
copy_data, kChunks[IMAGE_ID].tag); copy_data, image_tag);
if (err != WEBP_MUX_OK) goto Err; if (err != WEBP_MUX_OK) goto Err;
image_info = NULL; // Owned by 'chunk' now. image_info = NULL; // Owned by 'chunk' now.
err = ChunkSetNth(&chunk, &wpi.vp8_, 1); err = ChunkSetNth(&chunk, &wpi.img_, 1);
if (err != WEBP_MUX_OK) goto Err; if (err != WEBP_MUX_OK) goto Err;
ChunkInit(&chunk); // chunk owned by wpi.vp8_ now. ChunkInit(&chunk); // chunk owned by wpi.vp8_ now.
// Create frame/tile data from image_info. // Create frame/tile data from image_info.
err = CreateDataFromImageInfo(wpi.vp8_->image_info_, is_frame, err = CreateDataFromImageInfo(wpi.img_->image_info_, is_frame,
&frame_tile_data, &frame_tile_data_size); &frame_tile_data, &frame_tile_data_size);
if (err != WEBP_MUX_OK) goto Err; if (err != WEBP_MUX_OK) goto Err;
@ -413,7 +431,7 @@ WebPMuxError WebPMuxAddFrame(WebPMux* const mux, uint32_t nth,
uint32_t duration, int copy_data) { uint32_t duration, int copy_data) {
return MuxAddFrameTileInternal(mux, nth, data, size, alpha_data, alpha_size, return MuxAddFrameTileInternal(mux, nth, data, size, alpha_data, alpha_size,
x_offset, y_offset, duration, x_offset, y_offset, duration,
copy_data, kChunks[FRAME_ID].tag); copy_data, kChunks[IDX_FRAME].tag);
} }
WebPMuxError WebPMuxAddTile(WebPMux* const mux, uint32_t nth, WebPMuxError WebPMuxAddTile(WebPMux* const mux, uint32_t nth,
@ -423,7 +441,7 @@ WebPMuxError WebPMuxAddTile(WebPMux* const mux, uint32_t nth,
int copy_data) { int copy_data) {
return MuxAddFrameTileInternal(mux, nth, data, size, alpha_data, alpha_size, return MuxAddFrameTileInternal(mux, nth, data, size, alpha_data, alpha_size,
x_offset, y_offset, 1, x_offset, y_offset, 1,
copy_data, kChunks[TILE_ID].tag); copy_data, kChunks[IDX_TILE].tag);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -443,35 +461,56 @@ WebPMuxError WebPMuxDeleteImage(WebPMux* const mux) {
} }
WebPMuxError WebPMuxDeleteMetadata(WebPMux* const mux) { WebPMuxError WebPMuxDeleteMetadata(WebPMux* const mux) {
return MuxDeleteAllNamedData(mux, kChunks[META_ID].name); return MuxDeleteAllNamedData(mux, kChunks[IDX_META].name);
} }
WebPMuxError WebPMuxDeleteColorProfile(WebPMux* const mux) { WebPMuxError WebPMuxDeleteColorProfile(WebPMux* const mux) {
return MuxDeleteAllNamedData(mux, kChunks[ICCP_ID].name); return MuxDeleteAllNamedData(mux, kChunks[IDX_ICCP].name);
} }
static WebPMuxError DeleteFrameTileInternal(WebPMux* const mux, static WebPMuxError DeleteFrameTileInternal(WebPMux* const mux,
uint32_t nth, uint32_t nth,
const char* const tag) { const char* const name) {
const TAG_ID id = ChunkGetIdFromName(tag); const CHUNK_INDEX idx = ChunkGetIndexFromName(name);
const TAG_ID id = kChunks[idx].id;
if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (mux == NULL) return WEBP_MUX_INVALID_ARGUMENT;
assert(id == FRAME_ID || id == TILE_ID); assert(idx == IDX_FRAME || idx == IDX_TILE);
return MuxImageDeleteNth(&mux->images_, nth, id); return MuxImageDeleteNth(&mux->images_, nth, id);
} }
WebPMuxError WebPMuxDeleteFrame(WebPMux* const mux, uint32_t nth) { WebPMuxError WebPMuxDeleteFrame(WebPMux* const mux, uint32_t nth) {
return DeleteFrameTileInternal(mux, nth, kChunks[FRAME_ID].name); return DeleteFrameTileInternal(mux, nth, kChunks[IDX_FRAME].name);
} }
WebPMuxError WebPMuxDeleteTile(WebPMux* const mux, uint32_t nth) { WebPMuxError WebPMuxDeleteTile(WebPMux* const mux, uint32_t nth) {
return DeleteFrameTileInternal(mux, nth, kChunks[TILE_ID].name); return DeleteFrameTileInternal(mux, nth, kChunks[IDX_TILE].name);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Assembly of the WebP RIFF file. // Assembly of the WebP RIFF file.
static WebPMuxError GetImageCanvasHeightWidth( static WebPMuxError GetImageWidthHeight(const WebPChunk* const image_chunk,
int* const width, int* const height) {
const uint32_t tag = image_chunk->tag_;
int w, h;
int ok;
assert(image_chunk != NULL);
assert(tag == kChunks[IDX_VP8].tag || tag == kChunks[IDX_VP8L].tag);
ok = (tag == kChunks[IDX_VP8].tag) ?
VP8GetInfo(image_chunk->data_, image_chunk->payload_size_,
image_chunk->payload_size_, &w, &h) :
VP8LGetInfo(image_chunk->data_, image_chunk->payload_size_, &w, &h);
if (ok) {
*width = w;
*height = h;
return WEBP_MUX_OK;
} else {
return WEBP_MUX_BAD_DATA;
}
}
static WebPMuxError GetImageCanvasWidthHeight(
const WebPMux* const mux, const WebPMux* const mux,
uint32_t flags, uint32_t* width, uint32_t* height) { uint32_t flags, uint32_t* width, uint32_t* height) {
WebPMuxImage* wpi = NULL; WebPMuxImage* wpi = NULL;
@ -480,7 +519,7 @@ static WebPMuxError GetImageCanvasHeightWidth(
wpi = mux->images_; wpi = mux->images_;
assert(wpi != NULL); assert(wpi != NULL);
assert(wpi->vp8_ != NULL); assert(wpi->img_ != NULL);
if (wpi->next_) { if (wpi->next_) {
uint32_t max_x = 0; uint32_t max_x = 0;
@ -488,7 +527,7 @@ static WebPMuxError GetImageCanvasHeightWidth(
uint64_t image_area = 0; uint64_t image_area = 0;
// Aggregate the bounding box for animation frames & tiled images. // Aggregate the bounding box for animation frames & tiled images.
for (; wpi != NULL; wpi = wpi->next_) { for (; wpi != NULL; wpi = wpi->next_) {
const WebPImageInfo* image_info = wpi->vp8_->image_info_; const WebPImageInfo* image_info = wpi->img_->image_info_;
if (image_info != NULL) { if (image_info != NULL) {
const uint32_t max_x_pos = image_info->x_offset_ + image_info->width_; const uint32_t max_x_pos = image_info->x_offset_ + image_info->width_;
@ -516,15 +555,13 @@ static WebPMuxError GetImageCanvasHeightWidth(
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
} else { } else {
// For a single image, extract the width & height from VP8 image-data. // For a single image, extract the width & height from VP8/VP8L image-data.
int w, h; int w, h;
const WebPChunk* const image_chunk = wpi->vp8_; const WebPChunk* const image_chunk = wpi->img_;
assert(image_chunk != NULL); const WebPMuxError err = GetImageWidthHeight(image_chunk, &w, &h);
if (VP8GetInfo(image_chunk->data_, image_chunk->payload_size_, if (err != WEBP_MUX_OK) return err;
image_chunk->payload_size_, &w, &h)) { *width = w;
*width = w; *height = h;
*height = h;
}
} }
return WEBP_MUX_OK; return WEBP_MUX_OK;
} }
@ -545,13 +582,13 @@ static WebPMuxError CreateVP8XChunk(WebPMux* const mux) {
assert(mux != NULL); assert(mux != NULL);
images = mux->images_; // First image. images = mux->images_; // First image.
if (images == NULL || images->vp8_ == NULL || images->vp8_->data_ == NULL) { if (images == NULL || images->img_ == NULL || images->img_->data_ == NULL) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
// If VP8X chunk(s) is(are) already present, remove them (and later add new // If VP8X chunk(s) is(are) already present, remove them (and later add new
// VP8X chunk with updated flags). // VP8X chunk with updated flags).
err = MuxDeleteAllNamedData(mux, kChunks[VP8X_ID].name); err = MuxDeleteAllNamedData(mux, kChunks[IDX_VP8X].name);
if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err; if (err != WEBP_MUX_OK && err != WEBP_MUX_NOT_FOUND) return err;
// Set flags. // Set flags.
@ -564,10 +601,10 @@ static WebPMuxError CreateVP8XChunk(WebPMux* const mux) {
} }
if (images->header_ != NULL) { if (images->header_ != NULL) {
if (images->header_->tag_ == kChunks[TILE_ID].tag) { if (images->header_->tag_ == kChunks[IDX_TILE].tag) {
// This is a tiled image. // This is a tiled image.
flags |= TILE_FLAG; flags |= TILE_FLAG;
} else if (images->header_->tag_ == kChunks[FRAME_ID].tag) { } else if (images->header_->tag_ == kChunks[IDX_FRAME].tag) {
// This is an image with animation. // This is an image with animation.
flags |= ANIMATION_FLAG; flags |= ANIMATION_FLAG;
} }
@ -583,14 +620,14 @@ static WebPMuxError CreateVP8XChunk(WebPMux* const mux) {
return WEBP_MUX_OK; return WEBP_MUX_OK;
} }
err = GetImageCanvasHeightWidth(mux, flags, &width, &height); err = GetImageCanvasWidthHeight(mux, flags, &width, &height);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
PutLE32(data + 0, flags); // VP8X chunk flags. PutLE32(data + 0, flags); // VP8X chunk flags.
PutLE32(data + 4, width); // canvas width. PutLE32(data + 4, width); // canvas width.
PutLE32(data + 8, height); // canvas height. PutLE32(data + 8, height); // canvas height.
err = MuxAddChunk(mux, 1, kChunks[VP8X_ID].tag, data, data_size, err = MuxAddChunk(mux, 1, kChunks[IDX_VP8X].tag, data, data_size,
NULL, 1); NULL, 1);
return err; return err;
} }
@ -612,11 +649,11 @@ WebPMuxError WebPMuxAssemble(WebPMux* const mux,
*output_size = 0; *output_size = 0;
// Remove LOOP chunk if unnecessary. // Remove LOOP chunk if unnecessary.
err = WebPMuxNumNamedElements(mux, kChunks[LOOP_ID].name, err = WebPMuxNumNamedElements(mux, kChunks[IDX_LOOP].name,
&num_loop_chunks); &num_loop_chunks);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
if (num_loop_chunks >= 1) { if (num_loop_chunks >= 1) {
err = WebPMuxNumNamedElements(mux, kChunks[FRAME_ID].name, err = WebPMuxNumNamedElements(mux, kChunks[IDX_FRAME].name,
&num_frames); &num_frames);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
if (num_frames == 0) { if (num_frames == 0) {

View File

@ -14,6 +14,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "../dec/vp8i.h" #include "../dec/vp8i.h"
#include "../dec/vp8li.h"
#include "../dec/webpi.h" // For chunk-size constants. #include "../dec/webpi.h" // For chunk-size constants.
#include "../webp/mux.h" #include "../webp/mux.h"
@ -47,12 +48,12 @@ struct WebPChunk {
}; };
// MuxImage object. Store a full webp image (including frame/tile chunk, alpha // MuxImage object. Store a full webp image (including frame/tile chunk, alpha
// chunk and VP8 chunk), // chunk and VP8/VP8L chunk),
typedef struct WebPMuxImage WebPMuxImage; typedef struct WebPMuxImage WebPMuxImage;
struct WebPMuxImage { struct WebPMuxImage {
WebPChunk* header_; // Corresponds to FRAME_ID/TILE_ID. WebPChunk* header_; // Corresponds to FRAME_ID/TILE_ID.
WebPChunk* alpha_; // Corresponds to ALPHA_ID. WebPChunk* alpha_; // Corresponds to ALPHA_ID.
WebPChunk* vp8_; // Corresponds to IMAGE_ID. WebPChunk* img_; // Corresponds to IMAGE_ID.
int is_partial_; // True if only some of the chunks are filled. int is_partial_; // True if only some of the chunks are filled.
WebPMuxImage* next_; WebPMuxImage* next_;
}; };
@ -69,8 +70,9 @@ struct WebPMux {
WebPChunk* unknown_; WebPChunk* unknown_;
}; };
// TAG_ID enum: used to assign an ID to each type of chunk.
typedef enum { typedef enum {
VP8X_ID = 0, VP8X_ID,
ICCP_ID, ICCP_ID,
LOOP_ID, LOOP_ID,
FRAME_ID, FRAME_ID,
@ -79,11 +81,29 @@ typedef enum {
IMAGE_ID, IMAGE_ID,
META_ID, META_ID,
UNKNOWN_ID, UNKNOWN_ID,
NIL_ID
NIL_ID,
LAST_TAG_ID
} TAG_ID; } TAG_ID;
// CHUNK_INDEX enum: used for indexing within 'kChunks' (defined below) only.
// Note: the reason for having two enums ('TAG_ID' and 'CHUNK_INDEX') is to
// allow two different chunks to have the same id (e.g. TAG_ID 'IMAGE_ID' can
// correspond to CHUNK_INDEX 'IDX_VP8' or 'IDX_VP8L').
typedef enum {
IDX_VP8X = 0,
IDX_ICCP,
IDX_LOOP,
IDX_FRAME,
IDX_TILE,
IDX_ALPHA,
IDX_VP8,
IDX_VP8L,
IDX_META,
IDX_UNKNOWN,
IDX_NIL,
IDX_LAST_CHUNK
} CHUNK_INDEX;
// Maximum chunk payload (data) size such that adding the header and padding // Maximum chunk payload (data) size such that adding the header and padding
// won't overflow an uint32. // won't overflow an uint32.
#define MAX_CHUNK_PAYLOAD (~0U - CHUNK_HEADER_SIZE - 1) #define MAX_CHUNK_PAYLOAD (~0U - CHUNK_HEADER_SIZE - 1)
@ -101,7 +121,7 @@ typedef struct {
uint32_t size; uint32_t size;
} ChunkInfo; } ChunkInfo;
extern const ChunkInfo kChunks[LAST_TAG_ID]; extern const ChunkInfo kChunks[IDX_LAST_CHUNK];
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Helper functions. // Helper functions.
@ -130,10 +150,16 @@ static WEBP_INLINE size_t SizeWithPadding(size_t chunk_size) {
// Initialize. // Initialize.
void ChunkInit(WebPChunk* const chunk); void ChunkInit(WebPChunk* const chunk);
// Get chunk id from chunk name. // Get chunk index from chunk name.
TAG_ID ChunkGetIdFromName(const char* const what); // Returns IDX_NIL if chunk name is NULL or not found.
CHUNK_INDEX ChunkGetIndexFromName(const char* const name);
// Get chunk index from chunk tag.
// Returns IDX_NIL if not found.
CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag);
// Get chunk id from chunk tag. // Get chunk id from chunk tag.
// Returns NIL_ID if not found.
TAG_ID ChunkGetIdFromTag(uint32_t tag); TAG_ID ChunkGetIdFromTag(uint32_t tag);
// Search for nth chunk with given 'tag' in the chunk list. // Search for nth chunk with given 'tag' in the chunk list.
@ -185,8 +211,7 @@ WebPMuxImage* MuxImageDelete(WebPMuxImage* const wpi);
// Delete all images in 'wpi_list'. // Delete all images in 'wpi_list'.
void MuxImageDeleteAll(WebPMuxImage** const wpi_list); void MuxImageDeleteAll(WebPMuxImage** const wpi_list);
// Count number of images matching 'tag' in the 'wpi_list'. // Count number of images matching the given tag id in the 'wpi_list'.
// If tag == NIL_TAG, any tag will be matched.
int MuxImageCount(WebPMuxImage* const wpi_list, TAG_ID id); int MuxImageCount(WebPMuxImage* const wpi_list, TAG_ID id);
// Check if given ID corresponds to an image related chunk. // Check if given ID corresponds to an image related chunk.
@ -206,9 +231,9 @@ static WEBP_INLINE WebPChunk** MuxImageGetListFromId(WebPMuxImage* wpi,
assert(wpi != NULL); assert(wpi != NULL);
switch (id) { switch (id) {
case FRAME_ID: case FRAME_ID:
case TILE_ID: return &wpi->header_; case TILE_ID: return &wpi->header_;
case ALPHA_ID: return &wpi->alpha_; case ALPHA_ID: return &wpi->alpha_;
case IMAGE_ID: return &wpi->vp8_; case IMAGE_ID: return &wpi->img_;
default: return NULL; default: return NULL;
} }
} }
@ -218,11 +243,11 @@ static WEBP_INLINE WebPChunk** MuxImageGetListFromId(WebPMuxImage* wpi,
WebPMuxError MuxImageSetNth(const WebPMuxImage* wpi, WebPMuxImage** wpi_list, WebPMuxError MuxImageSetNth(const WebPMuxImage* wpi, WebPMuxImage** wpi_list,
uint32_t nth); uint32_t nth);
// Delete nth image in the image list with given tag. // Delete nth image in the image list with given tag id.
WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth, WebPMuxError MuxImageDeleteNth(WebPMuxImage** wpi_list, uint32_t nth,
TAG_ID id); TAG_ID id);
// Get nth image in the image list with given tag. // Get nth image in the image list with given tag id.
WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth, WebPMuxError MuxImageGetNth(const WebPMuxImage** wpi_list, uint32_t nth,
TAG_ID id, WebPMuxImage** wpi); TAG_ID id, WebPMuxImage** wpi);

View File

@ -27,6 +27,7 @@ const ChunkInfo kChunks[] = {
{"tile", mktag('T', 'I', 'L', 'E'), TILE_ID, TILE_CHUNK_SIZE}, {"tile", mktag('T', 'I', 'L', 'E'), TILE_ID, TILE_CHUNK_SIZE},
{"alpha", mktag('A', 'L', 'P', 'H'), ALPHA_ID, UNDEFINED_CHUNK_SIZE}, {"alpha", mktag('A', 'L', 'P', 'H'), ALPHA_ID, UNDEFINED_CHUNK_SIZE},
{"image", mktag('V', 'P', '8', ' '), IMAGE_ID, UNDEFINED_CHUNK_SIZE}, {"image", mktag('V', 'P', '8', ' '), IMAGE_ID, UNDEFINED_CHUNK_SIZE},
{"image", mktag('V', 'P', '8', 'L'), IMAGE_ID, UNDEFINED_CHUNK_SIZE},
{"meta", mktag('M', 'E', 'T', 'A'), META_ID, UNDEFINED_CHUNK_SIZE}, {"meta", mktag('M', 'E', 'T', 'A'), META_ID, UNDEFINED_CHUNK_SIZE},
{"unknown", mktag('U', 'N', 'K', 'N'), UNKNOWN_ID, UNDEFINED_CHUNK_SIZE}, {"unknown", mktag('U', 'N', 'K', 'N'), UNKNOWN_ID, UNDEFINED_CHUNK_SIZE},
@ -57,19 +58,27 @@ WebPChunk* ChunkRelease(WebPChunk* const chunk) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Chunk misc methods. // Chunk misc methods.
TAG_ID ChunkGetIdFromName(const char* const what) { CHUNK_INDEX ChunkGetIndexFromName(const char* const name) {
int i; int i;
if (what == NULL) return -1; if (name == NULL) return IDX_NIL;
for (i = 0; kChunks[i].name != NULL; ++i) { for (i = 0; kChunks[i].name != NULL; ++i) {
if (!strcmp(what, kChunks[i].name)) return i; if (!strcmp(name, kChunks[i].name)) return i;
} }
return NIL_ID; return IDX_NIL;
}
CHUNK_INDEX ChunkGetIndexFromTag(uint32_t tag) {
int i;
for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
if (tag == kChunks[i].tag) return i;
}
return IDX_NIL;
} }
TAG_ID ChunkGetIdFromTag(uint32_t tag) { TAG_ID ChunkGetIdFromTag(uint32_t tag) {
int i; int i;
for (i = 0; kChunks[i].tag != NIL_TAG; ++i) { for (i = 0; kChunks[i].tag != NIL_TAG; ++i) {
if (tag == kChunks[i].tag) return i; if (tag == kChunks[i].tag) return kChunks[i].id;
} }
return NIL_ID; return NIL_ID;
} }
@ -127,7 +136,7 @@ WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk,
WebPImageInfo* image_info, WebPImageInfo* image_info,
int copy_data, uint32_t tag) { int copy_data, uint32_t tag) {
// For internally allocated chunks, always copy data & make it owner of data. // For internally allocated chunks, always copy data & make it owner of data.
if (tag == kChunks[VP8X_ID].tag || tag == kChunks[LOOP_ID].tag) { if (tag == kChunks[IDX_VP8X].tag || tag == kChunks[IDX_LOOP].tag) {
copy_data = 1; copy_data = 1;
} }
@ -155,7 +164,7 @@ WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk,
} }
} }
if (tag == kChunks[IMAGE_ID].tag) { if (tag == kChunks[IDX_VP8].tag || tag == kChunks[IDX_VP8L].tag) {
chunk->image_info_ = image_info; chunk->image_info_ = image_info;
} }
@ -234,7 +243,7 @@ WebPMuxImage* MuxImageRelease(WebPMuxImage* const wpi) {
if (wpi == NULL) return NULL; if (wpi == NULL) return NULL;
ChunkDelete(wpi->header_); ChunkDelete(wpi->header_);
ChunkDelete(wpi->alpha_); ChunkDelete(wpi->alpha_);
ChunkDelete(wpi->vp8_); ChunkDelete(wpi->img_);
next = wpi->next_; next = wpi->next_;
MuxImageInit(wpi); MuxImageInit(wpi);
@ -249,8 +258,9 @@ int MuxImageCount(WebPMuxImage* const wpi_list, TAG_ID id) {
WebPMuxImage* current; WebPMuxImage* current;
for (current = wpi_list; current != NULL; current = current->next_) { for (current = wpi_list; current != NULL; current = current->next_) {
const WebPChunk* const wpi_chunk = *MuxImageGetListFromId(current, id); const WebPChunk* const wpi_chunk = *MuxImageGetListFromId(current, id);
if (wpi_chunk != NULL && wpi_chunk->tag_ == kChunks[id].tag) { if (wpi_chunk != NULL) {
++count; const TAG_ID wpi_chunk_id = ChunkGetIdFromTag(wpi_chunk->tag_);
if (wpi_chunk_id == id) ++count;
} }
} }
return count; return count;
@ -298,9 +308,12 @@ static int SearchImageToGetOrDelete(WebPMuxImage** wpi_list, uint32_t nth,
while (*wpi_list) { while (*wpi_list) {
WebPMuxImage* const cur_wpi = *wpi_list; WebPMuxImage* const cur_wpi = *wpi_list;
const WebPChunk* const wpi_chunk = *MuxImageGetListFromId(cur_wpi, id); const WebPChunk* const wpi_chunk = *MuxImageGetListFromId(cur_wpi, id);
if (wpi_chunk != NULL && wpi_chunk->tag_ == kChunks[id].tag) { if (wpi_chunk != NULL) {
++count; const TAG_ID wpi_chunk_id = ChunkGetIdFromTag(wpi_chunk->tag_);
if (count == nth) return 1; // Found. if (wpi_chunk_id == id) {
++count;
if (count == nth) return 1; // Found.
}
} }
wpi_list = &cur_wpi->next_; wpi_list = &cur_wpi->next_;
*location = wpi_list; *location = wpi_list;
@ -376,7 +389,7 @@ static size_t MuxImageDiskSize(const WebPMuxImage* wpi) {
size_t size = 0; size_t size = 0;
if (wpi->header_ != NULL) size += ChunkDiskSize(wpi->header_); if (wpi->header_ != NULL) size += ChunkDiskSize(wpi->header_);
if (wpi->alpha_ != NULL) size += ChunkDiskSize(wpi->alpha_); if (wpi->alpha_ != NULL) size += ChunkDiskSize(wpi->alpha_);
if (wpi->vp8_ != NULL) size += ChunkDiskSize(wpi->vp8_); if (wpi->img_ != NULL) size += ChunkDiskSize(wpi->img_);
return size; return size;
} }
@ -393,11 +406,11 @@ static uint8_t* MuxImageEmit(const WebPMuxImage* const wpi, uint8_t* dst) {
// Ordering of chunks to be emitted is strictly as follows: // Ordering of chunks to be emitted is strictly as follows:
// 1. Frame/Tile chunk (if present). // 1. Frame/Tile chunk (if present).
// 2. Alpha chunk (if present). // 2. Alpha chunk (if present).
// 3. VP8 chunk. // 3. VP8/VP8L chunk.
assert(wpi); assert(wpi);
if (wpi->header_ != NULL) dst = ChunkEmit(wpi->header_, dst); if (wpi->header_ != NULL) dst = ChunkEmit(wpi->header_, dst);
if (wpi->alpha_ != NULL) dst = ChunkEmit(wpi->alpha_, dst); if (wpi->alpha_ != NULL) dst = ChunkEmit(wpi->alpha_, dst);
if (wpi->vp8_ != NULL) dst = ChunkEmit(wpi->vp8_, dst); if (wpi->img_ != NULL) dst = ChunkEmit(wpi->img_, dst);
return dst; return dst;
} }
@ -415,24 +428,24 @@ uint8_t* MuxImageListEmit(const WebPMuxImage* wpi_list, uint8_t* dst) {
WebPChunk** GetChunkListFromId(const WebPMux* mux, TAG_ID id) { WebPChunk** GetChunkListFromId(const WebPMux* mux, TAG_ID id) {
assert(mux != NULL); assert(mux != NULL);
switch(id) { switch(id) {
case VP8X_ID: return (WebPChunk**)&mux->vp8x_; case VP8X_ID: return (WebPChunk**)&mux->vp8x_;
case ICCP_ID: return (WebPChunk**)&mux->iccp_; case ICCP_ID: return (WebPChunk**)&mux->iccp_;
case LOOP_ID: return (WebPChunk**)&mux->loop_; case LOOP_ID: return (WebPChunk**)&mux->loop_;
case META_ID: return (WebPChunk**)&mux->meta_; case META_ID: return (WebPChunk**)&mux->meta_;
case UNKNOWN_ID: return (WebPChunk**)&mux->unknown_; case UNKNOWN_ID: return (WebPChunk**)&mux->unknown_;
default: return NULL; default: return NULL;
} }
} }
WebPMuxError ValidateForImage(const WebPMux* const mux) { WebPMuxError ValidateForImage(const WebPMux* const mux) {
const int num_vp8 = MuxImageCount(mux->images_, IMAGE_ID); const int num_images = MuxImageCount(mux->images_, IMAGE_ID);
const int num_frames = MuxImageCount(mux->images_, FRAME_ID); const int num_frames = MuxImageCount(mux->images_, FRAME_ID);
const int num_tiles = MuxImageCount(mux->images_, TILE_ID); const int num_tiles = MuxImageCount(mux->images_, TILE_ID);
if (num_vp8 == 0) { if (num_images == 0) {
// No images in mux. // No images in mux.
return WEBP_MUX_NOT_FOUND; return WEBP_MUX_NOT_FOUND;
} else if (num_vp8 == 1 && num_frames == 0 && num_tiles == 0) { } else if (num_images == 1 && num_frames == 0 && num_tiles == 0) {
// Valid case (single image). // Valid case (single image).
return WEBP_MUX_OK; return WEBP_MUX_OK;
} else { } else {
@ -448,16 +461,14 @@ static int IsNotCompatible(int feature, int num_items) {
#define NO_FLAG 0 #define NO_FLAG 0
// Test basic constraints: // Test basic constraints:
// retrieval, maximum number of chunks by id (use -1 to skip) // retrieval, maximum number of chunks by index (use -1 to skip)
// and feature incompatibility (use NO_FLAG to skip). // and feature incompatibility (use NO_FLAG to skip).
// On success returns WEBP_MUX_OK and stores the chunk count in *num. // On success returns WEBP_MUX_OK and stores the chunk count in *num.
static WebPMuxError ValidateChunk(const WebPMux* const mux, TAG_ID id, static WebPMuxError ValidateChunk(const WebPMux* const mux, CHUNK_INDEX idx,
FeatureFlags feature, FeatureFlags vp8x_flags, FeatureFlags feature, FeatureFlags vp8x_flags,
int max, int* num) { int max, int* num) {
const WebPMuxError err = const WebPMuxError err =
WebPMuxNumNamedElements(mux, kChunks[id].name, num); WebPMuxNumNamedElements(mux, kChunks[idx].name, num);
assert(id == kChunks[id].id);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
if (max > -1 && *num > max) return WEBP_MUX_INVALID_ARGUMENT; if (max > -1 && *num > max) return WEBP_MUX_INVALID_ARGUMENT;
if (feature != NO_FLAG && IsNotCompatible(vp8x_flags & feature, *num)) { if (feature != NO_FLAG && IsNotCompatible(vp8x_flags & feature, *num)) {
@ -493,18 +504,18 @@ WebPMuxError WebPMuxValidate(const WebPMux* const mux) {
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
// At most one color profile chunk. // At most one color profile chunk.
err = ValidateChunk(mux, ICCP_ID, ICCP_FLAG, flags, 1, &num_iccp); err = ValidateChunk(mux, IDX_ICCP, ICCP_FLAG, flags, 1, &num_iccp);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
// At most one XMP metadata. // At most one XMP metadata.
err = ValidateChunk(mux, META_ID, META_FLAG, flags, 1, &num_meta); err = ValidateChunk(mux, IDX_META, META_FLAG, flags, 1, &num_meta);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
// Animation: ANIMATION_FLAG, loop chunk and frame chunk(s) are consistent. // Animation: ANIMATION_FLAG, loop chunk and frame chunk(s) are consistent.
// At most one loop chunk. // At most one loop chunk.
err = ValidateChunk(mux, LOOP_ID, NO_FLAG, flags, 1, &num_loop_chunks); err = ValidateChunk(mux, IDX_LOOP, NO_FLAG, flags, 1, &num_loop_chunks);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ValidateChunk(mux, FRAME_ID, NO_FLAG, flags, -1, &num_frames); err = ValidateChunk(mux, IDX_FRAME, NO_FLAG, flags, -1, &num_frames);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
{ {
@ -518,19 +529,19 @@ WebPMuxError WebPMuxValidate(const WebPMux* const mux) {
} }
// Tiling: TILE_FLAG and tile chunk(s) are consistent. // Tiling: TILE_FLAG and tile chunk(s) are consistent.
err = ValidateChunk(mux, TILE_ID, TILE_FLAG, flags, -1, &num_tiles); err = ValidateChunk(mux, IDX_TILE, TILE_FLAG, flags, -1, &num_tiles);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
// Verify either VP8X chunk is present OR there is only one elem in // Verify either VP8X chunk is present OR there is only one elem in
// mux->images_. // mux->images_.
err = ValidateChunk(mux, VP8X_ID, NO_FLAG, flags, 1, &num_vp8x); err = ValidateChunk(mux, IDX_VP8X, NO_FLAG, flags, 1, &num_vp8x);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ValidateChunk(mux, IMAGE_ID, NO_FLAG, flags, -1, &num_images); err = ValidateChunk(mux, IDX_VP8, NO_FLAG, flags, -1, &num_images);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT; if (num_vp8x == 0 && num_images != 1) return WEBP_MUX_INVALID_ARGUMENT;
// ALPHA_FLAG & alpha chunk(s) are consistent. // ALPHA_FLAG & alpha chunk(s) are consistent.
err = ValidateChunk(mux, ALPHA_ID, ALPHA_FLAG, flags, -1, &num_alpha); err = ValidateChunk(mux, IDX_ALPHA, ALPHA_FLAG, flags, -1, &num_alpha);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
// num_images & num_alpha_chunks are consistent. // num_images & num_alpha_chunks are consistent.

View File

@ -21,10 +21,10 @@ extern "C" {
// Helper method(s). // Helper method(s).
// Handy MACRO. // Handy MACRO.
#define SWITCH_ID_LIST(ID, LIST) \ #define SWITCH_ID_LIST(INDEX, LIST) \
if (id == (ID)) { \ if (idx == (INDEX)) { \
const WebPChunk* const chunk = ChunkSearchList((LIST), nth, \ const WebPChunk* const chunk = ChunkSearchList((LIST), nth, \
kChunks[(ID)].tag); \ kChunks[(INDEX)].tag); \
if (chunk) { \ if (chunk) { \
data->bytes_ = chunk->data_; \ data->bytes_ = chunk->data_; \
data->size_ = chunk->payload_size_; \ data->size_ = chunk->payload_size_; \
@ -34,17 +34,17 @@ extern "C" {
} \ } \
} }
static WebPMuxError MuxGet(const WebPMux* const mux, TAG_ID id, uint32_t nth, static WebPMuxError MuxGet(const WebPMux* const mux, CHUNK_INDEX idx,
WebPData* const data) { uint32_t nth, WebPData* const data) {
assert(mux != NULL); assert(mux != NULL);
assert(!IsWPI(id)); assert(!IsWPI(kChunks[idx].id));
memset(data, 0, sizeof(*data)); memset(data, 0, sizeof(*data));
SWITCH_ID_LIST(VP8X_ID, mux->vp8x_); SWITCH_ID_LIST(IDX_VP8X, mux->vp8x_);
SWITCH_ID_LIST(ICCP_ID, mux->iccp_); SWITCH_ID_LIST(IDX_ICCP, mux->iccp_);
SWITCH_ID_LIST(LOOP_ID, mux->loop_); SWITCH_ID_LIST(IDX_LOOP, mux->loop_);
SWITCH_ID_LIST(META_ID, mux->meta_); SWITCH_ID_LIST(IDX_META, mux->meta_);
SWITCH_ID_LIST(UNKNOWN_ID, mux->unknown_); SWITCH_ID_LIST(IDX_UNKNOWN, mux->unknown_);
return WEBP_MUX_NOT_FOUND; return WEBP_MUX_NOT_FOUND;
} }
#undef SWITCH_ID_LIST #undef SWITCH_ID_LIST
@ -102,9 +102,10 @@ WebPMux* WebPMuxCreateInternal(const uint8_t* data, size_t size, int copy_data,
} }
tag = GetLE32(data + RIFF_HEADER_SIZE); tag = GetLE32(data + RIFF_HEADER_SIZE);
if (tag != kChunks[IMAGE_ID].tag && tag != kChunks[VP8X_ID].tag) { if (tag != kChunks[IDX_VP8].tag &&
// First chunk should be either VP8X or VP8. tag != kChunks[IDX_VP8L].tag &&
goto Err; tag != kChunks[IDX_VP8X].tag) {
goto Err; // First chunk should be VP8, VP8L or VP8X.
} }
riff_size = SizeWithPadding(GetLE32(data + TAG_SIZE)); riff_size = SizeWithPadding(GetLE32(data + TAG_SIZE));
@ -203,9 +204,9 @@ WebPMuxError WebPMuxGetFeatures(const WebPMux* const mux, uint32_t* flags) {
*flags = 0; *flags = 0;
// Check if VP8X chunk is present. // Check if VP8X chunk is present.
err = MuxGet(mux, VP8X_ID, 1, &data); err = MuxGet(mux, IDX_VP8X, 1, &data);
if (err == WEBP_MUX_NOT_FOUND) { if (err == WEBP_MUX_NOT_FOUND) {
// Check if VP8 chunk is present. // Check if VP8/VP8L chunk is present.
err = WebPMuxGetImage(mux, &data, NULL); err = WebPMuxGetImage(mux, &data, NULL);
if (err == WEBP_MUX_NOT_FOUND && // Data not available (yet). if (err == WEBP_MUX_NOT_FOUND && // Data not available (yet).
mux->state_ == WEBP_MUX_STATE_PARTIAL) { // Incremental case. mux->state_ == WEBP_MUX_STATE_PARTIAL) { // Incremental case.
@ -252,9 +253,9 @@ WebPMuxError WebPMuxGetImage(const WebPMux* const mux,
// Get image chunk. // Get image chunk.
if (image != NULL) { if (image != NULL) {
memset(image, 0, sizeof(*image)); memset(image, 0, sizeof(*image));
if (wpi->vp8_ != NULL) { if (wpi->img_ != NULL) {
image->bytes_ = wpi->vp8_->data_; image->bytes_ = wpi->img_->data_;
image->size_ = wpi->vp8_->payload_size_; image->size_ = wpi->img_->payload_size_;
} }
} }
return WEBP_MUX_OK; return WEBP_MUX_OK;
@ -263,13 +264,13 @@ WebPMuxError WebPMuxGetImage(const WebPMux* const mux,
WebPMuxError WebPMuxGetMetadata(const WebPMux* const mux, WebPMuxError WebPMuxGetMetadata(const WebPMux* const mux,
WebPData* const metadata) { WebPData* const metadata) {
if (mux == NULL || metadata == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (mux == NULL || metadata == NULL) return WEBP_MUX_INVALID_ARGUMENT;
return MuxGet(mux, META_ID, 1, metadata); return MuxGet(mux, IDX_META, 1, metadata);
} }
WebPMuxError WebPMuxGetColorProfile(const WebPMux* const mux, WebPMuxError WebPMuxGetColorProfile(const WebPMux* const mux,
WebPData* const color_profile) { WebPData* const color_profile) {
if (mux == NULL || color_profile == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (mux == NULL || color_profile == NULL) return WEBP_MUX_INVALID_ARGUMENT;
return MuxGet(mux, ICCP_ID, 1, color_profile); return MuxGet(mux, IDX_ICCP, 1, color_profile);
} }
WebPMuxError WebPMuxGetLoopCount(const WebPMux* const mux, WebPMuxError WebPMuxGetLoopCount(const WebPMux* const mux,
@ -279,7 +280,7 @@ WebPMuxError WebPMuxGetLoopCount(const WebPMux* const mux,
if (mux == NULL || loop_count == NULL) return WEBP_MUX_INVALID_ARGUMENT; if (mux == NULL || loop_count == NULL) return WEBP_MUX_INVALID_ARGUMENT;
err = MuxGet(mux, LOOP_ID, 1, &image); err = MuxGet(mux, IDX_LOOP, 1, &image);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
if (image.size_ < kChunks[LOOP_ID].size) return WEBP_MUX_BAD_DATA; if (image.size_ < kChunks[LOOP_ID].size) return WEBP_MUX_BAD_DATA;
*loop_count = GetLE32(image.bytes_); *loop_count = GetLE32(image.bytes_);
@ -297,7 +298,8 @@ static WebPMuxError MuxGetFrameTileInternal(
WebPMuxImage* wpi; WebPMuxImage* wpi;
const int is_frame = (tag == kChunks[FRAME_ID].tag) ? 1 : 0; const int is_frame = (tag == kChunks[FRAME_ID].tag) ? 1 : 0;
const TAG_ID id = is_frame ? FRAME_ID : TILE_ID; const CHUNK_INDEX idx = is_frame ? IDX_FRAME : IDX_TILE;
const TAG_ID id = kChunks[idx].id;
if (mux == NULL || image == NULL || if (mux == NULL || image == NULL ||
x_offset == NULL || y_offset == NULL || (is_frame && duration == NULL)) { x_offset == NULL || y_offset == NULL || (is_frame && duration == NULL)) {
@ -313,7 +315,7 @@ static WebPMuxError MuxGetFrameTileInternal(
frame_tile_data = wpi->header_->data_; frame_tile_data = wpi->header_->data_;
frame_tile_size = wpi->header_->payload_size_; frame_tile_size = wpi->header_->payload_size_;
if (frame_tile_size < kChunks[id].size) return WEBP_MUX_BAD_DATA; if (frame_tile_size < kChunks[idx].size) return WEBP_MUX_BAD_DATA;
*x_offset = GetLE32(frame_tile_data + 0); *x_offset = GetLE32(frame_tile_data + 0);
*y_offset = GetLE32(frame_tile_data + 4); *y_offset = GetLE32(frame_tile_data + 4);
if (is_frame) *duration = GetLE32(frame_tile_data + 16); if (is_frame) *duration = GetLE32(frame_tile_data + 16);
@ -329,9 +331,9 @@ static WebPMuxError MuxGetFrameTileInternal(
// Get image chunk. // Get image chunk.
memset(image, 0, sizeof(*image)); memset(image, 0, sizeof(*image));
if (wpi->vp8_ != NULL) { if (wpi->img_ != NULL) {
image->bytes_ = wpi->vp8_->data_; image->bytes_ = wpi->img_->data_;
image->size_ = wpi->vp8_->payload_size_; image->size_ = wpi->img_->payload_size_;
} }
return WEBP_MUX_OK; return WEBP_MUX_OK;
@ -343,7 +345,7 @@ WebPMuxError WebPMuxGetFrame(const WebPMux* const mux, uint32_t nth,
uint32_t* duration) { uint32_t* duration) {
return MuxGetFrameTileInternal(mux, nth, image, alpha, return MuxGetFrameTileInternal(mux, nth, image, alpha,
x_offset, y_offset, duration, x_offset, y_offset, duration,
kChunks[FRAME_ID].tag); kChunks[IDX_FRAME].tag);
} }
WebPMuxError WebPMuxGetTile(const WebPMux* const mux, uint32_t nth, WebPMuxError WebPMuxGetTile(const WebPMux* const mux, uint32_t nth,
@ -351,7 +353,7 @@ WebPMuxError WebPMuxGetTile(const WebPMux* const mux, uint32_t nth,
uint32_t* x_offset, uint32_t* y_offset) { uint32_t* x_offset, uint32_t* y_offset) {
return MuxGetFrameTileInternal(mux, nth, image, alpha, return MuxGetFrameTileInternal(mux, nth, image, alpha,
x_offset, y_offset, NULL, x_offset, y_offset, NULL,
kChunks[TILE_ID].tag); kChunks[IDX_TILE].tag);
} }
// Count number of chunks matching 'tag' in the 'chunk_list'. // Count number of chunks matching 'tag' in the 'chunk_list'.
@ -367,11 +369,12 @@ static int CountChunks(const WebPChunk* const chunk_list, uint32_t tag) {
return count; return count;
} }
WebPMuxError WebPMuxNumNamedElements(const WebPMux* const mux, const char* tag, WebPMuxError WebPMuxNumNamedElements(const WebPMux* const mux, const char* name,
int* num_elements) { int* num_elements) {
const TAG_ID id = ChunkGetIdFromName(tag); const CHUNK_INDEX idx = ChunkGetIndexFromName(name);
const TAG_ID id = kChunks[idx].id;
if (mux == NULL || tag == NULL || num_elements == NULL) { if (mux == NULL || name == NULL || num_elements == NULL) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
@ -382,7 +385,7 @@ WebPMuxError WebPMuxNumNamedElements(const WebPMux* const mux, const char* tag,
if (chunk_list == NULL) { if (chunk_list == NULL) {
*num_elements = 0; *num_elements = 0;
} else { } else {
*num_elements = CountChunks(*chunk_list, kChunks[id].tag); *num_elements = CountChunks(*chunk_list, kChunks[idx].tag);
} }
} }

View File

@ -139,7 +139,7 @@ static WEBP_INLINE WebPMux* WebPMuxCreate(const uint8_t* data, size_t size,
// will be removed. // will be removed.
// Parameters: // Parameters:
// mux - (in/out) object in which the image is to be set // mux - (in/out) object in which the image is to be set
// data - (in) the image data to be set. The data can be either a VP8 // data - (in) the image data to be set. The data can be either a VP8/VP8L
// bitstream or a single-image WebP file (non-animated & non-tiled) // bitstream or a single-image WebP file (non-animated & non-tiled)
// size - (in) size of the image data // size - (in) size of the image data
// alpha_data - (in) the alpha data corresponding to the image (if present) // alpha_data - (in) the alpha data corresponding to the image (if present)
@ -267,8 +267,8 @@ WEBP_EXTERN(WebPMuxError) WebPMuxDeleteColorProfile(WebPMux* const mux);
// Parameters: // Parameters:
// mux - (in/out) object to which an animation frame is to be added // mux - (in/out) object to which an animation frame is to be added
// nth - (in) The position at which the frame is to be added. // nth - (in) The position at which the frame is to be added.
// data - (in) the raw VP8 image data corresponding to frame image. The data // data - (in) the raw VP8/VP8L image data corresponding to frame image. The
// can be either a VP8 bitstream or a single-image WebP file // data can be either a VP8/VP8L bitstream or a single-image WebP file
// (non-animated & non-tiled) // (non-animated & non-tiled)
// size - (in) size of frame chunk data // size - (in) size of frame chunk data
// alpha_data - (in) the alpha data corresponding to frame image (if present) // alpha_data - (in) the alpha data corresponding to frame image (if present)
@ -367,8 +367,8 @@ WEBP_EXTERN(WebPMuxError) WebPMuxGetLoopCount(const WebPMux* const mux,
// Parameters: // Parameters:
// mux - (in/out) object to which a tile is to be added // mux - (in/out) object to which a tile is to be added
// nth - (in) The position at which the tile is to be added. // nth - (in) The position at which the tile is to be added.
// data - (in) the raw VP8 image data corresponding to tile image. The data // data - (in) the raw VP8/VP8L image data corresponding to tile image. The
// can be either a VP8 bitstream or a single-image WebP file // data can be either a VP8/VP8L bitstream or a single-image WebP file
// (non-animated & non-tiled) // (non-animated & non-tiled)
// size - (in) size of tile chunk data // size - (in) size of tile chunk data
// alpha_data - (in) the alpha data corresponding to tile image (if present) // alpha_data - (in) the alpha data corresponding to tile image (if present)
@ -442,13 +442,15 @@ WEBP_EXTERN(WebPMuxError) WebPMuxGetFeatures(const WebPMux* const mux,
// Gets number of chunks having tag value tag in the mux object. // Gets number of chunks having tag value tag in the mux object.
// Parameters: // Parameters:
// mux - (in) object from which the info is to be fetched // mux - (in) object from which the info is to be fetched
// tag - (in) tag name specifying the type of chunk // name - (in) chunk name specifying the type of chunk
// Can be any of: "vp8x", "iccp", "loop", "frame", "tile", "alpha",
// "image", "meta" or "unknown"
// num_elements - (out) number of chunks corresponding to the specified tag // num_elements - (out) number of chunks corresponding to the specified tag
// Returns: // Returns:
// WEBP_MUX_INVALID_ARGUMENT - if either mux, tag or num_elements is NULL // WEBP_MUX_INVALID_ARGUMENT - if either mux, tag or num_elements is NULL
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxNumNamedElements(const WebPMux* const mux, WEBP_EXTERN(WebPMuxError) WebPMuxNumNamedElements(const WebPMux* const mux,
const char* tag, const char* name,
int* num_elements); int* num_elements);
// Assembles all chunks in WebP RIFF format and returns in output_data. // Assembles all chunks in WebP RIFF format and returns in output_data.