mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-27 06:08:21 +01:00
split the VP8 and VP8L decoding properly
* each with their own decoder instances. * Refactor the incremental buffer-update code a lot. * remove br_offset_ for VP8LDecoder along the way * make VP8GetHeaders() be used only for VP8, not VP8L bitstream * remove VP8LInitDecoder() * rename VP8LBitReaderResize() to VP8LBitReaderSetBuffer() (cherry picked from commit 5529a2e6d47212a721ca4ab003215f97bd88ebb4) Change-Id: I58f0b8abe1ef31c8b0e1a6175d2d86b863793ead
This commit is contained in:
parent
f2623dbe58
commit
6f01b830e2
276
src/dec/idec.c
276
src/dec/idec.c
@ -63,12 +63,13 @@ typedef struct {
|
||||
struct WebPIDecoder {
|
||||
DecState state_; // current decoding state
|
||||
WebPDecParams params_; // Params to store output info
|
||||
VP8Decoder* dec_;
|
||||
int is_lossless_; // for down-casting 'dec_'.
|
||||
void* dec_; // either a VP8Decoder or a VP8LDecoder instance
|
||||
VP8Io io_;
|
||||
|
||||
MemBuffer mem_; // input memory buffer.
|
||||
WebPDecBuffer output_; // output buffer (when no external one is supplied)
|
||||
uint32_t vp8_size_; // VP8 size extracted from VP8 Header.
|
||||
uint32_t chunk_size_; // Compressed VP8/VP8L size extracted from Header.
|
||||
};
|
||||
|
||||
// MB context to restore in case VP8DecodeMB() fails
|
||||
@ -84,20 +85,46 @@ typedef struct {
|
||||
//------------------------------------------------------------------------------
|
||||
// MemBuffer: incoming data handling
|
||||
|
||||
#define REMAP(PTR, OLD_BASE, NEW_BASE) (PTR) = (NEW_BASE) + ((PTR) - OLD_BASE)
|
||||
static void RemapBitReader(VP8BitReader* const br, ptrdiff_t offset) {
|
||||
if (br->buf_ != NULL) {
|
||||
br->buf_ += offset;
|
||||
br->buf_end_ += offset;
|
||||
}
|
||||
}
|
||||
|
||||
static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) {
|
||||
return (mem->end_ - mem->start_);
|
||||
}
|
||||
|
||||
static void ResizeLosslessBitReader(VP8Decoder* const dec,
|
||||
const uint8_t* const new_buf,
|
||||
size_t new_size) {
|
||||
VP8LDecoder* const vp8l_decoder = &dec->vp8l_decoder_;
|
||||
if (vp8l_decoder->br_.buf_ != NULL) {
|
||||
VP8LBitReaderResize(&vp8l_decoder->br_,
|
||||
new_buf + vp8l_decoder->br_offset_,
|
||||
new_size - vp8l_decoder->br_offset_);
|
||||
static void DoRemap(WebPIDecoder* const idec, ptrdiff_t offset) {
|
||||
MemBuffer* const mem = &idec->mem_;
|
||||
const uint8_t* const new_base = mem->buf_ + mem->start_;
|
||||
// note: for VP8, setting up idec->io_ is only really needed at the beginning
|
||||
// of the decoding, till partition #0 is complete.
|
||||
idec->io_.data = new_base;
|
||||
idec->io_.data_size = MemDataSize(mem);
|
||||
|
||||
if (idec->dec_ != NULL) {
|
||||
if (!idec->is_lossless_) {
|
||||
VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
|
||||
const int last_part = dec->num_parts_ - 1;
|
||||
if (offset != 0) {
|
||||
int p;
|
||||
for (p = 0; p <= last_part; ++p) {
|
||||
RemapBitReader(dec->parts_ + p, offset);
|
||||
}
|
||||
// Remap partition #0 data pointer to new offset, but only in MAP
|
||||
// mode (in APPEND mode, partition #0 is copied into a fixed memory).
|
||||
if (mem->mode_ == MEM_MODE_MAP) {
|
||||
RemapBitReader(&dec->br_, offset);
|
||||
}
|
||||
}
|
||||
assert(last_part >= 0);
|
||||
dec->parts_[last_part].buf_end_ = mem->buf_ + mem->end_;
|
||||
} else { // Resize lossless bitreader
|
||||
VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
|
||||
VP8LBitReaderSetBuffer(&dec->br_, new_base, MemDataSize(mem));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,103 +133,51 @@ static void ResizeLosslessBitReader(VP8Decoder* const dec,
|
||||
static int AppendToMemBuffer(WebPIDecoder* const idec,
|
||||
const uint8_t* const data, size_t data_size) {
|
||||
MemBuffer* const mem = &idec->mem_;
|
||||
VP8Decoder* const dec = idec->dec_;
|
||||
const int last_part = dec->num_parts_ - 1;
|
||||
const uint8_t* const old_base = mem->buf_ + mem->start_;
|
||||
assert(mem->mode_ == MEM_MODE_APPEND);
|
||||
|
||||
if (mem->end_ + data_size > mem->buf_size_) { // Need some free memory
|
||||
int p;
|
||||
const uint8_t* const base = mem->buf_ + mem->start_;
|
||||
const size_t new_size =
|
||||
(MemDataSize(mem) + data_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);
|
||||
uint8_t* const new_buf = (uint8_t*)malloc(new_size);
|
||||
|
||||
const size_t current_size = MemDataSize(mem);
|
||||
const size_t new_size = current_size + data_size;
|
||||
const size_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);
|
||||
uint8_t* const new_buf = (uint8_t*)malloc(extra_size);
|
||||
if (new_buf == NULL) return 0;
|
||||
memcpy(new_buf, base, MemDataSize(mem));
|
||||
|
||||
// adjust VP8BitReader pointers
|
||||
for (p = 0; p <= last_part; ++p) {
|
||||
if (dec->parts_[p].buf_) {
|
||||
REMAP(dec->parts_[p].buf_, base, new_buf);
|
||||
REMAP(dec->parts_[p].buf_end_, base, new_buf);
|
||||
}
|
||||
}
|
||||
|
||||
// adjust memory pointers
|
||||
memcpy(new_buf, old_base, current_size);
|
||||
free(mem->buf_);
|
||||
mem->buf_ = new_buf;
|
||||
mem->buf_size_ = new_size;
|
||||
|
||||
mem->end_ = MemDataSize(mem);
|
||||
mem->buf_size_ = extra_size;
|
||||
mem->start_ = 0;
|
||||
mem->end_ = current_size;
|
||||
}
|
||||
|
||||
memcpy(mem->buf_ + mem->end_, data, data_size);
|
||||
mem->end_ += data_size;
|
||||
assert(mem->end_ <= mem->buf_size_);
|
||||
assert(last_part >= 0);
|
||||
dec->parts_[last_part].buf_end_ = mem->buf_ + mem->end_;
|
||||
|
||||
// Lossless Bit Reader needs to be resized for every invocation of
|
||||
// WebPIAppend as buf_end_ is changing with every invocation.
|
||||
if (dec->is_lossless_) {
|
||||
VP8LDecoder* const vp8l_decoder = &dec->vp8l_decoder_;
|
||||
vp8l_decoder->br_offset_ = mem->start_;
|
||||
ResizeLosslessBitReader(dec, mem->buf_, mem->end_);
|
||||
}
|
||||
|
||||
// note: setting up idec->io_ is only really needed at the beginning
|
||||
// of the decoding, till partition #0 is complete.
|
||||
idec->io_.data = mem->buf_ + mem->start_;
|
||||
idec->io_.data_size = MemDataSize(mem);
|
||||
DoRemap(idec, mem->buf_ + mem->start_ - old_base);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int RemapMemBuffer(WebPIDecoder* const idec,
|
||||
const uint8_t* const data, size_t data_size) {
|
||||
int p;
|
||||
MemBuffer* const mem = &idec->mem_;
|
||||
VP8Decoder* const dec = idec->dec_;
|
||||
const int last_part = dec->num_parts_ - 1;
|
||||
const uint8_t* base = mem->buf_;
|
||||
|
||||
const uint8_t* const old_base = mem->buf_ + mem->start_;
|
||||
assert(mem->mode_ == MEM_MODE_MAP);
|
||||
if (data_size < mem->buf_size_) {
|
||||
return 0; // we cannot remap to a shorter buffer!
|
||||
}
|
||||
|
||||
if (!dec->is_lossless_) {
|
||||
for (p = 0; p <= last_part; ++p) {
|
||||
if (dec->parts_[p].buf_) {
|
||||
REMAP(dec->parts_[p].buf_, base, data);
|
||||
REMAP(dec->parts_[p].buf_end_, base, data);
|
||||
}
|
||||
}
|
||||
assert(last_part >= 0);
|
||||
dec->parts_[last_part].buf_end_ = data + data_size;
|
||||
|
||||
// Remap partition #0 data pointer to new offset.
|
||||
if (dec->br_.buf_) {
|
||||
REMAP(dec->br_.buf_, base, data);
|
||||
REMAP(dec->br_.buf_end_, base, data);
|
||||
}
|
||||
} else {
|
||||
ResizeLosslessBitReader(dec, data, data_size);
|
||||
}
|
||||
if (data_size < mem->buf_size_) return 0; // can't remap to a shorter buffer!
|
||||
|
||||
mem->buf_ = (uint8_t*)data;
|
||||
mem->end_ = mem->buf_size_ = data_size;
|
||||
|
||||
idec->io_.data = data;
|
||||
idec->io_.data_size = data_size;
|
||||
DoRemap(idec, mem->buf_ + mem->start_ - old_base);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void InitMemBuffer(MemBuffer* const mem) {
|
||||
mem->mode_ = MEM_MODE_NONE;
|
||||
mem->buf_ = 0;
|
||||
mem->buf_ = NULL;
|
||||
mem->buf_size_ = 0;
|
||||
mem->part0_buf_ = 0;
|
||||
mem->part0_buf_ = NULL;
|
||||
mem->part0_size_ = 0;
|
||||
}
|
||||
|
||||
@ -224,8 +199,6 @@ static int CheckMemBufferMode(MemBuffer* const mem, MemBufferMode expected) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#undef REMAP
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Macroblock-decoding contexts
|
||||
|
||||
@ -272,16 +245,19 @@ static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) {
|
||||
|
||||
static void ChangeState(WebPIDecoder* const idec, DecState new_state,
|
||||
uint32_t consumed_bytes) {
|
||||
MemBuffer* const mem = &idec->mem_;
|
||||
idec->state_ = new_state;
|
||||
idec->mem_.start_ += consumed_bytes;
|
||||
assert(idec->mem_.start_ <= idec->mem_.end_);
|
||||
mem->start_ += consumed_bytes;
|
||||
assert(mem->start_ <= mem->end_);
|
||||
idec->io_.data = mem->buf_ + mem->start_;
|
||||
idec->io_.data_size = MemDataSize(mem);
|
||||
}
|
||||
|
||||
// Headers
|
||||
static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {
|
||||
const uint8_t* data = idec->mem_.buf_ + idec->mem_.start_;
|
||||
VP8Decoder* const dec = idec->dec_;
|
||||
uint32_t curr_size = MemDataSize(&idec->mem_);
|
||||
MemBuffer* const mem = &idec->mem_;
|
||||
const uint8_t* data = mem->buf_ + mem->start_;
|
||||
uint32_t curr_size = MemDataSize(mem);
|
||||
VP8StatusCode status;
|
||||
WebPHeaderStructure headers;
|
||||
|
||||
@ -290,20 +266,36 @@ static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {
|
||||
status = WebPParseHeaders(&headers);
|
||||
if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
|
||||
return VP8_STATUS_SUSPENDED; // We haven't found a VP8 chunk yet.
|
||||
} else if (status == VP8_STATUS_OK) {
|
||||
idec->vp8_size_ = headers.vp8_size;
|
||||
dec->alpha_data_ = headers.alpha_data;
|
||||
dec->alpha_data_size_ = headers.alpha_data_size;
|
||||
dec->is_lossless_ = headers.is_lossless;
|
||||
if (!dec->is_lossless_) {
|
||||
ChangeState(idec, STATE_VP8_FRAME_HEADER, headers.offset);
|
||||
} else {
|
||||
ChangeState(idec, STATE_VP8L_HEADER, headers.offset);
|
||||
}
|
||||
return VP8_STATUS_OK;
|
||||
} else {
|
||||
} else if (status != VP8_STATUS_OK) {
|
||||
return IDecError(idec, status);
|
||||
}
|
||||
|
||||
idec->chunk_size_ = headers.vp8_size;
|
||||
idec->is_lossless_ = headers.is_lossless;
|
||||
if (!idec->is_lossless_) {
|
||||
VP8Decoder* const dec = VP8New();
|
||||
if (dec == NULL) {
|
||||
return VP8_STATUS_OUT_OF_MEMORY;
|
||||
}
|
||||
idec->dec_ = dec;
|
||||
#ifdef WEBP_USE_THREAD
|
||||
dec->use_threads_ = (idec->params_.options != NULL) &&
|
||||
(idec->params_.options->use_threads > 0);
|
||||
#else
|
||||
dec->use_threads_ = 0;
|
||||
#endif
|
||||
dec->alpha_data_ = headers.alpha_data;
|
||||
dec->alpha_data_size_ = headers.alpha_data_size;
|
||||
ChangeState(idec, STATE_VP8_FRAME_HEADER, headers.offset);
|
||||
} else {
|
||||
VP8LDecoder* const dec = VP8LNew();
|
||||
if (dec == NULL) {
|
||||
return VP8_STATUS_OUT_OF_MEMORY;
|
||||
}
|
||||
idec->dec_ = dec;
|
||||
ChangeState(idec, STATE_VP8L_HEADER, headers.offset);
|
||||
}
|
||||
return VP8_STATUS_OK;
|
||||
}
|
||||
|
||||
static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {
|
||||
@ -315,24 +307,26 @@ static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {
|
||||
// Not enough data bytes to extract VP8 Frame Header.
|
||||
return VP8_STATUS_SUSPENDED;
|
||||
}
|
||||
if (!VP8GetInfo(data, curr_size, idec->vp8_size_, NULL, NULL)) {
|
||||
if (!VP8GetInfo(data, curr_size, idec->chunk_size_, NULL, NULL)) {
|
||||
return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
|
||||
}
|
||||
|
||||
bits = data[0] | (data[1] << 8) | (data[2] << 16);
|
||||
idec->mem_.part0_size_ = (bits >> 5) + VP8_FRAME_HEADER_SIZE;
|
||||
|
||||
idec->io_.data_size = curr_size;
|
||||
idec->io_.data = data;
|
||||
idec->io_.data_size = curr_size;
|
||||
idec->state_ = STATE_VP8_PARTS0;
|
||||
return VP8_STATUS_OK;
|
||||
}
|
||||
|
||||
// Partition #0
|
||||
static int CopyParts0Data(WebPIDecoder* idec) {
|
||||
VP8BitReader* const br = &idec->dec_->br_;
|
||||
static int CopyParts0Data(WebPIDecoder* const idec) {
|
||||
VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
|
||||
VP8BitReader* const br = &dec->br_;
|
||||
const size_t psize = br->buf_end_ - br->buf_;
|
||||
MemBuffer* const mem = &idec->mem_;
|
||||
assert(!idec->is_lossless_);
|
||||
assert(!mem->part0_buf_);
|
||||
assert(psize > 0);
|
||||
assert(psize <= mem->part0_size_);
|
||||
@ -354,7 +348,7 @@ static int CopyParts0Data(WebPIDecoder* idec) {
|
||||
}
|
||||
|
||||
static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {
|
||||
VP8Decoder* const dec = idec->dec_;
|
||||
VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
|
||||
VP8Io* const io = &idec->io_;
|
||||
const WebPDecParams* const params = &idec->params_;
|
||||
WebPDecBuffer* const output = params->output;
|
||||
@ -402,7 +396,7 @@ static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {
|
||||
|
||||
// Remaining partitions
|
||||
static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) {
|
||||
VP8Decoder* const dec = idec->dec_;
|
||||
VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
|
||||
VP8Io* const io = &idec->io_;
|
||||
|
||||
assert(dec->ready_);
|
||||
@ -458,28 +452,18 @@ static int ErrorStatusLossless(WebPIDecoder* const idec, VP8StatusCode status) {
|
||||
|
||||
static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {
|
||||
VP8Io* const io = &idec->io_;
|
||||
VP8Decoder* const dec = idec->dec_;
|
||||
VP8LDecoder* const vp8l_decoder = &dec->vp8l_decoder_;
|
||||
VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
|
||||
const WebPDecParams* const params = &idec->params_;
|
||||
WebPDecBuffer* const output = params->output;
|
||||
uint32_t curr_size = MemDataSize(&idec->mem_);
|
||||
assert(idec->is_lossless_);
|
||||
|
||||
// Wait until there's enough data for decoding header.
|
||||
if (curr_size < (idec->vp8_size_ >> 3)) {
|
||||
if (curr_size < (idec->chunk_size_ >> 3)) {
|
||||
return VP8_STATUS_SUSPENDED;
|
||||
}
|
||||
|
||||
VP8LInitDecoder(vp8l_decoder);
|
||||
// start_ corresponds to bytes consumed in the last state (parsing RIFF
|
||||
// header). For Update mode, io->data is set to mem_.buf_ and hence require an
|
||||
// offset corresponding to start_ in BitReader. In Append mode, io->data
|
||||
// is already start_ offset over mem_.buf_, so doesn't require additional
|
||||
// offset in BitReader.
|
||||
vp8l_decoder->br_offset_ =
|
||||
(io->data == idec->mem_.buf_) ? idec->mem_.start_ : 0;
|
||||
if (!VP8LDecodeHeader(vp8l_decoder, io)) {
|
||||
VP8LClear(vp8l_decoder);
|
||||
return ErrorStatusLossless(idec, vp8l_decoder->status_);
|
||||
if (!VP8LDecodeHeader(dec, io)) {
|
||||
return ErrorStatusLossless(idec, dec->status_);
|
||||
}
|
||||
// Allocate/verify output buffer now.
|
||||
dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
|
||||
@ -493,18 +477,18 @@ static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {
|
||||
}
|
||||
|
||||
static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {
|
||||
VP8Decoder* const dec = idec->dec_;
|
||||
VP8LDecoder* const vp8l_decoder = &dec->vp8l_decoder_;
|
||||
VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
|
||||
const uint32_t curr_size = MemDataSize(&idec->mem_);
|
||||
assert(idec->is_lossless_);
|
||||
|
||||
// At present Lossless decoder can't decode image incrementally. So wait till
|
||||
// all the image data is aggregated before image can be decoded.
|
||||
if (curr_size < idec->vp8_size_) {
|
||||
if (curr_size < idec->chunk_size_) {
|
||||
return VP8_STATUS_SUSPENDED;
|
||||
}
|
||||
|
||||
if (!VP8LDecodeImage(vp8l_decoder)) {
|
||||
return ErrorStatusLossless(idec, vp8l_decoder->status_);
|
||||
if (!VP8LDecodeImage(dec)) {
|
||||
return ErrorStatusLossless(idec, dec->status_);
|
||||
}
|
||||
|
||||
idec->state_ = STATE_DONE;
|
||||
@ -515,10 +499,13 @@ static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {
|
||||
// Main decoding loop
|
||||
static VP8StatusCode IDecode(WebPIDecoder* idec) {
|
||||
VP8StatusCode status = VP8_STATUS_SUSPENDED;
|
||||
assert(idec->dec_);
|
||||
|
||||
if (idec->state_ == STATE_PRE_VP8) {
|
||||
status = DecodeWebPHeaders(idec);
|
||||
} else {
|
||||
if (idec->dec_ == NULL) {
|
||||
return VP8_STATUS_SUSPENDED; // can't continue if we have no decoder.
|
||||
}
|
||||
}
|
||||
if (idec->state_ == STATE_VP8_FRAME_HEADER) {
|
||||
status = DecodeVP8FrameHeader(idec);
|
||||
@ -547,13 +534,8 @@ WebPIDecoder* WebPINewDecoder(WebPDecBuffer* const output_buffer) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
idec->dec_ = VP8New();
|
||||
if (idec->dec_ == NULL) {
|
||||
free(idec);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
idec->state_ = STATE_PRE_VP8;
|
||||
idec->chunk_size_ = 0;
|
||||
|
||||
InitMemBuffer(&idec->mem_);
|
||||
WebPInitDecBuffer(&idec->output_);
|
||||
@ -563,14 +545,6 @@ WebPIDecoder* WebPINewDecoder(WebPDecBuffer* const output_buffer) {
|
||||
idec->params_.output = output_buffer ? output_buffer : &idec->output_;
|
||||
WebPInitCustomIo(&idec->params_, &idec->io_); // Plug the I/O functions.
|
||||
|
||||
#ifdef WEBP_USE_THREAD
|
||||
idec->dec_->use_threads_ = idec->params_.options &&
|
||||
(idec->params_.options->use_threads > 0);
|
||||
#else
|
||||
idec->dec_->use_threads_ = 0;
|
||||
#endif
|
||||
idec->vp8_size_ = 0;
|
||||
|
||||
return idec;
|
||||
}
|
||||
|
||||
@ -597,8 +571,14 @@ WebPIDecoder* WebPIDecode(const uint8_t* data, uint32_t data_size,
|
||||
}
|
||||
|
||||
void WebPIDelete(WebPIDecoder* const idec) {
|
||||
if (!idec) return;
|
||||
VP8Delete(idec->dec_);
|
||||
if (idec == NULL) return;
|
||||
if (idec->dec_ != NULL) {
|
||||
if (!idec->is_lossless_) {
|
||||
VP8Delete(idec->dec_);
|
||||
} else {
|
||||
VP8LDelete(idec->dec_);
|
||||
}
|
||||
}
|
||||
ClearMemBuffer(&idec->mem_);
|
||||
WebPFreeDecBuffer(&idec->output_);
|
||||
free(idec);
|
||||
@ -651,9 +631,6 @@ WebPIDecoder* WebPINewYUV(uint8_t* luma, int luma_size, int luma_stride,
|
||||
|
||||
static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {
|
||||
assert(idec);
|
||||
if (idec->dec_ == NULL) {
|
||||
return VP8_STATUS_USER_ABORT;
|
||||
}
|
||||
if (idec->state_ == STATE_ERROR) {
|
||||
return VP8_STATUS_BITSTREAM_ERROR;
|
||||
}
|
||||
@ -663,8 +640,8 @@ static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {
|
||||
return VP8_STATUS_SUSPENDED;
|
||||
}
|
||||
|
||||
VP8StatusCode WebPIAppend(WebPIDecoder* const idec, const uint8_t* data,
|
||||
uint32_t data_size) {
|
||||
VP8StatusCode WebPIAppend(WebPIDecoder* const idec,
|
||||
const uint8_t* const data, uint32_t data_size) {
|
||||
VP8StatusCode status;
|
||||
if (idec == NULL || data == NULL) {
|
||||
return VP8_STATUS_INVALID_PARAM;
|
||||
@ -684,8 +661,8 @@ VP8StatusCode WebPIAppend(WebPIDecoder* const idec, const uint8_t* data,
|
||||
return IDecode(idec);
|
||||
}
|
||||
|
||||
VP8StatusCode WebPIUpdate(WebPIDecoder* const idec, const uint8_t* data,
|
||||
uint32_t data_size) {
|
||||
VP8StatusCode WebPIUpdate(WebPIDecoder* const idec,
|
||||
const uint8_t* const data, uint32_t data_size) {
|
||||
VP8StatusCode status;
|
||||
if (idec == NULL || data == NULL) {
|
||||
return VP8_STATUS_INVALID_PARAM;
|
||||
@ -708,7 +685,10 @@ VP8StatusCode WebPIUpdate(WebPIDecoder* const idec, const uint8_t* data,
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
static const WebPDecBuffer* GetOutputBuffer(const WebPIDecoder* const idec) {
|
||||
if (!idec || !idec->dec_ || idec->state_ <= STATE_VP8_PARTS0) {
|
||||
if (idec == NULL || idec->dec_ == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (idec->state_ <= STATE_VP8_PARTS0) {
|
||||
return NULL;
|
||||
}
|
||||
return idec->params_.output;
|
||||
@ -772,7 +752,7 @@ int WebPISetIOHooks(WebPIDecoder* const idec,
|
||||
VP8IoSetupHook setup,
|
||||
VP8IoTeardownHook teardown,
|
||||
void* user_data) {
|
||||
if (!idec || !idec->dec_ || idec->state_ > STATE_PRE_VP8) {
|
||||
if (idec == NULL || idec->state_ > STATE_PRE_VP8) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -260,28 +260,16 @@ int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
|
||||
"null VP8Io passed to VP8GetHeaders()");
|
||||
}
|
||||
|
||||
// Process Pre-VP8 chunks.
|
||||
headers.data = io->data;
|
||||
headers.data_size = io->data_size;
|
||||
|
||||
// Process Pre-VP8 chunks.
|
||||
status = WebPParseHeaders(&headers);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return VP8SetError(dec, status, "Incorrect/incomplete header.");
|
||||
}
|
||||
if (headers.is_lossless) {
|
||||
int ok;
|
||||
VP8LDecoder* const vp8l_decoder = &dec->vp8l_decoder_;
|
||||
VP8LInitDecoder(vp8l_decoder);
|
||||
vp8l_decoder->br_offset_ = headers.offset;
|
||||
ok = VP8LDecodeHeader(vp8l_decoder, io);
|
||||
if (!ok) {
|
||||
VP8LClear(vp8l_decoder);
|
||||
return VP8SetError(dec, vp8l_decoder->status_,
|
||||
"Incorrect/incomplete header.");
|
||||
} else {
|
||||
dec->is_lossless_ = 1;
|
||||
}
|
||||
return ok;
|
||||
return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
|
||||
"Unexpected lossless format encountered.");
|
||||
}
|
||||
|
||||
if (dec->alpha_data_ == NULL) {
|
||||
@ -741,9 +729,6 @@ int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
|
||||
}
|
||||
assert(dec->ready_);
|
||||
|
||||
// TODO: Implement lossless decoding. Error till then.
|
||||
if (dec->is_lossless_) goto Error;
|
||||
|
||||
// Finish setting up the decoding parameter. Will call io->setup().
|
||||
ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
|
||||
if (ok) { // good to go.
|
||||
@ -757,7 +742,6 @@ int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
|
||||
ok &= VP8ExitCritical(dec, io);
|
||||
}
|
||||
|
||||
Error:
|
||||
if (!ok) {
|
||||
VP8Clear(dec);
|
||||
return 0;
|
||||
|
@ -281,9 +281,6 @@ struct VP8Decoder {
|
||||
int layer_colorspace_;
|
||||
const uint8_t* layer_data_; // compressed layer data (if present)
|
||||
size_t layer_data_size_;
|
||||
|
||||
int is_lossless_;
|
||||
VP8LDecoder vp8l_decoder_;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -368,7 +368,7 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
|
||||
alphabet_size += color_cache_size;
|
||||
}
|
||||
ok = ReadHuffmanCode(alphabet_size, dec, &htrees[i]);
|
||||
ok = ok & !br->error_;
|
||||
ok = ok && !br->error_;
|
||||
}
|
||||
|
||||
if (ok) { // finalize pointers and return
|
||||
@ -382,7 +382,7 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
|
||||
Error:
|
||||
free(huffman_image);
|
||||
free(meta_codes);
|
||||
if (htrees) {
|
||||
if (htrees != NULL) {
|
||||
for (i = 0; i < num_htrees; ++i) {
|
||||
HuffmanTreeRelease(&htrees[i]);
|
||||
}
|
||||
@ -814,19 +814,36 @@ static void ClearMetadata(VP8LMetadata* const hdr) {
|
||||
// -----------------------------------------------------------------------------
|
||||
// VP8LDecoder
|
||||
|
||||
void VP8LInitDecoder(VP8LDecoder* const dec) {
|
||||
assert(dec);
|
||||
memset(dec, 0, sizeof(*dec));
|
||||
}
|
||||
|
||||
VP8LDecoder* VP8LNew(void) {
|
||||
VP8LDecoder* const dec = (VP8LDecoder*)malloc(sizeof(*dec));
|
||||
if (dec != NULL) VP8LInitDecoder(dec);
|
||||
VP8LDecoder* const dec = (VP8LDecoder*)calloc(1, sizeof(*dec));
|
||||
if (dec == NULL) return NULL;
|
||||
dec->status_ = VP8_STATUS_OK;
|
||||
dec->action_ = READ_DIM;
|
||||
dec->state_ = READ_DIM;
|
||||
return dec;
|
||||
}
|
||||
|
||||
void VP8LClear(VP8LDecoder* const dec) {
|
||||
int i;
|
||||
if (dec == NULL) return;
|
||||
ClearMetadata(&dec->hdr_);
|
||||
|
||||
free(dec->argb_);
|
||||
dec->argb_ = NULL;
|
||||
for (i = 0; i < dec->next_transform_; ++i) {
|
||||
ClearTransform(&dec->transforms_[i]);
|
||||
}
|
||||
dec->next_transform_ = 0;
|
||||
|
||||
free(dec->rescaler_memory);
|
||||
dec->rescaler_memory = NULL;
|
||||
}
|
||||
|
||||
void VP8LDelete(VP8LDecoder* const dec) {
|
||||
free(dec);
|
||||
if (dec != NULL) {
|
||||
VP8LClear(dec);
|
||||
free(dec);
|
||||
}
|
||||
}
|
||||
|
||||
static void UpdateDecoder(VP8LDecoder* const dec, int width, int height) {
|
||||
@ -925,18 +942,17 @@ int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
|
||||
uint32_t* decoded_data = NULL;
|
||||
|
||||
if (dec == NULL) return 0;
|
||||
if (io == NULL || dec->br_offset_ > io->data_size) {
|
||||
if (io == NULL) {
|
||||
dec->status_ = VP8_STATUS_INVALID_PARAM;
|
||||
return 0;
|
||||
}
|
||||
|
||||
dec->io_ = io;
|
||||
dec->status_ = VP8_STATUS_OK;
|
||||
VP8LInitBitReader(&dec->br_, io->data + dec->br_offset_,
|
||||
io->data_size - dec->br_offset_);
|
||||
VP8LInitBitReader(&dec->br_, io->data, io->data_size);
|
||||
if (!ReadImageSize(&dec->br_, &width, &height)) {
|
||||
dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
|
||||
return 0;
|
||||
goto Error;
|
||||
}
|
||||
dec->state_ = READ_DIM;
|
||||
io->width = width;
|
||||
@ -945,11 +961,14 @@ int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
|
||||
dec->action_ = READ_HDR;
|
||||
if (!DecodeImageStream(width, height, 1, dec, &decoded_data)) {
|
||||
free(decoded_data);
|
||||
VP8LClear(dec);
|
||||
assert(dec->status_ != VP8_STATUS_OK);
|
||||
return 0;
|
||||
goto Error;
|
||||
}
|
||||
return 1;
|
||||
|
||||
Error:
|
||||
VP8LClear(dec);
|
||||
assert(dec->status_ != VP8_STATUS_OK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int VP8LDecodeImage(VP8LDecoder* const dec) {
|
||||
@ -1015,22 +1034,6 @@ int VP8LDecodeImage(VP8LDecoder* const dec) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void VP8LClear(VP8LDecoder* const dec) {
|
||||
int i;
|
||||
if (dec == NULL) return;
|
||||
ClearMetadata(&dec->hdr_);
|
||||
|
||||
free(dec->argb_);
|
||||
dec->argb_ = NULL;
|
||||
for (i = 0; i < dec->next_transform_; ++i) {
|
||||
ClearTransform(&dec->transforms_[i]);
|
||||
}
|
||||
dec->next_transform_ = 0;
|
||||
|
||||
free(dec->rescaler_memory);
|
||||
dec->rescaler_memory = NULL;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
|
@ -78,7 +78,6 @@ typedef struct {
|
||||
uint32_t *argb_cache_; // Scratch buffer for temporary BGRA storage.
|
||||
|
||||
BitReader br_;
|
||||
uint32_t br_offset_;
|
||||
|
||||
int width_;
|
||||
int height_;
|
||||
@ -109,9 +108,6 @@ int VP8LGetInfo(const uint8_t* data,
|
||||
// Allocates and initialize a new lossless decoder instance.
|
||||
VP8LDecoder* VP8LNew(void);
|
||||
|
||||
// Initializes the decoder object.
|
||||
void VP8LInitDecoder(VP8LDecoder* const dec);
|
||||
|
||||
// Decodes the image header. Returns false in case of error.
|
||||
int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io);
|
||||
|
||||
@ -120,6 +116,7 @@ int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io);
|
||||
int VP8LDecodeImage(VP8LDecoder* const dec);
|
||||
|
||||
// Resets the decoder in its initial state, reclaiming memory.
|
||||
// Preserves the dec->status_ value.
|
||||
void VP8LClear(VP8LDecoder* const dec);
|
||||
|
||||
// Clears and deallocate a lossless decoder instance.
|
||||
|
@ -298,50 +298,70 @@ void WebPResetDecParams(WebPDecParams* const params) {
|
||||
// Main flow
|
||||
static VP8StatusCode DecodeInto(const uint8_t* data, uint32_t data_size,
|
||||
WebPDecParams* const params) {
|
||||
VP8Decoder* dec = VP8New();
|
||||
VP8StatusCode status = VP8_STATUS_OK;
|
||||
VP8StatusCode status;
|
||||
VP8Io io;
|
||||
WebPHeaderStructure headers;
|
||||
|
||||
assert(params);
|
||||
if (dec == NULL) {
|
||||
return VP8_STATUS_INVALID_PARAM;
|
||||
headers.data = data;
|
||||
headers.data_size = data_size;
|
||||
status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.
|
||||
if (status != VP8_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
assert(params);
|
||||
VP8InitIo(&io);
|
||||
io.data = data;
|
||||
io.data_size = data_size;
|
||||
io.data = headers.data + headers.offset;
|
||||
io.data_size = headers.data_size - headers.offset;
|
||||
WebPInitCustomIo(params, &io); // Plug the I/O functions.
|
||||
|
||||
if (!headers.is_lossless) {
|
||||
VP8Decoder* const dec = VP8New();
|
||||
if (dec == NULL) {
|
||||
return VP8_STATUS_OUT_OF_MEMORY;
|
||||
}
|
||||
#ifdef WEBP_USE_THREAD
|
||||
dec->use_threads_ = params->options && (params->options->use_threads > 0);
|
||||
dec->use_threads_ = params->options && (params->options->use_threads > 0);
|
||||
#else
|
||||
dec->use_threads_ = 0;
|
||||
dec->use_threads_ = 0;
|
||||
#endif
|
||||
dec->alpha_data_ = headers.alpha_data;
|
||||
dec->alpha_data_size_ = headers.alpha_data_size;
|
||||
|
||||
// Decode bitstream header, update io->width/io->height.
|
||||
if (!VP8GetHeaders(dec, &io)) {
|
||||
status = VP8_STATUS_BITSTREAM_ERROR;
|
||||
} else {
|
||||
// Allocate/check output buffers.
|
||||
status = WebPAllocateDecBuffer(io.width, io.height, params->options,
|
||||
params->output);
|
||||
if (status == VP8_STATUS_OK) {
|
||||
// Decode
|
||||
if (!dec->is_lossless_) {
|
||||
// Decode bitstream header, update io->width/io->height.
|
||||
if (!VP8GetHeaders(dec, &io)) {
|
||||
status = dec->status_; // An error occurred. Grab error status.
|
||||
} else {
|
||||
// Allocate/check output buffers.
|
||||
status = WebPAllocateDecBuffer(io.width, io.height, params->options,
|
||||
params->output);
|
||||
if (status == VP8_STATUS_OK) { // Decode
|
||||
if (!VP8Decode(dec, &io)) {
|
||||
status = dec->status_;
|
||||
}
|
||||
} else {
|
||||
VP8LDecoder* const vp8l_decoder = &dec->vp8l_decoder_;
|
||||
if (!VP8LDecodeImage(vp8l_decoder)) {
|
||||
status = VP8_STATUS_BITSTREAM_ERROR;
|
||||
}
|
||||
}
|
||||
VP8Delete(dec);
|
||||
} else {
|
||||
VP8LDecoder* const dec = VP8LNew();
|
||||
if (dec == NULL) {
|
||||
return VP8_STATUS_OUT_OF_MEMORY;
|
||||
}
|
||||
if (!VP8LDecodeHeader(dec, &io)) {
|
||||
status = dec->status_; // An error occurred. Grab error status.
|
||||
} else {
|
||||
// Allocate/check output buffers.
|
||||
status = WebPAllocateDecBuffer(io.width, io.height, params->options,
|
||||
params->output);
|
||||
if (status == VP8_STATUS_OK) { // Decode
|
||||
if (!VP8LDecodeImage(dec)) {
|
||||
status = dec->status_;
|
||||
}
|
||||
}
|
||||
} else if (dec->is_lossless_) { // Clear lossless decoder on error.
|
||||
VP8LClear(&dec->vp8l_decoder_);
|
||||
}
|
||||
VP8LDelete(dec);
|
||||
}
|
||||
VP8Delete(dec);
|
||||
|
||||
if (status != VP8_STATUS_OK) {
|
||||
WebPFreeDecBuffer(params->output);
|
||||
}
|
||||
|
@ -122,12 +122,11 @@ void VP8LInitBitReader(BitReader* const br,
|
||||
}
|
||||
}
|
||||
|
||||
void VP8LBitReaderResize(BitReader* const br,
|
||||
const uint8_t* const new_start,
|
||||
size_t new_length) {
|
||||
if (new_length > br->len_) br->eos_ = 0;
|
||||
br->buf_ = new_start;
|
||||
br->len_ = new_length;
|
||||
void VP8LBitReaderSetBuffer(BitReader* const br,
|
||||
const uint8_t* const buf, size_t len) {
|
||||
br->eos_ = (br->pos_ >= len);
|
||||
br->buf_ = buf;
|
||||
br->len_ = len;
|
||||
}
|
||||
|
||||
static void ShiftBytes(BitReader* const br) {
|
||||
|
@ -168,10 +168,9 @@ void VP8LInitBitReader(BitReader* const br,
|
||||
const uint8_t* const start,
|
||||
size_t length);
|
||||
|
||||
// Resizes the BitReader corresponding to resized read buffer.
|
||||
void VP8LBitReaderResize(BitReader* const br,
|
||||
const uint8_t* const new_start,
|
||||
size_t new_length);
|
||||
// Sets a new data buffer.
|
||||
void VP8LBitReaderSetBuffer(BitReader* const br,
|
||||
const uint8_t* const buffer, size_t length);
|
||||
|
||||
// Reads the specified number of bits from Read Buffer.
|
||||
// Flags an error in case end_of_stream or n_bits is more than allowed limit.
|
||||
|
Loading…
Reference in New Issue
Block a user