FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
webp.c
Go to the documentation of this file.
1 /*
2  * WebP (.webp) image decoder
3  * Copyright (c) 2013 Aneesh Dogra <aneesh@sugarlabs.org>
4  * Copyright (c) 2013 Justin Ruggles <justin.ruggles@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * WebP image decoder
26  *
27  * @author Aneesh Dogra <aneesh@sugarlabs.org>
28  * Container and Lossy decoding
29  *
30  * @author Justin Ruggles <justin.ruggles@gmail.com>
31  * Lossless decoder
32  * Compressed alpha for lossy
33  *
34  * @author James Almer <jamrial@gmail.com>
35  * Exif metadata
36  *
37  * Unimplemented:
38  * - Animation
39  * - ICC profile
40  * - XMP metadata
41  */
42 
43 #define BITSTREAM_READER_LE
44 #include "libavutil/imgutils.h"
45 #include "avcodec.h"
46 #include "bytestream.h"
47 #include "exif.h"
48 #include "internal.h"
49 #include "get_bits.h"
50 #include "thread.h"
51 #include "vp8.h"
52 
53 #define VP8X_FLAG_ANIMATION 0x02
54 #define VP8X_FLAG_XMP_METADATA 0x04
55 #define VP8X_FLAG_EXIF_METADATA 0x08
56 #define VP8X_FLAG_ALPHA 0x10
57 #define VP8X_FLAG_ICC 0x20
58 
59 #define MAX_PALETTE_SIZE 256
60 #define MAX_CACHE_BITS 11
61 #define NUM_CODE_LENGTH_CODES 19
62 #define HUFFMAN_CODES_PER_META_CODE 5
63 #define NUM_LITERAL_CODES 256
64 #define NUM_LENGTH_CODES 24
65 #define NUM_DISTANCE_CODES 40
66 #define NUM_SHORT_DISTANCES 120
67 #define MAX_HUFFMAN_CODE_LENGTH 15
68 
69 static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE] = {
73 };
74 
76  17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
77 };
78 
79 static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2] = {
80  { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
81  { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
82  { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
83  { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
84  { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
85  { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
86  { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
87  { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
88  { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
89  { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
90  { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
91  { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
92  { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
93  { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
94  { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
95 };
96 
100 };
101 
107 };
108 
114 };
115 
131 };
132 
139 };
140 
141 /* The structure of WebP lossless is an optional series of transformation data,
142  * followed by the primary image. The primary image also optionally contains
143  * an entropy group mapping if there are multiple entropy groups. There is a
144  * basic image type called an "entropy coded image" that is used for all of
145  * these. The type of each entropy coded image is referred to by the
146  * specification as its role. */
147 enum ImageRole {
148  /* Primary Image: Stores the actual pixels of the image. */
150 
151  /* Entropy Image: Defines which Huffman group to use for different areas of
152  * the primary image. */
154 
155  /* Predictors: Defines which predictor type to use for different areas of
156  * the primary image. */
158 
159  /* Color Transform Data: Defines the color transformation for different
160  * areas of the primary image. */
162 
163  /* Color Index: Stored as an image of height == 1. */
165 
167 };
168 
169 typedef struct HuffReader {
170  VLC vlc; /* Huffman decoder context */
171  int simple; /* whether to use simple mode */
172  int nb_symbols; /* number of coded symbols */
173  uint16_t simple_symbols[2]; /* symbols for simple mode */
174 } HuffReader;
175 
176 typedef struct ImageContext {
177  enum ImageRole role; /* role of this image */
178  AVFrame *frame; /* AVFrame for data */
179  int color_cache_bits; /* color cache size, log2 */
180  uint32_t *color_cache; /* color cache data */
181  int nb_huffman_groups; /* number of huffman groups */
182  HuffReader *huffman_groups; /* reader for each huffman group */
183  int size_reduction; /* relative size compared to primary image, log2 */
185 } ImageContext;
186 
187 typedef struct WebPContext {
188  VP8Context v; /* VP8 Context used for lossy decoding */
189  GetBitContext gb; /* bitstream reader for main image chunk */
190  AVFrame *alpha_frame; /* AVFrame for alpha data decompressed from VP8L */
191  AVCodecContext *avctx; /* parent AVCodecContext */
192  int initialized; /* set once the VP8 context is initialized */
193  int has_alpha; /* has a separate alpha chunk */
194  enum AlphaCompression alpha_compression; /* compression type for alpha chunk */
195  enum AlphaFilter alpha_filter; /* filtering method for alpha chunk */
196  uint8_t *alpha_data; /* alpha chunk data */
197  int alpha_data_size; /* alpha chunk data size */
198  int has_exif; /* set after an EXIF chunk has been processed */
199  AVDictionary *exif_metadata; /* EXIF chunk data */
200  int width; /* image width */
201  int height; /* image height */
202  int lossless; /* indicates lossless or lossy */
203 
204  int nb_transforms; /* number of transforms */
205  enum TransformType transforms[4]; /* transformations used in the image, in order */
206  int reduced_width; /* reduced width for index image, if applicable */
207  int nb_huffman_groups; /* number of huffman groups in the primary image */
208  ImageContext image[IMAGE_ROLE_NB]; /* image context for each role */
209 } WebPContext;
210 
211 #define GET_PIXEL(frame, x, y) \
212  ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
213 
214 #define GET_PIXEL_COMP(frame, x, y, c) \
215  (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
216 
218 {
219  int i, j;
220 
221  av_free(img->color_cache);
222  if (img->role != IMAGE_ROLE_ARGB && !img->is_alpha_primary)
223  av_frame_free(&img->frame);
224  if (img->huffman_groups) {
225  for (i = 0; i < img->nb_huffman_groups; i++) {
226  for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++)
227  ff_free_vlc(&img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE + j].vlc);
228  }
229  av_free(img->huffman_groups);
230  }
231  memset(img, 0, sizeof(*img));
232 }
233 
234 
235 /* Differs from get_vlc2() in the following ways:
236  * - codes are bit-reversed
237  * - assumes 8-bit table to make reversal simpler
238  * - assumes max depth of 2 since the max code length for WebP is 15
239  */
241 {
242  int n, nb_bits;
243  unsigned int index;
244  int code;
245 
246  OPEN_READER(re, gb);
247  UPDATE_CACHE(re, gb);
248 
249  index = SHOW_UBITS(re, gb, 8);
250  index = ff_reverse[index];
251  code = table[index][0];
252  n = table[index][1];
253 
254  if (n < 0) {
255  LAST_SKIP_BITS(re, gb, 8);
256  UPDATE_CACHE(re, gb);
257 
258  nb_bits = -n;
259 
260  index = SHOW_UBITS(re, gb, nb_bits);
261  index = (ff_reverse[index] >> (8 - nb_bits)) + code;
262  code = table[index][0];
263  n = table[index][1];
264  }
265  SKIP_BITS(re, gb, n);
266 
267  CLOSE_READER(re, gb);
268 
269  return code;
270 }
271 
273 {
274  if (r->simple) {
275  if (r->nb_symbols == 1)
276  return r->simple_symbols[0];
277  else
278  return r->simple_symbols[get_bits1(gb)];
279  } else
280  return webp_get_vlc(gb, r->vlc.table);
281 }
282 
283 static int huff_reader_build_canonical(HuffReader *r, int *code_lengths,
284  int alphabet_size)
285 {
286  int len = 0, sym, code = 0, ret;
287  int max_code_length = 0;
288  uint16_t *codes;
289 
290  /* special-case 1 symbol since the vlc reader cannot handle it */
291  for (sym = 0; sym < alphabet_size; sym++) {
292  if (code_lengths[sym] > 0) {
293  len++;
294  code = sym;
295  if (len > 1)
296  break;
297  }
298  }
299  if (len == 1) {
300  r->nb_symbols = 1;
301  r->simple_symbols[0] = code;
302  r->simple = 1;
303  return 0;
304  }
305 
306  for (sym = 0; sym < alphabet_size; sym++)
307  max_code_length = FFMAX(max_code_length, code_lengths[sym]);
308 
309  if (max_code_length == 0 || max_code_length > MAX_HUFFMAN_CODE_LENGTH)
310  return AVERROR(EINVAL);
311 
312  codes = av_malloc_array(alphabet_size, sizeof(*codes));
313  if (!codes)
314  return AVERROR(ENOMEM);
315 
316  code = 0;
317  r->nb_symbols = 0;
318  for (len = 1; len <= max_code_length; len++) {
319  for (sym = 0; sym < alphabet_size; sym++) {
320  if (code_lengths[sym] != len)
321  continue;
322  codes[sym] = code++;
323  r->nb_symbols++;
324  }
325  code <<= 1;
326  }
327  if (!r->nb_symbols) {
328  av_free(codes);
329  return AVERROR_INVALIDDATA;
330  }
331 
332  ret = init_vlc(&r->vlc, 8, alphabet_size,
333  code_lengths, sizeof(*code_lengths), sizeof(*code_lengths),
334  codes, sizeof(*codes), sizeof(*codes), 0);
335  if (ret < 0) {
336  av_free(codes);
337  return ret;
338  }
339  r->simple = 0;
340 
341  av_free(codes);
342  return 0;
343 }
344 
346 {
347  hc->nb_symbols = get_bits1(&s->gb) + 1;
348 
349  if (get_bits1(&s->gb))
350  hc->simple_symbols[0] = get_bits(&s->gb, 8);
351  else
352  hc->simple_symbols[0] = get_bits1(&s->gb);
353 
354  if (hc->nb_symbols == 2)
355  hc->simple_symbols[1] = get_bits(&s->gb, 8);
356 
357  hc->simple = 1;
358 }
359 
361  int alphabet_size)
362 {
363  HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
364  int *code_lengths = NULL;
365  int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
366  int i, symbol, max_symbol, prev_code_len, ret;
367  int num_codes = 4 + get_bits(&s->gb, 4);
368 
369  if (num_codes > NUM_CODE_LENGTH_CODES)
370  return AVERROR_INVALIDDATA;
371 
372  for (i = 0; i < num_codes; i++)
373  code_length_code_lengths[code_length_code_order[i]] = get_bits(&s->gb, 3);
374 
375  ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths,
377  if (ret < 0)
378  goto finish;
379 
380  code_lengths = av_mallocz_array(alphabet_size, sizeof(*code_lengths));
381  if (!code_lengths) {
382  ret = AVERROR(ENOMEM);
383  goto finish;
384  }
385 
386  if (get_bits1(&s->gb)) {
387  int bits = 2 + 2 * get_bits(&s->gb, 3);
388  max_symbol = 2 + get_bits(&s->gb, bits);
389  if (max_symbol > alphabet_size) {
390  av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n",
391  max_symbol, alphabet_size);
392  ret = AVERROR_INVALIDDATA;
393  goto finish;
394  }
395  } else {
396  max_symbol = alphabet_size;
397  }
398 
399  prev_code_len = 8;
400  symbol = 0;
401  while (symbol < alphabet_size) {
402  int code_len;
403 
404  if (!max_symbol--)
405  break;
406  code_len = huff_reader_get_symbol(&code_len_hc, &s->gb);
407  if (code_len < 16) {
408  /* Code length code [0..15] indicates literal code lengths. */
409  code_lengths[symbol++] = code_len;
410  if (code_len)
411  prev_code_len = code_len;
412  } else {
413  int repeat = 0, length = 0;
414  switch (code_len) {
415  case 16:
416  /* Code 16 repeats the previous non-zero value [3..6] times,
417  * i.e., 3 + ReadBits(2) times. If code 16 is used before a
418  * non-zero value has been emitted, a value of 8 is repeated. */
419  repeat = 3 + get_bits(&s->gb, 2);
420  length = prev_code_len;
421  break;
422  case 17:
423  /* Code 17 emits a streak of zeros [3..10], i.e.,
424  * 3 + ReadBits(3) times. */
425  repeat = 3 + get_bits(&s->gb, 3);
426  break;
427  case 18:
428  /* Code 18 emits a streak of zeros of length [11..138], i.e.,
429  * 11 + ReadBits(7) times. */
430  repeat = 11 + get_bits(&s->gb, 7);
431  break;
432  }
433  if (symbol + repeat > alphabet_size) {
435  "invalid symbol %d + repeat %d > alphabet size %d\n",
436  symbol, repeat, alphabet_size);
437  ret = AVERROR_INVALIDDATA;
438  goto finish;
439  }
440  while (repeat-- > 0)
441  code_lengths[symbol++] = length;
442  }
443  }
444 
445  ret = huff_reader_build_canonical(hc, code_lengths, alphabet_size);
446 
447 finish:
448  ff_free_vlc(&code_len_hc.vlc);
449  av_free(code_lengths);
450  return ret;
451 }
452 
453 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
454  int w, int h);
455 
456 #define PARSE_BLOCK_SIZE(w, h) do { \
457  block_bits = get_bits(&s->gb, 3) + 2; \
458  blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
459  blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
460 } while (0)
461 
463 {
464  ImageContext *img;
465  int ret, block_bits, width, blocks_w, blocks_h, x, y, max;
466 
467  width = s->width;
468  if (s->reduced_width > 0)
469  width = s->reduced_width;
470 
471  PARSE_BLOCK_SIZE(width, s->height);
472 
473  ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h);
474  if (ret < 0)
475  return ret;
476 
477  img = &s->image[IMAGE_ROLE_ENTROPY];
478  img->size_reduction = block_bits;
479 
480  /* the number of huffman groups is determined by the maximum group number
481  * coded in the entropy image */
482  max = 0;
483  for (y = 0; y < img->frame->height; y++) {
484  for (x = 0; x < img->frame->width; x++) {
485  int p0 = GET_PIXEL_COMP(img->frame, x, y, 1);
486  int p1 = GET_PIXEL_COMP(img->frame, x, y, 2);
487  int p = p0 << 8 | p1;
488  max = FFMAX(max, p);
489  }
490  }
491  s->nb_huffman_groups = max + 1;
492 
493  return 0;
494 }
495 
497 {
498  int block_bits, blocks_w, blocks_h, ret;
499 
500  PARSE_BLOCK_SIZE(s->width, s->height);
501 
503  blocks_h);
504  if (ret < 0)
505  return ret;
506 
507  s->image[IMAGE_ROLE_PREDICTOR].size_reduction = block_bits;
508 
509  return 0;
510 }
511 
513 {
514  int block_bits, blocks_w, blocks_h, ret;
515 
516  PARSE_BLOCK_SIZE(s->width, s->height);
517 
519  blocks_h);
520  if (ret < 0)
521  return ret;
522 
524 
525  return 0;
526 }
527 
529 {
530  ImageContext *img;
531  int width_bits, index_size, ret, x;
532  uint8_t *ct;
533 
534  index_size = get_bits(&s->gb, 8) + 1;
535 
536  if (index_size <= 2)
537  width_bits = 3;
538  else if (index_size <= 4)
539  width_bits = 2;
540  else if (index_size <= 16)
541  width_bits = 1;
542  else
543  width_bits = 0;
544 
546  index_size, 1);
547  if (ret < 0)
548  return ret;
549 
550  img = &s->image[IMAGE_ROLE_COLOR_INDEXING];
551  img->size_reduction = width_bits;
552  if (width_bits > 0)
553  s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits;
554 
555  /* color index values are delta-coded */
556  ct = img->frame->data[0] + 4;
557  for (x = 4; x < img->frame->width * 4; x++, ct++)
558  ct[0] += ct[-4];
559 
560  return 0;
561 }
562 
564  int x, int y)
565 {
567  int group = 0;
568 
569  if (gimg->size_reduction > 0) {
570  int group_x = x >> gimg->size_reduction;
571  int group_y = y >> gimg->size_reduction;
572  int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1);
573  int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2);
574  group = g0 << 8 | g1;
575  }
576 
577  return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE];
578 }
579 
581 {
582  uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits);
583  img->color_cache[cache_idx] = c;
584 }
585 
587  int w, int h)
588 {
589  ImageContext *img;
590  HuffReader *hg;
591  int i, j, ret, x, y, width;
592 
593  img = &s->image[role];
594  img->role = role;
595 
596  if (!img->frame) {
597  img->frame = av_frame_alloc();
598  if (!img->frame)
599  return AVERROR(ENOMEM);
600  }
601 
602  img->frame->format = AV_PIX_FMT_ARGB;
603  img->frame->width = w;
604  img->frame->height = h;
605 
606  if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) {
607  ThreadFrame pt = { .f = img->frame };
608  ret = ff_thread_get_buffer(s->avctx, &pt, 0);
609  } else
610  ret = av_frame_get_buffer(img->frame, 1);
611  if (ret < 0)
612  return ret;
613 
614  if (get_bits1(&s->gb)) {
615  img->color_cache_bits = get_bits(&s->gb, 4);
616  if (img->color_cache_bits < 1 || img->color_cache_bits > 11) {
617  av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n",
618  img->color_cache_bits);
619  return AVERROR_INVALIDDATA;
620  }
622  sizeof(*img->color_cache));
623  if (!img->color_cache)
624  return AVERROR(ENOMEM);
625  } else {
626  img->color_cache_bits = 0;
627  }
628 
629  img->nb_huffman_groups = 1;
630  if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) {
631  ret = decode_entropy_image(s);
632  if (ret < 0)
633  return ret;
635  }
638  sizeof(*img->huffman_groups));
639  if (!img->huffman_groups)
640  return AVERROR(ENOMEM);
641 
642  for (i = 0; i < img->nb_huffman_groups; i++) {
644  for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) {
645  int alphabet_size = alphabet_sizes[j];
646  if (!j && img->color_cache_bits > 0)
647  alphabet_size += 1 << img->color_cache_bits;
648 
649  if (get_bits1(&s->gb)) {
650  read_huffman_code_simple(s, &hg[j]);
651  } else {
652  ret = read_huffman_code_normal(s, &hg[j], alphabet_size);
653  if (ret < 0)
654  return ret;
655  }
656  }
657  }
658 
659  width = img->frame->width;
660  if (role == IMAGE_ROLE_ARGB && s->reduced_width > 0)
661  width = s->reduced_width;
662 
663  x = 0; y = 0;
664  while (y < img->frame->height) {
665  int v;
666 
667  hg = get_huffman_group(s, img, x, y);
669  if (v < NUM_LITERAL_CODES) {
670  /* literal pixel values */
671  uint8_t *p = GET_PIXEL(img->frame, x, y);
672  p[2] = v;
673  p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb);
674  p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb);
675  p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb);
676  if (img->color_cache_bits)
677  color_cache_put(img, AV_RB32(p));
678  x++;
679  if (x == width) {
680  x = 0;
681  y++;
682  }
683  } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
684  /* LZ77 backwards mapping */
685  int prefix_code, length, distance, ref_x, ref_y;
686 
687  /* parse length and distance */
688  prefix_code = v - NUM_LITERAL_CODES;
689  if (prefix_code < 4) {
690  length = prefix_code + 1;
691  } else {
692  int extra_bits = (prefix_code - 2) >> 1;
693  int offset = 2 + (prefix_code & 1) << extra_bits;
694  length = offset + get_bits(&s->gb, extra_bits) + 1;
695  }
696  prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb);
697  if (prefix_code < 4) {
698  distance = prefix_code + 1;
699  } else {
700  int extra_bits = prefix_code - 2 >> 1;
701  int offset = 2 + (prefix_code & 1) << extra_bits;
702  distance = offset + get_bits(&s->gb, extra_bits) + 1;
703  }
704 
705  /* find reference location */
706  if (distance <= NUM_SHORT_DISTANCES) {
707  int xi = lz77_distance_offsets[distance - 1][0];
708  int yi = lz77_distance_offsets[distance - 1][1];
709  distance = FFMAX(1, xi + yi * width);
710  } else {
711  distance -= NUM_SHORT_DISTANCES;
712  }
713  ref_x = x;
714  ref_y = y;
715  if (distance <= x) {
716  ref_x -= distance;
717  distance = 0;
718  } else {
719  ref_x = 0;
720  distance -= x;
721  }
722  while (distance >= width) {
723  ref_y--;
724  distance -= width;
725  }
726  if (distance > 0) {
727  ref_x = width - distance;
728  ref_y--;
729  }
730  ref_x = FFMAX(0, ref_x);
731  ref_y = FFMAX(0, ref_y);
732 
733  /* copy pixels
734  * source and dest regions can overlap and wrap lines, so just
735  * copy per-pixel */
736  for (i = 0; i < length; i++) {
737  uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y);
738  uint8_t *p = GET_PIXEL(img->frame, x, y);
739 
740  AV_COPY32(p, p_ref);
741  if (img->color_cache_bits)
742  color_cache_put(img, AV_RB32(p));
743  x++;
744  ref_x++;
745  if (x == width) {
746  x = 0;
747  y++;
748  }
749  if (ref_x == width) {
750  ref_x = 0;
751  ref_y++;
752  }
753  if (y == img->frame->height || ref_y == img->frame->height)
754  break;
755  }
756  } else {
757  /* read from color cache */
758  uint8_t *p = GET_PIXEL(img->frame, x, y);
759  int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES);
760 
761  if (!img->color_cache_bits) {
762  av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n");
763  return AVERROR_INVALIDDATA;
764  }
765  if (cache_idx >= 1 << img->color_cache_bits) {
767  "color cache index out-of-bounds\n");
768  return AVERROR_INVALIDDATA;
769  }
770  AV_WB32(p, img->color_cache[cache_idx]);
771  x++;
772  if (x == width) {
773  x = 0;
774  y++;
775  }
776  }
777  }
778 
779  return 0;
780 }
781 
782 /* PRED_MODE_BLACK */
783 static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
784  const uint8_t *p_t, const uint8_t *p_tr)
785 {
786  AV_WB32(p, 0xFF000000);
787 }
788 
789 /* PRED_MODE_L */
790 static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
791  const uint8_t *p_t, const uint8_t *p_tr)
792 {
793  AV_COPY32(p, p_l);
794 }
795 
796 /* PRED_MODE_T */
797 static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
798  const uint8_t *p_t, const uint8_t *p_tr)
799 {
800  AV_COPY32(p, p_t);
801 }
802 
803 /* PRED_MODE_TR */
804 static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
805  const uint8_t *p_t, const uint8_t *p_tr)
806 {
807  AV_COPY32(p, p_tr);
808 }
809 
810 /* PRED_MODE_TL */
811 static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
812  const uint8_t *p_t, const uint8_t *p_tr)
813 {
814  AV_COPY32(p, p_tl);
815 }
816 
817 /* PRED_MODE_AVG_T_AVG_L_TR */
818 static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
819  const uint8_t *p_t, const uint8_t *p_tr)
820 {
821  p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
822  p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
823  p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
824  p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
825 }
826 
827 /* PRED_MODE_AVG_L_TL */
828 static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
829  const uint8_t *p_t, const uint8_t *p_tr)
830 {
831  p[0] = p_l[0] + p_tl[0] >> 1;
832  p[1] = p_l[1] + p_tl[1] >> 1;
833  p[2] = p_l[2] + p_tl[2] >> 1;
834  p[3] = p_l[3] + p_tl[3] >> 1;
835 }
836 
837 /* PRED_MODE_AVG_L_T */
838 static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
839  const uint8_t *p_t, const uint8_t *p_tr)
840 {
841  p[0] = p_l[0] + p_t[0] >> 1;
842  p[1] = p_l[1] + p_t[1] >> 1;
843  p[2] = p_l[2] + p_t[2] >> 1;
844  p[3] = p_l[3] + p_t[3] >> 1;
845 }
846 
847 /* PRED_MODE_AVG_TL_T */
848 static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
849  const uint8_t *p_t, const uint8_t *p_tr)
850 {
851  p[0] = p_tl[0] + p_t[0] >> 1;
852  p[1] = p_tl[1] + p_t[1] >> 1;
853  p[2] = p_tl[2] + p_t[2] >> 1;
854  p[3] = p_tl[3] + p_t[3] >> 1;
855 }
856 
857 /* PRED_MODE_AVG_T_TR */
858 static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
859  const uint8_t *p_t, const uint8_t *p_tr)
860 {
861  p[0] = p_t[0] + p_tr[0] >> 1;
862  p[1] = p_t[1] + p_tr[1] >> 1;
863  p[2] = p_t[2] + p_tr[2] >> 1;
864  p[3] = p_t[3] + p_tr[3] >> 1;
865 }
866 
867 /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */
868 static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
869  const uint8_t *p_t, const uint8_t *p_tr)
870 {
871  p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
872  p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
873  p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
874  p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
875 }
876 
877 /* PRED_MODE_SELECT */
878 static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
879  const uint8_t *p_t, const uint8_t *p_tr)
880 {
881  int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) +
882  (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) +
883  (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) +
884  (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3]));
885  if (diff <= 0)
886  AV_COPY32(p, p_t);
887  else
888  AV_COPY32(p, p_l);
889 }
890 
891 /* PRED_MODE_ADD_SUBTRACT_FULL */
892 static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
893  const uint8_t *p_t, const uint8_t *p_tr)
894 {
895  p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
896  p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
897  p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
898  p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
899 }
900 
902 {
903  int d = a + b >> 1;
904  return av_clip_uint8(d + (d - c) / 2);
905 }
906 
907 /* PRED_MODE_ADD_SUBTRACT_HALF */
908 static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
909  const uint8_t *p_t, const uint8_t *p_tr)
910 {
911  p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]);
912  p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]);
913  p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]);
914  p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]);
915 }
916 
917 typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l,
918  const uint8_t *p_tl, const uint8_t *p_t,
919  const uint8_t *p_tr);
920 
921 static const inv_predict_func inverse_predict[14] = {
926 };
927 
928 static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
929 {
930  uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
931  uint8_t p[4];
932 
933  dec = GET_PIXEL(frame, x, y);
934  p_l = GET_PIXEL(frame, x - 1, y);
935  p_tl = GET_PIXEL(frame, x - 1, y - 1);
936  p_t = GET_PIXEL(frame, x, y - 1);
937  if (x == frame->width - 1)
938  p_tr = GET_PIXEL(frame, 0, y);
939  else
940  p_tr = GET_PIXEL(frame, x + 1, y - 1);
941 
942  inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
943 
944  dec[0] += p[0];
945  dec[1] += p[1];
946  dec[2] += p[2];
947  dec[3] += p[3];
948 }
949 
951 {
954  int x, y;
955 
956  for (y = 0; y < img->frame->height; y++) {
957  for (x = 0; x < img->frame->width; x++) {
958  int tx = x >> pimg->size_reduction;
959  int ty = y >> pimg->size_reduction;
960  enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2);
961 
962  if (x == 0) {
963  if (y == 0)
964  m = PRED_MODE_BLACK;
965  else
966  m = PRED_MODE_T;
967  } else if (y == 0)
968  m = PRED_MODE_L;
969 
970  if (m > 13) {
972  "invalid predictor mode: %d\n", m);
973  return AVERROR_INVALIDDATA;
974  }
975  inverse_prediction(img->frame, m, x, y);
976  }
977  }
978  return 0;
979 }
980 
982  uint8_t color)
983 {
984  return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5;
985 }
986 
988 {
989  ImageContext *img, *cimg;
990  int x, y, cx, cy;
991  uint8_t *p, *cp;
992 
993  img = &s->image[IMAGE_ROLE_ARGB];
994  cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM];
995 
996  for (y = 0; y < img->frame->height; y++) {
997  for (x = 0; x < img->frame->width; x++) {
998  cx = x >> cimg->size_reduction;
999  cy = y >> cimg->size_reduction;
1000  cp = GET_PIXEL(cimg->frame, cx, cy);
1001  p = GET_PIXEL(img->frame, x, y);
1002 
1003  p[1] += color_transform_delta(cp[3], p[2]);
1004  p[3] += color_transform_delta(cp[2], p[2]) +
1005  color_transform_delta(cp[1], p[1]);
1006  }
1007  }
1008  return 0;
1009 }
1010 
1012 {
1013  int x, y;
1015 
1016  for (y = 0; y < img->frame->height; y++) {
1017  for (x = 0; x < img->frame->width; x++) {
1018  uint8_t *p = GET_PIXEL(img->frame, x, y);
1019  p[1] += p[2];
1020  p[3] += p[2];
1021  }
1022  }
1023  return 0;
1024 }
1025 
1027 {
1028  ImageContext *img;
1029  ImageContext *pal;
1030  int i, x, y;
1031  uint8_t *p, *pi;
1032 
1033  img = &s->image[IMAGE_ROLE_ARGB];
1034  pal = &s->image[IMAGE_ROLE_COLOR_INDEXING];
1035 
1036  if (pal->size_reduction > 0) {
1037  GetBitContext gb_g;
1038  uint8_t *line;
1039  int pixel_bits = 8 >> pal->size_reduction;
1040 
1041  line = av_malloc(img->frame->linesize[0]);
1042  if (!line)
1043  return AVERROR(ENOMEM);
1044 
1045  for (y = 0; y < img->frame->height; y++) {
1046  p = GET_PIXEL(img->frame, 0, y);
1047  memcpy(line, p, img->frame->linesize[0]);
1048  init_get_bits(&gb_g, line, img->frame->linesize[0] * 8);
1049  skip_bits(&gb_g, 16);
1050  i = 0;
1051  for (x = 0; x < img->frame->width; x++) {
1052  p = GET_PIXEL(img->frame, x, y);
1053  p[2] = get_bits(&gb_g, pixel_bits);
1054  i++;
1055  if (i == 1 << pal->size_reduction) {
1056  skip_bits(&gb_g, 24);
1057  i = 0;
1058  }
1059  }
1060  }
1061  av_free(line);
1062  }
1063 
1064  for (y = 0; y < img->frame->height; y++) {
1065  for (x = 0; x < img->frame->width; x++) {
1066  p = GET_PIXEL(img->frame, x, y);
1067  i = p[2];
1068  if (i >= pal->frame->width) {
1069  av_log(s->avctx, AV_LOG_ERROR, "invalid palette index %d\n", i);
1070  return AVERROR_INVALIDDATA;
1071  }
1072  pi = GET_PIXEL(pal->frame, i, 0);
1073  AV_COPY32(p, pi);
1074  }
1075  }
1076 
1077  return 0;
1078 }
1079 
1081  int *got_frame, uint8_t *data_start,
1082  unsigned int data_size, int is_alpha_chunk)
1083 {
1084  WebPContext *s = avctx->priv_data;
1085  int w, h, ret, i;
1086 
1087  if (!is_alpha_chunk) {
1088  s->lossless = 1;
1089  avctx->pix_fmt = AV_PIX_FMT_ARGB;
1090  }
1091 
1092  ret = init_get_bits(&s->gb, data_start, data_size * 8);
1093  if (ret < 0)
1094  return ret;
1095 
1096  if (!is_alpha_chunk) {
1097  if (get_bits(&s->gb, 8) != 0x2F) {
1098  av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n");
1099  return AVERROR_INVALIDDATA;
1100  }
1101 
1102  w = get_bits(&s->gb, 14) + 1;
1103  h = get_bits(&s->gb, 14) + 1;
1104  if (s->width && s->width != w) {
1105  av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n",
1106  s->width, w);
1107  }
1108  s->width = w;
1109  if (s->height && s->height != h) {
1110  av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n",
1111  s->width, w);
1112  }
1113  s->height = h;
1114 
1115  ret = ff_set_dimensions(avctx, s->width, s->height);
1116  if (ret < 0)
1117  return ret;
1118 
1119  s->has_alpha = get_bits1(&s->gb);
1120 
1121  if (get_bits(&s->gb, 3) != 0x0) {
1122  av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n");
1123  return AVERROR_INVALIDDATA;
1124  }
1125  } else {
1126  if (!s->width || !s->height)
1127  return AVERROR_BUG;
1128  w = s->width;
1129  h = s->height;
1130  }
1131 
1132  /* parse transformations */
1133  s->nb_transforms = 0;
1134  s->reduced_width = 0;
1135  while (get_bits1(&s->gb)) {
1136  enum TransformType transform = get_bits(&s->gb, 2);
1137  s->transforms[s->nb_transforms++] = transform;
1138  switch (transform) {
1139  case PREDICTOR_TRANSFORM:
1140  ret = parse_transform_predictor(s);
1141  break;
1142  case COLOR_TRANSFORM:
1143  ret = parse_transform_color(s);
1144  break;
1147  break;
1148  }
1149  if (ret < 0)
1150  goto free_and_return;
1151  }
1152 
1153  /* decode primary image */
1154  s->image[IMAGE_ROLE_ARGB].frame = p;
1155  if (is_alpha_chunk)
1158  if (ret < 0)
1159  goto free_and_return;
1160 
1161  /* apply transformations */
1162  for (i = s->nb_transforms - 1; i >= 0; i--) {
1163  switch (s->transforms[i]) {
1164  case PREDICTOR_TRANSFORM:
1165  ret = apply_predictor_transform(s);
1166  break;
1167  case COLOR_TRANSFORM:
1168  ret = apply_color_transform(s);
1169  break;
1170  case SUBTRACT_GREEN:
1172  break;
1175  break;
1176  }
1177  if (ret < 0)
1178  goto free_and_return;
1179  }
1180 
1181  *got_frame = 1;
1183  p->key_frame = 1;
1184  ret = data_size;
1185 
1186 free_and_return:
1187  for (i = 0; i < IMAGE_ROLE_NB; i++)
1188  image_ctx_free(&s->image[i]);
1189 
1190  return ret;
1191 }
1192 
1194 {
1195  int x, y, ls;
1196  uint8_t *dec;
1197 
1198  ls = frame->linesize[3];
1199 
1200  /* filter first row using horizontal filter */
1201  dec = frame->data[3] + 1;
1202  for (x = 1; x < frame->width; x++, dec++)
1203  *dec += *(dec - 1);
1204 
1205  /* filter first column using vertical filter */
1206  dec = frame->data[3] + ls;
1207  for (y = 1; y < frame->height; y++, dec += ls)
1208  *dec += *(dec - ls);
1209 
1210  /* filter the rest using the specified filter */
1211  switch (m) {
1213  for (y = 1; y < frame->height; y++) {
1214  dec = frame->data[3] + y * ls + 1;
1215  for (x = 1; x < frame->width; x++, dec++)
1216  *dec += *(dec - 1);
1217  }
1218  break;
1219  case ALPHA_FILTER_VERTICAL:
1220  for (y = 1; y < frame->height; y++) {
1221  dec = frame->data[3] + y * ls + 1;
1222  for (x = 1; x < frame->width; x++, dec++)
1223  *dec += *(dec - ls);
1224  }
1225  break;
1226  case ALPHA_FILTER_GRADIENT:
1227  for (y = 1; y < frame->height; y++) {
1228  dec = frame->data[3] + y * ls + 1;
1229  for (x = 1; x < frame->width; x++, dec++)
1230  dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1231  }
1232  break;
1233  }
1234 }
1235 
1237  uint8_t *data_start,
1238  unsigned int data_size)
1239 {
1240  WebPContext *s = avctx->priv_data;
1241  int x, y, ret;
1242 
1244  GetByteContext gb;
1245 
1246  bytestream2_init(&gb, data_start, data_size);
1247  for (y = 0; y < s->height; y++)
1248  bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y,
1249  s->width);
1250  } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) {
1251  uint8_t *ap, *pp;
1252  int alpha_got_frame = 0;
1253 
1254  s->alpha_frame = av_frame_alloc();
1255  if (!s->alpha_frame)
1256  return AVERROR(ENOMEM);
1257 
1258  ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame,
1259  data_start, data_size, 1);
1260  if (ret < 0) {
1262  return ret;
1263  }
1264  if (!alpha_got_frame) {
1266  return AVERROR_INVALIDDATA;
1267  }
1268 
1269  /* copy green component of alpha image to alpha plane of primary image */
1270  for (y = 0; y < s->height; y++) {
1271  ap = GET_PIXEL(s->alpha_frame, 0, y) + 2;
1272  pp = p->data[3] + p->linesize[3] * y;
1273  for (x = 0; x < s->width; x++) {
1274  *pp = *ap;
1275  pp++;
1276  ap += 4;
1277  }
1278  }
1280  }
1281 
1282  /* apply alpha filtering */
1283  if (s->alpha_filter)
1285 
1286  return 0;
1287 }
1288 
1290  int *got_frame, uint8_t *data_start,
1291  unsigned int data_size)
1292 {
1293  WebPContext *s = avctx->priv_data;
1294  AVPacket pkt;
1295  int ret;
1296 
1297  if (!s->initialized) {
1298  ff_vp8_decode_init(avctx);
1299  s->initialized = 1;
1300  if (s->has_alpha)
1301  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
1302  }
1303  s->lossless = 0;
1304 
1305  if (data_size > INT_MAX) {
1306  av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n");
1307  return AVERROR_PATCHWELCOME;
1308  }
1309 
1310  av_init_packet(&pkt);
1311  pkt.data = data_start;
1312  pkt.size = data_size;
1313 
1314  ret = ff_vp8_decode_frame(avctx, p, got_frame, &pkt);
1315  if (s->has_alpha) {
1316  ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data,
1317  s->alpha_data_size);
1318  if (ret < 0)
1319  return ret;
1320  }
1321  return ret;
1322 }
1323 
1324 static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1325  AVPacket *avpkt)
1326 {
1327  AVFrame * const p = data;
1328  WebPContext *s = avctx->priv_data;
1329  GetByteContext gb;
1330  int ret;
1331  uint32_t chunk_type, chunk_size;
1332  int vp8x_flags = 0;
1333 
1334  s->avctx = avctx;
1335  s->width = 0;
1336  s->height = 0;
1337  *got_frame = 0;
1338  s->has_alpha = 0;
1339  s->has_exif = 0;
1340  bytestream2_init(&gb, avpkt->data, avpkt->size);
1341 
1342  if (bytestream2_get_bytes_left(&gb) < 12)
1343  return AVERROR_INVALIDDATA;
1344 
1345  if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
1346  av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
1347  return AVERROR_INVALIDDATA;
1348  }
1349 
1350  chunk_size = bytestream2_get_le32(&gb);
1351  if (bytestream2_get_bytes_left(&gb) < chunk_size)
1352  return AVERROR_INVALIDDATA;
1353 
1354  if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) {
1355  av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n");
1356  return AVERROR_INVALIDDATA;
1357  }
1358 
1360  while (bytestream2_get_bytes_left(&gb) > 0) {
1361  char chunk_str[5] = { 0 };
1362 
1363  chunk_type = bytestream2_get_le32(&gb);
1364  chunk_size = bytestream2_get_le32(&gb);
1365  if (chunk_size == UINT32_MAX)
1366  return AVERROR_INVALIDDATA;
1367  chunk_size += chunk_size & 1;
1368 
1369  if (bytestream2_get_bytes_left(&gb) < chunk_size)
1370  return AVERROR_INVALIDDATA;
1371 
1372  switch (chunk_type) {
1373  case MKTAG('V', 'P', '8', ' '):
1374  if (!*got_frame) {
1375  ret = vp8_lossy_decode_frame(avctx, p, got_frame,
1376  avpkt->data + bytestream2_tell(&gb),
1377  chunk_size);
1378  if (ret < 0)
1379  return ret;
1380  }
1381  bytestream2_skip(&gb, chunk_size);
1382  break;
1383  case MKTAG('V', 'P', '8', 'L'):
1384  if (!*got_frame) {
1385  ret = vp8_lossless_decode_frame(avctx, p, got_frame,
1386  avpkt->data + bytestream2_tell(&gb),
1387  chunk_size, 0);
1388  if (ret < 0)
1389  return ret;
1390  }
1391  bytestream2_skip(&gb, chunk_size);
1392  break;
1393  case MKTAG('V', 'P', '8', 'X'):
1394  vp8x_flags = bytestream2_get_byte(&gb);
1395  bytestream2_skip(&gb, 3);
1396  s->width = bytestream2_get_le24(&gb) + 1;
1397  s->height = bytestream2_get_le24(&gb) + 1;
1398  ret = av_image_check_size(s->width, s->height, 0, avctx);
1399  if (ret < 0)
1400  return ret;
1401  break;
1402  case MKTAG('A', 'L', 'P', 'H'): {
1403  int alpha_header, filter_m, compression;
1404 
1405  if (!(vp8x_flags & VP8X_FLAG_ALPHA)) {
1406  av_log(avctx, AV_LOG_WARNING,
1407  "ALPHA chunk present, but alpha bit not set in the "
1408  "VP8X header\n");
1409  }
1410  if (chunk_size == 0) {
1411  av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n");
1412  return AVERROR_INVALIDDATA;
1413  }
1414  alpha_header = bytestream2_get_byte(&gb);
1415  s->alpha_data = avpkt->data + bytestream2_tell(&gb);
1416  s->alpha_data_size = chunk_size - 1;
1418 
1419  filter_m = (alpha_header >> 2) & 0x03;
1420  compression = alpha_header & 0x03;
1421 
1422  if (compression > ALPHA_COMPRESSION_VP8L) {
1423  av_log(avctx, AV_LOG_VERBOSE,
1424  "skipping unsupported ALPHA chunk\n");
1425  } else {
1426  s->has_alpha = 1;
1427  s->alpha_compression = compression;
1428  s->alpha_filter = filter_m;
1429  }
1430 
1431  break;
1432  }
1433  case MKTAG('E', 'X', 'I', 'F'): {
1434  int le, ifd_offset, exif_offset = bytestream2_tell(&gb);
1435  GetByteContext exif_gb;
1436 
1437  if (s->has_exif) {
1438  av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n");
1439  goto exif_end;
1440  }
1441  if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA))
1442  av_log(avctx, AV_LOG_WARNING,
1443  "EXIF chunk present, but Exif bit not set in the "
1444  "VP8X header\n");
1445 
1446  s->has_exif = 1;
1447  bytestream2_init(&exif_gb, avpkt->data + exif_offset,
1448  avpkt->size - exif_offset);
1449  if (ff_tdecode_header(&exif_gb, &le, &ifd_offset) < 0) {
1450  av_log(avctx, AV_LOG_ERROR, "invalid TIFF header "
1451  "in Exif data\n");
1452  goto exif_end;
1453  }
1454 
1455  bytestream2_seek(&exif_gb, ifd_offset, SEEK_SET);
1456  if (avpriv_exif_decode_ifd(avctx, &exif_gb, le, 0, &s->exif_metadata) < 0) {
1457  av_log(avctx, AV_LOG_ERROR, "error decoding Exif data\n");
1458  goto exif_end;
1459  }
1460 
1462 
1463 exif_end:
1465  bytestream2_skip(&gb, chunk_size);
1466  break;
1467  }
1468  case MKTAG('I', 'C', 'C', 'P'):
1469  case MKTAG('A', 'N', 'I', 'M'):
1470  case MKTAG('A', 'N', 'M', 'F'):
1471  case MKTAG('X', 'M', 'P', ' '):
1472  AV_WL32(chunk_str, chunk_type);
1473  av_log(avctx, AV_LOG_VERBOSE, "skipping unsupported chunk: %s\n",
1474  chunk_str);
1475  bytestream2_skip(&gb, chunk_size);
1476  break;
1477  default:
1478  AV_WL32(chunk_str, chunk_type);
1479  av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
1480  chunk_str);
1481  bytestream2_skip(&gb, chunk_size);
1482  break;
1483  }
1484  }
1485 
1486  if (!*got_frame) {
1487  av_log(avctx, AV_LOG_ERROR, "image data not found\n");
1488  return AVERROR_INVALIDDATA;
1489  }
1490 
1491  return avpkt->size;
1492 }
1493 
1495 {
1496  WebPContext *s = avctx->priv_data;
1497 
1498  if (s->initialized)
1499  return ff_vp8_decode_free(avctx);
1500 
1501  return 0;
1502 }
1503 
1505  .name = "webp",
1506  .long_name = NULL_IF_CONFIG_SMALL("WebP image"),
1507  .type = AVMEDIA_TYPE_VIDEO,
1508  .id = AV_CODEC_ID_WEBP,
1509  .priv_data_size = sizeof(WebPContext),
1512  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
1513 };