comparison ratecontrol.c @ 690:a1c69cb685b3 libavcodec

adaptive quantization (lumi/temporal & spatial complexity masking)
author michaelni
date Mon, 23 Sep 2002 14:56:11 +0000
parents 3d2da9b44cd8
children b6a7ff92df57
comparison
equal deleted inserted replaced
689:efcbfbd18864 690:a1c69cb685b3
36 36
37 static int init_pass2(MpegEncContext *s); 37 static int init_pass2(MpegEncContext *s);
38 static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num); 38 static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num);
39 39
40 void ff_write_pass1_stats(MpegEncContext *s){ 40 void ff_write_pass1_stats(MpegEncContext *s){
41 sprintf(s->avctx->stats_out, "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d;\n", 41 sprintf(s->avctx->stats_out, "in:%d out:%d type:%d q:%f itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d;\n",
42 s->picture_number, s->input_picture_number - s->max_b_frames, s->pict_type, 42 s->picture_number, s->input_picture_number - s->max_b_frames, s->pict_type,
43 s->qscale, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits, 43 s->frame_qscale, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits,
44 s->f_code, s->b_code, s->mc_mb_var_sum, s->mb_var_sum, s->i_count); 44 s->f_code, s->b_code, s->mc_mb_var_sum, s->mb_var_sum, s->i_count);
45 } 45 }
46 46
47 int ff_rate_control_init(MpegEncContext *s) 47 int ff_rate_control_init(MpegEncContext *s)
48 { 48 {
103 103
104 assert(picture_number >= 0); 104 assert(picture_number >= 0);
105 assert(picture_number < rcc->num_entries); 105 assert(picture_number < rcc->num_entries);
106 rce= &rcc->entry[picture_number]; 106 rce= &rcc->entry[picture_number];
107 107
108 e+=sscanf(p, " in:%*d out:%*d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d", 108 e+=sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d",
109 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits, 109 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits,
110 &rce->f_code, &rce->b_code, &rce->mc_mb_var_sum, &rce->mb_var_sum, &rce->i_count); 110 &rce->f_code, &rce->b_code, &rce->mc_mb_var_sum, &rce->mb_var_sum, &rce->i_count);
111 if(e!=12){ 111 if(e!=12){
112 fprintf(stderr, "statistics are damaged at line %d, parser out=%d\n", i, e); 112 fprintf(stderr, "statistics are damaged at line %d, parser out=%d\n", i, e);
113 return -1; 113 return -1;
431 q= 1.0/(1.0 + exp(q)); 431 q= 1.0/(1.0 + exp(q));
432 q= q*(max2-min2) + min2; 432 q= q*(max2-min2) + min2;
433 433
434 q= exp(q); 434 q= exp(q);
435 } 435 }
436 436
437 return q; 437 return q;
438 } 438 }
439 439
440 //---------------------------------- 440 //----------------------------------
441 // 1 Pass Code 441 // 1 Pass Code
460 p->coeff*= p->decay; 460 p->coeff*= p->decay;
461 p->count++; 461 p->count++;
462 p->coeff+= new_coeff; 462 p->coeff+= new_coeff;
463 } 463 }
464 464
465 int ff_rate_estimate_qscale(MpegEncContext *s) 465 static void adaptive_quantization(MpegEncContext *s, double q){
466 int i;
467 const float lumi_masking= s->avctx->lumi_masking / (128.0*128.0);
468 const float temp_cplx_masking= s->avctx->temporal_cplx_masking;
469 const float spatial_cplx_masking = s->avctx->spatial_cplx_masking;
470 const float p_masking = s->avctx->p_masking;
471 float bits_sum= 0.0;
472 float cplx_sum= 0.0;
473 float cplx_tab[s->mb_num];
474 float bits_tab[s->mb_num];
475 const int qmin= 2; //s->avctx->mb_qmin;
476 const int qmax= 31; //s->avctx->mb_qmax;
477
478 for(i=0; i<s->mb_num; i++){
479 float temp_cplx= sqrt(s->mc_mb_var[i]);
480 float spat_cplx= sqrt(s->mb_var[i]);
481 const int lumi= s->mb_mean[i];
482 float bits, cplx, factor;
483
484 if(spat_cplx < q/3) spat_cplx= q/3; //FIXME finetune
485 if(temp_cplx < q/3) temp_cplx= q/3; //FIXME finetune
486
487 if((s->mb_type[i]&MB_TYPE_INTRA)){//FIXME hq mode
488 cplx= spat_cplx;
489 factor= 1.0 + p_masking;
490 }else{
491 cplx= temp_cplx;
492 factor= pow(temp_cplx, - temp_cplx_masking);
493 }
494 factor*=pow(spat_cplx, - spatial_cplx_masking);
495 factor*= (1.0 - (lumi-128)*(lumi-128)*lumi_masking);
496
497 if(factor<0.00001) factor= 0.00001;
498
499 bits= cplx*factor;
500 cplx_sum+= cplx;
501 bits_sum+= bits;
502 cplx_tab[i]= cplx;
503 bits_tab[i]= bits;
504 }
505
506 /* handle qmin/qmax cliping */
507 if(s->flags&CODEC_FLAG_NORMALIZE_AQP){
508 for(i=0; i<s->mb_num; i++){
509 float newq= q*cplx_tab[i]/bits_tab[i];
510 newq*= bits_sum/cplx_sum;
511
512 if (newq > qmax){
513 bits_sum -= bits_tab[i];
514 cplx_sum -= cplx_tab[i]*q/qmax;
515 }
516 else if(newq < qmin){
517 bits_sum -= bits_tab[i];
518 cplx_sum -= cplx_tab[i]*q/qmin;
519 }
520 }
521 }
522
523 for(i=0; i<s->mb_num; i++){
524 float newq= q*cplx_tab[i]/bits_tab[i];
525 int intq;
526
527 if(s->flags&CODEC_FLAG_NORMALIZE_AQP){
528 newq*= bits_sum/cplx_sum;
529 }
530
531 if(i && ABS(s->qscale_table[i-1] - newq)<0.75)
532 intq= s->qscale_table[i-1];
533 else
534 intq= (int)(newq + 0.5);
535
536 if (intq > qmax) intq= qmax;
537 else if(intq < qmin) intq= qmin;
538 //if(i%s->mb_width==0) printf("\n");
539 //printf("%2d%3d ", intq, ff_sqrt(s->mc_mb_var[i]));
540 s->qscale_table[i]= intq;
541 }
542 }
543
544 float ff_rate_estimate_qscale(MpegEncContext *s)
466 { 545 {
467 float q; 546 float q;
468 int qscale, qmin, qmax; 547 int qmin, qmax;
469 float br_compensation; 548 float br_compensation;
470 double diff; 549 double diff;
471 double short_term_q; 550 double short_term_q;
472 double fps; 551 double fps;
473 int picture_number= s->picture_number; 552 int picture_number= s->picture_number;
579 else if(q>qmax) q=qmax; 658 else if(q>qmax) q=qmax;
580 659
581 // printf("%f %d %d %d\n", q, picture_number, (int)wanted_bits, (int)s->total_bits); 660 // printf("%f %d %d %d\n", q, picture_number, (int)wanted_bits, (int)s->total_bits);
582 661
583 //printf("%f %f %f\n", q, br_compensation, short_term_q); 662 //printf("%f %f %f\n", q, br_compensation, short_term_q);
584 qscale= (int)(q + 0.5); 663
585
586 //printf("q:%d diff:%d comp:%f st_q:%f last_size:%d type:%d\n", qscale, (int)diff, br_compensation, 664 //printf("q:%d diff:%d comp:%f st_q:%f last_size:%d type:%d\n", qscale, (int)diff, br_compensation,
587 // short_term_q, s->frame_bits, pict_type); 665 // short_term_q, s->frame_bits, pict_type);
588 //printf("%d %d\n", s->bit_rate, (int)fps); 666 //printf("%d %d\n", s->bit_rate, (int)fps);
589 667
590 rcc->last_qscale= qscale; 668 if(s->adaptive_quant)
669 adaptive_quantization(s, q);
670 else
671 q= (int)(q + 0.5);
672
673 rcc->last_qscale= q;
591 rcc->last_mc_mb_var_sum= s->mc_mb_var_sum; 674 rcc->last_mc_mb_var_sum= s->mc_mb_var_sum;
592 rcc->last_mb_var_sum= s->mb_var_sum; 675 rcc->last_mb_var_sum= s->mb_var_sum;
593 return qscale; 676 return q;
594 } 677 }
595 678
596 //---------------------------------------------- 679 //----------------------------------------------
597 // 2-Pass code 680 // 2-Pass code
598 681