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