Use WebPData in MUX set APIs

Change-Id: Ibdede3c1cd02c6aeef333718592da313f10f6408
This commit is contained in:
Urvang Joshi 2012-06-05 14:20:45 +05:30
parent c67bc979dd
commit 4fc4a47f6e
6 changed files with 134 additions and 139 deletions

View File

@ -320,16 +320,25 @@ static void PrintHelp(void) {
printf("\nINPUT & OUTPUT are in webp format.\n"); printf("\nINPUT & OUTPUT are in webp format.\n");
} }
static int ReadFileToWebPData(const char* const filename,
WebPData* const webp_data) {
const uint8_t* data;
size_t size;
if (!ExUtilReadFile(filename, &data, &size)) return 0;
webp_data->bytes_ = data;
webp_data->size_ = size;
return 1;
}
static int CreateMux(const char* const filename, WebPMux** mux) { static int CreateMux(const char* const filename, WebPMux** mux) {
size_t size = 0;
const uint8_t* data = NULL;
WebPMuxState mux_state; WebPMuxState mux_state;
WebPData bitstream;
assert(mux != NULL); assert(mux != NULL);
if (!ExUtilReadFile(filename, &data, &size)) return 0; if (!ReadFileToWebPData(filename, &bitstream)) return 0;
*mux = WebPMuxCreate(data, size, 1, &mux_state); *mux = WebPMuxCreate(&bitstream, 1, &mux_state);
free((void*)data); free((void*)bitstream.bytes_);
if (*mux != NULL && mux_state == WEBP_MUX_STATE_COMPLETE) return 1; if (*mux != NULL && mux_state == WEBP_MUX_STATE_COMPLETE) return 1;
fprintf(stderr, "Failed to create mux object from file %s. mux_state = %d.\n", fprintf(stderr, "Failed to create mux object from file %s. mux_state = %d.\n",
filename, mux_state); filename, mux_state);
@ -338,18 +347,15 @@ static int CreateMux(const char* const filename, WebPMux** mux) {
static int ReadImage(const char* filename, static int ReadImage(const char* filename,
WebPData* const image_ptr, WebPData* const alpha_ptr) { WebPData* const image_ptr, WebPData* const alpha_ptr) {
const uint8_t* data = NULL; WebPData bitstream, image, alpha;
size_t size = 0;
WebPData image, alpha;
WebPMux* mux; WebPMux* mux;
WebPMuxError err; WebPMuxError err;
int ok = 0; int ok = 0;
WebPMuxState mux_state; WebPMuxState mux_state;
if (!ExUtilReadFile(filename, &data, &size)) return 0; if (!ReadFileToWebPData(filename, &bitstream)) return 0;
mux = WebPMuxCreate(&bitstream, 1, &mux_state);
mux = WebPMuxCreate(data, size, 1, &mux_state); free((void*)bitstream.bytes_);
free((void*)data);
if (mux == NULL || mux_state != WEBP_MUX_STATE_COMPLETE) { if (mux == NULL || mux_state != WEBP_MUX_STATE_COMPLETE) {
fprintf(stderr, fprintf(stderr,
"Failed to create mux object from file %s. mux_state = %d.\n", "Failed to create mux object from file %s. mux_state = %d.\n",
@ -772,8 +778,7 @@ static int GetFrameTile(const WebPMux* mux,
err = WEBP_MUX_MEMORY_ERROR; err = WEBP_MUX_MEMORY_ERROR;
ERROR_GOTO2("ERROR#%d: Could not allocate a mux object.\n", err, ErrGet); ERROR_GOTO2("ERROR#%d: Could not allocate a mux object.\n", err, ErrGet);
} }
err = WebPMuxSetImage(mux_single, image.bytes_, image.size_, err = WebPMuxSetImage(mux_single, &image, &alpha, 1);
alpha.bytes_, alpha.size_, 1);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO2("ERROR#%d: Could not create single image mux object.\n", err, ERROR_GOTO2("ERROR#%d: Could not create single image mux object.\n", err,
ErrGet); ErrGet);
@ -789,8 +794,7 @@ static int GetFrameTile(const WebPMux* mux,
static int Process(const WebPMuxConfig* config) { static int Process(const WebPMuxConfig* config) {
WebPMux* mux = NULL; WebPMux* mux = NULL;
WebPData webpdata; WebPData webpdata;
const uint8_t* data = NULL; WebPData metadata, color_profile;
size_t size = 0;
uint32_t x_offset = 0; uint32_t x_offset = 0;
uint32_t y_offset = 0; uint32_t y_offset = 0;
WebPMuxError err = WEBP_MUX_OK; WebPMuxError err = WEBP_MUX_OK;
@ -862,8 +866,7 @@ static int Process(const WebPMuxConfig* config) {
WebPDataFree(&alpha); WebPDataFree(&alpha);
ERROR_GOTO1("ERROR: Could not parse frame properties.\n", Err2); ERROR_GOTO1("ERROR: Could not parse frame properties.\n", Err2);
} }
err = WebPMuxAddFrame(mux, 0, image.bytes_, image.size_, err = WebPMuxAddFrame(mux, 0, &image, &alpha,
alpha.bytes_, alpha.size_,
x_offset, y_offset, duration, 1); x_offset, y_offset, duration, 1);
WebPDataFree(&image); WebPDataFree(&image);
WebPDataFree(&alpha); WebPDataFree(&alpha);
@ -894,9 +897,7 @@ static int Process(const WebPMuxConfig* config) {
WebPDataFree(&alpha); WebPDataFree(&alpha);
ERROR_GOTO1("ERROR: Could not parse tile properties.\n", Err2); ERROR_GOTO1("ERROR: Could not parse tile properties.\n", Err2);
} }
err = WebPMuxAddTile(mux, 0, image.bytes_, image.size_, err = WebPMuxAddTile(mux, 0, &image, &alpha, x_offset, y_offset, 1);
alpha.bytes_, alpha.size_,
x_offset, y_offset, 1);
WebPDataFree(&image); WebPDataFree(&image);
WebPDataFree(&alpha); WebPDataFree(&alpha);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
@ -909,10 +910,10 @@ static int Process(const WebPMuxConfig* config) {
case FEATURE_ICCP: case FEATURE_ICCP:
ok = CreateMux(config->input_, &mux); ok = CreateMux(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
ok = ExUtilReadFile(feature->args_[0].filename_, &data, &size); ok = ReadFileToWebPData(feature->args_[0].filename_, &color_profile);
if (!ok) goto Err2; if (!ok) goto Err2;
err = WebPMuxSetColorProfile(mux, data, size, 1); err = WebPMuxSetColorProfile(mux, &color_profile, 1);
free((void*)data); free((void*)color_profile.bytes_);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO2("ERROR#%d: Could not set color profile.\n", err, Err2); ERROR_GOTO2("ERROR#%d: Could not set color profile.\n", err, Err2);
} }
@ -921,10 +922,10 @@ static int Process(const WebPMuxConfig* config) {
case FEATURE_XMP: case FEATURE_XMP:
ok = CreateMux(config->input_, &mux); ok = CreateMux(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
ok = ExUtilReadFile(feature->args_[0].filename_, &data, &size); ok = ReadFileToWebPData(feature->args_[0].filename_, &metadata);
if (!ok) goto Err2; if (!ok) goto Err2;
err = WebPMuxSetMetadata(mux, data, size, 1); err = WebPMuxSetMetadata(mux, &metadata, 1);
free((void*)data); free((void*)metadata.bytes_);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO2("ERROR#%d: Could not set XMP metadata.\n", err, Err2); ERROR_GOTO2("ERROR#%d: Could not set XMP metadata.\n", err, Err2);
} }

View File

@ -62,9 +62,8 @@ void WebPMuxDelete(WebPMux* const mux) {
// Handy MACRO, makes MuxSet() very symmetric to MuxGet(). // Handy MACRO, makes MuxSet() very symmetric to MuxGet().
#define SWITCH_ID_LIST(INDEX, LIST) \ #define SWITCH_ID_LIST(INDEX, LIST) \
if (idx == (INDEX)) { \ if (idx == (INDEX)) { \
err = ChunkAssignDataImageInfo(&chunk, data, size, \ err = ChunkAssignDataImageInfo(&chunk, data, image_info, copy_data, \
image_info, \ kChunks[(INDEX)].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); \
} \ } \
@ -72,7 +71,7 @@ void WebPMuxDelete(WebPMux* const mux) {
} }
static WebPMuxError MuxSet(WebPMux* const mux, CHUNK_INDEX idx, uint32_t nth, static WebPMuxError MuxSet(WebPMux* const mux, CHUNK_INDEX idx, uint32_t nth,
const uint8_t* data, size_t size, const WebPData* const data,
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;
@ -84,11 +83,12 @@ static WebPMuxError MuxSet(WebPMux* const mux, CHUNK_INDEX idx, uint32_t nth,
SWITCH_ID_LIST(IDX_ICCP, &mux->iccp_); SWITCH_ID_LIST(IDX_ICCP, &mux->iccp_);
SWITCH_ID_LIST(IDX_LOOP, &mux->loop_); SWITCH_ID_LIST(IDX_LOOP, &mux->loop_);
SWITCH_ID_LIST(IDX_META, &mux->meta_); SWITCH_ID_LIST(IDX_META, &mux->meta_);
if (idx == IDX_UNKNOWN && size > TAG_SIZE) { if (idx == IDX_UNKNOWN && data->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, const WebPData tmp = { data->bytes_ + TAG_SIZE, data->size_ - TAG_SIZE };
image_info, copy_data, GetLE32(data + 0)); err = ChunkAssignDataImageInfo(&chunk, &tmp, image_info, copy_data,
GetLE32(data->bytes_ + 0));
if (err == WEBP_MUX_OK) if (err == WEBP_MUX_OK)
err = ChunkSetNth(&chunk, &mux->unknown_, nth); err = ChunkSetNth(&chunk, &mux->unknown_, nth);
} }
@ -100,11 +100,12 @@ 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 CHUNK_INDEX idx = ChunkGetIndexFromTag(tag); const CHUNK_INDEX idx = ChunkGetIndexFromTag(tag);
const WebPData chunk_data = { data, size };
assert(mux != NULL); assert(mux != NULL);
assert(size <= MAX_CHUNK_PAYLOAD); assert(size <= MAX_CHUNK_PAYLOAD);
if (idx == IDX_NIL) return WEBP_MUX_INVALID_PARAMETER; if (idx == IDX_NIL) return WEBP_MUX_INVALID_PARAMETER;
return MuxSet(mux, idx, nth, data, size, image_info, copy_data); return MuxSet(mux, idx, nth, &chunk_data, image_info, copy_data);
} }
static void InitImageInfo(WebPImageInfo* const image_info) { static void InitImageInfo(WebPImageInfo* const image_info) {
@ -116,11 +117,13 @@ static void InitImageInfo(WebPImageInfo* const image_info) {
// Dimensions calculated from passed VP8/VP8L 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 WebPData* const image_data,
int is_lossless) { int is_lossless) {
int width; int width;
int height; int height;
WebPImageInfo* image_info = NULL; WebPImageInfo* image_info = NULL;
const uint8_t* const data = image_data->bytes_;
const size_t size = image_data->size_;
const int ok = is_lossless ? const int ok = is_lossless ?
VP8LGetInfo(data, size, &width, &height, NULL) : VP8LGetInfo(data, size, &width, &height, NULL) :
@ -143,7 +146,8 @@ static WebPImageInfo* CreateImageInfo(uint32_t x_offset, uint32_t y_offset,
// Create data for frame/tile given image_info. // Create data for frame/tile given image_info.
static WebPMuxError CreateDataFromImageInfo(const WebPImageInfo* image_info, static WebPMuxError CreateDataFromImageInfo(const WebPImageInfo* image_info,
int is_frame, int is_frame,
uint8_t** data, size_t* size) { uint8_t** const data,
size_t* const size) {
assert(data); assert(data);
assert(size); assert(size);
assert(image_info); assert(image_info);
@ -164,32 +168,28 @@ 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] == VP8L_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. // 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 WebPData* const bitstream,
WebPData* const image, WebPData* const alpha, WebPData* const image, WebPData* const alpha,
int* const is_lossless) { int* const is_lossless) {
if (size < TAG_SIZE || memcmp(data, "RIFF", TAG_SIZE)) { if (bitstream->size_ < TAG_SIZE ||
memcmp(bitstream->bytes_, "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 = *bitstream;
image->size_ = size; *is_lossless = VP8LCheckSignature(image->bytes_, image->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.
WebPMuxError err; WebPMuxError err;
WebPMuxState mux_state; WebPMuxState mux_state;
WebPMux* const mux = WebPMuxCreate(data, size, 0, &mux_state); WebPMux* const mux = WebPMuxCreate(bitstream, 0, &mux_state);
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); *is_lossless = VP8LCheckSignature(image->bytes_, image->size_);
return err; return err;
} }
} }
@ -232,24 +232,25 @@ static WebPMuxError DeleteLoopCount(WebPMux* const mux) {
// Set API(s). // Set API(s).
WebPMuxError WebPMuxSetImage(WebPMux* const mux, WebPMuxError WebPMuxSetImage(WebPMux* const mux,
const uint8_t* data, size_t size, const WebPData* const image,
const uint8_t* alpha_data, size_t alpha_size, const WebPData* const alpha,
int copy_data) { int copy_data) {
WebPMuxError err; WebPMuxError err;
WebPChunk chunk; WebPChunk chunk;
WebPMuxImage wpi; WebPMuxImage wpi;
WebPData image; WebPData image_raw;
const int has_alpha = (alpha_data != NULL && alpha_size != 0); const int has_alpha = (alpha != NULL && alpha->bytes_ != NULL);
int is_lossless; int is_lossless;
int image_tag; int image_tag;
if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) { if (mux == NULL || image == NULL || image->bytes_ == NULL ||
image->size_ > MAX_CHUNK_PAYLOAD) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
// If given data is for a whole webp file, // If given data is for a whole webp file,
// extract only the VP8/VP8L data from it. // extract only the VP8/VP8L data from it.
err = GetImageData(data, size, &image, NULL, &is_lossless); err = GetImageData(image, &image_raw, 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; image_tag = is_lossless ? kChunks[IDX_VP8L].tag : kChunks[IDX_VP8].tag;
@ -260,8 +261,8 @@ 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, NULL, copy_data,
copy_data, kChunks[IDX_ALPHA].tag); 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;
@ -269,8 +270,7 @@ 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_raw, NULL, copy_data, image_tag);
copy_data, image_tag);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
err = ChunkSetNth(&chunk, &wpi.img_, 1); err = ChunkSetNth(&chunk, &wpi.img_, 1);
if (err != WEBP_MUX_OK) return err; if (err != WEBP_MUX_OK) return err;
@ -280,11 +280,12 @@ WebPMuxError WebPMuxSetImage(WebPMux* const mux,
} }
WebPMuxError WebPMuxSetMetadata(WebPMux* const mux, WebPMuxError WebPMuxSetMetadata(WebPMux* const mux,
const uint8_t* data, size_t size, const WebPData* const metadata,
int copy_data) { int copy_data) {
WebPMuxError err; WebPMuxError err;
if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) { if (mux == NULL || metadata == NULL || metadata->bytes_ == NULL ||
metadata->size_ > MAX_CHUNK_PAYLOAD) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
@ -293,15 +294,16 @@ 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, IDX_META, 1, data, size, NULL, copy_data); return MuxSet(mux, IDX_META, 1, metadata, NULL, copy_data);
} }
WebPMuxError WebPMuxSetColorProfile(WebPMux* const mux, WebPMuxError WebPMuxSetColorProfile(WebPMux* const mux,
const uint8_t* data, size_t size, const WebPData* const color_profile,
int copy_data) { int copy_data) {
WebPMuxError err; WebPMuxError err;
if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) { if (mux == NULL || color_profile == NULL || color_profile->bytes_ == NULL ||
color_profile->size_ > MAX_CHUNK_PAYLOAD) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
@ -310,7 +312,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, IDX_ICCP, 1, data, size, NULL, copy_data); return MuxSet(mux, IDX_ICCP, 1, color_profile, NULL, copy_data);
} }
WebPMuxError WebPMuxSetLoopCount(WebPMux* const mux, uint32_t loop_count) { WebPMuxError WebPMuxSetLoopCount(WebPMux* const mux, uint32_t loop_count) {
@ -336,29 +338,30 @@ WebPMuxError WebPMuxSetLoopCount(WebPMux* const mux, uint32_t loop_count) {
static WebPMuxError MuxAddFrameTileInternal( static WebPMuxError MuxAddFrameTileInternal(
WebPMux* const mux, uint32_t nth, WebPMux* const mux, uint32_t nth,
const uint8_t* data, size_t size, const WebPData* const image, const WebPData* const alpha,
const uint8_t* alpha_data, size_t alpha_size,
uint32_t x_offset, uint32_t y_offset, uint32_t duration, uint32_t x_offset, uint32_t y_offset, uint32_t duration,
int copy_data, uint32_t tag) { int copy_data, uint32_t tag) {
WebPChunk chunk; WebPChunk chunk;
WebPData image; WebPData image_raw;
WebPMuxImage wpi; WebPMuxImage wpi;
WebPMuxError err; WebPMuxError err;
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_size = 0;
WebPData frame_tile;
const int is_frame = (tag == kChunks[IDX_FRAME].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 != NULL && alpha->bytes_ != NULL);
int is_lossless; int is_lossless;
int image_tag; int image_tag;
if (mux == NULL || data == NULL || size > MAX_CHUNK_PAYLOAD) { if (mux == NULL || image == NULL || image->bytes_ == NULL ||
image->size_ > MAX_CHUNK_PAYLOAD) {
return WEBP_MUX_INVALID_ARGUMENT; return WEBP_MUX_INVALID_ARGUMENT;
} }
// If given data is for a whole webp file, // If given data is for a whole webp file,
// extract only the VP8/VP8L data from it. // extract only the VP8/VP8L data from it.
err = GetImageData(data, size, &image, NULL, &is_lossless); err = GetImageData(image, &image_raw, 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; image_tag = is_lossless ? kChunks[IDX_VP8L].tag : kChunks[IDX_VP8].tag;
@ -367,8 +370,8 @@ 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, NULL, copy_data,
copy_data, kChunks[IDX_ALPHA].tag); 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;
@ -376,16 +379,16 @@ 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_raw,
image.bytes_, image.size_, is_lossless); is_lossless);
if (image_info == NULL) { if (image_info == NULL) {
MuxImageRelease(&wpi); MuxImageRelease(&wpi);
return WEBP_MUX_MEMORY_ERROR; return WEBP_MUX_MEMORY_ERROR;
} }
// Add image chunk. // Add image chunk.
err = ChunkAssignDataImageInfo(&chunk, image.bytes_, image.size_, image_info, err = ChunkAssignDataImageInfo(&chunk, &image_raw, image_info, copy_data,
copy_data, image_tag); 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.img_, 1); err = ChunkSetNth(&chunk, &wpi.img_, 1);
@ -394,12 +397,13 @@ static WebPMuxError MuxAddFrameTileInternal(
// Create frame/tile data from image_info. // Create frame/tile data from image_info.
err = CreateDataFromImageInfo(wpi.img_->image_info_, is_frame, err = CreateDataFromImageInfo(wpi.img_->image_info_, is_frame,
&frame_tile_data, &frame_tile_data_size); &frame_tile_data, &frame_tile_size);
if (err != WEBP_MUX_OK) goto Err; if (err != WEBP_MUX_OK) goto Err;
// Add frame/tile chunk (with copy_data = 1). // Add frame/tile chunk (with copy_data = 1).
err = ChunkAssignDataImageInfo(&chunk, frame_tile_data, frame_tile_data_size, frame_tile.bytes_ = frame_tile_data;
NULL, 1, tag); frame_tile.size_ = frame_tile_size;
err = ChunkAssignDataImageInfo(&chunk, &frame_tile, NULL, 1, tag);
if (err != WEBP_MUX_OK) goto Err; if (err != WEBP_MUX_OK) goto Err;
free(frame_tile_data); free(frame_tile_data);
frame_tile_data = NULL; frame_tile_data = NULL;
@ -425,21 +429,21 @@ static WebPMuxError MuxAddFrameTileInternal(
// TODO(urvang): Think about whether we need 'nth' while adding a frame or tile. // TODO(urvang): Think about whether we need 'nth' while adding a frame or tile.
WebPMuxError WebPMuxAddFrame(WebPMux* const mux, uint32_t nth, WebPMuxError WebPMuxAddFrame(WebPMux* const mux, uint32_t nth,
const uint8_t* data, size_t size, const WebPData* const image,
const uint8_t* alpha_data, size_t alpha_size, const WebPData* const alpha,
uint32_t x_offset, uint32_t y_offset, uint32_t x_offset, uint32_t y_offset,
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, image, alpha,
x_offset, y_offset, duration, x_offset, y_offset, duration,
copy_data, kChunks[IDX_FRAME].tag); copy_data, kChunks[IDX_FRAME].tag);
} }
WebPMuxError WebPMuxAddTile(WebPMux* const mux, uint32_t nth, WebPMuxError WebPMuxAddTile(WebPMux* const mux, uint32_t nth,
const uint8_t* data, size_t size, const WebPData* const image,
const uint8_t* alpha_data, size_t alpha_size, const WebPData* const alpha,
uint32_t x_offset, uint32_t y_offset, uint32_t x_offset, uint32_t y_offset,
int copy_data) { int copy_data) {
return MuxAddFrameTileInternal(mux, nth, data, size, alpha_data, alpha_size, return MuxAddFrameTileInternal(mux, nth, image, alpha,
x_offset, y_offset, 1, x_offset, y_offset, 1,
copy_data, kChunks[IDX_TILE].tag); copy_data, kChunks[IDX_TILE].tag);
} }

View File

@ -163,7 +163,7 @@ WebPChunk* ChunkSearchList(WebPChunk* first, uint32_t nth, uint32_t tag);
// Fill the chunk with the given data & image_info. // Fill the chunk with the given data & image_info.
WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk, WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk,
const uint8_t* data, size_t data_size, const WebPData* const data,
WebPImageInfo* image_info, WebPImageInfo* image_info,
int copy_data, uint32_t tag); int copy_data, uint32_t tag);

View File

@ -132,7 +132,7 @@ static int ChunkSearchListToSet(WebPChunk** chunk_list, uint32_t nth,
// Chunk writer methods. // Chunk writer methods.
WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk, WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk,
const uint8_t* data, size_t data_size, const WebPData* const data,
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.
@ -141,26 +141,21 @@ WebPMuxError ChunkAssignDataImageInfo(WebPChunk* chunk,
} }
ChunkRelease(chunk); ChunkRelease(chunk);
if (data == NULL) {
data_size = 0;
} else if (data_size == 0) {
data = NULL;
}
if (data != NULL) { if (data != NULL) {
if (copy_data) { if (copy_data) {
// Copy data. // Copy data.
chunk->data_ = (uint8_t*)malloc(data_size); chunk->data_ = (uint8_t*)malloc(data->size_);
if (chunk->data_ == NULL) return WEBP_MUX_MEMORY_ERROR; if (chunk->data_ == NULL) return WEBP_MUX_MEMORY_ERROR;
memcpy((uint8_t*)chunk->data_, data, data_size); memcpy((uint8_t*)chunk->data_, data->bytes_, data->size_);
chunk->payload_size_ = data_size; chunk->payload_size_ = data->size_;
// Chunk is owner of data. // Chunk is owner of data.
chunk->owner_ = 1; chunk->owner_ = 1;
} else { } else {
// Don't copy data. // Don't copy data.
chunk->data_ = data; chunk->data_ = data->bytes_;
chunk->payload_size_ = data_size; chunk->payload_size_ = data->size_;
} }
} }

View File

@ -55,6 +55,7 @@ static WebPMuxError ChunkAssignData(WebPChunk* chunk, const uint8_t* data,
size_t data_size, size_t riff_size, size_t data_size, size_t riff_size,
int copy_data) { int copy_data) {
uint32_t chunk_size; uint32_t chunk_size;
WebPData chunk_data;
// Sanity checks. // Sanity checks.
if (data_size < TAG_SIZE) return WEBP_MUX_NOT_ENOUGH_DATA; if (data_size < TAG_SIZE) return WEBP_MUX_NOT_ENOUGH_DATA;
@ -67,25 +68,34 @@ static WebPMuxError ChunkAssignData(WebPChunk* chunk, const uint8_t* data,
} }
// Data assignment. // Data assignment.
return ChunkAssignDataImageInfo(chunk, data + CHUNK_HEADER_SIZE, chunk_size, chunk_data.bytes_ = data + CHUNK_HEADER_SIZE;
NULL, copy_data, GetLE32(data + 0)); chunk_data.size_ = chunk_size;
return ChunkAssignDataImageInfo(chunk, &chunk_data, NULL, copy_data,
GetLE32(data + 0));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Create a mux object from WebP-RIFF data. // Create a mux object from WebP-RIFF data.
WebPMux* WebPMuxCreateInternal(const uint8_t* data, size_t size, int copy_data, WebPMux* WebPMuxCreateInternal(const WebPData* const bitstream, int copy_data,
WebPMuxState* const mux_state, int version) { WebPMuxState* const mux_state, int version) {
size_t riff_size; size_t riff_size;
uint32_t tag; uint32_t tag;
const uint8_t* end; const uint8_t* end;
WebPMux* mux = NULL; WebPMux* mux = NULL;
WebPMuxImage* wpi = NULL; WebPMuxImage* wpi = NULL;
const uint8_t* data;
size_t size;
if (mux_state) *mux_state = WEBP_MUX_STATE_PARTIAL; if (mux_state) *mux_state = WEBP_MUX_STATE_PARTIAL;
// Sanity checks. // Sanity checks.
if (version != WEBP_MUX_ABI_VERSION) goto Err; // version mismatch if (version != WEBP_MUX_ABI_VERSION) goto Err; // version mismatch
if (bitstream == NULL) goto Err;
data = bitstream->bytes_;
size = bitstream->size_;
if (data == NULL) goto Err; if (data == NULL) goto Err;
if (size < RIFF_HEADER_SIZE) return NULL; if (size < RIFF_HEADER_SIZE) return NULL;
if (GetLE32(data + 0) != mktag('R', 'I', 'F', 'F') || if (GetLE32(data + 0) != mktag('R', 'I', 'F', 'F') ||

View File

@ -18,12 +18,11 @@
// int copy_data = 0; // int copy_data = 0;
// WebPMux* mux = WebPMuxNew(); // WebPMux* mux = WebPMuxNew();
// // ... (Prepare image data). // // ... (Prepare image data).
// WebPMuxSetImage(mux, image_data, image_data_size, alpha_data, alpha_size, // WebPMuxSetImage(mux, image_data, alpha_data, copy_data);
// copy_data);
// // ... (Prepare ICCP color profile data). // // ... (Prepare ICCP color profile data).
// WebPMuxSetColorProfile(mux, icc_data, icc_data_size, copy_data); // WebPMuxSetColorProfile(mux, icc_data, copy_data);
// // ... (Prepare XMP metadata). // // ... (Prepare XMP metadata).
// WebPMuxSetMetadata(mux, xmp_data, xmp_data_size, copy_data); // WebPMuxSetMetadata(mux, xmp_data, copy_data);
// // Get data from mux in WebP RIFF format. // // Get data from mux in WebP RIFF format.
// WebPMuxAssemble(mux, &output_data, &output_data_size); // WebPMuxAssemble(mux, &output_data, &output_data_size);
// WebPMuxDelete(mux); // WebPMuxDelete(mux);
@ -34,7 +33,7 @@
// //
// int copy_data = 0; // int copy_data = 0;
// // ... (Read data from file). // // ... (Read data from file).
// WebPMux* mux = WebPMuxCreate(data, data_size, copy_data, NULL); // WebPMux* mux = WebPMuxCreate(data, copy_data, NULL);
// WebPMuxGetImage(mux, &image, &alpha); // WebPMuxGetImage(mux, &image, &alpha);
// // ... (Consume image; e.g. call WebPDecode() to decode the data). // // ... (Consume image; e.g. call WebPDecode() to decode the data).
// WebPMuxGetColorProfile(mux, &icc_profile); // WebPMuxGetColorProfile(mux, &icc_profile);
@ -111,13 +110,12 @@ WEBP_EXTERN(void) WebPMuxDelete(WebPMux* const mux);
// Mux creation. // Mux creation.
// Internal, version-checked, entry point // Internal, version-checked, entry point
WEBP_EXTERN(WebPMux*) WebPMuxCreateInternal(const uint8_t*, size_t, WEBP_EXTERN(WebPMux*) WebPMuxCreateInternal(const WebPData* const,
int, WebPMuxState* const, int); int, WebPMuxState* const, int);
// Creates a mux object from raw data given in WebP RIFF format. // Creates a mux object from raw data given in WebP RIFF format.
// Parameters: // Parameters:
// data - (in) the raw data in WebP RIFF format // bitstream - (in) the bitstream data in WebP RIFF format
// size - (in) size of raw data
// copy_data - (in) value 1 indicates given data WILL copied to the mux, and // copy_data - (in) value 1 indicates given data WILL copied to the mux, and
// value 0 indicates data will NOT be copied. // value 0 indicates data will NOT be copied.
// mux_state - (out) indicates the state of the mux returned. Can be passed // mux_state - (out) indicates the state of the mux returned. Can be passed
@ -125,11 +123,11 @@ WEBP_EXTERN(WebPMux*) WebPMuxCreateInternal(const uint8_t*, size_t,
// Returns: // Returns:
// A pointer to the mux object created from given data - on success. // A pointer to the mux object created from given data - on success.
// NULL - In case of invalid data or memory error. // NULL - In case of invalid data or memory error.
static WEBP_INLINE WebPMux* WebPMuxCreate(const uint8_t* data, size_t size, static WEBP_INLINE WebPMux* WebPMuxCreate(const WebPData* const bitstream,
int copy_data, int copy_data,
WebPMuxState* const mux_state) { WebPMuxState* const mux_state) {
return WebPMuxCreateInternal( return WebPMuxCreateInternal(
data, size, copy_data, mux_state, WEBP_MUX_ABI_VERSION); bitstream, copy_data, mux_state, WEBP_MUX_ABI_VERSION);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -139,11 +137,9 @@ 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/VP8L // image - (in) the image data. 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 // alpha - (in) the alpha data of the image (if present)
// alpha_data - (in) the alpha data corresponding to the image (if present)
// alpha_size - (in) size of alpha chunk data
// copy_data - (in) value 1 indicates given data WILL copied to the mux, and // copy_data - (in) value 1 indicates given data WILL copied to the mux, and
// value 0 indicates data will NOT be copied. // value 0 indicates data will NOT be copied.
// Returns: // Returns:
@ -152,8 +148,7 @@ static WEBP_INLINE WebPMux* WebPMuxCreate(const uint8_t* data, size_t size,
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxSetImage( WEBP_EXTERN(WebPMuxError) WebPMuxSetImage(
WebPMux* const mux, WebPMux* const mux,
const uint8_t* data, size_t size, const WebPData* const image, const WebPData* const alpha,
const uint8_t* alpha_data, size_t alpha_size,
int copy_data); int copy_data);
// Gets a reference to the image in the mux object. // Gets a reference to the image in the mux object.
@ -188,8 +183,7 @@ WEBP_EXTERN(WebPMuxError) WebPMuxDeleteImage(WebPMux* const mux);
// be removed. // be removed.
// Parameters: // Parameters:
// mux - (in/out) object to which the XMP metadata is to be added // mux - (in/out) object to which the XMP metadata is to be added
// data - (in) the XMP metadata data to be added // metadata - (in) the XMP metadata data to be added
// size - (in) size of the XMP metadata data
// copy_data - (in) value 1 indicates given data WILL copied to the mux, and // copy_data - (in) value 1 indicates given data WILL copied to the mux, and
// value 0 indicates data will NOT be copied. // value 0 indicates data will NOT be copied.
// Returns: // Returns:
@ -197,7 +191,7 @@ WEBP_EXTERN(WebPMuxError) WebPMuxDeleteImage(WebPMux* const mux);
// WEBP_MUX_MEMORY_ERROR - on memory allocation error. // WEBP_MUX_MEMORY_ERROR - on memory allocation error.
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxSetMetadata( WEBP_EXTERN(WebPMuxError) WebPMuxSetMetadata(
WebPMux* const mux, const uint8_t* data, size_t size, int copy_data); WebPMux* const mux, const WebPData* const metadata, int copy_data);
// Gets a reference to the XMP metadata in the mux object. // Gets a reference to the XMP metadata in the mux object.
// The caller should NOT free the returned data. // The caller should NOT free the returned data.
@ -227,8 +221,7 @@ WEBP_EXTERN(WebPMuxError) WebPMuxDeleteMetadata(WebPMux* const mux);
// will be removed. // will be removed.
// Parameters: // Parameters:
// mux - (in/out) object to which the color profile is to be added // mux - (in/out) object to which the color profile is to be added
// data - (in) the color profile data to be added // color_profile - (in) the color profile data to be added
// size - (in) size of the color profile data
// copy_data - (in) value 1 indicates given data WILL copied to the mux, and // copy_data - (in) value 1 indicates given data WILL copied to the mux, and
// value 0 indicates data will NOT be copied. // value 0 indicates data will NOT be copied.
// Returns: // Returns:
@ -236,7 +229,7 @@ WEBP_EXTERN(WebPMuxError) WebPMuxDeleteMetadata(WebPMux* const mux);
// WEBP_MUX_MEMORY_ERROR - on memory allocation error // WEBP_MUX_MEMORY_ERROR - on memory allocation error
// WEBP_MUX_OK - on success // WEBP_MUX_OK - on success
WEBP_EXTERN(WebPMuxError) WebPMuxSetColorProfile( WEBP_EXTERN(WebPMuxError) WebPMuxSetColorProfile(
WebPMux* const mux, const uint8_t* data, size_t size, int copy_data); WebPMux* const mux, const WebPData* const color_profile, int copy_data);
// Gets a reference to the color profile in the mux object. // Gets a reference to the color profile in the mux object.
// The caller should NOT free the returned data. // The caller should NOT free the returned data.
@ -267,12 +260,10 @@ 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/VP8L image data corresponding to frame image. The // image - (in) the raw VP8/VP8L image data corresponding to frame image. The
// data can be either a VP8/VP8L 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 // alpha - (in) the alpha data corresponding to frame image (if present)
// alpha_data - (in) the alpha data corresponding to frame image (if present)
// alpha_size - (in) size of alpha chunk data
// x_offset - (in) x-offset of the frame to be added // x_offset - (in) x-offset of the frame to be added
// y_offset - (in) y-offset of the frame to be added // y_offset - (in) y-offset of the frame to be added
// duration - (in) duration of the frame to be added (in milliseconds) // duration - (in) duration of the frame to be added (in milliseconds)
@ -285,18 +276,15 @@ WEBP_EXTERN(WebPMuxError) WebPMuxDeleteColorProfile(WebPMux* const mux);
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxAddFrame( WEBP_EXTERN(WebPMuxError) WebPMuxAddFrame(
WebPMux* const mux, uint32_t nth, WebPMux* const mux, uint32_t nth,
const uint8_t* data, size_t size, const WebPData* const image, const WebPData* const alpha,
const uint8_t* alpha_data, size_t alpha_size,
uint32_t x_offset, uint32_t y_offset, uint32_t duration, uint32_t x_offset, uint32_t y_offset, uint32_t duration,
int copy_data); int copy_data);
// TODO(urvang): Create a struct as follows to reduce argument list size: // TODO(urvang): Create a struct as follows to reduce argument list size:
// typedef struct { // typedef struct {
// int nth; // int nth;
// uint8_t* data; // WebPData image;
// uint32_t data_size; // WebPData alpha;
// uint8_t* alpha;
// uint32_t alpha_size;
// uint32_t x_offset, y_offset; // uint32_t x_offset, y_offset;
// uint32_t duration; // uint32_t duration;
// } FrameInfo; // } FrameInfo;
@ -367,12 +355,10 @@ 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/VP8L image data corresponding to tile image. The // image - (in) the raw VP8/VP8L image data corresponding to tile image. The
// data can be either a VP8/VP8L 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 // alpha - (in) the alpha data corresponding to tile image (if present)
// alpha_data - (in) the alpha data corresponding to tile image (if present)
// alpha_size - (in) size of alpha chunk data
// x_offset - (in) x-offset of the tile to be added // x_offset - (in) x-offset of the tile to be added
// y_offset - (in) y-offset of the tile to be added // y_offset - (in) y-offset of the tile to be added
// copy_data - (in) value 1 indicates given data WILL copied to the mux, and // copy_data - (in) value 1 indicates given data WILL copied to the mux, and
@ -384,8 +370,7 @@ WEBP_EXTERN(WebPMuxError) WebPMuxGetLoopCount(const WebPMux* const mux,
// WEBP_MUX_OK - on success. // WEBP_MUX_OK - on success.
WEBP_EXTERN(WebPMuxError) WebPMuxAddTile( WEBP_EXTERN(WebPMuxError) WebPMuxAddTile(
WebPMux* const mux, uint32_t nth, WebPMux* const mux, uint32_t nth,
const uint8_t* data, size_t size, const WebPData* const image, const WebPData* const alpha,
const uint8_t* alpha_data, size_t alpha_size,
uint32_t x_offset, uint32_t y_offset, uint32_t x_offset, uint32_t y_offset,
int copy_data); int copy_data);