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