Refactor decoder library

- Move common defines to dec/webpi.h
 - Regularize naming and parameters of various "CheckAndSkip" functions.
   Also they return VP8StatusCode for clarity.
 - Move WebP header/chunk parsing functions to webpi.h
 - Fix a bug in static function GetFeatures()

Change-Id: Ibc3da4ba210d860551724dc40741959750d66b89
This commit is contained in:
Urvang Joshi
2011-09-13 09:35:10 +00:00
committed by James Zern
parent 377ef43c3e
commit 76036f547e
5 changed files with 371 additions and 233 deletions

View File

@ -17,9 +17,6 @@
extern "C" {
#endif
#define RIFF_HEADER_SIZE 12
#define VP8X_HEADER_SIZE 20
//------------------------------------------------------------------------------
int WebPGetDecoderVersion(void) {
@ -81,39 +78,6 @@ int VP8SetError(VP8Decoder* const dec,
//------------------------------------------------------------------------------
static inline uint32_t get_le32(const uint8_t* const data) {
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
}
int VP8CheckAndSkipHeader(const uint8_t** data_ptr, uint32_t* data_size_ptr,
int* is_vp8_chunk, uint32_t* chunk_size,
uint32_t riff_size) {
if (!data_ptr || !data_size_ptr || !is_vp8_chunk || !chunk_size) {
return 0; // Invalid Args.
}
if (*data_size_ptr >= 8) {
if (!memcmp(*data_ptr, "VP8 ", 4)) {
*is_vp8_chunk = 1;
*chunk_size = get_le32(*data_ptr + 4);
if ((riff_size >= RIFF_HEADER_SIZE) &&
(*chunk_size > riff_size - RIFF_HEADER_SIZE)) {
return 0; // Inconsistent size information.
}
// We have consumed 8 bytes from VP8 Header. Skip it.
*data_ptr += 8;
*data_size_ptr -= 8;
} else {
*is_vp8_chunk = 0;
*chunk_size = 0;
}
} else {
*is_vp8_chunk = -1; // Insufficient data.
*chunk_size = 0;
}
return 1;
}
int VP8GetInfo(const uint8_t* data, uint32_t data_size, uint32_t chunk_size,
int* width, int* height, int* has_alpha) {
if (data_size < 10) {
@ -161,40 +125,6 @@ int VP8GetInfo(const uint8_t* data, uint32_t data_size, uint32_t chunk_size,
}
}
int VP8XGetInfo(const uint8_t** data_ptr, uint32_t* data_size_ptr,
int* is_vp8x_chunk, int* width, int* height, uint32_t* flags) {
if (!data_ptr || !data_size_ptr || !is_vp8x_chunk) {
return 0; // Invalid Args.
}
if (*data_size_ptr >= VP8X_HEADER_SIZE) {
if (!memcmp(*data_ptr, "VP8X", 4)) {
const uint32_t chunk_size = get_le32(*data_ptr + 4);
*is_vp8x_chunk = 1;
if (chunk_size != (VP8X_HEADER_SIZE - 8)) {
return 0; // Wrong chunk size.
}
if (flags) {
*flags = get_le32(*data_ptr + 8);
}
if (width) {
*width = get_le32(*data_ptr + 12);
}
if (height) {
*height = get_le32(*data_ptr + 16);
}
// We have consumed 20 bytes from VP8X Header. Skip it.
*data_ptr += VP8X_HEADER_SIZE;
*data_size_ptr -= VP8X_HEADER_SIZE;
} else {
*is_vp8x_chunk = 0;
}
} else {
*is_vp8x_chunk = -1; // Insufficient data.
}
return 1;
}
//------------------------------------------------------------------------------
// Header parsing
@ -319,10 +249,8 @@ static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
const uint8_t* buf;
uint32_t buf_size;
uint32_t riff_size;
uint32_t chunk_size;
int is_vp8x_chunk = 0;
int is_vp8_chunk = 0;
uint32_t vp8_chunk_size;
uint32_t bytes_skipped;
VP8FrameHeader* frm_hdr;
VP8PictureHeader* pic_hdr;
VP8BitReader* br;
@ -339,36 +267,17 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
buf = (uint8_t*)io->data;
buf_size = io->data_size;
if (buf == NULL || buf_size <= 4) {
return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
"Not enough data to parse frame header");
}
// Skip over valid RIFF headers.
if (!WebPCheckAndSkipRIFFHeader(&buf, &buf_size, &riff_size)) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"RIFF: Invalid RIFF container");
}
if (!VP8XGetInfo(&buf, &buf_size, &is_vp8x_chunk, NULL, NULL, NULL)) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"RIFF: Invalid VP8X container");
}
if (!VP8CheckAndSkipHeader(&buf, &buf_size, &is_vp8_chunk, &chunk_size,
riff_size)) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"RIFF: Inconsistent size information.");
}
if (is_vp8_chunk == -1) {
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
"Not enough data to parse frame header.");
// Process Pre-VP8 chunks.
status = WebPParseHeaders(&buf, &buf_size, &vp8_chunk_size, &bytes_skipped);
if (status != VP8_STATUS_OK) {
return VP8SetError(dec, status, "Incorrect/incomplete header.");
}
// Process the VP8 frame header.
if (buf_size < 4) {
return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
"RIFF: Truncated header.");
"Truncated header.");
}
// Paragraph 9.1