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