1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "aom_mem/aom_mem.h"
15 #include "config/aom_config.h"
16 #include "config/aom_version.h"
17 
18 #include "aom_ports/aom_once.h"
19 #include "aom_ports/mem_ops.h"
20 #include "aom_ports/system_state.h"
21 
22 #include "aom/aom_encoder.h"
23 #include "aom/internal/aom_codec_internal.h"
24 
25 #include "av1/av1_iface_common.h"
26 #include "av1/encoder/bitstream.h"
27 #include "av1/encoder/encoder.h"
28 #include "av1/encoder/ethread.h"
29 #include "av1/encoder/firstpass.h"
30 #include "av1/arg_defs.h"
31 
32 #include "common/args_helper.h"
33 
34 #define MAG_SIZE (4)
35 
36 struct av1_extracfg {
37   int cpu_used;
38   unsigned int enable_auto_alt_ref;
39   unsigned int enable_auto_bwd_ref;
40   unsigned int noise_sensitivity;
41   unsigned int sharpness;
42   unsigned int static_thresh;
43   unsigned int row_mt;
44   unsigned int tile_columns;  // log2 number of tile columns
45   unsigned int tile_rows;     // log2 number of tile rows
46   unsigned int enable_tpl_model;
47   unsigned int enable_keyframe_filtering;
48   unsigned int arnr_max_frames;
49   unsigned int arnr_strength;
50   unsigned int min_gf_interval;
51   unsigned int max_gf_interval;
52   unsigned int gf_min_pyr_height;
53   unsigned int gf_max_pyr_height;
54   aom_tune_metric tuning;
55   const char *vmaf_model_path;
56   unsigned int cq_level;  // constrained quality level
57   unsigned int rc_max_intra_bitrate_pct;
58   unsigned int rc_max_inter_bitrate_pct;
59   unsigned int gf_cbr_boost_pct;
60   unsigned int lossless;
61   unsigned int enable_cdef;
62   unsigned int enable_restoration;
63   unsigned int force_video_mode;
64   unsigned int enable_obmc;
65   unsigned int disable_trellis_quant;
66   unsigned int enable_qm;
67   unsigned int qm_y;
68   unsigned int qm_u;
69   unsigned int qm_v;
70   unsigned int qm_min;
71   unsigned int qm_max;
72   unsigned int num_tg;
73   unsigned int mtu_size;
74 
75   aom_timing_info_type_t timing_info_type;
76   unsigned int frame_parallel_decoding_mode;
77   int enable_dual_filter;
78   unsigned int enable_chroma_deltaq;
79   AQ_MODE aq_mode;
80   DELTAQ_MODE deltaq_mode;
81   int deltalf_mode;
82   unsigned int frame_periodic_boost;
83   aom_bit_depth_t bit_depth;
84   aom_tune_content content;
85   aom_color_primaries_t color_primaries;
86   aom_transfer_characteristics_t transfer_characteristics;
87   aom_matrix_coefficients_t matrix_coefficients;
88   aom_chroma_sample_position_t chroma_sample_position;
89   int color_range;
90   int render_width;
91   int render_height;
92   aom_superblock_size_t superblock_size;
93   unsigned int single_tile_decoding;
94   int error_resilient_mode;
95   int s_frame_mode;
96 
97   int film_grain_test_vector;
98   const char *film_grain_table_filename;
99   unsigned int motion_vector_unit_test;
100   unsigned int cdf_update_mode;
101   int enable_rect_partitions;    // enable rectangular partitions for sequence
102   int enable_ab_partitions;      // enable AB partitions for sequence
103   int enable_1to4_partitions;    // enable 1:4 and 4:1 partitions for sequence
104   int min_partition_size;        // min partition size [4,8,16,32,64,128]
105   int max_partition_size;        // max partition size [4,8,16,32,64,128]
106   int enable_intra_edge_filter;  // enable intra-edge filter for sequence
107   int enable_order_hint;         // enable order hint for sequence
108   int enable_tx64;               // enable 64-pt transform usage for sequence
109   int enable_flip_idtx;          // enable flip and identity transform types
110   int enable_rect_tx;        // enable rectangular transform usage for sequence
111   int enable_dist_wtd_comp;  // enable dist wtd compound for sequence
112   int max_reference_frames;  // maximum number of references per frame
113   int enable_reduced_reference_set;  // enable reduced set of references
114   int enable_ref_frame_mvs;          // sequence level
115   int allow_ref_frame_mvs;           // frame level
116   int enable_masked_comp;            // enable masked compound for sequence
117   int enable_onesided_comp;          // enable one sided compound for sequence
118   int enable_interintra_comp;        // enable interintra compound for sequence
119   int enable_smooth_interintra;      // enable smooth interintra mode usage
120   int enable_diff_wtd_comp;          // enable diff-wtd compound usage
121   int enable_interinter_wedge;       // enable interinter-wedge compound usage
122   int enable_interintra_wedge;       // enable interintra-wedge compound usage
123   int enable_global_motion;          // enable global motion usage for sequence
124   int enable_warped_motion;          // sequence level
125   int allow_warped_motion;           // frame level
126   int enable_filter_intra;           // enable filter intra for sequence
127   int enable_smooth_intra;           // enable smooth intra modes for sequence
128   int enable_paeth_intra;            // enable Paeth intra mode for sequence
129   int enable_cfl_intra;              // enable CFL uv intra mode for sequence
130   int enable_superres;
131   int enable_overlay;  // enable overlay for filtered arf frames
132   int enable_palette;
133   int enable_intrabc;
134   int enable_angle_delta;
135 #if CONFIG_DENOISE
136   float noise_level;
137   int noise_block_size;
138   int enable_dnl_denoising;
139 #endif
140 
141   unsigned int chroma_subsampling_x;
142   unsigned int chroma_subsampling_y;
143   int reduced_tx_type_set;
144   int use_intra_dct_only;
145   int use_inter_dct_only;
146   int use_intra_default_tx_only;
147   int quant_b_adapt;
148   unsigned int vbr_corpus_complexity_lap;
149   AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
150   // Bit mask to specify which tier each of the 32 possible operating points
151   // conforms to.
152   unsigned int tier_mask;
153   // min_cr / 100 is the target minimum compression ratio for each frame.
154   unsigned int min_cr;
155   COST_UPDATE_TYPE coeff_cost_upd_freq;
156   COST_UPDATE_TYPE mode_cost_upd_freq;
157   COST_UPDATE_TYPE mv_cost_upd_freq;
158   unsigned int ext_tile_debug;
159   unsigned int sb_multipass_unit_test;
160 };
161 
162 static struct av1_extracfg default_extra_cfg = {
163   0,              // cpu_used
164   1,              // enable_auto_alt_ref
165   0,              // enable_auto_bwd_ref
166   0,              // noise_sensitivity
167   0,              // sharpness
168   0,              // static_thresh
169   1,              // row_mt
170   0,              // tile_columns
171   0,              // tile_rows
172   1,              // enable_tpl_model
173   1,              // enable_keyframe_filtering
174   7,              // arnr_max_frames
175   5,              // arnr_strength
176   0,              // min_gf_interval; 0 -> default decision
177   0,              // max_gf_interval; 0 -> default decision
178   0,              // gf_min_pyr_height
179   5,              // gf_max_pyr_height
180   AOM_TUNE_PSNR,  // tuning
181   "/usr/local/share/model/vmaf_v0.6.1.pkl",  // VMAF model path
182   10,                                        // cq_level
183   0,                                         // rc_max_intra_bitrate_pct
184   0,                                         // rc_max_inter_bitrate_pct
185   0,                                         // gf_cbr_boost_pct
186   0,                                         // lossless
187   1,                                         // enable_cdef
188   1,                                         // enable_restoration
189   0,                                         // force_video_mode
190   1,                                         // enable_obmc
191   3,                                         // disable_trellis_quant
192   0,                                         // enable_qm
193   DEFAULT_QM_Y,                              // qm_y
194   DEFAULT_QM_U,                              // qm_u
195   DEFAULT_QM_V,                              // qm_v
196   DEFAULT_QM_FIRST,                          // qm_min
197   DEFAULT_QM_LAST,                           // qm_max
198   1,                                         // max number of tile groups
199   0,                                         // mtu_size
200   AOM_TIMING_UNSPECIFIED,       // No picture timing signaling in bitstream
201   0,                            // frame_parallel_decoding_mode
202   1,                            // enable dual filter
203   0,                            // enable delta quant in chroma planes
204   NO_AQ,                        // aq_mode
205   DELTA_Q_OBJECTIVE,            // deltaq_mode
206   0,                            // delta lf mode
207   0,                            // frame_periodic_boost
208   AOM_BITS_8,                   // Bit depth
209   AOM_CONTENT_DEFAULT,          // content
210   AOM_CICP_CP_UNSPECIFIED,      // CICP color primaries
211   AOM_CICP_TC_UNSPECIFIED,      // CICP transfer characteristics
212   AOM_CICP_MC_UNSPECIFIED,      // CICP matrix coefficients
213   AOM_CSP_UNKNOWN,              // chroma sample position
214   0,                            // color range
215   0,                            // render width
216   0,                            // render height
217   AOM_SUPERBLOCK_SIZE_DYNAMIC,  // superblock_size
218   1,                            // this depends on large_scale_tile.
219   0,                            // error_resilient_mode off by default.
220   0,                            // s_frame_mode off by default.
221   0,                            // film_grain_test_vector
222   0,                            // film_grain_table_filename
223   0,                            // motion_vector_unit_test
224   1,                            // CDF update mode
225   1,                            // enable rectangular partitions
226   1,                            // enable ab shape partitions
227   1,                            // enable 1:4 and 4:1 partitions
228   4,                            // min_partition_size
229   128,                          // max_partition_size
230   1,                            // enable intra edge filter
231   1,                            // frame order hint
232   1,                            // enable 64-pt transform usage
233   1,                            // enable flip and identity transform
234   1,                            // enable rectangular transform usage
235   1,                            // dist-wtd compound
236   7,                            // max_reference_frames
237   0,                            // enable_reduced_reference_set
238   1,                            // enable_ref_frame_mvs sequence level
239   1,                            // allow ref_frame_mvs frame level
240   1,                            // enable masked compound at sequence level
241   1,                            // enable one sided compound at sequence level
242   1,                            // enable interintra compound at sequence level
243   1,                            // enable smooth interintra mode
244   1,                            // enable difference-weighted compound
245   1,                            // enable interinter wedge compound
246   1,                            // enable interintra wedge compound
247   1,                            // enable_global_motion usage
248   1,                            // enable_warped_motion at sequence level
249   1,                            // allow_warped_motion at frame level
250   1,                            // enable filter intra at sequence level
251   1,                            // enable smooth intra modes usage for sequence
252   1,                            // enable Paeth intra mode usage for sequence
253   1,                            // enable CFL uv intra mode usage for sequence
254   1,                            // superres
255   1,                            // enable overlay
256   1,                            // enable palette
257   !CONFIG_SHARP_SETTINGS,       // enable intrabc
258   1,                            // enable angle delta
259 #if CONFIG_DENOISE
260   0,   // noise_level
261   32,  // noise_block_size
262   1,   // enable_dnl_denoising
263 #endif
264   0,  // chroma_subsampling_x
265   0,  // chroma_subsampling_y
266   0,  // reduced_tx_type_set
267   0,  // use_intra_dct_only
268   0,  // use_inter_dct_only
269   0,  // use_intra_default_tx_only
270   0,  // quant_b_adapt
271   0,  // vbr_corpus_complexity_lap
272   {
273       SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
274       SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
275       SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
276       SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
277       SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
278       SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
279       SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
280   },            // target_seq_level_idx
281   0,            // tier_mask
282   0,            // min_cr
283   COST_UPD_SB,  // coeff_cost_upd_freq
284   COST_UPD_SB,  // mode_cost_upd_freq
285   COST_UPD_SB,  // mv_cost_upd_freq
286   0,            // ext_tile_debug
287   0,            // sb_multipass_unit_test
288 };
289 
290 struct aom_codec_alg_priv {
291   aom_codec_priv_t base;
292   aom_codec_enc_cfg_t cfg;
293   struct av1_extracfg extra_cfg;
294   aom_rational64_t timestamp_ratio;
295   aom_codec_pts_t pts_offset;
296   unsigned char pts_offset_initialized;
297   AV1EncoderConfig oxcf;
298   AV1_COMP *cpi;
299   unsigned char *cx_data;
300   size_t cx_data_sz;
301   unsigned char *pending_cx_data;
302   size_t pending_cx_data_sz;
303   int pending_frame_count;
304   size_t pending_frame_sizes[8];
305   aom_image_t preview_img;
306   aom_enc_frame_flags_t next_frame_flags;
307   aom_codec_pkt_list_decl(256) pkt_list;
308   unsigned int fixed_kf_cntr;
309   // BufferPool that holds all reference frames.
310   BufferPool *buffer_pool;
311 
312   // lookahead instance variables
313   BufferPool *buffer_pool_lap;
314   AV1_COMP *cpi_lap;
315   FIRSTPASS_STATS *frame_stats_buffer;
316   // Number of stats buffers required for look ahead
317   int num_lap_buffers;
318   STATS_BUFFER_CTX stats_buf_context;
319 };
320 
gcd(int64_t a,int b)321 static INLINE int gcd(int64_t a, int b) {
322   int remainder;
323   while (b > 0) {
324     remainder = (int)(a % b);
325     a = b;
326     b = remainder;
327   }
328 
329   return (int)a;
330 }
331 
reduce_ratio(aom_rational64_t * ratio)332 static INLINE void reduce_ratio(aom_rational64_t *ratio) {
333   const int denom = gcd(ratio->num, ratio->den);
334   ratio->num /= denom;
335   ratio->den /= denom;
336 }
337 
update_error_state(aom_codec_alg_priv_t * ctx,const struct aom_internal_error_info * error)338 static aom_codec_err_t update_error_state(
339     aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
340   const aom_codec_err_t res = error->error_code;
341 
342   if (res != AOM_CODEC_OK)
343     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
344 
345   return res;
346 }
347 
348 #undef ERROR
349 #define ERROR(str)                  \
350   do {                              \
351     ctx->base.err_detail = str;     \
352     return AOM_CODEC_INVALID_PARAM; \
353   } while (0)
354 
355 #define RANGE_CHECK(p, memb, lo, hi)                   \
356   do {                                                 \
357     if (!((p)->memb >= (lo) && (p)->memb <= (hi)))     \
358       ERROR(#memb " out of range [" #lo ".." #hi "]"); \
359   } while (0)
360 
361 #define RANGE_CHECK_HI(p, memb, hi)                                     \
362   do {                                                                  \
363     if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
364   } while (0)
365 
366 #define RANGE_CHECK_BOOL(p, memb)                                     \
367   do {                                                                \
368     if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
369   } while (0)
370 
validate_config(aom_codec_alg_priv_t * ctx,const aom_codec_enc_cfg_t * cfg,const struct av1_extracfg * extra_cfg)371 static aom_codec_err_t validate_config(aom_codec_alg_priv_t *ctx,
372                                        const aom_codec_enc_cfg_t *cfg,
373                                        const struct av1_extracfg *extra_cfg) {
374   RANGE_CHECK(cfg, g_w, 1, 65535);  // 16 bits available
375   RANGE_CHECK(cfg, g_h, 1, 65535);  // 16 bits available
376   RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
377   RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
378   RANGE_CHECK_HI(cfg, g_profile, MAX_PROFILES - 1);
379 
380   RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
381   RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
382   RANGE_CHECK_BOOL(extra_cfg, lossless);
383   RANGE_CHECK_HI(extra_cfg, aq_mode, AQ_MODE_COUNT - 1);
384   RANGE_CHECK_HI(extra_cfg, deltaq_mode, DELTA_Q_MODE_COUNT - 1);
385   RANGE_CHECK_HI(extra_cfg, deltalf_mode, 1);
386   RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
387   RANGE_CHECK_HI(cfg, g_usage, 1);
388   RANGE_CHECK_HI(cfg, g_threads, MAX_NUM_THREADS);
389   RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
390   RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
391   RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
392   RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
393   RANGE_CHECK(cfg, kf_mode, AOM_KF_DISABLED, AOM_KF_AUTO);
394   RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
395   RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_LAST_PASS);
396   if (cfg->g_pass == AOM_RC_ONE_PASS) {
397     RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_TOTAL_BUFFERS);
398   } else {
399     RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
400   }
401   RANGE_CHECK_HI(extra_cfg, min_gf_interval, MAX_LAG_BUFFERS - 1);
402   RANGE_CHECK_HI(extra_cfg, max_gf_interval, MAX_LAG_BUFFERS - 1);
403   if (extra_cfg->max_gf_interval > 0) {
404     RANGE_CHECK(extra_cfg, max_gf_interval,
405                 AOMMAX(2, extra_cfg->min_gf_interval), (MAX_LAG_BUFFERS - 1));
406   }
407   RANGE_CHECK_HI(extra_cfg, gf_min_pyr_height, 5);
408   RANGE_CHECK_HI(extra_cfg, gf_max_pyr_height, 5);
409   if (extra_cfg->gf_min_pyr_height > extra_cfg->gf_max_pyr_height) {
410     ERROR(
411         "gf_min_pyr_height must be less than or equal to "
412         "gf_max_pyramid_height");
413   }
414 
415   RANGE_CHECK_HI(cfg, rc_resize_mode, RESIZE_MODES - 1);
416   RANGE_CHECK(cfg, rc_resize_denominator, SCALE_NUMERATOR,
417               SCALE_NUMERATOR << 1);
418   RANGE_CHECK(cfg, rc_resize_kf_denominator, SCALE_NUMERATOR,
419               SCALE_NUMERATOR << 1);
420   RANGE_CHECK_HI(cfg, rc_superres_mode, AOM_SUPERRES_AUTO);
421   RANGE_CHECK(cfg, rc_superres_denominator, SCALE_NUMERATOR,
422               SCALE_NUMERATOR << 1);
423   RANGE_CHECK(cfg, rc_superres_kf_denominator, SCALE_NUMERATOR,
424               SCALE_NUMERATOR << 1);
425   RANGE_CHECK(cfg, rc_superres_qthresh, 1, 63);
426   RANGE_CHECK(cfg, rc_superres_kf_qthresh, 1, 63);
427   RANGE_CHECK_HI(extra_cfg, cdf_update_mode, 2);
428 
429   RANGE_CHECK_HI(extra_cfg, motion_vector_unit_test, 2);
430   RANGE_CHECK_HI(extra_cfg, sb_multipass_unit_test, 1);
431   RANGE_CHECK_HI(extra_cfg, ext_tile_debug, 1);
432   RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 1);
433   RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
434   RANGE_CHECK(extra_cfg, cpu_used, 0, 9);
435   RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
436   RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
437               AOM_SUPERBLOCK_SIZE_DYNAMIC);
438   RANGE_CHECK_HI(cfg, large_scale_tile, 1);
439   RANGE_CHECK_HI(extra_cfg, single_tile_decoding, 1);
440 
441   RANGE_CHECK_HI(extra_cfg, row_mt, 1);
442 
443   RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
444   RANGE_CHECK_HI(extra_cfg, tile_rows, 6);
445 
446   RANGE_CHECK_HI(cfg, monochrome, 1);
447 
448   if (cfg->large_scale_tile && extra_cfg->aq_mode)
449     ERROR(
450         "Adaptive quantization are not supported in large scale tile "
451         "coding.");
452 
453   RANGE_CHECK_HI(extra_cfg, sharpness, 7);
454   RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
455   RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
456   RANGE_CHECK_HI(extra_cfg, cq_level, 63);
457   RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
458   RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
459   RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
460 
461   if (cfg->g_pass == AOM_RC_LAST_PASS) {
462     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
463     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
464     const FIRSTPASS_STATS *stats;
465 
466     if (cfg->rc_twopass_stats_in.buf == NULL)
467       ERROR("rc_twopass_stats_in.buf not set.");
468 
469     if (cfg->rc_twopass_stats_in.sz % packet_sz)
470       ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
471 
472     if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
473       ERROR("rc_twopass_stats_in requires at least two packets.");
474 
475     stats =
476         (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
477 
478     if ((int)(stats->count + 0.5) != n_packets - 1)
479       ERROR("rc_twopass_stats_in missing EOS stats packet");
480   }
481 
482   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
483       cfg->g_bit_depth > AOM_BITS_10) {
484     ERROR("Codec bit-depth 12 not supported in profile < 2");
485   }
486   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
487       cfg->g_input_bit_depth > 10) {
488     ERROR("Source bit-depth 12 not supported in profile < 2");
489   }
490 
491   if (cfg->rc_end_usage == AOM_Q) {
492     RANGE_CHECK_HI(cfg, use_fixed_qp_offsets, 1);
493     for (int i = 0; i < FIXED_QP_OFFSET_COUNT; ++i) {
494       RANGE_CHECK_HI(cfg, fixed_qp_offsets[i], 63);
495     }
496   } else {
497     if (cfg->use_fixed_qp_offsets > 0) {
498       ERROR("--use_fixed_qp_offsets can only be used with --end-usage=q");
499     }
500     for (int i = 0; i < FIXED_QP_OFFSET_COUNT; ++i) {
501       if (cfg->fixed_qp_offsets[i] >= 0) {
502         ERROR("--fixed_qp_offsets can only be used with --end-usage=q");
503       }
504     }
505   }
506 
507   RANGE_CHECK(extra_cfg, color_primaries, AOM_CICP_CP_BT_709,
508               AOM_CICP_CP_EBU_3213);  // Need to check range more precisely to
509                                       // check for reserved values?
510   RANGE_CHECK(extra_cfg, transfer_characteristics, AOM_CICP_TC_BT_709,
511               AOM_CICP_TC_HLG);
512   RANGE_CHECK(extra_cfg, matrix_coefficients, AOM_CICP_MC_IDENTITY,
513               AOM_CICP_MC_ICTCP);
514   RANGE_CHECK(extra_cfg, color_range, 0, 1);
515 
516   /* Average corpus complexity is supported only in the case of single pass
517    * VBR*/
518   if (cfg->g_pass == AOM_RC_ONE_PASS && cfg->rc_end_usage == AOM_VBR)
519     RANGE_CHECK_HI(extra_cfg, vbr_corpus_complexity_lap,
520                    MAX_VBR_CORPUS_COMPLEXITY);
521   else if (extra_cfg->vbr_corpus_complexity_lap != 0)
522     ERROR(
523         "VBR corpus complexity is supported only in the case of single pass "
524         "VBR mode.");
525 
526 #if !CONFIG_TUNE_VMAF
527   if (extra_cfg->tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
528       extra_cfg->tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
529     ERROR(
530         "This error may be related to the wrong configuration options: try to "
531         "set -DCONFIG_TUNE_VMAF=1 at the time CMake is run.");
532   }
533 #endif
534 
535 #if !CONFIG_USE_VMAF_RC
536   if (extra_cfg->tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
537     ERROR(
538         "This error may be related to the wrong configuration options: try to "
539         "set -DCONFIG_TUNE_VMAF=1 and -DCONFIG_USE_VMAF_RC=1 at the time CMake"
540         " is run.");
541   }
542 #endif
543 
544 #if CONFIG_TUNE_VMAF
545   RANGE_CHECK(extra_cfg, tuning, AOM_TUNE_PSNR, AOM_TUNE_VMAF_NEG_MAX_GAIN);
546 #else
547   RANGE_CHECK(extra_cfg, tuning, AOM_TUNE_PSNR, AOM_TUNE_SSIM);
548 #endif
549 
550   RANGE_CHECK(extra_cfg, timing_info_type, AOM_TIMING_UNSPECIFIED,
551               AOM_TIMING_DEC_MODEL);
552 
553   RANGE_CHECK(extra_cfg, film_grain_test_vector, 0, 16);
554 
555   if (extra_cfg->lossless) {
556     if (extra_cfg->aq_mode != 0)
557       ERROR("Only --aq_mode=0 can be used with --lossless=1.");
558     if (extra_cfg->enable_chroma_deltaq)
559       ERROR("Only --enable_chroma_deltaq=0 can be used with --lossless=1.");
560   }
561 
562   RANGE_CHECK(extra_cfg, max_reference_frames, 3, 7);
563   RANGE_CHECK(extra_cfg, enable_reduced_reference_set, 0, 1);
564   RANGE_CHECK_HI(extra_cfg, chroma_subsampling_x, 1);
565   RANGE_CHECK_HI(extra_cfg, chroma_subsampling_y, 1);
566 
567   RANGE_CHECK_HI(extra_cfg, disable_trellis_quant, 3);
568   RANGE_CHECK(extra_cfg, coeff_cost_upd_freq, 0, 3);
569   RANGE_CHECK(extra_cfg, mode_cost_upd_freq, 0, 3);
570   RANGE_CHECK(extra_cfg, mv_cost_upd_freq, 0, 3);
571 
572   RANGE_CHECK(extra_cfg, min_partition_size, 4, 128);
573   RANGE_CHECK(extra_cfg, max_partition_size, 4, 128);
574   RANGE_CHECK_HI(extra_cfg, min_partition_size, extra_cfg->max_partition_size);
575 
576   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
577     const int level_idx = extra_cfg->target_seq_level_idx[i];
578     if (!is_valid_seq_level_idx(level_idx) && level_idx != SEQ_LEVELS) {
579       ERROR("Target sequence level index is invalid");
580     }
581   }
582 
583   return AOM_CODEC_OK;
584 }
585 
validate_img(aom_codec_alg_priv_t * ctx,const aom_image_t * img)586 static aom_codec_err_t validate_img(aom_codec_alg_priv_t *ctx,
587                                     const aom_image_t *img) {
588   switch (img->fmt) {
589     case AOM_IMG_FMT_YV12:
590     case AOM_IMG_FMT_I420:
591     case AOM_IMG_FMT_YV1216:
592     case AOM_IMG_FMT_I42016: break;
593     case AOM_IMG_FMT_I444:
594     case AOM_IMG_FMT_I44416:
595       if (ctx->cfg.g_profile == (unsigned int)PROFILE_0 &&
596           !ctx->cfg.monochrome) {
597         ERROR("Invalid image format. I444 images not supported in profile.");
598       }
599       break;
600     case AOM_IMG_FMT_I422:
601     case AOM_IMG_FMT_I42216:
602       if (ctx->cfg.g_profile != (unsigned int)PROFILE_2) {
603         ERROR("Invalid image format. I422 images not supported in profile.");
604       }
605       break;
606     default:
607       ERROR(
608           "Invalid image format. Only YV12, I420, I422, I444 images are "
609           "supported.");
610       break;
611   }
612 
613   if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
614     ERROR("Image size must match encoder init configuration size");
615 
616   return AOM_CODEC_OK;
617 }
618 
get_image_bps(const aom_image_t * img)619 static int get_image_bps(const aom_image_t *img) {
620   switch (img->fmt) {
621     case AOM_IMG_FMT_YV12:
622     case AOM_IMG_FMT_I420: return 12;
623     case AOM_IMG_FMT_I422: return 16;
624     case AOM_IMG_FMT_I444: return 24;
625     case AOM_IMG_FMT_YV1216:
626     case AOM_IMG_FMT_I42016: return 24;
627     case AOM_IMG_FMT_I42216: return 32;
628     case AOM_IMG_FMT_I44416: return 48;
629     default: assert(0 && "Invalid image format"); break;
630   }
631   return 0;
632 }
633 
634 // Set appropriate options to disable frame super-resolution.
disable_superres(SuperResCfg * const superres_cfg)635 static void disable_superres(SuperResCfg *const superres_cfg) {
636   superres_cfg->superres_mode = AOM_SUPERRES_NONE;
637   superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
638   superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
639   superres_cfg->superres_qthresh = 255;
640   superres_cfg->superres_kf_qthresh = 255;
641 }
642 
update_default_encoder_config(const cfg_options_t * cfg,struct av1_extracfg * extra_cfg)643 static void update_default_encoder_config(const cfg_options_t *cfg,
644                                           struct av1_extracfg *extra_cfg) {
645   extra_cfg->enable_cdef = (cfg->disable_cdef == 0);
646   extra_cfg->enable_restoration = (cfg->disable_lr == 0);
647   extra_cfg->superblock_size = (cfg->super_block_size == 64)
648                                    ? AOM_SUPERBLOCK_SIZE_64X64
649                                    : (cfg->super_block_size == 128)
650                                          ? AOM_SUPERBLOCK_SIZE_128X128
651                                          : AOM_SUPERBLOCK_SIZE_DYNAMIC;
652   extra_cfg->enable_warped_motion = (cfg->disable_warp_motion == 0);
653   extra_cfg->enable_dist_wtd_comp = (cfg->disable_dist_wtd_comp == 0);
654   extra_cfg->enable_diff_wtd_comp = (cfg->disable_diff_wtd_comp == 0);
655   extra_cfg->enable_dual_filter = (cfg->disable_dual_filter == 0);
656   extra_cfg->enable_angle_delta = (cfg->disable_intra_angle_delta == 0);
657   extra_cfg->enable_rect_partitions = (cfg->disable_rect_partition_type == 0);
658   extra_cfg->enable_ab_partitions = (cfg->disable_ab_partition_type == 0);
659   extra_cfg->enable_1to4_partitions = (cfg->disable_1to4_partition_type == 0);
660   extra_cfg->max_partition_size = cfg->max_partition_size;
661   extra_cfg->min_partition_size = cfg->min_partition_size;
662   extra_cfg->enable_intra_edge_filter = (cfg->disable_intra_edge_filter == 0);
663   extra_cfg->enable_tx64 = (cfg->disable_tx_64x64 == 0);
664   extra_cfg->enable_flip_idtx = (cfg->disable_flip_idtx == 0);
665   extra_cfg->enable_masked_comp = (cfg->disable_masked_comp == 0);
666   extra_cfg->enable_interintra_comp = (cfg->disable_inter_intra_comp == 0);
667   extra_cfg->enable_smooth_interintra = (cfg->disable_smooth_inter_intra == 0);
668   extra_cfg->enable_interinter_wedge = (cfg->disable_inter_inter_wedge == 0);
669   extra_cfg->enable_interintra_wedge = (cfg->disable_inter_intra_wedge == 0);
670   extra_cfg->enable_global_motion = (cfg->disable_global_motion == 0);
671   extra_cfg->enable_filter_intra = (cfg->disable_filter_intra == 0);
672   extra_cfg->enable_smooth_intra = (cfg->disable_smooth_intra == 0);
673   extra_cfg->enable_paeth_intra = (cfg->disable_paeth_intra == 0);
674   extra_cfg->enable_cfl_intra = (cfg->disable_cfl == 0);
675   extra_cfg->enable_obmc = (cfg->disable_obmc == 0);
676   extra_cfg->enable_palette = (cfg->disable_palette == 0);
677   extra_cfg->enable_intrabc = (cfg->disable_intrabc == 0);
678   extra_cfg->disable_trellis_quant = cfg->disable_trellis_quant;
679   extra_cfg->allow_ref_frame_mvs = (cfg->disable_ref_frame_mv == 0);
680   extra_cfg->enable_ref_frame_mvs = (cfg->disable_ref_frame_mv == 0);
681   extra_cfg->enable_onesided_comp = (cfg->disable_one_sided_comp == 0);
682   extra_cfg->enable_reduced_reference_set = cfg->reduced_reference_set;
683   extra_cfg->reduced_tx_type_set = cfg->reduced_tx_type_set;
684 }
685 
convert_qp_offset(int cq_level,int q_offset,int bit_depth)686 static double convert_qp_offset(int cq_level, int q_offset, int bit_depth) {
687   const double base_q_val = av1_convert_qindex_to_q(cq_level, bit_depth);
688   const int new_q_index_offset = av1_quantizer_to_qindex(q_offset);
689   const int new_q_index = AOMMAX(cq_level - new_q_index_offset, 0);
690   const double new_q_val = av1_convert_qindex_to_q(new_q_index, bit_depth);
691   return (base_q_val - new_q_val);
692 }
693 
get_modeled_qp_offset(int cq_level,int level,int bit_depth)694 static double get_modeled_qp_offset(int cq_level, int level, int bit_depth) {
695   // 80% for keyframe was derived empirically.
696   // 40% similar to rc_pick_q_and_bounds_one_pass_vbr() for Q mode ARF.
697   // Rest derived similar to rc_pick_q_and_bounds_two_pass()
698   static const int percents[FIXED_QP_OFFSET_COUNT] = { 76, 60, 30, 15, 8 };
699   const double q_val = av1_convert_qindex_to_q(cq_level, bit_depth);
700   return q_val * percents[level] / 100;
701 }
702 
set_encoder_config(AV1EncoderConfig * oxcf,const aom_codec_enc_cfg_t * cfg,struct av1_extracfg * extra_cfg)703 static aom_codec_err_t set_encoder_config(AV1EncoderConfig *oxcf,
704                                           const aom_codec_enc_cfg_t *cfg,
705                                           struct av1_extracfg *extra_cfg) {
706   if (cfg->encoder_cfg.init_by_cfg_file) {
707     update_default_encoder_config(&cfg->encoder_cfg, extra_cfg);
708   }
709 
710   TuneCfg *const tune_cfg = &oxcf->tune_cfg;
711 
712   FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
713 
714   TileConfig *const tile_cfg = &oxcf->tile_cfg;
715 
716   ResizeCfg *const resize_cfg = &oxcf->resize_cfg;
717 
718   GFConfig *const gf_cfg = &oxcf->gf_cfg;
719 
720   PartitionCfg *const part_cfg = &oxcf->part_cfg;
721 
722   IntraModeCfg *const intra_mode_cfg = &oxcf->intra_mode_cfg;
723 
724   TxfmSizeTypeCfg *const txfm_cfg = &oxcf->txfm_cfg;
725 
726   CompoundTypeCfg *const comp_type_cfg = &oxcf->comp_type_cfg;
727 
728   SuperResCfg *const superres_cfg = &oxcf->superres_cfg;
729 
730   KeyFrameCfg *const kf_cfg = &oxcf->kf_cfg;
731 
732   DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
733 
734   RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
735 
736   QuantizationCfg *const q_cfg = &oxcf->q_cfg;
737 
738   ColorCfg *const color_cfg = &oxcf->color_cfg;
739 
740   InputCfg *const input_cfg = &oxcf->input_cfg;
741 
742   AlgoCfg *const algo_cfg = &oxcf->algo_cfg;
743 
744   ToolCfg *const tool_cfg = &oxcf->tool_cfg;
745 
746   const int is_vbr = cfg->rc_end_usage == AOM_VBR;
747   oxcf->profile = cfg->g_profile;
748   oxcf->max_threads = (int)cfg->g_threads;
749   oxcf->mode = (cfg->g_usage == AOM_USAGE_REALTIME) ? REALTIME : GOOD;
750 
751   // Set frame-dimension related configuration.
752   frm_dim_cfg->width = cfg->g_w;
753   frm_dim_cfg->height = cfg->g_h;
754   frm_dim_cfg->forced_max_frame_width = cfg->g_forced_max_frame_width;
755   frm_dim_cfg->forced_max_frame_height = cfg->g_forced_max_frame_height;
756   frm_dim_cfg->render_width = extra_cfg->render_width;
757   frm_dim_cfg->render_height = extra_cfg->render_height;
758 
759   // Set input video related configuration.
760   input_cfg->input_bit_depth = cfg->g_input_bit_depth;
761   // guess a frame rate if out of whack, use 30
762   input_cfg->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
763   if (cfg->g_pass == AOM_RC_LAST_PASS) {
764     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
765     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
766     input_cfg->limit = n_packets - 1;
767   } else {
768     input_cfg->limit = cfg->g_limit;
769   }
770   input_cfg->chroma_subsampling_x = extra_cfg->chroma_subsampling_x;
771   input_cfg->chroma_subsampling_y = extra_cfg->chroma_subsampling_y;
772   if (input_cfg->init_framerate > 180) {
773     input_cfg->init_framerate = 30;
774     dec_model_cfg->timing_info_present = 0;
775   }
776 
777   // Set Decoder model configuration.
778   if (extra_cfg->timing_info_type == AOM_TIMING_EQUAL ||
779       extra_cfg->timing_info_type == AOM_TIMING_DEC_MODEL) {
780     dec_model_cfg->timing_info_present = 1;
781     dec_model_cfg->timing_info.num_units_in_display_tick = cfg->g_timebase.num;
782     dec_model_cfg->timing_info.time_scale = cfg->g_timebase.den;
783     dec_model_cfg->timing_info.num_ticks_per_picture = 1;
784   } else {
785     dec_model_cfg->timing_info_present = 0;
786   }
787   if (extra_cfg->timing_info_type == AOM_TIMING_EQUAL) {
788     dec_model_cfg->timing_info.equal_picture_interval = 1;
789     dec_model_cfg->decoder_model_info_present_flag = 0;
790     dec_model_cfg->display_model_info_present_flag = 1;
791   } else if (extra_cfg->timing_info_type == AOM_TIMING_DEC_MODEL) {
792     //    if( extra_cfg->arnr_strength > 0 )
793     //    {
794     //      printf("Only --arnr-strength=0 can currently be used with
795     //      --timing-info=model."); return AOM_CODEC_INVALID_PARAM;
796     //    }
797     //    if( extra_cfg->enable_superres)
798     //    {
799     //      printf("Only --superres-mode=0 can currently be used with
800     //      --timing-info=model."); return AOM_CODEC_INVALID_PARAM;
801     //    }
802     dec_model_cfg->num_units_in_decoding_tick = cfg->g_timebase.num;
803     dec_model_cfg->timing_info.equal_picture_interval = 0;
804     dec_model_cfg->decoder_model_info_present_flag = 1;
805     dec_model_cfg->display_model_info_present_flag = 1;
806   }
807 
808   switch (cfg->g_pass) {
809     case AOM_RC_ONE_PASS: oxcf->pass = 0; break;
810     case AOM_RC_FIRST_PASS: oxcf->pass = 1; break;
811     case AOM_RC_LAST_PASS: oxcf->pass = 2; break;
812   }
813 
814   // Set Rate Control configuration.
815   rc_cfg->max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
816   rc_cfg->max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
817   rc_cfg->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
818   rc_cfg->mode = cfg->rc_end_usage;
819   rc_cfg->min_cr = extra_cfg->min_cr;
820   rc_cfg->best_allowed_q =
821       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
822   rc_cfg->worst_allowed_q =
823       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_max_quantizer);
824   rc_cfg->cq_level = av1_quantizer_to_qindex(extra_cfg->cq_level);
825   rc_cfg->under_shoot_pct = cfg->rc_undershoot_pct;
826   rc_cfg->over_shoot_pct = cfg->rc_overshoot_pct;
827   rc_cfg->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
828   rc_cfg->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
829   rc_cfg->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
830   // Convert target bandwidth from Kbit/s to Bit/s
831   rc_cfg->target_bandwidth = 1000 * cfg->rc_target_bitrate;
832   rc_cfg->drop_frames_water_mark = cfg->rc_dropframe_thresh;
833   rc_cfg->vbr_corpus_complexity_lap = extra_cfg->vbr_corpus_complexity_lap;
834   rc_cfg->vbrbias = cfg->rc_2pass_vbr_bias_pct;
835   rc_cfg->vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
836   rc_cfg->vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
837 
838   // Set Toolset related configuration.
839   tool_cfg->bit_depth = cfg->g_bit_depth;
840   tool_cfg->enable_cdef = extra_cfg->enable_cdef;
841   tool_cfg->enable_restoration =
842       (cfg->g_usage == AOM_USAGE_REALTIME) ? 0 : extra_cfg->enable_restoration;
843   tool_cfg->force_video_mode = extra_cfg->force_video_mode;
844   tool_cfg->enable_palette = extra_cfg->enable_palette;
845   // FIXME(debargha): Should this be:
846   // tool_cfg->enable_ref_frame_mvs  = extra_cfg->allow_ref_frame_mvs &
847   //                                         extra_cfg->enable_order_hint ?
848   // Disallow using temporal MVs while large_scale_tile = 1.
849   tool_cfg->enable_ref_frame_mvs =
850       extra_cfg->allow_ref_frame_mvs && !cfg->large_scale_tile;
851   tool_cfg->superblock_size = extra_cfg->superblock_size;
852   tool_cfg->enable_monochrome = cfg->monochrome;
853   tool_cfg->full_still_picture_hdr = cfg->full_still_picture_hdr;
854   tool_cfg->enable_dual_filter = extra_cfg->enable_dual_filter;
855   tool_cfg->enable_order_hint = extra_cfg->enable_order_hint;
856   tool_cfg->enable_interintra_comp = extra_cfg->enable_interintra_comp;
857   tool_cfg->ref_frame_mvs_present =
858       extra_cfg->enable_ref_frame_mvs & extra_cfg->enable_order_hint;
859   tool_cfg->enable_global_motion = extra_cfg->enable_global_motion;
860   tool_cfg->error_resilient_mode =
861       cfg->g_error_resilient | extra_cfg->error_resilient_mode;
862   tool_cfg->frame_parallel_decoding_mode =
863       extra_cfg->frame_parallel_decoding_mode;
864 
865   // Set Quantization related configuration.
866   q_cfg->using_qm = extra_cfg->enable_qm;
867   q_cfg->qm_minlevel = extra_cfg->qm_min;
868   q_cfg->qm_maxlevel = extra_cfg->qm_max;
869   q_cfg->quant_b_adapt = extra_cfg->quant_b_adapt;
870   q_cfg->enable_chroma_deltaq = extra_cfg->enable_chroma_deltaq;
871   q_cfg->aq_mode = extra_cfg->aq_mode;
872   q_cfg->deltaq_mode = extra_cfg->deltaq_mode;
873   q_cfg->use_fixed_qp_offsets =
874       cfg->use_fixed_qp_offsets && (rc_cfg->mode == AOM_Q);
875   for (int i = 0; i < FIXED_QP_OFFSET_COUNT; ++i) {
876     if (q_cfg->use_fixed_qp_offsets) {
877       if (cfg->fixed_qp_offsets[i] >= 0) {  // user-provided qp offset
878         q_cfg->fixed_qp_offsets[i] = convert_qp_offset(
879             rc_cfg->cq_level, cfg->fixed_qp_offsets[i], tool_cfg->bit_depth);
880       } else {  // auto-selected qp offset
881         q_cfg->fixed_qp_offsets[i] =
882             get_modeled_qp_offset(rc_cfg->cq_level, i, tool_cfg->bit_depth);
883       }
884     } else {
885       q_cfg->fixed_qp_offsets[i] = -1.0;
886     }
887   }
888 
889   tool_cfg->enable_deltalf_mode =
890       (q_cfg->deltaq_mode != NO_DELTA_Q) && extra_cfg->deltalf_mode;
891 
892   // Set cost update frequency configuration.
893   oxcf->cost_upd_freq.coeff = (COST_UPDATE_TYPE)extra_cfg->coeff_cost_upd_freq;
894   oxcf->cost_upd_freq.mode = (COST_UPDATE_TYPE)extra_cfg->mode_cost_upd_freq;
895   oxcf->cost_upd_freq.mv = (COST_UPDATE_TYPE)extra_cfg->mv_cost_upd_freq;
896 
897   // Set frame resize mode configuration.
898   resize_cfg->resize_mode = (RESIZE_MODE)cfg->rc_resize_mode;
899   resize_cfg->resize_scale_denominator = (uint8_t)cfg->rc_resize_denominator;
900   resize_cfg->resize_kf_scale_denominator =
901       (uint8_t)cfg->rc_resize_kf_denominator;
902   if (resize_cfg->resize_mode == RESIZE_FIXED &&
903       resize_cfg->resize_scale_denominator == SCALE_NUMERATOR &&
904       resize_cfg->resize_kf_scale_denominator == SCALE_NUMERATOR)
905     resize_cfg->resize_mode = RESIZE_NONE;
906 
907   // Set encoder algorithm related configuration.
908   algo_cfg->enable_overlay = extra_cfg->enable_overlay;
909   algo_cfg->disable_trellis_quant = extra_cfg->disable_trellis_quant;
910   algo_cfg->sharpness = extra_cfg->sharpness;
911   algo_cfg->arnr_max_frames = extra_cfg->arnr_max_frames;
912   algo_cfg->arnr_strength = extra_cfg->arnr_strength;
913   algo_cfg->cdf_update_mode = (uint8_t)extra_cfg->cdf_update_mode;
914   // TODO(any): Fix and Enable TPL for resize-mode > 0
915   algo_cfg->enable_tpl_model =
916       resize_cfg->resize_mode ? 0 : extra_cfg->enable_tpl_model;
917 
918   // Set two-pass stats configuration.
919   oxcf->twopass_stats_in = cfg->rc_twopass_stats_in;
920 
921   // Set Key frame configuration.
922   kf_cfg->fwd_kf_enabled = cfg->fwd_kf_enabled;
923   kf_cfg->auto_key =
924       cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
925   kf_cfg->key_freq_min = cfg->kf_min_dist;
926   kf_cfg->key_freq_max = cfg->kf_max_dist;
927   kf_cfg->sframe_dist = cfg->sframe_dist;
928   kf_cfg->sframe_mode = cfg->sframe_mode;
929   kf_cfg->enable_sframe = extra_cfg->s_frame_mode;
930   kf_cfg->enable_keyframe_filtering = extra_cfg->enable_keyframe_filtering;
931   kf_cfg->enable_intrabc = extra_cfg->enable_intrabc;
932 
933   oxcf->speed = extra_cfg->cpu_used;
934 
935   // Set Color related configuration.
936   color_cfg->color_primaries = extra_cfg->color_primaries;
937   color_cfg->transfer_characteristics = extra_cfg->transfer_characteristics;
938   color_cfg->matrix_coefficients = extra_cfg->matrix_coefficients;
939   color_cfg->color_range = extra_cfg->color_range;
940   color_cfg->chroma_sample_position = extra_cfg->chroma_sample_position;
941 
942   // Set Group of frames configuration.
943   gf_cfg->lag_in_frames = clamp(cfg->g_lag_in_frames, 0, MAX_LAG_BUFFERS);
944   gf_cfg->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
945   gf_cfg->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
946   gf_cfg->min_gf_interval = extra_cfg->min_gf_interval;
947   gf_cfg->max_gf_interval = extra_cfg->max_gf_interval;
948   gf_cfg->gf_min_pyr_height = extra_cfg->gf_min_pyr_height;
949   gf_cfg->gf_max_pyr_height = extra_cfg->gf_max_pyr_height;
950 
951   // Set tune related configuration.
952   tune_cfg->tuning = extra_cfg->tuning;
953   tune_cfg->vmaf_model_path = extra_cfg->vmaf_model_path;
954   tune_cfg->content = extra_cfg->content;
955   if (cfg->large_scale_tile) {
956     tune_cfg->film_grain_test_vector = 0;
957     tune_cfg->film_grain_table_filename = NULL;
958   } else {
959     tune_cfg->film_grain_test_vector = extra_cfg->film_grain_test_vector;
960     tune_cfg->film_grain_table_filename = extra_cfg->film_grain_table_filename;
961   }
962 #if CONFIG_DENOISE
963   oxcf->noise_level = extra_cfg->noise_level;
964   oxcf->noise_block_size = extra_cfg->noise_block_size;
965   oxcf->enable_dnl_denoising = extra_cfg->enable_dnl_denoising;
966 #endif
967 
968 #if CONFIG_AV1_TEMPORAL_DENOISING
969   // Temporal denoiser is for nonrd pickmode so disable it for speed < 7.
970   // Also disable it for speed 7 for now since it needs to be modified for
971   // the check_partition_merge_mode feature.
972   if (cfg->g_bit_depth == AOM_BITS_8 && oxcf->speed > 7) {
973     oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
974   } else {
975     oxcf->noise_sensitivity = 0;
976   }
977 #endif
978   // Set Tile related configuration.
979   tile_cfg->num_tile_groups = extra_cfg->num_tg;
980   // In large-scale tile encoding mode, num_tile_groups is always 1.
981   if (cfg->large_scale_tile) tile_cfg->num_tile_groups = 1;
982   tile_cfg->mtu = extra_cfg->mtu_size;
983   tile_cfg->enable_large_scale_tile = cfg->large_scale_tile;
984   tile_cfg->enable_single_tile_decoding =
985       (tile_cfg->enable_large_scale_tile) ? extra_cfg->single_tile_decoding : 0;
986   tile_cfg->tile_columns = extra_cfg->tile_columns;
987   tile_cfg->tile_rows = extra_cfg->tile_rows;
988   tile_cfg->tile_width_count = AOMMIN(cfg->tile_width_count, MAX_TILE_COLS);
989   tile_cfg->tile_height_count = AOMMIN(cfg->tile_height_count, MAX_TILE_ROWS);
990   for (int i = 0; i < tile_cfg->tile_width_count; i++) {
991     tile_cfg->tile_widths[i] = AOMMAX(cfg->tile_widths[i], 1);
992   }
993   for (int i = 0; i < tile_cfg->tile_height_count; i++) {
994     tile_cfg->tile_heights[i] = AOMMAX(cfg->tile_heights[i], 1);
995   }
996   tile_cfg->enable_ext_tile_debug = extra_cfg->ext_tile_debug;
997 
998   if (tile_cfg->enable_large_scale_tile) {
999     // The superblock_size can only be AOM_SUPERBLOCK_SIZE_64X64 or
1000     // AOM_SUPERBLOCK_SIZE_128X128 while tile_cfg->enable_large_scale_tile = 1.
1001     // If superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC, hard set it to
1002     // AOM_SUPERBLOCK_SIZE_64X64(default value in large_scale_tile).
1003     if (extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_64X64 &&
1004         extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_128X128)
1005       tool_cfg->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
1006   }
1007 
1008   // Set reference frame related configuration.
1009   oxcf->ref_frm_cfg.max_reference_frames = extra_cfg->max_reference_frames;
1010   oxcf->ref_frm_cfg.enable_reduced_reference_set =
1011       extra_cfg->enable_reduced_reference_set;
1012   oxcf->ref_frm_cfg.enable_onesided_comp = extra_cfg->enable_onesided_comp;
1013 
1014   oxcf->row_mt = extra_cfg->row_mt;
1015 
1016   // Set motion mode related configuration.
1017   oxcf->motion_mode_cfg.enable_obmc = extra_cfg->enable_obmc;
1018   oxcf->motion_mode_cfg.enable_warped_motion = extra_cfg->enable_warped_motion;
1019   oxcf->motion_mode_cfg.allow_warped_motion =
1020       (cfg->g_usage == AOM_USAGE_REALTIME)
1021           ? false
1022           : (extra_cfg->allow_warped_motion & extra_cfg->enable_warped_motion);
1023 
1024   // Set partition related configuration.
1025   part_cfg->enable_rect_partitions = extra_cfg->enable_rect_partitions;
1026   part_cfg->enable_ab_partitions = extra_cfg->enable_ab_partitions;
1027   part_cfg->enable_1to4_partitions = extra_cfg->enable_1to4_partitions;
1028   part_cfg->min_partition_size = extra_cfg->min_partition_size;
1029   part_cfg->max_partition_size = extra_cfg->max_partition_size;
1030 
1031   // Set intra mode configuration.
1032   intra_mode_cfg->enable_angle_delta = extra_cfg->enable_angle_delta;
1033   intra_mode_cfg->enable_intra_edge_filter =
1034       extra_cfg->enable_intra_edge_filter;
1035   intra_mode_cfg->enable_filter_intra = extra_cfg->enable_filter_intra;
1036   intra_mode_cfg->enable_smooth_intra = extra_cfg->enable_smooth_intra;
1037   intra_mode_cfg->enable_paeth_intra = extra_cfg->enable_paeth_intra;
1038   intra_mode_cfg->enable_cfl_intra = extra_cfg->enable_cfl_intra;
1039 
1040   // Set transform size/type configuration.
1041   txfm_cfg->enable_tx64 = extra_cfg->enable_tx64;
1042   txfm_cfg->enable_flip_idtx = extra_cfg->enable_flip_idtx;
1043   txfm_cfg->enable_rect_tx = extra_cfg->enable_rect_tx;
1044   txfm_cfg->reduced_tx_type_set = extra_cfg->reduced_tx_type_set;
1045   txfm_cfg->use_intra_dct_only = extra_cfg->use_intra_dct_only;
1046   txfm_cfg->use_inter_dct_only = extra_cfg->use_inter_dct_only;
1047   txfm_cfg->use_intra_default_tx_only = extra_cfg->use_intra_default_tx_only;
1048 
1049   // Set compound type configuration.
1050   comp_type_cfg->enable_dist_wtd_comp =
1051       extra_cfg->enable_dist_wtd_comp & extra_cfg->enable_order_hint;
1052   comp_type_cfg->enable_masked_comp = extra_cfg->enable_masked_comp;
1053   comp_type_cfg->enable_diff_wtd_comp =
1054       extra_cfg->enable_masked_comp & extra_cfg->enable_diff_wtd_comp;
1055   comp_type_cfg->enable_interinter_wedge =
1056       extra_cfg->enable_masked_comp & extra_cfg->enable_interinter_wedge;
1057   comp_type_cfg->enable_smooth_interintra =
1058       extra_cfg->enable_interintra_comp && extra_cfg->enable_smooth_interintra;
1059   comp_type_cfg->enable_interintra_wedge =
1060       extra_cfg->enable_interintra_comp & extra_cfg->enable_interintra_wedge;
1061 
1062   // Set Super-resolution mode configuration.
1063   if (extra_cfg->lossless || cfg->large_scale_tile) {
1064     disable_superres(superres_cfg);
1065   } else {
1066     superres_cfg->superres_mode = cfg->rc_superres_mode;
1067     superres_cfg->superres_scale_denominator =
1068         (uint8_t)cfg->rc_superres_denominator;
1069     superres_cfg->superres_kf_scale_denominator =
1070         (uint8_t)cfg->rc_superres_kf_denominator;
1071     superres_cfg->superres_qthresh =
1072         av1_quantizer_to_qindex(cfg->rc_superres_qthresh);
1073     superres_cfg->superres_kf_qthresh =
1074         av1_quantizer_to_qindex(cfg->rc_superres_kf_qthresh);
1075     if (superres_cfg->superres_mode == AOM_SUPERRES_FIXED &&
1076         superres_cfg->superres_scale_denominator == SCALE_NUMERATOR &&
1077         superres_cfg->superres_kf_scale_denominator == SCALE_NUMERATOR) {
1078       disable_superres(superres_cfg);
1079     }
1080     if (superres_cfg->superres_mode == AOM_SUPERRES_QTHRESH &&
1081         superres_cfg->superres_qthresh == 255 &&
1082         superres_cfg->superres_kf_qthresh == 255) {
1083       disable_superres(superres_cfg);
1084     }
1085   }
1086 
1087   superres_cfg->enable_superres =
1088       (superres_cfg->superres_mode != AOM_SUPERRES_NONE) &&
1089       extra_cfg->enable_superres;
1090   if (!superres_cfg->enable_superres) {
1091     disable_superres(superres_cfg);
1092   }
1093 
1094   if (input_cfg->limit == 1) {
1095     // still picture mode, display model and timing is meaningless
1096     dec_model_cfg->display_model_info_present_flag = 0;
1097     dec_model_cfg->timing_info_present = 0;
1098   }
1099 
1100   oxcf->save_as_annexb = cfg->save_as_annexb;
1101 
1102   // Set unit test related configuration.
1103   oxcf->unit_test_cfg.motion_vector_unit_test =
1104       extra_cfg->motion_vector_unit_test;
1105   oxcf->unit_test_cfg.sb_multipass_unit_test =
1106       extra_cfg->sb_multipass_unit_test;
1107 
1108   oxcf->border_in_pixels =
1109       (resize_cfg->resize_mode || superres_cfg->superres_mode)
1110           ? AOM_BORDER_IN_PIXELS
1111           : AOM_ENC_NO_SCALE_BORDER;
1112   memcpy(oxcf->target_seq_level_idx, extra_cfg->target_seq_level_idx,
1113          sizeof(oxcf->target_seq_level_idx));
1114   oxcf->tier_mask = extra_cfg->tier_mask;
1115 
1116   return AOM_CODEC_OK;
1117 }
1118 
encoder_set_config(aom_codec_alg_priv_t * ctx,const aom_codec_enc_cfg_t * cfg)1119 static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
1120                                           const aom_codec_enc_cfg_t *cfg) {
1121   InitialDimensions *const initial_dimensions = &ctx->cpi->initial_dimensions;
1122   aom_codec_err_t res;
1123   int force_key = 0;
1124 
1125   if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
1126     if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
1127       ERROR("Cannot change width or height after initialization");
1128     if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
1129         (initial_dimensions->width &&
1130          (int)cfg->g_w > initial_dimensions->width) ||
1131         (initial_dimensions->height &&
1132          (int)cfg->g_h > initial_dimensions->height))
1133       force_key = 1;
1134   }
1135 
1136   // Prevent increasing lag_in_frames. This check is stricter than it needs
1137   // to be -- the limit is not increasing past the first lag_in_frames
1138   // value, but we don't track the initial config, only the last successful
1139   // config.
1140   if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
1141     ERROR("Cannot increase lag_in_frames");
1142   // Prevent changing lag_in_frames if Lookahead Processing is enabled
1143   if (cfg->g_lag_in_frames != ctx->cfg.g_lag_in_frames &&
1144       ctx->num_lap_buffers > 0)
1145     ERROR("Cannot change lag_in_frames if LAP is enabled");
1146 
1147   res = validate_config(ctx, cfg, &ctx->extra_cfg);
1148 
1149   if (res == AOM_CODEC_OK) {
1150     ctx->cfg = *cfg;
1151     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
1152     // On profile change, request a key frame
1153     force_key |= ctx->cpi->common.seq_params.profile != ctx->oxcf.profile;
1154     av1_change_config(ctx->cpi, &ctx->oxcf);
1155     if (ctx->cpi_lap != NULL) {
1156       av1_change_config(ctx->cpi_lap, &ctx->oxcf);
1157     }
1158   }
1159 
1160   if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
1161 
1162   return res;
1163 }
1164 
encoder_get_global_headers(aom_codec_alg_priv_t * ctx)1165 static aom_fixed_buf_t *encoder_get_global_headers(aom_codec_alg_priv_t *ctx) {
1166   return av1_get_global_headers(ctx->cpi);
1167 }
1168 
ctrl_get_quantizer(aom_codec_alg_priv_t * ctx,va_list args)1169 static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
1170                                           va_list args) {
1171   int *const arg = va_arg(args, int *);
1172   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1173   *arg = av1_get_quantizer(ctx->cpi);
1174   return AOM_CODEC_OK;
1175 }
1176 
ctrl_get_quantizer64(aom_codec_alg_priv_t * ctx,va_list args)1177 static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
1178                                             va_list args) {
1179   int *const arg = va_arg(args, int *);
1180   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1181   *arg = av1_qindex_to_quantizer(av1_get_quantizer(ctx->cpi));
1182   return AOM_CODEC_OK;
1183 }
1184 
ctrl_get_baseline_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)1185 static aom_codec_err_t ctrl_get_baseline_gf_interval(aom_codec_alg_priv_t *ctx,
1186                                                      va_list args) {
1187   int *const arg = va_arg(args, int *);
1188   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1189   *arg = ctx->cpi->rc.baseline_gf_interval;
1190   return AOM_CODEC_OK;
1191 }
1192 
update_extra_cfg(aom_codec_alg_priv_t * ctx,struct av1_extracfg * extra_cfg)1193 static aom_codec_err_t update_extra_cfg(aom_codec_alg_priv_t *ctx,
1194                                         struct av1_extracfg *extra_cfg) {
1195   const aom_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
1196   if (res == AOM_CODEC_OK) {
1197     ctx->extra_cfg = *extra_cfg;
1198     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
1199     av1_change_config(ctx->cpi, &ctx->oxcf);
1200     if (ctx->cpi_lap != NULL) {
1201       av1_change_config(ctx->cpi_lap, &ctx->oxcf);
1202     }
1203   }
1204   return res;
1205 }
1206 
ctrl_set_cpuused(aom_codec_alg_priv_t * ctx,va_list args)1207 static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
1208                                         va_list args) {
1209   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1210   extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
1211   return update_extra_cfg(ctx, &extra_cfg);
1212 }
1213 
ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t * ctx,va_list args)1214 static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
1215                                                     va_list args) {
1216   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1217   extra_cfg.enable_auto_alt_ref = CAST(AOME_SET_ENABLEAUTOALTREF, args);
1218   return update_extra_cfg(ctx, &extra_cfg);
1219 }
1220 
ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t * ctx,va_list args)1221 static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
1222                                                     va_list args) {
1223   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1224   extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
1225   return update_extra_cfg(ctx, &extra_cfg);
1226 }
1227 
ctrl_set_noise_sensitivity(aom_codec_alg_priv_t * ctx,va_list args)1228 static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
1229                                                   va_list args) {
1230   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1231   extra_cfg.noise_sensitivity = CAST(AV1E_SET_NOISE_SENSITIVITY, args);
1232   return update_extra_cfg(ctx, &extra_cfg);
1233 }
1234 
ctrl_set_sharpness(aom_codec_alg_priv_t * ctx,va_list args)1235 static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
1236                                           va_list args) {
1237   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1238   extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
1239   return update_extra_cfg(ctx, &extra_cfg);
1240 }
1241 
ctrl_set_static_thresh(aom_codec_alg_priv_t * ctx,va_list args)1242 static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
1243                                               va_list args) {
1244   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1245   extra_cfg.static_thresh = CAST(AOME_SET_STATIC_THRESHOLD, args);
1246   return update_extra_cfg(ctx, &extra_cfg);
1247 }
1248 
ctrl_set_row_mt(aom_codec_alg_priv_t * ctx,va_list args)1249 static aom_codec_err_t ctrl_set_row_mt(aom_codec_alg_priv_t *ctx,
1250                                        va_list args) {
1251   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1252   extra_cfg.row_mt = CAST(AV1E_SET_ROW_MT, args);
1253   return update_extra_cfg(ctx, &extra_cfg);
1254 }
1255 
ctrl_set_tile_columns(aom_codec_alg_priv_t * ctx,va_list args)1256 static aom_codec_err_t ctrl_set_tile_columns(aom_codec_alg_priv_t *ctx,
1257                                              va_list args) {
1258   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1259   extra_cfg.tile_columns = CAST(AV1E_SET_TILE_COLUMNS, args);
1260   return update_extra_cfg(ctx, &extra_cfg);
1261 }
1262 
ctrl_set_tile_rows(aom_codec_alg_priv_t * ctx,va_list args)1263 static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
1264                                           va_list args) {
1265   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1266   extra_cfg.tile_rows = CAST(AV1E_SET_TILE_ROWS, args);
1267   return update_extra_cfg(ctx, &extra_cfg);
1268 }
1269 
ctrl_set_enable_tpl_model(aom_codec_alg_priv_t * ctx,va_list args)1270 static aom_codec_err_t ctrl_set_enable_tpl_model(aom_codec_alg_priv_t *ctx,
1271                                                  va_list args) {
1272   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1273   extra_cfg.enable_tpl_model = CAST(AV1E_SET_ENABLE_TPL_MODEL, args);
1274   return update_extra_cfg(ctx, &extra_cfg);
1275 }
1276 
ctrl_set_enable_keyframe_filtering(aom_codec_alg_priv_t * ctx,va_list args)1277 static aom_codec_err_t ctrl_set_enable_keyframe_filtering(
1278     aom_codec_alg_priv_t *ctx, va_list args) {
1279   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1280   extra_cfg.enable_keyframe_filtering =
1281       CAST(AV1E_SET_ENABLE_KEYFRAME_FILTERING, args);
1282   return update_extra_cfg(ctx, &extra_cfg);
1283 }
1284 
ctrl_set_arnr_max_frames(aom_codec_alg_priv_t * ctx,va_list args)1285 static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
1286                                                 va_list args) {
1287   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1288   extra_cfg.arnr_max_frames = CAST(AOME_SET_ARNR_MAXFRAMES, args);
1289   return update_extra_cfg(ctx, &extra_cfg);
1290 }
1291 
ctrl_set_arnr_strength(aom_codec_alg_priv_t * ctx,va_list args)1292 static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
1293                                               va_list args) {
1294   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1295   extra_cfg.arnr_strength = CAST(AOME_SET_ARNR_STRENGTH, args);
1296   return update_extra_cfg(ctx, &extra_cfg);
1297 }
1298 
ctrl_set_tuning(aom_codec_alg_priv_t * ctx,va_list args)1299 static aom_codec_err_t ctrl_set_tuning(aom_codec_alg_priv_t *ctx,
1300                                        va_list args) {
1301   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1302   extra_cfg.tuning = CAST(AOME_SET_TUNING, args);
1303   return update_extra_cfg(ctx, &extra_cfg);
1304 }
1305 
ctrl_set_cq_level(aom_codec_alg_priv_t * ctx,va_list args)1306 static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
1307                                          va_list args) {
1308   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1309   extra_cfg.cq_level = CAST(AOME_SET_CQ_LEVEL, args);
1310   return update_extra_cfg(ctx, &extra_cfg);
1311 }
1312 
ctrl_set_rc_max_intra_bitrate_pct(aom_codec_alg_priv_t * ctx,va_list args)1313 static aom_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
1314     aom_codec_alg_priv_t *ctx, va_list args) {
1315   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1316   extra_cfg.rc_max_intra_bitrate_pct =
1317       CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
1318   return update_extra_cfg(ctx, &extra_cfg);
1319 }
1320 
ctrl_set_rc_max_inter_bitrate_pct(aom_codec_alg_priv_t * ctx,va_list args)1321 static aom_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
1322     aom_codec_alg_priv_t *ctx, va_list args) {
1323   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1324   extra_cfg.rc_max_inter_bitrate_pct =
1325       CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
1326   return update_extra_cfg(ctx, &extra_cfg);
1327 }
1328 
ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t * ctx,va_list args)1329 static aom_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t *ctx,
1330                                                     va_list args) {
1331   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1332   extra_cfg.gf_cbr_boost_pct = CAST(AV1E_SET_GF_CBR_BOOST_PCT, args);
1333   return update_extra_cfg(ctx, &extra_cfg);
1334 }
1335 
ctrl_set_lossless(aom_codec_alg_priv_t * ctx,va_list args)1336 static aom_codec_err_t ctrl_set_lossless(aom_codec_alg_priv_t *ctx,
1337                                          va_list args) {
1338   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1339   extra_cfg.lossless = CAST(AV1E_SET_LOSSLESS, args);
1340   return update_extra_cfg(ctx, &extra_cfg);
1341 }
1342 
ctrl_set_enable_cdef(aom_codec_alg_priv_t * ctx,va_list args)1343 static aom_codec_err_t ctrl_set_enable_cdef(aom_codec_alg_priv_t *ctx,
1344                                             va_list args) {
1345   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1346   extra_cfg.enable_cdef = CAST(AV1E_SET_ENABLE_CDEF, args);
1347   return update_extra_cfg(ctx, &extra_cfg);
1348 }
1349 
ctrl_set_enable_restoration(aom_codec_alg_priv_t * ctx,va_list args)1350 static aom_codec_err_t ctrl_set_enable_restoration(aom_codec_alg_priv_t *ctx,
1351                                                    va_list args) {
1352   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1353   extra_cfg.enable_restoration = CAST(AV1E_SET_ENABLE_RESTORATION, args);
1354   return update_extra_cfg(ctx, &extra_cfg);
1355 }
1356 
ctrl_set_force_video_mode(aom_codec_alg_priv_t * ctx,va_list args)1357 static aom_codec_err_t ctrl_set_force_video_mode(aom_codec_alg_priv_t *ctx,
1358                                                  va_list args) {
1359   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1360   extra_cfg.force_video_mode = CAST(AV1E_SET_FORCE_VIDEO_MODE, args);
1361   return update_extra_cfg(ctx, &extra_cfg);
1362 }
1363 
ctrl_set_enable_obmc(aom_codec_alg_priv_t * ctx,va_list args)1364 static aom_codec_err_t ctrl_set_enable_obmc(aom_codec_alg_priv_t *ctx,
1365                                             va_list args) {
1366   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1367   extra_cfg.enable_obmc = CAST(AV1E_SET_ENABLE_OBMC, args);
1368   return update_extra_cfg(ctx, &extra_cfg);
1369 }
1370 
ctrl_set_disable_trellis_quant(aom_codec_alg_priv_t * ctx,va_list args)1371 static aom_codec_err_t ctrl_set_disable_trellis_quant(aom_codec_alg_priv_t *ctx,
1372                                                       va_list args) {
1373   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1374   extra_cfg.disable_trellis_quant = CAST(AV1E_SET_DISABLE_TRELLIS_QUANT, args);
1375   return update_extra_cfg(ctx, &extra_cfg);
1376 }
1377 
ctrl_set_enable_qm(aom_codec_alg_priv_t * ctx,va_list args)1378 static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
1379                                           va_list args) {
1380   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1381   extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
1382   return update_extra_cfg(ctx, &extra_cfg);
1383 }
ctrl_set_qm_y(aom_codec_alg_priv_t * ctx,va_list args)1384 static aom_codec_err_t ctrl_set_qm_y(aom_codec_alg_priv_t *ctx, va_list args) {
1385   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1386   extra_cfg.qm_y = CAST(AV1E_SET_QM_Y, args);
1387   return update_extra_cfg(ctx, &extra_cfg);
1388 }
ctrl_set_qm_u(aom_codec_alg_priv_t * ctx,va_list args)1389 static aom_codec_err_t ctrl_set_qm_u(aom_codec_alg_priv_t *ctx, va_list args) {
1390   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1391   extra_cfg.qm_u = CAST(AV1E_SET_QM_U, args);
1392   return update_extra_cfg(ctx, &extra_cfg);
1393 }
ctrl_set_qm_v(aom_codec_alg_priv_t * ctx,va_list args)1394 static aom_codec_err_t ctrl_set_qm_v(aom_codec_alg_priv_t *ctx, va_list args) {
1395   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1396   extra_cfg.qm_v = CAST(AV1E_SET_QM_V, args);
1397   return update_extra_cfg(ctx, &extra_cfg);
1398 }
ctrl_set_qm_min(aom_codec_alg_priv_t * ctx,va_list args)1399 static aom_codec_err_t ctrl_set_qm_min(aom_codec_alg_priv_t *ctx,
1400                                        va_list args) {
1401   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1402   extra_cfg.qm_min = CAST(AV1E_SET_QM_MIN, args);
1403   return update_extra_cfg(ctx, &extra_cfg);
1404 }
1405 
ctrl_set_qm_max(aom_codec_alg_priv_t * ctx,va_list args)1406 static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
1407                                        va_list args) {
1408   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1409   extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
1410   return update_extra_cfg(ctx, &extra_cfg);
1411 }
1412 
ctrl_set_num_tg(aom_codec_alg_priv_t * ctx,va_list args)1413 static aom_codec_err_t ctrl_set_num_tg(aom_codec_alg_priv_t *ctx,
1414                                        va_list args) {
1415   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1416   extra_cfg.num_tg = CAST(AV1E_SET_NUM_TG, args);
1417   return update_extra_cfg(ctx, &extra_cfg);
1418 }
1419 
ctrl_set_mtu(aom_codec_alg_priv_t * ctx,va_list args)1420 static aom_codec_err_t ctrl_set_mtu(aom_codec_alg_priv_t *ctx, va_list args) {
1421   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1422   extra_cfg.mtu_size = CAST(AV1E_SET_MTU, args);
1423   return update_extra_cfg(ctx, &extra_cfg);
1424 }
ctrl_set_timing_info_type(aom_codec_alg_priv_t * ctx,va_list args)1425 static aom_codec_err_t ctrl_set_timing_info_type(aom_codec_alg_priv_t *ctx,
1426                                                  va_list args) {
1427   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1428   extra_cfg.timing_info_type = CAST(AV1E_SET_TIMING_INFO_TYPE, args);
1429   return update_extra_cfg(ctx, &extra_cfg);
1430 }
1431 
ctrl_set_enable_dual_filter(aom_codec_alg_priv_t * ctx,va_list args)1432 static aom_codec_err_t ctrl_set_enable_dual_filter(aom_codec_alg_priv_t *ctx,
1433                                                    va_list args) {
1434   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1435   extra_cfg.enable_dual_filter = CAST(AV1E_SET_ENABLE_DUAL_FILTER, args);
1436   return update_extra_cfg(ctx, &extra_cfg);
1437 }
1438 
ctrl_set_enable_chroma_deltaq(aom_codec_alg_priv_t * ctx,va_list args)1439 static aom_codec_err_t ctrl_set_enable_chroma_deltaq(aom_codec_alg_priv_t *ctx,
1440                                                      va_list args) {
1441   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1442   extra_cfg.enable_chroma_deltaq = CAST(AV1E_SET_ENABLE_CHROMA_DELTAQ, args);
1443   return update_extra_cfg(ctx, &extra_cfg);
1444 }
1445 
ctrl_set_enable_rect_partitions(aom_codec_alg_priv_t * ctx,va_list args)1446 static aom_codec_err_t ctrl_set_enable_rect_partitions(
1447     aom_codec_alg_priv_t *ctx, va_list args) {
1448   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1449   extra_cfg.enable_rect_partitions =
1450       CAST(AV1E_SET_ENABLE_RECT_PARTITIONS, args);
1451   return update_extra_cfg(ctx, &extra_cfg);
1452 }
1453 
ctrl_set_enable_ab_partitions(aom_codec_alg_priv_t * ctx,va_list args)1454 static aom_codec_err_t ctrl_set_enable_ab_partitions(aom_codec_alg_priv_t *ctx,
1455                                                      va_list args) {
1456   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1457   extra_cfg.enable_ab_partitions = CAST(AV1E_SET_ENABLE_AB_PARTITIONS, args);
1458   return update_extra_cfg(ctx, &extra_cfg);
1459 }
1460 
ctrl_set_enable_1to4_partitions(aom_codec_alg_priv_t * ctx,va_list args)1461 static aom_codec_err_t ctrl_set_enable_1to4_partitions(
1462     aom_codec_alg_priv_t *ctx, va_list args) {
1463   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1464   extra_cfg.enable_1to4_partitions =
1465       CAST(AV1E_SET_ENABLE_1TO4_PARTITIONS, args);
1466   return update_extra_cfg(ctx, &extra_cfg);
1467 }
1468 
ctrl_set_min_partition_size(aom_codec_alg_priv_t * ctx,va_list args)1469 static aom_codec_err_t ctrl_set_min_partition_size(aom_codec_alg_priv_t *ctx,
1470                                                    va_list args) {
1471   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1472   extra_cfg.min_partition_size = CAST(AV1E_SET_MIN_PARTITION_SIZE, args);
1473   return update_extra_cfg(ctx, &extra_cfg);
1474 }
1475 
ctrl_set_max_partition_size(aom_codec_alg_priv_t * ctx,va_list args)1476 static aom_codec_err_t ctrl_set_max_partition_size(aom_codec_alg_priv_t *ctx,
1477                                                    va_list args) {
1478   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1479   extra_cfg.max_partition_size = CAST(AV1E_SET_MAX_PARTITION_SIZE, args);
1480   return update_extra_cfg(ctx, &extra_cfg);
1481 }
1482 
ctrl_set_enable_intra_edge_filter(aom_codec_alg_priv_t * ctx,va_list args)1483 static aom_codec_err_t ctrl_set_enable_intra_edge_filter(
1484     aom_codec_alg_priv_t *ctx, va_list args) {
1485   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1486   extra_cfg.enable_intra_edge_filter =
1487       CAST(AV1E_SET_ENABLE_INTRA_EDGE_FILTER, args);
1488   return update_extra_cfg(ctx, &extra_cfg);
1489 }
1490 
ctrl_set_enable_order_hint(aom_codec_alg_priv_t * ctx,va_list args)1491 static aom_codec_err_t ctrl_set_enable_order_hint(aom_codec_alg_priv_t *ctx,
1492                                                   va_list args) {
1493   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1494   extra_cfg.enable_order_hint = CAST(AV1E_SET_ENABLE_ORDER_HINT, args);
1495   return update_extra_cfg(ctx, &extra_cfg);
1496 }
1497 
ctrl_set_enable_tx64(aom_codec_alg_priv_t * ctx,va_list args)1498 static aom_codec_err_t ctrl_set_enable_tx64(aom_codec_alg_priv_t *ctx,
1499                                             va_list args) {
1500   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1501   extra_cfg.enable_tx64 = CAST(AV1E_SET_ENABLE_TX64, args);
1502   return update_extra_cfg(ctx, &extra_cfg);
1503 }
1504 
ctrl_set_enable_flip_idtx(aom_codec_alg_priv_t * ctx,va_list args)1505 static aom_codec_err_t ctrl_set_enable_flip_idtx(aom_codec_alg_priv_t *ctx,
1506                                                  va_list args) {
1507   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1508   extra_cfg.enable_flip_idtx = CAST(AV1E_SET_ENABLE_FLIP_IDTX, args);
1509   return update_extra_cfg(ctx, &extra_cfg);
1510 }
1511 
ctrl_set_enable_rect_tx(aom_codec_alg_priv_t * ctx,va_list args)1512 static aom_codec_err_t ctrl_set_enable_rect_tx(aom_codec_alg_priv_t *ctx,
1513                                                va_list args) {
1514   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1515   extra_cfg.enable_rect_tx = CAST(AV1E_SET_ENABLE_RECT_TX, args);
1516   return update_extra_cfg(ctx, &extra_cfg);
1517 }
1518 
ctrl_set_enable_dist_wtd_comp(aom_codec_alg_priv_t * ctx,va_list args)1519 static aom_codec_err_t ctrl_set_enable_dist_wtd_comp(aom_codec_alg_priv_t *ctx,
1520                                                      va_list args) {
1521   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1522   extra_cfg.enable_dist_wtd_comp = CAST(AV1E_SET_ENABLE_DIST_WTD_COMP, args);
1523   return update_extra_cfg(ctx, &extra_cfg);
1524 }
1525 
ctrl_set_max_reference_frames(aom_codec_alg_priv_t * ctx,va_list args)1526 static aom_codec_err_t ctrl_set_max_reference_frames(aom_codec_alg_priv_t *ctx,
1527                                                      va_list args) {
1528   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1529   extra_cfg.max_reference_frames = CAST(AV1E_SET_MAX_REFERENCE_FRAMES, args);
1530   return update_extra_cfg(ctx, &extra_cfg);
1531 }
1532 
ctrl_set_enable_reduced_reference_set(aom_codec_alg_priv_t * ctx,va_list args)1533 static aom_codec_err_t ctrl_set_enable_reduced_reference_set(
1534     aom_codec_alg_priv_t *ctx, va_list args) {
1535   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1536   extra_cfg.enable_reduced_reference_set =
1537       CAST(AV1E_SET_REDUCED_REFERENCE_SET, args);
1538   return update_extra_cfg(ctx, &extra_cfg);
1539 }
1540 
ctrl_set_enable_ref_frame_mvs(aom_codec_alg_priv_t * ctx,va_list args)1541 static aom_codec_err_t ctrl_set_enable_ref_frame_mvs(aom_codec_alg_priv_t *ctx,
1542                                                      va_list args) {
1543   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1544   extra_cfg.enable_ref_frame_mvs = CAST(AV1E_SET_ENABLE_REF_FRAME_MVS, args);
1545   return update_extra_cfg(ctx, &extra_cfg);
1546 }
1547 
ctrl_set_allow_ref_frame_mvs(aom_codec_alg_priv_t * ctx,va_list args)1548 static aom_codec_err_t ctrl_set_allow_ref_frame_mvs(aom_codec_alg_priv_t *ctx,
1549                                                     va_list args) {
1550   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1551   extra_cfg.allow_ref_frame_mvs = CAST(AV1E_SET_ALLOW_REF_FRAME_MVS, args);
1552   return update_extra_cfg(ctx, &extra_cfg);
1553 }
1554 
ctrl_set_enable_masked_comp(aom_codec_alg_priv_t * ctx,va_list args)1555 static aom_codec_err_t ctrl_set_enable_masked_comp(aom_codec_alg_priv_t *ctx,
1556                                                    va_list args) {
1557   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1558   extra_cfg.enable_masked_comp = CAST(AV1E_SET_ENABLE_MASKED_COMP, args);
1559   return update_extra_cfg(ctx, &extra_cfg);
1560 }
1561 
ctrl_set_enable_onesided_comp(aom_codec_alg_priv_t * ctx,va_list args)1562 static aom_codec_err_t ctrl_set_enable_onesided_comp(aom_codec_alg_priv_t *ctx,
1563                                                      va_list args) {
1564   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1565   extra_cfg.enable_onesided_comp = CAST(AV1E_SET_ENABLE_ONESIDED_COMP, args);
1566   return update_extra_cfg(ctx, &extra_cfg);
1567 }
1568 
ctrl_set_enable_interintra_comp(aom_codec_alg_priv_t * ctx,va_list args)1569 static aom_codec_err_t ctrl_set_enable_interintra_comp(
1570     aom_codec_alg_priv_t *ctx, va_list args) {
1571   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1572   extra_cfg.enable_interintra_comp =
1573       CAST(AV1E_SET_ENABLE_INTERINTRA_COMP, args);
1574   return update_extra_cfg(ctx, &extra_cfg);
1575 }
1576 
ctrl_set_enable_smooth_interintra(aom_codec_alg_priv_t * ctx,va_list args)1577 static aom_codec_err_t ctrl_set_enable_smooth_interintra(
1578     aom_codec_alg_priv_t *ctx, va_list args) {
1579   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1580   extra_cfg.enable_smooth_interintra =
1581       CAST(AV1E_SET_ENABLE_SMOOTH_INTERINTRA, args);
1582   return update_extra_cfg(ctx, &extra_cfg);
1583 }
1584 
ctrl_set_enable_diff_wtd_comp(aom_codec_alg_priv_t * ctx,va_list args)1585 static aom_codec_err_t ctrl_set_enable_diff_wtd_comp(aom_codec_alg_priv_t *ctx,
1586                                                      va_list args) {
1587   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1588   extra_cfg.enable_diff_wtd_comp = CAST(AV1E_SET_ENABLE_DIFF_WTD_COMP, args);
1589   return update_extra_cfg(ctx, &extra_cfg);
1590 }
1591 
ctrl_set_enable_interinter_wedge(aom_codec_alg_priv_t * ctx,va_list args)1592 static aom_codec_err_t ctrl_set_enable_interinter_wedge(
1593     aom_codec_alg_priv_t *ctx, va_list args) {
1594   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1595   extra_cfg.enable_interinter_wedge =
1596       CAST(AV1E_SET_ENABLE_INTERINTER_WEDGE, args);
1597   return update_extra_cfg(ctx, &extra_cfg);
1598 }
1599 
ctrl_set_enable_interintra_wedge(aom_codec_alg_priv_t * ctx,va_list args)1600 static aom_codec_err_t ctrl_set_enable_interintra_wedge(
1601     aom_codec_alg_priv_t *ctx, va_list args) {
1602   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1603   extra_cfg.enable_interintra_wedge =
1604       CAST(AV1E_SET_ENABLE_INTERINTRA_WEDGE, args);
1605   return update_extra_cfg(ctx, &extra_cfg);
1606 }
1607 
ctrl_set_enable_global_motion(aom_codec_alg_priv_t * ctx,va_list args)1608 static aom_codec_err_t ctrl_set_enable_global_motion(aom_codec_alg_priv_t *ctx,
1609                                                      va_list args) {
1610   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1611   extra_cfg.enable_global_motion = CAST(AV1E_SET_ENABLE_GLOBAL_MOTION, args);
1612   return update_extra_cfg(ctx, &extra_cfg);
1613 }
1614 
ctrl_set_enable_warped_motion(aom_codec_alg_priv_t * ctx,va_list args)1615 static aom_codec_err_t ctrl_set_enable_warped_motion(aom_codec_alg_priv_t *ctx,
1616                                                      va_list args) {
1617   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1618   extra_cfg.enable_warped_motion = CAST(AV1E_SET_ENABLE_WARPED_MOTION, args);
1619   return update_extra_cfg(ctx, &extra_cfg);
1620 }
1621 
ctrl_set_allow_warped_motion(aom_codec_alg_priv_t * ctx,va_list args)1622 static aom_codec_err_t ctrl_set_allow_warped_motion(aom_codec_alg_priv_t *ctx,
1623                                                     va_list args) {
1624   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1625   extra_cfg.allow_warped_motion = CAST(AV1E_SET_ALLOW_WARPED_MOTION, args);
1626   return update_extra_cfg(ctx, &extra_cfg);
1627 }
1628 
ctrl_set_enable_filter_intra(aom_codec_alg_priv_t * ctx,va_list args)1629 static aom_codec_err_t ctrl_set_enable_filter_intra(aom_codec_alg_priv_t *ctx,
1630                                                     va_list args) {
1631   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1632   extra_cfg.enable_filter_intra = CAST(AV1E_SET_ENABLE_FILTER_INTRA, args);
1633   return update_extra_cfg(ctx, &extra_cfg);
1634 }
1635 
ctrl_set_enable_smooth_intra(aom_codec_alg_priv_t * ctx,va_list args)1636 static aom_codec_err_t ctrl_set_enable_smooth_intra(aom_codec_alg_priv_t *ctx,
1637                                                     va_list args) {
1638   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1639   extra_cfg.enable_smooth_intra = CAST(AV1E_SET_ENABLE_SMOOTH_INTRA, args);
1640   return update_extra_cfg(ctx, &extra_cfg);
1641 }
1642 
ctrl_set_enable_paeth_intra(aom_codec_alg_priv_t * ctx,va_list args)1643 static aom_codec_err_t ctrl_set_enable_paeth_intra(aom_codec_alg_priv_t *ctx,
1644                                                    va_list args) {
1645   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1646   extra_cfg.enable_paeth_intra = CAST(AV1E_SET_ENABLE_PAETH_INTRA, args);
1647   return update_extra_cfg(ctx, &extra_cfg);
1648 }
1649 
ctrl_set_enable_cfl_intra(aom_codec_alg_priv_t * ctx,va_list args)1650 static aom_codec_err_t ctrl_set_enable_cfl_intra(aom_codec_alg_priv_t *ctx,
1651                                                  va_list args) {
1652   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1653   extra_cfg.enable_cfl_intra = CAST(AV1E_SET_ENABLE_CFL_INTRA, args);
1654   return update_extra_cfg(ctx, &extra_cfg);
1655 }
1656 
ctrl_set_enable_superres(aom_codec_alg_priv_t * ctx,va_list args)1657 static aom_codec_err_t ctrl_set_enable_superres(aom_codec_alg_priv_t *ctx,
1658                                                 va_list args) {
1659   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1660   extra_cfg.enable_superres = CAST(AV1E_SET_ENABLE_SUPERRES, args);
1661   return update_extra_cfg(ctx, &extra_cfg);
1662 }
1663 
ctrl_set_enable_overlay(aom_codec_alg_priv_t * ctx,va_list args)1664 static aom_codec_err_t ctrl_set_enable_overlay(aom_codec_alg_priv_t *ctx,
1665                                                va_list args) {
1666   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1667   extra_cfg.enable_overlay = CAST(AV1E_SET_ENABLE_OVERLAY, args);
1668   return update_extra_cfg(ctx, &extra_cfg);
1669 }
1670 
ctrl_set_enable_palette(aom_codec_alg_priv_t * ctx,va_list args)1671 static aom_codec_err_t ctrl_set_enable_palette(aom_codec_alg_priv_t *ctx,
1672                                                va_list args) {
1673   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1674   extra_cfg.enable_palette = CAST(AV1E_SET_ENABLE_PALETTE, args);
1675   return update_extra_cfg(ctx, &extra_cfg);
1676 }
1677 
ctrl_set_enable_intrabc(aom_codec_alg_priv_t * ctx,va_list args)1678 static aom_codec_err_t ctrl_set_enable_intrabc(aom_codec_alg_priv_t *ctx,
1679                                                va_list args) {
1680   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1681   extra_cfg.enable_intrabc = CAST(AV1E_SET_ENABLE_INTRABC, args);
1682   return update_extra_cfg(ctx, &extra_cfg);
1683 }
1684 
ctrl_set_enable_angle_delta(aom_codec_alg_priv_t * ctx,va_list args)1685 static aom_codec_err_t ctrl_set_enable_angle_delta(aom_codec_alg_priv_t *ctx,
1686                                                    va_list args) {
1687   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1688   extra_cfg.enable_angle_delta = CAST(AV1E_SET_ENABLE_ANGLE_DELTA, args);
1689   return update_extra_cfg(ctx, &extra_cfg);
1690 }
1691 
ctrl_set_error_resilient_mode(aom_codec_alg_priv_t * ctx,va_list args)1692 static aom_codec_err_t ctrl_set_error_resilient_mode(aom_codec_alg_priv_t *ctx,
1693                                                      va_list args) {
1694   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1695   extra_cfg.error_resilient_mode = CAST(AV1E_SET_ERROR_RESILIENT_MODE, args);
1696   return update_extra_cfg(ctx, &extra_cfg);
1697 }
1698 
ctrl_set_s_frame_mode(aom_codec_alg_priv_t * ctx,va_list args)1699 static aom_codec_err_t ctrl_set_s_frame_mode(aom_codec_alg_priv_t *ctx,
1700                                              va_list args) {
1701   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1702   extra_cfg.s_frame_mode = CAST(AV1E_SET_S_FRAME_MODE, args);
1703   return update_extra_cfg(ctx, &extra_cfg);
1704 }
1705 
ctrl_set_frame_parallel_decoding_mode(aom_codec_alg_priv_t * ctx,va_list args)1706 static aom_codec_err_t ctrl_set_frame_parallel_decoding_mode(
1707     aom_codec_alg_priv_t *ctx, va_list args) {
1708   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1709   extra_cfg.frame_parallel_decoding_mode =
1710       CAST(AV1E_SET_FRAME_PARALLEL_DECODING, args);
1711   return update_extra_cfg(ctx, &extra_cfg);
1712 }
1713 
ctrl_set_single_tile_decoding(aom_codec_alg_priv_t * ctx,va_list args)1714 static aom_codec_err_t ctrl_set_single_tile_decoding(aom_codec_alg_priv_t *ctx,
1715                                                      va_list args) {
1716   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1717   extra_cfg.single_tile_decoding = CAST(AV1E_SET_SINGLE_TILE_DECODING, args);
1718   return update_extra_cfg(ctx, &extra_cfg);
1719 }
1720 
ctrl_set_aq_mode(aom_codec_alg_priv_t * ctx,va_list args)1721 static aom_codec_err_t ctrl_set_aq_mode(aom_codec_alg_priv_t *ctx,
1722                                         va_list args) {
1723   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1724   extra_cfg.aq_mode = CAST(AV1E_SET_AQ_MODE, args);
1725   return update_extra_cfg(ctx, &extra_cfg);
1726 }
1727 
ctrl_set_reduced_tx_type_set(aom_codec_alg_priv_t * ctx,va_list args)1728 static aom_codec_err_t ctrl_set_reduced_tx_type_set(aom_codec_alg_priv_t *ctx,
1729                                                     va_list args) {
1730   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1731   extra_cfg.reduced_tx_type_set = CAST(AV1E_SET_REDUCED_TX_TYPE_SET, args);
1732   return update_extra_cfg(ctx, &extra_cfg);
1733 }
1734 
ctrl_set_intra_dct_only(aom_codec_alg_priv_t * ctx,va_list args)1735 static aom_codec_err_t ctrl_set_intra_dct_only(aom_codec_alg_priv_t *ctx,
1736                                                va_list args) {
1737   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1738   extra_cfg.use_intra_dct_only = CAST(AV1E_SET_INTRA_DCT_ONLY, args);
1739   return update_extra_cfg(ctx, &extra_cfg);
1740 }
1741 
ctrl_set_inter_dct_only(aom_codec_alg_priv_t * ctx,va_list args)1742 static aom_codec_err_t ctrl_set_inter_dct_only(aom_codec_alg_priv_t *ctx,
1743                                                va_list args) {
1744   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1745   extra_cfg.use_inter_dct_only = CAST(AV1E_SET_INTER_DCT_ONLY, args);
1746   return update_extra_cfg(ctx, &extra_cfg);
1747 }
1748 
ctrl_set_intra_default_tx_only(aom_codec_alg_priv_t * ctx,va_list args)1749 static aom_codec_err_t ctrl_set_intra_default_tx_only(aom_codec_alg_priv_t *ctx,
1750                                                       va_list args) {
1751   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1752   extra_cfg.use_intra_default_tx_only =
1753       CAST(AV1E_SET_INTRA_DEFAULT_TX_ONLY, args);
1754   return update_extra_cfg(ctx, &extra_cfg);
1755 }
1756 
ctrl_set_quant_b_adapt(aom_codec_alg_priv_t * ctx,va_list args)1757 static aom_codec_err_t ctrl_set_quant_b_adapt(aom_codec_alg_priv_t *ctx,
1758                                               va_list args) {
1759   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1760   extra_cfg.quant_b_adapt = CAST(AV1E_SET_QUANT_B_ADAPT, args);
1761   return update_extra_cfg(ctx, &extra_cfg);
1762 }
1763 
ctrl_set_vbr_corpus_complexity_lap(aom_codec_alg_priv_t * ctx,va_list args)1764 static aom_codec_err_t ctrl_set_vbr_corpus_complexity_lap(
1765     aom_codec_alg_priv_t *ctx, va_list args) {
1766   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1767   extra_cfg.vbr_corpus_complexity_lap =
1768       CAST(AV1E_SET_VBR_CORPUS_COMPLEXITY_LAP, args);
1769   return update_extra_cfg(ctx, &extra_cfg);
1770 }
ctrl_set_coeff_cost_upd_freq(aom_codec_alg_priv_t * ctx,va_list args)1771 static aom_codec_err_t ctrl_set_coeff_cost_upd_freq(aom_codec_alg_priv_t *ctx,
1772                                                     va_list args) {
1773   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1774   extra_cfg.coeff_cost_upd_freq = CAST(AV1E_SET_COEFF_COST_UPD_FREQ, args);
1775   return update_extra_cfg(ctx, &extra_cfg);
1776 }
1777 
ctrl_set_mode_cost_upd_freq(aom_codec_alg_priv_t * ctx,va_list args)1778 static aom_codec_err_t ctrl_set_mode_cost_upd_freq(aom_codec_alg_priv_t *ctx,
1779                                                    va_list args) {
1780   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1781   extra_cfg.mode_cost_upd_freq = CAST(AV1E_SET_MODE_COST_UPD_FREQ, args);
1782   return update_extra_cfg(ctx, &extra_cfg);
1783 }
1784 
ctrl_set_mv_cost_upd_freq(aom_codec_alg_priv_t * ctx,va_list args)1785 static aom_codec_err_t ctrl_set_mv_cost_upd_freq(aom_codec_alg_priv_t *ctx,
1786                                                  va_list args) {
1787   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1788   extra_cfg.mv_cost_upd_freq = CAST(AV1E_SET_MV_COST_UPD_FREQ, args);
1789   return update_extra_cfg(ctx, &extra_cfg);
1790 }
1791 
ctrl_set_vmaf_model_path(aom_codec_alg_priv_t * ctx,va_list args)1792 static aom_codec_err_t ctrl_set_vmaf_model_path(aom_codec_alg_priv_t *ctx,
1793                                                 va_list args) {
1794   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1795   extra_cfg.vmaf_model_path = CAST(AV1E_SET_VMAF_MODEL_PATH, args);
1796   return update_extra_cfg(ctx, &extra_cfg);
1797 }
1798 
ctrl_set_film_grain_test_vector(aom_codec_alg_priv_t * ctx,va_list args)1799 static aom_codec_err_t ctrl_set_film_grain_test_vector(
1800     aom_codec_alg_priv_t *ctx, va_list args) {
1801   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1802   extra_cfg.film_grain_test_vector =
1803       CAST(AV1E_SET_FILM_GRAIN_TEST_VECTOR, args);
1804   return update_extra_cfg(ctx, &extra_cfg);
1805 }
1806 
ctrl_set_film_grain_table(aom_codec_alg_priv_t * ctx,va_list args)1807 static aom_codec_err_t ctrl_set_film_grain_table(aom_codec_alg_priv_t *ctx,
1808                                                  va_list args) {
1809   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1810   extra_cfg.film_grain_table_filename = CAST(AV1E_SET_FILM_GRAIN_TABLE, args);
1811   return update_extra_cfg(ctx, &extra_cfg);
1812 }
1813 
ctrl_set_denoise_noise_level(aom_codec_alg_priv_t * ctx,va_list args)1814 static aom_codec_err_t ctrl_set_denoise_noise_level(aom_codec_alg_priv_t *ctx,
1815                                                     va_list args) {
1816 #if !CONFIG_DENOISE
1817   (void)ctx;
1818   (void)args;
1819   return AOM_CODEC_INCAPABLE;
1820 #else
1821   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1822   extra_cfg.noise_level =
1823       ((float)CAST(AV1E_SET_DENOISE_NOISE_LEVEL, args)) / 10.0f;
1824   return update_extra_cfg(ctx, &extra_cfg);
1825 #endif
1826 }
1827 
ctrl_set_denoise_block_size(aom_codec_alg_priv_t * ctx,va_list args)1828 static aom_codec_err_t ctrl_set_denoise_block_size(aom_codec_alg_priv_t *ctx,
1829                                                    va_list args) {
1830 #if !CONFIG_DENOISE
1831   (void)ctx;
1832   (void)args;
1833   return AOM_CODEC_INCAPABLE;
1834 #else
1835   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1836   extra_cfg.noise_block_size = CAST(AV1E_SET_DENOISE_BLOCK_SIZE, args);
1837   return update_extra_cfg(ctx, &extra_cfg);
1838 #endif
1839 }
1840 
ctrl_set_enable_dnl_denoising(aom_codec_alg_priv_t * ctx,va_list args)1841 static aom_codec_err_t ctrl_set_enable_dnl_denoising(aom_codec_alg_priv_t *ctx,
1842                                                      va_list args) {
1843 #if !CONFIG_DENOISE
1844   (void)ctx;
1845   (void)args;
1846   return AOM_CODEC_INCAPABLE;
1847 #else
1848   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1849   extra_cfg.enable_dnl_denoising = CAST(AV1E_SET_ENABLE_DNL_DENOISING, args);
1850   return update_extra_cfg(ctx, &extra_cfg);
1851 #endif
1852 }
1853 
ctrl_set_deltaq_mode(aom_codec_alg_priv_t * ctx,va_list args)1854 static aom_codec_err_t ctrl_set_deltaq_mode(aom_codec_alg_priv_t *ctx,
1855                                             va_list args) {
1856   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1857   extra_cfg.deltaq_mode = CAST(AV1E_SET_DELTAQ_MODE, args);
1858   return update_extra_cfg(ctx, &extra_cfg);
1859 }
1860 
ctrl_set_deltalf_mode(aom_codec_alg_priv_t * ctx,va_list args)1861 static aom_codec_err_t ctrl_set_deltalf_mode(aom_codec_alg_priv_t *ctx,
1862                                              va_list args) {
1863   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1864   extra_cfg.deltalf_mode = CAST(AV1E_SET_DELTALF_MODE, args);
1865   return update_extra_cfg(ctx, &extra_cfg);
1866 }
1867 
ctrl_set_min_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)1868 static aom_codec_err_t ctrl_set_min_gf_interval(aom_codec_alg_priv_t *ctx,
1869                                                 va_list args) {
1870   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1871   extra_cfg.min_gf_interval = CAST(AV1E_SET_MIN_GF_INTERVAL, args);
1872   return update_extra_cfg(ctx, &extra_cfg);
1873 }
1874 
ctrl_set_max_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)1875 static aom_codec_err_t ctrl_set_max_gf_interval(aom_codec_alg_priv_t *ctx,
1876                                                 va_list args) {
1877   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1878   extra_cfg.max_gf_interval = CAST(AV1E_SET_MAX_GF_INTERVAL, args);
1879   return update_extra_cfg(ctx, &extra_cfg);
1880 }
1881 
ctrl_set_gf_min_pyr_height(aom_codec_alg_priv_t * ctx,va_list args)1882 static aom_codec_err_t ctrl_set_gf_min_pyr_height(aom_codec_alg_priv_t *ctx,
1883                                                   va_list args) {
1884   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1885   extra_cfg.gf_min_pyr_height = CAST(AV1E_SET_GF_MIN_PYRAMID_HEIGHT, args);
1886   return update_extra_cfg(ctx, &extra_cfg);
1887 }
1888 
ctrl_set_gf_max_pyr_height(aom_codec_alg_priv_t * ctx,va_list args)1889 static aom_codec_err_t ctrl_set_gf_max_pyr_height(aom_codec_alg_priv_t *ctx,
1890                                                   va_list args) {
1891   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1892   extra_cfg.gf_max_pyr_height = CAST(AV1E_SET_GF_MAX_PYRAMID_HEIGHT, args);
1893   return update_extra_cfg(ctx, &extra_cfg);
1894 }
1895 
ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t * ctx,va_list args)1896 static aom_codec_err_t ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t *ctx,
1897                                                      va_list args) {
1898   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1899   extra_cfg.frame_periodic_boost = CAST(AV1E_SET_FRAME_PERIODIC_BOOST, args);
1900   return update_extra_cfg(ctx, &extra_cfg);
1901 }
1902 
ctrl_enable_motion_vector_unit_test(aom_codec_alg_priv_t * ctx,va_list args)1903 static aom_codec_err_t ctrl_enable_motion_vector_unit_test(
1904     aom_codec_alg_priv_t *ctx, va_list args) {
1905   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1906   extra_cfg.motion_vector_unit_test =
1907       CAST(AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
1908   return update_extra_cfg(ctx, &extra_cfg);
1909 }
1910 
ctrl_enable_ext_tile_debug(aom_codec_alg_priv_t * ctx,va_list args)1911 static aom_codec_err_t ctrl_enable_ext_tile_debug(aom_codec_alg_priv_t *ctx,
1912                                                   va_list args) {
1913   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1914   extra_cfg.ext_tile_debug = CAST(AV1E_ENABLE_EXT_TILE_DEBUG, args);
1915   return update_extra_cfg(ctx, &extra_cfg);
1916 }
1917 
ctrl_set_target_seq_level_idx(aom_codec_alg_priv_t * ctx,va_list args)1918 static aom_codec_err_t ctrl_set_target_seq_level_idx(aom_codec_alg_priv_t *ctx,
1919                                                      va_list args) {
1920   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1921   const int val = CAST(AV1E_SET_TARGET_SEQ_LEVEL_IDX, args);
1922   const int level = val % 100;
1923   const int operating_point_idx = val / 100;
1924   if (operating_point_idx >= 0 &&
1925       operating_point_idx < MAX_NUM_OPERATING_POINTS) {
1926     extra_cfg.target_seq_level_idx[operating_point_idx] = (AV1_LEVEL)level;
1927   }
1928   return update_extra_cfg(ctx, &extra_cfg);
1929 }
1930 
ctrl_set_tier_mask(aom_codec_alg_priv_t * ctx,va_list args)1931 static aom_codec_err_t ctrl_set_tier_mask(aom_codec_alg_priv_t *ctx,
1932                                           va_list args) {
1933   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1934   extra_cfg.tier_mask = CAST(AV1E_SET_TIER_MASK, args);
1935   return update_extra_cfg(ctx, &extra_cfg);
1936 }
1937 
ctrl_set_min_cr(aom_codec_alg_priv_t * ctx,va_list args)1938 static aom_codec_err_t ctrl_set_min_cr(aom_codec_alg_priv_t *ctx,
1939                                        va_list args) {
1940   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1941   extra_cfg.min_cr = CAST(AV1E_SET_MIN_CR, args);
1942   return update_extra_cfg(ctx, &extra_cfg);
1943 }
1944 
ctrl_enable_sb_multipass_unit_test(aom_codec_alg_priv_t * ctx,va_list args)1945 static aom_codec_err_t ctrl_enable_sb_multipass_unit_test(
1946     aom_codec_alg_priv_t *ctx, va_list args) {
1947   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1948   extra_cfg.sb_multipass_unit_test =
1949       CAST(AV1E_ENABLE_SB_MULTIPASS_UNIT_TEST, args);
1950   return update_extra_cfg(ctx, &extra_cfg);
1951 }
1952 
1953 #if !CONFIG_REALTIME_ONLY
create_stats_buffer(FIRSTPASS_STATS ** frame_stats_buffer,STATS_BUFFER_CTX * stats_buf_context,int num_lap_buffers)1954 static aom_codec_err_t create_stats_buffer(FIRSTPASS_STATS **frame_stats_buffer,
1955                                            STATS_BUFFER_CTX *stats_buf_context,
1956                                            int num_lap_buffers) {
1957   aom_codec_err_t res = AOM_CODEC_OK;
1958 
1959   int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
1960   *frame_stats_buffer =
1961       (FIRSTPASS_STATS *)aom_calloc(size, sizeof(FIRSTPASS_STATS));
1962   if (*frame_stats_buffer == NULL) return AOM_CODEC_MEM_ERROR;
1963 
1964   stats_buf_context->stats_in_start = *frame_stats_buffer;
1965   stats_buf_context->stats_in_end = stats_buf_context->stats_in_start;
1966   stats_buf_context->stats_in_buf_end =
1967       stats_buf_context->stats_in_start + size;
1968 
1969   stats_buf_context->total_left_stats = aom_calloc(1, sizeof(FIRSTPASS_STATS));
1970   if (stats_buf_context->total_left_stats == NULL) return AOM_CODEC_MEM_ERROR;
1971   av1_twopass_zero_stats(stats_buf_context->total_left_stats);
1972   stats_buf_context->total_stats = aom_calloc(1, sizeof(FIRSTPASS_STATS));
1973   if (stats_buf_context->total_stats == NULL) return AOM_CODEC_MEM_ERROR;
1974   av1_twopass_zero_stats(stats_buf_context->total_stats);
1975   return res;
1976 }
1977 #endif
1978 
create_context_and_bufferpool(AV1_COMP ** p_cpi,BufferPool ** p_buffer_pool,AV1EncoderConfig * oxcf,struct aom_codec_pkt_list * pkt_list_head,FIRSTPASS_STATS * frame_stats_buf,COMPRESSOR_STAGE stage,int num_lap_buffers,int lap_lag_in_frames,STATS_BUFFER_CTX * stats_buf_context)1979 static aom_codec_err_t create_context_and_bufferpool(
1980     AV1_COMP **p_cpi, BufferPool **p_buffer_pool, AV1EncoderConfig *oxcf,
1981     struct aom_codec_pkt_list *pkt_list_head, FIRSTPASS_STATS *frame_stats_buf,
1982     COMPRESSOR_STAGE stage, int num_lap_buffers, int lap_lag_in_frames,
1983     STATS_BUFFER_CTX *stats_buf_context) {
1984   aom_codec_err_t res = AOM_CODEC_OK;
1985 
1986   *p_buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
1987   if (*p_buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
1988 
1989 #if CONFIG_MULTITHREAD
1990   if (pthread_mutex_init(&((*p_buffer_pool)->pool_mutex), NULL)) {
1991     return AOM_CODEC_MEM_ERROR;
1992   }
1993 #endif
1994   *p_cpi = av1_create_compressor(oxcf, *p_buffer_pool, frame_stats_buf, stage,
1995                                  num_lap_buffers, lap_lag_in_frames,
1996                                  stats_buf_context);
1997   if (*p_cpi == NULL)
1998     res = AOM_CODEC_MEM_ERROR;
1999   else
2000     (*p_cpi)->output_pkt_list = pkt_list_head;
2001 
2002   return res;
2003 }
2004 
encoder_init(aom_codec_ctx_t * ctx)2005 static aom_codec_err_t encoder_init(aom_codec_ctx_t *ctx) {
2006   aom_codec_err_t res = AOM_CODEC_OK;
2007 
2008   if (ctx->priv == NULL) {
2009     aom_codec_alg_priv_t *const priv = aom_calloc(1, sizeof(*priv));
2010     if (priv == NULL) return AOM_CODEC_MEM_ERROR;
2011 
2012     ctx->priv = (aom_codec_priv_t *)priv;
2013     ctx->priv->init_flags = ctx->init_flags;
2014 
2015     if (ctx->config.enc) {
2016       // Update the reference to the config structure to an internal copy.
2017       priv->cfg = *ctx->config.enc;
2018       ctx->config.enc = &priv->cfg;
2019     }
2020 
2021     priv->extra_cfg = default_extra_cfg;
2022     aom_once(av1_initialize_enc);
2023 
2024     res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
2025 
2026     if (res == AOM_CODEC_OK) {
2027       int *num_lap_buffers = &priv->num_lap_buffers;
2028       int lap_lag_in_frames = 0;
2029       *num_lap_buffers = 0;
2030       priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
2031       priv->timestamp_ratio.num =
2032           (int64_t)priv->cfg.g_timebase.num * TICKS_PER_SEC;
2033       reduce_ratio(&priv->timestamp_ratio);
2034 
2035       set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
2036       if (priv->oxcf.rc_cfg.mode != AOM_CBR && priv->oxcf.pass == 0 &&
2037           priv->oxcf.mode == GOOD) {
2038         // Enable look ahead - enabled for AOM_Q, AOM_CQ, AOM_VBR
2039         *num_lap_buffers = priv->cfg.g_lag_in_frames;
2040         *num_lap_buffers =
2041             clamp(*num_lap_buffers, 0,
2042                   AOMMIN(MAX_LAP_BUFFERS, priv->oxcf.kf_cfg.key_freq_max +
2043                                               SCENE_CUT_KEY_TEST_INTERVAL));
2044         if ((int)priv->cfg.g_lag_in_frames - (*num_lap_buffers) >=
2045             LAP_LAG_IN_FRAMES) {
2046           lap_lag_in_frames = LAP_LAG_IN_FRAMES;
2047         }
2048       }
2049       priv->oxcf.use_highbitdepth =
2050           (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
2051 
2052 #if !CONFIG_REALTIME_ONLY
2053       res = create_stats_buffer(&priv->frame_stats_buffer,
2054                                 &priv->stats_buf_context, *num_lap_buffers);
2055       if (res != AOM_CODEC_OK) return AOM_CODEC_MEM_ERROR;
2056 #endif
2057 
2058       res = create_context_and_bufferpool(
2059           &priv->cpi, &priv->buffer_pool, &priv->oxcf, &priv->pkt_list.head,
2060           priv->frame_stats_buffer, ENCODE_STAGE, *num_lap_buffers, -1,
2061           &priv->stats_buf_context);
2062 
2063       // Create another compressor if look ahead is enabled
2064       if (res == AOM_CODEC_OK && *num_lap_buffers) {
2065         res = create_context_and_bufferpool(
2066             &priv->cpi_lap, &priv->buffer_pool_lap, &priv->oxcf, NULL,
2067             priv->frame_stats_buffer, LAP_STAGE, *num_lap_buffers,
2068             clamp(lap_lag_in_frames, 0, MAX_LAG_BUFFERS),
2069             &priv->stats_buf_context);
2070       }
2071     }
2072   }
2073 
2074   return res;
2075 }
2076 
destroy_context_and_bufferpool(AV1_COMP * cpi,BufferPool * buffer_pool)2077 static void destroy_context_and_bufferpool(AV1_COMP *cpi,
2078                                            BufferPool *buffer_pool) {
2079   av1_remove_compressor(cpi);
2080 #if CONFIG_MULTITHREAD
2081   if (buffer_pool) pthread_mutex_destroy(&buffer_pool->pool_mutex);
2082 #endif
2083   aom_free(buffer_pool);
2084 }
2085 
destroy_stats_buffer(STATS_BUFFER_CTX * stats_buf_context,FIRSTPASS_STATS * frame_stats_buffer)2086 static void destroy_stats_buffer(STATS_BUFFER_CTX *stats_buf_context,
2087                                  FIRSTPASS_STATS *frame_stats_buffer) {
2088   aom_free(stats_buf_context->total_left_stats);
2089   aom_free(stats_buf_context->total_stats);
2090   aom_free(frame_stats_buffer);
2091 }
2092 
encoder_destroy(aom_codec_alg_priv_t * ctx)2093 static aom_codec_err_t encoder_destroy(aom_codec_alg_priv_t *ctx) {
2094   free(ctx->cx_data);
2095   destroy_context_and_bufferpool(ctx->cpi, ctx->buffer_pool);
2096   if (ctx->cpi_lap) {
2097     // As both cpi and cpi_lap have the same lookahead_ctx, it is already freed
2098     // when destroy is called on cpi. Thus, setting lookahead_ctx to null here,
2099     // so that it doesn't attempt to free it again.
2100     ctx->cpi_lap->lookahead = NULL;
2101     destroy_context_and_bufferpool(ctx->cpi_lap, ctx->buffer_pool_lap);
2102   }
2103   destroy_stats_buffer(&ctx->stats_buf_context, ctx->frame_stats_buffer);
2104   aom_free(ctx);
2105   return AOM_CODEC_OK;
2106 }
2107 
get_frame_pkt_flags(const AV1_COMP * cpi,unsigned int lib_flags)2108 static aom_codec_frame_flags_t get_frame_pkt_flags(const AV1_COMP *cpi,
2109                                                    unsigned int lib_flags) {
2110   const SVC *const svc = &cpi->svc;
2111   aom_codec_frame_flags_t flags = lib_flags << 16;
2112 
2113   if (lib_flags & FRAMEFLAGS_KEY ||
2114       (cpi->use_svc &&
2115        svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
2116                           svc->temporal_layer_id]
2117            .is_key_frame))
2118     flags |= AOM_FRAME_IS_KEY;
2119   if (lib_flags & FRAMEFLAGS_INTRAONLY) flags |= AOM_FRAME_IS_INTRAONLY;
2120   if (lib_flags & FRAMEFLAGS_SWITCH) flags |= AOM_FRAME_IS_SWITCH;
2121   if (lib_flags & FRAMEFLAGS_ERROR_RESILIENT)
2122     flags |= AOM_FRAME_IS_ERROR_RESILIENT;
2123   if (cpi->droppable) flags |= AOM_FRAME_IS_DROPPABLE;
2124 
2125   return flags;
2126 }
2127 
2128 // TODO(Mufaddal): Check feasibility of abstracting functions related to LAP
2129 // into a separate function.
encoder_encode(aom_codec_alg_priv_t * ctx,const aom_image_t * img,aom_codec_pts_t pts,unsigned long duration,aom_enc_frame_flags_t enc_flags)2130 static aom_codec_err_t encoder_encode(aom_codec_alg_priv_t *ctx,
2131                                       const aom_image_t *img,
2132                                       aom_codec_pts_t pts,
2133                                       unsigned long duration,
2134                                       aom_enc_frame_flags_t enc_flags) {
2135   const size_t kMinCompressedSize = 8192;
2136   volatile aom_codec_err_t res = AOM_CODEC_OK;
2137   AV1_COMP *const cpi = ctx->cpi;
2138   const aom_rational64_t *const timestamp_ratio = &ctx->timestamp_ratio;
2139   volatile aom_codec_pts_t ptsvol = pts;
2140   // LAP context
2141   AV1_COMP *cpi_lap = ctx->cpi_lap;
2142 
2143   if (cpi == NULL) return AOM_CODEC_INVALID_PARAM;
2144 
2145   if (cpi->lap_enabled && cpi_lap == NULL && cpi->oxcf.pass == 0)
2146     return AOM_CODEC_INVALID_PARAM;
2147 
2148   if (img != NULL) {
2149     res = validate_img(ctx, img);
2150     if (res == AOM_CODEC_OK) {
2151       size_t data_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
2152                        ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) * get_image_bps(img);
2153       if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
2154       if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
2155         ctx->cx_data_sz = data_sz;
2156         free(ctx->cx_data);
2157         ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
2158         if (ctx->cx_data == NULL) {
2159           return AOM_CODEC_MEM_ERROR;
2160         }
2161       }
2162     }
2163   }
2164   if (ctx->oxcf.mode != GOOD && ctx->oxcf.mode != REALTIME) {
2165     ctx->oxcf.mode = GOOD;
2166     av1_change_config(ctx->cpi, &ctx->oxcf);
2167   }
2168 
2169   aom_codec_pkt_list_init(&ctx->pkt_list);
2170 
2171   volatile aom_enc_frame_flags_t flags = enc_flags;
2172 
2173   // The jmp_buf is valid only for the duration of the function that calls
2174   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2175   // before it returns.
2176   if (setjmp(cpi->common.error.jmp)) {
2177     cpi->common.error.setjmp = 0;
2178     res = update_error_state(ctx, &cpi->common.error);
2179     aom_clear_system_state();
2180     return res;
2181   }
2182   cpi->common.error.setjmp = 1;
2183   if (cpi_lap != NULL) {
2184     if (setjmp(cpi_lap->common.error.jmp)) {
2185       cpi_lap->common.error.setjmp = 0;
2186       res = update_error_state(ctx, &cpi_lap->common.error);
2187       aom_clear_system_state();
2188       return res;
2189     }
2190     cpi_lap->common.error.setjmp = 1;
2191   }
2192 
2193   // Note(yunqing): While applying encoding flags, always start from enabling
2194   // all, and then modifying according to the flags. Previous frame's flags are
2195   // overwritten.
2196   av1_apply_encoding_flags(cpi, flags);
2197   if (cpi_lap != NULL) {
2198     av1_apply_encoding_flags(cpi_lap, flags);
2199   }
2200 
2201 #if CONFIG_USE_VMAF_RC
2202   aom_init_vmaf_model_rc(&cpi->vmaf_info.vmaf_model,
2203                          cpi->oxcf.tune_cfg.vmaf_model_path);
2204 #endif
2205 
2206   // Handle fixed keyframe intervals
2207   if (is_stat_generation_stage(cpi)) {
2208     if (ctx->cfg.kf_mode == AOM_KF_AUTO &&
2209         ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
2210       if (cpi->common.spatial_layer_id == 0 &&
2211           ++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
2212         flags |= AOM_EFLAG_FORCE_KF;
2213         ctx->fixed_kf_cntr = 1;
2214       }
2215     }
2216   }
2217 
2218   if (res == AOM_CODEC_OK) {
2219     // Set up internal flags
2220     if (ctx->base.init_flags & AOM_CODEC_USE_PSNR) cpi->b_calculate_psnr = 1;
2221 
2222     if (img != NULL) {
2223       if (!ctx->pts_offset_initialized) {
2224         ctx->pts_offset = ptsvol;
2225         ctx->pts_offset_initialized = 1;
2226       }
2227       ptsvol -= ctx->pts_offset;
2228       int64_t src_time_stamp = timebase_units_to_ticks(timestamp_ratio, ptsvol);
2229       int64_t src_end_time_stamp =
2230           timebase_units_to_ticks(timestamp_ratio, ptsvol + duration);
2231 
2232       YV12_BUFFER_CONFIG sd;
2233       res = image2yuvconfig(img, &sd);
2234       // When generating a monochrome stream, make |sd| a monochrome image.
2235       if (ctx->cfg.monochrome) {
2236         sd.u_buffer = sd.v_buffer = NULL;
2237         sd.uv_stride = 0;
2238         sd.monochrome = 1;
2239       }
2240       int use_highbitdepth = (sd.flags & YV12_FLAG_HIGHBITDEPTH) != 0;
2241       int subsampling_x = sd.subsampling_x;
2242       int subsampling_y = sd.subsampling_y;
2243 
2244       if (!cpi->lookahead) {
2245         int lag_in_frames = cpi_lap != NULL ? cpi_lap->oxcf.gf_cfg.lag_in_frames
2246                                             : cpi->oxcf.gf_cfg.lag_in_frames;
2247 
2248         cpi->lookahead = av1_lookahead_init(
2249             cpi->oxcf.frm_dim_cfg.width, cpi->oxcf.frm_dim_cfg.height,
2250             subsampling_x, subsampling_y, use_highbitdepth, lag_in_frames,
2251             cpi->oxcf.border_in_pixels, cpi->common.features.byte_alignment,
2252             ctx->num_lap_buffers);
2253       }
2254       if (!cpi->lookahead)
2255         aom_internal_error(&cpi->common.error, AOM_CODEC_MEM_ERROR,
2256                            "Failed to allocate lag buffers");
2257 
2258       av1_check_initial_width(cpi, use_highbitdepth, subsampling_x,
2259                               subsampling_y);
2260       if (cpi_lap != NULL) {
2261         cpi_lap->lookahead = cpi->lookahead;
2262         av1_check_initial_width(cpi_lap, use_highbitdepth, subsampling_x,
2263                                 subsampling_y);
2264       }
2265 
2266       // Store the original flags in to the frame buffer. Will extract the
2267       // key frame flag when we actually encode this frame.
2268       if (av1_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
2269                                 src_time_stamp, src_end_time_stamp)) {
2270         res = update_error_state(ctx, &cpi->common.error);
2271       }
2272       ctx->next_frame_flags = 0;
2273     }
2274 
2275     unsigned char *cx_data = ctx->cx_data;
2276     size_t cx_data_sz = ctx->cx_data_sz;
2277 
2278     assert(!(cx_data == NULL && cx_data_sz != 0));
2279 
2280     /* Any pending invisible frames? */
2281     if (ctx->pending_cx_data) {
2282       memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
2283       ctx->pending_cx_data = cx_data;
2284       cx_data += ctx->pending_cx_data_sz;
2285       cx_data_sz -= ctx->pending_cx_data_sz;
2286 
2287       /* TODO: this is a minimal check, the underlying codec doesn't respect
2288        * the buffer size anyway.
2289        */
2290       if (cx_data_sz < ctx->cx_data_sz / 2) {
2291         aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR,
2292                            "Compressed data buffer too small");
2293       }
2294     }
2295 
2296     size_t frame_size = 0;
2297     unsigned int lib_flags = 0;
2298     int is_frame_visible = 0;
2299     int index_size = 0;
2300     int has_no_show_keyframe = 0;
2301     int num_workers = 0;
2302 
2303     if (cpi->oxcf.pass == 1) {
2304 #if !CONFIG_REALTIME_ONLY
2305       num_workers = av1_fp_compute_num_enc_workers(cpi);
2306 #endif
2307     } else {
2308       av1_compute_num_workers_for_mt(cpi);
2309       num_workers = av1_get_max_num_workers(cpi);
2310     }
2311     if ((num_workers > 1) && (cpi->mt_info.num_workers == 0)) {
2312       av1_create_workers(cpi, num_workers);
2313       if (cpi->oxcf.pass != 1) {
2314         av1_create_second_pass_workers(cpi, num_workers);
2315       }
2316     }
2317 
2318     // Call for LAP stage
2319     if (cpi_lap != NULL) {
2320       int64_t dst_time_stamp_la;
2321       int64_t dst_end_time_stamp_la;
2322       if (cpi_lap->mt_info.workers == NULL) {
2323         cpi_lap->mt_info.workers = cpi->mt_info.workers;
2324         cpi_lap->mt_info.tile_thr_data = cpi->mt_info.tile_thr_data;
2325       }
2326       cpi_lap->mt_info.num_workers = cpi->mt_info.num_workers;
2327       const int status = av1_get_compressed_data(
2328           cpi_lap, &lib_flags, &frame_size, NULL, &dst_time_stamp_la,
2329           &dst_end_time_stamp_la, !img, timestamp_ratio);
2330       if (status != -1) {
2331         if (status != AOM_CODEC_OK) {
2332           aom_internal_error(&cpi_lap->common.error, AOM_CODEC_ERROR, NULL);
2333         }
2334         cpi_lap->seq_params_locked = 1;
2335       }
2336       lib_flags = 0;
2337       frame_size = 0;
2338     }
2339 
2340     // Get the next visible frame. Invisible frames get packed with the next
2341     // visible frame.
2342     int64_t dst_time_stamp;
2343     int64_t dst_end_time_stamp;
2344     while (cx_data_sz - index_size >= ctx->cx_data_sz / 2 &&
2345            !is_frame_visible) {
2346       const int status = av1_get_compressed_data(
2347           cpi, &lib_flags, &frame_size, cx_data, &dst_time_stamp,
2348           &dst_end_time_stamp, !img, timestamp_ratio);
2349       if (status == -1) break;
2350       if (status != AOM_CODEC_OK) {
2351         aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
2352       }
2353 
2354       cpi->seq_params_locked = 1;
2355       if (frame_size) {
2356         if (ctx->pending_cx_data == 0) ctx->pending_cx_data = cx_data;
2357 
2358         const int write_temporal_delimiter =
2359             !cpi->common.spatial_layer_id && !ctx->pending_frame_count;
2360 
2361         if (write_temporal_delimiter) {
2362           uint32_t obu_header_size = 1;
2363           const uint32_t obu_payload_size = 0;
2364           const size_t length_field_size =
2365               aom_uleb_size_in_bytes(obu_payload_size);
2366 
2367           if (ctx->pending_cx_data) {
2368             const size_t move_offset = length_field_size + 1;
2369             memmove(ctx->pending_cx_data + move_offset, ctx->pending_cx_data,
2370                     frame_size);
2371           }
2372           const uint32_t obu_header_offset = 0;
2373           obu_header_size = av1_write_obu_header(
2374               &cpi->level_params, OBU_TEMPORAL_DELIMITER, 0,
2375               (uint8_t *)(ctx->pending_cx_data + obu_header_offset));
2376 
2377           // OBUs are preceded/succeeded by an unsigned leb128 coded integer.
2378           if (av1_write_uleb_obu_size(obu_header_size, obu_payload_size,
2379                                       ctx->pending_cx_data) != AOM_CODEC_OK) {
2380             aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
2381           }
2382 
2383           frame_size += obu_header_size + obu_payload_size + length_field_size;
2384         }
2385 
2386         if (ctx->oxcf.save_as_annexb) {
2387           size_t curr_frame_size = frame_size;
2388           if (av1_convert_sect5obus_to_annexb(cx_data, &curr_frame_size) !=
2389               AOM_CODEC_OK) {
2390             aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
2391           }
2392           frame_size = curr_frame_size;
2393 
2394           // B_PRIME (add frame size)
2395           const size_t length_field_size = aom_uleb_size_in_bytes(frame_size);
2396           if (ctx->pending_cx_data) {
2397             const size_t move_offset = length_field_size;
2398             memmove(cx_data + move_offset, cx_data, frame_size);
2399           }
2400           if (av1_write_uleb_obu_size(0, (uint32_t)frame_size, cx_data) !=
2401               AOM_CODEC_OK) {
2402             aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
2403           }
2404           frame_size += length_field_size;
2405         }
2406 
2407         ctx->pending_frame_sizes[ctx->pending_frame_count++] = frame_size;
2408         ctx->pending_cx_data_sz += frame_size;
2409 
2410         cx_data += frame_size;
2411         cx_data_sz -= frame_size;
2412 
2413         index_size = MAG_SIZE * (ctx->pending_frame_count - 1) + 2;
2414 
2415         is_frame_visible = cpi->common.show_frame;
2416 
2417         has_no_show_keyframe |=
2418             (!is_frame_visible &&
2419              cpi->common.current_frame.frame_type == KEY_FRAME);
2420       }
2421     }
2422     if (is_frame_visible) {
2423       // Add the frame packet to the list of returned packets.
2424       aom_codec_cx_pkt_t pkt;
2425 
2426       // decrement frames_left counter
2427       cpi->frames_left = AOMMAX(0, cpi->frames_left - 1);
2428       if (ctx->oxcf.save_as_annexb) {
2429         //  B_PRIME (add TU size)
2430         size_t tu_size = ctx->pending_cx_data_sz;
2431         const size_t length_field_size = aom_uleb_size_in_bytes(tu_size);
2432         if (ctx->pending_cx_data) {
2433           const size_t move_offset = length_field_size;
2434           memmove(ctx->pending_cx_data + move_offset, ctx->pending_cx_data,
2435                   tu_size);
2436         }
2437         if (av1_write_uleb_obu_size(0, (uint32_t)tu_size,
2438                                     ctx->pending_cx_data) != AOM_CODEC_OK) {
2439           aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
2440         }
2441         ctx->pending_cx_data_sz += length_field_size;
2442       }
2443 
2444       pkt.kind = AOM_CODEC_CX_FRAME_PKT;
2445 
2446       pkt.data.frame.buf = ctx->pending_cx_data;
2447       pkt.data.frame.sz = ctx->pending_cx_data_sz;
2448       pkt.data.frame.partition_id = -1;
2449       pkt.data.frame.vis_frame_size = frame_size;
2450 
2451       pkt.data.frame.pts =
2452           ticks_to_timebase_units(timestamp_ratio, dst_time_stamp) +
2453           ctx->pts_offset;
2454       pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
2455       if (has_no_show_keyframe) {
2456         // If one of the invisible frames in the packet is a keyframe, set
2457         // the delayed random access point flag.
2458         pkt.data.frame.flags |= AOM_FRAME_IS_DELAYED_RANDOM_ACCESS_POINT;
2459       }
2460       pkt.data.frame.duration = (uint32_t)ticks_to_timebase_units(
2461           timestamp_ratio, dst_end_time_stamp - dst_time_stamp);
2462 
2463       aom_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
2464 
2465       ctx->pending_cx_data = NULL;
2466       ctx->pending_cx_data_sz = 0;
2467       ctx->pending_frame_count = 0;
2468     }
2469   }
2470 
2471   cpi->common.error.setjmp = 0;
2472   return res;
2473 }
2474 
encoder_get_cxdata(aom_codec_alg_priv_t * ctx,aom_codec_iter_t * iter)2475 static const aom_codec_cx_pkt_t *encoder_get_cxdata(aom_codec_alg_priv_t *ctx,
2476                                                     aom_codec_iter_t *iter) {
2477   return aom_codec_pkt_list_get(&ctx->pkt_list.head, iter);
2478 }
2479 
ctrl_set_reference(aom_codec_alg_priv_t * ctx,va_list args)2480 static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
2481                                           va_list args) {
2482   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
2483 
2484   if (frame != NULL) {
2485     YV12_BUFFER_CONFIG sd;
2486 
2487     image2yuvconfig(&frame->img, &sd);
2488     av1_set_reference_enc(ctx->cpi, frame->idx, &sd);
2489     return AOM_CODEC_OK;
2490   } else {
2491     return AOM_CODEC_INVALID_PARAM;
2492   }
2493 }
2494 
ctrl_copy_reference(aom_codec_alg_priv_t * ctx,va_list args)2495 static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
2496                                            va_list args) {
2497   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
2498 
2499   if (frame != NULL) {
2500     YV12_BUFFER_CONFIG sd;
2501 
2502     image2yuvconfig(&frame->img, &sd);
2503     av1_copy_reference_enc(ctx->cpi, frame->idx, &sd);
2504     return AOM_CODEC_OK;
2505   } else {
2506     return AOM_CODEC_INVALID_PARAM;
2507   }
2508 }
2509 
ctrl_get_reference(aom_codec_alg_priv_t * ctx,va_list args)2510 static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
2511                                           va_list args) {
2512   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
2513 
2514   if (frame != NULL) {
2515     YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
2516     if (fb == NULL) return AOM_CODEC_ERROR;
2517 
2518     yuvconfig2image(&frame->img, fb, NULL);
2519     return AOM_CODEC_OK;
2520   } else {
2521     return AOM_CODEC_INVALID_PARAM;
2522   }
2523 }
2524 
ctrl_get_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)2525 static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
2526                                                 va_list args) {
2527   aom_image_t *const new_img = va_arg(args, aom_image_t *);
2528 
2529   if (new_img != NULL) {
2530     YV12_BUFFER_CONFIG new_frame;
2531 
2532     if (av1_get_last_show_frame(ctx->cpi, &new_frame) == 0) {
2533       yuvconfig2image(new_img, &new_frame, NULL);
2534       return AOM_CODEC_OK;
2535     } else {
2536       return AOM_CODEC_ERROR;
2537     }
2538   } else {
2539     return AOM_CODEC_INVALID_PARAM;
2540   }
2541 }
2542 
ctrl_copy_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)2543 static aom_codec_err_t ctrl_copy_new_frame_image(aom_codec_alg_priv_t *ctx,
2544                                                  va_list args) {
2545   aom_image_t *const new_img = va_arg(args, aom_image_t *);
2546 
2547   if (new_img != NULL) {
2548     YV12_BUFFER_CONFIG new_frame;
2549 
2550     if (av1_get_last_show_frame(ctx->cpi, &new_frame) == 0) {
2551       YV12_BUFFER_CONFIG sd;
2552       image2yuvconfig(new_img, &sd);
2553       return av1_copy_new_frame_enc(&ctx->cpi->common, &new_frame, &sd);
2554     } else {
2555       return AOM_CODEC_ERROR;
2556     }
2557   } else {
2558     return AOM_CODEC_INVALID_PARAM;
2559   }
2560 }
2561 
encoder_get_preview(aom_codec_alg_priv_t * ctx)2562 static aom_image_t *encoder_get_preview(aom_codec_alg_priv_t *ctx) {
2563   YV12_BUFFER_CONFIG sd;
2564 
2565   if (av1_get_preview_raw_frame(ctx->cpi, &sd) == 0) {
2566     yuvconfig2image(&ctx->preview_img, &sd, NULL);
2567     return &ctx->preview_img;
2568   } else {
2569     return NULL;
2570   }
2571 }
2572 
ctrl_use_reference(aom_codec_alg_priv_t * ctx,va_list args)2573 static aom_codec_err_t ctrl_use_reference(aom_codec_alg_priv_t *ctx,
2574                                           va_list args) {
2575   const int reference_flag = va_arg(args, int);
2576 
2577   av1_use_as_reference(&ctx->cpi->ext_flags.ref_frame_flags, reference_flag);
2578   return AOM_CODEC_OK;
2579 }
2580 
ctrl_set_roi_map(aom_codec_alg_priv_t * ctx,va_list args)2581 static aom_codec_err_t ctrl_set_roi_map(aom_codec_alg_priv_t *ctx,
2582                                         va_list args) {
2583   (void)ctx;
2584   (void)args;
2585 
2586   // TODO(yaowu): Need to re-implement and test for AV1.
2587   return AOM_CODEC_INVALID_PARAM;
2588 }
2589 
ctrl_set_active_map(aom_codec_alg_priv_t * ctx,va_list args)2590 static aom_codec_err_t ctrl_set_active_map(aom_codec_alg_priv_t *ctx,
2591                                            va_list args) {
2592   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
2593 
2594   if (map) {
2595     if (!av1_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
2596                             (int)map->cols))
2597       return AOM_CODEC_OK;
2598     else
2599       return AOM_CODEC_INVALID_PARAM;
2600   } else {
2601     return AOM_CODEC_INVALID_PARAM;
2602   }
2603 }
2604 
ctrl_get_active_map(aom_codec_alg_priv_t * ctx,va_list args)2605 static aom_codec_err_t ctrl_get_active_map(aom_codec_alg_priv_t *ctx,
2606                                            va_list args) {
2607   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
2608 
2609   if (map) {
2610     if (!av1_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
2611                             (int)map->cols))
2612       return AOM_CODEC_OK;
2613     else
2614       return AOM_CODEC_INVALID_PARAM;
2615   } else {
2616     return AOM_CODEC_INVALID_PARAM;
2617   }
2618 }
2619 
ctrl_set_scale_mode(aom_codec_alg_priv_t * ctx,va_list args)2620 static aom_codec_err_t ctrl_set_scale_mode(aom_codec_alg_priv_t *ctx,
2621                                            va_list args) {
2622   aom_scaling_mode_t *const mode = va_arg(args, aom_scaling_mode_t *);
2623 
2624   if (mode) {
2625     const int res = av1_set_internal_size(
2626         &ctx->cpi->oxcf, &ctx->cpi->resize_pending_params,
2627         (AOM_SCALING)mode->h_scaling_mode, (AOM_SCALING)mode->v_scaling_mode);
2628     return (res == 0) ? AOM_CODEC_OK : AOM_CODEC_INVALID_PARAM;
2629   } else {
2630     return AOM_CODEC_INVALID_PARAM;
2631   }
2632 }
2633 
ctrl_set_spatial_layer_id(aom_codec_alg_priv_t * ctx,va_list args)2634 static aom_codec_err_t ctrl_set_spatial_layer_id(aom_codec_alg_priv_t *ctx,
2635                                                  va_list args) {
2636   const int spatial_layer_id = va_arg(args, int);
2637   if (spatial_layer_id >= MAX_NUM_SPATIAL_LAYERS)
2638     return AOM_CODEC_INVALID_PARAM;
2639   ctx->cpi->common.spatial_layer_id = spatial_layer_id;
2640   return AOM_CODEC_OK;
2641 }
2642 
ctrl_set_number_spatial_layers(aom_codec_alg_priv_t * ctx,va_list args)2643 static aom_codec_err_t ctrl_set_number_spatial_layers(aom_codec_alg_priv_t *ctx,
2644                                                       va_list args) {
2645   const int number_spatial_layers = va_arg(args, int);
2646   if (number_spatial_layers > MAX_NUM_SPATIAL_LAYERS)
2647     return AOM_CODEC_INVALID_PARAM;
2648   ctx->cpi->common.number_spatial_layers = number_spatial_layers;
2649   return AOM_CODEC_OK;
2650 }
2651 
ctrl_set_layer_id(aom_codec_alg_priv_t * ctx,va_list args)2652 static aom_codec_err_t ctrl_set_layer_id(aom_codec_alg_priv_t *ctx,
2653                                          va_list args) {
2654   aom_svc_layer_id_t *const data = va_arg(args, aom_svc_layer_id_t *);
2655   ctx->cpi->common.spatial_layer_id = data->spatial_layer_id;
2656   ctx->cpi->common.temporal_layer_id = data->temporal_layer_id;
2657   ctx->cpi->svc.spatial_layer_id = data->spatial_layer_id;
2658   ctx->cpi->svc.temporal_layer_id = data->temporal_layer_id;
2659   return AOM_CODEC_OK;
2660 }
2661 
ctrl_set_svc_params(aom_codec_alg_priv_t * ctx,va_list args)2662 static aom_codec_err_t ctrl_set_svc_params(aom_codec_alg_priv_t *ctx,
2663                                            va_list args) {
2664   AV1_COMP *const cpi = ctx->cpi;
2665   AV1_COMMON *const cm = &cpi->common;
2666   aom_svc_params_t *const params = va_arg(args, aom_svc_params_t *);
2667   cm->number_spatial_layers = params->number_spatial_layers;
2668   cm->number_temporal_layers = params->number_temporal_layers;
2669   cpi->svc.number_spatial_layers = params->number_spatial_layers;
2670   cpi->svc.number_temporal_layers = params->number_temporal_layers;
2671   if (cm->number_spatial_layers > 1 || cm->number_temporal_layers > 1) {
2672     unsigned int sl, tl;
2673     cpi->use_svc = 1;
2674     for (sl = 0; sl < cm->number_spatial_layers; ++sl) {
2675       for (tl = 0; tl < cm->number_temporal_layers; ++tl) {
2676         const int layer = LAYER_IDS_TO_IDX(sl, tl, cm->number_temporal_layers);
2677         LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
2678         lc->max_q = params->max_quantizers[layer];
2679         lc->min_q = params->min_quantizers[layer];
2680         lc->scaling_factor_num = params->scaling_factor_num[sl];
2681         lc->scaling_factor_den = params->scaling_factor_den[sl];
2682         lc->layer_target_bitrate = 1000 * params->layer_target_bitrate[layer];
2683         lc->framerate_factor = params->framerate_factor[tl];
2684       }
2685     }
2686     if (cm->current_frame.frame_number == 0) {
2687       if (!cpi->seq_params_locked) {
2688         SequenceHeader *const seq_params = &cm->seq_params;
2689         seq_params->operating_points_cnt_minus_1 =
2690             cm->number_spatial_layers * cm->number_temporal_layers - 1;
2691         av1_init_seq_coding_tools(&cm->seq_params, cm, &cpi->oxcf, 1);
2692       }
2693       av1_init_layer_context(cpi);
2694     }
2695     av1_update_layer_context_change_config(cpi,
2696                                            cpi->oxcf.rc_cfg.target_bandwidth);
2697   }
2698   return AOM_CODEC_OK;
2699 }
2700 
ctrl_set_svc_ref_frame_config(aom_codec_alg_priv_t * ctx,va_list args)2701 static aom_codec_err_t ctrl_set_svc_ref_frame_config(aom_codec_alg_priv_t *ctx,
2702                                                      va_list args) {
2703   AV1_COMP *const cpi = ctx->cpi;
2704   aom_svc_ref_frame_config_t *const data =
2705       va_arg(args, aom_svc_ref_frame_config_t *);
2706   cpi->svc.external_ref_frame_config = 1;
2707   for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
2708     cpi->svc.reference[i] = data->reference[i];
2709     cpi->svc.ref_idx[i] = data->ref_idx[i];
2710   }
2711   for (unsigned int i = 0; i < REF_FRAMES; ++i)
2712     cpi->svc.refresh[i] = data->refresh[i];
2713   return AOM_CODEC_OK;
2714 }
2715 
ctrl_set_tune_content(aom_codec_alg_priv_t * ctx,va_list args)2716 static aom_codec_err_t ctrl_set_tune_content(aom_codec_alg_priv_t *ctx,
2717                                              va_list args) {
2718   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2719   extra_cfg.content = CAST(AV1E_SET_TUNE_CONTENT, args);
2720   return update_extra_cfg(ctx, &extra_cfg);
2721 }
2722 
ctrl_set_cdf_update_mode(aom_codec_alg_priv_t * ctx,va_list args)2723 static aom_codec_err_t ctrl_set_cdf_update_mode(aom_codec_alg_priv_t *ctx,
2724                                                 va_list args) {
2725   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2726   extra_cfg.cdf_update_mode = CAST(AV1E_SET_CDF_UPDATE_MODE, args);
2727   return update_extra_cfg(ctx, &extra_cfg);
2728 }
2729 
ctrl_set_color_primaries(aom_codec_alg_priv_t * ctx,va_list args)2730 static aom_codec_err_t ctrl_set_color_primaries(aom_codec_alg_priv_t *ctx,
2731                                                 va_list args) {
2732   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2733   extra_cfg.color_primaries = CAST(AV1E_SET_COLOR_PRIMARIES, args);
2734   return update_extra_cfg(ctx, &extra_cfg);
2735 }
2736 
ctrl_set_transfer_characteristics(aom_codec_alg_priv_t * ctx,va_list args)2737 static aom_codec_err_t ctrl_set_transfer_characteristics(
2738     aom_codec_alg_priv_t *ctx, va_list args) {
2739   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2740   extra_cfg.transfer_characteristics =
2741       CAST(AV1E_SET_TRANSFER_CHARACTERISTICS, args);
2742   return update_extra_cfg(ctx, &extra_cfg);
2743 }
2744 
ctrl_set_matrix_coefficients(aom_codec_alg_priv_t * ctx,va_list args)2745 static aom_codec_err_t ctrl_set_matrix_coefficients(aom_codec_alg_priv_t *ctx,
2746                                                     va_list args) {
2747   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2748   extra_cfg.matrix_coefficients = CAST(AV1E_SET_MATRIX_COEFFICIENTS, args);
2749   return update_extra_cfg(ctx, &extra_cfg);
2750 }
2751 
ctrl_set_chroma_sample_position(aom_codec_alg_priv_t * ctx,va_list args)2752 static aom_codec_err_t ctrl_set_chroma_sample_position(
2753     aom_codec_alg_priv_t *ctx, va_list args) {
2754   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2755   extra_cfg.chroma_sample_position =
2756       CAST(AV1E_SET_CHROMA_SAMPLE_POSITION, args);
2757   return update_extra_cfg(ctx, &extra_cfg);
2758 }
2759 
ctrl_set_color_range(aom_codec_alg_priv_t * ctx,va_list args)2760 static aom_codec_err_t ctrl_set_color_range(aom_codec_alg_priv_t *ctx,
2761                                             va_list args) {
2762   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2763   extra_cfg.color_range = CAST(AV1E_SET_COLOR_RANGE, args);
2764   return update_extra_cfg(ctx, &extra_cfg);
2765 }
2766 
ctrl_set_render_size(aom_codec_alg_priv_t * ctx,va_list args)2767 static aom_codec_err_t ctrl_set_render_size(aom_codec_alg_priv_t *ctx,
2768                                             va_list args) {
2769   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2770   int *const render_size = va_arg(args, int *);
2771   extra_cfg.render_width = render_size[0];
2772   extra_cfg.render_height = render_size[1];
2773   return update_extra_cfg(ctx, &extra_cfg);
2774 }
2775 
ctrl_set_superblock_size(aom_codec_alg_priv_t * ctx,va_list args)2776 static aom_codec_err_t ctrl_set_superblock_size(aom_codec_alg_priv_t *ctx,
2777                                                 va_list args) {
2778   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2779   extra_cfg.superblock_size = CAST(AV1E_SET_SUPERBLOCK_SIZE, args);
2780   return update_extra_cfg(ctx, &extra_cfg);
2781 }
2782 
ctrl_set_chroma_subsampling_x(aom_codec_alg_priv_t * ctx,va_list args)2783 static aom_codec_err_t ctrl_set_chroma_subsampling_x(aom_codec_alg_priv_t *ctx,
2784                                                      va_list args) {
2785   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2786   extra_cfg.chroma_subsampling_x = CAST(AV1E_SET_CHROMA_SUBSAMPLING_X, args);
2787   return update_extra_cfg(ctx, &extra_cfg);
2788 }
2789 
ctrl_set_chroma_subsampling_y(aom_codec_alg_priv_t * ctx,va_list args)2790 static aom_codec_err_t ctrl_set_chroma_subsampling_y(aom_codec_alg_priv_t *ctx,
2791                                                      va_list args) {
2792   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2793   extra_cfg.chroma_subsampling_y = CAST(AV1E_SET_CHROMA_SUBSAMPLING_Y, args);
2794   return update_extra_cfg(ctx, &extra_cfg);
2795 }
2796 
encoder_set_option(aom_codec_alg_priv_t * ctx,const char * name,const char * value)2797 static aom_codec_err_t encoder_set_option(aom_codec_alg_priv_t *ctx,
2798                                           const char *name, const char *value) {
2799   if (ctx == NULL || name == NULL || value == NULL)
2800     return AOM_CODEC_INVALID_PARAM;
2801   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2802   // Used to mock the argv with just one string "--{name}={value}"
2803   char *argv[2] = { NULL, "" };
2804   size_t len = strlen(name) + strlen(value) + 4;
2805   char *err_string = ctx->cpi->common.error.detail;
2806 
2807 #if __STDC_VERSION__ >= 201112L
2808   // We use the keyword _Static_assert because clang-cl does not allow the
2809   // convenience macro static_assert to be used in function scope. See
2810   // https://bugs.llvm.org/show_bug.cgi?id=48904.
2811   _Static_assert(sizeof(ctx->cpi->common.error.detail) >= ARG_ERR_MSG_MAX_LEN,
2812                  "The size of the err_msg buffer for arg_match_helper must be "
2813                  "at least ARG_ERR_MSG_MAX_LEN");
2814 #else
2815   assert(sizeof(ctx->cpi->common.error.detail) >= ARG_ERR_MSG_MAX_LEN);
2816 #endif
2817 
2818   argv[0] = aom_malloc(len * sizeof(argv[1][0]));
2819   snprintf(argv[0], len, "--%s=%s", name, value);
2820   struct arg arg;
2821 
2822   int match = 1;
2823   if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_keyframe_filtering,
2824                        argv, err_string)) {
2825     extra_cfg.enable_keyframe_filtering =
2826         arg_parse_uint_helper(&arg, err_string);
2827   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.min_gf_interval, argv,
2828                               err_string)) {
2829     extra_cfg.min_gf_interval = arg_parse_uint_helper(&arg, err_string);
2830   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_gf_interval, argv,
2831                               err_string)) {
2832     extra_cfg.max_gf_interval = arg_parse_uint_helper(&arg, err_string);
2833   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.gf_min_pyr_height,
2834                               argv, err_string)) {
2835     extra_cfg.gf_min_pyr_height = arg_parse_uint_helper(&arg, err_string);
2836   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.gf_max_pyr_height,
2837                               argv, err_string)) {
2838     extra_cfg.gf_max_pyr_height = arg_parse_uint_helper(&arg, err_string);
2839   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.cpu_used_av1, argv,
2840                               err_string)) {
2841     extra_cfg.cpu_used = arg_parse_uint_helper(&arg, err_string);
2842   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.auto_altref, argv,
2843                               err_string)) {
2844     extra_cfg.enable_auto_alt_ref = arg_parse_uint_helper(&arg, err_string);
2845   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.noise_sens, argv,
2846                               err_string)) {
2847     extra_cfg.noise_sensitivity = arg_parse_uint_helper(&arg, err_string);
2848   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.sharpness, argv,
2849                               err_string)) {
2850     extra_cfg.sharpness = arg_parse_uint_helper(&arg, err_string);
2851   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.static_thresh, argv,
2852                               err_string)) {
2853     extra_cfg.static_thresh = arg_parse_uint_helper(&arg, err_string);
2854   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.rowmtarg, argv,
2855                               err_string)) {
2856     extra_cfg.row_mt = arg_parse_uint_helper(&arg, err_string);
2857   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tile_cols, argv,
2858                               err_string)) {
2859     extra_cfg.tile_columns = arg_parse_uint_helper(&arg, err_string);
2860   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tile_rows, argv,
2861                               err_string)) {
2862     extra_cfg.tile_rows = arg_parse_uint_helper(&arg, err_string);
2863   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_tpl_model,
2864                               argv, err_string)) {
2865     extra_cfg.enable_tpl_model = arg_parse_uint_helper(&arg, err_string);
2866   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.arnr_maxframes, argv,
2867                               err_string)) {
2868     extra_cfg.arnr_max_frames = arg_parse_uint_helper(&arg, err_string);
2869   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.arnr_strength, argv,
2870                               err_string)) {
2871     extra_cfg.arnr_strength = arg_parse_uint_helper(&arg, err_string);
2872   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tune_metric, argv,
2873                               err_string)) {
2874     extra_cfg.tuning = arg_parse_enum_helper(&arg, err_string);
2875   }
2876 #if CONFIG_TUNE_VMAF
2877   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.vmaf_model_path, argv,
2878                             err_string)) {
2879     extra_cfg.vmaf_model_path = value;
2880   }
2881 #endif
2882   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.cq_level, argv,
2883                             err_string)) {
2884     extra_cfg.cq_level = arg_parse_uint_helper(&arg, err_string);
2885   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_intra_rate_pct,
2886                               argv, err_string)) {
2887     extra_cfg.rc_max_intra_bitrate_pct =
2888         arg_parse_uint_helper(&arg, err_string);
2889   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_inter_rate_pct,
2890                               argv, err_string)) {
2891     extra_cfg.rc_max_inter_bitrate_pct =
2892         arg_parse_uint_helper(&arg, err_string);
2893   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.gf_cbr_boost_pct,
2894                               argv, err_string)) {
2895     extra_cfg.gf_cbr_boost_pct = arg_parse_uint_helper(&arg, err_string);
2896   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.lossless, argv,
2897                               err_string)) {
2898     extra_cfg.lossless = arg_parse_uint_helper(&arg, err_string);
2899   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_cdef, argv,
2900                               err_string)) {
2901     extra_cfg.enable_cdef = arg_parse_uint_helper(&arg, err_string);
2902   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_restoration,
2903                               argv, err_string)) {
2904     extra_cfg.enable_restoration = arg_parse_uint_helper(&arg, err_string);
2905   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.force_video_mode,
2906                               argv, err_string)) {
2907     extra_cfg.force_video_mode = arg_parse_uint_helper(&arg, err_string);
2908   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_obmc, argv,
2909                               err_string)) {
2910     extra_cfg.enable_obmc = arg_parse_uint_helper(&arg, err_string);
2911   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.disable_trellis_quant,
2912                               argv, err_string)) {
2913     extra_cfg.disable_trellis_quant = arg_parse_uint_helper(&arg, err_string);
2914   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_qm, argv,
2915                               err_string)) {
2916     extra_cfg.enable_qm = arg_parse_uint_helper(&arg, err_string);
2917   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.qm_max, argv,
2918                               err_string)) {
2919     extra_cfg.qm_max = arg_parse_uint_helper(&arg, err_string);
2920   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.qm_min, argv,
2921                               err_string)) {
2922     extra_cfg.qm_min = arg_parse_uint_helper(&arg, err_string);
2923   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.num_tg, argv,
2924                               err_string)) {
2925     extra_cfg.num_tg = arg_parse_uint_helper(&arg, err_string);
2926   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.mtu_size, argv,
2927                               err_string)) {
2928     extra_cfg.mtu_size = arg_parse_uint_helper(&arg, err_string);
2929   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.timing_info, argv,
2930                               err_string)) {
2931     extra_cfg.timing_info_type = arg_parse_enum_helper(&arg, err_string);
2932   } else if (arg_match_helper(&arg,
2933                               &g_av1_codec_arg_defs.frame_parallel_decoding,
2934                               argv, err_string)) {
2935     extra_cfg.frame_parallel_decoding_mode =
2936         arg_parse_uint_helper(&arg, err_string);
2937   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_dual_filter,
2938                               argv, err_string)) {
2939     extra_cfg.enable_dual_filter = arg_parse_uint_helper(&arg, err_string);
2940   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_chroma_deltaq,
2941                               argv, err_string)) {
2942     extra_cfg.enable_chroma_deltaq = arg_parse_uint_helper(&arg, err_string);
2943   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.aq_mode, argv,
2944                               err_string)) {
2945     extra_cfg.aq_mode = arg_parse_uint_helper(&arg, err_string);
2946   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.deltaq_mode, argv,
2947                               err_string)) {
2948     extra_cfg.deltaq_mode = arg_parse_uint_helper(&arg, err_string);
2949   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.deltalf_mode, argv,
2950                               err_string)) {
2951     extra_cfg.deltalf_mode = arg_parse_uint_helper(&arg, err_string);
2952   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.frame_periodic_boost,
2953                               argv, err_string)) {
2954     extra_cfg.frame_periodic_boost = arg_parse_uint_helper(&arg, err_string);
2955   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tune_content, argv,
2956                               err_string)) {
2957     extra_cfg.content = arg_parse_enum_helper(&arg, err_string);
2958   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.input_color_primaries,
2959                               argv, err_string)) {
2960     extra_cfg.color_primaries = arg_parse_enum_helper(&arg, err_string);
2961   } else if (arg_match_helper(
2962                  &arg, &g_av1_codec_arg_defs.input_transfer_characteristics,
2963                  argv, err_string)) {
2964     extra_cfg.transfer_characteristics =
2965         arg_parse_enum_helper(&arg, err_string);
2966   } else if (arg_match_helper(&arg,
2967                               &g_av1_codec_arg_defs.input_matrix_coefficients,
2968                               argv, err_string)) {
2969     extra_cfg.matrix_coefficients = arg_parse_enum_helper(&arg, err_string);
2970   } else if (arg_match_helper(
2971                  &arg, &g_av1_codec_arg_defs.input_chroma_sample_position, argv,
2972                  err_string)) {
2973     extra_cfg.chroma_sample_position = arg_parse_enum_helper(&arg, err_string);
2974   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.superblock_size, argv,
2975                               err_string)) {
2976     extra_cfg.superblock_size = arg_parse_enum_helper(&arg, err_string);
2977   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.error_resilient_mode,
2978                               argv, err_string)) {
2979     extra_cfg.error_resilient_mode = arg_parse_int_helper(&arg, err_string);
2980   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.sframe_mode, argv,
2981                               err_string)) {
2982     extra_cfg.s_frame_mode = arg_parse_int_helper(&arg, err_string);
2983   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.film_grain_test, argv,
2984                               err_string)) {
2985     extra_cfg.film_grain_test_vector = arg_parse_int_helper(&arg, err_string);
2986   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.film_grain_table,
2987                               argv, err_string)) {
2988     extra_cfg.film_grain_table_filename = value;
2989   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.cdf_update_mode, argv,
2990                               err_string)) {
2991     extra_cfg.cdf_update_mode = arg_parse_int_helper(&arg, err_string);
2992   } else if (arg_match_helper(&arg,
2993                               &g_av1_codec_arg_defs.enable_rect_partitions,
2994                               argv, err_string)) {
2995     extra_cfg.enable_rect_partitions = arg_parse_int_helper(&arg, err_string);
2996   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_ab_partitions,
2997                               argv, err_string)) {
2998     extra_cfg.enable_ab_partitions = arg_parse_int_helper(&arg, err_string);
2999   } else if (arg_match_helper(&arg,
3000                               &g_av1_codec_arg_defs.enable_1to4_partitions,
3001                               argv, err_string)) {
3002     extra_cfg.enable_1to4_partitions = arg_parse_int_helper(&arg, err_string);
3003   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.min_partition_size,
3004                               argv, err_string)) {
3005     extra_cfg.min_partition_size = arg_parse_int_helper(&arg, err_string);
3006   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_partition_size,
3007                               argv, err_string)) {
3008     extra_cfg.max_partition_size = arg_parse_int_helper(&arg, err_string);
3009   } else if (arg_match_helper(&arg,
3010                               &g_av1_codec_arg_defs.enable_intra_edge_filter,
3011                               argv, err_string)) {
3012     extra_cfg.enable_intra_edge_filter =
3013         arg_parse_uint_helper(&arg, err_string);
3014   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_order_hint,
3015                               argv, err_string)) {
3016     extra_cfg.enable_order_hint = arg_parse_int_helper(&arg, err_string);
3017   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_tx64, argv,
3018                               err_string)) {
3019     extra_cfg.enable_tx64 = arg_parse_int_helper(&arg, err_string);
3020   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_flip_idtx,
3021                               argv, err_string)) {
3022     extra_cfg.enable_flip_idtx = arg_parse_int_helper(&arg, err_string);
3023   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_rect_tx, argv,
3024                               err_string)) {
3025     extra_cfg.enable_rect_tx = arg_parse_int_helper(&arg, err_string);
3026   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_dist_wtd_comp,
3027                               argv, err_string)) {
3028     extra_cfg.enable_dist_wtd_comp = arg_parse_int_helper(&arg, err_string);
3029   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_reference_frames,
3030                               argv, err_string)) {
3031     extra_cfg.max_reference_frames = arg_parse_int_helper(&arg, err_string);
3032   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.reduced_reference_set,
3033                               argv, err_string)) {
3034     extra_cfg.enable_reduced_reference_set =
3035         arg_parse_int_helper(&arg, err_string);
3036   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_ref_frame_mvs,
3037                               argv, err_string)) {
3038     extra_cfg.enable_ref_frame_mvs = arg_parse_int_helper(&arg, err_string);
3039   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_masked_comp,
3040                               argv, err_string)) {
3041     extra_cfg.enable_masked_comp = arg_parse_int_helper(&arg, err_string);
3042   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_onesided_comp,
3043                               argv, err_string)) {
3044     extra_cfg.enable_onesided_comp = arg_parse_int_helper(&arg, err_string);
3045   } else if (arg_match_helper(&arg,
3046                               &g_av1_codec_arg_defs.enable_interintra_comp,
3047                               argv, err_string)) {
3048     extra_cfg.enable_interintra_comp = arg_parse_int_helper(&arg, err_string);
3049   } else if (arg_match_helper(&arg,
3050                               &g_av1_codec_arg_defs.enable_smooth_interintra,
3051                               argv, err_string)) {
3052     extra_cfg.enable_smooth_interintra = arg_parse_int_helper(&arg, err_string);
3053   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_diff_wtd_comp,
3054                               argv, err_string)) {
3055     extra_cfg.enable_diff_wtd_comp = arg_parse_int_helper(&arg, err_string);
3056   } else if (arg_match_helper(&arg,
3057                               &g_av1_codec_arg_defs.enable_interinter_wedge,
3058                               argv, err_string)) {
3059     extra_cfg.enable_interinter_wedge = arg_parse_int_helper(&arg, err_string);
3060   } else if (arg_match_helper(&arg,
3061                               &g_av1_codec_arg_defs.enable_interintra_wedge,
3062                               argv, err_string)) {
3063     extra_cfg.enable_interintra_wedge = arg_parse_int_helper(&arg, err_string);
3064   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_global_motion,
3065                               argv, err_string)) {
3066     extra_cfg.enable_global_motion = arg_parse_int_helper(&arg, err_string);
3067   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_warped_motion,
3068                               argv, err_string)) {
3069     extra_cfg.enable_warped_motion = arg_parse_int_helper(&arg, err_string);
3070   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_filter_intra,
3071                               argv, err_string)) {
3072     extra_cfg.enable_filter_intra = arg_parse_int_helper(&arg, err_string);
3073   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_smooth_intra,
3074                               argv, err_string)) {
3075     extra_cfg.enable_smooth_intra = arg_parse_int_helper(&arg, err_string);
3076   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_paeth_intra,
3077                               argv, err_string)) {
3078     extra_cfg.enable_paeth_intra = arg_parse_int_helper(&arg, err_string);
3079   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_cfl_intra,
3080                               argv, err_string)) {
3081     extra_cfg.enable_cfl_intra = arg_parse_int_helper(&arg, err_string);
3082   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_overlay, argv,
3083                               err_string)) {
3084     extra_cfg.enable_overlay = arg_parse_int_helper(&arg, err_string);
3085   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_palette, argv,
3086                               err_string)) {
3087     extra_cfg.enable_palette = arg_parse_int_helper(&arg, err_string);
3088   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_intrabc, argv,
3089                               err_string)) {
3090     extra_cfg.enable_intrabc = arg_parse_int_helper(&arg, err_string);
3091   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_angle_delta,
3092                               argv, err_string)) {
3093     extra_cfg.enable_angle_delta = arg_parse_int_helper(&arg, err_string);
3094   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.reduced_tx_type_set,
3095                               argv, err_string)) {
3096     extra_cfg.reduced_tx_type_set = arg_parse_int_helper(&arg, err_string);
3097   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.use_intra_dct_only,
3098                               argv, err_string)) {
3099     extra_cfg.use_intra_dct_only = arg_parse_int_helper(&arg, err_string);
3100   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.use_inter_dct_only,
3101                               argv, err_string)) {
3102     extra_cfg.use_inter_dct_only = arg_parse_int_helper(&arg, err_string);
3103   } else if (arg_match_helper(&arg,
3104                               &g_av1_codec_arg_defs.use_intra_default_tx_only,
3105                               argv, err_string)) {
3106     extra_cfg.use_intra_default_tx_only =
3107         arg_parse_int_helper(&arg, err_string);
3108   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.quant_b_adapt, argv,
3109                               err_string)) {
3110     extra_cfg.quant_b_adapt = arg_parse_int_helper(&arg, err_string);
3111   } else if (arg_match_helper(&arg,
3112                               &g_av1_codec_arg_defs.vbr_corpus_complexity_lap,
3113                               argv, err_string)) {
3114     extra_cfg.vbr_corpus_complexity_lap =
3115         arg_parse_uint_helper(&arg, err_string);
3116   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.set_tier_mask, argv,
3117                               err_string)) {
3118     extra_cfg.tier_mask = arg_parse_uint_helper(&arg, err_string);
3119   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.set_min_cr, argv,
3120                               err_string)) {
3121     extra_cfg.min_cr = arg_parse_uint_helper(&arg, err_string);
3122   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.coeff_cost_upd_freq,
3123                               argv, err_string)) {
3124     extra_cfg.coeff_cost_upd_freq = arg_parse_uint_helper(&arg, err_string);
3125   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.mode_cost_upd_freq,
3126                               argv, err_string)) {
3127     extra_cfg.mode_cost_upd_freq = arg_parse_uint_helper(&arg, err_string);
3128   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.mv_cost_upd_freq,
3129                               argv, err_string)) {
3130     extra_cfg.mv_cost_upd_freq = arg_parse_uint_helper(&arg, err_string);
3131   }
3132 #if CONFIG_DENOISE
3133   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.denoise_noise_level,
3134                             argv, err_string)) {
3135     extra_cfg.noise_level =
3136         (float)arg_parse_int_helper(&arg, err_string) / 10.0f;
3137   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.denoise_block_size,
3138                               argv, err_string)) {
3139     extra_cfg.noise_block_size = arg_parse_uint_helper(&arg, err_string);
3140   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_dnl_denoising,
3141                               argv, err_string)) {
3142     extra_cfg.enable_dnl_denoising = arg_parse_uint_helper(&arg, err_string);
3143   }
3144 #endif
3145   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.target_seq_level_idx,
3146                             argv, err_string)) {
3147     const int val = arg_parse_int_helper(&arg, err_string);
3148     const int level = val % 100;
3149     const int operating_point_idx = val / 100;
3150     if (operating_point_idx >= 0 &&
3151         operating_point_idx < MAX_NUM_OPERATING_POINTS) {
3152       extra_cfg.target_seq_level_idx[operating_point_idx] = (AV1_LEVEL)level;
3153     }
3154   } else if (arg_match_helper(&arg,
3155                               &g_av1_codec_arg_defs.input_chroma_subsampling_x,
3156                               argv, err_string)) {
3157     extra_cfg.chroma_subsampling_x = arg_parse_uint_helper(&arg, err_string);
3158   } else if (arg_match_helper(&arg,
3159                               &g_av1_codec_arg_defs.input_chroma_subsampling_y,
3160                               argv, err_string)) {
3161     extra_cfg.chroma_subsampling_y = arg_parse_uint_helper(&arg, err_string);
3162   } else {
3163     match = 0;
3164     snprintf(err_string, ARG_ERR_MSG_MAX_LEN, "Cannot find aom option %s",
3165              name);
3166   }
3167   aom_free(argv[0]);
3168 
3169   if (strlen(err_string) != 0) {
3170     ctx->base.err_detail = err_string;
3171     return AOM_CODEC_INVALID_PARAM;
3172   }
3173 
3174   ctx->base.err_detail = NULL;
3175 
3176   if (!match) {
3177     return AOM_CODEC_INVALID_PARAM;
3178   }
3179   return update_extra_cfg(ctx, &extra_cfg);
3180 }
3181 
ctrl_get_seq_level_idx(aom_codec_alg_priv_t * ctx,va_list args)3182 static aom_codec_err_t ctrl_get_seq_level_idx(aom_codec_alg_priv_t *ctx,
3183                                               va_list args) {
3184   int *const arg = va_arg(args, int *);
3185   const AV1_COMP *const cpi = ctx->cpi;
3186   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
3187   return av1_get_seq_level_idx(&cpi->common.seq_params, &cpi->level_params,
3188                                arg);
3189 }
3190 
3191 static aom_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
3192   { AV1_COPY_REFERENCE, ctrl_copy_reference },
3193   { AOME_USE_REFERENCE, ctrl_use_reference },
3194 
3195   // Setters
3196   { AV1_SET_REFERENCE, ctrl_set_reference },
3197   { AOME_SET_ROI_MAP, ctrl_set_roi_map },
3198   { AOME_SET_ACTIVEMAP, ctrl_set_active_map },
3199   { AOME_SET_SCALEMODE, ctrl_set_scale_mode },
3200   { AOME_SET_SPATIAL_LAYER_ID, ctrl_set_spatial_layer_id },
3201   { AOME_SET_CPUUSED, ctrl_set_cpuused },
3202   { AOME_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
3203   { AOME_SET_ENABLEAUTOBWDREF, ctrl_set_enable_auto_bwd_ref },
3204   { AOME_SET_SHARPNESS, ctrl_set_sharpness },
3205   { AOME_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
3206   { AV1E_SET_ROW_MT, ctrl_set_row_mt },
3207   { AV1E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
3208   { AV1E_SET_TILE_ROWS, ctrl_set_tile_rows },
3209   { AV1E_SET_ENABLE_TPL_MODEL, ctrl_set_enable_tpl_model },
3210   { AV1E_SET_ENABLE_KEYFRAME_FILTERING, ctrl_set_enable_keyframe_filtering },
3211   { AOME_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
3212   { AOME_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
3213   { AOME_SET_TUNING, ctrl_set_tuning },
3214   { AOME_SET_CQ_LEVEL, ctrl_set_cq_level },
3215   { AOME_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
3216   { AOME_SET_NUMBER_SPATIAL_LAYERS, ctrl_set_number_spatial_layers },
3217   { AV1E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
3218   { AV1E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
3219   { AV1E_SET_LOSSLESS, ctrl_set_lossless },
3220   { AV1E_SET_ENABLE_CDEF, ctrl_set_enable_cdef },
3221   { AV1E_SET_ENABLE_RESTORATION, ctrl_set_enable_restoration },
3222   { AV1E_SET_FORCE_VIDEO_MODE, ctrl_set_force_video_mode },
3223   { AV1E_SET_ENABLE_OBMC, ctrl_set_enable_obmc },
3224   { AV1E_SET_DISABLE_TRELLIS_QUANT, ctrl_set_disable_trellis_quant },
3225   { AV1E_SET_ENABLE_QM, ctrl_set_enable_qm },
3226   { AV1E_SET_QM_Y, ctrl_set_qm_y },
3227   { AV1E_SET_QM_U, ctrl_set_qm_u },
3228   { AV1E_SET_QM_V, ctrl_set_qm_v },
3229   { AV1E_SET_QM_MIN, ctrl_set_qm_min },
3230   { AV1E_SET_QM_MAX, ctrl_set_qm_max },
3231   { AV1E_SET_NUM_TG, ctrl_set_num_tg },
3232   { AV1E_SET_MTU, ctrl_set_mtu },
3233   { AV1E_SET_TIMING_INFO_TYPE, ctrl_set_timing_info_type },
3234   { AV1E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
3235   { AV1E_SET_ERROR_RESILIENT_MODE, ctrl_set_error_resilient_mode },
3236   { AV1E_SET_S_FRAME_MODE, ctrl_set_s_frame_mode },
3237   { AV1E_SET_ENABLE_RECT_PARTITIONS, ctrl_set_enable_rect_partitions },
3238   { AV1E_SET_ENABLE_AB_PARTITIONS, ctrl_set_enable_ab_partitions },
3239   { AV1E_SET_ENABLE_1TO4_PARTITIONS, ctrl_set_enable_1to4_partitions },
3240   { AV1E_SET_MIN_PARTITION_SIZE, ctrl_set_min_partition_size },
3241   { AV1E_SET_MAX_PARTITION_SIZE, ctrl_set_max_partition_size },
3242   { AV1E_SET_ENABLE_DUAL_FILTER, ctrl_set_enable_dual_filter },
3243   { AV1E_SET_ENABLE_CHROMA_DELTAQ, ctrl_set_enable_chroma_deltaq },
3244   { AV1E_SET_ENABLE_INTRA_EDGE_FILTER, ctrl_set_enable_intra_edge_filter },
3245   { AV1E_SET_ENABLE_ORDER_HINT, ctrl_set_enable_order_hint },
3246   { AV1E_SET_ENABLE_TX64, ctrl_set_enable_tx64 },
3247   { AV1E_SET_ENABLE_FLIP_IDTX, ctrl_set_enable_flip_idtx },
3248   { AV1E_SET_ENABLE_RECT_TX, ctrl_set_enable_rect_tx },
3249   { AV1E_SET_ENABLE_DIST_WTD_COMP, ctrl_set_enable_dist_wtd_comp },
3250   { AV1E_SET_MAX_REFERENCE_FRAMES, ctrl_set_max_reference_frames },
3251   { AV1E_SET_REDUCED_REFERENCE_SET, ctrl_set_enable_reduced_reference_set },
3252   { AV1E_SET_ENABLE_REF_FRAME_MVS, ctrl_set_enable_ref_frame_mvs },
3253   { AV1E_SET_ALLOW_REF_FRAME_MVS, ctrl_set_allow_ref_frame_mvs },
3254   { AV1E_SET_ENABLE_MASKED_COMP, ctrl_set_enable_masked_comp },
3255   { AV1E_SET_ENABLE_ONESIDED_COMP, ctrl_set_enable_onesided_comp },
3256   { AV1E_SET_ENABLE_INTERINTRA_COMP, ctrl_set_enable_interintra_comp },
3257   { AV1E_SET_ENABLE_SMOOTH_INTERINTRA, ctrl_set_enable_smooth_interintra },
3258   { AV1E_SET_ENABLE_DIFF_WTD_COMP, ctrl_set_enable_diff_wtd_comp },
3259   { AV1E_SET_ENABLE_INTERINTER_WEDGE, ctrl_set_enable_interinter_wedge },
3260   { AV1E_SET_ENABLE_INTERINTRA_WEDGE, ctrl_set_enable_interintra_wedge },
3261   { AV1E_SET_ENABLE_GLOBAL_MOTION, ctrl_set_enable_global_motion },
3262   { AV1E_SET_ENABLE_WARPED_MOTION, ctrl_set_enable_warped_motion },
3263   { AV1E_SET_ALLOW_WARPED_MOTION, ctrl_set_allow_warped_motion },
3264   { AV1E_SET_ENABLE_FILTER_INTRA, ctrl_set_enable_filter_intra },
3265   { AV1E_SET_ENABLE_SMOOTH_INTRA, ctrl_set_enable_smooth_intra },
3266   { AV1E_SET_ENABLE_PAETH_INTRA, ctrl_set_enable_paeth_intra },
3267   { AV1E_SET_ENABLE_CFL_INTRA, ctrl_set_enable_cfl_intra },
3268   { AV1E_SET_ENABLE_SUPERRES, ctrl_set_enable_superres },
3269   { AV1E_SET_ENABLE_OVERLAY, ctrl_set_enable_overlay },
3270   { AV1E_SET_ENABLE_PALETTE, ctrl_set_enable_palette },
3271   { AV1E_SET_ENABLE_INTRABC, ctrl_set_enable_intrabc },
3272   { AV1E_SET_ENABLE_ANGLE_DELTA, ctrl_set_enable_angle_delta },
3273   { AV1E_SET_AQ_MODE, ctrl_set_aq_mode },
3274   { AV1E_SET_REDUCED_TX_TYPE_SET, ctrl_set_reduced_tx_type_set },
3275   { AV1E_SET_INTRA_DCT_ONLY, ctrl_set_intra_dct_only },
3276   { AV1E_SET_INTER_DCT_ONLY, ctrl_set_inter_dct_only },
3277   { AV1E_SET_INTRA_DEFAULT_TX_ONLY, ctrl_set_intra_default_tx_only },
3278   { AV1E_SET_QUANT_B_ADAPT, ctrl_set_quant_b_adapt },
3279   { AV1E_SET_COEFF_COST_UPD_FREQ, ctrl_set_coeff_cost_upd_freq },
3280   { AV1E_SET_MODE_COST_UPD_FREQ, ctrl_set_mode_cost_upd_freq },
3281   { AV1E_SET_MV_COST_UPD_FREQ, ctrl_set_mv_cost_upd_freq },
3282   { AV1E_SET_DELTAQ_MODE, ctrl_set_deltaq_mode },
3283   { AV1E_SET_DELTALF_MODE, ctrl_set_deltalf_mode },
3284   { AV1E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
3285   { AV1E_SET_TUNE_CONTENT, ctrl_set_tune_content },
3286   { AV1E_SET_CDF_UPDATE_MODE, ctrl_set_cdf_update_mode },
3287   { AV1E_SET_COLOR_PRIMARIES, ctrl_set_color_primaries },
3288   { AV1E_SET_TRANSFER_CHARACTERISTICS, ctrl_set_transfer_characteristics },
3289   { AV1E_SET_MATRIX_COEFFICIENTS, ctrl_set_matrix_coefficients },
3290   { AV1E_SET_CHROMA_SAMPLE_POSITION, ctrl_set_chroma_sample_position },
3291   { AV1E_SET_COLOR_RANGE, ctrl_set_color_range },
3292   { AV1E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
3293   { AV1E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
3294   { AV1E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
3295   { AV1E_SET_GF_MIN_PYRAMID_HEIGHT, ctrl_set_gf_min_pyr_height },
3296   { AV1E_SET_GF_MAX_PYRAMID_HEIGHT, ctrl_set_gf_max_pyr_height },
3297   { AV1E_SET_RENDER_SIZE, ctrl_set_render_size },
3298   { AV1E_SET_SUPERBLOCK_SIZE, ctrl_set_superblock_size },
3299   { AV1E_SET_SINGLE_TILE_DECODING, ctrl_set_single_tile_decoding },
3300   { AV1E_SET_VMAF_MODEL_PATH, ctrl_set_vmaf_model_path },
3301   { AV1E_SET_FILM_GRAIN_TEST_VECTOR, ctrl_set_film_grain_test_vector },
3302   { AV1E_SET_FILM_GRAIN_TABLE, ctrl_set_film_grain_table },
3303   { AV1E_SET_DENOISE_NOISE_LEVEL, ctrl_set_denoise_noise_level },
3304   { AV1E_SET_DENOISE_BLOCK_SIZE, ctrl_set_denoise_block_size },
3305   { AV1E_SET_ENABLE_DNL_DENOISING, ctrl_set_enable_dnl_denoising },
3306   { AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
3307   { AV1E_ENABLE_EXT_TILE_DEBUG, ctrl_enable_ext_tile_debug },
3308   { AV1E_SET_TARGET_SEQ_LEVEL_IDX, ctrl_set_target_seq_level_idx },
3309   { AV1E_SET_TIER_MASK, ctrl_set_tier_mask },
3310   { AV1E_SET_MIN_CR, ctrl_set_min_cr },
3311   { AV1E_SET_SVC_LAYER_ID, ctrl_set_layer_id },
3312   { AV1E_SET_SVC_PARAMS, ctrl_set_svc_params },
3313   { AV1E_SET_SVC_REF_FRAME_CONFIG, ctrl_set_svc_ref_frame_config },
3314   { AV1E_SET_VBR_CORPUS_COMPLEXITY_LAP, ctrl_set_vbr_corpus_complexity_lap },
3315   { AV1E_ENABLE_SB_MULTIPASS_UNIT_TEST, ctrl_enable_sb_multipass_unit_test },
3316 
3317   // Getters
3318   { AOME_GET_LAST_QUANTIZER, ctrl_get_quantizer },
3319   { AOME_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
3320   { AV1_GET_REFERENCE, ctrl_get_reference },
3321   { AV1E_GET_ACTIVEMAP, ctrl_get_active_map },
3322   { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
3323   { AV1_COPY_NEW_FRAME_IMAGE, ctrl_copy_new_frame_image },
3324   { AV1E_SET_CHROMA_SUBSAMPLING_X, ctrl_set_chroma_subsampling_x },
3325   { AV1E_SET_CHROMA_SUBSAMPLING_Y, ctrl_set_chroma_subsampling_y },
3326   { AV1E_GET_SEQ_LEVEL_IDX, ctrl_get_seq_level_idx },
3327   { AV1E_GET_BASELINE_GF_INTERVAL, ctrl_get_baseline_gf_interval },
3328 
3329   CTRL_MAP_END,
3330 };
3331 
3332 static const aom_codec_enc_cfg_t encoder_usage_cfg[] = {
3333   {
3334       // NOLINT
3335       AOM_USAGE_GOOD_QUALITY,  // g_usage - non-realtime usage
3336       0,                       // g_threads
3337       0,                       // g_profile
3338 
3339       320,         // g_w
3340       240,         // g_h
3341       0,           // g_limit
3342       0,           // g_forced_max_frame_width
3343       0,           // g_forced_max_frame_height
3344       AOM_BITS_8,  // g_bit_depth
3345       8,           // g_input_bit_depth
3346 
3347       { 1, 30 },  // g_timebase
3348 
3349       0,  // g_error_resilient
3350 
3351       AOM_RC_ONE_PASS,  // g_pass
3352 
3353       19,  // g_lag_in_frames
3354 
3355       0,                // rc_dropframe_thresh
3356       RESIZE_NONE,      // rc_resize_mode
3357       SCALE_NUMERATOR,  // rc_resize_denominator
3358       SCALE_NUMERATOR,  // rc_resize_kf_denominator
3359 
3360       AOM_SUPERRES_NONE,  // rc_superres_mode
3361       SCALE_NUMERATOR,    // rc_superres_denominator
3362       SCALE_NUMERATOR,    // rc_superres_kf_denominator
3363       63,                 // rc_superres_qthresh
3364       32,                 // rc_superres_kf_qthresh
3365 
3366       AOM_VBR,      // rc_end_usage
3367       { NULL, 0 },  // rc_twopass_stats_in
3368       { NULL, 0 },  // rc_firstpass_mb_stats_in
3369       256,          // rc_target_bandwidth
3370       0,            // rc_min_quantizer
3371       63,           // rc_max_quantizer
3372       25,           // rc_undershoot_pct
3373       25,           // rc_overshoot_pct
3374 
3375       6000,  // rc_max_buffer_size
3376       4000,  // rc_buffer_initial_size
3377       5000,  // rc_buffer_optimal_size
3378 
3379       50,    // rc_two_pass_vbrbias
3380       0,     // rc_two_pass_vbrmin_section
3381       2000,  // rc_two_pass_vbrmax_section
3382 
3383       // keyframing settings (kf)
3384       0,                       // fwd_kf_enabled
3385       AOM_KF_AUTO,             // kf_mode
3386       0,                       // kf_min_dist
3387       9999,                    // kf_max_dist
3388       0,                       // sframe_dist
3389       1,                       // sframe_mode
3390       0,                       // large_scale_tile
3391       0,                       // monochrome
3392       0,                       // full_still_picture_hdr
3393       0,                       // save_as_annexb
3394       0,                       // tile_width_count
3395       0,                       // tile_height_count
3396       { 0 },                   // tile_widths
3397       { 0 },                   // tile_heights
3398       0,                       // use_fixed_qp_offsets
3399       { -1, -1, -1, -1, -1 },  // fixed_qp_offsets
3400       { 0, 128, 128, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3401         0, 0,   0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  // cfg
3402   },
3403   {
3404       // NOLINT
3405       AOM_USAGE_REALTIME,  // g_usage - real-time usage
3406       0,                   // g_threads
3407       0,                   // g_profile
3408 
3409       320,         // g_w
3410       240,         // g_h
3411       0,           // g_limit
3412       0,           // g_forced_max_frame_width
3413       0,           // g_forced_max_frame_height
3414       AOM_BITS_8,  // g_bit_depth
3415       8,           // g_input_bit_depth
3416 
3417       { 1, 30 },  // g_timebase
3418 
3419       0,  // g_error_resilient
3420 
3421       AOM_RC_ONE_PASS,  // g_pass
3422 
3423       1,  // g_lag_in_frames
3424 
3425       0,                // rc_dropframe_thresh
3426       RESIZE_NONE,      // rc_resize_mode
3427       SCALE_NUMERATOR,  // rc_resize_denominator
3428       SCALE_NUMERATOR,  // rc_resize_kf_denominator
3429 
3430       AOM_SUPERRES_NONE,  // rc_superres_mode
3431       SCALE_NUMERATOR,    // rc_superres_denominator
3432       SCALE_NUMERATOR,    // rc_superres_kf_denominator
3433       63,                 // rc_superres_qthresh
3434       32,                 // rc_superres_kf_qthresh
3435 
3436       AOM_CBR,      // rc_end_usage
3437       { NULL, 0 },  // rc_twopass_stats_in
3438       { NULL, 0 },  // rc_firstpass_mb_stats_in
3439       256,          // rc_target_bandwidth
3440       0,            // rc_min_quantizer
3441       63,           // rc_max_quantizer
3442       25,           // rc_undershoot_pct
3443       25,           // rc_overshoot_pct
3444 
3445       6000,  // rc_max_buffer_size
3446       4000,  // rc_buffer_initial_size
3447       5000,  // rc_buffer_optimal_size
3448 
3449       50,    // rc_two_pass_vbrbias
3450       0,     // rc_two_pass_vbrmin_section
3451       2000,  // rc_two_pass_vbrmax_section
3452 
3453       // keyframing settings (kf)
3454       0,                       // fwd_kf_enabled
3455       AOM_KF_AUTO,             // kf_mode
3456       0,                       // kf_min_dist
3457       9999,                    // kf_max_dist
3458       0,                       // sframe_dist
3459       1,                       // sframe_mode
3460       0,                       // large_scale_tile
3461       0,                       // monochrome
3462       0,                       // full_still_picture_hdr
3463       0,                       // save_as_annexb
3464       0,                       // tile_width_count
3465       0,                       // tile_height_count
3466       { 0 },                   // tile_widths
3467       { 0 },                   // tile_heights
3468       0,                       // use_fixed_qp_offsets
3469       { -1, -1, -1, -1, -1 },  // fixed_qp_offsets
3470       { 0, 128, 128, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3471         0, 0,   0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  // cfg
3472   },
3473 };
3474 
3475 // This data structure and function are exported in aom/aomcx.h
3476 #ifndef VERSION_STRING
3477 #define VERSION_STRING
3478 #endif
3479 aom_codec_iface_t aom_codec_av1_cx_algo = {
3480   "AOMedia Project AV1 Encoder" VERSION_STRING,
3481   AOM_CODEC_INTERNAL_ABI_VERSION,
3482   AOM_CODEC_CAP_HIGHBITDEPTH | AOM_CODEC_CAP_ENCODER |
3483       AOM_CODEC_CAP_PSNR,  // aom_codec_caps_t
3484   encoder_init,            // aom_codec_init_fn_t
3485   encoder_destroy,         // aom_codec_destroy_fn_t
3486   encoder_ctrl_maps,       // aom_codec_ctrl_fn_map_t
3487   {
3488       // NOLINT
3489       NULL,  // aom_codec_peek_si_fn_t
3490       NULL,  // aom_codec_get_si_fn_t
3491       NULL,  // aom_codec_decode_fn_t
3492       NULL,  // aom_codec_get_frame_fn_t
3493       NULL   // aom_codec_set_fb_fn_t
3494   },
3495   {
3496       // NOLINT
3497       2,                           // 2 cfg
3498       encoder_usage_cfg,           // aom_codec_enc_cfg_t
3499       encoder_encode,              // aom_codec_encode_fn_t
3500       encoder_get_cxdata,          // aom_codec_get_cx_data_fn_t
3501       encoder_set_config,          // aom_codec_enc_config_set_fn_t
3502       encoder_get_global_headers,  // aom_codec_get_global_headers_fn_t
3503       encoder_get_preview          // aom_codec_get_preview_frame_fn_t
3504   },
3505   encoder_set_option  // aom_codec_set_option_fn_t
3506 };
3507 
aom_codec_av1_cx(void)3508 aom_codec_iface_t *aom_codec_av1_cx(void) { return &aom_codec_av1_cx_algo; }
3509