47 #define FRAGMENT_PIXELS 8
56 #define SB_NOT_CODED 0
57 #define SB_PARTIALLY_CODED 1
58 #define SB_FULLY_CODED 2
63 #define MAXIMUM_LONG_BIT_RUN 4129
65 #define MODE_INTER_NO_MV 0
67 #define MODE_INTER_PLUS_MV 2
68 #define MODE_INTER_LAST_MV 3
69 #define MODE_INTER_PRIOR_LAST 4
70 #define MODE_USING_GOLDEN 5
71 #define MODE_GOLDEN_MV 6
72 #define MODE_INTER_FOURMV 7
73 #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)
304 for (i = 0; i < 16; i++) {
332 int sb_x, sb_y, plane;
335 for (plane = 0; plane < 3; plane++) {
341 for (sb_y = 0; sb_y < sb_height; sb_y++)
342 for (sb_x = 0; sb_x < sb_width; sb_x++)
343 for (i = 0; i < 16; i++) {
345 y = 4*sb_y + hilbert_offset[i][1];
347 if (x < frag_width && y < frag_height)
365 int i, plane, inter, qri, bmi, bmj, qistart;
367 for(inter=0; inter<2; inter++){
368 for(plane=0; plane<3; plane++){
370 for(qri=0; qri<s->
qr_count[inter][plane]; qri++){
371 sum+= s->
qr_size[inter][plane][qri];
372 if(s->
qps[qpi] <= sum)
375 qistart= sum - s->
qr_size[inter][plane][qri];
376 bmi= s->
qr_base[inter][plane][qri ];
377 bmj= s->
qr_base[inter][plane][qri+1];
381 + s->
qr_size[inter][plane][qri])
382 / (2*s->
qr_size[inter][plane][qri]);
384 int qmin= 8<<(inter + !i);
385 int qscale= i ? ac_scale_factor : dc_scale_factor;
390 s->
qmat[qpi][inter][plane][0] = s->
qmat[0][inter][plane][0];
413 for (x = 0; x < filter_limit; x++) {
414 bounding_values[-x] = -x;
415 bounding_values[x] = x;
417 for (x = value = filter_limit; x < 128 &&
value; x++, value--) {
418 bounding_values[ x] =
value;
419 bounding_values[-x] = -
value;
422 bounding_values[128] =
value;
423 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
434 int current_superblock = 0;
436 int num_partial_superblocks = 0;
439 int current_fragment;
451 while (current_superblock < s->superblock_count &&
get_bits_left(gb) > 0) {
459 if (current_run == 34)
469 current_superblock += current_run;
471 num_partial_superblocks += current_run;
476 if (num_partial_superblocks < s->superblock_count) {
477 int superblocks_decoded = 0;
479 current_superblock = 0;
483 while (superblocks_decoded < s->superblock_count - num_partial_superblocks
493 if (current_run == 34)
496 for (j = 0; j < current_run; current_superblock++) {
508 superblocks_decoded += current_run;
514 if (num_partial_superblocks) {
529 for (plane = 0; plane < 3; plane++) {
530 int sb_start = superblock_starts[plane];
532 int num_coded_frags = 0;
534 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
537 for (j = 0; j < 16; j++) {
541 if (current_fragment != -1) {
548 if (current_run-- == 0) {
572 for (i = 0; i < 64; i++)
586 int i, j, k, sb_x, sb_y;
588 int current_macroblock;
589 int current_fragment;
606 for (i = 0; i < 8; i++)
608 for (i = 0; i < 8; i++)
609 custom_mode_alphabet[
get_bits(gb, 3)] = i;
610 alphabet = custom_mode_alphabet;
621 for (j = 0; j < 4; j++) {
622 int mb_x = 2*sb_x + (j>>1);
623 int mb_y = 2*sb_y + (((j>>1)+j)&1);
629 #define BLOCK_X (2*mb_x + (k&1))
630 #define BLOCK_Y (2*mb_y + (k>>1))
633 for (k = 0; k < 4; k++) {
647 coding_mode = alphabet
651 for (k = 0; k < 4; k++) {
657 #define SET_CHROMA_MODES \
658 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
659 frag[s->fragment_start[1]].coding_method = coding_mode;\
660 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
661 frag[s->fragment_start[2]].coding_method = coding_mode;
668 for (k = 0; k < 2; k++) {
673 for (k = 0; k < 4; k++) {
692 int j, k, sb_x, sb_y;
696 int last_motion_x = 0;
697 int last_motion_y = 0;
698 int prior_last_motion_x = 0;
699 int prior_last_motion_y = 0;
700 int current_macroblock;
701 int current_fragment;
717 for (j = 0; j < 4; j++) {
718 int mb_x = 2*sb_x + (j>>1);
719 int mb_y = 2*sb_y + (((j>>1)+j)&1);
731 if (coding_mode == 0) {
742 prior_last_motion_x = last_motion_x;
743 prior_last_motion_y = last_motion_y;
744 last_motion_x = motion_x[0];
745 last_motion_y = motion_y[0];
751 prior_last_motion_x = last_motion_x;
752 prior_last_motion_y = last_motion_y;
756 for (k = 0; k < 4; k++) {
759 if (coding_mode == 0) {
766 last_motion_x = motion_x[k];
767 last_motion_y = motion_y[k];
777 motion_x[0] = last_motion_x;
778 motion_y[0] = last_motion_y;
787 motion_x[0] = prior_last_motion_x;
788 motion_y[0] = prior_last_motion_y;
791 prior_last_motion_x = last_motion_x;
792 prior_last_motion_y = last_motion_y;
793 last_motion_x = motion_x[0];
794 last_motion_y = motion_y[0];
807 for (k = 0; k < 4; k++) {
811 s->
motion_val[0][current_fragment][0] = motion_x[k];
812 s->
motion_val[0][current_fragment][1] = motion_y[k];
814 s->
motion_val[0][current_fragment][0] = motion_x[0];
815 s->
motion_val[0][current_fragment][1] = motion_y[0];
821 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
822 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
824 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
825 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
831 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1], 1);
832 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1], 1);
833 motion_x[1] =
RSHIFT(motion_x[2] + motion_x[3], 1);
834 motion_y[1] =
RSHIFT(motion_y[2] + motion_y[3], 1);
836 motion_x[1] = motion_x[0];
837 motion_y[1] = motion_y[0];
839 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
840 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
843 for (k = 0; k < 2; k++) {
849 for (k = 0; k < 4; k++) {
869 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
872 for (qpi = 0; qpi < s->
nqps-1 && num_blocks > 0; qpi++) {
873 i = blocks_decoded = num_blocks_at_qpi = 0;
885 if (run_length == 34)
887 blocks_decoded += run_length;
890 num_blocks_at_qpi += run_length;
892 for (j = 0; j < run_length; i++) {
901 }
while (blocks_decoded < num_blocks &&
get_bits_left(gb) > 0);
903 num_blocks -= num_blocks_at_qpi;
934 int16_t *dct_tokens = s->
dct_tokens[plane][coeff_index];
944 if (eob_run > num_coeffs) {
945 coeff_i = blocks_ended = num_coeffs;
946 eob_run -= num_coeffs;
948 coeff_i = blocks_ended = eob_run;
954 dct_tokens[j++] = blocks_ended << 2;
958 token =
get_vlc2(gb, vlc_table, 11, 3);
960 if ((
unsigned) token <= 6
U) {
967 if (eob_run > num_coeffs - coeff_i) {
968 dct_tokens[j++] =
TOKEN_EOB(num_coeffs - coeff_i);
969 blocks_ended += num_coeffs - coeff_i;
970 eob_run -= num_coeffs - coeff_i;
971 coeff_i = num_coeffs;
974 blocks_ended += eob_run;
978 }
else if (token >= 0) {
981 bits_to_get =
get_bits(gb, bits_to_get);
996 all_fragments[coded_fragment_list[coeff_i]].
dc =
coeff;
1001 if (coeff_index + zero_run > 64) {
1003 " %d coeffs left\n", zero_run, 64-coeff_index);
1004 zero_run = 64 - coeff_index;
1009 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
1014 "Invalid token %d\n", token);
1025 for (i = coeff_index+1; i < 64; i++)
1030 s->
dct_tokens[plane+1][coeff_index] = dct_tokens + j;
1031 else if (coeff_index < 63)
1032 s->
dct_tokens[0][coeff_index+1] = dct_tokens + j;
1040 int fragment_height);
1052 int residual_eob_run = 0;
1064 0, residual_eob_run);
1065 if (residual_eob_run < 0)
1066 return residual_eob_run;
1073 1, residual_eob_run);
1074 if (residual_eob_run < 0)
1075 return residual_eob_run;
1077 2, residual_eob_run);
1078 if (residual_eob_run < 0)
1079 return residual_eob_run;
1095 for (i = 1; i <= 5; i++) {
1096 y_tables[i] = &s->
ac_vlc_1[ac_y_table];
1097 c_tables[i] = &s->
ac_vlc_1[ac_c_table];
1099 for (i = 6; i <= 14; i++) {
1100 y_tables[i] = &s->
ac_vlc_2[ac_y_table];
1101 c_tables[i] = &s->
ac_vlc_2[ac_c_table];
1103 for (i = 15; i <= 27; i++) {
1104 y_tables[i] = &s->
ac_vlc_3[ac_y_table];
1105 c_tables[i] = &s->
ac_vlc_3[ac_c_table];
1107 for (i = 28; i <= 63; i++) {
1108 y_tables[i] = &s->
ac_vlc_4[ac_y_table];
1109 c_tables[i] = &s->
ac_vlc_4[ac_c_table];
1113 for (i = 1; i <= 63; i++) {
1114 residual_eob_run =
unpack_vlcs(s, gb, y_tables[i], i,
1115 0, residual_eob_run);
1116 if (residual_eob_run < 0)
1117 return residual_eob_run;
1119 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1120 1, residual_eob_run);
1121 if (residual_eob_run < 0)
1122 return residual_eob_run;
1123 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1124 2, residual_eob_run);
1125 if (residual_eob_run < 0)
1126 return residual_eob_run;
1137 #define COMPATIBLE_FRAME(x) \
1138 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1139 #define DC_COEFF(u) s->all_fragments[u].dc
1144 int fragment_height)
1153 int i = first_fragment;
1158 int vl, vul, vu, vur;
1170 static const int predictor_transform[16][4] = {
1195 static const unsigned char compatible_frame[9] = {
1206 int current_frame_type;
1213 vul = vu = vur = vl = 0;
1214 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1217 for (y = 0; y < fragment_height; y++) {
1220 for (x = 0; x < fragment_width; x++, i++) {
1225 current_frame_type =
1236 u= i-fragment_width;
1241 ul= i-fragment_width-1;
1246 if(x + 1 < fragment_width){
1247 ur= i-fragment_width+1;
1254 if (transform == 0) {
1258 predicted_dc = last_dc[current_frame_type];
1263 (predictor_transform[transform][0] * vul) +
1264 (predictor_transform[transform][1] * vu) +
1265 (predictor_transform[transform][2] * vur) +
1266 (predictor_transform[transform][3] * vl);
1268 predicted_dc /= 128;
1272 if ((transform == 15) || (transform == 13)) {
1273 if (
FFABS(predicted_dc - vu) > 128)
1275 else if (
FFABS(predicted_dc - vl) > 128)
1277 else if (
FFABS(predicted_dc - vul) > 128)
1285 last_dc[current_frame_type] =
DC_COEFF(i);
1304 for (y = ystart; y < yend; y++) {
1306 for (x = 0; x <
width; x++) {
1317 stride, bounding_values);
1324 stride, bounding_values);
1330 if ((x < width - 1) &&
1333 plane_data + 8*x + 8,
1334 stride, bounding_values);
1340 if ((y < height - 1) &&
1343 plane_data + 8*x + 8*stride,
1344 stride, bounding_values);
1359 int plane,
int inter, int16_t
block[64])
1361 int16_t *dequantizer = s->
qmat[frag->
qpi][inter][plane];
1367 switch (token & 3) {
1376 i += (token >> 2) & 0x7f;
1381 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1385 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1396 block[0] = frag->
dc * s->
qmat[0][inter][plane][0];
1447 int border = motion_y&1;
1455 ref_row = y + (motion_y>>1);
1456 ref_row =
FFMAX(
FFABS(ref_row), ref_row + 8 + border);
1467 int x,
y, i, j, fragment;
1469 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1470 int motion_halfpel_index;
1472 int plane, first_pixel;
1477 for (plane = 0; plane < 3; plane++) {
1484 int8_t (*motion_val)[2] = s->
motion_val[!!plane];
1500 for (; sb_y < slice_height; sb_y++) {
1503 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1506 for (j = 0; j < 16; j++) {
1508 y = 4*sb_y + hilbert_offset[j][1];
1509 fragment = y*fragment_width + x;
1511 i = fragment_start + fragment;
1514 if (x >= fragment_width || y >= fragment_height)
1517 first_pixel = 8*y*stride + 8*x;
1526 motion_source= golden_plane;
1528 motion_source= last_plane;
1530 motion_source += first_pixel;
1531 motion_halfpel_index = 0;
1538 motion_x = motion_val[fragment][0];
1539 motion_y = motion_val[fragment][1];
1541 src_x= (motion_x>>1) + 8*x;
1542 src_y= (motion_y>>1) + 8*y;
1544 motion_halfpel_index = motion_x & 0x01;
1545 motion_source += (motion_x >> 1);
1547 motion_halfpel_index |= (motion_y & 0x01) << 1;
1548 motion_source += ((motion_y >> 1) * stride);
1550 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
1552 if(stride<0) temp -= 8*
stride;
1554 s->
vdsp.
emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
1555 motion_source=
temp;
1567 if(motion_halfpel_index != 3){
1569 output_plane + first_pixel,
1570 motion_source,
stride, 8);
1572 int d= (motion_x ^ motion_y)>>31;
1574 output_plane + first_pixel,
1576 motion_source + stride + 1 + d,
1586 output_plane + first_pixel,
1592 output_plane + first_pixel,
1603 output_plane + first_pixel,
1604 last_plane + first_pixel,
1632 int y_fragment_count, c_fragment_count;
1663 int i, inter, plane;
1666 int y_fragment_count, c_fragment_count;
1688 for (i = 0; i < 3; i++)
1726 for (i = 0; i < 64; i++) {
1735 for(inter=0; inter<2; inter++){
1736 for(plane=0; plane<3; plane++){
1738 s->
qr_size [inter][plane][0]= 63;
1740 s->
qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
1745 for (i = 0; i < 16; i++) {
1774 for (i = 0; i < 16; i++) {
1823 for (i = 0; i < 3; i++) {
1862 int qps_changed = 0, i, err;
1864 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1866 if (!
s1->current_frame.data[0]
1877 int y_fragment_count, c_fragment_count;
1892 for (i = 0; i < 3; i++) {
1893 if (s->
qps[i] !=
s1->qps[1]) {
1895 memcpy(&s->
qmat[i], &
s1->qmat[i],
sizeof(s->
qmat[i]));
1899 if (s->
qps[0] !=
s1->qps[0])
1913 void *
data,
int *got_frame,
1917 int buf_size = avpkt->
size;
1925 #if CONFIG_THEORA_DECODER
1932 av_log(avctx,
AV_LOG_ERROR,
"midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
1944 }
else if (type == 2) {
1965 for (i = 0; i < 3; i++)
1971 }
while(s->theora >= 0x030200 && s->nqps<3 &&
get_bits1(&gb));
1972 for (i = s->nqps; i < 3; i++)
1977 s->keyframe?
"key":
"", avctx->frame_number+1, s->qps[0]);
1979 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
1982 if (s->qps[0] != s->last_qps[0])
1985 for (i = 0; i < s->nqps; i++)
1988 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
1994 s->current_frame.reference = 3;
1996 s->current_frame.key_frame = s->keyframe;
2002 if (!s->edge_emu_buffer)
2003 s->edge_emu_buffer =
av_malloc(9*
FFABS(s->current_frame.linesize[0]));
2013 if (avctx->frame_number == 0)
2017 if (s->version || s->theora)
2024 if (!s->golden_frame.data[0]) {
2027 s->golden_frame.reference = 3;
2033 s->last_frame = s->golden_frame;
2039 memset(s->all_fragments, 0, s->fragment_count *
sizeof(
Vp3Fragment));
2063 for (i = 0; i < 3; i++) {
2064 int height = s->height >> (i && s->chroma_y_shift);
2065 if (s->flipped_image)
2066 s->data_offset[i] = 0;
2068 s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
2071 s->last_slice_end = 0;
2072 for (i = 0; i < s->c_superblock_height; i++)
2076 for (i = 0; i < 3; i++) {
2077 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
2094 avctx->release_buffer(avctx, &s->current_frame);
2110 av_dlog(avctx,
"hti %d hbits %x token %d entry : %d size %d\n",
2151 #if CONFIG_THEORA_DECODER
2159 int visible_width, visible_height,
colorspace;
2160 int offset_x = 0, offset_y = 0;
2168 if (s->
theora < 0x030200)
2183 if (s->
theora >= 0x030200) {
2193 if (fps.
num && fps.
den) {
2195 fps.
den, fps.
num, 1<<30);
2200 if (aspect.
num && aspect.
den) {
2203 aspect.
num, aspect.
den, 1<<30);
2206 if (s->
theora < 0x030200)
2213 if (s->
theora >= 0x030200)
2226 if ( visible_width <= s->
width && visible_width > s->
width-16
2227 && visible_height <= s->height && visible_height > s->
height-16
2228 && !offset_x && (offset_y == s->
height - visible_height))
2233 if (colorspace == 1) {
2235 }
else if (colorspace == 2) {
2238 if (colorspace == 1 || colorspace == 2) {
2249 int i, n, matrices, inter, plane;
2251 if (s->
theora >= 0x030200) {
2255 for (i = 0; i < 64; i++)
2259 if (s->
theora >= 0x030200)
2264 for (i = 0; i < 64; i++)
2267 if (s->
theora >= 0x030200)
2272 for (i = 0; i < 64; i++)
2275 if (s->
theora >= 0x030200)
2285 for(n=0; n<matrices; n++){
2286 for (i = 0; i < 64; i++)
2290 for (inter = 0; inter <= 1; inter++) {
2291 for (plane = 0; plane <= 2; plane++) {
2293 if (inter || plane > 0)
2301 qtj= (3*inter + plane - 1) / 3;
2302 plj= (plane + 2) % 3;
2317 s->
qr_base[inter][plane][qri]= i;
2321 s->
qr_size[inter][plane][qri++]= i;
2335 for (s->
hti = 0; s->
hti < 80; s->
hti++) {
2373 42, header_start, header_len) < 0) {
2379 if (header_len[i] <= 0)
2385 if (!(ptype & 0x80))
2414 if (s->
theora < 0x030200)
2426 .
init = theora_decode_init,