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