comparison mpegvideo.c @ 1389:da0b3a50d209 libavcodec

rate distortion mb decision support fix decoding of old %16!=0 divx fix assertion failure in motion_est.c
author michaelni
date Tue, 29 Jul 2003 02:09:12 +0000
parents 18cdce49339e
children ba99a78bcb37
comparison
equal deleted inserted replaced
1388:b5c65adac96a 1389:da0b3a50d209
48 DCTELEM *block, int n, int qscale); 48 DCTELEM *block, int n, int qscale);
49 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w); 49 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
50 #ifdef CONFIG_ENCODERS 50 #ifdef CONFIG_ENCODERS
51 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); 51 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
52 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); 52 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
53 static int sse_mb(MpegEncContext *s);
53 #endif //CONFIG_ENCODERS 54 #endif //CONFIG_ENCODERS
54 55
55 #ifdef HAVE_XVMC 56 #ifdef HAVE_XVMC
56 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx); 57 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
57 extern void XVMC_field_end(MpegEncContext *s); 58 extern void XVMC_field_end(MpegEncContext *s);
2423 } 2424 }
2424 } 2425 }
2425 else if (s->h263_pred || s->h263_aic) 2426 else if (s->h263_pred || s->h263_aic)
2426 s->mbintra_table[mb_xy]=1; 2427 s->mbintra_table[mb_xy]=1;
2427 2428
2428 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
2429 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
2430 //FIXME a lot of thet is only needed for !low_delay
2431 const int wrap = s->block_wrap[0];
2432 const int xy = s->block_index[0];
2433 if(s->mv_type != MV_TYPE_8X8){
2434 int motion_x, motion_y;
2435 if (s->mb_intra) {
2436 motion_x = 0;
2437 motion_y = 0;
2438 } else if (s->mv_type == MV_TYPE_16X16) {
2439 motion_x = s->mv[0][0][0];
2440 motion_y = s->mv[0][0][1];
2441 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
2442 int i;
2443 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
2444 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
2445 motion_x = (motion_x>>1) | (motion_x&1);
2446 for(i=0; i<2; i++){
2447 s->field_mv_table[mb_xy][i][0]= s->mv[0][i][0];
2448 s->field_mv_table[mb_xy][i][1]= s->mv[0][i][1];
2449 s->field_select_table[mb_xy][i]= s->field_select[0][i];
2450 }
2451 }
2452
2453 /* no update if 8X8 because it has been done during parsing */
2454 s->motion_val[xy][0] = motion_x;
2455 s->motion_val[xy][1] = motion_y;
2456 s->motion_val[xy + 1][0] = motion_x;
2457 s->motion_val[xy + 1][1] = motion_y;
2458 s->motion_val[xy + wrap][0] = motion_x;
2459 s->motion_val[xy + wrap][1] = motion_y;
2460 s->motion_val[xy + 1 + wrap][0] = motion_x;
2461 s->motion_val[xy + 1 + wrap][1] = motion_y;
2462 }
2463
2464 if(s->encoding){ //FIXME encoding MUST be cleaned up
2465 if (s->mv_type == MV_TYPE_8X8)
2466 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_8x8;
2467 else
2468 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;
2469 }
2470 }
2471
2472 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc 2429 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
2473 uint8_t *dest_y, *dest_cb, *dest_cr; 2430 uint8_t *dest_y, *dest_cb, *dest_cr;
2474 int dct_linesize, dct_offset; 2431 int dct_linesize, dct_offset;
2475 op_pixels_func (*op_pix)[4]; 2432 op_pixels_func (*op_pix)[4];
2476 qpel_mc_func (*op_qpix)[16]; 2433 qpel_mc_func (*op_qpix)[16];
2477 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics 2434 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2478 const int uvlinesize= s->current_picture.linesize[1]; 2435 const int uvlinesize= s->current_picture.linesize[1];
2479 2436
2480 /* avoid copy if macroblock skipped in last frame too */ 2437 /* avoid copy if macroblock skipped in last frame too */
2481 if (s->pict_type != B_TYPE) {
2482 s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
2483 }
2484
2485 /* skip only during decoding as we might trash the buffers during encoding a bit */ 2438 /* skip only during decoding as we might trash the buffers during encoding a bit */
2486 if(!s->encoding){ 2439 if(!s->encoding){
2487 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy]; 2440 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2488 const int age= s->current_picture.age; 2441 const int age= s->current_picture.age;
2489 2442
2504 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */ 2457 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2505 if(*mbskip_ptr >99) *mbskip_ptr= 99; 2458 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2506 } else{ 2459 } else{
2507 *mbskip_ptr = 0; /* not skipped */ 2460 *mbskip_ptr = 0; /* not skipped */
2508 } 2461 }
2509 }else
2510 s->mb_skiped= 0;
2511
2512 if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
2513 dest_y = s->current_picture.data[0] + mb_x * 16;
2514 dest_cb = s->current_picture.data[1] + mb_x * 8;
2515 dest_cr = s->current_picture.data[2] + mb_x * 8;
2516 }else{
2517 dest_y = s->current_picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
2518 dest_cb = s->current_picture.data[1] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2519 dest_cr = s->current_picture.data[2] + (mb_y * 8 * uvlinesize) + mb_x * 8;
2520 } 2462 }
2521 2463
2522 if (s->interlaced_dct) { 2464 if (s->interlaced_dct) {
2523 dct_linesize = linesize * 2; 2465 dct_linesize = linesize * 2;
2524 dct_offset = linesize; 2466 dct_offset = linesize;
2525 } else { 2467 } else {
2526 dct_linesize = linesize; 2468 dct_linesize = linesize;
2527 dct_offset = linesize * 8; 2469 dct_offset = linesize * 8;
2528 } 2470 }
2471
2472 dest_y= s->dest[0];
2473 dest_cb= s->dest[1];
2474 dest_cr= s->dest[2];
2529 2475
2530 if (!s->mb_intra) { 2476 if (!s->mb_intra) {
2531 /* motion handling */ 2477 /* motion handling */
2532 /* decoding or more than one mb_type (MC was allready done otherwise) */ 2478 /* decoding or more than one mb_type (MC was allready done otherwise) */
2533 if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){ 2479 if(!s->encoding){
2534 if ((!s->no_rounding) || s->pict_type==B_TYPE){ 2480 if ((!s->no_rounding) || s->pict_type==B_TYPE){
2535 op_pix = s->dsp.put_pixels_tab; 2481 op_pix = s->dsp.put_pixels_tab;
2536 op_qpix= s->dsp.put_qpel_pixels_tab; 2482 op_qpix= s->dsp.put_qpel_pixels_tab;
2537 }else{ 2483 }else{
2538 op_pix = s->dsp.put_no_rnd_pixels_tab; 2484 op_pix = s->dsp.put_no_rnd_pixels_tab;
2789 s->avctx->draw_horiz_band(s->avctx, src, offset, 2735 s->avctx->draw_horiz_band(s->avctx, src, offset,
2790 y, s->picture_structure, h); 2736 y, s->picture_structure, h);
2791 } 2737 }
2792 } 2738 }
2793 2739
2740 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2741 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
2742 const int uvlinesize= s->current_picture.linesize[1];
2743
2744 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2745 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
2746 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
2747 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
2748 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2749 s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
2750
2751 if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
2752 s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
2753 s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
2754 s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
2755 }else{
2756 s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16 - 16;
2757 s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
2758 s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
2759 }
2760 }
2761
2794 #ifdef CONFIG_ENCODERS 2762 #ifdef CONFIG_ENCODERS
2795 2763
2796 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) 2764 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2797 { 2765 {
2798 const int mb_x= s->mb_x; 2766 const int mb_x= s->mb_x;
2883 uint8_t *dest_y, *dest_cb, *dest_cr; 2851 uint8_t *dest_y, *dest_cb, *dest_cr;
2884 uint8_t *ptr_y, *ptr_cb, *ptr_cr; 2852 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2885 int wrap_y, wrap_c; 2853 int wrap_y, wrap_c;
2886 int emu=0; 2854 int emu=0;
2887 2855
2888 dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16; 2856 dest_y = s->dest[0];
2889 dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; 2857 dest_cb = s->dest[1];
2890 dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; 2858 dest_cr = s->dest[2];
2891 wrap_y = s->linesize; 2859 wrap_y = s->linesize;
2892 wrap_c = s->uvlinesize; 2860 wrap_c = s->uvlinesize;
2893 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16; 2861 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2894 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8; 2862 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2895 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8; 2863 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
3159 d->b_count= s->b_count; 3127 d->b_count= s->b_count;
3160 d->skip_count= s->skip_count; 3128 d->skip_count= s->skip_count;
3161 d->misc_bits= s->misc_bits; 3129 d->misc_bits= s->misc_bits;
3162 d->last_bits= 0; 3130 d->last_bits= 0;
3163 3131
3164 d->mb_skiped= s->mb_skiped; 3132 d->mb_skiped= 0;
3165 d->qscale= s->qscale; 3133 d->qscale= s->qscale;
3166 } 3134 }
3167 3135
3168 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ 3136 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
3169 int i; 3137 int i;
3204 3172
3205 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 3173 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
3206 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], 3174 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
3207 int *dmin, int *next_block, int motion_x, int motion_y) 3175 int *dmin, int *next_block, int motion_x, int motion_y)
3208 { 3176 {
3209 int bits_count; 3177 int score;
3178 uint8_t *dest_backup[3];
3210 3179
3211 copy_context_before_encode(s, backup, type); 3180 copy_context_before_encode(s, backup, type);
3212 3181
3213 s->block= s->blocks[*next_block]; 3182 s->block= s->blocks[*next_block];
3214 s->pb= pb[*next_block]; 3183 s->pb= pb[*next_block];
3215 if(s->data_partitioning){ 3184 if(s->data_partitioning){
3216 s->pb2 = pb2 [*next_block]; 3185 s->pb2 = pb2 [*next_block];
3217 s->tex_pb= tex_pb[*next_block]; 3186 s->tex_pb= tex_pb[*next_block];
3218 } 3187 }
3188
3189 if(*next_block){
3190 memcpy(dest_backup, s->dest, sizeof(s->dest));
3191 s->dest[0] = s->me.scratchpad;
3192 s->dest[1] = s->me.scratchpad + 16;
3193 s->dest[2] = s->me.scratchpad + 16 + 8;
3194 assert(2*s->uvlinesize == s->linesize); //should be no prob for encoding
3195 assert(s->linesize >= 64); //FIXME
3196 }
3219 3197
3220 encode_mb(s, motion_x, motion_y); 3198 encode_mb(s, motion_x, motion_y);
3221 3199
3222 bits_count= get_bit_count(&s->pb); 3200 score= get_bit_count(&s->pb);
3223 if(s->data_partitioning){ 3201 if(s->data_partitioning){
3224 bits_count+= get_bit_count(&s->pb2); 3202 score+= get_bit_count(&s->pb2);
3225 bits_count+= get_bit_count(&s->tex_pb); 3203 score+= get_bit_count(&s->tex_pb);
3226 } 3204 }
3227 3205
3228 if(bits_count<*dmin){ 3206 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
3229 *dmin= bits_count; 3207 MPV_decode_mb(s, s->block);
3208
3209 score *= s->qscale * s->qscale * 109;
3210 score += sse_mb(s) << 7;
3211 }
3212
3213 if(*next_block){
3214 memcpy(s->dest, dest_backup, sizeof(s->dest));
3215 }
3216
3217 if(score<*dmin){
3218 *dmin= score;
3230 *next_block^=1; 3219 *next_block^=1;
3231 3220
3232 copy_context_after_encode(best, s, type); 3221 copy_context_after_encode(best, s, type);
3233 } 3222 }
3234 } 3223 }
3235 3224
3236 static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){ 3225 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
3237 uint32_t *sq = squareTbl + 256; 3226 uint32_t *sq = squareTbl + 256;
3238 int acc=0; 3227 int acc=0;
3239 int x,y; 3228 int x,y;
3240 3229
3241 if(w==16 && h==16) 3230 if(w==16 && h==16)
3250 } 3239 }
3251 3240
3252 assert(acc>=0); 3241 assert(acc>=0);
3253 3242
3254 return acc; 3243 return acc;
3244 }
3245
3246 static int sse_mb(MpegEncContext *s){
3247 int w= 16;
3248 int h= 16;
3249
3250 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3251 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3252
3253 if(w==16 && h==16)
3254 return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize)
3255 +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize)
3256 +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize);
3257 else
3258 return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
3259 +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
3260 +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
3255 } 3261 }
3256 3262
3257 static void encode_picture(MpegEncContext *s, int picture_number) 3263 static void encode_picture(MpegEncContext *s, int picture_number)
3258 { 3264 {
3259 int mb_x, mb_y, pdif = 0; 3265 int mb_x, mb_y, pdif = 0;
3506 s->resync_mb_x=0; 3512 s->resync_mb_x=0;
3507 s->resync_mb_y=0; 3513 s->resync_mb_y=0;
3508 s->first_slice_line = 1; 3514 s->first_slice_line = 1;
3509 s->ptr_lastgob = s->pb.buf; 3515 s->ptr_lastgob = s->pb.buf;
3510 for(mb_y=0; mb_y < s->mb_height; mb_y++) { 3516 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
3517 s->mb_x=0;
3518 s->mb_y= mb_y;
3519
3511 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ]; 3520 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3512 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ]; 3521 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
3522 ff_init_block_index(s);
3513 3523
3514 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3515 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3516 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3517 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3518 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
3519 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
3520 for(mb_x=0; mb_x < s->mb_width; mb_x++) { 3524 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3521 const int xy= mb_y*s->mb_stride + mb_x; 3525 const int xy= mb_y*s->mb_stride + mb_x;
3522 int mb_type= s->mb_type[xy]; 3526 int mb_type= s->mb_type[xy];
3523 // int d; 3527 // int d;
3524 int dmin=10000000; 3528 int dmin= INT_MAX;
3525 3529
3526 s->mb_x = mb_x; 3530 s->mb_x = mb_x;
3527 s->mb_y = mb_y; 3531 ff_update_block_index(s);
3528 s->block_index[0]+=2;
3529 s->block_index[1]+=2;
3530 s->block_index[2]+=2;
3531 s->block_index[3]+=2;
3532 s->block_index[4]++;
3533 s->block_index[5]++;
3534 3532
3535 /* write gob / video packet header */ 3533 /* write gob / video packet header */
3536 #ifdef CONFIG_RISKY 3534 #ifdef CONFIG_RISKY
3537 if(s->rtp_mode){ 3535 if(s->rtp_mode){
3538 int current_packet_size, is_gob_start; 3536 int current_packet_size, is_gob_start;
3586 if( (s->resync_mb_x == s->mb_x) 3584 if( (s->resync_mb_x == s->mb_x)
3587 && s->resync_mb_y+1 == s->mb_y){ 3585 && s->resync_mb_y+1 == s->mb_y){
3588 s->first_slice_line=0; 3586 s->first_slice_line=0;
3589 } 3587 }
3590 3588
3589 s->mb_skiped=0;
3590
3591 if(mb_type & (mb_type-1)){ // more than 1 MB type possible 3591 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
3592 int next_block=0; 3592 int next_block=0;
3593 int pb_bits_count, pb2_bits_count, tex_pb_bits_count; 3593 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3594 3594
3595 copy_context_before_encode(&backup_s, s, -1); 3595 copy_context_before_encode(&backup_s, s, -1);
3668 s->mb_intra= 1; 3668 s->mb_intra= 1;
3669 s->mv[0][0][0] = 0; 3669 s->mv[0][0][0] = 0;
3670 s->mv[0][0][1] = 0; 3670 s->mv[0][0][1] = 0;
3671 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 3671 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
3672 &dmin, &next_block, 0, 0); 3672 &dmin, &next_block, 0, 0);
3673 /* force cleaning of ac/dc pred stuff if needed ... */ 3673 if(s->h263_pred || s->h263_aic){
3674 if(s->h263_pred || s->h263_aic) 3674 if(best_s.mb_intra)
3675 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1; 3675 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3676 else
3677 ff_clean_intra_table_entries(s); //old mode?
3678 }
3676 } 3679 }
3677 copy_context_after_encode(s, &best_s, -1); 3680 copy_context_after_encode(s, &best_s, -1);
3678 3681
3679 pb_bits_count= get_bit_count(&s->pb); 3682 pb_bits_count= get_bit_count(&s->pb);
3680 flush_put_bits(&s->pb); 3683 flush_put_bits(&s->pb);
3691 flush_put_bits(&s->tex_pb); 3694 flush_put_bits(&s->tex_pb);
3692 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count); 3695 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3693 s->tex_pb= backup_s.tex_pb; 3696 s->tex_pb= backup_s.tex_pb;
3694 } 3697 }
3695 s->last_bits= get_bit_count(&s->pb); 3698 s->last_bits= get_bit_count(&s->pb);
3699
3700 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
3701 ff_h263_update_motion_val(s);
3702
3703 if(next_block==0){
3704 s->dsp.put_pixels_tab[0][0](s->dest[0], s->me.scratchpad , s->linesize ,16);
3705 s->dsp.put_pixels_tab[1][0](s->dest[1], s->me.scratchpad + 16, s->uvlinesize, 8);
3706 s->dsp.put_pixels_tab[1][0](s->dest[2], s->me.scratchpad + 24, s->uvlinesize, 8);
3707 }
3708
3709 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3710 MPV_decode_mb(s, s->block);
3696 } else { 3711 } else {
3697 int motion_x, motion_y; 3712 int motion_x, motion_y;
3698 int intra_score; 3713 int intra_score;
3699 int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_stride]; 3714 int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_stride];
3700 3715
3701 if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){ 3716 if(s->avctx->mb_decision==FF_MB_DECISION_SIMPLE && s->pict_type==P_TYPE){ //FIXME check if the mess is usefull at all
3702 /* get luma score */ 3717 /* get luma score */
3703 if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){ 3718 if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
3704 intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_stride]<<8) - 500; //FIXME dont scale it down so we dont have to fix it 3719 intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_stride]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
3705 }else{ 3720 }else{
3706 uint8_t *dest_y; 3721 uint8_t *dest_y;
3844 3859
3845 encode_mb(s, motion_x, motion_y); 3860 encode_mb(s, motion_x, motion_y);
3846 3861
3847 // RAL: Update last macrobloc type 3862 // RAL: Update last macrobloc type
3848 s->last_mv_dir = s->mv_dir; 3863 s->last_mv_dir = s->mv_dir;
3864
3865 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
3866 ff_h263_update_motion_val(s);
3867
3868 MPV_decode_mb(s, s->block);
3849 } 3869 }
3850 3870
3851 /* clean the MV table in IPS frames for direct mode in B frames */ 3871 /* clean the MV table in IPS frames for direct mode in B frames */
3852 if(s->mb_intra /* && I,P,S_TYPE */){ 3872 if(s->mb_intra /* && I,P,S_TYPE */){
3853 s->p_mv_table[xy][0]=0; 3873 s->p_mv_table[xy][0]=0;
3854 s->p_mv_table[xy][1]=0; 3874 s->p_mv_table[xy][1]=0;
3855 } 3875 }
3856
3857 MPV_decode_mb(s, s->block);
3858 3876
3859 if(s->flags&CODEC_FLAG_PSNR){ 3877 if(s->flags&CODEC_FLAG_PSNR){
3860 int w= 16; 3878 int w= 16;
3861 int h= 16; 3879 int h= 16;
3862 3880
3863 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16; 3881 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3864 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16; 3882 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3865 3883
3866 s->current_picture_ptr->error[0] += sse( 3884 s->current_picture_ptr->error[0] += sse(
3867 s, 3885 s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3868 s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, 3886 s->dest[0], w, h, s->linesize);
3869 s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3870 w, h, s->linesize);
3871 s->current_picture_ptr->error[1] += sse( 3887 s->current_picture_ptr->error[1] += sse(
3872 s, 3888 s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3873 s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, 3889 s->dest[1], w>>1, h>>1, s->uvlinesize);
3874 s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3875 w>>1, h>>1, s->uvlinesize);
3876 s->current_picture_ptr->error[2] += sse( 3890 s->current_picture_ptr->error[2] += sse(
3877 s, 3891 s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3878 s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, 3892 s->dest[2], w>>1, h>>1, s->uvlinesize);
3879 s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
3880 w>>1, h>>1, s->uvlinesize);
3881 } 3893 }
3882 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb)); 3894 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
3883 } 3895 }
3884 } 3896 }
3885 emms_c(); 3897 emms_c();
4400 4412
4401 4413
4402 static const AVOption mpeg4_options[] = 4414 static const AVOption mpeg4_options[] =
4403 { 4415 {
4404 AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000), 4416 AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
4405 AVOPTION_CODEC_FLAG("vhq", "very high quality", flags, CODEC_FLAG_HQ, 0),
4406 AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference" 4417 AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
4407 "the reference can be CBR (for CBR pass1) or VBR (for pass2)", 4418 "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
4408 bit_rate_tolerance, 4, 240000000, 8000), 4419 bit_rate_tolerance, 4, 240000000, 8000),
4409 AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2), 4420 AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
4410 AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31), 4421 AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),