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