35 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
39 #define M_E 2.718281828
44 double rate_factor,
int frame_num);
49 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
50 "fcode:%d bcode:%d mc-var:%"PRId64
" var:%"PRId64
" icount:%d skipcount:%d hbits:%d;\n",
121 static double (*
const func1[])(
void *, double) = {
142 const_names, func1_names,
func1,
143 NULL, NULL, 0, s->
avctx);
149 for (i = 0; i < 5; i++) {
173 p = strchr(p + 1,
';');
199 next = strchr(p,
';');
204 e = sscanf(p,
" in:%d ", &picture_number);
206 assert(picture_number >= 0);
207 assert(picture_number < rcc->num_entries);
208 rce = &rcc->
entry[picture_number];
210 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",
218 "statistics are damaged at line %d, parser out=%d\n",
235 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
254 for (i = 0; i < 60 * 30; i++) {
258 if (i % ((s->
gop_size + 3) / 4) == 0)
324 buffer_size, rcc->
buffer_index, frame_size, min_rate, max_rate);
333 av_log(s->
avctx,
AV_LOG_ERROR,
"max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
342 int stuffing = ceil((rcc->
buffer_index - buffer_size) / 8);
361 double rate_factor,
int frame_num)
366 const double mb_num = s->
mb_num;
390 rcc->next_non_b_qscale,
415 if (rco[i].start_frame > frame_num)
417 if (rco[i].end_frame < frame_num)
421 bits =
qp2bits(rce, rco[i].qscale);
461 if (q > last_q + maxdiff)
462 q = last_q + maxdiff;
463 else if (q < last_q - maxdiff)
464 q = last_q - maxdiff;
483 assert(qmin <= qmax);
507 double q,
int frame_num)
531 double d = 2 * (buffer_size - expected_size) / buffer_size;
545 "limiting QP %f -> %f\n", q, q_limit);
551 double d = 2 * expected_size / buffer_size;
565 "limiting QP %f -> %f\n", q, q_limit);
570 av_dlog(s,
"q:%f max:%f min:%f size:%f index:%f agr:%f\n",
579 double min2 = log(qmin);
580 double max2 = log(qmax);
583 q = (q - min2) / (max2 - min2) - 0.5;
585 q = 1.0 / (1.0 + exp(q));
586 q = q * (max2 - min2) + min2;
604 double new_coeff = size * q / (var + 1);
611 p->
coeff += new_coeff;
623 float bits_sum = 0.0;
624 float cplx_sum = 0.0;
633 for (i = 0; i < s->
mb_num; i++) {
635 float temp_cplx = sqrt(pic->
mc_mb_var[mb_xy]);
636 float spat_cplx = sqrt(pic->
mb_var[mb_xy]);
637 const int lumi = pic->
mb_mean[mb_xy];
642 float mb_factor = 0.0;
650 factor = 1.0 + p_masking;
653 factor = pow(temp_cplx, -temp_cplx_masking);
655 factor *= pow(spat_cplx, -spatial_cplx_masking);
658 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
660 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
662 if (mb_x < mb_width / 5) {
663 mb_distance = mb_width / 5 - mb_x;
664 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
665 }
else if (mb_x > 4 * mb_width / 5) {
666 mb_distance = mb_x - 4 * mb_width / 5;
667 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
669 if (mb_y < mb_height / 5) {
670 mb_distance = mb_height / 5 - mb_y;
671 mb_factor =
FFMAX(mb_factor,
672 (
float)mb_distance / (
float)(mb_height / 5));
673 }
else if (mb_y > 4 * mb_height / 5) {
674 mb_distance = mb_y - 4 * mb_height / 5;
675 mb_factor =
FFMAX(mb_factor,
676 (
float)mb_distance / (
float)(mb_height / 5));
679 factor *= 1.0 - border_masking * mb_factor;
681 if (factor < 0.00001)
693 float factor = bits_sum / cplx_sum;
694 for (i = 0; i < s->
mb_num; i++) {
695 float newq = q * cplx_tab[i] / bits_tab[i];
699 bits_sum -= bits_tab[i];
700 cplx_sum -= cplx_tab[i] * q / qmax;
701 }
else if (newq < qmin) {
702 bits_sum -= bits_tab[i];
703 cplx_sum -= cplx_tab[i] * q / qmin;
706 if (bits_sum < 0.001)
708 if (cplx_sum < 0.001)
712 for (i = 0; i < s->
mb_num; i++) {
714 float newq = q * cplx_tab[i] / bits_tab[i];
718 newq *= bits_sum / cplx_sum;
721 intq = (int)(newq + 0.5);
725 else if (intq < qmin)
746 float br_compensation;
772 if (picture_number > 2 && !dry_run) {
773 const int64_t last_var =
784 assert(picture_number >= 0);
789 rce = &rcc->
entry[picture_number];
804 wanted_bits = (uint64_t)(s->
bit_rate * (
double)picture_number / fps);
806 wanted_bits = (uint64_t)(s->
bit_rate * (
double)dts_pic->
f->
pts / fps);
811 if (br_compensation <= 0.0)
812 br_compensation = 0.001;
823 br_compensation, s->
frame_bits, var, pict_type);
855 q =
get_qscale(s, rce, rate_factor, picture_number);
883 "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f "
884 "size:%d var:%"PRId64
"/%"PRId64
" br:%d fps:%d\n",
886 qmin, q, qmax, picture_number,
887 (
int)wanted_bits / 1000, (
int)s->
total_bits / 1000,
920 double complexity[5] = { 0 };
921 uint64_t const_bits[5] = { 0 };
922 uint64_t all_const_bits;
923 uint64_t all_available_bits = (uint64_t)(s->
bit_rate *
925 double rate_factor = 0;
927 const int filter_size = (int)(a->
qblur * 4) | 1;
928 double expected_bits = 0;
929 double *qscale, *blurred_qscale, qscale_sum;
950 if (all_available_bits < all_const_bits) {
959 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
972 assert(filter_size % 2 == 1);
992 double q = 0.0, sum = 0.0;
994 for (j = 0; j < filter_size; j++) {
995 int index = i + j - filter_size / 2;
996 double d = index - i;
1006 blurred_qscale[i] = q / sum;
1020 expected_bits +=
bits;
1024 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
1025 expected_bits, (
int)all_available_bits, rate_factor);
1026 if (expected_bits > all_available_bits) {
1027 rate_factor -= step;
1037 av_dlog(s,
"[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
1044 assert(toobig <= 40);
1046 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
1048 (
int)(expected_bits / ((
double)all_available_bits / s->
bit_rate)));
1050 "[lavc rc] estimated target average qp: %.3f\n",
1054 "[lavc rc] Using all of requested bitrate is not "
1055 "necessary for this video with these parameters.\n");
1056 }
else if (toobig == 40) {
1058 "[lavc rc] Error: bitrate too low for this video "
1059 "with these parameters.\n");
1061 }
else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
1063 "[lavc rc] Error: 2pass curve failed to converge\n");