1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // This code is licensed under the same terms as WebM:
4 // Software License Agreement: http://www.webmproject.org/license/software/
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
8 // Incremental decoding
10 // Author: somnath@google.com (Somnath Banerjee)
18 #include "../utils/utils.h"
20 #if defined(__cplusplus) || defined(c_plusplus)
24 // In append mode, buffer allocations increase as multiples of this value.
25 // Needs to be a power of 2.
26 #define CHUNK_SIZE 4096
27 #define MAX_MB_SIZE 4096
29 //------------------------------------------------------------------------------
30 // Data structures for memory and states
32 // Decoding states. State normally flows as:
33 // WEBP_HEADER->VP8_HEADER->VP8_PARTS0->VP8_DATA->DONE for a lossy image, and
34 // WEBP_HEADER->VP8L_HEADER->VP8L_DATA->DONE for a lossless image.
35 // If there is any error the decoder goes into state ERROR.
37 STATE_WEBP_HEADER, // All the data before that of the VP8/VP8L chunk.
38 STATE_VP8_HEADER, // The VP8 Frame header (within the VP8 chunk).
47 // Operating state for the MemBuffer
54 // storage for partition #0 and partial data (in a rolling fashion)
56 MemBufferMode mode_; // Operation mode
57 size_t start_; // start location of the data to be decoded
58 size_t end_; // end location
59 size_t buf_size_; // size of the allocated buffer
60 uint8_t* buf_; // We don't own this buffer in case WebPIUpdate()
62 size_t part0_size_; // size of partition #0
63 const uint8_t* part0_buf_; // buffer to store partition #0
67 DecState state_; // current decoding state
68 WebPDecParams params_; // Params to store output info
69 int is_lossless_; // for down-casting 'dec_'.
70 void* dec_; // either a VP8Decoder or a VP8LDecoder instance
73 MemBuffer mem_; // input memory buffer.
74 WebPDecBuffer output_; // output buffer (when no external one is supplied)
75 size_t chunk_size_; // Compressed VP8/VP8L size extracted from Header.
78 // MB context to restore in case VP8DecodeMB() fails
85 VP8BitReader token_br_;
88 //------------------------------------------------------------------------------
89 // MemBuffer: incoming data handling
91 static void RemapBitReader(VP8BitReader* const br, ptrdiff_t offset) {
92 if (br->buf_ != NULL) {
94 br->buf_end_ += offset;
98 static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) {
99 return (mem->end_ - mem->start_);
102 // Check if we need to preserve the compressed alpha data, as it may not have
104 static int NeedCompressedAlpha(const WebPIDecoder* const idec) {
105 if (idec->state_ == STATE_WEBP_HEADER) {
106 // We haven't parsed the headers yet, so we don't know whether the image is
107 // lossy or lossless. This also means that we haven't parsed the ALPH chunk.
110 if (idec->is_lossless_) {
111 return 0; // ALPH chunk is not present for lossless images.
113 const VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
114 assert(dec != NULL); // Must be true as idec->state_ != STATE_WEBP_HEADER.
115 return (dec->alpha_data_ != NULL) && !dec->is_alpha_decoded_;
119 static void DoRemap(WebPIDecoder* const idec, ptrdiff_t offset) {
120 MemBuffer* const mem = &idec->mem_;
121 const uint8_t* const new_base = mem->buf_ + mem->start_;
122 // note: for VP8, setting up idec->io_ is only really needed at the beginning
123 // of the decoding, till partition #0 is complete.
124 idec->io_.data = new_base;
125 idec->io_.data_size = MemDataSize(mem);
127 if (idec->dec_ != NULL) {
128 if (!idec->is_lossless_) {
129 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
130 const int last_part = dec->num_parts_ - 1;
133 for (p = 0; p <= last_part; ++p) {
134 RemapBitReader(dec->parts_ + p, offset);
136 // Remap partition #0 data pointer to new offset, but only in MAP
137 // mode (in APPEND mode, partition #0 is copied into a fixed memory).
138 if (mem->mode_ == MEM_MODE_MAP) {
139 RemapBitReader(&dec->br_, offset);
142 assert(last_part >= 0);
143 dec->parts_[last_part].buf_end_ = mem->buf_ + mem->end_;
144 if (NeedCompressedAlpha(idec)) dec->alpha_data_ += offset;
145 } else { // Resize lossless bitreader
146 VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
147 VP8LBitReaderSetBuffer(&dec->br_, new_base, MemDataSize(mem));
152 // Appends data to the end of MemBuffer->buf_. It expands the allocated memory
153 // size if required and also updates VP8BitReader's if new memory is allocated.
154 static int AppendToMemBuffer(WebPIDecoder* const idec,
155 const uint8_t* const data, size_t data_size) {
156 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
157 MemBuffer* const mem = &idec->mem_;
158 const int need_compressed_alpha = NeedCompressedAlpha(idec);
159 const uint8_t* const old_start = mem->buf_ + mem->start_;
160 const uint8_t* const old_base =
161 need_compressed_alpha ? dec->alpha_data_ : old_start;
162 assert(mem->mode_ == MEM_MODE_APPEND);
163 if (data_size > MAX_CHUNK_PAYLOAD) {
164 // security safeguard: trying to allocate more than what the format
165 // allows for a chunk should be considered a smoke smell.
169 if (mem->end_ + data_size > mem->buf_size_) { // Need some free memory
170 const size_t new_mem_start = old_start - old_base;
171 const size_t current_size = MemDataSize(mem) + new_mem_start;
172 const uint64_t new_size = (uint64_t)current_size + data_size;
173 const uint64_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);
174 uint8_t* const new_buf =
175 (uint8_t*)WebPSafeMalloc(extra_size, sizeof(*new_buf));
176 if (new_buf == NULL) return 0;
177 memcpy(new_buf, old_base, current_size);
180 mem->buf_size_ = (size_t)extra_size;
181 mem->start_ = new_mem_start;
182 mem->end_ = current_size;
185 memcpy(mem->buf_ + mem->end_, data, data_size);
186 mem->end_ += data_size;
187 assert(mem->end_ <= mem->buf_size_);
189 DoRemap(idec, mem->buf_ + mem->start_ - old_start);
193 static int RemapMemBuffer(WebPIDecoder* const idec,
194 const uint8_t* const data, size_t data_size) {
195 MemBuffer* const mem = &idec->mem_;
196 const uint8_t* const old_buf = mem->buf_;
197 const uint8_t* const old_start = old_buf + mem->start_;
198 assert(mem->mode_ == MEM_MODE_MAP);
200 if (data_size < mem->buf_size_) return 0; // can't remap to a shorter buffer!
202 mem->buf_ = (uint8_t*)data;
203 mem->end_ = mem->buf_size_ = data_size;
205 DoRemap(idec, mem->buf_ + mem->start_ - old_start);
209 static void InitMemBuffer(MemBuffer* const mem) {
210 mem->mode_ = MEM_MODE_NONE;
213 mem->part0_buf_ = NULL;
214 mem->part0_size_ = 0;
217 static void ClearMemBuffer(MemBuffer* const mem) {
219 if (mem->mode_ == MEM_MODE_APPEND) {
221 free((void*)mem->part0_buf_);
225 static int CheckMemBufferMode(MemBuffer* const mem, MemBufferMode expected) {
226 if (mem->mode_ == MEM_MODE_NONE) {
227 mem->mode_ = expected; // switch to the expected mode
228 } else if (mem->mode_ != expected) {
229 return 0; // we mixed the modes => error
231 assert(mem->mode_ == expected); // mode is ok
235 //------------------------------------------------------------------------------
236 // Macroblock-decoding contexts
238 static void SaveContext(const VP8Decoder* dec, const VP8BitReader* token_br,
239 MBContext* const context) {
240 const VP8BitReader* const br = &dec->br_;
241 const VP8MB* const left = dec->mb_info_ - 1;
242 const VP8MB* const info = dec->mb_info_ + dec->mb_x_;
244 context->left_ = *left;
245 context->info_ = *info;
247 context->token_br_ = *token_br;
248 memcpy(context->intra_t_, dec->intra_t_ + 4 * dec->mb_x_, 4);
249 memcpy(context->intra_l_, dec->intra_l_, 4);
252 static void RestoreContext(const MBContext* context, VP8Decoder* const dec,
253 VP8BitReader* const token_br) {
254 VP8BitReader* const br = &dec->br_;
255 VP8MB* const left = dec->mb_info_ - 1;
256 VP8MB* const info = dec->mb_info_ + dec->mb_x_;
258 *left = context->left_;
259 *info = context->info_;
261 *token_br = context->token_br_;
262 memcpy(dec->intra_t_ + 4 * dec->mb_x_, context->intra_t_, 4);
263 memcpy(dec->intra_l_, context->intra_l_, 4);
266 //------------------------------------------------------------------------------
268 static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) {
269 if (idec->state_ == STATE_VP8_DATA) {
270 VP8Io* const io = &idec->io_;
271 if (io->teardown != NULL) {
275 idec->state_ = STATE_ERROR;
279 static void ChangeState(WebPIDecoder* const idec, DecState new_state,
280 size_t consumed_bytes) {
281 MemBuffer* const mem = &idec->mem_;
282 idec->state_ = new_state;
283 mem->start_ += consumed_bytes;
284 assert(mem->start_ <= mem->end_);
285 idec->io_.data = mem->buf_ + mem->start_;
286 idec->io_.data_size = MemDataSize(mem);
290 static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {
291 MemBuffer* const mem = &idec->mem_;
292 const uint8_t* data = mem->buf_ + mem->start_;
293 size_t curr_size = MemDataSize(mem);
294 VP8StatusCode status;
295 WebPHeaderStructure headers;
298 headers.data_size = curr_size;
299 status = WebPParseHeaders(&headers);
300 if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
301 return VP8_STATUS_SUSPENDED; // We haven't found a VP8 chunk yet.
302 } else if (status != VP8_STATUS_OK) {
303 return IDecError(idec, status);
306 idec->chunk_size_ = headers.compressed_size;
307 idec->is_lossless_ = headers.is_lossless;
308 if (!idec->is_lossless_) {
309 VP8Decoder* const dec = VP8New();
311 return VP8_STATUS_OUT_OF_MEMORY;
314 #ifdef WEBP_USE_THREAD
315 dec->use_threads_ = (idec->params_.options != NULL) &&
316 (idec->params_.options->use_threads > 0);
318 dec->use_threads_ = 0;
320 dec->alpha_data_ = headers.alpha_data;
321 dec->alpha_data_size_ = headers.alpha_data_size;
322 ChangeState(idec, STATE_VP8_HEADER, headers.offset);
324 VP8LDecoder* const dec = VP8LNew();
326 return VP8_STATUS_OUT_OF_MEMORY;
329 ChangeState(idec, STATE_VP8L_HEADER, headers.offset);
331 return VP8_STATUS_OK;
334 static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {
335 const uint8_t* data = idec->mem_.buf_ + idec->mem_.start_;
336 const size_t curr_size = MemDataSize(&idec->mem_);
339 if (curr_size < VP8_FRAME_HEADER_SIZE) {
340 // Not enough data bytes to extract VP8 Frame Header.
341 return VP8_STATUS_SUSPENDED;
343 if (!VP8GetInfo(data, curr_size, idec->chunk_size_, NULL, NULL)) {
344 return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
347 bits = data[0] | (data[1] << 8) | (data[2] << 16);
348 idec->mem_.part0_size_ = (bits >> 5) + VP8_FRAME_HEADER_SIZE;
350 idec->io_.data = data;
351 idec->io_.data_size = curr_size;
352 idec->state_ = STATE_VP8_PARTS0;
353 return VP8_STATUS_OK;
357 static int CopyParts0Data(WebPIDecoder* const idec) {
358 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
359 VP8BitReader* const br = &dec->br_;
360 const size_t psize = br->buf_end_ - br->buf_;
361 MemBuffer* const mem = &idec->mem_;
362 assert(!idec->is_lossless_);
363 assert(mem->part0_buf_ == NULL);
365 assert(psize <= mem->part0_size_); // Format limit: no need for runtime check
366 if (mem->mode_ == MEM_MODE_APPEND) {
367 // We copy and grab ownership of the partition #0 data.
368 uint8_t* const part0_buf = (uint8_t*)malloc(psize);
369 if (part0_buf == NULL) {
372 memcpy(part0_buf, br->buf_, psize);
373 mem->part0_buf_ = part0_buf;
374 br->buf_ = part0_buf;
375 br->buf_end_ = part0_buf + psize;
377 // Else: just keep pointers to the partition #0's data in dec_->br_.
379 mem->start_ += psize;
383 static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {
384 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
385 VP8Io* const io = &idec->io_;
386 const WebPDecParams* const params = &idec->params_;
387 WebPDecBuffer* const output = params->output;
389 // Wait till we have enough data for the whole partition #0
390 if (MemDataSize(&idec->mem_) < idec->mem_.part0_size_) {
391 return VP8_STATUS_SUSPENDED;
394 if (!VP8GetHeaders(dec, io)) {
395 const VP8StatusCode status = dec->status_;
396 if (status == VP8_STATUS_SUSPENDED ||
397 status == VP8_STATUS_NOT_ENOUGH_DATA) {
398 // treating NOT_ENOUGH_DATA as SUSPENDED state
399 return VP8_STATUS_SUSPENDED;
401 return IDecError(idec, status);
404 // Allocate/Verify output buffer now
405 dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
407 if (dec->status_ != VP8_STATUS_OK) {
408 return IDecError(idec, dec->status_);
411 if (!CopyParts0Data(idec)) {
412 return IDecError(idec, VP8_STATUS_OUT_OF_MEMORY);
415 // Finish setting up the decoding parameters. Will call io->setup().
416 if (VP8EnterCritical(dec, io) != VP8_STATUS_OK) {
417 return IDecError(idec, dec->status_);
420 // Note: past this point, teardown() must always be called
422 idec->state_ = STATE_VP8_DATA;
423 // Allocate memory and prepare everything.
424 if (!VP8InitFrame(dec, io)) {
425 return IDecError(idec, dec->status_);
427 return VP8_STATUS_OK;
430 // Remaining partitions
431 static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) {
432 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
433 VP8Io* const io = &idec->io_;
437 for (; dec->mb_y_ < dec->mb_h_; ++dec->mb_y_) {
438 VP8BitReader* token_br = &dec->parts_[dec->mb_y_ & (dec->num_parts_ - 1)];
439 if (dec->mb_x_ == 0) {
440 VP8InitScanline(dec);
442 for (; dec->mb_x_ < dec->mb_w_; dec->mb_x_++) {
444 SaveContext(dec, token_br, &context);
446 if (!VP8DecodeMB(dec, token_br)) {
447 RestoreContext(&context, dec, token_br);
448 // We shouldn't fail when MAX_MB data was available
449 if (dec->num_parts_ == 1 && MemDataSize(&idec->mem_) > MAX_MB_SIZE) {
450 return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
452 return VP8_STATUS_SUSPENDED;
454 // Reconstruct and emit samples.
455 VP8ReconstructBlock(dec);
457 // Release buffer only if there is only one partition
458 if (dec->num_parts_ == 1) {
459 idec->mem_.start_ = token_br->buf_ - idec->mem_.buf_;
460 assert(idec->mem_.start_ <= idec->mem_.end_);
463 if (!VP8ProcessRow(dec, io)) {
464 return IDecError(idec, VP8_STATUS_USER_ABORT);
468 // Synchronize the thread and check for errors.
469 if (!VP8ExitCritical(dec, io)) {
470 return IDecError(idec, VP8_STATUS_USER_ABORT);
473 idec->state_ = STATE_DONE;
475 return VP8_STATUS_OK;
478 static int ErrorStatusLossless(WebPIDecoder* const idec, VP8StatusCode status) {
479 if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_NOT_ENOUGH_DATA) {
480 return VP8_STATUS_SUSPENDED;
482 return IDecError(idec, status);
485 static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {
486 VP8Io* const io = &idec->io_;
487 VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
488 const WebPDecParams* const params = &idec->params_;
489 WebPDecBuffer* const output = params->output;
490 size_t curr_size = MemDataSize(&idec->mem_);
491 assert(idec->is_lossless_);
493 // Wait until there's enough data for decoding header.
494 if (curr_size < (idec->chunk_size_ >> 3)) {
495 return VP8_STATUS_SUSPENDED;
497 if (!VP8LDecodeHeader(dec, io)) {
498 return ErrorStatusLossless(idec, dec->status_);
500 // Allocate/verify output buffer now.
501 dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
503 if (dec->status_ != VP8_STATUS_OK) {
504 return IDecError(idec, dec->status_);
507 idec->state_ = STATE_VP8L_DATA;
508 return VP8_STATUS_OK;
511 static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {
512 VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
513 const size_t curr_size = MemDataSize(&idec->mem_);
514 assert(idec->is_lossless_);
516 // At present Lossless decoder can't decode image incrementally. So wait till
517 // all the image data is aggregated before image can be decoded.
518 if (curr_size < idec->chunk_size_) {
519 return VP8_STATUS_SUSPENDED;
522 if (!VP8LDecodeImage(dec)) {
523 return ErrorStatusLossless(idec, dec->status_);
526 idec->state_ = STATE_DONE;
528 return VP8_STATUS_OK;
531 // Main decoding loop
532 static VP8StatusCode IDecode(WebPIDecoder* idec) {
533 VP8StatusCode status = VP8_STATUS_SUSPENDED;
535 if (idec->state_ == STATE_WEBP_HEADER) {
536 status = DecodeWebPHeaders(idec);
538 if (idec->dec_ == NULL) {
539 return VP8_STATUS_SUSPENDED; // can't continue if we have no decoder.
542 if (idec->state_ == STATE_VP8_HEADER) {
543 status = DecodeVP8FrameHeader(idec);
545 if (idec->state_ == STATE_VP8_PARTS0) {
546 status = DecodePartition0(idec);
548 if (idec->state_ == STATE_VP8_DATA) {
549 status = DecodeRemaining(idec);
551 if (idec->state_ == STATE_VP8L_HEADER) {
552 status = DecodeVP8LHeader(idec);
554 if (idec->state_ == STATE_VP8L_DATA) {
555 status = DecodeVP8LData(idec);
560 //------------------------------------------------------------------------------
563 WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer) {
564 WebPIDecoder* idec = (WebPIDecoder*)calloc(1, sizeof(*idec));
569 idec->state_ = STATE_WEBP_HEADER;
570 idec->chunk_size_ = 0;
572 InitMemBuffer(&idec->mem_);
573 WebPInitDecBuffer(&idec->output_);
574 VP8InitIo(&idec->io_);
576 WebPResetDecParams(&idec->params_);
577 idec->params_.output = (output_buffer != NULL) ? output_buffer
579 WebPInitCustomIo(&idec->params_, &idec->io_); // Plug the I/O functions.
584 WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,
585 WebPDecoderConfig* config) {
588 // Parse the bitstream's features, if requested:
589 if (data != NULL && data_size > 0 && config != NULL) {
590 if (WebPGetFeatures(data, data_size, &config->input) != VP8_STATUS_OK) {
594 // Create an instance of the incremental decoder
595 idec = WebPINewDecoder(config ? &config->output : NULL);
599 // Finish initialization
600 if (config != NULL) {
601 idec->params_.options = &config->options;
606 void WebPIDelete(WebPIDecoder* idec) {
607 if (idec == NULL) return;
608 if (idec->dec_ != NULL) {
609 if (!idec->is_lossless_) {
610 VP8Delete(idec->dec_);
612 VP8LDelete(idec->dec_);
615 ClearMemBuffer(&idec->mem_);
616 WebPFreeDecBuffer(&idec->output_);
620 //------------------------------------------------------------------------------
621 // Wrapper toward WebPINewDecoder
623 WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE mode, uint8_t* output_buffer,
624 size_t output_buffer_size, int output_stride) {
625 const int is_external_memory = (output_buffer != NULL);
628 if (mode >= MODE_YUV) return NULL;
629 if (!is_external_memory) { // Overwrite parameters to sane values.
630 output_buffer_size = 0;
632 } else { // A buffer was passed. Validate the other params.
633 if (output_stride == 0 || output_buffer_size == 0) {
634 return NULL; // invalid parameter.
637 idec = WebPINewDecoder(NULL);
638 if (idec == NULL) return NULL;
639 idec->output_.colorspace = mode;
640 idec->output_.is_external_memory = is_external_memory;
641 idec->output_.u.RGBA.rgba = output_buffer;
642 idec->output_.u.RGBA.stride = output_stride;
643 idec->output_.u.RGBA.size = output_buffer_size;
647 WebPIDecoder* WebPINewYUVA(uint8_t* luma, size_t luma_size, int luma_stride,
648 uint8_t* u, size_t u_size, int u_stride,
649 uint8_t* v, size_t v_size, int v_stride,
650 uint8_t* a, size_t a_size, int a_stride) {
651 const int is_external_memory = (luma != NULL);
653 WEBP_CSP_MODE colorspace;
655 if (!is_external_memory) { // Overwrite parameters to sane values.
656 luma_size = u_size = v_size = a_size = 0;
657 luma_stride = u_stride = v_stride = a_stride = 0;
659 colorspace = MODE_YUVA;
660 } else { // A luma buffer was passed. Validate the other parameters.
661 if (u == NULL || v == NULL) return NULL;
662 if (luma_size == 0 || u_size == 0 || v_size == 0) return NULL;
663 if (luma_stride == 0 || u_stride == 0 || v_stride == 0) return NULL;
665 if (a_size == 0 || a_stride == 0) return NULL;
667 colorspace = (a == NULL) ? MODE_YUV : MODE_YUVA;
670 idec = WebPINewDecoder(NULL);
671 if (idec == NULL) return NULL;
673 idec->output_.colorspace = colorspace;
674 idec->output_.is_external_memory = is_external_memory;
675 idec->output_.u.YUVA.y = luma;
676 idec->output_.u.YUVA.y_stride = luma_stride;
677 idec->output_.u.YUVA.y_size = luma_size;
678 idec->output_.u.YUVA.u = u;
679 idec->output_.u.YUVA.u_stride = u_stride;
680 idec->output_.u.YUVA.u_size = u_size;
681 idec->output_.u.YUVA.v = v;
682 idec->output_.u.YUVA.v_stride = v_stride;
683 idec->output_.u.YUVA.v_size = v_size;
684 idec->output_.u.YUVA.a = a;
685 idec->output_.u.YUVA.a_stride = a_stride;
686 idec->output_.u.YUVA.a_size = a_size;
690 WebPIDecoder* WebPINewYUV(uint8_t* luma, size_t luma_size, int luma_stride,
691 uint8_t* u, size_t u_size, int u_stride,
692 uint8_t* v, size_t v_size, int v_stride) {
693 return WebPINewYUVA(luma, luma_size, luma_stride,
699 //------------------------------------------------------------------------------
701 static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {
703 if (idec->state_ == STATE_ERROR) {
704 return VP8_STATUS_BITSTREAM_ERROR;
706 if (idec->state_ == STATE_DONE) {
707 return VP8_STATUS_OK;
709 return VP8_STATUS_SUSPENDED;
712 VP8StatusCode WebPIAppend(WebPIDecoder* idec,
713 const uint8_t* data, size_t data_size) {
714 VP8StatusCode status;
715 if (idec == NULL || data == NULL) {
716 return VP8_STATUS_INVALID_PARAM;
718 status = IDecCheckStatus(idec);
719 if (status != VP8_STATUS_SUSPENDED) {
722 // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
723 if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_APPEND)) {
724 return VP8_STATUS_INVALID_PARAM;
726 // Append data to memory buffer
727 if (!AppendToMemBuffer(idec, data, data_size)) {
728 return VP8_STATUS_OUT_OF_MEMORY;
730 return IDecode(idec);
733 VP8StatusCode WebPIUpdate(WebPIDecoder* idec,
734 const uint8_t* data, size_t data_size) {
735 VP8StatusCode status;
736 if (idec == NULL || data == NULL) {
737 return VP8_STATUS_INVALID_PARAM;
739 status = IDecCheckStatus(idec);
740 if (status != VP8_STATUS_SUSPENDED) {
743 // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
744 if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_MAP)) {
745 return VP8_STATUS_INVALID_PARAM;
747 // Make the memory buffer point to the new buffer
748 if (!RemapMemBuffer(idec, data, data_size)) {
749 return VP8_STATUS_INVALID_PARAM;
751 return IDecode(idec);
754 //------------------------------------------------------------------------------
756 static const WebPDecBuffer* GetOutputBuffer(const WebPIDecoder* const idec) {
757 if (idec == NULL || idec->dec_ == NULL) {
760 if (idec->state_ <= STATE_VP8_PARTS0) {
763 return idec->params_.output;
766 const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* idec,
768 int* width, int* height) {
769 const WebPDecBuffer* const src = GetOutputBuffer(idec);
770 if (left != NULL) *left = 0;
771 if (top != NULL) *top = 0;
772 // TODO(skal): later include handling of rotations.
774 if (width != NULL) *width = src->width;
775 if (height != NULL) *height = idec->params_.last_y;
777 if (width != NULL) *width = 0;
778 if (height != NULL) *height = 0;
783 uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int* last_y,
784 int* width, int* height, int* stride) {
785 const WebPDecBuffer* const src = GetOutputBuffer(idec);
786 if (src == NULL) return NULL;
787 if (src->colorspace >= MODE_YUV) {
791 if (last_y != NULL) *last_y = idec->params_.last_y;
792 if (width != NULL) *width = src->width;
793 if (height != NULL) *height = src->height;
794 if (stride != NULL) *stride = src->u.RGBA.stride;
796 return src->u.RGBA.rgba;
799 uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec, int* last_y,
800 uint8_t** u, uint8_t** v, uint8_t** a,
801 int* width, int* height,
802 int* stride, int* uv_stride, int* a_stride) {
803 const WebPDecBuffer* const src = GetOutputBuffer(idec);
804 if (src == NULL) return NULL;
805 if (src->colorspace < MODE_YUV) {
809 if (last_y != NULL) *last_y = idec->params_.last_y;
810 if (u != NULL) *u = src->u.YUVA.u;
811 if (v != NULL) *v = src->u.YUVA.v;
812 if (a != NULL) *a = src->u.YUVA.a;
813 if (width != NULL) *width = src->width;
814 if (height != NULL) *height = src->height;
815 if (stride != NULL) *stride = src->u.YUVA.y_stride;
816 if (uv_stride != NULL) *uv_stride = src->u.YUVA.u_stride;
817 if (a_stride != NULL) *a_stride = src->u.YUVA.a_stride;
819 return src->u.YUVA.y;
822 int WebPISetIOHooks(WebPIDecoder* const idec,
824 VP8IoSetupHook setup,
825 VP8IoTeardownHook teardown,
827 if (idec == NULL || idec->state_ > STATE_WEBP_HEADER) {
832 idec->io_.setup = setup;
833 idec->io_.teardown = teardown;
834 idec->io_.opaque = user_data;
839 #if defined(__cplusplus) || defined(c_plusplus)