43 #define BITSTREAM_READER_LE
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
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
76 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
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 }
211 #define GET_PIXEL(frame, x, y) \
212 ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x))
214 #define GET_PIXEL_COMP(frame, x, y, c) \
215 (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c))
231 memset(img, 0,
sizeof(*img));
286 int len = 0, sym, code = 0,
ret;
287 int max_code_length = 0;
291 for (sym = 0; sym < alphabet_size; sym++) {
292 if (code_lengths[sym] > 0) {
306 for (sym = 0; sym < alphabet_size; sym++)
307 max_code_length =
FFMAX(max_code_length, code_lengths[sym]);
318 for (len = 1; len <= max_code_length; len++) {
319 for (sym = 0; sym < alphabet_size; sym++) {
320 if (code_lengths[sym] != len)
333 code_lengths,
sizeof(*code_lengths),
sizeof(*code_lengths),
334 codes,
sizeof(*codes),
sizeof(*codes), 0);
363 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
364 int *code_lengths = NULL;
366 int i, symbol, max_symbol, prev_code_len,
ret;
372 for (i = 0; i < num_codes; i++)
389 if (max_symbol > alphabet_size) {
391 max_symbol, alphabet_size);
396 max_symbol = alphabet_size;
401 while (symbol < alphabet_size) {
409 code_lengths[symbol++] = code_len;
411 prev_code_len = code_len;
413 int repeat = 0,
length = 0;
433 if (symbol + repeat > alphabet_size) {
435 "invalid symbol %d + repeat %d > alphabet size %d\n",
436 symbol, repeat, alphabet_size);
441 code_lengths[symbol++] =
length;
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; \
465 int ret, block_bits,
width, blocks_w, blocks_h, x,
y, max;
487 int p = p0 << 8 | p1;
498 int block_bits, blocks_w, blocks_h,
ret;
514 int block_bits, blocks_w, blocks_h,
ret;
531 int width_bits, index_size,
ret, x;
538 else if (index_size <= 4)
540 else if (index_size <= 16)
557 for (x = 4; x < img->
frame->
width * 4; x++, ct++)
574 group = g0 << 8 | g1;
593 img = &s->
image[role];
689 if (prefix_code < 4) {
690 length = prefix_code + 1;
693 int offset = 2 + (prefix_code & 1) << extra_bits;
694 length = offset +
get_bits(&s->
gb, extra_bits) + 1;
697 if (prefix_code < 4) {
698 distance = prefix_code + 1;
701 int offset = 2 + (prefix_code & 1) << extra_bits;
702 distance = offset +
get_bits(&s->
gb, extra_bits) + 1;
709 distance =
FFMAX(1, xi + yi * width);
722 while (distance >= width) {
730 ref_x =
FFMAX(0, ref_x);
731 ref_y =
FFMAX(0, ref_y);
736 for (i = 0; i <
length; i++) {
749 if (ref_x == width) {
767 "color cache index out-of-bounds\n");
821 p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
822 p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
823 p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
824 p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
831 p[0] = p_l[0] + p_tl[0] >> 1;
832 p[1] = p_l[1] + p_tl[1] >> 1;
833 p[2] = p_l[2] + p_tl[2] >> 1;
834 p[3] = p_l[3] + p_tl[3] >> 1;
841 p[0] = p_l[0] + p_t[0] >> 1;
842 p[1] = p_l[1] + p_t[1] >> 1;
843 p[2] = p_l[2] + p_t[2] >> 1;
844 p[3] = p_l[3] + p_t[3] >> 1;
851 p[0] = p_tl[0] + p_t[0] >> 1;
852 p[1] = p_tl[1] + p_t[1] >> 1;
853 p[2] = p_tl[2] + p_t[2] >> 1;
854 p[3] = p_tl[3] + p_t[3] >> 1;
861 p[0] = p_t[0] + p_tr[0] >> 1;
862 p[1] = p_t[1] + p_tr[1] >> 1;
863 p[2] = p_t[2] + p_tr[2] >> 1;
864 p[3] = p_t[3] + p_tr[3] >> 1;
871 p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
872 p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
873 p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
874 p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
881 int diff = (
FFABS(p_l[0] - p_tl[0]) -
FFABS(p_t[0] - p_tl[0])) +
882 (
FFABS(p_l[1] - p_tl[1]) -
FFABS(p_t[1] - p_tl[1])) +
883 (
FFABS(p_l[2] - p_tl[2]) -
FFABS(p_t[2] - p_tl[2])) +
884 (
FFABS(p_l[3] - p_tl[3]) -
FFABS(p_t[3] - p_tl[3]));
895 p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
896 p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
897 p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
898 p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
904 return av_clip_uint8(d + (d - c) / 2);
930 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
937 if (x == frame->
width - 1)
942 inverse_predict[
m](p, p_l, p_tl, p_t, p_tr);
972 "invalid predictor mode: %d\n", m);
1053 p[2] =
get_bits(&gb_g, pixel_bits);
1071 memset(palette + size, 0, 256 * 4 - size);
1098 int *got_frame,
uint8_t *data_start,
1099 unsigned int data_size,
int is_alpha_chunk)
1104 if (!is_alpha_chunk) {
1113 if (!is_alpha_chunk) {
1155 switch (transform) {
1167 goto free_and_return;
1176 goto free_and_return;
1195 goto free_and_return;
1218 dec = frame->
data[3] + 1;
1219 for (x = 1; x < frame->
width; x++, dec++)
1223 dec = frame->
data[3] + ls;
1224 for (y = 1; y < frame->
height; y++, dec += ls)
1225 *dec += *(dec - ls);
1230 for (y = 1; y < frame->
height; y++) {
1231 dec = frame->
data[3] + y * ls + 1;
1232 for (x = 1; x < frame->
width; x++, dec++)
1237 for (y = 1; y < frame->
height; y++) {
1238 dec = frame->
data[3] + y * ls + 1;
1239 for (x = 1; x < frame->
width; x++, dec++)
1240 *dec += *(dec - ls);
1244 for (y = 1; y < frame->
height; y++) {
1245 dec = frame->
data[3] + y * ls + 1;
1246 for (x = 1; x < frame->
width; x++, dec++)
1247 dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1255 unsigned int data_size)
1264 for (y = 0; y < s->
height; y++)
1269 int alpha_got_frame = 0;
1276 data_start, data_size, 1);
1281 if (!alpha_got_frame) {
1287 for (y = 0; y < s->
height; y++) {
1290 for (x = 0; x < s->
width; x++) {
1307 int *got_frame,
uint8_t *data_start,
1308 unsigned int data_size)
1322 if (data_size > INT_MAX) {
1328 pkt.
data = data_start;
1329 pkt.
size = data_size;
1348 uint32_t chunk_type, chunk_size;
1362 if (bytestream2_get_le32(&gb) !=
MKTAG(
'R',
'I',
'F',
'F')) {
1367 chunk_size = bytestream2_get_le32(&gb);
1371 if (bytestream2_get_le32(&gb) !=
MKTAG(
'W',
'E',
'B',
'P')) {
1378 char chunk_str[5] = { 0 };
1380 chunk_type = bytestream2_get_le32(&gb);
1381 chunk_size = bytestream2_get_le32(&gb);
1382 if (chunk_size == UINT32_MAX)
1384 chunk_size += chunk_size & 1;
1389 switch (chunk_type) {
1390 case MKTAG(
'V',
'P',
'8',
' '):
1400 case MKTAG(
'V',
'P',
'8',
'L'):
1410 case MKTAG(
'V',
'P',
'8',
'X'):
1411 vp8x_flags = bytestream2_get_byte(&gb);
1413 s->
width = bytestream2_get_le24(&gb) + 1;
1414 s->
height = bytestream2_get_le24(&gb) + 1;
1419 case MKTAG(
'A',
'L',
'P',
'H'): {
1420 int alpha_header, filter_m, compression;
1424 "ALPHA chunk present, but alpha bit not set in the "
1427 if (chunk_size == 0) {
1431 alpha_header = bytestream2_get_byte(&gb);
1436 filter_m = (alpha_header >> 2) & 0x03;
1437 compression = alpha_header & 0x03;
1441 "skipping unsupported ALPHA chunk\n");
1450 case MKTAG(
'E',
'X',
'I',
'F'): {
1460 "EXIF chunk present, but Exif bit not set in the "
1465 avpkt->
size - exif_offset);
1485 case MKTAG(
'I',
'C',
'C',
'P'):
1486 case MKTAG(
'A',
'N',
'I',
'M'):
1487 case MKTAG(
'A',
'N',
'M',
'F'):
1488 case MKTAG(
'X',
'M',
'P',
' '):
1489 AV_WL32(chunk_str, chunk_type);
1495 AV_WL32(chunk_str, chunk_type);