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