Revision 49092244 libavcodec/h263.c

View differences:

libavcodec/h263.c
31 31
#define RDIV(a,b) ((a) > 0 ? ((a)+((b)>>1))/(b) : ((a)-((b)>>1))/(b))
32 32
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
33 33
#define ABS(a) (((a)>=0)?(a):(-(a)))
34
#define MAX(a,b) ((a) > (b) ? (a) : (b))
35
#define MIN(a,b) ((a) < (b) ? (a) : (b))
34 36

  
35 37
static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
36 38
			      int n);
......
47 49
static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr);
48 50
static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
49 51
                              int dir);
52
static void mpeg4_decode_sprite_trajectory(MpegEncContext * s);
53

  
50 54
extern UINT32 inverse[256];
51 55

  
52 56
static UINT16 mv_penalty[MAX_FCODE][MAX_MV*2+1];
......
1262 1266
            
1263 1267
}
1264 1268

  
1269
static inline void memsetw(short *tab, int val, int n)
1270
{
1271
    int i;
1272
    for(i=0;i<n;i++)
1273
        tab[i] = val;
1274
}
1275

  
1276
static int mpeg4_resync(MpegEncContext *s)
1277
{
1278
    int state, v, bits;
1279
    int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1280
    int header_extension=0, mb_num;
1281
    int c_wrap, c_xy, l_wrap, l_xy;
1282
//printf("resync at %d %d\n", s->mb_x, s->mb_y);
1283
//printf("%X\n", show_bits(&s->gb, 24));
1284

  
1285
    if( get_bits_count(&s->gb) > s->gb.size*8-32)
1286
        return 0;
1287

  
1288
    align_get_bits(&s->gb);
1289
    state = 0xff;
1290
    for(;;) {
1291
        v = get_bits(&s->gb, 8);
1292
//printf("%X ", v);
1293
        state = ((state << 8) | v) & 0xffff;
1294
        if (state == 0) break;
1295
        if( get_bits_count(&s->gb) > s->gb.size*8-32){
1296
            printf("resync failed\n");
1297
            return -1;
1298
        }
1299
    }
1300
//printf("%X\n", show_bits(&s->gb, 24));
1301
    bits=0;
1302
    while(!get_bits1(&s->gb) && bits<30) bits++;
1303
    if(s->pict_type == P_TYPE && bits != s->f_code-1)
1304
        printf("marker does not match f_code\n");
1305
    //FIXME check bits for B-framess
1306
//printf("%X\n", show_bits(&s->gb, 24));
1307

  
1308
    if(s->shape != RECT_SHAPE){
1309
        header_extension= get_bits1(&s->gb);
1310
        //FIXME more stuff here
1311
    }
1312

  
1313
    mb_num= get_bits(&s->gb, mb_num_bits);
1314
    if(mb_num != s->mb_x + s->mb_y*s->mb_width){
1315
        printf("MB-num change not supported %d %d\n", mb_num, s->mb_x + s->mb_y*s->mb_width);
1316
//        s->mb_x= mb_num % s->mb_width;
1317
//        s->mb_y= mb_num / s->mb_width;
1318
        //FIXME many vars are wrong now
1319
    } 
1320

  
1321
    if(s->shape != BIN_ONLY_SHAPE){
1322
        s->qscale= get_bits(&s->gb, 5);
1323
        h263_dc_scale(s);
1324
    }
1325

  
1326
    if(s->shape == RECT_SHAPE){
1327
        header_extension= get_bits1(&s->gb);
1328
    }
1329
    if(header_extension){
1330
        int time_incr=0;
1331
        printf("header extension not really supported\n");
1332
        while (get_bits1(&s->gb) != 0) 
1333
            time_incr++;
1334

  
1335
        check_marker(&s->gb, "before time_increment in video packed header");
1336
        s->time_increment= get_bits(&s->gb, s->time_increment_bits);
1337
        if(s->pict_type!=B_TYPE){
1338
            s->time_base+= time_incr;
1339
            s->last_non_b_time[1]= s->last_non_b_time[0];
1340
            s->last_non_b_time[0]= s->time_base*s->time_increment_resolution + s->time_increment;
1341
        }else{
1342
            s->time= (s->last_non_b_time[1]/s->time_increment_resolution + time_incr)*s->time_increment_resolution;
1343
            s->time+= s->time_increment;
1344
        }
1345
        check_marker(&s->gb, "before vop_coding_type in video packed header");
1346
        
1347
        skip_bits(&s->gb, 2); /* vop coding type */
1348
        //FIXME not rect stuff here
1349

  
1350
        if(s->shape != BIN_ONLY_SHAPE){
1351
            skip_bits(&s->gb, 3); /* intra dc vlc threshold */
1352

  
1353
            if(s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE && s->num_sprite_warping_points){
1354
                mpeg4_decode_sprite_trajectory(s);
1355
            }
1356

  
1357
            //FIXME reduced res stuff here
1358
            
1359
            if (s->pict_type != I_TYPE) {
1360
                s->f_code = get_bits(&s->gb, 3);	/* fcode_for */
1361
                if(s->f_code==0){
1362
                    printf("Error, video packet header damaged or not MPEG4 header (f_code=0)\n");
1363
                    return -1; // makes no sense to continue, as the MV decoding will break very quickly
1364
                }
1365
            }
1366
            if (s->pict_type == B_TYPE) {
1367
                s->b_code = get_bits(&s->gb, 3);
1368
            }       
1369
        }
1370

  
1371
    }
1372
    //FIXME new-pred stuff
1373

  
1374
    l_wrap= s->block_wrap[0];
1375
    l_xy= s->mb_y*l_wrap*2;
1376
    c_wrap= s->block_wrap[4];
1377
    c_xy= s->mb_y*c_wrap;
1378

  
1379
    /* clean DC */
1380
    memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*3);
1381
    memsetw(s->dc_val[1] + c_xy, 1024, c_wrap*2);
1382
    memsetw(s->dc_val[2] + c_xy, 1024, c_wrap*2);
1383

  
1384
    /* clean AC */
1385
    memset(s->ac_val[0] + l_xy, 0, l_wrap*3*16*sizeof(INT16));
1386
    memset(s->ac_val[1] + c_xy, 0, c_wrap*2*16*sizeof(INT16));
1387
    memset(s->ac_val[2] + c_xy, 0, c_wrap*2*16*sizeof(INT16));
1388

  
1389
    /* clean MV */
1390
    memset(s->motion_val + l_xy, 0, l_wrap*3*2*sizeof(INT16));
1391
//    memset(s->motion_val, 0, 2*sizeof(INT16)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
1392
    s->resync_x_pos= s->mb_x;
1393
    s->first_slice_line=1;
1394

  
1395
    return 0;
1396
}
1397

  
1265 1398
int h263_decode_mb(MpegEncContext *s,
1266 1399
                   DCTELEM block[6][64])
1267 1400
{
......
1269 1402
    INT16 *mot_val;
1270 1403
    static INT8 quant_tab[4] = { -1, -2, 1, 2 };
1271 1404

  
1405
    if(s->resync_marker){
1406
        if(   s->resync_x_pos == s->mb_x+1
1407
           || s->resync_x_pos == s->mb_x){
1408
            /* f*ck mpeg4
1409
               this is here so we dont need to slowdown h263_pred_motion with it */
1410
            if(s->resync_x_pos == s->mb_x+1 && s->mb_x==0){
1411
                int xy= s->block_index[0] - s->block_wrap[0];
1412
                s->motion_val[xy][0]= s->motion_val[xy+2][0];
1413
                s->motion_val[xy][1]= s->motion_val[xy+2][1];
1414
            }
1415

  
1416
            s->first_slice_line=0; 
1417
            s->resync_x_pos=0; // isnt needed but for cleanness sake ;)
1418
        }
1419

  
1420
        if(show_aligned_bits(&s->gb, 1, 16) == 0){
1421
            if( mpeg4_resync(s) < 0 ) return -1;
1422
            
1423
        }
1424
    }
1425

  
1272 1426
    if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
1273 1427
        if (get_bits1(&s->gb)) {
1274 1428
            /* skip mb */
......
1322 1476
                s->qscale = 1;
1323 1477
            else if (s->qscale > 31)
1324 1478
                s->qscale = 31;
1479
            h263_dc_scale(s);
1325 1480
        }
1326 1481
        s->mv_dir = MV_DIR_FORWARD;
1327 1482
        if ((cbpc & 16) == 0) {
......
1441 1596
                        s->qscale = 1;
1442 1597
                    else if (s->qscale > 31)
1443 1598
                        s->qscale = 31;
1599
                    h263_dc_scale(s);
1444 1600
                }
1445 1601
            }
1446 1602
        }else{
......
1526 1682
                s->qscale = 1;
1527 1683
            else if (s->qscale > 31)
1528 1684
                s->qscale = 31;
1685
            h263_dc_scale(s);
1529 1686
        }
1530 1687
    }
1531 1688

  
......
2250 2407
            if (get_bits1(&s->gb) == 1) {   /* not_8_bit */
2251 2408
                s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
2252 2409
                if(get_bits(&s->gb, 4)!=8) printf("N-bit not supported\n"); /* bits_per_pixel */
2410
                if(s->quant_precision!=5) printf("quant precission %d\n", s->quant_precision);
2253 2411
            } else {
2254 2412
                s->quant_precision = 5;
2255 2413
            }
......
2261 2419
            else s->quarter_sample=0;
2262 2420

  
2263 2421
            if(!get_bits1(&s->gb)) printf("Complexity estimation not supported\n");
2264
#if 0
2265
            if(get_bits1(&s->gb)) printf("resync disable\n");
2266
#else
2267
            skip_bits1(&s->gb);   /* resync_marker_disabled */
2268
#endif
2422

  
2423
            s->resync_marker= !get_bits1(&s->gb); /* resync_marker_disabled */
2424

  
2269 2425
            s->data_partioning= get_bits1(&s->gb);
2270 2426
            if(s->data_partioning){
2271 2427
                printf("data partitioning not supported\n");
......
2387 2543
//FIXME complexity estimation stuff
2388 2544
     
2389 2545
     if (s->shape != BIN_ONLY_SHAPE) {
2390
         skip_bits(&s->gb, 3); /* intra dc VLC threshold */
2546
         int t;
2547
         t=get_bits(&s->gb, 3); /* intra dc VLC threshold */
2548
//printf("threshold %d\n", t);
2391 2549
         //FIXME interlaced specific bits
2392 2550
     }
2393 2551

  

Also available in: Unified diff