comparison h263.c @ 1655:c92147a61d97 libavcodec

rv20 (h263) b frame decoding support
author michael
date Thu, 04 Dec 2003 18:34:47 +0000
parents 834922115010
children 34b0e799aeb6
comparison
equal deleted inserted replaced
1654:1c123e036890 1655:c92147a61d97
47 #define MV_VLC_BITS 9 47 #define MV_VLC_BITS 9
48 #define DC_VLC_BITS 9 48 #define DC_VLC_BITS 9
49 #define SPRITE_TRAJ_VLC_BITS 6 49 #define SPRITE_TRAJ_VLC_BITS 6
50 #define MB_TYPE_B_VLC_BITS 4 50 #define MB_TYPE_B_VLC_BITS 4
51 #define TEX_VLC_BITS 9 51 #define TEX_VLC_BITS 9
52 #define H263_MBTYPE_B_VLC_BITS 6
53 #define CBPC_B_VLC_BITS 3
52 54
53 #ifdef CONFIG_ENCODERS 55 #ifdef CONFIG_ENCODERS
54 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, 56 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
55 int n); 57 int n);
56 static void h263_encode_motion(MpegEncContext * s, int val, int fcode); 58 static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
1090 1092
1091 /* motion vectors: 16x16 mode */ 1093 /* motion vectors: 16x16 mode */
1092 h263_pred_motion(s, 0, &pred_x, &pred_y); 1094 h263_pred_motion(s, 0, &pred_x, &pred_y);
1093 1095
1094 if (!s->umvplus) { 1096 if (!s->umvplus) {
1095 h263_encode_motion(s, motion_x - pred_x, s->f_code); 1097 h263_encode_motion(s, motion_x - pred_x, 1);
1096 h263_encode_motion(s, motion_y - pred_y, s->f_code); 1098 h263_encode_motion(s, motion_y - pred_y, 1);
1097 } 1099 }
1098 else { 1100 else {
1099 h263p_encode_umotion(s, motion_x - pred_x); 1101 h263p_encode_umotion(s, motion_x - pred_x);
1100 h263p_encode_umotion(s, motion_y - pred_y); 1102 h263p_encode_umotion(s, motion_y - pred_y);
1101 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) 1103 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1119 h263_pred_motion(s, i, &pred_x, &pred_y); 1121 h263_pred_motion(s, i, &pred_x, &pred_y);
1120 1122
1121 motion_x= s->motion_val[ s->block_index[i] ][0]; 1123 motion_x= s->motion_val[ s->block_index[i] ][0];
1122 motion_y= s->motion_val[ s->block_index[i] ][1]; 1124 motion_y= s->motion_val[ s->block_index[i] ][1];
1123 if (!s->umvplus) { 1125 if (!s->umvplus) {
1124 h263_encode_motion(s, motion_x - pred_x, s->f_code); 1126 h263_encode_motion(s, motion_x - pred_x, 1);
1125 h263_encode_motion(s, motion_y - pred_y, s->f_code); 1127 h263_encode_motion(s, motion_y - pred_y, 1);
1126 } 1128 }
1127 else { 1129 else {
1128 h263p_encode_umotion(s, motion_x - pred_x); 1130 h263p_encode_umotion(s, motion_x - pred_x);
1129 h263p_encode_umotion(s, motion_y - pred_y); 1131 h263p_encode_umotion(s, motion_y - pred_y);
1130 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) 1132 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1509 C = s->motion_val[xy + off[block] - wrap]; 1511 C = s->motion_val[xy + off[block] - wrap];
1510 *px = mid_pred(A[0], B[0], C[0]); 1512 *px = mid_pred(A[0], B[0], C[0]);
1511 *py = mid_pred(A[1], B[1], C[1]); 1513 *py = mid_pred(A[1], B[1], C[1]);
1512 } 1514 }
1513 return mot_val; 1515 return mot_val;
1516 }
1517
1518 // identical to above but with s->current_picture->motion_val, the above one will be removed, and this renamed to it
1519 int16_t *h263_pred_motion2(MpegEncContext * s, int block, int dir,
1520 int *px, int *py)
1521 {
1522 int xy, wrap;
1523 int16_t *A, *B, *C, (*mot_val)[2];
1524 static const int off[4]= {2, 1, 1, -1};
1525
1526 wrap = s->b8_stride;
1527 xy = s->mb_x + s->mb_y * wrap;
1528
1529 mot_val = s->current_picture.motion_val[dir] + xy;
1530
1531 A = mot_val[ - 1];
1532 /* special case for first (slice) line */
1533 if (s->first_slice_line && block<3) {
1534 // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
1535 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
1536 if(block==0){ //most common case
1537 if(s->mb_x == s->resync_mb_x){ //rare
1538 *px= *py = 0;
1539 }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1540 C = mot_val[off[block] - wrap];
1541 if(s->mb_x==0){
1542 *px = C[0];
1543 *py = C[1];
1544 }else{
1545 *px = mid_pred(A[0], 0, C[0]);
1546 *py = mid_pred(A[1], 0, C[1]);
1547 }
1548 }else{
1549 *px = A[0];
1550 *py = A[1];
1551 }
1552 }else if(block==1){
1553 if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1554 C = mot_val[off[block] - wrap];
1555 *px = mid_pred(A[0], 0, C[0]);
1556 *py = mid_pred(A[1], 0, C[1]);
1557 }else{
1558 *px = A[0];
1559 *py = A[1];
1560 }
1561 }else{ /* block==2*/
1562 B = mot_val[ - wrap];
1563 C = mot_val[off[block] - wrap];
1564 if(s->mb_x == s->resync_mb_x) //rare
1565 A[0]=A[1]=0;
1566
1567 *px = mid_pred(A[0], B[0], C[0]);
1568 *py = mid_pred(A[1], B[1], C[1]);
1569 }
1570 } else {
1571 B = mot_val[ - wrap];
1572 C = mot_val[off[block] - wrap];
1573 *px = mid_pred(A[0], B[0], C[0]);
1574 *py = mid_pred(A[1], B[1], C[1]);
1575 }
1576 return *mot_val;
1514 } 1577 }
1515 1578
1516 #ifdef CONFIG_ENCODERS 1579 #ifdef CONFIG_ENCODERS
1517 static void h263_encode_motion(MpegEncContext * s, int val, int f_code) 1580 static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
1518 { 1581 {
2604 static VLC cbpy_vlc; 2667 static VLC cbpy_vlc;
2605 static VLC mv_vlc; 2668 static VLC mv_vlc;
2606 static VLC dc_lum, dc_chrom; 2669 static VLC dc_lum, dc_chrom;
2607 static VLC sprite_trajectory; 2670 static VLC sprite_trajectory;
2608 static VLC mb_type_b_vlc; 2671 static VLC mb_type_b_vlc;
2672 static VLC h263_mbtype_b_vlc;
2673 static VLC cbpc_b_vlc;
2609 2674
2610 void init_vlc_rl(RLTable *rl) 2675 void init_vlc_rl(RLTable *rl)
2611 { 2676 {
2612 int i, q; 2677 int i, q;
2613 2678
2696 &sprite_trajectory_tab[0][1], 4, 2, 2761 &sprite_trajectory_tab[0][1], 4, 2,
2697 &sprite_trajectory_tab[0][0], 4, 2); 2762 &sprite_trajectory_tab[0][0], 4, 2);
2698 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4, 2763 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2699 &mb_type_b_tab[0][1], 2, 1, 2764 &mb_type_b_tab[0][1], 2, 1,
2700 &mb_type_b_tab[0][0], 2, 1); 2765 &mb_type_b_tab[0][0], 2, 1);
2766 init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
2767 &h263_mbtype_b_tab[0][1], 2, 1,
2768 &h263_mbtype_b_tab[0][0], 2, 1);
2769 init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
2770 &cbpc_b_tab[0][1], 2, 1,
2771 &cbpc_b_tab[0][0], 2, 1);
2701 } 2772 }
2702 } 2773 }
2703 2774
2704 /** 2775 /**
2705 * Get the GOB height based on picture height. 2776 * Get the GOB height based on picture height.
3509 /* 16x16 motion prediction */ 3580 /* 16x16 motion prediction */
3510 mot_val= h263_pred_motion(s, 0, &pred_x, &pred_y); 3581 mot_val= h263_pred_motion(s, 0, &pred_x, &pred_y);
3511 if (s->umvplus) 3582 if (s->umvplus)
3512 mx = h263p_decode_umotion(s, pred_x); 3583 mx = h263p_decode_umotion(s, pred_x);
3513 else 3584 else
3514 mx = h263_decode_motion(s, pred_x, s->f_code); 3585 mx = h263_decode_motion(s, pred_x, 1);
3515 3586
3516 if (s->umvplus) 3587 if (s->umvplus)
3517 my = h263p_decode_umotion(s, pred_y); 3588 my = h263p_decode_umotion(s, pred_y);
3518 else 3589 else
3519 my = h263_decode_motion(s, pred_y, s->f_code); 3590 my = h263_decode_motion(s, pred_y, 1);
3520 3591
3521 mot_val[0 ]= mot_val[2 ]= 3592 mot_val[0 ]= mot_val[2 ]=
3522 mot_val[0+stride]= mot_val[2+stride]= mx; 3593 mot_val[0+stride]= mot_val[2+stride]= mx;
3523 mot_val[1 ]= mot_val[3 ]= 3594 mot_val[1 ]= mot_val[3 ]=
3524 mot_val[1+stride]= mot_val[3+stride]= my; 3595 mot_val[1+stride]= mot_val[3+stride]= my;
3527 for(i=0;i<4;i++) { 3598 for(i=0;i<4;i++) {
3528 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y); 3599 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
3529 if (s->umvplus) 3600 if (s->umvplus)
3530 mx = h263p_decode_umotion(s, pred_x); 3601 mx = h263p_decode_umotion(s, pred_x);
3531 else 3602 else
3532 mx = h263_decode_motion(s, pred_x, s->f_code); 3603 mx = h263_decode_motion(s, pred_x, 1);
3533 3604
3534 if (s->umvplus) 3605 if (s->umvplus)
3535 my = h263p_decode_umotion(s, pred_y); 3606 my = h263p_decode_umotion(s, pred_y);
3536 else 3607 else
3537 my = h263_decode_motion(s, pred_y, s->f_code); 3608 my = h263_decode_motion(s, pred_y, 1);
3538 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1) 3609 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
3539 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ 3610 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
3540 mot_val[0] = mx; 3611 mot_val[0] = mx;
3541 mot_val[1] = my; 3612 mot_val[1] = my;
3542 } 3613 }
3614 s->mv_type = MV_TYPE_16X16; 3685 s->mv_type = MV_TYPE_16X16;
3615 h263_pred_motion(s, 0, &pred_x, &pred_y); 3686 h263_pred_motion(s, 0, &pred_x, &pred_y);
3616 if (s->umvplus) 3687 if (s->umvplus)
3617 mx = h263p_decode_umotion(s, pred_x); 3688 mx = h263p_decode_umotion(s, pred_x);
3618 else 3689 else
3619 mx = h263_decode_motion(s, pred_x, s->f_code); 3690 mx = h263_decode_motion(s, pred_x, 1);
3620 3691
3621 if (mx >= 0xffff) 3692 if (mx >= 0xffff)
3622 return -1; 3693 return -1;
3623 3694
3624 if (s->umvplus) 3695 if (s->umvplus)
3625 my = h263p_decode_umotion(s, pred_y); 3696 my = h263p_decode_umotion(s, pred_y);
3626 else 3697 else
3627 my = h263_decode_motion(s, pred_y, s->f_code); 3698 my = h263_decode_motion(s, pred_y, 1);
3628 3699
3629 if (my >= 0xffff) 3700 if (my >= 0xffff)
3630 return -1; 3701 return -1;
3631 s->mv[0][0][0] = mx; 3702 s->mv[0][0][0] = mx;
3632 s->mv[0][0][1] = my; 3703 s->mv[0][0][1] = my;
3639 for(i=0;i<4;i++) { 3710 for(i=0;i<4;i++) {
3640 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y); 3711 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
3641 if (s->umvplus) 3712 if (s->umvplus)
3642 mx = h263p_decode_umotion(s, pred_x); 3713 mx = h263p_decode_umotion(s, pred_x);
3643 else 3714 else
3644 mx = h263_decode_motion(s, pred_x, s->f_code); 3715 mx = h263_decode_motion(s, pred_x, 1);
3645 if (mx >= 0xffff) 3716 if (mx >= 0xffff)
3646 return -1; 3717 return -1;
3647 3718
3648 if (s->umvplus) 3719 if (s->umvplus)
3649 my = h263p_decode_umotion(s, pred_y); 3720 my = h263p_decode_umotion(s, pred_y);
3650 else 3721 else
3651 my = h263_decode_motion(s, pred_y, s->f_code); 3722 my = h263_decode_motion(s, pred_y, 1);
3652 if (my >= 0xffff) 3723 if (my >= 0xffff)
3653 return -1; 3724 return -1;
3654 s->mv[0][i][0] = mx; 3725 s->mv[0][i][0] = mx;
3655 s->mv[0][i][1] = my; 3726 s->mv[0][i][1] = my;
3656 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1) 3727 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
3657 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ 3728 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
3658 mot_val[0] = mx; 3729 mot_val[0] = mx;
3659 mot_val[1] = my; 3730 mot_val[1] = my;
3660 } 3731 }
3661 } 3732 }
3733
3734 if(s->obmc){
3735 if(s->pict_type == P_TYPE && s->mb_x+1<s->mb_width)
3736 preview_obmc(s);
3737 }
3738 } else if(s->pict_type==B_TYPE) {
3739 int mb_type;
3740 const int stride= s->b8_stride;
3741 int16_t *mot_val0 = s->current_picture.motion_val[0][ s->mb_x + s->mb_y*stride ];
3742 int16_t *mot_val1 = s->current_picture.motion_val[1][ s->mb_x + s->mb_y*stride ];
3743 // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
3744
3745 //FIXME ugly
3746 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+stride]= mot_val0[2+stride]= 0;
3747 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+stride]= mot_val0[3+stride]= 0;
3748 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+stride]= mot_val1[2+stride]= 0;
3749 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+stride]= mot_val1[3+stride]= 0;
3750
3751 do{
3752 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
3753 if (mb_type < 0){
3754 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
3755 return -1;
3756 }
3757
3758 mb_type= h263_mb_type_b_map[ mb_type ];
3759 }while(!mb_type);
3760
3761 s->mb_intra = IS_INTRA(mb_type);
3762 if(HAS_CBP(mb_type)){
3763 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
3764 if(s->mb_intra){
3765 dquant = IS_QUANT(mb_type);
3766 goto intra;
3767 }
3768
3769 cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
3770
3771 if (cbpy < 0){
3772 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
3773 return -1;
3774 }
3775
3776 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
3777 cbpy ^= 0xF;
3778
3779 cbp = (cbpc & 3) | (cbpy << 2);
3780 }else
3781 cbp=0;
3782
3783 assert(!s->mb_intra);
3784
3785 if(IS_QUANT(mb_type)){
3786 if(s->modified_quant){ //FIXME factorize
3787 if(get_bits1(&s->gb))
3788 s->qscale= modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
3789 else
3790 s->qscale= get_bits(&s->gb, 5);
3791 }else
3792 s->qscale += quant_tab[get_bits(&s->gb, 2)];
3793 ff_set_qscale(s, s->qscale);
3794 }
3795
3796 if(IS_DIRECT(mb_type)){
3797 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3798 mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
3799 }else{
3800 s->mv_dir = 0;
3801 s->mv_type= MV_TYPE_16X16;
3802 //FIXME UMV
3803
3804 if(USES_LIST(mb_type, 0)){
3805 int16_t *mot_val= h263_pred_motion2(s, 0, 0, &mx, &my);
3806 s->mv_dir = MV_DIR_FORWARD;
3807
3808 mx = h263_decode_motion(s, mx, 1);
3809 my = h263_decode_motion(s, my, 1);
3810 s->mv[0][0][0] = mx;
3811 s->mv[0][0][1] = my;
3812 mot_val[0 ]= mot_val[2 ]= mot_val[0+stride]= mot_val[2+stride]= mx;
3813 mot_val[1 ]= mot_val[3 ]= mot_val[1+stride]= mot_val[3+stride]= my;
3814 }
3815
3816 if(USES_LIST(mb_type, 1)){
3817 int16_t *mot_val= h263_pred_motion2(s, 0, 1, &mx, &my);
3818 s->mv_dir |= MV_DIR_BACKWARD;
3819
3820 mx = h263_decode_motion(s, mx, 1);
3821 my = h263_decode_motion(s, my, 1);
3822 s->mv[1][0][0] = mx;
3823 s->mv[1][0][1] = my;
3824 mot_val[0 ]= mot_val[2 ]= mot_val[0+stride]= mot_val[2+stride]= mx;
3825 mot_val[1 ]= mot_val[3 ]= mot_val[1+stride]= mot_val[3+stride]= my;
3826 }
3827 }
3828
3829 s->current_picture.mb_type[xy]= mb_type;
3662 } else { /* I-Frame */ 3830 } else { /* I-Frame */
3663 do{ 3831 do{
3664 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2); 3832 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
3665 if (cbpc < 0){ 3833 if (cbpc < 0){
3666 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y); 3834 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
3696 s->qscale= get_bits(&s->gb, 5); 3864 s->qscale= get_bits(&s->gb, 5);
3697 }else 3865 }else
3698 s->qscale += quant_tab[get_bits(&s->gb, 2)]; 3866 s->qscale += quant_tab[get_bits(&s->gb, 2)];
3699 ff_set_qscale(s, s->qscale); 3867 ff_set_qscale(s, s->qscale);
3700 } 3868 }
3701
3702 /* decode each block */
3703 for (i = 0; i < 6; i++) {
3704 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
3705 return -1;
3706 cbp+=cbp;
3707 }
3708 goto end;
3709 } 3869 }
3710 3870
3711 /* decode each block */ 3871 /* decode each block */
3712 for (i = 0; i < 6; i++) { 3872 for (i = 0; i < 6; i++) {
3713 if (h263_decode_block(s, block[i], i, cbp&32) < 0) 3873 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
3714 return -1; 3874 return -1;
3715 cbp+=cbp; 3875 cbp+=cbp;
3716 } 3876 }
3717 end: 3877 end:
3718 if(s->obmc){
3719 if(s->pict_type == P_TYPE && s->mb_x+1<s->mb_width)
3720 preview_obmc(s);
3721 }
3722 3878
3723 /* per-MB end of slice check */ 3879 /* per-MB end of slice check */
3724 { 3880 {
3725 int v= show_bits(&s->gb, 16); 3881 int v= show_bits(&s->gb, 16);
3726 3882
4690 av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep); 4846 av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
4691 return -1; 4847 return -1;
4692 } 4848 }
4693 4849
4694 /* MPPTYPE */ 4850 /* MPPTYPE */
4695 s->pict_type = get_bits(&s->gb, 3) + I_TYPE; 4851 s->pict_type = get_bits(&s->gb, 3);
4696 if (s->pict_type == 8 && s->avctx->codec_tag == ff_get_fourcc("ZYGO")) 4852 switch(s->pict_type){
4697 s->pict_type = I_TYPE; 4853 case 0: s->pict_type= I_TYPE;break;
4698 if (s->pict_type != I_TYPE && 4854 case 1: s->pict_type= P_TYPE;break;
4699 s->pict_type != P_TYPE) 4855 case 3: s->pict_type= B_TYPE;break;
4856 case 7: s->pict_type= I_TYPE;break; //ZYGO
4857 default:
4700 return -1; 4858 return -1;
4859 }
4701 skip_bits(&s->gb, 2); 4860 skip_bits(&s->gb, 2);
4702 s->no_rounding = get_bits1(&s->gb); 4861 s->no_rounding = get_bits1(&s->gb);
4703 skip_bits(&s->gb, 4); 4862 skip_bits(&s->gb, 4);
4704 4863
4705 /* Get the picture dimensions */ 4864 /* Get the picture dimensions */