FFmpeg  4.3.8
libvpxenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Google, Inc.
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * VP8/9 encoder support via libvpx
24  */
25 
26 #define VPX_DISABLE_CTRL_TYPECHECKS 1
27 #define VPX_CODEC_DISABLE_COMPAT 1
28 #include <vpx/vpx_encoder.h>
29 #include <vpx/vp8cx.h>
30 
31 #include "avcodec.h"
32 #include "internal.h"
33 #include "libavutil/avassert.h"
34 #include "libvpx.h"
35 #include "packet_internal.h"
36 #include "profiles.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/base64.h"
39 #include "libavutil/common.h"
40 #include "libavutil/internal.h"
41 #include "libavutil/intreadwrite.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/opt.h"
44 
45 /**
46  * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
47  * One encoded frame returned from the library.
48  */
49 struct FrameListData {
50  void *buf; /**< compressed data buffer */
51  size_t sz; /**< length of compressed data */
52  void *buf_alpha;
53  size_t sz_alpha;
54  int64_t pts; /**< time stamp to show frame
55  (in timebase units) */
56  unsigned long duration; /**< duration to show frame
57  (in timebase units) */
58  uint32_t flags; /**< flags for this frame */
59  uint64_t sse[4];
60  int have_sse; /**< true if we have pending sse[] */
61  uint64_t frame_number;
62  struct FrameListData *next;
63 };
64 
65 typedef struct VPxEncoderContext {
66  AVClass *class;
67  struct vpx_codec_ctx encoder;
68  struct vpx_image rawimg;
69  struct vpx_codec_ctx encoder_alpha;
70  struct vpx_image rawimg_alpha;
72  struct vpx_fixed_buf twopass_stats;
73  int deadline; //i.e., RT/GOOD/BEST
74  uint64_t sse[4];
75  int have_sse; /**< true if we have pending sse[] */
76  uint64_t frame_number;
78 
79  int cpu_used;
80  int sharpness;
81  /**
82  * VP8 specific flags, see VP8F_* below.
83  */
84  int flags;
85 #define VP8F_ERROR_RESILIENT 0x00000001 ///< Enable measures appropriate for streaming over lossy links
86 #define VP8F_AUTO_ALT_REF 0x00000002 ///< Enable automatic alternate reference frame generation
87 
89 
92  int arnr_type;
93 
94  int tune;
95 
98  int crf;
103 
107 
108  // VP9-only
109  int lossless;
113  int aq_mode;
116  int vpx_cs;
117  float level;
118  int row_mt;
122  /**
123  * If the driver does not support ROI then warn the first time we
124  * encounter a frame with ROI side data.
125  */
127 } VPxContext;
128 
129 /** String mappings for enum vp8e_enc_control_id */
130 static const char *const ctlidstr[] = {
131  [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED",
132  [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF",
133  [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
134  [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD",
135  [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS",
136  [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES",
137  [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH",
138  [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE",
139  [VP8E_SET_TUNING] = "VP8E_SET_TUNING",
140  [VP8E_SET_CQ_LEVEL] = "VP8E_SET_CQ_LEVEL",
141  [VP8E_SET_MAX_INTRA_BITRATE_PCT] = "VP8E_SET_MAX_INTRA_BITRATE_PCT",
142  [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS",
143  [VP8E_SET_TEMPORAL_LAYER_ID] = "VP8E_SET_TEMPORAL_LAYER_ID",
144 #if CONFIG_LIBVPX_VP9_ENCODER
145  [VP9E_SET_LOSSLESS] = "VP9E_SET_LOSSLESS",
146  [VP9E_SET_TILE_COLUMNS] = "VP9E_SET_TILE_COLUMNS",
147  [VP9E_SET_TILE_ROWS] = "VP9E_SET_TILE_ROWS",
148  [VP9E_SET_FRAME_PARALLEL_DECODING] = "VP9E_SET_FRAME_PARALLEL_DECODING",
149  [VP9E_SET_AQ_MODE] = "VP9E_SET_AQ_MODE",
150  [VP9E_SET_COLOR_SPACE] = "VP9E_SET_COLOR_SPACE",
151  [VP9E_SET_SVC_LAYER_ID] = "VP9E_SET_SVC_LAYER_ID",
152 #if VPX_ENCODER_ABI_VERSION >= 12
153  [VP9E_SET_SVC_PARAMETERS] = "VP9E_SET_SVC_PARAMETERS",
154 #endif
155  [VP9E_SET_SVC] = "VP9E_SET_SVC",
156 #if VPX_ENCODER_ABI_VERSION >= 11
157  [VP9E_SET_COLOR_RANGE] = "VP9E_SET_COLOR_RANGE",
158 #endif
159 #if VPX_ENCODER_ABI_VERSION >= 12
160  [VP9E_SET_TARGET_LEVEL] = "VP9E_SET_TARGET_LEVEL",
161  [VP9E_GET_LEVEL] = "VP9E_GET_LEVEL",
162 #endif
163 #ifdef VPX_CTRL_VP9E_SET_ROW_MT
164  [VP9E_SET_ROW_MT] = "VP9E_SET_ROW_MT",
165 #endif
166 #ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT
167  [VP9E_SET_TUNE_CONTENT] = "VP9E_SET_TUNE_CONTENT",
168 #endif
169 #ifdef VPX_CTRL_VP9E_SET_TPL
170  [VP9E_SET_TPL] = "VP9E_SET_TPL",
171 #endif
172 #endif
173 };
174 
175 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
176 {
177  VPxContext *ctx = avctx->priv_data;
178  const char *error = vpx_codec_error(&ctx->encoder);
179  const char *detail = vpx_codec_error_detail(&ctx->encoder);
180 
181  av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
182  if (detail)
183  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
184 }
185 
187  const struct vpx_codec_enc_cfg *cfg)
188 {
189  int width = -30;
190  int level = AV_LOG_DEBUG;
191  int i;
192 
193  av_log(avctx, level, "vpx_codec_enc_cfg\n");
194  av_log(avctx, level, "generic settings\n"
195  " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
197  " %*s%u\n %*s%u\n"
198 #endif
199  " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
200  width, "g_usage:", cfg->g_usage,
201  width, "g_threads:", cfg->g_threads,
202  width, "g_profile:", cfg->g_profile,
203  width, "g_w:", cfg->g_w,
204  width, "g_h:", cfg->g_h,
206  width, "g_bit_depth:", cfg->g_bit_depth,
207  width, "g_input_bit_depth:", cfg->g_input_bit_depth,
208 #endif
209  width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
210  width, "g_error_resilient:", cfg->g_error_resilient,
211  width, "g_pass:", cfg->g_pass,
212  width, "g_lag_in_frames:", cfg->g_lag_in_frames);
213  av_log(avctx, level, "rate control settings\n"
214  " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
215  " %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n",
216  width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
217  width, "rc_resize_allowed:", cfg->rc_resize_allowed,
218  width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
219  width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
220  width, "rc_end_usage:", cfg->rc_end_usage,
221  width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
222  width, "rc_target_bitrate:", cfg->rc_target_bitrate);
223  av_log(avctx, level, "quantizer settings\n"
224  " %*s%u\n %*s%u\n",
225  width, "rc_min_quantizer:", cfg->rc_min_quantizer,
226  width, "rc_max_quantizer:", cfg->rc_max_quantizer);
227  av_log(avctx, level, "bitrate tolerance\n"
228  " %*s%u\n %*s%u\n",
229  width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
230  width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
231  av_log(avctx, level, "temporal layering settings\n"
232  " %*s%u\n", width, "ts_number_layers:", cfg->ts_number_layers);
233  if (avctx->codec_id == AV_CODEC_ID_VP8) {
234  av_log(avctx, level,
235  "\n %*s", width, "ts_target_bitrate:");
236  for (i = 0; i < VPX_TS_MAX_LAYERS; i++)
237  av_log(avctx, level,
238  "%u ", cfg->ts_target_bitrate[i]);
239  }
240 #if (VPX_ENCODER_ABI_VERSION >= 12) && CONFIG_LIBVPX_VP9_ENCODER
241  if (avctx->codec_id == AV_CODEC_ID_VP9) {
242  av_log(avctx, level,
243  "\n %*s", width, "layer_target_bitrate:");
244  for (i = 0; i < VPX_TS_MAX_LAYERS; i++)
245  av_log(avctx, level,
246  "%u ", cfg->layer_target_bitrate[i]);
247  }
248 #endif
249  av_log(avctx, level, "\n");
250  av_log(avctx, level,
251  "\n %*s", width, "ts_rate_decimator:");
252  for (i = 0; i < VPX_TS_MAX_LAYERS; i++)
253  av_log(avctx, level, "%u ", cfg->ts_rate_decimator[i]);
254  av_log(avctx, level, "\n");
255  av_log(avctx, level,
256  "\n %*s%u\n", width, "ts_periodicity:", cfg->ts_periodicity);
257  av_log(avctx, level,
258  "\n %*s", width, "ts_layer_id:");
259  for (i = 0; i < VPX_TS_MAX_PERIODICITY; i++)
260  av_log(avctx, level, "%u ", cfg->ts_layer_id[i]);
261  av_log(avctx, level, "\n");
262  av_log(avctx, level, "decoder buffer model\n"
263  " %*s%u\n %*s%u\n %*s%u\n",
264  width, "rc_buf_sz:", cfg->rc_buf_sz,
265  width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
266  width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
267  av_log(avctx, level, "2 pass rate control settings\n"
268  " %*s%u\n %*s%u\n %*s%u\n",
269  width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
270  width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
271  width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
272 #if VPX_ENCODER_ABI_VERSION >= 14
273  av_log(avctx, level, " %*s%u\n",
274  width, "rc_2pass_vbr_corpus_complexity:", cfg->rc_2pass_vbr_corpus_complexity);
275 #endif
276  av_log(avctx, level, "keyframing settings\n"
277  " %*s%d\n %*s%u\n %*s%u\n",
278  width, "kf_mode:", cfg->kf_mode,
279  width, "kf_min_dist:", cfg->kf_min_dist,
280  width, "kf_max_dist:", cfg->kf_max_dist);
281  av_log(avctx, level, "\n");
282 }
283 
284 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
285 {
286  struct FrameListData **p = list;
287 
288  while (*p)
289  p = &(*p)->next;
290  *p = cx_frame;
291  cx_frame->next = NULL;
292 }
293 
294 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
295 {
296  av_freep(&cx_frame->buf);
297  if (cx_frame->buf_alpha)
298  av_freep(&cx_frame->buf_alpha);
299  av_freep(&cx_frame);
300 }
301 
302 static av_cold void free_frame_list(struct FrameListData *list)
303 {
304  struct FrameListData *p = list;
305 
306  while (p) {
307  list = list->next;
308  free_coded_frame(p);
309  p = list;
310  }
311 }
312 
314  enum vp8e_enc_control_id id, int val)
315 {
316  VPxContext *ctx = avctx->priv_data;
317  char buf[80];
318  int width = -30;
319  int res;
320 
321  snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
322  av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
323 
324  res = vpx_codec_control(&ctx->encoder, id, val);
325  if (res != VPX_CODEC_OK) {
326  snprintf(buf, sizeof(buf), "Failed to set %s codec control",
327  ctlidstr[id]);
328  log_encoder_error(avctx, buf);
329  }
330 
331  return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
332 }
333 
334 #if VPX_ENCODER_ABI_VERSION >= 12
335 static av_cold int codecctl_intp(AVCodecContext *avctx,
336  enum vp8e_enc_control_id id, int *val)
337 {
338  VPxContext *ctx = avctx->priv_data;
339  char buf[80];
340  int width = -30;
341  int res;
342 
343  snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
344  av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, *val);
345 
346  res = vpx_codec_control(&ctx->encoder, id, val);
347  if (res != VPX_CODEC_OK) {
348  snprintf(buf, sizeof(buf), "Failed to set %s codec control",
349  ctlidstr[id]);
350  log_encoder_error(avctx, buf);
351  }
352 
353  return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
354 }
355 #endif
356 
357 static av_cold int vpx_free(AVCodecContext *avctx)
358 {
359  VPxContext *ctx = avctx->priv_data;
360 
361 #if VPX_ENCODER_ABI_VERSION >= 12
362  if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->level >= 0 &&
363  !(avctx->flags & AV_CODEC_FLAG_PASS1)) {
364  int level_out = 0;
365  if (!codecctl_intp(avctx, VP9E_GET_LEVEL, &level_out))
366  av_log(avctx, AV_LOG_INFO, "Encoded level %.1f\n", level_out * 0.1);
367  }
368 #endif
369 
370  av_freep(&ctx->ts_layer_flags);
371 
372  vpx_codec_destroy(&ctx->encoder);
373  if (ctx->is_alpha) {
374  vpx_codec_destroy(&ctx->encoder_alpha);
375  av_freep(&ctx->rawimg_alpha.planes[VPX_PLANE_U]);
376  av_freep(&ctx->rawimg_alpha.planes[VPX_PLANE_V]);
377  }
378  av_freep(&ctx->twopass_stats.buf);
379  av_freep(&avctx->stats_out);
381  return 0;
382 }
383 
384 static void vp8_ts_parse_int_array(int *dest, char *value, size_t value_len, int max_entries)
385 {
386  int dest_idx = 0;
387  char *saveptr = NULL;
388  char *token = av_strtok(value, ",", &saveptr);
389 
390  while (token && dest_idx < max_entries) {
391  dest[dest_idx++] = strtoul(token, NULL, 10);
392  token = av_strtok(NULL, ",", &saveptr);
393  }
394 }
395 
396 static void set_temporal_layer_pattern(int layering_mode, vpx_codec_enc_cfg_t *cfg,
397  int *layer_flags, int *flag_periodicity)
398 {
399  switch (layering_mode) {
400  case 2: {
401  /**
402  * 2-layers, 2-frame period.
403  */
404  static const int ids[2] = { 0, 1 };
405  cfg->ts_periodicity = 2;
406  *flag_periodicity = 2;
407  cfg->ts_number_layers = 2;
408  cfg->ts_rate_decimator[0] = 2;
409  cfg->ts_rate_decimator[1] = 1;
410  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
411 
412  layer_flags[0] =
413  VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
414  VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
415  layer_flags[1] =
416  VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF |
417  VP8_EFLAG_NO_UPD_LAST |
418  VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF;
419  break;
420  }
421  case 3: {
422  /**
423  * 3-layers structure with one reference frame.
424  * This works same as temporal_layering_mode 3.
425  *
426  * 3-layers, 4-frame period.
427  */
428  static const int ids[4] = { 0, 2, 1, 2 };
429  cfg->ts_periodicity = 4;
430  *flag_periodicity = 4;
431  cfg->ts_number_layers = 3;
432  cfg->ts_rate_decimator[0] = 4;
433  cfg->ts_rate_decimator[1] = 2;
434  cfg->ts_rate_decimator[2] = 1;
435  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
436 
437  /**
438  * 0=L, 1=GF, 2=ARF,
439  * Intra-layer prediction disabled.
440  */
441  layer_flags[0] =
442  VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
443  VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
444  layer_flags[1] =
445  VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
446  VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
447  VP8_EFLAG_NO_UPD_ARF;
448  layer_flags[2] =
449  VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
450  VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
451  layer_flags[3] =
452  VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF |
453  VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
454  VP8_EFLAG_NO_UPD_ARF;
455  break;
456  }
457  case 4: {
458  /**
459  * 3-layers structure.
460  * added dependency between the two TL2 frames (on top of case 3).
461  * 3-layers, 4-frame period.
462  */
463  static const int ids[4] = { 0, 2, 1, 2 };
464  cfg->ts_periodicity = 4;
465  *flag_periodicity = 4;
466  cfg->ts_number_layers = 3;
467  cfg->ts_rate_decimator[0] = 4;
468  cfg->ts_rate_decimator[1] = 2;
469  cfg->ts_rate_decimator[2] = 1;
470  memcpy(cfg->ts_layer_id, ids, sizeof(ids));
471 
472  /**
473  * 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
474  */
475  layer_flags[0] =
476  VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
477  VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
478  layer_flags[1] =
479  VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
480  VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
481  layer_flags[2] =
482  VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF |
483  VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST;
484  layer_flags[3] =
485  VP8_EFLAG_NO_REF_LAST |
486  VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
487  VP8_EFLAG_NO_UPD_ARF;
488  break;
489  }
490  default:
491  /**
492  * do not change the layer_flags or the flag_periodicity in this case;
493  * it might be that the code is using external flags to be used.
494  */
495  break;
496 
497  }
498 }
499 
500 static int vpx_ts_param_parse(VPxContext *ctx, struct vpx_codec_enc_cfg *enccfg,
501  char *key, char *value, enum AVCodecID codec_id)
502 {
503  size_t value_len = strlen(value);
504  int ts_layering_mode = 0;
505 
506  if (!value_len)
507  return -1;
508 
509  if (!strcmp(key, "ts_number_layers"))
510  enccfg->ts_number_layers = strtoul(value, &value, 10);
511  else if (!strcmp(key, "ts_target_bitrate")) {
512  if (codec_id == AV_CODEC_ID_VP8)
513  vp8_ts_parse_int_array(enccfg->ts_target_bitrate, value, value_len, VPX_TS_MAX_LAYERS);
514 #if (VPX_ENCODER_ABI_VERSION >= 12) && CONFIG_LIBVPX_VP9_ENCODER
515  if (codec_id == AV_CODEC_ID_VP9)
516  vp8_ts_parse_int_array(enccfg->layer_target_bitrate, value, value_len, VPX_TS_MAX_LAYERS);
517 #endif
518  } else if (!strcmp(key, "ts_rate_decimator")) {
519  vp8_ts_parse_int_array(enccfg->ts_rate_decimator, value, value_len, VPX_TS_MAX_LAYERS);
520  } else if (!strcmp(key, "ts_periodicity")) {
521  enccfg->ts_periodicity = strtoul(value, &value, 10);
522  } else if (!strcmp(key, "ts_layer_id")) {
523  vp8_ts_parse_int_array(enccfg->ts_layer_id, value, value_len, VPX_TS_MAX_PERIODICITY);
524  } else if (!strcmp(key, "ts_layering_mode")) {
525  /* option for pre-defined temporal structures in function set_temporal_layer_pattern. */
526  ts_layering_mode = strtoul(value, &value, 4);
527  }
528 
529 #if (VPX_ENCODER_ABI_VERSION >= 12) && CONFIG_LIBVPX_VP9_ENCODER
530  enccfg->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS; // only bypass mode is supported for now.
531  enccfg->ss_number_layers = 1; // TODO: add spatial scalability support.
532 #endif
533  if (ts_layering_mode) {
534  // make sure the ts_layering_mode comes at the end of the ts_parameter string to ensure that
535  // correct configuration is done.
536  ctx->ts_layer_flags = av_malloc_array(VPX_TS_MAX_PERIODICITY, sizeof(*ctx->ts_layer_flags));
537  set_temporal_layer_pattern(ts_layering_mode, enccfg, ctx->ts_layer_flags, &enccfg->ts_periodicity);
538  }
539 
540  return 0;
541 }
542 
543 #if CONFIG_LIBVPX_VP9_ENCODER
544 static int set_pix_fmt(AVCodecContext *avctx, vpx_codec_caps_t codec_caps,
545  struct vpx_codec_enc_cfg *enccfg, vpx_codec_flags_t *flags,
546  vpx_img_fmt_t *img_fmt)
547 {
548  VPxContext av_unused *ctx = avctx->priv_data;
549  enccfg->g_bit_depth = enccfg->g_input_bit_depth = 8;
550  switch (avctx->pix_fmt) {
551  case AV_PIX_FMT_YUV420P:
552  case AV_PIX_FMT_YUVA420P:
553  enccfg->g_profile = 0;
554  *img_fmt = VPX_IMG_FMT_I420;
555  return 0;
556  case AV_PIX_FMT_YUV422P:
557  enccfg->g_profile = 1;
558  *img_fmt = VPX_IMG_FMT_I422;
559  return 0;
560  case AV_PIX_FMT_YUV440P:
561  enccfg->g_profile = 1;
562  *img_fmt = VPX_IMG_FMT_I440;
563  return 0;
564  case AV_PIX_FMT_GBRP:
565  ctx->vpx_cs = VPX_CS_SRGB;
566  case AV_PIX_FMT_YUV444P:
567  enccfg->g_profile = 1;
568  *img_fmt = VPX_IMG_FMT_I444;
569  return 0;
572  if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
573  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
574  avctx->pix_fmt == AV_PIX_FMT_YUV420P10 ? 10 : 12;
575  enccfg->g_profile = 2;
576  *img_fmt = VPX_IMG_FMT_I42016;
577  *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
578  return 0;
579  }
580  break;
583  if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
584  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
585  avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12;
586  enccfg->g_profile = 3;
587  *img_fmt = VPX_IMG_FMT_I42216;
588  *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
589  return 0;
590  }
591  break;
594  if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
595  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
596  avctx->pix_fmt == AV_PIX_FMT_YUV440P10 ? 10 : 12;
597  enccfg->g_profile = 3;
598  *img_fmt = VPX_IMG_FMT_I44016;
599  *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
600  return 0;
601  }
602  break;
603  case AV_PIX_FMT_GBRP10:
604  case AV_PIX_FMT_GBRP12:
605  ctx->vpx_cs = VPX_CS_SRGB;
608  if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) {
609  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
610  avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ||
611  avctx->pix_fmt == AV_PIX_FMT_GBRP10 ? 10 : 12;
612  enccfg->g_profile = 3;
613  *img_fmt = VPX_IMG_FMT_I44416;
614  *flags |= VPX_CODEC_USE_HIGHBITDEPTH;
615  return 0;
616  }
617  break;
618  default:
619  break;
620  }
621  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
622  return AVERROR_INVALIDDATA;
623 }
624 
625 static void set_colorspace(AVCodecContext *avctx)
626 {
627  enum vpx_color_space vpx_cs;
628  VPxContext *ctx = avctx->priv_data;
629 
630  if (ctx->vpx_cs) {
631  vpx_cs = ctx->vpx_cs;
632  } else {
633  switch (avctx->colorspace) {
634  case AVCOL_SPC_RGB: vpx_cs = VPX_CS_SRGB; break;
635  case AVCOL_SPC_BT709: vpx_cs = VPX_CS_BT_709; break;
636  case AVCOL_SPC_UNSPECIFIED: vpx_cs = VPX_CS_UNKNOWN; break;
637  case AVCOL_SPC_RESERVED: vpx_cs = VPX_CS_RESERVED; break;
638  case AVCOL_SPC_BT470BG: vpx_cs = VPX_CS_BT_601; break;
639  case AVCOL_SPC_SMPTE170M: vpx_cs = VPX_CS_SMPTE_170; break;
640  case AVCOL_SPC_SMPTE240M: vpx_cs = VPX_CS_SMPTE_240; break;
641  case AVCOL_SPC_BT2020_NCL: vpx_cs = VPX_CS_BT_2020; break;
642  default:
643  av_log(avctx, AV_LOG_WARNING, "Unsupported colorspace (%d)\n",
644  avctx->colorspace);
645  return;
646  }
647  }
648  codecctl_int(avctx, VP9E_SET_COLOR_SPACE, vpx_cs);
649 }
650 
651 #if VPX_ENCODER_ABI_VERSION >= 11
652 static void set_color_range(AVCodecContext *avctx)
653 {
654  enum vpx_color_range vpx_cr;
655  switch (avctx->color_range) {
657  case AVCOL_RANGE_MPEG: vpx_cr = VPX_CR_STUDIO_RANGE; break;
658  case AVCOL_RANGE_JPEG: vpx_cr = VPX_CR_FULL_RANGE; break;
659  default:
660  av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n",
661  avctx->color_range);
662  return;
663  }
664 
665  codecctl_int(avctx, VP9E_SET_COLOR_RANGE, vpx_cr);
666 }
667 #endif
668 #endif
669 
670 /**
671  * Set the target bitrate to VPX library default. Also set CRF to 32 if needed.
672  */
673 static void set_vp8_defaults(AVCodecContext *avctx,
674  struct vpx_codec_enc_cfg *enccfg)
675 {
676  VPxContext *ctx = avctx->priv_data;
677  av_assert0(!avctx->bit_rate);
678  avctx->bit_rate = enccfg->rc_target_bitrate * 1000;
679  if (enccfg->rc_end_usage == VPX_CQ) {
680  av_log(avctx, AV_LOG_WARNING,
681  "Bitrate not specified for constrained quality mode, using default of %dkbit/sec\n",
682  enccfg->rc_target_bitrate);
683  } else {
684  enccfg->rc_end_usage = VPX_CQ;
685  ctx->crf = 32;
686  av_log(avctx, AV_LOG_WARNING,
687  "Neither bitrate nor constrained quality specified, using default CRF of %d and bitrate of %dkbit/sec\n",
688  ctx->crf, enccfg->rc_target_bitrate);
689  }
690 }
691 
692 
693 #if CONFIG_LIBVPX_VP9_ENCODER
694 /**
695  * Keep the target bitrate at 0 to engage constant quality mode. If CRF is not
696  * set, use 32.
697  */
698 static void set_vp9_defaults(AVCodecContext *avctx,
699  struct vpx_codec_enc_cfg *enccfg)
700 {
701  VPxContext *ctx = avctx->priv_data;
702  av_assert0(!avctx->bit_rate);
703  if (enccfg->rc_end_usage != VPX_Q && ctx->lossless < 0) {
704  enccfg->rc_end_usage = VPX_Q;
705  ctx->crf = 32;
706  av_log(avctx, AV_LOG_WARNING,
707  "Neither bitrate nor constrained quality specified, using default CRF of %d\n",
708  ctx->crf);
709  }
710 }
711 #endif
712 
713 /**
714  * Called when the bitrate is not set. It sets appropriate default values for
715  * bitrate and CRF.
716  */
717 static void set_vpx_defaults(AVCodecContext *avctx,
718  struct vpx_codec_enc_cfg *enccfg)
719 {
720  av_assert0(!avctx->bit_rate);
721 #if CONFIG_LIBVPX_VP9_ENCODER
722  if (avctx->codec_id == AV_CODEC_ID_VP9) {
723  set_vp9_defaults(avctx, enccfg);
724  return;
725  }
726 #endif
727  set_vp8_defaults(avctx, enccfg);
728 }
729 
730 static av_cold int vpx_init(AVCodecContext *avctx,
731  const struct vpx_codec_iface *iface)
732 {
733  VPxContext *ctx = avctx->priv_data;
734  struct vpx_codec_enc_cfg enccfg = { 0 };
735  struct vpx_codec_enc_cfg enccfg_alpha;
736  vpx_codec_flags_t flags = (avctx->flags & AV_CODEC_FLAG_PSNR) ? VPX_CODEC_USE_PSNR : 0;
737  AVCPBProperties *cpb_props;
738  int res;
739  vpx_img_fmt_t img_fmt = VPX_IMG_FMT_I420;
740 #if CONFIG_LIBVPX_VP9_ENCODER
741  vpx_codec_caps_t codec_caps = vpx_codec_get_caps(iface);
742  vpx_svc_extra_cfg_t svc_params;
743 #endif
744  AVDictionaryEntry* en = NULL;
745 
746  av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
747  av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
748 
749  if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P)
750  ctx->is_alpha = 1;
751 
752  if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
753  av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
754  vpx_codec_err_to_string(res));
755  return AVERROR(EINVAL);
756  }
757 
758 #if CONFIG_LIBVPX_VP9_ENCODER
759  if (avctx->codec_id == AV_CODEC_ID_VP9) {
760  if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
761  return AVERROR(EINVAL);
762  }
763 #endif
764 
765  if(!avctx->bit_rate)
766  if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
767  av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
768  return AVERROR(EINVAL);
769  }
770 
771  dump_enc_cfg(avctx, &enccfg);
772 
773  enccfg.g_w = avctx->width;
774  enccfg.g_h = avctx->height;
775  enccfg.g_timebase.num = avctx->time_base.num;
776  enccfg.g_timebase.den = avctx->time_base.den;
777  enccfg.g_threads =
778  FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 16);
779  enccfg.g_lag_in_frames= ctx->lag_in_frames;
780 
781  if (avctx->flags & AV_CODEC_FLAG_PASS1)
782  enccfg.g_pass = VPX_RC_FIRST_PASS;
783  else if (avctx->flags & AV_CODEC_FLAG_PASS2)
784  enccfg.g_pass = VPX_RC_LAST_PASS;
785  else
786  enccfg.g_pass = VPX_RC_ONE_PASS;
787 
788  if (avctx->rc_min_rate == avctx->rc_max_rate &&
789  avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
790  enccfg.rc_end_usage = VPX_CBR;
791  } else if (ctx->crf >= 0) {
792  enccfg.rc_end_usage = VPX_CQ;
793 #if CONFIG_LIBVPX_VP9_ENCODER
794  if (!avctx->bit_rate && avctx->codec_id == AV_CODEC_ID_VP9)
795  enccfg.rc_end_usage = VPX_Q;
796 #endif
797  }
798 
799  if (avctx->bit_rate) {
800  enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
802 #if CONFIG_LIBVPX_VP9_ENCODER
803  enccfg.ss_target_bitrate[0] = enccfg.rc_target_bitrate;
804 #endif
805  } else {
806  // Set bitrate to default value. Also sets CRF to default if needed.
807  set_vpx_defaults(avctx, &enccfg);
808  }
809 
810  if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->lossless == 1) {
811  enccfg.rc_min_quantizer =
812  enccfg.rc_max_quantizer = 0;
813  } else {
814  if (avctx->qmin >= 0)
815  enccfg.rc_min_quantizer = avctx->qmin;
816  if (avctx->qmax >= 0)
817  enccfg.rc_max_quantizer = avctx->qmax;
818  }
819 
820  if (enccfg.rc_end_usage == VPX_CQ
822  || enccfg.rc_end_usage == VPX_Q
823 #endif
824  ) {
825  if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
826  av_log(avctx, AV_LOG_ERROR,
827  "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
828  ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
829  return AVERROR(EINVAL);
830  }
831  }
832 
833 #if FF_API_PRIVATE_OPT
835  if (avctx->frame_skip_threshold)
836  ctx->drop_threshold = avctx->frame_skip_threshold;
838 #endif
839  enccfg.rc_dropframe_thresh = ctx->drop_threshold;
840 
841  //0-100 (0 => CBR, 100 => VBR)
842  enccfg.rc_2pass_vbr_bias_pct = lrint(avctx->qcompress * 100);
843  if (avctx->bit_rate)
844  enccfg.rc_2pass_vbr_minsection_pct =
845  avctx->rc_min_rate * 100LL / avctx->bit_rate;
846  if (avctx->rc_max_rate)
847  enccfg.rc_2pass_vbr_maxsection_pct =
848  avctx->rc_max_rate * 100LL / avctx->bit_rate;
849 #if CONFIG_LIBVPX_VP9_ENCODER
850  if (avctx->codec_id == AV_CODEC_ID_VP9) {
851 #if VPX_ENCODER_ABI_VERSION >= 14
852  if (ctx->corpus_complexity >= 0)
853  enccfg.rc_2pass_vbr_corpus_complexity = ctx->corpus_complexity;
854 #endif
855  }
856 #endif
857 
858  if (avctx->rc_buffer_size)
859  enccfg.rc_buf_sz =
860  avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
861  if (avctx->rc_initial_buffer_occupancy)
862  enccfg.rc_buf_initial_sz =
863  avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
864  enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
865  if (ctx->rc_undershoot_pct >= 0)
866  enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct;
867  if (ctx->rc_overshoot_pct >= 0)
868  enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct;
869 
870  //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO
871  if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
872  enccfg.kf_min_dist = avctx->keyint_min;
873  if (avctx->gop_size >= 0)
874  enccfg.kf_max_dist = avctx->gop_size;
875 
876  if (enccfg.g_pass == VPX_RC_FIRST_PASS)
877  enccfg.g_lag_in_frames = 0;
878  else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
879  int decode_size, ret;
880 
881  if (!avctx->stats_in) {
882  av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
883  return AVERROR_INVALIDDATA;
884  }
885 
886  ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
887  ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
888  if (ret < 0) {
889  av_log(avctx, AV_LOG_ERROR,
890  "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
891  ctx->twopass_stats.sz);
892  ctx->twopass_stats.sz = 0;
893  return ret;
894  }
895  decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
896  ctx->twopass_stats.sz);
897  if (decode_size < 0) {
898  av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
899  return AVERROR_INVALIDDATA;
900  }
901 
902  ctx->twopass_stats.sz = decode_size;
903  enccfg.rc_twopass_stats_in = ctx->twopass_stats;
904  }
905 
906  /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
907  complexity playback on low powered devices at the expense of encode
908  quality. */
909  if (avctx->profile != FF_PROFILE_UNKNOWN)
910  enccfg.g_profile = avctx->profile;
911 
912  enccfg.g_error_resilient = ctx->error_resilient || ctx->flags & VP8F_ERROR_RESILIENT;
913 
914  while ((en = av_dict_get(ctx->vpx_ts_parameters, "", en, AV_DICT_IGNORE_SUFFIX))) {
915  if (vpx_ts_param_parse(ctx, &enccfg, en->key, en->value, avctx->codec_id) < 0)
916  av_log(avctx, AV_LOG_WARNING,
917  "Error parsing option '%s = %s'.\n",
918  en->key, en->value);
919  }
920 
921  dump_enc_cfg(avctx, &enccfg);
922  /* Construct Encoder Context */
923  res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
924  if (res != VPX_CODEC_OK) {
925  log_encoder_error(avctx, "Failed to initialize encoder");
926  return AVERROR(EINVAL);
927  }
928 #if CONFIG_LIBVPX_VP9_ENCODER
929  if (avctx->codec_id == AV_CODEC_ID_VP9 && enccfg.ts_number_layers > 1) {
930  memset(&svc_params, 0, sizeof(svc_params));
931  for (int i = 0; i < enccfg.ts_number_layers; ++i) {
932  svc_params.max_quantizers[i] = enccfg.rc_max_quantizer;
933  svc_params.min_quantizers[i] = enccfg.rc_min_quantizer;
934  }
935  svc_params.scaling_factor_num[0] = enccfg.g_h;
936  svc_params.scaling_factor_den[0] = enccfg.g_h;
937 #if VPX_ENCODER_ABI_VERSION >= 12
938  codecctl_int(avctx, VP9E_SET_SVC, 1);
939  codecctl_intp(avctx, VP9E_SET_SVC_PARAMETERS, (int *)&svc_params);
940 #endif
941  }
942 #endif
943  if (ctx->is_alpha) {
944  enccfg_alpha = enccfg;
945  res = vpx_codec_enc_init(&ctx->encoder_alpha, iface, &enccfg_alpha, flags);
946  if (res != VPX_CODEC_OK) {
947  log_encoder_error(avctx, "Failed to initialize alpha encoder");
948  return AVERROR(EINVAL);
949  }
950  }
951 
952  //codec control failures are currently treated only as warnings
953  av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
954  codecctl_int(avctx, VP8E_SET_CPUUSED, ctx->cpu_used);
955  if (ctx->flags & VP8F_AUTO_ALT_REF)
956  ctx->auto_alt_ref = 1;
957  if (ctx->auto_alt_ref >= 0)
958  codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF,
959  avctx->codec_id == AV_CODEC_ID_VP8 ? !!ctx->auto_alt_ref : ctx->auto_alt_ref);
960  if (ctx->arnr_max_frames >= 0)
961  codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
962  if (ctx->arnr_strength >= 0)
963  codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH, ctx->arnr_strength);
964  if (ctx->arnr_type >= 0)
965  codecctl_int(avctx, VP8E_SET_ARNR_TYPE, ctx->arnr_type);
966  if (ctx->tune >= 0)
967  codecctl_int(avctx, VP8E_SET_TUNING, ctx->tune);
968 
969  if (ctx->auto_alt_ref && ctx->is_alpha && avctx->codec_id == AV_CODEC_ID_VP8) {
970  av_log(avctx, AV_LOG_ERROR, "Transparency encoding with auto_alt_ref does not work\n");
971  return AVERROR(EINVAL);
972  }
973 
974  if (ctx->sharpness >= 0)
975  codecctl_int(avctx, VP8E_SET_SHARPNESS, ctx->sharpness);
976 
978 #if FF_API_PRIVATE_OPT
980  if (avctx->noise_reduction)
981  ctx->noise_sensitivity = avctx->noise_reduction;
983 #endif
984  codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, ctx->noise_sensitivity);
985  codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
986  }
987  codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD, ctx->static_thresh);
988  if (ctx->crf >= 0)
989  codecctl_int(avctx, VP8E_SET_CQ_LEVEL, ctx->crf);
990  if (ctx->max_intra_rate >= 0)
991  codecctl_int(avctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, ctx->max_intra_rate);
992 
993 #if CONFIG_LIBVPX_VP9_ENCODER
994  if (avctx->codec_id == AV_CODEC_ID_VP9) {
995  if (ctx->lossless >= 0)
996  codecctl_int(avctx, VP9E_SET_LOSSLESS, ctx->lossless);
997  if (ctx->tile_columns >= 0)
998  codecctl_int(avctx, VP9E_SET_TILE_COLUMNS, ctx->tile_columns);
999  if (ctx->tile_rows >= 0)
1000  codecctl_int(avctx, VP9E_SET_TILE_ROWS, ctx->tile_rows);
1001  if (ctx->frame_parallel >= 0)
1002  codecctl_int(avctx, VP9E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
1003  if (ctx->aq_mode >= 0)
1004  codecctl_int(avctx, VP9E_SET_AQ_MODE, ctx->aq_mode);
1005  set_colorspace(avctx);
1006 #if VPX_ENCODER_ABI_VERSION >= 11
1007  set_color_range(avctx);
1008 #endif
1009 #if VPX_ENCODER_ABI_VERSION >= 12
1010  codecctl_int(avctx, VP9E_SET_TARGET_LEVEL, ctx->level < 0 ? 255 : lrint(ctx->level * 10));
1011 #endif
1012 #ifdef VPX_CTRL_VP9E_SET_ROW_MT
1013  if (ctx->row_mt >= 0)
1014  codecctl_int(avctx, VP9E_SET_ROW_MT, ctx->row_mt);
1015 #endif
1016 #ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT
1017  if (ctx->tune_content >= 0)
1018  codecctl_int(avctx, VP9E_SET_TUNE_CONTENT, ctx->tune_content);
1019 #endif
1020 #ifdef VPX_CTRL_VP9E_SET_TPL
1021  if (ctx->tpl_model >= 0)
1022  codecctl_int(avctx, VP9E_SET_TPL, ctx->tpl_model);
1023 #endif
1024  }
1025 #endif
1026 
1027  av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline);
1028 
1029  //provide dummy value to initialize wrapper, values will be updated each _encode()
1030  vpx_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
1031  (unsigned char*)1);
1032 #if CONFIG_LIBVPX_VP9_ENCODER
1033  if (avctx->codec_id == AV_CODEC_ID_VP9 && (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH))
1034  ctx->rawimg.bit_depth = enccfg.g_bit_depth;
1035 #endif
1036 
1037  cpb_props = ff_add_cpb_side_data(avctx);
1038  if (!cpb_props)
1039  return AVERROR(ENOMEM);
1040 
1041  if (enccfg.rc_end_usage == VPX_CBR ||
1042  enccfg.g_pass != VPX_RC_ONE_PASS) {
1043  cpb_props->max_bitrate = avctx->rc_max_rate;
1044  cpb_props->min_bitrate = avctx->rc_min_rate;
1045  cpb_props->avg_bitrate = avctx->bit_rate;
1046  }
1047  cpb_props->buffer_size = avctx->rc_buffer_size;
1048 
1049  return 0;
1050 }
1051 
1052 static inline void cx_pktcpy(struct FrameListData *dst,
1053  const struct vpx_codec_cx_pkt *src,
1054  const struct vpx_codec_cx_pkt *src_alpha,
1055  VPxContext *ctx)
1056 {
1057  dst->pts = src->data.frame.pts;
1058  dst->duration = src->data.frame.duration;
1059  dst->flags = src->data.frame.flags;
1060  dst->sz = src->data.frame.sz;
1061  dst->buf = src->data.frame.buf;
1062  dst->have_sse = 0;
1063  /* For alt-ref frame, don't store PSNR or increment frame_number */
1064  if (!(dst->flags & VPX_FRAME_IS_INVISIBLE)) {
1065  dst->frame_number = ++ctx->frame_number;
1066  dst->have_sse = ctx->have_sse;
1067  if (ctx->have_sse) {
1068  /* associate last-seen SSE to the frame. */
1069  /* Transfers ownership from ctx to dst. */
1070  /* WARNING! This makes the assumption that PSNR_PKT comes
1071  just before the frame it refers to! */
1072  memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
1073  ctx->have_sse = 0;
1074  }
1075  } else {
1076  dst->frame_number = -1; /* sanity marker */
1077  }
1078  if (src_alpha) {
1079  dst->buf_alpha = src_alpha->data.frame.buf;
1080  dst->sz_alpha = src_alpha->data.frame.sz;
1081  } else {
1082  dst->buf_alpha = NULL;
1083  dst->sz_alpha = 0;
1084  }
1085 }
1086 
1087 /**
1088  * Store coded frame information in format suitable for return from encode2().
1089  *
1090  * Write information from @a cx_frame to @a pkt
1091  * @return packet data size on success
1092  * @return a negative AVERROR on error
1093  */
1094 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
1095  AVPacket *pkt)
1096 {
1097  int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0);
1098  uint8_t *side_data;
1099  if (ret >= 0) {
1100  int pict_type;
1101  memcpy(pkt->data, cx_frame->buf, pkt->size);
1102  pkt->pts = pkt->dts = cx_frame->pts;
1103 #if FF_API_CODED_FRAME
1105  avctx->coded_frame->pts = cx_frame->pts;
1106  avctx->coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
1108 #endif
1109 
1110  if (!!(cx_frame->flags & VPX_FRAME_IS_KEY)) {
1111  pict_type = AV_PICTURE_TYPE_I;
1112 #if FF_API_CODED_FRAME
1114  avctx->coded_frame->pict_type = pict_type;
1116 #endif
1117  pkt->flags |= AV_PKT_FLAG_KEY;
1118  } else {
1119  pict_type = AV_PICTURE_TYPE_P;
1120 #if FF_API_CODED_FRAME
1122  avctx->coded_frame->pict_type = pict_type;
1124 #endif
1125  }
1126 
1127  ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1,
1128  cx_frame->have_sse ? 3 : 0, pict_type);
1129 
1130  if (cx_frame->have_sse) {
1131  int i;
1132  /* Beware of the Y/U/V/all order! */
1133 #if FF_API_CODED_FRAME
1135  avctx->coded_frame->error[0] = cx_frame->sse[1];
1136  avctx->coded_frame->error[1] = cx_frame->sse[2];
1137  avctx->coded_frame->error[2] = cx_frame->sse[3];
1138  avctx->coded_frame->error[3] = 0; // alpha
1140 #endif
1141  for (i = 0; i < 3; ++i) {
1142  avctx->error[i] += cx_frame->sse[i + 1];
1143  }
1144  cx_frame->have_sse = 0;
1145  }
1146  if (cx_frame->sz_alpha > 0) {
1147  side_data = av_packet_new_side_data(pkt,
1149  cx_frame->sz_alpha + 8);
1150  if(!side_data) {
1151  av_packet_unref(pkt);
1152  return AVERROR(ENOMEM);
1153  }
1154  AV_WB64(side_data, 1);
1155  memcpy(side_data + 8, cx_frame->buf_alpha, cx_frame->sz_alpha);
1156  }
1157  } else {
1158  return ret;
1159  }
1160  return pkt->size;
1161 }
1162 
1163 /**
1164  * Queue multiple output frames from the encoder, returning the front-most.
1165  * In cases where vpx_codec_get_cx_data() returns more than 1 frame append
1166  * the frame queue. Return the head frame if available.
1167  * @return Stored frame size
1168  * @return AVERROR(EINVAL) on output size error
1169  * @return AVERROR(ENOMEM) on coded frame queue data allocation error
1170  */
1171 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
1172 {
1173  VPxContext *ctx = avctx->priv_data;
1174  const struct vpx_codec_cx_pkt *pkt;
1175  const struct vpx_codec_cx_pkt *pkt_alpha = NULL;
1176  const void *iter = NULL;
1177  const void *iter_alpha = NULL;
1178  int size = 0;
1179 
1180  if (ctx->coded_frame_list) {
1181  struct FrameListData *cx_frame = ctx->coded_frame_list;
1182  /* return the leading frame if we've already begun queueing */
1183  size = storeframe(avctx, cx_frame, pkt_out);
1184  if (size < 0)
1185  return size;
1186  ctx->coded_frame_list = cx_frame->next;
1187  free_coded_frame(cx_frame);
1188  }
1189 
1190  /* consume all available output from the encoder before returning. buffers
1191  are only good through the next vpx_codec call */
1192  while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter)) &&
1193  (!ctx->is_alpha ||
1194  (pkt_alpha = vpx_codec_get_cx_data(&ctx->encoder_alpha, &iter_alpha)))) {
1195  switch (pkt->kind) {
1196  case VPX_CODEC_CX_FRAME_PKT:
1197  if (!size) {
1198  struct FrameListData cx_frame;
1199 
1200  /* avoid storing the frame when the list is empty and we haven't yet
1201  provided a frame for output */
1203  cx_pktcpy(&cx_frame, pkt, pkt_alpha, ctx);
1204  size = storeframe(avctx, &cx_frame, pkt_out);
1205  if (size < 0)
1206  return size;
1207  } else {
1208  struct FrameListData *cx_frame = av_malloc(sizeof(*cx_frame));
1209 
1210  if (!cx_frame) {
1211  av_log(avctx, AV_LOG_ERROR,
1212  "Frame queue element alloc failed\n");
1213  return AVERROR(ENOMEM);
1214  }
1215  cx_pktcpy(cx_frame, pkt, pkt_alpha, ctx);
1216  cx_frame->buf = av_malloc(cx_frame->sz);
1217 
1218  if (!cx_frame->buf) {
1219  av_log(avctx, AV_LOG_ERROR,
1220  "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
1221  cx_frame->sz);
1222  av_freep(&cx_frame);
1223  return AVERROR(ENOMEM);
1224  }
1225  memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
1226  if (ctx->is_alpha) {
1227  cx_frame->buf_alpha = av_malloc(cx_frame->sz_alpha);
1228  if (!cx_frame->buf_alpha) {
1229  av_log(avctx, AV_LOG_ERROR,
1230  "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
1231  cx_frame->sz_alpha);
1232  av_free(cx_frame);
1233  return AVERROR(ENOMEM);
1234  }
1235  memcpy(cx_frame->buf_alpha, pkt_alpha->data.frame.buf, pkt_alpha->data.frame.sz);
1236  }
1237  coded_frame_add(&ctx->coded_frame_list, cx_frame);
1238  }
1239  break;
1240  case VPX_CODEC_STATS_PKT: {
1241  struct vpx_fixed_buf *stats = &ctx->twopass_stats;
1242  int err;
1243  if ((err = av_reallocp(&stats->buf,
1244  stats->sz +
1245  pkt->data.twopass_stats.sz)) < 0) {
1246  stats->sz = 0;
1247  av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
1248  return err;
1249  }
1250  memcpy((uint8_t*)stats->buf + stats->sz,
1251  pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
1252  stats->sz += pkt->data.twopass_stats.sz;
1253  break;
1254  }
1255  case VPX_CODEC_PSNR_PKT:
1256  av_assert0(!ctx->have_sse);
1257  ctx->sse[0] = pkt->data.psnr.sse[0];
1258  ctx->sse[1] = pkt->data.psnr.sse[1];
1259  ctx->sse[2] = pkt->data.psnr.sse[2];
1260  ctx->sse[3] = pkt->data.psnr.sse[3];
1261  ctx->have_sse = 1;
1262  break;
1263  case VPX_CODEC_CUSTOM_PKT:
1264  //ignore unsupported/unrecognized packet types
1265  break;
1266  }
1267  }
1268 
1269  return size;
1270 }
1271 
1272 static int set_roi_map(AVCodecContext *avctx, const AVFrameSideData *sd, int frame_width, int frame_height,
1273  vpx_roi_map_t *roi_map, int block_size, int segment_cnt)
1274 {
1275  /**
1276  * range of vpx_roi_map_t.delta_q[i] is [-63, 63]
1277  */
1278 #define MAX_DELTA_Q 63
1279 
1280  const AVRegionOfInterest *roi = NULL;
1281  int nb_rois;
1282  uint32_t self_size;
1283  int segment_id;
1284 
1285  /* record the mapping from delta_q to "segment id + 1" in segment_mapping[].
1286  * the range of delta_q is [-MAX_DELTA_Q, MAX_DELTA_Q],
1287  * and its corresponding array index is [0, 2 * MAX_DELTA_Q],
1288  * and so the length of the mapping array is 2 * MAX_DELTA_Q + 1.
1289  * "segment id + 1", so we can say there's no mapping if the value of array element is zero.
1290  */
1291  int segment_mapping[2 * MAX_DELTA_Q + 1] = { 0 };
1292 
1293  memset(roi_map, 0, sizeof(*roi_map));
1294 
1295  /* segment id 0 in roi_map is reserved for the areas not covered by AVRegionOfInterest.
1296  * segment id 0 in roi_map is also for the areas with AVRegionOfInterest.qoffset near 0.
1297  * (delta_q of segment id 0 is 0).
1298  */
1299  segment_mapping[MAX_DELTA_Q] = 1;
1300  segment_id = 1;
1301 
1302  roi = (const AVRegionOfInterest*)sd->data;
1303  self_size = roi->self_size;
1304  if (!self_size || sd->size % self_size) {
1305  av_log(avctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
1306  return AVERROR(EINVAL);
1307  }
1308  nb_rois = sd->size / self_size;
1309 
1310  /* This list must be iterated from zero because regions are
1311  * defined in order of decreasing importance. So discard less
1312  * important areas if they exceed the segment count.
1313  */
1314  for (int i = 0; i < nb_rois; i++) {
1315  int delta_q;
1316  int mapping_index;
1317 
1318  roi = (const AVRegionOfInterest*)(sd->data + self_size * i);
1319  if (!roi->qoffset.den) {
1320  av_log(avctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n");
1321  return AVERROR(EINVAL);
1322  }
1323 
1324  delta_q = (int)(roi->qoffset.num * 1.0f / roi->qoffset.den * MAX_DELTA_Q);
1325  delta_q = av_clip(delta_q, -MAX_DELTA_Q, MAX_DELTA_Q);
1326 
1327  mapping_index = delta_q + MAX_DELTA_Q;
1328  if (!segment_mapping[mapping_index]) {
1329  if (segment_id == segment_cnt) {
1330  av_log(avctx, AV_LOG_WARNING,
1331  "ROI only supports %d segments (and segment 0 is reserved for non-ROIs), skipping the left ones.\n",
1332  segment_cnt);
1333  break;
1334  }
1335 
1336  segment_mapping[mapping_index] = segment_id + 1;
1337  roi_map->delta_q[segment_id] = delta_q;
1338  segment_id++;
1339  }
1340  }
1341 
1342  roi_map->rows = (frame_height + block_size - 1) / block_size;
1343  roi_map->cols = (frame_width + block_size - 1) / block_size;
1344  roi_map->roi_map = av_mallocz_array(roi_map->rows * roi_map->cols, sizeof(*roi_map->roi_map));
1345  if (!roi_map->roi_map) {
1346  av_log(avctx, AV_LOG_ERROR, "roi_map alloc failed.\n");
1347  return AVERROR(ENOMEM);
1348  }
1349 
1350  /* This list must be iterated in reverse, so for the case that
1351  * two regions are overlapping, the more important area takes effect.
1352  */
1353  for (int i = nb_rois - 1; i >= 0; i--) {
1354  int delta_q;
1355  int mapping_value;
1356  int starty, endy, startx, endx;
1357 
1358  roi = (const AVRegionOfInterest*)(sd->data + self_size * i);
1359 
1360  starty = av_clip(roi->top / block_size, 0, roi_map->rows);
1361  endy = av_clip((roi->bottom + block_size - 1) / block_size, 0, roi_map->rows);
1362  startx = av_clip(roi->left / block_size, 0, roi_map->cols);
1363  endx = av_clip((roi->right + block_size - 1) / block_size, 0, roi_map->cols);
1364 
1365  delta_q = (int)(roi->qoffset.num * 1.0f / roi->qoffset.den * MAX_DELTA_Q);
1366  delta_q = av_clip(delta_q, -MAX_DELTA_Q, MAX_DELTA_Q);
1367 
1368  mapping_value = segment_mapping[delta_q + MAX_DELTA_Q];
1369  if (mapping_value) {
1370  for (int y = starty; y < endy; y++)
1371  for (int x = startx; x < endx; x++)
1372  roi_map->roi_map[x + y * roi_map->cols] = mapping_value - 1;
1373  }
1374  }
1375 
1376  return 0;
1377 }
1378 
1379 static int vp9_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
1380 {
1381  VPxContext *ctx = avctx->priv_data;
1382 
1383 #ifdef VPX_CTRL_VP9E_SET_ROI_MAP
1384  int version = vpx_codec_version();
1385  int major = VPX_VERSION_MAJOR(version);
1386  int minor = VPX_VERSION_MINOR(version);
1387  int patch = VPX_VERSION_PATCH(version);
1388 
1389  if (major > 1 || (major == 1 && minor > 8) || (major == 1 && minor == 8 && patch >= 1)) {
1390  vpx_roi_map_t roi_map;
1391  const int segment_cnt = 8;
1392  const int block_size = 8;
1393  int ret;
1394 
1395  if (ctx->aq_mode > 0 || ctx->cpu_used < 5 || ctx->deadline != VPX_DL_REALTIME) {
1396  if (!ctx->roi_warned) {
1397  ctx->roi_warned = 1;
1398  av_log(avctx, AV_LOG_WARNING, "ROI is only enabled when aq_mode is 0, cpu_used >= 5 "
1399  "and deadline is REALTIME, so skipping ROI.\n");
1400  return AVERROR(EINVAL);
1401  }
1402  }
1403 
1404  ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
1405  if (ret) {
1406  log_encoder_error(avctx, "Failed to set_roi_map.\n");
1407  return ret;
1408  }
1409 
1410  memset(roi_map.ref_frame, -1, sizeof(roi_map.ref_frame));
1411 
1412  if (vpx_codec_control(&ctx->encoder, VP9E_SET_ROI_MAP, &roi_map)) {
1413  log_encoder_error(avctx, "Failed to set VP9E_SET_ROI_MAP codec control.\n");
1414  ret = AVERROR_INVALIDDATA;
1415  }
1416  av_freep(&roi_map.roi_map);
1417  return ret;
1418  }
1419 #endif
1420 
1421  if (!ctx->roi_warned) {
1422  ctx->roi_warned = 1;
1423  av_log(avctx, AV_LOG_WARNING, "ROI is not supported, please upgrade libvpx to version >= 1.8.1. "
1424  "You may need to rebuild ffmpeg.\n");
1425  }
1426  return 0;
1427 }
1428 
1429 static int vp8_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
1430 {
1431  vpx_roi_map_t roi_map;
1432  const int segment_cnt = 4;
1433  const int block_size = 16;
1434  VPxContext *ctx = avctx->priv_data;
1435 
1436  int ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
1437  if (ret) {
1438  log_encoder_error(avctx, "Failed to set_roi_map.\n");
1439  return ret;
1440  }
1441 
1442  if (vpx_codec_control(&ctx->encoder, VP8E_SET_ROI_MAP, &roi_map)) {
1443  log_encoder_error(avctx, "Failed to set VP8E_SET_ROI_MAP codec control.\n");
1444  ret = AVERROR_INVALIDDATA;
1445  }
1446 
1447  av_freep(&roi_map.roi_map);
1448  return ret;
1449 }
1450 
1451 static int realloc_alpha_uv(AVCodecContext *avctx, int width, int height)
1452 {
1453  VPxContext *ctx = avctx->priv_data;
1454  struct vpx_image *rawimg_alpha = &ctx->rawimg_alpha;
1455  unsigned char **planes = rawimg_alpha->planes;
1456  int *stride = rawimg_alpha->stride;
1457 
1458  if (!planes[VPX_PLANE_U] ||
1459  !planes[VPX_PLANE_V] ||
1460  width != (int)rawimg_alpha->d_w ||
1461  height != (int)rawimg_alpha->d_h) {
1462  av_freep(&planes[VPX_PLANE_U]);
1463  av_freep(&planes[VPX_PLANE_V]);
1464 
1465  vpx_img_wrap(rawimg_alpha, VPX_IMG_FMT_I420, width, height, 1,
1466  (unsigned char*)1);
1467  planes[VPX_PLANE_U] = av_malloc_array(stride[VPX_PLANE_U], height);
1468  planes[VPX_PLANE_V] = av_malloc_array(stride[VPX_PLANE_V], height);
1469  if (!planes[VPX_PLANE_U] || !planes[VPX_PLANE_V])
1470  return AVERROR(ENOMEM);
1471 
1472  memset(planes[VPX_PLANE_U], 0x80, stride[VPX_PLANE_U] * height);
1473  memset(planes[VPX_PLANE_V], 0x80, stride[VPX_PLANE_V] * height);
1474  }
1475 
1476  return 0;
1477 }
1478 
1480  const AVFrame *frame, int *got_packet)
1481 {
1482  VPxContext *ctx = avctx->priv_data;
1483  struct vpx_image *rawimg = NULL;
1484  struct vpx_image *rawimg_alpha = NULL;
1485  int64_t timestamp = 0;
1486  int res, coded_size;
1487  vpx_enc_frame_flags_t flags = 0;
1488  const struct vpx_codec_enc_cfg *enccfg = ctx->encoder.config.enc;
1489  vpx_svc_layer_id_t layer_id;
1490  int layer_id_valid = 0;
1491 
1492  if (frame) {
1494  rawimg = &ctx->rawimg;
1495  rawimg->planes[VPX_PLANE_Y] = frame->data[0];
1496  rawimg->planes[VPX_PLANE_U] = frame->data[1];
1497  rawimg->planes[VPX_PLANE_V] = frame->data[2];
1498  rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
1499  rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
1500  rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
1501  if (ctx->is_alpha) {
1502  rawimg_alpha = &ctx->rawimg_alpha;
1503  res = realloc_alpha_uv(avctx, frame->width, frame->height);
1504  if (res < 0)
1505  return res;
1506  rawimg_alpha->planes[VPX_PLANE_Y] = frame->data[3];
1507  rawimg_alpha->stride[VPX_PLANE_Y] = frame->linesize[3];
1508  }
1509  timestamp = frame->pts;
1510 #if VPX_IMAGE_ABI_VERSION >= 4
1511  switch (frame->color_range) {
1512  case AVCOL_RANGE_MPEG:
1513  rawimg->range = VPX_CR_STUDIO_RANGE;
1514  break;
1515  case AVCOL_RANGE_JPEG:
1516  rawimg->range = VPX_CR_FULL_RANGE;
1517  break;
1518  }
1519 #endif
1520  if (frame->pict_type == AV_PICTURE_TYPE_I)
1521  flags |= VPX_EFLAG_FORCE_KF;
1522  if (frame->metadata) {
1523  AVDictionaryEntry* en = av_dict_get(frame->metadata, "vp8-flags", NULL, 0);
1524  if (en) {
1525  flags |= strtoul(en->value, NULL, 10);
1526  }
1527 
1528  memset(&layer_id, 0, sizeof(layer_id));
1529 
1530  en = av_dict_get(frame->metadata, "temporal_id", NULL, 0);
1531  if (en) {
1532  layer_id.temporal_layer_id = strtoul(en->value, NULL, 10);
1533 #ifdef VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT
1534  layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id;
1535 #endif
1536  layer_id_valid = 1;
1537  }
1538  }
1539 
1540  if (sd) {
1541  if (avctx->codec_id == AV_CODEC_ID_VP8) {
1542  vp8_encode_set_roi(avctx, frame->width, frame->height, sd);
1543  } else {
1544  vp9_encode_set_roi(avctx, frame->width, frame->height, sd);
1545  }
1546  }
1547  }
1548 
1549  // this is for encoding with preset temporal layering patterns defined in
1550  // set_temporal_layer_pattern function.
1551  if (enccfg->ts_number_layers > 1 && ctx->ts_layer_flags) {
1552  if (flags & VPX_EFLAG_FORCE_KF) {
1553  // keyframe, reset temporal layering.
1554  ctx->current_temporal_idx = 0;
1555  flags = VPX_EFLAG_FORCE_KF;
1556  } else {
1557  flags = 0;
1558  }
1559 
1560  /* get the flags from the temporal layer configuration. */
1561  flags |= ctx->ts_layer_flags[ctx->current_temporal_idx];
1562 
1563  memset(&layer_id, 0, sizeof(layer_id));
1564 #if VPX_ENCODER_ABI_VERSION >= 12
1565  layer_id.spatial_layer_id = 0;
1566 #endif
1567  layer_id.temporal_layer_id = enccfg->ts_layer_id[ctx->current_temporal_idx];
1568 #ifdef VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT
1569  layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id;
1570 #endif
1571  layer_id_valid = 1;
1572  }
1573 
1574  if (layer_id_valid) {
1575  if (avctx->codec_id == AV_CODEC_ID_VP8) {
1576  codecctl_int(avctx, VP8E_SET_TEMPORAL_LAYER_ID, layer_id.temporal_layer_id);
1577  }
1578 #if CONFIG_LIBVPX_VP9_ENCODER && VPX_ENCODER_ABI_VERSION >= 12
1579  else if (avctx->codec_id == AV_CODEC_ID_VP9) {
1580  codecctl_intp(avctx, VP9E_SET_SVC_LAYER_ID, (int *)&layer_id);
1581  }
1582 #endif
1583  }
1584 
1585  res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
1586  avctx->ticks_per_frame, flags, ctx->deadline);
1587  if (res != VPX_CODEC_OK) {
1588  log_encoder_error(avctx, "Error encoding frame");
1589  return AVERROR_INVALIDDATA;
1590  }
1591 
1592  if (ctx->is_alpha) {
1593  res = vpx_codec_encode(&ctx->encoder_alpha, rawimg_alpha, timestamp,
1594  avctx->ticks_per_frame, flags, ctx->deadline);
1595  if (res != VPX_CODEC_OK) {
1596  log_encoder_error(avctx, "Error encoding alpha frame");
1597  return AVERROR_INVALIDDATA;
1598  }
1599  }
1600 
1601  coded_size = queue_frames(avctx, pkt);
1602 
1603  if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
1604  unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
1605 
1606  avctx->stats_out = av_malloc(b64_size);
1607  if (!avctx->stats_out) {
1608  av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
1609  b64_size);
1610  return AVERROR(ENOMEM);
1611  }
1612  av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
1613  ctx->twopass_stats.sz);
1614  } else if (enccfg->ts_number_layers > 1 && ctx->ts_layer_flags) {
1615  ctx->current_temporal_idx = (ctx->current_temporal_idx + 1) % enccfg->ts_periodicity;
1616  }
1617 
1618  *got_packet = !!coded_size;
1619  return 0;
1620 }
1621 
1622 #define OFFSET(x) offsetof(VPxContext, x)
1623 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1624 
1625 #define COMMON_OPTIONS \
1626  { "lag-in-frames", "Number of frames to look ahead for " \
1627  "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
1628  { "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
1629  { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
1630  { "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"}, \
1631  { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" }, \
1632  { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" }, \
1633  { "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" }, \
1634  { "tune", "Tune the encoding to a specific scenario", OFFSET(tune), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "tune"}, \
1635  { "psnr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VP8_TUNE_PSNR}, 0, 0, VE, "tune"}, \
1636  { "ssim", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VP8_TUNE_SSIM}, 0, 0, VE, "tune"}, \
1637  { "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
1638  { "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"}, \
1639  { "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"}, \
1640  { "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"}, \
1641  { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, \
1642  { "max-intra-rate", "Maximum I-frame bitrate (pct) 0=unlimited", OFFSET(max_intra_rate), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
1643  { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, \
1644  { "partitions", "The frame partitions are independently decodable " \
1645  "by the bool decoder, meaning that partitions can be decoded even " \
1646  "though earlier partitions have been lost. Note that intra prediction" \
1647  " is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"}, \
1648  { "crf", "Select the quality for constant quality mode", offsetof(VPxContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE }, \
1649  { "static-thresh", "A change threshold on blocks below which they will be skipped by the encoder", OFFSET(static_thresh), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, \
1650  { "drop-threshold", "Frame drop threshold", offsetof(VPxContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE }, \
1651  { "noise-sensitivity", "Noise sensitivity", OFFSET(noise_sensitivity), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE}, \
1652  { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, VE }, \
1653  { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1000, VE }, \
1654  { "ts-parameters", "Temporal scaling configuration using a :-separated list of key=value parameters", OFFSET(vpx_ts_parameters), AV_OPT_TYPE_DICT, {.str=NULL}, 0, 0, VE}, \
1655 
1656 #define LEGACY_OPTIONS \
1657  {"speed", "", offsetof(VPxContext, cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
1658  {"quality", "", offsetof(VPxContext, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
1659  {"vp8flags", "", offsetof(VPxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, UINT_MAX, VE, "flags"}, \
1660  {"error_resilient", "enable error resilience", 0, AV_OPT_TYPE_CONST, {.i64 = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"}, \
1661  {"altref", "enable use of alternate reference frames (VP8/2-pass only)", 0, AV_OPT_TYPE_CONST, {.i64 = VP8F_AUTO_ALT_REF}, INT_MIN, INT_MAX, VE, "flags"}, \
1662  {"arnr_max_frames", "altref noise reduction max frame count", offsetof(VPxContext, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE}, \
1663  {"arnr_strength", "altref noise reduction filter strength", offsetof(VPxContext, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE}, \
1664  {"arnr_type", "altref noise reduction filter type", offsetof(VPxContext, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE}, \
1665  {"rc_lookahead", "Number of frames to look ahead for alternate reference frame selection", offsetof(VPxContext, lag_in_frames), AV_OPT_TYPE_INT, {.i64 = 25}, 0, 25, VE}, \
1666  {"sharpness", "Increase sharpness at the expense of lower PSNR", offsetof(VPxContext, sharpness), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, VE},
1667 
1668 #if CONFIG_LIBVPX_VP8_ENCODER
1669 static const AVOption vp8_options[] = {
1671  { "auto-alt-ref", "Enable use of alternate reference "
1672  "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
1673  { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE},
1675  { NULL }
1676 };
1677 #endif
1678 
1679 #if CONFIG_LIBVPX_VP9_ENCODER
1680 static const AVOption vp9_options[] = {
1682  { "auto-alt-ref", "Enable use of alternate reference "
1683  "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1684  { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -8, 8, VE},
1685  { "lossless", "Lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
1686  { "tile-columns", "Number of tile columns to use, log2", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1687  { "tile-rows", "Number of tile rows to use, log2", OFFSET(tile_rows), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
1688  { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_BOOL,{.i64 = -1}, -1, 1, VE},
1689 #if VPX_ENCODER_ABI_VERSION >= 12
1690  { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 4, VE, "aq_mode"},
1691 #else
1692  { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 3, VE, "aq_mode"},
1693 #endif
1694  { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode" },
1695  { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode" },
1696  { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode" },
1697  { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode" },
1698 #if VPX_ENCODER_ABI_VERSION >= 12
1699  { "equator360", "360 video Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 4}, 0, 0, VE, "aq_mode" },
1700  {"level", "Specify level", OFFSET(level), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 6.2, VE},
1701 #endif
1702 #ifdef VPX_CTRL_VP9E_SET_ROW_MT
1703  {"row-mt", "Row based multi-threading", OFFSET(row_mt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1704 #endif
1705 #ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT
1706 #if VPX_ENCODER_ABI_VERSION >= 14
1707  { "tune-content", "Tune content type", OFFSET(tune_content), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE, "tune_content" },
1708 #else
1709  { "tune-content", "Tune content type", OFFSET(tune_content), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE, "tune_content" },
1710 #endif
1711  { "default", "Regular video content", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "tune_content" },
1712  { "screen", "Screen capture content", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "tune_content" },
1713 #if VPX_ENCODER_ABI_VERSION >= 14
1714  { "film", "Film content; improves grain retention", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "tune_content" },
1715 #endif
1716 #endif
1717 #if VPX_ENCODER_ABI_VERSION >= 14
1718  { "corpus-complexity", "corpus vbr complexity midpoint", OFFSET(corpus_complexity), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 10000, VE },
1719 #endif
1720 #ifdef VPX_CTRL_VP9E_SET_TPL
1721  { "enable-tpl", "Enable temporal dependency model", OFFSET(tpl_model), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE },
1722 #endif
1724  { NULL }
1725 };
1726 #endif
1727 
1728 #undef COMMON_OPTIONS
1729 #undef LEGACY_OPTIONS
1730 
1731 static const AVCodecDefault defaults[] = {
1732  { "b", "0" },
1733  { "qmin", "-1" },
1734  { "qmax", "-1" },
1735  { "g", "-1" },
1736  { "keyint_min", "-1" },
1737  { NULL },
1738 };
1739 
1740 #if CONFIG_LIBVPX_VP8_ENCODER
1741 static av_cold int vp8_init(AVCodecContext *avctx)
1742 {
1743  return vpx_init(avctx, vpx_codec_vp8_cx());
1744 }
1745 
1746 static const AVClass class_vp8 = {
1747  .class_name = "libvpx-vp8 encoder",
1748  .item_name = av_default_item_name,
1749  .option = vp8_options,
1750  .version = LIBAVUTIL_VERSION_INT,
1751 };
1752 
1754  .name = "libvpx",
1755  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
1756  .type = AVMEDIA_TYPE_VIDEO,
1757  .id = AV_CODEC_ID_VP8,
1758  .priv_data_size = sizeof(VPxContext),
1759  .init = vp8_init,
1760  .encode2 = vpx_encode,
1761  .close = vpx_free,
1764  .priv_class = &class_vp8,
1765  .defaults = defaults,
1766  .wrapper_name = "libvpx",
1767 };
1768 #endif /* CONFIG_LIBVPX_VP8_ENCODER */
1769 
1770 #if CONFIG_LIBVPX_VP9_ENCODER
1771 static av_cold int vp9_init(AVCodecContext *avctx)
1772 {
1773  return vpx_init(avctx, vpx_codec_vp9_cx());
1774 }
1775 
1776 static const AVClass class_vp9 = {
1777  .class_name = "libvpx-vp9 encoder",
1778  .item_name = av_default_item_name,
1779  .option = vp9_options,
1780  .version = LIBAVUTIL_VERSION_INT,
1781 };
1782 
1784  .name = "libvpx-vp9",
1785  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
1786  .type = AVMEDIA_TYPE_VIDEO,
1787  .id = AV_CODEC_ID_VP9,
1788  .priv_data_size = sizeof(VPxContext),
1789  .init = vp9_init,
1790  .encode2 = vpx_encode,
1791  .close = vpx_free,
1794  .priv_class = &class_vp9,
1795  .defaults = defaults,
1796  .init_static_data = ff_vp9_init_static,
1797  .wrapper_name = "libvpx",
1798 };
1799 #endif /* CONFIG_LIBVPX_VP9_ENCODER */
uint8_t is_alpha
Definition: libvpxenc.c:71
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:511
struct vpx_image rawimg
Definition: libvpxenc.c:68
int arnr_max_frames
Definition: libvpxenc.c:90
#define NULL
Definition: coverity.c:32
static void set_vpx_defaults(AVCodecContext *avctx, struct vpx_codec_enc_cfg *enccfg)
Called when the bitrate is not set.
Definition: libvpxenc.c:717
int row_mt
Definition: libvpxenc.c:118
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int top
Distance in pixels from the top edge of the frame to the top and bottom edges and from the left edge ...
Definition: frame.h:240
version
Definition: libkvazaar.c:292
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:399
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
static av_cold int vpx_free(AVCodecContext *avctx)
Definition: libvpxenc.c:357
AVOption.
Definition: opt.h:246
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:728
int av_cpu_count(void)
Definition: cpu.c:267
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition: avcodec.h:1709
#define MAX_DELTA_Q
#define OFFSET(x)
Definition: libvpxenc.c:1622
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int rc_overshoot_pct
Definition: libvpxenc.c:102
const char * desc
Definition: nvenc.c:79
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
struct FrameListData * coded_frame_list
Definition: libvpxenc.c:77
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:454
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:515
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:1432
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1161
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
int num
Numerator.
Definition: rational.h:59
int size
Definition: packet.h:356
void * buf
compressed data buffer
Definition: libaomenc.c:48
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:413
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int aq_mode
Definition: libvpxenc.c:113
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:516
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
size_t sz
length of compressed data
Definition: libaomenc.c:49
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:401
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1553
static void cx_pktcpy(struct FrameListData *dst, const struct vpx_codec_cx_pkt *src, const struct vpx_codec_cx_pkt *src_alpha, VPxContext *ctx)
Definition: libvpxenc.c:1052
const char * key
int frame_parallel
Definition: libvpxenc.c:112
int static_thresh
Definition: libvpxenc.c:99
static AVPacket pkt
static void error(const char *err)
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: codec.h:118
int profile
profile
Definition: avcodec.h:1859
int stride
Definition: mace.c:144
AVCodec.
Definition: codec.h:190
struct vpx_fixed_buf twopass_stats
Definition: libvpxenc.c:72
int error_resilient
Definition: libvpxenc.c:97
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:510
int tile_rows
Definition: libvpxenc.c:111
uint64_t frame_number
Definition: libvpxenc.c:76
int min_bitrate
Minimum bitrate of the stream, in bits per second.
Definition: avcodec.h:463
functionally identical to above
Definition: pixfmt.h:517
AVRational qoffset
Quantisation offset.
Definition: frame.h:267
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:649
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:739
#define VP8F_AUTO_ALT_REF
Enable automatic alternate reference frame generation.
Definition: libvpxenc.c:86
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:75
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
struct FrameListData * next
Definition: libaomenc.c:58
uint8_t
#define av_cold
Definition: attributes.h:88
static av_cold int codecctl_int(AVCodecContext *avctx, enum vp8e_enc_control_id id, int val)
Definition: libvpxenc.c:313
#define av_malloc(s)
int64_t pts
time stamp to show frame (in timebase units)
Definition: libaomenc.c:50
AVOptions.
AVCodec ff_libvpx_vp8_encoder
static void coded_frame_add(void *list, struct FrameListData *cx_frame)
Definition: libvpxenc.c:284
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:393
static AVFrame * frame
Structure to hold side data for an AVFrame.
Definition: frame.h:206
int auto_alt_ref
Definition: libvpxenc.c:88
#define height
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
uint8_t * data
Definition: packet.h:355
#define CONFIG_LIBVPX_VP9_ENCODER
Definition: config.h:1447
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVDictionary * metadata
metadata.
Definition: frame.h:586
int vpx_cs
Definition: libvpxenc.c:116
uint64_t sse[4]
Definition: libvpxenc.c:74
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:481
uint32_t self_size
Must be set to the size of this data structure (that is, sizeof(AVRegionOfInterest)).
Definition: frame.h:230
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:402
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1545
attribute_deprecated uint64_t error[AV_NUM_DATA_POINTERS]
Definition: frame.h:435
attribute_deprecated int frame_skip_threshold
Definition: avcodec.h:1455
#define av_log(a,...)
int noise_sensitivity
Definition: libvpxenc.c:115
#define LEGACY_OPTIONS
Definition: libvpxenc.c:1656
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:388
#define src
Definition: vp8dsp.c:254
static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libvpxenc.c:1479
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
int width
Definition: frame.h:358
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int arnr_type
Definition: libvpxenc.c:92
int tune
Definition: libvpxenc.c:94
static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt)
Store coded frame information in format suitable for return from encode2().
Definition: libvpxenc.c:1094
#define AVERROR(e)
Definition: error.h:43
#define CONFIG_LIBVPX_VP8_ENCODER
Definition: config.h:1446
#define COMMON_OPTIONS
Definition: libvpxenc.c:1625
int qmax
maximum quantizer
Definition: avcodec.h:1375
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:544
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int roi_warned
If the driver does not support ROI then warn the first time we encounter a frame with ROI side data...
Definition: libvpxenc.c:126
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: codec.h:197
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:400
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:140
static void set_color_range(AVCodecContext *avctx)
Definition: libaomenc.c:336
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1389
static int vp8_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
Definition: libvpxenc.c:1429
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:1411
common internal API header
float level
Definition: libvpxenc.c:117
int lossless
Definition: libvpxenc.c:109
static const struct @315 planes[]
static av_cold void dump_enc_cfg(AVCodecContext *avctx, const struct vpx_codec_enc_cfg *cfg)
Definition: libvpxenc.c:186
int tpl_model
Definition: libvpxenc.c:121
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
struct vpx_image rawimg_alpha
Definition: libvpxenc.c:70
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition: base64.h:66
#define FFMIN(a, b)
Definition: common.h:96
uint64_t sse[4]
Definition: libaomenc.c:55
#define width
int width
picture width / height.
Definition: avcodec.h:699
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1860
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:520
attribute_deprecated int noise_reduction
Definition: avcodec.h:1044
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:312
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:296
static void vp8_ts_parse_int_array(int *dest, char *value, size_t value_len, int max_entries)
Definition: libvpxenc.c:384
static void stats(AVPacket *const *in, int n_in, unsigned *_max, unsigned *_sum)
int deadline
Definition: libvpxenc.c:73
static int vp9_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
Definition: libvpxenc.c:1379
Structure describing a single Region Of Interest.
Definition: frame.h:225
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:658
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
Definition: libvpxenc.c:175
Round to nearest and halfway cases away from zero.
Definition: mathematics.h:84
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
int tile_rows
Definition: h265_levels.c:217
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:1785
#define av_log2
Definition: intmath.h:83
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:535
AVCodec ff_libvpx_vp9_encoder
#define VP8F_ERROR_RESILIENT
Enable measures appropriate for streaming over lossy links.
Definition: libvpxenc.c:85
int cpu_used
Definition: libvpxenc.c:79
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
Definition: libvpxenc.c:730
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:448
static const AVCodecDefault defaults[]
Definition: libvpxenc.c:1731
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int max_intra_rate
Definition: libvpxenc.c:100
#define delta_q(name)
Definition: cbs_av1.c:714
#define VE
Definition: libvpxenc.c:1623
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:161
int tune_content
Definition: libvpxenc.c:119
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:536
av_cold void ff_vp9_init_static(AVCodec *codec)
Definition: libvpx.c:68
int lag_in_frames
Definition: libvpxenc.c:96
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
static av_cold int vp9_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
Definition: rtpdec_vp9.c:34
main external API structure.
Definition: avcodec.h:526
static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
Queue multiple output frames from the encoder, returning the front-most.
Definition: libvpxenc.c:1171
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
long long int64_t
Definition: coverity.c:34
AVDictionary * vpx_ts_parameters
Definition: libvpxenc.c:104
static void set_vp8_defaults(AVCodecContext *avctx, struct vpx_codec_enc_cfg *enccfg)
Set the target bitrate to VPX library default.
Definition: libvpxenc.c:673
int qmin
minimum quantizer
Definition: avcodec.h:1368
static void set_temporal_layer_pattern(int layering_mode, vpx_codec_enc_cfg_t *cfg, int *layer_flags, int *flag_periodicity)
Definition: libvpxenc.c:396
int current_temporal_idx
Definition: libvpxenc.c:106
uint8_t * data
Definition: frame.h:208
int * ts_layer_flags
Definition: libvpxenc.c:105
Data found in BlockAdditional element of matroska container.
Definition: packet.h:191
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:397
double value
Definition: eval.c:98
Describe the class of an AVClass context structure.
Definition: log.h:67
static int vpx_ts_param_parse(VPxContext *ctx, struct vpx_codec_enc_cfg *enccfg, char *key, char *value, enum AVCodecID codec_id)
Definition: libvpxenc.c:500
static const AVProfile profiles[]
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1154
static int set_pix_fmt(AVCodecContext *avctx, struct aom_image *img)
Definition: libaomdec.c:86
Regions Of Interest, the data is an array of AVRegionOfInterest type, the number of array element is ...
Definition: frame.h:181
uint32_t flags
flags for this frame
Definition: libaomenc.c:54
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:403
#define snprintf
Definition: snprintf.h:34
static av_cold void free_coded_frame(struct FrameListData *cx_frame)
Definition: libvpxenc.c:294
uint64_t frame_number
Definition: libaomenc.c:57
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:1360
int sharpness
Definition: libvpxenc.c:80
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
size_t sz_alpha
Definition: libvpxenc.c:53
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:414
int have_sse
true if we have pending sse[]
Definition: libaomenc.c:56
#define SIZE_SPECIFIER
Definition: internal.h:264
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:404
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
uint8_t level
Definition: svq3.c:210
static int set_roi_map(AVCodecContext *avctx, const AVFrameSideData *sd, int frame_width, int frame_height, vpx_roi_map_t *roi_map, int block_size, int segment_cnt)
Definition: libvpxenc.c:1272
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:534
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
int flags
VP8 specific flags, see VP8F_* below.
Definition: libvpxenc.c:84
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:721
static const char *const ctlidstr[]
String mappings for enum vp8e_enc_control_id.
Definition: libvpxenc.c:130
struct vpx_codec_ctx encoder_alpha
Definition: libvpxenc.c:69
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
Definition: rtpdec_vp8.c:263
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
struct vpx_codec_ctx encoder
Definition: libvpxenc.c:67
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1776
char * key
Definition: dict.h:86
int den
Denominator.
Definition: rational.h:60
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: utils.c:2046
static av_cold void free_frame_list(struct FrameListData *list)
Definition: libvpxenc.c:302
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:300
void * buf_alpha
Definition: libvpxenc.c:52
int slices
Number of slices.
Definition: avcodec.h:1177
void * priv_data
Definition: avcodec.h:553
#define av_free(p)
char * value
Definition: dict.h:87
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
Definition: libaomenc.c:47
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:472
int arnr_strength
Definition: libvpxenc.c:91
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
unsigned long duration
duration to show frame (in timebase units)
Definition: libaomenc.c:52
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition: base64.c:79
#define lrint
Definition: tablegen.h:53
int drop_threshold
Definition: libvpxenc.c:114
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:354
int have_sse
true if we have pending sse[]
Definition: libvpxenc.c:75
int height
Definition: frame.h:358
#define av_freep(p)
int rc_undershoot_pct
Definition: libvpxenc.c:101
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
static int realloc_alpha_uv(AVCodecContext *avctx, int width, int height)
Definition: libvpxenc.c:1451
#define av_malloc_array(a, b)
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:332
const AVProfile ff_vp9_profiles[]
Definition: profiles.c:133
int corpus_complexity
Definition: libvpxenc.c:120
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:332
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:348
Predicted.
Definition: avutil.h:275
#define av_unused
Definition: attributes.h:131
int tile_columns
Definition: libvpxenc.c:110
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1404
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
int keyint_min
minimum GOP size
Definition: avcodec.h:1107