41 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
42 "fcode:%d bcode:%d mc-var:%"PRId64
" var:%"PRId64
" icount:%d hbits:%d;\n",
43 s->c.cur_pic.ptr->display_picture_number,
44 s->c.cur_pic.ptr->coded_picture_number,
46 s->c.cur_pic.ptr->f->quality,
109 q = last_p_q *
FFABS(
a->i_quant_factor) +
a->i_quant_offset;
111 a->b_quant_factor > 0.0)
112 q = last_non_b_q *
a->b_quant_factor +
a->b_quant_offset;
121 if (q > last_q + maxdiff)
122 q = last_q + maxdiff;
123 else if (q < last_q - maxdiff)
124 q = last_q - maxdiff;
148 qmin = (int)(qmin *
FFABS(
s->c.avctx->b_quant_factor) +
s->c.avctx->b_quant_offset + 0.5);
149 qmax = (int)(qmax *
FFABS(
s->c.avctx->b_quant_factor) +
s->c.avctx->b_quant_offset + 0.5);
152 qmin = (int)(qmin *
FFABS(
s->c.avctx->i_quant_factor) +
s->c.avctx->i_quant_offset + 0.5);
153 qmax = (int)(qmax *
FFABS(
s->c.avctx->i_quant_factor) +
s->c.avctx->i_quant_offset + 0.5);
168 double q,
int frame_num)
172 const double buffer_size =
s->c.avctx->rc_buffer_size;
173 const double fps =
get_fps(
s->c.avctx);
174 const double min_rate =
s->c.avctx->rc_min_rate / fps;
175 const double max_rate =
s->c.avctx->rc_max_rate / fps;
193 double d = 2 * (buffer_size - expected_size) / buffer_size;
202 s->c.avctx->rc_min_vbv_overflow_use, 1));
207 "limiting QP %f -> %f\n", q, q_limit);
213 double d = 2 * expected_size / buffer_size;
222 s->c.avctx->rc_max_available_vbv_use,
227 "limiting QP %f -> %f\n", q, q_limit);
232 ff_dlog(
s->c.avctx,
"q:%f max:%f min:%f size:%f index:%f agr:%f\n",
235 if (rcc->
qsquish == 0.0 || qmin == qmax) {
241 double min2 = log(qmin);
242 double max2 = log(qmax);
245 q = (q - min2) / (max2 - min2) - 0.5;
247 q = 1.0 / (1.0 +
exp(q));
248 q = q * (max2 - min2) + min2;
260 double rate_factor,
int frame_num)
266 const double mb_num =
s->c.mb_num;
309 if (rco[
i].start_frame > frame_num)
311 if (rco[
i].end_frame < frame_num)
340 double complexity[5] = { 0 };
341 uint64_t const_bits[5] = { 0 };
342 uint64_t all_const_bits;
346 double rate_factor = 0;
348 const int filter_size = (int)(avctx->
qblur * 4) | 1;
349 double expected_bits = 0;
350 double *qscale, *blurred_qscale, qscale_sum;
371 if (all_available_bits < all_const_bits) {
378 if (!qscale || !blurred_qscale) {
385 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
418 double q = 0.0, sum = 0.0;
420 for (j = 0; j < filter_size; j++) {
421 int index =
i + j - filter_size / 2;
432 blurred_qscale[
i] = q / sum;
446 expected_bits +=
bits;
450 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
451 expected_bits, (
int)all_available_bits, rate_factor);
452 if (expected_bits > all_available_bits) {
463 ff_dlog(avctx,
"[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
472 "[lavc rc] requested bitrate: %"PRId64
" bps expected bitrate: %"PRId64
" bps\n",
474 (
int64_t)(expected_bits / ((
double)all_available_bits / m->
bit_rate)));
476 "[lavc rc] estimated target average qp: %.3f\n",
480 "[lavc rc] Using all of requested bitrate is not "
481 "necessary for this video with these parameters.\n");
482 }
else if (toobig == 40) {
484 "[lavc rc] Error: bitrate too low for this video "
485 "with these parameters.\n");
487 }
else if (
fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
489 "[lavc rc] Error: 2pass curve failed to converge\n");
552 for (
i = 0;
i < 5;
i++) {
576 p = strchr(p + 1,
';');
604 next = strchr(p,
';');
609 e = sscanf(p,
" in:%d ", &picture_number);
612 av_assert0(picture_number < rcc->num_entries);
613 rce = &rcc->
entry[picture_number];
615 e += sscanf(p,
" in:%*d out:%*d type:%d q:%f itex:%d ptex:%d "
618 "mc-var:%"SCNd64
" var:%"SCNd64
" "
619 "icount:%d hbits:%d",
627 "statistics are damaged at line %d, parser out=%d\n",
647 if (avctx->
qblur > 1.0) {
653 for (
i = 0;
i < 60 * 30;
i++) {
697 if (
s->adaptive_quant) {
698 unsigned mb_array_size =
s->c.mb_stride *
s->c.mb_height;
723 const double fps =
get_fps(avctx);
728 ff_dlog(avctx,
"%d %f %d %f %f\n",
738 av_log(avctx,
AV_LOG_ERROR,
"max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
764 return p->coeff * var / (q * p->count);
769 double new_coeff =
size * q / (var + 1);
773 p->count *= p->decay;
774 p->coeff *= p->decay;
776 p->coeff += new_coeff;
783 const float lumi_masking =
s->c.avctx->lumi_masking / (128.0 * 128.0);
784 const float dark_masking =
s->c.avctx->dark_masking / (128.0 * 128.0);
785 const float temp_cplx_masking =
s->c.avctx->temporal_cplx_masking;
786 const float spatial_cplx_masking =
s->c.avctx->spatial_cplx_masking;
787 const float p_masking =
s->c.avctx->p_masking;
789 float bits_sum = 0.0;
790 float cplx_sum = 0.0;
793 const int qmin =
s->c.avctx->mb_lmin;
794 const int qmax =
s->c.avctx->mb_lmax;
795 const int mb_width =
s->c.mb_width;
796 const int mb_height =
s->c.mb_height;
798 for (
int i = 0;
i <
s->c.mb_num;
i++) {
799 const int mb_xy =
s->c.mb_index2xy[
i];
800 float temp_cplx = sqrt(
s->mc_mb_var[mb_xy]);
801 float spat_cplx = sqrt(
s->mb_var[mb_xy]);
802 const int lumi =
s->mb_mean[mb_xy];
804 int mb_x = mb_xy %
s->c.mb_stride;
805 int mb_y = mb_xy /
s->c.mb_stride;
807 float mb_factor = 0.0;
818 factor = pow(temp_cplx, -temp_cplx_masking);
820 factor *= pow(spat_cplx, -spatial_cplx_masking);
823 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
825 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
827 if (mb_x < mb_width / 5) {
828 mb_distance = mb_width / 5 - mb_x;
829 mb_factor = (
float)mb_distance / (
float)(mb_width / 5);
830 }
else if (mb_x > 4 * mb_width / 5) {
831 mb_distance = mb_x - 4 * mb_width / 5;
832 mb_factor = (
float)mb_distance / (
float)(mb_width / 5);
834 if (mb_y < mb_height / 5) {
835 mb_distance = mb_height / 5 - mb_y;
836 mb_factor =
FFMAX(mb_factor,
837 (
float)mb_distance / (
float)(mb_height / 5));
838 }
else if (mb_y > 4 * mb_height / 5) {
839 mb_distance = mb_y - 4 * mb_height / 5;
840 mb_factor =
FFMAX(mb_factor,
841 (
float)mb_distance / (
float)(mb_height / 5));
844 factor *= 1.0 - border_masking * mb_factor;
858 float factor = bits_sum / cplx_sum;
859 for (
int i = 0;
i <
s->c.mb_num;
i++) {
860 float newq = q * cplx_tab[
i] / bits_tab[
i];
864 bits_sum -= bits_tab[
i];
865 cplx_sum -= cplx_tab[
i] * q / qmax;
866 }
else if (newq < qmin) {
867 bits_sum -= bits_tab[
i];
868 cplx_sum -= cplx_tab[
i] * q / qmin;
871 if (bits_sum < 0.001)
873 if (cplx_sum < 0.001)
877 for (
int i = 0;
i <
s->c.mb_num;
i++) {
878 const int mb_xy =
s->c.mb_index2xy[
i];
879 float newq = q * cplx_tab[
i] / bits_tab[
i];
883 newq *= bits_sum / cplx_sum;
886 intq = (int)(newq + 0.5);
890 else if (intq < qmin)
892 s->lambda_table[mb_xy] = intq;
915 float br_compensation;
919 int picture_number =
s->picture_number;
925 const int pict_type =
s->c.pict_type;
931 if (picture_number > 2 && !dry_run) {
948 rce = &rcc->
entry[picture_number];
952 double wanted_bits_double;
959 dts_pic =
s->c.cur_pic.ptr;
961 dts_pic =
s->c.last_pic.ptr;
967 if (wanted_bits_double > INT64_MAX) {
969 wanted_bits = INT64_MAX;
971 wanted_bits = (
int64_t)wanted_bits_double;
975 br_compensation = (
a->bit_rate_tolerance -
diff) /
a->bit_rate_tolerance;
976 if (br_compensation <= 0.0)
977 br_compensation = 0.001;
988 br_compensation, m->
frame_bits, var, pict_type);
1019 q =
get_qscale(m, rce, rate_factor, picture_number);
1047 "%c qp:%d<%2.1f<%d %d want:%"PRId64
" total:%"PRId64
" comp:%f st_q:%2.2f "
1048 "size:%d var:%"PRId64
"/%"PRId64
" br:%"PRId64
" fps:%d\n",
1050 qmin, q, qmax, picture_number,
1052 br_compensation, short_term_q, m->
frame_bits,
1062 if (
s->adaptive_quant)
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static __device__ float ceil(float a)
static __device__ float fabs(float a)
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
simple arithmetic expression evaluator
static const uint8_t bits[8]
static const uint8_t frame_size[4]
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
#define AV_NOPTS_VALUE
Undefined timestamp value.
static const int factor[16]
Macro definitions for various function/variable attributes.
common internal API header
Memory handling functions.
#define CANDIDATE_MB_TYPE_INTRA
static double get_qscale(MPVMainEncContext *const m, RateControlEntry *rce, double rate_factor, int frame_num)
Modify the bitrate curve from pass1 for one frame.
static void update_predictor(Predictor *p, double q, double var, double size)
void ff_write_pass1_stats(MPVMainEncContext *const m)
static double get_fps(AVCodecContext *avctx)
static double qp2bits(const RateControlEntry *rce, double qp)
static double qp2bits_cb(void *rce, double qp)
static double predict_size(Predictor *p, double q, double var)
static AVRational get_fpsQ(AVCodecContext *avctx)
static void get_qminmax(int *qmin_ret, int *qmax_ret, MPVMainEncContext *const m, int pict_type)
Get the qmin & qmax for pict_type.
static void adaptive_quantization(RateControlContext *const rcc, MPVMainEncContext *const m, double q)
void ff_get_2pass_fcode(MPVMainEncContext *const m)
static double modify_qscale(MPVMainEncContext *const m, const RateControlEntry *rce, double q, int frame_num)
static double bits2qp(const RateControlEntry *rce, double bits)
av_cold void ff_rate_control_uninit(RateControlContext *rcc)
static int init_pass2(MPVMainEncContext *const m)
float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
int ff_vbv_update(MPVMainEncContext *m, int frame_size)
static double get_diff_limited_q(MPVMainEncContext *m, const RateControlEntry *rce, double q)
av_cold int ff_rate_control_init(MPVMainEncContext *const m)
static double bits2qp_cb(void *rce, double qp)
main external API structure.
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow.
int rc_buffer_size
decoder bitstream buffer size
float b_quant_offset
qscale offset between IP and B-frames
int qmin
minimum quantizer
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
int rc_override_count
ratecontrol override, see RcOverride
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
int64_t rc_max_rate
maximum bitrate
int qmax
maximum quantizer
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int flags
AV_CODEC_FLAG_*.
int64_t rc_min_rate
minimum bitrate
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
float qblur
amount of qscale smoothing over time (0.0-1.0)
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
float i_quant_offset
qscale offset between P and I-frames
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Rational number (pair of numerator and denominator).
int max_b_frames
max number of B-frames
int frame_bits
bits used for the current frame
int stuffing_bits
bits used for stuffing
MPVEncContext s
The main slicecontext.
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
int64_t mb_var_sum
sum of MB variance for current frame
int intra_only
if true, only intra pictures are generated
float qsquish
ratecontrol qmin qmax limiting method 0-> clipping, 1-> use a nice continuous function to limit qscal...
struct AVExpr * rc_eq_eval
double short_term_qcount
count of recent qscales
double buffer_index
amount of bits in the video/audio buffer
int num_entries
number of RateControlEntries
double pass1_rc_eq_output_sum
sum of the output of the rc equation, this is used for normalization
double pass1_wanted_bits
bits which should have been output by the pass1 code (including complexity init)
int64_t last_mc_mb_var_sum
double last_qscale_for[5]
last qscale for a specific pict type, used for max_diff & ipb factor stuff
double short_term_qsum
sum of recent qscales
float buffer_aggressivity
#define av_malloc_array(a, b)
static const double const_values[]
static const char *const const_names[]
static const double coeff[2][5]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double(*const func1[])(void *, double)
static const char *const func1_names[]