46 #define FRAGMENT_PIXELS 8
55 #define SB_NOT_CODED 0
56 #define SB_PARTIALLY_CODED 1
57 #define SB_FULLY_CODED 2
62 #define MAXIMUM_LONG_BIT_RUN 4129
64 #define MODE_INTER_NO_MV 0
66 #define MODE_INTER_PLUS_MV 2
67 #define MODE_INTER_LAST_MV 3
68 #define MODE_INTER_PRIOR_LAST 4
69 #define MODE_USING_GOLDEN 5
70 #define MODE_GOLDEN_MV 6
71 #define MODE_INTER_FOURMV 7
72 #define CODING_MODE_COUNT 8
119 {0,0}, {1,0}, {1,1}, {0,1},
120 {0,2}, {0,3}, {1,3}, {1,2},
121 {2,2}, {2,3}, {3,3}, {3,2},
122 {3,1}, {2,1}, {2,0}, {3,0}
125 #define MIN_DEQUANT_VAL 2
200 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
201 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
202 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
294 for (i = 0; i < 16; i++) {
322 int sb_x, sb_y, plane;
325 for (plane = 0; plane < 3; plane++) {
331 for (sb_y = 0; sb_y < sb_height; sb_y++)
332 for (sb_x = 0; sb_x < sb_width; sb_x++)
333 for (i = 0; i < 16; i++) {
335 y = 4*sb_y + hilbert_offset[i][1];
337 if (x < frag_width && y < frag_height)
355 int i, plane, inter, qri, bmi, bmj, qistart;
357 for(inter=0; inter<2; inter++){
358 for(plane=0; plane<3; plane++){
360 for(qri=0; qri<s->
qr_count[inter][plane]; qri++){
361 sum+= s->
qr_size[inter][plane][qri];
362 if(s->
qps[qpi] <= sum)
365 qistart= sum - s->
qr_size[inter][plane][qri];
366 bmi= s->
qr_base[inter][plane][qri ];
367 bmj= s->
qr_base[inter][plane][qri+1];
371 + s->
qr_size[inter][plane][qri])
372 / (2*s->
qr_size[inter][plane][qri]);
374 int qmin= 8<<(inter + !i);
375 int qscale= i ? ac_scale_factor : dc_scale_factor;
380 s->
qmat[qpi][inter][plane][0] = s->
qmat[0][inter][plane][0];
402 for (x = 0; x < filter_limit; x++) {
403 bounding_values[-x] = -x;
404 bounding_values[x] = x;
406 for (x = value = filter_limit; x < 128 && value; x++, value--) {
407 bounding_values[ x] = value;
408 bounding_values[-x] = -value;
411 bounding_values[128] = value;
412 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
423 int current_superblock = 0;
425 int num_partial_superblocks = 0;
428 int current_fragment;
440 while (current_superblock < s->superblock_count &&
get_bits_left(gb) > 0) {
448 if (current_run == 34)
458 current_superblock += current_run;
460 num_partial_superblocks += current_run;
465 if (num_partial_superblocks < s->superblock_count) {
466 int superblocks_decoded = 0;
468 current_superblock = 0;
472 while (superblocks_decoded < s->superblock_count - num_partial_superblocks
482 if (current_run == 34)
485 for (j = 0; j < current_run; current_superblock++) {
497 superblocks_decoded += current_run;
503 if (num_partial_superblocks) {
518 for (plane = 0; plane < 3; plane++) {
519 int sb_start = superblock_starts[plane];
521 int num_coded_frags = 0;
523 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
526 for (j = 0; j < 16; j++) {
530 if (current_fragment != -1) {
537 if (current_run-- == 0) {
561 for (i = 0; i < 64; i++)
575 int i, j, k, sb_x, sb_y;
577 int current_macroblock;
578 int current_fragment;
595 for (i = 0; i < 8; i++)
597 for (i = 0; i < 8; i++)
598 custom_mode_alphabet[
get_bits(gb, 3)] = i;
599 alphabet = custom_mode_alphabet;
610 for (j = 0; j < 4; j++) {
611 int mb_x = 2*sb_x + (j>>1);
612 int mb_y = 2*sb_y + (((j>>1)+j)&1);
618 #define BLOCK_X (2*mb_x + (k&1))
619 #define BLOCK_Y (2*mb_y + (k>>1))
622 for (k = 0; k < 4; k++) {
636 coding_mode = alphabet
640 for (k = 0; k < 4; k++) {
646 #define SET_CHROMA_MODES \
647 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
648 frag[s->fragment_start[1]].coding_method = coding_mode;\
649 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
650 frag[s->fragment_start[2]].coding_method = coding_mode;
657 for (k = 0; k < 2; k++) {
662 for (k = 0; k < 4; k++) {
681 int j, k, sb_x, sb_y;
685 int last_motion_x = 0;
686 int last_motion_y = 0;
687 int prior_last_motion_x = 0;
688 int prior_last_motion_y = 0;
689 int current_macroblock;
690 int current_fragment;
706 for (j = 0; j < 4; j++) {
707 int mb_x = 2*sb_x + (j>>1);
708 int mb_y = 2*sb_y + (((j>>1)+j)&1);
720 if (coding_mode == 0) {
731 prior_last_motion_x = last_motion_x;
732 prior_last_motion_y = last_motion_y;
733 last_motion_x = motion_x[0];
734 last_motion_y = motion_y[0];
740 prior_last_motion_x = last_motion_x;
741 prior_last_motion_y = last_motion_y;
745 for (k = 0; k < 4; k++) {
748 if (coding_mode == 0) {
755 last_motion_x = motion_x[k];
756 last_motion_y = motion_y[k];
766 motion_x[0] = last_motion_x;
767 motion_y[0] = last_motion_y;
776 motion_x[0] = prior_last_motion_x;
777 motion_y[0] = prior_last_motion_y;
780 prior_last_motion_x = last_motion_x;
781 prior_last_motion_y = last_motion_y;
782 last_motion_x = motion_x[0];
783 last_motion_y = motion_y[0];
796 for (k = 0; k < 4; k++) {
800 s->
motion_val[0][current_fragment][0] = motion_x[k];
801 s->
motion_val[0][current_fragment][1] = motion_y[k];
803 s->
motion_val[0][current_fragment][0] = motion_x[0];
804 s->
motion_val[0][current_fragment][1] = motion_y[0];
810 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
811 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
813 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
814 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
820 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1], 1);
821 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1], 1);
822 motion_x[1] =
RSHIFT(motion_x[2] + motion_x[3], 1);
823 motion_y[1] =
RSHIFT(motion_y[2] + motion_y[3], 1);
825 motion_x[1] = motion_x[0];
826 motion_y[1] = motion_y[0];
828 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
829 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
832 for (k = 0; k < 2; k++) {
838 for (k = 0; k < 4; k++) {
858 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
861 for (qpi = 0; qpi < s->
nqps-1 && num_blocks > 0; qpi++) {
862 i = blocks_decoded = num_blocks_at_qpi = 0;
874 if (run_length == 34)
876 blocks_decoded += run_length;
879 num_blocks_at_qpi += run_length;
881 for (j = 0; j < run_length; i++) {
890 }
while (blocks_decoded < num_blocks &&
get_bits_left(gb) > 0);
892 num_blocks -= num_blocks_at_qpi;
911 VLC *table,
int coeff_index,
923 int16_t *dct_tokens = s->
dct_tokens[plane][coeff_index];
933 if (eob_run > num_coeffs) {
934 coeff_i = blocks_ended = num_coeffs;
935 eob_run -= num_coeffs;
937 coeff_i = blocks_ended = eob_run;
943 dct_tokens[j++] = blocks_ended << 2;
947 token =
get_vlc2(gb, vlc_table, 11, 3);
949 if ((
unsigned) token <= 6
U) {
956 if (eob_run > num_coeffs - coeff_i) {
957 dct_tokens[j++] =
TOKEN_EOB(num_coeffs - coeff_i);
958 blocks_ended += num_coeffs - coeff_i;
959 eob_run -= num_coeffs - coeff_i;
960 coeff_i = num_coeffs;
963 blocks_ended += eob_run;
967 }
else if (token >= 0) {
970 bits_to_get =
get_bits(gb, bits_to_get);
985 all_fragments[coded_fragment_list[coeff_i]].
dc = coeff;
990 if (coeff_index + zero_run > 64) {
992 " %d coeffs left\n", zero_run, 64-coeff_index);
993 zero_run = 64 - coeff_index;
998 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
1003 "Invalid token %d\n", token);
1014 for (i = coeff_index+1; i < 64; i++)
1019 s->
dct_tokens[plane+1][coeff_index] = dct_tokens + j;
1020 else if (coeff_index < 63)
1021 s->
dct_tokens[0][coeff_index+1] = dct_tokens + j;
1029 int fragment_height);
1041 int residual_eob_run = 0;
1053 0, residual_eob_run);
1054 if (residual_eob_run < 0)
1055 return residual_eob_run;
1062 1, residual_eob_run);
1063 if (residual_eob_run < 0)
1064 return residual_eob_run;
1066 2, residual_eob_run);
1067 if (residual_eob_run < 0)
1068 return residual_eob_run;
1084 for (i = 1; i <= 5; i++) {
1085 y_tables[i] = &s->
ac_vlc_1[ac_y_table];
1086 c_tables[i] = &s->
ac_vlc_1[ac_c_table];
1088 for (i = 6; i <= 14; i++) {
1089 y_tables[i] = &s->
ac_vlc_2[ac_y_table];
1090 c_tables[i] = &s->
ac_vlc_2[ac_c_table];
1092 for (i = 15; i <= 27; i++) {
1093 y_tables[i] = &s->
ac_vlc_3[ac_y_table];
1094 c_tables[i] = &s->
ac_vlc_3[ac_c_table];
1096 for (i = 28; i <= 63; i++) {
1097 y_tables[i] = &s->
ac_vlc_4[ac_y_table];
1098 c_tables[i] = &s->
ac_vlc_4[ac_c_table];
1102 for (i = 1; i <= 63; i++) {
1103 residual_eob_run =
unpack_vlcs(s, gb, y_tables[i], i,
1104 0, residual_eob_run);
1105 if (residual_eob_run < 0)
1106 return residual_eob_run;
1108 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1109 1, residual_eob_run);
1110 if (residual_eob_run < 0)
1111 return residual_eob_run;
1112 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1113 2, residual_eob_run);
1114 if (residual_eob_run < 0)
1115 return residual_eob_run;
1126 #define COMPATIBLE_FRAME(x) \
1127 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1128 #define DC_COEFF(u) s->all_fragments[u].dc
1133 int fragment_height)
1142 int i = first_fragment;
1147 int vl, vul, vu, vur;
1159 static const int predictor_transform[16][4] = {
1184 static const unsigned char compatible_frame[9] = {
1195 int current_frame_type;
1202 vul = vu = vur = vl = 0;
1203 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1206 for (y = 0; y < fragment_height; y++) {
1209 for (x = 0; x < fragment_width; x++, i++) {
1214 current_frame_type =
1225 u= i-fragment_width;
1230 ul= i-fragment_width-1;
1235 if(x + 1 < fragment_width){
1236 ur= i-fragment_width+1;
1243 if (transform == 0) {
1247 predicted_dc = last_dc[current_frame_type];
1252 (predictor_transform[transform][0] * vul) +
1253 (predictor_transform[transform][1] * vu) +
1254 (predictor_transform[transform][2] * vur) +
1255 (predictor_transform[transform][3] * vl);
1257 predicted_dc /= 128;
1261 if ((transform == 15) || (transform == 13)) {
1262 if (
FFABS(predicted_dc - vu) > 128)
1264 else if (
FFABS(predicted_dc - vl) > 128)
1266 else if (
FFABS(predicted_dc - vul) > 128)
1274 last_dc[current_frame_type] =
DC_COEFF(i);
1293 for (y = ystart; y < yend; y++) {
1295 for (x = 0; x <
width; x++) {
1306 stride, bounding_values);
1313 stride, bounding_values);
1319 if ((x < width - 1) &&
1322 plane_data + 8*x + 8,
1323 stride, bounding_values);
1329 if ((y < height - 1) &&
1332 plane_data + 8*x + 8*stride,
1333 stride, bounding_values);
1350 int16_t *dequantizer = s->
qmat[frag->
qpi][inter][plane];
1356 switch (token & 3) {
1365 i += (token >> 2) & 0x7f;
1370 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1374 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1385 block[0] = frag->
dc * s->
qmat[0][inter][plane][0];
1436 int border = motion_y&1;
1444 ref_row = y + (motion_y>>1);
1445 ref_row =
FFMAX(
FFABS(ref_row), ref_row + 8 + border);
1456 int x, y, i, j, fragment;
1458 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1459 int motion_halfpel_index;
1460 uint8_t *motion_source;
1461 int plane, first_pixel;
1466 for (plane = 0; plane < 3; plane++) {
1468 uint8_t * last_plane = s-> last_frame.data [plane] + s->
data_offset[plane];
1469 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->
data_offset[plane];
1473 int8_t (*motion_val)[2] = s->
motion_val[!!plane];
1489 for (; sb_y < slice_height; sb_y++) {
1492 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1495 for (j = 0; j < 16; j++) {
1497 y = 4*sb_y + hilbert_offset[j][1];
1498 fragment = y*fragment_width + x;
1500 i = fragment_start + fragment;
1503 if (x >= fragment_width || y >= fragment_height)
1506 first_pixel = 8*y*stride + 8*x;
1515 motion_source= golden_plane;
1517 motion_source= last_plane;
1519 motion_source += first_pixel;
1520 motion_halfpel_index = 0;
1527 motion_x = motion_val[fragment][0];
1528 motion_y = motion_val[fragment][1];
1530 src_x= (motion_x>>1) + 8*x;
1531 src_y= (motion_y>>1) + 8*y;
1533 motion_halfpel_index = motion_x & 0x01;
1534 motion_source += (motion_x >> 1);
1536 motion_halfpel_index |= (motion_y & 0x01) << 1;
1537 motion_source += ((motion_y >> 1) * stride);
1539 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
1541 if(stride<0) temp -= 8*
stride;
1543 s->
dsp.
emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
1544 motion_source= temp;
1556 if(motion_halfpel_index != 3){
1558 output_plane + first_pixel,
1559 motion_source,
stride, 8);
1561 int d= (motion_x ^ motion_y)>>31;
1563 output_plane + first_pixel,
1565 motion_source + stride + 1 + d,
1582 output_plane + first_pixel,
1591 output_plane + first_pixel,
1602 output_plane + first_pixel,
1603 last_plane + first_pixel,
1631 int y_fragment_count, c_fragment_count;
1662 int i, inter, plane;
1665 int y_fragment_count, c_fragment_count;
1686 for (i = 0; i < 3; i++)
1724 for (i = 0; i < 64; i++) {
1733 for(inter=0; inter<2; inter++){
1734 for(plane=0; plane<3; plane++){
1736 s->
qr_size [inter][plane][0]= 63;
1738 s->
qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
1743 for (i = 0; i < 16; i++) {
1772 for (i = 0; i < 16; i++) {
1821 for (i = 0; i < 3; i++) {
1860 int qps_changed = 0, i, err;
1862 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1864 if (!
s1->current_frame.data[0]
1875 int y_fragment_count, c_fragment_count;
1890 for (i = 0; i < 3; i++) {
1891 if (s->
qps[i] !=
s1->qps[1]) {
1893 memcpy(&s->
qmat[i], &
s1->qmat[i],
sizeof(s->
qmat[i]));
1897 if (s->
qps[0] !=
s1->qps[0])
1911 void *
data,
int *data_size,
1914 const uint8_t *buf = avpkt->
data;
1915 int buf_size = avpkt->
size;
1931 for (i = 0; i < 3; i++)
1937 }
while(s->theora >= 0x030200 && s->nqps<3 &&
get_bits1(&gb));
1938 for (i = s->nqps; i < 3; i++)
1943 s->keyframe?
"key":
"", avctx->frame_number+1, s->qps[0]);
1945 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
1948 if (s->qps[0] != s->last_qps[0])
1951 for (i = 0; i < s->nqps; i++)
1954 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
1960 s->current_frame.reference = 3;
1967 if (!s->edge_emu_buffer)
1968 s->edge_emu_buffer =
av_malloc(9*
FFABS(s->current_frame.linesize[0]));
1978 if (avctx->frame_number == 0)
1982 if (s->version || s->theora)
1989 if (!s->golden_frame.data[0]) {
1992 s->golden_frame.reference = 3;
1998 s->last_frame = s->golden_frame;
2004 memset(s->all_fragments, 0, s->fragment_count *
sizeof(
Vp3Fragment));
2028 for (i = 0; i < 3; i++) {
2029 int height = s->height >> (i && s->chroma_y_shift);
2030 if (s->flipped_image)
2031 s->data_offset[i] = 0;
2033 s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
2036 s->last_slice_end = 0;
2037 for (i = 0; i < s->c_superblock_height; i++)
2041 for (i = 0; i < 3; i++) {
2042 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
2059 avctx->release_buffer(avctx, &s->current_frame);
2115 #if CONFIG_THEORA_DECODER
2116 static const enum PixelFormat theora_pix_fmts[4] = {
2123 int visible_width, visible_height,
colorspace;
2124 int offset_x = 0, offset_y = 0;
2132 if (s->
theora < 0x030200)
2147 if (s->
theora >= 0x030200) {
2157 if (fps.
num && fps.
den) {
2158 if (fps.
num < 0 || fps.
den < 0) {
2163 fps.
den, fps.
num, 1<<30);
2168 if (aspect.
num && aspect.
den) {
2171 aspect.
num, aspect.
den, 1<<30);
2174 if (s->
theora < 0x030200)
2181 if (s->
theora >= 0x030200)
2190 if ( visible_width <= s->
width && visible_width > s->
width-16
2191 && visible_height <= s->height && visible_height > s->
height-16
2192 && !offset_x && (offset_y == s->
height - visible_height))
2197 if (colorspace == 1) {
2199 }
else if (colorspace == 2) {
2202 if (colorspace == 1 || colorspace == 2) {
2213 int i, n, matrices, inter, plane;
2215 if (s->
theora >= 0x030200) {
2219 for (i = 0; i < 64; i++)
2223 if (s->
theora >= 0x030200)
2228 for (i = 0; i < 64; i++)
2231 if (s->
theora >= 0x030200)
2236 for (i = 0; i < 64; i++)
2239 if (s->
theora >= 0x030200)
2249 for(n=0; n<matrices; n++){
2250 for (i = 0; i < 64; i++)
2254 for (inter = 0; inter <= 1; inter++) {
2255 for (plane = 0; plane <= 2; plane++) {
2257 if (inter || plane > 0)
2265 qtj= (3*inter + plane - 1) / 3;
2266 plj= (plane + 2) % 3;
2276 i=
get_bits(gb, av_log2(matrices-1)+1);
2281 s->
qr_base[inter][plane][qri]= i;
2284 i =
get_bits(gb, av_log2(63-qi)+1) + 1;
2285 s->
qr_size[inter][plane][qri++]= i;
2299 for (s->
hti = 0; s->
hti < 80; s->
hti++) {
2322 uint8_t *header_start[3];
2335 42, header_start, header_len) < 0) {
2345 if (!(ptype & 0x80))
2357 theora_decode_header(avctx, &gb);
2364 if (theora_decode_tables(avctx, &gb))
2373 if (s->
theora < 0x030200)
2385 .
init = theora_decode_init,