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
211 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
212 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
213 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
313 for (i = 0; i < 16; i++) {
338 int sb_x, sb_y, plane;
341 for (plane = 0; plane < 3; plane++) {
349 for (sb_y = 0; sb_y < sb_height; sb_y++)
350 for (sb_x = 0; sb_x < sb_width; sb_x++)
351 for (i = 0; i < 16; i++) {
353 y = 4 * sb_y + hilbert_offset[i][1];
355 if (x < frag_width && y < frag_height)
374 int i, plane, inter, qri, bmi, bmj, qistart;
376 for (inter = 0; inter < 2; inter++) {
377 for (plane = 0; plane < 3; plane++) {
379 for (qri = 0; qri < s->
qr_count[inter][plane]; qri++) {
380 sum += s->
qr_size[inter][plane][qri];
381 if (s->
qps[qpi] <= sum)
384 qistart = sum - s->
qr_size[inter][plane][qri];
385 bmi = s->
qr_base[inter][plane][qri];
386 bmj = s->
qr_base[inter][plane][qri + 1];
387 for (i = 0; i < 64; i++) {
390 s->
qr_size[inter][plane][qri]) /
391 (2 * s->
qr_size[inter][plane][qri]);
393 int qmin = 8 << (inter + !i);
394 int qscale = i ? ac_scale_factor : dc_scale_factor;
397 av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
401 s->
qmat[qpi][inter][plane][0] = s->
qmat[0][inter][plane][0];
424 for (x = 0; x < filter_limit; x++) {
425 bounding_values[-x] = -x;
426 bounding_values[x] = x;
428 for (x = value = filter_limit; x < 128 &&
value; x++, value--) {
429 bounding_values[ x] =
value;
430 bounding_values[-x] = -
value;
433 bounding_values[128] =
value;
434 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
443 int superblock_starts[3] = {
447 int current_superblock = 0;
449 int num_partial_superblocks = 0;
452 int current_fragment;
462 while (current_superblock < s->superblock_count &&
get_bits_left(gb) > 0) {
470 if (current_run == 34)
475 "Invalid partially coded superblock run length\n");
481 current_superblock += current_run;
483 num_partial_superblocks += current_run;
488 if (num_partial_superblocks < s->superblock_count) {
489 int superblocks_decoded = 0;
491 current_superblock = 0;
495 while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
504 if (current_run == 34)
507 for (j = 0; j < current_run; current_superblock++) {
510 "Invalid fully coded superblock run length\n");
520 superblocks_decoded += current_run;
526 if (num_partial_superblocks) {
540 for (plane = 0; plane < 3; plane++) {
541 int sb_start = superblock_starts[plane];
544 int num_coded_frags = 0;
546 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
548 for (j = 0; j < 16; j++) {
551 if (current_fragment != -1) {
557 if (current_run-- == 0) {
580 for (i = 0; i < 64; i++)
595 int i, j, k, sb_x, sb_y;
597 int current_macroblock;
598 int current_fragment;
613 for (i = 0; i < 8; i++)
615 for (i = 0; i < 8; i++)
616 custom_mode_alphabet[
get_bits(gb, 3)] = i;
617 alphabet = custom_mode_alphabet;
628 for (j = 0; j < 4; j++) {
629 int mb_x = 2 * sb_x + (j >> 1);
630 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
637 #define BLOCK_X (2 * mb_x + (k & 1))
638 #define BLOCK_Y (2 * mb_y + (k >> 1))
642 for (k = 0; k < 4; k++) {
660 for (k = 0; k < 4; k++) {
666 #define SET_CHROMA_MODES \
667 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
668 frag[s->fragment_start[1]].coding_method = coding_mode; \
669 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
670 frag[s->fragment_start[2]].coding_method = coding_mode;
679 for (k = 0; k < 2; k++) {
684 for (k = 0; k < 4; k++) {
704 int j, k, sb_x, sb_y;
708 int last_motion_x = 0;
709 int last_motion_y = 0;
710 int prior_last_motion_x = 0;
711 int prior_last_motion_y = 0;
712 int current_macroblock;
713 int current_fragment;
729 for (j = 0; j < 4; j++) {
730 int mb_x = 2 * sb_x + (j >> 1);
731 int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
743 if (coding_mode == 0) {
753 prior_last_motion_x = last_motion_x;
754 prior_last_motion_y = last_motion_y;
755 last_motion_x = motion_x[0];
756 last_motion_y = motion_y[0];
762 prior_last_motion_x = last_motion_x;
763 prior_last_motion_y = last_motion_y;
767 for (k = 0; k < 4; k++) {
770 if (coding_mode == 0) {
777 last_motion_x = motion_x[k];
778 last_motion_y = motion_y[k];
788 motion_x[0] = last_motion_x;
789 motion_y[0] = last_motion_y;
798 motion_x[0] = prior_last_motion_x;
799 motion_y[0] = prior_last_motion_y;
802 prior_last_motion_x = last_motion_x;
803 prior_last_motion_y = last_motion_y;
804 last_motion_x = motion_x[0];
805 last_motion_y = motion_y[0];
818 for (k = 0; k < 4; k++) {
822 s->
motion_val[0][current_fragment][0] = motion_x[k];
823 s->
motion_val[0][current_fragment][1] = motion_y[k];
825 s->
motion_val[0][current_fragment][0] = motion_x[0];
826 s->
motion_val[0][current_fragment][1] = motion_y[0];
832 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1] +
833 motion_x[2] + motion_x[3], 2);
834 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1] +
835 motion_y[2] + motion_y[3], 2);
837 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
838 motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
844 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1], 1);
845 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1], 1);
846 motion_x[1] =
RSHIFT(motion_x[2] + motion_x[3], 1);
847 motion_y[1] =
RSHIFT(motion_y[2] + motion_y[3], 1);
849 motion_x[1] = motion_x[0];
850 motion_y[1] = motion_y[0];
852 motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
853 motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
856 for (k = 0; k < 2; k++) {
862 for (k = 0; k < 4; k++) {
882 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
885 for (qpi = 0; qpi < s->
nqps - 1 && num_blocks > 0; qpi++) {
886 i = blocks_decoded = num_blocks_at_qpi = 0;
898 if (run_length == 34)
900 blocks_decoded += run_length;
903 num_blocks_at_qpi += run_length;
905 for (j = 0; j < run_length; i++) {
914 }
while (blocks_decoded < num_blocks &&
get_bits_left(gb) > 0);
916 num_blocks -= num_blocks_at_qpi;
947 int16_t *dct_tokens = s->
dct_tokens[plane][coeff_index];
956 "Invalid number of coefficents at level %d\n", coeff_index);
958 if (eob_run > num_coeffs) {
960 blocks_ended = num_coeffs;
961 eob_run -= num_coeffs;
964 blocks_ended = eob_run;
970 dct_tokens[j++] = blocks_ended << 2;
974 token =
get_vlc2(gb, vlc_table, 11, 3);
976 if ((
unsigned) token <= 6
U) {
983 if (eob_run > num_coeffs - coeff_i) {
984 dct_tokens[j++] =
TOKEN_EOB(num_coeffs - coeff_i);
985 blocks_ended += num_coeffs - coeff_i;
986 eob_run -= num_coeffs - coeff_i;
987 coeff_i = num_coeffs;
990 blocks_ended += eob_run;
994 }
else if (token >= 0) {
997 bits_to_get =
get_bits(gb, bits_to_get);
1012 all_fragments[coded_fragment_list[coeff_i]].
dc =
coeff;
1017 if (coeff_index + zero_run > 64) {
1019 "Invalid zero run of %d with %d coeffs left\n",
1020 zero_run, 64 - coeff_index);
1021 zero_run = 64 - coeff_index;
1026 for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
1041 for (i = coeff_index + 1; i < 64; i++)
1046 s->
dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
1047 else if (coeff_index < 63)
1048 s->
dct_tokens[0][coeff_index + 1] = dct_tokens + j;
1056 int fragment_height);
1068 int residual_eob_run = 0;
1080 0, residual_eob_run);
1081 if (residual_eob_run < 0)
1082 return residual_eob_run;
1089 1, residual_eob_run);
1090 if (residual_eob_run < 0)
1091 return residual_eob_run;
1093 2, residual_eob_run);
1094 if (residual_eob_run < 0)
1095 return residual_eob_run;
1110 for (i = 1; i <= 5; i++) {
1111 y_tables[i] = &s->
ac_vlc_1[ac_y_table];
1112 c_tables[i] = &s->
ac_vlc_1[ac_c_table];
1114 for (i = 6; i <= 14; i++) {
1115 y_tables[i] = &s->
ac_vlc_2[ac_y_table];
1116 c_tables[i] = &s->
ac_vlc_2[ac_c_table];
1118 for (i = 15; i <= 27; i++) {
1119 y_tables[i] = &s->
ac_vlc_3[ac_y_table];
1120 c_tables[i] = &s->
ac_vlc_3[ac_c_table];
1122 for (i = 28; i <= 63; i++) {
1123 y_tables[i] = &s->
ac_vlc_4[ac_y_table];
1124 c_tables[i] = &s->
ac_vlc_4[ac_c_table];
1128 for (i = 1; i <= 63; i++) {
1129 residual_eob_run =
unpack_vlcs(s, gb, y_tables[i], i,
1130 0, residual_eob_run);
1131 if (residual_eob_run < 0)
1132 return residual_eob_run;
1134 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1135 1, residual_eob_run);
1136 if (residual_eob_run < 0)
1137 return residual_eob_run;
1138 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1139 2, residual_eob_run);
1140 if (residual_eob_run < 0)
1141 return residual_eob_run;
1152 #define COMPATIBLE_FRAME(x) \
1153 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1154 #define DC_COEFF(u) s->all_fragments[u].dc
1159 int fragment_height)
1167 int i = first_fragment;
1172 int vl, vul, vu, vur;
1184 static const int predictor_transform[16][4] = {
1198 { -104, 116, 0, 116 },
1200 { -104, 116, 0, 116 }
1209 static const unsigned char compatible_frame[9] = {
1220 int current_frame_type;
1236 for (y = 0; y < fragment_height; y++) {
1238 for (x = 0; x < fragment_width; x++, i++) {
1242 current_frame_type =
1253 u = i - fragment_width;
1258 ul = i - fragment_width - 1;
1263 if (x + 1 < fragment_width) {
1264 ur = i - fragment_width + 1;
1271 if (transform == 0) {
1274 predicted_dc = last_dc[current_frame_type];
1278 (predictor_transform[
transform][0] * vul) +
1279 (predictor_transform[transform][1] * vu) +
1280 (predictor_transform[
transform][2] * vur) +
1281 (predictor_transform[transform][3] * vl);
1283 predicted_dc /= 128;
1287 if ((transform == 15) || (transform == 13)) {
1288 if (
FFABS(predicted_dc - vu) > 128)
1290 else if (
FFABS(predicted_dc - vl) > 128)
1292 else if (
FFABS(predicted_dc - vul) > 128)
1300 last_dc[current_frame_type] =
DC_COEFF(i);
1307 int ystart,
int yend)
1319 plane_data += s->
data_offset[plane] + 8 * ystart * stride;
1321 for (y = ystart; y < yend; y++) {
1322 for (x = 0; x <
width; x++) {
1332 stride, bounding_values);
1339 stride, bounding_values);
1345 if ((x < width - 1) &&
1348 plane_data + 8 * x + 8,
1349 stride, bounding_values);
1355 if ((y < height - 1) &&
1358 plane_data + 8 * x + 8 * stride,
1359 stride, bounding_values);
1365 plane_data += 8 * stride;
1374 int plane,
int inter, int16_t
block[64])
1376 int16_t *dequantizer = s->
qmat[frag->
qpi][inter][plane];
1382 switch (token & 3) {
1391 i += (token >> 2) & 0x7f;
1396 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1400 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1411 block[0] = frag->
dc * s->
qmat[0][inter][plane][0];
1430 y_flipped == s->
height ? INT_MAX
1461 int motion_y,
int y)
1465 int border = motion_y & 1;
1473 ref_row = y + (motion_y >> 1);
1474 ref_row =
FFMAX(
FFABS(ref_row), ref_row + 8 + border);
1485 int x,
y, i, j, fragment;
1487 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1488 int motion_halfpel_index;
1490 int plane, first_pixel;
1495 for (plane = 0; plane < 3; plane++) {
1505 int8_t(*motion_val)[2] = s->
motion_val[!!plane];
1516 int do_await = !plane && HAVE_THREADS &&
1525 for (; sb_y < slice_height; sb_y++) {
1527 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1529 for (j = 0; j < 16; j++) {
1531 y = 4 * sb_y + hilbert_offset[j][1];
1532 fragment = y * fragment_width + x;
1534 i = fragment_start + fragment;
1537 if (x >= fragment_width || y >= fragment_height)
1540 first_pixel = 8 * y * stride + 8 * x;
1545 motion_val[fragment][1],
1552 motion_source = golden_plane;
1554 motion_source = last_plane;
1556 motion_source += first_pixel;
1557 motion_halfpel_index = 0;
1564 motion_x = motion_val[fragment][0];
1565 motion_y = motion_val[fragment][1];
1567 src_x = (motion_x >> 1) + 8 * x;
1568 src_y = (motion_y >> 1) + 8 * y;
1570 motion_halfpel_index = motion_x & 0x01;
1571 motion_source += (motion_x >> 1);
1573 motion_halfpel_index |= (motion_y & 0x01) << 1;
1574 motion_source += ((motion_y >> 1) * stride);
1576 if (src_x < 0 || src_y < 0 ||
1577 src_x + 9 >= plane_width ||
1578 src_y + 9 >= plane_height) {
1588 motion_source =
temp;
1599 if (motion_halfpel_index != 3) {
1601 output_plane + first_pixel,
1602 motion_source, stride, 8);
1606 int d = (motion_x ^ motion_y) >> 31;
1609 motion_source + stride + 1 + d,
1636 output_plane + first_pixel,
1637 last_plane + first_pixel,
1646 FFMIN(4 * sb_y + 3, fragment_height - 1));
1666 int y_fragment_count, c_fragment_count;
1719 int i, inter, plane,
ret;
1722 int y_fragment_count, c_fragment_count;
1745 for (i = 0; i < 64; i++) {
1746 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
1754 for (i = 0; i < 3; i++)
1791 for (i = 0; i < 64; i++) {
1800 for (inter = 0; inter < 2; inter++) {
1801 for (plane = 0; plane < 3; plane++) {
1803 s->
qr_size[inter][plane][0] = 63;
1805 s->
qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
1810 for (i = 0; i < 16; i++) {
1837 for (i = 0; i < 16; i++) {
1918 if (src->
f->
data[0])
1936 int qps_changed = 0, i, err;
1938 #define copy_fields(to, from, start_field, end_field) \
1939 memcpy(&to->start_field, &from->start_field, \
1940 (char *) &to->end_field - (char *) &to->start_field)
1942 if (!
s1->current_frame.f->data[0] ||
1952 int y_fragment_count, c_fragment_count;
1960 y_fragment_count *
sizeof(*s->
motion_val[0]));
1962 c_fragment_count *
sizeof(*s->
motion_val[1]));
1972 for (i = 0; i < 3; i++) {
1973 if (s->
qps[i] !=
s1->qps[1]) {
1975 memcpy(&s->
qmat[i], &
s1->qmat[i],
sizeof(s->
qmat[i]));
1979 if (s->
qps[0] !=
s1->qps[0])
1992 void *
data,
int *got_frame,
1996 int buf_size = avpkt->
size;
2004 #if CONFIG_THEORA_DECODER
2010 av_log(avctx,
AV_LOG_ERROR,
"midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
2022 }
else if (type == 2) {
2032 "Header packet passed to frame decoder, skipping\n");
2044 for (i = 0; i < 3; i++)
2050 }
while (
s->theora >= 0x030200 &&
s->nqps < 3 &&
get_bits1(&gb));
2051 for (i =
s->nqps; i < 3; i++)
2056 s->keyframe ?
"key" :
"", avctx->frame_number + 1,
s->qps[0]);
2058 s->skip_loop_filter = !
s->filter_limit_values[
s->qps[0]] ||
2062 if (
s->qps[0] !=
s->last_qps[0])
2065 for (i = 0; i <
s->nqps; i++)
2068 if (
s->qps[i] !=
s->last_qps[i] ||
s->qps[0] !=
s->last_qps[0])
2076 s->current_frame.f->key_frame =
s->keyframe;
2080 if (!
s->edge_emu_buffer)
2081 s->edge_emu_buffer =
av_malloc(9 *
FFABS(
s->current_frame.f->linesize[0]));
2089 if (avctx->frame_number == 0)
2091 "VP version: %d\n",
s->version);
2094 if (
s->version ||
s->theora) {
2097 "Warning, unsupported keyframe coding type?!\n");
2101 if (!
s->golden_frame.f->data[0]) {
2103 "vp3: first frame not a keyframe\n");
2111 &
s->golden_frame)) < 0)
2117 memset(
s->all_fragments, 0,
s->fragment_count *
sizeof(
Vp3Fragment));
2141 for (i = 0; i < 3; i++) {
2142 int height =
s->height >> (i &&
s->chroma_y_shift);
2143 if (
s->flipped_image)
2144 s->data_offset[i] = 0;
2146 s->data_offset[i] = (height - 1) *
s->current_frame.f->linesize[i];
2149 s->last_slice_end = 0;
2150 for (i = 0; i <
s->c_superblock_height; i++)
2154 for (i = 0; i < 3; i++) {
2155 int row = (
s->height >> (3 + (i &&
s->chroma_y_shift))) - 1;
2163 for (i = 0; i < 3; i++) {
2165 int off = (
s->offset_x >> (i &&
s->chroma_y_shift)) +
2166 (
s->offset_y >> (i &&
s->chroma_y_shift)) * dst->
linesize[i];
2167 dst->
data[i] += off;
2199 av_dlog(avctx,
"hti %d hbits %x token %d entry : %d size %d\n",
2239 #if CONFIG_THEORA_DECODER
2247 int visible_width, visible_height,
colorspace;
2248 uint8_t offset_x = 0, offset_y = 0;
2257 if (s->
theora < 0x030200) {
2260 "Old (<alpha3) Theora bitstream, flipped image\n");
2268 if (s->
theora >= 0x030200) {
2278 visible_width + offset_x > s->
width ||
2279 visible_height + offset_y > s->
height) {
2281 "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
2282 visible_width, visible_height, offset_x, offset_y,
2289 if (fps.
num && fps.
den) {
2290 if (fps.
num < 0 || fps.
den < 0) {
2295 fps.
den, fps.
num, 1 << 30);
2300 if (aspect.
num && aspect.
den) {
2303 aspect.
num, aspect.
den, 1 << 30);
2307 if (s->
theora < 0x030200)
2314 if (s->
theora >= 0x030200) {
2328 avctx->
width = visible_width;
2329 avctx->
height = visible_height;
2340 "chroma samples to preserve alignment.\n",
2346 if (colorspace == 1)
2348 else if (colorspace == 2)
2351 if (colorspace == 1 || colorspace == 2) {
2362 int i,
n, matrices, inter, plane;
2364 if (s->
theora >= 0x030200) {
2368 for (i = 0; i < 64; i++)
2372 if (s->
theora >= 0x030200)
2377 for (i = 0; i < 64; i++)
2380 if (s->
theora >= 0x030200)
2385 for (i = 0; i < 64; i++)
2388 if (s->
theora >= 0x030200)
2393 if (matrices > 384) {
2398 for (n = 0; n < matrices; n++)
2399 for (i = 0; i < 64; i++)
2402 for (inter = 0; inter <= 1; inter++) {
2403 for (plane = 0; plane <= 2; plane++) {
2405 if (inter || plane > 0)
2413 qtj = (3 * inter + plane - 1) / 3;
2414 plj = (plane + 2) % 3;
2427 if (i >= matrices) {
2429 "invalid base matrix index\n");
2432 s->
qr_base[inter][plane][qri] = i;
2436 s->
qr_size[inter][plane][qri++] = i;
2450 for (s->
hti = 0; s->
hti < 80; s->
hti++) {
2473 const uint8_t *header_start[3];
2487 42, header_start, header_len) < 0) {
2492 for (i = 0; i < 3; i++) {
2493 if (header_len[i] <= 0)
2499 if (!(ptype & 0x80)) {
2522 "Unknown Theora config packet: %d\n", ptype & ~0x80);
2527 "%d bits left in packet %X\n",
2529 if (s->
theora < 0x030200)
2542 .
init = theora_decode_init,