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