40 #define BITSTREAM_READER_LE
49 #define VP8X_FLAG_ANIMATION 0x02
50 #define VP8X_FLAG_XMP_METADATA 0x04
51 #define VP8X_FLAG_EXIF_METADATA 0x08
52 #define VP8X_FLAG_ALPHA 0x10
53 #define VP8X_FLAG_ICC 0x20
55 #define MAX_PALETTE_SIZE 256
56 #define MAX_CACHE_BITS 11
57 #define NUM_CODE_LENGTH_CODES 19
58 #define HUFFMAN_CODES_PER_META_CODE 5
59 #define NUM_LITERAL_CODES 256
60 #define NUM_LENGTH_CODES 24
61 #define NUM_DISTANCE_CODES 40
62 #define NUM_SHORT_DISTANCES 120
63 #define MAX_HUFFMAN_CODE_LENGTH 15
72 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
76 { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 },
77 { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 },
78 { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 },
79 { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 },
80 { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 },
81 { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 },
82 { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 },
83 { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 },
84 { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 },
85 { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 },
86 { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 },
87 { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 },
88 { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 },
89 { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 },
90 { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 }
205 #define GET_PIXEL(frame, x, y) \
206 ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
208 #define GET_PIXEL_COMP(frame, x, y, c) \
209 (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
225 memset(img, 0,
sizeof(*img));
245 code = table[
index][0];
256 code = table[
index][0];
280 int len = 0, sym, code = 0, ret;
281 int max_code_length = 0;
285 for (sym = 0; sym < alphabet_size; sym++) {
286 if (code_lengths[sym] > 0) {
300 for (sym = 0; sym < alphabet_size; sym++)
301 max_code_length =
FFMAX(max_code_length, code_lengths[sym]);
306 codes =
av_malloc(alphabet_size *
sizeof(*codes));
312 for (len = 1; len <= max_code_length; len++) {
313 for (sym = 0; sym < alphabet_size; sym++) {
314 if (code_lengths[sym] != len)
327 code_lengths,
sizeof(*code_lengths),
sizeof(*code_lengths),
328 codes,
sizeof(*codes),
sizeof(*codes), 0);
357 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
358 int *code_lengths =
NULL;
360 int i, symbol, max_symbol, prev_code_len, ret;
366 for (i = 0; i < num_codes; i++)
383 if (max_symbol > alphabet_size) {
385 max_symbol, alphabet_size);
390 max_symbol = alphabet_size;
395 while (symbol < alphabet_size) {
403 code_lengths[symbol++] = code_len;
405 prev_code_len = code_len;
407 int repeat = 0, length = 0;
414 length = prev_code_len;
427 if (symbol + repeat > alphabet_size) {
429 "invalid symbol %d + repeat %d > alphabet size %d\n",
430 symbol, repeat, alphabet_size);
435 code_lengths[symbol++] = length;
450 #define PARSE_BLOCK_SIZE(w, h) do { \
451 block_bits = get_bits(&s->gb, 3) + 2; \
452 blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
453 blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
459 int ret, block_bits,
width, blocks_w, blocks_h, x, y, max;
490 int block_bits, blocks_w, blocks_h, ret;
506 int block_bits, blocks_w, blocks_h, ret;
523 int width_bits, index_size, ret, x;
530 else if (index_size <= 4)
532 else if (index_size <= 16)
549 for (x = 4; x < img->
frame->
width * 4; x++, ct++)
581 int i, j, ret, x, y,
width;
583 img = &s->
image[role];
654 while (y < img->frame->height) {
675 int prefix_code, length,
distance, ref_x, ref_y;
679 if (prefix_code < 4) {
680 length = prefix_code + 1;
683 int offset = 2 + (prefix_code & 1) << extra_bits;
684 length = offset +
get_bits(&s->
gb, extra_bits) + 1;
687 if (prefix_code > 39) {
689 "distance prefix code too large: %d\n", prefix_code);
692 if (prefix_code < 4) {
693 distance = prefix_code + 1;
696 int offset = 2 + (prefix_code & 1) << extra_bits;
697 distance = offset +
get_bits(&s->
gb, extra_bits) + 1;
704 distance =
FFMAX(1, xi + yi * width);
717 while (distance >= width) {
725 ref_x =
FFMAX(0, ref_x);
726 ref_y =
FFMAX(0, ref_y);
731 for (i = 0; i < length; i++) {
744 if (ref_x == width) {
762 "color cache index out-of-bounds\n");
816 p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
817 p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
818 p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
819 p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
826 p[0] = p_l[0] + p_tl[0] >> 1;
827 p[1] = p_l[1] + p_tl[1] >> 1;
828 p[2] = p_l[2] + p_tl[2] >> 1;
829 p[3] = p_l[3] + p_tl[3] >> 1;
836 p[0] = p_l[0] + p_t[0] >> 1;
837 p[1] = p_l[1] + p_t[1] >> 1;
838 p[2] = p_l[2] + p_t[2] >> 1;
839 p[3] = p_l[3] + p_t[3] >> 1;
846 p[0] = p_tl[0] + p_t[0] >> 1;
847 p[1] = p_tl[1] + p_t[1] >> 1;
848 p[2] = p_tl[2] + p_t[2] >> 1;
849 p[3] = p_tl[3] + p_t[3] >> 1;
856 p[0] = p_t[0] + p_tr[0] >> 1;
857 p[1] = p_t[1] + p_tr[1] >> 1;
858 p[2] = p_t[2] + p_tr[2] >> 1;
859 p[3] = p_t[3] + p_tr[3] >> 1;
866 p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
867 p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
868 p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
869 p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
876 int diff = (
FFABS(p_l[0] - p_tl[0]) -
FFABS(p_t[0] - p_tl[0])) +
877 (
FFABS(p_l[1] - p_tl[1]) -
FFABS(p_t[1] - p_tl[1])) +
878 (
FFABS(p_l[2] - p_tl[2]) -
FFABS(p_t[2] - p_tl[2])) +
879 (
FFABS(p_l[3] - p_tl[3]) -
FFABS(p_t[3] - p_tl[3]));
890 p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
891 p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
892 p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
893 p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
899 return av_clip_uint8(d + (d - c) / 2);
925 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
932 if (x == frame->
width - 1)
937 inverse_predict[m](p, p_l, p_tl, p_t, p_tr);
967 "invalid predictor mode: %d\n", m);
1048 p[2] =
get_bits(&gb_g, pixel_bits);
1076 int *got_frame,
uint8_t *data_start,
1077 unsigned int data_size,
int is_alpha_chunk)
1082 if (!is_alpha_chunk) {
1091 if (!is_alpha_chunk) {
1133 switch (transform) {
1145 goto free_and_return;
1154 goto free_and_return;
1173 goto free_and_return;
1196 dec = frame->
data[3] + 1;
1197 for (x = 1; x < frame->
width; x++, dec++)
1201 dec = frame->
data[3] + ls;
1202 for (y = 1; y < frame->
height; y++, dec += ls)
1203 *dec += *(dec - ls);
1208 for (y = 1; y < frame->
height; y++) {
1209 dec = frame->
data[3] + y * ls + 1;
1210 for (x = 1; x < frame->
width; x++, dec++)
1215 for (y = 1; y < frame->
height; y++) {
1216 dec = frame->
data[3] + y * ls + 1;
1217 for (x = 1; x < frame->
width; x++, dec++)
1218 *dec += *(dec - ls);
1222 for (y = 1; y < frame->
height; y++) {
1223 dec = frame->
data[3] + y * ls + 1;
1224 for (x = 1; x < frame->
width; x++, dec++)
1225 dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1233 unsigned int data_size)
1242 for (y = 0; y < s->
height; y++)
1247 int alpha_got_frame = 0;
1254 data_start, data_size, 1);
1259 if (!alpha_got_frame) {
1265 for (y = 0; y < s->
height; y++) {
1268 for (x = 0; x < s->
width; x++) {
1285 int *got_frame,
uint8_t *data_start,
1286 unsigned int data_size)
1300 if (data_size > INT_MAX) {
1306 pkt.
data = data_start;
1307 pkt.
size = data_size;
1326 uint32_t chunk_type, chunk_size;
1339 if (bytestream2_get_le32(&gb) !=
MKTAG(
'R',
'I',
'F',
'F')) {
1344 chunk_size = bytestream2_get_le32(&gb);
1348 if (bytestream2_get_le32(&gb) !=
MKTAG(
'W',
'E',
'B',
'P')) {
1354 char chunk_str[5] = { 0 };
1356 chunk_type = bytestream2_get_le32(&gb);
1357 chunk_size = bytestream2_get_le32(&gb);
1358 if (chunk_size == UINT32_MAX)
1360 chunk_size += chunk_size & 1;
1365 switch (chunk_type) {
1366 case MKTAG(
'V',
'P',
'8',
' '):
1376 case MKTAG(
'V',
'P',
'8',
'L'):
1386 case MKTAG(
'V',
'P',
'8',
'X'):
1387 vp8x_flags = bytestream2_get_byte(&gb);
1389 s->
width = bytestream2_get_le24(&gb) + 1;
1390 s->
height = bytestream2_get_le24(&gb) + 1;
1395 case MKTAG(
'A',
'L',
'P',
'H'): {
1396 int alpha_header, filter_m, compression;
1400 "ALPHA chunk present, but alpha bit not set in the "
1403 if (chunk_size == 0) {
1407 alpha_header = bytestream2_get_byte(&gb);
1412 filter_m = (alpha_header >> 2) & 0x03;
1413 compression = alpha_header & 0x03;
1417 "skipping unsupported ALPHA chunk\n");
1426 case MKTAG(
'I',
'C',
'C',
'P'):
1427 case MKTAG(
'A',
'N',
'I',
'M'):
1428 case MKTAG(
'A',
'N',
'M',
'F'):
1429 case MKTAG(
'E',
'X',
'I',
'F'):
1430 case MKTAG(
'X',
'M',
'P',
' '):
1431 AV_WL32(chunk_str, chunk_type);
1437 AV_WL32(chunk_str, chunk_type);