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