2 * WebP (.webp) image decoder
3 * Copyright (c) 2013 Aneesh Dogra <aneesh@sugarlabs.org>
4 * Copyright (c) 2013 Justin Ruggles <justin.ruggles@gmail.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * @author Aneesh Dogra <aneesh@sugarlabs.org>
28 * Container and Lossy decoding
30 * @author Justin Ruggles <justin.ruggles@gmail.com>
32 * Compressed alpha for lossy
34 * @author James Almer <jamrial@gmail.com>
43 #include "libavutil/imgutils.h"
45 #define BITSTREAM_READER_LE
47 #include "bytestream.h"
54 #define VP8X_FLAG_ANIMATION 0x02
55 #define VP8X_FLAG_XMP_METADATA 0x04
56 #define VP8X_FLAG_EXIF_METADATA 0x08
57 #define VP8X_FLAG_ALPHA 0x10
58 #define VP8X_FLAG_ICC 0x20
60 #define MAX_PALETTE_SIZE 256
61 #define MAX_CACHE_BITS 11
62 #define NUM_CODE_LENGTH_CODES 19
63 #define HUFFMAN_CODES_PER_META_CODE 5
64 #define NUM_LITERAL_CODES 256
65 #define NUM_LENGTH_CODES 24
66 #define NUM_DISTANCE_CODES 40
67 #define NUM_SHORT_DISTANCES 120
68 #define MAX_HUFFMAN_CODE_LENGTH 15
70 static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE] = {
71 NUM_LITERAL_CODES + NUM_LENGTH_CODES,
72 NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
76 static const uint8_t code_length_code_order[NUM_CODE_LENGTH_CODES] = {
77 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
80 static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2] = {
81 { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
82 { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
83 { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
84 { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
85 { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
86 { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
87 { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
88 { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
89 { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
90 { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
91 { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
92 { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
93 { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
94 { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
95 { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
98 enum AlphaCompression {
99 ALPHA_COMPRESSION_NONE,
100 ALPHA_COMPRESSION_VP8L,
105 ALPHA_FILTER_HORIZONTAL,
106 ALPHA_FILTER_VERTICAL,
107 ALPHA_FILTER_GRADIENT,
111 PREDICTOR_TRANSFORM = 0,
114 COLOR_INDEXING_TRANSFORM = 3,
117 enum PredictionMode {
123 PRED_MODE_AVG_T_AVG_L_TR,
128 PRED_MODE_AVG_AVG_L_TL_AVG_T_TR,
130 PRED_MODE_ADD_SUBTRACT_FULL,
131 PRED_MODE_ADD_SUBTRACT_HALF,
142 /* The structure of WebP lossless is an optional series of transformation data,
143 * followed by the primary image. The primary image also optionally contains
144 * an entropy group mapping if there are multiple entropy groups. There is a
145 * basic image type called an "entropy coded image" that is used for all of
146 * these. The type of each entropy coded image is referred to by the
147 * specification as its role. */
149 /* Primary Image: Stores the actual pixels of the image. */
152 /* Entropy Image: Defines which Huffman group to use for different areas of
153 * the primary image. */
156 /* Predictors: Defines which predictor type to use for different areas of
157 * the primary image. */
158 IMAGE_ROLE_PREDICTOR,
160 /* Color Transform Data: Defines the color transformation for different
161 * areas of the primary image. */
162 IMAGE_ROLE_COLOR_TRANSFORM,
164 /* Color Index: Stored as an image of height == 1. */
165 IMAGE_ROLE_COLOR_INDEXING,
170 typedef struct HuffReader {
171 VLC vlc; /* Huffman decoder context */
172 int simple; /* whether to use simple mode */
173 int nb_symbols; /* number of coded symbols */
174 uint16_t simple_symbols[2]; /* symbols for simple mode */
177 typedef struct ImageContext {
178 enum ImageRole role; /* role of this image */
179 AVFrame *frame; /* AVFrame for data */
180 int color_cache_bits; /* color cache size, log2 */
181 uint32_t *color_cache; /* color cache data */
182 int nb_huffman_groups; /* number of huffman groups */
183 HuffReader *huffman_groups; /* reader for each huffman group */
184 int size_reduction; /* relative size compared to primary image, log2 */
185 int is_alpha_primary;
188 typedef struct WebPContext {
189 VP8Context v; /* VP8 Context used for lossy decoding */
190 GetBitContext gb; /* bitstream reader for main image chunk */
191 AVFrame *alpha_frame; /* AVFrame for alpha data decompressed from VP8L */
192 AVCodecContext *avctx; /* parent AVCodecContext */
193 int initialized; /* set once the VP8 context is initialized */
194 int has_alpha; /* has a separate alpha chunk */
195 enum AlphaCompression alpha_compression; /* compression type for alpha chunk */
196 enum AlphaFilter alpha_filter; /* filtering method for alpha chunk */
197 uint8_t *alpha_data; /* alpha chunk data */
198 int alpha_data_size; /* alpha chunk data size */
199 int has_exif; /* set after an EXIF chunk has been processed */
200 int has_iccp; /* set after an ICCP chunk has been processed */
201 int width; /* image width */
202 int height; /* image height */
203 int lossless; /* indicates lossless or lossy */
205 int nb_transforms; /* number of transforms */
206 enum TransformType transforms[4]; /* transformations used in the image, in order */
207 int reduced_width; /* reduced width for index image, if applicable */
208 int nb_huffman_groups; /* number of huffman groups in the primary image */
209 ImageContext image[IMAGE_ROLE_NB]; /* image context for each role */
212 #define GET_PIXEL(frame, x, y) \
213 ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
215 #define GET_PIXEL_COMP(frame, x, y, c) \
216 (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
218 static void image_ctx_free(ImageContext *img)
222 av_free(img->color_cache);
223 if (img->role != IMAGE_ROLE_ARGB && !img->is_alpha_primary)
224 av_frame_free(&img->frame);
225 if (img->huffman_groups) {
226 for (i = 0; i < img->nb_huffman_groups; i++) {
227 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++)
228 ff_free_vlc(&img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE + j].vlc);
230 av_free(img->huffman_groups);
232 memset(img, 0, sizeof(*img));
236 /* Differs from get_vlc2() in the following ways:
237 * - codes are bit-reversed
238 * - assumes 8-bit table to make reversal simpler
239 * - assumes max depth of 2 since the max code length for WebP is 15
241 static av_always_inline int webp_get_vlc(GetBitContext *gb, VLC_TYPE (*table)[2])
248 UPDATE_CACHE(re, gb);
250 index = SHOW_UBITS(re, gb, 8);
251 index = ff_reverse[index];
252 code = table[index][0];
256 LAST_SKIP_BITS(re, gb, 8);
257 UPDATE_CACHE(re, gb);
261 index = SHOW_UBITS(re, gb, nb_bits);
262 index = (ff_reverse[index] >> (8 - nb_bits)) + code;
263 code = table[index][0];
266 SKIP_BITS(re, gb, n);
268 CLOSE_READER(re, gb);
273 static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
276 if (r->nb_symbols == 1)
277 return r->simple_symbols[0];
279 return r->simple_symbols[get_bits1(gb)];
281 return webp_get_vlc(gb, r->vlc.table);
284 static int huff_reader_build_canonical(HuffReader *r, int *code_lengths,
287 int len = 0, sym, code = 0, ret;
288 int max_code_length = 0;
291 /* special-case 1 symbol since the vlc reader cannot handle it */
292 for (sym = 0; sym < alphabet_size; sym++) {
293 if (code_lengths[sym] > 0) {
302 r->simple_symbols[0] = code;
307 for (sym = 0; sym < alphabet_size; sym++)
308 max_code_length = FFMAX(max_code_length, code_lengths[sym]);
310 if (max_code_length == 0 || max_code_length > MAX_HUFFMAN_CODE_LENGTH)
311 return AVERROR(EINVAL);
313 codes = av_malloc_array(alphabet_size, sizeof(*codes));
315 return AVERROR(ENOMEM);
319 for (len = 1; len <= max_code_length; len++) {
320 for (sym = 0; sym < alphabet_size; sym++) {
321 if (code_lengths[sym] != len)
328 if (!r->nb_symbols) {
330 return AVERROR_INVALIDDATA;
333 ret = init_vlc(&r->vlc, 8, alphabet_size,
334 code_lengths, sizeof(*code_lengths), sizeof(*code_lengths),
335 codes, sizeof(*codes), sizeof(*codes), 0);
346 static void read_huffman_code_simple(WebPContext *s, HuffReader *hc)
348 hc->nb_symbols = get_bits1(&s->gb) + 1;
350 if (get_bits1(&s->gb))
351 hc->simple_symbols[0] = get_bits(&s->gb, 8);
353 hc->simple_symbols[0] = get_bits1(&s->gb);
355 if (hc->nb_symbols == 2)
356 hc->simple_symbols[1] = get_bits(&s->gb, 8);
361 static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
364 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
365 int *code_lengths = NULL;
366 int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
367 int i, symbol, max_symbol, prev_code_len, ret;
368 int num_codes = 4 + get_bits(&s->gb, 4);
370 if (num_codes > NUM_CODE_LENGTH_CODES)
371 return AVERROR_INVALIDDATA;
373 for (i = 0; i < num_codes; i++)
374 code_length_code_lengths[code_length_code_order[i]] = get_bits(&s->gb, 3);
376 ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths,
377 NUM_CODE_LENGTH_CODES);
381 code_lengths = av_mallocz_array(alphabet_size, sizeof(*code_lengths));
383 ret = AVERROR(ENOMEM);
387 if (get_bits1(&s->gb)) {
388 int bits = 2 + 2 * get_bits(&s->gb, 3);
389 max_symbol = 2 + get_bits(&s->gb, bits);
390 if (max_symbol > alphabet_size) {
391 av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n",
392 max_symbol, alphabet_size);
393 ret = AVERROR_INVALIDDATA;
397 max_symbol = alphabet_size;
402 while (symbol < alphabet_size) {
407 code_len = huff_reader_get_symbol(&code_len_hc, &s->gb);
409 /* Code length code [0..15] indicates literal code lengths. */
410 code_lengths[symbol++] = code_len;
412 prev_code_len = code_len;
414 int repeat = 0, length = 0;
417 /* Code 16 repeats the previous non-zero value [3..6] times,
418 * i.e., 3 + ReadBits(2) times. If code 16 is used before a
419 * non-zero value has been emitted, a value of 8 is repeated. */
420 repeat = 3 + get_bits(&s->gb, 2);
421 length = prev_code_len;
424 /* Code 17 emits a streak of zeros [3..10], i.e.,
425 * 3 + ReadBits(3) times. */
426 repeat = 3 + get_bits(&s->gb, 3);
429 /* Code 18 emits a streak of zeros of length [11..138], i.e.,
430 * 11 + ReadBits(7) times. */
431 repeat = 11 + get_bits(&s->gb, 7);
434 if (symbol + repeat > alphabet_size) {
435 av_log(s->avctx, AV_LOG_ERROR,
436 "invalid symbol %d + repeat %d > alphabet size %d\n",
437 symbol, repeat, alphabet_size);
438 ret = AVERROR_INVALIDDATA;
442 code_lengths[symbol++] = length;
446 ret = huff_reader_build_canonical(hc, code_lengths, alphabet_size);
449 ff_free_vlc(&code_len_hc.vlc);
450 av_free(code_lengths);
454 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
457 #define PARSE_BLOCK_SIZE(w, h) do { \
458 block_bits = get_bits(&s->gb, 3) + 2; \
459 blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
460 blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
463 static int decode_entropy_image(WebPContext *s)
466 int ret, block_bits, width, blocks_w, blocks_h, x, y, max;
469 if (s->reduced_width > 0)
470 width = s->reduced_width;
472 PARSE_BLOCK_SIZE(width, s->height);
474 ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h);
478 img = &s->image[IMAGE_ROLE_ENTROPY];
479 img->size_reduction = block_bits;
481 /* the number of huffman groups is determined by the maximum group number
482 * coded in the entropy image */
484 for (y = 0; y < img->frame->height; y++) {
485 for (x = 0; x < img->frame->width; x++) {
486 int p0 = GET_PIXEL_COMP(img->frame, x, y, 1);
487 int p1 = GET_PIXEL_COMP(img->frame, x, y, 2);
488 int p = p0 << 8 | p1;
492 s->nb_huffman_groups = max + 1;
497 static int parse_transform_predictor(WebPContext *s)
499 int block_bits, blocks_w, blocks_h, ret;
501 PARSE_BLOCK_SIZE(s->width, s->height);
503 ret = decode_entropy_coded_image(s, IMAGE_ROLE_PREDICTOR, blocks_w,
508 s->image[IMAGE_ROLE_PREDICTOR].size_reduction = block_bits;
513 static int parse_transform_color(WebPContext *s)
515 int block_bits, blocks_w, blocks_h, ret;
517 PARSE_BLOCK_SIZE(s->width, s->height);
519 ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_TRANSFORM, blocks_w,
524 s->image[IMAGE_ROLE_COLOR_TRANSFORM].size_reduction = block_bits;
529 static int parse_transform_color_indexing(WebPContext *s)
532 int width_bits, index_size, ret, x;
535 index_size = get_bits(&s->gb, 8) + 1;
539 else if (index_size <= 4)
541 else if (index_size <= 16)
546 ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_INDEXING,
551 img = &s->image[IMAGE_ROLE_COLOR_INDEXING];
552 img->size_reduction = width_bits;
554 s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits;
556 /* color index values are delta-coded */
557 ct = img->frame->data[0] + 4;
558 for (x = 4; x < img->frame->width * 4; x++, ct++)
564 static HuffReader *get_huffman_group(WebPContext *s, ImageContext *img,
567 ImageContext *gimg = &s->image[IMAGE_ROLE_ENTROPY];
570 if (gimg->size_reduction > 0) {
571 int group_x = x >> gimg->size_reduction;
572 int group_y = y >> gimg->size_reduction;
573 int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1);
574 int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2);
575 group = g0 << 8 | g1;
578 return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE];
581 static av_always_inline void color_cache_put(ImageContext *img, uint32_t c)
583 uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits);
584 img->color_cache[cache_idx] = c;
587 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
592 int i, j, ret, x, y, width;
594 img = &s->image[role];
598 img->frame = av_frame_alloc();
600 return AVERROR(ENOMEM);
603 img->frame->format = AV_PIX_FMT_ARGB;
604 img->frame->width = w;
605 img->frame->height = h;
607 if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) {
608 ThreadFrame pt = { .f = img->frame };
609 ret = ff_thread_get_buffer(s->avctx, &pt, 0);
611 ret = av_frame_get_buffer(img->frame, 1);
615 if (get_bits1(&s->gb)) {
616 img->color_cache_bits = get_bits(&s->gb, 4);
617 if (img->color_cache_bits < 1 || img->color_cache_bits > 11) {
618 av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n",
619 img->color_cache_bits);
620 return AVERROR_INVALIDDATA;
622 img->color_cache = av_mallocz_array(1 << img->color_cache_bits,
623 sizeof(*img->color_cache));
624 if (!img->color_cache)
625 return AVERROR(ENOMEM);
627 img->color_cache_bits = 0;
630 img->nb_huffman_groups = 1;
631 if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) {
632 ret = decode_entropy_image(s);
635 img->nb_huffman_groups = s->nb_huffman_groups;
637 img->huffman_groups = av_mallocz_array(img->nb_huffman_groups *
638 HUFFMAN_CODES_PER_META_CODE,
639 sizeof(*img->huffman_groups));
640 if (!img->huffman_groups)
641 return AVERROR(ENOMEM);
643 for (i = 0; i < img->nb_huffman_groups; i++) {
644 hg = &img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE];
645 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) {
646 int alphabet_size = alphabet_sizes[j];
647 if (!j && img->color_cache_bits > 0)
648 alphabet_size += 1 << img->color_cache_bits;
650 if (get_bits1(&s->gb)) {
651 read_huffman_code_simple(s, &hg[j]);
653 ret = read_huffman_code_normal(s, &hg[j], alphabet_size);
660 width = img->frame->width;
661 if (role == IMAGE_ROLE_ARGB && s->reduced_width > 0)
662 width = s->reduced_width;
665 while (y < img->frame->height) {
668 hg = get_huffman_group(s, img, x, y);
669 v = huff_reader_get_symbol(&hg[HUFF_IDX_GREEN], &s->gb);
670 if (v < NUM_LITERAL_CODES) {
671 /* literal pixel values */
672 uint8_t *p = GET_PIXEL(img->frame, x, y);
674 p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb);
675 p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb);
676 p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb);
677 if (img->color_cache_bits)
678 color_cache_put(img, AV_RB32(p));
684 } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
685 /* LZ77 backwards mapping */
686 int prefix_code, length, distance, ref_x, ref_y;
688 /* parse length and distance */
689 prefix_code = v - NUM_LITERAL_CODES;
690 if (prefix_code < 4) {
691 length = prefix_code + 1;
693 int extra_bits = (prefix_code - 2) >> 1;
694 int offset = 2 + (prefix_code & 1) << extra_bits;
695 length = offset + get_bits(&s->gb, extra_bits) + 1;
697 prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb);
698 if (prefix_code > 39U) {
699 av_log(s->avctx, AV_LOG_ERROR,
700 "distance prefix code too large: %d\n", prefix_code);
701 return AVERROR_INVALIDDATA;
703 if (prefix_code < 4) {
704 distance = prefix_code + 1;
706 int extra_bits = prefix_code - 2 >> 1;
707 int offset = 2 + (prefix_code & 1) << extra_bits;
708 distance = offset + get_bits(&s->gb, extra_bits) + 1;
711 /* find reference location */
712 if (distance <= NUM_SHORT_DISTANCES) {
713 int xi = lz77_distance_offsets[distance - 1][0];
714 int yi = lz77_distance_offsets[distance - 1][1];
715 distance = FFMAX(1, xi + yi * width);
717 distance -= NUM_SHORT_DISTANCES;
728 while (distance >= width) {
733 ref_x = width - distance;
736 ref_x = FFMAX(0, ref_x);
737 ref_y = FFMAX(0, ref_y);
740 * source and dest regions can overlap and wrap lines, so just
742 for (i = 0; i < length; i++) {
743 uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y);
744 uint8_t *p = GET_PIXEL(img->frame, x, y);
747 if (img->color_cache_bits)
748 color_cache_put(img, AV_RB32(p));
755 if (ref_x == width) {
759 if (y == img->frame->height || ref_y == img->frame->height)
763 /* read from color cache */
764 uint8_t *p = GET_PIXEL(img->frame, x, y);
765 int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES);
767 if (!img->color_cache_bits) {
768 av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n");
769 return AVERROR_INVALIDDATA;
771 if (cache_idx >= 1 << img->color_cache_bits) {
772 av_log(s->avctx, AV_LOG_ERROR,
773 "color cache index out-of-bounds\n");
774 return AVERROR_INVALIDDATA;
776 AV_WB32(p, img->color_cache[cache_idx]);
788 /* PRED_MODE_BLACK */
789 static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
790 const uint8_t *p_t, const uint8_t *p_tr)
792 AV_WB32(p, 0xFF000000);
796 static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
797 const uint8_t *p_t, const uint8_t *p_tr)
803 static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
804 const uint8_t *p_t, const uint8_t *p_tr)
810 static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
811 const uint8_t *p_t, const uint8_t *p_tr)
817 static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
818 const uint8_t *p_t, const uint8_t *p_tr)
823 /* PRED_MODE_AVG_T_AVG_L_TR */
824 static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
825 const uint8_t *p_t, const uint8_t *p_tr)
827 p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
828 p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
829 p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
830 p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
833 /* PRED_MODE_AVG_L_TL */
834 static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
835 const uint8_t *p_t, const uint8_t *p_tr)
837 p[0] = p_l[0] + p_tl[0] >> 1;
838 p[1] = p_l[1] + p_tl[1] >> 1;
839 p[2] = p_l[2] + p_tl[2] >> 1;
840 p[3] = p_l[3] + p_tl[3] >> 1;
843 /* PRED_MODE_AVG_L_T */
844 static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
845 const uint8_t *p_t, const uint8_t *p_tr)
847 p[0] = p_l[0] + p_t[0] >> 1;
848 p[1] = p_l[1] + p_t[1] >> 1;
849 p[2] = p_l[2] + p_t[2] >> 1;
850 p[3] = p_l[3] + p_t[3] >> 1;
853 /* PRED_MODE_AVG_TL_T */
854 static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
855 const uint8_t *p_t, const uint8_t *p_tr)
857 p[0] = p_tl[0] + p_t[0] >> 1;
858 p[1] = p_tl[1] + p_t[1] >> 1;
859 p[2] = p_tl[2] + p_t[2] >> 1;
860 p[3] = p_tl[3] + p_t[3] >> 1;
863 /* PRED_MODE_AVG_T_TR */
864 static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
865 const uint8_t *p_t, const uint8_t *p_tr)
867 p[0] = p_t[0] + p_tr[0] >> 1;
868 p[1] = p_t[1] + p_tr[1] >> 1;
869 p[2] = p_t[2] + p_tr[2] >> 1;
870 p[3] = p_t[3] + p_tr[3] >> 1;
873 /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */
874 static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
875 const uint8_t *p_t, const uint8_t *p_tr)
877 p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
878 p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
879 p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
880 p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
883 /* PRED_MODE_SELECT */
884 static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
885 const uint8_t *p_t, const uint8_t *p_tr)
887 int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) +
888 (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) +
889 (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) +
890 (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3]));
897 /* PRED_MODE_ADD_SUBTRACT_FULL */
898 static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
899 const uint8_t *p_t, const uint8_t *p_tr)
901 p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
902 p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
903 p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
904 p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
907 static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c)
910 return av_clip_uint8(d + (d - c) / 2);
913 /* PRED_MODE_ADD_SUBTRACT_HALF */
914 static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
915 const uint8_t *p_t, const uint8_t *p_tr)
917 p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]);
918 p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]);
919 p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]);
920 p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]);
923 typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l,
924 const uint8_t *p_tl, const uint8_t *p_t,
925 const uint8_t *p_tr);
927 static const inv_predict_func inverse_predict[14] = {
928 inv_predict_0, inv_predict_1, inv_predict_2, inv_predict_3,
929 inv_predict_4, inv_predict_5, inv_predict_6, inv_predict_7,
930 inv_predict_8, inv_predict_9, inv_predict_10, inv_predict_11,
931 inv_predict_12, inv_predict_13,
934 static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
936 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
939 dec = GET_PIXEL(frame, x, y);
940 p_l = GET_PIXEL(frame, x - 1, y);
941 p_tl = GET_PIXEL(frame, x - 1, y - 1);
942 p_t = GET_PIXEL(frame, x, y - 1);
943 if (x == frame->width - 1)
944 p_tr = GET_PIXEL(frame, 0, y);
946 p_tr = GET_PIXEL(frame, x + 1, y - 1);
948 inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
956 static int apply_predictor_transform(WebPContext *s)
958 ImageContext *img = &s->image[IMAGE_ROLE_ARGB];
959 ImageContext *pimg = &s->image[IMAGE_ROLE_PREDICTOR];
962 for (y = 0; y < img->frame->height; y++) {
963 for (x = 0; x < img->frame->width; x++) {
964 int tx = x >> pimg->size_reduction;
965 int ty = y >> pimg->size_reduction;
966 enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2);
977 av_log(s->avctx, AV_LOG_ERROR,
978 "invalid predictor mode: %d\n", m);
979 return AVERROR_INVALIDDATA;
981 inverse_prediction(img->frame, m, x, y);
987 static av_always_inline uint8_t color_transform_delta(uint8_t color_pred,
990 return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5;
993 static int apply_color_transform(WebPContext *s)
995 ImageContext *img, *cimg;
999 img = &s->image[IMAGE_ROLE_ARGB];
1000 cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM];
1002 for (y = 0; y < img->frame->height; y++) {
1003 for (x = 0; x < img->frame->width; x++) {
1004 cx = x >> cimg->size_reduction;
1005 cy = y >> cimg->size_reduction;
1006 cp = GET_PIXEL(cimg->frame, cx, cy);
1007 p = GET_PIXEL(img->frame, x, y);
1009 p[1] += color_transform_delta(cp[3], p[2]);
1010 p[3] += color_transform_delta(cp[2], p[2]) +
1011 color_transform_delta(cp[1], p[1]);
1017 static int apply_subtract_green_transform(WebPContext *s)
1020 ImageContext *img = &s->image[IMAGE_ROLE_ARGB];
1022 for (y = 0; y < img->frame->height; y++) {
1023 for (x = 0; x < img->frame->width; x++) {
1024 uint8_t *p = GET_PIXEL(img->frame, x, y);
1032 static int apply_color_indexing_transform(WebPContext *s)
1039 img = &s->image[IMAGE_ROLE_ARGB];
1040 pal = &s->image[IMAGE_ROLE_COLOR_INDEXING];
1042 if (pal->size_reduction > 0) {
1045 int pixel_bits = 8 >> pal->size_reduction;
1047 line = av_malloc(img->frame->linesize[0] + AV_INPUT_BUFFER_PADDING_SIZE);
1049 return AVERROR(ENOMEM);
1051 for (y = 0; y < img->frame->height; y++) {
1052 p = GET_PIXEL(img->frame, 0, y);
1053 memcpy(line, p, img->frame->linesize[0]);
1054 init_get_bits(&gb_g, line, img->frame->linesize[0] * 8);
1055 skip_bits(&gb_g, 16);
1057 for (x = 0; x < img->frame->width; x++) {
1058 p = GET_PIXEL(img->frame, x, y);
1059 p[2] = get_bits(&gb_g, pixel_bits);
1061 if (i == 1 << pal->size_reduction) {
1062 skip_bits(&gb_g, 24);
1070 // switch to local palette if it's worth initializing it
1071 if (img->frame->height * img->frame->width > 300) {
1072 uint8_t palette[256 * 4];
1073 const int size = pal->frame->width * 4;
1074 av_assert0(size <= 1024U);
1075 memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size); // copy palette
1076 // set extra entries to transparent black
1077 memset(palette + size, 0, 256 * 4 - size);
1078 for (y = 0; y < img->frame->height; y++) {
1079 for (x = 0; x < img->frame->width; x++) {
1080 p = GET_PIXEL(img->frame, x, y);
1082 AV_COPY32(p, &palette[i * 4]);
1086 for (y = 0; y < img->frame->height; y++) {
1087 for (x = 0; x < img->frame->width; x++) {
1088 p = GET_PIXEL(img->frame, x, y);
1090 if (i >= pal->frame->width) {
1091 AV_WB32(p, 0x00000000);
1093 const uint8_t *pi = GET_PIXEL(pal->frame, i, 0);
1103 static void update_canvas_size(AVCodecContext *avctx, int w, int h)
1105 WebPContext *s = avctx->priv_data;
1106 if (s->width && s->width != w) {
1107 av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n",
1111 if (s->height && s->height != h) {
1112 av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n",
1118 static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p,
1119 int *got_frame, uint8_t *data_start,
1120 unsigned int data_size, int is_alpha_chunk)
1122 WebPContext *s = avctx->priv_data;
1123 int w, h, ret, i, used;
1125 if (!is_alpha_chunk) {
1127 avctx->pix_fmt = AV_PIX_FMT_ARGB;
1130 ret = init_get_bits8(&s->gb, data_start, data_size);
1134 if (!is_alpha_chunk) {
1135 if (get_bits(&s->gb, 8) != 0x2F) {
1136 av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n");
1137 return AVERROR_INVALIDDATA;
1140 w = get_bits(&s->gb, 14) + 1;
1141 h = get_bits(&s->gb, 14) + 1;
1143 update_canvas_size(avctx, w, h);
1145 ret = ff_set_dimensions(avctx, s->width, s->height);
1149 s->has_alpha = get_bits1(&s->gb);
1151 if (get_bits(&s->gb, 3) != 0x0) {
1152 av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n");
1153 return AVERROR_INVALIDDATA;
1156 if (!s->width || !s->height)
1162 /* parse transformations */
1163 s->nb_transforms = 0;
1164 s->reduced_width = 0;
1166 while (get_bits1(&s->gb)) {
1167 enum TransformType transform = get_bits(&s->gb, 2);
1168 if (used & (1 << transform)) {
1169 av_log(avctx, AV_LOG_ERROR, "Transform %d used more than once\n",
1171 ret = AVERROR_INVALIDDATA;
1172 goto free_and_return;
1174 used |= (1 << transform);
1175 s->transforms[s->nb_transforms++] = transform;
1176 switch (transform) {
1177 case PREDICTOR_TRANSFORM:
1178 ret = parse_transform_predictor(s);
1180 case COLOR_TRANSFORM:
1181 ret = parse_transform_color(s);
1183 case COLOR_INDEXING_TRANSFORM:
1184 ret = parse_transform_color_indexing(s);
1188 goto free_and_return;
1191 /* decode primary image */
1192 s->image[IMAGE_ROLE_ARGB].frame = p;
1194 s->image[IMAGE_ROLE_ARGB].is_alpha_primary = 1;
1195 ret = decode_entropy_coded_image(s, IMAGE_ROLE_ARGB, w, h);
1197 goto free_and_return;
1199 /* apply transformations */
1200 for (i = s->nb_transforms - 1; i >= 0; i--) {
1201 switch (s->transforms[i]) {
1202 case PREDICTOR_TRANSFORM:
1203 ret = apply_predictor_transform(s);
1205 case COLOR_TRANSFORM:
1206 ret = apply_color_transform(s);
1208 case SUBTRACT_GREEN:
1209 ret = apply_subtract_green_transform(s);
1211 case COLOR_INDEXING_TRANSFORM:
1212 ret = apply_color_indexing_transform(s);
1216 goto free_and_return;
1220 p->pict_type = AV_PICTURE_TYPE_I;
1225 for (i = 0; i < IMAGE_ROLE_NB; i++)
1226 image_ctx_free(&s->image[i]);
1231 static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m)
1236 ls = frame->linesize[3];
1238 /* filter first row using horizontal filter */
1239 dec = frame->data[3] + 1;
1240 for (x = 1; x < frame->width; x++, dec++)
1243 /* filter first column using vertical filter */
1244 dec = frame->data[3] + ls;
1245 for (y = 1; y < frame->height; y++, dec += ls)
1246 *dec += *(dec - ls);
1248 /* filter the rest using the specified filter */
1250 case ALPHA_FILTER_HORIZONTAL:
1251 for (y = 1; y < frame->height; y++) {
1252 dec = frame->data[3] + y * ls + 1;
1253 for (x = 1; x < frame->width; x++, dec++)
1257 case ALPHA_FILTER_VERTICAL:
1258 for (y = 1; y < frame->height; y++) {
1259 dec = frame->data[3] + y * ls + 1;
1260 for (x = 1; x < frame->width; x++, dec++)
1261 *dec += *(dec - ls);
1264 case ALPHA_FILTER_GRADIENT:
1265 for (y = 1; y < frame->height; y++) {
1266 dec = frame->data[3] + y * ls + 1;
1267 for (x = 1; x < frame->width; x++, dec++)
1268 dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1274 static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p,
1275 uint8_t *data_start,
1276 unsigned int data_size)
1278 WebPContext *s = avctx->priv_data;
1281 if (s->alpha_compression == ALPHA_COMPRESSION_NONE) {
1284 bytestream2_init(&gb, data_start, data_size);
1285 for (y = 0; y < s->height; y++)
1286 bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y,
1288 } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) {
1290 int alpha_got_frame = 0;
1292 s->alpha_frame = av_frame_alloc();
1293 if (!s->alpha_frame)
1294 return AVERROR(ENOMEM);
1296 ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame,
1297 data_start, data_size, 1);
1299 av_frame_free(&s->alpha_frame);
1302 if (!alpha_got_frame) {
1303 av_frame_free(&s->alpha_frame);
1304 return AVERROR_INVALIDDATA;
1307 /* copy green component of alpha image to alpha plane of primary image */
1308 for (y = 0; y < s->height; y++) {
1309 ap = GET_PIXEL(s->alpha_frame, 0, y) + 2;
1310 pp = p->data[3] + p->linesize[3] * y;
1311 for (x = 0; x < s->width; x++) {
1317 av_frame_free(&s->alpha_frame);
1320 /* apply alpha filtering */
1321 if (s->alpha_filter)
1322 alpha_inverse_prediction(p, s->alpha_filter);
1327 static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p,
1328 int *got_frame, uint8_t *data_start,
1329 unsigned int data_size)
1331 WebPContext *s = avctx->priv_data;
1335 if (!s->initialized) {
1336 ff_vp8_decode_init(avctx);
1339 avctx->pix_fmt = s->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1342 if (data_size > INT_MAX) {
1343 av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n");
1344 return AVERROR_PATCHWELCOME;
1347 av_init_packet(&pkt);
1348 pkt.data = data_start;
1349 pkt.size = data_size;
1351 ret = ff_vp8_decode_frame(avctx, p, got_frame, &pkt);
1356 return AVERROR_INVALIDDATA;
1358 update_canvas_size(avctx, avctx->width, avctx->height);
1361 ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data,
1362 s->alpha_data_size);
1369 static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1372 AVFrame * const p = data;
1373 WebPContext *s = avctx->priv_data;
1376 uint32_t chunk_type, chunk_size;
1386 bytestream2_init(&gb, avpkt->data, avpkt->size);
1388 if (bytestream2_get_bytes_left(&gb) < 12)
1389 return AVERROR_INVALIDDATA;
1391 if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
1392 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
1393 return AVERROR_INVALIDDATA;
1396 chunk_size = bytestream2_get_le32(&gb);
1397 if (bytestream2_get_bytes_left(&gb) < chunk_size)
1398 return AVERROR_INVALIDDATA;
1400 if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) {
1401 av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n");
1402 return AVERROR_INVALIDDATA;
1405 while (bytestream2_get_bytes_left(&gb) > 8) {
1406 char chunk_str[5] = { 0 };
1408 chunk_type = bytestream2_get_le32(&gb);
1409 chunk_size = bytestream2_get_le32(&gb);
1410 if (chunk_size == UINT32_MAX)
1411 return AVERROR_INVALIDDATA;
1412 chunk_size += chunk_size & 1;
1414 if (bytestream2_get_bytes_left(&gb) < chunk_size)
1415 return AVERROR_INVALIDDATA;
1417 switch (chunk_type) {
1418 case MKTAG('V', 'P', '8', ' '):
1420 ret = vp8_lossy_decode_frame(avctx, p, got_frame,
1421 avpkt->data + bytestream2_tell(&gb),
1426 bytestream2_skip(&gb, chunk_size);
1428 case MKTAG('V', 'P', '8', 'L'):
1430 ret = vp8_lossless_decode_frame(avctx, p, got_frame,
1431 avpkt->data + bytestream2_tell(&gb),
1435 avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
1437 bytestream2_skip(&gb, chunk_size);
1439 case MKTAG('V', 'P', '8', 'X'):
1440 if (s->width || s->height || *got_frame) {
1441 av_log(avctx, AV_LOG_ERROR, "Canvas dimensions are already set\n");
1442 return AVERROR_INVALIDDATA;
1444 vp8x_flags = bytestream2_get_byte(&gb);
1445 bytestream2_skip(&gb, 3);
1446 s->width = bytestream2_get_le24(&gb) + 1;
1447 s->height = bytestream2_get_le24(&gb) + 1;
1448 ret = av_image_check_size(s->width, s->height, 0, avctx);
1452 case MKTAG('A', 'L', 'P', 'H'): {
1453 int alpha_header, filter_m, compression;
1455 if (!(vp8x_flags & VP8X_FLAG_ALPHA)) {
1456 av_log(avctx, AV_LOG_WARNING,
1457 "ALPHA chunk present, but alpha bit not set in the "
1460 if (chunk_size == 0) {
1461 av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n");
1462 return AVERROR_INVALIDDATA;
1464 alpha_header = bytestream2_get_byte(&gb);
1465 s->alpha_data = avpkt->data + bytestream2_tell(&gb);
1466 s->alpha_data_size = chunk_size - 1;
1467 bytestream2_skip(&gb, s->alpha_data_size);
1469 filter_m = (alpha_header >> 2) & 0x03;
1470 compression = alpha_header & 0x03;
1472 if (compression > ALPHA_COMPRESSION_VP8L) {
1473 av_log(avctx, AV_LOG_VERBOSE,
1474 "skipping unsupported ALPHA chunk\n");
1477 s->alpha_compression = compression;
1478 s->alpha_filter = filter_m;
1483 case MKTAG('E', 'X', 'I', 'F'): {
1484 int le, ifd_offset, exif_offset = bytestream2_tell(&gb);
1485 AVDictionary *exif_metadata = NULL;
1486 GetByteContext exif_gb;
1489 av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n");
1492 if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA))
1493 av_log(avctx, AV_LOG_WARNING,
1494 "EXIF chunk present, but Exif bit not set in the "
1498 bytestream2_init(&exif_gb, avpkt->data + exif_offset,
1499 avpkt->size - exif_offset);
1500 if (ff_tdecode_header(&exif_gb, &le, &ifd_offset) < 0) {
1501 av_log(avctx, AV_LOG_ERROR, "invalid TIFF header "
1506 bytestream2_seek(&exif_gb, ifd_offset, SEEK_SET);
1507 if (ff_exif_decode_ifd(avctx, &exif_gb, le, 0, &exif_metadata) < 0) {
1508 av_log(avctx, AV_LOG_ERROR, "error decoding Exif data\n");
1512 av_dict_copy(&((AVFrame *) data)->metadata, exif_metadata, 0);
1515 av_dict_free(&exif_metadata);
1516 bytestream2_skip(&gb, chunk_size);
1519 case MKTAG('I', 'C', 'C', 'P'): {
1520 AVFrameSideData *sd;
1523 av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra ICCP chunk\n");
1524 bytestream2_skip(&gb, chunk_size);
1527 if (!(vp8x_flags & VP8X_FLAG_ICC))
1528 av_log(avctx, AV_LOG_WARNING,
1529 "ICCP chunk present, but ICC Profile bit not set in the "
1533 sd = av_frame_new_side_data(p, AV_FRAME_DATA_ICC_PROFILE, chunk_size);
1535 return AVERROR(ENOMEM);
1537 bytestream2_get_buffer(&gb, sd->data, chunk_size);
1540 case MKTAG('A', 'N', 'I', 'M'):
1541 case MKTAG('A', 'N', 'M', 'F'):
1542 case MKTAG('X', 'M', 'P', ' '):
1543 AV_WL32(chunk_str, chunk_type);
1544 av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n",
1546 bytestream2_skip(&gb, chunk_size);
1549 AV_WL32(chunk_str, chunk_type);
1550 av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
1552 bytestream2_skip(&gb, chunk_size);
1558 av_log(avctx, AV_LOG_ERROR, "image data not found\n");
1559 return AVERROR_INVALIDDATA;
1565 static av_cold int webp_decode_close(AVCodecContext *avctx)
1567 WebPContext *s = avctx->priv_data;
1570 return ff_vp8_decode_free(avctx);
1575 AVCodec ff_webp_decoder = {
1577 .long_name = NULL_IF_CONFIG_SMALL("WebP image"),
1578 .type = AVMEDIA_TYPE_VIDEO,
1579 .id = AV_CODEC_ID_WEBP,
1580 .priv_data_size = sizeof(WebPContext),
1581 .decode = webp_decode_frame,
1582 .close = webp_decode_close,
1583 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,