48 #define FRAGMENT_PIXELS 8
57 #define SB_NOT_CODED 0
58 #define SB_PARTIALLY_CODED 1
59 #define SB_FULLY_CODED 2
64 #define MAXIMUM_LONG_BIT_RUN 4129
66 #define MODE_INTER_NO_MV 0
68 #define MODE_INTER_PLUS_MV 2
69 #define MODE_INTER_LAST_MV 3
70 #define MODE_INTER_PRIOR_LAST 4
71 #define MODE_USING_GOLDEN 5
72 #define MODE_GOLDEN_MV 6
73 #define MODE_INTER_FOURMV 7
74 #define CODING_MODE_COUNT 8
121 {0,0}, {1,0}, {1,1}, {0,1},
122 {0,2}, {0,3}, {1,3}, {1,2},
123 {2,2}, {2,3}, {3,3}, {3,2},
124 {3,1}, {2,1}, {2,0}, {3,0}
127 #define MIN_DEQUANT_VAL 2
205 #define TOKEN_EOB(eob_run) ((eob_run) << 2)
206 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
207 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
297 for (i = 0; i < 16; i++) {
323 int sb_x, sb_y, plane;
326 for (plane = 0; plane < 3; plane++) {
332 for (sb_y = 0; sb_y < sb_height; sb_y++)
333 for (sb_x = 0; sb_x < sb_width; sb_x++)
334 for (i = 0; i < 16; i++) {
336 y = 4*sb_y + hilbert_offset[i][1];
338 if (x < frag_width && y < frag_height)
356 int i, plane, inter, qri, bmi, bmj, qistart;
358 for(inter=0; inter<2; inter++){
359 for(plane=0; plane<3; plane++){
361 for(qri=0; qri<s->
qr_count[inter][plane]; qri++){
362 sum+= s->
qr_size[inter][plane][qri];
363 if(s->
qps[qpi] <= sum)
366 qistart= sum - s->
qr_size[inter][plane][qri];
367 bmi= s->
qr_base[inter][plane][qri ];
368 bmj= s->
qr_base[inter][plane][qri+1];
372 + s->
qr_size[inter][plane][qri])
373 / (2*s->
qr_size[inter][plane][qri]);
375 int qmin= 8<<(inter + !i);
376 int qscale= i ? ac_scale_factor : dc_scale_factor;
379 av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
382 s->
qmat[qpi][inter][plane][0] = s->
qmat[0][inter][plane][0];
401 assert(filter_limit < 128);
405 for (x = 0; x < filter_limit; x++) {
406 bounding_values[-x] = -x;
407 bounding_values[x] = x;
409 for (x = value = filter_limit; x < 128 && value; x++, value--) {
410 bounding_values[ x] = value;
411 bounding_values[-x] = -value;
414 bounding_values[128] = value;
415 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
426 int current_superblock = 0;
428 int num_partial_superblocks = 0;
431 int current_fragment;
443 while (current_superblock < s->superblock_count &&
get_bits_left(gb) > 0) {
451 if (current_run == 34)
461 current_superblock += current_run;
463 num_partial_superblocks += current_run;
468 if (num_partial_superblocks < s->superblock_count) {
469 int superblocks_decoded = 0;
471 current_superblock = 0;
475 while (superblocks_decoded < s->superblock_count - num_partial_superblocks
485 if (current_run == 34)
488 for (j = 0; j < current_run; current_superblock++) {
500 superblocks_decoded += current_run;
506 if (num_partial_superblocks) {
521 for (plane = 0; plane < 3; plane++) {
522 int sb_start = superblock_starts[plane];
524 int num_coded_frags = 0;
526 for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
529 for (j = 0; j < 16; j++) {
533 if (current_fragment != -1) {
540 if (current_run-- == 0) {
564 for (i = 0; i < 64; i++)
578 int i, j, k, sb_x, sb_y;
580 int current_macroblock;
581 int current_fragment;
598 for (i = 0; i < 8; i++)
600 for (i = 0; i < 8; i++)
601 custom_mode_alphabet[
get_bits(gb, 3)] = i;
602 alphabet = custom_mode_alphabet;
613 for (j = 0; j < 4; j++) {
614 int mb_x = 2*sb_x + (j>>1);
615 int mb_y = 2*sb_y + (((j>>1)+j)&1);
621 #define BLOCK_X (2*mb_x + (k&1))
622 #define BLOCK_Y (2*mb_y + (k>>1))
625 for (k = 0; k < 4; k++) {
639 coding_mode = alphabet
643 for (k = 0; k < 4; k++) {
649 #define SET_CHROMA_MODES \
650 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
651 frag[s->fragment_start[1]].coding_method = coding_mode;\
652 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
653 frag[s->fragment_start[2]].coding_method = coding_mode;
660 for (k = 0; k < 2; k++) {
665 for (k = 0; k < 4; k++) {
684 int j, k, sb_x, sb_y;
688 int last_motion_x = 0;
689 int last_motion_y = 0;
690 int prior_last_motion_x = 0;
691 int prior_last_motion_y = 0;
692 int current_macroblock;
693 int current_fragment;
709 for (j = 0; j < 4; j++) {
710 int mb_x = 2*sb_x + (j>>1);
711 int mb_y = 2*sb_y + (((j>>1)+j)&1);
723 if (coding_mode == 0) {
734 prior_last_motion_x = last_motion_x;
735 prior_last_motion_y = last_motion_y;
736 last_motion_x = motion_x[0];
737 last_motion_y = motion_y[0];
743 prior_last_motion_x = last_motion_x;
744 prior_last_motion_y = last_motion_y;
748 for (k = 0; k < 4; k++) {
751 if (coding_mode == 0) {
758 last_motion_x = motion_x[k];
759 last_motion_y = motion_y[k];
769 motion_x[0] = last_motion_x;
770 motion_y[0] = last_motion_y;
779 motion_x[0] = prior_last_motion_x;
780 motion_y[0] = prior_last_motion_y;
783 prior_last_motion_x = last_motion_x;
784 prior_last_motion_y = last_motion_y;
785 last_motion_x = motion_x[0];
786 last_motion_y = motion_y[0];
799 for (k = 0; k < 4; k++) {
803 s->
motion_val[0][current_fragment][0] = motion_x[k];
804 s->
motion_val[0][current_fragment][1] = motion_y[k];
806 s->
motion_val[0][current_fragment][0] = motion_x[0];
807 s->
motion_val[0][current_fragment][1] = motion_y[0];
813 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
814 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
816 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
817 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
823 motion_x[0] =
RSHIFT(motion_x[0] + motion_x[1], 1);
824 motion_y[0] =
RSHIFT(motion_y[0] + motion_y[1], 1);
825 motion_x[1] =
RSHIFT(motion_x[2] + motion_x[3], 1);
826 motion_y[1] =
RSHIFT(motion_y[2] + motion_y[3], 1);
828 motion_x[1] = motion_x[0];
829 motion_y[1] = motion_y[0];
831 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
832 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
835 for (k = 0; k < 2; k++) {
841 for (k = 0; k < 4; k++) {
861 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
864 for (qpi = 0; qpi < s->
nqps-1 && num_blocks > 0; qpi++) {
865 i = blocks_decoded = num_blocks_at_qpi = 0;
877 if (run_length == 34)
879 blocks_decoded += run_length;
882 num_blocks_at_qpi += run_length;
884 for (j = 0; j < run_length; i++) {
893 }
while (blocks_decoded < num_blocks &&
get_bits_left(gb) > 0);
895 num_blocks -= num_blocks_at_qpi;
914 VLC *table,
int coeff_index,
926 int16_t *dct_tokens = s->
dct_tokens[plane][coeff_index];
936 if (eob_run > num_coeffs) {
937 coeff_i = blocks_ended = num_coeffs;
938 eob_run -= num_coeffs;
940 coeff_i = blocks_ended = eob_run;
946 dct_tokens[j++] = blocks_ended << 2;
950 token =
get_vlc2(gb, vlc_table, 11, 3);
952 if ((
unsigned) token <= 6
U) {
959 if (eob_run > num_coeffs - coeff_i) {
960 dct_tokens[j++] =
TOKEN_EOB(num_coeffs - coeff_i);
961 blocks_ended += num_coeffs - coeff_i;
962 eob_run -= num_coeffs - coeff_i;
963 coeff_i = num_coeffs;
966 blocks_ended += eob_run;
970 }
else if (token >= 0) {
973 bits_to_get =
get_bits(gb, bits_to_get);
988 all_fragments[coded_fragment_list[coeff_i]].
dc = coeff;
993 if (coeff_index + zero_run > 64) {
995 " %d coeffs left\n", zero_run, 64-coeff_index);
996 zero_run = 64 - coeff_index;
1001 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
1006 "Invalid token %d\n", token);
1017 for (i = coeff_index+1; i < 64; i++)
1022 s->
dct_tokens[plane+1][coeff_index] = dct_tokens + j;
1023 else if (coeff_index < 63)
1024 s->
dct_tokens[0][coeff_index+1] = dct_tokens + j;
1032 int fragment_height);
1044 int residual_eob_run = 0;
1056 0, residual_eob_run);
1057 if (residual_eob_run < 0)
1058 return residual_eob_run;
1065 1, residual_eob_run);
1066 if (residual_eob_run < 0)
1067 return residual_eob_run;
1069 2, residual_eob_run);
1070 if (residual_eob_run < 0)
1071 return residual_eob_run;
1087 for (i = 1; i <= 5; i++) {
1088 y_tables[i] = &s->
ac_vlc_1[ac_y_table];
1089 c_tables[i] = &s->
ac_vlc_1[ac_c_table];
1091 for (i = 6; i <= 14; i++) {
1092 y_tables[i] = &s->
ac_vlc_2[ac_y_table];
1093 c_tables[i] = &s->
ac_vlc_2[ac_c_table];
1095 for (i = 15; i <= 27; i++) {
1096 y_tables[i] = &s->
ac_vlc_3[ac_y_table];
1097 c_tables[i] = &s->
ac_vlc_3[ac_c_table];
1099 for (i = 28; i <= 63; i++) {
1100 y_tables[i] = &s->
ac_vlc_4[ac_y_table];
1101 c_tables[i] = &s->
ac_vlc_4[ac_c_table];
1105 for (i = 1; i <= 63; i++) {
1106 residual_eob_run =
unpack_vlcs(s, gb, y_tables[i], i,
1107 0, residual_eob_run);
1108 if (residual_eob_run < 0)
1109 return residual_eob_run;
1111 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1112 1, residual_eob_run);
1113 if (residual_eob_run < 0)
1114 return residual_eob_run;
1115 residual_eob_run =
unpack_vlcs(s, gb, c_tables[i], i,
1116 2, residual_eob_run);
1117 if (residual_eob_run < 0)
1118 return residual_eob_run;
1129 #define COMPATIBLE_FRAME(x) \
1130 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
1131 #define DC_COEFF(u) s->all_fragments[u].dc
1136 int fragment_height)
1145 int i = first_fragment;
1150 int vl, vul, vu, vur;
1162 static const int predictor_transform[16][4] = {
1187 static const unsigned char compatible_frame[9] = {
1198 int current_frame_type;
1205 vul = vu = vur = vl = 0;
1206 last_dc[0] = last_dc[1] = last_dc[2] = 0;
1209 for (y = 0; y < fragment_height; y++) {
1212 for (x = 0; x < fragment_width; x++, i++) {
1217 current_frame_type =
1228 u= i-fragment_width;
1233 ul= i-fragment_width-1;
1238 if(x + 1 < fragment_width){
1239 ur= i-fragment_width+1;
1246 if (transform == 0) {
1250 predicted_dc = last_dc[current_frame_type];
1255 (predictor_transform[
transform][0] * vul) +
1256 (predictor_transform[transform][1] * vu) +
1257 (predictor_transform[
transform][2] * vur) +
1258 (predictor_transform[transform][3] * vl);
1260 predicted_dc /= 128;
1264 if ((transform == 15) || (transform == 13)) {
1265 if (
FFABS(predicted_dc - vu) > 128)
1267 else if (
FFABS(predicted_dc - vl) > 128)
1269 else if (
FFABS(predicted_dc - vul) > 128)
1277 last_dc[current_frame_type] =
DC_COEFF(i);
1296 for (y = ystart; y < yend; y++) {
1298 for (x = 0; x <
width; x++) {
1309 stride, bounding_values);
1316 stride, bounding_values);
1322 if ((x < width - 1) &&
1325 plane_data + 8*x + 8,
1326 stride, bounding_values);
1332 if ((y < height - 1) &&
1335 plane_data + 8*x + 8*stride,
1336 stride, bounding_values);
1351 int plane,
int inter, int16_t
block[64])
1353 int16_t *dequantizer = s->
qmat[frag->
qpi][inter][plane];
1359 switch (token & 3) {
1368 i += (token >> 2) & 0x7f;
1373 block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1377 block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
1388 block[0] = frag->
dc * s->
qmat[0][inter][plane][0];
1439 int border = motion_y&1;
1447 ref_row = y + (motion_y>>1);
1448 ref_row =
FFMAX(
FFABS(ref_row), ref_row + 8 + border);
1459 int x, y, i, j, fragment;
1461 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
1462 int motion_halfpel_index;
1464 int plane, first_pixel;
1469 for (plane = 0; plane < 3; plane++) {
1476 int8_t (*motion_val)[2] = s->
motion_val[!!plane];
1492 for (; sb_y < slice_height; sb_y++) {
1495 for (sb_x = 0; sb_x < slice_width; sb_x++) {
1498 for (j = 0; j < 16; j++) {
1500 y = 4*sb_y + hilbert_offset[j][1];
1501 fragment = y*fragment_width + x;
1503 i = fragment_start + fragment;
1506 if (x >= fragment_width || y >= fragment_height)
1509 first_pixel = 8*y*stride + 8*x;
1518 motion_source= golden_plane;
1520 motion_source= last_plane;
1522 motion_source += first_pixel;
1523 motion_halfpel_index = 0;
1530 motion_x = motion_val[fragment][0];
1531 motion_y = motion_val[fragment][1];
1533 src_x= (motion_x>>1) + 8*x;
1534 src_y= (motion_y>>1) + 8*y;
1536 motion_halfpel_index = motion_x & 0x01;
1537 motion_source += (motion_x >> 1);
1539 motion_halfpel_index |= (motion_y & 0x01) << 1;
1540 motion_source += ((motion_y >> 1) * stride);
1542 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
1544 if(stride<0) temp -= 8*
stride;
1551 motion_source= temp;
1563 if(motion_halfpel_index != 3){
1565 output_plane + first_pixel,
1566 motion_source,
stride, 8);
1568 int d= (motion_x ^ motion_y)>>31;
1570 output_plane + first_pixel,
1572 motion_source + stride + 1 + d,
1585 output_plane + first_pixel,
1594 output_plane + first_pixel,
1605 output_plane + first_pixel,
1606 last_plane + first_pixel,
1634 int y_fragment_count, c_fragment_count;
1681 int i, inter, plane, ret;
1684 int y_fragment_count, c_fragment_count;
1707 for (i = 0; i < 64; i++) {
1708 #define T(x) (x >> 3) | ((x & 7) << 3)
1716 for (i = 0; i < 3; i++)
1755 for (i = 0; i < 64; i++) {
1764 for(inter=0; inter<2; inter++){
1765 for(plane=0; plane<3; plane++){
1767 s->
qr_size [inter][plane][0]= 63;
1769 s->
qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
1774 for (i = 0; i < 16; i++) {
1803 for (i = 0; i < 16; i++) {
1885 if (src->
f->
data[0])
1903 int qps_changed = 0, i, err;
1905 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1907 if (!s1->current_frame.f->data[0]
1908 ||s->
width != s1->width
1909 ||s->
height!= s1->height) {
1918 int y_fragment_count, c_fragment_count;
1936 for (i = 0; i < 3; i++) {
1937 if (s->
qps[i] != s1->qps[1]) {
1939 memcpy(&s->
qmat[i], &s1->qmat[i],
sizeof(s->
qmat[i]));
1943 if (s->
qps[0] != s1->qps[0])
1955 void *
data,
int *got_frame,
1959 int buf_size = avpkt->
size;
1975 for (i = 0; i < 3; i++)
1981 }
while(s->theora >= 0x030200 && s->nqps<3 &&
get_bits1(&gb));
1982 for (i = s->nqps; i < 3; i++)
1987 s->keyframe?
"key":
"", avctx->frame_number+1, s->qps[0]);
1989 s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
1992 if (s->qps[0] != s->last_qps[0])
1995 for (i = 0; i < s->nqps; i++)
1998 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
2010 if (!s->edge_emu_buffer)
2011 s->edge_emu_buffer =
av_malloc(9*
FFABS(s->current_frame.f->linesize[0]));
2021 if (avctx->frame_number == 0)
2025 if (s->version || s->theora)
2032 if (!s->golden_frame.f->data[0]) {
2047 memset(s->all_fragments, 0, s->fragment_count *
sizeof(
Vp3Fragment));
2071 for (i = 0; i < 3; i++) {
2072 int height = s->height >> (i && s->chroma_y_shift);
2073 if (s->flipped_image)
2074 s->data_offset[i] = 0;
2076 s->data_offset[i] = (height-1) * s->current_frame.f->linesize[i];
2079 s->last_slice_end = 0;
2080 for (i = 0; i < s->c_superblock_height; i++)
2084 for (i = 0; i < 3; i++) {
2085 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
2122 av_dlog(avctx,
"hti %d hbits %x token %d entry : %d size %d\n",
2163 #if CONFIG_THEORA_DECODER
2171 int visible_width, visible_height,
colorspace;
2172 int offset_x = 0, offset_y = 0;
2181 if (s->
theora < 0x030200)
2190 if (s->
theora >= 0x030200) {
2200 if (fps.
num && fps.
den) {
2201 if (fps.
num < 0 || fps.
den < 0) {
2206 fps.
den, fps.
num, 1<<30);
2211 if (aspect.
num && aspect.
den) {
2214 aspect.
num, aspect.
den, 1<<30);
2217 if (s->
theora < 0x030200)
2224 if (s->
theora >= 0x030200)
2233 if ( visible_width <= s->
width && visible_width > s->
width-16
2234 && visible_height <= s->height && visible_height > s->
height-16
2235 && !offset_x && (offset_y == s->
height - visible_height))
2242 if (colorspace == 1) {
2244 }
else if (colorspace == 2) {
2247 if (colorspace == 1 || colorspace == 2) {
2258 int i, n, matrices, inter, plane;
2260 if (s->
theora >= 0x030200) {
2264 for (i = 0; i < 64; i++)
2268 if (s->
theora >= 0x030200)
2273 for (i = 0; i < 64; i++)
2276 if (s->
theora >= 0x030200)
2281 for (i = 0; i < 64; i++)
2284 if (s->
theora >= 0x030200)
2294 for(n=0; n<matrices; n++){
2295 for (i = 0; i < 64; i++)
2299 for (inter = 0; inter <= 1; inter++) {
2300 for (plane = 0; plane <= 2; plane++) {
2302 if (inter || plane > 0)
2310 qtj= (3*inter + plane - 1) / 3;
2311 plj= (plane + 2) % 3;
2326 s->
qr_base[inter][plane][qri]= i;
2330 s->
qr_size[inter][plane][qri++]= i;
2344 for (s->
hti = 0; s->
hti < 80; s->
hti++) {
2380 42, header_start, header_len) < 0) {
2386 if (header_len[i] <= 0)
2392 if (!(ptype & 0x80))
2404 theora_decode_header(avctx, &gb);
2411 if (theora_decode_tables(avctx, &gb))
2420 if (s->
theora < 0x030200)
2433 .
init = theora_decode_init,