blob: 7bab1eab795f4743510514fb2de37d860906c139 [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Vikas Arora03d5e342011-06-02 23:59:44 +05302//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// 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.
Vikas Arora03d5e342011-06-02 23:59:44 +05308// -----------------------------------------------------------------------------
9//
10// Incremental decoding
11//
12// Author: somnath@google.com (Somnath Banerjee)
13
14#include <assert.h>
15#include <string.h>
16#include <stdlib.h>
17
Vikas Arora8b720222014-01-02 16:48:02 -080018#include "./alphai.h"
Vikas Aroraa2415722012-08-09 16:18:58 -070019#include "./webpi.h"
20#include "./vp8i.h"
21#include "../utils/utils.h"
Vikas Arora03d5e342011-06-02 23:59:44 +053022
Vikas Aroraa2415722012-08-09 16:18:58 -070023// In append mode, buffer allocations increase as multiples of this value.
24// Needs to be a power of 2.
Vikas Arora03d5e342011-06-02 23:59:44 +053025#define CHUNK_SIZE 4096
26#define MAX_MB_SIZE 4096
27
28//------------------------------------------------------------------------------
29// Data structures for memory and states
30
Vikas Arora8b720222014-01-02 16:48:02 -080031// Decoding states. State normally flows as:
32// WEBP_HEADER->VP8_HEADER->VP8_PARTS0->VP8_DATA->DONE for a lossy image, and
33// WEBP_HEADER->VP8L_HEADER->VP8L_DATA->DONE for a lossless image.
Vikas Arora03d5e342011-06-02 23:59:44 +053034// If there is any error the decoder goes into state ERROR.
Vikas Aroraa2415722012-08-09 16:18:58 -070035typedef enum {
Vikas Arora8b720222014-01-02 16:48:02 -080036 STATE_WEBP_HEADER, // All the data before that of the VP8/VP8L chunk.
37 STATE_VP8_HEADER, // The VP8 Frame header (within the VP8 chunk).
Vikas Aroraa2415722012-08-09 16:18:58 -070038 STATE_VP8_PARTS0,
39 STATE_VP8_DATA,
40 STATE_VP8L_HEADER,
41 STATE_VP8L_DATA,
42 STATE_DONE,
43 STATE_ERROR
Vikas Arora03d5e342011-06-02 23:59:44 +053044} DecState;
45
46// Operating state for the MemBuffer
Vikas Aroraa2415722012-08-09 16:18:58 -070047typedef enum {
48 MEM_MODE_NONE = 0,
49 MEM_MODE_APPEND,
50 MEM_MODE_MAP
Vikas Arora03d5e342011-06-02 23:59:44 +053051} MemBufferMode;
52
53// storage for partition #0 and partial data (in a rolling fashion)
54typedef struct {
55 MemBufferMode mode_; // Operation mode
Vikas Aroraa2415722012-08-09 16:18:58 -070056 size_t start_; // start location of the data to be decoded
57 size_t end_; // end location
Vikas Arora03d5e342011-06-02 23:59:44 +053058 size_t buf_size_; // size of the allocated buffer
59 uint8_t* buf_; // We don't own this buffer in case WebPIUpdate()
60
61 size_t part0_size_; // size of partition #0
62 const uint8_t* part0_buf_; // buffer to store partition #0
63} MemBuffer;
64
65struct WebPIDecoder {
66 DecState state_; // current decoding state
Vikas Arora03d5e342011-06-02 23:59:44 +053067 WebPDecParams params_; // Params to store output info
Vikas Aroraa2415722012-08-09 16:18:58 -070068 int is_lossless_; // for down-casting 'dec_'.
69 void* dec_; // either a VP8Decoder or a VP8LDecoder instance
Vikas Arora03d5e342011-06-02 23:59:44 +053070 VP8Io io_;
71
Vikas Arora46672792011-07-13 16:37:55 +053072 MemBuffer mem_; // input memory buffer.
73 WebPDecBuffer output_; // output buffer (when no external one is supplied)
Vikas Aroraa2415722012-08-09 16:18:58 -070074 size_t chunk_size_; // Compressed VP8/VP8L size extracted from Header.
Vikas Aroraaf51b942014-08-28 10:51:12 -070075
76 int last_mb_y_; // last row reached for intra-mode decoding
Vikas Arora03d5e342011-06-02 23:59:44 +053077};
78
79// MB context to restore in case VP8DecodeMB() fails
80typedef struct {
81 VP8MB left_;
82 VP8MB info_;
Vikas Arora03d5e342011-06-02 23:59:44 +053083 VP8BitReader token_br_;
84} MBContext;
85
86//------------------------------------------------------------------------------
87// MemBuffer: incoming data handling
88
Vikas Aroraa2415722012-08-09 16:18:58 -070089static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) {
Vikas Arora03d5e342011-06-02 23:59:44 +053090 return (mem->end_ - mem->start_);
91}
92
Vikas Arora0406ce12013-08-09 15:57:12 -070093// Check if we need to preserve the compressed alpha data, as it may not have
94// been decoded yet.
95static int NeedCompressedAlpha(const WebPIDecoder* const idec) {
Vikas Arora8b720222014-01-02 16:48:02 -080096 if (idec->state_ == STATE_WEBP_HEADER) {
Vikas Arora0406ce12013-08-09 15:57:12 -070097 // We haven't parsed the headers yet, so we don't know whether the image is
98 // lossy or lossless. This also means that we haven't parsed the ALPH chunk.
99 return 0;
100 }
101 if (idec->is_lossless_) {
102 return 0; // ALPH chunk is not present for lossless images.
103 } else {
104 const VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
Vikas Arora8b720222014-01-02 16:48:02 -0800105 assert(dec != NULL); // Must be true as idec->state_ != STATE_WEBP_HEADER.
Vikas Arora0406ce12013-08-09 15:57:12 -0700106 return (dec->alpha_data_ != NULL) && !dec->is_alpha_decoded_;
107 }
108}
109
Vikas Aroraa2415722012-08-09 16:18:58 -0700110static void DoRemap(WebPIDecoder* const idec, ptrdiff_t offset) {
111 MemBuffer* const mem = &idec->mem_;
112 const uint8_t* const new_base = mem->buf_ + mem->start_;
113 // note: for VP8, setting up idec->io_ is only really needed at the beginning
114 // of the decoding, till partition #0 is complete.
115 idec->io_.data = new_base;
116 idec->io_.data_size = MemDataSize(mem);
117
118 if (idec->dec_ != NULL) {
119 if (!idec->is_lossless_) {
120 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
121 const int last_part = dec->num_parts_ - 1;
122 if (offset != 0) {
123 int p;
124 for (p = 0; p <= last_part; ++p) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700125 VP8RemapBitReader(dec->parts_ + p, offset);
Vikas Aroraa2415722012-08-09 16:18:58 -0700126 }
127 // Remap partition #0 data pointer to new offset, but only in MAP
128 // mode (in APPEND mode, partition #0 is copied into a fixed memory).
129 if (mem->mode_ == MEM_MODE_MAP) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700130 VP8RemapBitReader(&dec->br_, offset);
Vikas Aroraa2415722012-08-09 16:18:58 -0700131 }
132 }
133 assert(last_part >= 0);
134 dec->parts_[last_part].buf_end_ = mem->buf_ + mem->end_;
Vikas Arora8b720222014-01-02 16:48:02 -0800135 if (NeedCompressedAlpha(idec)) {
136 ALPHDecoder* const alph_dec = dec->alph_dec_;
137 dec->alpha_data_ += offset;
138 if (alph_dec != NULL) {
139 if (alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION) {
140 VP8LDecoder* const alph_vp8l_dec = alph_dec->vp8l_dec_;
141 assert(alph_vp8l_dec != NULL);
142 assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN);
143 VP8LBitReaderSetBuffer(&alph_vp8l_dec->br_,
144 dec->alpha_data_ + ALPHA_HEADER_LEN,
145 dec->alpha_data_size_ - ALPHA_HEADER_LEN);
146 } else { // alph_dec->method_ == ALPHA_NO_COMPRESSION
147 // Nothing special to do in this case.
148 }
149 }
150 }
Vikas Aroraa2415722012-08-09 16:18:58 -0700151 } else { // Resize lossless bitreader
152 VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
153 VP8LBitReaderSetBuffer(&dec->br_, new_base, MemDataSize(mem));
154 }
155 }
156}
157
Vikas Arora03d5e342011-06-02 23:59:44 +0530158// Appends data to the end of MemBuffer->buf_. It expands the allocated memory
159// size if required and also updates VP8BitReader's if new memory is allocated.
160static int AppendToMemBuffer(WebPIDecoder* const idec,
161 const uint8_t* const data, size_t data_size) {
Vikas Arora0406ce12013-08-09 15:57:12 -0700162 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
Vikas Arora03d5e342011-06-02 23:59:44 +0530163 MemBuffer* const mem = &idec->mem_;
Vikas Arora0406ce12013-08-09 15:57:12 -0700164 const int need_compressed_alpha = NeedCompressedAlpha(idec);
165 const uint8_t* const old_start = mem->buf_ + mem->start_;
166 const uint8_t* const old_base =
167 need_compressed_alpha ? dec->alpha_data_ : old_start;
Vikas Arora03d5e342011-06-02 23:59:44 +0530168 assert(mem->mode_ == MEM_MODE_APPEND);
Vikas Aroraa2415722012-08-09 16:18:58 -0700169 if (data_size > MAX_CHUNK_PAYLOAD) {
170 // security safeguard: trying to allocate more than what the format
171 // allows for a chunk should be considered a smoke smell.
172 return 0;
173 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530174
175 if (mem->end_ + data_size > mem->buf_size_) { // Need some free memory
Vikas Arora0406ce12013-08-09 15:57:12 -0700176 const size_t new_mem_start = old_start - old_base;
177 const size_t current_size = MemDataSize(mem) + new_mem_start;
Vikas Aroraa2415722012-08-09 16:18:58 -0700178 const uint64_t new_size = (uint64_t)current_size + data_size;
179 const uint64_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);
180 uint8_t* const new_buf =
181 (uint8_t*)WebPSafeMalloc(extra_size, sizeof(*new_buf));
182 if (new_buf == NULL) return 0;
183 memcpy(new_buf, old_base, current_size);
Vikas Aroraaf51b942014-08-28 10:51:12 -0700184 WebPSafeFree(mem->buf_);
Vikas Arora03d5e342011-06-02 23:59:44 +0530185 mem->buf_ = new_buf;
Vikas Aroraa2415722012-08-09 16:18:58 -0700186 mem->buf_size_ = (size_t)extra_size;
Vikas Arora0406ce12013-08-09 15:57:12 -0700187 mem->start_ = new_mem_start;
Vikas Aroraa2415722012-08-09 16:18:58 -0700188 mem->end_ = current_size;
Vikas Arora03d5e342011-06-02 23:59:44 +0530189 }
190
191 memcpy(mem->buf_ + mem->end_, data, data_size);
192 mem->end_ += data_size;
193 assert(mem->end_ <= mem->buf_size_);
Vikas Arora03d5e342011-06-02 23:59:44 +0530194
Vikas Arora0406ce12013-08-09 15:57:12 -0700195 DoRemap(idec, mem->buf_ + mem->start_ - old_start);
Vikas Arora03d5e342011-06-02 23:59:44 +0530196 return 1;
197}
198
199static int RemapMemBuffer(WebPIDecoder* const idec,
200 const uint8_t* const data, size_t data_size) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530201 MemBuffer* const mem = &idec->mem_;
Vikas Arora0406ce12013-08-09 15:57:12 -0700202 const uint8_t* const old_buf = mem->buf_;
203 const uint8_t* const old_start = old_buf + mem->start_;
Vikas Arora03d5e342011-06-02 23:59:44 +0530204 assert(mem->mode_ == MEM_MODE_MAP);
Vikas Arora03d5e342011-06-02 23:59:44 +0530205
Vikas Aroraa2415722012-08-09 16:18:58 -0700206 if (data_size < mem->buf_size_) return 0; // can't remap to a shorter buffer!
Vikas Arora03d5e342011-06-02 23:59:44 +0530207
208 mem->buf_ = (uint8_t*)data;
209 mem->end_ = mem->buf_size_ = data_size;
210
Vikas Arora0406ce12013-08-09 15:57:12 -0700211 DoRemap(idec, mem->buf_ + mem->start_ - old_start);
Vikas Arora03d5e342011-06-02 23:59:44 +0530212 return 1;
213}
214
215static void InitMemBuffer(MemBuffer* const mem) {
216 mem->mode_ = MEM_MODE_NONE;
Vikas Aroraa2415722012-08-09 16:18:58 -0700217 mem->buf_ = NULL;
Vikas Arora03d5e342011-06-02 23:59:44 +0530218 mem->buf_size_ = 0;
Vikas Aroraa2415722012-08-09 16:18:58 -0700219 mem->part0_buf_ = NULL;
Vikas Arora03d5e342011-06-02 23:59:44 +0530220 mem->part0_size_ = 0;
221}
222
223static void ClearMemBuffer(MemBuffer* const mem) {
224 assert(mem);
225 if (mem->mode_ == MEM_MODE_APPEND) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700226 WebPSafeFree(mem->buf_);
227 WebPSafeFree((void*)mem->part0_buf_);
Vikas Arora03d5e342011-06-02 23:59:44 +0530228 }
229}
230
231static int CheckMemBufferMode(MemBuffer* const mem, MemBufferMode expected) {
232 if (mem->mode_ == MEM_MODE_NONE) {
233 mem->mode_ = expected; // switch to the expected mode
234 } else if (mem->mode_ != expected) {
235 return 0; // we mixed the modes => error
236 }
237 assert(mem->mode_ == expected); // mode is ok
238 return 1;
239}
240
Vikas Aroraaf51b942014-08-28 10:51:12 -0700241// To be called last.
242static VP8StatusCode FinishDecoding(WebPIDecoder* const idec) {
243#if WEBP_DECODER_ABI_VERSION > 0x0203
244 const WebPDecoderOptions* const options = idec->params_.options;
245 WebPDecBuffer* const output = idec->params_.output;
246
247 idec->state_ = STATE_DONE;
248 if (options != NULL && options->flip) {
249 return WebPFlipBuffer(output);
250 }
251#endif
252 idec->state_ = STATE_DONE;
253 return VP8_STATUS_OK;
254}
255
Vikas Arora03d5e342011-06-02 23:59:44 +0530256//------------------------------------------------------------------------------
257// Macroblock-decoding contexts
258
259static void SaveContext(const VP8Decoder* dec, const VP8BitReader* token_br,
260 MBContext* const context) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700261 context->left_ = dec->mb_info_[-1];
262 context->info_ = dec->mb_info_[dec->mb_x_];
Vikas Arora03d5e342011-06-02 23:59:44 +0530263 context->token_br_ = *token_br;
Vikas Arora03d5e342011-06-02 23:59:44 +0530264}
265
266static void RestoreContext(const MBContext* context, VP8Decoder* const dec,
267 VP8BitReader* const token_br) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700268 dec->mb_info_[-1] = context->left_;
269 dec->mb_info_[dec->mb_x_] = context->info_;
Vikas Arora03d5e342011-06-02 23:59:44 +0530270 *token_br = context->token_br_;
Vikas Arora03d5e342011-06-02 23:59:44 +0530271}
272
273//------------------------------------------------------------------------------
274
Vikas Aroraa2415722012-08-09 16:18:58 -0700275static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) {
276 if (idec->state_ == STATE_VP8_DATA) {
Vikas Arora88fe2b82011-07-22 14:49:55 +0530277 VP8Io* const io = &idec->io_;
Vikas Arora8b720222014-01-02 16:48:02 -0800278 if (io->teardown != NULL) {
Vikas Arora88fe2b82011-07-22 14:49:55 +0530279 io->teardown(io);
280 }
281 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530282 idec->state_ = STATE_ERROR;
283 return error;
284}
285
Vikas Aroraa2415722012-08-09 16:18:58 -0700286static void ChangeState(WebPIDecoder* const idec, DecState new_state,
287 size_t consumed_bytes) {
288 MemBuffer* const mem = &idec->mem_;
289 idec->state_ = new_state;
290 mem->start_ += consumed_bytes;
291 assert(mem->start_ <= mem->end_);
292 idec->io_.data = mem->buf_ + mem->start_;
293 idec->io_.data_size = MemDataSize(mem);
294}
Vikas Arora03d5e342011-06-02 23:59:44 +0530295
Vikas Aroraa2415722012-08-09 16:18:58 -0700296// Headers
297static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {
298 MemBuffer* const mem = &idec->mem_;
299 const uint8_t* data = mem->buf_ + mem->start_;
300 size_t curr_size = MemDataSize(mem);
301 VP8StatusCode status;
302 WebPHeaderStructure headers;
303
304 headers.data = data;
305 headers.data_size = curr_size;
Vikas Aroraaf51b942014-08-28 10:51:12 -0700306 headers.have_all_data = 0;
Vikas Aroraa2415722012-08-09 16:18:58 -0700307 status = WebPParseHeaders(&headers);
308 if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
309 return VP8_STATUS_SUSPENDED; // We haven't found a VP8 chunk yet.
310 } else if (status != VP8_STATUS_OK) {
311 return IDecError(idec, status);
Vikas Arora03d5e342011-06-02 23:59:44 +0530312 }
313
Vikas Aroraa2415722012-08-09 16:18:58 -0700314 idec->chunk_size_ = headers.compressed_size;
315 idec->is_lossless_ = headers.is_lossless;
316 if (!idec->is_lossless_) {
317 VP8Decoder* const dec = VP8New();
318 if (dec == NULL) {
319 return VP8_STATUS_OUT_OF_MEMORY;
320 }
321 idec->dec_ = dec;
Vikas Aroraa2415722012-08-09 16:18:58 -0700322 dec->alpha_data_ = headers.alpha_data;
323 dec->alpha_data_size_ = headers.alpha_data_size;
Vikas Arora8b720222014-01-02 16:48:02 -0800324 ChangeState(idec, STATE_VP8_HEADER, headers.offset);
Vikas Aroraa2415722012-08-09 16:18:58 -0700325 } else {
326 VP8LDecoder* const dec = VP8LNew();
327 if (dec == NULL) {
328 return VP8_STATUS_OUT_OF_MEMORY;
329 }
330 idec->dec_ = dec;
331 ChangeState(idec, STATE_VP8L_HEADER, headers.offset);
332 }
333 return VP8_STATUS_OK;
334}
335
336static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {
337 const uint8_t* data = idec->mem_.buf_ + idec->mem_.start_;
338 const size_t curr_size = MemDataSize(&idec->mem_);
Vikas Arora8b720222014-01-02 16:48:02 -0800339 int width, height;
Vikas Aroraa2415722012-08-09 16:18:58 -0700340 uint32_t bits;
341
342 if (curr_size < VP8_FRAME_HEADER_SIZE) {
343 // Not enough data bytes to extract VP8 Frame Header.
344 return VP8_STATUS_SUSPENDED;
345 }
Vikas Arora8b720222014-01-02 16:48:02 -0800346 if (!VP8GetInfo(data, curr_size, idec->chunk_size_, &width, &height)) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530347 return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
348 }
349
Vikas Arora03d5e342011-06-02 23:59:44 +0530350 bits = data[0] | (data[1] << 8) | (data[2] << 16);
Vikas Aroraa2415722012-08-09 16:18:58 -0700351 idec->mem_.part0_size_ = (bits >> 5) + VP8_FRAME_HEADER_SIZE;
Vikas Arora03d5e342011-06-02 23:59:44 +0530352
Vikas Arora03d5e342011-06-02 23:59:44 +0530353 idec->io_.data = data;
Vikas Aroraa2415722012-08-09 16:18:58 -0700354 idec->io_.data_size = curr_size;
355 idec->state_ = STATE_VP8_PARTS0;
Vikas Arora03d5e342011-06-02 23:59:44 +0530356 return VP8_STATUS_OK;
357}
358
359// Partition #0
Vikas Aroraa2415722012-08-09 16:18:58 -0700360static int CopyParts0Data(WebPIDecoder* const idec) {
361 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
362 VP8BitReader* const br = &dec->br_;
Vikas Arora03d5e342011-06-02 23:59:44 +0530363 const size_t psize = br->buf_end_ - br->buf_;
364 MemBuffer* const mem = &idec->mem_;
Vikas Aroraa2415722012-08-09 16:18:58 -0700365 assert(!idec->is_lossless_);
366 assert(mem->part0_buf_ == NULL);
Vikas Arora03d5e342011-06-02 23:59:44 +0530367 assert(psize > 0);
Vikas Aroraa2415722012-08-09 16:18:58 -0700368 assert(psize <= mem->part0_size_); // Format limit: no need for runtime check
Vikas Arora03d5e342011-06-02 23:59:44 +0530369 if (mem->mode_ == MEM_MODE_APPEND) {
370 // We copy and grab ownership of the partition #0 data.
Vikas Aroraaf51b942014-08-28 10:51:12 -0700371 uint8_t* const part0_buf = (uint8_t*)WebPSafeMalloc(1ULL, psize);
Vikas Aroraa2415722012-08-09 16:18:58 -0700372 if (part0_buf == NULL) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530373 return 0;
374 }
375 memcpy(part0_buf, br->buf_, psize);
376 mem->part0_buf_ = part0_buf;
Vikas Arora03d5e342011-06-02 23:59:44 +0530377 br->buf_ = part0_buf;
378 br->buf_end_ = part0_buf + psize;
379 } else {
380 // Else: just keep pointers to the partition #0's data in dec_->br_.
381 }
Vikas Aroraa2415722012-08-09 16:18:58 -0700382 mem->start_ += psize;
Vikas Arora03d5e342011-06-02 23:59:44 +0530383 return 1;
384}
385
386static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700387 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
Vikas Arora03d5e342011-06-02 23:59:44 +0530388 VP8Io* const io = &idec->io_;
389 const WebPDecParams* const params = &idec->params_;
Vikas Arora46672792011-07-13 16:37:55 +0530390 WebPDecBuffer* const output = params->output;
Vikas Arora03d5e342011-06-02 23:59:44 +0530391
392 // Wait till we have enough data for the whole partition #0
393 if (MemDataSize(&idec->mem_) < idec->mem_.part0_size_) {
394 return VP8_STATUS_SUSPENDED;
395 }
396
Vikas Arora03d5e342011-06-02 23:59:44 +0530397 if (!VP8GetHeaders(dec, io)) {
398 const VP8StatusCode status = dec->status_;
399 if (status == VP8_STATUS_SUSPENDED ||
400 status == VP8_STATUS_NOT_ENOUGH_DATA) {
401 // treating NOT_ENOUGH_DATA as SUSPENDED state
402 return VP8_STATUS_SUSPENDED;
403 }
404 return IDecError(idec, status);
405 }
406
Vikas Arora46672792011-07-13 16:37:55 +0530407 // Allocate/Verify output buffer now
408 dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
409 output);
410 if (dec->status_ != VP8_STATUS_OK) {
411 return IDecError(idec, dec->status_);
Vikas Arora03d5e342011-06-02 23:59:44 +0530412 }
Vikas Arora8b720222014-01-02 16:48:02 -0800413 // This change must be done before calling VP8InitFrame()
414 dec->mt_method_ = VP8GetThreadMethod(params->options, NULL,
415 io->width, io->height);
416 VP8InitDithering(params->options, dec);
Vikas Arora03d5e342011-06-02 23:59:44 +0530417 if (!CopyParts0Data(idec)) {
418 return IDecError(idec, VP8_STATUS_OUT_OF_MEMORY);
419 }
Vikas Arora88fe2b82011-07-22 14:49:55 +0530420
Vikas Aroraa2415722012-08-09 16:18:58 -0700421 // Finish setting up the decoding parameters. Will call io->setup().
422 if (VP8EnterCritical(dec, io) != VP8_STATUS_OK) {
Vikas Arora88fe2b82011-07-22 14:49:55 +0530423 return IDecError(idec, dec->status_);
424 }
Vikas Aroraa2415722012-08-09 16:18:58 -0700425
Vikas Arora88fe2b82011-07-22 14:49:55 +0530426 // Note: past this point, teardown() must always be called
427 // in case of error.
Vikas Aroraa2415722012-08-09 16:18:58 -0700428 idec->state_ = STATE_VP8_DATA;
429 // Allocate memory and prepare everything.
430 if (!VP8InitFrame(dec, io)) {
431 return IDecError(idec, dec->status_);
432 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530433 return VP8_STATUS_OK;
434}
435
436// Remaining partitions
437static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700438 VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
Vikas Arora03d5e342011-06-02 23:59:44 +0530439 VP8Io* const io = &idec->io_;
440
441 assert(dec->ready_);
Vikas Arora03d5e342011-06-02 23:59:44 +0530442 for (; dec->mb_y_ < dec->mb_h_; ++dec->mb_y_) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700443 if (idec->last_mb_y_ != dec->mb_y_) {
444 if (!VP8ParseIntraModeRow(&dec->br_, dec)) {
445 // note: normally, error shouldn't occur since we already have the whole
446 // partition0 available here in DecodeRemaining(). Reaching EOF while
447 // reading intra modes really means a BITSTREAM_ERROR.
448 return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
449 }
450 idec->last_mb_y_ = dec->mb_y_;
451 }
Vikas Arora8b720222014-01-02 16:48:02 -0800452 for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700453 VP8BitReader* const token_br =
454 &dec->parts_[dec->mb_y_ & (dec->num_parts_ - 1)];
Vikas Arora03d5e342011-06-02 23:59:44 +0530455 MBContext context;
456 SaveContext(dec, token_br, &context);
Vikas Arora03d5e342011-06-02 23:59:44 +0530457 if (!VP8DecodeMB(dec, token_br)) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530458 // We shouldn't fail when MAX_MB data was available
459 if (dec->num_parts_ == 1 && MemDataSize(&idec->mem_) > MAX_MB_SIZE) {
460 return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
461 }
Vikas Aroraaf51b942014-08-28 10:51:12 -0700462 RestoreContext(&context, dec, token_br);
Vikas Arora03d5e342011-06-02 23:59:44 +0530463 return VP8_STATUS_SUSPENDED;
464 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530465 // Release buffer only if there is only one partition
466 if (dec->num_parts_ == 1) {
467 idec->mem_.start_ = token_br->buf_ - idec->mem_.buf_;
468 assert(idec->mem_.start_ <= idec->mem_.end_);
469 }
470 }
Vikas Arora8b720222014-01-02 16:48:02 -0800471 VP8InitScanline(dec); // Prepare for next scanline
472
473 // Reconstruct, filter and emit the row.
Vikas Aroraa2415722012-08-09 16:18:58 -0700474 if (!VP8ProcessRow(dec, io)) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530475 return IDecError(idec, VP8_STATUS_USER_ABORT);
476 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530477 }
Vikas Aroraa2415722012-08-09 16:18:58 -0700478 // Synchronize the thread and check for errors.
479 if (!VP8ExitCritical(dec, io)) {
480 return IDecError(idec, VP8_STATUS_USER_ABORT);
Vikas Arora03d5e342011-06-02 23:59:44 +0530481 }
482 dec->ready_ = 0;
Vikas Aroraaf51b942014-08-28 10:51:12 -0700483 return FinishDecoding(idec);
Vikas Arora03d5e342011-06-02 23:59:44 +0530484}
485
Vikas Arora8b720222014-01-02 16:48:02 -0800486static VP8StatusCode ErrorStatusLossless(WebPIDecoder* const idec,
487 VP8StatusCode status) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700488 if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_NOT_ENOUGH_DATA) {
489 return VP8_STATUS_SUSPENDED;
490 }
491 return IDecError(idec, status);
492}
493
494static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {
495 VP8Io* const io = &idec->io_;
496 VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
497 const WebPDecParams* const params = &idec->params_;
498 WebPDecBuffer* const output = params->output;
499 size_t curr_size = MemDataSize(&idec->mem_);
500 assert(idec->is_lossless_);
501
502 // Wait until there's enough data for decoding header.
503 if (curr_size < (idec->chunk_size_ >> 3)) {
504 return VP8_STATUS_SUSPENDED;
505 }
506 if (!VP8LDecodeHeader(dec, io)) {
507 return ErrorStatusLossless(idec, dec->status_);
508 }
509 // Allocate/verify output buffer now.
510 dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
511 output);
512 if (dec->status_ != VP8_STATUS_OK) {
513 return IDecError(idec, dec->status_);
514 }
515
516 idec->state_ = STATE_VP8L_DATA;
517 return VP8_STATUS_OK;
518}
519
520static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {
521 VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
522 const size_t curr_size = MemDataSize(&idec->mem_);
523 assert(idec->is_lossless_);
524
525 // At present Lossless decoder can't decode image incrementally. So wait till
526 // all the image data is aggregated before image can be decoded.
527 if (curr_size < idec->chunk_size_) {
528 return VP8_STATUS_SUSPENDED;
529 }
530
531 if (!VP8LDecodeImage(dec)) {
532 return ErrorStatusLossless(idec, dec->status_);
533 }
534
Vikas Aroraaf51b942014-08-28 10:51:12 -0700535 return FinishDecoding(idec);
Vikas Aroraa2415722012-08-09 16:18:58 -0700536}
537
Vikas Arora03d5e342011-06-02 23:59:44 +0530538 // Main decoding loop
539static VP8StatusCode IDecode(WebPIDecoder* idec) {
540 VP8StatusCode status = VP8_STATUS_SUSPENDED;
Vikas Arora03d5e342011-06-02 23:59:44 +0530541
Vikas Arora8b720222014-01-02 16:48:02 -0800542 if (idec->state_ == STATE_WEBP_HEADER) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700543 status = DecodeWebPHeaders(idec);
544 } else {
545 if (idec->dec_ == NULL) {
546 return VP8_STATUS_SUSPENDED; // can't continue if we have no decoder.
547 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530548 }
Vikas Arora8b720222014-01-02 16:48:02 -0800549 if (idec->state_ == STATE_VP8_HEADER) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700550 status = DecodeVP8FrameHeader(idec);
551 }
552 if (idec->state_ == STATE_VP8_PARTS0) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530553 status = DecodePartition0(idec);
554 }
Vikas Aroraa2415722012-08-09 16:18:58 -0700555 if (idec->state_ == STATE_VP8_DATA) {
Vikas Arora46672792011-07-13 16:37:55 +0530556 status = DecodeRemaining(idec);
Vikas Arora03d5e342011-06-02 23:59:44 +0530557 }
Vikas Aroraa2415722012-08-09 16:18:58 -0700558 if (idec->state_ == STATE_VP8L_HEADER) {
559 status = DecodeVP8LHeader(idec);
560 }
561 if (idec->state_ == STATE_VP8L_DATA) {
562 status = DecodeVP8LData(idec);
563 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530564 return status;
565}
566
567//------------------------------------------------------------------------------
568// Public functions
569
Vikas Aroraa2415722012-08-09 16:18:58 -0700570WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer) {
Vikas Aroraaf51b942014-08-28 10:51:12 -0700571 WebPIDecoder* idec = (WebPIDecoder*)WebPSafeCalloc(1ULL, sizeof(*idec));
Vikas Arora46672792011-07-13 16:37:55 +0530572 if (idec == NULL) {
573 return NULL;
574 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530575
Vikas Arora8b720222014-01-02 16:48:02 -0800576 idec->state_ = STATE_WEBP_HEADER;
Vikas Aroraa2415722012-08-09 16:18:58 -0700577 idec->chunk_size_ = 0;
Vikas Arora03d5e342011-06-02 23:59:44 +0530578
Vikas Aroraaf51b942014-08-28 10:51:12 -0700579 idec->last_mb_y_ = -1;
580
Vikas Arora03d5e342011-06-02 23:59:44 +0530581 InitMemBuffer(&idec->mem_);
Vikas Arora46672792011-07-13 16:37:55 +0530582 WebPInitDecBuffer(&idec->output_);
Vikas Arora03d5e342011-06-02 23:59:44 +0530583 VP8InitIo(&idec->io_);
Vikas Arora46672792011-07-13 16:37:55 +0530584
585 WebPResetDecParams(&idec->params_);
Vikas Arora8b720222014-01-02 16:48:02 -0800586 idec->params_.output = (output_buffer != NULL) ? output_buffer
587 : &idec->output_;
Vikas Arora46672792011-07-13 16:37:55 +0530588 WebPInitCustomIo(&idec->params_, &idec->io_); // Plug the I/O functions.
589
590 return idec;
591}
592
Vikas Aroraa2415722012-08-09 16:18:58 -0700593WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,
594 WebPDecoderConfig* config) {
Vikas Arora46672792011-07-13 16:37:55 +0530595 WebPIDecoder* idec;
596
597 // Parse the bitstream's features, if requested:
598 if (data != NULL && data_size > 0 && config != NULL) {
599 if (WebPGetFeatures(data, data_size, &config->input) != VP8_STATUS_OK) {
600 return NULL;
601 }
602 }
603 // Create an instance of the incremental decoder
604 idec = WebPINewDecoder(config ? &config->output : NULL);
Vikas Aroraa2415722012-08-09 16:18:58 -0700605 if (idec == NULL) {
Vikas Arora46672792011-07-13 16:37:55 +0530606 return NULL;
607 }
608 // Finish initialization
609 if (config != NULL) {
610 idec->params_.options = &config->options;
611 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530612 return idec;
613}
614
Vikas Aroraa2415722012-08-09 16:18:58 -0700615void WebPIDelete(WebPIDecoder* idec) {
616 if (idec == NULL) return;
617 if (idec->dec_ != NULL) {
618 if (!idec->is_lossless_) {
Vikas Arora513e97b2013-09-18 14:21:10 -0700619 if (idec->state_ == STATE_VP8_DATA) {
620 // Synchronize the thread, clean-up and check for errors.
Vikas Arora8b720222014-01-02 16:48:02 -0800621 VP8ExitCritical((VP8Decoder*)idec->dec_, &idec->io_);
Vikas Arora513e97b2013-09-18 14:21:10 -0700622 }
Vikas Arora8b720222014-01-02 16:48:02 -0800623 VP8Delete((VP8Decoder*)idec->dec_);
Vikas Aroraa2415722012-08-09 16:18:58 -0700624 } else {
Vikas Arora8b720222014-01-02 16:48:02 -0800625 VP8LDelete((VP8LDecoder*)idec->dec_);
Vikas Aroraa2415722012-08-09 16:18:58 -0700626 }
627 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530628 ClearMemBuffer(&idec->mem_);
Vikas Arora46672792011-07-13 16:37:55 +0530629 WebPFreeDecBuffer(&idec->output_);
Vikas Aroraaf51b942014-08-28 10:51:12 -0700630 WebPSafeFree(idec);
Vikas Arora03d5e342011-06-02 23:59:44 +0530631}
632
633//------------------------------------------------------------------------------
Vikas Arora46672792011-07-13 16:37:55 +0530634// Wrapper toward WebPINewDecoder
635
Vikas Arora03d5e342011-06-02 23:59:44 +0530636WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE mode, uint8_t* output_buffer,
Vikas Aroraa2415722012-08-09 16:18:58 -0700637 size_t output_buffer_size, int output_stride) {
Vikas Arora1e7bf882013-03-13 16:43:18 -0700638 const int is_external_memory = (output_buffer != NULL);
Vikas Arora03d5e342011-06-02 23:59:44 +0530639 WebPIDecoder* idec;
Vikas Arora1e7bf882013-03-13 16:43:18 -0700640
Vikas Arora46672792011-07-13 16:37:55 +0530641 if (mode >= MODE_YUV) return NULL;
Vikas Arora1e7bf882013-03-13 16:43:18 -0700642 if (!is_external_memory) { // Overwrite parameters to sane values.
643 output_buffer_size = 0;
644 output_stride = 0;
645 } else { // A buffer was passed. Validate the other params.
646 if (output_stride == 0 || output_buffer_size == 0) {
647 return NULL; // invalid parameter.
648 }
649 }
Vikas Arora46672792011-07-13 16:37:55 +0530650 idec = WebPINewDecoder(NULL);
Vikas Aroraa2415722012-08-09 16:18:58 -0700651 if (idec == NULL) return NULL;
Vikas Arora46672792011-07-13 16:37:55 +0530652 idec->output_.colorspace = mode;
Vikas Arora1e7bf882013-03-13 16:43:18 -0700653 idec->output_.is_external_memory = is_external_memory;
Vikas Arora46672792011-07-13 16:37:55 +0530654 idec->output_.u.RGBA.rgba = output_buffer;
655 idec->output_.u.RGBA.stride = output_stride;
656 idec->output_.u.RGBA.size = output_buffer_size;
Vikas Arora03d5e342011-06-02 23:59:44 +0530657 return idec;
658}
659
Vikas Aroraa2415722012-08-09 16:18:58 -0700660WebPIDecoder* WebPINewYUVA(uint8_t* luma, size_t luma_size, int luma_stride,
661 uint8_t* u, size_t u_size, int u_stride,
662 uint8_t* v, size_t v_size, int v_stride,
663 uint8_t* a, size_t a_size, int a_stride) {
Vikas Arora1e7bf882013-03-13 16:43:18 -0700664 const int is_external_memory = (luma != NULL);
665 WebPIDecoder* idec;
666 WEBP_CSP_MODE colorspace;
667
668 if (!is_external_memory) { // Overwrite parameters to sane values.
669 luma_size = u_size = v_size = a_size = 0;
670 luma_stride = u_stride = v_stride = a_stride = 0;
671 u = v = a = NULL;
672 colorspace = MODE_YUVA;
673 } else { // A luma buffer was passed. Validate the other parameters.
674 if (u == NULL || v == NULL) return NULL;
675 if (luma_size == 0 || u_size == 0 || v_size == 0) return NULL;
676 if (luma_stride == 0 || u_stride == 0 || v_stride == 0) return NULL;
677 if (a != NULL) {
678 if (a_size == 0 || a_stride == 0) return NULL;
679 }
680 colorspace = (a == NULL) ? MODE_YUV : MODE_YUVA;
681 }
682
683 idec = WebPINewDecoder(NULL);
Vikas Aroraa2415722012-08-09 16:18:58 -0700684 if (idec == NULL) return NULL;
Vikas Arora1e7bf882013-03-13 16:43:18 -0700685
686 idec->output_.colorspace = colorspace;
687 idec->output_.is_external_memory = is_external_memory;
Vikas Arora46672792011-07-13 16:37:55 +0530688 idec->output_.u.YUVA.y = luma;
689 idec->output_.u.YUVA.y_stride = luma_stride;
690 idec->output_.u.YUVA.y_size = luma_size;
691 idec->output_.u.YUVA.u = u;
692 idec->output_.u.YUVA.u_stride = u_stride;
693 idec->output_.u.YUVA.u_size = u_size;
694 idec->output_.u.YUVA.v = v;
695 idec->output_.u.YUVA.v_stride = v_stride;
696 idec->output_.u.YUVA.v_size = v_size;
Vikas Aroraa2415722012-08-09 16:18:58 -0700697 idec->output_.u.YUVA.a = a;
698 idec->output_.u.YUVA.a_stride = a_stride;
699 idec->output_.u.YUVA.a_size = a_size;
Vikas Arora03d5e342011-06-02 23:59:44 +0530700 return idec;
701}
702
Vikas Aroraa2415722012-08-09 16:18:58 -0700703WebPIDecoder* WebPINewYUV(uint8_t* luma, size_t luma_size, int luma_stride,
704 uint8_t* u, size_t u_size, int u_stride,
705 uint8_t* v, size_t v_size, int v_stride) {
706 return WebPINewYUVA(luma, luma_size, luma_stride,
707 u, u_size, u_stride,
708 v, v_size, v_stride,
709 NULL, 0, 0);
710}
711
Vikas Arora03d5e342011-06-02 23:59:44 +0530712//------------------------------------------------------------------------------
713
714static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {
715 assert(idec);
Vikas Arora03d5e342011-06-02 23:59:44 +0530716 if (idec->state_ == STATE_ERROR) {
717 return VP8_STATUS_BITSTREAM_ERROR;
718 }
719 if (idec->state_ == STATE_DONE) {
720 return VP8_STATUS_OK;
721 }
722 return VP8_STATUS_SUSPENDED;
723}
724
Vikas Aroraa2415722012-08-09 16:18:58 -0700725VP8StatusCode WebPIAppend(WebPIDecoder* idec,
726 const uint8_t* data, size_t data_size) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530727 VP8StatusCode status;
728 if (idec == NULL || data == NULL) {
729 return VP8_STATUS_INVALID_PARAM;
730 }
731 status = IDecCheckStatus(idec);
732 if (status != VP8_STATUS_SUSPENDED) {
733 return status;
734 }
735 // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
736 if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_APPEND)) {
737 return VP8_STATUS_INVALID_PARAM;
738 }
739 // Append data to memory buffer
740 if (!AppendToMemBuffer(idec, data, data_size)) {
741 return VP8_STATUS_OUT_OF_MEMORY;
742 }
743 return IDecode(idec);
744}
745
Vikas Aroraa2415722012-08-09 16:18:58 -0700746VP8StatusCode WebPIUpdate(WebPIDecoder* idec,
747 const uint8_t* data, size_t data_size) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530748 VP8StatusCode status;
749 if (idec == NULL || data == NULL) {
750 return VP8_STATUS_INVALID_PARAM;
751 }
752 status = IDecCheckStatus(idec);
753 if (status != VP8_STATUS_SUSPENDED) {
754 return status;
755 }
756 // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
757 if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_MAP)) {
758 return VP8_STATUS_INVALID_PARAM;
759 }
760 // Make the memory buffer point to the new buffer
761 if (!RemapMemBuffer(idec, data, data_size)) {
762 return VP8_STATUS_INVALID_PARAM;
763 }
764 return IDecode(idec);
765}
766
767//------------------------------------------------------------------------------
768
Vikas Arora46672792011-07-13 16:37:55 +0530769static const WebPDecBuffer* GetOutputBuffer(const WebPIDecoder* const idec) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700770 if (idec == NULL || idec->dec_ == NULL) {
771 return NULL;
772 }
773 if (idec->state_ <= STATE_VP8_PARTS0) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530774 return NULL;
775 }
Vikas Arora03d5e342011-06-02 23:59:44 +0530776 return idec->params_.output;
777}
778
Vikas Aroraa2415722012-08-09 16:18:58 -0700779const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* idec,
780 int* left, int* top,
781 int* width, int* height) {
Vikas Arora46672792011-07-13 16:37:55 +0530782 const WebPDecBuffer* const src = GetOutputBuffer(idec);
Vikas Aroraa2415722012-08-09 16:18:58 -0700783 if (left != NULL) *left = 0;
784 if (top != NULL) *top = 0;
Vikas Arora46672792011-07-13 16:37:55 +0530785 // TODO(skal): later include handling of rotations.
786 if (src) {
Vikas Aroraa2415722012-08-09 16:18:58 -0700787 if (width != NULL) *width = src->width;
788 if (height != NULL) *height = idec->params_.last_y;
Vikas Arora46672792011-07-13 16:37:55 +0530789 } else {
Vikas Aroraa2415722012-08-09 16:18:58 -0700790 if (width != NULL) *width = 0;
791 if (height != NULL) *height = 0;
Vikas Arora46672792011-07-13 16:37:55 +0530792 }
793 return src;
794}
795
Vikas Aroraa2415722012-08-09 16:18:58 -0700796uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int* last_y,
Vikas Arora46672792011-07-13 16:37:55 +0530797 int* width, int* height, int* stride) {
798 const WebPDecBuffer* const src = GetOutputBuffer(idec);
Vikas Aroraa2415722012-08-09 16:18:58 -0700799 if (src == NULL) return NULL;
Vikas Arora46672792011-07-13 16:37:55 +0530800 if (src->colorspace >= MODE_YUV) {
Vikas Arora03d5e342011-06-02 23:59:44 +0530801 return NULL;
802 }
803
Vikas Aroraa2415722012-08-09 16:18:58 -0700804 if (last_y != NULL) *last_y = idec->params_.last_y;
805 if (width != NULL) *width = src->width;
806 if (height != NULL) *height = src->height;
807 if (stride != NULL) *stride = src->u.RGBA.stride;
Vikas Arora03d5e342011-06-02 23:59:44 +0530808
Vikas Arora46672792011-07-13 16:37:55 +0530809 return src->u.RGBA.rgba;
810}
811
Vikas Aroraa2415722012-08-09 16:18:58 -0700812uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec, int* last_y,
813 uint8_t** u, uint8_t** v, uint8_t** a,
814 int* width, int* height,
815 int* stride, int* uv_stride, int* a_stride) {
Vikas Arora46672792011-07-13 16:37:55 +0530816 const WebPDecBuffer* const src = GetOutputBuffer(idec);
Vikas Aroraa2415722012-08-09 16:18:58 -0700817 if (src == NULL) return NULL;
Vikas Arora46672792011-07-13 16:37:55 +0530818 if (src->colorspace < MODE_YUV) {
819 return NULL;
820 }
821
Vikas Aroraa2415722012-08-09 16:18:58 -0700822 if (last_y != NULL) *last_y = idec->params_.last_y;
823 if (u != NULL) *u = src->u.YUVA.u;
824 if (v != NULL) *v = src->u.YUVA.v;
825 if (a != NULL) *a = src->u.YUVA.a;
826 if (width != NULL) *width = src->width;
827 if (height != NULL) *height = src->height;
828 if (stride != NULL) *stride = src->u.YUVA.y_stride;
829 if (uv_stride != NULL) *uv_stride = src->u.YUVA.u_stride;
830 if (a_stride != NULL) *a_stride = src->u.YUVA.a_stride;
Vikas Arora46672792011-07-13 16:37:55 +0530831
832 return src->u.YUVA.y;
Vikas Arora03d5e342011-06-02 23:59:44 +0530833}
834
Vikas Arora3417a632011-07-06 15:38:22 +0530835int WebPISetIOHooks(WebPIDecoder* const idec,
836 VP8IoPutHook put,
837 VP8IoSetupHook setup,
838 VP8IoTeardownHook teardown,
839 void* user_data) {
Vikas Arora8b720222014-01-02 16:48:02 -0800840 if (idec == NULL || idec->state_ > STATE_WEBP_HEADER) {
Vikas Arora3417a632011-07-06 15:38:22 +0530841 return 0;
842 }
843
844 idec->io_.put = put;
845 idec->io_.setup = setup;
846 idec->io_.teardown = teardown;
847 idec->io_.opaque = user_data;
848
849 return 1;
850}
851