comparison vp3.c @ 11475:b7744b7bee4b libavcodec

theora: Add support for 4:2:2 and 4:4:4 subsampling
author conrad
date Sat, 13 Mar 2010 05:59:22 +0000
parents 66b2c909d530
children 445659683743
comparison
equal deleted inserted replaced
11474:66b2c909d530 11475:b7744b7bee4b
128 typedef struct Vp3DecodeContext { 128 typedef struct Vp3DecodeContext {
129 AVCodecContext *avctx; 129 AVCodecContext *avctx;
130 int theora, theora_tables; 130 int theora, theora_tables;
131 int version; 131 int version;
132 int width, height; 132 int width, height;
133 int chroma_x_shift, chroma_y_shift;
133 AVFrame golden_frame; 134 AVFrame golden_frame;
134 AVFrame last_frame; 135 AVFrame last_frame;
135 AVFrame current_frame; 136 AVFrame current_frame;
136 int keyframe; 137 int keyframe;
137 DSPContext dsp; 138 DSPContext dsp;
514 int current_macroblock; 515 int current_macroblock;
515 int current_fragment; 516 int current_fragment;
516 int coding_mode; 517 int coding_mode;
517 int custom_mode_alphabet[CODING_MODE_COUNT]; 518 int custom_mode_alphabet[CODING_MODE_COUNT];
518 const int *alphabet; 519 const int *alphabet;
520 Vp3Fragment *frag;
519 521
520 if (s->keyframe) { 522 if (s->keyframe) {
521 for (i = 0; i < s->fragment_count; i++) 523 for (i = 0; i < s->fragment_count; i++)
522 s->all_fragments[i].coding_method = MODE_INTRA; 524 s->all_fragments[i].coding_method = MODE_INTRA;
523 525
570 coding_mode = alphabet 572 coding_mode = alphabet
571 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)]; 573 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
572 574
573 s->macroblock_coding[current_macroblock] = coding_mode; 575 s->macroblock_coding[current_macroblock] = coding_mode;
574 for (k = 0; k < 4; k++) { 576 for (k = 0; k < 4; k++) {
575 current_fragment = 577 frag = s->all_fragments + BLOCK_Y*s->fragment_width[0] + BLOCK_X;
576 BLOCK_Y*s->fragment_width[0] + BLOCK_X; 578 if (frag->coding_method != MODE_COPY)
577 if (s->all_fragments[current_fragment].coding_method != 579 frag->coding_method = coding_mode;
578 MODE_COPY) 580 }
579 s->all_fragments[current_fragment].coding_method = 581
580 coding_mode; 582 #define SET_CHROMA_MODES \
581 } 583 if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
582 for (k = 0; k < 2; k++) { 584 frag[s->fragment_start[1]].coding_method = coding_mode;\
583 current_fragment = s->fragment_start[k+1] + 585 if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
584 mb_y*s->fragment_width[1] + mb_x; 586 frag[s->fragment_start[2]].coding_method = coding_mode;
585 if (s->all_fragments[current_fragment].coding_method != 587
586 MODE_COPY) 588 if (s->chroma_y_shift) {
587 s->all_fragments[current_fragment].coding_method = 589 frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;
588 coding_mode; 590 SET_CHROMA_MODES
591 } else if (s->chroma_x_shift) {
592 frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;
593 for (k = 0; k < 2; k++) {
594 SET_CHROMA_MODES
595 frag += s->fragment_width[1];
596 }
597 } else {
598 for (k = 0; k < 4; k++) {
599 frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;
600 SET_CHROMA_MODES
601 }
589 } 602 }
590 } 603 }
591 } 604 }
592 } 605 }
593 } 606 }
609 int last_motion_y = 0; 622 int last_motion_y = 0;
610 int prior_last_motion_x = 0; 623 int prior_last_motion_x = 0;
611 int prior_last_motion_y = 0; 624 int prior_last_motion_y = 0;
612 int current_macroblock; 625 int current_macroblock;
613 int current_fragment; 626 int current_fragment;
627 Vp3Fragment *frag;
614 628
615 if (s->keyframe) 629 if (s->keyframe)
616 return 0; 630 return 0;
617 631
618 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */ 632 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
722 } else { 736 } else {
723 s->all_fragments[current_fragment].motion_x = motion_x[0]; 737 s->all_fragments[current_fragment].motion_x = motion_x[0];
724 s->all_fragments[current_fragment].motion_y = motion_y[0]; 738 s->all_fragments[current_fragment].motion_y = motion_y[0];
725 } 739 }
726 } 740 }
741
742 #define SET_CHROMA_MV(mx, my) \
743 frag[s->fragment_start[1]].motion_x = mx; \
744 frag[s->fragment_start[1]].motion_y = my; \
745 frag[s->fragment_start[2]].motion_x = mx; \
746 frag[s->fragment_start[2]].motion_y = my
747
748 if (s->chroma_y_shift) {
727 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { 749 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
728 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2); 750 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
729 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2); 751 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
730 } 752 }
731 for (k = 0; k < 2; k++) { 753 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
732 current_fragment = s->fragment_start[k+1] + 754 motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
733 mb_y*s->fragment_width[1] + mb_x; 755 frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;
734 s->all_fragments[current_fragment].motion_x = motion_x[0]; 756 SET_CHROMA_MV(motion_x[0], motion_y[0]);
735 s->all_fragments[current_fragment].motion_y = motion_y[0]; 757 } else if (s->chroma_x_shift) {
758 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
759 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
760 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
761 motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
762 motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
763 } else {
764 motion_x[1] = motion_x[0];
765 motion_y[1] = motion_y[0];
766 }
767 motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
768 motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
769
770 frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;
771 for (k = 0; k < 2; k++) {
772 SET_CHROMA_MV(motion_x[k], motion_y[k]);
773 frag += s->fragment_width[1];
774 }
775 } else {
776 for (k = 0; k < 4; k++) {
777 frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;
778 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
779 SET_CHROMA_MV(motion_x[k], motion_y[k]);
780 } else {
781 SET_CHROMA_MV(motion_x[0], motion_y[0]);
782 }
783 }
736 } 784 }
737 } 785 }
738 } 786 }
739 } 787 }
740 788
1302 for (plane = 0; plane < 3; plane++) { 1350 for (plane = 0; plane < 3; plane++) {
1303 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane]; 1351 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
1304 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane]; 1352 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
1305 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane]; 1353 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
1306 int stride = s->current_frame.linesize[plane]; 1354 int stride = s->current_frame.linesize[plane];
1307 int plane_width = s->width >> !!plane; 1355 int plane_width = s->width >> (plane && s->chroma_x_shift);
1308 int plane_height = s->height >> !!plane; 1356 int plane_height = s->height >> (plane && s->chroma_y_shift);
1309 1357
1310 int sb_x, sb_y = slice << !plane; 1358 int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
1311 int slice_height = sb_y + (plane ? 1 : 2); 1359 int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
1312 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width; 1360 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
1313 1361
1314 int fragment_width = s->fragment_width[!!plane]; 1362 int fragment_width = s->fragment_width[!!plane];
1315 int fragment_height = s->fragment_height[!!plane]; 1363 int fragment_height = s->fragment_height[!!plane];
1316 int fragment_start = s->fragment_start[plane]; 1364 int fragment_start = s->fragment_start[plane];
1360 if ((s->all_fragments[i].coding_method > MODE_INTRA) && 1408 if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
1361 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) { 1409 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
1362 int src_x, src_y; 1410 int src_x, src_y;
1363 motion_x = s->all_fragments[i].motion_x; 1411 motion_x = s->all_fragments[i].motion_x;
1364 motion_y = s->all_fragments[i].motion_y; 1412 motion_y = s->all_fragments[i].motion_y;
1365 if(plane){
1366 motion_x= (motion_x>>1) | (motion_x&1);
1367 motion_y= (motion_y>>1) | (motion_y&1);
1368 }
1369 1413
1370 src_x= (motion_x>>1) + 8*x; 1414 src_x= (motion_x>>1) + 8*x;
1371 src_y= (motion_y>>1) + 8*y; 1415 src_y= (motion_y>>1) + 8*y;
1372 1416
1373 motion_halfpel_index = motion_x & 0x01; 1417 motion_halfpel_index = motion_x & 0x01;
1461 { 1505 {
1462 Vp3DecodeContext *s = avctx->priv_data; 1506 Vp3DecodeContext *s = avctx->priv_data;
1463 int i, inter, plane; 1507 int i, inter, plane;
1464 int c_width; 1508 int c_width;
1465 int c_height; 1509 int c_height;
1510 int y_fragment_count, c_fragment_count;
1466 1511
1467 if (avctx->codec_tag == MKTAG('V','P','3','0')) 1512 if (avctx->codec_tag == MKTAG('V','P','3','0'))
1468 s->version = 0; 1513 s->version = 0;
1469 else 1514 else
1470 s->version = 1; 1515 s->version = 1;
1471 1516
1472 s->avctx = avctx; 1517 s->avctx = avctx;
1473 s->width = FFALIGN(avctx->width, 16); 1518 s->width = FFALIGN(avctx->width, 16);
1474 s->height = FFALIGN(avctx->height, 16); 1519 s->height = FFALIGN(avctx->height, 16);
1475 avctx->pix_fmt = PIX_FMT_YUV420P; 1520 if (avctx->pix_fmt == PIX_FMT_NONE)
1521 avctx->pix_fmt = PIX_FMT_YUV420P;
1476 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; 1522 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1477 if(avctx->idct_algo==FF_IDCT_AUTO) 1523 if(avctx->idct_algo==FF_IDCT_AUTO)
1478 avctx->idct_algo=FF_IDCT_VP3; 1524 avctx->idct_algo=FF_IDCT_VP3;
1479 dsputil_init(&s->dsp, avctx); 1525 dsputil_init(&s->dsp, avctx);
1480 1526
1483 /* initialize to an impossible value which will force a recalculation 1529 /* initialize to an impossible value which will force a recalculation
1484 * in the first frame decode */ 1530 * in the first frame decode */
1485 for (i = 0; i < 3; i++) 1531 for (i = 0; i < 3; i++)
1486 s->qps[i] = -1; 1532 s->qps[i] = -1;
1487 1533
1534 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
1535
1488 s->y_superblock_width = (s->width + 31) / 32; 1536 s->y_superblock_width = (s->width + 31) / 32;
1489 s->y_superblock_height = (s->height + 31) / 32; 1537 s->y_superblock_height = (s->height + 31) / 32;
1490 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height; 1538 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
1491 1539
1492 /* work out the dimensions for the C planes */ 1540 /* work out the dimensions for the C planes */
1493 c_width = s->width / 2; 1541 c_width = s->width >> s->chroma_x_shift;
1494 c_height = s->height / 2; 1542 c_height = s->height >> s->chroma_y_shift;
1495 s->c_superblock_width = (c_width + 31) / 32; 1543 s->c_superblock_width = (c_width + 31) / 32;
1496 s->c_superblock_height = (c_height + 31) / 32; 1544 s->c_superblock_height = (c_height + 31) / 32;
1497 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height; 1545 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
1498 1546
1499 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2); 1547 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
1505 s->macroblock_height = (s->height + 15) / 16; 1553 s->macroblock_height = (s->height + 15) / 16;
1506 s->macroblock_count = s->macroblock_width * s->macroblock_height; 1554 s->macroblock_count = s->macroblock_width * s->macroblock_height;
1507 1555
1508 s->fragment_width[0] = s->width / FRAGMENT_PIXELS; 1556 s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
1509 s->fragment_height[0] = s->height / FRAGMENT_PIXELS; 1557 s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
1510 s->fragment_width[1] = s->fragment_width[0] >> 1; 1558 s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
1511 s->fragment_height[1] = s->fragment_height[0] >> 1; 1559 s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
1512 1560
1513 /* fragment count covers all 8x8 blocks for all 3 planes */ 1561 /* fragment count covers all 8x8 blocks for all 3 planes */
1514 s->fragment_count = s->fragment_width[0] * s->fragment_height[0] * 3 / 2; 1562 y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1515 s->fragment_start[1] = s->fragment_width[0] * s->fragment_height[0]; 1563 c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1516 s->fragment_start[2] = s->fragment_width[0] * s->fragment_height[0] * 5 / 4; 1564 s->fragment_count = y_fragment_count + 2*c_fragment_count;
1565 s->fragment_start[1] = y_fragment_count;
1566 s->fragment_start[2] = y_fragment_count + c_fragment_count;
1517 1567
1518 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment)); 1568 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
1519 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int)); 1569 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
1520 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base)); 1570 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
1521 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base || 1571 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
1762 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); 1812 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
1763 goto error; 1813 goto error;
1764 } 1814 }
1765 1815
1766 for (i = 0; i < 3; i++) { 1816 for (i = 0; i < 3; i++) {
1817 int height = s->height >> (i && s->chroma_y_shift);
1767 if (s->flipped_image) 1818 if (s->flipped_image)
1768 s->data_offset[i] = 0; 1819 s->data_offset[i] = 0;
1769 else 1820 else
1770 s->data_offset[i] = ((s->height>>!!i)-1) * s->current_frame.linesize[i]; 1821 s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
1771 } 1822 }
1772 1823
1773 s->last_slice_end = 0; 1824 s->last_slice_end = 0;
1774 for (i = 0; i < s->c_superblock_height; i++) 1825 for (i = 0; i < s->c_superblock_height; i++)
1775 render_slice(s, i); 1826 render_slice(s, i);
1776 1827
1777 // filter the last row 1828 // filter the last row
1778 for (i = 0; i < 3; i++) { 1829 for (i = 0; i < 3; i++) {
1779 int row = (s->height >> (3+!!i)) - 1; 1830 int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
1780 apply_loop_filter(s, i, row, row+1); 1831 apply_loop_filter(s, i, row, row+1);
1781 } 1832 }
1782 vp3_draw_horiz_band(s, s->height); 1833 vp3_draw_horiz_band(s, s->height);
1783 1834
1784 *data_size=sizeof(AVFrame); 1835 *data_size=sizeof(AVFrame);
1881 } 1932 }
1882 return 0; 1933 return 0;
1883 } 1934 }
1884 1935
1885 #if CONFIG_THEORA_DECODER 1936 #if CONFIG_THEORA_DECODER
1937 static const enum PixelFormat theora_pix_fmts[4] = {
1938 PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P
1939 };
1940
1886 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) 1941 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
1887 { 1942 {
1888 Vp3DecodeContext *s = avctx->priv_data; 1943 Vp3DecodeContext *s = avctx->priv_data;
1889 int visible_width, visible_height, colorspace; 1944 int visible_width, visible_height, colorspace;
1890 1945
1929 skip_bits(gb, 6); /* quality hint */ 1984 skip_bits(gb, 6); /* quality hint */
1930 1985
1931 if (s->theora >= 0x030200) 1986 if (s->theora >= 0x030200)
1932 { 1987 {
1933 skip_bits(gb, 5); /* keyframe frequency force */ 1988 skip_bits(gb, 5); /* keyframe frequency force */
1934 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */ 1989 avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
1935 skip_bits(gb, 3); /* reserved */ 1990 skip_bits(gb, 3); /* reserved */
1936 } 1991 }
1937 1992
1938 // align_get_bits(gb); 1993 // align_get_bits(gb);
1939 1994