1// Copyright 2010 Google Inc. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8// -----------------------------------------------------------------------------
9//
10// Main decoding functions for WEBP images.
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include <stdlib.h>
15
16#include "./vp8i_dec.h"
17#include "./vp8li_dec.h"
18#include "./webpi_dec.h"
19#include "../utils/utils.h"
20#include "../webp/mux_types.h"  // ALPHA_FLAG
21
22//------------------------------------------------------------------------------
23// RIFF layout is:
24//   Offset  tag
25//   0...3   "RIFF" 4-byte tag
26//   4...7   size of image data (including metadata) starting at offset 8
27//   8...11  "WEBP"   our form-type signature
28// The RIFF container (12 bytes) is followed by appropriate chunks:
29//   12..15  "VP8 ": 4-bytes tags, signaling the use of VP8 video format
30//   16..19  size of the raw VP8 image data, starting at offset 20
31//   20....  the VP8 bytes
32// Or,
33//   12..15  "VP8L": 4-bytes tags, signaling the use of VP8L lossless format
34//   16..19  size of the raw VP8L image data, starting at offset 20
35//   20....  the VP8L bytes
36// Or,
37//   12..15  "VP8X": 4-bytes tags, describing the extended-VP8 chunk.
38//   16..19  size of the VP8X chunk starting at offset 20.
39//   20..23  VP8X flags bit-map corresponding to the chunk-types present.
40//   24..26  Width of the Canvas Image.
41//   27..29  Height of the Canvas Image.
42// There can be extra chunks after the "VP8X" chunk (ICCP, ANMF, VP8, VP8L,
43// XMP, EXIF  ...)
44// All sizes are in little-endian order.
45// Note: chunk data size must be padded to multiple of 2 when written.
46
47// Validates the RIFF container (if detected) and skips over it.
48// If a RIFF container is detected, returns:
49//     VP8_STATUS_BITSTREAM_ERROR for invalid header,
50//     VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true,
51// and VP8_STATUS_OK otherwise.
52// In case there are not enough bytes (partial RIFF container), return 0 for
53// *riff_size. Else return the RIFF size extracted from the header.
54static VP8StatusCode ParseRIFF(const uint8_t** const data,
55                               size_t* const data_size, int have_all_data,
56                               size_t* const riff_size) {
57  assert(data != NULL);
58  assert(data_size != NULL);
59  assert(riff_size != NULL);
60
61  *riff_size = 0;  // Default: no RIFF present.
62  if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {
63    if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
64      return VP8_STATUS_BITSTREAM_ERROR;  // Wrong image file signature.
65    } else {
66      const uint32_t size = GetLE32(*data + TAG_SIZE);
67      // Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
68      if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {
69        return VP8_STATUS_BITSTREAM_ERROR;
70      }
71      if (size > MAX_CHUNK_PAYLOAD) {
72        return VP8_STATUS_BITSTREAM_ERROR;
73      }
74      if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
75        return VP8_STATUS_NOT_ENOUGH_DATA;  // Truncated bitstream.
76      }
77      // We have a RIFF container. Skip it.
78      *riff_size = size;
79      *data += RIFF_HEADER_SIZE;
80      *data_size -= RIFF_HEADER_SIZE;
81    }
82  }
83  return VP8_STATUS_OK;
84}
85
86// Validates the VP8X header and skips over it.
87// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,
88//         VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
89//         VP8_STATUS_OK otherwise.
90// If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,
91// *height_ptr and *flags_ptr are set to the corresponding values extracted
92// from the VP8X chunk.
93static VP8StatusCode ParseVP8X(const uint8_t** const data,
94                               size_t* const data_size,
95                               int* const found_vp8x,
96                               int* const width_ptr, int* const height_ptr,
97                               uint32_t* const flags_ptr) {
98  const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
99  assert(data != NULL);
100  assert(data_size != NULL);
101  assert(found_vp8x != NULL);
102
103  *found_vp8x = 0;
104
105  if (*data_size < CHUNK_HEADER_SIZE) {
106    return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
107  }
108
109  if (!memcmp(*data, "VP8X", TAG_SIZE)) {
110    int width, height;
111    uint32_t flags;
112    const uint32_t chunk_size = GetLE32(*data + TAG_SIZE);
113    if (chunk_size != VP8X_CHUNK_SIZE) {
114      return VP8_STATUS_BITSTREAM_ERROR;  // Wrong chunk size.
115    }
116
117    // Verify if enough data is available to validate the VP8X chunk.
118    if (*data_size < vp8x_size) {
119      return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
120    }
121    flags = GetLE32(*data + 8);
122    width = 1 + GetLE24(*data + 12);
123    height = 1 + GetLE24(*data + 15);
124    if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
125      return VP8_STATUS_BITSTREAM_ERROR;  // image is too large
126    }
127
128    if (flags_ptr != NULL) *flags_ptr = flags;
129    if (width_ptr != NULL) *width_ptr = width;
130    if (height_ptr != NULL) *height_ptr = height;
131    // Skip over VP8X header bytes.
132    *data += vp8x_size;
133    *data_size -= vp8x_size;
134    *found_vp8x = 1;
135  }
136  return VP8_STATUS_OK;
137}
138
139// Skips to the next VP8/VP8L chunk header in the data given the size of the
140// RIFF chunk 'riff_size'.
141// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,
142//         VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
143//         VP8_STATUS_OK otherwise.
144// If an alpha chunk is found, *alpha_data and *alpha_size are set
145// appropriately.
146static VP8StatusCode ParseOptionalChunks(const uint8_t** const data,
147                                         size_t* const data_size,
148                                         size_t const riff_size,
149                                         const uint8_t** const alpha_data,
150                                         size_t* const alpha_size) {
151  const uint8_t* buf;
152  size_t buf_size;
153  uint32_t total_size = TAG_SIZE +           // "WEBP".
154                        CHUNK_HEADER_SIZE +  // "VP8Xnnnn".
155                        VP8X_CHUNK_SIZE;     // data.
156  assert(data != NULL);
157  assert(data_size != NULL);
158  buf = *data;
159  buf_size = *data_size;
160
161  assert(alpha_data != NULL);
162  assert(alpha_size != NULL);
163  *alpha_data = NULL;
164  *alpha_size = 0;
165
166  while (1) {
167    uint32_t chunk_size;
168    uint32_t disk_chunk_size;   // chunk_size with padding
169
170    *data = buf;
171    *data_size = buf_size;
172
173    if (buf_size < CHUNK_HEADER_SIZE) {  // Insufficient data.
174      return VP8_STATUS_NOT_ENOUGH_DATA;
175    }
176
177    chunk_size = GetLE32(buf + TAG_SIZE);
178    if (chunk_size > MAX_CHUNK_PAYLOAD) {
179      return VP8_STATUS_BITSTREAM_ERROR;          // Not a valid chunk size.
180    }
181    // For odd-sized chunk-payload, there's one byte padding at the end.
182    disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
183    total_size += disk_chunk_size;
184
185    // Check that total bytes skipped so far does not exceed riff_size.
186    if (riff_size > 0 && (total_size > riff_size)) {
187      return VP8_STATUS_BITSTREAM_ERROR;          // Not a valid chunk size.
188    }
189
190    // Start of a (possibly incomplete) VP8/VP8L chunk implies that we have
191    // parsed all the optional chunks.
192    // Note: This check must occur before the check 'buf_size < disk_chunk_size'
193    // below to allow incomplete VP8/VP8L chunks.
194    if (!memcmp(buf, "VP8 ", TAG_SIZE) ||
195        !memcmp(buf, "VP8L", TAG_SIZE)) {
196      return VP8_STATUS_OK;
197    }
198
199    if (buf_size < disk_chunk_size) {             // Insufficient data.
200      return VP8_STATUS_NOT_ENOUGH_DATA;
201    }
202
203    if (!memcmp(buf, "ALPH", TAG_SIZE)) {         // A valid ALPH header.
204      *alpha_data = buf + CHUNK_HEADER_SIZE;
205      *alpha_size = chunk_size;
206    }
207
208    // We have a full and valid chunk; skip it.
209    buf += disk_chunk_size;
210    buf_size -= disk_chunk_size;
211  }
212}
213
214// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
215// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
216//         riff_size) VP8/VP8L header,
217//         VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
218//         VP8_STATUS_OK otherwise.
219// If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes
220// extracted from the VP8/VP8L chunk header.
221// The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.
222static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,
223                                    size_t* const data_size, int have_all_data,
224                                    size_t riff_size, size_t* const chunk_size,
225                                    int* const is_lossless) {
226  const uint8_t* const data = *data_ptr;
227  const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);
228  const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);
229  const uint32_t minimal_size =
230      TAG_SIZE + CHUNK_HEADER_SIZE;  // "WEBP" + "VP8 nnnn" OR
231                                     // "WEBP" + "VP8Lnnnn"
232  assert(data != NULL);
233  assert(data_size != NULL);
234  assert(chunk_size != NULL);
235  assert(is_lossless != NULL);
236
237  if (*data_size < CHUNK_HEADER_SIZE) {
238    return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
239  }
240
241  if (is_vp8 || is_vp8l) {
242    // Bitstream contains VP8/VP8L header.
243    const uint32_t size = GetLE32(data + TAG_SIZE);
244    if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {
245      return VP8_STATUS_BITSTREAM_ERROR;  // Inconsistent size information.
246    }
247    if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
248      return VP8_STATUS_NOT_ENOUGH_DATA;  // Truncated bitstream.
249    }
250    // Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.
251    *chunk_size = size;
252    *data_ptr += CHUNK_HEADER_SIZE;
253    *data_size -= CHUNK_HEADER_SIZE;
254    *is_lossless = is_vp8l;
255  } else {
256    // Raw VP8/VP8L bitstream (no header).
257    *is_lossless = VP8LCheckSignature(data, *data_size);
258    *chunk_size = *data_size;
259  }
260
261  return VP8_STATUS_OK;
262}
263
264//------------------------------------------------------------------------------
265
266// Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on
267// 'data'. All the output parameters may be NULL. If 'headers' is NULL only the
268// minimal amount will be read to fetch the remaining parameters.
269// If 'headers' is non-NULL this function will attempt to locate both alpha
270// data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).
271// Note: The following chunk sequences (before the raw VP8/VP8L data) are
272// considered valid by this function:
273// RIFF + VP8(L)
274// RIFF + VP8X + (optional chunks) + VP8(L)
275// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
276// VP8(L)     <-- Not a valid WebP format: only allowed for internal purpose.
277static VP8StatusCode ParseHeadersInternal(const uint8_t* data,
278                                          size_t data_size,
279                                          int* const width,
280                                          int* const height,
281                                          int* const has_alpha,
282                                          int* const has_animation,
283                                          int* const format,
284                                          WebPHeaderStructure* const headers) {
285  int canvas_width = 0;
286  int canvas_height = 0;
287  int image_width = 0;
288  int image_height = 0;
289  int found_riff = 0;
290  int found_vp8x = 0;
291  int animation_present = 0;
292  const int have_all_data = (headers != NULL) ? headers->have_all_data : 0;
293
294  VP8StatusCode status;
295  WebPHeaderStructure hdrs;
296
297  if (data == NULL || data_size < RIFF_HEADER_SIZE) {
298    return VP8_STATUS_NOT_ENOUGH_DATA;
299  }
300  memset(&hdrs, 0, sizeof(hdrs));
301  hdrs.data = data;
302  hdrs.data_size = data_size;
303
304  // Skip over RIFF header.
305  status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size);
306  if (status != VP8_STATUS_OK) {
307    return status;   // Wrong RIFF header / insufficient data.
308  }
309  found_riff = (hdrs.riff_size > 0);
310
311  // Skip over VP8X.
312  {
313    uint32_t flags = 0;
314    status = ParseVP8X(&data, &data_size, &found_vp8x,
315                       &canvas_width, &canvas_height, &flags);
316    if (status != VP8_STATUS_OK) {
317      return status;  // Wrong VP8X / insufficient data.
318    }
319    animation_present = !!(flags & ANIMATION_FLAG);
320    if (!found_riff && found_vp8x) {
321      // Note: This restriction may be removed in the future, if it becomes
322      // necessary to send VP8X chunk to the decoder.
323      return VP8_STATUS_BITSTREAM_ERROR;
324    }
325    if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG);
326    if (has_animation != NULL) *has_animation = animation_present;
327    if (format != NULL) *format = 0;   // default = undefined
328
329    image_width = canvas_width;
330    image_height = canvas_height;
331    if (found_vp8x && animation_present && headers == NULL) {
332      status = VP8_STATUS_OK;
333      goto ReturnWidthHeight;  // Just return features from VP8X header.
334    }
335  }
336
337  if (data_size < TAG_SIZE) {
338    status = VP8_STATUS_NOT_ENOUGH_DATA;
339    goto ReturnWidthHeight;
340  }
341
342  // Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".
343  if ((found_riff && found_vp8x) ||
344      (!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {
345    status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,
346                                 &hdrs.alpha_data, &hdrs.alpha_data_size);
347    if (status != VP8_STATUS_OK) {
348      goto ReturnWidthHeight;  // Invalid chunk size / insufficient data.
349    }
350  }
351
352  // Skip over VP8/VP8L header.
353  status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size,
354                          &hdrs.compressed_size, &hdrs.is_lossless);
355  if (status != VP8_STATUS_OK) {
356    goto ReturnWidthHeight;  // Wrong VP8/VP8L chunk-header / insufficient data.
357  }
358  if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {
359    return VP8_STATUS_BITSTREAM_ERROR;
360  }
361
362  if (format != NULL && !animation_present) {
363    *format = hdrs.is_lossless ? 2 : 1;
364  }
365
366  if (!hdrs.is_lossless) {
367    if (data_size < VP8_FRAME_HEADER_SIZE) {
368      status = VP8_STATUS_NOT_ENOUGH_DATA;
369      goto ReturnWidthHeight;
370    }
371    // Validates raw VP8 data.
372    if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size,
373                    &image_width, &image_height)) {
374      return VP8_STATUS_BITSTREAM_ERROR;
375    }
376  } else {
377    if (data_size < VP8L_FRAME_HEADER_SIZE) {
378      status = VP8_STATUS_NOT_ENOUGH_DATA;
379      goto ReturnWidthHeight;
380    }
381    // Validates raw VP8L data.
382    if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) {
383      return VP8_STATUS_BITSTREAM_ERROR;
384    }
385  }
386  // Validates image size coherency.
387  if (found_vp8x) {
388    if (canvas_width != image_width || canvas_height != image_height) {
389      return VP8_STATUS_BITSTREAM_ERROR;
390    }
391  }
392  if (headers != NULL) {
393    *headers = hdrs;
394    headers->offset = data - headers->data;
395    assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);
396    assert(headers->offset == headers->data_size - data_size);
397  }
398 ReturnWidthHeight:
399  if (status == VP8_STATUS_OK ||
400      (status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) {
401    if (has_alpha != NULL) {
402      // If the data did not contain a VP8X/VP8L chunk the only definitive way
403      // to set this is by looking for alpha data (from an ALPH chunk).
404      *has_alpha |= (hdrs.alpha_data != NULL);
405    }
406    if (width != NULL) *width = image_width;
407    if (height != NULL) *height = image_height;
408    return VP8_STATUS_OK;
409  } else {
410    return status;
411  }
412}
413
414VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
415  // status is marked volatile as a workaround for a clang-3.8 (aarch64) bug
416  volatile VP8StatusCode status;
417  int has_animation = 0;
418  assert(headers != NULL);
419  // fill out headers, ignore width/height/has_alpha.
420  status = ParseHeadersInternal(headers->data, headers->data_size,
421                                NULL, NULL, NULL, &has_animation,
422                                NULL, headers);
423  if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) {
424    // TODO(jzern): full support of animation frames will require API additions.
425    if (has_animation) {
426      status = VP8_STATUS_UNSUPPORTED_FEATURE;
427    }
428  }
429  return status;
430}
431
432//------------------------------------------------------------------------------
433// WebPDecParams
434
435void WebPResetDecParams(WebPDecParams* const params) {
436  if (params != NULL) {
437    memset(params, 0, sizeof(*params));
438  }
439}
440
441//------------------------------------------------------------------------------
442// "Into" decoding variants
443
444// Main flow
445static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,
446                                WebPDecParams* const params) {
447  VP8StatusCode status;
448  VP8Io io;
449  WebPHeaderStructure headers;
450
451  headers.data = data;
452  headers.data_size = data_size;
453  headers.have_all_data = 1;
454  status = WebPParseHeaders(&headers);   // Process Pre-VP8 chunks.
455  if (status != VP8_STATUS_OK) {
456    return status;
457  }
458
459  assert(params != NULL);
460  VP8InitIo(&io);
461  io.data = headers.data + headers.offset;
462  io.data_size = headers.data_size - headers.offset;
463  WebPInitCustomIo(params, &io);  // Plug the I/O functions.
464
465  if (!headers.is_lossless) {
466    VP8Decoder* const dec = VP8New();
467    if (dec == NULL) {
468      return VP8_STATUS_OUT_OF_MEMORY;
469    }
470    dec->alpha_data_ = headers.alpha_data;
471    dec->alpha_data_size_ = headers.alpha_data_size;
472
473    // Decode bitstream header, update io->width/io->height.
474    if (!VP8GetHeaders(dec, &io)) {
475      status = dec->status_;   // An error occurred. Grab error status.
476    } else {
477      // Allocate/check output buffers.
478      status = WebPAllocateDecBuffer(io.width, io.height, params->options,
479                                     params->output);
480      if (status == VP8_STATUS_OK) {  // Decode
481        // This change must be done before calling VP8Decode()
482        dec->mt_method_ = VP8GetThreadMethod(params->options, &headers,
483                                             io.width, io.height);
484        VP8InitDithering(params->options, dec);
485        if (!VP8Decode(dec, &io)) {
486          status = dec->status_;
487        }
488      }
489    }
490    VP8Delete(dec);
491  } else {
492    VP8LDecoder* const dec = VP8LNew();
493    if (dec == NULL) {
494      return VP8_STATUS_OUT_OF_MEMORY;
495    }
496    if (!VP8LDecodeHeader(dec, &io)) {
497      status = dec->status_;   // An error occurred. Grab error status.
498    } else {
499      // Allocate/check output buffers.
500      status = WebPAllocateDecBuffer(io.width, io.height, params->options,
501                                     params->output);
502      if (status == VP8_STATUS_OK) {  // Decode
503        if (!VP8LDecodeImage(dec)) {
504          status = dec->status_;
505        }
506      }
507    }
508    VP8LDelete(dec);
509  }
510
511  if (status != VP8_STATUS_OK) {
512    WebPFreeDecBuffer(params->output);
513  } else {
514    if (params->options != NULL && params->options->flip) {
515      // This restores the original stride values if options->flip was used
516      // during the call to WebPAllocateDecBuffer above.
517      status = WebPFlipBuffer(params->output);
518    }
519  }
520  return status;
521}
522
523// Helpers
524static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
525                                     const uint8_t* const data,
526                                     size_t data_size,
527                                     uint8_t* const rgba,
528                                     int stride, size_t size) {
529  WebPDecParams params;
530  WebPDecBuffer buf;
531  if (rgba == NULL) {
532    return NULL;
533  }
534  WebPInitDecBuffer(&buf);
535  WebPResetDecParams(&params);
536  params.output = &buf;
537  buf.colorspace    = colorspace;
538  buf.u.RGBA.rgba   = rgba;
539  buf.u.RGBA.stride = stride;
540  buf.u.RGBA.size   = size;
541  buf.is_external_memory = 1;
542  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
543    return NULL;
544  }
545  return rgba;
546}
547
548uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
549                           uint8_t* output, size_t size, int stride) {
550  return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
551}
552
553uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
554                            uint8_t* output, size_t size, int stride) {
555  return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
556}
557
558uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
559                            uint8_t* output, size_t size, int stride) {
560  return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
561}
562
563uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
564                           uint8_t* output, size_t size, int stride) {
565  return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
566}
567
568uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
569                            uint8_t* output, size_t size, int stride) {
570  return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
571}
572
573uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,
574                           uint8_t* luma, size_t luma_size, int luma_stride,
575                           uint8_t* u, size_t u_size, int u_stride,
576                           uint8_t* v, size_t v_size, int v_stride) {
577  WebPDecParams params;
578  WebPDecBuffer output;
579  if (luma == NULL) return NULL;
580  WebPInitDecBuffer(&output);
581  WebPResetDecParams(&params);
582  params.output = &output;
583  output.colorspace      = MODE_YUV;
584  output.u.YUVA.y        = luma;
585  output.u.YUVA.y_stride = luma_stride;
586  output.u.YUVA.y_size   = luma_size;
587  output.u.YUVA.u        = u;
588  output.u.YUVA.u_stride = u_stride;
589  output.u.YUVA.u_size   = u_size;
590  output.u.YUVA.v        = v;
591  output.u.YUVA.v_stride = v_stride;
592  output.u.YUVA.v_size   = v_size;
593  output.is_external_memory = 1;
594  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
595    return NULL;
596  }
597  return luma;
598}
599
600//------------------------------------------------------------------------------
601
602static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data,
603                       size_t data_size, int* const width, int* const height,
604                       WebPDecBuffer* const keep_info) {
605  WebPDecParams params;
606  WebPDecBuffer output;
607
608  WebPInitDecBuffer(&output);
609  WebPResetDecParams(&params);
610  params.output = &output;
611  output.colorspace = mode;
612
613  // Retrieve (and report back) the required dimensions from bitstream.
614  if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
615    return NULL;
616  }
617  if (width != NULL) *width = output.width;
618  if (height != NULL) *height = output.height;
619
620  // Decode
621  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
622    return NULL;
623  }
624  if (keep_info != NULL) {    // keep track of the side-info
625    WebPCopyDecBuffer(&output, keep_info);
626  }
627  // return decoded samples (don't clear 'output'!)
628  return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;
629}
630
631uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
632                       int* width, int* height) {
633  return Decode(MODE_RGB, data, data_size, width, height, NULL);
634}
635
636uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
637                        int* width, int* height) {
638  return Decode(MODE_RGBA, data, data_size, width, height, NULL);
639}
640
641uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
642                        int* width, int* height) {
643  return Decode(MODE_ARGB, data, data_size, width, height, NULL);
644}
645
646uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
647                       int* width, int* height) {
648  return Decode(MODE_BGR, data, data_size, width, height, NULL);
649}
650
651uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
652                        int* width, int* height) {
653  return Decode(MODE_BGRA, data, data_size, width, height, NULL);
654}
655
656uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
657                       int* width, int* height, uint8_t** u, uint8_t** v,
658                       int* stride, int* uv_stride) {
659  WebPDecBuffer output;   // only to preserve the side-infos
660  uint8_t* const out = Decode(MODE_YUV, data, data_size,
661                              width, height, &output);
662
663  if (out != NULL) {
664    const WebPYUVABuffer* const buf = &output.u.YUVA;
665    *u = buf->u;
666    *v = buf->v;
667    *stride = buf->y_stride;
668    *uv_stride = buf->u_stride;
669    assert(buf->u_stride == buf->v_stride);
670  }
671  return out;
672}
673
674static void DefaultFeatures(WebPBitstreamFeatures* const features) {
675  assert(features != NULL);
676  memset(features, 0, sizeof(*features));
677}
678
679static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,
680                                 WebPBitstreamFeatures* const features) {
681  if (features == NULL || data == NULL) {
682    return VP8_STATUS_INVALID_PARAM;
683  }
684  DefaultFeatures(features);
685
686  // Only parse enough of the data to retrieve the features.
687  return ParseHeadersInternal(data, data_size,
688                              &features->width, &features->height,
689                              &features->has_alpha, &features->has_animation,
690                              &features->format, NULL);
691}
692
693//------------------------------------------------------------------------------
694// WebPGetInfo()
695
696int WebPGetInfo(const uint8_t* data, size_t data_size,
697                int* width, int* height) {
698  WebPBitstreamFeatures features;
699
700  if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {
701    return 0;
702  }
703
704  if (width != NULL) {
705    *width  = features.width;
706  }
707  if (height != NULL) {
708    *height = features.height;
709  }
710
711  return 1;
712}
713
714//------------------------------------------------------------------------------
715// Advance decoding API
716
717int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,
718                                  int version) {
719  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
720    return 0;   // version mismatch
721  }
722  if (config == NULL) {
723    return 0;
724  }
725  memset(config, 0, sizeof(*config));
726  DefaultFeatures(&config->input);
727  WebPInitDecBuffer(&config->output);
728  return 1;
729}
730
731VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,
732                                      WebPBitstreamFeatures* features,
733                                      int version) {
734  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
735    return VP8_STATUS_INVALID_PARAM;   // version mismatch
736  }
737  if (features == NULL) {
738    return VP8_STATUS_INVALID_PARAM;
739  }
740  return GetFeatures(data, data_size, features);
741}
742
743VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
744                         WebPDecoderConfig* config) {
745  WebPDecParams params;
746  VP8StatusCode status;
747
748  if (config == NULL) {
749    return VP8_STATUS_INVALID_PARAM;
750  }
751
752  status = GetFeatures(data, data_size, &config->input);
753  if (status != VP8_STATUS_OK) {
754    if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
755      return VP8_STATUS_BITSTREAM_ERROR;  // Not-enough-data treated as error.
756    }
757    return status;
758  }
759
760  WebPResetDecParams(&params);
761  params.options = &config->options;
762  params.output = &config->output;
763  if (WebPAvoidSlowMemory(params.output, &config->input)) {
764    // decoding to slow memory: use a temporary in-mem buffer to decode into.
765    WebPDecBuffer in_mem_buffer;
766    WebPInitDecBuffer(&in_mem_buffer);
767    in_mem_buffer.colorspace = config->output.colorspace;
768    in_mem_buffer.width = config->input.width;
769    in_mem_buffer.height = config->input.height;
770    params.output = &in_mem_buffer;
771    status = DecodeInto(data, data_size, &params);
772    if (status == VP8_STATUS_OK) {  // do the slow-copy
773      status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output);
774    }
775    WebPFreeDecBuffer(&in_mem_buffer);
776  } else {
777    status = DecodeInto(data, data_size, &params);
778  }
779
780  return status;
781}
782
783//------------------------------------------------------------------------------
784// Cropping and rescaling.
785
786int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
787                          VP8Io* const io, WEBP_CSP_MODE src_colorspace) {
788  const int W = io->width;
789  const int H = io->height;
790  int x = 0, y = 0, w = W, h = H;
791
792  // Cropping
793  io->use_cropping = (options != NULL) && (options->use_cropping > 0);
794  if (io->use_cropping) {
795    w = options->crop_width;
796    h = options->crop_height;
797    x = options->crop_left;
798    y = options->crop_top;
799    if (!WebPIsRGBMode(src_colorspace)) {   // only snap for YUV420
800      x &= ~1;
801      y &= ~1;
802    }
803    if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) {
804      return 0;  // out of frame boundary error
805    }
806  }
807  io->crop_left   = x;
808  io->crop_top    = y;
809  io->crop_right  = x + w;
810  io->crop_bottom = y + h;
811  io->mb_w = w;
812  io->mb_h = h;
813
814  // Scaling
815  io->use_scaling = (options != NULL) && (options->use_scaling > 0);
816  if (io->use_scaling) {
817    int scaled_width = options->scaled_width;
818    int scaled_height = options->scaled_height;
819    if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) {
820      return 0;
821    }
822    io->scaled_width = scaled_width;
823    io->scaled_height = scaled_height;
824  }
825
826  // Filter
827  io->bypass_filtering = (options != NULL) && options->bypass_filtering;
828
829  // Fancy upsampler
830#ifdef FANCY_UPSAMPLING
831  io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);
832#endif
833
834  if (io->use_scaling) {
835    // disable filter (only for large downscaling ratio).
836    io->bypass_filtering = (io->scaled_width < W * 3 / 4) &&
837                           (io->scaled_height < H * 3 / 4);
838    io->fancy_upsampling = 0;
839  }
840  return 1;
841}
842
843//------------------------------------------------------------------------------
844