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