FFmpeg  4.3.7
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  * ICC profile
37  *
38  * Unimplemented:
39  * - Animation
40  * - XMP metadata
41  */
42 
43 #include "libavutil/imgutils.h"
44 
45 #define BITSTREAM_READER_LE
46 #include "avcodec.h"
47 #include "bytestream.h"
48 #include "exif.h"
49 #include "get_bits.h"
50 #include "internal.h"
51 #include "thread.h"
52 #include "vp8.h"
53 
54 #define VP8X_FLAG_ANIMATION 0x02
55 #define VP8X_FLAG_XMP_METADATA 0x04
56 #define VP8X_FLAG_EXIF_METADATA 0x08
57 #define VP8X_FLAG_ALPHA 0x10
58 #define VP8X_FLAG_ICC 0x20
59 
60 #define MAX_PALETTE_SIZE 256
61 #define MAX_CACHE_BITS 11
62 #define NUM_CODE_LENGTH_CODES 19
63 #define HUFFMAN_CODES_PER_META_CODE 5
64 #define NUM_LITERAL_CODES 256
65 #define NUM_LENGTH_CODES 24
66 #define NUM_DISTANCE_CODES 40
67 #define NUM_SHORT_DISTANCES 120
68 #define MAX_HUFFMAN_CODE_LENGTH 15
69 
70 static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE] = {
74 };
75 
77  17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
78 };
79 
80 static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2] = {
81  { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
82  { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
83  { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
84  { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
85  { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
86  { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
87  { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
88  { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
89  { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
90  { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
91  { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
92  { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
93  { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
94  { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
95  { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
96 };
97 
101 };
102 
108 };
109 
115 };
116 
132 };
133 
140 };
141 
142 /* The structure of WebP lossless is an optional series of transformation data,
143  * followed by the primary image. The primary image also optionally contains
144  * an entropy group mapping if there are multiple entropy groups. There is a
145  * basic image type called an "entropy coded image" that is used for all of
146  * these. The type of each entropy coded image is referred to by the
147  * specification as its role. */
148 enum ImageRole {
149  /* Primary Image: Stores the actual pixels of the image. */
151 
152  /* Entropy Image: Defines which Huffman group to use for different areas of
153  * the primary image. */
155 
156  /* Predictors: Defines which predictor type to use for different areas of
157  * the primary image. */
159 
160  /* Color Transform Data: Defines the color transformation for different
161  * areas of the primary image. */
163 
164  /* Color Index: Stored as an image of height == 1. */
166 
168 };
169 
170 typedef struct HuffReader {
171  VLC vlc; /* Huffman decoder context */
172  int simple; /* whether to use simple mode */
173  int nb_symbols; /* number of coded symbols */
174  uint16_t simple_symbols[2]; /* symbols for simple mode */
175 } HuffReader;
176 
177 typedef struct ImageContext {
178  enum ImageRole role; /* role of this image */
179  AVFrame *frame; /* AVFrame for data */
180  int color_cache_bits; /* color cache size, log2 */
181  uint32_t *color_cache; /* color cache data */
182  int nb_huffman_groups; /* number of huffman groups */
183  HuffReader *huffman_groups; /* reader for each huffman group */
184  int size_reduction; /* relative size compared to primary image, log2 */
186 } ImageContext;
187 
188 typedef struct WebPContext {
189  VP8Context v; /* VP8 Context used for lossy decoding */
190  GetBitContext gb; /* bitstream reader for main image chunk */
191  AVFrame *alpha_frame; /* AVFrame for alpha data decompressed from VP8L */
192  AVCodecContext *avctx; /* parent AVCodecContext */
193  int initialized; /* set once the VP8 context is initialized */
194  int has_alpha; /* has a separate alpha chunk */
195  enum AlphaCompression alpha_compression; /* compression type for alpha chunk */
196  enum AlphaFilter alpha_filter; /* filtering method for alpha chunk */
197  uint8_t *alpha_data; /* alpha chunk data */
198  int alpha_data_size; /* alpha chunk data size */
199  int has_exif; /* set after an EXIF chunk has been processed */
200  int has_iccp; /* set after an ICCP chunk has been processed */
201  int width; /* image width */
202  int height; /* image height */
203  int lossless; /* indicates lossless or lossy */
204 
205  int nb_transforms; /* number of transforms */
206  enum TransformType transforms[4]; /* transformations used in the image, in order */
207  int reduced_width; /* reduced width for index image, if applicable */
208  int nb_huffman_groups; /* number of huffman groups in the primary image */
209  ImageContext image[IMAGE_ROLE_NB]; /* image context for each role */
210 } WebPContext;
211 
212 #define GET_PIXEL(frame, x, y) \
213  ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
214 
215 #define GET_PIXEL_COMP(frame, x, y, c) \
216  (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
217 
219 {
220  int i, j;
221 
222  av_free(img->color_cache);
223  if (img->role != IMAGE_ROLE_ARGB && !img->is_alpha_primary)
224  av_frame_free(&img->frame);
225  if (img->huffman_groups) {
226  for (i = 0; i < img->nb_huffman_groups; i++) {
227  for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++)
228  ff_free_vlc(&img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE + j].vlc);
229  }
230  av_free(img->huffman_groups);
231  }
232  memset(img, 0, sizeof(*img));
233 }
234 
235 
236 /* Differs from get_vlc2() in the following ways:
237  * - codes are bit-reversed
238  * - assumes 8-bit table to make reversal simpler
239  * - assumes max depth of 2 since the max code length for WebP is 15
240  */
242 {
243  int n, nb_bits;
244  unsigned int index;
245  int code;
246 
247  OPEN_READER(re, gb);
248  UPDATE_CACHE(re, gb);
249 
250  index = SHOW_UBITS(re, gb, 8);
251  index = ff_reverse[index];
252  code = table[index][0];
253  n = table[index][1];
254 
255  if (n < 0) {
256  LAST_SKIP_BITS(re, gb, 8);
257  UPDATE_CACHE(re, gb);
258 
259  nb_bits = -n;
260 
261  index = SHOW_UBITS(re, gb, nb_bits);
262  index = (ff_reverse[index] >> (8 - nb_bits)) + code;
263  code = table[index][0];
264  n = table[index][1];
265  }
266  SKIP_BITS(re, gb, n);
267 
268  CLOSE_READER(re, gb);
269 
270  return code;
271 }
272 
274 {
275  if (r->simple) {
276  if (r->nb_symbols == 1)
277  return r->simple_symbols[0];
278  else
279  return r->simple_symbols[get_bits1(gb)];
280  } else
281  return webp_get_vlc(gb, r->vlc.table);
282 }
283 
284 static int huff_reader_build_canonical(HuffReader *r, int *code_lengths,
285  int alphabet_size)
286 {
287  int len = 0, sym, code = 0, ret;
288  int max_code_length = 0;
289  uint16_t *codes;
290 
291  /* special-case 1 symbol since the vlc reader cannot handle it */
292  for (sym = 0; sym < alphabet_size; sym++) {
293  if (code_lengths[sym] > 0) {
294  len++;
295  code = sym;
296  if (len > 1)
297  break;
298  }
299  }
300  if (len == 1) {
301  r->nb_symbols = 1;
302  r->simple_symbols[0] = code;
303  r->simple = 1;
304  return 0;
305  }
306 
307  for (sym = 0; sym < alphabet_size; sym++)
308  max_code_length = FFMAX(max_code_length, code_lengths[sym]);
309 
310  if (max_code_length == 0 || max_code_length > MAX_HUFFMAN_CODE_LENGTH)
311  return AVERROR(EINVAL);
312 
313  codes = av_malloc_array(alphabet_size, sizeof(*codes));
314  if (!codes)
315  return AVERROR(ENOMEM);
316 
317  code = 0;
318  r->nb_symbols = 0;
319  for (len = 1; len <= max_code_length; len++) {
320  for (sym = 0; sym < alphabet_size; sym++) {
321  if (code_lengths[sym] != len)
322  continue;
323  codes[sym] = code++;
324  r->nb_symbols++;
325  }
326  code <<= 1;
327  }
328  if (!r->nb_symbols) {
329  av_free(codes);
330  return AVERROR_INVALIDDATA;
331  }
332 
333  ret = init_vlc(&r->vlc, 8, alphabet_size,
334  code_lengths, sizeof(*code_lengths), sizeof(*code_lengths),
335  codes, sizeof(*codes), sizeof(*codes), 0);
336  if (ret < 0) {
337  av_free(codes);
338  return ret;
339  }
340  r->simple = 0;
341 
342  av_free(codes);
343  return 0;
344 }
345 
347 {
348  hc->nb_symbols = get_bits1(&s->gb) + 1;
349 
350  if (get_bits1(&s->gb))
351  hc->simple_symbols[0] = get_bits(&s->gb, 8);
352  else
353  hc->simple_symbols[0] = get_bits1(&s->gb);
354 
355  if (hc->nb_symbols == 2)
356  hc->simple_symbols[1] = get_bits(&s->gb, 8);
357 
358  hc->simple = 1;
359 }
360 
362  int alphabet_size)
363 {
364  HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
365  int *code_lengths = NULL;
366  int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
367  int i, symbol, max_symbol, prev_code_len, ret;
368  int num_codes = 4 + get_bits(&s->gb, 4);
369 
370  if (num_codes > NUM_CODE_LENGTH_CODES)
371  return AVERROR_INVALIDDATA;
372 
373  for (i = 0; i < num_codes; i++)
374  code_length_code_lengths[code_length_code_order[i]] = get_bits(&s->gb, 3);
375 
376  ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths,
378  if (ret < 0)
379  goto finish;
380 
381  code_lengths = av_mallocz_array(alphabet_size, sizeof(*code_lengths));
382  if (!code_lengths) {
383  ret = AVERROR(ENOMEM);
384  goto finish;
385  }
386 
387  if (get_bits1(&s->gb)) {
388  int bits = 2 + 2 * get_bits(&s->gb, 3);
389  max_symbol = 2 + get_bits(&s->gb, bits);
390  if (max_symbol > alphabet_size) {
391  av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n",
392  max_symbol, alphabet_size);
393  ret = AVERROR_INVALIDDATA;
394  goto finish;
395  }
396  } else {
397  max_symbol = alphabet_size;
398  }
399 
400  prev_code_len = 8;
401  symbol = 0;
402  while (symbol < alphabet_size) {
403  int code_len;
404 
405  if (!max_symbol--)
406  break;
407  code_len = huff_reader_get_symbol(&code_len_hc, &s->gb);
408  if (code_len < 16) {
409  /* Code length code [0..15] indicates literal code lengths. */
410  code_lengths[symbol++] = code_len;
411  if (code_len)
412  prev_code_len = code_len;
413  } else {
414  int repeat = 0, length = 0;
415  switch (code_len) {
416  case 16:
417  /* Code 16 repeats the previous non-zero value [3..6] times,
418  * i.e., 3 + ReadBits(2) times. If code 16 is used before a
419  * non-zero value has been emitted, a value of 8 is repeated. */
420  repeat = 3 + get_bits(&s->gb, 2);
421  length = prev_code_len;
422  break;
423  case 17:
424  /* Code 17 emits a streak of zeros [3..10], i.e.,
425  * 3 + ReadBits(3) times. */
426  repeat = 3 + get_bits(&s->gb, 3);
427  break;
428  case 18:
429  /* Code 18 emits a streak of zeros of length [11..138], i.e.,
430  * 11 + ReadBits(7) times. */
431  repeat = 11 + get_bits(&s->gb, 7);
432  break;
433  }
434  if (symbol + repeat > alphabet_size) {
436  "invalid symbol %d + repeat %d > alphabet size %d\n",
437  symbol, repeat, alphabet_size);
438  ret = AVERROR_INVALIDDATA;
439  goto finish;
440  }
441  while (repeat-- > 0)
442  code_lengths[symbol++] = length;
443  }
444  }
445 
446  ret = huff_reader_build_canonical(hc, code_lengths, alphabet_size);
447 
448 finish:
449  ff_free_vlc(&code_len_hc.vlc);
450  av_free(code_lengths);
451  return ret;
452 }
453 
454 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
455  int w, int h);
456 
457 #define PARSE_BLOCK_SIZE(w, h) do { \
458  block_bits = get_bits(&s->gb, 3) + 2; \
459  blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
460  blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
461 } while (0)
462 
464 {
465  ImageContext *img;
466  int ret, block_bits, width, blocks_w, blocks_h, x, y, max;
467 
468  width = s->width;
469  if (s->reduced_width > 0)
470  width = s->reduced_width;
471 
472  PARSE_BLOCK_SIZE(width, s->height);
473 
474  ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h);
475  if (ret < 0)
476  return ret;
477 
478  img = &s->image[IMAGE_ROLE_ENTROPY];
479  img->size_reduction = block_bits;
480 
481  /* the number of huffman groups is determined by the maximum group number
482  * coded in the entropy image */
483  max = 0;
484  for (y = 0; y < img->frame->height; y++) {
485  for (x = 0; x < img->frame->width; x++) {
486  int p0 = GET_PIXEL_COMP(img->frame, x, y, 1);
487  int p1 = GET_PIXEL_COMP(img->frame, x, y, 2);
488  int p = p0 << 8 | p1;
489  max = FFMAX(max, p);
490  }
491  }
492  s->nb_huffman_groups = max + 1;
493 
494  return 0;
495 }
496 
498 {
499  int block_bits, blocks_w, blocks_h, ret;
500 
501  PARSE_BLOCK_SIZE(s->width, s->height);
502 
504  blocks_h);
505  if (ret < 0)
506  return ret;
507 
509 
510  return 0;
511 }
512 
514 {
515  int block_bits, blocks_w, blocks_h, ret;
516 
517  PARSE_BLOCK_SIZE(s->width, s->height);
518 
520  blocks_h);
521  if (ret < 0)
522  return ret;
523 
525 
526  return 0;
527 }
528 
530 {
531  ImageContext *img;
532  int width_bits, index_size, ret, x;
533  uint8_t *ct;
534 
535  index_size = get_bits(&s->gb, 8) + 1;
536 
537  if (index_size <= 2)
538  width_bits = 3;
539  else if (index_size <= 4)
540  width_bits = 2;
541  else if (index_size <= 16)
542  width_bits = 1;
543  else
544  width_bits = 0;
545 
547  index_size, 1);
548  if (ret < 0)
549  return ret;
550 
551  img = &s->image[IMAGE_ROLE_COLOR_INDEXING];
552  img->size_reduction = width_bits;
553  if (width_bits > 0)
554  s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits;
555 
556  /* color index values are delta-coded */
557  ct = img->frame->data[0] + 4;
558  for (x = 4; x < img->frame->width * 4; x++, ct++)
559  ct[0] += ct[-4];
560 
561  return 0;
562 }
563 
565  int x, int y)
566 {
568  int group = 0;
569 
570  if (gimg->size_reduction > 0) {
571  int group_x = x >> gimg->size_reduction;
572  int group_y = y >> gimg->size_reduction;
573  int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1);
574  int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2);
575  group = g0 << 8 | g1;
576  }
577 
578  return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE];
579 }
580 
582 {
583  uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits);
584  img->color_cache[cache_idx] = c;
585 }
586 
588  int w, int h)
589 {
590  ImageContext *img;
591  HuffReader *hg;
592  int i, j, ret, x, y, width;
593 
594  img = &s->image[role];
595  img->role = role;
596 
597  if (!img->frame) {
598  img->frame = av_frame_alloc();
599  if (!img->frame)
600  return AVERROR(ENOMEM);
601  }
602 
603  img->frame->format = AV_PIX_FMT_ARGB;
604  img->frame->width = w;
605  img->frame->height = h;
606 
607  if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) {
608  ThreadFrame pt = { .f = img->frame };
609  ret = ff_thread_get_buffer(s->avctx, &pt, 0);
610  } else
611  ret = av_frame_get_buffer(img->frame, 1);
612  if (ret < 0)
613  return ret;
614 
615  if (get_bits1(&s->gb)) {
616  img->color_cache_bits = get_bits(&s->gb, 4);
617  if (img->color_cache_bits < 1 || img->color_cache_bits > 11) {
618  av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n",
619  img->color_cache_bits);
620  return AVERROR_INVALIDDATA;
621  }
623  sizeof(*img->color_cache));
624  if (!img->color_cache)
625  return AVERROR(ENOMEM);
626  } else {
627  img->color_cache_bits = 0;
628  }
629 
630  img->nb_huffman_groups = 1;
631  if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) {
632  ret = decode_entropy_image(s);
633  if (ret < 0)
634  return ret;
636  }
639  sizeof(*img->huffman_groups));
640  if (!img->huffman_groups)
641  return AVERROR(ENOMEM);
642 
643  for (i = 0; i < img->nb_huffman_groups; i++) {
645  for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) {
646  int alphabet_size = alphabet_sizes[j];
647  if (!j && img->color_cache_bits > 0)
648  alphabet_size += 1 << img->color_cache_bits;
649 
650  if (get_bits1(&s->gb)) {
651  read_huffman_code_simple(s, &hg[j]);
652  } else {
653  ret = read_huffman_code_normal(s, &hg[j], alphabet_size);
654  if (ret < 0)
655  return ret;
656  }
657  }
658  }
659 
660  width = img->frame->width;
661  if (role == IMAGE_ROLE_ARGB && s->reduced_width > 0)
662  width = s->reduced_width;
663 
664  x = 0; y = 0;
665  while (y < img->frame->height) {
666  int v;
667 
668  if (get_bits_left(&s->gb) < 0)
669  return AVERROR_INVALIDDATA;
670 
671  hg = get_huffman_group(s, img, x, y);
673  if (v < NUM_LITERAL_CODES) {
674  /* literal pixel values */
675  uint8_t *p = GET_PIXEL(img->frame, x, y);
676  p[2] = v;
677  p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb);
678  p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb);
679  p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb);
680  if (img->color_cache_bits)
681  color_cache_put(img, AV_RB32(p));
682  x++;
683  if (x == width) {
684  x = 0;
685  y++;
686  }
687  } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
688  /* LZ77 backwards mapping */
689  int prefix_code, length, distance, ref_x, ref_y;
690 
691  /* parse length and distance */
692  prefix_code = v - NUM_LITERAL_CODES;
693  if (prefix_code < 4) {
694  length = prefix_code + 1;
695  } else {
696  int extra_bits = (prefix_code - 2) >> 1;
697  int offset = 2 + (prefix_code & 1) << extra_bits;
698  length = offset + get_bits(&s->gb, extra_bits) + 1;
699  }
700  prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb);
701  if (prefix_code > 39U) {
703  "distance prefix code too large: %d\n", prefix_code);
704  return AVERROR_INVALIDDATA;
705  }
706  if (prefix_code < 4) {
707  distance = prefix_code + 1;
708  } else {
709  int extra_bits = prefix_code - 2 >> 1;
710  int offset = 2 + (prefix_code & 1) << extra_bits;
711  distance = offset + get_bits(&s->gb, extra_bits) + 1;
712  }
713 
714  /* find reference location */
715  if (distance <= NUM_SHORT_DISTANCES) {
716  int xi = lz77_distance_offsets[distance - 1][0];
717  int yi = lz77_distance_offsets[distance - 1][1];
718  distance = FFMAX(1, xi + yi * width);
719  } else {
720  distance -= NUM_SHORT_DISTANCES;
721  }
722  ref_x = x;
723  ref_y = y;
724  if (distance <= x) {
725  ref_x -= distance;
726  distance = 0;
727  } else {
728  ref_x = 0;
729  distance -= x;
730  }
731  while (distance >= width) {
732  ref_y--;
733  distance -= width;
734  }
735  if (distance > 0) {
736  ref_x = width - distance;
737  ref_y--;
738  }
739  ref_x = FFMAX(0, ref_x);
740  ref_y = FFMAX(0, ref_y);
741 
742  /* copy pixels
743  * source and dest regions can overlap and wrap lines, so just
744  * copy per-pixel */
745  for (i = 0; i < length; i++) {
746  uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y);
747  uint8_t *p = GET_PIXEL(img->frame, x, y);
748 
749  AV_COPY32(p, p_ref);
750  if (img->color_cache_bits)
751  color_cache_put(img, AV_RB32(p));
752  x++;
753  ref_x++;
754  if (x == width) {
755  x = 0;
756  y++;
757  }
758  if (ref_x == width) {
759  ref_x = 0;
760  ref_y++;
761  }
762  if (y == img->frame->height || ref_y == img->frame->height)
763  break;
764  }
765  } else {
766  /* read from color cache */
767  uint8_t *p = GET_PIXEL(img->frame, x, y);
768  int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES);
769 
770  if (!img->color_cache_bits) {
771  av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n");
772  return AVERROR_INVALIDDATA;
773  }
774  if (cache_idx >= 1 << img->color_cache_bits) {
776  "color cache index out-of-bounds\n");
777  return AVERROR_INVALIDDATA;
778  }
779  AV_WB32(p, img->color_cache[cache_idx]);
780  x++;
781  if (x == width) {
782  x = 0;
783  y++;
784  }
785  }
786  }
787 
788  return 0;
789 }
790 
791 /* PRED_MODE_BLACK */
792 static void inv_predict_0(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  AV_WB32(p, 0xFF000000);
796 }
797 
798 /* PRED_MODE_L */
799 static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
800  const uint8_t *p_t, const uint8_t *p_tr)
801 {
802  AV_COPY32(p, p_l);
803 }
804 
805 /* PRED_MODE_T */
806 static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
807  const uint8_t *p_t, const uint8_t *p_tr)
808 {
809  AV_COPY32(p, p_t);
810 }
811 
812 /* PRED_MODE_TR */
813 static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
814  const uint8_t *p_t, const uint8_t *p_tr)
815 {
816  AV_COPY32(p, p_tr);
817 }
818 
819 /* PRED_MODE_TL */
820 static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
821  const uint8_t *p_t, const uint8_t *p_tr)
822 {
823  AV_COPY32(p, p_tl);
824 }
825 
826 /* PRED_MODE_AVG_T_AVG_L_TR */
827 static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
828  const uint8_t *p_t, const uint8_t *p_tr)
829 {
830  p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
831  p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
832  p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
833  p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
834 }
835 
836 /* PRED_MODE_AVG_L_TL */
837 static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
838  const uint8_t *p_t, const uint8_t *p_tr)
839 {
840  p[0] = p_l[0] + p_tl[0] >> 1;
841  p[1] = p_l[1] + p_tl[1] >> 1;
842  p[2] = p_l[2] + p_tl[2] >> 1;
843  p[3] = p_l[3] + p_tl[3] >> 1;
844 }
845 
846 /* PRED_MODE_AVG_L_T */
847 static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
848  const uint8_t *p_t, const uint8_t *p_tr)
849 {
850  p[0] = p_l[0] + p_t[0] >> 1;
851  p[1] = p_l[1] + p_t[1] >> 1;
852  p[2] = p_l[2] + p_t[2] >> 1;
853  p[3] = p_l[3] + p_t[3] >> 1;
854 }
855 
856 /* PRED_MODE_AVG_TL_T */
857 static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
858  const uint8_t *p_t, const uint8_t *p_tr)
859 {
860  p[0] = p_tl[0] + p_t[0] >> 1;
861  p[1] = p_tl[1] + p_t[1] >> 1;
862  p[2] = p_tl[2] + p_t[2] >> 1;
863  p[3] = p_tl[3] + p_t[3] >> 1;
864 }
865 
866 /* PRED_MODE_AVG_T_TR */
867 static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
868  const uint8_t *p_t, const uint8_t *p_tr)
869 {
870  p[0] = p_t[0] + p_tr[0] >> 1;
871  p[1] = p_t[1] + p_tr[1] >> 1;
872  p[2] = p_t[2] + p_tr[2] >> 1;
873  p[3] = p_t[3] + p_tr[3] >> 1;
874 }
875 
876 /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */
877 static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
878  const uint8_t *p_t, const uint8_t *p_tr)
879 {
880  p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
881  p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
882  p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
883  p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
884 }
885 
886 /* PRED_MODE_SELECT */
887 static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
888  const uint8_t *p_t, const uint8_t *p_tr)
889 {
890  int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) +
891  (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) +
892  (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) +
893  (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3]));
894  if (diff <= 0)
895  AV_COPY32(p, p_t);
896  else
897  AV_COPY32(p, p_l);
898 }
899 
900 /* PRED_MODE_ADD_SUBTRACT_FULL */
901 static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
902  const uint8_t *p_t, const uint8_t *p_tr)
903 {
904  p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
905  p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
906  p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
907  p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
908 }
909 
911 {
912  int d = a + b >> 1;
913  return av_clip_uint8(d + (d - c) / 2);
914 }
915 
916 /* PRED_MODE_ADD_SUBTRACT_HALF */
917 static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
918  const uint8_t *p_t, const uint8_t *p_tr)
919 {
920  p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]);
921  p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]);
922  p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]);
923  p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]);
924 }
925 
926 typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l,
927  const uint8_t *p_tl, const uint8_t *p_t,
928  const uint8_t *p_tr);
929 
930 static const inv_predict_func inverse_predict[14] = {
935 };
936 
937 static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
938 {
939  uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
940  uint8_t p[4];
941 
942  dec = GET_PIXEL(frame, x, y);
943  p_l = GET_PIXEL(frame, x - 1, y);
944  p_tl = GET_PIXEL(frame, x - 1, y - 1);
945  p_t = GET_PIXEL(frame, x, y - 1);
946  if (x == frame->width - 1)
947  p_tr = GET_PIXEL(frame, 0, y);
948  else
949  p_tr = GET_PIXEL(frame, x + 1, y - 1);
950 
951  inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
952 
953  dec[0] += p[0];
954  dec[1] += p[1];
955  dec[2] += p[2];
956  dec[3] += p[3];
957 }
958 
960 {
963  int x, y;
964 
965  for (y = 0; y < img->frame->height; y++) {
966  for (x = 0; x < img->frame->width; x++) {
967  int tx = x >> pimg->size_reduction;
968  int ty = y >> pimg->size_reduction;
969  enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2);
970 
971  if (x == 0) {
972  if (y == 0)
973  m = PRED_MODE_BLACK;
974  else
975  m = PRED_MODE_T;
976  } else if (y == 0)
977  m = PRED_MODE_L;
978 
979  if (m > 13) {
981  "invalid predictor mode: %d\n", m);
982  return AVERROR_INVALIDDATA;
983  }
984  inverse_prediction(img->frame, m, x, y);
985  }
986  }
987  return 0;
988 }
989 
991  uint8_t color)
992 {
993  return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5;
994 }
995 
997 {
998  ImageContext *img, *cimg;
999  int x, y, cx, cy;
1000  uint8_t *p, *cp;
1001 
1002  img = &s->image[IMAGE_ROLE_ARGB];
1003  cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM];
1004 
1005  for (y = 0; y < img->frame->height; y++) {
1006  for (x = 0; x < img->frame->width; x++) {
1007  cx = x >> cimg->size_reduction;
1008  cy = y >> cimg->size_reduction;
1009  cp = GET_PIXEL(cimg->frame, cx, cy);
1010  p = GET_PIXEL(img->frame, x, y);
1011 
1012  p[1] += color_transform_delta(cp[3], p[2]);
1013  p[3] += color_transform_delta(cp[2], p[2]) +
1014  color_transform_delta(cp[1], p[1]);
1015  }
1016  }
1017  return 0;
1018 }
1019 
1021 {
1022  int x, y;
1024 
1025  for (y = 0; y < img->frame->height; y++) {
1026  for (x = 0; x < img->frame->width; x++) {
1027  uint8_t *p = GET_PIXEL(img->frame, x, y);
1028  p[1] += p[2];
1029  p[3] += p[2];
1030  }
1031  }
1032  return 0;
1033 }
1034 
1036 {
1037  ImageContext *img;
1038  ImageContext *pal;
1039  int i, x, y;
1040  uint8_t *p;
1041 
1042  img = &s->image[IMAGE_ROLE_ARGB];
1043  pal = &s->image[IMAGE_ROLE_COLOR_INDEXING];
1044 
1045  if (pal->size_reduction > 0) {
1046  GetBitContext gb_g;
1047  uint8_t *line;
1048  int pixel_bits = 8 >> pal->size_reduction;
1049 
1051  if (!line)
1052  return AVERROR(ENOMEM);
1053 
1054  for (y = 0; y < img->frame->height; y++) {
1055  p = GET_PIXEL(img->frame, 0, y);
1056  memcpy(line, p, img->frame->linesize[0]);
1057  init_get_bits(&gb_g, line, img->frame->linesize[0] * 8);
1058  skip_bits(&gb_g, 16);
1059  i = 0;
1060  for (x = 0; x < img->frame->width; x++) {
1061  p = GET_PIXEL(img->frame, x, y);
1062  p[2] = get_bits(&gb_g, pixel_bits);
1063  i++;
1064  if (i == 1 << pal->size_reduction) {
1065  skip_bits(&gb_g, 24);
1066  i = 0;
1067  }
1068  }
1069  }
1070  av_free(line);
1071  }
1072 
1073  // switch to local palette if it's worth initializing it
1074  if (img->frame->height * img->frame->width > 300) {
1075  uint8_t palette[256 * 4];
1076  const int size = pal->frame->width * 4;
1077  av_assert0(size <= 1024U);
1078  memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size); // copy palette
1079  // set extra entries to transparent black
1080  memset(palette + size, 0, 256 * 4 - size);
1081  for (y = 0; y < img->frame->height; y++) {
1082  for (x = 0; x < img->frame->width; x++) {
1083  p = GET_PIXEL(img->frame, x, y);
1084  i = p[2];
1085  AV_COPY32(p, &palette[i * 4]);
1086  }
1087  }
1088  } else {
1089  for (y = 0; y < img->frame->height; y++) {
1090  for (x = 0; x < img->frame->width; x++) {
1091  p = GET_PIXEL(img->frame, x, y);
1092  i = p[2];
1093  if (i >= pal->frame->width) {
1094  AV_WB32(p, 0x00000000);
1095  } else {
1096  const uint8_t *pi = GET_PIXEL(pal->frame, i, 0);
1097  AV_COPY32(p, pi);
1098  }
1099  }
1100  }
1101  }
1102 
1103  return 0;
1104 }
1105 
1106 static void update_canvas_size(AVCodecContext *avctx, int w, int h)
1107 {
1108  WebPContext *s = avctx->priv_data;
1109  if (s->width && s->width != w) {
1110  av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n",
1111  s->width, w);
1112  }
1113  s->width = w;
1114  if (s->height && s->height != h) {
1115  av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n",
1116  s->height, h);
1117  }
1118  s->height = h;
1119 }
1120 
1122  int *got_frame, uint8_t *data_start,
1123  unsigned int data_size, int is_alpha_chunk)
1124 {
1125  WebPContext *s = avctx->priv_data;
1126  int w, h, ret, i, used;
1127 
1128  if (!is_alpha_chunk) {
1129  s->lossless = 1;
1130  avctx->pix_fmt = AV_PIX_FMT_ARGB;
1131  }
1132 
1133  ret = init_get_bits8(&s->gb, data_start, data_size);
1134  if (ret < 0)
1135  return ret;
1136 
1137  if (!is_alpha_chunk) {
1138  if (get_bits(&s->gb, 8) != 0x2F) {
1139  av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n");
1140  return AVERROR_INVALIDDATA;
1141  }
1142 
1143  w = get_bits(&s->gb, 14) + 1;
1144  h = get_bits(&s->gb, 14) + 1;
1145 
1146  update_canvas_size(avctx, w, h);
1147 
1148  ret = ff_set_dimensions(avctx, s->width, s->height);
1149  if (ret < 0)
1150  return ret;
1151 
1152  s->has_alpha = get_bits1(&s->gb);
1153 
1154  if (get_bits(&s->gb, 3) != 0x0) {
1155  av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n");
1156  return AVERROR_INVALIDDATA;
1157  }
1158  } else {
1159  if (!s->width || !s->height)
1160  return AVERROR_BUG;
1161  w = s->width;
1162  h = s->height;
1163  }
1164 
1165  /* parse transformations */
1166  s->nb_transforms = 0;
1167  s->reduced_width = 0;
1168  used = 0;
1169  while (get_bits1(&s->gb)) {
1170  enum TransformType transform = get_bits(&s->gb, 2);
1171  if (used & (1 << transform)) {
1172  av_log(avctx, AV_LOG_ERROR, "Transform %d used more than once\n",
1173  transform);
1174  ret = AVERROR_INVALIDDATA;
1175  goto free_and_return;
1176  }
1177  used |= (1 << transform);
1178  s->transforms[s->nb_transforms++] = transform;
1179  switch (transform) {
1180  case PREDICTOR_TRANSFORM:
1181  ret = parse_transform_predictor(s);
1182  break;
1183  case COLOR_TRANSFORM:
1184  ret = parse_transform_color(s);
1185  break;
1188  break;
1189  }
1190  if (ret < 0)
1191  goto free_and_return;
1192  }
1193 
1194  /* decode primary image */
1195  s->image[IMAGE_ROLE_ARGB].frame = p;
1196  if (is_alpha_chunk)
1199  if (ret < 0)
1200  goto free_and_return;
1201 
1202  /* apply transformations */
1203  for (i = s->nb_transforms - 1; i >= 0; i--) {
1204  switch (s->transforms[i]) {
1205  case PREDICTOR_TRANSFORM:
1206  ret = apply_predictor_transform(s);
1207  break;
1208  case COLOR_TRANSFORM:
1209  ret = apply_color_transform(s);
1210  break;
1211  case SUBTRACT_GREEN:
1213  break;
1216  break;
1217  }
1218  if (ret < 0)
1219  goto free_and_return;
1220  }
1221 
1222  *got_frame = 1;
1224  p->key_frame = 1;
1225  ret = data_size;
1226 
1227 free_and_return:
1228  for (i = 0; i < IMAGE_ROLE_NB; i++)
1229  image_ctx_free(&s->image[i]);
1230 
1231  return ret;
1232 }
1233 
1235 {
1236  int x, y, ls;
1237  uint8_t *dec;
1238 
1239  ls = frame->linesize[3];
1240 
1241  /* filter first row using horizontal filter */
1242  dec = frame->data[3] + 1;
1243  for (x = 1; x < frame->width; x++, dec++)
1244  *dec += *(dec - 1);
1245 
1246  /* filter first column using vertical filter */
1247  dec = frame->data[3] + ls;
1248  for (y = 1; y < frame->height; y++, dec += ls)
1249  *dec += *(dec - ls);
1250 
1251  /* filter the rest using the specified filter */
1252  switch (m) {
1254  for (y = 1; y < frame->height; y++) {
1255  dec = frame->data[3] + y * ls + 1;
1256  for (x = 1; x < frame->width; x++, dec++)
1257  *dec += *(dec - 1);
1258  }
1259  break;
1260  case ALPHA_FILTER_VERTICAL:
1261  for (y = 1; y < frame->height; y++) {
1262  dec = frame->data[3] + y * ls + 1;
1263  for (x = 1; x < frame->width; x++, dec++)
1264  *dec += *(dec - ls);
1265  }
1266  break;
1267  case ALPHA_FILTER_GRADIENT:
1268  for (y = 1; y < frame->height; y++) {
1269  dec = frame->data[3] + y * ls + 1;
1270  for (x = 1; x < frame->width; x++, dec++)
1271  dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1272  }
1273  break;
1274  }
1275 }
1276 
1278  uint8_t *data_start,
1279  unsigned int data_size)
1280 {
1281  WebPContext *s = avctx->priv_data;
1282  int x, y, ret;
1283 
1285  GetByteContext gb;
1286 
1287  bytestream2_init(&gb, data_start, data_size);
1288  for (y = 0; y < s->height; y++)
1289  bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y,
1290  s->width);
1291  } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) {
1292  uint8_t *ap, *pp;
1293  int alpha_got_frame = 0;
1294 
1295  s->alpha_frame = av_frame_alloc();
1296  if (!s->alpha_frame)
1297  return AVERROR(ENOMEM);
1298 
1299  ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame,
1300  data_start, data_size, 1);
1301  if (ret < 0) {
1303  return ret;
1304  }
1305  if (!alpha_got_frame) {
1307  return AVERROR_INVALIDDATA;
1308  }
1309 
1310  /* copy green component of alpha image to alpha plane of primary image */
1311  for (y = 0; y < s->height; y++) {
1312  ap = GET_PIXEL(s->alpha_frame, 0, y) + 2;
1313  pp = p->data[3] + p->linesize[3] * y;
1314  for (x = 0; x < s->width; x++) {
1315  *pp = *ap;
1316  pp++;
1317  ap += 4;
1318  }
1319  }
1321  }
1322 
1323  /* apply alpha filtering */
1324  if (s->alpha_filter)
1326 
1327  return 0;
1328 }
1329 
1331  int *got_frame, uint8_t *data_start,
1332  unsigned int data_size)
1333 {
1334  WebPContext *s = avctx->priv_data;
1335  AVPacket pkt;
1336  int ret;
1337 
1338  if (!s->initialized) {
1339  ff_vp8_decode_init(avctx);
1340  s->initialized = 1;
1341  s->v.actually_webp = 1;
1342  }
1344  s->lossless = 0;
1345 
1346  if (data_size > INT_MAX) {
1347  av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n");
1348  return AVERROR_PATCHWELCOME;
1349  }
1350 
1351  av_init_packet(&pkt);
1352  pkt.data = data_start;
1353  pkt.size = data_size;
1354 
1355  ret = ff_vp8_decode_frame(avctx, p, got_frame, &pkt);
1356  if (ret < 0)
1357  return ret;
1358 
1359  if (!*got_frame)
1360  return AVERROR_INVALIDDATA;
1361 
1362  update_canvas_size(avctx, avctx->width, avctx->height);
1363 
1364  if (s->has_alpha) {
1365  ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data,
1366  s->alpha_data_size);
1367  if (ret < 0)
1368  return ret;
1369  }
1370  return ret;
1371 }
1372 
1373 static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1374  AVPacket *avpkt)
1375 {
1376  AVFrame * const p = data;
1377  WebPContext *s = avctx->priv_data;
1378  GetByteContext gb;
1379  int ret;
1380  uint32_t chunk_type, chunk_size;
1381  int vp8x_flags = 0;
1382 
1383  s->avctx = avctx;
1384  s->width = 0;
1385  s->height = 0;
1386  *got_frame = 0;
1387  s->has_alpha = 0;
1388  s->has_exif = 0;
1389  s->has_iccp = 0;
1390  bytestream2_init(&gb, avpkt->data, avpkt->size);
1391 
1392  if (bytestream2_get_bytes_left(&gb) < 12)
1393  return AVERROR_INVALIDDATA;
1394 
1395  if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
1396  av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
1397  return AVERROR_INVALIDDATA;
1398  }
1399 
1400  chunk_size = bytestream2_get_le32(&gb);
1401  if (bytestream2_get_bytes_left(&gb) < chunk_size)
1402  return AVERROR_INVALIDDATA;
1403 
1404  if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) {
1405  av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n");
1406  return AVERROR_INVALIDDATA;
1407  }
1408 
1409  while (bytestream2_get_bytes_left(&gb) > 8) {
1410  char chunk_str[5] = { 0 };
1411 
1412  chunk_type = bytestream2_get_le32(&gb);
1413  chunk_size = bytestream2_get_le32(&gb);
1414  if (chunk_size == UINT32_MAX)
1415  return AVERROR_INVALIDDATA;
1416  chunk_size += chunk_size & 1;
1417 
1418  if (bytestream2_get_bytes_left(&gb) < chunk_size) {
1419  /* we seem to be running out of data, but it could also be that the
1420  bitstream has trailing junk leading to bogus chunk_size. */
1421  break;
1422  }
1423 
1424  switch (chunk_type) {
1425  case MKTAG('V', 'P', '8', ' '):
1426  if (!*got_frame) {
1427  ret = vp8_lossy_decode_frame(avctx, p, got_frame,
1428  avpkt->data + bytestream2_tell(&gb),
1429  chunk_size);
1430  if (ret < 0)
1431  return ret;
1432  }
1433  bytestream2_skip(&gb, chunk_size);
1434  break;
1435  case MKTAG('V', 'P', '8', 'L'):
1436  if (!*got_frame) {
1437  ret = vp8_lossless_decode_frame(avctx, p, got_frame,
1438  avpkt->data + bytestream2_tell(&gb),
1439  chunk_size, 0);
1440  if (ret < 0)
1441  return ret;
1443  }
1444  bytestream2_skip(&gb, chunk_size);
1445  break;
1446  case MKTAG('V', 'P', '8', 'X'):
1447  if (s->width || s->height || *got_frame) {
1448  av_log(avctx, AV_LOG_ERROR, "Canvas dimensions are already set\n");
1449  return AVERROR_INVALIDDATA;
1450  }
1451  vp8x_flags = bytestream2_get_byte(&gb);
1452  bytestream2_skip(&gb, 3);
1453  s->width = bytestream2_get_le24(&gb) + 1;
1454  s->height = bytestream2_get_le24(&gb) + 1;
1455  ret = av_image_check_size(s->width, s->height, 0, avctx);
1456  if (ret < 0)
1457  return ret;
1458  break;
1459  case MKTAG('A', 'L', 'P', 'H'): {
1460  int alpha_header, filter_m, compression;
1461 
1462  if (!(vp8x_flags & VP8X_FLAG_ALPHA)) {
1463  av_log(avctx, AV_LOG_WARNING,
1464  "ALPHA chunk present, but alpha bit not set in the "
1465  "VP8X header\n");
1466  }
1467  if (chunk_size == 0) {
1468  av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n");
1469  return AVERROR_INVALIDDATA;
1470  }
1471  alpha_header = bytestream2_get_byte(&gb);
1472  s->alpha_data = avpkt->data + bytestream2_tell(&gb);
1473  s->alpha_data_size = chunk_size - 1;
1475 
1476  filter_m = (alpha_header >> 2) & 0x03;
1477  compression = alpha_header & 0x03;
1478 
1479  if (compression > ALPHA_COMPRESSION_VP8L) {
1480  av_log(avctx, AV_LOG_VERBOSE,
1481  "skipping unsupported ALPHA chunk\n");
1482  } else {
1483  s->has_alpha = 1;
1484  s->alpha_compression = compression;
1485  s->alpha_filter = filter_m;
1486  }
1487 
1488  break;
1489  }
1490  case MKTAG('E', 'X', 'I', 'F'): {
1491  int le, ifd_offset, exif_offset = bytestream2_tell(&gb);
1492  AVDictionary *exif_metadata = NULL;
1493  GetByteContext exif_gb;
1494 
1495  if (s->has_exif) {
1496  av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n");
1497  goto exif_end;
1498  }
1499  if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA))
1500  av_log(avctx, AV_LOG_WARNING,
1501  "EXIF chunk present, but Exif bit not set in the "
1502  "VP8X header\n");
1503 
1504  s->has_exif = 1;
1505  bytestream2_init(&exif_gb, avpkt->data + exif_offset,
1506  avpkt->size - exif_offset);
1507  if (ff_tdecode_header(&exif_gb, &le, &ifd_offset) < 0) {
1508  av_log(avctx, AV_LOG_ERROR, "invalid TIFF header "
1509  "in Exif data\n");
1510  goto exif_end;
1511  }
1512 
1513  bytestream2_seek(&exif_gb, ifd_offset, SEEK_SET);
1514  if (ff_exif_decode_ifd(avctx, &exif_gb, le, 0, &exif_metadata) < 0) {
1515  av_log(avctx, AV_LOG_ERROR, "error decoding Exif data\n");
1516  goto exif_end;
1517  }
1518 
1519  av_dict_copy(&((AVFrame *) data)->metadata, exif_metadata, 0);
1520 
1521 exif_end:
1522  av_dict_free(&exif_metadata);
1523  bytestream2_skip(&gb, chunk_size);
1524  break;
1525  }
1526  case MKTAG('I', 'C', 'C', 'P'): {
1527  AVFrameSideData *sd;
1528 
1529  if (s->has_iccp) {
1530  av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra ICCP chunk\n");
1531  bytestream2_skip(&gb, chunk_size);
1532  break;
1533  }
1534  if (!(vp8x_flags & VP8X_FLAG_ICC))
1535  av_log(avctx, AV_LOG_WARNING,
1536  "ICCP chunk present, but ICC Profile bit not set in the "
1537  "VP8X header\n");
1538 
1539  s->has_iccp = 1;
1541  if (!sd)
1542  return AVERROR(ENOMEM);
1543 
1544  bytestream2_get_buffer(&gb, sd->data, chunk_size);
1545  break;
1546  }
1547  case MKTAG('A', 'N', 'I', 'M'):
1548  case MKTAG('A', 'N', 'M', 'F'):
1549  case MKTAG('X', 'M', 'P', ' '):
1550  AV_WL32(chunk_str, chunk_type);
1551  av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n",
1552  chunk_str);
1553  bytestream2_skip(&gb, chunk_size);
1554  break;
1555  default:
1556  AV_WL32(chunk_str, chunk_type);
1557  av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
1558  chunk_str);
1559  bytestream2_skip(&gb, chunk_size);
1560  break;
1561  }
1562  }
1563 
1564  if (!*got_frame) {
1565  av_log(avctx, AV_LOG_ERROR, "image data not found\n");
1566  return AVERROR_INVALIDDATA;
1567  }
1568 
1569  return avpkt->size;
1570 }
1571 
1573 {
1574  WebPContext *s = avctx->priv_data;
1575 
1576  if (s->initialized)
1577  return ff_vp8_decode_free(avctx);
1578 
1579  return 0;
1580 }
1581 
1583  .name = "webp",
1584  .long_name = NULL_IF_CONFIG_SMALL("WebP image"),
1585  .type = AVMEDIA_TYPE_VIDEO,
1586  .id = AV_CODEC_ID_WEBP,
1587  .priv_data_size = sizeof(WebPContext),
1589  .close = webp_decode_close,
1590  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1591 };
int nb_huffman_groups
Definition: webp.c:182
#define extra_bits(eb)
Definition: intrax8.c:159
static int read_huffman_code_normal(WebPContext *s, HuffReader *hc, int alphabet_size)
Definition: webp.c:361
enum ImageRole role
Definition: webp.c:178
#define NULL
Definition: coverity.c:32
HuffReader * huffman_groups
Definition: webp.c:183
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
static const struct @78 transforms[18]
int ff_exif_decode_ifd(void *logctx, GetByteContext *gbytes, int le, int depth, AVDictionary **metadata)
Definition: exif.c:122
TransformType
Definition: webp.c:110
static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:877
float re
Definition: fft.c:82
ImageRole
Definition: webp.c:148
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
int initialized
Definition: webp.c:193
static int parse_transform_color_indexing(WebPContext *s)
Definition: webp.c:529
const uint8_t ff_reverse[256]
Definition: reverse.c:23
static HuffReader * get_huffman_group(WebPContext *s, ImageContext *img, int x, int y)
Definition: webp.c:564
HuffmanIndex
Definition: webp.c:134
static const uint8_t code_length_code_order[NUM_CODE_LENGTH_CODES]
Definition: webp.c:76
int size
Definition: packet.h:356
const char * b
Definition: vf_curves.c:116
void(* inv_predict_func)(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:926
#define VP8X_FLAG_EXIF_METADATA
Definition: webp.c:56
static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:887
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
Definition: webp.c:273
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
GetBitContext gb
Definition: webp.c:190
static int8_t ff_u8_to_s8(uint8_t a)
Definition: mathops.h:233
static const inv_predict_func inverse_predict[14]
Definition: webp.c:930
static AVPacket pkt
static int apply_color_indexing_transform(WebPContext *s)
Definition: webp.c:1035
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
AVCodec.
Definition: codec.h:190
EXIF metadata parser.
#define AV_COPY32(d, s)
Definition: intreadwrite.h:601
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AlphaCompression
Definition: webp.c:98
static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c)
Definition: webp.c:910
enum TransformType transforms[4]
Definition: webp.c:206
uint16_t simple_symbols[2]
Definition: webp.c:174
int height
Definition: webp.c:202
static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p, uint8_t *data_start, unsigned int data_size)
Definition: webp.c:1277
#define NUM_LITERAL_CODES
Definition: webp.c:64
#define img
static av_always_inline void color_cache_put(ImageContext *img, uint32_t c)
Definition: webp.c:581
enum AlphaFilter alpha_filter
Definition: webp.c:196
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:867
uint8_t * alpha_data
Definition: webp.c:197
#define NUM_CODE_LENGTH_CODES
Definition: webp.c:62
int reduced_width
Definition: webp.c:207
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
int nb_huffman_groups
Definition: webp.c:208
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
int nb_symbols
Definition: webp.c:173
#define AV_RB32
Definition: intreadwrite.h:130
Multithreading support functions.
int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vp8.c:2814
#define FF_CODEC_PROPERTY_LOSSLESS
Definition: avcodec.h:2192
int simple
Definition: webp.c:172
static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:901
static AVFrame * frame
const char data[16]
Definition: mxf.c:91
Structure to hold side data for an AVFrame.
Definition: frame.h:206
#define NUM_SHORT_DISTANCES
Definition: webp.c:67
int pt
Definition: rtp.c:35
static void finish(void)
Definition: movenc.c:345
uint8_t * data
Definition: packet.h:355
#define VP8X_FLAG_ICC
Definition: webp.c:58
bitstream reader API header.
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static int decode_entropy_image(WebPContext *s)
Definition: webp.c:463
#define max(a, b)
Definition: cuda_runtime.h:33
#define av_log(a,...)
static const uint16_t table[]
Definition: prosumer.c:206
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:799
#define U(x)
Definition: vp56_arith.h:37
static int apply_color_transform(WebPContext *s)
Definition: webp.c:996
#define VP8X_FLAG_ALPHA
Definition: webp.c:57
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:178
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
int width
Definition: frame.h:358
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: webp.c:1373
static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:837
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
const uint8_t * code
Definition: spdifenc.c:413
const char * r
Definition: vf_curves.c:114
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
Definition: graph2dot.c:48
const char * name
Name of the codec implementation.
Definition: codec.h:197
uint8_t bits
Definition: vp3data.h:202
static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:813
int has_exif
Definition: webp.c:199
VP8Context v
Definition: webp.c:189
static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, uint8_t *data_start, unsigned int data_size)
Definition: webp.c:1330
static const uint8_t offset[127][2]
Definition: vf_spp.c:93
#define CLOSE_READER(name, gb)
Definition: get_bits.h:149
#define FFMAX(a, b)
Definition: common.h:94
static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, uint8_t *data_start, unsigned int data_size, int is_alpha_chunk)
Definition: webp.c:1121
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:106
enum AlphaCompression alpha_compression
Definition: webp.c:195
static void image_ctx_free(ImageContext *img)
Definition: webp.c:218
Definition: vlc.h:26
#define SKIP_BITS(name, gb, num)
Definition: get_bits.h:193
static float distance(float x, float y, int band)
static av_always_inline int webp_get_vlc(GetBitContext *gb, VLC_TYPE(*table)[2])
Definition: webp.c:241
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:282
#define PARSE_BLOCK_SIZE(w, h)
Definition: webp.c:457
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:806
int has_iccp
Definition: webp.c:200
#define xi(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h2645.c:396
#define width
int width
picture width / height.
Definition: avcodec.h:699
uint8_t w
Definition: llviddspenc.c:38
uint32_t * color_cache
Definition: webp.c:181
AlphaFilter
Definition: webp.c:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame * frame
Definition: webp.c:179
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:143
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:199
int has_alpha
Definition: webp.c:194
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:211
PredictionMode
Definition: webp.c:117
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:373
static const int8_t transform[32][32]
Definition: hevcdsp.c:27
int alpha_data_size
Definition: webp.c:198
Libavcodec external API header.
typedef void(RENAME(mix_any_func_type))
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static int huff_reader_build_canonical(HuffReader *r, int *code_lengths, int alphabet_size)
Definition: webp.c:284
main external API structure.
Definition: avcodec.h:526
#define OPEN_READER(name, gb)
Definition: get_bits.h:138
uint8_t * data
Definition: frame.h:208
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
static av_always_inline uint8_t color_transform_delta(uint8_t color_pred, uint8_t color)
Definition: webp.c:990
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:727
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int index
Definition: gxfenc.c:89
AVCodec ff_webp_decoder
Definition: webp.c:1582
static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:820
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:792
#define MAX_HUFFMAN_CODE_LENGTH
Definition: webp.c:68
int size_reduction
Definition: webp.c:184
int actually_webp
Definition: vp8.h:151
static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE]
Definition: webp.c:70
#define HUFFMAN_CODES_PER_META_CODE
Definition: webp.c:63
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:325
static void read_huffman_code_simple(WebPContext *s, HuffReader *hc)
Definition: webp.c:346
av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
Definition: vp8.c:2898
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
Definition: vp8.c:2828
static int parse_transform_predictor(WebPContext *s)
Definition: webp.c:497
#define GET_PIXEL(frame, x, y)
Definition: webp.c:212
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
static av_cold int webp_decode_close(AVCodecContext *avctx)
Definition: webp.c:1572
int nb_transforms
Definition: webp.c:205
common internal api header.
static int apply_subtract_green_transform(WebPContext *s)
Definition: webp.c:1020
static double c[64]
int is_alpha_primary
Definition: webp.c:185
#define GET_PIXEL_COMP(frame, x, y, c)
Definition: webp.c:215
static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:857
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:2191
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:35
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
void * priv_data
Definition: avcodec.h:553
static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2]
Definition: webp.c:80
ImageContext image[IMAGE_ROLE_NB]
Definition: webp.c:209
int width
Definition: webp.c:201
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int apply_predictor_transform(WebPContext *s)
Definition: webp.c:959
#define av_free(p)
VLC vlc
Definition: webp.c:171
int len
int color_cache_bits
Definition: webp.c:180
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static int parse_transform_color(WebPContext *s)
Definition: webp.c:513
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:847
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, int w, int h)
Definition: webp.c:587
static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:917
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
Decodes a TIFF header from the input bytestream and sets the endianness in *le and the offset to the ...
Definition: tiff_common.c:261
AVCodecContext * avctx
Definition: webp.c:192
int height
Definition: frame.h:358
static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m)
Definition: webp.c:1234
#define av_always_inline
Definition: attributes.h:45
static const uint8_t block_bits[]
Definition: imm4.c:106
#define VLC_TYPE
Definition: vlc.h:24
#define av_malloc_array(a, b)
int lossless
Definition: webp.c:203
static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
Definition: webp.c:937
#define MKTAG(a, b, c, d)
Definition: common.h:406
This structure stores compressed data.
Definition: packet.h:332
AVFrame * alpha_frame
Definition: webp.c:191
static void update_canvas_size(AVCodecContext *avctx, int w, int h)
Definition: webp.c:1106
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
Definition: webp.c:827
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
#define NUM_LENGTH_CODES
Definition: webp.c:65
#define NUM_DISTANCE_CODES
Definition: webp.c:66
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190