Index: third_party/libwebp/dec/idec.c |
diff --git a/third_party/libwebp/dec/idec.c b/third_party/libwebp/dec/idec.c |
index 3c773dd361a402943eb17933a7d6b9fcf0f03a2a..7df790ced86aa8940df4ab7994fdf1fa39760838 100644 |
--- a/third_party/libwebp/dec/idec.c |
+++ b/third_party/libwebp/dec/idec.c |
@@ -1,4 +1,4 @@ |
-// Copyright 2011 Google Inc. |
+// Copyright 2011 Google Inc. All Rights Reserved. |
// |
// This code is licensed under the same terms as WebM: |
// Software License Agreement: http://www.webmproject.org/license/software/ |
@@ -13,13 +13,16 @@ |
#include <string.h> |
#include <stdlib.h> |
-#include "webpi.h" |
-#include "vp8i.h" |
+#include "./webpi.h" |
+#include "./vp8i.h" |
+#include "../utils/utils.h" |
#if defined(__cplusplus) || defined(c_plusplus) |
extern "C" { |
#endif |
+// In append mode, buffer allocations increase as multiples of this value. |
+// Needs to be a power of 2. |
#define CHUNK_SIZE 4096 |
#define MAX_MB_SIZE 4096 |
@@ -33,6 +36,8 @@ typedef enum { |
STATE_VP8_FRAME_HEADER, // For VP8 Frame header (within VP8 chunk). |
STATE_VP8_PARTS0, |
STATE_VP8_DATA, |
+ STATE_VP8L_HEADER, |
+ STATE_VP8L_DATA, |
STATE_DONE, |
STATE_ERROR |
} DecState; |
@@ -47,8 +52,8 @@ typedef enum { |
// storage for partition #0 and partial data (in a rolling fashion) |
typedef struct { |
MemBufferMode mode_; // Operation mode |
- uint32_t start_; // start location of the data to be decoded |
- uint32_t end_; // end location |
+ size_t start_; // start location of the data to be decoded |
+ size_t end_; // end location |
size_t buf_size_; // size of the allocated buffer |
uint8_t* buf_; // We don't own this buffer in case WebPIUpdate() |
@@ -59,12 +64,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. |
+ size_t chunk_size_; // Compressed VP8/VP8L size extracted from Header. |
}; |
// MB context to restore in case VP8DecodeMB() fails |
@@ -80,104 +86,105 @@ 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 inline size_t MemDataSize(const MemBuffer* mem) { |
+static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) { |
return (mem->end_ - mem->start_); |
} |
+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)); |
+ } |
+ } |
+} |
+ |
// Appends data to the end of MemBuffer->buf_. It expands the allocated memory |
// size if required and also updates VP8BitReader's if new memory is allocated. |
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 (data_size > MAX_CHUNK_PAYLOAD) { |
+ // security safeguard: trying to allocate more than what the format |
+ // allows for a chunk should be considered a smoke smell. |
+ return 0; |
+ } |
if (mem->end_ + data_size > mem->buf_size_) { // Need some free memory |
- int p; |
- uint8_t* new_buf = NULL; |
- const int num_chunks = (MemDataSize(mem) + data_size + CHUNK_SIZE - 1) |
- / CHUNK_SIZE; |
- const size_t new_size = num_chunks * CHUNK_SIZE; |
- const uint8_t* const base = mem->buf_ + mem->start_; |
- |
- new_buf = (uint8_t*)malloc(new_size); |
- if (!new_buf) 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 |
+ const size_t current_size = MemDataSize(mem); |
+ const uint64_t new_size = (uint64_t)current_size + data_size; |
+ const uint64_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1); |
+ uint8_t* const new_buf = |
+ (uint8_t*)WebPSafeMalloc(extra_size, sizeof(*new_buf)); |
+ if (new_buf == NULL) return 0; |
+ 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_ = (size_t)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_; |
- // 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! |
- } |
- |
- 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); |
- } |
+ 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; |
} |
@@ -199,8 +206,6 @@ static int CheckMemBufferMode(MemBuffer* const mem, MemBufferMode expected) { |
return 1; |
} |
-#undef REMAP |
- |
//------------------------------------------------------------------------------ |
// Macroblock-decoding contexts |
@@ -246,66 +251,96 @@ static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) { |
} |
static void ChangeState(WebPIDecoder* const idec, DecState new_state, |
- uint32_t consumed_bytes) { |
+ size_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_; |
- uint32_t curr_size = MemDataSize(&idec->mem_); |
- uint32_t vp8_size; |
- uint32_t bytes_skipped; |
+ MemBuffer* const mem = &idec->mem_; |
+ const uint8_t* data = mem->buf_ + mem->start_; |
+ size_t curr_size = MemDataSize(mem); |
VP8StatusCode status; |
+ WebPHeaderStructure headers; |
- status = WebPParseHeaders(&data, &curr_size, &vp8_size, &bytes_skipped); |
+ headers.data = data; |
+ headers.data_size = curr_size; |
+ 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_ = vp8_size; |
- ChangeState(idec, STATE_VP8_FRAME_HEADER, bytes_skipped); |
- return VP8_STATUS_OK; // We have skipped all pre-VP8 chunks. |
- } else { |
+ } else if (status != VP8_STATUS_OK) { |
return IDecError(idec, status); |
} |
+ |
+ idec->chunk_size_ = headers.compressed_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) { |
const uint8_t* data = idec->mem_.buf_ + idec->mem_.start_; |
- const uint32_t curr_size = MemDataSize(&idec->mem_); |
+ const size_t curr_size = MemDataSize(&idec->mem_); |
uint32_t bits; |
if (curr_size < VP8_FRAME_HEADER_SIZE) { |
// Not enough data bytes to extract VP8 Frame Header. |
return VP8_STATUS_SUSPENDED; |
} |
- if (!VP8GetInfo(data, curr_size, idec->vp8_size_, NULL, 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(!mem->part0_buf_); |
+ assert(!idec->is_lossless_); |
+ assert(mem->part0_buf_ == NULL); |
assert(psize > 0); |
- assert(psize <= mem->part0_size_); |
+ assert(psize <= mem->part0_size_); // Format limit: no need for runtime check |
if (mem->mode_ == MEM_MODE_APPEND) { |
// We copy and grab ownership of the partition #0 data. |
uint8_t* const part0_buf = (uint8_t*)malloc(psize); |
- if (!part0_buf) { |
+ if (part0_buf == NULL) { |
return 0; |
} |
memcpy(part0_buf, br->buf_, psize); |
@@ -320,7 +355,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; |
@@ -368,13 +403,11 @@ static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) { |
// Remaining partitions |
static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) { |
- VP8BitReader* br; |
- VP8Decoder* const dec = idec->dec_; |
+ VP8Decoder* const dec = (VP8Decoder*)idec->dec_; |
VP8Io* const io = &idec->io_; |
assert(dec->ready_); |
- br = &dec->br_; |
for (; dec->mb_y_ < dec->mb_h_; ++dec->mb_y_) { |
VP8BitReader* token_br = &dec->parts_[dec->mb_y_ & (dec->num_parts_ - 1)]; |
if (dec->mb_x_ == 0) { |
@@ -417,13 +450,69 @@ static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) { |
return VP8_STATUS_OK; |
} |
+static int ErrorStatusLossless(WebPIDecoder* const idec, VP8StatusCode status) { |
+ if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_NOT_ENOUGH_DATA) { |
+ return VP8_STATUS_SUSPENDED; |
+ } |
+ return IDecError(idec, status); |
+} |
+ |
+static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) { |
+ VP8Io* const io = &idec->io_; |
+ VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_; |
+ const WebPDecParams* const params = &idec->params_; |
+ WebPDecBuffer* const output = params->output; |
+ size_t curr_size = MemDataSize(&idec->mem_); |
+ assert(idec->is_lossless_); |
+ |
+ // Wait until there's enough data for decoding header. |
+ if (curr_size < (idec->chunk_size_ >> 3)) { |
+ return VP8_STATUS_SUSPENDED; |
+ } |
+ if (!VP8LDecodeHeader(dec, io)) { |
+ return ErrorStatusLossless(idec, dec->status_); |
+ } |
+ // Allocate/verify output buffer now. |
+ dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options, |
+ output); |
+ if (dec->status_ != VP8_STATUS_OK) { |
+ return IDecError(idec, dec->status_); |
+ } |
+ |
+ idec->state_ = STATE_VP8L_DATA; |
+ return VP8_STATUS_OK; |
+} |
+ |
+static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) { |
+ VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_; |
+ const size_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->chunk_size_) { |
+ return VP8_STATUS_SUSPENDED; |
+ } |
+ |
+ if (!VP8LDecodeImage(dec)) { |
+ return ErrorStatusLossless(idec, dec->status_); |
+ } |
+ |
+ idec->state_ = STATE_DONE; |
+ |
+ return VP8_STATUS_OK; |
+} |
+ |
// 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); |
@@ -434,25 +523,26 @@ static VP8StatusCode IDecode(WebPIDecoder* idec) { |
if (idec->state_ == STATE_VP8_DATA) { |
status = DecodeRemaining(idec); |
} |
+ if (idec->state_ == STATE_VP8L_HEADER) { |
+ status = DecodeVP8LHeader(idec); |
+ } |
+ if (idec->state_ == STATE_VP8L_DATA) { |
+ status = DecodeVP8LData(idec); |
+ } |
return status; |
} |
//------------------------------------------------------------------------------ |
// Public functions |
-WebPIDecoder* WebPINewDecoder(WebPDecBuffer* const output_buffer) { |
- WebPIDecoder* idec = (WebPIDecoder*)calloc(1, sizeof(WebPIDecoder)); |
+WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer) { |
+ WebPIDecoder* idec = (WebPIDecoder*)calloc(1, sizeof(*idec)); |
if (idec == NULL) { |
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_); |
@@ -462,19 +552,11 @@ 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; |
} |
-WebPIDecoder* WebPIDecode(const uint8_t* data, uint32_t data_size, |
- WebPDecoderConfig* const config) { |
+WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size, |
+ WebPDecoderConfig* config) { |
WebPIDecoder* idec; |
// Parse the bitstream's features, if requested: |
@@ -485,7 +567,7 @@ WebPIDecoder* WebPIDecode(const uint8_t* data, uint32_t data_size, |
} |
// Create an instance of the incremental decoder |
idec = WebPINewDecoder(config ? &config->output : NULL); |
- if (!idec) { |
+ if (idec == NULL) { |
return NULL; |
} |
// Finish initialization |
@@ -495,9 +577,15 @@ WebPIDecoder* WebPIDecode(const uint8_t* data, uint32_t data_size, |
return idec; |
} |
-void WebPIDelete(WebPIDecoder* const idec) { |
- if (!idec) return; |
- VP8Delete(idec->dec_); |
+void WebPIDelete(WebPIDecoder* idec) { |
+ 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); |
@@ -506,19 +594,12 @@ void WebPIDelete(WebPIDecoder* const idec) { |
//------------------------------------------------------------------------------ |
// Wrapper toward WebPINewDecoder |
-WebPIDecoder* WebPINew(WEBP_CSP_MODE mode) { |
- WebPIDecoder* const idec = WebPINewDecoder(NULL); |
- if (!idec) return NULL; |
- idec->output_.colorspace = mode; |
- return idec; |
-} |
- |
WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE mode, uint8_t* output_buffer, |
- int output_buffer_size, int output_stride) { |
+ size_t output_buffer_size, int output_stride) { |
WebPIDecoder* idec; |
if (mode >= MODE_YUV) return NULL; |
idec = WebPINewDecoder(NULL); |
- if (!idec) return NULL; |
+ if (idec == NULL) return NULL; |
idec->output_.colorspace = mode; |
idec->output_.is_external_memory = 1; |
idec->output_.u.RGBA.rgba = output_buffer; |
@@ -527,12 +608,13 @@ WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE mode, uint8_t* output_buffer, |
return idec; |
} |
-WebPIDecoder* WebPINewYUV(uint8_t* luma, int luma_size, int luma_stride, |
- uint8_t* u, int u_size, int u_stride, |
- uint8_t* v, int v_size, int v_stride) { |
+WebPIDecoder* WebPINewYUVA(uint8_t* luma, size_t luma_size, int luma_stride, |
+ uint8_t* u, size_t u_size, int u_stride, |
+ uint8_t* v, size_t v_size, int v_stride, |
+ uint8_t* a, size_t a_size, int a_stride) { |
WebPIDecoder* const idec = WebPINewDecoder(NULL); |
- if (!idec) return NULL; |
- idec->output_.colorspace = MODE_YUV; |
+ if (idec == NULL) return NULL; |
+ idec->output_.colorspace = (a == NULL) ? MODE_YUV : MODE_YUVA; |
idec->output_.is_external_memory = 1; |
idec->output_.u.YUVA.y = luma; |
idec->output_.u.YUVA.y_stride = luma_stride; |
@@ -543,16 +625,25 @@ WebPIDecoder* WebPINewYUV(uint8_t* luma, int luma_size, int luma_stride, |
idec->output_.u.YUVA.v = v; |
idec->output_.u.YUVA.v_stride = v_stride; |
idec->output_.u.YUVA.v_size = v_size; |
+ idec->output_.u.YUVA.a = a; |
+ idec->output_.u.YUVA.a_stride = a_stride; |
+ idec->output_.u.YUVA.a_size = a_size; |
return idec; |
} |
+WebPIDecoder* WebPINewYUV(uint8_t* luma, size_t luma_size, int luma_stride, |
+ uint8_t* u, size_t u_size, int u_stride, |
+ uint8_t* v, size_t v_size, int v_stride) { |
+ return WebPINewYUVA(luma, luma_size, luma_stride, |
+ u, u_size, u_stride, |
+ v, v_size, v_stride, |
+ NULL, 0, 0); |
+} |
+ |
//------------------------------------------------------------------------------ |
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; |
} |
@@ -562,8 +653,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* idec, |
+ const uint8_t* data, size_t data_size) { |
VP8StatusCode status; |
if (idec == NULL || data == NULL) { |
return VP8_STATUS_INVALID_PARAM; |
@@ -583,8 +674,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* idec, |
+ const uint8_t* data, size_t data_size) { |
VP8StatusCode status; |
if (idec == NULL || data == NULL) { |
return VP8_STATUS_INVALID_PARAM; |
@@ -607,61 +698,67 @@ 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; |
} |
-const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* const idec, |
- int* const left, int* const top, |
- int* const width, int* const height) { |
+const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* idec, |
+ int* left, int* top, |
+ int* width, int* height) { |
const WebPDecBuffer* const src = GetOutputBuffer(idec); |
- if (left) *left = 0; |
- if (top) *top = 0; |
+ if (left != NULL) *left = 0; |
+ if (top != NULL) *top = 0; |
// TODO(skal): later include handling of rotations. |
if (src) { |
- if (width) *width = src->width; |
- if (height) *height = idec->params_.last_y; |
+ if (width != NULL) *width = src->width; |
+ if (height != NULL) *height = idec->params_.last_y; |
} else { |
- if (width) *width = 0; |
- if (height) *height = 0; |
+ if (width != NULL) *width = 0; |
+ if (height != NULL) *height = 0; |
} |
return src; |
} |
-uint8_t* WebPIDecGetRGB(const WebPIDecoder* const idec, int* last_y, |
+uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int* last_y, |
int* width, int* height, int* stride) { |
const WebPDecBuffer* const src = GetOutputBuffer(idec); |
- if (!src) return NULL; |
+ if (src == NULL) return NULL; |
if (src->colorspace >= MODE_YUV) { |
return NULL; |
} |
- if (last_y) *last_y = idec->params_.last_y; |
- if (width) *width = src->width; |
- if (height) *height = src->height; |
- if (stride) *stride = src->u.RGBA.stride; |
+ if (last_y != NULL) *last_y = idec->params_.last_y; |
+ if (width != NULL) *width = src->width; |
+ if (height != NULL) *height = src->height; |
+ if (stride != NULL) *stride = src->u.RGBA.stride; |
return src->u.RGBA.rgba; |
} |
-uint8_t* WebPIDecGetYUV(const WebPIDecoder* const idec, int* last_y, |
- uint8_t** u, uint8_t** v, |
- int* width, int* height, int *stride, int* uv_stride) { |
+uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec, int* last_y, |
+ uint8_t** u, uint8_t** v, uint8_t** a, |
+ int* width, int* height, |
+ int* stride, int* uv_stride, int* a_stride) { |
const WebPDecBuffer* const src = GetOutputBuffer(idec); |
- if (!src) return NULL; |
+ if (src == NULL) return NULL; |
if (src->colorspace < MODE_YUV) { |
return NULL; |
} |
- if (last_y) *last_y = idec->params_.last_y; |
- if (u) *u = src->u.YUVA.u; |
- if (v) *v = src->u.YUVA.v; |
- if (width) *width = src->width; |
- if (height) *height = src->height; |
- if (stride) *stride = src->u.YUVA.y_stride; |
- if (uv_stride) *uv_stride = src->u.YUVA.u_stride; |
+ if (last_y != NULL) *last_y = idec->params_.last_y; |
+ if (u != NULL) *u = src->u.YUVA.u; |
+ if (v != NULL) *v = src->u.YUVA.v; |
+ if (a != NULL) *a = src->u.YUVA.a; |
+ if (width != NULL) *width = src->width; |
+ if (height != NULL) *height = src->height; |
+ if (stride != NULL) *stride = src->u.YUVA.y_stride; |
+ if (uv_stride != NULL) *uv_stride = src->u.YUVA.u_stride; |
+ if (a_stride != NULL) *a_stride = src->u.YUVA.a_stride; |
return src->u.YUVA.y; |
} |
@@ -671,7 +768,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; |
} |