libaomenc.c 65 KB

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