FFmpeg
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 "packet_internal.h"
41 #include "profiles.h"
42 
43 /*
44  * Portion of struct aom_codec_cx_pkt from aom_encoder.h.
45  * One encoded frame returned from the library.
46  */
47 struct FrameListData {
48  void *buf; /**< compressed data buffer */
49  size_t sz; /**< length of compressed data */
50  int64_t pts; /**< time stamp to show frame
51  (in timebase units) */
52  unsigned long duration; /**< duration to show frame
53  (in timebase units) */
54  uint32_t flags; /**< flags for this frame */
55  uint64_t sse[4];
56  int have_sse; /**< true if we have pending sse[] */
57  uint64_t frame_number;
59 };
60 
61 typedef struct AOMEncoderContext {
62  AVClass *class;
64  struct aom_codec_ctx encoder;
65  struct aom_image rawimg;
66  struct aom_fixed_buf twopass_stats;
68  int cpu_used;
72  int aq_mode;
75  int crf;
80  uint64_t sse[4];
81  int have_sse; /**< true if we have pending sse[] */
82  uint64_t frame_number;
89  int tile_cols_log2, tile_rows_log2;
90  aom_superblock_size_t superblock_size;
92  int row_mt;
97  int usage;
98  int tune;
127 } AOMContext;
128 
129 static const char *const ctlidstr[] = {
130  [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED",
131  [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL",
132  [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF",
133  [AOME_SET_ARNR_MAXFRAMES] = "AOME_SET_ARNR_MAXFRAMES",
134  [AOME_SET_ARNR_STRENGTH] = "AOME_SET_ARNR_STRENGTH",
135  [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD",
136  [AV1E_SET_COLOR_RANGE] = "AV1E_SET_COLOR_RANGE",
137  [AV1E_SET_COLOR_PRIMARIES] = "AV1E_SET_COLOR_PRIMARIES",
138  [AV1E_SET_MATRIX_COEFFICIENTS] = "AV1E_SET_MATRIX_COEFFICIENTS",
139  [AV1E_SET_TRANSFER_CHARACTERISTICS] = "AV1E_SET_TRANSFER_CHARACTERISTICS",
140  [AV1E_SET_AQ_MODE] = "AV1E_SET_AQ_MODE",
141  [AV1E_SET_FRAME_PARALLEL_DECODING] = "AV1E_SET_FRAME_PARALLEL_DECODING",
142  [AV1E_SET_SUPERBLOCK_SIZE] = "AV1E_SET_SUPERBLOCK_SIZE",
143  [AV1E_SET_TILE_COLUMNS] = "AV1E_SET_TILE_COLUMNS",
144  [AV1E_SET_TILE_ROWS] = "AV1E_SET_TILE_ROWS",
145  [AV1E_SET_ENABLE_RESTORATION] = "AV1E_SET_ENABLE_RESTORATION",
146 #ifdef AOM_CTRL_AV1E_SET_ROW_MT
147  [AV1E_SET_ROW_MT] = "AV1E_SET_ROW_MT",
148 #endif
149 #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL
150  [AV1E_SET_DENOISE_NOISE_LEVEL] = "AV1E_SET_DENOISE_NOISE_LEVEL",
151 #endif
152 #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE
153  [AV1E_SET_DENOISE_BLOCK_SIZE] = "AV1E_SET_DENOISE_BLOCK_SIZE",
154 #endif
155 #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES
156  [AV1E_SET_MAX_REFERENCE_FRAMES] = "AV1E_SET_MAX_REFERENCE_FRAMES",
157 #endif
158 #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION
159  [AV1E_SET_ENABLE_GLOBAL_MOTION] = "AV1E_SET_ENABLE_GLOBAL_MOTION",
160 #endif
161 #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC
162  [AV1E_SET_ENABLE_INTRABC] = "AV1E_SET_ENABLE_INTRABC",
163 #endif
164  [AV1E_SET_ENABLE_CDEF] = "AV1E_SET_ENABLE_CDEF",
165  [AOME_SET_TUNING] = "AOME_SET_TUNING",
166 #if AOM_ENCODER_ABI_VERSION >= 22
167  [AV1E_SET_ENABLE_1TO4_PARTITIONS] = "AV1E_SET_ENABLE_1TO4_PARTITIONS",
168  [AV1E_SET_ENABLE_AB_PARTITIONS] = "AV1E_SET_ENABLE_AB_PARTITIONS",
169  [AV1E_SET_ENABLE_RECT_PARTITIONS] = "AV1E_SET_ENABLE_RECT_PARTITIONS",
170  [AV1E_SET_ENABLE_ANGLE_DELTA] = "AV1E_SET_ENABLE_ANGLE_DELTA",
171  [AV1E_SET_ENABLE_CFL_INTRA] = "AV1E_SET_ENABLE_CFL_INTRA",
172  [AV1E_SET_ENABLE_FILTER_INTRA] = "AV1E_SET_ENABLE_FILTER_INTRA",
173  [AV1E_SET_ENABLE_INTRA_EDGE_FILTER] = "AV1E_SET_ENABLE_INTRA_EDGE_FILTER",
174  [AV1E_SET_ENABLE_PAETH_INTRA] = "AV1E_SET_ENABLE_PAETH_INTRA",
175  [AV1E_SET_ENABLE_SMOOTH_INTRA] = "AV1E_SET_ENABLE_SMOOTH_INTRA",
176  [AV1E_SET_ENABLE_PALETTE] = "AV1E_SET_ENABLE_PALETTE",
177  [AV1E_SET_ENABLE_FLIP_IDTX] = "AV1E_SET_ENABLE_FLIP_IDTX",
178  [AV1E_SET_ENABLE_TX64] = "AV1E_SET_ENABLE_TX64",
179  [AV1E_SET_INTRA_DCT_ONLY] = "AV1E_SET_INTRA_DCT_ONLY",
180  [AV1E_SET_INTER_DCT_ONLY] = "AV1E_SET_INTER_DCT_ONLY",
181  [AV1E_SET_INTRA_DEFAULT_TX_ONLY] = "AV1E_SET_INTRA_DEFAULT_TX_ONLY",
182  [AV1E_SET_REDUCED_TX_TYPE_SET] = "AV1E_SET_REDUCED_TX_TYPE_SET",
183  [AV1E_SET_ENABLE_DIFF_WTD_COMP] = "AV1E_SET_ENABLE_DIFF_WTD_COMP",
184  [AV1E_SET_ENABLE_DIST_WTD_COMP] = "AV1E_SET_ENABLE_DIST_WTD_COMP",
185  [AV1E_SET_ENABLE_DUAL_FILTER] = "AV1E_SET_ENABLE_DUAL_FILTER",
186  [AV1E_SET_ENABLE_INTERINTER_WEDGE] = "AV1E_SET_ENABLE_INTERINTER_WEDGE",
187  [AV1E_SET_ENABLE_INTERINTRA_WEDGE] = "AV1E_SET_ENABLE_INTERINTRA_WEDGE",
188  [AV1E_SET_ENABLE_MASKED_COMP] = "AV1E_SET_ENABLE_MASKED_COMP",
189  [AV1E_SET_ENABLE_INTERINTRA_COMP] = "AV1E_SET_ENABLE_INTERINTRA_COMP",
190  [AV1E_SET_ENABLE_OBMC] = "AV1E_SET_ENABLE_OBMC",
191  [AV1E_SET_ENABLE_ONESIDED_COMP] = "AV1E_SET_ENABLE_ONESIDED_COMP",
192  [AV1E_SET_REDUCED_REFERENCE_SET] = "AV1E_SET_REDUCED_REFERENCE_SET",
193  [AV1E_SET_ENABLE_SMOOTH_INTERINTRA] = "AV1E_SET_ENABLE_SMOOTH_INTERINTRA",
194  [AV1E_SET_ENABLE_REF_FRAME_MVS] = "AV1E_SET_ENABLE_REF_FRAME_MVS",
195 #endif
196 };
197 
198 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
199 {
200  AOMContext *ctx = avctx->priv_data;
201  const char *error = aom_codec_error(&ctx->encoder);
202  const char *detail = aom_codec_error_detail(&ctx->encoder);
203 
204  av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
205  if (detail)
206  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
207 }
208 
210  const struct aom_codec_enc_cfg *cfg)
211 {
212  int width = -30;
213  int level = AV_LOG_DEBUG;
214 
215  av_log(avctx, level, "aom_codec_enc_cfg\n");
216  av_log(avctx, level, "generic settings\n"
217  " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
218  " %*s%u\n %*s%u\n"
219  " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
220  width, "g_usage:", cfg->g_usage,
221  width, "g_threads:", cfg->g_threads,
222  width, "g_profile:", cfg->g_profile,
223  width, "g_w:", cfg->g_w,
224  width, "g_h:", cfg->g_h,
225  width, "g_bit_depth:", cfg->g_bit_depth,
226  width, "g_input_bit_depth:", cfg->g_input_bit_depth,
227  width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
228  width, "g_error_resilient:", cfg->g_error_resilient,
229  width, "g_pass:", cfg->g_pass,
230  width, "g_lag_in_frames:", cfg->g_lag_in_frames);
231  av_log(avctx, level, "rate control settings\n"
232  " %*s%u\n %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n",
233  width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
234  width, "rc_end_usage:", cfg->rc_end_usage,
235  width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
236  width, "rc_target_bitrate:", cfg->rc_target_bitrate);
237  av_log(avctx, level, "quantizer settings\n"
238  " %*s%u\n %*s%u\n",
239  width, "rc_min_quantizer:", cfg->rc_min_quantizer,
240  width, "rc_max_quantizer:", cfg->rc_max_quantizer);
241  av_log(avctx, level, "bitrate tolerance\n"
242  " %*s%u\n %*s%u\n",
243  width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
244  width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
245  av_log(avctx, level, "decoder buffer model\n"
246  " %*s%u\n %*s%u\n %*s%u\n",
247  width, "rc_buf_sz:", cfg->rc_buf_sz,
248  width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
249  width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
250  av_log(avctx, level, "2 pass rate control settings\n"
251  " %*s%u\n %*s%u\n %*s%u\n",
252  width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
253  width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
254  width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
255  av_log(avctx, level, "keyframing settings\n"
256  " %*s%d\n %*s%u\n %*s%u\n",
257  width, "kf_mode:", cfg->kf_mode,
258  width, "kf_min_dist:", cfg->kf_min_dist,
259  width, "kf_max_dist:", cfg->kf_max_dist);
260  av_log(avctx, level, "tile settings\n"
261  " %*s%d\n %*s%d\n",
262  width, "tile_width_count:", cfg->tile_width_count,
263  width, "tile_height_count:", cfg->tile_height_count);
264  av_log(avctx, level, "\n");
265 }
266 
267 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
268 {
269  struct FrameListData **p = list;
270 
271  while (*p)
272  p = &(*p)->next;
273  *p = cx_frame;
274  cx_frame->next = NULL;
275 }
276 
277 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
278 {
279  av_freep(&cx_frame->buf);
280  av_freep(&cx_frame);
281 }
282 
284 {
285  struct FrameListData *p = list;
286 
287  while (p) {
288  list = list->next;
289  free_coded_frame(p);
290  p = list;
291  }
292 }
293 
295 #ifdef UENUM1BYTE
296  aome_enc_control_id id,
297 #else
298  enum aome_enc_control_id id,
299 #endif
300  int val)
301 {
302  AOMContext *ctx = avctx->priv_data;
303  char buf[80];
304  int width = -30;
305  int res;
306 
307  snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
308  av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
309 
310  res = aom_codec_control(&ctx->encoder, id, val);
311  if (res != AOM_CODEC_OK) {
312  snprintf(buf, sizeof(buf), "Failed to set %s codec control",
313  ctlidstr[id]);
314  log_encoder_error(avctx, buf);
315  return AVERROR(EINVAL);
316  }
317 
318  return 0;
319 }
320 
321 static av_cold int aom_free(AVCodecContext *avctx)
322 {
323  AOMContext *ctx = avctx->priv_data;
324 
325  aom_codec_destroy(&ctx->encoder);
326  av_freep(&ctx->twopass_stats.buf);
327  av_freep(&avctx->stats_out);
329  av_bsf_free(&ctx->bsf);
330  return 0;
331 }
332 
333 static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps,
334  struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags,
335  aom_img_fmt_t *img_fmt)
336 {
337  AOMContext av_unused *ctx = avctx->priv_data;
339  enccfg->g_bit_depth = enccfg->g_input_bit_depth = desc->comp[0].depth;
340  switch (avctx->pix_fmt) {
341  case AV_PIX_FMT_YUV420P:
342  enccfg->g_profile = FF_PROFILE_AV1_MAIN;
343  *img_fmt = AOM_IMG_FMT_I420;
344  return 0;
345  case AV_PIX_FMT_YUV422P:
346  enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
347  *img_fmt = AOM_IMG_FMT_I422;
348  return 0;
349  case AV_PIX_FMT_YUV444P:
350  case AV_PIX_FMT_GBRP:
351  enccfg->g_profile = FF_PROFILE_AV1_HIGH;
352  *img_fmt = AOM_IMG_FMT_I444;
353  return 0;
356  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
357  enccfg->g_profile =
358  enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_MAIN : FF_PROFILE_AV1_PROFESSIONAL;
359  *img_fmt = AOM_IMG_FMT_I42016;
360  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
361  return 0;
362  }
363  break;
366  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
367  enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
368  *img_fmt = AOM_IMG_FMT_I42216;
369  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
370  return 0;
371  }
372  break;
375  case AV_PIX_FMT_GBRP10:
376  case AV_PIX_FMT_GBRP12:
377  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
378  enccfg->g_profile =
379  enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_HIGH : FF_PROFILE_AV1_PROFESSIONAL;
380  *img_fmt = AOM_IMG_FMT_I44416;
381  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
382  return 0;
383  }
384  break;
385  default:
386  break;
387  }
388  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
389  return AVERROR_INVALIDDATA;
390 }
391 
392 static void set_color_range(AVCodecContext *avctx)
393 {
394  aom_color_range_t aom_cr;
395  switch (avctx->color_range) {
397  case AVCOL_RANGE_MPEG: aom_cr = AOM_CR_STUDIO_RANGE; break;
398  case AVCOL_RANGE_JPEG: aom_cr = AOM_CR_FULL_RANGE; break;
399  default:
400  av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n",
401  avctx->color_range);
402  return;
403  }
404 
405  codecctl_int(avctx, AV1E_SET_COLOR_RANGE, aom_cr);
406 }
407 
408 static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
409 {
410  int sb_dim = (dim + sb_size - 1) / sb_size;
411  int tile_dim = (sb_dim + (1 << tiles_log2) - 1) >> tiles_log2;
412  av_assert0(tile_dim > 0);
413  return (sb_dim + tile_dim - 1) / tile_dim;
414 }
415 
416 static int choose_tiling(AVCodecContext *avctx,
417  struct aom_codec_enc_cfg *enccfg)
418 {
419  AOMContext *ctx = avctx->priv_data;
420  int sb_128x128_possible, sb_size, sb_width, sb_height;
421  int uniform_rows, uniform_cols;
422  int uniform_64x64_possible, uniform_128x128_possible;
423  int tile_size, rounding, i;
424 
425  if (ctx->tile_cols_log2 >= 0)
426  ctx->tile_cols = 1 << ctx->tile_cols_log2;
427  if (ctx->tile_rows_log2 >= 0)
428  ctx->tile_rows = 1 << ctx->tile_rows_log2;
429 
430  if (ctx->tile_cols == 0) {
431  ctx->tile_cols = (avctx->width + AV1_MAX_TILE_WIDTH - 1) /
433  if (ctx->tile_cols > 1) {
434  av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
435  "columns to fill width.\n", ctx->tile_cols);
436  }
437  }
438  av_assert0(ctx->tile_cols > 0);
439  if (ctx->tile_rows == 0) {
440  int max_tile_width =
441  FFALIGN((FFALIGN(avctx->width, 128) +
442  ctx->tile_cols - 1) / ctx->tile_cols, 128);
443  ctx->tile_rows =
444  (max_tile_width * FFALIGN(avctx->height, 128) +
446  if (ctx->tile_rows > 1) {
447  av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
448  "rows to fill area.\n", ctx->tile_rows);
449  }
450  }
451  av_assert0(ctx->tile_rows > 0);
452 
453  if ((avctx->width + 63) / 64 < ctx->tile_cols ||
454  (avctx->height + 63) / 64 < ctx->tile_rows) {
455  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: frame not "
456  "large enough to fit specified tile arrangement.\n");
457  return AVERROR(EINVAL);
458  }
459  if (ctx->tile_cols > AV1_MAX_TILE_COLS ||
460  ctx->tile_rows > AV1_MAX_TILE_ROWS) {
461  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
462  "not allow more than %dx%d tiles.\n",
464  return AVERROR(EINVAL);
465  }
466  if (avctx->width / ctx->tile_cols > AV1_MAX_TILE_WIDTH) {
467  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
468  "not allow tiles of width greater than %d.\n",
470  return AVERROR(EINVAL);
471  }
472 
473  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC;
474 
475  if (ctx->tile_cols == 1 && ctx->tile_rows == 1) {
476  av_log(avctx, AV_LOG_DEBUG, "Using a single tile.\n");
477  return 0;
478  }
479 
480  sb_128x128_possible =
481  (avctx->width + 127) / 128 >= ctx->tile_cols &&
482  (avctx->height + 127) / 128 >= ctx->tile_rows;
483 
484  ctx->tile_cols_log2 = ctx->tile_cols == 1 ? 0 :
485  av_log2(ctx->tile_cols - 1) + 1;
486  ctx->tile_rows_log2 = ctx->tile_rows == 1 ? 0 :
487  av_log2(ctx->tile_rows - 1) + 1;
488 
489  uniform_cols = count_uniform_tiling(avctx->width,
490  64, ctx->tile_cols_log2);
491  uniform_rows = count_uniform_tiling(avctx->height,
492  64, ctx->tile_rows_log2);
493  av_log(avctx, AV_LOG_DEBUG, "Uniform with 64x64 superblocks "
494  "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
495  uniform_64x64_possible = uniform_cols == ctx->tile_cols &&
496  uniform_rows == ctx->tile_rows;
497 
498  if (sb_128x128_possible) {
499  uniform_cols = count_uniform_tiling(avctx->width,
500  128, ctx->tile_cols_log2);
501  uniform_rows = count_uniform_tiling(avctx->height,
502  128, ctx->tile_rows_log2);
503  av_log(avctx, AV_LOG_DEBUG, "Uniform with 128x128 superblocks "
504  "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
505  uniform_128x128_possible = uniform_cols == ctx->tile_cols &&
506  uniform_rows == ctx->tile_rows;
507  } else {
508  av_log(avctx, AV_LOG_DEBUG, "128x128 superblocks not possible.\n");
509  uniform_128x128_possible = 0;
510  }
511 
512  ctx->uniform_tiles = 1;
513  if (uniform_64x64_possible && uniform_128x128_possible) {
514  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with dynamic "
515  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
516  ctx->tile_cols_log2, ctx->tile_rows_log2);
517  return 0;
518  }
519  if (uniform_64x64_possible && !sb_128x128_possible) {
520  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 64x64 "
521  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
522  ctx->tile_cols_log2, ctx->tile_rows_log2);
523  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
524  return 0;
525  }
526  if (uniform_128x128_possible) {
527  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 128x128 "
528  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
529  ctx->tile_cols_log2, ctx->tile_rows_log2);
530  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
531  return 0;
532  }
533  ctx->uniform_tiles = 0;
534 
535  if (sb_128x128_possible) {
536  sb_size = 128;
537  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
538  } else {
539  sb_size = 64;
540  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
541  }
542  av_log(avctx, AV_LOG_DEBUG, "Using fixed tiling with %dx%d "
543  "superblocks (tile_cols = %d, tile_rows = %d).\n",
544  sb_size, sb_size, ctx->tile_cols, ctx->tile_rows);
545 
546  enccfg->tile_width_count = ctx->tile_cols;
547  enccfg->tile_height_count = ctx->tile_rows;
548 
549  sb_width = (avctx->width + sb_size - 1) / sb_size;
550  sb_height = (avctx->height + sb_size - 1) / sb_size;
551 
552  tile_size = sb_width / ctx->tile_cols;
553  rounding = sb_width % ctx->tile_cols;
554  for (i = 0; i < ctx->tile_cols; i++) {
555  enccfg->tile_widths[i] = tile_size +
556  (i < rounding / 2 ||
557  i > ctx->tile_cols - 1 - (rounding + 1) / 2);
558  }
559 
560  tile_size = sb_height / ctx->tile_rows;
561  rounding = sb_height % ctx->tile_rows;
562  for (i = 0; i < ctx->tile_rows; i++) {
563  enccfg->tile_heights[i] = tile_size +
564  (i < rounding / 2 ||
565  i > ctx->tile_rows - 1 - (rounding + 1) / 2);
566  }
567 
568  return 0;
569 }
570 
571 static av_cold int aom_init(AVCodecContext *avctx,
572  const struct aom_codec_iface *iface)
573 {
574  AOMContext *ctx = avctx->priv_data;
576  struct aom_codec_enc_cfg enccfg = { 0 };
577 #ifdef AOM_FRAME_IS_INTRAONLY
578  aom_codec_flags_t flags =
579  (avctx->flags & AV_CODEC_FLAG_PSNR) ? AOM_CODEC_USE_PSNR : 0;
580 #else
581  aom_codec_flags_t flags = 0;
582 #endif
583  AVCPBProperties *cpb_props;
584  int res;
585  aom_img_fmt_t img_fmt;
586  aom_codec_caps_t codec_caps = aom_codec_get_caps(iface);
587 
588  av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
589  av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
590 
591  if ((res = aom_codec_enc_config_default(iface, &enccfg, 0)) != AOM_CODEC_OK) {
592  av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
593  aom_codec_err_to_string(res));
594  return AVERROR(EINVAL);
595  }
596 
597  if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
598  return AVERROR(EINVAL);
599 
600  if(!avctx->bit_rate)
601  if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
602  av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
603  return AVERROR(EINVAL);
604  }
605 
606  dump_enc_cfg(avctx, &enccfg);
607 
608  enccfg.g_w = avctx->width;
609  enccfg.g_h = avctx->height;
610  enccfg.g_timebase.num = avctx->time_base.num;
611  enccfg.g_timebase.den = avctx->time_base.den;
612  enccfg.g_threads =
613  FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 64);
614 
615  enccfg.g_usage = ctx->usage;
616 
617  if (ctx->lag_in_frames >= 0)
618  enccfg.g_lag_in_frames = ctx->lag_in_frames;
619 
620  if (avctx->flags & AV_CODEC_FLAG_PASS1)
621  enccfg.g_pass = AOM_RC_FIRST_PASS;
622  else if (avctx->flags & AV_CODEC_FLAG_PASS2)
623  enccfg.g_pass = AOM_RC_LAST_PASS;
624  else
625  enccfg.g_pass = AOM_RC_ONE_PASS;
626 
627  if (avctx->rc_min_rate == avctx->rc_max_rate &&
628  avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
629  enccfg.rc_end_usage = AOM_CBR;
630  } else if (ctx->crf >= 0) {
631  enccfg.rc_end_usage = AOM_CQ;
632  if (!avctx->bit_rate)
633  enccfg.rc_end_usage = AOM_Q;
634  }
635 
636  if (avctx->bit_rate) {
637  enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
639  } else if (enccfg.rc_end_usage != AOM_Q) {
640  enccfg.rc_end_usage = AOM_Q;
641  ctx->crf = 32;
642  av_log(avctx, AV_LOG_WARNING,
643  "Neither bitrate nor constrained quality specified, using default CRF of %d\n",
644  ctx->crf);
645  }
646 
647  if (avctx->qmin >= 0)
648  enccfg.rc_min_quantizer = avctx->qmin;
649  if (avctx->qmax >= 0)
650  enccfg.rc_max_quantizer = avctx->qmax;
651 
652  if (enccfg.rc_end_usage == AOM_CQ || enccfg.rc_end_usage == AOM_Q) {
653  if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
654  av_log(avctx, AV_LOG_ERROR,
655  "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
656  ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
657  return AVERROR(EINVAL);
658  }
659  }
660 
661  enccfg.rc_dropframe_thresh = ctx->drop_threshold;
662 
663  // 0-100 (0 => CBR, 100 => VBR)
664  enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
665  if (ctx->minsection_pct >= 0)
666  enccfg.rc_2pass_vbr_minsection_pct = ctx->minsection_pct;
667  else if (avctx->bit_rate)
668  enccfg.rc_2pass_vbr_minsection_pct =
669  avctx->rc_min_rate * 100LL / avctx->bit_rate;
670  if (ctx->maxsection_pct >= 0)
671  enccfg.rc_2pass_vbr_maxsection_pct = ctx->maxsection_pct;
672  else if (avctx->rc_max_rate)
673  enccfg.rc_2pass_vbr_maxsection_pct =
674  avctx->rc_max_rate * 100LL / avctx->bit_rate;
675 
676  if (avctx->rc_buffer_size)
677  enccfg.rc_buf_sz =
678  avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
679  if (avctx->rc_initial_buffer_occupancy)
680  enccfg.rc_buf_initial_sz =
681  avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
682  enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
683 
684  if (ctx->rc_undershoot_pct >= 0)
685  enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct;
686  if (ctx->rc_overshoot_pct >= 0)
687  enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct;
688 
689  // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO
690  if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
691  enccfg.kf_min_dist = avctx->keyint_min;
692  if (avctx->gop_size >= 0)
693  enccfg.kf_max_dist = avctx->gop_size;
694 
695  if (enccfg.g_pass == AOM_RC_FIRST_PASS)
696  enccfg.g_lag_in_frames = 0;
697  else if (enccfg.g_pass == AOM_RC_LAST_PASS) {
698  int decode_size, ret;
699 
700  if (!avctx->stats_in) {
701  av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
702  return AVERROR_INVALIDDATA;
703  }
704 
705  ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
706  ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
707  if (ret < 0) {
708  av_log(avctx, AV_LOG_ERROR,
709  "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
710  ctx->twopass_stats.sz);
711  ctx->twopass_stats.sz = 0;
712  return ret;
713  }
714  decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
715  ctx->twopass_stats.sz);
716  if (decode_size < 0) {
717  av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
718  return AVERROR_INVALIDDATA;
719  }
720 
721  ctx->twopass_stats.sz = decode_size;
722  enccfg.rc_twopass_stats_in = ctx->twopass_stats;
723  }
724 
725  /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
726  * complexity playback on low powered devices at the expense of encode
727  * quality. */
728  if (avctx->profile != FF_PROFILE_UNKNOWN)
729  enccfg.g_profile = avctx->profile;
730 
731  enccfg.g_error_resilient = ctx->error_resilient;
732 
733  res = choose_tiling(avctx, &enccfg);
734  if (res < 0)
735  return res;
736 
737  dump_enc_cfg(avctx, &enccfg);
738  /* Construct Encoder Context */
739  res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
740  if (res != AOM_CODEC_OK) {
741  log_encoder_error(avctx, "Failed to initialize encoder");
742  return AVERROR(EINVAL);
743  }
744 
745  // codec control failures are currently treated only as warnings
746  av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n");
747  codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used);
748  if (ctx->auto_alt_ref >= 0)
749  codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
750  if (ctx->arnr_max_frames >= 0)
751  codecctl_int(avctx, AOME_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
752  if (ctx->arnr_strength >= 0)
753  codecctl_int(avctx, AOME_SET_ARNR_STRENGTH, ctx->arnr_strength);
754  if (ctx->enable_cdef >= 0)
755  codecctl_int(avctx, AV1E_SET_ENABLE_CDEF, ctx->enable_cdef);
756  if (ctx->enable_restoration >= 0)
757  codecctl_int(avctx, AV1E_SET_ENABLE_RESTORATION, ctx->enable_restoration);
758 #if AOM_ENCODER_ABI_VERSION >= 22
759  if (ctx->enable_rect_partitions >= 0)
760  codecctl_int(avctx, AV1E_SET_ENABLE_RECT_PARTITIONS, ctx->enable_rect_partitions);
761  if (ctx->enable_1to4_partitions >= 0)
762  codecctl_int(avctx, AV1E_SET_ENABLE_1TO4_PARTITIONS, ctx->enable_1to4_partitions);
763  if (ctx->enable_ab_partitions >= 0)
764  codecctl_int(avctx, AV1E_SET_ENABLE_AB_PARTITIONS, ctx->enable_ab_partitions);
765  if (ctx->enable_angle_delta >= 0)
766  codecctl_int(avctx, AV1E_SET_ENABLE_ANGLE_DELTA, ctx->enable_angle_delta);
767  if (ctx->enable_cfl_intra >= 0)
768  codecctl_int(avctx, AV1E_SET_ENABLE_CFL_INTRA, ctx->enable_cfl_intra);
769  if (ctx->enable_filter_intra >= 0)
770  codecctl_int(avctx, AV1E_SET_ENABLE_FILTER_INTRA, ctx->enable_filter_intra);
771  if (ctx->enable_intra_edge_filter >= 0)
772  codecctl_int(avctx, AV1E_SET_ENABLE_INTRA_EDGE_FILTER, ctx->enable_intra_edge_filter);
773  if (ctx->enable_paeth_intra >= 0)
774  codecctl_int(avctx, AV1E_SET_ENABLE_PAETH_INTRA, ctx->enable_paeth_intra);
775  if (ctx->enable_smooth_intra >= 0)
776  codecctl_int(avctx, AV1E_SET_ENABLE_SMOOTH_INTRA, ctx->enable_smooth_intra);
777  if (ctx->enable_palette >= 0)
778  codecctl_int(avctx, AV1E_SET_ENABLE_PALETTE, ctx->enable_palette);
779  if (ctx->enable_tx64 >= 0)
780  codecctl_int(avctx, AV1E_SET_ENABLE_TX64, ctx->enable_tx64);
781  if (ctx->enable_flip_idtx >= 0)
782  codecctl_int(avctx, AV1E_SET_ENABLE_FLIP_IDTX, ctx->enable_flip_idtx);
783  if (ctx->use_intra_dct_only >= 0)
784  codecctl_int(avctx, AV1E_SET_INTRA_DCT_ONLY, ctx->use_intra_dct_only);
785  if (ctx->use_inter_dct_only >= 0)
786  codecctl_int(avctx, AV1E_SET_INTER_DCT_ONLY, ctx->use_inter_dct_only);
787  if (ctx->use_intra_default_tx_only >= 0)
788  codecctl_int(avctx, AV1E_SET_INTRA_DEFAULT_TX_ONLY, ctx->use_intra_default_tx_only);
789  if (ctx->reduced_tx_type_set >= 0)
790  codecctl_int(avctx, AV1E_SET_REDUCED_TX_TYPE_SET, ctx->reduced_tx_type_set);
791  if (ctx->enable_ref_frame_mvs >= 0)
792  codecctl_int(avctx, AV1E_SET_ENABLE_REF_FRAME_MVS, ctx->enable_ref_frame_mvs);
793  if (ctx->enable_reduced_reference_set >= 0)
794  codecctl_int(avctx, AV1E_SET_REDUCED_REFERENCE_SET, ctx->enable_reduced_reference_set);
795  if (ctx->enable_diff_wtd_comp >= 0)
796  codecctl_int(avctx, AV1E_SET_ENABLE_DIFF_WTD_COMP, ctx->enable_diff_wtd_comp);
797  if (ctx->enable_dist_wtd_comp >= 0)
798  codecctl_int(avctx, AV1E_SET_ENABLE_DIST_WTD_COMP, ctx->enable_dist_wtd_comp);
799  if (ctx->enable_dual_filter >= 0)
800  codecctl_int(avctx, AV1E_SET_ENABLE_DUAL_FILTER, ctx->enable_dual_filter);
801  if (ctx->enable_interinter_wedge >= 0)
802  codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTER_WEDGE, ctx->enable_interinter_wedge);
803  if (ctx->enable_masked_comp >= 0)
804  codecctl_int(avctx, AV1E_SET_ENABLE_MASKED_COMP, ctx->enable_masked_comp);
805  if (ctx->enable_interintra_comp >= 0)
806  codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTRA_COMP, ctx->enable_interintra_comp);
807  if (ctx->enable_interintra_wedge >= 0)
808  codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTRA_WEDGE, ctx->enable_interintra_wedge);
809  if (ctx->enable_obmc >= 0)
810  codecctl_int(avctx, AV1E_SET_ENABLE_OBMC, ctx->enable_obmc);
811  if (ctx->enable_onesided_comp >= 0)
812  codecctl_int(avctx, AV1E_SET_ENABLE_ONESIDED_COMP, ctx->enable_onesided_comp);
813  if (ctx->enable_smooth_interintra >= 0)
814  codecctl_int(avctx, AV1E_SET_ENABLE_SMOOTH_INTERINTRA, ctx->enable_smooth_interintra);
815 #endif
816 
817  codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh);
818  if (ctx->crf >= 0)
819  codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf);
820  if (ctx->tune >= 0)
821  codecctl_int(avctx, AOME_SET_TUNING, ctx->tune);
822 
823  if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
824  codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, AVCOL_PRI_BT709);
825  codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, AVCOL_SPC_RGB);
826  codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, AVCOL_TRC_IEC61966_2_1);
827  } else {
828  codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries);
829  codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace);
830  codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc);
831  }
832  if (ctx->aq_mode >= 0)
833  codecctl_int(avctx, AV1E_SET_AQ_MODE, ctx->aq_mode);
834  if (ctx->frame_parallel >= 0)
835  codecctl_int(avctx, AV1E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
836  set_color_range(avctx);
837 
838  codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size);
839  if (ctx->uniform_tiles) {
840  codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2);
841  codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2);
842  }
843 
844 #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL
845  if (ctx->denoise_noise_level >= 0)
846  codecctl_int(avctx, AV1E_SET_DENOISE_NOISE_LEVEL, ctx->denoise_noise_level);
847 #endif
848 #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE
849  if (ctx->denoise_block_size >= 0)
850  codecctl_int(avctx, AV1E_SET_DENOISE_BLOCK_SIZE, ctx->denoise_block_size);
851 #endif
852 #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION
853  if (ctx->enable_global_motion >= 0)
854  codecctl_int(avctx, AV1E_SET_ENABLE_GLOBAL_MOTION, ctx->enable_global_motion);
855 #endif
856 #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES
857  if (avctx->refs >= 3) {
858  codecctl_int(avctx, AV1E_SET_MAX_REFERENCE_FRAMES, avctx->refs);
859  }
860 #endif
861 #ifdef AOM_CTRL_AV1E_SET_ROW_MT
862  if (ctx->row_mt >= 0)
863  codecctl_int(avctx, AV1E_SET_ROW_MT, ctx->row_mt);
864 #endif
865 #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC
866  if (ctx->enable_intrabc >= 0)
867  codecctl_int(avctx, AV1E_SET_ENABLE_INTRABC, ctx->enable_intrabc);
868 #endif
869 
870  // provide dummy value to initialize wrapper, values will be updated each _encode()
871  aom_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
872  (unsigned char*)1);
873 
874  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
875  ctx->rawimg.bit_depth = enccfg.g_bit_depth;
876 
877  cpb_props = ff_add_cpb_side_data(avctx);
878  if (!cpb_props)
879  return AVERROR(ENOMEM);
880 
881  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
882  const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata");
883  int ret;
884 
885  if (!filter) {
886  av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter "
887  "not found. This is a bug, please report it.\n");
888  return AVERROR_BUG;
889  }
890  ret = av_bsf_alloc(filter, &ctx->bsf);
891  if (ret < 0)
892  return ret;
893 
894  ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx);
895  if (ret < 0)
896  return ret;
897 
898  ret = av_bsf_init(ctx->bsf);
899  if (ret < 0)
900  return ret;
901  }
902 
903  if (enccfg.rc_end_usage == AOM_CBR ||
904  enccfg.g_pass != AOM_RC_ONE_PASS) {
905  cpb_props->max_bitrate = avctx->rc_max_rate;
906  cpb_props->min_bitrate = avctx->rc_min_rate;
907  cpb_props->avg_bitrate = avctx->bit_rate;
908  }
909  cpb_props->buffer_size = avctx->rc_buffer_size;
910 
911  return 0;
912 }
913 
914 static inline void cx_pktcpy(AOMContext *ctx,
915  struct FrameListData *dst,
916  const struct aom_codec_cx_pkt *src)
917 {
918  dst->pts = src->data.frame.pts;
919  dst->duration = src->data.frame.duration;
920  dst->flags = src->data.frame.flags;
921  dst->sz = src->data.frame.sz;
922  dst->buf = src->data.frame.buf;
923 #ifdef AOM_FRAME_IS_INTRAONLY
924  dst->have_sse = 0;
925  dst->frame_number = ++ctx->frame_number;
926  dst->have_sse = ctx->have_sse;
927  if (ctx->have_sse) {
928  /* associate last-seen SSE to the frame. */
929  /* Transfers ownership from ctx to dst. */
930  memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
931  ctx->have_sse = 0;
932  }
933 #endif
934 }
935 
936 /**
937  * Store coded frame information in format suitable for return from encode2().
938  *
939  * Write information from @a cx_frame to @a pkt
940  * @return packet data size on success
941  * @return a negative AVERROR on error
942  */
943 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
944  AVPacket *pkt)
945 {
946  AOMContext *ctx = avctx->priv_data;
947  int av_unused pict_type;
948  int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0);
949  if (ret < 0) {
950  av_log(avctx, AV_LOG_ERROR,
951  "Error getting output packet of size %"SIZE_SPECIFIER".\n", cx_frame->sz);
952  return ret;
953  }
954  memcpy(pkt->data, cx_frame->buf, pkt->size);
955  pkt->pts = pkt->dts = cx_frame->pts;
956 
957  if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) {
958  pkt->flags |= AV_PKT_FLAG_KEY;
959 #ifdef AOM_FRAME_IS_INTRAONLY
960  pict_type = AV_PICTURE_TYPE_I;
961  } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) {
962  pict_type = AV_PICTURE_TYPE_I;
963  } else {
964  pict_type = AV_PICTURE_TYPE_P;
965  }
966 
967  ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1,
968  cx_frame->have_sse ? 3 : 0, pict_type);
969 
970  if (cx_frame->have_sse) {
971  int i;
972  for (i = 0; i < 3; ++i) {
973  avctx->error[i] += cx_frame->sse[i + 1];
974  }
975  cx_frame->have_sse = 0;
976 #endif
977  }
978 
979  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
980  ret = av_bsf_send_packet(ctx->bsf, pkt);
981  if (ret < 0) {
982  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
983  "failed to send input packet\n");
984  return ret;
985  }
986  ret = av_bsf_receive_packet(ctx->bsf, pkt);
987 
988  if (ret < 0) {
989  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
990  "failed to receive output packet\n");
991  return ret;
992  }
993  }
994  return pkt->size;
995 }
996 
997 /**
998  * Queue multiple output frames from the encoder, returning the front-most.
999  * In cases where aom_codec_get_cx_data() returns more than 1 frame append
1000  * the frame queue. Return the head frame if available.
1001  * @return Stored frame size
1002  * @return AVERROR(EINVAL) on output size error
1003  * @return AVERROR(ENOMEM) on coded frame queue data allocation error
1004  */
1005 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
1006 {
1007  AOMContext *ctx = avctx->priv_data;
1008  const struct aom_codec_cx_pkt *pkt;
1009  const void *iter = NULL;
1010  int size = 0;
1011 
1012  if (ctx->coded_frame_list) {
1013  struct FrameListData *cx_frame = ctx->coded_frame_list;
1014  /* return the leading frame if we've already begun queueing */
1015  size = storeframe(avctx, cx_frame, pkt_out);
1016  if (size < 0)
1017  return size;
1018  ctx->coded_frame_list = cx_frame->next;
1019  free_coded_frame(cx_frame);
1020  }
1021 
1022  /* consume all available output from the encoder before returning. buffers
1023  * are only good through the next aom_codec call */
1024  while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) {
1025  switch (pkt->kind) {
1026  case AOM_CODEC_CX_FRAME_PKT:
1027  if (!size) {
1028  struct FrameListData cx_frame;
1029 
1030  /* avoid storing the frame when the list is empty and we haven't yet
1031  * provided a frame for output */
1033  cx_pktcpy(ctx, &cx_frame, pkt);
1034  size = storeframe(avctx, &cx_frame, pkt_out);
1035  if (size < 0)
1036  return size;
1037  } else {
1038  struct FrameListData *cx_frame =
1039  av_malloc(sizeof(struct FrameListData));
1040 
1041  if (!cx_frame) {
1042  av_log(avctx, AV_LOG_ERROR,
1043  "Frame queue element alloc failed\n");
1044  return AVERROR(ENOMEM);
1045  }
1046  cx_pktcpy(ctx, cx_frame, pkt);
1047  cx_frame->buf = av_malloc(cx_frame->sz);
1048 
1049  if (!cx_frame->buf) {
1050  av_log(avctx, AV_LOG_ERROR,
1051  "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
1052  cx_frame->sz);
1053  av_freep(&cx_frame);
1054  return AVERROR(ENOMEM);
1055  }
1056  memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
1057  coded_frame_add(&ctx->coded_frame_list, cx_frame);
1058  }
1059  break;
1060  case AOM_CODEC_STATS_PKT:
1061  {
1062  struct aom_fixed_buf *stats = &ctx->twopass_stats;
1063  int err;
1064  if ((err = av_reallocp(&stats->buf,
1065  stats->sz +
1066  pkt->data.twopass_stats.sz)) < 0) {
1067  stats->sz = 0;
1068  av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
1069  return err;
1070  }
1071  memcpy((uint8_t *)stats->buf + stats->sz,
1072  pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
1073  stats->sz += pkt->data.twopass_stats.sz;
1074  break;
1075  }
1076 #ifdef AOM_FRAME_IS_INTRAONLY
1077  case AOM_CODEC_PSNR_PKT:
1078  {
1079  av_assert0(!ctx->have_sse);
1080  ctx->sse[0] = pkt->data.psnr.sse[0];
1081  ctx->sse[1] = pkt->data.psnr.sse[1];
1082  ctx->sse[2] = pkt->data.psnr.sse[2];
1083  ctx->sse[3] = pkt->data.psnr.sse[3];
1084  ctx->have_sse = 1;
1085  break;
1086  }
1087 #endif
1088  case AOM_CODEC_CUSTOM_PKT:
1089  // ignore unsupported/unrecognized packet types
1090  break;
1091  }
1092  }
1093 
1094  return size;
1095 }
1096 
1098  const AVFrame *frame, int *got_packet)
1099 {
1100  AOMContext *ctx = avctx->priv_data;
1101  struct aom_image *rawimg = NULL;
1102  int64_t timestamp = 0;
1103  int res, coded_size;
1104  aom_enc_frame_flags_t flags = 0;
1105 
1106  if (frame) {
1107  rawimg = &ctx->rawimg;
1108  rawimg->planes[AOM_PLANE_Y] = frame->data[0];
1109  rawimg->planes[AOM_PLANE_U] = frame->data[1];
1110  rawimg->planes[AOM_PLANE_V] = frame->data[2];
1111  rawimg->stride[AOM_PLANE_Y] = frame->linesize[0];
1112  rawimg->stride[AOM_PLANE_U] = frame->linesize[1];
1113  rawimg->stride[AOM_PLANE_V] = frame->linesize[2];
1114  timestamp = frame->pts;
1115  switch (frame->color_range) {
1116  case AVCOL_RANGE_MPEG:
1117  rawimg->range = AOM_CR_STUDIO_RANGE;
1118  break;
1119  case AVCOL_RANGE_JPEG:
1120  rawimg->range = AOM_CR_FULL_RANGE;
1121  break;
1122  }
1123 
1124  if (frame->pict_type == AV_PICTURE_TYPE_I)
1125  flags |= AOM_EFLAG_FORCE_KF;
1126  }
1127 
1128  res = aom_codec_encode(&ctx->encoder, rawimg, timestamp,
1129  avctx->ticks_per_frame, flags);
1130  if (res != AOM_CODEC_OK) {
1131  log_encoder_error(avctx, "Error encoding frame");
1132  return AVERROR_INVALIDDATA;
1133  }
1134  coded_size = queue_frames(avctx, pkt);
1135 
1136  if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
1137  size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
1138 
1139  avctx->stats_out = av_malloc(b64_size);
1140  if (!avctx->stats_out) {
1141  av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
1142  b64_size);
1143  return AVERROR(ENOMEM);
1144  }
1145  av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
1146  ctx->twopass_stats.sz);
1147  }
1148 
1149  *got_packet = !!coded_size;
1150  return 0;
1151 }
1152 
1153 static const enum AVPixelFormat av1_pix_fmts[] = {
1159 };
1160 
1161 static const enum AVPixelFormat av1_pix_fmts_highbd[] = {
1175 };
1176 
1177 static av_cold void av1_init_static(AVCodec *codec)
1178 {
1179  aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx());
1180  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
1181  codec->pix_fmts = av1_pix_fmts_highbd;
1182  else
1183  codec->pix_fmts = av1_pix_fmts;
1184 
1185  if (aom_codec_version_major() < 2)
1187 }
1188 
1189 static av_cold int av1_init(AVCodecContext *avctx)
1190 {
1191  return aom_init(avctx, aom_codec_av1_cx());
1192 }
1193 
1194 #define OFFSET(x) offsetof(AOMContext, x)
1195 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1196 static const AVOption options[] = {
1197  { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VE},
1198  { "auto-alt-ref", "Enable use of alternate reference "
1199  "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
1200  { "lag-in-frames", "Number of frames to look ahead at for "
1201  "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1202  { "arnr-max-frames", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1203  { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1204  { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 4, VE, "aq_mode"},
1205  { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode"},
1206  { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode"},
1207  { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode"},
1208  { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode"},
1209  { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
1210  { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
1211  { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE },
1212  { "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 },
1213  { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
1214  { "denoise-noise-level", "Amount of noise to be removed", OFFSET(denoise_noise_level), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1215  { "denoise-block-size", "Denoise block size ", OFFSET(denoise_block_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1216  { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
1217  { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1000, VE},
1218  { "minsection-pct", "GOP min bitrate (% of target)", OFFSET(minsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
1219  { "maxsection-pct", "GOP max bitrate (% of target)", OFFSET(maxsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 5000, VE},
1220  { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1221  { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE },
1222  { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1223  { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1224  { "row-mt", "Enable row based multi-threading", OFFSET(row_mt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1225  { "enable-cdef", "Enable CDEF filtering", OFFSET(enable_cdef), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1226  { "enable-global-motion", "Enable global motion", OFFSET(enable_global_motion), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1227  { "enable-intrabc", "Enable intra block copy prediction mode", OFFSET(enable_intrabc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1228  { "enable-restoration", "Enable Loop Restoration filtering", OFFSET(enable_restoration), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1229  { "usage", "Quality and compression efficiency vs speed trade-off", OFFSET(usage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "usage"},
1230  { "good", "Good quality", 0, AV_OPT_TYPE_CONST, {.i64 = 0 /* AOM_USAGE_GOOD_QUALITY */}, 0, 0, VE, "usage"},
1231  { "realtime", "Realtime encoding", 0, AV_OPT_TYPE_CONST, {.i64 = 1 /* AOM_USAGE_REALTIME */}, 0, 0, VE, "usage"},
1232  { "tune", "The metric that the encoder tunes for. Automatically chosen by the encoder by default", OFFSET(tune), AV_OPT_TYPE_INT, {.i64 = -1}, -1, AOM_TUNE_SSIM, VE, "tune"},
1233  { "psnr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_PSNR}, 0, 0, VE, "tune"},
1234  { "ssim", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_SSIM}, 0, 0, VE, "tune"},
1236  { "enable-rect-partitions", "Enable rectangular partitions", OFFSET(enable_rect_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1237  { "enable-1to4-partitions", "Enable 1:4/4:1 partitions", OFFSET(enable_1to4_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1238  { "enable-ab-partitions", "Enable ab shape partitions", OFFSET(enable_ab_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1239  { "enable-angle-delta", "Enable angle delta intra prediction", OFFSET(enable_angle_delta), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1240  { "enable-cfl-intra", "Enable chroma predicted from luma intra prediction", OFFSET(enable_cfl_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1241  { "enable-filter-intra", "Enable filter intra predictor", OFFSET(enable_filter_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1242  { "enable-intra-edge-filter", "Enable intra edge filter", OFFSET(enable_intra_edge_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1243  { "enable-smooth-intra", "Enable smooth intra prediction mode", OFFSET(enable_smooth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1244  { "enable-paeth-intra", "Enable paeth predictor in intra prediction", OFFSET(enable_paeth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1245  { "enable-palette", "Enable palette prediction mode", OFFSET(enable_palette), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1246  { "enable-flip-idtx", "Enable extended transform type", OFFSET(enable_flip_idtx), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1247  { "enable-tx64", "Enable 64-pt transform", OFFSET(enable_tx64), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1248  { "reduced-tx-type-set", "Use reduced set of transform types", OFFSET(reduced_tx_type_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1249  { "use-intra-dct-only", "Use DCT only for INTRA modes", OFFSET(use_intra_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1250  { "use-inter-dct-only", "Use DCT only for INTER modes", OFFSET(use_inter_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1251  { "use-intra-default-tx-only", "Use default-transform only for INTRA modes", OFFSET(use_intra_default_tx_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1252  { "enable-ref-frame-mvs", "Enable temporal mv prediction", OFFSET(enable_ref_frame_mvs), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1253  { "enable-reduced-reference-set", "Use reduced set of single and compound references", OFFSET(enable_reduced_reference_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1254  { "enable-obmc", "Enable obmc", OFFSET(enable_obmc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1255  { "enable-dual-filter", "Enable dual filter", OFFSET(enable_dual_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1256  { "enable-diff-wtd-comp", "Enable difference-weighted compound", OFFSET(enable_diff_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1257  { "enable-dist-wtd-comp", "Enable distance-weighted compound", OFFSET(enable_dist_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1258  { "enable-onesided-comp", "Enable one sided compound", OFFSET(enable_onesided_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1259  { "enable-interinter-wedge", "Enable interinter wedge compound", OFFSET(enable_interinter_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1260  { "enable-interintra-wedge", "Enable interintra wedge compound", OFFSET(enable_interintra_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1261  { "enable-masked-comp", "Enable masked compound", OFFSET(enable_masked_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1262  { "enable-interintra-comp", "Enable interintra compound", OFFSET(enable_interintra_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1263  { "enable-smooth-interintra", "Enable smooth interintra mode", OFFSET(enable_smooth_interintra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1264  { NULL },
1265 };
1266 
1267 static const AVCodecDefault defaults[] = {
1268  { "b", "0" },
1269  { "qmin", "-1" },
1270  { "qmax", "-1" },
1271  { "g", "-1" },
1272  { "keyint_min", "-1" },
1273  { NULL },
1274 };
1275 
1276 static const AVClass class_aom = {
1277  .class_name = "libaom-av1 encoder",
1278  .item_name = av_default_item_name,
1279  .option = options,
1280  .version = LIBAVUTIL_VERSION_INT,
1281 };
1282 
1284  .name = "libaom-av1",
1285  .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
1286  .type = AVMEDIA_TYPE_VIDEO,
1287  .id = AV_CODEC_ID_AV1,
1288  .priv_data_size = sizeof(AOMContext),
1289  .init = av1_init,
1290  .encode2 = aom_encode,
1291  .close = aom_free,
1294  .priv_class = &class_aom,
1295  .defaults = defaults,
1296  .init_static_data = av1_init_static,
1297  .wrapper_name = "libaom",
1298 };
#define OFFSET(x)
Definition: libaomenc.c:1194
int denoise_block_size
Definition: libaomenc.c:79
#define NULL
Definition: coverity.c:32
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
ptrdiff_t const GLvoid GLenum usage
Definition: opengl_enc.c:100
uint64_t sse[4]
Definition: libaomenc.c:80
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int minsection_pct
Definition: libaomenc.c:85
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
int enable_cdef
Definition: libaomenc.c:93
AVOption.
Definition: opt.h:248
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:729
int av_cpu_count(void)
Definition: cpu.c:275
int enable_paeth_intra
Definition: libaomenc.c:104
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition: avcodec.h:1709
int use_intra_default_tx_only
Definition: libaomenc.c:114
int enable_onesided_comp
Definition: libaomenc.c:121
const char * desc
Definition: libsvtav1.c:79
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:454
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:1432
int enable_smooth_interintra
Definition: libaomenc.c:123
static av_cold int aom_init(AVCodecContext *avctx, const struct aom_codec_iface *iface)
Definition: libaomenc.c:571
int enable_palette
Definition: libaomenc.c:107
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1161
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: bsf.h:49
int size
Definition: packet.h:364
void * buf
compressed data buffer
Definition: libaomenc.c:48
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int av_log2(unsigned v)
Definition: intmath.c:26
int maxsection_pct
Definition: libaomenc.c:86
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
size_t sz
length of compressed data
Definition: libaomenc.c:49
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: codec.h:98
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1553
int tile_cols_log2
Definition: libaomenc.c:89
uint64_t frame_number
Definition: libaomenc.c:82
static AVPacket pkt
static const AVOption options[]
Definition: libaomenc.c:1196
#define FF_AV1_PROFILE_OPTS
Definition: profiles.h:54
static void error(const char *err)
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: codec.h:118
int profile
profile
Definition: avcodec.h:1859
AVCodec.
Definition: codec.h:190
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:513
int min_bitrate
Minimum bitrate of the stream, in bits per second.
Definition: avcodec.h:463
int error_resilient
Definition: libaomenc.c:74
int use_inter_dct_only
Definition: libaomenc.c:113
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:649
int tile_rows_log2
Definition: libaomenc.c:89
static int choose_tiling(AVCodecContext *avctx, struct aom_codec_enc_cfg *enccfg)
Definition: libaomenc.c:416
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: codec.h:75
#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:33
static av_cold void free_frame_list(struct FrameListData *list)
Definition: libaomenc.c:283
int enable_dist_wtd_comp
Definition: libaomenc.c:125
int enable_reduced_reference_set
Definition: libaomenc.c:122
int enable_interintra_wedge
Definition: libaomenc.c:117
struct FrameListData * next
Definition: libaomenc.c:58
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
struct aom_fixed_buf twopass_stats
Definition: libaomenc.c:66
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
int64_t pts
time stamp to show frame (in timebase units)
Definition: libaomenc.c:50
int arnr_strength
Definition: libaomenc.c:71
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:401
int usage
Definition: libaomenc.c:97
int enable_dual_filter
Definition: libaomenc.c:126
struct aom_image rawimg
Definition: libaomenc.c:65
uint8_t * data
Definition: packet.h:363
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
static av_cold int codecctl_int(AVCodecContext *avctx, enum aome_enc_control_id id, int val)
Definition: libaomenc.c:294
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:481
int enable_smooth_intra
Definition: libaomenc.c:105
ptrdiff_t size
Definition: opengl_enc.c:100
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1545
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int rc_overshoot_pct
Definition: libaomenc.c:84
int enable_intrabc
Definition: libaomenc.c:95
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:396
int enable_rect_partitions
Definition: libaomenc.c:99
static void cx_pktcpy(AOMContext *ctx, struct FrameListData *dst, const struct aom_codec_cx_pkt *src)
Definition: libaomenc.c:914
#define VE
Definition: libaomenc.c:1195
#define src
Definition: vp8dsp.c:254
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:94
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
static av_cold void av1_init_static(AVCodec *codec)
Definition: libaomenc.c:1177
int tile_rows
Definition: libaomenc.c:88
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
int qmax
maximum quantizer
Definition: avcodec.h:1375
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
int enable_ref_frame_mvs
Definition: libaomenc.c:115
int aq_mode
Definition: libaomenc.c:72
int enable_flip_idtx
Definition: libaomenc.c:109
int capabilities
Codec capabilities.
Definition: codec.h:209
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:552
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
Round to nearest and halfway cases away from zero.
Definition: mathematics.h:84
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:460
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: codec.h:197
int enable_1to4_partitions
Definition: libaomenc.c:100
static av_always_inline av_const double round(double x)
Definition: libm.h:444
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:138
static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt)
Store coded frame information in format suitable for return from encode2().
Definition: libaomenc.c:943
static void set_color_range(AVCodecContext *avctx)
Definition: libaomenc.c:392
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:369
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:147
int enable_diff_wtd_comp
Definition: libaomenc.c:124
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:1389
static const AVClass class_aom
Definition: libaomenc.c:1276
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:1411
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
AVCodec ff_libaom_av1_encoder
Definition: libaomenc.c:1283
int refs
number of reference frames
Definition: avcodec.h:1114
struct FrameListData * coded_frame_list
Definition: libaomenc.c:67
static enum AVPixelFormat av1_pix_fmts_highbd[]
Definition: libaomenc.c:1161
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:211
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:391
#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:1005
#define FFMIN(a, b)
Definition: common.h:96
uint64_t sse[4]
Definition: libaomenc.c:55
#define width
int width
picture width / height.
Definition: avcodec.h:699
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1860
static av_cold void free_coded_frame(struct FrameListData *cx_frame)
Definition: libaomenc.c:277
int enable_ab_partitions
Definition: libaomenc.c:101
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:312
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:296
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1140
static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
Definition: libaomenc.c:408
static void stats(AVPacket *const *in, int n_in, unsigned *_max, unsigned *_sum)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
int enable_interintra_comp
Definition: libaomenc.c:118
int row_mt
Definition: libaomenc.c:92
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:658
static const char *const ctlidstr[]
Definition: libaomenc.c:129
int enable_filter_intra
Definition: libaomenc.c:108
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
int cpu_used
Definition: libaomenc.c:68
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:1785
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:538
int have_sse
true if we have pending sse[]
Definition: libaomenc.c:81
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:333
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:448
struct aom_codec_ctx encoder
Definition: libaomenc.c:64
int enable_global_motion
Definition: libaomenc.c:94
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:200
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:161
Libavcodec external API header.
aom_superblock_size_t superblock_size
Definition: libaomenc.c:90
int enable_obmc
Definition: libaomenc.c:120
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:339
const AVProfile ff_av1_profiles[]
Definition: profiles.c:141
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int enable_masked_comp
Definition: libaomenc.c:119
main external API structure.
Definition: avcodec.h:526
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
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:226
static enum AVPixelFormat av1_pix_fmts[]
Definition: libaomenc.c:1153
int qmin
minimum quantizer
Definition: avcodec.h:1368
AV1 common definitions.
int frame_parallel
Definition: libaomenc.c:87
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
static av_cold int av1_init(AVCodecContext *avctx)
Definition: libaomenc.c:1189
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
static void coded_frame_add(void *list, struct FrameListData *cx_frame)
Definition: libaomenc.c:267
Describe the class of an AVClass context structure.
Definition: log.h:67
int enable_cfl_intra
Definition: libaomenc.c:103
static const AVProfile profiles[]
#define FF_PROFILE_AV1_PROFESSIONAL
Definition: avcodec.h:1954
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1154
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1147
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:2093
int uniform_tiles
Definition: libaomenc.c:91
#define FF_PROFILE_AV1_MAIN
Definition: avcodec.h:1952
uint32_t flags
flags for this frame
Definition: libaomenc.c:54
int dim
int enable_angle_delta
Definition: libaomenc.c:102
#define snprintf
Definition: snprintf.h:34
uint64_t frame_number
Definition: libaomenc.c:57
offset must point to two consecutive integers
Definition: opt.h:235
int static_thresh
Definition: libaomenc.c:76
int reduced_tx_type_set
Definition: libaomenc.c:111
int tune
Definition: libaomenc.c:98
static av_cold void dump_enc_cfg(AVCodecContext *avctx, const struct aom_codec_enc_cfg *cfg)
Definition: libaomenc.c:209
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:1360
int enable_intra_edge_filter
Definition: libaomenc.c:106
int drop_threshold
Definition: libaomenc.c:77
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
int have_sse
true if we have pending sse[]
Definition: libaomenc.c:56
#define SIZE_SPECIFIER
Definition: internal.h:229
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
uint8_t level
Definition: svq3.c:209
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:329
AVBSFContext * bsf
Definition: libaomenc.c:63
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:537
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:721
int denoise_noise_level
Definition: libaomenc.c:78
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:497
common internal api header.
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
Definition: libaomenc.c:198
common internal and external API header
int auto_alt_ref
Definition: libaomenc.c:69
static int aom_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libaomenc.c:1097
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:2003
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:300
void * priv_data
Definition: avcodec.h:553
int enable_restoration
Definition: libaomenc.c:96
int tile_cols
Definition: libaomenc.c:88
Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
Definition: libaomenc.c:47
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:472
int use_intra_dct_only
Definition: libaomenc.c:112
int arnr_max_frames
Definition: libaomenc.c:70
unsigned long duration
duration to show frame (in timebase units)
Definition: libaomenc.c:52
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition: base64.c:79
int enable_interinter_wedge
Definition: libaomenc.c:116
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:362
#define av_freep(p)
static const AVCodecDefault defaults[]
Definition: libaomenc.c:1267
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:40
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int enable_tx64
Definition: libaomenc.c:110
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:340
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77
int rc_undershoot_pct
Definition: libaomenc.c:83
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
static av_cold int aom_free(AVCodecContext *avctx)
Definition: libaomenc.c:321
int i
Definition: input.c:407
Predicted.
Definition: avutil.h:275
int lag_in_frames
Definition: libaomenc.c:73
#define av_unused
Definition: attributes.h:131
#define FF_PROFILE_AV1_HIGH
Definition: avcodec.h:1953
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1404
int keyint_min
minimum GOP size
Definition: avcodec.h:1107