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