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",
92 static const char *
const const_names[] = {
121 static double (*
const func1[])(
void *, double) = {
142 const_names, func1_names,
func1,
149 #if FF_API_RC_STRATEGY
156 for (i = 0; i < 5; i++) {
180 p = strchr(p + 1,
';');
208 next = strchr(p,
';');
213 e = sscanf(p,
" in:%d ", &picture_number);
216 av_assert0(picture_number < rcc->num_entries);
217 rce = &rcc->
entry[picture_number];
219 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",
227 "statistics are damaged at line %d, parser out=%d\n",
246 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
265 for (i = 0; i < 60 * 30; i++) {
269 if (i % ((s->
gop_size + 3) / 4) == 0)
335 buffer_size, rcc->
buffer_index, frame_size, min_rate, max_rate);
344 av_log(s->
avctx,
AV_LOG_ERROR,
"max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
353 int stuffing = ceil((rcc->
buffer_index - buffer_size) / 8);
372 double rate_factor,
int frame_num)
377 const double mb_num = s->
mb_num;
381 double const_values[] = {
401 rcc->next_non_b_qscale,
426 if (rco[i].start_frame > frame_num)
428 if (rco[i].end_frame < frame_num)
432 bits =
qp2bits(rce, rco[i].qscale);
472 if (q > last_q + maxdiff)
473 q = last_q + maxdiff;
474 else if (q < last_q - maxdiff)
475 q = last_q - maxdiff;
518 double q,
int frame_num)
542 double d = 2 * (buffer_size - expected_size) / buffer_size;
556 "limiting QP %f -> %f\n", q, q_limit);
562 double d = 2 * expected_size / buffer_size;
576 "limiting QP %f -> %f\n", q, q_limit);
581 ff_dlog(s,
"q:%f max:%f min:%f size:%f index:%f agr:%f\n",
590 double min2 = log(qmin);
591 double max2 = log(qmax);
594 q = (q - min2) / (max2 - min2) - 0.5;
596 q = 1.0 / (1.0 + exp(q));
597 q = q * (max2 - min2) + min2;
615 double new_coeff = size * q / (var + 1);
622 p->
coeff += new_coeff;
634 float bits_sum = 0.0;
635 float cplx_sum = 0.0;
644 for (i = 0; i < s->
mb_num; i++) {
646 float temp_cplx = sqrt(pic->
mc_mb_var[mb_xy]);
647 float spat_cplx = sqrt(pic->
mb_var[mb_xy]);
648 const int lumi = pic->
mb_mean[mb_xy];
653 float mb_factor = 0.0;
661 factor = 1.0 + p_masking;
664 factor = pow(temp_cplx, -temp_cplx_masking);
666 factor *= pow(spat_cplx, -spatial_cplx_masking);
669 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
671 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
673 if (mb_x < mb_width / 5) {
674 mb_distance = mb_width / 5 - mb_x;
675 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
676 }
else if (mb_x > 4 * mb_width / 5) {
677 mb_distance = mb_x - 4 * mb_width / 5;
678 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
680 if (mb_y < mb_height / 5) {
681 mb_distance = mb_height / 5 - mb_y;
682 mb_factor =
FFMAX(mb_factor,
683 (
float)mb_distance / (
float)(mb_height / 5));
684 }
else if (mb_y > 4 * mb_height / 5) {
685 mb_distance = mb_y - 4 * mb_height / 5;
686 mb_factor =
FFMAX(mb_factor,
687 (
float)mb_distance / (
float)(mb_height / 5));
690 factor *= 1.0 - border_masking * mb_factor;
692 if (factor < 0.00001)
704 float factor = bits_sum / cplx_sum;
705 for (i = 0; i < s->
mb_num; i++) {
706 float newq = q * cplx_tab[i] / bits_tab[i];
710 bits_sum -= bits_tab[i];
711 cplx_sum -= cplx_tab[i] * q / qmax;
712 }
else if (newq < qmin) {
713 bits_sum -= bits_tab[i];
714 cplx_sum -= cplx_tab[i] * q / qmin;
717 if (bits_sum < 0.001)
719 if (cplx_sum < 0.001)
723 for (i = 0; i < s->
mb_num; i++) {
725 float newq = q * cplx_tab[i] / bits_tab[i];
729 newq *= bits_sum / cplx_sum;
732 intq = (int)(newq + 0.5);
736 else if (intq < qmin)
757 float br_compensation;
782 if (picture_number > 2 && !dry_run) {
783 const int64_t last_var =
799 rce = &rcc->
entry[picture_number];
814 wanted_bits = (uint64_t)(s->
bit_rate * (
double)picture_number / fps);
816 wanted_bits = (uint64_t)(s->
bit_rate * (
double)dts_pic->
f->
pts / fps);
821 if (br_compensation <= 0.0)
822 br_compensation = 0.001;
833 br_compensation, s->
frame_bits, var, pict_type);
864 q =
get_qscale(s, rce, rate_factor, picture_number);
892 "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f "
893 "size:%d var:%"PRId64
"/%"PRId64
" br:%"PRId64
" fps:%d\n",
895 qmin, q, qmax, picture_number,
896 (
int)wanted_bits / 1000, (
int)s->
total_bits / 1000,
929 double complexity[5] = { 0 };
930 uint64_t const_bits[5] = { 0 };
931 uint64_t all_const_bits;
932 uint64_t all_available_bits = (uint64_t)(s->
bit_rate *
934 double rate_factor = 0;
936 const int filter_size = (int)(a->
qblur * 4) | 1;
937 double expected_bits = 0;
938 double *qscale, *blurred_qscale, qscale_sum;
959 if (all_available_bits < all_const_bits) {
966 if (!qscale || !blurred_qscale) {
973 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
1006 double q = 0.0, sum = 0.0;
1008 for (j = 0; j < filter_size; j++) {
1009 int index = i + j - filter_size / 2;
1010 double d = index - i;
1020 blurred_qscale[i] = q / sum;
1034 expected_bits +=
bits;
1038 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
1039 expected_bits, (
int)all_available_bits, rate_factor);
1040 if (expected_bits > all_available_bits) {
1041 rate_factor -= step;
1051 ff_dlog(s,
"[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
1060 "[lavc rc] requested bitrate: %"PRId64
" bps expected bitrate: %"PRId64
" bps\n",
1062 (int64_t)(expected_bits / ((
double)all_available_bits / s->
bit_rate)));
1064 "[lavc rc] estimated target average qp: %.3f\n",
1068 "[lavc rc] Using all of requested bitrate is not "
1069 "necessary for this video with these parameters.\n");
1070 }
else if (toobig == 40) {
1072 "[lavc rc] Error: bitrate too low for this video "
1073 "with these parameters.\n");
1075 }
else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
1077 "[lavc rc] Error: 2pass curve failed to converge\n");
int frame_bits
bits used for the current frame
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
av_cold void ff_rate_control_uninit(MpegEncContext *s)
static const char *const func1_names[]
double pass1_rc_eq_output_sum
sum of the output of the rc equation, this is used for normalization
uint8_t * mb_mean
Table for MB luminance.
float qblur
amount of qscale smoothing over time (0.0-1.0)
uint16_t * mb_var
Table for MB variances.
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
void ff_get_2pass_fcode(MpegEncContext *s)
int mb_lmin
minimum MB lagrange multipler
static void update_predictor(Predictor *p, double q, double var, double size)
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
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.
int mpv_flags
flags set by private options
int mb_num
number of MBs of a picture
float i_quant_offset
qscale offset between P and I-frames
Macro definitions for various function/variable attributes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type)
Get the qmin & qmax for pict_type.
float rc_buffer_aggressivity
float p_masking
p block masking (0-> disabled)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
attribute_deprecated int rc_strategy
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
int mb_lmax
maximum MB lagrange multipler
static av_always_inline av_const int isnan(float x)
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...
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int misc_bits
cbp, mb_type
Picture current_picture
copy of the current picture structure.
static double av_q2d(AVRational a)
Convert rational to double.
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
int mb_height
number of MBs horizontally & vertically
float lumi_masking
luminance masking (0-> disabled)
char * stats_out
pass1 encoding statistics output buffer
int num_entries
number of RateControlEntries
int intra_only
if true, only intra pictures are generated
static void adaptive_quantization(MpegEncContext *s, double q)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static double(*const func1[])(void *, double)
av_cold int ff_rate_control_init(MpegEncContext *s)
int qmax
maximum quantizer
void ff_write_pass1_stats(MpegEncContext *s)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static double predict_size(Predictor *p, double q, double var)
int flags
AV_CODEC_FLAG_*.
int rc_max_rate
maximum bitrate
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
int low_delay
no reordering needed / has no b-frames
Libavcodec external API header.
av_cold void ff_xvid_rate_control_uninit(MpegEncContext *s)
int rc_buffer_size
decoder bitstream buffer size
common internal API header
int rc_override_count
ratecontrol override, see RcOverride
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
int display_picture_number
picture number in display order
Picture * current_picture_ptr
pointer to the current picture
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow. ...
float rc_min_vbv_overflow_use
Ratecontrol attempt to use, at least, times the amount needed to prevent a vbv overflow.
float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
int max_qdiff
maximum quantizer difference between frames
double pass1_wanted_bits
bits which should have been outputed by the pass1 code (including complexity init) ...
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
uint16_t * mc_mb_var
Table for motion compensated MB variances.
int coded_picture_number
picture number in bitstream order
#define AV_LOG_INFO
Standard information.
double buffer_index
amount of bits in the video/audio buffer
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
main external API structure.
int qmin
minimum quantizer
int64_t last_mc_mb_var_sum
float spatial_cplx_masking
spatial complexity masking (0-> disabled)
int stuffing_bits
bits used for stuffing
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
static double get_fps(AVCodecContext *avctx)
static const int factor[16]
float b_quant_offset
qscale offset between IP and B-frames
int f_code
forward MV resolution
double last_qscale_for[5]
last qscale for a specific pict type, used for max_diff & ipb factor stuff
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
int max_b_frames
max number of b-frames for encoding
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
float dark_masking
darkness masking (0-> disabled)
float temporal_cplx_masking
temporary complexity masking (0-> disabled)
double short_term_qsum
sum of recent qscales
struct AVCodecContext * avctx
av_cold int ff_xvid_rate_control_init(MpegEncContext *s)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
static int init_pass2(MpegEncContext *s)
static double qp2bits(RateControlEntry *rce, double qp)
int adaptive_quant
use adaptive quantization
Picture * last_picture_ptr
pointer to the previous picture.
float rc_qsquish
ratecontrol qmin qmax limiting method 0-> clipping, 1-> use a nice continuous function to limit qscal...
int ff_vbv_update(MpegEncContext *s, int frame_size)
static double bits2qp(RateControlEntry *rce, double bits)
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define CANDIDATE_MB_TYPE_INTRA
static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num)
Modify the bitrate curve from pass1 for one frame.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_ENABLE_DEPRECATION_WARNINGS
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
int64_t bit_rate
wanted bit rate
static const double coeff[2][5]
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q)
#define av_malloc_array(a, b)
int rc_min_rate
minimum bitrate
int b_code
backward MV resolution for B Frames (mpeg4)
int64_t mb_var_sum
sum of MB variance for current frame
double short_term_qcount
count of recent qscales
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define AV_NOPTS_VALUE
Undefined timestamp value.
float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run)
simple arithmetic expression evaluator
static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num)