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