comparison ratecontrol.c @ 6481:493dc59d469a libavcodec

add FF_ prefix to all (frame)_TYPE usage
author aurel
date Sun, 09 Mar 2008 23:31:02 +0000
parents e56b22861426
children 52925923273f
comparison
equal deleted inserted replaced
6480:6f01a499e785 6481:493dc59d469a
142 rcc->num_entries= i; 142 rcc->num_entries= i;
143 143
144 /* init all to skipped p frames (with b frames we might have a not encoded frame at the end FIXME) */ 144 /* init all to skipped p frames (with b frames we might have a not encoded frame at the end FIXME) */
145 for(i=0; i<rcc->num_entries; i++){ 145 for(i=0; i<rcc->num_entries; i++){
146 RateControlEntry *rce= &rcc->entry[i]; 146 RateControlEntry *rce= &rcc->entry[i];
147 rce->pict_type= rce->new_pict_type=P_TYPE; 147 rce->pict_type= rce->new_pict_type=FF_P_TYPE;
148 rce->qscale= rce->new_qscale=FF_QP2LAMBDA * 2; 148 rce->qscale= rce->new_qscale=FF_QP2LAMBDA * 2;
149 rce->misc_bits= s->mb_num + 10; 149 rce->misc_bits= s->mb_num + 10;
150 rce->mb_var_sum= s->mb_num*100; 150 rce->mb_var_sum= s->mb_num*100;
151 } 151 }
152 152
210 for(i=0; i<60*30; i++){ 210 for(i=0; i<60*30; i++){
211 double bits= s->avctx->rc_initial_cplx * (i/10000.0 + 1.0)*s->mb_num; 211 double bits= s->avctx->rc_initial_cplx * (i/10000.0 + 1.0)*s->mb_num;
212 RateControlEntry rce; 212 RateControlEntry rce;
213 double q; 213 double q;
214 214
215 if (i%((s->gop_size+3)/4)==0) rce.pict_type= I_TYPE; 215 if (i%((s->gop_size+3)/4)==0) rce.pict_type= FF_I_TYPE;
216 else if(i%(s->max_b_frames+1)) rce.pict_type= B_TYPE; 216 else if(i%(s->max_b_frames+1)) rce.pict_type= FF_B_TYPE;
217 else rce.pict_type= P_TYPE; 217 else rce.pict_type= FF_P_TYPE;
218 218
219 rce.new_pict_type= rce.pict_type; 219 rce.new_pict_type= rce.pict_type;
220 rce.mc_mb_var_sum= bits*s->mb_num/100000; 220 rce.mc_mb_var_sum= bits*s->mb_num/100000;
221 rce.mb_var_sum = s->mb_num; 221 rce.mb_var_sum = s->mb_num;
222 rce.qscale = FF_QP2LAMBDA * 2; 222 rce.qscale = FF_QP2LAMBDA * 2;
223 rce.f_code = 2; 223 rce.f_code = 2;
224 rce.b_code = 1; 224 rce.b_code = 1;
225 rce.misc_bits= 1; 225 rce.misc_bits= 1;
226 226
227 if(s->pict_type== I_TYPE){ 227 if(s->pict_type== FF_I_TYPE){
228 rce.i_count = s->mb_num; 228 rce.i_count = s->mb_num;
229 rce.i_tex_bits= bits; 229 rce.i_tex_bits= bits;
230 rce.p_tex_bits= 0; 230 rce.p_tex_bits= 0;
231 rce.mv_bits= 0; 231 rce.mv_bits= 0;
232 }else{ 232 }else{
318 M_E, 318 M_E,
319 rce->i_tex_bits*rce->qscale, 319 rce->i_tex_bits*rce->qscale,
320 rce->p_tex_bits*rce->qscale, 320 rce->p_tex_bits*rce->qscale,
321 (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale, 321 (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale,
322 rce->mv_bits/mb_num, 322 rce->mv_bits/mb_num,
323 rce->pict_type == B_TYPE ? (rce->f_code + rce->b_code)*0.5 : rce->f_code, 323 rce->pict_type == FF_B_TYPE ? (rce->f_code + rce->b_code)*0.5 : rce->f_code,
324 rce->i_count/mb_num, 324 rce->i_count/mb_num,
325 rce->mc_mb_var_sum/mb_num, 325 rce->mc_mb_var_sum/mb_num,
326 rce->mb_var_sum/mb_num, 326 rce->mb_var_sum/mb_num,
327 rce->pict_type == I_TYPE, 327 rce->pict_type == FF_I_TYPE,
328 rce->pict_type == P_TYPE, 328 rce->pict_type == FF_P_TYPE,
329 rce->pict_type == B_TYPE, 329 rce->pict_type == FF_B_TYPE,
330 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type], 330 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
331 a->qcompress, 331 a->qcompress,
332 /* rcc->last_qscale_for[I_TYPE], 332 /* rcc->last_qscale_for[FF_I_TYPE],
333 rcc->last_qscale_for[P_TYPE], 333 rcc->last_qscale_for[FF_P_TYPE],
334 rcc->last_qscale_for[B_TYPE], 334 rcc->last_qscale_for[FF_B_TYPE],
335 rcc->next_non_b_qscale,*/ 335 rcc->next_non_b_qscale,*/
336 rcc->i_cplx_sum[I_TYPE] / (double)rcc->frame_count[I_TYPE], 336 rcc->i_cplx_sum[FF_I_TYPE] / (double)rcc->frame_count[FF_I_TYPE],
337 rcc->i_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE], 337 rcc->i_cplx_sum[FF_P_TYPE] / (double)rcc->frame_count[FF_P_TYPE],
338 rcc->p_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE], 338 rcc->p_cplx_sum[FF_P_TYPE] / (double)rcc->frame_count[FF_P_TYPE],
339 rcc->p_cplx_sum[B_TYPE] / (double)rcc->frame_count[B_TYPE], 339 rcc->p_cplx_sum[FF_B_TYPE] / (double)rcc->frame_count[FF_B_TYPE],
340 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type], 340 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
341 0 341 0
342 }; 342 };
343 343
344 bits= ff_parse_eval(rcc->rc_eq_eval, const_values, rce); 344 bits= ff_parse_eval(rcc->rc_eq_eval, const_values, rce);
365 } 365 }
366 366
367 q= bits2qp(rce, bits); 367 q= bits2qp(rce, bits);
368 368
369 /* I/B difference */ 369 /* I/B difference */
370 if (pict_type==I_TYPE && s->avctx->i_quant_factor<0.0) 370 if (pict_type==FF_I_TYPE && s->avctx->i_quant_factor<0.0)
371 q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset; 371 q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset;
372 else if(pict_type==B_TYPE && s->avctx->b_quant_factor<0.0) 372 else if(pict_type==FF_B_TYPE && s->avctx->b_quant_factor<0.0)
373 q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset; 373 q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset;
374 if(q<1) q=1; 374 if(q<1) q=1;
375 375
376 return q; 376 return q;
377 } 377 }
378 378
379 static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q){ 379 static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q){
380 RateControlContext *rcc= &s->rc_context; 380 RateControlContext *rcc= &s->rc_context;
381 AVCodecContext *a= s->avctx; 381 AVCodecContext *a= s->avctx;
382 const int pict_type= rce->new_pict_type; 382 const int pict_type= rce->new_pict_type;
383 const double last_p_q = rcc->last_qscale_for[P_TYPE]; 383 const double last_p_q = rcc->last_qscale_for[FF_P_TYPE];
384 const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type]; 384 const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type];
385 385
386 if (pict_type==I_TYPE && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==P_TYPE)) 386 if (pict_type==FF_I_TYPE && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==FF_P_TYPE))
387 q= last_p_q *FFABS(a->i_quant_factor) + a->i_quant_offset; 387 q= last_p_q *FFABS(a->i_quant_factor) + a->i_quant_offset;
388 else if(pict_type==B_TYPE && a->b_quant_factor>0.0) 388 else if(pict_type==FF_B_TYPE && a->b_quant_factor>0.0)
389 q= last_non_b_q* a->b_quant_factor + a->b_quant_offset; 389 q= last_non_b_q* a->b_quant_factor + a->b_quant_offset;
390 if(q<1) q=1; 390 if(q<1) q=1;
391 391
392 /* last qscale / qdiff stuff */ 392 /* last qscale / qdiff stuff */
393 if(rcc->last_non_b_pict_type==pict_type || pict_type!=I_TYPE){ 393 if(rcc->last_non_b_pict_type==pict_type || pict_type!=FF_I_TYPE){
394 double last_q= rcc->last_qscale_for[pict_type]; 394 double last_q= rcc->last_qscale_for[pict_type];
395 const int maxdiff= FF_QP2LAMBDA * a->max_qdiff; 395 const int maxdiff= FF_QP2LAMBDA * a->max_qdiff;
396 396
397 if (q > last_q + maxdiff) q= last_q + maxdiff; 397 if (q > last_q + maxdiff) q= last_q + maxdiff;
398 else if(q < last_q - maxdiff) q= last_q - maxdiff; 398 else if(q < last_q - maxdiff) q= last_q - maxdiff;
399 } 399 }
400 400
401 rcc->last_qscale_for[pict_type]= q; //Note we cannot do that after blurring 401 rcc->last_qscale_for[pict_type]= q; //Note we cannot do that after blurring
402 402
403 if(pict_type!=B_TYPE) 403 if(pict_type!=FF_B_TYPE)
404 rcc->last_non_b_pict_type= pict_type; 404 rcc->last_non_b_pict_type= pict_type;
405 405
406 return q; 406 return q;
407 } 407 }
408 408
413 int qmin= s->avctx->lmin; 413 int qmin= s->avctx->lmin;
414 int qmax= s->avctx->lmax; 414 int qmax= s->avctx->lmax;
415 415
416 assert(qmin <= qmax); 416 assert(qmin <= qmax);
417 417
418 if(pict_type==B_TYPE){ 418 if(pict_type==FF_B_TYPE){
419 qmin= (int)(qmin*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5); 419 qmin= (int)(qmin*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
420 qmax= (int)(qmax*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5); 420 qmax= (int)(qmax*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
421 }else if(pict_type==I_TYPE){ 421 }else if(pict_type==FF_I_TYPE){
422 qmin= (int)(qmin*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5); 422 qmin= (int)(qmin*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
423 qmax= (int)(qmax*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5); 423 qmax= (int)(qmax*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
424 } 424 }
425 425
426 qmin= av_clip(qmin, 1, FF_LAMBDA_MAX); 426 qmin= av_clip(qmin, 1, FF_LAMBDA_MAX);
443 const double max_rate= s->avctx->rc_max_rate / fps; 443 const double max_rate= s->avctx->rc_max_rate / fps;
444 444
445 get_qminmax(&qmin, &qmax, s, pict_type); 445 get_qminmax(&qmin, &qmax, s, pict_type);
446 446
447 /* modulation */ 447 /* modulation */
448 if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==P_TYPE) 448 if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==FF_P_TYPE)
449 q*= s->avctx->rc_qmod_amp; 449 q*= s->avctx->rc_qmod_amp;
450 450
451 bits= qp2bits(rce, q); 451 bits= qp2bits(rce, q);
452 //printf("q:%f\n", q); 452 //printf("q:%f\n", q);
453 /* buffer overflow/underflow protection */ 453 /* buffer overflow/underflow protection */
687 687
688 fps= 1/av_q2d(s->avctx->time_base); 688 fps= 1/av_q2d(s->avctx->time_base);
689 //printf("input_pic_num:%d pic_num:%d frame_rate:%d\n", s->input_picture_number, s->picture_number, s->frame_rate); 689 //printf("input_pic_num:%d pic_num:%d frame_rate:%d\n", s->input_picture_number, s->picture_number, s->frame_rate);
690 /* update predictors */ 690 /* update predictors */
691 if(picture_number>2 && !dry_run){ 691 if(picture_number>2 && !dry_run){
692 const int last_var= s->last_pict_type == I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum; 692 const int last_var= s->last_pict_type == FF_I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum;
693 update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits); 693 update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits);
694 } 694 }
695 695
696 if(s->flags&CODEC_FLAG_PASS2){ 696 if(s->flags&CODEC_FLAG_PASS2){
697 assert(picture_number>=0); 697 assert(picture_number>=0);
702 Picture *dts_pic; 702 Picture *dts_pic;
703 rce= &local_rce; 703 rce= &local_rce;
704 704
705 //FIXME add a dts field to AVFrame and ensure its set and use it here instead of reordering 705 //FIXME add a dts field to AVFrame and ensure its set and use it here instead of reordering
706 //but the reordering is simpler for now until h.264 b pyramid must be handeld 706 //but the reordering is simpler for now until h.264 b pyramid must be handeld
707 if(s->pict_type == B_TYPE || s->low_delay) 707 if(s->pict_type == FF_B_TYPE || s->low_delay)
708 dts_pic= s->current_picture_ptr; 708 dts_pic= s->current_picture_ptr;
709 else 709 else
710 dts_pic= s->last_picture_ptr; 710 dts_pic= s->last_picture_ptr;
711 711
712 //if(dts_pic) 712 //if(dts_pic)
720 720
721 diff= s->total_bits - wanted_bits; 721 diff= s->total_bits - wanted_bits;
722 br_compensation= (a->bit_rate_tolerance - diff)/a->bit_rate_tolerance; 722 br_compensation= (a->bit_rate_tolerance - diff)/a->bit_rate_tolerance;
723 if(br_compensation<=0.0) br_compensation=0.001; 723 if(br_compensation<=0.0) br_compensation=0.001;
724 724
725 var= pict_type == I_TYPE ? pic->mb_var_sum : pic->mc_mb_var_sum; 725 var= pict_type == FF_I_TYPE ? pic->mb_var_sum : pic->mc_mb_var_sum;
726 726
727 short_term_q = 0; /* avoid warning */ 727 short_term_q = 0; /* avoid warning */
728 if(s->flags&CODEC_FLAG_PASS2){ 728 if(s->flags&CODEC_FLAG_PASS2){
729 if(pict_type!=I_TYPE) 729 if(pict_type!=FF_I_TYPE)
730 assert(pict_type == rce->new_pict_type); 730 assert(pict_type == rce->new_pict_type);
731 731
732 q= rce->new_qscale / br_compensation; 732 q= rce->new_qscale / br_compensation;
733 //printf("%f %f %f last:%d var:%d type:%d//\n", q, rce->new_qscale, br_compensation, s->frame_bits, var, pict_type); 733 //printf("%f %f %f last:%d var:%d type:%d//\n", q, rce->new_qscale, br_compensation, s->frame_bits, var, pict_type);
734 }else{ 734 }else{
740 rce->f_code = s->f_code; 740 rce->f_code = s->f_code;
741 rce->b_code = s->b_code; 741 rce->b_code = s->b_code;
742 rce->misc_bits= 1; 742 rce->misc_bits= 1;
743 743
744 bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var)); 744 bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
745 if(pict_type== I_TYPE){ 745 if(pict_type== FF_I_TYPE){
746 rce->i_count = s->mb_num; 746 rce->i_count = s->mb_num;
747 rce->i_tex_bits= bits; 747 rce->i_tex_bits= bits;
748 rce->p_tex_bits= 0; 748 rce->p_tex_bits= 0;
749 rce->mv_bits= 0; 749 rce->mv_bits= 0;
750 }else{ 750 }else{
770 //printf("%f ", q); 770 //printf("%f ", q);
771 q= get_diff_limited_q(s, rce, q); 771 q= get_diff_limited_q(s, rce, q);
772 //printf("%f ", q); 772 //printf("%f ", q);
773 assert(q>0.0); 773 assert(q>0.0);
774 774
775 if(pict_type==P_TYPE || s->intra_only){ //FIXME type dependent blur like in 2-pass 775 if(pict_type==FF_P_TYPE || s->intra_only){ //FIXME type dependent blur like in 2-pass
776 rcc->short_term_qsum*=a->qblur; 776 rcc->short_term_qsum*=a->qblur;
777 rcc->short_term_qcount*=a->qblur; 777 rcc->short_term_qcount*=a->qblur;
778 778
779 rcc->short_term_qsum+= q; 779 rcc->short_term_qsum+= q;
780 rcc->short_term_qcount++; 780 rcc->short_term_qcount++;
853 rcc->frame_count[rce->pict_type] ++; 853 rcc->frame_count[rce->pict_type] ++;
854 854
855 complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale; 855 complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;
856 const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits; 856 const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;
857 } 857 }
858 all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE]; 858 all_const_bits= const_bits[FF_I_TYPE] + const_bits[FF_P_TYPE] + const_bits[FF_B_TYPE];
859 859
860 if(all_available_bits < all_const_bits){ 860 if(all_available_bits < all_const_bits){
861 av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n"); 861 av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n");
862 return -1; 862 return -1;
863 } 863 }