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