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 #define BITSTREAM_READER_LE
52
53 #define VP8X_FLAG_ANIMATION 0x02
54 #define VP8X_FLAG_XMP_METADATA 0x04
55 #define VP8X_FLAG_EXIF_METADATA 0x08
56 #define VP8X_FLAG_ALPHA 0x10
57 #define VP8X_FLAG_ICC 0x20
58
59 #define MAX_PALETTE_SIZE 256
60 #define MAX_CACHE_BITS 11
61 #define NUM_CODE_LENGTH_CODES 19
62 #define HUFFMAN_CODES_PER_META_CODE 5
63 #define NUM_LITERAL_CODES 256
64 #define NUM_LENGTH_CODES 24
65 #define NUM_DISTANCE_CODES 40
66 #define NUM_SHORT_DISTANCES 120
67 #define MAX_HUFFMAN_CODE_LENGTH 15
68
73 };
74
76 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
77 };
78
80 { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
81 { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
82 { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
83 { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
84 { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
85 { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
86 { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
87 { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
88 { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
89 { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
90 { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
91 { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
92 { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
93 { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
94 { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
95 };
96
100 };
101
107 };
108
114 };
115
131 };
132
139 };
140
141 /* The structure of WebP lossless is an optional series of transformation data,
142 * followed by the primary image. The primary image also optionally contains
143 * an entropy group mapping if there are multiple entropy groups. There is a
144 * basic image type called an "entropy coded image" that is used for all of
145 * these. The type of each entropy coded image is referred to by the
146 * specification as its role. */
148 /* Primary Image: Stores the actual pixels of the image. */
150
151 /* Entropy Image: Defines which Huffman group to use for different areas of
152 * the primary image. */
154
155 /* Predictors: Defines which predictor type to use for different areas of
156 * the primary image. */
158
159 /* Color Transform Data: Defines the color transformation for different
160 * areas of the primary image. */
162
163 /* Color Index: Stored as an image of height == 1. */
165
167 };
168
171 int simple;
/* whether to use simple mode */
175
186
198 int has_exif;
/* set after an EXIF chunk has been processed */
203
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
218 {
219 int i, j;
220
228 }
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 */
241 {
244 int code;
245
248
253
254 if (n < 0) {
257
259
264 }
266
268
269 return code;
270 }
271
273 {
277 else
279 } else
281 }
282
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) {
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
311
313 if (!codes)
315
316 code = 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++;
324 }
325 code <<= 1;
326 }
330 }
331
333 code_lengths, sizeof(*code_lengths), sizeof(*code_lengths),
334 codes, sizeof(*codes), sizeof(*codes), 0);
335 if (ret < 0) {
337 return ret;
338 }
340
342 return 0;
343 }
344
346 {
348
351 else
353
356
358 }
359
361 int alphabet_size)
362 {
363 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
364 int *code_lengths =
NULL;
366 int i, symbol, max_symbol, prev_code_len, ret;
368
371
372 for (i = 0; i < num_codes; i++)
374
377 if (ret < 0)
379
381 if (!code_lengths) {
384 }
385
389 if (max_symbol > alphabet_size) {
391 max_symbol, alphabet_size);
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;
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. */
421 break;
422 case 17:
423 /* Code 17 emits a streak of zeros [3..10], i.e.,
424 * 3 + ReadBits(3) times. */
426 break;
427 case 18:
428 /* Code 18 emits a streak of zeros of length [11..138], i.e.,
429 * 11 + ReadBits(7) times. */
431 break;
432 }
433 if (symbol + repeat > alphabet_size) {
435 "invalid symbol %d + repeat %d > alphabet size %d\n",
436 symbol, repeat, alphabet_size);
439 }
440 while (repeat-- > 0)
441 code_lengths[symbol++] =
length;
442 }
443 }
444
446
450 return ret;
451 }
452
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
463 {
465 int ret, block_bits,
width, blocks_w, blocks_h, x, y, max;
466
470
472
474 if (ret < 0)
475 return ret;
476
479
480 /* the number of huffman groups is determined by the maximum group number
481 * coded in the entropy image */
482 max = 0;
487 int p = p0 << 8 | p1;
489 }
490 }
492
493 return 0;
494 }
495
497 {
498 int block_bits, blocks_w, blocks_h, ret;
499
501
503 blocks_h);
504 if (ret < 0)
505 return ret;
506
508
509 return 0;
510 }
511
513 {
514 int block_bits, blocks_w, blocks_h, ret;
515
517
519 blocks_h);
520 if (ret < 0)
521 return ret;
522
524
525 return 0;
526 }
527
529 {
531 int width_bits, index_size, ret, x;
533
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
546 index_size, 1);
547 if (ret < 0)
548 return ret;
549
552 if (width_bits > 0)
554
555 /* color index values are delta-coded */
557 for (x = 4; x < img->
frame->
width * 4; x++, ct++)
558 ct[0] += ct[-4];
559
560 return 0;
561 }
562
564 int x, int y)
565 {
567 int group = 0;
568
574 group = g0 << 8 | g1;
575 }
576
578 }
579
581 {
584 }
585
588 {
591 int i, j, ret, x, y,
width;
592
593 img = &s->
image[role];
595
600 }
601
605
609 } else
611 if (ret < 0)
612 return ret;
613
620 }
625 } else {
627 }
628
632 if (ret < 0)
633 return ret;
635 }
641
648
651 } else {
653 if (ret < 0)
654 return ret;
655 }
656 }
657 }
658
662
663 x = 0; y = 0;
665 int v;
666
670 /* literal pixel values */
672 p[2] = v;
678 x++;
679 if (x == width) {
680 x = 0;
681 y++;
682 }
684 /* LZ77 backwards mapping */
686
687 /* parse length and distance */
689 if (prefix_code < 4) {
690 length = prefix_code + 1;
691 } else {
693 int offset = 2 + (prefix_code & 1) << extra_bits;
694 length = offset +
get_bits(&s->
gb, extra_bits) + 1;
695 }
697 if (prefix_code > 39) {
699 "distance prefix code too large: %d\n", prefix_code);
701 }
702 if (prefix_code < 4) {
703 distance = prefix_code + 1;
704 } else {
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 */
714 distance =
FFMAX(1, xi + yi * width);
715 } else {
717 }
718 ref_x = x;
719 ref_y = y;
720 if (distance <= x) {
722 distance = 0;
723 } else {
724 ref_x = 0;
725 distance -= x;
726 }
727 while (distance >= width) {
728 ref_y--;
730 }
731 if (distance > 0) {
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++) {
744
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 }
759 break;
760 }
761 } else {
762 /* read from color cache */
765
769 }
772 "color cache index out-of-bounds\n");
774 }
776 x++;
777 if (x == width) {
778 x = 0;
779 y++;
780 }
781 }
782 }
783
784 return 0;
785 }
786
787 /* PRED_MODE_BLACK */
790 {
792 }
793
794 /* PRED_MODE_L */
797 {
799 }
800
801 /* PRED_MODE_T */
804 {
806 }
807
808 /* PRED_MODE_TR */
811 {
813 }
814
815 /* PRED_MODE_TL */
818 {
820 }
821
822 /* PRED_MODE_AVG_T_AVG_L_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 */
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 */
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 */
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 */
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 */
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 */
885 {
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)
892 else
894 }
895
896 /* PRED_MODE_ADD_SUBTRACT_FULL */
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
907 {
908 int d = a + b >> 1;
909 return av_clip_uint8(d + (d - c) / 2);
910 }
911
912 /* PRED_MODE_ADD_SUBTRACT_HALF */
915 {
920 }
921
925
931 };
932
934 {
935 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
937
942 if (x == frame->
width - 1)
944 else
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
956 {
959 int x, y;
960
966
967 if (x == 0) {
968 if (y == 0)
970 else
972 } else if (y == 0)
974
975 if (m > 13) {
977 "invalid predictor mode: %d\n", m);
979 }
981 }
982 }
983 return 0;
984 }
985
988 {
990 }
991
993 {
995 int x, y, cx, cy;
997
1000
1007
1011 }
1012 }
1013 return 0;
1014 }
1015
1017 {
1018 int x, y;
1020
1024 p[1] += p[2];
1025 p[3] += p[2];
1026 }
1027 }
1028 return 0;
1029 }
1030
1032 {
1035 int i, x, y;
1037
1040
1045
1047 if (!line)
1049
1055 i = 0;
1058 p[2] =
get_bits(&gb_g, pixel_bits);
1059 i++;
1062 i = 0;
1063 }
1064 }
1065 }
1067 }
1068
1069 // switch to local palette if it's worth initializing it
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);
1080 i = p[2];
1082 }
1083 }
1084 } else {
1088 i = p[2];
1091 } else {
1094 }
1095 }
1096 }
1097 }
1098
1099 return 0;
1100 }
1101
1103 int *got_frame,
uint8_t *data_start,
1104 unsigned int data_size, int is_alpha_chunk)
1105 {
1107 int w,
h, ret, i, used;
1108
1109 if (!is_alpha_chunk) {
1112 }
1113
1115 if (ret < 0)
1116 return ret;
1117
1118 if (!is_alpha_chunk) {
1122 }
1123
1129 }
1134 }
1136
1138 if (ret < 0)
1139 return ret;
1140
1142
1146 }
1147 } else {
1152 }
1153
1154 /* parse transformations */
1157 used = 0;
1160 if (used & (1 << transform)) {
1162 transform);
1164 goto free_and_return;
1165 }
1168 switch (transform) {
1171 break;
1174 break;
1177 break;
1178 }
1179 if (ret < 0)
1180 goto free_and_return;
1181 }
1182
1183 /* decode primary image */
1185 if (is_alpha_chunk)
1188 if (ret < 0)
1189 goto free_and_return;
1190
1191 /* apply transformations */
1196 break;
1199 break;
1202 break;
1205 break;
1206 }
1207 if (ret < 0)
1208 goto free_and_return;
1209 }
1210
1211 *got_frame = 1;
1214 ret = data_size;
1215
1216 free_and_return:
1219
1220 return ret;
1221 }
1222
1224 {
1225 int x, y, ls;
1227
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) {
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;
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;
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
1268 unsigned int data_size)
1269 {
1271 int x, y, ret;
1272
1275
1277 for (y = 0; y < s->
height; y++)
1282 int alpha_got_frame = 0;
1283
1287
1289 data_start, data_size, 1);
1290 if (ret < 0) {
1292 return ret;
1293 }
1294 if (!alpha_got_frame) {
1297 }
1298
1299 /* copy green component of alpha image to alpha plane of primary image */
1300 for (y = 0; y < s->
height; y++) {
1303 for (x = 0; x < s->
width; x++) {
1304 *pp = *ap;
1305 pp++;
1306 ap += 4;
1307 }
1308 }
1310 }
1311
1312 /* apply alpha filtering */
1315
1316 return 0;
1317 }
1318
1320 int *got_frame,
uint8_t *data_start,
1321 unsigned int data_size)
1322 {
1325 int ret;
1326
1332 }
1334
1335 if (data_size > INT_MAX) {
1338 }
1339
1341 pkt.
data = data_start;
1342 pkt.
size = data_size;
1343
1348 if (ret < 0)
1349 return ret;
1350 }
1351 return ret;
1352 }
1353
1356 {
1360 int ret;
1361 uint32_t chunk_type, chunk_size;
1362 int vp8x_flags = 0;
1363
1367 *got_frame = 0;
1371
1374
1375 if (bytestream2_get_le32(&gb) !=
MKTAG(
'R',
'I',
'F',
'F')) {
1378 }
1379
1380 chunk_size = bytestream2_get_le32(&gb);
1383
1384 if (bytestream2_get_le32(&gb) !=
MKTAG(
'W',
'E',
'B',
'P')) {
1387 }
1388
1391 char chunk_str[5] = { 0 };
1392
1393 chunk_type = bytestream2_get_le32(&gb);
1394 chunk_size = bytestream2_get_le32(&gb);
1395 if (chunk_size == UINT32_MAX)
1397 chunk_size += chunk_size & 1;
1398
1401
1402 switch (chunk_type) {
1403 case MKTAG(
'V',
'P',
'8',
' '):
1404 if (!*got_frame) {
1407 chunk_size);
1408 if (ret < 0)
1409 return ret;
1410 }
1412 break;
1413 case MKTAG(
'V',
'P',
'8',
'L'):
1414 if (!*got_frame) {
1417 chunk_size, 0);
1418 if (ret < 0)
1419 return ret;
1421 }
1423 break;
1424 case MKTAG(
'V',
'P',
'8',
'X'):
1425 vp8x_flags = bytestream2_get_byte(&gb);
1427 s->
width = bytestream2_get_le24(&gb) + 1;
1428 s->
height = bytestream2_get_le24(&gb) + 1;
1430 if (ret < 0)
1431 return ret;
1432 break;
1433 case MKTAG(
'A',
'L',
'P',
'H'): {
1434 int alpha_header, filter_m, compression;
1435
1438 "ALPHA chunk present, but alpha bit not set in the "
1439 "VP8X header\n");
1440 }
1441 if (chunk_size == 0) {
1444 }
1445 alpha_header = bytestream2_get_byte(&gb);
1449
1450 filter_m = (alpha_header >> 2) & 0x03;
1451 compression = alpha_header & 0x03;
1452
1455 "skipping unsupported ALPHA chunk\n");
1456 } else {
1460 }
1461
1462 break;
1463 }
1464 case MKTAG(
'E',
'X',
'I',
'F'): {
1467
1470 goto exif_end;
1471 }
1474 "EXIF chunk present, but Exif bit not set in the "
1475 "VP8X header\n");
1476
1479 avpkt->
size - exif_offset);
1482 "in Exif data\n");
1483 goto exif_end;
1484 }
1485
1489 goto exif_end;
1490 }
1491
1493
1494 exif_end:
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);
1505 chunk_str);
1507 break;
1508 default:
1509 AV_WL32(chunk_str, chunk_type);
1511 chunk_str);
1513 break;
1514 }
1515 }
1516
1517 if (!*got_frame) {
1520 }
1521
1523 }
1524
1526 {
1528
1531
1532 return 0;
1533 }
1534
1544 };
static int read_huffman_code_normal(WebPContext *s, HuffReader *hc, int alphabet_size)
HuffReader * huffman_groups
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
static int parse_transform_color_indexing(WebPContext *s)
static HuffReader * get_huffman_group(WebPContext *s, ImageContext *img, int x, int y)
static const uint8_t code_length_code_order[NUM_CODE_LENGTH_CODES]
#define VP8X_FLAG_EXIF_METADATA
static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
static int8_t ff_u8_to_s8(uint8_t a)
static const inv_predict_func inverse_predict[14]
static int apply_color_indexing_transform(WebPContext *s)
static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c)
enum TransformType transforms[4]
uint16_t simple_symbols[2]
static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p, uint8_t *data_start, unsigned int data_size)
#define NUM_LITERAL_CODES
static av_always_inline void color_cache_put(ImageContext *img, uint32_t c)
enum AlphaFilter alpha_filter
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define NUM_CODE_LENGTH_CODES
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Multithreading support functions.
AVDictionary * exif_metadata
int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define FF_CODEC_PROPERTY_LOSSLESS
static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
#define NUM_SHORT_DISTANCES
bitstream reader API header.
#define AV_LOG_VERBOSE
Detailed information.
static int decode_entropy_image(WebPContext *s)
static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static int apply_color_transform(WebPContext *s)
#define UPDATE_CACHE(name, gb)
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int webp_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const uint8_t ff_reverse[256]
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
const char * name
Name of the codec implementation.
static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, uint8_t *data_start, unsigned int data_size)
static const uint8_t offset[127][2]
#define CLOSE_READER(name, gb)
static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, uint8_t *data_start, unsigned int data_size, int is_alpha_chunk)
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
enum AlphaCompression alpha_compression
static void image_ctx_free(ImageContext *img)
#define SKIP_BITS(name, gb, num)
static float distance(float x, float y, int band)
static av_always_inline int webp_get_vlc(GetBitContext *gb, VLC_TYPE(*table)[2])
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
#define PARSE_BLOCK_SIZE(w, h)
enum AVPictureType pict_type
Picture type of the frame.
static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
#define LAST_SKIP_BITS(name, gb, num)
#define SHOW_UBITS(name, gb, num)
void(* inv_predict_func)(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
static av_always_inline int bytestream2_tell(GetByteContext *g)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
static const int8_t transform[32][32]
Libavcodec external API header.
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static int huff_reader_build_canonical(HuffReader *r, int *code_lengths, int alphabet_size)
main external API structure.
#define OPEN_READER(name, gb)
int avpriv_exif_decode_ifd(AVCodecContext *avctx, GetByteContext *gbytes, int le, int depth, AVDictionary **metadata)
Recursively decodes all IFD's and adds included TAGS into the metadata dictionary.
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
static av_always_inline uint8_t color_transform_delta(uint8_t color_pred, uint8_t color)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define MAX_HUFFMAN_CODE_LENGTH
static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE]
#define HUFFMAN_CODES_PER_META_CODE
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
static void read_huffman_code_simple(WebPContext *s, HuffReader *hc)
av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
static int parse_transform_predictor(WebPContext *s)
#define GET_PIXEL(frame, x, y)
static av_cold int webp_decode_close(AVCodecContext *avctx)
common internal api header.
static int apply_subtract_green_transform(WebPContext *s)
#define GET_PIXEL_COMP(frame, x, y, c)
static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2]
ImageContext image[IMAGE_ROLE_NB]
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int apply_predictor_transform(WebPContext *s)
VLC_TYPE(* table)[2]
code, bits
static int parse_transform_color(WebPContext *s)
int key_frame
1 -> keyframe, 0-> not
static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, int w, int h)
static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
static void * av_mallocz_array(size_t nmemb, size_t size)
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
Decodes a TIFF header from the input bytestream and sets the endianness in *le and the offset to the ...
static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m)
#define av_malloc_array(a, b)
static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y)
#define MKTAG(a, b, c, d)
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, const uint8_t *p_t, const uint8_t *p_tr)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define NUM_DISTANCE_CODES