Revision 3729c912

View differences:

libavcodec/mpeg12.c
58 58
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
59 59
                              DCTELEM *block, 
60 60
                              int n);
61
static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
61
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
62 62
                                        DCTELEM *block, 
63 63
                                        int n);
64
static int mpeg2_decode_block_intra(MpegEncContext *s, 
64
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
65 65
                                    DCTELEM *block, 
66 66
                                    int n);
67 67
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
......
1117 1117
        UPDATE_CACHE(re, &s->gb);
1118 1118
        v= SHOW_UBITS(re, &s->gb, 2);
1119 1119
        if (v & 2) {
1120
            SKIP_BITS(re, &s->gb, 2);
1120
            LAST_SKIP_BITS(re, &s->gb, 2);
1121 1121
            level= (3*qscale*quant_matrix[0])>>4;
1122 1122
            level= (level-1)|1;
1123 1123
            if(v&1)
1124 1124
                level= -level;
1125
            block[ scantable[0] ] = level;
1125
            block[0] = level;
1126 1126
            i++;
1127 1127
        }
1128
        CLOSE_READER(re, &s->gb);
1129
    }
1130
    {
1131
        OPEN_READER(re, &s->gb);    
1128

  
1132 1129
        /* now quantify & encode AC coefs */
1133 1130
        for(;;) {
1134 1131
            UPDATE_CACHE(re, &s->gb);
......
1180 1177

  
1181 1178
/* Also does unquantization here, since I will never support mpeg2
1182 1179
   encoding */
1183
static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
1184
                                        DCTELEM *block, 
1185
                                        int n)
1180
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
1181
                               DCTELEM *block, 
1182
                               int n)
1186 1183
{
1187 1184
    int level, i, j, run;
1188
    int code;
1189 1185
    RLTable *rl = &rl_mpeg1;
1190
    const UINT8 *scan_table;
1191
    const UINT16 *matrix;
1186
    UINT8 * scantable;
1187
    const UINT16 *quant_matrix;
1188
    const int qscale= s->qscale;
1192 1189
    int mismatch;
1193 1190

  
1194 1191
    if (s->alternate_scan)
1195
        scan_table = s->intra_v_scantable.permutated;
1192
        scantable = s->intra_v_scantable.permutated;
1196 1193
    else
1197
        scan_table = s->intra_scantable.permutated;
1194
        scantable = s->intra_scantable.permutated;
1198 1195
    mismatch = 1;
1199 1196

  
1200 1197
    {
1201 1198
        int v;
1202 1199
        OPEN_READER(re, &s->gb);
1203
        i = 0;
1200
        i = -1;
1204 1201
        if (n < 4)
1205
            matrix = s->inter_matrix;
1202
            quant_matrix = s->inter_matrix;
1206 1203
        else
1207
            matrix = s->chroma_inter_matrix;
1204
            quant_matrix = s->chroma_inter_matrix;
1208 1205

  
1209 1206
        /* special case for the first coef. no need to add a second vlc table */
1210 1207
        UPDATE_CACHE(re, &s->gb);
1211 1208
        v= SHOW_UBITS(re, &s->gb, 2);
1212 1209
        if (v & 2) {
1213
            run = 0;
1214
            level = 5 - (v << 1);
1215
            SKIP_BITS(re, &s->gb, 2);
1216
            CLOSE_READER(re, &s->gb);
1217
            goto add_coef;
1210
            LAST_SKIP_BITS(re, &s->gb, 2);
1211
            level= (3*qscale*quant_matrix[0])>>5;
1212
            if(v&1)
1213
                level= -level;
1214
            block[0] = level;
1215
            mismatch ^= level;
1216
            i++;
1218 1217
        }
1219
        CLOSE_READER(re, &s->gb);
1220
    }
1221 1218

  
1222
    /* now quantify & encode AC coefs */
1223
    for(;;) {
1224
        code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
1225
        if (code < 0){
1226
            fprintf(stderr, "invalid ac code at %d %d\n", s->mb_x, s->mb_y);
1227
            return -1;
1228
        }
1229
        if (code == 112) {
1230
            break;
1231
        } else if (code == 111) {
1232
            /* escape */
1233
            run = get_bits(&s->gb, 6);
1234
            level = get_bits(&s->gb, 12);
1235
            level= (level + ((-1)<<11)) ^ ((-1)<<11); //sign extension
1236
        } else {
1237
            run = rl->table_run[code];
1238
            level = rl->table_level[code];
1239
            if (get_bits1(&s->gb))
1240
                level = -level;
1241
        }
1242
        i += run;
1243
        if (i >= 64){
1244
            fprintf(stderr, "run too long at %d %d\n", s->mb_x, s->mb_y);
1245
            return -1;
1246
        }
1247
    add_coef:
1248
	j = scan_table[i];
1249
        dprintf("%d: run=%d level=%d\n", n, run, level);
1250
        /* XXX: optimize */
1251
        if (level > 0) {
1252
            level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1253
        } else {
1254
            level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1255
            level = -level;
1219
        /* now quantify & encode AC coefs */
1220
        for(;;) {
1221
            UPDATE_CACHE(re, &s->gb);
1222
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1223
            
1224
            if(level == 127){
1225
                break;
1226
            } else if(level != 0) {
1227
                i += run;
1228
                j = scantable[i];
1229
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1230
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1231
                LAST_SKIP_BITS(re, &s->gb, 1);
1232
            } else {
1233
                /* escape */
1234
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1235
                UPDATE_CACHE(re, &s->gb);
1236
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1237

  
1238
                i += run;
1239
                j = scantable[i];
1240
                if(level<0){
1241
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1242
                    level= -level;
1243
                }else{
1244
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1245
                }
1246
            }
1247
            if (i > 63){
1248
                fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1249
                return -1;
1250
            }
1251
            
1252
            mismatch ^= level;
1253
            block[j] = level;
1256 1254
        }
1257
        /* XXX: is it really necessary to saturate since the encoder
1258
           knows whats going on ? */
1259
        mismatch ^= level;
1260
        block[j] = level;
1261
        i++;
1255
        CLOSE_READER(re, &s->gb);
1262 1256
    }
1263 1257
    block[63] ^= (mismatch & 1);
1258
    
1264 1259
    s->block_last_index[n] = i;
1265

  
1266 1260
    return 0;
1267 1261
}
1268 1262

  
1269
static int mpeg2_decode_block_intra(MpegEncContext *s, 
1270
                                    DCTELEM *block, 
1271
                                    int n)
1263
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
1264
                               DCTELEM *block, 
1265
                               int n)
1272 1266
{
1273 1267
    int level, dc, diff, i, j, run;
1274
    int code, component;
1268
    int component;
1275 1269
    RLTable *rl;
1276
    const UINT8 *scan_table;
1277
    const UINT16 *matrix;
1270
    UINT8 * scantable;
1271
    const UINT16 *quant_matrix;
1272
    const int qscale= s->qscale;
1278 1273
    int mismatch;
1279 1274

  
1280 1275
    if (s->alternate_scan)
1281
        scan_table = s->intra_v_scantable.permutated;
1276
        scantable = s->intra_v_scantable.permutated;
1282 1277
    else
1283
        scan_table = s->intra_scantable.permutated;
1278
        scantable = s->intra_scantable.permutated;
1284 1279

  
1285 1280
    /* DC coef */
1286
    component = (n <= 3 ? 0 : n - 4 + 1);
1281
    if (n < 4){
1282
        quant_matrix = s->intra_matrix;
1283
        component = 0; 
1284
    }else{
1285
        quant_matrix = s->chroma_intra_matrix;
1286
        component = n - 3;
1287
    }
1287 1288
    diff = decode_dc(s, component);
1288 1289
    if (diff >= 0xffff)
1289 1290
        return -1;
......
1293 1294
    block[0] = dc << (3 - s->intra_dc_precision);
1294 1295
    dprintf("dc=%d\n", block[0]);
1295 1296
    mismatch = block[0] ^ 1;
1296
    i = 1;
1297
    i = 0;
1297 1298
    if (s->intra_vlc_format)
1298 1299
        rl = &rl_mpeg2;
1299 1300
    else
1300 1301
        rl = &rl_mpeg1;
1301
    if (n < 4) 
1302
        matrix = s->intra_matrix;
1303
    else
1304
        matrix = s->chroma_intra_matrix;
1305 1302

  
1306
    /* now quantify & encode AC coefs */
1307
    for(;;) {
1308
        code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
1309
        if (code < 0){
1310
            fprintf(stderr, "invalid ac code at %d %d\n", s->mb_x, s->mb_y);
1311
            return -1;
1312
        }
1313
        if (code == 112) {
1314
            break;
1315
        } else if (code == 111) {
1316
            /* escape */
1317
            run = get_bits(&s->gb, 6);
1318
            level = get_bits(&s->gb, 12);
1319
            level= (level + ((-1)<<11)) ^ ((-1)<<11); //sign extension
1320
        } else {
1321
            run = rl->table_run[code];
1322
            level = rl->table_level[code];
1323
            if (get_bits1(&s->gb))
1324
                level = -level;
1325
        }
1326
        i += run;
1327
        if (i >= 64){
1328
            fprintf(stderr, "run too long at %d %d\n", s->mb_x, s->mb_y);
1329
            return -1;
1303
    {
1304
        OPEN_READER(re, &s->gb);    
1305
        /* now quantify & encode AC coefs */
1306
        for(;;) {
1307
            UPDATE_CACHE(re, &s->gb);
1308
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1309
            
1310
            if(level == 127){
1311
                break;
1312
            } else if(level != 0) {
1313
                i += run;
1314
                j = scantable[i];
1315
                level= (level*qscale*quant_matrix[j])>>4;
1316
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1317
                LAST_SKIP_BITS(re, &s->gb, 1);
1318
            } else {
1319
                /* escape */
1320
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1321
                UPDATE_CACHE(re, &s->gb);
1322
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1323
                i += run;
1324
                j = scantable[i];
1325
                if(level<0){
1326
                    level= (-level*qscale*quant_matrix[j])>>4;
1327
                    level= -level;
1328
                }else{
1329
                    level= (level*qscale*quant_matrix[j])>>4;
1330
                }
1331
            }
1332
            if (i > 63){
1333
                fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1334
                return -1;
1335
            }
1336
            
1337
            mismatch^= level;
1338
            block[j] = level;
1330 1339
        }
1331
	j = scan_table[i];
1332
        dprintf("%d: run=%d level=%d\n", n, run, level);
1333
        level = (level * s->qscale * matrix[j]) / 16;
1334
        /* XXX: is it really necessary to saturate since the encoder
1335
           knows whats going on ? */
1336
        mismatch ^= level;
1337
        block[j] = level;
1338
        i++;
1340
        CLOSE_READER(re, &s->gb);
1339 1341
    }
1340
    block[63] ^= (mismatch & 1);
1342
    block[63]^= mismatch&1;
1343
    
1341 1344
    s->block_last_index[n] = i;
1342 1345
    return 0;
1343 1346
}

Also available in: Unified diff