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