Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 119014 | Differences between
and this patch

Collapse All | Expand All

(-)mpeg12.c (-826 / +825 lines)
Lines 1038-1604 Link Here
1038
        return 0;
1038
        return 0;
1039
}
1039
}
1040
1040
1041
static inline int get_qscale(MpegEncContext *s)
1041
static inline int decode_dc(GetBitContext *gb, int component)
1042
{
1042
{
1043
    int qscale = get_bits(&s->gb, 5);
1043
    int code, diff;
1044
    if (s->q_scale_type) {
1044
1045
        return non_linear_qscale[qscale];
1045
    if (component == 0) {
1046
        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1046
    } else {
1047
    } else {
1047
        return qscale << 1;
1048
        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1049
    }
1050
    if (code < 0){
1051
        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
1052
        return 0xffff;
1053
    }
1054
    if (code == 0) {
1055
        diff = 0;
1056
    } else {
1057
        diff = get_xbits(gb, code);
1048
    }
1058
    }
1059
    return diff;
1049
}
1060
}
1050
1061
1051
/* motion type (for mpeg2) */
1062
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, 
1052
#define MT_FIELD 1
1063
                               DCTELEM *block, 
1053
#define MT_FRAME 2
1064
                               int n)
1054
#define MT_16X8  2
1055
#define MT_DMV   3
1056
1057
static int mpeg_decode_mb(MpegEncContext *s,
1058
                          DCTELEM block[12][64])
1059
{
1065
{
1060
    int i, j, k, cbp, val, mb_type, motion_type;
1066
    int level, dc, diff, j, run;
1061
    const int mb_block_count = 4 + (1<< s->chroma_format);
1067
    int component;
1062
1068
    RLTable *rl;
1063
    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1069
    uint8_t * scantable= s->intra_scantable.permutated;
1070
    const uint16_t *quant_matrix;
1071
    const int qscale= s->qscale;
1064
1072
1065
    assert(s->mb_skipped==0);
1073
    /* DC coef */
1074
    if (n < 4){
1075
        quant_matrix = s->intra_matrix;
1076
        component = 0; 
1077
    }else{
1078
        quant_matrix = s->chroma_intra_matrix;
1079
        component = (n&1) + 1;
1080
    }
1081
    diff = decode_dc(&s->gb, component);
1082
    if (diff >= 0xffff)
1083
        return -1;
1084
    dc = s->last_dc[component];
1085
    dc += diff;
1086
    s->last_dc[component] = dc;
1087
    block[0] = dc << (3 - s->intra_dc_precision);
1088
    if (s->intra_vlc_format)
1089
        rl = &rl_mpeg2;
1090
    else
1091
        rl = &rl_mpeg1;
1066
1092
1067
    if (s->mb_skip_run-- != 0) {
1093
    {
1068
        if(s->pict_type == I_TYPE){
1094
        OPEN_READER(re, &s->gb);    
1069
            av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1095
        /* now quantify & encode AC coefs */
1070
            return -1;
1096
        for(;;) {
1071
        }
1097
            UPDATE_CACHE(re, &s->gb);
1072
    
1098
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1073
        /* skip mb */
1074
        s->mb_intra = 0;
1075
        for(i=0;i<12;i++)
1076
            s->block_last_index[i] = -1;
1077
        if(s->picture_structure == PICT_FRAME)
1078
            s->mv_type = MV_TYPE_16X16;
1079
        else
1080
            s->mv_type = MV_TYPE_FIELD;
1081
        if (s->pict_type == P_TYPE) {
1082
            /* if P type, zero motion vector is implied */
1083
            s->mv_dir = MV_DIR_FORWARD;
1084
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
1085
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1086
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1087
            s->field_select[0][0]= s->picture_structure - 1;
1088
            s->mb_skipped = 1;
1089
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1090
        } else {
1091
            int mb_type;
1092
            
1099
            
1093
            if(s->mb_x)
1100
            if(level == 127){
1094
                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
1101
                break;
1095
            else
1102
            } else if(level != 0) {
1096
                mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all, 
1103
                scantable += run;
1097
            if(IS_INTRA(mb_type))
1104
                j = *scantable;
1098
                return -1;
1105
                level= (level*qscale*quant_matrix[j])>>4;
1106
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1107
                LAST_SKIP_BITS(re, &s->gb, 1);
1108
            } else {
1109
                /* escape */
1110
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1111
                UPDATE_CACHE(re, &s->gb);
1112
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1113
                scantable += run;
1114
                j = *scantable;
1115
                if(level<0){
1116
                    level= (-level*qscale*quant_matrix[j])>>4;
1117
                    level= -level;
1118
                }else{
1119
                    level= (level*qscale*quant_matrix[j])>>4;
1120
                }
1121
            }
1099
            
1122
            
1100
            /* if B type, reuse previous vectors and directions */
1123
            block[j] = level;
1101
            s->mv[0][0][0] = s->last_mv[0][0][0];
1102
            s->mv[0][0][1] = s->last_mv[0][0][1];
1103
            s->mv[1][0][0] = s->last_mv[1][0][0];
1104
            s->mv[1][0][1] = s->last_mv[1][0][1];
1105
1106
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= 
1107
                mb_type | MB_TYPE_SKIP;
1108
//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1109
1110
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) 
1111
                s->mb_skipped = 1;
1112
        }
1124
        }
1125
        CLOSE_READER(re, &s->gb);
1126
    }
1127
    
1128
    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
1129
    return 0;
1130
}
1113
1131
1114
        return 0;
1132
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
1133
                               DCTELEM *block, 
1134
                               int n)
1135
{
1136
    int level, dc, diff, i, j, run;
1137
    int component;
1138
    RLTable *rl;
1139
    uint8_t * const scantable= s->intra_scantable.permutated;
1140
    const uint16_t *quant_matrix;
1141
    const int qscale= s->qscale;
1142
    int mismatch;
1143
1144
    /* DC coef */
1145
    if (n < 4){
1146
        quant_matrix = s->intra_matrix;
1147
        component = 0; 
1148
    }else{
1149
        quant_matrix = s->chroma_intra_matrix;
1150
        component = (n&1) + 1;
1115
    }
1151
    }
1152
    diff = decode_dc(&s->gb, component);
1153
    if (diff >= 0xffff)
1154
        return -1;
1155
    dc = s->last_dc[component];
1156
    dc += diff;
1157
    s->last_dc[component] = dc;
1158
    block[0] = dc << (3 - s->intra_dc_precision);
1159
    dprintf("dc=%d\n", block[0]);
1160
    mismatch = block[0] ^ 1;
1161
    i = 0;
1162
    if (s->intra_vlc_format)
1163
        rl = &rl_mpeg2;
1164
    else
1165
        rl = &rl_mpeg1;
1116
1166
1117
    switch(s->pict_type) {
1167
    {
1118
    default:
1168
        OPEN_READER(re, &s->gb);    
1119
    case I_TYPE:
1169
        /* now quantify & encode AC coefs */
1120
        if (get_bits1(&s->gb) == 0) {
1170
        for(;;) {
1121
            if (get_bits1(&s->gb) == 0){
1171
            UPDATE_CACHE(re, &s->gb);
1122
                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
1172
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1173
            
1174
            if(level == 127){
1175
                break;
1176
            } else if(level != 0) {
1177
                i += run;
1178
                j = scantable[i];
1179
                level= (level*qscale*quant_matrix[j])>>4;
1180
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1181
                LAST_SKIP_BITS(re, &s->gb, 1);
1182
            } else {
1183
                /* escape */
1184
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1185
                UPDATE_CACHE(re, &s->gb);
1186
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1187
                i += run;
1188
                j = scantable[i];
1189
                if(level<0){
1190
                    level= (-level*qscale*quant_matrix[j])>>4;
1191
                    level= -level;
1192
                }else{
1193
                    level= (level*qscale*quant_matrix[j])>>4;
1194
                }
1195
            }
1196
            if (i > 63){
1197
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1123
                return -1;
1198
                return -1;
1124
            }
1199
            }
1125
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
1200
            
1126
        } else {
1201
            mismatch^= level;
1127
            mb_type = MB_TYPE_INTRA;
1202
            block[j] = level;
1128
        }
1129
        break;
1130
    case P_TYPE:
1131
        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
1132
        if (mb_type < 0){
1133
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
1134
            return -1;
1135
        }
1136
        mb_type = ptype2mb_type[ mb_type ];
1137
        break;
1138
    case B_TYPE:
1139
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
1140
        if (mb_type < 0){
1141
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
1142
            return -1;
1143
        }
1203
        }
1144
        mb_type = btype2mb_type[ mb_type ];
1204
        CLOSE_READER(re, &s->gb);
1145
        break;
1146
    }
1205
    }
1147
    dprintf("mb_type=%x\n", mb_type);
1206
    block[63]^= mismatch&1;
1148
//    motion_type = 0; /* avoid warning */
1149
    if (IS_INTRA(mb_type)) {
1150
        s->dsp.clear_blocks(s->block[0]);
1151
    
1152
        if(!s->chroma_y_shift){
1153
            s->dsp.clear_blocks(s->block[6]);
1154
        }
1155
    
1207
    
1156
        /* compute dct type */
1208
    s->block_last_index[n] = i;
1157
        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1209
    return 0;
1158
            !s->frame_pred_frame_dct) {
1210
}
1159
            s->interlaced_dct = get_bits1(&s->gb);
1160
        }
1161
1211
1162
        if (IS_QUANT(mb_type))
1212
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
1163
            s->qscale = get_qscale(s);
1213
                               DCTELEM *block, 
1164
        
1214
                               int n)
1165
        if (s->concealment_motion_vectors) {
1215
{
1166
            /* just parse them */
1216
    int level, dc, diff, i, j, run;
1167
            if (s->picture_structure != PICT_FRAME) 
1217
    int component;
1168
                skip_bits1(&s->gb); /* field select */
1218
    RLTable *rl = &rl_mpeg1;
1169
            
1219
    uint8_t * const scantable= s->intra_scantable.permutated;
1170
            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] = 
1220
    const uint16_t *quant_matrix= s->intra_matrix;
1171
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
1221
    const int qscale= s->qscale;
1172
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] = 
1173
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
1174
1175
            skip_bits1(&s->gb); /* marker */
1176
        }else
1177
            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
1178
        s->mb_intra = 1;
1179
#ifdef HAVE_XVMC
1180
        //one 1 we memcpy blocks in xvmcvideo
1181
        if(s->avctx->xvmc_acceleration > 1){
1182
            XVMC_pack_pblocks(s,-1);//inter are always full blocks
1183
            if(s->swap_uv){
1184
                exchange_uv(s);
1185
            }
1186
        }
1187
#endif
1188
1222
1189
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1223
    /* DC coef */
1190
            if(s->flags2 & CODEC_FLAG2_FAST){
1224
    component = (n <= 3 ? 0 : n - 4 + 1);
1191
                for(i=0;i<6;i++) {
1225
    diff = decode_dc(&s->gb, component);
1192
                    mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
1226
    if (diff >= 0xffff)
1227
        return -1;
1228
    dc = s->last_dc[component];
1229
    dc += diff;
1230
    s->last_dc[component] = dc;
1231
    block[0] = dc<<3;
1232
    dprintf("dc=%d diff=%d\n", dc, diff);
1233
    i = 0;
1234
    {
1235
        OPEN_READER(re, &s->gb);    
1236
        /* now quantify & encode AC coefs */
1237
        for(;;) {
1238
            UPDATE_CACHE(re, &s->gb);
1239
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1240
            
1241
            if(level == 127){
1242
                break;
1243
            } else if(level != 0) {
1244
                i += run;
1245
                j = scantable[i];
1246
                level= (level*qscale*quant_matrix[j])>>4;
1247
                level= (level-1)|1;
1248
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1249
                LAST_SKIP_BITS(re, &s->gb, 1);
1250
            } else {
1251
                /* escape */
1252
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1253
                UPDATE_CACHE(re, &s->gb);
1254
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1255
                if (level == -128) {
1256
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1257
                } else if (level == 0) {
1258
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
1193
                }
1259
                }
1194
            }else{
1260
                i += run;
1195
                for(i=0;i<mb_block_count;i++) {
1261
                j = scantable[i];
1196
                    if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
1262
                if(level<0){
1197
                        return -1;
1263
                    level= -level;
1264
                    level= (level*qscale*quant_matrix[j])>>4;
1265
                    level= (level-1)|1;
1266
                    level= -level;
1267
                }else{
1268
                    level= (level*qscale*quant_matrix[j])>>4;
1269
                    level= (level-1)|1;
1198
                }
1270
                }
1199
            }
1271
            }
1200
        } else {
1272
            if (i > 63){
1201
            for(i=0;i<6;i++) {
1273
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1202
                if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
1274
                return -1;
1203
                    return -1;
1204
            }
1275
            }
1276
1277
            block[j] = level;
1205
        }
1278
        }
1206
    } else {
1279
        CLOSE_READER(re, &s->gb);
1207
        if (mb_type & MB_TYPE_ZERO_MV){
1280
    }
1208
            assert(mb_type & MB_TYPE_CBP);
1281
    s->block_last_index[n] = i;
1282
   return 0;
1283
}
1209
1284
1210
            /* compute dct type */
1285
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
1211
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1286
                               DCTELEM *block, 
1212
                !s->frame_pred_frame_dct) {
1287
                               int n)
1213
                s->interlaced_dct = get_bits1(&s->gb);
1288
{
1214
            }
1289
    int level, i, j, run;
1290
    RLTable *rl = &rl_mpeg1;
1291
    uint8_t * const scantable= s->intra_scantable.permutated;
1292
    const uint16_t *quant_matrix= s->inter_matrix;
1293
    const int qscale= s->qscale;
1215
1294
1216
            if (IS_QUANT(mb_type))
1295
    {
1217
                s->qscale = get_qscale(s);
1296
        OPEN_READER(re, &s->gb);
1297
        i = -1;
1298
        /* special case for the first coef. no need to add a second vlc table */
1299
        UPDATE_CACHE(re, &s->gb);
1300
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1301
            level= (3*qscale*quant_matrix[0])>>5;
1302
            level= (level-1)|1;
1303
            if(GET_CACHE(re, &s->gb)&0x40000000)
1304
                level= -level;
1305
            block[0] = level;
1306
            i++;
1307
            SKIP_BITS(re, &s->gb, 2);
1308
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1309
                goto end;
1310
        }
1218
1311
1219
            s->mv_dir = MV_DIR_FORWARD;
1312
        /* now quantify & encode AC coefs */
1220
            if(s->picture_structure == PICT_FRAME)
1313
        for(;;) {
1221
                s->mv_type = MV_TYPE_16X16;
1314
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1222
            else{
1315
            
1223
                s->mv_type = MV_TYPE_FIELD;
1316
            if(level != 0) {
1224
                mb_type |= MB_TYPE_INTERLACED;
1317
                i += run;
1225
                s->field_select[0][0]= s->picture_structure - 1;
1318
                j = scantable[i];
1319
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1320
                level= (level-1)|1;
1321
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1322
                SKIP_BITS(re, &s->gb, 1);
1323
            } else {
1324
                /* escape */
1325
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1326
                UPDATE_CACHE(re, &s->gb);
1327
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1328
                if (level == -128) {
1329
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1330
                } else if (level == 0) {
1331
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
1332
                }
1333
                i += run;
1334
                j = scantable[i];
1335
                if(level<0){
1336
                    level= -level;
1337
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1338
                    level= (level-1)|1;
1339
                    level= -level;
1340
                }else{
1341
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1342
                    level= (level-1)|1;
1343
                }
1226
            }
1344
            }
1227
            s->last_mv[0][0][0] = 0;
1345
            if (i > 63){
1228
            s->last_mv[0][0][1] = 0;
1346
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1229
            s->last_mv[0][1][0] = 0;
1347
                return -1;
1230
            s->last_mv[0][1][1] = 0;
1231
            s->mv[0][0][0] = 0;
1232
            s->mv[0][0][1] = 0;
1233
        }else{
1234
            assert(mb_type & MB_TYPE_L0L1);
1235
//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1236
            /* get additionnal motion vector type */
1237
            if (s->frame_pred_frame_dct) 
1238
                motion_type = MT_FRAME;
1239
            else{
1240
                motion_type = get_bits(&s->gb, 2);
1241
            }
1348
            }
1242
1349
1243
            /* compute dct type */
1350
            block[j] = level;
1244
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1351
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1245
                !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
1352
                break;
1246
                s->interlaced_dct = get_bits1(&s->gb);
1353
            UPDATE_CACHE(re, &s->gb);
1247
            }
1354
        }
1355
end:
1356
        LAST_SKIP_BITS(re, &s->gb, 2);
1357
        CLOSE_READER(re, &s->gb);
1358
    }
1359
    s->block_last_index[n] = i;
1360
    return 0;
1361
}
1248
1362
1249
            if (IS_QUANT(mb_type))
1363
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
1250
                s->qscale = get_qscale(s);
1364
{
1365
    int level, i, j, run;
1366
    RLTable *rl = &rl_mpeg1;
1367
    uint8_t * const scantable= s->intra_scantable.permutated;
1368
    const int qscale= s->qscale;
1251
1369
1252
            /* motion vectors */
1370
    {
1253
            s->mv_dir = 0;
1371
        OPEN_READER(re, &s->gb);
1254
            for(i=0;i<2;i++) {
1372
        i = -1;
1255
                if (USES_LIST(mb_type, i)) {
1373
        /* special case for the first coef. no need to add a second vlc table */
1256
                    s->mv_dir |= (MV_DIR_FORWARD >> i);
1374
        UPDATE_CACHE(re, &s->gb);
1257
                    dprintf("motion_type=%d\n", motion_type);
1375
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1258
                    switch(motion_type) {
1376
            level= (3*qscale)>>1;
1259
                    case MT_FRAME: /* or MT_16X8 */
1377
            level= (level-1)|1;
1260
                        if (s->picture_structure == PICT_FRAME) {
1378
            if(GET_CACHE(re, &s->gb)&0x40000000)
1261
                            /* MT_FRAME */
1379
                level= -level;
1262
                            mb_type |= MB_TYPE_16x16; 
1380
            block[0] = level;
1263
                            s->mv_type = MV_TYPE_16X16;
1381
            i++;
1264
                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] = 
1382
            SKIP_BITS(re, &s->gb, 2);
1265
                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1383
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1266
                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] = 
1384
                goto end;
1267
                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
1268
                            /* full_pel: only for mpeg1 */
1269
                            if (s->full_pel[i]){
1270
                                s->mv[i][0][0] <<= 1;
1271
                                s->mv[i][0][1] <<= 1;
1272
                            }
1273
                        } else {
1274
                            /* MT_16X8 */
1275
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
1276
                            s->mv_type = MV_TYPE_16X8;
1277
                            for(j=0;j<2;j++) {
1278
                                s->field_select[i][j] = get_bits1(&s->gb);
1279
                                for(k=0;k<2;k++) {
1280
                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1281
                                                             s->last_mv[i][j][k]);
1282
                                    s->last_mv[i][j][k] = val;
1283
                                    s->mv[i][j][k] = val;
1284
                                }
1285
                            }
1286
                        }
1287
                        break;
1288
                    case MT_FIELD:
1289
                        s->mv_type = MV_TYPE_FIELD;
1290
                        if (s->picture_structure == PICT_FRAME) {
1291
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
1292
                            for(j=0;j<2;j++) {
1293
                                s->field_select[i][j] = get_bits1(&s->gb);
1294
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1295
                                                         s->last_mv[i][j][0]);
1296
                                s->last_mv[i][j][0] = val;
1297
                                s->mv[i][j][0] = val;
1298
                                dprintf("fmx=%d\n", val);
1299
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1300
                                                         s->last_mv[i][j][1] >> 1);
1301
                                s->last_mv[i][j][1] = val << 1;
1302
                                s->mv[i][j][1] = val;
1303
                                dprintf("fmy=%d\n", val);
1304
                            }
1305
                        } else {
1306
                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
1307
                            s->field_select[i][0] = get_bits1(&s->gb);
1308
                            for(k=0;k<2;k++) {
1309
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1310
                                                         s->last_mv[i][0][k]);
1311
                                s->last_mv[i][0][k] = val;
1312
                                s->last_mv[i][1][k] = val;
1313
                                s->mv[i][0][k] = val;
1314
                            }
1315
                        }
1316
                        break;
1317
                    case MT_DMV:
1318
                        {
1319
                            int dmx, dmy, mx, my, m;
1320
1321
                            mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
1322
                                                    s->last_mv[i][0][0]);
1323
                            s->last_mv[i][0][0] = mx;
1324
                            s->last_mv[i][1][0] = mx;
1325
                            dmx = get_dmv(s);
1326
                            my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
1327
                                                    s->last_mv[i][0][1] >> 1);
1328
                            dmy = get_dmv(s);
1329
                            s->mv_type = MV_TYPE_DMV;
1330
1331
1332
                            s->last_mv[i][0][1] = my<<1;
1333
                            s->last_mv[i][1][1] = my<<1;
1334
1335
                            s->mv[i][0][0] = mx;
1336
                            s->mv[i][0][1] = my;
1337
                            s->mv[i][1][0] = mx;//not used
1338
                            s->mv[i][1][1] = my;//not used
1339
1340
                            if (s->picture_structure == PICT_FRAME) {
1341
                                mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
1342
1343
                                //m = 1 + 2 * s->top_field_first;
1344
                                m = s->top_field_first ? 1 : 3;
1345
1346
                                /* top -> top pred */
1347
                                s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1348
                                s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1349
                                m = 4 - m;
1350
                                s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1351
                                s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1352
                            } else {
1353
                                mb_type |= MB_TYPE_16x16;
1354
1355
                                s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1356
                                s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1357
                                if(s->picture_structure == PICT_TOP_FIELD)
1358
                                    s->mv[i][2][1]--;
1359
                                else 
1360
                                    s->mv[i][2][1]++;
1361
                            }
1362
                        }
1363
                        break;
1364
                    default:
1365
                        av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1366
                        return -1;
1367
                    }
1368
                }
1369
            }
1370
        }
1385
        }
1371
        
1372
        s->mb_intra = 0;
1373
        if (HAS_CBP(mb_type)) {
1374
            s->dsp.clear_blocks(s->block[0]);
1375
        
1376
            if(!s->chroma_y_shift){
1377
                s->dsp.clear_blocks(s->block[6]);
1378
            }
1379
1380
            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1381
            if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
1382
                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1383
                return -1;
1384
            }
1385
            if(mb_block_count > 6){
1386
	         cbp<<= mb_block_count-6;
1387
		 cbp |= get_bits(&s->gb, mb_block_count-6);
1388
            }
1389
1390
#ifdef HAVE_XVMC
1391
            //on 1 we memcpy blocks in xvmcvideo
1392
            if(s->avctx->xvmc_acceleration > 1){
1393
                XVMC_pack_pblocks(s,cbp);
1394
                if(s->swap_uv){
1395
                    exchange_uv(s);
1396
                }
1397
            }    
1398
#endif
1399
1386
1400
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1387
        /* now quantify & encode AC coefs */
1401
                if(s->flags2 & CODEC_FLAG2_FAST){
1388
        for(;;) {
1402
                    for(i=0;i<6;i++) {
1389
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1403
                        if(cbp & 32) {
1390
            
1404
                            mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
1391
            if(level != 0) {
1405
                        } else {
1392
                i += run;
1406
                            s->block_last_index[i] = -1;
1393
                j = scantable[i];
1407
                        }
1394
                level= ((level*2+1)*qscale)>>1;
1408
                        cbp+=cbp;
1395
                level= (level-1)|1;
1409
                    }
1396
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1410
                }else{
1397
                SKIP_BITS(re, &s->gb, 1);
1411
                    cbp<<= 12-mb_block_count;
1412
    
1413
                    for(i=0;i<mb_block_count;i++) {
1414
                        if ( cbp & (1<<11) ) {
1415
                            if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1416
                                return -1;
1417
                        } else {
1418
                            s->block_last_index[i] = -1;
1419
                        }
1420
                        cbp+=cbp;
1421
                    }
1422
                }
1423
            } else {
1398
            } else {
1424
                if(s->flags2 & CODEC_FLAG2_FAST){
1399
                /* escape */
1425
                    for(i=0;i<6;i++) {
1400
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1426
                        if (cbp & 32) {
1401
                UPDATE_CACHE(re, &s->gb);
1427
                            mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
1402
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1428
                        } else {
1403
                if (level == -128) {
1429
                            s->block_last_index[i] = -1;
1404
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1430
                        }
1405
                } else if (level == 0) {
1431
                        cbp+=cbp;
1406
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
1432
                    }
1407
                }
1408
                i += run;
1409
                j = scantable[i];
1410
                if(level<0){
1411
                    level= -level;
1412
                    level= ((level*2+1)*qscale)>>1;
1413
                    level= (level-1)|1;
1414
                    level= -level;
1433
                }else{
1415
                }else{
1434
                    for(i=0;i<6;i++) {
1416
                    level= ((level*2+1)*qscale)>>1;
1435
                        if (cbp & 32) {
1417
                    level= (level-1)|1;
1436
                            if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
1437
                                return -1;
1438
                        } else {
1439
                            s->block_last_index[i] = -1;
1440
                        }
1441
                        cbp+=cbp;
1442
                    }
1443
                }
1418
                }
1444
            }
1419
            }
1445
        }else{
1420
1446
            for(i=0;i<6;i++)
1421
            block[j] = level;
1447
                s->block_last_index[i] = -1;
1422
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1423
                break;
1424
            UPDATE_CACHE(re, &s->gb);
1448
        }
1425
        }
1426
end:
1427
        LAST_SKIP_BITS(re, &s->gb, 2);
1428
        CLOSE_READER(re, &s->gb);
1449
    }
1429
    }
1450
1430
    s->block_last_index[n] = i;
1451
    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1452
1453
    return 0;
1431
    return 0;
1454
}
1432
}
1455
1433
1456
/* as h263, but only 17 codes */
1457
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1458
{
1459
    int code, sign, val, l, shift;
1460
1434
1461
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1435
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
1462
    if (code == 0) {
1436
                               DCTELEM *block, 
1463
        return pred;
1437
                               int n)
1464
    }
1438
{
1465
    if (code < 0) {
1439
    int level, i, j, run;
1466
        return 0xffff;
1440
    RLTable *rl = &rl_mpeg1;
1467
    }
1441
    uint8_t * const scantable= s->intra_scantable.permutated;
1442
    const uint16_t *quant_matrix;
1443
    const int qscale= s->qscale;
1444
    int mismatch;
1468
1445
1469
    sign = get_bits1(&s->gb);
1446
    mismatch = 1;
1470
    shift = fcode - 1;
1471
    val = code;
1472
    if (shift) {
1473
        val = (val - 1) << shift;
1474
        val |= get_bits(&s->gb, shift);
1475
        val++;
1476
    }
1477
    if (sign)
1478
        val = -val;
1479
    val += pred;
1480
    
1481
    /* modulo decoding */
1482
    l= INT_BIT - 5 - shift;
1483
    val = (val<<l)>>l;
1484
    return val;
1485
}
1486
1447
1487
static inline int decode_dc(GetBitContext *gb, int component)
1448
    {
1488
{
1449
        OPEN_READER(re, &s->gb);
1489
    int code, diff;
1450
        i = -1;
1451
        if (n < 4)
1452
            quant_matrix = s->inter_matrix;
1453
        else
1454
            quant_matrix = s->chroma_inter_matrix;
1490
1455
1491
    if (component == 0) {
1492
        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1493
    } else {
1494
        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1495
    }
1496
    if (code < 0){
1497
        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
1498
        return 0xffff;
1499
    }
1500
    if (code == 0) {
1501
        diff = 0;
1502
    } else {
1503
        diff = get_xbits(gb, code);
1504
    }
1505
    return diff;
1506
}
1507
1508
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
1509
                               DCTELEM *block, 
1510
                               int n)
1511
{
1512
    int level, dc, diff, i, j, run;
1513
    int component;
1514
    RLTable *rl = &rl_mpeg1;
1515
    uint8_t * const scantable= s->intra_scantable.permutated;
1516
    const uint16_t *quant_matrix= s->intra_matrix;
1517
    const int qscale= s->qscale;
1518
1519
    /* DC coef */
1520
    component = (n <= 3 ? 0 : n - 4 + 1);
1521
    diff = decode_dc(&s->gb, component);
1522
    if (diff >= 0xffff)
1523
        return -1;
1524
    dc = s->last_dc[component];
1525
    dc += diff;
1526
    s->last_dc[component] = dc;
1527
    block[0] = dc<<3;
1528
    dprintf("dc=%d diff=%d\n", dc, diff);
1529
    i = 0;
1530
    {
1531
        OPEN_READER(re, &s->gb);    
1532
        /* now quantify & encode AC coefs */
1533
        for(;;) {
1534
            UPDATE_CACHE(re, &s->gb);
1535
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1536
            
1537
            if(level == 127){
1538
                break;
1539
            } else if(level != 0) {
1540
                i += run;
1541
                j = scantable[i];
1542
                level= (level*qscale*quant_matrix[j])>>4;
1543
                level= (level-1)|1;
1544
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1545
                LAST_SKIP_BITS(re, &s->gb, 1);
1546
            } else {
1547
                /* escape */
1548
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1549
                UPDATE_CACHE(re, &s->gb);
1550
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1551
                if (level == -128) {
1552
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1553
                } else if (level == 0) {
1554
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
1555
                }
1556
                i += run;
1557
                j = scantable[i];
1558
                if(level<0){
1559
                    level= -level;
1560
                    level= (level*qscale*quant_matrix[j])>>4;
1561
                    level= (level-1)|1;
1562
                    level= -level;
1563
                }else{
1564
                    level= (level*qscale*quant_matrix[j])>>4;
1565
                    level= (level-1)|1;
1566
                }
1567
            }
1568
            if (i > 63){
1569
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1570
                return -1;
1571
            }
1572
1573
            block[j] = level;
1574
        }
1575
        CLOSE_READER(re, &s->gb);
1576
    }
1577
    s->block_last_index[n] = i;
1578
   return 0;
1579
}
1580
1581
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
1582
                               DCTELEM *block, 
1583
                               int n)
1584
{
1585
    int level, i, j, run;
1586
    RLTable *rl = &rl_mpeg1;
1587
    uint8_t * const scantable= s->intra_scantable.permutated;
1588
    const uint16_t *quant_matrix= s->inter_matrix;
1589
    const int qscale= s->qscale;
1590
1591
    {
1592
        OPEN_READER(re, &s->gb);
1593
        i = -1;
1594
        /* special case for the first coef. no need to add a second vlc table */
1456
        /* special case for the first coef. no need to add a second vlc table */
1595
        UPDATE_CACHE(re, &s->gb);
1457
        UPDATE_CACHE(re, &s->gb);
1596
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1458
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1597
            level= (3*qscale*quant_matrix[0])>>5;
1459
            level= (3*qscale*quant_matrix[0])>>5;
1598
            level= (level-1)|1;
1599
            if(GET_CACHE(re, &s->gb)&0x40000000)
1460
            if(GET_CACHE(re, &s->gb)&0x40000000)
1600
                level= -level;
1461
                level= -level;
1601
            block[0] = level;
1462
            block[0] = level;
1463
            mismatch ^= level;
1602
            i++;
1464
            i++;
1603
            SKIP_BITS(re, &s->gb, 2);
1465
            SKIP_BITS(re, &s->gb, 2);
1604
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1466
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
Lines 1613-1648 Link Here
1613
                i += run;
1475
                i += run;
1614
                j = scantable[i];
1476
                j = scantable[i];
1615
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1477
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1616
                level= (level-1)|1;
1617
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1478
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1618
                SKIP_BITS(re, &s->gb, 1);
1479
                SKIP_BITS(re, &s->gb, 1);
1619
            } else {
1480
            } else {
1620
                /* escape */
1481
                /* escape */
1621
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1482
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1622
                UPDATE_CACHE(re, &s->gb);
1483
                UPDATE_CACHE(re, &s->gb);
1623
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1484
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1624
                if (level == -128) {
1485
1625
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1626
                } else if (level == 0) {
1627
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
1628
                }
1629
                i += run;
1486
                i += run;
1630
                j = scantable[i];
1487
                j = scantable[i];
1631
                if(level<0){
1488
                if(level<0){
1632
                    level= -level;
1489
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1633
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1634
                    level= (level-1)|1;
1635
                    level= -level;
1490
                    level= -level;
1636
                }else{
1491
                }else{
1637
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1492
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1638
                    level= (level-1)|1;
1639
                }
1493
                }
1640
            }
1494
            }
1641
            if (i > 63){
1495
            if (i > 63){
1642
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1496
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1643
                return -1;
1497
                return -1;
1644
            }
1498
            }
1645
1499
            
1500
            mismatch ^= level;
1646
            block[j] = level;
1501
            block[j] = level;
1647
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1502
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1648
                break;
1503
                break;
Lines 1652-2025 Link Here
1652
        LAST_SKIP_BITS(re, &s->gb, 2);
1507
        LAST_SKIP_BITS(re, &s->gb, 2);
1653
        CLOSE_READER(re, &s->gb);
1508
        CLOSE_READER(re, &s->gb);
1654
    }
1509
    }
1510
    block[63] ^= (mismatch & 1);
1511
    
1655
    s->block_last_index[n] = i;
1512
    s->block_last_index[n] = i;
1656
    return 0;
1513
    return 0;
1657
}
1514
}
1658
1515
1659
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
1516
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, 
1517
                               DCTELEM *block, 
1518
                               int n)
1660
{
1519
{
1661
    int level, i, j, run;
1520
    int level, i, j, run;
1662
    RLTable *rl = &rl_mpeg1;
1521
    RLTable *rl = &rl_mpeg1;
1663
    uint8_t * const scantable= s->intra_scantable.permutated;
1522
    uint8_t * const scantable= s->intra_scantable.permutated;
1664
    const int qscale= s->qscale;
1523
    const int qscale= s->qscale;
1524
    OPEN_READER(re, &s->gb);
1525
    i = -1;
1665
1526
1666
    {
1527
    /* special case for the first coef. no need to add a second vlc table */
1667
        OPEN_READER(re, &s->gb);
1528
    UPDATE_CACHE(re, &s->gb);
1668
        i = -1;
1529
    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1669
        /* special case for the first coef. no need to add a second vlc table */
1530
        level= (3*qscale)>>1;
1670
        UPDATE_CACHE(re, &s->gb);
1531
        if(GET_CACHE(re, &s->gb)&0x40000000)
1671
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1532
            level= -level;
1672
            level= (3*qscale)>>1;
1533
        block[0] = level;
1673
            level= (level-1)|1;
1534
        i++;
1674
            if(GET_CACHE(re, &s->gb)&0x40000000)
1535
        SKIP_BITS(re, &s->gb, 2);
1675
                level= -level;
1536
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1676
            block[0] = level;
1537
            goto end;
1677
            i++;
1538
    }
1678
            SKIP_BITS(re, &s->gb, 2);
1679
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1680
                goto end;
1681
        }
1682
1539
1683
        /* now quantify & encode AC coefs */
1540
    /* now quantify & encode AC coefs */
1684
        for(;;) {
1541
    for(;;) {
1685
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1542
        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1686
            
1543
        
1687
            if(level != 0) {
1544
        if(level != 0) {
1688
                i += run;
1545
            i += run;
1689
                j = scantable[i];
1546
            j = scantable[i];
1547
            level= ((level*2+1)*qscale)>>1;
1548
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1549
            SKIP_BITS(re, &s->gb, 1);
1550
        } else {
1551
            /* escape */
1552
            run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1553
            UPDATE_CACHE(re, &s->gb);
1554
            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1555
1556
            i += run;
1557
            j = scantable[i];
1558
            if(level<0){
1559
                level= ((-level*2+1)*qscale)>>1;
1560
                level= -level;
1561
            }else{
1690
                level= ((level*2+1)*qscale)>>1;
1562
                level= ((level*2+1)*qscale)>>1;
1691
                level= (level-1)|1;
1692
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1693
                SKIP_BITS(re, &s->gb, 1);
1694
            } else {
1695
                /* escape */
1696
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1697
                UPDATE_CACHE(re, &s->gb);
1698
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1699
                if (level == -128) {
1700
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
1701
                } else if (level == 0) {
1702
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
1703
                }
1704
                i += run;
1705
                j = scantable[i];
1706
                if(level<0){
1707
                    level= -level;
1708
                    level= ((level*2+1)*qscale)>>1;
1709
                    level= (level-1)|1;
1710
                    level= -level;
1711
                }else{
1712
                    level= ((level*2+1)*qscale)>>1;
1713
                    level= (level-1)|1;
1714
                }
1715
            }
1563
            }
1716
1717
            block[j] = level;
1718
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1719
                break;
1720
            UPDATE_CACHE(re, &s->gb);
1721
        }
1564
        }
1722
end:
1565
        
1723
        LAST_SKIP_BITS(re, &s->gb, 2);
1566
        block[j] = level;
1724
        CLOSE_READER(re, &s->gb);
1567
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1568
            break;
1569
        UPDATE_CACHE(re, &s->gb);
1725
    }
1570
    }
1571
end:
1572
    LAST_SKIP_BITS(re, &s->gb, 2);    
1573
    CLOSE_READER(re, &s->gb);
1726
    s->block_last_index[n] = i;
1574
    s->block_last_index[n] = i;
1727
    return 0;
1575
    return 0;
1728
}
1576
}
1729
1577
1578
static inline int get_qscale(MpegEncContext *s)
1579
{
1580
    int qscale = get_bits(&s->gb, 5);
1581
    if (s->q_scale_type) {
1582
        return non_linear_qscale[qscale];
1583
    } else {
1584
        return qscale << 1;
1585
    }
1586
}
1730
1587
1731
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
1588
/* motion type (for mpeg2) */
1732
                               DCTELEM *block, 
1589
#define MT_FIELD 1
1733
                               int n)
1590
#define MT_FRAME 2
1591
#define MT_16X8  2
1592
#define MT_DMV   3
1593
1594
static int mpeg_decode_mb(MpegEncContext *s,
1595
                          DCTELEM block[12][64])
1734
{
1596
{
1735
    int level, i, j, run;
1597
    int i, j, k, cbp, val, mb_type, motion_type;
1736
    RLTable *rl = &rl_mpeg1;
1598
    const int mb_block_count = 4 + (1<< s->chroma_format);
1737
    uint8_t * const scantable= s->intra_scantable.permutated;
1738
    const uint16_t *quant_matrix;
1739
    const int qscale= s->qscale;
1740
    int mismatch;
1741
1599
1742
    mismatch = 1;
1600
    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1743
1601
1744
    {
1602
    assert(s->mb_skipped==0);
1745
        OPEN_READER(re, &s->gb);
1603
1746
        i = -1;
1604
    if (s->mb_skip_run-- != 0) {
1747
        if (n < 4)
1605
        if(s->pict_type == I_TYPE){
1748
            quant_matrix = s->inter_matrix;
1606
            av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1607
            return -1;
1608
        }
1609
    
1610
        /* skip mb */
1611
        s->mb_intra = 0;
1612
        for(i=0;i<12;i++)
1613
            s->block_last_index[i] = -1;
1614
        if(s->picture_structure == PICT_FRAME)
1615
            s->mv_type = MV_TYPE_16X16;
1749
        else
1616
        else
1750
            quant_matrix = s->chroma_inter_matrix;
1617
            s->mv_type = MV_TYPE_FIELD;
1618
        if (s->pict_type == P_TYPE) {
1619
            /* if P type, zero motion vector is implied */
1620
            s->mv_dir = MV_DIR_FORWARD;
1621
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
1622
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1623
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1624
            s->field_select[0][0]= s->picture_structure - 1;
1625
            s->mb_skipped = 1;
1626
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1627
        } else {
1628
            int mb_type;
1629
            
1630
            if(s->mb_x)
1631
                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
1632
            else
1633
                mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all, 
1634
            if(IS_INTRA(mb_type))
1635
                return -1;
1636
            
1637
            /* if B type, reuse previous vectors and directions */
1638
            s->mv[0][0][0] = s->last_mv[0][0][0];
1639
            s->mv[0][0][1] = s->last_mv[0][0][1];
1640
            s->mv[1][0][0] = s->last_mv[1][0][0];
1641
            s->mv[1][0][1] = s->last_mv[1][0][1];
1751
1642
1752
        /* special case for the first coef. no need to add a second vlc table */
1643
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= 
1753
        UPDATE_CACHE(re, &s->gb);
1644
                mb_type | MB_TYPE_SKIP;
1754
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1645
//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1755
            level= (3*qscale*quant_matrix[0])>>5;
1646
1756
            if(GET_CACHE(re, &s->gb)&0x40000000)
1647
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) 
1757
                level= -level;
1648
                s->mb_skipped = 1;
1758
            block[0] = level;
1759
            mismatch ^= level;
1760
            i++;
1761
            SKIP_BITS(re, &s->gb, 2);
1762
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1763
                goto end;
1764
        }
1649
        }
1765
1650
1766
        /* now quantify & encode AC coefs */
1651
        return 0;
1767
        for(;;) {
1652
    }
1768
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1769
            
1770
            if(level != 0) {
1771
                i += run;
1772
                j = scantable[i];
1773
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1774
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1775
                SKIP_BITS(re, &s->gb, 1);
1776
            } else {
1777
                /* escape */
1778
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1779
                UPDATE_CACHE(re, &s->gb);
1780
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1781
1653
1782
                i += run;
1654
    switch(s->pict_type) {
1783
                j = scantable[i];
1655
    default:
1784
                if(level<0){
1656
    case I_TYPE:
1785
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1657
        if (get_bits1(&s->gb) == 0) {
1786
                    level= -level;
1658
            if (get_bits1(&s->gb) == 0){
1787
                }else{
1659
                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
1788
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1789
                }
1790
            }
1791
            if (i > 63){
1792
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1793
                return -1;
1660
                return -1;
1794
            }
1661
            }
1795
            
1662
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
1796
            mismatch ^= level;
1663
        } else {
1797
            block[j] = level;
1664
            mb_type = MB_TYPE_INTRA;
1798
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1799
                break;
1800
            UPDATE_CACHE(re, &s->gb);
1801
        }
1665
        }
1802
end:
1666
        break;
1803
        LAST_SKIP_BITS(re, &s->gb, 2);
1667
    case P_TYPE:
1804
        CLOSE_READER(re, &s->gb);
1668
        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
1669
        if (mb_type < 0){
1670
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
1671
            return -1;
1672
        }
1673
        mb_type = ptype2mb_type[ mb_type ];
1674
        break;
1675
    case B_TYPE:
1676
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
1677
        if (mb_type < 0){
1678
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
1679
            return -1;
1680
        }
1681
        mb_type = btype2mb_type[ mb_type ];
1682
        break;
1805
    }
1683
    }
1806
    block[63] ^= (mismatch & 1);
1684
    dprintf("mb_type=%x\n", mb_type);
1685
//    motion_type = 0; /* avoid warning */
1686
    if (IS_INTRA(mb_type)) {
1687
        s->dsp.clear_blocks(s->block[0]);
1807
    
1688
    
1808
    s->block_last_index[n] = i;
1689
        if(!s->chroma_y_shift){
1809
    return 0;
1690
            s->dsp.clear_blocks(s->block[6]);
1810
}
1691
        }
1692
    
1693
        /* compute dct type */
1694
        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1695
            !s->frame_pred_frame_dct) {
1696
            s->interlaced_dct = get_bits1(&s->gb);
1697
        }
1811
1698
1812
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, 
1699
        if (IS_QUANT(mb_type))
1813
                               DCTELEM *block, 
1700
            s->qscale = get_qscale(s);
1814
                               int n)
1701
        
1815
{
1702
        if (s->concealment_motion_vectors) {
1816
    int level, i, j, run;
1703
            /* just parse them */
1817
    RLTable *rl = &rl_mpeg1;
1704
            if (s->picture_structure != PICT_FRAME) 
1818
    uint8_t * const scantable= s->intra_scantable.permutated;
1705
                skip_bits1(&s->gb); /* field select */
1819
    const int qscale= s->qscale;
1706
            
1820
    OPEN_READER(re, &s->gb);
1707
            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] = 
1821
    i = -1;
1708
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
1709
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] = 
1710
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
1822
1711
1823
    /* special case for the first coef. no need to add a second vlc table */
1712
            skip_bits1(&s->gb); /* marker */
1824
    UPDATE_CACHE(re, &s->gb);
1713
        }else
1825
    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
1714
            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
1826
        level= (3*qscale)>>1;
1715
        s->mb_intra = 1;
1827
        if(GET_CACHE(re, &s->gb)&0x40000000)
1716
#ifdef HAVE_XVMC
1828
            level= -level;
1717
        //one 1 we memcpy blocks in xvmcvideo
1829
        block[0] = level;
1718
        if(s->avctx->xvmc_acceleration > 1){
1830
        i++;
1719
            XVMC_pack_pblocks(s,-1);//inter are always full blocks
1831
        SKIP_BITS(re, &s->gb, 2);
1720
            if(s->swap_uv){
1832
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1721
                exchange_uv(s);
1833
            goto end;
1722
            }
1834
    }
1723
        }
1724
#endif
1835
1725
1836
    /* now quantify & encode AC coefs */
1726
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1837
    for(;;) {
1727
            if(s->flags2 & CODEC_FLAG2_FAST){
1838
        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1728
                for(i=0;i<6;i++) {
1839
        
1729
                    mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
1840
        if(level != 0) {
1730
                }
1841
            i += run;
1731
            }else{
1842
            j = scantable[i];
1732
                for(i=0;i<mb_block_count;i++) {
1843
            level= ((level*2+1)*qscale)>>1;
1733
                    if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
1844
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1734
                        return -1;
1845
            SKIP_BITS(re, &s->gb, 1);
1735
                }
1736
            }
1846
        } else {
1737
        } else {
1847
            /* escape */
1738
            for(i=0;i<6;i++) {
1848
            run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1739
                if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
1849
            UPDATE_CACHE(re, &s->gb);
1740
                    return -1;
1850
            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1741
            }
1742
        }
1743
    } else {
1744
        if (mb_type & MB_TYPE_ZERO_MV){
1745
            assert(mb_type & MB_TYPE_CBP);
1851
1746
1852
            i += run;
1747
            /* compute dct type */
1853
            j = scantable[i];
1748
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1854
            if(level<0){
1749
                !s->frame_pred_frame_dct) {
1855
                level= ((-level*2+1)*qscale)>>1;
1750
                s->interlaced_dct = get_bits1(&s->gb);
1856
                level= -level;
1751
            }
1857
            }else{
1752
1858
                level= ((level*2+1)*qscale)>>1;
1753
            if (IS_QUANT(mb_type))
1754
                s->qscale = get_qscale(s);
1755
1756
            s->mv_dir = MV_DIR_FORWARD;
1757
            if(s->picture_structure == PICT_FRAME)
1758
                s->mv_type = MV_TYPE_16X16;
1759
            else{
1760
                s->mv_type = MV_TYPE_FIELD;
1761
                mb_type |= MB_TYPE_INTERLACED;
1762
                s->field_select[0][0]= s->picture_structure - 1;
1763
            }
1764
            s->last_mv[0][0][0] = 0;
1765
            s->last_mv[0][0][1] = 0;
1766
            s->last_mv[0][1][0] = 0;
1767
            s->last_mv[0][1][1] = 0;
1768
            s->mv[0][0][0] = 0;
1769
            s->mv[0][0][1] = 0;
1770
        }else{
1771
            assert(mb_type & MB_TYPE_L0L1);
1772
//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1773
            /* get additionnal motion vector type */
1774
            if (s->frame_pred_frame_dct) 
1775
                motion_type = MT_FRAME;
1776
            else{
1777
                motion_type = get_bits(&s->gb, 2);
1778
            }
1779
1780
            /* compute dct type */
1781
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1782
                !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
1783
                s->interlaced_dct = get_bits1(&s->gb);
1784
            }
1785
1786
            if (IS_QUANT(mb_type))
1787
                s->qscale = get_qscale(s);
1788
1789
            /* motion vectors */
1790
            s->mv_dir = 0;
1791
            for(i=0;i<2;i++) {
1792
                if (USES_LIST(mb_type, i)) {
1793
                    s->mv_dir |= (MV_DIR_FORWARD >> i);
1794
                    dprintf("motion_type=%d\n", motion_type);
1795
                    switch(motion_type) {
1796
                    case MT_FRAME: /* or MT_16X8 */
1797
                        if (s->picture_structure == PICT_FRAME) {
1798
                            /* MT_FRAME */
1799
                            mb_type |= MB_TYPE_16x16; 
1800
                            s->mv_type = MV_TYPE_16X16;
1801
                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] = 
1802
                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1803
                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] = 
1804
                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
1805
                            /* full_pel: only for mpeg1 */
1806
                            if (s->full_pel[i]){
1807
                                s->mv[i][0][0] <<= 1;
1808
                                s->mv[i][0][1] <<= 1;
1809
                            }
1810
                        } else {
1811
                            /* MT_16X8 */
1812
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
1813
                            s->mv_type = MV_TYPE_16X8;
1814
                            for(j=0;j<2;j++) {
1815
                                s->field_select[i][j] = get_bits1(&s->gb);
1816
                                for(k=0;k<2;k++) {
1817
                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1818
                                                             s->last_mv[i][j][k]);
1819
                                    s->last_mv[i][j][k] = val;
1820
                                    s->mv[i][j][k] = val;
1821
                                }
1822
                            }
1823
                        }
1824
                        break;
1825
                    case MT_FIELD:
1826
                        s->mv_type = MV_TYPE_FIELD;
1827
                        if (s->picture_structure == PICT_FRAME) {
1828
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
1829
                            for(j=0;j<2;j++) {
1830
                                s->field_select[i][j] = get_bits1(&s->gb);
1831
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1832
                                                         s->last_mv[i][j][0]);
1833
                                s->last_mv[i][j][0] = val;
1834
                                s->mv[i][j][0] = val;
1835
                                dprintf("fmx=%d\n", val);
1836
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1837
                                                         s->last_mv[i][j][1] >> 1);
1838
                                s->last_mv[i][j][1] = val << 1;
1839
                                s->mv[i][j][1] = val;
1840
                                dprintf("fmy=%d\n", val);
1841
                            }
1842
                        } else {
1843
                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
1844
                            s->field_select[i][0] = get_bits1(&s->gb);
1845
                            for(k=0;k<2;k++) {
1846
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1847
                                                         s->last_mv[i][0][k]);
1848
                                s->last_mv[i][0][k] = val;
1849
                                s->last_mv[i][1][k] = val;
1850
                                s->mv[i][0][k] = val;
1851
                            }
1852
                        }
1853
                        break;
1854
                    case MT_DMV:
1855
                        {
1856
                            int dmx, dmy, mx, my, m;
1857
1858
                            mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
1859
                                                    s->last_mv[i][0][0]);
1860
                            s->last_mv[i][0][0] = mx;
1861
                            s->last_mv[i][1][0] = mx;
1862
                            dmx = get_dmv(s);
1863
                            my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
1864
                                                    s->last_mv[i][0][1] >> 1);
1865
                            dmy = get_dmv(s);
1866
                            s->mv_type = MV_TYPE_DMV;
1867
1868
1869
                            s->last_mv[i][0][1] = my<<1;
1870
                            s->last_mv[i][1][1] = my<<1;
1871
1872
                            s->mv[i][0][0] = mx;
1873
                            s->mv[i][0][1] = my;
1874
                            s->mv[i][1][0] = mx;//not used
1875
                            s->mv[i][1][1] = my;//not used
1876
1877
                            if (s->picture_structure == PICT_FRAME) {
1878
                                mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
1879
1880
                                //m = 1 + 2 * s->top_field_first;
1881
                                m = s->top_field_first ? 1 : 3;
1882
1883
                                /* top -> top pred */
1884
                                s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1885
                                s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1886
                                m = 4 - m;
1887
                                s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1888
                                s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1889
                            } else {
1890
                                mb_type |= MB_TYPE_16x16;
1891
1892
                                s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1893
                                s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1894
                                if(s->picture_structure == PICT_TOP_FIELD)
1895
                                    s->mv[i][2][1]--;
1896
                                else 
1897
                                    s->mv[i][2][1]++;
1898
                            }
1899
                        }
1900
                        break;
1901
                    default:
1902
                        av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1903
                        return -1;
1904
                    }
1905
                }
1859
            }
1906
            }
1860
        }
1907
        }
1861
        
1908
        
1862
        block[j] = level;
1909
        s->mb_intra = 0;
1863
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1910
        if (HAS_CBP(mb_type)) {
1864
            break;
1911
            s->dsp.clear_blocks(s->block[0]);
1865
        UPDATE_CACHE(re, &s->gb);
1912
        
1866
    }
1913
            if(!s->chroma_y_shift){
1867
end:
1914
                s->dsp.clear_blocks(s->block[6]);
1868
    LAST_SKIP_BITS(re, &s->gb, 2);    
1915
            }
1869
    CLOSE_READER(re, &s->gb);
1870
    s->block_last_index[n] = i;
1871
    return 0;
1872
}
1873
1874
1916
1875
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
1917
            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1876
                               DCTELEM *block, 
1918
            if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
1877
                               int n)
1919
                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1878
{
1920
                return -1;
1879
    int level, dc, diff, i, j, run;
1921
            }
1880
    int component;
1922
            if(mb_block_count > 6){
1881
    RLTable *rl;
1923
	         cbp<<= mb_block_count-6;
1882
    uint8_t * const scantable= s->intra_scantable.permutated;
1924
		 cbp |= get_bits(&s->gb, mb_block_count-6);
1883
    const uint16_t *quant_matrix;
1925
            }
1884
    const int qscale= s->qscale;
1885
    int mismatch;
1886
1926
1887
    /* DC coef */
1927
#ifdef HAVE_XVMC
1888
    if (n < 4){
1928
            //on 1 we memcpy blocks in xvmcvideo
1889
        quant_matrix = s->intra_matrix;
1929
            if(s->avctx->xvmc_acceleration > 1){
1890
        component = 0; 
1930
                XVMC_pack_pblocks(s,cbp);
1891
    }else{
1931
                if(s->swap_uv){
1892
        quant_matrix = s->chroma_intra_matrix;
1932
                    exchange_uv(s);
1893
        component = (n&1) + 1;
1933
                }
1894
    }
1934
            }    
1895
    diff = decode_dc(&s->gb, component);
1935
#endif
1896
    if (diff >= 0xffff)
1897
        return -1;
1898
    dc = s->last_dc[component];
1899
    dc += diff;
1900
    s->last_dc[component] = dc;
1901
    block[0] = dc << (3 - s->intra_dc_precision);
1902
    dprintf("dc=%d\n", block[0]);
1903
    mismatch = block[0] ^ 1;
1904
    i = 0;
1905
    if (s->intra_vlc_format)
1906
        rl = &rl_mpeg2;
1907
    else
1908
        rl = &rl_mpeg1;
1909
1936
1910
    {
1937
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1911
        OPEN_READER(re, &s->gb);    
1938
                if(s->flags2 & CODEC_FLAG2_FAST){
1912
        /* now quantify & encode AC coefs */
1939
                    for(i=0;i<6;i++) {
1913
        for(;;) {
1940
                        if(cbp & 32) {
1914
            UPDATE_CACHE(re, &s->gb);
1941
                            mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
1915
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1942
                        } else {
1916
            
1943
                            s->block_last_index[i] = -1;
1917
            if(level == 127){
1944
                        }
1918
                break;
1945
                        cbp+=cbp;
1919
            } else if(level != 0) {
1946
                    }
1920
                i += run;
1947
                }else{
1921
                j = scantable[i];
1948
                    cbp<<= 12-mb_block_count;
1922
                level= (level*qscale*quant_matrix[j])>>4;
1949
    
1923
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1950
                    for(i=0;i<mb_block_count;i++) {
1924
                LAST_SKIP_BITS(re, &s->gb, 1);
1951
                        if ( cbp & (1<<11) ) {
1952
                            if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1953
                                return -1;
1954
                        } else {
1955
                            s->block_last_index[i] = -1;
1956
                        }
1957
                        cbp+=cbp;
1958
                    }
1959
                }
1925
            } else {
1960
            } else {
1926
                /* escape */
1961
                if(s->flags2 & CODEC_FLAG2_FAST){
1927
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1962
                    for(i=0;i<6;i++) {
1928
                UPDATE_CACHE(re, &s->gb);
1963
                        if (cbp & 32) {
1929
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1964
                            mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
1930
                i += run;
1965
                        } else {
1931
                j = scantable[i];
1966
                            s->block_last_index[i] = -1;
1932
                if(level<0){
1967
                        }
1933
                    level= (-level*qscale*quant_matrix[j])>>4;
1968
                        cbp+=cbp;
1934
                    level= -level;
1969
                    }
1935
                }else{
1970
                }else{
1936
                    level= (level*qscale*quant_matrix[j])>>4;
1971
                    for(i=0;i<6;i++) {
1972
                        if (cbp & 32) {
1973
                            if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
1974
                                return -1;
1975
                        } else {
1976
                            s->block_last_index[i] = -1;
1977
                        }
1978
                        cbp+=cbp;
1979
                    }
1937
                }
1980
                }
1938
            }
1981
            }
1939
            if (i > 63){
1982
        }else{
1940
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1983
            for(i=0;i<6;i++)
1941
                return -1;
1984
                s->block_last_index[i] = -1;
1942
            }
1943
            
1944
            mismatch^= level;
1945
            block[j] = level;
1946
        }
1985
        }
1947
        CLOSE_READER(re, &s->gb);
1948
    }
1986
    }
1949
    block[63]^= mismatch&1;
1987
1950
    
1988
    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1951
    s->block_last_index[n] = i;
1989
1952
    return 0;
1990
    return 0;
1953
}
1991
}
1954
1992
1955
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, 
1993
/* as h263, but only 17 codes */
1956
                               DCTELEM *block, 
1994
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1957
                               int n)
1958
{
1995
{
1959
    int level, dc, diff, j, run;
1996
    int code, sign, val, l, shift;
1960
    int component;
1961
    RLTable *rl;
1962
    uint8_t * scantable= s->intra_scantable.permutated;
1963
    const uint16_t *quant_matrix;
1964
    const int qscale= s->qscale;
1965
1997
1966
    /* DC coef */
1998
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1967
    if (n < 4){
1999
    if (code == 0) {
1968
        quant_matrix = s->intra_matrix;
2000
        return pred;
1969
        component = 0; 
2001
    }
1970
    }else{
2002
    if (code < 0) {
1971
        quant_matrix = s->chroma_intra_matrix;
2003
        return 0xffff;
1972
        component = (n&1) + 1;
1973
    }
2004
    }
1974
    diff = decode_dc(&s->gb, component);
1975
    if (diff >= 0xffff)
1976
        return -1;
1977
    dc = s->last_dc[component];
1978
    dc += diff;
1979
    s->last_dc[component] = dc;
1980
    block[0] = dc << (3 - s->intra_dc_precision);
1981
    if (s->intra_vlc_format)
1982
        rl = &rl_mpeg2;
1983
    else
1984
        rl = &rl_mpeg1;
1985
2005
1986
    {
2006
    sign = get_bits1(&s->gb);
1987
        OPEN_READER(re, &s->gb);    
2007
    shift = fcode - 1;
1988
        /* now quantify & encode AC coefs */
2008
    val = code;
1989
        for(;;) {
2009
    if (shift) {
1990
            UPDATE_CACHE(re, &s->gb);
2010
        val = (val - 1) << shift;
1991
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
2011
        val |= get_bits(&s->gb, shift);
1992
            
2012
        val++;
1993
            if(level == 127){
1994
                break;
1995
            } else if(level != 0) {
1996
                scantable += run;
1997
                j = *scantable;
1998
                level= (level*qscale*quant_matrix[j])>>4;
1999
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
2000
                LAST_SKIP_BITS(re, &s->gb, 1);
2001
            } else {
2002
                /* escape */
2003
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
2004
                UPDATE_CACHE(re, &s->gb);
2005
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
2006
                scantable += run;
2007
                j = *scantable;
2008
                if(level<0){
2009
                    level= (-level*qscale*quant_matrix[j])>>4;
2010
                    level= -level;
2011
                }else{
2012
                    level= (level*qscale*quant_matrix[j])>>4;
2013
                }
2014
            }
2015
            
2016
            block[j] = level;
2017
        }
2018
        CLOSE_READER(re, &s->gb);
2019
    }
2013
    }
2014
    if (sign)
2015
        val = -val;
2016
    val += pred;
2020
    
2017
    
2021
    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
2018
    /* modulo decoding */
2022
    return 0;
2019
    l= INT_BIT - 5 - shift;
2020
    val = (val<<l)>>l;
2021
    return val;
2023
}
2022
}
2024
2023
2025
typedef struct Mpeg1Context {
2024
typedef struct Mpeg1Context {

Return to bug 119014