FFmpeg
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/emms.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mem.h"
32 
33 #include "avcodec.h"
34 #include "ratecontrol.h"
35 #include "mpegvideoenc.h"
36 #include "libavutil/eval.h"
37 
39 {
40  const MPVEncContext *const s = &m->s;
41  snprintf(s->c.avctx->stats_out, 256,
42  "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
43  "fcode:%d bcode:%d mc-var:%"PRId64" var:%"PRId64" icount:%d hbits:%d;\n",
44  s->c.cur_pic.ptr->display_picture_number,
45  s->c.cur_pic.ptr->coded_picture_number,
46  s->c.pict_type,
47  s->c.cur_pic.ptr->f->quality,
48  s->i_tex_bits,
49  s->p_tex_bits,
50  s->mv_bits,
51  s->misc_bits,
52  s->c.f_code,
53  s->c.b_code,
54  m->mc_mb_var_sum,
55  m->mb_var_sum,
56  s->i_count,
57  m->header_bits);
58 }
59 
61 {
62  if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
63  return avctx->framerate;
64 
66 #if FF_API_TICKS_PER_FRAME
67  return av_div_q((AVRational){1, FFMAX(avctx->ticks_per_frame, 1)}, avctx->time_base);
68 #else
69  return av_inv_q(avctx->time_base);
70 #endif
72 }
73 
74 static double get_fps(AVCodecContext *avctx)
75 {
76  return av_q2d(get_fpsQ(avctx));
77 }
78 
79 static inline double qp2bits(const RateControlEntry *rce, double qp)
80 {
81  if (qp <= 0.0) {
82  av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
83  }
84  return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / qp;
85 }
86 
87 static double qp2bits_cb(void *rce, double qp)
88 {
89  return qp2bits(rce, qp);
90 }
91 
92 static inline double bits2qp(const RateControlEntry *rce, double bits)
93 {
94  if (bits < 0.9) {
95  av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
96  }
97  return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits;
98 }
99 
100 static double bits2qp_cb(void *rce, double qp)
101 {
102  return bits2qp(rce, qp);
103 }
104 
105 static double get_diff_limited_q(MPVMainEncContext *m, const RateControlEntry *rce, double q)
106 {
107  MPVEncContext *const s = &m->s;
108  RateControlContext *const rcc = &m->rc_context;
109  AVCodecContext *const a = s->c.avctx;
110  const int pict_type = rce->new_pict_type;
111  const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P];
112  const double last_non_b_q = rcc->last_qscale_for[rcc->last_non_b_pict_type];
113 
114  if (pict_type == AV_PICTURE_TYPE_I &&
115  (a->i_quant_factor > 0.0 || rcc->last_non_b_pict_type == AV_PICTURE_TYPE_P))
116  q = last_p_q * FFABS(a->i_quant_factor) + a->i_quant_offset;
117  else if (pict_type == AV_PICTURE_TYPE_B &&
118  a->b_quant_factor > 0.0)
119  q = last_non_b_q * a->b_quant_factor + a->b_quant_offset;
120  if (q < 1)
121  q = 1;
122 
123  /* last qscale / qdiff stuff */
124  if (rcc->last_non_b_pict_type == pict_type || pict_type != AV_PICTURE_TYPE_I) {
125  double last_q = rcc->last_qscale_for[pict_type];
126  const int maxdiff = FF_QP2LAMBDA * a->max_qdiff;
127 
128  if (q > last_q + maxdiff)
129  q = last_q + maxdiff;
130  else if (q < last_q - maxdiff)
131  q = last_q - maxdiff;
132  }
133 
134  rcc->last_qscale_for[pict_type] = q; // Note we cannot do that after blurring
135 
136  if (pict_type != AV_PICTURE_TYPE_B)
137  rcc->last_non_b_pict_type = pict_type;
138 
139  return q;
140 }
141 
142 /**
143  * Get the qmin & qmax for pict_type.
144  */
145 static void get_qminmax(int *qmin_ret, int *qmax_ret, MPVMainEncContext *const m, int pict_type)
146 {
147  MPVEncContext *const s = &m->s;
148  int qmin = m->lmin;
149  int qmax = m->lmax;
150 
151  av_assert0(qmin <= qmax);
152 
153  switch (pict_type) {
154  case AV_PICTURE_TYPE_B:
155  qmin = (int)(qmin * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset + 0.5);
156  qmax = (int)(qmax * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset + 0.5);
157  break;
158  case AV_PICTURE_TYPE_I:
159  qmin = (int)(qmin * FFABS(s->c.avctx->i_quant_factor) + s->c.avctx->i_quant_offset + 0.5);
160  qmax = (int)(qmax * FFABS(s->c.avctx->i_quant_factor) + s->c.avctx->i_quant_offset + 0.5);
161  break;
162  }
163 
164  qmin = av_clip(qmin, 1, FF_LAMBDA_MAX);
165  qmax = av_clip(qmax, 1, FF_LAMBDA_MAX);
166 
167  if (qmax < qmin)
168  qmax = qmin;
169 
170  *qmin_ret = qmin;
171  *qmax_ret = qmax;
172 }
173 
174 static double modify_qscale(MPVMainEncContext *const m, const RateControlEntry *rce,
175  double q, int frame_num)
176 {
177  MPVEncContext *const s = &m->s;
178  RateControlContext *const rcc = &m->rc_context;
179  const double buffer_size = s->c.avctx->rc_buffer_size;
180  const double fps = get_fps(s->c.avctx);
181  const double min_rate = s->c.avctx->rc_min_rate / fps;
182  const double max_rate = s->c.avctx->rc_max_rate / fps;
183  const int pict_type = rce->new_pict_type;
184  int qmin, qmax;
185 
186  get_qminmax(&qmin, &qmax, m, pict_type);
187 
188  /* modulation */
189  if (rcc->qmod_freq &&
190  frame_num % rcc->qmod_freq == 0 &&
191  pict_type == AV_PICTURE_TYPE_P)
192  q *= rcc->qmod_amp;
193 
194  /* buffer overflow/underflow protection */
195  if (buffer_size) {
196  double expected_size = rcc->buffer_index;
197  double q_limit;
198 
199  if (min_rate) {
200  double d = 2 * (buffer_size - expected_size) / buffer_size;
201  if (d > 1.0)
202  d = 1.0;
203  else if (d < 0.0001)
204  d = 0.0001;
205  q *= pow(d, 1.0 / rcc->buffer_aggressivity);
206 
207  q_limit = bits2qp(rce,
208  FFMAX((min_rate - buffer_size + rcc->buffer_index) *
209  s->c.avctx->rc_min_vbv_overflow_use, 1));
210 
211  if (q > q_limit) {
212  if (s->c.avctx->debug & FF_DEBUG_RC)
213  av_log(s->c.avctx, AV_LOG_DEBUG,
214  "limiting QP %f -> %f\n", q, q_limit);
215  q = q_limit;
216  }
217  }
218 
219  if (max_rate) {
220  double d = 2 * expected_size / buffer_size;
221  if (d > 1.0)
222  d = 1.0;
223  else if (d < 0.0001)
224  d = 0.0001;
225  q /= pow(d, 1.0 / rcc->buffer_aggressivity);
226 
227  q_limit = bits2qp(rce,
228  FFMAX(rcc->buffer_index *
229  s->c.avctx->rc_max_available_vbv_use,
230  1));
231  if (q < q_limit) {
232  if (s->c.avctx->debug & FF_DEBUG_RC)
233  av_log(s->c.avctx, AV_LOG_DEBUG,
234  "limiting QP %f -> %f\n", q, q_limit);
235  q = q_limit;
236  }
237  }
238  }
239  ff_dlog(s->c.avctx, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",
240  q, max_rate, min_rate, buffer_size, rcc->buffer_index,
241  rcc->buffer_aggressivity);
242  if (rcc->qsquish == 0.0 || qmin == qmax) {
243  if (q < qmin)
244  q = qmin;
245  else if (q > qmax)
246  q = qmax;
247  } else {
248  double min2 = log(qmin);
249  double max2 = log(qmax);
250 
251  q = log(q);
252  q = (q - min2) / (max2 - min2) - 0.5;
253  q *= -4.0;
254  q = 1.0 / (1.0 + exp(q));
255  q = q * (max2 - min2) + min2;
256 
257  q = exp(q);
258  }
259 
260  return q;
261 }
262 
263 /**
264  * Modify the bitrate curve from pass1 for one frame.
265  */
266 static double get_qscale(MPVMainEncContext *const m, RateControlEntry *rce,
267  double rate_factor, int frame_num)
268 {
269  MPVEncContext *const s = &m->s;
270  RateControlContext *rcc = &m->rc_context;
271  AVCodecContext *const avctx = s->c.avctx;
272  const int pict_type = rce->new_pict_type;
273  const double mb_num = s->c.mb_num;
274  double q, bits;
275  int i;
276 
277  double const_values[] = {
278  M_PI,
279  M_E,
280  rce->i_tex_bits * rce->qscale,
281  rce->p_tex_bits * rce->qscale,
282  (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale,
283  rce->mv_bits / mb_num,
284  rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code) * 0.5 : rce->f_code,
285  rce->i_count / mb_num,
286  rce->mc_mb_var_sum / mb_num,
287  rce->mb_var_sum / mb_num,
291  rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
292  avctx->qcompress,
297  (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
298  0
299  };
300 
302  if (isnan(bits)) {
303  av_log(avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", rcc->rc_eq);
304  return -1;
305  }
306 
308  bits *= rate_factor;
309  if (bits < 0.0)
310  bits = 0.0;
311  bits += 1.0; // avoid 1/0 issues
312 
313  /* user override */
314  for (i = 0; i < avctx->rc_override_count; i++) {
315  RcOverride *rco = avctx->rc_override;
316  if (rco[i].start_frame > frame_num)
317  continue;
318  if (rco[i].end_frame < frame_num)
319  continue;
320 
321  if (rco[i].qscale)
322  bits = qp2bits(rce, rco[i].qscale); // FIXME move at end to really force it?
323  else
324  bits *= rco[i].quality_factor;
325  }
326 
327  q = bits2qp(rce, bits);
328 
329  /* I/B difference */
330  if (pict_type == AV_PICTURE_TYPE_I && avctx->i_quant_factor < 0.0)
331  q = -q * avctx->i_quant_factor + avctx->i_quant_offset;
332  else if (pict_type == AV_PICTURE_TYPE_B && avctx->b_quant_factor < 0.0)
333  q = -q * avctx->b_quant_factor + avctx->b_quant_offset;
334  if (q < 1)
335  q = 1;
336 
337  return q;
338 }
339 
340 static int init_pass2(MPVMainEncContext *const m)
341 {
342  RateControlContext *const rcc = &m->rc_context;
343  MPVEncContext *const s = &m->s;
344  AVCodecContext *const avctx = s->c.avctx;
345  int i, toobig;
346  AVRational fps = get_fpsQ(avctx);
347  double complexity[5] = { 0 }; // approximate bits at quant=1
348  uint64_t const_bits[5] = { 0 }; // quantizer independent bits
349  uint64_t all_const_bits;
350  uint64_t all_available_bits = av_rescale_q(m->bit_rate,
351  (AVRational){rcc->num_entries,1},
352  fps);
353  double rate_factor = 0;
354  double step;
355  const int filter_size = (int)(avctx->qblur * 4) | 1;
356  double expected_bits = 0; // init to silence gcc warning
357  double *qscale, *blurred_qscale, qscale_sum;
358 
359  /* find complexity & const_bits & decide the pict_types */
360  for (i = 0; i < rcc->num_entries; i++) {
361  RateControlEntry *rce = &rcc->entry[i];
362 
363  rce->new_pict_type = rce->pict_type;
364  rcc->i_cplx_sum[rce->pict_type] += rce->i_tex_bits * rce->qscale;
365  rcc->p_cplx_sum[rce->pict_type] += rce->p_tex_bits * rce->qscale;
366  rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits;
367  rcc->frame_count[rce->pict_type]++;
368 
369  complexity[rce->new_pict_type] += (rce->i_tex_bits + rce->p_tex_bits) *
370  (double)rce->qscale;
371  const_bits[rce->new_pict_type] += rce->mv_bits + rce->misc_bits;
372  }
373 
374  all_const_bits = const_bits[AV_PICTURE_TYPE_I] +
375  const_bits[AV_PICTURE_TYPE_P] +
376  const_bits[AV_PICTURE_TYPE_B];
377 
378  if (all_available_bits < all_const_bits) {
379  av_log(avctx, AV_LOG_ERROR, "requested bitrate is too low\n");
380  return -1;
381  }
382 
383  qscale = av_malloc_array(rcc->num_entries, sizeof(double));
384  blurred_qscale = av_malloc_array(rcc->num_entries, sizeof(double));
385  if (!qscale || !blurred_qscale) {
386  av_free(qscale);
387  av_free(blurred_qscale);
388  return AVERROR(ENOMEM);
389  }
390  toobig = 0;
391 
392  for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
393  expected_bits = 0;
394  rate_factor += step;
395 
396  rcc->buffer_index = avctx->rc_buffer_size / 2;
397 
398  /* find qscale */
399  for (i = 0; i < rcc->num_entries; i++) {
400  const RateControlEntry *rce = &rcc->entry[i];
401 
402  qscale[i] = get_qscale(m, &rcc->entry[i], rate_factor, i);
403  rcc->last_qscale_for[rce->pict_type] = qscale[i];
404  }
405  av_assert0(filter_size % 2 == 1);
406 
407  /* fixed I/B QP relative to P mode */
408  for (i = FFMAX(0, rcc->num_entries - 300); i < rcc->num_entries; i++) {
409  const RateControlEntry *rce = &rcc->entry[i];
410 
411  qscale[i] = get_diff_limited_q(m, rce, qscale[i]);
412  }
413 
414  for (i = rcc->num_entries - 1; i >= 0; i--) {
415  const RateControlEntry *rce = &rcc->entry[i];
416 
417  qscale[i] = get_diff_limited_q(m, rce, qscale[i]);
418  }
419 
420  /* smooth curve */
421  for (i = 0; i < rcc->num_entries; i++) {
422  const RateControlEntry *rce = &rcc->entry[i];
423  const int pict_type = rce->new_pict_type;
424  int j;
425  double q = 0.0, sum = 0.0;
426 
427  for (j = 0; j < filter_size; j++) {
428  int index = i + j - filter_size / 2;
429  double d = index - i;
430  double coeff = avctx->qblur == 0 ? 1.0 : exp(-d * d / (avctx->qblur * avctx->qblur));
431 
432  if (index < 0 || index >= rcc->num_entries)
433  continue;
434  if (pict_type != rcc->entry[index].new_pict_type)
435  continue;
436  q += qscale[index] * coeff;
437  sum += coeff;
438  }
439  blurred_qscale[i] = q / sum;
440  }
441 
442  /* find expected bits */
443  for (i = 0; i < rcc->num_entries; i++) {
444  RateControlEntry *rce = &rcc->entry[i];
445  double bits;
446 
447  rce->new_qscale = modify_qscale(m, rce, blurred_qscale[i], i);
448 
449  bits = qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
450  bits += 8 * ff_vbv_update(m, bits);
451 
452  rce->expected_bits = expected_bits;
453  expected_bits += bits;
454  }
455 
456  ff_dlog(avctx,
457  "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
458  expected_bits, (int)all_available_bits, rate_factor);
459  if (expected_bits > all_available_bits) {
460  rate_factor -= step;
461  ++toobig;
462  }
463  }
464  av_free(qscale);
465  av_free(blurred_qscale);
466 
467  /* check bitrate calculations and print info */
468  qscale_sum = 0.0;
469  for (i = 0; i < rcc->num_entries; i++) {
470  ff_dlog(avctx, "[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
471  i,
472  rcc->entry[i].new_qscale,
473  rcc->entry[i].new_qscale / FF_QP2LAMBDA);
474  qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA,
475  avctx->qmin, avctx->qmax);
476  }
477  av_assert0(toobig <= 40);
478  av_log(avctx, AV_LOG_DEBUG,
479  "[lavc rc] requested bitrate: %"PRId64" bps expected bitrate: %"PRId64" bps\n",
480  m->bit_rate,
481  (int64_t)(expected_bits / ((double)all_available_bits / m->bit_rate)));
482  av_log(avctx, AV_LOG_DEBUG,
483  "[lavc rc] estimated target average qp: %.3f\n",
484  (float)qscale_sum / rcc->num_entries);
485  if (toobig == 0) {
486  av_log(avctx, AV_LOG_INFO,
487  "[lavc rc] Using all of requested bitrate is not "
488  "necessary for this video with these parameters.\n");
489  } else if (toobig == 40) {
490  av_log(avctx, AV_LOG_ERROR,
491  "[lavc rc] Error: bitrate too low for this video "
492  "with these parameters.\n");
493  return -1;
494  } else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) {
495  av_log(avctx, AV_LOG_ERROR,
496  "[lavc rc] Error: 2pass curve failed to converge\n");
497  return -1;
498  }
499 
500  return 0;
501 }
502 
504 {
505  MPVEncContext *const s = &m->s;
506  RateControlContext *rcc = &m->rc_context;
507  AVCodecContext *const avctx = s->c.avctx;
508  int i, res;
509  static const char * const const_names[] = {
510  "PI",
511  "E",
512  "iTex",
513  "pTex",
514  "tex",
515  "mv",
516  "fCode",
517  "iCount",
518  "mcVar",
519  "var",
520  "isI",
521  "isP",
522  "isB",
523  "avgQP",
524  "qComp",
525  "avgIITex",
526  "avgPITex",
527  "avgPPTex",
528  "avgBPTex",
529  "avgTex",
530  NULL
531  };
532  static double (* const func1[])(void *, double) = {
533  bits2qp_cb,
534  qp2bits_cb,
535  NULL
536  };
537  static const char * const func1_names[] = {
538  "bits2qp",
539  "qp2bits",
540  NULL
541  };
542  emms_c();
543 
544  if (!avctx->rc_max_available_vbv_use && avctx->rc_buffer_size) {
545  if (avctx->rc_max_rate) {
546  avctx->rc_max_available_vbv_use = av_clipf(avctx->rc_max_rate/(avctx->rc_buffer_size*get_fps(avctx)), 1.0/3, 1.0);
547  } else
548  avctx->rc_max_available_vbv_use = 1.0;
549  }
550 
551  res = av_expr_parse(&rcc->rc_eq_eval,
552  rcc->rc_eq ? rcc->rc_eq : "tex^qComp",
554  NULL, NULL, 0, avctx);
555  if (res < 0) {
556  av_log(avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", rcc->rc_eq);
557  return res;
558  }
559 
560  for (i = 0; i < 5; i++) {
561  rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0;
562  rcc->pred[i].count = 1.0;
563  rcc->pred[i].decay = 0.4;
564 
565  rcc->i_cplx_sum [i] =
566  rcc->p_cplx_sum [i] =
567  rcc->mv_bits_sum[i] =
568  rcc->qscale_sum [i] =
569  rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such
570 
571  rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5;
572  }
574  if (!rcc->buffer_index)
575  rcc->buffer_index = avctx->rc_buffer_size * 3 / 4;
576 
577  if (avctx->flags & AV_CODEC_FLAG_PASS2) {
578  int i;
579  char *p;
580 
581  /* find number of pics */
582  p = avctx->stats_in;
583  for (i = -1; p; i++)
584  p = strchr(p + 1, ';');
585  i += m->max_b_frames;
586  if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry))
587  return -1;
588  rcc->entry = av_mallocz(i * sizeof(RateControlEntry));
589  if (!rcc->entry)
590  return AVERROR(ENOMEM);
591  rcc->num_entries = i;
592 
593  /* init all to skipped P-frames
594  * (with B-frames we might have a not encoded frame at the end FIXME) */
595  for (i = 0; i < rcc->num_entries; i++) {
596  RateControlEntry *rce = &rcc->entry[i];
597 
599  rce->qscale = rce->new_qscale = FF_QP2LAMBDA * 2;
600  rce->misc_bits = s->c.mb_num + 10;
601  rce->mb_var_sum = s->c.mb_num * 100;
602  }
603 
604  /* read stats */
605  p = avctx->stats_in;
606  for (i = 0; i < rcc->num_entries - m->max_b_frames; i++) {
607  RateControlEntry *rce;
608  int picture_number;
609  int e;
610  char *next;
611 
612  next = strchr(p, ';');
613  if (next) {
614  (*next) = 0; // sscanf is unbelievably slow on looong strings // FIXME copy / do not write
615  next++;
616  }
617  e = sscanf(p, " in:%d ", &picture_number);
618 
619  av_assert0(picture_number >= 0);
620  av_assert0(picture_number < rcc->num_entries);
621  rce = &rcc->entry[picture_number];
622 
623  e += sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d "
624  "mv:%d misc:%d "
625  "fcode:%d bcode:%d "
626  "mc-var:%"SCNd64" var:%"SCNd64" "
627  "icount:%d hbits:%d",
628  &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits,
629  &rce->mv_bits, &rce->misc_bits,
630  &rce->f_code, &rce->b_code,
631  &rce->mc_mb_var_sum, &rce->mb_var_sum,
632  &rce->i_count, &rce->header_bits);
633  if (e != 13) {
634  av_log(avctx, AV_LOG_ERROR,
635  "statistics are damaged at line %d, parser out=%d\n",
636  i, e);
637  return -1;
638  }
639 
640  p = next;
641  }
642 
643  res = init_pass2(m);
644  if (res < 0)
645  return res;
646  }
647 
648  if (!(avctx->flags & AV_CODEC_FLAG_PASS2)) {
649  rcc->short_term_qsum = 0.001;
650  rcc->short_term_qcount = 0.001;
651 
652  rcc->pass1_rc_eq_output_sum = 0.001;
653  rcc->pass1_wanted_bits = 0.001;
654 
655  if (avctx->qblur > 1.0) {
656  av_log(avctx, AV_LOG_ERROR, "qblur too large\n");
657  return -1;
658  }
659  /* init stuff with the user specified complexity */
660  if (rcc->initial_cplx) {
661  for (i = 0; i < 60 * 30; i++) {
662  double bits = rcc->initial_cplx * (i / 10000.0 + 1.0) * s->c.mb_num;
663  RateControlEntry rce;
664 
665  if (i % ((m->gop_size + 3) / 4) == 0)
667  else if (i % (m->max_b_frames + 1))
669  else
671 
672  rce.new_pict_type = rce.pict_type;
673  rce.mc_mb_var_sum = bits * s->c.mb_num / 100000;
674  rce.mb_var_sum = s->c.mb_num;
675 
676  rce.qscale = FF_QP2LAMBDA * 2;
677  rce.f_code = 2;
678  rce.b_code = 1;
679  rce.misc_bits = 1;
680 
681  if (s->c.pict_type == AV_PICTURE_TYPE_I) {
682  rce.i_count = s->c.mb_num;
683  rce.i_tex_bits = bits;
684  rce.p_tex_bits = 0;
685  rce.mv_bits = 0;
686  } else {
687  rce.i_count = 0; // FIXME we do know this approx
688  rce.i_tex_bits = 0;
689  rce.p_tex_bits = bits * 0.9;
690  rce.mv_bits = bits * 0.1;
691  }
692  rcc->i_cplx_sum[rce.pict_type] += rce.i_tex_bits * rce.qscale;
693  rcc->p_cplx_sum[rce.pict_type] += rce.p_tex_bits * rce.qscale;
694  rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
695  rcc->frame_count[rce.pict_type]++;
696 
697  get_qscale(m, &rce, rcc->pass1_wanted_bits / rcc->pass1_rc_eq_output_sum, i);
698 
699  // FIXME misbehaves a little for variable fps
700  rcc->pass1_wanted_bits += m->bit_rate / get_fps(avctx);
701  }
702  }
703  }
704 
705  if (s->adaptive_quant) {
706  unsigned mb_array_size = s->c.mb_stride * s->c.mb_height;
707 
708  rcc->cplx_tab = av_malloc_array(mb_array_size, 2 * sizeof(rcc->cplx_tab));
709  if (!rcc->cplx_tab)
710  return AVERROR(ENOMEM);
711  rcc->bits_tab = rcc->cplx_tab + mb_array_size;
712  }
713 
714  return 0;
715 }
716 
718 {
719  emms_c();
720 
721  // rc_eq is always managed via an AVOption and therefore not freed here.
722  av_expr_free(rcc->rc_eq_eval);
723  rcc->rc_eq_eval = NULL;
724  av_freep(&rcc->entry);
725  av_freep(&rcc->cplx_tab);
726 }
727 
729 {
730  MPVEncContext *const s = &m->s;
731  RateControlContext *const rcc = &m->rc_context;
732  AVCodecContext *const avctx = s->c.avctx;
733  const double fps = get_fps(avctx);
734  const int buffer_size = avctx->rc_buffer_size;
735  const double min_rate = avctx->rc_min_rate / fps;
736  const double max_rate = avctx->rc_max_rate / fps;
737 
738  ff_dlog(avctx, "%d %f %d %f %f\n",
739  buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
740 
741  if (buffer_size) {
742  int left;
743 
744  rcc->buffer_index -= frame_size;
745  if (rcc->buffer_index < 0) {
746  av_log(avctx, AV_LOG_ERROR, "rc buffer underflow\n");
747  if (frame_size > max_rate && s->c.qscale == avctx->qmax) {
748  av_log(avctx, AV_LOG_ERROR, "max bitrate possibly too small or try trellis with large lmax or increase qmax\n");
749  }
750  rcc->buffer_index = 0;
751  }
752 
753  left = buffer_size - rcc->buffer_index - 1;
754  rcc->buffer_index += av_clip(left, min_rate, max_rate);
755 
756  if (rcc->buffer_index > buffer_size) {
757  int stuffing = ceil((rcc->buffer_index - buffer_size) / 8);
758 
759  if (stuffing < 4 && s->c.codec_id == AV_CODEC_ID_MPEG4)
760  stuffing = 4;
761  rcc->buffer_index -= 8 * stuffing;
762 
763  if (avctx->debug & FF_DEBUG_RC)
764  av_log(avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
765 
766  return stuffing;
767  }
768  }
769  return 0;
770 }
771 
772 static double predict_size(Predictor *p, double q, double var)
773 {
774  return p->coeff * var / (q * p->count);
775 }
776 
777 static void update_predictor(Predictor *p, double q, double var, double size)
778 {
779  double new_coeff = size * q / (var + 1);
780  if (var < 10)
781  return;
782 
783  p->count *= p->decay;
784  p->coeff *= p->decay;
785  p->count++;
786  p->coeff += new_coeff;
787 }
788 
790  MPVMainEncContext *const m, double q)
791 {
792  MPVEncContext *const s = &m->s;
793  const float lumi_masking = s->c.avctx->lumi_masking / (128.0 * 128.0);
794  const float dark_masking = s->c.avctx->dark_masking / (128.0 * 128.0);
795  const float temp_cplx_masking = s->c.avctx->temporal_cplx_masking;
796  const float spatial_cplx_masking = s->c.avctx->spatial_cplx_masking;
797  const float p_masking = s->c.avctx->p_masking;
798  const float border_masking = m->border_masking;
799  float bits_sum = 0.0;
800  float cplx_sum = 0.0;
801  float *cplx_tab = rcc->cplx_tab;
802  float *bits_tab = rcc->bits_tab;
803  const int qmin = s->c.avctx->mb_lmin;
804  const int qmax = s->c.avctx->mb_lmax;
805  const int mb_width = s->c.mb_width;
806  const int mb_height = s->c.mb_height;
807 
808  for (int i = 0; i < s->c.mb_num; i++) {
809  const int mb_xy = s->c.mb_index2xy[i];
810  float temp_cplx = sqrt(s->mc_mb_var[mb_xy]); // FIXME merge in pow()
811  float spat_cplx = sqrt(s->mb_var[mb_xy]);
812  const int lumi = s->mb_mean[mb_xy];
813  float bits, cplx, factor;
814  int mb_x = mb_xy % s->c.mb_stride;
815  int mb_y = mb_xy / s->c.mb_stride;
816  int mb_distance;
817  float mb_factor = 0.0;
818  if (spat_cplx < 4)
819  spat_cplx = 4; // FIXME fine-tune
820  if (temp_cplx < 4)
821  temp_cplx = 4; // FIXME fine-tune
822 
823  if ((s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTRA)) { // FIXME hq mode
824  cplx = spat_cplx;
825  factor = 1.0 + p_masking;
826  } else {
827  cplx = temp_cplx;
828  factor = pow(temp_cplx, -temp_cplx_masking);
829  }
830  factor *= pow(spat_cplx, -spatial_cplx_masking);
831 
832  if (lumi > 127)
833  factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking);
834  else
835  factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking);
836 
837  if (mb_x < mb_width / 5) {
838  mb_distance = mb_width / 5 - mb_x;
839  mb_factor = (float)mb_distance / (float)(mb_width / 5);
840  } else if (mb_x > 4 * mb_width / 5) {
841  mb_distance = mb_x - 4 * mb_width / 5;
842  mb_factor = (float)mb_distance / (float)(mb_width / 5);
843  }
844  if (mb_y < mb_height / 5) {
845  mb_distance = mb_height / 5 - mb_y;
846  mb_factor = FFMAX(mb_factor,
847  (float)mb_distance / (float)(mb_height / 5));
848  } else if (mb_y > 4 * mb_height / 5) {
849  mb_distance = mb_y - 4 * mb_height / 5;
850  mb_factor = FFMAX(mb_factor,
851  (float)mb_distance / (float)(mb_height / 5));
852  }
853 
854  factor *= 1.0 - border_masking * mb_factor;
855 
856  if (factor < 0.00001)
857  factor = 0.00001;
858 
859  bits = cplx * factor;
860  cplx_sum += cplx;
861  bits_sum += bits;
862  cplx_tab[i] = cplx;
863  bits_tab[i] = bits;
864  }
865 
866  /* handle qmin/qmax clipping */
867  if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
868  float factor = bits_sum / cplx_sum;
869  for (int i = 0; i < s->c.mb_num; i++) {
870  float newq = q * cplx_tab[i] / bits_tab[i];
871  newq *= factor;
872 
873  if (newq > qmax) {
874  bits_sum -= bits_tab[i];
875  cplx_sum -= cplx_tab[i] * q / qmax;
876  } else if (newq < qmin) {
877  bits_sum -= bits_tab[i];
878  cplx_sum -= cplx_tab[i] * q / qmin;
879  }
880  }
881  if (bits_sum < 0.001)
882  bits_sum = 0.001;
883  if (cplx_sum < 0.001)
884  cplx_sum = 0.001;
885  }
886 
887  for (int i = 0; i < s->c.mb_num; i++) {
888  const int mb_xy = s->c.mb_index2xy[i];
889  float newq = q * cplx_tab[i] / bits_tab[i];
890  int intq;
891 
892  if (s->mpv_flags & FF_MPV_FLAG_NAQ) {
893  newq *= bits_sum / cplx_sum;
894  }
895 
896  intq = (int)(newq + 0.5);
897 
898  if (intq > qmax)
899  intq = qmax;
900  else if (intq < qmin)
901  intq = qmin;
902  s->lambda_table[mb_xy] = intq;
903  }
904 }
905 
907 {
908  MPVEncContext *const s = &m->s;
909  const RateControlContext *rcc = &m->rc_context;
910  const RateControlEntry *rce = &rcc->entry[s->c.picture_number];
911 
912  s->c.f_code = rce->f_code;
913  s->c.b_code = rce->b_code;
914 }
915 
916 // FIXME rd or at least approx for dquant
917 
918 float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
919 {
920  MPVEncContext *const s = &m->s;
921  RateControlContext *rcc = &m->rc_context;
922  AVCodecContext *const a = s->c.avctx;
923  float q;
924  int qmin, qmax;
925  float br_compensation;
926  double diff;
927  double short_term_q;
928  double fps;
929  int picture_number = s->c.picture_number;
930  int64_t wanted_bits;
931  RateControlEntry local_rce, *rce;
932  double bits;
933  double rate_factor;
934  int64_t var;
935  const int pict_type = s->c.pict_type;
936  emms_c();
937 
938  get_qminmax(&qmin, &qmax, m, pict_type);
939 
940  fps = get_fps(s->c.avctx);
941  /* update predictors */
942  if (picture_number > 2 && !dry_run) {
943  const int64_t last_var =
945  : rcc->last_mc_mb_var_sum;
948  rcc->last_qscale,
949  sqrt(last_var),
950  m->frame_bits - m->stuffing_bits);
951  }
952 
953  if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
954  av_assert0(picture_number >= 0);
955  if (picture_number >= rcc->num_entries) {
956  av_log(s->c.avctx, AV_LOG_ERROR, "Input is longer than 2-pass log file\n");
957  return -1;
958  }
959  rce = &rcc->entry[picture_number];
960  wanted_bits = rce->expected_bits;
961  } else {
962  const MPVPicture *dts_pic;
963  double wanted_bits_double;
964  rce = &local_rce;
965 
966  /* FIXME add a dts field to AVFrame and ensure it is set and use it
967  * here instead of reordering but the reordering is simpler for now
968  * until H.264 B-pyramid must be handled. */
969  if (s->c.pict_type == AV_PICTURE_TYPE_B || s->c.low_delay)
970  dts_pic = s->c.cur_pic.ptr;
971  else
972  dts_pic = s->c.last_pic.ptr;
973 
974  if (!dts_pic || dts_pic->f->pts == AV_NOPTS_VALUE)
975  wanted_bits_double = m->bit_rate * (double)picture_number / fps;
976  else
977  wanted_bits_double = m->bit_rate * (double)dts_pic->f->pts / fps;
978  if (wanted_bits_double > INT64_MAX) {
979  av_log(s->c.avctx, AV_LOG_WARNING, "Bits exceed 64bit range\n");
980  wanted_bits = INT64_MAX;
981  } else
982  wanted_bits = (int64_t)wanted_bits_double;
983  }
984 
985  diff = m->total_bits - wanted_bits;
986  br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance;
987  if (br_compensation <= 0.0)
988  br_compensation = 0.001;
989 
990  var = pict_type == AV_PICTURE_TYPE_I ? m->mb_var_sum : m->mc_mb_var_sum;
991 
992  short_term_q = 0; /* avoid warning */
993  if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
994  if (pict_type != AV_PICTURE_TYPE_I)
995  av_assert0(pict_type == rce->new_pict_type);
996 
997  q = rce->new_qscale / br_compensation;
998  ff_dlog(s->c.avctx, "%f %f %f last:%d var:%"PRId64" type:%d//\n", q, rce->new_qscale,
999  br_compensation, m->frame_bits, var, pict_type);
1000  } else {
1001  rce->pict_type =
1002  rce->new_pict_type = pict_type;
1003  rce->mc_mb_var_sum = m->mc_mb_var_sum;
1004  rce->mb_var_sum = m->mb_var_sum;
1005  rce->qscale = FF_QP2LAMBDA * 2;
1006  rce->f_code = s->c.f_code;
1007  rce->b_code = s->c.b_code;
1008  rce->misc_bits = 1;
1009 
1010  bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
1011  if (pict_type == AV_PICTURE_TYPE_I) {
1012  rce->i_count = s->c.mb_num;
1013  rce->i_tex_bits = bits;
1014  rce->p_tex_bits = 0;
1015  rce->mv_bits = 0;
1016  } else {
1017  rce->i_count = 0; // FIXME we do know this approx
1018  rce->i_tex_bits = 0;
1019  rce->p_tex_bits = bits * 0.9;
1020  rce->mv_bits = bits * 0.1;
1021  }
1022  rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale;
1023  rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale;
1024  rcc->mv_bits_sum[pict_type] += rce->mv_bits;
1025  rcc->frame_count[pict_type]++;
1026 
1027  rate_factor = rcc->pass1_wanted_bits /
1028  rcc->pass1_rc_eq_output_sum * br_compensation;
1029 
1030  q = get_qscale(m, rce, rate_factor, picture_number);
1031  if (q < 0)
1032  return -1;
1033 
1034  av_assert0(q > 0.0);
1035  q = get_diff_limited_q(m, rce, q);
1036  av_assert0(q > 0.0);
1037 
1038  // FIXME type dependent blur like in 2-pass
1039  if (pict_type == AV_PICTURE_TYPE_P || m->intra_only) {
1040  rcc->short_term_qsum *= a->qblur;
1041  rcc->short_term_qcount *= a->qblur;
1042 
1043  rcc->short_term_qsum += q;
1044  rcc->short_term_qcount++;
1045  q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount;
1046  }
1047  av_assert0(q > 0.0);
1048 
1049  q = modify_qscale(m, rce, q, picture_number);
1050 
1051  rcc->pass1_wanted_bits += m->bit_rate / fps;
1052 
1053  av_assert0(q > 0.0);
1054  }
1055 
1056  if (s->c.avctx->debug & FF_DEBUG_RC) {
1057  av_log(s->c.avctx, AV_LOG_DEBUG,
1058  "%c qp:%d<%2.1f<%d %d want:%"PRId64" total:%"PRId64" comp:%f st_q:%2.2f "
1059  "size:%d var:%"PRId64"/%"PRId64" br:%"PRId64" fps:%d\n",
1060  av_get_picture_type_char(pict_type),
1061  qmin, q, qmax, picture_number,
1062  wanted_bits / 1000, m->total_bits / 1000,
1063  br_compensation, short_term_q, m->frame_bits,
1064  m->mb_var_sum, m->mc_mb_var_sum,
1065  m->bit_rate / 1000, (int)fps);
1066  }
1067 
1068  if (q < qmin)
1069  q = qmin;
1070  else if (q > qmax)
1071  q = qmax;
1072 
1073  if (s->adaptive_quant)
1074  adaptive_quantization(rcc, m, q);
1075  else
1076  q = (int)(q + 0.5);
1077 
1078  if (!dry_run) {
1079  rcc->last_qscale = q;
1081  rcc->last_mb_var_sum = m->mb_var_sum;
1082  }
1083  return q;
1084 }
FF_DEBUG_RC
#define FF_DEBUG_RC
Definition: avcodec.h:1416
ratecontrol.h
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
RateControlContext::bits_tab
float * bits_tab
Definition: ratecontrol.h:93
MPVMainEncContext::bit_rate
int64_t bit_rate
Definition: mpegvideoenc.h:224
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
av_clip
#define av_clip
Definition: common.h:100
MPVEncContext
Definition: mpegvideoenc.h:45
RateControlContext::last_mb_var_sum
int64_t last_mb_var_sum
Definition: ratecontrol.h:72
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
get_diff_limited_q
static double get_diff_limited_q(MPVMainEncContext *m, const RateControlEntry *rce, double q)
Definition: ratecontrol.c:105
FF_MPV_FLAG_NAQ
#define FF_MPV_FLAG_NAQ
Definition: mpegvideoenc.h:285
RateControlContext::initial_cplx
float initial_cplx
Definition: ratecontrol.h:87
AVCodecContext::rc_min_rate
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:1317
av_div_q
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
MPVMainEncContext::total_bits
int64_t total_bits
Definition: mpegvideoenc.h:225
mpegvideoenc.h
int64_t
long long int64_t
Definition: coverity.c:34
normalize.log
log
Definition: normalize.py:21
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
RateControlEntry
Definition: ratecontrol.h:39
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:522
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
RateControlContext::rc_eq
char * rc_eq
Definition: ratecontrol.h:90
get_fpsQ
static AVRational get_fpsQ(AVCodecContext *avctx)
Definition: ratecontrol.c:60
FF_LAMBDA_MAX
#define FF_LAMBDA_MAX
Definition: avutil.h:228
RateControlEntry::i_count
int i_count
Definition: ratecontrol.h:42
RateControlContext::qmod_freq
int qmod_freq
Definition: ratecontrol.h:86
RateControlContext::rc_eq_eval
struct AVExpr * rc_eq_eval
Definition: ratecontrol.h:91
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:825
get_qscale
static double get_qscale(MPVMainEncContext *const m, RateControlEntry *rce, double rate_factor, int frame_num)
Modify the bitrate curve from pass1 for one frame.
Definition: ratecontrol.c:266
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MPVMainEncContext::gop_size
int gop_size
Definition: mpegvideoenc.h:176
RateControlEntry::f_code
int f_code
Definition: ratecontrol.h:43
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1281
MPVMainEncContext::mb_var_sum
int64_t mb_var_sum
sum of MB variance for current frame
Definition: mpegvideoenc.h:239
RateControlEntry::p_tex_bits
int p_tex_bits
Definition: ratecontrol.h:47
RateControlContext::short_term_qcount
double short_term_qcount
count of recent qscales
Definition: ratecontrol.h:66
qp2bits
static double qp2bits(const RateControlEntry *rce, double qp)
Definition: ratecontrol.c:79
const_values
static const double const_values[]
Definition: eval.c:28
bits2qp
static double bits2qp(const RateControlEntry *rce, double bits)
Definition: ratecontrol.c:92
av_expr_parse
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:710
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:574
AVCodecContext::i_quant_factor
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition: avcodec.h:834
RateControlContext::cplx_tab
float * cplx_tab
Definition: ratecontrol.h:93
ff_vbv_update
int ff_vbv_update(MPVMainEncContext *m, int frame_size)
Definition: ratecontrol.c:728
RateControlContext::qmod_amp
float qmod_amp
Definition: ratecontrol.h:85
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:508
func1_names
static const char *const func1_names[]
Definition: vf_rotate.c:188
av_expr_free
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:358
AVRational::num
int num
Numerator.
Definition: rational.h:59
RateControlContext::pred
Predictor pred[5]
Definition: ratecontrol.h:64
MPVMainEncContext::stuffing_bits
int stuffing_bits
bits used for stuffing
Definition: mpegvideoenc.h:228
ceil
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
RateControlContext
rate control context.
Definition: ratecontrol.h:60
RateControlContext::num_entries
int num_entries
number of RateControlEntries
Definition: ratecontrol.h:61
RcOverride::quality_factor
float quality_factor
Definition: avcodec.h:208
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::rc_initial_buffer_occupancy
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:1338
emms_c
#define emms_c()
Definition: emms.h:63
float
float
Definition: af_crystalizer.c:122
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCodecContext::stats_in
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1360
M_E
#define M_E
Definition: mathematics.h:37
MPVMainEncContext::intra_only
int intra_only
if true, only intra pictures are generated
Definition: mpegvideoenc.h:175
RcOverride
Definition: avcodec.h:204
MPVMainEncContext::mc_mb_var_sum
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
Definition: mpegvideoenc.h:240
frame_size
int frame_size
Definition: mxfenc.c:2446
MPVMainEncContext::rc_context
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
Definition: mpegvideoenc.h:234
RateControlContext::pass1_wanted_bits
double pass1_wanted_bits
bits which should have been output by the pass1 code (including complexity init)
Definition: ratecontrol.h:68
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
RateControlContext::qsquish
float qsquish
ratecontrol qmin qmax limiting method 0-> clipping, 1-> use a nice continuous function to limit qscal...
Definition: ratecontrol.h:84
bits
uint8_t bits
Definition: vp3data.h:128
RateControlEntry::misc_bits
int misc_bits
Definition: ratecontrol.h:48
MPVMainEncContext::header_bits
int header_bits
Definition: mpegvideoenc.h:227
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
Predictor::coeff
double coeff
Definition: ratecontrol.h:34
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
RateControlEntry::new_pict_type
int new_pict_type
Definition: ratecontrol.h:51
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:792
MPVMainEncContext::max_b_frames
int max_b_frames
max number of B-frames
Definition: mpegvideoenc.h:177
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1310
Predictor::count
double count
Definition: ratecontrol.h:35
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
if
if(ret)
Definition: filter_design.txt:179
AVCodecContext::rc_buffer_size
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1295
RateControlEntry::b_code
int b_code
Definition: ratecontrol.h:44
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
init_pass2
static int init_pass2(MPVMainEncContext *const m)
Definition: ratecontrol.c:340
MPVMainEncContext::lmin
int lmin
Definition: mpegvideoenc.h:206
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_rate_estimate_qscale
float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
Definition: ratecontrol.c:918
AVCodecContext::qblur
float qblur
amount of qscale smoothing over time (0.0-1.0)
Definition: avcodec.h:1267
isnan
#define isnan(x)
Definition: libm.h:342
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
double
double
Definition: af_crystalizer.c:132
RateControlEntry::pict_type
int pict_type
Definition: ratecontrol.h:40
modify_qscale
static double modify_qscale(MPVMainEncContext *const m, const RateControlEntry *rce, double q, int frame_num)
Definition: ratecontrol.c:174
RateControlEntry::header_bits
int header_bits
Definition: ratecontrol.h:49
av_clipf
av_clipf
Definition: af_crystalizer.c:122
exp
int8_t exp
Definition: eval.c:73
MPVMainEncContext
Definition: mpegvideoenc.h:172
index
int index
Definition: gxfenc.c:90
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
RateControlContext::qscale_sum
uint64_t qscale_sum[5]
Definition: ratecontrol.h:76
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
RateControlEntry::new_qscale
float new_qscale
Definition: ratecontrol.h:52
update_predictor
static void update_predictor(Predictor *p, double q, double var, double size)
Definition: ratecontrol.c:777
AVCodecContext::qcompress
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:1266
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:558
eval.h
MPVMainEncContext::last_pict_type
int last_pict_type
Definition: mpegvideoenc.h:232
AVCodecContext::rc_override
RcOverride * rc_override
Definition: avcodec.h:1303
RateControlContext::mv_bits_sum
uint64_t mv_bits_sum[5]
Definition: ratecontrol.h:75
size
int size
Definition: twinvq_data.h:10344
CANDIDATE_MB_TYPE_INTRA
#define CANDIDATE_MB_TYPE_INTRA
Definition: mpegvideoenc.h:263
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
MPVMainEncContext::frame_bits
int frame_bits
bits used for the current frame
Definition: mpegvideoenc.h:226
Predictor::decay
double decay
Definition: ratecontrol.h:36
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
get_fps
static double get_fps(AVCodecContext *avctx)
Definition: ratecontrol.c:74
AV_CODEC_FLAG_PASS2
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:314
Predictor
Definition: ratecontrol.h:33
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
attributes.h
RateControlContext::pass1_rc_eq_output_sum
double pass1_rc_eq_output_sum
sum of the output of the rc equation, this is used for normalization
Definition: ratecontrol.h:67
M_PI
#define M_PI
Definition: mathematics.h:67
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
AVCodecContext::b_quant_factor
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:818
av_get_picture_type_char
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:40
emms.h
RateControlEntry::qscale
float qscale
Definition: ratecontrol.h:41
RateControlContext::buffer_aggressivity
float buffer_aggressivity
Definition: ratecontrol.h:88
func1
static double(*const func1[])(void *, double)
Definition: vf_rotate.c:182
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
RateControlEntry::mv_bits
int mv_bits
Definition: ratecontrol.h:45
RateControlContext::last_mc_mb_var_sum
int64_t last_mc_mb_var_sum
Definition: ratecontrol.h:71
internal.h
RateControlContext::frame_count
int frame_count[5]
Definition: ratecontrol.h:77
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
ff_rate_control_init
av_cold int ff_rate_control_init(MPVMainEncContext *const m)
Definition: ratecontrol.c:503
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
MPVMainEncContext::border_masking
float border_masking
Definition: mpegvideoenc.h:205
ff_write_pass1_stats
void ff_write_pass1_stats(MPVMainEncContext *const m)
Definition: ratecontrol.c:38
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
RateControlContext::buffer_index
double buffer_index
amount of bits in the video/audio buffer
Definition: ratecontrol.h:63
ff_get_2pass_fcode
void ff_get_2pass_fcode(MPVMainEncContext *const m)
Definition: ratecontrol.c:906
get_qminmax
static void get_qminmax(int *qmin_ret, int *qmax_ret, MPVMainEncContext *const m, int pict_type)
Get the qmin & qmax for pict_type.
Definition: ratecontrol.c:145
avcodec.h
RateControlContext::last_non_b_pict_type
int last_non_b_pict_type
Definition: ratecontrol.h:78
RateControlContext::last_qscale_for
double last_qscale_for[5]
last qscale for a specific pict type, used for max_diff & ipb factor stuff
Definition: ratecontrol.h:70
bits2qp_cb
static double bits2qp_cb(void *rce, double qp)
Definition: ratecontrol.c:100
const_names
static const char *const const_names[]
Definition: eval.c:34
MPVPicture::f
struct AVFrame * f
Definition: mpegpicture.h:59
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
RateControlContext::i_cplx_sum
uint64_t i_cplx_sum[5]
Definition: ratecontrol.h:73
RateControlEntry::mc_mb_var_sum
int64_t mc_mb_var_sum
Definition: ratecontrol.h:53
AVCodecContext
main external API structure.
Definition: avcodec.h:451
RateControlContext::short_term_qsum
double short_term_qsum
sum of recent qscales
Definition: ratecontrol.h:65
MPVMainEncContext::lmax
int lmax
Definition: mpegvideoenc.h:206
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1274
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:841
RateControlContext::p_cplx_sum
uint64_t p_cplx_sum[5]
Definition: ratecontrol.h:74
AVCodecContext::ticks_per_frame
attribute_deprecated int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:590
qp2bits_cb
static double qp2bits_cb(void *rce, double qp)
Definition: ratecontrol.c:87
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1414
factor
static const int factor[16]
Definition: vf_pp7.c:80
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
mem.h
AVCodecContext::rc_max_available_vbv_use
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow.
Definition: avcodec.h:1324
RateControlEntry::expected_bits
uint64_t expected_bits
Definition: ratecontrol.h:50
predict_size
static double predict_size(Predictor *p, double q, double var)
Definition: ratecontrol.c:772
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::rc_override_count
int rc_override_count
ratecontrol override, see RcOverride
Definition: avcodec.h:1302
RateControlEntry::i_tex_bits
int i_tex_bits
Definition: ratecontrol.h:46
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:80
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
RateControlEntry::mb_var_sum
int64_t mb_var_sum
Definition: ratecontrol.h:54
MPVPicture
MPVPicture.
Definition: mpegpicture.h:58
RateControlContext::last_qscale
double last_qscale
Definition: ratecontrol.h:69
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
snprintf
#define snprintf
Definition: snprintf.h:34
RateControlContext::entry
RateControlEntry * entry
Definition: ratecontrol.h:62
MPVMainEncContext::s
MPVEncContext s
The main slicecontext.
Definition: mpegvideoenc.h:173
adaptive_quantization
static void adaptive_quantization(RateControlContext *const rcc, MPVMainEncContext *const m, double q)
Definition: ratecontrol.c:789
ff_rate_control_uninit
av_cold void ff_rate_control_uninit(RateControlContext *rcc)
Definition: ratecontrol.c:717