Revision 11ce8834

View differences:

libavcodec/avcodec.h
81 81
    /* video only */
82 82
    int frame_rate; /* frames per sec multiplied by FRAME_RATE_BASE */
83 83
    int width, height;
84
    int aspect_ratio_info;
84 85
    int gop_size; /* 0 = intra only */
85 86
    int pix_fmt;  /* pixel format, see PIX_FMT_xxx */
86 87

  
libavcodec/h263.c
781 781
    put_bits(&s->pb, 1, 1);		/* is obj layer id= yes */
782 782
      put_bits(&s->pb, 4, vo_ver_id);	/* is obj layer ver id */
783 783
      put_bits(&s->pb, 3, 1);		/* is obj layer priority */
784
    put_bits(&s->pb, 4, 1);		/* aspect ratio info= sqare pixel */ //FIXME real aspect
784
    if(s->aspect_ratio_info) 
785
        put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
786
    else
787
        put_bits(&s->pb, 4, 1);		/* aspect ratio info= sqare pixel */
785 788
    put_bits(&s->pb, 1, 0);		/* vol control parameters= no */
786 789
    put_bits(&s->pb, 2, RECT_SHAPE);	/* vol shape= rectangle */
787 790
    put_bits(&s->pb, 1, 1);		/* marker bit */
......
2149 2152
            skip_bits(&s->gb, 8); //par_width
2150 2153
            skip_bits(&s->gb, 8); // par_height
2151 2154
        }
2155

  
2152 2156
        if(get_bits1(&s->gb)){ /* vol control parameter */
2153 2157
            printf("vol control parameter not supported\n");
2154 2158
            return -1;   
......
2388 2392
//printf("b-code %d\n", s->b_code);
2389 2393
         }
2390 2394
//printf("quant:%d fcode:%d\n", s->qscale, s->f_code);
2391

  
2392 2395
         if(!s->scalability){
2393 2396
             if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) {
2394 2397
                 skip_bits1(&s->gb); // vop shape coding type
libavcodec/h263dec.c
123 123
    if (!s->context_initialized) {
124 124
        avctx->width = s->width;
125 125
        avctx->height = s->height;
126
        avctx->aspect_ratio_info= s->aspect_ratio_info;
126 127
        if (MPV_common_init(s) < 0)
127 128
            return -1;
128 129
    } else if (s->width != avctx->width || s->height != avctx->height) {
libavcodec/motion_est.c
335 335
#define Z_THRESHOLD 256
336 336

  
337 337
#define CHECK_MV(x,y)\
338
{\
338 339
    d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);\
339 340
    d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
340 341
    if(d<dmin){\
341 342
        best[0]=x;\
342 343
        best[1]=y;\
343 344
        dmin=d;\
344
    }
345
    }\
346
}
345 347

  
346 348
#define CHECK_MV_DIR(x,y,new_dir)\
347 349
{\
......
355 357
    }\
356 358
}
357 359

  
360
#define check(x,y,S,v)\
361
if( (x)<(xmin<<(S)) ) printf("%d %d %d %d xmin" #v, (x), (y), s->mb_x, s->mb_y);\
362
if( (x)>(xmax<<(S)) ) printf("%d %d %d %d xmax" #v, (x), (y), s->mb_x, s->mb_y);\
363
if( (y)<(ymin<<(S)) ) printf("%d %d %d %d ymin" #v, (x), (y), s->mb_x, s->mb_y);\
364
if( (y)>(ymax<<(S)) ) printf("%d %d %d %d ymax" #v, (x), (y), s->mb_x, s->mb_y);\
365

  
366

  
358 367
static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
359 368
                                       UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
360 369
                                       int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
......
379 388
            return dmin;
380 389
        }
381 390
    }
391

  
392
/*    for(;;){
393
        int d;
394
        const int x= best[0];
395
        const int y= best[1];
396
        const int last_min=dmin;
397
        if(x>xmin) CHECK_MV(x-1, y  )
398
        if(y>xmin) CHECK_MV(x  , y-1)
399
        if(x<xmax) CHECK_MV(x+1, y  )
400
        if(y<xmax) CHECK_MV(x  , y+1)
401
        if(x>xmin && y>ymin) CHECK_MV(x-1, y-1)
402
        if(x>xmin && y<ymax) CHECK_MV(x-1, y+1)
403
        if(x<xmax && y>ymin) CHECK_MV(x+1, y-1)
404
        if(x<xmax && y<ymax) CHECK_MV(x+1, y+1)
405
        if(x-1>xmin) CHECK_MV(x-2, y  )
406
        if(y-1>xmin) CHECK_MV(x  , y-2)
407
        if(x+1<xmax) CHECK_MV(x+2, y  )
408
        if(y+1<xmax) CHECK_MV(x  , y+2)
409
        if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2)
410
        if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2)
411
        if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2)
412
        if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2)
413
        if(dmin==last_min) return dmin;
414
    }
415
    */
416
}
417

  
418
static inline int snake_search(MpegEncContext * s, int *best, int dmin,
419
                                       UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
420
                                       int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
421
                                       int xmin, int ymin, int xmax, int ymax, int shift)
422
{
423
    int dir=0;
424
    int c=1;
425
    static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1};
426
    static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1};
427
    int fails=0;
428
    int last_d[2]={dmin, dmin};
429

  
430
/*static int good=0;
431
static int bad=0;
432
static int point=0;
433

  
434
point++;
435
if(256*256*256*64%point==0)
436
{
437
    printf("%d %d %d\n", good, bad, point);
438
}*/
439

  
440
    for(;;){
441
        int x= best[0];
442
        int y= best[1];
443
        int d;
444
        x+=x_dir[dir];
445
        y+=y_dir[dir];
446
        if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
447
            d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);
448
            d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;
449
        }else{
450
            d = dmin + 10000; //FIXME smarter boundary handling
451
        }
452
        if(d<dmin){
453
            best[0]=x;
454
            best[1]=y;
455
            dmin=d;
456

  
457
            if(last_d[1] - last_d[0] > last_d[0] - d) c= -c;
458
            dir+=c;
459

  
460
            fails=0;
461
//good++;
462
            last_d[1]=last_d[0];
463
            last_d[0]=d;
464
        }else{
465
//bad++;
466
            if(fails){
467
                if(fails>=3) return dmin;
468
            }else{
469
                c= -c;
470
            }
471
            dir+=c*2;
472
            fails++;
473
        }
474
        dir&=7;
475
    }
382 476
}
383 477

  
384 478
static int epzs_motion_search(MpegEncContext * s,
......
397 491

  
398 492
    new_pic = s->new_picture[0] + pic_xy;
399 493
    old_pic = s->last_picture[0] + pic_xy;
400
//printf("%d %d %d %d\n", xmin, ymin, xmax, ymax);
401
    
494
   
402 495
    dmin = pix_abs16x16(new_pic, old_pic, pic_stride, 16);
403 496
    if(dmin<Z_THRESHOLD){
404 497
        *mx_ptr= 0;
......
424 517
    }
425 518
    CHECK_MV(P[0][0]>>shift, P[0][1]>>shift)
426 519

  
427
    dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride, 
428
                               pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
520
//check(best[0],best[1],0, b0)
521
    if(s->full_search==ME_EPZS)
522
        dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride, 
523
                                   pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
524
    else
525
        dmin=         snake_search(s, best, dmin, new_pic, old_pic, pic_stride, 
526
                                   pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
527
//check(best[0],best[1],0, b1)
429 528
    *mx_ptr= best[0];
430 529
    *my_ptr= best[1];    
431 530

  
432 531
//    printf("%d %d %d \n", best[0], best[1], dmin);
433

  
434 532
    return dmin;
435 533
}
436 534

  
......
563 661
        rel_ymax= ymax - s->mb_y*16;
564 662
        if(s->out_format == FMT_H263){
565 663
            static const int off[4]= {2, 1, 1, -1};
566
            const int mot_stride = s->block_wrap[0];
567
            const int mot_xy = s->block_index[0];
664
            const int mot_stride = s->mb_width*2 + 2;
665
            const int mot_xy = (s->mb_y*2 + 1)*mot_stride + s->mb_x*2 + 1;
568 666
         
569 667
            P[0][0] = s->motion_val[mot_xy    ][0];
570 668
            P[0][1] = s->motion_val[mot_xy    ][1];
......
618 716
                P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
619 717
            }
620 718
        }
621
	dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax);
719
        dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax);
720
 
622 721
        mx+= s->mb_x*16;
623 722
        my+= s->mb_y*16;
624 723
        break;
......
654 753
            mx -= 16 * s->mb_x;
655 754
            my -= 16 * s->mb_y;
656 755
        }
756
//        check(mx + 32*s->mb_x, my + 32*s->mb_y, 1, end)
757
        
657 758
	*mx_ptr = mx;
658 759
	*my_ptr = my;
659 760
	return 0;
libavcodec/mpeg12.c
51 51
                                    int n);
52 52
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
53 53

  
54
static UINT16 mv_penalty[MAX_FCODE][MAX_MV*2+1];
55
static UINT8 fcode_tab[MAX_MV*2+1];
56

  
54 57
static void put_header(MpegEncContext *s, int header)
55 58
{
56 59
    align_put_bits(&s->pb);
......
353 356
    }
354 357
}
355 358

  
359
void mpeg1_encode_init(MpegEncContext *s)
360
{
361
    static int done=0;
362
    if(!done){
363
        int f_code;
364
        int mv;
365

  
366
        done=1;
367
        for(f_code=1; f_code<=MAX_FCODE; f_code++){
368
            for(mv=-MAX_MV; mv<=MAX_MV; mv++){
369
                int len;
370

  
371
                if(mv==0) len= mbMotionVectorTable[0][1];
372
                else{
373
                    int val, bit_size, range, code;
374

  
375
                    bit_size = s->f_code - 1;
376
                    range = 1 << bit_size;
377

  
378
                    val=mv;
379
                    if (val < 0) 
380
                        val = -val;
381
                    val--;
382
                    code = (val >> bit_size) + 1;
383
                    if(code<17){
384
                        len= mbMotionVectorTable[code][1] + 1 + bit_size;
385
                    }else{
386
                        len= mbMotionVectorTable[16][1] + 2 + bit_size;
387
                    }
388
                }
389

  
390
                mv_penalty[f_code][mv+MAX_MV]= len;
391
            }
392
        }
393
        
394

  
395
        for(f_code=MAX_FCODE; f_code>0; f_code--){
396
            for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
397
                fcode_tab[mv+MAX_MV]= f_code;
398
            }
399
        }
400
    }
401
    s->mv_penalty= mv_penalty;
402
    
403
    s->fcode_tab= fcode_tab;
404
}
405
 
356 406
static inline void encode_dc(MpegEncContext *s, int diff, int component)
357 407
{
358 408
    if (component == 0) {
libavcodec/mpegvideo.c
294 294
    s->qcompress= avctx->qcompress;
295 295
    s->qblur= avctx->qblur;
296 296
    s->avctx = avctx;
297
    s->aspect_ratio_info= avctx->aspect_ratio_info;
297 298
    
298 299
    if (s->gop_size <= 1) {
299 300
        s->intra_only = 1;
......
377 378

  
378 379
    if (s->out_format == FMT_H263)
379 380
        h263_encode_init(s);
381
    else if (s->out_format == FMT_MPEG1)
382
        mpeg1_encode_init(s);
380 383

  
381 384
    s->encoding = 1;
382 385

  
......
1075 1078
    /* Reset the average MB variance */
1076 1079
    s->avg_mb_var = 0;
1077 1080
    s->mc_mb_var = 0;
1078

  
1079 1081
    /* Estimate motion for every MB */
1080 1082
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1081 1083
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
......
1132 1134

  
1133 1135
        for(i=MAX_FCODE; i>1; i--){
1134 1136
            loose+= mv_num[i];
1135
            if(loose > 4) break;
1137
            if(loose > 4) break; //FIXME this is pretty ineffective
1136 1138
        }
1137 1139
        s->f_code= i;
1138 1140
    }else{
1139 1141
        s->f_code= 1;
1140 1142
    }
1143

  
1141 1144
//printf("f_code %d ///\n", s->f_code);
1142 1145
    /* convert MBs with too long MVs to I-Blocks */
1143 1146
    if(s->pict_type==P_TYPE){
libavcodec/mpegvideo.h
309 309
void mpeg1_encode_mb(MpegEncContext *s,
310 310
                     DCTELEM block[6][64],
311 311
                     int motion_x, int motion_y);
312
void mpeg1_encode_init(MpegEncContext *s);
312 313

  
313 314
/* h263enc.c */
314 315

  

Also available in: Unified diff