49 #define FRAGMENT_PIXELS 8
58 #define SB_NOT_CODED 0
59 #define SB_PARTIALLY_CODED 1
60 #define SB_FULLY_CODED 2
65 #define MAXIMUM_LONG_BIT_RUN 4129
67 #define MODE_INTER_NO_MV 0
69 #define MODE_INTER_PLUS_MV 2
70 #define MODE_INTER_LAST_MV 3
71 #define MODE_INTER_PRIOR_LAST 4
72 #define MODE_USING_GOLDEN 5
73 #define MODE_GOLDEN_MV 6
74 #define MODE_INTER_FOURMV 7
75 #define CODING_MODE_COUNT 8
124 { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
125 { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
126 { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
127 { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
130 #define MIN_DEQUANT_VAL 2
208 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
209 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
210 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
310 for (i = 0; i < 16; i++) {
335 int sb_x, sb_y, plane;
338 for (plane = 0; plane < 3; plane++) {
346 for (sb_y = 0; sb_y < sb_height; sb_y++)
347 for (sb_x = 0; sb_x < sb_width; sb_x++)
348 for (i = 0; i < 16; i++) {
350 y = 4 * sb_y + hilbert_offset[i][1];
352 if (x < frag_width && y < frag_height)
371 int i, plane, inter, qri, bmi, bmj, qistart;
373 for (inter = 0; inter < 2; inter++) {
374 for (plane = 0; plane < 3; plane++) {
376 for (qri = 0; qri < s->
qr_count[inter][plane]; qri++) {
377 sum += s->
qr_size[inter][plane][qri];
378 if (s->
qps[qpi] <= sum)
381 qistart = sum - s->
qr_size[inter][plane][qri];
382 bmi = s->
qr_base[inter][plane][qri];
383 bmj = s->
qr_base[inter][plane][qri + 1];
384 for (i = 0; i < 64; i++) {
387 s->
qr_size[inter][plane][qri]) /
388 (2 * s->
qr_size[inter][plane][qri]);
390 int qmin = 8 << (inter + !i);
391 int qscale = i ? ac_scale_factor : dc_scale_factor;
394 av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
398 s->
qmat[qpi][inter][plane][0] = s->
qmat[0][inter][plane][0];
421 for (x = 0; x < filter_limit; x++) {
422 bounding_values[-x] = -x;
423 bounding_values[x] = x;
425 for (x = value = filter_limit; x < 128 &&
value; x++, value--) {
426 bounding_values[ x] =
value;
427 bounding_values[-x] = -
value;
430 bounding_values[128] =
value;
431 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
440 int superblock_starts[3] = {
444 int current_superblock = 0;
446 int num_partial_superblocks = 0;
449 int current_fragment;
459 while (current_superblock < s->superblock_count &&
get_bits_left(gb) > 0) {
467 if (current_run == 34)
472 "Invalid partially coded superblock run length\n");
478 current_superblock += current_run;
480 num_partial_superblocks += current_run;
485 if (num_partial_superblocks < s->superblock_count) {
486 int superblocks_decoded = 0;
488 current_superblock = 0;
492 while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
501 if (current_run == 34)
504 for (j = 0; j < current_run; current_superblock++) {
507 "Invalid fully coded superblock run length\n");
517 superblocks_decoded += current_run;
523 if (num_partial_superblocks) {
537 for (plane = 0; plane < 3; plane++) {
538 int sb_start = superblock_starts[plane];
541 int num_coded_frags = 0;
543 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
545 for (j = 0; j < 16; j++) {
548 if (current_fragment != -1) {
554 if (current_run-- == 0) {
577 for (i = 0; i < 64; i++)
592 int i, j, k, sb_x, sb_y;
594 int current_macroblock;
595 int current_fragment;
610 for (i = 0; i < 8; i++)
612 for (i = 0; i < 8; i++)
613 custom_mode_alphabet[
get_bits(gb, 3)] = i;
614 alphabet = custom_mode_alphabet;
625 for (j = 0; j < 4; j++) {
626 int mb_x = 2 * sb_x + (j >> 1);
627 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
634 #define BLOCK_X (2 * mb_x + (k & 1))
635 #define BLOCK_Y (2 * mb_y + (k >> 1))
639 for (k = 0; k < 4; k++) {
657 for (k = 0; k < 4; k++) {
663 #define SET_CHROMA_MODES \
664 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
665 frag[s->fragment_start[1]].coding_method = coding_mode; \
666 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
667 frag[s->fragment_start[2]].coding_method = coding_mode;
676 for (k = 0; k < 2; k++) {
681 for (k = 0; k < 4; k++) {
701 int j, k, sb_x, sb_y;
705 int last_motion_x = 0;
706 int last_motion_y = 0;
707 int prior_last_motion_x = 0;
708 int prior_last_motion_y = 0;
709 int current_macroblock;
710 int current_fragment;
726 for (j = 0; j < 4; j++) {
727 int mb_x = 2 * sb_x + (j >> 1);
728 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
740 if (coding_mode == 0) {
750 prior_last_motion_x = last_motion_x;
751 prior_last_motion_y = last_motion_y;
752 last_motion_x = motion_x[0];
753 last_motion_y = motion_y[0];
759 prior_last_motion_x = last_motion_x;
760 prior_last_motion_y = last_motion_y;
764 for (k = 0; k < 4; k++) {
767 if (coding_mode == 0) {
774 last_motion_x = motion_x[k];
775 last_motion_y = motion_y[k];
785 motion_x[0] = last_motion_x;
786 motion_y[0] = last_motion_y;
795 motion_x[0] = prior_last_motion_x;
796 motion_y[0] = prior_last_motion_y;
799 prior_last_motion_x = last_motion_x;
800 prior_last_motion_y = last_motion_y;
801 last_motion_x = motion_x[0];
802 last_motion_y = motion_y[0];
815 for (k = 0; k < 4; k++) {
819 s->
motion_val[0][current_fragment][0] = motion_x[k];
820 s->
motion_val[0][current_fragment][1] = motion_y[k];
822 s->
motion_val[0][current_fragment][0] = motion_x[0];
823 s->
motion_val[0][current_fragment][1] = motion_y[0];
829 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1] +
830 motion_x[2] + motion_x[3], 2);
831 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1] +
832 motion_y[2] + motion_y[3], 2);
834 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
835 motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
841 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1], 1);
842 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1], 1);
843 motion_x[1] =
RSHIFT(motion_x[2] + motion_x[3], 1);
844 motion_y[1] =
RSHIFT(motion_y[2] + motion_y[3], 1);
846 motion_x[1] = motion_x[0];
847 motion_y[1] = motion_y[0];
849 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
850 motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
853 for (k = 0; k < 2; k++) {
859 for (k = 0; k < 4; k++) {
879 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
882 for (qpi = 0; qpi < s->
nqps - 1 && num_blocks > 0; qpi++) {
883 i = blocks_decoded = num_blocks_at_qpi = 0;
895 if (run_length == 34)
897 blocks_decoded += run_length;
900 num_blocks_at_qpi += run_length;
902 for (j = 0; j < run_length; i++) {
911 }
while (blocks_decoded < num_blocks &&
get_bits_left(gb) > 0);
913 num_blocks -= num_blocks_at_qpi;
944 int16_t *dct_tokens = s->
dct_tokens[plane][coeff_index];
953 "Invalid number of coefficents at level %d\n", coeff_index);
955 if (eob_run > num_coeffs) {
957 blocks_ended = num_coeffs;
958 eob_run -= num_coeffs;
961 blocks_ended = eob_run;
967 dct_tokens[j++] = blocks_ended << 2;
971 token =
get_vlc2(gb, vlc_table, 11, 3);
973 if ((
unsigned) token <= 6
U) {
980 if (eob_run > num_coeffs - coeff_i) {
981 dct_tokens[j++] =
TOKEN_EOB(num_coeffs - coeff_i);
982 blocks_ended += num_coeffs - coeff_i;
983 eob_run -= num_coeffs - coeff_i;
984 coeff_i = num_coeffs;
987 blocks_ended += eob_run;
991 }
else if (token >= 0) {
994 bits_to_get =
get_bits(gb, bits_to_get);
1009 all_fragments[coded_fragment_list[coeff_i]].
dc =
coeff;
1014 if (coeff_index + zero_run > 64) {
1016 "Invalid zero run of %d with %d coeffs left\n",
1017 zero_run, 64 - coeff_index);
1018 zero_run = 64 - coeff_index;
1023 for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1038 for (i = coeff_index + 1; i < 64; i++)
1043 s->
dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1044 else if (coeff_index < 63)
1045 s->
dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1053 int fragment_height);
1065 int residual_eob_run = 0;
1077 0, residual_eob_run);
1078 if (residual_eob_run < 0)
1079 return residual_eob_run;
1086 1, residual_eob_run);
1087 if (residual_eob_run < 0)
1088 return residual_eob_run;
1090 2, residual_eob_run);
1091 if (residual_eob_run < 0)
1092 return residual_eob_run;
1107 for (i = 1; i <= 5; i++) {
1108 y_tables[i] = &s->
ac_vlc_1[ac_y_table];
1109 c_tables[i] = &s->
ac_vlc_1[ac_c_table];
1111 for (i = 6; i <= 14; i++) {
1112 y_tables[i] = &s->
ac_vlc_2[ac_y_table];
1113 c_tables[i] = &s->
ac_vlc_2[ac_c_table];
1115 for (i = 15; i <= 27; i++) {
1116 y_tables[i] = &s->
ac_vlc_3[ac_y_table];
1117 c_tables[i] = &s->
ac_vlc_3[ac_c_table];
1119 for (i = 28; i <= 63; i++) {
1120 y_tables[i] = &s->
ac_vlc_4[ac_y_table];
1121 c_tables[i] = &s->
ac_vlc_4[ac_c_table];
1125 for (i = 1; i <= 63; i++) {
1126 residual_eob_run =
unpack_vlcs(s, gb, y_tables[i], i,
1127 0, residual_eob_run);
1128 if (residual_eob_run < 0)
1129 return residual_eob_run;
1131 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1132 1, residual_eob_run);
1133 if (residual_eob_run < 0)
1134 return residual_eob_run;
1135 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1136 2, residual_eob_run);
1137 if (residual_eob_run < 0)
1138 return residual_eob_run;
1149 #define COMPATIBLE_FRAME(x) \
1150 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1151 #define DC_COEFF(u) s->all_fragments[u].dc
1156 int fragment_height)
1164 int i = first_fragment;
1169 int vl, vul, vu, vur;
1181 static const int predictor_transform[16][4] = {
1195 { -104, 116, 0, 116 },
1197 { -104, 116, 0, 116 }
1206 static const unsigned char compatible_frame[9] = {
1217 int current_frame_type;
1233 for (y = 0; y < fragment_height; y++) {
1235 for (x = 0; x < fragment_width; x++, i++) {
1239 current_frame_type =
1250 u = i - fragment_width;
1255 ul = i - fragment_width - 1;
1260 if (x + 1 < fragment_width) {
1261 ur = i - fragment_width + 1;
1268 if (transform == 0) {
1271 predicted_dc = last_dc[current_frame_type];
1275 (predictor_transform[
transform][0] * vul) +
1276 (predictor_transform[transform][1] * vu) +
1277 (predictor_transform[
transform][2] * vur) +
1278 (predictor_transform[transform][3] * vl);
1280 predicted_dc /= 128;
1284 if ((transform == 15) || (transform == 13)) {
1285 if (
FFABS(predicted_dc - vu) > 128)
1287 else if (
FFABS(predicted_dc - vl) > 128)
1289 else if (
FFABS(predicted_dc - vul) > 128)
1297 last_dc[current_frame_type] =
DC_COEFF(i);
1304 int ystart,
int yend)
1316 plane_data += s->
data_offset[plane] + 8 * ystart * stride;
1318 for (y = ystart; y < yend; y++) {
1319 for (x = 0; x <
width; x++) {
1329 stride, bounding_values);
1336 stride, bounding_values);
1342 if ((x < width - 1) &&
1345 plane_data + 8 * x + 8,
1346 stride, bounding_values);
1352 if ((y < height - 1) &&
1355 plane_data + 8 * x + 8 * stride,
1356 stride, bounding_values);
1362 plane_data += 8 * stride;
1371 int plane,
int inter, int16_t
block[64])
1373 int16_t *dequantizer = s->
qmat[frag->
qpi][inter][plane];
1379 switch (token & 3) {
1388 i += (token >> 2) & 0x7f;
1393 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1397 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1408 block[0] = frag->
dc * s->
qmat[0][inter][plane][0];
1458 int motion_y,
int y)
1462 int border = motion_y & 1;
1470 ref_row = y + (motion_y >> 1);
1471 ref_row =
FFMAX(
FFABS(ref_row), ref_row + 8 + border);
1482 int x,
y, i, j, fragment;
1484 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1485 int motion_halfpel_index;
1487 int plane, first_pixel;
1492 for (plane = 0; plane < 3; plane++) {
1502 int8_t(*motion_val)[2] = s->
motion_val[!!plane];
1513 int do_await = !plane && HAVE_THREADS &&
1522 for (; sb_y < slice_height; sb_y++) {
1524 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1526 for (j = 0; j < 16; j++) {
1528 y = 4 * sb_y + hilbert_offset[j][1];
1529 fragment = y * fragment_width + x;
1531 i = fragment_start + fragment;
1534 if (x >= fragment_width || y >= fragment_height)
1537 first_pixel = 8 * y * stride + 8 * x;
1542 motion_val[fragment][1],
1549 motion_source = golden_plane;
1551 motion_source = last_plane;
1553 motion_source += first_pixel;
1554 motion_halfpel_index = 0;
1561 motion_x = motion_val[fragment][0];
1562 motion_y = motion_val[fragment][1];
1564 src_x = (motion_x >> 1) + 8 * x;
1565 src_y = (motion_y >> 1) + 8 * y;
1567 motion_halfpel_index = motion_x & 0x01;
1568 motion_source += (motion_x >> 1);
1570 motion_halfpel_index |= (motion_y & 0x01) << 1;
1571 motion_source += ((motion_y >> 1) * stride);
1573 if (src_x < 0 || src_y < 0 ||
1574 src_x + 9 >= plane_width ||
1575 src_y + 9 >= plane_height) {
1585 motion_source =
temp;
1596 if (motion_halfpel_index != 3) {
1598 output_plane + first_pixel,
1599 motion_source, stride, 8);
1603 int d = (motion_x ^ motion_y) >> 31;
1606 motion_source + stride + 1 + d,
1633 output_plane + first_pixel,
1634 last_plane + first_pixel,
1643 FFMIN(4 * sb_y + 3, fragment_height - 1));
1663 int y_fragment_count, c_fragment_count;
1716 int i, inter, plane,
ret;
1719 int y_fragment_count, c_fragment_count;
1742 for (i = 0; i < 64; i++) {
1743 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
1751 for (i = 0; i < 3; i++)
1788 for (i = 0; i < 64; i++) {
1797 for (inter = 0; inter < 2; inter++) {
1798 for (plane = 0; plane < 3; plane++) {
1800 s->
qr_size[inter][plane][0] = 63;
1802 s->
qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
1807 for (i = 0; i < 16; i++) {
1834 for (i = 0; i < 16; i++) {
1915 if (src->
f->
data[0])
1933 int qps_changed = 0, i, err;
1935 #define copy_fields(to, from, start_field, end_field) \
1936 memcpy(&to->start_field, &from->start_field, \
1937 (char *) &to->end_field - (char *) &to->start_field)
1939 if (!
s1->current_frame.f->data[0] ||
1949 int y_fragment_count, c_fragment_count;
1957 y_fragment_count *
sizeof(*s->
motion_val[0]));
1959 c_fragment_count *
sizeof(*s->
motion_val[1]));
1969 for (i = 0; i < 3; i++) {
1970 if (s->
qps[i] !=
s1->qps[1]) {
1972 memcpy(&s->
qmat[i], &
s1->qmat[i],
sizeof(s->
qmat[i]));
1976 if (s->
qps[0] !=
s1->qps[0])
1989 void *
data,
int *got_frame,
1993 int buf_size = avpkt->
size;
2000 #if CONFIG_THEORA_DECODER
2006 av_log(avctx,
AV_LOG_ERROR,
"midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2018 }
else if (type == 2) {
2028 "Header packet passed to frame decoder, skipping\n");
2040 for (i = 0; i < 3; i++)
2046 }
while (
s->theora >= 0x030200 &&
s->nqps < 3 &&
get_bits1(&gb));
2047 for (i =
s->nqps; i < 3; i++)
2052 s->keyframe ?
"key" :
"", avctx->frame_number + 1,
s->qps[0]);
2054 s->skip_loop_filter = !
s->filter_limit_values[
s->qps[0]] ||
2058 if (
s->qps[0] !=
s->last_qps[0])
2061 for (i = 0; i <
s->nqps; i++)
2064 if (
s->qps[i] !=
s->last_qps[i] ||
s->qps[0] !=
s->last_qps[0])
2072 s->current_frame.f->key_frame =
s->keyframe;
2076 if (!
s->edge_emu_buffer)
2077 s->edge_emu_buffer =
av_malloc(9 *
FFABS(
s->current_frame.f->linesize[0]));
2085 if (avctx->frame_number == 0)
2087 "VP version: %d\n",
s->version);
2090 if (
s->version ||
s->theora) {
2093 "Warning, unsupported keyframe coding type?!\n");
2097 if (!
s->golden_frame.f->data[0]) {
2099 "vp3: first frame not a keyframe\n");
2107 &
s->golden_frame)) < 0)
2113 memset(
s->all_fragments, 0,
s->fragment_count *
sizeof(
Vp3Fragment));
2137 for (i = 0; i < 3; i++) {
2138 int height =
s->height >> (i &&
s->chroma_y_shift);
2139 if (
s->flipped_image)
2140 s->data_offset[i] = 0;
2142 s->data_offset[i] = (height - 1) *
s->current_frame.f->linesize[i];
2145 s->last_slice_end = 0;
2146 for (i = 0; i <
s->c_superblock_height; i++)
2150 for (i = 0; i < 3; i++) {
2151 int row = (
s->height >> (3 + (i &&
s->chroma_y_shift))) - 1;
2188 av_dlog(avctx,
"hti %d hbits %x token %d entry : %d size %d\n",
2228 #if CONFIG_THEORA_DECODER
2236 int visible_width, visible_height,
colorspace;
2237 int offset_x = 0, offset_y = 0;
2246 if (s->
theora < 0x030200) {
2249 "Old (<alpha3) Theora bitstream, flipped image\n");
2257 if (s->
theora >= 0x030200) {
2267 if (fps.
num && fps.
den) {
2268 if (fps.
num < 0 || fps.
den < 0) {
2273 fps.
den, fps.
num, 1 << 30);
2278 if (aspect.
num && aspect.
den) {
2281 aspect.
num, aspect.
den, 1 << 30);
2285 if (s->
theora < 0x030200)
2292 if (s->
theora >= 0x030200) {
2304 if (visible_width <= s->
width && visible_width > s->
width - 16 &&
2305 visible_height <= s->height && visible_height > s->
height - 16 &&
2306 !offset_x && (offset_y == s->
height - visible_height))
2313 if (colorspace == 1)
2315 else if (colorspace == 2)
2318 if (colorspace == 1 || colorspace == 2) {
2329 int i,
n, matrices, inter, plane;
2331 if (s->
theora >= 0x030200) {
2335 for (i = 0; i < 64; i++)
2339 if (s->
theora >= 0x030200)
2344 for (i = 0; i < 64; i++)
2347 if (s->
theora >= 0x030200)
2352 for (i = 0; i < 64; i++)
2355 if (s->
theora >= 0x030200)
2360 if (matrices > 384) {
2365 for (n = 0; n < matrices; n++)
2366 for (i = 0; i < 64; i++)
2369 for (inter = 0; inter <= 1; inter++) {
2370 for (plane = 0; plane <= 2; plane++) {
2372 if (inter || plane > 0)
2380 qtj = (3 * inter + plane - 1) / 3;
2381 plj = (plane + 2) % 3;
2394 if (i >= matrices) {
2396 "invalid base matrix index\n");
2399 s->
qr_base[inter][plane][qri] = i;
2403 s->
qr_size[inter][plane][qri++] = i;
2417 for (s->
hti = 0; s->
hti < 80; s->
hti++) {
2454 42, header_start, header_len) < 0) {
2459 for (i = 0; i < 3; i++) {
2460 if (header_len[i] <= 0)
2466 if (!(ptype & 0x80)) {
2489 "Unknown Theora config packet: %d\n", ptype & ~0x80);
2494 "%d bits left in packet %X\n",
2496 if (s->
theora < 0x030200)
2509 .
init = theora_decode_init,