FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ratecontrol.c
Go to the documentation of this file.
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 
28 #include "libavutil/attributes.h"
29 #include "libavutil/internal.h"
30 
31 #include "avcodec.h"
32 #include "internal.h"
33 #include "ratecontrol.h"
34 #include "mpegutils.h"
35 #include "mpegvideo.h"
36 #include "libavutil/eval.h"
37 
38 static int init_pass2(MpegEncContext *s);
39 static double get_qscale(MpegEncContext *s, RateControlEntry *rce,
40  double rate_factor, int frame_num);
41 
43 {
44  snprintf(s->avctx->stats_out, 256,
45  "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
46  "fcode:%d bcode:%d mc-var:%"PRId64" var:%"PRId64" icount:%d skipcount:%d hbits:%d;\n",
49  s->pict_type,
51  s->i_tex_bits,
52  s->p_tex_bits,
53  s->mv_bits,
54  s->misc_bits,
55  s->f_code,
56  s->b_code,
59  s->i_count, s->skip_count,
60  s->header_bits);
61 }
62 
63 static double get_fps(AVCodecContext *avctx)
64 {
65  return 1.0 / av_q2d(avctx->time_base) / FFMAX(avctx->ticks_per_frame, 1);
66 }
67 
68 static inline double qp2bits(RateControlEntry *rce, double qp)
69 {
70  if (qp <= 0.0) {
71  av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
72  }
73  return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / qp;
74 }
75 
76 static inline double bits2qp(RateControlEntry *rce, double bits)
77 {
78  if (bits < 0.9) {
79  av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
80  }
81  return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits;
82 }
83 
85 {
86  RateControlContext *rcc = &s->rc_context;
87  int i, res;
88  static const char * const const_names[] = {
89  "PI",
90  "E",
91  "iTex",
92  "pTex",
93  "tex",
94  "mv",
95  "fCode",
96  "iCount",
97  "mcVar",
98  "var",
99  "isI",
100  "isP",
101  "isB",
102  "avgQP",
103  "qComp",
104 #if 0
105  "lastIQP",
106  "lastPQP",
107  "lastBQP",
108  "nextNonBQP",
109 #endif
110  "avgIITex",
111  "avgPITex",
112  "avgPPTex",
113  "avgBPTex",
114  "avgTex",
115  NULL
116  };
117  static double (* const func1[])(void *, double) = {
118  (void *)bits2qp,
119  (void *)qp2bits,
120  NULL
121  };
122  static const char * const func1_names[] = {
123  "bits2qp",
124  "qp2bits",
125  NULL
126  };
127  emms_c();
128 
130  if (s->avctx->rc_max_rate) {
131  s->avctx->rc_max_available_vbv_use = av_clipf(s->avctx->rc_max_rate/(s->avctx->rc_buffer_size*get_fps(s->avctx)), 1.0/3, 1.0);
132  } else
134  }
135 
136  res = av_expr_parse(&rcc->rc_eq_eval,
137  s->rc_eq ? s->rc_eq : "tex^qComp",
138  const_names, func1_names, func1,
139  NULL, NULL, 0, s->avctx);
140  if (res < 0) {
141  av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->rc_eq);
142  return res;
143  }
144 
145 #if FF_API_RC_STRATEGY
147  if (!s->rc_strategy)
148  s->rc_strategy = s->avctx->rc_strategy;
150 #endif
151 
152  for (i = 0; i < 5; i++) {
153  rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
154  rcc->pred[i].count = 1.0;
155  rcc->pred[i].decay = 0.4;
156 
157  rcc->i_cplx_sum [i] =
158  rcc->p_cplx_sum [i] =
159  rcc->mv_bits_sum[i] =
160  rcc->qscale_sum [i] =
161  rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such
162 
163  rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5;
164  }
166  if (!rcc->buffer_index)
167  rcc->buffer_index = s->avctx->rc_buffer_size * 3 / 4;
168 
169  if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
170  int i;
171  char *p;
172 
173  /* find number of pics */
174  p = s->avctx->stats_in;
175  for (i = -1; p; i++)
176  p = strchr(p + 1, ';');
177  i += s->max_b_frames;
178  if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry))
179  return -1;
180  rcc->entry = av_mallocz(i * sizeof(RateControlEntry));
181  if (!rcc->entry)
182  return AVERROR(ENOMEM);
183  rcc->num_entries = i;
184 
185  /* init all to skipped P-frames
186  * (with B-frames we might have a not encoded frame at the end FIXME) */
187  for (i = 0; i < rcc->num_entries; i++) {
188  RateControlEntry *rce = &rcc->entry[i];
189 
191  rce->qscale = rce->new_qscale = FF_QP2LAMBDA * 2;
192  rce->misc_bits = s->mb_num + 10;
193  rce->mb_var_sum = s->mb_num * 100;
194  }
195 
196  /* read stats */
197  p = s->avctx->stats_in;
198  for (i = 0; i < rcc->num_entries - s->max_b_frames; i++) {
199  RateControlEntry *rce;
200  int picture_number;
201  int e;
202  char *next;
203 
204  next = strchr(p, ';');
205  if (next) {
206  (*next) = 0; // sscanf is unbelievably slow on looong strings // FIXME copy / do not write
207  next++;
208  }
209  e = sscanf(p, " in:%d ", &picture_number);
210 
211  av_assert0(picture_number >= 0);
212  av_assert0(picture_number < rcc->num_entries);
213  rce = &rcc->entry[picture_number];
214 
215  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",
216  &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits,
217  &rce->mv_bits, &rce->misc_bits,
218  &rce->f_code, &rce->b_code,
219  &rce->mc_mb_var_sum, &rce->mb_var_sum,
220  &rce->i_count, &rce->skip_count, &rce->header_bits);
221  if (e != 14) {
223  "statistics are damaged at line %d, parser out=%d\n",
224  i, e);
225  return -1;
226  }
227 
228  p = next;
229  }
230 
231  if (init_pass2(s) < 0) {
233  return -1;
234  }
235 
236 #if FF_API_RC_STRATEGY
238 #endif
239 
240  // FIXME maybe move to end
242 #if CONFIG_LIBXVID
243  return ff_xvid_rate_control_init(s);
244 #else
246  "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
247  return -1;
248 #endif
249  }
250  }
251 
252  if (!(s->avctx->flags & AV_CODEC_FLAG_PASS2)) {
253  rcc->short_term_qsum = 0.001;
254  rcc->short_term_qcount = 0.001;
255 
256  rcc->pass1_rc_eq_output_sum = 0.001;
257  rcc->pass1_wanted_bits = 0.001;
258 
259  if (s->avctx->qblur > 1.0) {
260  av_log(s->avctx, AV_LOG_ERROR, "qblur too large\n");
261  return -1;
262  }
263  /* init stuff with the user specified complexity */
264  if (s->rc_initial_cplx) {
265  for (i = 0; i < 60 * 30; i++) {
266  double bits = s->rc_initial_cplx * (i / 10000.0 + 1.0) * s->mb_num;
267  RateControlEntry rce;
268 
269  if (i % ((s->gop_size + 3) / 4) == 0)
271  else if (i % (s->max_b_frames + 1))
273  else
275 
276  rce.new_pict_type = rce.pict_type;
277  rce.mc_mb_var_sum = bits * s->mb_num / 100000;
278  rce.mb_var_sum = s->mb_num;
279 
280  rce.qscale = FF_QP2LAMBDA * 2;
281  rce.f_code = 2;
282  rce.b_code = 1;
283  rce.misc_bits = 1;
284 
285  if (s->pict_type == AV_PICTURE_TYPE_I) {
286  rce.i_count = s->mb_num;
287  rce.i_tex_bits = bits;
288  rce.p_tex_bits = 0;
289  rce.mv_bits = 0;
290  } else {
291  rce.i_count = 0; // FIXME we do know this approx
292  rce.i_tex_bits = 0;
293  rce.p_tex_bits = bits * 0.9;
294  rce.mv_bits = bits * 0.1;
295  }
296  rcc->i_cplx_sum[rce.pict_type] += rce.i_tex_bits * rce.qscale;
297  rcc->p_cplx_sum[rce.pict_type] += rce.p_tex_bits * rce.qscale;
298  rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
299  rcc->frame_count[rce.pict_type]++;
300 
301  get_qscale(s, &rce, rcc->pass1_wanted_bits / rcc->pass1_rc_eq_output_sum, i);
302 
303  // FIXME misbehaves a little for variable fps
304  rcc->pass1_wanted_bits += s->bit_rate / get_fps(s->avctx);
305  }
306  }
307  }
308 
309  return 0;
310 }
311 
313 {
314  RateControlContext *rcc = &s->rc_context;
315  emms_c();
316 
317  av_expr_free(rcc->rc_eq_eval);
318  av_freep(&rcc->entry);
319 
320 #if CONFIG_LIBXVID
323 #endif
324 }
325 
327 {
328  RateControlContext *rcc = &s->rc_context;
329  const double fps = get_fps(s->avctx);
330  const int buffer_size = s->avctx->rc_buffer_size;
331  const double min_rate = s->avctx->rc_min_rate / fps;
332  const double max_rate = s->avctx->rc_max_rate / fps;
333 
334  ff_dlog(s, "%d %f %d %f %f\n",
335  buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
336 
337  if (buffer_size) {
338  int left;
339 
340  rcc->buffer_index -= frame_size;
341  if (rcc->buffer_index < 0) {
342  av_log(s->avctx, AV_LOG_ERROR, "rc buffer underflow\n");
343  if (frame_size > max_rate && s->qscale == s->avctx->qmax) {
344  av_log(s->avctx, AV_LOG_ERROR, "max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
345  }
346  rcc->buffer_index = 0;
347  }
348 
349  left = buffer_size - rcc->buffer_index - 1;
350  rcc->buffer_index += av_clip(left, min_rate, max_rate);
351 
352  if (rcc->buffer_index > buffer_size) {
353  int stuffing = ceil((rcc->buffer_index - buffer_size) / 8);
354 
355  if (stuffing < 4 && s->codec_id == AV_CODEC_ID_MPEG4)
356  stuffing = 4;
357  rcc->buffer_index -= 8 * stuffing;
358 
359  if (s->avctx->debug & FF_DEBUG_RC)
360  av_log(s->avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
361 
362  return stuffing;
363  }
364  }
365  return 0;
366 }
367 
368 /**
369  * Modify the bitrate curve from pass1 for one frame.
370  */
372  double rate_factor, int frame_num)
373 {
374  RateControlContext *rcc = &s->rc_context;
375  AVCodecContext *a = s->avctx;
376  const int pict_type = rce->new_pict_type;
377  const double mb_num = s->mb_num;
378  double q, bits;
379  int i;
380 
381  double const_values[] = {
382  M_PI,
383  M_E,
384  rce->i_tex_bits * rce->qscale,
385  rce->p_tex_bits * rce->qscale,
386  (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale,
387  rce->mv_bits / mb_num,
388  rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code) * 0.5 : rce->f_code,
389  rce->i_count / mb_num,
390  rce->mc_mb_var_sum / mb_num,
391  rce->mb_var_sum / mb_num,
395  rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
396  a->qcompress,
397 #if 0
401  rcc->next_non_b_qscale,
402 #endif
407  (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
408  0
409  };
410 
411  bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
412  if (isnan(bits)) {
413  av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", s->rc_eq);
414  return -1;
415  }
416 
418  bits *= rate_factor;
419  if (bits < 0.0)
420  bits = 0.0;
421  bits += 1.0; // avoid 1/0 issues
422 
423  /* user override */
424  for (i = 0; i < s->avctx->rc_override_count; i++) {
425  RcOverride *rco = s->avctx->rc_override;
426  if (rco[i].start_frame > frame_num)
427  continue;
428  if (rco[i].end_frame < frame_num)
429  continue;
430 
431  if (rco[i].qscale)
432  bits = qp2bits(rce, rco[i].qscale); // FIXME move at end to really force it?
433  else
434  bits *= rco[i].quality_factor;
435  }
436 
437  q = bits2qp(rce, bits);
438 
439  /* I/B difference */
440  if (pict_type == AV_PICTURE_TYPE_I && s->avctx->i_quant_factor < 0.0)
441  q = -q * s->avctx->i_quant_factor + s->avctx->i_quant_offset;
442  else if (pict_type == AV_PICTURE_TYPE_B && s->avctx->b_quant_factor < 0.0)
443  q = -q * s->avctx->b_quant_factor + s->avctx->b_quant_offset;
444  if (q < 1)
445  q = 1;
446 
447  return q;
448 }
449 
450 static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q)
451 {
452  RateControlContext *rcc = &s->rc_context;
453  AVCodecContext *a = s->avctx;
454  const int pict_type = rce->new_pict_type;
455  const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P];
456  const double last_non_b_q = rcc->last_qscale_for[rcc->last_non_b_pict_type];
457 
458  if (pict_type == AV_PICTURE_TYPE_I &&
460  q = last_p_q * FFABS(a->i_quant_factor) + a->i_quant_offset;
461  else if (pict_type == AV_PICTURE_TYPE_B &&
462  a->b_quant_factor > 0.0)
463  q = last_non_b_q * a->b_quant_factor + a->b_quant_offset;
464  if (q < 1)
465  q = 1;
466 
467  /* last qscale / qdiff stuff */
468  if (rcc->last_non_b_pict_type == pict_type || pict_type != AV_PICTURE_TYPE_I) {
469  double last_q = rcc->last_qscale_for[pict_type];
470  const int maxdiff = FF_QP2LAMBDA * a->max_qdiff;
471 
472  if (q > last_q + maxdiff)
473  q = last_q + maxdiff;
474  else if (q < last_q - maxdiff)
475  q = last_q - maxdiff;
476  }
477 
478  rcc->last_qscale_for[pict_type] = q; // Note we cannot do that after blurring
479 
480  if (pict_type != AV_PICTURE_TYPE_B)
481  rcc->last_non_b_pict_type = pict_type;
482 
483  return q;
484 }
485 
486 /**
487  * Get the qmin & qmax for pict_type.
488  */
489 static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type)
490 {
491  int qmin = s->lmin;
492  int qmax = s->lmax;
493 
494  av_assert0(qmin <= qmax);
495 
496  switch (pict_type) {
497  case AV_PICTURE_TYPE_B:
498  qmin = (int)(qmin * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5);
499  qmax = (int)(qmax * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5);
500  break;
501  case AV_PICTURE_TYPE_I:
502  qmin = (int)(qmin * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5);
503  qmax = (int)(qmax * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5);
504  break;
505  }
506 
507  qmin = av_clip(qmin, 1, FF_LAMBDA_MAX);
508  qmax = av_clip(qmax, 1, FF_LAMBDA_MAX);
509 
510  if (qmax < qmin)
511  qmax = qmin;
512 
513  *qmin_ret = qmin;
514  *qmax_ret = qmax;
515 }
516 
518  double q, int frame_num)
519 {
520  RateControlContext *rcc = &s->rc_context;
521  const double buffer_size = s->avctx->rc_buffer_size;
522  const double fps = get_fps(s->avctx);
523  const double min_rate = s->avctx->rc_min_rate / fps;
524  const double max_rate = s->avctx->rc_max_rate / fps;
525  const int pict_type = rce->new_pict_type;
526  int qmin, qmax;
527 
528  get_qminmax(&qmin, &qmax, s, pict_type);
529 
530  /* modulation */
531  if (s->rc_qmod_freq &&
532  frame_num % s->rc_qmod_freq == 0 &&
533  pict_type == AV_PICTURE_TYPE_P)
534  q *= s->rc_qmod_amp;
535 
536  /* buffer overflow/underflow protection */
537  if (buffer_size) {
538  double expected_size = rcc->buffer_index;
539  double q_limit;
540 
541  if (min_rate) {
542  double d = 2 * (buffer_size - expected_size) / buffer_size;
543  if (d > 1.0)
544  d = 1.0;
545  else if (d < 0.0001)
546  d = 0.0001;
547  q *= pow(d, 1.0 / s->rc_buffer_aggressivity);
548 
549  q_limit = bits2qp(rce,
550  FFMAX((min_rate - buffer_size + rcc->buffer_index) *
552 
553  if (q > q_limit) {
554  if (s->avctx->debug & FF_DEBUG_RC)
556  "limiting QP %f -> %f\n", q, q_limit);
557  q = q_limit;
558  }
559  }
560 
561  if (max_rate) {
562  double d = 2 * expected_size / buffer_size;
563  if (d > 1.0)
564  d = 1.0;
565  else if (d < 0.0001)
566  d = 0.0001;
567  q /= pow(d, 1.0 / s->rc_buffer_aggressivity);
568 
569  q_limit = bits2qp(rce,
570  FFMAX(rcc->buffer_index *
572  1));
573  if (q < q_limit) {
574  if (s->avctx->debug & FF_DEBUG_RC)
576  "limiting QP %f -> %f\n", q, q_limit);
577  q = q_limit;
578  }
579  }
580  }
581  ff_dlog(s, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",
582  q, max_rate, min_rate, buffer_size, rcc->buffer_index,
584  if (s->rc_qsquish == 0.0 || qmin == qmax) {
585  if (q < qmin)
586  q = qmin;
587  else if (q > qmax)
588  q = qmax;
589  } else {
590  double min2 = log(qmin);
591  double max2 = log(qmax);
592 
593  q = log(q);
594  q = (q - min2) / (max2 - min2) - 0.5;
595  q *= -4.0;
596  q = 1.0 / (1.0 + exp(q));
597  q = q * (max2 - min2) + min2;
598 
599  q = exp(q);
600  }
601 
602  return q;
603 }
604 
605 // ----------------------------------
606 // 1 Pass Code
607 
608 static double predict_size(Predictor *p, double q, double var)
609 {
610  return p->coeff * var / (q * p->count);
611 }
612 
613 static void update_predictor(Predictor *p, double q, double var, double size)
614 {
615  double new_coeff = size * q / (var + 1);
616  if (var < 10)
617  return;
618 
619  p->count *= p->decay;
620  p->coeff *= p->decay;
621  p->count++;
622  p->coeff += new_coeff;
623 }
624 
625 static void adaptive_quantization(MpegEncContext *s, double q)
626 {
627  int i;
628  const float lumi_masking = s->avctx->lumi_masking / (128.0 * 128.0);
629  const float dark_masking = s->avctx->dark_masking / (128.0 * 128.0);
630  const float temp_cplx_masking = s->avctx->temporal_cplx_masking;
631  const float spatial_cplx_masking = s->avctx->spatial_cplx_masking;
632  const float p_masking = s->avctx->p_masking;
633  const float border_masking = s->border_masking;
634  float bits_sum = 0.0;
635  float cplx_sum = 0.0;
636  float *cplx_tab = s->cplx_tab;
637  float *bits_tab = s->bits_tab;
638  const int qmin = s->avctx->mb_lmin;
639  const int qmax = s->avctx->mb_lmax;
640  Picture *const pic = &s->current_picture;
641  const int mb_width = s->mb_width;
642  const int mb_height = s->mb_height;
643 
644  for (i = 0; i < s->mb_num; i++) {
645  const int mb_xy = s->mb_index2xy[i];
646  float temp_cplx = sqrt(pic->mc_mb_var[mb_xy]); // FIXME merge in pow()
647  float spat_cplx = sqrt(pic->mb_var[mb_xy]);
648  const int lumi = pic->mb_mean[mb_xy];
649  float bits, cplx, factor;
650  int mb_x = mb_xy % s->mb_stride;
651  int mb_y = mb_xy / s->mb_stride;
652  int mb_distance;
653  float mb_factor = 0.0;
654  if (spat_cplx < 4)
655  spat_cplx = 4; // FIXME fine-tune
656  if (temp_cplx < 4)
657  temp_cplx = 4; // FIXME fine-tune
658 
659  if ((s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTRA)) { // FIXME hq mode
660  cplx = spat_cplx;
661  factor = 1.0 + p_masking;
662  } else {
663  cplx = temp_cplx;
664  factor = pow(temp_cplx, -temp_cplx_masking);
665  }
666  factor *= pow(spat_cplx, -spatial_cplx_masking);
667 
668  if (lumi > 127)
669  factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
670  else
671  factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
672 
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);
679  }
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));
688  }
689 
690  factor *= 1.0 - border_masking * mb_factor;
691 
692  if (factor < 0.00001)
693  factor = 0.00001;
694 
695  bits = cplx * factor;
696  cplx_sum += cplx;
697  bits_sum += bits;
698  cplx_tab[i] = cplx;
699  bits_tab[i] = bits;
700  }
701 
702  /* handle qmin/qmax clipping */
703  if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
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];
707  newq *= factor;
708 
709  if (newq > qmax) {
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;
715  }
716  }
717  if (bits_sum < 0.001)
718  bits_sum = 0.001;
719  if (cplx_sum < 0.001)
720  cplx_sum = 0.001;
721  }
722 
723  for (i = 0; i < s->mb_num; i++) {
724  const int mb_xy = s->mb_index2xy[i];
725  float newq = q * cplx_tab[i] / bits_tab[i];
726  int intq;
727 
728  if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
729  newq *= bits_sum / cplx_sum;
730  }
731 
732  intq = (int)(newq + 0.5);
733 
734  if (intq > qmax)
735  intq = qmax;
736  else if (intq < qmin)
737  intq = qmin;
738  s->lambda_table[mb_xy] = intq;
739  }
740 }
741 
743 {
744  RateControlContext *rcc = &s->rc_context;
745  RateControlEntry *rce = &rcc->entry[s->picture_number];
746 
747  s->f_code = rce->f_code;
748  s->b_code = rce->b_code;
749 }
750 
751 // FIXME rd or at least approx for dquant
752 
754 {
755  float q;
756  int qmin, qmax;
757  float br_compensation;
758  double diff;
759  double short_term_q;
760  double fps;
761  int picture_number = s->picture_number;
762  int64_t wanted_bits;
763  RateControlContext *rcc = &s->rc_context;
764  AVCodecContext *a = s->avctx;
765  RateControlEntry local_rce, *rce;
766  double bits;
767  double rate_factor;
768  int64_t var;
769  const int pict_type = s->pict_type;
770  Picture * const pic = &s->current_picture;
771  emms_c();
772 
773 #if CONFIG_LIBXVID
775  return ff_xvid_rate_estimate_qscale(s, dry_run);
776 #endif
777 
778  get_qminmax(&qmin, &qmax, s, pict_type);
779 
780  fps = get_fps(s->avctx);
781  /* update predictors */
782  if (picture_number > 2 && !dry_run) {
783  const int64_t last_var =
785  : rcc->last_mc_mb_var_sum;
788  rcc->last_qscale,
789  sqrt(last_var),
790  s->frame_bits - s->stuffing_bits);
791  }
792 
793  if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
794  av_assert0(picture_number >= 0);
795  if (picture_number >= rcc->num_entries) {
796  av_log(s, AV_LOG_ERROR, "Input is longer than 2-pass log file\n");
797  return -1;
798  }
799  rce = &rcc->entry[picture_number];
800  wanted_bits = rce->expected_bits;
801  } else {
802  Picture *dts_pic;
803  rce = &local_rce;
804 
805  /* FIXME add a dts field to AVFrame and ensure it is set and use it
806  * here instead of reordering but the reordering is simpler for now
807  * until H.264 B-pyramid must be handled. */
808  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
809  dts_pic = s->current_picture_ptr;
810  else
811  dts_pic = s->last_picture_ptr;
812 
813  if (!dts_pic || dts_pic->f->pts == AV_NOPTS_VALUE)
814  wanted_bits = (uint64_t)(s->bit_rate * (double)picture_number / fps);
815  else
816  wanted_bits = (uint64_t)(s->bit_rate * (double)dts_pic->f->pts / fps);
817  }
818 
819  diff = s->total_bits - wanted_bits;
820  br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance;
821  if (br_compensation <= 0.0)
822  br_compensation = 0.001;
823 
824  var = pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum;
825 
826  short_term_q = 0; /* avoid warning */
827  if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
828  if (pict_type != AV_PICTURE_TYPE_I)
829  av_assert0(pict_type == rce->new_pict_type);
830 
831  q = rce->new_qscale / br_compensation;
832  ff_dlog(s, "%f %f %f last:%d var:%"PRId64" type:%d//\n", q, rce->new_qscale,
833  br_compensation, s->frame_bits, var, pict_type);
834  } else {
835  rce->pict_type =
836  rce->new_pict_type = pict_type;
837  rce->mc_mb_var_sum = pic->mc_mb_var_sum;
838  rce->mb_var_sum = pic->mb_var_sum;
839  rce->qscale = FF_QP2LAMBDA * 2;
840  rce->f_code = s->f_code;
841  rce->b_code = s->b_code;
842  rce->misc_bits = 1;
843 
844  bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
845  if (pict_type == AV_PICTURE_TYPE_I) {
846  rce->i_count = s->mb_num;
847  rce->i_tex_bits = bits;
848  rce->p_tex_bits = 0;
849  rce->mv_bits = 0;
850  } else {
851  rce->i_count = 0; // FIXME we do know this approx
852  rce->i_tex_bits = 0;
853  rce->p_tex_bits = bits * 0.9;
854  rce->mv_bits = bits * 0.1;
855  }
856  rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale;
857  rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale;
858  rcc->mv_bits_sum[pict_type] += rce->mv_bits;
859  rcc->frame_count[pict_type]++;
860 
861  rate_factor = rcc->pass1_wanted_bits /
862  rcc->pass1_rc_eq_output_sum * br_compensation;
863 
864  q = get_qscale(s, rce, rate_factor, picture_number);
865  if (q < 0)
866  return -1;
867 
868  av_assert0(q > 0.0);
869  q = get_diff_limited_q(s, rce, q);
870  av_assert0(q > 0.0);
871 
872  // FIXME type dependent blur like in 2-pass
873  if (pict_type == AV_PICTURE_TYPE_P || s->intra_only) {
874  rcc->short_term_qsum *= a->qblur;
875  rcc->short_term_qcount *= a->qblur;
876 
877  rcc->short_term_qsum += q;
878  rcc->short_term_qcount++;
879  q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount;
880  }
881  av_assert0(q > 0.0);
882 
883  q = modify_qscale(s, rce, q, picture_number);
884 
885  rcc->pass1_wanted_bits += s->bit_rate / fps;
886 
887  av_assert0(q > 0.0);
888  }
889 
890  if (s->avctx->debug & FF_DEBUG_RC) {
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",
894  av_get_picture_type_char(pict_type),
895  qmin, q, qmax, picture_number,
896  (int)wanted_bits / 1000, (int)s->total_bits / 1000,
897  br_compensation, short_term_q, s->frame_bits,
898  pic->mb_var_sum, pic->mc_mb_var_sum,
899  s->bit_rate / 1000, (int)fps);
900  }
901 
902  if (q < qmin)
903  q = qmin;
904  else if (q > qmax)
905  q = qmax;
906 
907  if (s->adaptive_quant)
908  adaptive_quantization(s, q);
909  else
910  q = (int)(q + 0.5);
911 
912  if (!dry_run) {
913  rcc->last_qscale = q;
914  rcc->last_mc_mb_var_sum = pic->mc_mb_var_sum;
915  rcc->last_mb_var_sum = pic->mb_var_sum;
916  }
917  return q;
918 }
919 
920 // ----------------------------------------------
921 // 2-Pass code
922 
924 {
925  RateControlContext *rcc = &s->rc_context;
926  AVCodecContext *a = s->avctx;
927  int i, toobig;
928  double fps = get_fps(s->avctx);
929  double complexity[5] = { 0 }; // approximate bits at quant=1
930  uint64_t const_bits[5] = { 0 }; // quantizer independent bits
931  uint64_t all_const_bits;
932  uint64_t all_available_bits = (uint64_t)(s->bit_rate *
933  (double)rcc->num_entries / fps);
934  double rate_factor = 0;
935  double step;
936  const int filter_size = (int)(a->qblur * 4) | 1;
937  double expected_bits = 0; // init to silence gcc warning
938  double *qscale, *blurred_qscale, qscale_sum;
939 
940  /* find complexity & const_bits & decide the pict_types */
941  for (i = 0; i < rcc->num_entries; i++) {
942  RateControlEntry *rce = &rcc->entry[i];
943 
944  rce->new_pict_type = rce->pict_type;
945  rcc->i_cplx_sum[rce->pict_type] += rce->i_tex_bits * rce->qscale;
946  rcc->p_cplx_sum[rce->pict_type] += rce->p_tex_bits * rce->qscale;
947  rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits;
948  rcc->frame_count[rce->pict_type]++;
949 
950  complexity[rce->new_pict_type] += (rce->i_tex_bits + rce->p_tex_bits) *
951  (double)rce->qscale;
952  const_bits[rce->new_pict_type] += rce->mv_bits + rce->misc_bits;
953  }
954 
955  all_const_bits = const_bits[AV_PICTURE_TYPE_I] +
956  const_bits[AV_PICTURE_TYPE_P] +
957  const_bits[AV_PICTURE_TYPE_B];
958 
959  if (all_available_bits < all_const_bits) {
960  av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n");
961  return -1;
962  }
963 
964  qscale = av_malloc_array(rcc->num_entries, sizeof(double));
965  blurred_qscale = av_malloc_array(rcc->num_entries, sizeof(double));
966  if (!qscale || !blurred_qscale) {
967  av_free(qscale);
968  av_free(blurred_qscale);
969  return AVERROR(ENOMEM);
970  }
971  toobig = 0;
972 
973  for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
974  expected_bits = 0;
975  rate_factor += step;
976 
977  rcc->buffer_index = s->avctx->rc_buffer_size / 2;
978 
979  /* find qscale */
980  for (i = 0; i < rcc->num_entries; i++) {
981  RateControlEntry *rce = &rcc->entry[i];
982 
983  qscale[i] = get_qscale(s, &rcc->entry[i], rate_factor, i);
984  rcc->last_qscale_for[rce->pict_type] = qscale[i];
985  }
986  av_assert0(filter_size % 2 == 1);
987 
988  /* fixed I/B QP relative to P mode */
989  for (i = FFMAX(0, rcc->num_entries - 300); i < rcc->num_entries; i++) {
990  RateControlEntry *rce = &rcc->entry[i];
991 
992  qscale[i] = get_diff_limited_q(s, rce, qscale[i]);
993  }
994 
995  for (i = rcc->num_entries - 1; i >= 0; i--) {
996  RateControlEntry *rce = &rcc->entry[i];
997 
998  qscale[i] = get_diff_limited_q(s, rce, qscale[i]);
999  }
1000 
1001  /* smooth curve */
1002  for (i = 0; i < rcc->num_entries; i++) {
1003  RateControlEntry *rce = &rcc->entry[i];
1004  const int pict_type = rce->new_pict_type;
1005  int j;
1006  double q = 0.0, sum = 0.0;
1007 
1008  for (j = 0; j < filter_size; j++) {
1009  int index = i + j - filter_size / 2;
1010  double d = index - i;
1011  double coeff = a->qblur == 0 ? 1.0 : exp(-d * d / (a->qblur * a->qblur));
1012 
1013  if (index < 0 || index >= rcc->num_entries)
1014  continue;
1015  if (pict_type != rcc->entry[index].new_pict_type)
1016  continue;
1017  q += qscale[index] * coeff;
1018  sum += coeff;
1019  }
1020  blurred_qscale[i] = q / sum;
1021  }
1022 
1023  /* find expected bits */
1024  for (i = 0; i < rcc->num_entries; i++) {
1025  RateControlEntry *rce = &rcc->entry[i];
1026  double bits;
1027 
1028  rce->new_qscale = modify_qscale(s, rce, blurred_qscale[i], i);
1029 
1030  bits = qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
1031  bits += 8 * ff_vbv_update(s, bits);
1032 
1033  rce->expected_bits = expected_bits;
1034  expected_bits += bits;
1035  }
1036 
1037  ff_dlog(s->avctx,
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;
1042  ++toobig;
1043  }
1044  }
1045  av_free(qscale);
1046  av_free(blurred_qscale);
1047 
1048  /* check bitrate calculations and print info */
1049  qscale_sum = 0.0;
1050  for (i = 0; i < rcc->num_entries; i++) {
1051  ff_dlog(s, "[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
1052  i,
1053  rcc->entry[i].new_qscale,
1054  rcc->entry[i].new_qscale / FF_QP2LAMBDA);
1055  qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA,
1056  s->avctx->qmin, s->avctx->qmax);
1057  }
1058  av_assert0(toobig <= 40);
1060  "[lavc rc] requested bitrate: %"PRId64" bps expected bitrate: %"PRId64" bps\n",
1061  s->bit_rate,
1062  (int64_t)(expected_bits / ((double)all_available_bits / s->bit_rate)));
1064  "[lavc rc] estimated target average qp: %.3f\n",
1065  (float)qscale_sum / rcc->num_entries);
1066  if (toobig == 0) {
1067  av_log(s->avctx, AV_LOG_INFO,
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");
1074  return -1;
1075  } else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
1077  "[lavc rc] Error: 2pass curve failed to converge\n");
1078  return -1;
1079  }
1080 
1081  return 0;
1082 }
int frame_bits
bits used for the current frame
Definition: mpegvideo.h:338
#define NULL
Definition: coverity.c:32
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
Definition: mpegvideo.h:341
av_cold void ff_rate_control_uninit(MpegEncContext *s)
Definition: ratecontrol.c:312
int picture_number
Definition: mpegvideo.h:124
const char * s
Definition: avisynth_c.h:631
enum AVCodecID codec_id
Definition: ffmpeg_vaapi.c:149
static const char *const func1_names[]
Definition: vf_rotate.c:195
rate control context.
Definition: ratecontrol.h:63
double pass1_rc_eq_output_sum
sum of the output of the rc equation, this is used for normalization
Definition: ratecontrol.h:70
#define FF_DEBUG_RC
Definition: avcodec.h:2898
uint8_t * mb_mean
Table for MB luminance.
Definition: mpegpicture.h:74
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:2592
RateControlEntry * entry
Definition: ratecontrol.h:65
uint16_t * mb_var
Table for MB variances.
Definition: mpegpicture.h:65
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:2695
void ff_get_2pass_fcode(MpegEncContext *s)
Definition: ratecontrol.c:742
int mb_lmin
minimum MB Lagrange multiplier
Definition: avcodec.h:2296
static void update_predictor(Predictor *p, double q, double var, double size)
Definition: ratecontrol.c:613
#define FF_LAMBDA_MAX
Definition: avutil.h:220
#define FF_MPV_FLAG_NAQ
Definition: mpegvideo.h:574
double count
Definition: ratecontrol.h:37
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2833
mpegvideo header.
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.
Definition: eval.c:658
int mpv_flags
flags set by private options
Definition: mpegvideo.h:525
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:130
int qscale
QP.
Definition: mpegvideo.h:201
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:2001
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...
Definition: avcodec.h:1794
static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type)
Get the qmin & qmax for pict_type.
Definition: ratecontrol.c:489
float rc_buffer_aggressivity
Definition: mpegvideo.h:536
float p_masking
p block masking (0-> disabled)
Definition: avcodec.h:2029
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
attribute_deprecated int rc_strategy
Definition: avcodec.h:1956
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition: avcodec.h:1730
int mb_lmax
maximum MB Lagrange multiplier
Definition: avcodec.h:2303
uint8_t bits
Definition: crc.c:296
#define av_cold
Definition: attributes.h:82
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...
Definition: avcodec.h:1952
#define FF_RC_STRATEGY_XVID
Definition: avcodec.h:1957
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
int misc_bits
cbp, mb_type
Definition: mpegvideo.h:352
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:177
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
float * cplx_tab
Definition: mpegvideo.h:544
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition: utils.c:91
#define ff_dlog(a,...)
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:126
ptrdiff_t size
Definition: opengl_enc.c:101
float lumi_masking
luminance masking (0-> disabled)
Definition: avcodec.h:2008
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2825
int num_entries
number of RateControlEntries
Definition: ratecontrol.h:64
#define av_log(a,...)
float quality_factor
Definition: avcodec.h:807
static const double const_values[]
Definition: eval.c:27
int intra_only
if true, only intra pictures are generated
Definition: mpegvideo.h:99
static void adaptive_quantization(MpegEncContext *s, double q)
Definition: ratecontrol.c:625
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int64_t total_bits
Definition: mpegvideo.h:337
static double(*const func1[])(void *, double)
Definition: vf_rotate.c:189
#define AVERROR(e)
Definition: error.h:43
av_cold int ff_rate_control_init(MpegEncContext *s)
Definition: ratecontrol.c:84
#define M_E
Definition: mathematics.h:31
int qmax
maximum quantizer
Definition: avcodec.h:2606
void ff_write_pass1_stats(MpegEncContext *s)
Definition: ratecontrol.c:42
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static double predict_size(Predictor *p, double q, double var)
Definition: ratecontrol.c:608
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1752
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition: avcodec.h:1994
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
Definition: mpegvideo.h:291
int low_delay
no reordering needed / has no B-frames
Definition: mpegvideo.h:404
#define FFMAX(a, b)
Definition: common.h:94
int8_t exp
Definition: eval.c:64
int64_t last_mb_var_sum
Definition: ratecontrol.h:75
av_cold void ff_xvid_rate_control_uninit(MpegEncContext *s)
Definition: libxvid_rc.c:158
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2633
int * lambda_table
Definition: mpegvideo.h:205
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:2663
common internal API header
int rc_override_count
ratecontrol override, see RcOverride
Definition: avcodec.h:2640
float border_masking
Definition: mpegvideo.h:537
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int display_picture_number
picture number in display order
Definition: frame.h:289
AVExpr * rc_eq_eval
Definition: ratecontrol.h:86
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:181
Picture.
Definition: mpegpicture.h:45
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow. ...
Definition: avcodec.h:2681
float rc_min_vbv_overflow_use
Ratecontrol attempt to use, at least, times the amount needed to prevent a vbv overflow.
Definition: avcodec.h:2688
float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
Definition: ratecontrol.c:753
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:294
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1803
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:2613
double pass1_wanted_bits
bits which should have been output by the pass1 code (including complexity init)
Definition: ratecontrol.h:71
RcOverride * rc_override
Definition: avcodec.h:2641
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:297
uint16_t * mc_mb_var
Table for motion compensated MB variances.
Definition: mpegpicture.h:68
int coded_picture_number
picture number in bitstream order
Definition: frame.h:285
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int frame_size
Definition: mxfenc.c:1821
double buffer_index
amount of bits in the video/audio buffer
Definition: ratecontrol.h:66
Libavcodec external API header.
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:318
double decay
Definition: ratecontrol.h:38
double coeff
Definition: ratecontrol.h:36
int debug
debug
Definition: avcodec.h:2896
main external API structure.
Definition: avcodec.h:1657
int qmin
minimum quantizer
Definition: avcodec.h:2599
int64_t last_mc_mb_var_sum
Definition: ratecontrol.h:74
float spatial_cplx_masking
spatial complexity masking (0-> disabled)
Definition: avcodec.h:2022
float rc_qmod_amp
Definition: mpegvideo.h:533
int stuffing_bits
bits used for stuffing
Definition: mpegvideo.h:339
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
Definition: mpegpicture.h:82
int index
Definition: gxfenc.c:89
struct AVFrame * f
Definition: mpegpicture.h:46
#define isnan(x)
Definition: libm.h:340
static double get_fps(AVCodecContext *avctx)
Definition: ratecontrol.c:63
uint64_t p_cplx_sum[5]
Definition: ratecontrol.h:77
static const int factor[16]
Definition: vf_pp7.c:75
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1971
#define snprintf
Definition: snprintf.h:34
int f_code
forward MV resolution
Definition: mpegvideo.h:235
double last_qscale_for[5]
last qscale for a specific pict type, used for max_diff & ipb factor stuff
Definition: ratecontrol.h:73
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2591
int max_b_frames
max number of B-frames for encoding
Definition: mpegvideo.h:112
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:209
float dark_masking
darkness masking (0-> disabled)
Definition: avcodec.h:2036
float temporal_cplx_masking
temporary complexity masking (0-> disabled)
Definition: avcodec.h:2015
uint64_t i_cplx_sum[5]
Definition: ratecontrol.h:76
double short_term_qsum
sum of recent qscales
Definition: ratecontrol.h:68
MpegEncContext.
Definition: mpegvideo.h:78
uint64_t mv_bits_sum[5]
Definition: ratecontrol.h:78
char * rc_eq
Definition: mpegvideo.h:541
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
av_cold int ff_xvid_rate_control_init(MpegEncContext *s)
Definition: libxvid_rc.c:42
float rc_initial_cplx
Definition: mpegvideo.h:535
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:127
if(ret< 0)
Definition: vf_mcdeint.c:282
static int init_pass2(MpegEncContext *s)
Definition: ratecontrol.c:923
static double qp2bits(RateControlEntry *rce, double qp)
Definition: ratecontrol.c:68
int last_pict_type
Definition: mpegvideo.h:211
int adaptive_quant
use adaptive quantization
Definition: mpegvideo.h:206
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:179
Bi-dir predicted.
Definition: avutil.h:268
float rc_qsquish
ratecontrol qmin qmax limiting method 0-> clipping, 1-> use a nice continuous function to limit qscal...
Definition: mpegvideo.h:532
int ff_vbv_update(MpegEncContext *s, int frame_size)
Definition: ratecontrol.c:326
static double bits2qp(RateControlEntry *rce, double bits)
Definition: ratecontrol.c:76
uint64_t qscale_sum[5]
Definition: ratecontrol.h:79
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:850
#define CANDIDATE_MB_TYPE_INTRA
Definition: mpegutils.h:106
static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num)
Modify the bitrate curve from pass1 for one frame.
Definition: ratecontrol.c:371
int64_t mc_mb_var_sum
Definition: ratecontrol.h:52
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:713
int64_t mb_var_sum
Definition: ratecontrol.h:53
ratecontrol header.
int64_t bit_rate
wanted bit rate
Definition: mpegvideo.h:100
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:219
static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q)
Definition: ratecontrol.c:450
#define av_freep(p)
uint64_t expected_bits
Definition: ratecontrol.h:49
#define M_PI
Definition: mathematics.h:46
#define av_malloc_array(a, b)
static const char *const const_names[]
Definition: eval.c:33
int b_code
backward MV resolution for B-frames (MPEG-4)
Definition: mpegvideo.h:236
float * bits_tab
Definition: mpegvideo.h:544
int64_t mb_var_sum
sum of MB variance for current frame
Definition: mpegpicture.h:81
double short_term_qcount
count of recent qscales
Definition: ratecontrol.h:69
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
Predictor pred[5]
Definition: ratecontrol.h:67
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
Predicted.
Definition: avutil.h:267
float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run)
Definition: libxvid_rc.c:101
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2656
simple arithmetic expression evaluator
static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num)
Definition: ratecontrol.c:517