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 #ifndef M_E
39 #define M_E 2.718281828
40 #endif
41 
42 static int init_pass2(MpegEncContext *s);
43 static double get_qscale(MpegEncContext *s, RateControlEntry *rce,
44  double rate_factor, int frame_num);
45 
47 {
48  snprintf(s->avctx->stats_out, 256,
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",
53  s->pict_type,
55  s->i_tex_bits,
56  s->p_tex_bits,
57  s->mv_bits,
58  s->misc_bits,
59  s->f_code,
60  s->b_code,
63  s->i_count, s->skip_count,
64  s->header_bits);
65 }
66 
67 static double get_fps(AVCodecContext *avctx)
68 {
69  return 1.0 / av_q2d(avctx->time_base) / FFMAX(avctx->ticks_per_frame, 1);
70 }
71 
72 static inline double qp2bits(RateControlEntry *rce, double qp)
73 {
74  if (qp <= 0.0) {
75  av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
76  }
77  return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / qp;
78 }
79 
80 static inline double bits2qp(RateControlEntry *rce, double bits)
81 {
82  if (bits < 0.9) {
83  av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
84  }
85  return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits;
86 }
87 
89 {
90  RateControlContext *rcc = &s->rc_context;
91  int i, res;
92  static const char * const const_names[] = {
93  "PI",
94  "E",
95  "iTex",
96  "pTex",
97  "tex",
98  "mv",
99  "fCode",
100  "iCount",
101  "mcVar",
102  "var",
103  "isI",
104  "isP",
105  "isB",
106  "avgQP",
107  "qComp",
108 #if 0
109  "lastIQP",
110  "lastPQP",
111  "lastBQP",
112  "nextNonBQP",
113 #endif
114  "avgIITex",
115  "avgPITex",
116  "avgPPTex",
117  "avgBPTex",
118  "avgTex",
119  NULL
120  };
121  static double (* const func1[])(void *, double) = {
122  (void *)bits2qp,
123  (void *)qp2bits,
124  NULL
125  };
126  static const char * const func1_names[] = {
127  "bits2qp",
128  "qp2bits",
129  NULL
130  };
131  emms_c();
132 
134  if (s->avctx->rc_max_rate) {
135  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);
136  } else
138  }
139 
140  res = av_expr_parse(&rcc->rc_eq_eval,
141  s->rc_eq ? s->rc_eq : "tex^qComp",
142  const_names, func1_names, func1,
143  NULL, NULL, 0, s->avctx);
144  if (res < 0) {
145  av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->rc_eq);
146  return res;
147  }
148 
149 #if FF_API_RC_STRATEGY
151  if (!s->rc_strategy)
152  s->rc_strategy = s->avctx->rc_strategy;
154 #endif
155 
156  for (i = 0; i < 5; i++) {
157  rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
158  rcc->pred[i].count = 1.0;
159  rcc->pred[i].decay = 0.4;
160 
161  rcc->i_cplx_sum [i] =
162  rcc->p_cplx_sum [i] =
163  rcc->mv_bits_sum[i] =
164  rcc->qscale_sum [i] =
165  rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such
166 
167  rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5;
168  }
170  if (!rcc->buffer_index)
171  rcc->buffer_index = s->avctx->rc_buffer_size * 3 / 4;
172 
173  if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
174  int i;
175  char *p;
176 
177  /* find number of pics */
178  p = s->avctx->stats_in;
179  for (i = -1; p; i++)
180  p = strchr(p + 1, ';');
181  i += s->max_b_frames;
182  if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry))
183  return -1;
184  rcc->entry = av_mallocz(i * sizeof(RateControlEntry));
185  if (!rcc->entry)
186  return AVERROR(ENOMEM);
187  rcc->num_entries = i;
188 
189  /* init all to skipped p frames
190  * (with b frames we might have a not encoded frame at the end FIXME) */
191  for (i = 0; i < rcc->num_entries; i++) {
192  RateControlEntry *rce = &rcc->entry[i];
193 
195  rce->qscale = rce->new_qscale = FF_QP2LAMBDA * 2;
196  rce->misc_bits = s->mb_num + 10;
197  rce->mb_var_sum = s->mb_num * 100;
198  }
199 
200  /* read stats */
201  p = s->avctx->stats_in;
202  for (i = 0; i < rcc->num_entries - s->max_b_frames; i++) {
203  RateControlEntry *rce;
204  int picture_number;
205  int e;
206  char *next;
207 
208  next = strchr(p, ';');
209  if (next) {
210  (*next) = 0; // sscanf in unbelievably slow on looong strings // FIXME copy / do not write
211  next++;
212  }
213  e = sscanf(p, " in:%d ", &picture_number);
214 
215  av_assert0(picture_number >= 0);
216  av_assert0(picture_number < rcc->num_entries);
217  rce = &rcc->entry[picture_number];
218 
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",
220  &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits,
221  &rce->mv_bits, &rce->misc_bits,
222  &rce->f_code, &rce->b_code,
223  &rce->mc_mb_var_sum, &rce->mb_var_sum,
224  &rce->i_count, &rce->skip_count, &rce->header_bits);
225  if (e != 14) {
227  "statistics are damaged at line %d, parser out=%d\n",
228  i, e);
229  return -1;
230  }
231 
232  p = next;
233  }
234 
235  if (init_pass2(s) < 0) {
237  return -1;
238  }
239 
240  // FIXME maybe move to end
241  if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == 1) {
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
321  if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == 1)
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 finetune
656  if (temp_cplx < 4)
657  temp_cplx = 4; // FIXME finetune
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
774  if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == 1)
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:345
#define NULL
Definition: coverity.c:32
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
Definition: mpegvideo.h:348
av_cold void ff_rate_control_uninit(MpegEncContext *s)
Definition: ratecontrol.c:312
int picture_number
Definition: mpegvideo.h:134
const char * s
Definition: avisynth_c.h:631
static const char *const func1_names[]
Definition: vf_rotate.c:184
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
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:2540
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:2643
void ff_get_2pass_fcode(MpegEncContext *s)
Definition: ratecontrol.c:742
int mb_lmin
minimum MB lagrange multipler
Definition: avcodec.h:2136
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:566
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:2779
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:653
int mpv_flags
flags set by private options
Definition: mpegvideo.h:528
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:140
int qscale
QP.
Definition: mpegvideo.h:211
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1835
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:1631
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:539
float p_masking
p block masking (0-> disabled)
Definition: avcodec.h:1863
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
attribute_deprecated int rc_strategy
Definition: avcodec.h:1793
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition: avcodec.h:1575
int mb_lmax
maximum MB lagrange multipler
Definition: avcodec.h:2143
uint8_t bits
Definition: crc.c:295
#define av_cold
Definition: attributes.h:74
static av_always_inline av_const int isnan(float x)
Definition: libm.h:96
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:1789
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
int misc_bits
cbp, mb_type
Definition: mpegvideo.h:359
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:187
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
float * cplx_tab
Definition: mpegvideo.h:547
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:136
ptrdiff_t size
Definition: opengl_enc.c:101
float lumi_masking
luminance masking (0-> disabled)
Definition: avcodec.h:1842
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2771
int num_entries
number of RateControlEntries
Definition: ratecontrol.h:64
#define av_log(a,...)
float quality_factor
Definition: avcodec.h:712
int intra_only
if true, only intra pictures are generated
Definition: mpegvideo.h:109
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:344
static double(*const func1[])(void *, double)
Definition: vf_rotate.c:178
#define AVERROR(e)
Definition: error.h:43
av_cold int ff_rate_control_init(MpegEncContext *s)
Definition: ratecontrol.c:88
int qmax
maximum quantizer
Definition: avcodec.h:2554
void ff_write_pass1_stats(MpegEncContext *s)
Definition: ratecontrol.c:46
#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:1597
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2604
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*...
Definition: avcodec.h:1828
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
Definition: mpegvideo.h:299
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:411
enum AVCodecID codec_id
Definition: mov_chan.c:433
#define FFMAX(a, b)
Definition: common.h:79
Libavcodec external API header.
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:2581
int * lambda_table
Definition: mpegvideo.h:215
common internal API header
int rc_override_count
ratecontrol override, see RcOverride
Definition: avcodec.h:2588
float border_masking
Definition: mpegvideo.h:540
#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:278
AVExpr * rc_eq_eval
Definition: ratecontrol.h:86
#define M_E
Definition: ratecontrol.c:39
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:191
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:2629
float rc_min_vbv_overflow_use
Ratecontrol attempt to use, at least, times the amount needed to prevent a vbv overflow.
Definition: avcodec.h:2636
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:68
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:283
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1640
int max_qdiff
maximum quantizer difference between frames
Definition: avcodec.h:2561
double pass1_wanted_bits
bits which should have been outputed by the pass1 code (including complexity init) ...
Definition: ratecontrol.h:71
RcOverride * rc_override
Definition: avcodec.h:2589
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:305
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:274
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int frame_size
Definition: mxfenc.c:1805
double buffer_index
amount of bits in the video/audio buffer
Definition: ratecontrol.h:66
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:313
double decay
Definition: ratecontrol.h:38
#define FF_DEBUG_RC
Definition: avcodec.h:2844
double coeff
Definition: ratecontrol.h:36
int debug
debug
Definition: avcodec.h:2842
main external API structure.
Definition: avcodec.h:1502
int qmin
minimum quantizer
Definition: avcodec.h:2547
int64_t last_mc_mb_var_sum
Definition: ratecontrol.h:74
float spatial_cplx_masking
spatial complexity masking (0-> disabled)
Definition: avcodec.h:1856
float rc_qmod_amp
Definition: mpegvideo.h:536
int stuffing_bits
bits used for stuffing
Definition: mpegvideo.h:346
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
static double get_fps(AVCodecContext *avctx)
Definition: ratecontrol.c:67
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:1804
#define snprintf
Definition: snprintf.h:34
int f_code
forward MV resolution
Definition: mpegvideo.h:245
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:2539
int max_b_frames
max number of b-frames for encoding
Definition: mpegvideo.h:122
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:219
float dark_masking
darkness masking (0-> disabled)
Definition: avcodec.h:1870
float temporal_cplx_masking
temporary complexity masking (0-> disabled)
Definition: avcodec.h:1849
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:88
uint64_t mv_bits_sum[5]
Definition: ratecontrol.h:78
char * rc_eq
Definition: mpegvideo.h:544
struct AVCodecContext * avctx
Definition: mpegvideo.h:105
av_cold int ff_xvid_rate_control_init(MpegEncContext *s)
Definition: libxvid_rc.c:42
float rc_initial_cplx
Definition: mpegvideo.h:538
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
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:137
if(ret< 0)
Definition: vf_mcdeint.c:280
static int init_pass2(MpegEncContext *s)
Definition: ratecontrol.c:923
static double qp2bits(RateControlEntry *rce, double qp)
Definition: ratecontrol.c:72
int last_pict_type
Definition: mpegvideo.h:221
int adaptive_quant
use adaptive quantization
Definition: mpegvideo.h:216
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:189
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:535
int ff_vbv_update(MpegEncContext *s, int frame_size)
Definition: ratecontrol.c:326
static double bits2qp(RateControlEntry *rce, double bits)
Definition: ratecontrol.c:80
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:755
#define CANDIDATE_MB_TYPE_INTRA
Definition: mpegutils.h:100
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:80
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:708
int64_t mb_var_sum
Definition: ratecontrol.h:53
ratecontrol header.
int64_t bit_rate
wanted bit rate
Definition: mpegvideo.h:110
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)
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2611
int b_code
backward MV resolution for B Frames (mpeg4)
Definition: mpegvideo.h:246
float * bits_tab
Definition: mpegvideo.h:547
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
simple arithmetic expression evaluator
static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num)
Definition: ratecontrol.c:517