FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 = avctx->thread_count ? avctx->thread_count : av_cpu_count();
509 
510  if (ctx->lag_in_frames >= 0)
511  enccfg.g_lag_in_frames = ctx->lag_in_frames;
512 
513  if (avctx->flags & AV_CODEC_FLAG_PASS1)
514  enccfg.g_pass = AOM_RC_FIRST_PASS;
515  else if (avctx->flags & AV_CODEC_FLAG_PASS2)
516  enccfg.g_pass = AOM_RC_LAST_PASS;
517  else
518  enccfg.g_pass = AOM_RC_ONE_PASS;
519 
520  if (avctx->rc_min_rate == avctx->rc_max_rate &&
521  avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
522  enccfg.rc_end_usage = AOM_CBR;
523  } else if (ctx->crf >= 0) {
524  enccfg.rc_end_usage = AOM_CQ;
525  if (!avctx->bit_rate)
526  enccfg.rc_end_usage = AOM_Q;
527  }
528 
529  if (avctx->bit_rate) {
530  enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
532  } else if (enccfg.rc_end_usage != AOM_Q) {
533  if (enccfg.rc_end_usage == AOM_CQ) {
534  enccfg.rc_target_bitrate = 1000000;
535  } else {
536  avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
537  av_log(avctx, AV_LOG_WARNING,
538  "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
539  enccfg.rc_target_bitrate);
540  }
541  }
542 
543  if (avctx->qmin >= 0)
544  enccfg.rc_min_quantizer = avctx->qmin;
545  if (avctx->qmax >= 0)
546  enccfg.rc_max_quantizer = avctx->qmax;
547 
548  if (enccfg.rc_end_usage == AOM_CQ || enccfg.rc_end_usage == AOM_Q) {
549  if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
550  av_log(avctx, AV_LOG_ERROR,
551  "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
552  ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
553  return AVERROR(EINVAL);
554  }
555  }
556 
557  enccfg.rc_dropframe_thresh = ctx->drop_threshold;
558 
559  // 0-100 (0 => CBR, 100 => VBR)
560  enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
561  if (avctx->bit_rate)
562  enccfg.rc_2pass_vbr_minsection_pct =
563  avctx->rc_min_rate * 100LL / avctx->bit_rate;
564  if (avctx->rc_max_rate)
565  enccfg.rc_2pass_vbr_maxsection_pct =
566  avctx->rc_max_rate * 100LL / avctx->bit_rate;
567 
568  if (avctx->rc_buffer_size)
569  enccfg.rc_buf_sz =
570  avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
571  if (avctx->rc_initial_buffer_occupancy)
572  enccfg.rc_buf_initial_sz =
573  avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
574  enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
575 
576  // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO
577  if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
578  enccfg.kf_min_dist = avctx->keyint_min;
579  if (avctx->gop_size >= 0)
580  enccfg.kf_max_dist = avctx->gop_size;
581 
582  if (enccfg.g_pass == AOM_RC_FIRST_PASS)
583  enccfg.g_lag_in_frames = 0;
584  else if (enccfg.g_pass == AOM_RC_LAST_PASS) {
585  int decode_size, ret;
586 
587  if (!avctx->stats_in) {
588  av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
589  return AVERROR_INVALIDDATA;
590  }
591 
592  ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
593  ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
594  if (ret < 0) {
595  av_log(avctx, AV_LOG_ERROR,
596  "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
597  ctx->twopass_stats.sz);
598  ctx->twopass_stats.sz = 0;
599  return ret;
600  }
601  decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
602  ctx->twopass_stats.sz);
603  if (decode_size < 0) {
604  av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
605  return AVERROR_INVALIDDATA;
606  }
607 
608  ctx->twopass_stats.sz = decode_size;
609  enccfg.rc_twopass_stats_in = ctx->twopass_stats;
610  }
611 
612  /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
613  * complexity playback on low powered devices at the expense of encode
614  * quality. */
615  if (avctx->profile != FF_PROFILE_UNKNOWN)
616  enccfg.g_profile = avctx->profile;
617 
618  enccfg.g_error_resilient = ctx->error_resilient;
619 
620  res = choose_tiling(avctx, &enccfg);
621  if (res < 0)
622  return res;
623 
624  dump_enc_cfg(avctx, &enccfg);
625  /* Construct Encoder Context */
626  res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
627  if (res != AOM_CODEC_OK) {
628  log_encoder_error(avctx, "Failed to initialize encoder");
629  return AVERROR(EINVAL);
630  }
631 
632  // codec control failures are currently treated only as warnings
633  av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n");
634  codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used);
635  if (ctx->auto_alt_ref >= 0)
636  codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
637 
638  codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh);
639  if (ctx->crf >= 0)
640  codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf);
641 
642  codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries);
643  codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace);
644  codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc);
645  set_color_range(avctx);
646 
647  codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size);
648  if (ctx->uniform_tiles) {
649  codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2);
650  codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2);
651  }
652 
653  // provide dummy value to initialize wrapper, values will be updated each _encode()
654  aom_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
655  (unsigned char*)1);
656 
657  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
658  ctx->rawimg.bit_depth = enccfg.g_bit_depth;
659 
660  cpb_props = ff_add_cpb_side_data(avctx);
661  if (!cpb_props)
662  return AVERROR(ENOMEM);
663 
664  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
665  const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata");
666  int ret;
667 
668  if (!filter) {
669  av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter "
670  "not found. This is a bug, please report it.\n");
671  return AVERROR_BUG;
672  }
673  ret = av_bsf_alloc(filter, &ctx->bsf);
674  if (ret < 0)
675  return ret;
676 
677  ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx);
678  if (ret < 0)
679  return ret;
680 
681  ret = av_bsf_init(ctx->bsf);
682  if (ret < 0)
683  return ret;
684  }
685 
686  if (enccfg.rc_end_usage == AOM_CBR ||
687  enccfg.g_pass != AOM_RC_ONE_PASS) {
688  cpb_props->max_bitrate = avctx->rc_max_rate;
689  cpb_props->min_bitrate = avctx->rc_min_rate;
690  cpb_props->avg_bitrate = avctx->bit_rate;
691  }
692  cpb_props->buffer_size = avctx->rc_buffer_size;
693 
694  return 0;
695 }
696 
697 static inline void cx_pktcpy(AOMContext *ctx,
698  struct FrameListData *dst,
699  const struct aom_codec_cx_pkt *src)
700 {
701  dst->pts = src->data.frame.pts;
702  dst->duration = src->data.frame.duration;
703  dst->flags = src->data.frame.flags;
704  dst->sz = src->data.frame.sz;
705  dst->buf = src->data.frame.buf;
706 #ifdef AOM_FRAME_IS_INTRAONLY
707  dst->have_sse = 0;
708  dst->frame_number = ++ctx->frame_number;
709  dst->have_sse = ctx->have_sse;
710  if (ctx->have_sse) {
711  /* associate last-seen SSE to the frame. */
712  /* Transfers ownership from ctx to dst. */
713  memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
714  ctx->have_sse = 0;
715  }
716 #endif
717 }
718 
719 /**
720  * Store coded frame information in format suitable for return from encode2().
721  *
722  * Write information from @a cx_frame to @a pkt
723  * @return packet data size on success
724  * @return a negative AVERROR on error
725  */
726 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
727  AVPacket *pkt)
728 {
729  AOMContext *ctx = avctx->priv_data;
730  int pict_type;
731  int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0);
732  if (ret < 0) {
733  av_log(avctx, AV_LOG_ERROR,
734  "Error getting output packet of size %"SIZE_SPECIFIER".\n", cx_frame->sz);
735  return ret;
736  }
737  memcpy(pkt->data, cx_frame->buf, pkt->size);
738  pkt->pts = pkt->dts = cx_frame->pts;
739 
740  if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) {
741  pkt->flags |= AV_PKT_FLAG_KEY;
742 #ifdef AOM_FRAME_IS_INTRAONLY
743  pict_type = AV_PICTURE_TYPE_I;
744  } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) {
745  pict_type = AV_PICTURE_TYPE_I;
746  } else {
747  pict_type = AV_PICTURE_TYPE_P;
748  }
749 
750  ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1,
751  cx_frame->have_sse ? 3 : 0, pict_type);
752 
753  if (cx_frame->have_sse) {
754  int i;
755  for (i = 0; i < 3; ++i) {
756  avctx->error[i] += cx_frame->sse[i + 1];
757  }
758  cx_frame->have_sse = 0;
759 #endif
760  }
761 
762  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
763  ret = av_bsf_send_packet(ctx->bsf, pkt);
764  if (ret < 0) {
765  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
766  "failed to send input packet\n");
767  return ret;
768  }
769  ret = av_bsf_receive_packet(ctx->bsf, pkt);
770 
771  if (ret < 0) {
772  av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
773  "failed to receive output packet\n");
774  return ret;
775  }
776  }
777  return pkt->size;
778 }
779 
780 /**
781  * Queue multiple output frames from the encoder, returning the front-most.
782  * In cases where aom_codec_get_cx_data() returns more than 1 frame append
783  * the frame queue. Return the head frame if available.
784  * @return Stored frame size
785  * @return AVERROR(EINVAL) on output size error
786  * @return AVERROR(ENOMEM) on coded frame queue data allocation error
787  */
788 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
789 {
790  AOMContext *ctx = avctx->priv_data;
791  const struct aom_codec_cx_pkt *pkt;
792  const void *iter = NULL;
793  int size = 0;
794 
795  if (ctx->coded_frame_list) {
796  struct FrameListData *cx_frame = ctx->coded_frame_list;
797  /* return the leading frame if we've already begun queueing */
798  size = storeframe(avctx, cx_frame, pkt_out);
799  if (size < 0)
800  return size;
801  ctx->coded_frame_list = cx_frame->next;
802  free_coded_frame(cx_frame);
803  }
804 
805  /* consume all available output from the encoder before returning. buffers
806  * are only good through the next aom_codec call */
807  while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) {
808  switch (pkt->kind) {
809  case AOM_CODEC_CX_FRAME_PKT:
810  if (!size) {
811  struct FrameListData cx_frame;
812 
813  /* avoid storing the frame when the list is empty and we haven't yet
814  * provided a frame for output */
816  cx_pktcpy(ctx, &cx_frame, pkt);
817  size = storeframe(avctx, &cx_frame, pkt_out);
818  if (size < 0)
819  return size;
820  } else {
821  struct FrameListData *cx_frame =
822  av_malloc(sizeof(struct FrameListData));
823 
824  if (!cx_frame) {
825  av_log(avctx, AV_LOG_ERROR,
826  "Frame queue element alloc failed\n");
827  return AVERROR(ENOMEM);
828  }
829  cx_pktcpy(ctx, cx_frame, pkt);
830  cx_frame->buf = av_malloc(cx_frame->sz);
831 
832  if (!cx_frame->buf) {
833  av_log(avctx, AV_LOG_ERROR,
834  "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
835  cx_frame->sz);
836  av_freep(&cx_frame);
837  return AVERROR(ENOMEM);
838  }
839  memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
840  coded_frame_add(&ctx->coded_frame_list, cx_frame);
841  }
842  break;
843  case AOM_CODEC_STATS_PKT:
844  {
845  struct aom_fixed_buf *stats = &ctx->twopass_stats;
846  int err;
847  if ((err = av_reallocp(&stats->buf,
848  stats->sz +
849  pkt->data.twopass_stats.sz)) < 0) {
850  stats->sz = 0;
851  av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
852  return err;
853  }
854  memcpy((uint8_t *)stats->buf + stats->sz,
855  pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
856  stats->sz += pkt->data.twopass_stats.sz;
857  break;
858  }
859 #ifdef AOM_FRAME_IS_INTRAONLY
860  case AOM_CODEC_PSNR_PKT:
861  {
862  av_assert0(!ctx->have_sse);
863  ctx->sse[0] = pkt->data.psnr.sse[0];
864  ctx->sse[1] = pkt->data.psnr.sse[1];
865  ctx->sse[2] = pkt->data.psnr.sse[2];
866  ctx->sse[3] = pkt->data.psnr.sse[3];
867  ctx->have_sse = 1;
868  break;
869  }
870 #endif
871  case AOM_CODEC_CUSTOM_PKT:
872  // ignore unsupported/unrecognized packet types
873  break;
874  }
875  }
876 
877  return size;
878 }
879 
880 static int aom_encode(AVCodecContext *avctx, AVPacket *pkt,
881  const AVFrame *frame, int *got_packet)
882 {
883  AOMContext *ctx = avctx->priv_data;
884  struct aom_image *rawimg = NULL;
885  int64_t timestamp = 0;
886  int res, coded_size;
887  aom_enc_frame_flags_t flags = 0;
888 
889  if (frame) {
890  rawimg = &ctx->rawimg;
891  rawimg->planes[AOM_PLANE_Y] = frame->data[0];
892  rawimg->planes[AOM_PLANE_U] = frame->data[1];
893  rawimg->planes[AOM_PLANE_V] = frame->data[2];
894  rawimg->stride[AOM_PLANE_Y] = frame->linesize[0];
895  rawimg->stride[AOM_PLANE_U] = frame->linesize[1];
896  rawimg->stride[AOM_PLANE_V] = frame->linesize[2];
897  timestamp = frame->pts;
898  switch (frame->color_range) {
899  case AVCOL_RANGE_MPEG:
900  rawimg->range = AOM_CR_STUDIO_RANGE;
901  break;
902  case AVCOL_RANGE_JPEG:
903  rawimg->range = AOM_CR_FULL_RANGE;
904  break;
905  }
906 
907  if (frame->pict_type == AV_PICTURE_TYPE_I)
908  flags |= AOM_EFLAG_FORCE_KF;
909  }
910 
911  res = aom_codec_encode(&ctx->encoder, rawimg, timestamp,
912  avctx->ticks_per_frame, flags);
913  if (res != AOM_CODEC_OK) {
914  log_encoder_error(avctx, "Error encoding frame");
915  return AVERROR_INVALIDDATA;
916  }
917  coded_size = queue_frames(avctx, pkt);
918 
919  if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
920  size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
921 
922  avctx->stats_out = av_malloc(b64_size);
923  if (!avctx->stats_out) {
924  av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
925  b64_size);
926  return AVERROR(ENOMEM);
927  }
928  av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
929  ctx->twopass_stats.sz);
930  }
931 
932  *got_packet = !!coded_size;
933  return 0;
934 }
935 
936 static const enum AVPixelFormat av1_pix_fmts[] = {
941 };
942 
943 static const enum AVPixelFormat av1_pix_fmts_highbd[] = {
954 };
955 
956 static av_cold void av1_init_static(AVCodec *codec)
957 {
958  aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx());
959  if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
960  codec->pix_fmts = av1_pix_fmts_highbd;
961  else
962  codec->pix_fmts = av1_pix_fmts;
963 }
964 
965 static av_cold int av1_init(AVCodecContext *avctx)
966 {
967  return aom_init(avctx, aom_codec_av1_cx());
968 }
969 
970 #define OFFSET(x) offsetof(AOMContext, x)
971 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
972 static const AVOption options[] = {
973  { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VE},
974  { "auto-alt-ref", "Enable use of alternate reference "
975  "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
976  { "lag-in-frames", "Number of frames to look ahead at for "
977  "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
978  { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
979  { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
980  { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE },
981  { "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 },
982  { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
983  { "noise-sensitivity", "Noise sensitivity", OFFSET(noise_sensitivity), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE},
984  { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE },
985  { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
986  { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
987  { NULL }
988 };
989 
990 static const AVCodecDefault defaults[] = {
991  { "qmin", "-1" },
992  { "qmax", "-1" },
993  { "g", "-1" },
994  { "keyint_min", "-1" },
995  { NULL },
996 };
997 
998 static const AVClass class_aom = {
999  .class_name = "libaom-av1 encoder",
1000  .item_name = av_default_item_name,
1001  .option = options,
1002  .version = LIBAVUTIL_VERSION_INT,
1003 };
1004 
1006  .name = "libaom-av1",
1007  .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
1008  .type = AVMEDIA_TYPE_VIDEO,
1009  .id = AV_CODEC_ID_AV1,
1010  .priv_data_size = sizeof(AOMContext),
1011  .init = av1_init,
1012  .encode2 = aom_encode,
1013  .close = aom_free,
1016  .priv_class = &class_aom,
1017  .defaults = defaults,
1018  .init_static_data = av1_init_static,
1019  .wrapper_name = "libaom",
1020 };
#define OFFSET(x)
Definition: libaomenc.c:970
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
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)
int av_log2(unsigned v)
Definition: intmath.c:26
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
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
#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:972
#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:134
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:81
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:211
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
ptrdiff_t size
Definition: opengl_enc.c:101
#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:697
#define VE
Definition: libaomenc.c:971
#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:956
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:186
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:726
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:998
int64_t rc_min_rate
minimum bitrate
Definition: avcodec.h:2414
AVCodec ff_libaom_av1_encoder
Definition: libaomenc.c:1005
struct FrameListData * coded_frame_list
Definition: libaomenc.c:66
static enum AVPixelFormat av1_pix_fmts_highbd[]
Definition: libaomenc.c:943
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:788
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:185
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
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:936
int qmin
minimum quantizer
Definition: avcodec.h:2371
void * buf
Definition: avisynth_c.h:690
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:965
#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:2031
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:262
#define flags(name, subs,...)
Definition: cbs_av1.c:596
#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:880
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:1946
#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:990
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