Revision 6f91bcd1 libavcodec/h263.c

View differences:

libavcodec/h263.c
35 35
static void h263p_encode_umotion(MpegEncContext * s, int val);
36 36
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
37 37
			       int n);
38
static int h263_decode_motion(MpegEncContext * s, int pred);
38
static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
39 39
static int h263p_decode_umotion(MpegEncContext * s, int pred);
40 40
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
41 41
                             int n, int coded);
......
421 421
    return mot_val;
422 422
}
423 423

  
424

  
425 424
static void h263_encode_motion(MpegEncContext * s, int val)
426 425
{
427 426
    int range, l, m, bit_size, sign, code, bits;
......
571 570

  
572 571
/***************************************************/
573 572

  
573
static void mpeg4_stuffing(PutBitContext * pbc)
574
{
575
    int length;
576
    put_bits(pbc, 1, 0);
577
    length= (-get_bit_count(pbc))&7;
578
    put_bits(pbc, length, (1<<length)-1);
579
}
580

  
581
static void put_string(PutBitContext * pbc, char *s)
582
{
583
    while(*s){
584
        put_bits(pbc, 8, *s);
585
        s++;
586
    }
587
    put_bits(pbc, 8, 0);
588
}
589

  
574 590
/* write mpeg4 VOP header */
575 591
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
576 592
{
577
    align_put_bits(&s->pb);
578

  
593
    mpeg4_stuffing(&s->pb);
594
    put_bits(&s->pb, 16, 0);
595
    put_bits(&s->pb, 16, 0x1B2);	/* user_data */
596
    put_string(&s->pb, "ffmpeg"); //FIXME append some version ...
597
    
598
    mpeg4_stuffing(&s->pb);
579 599
    put_bits(&s->pb, 16, 0);	        /* vop header */
580 600
    put_bits(&s->pb, 16, 0x1B6);	/* vop header */
581 601
    put_bits(&s->pb, 2, s->pict_type - 1);	/* pict type: I = 0 , P = 1 */
......
839 859
static VLC mv_vlc;
840 860
static VLC dc_lum, dc_chrom;
841 861
static VLC sprite_trajectory;
862
static VLC mb_type_b_vlc;
842 863

  
843 864
void init_rl(RLTable *rl)
844 865
{
......
922 943
        init_vlc(&sprite_trajectory, 9, 15,
923 944
                 &sprite_trajectory_tab[0][1], 4, 2,
924 945
                 &sprite_trajectory_tab[0][0], 4, 2);
946
        init_vlc(&mb_type_b_vlc, 4, 4,
947
                 &mb_type_b_tab[0][1], 2, 1,
948
                 &mb_type_b_tab[0][0], 2, 1);
925 949
    }
926 950
}
927 951

  
......
997 1021
        
998 1022
        dquant = cbpc & 8;
999 1023
        s->mb_intra = ((cbpc & 4) != 0);
1000
    } else {
1001
        cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
1002
        if (cbpc < 0)
1003
            return -1;
1004
        dquant = cbpc & 4;
1005
        s->mb_intra = 1;
1006
    }
1007

  
1008
    if (!s->mb_intra) {
1024
        if (s->mb_intra) goto intra;
1025
        
1009 1026
        if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
1010 1027
            s->mcsel= get_bits1(&s->gb);
1011 1028
        else s->mcsel= 0;
......
1026 1043
            if (s->umvplus_dec)
1027 1044
               mx = h263p_decode_umotion(s, pred_x);
1028 1045
            else if(!s->mcsel)
1029
               mx = h263_decode_motion(s, pred_x);
1046
               mx = h263_decode_motion(s, pred_x, s->f_code);
1030 1047
            else {
1031 1048
               const int a= s->sprite_warping_accuracy;
1032 1049
//        int l = (1 << (s->f_code - 1)) * 32;
......
1040 1057
            if (s->umvplus_dec)
1041 1058
               my = h263p_decode_umotion(s, pred_y);
1042 1059
            else if(!s->mcsel)
1043
               my = h263_decode_motion(s, pred_y);
1060
               my = h263_decode_motion(s, pred_y, s->f_code);
1044 1061
            else{
1045 1062
               const int a= s->sprite_warping_accuracy;
1046 1063
//       int l = (1 << (s->f_code - 1)) * 32;
......
1065 1082
                if (s->umvplus_dec)
1066 1083
                  mx = h263p_decode_umotion(s, pred_x);
1067 1084
                else
1068
                  mx = h263_decode_motion(s, pred_x);
1085
                  mx = h263_decode_motion(s, pred_x, s->f_code);
1069 1086
                if (mx >= 0xffff)
1070 1087
                    return -1;
1071 1088
                
1072 1089
                if (s->umvplus_dec)
1073 1090
                  my = h263p_decode_umotion(s, pred_y);
1074 1091
                else    
1075
                  my = h263_decode_motion(s, pred_y);
1092
                  my = h263_decode_motion(s, pred_y, s->f_code);
1076 1093
                if (my >= 0xffff)
1077 1094
                    return -1;
1078 1095
                s->mv[0][i][0] = mx;
......
1083 1100
                mot_val[1] = my;
1084 1101
            }
1085 1102
        }
1086
    } else {
1103
    } else if(s->pict_type==B_TYPE) {
1104
        int modb1; // first bit of modb
1105
        int modb2; // second bit of modb
1106
        int mb_type;
1107
        int time_pp;
1108
        int time_pb;
1109
        int xy;
1110

  
1111
        s->mb_intra = 0; //B-frames never contain intra blocks
1112
        s->mcsel=0;      //     ...               true gmc blocks
1113

  
1114
        if(s->mb_x==0){
1115
            s->last_mv[0][0][0]= 
1116
            s->last_mv[0][0][1]= 
1117
            s->last_mv[1][0][0]= 
1118
            s->last_mv[1][0][1]= 0;
1119
        }
1120

  
1121
        /* if we skipped it in the future P Frame than skip it now too */
1122
        s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC
1123

  
1124
        if(s->mb_skiped){
1125
                /* skip mb */
1126
            for(i=0;i<6;i++)
1127
                s->block_last_index[i] = -1;
1128

  
1129
            s->mv_dir = MV_DIR_FORWARD;
1130
            s->mv_type = MV_TYPE_16X16;
1131
            s->mv[0][0][0] = 0;
1132
            s->mv[0][0][1] = 0;
1133
            s->mv[1][0][0] = 0;
1134
            s->mv[1][0][1] = 0;
1135
            s->last_mv[0][0][0]=
1136
            s->last_mv[0][0][1]= 
1137
            s->last_mv[1][0][0]= 
1138
            s->last_mv[1][0][1]= 0;
1139
            s->mb_skiped = 1;
1140
            return 0;
1141
        }
1142

  
1143
        modb1= get_bits1(&s->gb);
1144
        if(modb1==0){
1145
            modb2= get_bits1(&s->gb);
1146
            mb_type= get_vlc(&s->gb, &mb_type_b_vlc);
1147
            if(modb2==0) cbp= get_bits(&s->gb, 6);
1148
            else cbp=0;
1149
            if (mb_type && cbp) {
1150
                if(get_bits1(&s->gb)){
1151
                    s->qscale +=get_bits1(&s->gb)*4 - 2;
1152
                    if (s->qscale < 1)
1153
                        s->qscale = 1;
1154
                    else if (s->qscale > 31)
1155
                        s->qscale = 31;
1156
                }
1157
            }
1158
        }else{
1159
            mb_type=4; //like 0 but no vectors coded
1160
            cbp=0;
1161
        }
1162
        s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here
1163
        mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my
1164
        switch(mb_type)
1165
        {
1166
        case 0: 
1167
            mx = h263_decode_motion(s, 0, 1);
1168
            my = h263_decode_motion(s, 0, 1);
1169
        case 4: 
1170
            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1171
            xy= (2*s->mb_width + 2)*(2*s->mb_y + 1) + 2*s->mb_x + 1;
1172
            time_pp= s->last_non_b_time[0] - s->last_non_b_time[1];
1173
            time_pb= s->time - s->last_non_b_time[1];
1174
//if(time_pp>3000 )printf("%d %d  ", time_pp, time_pb);
1175
            //FIXME 4MV
1176
            //FIXME avoid divides
1177
            s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
1178
            s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
1179
            s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
1180
                                : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp + mx;
1181
            s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] 
1182
                                : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my;
1183
/*            s->mv[0][0][0] = 
1184
            s->mv[0][0][1] = 
1185
            s->mv[1][0][0] = 
1186
            s->mv[1][0][1] = 1000;*/
1187
            break;
1188
        case 1: 
1189
            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1190
            mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1191
            my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1192
            s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
1193
            s->last_mv[0][0][1]= s->mv[0][0][1] = my;
1194

  
1195
            mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1196
            my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1197
            s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
1198
            s->last_mv[1][0][1]= s->mv[1][0][1] = my;
1199
            break;
1200
        case 2: 
1201
            s->mv_dir = MV_DIR_BACKWARD;
1202
            mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1203
            my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1204
            s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
1205
            s->last_mv[1][0][1]= s->mv[1][0][1] = my;
1206
            break;
1207
        case 3:
1208
            s->mv_dir = MV_DIR_FORWARD;
1209
            mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1210
            my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1211
            s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
1212
            s->last_mv[0][0][1]= s->mv[0][0][1] = my;
1213
            break;
1214
        default: return -1;
1215
        }
1216
    } else { /* I-Frame */
1217
        cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
1218
        if (cbpc < 0)
1219
            return -1;
1220
        dquant = cbpc & 4;
1221
        s->mb_intra = 1;
1222
intra:
1087 1223
        s->ac_pred = 0;
1088 1224
        if (s->h263_pred || s->h263_aic) {
1089 1225
            s->ac_pred = get_bits1(&s->gb);
......
1120 1256
    return 0;
1121 1257
}
1122 1258

  
1123
static int h263_decode_motion(MpegEncContext * s, int pred)
1259
static int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
1124 1260
{
1125 1261
    int code, val, sign, shift, l, m;
1126 1262

  
......
1131 1267
    if (code == 0)
1132 1268
        return pred;
1133 1269
    sign = get_bits1(&s->gb);
1134
    shift = s->f_code - 1;
1270
    shift = f_code - 1;
1135 1271
    val = (code - 1) << shift;
1136 1272
    if (shift > 0)
1137 1273
        val |= get_bits(&s->gb, shift);
......
1142 1278
    
1143 1279
    /* modulo decoding */
1144 1280
    if (!s->h263_long_vectors) {
1145
        l = (1 << (s->f_code - 1)) * 32;
1281
        l = (1 << (f_code - 1)) * 32;
1146 1282
        m = 2 * l;
1147 1283
        if (val < -l) {
1148 1284
            val += m;
......
1577 1713
    h2= 1<<beta;
1578 1714

  
1579 1715
// Note, the 4th point isnt used for GMC
1580
/*
1581
    sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
1582
    sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
1583
    sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
1584
    sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
1585
    sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
1586
    sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
1587
*/
1588
//FIXME DIVX5 vs. mpeg4 ?
1589
    sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
1590
    sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
1591
    sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
1592
    sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
1593
    sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
1594
    sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
1716
    if(s->divx_version==500 && s->divx_build==413){
1717
        sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
1718
        sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
1719
        sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
1720
        sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
1721
        sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
1722
        sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
1723
    } else {
1724
        sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
1725
        sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
1726
        sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
1727
        sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
1728
        sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
1729
        sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
1730
    }
1595 1731
/*    sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
1596 1732
    sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
1597 1733
    
......
1715 1851
    else
1716 1852
        s->real_sprite_warping_points= s->num_sprite_warping_points;
1717 1853

  
1718
//FIXME convert stuff if accurace != 3
1719 1854
}
1720 1855

  
1721 1856
/* decode mpeg4 VOP header */
......
1741 1876
    }
1742 1877
//printf("startcode %X %d\n", startcode, get_bits_count(&s->gb));
1743 1878
    if (startcode == 0x120) { // Video Object Layer
1744
        int time_increment_resolution, width, height, vo_ver_id;
1879
        int width, height, vo_ver_id;
1745 1880

  
1746 1881
        /* vol header */
1747 1882
        skip_bits(&s->gb, 1); /* random access */
......
1770 1905

  
1771 1906
        skip_bits1(&s->gb);   /* marker */
1772 1907
        
1773
        time_increment_resolution = get_bits(&s->gb, 16);
1774
        s->time_increment_bits = av_log2(time_increment_resolution - 1) + 1;
1908
        s->time_increment_resolution = get_bits(&s->gb, 16);
1909
        s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
1775 1910
        if (s->time_increment_bits < 1)
1776 1911
            s->time_increment_bits = 1;
1777 1912
        skip_bits1(&s->gb);   /* marker */
......
1899 2034
    }
1900 2035

  
1901 2036
    s->pict_type = get_bits(&s->gb, 2) + 1;	/* pict type: I = 0 , P = 1 */
1902
    if(s->pict_type == B_TYPE)
1903
    {
1904
        printf("B-VOP\n");
1905
	return -1;
1906
    }
1907 2037
 
1908
    /* XXX: parse time base */
1909
    time_incr = 0;
2038
    time_incr=0;
1910 2039
    while (get_bits1(&s->gb) != 0) 
1911 2040
        time_incr++;
1912 2041

  
1913 2042
    skip_bits1(&s->gb);   	/* marker */
1914
    skip_bits(&s->gb, s->time_increment_bits);
2043
    s->time_increment= get_bits(&s->gb, s->time_increment_bits);
2044
    if(s->pict_type!=B_TYPE){
2045
        s->time_base+= time_incr;
2046
        s->last_non_b_time[1]= s->last_non_b_time[0];
2047
        s->last_non_b_time[0]= s->time_base*s->time_increment_resolution + s->time_increment;
2048
    }else{
2049
        s->time= (s->last_non_b_time[1]/s->time_increment_resolution + time_incr)*s->time_increment_resolution;
2050
        s->time+= s->time_increment;
2051
    }
1915 2052
    skip_bits1(&s->gb);   	/* marker */
1916 2053
    /* vop coded */
1917 2054
    if (get_bits1(&s->gb) != 1)
1918 2055
        goto redo;
1919
    
2056
//printf("time %d %d %d || %d %d %d\n", s->time_increment_bits, s->time_increment, s->time_base,
2057
//s->time, s->last_non_b_time[0], s->last_non_b_time[1]);  
1920 2058
    if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE
1921 2059
                          || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) {
1922 2060
        /* rounding type for motion estimation */
......
1967 2105
  
1968 2106
         if (s->pict_type != I_TYPE) {
1969 2107
             s->f_code = get_bits(&s->gb, 3);	/* fcode_for */
2108
//printf("f-code %d\n", s->f_code);
1970 2109
         }
1971 2110
         if (s->pict_type == B_TYPE) {
1972 2111
             s->b_code = get_bits(&s->gb, 3);
2112
//printf("b-code %d\n", s->b_code);
1973 2113
         }
1974 2114
//printf("quant:%d fcode:%d\n", s->qscale, s->f_code);
1975 2115
         if(!s->scalability){

Also available in: Unified diff