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