FFmpeg  4.1.11
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 crf;
75  uint64_t sse[4];
76  int have_sse; /**< true if we have pending sse[] */
77  uint64_t frame_number;
78  int tile_cols, tile_rows;
79  int tile_cols_log2, tile_rows_log2;
80  aom_superblock_size_t superblock_size;
82 } AOMContext;
83 
84 static const char *const ctlidstr[] = {
85  [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED",
86  [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL",
87  [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF",
88  [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD",
89  [AV1E_SET_COLOR_RANGE] = "AV1E_SET_COLOR_RANGE",
90  [AV1E_SET_COLOR_PRIMARIES] = "AV1E_SET_COLOR_PRIMARIES",
91  [AV1E_SET_MATRIX_COEFFICIENTS] = "AV1E_SET_MATRIX_COEFFICIENTS",
92  [AV1E_SET_TRANSFER_CHARACTERISTICS] = "AV1E_SET_TRANSFER_CHARACTERISTICS",
93  [AV1E_SET_SUPERBLOCK_SIZE] = "AV1E_SET_SUPERBLOCK_SIZE",
94  [AV1E_SET_TILE_COLUMNS] = "AV1E_SET_TILE_COLUMNS",
95  [AV1E_SET_TILE_ROWS] = "AV1E_SET_TILE_ROWS",
96 };
97 
98 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
99 {
100  AOMContext *ctx = avctx->priv_data;
101  const char *error = aom_codec_error(&ctx->encoder);
102  const char *detail = aom_codec_error_detail(&ctx->encoder);
103 
104  av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
105  if (detail)
106  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
107 }
108 
110  const struct aom_codec_enc_cfg *cfg)
111 {
112  int width = -30;
113  int level = AV_LOG_DEBUG;
114 
115  av_log(avctx, level, "aom_codec_enc_cfg\n");
116  av_log(avctx, level, "generic settings\n"
117  " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
118  " %*s%u\n %*s%u\n"
119  " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
120  width, "g_usage:", cfg->g_usage,
121  width, "g_threads:", cfg->g_threads,
122  width, "g_profile:", cfg->g_profile,
123  width, "g_w:", cfg->g_w,
124  width, "g_h:", cfg->g_h,
125  width, "g_bit_depth:", cfg->g_bit_depth,
126  width, "g_input_bit_depth:", cfg->g_input_bit_depth,
127  width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
128  width, "g_error_resilient:", cfg->g_error_resilient,
129  width, "g_pass:", cfg->g_pass,
130  width, "g_lag_in_frames:", cfg->g_lag_in_frames);
131  av_log(avctx, level, "rate control settings\n"
132  " %*s%u\n %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n",
133  width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
134  width, "rc_end_usage:", cfg->rc_end_usage,
135  width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
136  width, "rc_target_bitrate:", cfg->rc_target_bitrate);
137  av_log(avctx, level, "quantizer settings\n"
138  " %*s%u\n %*s%u\n",
139  width, "rc_min_quantizer:", cfg->rc_min_quantizer,
140  width, "rc_max_quantizer:", cfg->rc_max_quantizer);
141  av_log(avctx, level, "bitrate tolerance\n"
142  " %*s%u\n %*s%u\n",
143  width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
144  width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
145  av_log(avctx, level, "decoder buffer model\n"
146  " %*s%u\n %*s%u\n %*s%u\n",
147  width, "rc_buf_sz:", cfg->rc_buf_sz,
148  width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
149  width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
150  av_log(avctx, level, "2 pass rate control settings\n"
151  " %*s%u\n %*s%u\n %*s%u\n",
152  width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
153  width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
154  width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
155  av_log(avctx, level, "keyframing settings\n"
156  " %*s%d\n %*s%u\n %*s%u\n",
157  width, "kf_mode:", cfg->kf_mode,
158  width, "kf_min_dist:", cfg->kf_min_dist,
159  width, "kf_max_dist:", cfg->kf_max_dist);
160  av_log(avctx, level, "tile settings\n"
161  " %*s%d\n %*s%d\n",
162  width, "tile_width_count:", cfg->tile_width_count,
163  width, "tile_height_count:", cfg->tile_height_count);
164  av_log(avctx, level, "\n");
165 }
166 
167 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
168 {
169  struct FrameListData **p = list;
170 
171  while (*p)
172  p = &(*p)->next;
173  *p = cx_frame;
174  cx_frame->next = NULL;
175 }
176 
177 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
178 {
179  av_freep(&cx_frame->buf);
180  av_freep(&cx_frame);
181 }
182 
183 static av_cold void free_frame_list(struct FrameListData *list)
184 {
185  struct FrameListData *p = list;
186 
187  while (p) {
188  list = list->next;
189  free_coded_frame(p);
190  p = list;
191  }
192 }
193 
195  enum aome_enc_control_id id, int val)
196 {
197  AOMContext *ctx = avctx->priv_data;
198  char buf[80];
199  int width = -30;
200  int res;
201 
202  snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
203  av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
204 
205  res = aom_codec_control(&ctx->encoder, id, val);
206  if (res != AOM_CODEC_OK) {
207  snprintf(buf, sizeof(buf), "Failed to set %s codec control",
208  ctlidstr[id]);
209  log_encoder_error(avctx, buf);
210  return AVERROR(EINVAL);
211  }
212 
213  return 0;
214 }
215 
216 static av_cold int aom_free(AVCodecContext *avctx)
217 {
218  AOMContext *ctx = avctx->priv_data;
219 
220  aom_codec_destroy(&ctx->encoder);
221  av_freep(&ctx->twopass_stats.buf);
222  av_freep(&avctx->stats_out);
224  av_bsf_free(&ctx->bsf);
225  return 0;
226 }
227 
228 static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps,
229  struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags,
230  aom_img_fmt_t *img_fmt)
231 {
232  AOMContext av_unused *ctx = avctx->priv_data;
233  enccfg->g_bit_depth = enccfg->g_input_bit_depth = 8;
234  switch (avctx->pix_fmt) {
235  case AV_PIX_FMT_YUV420P:
236  enccfg->g_profile = FF_PROFILE_AV1_MAIN;
237  *img_fmt = AOM_IMG_FMT_I420;
238  return 0;
239  case AV_PIX_FMT_YUV422P:
240  enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
241  *img_fmt = AOM_IMG_FMT_I422;
242  return 0;
243  case AV_PIX_FMT_YUV444P:
244  enccfg->g_profile = FF_PROFILE_AV1_HIGH;
245  *img_fmt = AOM_IMG_FMT_I444;
246  return 0;
249  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
250  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
251  avctx->pix_fmt == AV_PIX_FMT_YUV420P10 ? 10 : 12;
252  enccfg->g_profile =
253  enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_MAIN : FF_PROFILE_AV1_PROFESSIONAL;
254  *img_fmt = AOM_IMG_FMT_I42016;
255  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
256  return 0;
257  }
258  break;
261  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
262  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
263  avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12;
264  enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
265  *img_fmt = AOM_IMG_FMT_I42216;
266  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
267  return 0;
268  }
269  break;
272  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
273  enccfg->g_bit_depth = enccfg->g_input_bit_depth =
274  avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ? 10 : 12;
275  enccfg->g_profile =
276  enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_HIGH : FF_PROFILE_AV1_PROFESSIONAL;
277  *img_fmt = AOM_IMG_FMT_I44416;
278  *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
279  return 0;
280  }
281  break;
282  default:
283  break;
284  }
285  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
286  return AVERROR_INVALIDDATA;
287 }
288 
289 static void set_color_range(AVCodecContext *avctx)
290 {
291  enum aom_color_range aom_cr;
292  switch (avctx->color_range) {
294  case AVCOL_RANGE_MPEG: aom_cr = AOM_CR_STUDIO_RANGE; break;
295  case AVCOL_RANGE_JPEG: aom_cr = AOM_CR_FULL_RANGE; break;
296  default:
297  av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n",
298  avctx->color_range);
299  return;
300  }
301 
302  codecctl_int(avctx, AV1E_SET_COLOR_RANGE, aom_cr);
303 }
304 
305 static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
306 {
307  int sb_dim = (dim + sb_size - 1) / sb_size;
308  int tile_dim = (sb_dim + (1 << tiles_log2) - 1) >> tiles_log2;
309  av_assert0(tile_dim > 0);
310  return (sb_dim + tile_dim - 1) / tile_dim;
311 }
312 
313 static int choose_tiling(AVCodecContext *avctx,
314  struct aom_codec_enc_cfg *enccfg)
315 {
316  AOMContext *ctx = avctx->priv_data;
317  int sb_128x128_possible, sb_size, sb_width, sb_height;
318  int uniform_rows, uniform_cols;
319  int uniform_64x64_possible, uniform_128x128_possible;
320  int tile_size, rounding, i;
321 
322  if (ctx->tile_cols_log2 >= 0)
323  ctx->tile_cols = 1 << ctx->tile_cols_log2;
324  if (ctx->tile_rows_log2 >= 0)
325  ctx->tile_rows = 1 << ctx->tile_rows_log2;
326 
327  if (ctx->tile_cols == 0) {
328  ctx->tile_cols = (avctx->width + AV1_MAX_TILE_WIDTH - 1) /
330  if (ctx->tile_cols > 1) {
331  av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
332  "columns to fill width.\n", ctx->tile_cols);
333  }
334  }
335  av_assert0(ctx->tile_cols > 0);
336  if (ctx->tile_rows == 0) {
337  int max_tile_width =
338  FFALIGN((FFALIGN(avctx->width, 128) +
339  ctx->tile_cols - 1) / ctx->tile_cols, 128);
340  ctx->tile_rows =
341  (max_tile_width * FFALIGN(avctx->height, 128) +
343  if (ctx->tile_rows > 1) {
344  av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
345  "rows to fill area.\n", ctx->tile_rows);
346  }
347  }
348  av_assert0(ctx->tile_rows > 0);
349 
350  if ((avctx->width + 63) / 64 < ctx->tile_cols ||
351  (avctx->height + 63) / 64 < ctx->tile_rows) {
352  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: frame not "
353  "large enough to fit specified tile arrangement.\n");
354  return AVERROR(EINVAL);
355  }
356  if (ctx->tile_cols > AV1_MAX_TILE_COLS ||
357  ctx->tile_rows > AV1_MAX_TILE_ROWS) {
358  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
359  "not allow more than %dx%d tiles.\n",
361  return AVERROR(EINVAL);
362  }
363  if (avctx->width / ctx->tile_cols > AV1_MAX_TILE_WIDTH) {
364  av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
365  "not allow tiles of width greater than %d.\n",
367  return AVERROR(EINVAL);
368  }
369 
370  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC;
371 
372  if (ctx->tile_cols == 1 && ctx->tile_rows == 1) {
373  av_log(avctx, AV_LOG_DEBUG, "Using a single tile.\n");
374  return 0;
375  }
376 
377  sb_128x128_possible =
378  (avctx->width + 127) / 128 >= ctx->tile_cols &&
379  (avctx->height + 127) / 128 >= ctx->tile_rows;
380 
381  ctx->tile_cols_log2 = ctx->tile_cols == 1 ? 0 :
382  av_log2(ctx->tile_cols - 1) + 1;
383  ctx->tile_rows_log2 = ctx->tile_rows == 1 ? 0 :
384  av_log2(ctx->tile_rows - 1) + 1;
385 
386  uniform_cols = count_uniform_tiling(avctx->width,
387  64, ctx->tile_cols_log2);
388  uniform_rows = count_uniform_tiling(avctx->height,
389  64, ctx->tile_rows_log2);
390  av_log(avctx, AV_LOG_DEBUG, "Uniform with 64x64 superblocks "
391  "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
392  uniform_64x64_possible = uniform_cols == ctx->tile_cols &&
393  uniform_rows == ctx->tile_rows;
394 
395  if (sb_128x128_possible) {
396  uniform_cols = count_uniform_tiling(avctx->width,
397  128, ctx->tile_cols_log2);
398  uniform_rows = count_uniform_tiling(avctx->height,
399  128, ctx->tile_rows_log2);
400  av_log(avctx, AV_LOG_DEBUG, "Uniform with 128x128 superblocks "
401  "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
402  uniform_128x128_possible = uniform_cols == ctx->tile_cols &&
403  uniform_rows == ctx->tile_rows;
404  } else {
405  av_log(avctx, AV_LOG_DEBUG, "128x128 superblocks not possible.\n");
406  uniform_128x128_possible = 0;
407  }
408 
409  ctx->uniform_tiles = 1;
410  if (uniform_64x64_possible && uniform_128x128_possible) {
411  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with dynamic "
412  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
413  ctx->tile_cols_log2, ctx->tile_rows_log2);
414  return 0;
415  }
416  if (uniform_64x64_possible && !sb_128x128_possible) {
417  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 64x64 "
418  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
419  ctx->tile_cols_log2, ctx->tile_rows_log2);
420  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
421  return 0;
422  }
423  if (uniform_128x128_possible) {
424  av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 128x128 "
425  "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
426  ctx->tile_cols_log2, ctx->tile_rows_log2);
427  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
428  return 0;
429  }
430  ctx->uniform_tiles = 0;
431 
432  if (sb_128x128_possible) {
433  sb_size = 128;
434  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
435  } else {
436  sb_size = 64;
437  ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
438  }
439  av_log(avctx, AV_LOG_DEBUG, "Using fixed tiling with %dx%d "
440  "superblocks (tile_cols = %d, tile_rows = %d).\n",
441  sb_size, sb_size, ctx->tile_cols, ctx->tile_rows);
442 
443  enccfg->tile_width_count = ctx->tile_cols;
444  enccfg->tile_height_count = ctx->tile_rows;
445 
446  sb_width = (avctx->width + sb_size - 1) / sb_size;
447  sb_height = (avctx->height + sb_size - 1) / sb_size;
448 
449  tile_size = sb_width / ctx->tile_cols;
450  rounding = sb_width % ctx->tile_cols;
451  for (i = 0; i < ctx->tile_cols; i++) {
452  enccfg->tile_widths[i] = tile_size +
453  (i < rounding / 2 ||
454  i > ctx->tile_cols - 1 - (rounding + 1) / 2);
455  }
456 
457  tile_size = sb_height / ctx->tile_rows;
458  rounding = sb_height % ctx->tile_rows;
459  for (i = 0; i < ctx->tile_rows; i++) {
460  enccfg->tile_heights[i] = tile_size +
461  (i < rounding / 2 ||
462  i > ctx->tile_rows - 1 - (rounding + 1) / 2);
463  }
464 
465  return 0;
466 }
467 
468 static av_cold int aom_init(AVCodecContext *avctx,
469  const struct aom_codec_iface *iface)
470 {
471  AOMContext *ctx = avctx->priv_data;
472  struct aom_codec_enc_cfg enccfg = { 0 };
473 #ifdef AOM_FRAME_IS_INTRAONLY
474  aom_codec_flags_t flags =
475  (avctx->flags & AV_CODEC_FLAG_PSNR) ? AOM_CODEC_USE_PSNR : 0;
476 #else
477  aom_codec_flags_t flags = 0;
478 #endif
479  AVCPBProperties *cpb_props;
480  int res;
481  aom_img_fmt_t img_fmt;
482  aom_codec_caps_t codec_caps = aom_codec_get_caps(iface);
483 
484  av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
485  av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
486 
487  if ((res = aom_codec_enc_config_default(iface, &enccfg, 0)) != AOM_CODEC_OK) {
488  av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
489  aom_codec_err_to_string(res));
490  return AVERROR(EINVAL);
491  }
492 
493  if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
494  return AVERROR(EINVAL);
495 
496  if(!avctx->bit_rate)
497  if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
498  av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
499  return AVERROR(EINVAL);
500  }
501 
502  dump_enc_cfg(avctx, &enccfg);
503 
504  enccfg.g_w = avctx->width;
505  enccfg.g_h = avctx->height;
506  enccfg.g_timebase.num = avctx->time_base.num;
507  enccfg.g_timebase.den = avctx->time_base.den;
508  enccfg.g_threads =
509  FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 64);
510 
511  if (ctx->lag_in_frames >= 0)
512  enccfg.g_lag_in_frames = ctx->lag_in_frames;
513 
514  if (avctx->flags & AV_CODEC_FLAG_PASS1)
515  enccfg.g_pass = AOM_RC_FIRST_PASS;
516  else if (avctx->flags & AV_CODEC_FLAG_PASS2)
517  enccfg.g_pass = AOM_RC_LAST_PASS;
518  else
519  enccfg.g_pass = AOM_RC_ONE_PASS;
520 
521  if (avctx->rc_min_rate == avctx->rc_max_rate &&
522  avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
523  enccfg.rc_end_usage = AOM_CBR;
524  } else if (ctx->crf >= 0) {
525  enccfg.rc_end_usage = AOM_CQ;
526  if (!avctx->bit_rate)
527  enccfg.rc_end_usage = AOM_Q;
528  }
529 
530  if (avctx->bit_rate) {
531  enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
533  } else if (enccfg.rc_end_usage != AOM_Q) {
534  if (enccfg.rc_end_usage == AOM_CQ) {
535  enccfg.rc_target_bitrate = 1000000;
536  } else {
537  avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
538  av_log(avctx, AV_LOG_WARNING,
539  "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
540  enccfg.rc_target_bitrate);
541  }
542  }
543 
544  if (avctx->qmin >= 0)
545  enccfg.rc_min_quantizer = avctx->qmin;
546  if (avctx->qmax >= 0)
547  enccfg.rc_max_quantizer = avctx->qmax;
548 
549  if (enccfg.rc_end_usage == AOM_CQ || enccfg.rc_end_usage == AOM_Q) {
550  if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
551  av_log(avctx, AV_LOG_ERROR,
552  "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
553  ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
554  return AVERROR(EINVAL);
555  }
556  }
557 
558  enccfg.rc_dropframe_thresh = ctx->drop_threshold;
559 
560  // 0-100 (0 => CBR, 100 => VBR)
561  enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
562  if (avctx->bit_rate)
563  enccfg.rc_2pass_vbr_minsection_pct =
564  avctx->rc_min_rate * 100LL / avctx->bit_rate;
565  if (avctx->rc_max_rate)
566  enccfg.rc_2pass_vbr_maxsection_pct =
567  avctx->rc_max_rate * 100LL / avctx->bit_rate;
568 
569  if (avctx->rc_buffer_size)
570  enccfg.rc_buf_sz =
571  avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
572  if (avctx->rc_initial_buffer_occupancy)
573  enccfg.rc_buf_initial_sz =
574  avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
575  enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
576 
577  // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO
578  if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
579  enccfg.kf_min_dist = avctx->keyint_min;
580  if (avctx->gop_size >= 0)
581  enccfg.kf_max_dist = avctx->gop_size;
582 
583  if (enccfg.g_pass == AOM_RC_FIRST_PASS)
584  enccfg.g_lag_in_frames = 0;
585  else if (enccfg.g_pass == AOM_RC_LAST_PASS) {
586  int decode_size, ret;
587 
588  if (!avctx->stats_in) {
589  av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
590  return AVERROR_INVALIDDATA;
591  }
592 
593  ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
594  ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
595  if (ret < 0) {
596  av_log(avctx, AV_LOG_ERROR,
597  "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
598  ctx->twopass_stats.sz);
599  ctx->twopass_stats.sz = 0;
600  return ret;
601  }
602  decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
603  ctx->twopass_stats.sz);
604  if (decode_size < 0) {
605  av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
606  return AVERROR_INVALIDDATA;
607  }
608 
609  ctx->twopass_stats.sz = decode_size;
610  enccfg.rc_twopass_stats_in = ctx->twopass_stats;
611  }
612 
613  /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
614  * complexity playback on low powered devices at the expense of encode
615  * quality. */
616  if (avctx->profile != FF_PROFILE_UNKNOWN)
617  enccfg.g_profile = avctx->profile;
618 
619  enccfg.g_error_resilient = ctx->error_resilient;
620 
621  res = choose_tiling(avctx, &enccfg);
622  if (res < 0)
623  return res;
624 
625  dump_enc_cfg(avctx, &enccfg);
626  /* Construct Encoder Context */
627  res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
628  if (res != AOM_CODEC_OK) {
629  log_encoder_error(avctx, "Failed to initialize encoder");
630  return AVERROR(EINVAL);
631  }
632 
633  // codec control failures are currently treated only as warnings
634  av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n");
635  codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used);
636  if (ctx->auto_alt_ref >= 0)
637  codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
638 
639  codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh);
640  if (ctx->crf >= 0)
641  codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf);
642 
643  codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries);
644  codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace);
645  codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc);
646  set_color_range(avctx);
647 
648  codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size);
649  if (ctx->uniform_tiles) {
650  codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2);
651  codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2);
652  }
653 
654  // provide dummy value to initialize wrapper, values will be updated each _encode()
655  aom_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
656  (unsigned char*)1);
657 
658  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
659  ctx->rawimg.bit_depth = enccfg.g_bit_depth;
660 
661  cpb_props = ff_add_cpb_side_data(avctx);
662  if (!cpb_props)
663  return AVERROR(ENOMEM);
664 
665  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
666  const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata");
667  int ret;
668 
669  if (!filter) {
670  av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter "
671  "not found. This is a bug, please report it.\n");
672  return AVERROR_BUG;
673  }
674  ret = av_bsf_alloc(filter, &ctx->bsf);
675  if (ret < 0)
676  return ret;
677 
678  ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx);
679  if (ret < 0)
680  return ret;
681 
682  ret = av_bsf_init(ctx->bsf);
683  if (ret < 0)
684  return ret;
685  }
686 
687  if (enccfg.rc_end_usage == AOM_CBR ||
688  enccfg.g_pass != AOM_RC_ONE_PASS) {
689  cpb_props->max_bitrate = avctx->rc_max_rate;
690  cpb_props->min_bitrate = avctx->rc_min_rate;
691  cpb_props->avg_bitrate = avctx->bit_rate;
692  }
693  cpb_props->buffer_size = avctx->rc_buffer_size;
694 
695  return 0;
696 }
697 
698 static inline void cx_pktcpy(AOMContext *ctx,
699  struct FrameListData *dst,
700  const struct aom_codec_cx_pkt *src)
701 {
702  dst->pts = src->data.frame.pts;
703  dst->duration = src->data.frame.duration;
704  dst->flags = src->data.frame.flags;
705  dst->sz = src->data.frame.sz;
706  dst->buf = src->data.frame.buf;
707 #ifdef AOM_FRAME_IS_INTRAONLY
708  dst->have_sse = 0;
709  dst->frame_number = ++ctx->frame_number;
710  dst->have_sse = ctx->have_sse;
711  if (ctx->have_sse) {
712  /* associate last-seen SSE to the frame. */
713  /* Transfers ownership from ctx to dst. */
714  memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
715  ctx->have_sse = 0;
716  }
717 #endif
718 }
719 
720 /**
721  * Store coded frame information in format suitable for return from encode2().
722  *
723  * Write information from @a cx_frame to @a pkt
724  * @return packet data size on success
725  * @return a negative AVERROR on error
726  */
727 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
728  AVPacket *pkt)
729 {
730  AOMContext *ctx = avctx->priv_data;
731  int pict_type;
732  int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0);
733  if (ret < 0) {
734  av_log(avctx, AV_LOG_ERROR,
735  "Error getting output packet of size %"SIZE_SPECIFIER".\n", cx_frame->sz);
736  return ret;
737  }
738  memcpy(pkt->data, cx_frame->buf, pkt->size);
739  pkt->pts = pkt->dts = cx_frame->pts;
740 
741  if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) {
742  pkt->flags |= AV_PKT_FLAG_KEY;
743 #ifdef AOM_FRAME_IS_INTRAONLY
744  pict_type = AV_PICTURE_TYPE_I;
745  } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) {
746  pict_type = AV_PICTURE_TYPE_I;
747  } else {
748  pict_type = AV_PICTURE_TYPE_P;
749  }
750 
751  ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1,
752  cx_frame->have_sse ? 3 : 0, pict_type);
753 
754  if (cx_frame->have_sse) {
755  int i;
756  for (i = 0; i < 3; ++i) {
757  avctx->error[i] += cx_frame->sse[i + 1];
758  }
759  cx_frame->have_sse = 0;
760 #endif
761  }
762 
763  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
764  ret = av_bsf_send_packet(ctx->bsf, pkt);
765  if (ret < 0) {
766  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
767  "failed to send input packet\n");
768  return ret;
769  }
770  ret = av_bsf_receive_packet(ctx->bsf, pkt);
771 
772  if (ret < 0) {
773  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
774  "failed to receive output packet\n");
775  return ret;
776  }
777  }
778  return pkt->size;
779 }
780 
781 /**
782  * Queue multiple output frames from the encoder, returning the front-most.
783  * In cases where aom_codec_get_cx_data() returns more than 1 frame append
784  * the frame queue. Return the head frame if available.
785  * @return Stored frame size
786  * @return AVERROR(EINVAL) on output size error
787  * @return AVERROR(ENOMEM) on coded frame queue data allocation error
788  */
789 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
790 {
791  AOMContext *ctx = avctx->priv_data;
792  const struct aom_codec_cx_pkt *pkt;
793  const void *iter = NULL;
794  int size = 0;
795 
796  if (ctx->coded_frame_list) {
797  struct FrameListData *cx_frame = ctx->coded_frame_list;
798  /* return the leading frame if we've already begun queueing */
799  size = storeframe(avctx, cx_frame, pkt_out);
800  if (size < 0)
801  return size;
802  ctx->coded_frame_list = cx_frame->next;
803  free_coded_frame(cx_frame);
804  }
805 
806  /* consume all available output from the encoder before returning. buffers
807  * are only good through the next aom_codec call */
808  while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) {
809  switch (pkt->kind) {
810  case AOM_CODEC_CX_FRAME_PKT:
811  if (!size) {
812  struct FrameListData cx_frame;
813 
814  /* avoid storing the frame when the list is empty and we haven't yet
815  * provided a frame for output */
817  cx_pktcpy(ctx, &cx_frame, pkt);
818  size = storeframe(avctx, &cx_frame, pkt_out);
819  if (size < 0)
820  return size;
821  } else {
822  struct FrameListData *cx_frame =
823  av_malloc(sizeof(struct FrameListData));
824 
825  if (!cx_frame) {
826  av_log(avctx, AV_LOG_ERROR,
827  "Frame queue element alloc failed\n");
828  return AVERROR(ENOMEM);
829  }
830  cx_pktcpy(ctx, cx_frame, pkt);
831  cx_frame->buf = av_malloc(cx_frame->sz);
832 
833  if (!cx_frame->buf) {
834  av_log(avctx, AV_LOG_ERROR,
835  "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
836  cx_frame->sz);
837  av_freep(&cx_frame);
838  return AVERROR(ENOMEM);
839  }
840  memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
841  coded_frame_add(&ctx->coded_frame_list, cx_frame);
842  }
843  break;
844  case AOM_CODEC_STATS_PKT:
845  {
846  struct aom_fixed_buf *stats = &ctx->twopass_stats;
847  int err;
848  if ((err = av_reallocp(&stats->buf,
849  stats->sz +
850  pkt->data.twopass_stats.sz)) < 0) {
851  stats->sz = 0;
852  av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
853  return err;
854  }
855  memcpy((uint8_t *)stats->buf + stats->sz,
856  pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
857  stats->sz += pkt->data.twopass_stats.sz;
858  break;
859  }
860 #ifdef AOM_FRAME_IS_INTRAONLY
861  case AOM_CODEC_PSNR_PKT:
862  {
863  av_assert0(!ctx->have_sse);
864  ctx->sse[0] = pkt->data.psnr.sse[0];
865  ctx->sse[1] = pkt->data.psnr.sse[1];
866  ctx->sse[2] = pkt->data.psnr.sse[2];
867  ctx->sse[3] = pkt->data.psnr.sse[3];
868  ctx->have_sse = 1;
869  break;
870  }
871 #endif
872  case AOM_CODEC_CUSTOM_PKT:
873  // ignore unsupported/unrecognized packet types
874  break;
875  }
876  }
877 
878  return size;
879 }
880 
881 static int aom_encode(AVCodecContext *avctx, AVPacket *pkt,
882  const AVFrame *frame, int *got_packet)
883 {
884  AOMContext *ctx = avctx->priv_data;
885  struct aom_image *rawimg = NULL;
886  int64_t timestamp = 0;
887  int res, coded_size;
888  aom_enc_frame_flags_t flags = 0;
889 
890  if (frame) {
891  rawimg = &ctx->rawimg;
892  rawimg->planes[AOM_PLANE_Y] = frame->data[0];
893  rawimg->planes[AOM_PLANE_U] = frame->data[1];
894  rawimg->planes[AOM_PLANE_V] = frame->data[2];
895  rawimg->stride[AOM_PLANE_Y] = frame->linesize[0];
896  rawimg->stride[AOM_PLANE_U] = frame->linesize[1];
897  rawimg->stride[AOM_PLANE_V] = frame->linesize[2];
898  timestamp = frame->pts;
899  switch (frame->color_range) {
900  case AVCOL_RANGE_MPEG:
901  rawimg->range = AOM_CR_STUDIO_RANGE;
902  break;
903  case AVCOL_RANGE_JPEG:
904  rawimg->range = AOM_CR_FULL_RANGE;
905  break;
906  }
907 
908  if (frame->pict_type == AV_PICTURE_TYPE_I)
909  flags |= AOM_EFLAG_FORCE_KF;
910  }
911 
912  res = aom_codec_encode(&ctx->encoder, rawimg, timestamp,
913  avctx->ticks_per_frame, flags);
914  if (res != AOM_CODEC_OK) {
915  log_encoder_error(avctx, "Error encoding frame");
916  return AVERROR_INVALIDDATA;
917  }
918  coded_size = queue_frames(avctx, pkt);
919 
920  if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
921  size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
922 
923  avctx->stats_out = av_malloc(b64_size);
924  if (!avctx->stats_out) {
925  av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
926  b64_size);
927  return AVERROR(ENOMEM);
928  }
929  av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
930  ctx->twopass_stats.sz);
931  }
932 
933  *got_packet = !!coded_size;
934  return 0;
935 }
936 
937 static const enum AVPixelFormat av1_pix_fmts[] = {
942 };
943 
944 static const enum AVPixelFormat av1_pix_fmts_highbd[] = {
955 };
956 
957 static av_cold void av1_init_static(AVCodec *codec)
958 {
959  aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx());
960  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
961  codec->pix_fmts = av1_pix_fmts_highbd;
962  else
963  codec->pix_fmts = av1_pix_fmts;
964 }
965 
966 static av_cold int av1_init(AVCodecContext *avctx)
967 {
968  return aom_init(avctx, aom_codec_av1_cx());
969 }
970 
971 #define OFFSET(x) offsetof(AOMContext, x)
972 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
973 static const AVOption options[] = {
974  { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VE},
975  { "auto-alt-ref", "Enable use of alternate reference "
976  "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
977  { "lag-in-frames", "Number of frames to look ahead at for "
978  "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
979  { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
980  { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
981  { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE },
982  { "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 },
983  { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
984  { "noise-sensitivity", "Noise sensitivity", OFFSET(noise_sensitivity), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE},
985  { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE },
986  { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
987  { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
988  { NULL }
989 };
990 
991 static const AVCodecDefault defaults[] = {
992  { "qmin", "-1" },
993  { "qmax", "-1" },
994  { "g", "-1" },
995  { "keyint_min", "-1" },
996  { NULL },
997 };
998 
999 static const AVClass class_aom = {
1000  .class_name = "libaom-av1 encoder",
1001  .item_name = av_default_item_name,
1002  .option = options,
1003  .version = LIBAVUTIL_VERSION_INT,
1004 };
1005 
1007  .name = "libaom-av1",
1008  .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
1009  .type = AVMEDIA_TYPE_VIDEO,
1010  .id = AV_CODEC_ID_AV1,
1011  .priv_data_size = sizeof(AOMContext),
1012  .init = av1_init,
1013  .encode2 = aom_encode,
1014  .close = aom_free,
1017  .priv_class = &class_aom,
1018  .defaults = defaults,
1019  .init_static_data = av1_init_static,
1020  .wrapper_name = "libaom",
1021 };
#define OFFSET(x)
Definition: libaomenc.c:971
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:771
uint64_t sse[4]
Definition: libaomenc.c:75
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
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:716
int av_cpu_count(void)
Definition: cpu.c:267
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition: avcodec.h:2709
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:1583
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
const char * desc
Definition: nvenc.c:65
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:1113
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:2435
static av_cold int aom_init(AVCodecContext *avctx, const struct aom_codec_iface *iface)
Definition: libaomenc.c:468
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2164
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: avcodec.h:5703
int size
Definition: avcodec.h:1446
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)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
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:1016
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:383
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2556
int tile_cols_log2
Definition: libaomenc.c:79
uint64_t frame_number
Definition: libaomenc.c:77
static AVPacket pkt
static const AVOption options[]
Definition: libaomenc.c:973
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1036
#define src
Definition: vp8dsp.c:254
int profile
profile
Definition: avcodec.h:2859
AVCodec.
Definition: avcodec.h:3424
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:1118
int error_resilient
Definition: libaomenc.c:70
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1656
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:79
static int choose_tiling(AVCodecContext *avctx, struct aom_codec_enc_cfg *enccfg)
Definition: libaomenc.c:313
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:993
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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:183
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
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
static AVFrame * frame
struct aom_image rawimg
Definition: libaomenc.c:64
uint8_t * data
Definition: avcodec.h:1445
#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:194
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:1129
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:384
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2548
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
static void cx_pktcpy(AOMContext *ctx, struct FrameListData *dst, const struct aom_codec_cx_pkt *src)
Definition: libaomenc.c:698
#define VE
Definition: libaomenc.c:972
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#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:957
int tile_rows
Definition: libaomenc.c:78
#define AVERROR(e)
Definition: error.h:43
int qmax
maximum quantizer
Definition: avcodec.h:2378
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:471
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1613
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:3431
static av_always_inline av_const double round(double x)
Definition: libm.h:444
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:382
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:727
static void set_color_range(AVCodecContext *avctx)
Definition: libaomenc.c:289
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
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:2392
static const AVClass class_aom
Definition: libaomenc.c:999
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:2414
AVCodec ff_libaom_av1_encoder
Definition: libaomenc.c:1006
struct FrameListData * coded_frame_list
Definition: libaomenc.c:66
static enum AVPixelFormat av1_pix_fmts_highbd[]
Definition: libaomenc.c:944
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3445
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:309
#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:789
#define FFMIN(a, b)
Definition: common.h:96
uint64_t sse[4]
Definition: libaomenc.c:54
#define width
int noise_sensitivity
Definition: libaomenc.c:74
int width
picture width / height.
Definition: avcodec.h:1706
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2860
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:177
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:874
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:858
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2143
static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
Definition: libaomenc.c:305
static void stats(AVPacket *const *in, int n_in, unsigned *_max, unsigned *_sum)
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1665
static const char *const ctlidstr[]
Definition: libaomenc.c:84
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:2785
#define av_log2
Definition: intmath.h:83
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:512
int have_sse
true if we have pending sse[]
Definition: libaomenc.c:76
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:228
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:1108
struct aom_codec_ctx encoder
Definition: libaomenc.c:63
#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:80
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
const AVProfile ff_av1_profiles[]
Definition: profiles.c:142
main external API structure.
Definition: avcodec.h:1533
static enum AVPixelFormat av1_pix_fmts[]
Definition: libaomenc.c:937
int qmin
minimum quantizer
Definition: avcodec.h:2371
AV1 common definitions.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
static av_cold int av1_init(AVCodecContext *avctx)
Definition: libaomenc.c:966
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:379
static void coded_frame_add(void *list, struct FrameListData *cx_frame)
Definition: libaomenc.c:167
Describe the class of an AVClass context structure.
Definition: log.h:67
static const AVProfile profiles[]
#define FF_PROFILE_AV1_PROFESSIONAL
Definition: avcodec.h:2954
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2157
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2150
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:2104
int uniform_tiles
Definition: libaomenc.c:81
#define FF_PROFILE_AV1_MAIN
Definition: avcodec.h:2952
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:72
static av_cold void dump_enc_cfg(AVCodecContext *avctx, const struct aom_codec_enc_cfg *cfg)
Definition: libaomenc.c:109
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2363
int drop_threshold
Definition: libaomenc.c:73
int have_sse
true if we have pending sse[]
Definition: libaomenc.c:55
#define SIZE_SPECIFIER
Definition: internal.h:264
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:380
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:386
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
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:891
AVBSFContext * bsf
Definition: libaomenc.c:62
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:511
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1728
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:98
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:881
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:2019
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:862
void * priv_data
Definition: avcodec.h:1560
int tile_cols
Definition: libaomenc.c:78
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:1123
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:1444
#define av_freep(p)
static const AVCodecDefault defaults[]
Definition: libaomenc.c:991
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1422
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5731
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
static av_cold int aom_free(AVCodecContext *avctx)
Definition: libaomenc.c:216
Predicted.
Definition: avutil.h:275
int lag_in_frames
Definition: libaomenc.c:69
#define av_unused
Definition: attributes.h:125
#define FF_PROFILE_AV1_HIGH
Definition: avcodec.h:2953
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2407
int keyint_min
minimum GOP size
Definition: avcodec.h:2110