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 skipcount:%d hbits:%d;\n",
102 if (q > last_q + maxdiff)
103 q = last_q + maxdiff;
104 else if (q < last_q - maxdiff)
105 q = last_q - maxdiff;
148 double q,
int frame_num)
172 double d = 2 * (buffer_size - expected_size) / buffer_size;
186 "limiting QP %f -> %f\n", q, q_limit);
192 double d = 2 * expected_size / buffer_size;
206 "limiting QP %f -> %f\n", q, q_limit);
211 ff_dlog(s,
"q:%f max:%f min:%f size:%f index:%f agr:%f\n",
220 double min2 = log(qmin);
221 double max2 = log(qmax);
224 q = (q - min2) / (max2 - min2) - 0.5;
226 q = 1.0 / (1.0 +
exp(q));
227 q = q * (max2 - min2) + min2;
239 double rate_factor,
int frame_num)
244 const double mb_num = s->
mb_num;
287 if (rco[i].start_frame > frame_num)
289 if (rco[i].end_frame < frame_num)
293 bits =
qp2bits(rce, rco[i].qscale);
317 double complexity[5] = { 0 };
318 uint64_t const_bits[5] = { 0 };
319 uint64_t all_const_bits;
320 uint64_t all_available_bits = (uint64_t)(s->
bit_rate *
322 double rate_factor = 0;
324 const int filter_size = (
int)(a->
qblur * 4) | 1;
325 double expected_bits = 0;
326 double *qscale, *blurred_qscale, qscale_sum;
347 if (all_available_bits < all_const_bits) {
354 if (!qscale || !blurred_qscale) {
361 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
394 double q = 0.0, sum = 0.0;
396 for (j = 0; j < filter_size; j++) {
397 int index = i + j - filter_size / 2;
398 double d = index -
i;
408 blurred_qscale[
i] = q / sum;
422 expected_bits +=
bits;
426 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
427 expected_bits, (
int)all_available_bits, rate_factor);
428 if (expected_bits > all_available_bits) {
439 ff_dlog(s,
"[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
448 "[lavc rc] requested bitrate: %"PRId64
" bps expected bitrate: %"PRId64
" bps\n",
450 (int64_t)(expected_bits / ((
double)all_available_bits / s->
bit_rate)));
452 "[lavc rc] estimated target average qp: %.3f\n",
456 "[lavc rc] Using all of requested bitrate is not " 457 "necessary for this video with these parameters.\n");
458 }
else if (toobig == 40) {
460 "[lavc rc] Error: bitrate too low for this video " 461 "with these parameters.\n");
463 }
else if (
fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
465 "[lavc rc] Error: 2pass curve failed to converge\n");
499 static double (*
const func1[])(
void *, double) = {
500 (double (*)(
void *, double))
bits2qp,
501 (double (*)(
void *, double))
qp2bits,
520 const_names, func1_names,
func1,
521 NULL, NULL, 0, s->
avctx);
527 for (i = 0; i < 5; i++) {
551 p = strchr(p + 1,
';');
579 next = strchr(p,
';');
584 e = sscanf(p,
" in:%d ", &picture_number);
587 av_assert0(picture_number < rcc->num_entries);
588 rce = &rcc->
entry[picture_number];
590 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",
598 "statistics are damaged at line %d, parser out=%d\n",
625 for (i = 0; i < 60 * 30; i++) {
629 if (i % ((s->
gop_size + 3) / 4) == 0)
690 buffer_size, rcc->
buffer_index, frame_size, min_rate, max_rate);
699 av_log(s->
avctx,
AV_LOG_ERROR,
"max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
730 double new_coeff = size * q / (var + 1);
737 p->
coeff += new_coeff;
749 float bits_sum = 0.0;
750 float cplx_sum = 0.0;
759 for (i = 0; i < s->
mb_num; i++) {
761 float temp_cplx = sqrt(pic->
mc_mb_var[mb_xy]);
762 float spat_cplx = sqrt(pic->
mb_var[mb_xy]);
763 const int lumi = pic->
mb_mean[mb_xy];
768 float mb_factor = 0.0;
776 factor = 1.0 + p_masking;
779 factor = pow(temp_cplx, -temp_cplx_masking);
781 factor *= pow(spat_cplx, -spatial_cplx_masking);
784 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
786 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
788 if (mb_x < mb_width / 5) {
789 mb_distance = mb_width / 5 - mb_x;
790 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
791 }
else if (mb_x > 4 * mb_width / 5) {
792 mb_distance = mb_x - 4 * mb_width / 5;
793 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
795 if (mb_y < mb_height / 5) {
796 mb_distance = mb_height / 5 - mb_y;
797 mb_factor =
FFMAX(mb_factor,
798 (
float)mb_distance / (
float)(mb_height / 5));
799 }
else if (mb_y > 4 * mb_height / 5) {
800 mb_distance = mb_y - 4 * mb_height / 5;
801 mb_factor =
FFMAX(mb_factor,
802 (
float)mb_distance / (
float)(mb_height / 5));
805 factor *= 1.0 - border_masking * mb_factor;
807 if (factor < 0.00001)
819 float factor = bits_sum / cplx_sum;
820 for (i = 0; i < s->
mb_num; i++) {
821 float newq = q * cplx_tab[
i] / bits_tab[
i];
825 bits_sum -= bits_tab[
i];
826 cplx_sum -= cplx_tab[
i] * q / qmax;
827 }
else if (newq < qmin) {
828 bits_sum -= bits_tab[
i];
829 cplx_sum -= cplx_tab[
i] * q / qmin;
832 if (bits_sum < 0.001)
834 if (cplx_sum < 0.001)
838 for (i = 0; i < s->
mb_num; i++) {
840 float newq = q * cplx_tab[
i] / bits_tab[
i];
844 newq *= bits_sum / cplx_sum;
847 intq = (
int)(newq + 0.5);
851 else if (intq < qmin)
872 float br_compensation;
892 if (picture_number > 2 && !dry_run) {
893 const int64_t last_var =
909 rce = &rcc->
entry[picture_number];
924 wanted_bits = (uint64_t)(s->
bit_rate * (
double)picture_number / fps);
926 wanted_bits = (uint64_t)(s->
bit_rate * (
double)dts_pic->
f->
pts / fps);
931 if (br_compensation <= 0.0)
932 br_compensation = 0.001;
943 br_compensation, s->
frame_bits, var, pict_type);
974 q =
get_qscale(s, rce, rate_factor, picture_number);
1002 "%c qp:%d<%2.1f<%d %d want:%"PRId64
" total:%"PRId64
" comp:%f st_q:%2.2f " 1003 "size:%d var:%"PRId64
"/%"PRId64
" br:%"PRId64
" fps:%d\n",
1005 qmin, q, qmax, picture_number,
1007 br_compensation, short_term_q, s->
frame_bits,
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 multiplier
static void update_predictor(Predictor *p, double q, double var, double size)
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
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
static __device__ float ceil(float a)
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.
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
int mb_lmax
maximum MB Lagrange multiplier
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 an AVRational to a 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
static const double const_values[]
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)
static __device__ float fabs(float a)
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_*.
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
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
int rc_buffer_size
decoder bitstream buffer size
int64_t rc_min_rate
minimum bitrate
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 output 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
Libavcodec external API header.
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
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
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
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)
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)
static const char *const const_names[]
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int b_code
backward MV resolution for B-frames (MPEG-4)
int64_t mb_var_sum
sum of MB variance for current frame
double short_term_qcount
count of recent qscales
#define AV_NOPTS_VALUE
Undefined timestamp value.
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
int64_t rc_max_rate
maximum bitrate
simple arithmetic expression evaluator
static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num)