1 /*
2 * Rate control for video encoders
3 *
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Rate control for video encoders.
26 */
27
33
34 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
35 #include <assert.h>
36
37 #ifndef M_E
38 #define M_E 2.718281828
39 #endif
40
43 double rate_factor, int frame_num);
44
46 {
48 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
49 "fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d;\n",
64 }
65
67 {
69 }
70
72 {
73 if (qp <= 0.0) {
75 }
77 }
78
80 {
81 if (bits < 0.9) {
83 }
85 }
86
88 {
92 "PI",
93 "E",
94 "iTex",
95 "pTex",
96 "tex",
97 "mv",
98 "fCode",
99 "iCount",
100 "mcVar",
101 "var",
102 "isI",
103 "isP",
104 "isB",
105 "avgQP",
106 "qComp",
107 #if 0
108 "lastIQP",
109 "lastPQP",
110 "lastBQP",
111 "nextNonBQP",
112 #endif
113 "avgIITex",
114 "avgPITex",
115 "avgPPTex",
116 "avgBPTex",
117 "avgTex",
118 NULL
119 };
120 static double (*
const func1[])(
void *, double) = {
123 NULL
124 };
126 "bits2qp",
127 "qp2bits",
128 NULL
129 };
131
135 } else
137 }
138
141 const_names, func1_names,
func1,
142 NULL, NULL, 0, s->
avctx);
143 if (res < 0) {
146 }
147
148 for (i = 0; i < 5; i++) {
152
157 rcc->
frame_count[i] = 1;
// 1 is better because of 1/0 and such
158
160 }
164
166 int i;
167 char *p;
168
169 /* find number of pics */
171 for (i = -1; p; i++)
172 p = strchr(p + 1, ';');
175 return -1;
178
179 /* init all to skipped p frames
180 * (with b frames we might have a not encoded frame at the end FIXME) */
183
188 }
189
190 /* read stats */
194 int picture_number;
195 int e;
196 char *next;
197
198 next = strchr(p, ';');
199 if (next) {
200 (*next) = 0; // sscanf in unbelievably slow on looong strings // FIXME copy / do not write
201 next++;
202 }
203 e = sscanf(p, " in:%d ", &picture_number);
204
205 assert(picture_number >= 0);
206 assert(picture_number < rcc->num_entries);
207 rce = &rcc->
entry[picture_number];
208
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:%d var:%d icount:%d skipcount:%d hbits:%d",
215 if (e != 14) {
217 "statistics are damaged at line %d, parser out=%d\n",
218 i, e);
219 return -1;
220 }
221
222 p = next;
223 }
224
226 return -1;
227
228 // FIXME maybe move to end
230 #if CONFIG_LIBXVID
232 #else
234 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
235 return -1;
236 #endif
237 }
238 }
239
243
246
249 return -1;
250 }
251 /* init stuff with the user specified complexity */
253 for (i = 0; i < 60 * 30; i++) {
256
257 if (i % ((s->
gop_size + 3) / 4) == 0)
261 else
263
267
272
278 } else {
279 rce.
i_count = 0;
// FIXME we do know this approx
283 }
288
290
291 // FIXME misbehaves a little for variable fps
293 }
294 }
295 }
296
297 return 0;
298 }
299
301 {
304
307
308 #if CONFIG_LIBXVID
311 #endif
312 }
313
315 {
321
323 buffer_size, rcc->
buffer_index, frame_size, min_rate, max_rate);
324
325 if (buffer_size) {
326 int left;
327
332 }
333
336
338 int stuffing = ceil((rcc->
buffer_index - buffer_size) / 8);
339
341 stuffing = 4;
343
346
347 return stuffing;
348 }
349 }
350 return 0;
351 }
352
353 /**
354 * Modify the bitrate curve from pass1 for one frame.
355 */
357 double rate_factor, int frame_num)
358 {
362 const double mb_num = s->
mb_num;
364 int i;
365
382 #if 0
386 rcc->next_non_b_qscale,
387 #endif
393 0
394 };
395
399 return -1;
400 }
401
403 bits *= rate_factor;
404 if (bits < 0.0)
405 bits = 0.0;
406 bits += 1.0; // avoid 1/0 issues
407
408 /* user override */
411 if (rco[i].start_frame > frame_num)
412 continue;
413 if (rco[i].end_frame < frame_num)
414 continue;
415
416 if (rco[i].qscale)
417 bits =
qp2bits(rce, rco[i].qscale);
// FIXME move at end to really force it?
418 else
420 }
421
423
424 /* I/B difference */
429 if (q < 1)
430 q = 1;
431
432 return q;
433 }
434
436 {
442
449 if (q < 1)
450 q = 1;
451
452 /* last qscale / qdiff stuff */
456
457 if (q > last_q + maxdiff)
458 q = last_q + maxdiff;
459 else if (q < last_q - maxdiff)
460 q = last_q - maxdiff;
461 }
462
463 rcc->
last_qscale_for[pict_type] = q;
// Note we cannot do that after blurring
464
467
468 return q;
469 }
470
471 /**
472 * Get the qmin & qmax for pict_type.
473 */
475 {
478
479 assert(qmin <= qmax);
480
481 switch (pict_type) {
485 break;
489 break;
490 }
491
494
495 if (qmax < qmin)
496 qmax = qmin;
497
498 *qmin_ret = qmin;
499 *qmax_ret = qmax;
500 }
501
503 double q, int frame_num)
504 {
511 int qmin, qmax;
512
514
515 /* modulation */
520
521 /* buffer overflow/underflow protection */
522 if (buffer_size) {
524 double q_limit;
525
526 if (min_rate) {
527 double d = 2 * (buffer_size - expected_size) / buffer_size;
528 if (d > 1.0)
529 d = 1.0;
530 else if (d < 0.0001)
531 d = 0.0001;
533
537
538 if (q > q_limit) {
541 "limiting QP %f -> %f\n", q, q_limit);
542 q = q_limit;
543 }
544 }
545
546 if (max_rate) {
547 double d = 2 * expected_size / buffer_size;
548 if (d > 1.0)
549 d = 1.0;
550 else if (d < 0.0001)
551 d = 0.0001;
553
557 1));
558 if (q < q_limit) {
561 "limiting QP %f -> %f\n", q, q_limit);
562 q = q_limit;
563 }
564 }
565 }
566 av_dlog(s,
"q:%f max:%f min:%f size:%f index:%f agr:%f\n",
570 if (q < qmin)
571 q = qmin;
572 else if (q > qmax)
573 q = qmax;
574 } else {
575 double min2 = log(qmin);
576 double max2 = log(qmax);
577
578 q = log(q);
579 q = (q - min2) / (max2 - min2) - 0.5;
580 q *= -4.0;
581 q = 1.0 / (1.0 + exp(q));
582 q = q * (max2 - min2) + min2;
583
584 q = exp(q);
585 }
586
587 return q;
588 }
589
590 // ----------------------------------
591 // 1 Pass Code
592
594 {
596 }
597
599 {
600 double new_coeff = size * q / (var + 1);
601 if (var < 10)
602 return;
603
607 p->
coeff += new_coeff;
608 }
609
611 {
612 int i;
619 float bits_sum = 0.0;
620 float cplx_sum = 0.0;
628
629 for (i = 0; i < s->
mb_num; i++) {
631 float temp_cplx = sqrt(pic->
mc_mb_var[mb_xy]);
// FIXME merge in pow()
632 float spat_cplx = sqrt(pic->
mb_var[mb_xy]);
633 const int lumi = pic->
mb_mean[mb_xy];
637 int mb_distance;
638 float mb_factor = 0.0;
639 if (spat_cplx < 4)
640 spat_cplx = 4; // FIXME finetune
641 if (temp_cplx < 4)
642 temp_cplx = 4; // FIXME finetune
643
645 cplx = spat_cplx;
646 factor = 1.0 + p_masking;
647 } else {
648 cplx = temp_cplx;
649 factor = pow(temp_cplx, -temp_cplx_masking);
650 }
651 factor *= pow(spat_cplx, -spatial_cplx_masking);
652
653 if (lumi > 127)
654 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
655 else
656 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
657
658 if (mb_x < mb_width / 5) {
659 mb_distance = mb_width / 5 - mb_x;
660 mb_factor = (float)mb_distance / (float)(mb_width / 5);
661 } else if (mb_x > 4 * mb_width / 5) {
662 mb_distance = mb_x - 4 * mb_width / 5;
663 mb_factor = (float)mb_distance / (float)(mb_width / 5);
664 }
665 if (mb_y < mb_height / 5) {
666 mb_distance = mb_height / 5 - mb_y;
667 mb_factor =
FFMAX(mb_factor,
668 (float)mb_distance / (float)(mb_height / 5));
669 } else if (mb_y > 4 * mb_height / 5) {
670 mb_distance = mb_y - 4 * mb_height / 5;
671 mb_factor =
FFMAX(mb_factor,
672 (float)mb_distance / (float)(mb_height / 5));
673 }
674
675 factor *= 1.0 - border_masking * mb_factor;
676
677 if (factor < 0.00001)
678 factor = 0.00001;
679
681 cplx_sum += cplx;
683 cplx_tab[i] = cplx;
685 }
686
687 /* handle qmin/qmax clipping */
689 float factor = bits_sum / cplx_sum;
690 for (i = 0; i < s->
mb_num; i++) {
691 float newq = q * cplx_tab[i] / bits_tab[i];
693
694 if (newq > qmax) {
695 bits_sum -= bits_tab[i];
696 cplx_sum -= cplx_tab[i] * q / qmax;
697 } else if (newq < qmin) {
698 bits_sum -= bits_tab[i];
699 cplx_sum -= cplx_tab[i] * q / qmin;
700 }
701 }
702 if (bits_sum < 0.001)
703 bits_sum = 0.001;
704 if (cplx_sum < 0.001)
705 cplx_sum = 0.001;
706 }
707
708 for (i = 0; i < s->
mb_num; i++) {
710 float newq = q * cplx_tab[i] / bits_tab[i];
711 int intq;
712
714 newq *= bits_sum / cplx_sum;
715 }
716
717 intq = (int)(newq + 0.5);
718
719 if (intq > qmax)
720 intq = qmax;
721 else if (intq < qmin)
722 intq = qmin;
724 }
725 }
726
728 {
731
734 }
735
736 // FIXME rd or at least approx for dquant
737
739 {
740 float q;
741 int qmin, qmax;
742 float br_compensation;
744 double short_term_q;
745 double fps;
747 int64_t wanted_bits;
752 double rate_factor;
753 int var;
757
758 #if CONFIG_LIBXVID
762 #endif
763
765
767 /* update predictors */
768 if (picture_number > 2 && !dry_run) {
774 sqrt(last_var),
776 }
777
779 assert(picture_number >= 0);
782 return -1;
783 }
784 rce = &rcc->
entry[picture_number];
786 } else {
788 rce = &local_rce;
789
790 /* FIXME add a dts field to AVFrame and ensure it is set and use it
791 * here instead of reordering but the reordering is simpler for now
792 * until H.264 B-pyramid must be handled. */
795 else
797
799 wanted_bits = (uint64_t)(s->
bit_rate * (
double)picture_number / fps);
800 else
801 wanted_bits = (uint64_t)(s->
bit_rate * (
double)dts_pic->
f.
pts / fps);
802 }
803
806 if (br_compensation <= 0.0)
807 br_compensation = 0.001;
808
810
811 short_term_q = 0; /* avoid warning */
815
818 br_compensation, s->
frame_bits, var, pict_type);
819 } else {
828
835 } else {
836 rce->
i_count = 0;
// FIXME we do know this approx
840 }
845
849
850 q =
get_qscale(s, rce, rate_factor, picture_number);
851 if (q < 0)
852 return -1;
853
854 assert(q > 0.0);
856 assert(q > 0.0);
857
858 // FIXME type dependent blur like in 2-pass
862
866 }
867 assert(q > 0.0);
868
870
872
873 assert(q > 0.0);
874 }
875
878 "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f "
879 "size:%d var:%d/%d br:%d fps:%d\n",
881 qmin, q, qmax, picture_number,
882 (
int)wanted_bits / 1000, (
int)s->
total_bits / 1000,
886 }
887
888 if (q < qmin)
889 q = qmin;
890 else if (q > qmax)
891 q = qmax;
892
895 else
896 q = (int)(q + 0.5);
897
898 if (!dry_run) {
902 }
903 return q;
904 }
905
906 // ----------------------------------------------
907 // 2-Pass code
908
910 {
913 int i, toobig;
915 double complexity[5] = { 0 }; // approximate bits at quant=1
916 uint64_t const_bits[5] = { 0 }; // quantizer independent bits
917 uint64_t all_const_bits;
918 uint64_t all_available_bits = (uint64_t)(s->
bit_rate *
920 double rate_factor = 0;
921 double step;
922 const int filter_size = (int)(a->
qblur * 4) | 1;
923 double expected_bits = 0; // init to silence gcc warning
924 double *qscale, *blurred_qscale, qscale_sum;
925
926 /* find complexity & const_bits & decide the pict_types */
929
935
939 }
940
944
945 if (all_available_bits < all_const_bits) {
947 return -1;
948 }
949
952 toobig = 0;
953
954 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
955 expected_bits = 0;
956 rate_factor += step;
957
959
960 /* find qscale */
963
966 }
967 assert(filter_size % 2 == 1);
968
969 /* fixed I/B QP relative to P mode */
972
974 }
975
978
980 }
981
982 /* smooth curve */
986 int j;
987 double q = 0.0, sum = 0.0;
988
989 for (j = 0; j < filter_size; j++) {
990 int index = i + j - filter_size / 2;
991 double d = index - i;
993
995 continue;
997 continue;
1000 }
1001 blurred_qscale[i] = q / sum;
1002 }
1003
1004 /* find expected bits */
1008
1010
1013
1015 expected_bits +=
bits;
1016 }
1017
1019 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
1020 expected_bits, (int)all_available_bits, rate_factor);
1021 if (expected_bits > all_available_bits) {
1022 rate_factor -= step;
1023 ++toobig;
1024 }
1025 }
1028
1029 /* check bitrate calculations and print info */
1030 qscale_sum = 0.0;
1032 av_dlog(s,
"[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
1033 i,
1038 }
1039 assert(toobig <= 40);
1041 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
1043 (
int)(expected_bits / ((
double)all_available_bits / s->
bit_rate)));
1045 "[lavc rc] estimated target average qp: %.3f\n",
1047 if (toobig == 0) {
1049 "[lavc rc] Using all of requested bitrate is not "
1050 "necessary for this video with these parameters.\n");
1051 } else if (toobig == 40) {
1053 "[lavc rc] Error: bitrate too low for this video "
1054 "with these parameters.\n");
1055 return -1;
1056 } else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
1058 "[lavc rc] Error: 2pass curve failed to converge\n");
1059 return -1;
1060 }
1061
1062 return 0;
1063 }