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));
281 int max_code_length = 0;
284 for (sym = 0; sym < alphabet_size; sym++)
285 max_code_length =
FFMAX(max_code_length, code_lengths[sym]);
290 codes =
av_malloc(alphabet_size *
sizeof(*codes));
296 for (len = 1; len <= max_code_length; len++) {
297 for (sym = 0; sym < alphabet_size; sym++) {
298 if (code_lengths[sym] != len)
311 code_lengths,
sizeof(*code_lengths),
sizeof(*code_lengths),
312 codes,
sizeof(*codes),
sizeof(*codes), 0);
341 HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } };
342 int *code_lengths = NULL;
344 int i, symbol, max_symbol, prev_code_len,
ret;
350 for (i = 0; i < num_codes; i++)
358 code_lengths = av_mallocz_array(alphabet_size,
sizeof(*code_lengths));
367 if (max_symbol > alphabet_size) {
369 max_symbol, alphabet_size);
374 max_symbol = alphabet_size;
379 while (symbol < alphabet_size) {
387 code_lengths[symbol++] = code_len;
389 prev_code_len = code_len;
391 int repeat = 0,
length = 0;
411 if (symbol + repeat > alphabet_size) {
413 "invalid symbol %d + repeat %d > alphabet size %d\n",
414 symbol, repeat, alphabet_size);
419 code_lengths[symbol++] =
length;
434 #define PARSE_BLOCK_SIZE(w, h) do { \
435 block_bits = get_bits(&s->gb, 3) + 2; \
436 blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \
437 blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \
443 int ret, block_bits,
width, blocks_w, blocks_h, x,
y, max;
474 int block_bits, blocks_w, blocks_h,
ret;
490 int block_bits, blocks_w, blocks_h,
ret;
507 int width_bits, index_size,
ret, x;
514 else if (index_size <= 4)
516 else if (index_size <= 16)
533 for (x = 4; x < img->
frame->
width * 4; x++, ct++)
567 img = &s->
image[role];
663 if (prefix_code < 4) {
664 length = prefix_code + 1;
667 int offset = 2 + (prefix_code & 1) << extra_bits;
668 length = offset +
get_bits(&s->
gb, extra_bits) + 1;
671 if (prefix_code < 4) {
672 distance = prefix_code + 1;
675 int offset = 2 + (prefix_code & 1) << extra_bits;
676 distance = offset +
get_bits(&s->
gb, extra_bits) + 1;
683 distance =
FFMAX(1, xi + yi * width);
696 while (distance >= width) {
704 ref_x =
FFMAX(0, ref_x);
705 ref_y =
FFMAX(0, ref_y);
710 for (i = 0; i <
length; i++) {
723 if (ref_x == width) {
741 "color cache index out-of-bounds\n");
795 p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1;
796 p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1;
797 p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1;
798 p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1;
805 p[0] = p_l[0] + p_tl[0] >> 1;
806 p[1] = p_l[1] + p_tl[1] >> 1;
807 p[2] = p_l[2] + p_tl[2] >> 1;
808 p[3] = p_l[3] + p_tl[3] >> 1;
815 p[0] = p_l[0] + p_t[0] >> 1;
816 p[1] = p_l[1] + p_t[1] >> 1;
817 p[2] = p_l[2] + p_t[2] >> 1;
818 p[3] = p_l[3] + p_t[3] >> 1;
825 p[0] = p_tl[0] + p_t[0] >> 1;
826 p[1] = p_tl[1] + p_t[1] >> 1;
827 p[2] = p_tl[2] + p_t[2] >> 1;
828 p[3] = p_tl[3] + p_t[3] >> 1;
835 p[0] = p_t[0] + p_tr[0] >> 1;
836 p[1] = p_t[1] + p_tr[1] >> 1;
837 p[2] = p_t[2] + p_tr[2] >> 1;
838 p[3] = p_t[3] + p_tr[3] >> 1;
845 p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1;
846 p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1;
847 p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1;
848 p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1;
855 int diff = (
FFABS(p_l[0] - p_tl[0]) -
FFABS(p_t[0] - p_tl[0])) +
856 (
FFABS(p_l[1] - p_tl[1]) -
FFABS(p_t[1] - p_tl[1])) +
857 (
FFABS(p_l[2] - p_tl[2]) -
FFABS(p_t[2] - p_tl[2])) +
858 (
FFABS(p_l[3] - p_tl[3]) -
FFABS(p_t[3] - p_tl[3]));
869 p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]);
870 p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]);
871 p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]);
872 p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]);
878 return av_clip_uint8(d + (d - c) / 2);
904 uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr;
911 if (x == frame->
width - 1)
916 inverse_predict[
m](p, p_l, p_tl, p_t, p_tr);
946 "invalid predictor mode: %d\n", m);
1027 p[2] =
get_bits(&gb_g, pixel_bits);
1055 int *got_frame,
uint8_t *data_start,
1056 unsigned int data_size,
int is_alpha_chunk)
1061 if (!is_alpha_chunk) {
1070 if (!is_alpha_chunk) {
1112 switch (transform) {
1124 goto free_and_return;
1134 goto free_and_return;
1155 goto free_and_return;
1179 dec = frame->
data[3] + 1;
1180 for (x = 1; x < frame->
width; x++, dec++)
1184 dec = frame->
data[3] + ls;
1185 for (y = 1; y < frame->
height; y++, dec += ls)
1186 *dec += *(dec - ls);
1191 for (y = 1; y < frame->
height; y++) {
1192 dec = frame->
data[3] + y * ls + 1;
1193 for (x = 1; x < frame->
width; x++, dec++)
1198 for (y = 1; y < frame->
height; y++) {
1199 dec = frame->
data[3] + y * ls + 1;
1200 for (x = 1; x < frame->
width; x++, dec++)
1201 *dec += *(dec - ls);
1205 for (y = 1; y < frame->
height; y++) {
1206 dec = frame->
data[3] + y * ls + 1;
1207 for (x = 1; x < frame->
width; x++, dec++)
1208 dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1));
1216 unsigned int data_size)
1225 for (y = 0; y < s->
height; y++)
1230 int alpha_got_frame = 0;
1237 data_start, data_size, 1);
1242 if (!alpha_got_frame) {
1248 for (y = 0; y < s->
height; y++) {
1251 for (x = 0; x < s->
width; x++) {
1268 int *got_frame,
uint8_t *data_start,
1269 unsigned int data_size)
1283 if (data_size > INT_MAX) {
1289 pkt.
data = data_start;
1290 pkt.
size = data_size;
1309 uint32_t chunk_type, chunk_size;
1322 if (bytestream2_get_le32(&gb) !=
MKTAG(
'R',
'I',
'F',
'F')) {
1327 chunk_size = bytestream2_get_le32(&gb);
1331 if (bytestream2_get_le32(&gb) !=
MKTAG(
'W',
'E',
'B',
'P')) {
1337 char chunk_str[5] = { 0 };
1339 chunk_type = bytestream2_get_le32(&gb);
1340 chunk_size = bytestream2_get_le32(&gb);
1341 if (chunk_size == UINT32_MAX)
1343 chunk_size += chunk_size & 1;
1348 switch (chunk_type) {
1349 case MKTAG(
'V',
'P',
'8',
' '):
1359 case MKTAG(
'V',
'P',
'8',
'L'):
1369 case MKTAG(
'V',
'P',
'8',
'X'):
1370 vp8x_flags = bytestream2_get_byte(&gb);
1372 s->
width = bytestream2_get_le24(&gb) + 1;
1373 s->
height = bytestream2_get_le24(&gb) + 1;
1378 case MKTAG(
'A',
'L',
'P',
'H'): {
1379 int alpha_header, filter_m, compression;
1383 "ALPHA chunk present, but alpha bit not set in the "
1386 if (chunk_size == 0) {
1390 alpha_header = bytestream2_get_byte(&gb);
1395 filter_m = (alpha_header >> 2) & 0x03;
1396 compression = alpha_header & 0x03;
1400 "skipping unsupported ALPHA chunk\n");
1409 case MKTAG(
'I',
'C',
'C',
'P'):
1410 case MKTAG(
'A',
'N',
'I',
'M'):
1411 case MKTAG(
'A',
'N',
'M',
'F'):
1412 case MKTAG(
'E',
'X',
'I',
'F'):
1413 case MKTAG(
'X',
'M',
'P',
' '):
1414 AV_WL32(chunk_str, chunk_type);
1420 AV_WL32(chunk_str, chunk_type);