FFmpeg
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 
236 {
237  if (r->simple) {
238  if (r->nb_symbols == 1)
239  return r->simple_symbols[0];
240  else
241  return r->simple_symbols[get_bits1(gb)];
242  } else
243  return get_vlc2(gb, r->vlc.table, 8, 2);
244 }
245 
246 static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths,
247  int alphabet_size)
248 {
249  int len = 0, sym, code = 0, ret;
250  int max_code_length = 0;
251  uint16_t *codes;
252 
253  /* special-case 1 symbol since the vlc reader cannot handle it */
254  for (sym = 0; sym < alphabet_size; sym++) {
255  if (code_lengths[sym] > 0) {
256  len++;
257  code = sym;
258  if (len > 1)
259  break;
260  }
261  }
262  if (len == 1) {
263  r->nb_symbols = 1;
264  r->simple_symbols[0] = code;
265  r->simple = 1;
266  return 0;
267  }
268 
269  for (sym = 0; sym < alphabet_size; sym++)
270  max_code_length = FFMAX(max_code_length, code_lengths[sym]);
271 
272  if (max_code_length == 0 || max_code_length > MAX_HUFFMAN_CODE_LENGTH)
273  return AVERROR(EINVAL);
274 
275  codes = av_malloc_array(alphabet_size, sizeof(*codes));
276  if (!codes)
277  return AVERROR(ENOMEM);
278 
279  code = 0;
280  r->nb_symbols = 0;
281  for (len = 1; len <= max_code_length; len++) {
282  for (sym = 0; sym < alphabet_size; sym++) {
283  if (code_lengths[sym] != len)
284  continue;
285  codes[sym] = code++;
286  r->nb_symbols++;
287  }
288  code <<= 1;
289  }
290  if (!r->nb_symbols) {
291  av_free(codes);
292  return AVERROR_INVALIDDATA;
293  }
294 
295  ret = init_vlc(&r->vlc, 8, alphabet_size,
296  code_lengths, sizeof(*code_lengths), sizeof(*code_lengths),
297  codes, sizeof(*codes), sizeof(*codes), INIT_VLC_OUTPUT_LE);
298  if (ret < 0) {
299  av_free(codes);
300  return ret;
301  }
302  r->simple = 0;
303 
304  av_free(codes);
305  return 0;
306 }
307 
309 {
310  hc->nb_symbols = get_bits1(&s->gb) + 1;
311 
312  if (get_bits1(&s->gb))
313  hc->simple_symbols[0] = get_bits(&s->gb, 8);
314  else
315  hc->simple_symbols[0] = get_bits1(&s->gb);
316 
317  if (hc->nb_symbols == 2)
318  hc->simple_symbols[1] = get_bits(&s->gb, 8);
319 
320  hc->simple = 1;
321 }
322 
324  int alphabet_size)
325 {
326  HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
327  uint8_t *code_lengths;
328  uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
329  int i, symbol, max_symbol, prev_code_len, ret;
330  int num_codes = 4 + get_bits(&s->gb, 4);
331 
332  av_assert1(num_codes <= NUM_CODE_LENGTH_CODES);
333 
334  for (i = 0; i < num_codes; i++)
335  code_length_code_lengths[code_length_code_order[i]] = get_bits(&s->gb, 3);
336 
337  ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths,
339  if (ret < 0)
340  return ret;
341 
342  code_lengths = av_mallocz(alphabet_size);
343  if (!code_lengths) {
344  ret = AVERROR(ENOMEM);
345  goto finish;
346  }
347 
348  if (get_bits1(&s->gb)) {
349  int bits = 2 + 2 * get_bits(&s->gb, 3);
350  max_symbol = 2 + get_bits(&s->gb, bits);
351  if (max_symbol > alphabet_size) {
352  av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n",
353  max_symbol, alphabet_size);
354  ret = AVERROR_INVALIDDATA;
355  goto finish;
356  }
357  } else {
358  max_symbol = alphabet_size;
359  }
360 
361  prev_code_len = 8;
362  symbol = 0;
363  while (symbol < alphabet_size) {
364  int code_len;
365 
366  if (!max_symbol--)
367  break;
368  code_len = huff_reader_get_symbol(&code_len_hc, &s->gb);
369  if (code_len < 16) {
370  /* Code length code [0..15] indicates literal code lengths. */
371  code_lengths[symbol++] = code_len;
372  if (code_len)
373  prev_code_len = code_len;
374  } else {
375  int repeat = 0, length = 0;
376  switch (code_len) {
377  case 16:
378  /* Code 16 repeats the previous non-zero value [3..6] times,
379  * i.e., 3 + ReadBits(2) times. If code 16 is used before a
380  * non-zero value has been emitted, a value of 8 is repeated. */
381  repeat = 3 + get_bits(&s->gb, 2);
382  length = prev_code_len;
383  break;
384  case 17:
385  /* Code 17 emits a streak of zeros [3..10], i.e.,
386  * 3 + ReadBits(3) times. */
387  repeat = 3 + get_bits(&s->gb, 3);
388  break;
389  case 18:
390  /* Code 18 emits a streak of zeros of length [11..138], i.e.,
391  * 11 + ReadBits(7) times. */
392  repeat = 11 + get_bits(&s->gb, 7);
393  break;
394  }
395  if (symbol + repeat > alphabet_size) {
397  "invalid symbol %d + repeat %d > alphabet size %d\n",
398  symbol, repeat, alphabet_size);
399  ret = AVERROR_INVALIDDATA;
400  goto finish;
401  }
402  while (repeat-- > 0)
403  code_lengths[symbol++] = length;
404  }
405  }
406 
407  ret = huff_reader_build_canonical(hc, code_lengths, alphabet_size);
408 
409 finish:
410  ff_free_vlc(&code_len_hc.vlc);
411  av_free(code_lengths);
412  return ret;
413 }
414 
415 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
416  int w, int h);
417 
418 #define PARSE_BLOCK_SIZE(w, h) do { \
419  block_bits = get_bits(&s->gb, 3) + 2; \
420  blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
421  blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
422 } while (0)
423 
425 {
426  ImageContext *img;
427  int ret, block_bits, width, blocks_w, blocks_h, x, y, max;
428 
429  width = s->width;
430  if (s->reduced_width > 0)
431  width = s->reduced_width;
432 
433  PARSE_BLOCK_SIZE(width, s->height);
434 
435  ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h);
436  if (ret < 0)
437  return ret;
438 
439  img = &s->image[IMAGE_ROLE_ENTROPY];
440  img->size_reduction = block_bits;
441 
442  /* the number of huffman groups is determined by the maximum group number
443  * coded in the entropy image */
444  max = 0;
445  for (y = 0; y < img->frame->height; y++) {
446  for (x = 0; x < img->frame->width; x++) {
447  int p0 = GET_PIXEL_COMP(img->frame, x, y, 1);
448  int p1 = GET_PIXEL_COMP(img->frame, x, y, 2);
449  int p = p0 << 8 | p1;
450  max = FFMAX(max, p);
451  }
452  }
453  s->nb_huffman_groups = max + 1;
454 
455  return 0;
456 }
457 
459 {
460  int block_bits, blocks_w, blocks_h, ret;
461 
462  PARSE_BLOCK_SIZE(s->width, s->height);
463 
465  blocks_h);
466  if (ret < 0)
467  return ret;
468 
470 
471  return 0;
472 }
473 
475 {
476  int block_bits, blocks_w, blocks_h, ret;
477 
478  PARSE_BLOCK_SIZE(s->width, s->height);
479 
481  blocks_h);
482  if (ret < 0)
483  return ret;
484 
486 
487  return 0;
488 }
489 
491 {
492  ImageContext *img;
493  int width_bits, index_size, ret, x;
494  uint8_t *ct;
495 
496  index_size = get_bits(&s->gb, 8) + 1;
497 
498  if (index_size <= 2)
499  width_bits = 3;
500  else if (index_size <= 4)
501  width_bits = 2;
502  else if (index_size <= 16)
503  width_bits = 1;
504  else
505  width_bits = 0;
506 
508  index_size, 1);
509  if (ret < 0)
510  return ret;
511 
512  img = &s->image[IMAGE_ROLE_COLOR_INDEXING];
513  img->size_reduction = width_bits;
514  if (width_bits > 0)
515  s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits;
516 
517  /* color index values are delta-coded */
518  ct = img->frame->data[0] + 4;
519  for (x = 4; x < img->frame->width * 4; x++, ct++)
520  ct[0] += ct[-4];
521 
522  return 0;
523 }
524 
526  int x, int y)
527 {
529  int group = 0;
530 
531  if (gimg->size_reduction > 0) {
532  int group_x = x >> gimg->size_reduction;
533  int group_y = y >> gimg->size_reduction;
534  int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1);
535  int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2);
536  group = g0 << 8 | g1;
537  }
538 
539  return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE];
540 }
541 
543 {
544  uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits);
545  img->color_cache[cache_idx] = c;
546 }
547 
549  int w, int h)
550 {
551  ImageContext *img;
552  HuffReader *hg;
553  int i, j, ret, x, y, width;
554 
555  img = &s->image[role];
556  img->role = role;
557 
558  if (!img->frame) {
559  img->frame = av_frame_alloc();
560  if (!img->frame)
561  return AVERROR(ENOMEM);
562  }
563 
564  img->frame->format = AV_PIX_FMT_ARGB;
565  img->frame->width = w;
566  img->frame->height = h;
567 
568  if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) {
569  ThreadFrame pt = { .f = img->frame };
570  ret = ff_thread_get_buffer(s->avctx, &pt, 0);
571  } else
572  ret = av_frame_get_buffer(img->frame, 1);
573  if (ret < 0)
574  return ret;
575 
576  if (get_bits1(&s->gb)) {
577  img->color_cache_bits = get_bits(&s->gb, 4);
578  if (img->color_cache_bits < 1 || img->color_cache_bits > 11) {
579  av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n",
580  img->color_cache_bits);
581  return AVERROR_INVALIDDATA;
582  }
584  sizeof(*img->color_cache));
585  if (!img->color_cache)
586  return AVERROR(ENOMEM);
587  } else {
588  img->color_cache_bits = 0;
589  }
590 
591  img->nb_huffman_groups = 1;
592  if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) {
593  ret = decode_entropy_image(s);
594  if (ret < 0)
595  return ret;
597  }
600  sizeof(*img->huffman_groups));
601  if (!img->huffman_groups)
602  return AVERROR(ENOMEM);
603 
604  for (i = 0; i < img->nb_huffman_groups; i++) {
606  for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) {
607  int alphabet_size = alphabet_sizes[j];
608  if (!j && img->color_cache_bits > 0)
609  alphabet_size += 1 << img->color_cache_bits;
610 
611  if (get_bits1(&s->gb)) {
612  read_huffman_code_simple(s, &hg[j]);
613  } else {
614  ret = read_huffman_code_normal(s, &hg[j], alphabet_size);
615  if (ret < 0)
616  return ret;
617  }
618  }
619  }
620 
621  width = img->frame->width;
622  if (role == IMAGE_ROLE_ARGB && s->reduced_width > 0)
623  width = s->reduced_width;
624 
625  x = 0; y = 0;
626  while (y < img->frame->height) {
627  int v;
628 
629  hg = get_huffman_group(s, img, x, y);
631  if (v < NUM_LITERAL_CODES) {
632  /* literal pixel values */
633  uint8_t *p = GET_PIXEL(img->frame, x, y);
634  p[2] = v;
635  p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb);
636  p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb);
637  p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb);
638  if (img->color_cache_bits)
639  color_cache_put(img, AV_RB32(p));
640  x++;
641  if (x == width) {
642  x = 0;
643  y++;
644  }
645  } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
646  /* LZ77 backwards mapping */
647  int prefix_code, length, distance, ref_x, ref_y;
648 
649  /* parse length and distance */
650  prefix_code = v - NUM_LITERAL_CODES;
651  if (prefix_code < 4) {
652  length = prefix_code + 1;
653  } else {
654  int extra_bits = (prefix_code - 2) >> 1;
655  int offset = 2 + (prefix_code & 1) << extra_bits;
656  length = offset + get_bits(&s->gb, extra_bits) + 1;
657  }
658  prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb);
659  if (prefix_code > 39U) {
661  "distance prefix code too large: %d\n", prefix_code);
662  return AVERROR_INVALIDDATA;
663  }
664  if (prefix_code < 4) {
665  distance = prefix_code + 1;
666  } else {
667  int extra_bits = prefix_code - 2 >> 1;
668  int offset = 2 + (prefix_code & 1) << extra_bits;
669  distance = offset + get_bits(&s->gb, extra_bits) + 1;
670  }
671 
672  /* find reference location */
673  if (distance <= NUM_SHORT_DISTANCES) {
674  int xi = lz77_distance_offsets[distance - 1][0];
675  int yi = lz77_distance_offsets[distance - 1][1];
676  distance = FFMAX(1, xi + yi * width);
677  } else {
678  distance -= NUM_SHORT_DISTANCES;
679  }
680  ref_x = x;
681  ref_y = y;
682  if (distance <= x) {
683  ref_x -= distance;
684  distance = 0;
685  } else {
686  ref_x = 0;
687  distance -= x;
688  }
689  while (distance >= width) {
690  ref_y--;
691  distance -= width;
692  }
693  if (distance > 0) {
694  ref_x = width - distance;
695  ref_y--;
696  }
697  ref_x = FFMAX(0, ref_x);
698  ref_y = FFMAX(0, ref_y);
699 
700  /* copy pixels
701  * source and dest regions can overlap and wrap lines, so just
702  * copy per-pixel */
703  for (i = 0; i < length; i++) {
704  uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y);
705  uint8_t *p = GET_PIXEL(img->frame, x, y);
706 
707  AV_COPY32(p, p_ref);
708  if (img->color_cache_bits)
709  color_cache_put(img, AV_RB32(p));
710  x++;
711  ref_x++;
712  if (x == width) {
713  x = 0;
714  y++;
715  }
716  if (ref_x == width) {
717  ref_x = 0;
718  ref_y++;
719  }
720  if (y == img->frame->height || ref_y == img->frame->height)
721  break;
722  }
723  } else {
724  /* read from color cache */
725  uint8_t *p = GET_PIXEL(img->frame, x, y);
726  int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES);
727 
728  if (!img->color_cache_bits) {
729  av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n");
730  return AVERROR_INVALIDDATA;
731  }
732  if (cache_idx >= 1 << img->color_cache_bits) {
734  "color cache index out-of-bounds\n");
735  return AVERROR_INVALIDDATA;
736  }
737  AV_WB32(p, img->color_cache[cache_idx]);
738  x++;
739  if (x == width) {
740  x = 0;
741  y++;
742  }
743  }
744  }
745 
746  return 0;
747 }
748 
749 /* PRED_MODE_BLACK */
750 static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
751  const uint8_t *p_t, const uint8_t *p_tr)
752 {
753  AV_WB32(p, 0xFF000000);
754 }
755 
756 /* PRED_MODE_L */
757 static void inv_predict_1(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_COPY32(p, p_l);
761 }
762 
763 /* PRED_MODE_T */
764 static void inv_predict_2(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_t);
768 }
769 
770 /* PRED_MODE_TR */
771 static void inv_predict_3(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_tr);
775 }
776 
777 /* PRED_MODE_TL */
778 static void inv_predict_4(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_tl);
782 }
783 
784 /* PRED_MODE_AVG_T_AVG_L_TR */
785 static void inv_predict_5(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  p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
789  p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
790  p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
791  p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
792 }
793 
794 /* PRED_MODE_AVG_L_TL */
795 static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
796  const uint8_t *p_t, const uint8_t *p_tr)
797 {
798  p[0] = p_l[0] + p_tl[0] >> 1;
799  p[1] = p_l[1] + p_tl[1] >> 1;
800  p[2] = p_l[2] + p_tl[2] >> 1;
801  p[3] = p_l[3] + p_tl[3] >> 1;
802 }
803 
804 /* PRED_MODE_AVG_L_T */
805 static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
806  const uint8_t *p_t, const uint8_t *p_tr)
807 {
808  p[0] = p_l[0] + p_t[0] >> 1;
809  p[1] = p_l[1] + p_t[1] >> 1;
810  p[2] = p_l[2] + p_t[2] >> 1;
811  p[3] = p_l[3] + p_t[3] >> 1;
812 }
813 
814 /* PRED_MODE_AVG_TL_T */
815 static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
816  const uint8_t *p_t, const uint8_t *p_tr)
817 {
818  p[0] = p_tl[0] + p_t[0] >> 1;
819  p[1] = p_tl[1] + p_t[1] >> 1;
820  p[2] = p_tl[2] + p_t[2] >> 1;
821  p[3] = p_tl[3] + p_t[3] >> 1;
822 }
823 
824 /* PRED_MODE_AVG_T_TR */
825 static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
826  const uint8_t *p_t, const uint8_t *p_tr)
827 {
828  p[0] = p_t[0] + p_tr[0] >> 1;
829  p[1] = p_t[1] + p_tr[1] >> 1;
830  p[2] = p_t[2] + p_tr[2] >> 1;
831  p[3] = p_t[3] + p_tr[3] >> 1;
832 }
833 
834 /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */
835 static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
836  const uint8_t *p_t, const uint8_t *p_tr)
837 {
838  p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
839  p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
840  p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
841  p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
842 }
843 
844 /* PRED_MODE_SELECT */
845 static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
846  const uint8_t *p_t, const uint8_t *p_tr)
847 {
848  int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) +
849  (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) +
850  (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) +
851  (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3]));
852  if (diff <= 0)
853  AV_COPY32(p, p_t);
854  else
855  AV_COPY32(p, p_l);
856 }
857 
858 /* PRED_MODE_ADD_SUBTRACT_FULL */
859 static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
860  const uint8_t *p_t, const uint8_t *p_tr)
861 {
862  p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
863  p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
864  p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
865  p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
866 }
867 
869 {
870  int d = a + b >> 1;
871  return av_clip_uint8(d + (d - c) / 2);
872 }
873 
874 /* PRED_MODE_ADD_SUBTRACT_HALF */
875 static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
876  const uint8_t *p_t, const uint8_t *p_tr)
877 {
878  p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]);
879  p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]);
880  p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]);
881  p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]);
882 }
883 
884 typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l,
885  const uint8_t *p_tl, const uint8_t *p_t,
886  const uint8_t *p_tr);
887 
888 static const inv_predict_func inverse_predict[14] = {
893 };
894 
895 static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
896 {
897  uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
898  uint8_t p[4];
899 
900  dec = GET_PIXEL(frame, x, y);
901  p_l = GET_PIXEL(frame, x - 1, y);
902  p_tl = GET_PIXEL(frame, x - 1, y - 1);
903  p_t = GET_PIXEL(frame, x, y - 1);
904  if (x == frame->width - 1)
905  p_tr = GET_PIXEL(frame, 0, y);
906  else
907  p_tr = GET_PIXEL(frame, x + 1, y - 1);
908 
909  inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
910 
911  dec[0] += p[0];
912  dec[1] += p[1];
913  dec[2] += p[2];
914  dec[3] += p[3];
915 }
916 
918 {
921  int x, y;
922 
923  for (y = 0; y < img->frame->height; y++) {
924  for (x = 0; x < img->frame->width; x++) {
925  int tx = x >> pimg->size_reduction;
926  int ty = y >> pimg->size_reduction;
927  enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2);
928 
929  if (x == 0) {
930  if (y == 0)
931  m = PRED_MODE_BLACK;
932  else
933  m = PRED_MODE_T;
934  } else if (y == 0)
935  m = PRED_MODE_L;
936 
937  if (m > 13) {
939  "invalid predictor mode: %d\n", m);
940  return AVERROR_INVALIDDATA;
941  }
942  inverse_prediction(img->frame, m, x, y);
943  }
944  }
945  return 0;
946 }
947 
949  uint8_t color)
950 {
951  return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5;
952 }
953 
955 {
956  ImageContext *img, *cimg;
957  int x, y, cx, cy;
958  uint8_t *p, *cp;
959 
960  img = &s->image[IMAGE_ROLE_ARGB];
961  cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM];
962 
963  for (y = 0; y < img->frame->height; y++) {
964  for (x = 0; x < img->frame->width; x++) {
965  cx = x >> cimg->size_reduction;
966  cy = y >> cimg->size_reduction;
967  cp = GET_PIXEL(cimg->frame, cx, cy);
968  p = GET_PIXEL(img->frame, x, y);
969 
970  p[1] += color_transform_delta(cp[3], p[2]);
971  p[3] += color_transform_delta(cp[2], p[2]) +
972  color_transform_delta(cp[1], p[1]);
973  }
974  }
975  return 0;
976 }
977 
979 {
980  int x, y;
982 
983  for (y = 0; y < img->frame->height; y++) {
984  for (x = 0; x < img->frame->width; x++) {
985  uint8_t *p = GET_PIXEL(img->frame, x, y);
986  p[1] += p[2];
987  p[3] += p[2];
988  }
989  }
990  return 0;
991 }
992 
994 {
995  ImageContext *img;
996  ImageContext *pal;
997  int i, x, y;
998  uint8_t *p;
999 
1000  img = &s->image[IMAGE_ROLE_ARGB];
1001  pal = &s->image[IMAGE_ROLE_COLOR_INDEXING];
1002 
1003  if (pal->size_reduction > 0) {
1004  GetBitContext gb_g;
1005  uint8_t *line;
1006  int pixel_bits = 8 >> pal->size_reduction;
1007 
1009  if (!line)
1010  return AVERROR(ENOMEM);
1011 
1012  for (y = 0; y < img->frame->height; y++) {
1013  p = GET_PIXEL(img->frame, 0, y);
1014  memcpy(line, p, img->frame->linesize[0]);
1015  init_get_bits(&gb_g, line, img->frame->linesize[0] * 8);
1016  skip_bits(&gb_g, 16);
1017  i = 0;
1018  for (x = 0; x < img->frame->width; x++) {
1019  p = GET_PIXEL(img->frame, x, y);
1020  p[2] = get_bits(&gb_g, pixel_bits);
1021  i++;
1022  if (i == 1 << pal->size_reduction) {
1023  skip_bits(&gb_g, 24);
1024  i = 0;
1025  }
1026  }
1027  }
1028  av_free(line);
1029  }
1030 
1031  // switch to local palette if it's worth initializing it
1032  if (img->frame->height * img->frame->width > 300) {
1033  uint8_t palette[256 * 4];
1034  const int size = pal->frame->width * 4;
1035  av_assert0(size <= 1024U);
1036  memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size); // copy palette
1037  // set extra entries to transparent black
1038  memset(palette + size, 0, 256 * 4 - size);
1039  for (y = 0; y < img->frame->height; y++) {
1040  for (x = 0; x < img->frame->width; x++) {
1041  p = GET_PIXEL(img->frame, x, y);
1042  i = p[2];
1043  AV_COPY32(p, &palette[i * 4]);
1044  }
1045  }
1046  } else {
1047  for (y = 0; y < img->frame->height; y++) {
1048  for (x = 0; x < img->frame->width; x++) {
1049  p = GET_PIXEL(img->frame, x, y);
1050  i = p[2];
1051  if (i >= pal->frame->width) {
1052  AV_WB32(p, 0x00000000);
1053  } else {
1054  const uint8_t *pi = GET_PIXEL(pal->frame, i, 0);
1055  AV_COPY32(p, pi);
1056  }
1057  }
1058  }
1059  }
1060 
1061  return 0;
1062 }
1063 
1064 static void update_canvas_size(AVCodecContext *avctx, int w, int h)
1065 {
1066  WebPContext *s = avctx->priv_data;
1067  if (s->width && s->width != w) {
1068  av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n",
1069  s->width, w);
1070  }
1071  s->width = w;
1072  if (s->height && s->height != h) {
1073  av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n",
1074  s->height, h);
1075  }
1076  s->height = h;
1077 }
1078 
1080  int *got_frame, uint8_t *data_start,
1081  unsigned int data_size, int is_alpha_chunk)
1082 {
1083  WebPContext *s = avctx->priv_data;
1084  int w, h, ret, i, used;
1085 
1086  if (!is_alpha_chunk) {
1087  s->lossless = 1;
1088  avctx->pix_fmt = AV_PIX_FMT_ARGB;
1089  }
1090 
1091  ret = init_get_bits8(&s->gb, data_start, data_size);
1092  if (ret < 0)
1093  return ret;
1094 
1095  if (!is_alpha_chunk) {
1096  if (get_bits(&s->gb, 8) != 0x2F) {
1097  av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n");
1098  return AVERROR_INVALIDDATA;
1099  }
1100 
1101  w = get_bits(&s->gb, 14) + 1;
1102  h = get_bits(&s->gb, 14) + 1;
1103 
1104  update_canvas_size(avctx, w, h);
1105 
1106  ret = ff_set_dimensions(avctx, s->width, s->height);
1107  if (ret < 0)
1108  return ret;
1109 
1110  s->has_alpha = get_bits1(&s->gb);
1111 
1112  if (get_bits(&s->gb, 3) != 0x0) {
1113  av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n");
1114  return AVERROR_INVALIDDATA;
1115  }
1116  } else {
1117  if (!s->width || !s->height)
1118  return AVERROR_BUG;
1119  w = s->width;
1120  h = s->height;
1121  }
1122 
1123  /* parse transformations */
1124  s->nb_transforms = 0;
1125  s->reduced_width = 0;
1126  used = 0;
1127  while (get_bits1(&s->gb)) {
1128  enum TransformType transform = get_bits(&s->gb, 2);
1129  if (used & (1 << transform)) {
1130  av_log(avctx, AV_LOG_ERROR, "Transform %d used more than once\n",
1131  transform);
1132  ret = AVERROR_INVALIDDATA;
1133  goto free_and_return;
1134  }
1135  used |= (1 << transform);
1136  s->transforms[s->nb_transforms++] = transform;
1137  switch (transform) {
1138  case PREDICTOR_TRANSFORM:
1139  ret = parse_transform_predictor(s);
1140  break;
1141  case COLOR_TRANSFORM:
1142  ret = parse_transform_color(s);
1143  break;
1146  break;
1147  }
1148  if (ret < 0)
1149  goto free_and_return;
1150  }
1151 
1152  /* decode primary image */
1153  s->image[IMAGE_ROLE_ARGB].frame = p;
1154  if (is_alpha_chunk)
1157  if (ret < 0)
1158  goto free_and_return;
1159 
1160  /* apply transformations */
1161  for (i = s->nb_transforms - 1; i >= 0; i--) {
1162  switch (s->transforms[i]) {
1163  case PREDICTOR_TRANSFORM:
1164  ret = apply_predictor_transform(s);
1165  break;
1166  case COLOR_TRANSFORM:
1167  ret = apply_color_transform(s);
1168  break;
1169  case SUBTRACT_GREEN:
1171  break;
1174  break;
1175  }
1176  if (ret < 0)
1177  goto free_and_return;
1178  }
1179 
1180  *got_frame = 1;
1182  p->key_frame = 1;
1183  ret = data_size;
1184 
1185 free_and_return:
1186  for (i = 0; i < IMAGE_ROLE_NB; i++)
1187  image_ctx_free(&s->image[i]);
1188 
1189  return ret;
1190 }
1191 
1193 {
1194  int x, y, ls;
1195  uint8_t *dec;
1196 
1197  ls = frame->linesize[3];
1198 
1199  /* filter first row using horizontal filter */
1200  dec = frame->data[3] + 1;
1201  for (x = 1; x < frame->width; x++, dec++)
1202  *dec += *(dec - 1);
1203 
1204  /* filter first column using vertical filter */
1205  dec = frame->data[3] + ls;
1206  for (y = 1; y < frame->height; y++, dec += ls)
1207  *dec += *(dec - ls);
1208 
1209  /* filter the rest using the specified filter */
1210  switch (m) {
1212  for (y = 1; y < frame->height; y++) {
1213  dec = frame->data[3] + y * ls + 1;
1214  for (x = 1; x < frame->width; x++, dec++)
1215  *dec += *(dec - 1);
1216  }
1217  break;
1218  case ALPHA_FILTER_VERTICAL:
1219  for (y = 1; y < frame->height; y++) {
1220  dec = frame->data[3] + y * ls + 1;
1221  for (x = 1; x < frame->width; x++, dec++)
1222  *dec += *(dec - ls);
1223  }
1224  break;
1225  case ALPHA_FILTER_GRADIENT:
1226  for (y = 1; y < frame->height; y++) {
1227  dec = frame->data[3] + y * ls + 1;
1228  for (x = 1; x < frame->width; x++, dec++)
1229  dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1230  }
1231  break;
1232  }
1233 }
1234 
1236  uint8_t *data_start,
1237  unsigned int data_size)
1238 {
1239  WebPContext *s = avctx->priv_data;
1240  int x, y, ret;
1241 
1243  GetByteContext gb;
1244 
1245  bytestream2_init(&gb, data_start, data_size);
1246  for (y = 0; y < s->height; y++)
1247  bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y,
1248  s->width);
1249  } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) {
1250  uint8_t *ap, *pp;
1251  int alpha_got_frame = 0;
1252 
1253  s->alpha_frame = av_frame_alloc();
1254  if (!s->alpha_frame)
1255  return AVERROR(ENOMEM);
1256 
1257  ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame,
1258  data_start, data_size, 1);
1259  if (ret < 0) {
1261  return ret;
1262  }
1263  if (!alpha_got_frame) {
1265  return AVERROR_INVALIDDATA;
1266  }
1267 
1268  /* copy green component of alpha image to alpha plane of primary image */
1269  for (y = 0; y < s->height; y++) {
1270  ap = GET_PIXEL(s->alpha_frame, 0, y) + 2;
1271  pp = p->data[3] + p->linesize[3] * y;
1272  for (x = 0; x < s->width; x++) {
1273  *pp = *ap;
1274  pp++;
1275  ap += 4;
1276  }
1277  }
1279  }
1280 
1281  /* apply alpha filtering */
1282  if (s->alpha_filter)
1284 
1285  return 0;
1286 }
1287 
1289  int *got_frame, uint8_t *data_start,
1290  unsigned int data_size)
1291 {
1292  WebPContext *s = avctx->priv_data;
1293  AVPacket pkt;
1294  int ret;
1295 
1296  if (!s->initialized) {
1297  ff_vp8_decode_init(avctx);
1298  s->initialized = 1;
1299  s->v.actually_webp = 1;
1300  }
1302  s->lossless = 0;
1303 
1304  if (data_size > INT_MAX) {
1305  av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n");
1306  return AVERROR_PATCHWELCOME;
1307  }
1308 
1309  av_init_packet(&pkt);
1310  pkt.data = data_start;
1311  pkt.size = data_size;
1312 
1313  ret = ff_vp8_decode_frame(avctx, p, got_frame, &pkt);
1314  if (ret < 0)
1315  return ret;
1316 
1317  if (!*got_frame)
1318  return AVERROR_INVALIDDATA;
1319 
1320  update_canvas_size(avctx, avctx->width, avctx->height);
1321 
1322  if (s->has_alpha) {
1323  ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data,
1324  s->alpha_data_size);
1325  if (ret < 0)
1326  return ret;
1327  }
1328  return ret;
1329 }
1330 
1331 static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1332  AVPacket *avpkt)
1333 {
1334  AVFrame * const p = data;
1335  WebPContext *s = avctx->priv_data;
1336  GetByteContext gb;
1337  int ret;
1338  uint32_t chunk_type, chunk_size;
1339  int vp8x_flags = 0;
1340 
1341  s->avctx = avctx;
1342  s->width = 0;
1343  s->height = 0;
1344  *got_frame = 0;
1345  s->has_alpha = 0;
1346  s->has_exif = 0;
1347  s->has_iccp = 0;
1348  bytestream2_init(&gb, avpkt->data, avpkt->size);
1349 
1350  if (bytestream2_get_bytes_left(&gb) < 12)
1351  return AVERROR_INVALIDDATA;
1352 
1353  if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
1354  av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
1355  return AVERROR_INVALIDDATA;
1356  }
1357 
1358  chunk_size = bytestream2_get_le32(&gb);
1359  if (bytestream2_get_bytes_left(&gb) < chunk_size)
1360  return AVERROR_INVALIDDATA;
1361 
1362  if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) {
1363  av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n");
1364  return AVERROR_INVALIDDATA;
1365  }
1366 
1367  while (bytestream2_get_bytes_left(&gb) > 8) {
1368  char chunk_str[5] = { 0 };
1369 
1370  chunk_type = bytestream2_get_le32(&gb);
1371  chunk_size = bytestream2_get_le32(&gb);
1372  if (chunk_size == UINT32_MAX)
1373  return AVERROR_INVALIDDATA;
1374  chunk_size += chunk_size & 1;
1375 
1376  if (bytestream2_get_bytes_left(&gb) < chunk_size) {
1377  /* we seem to be running out of data, but it could also be that the
1378  bitstream has trailing junk leading to bogus chunk_size. */
1379  break;
1380  }
1381 
1382  switch (chunk_type) {
1383  case MKTAG('V', 'P', '8', ' '):
1384  if (!*got_frame) {
1385  ret = vp8_lossy_decode_frame(avctx, p, got_frame,
1386  avpkt->data + bytestream2_tell(&gb),
1387  chunk_size);
1388  if (ret < 0)
1389  return ret;
1390  }
1391  bytestream2_skip(&gb, chunk_size);
1392  break;
1393  case MKTAG('V', 'P', '8', 'L'):
1394  if (!*got_frame) {
1395  ret = vp8_lossless_decode_frame(avctx, p, got_frame,
1396  avpkt->data + bytestream2_tell(&gb),
1397  chunk_size, 0);
1398  if (ret < 0)
1399  return ret;
1401  }
1402  bytestream2_skip(&gb, chunk_size);
1403  break;
1404  case MKTAG('V', 'P', '8', 'X'):
1405  if (s->width || s->height || *got_frame) {
1406  av_log(avctx, AV_LOG_ERROR, "Canvas dimensions are already set\n");
1407  return AVERROR_INVALIDDATA;
1408  }
1409  vp8x_flags = bytestream2_get_byte(&gb);
1410  bytestream2_skip(&gb, 3);
1411  s->width = bytestream2_get_le24(&gb) + 1;
1412  s->height = bytestream2_get_le24(&gb) + 1;
1413  ret = av_image_check_size(s->width, s->height, 0, avctx);
1414  if (ret < 0)
1415  return ret;
1416  break;
1417  case MKTAG('A', 'L', 'P', 'H'): {
1418  int alpha_header, filter_m, compression;
1419 
1420  if (!(vp8x_flags & VP8X_FLAG_ALPHA)) {
1421  av_log(avctx, AV_LOG_WARNING,
1422  "ALPHA chunk present, but alpha bit not set in the "
1423  "VP8X header\n");
1424  }
1425  if (chunk_size == 0) {
1426  av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n");
1427  return AVERROR_INVALIDDATA;
1428  }
1429  alpha_header = bytestream2_get_byte(&gb);
1430  s->alpha_data = avpkt->data + bytestream2_tell(&gb);
1431  s->alpha_data_size = chunk_size - 1;
1433 
1434  filter_m = (alpha_header >> 2) & 0x03;
1435  compression = alpha_header & 0x03;
1436 
1437  if (compression > ALPHA_COMPRESSION_VP8L) {
1438  av_log(avctx, AV_LOG_VERBOSE,
1439  "skipping unsupported ALPHA chunk\n");
1440  } else {
1441  s->has_alpha = 1;
1442  s->alpha_compression = compression;
1443  s->alpha_filter = filter_m;
1444  }
1445 
1446  break;
1447  }
1448  case MKTAG('E', 'X', 'I', 'F'): {
1449  int le, ifd_offset, exif_offset = bytestream2_tell(&gb);
1450  AVDictionary *exif_metadata = NULL;
1451  GetByteContext exif_gb;
1452 
1453  if (s->has_exif) {
1454  av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n");
1455  goto exif_end;
1456  }
1457  if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA))
1458  av_log(avctx, AV_LOG_WARNING,
1459  "EXIF chunk present, but Exif bit not set in the "
1460  "VP8X header\n");
1461 
1462  s->has_exif = 1;
1463  bytestream2_init(&exif_gb, avpkt->data + exif_offset,
1464  avpkt->size - exif_offset);
1465  if (ff_tdecode_header(&exif_gb, &le, &ifd_offset) < 0) {
1466  av_log(avctx, AV_LOG_ERROR, "invalid TIFF header "
1467  "in Exif data\n");
1468  goto exif_end;
1469  }
1470 
1471  bytestream2_seek(&exif_gb, ifd_offset, SEEK_SET);
1472  if (ff_exif_decode_ifd(avctx, &exif_gb, le, 0, &exif_metadata) < 0) {
1473  av_log(avctx, AV_LOG_ERROR, "error decoding Exif data\n");
1474  goto exif_end;
1475  }
1476 
1477  av_dict_copy(&((AVFrame *) data)->metadata, exif_metadata, 0);
1478 
1479 exif_end:
1480  av_dict_free(&exif_metadata);
1481  bytestream2_skip(&gb, chunk_size);
1482  break;
1483  }
1484  case MKTAG('I', 'C', 'C', 'P'): {
1485  AVFrameSideData *sd;
1486 
1487  if (s->has_iccp) {
1488  av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra ICCP chunk\n");
1489  bytestream2_skip(&gb, chunk_size);
1490  break;
1491  }
1492  if (!(vp8x_flags & VP8X_FLAG_ICC))
1493  av_log(avctx, AV_LOG_WARNING,
1494  "ICCP chunk present, but ICC Profile bit not set in the "
1495  "VP8X header\n");
1496 
1497  s->has_iccp = 1;
1499  if (!sd)
1500  return AVERROR(ENOMEM);
1501 
1502  bytestream2_get_buffer(&gb, sd->data, chunk_size);
1503  break;
1504  }
1505  case MKTAG('A', 'N', 'I', 'M'):
1506  case MKTAG('A', 'N', 'M', 'F'):
1507  case MKTAG('X', 'M', 'P', ' '):
1508  AV_WL32(chunk_str, chunk_type);
1509  av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n",
1510  chunk_str);
1511  bytestream2_skip(&gb, chunk_size);
1512  break;
1513  default:
1514  AV_WL32(chunk_str, chunk_type);
1515  av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
1516  chunk_str);
1517  bytestream2_skip(&gb, chunk_size);
1518  break;
1519  }
1520  }
1521 
1522  if (!*got_frame) {
1523  av_log(avctx, AV_LOG_ERROR, "image data not found\n");
1524  return AVERROR_INVALIDDATA;
1525  }
1526 
1527  return avpkt->size;
1528 }
1529 
1531 {
1532  WebPContext *s = avctx->priv_data;
1533 
1534  if (s->initialized)
1535  return ff_vp8_decode_free(avctx);
1536 
1537  return 0;
1538 }
1539 
1541  .name = "webp",
1542  .long_name = NULL_IF_CONFIG_SMALL("WebP image"),
1543  .type = AVMEDIA_TYPE_VIDEO,
1544  .id = AV_CODEC_ID_WEBP,
1545  .priv_data_size = sizeof(WebPContext),
1547  .close = webp_decode_close,
1548  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1549 };
int nb_huffman_groups
Definition: webp.c:182
#define extra_bits(eb)
Definition: intrax8.c:125
static int read_huffman_code_normal(WebPContext *s, HuffReader *hc, int alphabet_size)
Definition: webp.c:323
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
static const struct @74 transforms[18]
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
int ff_exif_decode_ifd(void *logctx, GetByteContext *gbytes, int le, int depth, AVDictionary **metadata)
Definition: exif.c:122
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
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:835
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:200
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:106
int initialized
Definition: webp.c:193
static int parse_transform_color_indexing(WebPContext *s)
Definition: webp.c:490
static HuffReader * get_huffman_group(WebPContext *s, ImageContext *img, int x, int y)
Definition: webp.c:525
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:364
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:884
#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:845
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
Definition: webp.c:235
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
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
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static const inv_predict_func inverse_predict[14]
Definition: webp.c:888
static AVPacket pkt
static int apply_color_indexing_transform(WebPContext *s)
Definition: webp.c:993
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:868
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:1235
#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:542
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:825
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
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
int nb_symbols
Definition: webp.c:173
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
Multithreading support functions.
int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vp8.c:2810
#define FF_CODEC_PROPERTY_LOSSLESS
Definition: avcodec.h:2207
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:859
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:91
Structure to hold side data for an AVFrame.
Definition: frame.h:220
#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:363
#define VP8X_FLAG_ICC
Definition: webp.c:58
bitstream reader API header.
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
static int decode_entropy_image(WebPContext *s)
Definition: webp.c:424
#define max(a, b)
Definition: cuda_runtime.h:33
ptrdiff_t size
Definition: opengl_enc.c:100
#define av_log(a,...)
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
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:757
#define U(x)
Definition: vp56_arith.h:37
static int apply_color_transform(WebPContext *s)
Definition: webp.c:954
#define VP8X_FLAG_ALPHA
Definition: webp.c:57
int width
Definition: frame.h:372
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: webp.c:1331
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:795
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
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:115
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:267
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
GLsizei GLsizei * length
Definition: opengl_enc.c:114
const char * name
Name of the codec implementation.
Definition: codec.h:197
uint8_t bits
Definition: vp3data.h:141
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:771
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:1288
#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:1079
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
static float distance(float x, float y, int band)
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in AVCodec caps_internal and use ff_thread_get_buffer() to allocate frames.The frames must then be freed with ff_thread_release_buffer().Otherwise decode directly into the user-supplied frames.Call ff_thread_report_progress() after some part of the current picture has decoded.A good place to put this is where draw_horiz_band() is called-add this if it isn't called anywhere
static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths, int alphabet_size)
Definition: webp.c:246
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:317
#define b
Definition: input.c:41
#define PARSE_BLOCK_SIZE(w, h)
Definition: webp.c:418
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
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:764
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int has_iccp
Definition: webp.c:200
#define xi(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h2645.c:404
#define width
int width
picture width / height.
Definition: avcodec.h:704
uint8_t w
Definition: llviddspenc.c:39
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
uint32_t * color_cache
Definition: webp.c:181
AlphaFilter
Definition: webp.c:103
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#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
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
int has_alpha
Definition: webp.c:194
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:192
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:387
static const int8_t transform[32][32]
Definition: hevcdsp.c:27
int alpha_data_size
Definition: webp.c:198
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
main external API structure.
Definition: avcodec.h:531
uint8_t * data
Definition: frame.h:222
#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:948
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:726
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
AVCodec ff_webp_decoder
Definition: webp.c:1540
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:778
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:750
#define MAX_HUFFMAN_CODE_LENGTH
Definition: webp.c:68
int size_reduction
Definition: webp.c:184
int actually_webp
Definition: vp8.h:152
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:337
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
static void read_huffman_code_simple(WebPContext *s, HuffReader *hc)
Definition: webp.c:308
av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
Definition: vp8.c:2894
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
Definition: vp8.c:2824
static int parse_transform_predictor(WebPContext *s)
Definition: webp.c:458
#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
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:33
static av_cold int webp_decode_close(AVCodecContext *avctx)
Definition: webp.c:1530
int nb_transforms
Definition: webp.c:205
common internal api header.
static int apply_subtract_green_transform(WebPContext *s)
Definition: webp.c:978
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:815
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:2206
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:558
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:917
#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:474
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
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:805
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, int w, int h)
Definition: webp.c:548
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:875
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:372
static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m)
Definition: webp.c:1192
#define av_always_inline
Definition: attributes.h:45
static const uint8_t block_bits[]
Definition: imm4.c:103
#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:895
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
#define MKTAG(a, b, c, d)
Definition: common.h:405
This structure stores compressed data.
Definition: packet.h:340
AVFrame * alpha_frame
Definition: webp.c:191
static void update_canvas_size(AVCodecContext *avctx, int w, int h)
Definition: webp.c:1064
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:431
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:785
#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
int i
Definition: input.c:407
#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)
Definition: mem.c:190
#define INIT_VLC_OUTPUT_LE
Definition: vlc.h:93