You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1299 lines
55KB

  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. * @file
  22. * AV1 encoder support via libaom
  23. */
  24. #define AOM_DISABLE_CTRL_TYPECHECKS 1
  25. #include <aom/aom_encoder.h>
  26. #include <aom/aomcx.h>
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/base64.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "av1.h"
  34. #include "avcodec.h"
  35. #include "internal.h"
  36. #include "packet_internal.h"
  37. #include "profiles.h"
  38. /*
  39. * Portion of struct aom_codec_cx_pkt from aom_encoder.h.
  40. * One encoded frame returned from the library.
  41. */
  42. struct FrameListData {
  43. void *buf; /**< compressed data buffer */
  44. size_t sz; /**< length of compressed data */
  45. int64_t pts; /**< time stamp to show frame
  46. (in timebase units) */
  47. unsigned long duration; /**< duration to show frame
  48. (in timebase units) */
  49. uint32_t flags; /**< flags for this frame */
  50. uint64_t sse[4];
  51. int have_sse; /**< true if we have pending sse[] */
  52. uint64_t frame_number;
  53. struct FrameListData *next;
  54. };
  55. typedef struct AOMEncoderContext {
  56. AVClass *class;
  57. AVBSFContext *bsf;
  58. struct aom_codec_ctx encoder;
  59. struct aom_image rawimg;
  60. struct aom_fixed_buf twopass_stats;
  61. struct FrameListData *coded_frame_list;
  62. int cpu_used;
  63. int auto_alt_ref;
  64. int arnr_max_frames;
  65. int arnr_strength;
  66. int aq_mode;
  67. int lag_in_frames;
  68. int error_resilient;
  69. int crf;
  70. int static_thresh;
  71. int drop_threshold;
  72. int denoise_noise_level;
  73. int denoise_block_size;
  74. uint64_t sse[4];
  75. int have_sse; /**< true if we have pending sse[] */
  76. uint64_t frame_number;
  77. int rc_undershoot_pct;
  78. int rc_overshoot_pct;
  79. int minsection_pct;
  80. int maxsection_pct;
  81. int frame_parallel;
  82. int tile_cols, tile_rows;
  83. int tile_cols_log2, tile_rows_log2;
  84. aom_superblock_size_t superblock_size;
  85. int uniform_tiles;
  86. int row_mt;
  87. int enable_cdef;
  88. int enable_global_motion;
  89. int enable_intrabc;
  90. int enable_restoration;
  91. int usage;
  92. int tune;
  93. int enable_rect_partitions;
  94. int enable_1to4_partitions;
  95. int enable_ab_partitions;
  96. int enable_angle_delta;
  97. int enable_cfl_intra;
  98. int enable_paeth_intra;
  99. int enable_smooth_intra;
  100. int enable_intra_edge_filter;
  101. int enable_palette;
  102. int enable_filter_intra;
  103. int enable_flip_idtx;
  104. int enable_tx64;
  105. int reduced_tx_type_set;
  106. int use_intra_dct_only;
  107. int use_inter_dct_only;
  108. int use_intra_default_tx_only;
  109. int enable_ref_frame_mvs;
  110. int enable_interinter_wedge;
  111. int enable_interintra_wedge;
  112. int enable_interintra_comp;
  113. int enable_masked_comp;
  114. int enable_obmc;
  115. int enable_onesided_comp;
  116. int enable_reduced_reference_set;
  117. int enable_smooth_interintra;
  118. int enable_diff_wtd_comp;
  119. int enable_dist_wtd_comp;
  120. int enable_dual_filter;
  121. } AOMContext;
  122. static const char *const ctlidstr[] = {
  123. [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED",
  124. [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL",
  125. [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF",
  126. [AOME_SET_ARNR_MAXFRAMES] = "AOME_SET_ARNR_MAXFRAMES",
  127. [AOME_SET_ARNR_STRENGTH] = "AOME_SET_ARNR_STRENGTH",
  128. [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD",
  129. [AV1E_SET_COLOR_RANGE] = "AV1E_SET_COLOR_RANGE",
  130. [AV1E_SET_COLOR_PRIMARIES] = "AV1E_SET_COLOR_PRIMARIES",
  131. [AV1E_SET_MATRIX_COEFFICIENTS] = "AV1E_SET_MATRIX_COEFFICIENTS",
  132. [AV1E_SET_TRANSFER_CHARACTERISTICS] = "AV1E_SET_TRANSFER_CHARACTERISTICS",
  133. [AV1E_SET_AQ_MODE] = "AV1E_SET_AQ_MODE",
  134. [AV1E_SET_FRAME_PARALLEL_DECODING] = "AV1E_SET_FRAME_PARALLEL_DECODING",
  135. [AV1E_SET_SUPERBLOCK_SIZE] = "AV1E_SET_SUPERBLOCK_SIZE",
  136. [AV1E_SET_TILE_COLUMNS] = "AV1E_SET_TILE_COLUMNS",
  137. [AV1E_SET_TILE_ROWS] = "AV1E_SET_TILE_ROWS",
  138. [AV1E_SET_ENABLE_RESTORATION] = "AV1E_SET_ENABLE_RESTORATION",
  139. #ifdef AOM_CTRL_AV1E_SET_ROW_MT
  140. [AV1E_SET_ROW_MT] = "AV1E_SET_ROW_MT",
  141. #endif
  142. #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL
  143. [AV1E_SET_DENOISE_NOISE_LEVEL] = "AV1E_SET_DENOISE_NOISE_LEVEL",
  144. #endif
  145. #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE
  146. [AV1E_SET_DENOISE_BLOCK_SIZE] = "AV1E_SET_DENOISE_BLOCK_SIZE",
  147. #endif
  148. #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES
  149. [AV1E_SET_MAX_REFERENCE_FRAMES] = "AV1E_SET_MAX_REFERENCE_FRAMES",
  150. #endif
  151. #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION
  152. [AV1E_SET_ENABLE_GLOBAL_MOTION] = "AV1E_SET_ENABLE_GLOBAL_MOTION",
  153. #endif
  154. #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC
  155. [AV1E_SET_ENABLE_INTRABC] = "AV1E_SET_ENABLE_INTRABC",
  156. #endif
  157. [AV1E_SET_ENABLE_CDEF] = "AV1E_SET_ENABLE_CDEF",
  158. [AOME_SET_TUNING] = "AOME_SET_TUNING",
  159. #if AOM_ENCODER_ABI_VERSION >= 22
  160. [AV1E_SET_ENABLE_1TO4_PARTITIONS] = "AV1E_SET_ENABLE_1TO4_PARTITIONS",
  161. [AV1E_SET_ENABLE_AB_PARTITIONS] = "AV1E_SET_ENABLE_AB_PARTITIONS",
  162. [AV1E_SET_ENABLE_RECT_PARTITIONS] = "AV1E_SET_ENABLE_RECT_PARTITIONS",
  163. [AV1E_SET_ENABLE_ANGLE_DELTA] = "AV1E_SET_ENABLE_ANGLE_DELTA",
  164. [AV1E_SET_ENABLE_CFL_INTRA] = "AV1E_SET_ENABLE_CFL_INTRA",
  165. [AV1E_SET_ENABLE_FILTER_INTRA] = "AV1E_SET_ENABLE_FILTER_INTRA",
  166. [AV1E_SET_ENABLE_INTRA_EDGE_FILTER] = "AV1E_SET_ENABLE_INTRA_EDGE_FILTER",
  167. [AV1E_SET_ENABLE_PAETH_INTRA] = "AV1E_SET_ENABLE_PAETH_INTRA",
  168. [AV1E_SET_ENABLE_SMOOTH_INTRA] = "AV1E_SET_ENABLE_SMOOTH_INTRA",
  169. [AV1E_SET_ENABLE_PALETTE] = "AV1E_SET_ENABLE_PALETTE",
  170. [AV1E_SET_ENABLE_FLIP_IDTX] = "AV1E_SET_ENABLE_FLIP_IDTX",
  171. [AV1E_SET_ENABLE_TX64] = "AV1E_SET_ENABLE_TX64",
  172. [AV1E_SET_INTRA_DCT_ONLY] = "AV1E_SET_INTRA_DCT_ONLY",
  173. [AV1E_SET_INTER_DCT_ONLY] = "AV1E_SET_INTER_DCT_ONLY",
  174. [AV1E_SET_INTRA_DEFAULT_TX_ONLY] = "AV1E_SET_INTRA_DEFAULT_TX_ONLY",
  175. [AV1E_SET_REDUCED_TX_TYPE_SET] = "AV1E_SET_REDUCED_TX_TYPE_SET",
  176. [AV1E_SET_ENABLE_DIFF_WTD_COMP] = "AV1E_SET_ENABLE_DIFF_WTD_COMP",
  177. [AV1E_SET_ENABLE_DIST_WTD_COMP] = "AV1E_SET_ENABLE_DIST_WTD_COMP",
  178. [AV1E_SET_ENABLE_DUAL_FILTER] = "AV1E_SET_ENABLE_DUAL_FILTER",
  179. [AV1E_SET_ENABLE_INTERINTER_WEDGE] = "AV1E_SET_ENABLE_INTERINTER_WEDGE",
  180. [AV1E_SET_ENABLE_INTERINTRA_WEDGE] = "AV1E_SET_ENABLE_INTERINTRA_WEDGE",
  181. [AV1E_SET_ENABLE_MASKED_COMP] = "AV1E_SET_ENABLE_MASKED_COMP",
  182. [AV1E_SET_ENABLE_INTERINTRA_COMP] = "AV1E_SET_ENABLE_INTERINTRA_COMP",
  183. [AV1E_SET_ENABLE_OBMC] = "AV1E_SET_ENABLE_OBMC",
  184. [AV1E_SET_ENABLE_ONESIDED_COMP] = "AV1E_SET_ENABLE_ONESIDED_COMP",
  185. [AV1E_SET_REDUCED_REFERENCE_SET] = "AV1E_SET_REDUCED_REFERENCE_SET",
  186. [AV1E_SET_ENABLE_SMOOTH_INTERINTRA] = "AV1E_SET_ENABLE_SMOOTH_INTERINTRA",
  187. [AV1E_SET_ENABLE_REF_FRAME_MVS] = "AV1E_SET_ENABLE_REF_FRAME_MVS",
  188. #endif
  189. };
  190. static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
  191. {
  192. AOMContext *ctx = avctx->priv_data;
  193. const char *error = aom_codec_error(&ctx->encoder);
  194. const char *detail = aom_codec_error_detail(&ctx->encoder);
  195. av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
  196. if (detail)
  197. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
  198. }
  199. static av_cold void dump_enc_cfg(AVCodecContext *avctx,
  200. const struct aom_codec_enc_cfg *cfg)
  201. {
  202. int width = -30;
  203. int level = AV_LOG_DEBUG;
  204. av_log(avctx, level, "aom_codec_enc_cfg\n");
  205. av_log(avctx, level, "generic settings\n"
  206. " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
  207. " %*s%u\n %*s%u\n"
  208. " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
  209. width, "g_usage:", cfg->g_usage,
  210. width, "g_threads:", cfg->g_threads,
  211. width, "g_profile:", cfg->g_profile,
  212. width, "g_w:", cfg->g_w,
  213. width, "g_h:", cfg->g_h,
  214. width, "g_bit_depth:", cfg->g_bit_depth,
  215. width, "g_input_bit_depth:", cfg->g_input_bit_depth,
  216. width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
  217. width, "g_error_resilient:", cfg->g_error_resilient,
  218. width, "g_pass:", cfg->g_pass,
  219. width, "g_lag_in_frames:", cfg->g_lag_in_frames);
  220. av_log(avctx, level, "rate control settings\n"
  221. " %*s%u\n %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n",
  222. width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
  223. width, "rc_end_usage:", cfg->rc_end_usage,
  224. width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
  225. width, "rc_target_bitrate:", cfg->rc_target_bitrate);
  226. av_log(avctx, level, "quantizer settings\n"
  227. " %*s%u\n %*s%u\n",
  228. width, "rc_min_quantizer:", cfg->rc_min_quantizer,
  229. width, "rc_max_quantizer:", cfg->rc_max_quantizer);
  230. av_log(avctx, level, "bitrate tolerance\n"
  231. " %*s%u\n %*s%u\n",
  232. width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
  233. width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
  234. av_log(avctx, level, "decoder buffer model\n"
  235. " %*s%u\n %*s%u\n %*s%u\n",
  236. width, "rc_buf_sz:", cfg->rc_buf_sz,
  237. width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
  238. width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
  239. av_log(avctx, level, "2 pass rate control settings\n"
  240. " %*s%u\n %*s%u\n %*s%u\n",
  241. width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
  242. width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
  243. width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
  244. av_log(avctx, level, "keyframing settings\n"
  245. " %*s%d\n %*s%u\n %*s%u\n",
  246. width, "kf_mode:", cfg->kf_mode,
  247. width, "kf_min_dist:", cfg->kf_min_dist,
  248. width, "kf_max_dist:", cfg->kf_max_dist);
  249. av_log(avctx, level, "tile settings\n"
  250. " %*s%d\n %*s%d\n",
  251. width, "tile_width_count:", cfg->tile_width_count,
  252. width, "tile_height_count:", cfg->tile_height_count);
  253. av_log(avctx, level, "\n");
  254. }
  255. static void coded_frame_add(void *list, struct FrameListData *cx_frame)
  256. {
  257. struct FrameListData **p = list;
  258. while (*p)
  259. p = &(*p)->next;
  260. *p = cx_frame;
  261. cx_frame->next = NULL;
  262. }
  263. static av_cold void free_coded_frame(struct FrameListData *cx_frame)
  264. {
  265. av_freep(&cx_frame->buf);
  266. av_freep(&cx_frame);
  267. }
  268. static av_cold void free_frame_list(struct FrameListData *list)
  269. {
  270. struct FrameListData *p = list;
  271. while (p) {
  272. list = list->next;
  273. free_coded_frame(p);
  274. p = list;
  275. }
  276. }
  277. static av_cold int codecctl_int(AVCodecContext *avctx,
  278. #ifdef UENUM1BYTE
  279. aome_enc_control_id id,
  280. #else
  281. enum aome_enc_control_id id,
  282. #endif
  283. int val)
  284. {
  285. AOMContext *ctx = avctx->priv_data;
  286. char buf[80];
  287. int width = -30;
  288. int res;
  289. snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
  290. av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
  291. res = aom_codec_control(&ctx->encoder, id, val);
  292. if (res != AOM_CODEC_OK) {
  293. snprintf(buf, sizeof(buf), "Failed to set %s codec control",
  294. ctlidstr[id]);
  295. log_encoder_error(avctx, buf);
  296. return AVERROR(EINVAL);
  297. }
  298. return 0;
  299. }
  300. static av_cold int aom_free(AVCodecContext *avctx)
  301. {
  302. AOMContext *ctx = avctx->priv_data;
  303. aom_codec_destroy(&ctx->encoder);
  304. av_freep(&ctx->twopass_stats.buf);
  305. av_freep(&avctx->stats_out);
  306. free_frame_list(ctx->coded_frame_list);
  307. av_bsf_free(&ctx->bsf);
  308. return 0;
  309. }
  310. static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps,
  311. struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags,
  312. aom_img_fmt_t *img_fmt)
  313. {
  314. AOMContext av_unused *ctx = avctx->priv_data;
  315. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  316. enccfg->g_bit_depth = enccfg->g_input_bit_depth = desc->comp[0].depth;
  317. switch (avctx->pix_fmt) {
  318. case AV_PIX_FMT_YUV420P:
  319. enccfg->g_profile = FF_PROFILE_AV1_MAIN;
  320. *img_fmt = AOM_IMG_FMT_I420;
  321. return 0;
  322. case AV_PIX_FMT_YUV422P:
  323. enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
  324. *img_fmt = AOM_IMG_FMT_I422;
  325. return 0;
  326. case AV_PIX_FMT_YUV444P:
  327. case AV_PIX_FMT_GBRP:
  328. enccfg->g_profile = FF_PROFILE_AV1_HIGH;
  329. *img_fmt = AOM_IMG_FMT_I444;
  330. return 0;
  331. case AV_PIX_FMT_YUV420P10:
  332. case AV_PIX_FMT_YUV420P12:
  333. if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
  334. enccfg->g_profile =
  335. enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_MAIN : FF_PROFILE_AV1_PROFESSIONAL;
  336. *img_fmt = AOM_IMG_FMT_I42016;
  337. *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
  338. return 0;
  339. }
  340. break;
  341. case AV_PIX_FMT_YUV422P10:
  342. case AV_PIX_FMT_YUV422P12:
  343. if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
  344. enccfg->g_profile = FF_PROFILE_AV1_PROFESSIONAL;
  345. *img_fmt = AOM_IMG_FMT_I42216;
  346. *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
  347. return 0;
  348. }
  349. break;
  350. case AV_PIX_FMT_YUV444P10:
  351. case AV_PIX_FMT_YUV444P12:
  352. case AV_PIX_FMT_GBRP10:
  353. case AV_PIX_FMT_GBRP12:
  354. if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
  355. enccfg->g_profile =
  356. enccfg->g_bit_depth == 10 ? FF_PROFILE_AV1_HIGH : FF_PROFILE_AV1_PROFESSIONAL;
  357. *img_fmt = AOM_IMG_FMT_I44416;
  358. *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
  359. return 0;
  360. }
  361. break;
  362. default:
  363. break;
  364. }
  365. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
  366. return AVERROR_INVALIDDATA;
  367. }
  368. static void set_color_range(AVCodecContext *avctx)
  369. {
  370. aom_color_range_t aom_cr;
  371. switch (avctx->color_range) {
  372. case AVCOL_RANGE_UNSPECIFIED:
  373. case AVCOL_RANGE_MPEG: aom_cr = AOM_CR_STUDIO_RANGE; break;
  374. case AVCOL_RANGE_JPEG: aom_cr = AOM_CR_FULL_RANGE; break;
  375. default:
  376. av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n",
  377. avctx->color_range);
  378. return;
  379. }
  380. codecctl_int(avctx, AV1E_SET_COLOR_RANGE, aom_cr);
  381. }
  382. static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
  383. {
  384. int sb_dim = (dim + sb_size - 1) / sb_size;
  385. int tile_dim = (sb_dim + (1 << tiles_log2) - 1) >> tiles_log2;
  386. av_assert0(tile_dim > 0);
  387. return (sb_dim + tile_dim - 1) / tile_dim;
  388. }
  389. static int choose_tiling(AVCodecContext *avctx,
  390. struct aom_codec_enc_cfg *enccfg)
  391. {
  392. AOMContext *ctx = avctx->priv_data;
  393. int sb_128x128_possible, sb_size, sb_width, sb_height;
  394. int uniform_rows, uniform_cols;
  395. int uniform_64x64_possible, uniform_128x128_possible;
  396. int tile_size, rounding, i;
  397. if (ctx->tile_cols_log2 >= 0)
  398. ctx->tile_cols = 1 << ctx->tile_cols_log2;
  399. if (ctx->tile_rows_log2 >= 0)
  400. ctx->tile_rows = 1 << ctx->tile_rows_log2;
  401. if (ctx->tile_cols == 0) {
  402. ctx->tile_cols = (avctx->width + AV1_MAX_TILE_WIDTH - 1) /
  403. AV1_MAX_TILE_WIDTH;
  404. if (ctx->tile_cols > 1) {
  405. av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
  406. "columns to fill width.\n", ctx->tile_cols);
  407. }
  408. }
  409. av_assert0(ctx->tile_cols > 0);
  410. if (ctx->tile_rows == 0) {
  411. int max_tile_width =
  412. FFALIGN((FFALIGN(avctx->width, 128) +
  413. ctx->tile_cols - 1) / ctx->tile_cols, 128);
  414. ctx->tile_rows =
  415. (max_tile_width * FFALIGN(avctx->height, 128) +
  416. AV1_MAX_TILE_AREA - 1) / AV1_MAX_TILE_AREA;
  417. if (ctx->tile_rows > 1) {
  418. av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
  419. "rows to fill area.\n", ctx->tile_rows);
  420. }
  421. }
  422. av_assert0(ctx->tile_rows > 0);
  423. if ((avctx->width + 63) / 64 < ctx->tile_cols ||
  424. (avctx->height + 63) / 64 < ctx->tile_rows) {
  425. av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: frame not "
  426. "large enough to fit specified tile arrangement.\n");
  427. return AVERROR(EINVAL);
  428. }
  429. if (ctx->tile_cols > AV1_MAX_TILE_COLS ||
  430. ctx->tile_rows > AV1_MAX_TILE_ROWS) {
  431. av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
  432. "not allow more than %dx%d tiles.\n",
  433. AV1_MAX_TILE_COLS, AV1_MAX_TILE_ROWS);
  434. return AVERROR(EINVAL);
  435. }
  436. if (avctx->width / ctx->tile_cols > AV1_MAX_TILE_WIDTH) {
  437. av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
  438. "not allow tiles of width greater than %d.\n",
  439. AV1_MAX_TILE_WIDTH);
  440. return AVERROR(EINVAL);
  441. }
  442. ctx->superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC;
  443. if (ctx->tile_cols == 1 && ctx->tile_rows == 1) {
  444. av_log(avctx, AV_LOG_DEBUG, "Using a single tile.\n");
  445. return 0;
  446. }
  447. sb_128x128_possible =
  448. (avctx->width + 127) / 128 >= ctx->tile_cols &&
  449. (avctx->height + 127) / 128 >= ctx->tile_rows;
  450. ctx->tile_cols_log2 = ctx->tile_cols == 1 ? 0 :
  451. av_log2(ctx->tile_cols - 1) + 1;
  452. ctx->tile_rows_log2 = ctx->tile_rows == 1 ? 0 :
  453. av_log2(ctx->tile_rows - 1) + 1;
  454. uniform_cols = count_uniform_tiling(avctx->width,
  455. 64, ctx->tile_cols_log2);
  456. uniform_rows = count_uniform_tiling(avctx->height,
  457. 64, ctx->tile_rows_log2);
  458. av_log(avctx, AV_LOG_DEBUG, "Uniform with 64x64 superblocks "
  459. "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
  460. uniform_64x64_possible = uniform_cols == ctx->tile_cols &&
  461. uniform_rows == ctx->tile_rows;
  462. if (sb_128x128_possible) {
  463. uniform_cols = count_uniform_tiling(avctx->width,
  464. 128, ctx->tile_cols_log2);
  465. uniform_rows = count_uniform_tiling(avctx->height,
  466. 128, ctx->tile_rows_log2);
  467. av_log(avctx, AV_LOG_DEBUG, "Uniform with 128x128 superblocks "
  468. "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
  469. uniform_128x128_possible = uniform_cols == ctx->tile_cols &&
  470. uniform_rows == ctx->tile_rows;
  471. } else {
  472. av_log(avctx, AV_LOG_DEBUG, "128x128 superblocks not possible.\n");
  473. uniform_128x128_possible = 0;
  474. }
  475. ctx->uniform_tiles = 1;
  476. if (uniform_64x64_possible && uniform_128x128_possible) {
  477. av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with dynamic "
  478. "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
  479. ctx->tile_cols_log2, ctx->tile_rows_log2);
  480. return 0;
  481. }
  482. if (uniform_64x64_possible && !sb_128x128_possible) {
  483. av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 64x64 "
  484. "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
  485. ctx->tile_cols_log2, ctx->tile_rows_log2);
  486. ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
  487. return 0;
  488. }
  489. if (uniform_128x128_possible) {
  490. av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 128x128 "
  491. "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
  492. ctx->tile_cols_log2, ctx->tile_rows_log2);
  493. ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
  494. return 0;
  495. }
  496. ctx->uniform_tiles = 0;
  497. if (sb_128x128_possible) {
  498. sb_size = 128;
  499. ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
  500. } else {
  501. sb_size = 64;
  502. ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
  503. }
  504. av_log(avctx, AV_LOG_DEBUG, "Using fixed tiling with %dx%d "
  505. "superblocks (tile_cols = %d, tile_rows = %d).\n",
  506. sb_size, sb_size, ctx->tile_cols, ctx->tile_rows);
  507. enccfg->tile_width_count = ctx->tile_cols;
  508. enccfg->tile_height_count = ctx->tile_rows;
  509. sb_width = (avctx->width + sb_size - 1) / sb_size;
  510. sb_height = (avctx->height + sb_size - 1) / sb_size;
  511. tile_size = sb_width / ctx->tile_cols;
  512. rounding = sb_width % ctx->tile_cols;
  513. for (i = 0; i < ctx->tile_cols; i++) {
  514. enccfg->tile_widths[i] = tile_size +
  515. (i < rounding / 2 ||
  516. i > ctx->tile_cols - 1 - (rounding + 1) / 2);
  517. }
  518. tile_size = sb_height / ctx->tile_rows;
  519. rounding = sb_height % ctx->tile_rows;
  520. for (i = 0; i < ctx->tile_rows; i++) {
  521. enccfg->tile_heights[i] = tile_size +
  522. (i < rounding / 2 ||
  523. i > ctx->tile_rows - 1 - (rounding + 1) / 2);
  524. }
  525. return 0;
  526. }
  527. static av_cold int aom_init(AVCodecContext *avctx,
  528. const struct aom_codec_iface *iface)
  529. {
  530. AOMContext *ctx = avctx->priv_data;
  531. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  532. struct aom_codec_enc_cfg enccfg = { 0 };
  533. #ifdef AOM_FRAME_IS_INTRAONLY
  534. aom_codec_flags_t flags =
  535. (avctx->flags & AV_CODEC_FLAG_PSNR) ? AOM_CODEC_USE_PSNR : 0;
  536. #else
  537. aom_codec_flags_t flags = 0;
  538. #endif
  539. AVCPBProperties *cpb_props;
  540. int res;
  541. aom_img_fmt_t img_fmt;
  542. aom_codec_caps_t codec_caps = aom_codec_get_caps(iface);
  543. av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
  544. av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
  545. if ((res = aom_codec_enc_config_default(iface, &enccfg, 0)) != AOM_CODEC_OK) {
  546. av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
  547. aom_codec_err_to_string(res));
  548. return AVERROR(EINVAL);
  549. }
  550. if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
  551. return AVERROR(EINVAL);
  552. if(!avctx->bit_rate)
  553. if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
  554. av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
  555. return AVERROR(EINVAL);
  556. }
  557. dump_enc_cfg(avctx, &enccfg);
  558. enccfg.g_w = avctx->width;
  559. enccfg.g_h = avctx->height;
  560. enccfg.g_timebase.num = avctx->time_base.num;
  561. enccfg.g_timebase.den = avctx->time_base.den;
  562. enccfg.g_threads =
  563. FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 64);
  564. enccfg.g_usage = ctx->usage;
  565. if (ctx->lag_in_frames >= 0)
  566. enccfg.g_lag_in_frames = ctx->lag_in_frames;
  567. if (avctx->flags & AV_CODEC_FLAG_PASS1)
  568. enccfg.g_pass = AOM_RC_FIRST_PASS;
  569. else if (avctx->flags & AV_CODEC_FLAG_PASS2)
  570. enccfg.g_pass = AOM_RC_LAST_PASS;
  571. else
  572. enccfg.g_pass = AOM_RC_ONE_PASS;
  573. if (avctx->rc_min_rate == avctx->rc_max_rate &&
  574. avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
  575. enccfg.rc_end_usage = AOM_CBR;
  576. } else if (ctx->crf >= 0) {
  577. enccfg.rc_end_usage = AOM_CQ;
  578. if (!avctx->bit_rate)
  579. enccfg.rc_end_usage = AOM_Q;
  580. }
  581. if (avctx->bit_rate) {
  582. enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
  583. AV_ROUND_NEAR_INF);
  584. } else if (enccfg.rc_end_usage != AOM_Q) {
  585. enccfg.rc_end_usage = AOM_Q;
  586. ctx->crf = 32;
  587. av_log(avctx, AV_LOG_WARNING,
  588. "Neither bitrate nor constrained quality specified, using default CRF of %d\n",
  589. ctx->crf);
  590. }
  591. if (avctx->qmin >= 0)
  592. enccfg.rc_min_quantizer = avctx->qmin;
  593. if (avctx->qmax >= 0)
  594. enccfg.rc_max_quantizer = avctx->qmax;
  595. if (enccfg.rc_end_usage == AOM_CQ || enccfg.rc_end_usage == AOM_Q) {
  596. if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) {
  597. av_log(avctx, AV_LOG_ERROR,
  598. "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
  599. ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer);
  600. return AVERROR(EINVAL);
  601. }
  602. }
  603. enccfg.rc_dropframe_thresh = ctx->drop_threshold;
  604. // 0-100 (0 => CBR, 100 => VBR)
  605. enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
  606. if (ctx->minsection_pct >= 0)
  607. enccfg.rc_2pass_vbr_minsection_pct = ctx->minsection_pct;
  608. else if (avctx->bit_rate)
  609. enccfg.rc_2pass_vbr_minsection_pct =
  610. avctx->rc_min_rate * 100LL / avctx->bit_rate;
  611. if (ctx->maxsection_pct >= 0)
  612. enccfg.rc_2pass_vbr_maxsection_pct = ctx->maxsection_pct;
  613. else if (avctx->rc_max_rate)
  614. enccfg.rc_2pass_vbr_maxsection_pct =
  615. avctx->rc_max_rate * 100LL / avctx->bit_rate;
  616. if (avctx->rc_buffer_size)
  617. enccfg.rc_buf_sz =
  618. avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
  619. if (avctx->rc_initial_buffer_occupancy)
  620. enccfg.rc_buf_initial_sz =
  621. avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
  622. enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
  623. if (ctx->rc_undershoot_pct >= 0)
  624. enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct;
  625. if (ctx->rc_overshoot_pct >= 0)
  626. enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct;
  627. // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO
  628. if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
  629. enccfg.kf_min_dist = avctx->keyint_min;
  630. if (avctx->gop_size >= 0)
  631. enccfg.kf_max_dist = avctx->gop_size;
  632. if (enccfg.g_pass == AOM_RC_FIRST_PASS)
  633. enccfg.g_lag_in_frames = 0;
  634. else if (enccfg.g_pass == AOM_RC_LAST_PASS) {
  635. int decode_size, ret;
  636. if (!avctx->stats_in) {
  637. av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
  638. return AVERROR_INVALIDDATA;
  639. }
  640. ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
  641. ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
  642. if (ret < 0) {
  643. av_log(avctx, AV_LOG_ERROR,
  644. "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
  645. ctx->twopass_stats.sz);
  646. ctx->twopass_stats.sz = 0;
  647. return ret;
  648. }
  649. decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
  650. ctx->twopass_stats.sz);
  651. if (decode_size < 0) {
  652. av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
  653. return AVERROR_INVALIDDATA;
  654. }
  655. ctx->twopass_stats.sz = decode_size;
  656. enccfg.rc_twopass_stats_in = ctx->twopass_stats;
  657. }
  658. /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
  659. * complexity playback on low powered devices at the expense of encode
  660. * quality. */
  661. if (avctx->profile != FF_PROFILE_UNKNOWN)
  662. enccfg.g_profile = avctx->profile;
  663. enccfg.g_error_resilient = ctx->error_resilient;
  664. res = choose_tiling(avctx, &enccfg);
  665. if (res < 0)
  666. return res;
  667. dump_enc_cfg(avctx, &enccfg);
  668. /* Construct Encoder Context */
  669. res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
  670. if (res != AOM_CODEC_OK) {
  671. log_encoder_error(avctx, "Failed to initialize encoder");
  672. return AVERROR(EINVAL);
  673. }
  674. // codec control failures are currently treated only as warnings
  675. av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n");
  676. codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used);
  677. if (ctx->auto_alt_ref >= 0)
  678. codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
  679. if (ctx->arnr_max_frames >= 0)
  680. codecctl_int(avctx, AOME_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
  681. if (ctx->arnr_strength >= 0)
  682. codecctl_int(avctx, AOME_SET_ARNR_STRENGTH, ctx->arnr_strength);
  683. if (ctx->enable_cdef >= 0)
  684. codecctl_int(avctx, AV1E_SET_ENABLE_CDEF, ctx->enable_cdef);
  685. if (ctx->enable_restoration >= 0)
  686. codecctl_int(avctx, AV1E_SET_ENABLE_RESTORATION, ctx->enable_restoration);
  687. #if AOM_ENCODER_ABI_VERSION >= 22
  688. if (ctx->enable_rect_partitions >= 0)
  689. codecctl_int(avctx, AV1E_SET_ENABLE_RECT_PARTITIONS, ctx->enable_rect_partitions);
  690. if (ctx->enable_1to4_partitions >= 0)
  691. codecctl_int(avctx, AV1E_SET_ENABLE_1TO4_PARTITIONS, ctx->enable_1to4_partitions);
  692. if (ctx->enable_ab_partitions >= 0)
  693. codecctl_int(avctx, AV1E_SET_ENABLE_AB_PARTITIONS, ctx->enable_ab_partitions);
  694. if (ctx->enable_angle_delta >= 0)
  695. codecctl_int(avctx, AV1E_SET_ENABLE_ANGLE_DELTA, ctx->enable_angle_delta);
  696. if (ctx->enable_cfl_intra >= 0)
  697. codecctl_int(avctx, AV1E_SET_ENABLE_CFL_INTRA, ctx->enable_cfl_intra);
  698. if (ctx->enable_filter_intra >= 0)
  699. codecctl_int(avctx, AV1E_SET_ENABLE_FILTER_INTRA, ctx->enable_filter_intra);
  700. if (ctx->enable_intra_edge_filter >= 0)
  701. codecctl_int(avctx, AV1E_SET_ENABLE_INTRA_EDGE_FILTER, ctx->enable_intra_edge_filter);
  702. if (ctx->enable_paeth_intra >= 0)
  703. codecctl_int(avctx, AV1E_SET_ENABLE_PAETH_INTRA, ctx->enable_paeth_intra);
  704. if (ctx->enable_smooth_intra >= 0)
  705. codecctl_int(avctx, AV1E_SET_ENABLE_SMOOTH_INTRA, ctx->enable_smooth_intra);
  706. if (ctx->enable_palette >= 0)
  707. codecctl_int(avctx, AV1E_SET_ENABLE_PALETTE, ctx->enable_palette);
  708. if (ctx->enable_tx64 >= 0)
  709. codecctl_int(avctx, AV1E_SET_ENABLE_TX64, ctx->enable_tx64);
  710. if (ctx->enable_flip_idtx >= 0)
  711. codecctl_int(avctx, AV1E_SET_ENABLE_FLIP_IDTX, ctx->enable_flip_idtx);
  712. if (ctx->use_intra_dct_only >= 0)
  713. codecctl_int(avctx, AV1E_SET_INTRA_DCT_ONLY, ctx->use_intra_dct_only);
  714. if (ctx->use_inter_dct_only >= 0)
  715. codecctl_int(avctx, AV1E_SET_INTER_DCT_ONLY, ctx->use_inter_dct_only);
  716. if (ctx->use_intra_default_tx_only >= 0)
  717. codecctl_int(avctx, AV1E_SET_INTRA_DEFAULT_TX_ONLY, ctx->use_intra_default_tx_only);
  718. if (ctx->reduced_tx_type_set >= 0)
  719. codecctl_int(avctx, AV1E_SET_REDUCED_TX_TYPE_SET, ctx->reduced_tx_type_set);
  720. if (ctx->enable_ref_frame_mvs >= 0)
  721. codecctl_int(avctx, AV1E_SET_ENABLE_REF_FRAME_MVS, ctx->enable_ref_frame_mvs);
  722. if (ctx->enable_reduced_reference_set >= 0)
  723. codecctl_int(avctx, AV1E_SET_REDUCED_REFERENCE_SET, ctx->enable_reduced_reference_set);
  724. if (ctx->enable_diff_wtd_comp >= 0)
  725. codecctl_int(avctx, AV1E_SET_ENABLE_DIFF_WTD_COMP, ctx->enable_diff_wtd_comp);
  726. if (ctx->enable_dist_wtd_comp >= 0)
  727. codecctl_int(avctx, AV1E_SET_ENABLE_DIST_WTD_COMP, ctx->enable_dist_wtd_comp);
  728. if (ctx->enable_dual_filter >= 0)
  729. codecctl_int(avctx, AV1E_SET_ENABLE_DUAL_FILTER, ctx->enable_dual_filter);
  730. if (ctx->enable_interinter_wedge >= 0)
  731. codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTER_WEDGE, ctx->enable_interinter_wedge);
  732. if (ctx->enable_masked_comp >= 0)
  733. codecctl_int(avctx, AV1E_SET_ENABLE_MASKED_COMP, ctx->enable_masked_comp);
  734. if (ctx->enable_interintra_comp >= 0)
  735. codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTRA_COMP, ctx->enable_interintra_comp);
  736. if (ctx->enable_interintra_wedge >= 0)
  737. codecctl_int(avctx, AV1E_SET_ENABLE_INTERINTRA_WEDGE, ctx->enable_interintra_wedge);
  738. if (ctx->enable_obmc >= 0)
  739. codecctl_int(avctx, AV1E_SET_ENABLE_OBMC, ctx->enable_obmc);
  740. if (ctx->enable_onesided_comp >= 0)
  741. codecctl_int(avctx, AV1E_SET_ENABLE_ONESIDED_COMP, ctx->enable_onesided_comp);
  742. if (ctx->enable_smooth_interintra >= 0)
  743. codecctl_int(avctx, AV1E_SET_ENABLE_SMOOTH_INTERINTRA, ctx->enable_smooth_interintra);
  744. #endif
  745. codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh);
  746. if (ctx->crf >= 0)
  747. codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf);
  748. if (ctx->tune >= 0)
  749. codecctl_int(avctx, AOME_SET_TUNING, ctx->tune);
  750. if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
  751. codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, AVCOL_PRI_BT709);
  752. codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, AVCOL_SPC_RGB);
  753. codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, AVCOL_TRC_IEC61966_2_1);
  754. } else {
  755. codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries);
  756. codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace);
  757. codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc);
  758. }
  759. if (ctx->aq_mode >= 0)
  760. codecctl_int(avctx, AV1E_SET_AQ_MODE, ctx->aq_mode);
  761. if (ctx->frame_parallel >= 0)
  762. codecctl_int(avctx, AV1E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
  763. set_color_range(avctx);
  764. codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size);
  765. if (ctx->uniform_tiles) {
  766. codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2);
  767. codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2);
  768. }
  769. #ifdef AOM_CTRL_AV1E_SET_DENOISE_NOISE_LEVEL
  770. if (ctx->denoise_noise_level >= 0)
  771. codecctl_int(avctx, AV1E_SET_DENOISE_NOISE_LEVEL, ctx->denoise_noise_level);
  772. #endif
  773. #ifdef AOM_CTRL_AV1E_SET_DENOISE_BLOCK_SIZE
  774. if (ctx->denoise_block_size >= 0)
  775. codecctl_int(avctx, AV1E_SET_DENOISE_BLOCK_SIZE, ctx->denoise_block_size);
  776. #endif
  777. #ifdef AOM_CTRL_AV1E_SET_ENABLE_GLOBAL_MOTION
  778. if (ctx->enable_global_motion >= 0)
  779. codecctl_int(avctx, AV1E_SET_ENABLE_GLOBAL_MOTION, ctx->enable_global_motion);
  780. #endif
  781. #ifdef AOM_CTRL_AV1E_SET_MAX_REFERENCE_FRAMES
  782. if (avctx->refs >= 3) {
  783. codecctl_int(avctx, AV1E_SET_MAX_REFERENCE_FRAMES, avctx->refs);
  784. }
  785. #endif
  786. #ifdef AOM_CTRL_AV1E_SET_ROW_MT
  787. if (ctx->row_mt >= 0)
  788. codecctl_int(avctx, AV1E_SET_ROW_MT, ctx->row_mt);
  789. #endif
  790. #ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC
  791. if (ctx->enable_intrabc >= 0)
  792. codecctl_int(avctx, AV1E_SET_ENABLE_INTRABC, ctx->enable_intrabc);
  793. #endif
  794. // provide dummy value to initialize wrapper, values will be updated each _encode()
  795. aom_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
  796. (unsigned char*)1);
  797. if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
  798. ctx->rawimg.bit_depth = enccfg.g_bit_depth;
  799. cpb_props = ff_add_cpb_side_data(avctx);
  800. if (!cpb_props)
  801. return AVERROR(ENOMEM);
  802. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  803. const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata");
  804. int ret;
  805. if (!filter) {
  806. av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter "
  807. "not found. This is a bug, please report it.\n");
  808. return AVERROR_BUG;
  809. }
  810. ret = av_bsf_alloc(filter, &ctx->bsf);
  811. if (ret < 0)
  812. return ret;
  813. ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx);
  814. if (ret < 0)
  815. return ret;
  816. ret = av_bsf_init(ctx->bsf);
  817. if (ret < 0)
  818. return ret;
  819. }
  820. if (enccfg.rc_end_usage == AOM_CBR ||
  821. enccfg.g_pass != AOM_RC_ONE_PASS) {
  822. cpb_props->max_bitrate = avctx->rc_max_rate;
  823. cpb_props->min_bitrate = avctx->rc_min_rate;
  824. cpb_props->avg_bitrate = avctx->bit_rate;
  825. }
  826. cpb_props->buffer_size = avctx->rc_buffer_size;
  827. return 0;
  828. }
  829. static inline void cx_pktcpy(AOMContext *ctx,
  830. struct FrameListData *dst,
  831. const struct aom_codec_cx_pkt *src)
  832. {
  833. dst->pts = src->data.frame.pts;
  834. dst->duration = src->data.frame.duration;
  835. dst->flags = src->data.frame.flags;
  836. dst->sz = src->data.frame.sz;
  837. dst->buf = src->data.frame.buf;
  838. #ifdef AOM_FRAME_IS_INTRAONLY
  839. dst->have_sse = 0;
  840. dst->frame_number = ++ctx->frame_number;
  841. dst->have_sse = ctx->have_sse;
  842. if (ctx->have_sse) {
  843. /* associate last-seen SSE to the frame. */
  844. /* Transfers ownership from ctx to dst. */
  845. memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
  846. ctx->have_sse = 0;
  847. }
  848. #endif
  849. }
  850. /**
  851. * Store coded frame information in format suitable for return from encode2().
  852. *
  853. * Write information from @a cx_frame to @a pkt
  854. * @return packet data size on success
  855. * @return a negative AVERROR on error
  856. */
  857. static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
  858. AVPacket *pkt)
  859. {
  860. AOMContext *ctx = avctx->priv_data;
  861. int av_unused pict_type;
  862. int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz, 0);
  863. if (ret < 0) {
  864. av_log(avctx, AV_LOG_ERROR,
  865. "Error getting output packet of size %"SIZE_SPECIFIER".\n", cx_frame->sz);
  866. return ret;
  867. }
  868. memcpy(pkt->data, cx_frame->buf, pkt->size);
  869. pkt->pts = pkt->dts = cx_frame->pts;
  870. if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) {
  871. pkt->flags |= AV_PKT_FLAG_KEY;
  872. #ifdef AOM_FRAME_IS_INTRAONLY
  873. pict_type = AV_PICTURE_TYPE_I;
  874. } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) {
  875. pict_type = AV_PICTURE_TYPE_I;
  876. } else {
  877. pict_type = AV_PICTURE_TYPE_P;
  878. }
  879. ff_side_data_set_encoder_stats(pkt, 0, cx_frame->sse + 1,
  880. cx_frame->have_sse ? 3 : 0, pict_type);
  881. if (cx_frame->have_sse) {
  882. int i;
  883. for (i = 0; i < 3; ++i) {
  884. avctx->error[i] += cx_frame->sse[i + 1];
  885. }
  886. cx_frame->have_sse = 0;
  887. #endif
  888. }
  889. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  890. ret = av_bsf_send_packet(ctx->bsf, pkt);
  891. if (ret < 0) {
  892. av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
  893. "failed to send input packet\n");
  894. return ret;
  895. }
  896. ret = av_bsf_receive_packet(ctx->bsf, pkt);
  897. if (ret < 0) {
  898. av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
  899. "failed to receive output packet\n");
  900. return ret;
  901. }
  902. }
  903. return pkt->size;
  904. }
  905. /**
  906. * Queue multiple output frames from the encoder, returning the front-most.
  907. * In cases where aom_codec_get_cx_data() returns more than 1 frame append
  908. * the frame queue. Return the head frame if available.
  909. * @return Stored frame size
  910. * @return AVERROR(EINVAL) on output size error
  911. * @return AVERROR(ENOMEM) on coded frame queue data allocation error
  912. */
  913. static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
  914. {
  915. AOMContext *ctx = avctx->priv_data;
  916. const struct aom_codec_cx_pkt *pkt;
  917. const void *iter = NULL;
  918. int size = 0;
  919. if (ctx->coded_frame_list) {
  920. struct FrameListData *cx_frame = ctx->coded_frame_list;
  921. /* return the leading frame if we've already begun queueing */
  922. size = storeframe(avctx, cx_frame, pkt_out);
  923. if (size < 0)
  924. return size;
  925. ctx->coded_frame_list = cx_frame->next;
  926. free_coded_frame(cx_frame);
  927. }
  928. /* consume all available output from the encoder before returning. buffers
  929. * are only good through the next aom_codec call */
  930. while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) {
  931. switch (pkt->kind) {
  932. case AOM_CODEC_CX_FRAME_PKT:
  933. if (!size) {
  934. struct FrameListData cx_frame;
  935. /* avoid storing the frame when the list is empty and we haven't yet
  936. * provided a frame for output */
  937. av_assert0(!ctx->coded_frame_list);
  938. cx_pktcpy(ctx, &cx_frame, pkt);
  939. size = storeframe(avctx, &cx_frame, pkt_out);
  940. if (size < 0)
  941. return size;
  942. } else {
  943. struct FrameListData *cx_frame =
  944. av_malloc(sizeof(struct FrameListData));
  945. if (!cx_frame) {
  946. av_log(avctx, AV_LOG_ERROR,
  947. "Frame queue element alloc failed\n");
  948. return AVERROR(ENOMEM);
  949. }
  950. cx_pktcpy(ctx, cx_frame, pkt);
  951. cx_frame->buf = av_malloc(cx_frame->sz);
  952. if (!cx_frame->buf) {
  953. av_log(avctx, AV_LOG_ERROR,
  954. "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
  955. cx_frame->sz);
  956. av_freep(&cx_frame);
  957. return AVERROR(ENOMEM);
  958. }
  959. memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
  960. coded_frame_add(&ctx->coded_frame_list, cx_frame);
  961. }
  962. break;
  963. case AOM_CODEC_STATS_PKT:
  964. {
  965. struct aom_fixed_buf *stats = &ctx->twopass_stats;
  966. int err;
  967. if ((err = av_reallocp(&stats->buf,
  968. stats->sz +
  969. pkt->data.twopass_stats.sz)) < 0) {
  970. stats->sz = 0;
  971. av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
  972. return err;
  973. }
  974. memcpy((uint8_t *)stats->buf + stats->sz,
  975. pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
  976. stats->sz += pkt->data.twopass_stats.sz;
  977. break;
  978. }
  979. #ifdef AOM_FRAME_IS_INTRAONLY
  980. case AOM_CODEC_PSNR_PKT:
  981. {
  982. av_assert0(!ctx->have_sse);
  983. ctx->sse[0] = pkt->data.psnr.sse[0];
  984. ctx->sse[1] = pkt->data.psnr.sse[1];
  985. ctx->sse[2] = pkt->data.psnr.sse[2];
  986. ctx->sse[3] = pkt->data.psnr.sse[3];
  987. ctx->have_sse = 1;
  988. break;
  989. }
  990. #endif
  991. case AOM_CODEC_CUSTOM_PKT:
  992. // ignore unsupported/unrecognized packet types
  993. break;
  994. }
  995. }
  996. return size;
  997. }
  998. static int aom_encode(AVCodecContext *avctx, AVPacket *pkt,
  999. const AVFrame *frame, int *got_packet)
  1000. {
  1001. AOMContext *ctx = avctx->priv_data;
  1002. struct aom_image *rawimg = NULL;
  1003. int64_t timestamp = 0;
  1004. int res, coded_size;
  1005. aom_enc_frame_flags_t flags = 0;
  1006. if (frame) {
  1007. rawimg = &ctx->rawimg;
  1008. rawimg->planes[AOM_PLANE_Y] = frame->data[0];
  1009. rawimg->planes[AOM_PLANE_U] = frame->data[1];
  1010. rawimg->planes[AOM_PLANE_V] = frame->data[2];
  1011. rawimg->stride[AOM_PLANE_Y] = frame->linesize[0];
  1012. rawimg->stride[AOM_PLANE_U] = frame->linesize[1];
  1013. rawimg->stride[AOM_PLANE_V] = frame->linesize[2];
  1014. timestamp = frame->pts;
  1015. switch (frame->color_range) {
  1016. case AVCOL_RANGE_MPEG:
  1017. rawimg->range = AOM_CR_STUDIO_RANGE;
  1018. break;
  1019. case AVCOL_RANGE_JPEG:
  1020. rawimg->range = AOM_CR_FULL_RANGE;
  1021. break;
  1022. }
  1023. if (frame->pict_type == AV_PICTURE_TYPE_I)
  1024. flags |= AOM_EFLAG_FORCE_KF;
  1025. }
  1026. res = aom_codec_encode(&ctx->encoder, rawimg, timestamp,
  1027. avctx->ticks_per_frame, flags);
  1028. if (res != AOM_CODEC_OK) {
  1029. log_encoder_error(avctx, "Error encoding frame");
  1030. return AVERROR_INVALIDDATA;
  1031. }
  1032. coded_size = queue_frames(avctx, pkt);
  1033. if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
  1034. size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
  1035. avctx->stats_out = av_malloc(b64_size);
  1036. if (!avctx->stats_out) {
  1037. av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n",
  1038. b64_size);
  1039. return AVERROR(ENOMEM);
  1040. }
  1041. av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
  1042. ctx->twopass_stats.sz);
  1043. }
  1044. *got_packet = !!coded_size;
  1045. return 0;
  1046. }
  1047. static const enum AVPixelFormat av1_pix_fmts[] = {
  1048. AV_PIX_FMT_YUV420P,
  1049. AV_PIX_FMT_YUV422P,
  1050. AV_PIX_FMT_YUV444P,
  1051. AV_PIX_FMT_GBRP,
  1052. AV_PIX_FMT_NONE
  1053. };
  1054. static const enum AVPixelFormat av1_pix_fmts_highbd[] = {
  1055. AV_PIX_FMT_YUV420P,
  1056. AV_PIX_FMT_YUV422P,
  1057. AV_PIX_FMT_YUV444P,
  1058. AV_PIX_FMT_GBRP,
  1059. AV_PIX_FMT_YUV420P10,
  1060. AV_PIX_FMT_YUV422P10,
  1061. AV_PIX_FMT_YUV444P10,
  1062. AV_PIX_FMT_YUV420P12,
  1063. AV_PIX_FMT_YUV422P12,
  1064. AV_PIX_FMT_YUV444P12,
  1065. AV_PIX_FMT_GBRP10,
  1066. AV_PIX_FMT_GBRP12,
  1067. AV_PIX_FMT_NONE
  1068. };
  1069. static av_cold void av1_init_static(AVCodec *codec)
  1070. {
  1071. aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx());
  1072. if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
  1073. codec->pix_fmts = av1_pix_fmts_highbd;
  1074. else
  1075. codec->pix_fmts = av1_pix_fmts;
  1076. if (aom_codec_version_major() < 2)
  1077. codec->capabilities |= AV_CODEC_CAP_EXPERIMENTAL;
  1078. }
  1079. static av_cold int av1_init(AVCodecContext *avctx)
  1080. {
  1081. return aom_init(avctx, aom_codec_av1_cx());
  1082. }
  1083. #define OFFSET(x) offsetof(AOMContext, x)
  1084. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1085. static const AVOption options[] = {
  1086. { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VE},
  1087. { "auto-alt-ref", "Enable use of alternate reference "
  1088. "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
  1089. { "lag-in-frames", "Number of frames to look ahead at for "
  1090. "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
  1091. { "arnr-max-frames", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
  1092. { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
  1093. { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 4, VE, "aq_mode"},
  1094. { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode"},
  1095. { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode"},
  1096. { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode"},
  1097. { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode"},
  1098. { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
  1099. { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
  1100. { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE },
  1101. { "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 },
  1102. { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
  1103. { "denoise-noise-level", "Amount of noise to be removed", OFFSET(denoise_noise_level), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
  1104. { "denoise-block-size", "Denoise block size ", OFFSET(denoise_block_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
  1105. { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
  1106. { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1000, VE},
  1107. { "minsection-pct", "GOP min bitrate (% of target)", OFFSET(minsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
  1108. { "maxsection-pct", "GOP max bitrate (% of target)", OFFSET(maxsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 5000, VE},
  1109. { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1110. { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE },
  1111. { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
  1112. { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
  1113. { "row-mt", "Enable row based multi-threading", OFFSET(row_mt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1114. { "enable-cdef", "Enable CDEF filtering", OFFSET(enable_cdef), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1115. { "enable-global-motion", "Enable global motion", OFFSET(enable_global_motion), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1116. { "enable-intrabc", "Enable intra block copy prediction mode", OFFSET(enable_intrabc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1117. { "enable-restoration", "Enable Loop Restoration filtering", OFFSET(enable_restoration), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1118. { "usage", "Quality and compression efficiency vs speed trade-off", OFFSET(usage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "usage"},
  1119. { "good", "Good quality", 0, AV_OPT_TYPE_CONST, {.i64 = 0 /* AOM_USAGE_GOOD_QUALITY */}, 0, 0, VE, "usage"},
  1120. { "realtime", "Realtime encoding", 0, AV_OPT_TYPE_CONST, {.i64 = 1 /* AOM_USAGE_REALTIME */}, 0, 0, VE, "usage"},
  1121. { "tune", "The metric that the encoder tunes for. Automatically chosen by the encoder by default", OFFSET(tune), AV_OPT_TYPE_INT, {.i64 = -1}, -1, AOM_TUNE_SSIM, VE, "tune"},
  1122. { "psnr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_PSNR}, 0, 0, VE, "tune"},
  1123. { "ssim", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_SSIM}, 0, 0, VE, "tune"},
  1124. FF_AV1_PROFILE_OPTS
  1125. { "enable-rect-partitions", "Enable rectangular partitions", OFFSET(enable_rect_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1126. { "enable-1to4-partitions", "Enable 1:4/4:1 partitions", OFFSET(enable_1to4_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1127. { "enable-ab-partitions", "Enable ab shape partitions", OFFSET(enable_ab_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1128. { "enable-angle-delta", "Enable angle delta intra prediction", OFFSET(enable_angle_delta), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1129. { "enable-cfl-intra", "Enable chroma predicted from luma intra prediction", OFFSET(enable_cfl_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1130. { "enable-filter-intra", "Enable filter intra predictor", OFFSET(enable_filter_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1131. { "enable-intra-edge-filter", "Enable intra edge filter", OFFSET(enable_intra_edge_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1132. { "enable-smooth-intra", "Enable smooth intra prediction mode", OFFSET(enable_smooth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1133. { "enable-paeth-intra", "Enable paeth predictor in intra prediction", OFFSET(enable_paeth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1134. { "enable-palette", "Enable palette prediction mode", OFFSET(enable_palette), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1135. { "enable-flip-idtx", "Enable extended transform type", OFFSET(enable_flip_idtx), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1136. { "enable-tx64", "Enable 64-pt transform", OFFSET(enable_tx64), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1137. { "reduced-tx-type-set", "Use reduced set of transform types", OFFSET(reduced_tx_type_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1138. { "use-intra-dct-only", "Use DCT only for INTRA modes", OFFSET(use_intra_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1139. { "use-inter-dct-only", "Use DCT only for INTER modes", OFFSET(use_inter_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1140. { "use-intra-default-tx-only", "Use default-transform only for INTRA modes", OFFSET(use_intra_default_tx_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1141. { "enable-ref-frame-mvs", "Enable temporal mv prediction", OFFSET(enable_ref_frame_mvs), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1142. { "enable-reduced-reference-set", "Use reduced set of single and compound references", OFFSET(enable_reduced_reference_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1143. { "enable-obmc", "Enable obmc", OFFSET(enable_obmc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1144. { "enable-dual-filter", "Enable dual filter", OFFSET(enable_dual_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1145. { "enable-diff-wtd-comp", "Enable difference-weighted compound", OFFSET(enable_diff_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1146. { "enable-dist-wtd-comp", "Enable distance-weighted compound", OFFSET(enable_dist_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1147. { "enable-onesided-comp", "Enable one sided compound", OFFSET(enable_onesided_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1148. { "enable-interinter-wedge", "Enable interinter wedge compound", OFFSET(enable_interinter_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1149. { "enable-interintra-wedge", "Enable interintra wedge compound", OFFSET(enable_interintra_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1150. { "enable-masked-comp", "Enable masked compound", OFFSET(enable_masked_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1151. { "enable-interintra-comp", "Enable interintra compound", OFFSET(enable_interintra_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1152. { "enable-smooth-interintra", "Enable smooth interintra mode", OFFSET(enable_smooth_interintra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
  1153. { NULL },
  1154. };
  1155. static const AVCodecDefault defaults[] = {
  1156. { "b", "0" },
  1157. { "qmin", "-1" },
  1158. { "qmax", "-1" },
  1159. { "g", "-1" },
  1160. { "keyint_min", "-1" },
  1161. { NULL },
  1162. };
  1163. static const AVClass class_aom = {
  1164. .class_name = "libaom-av1 encoder",
  1165. .item_name = av_default_item_name,
  1166. .option = options,
  1167. .version = LIBAVUTIL_VERSION_INT,
  1168. };
  1169. AVCodec ff_libaom_av1_encoder = {
  1170. .name = "libaom-av1",
  1171. .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
  1172. .type = AVMEDIA_TYPE_VIDEO,
  1173. .id = AV_CODEC_ID_AV1,
  1174. .priv_data_size = sizeof(AOMContext),
  1175. .init = av1_init,
  1176. .encode2 = aom_encode,
  1177. .close = aom_free,
  1178. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  1179. .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
  1180. .priv_class = &class_aom,
  1181. .defaults = defaults,
  1182. .init_static_data = av1_init_static,
  1183. .wrapper_name = "libaom",
  1184. };