33 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them. 37 #define M_E 2.718281828 42 double rate_factor,
int frame_num);
47 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d " 48 "fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d;\n",
119 static double (*
const func1[])(
void *, double) = {
124 static const char *
const func1_names[] = {
140 const_names, func1_names, func1,
141 NULL, NULL, 0, s->
avctx);
147 for (i = 0; i < 5; i++) {
171 p = strchr(p + 1,
';');
197 next = strchr(p,
';');
202 e = sscanf(p,
" in:%d ", &picture_number);
204 assert(picture_number >= 0);
205 assert(picture_number < rcc->num_entries);
206 rce = &rcc->
entry[picture_number];
208 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",
216 "statistics are damaged at line %d, parser out=%d\n",
233 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
252 for (i = 0; i < 60 * 30; i++) {
256 if (i % ((s->
gop_size + 3) / 4) == 0)
322 buffer_size, rcc->
buffer_index, frame_size, min_rate, max_rate);
337 int stuffing = ceil((rcc->
buffer_index - buffer_size) / 8);
356 double rate_factor,
int frame_num)
361 const double mb_num = s->
mb_num;
385 rcc->next_non_b_qscale,
412 if (rco[i].end_frame < frame_num)
416 bits =
qp2bits(rce, rco[i].qscale);
456 if (q > last_q + maxdiff)
457 q = last_q + maxdiff;
458 else if (q < last_q - maxdiff)
459 q = last_q - maxdiff;
478 assert(qmin <= qmax);
502 double q,
int frame_num)
526 double d = 2 * (buffer_size - expected_size) / buffer_size;
540 "limiting QP %f -> %f\n", q, q_limit);
546 double d = 2 * expected_size / buffer_size;
560 "limiting QP %f -> %f\n", q, q_limit);
565 av_dlog(s,
"q:%f max:%f min:%f size:%f index:%f agr:%f\n",
574 double min2 = log(qmin);
575 double max2 = log(qmax);
578 q = (q - min2) / (max2 - min2) - 0.5;
580 q = 1.0 / (1.0 +
exp(q));
581 q = q * (max2 - min2) + min2;
599 double new_coeff = size * q / (var + 1);
606 p->
coeff += new_coeff;
618 float bits_sum = 0.0;
619 float cplx_sum = 0.0;
628 for (i = 0; i < s->
mb_num; i++) {
632 const int lumi = pic->
mb_mean[mb_xy];
637 float mb_factor = 0.0;
645 factor = 1.0 + p_masking;
648 factor = pow(temp_cplx, -temp_cplx_masking);
650 factor *= pow(spat_cplx, -spatial_cplx_masking);
653 factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
655 factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
657 if (mb_x < mb_width / 5) {
658 mb_distance = mb_width / 5 - mb_x;
659 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
660 }
else if (mb_x > 4 * mb_width / 5) {
661 mb_distance = mb_x - 4 * mb_width / 5;
662 mb_factor = (float)mb_distance / (
float)(mb_width / 5);
664 if (mb_y < mb_height / 5) {
665 mb_distance = mb_height / 5 - mb_y;
666 mb_factor =
FFMAX(mb_factor,
667 (
float)mb_distance / (
float)(mb_height / 5));
668 }
else if (mb_y > 4 * mb_height / 5) {
669 mb_distance = mb_y - 4 * mb_height / 5;
670 mb_factor =
FFMAX(mb_factor,
671 (
float)mb_distance / (
float)(mb_height / 5));
674 factor *= 1.0 - border_masking * mb_factor;
676 if (factor < 0.00001)
688 float factor = bits_sum / cplx_sum;
689 for (i = 0; i < s->
mb_num; i++) {
690 float newq = q * cplx_tab[
i] / bits_tab[
i];
694 bits_sum -= bits_tab[
i];
695 cplx_sum -= cplx_tab[
i] * q / qmax;
696 }
else if (newq < qmin) {
697 bits_sum -= bits_tab[
i];
698 cplx_sum -= cplx_tab[
i] * q / qmin;
701 if (bits_sum < 0.001)
703 if (cplx_sum < 0.001)
707 for (i = 0; i < s->
mb_num; i++) {
709 float newq = q * cplx_tab[
i] / bits_tab[
i];
713 newq *= bits_sum / cplx_sum;
716 intq = (int)(newq + 0.5);
720 else if (intq < qmin)
741 float br_compensation;
767 if (picture_number > 2 && !dry_run) {
778 assert(picture_number >= 0);
783 rce = &rcc->
entry[picture_number];
798 wanted_bits = (uint64_t)(s->
bit_rate * (
double)picture_number / fps);
800 wanted_bits = (uint64_t)(s->
bit_rate * (
double)dts_pic->
f.
pts / fps);
805 if (br_compensation <= 0.0)
806 br_compensation = 0.001;
817 br_compensation, s->
frame_bits, var, pict_type);
849 q =
get_qscale(s, rce, rate_factor, picture_number);
877 "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f " 878 "size:%d var:%d/%d br:%d fps:%d\n",
880 qmin, q, qmax, picture_number,
881 (
int)wanted_bits / 1000, (
int)s->
total_bits / 1000,
914 double complexity[5] = { 0 };
915 uint64_t const_bits[5] = { 0 };
916 uint64_t all_const_bits;
917 uint64_t all_available_bits = (uint64_t)(s->
bit_rate *
919 double rate_factor = 0;
921 const int filter_size = (int)(a->
qblur * 4) | 1;
922 double expected_bits;
923 double *qscale, *blurred_qscale, qscale_sum;
944 if (all_available_bits < all_const_bits) {
953 for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
966 assert(filter_size % 2 == 1);
986 double q = 0.0, sum = 0.0;
988 for (j = 0; j < filter_size; j++) {
989 int index = i + j - filter_size / 2;
990 double d = index -
i;
1000 blurred_qscale[
i] = q / sum;
1014 expected_bits +=
bits;
1018 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
1019 expected_bits, (
int)all_available_bits, rate_factor);
1020 if (expected_bits > all_available_bits) {
1021 rate_factor -=
step;
1031 av_dlog(s,
"[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
1038 assert(toobig <= 40);
1040 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
1042 (
int)(expected_bits / ((
double)all_available_bits / s->
bit_rate)));
1044 "[lavc rc] estimated target average qp: %.3f\n",
1048 "[lavc rc] Using all of requested bitrate is not " 1049 "necessary for this video with these parameters.\n");
1050 }
else if (toobig == 40) {
1052 "[lavc rc] Error: bitrate too low for this video " 1053 "with these parameters.\n");
1055 }
else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
1057 "[lavc rc] Error: 2pass curve failed to converge\n");
int frame_bits
bits used for the current frame
void * av_mallocz(size_t size)
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
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.
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
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
#define CANDIDATE_MB_TYPE_INTRA
void ff_get_2pass_fcode(MpegEncContext *s)
static void update_predictor(Predictor *p, double q, double var, double size)
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
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)
int mb_num
number of MBs of a picture
static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type)
static av_always_inline av_const int isnan(float x)
int misc_bits
cbp, mb_type
Picture current_picture
buffer to store the decompressed current picture
static double av_q2d(AVRational a)
static const double const_values[]
char av_get_picture_type_char(enum AVPictureType pict_type)
int mb_height
number of MBs horizontally & vertically
int num_entries
number of RateControlEntries
int intra_only
if true, only intra pictures are generated
static const uint8_t frame_size[4]
static void adaptive_quantization(MpegEncContext *s, double q)
void ff_write_pass1_stats(MpegEncContext *s)
static double predict_size(Predictor *p, double q, double var)
#define FF_RC_STRATEGY_XVID
void ff_rate_control_uninit(MpegEncContext *s)
void av_log(void *avcl, int level, const char *fmt,...)
uint16_t * mb_type
Table for candidate MB types for encoding.
int low_delay
no reordering needed / has no b-frames
int ff_xvid_rate_control_init(MpegEncContext *s)
static const char *const const_names[]
int display_picture_number
Picture * current_picture_ptr
pointer to the current picture
float rc_max_available_vbv_use
float rc_min_vbv_overflow_use
float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
#define diff(a, as, b, bs)
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
void ff_xvid_rate_control_uninit(MpegEncContext *s)
uint16_t * mc_mb_var
Table for motion compensated MB variances.
#define CODEC_FLAG_NORMALIZE_AQP
Normalize adaptive quantization.
double buffer_index
amount of bits in the video/audio buffer
void av_expr_free(AVExpr *e)
float spatial_cplx_masking
void * av_malloc(size_t size)
int stuffing_bits
bits used for stuffing
float rc_buffer_aggressivity
synthesis window for stochastic i
static double get_fps(AVCodecContext *avctx)
static const int factor[16]
static const double coeff[2][5]
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, ...
int bit_rate
wanted bit rate
float temporal_cplx_masking
double short_term_qsum
sum of recent qscales
struct AVCodecContext * avctx
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.
int ff_rate_control_init(MpegEncContext *s)
int ff_vbv_update(MpegEncContext *s, int frame_size)
static double bits2qp(RateControlEntry *rce, double bits)
static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num)
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q)
int mb_var_sum
sum of MB variance for current frame
int flags
AVCodecContext.flags (HQ, MV4, ...)
int mc_mb_var_sum
motion compensated MB variance for current frame
Note except for filters that can have queued request_frame does not push and as a the filter_frame method will be called and do the work Legacy the filter_frame method was it was made of start_frame
int b_code
backward MV resolution for B Frames (mpeg4)
double short_term_qcount
count of recent qscales
#define AV_NOPTS_VALUE
Undefined timestamp value.
float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run)
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
static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num)