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