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