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