FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/webp.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 933 1176 79.3%
Functions: 51 53 96.2%
Branches: 400 556 71.9%

Line Branch Exec Source
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 * Copyright (c) 2020 Pexeso Inc.
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * WebP image decoder
27 *
28 * @author Aneesh Dogra <aneesh@sugarlabs.org>
29 * Container and Lossy decoding
30 *
31 * @author Justin Ruggles <justin.ruggles@gmail.com>
32 * Lossless decoder
33 * Compressed alpha for lossy
34 *
35 * @author James Almer <jamrial@gmail.com>
36 * Exif metadata
37 * ICC profile
38 *
39 * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
40 * XMP metadata
41 *
42 * @author Josef Zlomek, Pexeso Inc. <josef@pex.com>
43 * Animation
44 */
45
46 #include "config_components.h"
47
48 #include "libavutil/colorspace.h"
49 #include "libavutil/imgutils.h"
50 #include "libavutil/mem.h"
51
52 #define BITSTREAM_READER_LE
53 #include "avcodec.h"
54 #include "bytestream.h"
55 #include "codec_internal.h"
56 #include "decode.h"
57 #include "exif_internal.h"
58 #include "get_bits.h"
59 #include "thread.h"
60 #include "tiff_common.h"
61 #include "vp8.h"
62
63 #define VP8X_FLAG_ANIMATION 0x02
64 #define VP8X_FLAG_XMP_METADATA 0x04
65 #define VP8X_FLAG_EXIF_METADATA 0x08
66 #define VP8X_FLAG_ALPHA 0x10
67 #define VP8X_FLAG_ICC 0x20
68
69 #define MAX_PALETTE_SIZE 256
70 #define MAX_CACHE_BITS 11
71 #define NUM_CODE_LENGTH_CODES 19
72 #define HUFFMAN_CODES_PER_META_CODE 5
73 #define NUM_LITERAL_CODES 256
74 #define NUM_LENGTH_CODES 24
75 #define NUM_DISTANCE_CODES 40
76 #define NUM_SHORT_DISTANCES 120
77 #define MAX_HUFFMAN_CODE_LENGTH 15
78
79 static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE] = {
80 NUM_LITERAL_CODES + NUM_LENGTH_CODES,
81 NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
82 NUM_DISTANCE_CODES
83 };
84
85 static const uint8_t code_length_code_order[NUM_CODE_LENGTH_CODES] = {
86 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
87 };
88
89 static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2] = {
90 { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
91 { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
92 { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
93 { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
94 { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
95 { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
96 { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
97 { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
98 { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
99 { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
100 { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
101 { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
102 { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
103 { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
104 { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
105 };
106
107 enum AlphaCompression {
108 ALPHA_COMPRESSION_NONE,
109 ALPHA_COMPRESSION_VP8L,
110 };
111
112 enum AlphaFilter {
113 ALPHA_FILTER_NONE,
114 ALPHA_FILTER_HORIZONTAL,
115 ALPHA_FILTER_VERTICAL,
116 ALPHA_FILTER_GRADIENT,
117 };
118
119 enum TransformType {
120 PREDICTOR_TRANSFORM = 0,
121 COLOR_TRANSFORM = 1,
122 SUBTRACT_GREEN = 2,
123 COLOR_INDEXING_TRANSFORM = 3,
124 };
125
126 enum PredictionMode {
127 PRED_MODE_BLACK,
128 PRED_MODE_L,
129 PRED_MODE_T,
130 PRED_MODE_TR,
131 PRED_MODE_TL,
132 PRED_MODE_AVG_T_AVG_L_TR,
133 PRED_MODE_AVG_L_TL,
134 PRED_MODE_AVG_L_T,
135 PRED_MODE_AVG_TL_T,
136 PRED_MODE_AVG_T_TR,
137 PRED_MODE_AVG_AVG_L_TL_AVG_T_TR,
138 PRED_MODE_SELECT,
139 PRED_MODE_ADD_SUBTRACT_FULL,
140 PRED_MODE_ADD_SUBTRACT_HALF,
141 };
142
143 enum HuffmanIndex {
144 HUFF_IDX_GREEN = 0,
145 HUFF_IDX_RED = 1,
146 HUFF_IDX_BLUE = 2,
147 HUFF_IDX_ALPHA = 3,
148 HUFF_IDX_DIST = 4
149 };
150
151 /* The structure of WebP lossless is an optional series of transformation data,
152 * followed by the primary image. The primary image also optionally contains
153 * an entropy group mapping if there are multiple entropy groups. There is a
154 * basic image type called an "entropy coded image" that is used for all of
155 * these. The type of each entropy coded image is referred to by the
156 * specification as its role. */
157 enum ImageRole {
158 /* Primary Image: Stores the actual pixels of the image. */
159 IMAGE_ROLE_ARGB,
160
161 /* Entropy Image: Defines which Huffman group to use for different areas of
162 * the primary image. */
163 IMAGE_ROLE_ENTROPY,
164
165 /* Predictors: Defines which predictor type to use for different areas of
166 * the primary image. */
167 IMAGE_ROLE_PREDICTOR,
168
169 /* Color Transform Data: Defines the color transformation for different
170 * areas of the primary image. */
171 IMAGE_ROLE_COLOR_TRANSFORM,
172
173 /* Color Index: Stored as an image of height == 1. */
174 IMAGE_ROLE_COLOR_INDEXING,
175
176 IMAGE_ROLE_NB,
177 };
178
179 typedef struct HuffReader {
180 VLC vlc; /* Huffman decoder context */
181 int simple; /* whether to use simple mode */
182 int nb_symbols; /* number of coded symbols */
183 uint16_t simple_symbols[2]; /* symbols for simple mode */
184 } HuffReader;
185
186 typedef struct ImageContext {
187 enum ImageRole role; /* role of this image */
188 AVFrame *frame; /* AVFrame for data */
189 int color_cache_bits; /* color cache size, log2 */
190 uint32_t *color_cache; /* color cache data */
191 int nb_huffman_groups; /* number of huffman groups */
192 HuffReader *huffman_groups; /* reader for each huffman group */
193 /* relative size compared to primary image, log2.
194 * for IMAGE_ROLE_COLOR_INDEXING with <= 16 colors, this is log2 of the
195 * number of pixels per byte in the primary image (pixel packing) */
196 int size_reduction;
197 int is_alpha_primary;
198 } ImageContext;
199
200 typedef struct WebPContext {
201 VP8Context v; /* VP8 Context used for lossy decoding */
202 GetBitContext gb; /* bitstream reader for main image chunk */
203 AVFrame *alpha_frame; /* AVFrame for alpha data decompressed from VP8L */
204 AVPacket *pkt; /* AVPacket to be passed to the underlying VP8 decoder */
205 AVCodecContext *avctx; /* parent AVCodecContext */
206 int initialized; /* set once the VP8 context is initialized */
207 int has_alpha; /* has a separate alpha chunk */
208 enum AlphaCompression alpha_compression; /* compression type for alpha chunk */
209 enum AlphaFilter alpha_filter; /* filtering method for alpha chunk */
210 const uint8_t *alpha_data; /* alpha chunk data */
211 int alpha_data_size; /* alpha chunk data size */
212 int has_exif; /* set after an EXIF chunk has been processed */
213 int has_iccp; /* set after an ICCP chunk has been processed */
214 int has_xmp; /* set after an XMP chunk has been processed */
215 int width; /* image width */
216 int height; /* image height */
217
218 int nb_transforms; /* number of transforms */
219 enum TransformType transforms[4]; /* transformations used in the image, in order */
220 /* reduced width when using a color indexing transform with <= 16 colors (pixel packing)
221 * before pixels are unpacked, or same as width otherwise. */
222 int reduced_width;
223 int nb_huffman_groups; /* number of huffman groups in the primary image */
224 ImageContext image[IMAGE_ROLE_NB]; /* image context for each role */
225 } WebPContext;
226
227 #define GET_PIXEL(frame, x, y) \
228 ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
229
230 #define GET_PIXEL_COMP(frame, x, y, c) \
231 (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
232
233 595 static void image_ctx_free(ImageContext *img)
234 {
235 int i, j;
236
237 595 av_free(img->color_cache);
238
3/4
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 446 times.
✓ Branch 2 taken 149 times.
✗ Branch 3 not taken.
595 if (img->role != IMAGE_ROLE_ARGB && !img->is_alpha_primary)
239 149 av_frame_free(&img->frame);
240
2/2
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 327 times.
595 if (img->huffman_groups) {
241
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 268 times.
566 for (i = 0; i < img->nb_huffman_groups; i++) {
242
2/2
✓ Branch 0 taken 1490 times.
✓ Branch 1 taken 298 times.
1788 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++)
243 1490 ff_vlc_free(&img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE + j].vlc);
244 }
245 268 av_free(img->huffman_groups);
246 }
247 595 memset(img, 0, sizeof(*img));
248 595 }
249
250 909961 static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
251 {
252
2/2
✓ Branch 0 taken 487590 times.
✓ Branch 1 taken 422371 times.
909961 if (r->simple) {
253
2/2
✓ Branch 0 taken 467678 times.
✓ Branch 1 taken 19912 times.
487590 if (r->nb_symbols == 1)
254 467678 return r->simple_symbols[0];
255 else
256 19912 return r->simple_symbols[get_bits1(gb)];
257 } else
258 422371 return get_vlc2(gb, r->vlc.table, 8, 2);
259 }
260
261 1106 static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths,
262 uint16_t len_counts[MAX_HUFFMAN_CODE_LENGTH + 1],
263 uint8_t lens[], uint16_t syms[],
264 int alphabet_size, void *logctx)
265 {
266 1106 unsigned nb_codes = 0;
267 int ret;
268
269 // Count the number of symbols of each length and transform len_counts
270 // into an array of offsets.
271
2/2
✓ Branch 0 taken 16590 times.
✓ Branch 1 taken 1106 times.
17696 for (int len = 1; len <= MAX_HUFFMAN_CODE_LENGTH; ++len) {
272 16590 unsigned cnt = len_counts[len];
273 16590 len_counts[len] = nb_codes;
274 16590 nb_codes += cnt;
275 }
276
277
2/2
✓ Branch 0 taken 150106 times.
✓ Branch 1 taken 1106 times.
151212 for (int sym = 0; sym < alphabet_size; ++sym) {
278
2/2
✓ Branch 0 taken 40151 times.
✓ Branch 1 taken 109955 times.
150106 if (code_lengths[sym]) {
279 40151 unsigned idx = len_counts[code_lengths[sym]]++;
280 40151 syms[idx] = sym;
281 40151 lens[idx] = code_lengths[sym];
282 }
283 }
284
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1106 times.
1106 if (nb_codes == 0) {
286 // No symbols
287 ✗ return AVERROR_INVALIDDATA;
288 }
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1106 times.
1106 if (nb_codes == 1) {
290 // Special-case 1 symbol since the VLC reader cannot handle it
291 ✗ r->nb_symbols = 1;
292 ✗ r->simple = 1;
293 ✗ r->simple_symbols[0] = syms[0];
294 ✗ return 0;
295 }
296
297 1106 ret = ff_vlc_init_from_lengths(&r->vlc, 8, nb_codes, lens, 1,
298 syms, 2, 2, 0, VLC_INIT_OUTPUT_LE, logctx);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1106 times.
1106 if (ret < 0)
300 ✗ return ret;
301 1106 r->simple = 0;
302
303 1106 return 0;
304 }
305
306 937 static void read_huffman_code_simple(WebPContext *s, HuffReader *hc)
307 {
308 937 hc->nb_symbols = get_bits1(&s->gb) + 1;
309
310
2/2
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 784 times.
937 if (get_bits1(&s->gb))
311 153 hc->simple_symbols[0] = get_bits(&s->gb, 8);
312 else
313 784 hc->simple_symbols[0] = get_bits1(&s->gb);
314
315
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 715 times.
937 if (hc->nb_symbols == 2)
316 222 hc->simple_symbols[1] = get_bits(&s->gb, 8);
317
318 937 hc->simple = 1;
319 937 }
320
321 553 static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
322 int alphabet_size)
323 {
324 553 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
325 uint8_t *code_lengths;
326 553 uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
327 uint8_t reordered_code_length_code_lengths[NUM_CODE_LENGTH_CODES];
328 uint16_t reordered_code_length_syms[NUM_CODE_LENGTH_CODES];
329 553 uint16_t len_counts[MAX_HUFFMAN_CODE_LENGTH + 1] = { 0 };
330 int symbol, max_symbol, prev_code_len, ret;
331 553 int num_codes = 4 + get_bits(&s->gb, 4);
332
333 av_assert1(num_codes <= NUM_CODE_LENGTH_CODES);
334
335
2/2
✓ Branch 0 taken 6467 times.
✓ Branch 1 taken 553 times.
7020 for (int i = 0; i < num_codes; i++) {
336 6467 unsigned len = get_bits(&s->gb, 3);
337 6467 code_length_code_lengths[code_length_code_order[i]] = len;
338 6467 len_counts[len]++;
339 }
340
341
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 539 times.
553 if (get_bits1(&s->gb)) {
342 14 int bits = 2 + 2 * get_bits(&s->gb, 3);
343 14 max_symbol = 2 + get_bits(&s->gb, bits);
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (max_symbol > alphabet_size) {
345 ✗ av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n",
346 max_symbol, alphabet_size);
347 ✗ return AVERROR_INVALIDDATA;
348 }
349 } else {
350 539 max_symbol = alphabet_size;
351 }
352
353 553 ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths, len_counts,
354 reordered_code_length_code_lengths,
355 reordered_code_length_syms,
356 553 NUM_CODE_LENGTH_CODES, s->avctx);
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
553 if (ret < 0)
358 ✗ return ret;
359
360 553 code_lengths = av_malloc_array(alphabet_size, 2 * sizeof(uint8_t) + sizeof(uint16_t));
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
553 if (!code_lengths) {
362 ✗ ret = AVERROR(ENOMEM);
363 ✗ goto finish;
364 }
365
366 553 prev_code_len = 8;
367 553 symbol = 0;
368 553 memset(len_counts, 0, sizeof(len_counts));
369
2/2
✓ Branch 0 taken 18532 times.
✓ Branch 1 taken 539 times.
19071 while (symbol < alphabet_size) {
370 int code_len;
371
372
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 18518 times.
18532 if (!max_symbol--)
373 14 break;
374 18518 code_len = huff_reader_get_symbol(&code_len_hc, &s->gb);
375
2/2
✓ Branch 0 taken 11297 times.
✓ Branch 1 taken 7221 times.
18518 if (code_len < 16U) {
376 /* Code length code [0..15] indicates literal code lengths. */
377 11297 code_lengths[symbol++] = code_len;
378 11297 len_counts[code_len]++;
379
2/2
✓ Branch 0 taken 10836 times.
✓ Branch 1 taken 461 times.
11297 if (code_len)
380 10836 prev_code_len = code_len;
381 } else {
382 7221 int repeat = 0, length = 0;
383
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4398 times.
✓ Branch 2 taken 1111 times.
✓ Branch 3 taken 1712 times.
7221 switch (code_len) {
384 ✗ default:
385 ✗ ret = AVERROR_INVALIDDATA;
386 ✗ goto finish;
387 4398 case 16:
388 /* Code 16 repeats the previous non-zero value [3..6] times,
389 * i.e., 3 + ReadBits(2) times. If code 16 is used before a
390 * non-zero value has been emitted, a value of 8 is repeated. */
391 4398 repeat = 3 + get_bits(&s->gb, 2);
392 4398 length = prev_code_len;
393 4398 len_counts[length] += repeat;
394 4398 break;
395 1111 case 17:
396 /* Code 17 emits a streak of zeros [3..10], i.e.,
397 * 3 + ReadBits(3) times. */
398 1111 repeat = 3 + get_bits(&s->gb, 3);
399 1111 break;
400 1712 case 18:
401 /* Code 18 emits a streak of zeros of length [11..138], i.e.,
402 * 11 + ReadBits(7) times. */
403 1712 repeat = 11 + get_bits(&s->gb, 7);
404 1712 break;
405 }
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7221 times.
7221 if (symbol + repeat > alphabet_size) {
407 ✗ av_log(s->avctx, AV_LOG_ERROR,
408 "invalid symbol %d + repeat %d > alphabet size %d\n",
409 symbol, repeat, alphabet_size);
410 ✗ ret = AVERROR_INVALIDDATA;
411 ✗ goto finish;
412 }
413
2/2
✓ Branch 0 taken 128302 times.
✓ Branch 1 taken 7221 times.
135523 while (repeat-- > 0)
414 128302 code_lengths[symbol++] = length;
415 }
416 }
417
418 553 ret = huff_reader_build_canonical(hc, code_lengths, len_counts,
419 code_lengths + symbol,
420 553 (uint16_t*)(code_lengths + 2 * symbol),
421 553 symbol, s->avctx);
422
423 553 finish:
424 553 ff_vlc_free(&code_len_hc.vlc);
425 553 av_free(code_lengths);
426 553 return ret;
427 }
428
429 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
430 int w, int h);
431
432 #define PARSE_BLOCK_SIZE(w, h) do { \
433 block_bits = get_bits(&s->gb, 3) + 2; \
434 blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
435 blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
436 } while (0)
437
438 25 static int decode_entropy_image(WebPContext *s)
439 {
440 ImageContext *img;
441 int ret, block_bits, blocks_w, blocks_h, x, y, max;
442
443 25 PARSE_BLOCK_SIZE(s->reduced_width, s->height);
444
445 25 ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h);
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
447 ✗ return ret;
448
449 25 img = &s->image[IMAGE_ROLE_ENTROPY];
450 25 img->size_reduction = block_bits;
451
452 /* the number of huffman groups is determined by the maximum group number
453 * coded in the entropy image */
454 25 max = 0;
455
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 25 times.
218 for (y = 0; y < img->frame->height; y++) {
456
2/2
✓ Branch 0 taken 3897 times.
✓ Branch 1 taken 193 times.
4090 for (x = 0; x < img->frame->width; x++) {
457 3897 int p0 = GET_PIXEL_COMP(img->frame, x, y, 1);
458 3897 int p1 = GET_PIXEL_COMP(img->frame, x, y, 2);
459 3897 int p = p0 << 8 | p1;
460 3897 max = FFMAX(max, p);
461 }
462 }
463 25 s->nb_huffman_groups = max + 1;
464
465 25 return 0;
466 }
467
468 6 static int parse_transform_predictor(WebPContext *s)
469 {
470 int block_bits, blocks_w, blocks_h, ret;
471
472 6 PARSE_BLOCK_SIZE(s->reduced_width, s->height);
473
474 6 ret = decode_entropy_coded_image(s, IMAGE_ROLE_PREDICTOR, blocks_w,
475 blocks_h);
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
477 ✗ return ret;
478
479 6 s->image[IMAGE_ROLE_PREDICTOR].size_reduction = block_bits;
480
481 6 return 0;
482 }
483
484 4 static int parse_transform_color(WebPContext *s)
485 {
486 int block_bits, blocks_w, blocks_h, ret;
487
488 4 PARSE_BLOCK_SIZE(s->reduced_width, s->height);
489
490 4 ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_TRANSFORM, blocks_w,
491 blocks_h);
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
493 ✗ return ret;
494
495 4 s->image[IMAGE_ROLE_COLOR_TRANSFORM].size_reduction = block_bits;
496
497 4 return 0;
498 }
499
500 114 static int parse_transform_color_indexing(WebPContext *s)
501 {
502 ImageContext *img;
503 int width_bits, index_size, ret, x;
504 uint8_t *ct;
505
506 114 index_size = get_bits(&s->gb, 8) + 1;
507
508
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 96 times.
114 if (index_size <= 2)
509 18 width_bits = 3;
510
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 94 times.
96 else if (index_size <= 4)
511 2 width_bits = 2;
512
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 93 times.
94 else if (index_size <= 16)
513 1 width_bits = 1;
514 else
515 93 width_bits = 0;
516
517 114 ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_INDEXING,
518 index_size, 1);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if (ret < 0)
520 ✗ return ret;
521
522 114 img = &s->image[IMAGE_ROLE_COLOR_INDEXING];
523 114 img->size_reduction = width_bits;
524
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 93 times.
114 if (width_bits > 0)
525 21 s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits;
526
527 /* color index values are delta-coded */
528 114 ct = img->frame->data[0] + 4;
529
2/2
✓ Branch 0 taken 28612 times.
✓ Branch 1 taken 114 times.
28726 for (x = 4; x < img->frame->width * 4; x++, ct++)
530 28612 ct[0] += ct[-4];
531
532 114 return 0;
533 }
534
535 239514 static HuffReader *get_huffman_group(WebPContext *s, ImageContext *img,
536 int x, int y)
537 {
538 239514 ImageContext *gimg = &s->image[IMAGE_ROLE_ENTROPY];
539 239514 int group = 0;
540
541
2/2
✓ Branch 0 taken 146268 times.
✓ Branch 1 taken 93246 times.
239514 if (gimg->size_reduction > 0) {
542 146268 int group_x = x >> gimg->size_reduction;
543 146268 int group_y = y >> gimg->size_reduction;
544 146268 int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1);
545 146268 int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2);
546 146268 group = g0 << 8 | g1;
547 }
548
549 239514 return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE];
550 }
551
552 209778 static av_always_inline void color_cache_put(ImageContext *img, uint32_t c)
553 {
554 209778 uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits);
555 209778 img->color_cache[cache_idx] = c;
556 209778 }
557
558 268 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
559 int w, int h)
560 {
561 ImageContext *img;
562 HuffReader *hg;
563 int i, j, ret, x, y, width;
564
565 268 img = &s->image[role];
566 268 img->role = role;
567
568
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 119 times.
268 if (!img->frame) {
569 149 img->frame = av_frame_alloc();
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
149 if (!img->frame)
571 ✗ return AVERROR(ENOMEM);
572 }
573
574 268 img->frame->format = AV_PIX_FMT_ARGB;
575 268 img->frame->width = w;
576 268 img->frame->height = h;
577
578
4/4
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 16 times.
268 if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) {
579 103 ret = ff_thread_get_buffer(s->avctx, img->frame, 0);
580 } else
581 165 ret = av_frame_get_buffer(img->frame, 1);
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 268 times.
268 if (ret < 0)
583 ✗ return ret;
584
585
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 264 times.
268 if (get_bits1(&s->gb)) {
586 4 img->color_cache_bits = get_bits(&s->gb, 4);
587
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (img->color_cache_bits < 1 || img->color_cache_bits > 11) {
588 ✗ av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n",
589 img->color_cache_bits);
590 ✗ return AVERROR_INVALIDDATA;
591 }
592 4 img->color_cache = av_calloc(1 << img->color_cache_bits,
593 sizeof(*img->color_cache));
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!img->color_cache)
595 ✗ return AVERROR(ENOMEM);
596 } else {
597 264 img->color_cache_bits = 0;
598 }
599
600 268 img->nb_huffman_groups = 1;
601
4/4
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 149 times.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 94 times.
268 if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) {
602 25 ret = decode_entropy_image(s);
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
604 ✗ return ret;
605 25 img->nb_huffman_groups = s->nb_huffman_groups;
606 }
607 268 img->huffman_groups = av_calloc(img->nb_huffman_groups,
608 HUFFMAN_CODES_PER_META_CODE *
609 sizeof(*img->huffman_groups));
610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 268 times.
268 if (!img->huffman_groups)
611 ✗ return AVERROR(ENOMEM);
612
613
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 268 times.
566 for (i = 0; i < img->nb_huffman_groups; i++) {
614 298 hg = &img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE];
615
2/2
✓ Branch 0 taken 1490 times.
✓ Branch 1 taken 298 times.
1788 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) {
616 1490 int alphabet_size = alphabet_sizes[j];
617
4/4
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 1192 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 289 times.
1490 if (!j && img->color_cache_bits > 0)
618 9 alphabet_size += 1 << img->color_cache_bits;
619
620
2/2
✓ Branch 1 taken 937 times.
✓ Branch 2 taken 553 times.
1490 if (get_bits1(&s->gb)) {
621 937 read_huffman_code_simple(s, &hg[j]);
622 } else {
623 553 ret = read_huffman_code_normal(s, &hg[j], alphabet_size);
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
553 if (ret < 0)
625 ✗ return ret;
626 }
627 }
628 }
629
630 268 width = img->frame->width;
631
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 149 times.
268 if (role == IMAGE_ROLE_ARGB)
632 119 width = s->reduced_width;
633
634 268 x = 0; y = 0;
635
2/2
✓ Branch 0 taken 239514 times.
✓ Branch 1 taken 268 times.
239782 while (y < img->frame->height) {
636 int v;
637
638
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 239514 times.
239514 if (get_bits_left(&s->gb) < 0)
639 ✗ return AVERROR_INVALIDDATA;
640
641 239514 hg = get_huffman_group(s, img, x, y);
642 239514 v = huff_reader_get_symbol(&hg[HUFF_IDX_GREEN], &s->gb);
643
2/2
✓ Branch 0 taken 207984 times.
✓ Branch 1 taken 31530 times.
239514 if (v < NUM_LITERAL_CODES) {
644 /* literal pixel values */
645 207984 uint8_t *p = GET_PIXEL(img->frame, x, y);
646 207984 p[2] = v;
647 207984 p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb);
648 207984 p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb);
649 207984 p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb);
650
2/2
✓ Branch 0 taken 8244 times.
✓ Branch 1 taken 199740 times.
207984 if (img->color_cache_bits)
651 8244 color_cache_put(img, AV_RB32(p));
652 207984 x++;
653
2/2
✓ Branch 0 taken 1477 times.
✓ Branch 1 taken 206507 times.
207984 if (x == width) {
654 1477 x = 0;
655 1477 y++;
656 }
657
2/2
✓ Branch 0 taken 27977 times.
✓ Branch 1 taken 3553 times.
31530 } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
658 /* LZ77 backwards mapping */
659 int prefix_code, length, distance, ref_x, ref_y;
660
661 /* parse length and distance */
662 27977 prefix_code = v - NUM_LITERAL_CODES;
663
2/2
✓ Branch 0 taken 4044 times.
✓ Branch 1 taken 23933 times.
27977 if (prefix_code < 4) {
664 4044 length = prefix_code + 1;
665 } else {
666 23933 int extra_bits = (prefix_code - 2) >> 1;
667 23933 int offset = 2 + (prefix_code & 1) << extra_bits;
668 23933 length = offset + get_bits(&s->gb, extra_bits) + 1;
669 }
670 27977 prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb);
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27977 times.
27977 if (prefix_code > 39U) {
672 ✗ av_log(s->avctx, AV_LOG_ERROR,
673 "distance prefix code too large: %d\n", prefix_code);
674 ✗ return AVERROR_INVALIDDATA;
675 }
676
2/2
✓ Branch 0 taken 18110 times.
✓ Branch 1 taken 9867 times.
27977 if (prefix_code < 4) {
677 18110 distance = prefix_code + 1;
678 } else {
679 9867 int extra_bits = prefix_code - 2 >> 1;
680 9867 int offset = 2 + (prefix_code & 1) << extra_bits;
681 9867 distance = offset + get_bits(&s->gb, extra_bits) + 1;
682 }
683
684 /* find reference location */
685
2/2
✓ Branch 0 taken 22481 times.
✓ Branch 1 taken 5496 times.
27977 if (distance <= NUM_SHORT_DISTANCES) {
686 22481 int xi = lz77_distance_offsets[distance - 1][0];
687 22481 int yi = lz77_distance_offsets[distance - 1][1];
688 22481 distance = FFMAX(1, xi + yi * width);
689 } else {
690 5496 distance -= NUM_SHORT_DISTANCES;
691 }
692 27977 ref_x = x;
693 27977 ref_y = y;
694
2/2
✓ Branch 0 taken 7319 times.
✓ Branch 1 taken 20658 times.
27977 if (distance <= x) {
695 7319 ref_x -= distance;
696 7319 distance = 0;
697 } else {
698 20658 ref_x = 0;
699 20658 distance -= x;
700 }
701
2/2
✓ Branch 0 taken 70981 times.
✓ Branch 1 taken 27977 times.
98958 while (distance >= width) {
702 70981 ref_y--;
703 70981 distance -= width;
704 }
705
2/2
✓ Branch 0 taken 20440 times.
✓ Branch 1 taken 7537 times.
27977 if (distance > 0) {
706 20440 ref_x = width - distance;
707 20440 ref_y--;
708 }
709 27977 ref_x = FFMAX(0, ref_x);
710 27977 ref_y = FFMAX(0, ref_y);
711
712
3/4
✓ Branch 0 taken 7319 times.
✓ Branch 1 taken 20658 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7319 times.
27977 if (ref_y == y && ref_x >= x)
713 ✗ return AVERROR_INVALIDDATA;
714
715 /* copy pixels
716 * source and dest regions can overlap and wrap lines, so just
717 * copy per-pixel */
718
2/2
✓ Branch 0 taken 956688 times.
✓ Branch 1 taken 27877 times.
984565 for (i = 0; i < length; i++) {
719 956688 uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y);
720 956688 uint8_t *p = GET_PIXEL(img->frame, x, y);
721
722 956688 AV_COPY32(p, p_ref);
723
2/2
✓ Branch 0 taken 201534 times.
✓ Branch 1 taken 755154 times.
956688 if (img->color_cache_bits)
724 201534 color_cache_put(img, AV_RB32(p));
725 956688 x++;
726 956688 ref_x++;
727
2/2
✓ Branch 0 taken 8717 times.
✓ Branch 1 taken 947971 times.
956688 if (x == width) {
728 8717 x = 0;
729 8717 y++;
730 }
731
2/2
✓ Branch 0 taken 8513 times.
✓ Branch 1 taken 948175 times.
956688 if (ref_x == width) {
732 8513 ref_x = 0;
733 8513 ref_y++;
734 }
735
3/4
✓ Branch 0 taken 956588 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 956588 times.
✗ Branch 3 not taken.
956688 if (y == img->frame->height || ref_y == img->frame->height)
736 break;
737 }
738 } else {
739 /* read from color cache */
740 3553 uint8_t *p = GET_PIXEL(img->frame, x, y);
741 3553 int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES);
742
743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3553 times.
3553 if (!img->color_cache_bits) {
744 ✗ av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n");
745 ✗ return AVERROR_INVALIDDATA;
746 }
747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3553 times.
3553 if (cache_idx >= 1 << img->color_cache_bits) {
748 ✗ av_log(s->avctx, AV_LOG_ERROR,
749 "color cache index out-of-bounds\n");
750 ✗ return AVERROR_INVALIDDATA;
751 }
752 3553 AV_WB32(p, img->color_cache[cache_idx]);
753 3553 x++;
754
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 3512 times.
3553 if (x == width) {
755 41 x = 0;
756 41 y++;
757 }
758 }
759 }
760
761 268 return 0;
762 }
763
764 /* PRED_MODE_BLACK */
765 6 static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
766 const uint8_t *p_t, const uint8_t *p_tr)
767 {
768 6 AV_WB32(p, 0xFF000000);
769 6 }
770
771 /* PRED_MODE_L */
772 5300 static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
773 const uint8_t *p_t, const uint8_t *p_tr)
774 {
775 5300 AV_COPY32(p, p_l);
776 5300 }
777
778 /* PRED_MODE_T */
779 21182 static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
780 const uint8_t *p_t, const uint8_t *p_tr)
781 {
782 21182 AV_COPY32(p, p_t);
783 21182 }
784
785 /* PRED_MODE_TR */
786 6336 static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
787 const uint8_t *p_t, const uint8_t *p_tr)
788 {
789 6336 AV_COPY32(p, p_tr);
790 6336 }
791
792 /* PRED_MODE_TL */
793 1248 static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
794 const uint8_t *p_t, const uint8_t *p_tr)
795 {
796 1248 AV_COPY32(p, p_tl);
797 1248 }
798
799 /* PRED_MODE_AVG_T_AVG_L_TR */
800 4832 static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
801 const uint8_t *p_t, const uint8_t *p_tr)
802 {
803 4832 p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
804 4832 p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
805 4832 p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
806 4832 p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
807 4832 }
808
809 /* PRED_MODE_AVG_L_TL */
810 768 static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
811 const uint8_t *p_t, const uint8_t *p_tr)
812 {
813 768 p[0] = p_l[0] + p_tl[0] >> 1;
814 768 p[1] = p_l[1] + p_tl[1] >> 1;
815 768 p[2] = p_l[2] + p_tl[2] >> 1;
816 768 p[3] = p_l[3] + p_tl[3] >> 1;
817 768 }
818
819 /* PRED_MODE_AVG_L_T */
820 3072 static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
821 const uint8_t *p_t, const uint8_t *p_tr)
822 {
823 3072 p[0] = p_l[0] + p_t[0] >> 1;
824 3072 p[1] = p_l[1] + p_t[1] >> 1;
825 3072 p[2] = p_l[2] + p_t[2] >> 1;
826 3072 p[3] = p_l[3] + p_t[3] >> 1;
827 3072 }
828
829 /* PRED_MODE_AVG_TL_T */
830 4512 static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
831 const uint8_t *p_t, const uint8_t *p_tr)
832 {
833 4512 p[0] = p_tl[0] + p_t[0] >> 1;
834 4512 p[1] = p_tl[1] + p_t[1] >> 1;
835 4512 p[2] = p_tl[2] + p_t[2] >> 1;
836 4512 p[3] = p_tl[3] + p_t[3] >> 1;
837 4512 }
838
839 /* PRED_MODE_AVG_T_TR */
840 11488 static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
841 const uint8_t *p_t, const uint8_t *p_tr)
842 {
843 11488 p[0] = p_t[0] + p_tr[0] >> 1;
844 11488 p[1] = p_t[1] + p_tr[1] >> 1;
845 11488 p[2] = p_t[2] + p_tr[2] >> 1;
846 11488 p[3] = p_t[3] + p_tr[3] >> 1;
847 11488 }
848
849 /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */
850 3236 static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
851 const uint8_t *p_t, const uint8_t *p_tr)
852 {
853 3236 p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
854 3236 p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
855 3236 p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
856 3236 p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
857 3236 }
858
859 /* PRED_MODE_SELECT */
860 3824 static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
861 const uint8_t *p_t, const uint8_t *p_tr)
862 {
863 3824 int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) +
864 3824 (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) +
865 3824 (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) +
866 3824 (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3]));
867
2/2
✓ Branch 0 taken 2602 times.
✓ Branch 1 taken 1222 times.
3824 if (diff <= 0)
868 2602 AV_COPY32(p, p_t);
869 else
870 1222 AV_COPY32(p, p_l);
871 3824 }
872
873 /* PRED_MODE_ADD_SUBTRACT_FULL */
874 ✗ static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
875 const uint8_t *p_t, const uint8_t *p_tr)
876 {
877 ✗ p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
878 ✗ p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
879 ✗ p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
880 ✗ p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
881 ✗ }
882
883 2048 static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c)
884 {
885 2048 int d = a + b >> 1;
886 2048 return av_clip_uint8(d + (d - c) / 2);
887 }
888
889 /* PRED_MODE_ADD_SUBTRACT_HALF */
890 512 static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
891 const uint8_t *p_t, const uint8_t *p_tr)
892 {
893 512 p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]);
894 512 p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]);
895 512 p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]);
896 512 p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]);
897 512 }
898
899 typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l,
900 const uint8_t *p_tl, const uint8_t *p_t,
901 const uint8_t *p_tr);
902
903 static const inv_predict_func inverse_predict[14] = {
904 inv_predict_0, inv_predict_1, inv_predict_2, inv_predict_3,
905 inv_predict_4, inv_predict_5, inv_predict_6, inv_predict_7,
906 inv_predict_8, inv_predict_9, inv_predict_10, inv_predict_11,
907 inv_predict_12, inv_predict_13,
908 };
909
910 66316 static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
911 {
912 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
913 uint8_t p[4];
914
915 66316 dec = GET_PIXEL(frame, x, y);
916 66316 p_l = GET_PIXEL(frame, x - 1, y);
917 66316 p_tl = GET_PIXEL(frame, x - 1, y - 1);
918 66316 p_t = GET_PIXEL(frame, x, y - 1);
919
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 65804 times.
66316 if (x == frame->width - 1)
920 512 p_tr = GET_PIXEL(frame, 0, y);
921 else
922 65804 p_tr = GET_PIXEL(frame, x + 1, y - 1);
923
924 66316 inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
925
926 66316 dec[0] += p[0];
927 66316 dec[1] += p[1];
928 66316 dec[2] += p[2];
929 66316 dec[3] += p[3];
930 66316 }
931
932 6 static int apply_predictor_transform(WebPContext *s)
933 {
934 6 ImageContext *img = &s->image[IMAGE_ROLE_ARGB];
935 6 ImageContext *pimg = &s->image[IMAGE_ROLE_PREDICTOR];
936 int x, y;
937
938
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 6 times.
578 for (y = 0; y < img->frame->height; y++) {
939
2/2
✓ Branch 0 taken 66316 times.
✓ Branch 1 taken 572 times.
66888 for (x = 0; x < s->reduced_width; x++) {
940 66316 int tx = x >> pimg->size_reduction;
941 66316 int ty = y >> pimg->size_reduction;
942 66316 enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2);
943
944
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 65744 times.
66316 if (x == 0) {
945
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 566 times.
572 if (y == 0)
946 6 m = PRED_MODE_BLACK;
947 else
948 566 m = PRED_MODE_T;
949
2/2
✓ Branch 0 taken 532 times.
✓ Branch 1 taken 65212 times.
65744 } else if (y == 0)
950 532 m = PRED_MODE_L;
951
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66316 times.
66316 if (m > 13) {
953 ✗ av_log(s->avctx, AV_LOG_ERROR,
954 "invalid predictor mode: %d\n", m);
955 ✗ return AVERROR_INVALIDDATA;
956 }
957 66316 inverse_prediction(img->frame, m, x, y);
958 }
959 }
960 6 return 0;
961 }
962
963 196608 static av_always_inline uint8_t color_transform_delta(uint8_t color_pred,
964 uint8_t color)
965 {
966 196608 return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5;
967 }
968
969 4 static int apply_color_transform(WebPContext *s)
970 {
971 ImageContext *img, *cimg;
972 int x, y, cx, cy;
973 uint8_t *p, *cp;
974
975 4 img = &s->image[IMAGE_ROLE_ARGB];
976 4 cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM];
977
978
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
516 for (y = 0; y < img->frame->height; y++) {
979
2/2
✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
66048 for (x = 0; x < s->reduced_width; x++) {
980 65536 cx = x >> cimg->size_reduction;
981 65536 cy = y >> cimg->size_reduction;
982 65536 cp = GET_PIXEL(cimg->frame, cx, cy);
983 65536 p = GET_PIXEL(img->frame, x, y);
984
985 65536 p[1] += color_transform_delta(cp[3], p[2]);
986 65536 p[3] += color_transform_delta(cp[2], p[2]) +
987 65536 color_transform_delta(cp[1], p[1]);
988 }
989 }
990 4 return 0;
991 }
992
993 5 static int apply_subtract_green_transform(WebPContext *s)
994 {
995 int x, y;
996 5 ImageContext *img = &s->image[IMAGE_ROLE_ARGB];
997
998
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 5 times.
864 for (y = 0; y < img->frame->height; y++) {
999
2/2
✓ Branch 0 taken 206765 times.
✓ Branch 1 taken 859 times.
207624 for (x = 0; x < s->reduced_width; x++) {
1000 206765 uint8_t *p = GET_PIXEL(img->frame, x, y);
1001 206765 p[1] += p[2];
1002 206765 p[3] += p[2];
1003 }
1004 }
1005 5 return 0;
1006 }
1007
1008 114 static int apply_color_indexing_transform(WebPContext *s)
1009 {
1010 ImageContext *img;
1011 ImageContext *pal;
1012 int i, x, y;
1013 uint8_t *p;
1014
1015 114 img = &s->image[IMAGE_ROLE_ARGB];
1016 114 pal = &s->image[IMAGE_ROLE_COLOR_INDEXING];
1017
1018
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 93 times.
114 if (pal->size_reduction > 0) { // undo pixel packing
1019 GetBitContext gb_g;
1020 uint8_t *line;
1021 21 int pixel_bits = 8 >> pal->size_reduction;
1022
1023 21 line = av_malloc(img->frame->linesize[0] + AV_INPUT_BUFFER_PADDING_SIZE);
1024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!line)
1025 ✗ return AVERROR(ENOMEM);
1026
1027
2/2
✓ Branch 0 taken 1912 times.
✓ Branch 1 taken 21 times.
1933 for (y = 0; y < img->frame->height; y++) {
1028 1912 p = GET_PIXEL(img->frame, 0, y);
1029 1912 memcpy(line, p, img->frame->linesize[0]);
1030 1912 init_get_bits(&gb_g, line, img->frame->linesize[0] * 8);
1031 1912 skip_bits(&gb_g, 16);
1032 1912 i = 0;
1033
2/2
✓ Branch 0 taken 411466 times.
✓ Branch 1 taken 1912 times.
413378 for (x = 0; x < img->frame->width; x++) {
1034 411466 p = GET_PIXEL(img->frame, x, y);
1035 411466 p[2] = get_bits(&gb_g, pixel_bits);
1036 411466 i++;
1037
2/2
✓ Branch 0 taken 50969 times.
✓ Branch 1 taken 360497 times.
411466 if (i == 1 << pal->size_reduction) {
1038 50969 skip_bits(&gb_g, 24);
1039 50969 i = 0;
1040 }
1041 }
1042 }
1043 21 av_free(line);
1044 21 s->reduced_width = s->width; // we are back to full size
1045 }
1046
1047 // switch to local palette if it's worth initializing it
1048
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 7 times.
114 if (img->frame->height * img->frame->width > 300) {
1049 uint8_t palette[256 * 4];
1050 107 const int size = pal->frame->width * 4;
1051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 av_assert0(size <= 1024U);
1052 107 memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size); // copy palette
1053 // set extra entries to transparent black
1054 107 memset(palette + size, 0, 256 * 4 - size);
1055
2/2
✓ Branch 0 taken 8887 times.
✓ Branch 1 taken 107 times.
8994 for (y = 0; y < img->frame->height; y++) {
1056
2/2
✓ Branch 0 taken 1306622 times.
✓ Branch 1 taken 8887 times.
1315509 for (x = 0; x < img->frame->width; x++) {
1057 1306622 p = GET_PIXEL(img->frame, x, y);
1058 1306622 i = p[2];
1059 1306622 AV_COPY32(p, &palette[i * 4]);
1060 }
1061 }
1062 } else {
1063
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 7 times.
57 for (y = 0; y < img->frame->height; y++) {
1064
2/2
✓ Branch 0 taken 578 times.
✓ Branch 1 taken 50 times.
628 for (x = 0; x < img->frame->width; x++) {
1065 578 p = GET_PIXEL(img->frame, x, y);
1066 578 i = p[2];
1067
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 578 times.
578 if (i >= pal->frame->width) {
1068 ✗ AV_WB32(p, 0x00000000);
1069 } else {
1070 578 const uint8_t *pi = GET_PIXEL(pal->frame, i, 0);
1071 578 AV_COPY32(p, pi);
1072 }
1073 }
1074 }
1075 }
1076
1077 114 return 0;
1078 }
1079
1080 152 static void update_canvas_size(AVCodecContext *avctx, int w, int h)
1081 {
1082 152 WebPContext *s = avctx->priv_data;
1083
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
152 if (s->width && s->width != w) {
1084 ✗ av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n",
1085 s->width, w);
1086 }
1087 152 s->width = w;
1088
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
152 if (s->height && s->height != h) {
1089 ✗ av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n",
1090 s->height, h);
1091 }
1092 152 s->height = h;
1093 152 }
1094
1095 119 static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p,
1096 int *got_frame, const uint8_t *data_start,
1097 unsigned int data_size, int is_alpha_chunk)
1098 {
1099 119 WebPContext *s = avctx->priv_data;
1100 int w, h, ret, i, used;
1101
1102
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 16 times.
119 if (!is_alpha_chunk)
1103 103 avctx->pix_fmt = AV_PIX_FMT_ARGB;
1104
1105 119 ret = init_get_bits8(&s->gb, data_start, data_size);
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
119 if (ret < 0)
1107 ✗ return ret;
1108
1109
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 16 times.
119 if (!is_alpha_chunk) {
1110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
103 if (get_bits(&s->gb, 8) != 0x2F) {
1111 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n");
1112 ✗ return AVERROR_INVALIDDATA;
1113 }
1114
1115 103 w = get_bits(&s->gb, 14) + 1;
1116 103 h = get_bits(&s->gb, 14) + 1;
1117
1118 103 update_canvas_size(avctx, w, h);
1119
1120 103 ret = ff_set_dimensions(avctx, s->width, s->height);
1121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (ret < 0)
1122 ✗ return ret;
1123
1124 103 s->has_alpha = get_bits1(&s->gb);
1125
1126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
103 if (get_bits(&s->gb, 3) != 0x0) {
1127 ✗ av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n");
1128 ✗ return AVERROR_INVALIDDATA;
1129 }
1130 } else {
1131
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (!s->width || !s->height)
1132 ✗ return AVERROR_BUG;
1133 16 w = s->width;
1134 16 h = s->height;
1135 }
1136
1137 /* parse transformations */
1138 119 s->nb_transforms = 0;
1139 119 s->reduced_width = s->width;
1140 119 used = 0;
1141
2/2
✓ Branch 1 taken 129 times.
✓ Branch 2 taken 119 times.
248 while (get_bits1(&s->gb)) {
1142 129 enum TransformType transform = get_bits(&s->gb, 2);
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 if (used & (1 << transform)) {
1144 ✗ av_log(avctx, AV_LOG_ERROR, "Transform %d used more than once\n",
1145 transform);
1146 ✗ ret = AVERROR_INVALIDDATA;
1147 ✗ goto free_and_return;
1148 }
1149 129 used |= (1 << transform);
1150 129 s->transforms[s->nb_transforms++] = transform;
1151
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 5 times.
129 switch (transform) {
1152 6 case PREDICTOR_TRANSFORM:
1153 6 ret = parse_transform_predictor(s);
1154 6 break;
1155 4 case COLOR_TRANSFORM:
1156 4 ret = parse_transform_color(s);
1157 4 break;
1158 114 case COLOR_INDEXING_TRANSFORM:
1159 114 ret = parse_transform_color_indexing(s);
1160 114 break;
1161 }
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 if (ret < 0)
1163 ✗ goto free_and_return;
1164 }
1165
1166 /* decode primary image */
1167 119 s->image[IMAGE_ROLE_ARGB].frame = p;
1168
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 103 times.
119 if (is_alpha_chunk)
1169 16 s->image[IMAGE_ROLE_ARGB].is_alpha_primary = 1;
1170 119 ret = decode_entropy_coded_image(s, IMAGE_ROLE_ARGB, w, h);
1171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
119 if (ret < 0)
1172 ✗ goto free_and_return;
1173
1174 /* apply transformations */
1175
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 119 times.
248 for (i = s->nb_transforms - 1; i >= 0; i--) {
1176
4/5
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 114 times.
✗ Branch 4 not taken.
129 switch (s->transforms[i]) {
1177 6 case PREDICTOR_TRANSFORM:
1178 6 ret = apply_predictor_transform(s);
1179 6 break;
1180 4 case COLOR_TRANSFORM:
1181 4 ret = apply_color_transform(s);
1182 4 break;
1183 5 case SUBTRACT_GREEN:
1184 5 ret = apply_subtract_green_transform(s);
1185 5 break;
1186 114 case COLOR_INDEXING_TRANSFORM:
1187 114 ret = apply_color_indexing_transform(s);
1188 114 break;
1189 }
1190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 if (ret < 0)
1191 ✗ goto free_and_return;
1192 }
1193
1194 119 *got_frame = 1;
1195 119 p->pict_type = AV_PICTURE_TYPE_I;
1196 119 p->flags |= AV_FRAME_FLAG_KEY;
1197 119 p->flags |= AV_FRAME_FLAG_LOSSLESS;
1198 119 ret = data_size;
1199
1200 119 free_and_return:
1201
2/2
✓ Branch 0 taken 595 times.
✓ Branch 1 taken 119 times.
714 for (i = 0; i < IMAGE_ROLE_NB; i++)
1202 595 image_ctx_free(&s->image[i]);
1203
1204 119 return ret;
1205 }
1206
1207 ✗ static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m)
1208 {
1209 int x, y, ls;
1210 uint8_t *dec;
1211
1212 ✗ ls = frame->linesize[3];
1213
1214 /* filter first row using horizontal filter */
1215 ✗ dec = frame->data[3] + 1;
1216 ✗ for (x = 1; x < frame->width; x++, dec++)
1217 ✗ *dec += *(dec - 1);
1218
1219 /* filter first column using vertical filter */
1220 ✗ dec = frame->data[3] + ls;
1221 ✗ for (y = 1; y < frame->height; y++, dec += ls)
1222 ✗ *dec += *(dec - ls);
1223
1224 /* filter the rest using the specified filter */
1225 ✗ switch (m) {
1226 ✗ case ALPHA_FILTER_HORIZONTAL:
1227 ✗ for (y = 1; y < frame->height; y++) {
1228 ✗ dec = frame->data[3] + y * ls + 1;
1229 ✗ for (x = 1; x < frame->width; x++, dec++)
1230 ✗ *dec += *(dec - 1);
1231 }
1232 ✗ break;
1233 ✗ case ALPHA_FILTER_VERTICAL:
1234 ✗ for (y = 1; y < frame->height; y++) {
1235 ✗ dec = frame->data[3] + y * ls + 1;
1236 ✗ for (x = 1; x < frame->width; x++, dec++)
1237 ✗ *dec += *(dec - ls);
1238 }
1239 ✗ break;
1240 ✗ case ALPHA_FILTER_GRADIENT:
1241 ✗ for (y = 1; y < frame->height; y++) {
1242 ✗ dec = frame->data[3] + y * ls + 1;
1243 ✗ for (x = 1; x < frame->width; x++, dec++)
1244 ✗ dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1245 }
1246 ✗ break;
1247 }
1248 ✗ }
1249
1250 16 static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p,
1251 const uint8_t *data_start,
1252 unsigned int data_size)
1253 {
1254 16 WebPContext *s = avctx->priv_data;
1255 int x, y, ret;
1256
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s->alpha_compression == ALPHA_COMPRESSION_NONE) {
1258 GetByteContext gb;
1259
1260 ✗ if (data_size < s->width * s->height)
1261 ✗ return AVERROR_INVALIDDATA;
1262 ✗ bytestream2_init(&gb, data_start, data_size);
1263 ✗ for (y = 0; y < s->height; y++)
1264 ✗ bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y,
1265 ✗ s->width);
1266
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) {
1267 uint8_t *ap, *pp;
1268 16 int alpha_got_frame = 0;
1269
1270 16 s->alpha_frame = av_frame_alloc();
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!s->alpha_frame)
1272 ✗ return AVERROR(ENOMEM);
1273
1274 16 ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame,
1275 data_start, data_size, 1);
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0) {
1277 ✗ av_frame_free(&s->alpha_frame);
1278 ✗ return ret;
1279 }
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!alpha_got_frame) {
1281 ✗ av_frame_free(&s->alpha_frame);
1282 ✗ return AVERROR_INVALIDDATA;
1283 }
1284
1285 /* copy green component of alpha image to alpha plane of primary image */
1286
2/2
✓ Branch 0 taken 1727 times.
✓ Branch 1 taken 16 times.
1743 for (y = 0; y < s->height; y++) {
1287 1727 ap = GET_PIXEL(s->alpha_frame, 0, y) + 2;
1288 1727 pp = p->data[3] + p->linesize[3] * y;
1289
2/2
✓ Branch 0 taken 397722 times.
✓ Branch 1 taken 1727 times.
399449 for (x = 0; x < s->width; x++) {
1290 397722 *pp = *ap;
1291 397722 pp++;
1292 397722 ap += 4;
1293 }
1294 }
1295 16 av_frame_free(&s->alpha_frame);
1296 }
1297
1298 /* apply alpha filtering */
1299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s->alpha_filter)
1300 ✗ alpha_inverse_prediction(p, s->alpha_filter);
1301
1302 16 return 0;
1303 }
1304
1305 49 static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p,
1306 int *got_frame, uint8_t *data_start,
1307 unsigned int data_size)
1308 {
1309 49 WebPContext *s = avctx->priv_data;
1310 int ret;
1311
1312
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 41 times.
49 if (!s->initialized) {
1313 8 ff_vp8_decode_init(avctx);
1314 8 s->initialized = 1;
1315 8 s->v.actually_webp = 1;
1316 }
1317
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 33 times.
49 avctx->pix_fmt = s->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1318
1319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (data_size > INT_MAX) {
1320 ✗ av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n");
1321 ✗ return AVERROR_PATCHWELCOME;
1322 }
1323
1324 49 av_packet_unref(s->pkt);
1325 49 s->pkt->data = data_start;
1326 49 s->pkt->size = data_size;
1327
1328 49 ret = ff_vp8_decode_frame(avctx, p, got_frame, s->pkt);
1329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (ret < 0)
1330 ✗ return ret;
1331
1332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (!*got_frame)
1333 ✗ return AVERROR_INVALIDDATA;
1334
1335 49 update_canvas_size(avctx, avctx->width, avctx->height);
1336
1337
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 33 times.
49 if (s->has_alpha) {
1338 16 ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data,
1339 16 s->alpha_data_size);
1340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0) {
1341 ✗ *got_frame = 0;
1342 ✗ return ret;
1343 }
1344 }
1345 49 return ret;
1346 }
1347
1348 16 static int webp_decode_frame(AVCodecContext *avctx, AVFrame *p,
1349 int *got_frame, AVPacket *avpkt)
1350 {
1351 16 WebPContext *s = avctx->priv_data;
1352 GetByteContext gb;
1353 int ret;
1354 uint32_t chunk_type, chunk_size;
1355 16 int vp8x_flags = 0;
1356
1357 16 s->avctx = avctx;
1358 16 s->width = 0;
1359 16 s->height = 0;
1360 16 *got_frame = 0;
1361 16 s->has_alpha = 0;
1362 16 s->has_exif = 0;
1363 16 s->has_iccp = 0;
1364 16 s->has_xmp = 0;
1365 16 bytestream2_init(&gb, avpkt->data, avpkt->size);
1366
1367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (bytestream2_get_bytes_left(&gb) < 12)
1368 ✗ return AVERROR_INVALIDDATA;
1369
1370
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
1371 ✗ av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
1372 ✗ return AVERROR_INVALIDDATA;
1373 }
1374
1375 16 chunk_size = bytestream2_get_le32(&gb);
1376
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (bytestream2_get_bytes_left(&gb) < chunk_size)
1377 ✗ return AVERROR_INVALIDDATA;
1378
1379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) {
1380 ✗ av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n");
1381 ✗ return AVERROR_INVALIDDATA;
1382 }
1383
1384
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 16 times.
40 while (bytestream2_get_bytes_left(&gb) > 8) {
1385 24 chunk_type = bytestream2_get_le32(&gb);
1386 24 chunk_size = bytestream2_get_le32(&gb);
1387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (chunk_size == UINT32_MAX)
1388 ✗ return AVERROR_INVALIDDATA;
1389 24 chunk_size += chunk_size & 1;
1390
1391
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (bytestream2_get_bytes_left(&gb) < chunk_size) {
1392 /* we seem to be running out of data, but it could also be that the
1393 bitstream has trailing junk leading to bogus chunk_size. */
1394 ✗ break;
1395 }
1396
1397
5/9
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
24 switch (chunk_type) {
1398 6 case MKTAG('V', 'P', '8', ' '):
1399
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!*got_frame) {
1400 6 ret = vp8_lossy_decode_frame(avctx, p, got_frame,
1401 6 avpkt->data + bytestream2_tell(&gb),
1402 chunk_size);
1403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1404 ✗ return ret;
1405 }
1406 6 bytestream2_skip(&gb, chunk_size);
1407 24 break;
1408 10 case MKTAG('V', 'P', '8', 'L'):
1409
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!*got_frame) {
1410 10 ret = vp8_lossless_decode_frame(avctx, p, got_frame,
1411 10 avpkt->data + bytestream2_tell(&gb),
1412 chunk_size, 0);
1413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
1414 ✗ return ret;
1415 }
1416 10 bytestream2_skip(&gb, chunk_size);
1417 10 break;
1418 4 case MKTAG('V', 'P', '8', 'X'):
1419
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (s->width || s->height || *got_frame) {
1420 ✗ av_log(avctx, AV_LOG_ERROR, "Canvas dimensions are already set\n");
1421 ✗ return AVERROR_INVALIDDATA;
1422 }
1423 4 vp8x_flags = bytestream2_get_byte(&gb);
1424 4 bytestream2_skip(&gb, 3);
1425 4 s->width = bytestream2_get_le24(&gb) + 1;
1426 4 s->height = bytestream2_get_le24(&gb) + 1;
1427 4 ret = av_image_check_size(s->width, s->height, 0, avctx);
1428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
1429 ✗ return ret;
1430 4 break;
1431 2 case MKTAG('A', 'L', 'P', 'H'): {
1432 int alpha_header, filter_m, compression;
1433
1434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(vp8x_flags & VP8X_FLAG_ALPHA)) {
1435 ✗ av_log(avctx, AV_LOG_WARNING,
1436 "ALPHA chunk present, but alpha bit not set in the "
1437 "VP8X header\n");
1438 }
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (chunk_size == 0) {
1440 ✗ av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n");
1441 ✗ return AVERROR_INVALIDDATA;
1442 }
1443 2 alpha_header = bytestream2_get_byte(&gb);
1444 2 s->alpha_data = avpkt->data + bytestream2_tell(&gb);
1445 2 s->alpha_data_size = chunk_size - 1;
1446 2 bytestream2_skip(&gb, s->alpha_data_size);
1447
1448 2 filter_m = (alpha_header >> 2) & 0x03;
1449 2 compression = alpha_header & 0x03;
1450
1451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (compression > ALPHA_COMPRESSION_VP8L) {
1452 ✗ av_log(avctx, AV_LOG_VERBOSE,
1453 "skipping unsupported ALPHA chunk\n");
1454 } else {
1455 2 s->has_alpha = 1;
1456 2 s->alpha_compression = compression;
1457 2 s->alpha_filter = filter_m;
1458 }
1459
1460 2 break;
1461 }
1462 2 case MKTAG('E', 'X', 'I', 'F'): {
1463 2 AVBufferRef *exif_buf = NULL;
1464
1465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->has_exif) {
1466 ✗ av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n");
1467 ✗ goto exif_end;
1468 }
1469
1470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA))
1471 ✗ av_log(avctx, AV_LOG_WARNING,
1472 "EXIF chunk present, but Exif bit not set in the "
1473 "VP8X header\n");
1474
1475 2 exif_buf = av_buffer_alloc(chunk_size);
1476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!exif_buf) {
1477 ✗ av_log(avctx, AV_LOG_WARNING, "unable to allocate EXIF buffer\n");
1478 ✗ goto exif_end;
1479 }
1480 2 s->has_exif = 1;
1481 2 memcpy(exif_buf->data, gb.buffer, chunk_size);
1482
1483 2 ret = ff_decode_exif_attach_buffer(avctx, p, &exif_buf, AV_EXIF_TIFF_HEADER);
1484
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ret < 0)
1485 ✗ av_log(avctx, AV_LOG_WARNING, "unable to attach EXIF buffer\n");
1486
1487 2 exif_end:
1488 2 bytestream2_skip(&gb, chunk_size);
1489 2 break;
1490 }
1491 ✗ case MKTAG('I', 'C', 'C', 'P'): {
1492 AVFrameSideData *sd;
1493
1494 ✗ if (s->has_iccp) {
1495 ✗ av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra ICCP chunk\n");
1496 ✗ bytestream2_skip(&gb, chunk_size);
1497 ✗ break;
1498 }
1499 ✗ if (!(vp8x_flags & VP8X_FLAG_ICC))
1500 ✗ av_log(avctx, AV_LOG_WARNING,
1501 "ICCP chunk present, but ICC Profile bit not set in the "
1502 "VP8X header\n");
1503
1504 ✗ s->has_iccp = 1;
1505
1506 ✗ ret = ff_frame_new_side_data(avctx, p, AV_FRAME_DATA_ICC_PROFILE, chunk_size, &sd);
1507 ✗ if (ret < 0)
1508 ✗ return ret;
1509
1510 ✗ if (sd) {
1511 ✗ bytestream2_get_buffer(&gb, sd->data, chunk_size);
1512 } else {
1513 ✗ bytestream2_skip(&gb, chunk_size);
1514 }
1515 ✗ break;
1516 }
1517 ✗ case MKTAG('A', 'N', 'I', 'M'):
1518 case MKTAG('A', 'N', 'M', 'F'):
1519 ✗ av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n",
1520 ✗ av_fourcc2str(chunk_type));
1521 ✗ bytestream2_skip(&gb, chunk_size);
1522 ✗ break;
1523 ✗ case MKTAG('X', 'M', 'P', ' '): {
1524 ✗ if (s->has_xmp) {
1525 ✗ av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra XMP chunk\n");
1526 ✗ bytestream2_skip(&gb, chunk_size);
1527 ✗ break;
1528 }
1529 ✗ if (!(vp8x_flags & VP8X_FLAG_XMP_METADATA))
1530 ✗ av_log(avctx, AV_LOG_WARNING,
1531 "XMP chunk present, but XMP bit not set in the "
1532 "VP8X header\n");
1533
1534 ✗ s->has_xmp = 1;
1535
1536 // there are at least chunk_size bytes left to read
1537 ✗ uint8_t *buffer = av_malloc(chunk_size + 1);
1538 ✗ if (!buffer)
1539 ✗ return AVERROR(ENOMEM);
1540
1541 ✗ bytestream2_get_buffer(&gb, buffer, chunk_size);
1542 ✗ buffer[chunk_size] = '\0';
1543
1544 ✗ av_dict_set(&p->metadata, "xmp", buffer, AV_DICT_DONT_STRDUP_VAL);
1545 ✗ break;
1546 }
1547 ✗ default:
1548 ✗ av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
1549 ✗ av_fourcc2str(chunk_type));
1550 ✗ bytestream2_skip(&gb, chunk_size);
1551 ✗ break;
1552 }
1553 }
1554
1555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!*got_frame) {
1556 ✗ av_log(avctx, AV_LOG_ERROR, "image data not found\n");
1557 ✗ return AVERROR_INVALIDDATA;
1558 }
1559
1560 16 return avpkt->size;
1561 }
1562
1563 22 static av_cold int webp_decode_init(AVCodecContext *avctx)
1564 {
1565 22 WebPContext *s = avctx->priv_data;
1566
1567 22 s->pkt = av_packet_alloc();
1568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!s->pkt)
1569 ✗ return AVERROR(ENOMEM);
1570
1571 22 return 0;
1572 }
1573
1574 22 static av_cold int webp_decode_close(AVCodecContext *avctx)
1575 {
1576 22 WebPContext *s = avctx->priv_data;
1577
1578 22 av_packet_free(&s->pkt);
1579
1580
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 14 times.
22 if (s->initialized)
1581 8 return ff_vp8_decode_free(avctx);
1582
1583 14 return 0;
1584 }
1585
1586 const FFCodec ff_webp_decoder = {
1587 .p.name = "webp",
1588 CODEC_LONG_NAME("WebP image"),
1589 .p.type = AVMEDIA_TYPE_VIDEO,
1590 .p.id = AV_CODEC_ID_WEBP,
1591 .priv_data_size = sizeof(WebPContext),
1592 .init = webp_decode_init,
1593 FF_CODEC_DECODE_CB(webp_decode_frame),
1594 .close = webp_decode_close,
1595 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1596 .caps_internal = FF_CODEC_CAP_ICC_PROFILES |
1597 FF_CODEC_CAP_USES_PROGRESSFRAMES,
1598 };
1599
1600 #if CONFIG_WEBP_ANIM_DECODER
1601
1602 #define ANMF_FLAG_DISPOSE (1 << 0)
1603 #define ANMF_FLAG_NO_BLEND (1 << 1)
1604
1605 typedef struct AnimatedWebPContext {
1606 WebPContext w;
1607
1608 AVFrame *canvas; /* AVFrame for canvas */
1609 AVFrame *subframe; /* AVFrame for subframe */
1610 int canvas_width; /* canvas width */
1611 int canvas_height; /* canvas height */
1612 int anmf_flags; /* frame flags from ANMF chunk */
1613 int pos_x; /* frame position X */
1614 int pos_y; /* frame position Y */
1615 int duration; /* frame duration */
1616 int prev_anmf_flags; /* previous frame flags from ANMF chunk */
1617 int prev_width; /* previous frame width */
1618 int prev_height; /* previous frame height */
1619 int prev_pos_x; /* previous frame position X */
1620 int prev_pos_y; /* previous frame position Y */
1621 uint8_t background_argb[4]; /* background color in ARGB format */
1622 uint8_t background_yuva[4]; /* background color in YUVA format */
1623 } AnimatedWebPContext;
1624
1625 /*
1626 * Blend src (foreground) into dst (background), in ARGB format.
1627 * pos_x, pos_y is the position in dst.
1628 */
1629 92 static void blend_alpha_argb(AVFrame *dst, AVFrame *src, int pos_x, int pos_y)
1630 {
1631
2/2
✓ Branch 0 taken 7118 times.
✓ Branch 1 taken 92 times.
7210 for (int y = 0; y < src->height; y++) {
1632 7118 const uint8_t *src_argb = src->data[0] + y * src->linesize[0];
1633 7118 uint8_t *dst_argb = dst->data[0] + (pos_y + y) * dst->linesize[0] + pos_x * sizeof(uint32_t);
1634
2/2
✓ Branch 0 taken 903094 times.
✓ Branch 1 taken 7118 times.
910212 for (int x = 0; x < src->width; x++) {
1635 903094 int src_alpha = src_argb[0];
1636 903094 int dst_alpha = dst_argb[0];
1637
1638
2/2
✓ Branch 0 taken 179241 times.
✓ Branch 1 taken 723853 times.
903094 if (src_alpha == 255) {
1639 179241 memcpy(dst_argb, src_argb, sizeof(uint32_t));
1640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 723853 times.
723853 } else if (src_alpha == 0) {
1641 // no-op
1642 } else {
1643 ✗ int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8;
1644 ✗ int blend_alpha = src_alpha + tmp_alpha;
1645 ✗ int scale = (1UL << 24) / blend_alpha;
1646
1647 ✗ dst_argb[0] = blend_alpha;
1648 ✗ dst_argb[1] = (((uint32_t) (src_argb[1] * src_alpha + dst_argb[1] * tmp_alpha)) * scale) >> 24;
1649 ✗ dst_argb[2] = (((uint32_t) (src_argb[2] * src_alpha + dst_argb[2] * tmp_alpha)) * scale) >> 24;
1650 ✗ dst_argb[3] = (((uint32_t) (src_argb[3] * src_alpha + dst_argb[3] * tmp_alpha)) * scale) >> 24;
1651 }
1652 903094 src_argb += sizeof(uint32_t);
1653 903094 dst_argb += sizeof(uint32_t);
1654 }
1655 }
1656 92 }
1657
1658 /*
1659 * Blend src (foreground) into dst (background), in YUVA format.
1660 * pos_x, pos_y is the position in dst.
1661 */
1662 1 static void blend_alpha_yuva(AVFrame *dst, AVFrame *src, int pos_x, int pos_y)
1663 {
1664 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
1665
1666 1 int plane_y = desc->comp[0].plane;
1667 1 int plane_u = desc->comp[1].plane;
1668 1 int plane_v = desc->comp[2].plane;
1669 1 int plane_a = desc->comp[3].plane;
1670
1671 // blend U & V planes first, because the later step may modify alpha plane
1672
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 times.
61 for (int y = 0; y < AV_CEIL_RSHIFT(src->height, 1); y++) {
1673 60 int tile_h = FFMIN(src->height - y * 2, 2);
1674 60 const uint8_t *src_u = src->data[plane_u] + y * src->linesize[plane_u];
1675 60 const uint8_t *src_v = src->data[plane_v] + y * src->linesize[plane_v];
1676 60 uint8_t *dst_u = dst->data[plane_u] + ((pos_y >> 1) + y) * dst->linesize[plane_u] + (pos_x >> 1);
1677 60 uint8_t *dst_v = dst->data[plane_v] + ((pos_y >> 1) + y) * dst->linesize[plane_v] + (pos_x >> 1);
1678
2/2
✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 60 times.
5940 for (int x = 0; x < AV_CEIL_RSHIFT(src->width, 1); x++) {
1679 5880 int tile_w = FFMIN(src->width - x * 2, 2);
1680 // calculate the average alpha of the tile
1681 5880 int src_alpha = 0;
1682 5880 int dst_alpha = 0;
1683
2/2
✓ Branch 0 taken 11662 times.
✓ Branch 1 taken 5880 times.
17542 for (int yy = 0; yy < tile_h; yy++) {
1684
2/2
✓ Branch 0 taken 23324 times.
✓ Branch 1 taken 11662 times.
34986 for (int xx = 0; xx < tile_w; xx++) {
1685 23324 src_alpha += src->data[plane_a][(y * 2 + yy) * src->linesize[plane_a] +
1686 23324 (x * 2 + xx)];
1687 23324 dst_alpha += dst->data[plane_a][(((pos_y >> 1) + y) * 2 + yy) * dst->linesize[plane_a] +
1688 23324 (((pos_x >> 1) + x) * 2 + xx)];
1689 }
1690 }
1691 5880 int shift = (tile_h == 2) + (tile_w == 2);
1692 5880 src_alpha = AV_CEIL_RSHIFT(src_alpha, shift);
1693 5880 dst_alpha = AV_CEIL_RSHIFT(dst_alpha, shift);
1694
1695
2/2
✓ Branch 0 taken 3592 times.
✓ Branch 1 taken 2288 times.
5880 if (src_alpha == 255) {
1696 3592 *dst_u = *src_u;
1697 3592 *dst_v = *src_v;
1698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2288 times.
2288 } else if (src_alpha == 0) {
1699 // no-op
1700 } else {
1701 ✗ int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8;
1702 ✗ int blend_alpha = src_alpha + tmp_alpha;
1703 ✗ int scale = (1UL << 24) / blend_alpha;
1704 ✗ *dst_u = (((uint32_t) (*src_u * src_alpha + *dst_u * tmp_alpha)) * scale) >> 24;
1705 ✗ *dst_v = (((uint32_t) (*src_v * src_alpha + *dst_v * tmp_alpha)) * scale) >> 24;
1706 }
1707 5880 src_u += 1;
1708 5880 src_v += 1;
1709 5880 dst_u += 1;
1710 5880 dst_v += 1;
1711 }
1712 }
1713
1714 // blend Y & A planes
1715
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 1 times.
120 for (int y = 0; y < src->height; y++) {
1716 119 const uint8_t *src_y = src->data[plane_y] + y * src->linesize[plane_y];
1717 119 const uint8_t *src_a = src->data[plane_a] + y * src->linesize[plane_a];
1718 119 uint8_t *dst_y = dst->data[plane_y] + (pos_y + y) * dst->linesize[plane_y] + pos_x;
1719 119 uint8_t *dst_a = dst->data[plane_a] + (pos_y + y) * dst->linesize[plane_a] + pos_x;
1720
2/2
✓ Branch 0 taken 23324 times.
✓ Branch 1 taken 119 times.
23443 for (int x = 0; x < src->width; x++) {
1721 23324 int src_alpha = *src_a;
1722 23324 int dst_alpha = *dst_a;
1723
1724
2/2
✓ Branch 0 taken 14172 times.
✓ Branch 1 taken 9152 times.
23324 if (src_alpha == 255) {
1725 14172 *dst_y = *src_y;
1726 14172 *dst_a = 255;
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9152 times.
9152 } else if (src_alpha == 0) {
1728 // no-op
1729 } else {
1730 ✗ int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8;
1731 ✗ int blend_alpha = src_alpha + tmp_alpha;
1732 ✗ int scale = (1UL << 24) / blend_alpha;
1733 ✗ *dst_y = (((uint32_t) (*src_y * src_alpha + *dst_y * tmp_alpha)) * scale) >> 24;
1734 ✗ *dst_a = blend_alpha;
1735 }
1736 23324 src_y += 1;
1737 23324 src_a += 1;
1738 23324 dst_y += 1;
1739 23324 dst_a += 1;
1740 }
1741 }
1742 1 }
1743
1744 2203726 static av_always_inline void webp_yuva2argb(uint8_t *out, int Y, int U, int V, int A)
1745 {
1746 // variables used in macros
1747 2203726 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
1748 uint8_t r, g, b;
1749 int y, cb, cr;
1750 int r_add, g_add, b_add;
1751
1752 2203726 YUV_TO_RGB1_CCIR(U, V);
1753 2203726 YUV_TO_RGB2_CCIR(r, g, b, Y);
1754
1755 2203726 out[0] = av_clip_uint8(A);
1756 2203726 out[1] = av_clip_uint8(r);
1757 2203726 out[2] = av_clip_uint8(g);
1758 2203726 out[3] = av_clip_uint8(b);
1759 2203726 }
1760
1761 28 static void copy_yuva2argb(AVFrame *dst, AVFrame *src, int pos_x, int pos_y)
1762 {
1763 28 const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src->format);
1764
1765 28 int alpha = src_desc->nb_components > 3;
1766 28 int plane_y = src_desc->comp[0].plane;
1767 28 int plane_u = src_desc->comp[1].plane;
1768 28 int plane_v = src_desc->comp[2].plane;
1769 28 int plane_a = src_desc->comp[3].plane;
1770
1771
2/2
✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 28 times.
6220 for (int y = 0; y < src->height; y++) {
1772 6192 const uint8_t *src_y = src->data[plane_y] + y * src->linesize[plane_y];
1773 6192 const uint8_t *src_u = src->data[plane_u] + (y >> 1) * src->linesize[plane_u];
1774 6192 const uint8_t *src_v = src->data[plane_v] + (y >> 1) * src->linesize[plane_v];
1775 6192 const uint8_t *src_a = NULL;
1776 6192 uint8_t *dst_argb = dst->data[0] + (pos_y + y) * dst->linesize[0] + pos_x * 4;
1777
2/2
✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 1392 times.
6192 if (alpha)
1778 4800 src_a = src->data[plane_a] + y * src->linesize[plane_a];
1779
1780
2/2
✓ Branch 0 taken 1938576 times.
✓ Branch 1 taken 6192 times.
1944768 for (int x = 0; x < src->width; x++) {
1781
2/2
✓ Branch 0 taken 1536000 times.
✓ Branch 1 taken 402576 times.
1938576 webp_yuva2argb(dst_argb, *src_y, *src_u, *src_v, (alpha ? *src_a : 255));
1782 1938576 src_y += 1;
1783 1938576 src_u += x & 1;
1784 1938576 src_v += x & 1;
1785
2/2
✓ Branch 0 taken 1536000 times.
✓ Branch 1 taken 402576 times.
1938576 if (alpha)
1786 1536000 src_a += 1;
1787 1938576 dst_argb += sizeof(uint32_t);
1788 }
1789 }
1790 28 }
1791
1792 13 static void blend_yuva2argb(AVFrame *dst, AVFrame *src, int pos_x, int pos_y)
1793 {
1794 13 const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src->format);
1795
1796 13 int plane_y = src_desc->comp[0].plane;
1797 13 int plane_u = src_desc->comp[1].plane;
1798 13 int plane_v = src_desc->comp[2].plane;
1799 13 int plane_a = src_desc->comp[3].plane;
1800
1801
2/2
✓ Branch 0 taken 1592 times.
✓ Branch 1 taken 13 times.
1605 for (int y = 0; y < src->height; y++) {
1802 1592 const uint8_t *src_y = src->data[plane_y] + y * src->linesize[plane_y];
1803 1592 const uint8_t *src_u = src->data[plane_u] + (y >> 1) * src->linesize[plane_u];
1804 1592 const uint8_t *src_v = src->data[plane_v] + (y >> 1) * src->linesize[plane_v];
1805 1592 const uint8_t *src_a = src->data[plane_a] + y * src->linesize[plane_a];
1806 1592 uint8_t *dst_argb = dst->data[0] + (pos_y + y) * dst->linesize[0] + pos_x * 4;
1807
1808
2/2
✓ Branch 0 taken 374206 times.
✓ Branch 1 taken 1592 times.
375798 for (int x = 0; x < src->width; x++) {
1809 374206 int src_alpha = *src_a;
1810 374206 int dst_alpha = dst_argb[0];
1811
1812
2/2
✓ Branch 0 taken 265150 times.
✓ Branch 1 taken 109056 times.
374206 if (src_alpha == 255) {
1813 265150 webp_yuva2argb(dst_argb, *src_y, *src_u, *src_v, src_alpha);
1814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109056 times.
109056 } else if (src_alpha == 0) {
1815 // no-op
1816 } else {
1817 uint8_t tmp[4];
1818 ✗ int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8;
1819 ✗ int blend_alpha = src_alpha + tmp_alpha;
1820 ✗ int scale = (1UL << 24) / blend_alpha;
1821
1822 ✗ webp_yuva2argb(tmp, *src_y, *src_u, *src_v, src_alpha);
1823
1824 ✗ dst_argb[0] = blend_alpha;
1825 ✗ dst_argb[1] = (((uint32_t) (tmp[1] * src_alpha + dst_argb[1] * tmp_alpha)) * scale) >> 24;
1826 ✗ dst_argb[2] = (((uint32_t) (tmp[2] * src_alpha + dst_argb[2] * tmp_alpha)) * scale) >> 24;
1827 ✗ dst_argb[3] = (((uint32_t) (tmp[3] * src_alpha + dst_argb[3] * tmp_alpha)) * scale) >> 24;
1828 }
1829
1830 374206 src_y += 1;
1831 374206 src_u += x & 1;
1832 374206 src_v += x & 1;
1833 374206 src_a += 1;
1834 374206 dst_argb += sizeof(uint32_t);
1835 }
1836 }
1837 13 }
1838
1839 136 static int blend_subframe_into_canvas(AnimatedWebPContext *s)
1840 {
1841 136 AVFrame *canvas = s->canvas;
1842 136 AVFrame *frame = s->subframe;
1843
1844
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 22 times.
136 if ((s->anmf_flags & ANMF_FLAG_NO_BLEND)
1845
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 106 times.
114 || frame->format == AV_PIX_FMT_YUV420P) {
1846 // do not blend, overwrite
1847
1848
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 21 times.
30 if (canvas->format == AV_PIX_FMT_ARGB) {
1849
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (canvas->format == frame->format) {
1850 1 const uint8_t *src = frame->data[0];
1851 1 uint8_t *dst = canvas->data[0] +
1852 1 s->pos_y * canvas->linesize[0] +
1853 1 s->pos_x * sizeof(uint32_t);
1854
2/2
✓ Branch 0 taken 347 times.
✓ Branch 1 taken 1 times.
348 for (int y = 0; y < s->w.height; y++) {
1855 347 memcpy(dst, src, s->w.width * sizeof(uint32_t));
1856 347 src += frame->linesize[0];
1857 347 dst += canvas->linesize[0];
1858 }
1859 } else {
1860 8 copy_yuva2argb(canvas, frame, s->pos_x, s->pos_y);
1861 }
1862 } else /* if (canvas->format == AV_PIX_FMT_YUVA420P) */ {
1863 21 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1864
1865
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
84 for (int comp = 0; comp < desc->nb_components; comp++) {
1866 63 int plane = desc->comp[comp].plane;
1867
4/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 21 times.
63 int shift = (comp == 1 || comp == 2) ? 1 : 0;
1868 63 const uint8_t *src = frame->data[plane];
1869 63 uint8_t *dst = canvas->data[plane] +
1870 63 (s->pos_y >> shift) * canvas->linesize[plane] +
1871 63 (s->pos_x >> shift);
1872
2/2
✓ Branch 0 taken 10080 times.
✓ Branch 1 taken 63 times.
10143 for (int y = 0; y < AV_CEIL_RSHIFT(s->w.height, shift); y++) {
1873 10080 memcpy(dst, src, AV_CEIL_RSHIFT(s->w.width, shift));
1874 10080 src += frame->linesize[plane];
1875 10080 dst += canvas->linesize[plane];
1876 }
1877 }
1878
1879
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 if (canvas->format == AV_PIX_FMT_YUVA420P && desc->nb_components < 4) {
1880 // frame does not have alpha, set alpha to 255
1881 21 const AVPixFmtDescriptor *canvas_desc = av_pix_fmt_desc_get(canvas->format);
1882 21 int plane = canvas_desc->comp[3].plane;
1883 21 uint8_t *dst = canvas->data[plane] + s->pos_y * canvas->linesize[plane] + s->pos_x;
1884
2/2
✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 21 times.
5061 for (int y = 0; y < s->w.height; y++) {
1885 5040 memset(dst, 255, s->w.width);
1886 5040 dst += canvas->linesize[plane];
1887 }
1888 }
1889 }
1890 } else {
1891 // alpha blending
1892
1893
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 1 times.
106 if (canvas->format == AV_PIX_FMT_ARGB) {
1894
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 13 times.
105 if (canvas->format == frame->format) {
1895 92 blend_alpha_argb(canvas, frame, s->pos_x, s->pos_y);
1896 } else {
1897 13 blend_yuva2argb(canvas, frame, s->pos_x, s->pos_y);
1898 }
1899 } else /* if (canvas->format == AV_PIX_FMT_YUVA420P) */ {
1900 1 blend_alpha_yuva(canvas, frame, s->pos_x, s->pos_y);
1901 }
1902 }
1903
1904 136 return 0;
1905 }
1906
1907 /**
1908 * Fill a rectangle on the canvas with the background color (transparent black
1909 * by default, or the color from the ANIM chunk if provided by the demuxer).
1910 */
1911 46 static void fill_canvas_rect(AnimatedWebPContext *s, int pos_x, int pos_y, int width, int height)
1912 {
1913 46 AVFrame *canvas = s->canvas;
1914
1915
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 21 times.
46 if (canvas->format == AV_PIX_FMT_ARGB) {
1916 25 uint32_t bg_color = AV_RN32(s->background_argb);
1917 25 int is_repeatable = (bg_color == ((bg_color & 0xff) * 0x01010101));
1918
2/2
✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 25 times.
3025 for (int y = 0; y < height; y++) {
1919 3000 uint32_t *dst = (uint32_t *) (canvas->data[0] + (pos_y + y) * canvas->linesize[0]) + pos_x;
1920
1/2
✓ Branch 0 taken 3000 times.
✗ Branch 1 not taken.
3000 if (is_repeatable) {
1921 3000 memset(dst, bg_color, width * sizeof(uint32_t));
1922 } else {
1923 ✗ for (int x = 0; x < width; x++)
1924 ✗ dst[x] = bg_color;
1925 }
1926 }
1927 } else /* if (canvas->format == AV_PIX_FMT_YUVA420P) */ {
1928 21 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(canvas->format);
1929
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 21 times.
105 for (int comp = 0; comp < desc->nb_components; comp++) {
1930
4/4
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 42 times.
84 int shift = (comp == 1 || comp == 2) ? 1 : 0;
1931 84 int plane = desc->comp[comp].plane;
1932 84 uint8_t *dst = canvas->data[plane] + (pos_y >> shift) * canvas->linesize[plane] + (pos_x >> shift);
1933
2/2
✓ Branch 0 taken 15120 times.
✓ Branch 1 taken 84 times.
15204 for (int y = 0; y < AV_CEIL_RSHIFT(height, shift); y++) {
1934 15120 memset(dst, s->background_yuva[plane], AV_CEIL_RSHIFT(width, shift));
1935 15120 dst += canvas->linesize[plane];
1936 }
1937 }
1938 }
1939 46 }
1940
1941 43 static int allocate_canvas(AnimatedWebPContext *s, int format)
1942 {
1943 43 s->w.avctx->pix_fmt = format;
1944 43 int ret = ff_set_dimensions(s->w.avctx, s->canvas_width, s->canvas_height);
1945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
1946 ✗ return ret;
1947 43 return ff_reget_buffer(s->w.avctx, s->canvas, 0);
1948 }
1949
1950 136 static int prepare_canvas(AnimatedWebPContext *s, int key_frame, int format)
1951 {
1952 int ret;
1953
1954 /**
1955 * Clear the canvas on keyframes and frames that overwrite the entire
1956 * canvas.
1957 */
1958
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 3 times.
136 if (key_frame ||
1959
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 113 times.
133 ((s->anmf_flags & ANMF_FLAG_NO_BLEND) &&
1960
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 (s->pos_x == 0) && (s->pos_x + s->w.width == s->canvas_width) &&
1961
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 (s->pos_y == 0) && (s->pos_y + s->w.height == s->canvas_height)))
1962 23 av_frame_unref(s->canvas);
1963
1964
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 113 times.
136 if (!s->canvas->buf[0]) {
1965 /* Allocate new canvas frame */
1966 23 ret = allocate_canvas(s, format);
1967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
1968 ✗ return ret;
1969 /* ... and initialize it. */
1970 23 fill_canvas_rect(s, 0, 0, s->canvas->width, s->canvas->height);
1971 } else {
1972
4/4
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 71 times.
133 if (format == AV_PIX_FMT_ARGB && s->canvas->format == AV_PIX_FMT_YUVA420P) {
1973 /**
1974 * If we have a lossless frame following a lossy frame, we upgrade
1975 * the canvas to ARGB, but we don't convert the canvas back to YUVA
1976 * if there is a lossy frame following a lossless frame.
1977 */
1978 20 AVFrame *yuva_canvas = av_frame_clone(s->canvas);
1979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!yuva_canvas)
1980 ✗ return AVERROR(ENOMEM);
1981 20 av_frame_unref(s->canvas);
1982 20 ret = allocate_canvas(s, AV_PIX_FMT_ARGB);
1983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0) {
1984 ✗ av_frame_free(&yuva_canvas);
1985 ✗ return ret;
1986 }
1987 20 copy_yuva2argb(s->canvas, yuva_canvas, 0, 0);
1988 20 av_frame_free(&yuva_canvas);
1989 } else {
1990 /**
1991 * The decode frame function returns a reference to the canvas,
1992 * therefore we have to ensure it is writable before using it
1993 * for a new frame.
1994 */
1995 93 ret = av_frame_make_writable(s->canvas);
1996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
1997 ✗ return ret;
1998 }
1999 /* Dispose of previous frame if needed. */
2000
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 90 times.
113 if (s->prev_anmf_flags & ANMF_FLAG_DISPOSE)
2001 23 fill_canvas_rect(s, s->prev_pos_x, s->prev_pos_y, s->prev_width, s->prev_height);
2002 }
2003
2004 136 return 0;
2005 }
2006
2007 136 static int webp_anim_decode_frame(AVCodecContext *avctx, AVFrame *p,
2008 int *got_frame, AVPacket *avpkt)
2009 {
2010 136 AnimatedWebPContext *s = avctx->priv_data;
2011 136 int key_frame = (avpkt->flags & AV_PKT_FLAG_KEY);
2012 int ret;
2013
2014 GetByteContext gb;
2015 136 bytestream2_init(&gb, avpkt->data, avpkt->size);
2016
2017 /* Parse ANMF header. */
2018 136 s->pos_x = bytestream2_get_le24(&gb) * 2;
2019 136 s->pos_y = bytestream2_get_le24(&gb) * 2;
2020 136 s->w.width = bytestream2_get_le24(&gb) + 1;
2021 136 s->w.height = bytestream2_get_le24(&gb) + 1;
2022 136 s->duration = bytestream2_get_le24(&gb);
2023 136 s->anmf_flags = bytestream2_get_byte(&gb);
2024
2025 136 av_log(avctx, AV_LOG_DEBUG,
2026 "ANMF frame pos: %dx%d size: %dx%d duration: %d\n",
2027 s->pos_x, s->pos_y, s->w.width, s->w.height, s->duration);
2028
2029 /* Reset alpha field from previous frame. */
2030 136 s->w.has_alpha = 0;
2031
2032 /* Parse ANMF subchunks. */
2033
2/2
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 136 times.
286 while (bytestream2_get_bytes_left(&gb) > 8) {
2034 150 uint32_t chunk_type = bytestream2_get_le32(&gb);
2035 150 uint32_t chunk_size = bytestream2_get_le32(&gb);
2036
2037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (chunk_size == UINT32_MAX) {
2038 ✗ ret = AVERROR_INVALIDDATA;
2039 ✗ goto end;
2040 }
2041 150 chunk_size += chunk_size & 1;
2042
2043
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
150 if (bytestream2_get_bytes_left(&gb) < chunk_size) {
2044 /* we seem to be running out of data, but it could also be that the
2045 * bitstream has trailing junk leading to bogus chunk_size. */
2046 ✗ break;
2047 }
2048
2049
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
150 switch (chunk_type) {
2050 14 case MKTAG('A', 'L', 'P', 'H'): {
2051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (chunk_size == 0) {
2052 ✗ av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n");
2053 ✗ ret = AVERROR_INVALIDDATA;
2054 ✗ goto end;
2055 }
2056 14 int alpha_header = bytestream2_get_byte(&gb);
2057 14 s->w.alpha_data = avpkt->data + bytestream2_tell(&gb);
2058 14 s->w.alpha_data_size = chunk_size - 1;
2059 14 bytestream2_skip(&gb, s->w.alpha_data_size);
2060
2061 14 int filter_m = (alpha_header >> 2) & 0x03;
2062 14 int compression = alpha_header & 0x03;
2063
2064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (compression > ALPHA_COMPRESSION_VP8L) {
2065 ✗ av_log(avctx, AV_LOG_VERBOSE,
2066 "skipping unsupported ALPHA chunk\n");
2067 } else {
2068 14 s->w.has_alpha = 1;
2069 14 s->w.alpha_compression = compression;
2070 14 s->w.alpha_filter = filter_m;
2071 }
2072
2073 150 break;
2074 }
2075 43 case MKTAG('V', 'P', '8', ' '):
2076
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (*got_frame) {
2077 ✗ av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra VP8 chunk\n");
2078 ✗ bytestream2_skip(&gb, chunk_size);
2079 ✗ break;
2080 }
2081 43 ret = vp8_lossy_decode_frame(avctx, s->subframe, got_frame,
2082 43 avpkt->data + bytestream2_tell(&gb),
2083 chunk_size);
2084
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
2085 ✗ goto end;
2086 43 ret = prepare_canvas(s, key_frame, AV_PIX_FMT_YUVA420P);
2087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
2088 ✗ goto end;
2089 43 bytestream2_skip(&gb, chunk_size);
2090 43 break;
2091 93 case MKTAG('V', 'P', '8', 'L'):
2092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (*got_frame) {
2093 ✗ av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra VP8L chunk\n");
2094 ✗ bytestream2_skip(&gb, chunk_size);
2095 ✗ break;
2096 }
2097 93 ret = vp8_lossless_decode_frame(avctx, s->subframe, got_frame,
2098 93 avpkt->data + bytestream2_tell(&gb),
2099 chunk_size, 0);
2100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
2101 ✗ goto end;
2102 93 ret = prepare_canvas(s, key_frame, AV_PIX_FMT_ARGB);
2103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0)
2104 ✗ goto end;
2105 93 bytestream2_skip(&gb, chunk_size);
2106 93 break;
2107 ✗ default:
2108 ✗ av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
2109 ✗ av_fourcc2str(chunk_type));
2110 ✗ bytestream2_skip(&gb, chunk_size);
2111 ✗ break;
2112 }
2113 }
2114
2115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (!*got_frame) {
2116 ✗ av_log(avctx, AV_LOG_ERROR, "image data not found\n");
2117 ✗ ret = AVERROR_INVALIDDATA;
2118 ✗ goto end;
2119 }
2120
2121 /* The subframe dimensions may have been modified by update_canvas_size() */
2122
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (s->pos_x + s->w.width > s->canvas_width ||
2123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 s->pos_y + s->w.height > s->canvas_height) {
2124 ✗ av_log(avctx, AV_LOG_ERROR,
2125 "Frame (%dx%d at pos %dx%d) does not fit into canvas (%dx%d)\n",
2126 s->w.width, s->w.height, s->pos_x, s->pos_y,
2127 s->canvas_width, s->canvas_height);
2128 ✗ ret = AVERROR_INVALIDDATA;
2129 ✗ goto end;
2130 }
2131
2132 136 ret = blend_subframe_into_canvas(s);
2133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (ret < 0)
2134 ✗ goto end;
2135
2136 136 ret = av_frame_ref(p, s->canvas);
2137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (ret < 0)
2138 ✗ goto end;
2139
2140
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 133 times.
136 p->pict_type = key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
2141 136 p->pts = avpkt->pts;
2142 136 p->duration = s->duration;
2143
2144 136 s->prev_anmf_flags = s->anmf_flags;
2145 136 s->prev_width = s->w.width;
2146 136 s->prev_height = s->w.height;
2147 136 s->prev_pos_x = s->pos_x;
2148 136 s->prev_pos_y = s->pos_y;
2149
2150 136 ret = avpkt->size;
2151
2152 136 end:
2153 136 av_frame_unref(s->subframe);
2154 136 return ret;
2155 }
2156
2157 6 static av_cold int webp_anim_decode_init(AVCodecContext *avctx)
2158 {
2159 6 AnimatedWebPContext *s = avctx->priv_data;
2160
2161 6 s->w.avctx = avctx;
2162 6 s->canvas_width = avctx->width;
2163 6 s->canvas_height = avctx->height;
2164
2165 6 s->canvas = av_frame_alloc();
2166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!s->canvas)
2167 ✗ return AVERROR(ENOMEM);
2168
2169 6 s->subframe = av_frame_alloc();
2170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!s->subframe)
2171 ✗ return AVERROR(ENOMEM);
2172
2173 /**
2174 * Use background color if it was provided by the demuxer. Otherwise, the
2175 * background color will be 0x00000000 (transparent black).
2176 */
2177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (avctx->extradata_size >= 4) {
2178 ✗ s->background_argb[0] = avctx->extradata[3];
2179 ✗ s->background_argb[1] = avctx->extradata[2];
2180 ✗ s->background_argb[2] = avctx->extradata[1];
2181 ✗ s->background_argb[3] = avctx->extradata[0];
2182 }
2183
2184 /* Convert background color to YUVA. */
2185 6 const uint8_t *argb = s->background_argb;
2186 6 s->background_yuva[0] = RGB_TO_Y_CCIR(argb[1], argb[2], argb[3]);
2187 6 s->background_yuva[1] = RGB_TO_U_CCIR(argb[1], argb[2], argb[3], 0);
2188 6 s->background_yuva[2] = RGB_TO_V_CCIR(argb[1], argb[2], argb[3], 0);
2189 6 s->background_yuva[3] = argb[0];
2190
2191 6 return webp_decode_init(avctx);
2192 }
2193
2194 6 static av_cold int webp_anim_decode_close(AVCodecContext *avctx)
2195 {
2196 6 AnimatedWebPContext *s = avctx->priv_data;
2197
2198 6 av_frame_free(&s->canvas);
2199 6 av_frame_free(&s->subframe);
2200
2201 6 return webp_decode_close(avctx);
2202 }
2203
2204 const FFCodec ff_webp_anim_decoder = {
2205 .p.name = "webp_anim",
2206 CODEC_LONG_NAME("Animated WebP image"),
2207 .p.type = AVMEDIA_TYPE_VIDEO,
2208 .p.id = AV_CODEC_ID_WEBP_ANIM,
2209 .priv_data_size = sizeof(AnimatedWebPContext),
2210 .init = webp_anim_decode_init,
2211 FF_CODEC_DECODE_CB(webp_anim_decode_frame),
2212 .close = webp_anim_decode_close,
2213 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
2214 .caps_internal = FF_CODEC_CAP_USES_PROGRESSFRAMES,
2215 };
2216 #endif /* CONFIG_WEBP_ANIM_DECODER */
2217