xref: /aosp_15_r20/external/webp/src/dec/idec_dec.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Incremental decoding
11 //
12 // Author: [email protected] (Somnath Banerjee)
13 
14 #include <assert.h>
15 #include <string.h>
16 #include <stdlib.h>
17 
18 #include "src/dec/alphai_dec.h"
19 #include "src/dec/webpi_dec.h"
20 #include "src/dec/vp8_dec.h"
21 #include "src/dec/vp8i_dec.h"
22 #include "src/utils/utils.h"
23 #include "src/webp/decode.h"
24 
25 // In append mode, buffer allocations increase as multiples of this value.
26 // Needs to be a power of 2.
27 #define CHUNK_SIZE 4096
28 #define MAX_MB_SIZE 4096
29 
30 //------------------------------------------------------------------------------
31 // Data structures for memory and states
32 
33 // Decoding states. State normally flows as:
34 // WEBP_HEADER->VP8_HEADER->VP8_PARTS0->VP8_DATA->DONE for a lossy image, and
35 // WEBP_HEADER->VP8L_HEADER->VP8L_DATA->DONE for a lossless image.
36 // If there is any error the decoder goes into state ERROR.
37 typedef enum {
38   STATE_WEBP_HEADER,  // All the data before that of the VP8/VP8L chunk.
39   STATE_VP8_HEADER,   // The VP8 Frame header (within the VP8 chunk).
40   STATE_VP8_PARTS0,
41   STATE_VP8_DATA,
42   STATE_VP8L_HEADER,
43   STATE_VP8L_DATA,
44   STATE_DONE,
45   STATE_ERROR
46 } DecState;
47 
48 // Operating state for the MemBuffer
49 typedef enum {
50   MEM_MODE_NONE = 0,
51   MEM_MODE_APPEND,
52   MEM_MODE_MAP
53 } MemBufferMode;
54 
55 // storage for partition #0 and partial data (in a rolling fashion)
56 typedef struct {
57   MemBufferMode mode_;  // Operation mode
58   size_t start_;        // start location of the data to be decoded
59   size_t end_;          // end location
60   size_t buf_size_;     // size of the allocated buffer
61   uint8_t* buf_;        // We don't own this buffer in case WebPIUpdate()
62 
63   size_t part0_size_;         // size of partition #0
64   const uint8_t* part0_buf_;  // buffer to store partition #0
65 } MemBuffer;
66 
67 struct WebPIDecoder {
68   DecState state_;         // current decoding state
69   WebPDecParams params_;   // Params to store output info
70   int is_lossless_;        // for down-casting 'dec_'.
71   void* dec_;              // either a VP8Decoder or a VP8LDecoder instance
72   VP8Io io_;
73 
74   MemBuffer mem_;          // input memory buffer.
75   WebPDecBuffer output_;   // output buffer (when no external one is supplied,
76                            // or if the external one has slow-memory)
77   WebPDecBuffer* final_output_;  // Slow-memory output to copy to eventually.
78   size_t chunk_size_;      // Compressed VP8/VP8L size extracted from Header.
79 
80   int last_mb_y_;          // last row reached for intra-mode decoding
81 };
82 
83 // MB context to restore in case VP8DecodeMB() fails
84 typedef struct {
85   VP8MB left_;
86   VP8MB info_;
87   VP8BitReader token_br_;
88 } MBContext;
89 
90 //------------------------------------------------------------------------------
91 // MemBuffer: incoming data handling
92 
MemDataSize(const MemBuffer * mem)93 static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) {
94   return (mem->end_ - mem->start_);
95 }
96 
97 // Check if we need to preserve the compressed alpha data, as it may not have
98 // been decoded yet.
NeedCompressedAlpha(const WebPIDecoder * const idec)99 static int NeedCompressedAlpha(const WebPIDecoder* const idec) {
100   if (idec->state_ == STATE_WEBP_HEADER) {
101     // We haven't parsed the headers yet, so we don't know whether the image is
102     // lossy or lossless. This also means that we haven't parsed the ALPH chunk.
103     return 0;
104   }
105   if (idec->is_lossless_) {
106     return 0;  // ALPH chunk is not present for lossless images.
107   } else {
108     const VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
109     assert(dec != NULL);  // Must be true as idec->state_ != STATE_WEBP_HEADER.
110     return (dec->alpha_data_ != NULL) && !dec->is_alpha_decoded_;
111   }
112 }
113 
DoRemap(WebPIDecoder * const idec,ptrdiff_t offset)114 static void DoRemap(WebPIDecoder* const idec, ptrdiff_t offset) {
115   MemBuffer* const mem = &idec->mem_;
116   const uint8_t* const new_base = mem->buf_ + mem->start_;
117   // note: for VP8, setting up idec->io_ is only really needed at the beginning
118   // of the decoding, till partition #0 is complete.
119   idec->io_.data = new_base;
120   idec->io_.data_size = MemDataSize(mem);
121 
122   if (idec->dec_ != NULL) {
123     if (!idec->is_lossless_) {
124       VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
125       const uint32_t last_part = dec->num_parts_minus_one_;
126       if (offset != 0) {
127         uint32_t p;
128         for (p = 0; p <= last_part; ++p) {
129           VP8RemapBitReader(dec->parts_ + p, offset);
130         }
131         // Remap partition #0 data pointer to new offset, but only in MAP
132         // mode (in APPEND mode, partition #0 is copied into a fixed memory).
133         if (mem->mode_ == MEM_MODE_MAP) {
134           VP8RemapBitReader(&dec->br_, offset);
135         }
136       }
137       {
138         const uint8_t* const last_start = dec->parts_[last_part].buf_;
139         VP8BitReaderSetBuffer(&dec->parts_[last_part], last_start,
140                               mem->buf_ + mem->end_ - last_start);
141       }
142       if (NeedCompressedAlpha(idec)) {
143         ALPHDecoder* const alph_dec = dec->alph_dec_;
144         dec->alpha_data_ += offset;
145         if (alph_dec != NULL && alph_dec->vp8l_dec_ != NULL) {
146           if (alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION) {
147             VP8LDecoder* const alph_vp8l_dec = alph_dec->vp8l_dec_;
148             assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN);
149             VP8LBitReaderSetBuffer(&alph_vp8l_dec->br_,
150                                    dec->alpha_data_ + ALPHA_HEADER_LEN,
151                                    dec->alpha_data_size_ - ALPHA_HEADER_LEN);
152           } else {  // alph_dec->method_ == ALPHA_NO_COMPRESSION
153             // Nothing special to do in this case.
154           }
155         }
156       }
157     } else {    // Resize lossless bitreader
158       VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
159       VP8LBitReaderSetBuffer(&dec->br_, new_base, MemDataSize(mem));
160     }
161   }
162 }
163 
164 // Appends data to the end of MemBuffer->buf_. It expands the allocated memory
165 // size if required and also updates VP8BitReader's if new memory is allocated.
AppendToMemBuffer(WebPIDecoder * const idec,const uint8_t * const data,size_t data_size)166 WEBP_NODISCARD static int AppendToMemBuffer(WebPIDecoder* const idec,
167                                             const uint8_t* const data,
168                                             size_t data_size) {
169   VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
170   MemBuffer* const mem = &idec->mem_;
171   const int need_compressed_alpha = NeedCompressedAlpha(idec);
172   const uint8_t* const old_start =
173       (mem->buf_ == NULL) ? NULL : mem->buf_ + mem->start_;
174   const uint8_t* const old_base =
175       need_compressed_alpha ? dec->alpha_data_ : old_start;
176   assert(mem->buf_ != NULL || mem->start_ == 0);
177   assert(mem->mode_ == MEM_MODE_APPEND);
178   if (data_size > MAX_CHUNK_PAYLOAD) {
179     // security safeguard: trying to allocate more than what the format
180     // allows for a chunk should be considered a smoke smell.
181     return 0;
182   }
183 
184   if (mem->end_ + data_size > mem->buf_size_) {  // Need some free memory
185     const size_t new_mem_start = old_start - old_base;
186     const size_t current_size = MemDataSize(mem) + new_mem_start;
187     const uint64_t new_size = (uint64_t)current_size + data_size;
188     const uint64_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);
189     uint8_t* const new_buf =
190         (uint8_t*)WebPSafeMalloc(extra_size, sizeof(*new_buf));
191     if (new_buf == NULL) return 0;
192     if (old_base != NULL) memcpy(new_buf, old_base, current_size);
193     WebPSafeFree(mem->buf_);
194     mem->buf_ = new_buf;
195     mem->buf_size_ = (size_t)extra_size;
196     mem->start_ = new_mem_start;
197     mem->end_ = current_size;
198   }
199 
200   assert(mem->buf_ != NULL);
201   memcpy(mem->buf_ + mem->end_, data, data_size);
202   mem->end_ += data_size;
203   assert(mem->end_ <= mem->buf_size_);
204 
205   DoRemap(idec, mem->buf_ + mem->start_ - old_start);
206   return 1;
207 }
208 
RemapMemBuffer(WebPIDecoder * const idec,const uint8_t * const data,size_t data_size)209 WEBP_NODISCARD static int RemapMemBuffer(WebPIDecoder* const idec,
210                                          const uint8_t* const data,
211                                          size_t data_size) {
212   MemBuffer* const mem = &idec->mem_;
213   const uint8_t* const old_buf = mem->buf_;
214   const uint8_t* const old_start =
215       (old_buf == NULL) ? NULL : old_buf + mem->start_;
216   assert(old_buf != NULL || mem->start_ == 0);
217   assert(mem->mode_ == MEM_MODE_MAP);
218 
219   if (data_size < mem->buf_size_) return 0;  // can't remap to a shorter buffer!
220 
221   mem->buf_ = (uint8_t*)data;
222   mem->end_ = mem->buf_size_ = data_size;
223 
224   DoRemap(idec, mem->buf_ + mem->start_ - old_start);
225   return 1;
226 }
227 
InitMemBuffer(MemBuffer * const mem)228 static void InitMemBuffer(MemBuffer* const mem) {
229   mem->mode_       = MEM_MODE_NONE;
230   mem->buf_        = NULL;
231   mem->buf_size_   = 0;
232   mem->part0_buf_  = NULL;
233   mem->part0_size_ = 0;
234 }
235 
ClearMemBuffer(MemBuffer * const mem)236 static void ClearMemBuffer(MemBuffer* const mem) {
237   assert(mem);
238   if (mem->mode_ == MEM_MODE_APPEND) {
239     WebPSafeFree(mem->buf_);
240     WebPSafeFree((void*)mem->part0_buf_);
241   }
242 }
243 
CheckMemBufferMode(MemBuffer * const mem,MemBufferMode expected)244 WEBP_NODISCARD static int CheckMemBufferMode(MemBuffer* const mem,
245                                              MemBufferMode expected) {
246   if (mem->mode_ == MEM_MODE_NONE) {
247     mem->mode_ = expected;    // switch to the expected mode
248   } else if (mem->mode_ != expected) {
249     return 0;         // we mixed the modes => error
250   }
251   assert(mem->mode_ == expected);   // mode is ok
252   return 1;
253 }
254 
255 // To be called last.
FinishDecoding(WebPIDecoder * const idec)256 WEBP_NODISCARD static VP8StatusCode FinishDecoding(WebPIDecoder* const idec) {
257   const WebPDecoderOptions* const options = idec->params_.options;
258   WebPDecBuffer* const output = idec->params_.output;
259 
260   idec->state_ = STATE_DONE;
261   if (options != NULL && options->flip) {
262     const VP8StatusCode status = WebPFlipBuffer(output);
263     if (status != VP8_STATUS_OK) return status;
264   }
265   if (idec->final_output_ != NULL) {
266     const VP8StatusCode status = WebPCopyDecBufferPixels(
267         output, idec->final_output_);  // do the slow-copy
268     WebPFreeDecBuffer(&idec->output_);
269     if (status != VP8_STATUS_OK) return status;
270     *output = *idec->final_output_;
271     idec->final_output_ = NULL;
272   }
273   return VP8_STATUS_OK;
274 }
275 
276 //------------------------------------------------------------------------------
277 // Macroblock-decoding contexts
278 
SaveContext(const VP8Decoder * dec,const VP8BitReader * token_br,MBContext * const context)279 static void SaveContext(const VP8Decoder* dec, const VP8BitReader* token_br,
280                         MBContext* const context) {
281   context->left_ = dec->mb_info_[-1];
282   context->info_ = dec->mb_info_[dec->mb_x_];
283   context->token_br_ = *token_br;
284 }
285 
RestoreContext(const MBContext * context,VP8Decoder * const dec,VP8BitReader * const token_br)286 static void RestoreContext(const MBContext* context, VP8Decoder* const dec,
287                            VP8BitReader* const token_br) {
288   dec->mb_info_[-1] = context->left_;
289   dec->mb_info_[dec->mb_x_] = context->info_;
290   *token_br = context->token_br_;
291 }
292 
293 //------------------------------------------------------------------------------
294 
IDecError(WebPIDecoder * const idec,VP8StatusCode error)295 static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) {
296   if (idec->state_ == STATE_VP8_DATA) {
297     // Synchronize the thread, clean-up and check for errors.
298     (void)VP8ExitCritical((VP8Decoder*)idec->dec_, &idec->io_);
299   }
300   idec->state_ = STATE_ERROR;
301   return error;
302 }
303 
ChangeState(WebPIDecoder * const idec,DecState new_state,size_t consumed_bytes)304 static void ChangeState(WebPIDecoder* const idec, DecState new_state,
305                         size_t consumed_bytes) {
306   MemBuffer* const mem = &idec->mem_;
307   idec->state_ = new_state;
308   mem->start_ += consumed_bytes;
309   assert(mem->start_ <= mem->end_);
310   idec->io_.data = mem->buf_ + mem->start_;
311   idec->io_.data_size = MemDataSize(mem);
312 }
313 
314 // Headers
DecodeWebPHeaders(WebPIDecoder * const idec)315 static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {
316   MemBuffer* const mem = &idec->mem_;
317   const uint8_t* data = mem->buf_ + mem->start_;
318   size_t curr_size = MemDataSize(mem);
319   VP8StatusCode status;
320   WebPHeaderStructure headers;
321 
322   headers.data = data;
323   headers.data_size = curr_size;
324   headers.have_all_data = 0;
325   status = WebPParseHeaders(&headers);
326   if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
327     return VP8_STATUS_SUSPENDED;  // We haven't found a VP8 chunk yet.
328   } else if (status != VP8_STATUS_OK) {
329     return IDecError(idec, status);
330   }
331 
332   idec->chunk_size_ = headers.compressed_size;
333   idec->is_lossless_ = headers.is_lossless;
334   if (!idec->is_lossless_) {
335     VP8Decoder* const dec = VP8New();
336     if (dec == NULL) {
337       return VP8_STATUS_OUT_OF_MEMORY;
338     }
339     dec->incremental_ = 1;
340     idec->dec_ = dec;
341     dec->alpha_data_ = headers.alpha_data;
342     dec->alpha_data_size_ = headers.alpha_data_size;
343     ChangeState(idec, STATE_VP8_HEADER, headers.offset);
344   } else {
345     VP8LDecoder* const dec = VP8LNew();
346     if (dec == NULL) {
347       return VP8_STATUS_OUT_OF_MEMORY;
348     }
349     idec->dec_ = dec;
350     ChangeState(idec, STATE_VP8L_HEADER, headers.offset);
351   }
352   return VP8_STATUS_OK;
353 }
354 
DecodeVP8FrameHeader(WebPIDecoder * const idec)355 static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {
356   const uint8_t* data = idec->mem_.buf_ + idec->mem_.start_;
357   const size_t curr_size = MemDataSize(&idec->mem_);
358   int width, height;
359   uint32_t bits;
360 
361   if (curr_size < VP8_FRAME_HEADER_SIZE) {
362     // Not enough data bytes to extract VP8 Frame Header.
363     return VP8_STATUS_SUSPENDED;
364   }
365   if (!VP8GetInfo(data, curr_size, idec->chunk_size_, &width, &height)) {
366     return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
367   }
368 
369   bits = data[0] | (data[1] << 8) | (data[2] << 16);
370   idec->mem_.part0_size_ = (bits >> 5) + VP8_FRAME_HEADER_SIZE;
371 
372   idec->io_.data = data;
373   idec->io_.data_size = curr_size;
374   idec->state_ = STATE_VP8_PARTS0;
375   return VP8_STATUS_OK;
376 }
377 
378 // Partition #0
CopyParts0Data(WebPIDecoder * const idec)379 static VP8StatusCode CopyParts0Data(WebPIDecoder* const idec) {
380   VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
381   VP8BitReader* const br = &dec->br_;
382   const size_t part_size = br->buf_end_ - br->buf_;
383   MemBuffer* const mem = &idec->mem_;
384   assert(!idec->is_lossless_);
385   assert(mem->part0_buf_ == NULL);
386   // the following is a format limitation, no need for runtime check:
387   assert(part_size <= mem->part0_size_);
388   if (part_size == 0) {   // can't have zero-size partition #0
389     return VP8_STATUS_BITSTREAM_ERROR;
390   }
391   if (mem->mode_ == MEM_MODE_APPEND) {
392     // We copy and grab ownership of the partition #0 data.
393     uint8_t* const part0_buf = (uint8_t*)WebPSafeMalloc(1ULL, part_size);
394     if (part0_buf == NULL) {
395       return VP8_STATUS_OUT_OF_MEMORY;
396     }
397     memcpy(part0_buf, br->buf_, part_size);
398     mem->part0_buf_ = part0_buf;
399     VP8BitReaderSetBuffer(br, part0_buf, part_size);
400   } else {
401     // Else: just keep pointers to the partition #0's data in dec_->br_.
402   }
403   mem->start_ += part_size;
404   return VP8_STATUS_OK;
405 }
406 
DecodePartition0(WebPIDecoder * const idec)407 static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {
408   VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
409   VP8Io* const io = &idec->io_;
410   const WebPDecParams* const params = &idec->params_;
411   WebPDecBuffer* const output = params->output;
412 
413   // Wait till we have enough data for the whole partition #0
414   if (MemDataSize(&idec->mem_) < idec->mem_.part0_size_) {
415     return VP8_STATUS_SUSPENDED;
416   }
417 
418   if (!VP8GetHeaders(dec, io)) {
419     const VP8StatusCode status = dec->status_;
420     if (status == VP8_STATUS_SUSPENDED ||
421         status == VP8_STATUS_NOT_ENOUGH_DATA) {
422       // treating NOT_ENOUGH_DATA as SUSPENDED state
423       return VP8_STATUS_SUSPENDED;
424     }
425     return IDecError(idec, status);
426   }
427 
428   // Allocate/Verify output buffer now
429   dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
430                                        output);
431   if (dec->status_ != VP8_STATUS_OK) {
432     return IDecError(idec, dec->status_);
433   }
434   // This change must be done before calling VP8InitFrame()
435   dec->mt_method_ = VP8GetThreadMethod(params->options, NULL,
436                                        io->width, io->height);
437   VP8InitDithering(params->options, dec);
438 
439   dec->status_ = CopyParts0Data(idec);
440   if (dec->status_ != VP8_STATUS_OK) {
441     return IDecError(idec, dec->status_);
442   }
443 
444   // Finish setting up the decoding parameters. Will call io->setup().
445   if (VP8EnterCritical(dec, io) != VP8_STATUS_OK) {
446     return IDecError(idec, dec->status_);
447   }
448 
449   // Note: past this point, teardown() must always be called
450   // in case of error.
451   idec->state_ = STATE_VP8_DATA;
452   // Allocate memory and prepare everything.
453   if (!VP8InitFrame(dec, io)) {
454     return IDecError(idec, dec->status_);
455   }
456   return VP8_STATUS_OK;
457 }
458 
459 // Remaining partitions
DecodeRemaining(WebPIDecoder * const idec)460 static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) {
461   VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
462   VP8Io* const io = &idec->io_;
463 
464   // Make sure partition #0 has been read before, to set dec to ready_.
465   if (!dec->ready_) {
466     return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
467   }
468   for (; dec->mb_y_ < dec->mb_h_; ++dec->mb_y_) {
469     if (idec->last_mb_y_ != dec->mb_y_) {
470       if (!VP8ParseIntraModeRow(&dec->br_, dec)) {
471         // note: normally, error shouldn't occur since we already have the whole
472         // partition0 available here in DecodeRemaining(). Reaching EOF while
473         // reading intra modes really means a BITSTREAM_ERROR.
474         return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
475       }
476       idec->last_mb_y_ = dec->mb_y_;
477     }
478     for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
479       VP8BitReader* const token_br =
480           &dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];
481       MBContext context;
482       SaveContext(dec, token_br, &context);
483       if (!VP8DecodeMB(dec, token_br)) {
484         // We shouldn't fail when MAX_MB data was available
485         if (dec->num_parts_minus_one_ == 0 &&
486             MemDataSize(&idec->mem_) > MAX_MB_SIZE) {
487           return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
488         }
489         // Synchronize the threads.
490         if (dec->mt_method_ > 0) {
491           if (!WebPGetWorkerInterface()->Sync(&dec->worker_)) {
492             return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
493           }
494         }
495         RestoreContext(&context, dec, token_br);
496         return VP8_STATUS_SUSPENDED;
497       }
498       // Release buffer only if there is only one partition
499       if (dec->num_parts_minus_one_ == 0) {
500         idec->mem_.start_ = token_br->buf_ - idec->mem_.buf_;
501         assert(idec->mem_.start_ <= idec->mem_.end_);
502       }
503     }
504     VP8InitScanline(dec);   // Prepare for next scanline
505 
506     // Reconstruct, filter and emit the row.
507     if (!VP8ProcessRow(dec, io)) {
508       return IDecError(idec, VP8_STATUS_USER_ABORT);
509     }
510   }
511   // Synchronize the thread and check for errors.
512   if (!VP8ExitCritical(dec, io)) {
513     idec->state_ = STATE_ERROR;  // prevent re-entry in IDecError
514     return IDecError(idec, VP8_STATUS_USER_ABORT);
515   }
516   dec->ready_ = 0;
517   return FinishDecoding(idec);
518 }
519 
ErrorStatusLossless(WebPIDecoder * const idec,VP8StatusCode status)520 static VP8StatusCode ErrorStatusLossless(WebPIDecoder* const idec,
521                                          VP8StatusCode status) {
522   if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_NOT_ENOUGH_DATA) {
523     return VP8_STATUS_SUSPENDED;
524   }
525   return IDecError(idec, status);
526 }
527 
DecodeVP8LHeader(WebPIDecoder * const idec)528 static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {
529   VP8Io* const io = &idec->io_;
530   VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
531   const WebPDecParams* const params = &idec->params_;
532   WebPDecBuffer* const output = params->output;
533   size_t curr_size = MemDataSize(&idec->mem_);
534   assert(idec->is_lossless_);
535 
536   // Wait until there's enough data for decoding header.
537   if (curr_size < (idec->chunk_size_ >> 3)) {
538     dec->status_ = VP8_STATUS_SUSPENDED;
539     return ErrorStatusLossless(idec, dec->status_);
540   }
541 
542   if (!VP8LDecodeHeader(dec, io)) {
543     if (dec->status_ == VP8_STATUS_BITSTREAM_ERROR &&
544         curr_size < idec->chunk_size_) {
545       dec->status_ = VP8_STATUS_SUSPENDED;
546     }
547     return ErrorStatusLossless(idec, dec->status_);
548   }
549   // Allocate/verify output buffer now.
550   dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
551                                        output);
552   if (dec->status_ != VP8_STATUS_OK) {
553     return IDecError(idec, dec->status_);
554   }
555 
556   idec->state_ = STATE_VP8L_DATA;
557   return VP8_STATUS_OK;
558 }
559 
DecodeVP8LData(WebPIDecoder * const idec)560 static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {
561   VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
562   const size_t curr_size = MemDataSize(&idec->mem_);
563   assert(idec->is_lossless_);
564 
565   // Switch to incremental decoding if we don't have all the bytes available.
566   dec->incremental_ = (curr_size < idec->chunk_size_);
567 
568   if (!VP8LDecodeImage(dec)) {
569     return ErrorStatusLossless(idec, dec->status_);
570   }
571   assert(dec->status_ == VP8_STATUS_OK || dec->status_ == VP8_STATUS_SUSPENDED);
572   return (dec->status_ == VP8_STATUS_SUSPENDED) ? dec->status_
573                                                 : FinishDecoding(idec);
574 }
575 
576   // Main decoding loop
IDecode(WebPIDecoder * idec)577 static VP8StatusCode IDecode(WebPIDecoder* idec) {
578   VP8StatusCode status = VP8_STATUS_SUSPENDED;
579 
580   if (idec->state_ == STATE_WEBP_HEADER) {
581     status = DecodeWebPHeaders(idec);
582   } else {
583     if (idec->dec_ == NULL) {
584       return VP8_STATUS_SUSPENDED;    // can't continue if we have no decoder.
585     }
586   }
587   if (idec->state_ == STATE_VP8_HEADER) {
588     status = DecodeVP8FrameHeader(idec);
589   }
590   if (idec->state_ == STATE_VP8_PARTS0) {
591     status = DecodePartition0(idec);
592   }
593   if (idec->state_ == STATE_VP8_DATA) {
594     const VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
595     if (dec == NULL) {
596       return VP8_STATUS_SUSPENDED;  // can't continue if we have no decoder.
597     }
598     status = DecodeRemaining(idec);
599   }
600   if (idec->state_ == STATE_VP8L_HEADER) {
601     status = DecodeVP8LHeader(idec);
602   }
603   if (idec->state_ == STATE_VP8L_DATA) {
604     status = DecodeVP8LData(idec);
605   }
606   return status;
607 }
608 
609 //------------------------------------------------------------------------------
610 // Internal constructor
611 
NewDecoder(WebPDecBuffer * const output_buffer,const WebPBitstreamFeatures * const features)612 WEBP_NODISCARD static WebPIDecoder* NewDecoder(
613     WebPDecBuffer* const output_buffer,
614     const WebPBitstreamFeatures* const features) {
615   WebPIDecoder* idec = (WebPIDecoder*)WebPSafeCalloc(1ULL, sizeof(*idec));
616   if (idec == NULL) {
617     return NULL;
618   }
619 
620   idec->state_ = STATE_WEBP_HEADER;
621   idec->chunk_size_ = 0;
622 
623   idec->last_mb_y_ = -1;
624 
625   InitMemBuffer(&idec->mem_);
626   if (!WebPInitDecBuffer(&idec->output_) || !VP8InitIo(&idec->io_)) {
627     WebPSafeFree(idec);
628     return NULL;
629   }
630 
631   WebPResetDecParams(&idec->params_);
632   if (output_buffer == NULL || WebPAvoidSlowMemory(output_buffer, features)) {
633     idec->params_.output = &idec->output_;
634     idec->final_output_ = output_buffer;
635     if (output_buffer != NULL) {
636       idec->params_.output->colorspace = output_buffer->colorspace;
637     }
638   } else {
639     idec->params_.output = output_buffer;
640     idec->final_output_ = NULL;
641   }
642   WebPInitCustomIo(&idec->params_, &idec->io_);  // Plug the I/O functions.
643 
644   return idec;
645 }
646 
647 //------------------------------------------------------------------------------
648 // Public functions
649 
WebPINewDecoder(WebPDecBuffer * output_buffer)650 WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer) {
651   return NewDecoder(output_buffer, NULL);
652 }
653 
WebPIDecode(const uint8_t * data,size_t data_size,WebPDecoderConfig * config)654 WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,
655                           WebPDecoderConfig* config) {
656   WebPIDecoder* idec;
657   WebPBitstreamFeatures tmp_features;
658   WebPBitstreamFeatures* const features =
659       (config == NULL) ? &tmp_features : &config->input;
660   memset(&tmp_features, 0, sizeof(tmp_features));
661 
662   // Parse the bitstream's features, if requested:
663   if (data != NULL && data_size > 0) {
664     if (WebPGetFeatures(data, data_size, features) != VP8_STATUS_OK) {
665       return NULL;
666     }
667   }
668 
669   // Create an instance of the incremental decoder
670   idec = (config != NULL) ? NewDecoder(&config->output, features)
671                           : NewDecoder(NULL, features);
672   if (idec == NULL) {
673     return NULL;
674   }
675   // Finish initialization
676   if (config != NULL) {
677     idec->params_.options = &config->options;
678   }
679   return idec;
680 }
681 
WebPIDelete(WebPIDecoder * idec)682 void WebPIDelete(WebPIDecoder* idec) {
683   if (idec == NULL) return;
684   if (idec->dec_ != NULL) {
685     if (!idec->is_lossless_) {
686       if (idec->state_ == STATE_VP8_DATA) {
687         // Synchronize the thread, clean-up and check for errors.
688         // TODO(vrabaud) do we care about the return result?
689         (void)VP8ExitCritical((VP8Decoder*)idec->dec_, &idec->io_);
690       }
691       VP8Delete((VP8Decoder*)idec->dec_);
692     } else {
693       VP8LDelete((VP8LDecoder*)idec->dec_);
694     }
695   }
696   ClearMemBuffer(&idec->mem_);
697   WebPFreeDecBuffer(&idec->output_);
698   WebPSafeFree(idec);
699 }
700 
701 //------------------------------------------------------------------------------
702 // Wrapper toward WebPINewDecoder
703 
WebPINewRGB(WEBP_CSP_MODE csp,uint8_t * output_buffer,size_t output_buffer_size,int output_stride)704 WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE csp, uint8_t* output_buffer,
705                           size_t output_buffer_size, int output_stride) {
706   const int is_external_memory = (output_buffer != NULL) ? 1 : 0;
707   WebPIDecoder* idec;
708 
709   if (csp >= MODE_YUV) return NULL;
710   if (is_external_memory == 0) {    // Overwrite parameters to sane values.
711     output_buffer_size = 0;
712     output_stride = 0;
713   } else {  // A buffer was passed. Validate the other params.
714     if (output_stride == 0 || output_buffer_size == 0) {
715       return NULL;   // invalid parameter.
716     }
717   }
718   idec = WebPINewDecoder(NULL);
719   if (idec == NULL) return NULL;
720   idec->output_.colorspace = csp;
721   idec->output_.is_external_memory = is_external_memory;
722   idec->output_.u.RGBA.rgba = output_buffer;
723   idec->output_.u.RGBA.stride = output_stride;
724   idec->output_.u.RGBA.size = output_buffer_size;
725   return idec;
726 }
727 
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)728 WebPIDecoder* WebPINewYUVA(uint8_t* luma, size_t luma_size, int luma_stride,
729                            uint8_t* u, size_t u_size, int u_stride,
730                            uint8_t* v, size_t v_size, int v_stride,
731                            uint8_t* a, size_t a_size, int a_stride) {
732   const int is_external_memory = (luma != NULL) ? 1 : 0;
733   WebPIDecoder* idec;
734   WEBP_CSP_MODE colorspace;
735 
736   if (is_external_memory == 0) {    // Overwrite parameters to sane values.
737     luma_size = u_size = v_size = a_size = 0;
738     luma_stride = u_stride = v_stride = a_stride = 0;
739     u = v = a = NULL;
740     colorspace = MODE_YUVA;
741   } else {  // A luma buffer was passed. Validate the other parameters.
742     if (u == NULL || v == NULL) return NULL;
743     if (luma_size == 0 || u_size == 0 || v_size == 0) return NULL;
744     if (luma_stride == 0 || u_stride == 0 || v_stride == 0) return NULL;
745     if (a != NULL) {
746       if (a_size == 0 || a_stride == 0) return NULL;
747     }
748     colorspace = (a == NULL) ? MODE_YUV : MODE_YUVA;
749   }
750 
751   idec = WebPINewDecoder(NULL);
752   if (idec == NULL) return NULL;
753 
754   idec->output_.colorspace = colorspace;
755   idec->output_.is_external_memory = is_external_memory;
756   idec->output_.u.YUVA.y = luma;
757   idec->output_.u.YUVA.y_stride = luma_stride;
758   idec->output_.u.YUVA.y_size = luma_size;
759   idec->output_.u.YUVA.u = u;
760   idec->output_.u.YUVA.u_stride = u_stride;
761   idec->output_.u.YUVA.u_size = u_size;
762   idec->output_.u.YUVA.v = v;
763   idec->output_.u.YUVA.v_stride = v_stride;
764   idec->output_.u.YUVA.v_size = v_size;
765   idec->output_.u.YUVA.a = a;
766   idec->output_.u.YUVA.a_stride = a_stride;
767   idec->output_.u.YUVA.a_size = a_size;
768   return idec;
769 }
770 
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)771 WebPIDecoder* WebPINewYUV(uint8_t* luma, size_t luma_size, int luma_stride,
772                           uint8_t* u, size_t u_size, int u_stride,
773                           uint8_t* v, size_t v_size, int v_stride) {
774   return WebPINewYUVA(luma, luma_size, luma_stride,
775                       u, u_size, u_stride,
776                       v, v_size, v_stride,
777                       NULL, 0, 0);
778 }
779 
780 //------------------------------------------------------------------------------
781 
IDecCheckStatus(const WebPIDecoder * const idec)782 static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {
783   assert(idec);
784   if (idec->state_ == STATE_ERROR) {
785     return VP8_STATUS_BITSTREAM_ERROR;
786   }
787   if (idec->state_ == STATE_DONE) {
788     return VP8_STATUS_OK;
789   }
790   return VP8_STATUS_SUSPENDED;
791 }
792 
WebPIAppend(WebPIDecoder * idec,const uint8_t * data,size_t data_size)793 VP8StatusCode WebPIAppend(WebPIDecoder* idec,
794                           const uint8_t* data, size_t data_size) {
795   VP8StatusCode status;
796   if (idec == NULL || data == NULL) {
797     return VP8_STATUS_INVALID_PARAM;
798   }
799   status = IDecCheckStatus(idec);
800   if (status != VP8_STATUS_SUSPENDED) {
801     return status;
802   }
803   // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
804   if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_APPEND)) {
805     return VP8_STATUS_INVALID_PARAM;
806   }
807   // Append data to memory buffer
808   if (!AppendToMemBuffer(idec, data, data_size)) {
809     return VP8_STATUS_OUT_OF_MEMORY;
810   }
811   return IDecode(idec);
812 }
813 
WebPIUpdate(WebPIDecoder * idec,const uint8_t * data,size_t data_size)814 VP8StatusCode WebPIUpdate(WebPIDecoder* idec,
815                           const uint8_t* data, size_t data_size) {
816   VP8StatusCode status;
817   if (idec == NULL || data == NULL) {
818     return VP8_STATUS_INVALID_PARAM;
819   }
820   status = IDecCheckStatus(idec);
821   if (status != VP8_STATUS_SUSPENDED) {
822     return status;
823   }
824   // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
825   if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_MAP)) {
826     return VP8_STATUS_INVALID_PARAM;
827   }
828   // Make the memory buffer point to the new buffer
829   if (!RemapMemBuffer(idec, data, data_size)) {
830     return VP8_STATUS_INVALID_PARAM;
831   }
832   return IDecode(idec);
833 }
834 
835 //------------------------------------------------------------------------------
836 
GetOutputBuffer(const WebPIDecoder * const idec)837 static const WebPDecBuffer* GetOutputBuffer(const WebPIDecoder* const idec) {
838   if (idec == NULL || idec->dec_ == NULL) {
839     return NULL;
840   }
841   if (idec->state_ <= STATE_VP8_PARTS0) {
842     return NULL;
843   }
844   if (idec->final_output_ != NULL) {
845     return NULL;   // not yet slow-copied
846   }
847   return idec->params_.output;
848 }
849 
WebPIDecodedArea(const WebPIDecoder * idec,int * left,int * top,int * width,int * height)850 const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* idec,
851                                       int* left, int* top,
852                                       int* width, int* height) {
853   const WebPDecBuffer* const src = GetOutputBuffer(idec);
854   if (left != NULL) *left = 0;
855   if (top != NULL) *top = 0;
856   if (src != NULL) {
857     if (width != NULL) *width = src->width;
858     if (height != NULL) *height = idec->params_.last_y;
859   } else {
860     if (width != NULL) *width = 0;
861     if (height != NULL) *height = 0;
862   }
863   return src;
864 }
865 
WebPIDecGetRGB(const WebPIDecoder * idec,int * last_y,int * width,int * height,int * stride)866 WEBP_NODISCARD uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int* last_y,
867                                        int* width, int* height, int* stride) {
868   const WebPDecBuffer* const src = GetOutputBuffer(idec);
869   if (src == NULL) return NULL;
870   if (src->colorspace >= MODE_YUV) {
871     return NULL;
872   }
873 
874   if (last_y != NULL) *last_y = idec->params_.last_y;
875   if (width != NULL) *width = src->width;
876   if (height != NULL) *height = src->height;
877   if (stride != NULL) *stride = src->u.RGBA.stride;
878 
879   return src->u.RGBA.rgba;
880 }
881 
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)882 WEBP_NODISCARD uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec, int* last_y,
883                                         uint8_t** u, uint8_t** v, uint8_t** a,
884                                         int* width, int* height, int* stride,
885                                         int* uv_stride, int* a_stride) {
886   const WebPDecBuffer* const src = GetOutputBuffer(idec);
887   if (src == NULL) return NULL;
888   if (src->colorspace < MODE_YUV) {
889     return NULL;
890   }
891 
892   if (last_y != NULL) *last_y = idec->params_.last_y;
893   if (u != NULL) *u = src->u.YUVA.u;
894   if (v != NULL) *v = src->u.YUVA.v;
895   if (a != NULL) *a = src->u.YUVA.a;
896   if (width != NULL) *width = src->width;
897   if (height != NULL) *height = src->height;
898   if (stride != NULL) *stride = src->u.YUVA.y_stride;
899   if (uv_stride != NULL) *uv_stride = src->u.YUVA.u_stride;
900   if (a_stride != NULL) *a_stride = src->u.YUVA.a_stride;
901 
902   return src->u.YUVA.y;
903 }
904 
WebPISetIOHooks(WebPIDecoder * const idec,VP8IoPutHook put,VP8IoSetupHook setup,VP8IoTeardownHook teardown,void * user_data)905 int WebPISetIOHooks(WebPIDecoder* const idec,
906                     VP8IoPutHook put,
907                     VP8IoSetupHook setup,
908                     VP8IoTeardownHook teardown,
909                     void* user_data) {
910   if (idec == NULL || idec->state_ > STATE_WEBP_HEADER) {
911     return 0;
912   }
913 
914   idec->io_.put = put;
915   idec->io_.setup = setup;
916   idec->io_.teardown = teardown;
917   idec->io_.opaque = user_data;
918 
919   return 1;
920 }
921