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