FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/webp.c
Date: 2022-01-21 12:56:39
Exec Total Coverage
Lines: 611 799 76.5%
Branches: 257 391 65.7%

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 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * WebP image decoder
26 *
27 * @author Aneesh Dogra <aneesh@sugarlabs.org>
28 * Container and Lossy decoding
29 *
30 * @author Justin Ruggles <justin.ruggles@gmail.com>
31 * Lossless decoder
32 * Compressed alpha for lossy
33 *
34 * @author James Almer <jamrial@gmail.com>
35 * Exif metadata
36 * ICC profile
37 *
38 * Unimplemented:
39 * - Animation
40 * - XMP metadata
41 */
42
43 #include "libavutil/imgutils.h"
44
45 #define BITSTREAM_READER_LE
46 #include "avcodec.h"
47 #include "bytestream.h"
48 #include "exif.h"
49 #include "get_bits.h"
50 #include "internal.h"
51 #include "thread.h"
52 #include "vp8.h"
53
54 #define VP8X_FLAG_ANIMATION 0x02
55 #define VP8X_FLAG_XMP_METADATA 0x04
56 #define VP8X_FLAG_EXIF_METADATA 0x08
57 #define VP8X_FLAG_ALPHA 0x10
58 #define VP8X_FLAG_ICC 0x20
59
60 #define MAX_PALETTE_SIZE 256
61 #define MAX_CACHE_BITS 11
62 #define NUM_CODE_LENGTH_CODES 19
63 #define HUFFMAN_CODES_PER_META_CODE 5
64 #define NUM_LITERAL_CODES 256
65 #define NUM_LENGTH_CODES 24
66 #define NUM_DISTANCE_CODES 40
67 #define NUM_SHORT_DISTANCES 120
68 #define MAX_HUFFMAN_CODE_LENGTH 15
69
70 static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE] = {
71 NUM_LITERAL_CODES + NUM_LENGTH_CODES,
72 NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
73 NUM_DISTANCE_CODES
74 };
75
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
78 };
79
80 static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2] = {
81 { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
82 { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
83 { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
84 { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
85 { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
86 { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
87 { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
88 { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
89 { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
90 { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
91 { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
92 { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
93 { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
94 { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
95 { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
96 };
97
98 enum AlphaCompression {
99 ALPHA_COMPRESSION_NONE,
100 ALPHA_COMPRESSION_VP8L,
101 };
102
103 enum AlphaFilter {
104 ALPHA_FILTER_NONE,
105 ALPHA_FILTER_HORIZONTAL,
106 ALPHA_FILTER_VERTICAL,
107 ALPHA_FILTER_GRADIENT,
108 };
109
110 enum TransformType {
111 PREDICTOR_TRANSFORM = 0,
112 COLOR_TRANSFORM = 1,
113 SUBTRACT_GREEN = 2,
114 COLOR_INDEXING_TRANSFORM = 3,
115 };
116
117 enum PredictionMode {
118 PRED_MODE_BLACK,
119 PRED_MODE_L,
120 PRED_MODE_T,
121 PRED_MODE_TR,
122 PRED_MODE_TL,
123 PRED_MODE_AVG_T_AVG_L_TR,
124 PRED_MODE_AVG_L_TL,
125 PRED_MODE_AVG_L_T,
126 PRED_MODE_AVG_TL_T,
127 PRED_MODE_AVG_T_TR,
128 PRED_MODE_AVG_AVG_L_TL_AVG_T_TR,
129 PRED_MODE_SELECT,
130 PRED_MODE_ADD_SUBTRACT_FULL,
131 PRED_MODE_ADD_SUBTRACT_HALF,
132 };
133
134 enum HuffmanIndex {
135 HUFF_IDX_GREEN = 0,
136 HUFF_IDX_RED = 1,
137 HUFF_IDX_BLUE = 2,
138 HUFF_IDX_ALPHA = 3,
139 HUFF_IDX_DIST = 4
140 };
141
142 /* The structure of WebP lossless is an optional series of transformation data,
143 * followed by the primary image. The primary image also optionally contains
144 * an entropy group mapping if there are multiple entropy groups. There is a
145 * basic image type called an "entropy coded image" that is used for all of
146 * these. The type of each entropy coded image is referred to by the
147 * specification as its role. */
148 enum ImageRole {
149 /* Primary Image: Stores the actual pixels of the image. */
150 IMAGE_ROLE_ARGB,
151
152 /* Entropy Image: Defines which Huffman group to use for different areas of
153 * the primary image. */
154 IMAGE_ROLE_ENTROPY,
155
156 /* Predictors: Defines which predictor type to use for different areas of
157 * the primary image. */
158 IMAGE_ROLE_PREDICTOR,
159
160 /* Color Transform Data: Defines the color transformation for different
161 * areas of the primary image. */
162 IMAGE_ROLE_COLOR_TRANSFORM,
163
164 /* Color Index: Stored as an image of height == 1. */
165 IMAGE_ROLE_COLOR_INDEXING,
166
167 IMAGE_ROLE_NB,
168 };
169
170 typedef struct HuffReader {
171 VLC vlc; /* Huffman decoder context */
172 int simple; /* whether to use simple mode */
173 int nb_symbols; /* number of coded symbols */
174 uint16_t simple_symbols[2]; /* symbols for simple mode */
175 } HuffReader;
176
177 typedef struct ImageContext {
178 enum ImageRole role; /* role of this image */
179 AVFrame *frame; /* AVFrame for data */
180 int color_cache_bits; /* color cache size, log2 */
181 uint32_t *color_cache; /* color cache data */
182 int nb_huffman_groups; /* number of huffman groups */
183 HuffReader *huffman_groups; /* reader for each huffman group */
184 /* relative size compared to primary image, log2.
185 * for IMAGE_ROLE_COLOR_INDEXING with <= 16 colors, this is log2 of the
186 * number of pixels per byte in the primary image (pixel packing) */
187 int size_reduction;
188 int is_alpha_primary;
189 } ImageContext;
190
191 typedef struct WebPContext {
192 VP8Context v; /* VP8 Context used for lossy decoding */
193 GetBitContext gb; /* bitstream reader for main image chunk */
194 AVFrame *alpha_frame; /* AVFrame for alpha data decompressed from VP8L */
195 AVPacket *pkt; /* AVPacket to be passed to the underlying VP8 decoder */
196 AVCodecContext *avctx; /* parent AVCodecContext */
197 int initialized; /* set once the VP8 context is initialized */
198 int has_alpha; /* has a separate alpha chunk */
199 enum AlphaCompression alpha_compression; /* compression type for alpha chunk */
200 enum AlphaFilter alpha_filter; /* filtering method for alpha chunk */
201 uint8_t *alpha_data; /* alpha chunk data */
202 int alpha_data_size; /* alpha chunk data size */
203 int has_exif; /* set after an EXIF chunk has been processed */
204 int has_iccp; /* set after an ICCP chunk has been processed */
205 int width; /* image width */
206 int height; /* image height */
207 int lossless; /* indicates lossless or lossy */
208
209 int nb_transforms; /* number of transforms */
210 enum TransformType transforms[4]; /* transformations used in the image, in order */
211 /* reduced width when using a color indexing transform with <= 16 colors (pixel packing)
212 * before pixels are unpacked, or same as width otherwise. */
213 int reduced_width;
214 int nb_huffman_groups; /* number of huffman groups in the primary image */
215 ImageContext image[IMAGE_ROLE_NB]; /* image context for each role */
216 } WebPContext;
217
218 #define GET_PIXEL(frame, x, y) \
219 ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
220
221 #define GET_PIXEL_COMP(frame, x, y, c) \
222 (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
223
224 50 static void image_ctx_free(ImageContext *img)
225 {
226 int i, j;
227
228 50 av_free(img->color_cache);
229
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
50 if (img->role != IMAGE_ROLE_ARGB && !img->is_alpha_primary)
230 18 av_frame_free(&img->frame);
231
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 22 times.
50 if (img->huffman_groups) {
232
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 28 times.
64 for (i = 0; i < img->nb_huffman_groups; i++) {
233
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 36 times.
216 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++)
234 180 ff_free_vlc(&img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE + j].vlc);
235 }
236 28 av_free(img->huffman_groups);
237 }
238 50 memset(img, 0, sizeof(*img));
239 50 }
240
241 277908 static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
242 {
243
2/2
✓ Branch 0 taken 72022 times.
✓ Branch 1 taken 205886 times.
277908 if (r->simple) {
244
2/2
✓ Branch 0 taken 71710 times.
✓ Branch 1 taken 312 times.
72022 if (r->nb_symbols == 1)
245 71710 return r->simple_symbols[0];
246 else
247 312 return r->simple_symbols[get_bits1(gb)];
248 } else
249 205886 return get_vlc2(gb, r->vlc.table, 8, 2);
250 }
251
252 156 static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths,
253 int alphabet_size)
254 {
255 156 int len = 0, sym, code = 0, ret;
256 156 int max_code_length = 0;
257 uint16_t *codes;
258
259 /* special-case 1 symbol since the vlc reader cannot handle it */
260
1/2
✓ Branch 0 taken 706 times.
✗ Branch 1 not taken.
706 for (sym = 0; sym < alphabet_size; sym++) {
261
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 394 times.
706 if (code_lengths[sym] > 0) {
262 312 len++;
263 312 code = sym;
264
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 156 times.
312 if (len > 1)
265 156 break;
266 }
267 }
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (len == 1) {
269 r->nb_symbols = 1;
270 r->simple_symbols[0] = code;
271 r->simple = 1;
272 return 0;
273 }
274
275
2/2
✓ Branch 0 taken 22314 times.
✓ Branch 1 taken 156 times.
22470 for (sym = 0; sym < alphabet_size; sym++)
276 22314 max_code_length = FFMAX(max_code_length, code_lengths[sym]);
277
278
2/4
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 156 times.
156 if (max_code_length == 0 || max_code_length > MAX_HUFFMAN_CODE_LENGTH)
279 return AVERROR(EINVAL);
280
281 156 codes = av_malloc_array(alphabet_size, sizeof(*codes));
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (!codes)
283 return AVERROR(ENOMEM);
284
285 156 code = 0;
286 156 r->nb_symbols = 0;
287
2/2
✓ Branch 0 taken 1070 times.
✓ Branch 1 taken 156 times.
1226 for (len = 1; len <= max_code_length; len++) {
288
2/2
✓ Branch 0 taken 195560 times.
✓ Branch 1 taken 1070 times.
196630 for (sym = 0; sym < alphabet_size; sym++) {
289
2/2
✓ Branch 0 taken 188272 times.
✓ Branch 1 taken 7288 times.
195560 if (code_lengths[sym] != len)
290 188272 continue;
291 7288 codes[sym] = code++;
292 7288 r->nb_symbols++;
293 }
294 1070 code <<= 1;
295 }
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (!r->nb_symbols) {
297 av_free(codes);
298 return AVERROR_INVALIDDATA;
299 }
300
301 156 ret = init_vlc(&r->vlc, 8, alphabet_size,
302 code_lengths, sizeof(*code_lengths), sizeof(*code_lengths),
303 codes, sizeof(*codes), sizeof(*codes), INIT_VLC_OUTPUT_LE);
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (ret < 0) {
305 av_free(codes);
306 return ret;
307 }
308 156 r->simple = 0;
309
310 156 av_free(codes);
311 156 return 0;
312 }
313
314 102 static void read_huffman_code_simple(WebPContext *s, HuffReader *hc)
315 {
316 102 hc->nb_symbols = get_bits1(&s->gb) + 1;
317
318
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 88 times.
102 if (get_bits1(&s->gb))
319 14 hc->simple_symbols[0] = get_bits(&s->gb, 8);
320 else
321 88 hc->simple_symbols[0] = get_bits1(&s->gb);
322
323
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 92 times.
102 if (hc->nb_symbols == 2)
324 10 hc->simple_symbols[1] = get_bits(&s->gb, 8);
325
326 102 hc->simple = 1;
327 102 }
328
329 78 static int read_huffman_code_normal(WebPContext *s, HuffReader *hc,
330 int alphabet_size)
331 {
332 78 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
333 uint8_t *code_lengths;
334 78 uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
335 int i, symbol, max_symbol, prev_code_len, ret;
336 78 int num_codes = 4 + get_bits(&s->gb, 4);
337
338 av_assert1(num_codes <= NUM_CODE_LENGTH_CODES);
339
340
2/2
✓ Branch 0 taken 1076 times.
✓ Branch 1 taken 78 times.
1154 for (i = 0; i < num_codes; i++)
341 1076 code_length_code_lengths[code_length_code_order[i]] = get_bits(&s->gb, 3);
342
343 78 ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths,
344 NUM_CODE_LENGTH_CODES);
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
346 return ret;
347
348 78 code_lengths = av_mallocz(alphabet_size);
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!code_lengths) {
350 ret = AVERROR(ENOMEM);
351 goto finish;
352 }
353
354
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 70 times.
78 if (get_bits1(&s->gb)) {
355 8 int bits = 2 + 2 * get_bits(&s->gb, 3);
356 8 max_symbol = 2 + get_bits(&s->gb, bits);
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (max_symbol > alphabet_size) {
358 av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n",
359 max_symbol, alphabet_size);
360 ret = AVERROR_INVALIDDATA;
361 goto finish;
362 }
363 } else {
364 70 max_symbol = alphabet_size;
365 }
366
367 78 prev_code_len = 8;
368 78 symbol = 0;
369
2/2
✓ Branch 0 taken 3152 times.
✓ Branch 1 taken 70 times.
3222 while (symbol < alphabet_size) {
370 int code_len;
371
372
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3144 times.
3152 if (!max_symbol--)
373 8 break;
374 3144 code_len = huff_reader_get_symbol(&code_len_hc, &s->gb);
375
2/2
✓ Branch 0 taken 1880 times.
✓ Branch 1 taken 1264 times.
3144 if (code_len < 16) {
376 /* Code length code [0..15] indicates literal code lengths. */
377 1880 code_lengths[symbol++] = code_len;
378
2/2
✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 120 times.
1880 if (code_len)
379 1760 prev_code_len = code_len;
380 } else {
381 1264 int repeat = 0, length = 0;
382
3/4
✓ Branch 0 taken 868 times.
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
1264 switch (code_len) {
383 868 case 16:
384 /* Code 16 repeats the previous non-zero value [3..6] times,
385 * i.e., 3 + ReadBits(2) times. If code 16 is used before a
386 * non-zero value has been emitted, a value of 8 is repeated. */
387 868 repeat = 3 + get_bits(&s->gb, 2);
388 868 length = prev_code_len;
389 868 break;
390 140 case 17:
391 /* Code 17 emits a streak of zeros [3..10], i.e.,
392 * 3 + ReadBits(3) times. */
393 140 repeat = 3 + get_bits(&s->gb, 3);
394 140 break;
395 256 case 18:
396 /* Code 18 emits a streak of zeros of length [11..138], i.e.,
397 * 11 + ReadBits(7) times. */
398 256 repeat = 11 + get_bits(&s->gb, 7);
399 256 break;
400 }
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1264 times.
1264 if (symbol + repeat > alphabet_size) {
402 av_log(s->avctx, AV_LOG_ERROR,
403 "invalid symbol %d + repeat %d > alphabet size %d\n",
404 symbol, repeat, alphabet_size);
405 ret = AVERROR_INVALIDDATA;
406 goto finish;
407 }
408
2/2
✓ Branch 0 taken 18090 times.
✓ Branch 1 taken 1264 times.
19354 while (repeat-- > 0)
409 18090 code_lengths[symbol++] = length;
410 }
411 }
412
413 78 ret = huff_reader_build_canonical(hc, code_lengths, alphabet_size);
414
415 78 finish:
416 78 ff_free_vlc(&code_len_hc.vlc);
417 78 av_free(code_lengths);
418 78 return ret;
419 }
420
421 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
422 int w, int h);
423
424 #define PARSE_BLOCK_SIZE(w, h) do { \
425 block_bits = get_bits(&s->gb, 3) + 2; \
426 blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
427 blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
428 } while (0)
429
430 4 static int decode_entropy_image(WebPContext *s)
431 {
432 ImageContext *img;
433 int ret, block_bits, blocks_w, blocks_h, x, y, max;
434
435 4 PARSE_BLOCK_SIZE(s->reduced_width, s->height);
436
437 4 ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
439 return ret;
440
441 4 img = &s->image[IMAGE_ROLE_ENTROPY];
442 4 img->size_reduction = block_bits;
443
444 /* the number of huffman groups is determined by the maximum group number
445 * coded in the entropy image */
446 4 max = 0;
447
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4 times.
68 for (y = 0; y < img->frame->height; y++) {
448
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 64 times.
1088 for (x = 0; x < img->frame->width; x++) {
449 1024 int p0 = GET_PIXEL_COMP(img->frame, x, y, 1);
450 1024 int p1 = GET_PIXEL_COMP(img->frame, x, y, 2);
451 1024 int p = p0 << 8 | p1;
452 1024 max = FFMAX(max, p);
453 }
454 }
455 4 s->nb_huffman_groups = max + 1;
456
457 4 return 0;
458 }
459
460 4 static int parse_transform_predictor(WebPContext *s)
461 {
462 int block_bits, blocks_w, blocks_h, ret;
463
464 4 PARSE_BLOCK_SIZE(s->reduced_width, s->height);
465
466 4 ret = decode_entropy_coded_image(s, IMAGE_ROLE_PREDICTOR, blocks_w,
467 blocks_h);
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
469 return ret;
470
471 4 s->image[IMAGE_ROLE_PREDICTOR].size_reduction = block_bits;
472
473 4 return 0;
474 }
475
476 4 static int parse_transform_color(WebPContext *s)
477 {
478 int block_bits, blocks_w, blocks_h, ret;
479
480 4 PARSE_BLOCK_SIZE(s->reduced_width, s->height);
481
482 4 ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_TRANSFORM, blocks_w,
483 blocks_h);
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
485 return ret;
486
487 4 s->image[IMAGE_ROLE_COLOR_TRANSFORM].size_reduction = block_bits;
488
489 4 return 0;
490 }
491
492 6 static int parse_transform_color_indexing(WebPContext *s)
493 {
494 ImageContext *img;
495 int width_bits, index_size, ret, x;
496 uint8_t *ct;
497
498 6 index_size = get_bits(&s->gb, 8) + 1;
499
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (index_size <= 2)
501 width_bits = 3;
502
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 else if (index_size <= 4)
503 2 width_bits = 2;
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 else if (index_size <= 16)
505 width_bits = 1;
506 else
507 4 width_bits = 0;
508
509 6 ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_INDEXING,
510 index_size, 1);
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
512 return ret;
513
514 6 img = &s->image[IMAGE_ROLE_COLOR_INDEXING];
515 6 img->size_reduction = width_bits;
516
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (width_bits > 0)
517 2 s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits;
518
519 /* color index values are delta-coded */
520 6 ct = img->frame->data[0] + 4;
521
2/2
✓ Branch 0 taken 1416 times.
✓ Branch 1 taken 6 times.
1422 for (x = 4; x < img->frame->width * 4; x++, ct++)
522 1416 ct[0] += ct[-4];
523
524 6 return 0;
525 }
526
527 68752 static HuffReader *get_huffman_group(WebPContext *s, ImageContext *img,
528 int x, int y)
529 {
530 68752 ImageContext *gimg = &s->image[IMAGE_ROLE_ENTROPY];
531 68752 int group = 0;
532
533
2/2
✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 3216 times.
68752 if (gimg->size_reduction > 0) {
534 65536 int group_x = x >> gimg->size_reduction;
535 65536 int group_y = y >> gimg->size_reduction;
536 65536 int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1);
537 65536 int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2);
538 65536 group = g0 << 8 | g1;
539 }
540
541 68752 return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE];
542 }
543
544 static av_always_inline void color_cache_put(ImageContext *img, uint32_t c)
545 {
546 uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits);
547 img->color_cache[cache_idx] = c;
548 }
549
550 28 static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role,
551 int w, int h)
552 {
553 ImageContext *img;
554 HuffReader *hg;
555 int i, j, ret, x, y, width;
556
557 28 img = &s->image[role];
558 28 img->role = role;
559
560
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 if (!img->frame) {
561 18 img->frame = av_frame_alloc();
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!img->frame)
563 return AVERROR(ENOMEM);
564 }
565
566 28 img->frame->format = AV_PIX_FMT_ARGB;
567 28 img->frame->width = w;
568 28 img->frame->height = h;
569
570
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 2 times.
36 if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) {
571 8 ThreadFrame pt = { .f = img->frame };
572 8 ret = ff_thread_get_buffer(s->avctx, &pt, 0);
573 } else
574 20 ret = av_frame_get_buffer(img->frame, 1);
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
576 return ret;
577
578
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (get_bits1(&s->gb)) {
579 img->color_cache_bits = get_bits(&s->gb, 4);
580 if (img->color_cache_bits < 1 || img->color_cache_bits > 11) {
581 av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n",
582 img->color_cache_bits);
583 return AVERROR_INVALIDDATA;
584 }
585 img->color_cache = av_calloc(1 << img->color_cache_bits,
586 sizeof(*img->color_cache));
587 if (!img->color_cache)
588 return AVERROR(ENOMEM);
589 } else {
590 28 img->color_cache_bits = 0;
591 }
592
593 28 img->nb_huffman_groups = 1;
594
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 6 times.
28 if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) {
595 4 ret = decode_entropy_image(s);
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
597 return ret;
598 4 img->nb_huffman_groups = s->nb_huffman_groups;
599 }
600 28 img->huffman_groups = av_calloc(img->nb_huffman_groups,
601 HUFFMAN_CODES_PER_META_CODE *
602 sizeof(*img->huffman_groups));
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!img->huffman_groups)
604 return AVERROR(ENOMEM);
605
606
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 28 times.
64 for (i = 0; i < img->nb_huffman_groups; i++) {
607 36 hg = &img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE];
608
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 36 times.
216 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) {
609 180 int alphabet_size = alphabet_sizes[j];
610
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
180 if (!j && img->color_cache_bits > 0)
611 alphabet_size += 1 << img->color_cache_bits;
612
613
2/2
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 78 times.
180 if (get_bits1(&s->gb)) {
614 102 read_huffman_code_simple(s, &hg[j]);
615 } else {
616 78 ret = read_huffman_code_normal(s, &hg[j], alphabet_size);
617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
618 return ret;
619 }
620 }
621 }
622
623 28 width = img->frame->width;
624
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 18 times.
28 if (role == IMAGE_ROLE_ARGB)
625 10 width = s->reduced_width;
626
627 28 x = 0; y = 0;
628
2/2
✓ Branch 0 taken 68752 times.
✓ Branch 1 taken 28 times.
68780 while (y < img->frame->height) {
629 int v;
630
631
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68752 times.
68752 if (get_bits_left(&s->gb) < 0)
632 return AVERROR_INVALIDDATA;
633
634 68752 hg = get_huffman_group(s, img, x, y);
635 68752 v = huff_reader_get_symbol(&hg[HUFF_IDX_GREEN], &s->gb);
636
2/2
✓ Branch 0 taken 68630 times.
✓ Branch 1 taken 122 times.
68752 if (v < NUM_LITERAL_CODES) {
637 /* literal pixel values */
638 68630 uint8_t *p = GET_PIXEL(img->frame, x, y);
639 68630 p[2] = v;
640 68630 p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb);
641 68630 p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb);
642 68630 p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb);
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68630 times.
68630 if (img->color_cache_bits)
644 color_cache_put(img, AV_RB32(p));
645 68630 x++;
646
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 67930 times.
68630 if (x == width) {
647 700 x = 0;
648 700 y++;
649 }
650
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
122 } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) {
651 /* LZ77 backwards mapping */
652 int prefix_code, length, distance, ref_x, ref_y;
653
654 /* parse length and distance */
655 122 prefix_code = v - NUM_LITERAL_CODES;
656
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 74 times.
122 if (prefix_code < 4) {
657 48 length = prefix_code + 1;
658 } else {
659 74 int extra_bits = (prefix_code - 2) >> 1;
660 74 int offset = 2 + (prefix_code & 1) << extra_bits;
661 74 length = offset + get_bits(&s->gb, extra_bits) + 1;
662 }
663 122 prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
122 if (prefix_code > 39U) {
665 av_log(s->avctx, AV_LOG_ERROR,
666 "distance prefix code too large: %d\n", prefix_code);
667 return AVERROR_INVALIDDATA;
668 }
669
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
122 if (prefix_code < 4) {
670 122 distance = prefix_code + 1;
671 } else {
672 int extra_bits = prefix_code - 2 >> 1;
673 int offset = 2 + (prefix_code & 1) << extra_bits;
674 distance = offset + get_bits(&s->gb, extra_bits) + 1;
675 }
676
677 /* find reference location */
678
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
122 if (distance <= NUM_SHORT_DISTANCES) {
679 122 int xi = lz77_distance_offsets[distance - 1][0];
680 122 int yi = lz77_distance_offsets[distance - 1][1];
681 122 distance = FFMAX(1, xi + yi * width);
682 } else {
683 distance -= NUM_SHORT_DISTANCES;
684 }
685 122 ref_x = x;
686 122 ref_y = y;
687
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 86 times.
122 if (distance <= x) {
688 36 ref_x -= distance;
689 36 distance = 0;
690 } else {
691 86 ref_x = 0;
692 86 distance -= x;
693 }
694
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 122 times.
144 while (distance >= width) {
695 22 ref_y--;
696 22 distance -= width;
697 }
698
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 58 times.
122 if (distance > 0) {
699 64 ref_x = width - distance;
700 64 ref_y--;
701 }
702 122 ref_x = FFMAX(0, ref_x);
703 122 ref_y = FFMAX(0, ref_y);
704
705 /* copy pixels
706 * source and dest regions can overlap and wrap lines, so just
707 * copy per-pixel */
708
2/2
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 116 times.
886 for (i = 0; i < length; i++) {
709 770 uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y);
710 770 uint8_t *p = GET_PIXEL(img->frame, x, y);
711
712 770 AV_COPY32(p, p_ref);
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770 times.
770 if (img->color_cache_bits)
714 color_cache_put(img, AV_RB32(p));
715 770 x++;
716 770 ref_x++;
717
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 712 times.
770 if (x == width) {
718 58 x = 0;
719 58 y++;
720 }
721
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 712 times.
770 if (ref_x == width) {
722 58 ref_x = 0;
723 58 ref_y++;
724 }
725
3/4
✓ Branch 0 taken 764 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 764 times.
✗ Branch 3 not taken.
770 if (y == img->frame->height || ref_y == img->frame->height)
726 break;
727 }
728 } else {
729 /* read from color cache */
730 uint8_t *p = GET_PIXEL(img->frame, x, y);
731 int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES);
732
733 if (!img->color_cache_bits) {
734 av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n");
735 return AVERROR_INVALIDDATA;
736 }
737 if (cache_idx >= 1 << img->color_cache_bits) {
738 av_log(s->avctx, AV_LOG_ERROR,
739 "color cache index out-of-bounds\n");
740 return AVERROR_INVALIDDATA;
741 }
742 AV_WB32(p, img->color_cache[cache_idx]);
743 x++;
744 if (x == width) {
745 x = 0;
746 y++;
747 }
748 }
749 }
750
751 28 return 0;
752 }
753
754 /* PRED_MODE_BLACK */
755 4 static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
756 const uint8_t *p_t, const uint8_t *p_tr)
757 {
758 4 AV_WB32(p, 0xFF000000);
759 4 }
760
761 /* PRED_MODE_L */
762 5276 static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
763 const uint8_t *p_t, const uint8_t *p_tr)
764 {
765 5276 AV_COPY32(p, p_l);
766 5276 }
767
768 /* PRED_MODE_T */
769 20764 static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
770 const uint8_t *p_t, const uint8_t *p_tr)
771 {
772 20764 AV_COPY32(p, p_t);
773 20764 }
774
775 /* PRED_MODE_TR */
776 6336 static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
777 const uint8_t *p_t, const uint8_t *p_tr)
778 {
779 6336 AV_COPY32(p, p_tr);
780 6336 }
781
782 /* PRED_MODE_TL */
783 1248 static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
784 const uint8_t *p_t, const uint8_t *p_tr)
785 {
786 1248 AV_COPY32(p, p_tl);
787 1248 }
788
789 /* PRED_MODE_AVG_T_AVG_L_TR */
790 4832 static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
791 const uint8_t *p_t, const uint8_t *p_tr)
792 {
793 4832 p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
794 4832 p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
795 4832 p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
796 4832 p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
797 4832 }
798
799 /* PRED_MODE_AVG_L_TL */
800 768 static void inv_predict_6(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 768 p[0] = p_l[0] + p_tl[0] >> 1;
804 768 p[1] = p_l[1] + p_tl[1] >> 1;
805 768 p[2] = p_l[2] + p_tl[2] >> 1;
806 768 p[3] = p_l[3] + p_tl[3] >> 1;
807 768 }
808
809 /* PRED_MODE_AVG_L_T */
810 3072 static void inv_predict_7(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 3072 p[0] = p_l[0] + p_t[0] >> 1;
814 3072 p[1] = p_l[1] + p_t[1] >> 1;
815 3072 p[2] = p_l[2] + p_t[2] >> 1;
816 3072 p[3] = p_l[3] + p_t[3] >> 1;
817 3072 }
818
819 /* PRED_MODE_AVG_TL_T */
820 4512 static void inv_predict_8(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 4512 p[0] = p_tl[0] + p_t[0] >> 1;
824 4512 p[1] = p_tl[1] + p_t[1] >> 1;
825 4512 p[2] = p_tl[2] + p_t[2] >> 1;
826 4512 p[3] = p_tl[3] + p_t[3] >> 1;
827 4512 }
828
829 /* PRED_MODE_AVG_T_TR */
830 11488 static void inv_predict_9(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 11488 p[0] = p_t[0] + p_tr[0] >> 1;
834 11488 p[1] = p_t[1] + p_tr[1] >> 1;
835 11488 p[2] = p_t[2] + p_tr[2] >> 1;
836 11488 p[3] = p_t[3] + p_tr[3] >> 1;
837 11488 }
838
839 /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */
840 3236 static void inv_predict_10(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 3236 p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
844 3236 p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
845 3236 p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
846 3236 p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
847 3236 }
848
849 /* PRED_MODE_SELECT */
850 3488 static void inv_predict_11(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 3488 int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) +
854 3488 (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) +
855 3488 (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) +
856 3488 (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3]));
857
2/2
✓ Branch 0 taken 2296 times.
✓ Branch 1 taken 1192 times.
3488 if (diff <= 0)
858 2296 AV_COPY32(p, p_t);
859 else
860 1192 AV_COPY32(p, p_l);
861 3488 }
862
863 /* PRED_MODE_ADD_SUBTRACT_FULL */
864 static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
865 const uint8_t *p_t, const uint8_t *p_tr)
866 {
867 p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
868 p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
869 p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
870 p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
871 }
872
873 2048 static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c)
874 {
875 2048 int d = a + b >> 1;
876 2048 return av_clip_uint8(d + (d - c) / 2);
877 }
878
879 /* PRED_MODE_ADD_SUBTRACT_HALF */
880 512 static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl,
881 const uint8_t *p_t, const uint8_t *p_tr)
882 {
883 512 p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]);
884 512 p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]);
885 512 p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]);
886 512 p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]);
887 512 }
888
889 typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l,
890 const uint8_t *p_tl, const uint8_t *p_t,
891 const uint8_t *p_tr);
892
893 static const inv_predict_func inverse_predict[14] = {
894 inv_predict_0, inv_predict_1, inv_predict_2, inv_predict_3,
895 inv_predict_4, inv_predict_5, inv_predict_6, inv_predict_7,
896 inv_predict_8, inv_predict_9, inv_predict_10, inv_predict_11,
897 inv_predict_12, inv_predict_13,
898 };
899
900 65536 static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
901 {
902 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
903 uint8_t p[4];
904
905 65536 dec = GET_PIXEL(frame, x, y);
906 65536 p_l = GET_PIXEL(frame, x - 1, y);
907 65536 p_tl = GET_PIXEL(frame, x - 1, y - 1);
908 65536 p_t = GET_PIXEL(frame, x, y - 1);
909
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 65024 times.
65536 if (x == frame->width - 1)
910 512 p_tr = GET_PIXEL(frame, 0, y);
911 else
912 65024 p_tr = GET_PIXEL(frame, x + 1, y - 1);
913
914 65536 inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
915
916 65536 dec[0] += p[0];
917 65536 dec[1] += p[1];
918 65536 dec[2] += p[2];
919 65536 dec[3] += p[3];
920 65536 }
921
922 4 static int apply_predictor_transform(WebPContext *s)
923 {
924 4 ImageContext *img = &s->image[IMAGE_ROLE_ARGB];
925 4 ImageContext *pimg = &s->image[IMAGE_ROLE_PREDICTOR];
926 int x, y;
927
928
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
516 for (y = 0; y < img->frame->height; y++) {
929
2/2
✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
66048 for (x = 0; x < s->reduced_width; x++) {
930 65536 int tx = x >> pimg->size_reduction;
931 65536 int ty = y >> pimg->size_reduction;
932 65536 enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2);
933
934
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 65024 times.
65536 if (x == 0) {
935
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 508 times.
512 if (y == 0)
936 4 m = PRED_MODE_BLACK;
937 else
938 508 m = PRED_MODE_T;
939
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 64516 times.
65024 } else if (y == 0)
940 508 m = PRED_MODE_L;
941
942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65536 times.
65536 if (m > 13) {
943 av_log(s->avctx, AV_LOG_ERROR,
944 "invalid predictor mode: %d\n", m);
945 return AVERROR_INVALIDDATA;
946 }
947 65536 inverse_prediction(img->frame, m, x, y);
948 }
949 }
950 4 return 0;
951 }
952
953 196608 static av_always_inline uint8_t color_transform_delta(uint8_t color_pred,
954 uint8_t color)
955 {
956 196608 return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5;
957 }
958
959 4 static int apply_color_transform(WebPContext *s)
960 {
961 ImageContext *img, *cimg;
962 int x, y, cx, cy;
963 uint8_t *p, *cp;
964
965 4 img = &s->image[IMAGE_ROLE_ARGB];
966 4 cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM];
967
968
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
516 for (y = 0; y < img->frame->height; y++) {
969
2/2
✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
66048 for (x = 0; x < s->reduced_width; x++) {
970 65536 cx = x >> cimg->size_reduction;
971 65536 cy = y >> cimg->size_reduction;
972 65536 cp = GET_PIXEL(cimg->frame, cx, cy);
973 65536 p = GET_PIXEL(img->frame, x, y);
974
975 65536 p[1] += color_transform_delta(cp[3], p[2]);
976 65536 p[3] += color_transform_delta(cp[2], p[2]) +
977 65536 color_transform_delta(cp[1], p[1]);
978 }
979 }
980 4 return 0;
981 }
982
983 4 static int apply_subtract_green_transform(WebPContext *s)
984 {
985 int x, y;
986 4 ImageContext *img = &s->image[IMAGE_ROLE_ARGB];
987
988
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
516 for (y = 0; y < img->frame->height; y++) {
989
2/2
✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
66048 for (x = 0; x < s->reduced_width; x++) {
990 65536 uint8_t *p = GET_PIXEL(img->frame, x, y);
991 65536 p[1] += p[2];
992 65536 p[3] += p[2];
993 }
994 }
995 4 return 0;
996 }
997
998 6 static int apply_color_indexing_transform(WebPContext *s)
999 {
1000 ImageContext *img;
1001 ImageContext *pal;
1002 int i, x, y;
1003 uint8_t *p;
1004
1005 6 img = &s->image[IMAGE_ROLE_ARGB];
1006 6 pal = &s->image[IMAGE_ROLE_COLOR_INDEXING];
1007
1008
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (pal->size_reduction > 0) { // undo pixel packing
1009 GetBitContext gb_g;
1010 uint8_t *line;
1011 2 int pixel_bits = 8 >> pal->size_reduction;
1012
1013 2 line = av_malloc(img->frame->linesize[0] + AV_INPUT_BUFFER_PADDING_SIZE);
1014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!line)
1015 return AVERROR(ENOMEM);
1016
1017
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (y = 0; y < img->frame->height; y++) {
1018 16 p = GET_PIXEL(img->frame, 0, y);
1019 16 memcpy(line, p, img->frame->linesize[0]);
1020 16 init_get_bits(&gb_g, line, img->frame->linesize[0] * 8);
1021 16 skip_bits(&gb_g, 16);
1022 16 i = 0;
1023
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 16 times.
208 for (x = 0; x < img->frame->width; x++) {
1024 192 p = GET_PIXEL(img->frame, x, y);
1025 192 p[2] = get_bits(&gb_g, pixel_bits);
1026 192 i++;
1027
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 144 times.
192 if (i == 1 << pal->size_reduction) {
1028 48 skip_bits(&gb_g, 24);
1029 48 i = 0;
1030 }
1031 }
1032 }
1033 2 av_free(line);
1034 2 s->reduced_width = s->width; // we are back to full size
1035 }
1036
1037 // switch to local palette if it's worth initializing it
1038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (img->frame->height * img->frame->width > 300) {
1039 uint8_t palette[256 * 4];
1040 const int size = pal->frame->width * 4;
1041 av_assert0(size <= 1024U);
1042 memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size); // copy palette
1043 // set extra entries to transparent black
1044 memset(palette + size, 0, 256 * 4 - size);
1045 for (y = 0; y < img->frame->height; y++) {
1046 for (x = 0; x < img->frame->width; x++) {
1047 p = GET_PIXEL(img->frame, x, y);
1048 i = p[2];
1049 AV_COPY32(p, &palette[i * 4]);
1050 }
1051 }
1052 } else {
1053
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
54 for (y = 0; y < img->frame->height; y++) {
1054
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 48 times.
624 for (x = 0; x < img->frame->width; x++) {
1055 576 p = GET_PIXEL(img->frame, x, y);
1056 576 i = p[2];
1057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 576 times.
576 if (i >= pal->frame->width) {
1058 AV_WB32(p, 0x00000000);
1059 } else {
1060 576 const uint8_t *pi = GET_PIXEL(pal->frame, i, 0);
1061 576 AV_COPY32(p, pi);
1062 }
1063 }
1064 }
1065 }
1066
1067 6 return 0;
1068 }
1069
1070 14 static void update_canvas_size(AVCodecContext *avctx, int w, int h)
1071 {
1072 14 WebPContext *s = avctx->priv_data;
1073
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
14 if (s->width && s->width != w) {
1074 av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n",
1075 s->width, w);
1076 }
1077 14 s->width = w;
1078
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
14 if (s->height && s->height != h) {
1079 av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n",
1080 s->height, h);
1081 }
1082 14 s->height = h;
1083 14 }
1084
1085 10 static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p,
1086 int *got_frame, uint8_t *data_start,
1087 unsigned int data_size, int is_alpha_chunk)
1088 {
1089 10 WebPContext *s = avctx->priv_data;
1090 int w, h, ret, i, used;
1091
1092
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (!is_alpha_chunk) {
1093 8 s->lossless = 1;
1094 8 avctx->pix_fmt = AV_PIX_FMT_ARGB;
1095 }
1096
1097 10 ret = init_get_bits8(&s->gb, data_start, data_size);
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
1099 return ret;
1100
1101
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (!is_alpha_chunk) {
1102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (get_bits(&s->gb, 8) != 0x2F) {
1103 av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n");
1104 return AVERROR_INVALIDDATA;
1105 }
1106
1107 8 w = get_bits(&s->gb, 14) + 1;
1108 8 h = get_bits(&s->gb, 14) + 1;
1109
1110 8 update_canvas_size(avctx, w, h);
1111
1112 8 ret = ff_set_dimensions(avctx, s->width, s->height);
1113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
1114 return ret;
1115
1116 8 s->has_alpha = get_bits1(&s->gb);
1117
1118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (get_bits(&s->gb, 3) != 0x0) {
1119 av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n");
1120 return AVERROR_INVALIDDATA;
1121 }
1122 } else {
1123
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!s->width || !s->height)
1124 return AVERROR_BUG;
1125 2 w = s->width;
1126 2 h = s->height;
1127 }
1128
1129 /* parse transformations */
1130 10 s->nb_transforms = 0;
1131 10 s->reduced_width = s->width;
1132 10 used = 0;
1133
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 10 times.
28 while (get_bits1(&s->gb)) {
1134 18 enum TransformType transform = get_bits(&s->gb, 2);
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (used & (1 << transform)) {
1136 av_log(avctx, AV_LOG_ERROR, "Transform %d used more than once\n",
1137 transform);
1138 ret = AVERROR_INVALIDDATA;
1139 goto free_and_return;
1140 }
1141 18 used |= (1 << transform);
1142 18 s->transforms[s->nb_transforms++] = transform;
1143
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 4 times.
18 switch (transform) {
1144 4 case PREDICTOR_TRANSFORM:
1145 4 ret = parse_transform_predictor(s);
1146 4 break;
1147 4 case COLOR_TRANSFORM:
1148 4 ret = parse_transform_color(s);
1149 4 break;
1150 6 case COLOR_INDEXING_TRANSFORM:
1151 6 ret = parse_transform_color_indexing(s);
1152 6 break;
1153 }
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
1155 goto free_and_return;
1156 }
1157
1158 /* decode primary image */
1159 10 s->image[IMAGE_ROLE_ARGB].frame = p;
1160
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (is_alpha_chunk)
1161 2 s->image[IMAGE_ROLE_ARGB].is_alpha_primary = 1;
1162 10 ret = decode_entropy_coded_image(s, IMAGE_ROLE_ARGB, w, h);
1163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
1164 goto free_and_return;
1165
1166 /* apply transformations */
1167
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 for (i = s->nb_transforms - 1; i >= 0; i--) {
1168
4/5
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
18 switch (s->transforms[i]) {
1169 4 case PREDICTOR_TRANSFORM:
1170 4 ret = apply_predictor_transform(s);
1171 4 break;
1172 4 case COLOR_TRANSFORM:
1173 4 ret = apply_color_transform(s);
1174 4 break;
1175 4 case SUBTRACT_GREEN:
1176 4 ret = apply_subtract_green_transform(s);
1177 4 break;
1178 6 case COLOR_INDEXING_TRANSFORM:
1179 6 ret = apply_color_indexing_transform(s);
1180 6 break;
1181 }
1182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
1183 goto free_and_return;
1184 }
1185
1186 10 *got_frame = 1;
1187 10 p->pict_type = AV_PICTURE_TYPE_I;
1188 10 p->key_frame = 1;
1189 10 ret = data_size;
1190
1191 10 free_and_return:
1192
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 10 times.
60 for (i = 0; i < IMAGE_ROLE_NB; i++)
1193 50 image_ctx_free(&s->image[i]);
1194
1195 10 return ret;
1196 }
1197
1198 static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m)
1199 {
1200 int x, y, ls;
1201 uint8_t *dec;
1202
1203 ls = frame->linesize[3];
1204
1205 /* filter first row using horizontal filter */
1206 dec = frame->data[3] + 1;
1207 for (x = 1; x < frame->width; x++, dec++)
1208 *dec += *(dec - 1);
1209
1210 /* filter first column using vertical filter */
1211 dec = frame->data[3] + ls;
1212 for (y = 1; y < frame->height; y++, dec += ls)
1213 *dec += *(dec - ls);
1214
1215 /* filter the rest using the specified filter */
1216 switch (m) {
1217 case ALPHA_FILTER_HORIZONTAL:
1218 for (y = 1; y < frame->height; y++) {
1219 dec = frame->data[3] + y * ls + 1;
1220 for (x = 1; x < frame->width; x++, dec++)
1221 *dec += *(dec - 1);
1222 }
1223 break;
1224 case ALPHA_FILTER_VERTICAL:
1225 for (y = 1; y < frame->height; y++) {
1226 dec = frame->data[3] + y * ls + 1;
1227 for (x = 1; x < frame->width; x++, dec++)
1228 *dec += *(dec - ls);
1229 }
1230 break;
1231 case ALPHA_FILTER_GRADIENT:
1232 for (y = 1; y < frame->height; y++) {
1233 dec = frame->data[3] + y * ls + 1;
1234 for (x = 1; x < frame->width; x++, dec++)
1235 dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1236 }
1237 break;
1238 }
1239 }
1240
1241 2 static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p,
1242 uint8_t *data_start,
1243 unsigned int data_size)
1244 {
1245 2 WebPContext *s = avctx->priv_data;
1246 int x, y, ret;
1247
1248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->alpha_compression == ALPHA_COMPRESSION_NONE) {
1249 GetByteContext gb;
1250
1251 bytestream2_init(&gb, data_start, data_size);
1252 for (y = 0; y < s->height; y++)
1253 bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y,
1254 s->width);
1255
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) {
1256 uint8_t *ap, *pp;
1257 2 int alpha_got_frame = 0;
1258
1259 2 s->alpha_frame = av_frame_alloc();
1260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->alpha_frame)
1261 return AVERROR(ENOMEM);
1262
1263 2 ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame,
1264 data_start, data_size, 1);
1265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
1266 av_frame_free(&s->alpha_frame);
1267 return ret;
1268 }
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!alpha_got_frame) {
1270 av_frame_free(&s->alpha_frame);
1271 return AVERROR_INVALIDDATA;
1272 }
1273
1274 /* copy green component of alpha image to alpha plane of primary image */
1275
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (y = 0; y < s->height; y++) {
1276 16 ap = GET_PIXEL(s->alpha_frame, 0, y) + 2;
1277 16 pp = p->data[3] + p->linesize[3] * y;
1278
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 16 times.
208 for (x = 0; x < s->width; x++) {
1279 192 *pp = *ap;
1280 192 pp++;
1281 192 ap += 4;
1282 }
1283 }
1284 2 av_frame_free(&s->alpha_frame);
1285 }
1286
1287 /* apply alpha filtering */
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->alpha_filter)
1289 alpha_inverse_prediction(p, s->alpha_filter);
1290
1291 2 return 0;
1292 }
1293
1294 6 static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p,
1295 int *got_frame, uint8_t *data_start,
1296 unsigned int data_size)
1297 {
1298 6 WebPContext *s = avctx->priv_data;
1299 int ret;
1300
1301
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!s->initialized) {
1302 6 ff_vp8_decode_init(avctx);
1303 6 s->initialized = 1;
1304 6 s->v.actually_webp = 1;
1305 }
1306
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 avctx->pix_fmt = s->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
1307 6 s->lossless = 0;
1308
1309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (data_size > INT_MAX) {
1310 av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n");
1311 return AVERROR_PATCHWELCOME;
1312 }
1313
1314 6 av_packet_unref(s->pkt);
1315 6 s->pkt->data = data_start;
1316 6 s->pkt->size = data_size;
1317
1318 6 ret = ff_vp8_decode_frame(avctx, p, got_frame, s->pkt);
1319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1320 return ret;
1321
1322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!*got_frame)
1323 return AVERROR_INVALIDDATA;
1324
1325 6 update_canvas_size(avctx, avctx->width, avctx->height);
1326
1327
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (s->has_alpha) {
1328 2 ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data,
1329 2 s->alpha_data_size);
1330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1331 return ret;
1332 }
1333 6 return ret;
1334 }
1335
1336 14 static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1337 AVPacket *avpkt)
1338 {
1339 14 AVFrame * const p = data;
1340 14 WebPContext *s = avctx->priv_data;
1341 GetByteContext gb;
1342 int ret;
1343 uint32_t chunk_type, chunk_size;
1344 14 int vp8x_flags = 0;
1345
1346 14 s->avctx = avctx;
1347 14 s->width = 0;
1348 14 s->height = 0;
1349 14 *got_frame = 0;
1350 14 s->has_alpha = 0;
1351 14 s->has_exif = 0;
1352 14 s->has_iccp = 0;
1353 14 bytestream2_init(&gb, avpkt->data, avpkt->size);
1354
1355
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (bytestream2_get_bytes_left(&gb) < 12)
1356 return AVERROR_INVALIDDATA;
1357
1358
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
1359 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
1360 return AVERROR_INVALIDDATA;
1361 }
1362
1363 14 chunk_size = bytestream2_get_le32(&gb);
1364
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (bytestream2_get_bytes_left(&gb) < chunk_size)
1365 return AVERROR_INVALIDDATA;
1366
1367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) {
1368 av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n");
1369 return AVERROR_INVALIDDATA;
1370 }
1371
1372
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 14 times.
36 while (bytestream2_get_bytes_left(&gb) > 8) {
1373 22 char chunk_str[5] = { 0 };
1374
1375 22 chunk_type = bytestream2_get_le32(&gb);
1376 22 chunk_size = bytestream2_get_le32(&gb);
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (chunk_size == UINT32_MAX)
1378 return AVERROR_INVALIDDATA;
1379 22 chunk_size += chunk_size & 1;
1380
1381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if (bytestream2_get_bytes_left(&gb) < chunk_size) {
1382 /* we seem to be running out of data, but it could also be that the
1383 bitstream has trailing junk leading to bogus chunk_size. */
1384 break;
1385 }
1386
1387
5/8
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 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.
22 switch (chunk_type) {
1388 6 case MKTAG('V', 'P', '8', ' '):
1389
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!*got_frame) {
1390 6 ret = vp8_lossy_decode_frame(avctx, p, got_frame,
1391 6 avpkt->data + bytestream2_tell(&gb),
1392 chunk_size);
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1394 return ret;
1395 }
1396 6 bytestream2_skip(&gb, chunk_size);
1397 6 break;
1398 8 case MKTAG('V', 'P', '8', 'L'):
1399
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!*got_frame) {
1400 8 ret = vp8_lossless_decode_frame(avctx, p, got_frame,
1401 8 avpkt->data + bytestream2_tell(&gb),
1402 chunk_size, 0);
1403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
1404 return ret;
1405 8 avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
1406 }
1407 8 bytestream2_skip(&gb, chunk_size);
1408 8 break;
1409 4 case MKTAG('V', 'P', '8', 'X'):
1410
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) {
1411 av_log(avctx, AV_LOG_ERROR, "Canvas dimensions are already set\n");
1412 return AVERROR_INVALIDDATA;
1413 }
1414 4 vp8x_flags = bytestream2_get_byte(&gb);
1415 4 bytestream2_skip(&gb, 3);
1416 4 s->width = bytestream2_get_le24(&gb) + 1;
1417 4 s->height = bytestream2_get_le24(&gb) + 1;
1418 4 ret = av_image_check_size(s->width, s->height, 0, avctx);
1419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
1420 return ret;
1421 4 break;
1422 2 case MKTAG('A', 'L', 'P', 'H'): {
1423 int alpha_header, filter_m, compression;
1424
1425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(vp8x_flags & VP8X_FLAG_ALPHA)) {
1426 av_log(avctx, AV_LOG_WARNING,
1427 "ALPHA chunk present, but alpha bit not set in the "
1428 "VP8X header\n");
1429 }
1430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (chunk_size == 0) {
1431 av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n");
1432 return AVERROR_INVALIDDATA;
1433 }
1434 2 alpha_header = bytestream2_get_byte(&gb);
1435 2 s->alpha_data = avpkt->data + bytestream2_tell(&gb);
1436 2 s->alpha_data_size = chunk_size - 1;
1437 2 bytestream2_skip(&gb, s->alpha_data_size);
1438
1439 2 filter_m = (alpha_header >> 2) & 0x03;
1440 2 compression = alpha_header & 0x03;
1441
1442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (compression > ALPHA_COMPRESSION_VP8L) {
1443 av_log(avctx, AV_LOG_VERBOSE,
1444 "skipping unsupported ALPHA chunk\n");
1445 } else {
1446 2 s->has_alpha = 1;
1447 2 s->alpha_compression = compression;
1448 2 s->alpha_filter = filter_m;
1449 }
1450
1451 2 break;
1452 }
1453 2 case MKTAG('E', 'X', 'I', 'F'): {
1454 2 int le, ifd_offset, exif_offset = bytestream2_tell(&gb);
1455 2 AVDictionary *exif_metadata = NULL;
1456 GetByteContext exif_gb;
1457
1458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->has_exif) {
1459 av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n");
1460 goto exif_end;
1461 }
1462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA))
1463 av_log(avctx, AV_LOG_WARNING,
1464 "EXIF chunk present, but Exif bit not set in the "
1465 "VP8X header\n");
1466
1467 2 s->has_exif = 1;
1468 2 bytestream2_init(&exif_gb, avpkt->data + exif_offset,
1469 2 avpkt->size - exif_offset);
1470
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (ff_tdecode_header(&exif_gb, &le, &ifd_offset) < 0) {
1471 av_log(avctx, AV_LOG_ERROR, "invalid TIFF header "
1472 "in Exif data\n");
1473 goto exif_end;
1474 }
1475
1476 2 bytestream2_seek(&exif_gb, ifd_offset, SEEK_SET);
1477
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (ff_exif_decode_ifd(avctx, &exif_gb, le, 0, &exif_metadata) < 0) {
1478 av_log(avctx, AV_LOG_ERROR, "error decoding Exif data\n");
1479 goto exif_end;
1480 }
1481
1482 2 av_dict_copy(&((AVFrame *) data)->metadata, exif_metadata, 0);
1483
1484 2 exif_end:
1485 2 av_dict_free(&exif_metadata);
1486 2 bytestream2_skip(&gb, chunk_size);
1487 2 break;
1488 }
1489 case MKTAG('I', 'C', 'C', 'P'): {
1490 AVFrameSideData *sd;
1491
1492 if (s->has_iccp) {
1493 av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra ICCP chunk\n");
1494 bytestream2_skip(&gb, chunk_size);
1495 break;
1496 }
1497 if (!(vp8x_flags & VP8X_FLAG_ICC))
1498 av_log(avctx, AV_LOG_WARNING,
1499 "ICCP chunk present, but ICC Profile bit not set in the "
1500 "VP8X header\n");
1501
1502 s->has_iccp = 1;
1503 sd = av_frame_new_side_data(p, AV_FRAME_DATA_ICC_PROFILE, chunk_size);
1504 if (!sd)
1505 return AVERROR(ENOMEM);
1506
1507 bytestream2_get_buffer(&gb, sd->data, chunk_size);
1508 break;
1509 }
1510 case MKTAG('A', 'N', 'I', 'M'):
1511 case MKTAG('A', 'N', 'M', 'F'):
1512 case MKTAG('X', 'M', 'P', ' '):
1513 AV_WL32(chunk_str, chunk_type);
1514 av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n",
1515 chunk_str);
1516 bytestream2_skip(&gb, chunk_size);
1517 break;
1518 default:
1519 AV_WL32(chunk_str, chunk_type);
1520 av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n",
1521 chunk_str);
1522 bytestream2_skip(&gb, chunk_size);
1523 break;
1524 }
1525 }
1526
1527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!*got_frame) {
1528 av_log(avctx, AV_LOG_ERROR, "image data not found\n");
1529 return AVERROR_INVALIDDATA;
1530 }
1531
1532 14 return avpkt->size;
1533 }
1534
1535 14 static av_cold int webp_decode_init(AVCodecContext *avctx)
1536 {
1537 14 WebPContext *s = avctx->priv_data;
1538
1539 14 s->pkt = av_packet_alloc();
1540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!s->pkt)
1541 return AVERROR(ENOMEM);
1542
1543 14 return 0;
1544 }
1545
1546 14 static av_cold int webp_decode_close(AVCodecContext *avctx)
1547 {
1548 14 WebPContext *s = avctx->priv_data;
1549
1550 14 av_packet_free(&s->pkt);
1551
1552
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 if (s->initialized)
1553 6 return ff_vp8_decode_free(avctx);
1554
1555 8 return 0;
1556 }
1557
1558 const AVCodec ff_webp_decoder = {
1559 .name = "webp",
1560 .long_name = NULL_IF_CONFIG_SMALL("WebP image"),
1561 .type = AVMEDIA_TYPE_VIDEO,
1562 .id = AV_CODEC_ID_WEBP,
1563 .priv_data_size = sizeof(WebPContext),
1564 .init = webp_decode_init,
1565 .decode = webp_decode_frame,
1566 .close = webp_decode_close,
1567 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1568 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1569 };
1570