FFmpeg  4.2.3
libaomenc.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  * AV1 encoder support via libaom
24  */
25 
26 #define AOM_DISABLE_CTRL_TYPECHECKS 1
27 #include <aom/aom_encoder.h>
28 #include <aom/aomcx.h>
29 
30 #include "libavutil/avassert.h"
31 #include "libavutil/base64.h"
32 #include "libavutil/common.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 
37 #include "av1.h"
38 #include "avcodec.h"
39 #include "internal.h"
40 #include "profiles.h"
41 
42 /*
43  * Portion of struct aom_codec_cx_pkt from aom_encoder.h.
44  * One encoded frame returned from the library.
45  */
46 struct FrameListData {
47  void *buf; /**< compressed data buffer */
48  size_t sz; /**< length of compressed data */
49  int64_t pts; /**< time stamp to show frame
50  (in timebase units) */
51  unsigned long duration; /**< duration to show frame
52  (in timebase units) */
53  uint32_t flags; /**< flags for this frame */
54  uint64_t sse[4];
55  int have_sse; /**< true if we have pending sse[] */
56  uint64_t frame_number;
58 };
59 
60 typedef struct AOMEncoderContext {
61  AVClass *class;
63  struct aom_codec_ctx encoder;
64  struct aom_image rawimg;
65  struct aom_fixed_buf twopass_stats;
67  int cpu_used;
71  int aq_mode;
74  int crf;
79  uint64_t sse[4];
80  int have_sse; /**< true if we have pending sse[] */
81  uint64_t frame_number;
88  int tile_cols_log2, tile_rows_log2;
89  aom_superblock_size_t superblock_size;
91  int row_mt;
95 } AOMContext;
96 
97 static const char *const ctlidstr[] = {
98  [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED",
99  [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL",
100  [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF",
101  [AOME_SET_ARNR_MAXFRAMES] = "AOME_SET_ARNR_MAXFRAMES",
102  [AOME_SET_ARNR_STRENGTH] = "AOME_SET_ARNR_STRENGTH",
103  [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD",
104  [AV1E_SET_COLOR_RANGE] = "AV1E_SET_COLOR_RANGE",
105  [AV1E_SET_COLOR_PRIMARIES] = "AV1E_SET_COLOR_PRIMARIES",
106  [AV1E_SET_MATRIX_COEFFICIENTS] = "AV1E_SET_MATRIX_COEFFICIENTS",
107  [AV1E_SET_TRANSFER_CHARACTERISTICS] = "AV1E_SET_TRANSFER_CHARACTERISTICS",
108  [AV1E_SET_AQ_MODE] = "AV1E_SET_AQ_MODE",
109  [AV1E_SET_FRAME_PARALLEL_DECODING] = "AV1E_SET_FRAME_PARALLEL_DECODING",
110  [AV1E_SET_SUPERBLOCK_SIZE] = "AV1E_SET_SUPERBLOCK_SIZE",
111  [AV1E_SET_TILE_COLUMNS] = "AV1E_SET_TILE_COLUMNS",
112  [AV1E_SET_TILE_ROWS] = "AV1E_SET_TILE_ROWS",
113 #ifdef AOM_CTRL_AV1E_SET_ROW_MT
114  [AV1E_SET_ROW_MT] = "AV1E_SET_ROW_MT",
115 #endif
116 #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL
117  [AV1E_SET_DENOISE_NOISE_LEVEL] = "AV1E_SET_DENOISE_NOISE_LEVEL",
118 #endif
119 #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE
120  [AV1E_SET_DENOISE_BLOCK_SIZE] = "AV1E_SET_DENOISE_BLOCK_SIZE",
121 #endif
122 #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES
123  [AV1E_SET_MAX_REFERENCE_FRAMES] = "AV1E_SET_MAX_REFERENCE_FRAMES",
124 #endif
125 #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION
126  [AV1E_SET_ENABLE_GLOBAL_MOTION] = "AV1E_SET_ENABLE_GLOBAL_MOTION",
127 #endif
128 #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC
129  [AV1E_SET_ENABLE_INTRABC] = "AV1E_SET_ENABLE_INTRABC",
130 #endif
131  [AV1E_SET_ENABLE_CDEF] = "AV1E_SET_ENABLE_CDEF",
132 };
133 
134 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
135 {
136  AOMContext *ctx = avctx->priv_data;
137  const char *error = aom_codec_error(&ctx->encoder);
138  const char *detail = aom_codec_error_detail(&ctx->encoder);
139 
140  av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
141  if (detail)
142  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
143 }
144 
146  const struct aom_codec_enc_cfg *cfg)
147 {
148  int width = -30;
149  int level = AV_LOG_DEBUG;
150 
151  av_log(avctx, level, "aom_codec_enc_cfg\n");
152  av_log(avctx, level, "generic settings\n"
153  " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
154  " %*s%u\n %*s%u\n"
155  " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
156  width, "g_usage:", cfg->g_usage,
157  width, "g_threads:", cfg->g_threads,
158  width, "g_profile:", cfg->g_profile,
159  width, "g_w:", cfg->g_w,
160  width, "g_h:", cfg->g_h,
161  width, "g_bit_depth:", cfg->g_bit_depth,
162  width, "g_input_bit_depth:", cfg->g_input_bit_depth,
163  width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
164  width, "g_error_resilient:", cfg->g_error_resilient,
165  width, "g_pass:", cfg->g_pass,
166  width, "g_lag_in_frames:", cfg->g_lag_in_frames);
167  av_log(avctx, level, "rate control settings\n"
168  " %*s%u\n %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n",
169  width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
170  width, "rc_end_usage:", cfg->rc_end_usage,
171  width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
172  width, "rc_target_bitrate:", cfg->rc_target_bitrate);
173  av_log(avctx, level, "quantizer settings\n"
174  " %*s%u\n %*s%u\n",
175  width, "rc_min_quantizer:", cfg->rc_min_quantizer,
176  width, "rc_max_quantizer:", cfg->rc_max_quantizer);
177  av_log(avctx, level, "bitrate tolerance\n"
178  " %*s%u\n %*s%u\n",
179  width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
180  width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
181  av_log(avctx, level, "decoder buffer model\n"
182  " %*s%u\n %*s%u\n %*s%u\n",
183  width, "rc_buf_sz:", cfg->rc_buf_sz,
184  width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
185  width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
186  av_log(avctx, level, "2 pass rate control settings\n"
187  " %*s%u\n %*s%u\n %*s%u\n",
188  width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
189  width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
190  width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
191  av_log(avctx, level, "keyframing settings\n"
192  " %*s%d\n %*s%u\n %*s%u\n",
193  width, "kf_mode:", cfg->kf_mode,
194  width, "kf_min_dist:", cfg->kf_min_dist,
195  width, "kf_max_dist:", cfg->kf_max_dist);
196  av_log(avctx, level, "tile settings\n"
197  " %*s%d\n %*s%d\n",
198  width, "tile_width_count:", cfg->tile_width_count,
199  width, "tile_height_count:", cfg->tile_height_count);
200  av_log(avctx, level, "\n");
201 }
202 
203 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
204 {
205  struct FrameListData **p = list;
206 
207  while (*p)
208  p = &(*p)->next;
209  *p = cx_frame;
210  cx_frame->next = NULL;
211 }
212 
213 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
214 {
215  av_freep(&cx_frame->buf);
216  av_freep(&cx_frame);
217 }
218 
219 static av_cold void free_frame_list(struct FrameListData *list)
220 {
221  struct FrameListData *p = list;
222 
223  while (p) {
224  list = list->next;
225  free_coded_frame(p);
226  p = list;
227  }
228 }
229 
231 #ifdef UENUM1BYTE
232  aome_enc_control_id id,
233 #else
234  enum aome_enc_control_id id,
235 #endif
236  int val)
237 {
238  AOMContext *ctx = avctx->priv_data;
239  char buf[80];
240  int width = -30;
241  int res;
242 
243  snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
244  av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
245 
246  res = aom_codec_control(&ctx->encoder, id, val);
247  if (res != AOM_CODEC_OK) {
248  snprintf(buf, sizeof(buf), "Failed to set %s codec control",
249  ctlidstr[id]);
250  log_encoder_error(avctx, buf);
251  return AVERROR(EINVAL);
252  }
253 
254  return 0;
255 }
256 
257 static av_cold int aom_free(AVCodecContext *avctx)
258 {
259  AOMContext *ctx = avctx->priv_data;
260 
261  aom_codec_destroy(&ctx->encoder);
262  av_freep(&ctx->twopass_stats.buf);
263  av_freep(&avctx->stats_out);
265  av_bsf_free(&ctx->bsf);
266  return 0;
267 }
268 
269 static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps,
270  struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags,
271  aom_img_fmt_t *img_fmt)
272 {
273  AOMContext av_unused *ctx = avctx->priv_data;
274  enccfg->g_bit_depth = enccfg->g_input_bit_depth = 8;
275  switch (avctx->pix_fmt) {
276  case AV_PIX_FMT_YUV420P:
277  enccfg->g_profile = FF_PROFILE_AV1_MAIN;
278  *img_fmt = AOM_IMG_FMT_I420;
279  return 0;
280  case AV_PIX_FMT_YUV422P:
281  enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
282  *img_fmt = AOM_IMG_FMT_I422;
283  return 0;
284  case AV_PIX_FMT_YUV444P:
285  enccfg->g_profile = FF_PROFILE_AV1_HIGH;
286  *img_fmt = AOM_IMG_FMT_I444;
287  return 0;
290  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
291  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
292  avctx->pix_fmt == AV_PIX_FMT_YUV420P10 ? 10 : 12;
293  enccfg->g_profile =
294  enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_MAIN : FF_PROFILE_AV1_PROFESSIONAL;
295  *img_fmt = AOM_IMG_FMT_I42016;
296  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
297  return 0;
298  }
299  break;
302  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
303  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
304  avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12;
305  enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
306  *img_fmt = AOM_IMG_FMT_I42216;
307  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
308  return 0;
309  }
310  break;
313  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
314  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
315  avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ? 10 : 12;
316  enccfg->g_profile =
317  enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_HIGH : FF_PROFILE_AV1_PROFESSIONAL;
318  *img_fmt = AOM_IMG_FMT_I44416;
319  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
320  return 0;
321  }
322  break;
323  default:
324  break;
325  }
326  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
327  return AVERROR_INVALIDDATA;
328 }
329 
330 static void set_color_range(AVCodecContext *avctx)
331 {
332  aom_color_range_t aom_cr;
333  switch (avctx->color_range) {
335  case AVCOL_RANGE_MPEG: aom_cr = AOM_CR_STUDIO_RANGE; break;
336  case AVCOL_RANGE_JPEG: aom_cr = AOM_CR_FULL_RANGE; break;
337  default:
338  av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n",
339  avctx->color_range);
340  return;
341  }
342 
343  codecctl_int(avctx, AV1E_SET_COLOR_RANGE, aom_cr);
344 }
345 
346 static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
347 {
348  int sb_dim = (dim + sb_size - 1) / sb_size;
349  int tile_dim = (sb_dim + (1 << tiles_log2) - 1) >> tiles_log2;
350  av_assert0(tile_dim > 0);
351  return (sb_dim + tile_dim - 1) / tile_dim;
352 }
353 
354 static int choose_tiling(AVCodecContext *avctx,
355  struct aom_codec_enc_cfg *enccfg)
356 {
357  AOMContext *ctx = avctx->priv_data;
358  int sb_128x128_possible, sb_size, sb_width, sb_height;
359  int uniform_rows, uniform_cols;
360  int uniform_64x64_possible, uniform_128x128_possible;
361  int tile_size, rounding, i;
362 
363  if (ctx->tile_cols_log2 >= 0)
364  ctx->tile_cols = 1 << ctx->tile_cols_log2;
365  if (ctx->tile_rows_log2 >= 0)
366  ctx->tile_rows = 1 << ctx->tile_rows_log2;
367 
368  if (ctx->tile_cols == 0) {
369  ctx->tile_cols = (avctx->width + AV1_MAX_TILE_WIDTH - 1) /
371  if (ctx->tile_cols > 1) {
372  av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
373  "columns to fill width.\n", ctx->tile_cols);
374  }
375  }
376  av_assert0(ctx->tile_cols > 0);
377  if (ctx->tile_rows == 0) {
378  int max_tile_width =
379  FFALIGN((FFALIGN(avctx->width, 128) +
380  ctx->tile_cols - 1) / ctx->tile_cols, 128);
381  ctx->tile_rows =
382  (max_tile_width * FFALIGN(avctx->height, 128) +
384  if (ctx->tile_rows > 1) {
385  av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
386  "rows to fill area.\n", ctx->tile_rows);
387  }
388  }
389  av_assert0(ctx->tile_rows > 0);
390 
391  if ((avctx->width + 63) / 64 < ctx->tile_cols ||
392  (avctx->height + 63) / 64 < ctx->tile_rows) {
393  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: frame not "
394  "large enough to fit specified tile arrangement.\n");
395  return AVERROR(EINVAL);
396  }
397  if (ctx->tile_cols > AV1_MAX_TILE_COLS ||
398  ctx->tile_rows > AV1_MAX_TILE_ROWS) {
399  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
400  "not allow more than %dx%d tiles.\n",
402  return AVERROR(EINVAL);
403  }
404  if (avctx->width / ctx->tile_cols > AV1_MAX_TILE_WIDTH) {
405  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
406  "not allow tiles of width greater than %d.\n",
408  return AVERROR(EINVAL);
409  }
410 
411  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC;
412 
413  if (ctx->tile_cols == 1 && ctx->tile_rows == 1) {
414  av_log(avctx, AV_LOG_DEBUG, "Using a single tile.\n");
415  return 0;
416  }
417 
418  sb_128x128_possible =
419  (avctx->width + 127) / 128 >= ctx->tile_cols &&
420  (avctx->height + 127) / 128 >= ctx->tile_rows;
421 
422  ctx->tile_cols_log2 = ctx->tile_cols == 1 ? 0 :
423  av_log2(ctx->tile_cols - 1) + 1;
424  ctx->tile_rows_log2 = ctx->tile_rows == 1 ? 0 :
425  av_log2(ctx->tile_rows - 1) + 1;
426 
427  uniform_cols = count_uniform_tiling(avctx->width,
428  64, ctx->tile_cols_log2);
429  uniform_rows = count_uniform_tiling(avctx->height,
430  64, ctx->tile_rows_log2);
431  av_log(avctx, AV_LOG_DEBUG, "Uniform with 64x64 superblocks "
432  "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
433  uniform_64x64_possible = uniform_cols == ctx->tile_cols &&
434  uniform_rows == ctx->tile_rows;
435 
436  if (sb_128x128_possible) {
437  uniform_cols = count_uniform_tiling(avctx->width,
438  128, ctx->tile_cols_log2);
439  uniform_rows = count_uniform_tiling(avctx->height,
440  128, ctx->tile_rows_log2);
441  av_log(avctx, AV_LOG_DEBUG, "Uniform with 128x128 superblocks "
442  "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
443  uniform_128x128_possible = uniform_cols == ctx->tile_cols &&
444  uniform_rows == ctx->tile_rows;
445  } else {
446  av_log(avctx, AV_LOG_DEBUG, "128x128 superblocks not possible.\n");
447  uniform_128x128_possible = 0;
448  }
449 
450  ctx->uniform_tiles = 1;
451  if (uniform_64x64_possible && uniform_128x128_possible) {
452  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with dynamic "
453  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
454  ctx->tile_cols_log2, ctx->tile_rows_log2);
455  return 0;
456  }
457  if (uniform_64x64_possible && !sb_128x128_possible) {
458  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 64x64 "
459  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
460  ctx->tile_cols_log2, ctx->tile_rows_log2);
461  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
462  return 0;
463  }
464  if (uniform_128x128_possible) {
465  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 128x128 "
466  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
467  ctx->tile_cols_log2, ctx->tile_rows_log2);
468  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
469  return 0;
470  }
471  ctx->uniform_tiles = 0;
472 
473  if (sb_128x128_possible) {
474  sb_size = 128;
475  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
476  } else {
477  sb_size = 64;
478  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
479  }
480  av_log(avctx, AV_LOG_DEBUG, "Using fixed tiling with %dx%d "
481  "superblocks (tile_cols = %d, tile_rows = %d).\n",
482  sb_size, sb_size, ctx->tile_cols, ctx->tile_rows);
483 
484  enccfg->tile_width_count = ctx->tile_cols;
485  enccfg->tile_height_count = ctx->tile_rows;
486 
487  sb_width = (avctx->width + sb_size - 1) / sb_size;
488  sb_height = (avctx->height + sb_size - 1) / sb_size;
489 
490  tile_size = sb_width / ctx->tile_cols;
491  rounding = sb_width % ctx->tile_cols;
492  for (i = 0; i < ctx->tile_cols; i++) {
493  enccfg->tile_widths[i] = tile_size +
494  (i < rounding / 2 ||
495  i > ctx->tile_cols - 1 - (rounding + 1) / 2);
496  }
497 
498  tile_size = sb_height / ctx->tile_rows;
499  rounding = sb_height % ctx->tile_rows;
500  for (i = 0; i < ctx->tile_rows; i++) {
501  enccfg->tile_heights[i] = tile_size +
502  (i < rounding / 2 ||
503  i > ctx->tile_rows - 1 - (rounding + 1) / 2);
504  }
505 
506  return 0;
507 }
508 
509 static av_cold int aom_init(AVCodecContext *avctx,
510  const struct aom_codec_iface *iface)
511 {
512  AOMContext *ctx = avctx->priv_data;
513  struct aom_codec_enc_cfg enccfg = { 0 };
514 #ifdef AOM_FRAME_IS_INTRAONLY
515  aom_codec_flags_t flags =
516  (avctx->flags & AV_CODEC_FLAG_PSNR) ? AOM_CODEC_USE_PSNR : 0;
517 #else
518  aom_codec_flags_t flags = 0;
519 #endif
520  AVCPBProperties *cpb_props;
521  int res;
522  aom_img_fmt_t img_fmt;
523  aom_codec_caps_t codec_caps = aom_codec_get_caps(iface);
524 
525  av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
526  av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
527 
528  if ((res = aom_codec_enc_config_default(iface, &enccfg, 0)) != AOM_CODEC_OK) {
529  av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
530  aom_codec_err_to_string(res));
531  return AVERROR(EINVAL);
532  }
533 
534  if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
535  return AVERROR(EINVAL);
536 
537  if(!avctx->bit_rate)
538  if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
539  av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
540  return AVERROR(EINVAL);
541  }
542 
543  dump_enc_cfg(avctx, &enccfg);
544 
545  enccfg.g_w = avctx->width;
546  enccfg.g_h = avctx->height;
547  enccfg.g_timebase.num = avctx->time_base.num;
548  enccfg.g_timebase.den = avctx->time_base.den;
549  enccfg.g_threads =
550  FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 64);
551 
552  if (ctx->lag_in_frames >= 0)
553  enccfg.g_lag_in_frames = ctx->lag_in_frames;
554 
555  if (avctx->flags & AV_CODEC_FLAG_PASS1)
556  enccfg.g_pass = AOM_RC_FIRST_PASS;
557  else if (avctx->flags & AV_CODEC_FLAG_PASS2)
558  enccfg.g_pass = AOM_RC_LAST_PASS;
559  else
560  enccfg.g_pass = AOM_RC_ONE_PASS;
561 
562  if (avctx->rc_min_rate == avctx->rc_max_rate &&
563  avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
564  enccfg.rc_end_usage = AOM_CBR;
565  } else if (ctx->crf >= 0) {
566  enccfg.rc_end_usage = AOM_CQ;
567  if (!avctx->bit_rate)
568  enccfg.rc_end_usage = AOM_Q;
569  }
570 
571  if (avctx->bit_rate) {
572  enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
574  } else if (enccfg.rc_end_usage != AOM_Q) {
575  if (enccfg.rc_end_usage == AOM_CQ) {
576  enccfg.rc_target_bitrate = 1000000;
577  } else {
578  avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
579  av_log(avctx, AV_LOG_WARNING,
580  "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
581  enccfg.rc_target_bitrate);
582  }
583  }
584 
585  if (avctx->qmin >= 0)
586  enccfg.rc_min_quantizer = avctx->qmin;
587  if (avctx->qmax >= 0)
588  enccfg.rc_max_quantizer = avctx->qmax;
589 
590  if (enccfg.rc_end_usage == AOM_CQ || enccfg.rc_end_usage == AOM_Q) {
591  if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
592  av_log(avctx, AV_LOG_ERROR,
593  "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
594  ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
595  return AVERROR(EINVAL);
596  }
597  }
598 
599  enccfg.rc_dropframe_thresh = ctx->drop_threshold;
600 
601  // 0-100 (0 => CBR, 100 => VBR)
602  enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
603  if (ctx->minsection_pct >= 0)
604  enccfg.rc_2pass_vbr_minsection_pct = ctx->minsection_pct;
605  else if (avctx->bit_rate)
606  enccfg.rc_2pass_vbr_minsection_pct =
607  avctx->rc_min_rate * 100LL / avctx->bit_rate;
608  if (ctx->maxsection_pct >= 0)
609  enccfg.rc_2pass_vbr_maxsection_pct = ctx->maxsection_pct;
610  else if (avctx->rc_max_rate)
611  enccfg.rc_2pass_vbr_maxsection_pct =
612  avctx->rc_max_rate * 100LL / avctx->bit_rate;
613 
614  if (avctx->rc_buffer_size)
615  enccfg.rc_buf_sz =
616  avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
617  if (avctx->rc_initial_buffer_occupancy)
618  enccfg.rc_buf_initial_sz =
619  avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
620  enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
621 
622  if (ctx->rc_undershoot_pct >= 0)
623  enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct;
624  if (ctx->rc_overshoot_pct >= 0)
625  enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct;
626 
627  // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO
628  if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
629  enccfg.kf_min_dist = avctx->keyint_min;
630  if (avctx->gop_size >= 0)
631  enccfg.kf_max_dist = avctx->gop_size;
632 
633  if (enccfg.g_pass == AOM_RC_FIRST_PASS)
634  enccfg.g_lag_in_frames = 0;
635  else if (enccfg.g_pass == AOM_RC_LAST_PASS) {
636  int decode_size, ret;
637 
638  if (!avctx->stats_in) {
639  av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
640  return AVERROR_INVALIDDATA;
641  }
642 
643  ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
644  ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
645  if (ret < 0) {
646  av_log(avctx, AV_LOG_ERROR,
647  "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
648  ctx->twopass_stats.sz);
649  ctx->twopass_stats.sz = 0;
650  return ret;
651  }
652  decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
653  ctx->twopass_stats.sz);
654  if (decode_size < 0) {
655  av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
656  return AVERROR_INVALIDDATA;
657  }
658 
659  ctx->twopass_stats.sz = decode_size;
660  enccfg.rc_twopass_stats_in = ctx->twopass_stats;
661  }
662 
663  /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
664  * complexity playback on low powered devices at the expense of encode
665  * quality. */
666  if (avctx->profile != FF_PROFILE_UNKNOWN)
667  enccfg.g_profile = avctx->profile;
668 
669  enccfg.g_error_resilient = ctx->error_resilient;
670 
671  res = choose_tiling(avctx, &enccfg);
672  if (res < 0)
673  return res;
674 
675  dump_enc_cfg(avctx, &enccfg);
676  /* Construct Encoder Context */
677  res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
678  if (res != AOM_CODEC_OK) {
679  log_encoder_error(avctx, "Failed to initialize encoder");
680  return AVERROR(EINVAL);
681  }
682 
683  // codec control failures are currently treated only as warnings
684  av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n");
685  codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used);
686  if (ctx->auto_alt_ref >= 0)
687  codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
688  if (ctx->arnr_max_frames >= 0)
689  codecctl_int(avctx, AOME_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
690  if (ctx->arnr_strength >= 0)
691  codecctl_int(avctx, AOME_SET_ARNR_STRENGTH, ctx->arnr_strength);
692  if (ctx->enable_cdef >= 0)
693  codecctl_int(avctx, AV1E_SET_ENABLE_CDEF, ctx->enable_cdef);
694  codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh);
695  if (ctx->crf >= 0)
696  codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf);
697 
698  codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries);
699  codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace);
700  codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc);
701  if (ctx->aq_mode >= 0)
702  codecctl_int(avctx, AV1E_SET_AQ_MODE, ctx->aq_mode);
703  if (ctx->frame_parallel >= 0)
704  codecctl_int(avctx, AV1E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
705  set_color_range(avctx);
706 
707  codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size);
708  if (ctx->uniform_tiles) {
709  codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2);
710  codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2);
711  }
712 
713 #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL
714  if (ctx->denoise_noise_level >= 0)
715  codecctl_int(avctx, AV1E_SET_DENOISE_NOISE_LEVEL, ctx->denoise_noise_level);
716 #endif
717 #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE
718  if (ctx->denoise_block_size >= 0)
719  codecctl_int(avctx, AV1E_SET_DENOISE_BLOCK_SIZE, ctx->denoise_block_size);
720 #endif
721 #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION
722  if (ctx->enable_global_motion >= 0)
723  codecctl_int(avctx, AV1E_SET_ENABLE_GLOBAL_MOTION, ctx->enable_global_motion);
724 #endif
725 #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES
726  if (avctx->refs >= 3) {
727  codecctl_int(avctx, AV1E_SET_MAX_REFERENCE_FRAMES, avctx->refs);
728  }
729 #endif
730 #ifdef AOM_CTRL_AV1E_SET_ROW_MT
731  if (ctx->row_mt >= 0)
732  codecctl_int(avctx, AV1E_SET_ROW_MT, ctx->row_mt);
733 #endif
734 #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC
735  if (ctx->enable_intrabc >= 0)
736  codecctl_int(avctx, AV1E_SET_ENABLE_INTRABC, ctx->enable_intrabc);
737 #endif
738 
739  // provide dummy value to initialize wrapper, values will be updated each _encode()
740  aom_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
741  (unsigned char*)1);
742 
743  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
744  ctx->rawimg.bit_depth = enccfg.g_bit_depth;
745 
746  cpb_props = ff_add_cpb_side_data(avctx);
747  if (!cpb_props)
748  return AVERROR(ENOMEM);
749 
750  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
751  const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata");
752  int ret;
753 
754  if (!filter) {
755  av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter "
756  "not found. This is a bug, please report it.\n");
757  return AVERROR_BUG;
758  }
759  ret = av_bsf_alloc(filter, &ctx->bsf);
760  if (ret < 0)
761  return ret;
762 
763  ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx);
764  if (ret < 0)
765  return ret;
766 
767  ret = av_bsf_init(ctx->bsf);
768  if (ret < 0)
769  return ret;
770  }
771 
772  if (enccfg.rc_end_usage == AOM_CBR ||
773  enccfg.g_pass != AOM_RC_ONE_PASS) {
774  cpb_props->max_bitrate = avctx->rc_max_rate;
775  cpb_props->min_bitrate = avctx->rc_min_rate;
776  cpb_props->avg_bitrate = avctx->bit_rate;
777  }
778  cpb_props->buffer_size = avctx->rc_buffer_size;
779 
780  return 0;
781 }
782 
783 static inline void cx_pktcpy(AOMContext *ctx,
784  struct FrameListData *dst,
785  const struct aom_codec_cx_pkt *src)
786 {
787  dst->pts = src->data.frame.pts;
788  dst->duration = src->data.frame.duration;
789  dst->flags = src->data.frame.flags;
790  dst->sz = src->data.frame.sz;
791  dst->buf = src->data.frame.buf;
792 #ifdef AOM_FRAME_IS_INTRAONLY
793  dst->have_sse = 0;
794  dst->frame_number = ++ctx->frame_number;
795  dst->have_sse = ctx->have_sse;
796  if (ctx->have_sse) {
797  /* associate last-seen SSE to the frame. */
798  /* Transfers ownership from ctx to dst. */
799  memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
800  ctx->have_sse = 0;
801  }
802 #endif
803 }
804 
805 /**
806  * Store coded frame information in format suitable for return from encode2().
807  *
808  * Write information from @a cx_frame to @a pkt
809  * @return packet data size on success
810  * @return a negative AVERROR on error
811  */
812 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
813  AVPacket *pkt)
814 {
815  AOMContext *ctx = avctx->priv_data;
816  int av_unused pict_type;
817  int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0);
818  if (ret < 0) {
819  av_log(avctx, AV_LOG_ERROR,
820  "Error getting output packet of size %"SIZE_SPECIFIER".\n", cx_frame->sz);
821  return ret;
822  }
823  memcpy(pkt->data, cx_frame->buf, pkt->size);
824  pkt->pts = pkt->dts = cx_frame->pts;
825 
826  if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) {
827  pkt->flags |= AV_PKT_FLAG_KEY;
828 #ifdef AOM_FRAME_IS_INTRAONLY
829  pict_type = AV_PICTURE_TYPE_I;
830  } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) {
831  pict_type = AV_PICTURE_TYPE_I;
832  } else {
833  pict_type = AV_PICTURE_TYPE_P;
834  }
835 
836  ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1,
837  cx_frame->have_sse ? 3 : 0, pict_type);
838 
839  if (cx_frame->have_sse) {
840  int i;
841  for (i = 0; i < 3; ++i) {
842  avctx->error[i] += cx_frame->sse[i + 1];
843  }
844  cx_frame->have_sse = 0;
845 #endif
846  }
847 
848  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
849  ret = av_bsf_send_packet(ctx->bsf, pkt);
850  if (ret < 0) {
851  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
852  "failed to send input packet\n");
853  return ret;
854  }
855  ret = av_bsf_receive_packet(ctx->bsf, pkt);
856 
857  if (ret < 0) {
858  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
859  "failed to receive output packet\n");
860  return ret;
861  }
862  }
863  return pkt->size;
864 }
865 
866 /**
867  * Queue multiple output frames from the encoder, returning the front-most.
868  * In cases where aom_codec_get_cx_data() returns more than 1 frame append
869  * the frame queue. Return the head frame if available.
870  * @return Stored frame size
871  * @return AVERROR(EINVAL) on output size error
872  * @return AVERROR(ENOMEM) on coded frame queue data allocation error
873  */
874 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
875 {
876  AOMContext *ctx = avctx->priv_data;
877  const struct aom_codec_cx_pkt *pkt;
878  const void *iter = NULL;
879  int size = 0;
880 
881  if (ctx->coded_frame_list) {
882  struct FrameListData *cx_frame = ctx->coded_frame_list;
883  /* return the leading frame if we've already begun queueing */
884  size = storeframe(avctx, cx_frame, pkt_out);
885  if (size < 0)
886  return size;
887  ctx->coded_frame_list = cx_frame->next;
888  free_coded_frame(cx_frame);
889  }
890 
891  /* consume all available output from the encoder before returning. buffers
892  * are only good through the next aom_codec call */
893  while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) {
894  switch (pkt->kind) {
895  case AOM_CODEC_CX_FRAME_PKT:
896  if (!size) {
897  struct FrameListData cx_frame;
898 
899  /* avoid storing the frame when the list is empty and we haven't yet
900  * provided a frame for output */
902  cx_pktcpy(ctx, &cx_frame, pkt);
903  size = storeframe(avctx, &cx_frame, pkt_out);
904  if (size < 0)
905  return size;
906  } else {
907  struct FrameListData *cx_frame =
908  av_malloc(sizeof(struct FrameListData));
909 
910  if (!cx_frame) {
911  av_log(avctx, AV_LOG_ERROR,
912  "Frame queue element alloc failed\n");
913  return AVERROR(ENOMEM);
914  }
915  cx_pktcpy(ctx, cx_frame, pkt);
916  cx_frame->buf = av_malloc(cx_frame->sz);
917 
918  if (!cx_frame->buf) {
919  av_log(avctx, AV_LOG_ERROR,
920  "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
921  cx_frame->sz);
922  av_freep(&cx_frame);
923  return AVERROR(ENOMEM);
924  }
925  memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
926  coded_frame_add(&ctx->coded_frame_list, cx_frame);
927  }
928  break;
929  case AOM_CODEC_STATS_PKT:
930  {
931  struct aom_fixed_buf *stats = &ctx->twopass_stats;
932  int err;
933  if ((err = av_reallocp(&stats->buf,
934  stats->sz +
935  pkt->data.twopass_stats.sz)) < 0) {
936  stats->sz = 0;
937  av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
938  return err;
939  }
940  memcpy((uint8_t *)stats->buf + stats->sz,
941  pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
942  stats->sz += pkt->data.twopass_stats.sz;
943  break;
944  }
945 #ifdef AOM_FRAME_IS_INTRAONLY
946  case AOM_CODEC_PSNR_PKT:
947  {
948  av_assert0(!ctx->have_sse);
949  ctx->sse[0] = pkt->data.psnr.sse[0];
950  ctx->sse[1] = pkt->data.psnr.sse[1];
951  ctx->sse[2] = pkt->data.psnr.sse[2];
952  ctx->sse[3] = pkt->data.psnr.sse[3];
953  ctx->have_sse = 1;
954  break;
955  }
956 #endif
957  case AOM_CODEC_CUSTOM_PKT:
958  // ignore unsupported/unrecognized packet types
959  break;
960  }
961  }
962 
963  return size;
964 }
965 
966 static int aom_encode(AVCodecContext *avctx, AVPacket *pkt,
967  const AVFrame *frame, int *got_packet)
968 {
969  AOMContext *ctx = avctx->priv_data;
970  struct aom_image *rawimg = NULL;
971  int64_t timestamp = 0;
972  int res, coded_size;
973  aom_enc_frame_flags_t flags = 0;
974 
975  if (frame) {
976  rawimg = &ctx->rawimg;
977  rawimg->planes[AOM_PLANE_Y] = frame->data[0];
978  rawimg->planes[AOM_PLANE_U] = frame->data[1];
979  rawimg->planes[AOM_PLANE_V] = frame->data[2];
980  rawimg->stride[AOM_PLANE_Y] = frame->linesize[0];
981  rawimg->stride[AOM_PLANE_U] = frame->linesize[1];
982  rawimg->stride[AOM_PLANE_V] = frame->linesize[2];
983  timestamp = frame->pts;
984  switch (frame->color_range) {
985  case AVCOL_RANGE_MPEG:
986  rawimg->range = AOM_CR_STUDIO_RANGE;
987  break;
988  case AVCOL_RANGE_JPEG:
989  rawimg->range = AOM_CR_FULL_RANGE;
990  break;
991  }
992 
993  if (frame->pict_type == AV_PICTURE_TYPE_I)
994  flags |= AOM_EFLAG_FORCE_KF;
995  }
996 
997  res = aom_codec_encode(&ctx->encoder, rawimg, timestamp,
998  avctx->ticks_per_frame, flags);
999  if (res != AOM_CODEC_OK) {
1000  log_encoder_error(avctx, "Error encoding frame");
1001  return AVERROR_INVALIDDATA;
1002  }
1003  coded_size = queue_frames(avctx, pkt);
1004 
1005  if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
1006  size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
1007 
1008  avctx->stats_out = av_malloc(b64_size);
1009  if (!avctx->stats_out) {
1010  av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
1011  b64_size);
1012  return AVERROR(ENOMEM);
1013  }
1014  av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
1015  ctx->twopass_stats.sz);
1016  }
1017 
1018  *got_packet = !!coded_size;
1019  return 0;
1020 }
1021 
1022 static const enum AVPixelFormat av1_pix_fmts[] = {
1027 };
1028 
1029 static const enum AVPixelFormat av1_pix_fmts_highbd[] = {
1040 };
1041 
1042 static av_cold void av1_init_static(AVCodec *codec)
1043 {
1044  aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx());
1045  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
1046  codec->pix_fmts = av1_pix_fmts_highbd;
1047  else
1048  codec->pix_fmts = av1_pix_fmts;
1049 }
1050 
1051 static av_cold int av1_init(AVCodecContext *avctx)
1052 {
1053  return aom_init(avctx, aom_codec_av1_cx());
1054 }
1055 
1056 #define OFFSET(x) offsetof(AOMContext, x)
1057 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1058 static const AVOption options[] = {
1059  { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VE},
1060  { "auto-alt-ref", "Enable use of alternate reference "
1061  "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
1062  { "lag-in-frames", "Number of frames to look ahead at for "
1063  "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1064  { "arnr-max-frames", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1065  { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1066  { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 4, VE, "aq_mode"},
1067  { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode"},
1068  { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode"},
1069  { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode"},
1070  { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode"},
1071  { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
1072  { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
1073  { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE },
1074  { "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 },
1075  { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
1076  { "denoise-noise-level", "Amount of noise to be removed", OFFSET(denoise_noise_level), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1077  { "denoise-block-size", "Denoise block size ", OFFSET(denoise_block_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1078  { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
1079  { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1000, VE},
1080  { "minsection-pct", "GOP min bitrate (% of target)", OFFSET(minsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
1081  { "maxsection-pct", "GOP max bitrate (% of target)", OFFSET(maxsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 5000, VE},
1082  { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1083  { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE },
1084  { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1085  { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1086  { "row-mt", "Enable row based multi-threading", OFFSET(row_mt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1087  { "enable-cdef", "Enable CDEF filtering", OFFSET(enable_cdef), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1088  { "enable-global-motion", "Enable global motion", OFFSET(enable_global_motion), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1089  { "enable-intrabc", "Enable intra block copy prediction mode", OFFSET(enable_intrabc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1090  { NULL },
1091 };
1092 
1093 static const AVCodecDefault defaults[] = {
1094  { "b", "256*1000" },
1095  { "qmin", "-1" },
1096  { "qmax", "-1" },
1097  { "g", "-1" },
1098  { "keyint_min", "-1" },
1099  { NULL },
1100 };
1101 
1102 static const AVClass class_aom = {
1103  .class_name = "libaom-av1 encoder",
1104  .item_name = av_default_item_name,
1105  .option = options,
1106  .version = LIBAVUTIL_VERSION_INT,
1107 };
1108 
1110  .name = "libaom-av1",
1111  .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
1112  .type = AVMEDIA_TYPE_VIDEO,
1113  .id = AV_CODEC_ID_AV1,
1114  .priv_data_size = sizeof(AOMContext),
1115  .init = av1_init,
1116  .encode2 = aom_encode,
1117  .close = aom_free,
1120  .priv_class = &class_aom,
1121  .defaults = defaults,
1122  .init_static_data = av1_init_static,
1123  .wrapper_name = "libaom",
1124 };
#define OFFSET(x)
Definition: libaomenc.c:1056
int denoise_block_size
Definition: libaomenc.c:78
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:35
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:863
uint64_t sse[4]
Definition: libaomenc.c:79
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
int minsection_pct
Definition: libaomenc.c:84
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
int enable_cdef
Definition: libaomenc.c:92
AVOption.
Definition: opt.h:246
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:720
int av_cpu_count(void)
Definition: cpu.c:267
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition: avcodec.h:2748
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1615
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
const char * desc
Definition: nvenc.c:68
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:1134
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:2471
static av_cold int aom_init(AVCodecContext *avctx, const struct aom_codec_iface *iface)
Definition: libaomenc.c:509
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2200
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: avcodec.h:5763
int size
Definition: avcodec.h:1478
void * buf
compressed data buffer
Definition: libaomenc.c:47
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
int maxsection_pct
Definition: libaomenc.c:85
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
size_t sz
length of compressed data
Definition: libaomenc.c:48
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:1029
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:391
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2592
int tile_cols_log2
Definition: libaomenc.c:88
uint64_t frame_number
Definition: libaomenc.c:81
static AVPacket pkt
static const AVOption options[]
Definition: libaomenc.c:1058
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1049
#define src
Definition: vp8dsp.c:254
int profile
profile
Definition: avcodec.h:2898
AVCodec.
Definition: avcodec.h:3481
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:135
int min_bitrate
Minimum bitrate of the stream, in bits per second.
Definition: avcodec.h:1143
int error_resilient
Definition: libaomenc.c:73
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1688
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:82
int tile_rows_log2
Definition: libaomenc.c:88
static int choose_tiling(AVCodecContext *avctx, struct aom_codec_enc_cfg *enccfg)
Definition: libaomenc.c:354
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:1006
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int tile_cols
Definition: h265_levels.c:218
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
static av_cold void free_frame_list(struct FrameListData *list)
Definition: libaomenc.c:219
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:212
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:153
struct FrameListData * next
Definition: libaomenc.c:57
uint8_t
struct aom_fixed_buf twopass_stats
Definition: libaomenc.c:65
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
int64_t pts
time stamp to show frame (in timebase units)
Definition: libaomenc.c:49
int arnr_strength
Definition: libaomenc.c:70
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
static AVFrame * frame
struct aom_image rawimg
Definition: libaomenc.c:64
uint8_t * data
Definition: avcodec.h:1477
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static av_cold int codecctl_int(AVCodecContext *avctx, enum aome_enc_control_id id, int val)
Definition: libaomenc.c:230
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:1161
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:392
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2584
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int rc_overshoot_pct
Definition: libaomenc.c:83
int enable_intrabc
Definition: libaomenc.c:94
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
static void cx_pktcpy(AOMContext *ctx, struct FrameListData *dst, const struct aom_codec_cx_pkt *src)
Definition: libaomenc.c:783
#define VE
Definition: libaomenc.c:1057
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_cold void av1_init_static(AVCodec *codec)
Definition: libaomenc.c:1042
int tile_rows
Definition: libaomenc.c:87
#define AVERROR(e)
Definition: error.h:43
int qmax
maximum quantizer
Definition: avcodec.h:2414
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int aq_mode
Definition: libaomenc.c:71
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:539
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1645
Round to nearest and halfway cases away from zero.
Definition: mathematics.h:84
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
static av_always_inline av_const double round(double x)
Definition: libm.h:444
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:390
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:138
static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt)
Store coded frame information in format suitable for return from encode2().
Definition: libaomenc.c:812
static void set_color_range(AVCodecContext *avctx)
Definition: libaomenc.c:330
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2428
static const AVClass class_aom
Definition: libaomenc.c:1102
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:2450
AVCodec ff_libaom_av1_encoder
Definition: libaomenc.c:1109
int refs
number of reference frames
Definition: avcodec.h:2153
struct FrameListData * coded_frame_list
Definition: libaomenc.c:66
static enum AVPixelFormat av1_pix_fmts_highbd[]
Definition: libaomenc.c:1029
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3502
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition: base64.h:66
static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
Queue multiple output frames from the encoder, returning the front-most.
Definition: libaomenc.c:874
#define FFMIN(a, b)
Definition: common.h:96
uint64_t sse[4]
Definition: libaomenc.c:54
#define width
int width
picture width / height.
Definition: avcodec.h:1738
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2899
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:186
static av_cold void free_coded_frame(struct FrameListData *cx_frame)
Definition: libaomenc.c:213
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:887
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:871
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2179
static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
Definition: libaomenc.c:346
static void stats(AVPacket *const *in, int n_in, unsigned *_max, unsigned *_sum)
int row_mt
Definition: libaomenc.c:91
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1697
static const char *const ctlidstr[]
Definition: libaomenc.c:97
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
static void error(const char *err)
int cpu_used
Definition: libaomenc.c:67
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2824
#define av_log2
Definition: intmath.h:83
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:522
int have_sse
true if we have pending sse[]
Definition: libaomenc.c:80
static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps, struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags, aom_img_fmt_t *img_fmt)
Definition: libaomenc.c:269
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:1128
struct aom_codec_ctx encoder
Definition: libaomenc.c:63
int enable_global_motion
Definition: libaomenc.c:93
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
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:163
Libavcodec external API header.
aom_superblock_size_t superblock_size
Definition: libaomenc.c:89
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
const AVProfile ff_av1_profiles[]
Definition: profiles.c:142
main external API structure.
Definition: avcodec.h:1565
static enum AVPixelFormat av1_pix_fmts[]
Definition: libaomenc.c:1022
int qmin
minimum quantizer
Definition: avcodec.h:2407
AV1 common definitions.
int frame_parallel
Definition: libaomenc.c:86
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
static av_cold int av1_init(AVCodecContext *avctx)
Definition: libaomenc.c:1051
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:387
static void coded_frame_add(void *list, struct FrameListData *cx_frame)
Definition: libaomenc.c:203
Describe the class of an AVClass context structure.
Definition: log.h:67
static const AVProfile profiles[]
#define FF_PROFILE_AV1_PROFESSIONAL
Definition: avcodec.h:2993
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2193
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2186
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2058
int uniform_tiles
Definition: libaomenc.c:90
#define FF_PROFILE_AV1_MAIN
Definition: avcodec.h:2991
uint32_t flags
flags for this frame
Definition: libaomenc.c:53
int dim
#define snprintf
Definition: snprintf.h:34
uint64_t frame_number
Definition: libaomenc.c:56
offset must point to two consecutive integers
Definition: opt.h:233
int static_thresh
Definition: libaomenc.c:75
static av_cold void dump_enc_cfg(AVCodecContext *avctx, const struct aom_codec_enc_cfg *cfg)
Definition: libaomenc.c:145
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2399
int drop_threshold
Definition: libaomenc.c:76
int have_sse
true if we have pending sse[]
Definition: libaomenc.c:55
#define SIZE_SPECIFIER
Definition: internal.h:262
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:388
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:394
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
uint8_t level
Definition: svq3.c:207
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:904
AVBSFContext * bsf
Definition: libaomenc.c:62
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:521
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1760
int denoise_noise_level
Definition: libaomenc.c:77
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
common internal api header.
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
Definition: libaomenc.c:134
common internal and external API header
int auto_alt_ref
Definition: libaomenc.c:68
static int aom_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libaomenc.c:966
int den
Denominator.
Definition: rational.h:60
AVCPBProperties * ff_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition: utils.c:1973
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:875
void * priv_data
Definition: avcodec.h:1592
int tile_cols
Definition: libaomenc.c:87
Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
Definition: libaomenc.c:46
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:1152
int arnr_max_frames
Definition: libaomenc.c:69
unsigned long duration
duration to show frame (in timebase units)
Definition: libaomenc.c:51
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition: base64.c:79
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1476
#define av_freep(p)
static const AVCodecDefault defaults[]
Definition: libaomenc.c:1093
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1454
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5791
int rc_undershoot_pct
Definition: libaomenc.c:82
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
static av_cold int aom_free(AVCodecContext *avctx)
Definition: libaomenc.c:257
Predicted.
Definition: avutil.h:275
int lag_in_frames
Definition: libaomenc.c:72
#define av_unused
Definition: attributes.h:125
#define FF_PROFILE_AV1_HIGH
Definition: avcodec.h:2992
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2443
int keyint_min
minimum GOP size
Definition: avcodec.h:2146