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