34 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
38 #define M_E 2.718281828
43 double rate_factor,
int frame_num);
48 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
49 "fcode:%d bcode:%d mc-var:%"PRId64
" var:%"PRId64
" icount:%d skipcount:%d hbits:%d;\n",
120 static double (*
const func1[])(
void *, double) = {
141 const_names, func1_names,
func1,
142 NULL, NULL, 0, s->
avctx);
148 for (i = 0; i < 5; i++) {
172 p = strchr(p + 1,
';');
198 next = strchr(p,
';');
203 e = sscanf(p,
" in:%d ", &picture_number);
205 assert(picture_number >= 0);
206 assert(picture_number < rcc->num_entries);
207 rce = &rcc->
entry[picture_number];
209 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:%"SCNd64
" var:%"SCNd64
" icount:%d skipcount:%d hbits:%d",
217 "statistics are damaged at line %d, parser out=%d\n",
234 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
253 for (i = 0; i < 60 * 30; i++) {
257 if (i % ((s->
gop_size + 3) / 4) == 0)
323 buffer_size, rcc->
buffer_index, frame_size, min_rate, max_rate);
332 av_log(s->
avctx,
AV_LOG_ERROR,
"max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
341 int stuffing = ceil((rcc->
buffer_index - buffer_size) / 8);
360 double rate_factor,
int frame_num)
365 const double mb_num = s->
mb_num;
389 rcc->next_non_b_qscale,
414 if (rco[i].start_frame > frame_num)
416 if (rco[i].end_frame < frame_num)
420 bits =
qp2bits(rce, rco[i].qscale);
460 if (q > last_q + maxdiff)
461 q = last_q + maxdiff;
462 else if (q < last_q - maxdiff)
463 q = last_q - maxdiff;
482 assert(qmin <= qmax);
506 double q,
int frame_num)
530 double d = 2 * (buffer_size - expected_size) / buffer_size;
544 "limiting QP %f -> %f\n", q, q_limit);
550 double d = 2 * expected_size / buffer_size;
564 "limiting QP %f -> %f\n", q, q_limit);
569 av_dlog(s,
"q:%f max:%f min:%f size:%f index:%f agr:%f\n",
578 double min2 = log(qmin);
579 double max2 = log(qmax);
582 q = (q - min2) / (max2 - min2) - 0.5;
584 q = 1.0 / (1.0 + exp(q));
585 q = q * (max2 - min2) + min2;
603 double new_coeff = size * q / (var + 1);
610 p->
coeff += new_coeff;
622 float bits_sum = 0.0;
623 float cplx_sum = 0.0;
632 for (i = 0; i < s->
mb_num; i++) {
634 float temp_cplx = sqrt(pic->
mc_mb_var[mb_xy]);
635 float spat_cplx = sqrt(pic->
mb_var[mb_xy]);
636 const int lumi = pic->
mb_mean[mb_xy];
641 float mb_factor = 0.0;
649 factor = 1.0 + p_masking;
652 factor = pow(temp_cplx, -temp_cplx_masking);
654 factor *= pow(spat_cplx, -spatial_cplx_masking);
657 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
659 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
661 if (mb_x < mb_width / 5) {
662 mb_distance = mb_width / 5 - mb_x;
663 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
664 }
else if (mb_x > 4 * mb_width / 5) {
665 mb_distance = mb_x - 4 * mb_width / 5;
666 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
668 if (mb_y < mb_height / 5) {
669 mb_distance = mb_height / 5 - mb_y;
670 mb_factor =
FFMAX(mb_factor,
671 (
float)mb_distance / (
float)(mb_height / 5));
672 }
else if (mb_y > 4 * mb_height / 5) {
673 mb_distance = mb_y - 4 * mb_height / 5;
674 mb_factor =
FFMAX(mb_factor,
675 (
float)mb_distance / (
float)(mb_height / 5));
678 factor *= 1.0 - border_masking * mb_factor;
680 if (factor < 0.00001)
692 float factor = bits_sum / cplx_sum;
693 for (i = 0; i < s->
mb_num; i++) {
694 float newq = q * cplx_tab[i] / bits_tab[i];
698 bits_sum -= bits_tab[i];
699 cplx_sum -= cplx_tab[i] * q / qmax;
700 }
else if (newq < qmin) {
701 bits_sum -= bits_tab[i];
702 cplx_sum -= cplx_tab[i] * q / qmin;
705 if (bits_sum < 0.001)
707 if (cplx_sum < 0.001)
711 for (i = 0; i < s->
mb_num; i++) {
713 float newq = q * cplx_tab[i] / bits_tab[i];
717 newq *= bits_sum / cplx_sum;
720 intq = (int)(newq + 0.5);
724 else if (intq < qmin)
745 float br_compensation;
771 if (picture_number > 2 && !dry_run) {
772 const int64_t last_var =
783 assert(picture_number >= 0);
788 rce = &rcc->
entry[picture_number];
803 wanted_bits = (uint64_t)(s->
bit_rate * (
double)picture_number / fps);
805 wanted_bits = (uint64_t)(s->
bit_rate * (
double)dts_pic->
f.
pts / fps);
810 if (br_compensation <= 0.0)
811 br_compensation = 0.001;
822 br_compensation, s->
frame_bits, var, pict_type);
854 q =
get_qscale(s, rce, rate_factor, picture_number);
882 "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f "
883 "size:%d var:%"PRId64
"/%"PRId64
" br:%d fps:%d\n",
885 qmin, q, qmax, picture_number,
886 (
int)wanted_bits / 1000, (
int)s->
total_bits / 1000,
919 double complexity[5] = { 0 };
920 uint64_t const_bits[5] = { 0 };
921 uint64_t all_const_bits;
922 uint64_t all_available_bits = (uint64_t)(s->
bit_rate *
924 double rate_factor = 0;
926 const int filter_size = (int)(a->
qblur * 4) | 1;
927 double expected_bits = 0;
928 double *qscale, *blurred_qscale, qscale_sum;
949 if (all_available_bits < all_const_bits) {
958 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
971 assert(filter_size % 2 == 1);
991 double q = 0.0, sum = 0.0;
993 for (j = 0; j < filter_size; j++) {
994 int index = i + j - filter_size / 2;
995 double d = index - i;
1005 blurred_qscale[i] = q / sum;
1019 expected_bits +=
bits;
1023 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
1024 expected_bits, (
int)all_available_bits, rate_factor);
1025 if (expected_bits > all_available_bits) {
1026 rate_factor -= step;
1036 av_dlog(s,
"[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
1043 assert(toobig <= 40);
1045 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
1047 (
int)(expected_bits / ((
double)all_available_bits / s->
bit_rate)));
1049 "[lavc rc] estimated target average qp: %.3f\n",
1053 "[lavc rc] Using all of requested bitrate is not "
1054 "necessary for this video with these parameters.\n");
1055 }
else if (toobig == 40) {
1057 "[lavc rc] Error: bitrate too low for this video "
1058 "with these parameters.\n");
1060 }
else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
1062 "[lavc rc] Error: 2pass curve failed to converge\n");