xref: /aosp_15_r20/external/libaom/av1/av1_cx_iface.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 <limits.h>
12 #include <math.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "config/aom_config.h"
19 #include "config/aom_version.h"
20 
21 #include "aom/aomcx.h"
22 #include "aom/aom_encoder.h"
23 #include "aom/aom_external_partition.h"
24 #include "aom/aom_image.h"
25 #include "aom/internal/aom_codec_internal.h"
26 #include "aom_dsp/flow_estimation/flow_estimation.h"
27 #include "aom_mem/aom_mem.h"
28 #include "aom_scale/yv12config.h"
29 #include "aom_util/aom_pthread.h"
30 
31 #include "av1/av1_cx_iface.h"
32 #include "av1/av1_iface_common.h"
33 #include "av1/common/av1_common_int.h"
34 #include "av1/common/enums.h"
35 #include "av1/common/scale.h"
36 #include "av1/encoder/bitstream.h"
37 #include "av1/encoder/enc_enums.h"
38 #include "av1/encoder/encoder.h"
39 #include "av1/encoder/encoder_alloc.h"
40 #include "av1/encoder/encoder_utils.h"
41 #include "av1/encoder/ethread.h"
42 #include "av1/encoder/external_partition.h"
43 #include "av1/encoder/firstpass.h"
44 #include "av1/encoder/lookahead.h"
45 #include "av1/encoder/rc_utils.h"
46 #include "av1/arg_defs.h"
47 
48 #include "common/args_helper.h"
49 
50 struct av1_extracfg {
51   unsigned int usage;  // Same as g_usage in aom_codec_enc_cfg_t
52   int cpu_used;
53   unsigned int enable_auto_alt_ref;
54   unsigned int enable_auto_bwd_ref;
55   unsigned int noise_sensitivity;
56   unsigned int sharpness;
57   unsigned int static_thresh;
58   unsigned int row_mt;
59   unsigned int fp_mt;
60   unsigned int tile_columns;  // log2 number of tile columns
61   unsigned int tile_rows;     // log2 number of tile rows
62   unsigned int auto_tiles;
63   unsigned int enable_tpl_model;
64   unsigned int enable_keyframe_filtering;
65   unsigned int arnr_max_frames;
66   unsigned int arnr_strength;
67   unsigned int min_gf_interval;
68   unsigned int max_gf_interval;
69   unsigned int gf_min_pyr_height;
70   unsigned int gf_max_pyr_height;
71   aom_tune_metric tuning;
72   const char *vmaf_model_path;
73   const char *partition_info_path;
74   unsigned int enable_rate_guide_deltaq;
75   const char *rate_distribution_info;
76   aom_dist_metric dist_metric;
77   unsigned int cq_level;  // constrained quality level
78   unsigned int rc_max_intra_bitrate_pct;
79   unsigned int rc_max_inter_bitrate_pct;
80   unsigned int gf_cbr_boost_pct;
81   unsigned int lossless;
82   unsigned int enable_cdef;
83   unsigned int enable_restoration;
84   unsigned int force_video_mode;
85   unsigned int enable_obmc;
86   unsigned int disable_trellis_quant;
87   unsigned int enable_qm;
88   unsigned int qm_y;
89   unsigned int qm_u;
90   unsigned int qm_v;
91   unsigned int qm_min;
92   unsigned int qm_max;
93   unsigned int num_tg;
94   unsigned int mtu_size;
95 
96   aom_timing_info_type_t timing_info_type;
97   unsigned int frame_parallel_decoding_mode;
98   int enable_dual_filter;
99   unsigned int enable_chroma_deltaq;
100   AQ_MODE aq_mode;
101   DELTAQ_MODE deltaq_mode;
102   int deltaq_strength;
103   int deltalf_mode;
104   unsigned int frame_periodic_boost;
105   aom_tune_content content;
106   aom_color_primaries_t color_primaries;
107   aom_transfer_characteristics_t transfer_characteristics;
108   aom_matrix_coefficients_t matrix_coefficients;
109   aom_chroma_sample_position_t chroma_sample_position;
110   int color_range;
111   int render_width;
112   int render_height;
113   aom_superblock_size_t superblock_size;
114   unsigned int single_tile_decoding;
115   int error_resilient_mode;
116   int s_frame_mode;
117 
118   int film_grain_test_vector;
119   const char *film_grain_table_filename;
120   unsigned int motion_vector_unit_test;
121 #if CONFIG_FPMT_TEST
122   unsigned int fpmt_unit_test;
123 #endif
124   unsigned int cdf_update_mode;
125   int enable_rect_partitions;    // enable rectangular partitions for sequence
126   int enable_ab_partitions;      // enable AB partitions for sequence
127   int enable_1to4_partitions;    // enable 1:4 and 4:1 partitions for sequence
128   int min_partition_size;        // min partition size [4,8,16,32,64,128]
129   int max_partition_size;        // max partition size [4,8,16,32,64,128]
130   int enable_intra_edge_filter;  // enable intra-edge filter for sequence
131   int enable_order_hint;         // enable order hint for sequence
132   int enable_tx64;               // enable 64-pt transform usage for sequence
133   int enable_flip_idtx;          // enable flip and identity transform types
134   int enable_rect_tx;        // enable rectangular transform usage for sequence
135   int enable_dist_wtd_comp;  // enable dist wtd compound for sequence
136   int max_reference_frames;  // maximum number of references per frame
137   int enable_reduced_reference_set;  // enable reduced set of references
138   int enable_ref_frame_mvs;          // sequence level
139   int allow_ref_frame_mvs;           // frame level
140   int enable_masked_comp;            // enable masked compound for sequence
141   int enable_onesided_comp;          // enable one sided compound for sequence
142   int enable_interintra_comp;        // enable interintra compound for sequence
143   int enable_smooth_interintra;      // enable smooth interintra mode usage
144   int enable_diff_wtd_comp;          // enable diff-wtd compound usage
145   int enable_interinter_wedge;       // enable interinter-wedge compound usage
146   int enable_interintra_wedge;       // enable interintra-wedge compound usage
147   int enable_global_motion;          // enable global motion usage for sequence
148   int enable_warped_motion;          // sequence level
149   int allow_warped_motion;           // frame level
150   int enable_filter_intra;           // enable filter intra for sequence
151   int enable_smooth_intra;           // enable smooth intra modes for sequence
152   int enable_paeth_intra;            // enable Paeth intra mode for sequence
153   int enable_cfl_intra;              // enable CFL uv intra mode for sequence
154   int enable_directional_intra;      // enable directional modes for sequence
155   int enable_diagonal_intra;  // enable D45 to D203 intra modes for sequence
156   int enable_superres;
157   int enable_overlay;  // enable overlay for filtered arf frames
158   int enable_palette;
159   int enable_intrabc;
160   int enable_angle_delta;
161 #if CONFIG_DENOISE
162   float noise_level;
163   int noise_block_size;
164   int enable_dnl_denoising;
165 #endif
166 
167   unsigned int chroma_subsampling_x;
168   unsigned int chroma_subsampling_y;
169   int reduced_tx_type_set;
170   int use_intra_dct_only;
171   int use_inter_dct_only;
172   int use_intra_default_tx_only;
173   int enable_tx_size_search;
174   int quant_b_adapt;
175   unsigned int vbr_corpus_complexity_lap;
176   AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
177   // Bit mask to specify which tier each of the 32 possible operating points
178   // conforms to.
179   unsigned int tier_mask;
180   // min_cr / 100 is the target minimum compression ratio for each frame.
181   unsigned int min_cr;
182   COST_UPDATE_TYPE coeff_cost_upd_freq;
183   COST_UPDATE_TYPE mode_cost_upd_freq;
184   COST_UPDATE_TYPE mv_cost_upd_freq;
185   COST_UPDATE_TYPE dv_cost_upd_freq;
186   unsigned int ext_tile_debug;
187   unsigned int sb_multipass_unit_test;
188   // Total number of passes. If this number is -1, then we assume passes = 1 or
189   // 2 (passes = 1 if pass == AOM_RC_ONE_PASS and passes = 2 otherwise).
190   int passes;
191   int fwd_kf_dist;
192 
193   LOOPFILTER_CONTROL loopfilter_control;
194   // Indicates if the application of post-processing filters should be skipped
195   // on reconstructed frame.
196   unsigned int skip_postproc_filtering;
197   // the name of the second pass output file when passes > 2
198   const char *two_pass_output;
199   const char *second_pass_log;
200   // Automatically determine whether to disable several intra tools
201   // when "--deltaq-mode=3" is true.
202   // Default as 0.
203   // When set to 1, the encoder will analyze the reconstruction quality
204   // as compared to the source image in the preprocessing pass.
205   // If the recontruction quality is considered high enough, we disable
206   // the following intra coding tools, for better encoding speed:
207   // "--enable_smooth_intra",
208   // "--enable_paeth_intra",
209   // "--enable_cfl_intra",
210   // "--enable_diagonal_intra".
211   int auto_intra_tools_off;
212   int strict_level_conformance;
213   int kf_max_pyr_height;
214   int sb_qp_sweep;
215 };
216 
217 static const struct av1_extracfg default_extra_cfg[] = {
218 #if !CONFIG_REALTIME_ONLY
219   {
220       AOM_USAGE_GOOD_QUALITY,  // usage
221       0,                       // cpu_used
222       1,                       // enable_auto_alt_ref
223       0,                       // enable_auto_bwd_ref
224       0,                       // noise_sensitivity
225       0,                       // sharpness
226       0,                       // static_thresh
227       1,                       // row_mt
228       0,                       // fp_mt
229       0,                       // tile_columns
230       0,                       // tile_rows
231       0,                       // auto_tiles
232       1,                       // enable_tpl_model
233       1,                       // enable_keyframe_filtering
234       7,                       // arnr_max_frames
235       5,                       // arnr_strength
236       0,                       // min_gf_interval; 0 -> default decision
237       0,                       // max_gf_interval; 0 -> default decision
238       0,                       // gf_min_pyr_height
239       5,                       // gf_max_pyr_height
240       AOM_TUNE_PSNR,           // tuning
241       "/usr/local/share/model/vmaf_v0.6.1.json",  // VMAF model path
242       ".",                                        // partition info path
243       0,                                          // enable rate guide deltaq
244       "./rate_map.txt",                           // rate distribution input
245       AOM_DIST_METRIC_PSNR,                       // dist_metric
246       10,                                         // cq_level
247       0,                                          // rc_max_intra_bitrate_pct
248       0,                                          // rc_max_inter_bitrate_pct
249       0,                                          // gf_cbr_boost_pct
250       0,                                          // lossless
251       1,                                          // enable_cdef
252       1,                                          // enable_restoration
253       0,                                          // force_video_mode
254       1,                                          // enable_obmc
255       3,                                          // disable_trellis_quant
256       0,                                          // enable_qm
257       DEFAULT_QM_Y,                               // qm_y
258       DEFAULT_QM_U,                               // qm_u
259       DEFAULT_QM_V,                               // qm_v
260       DEFAULT_QM_FIRST,                           // qm_min
261       DEFAULT_QM_LAST,                            // qm_max
262       1,                                          // max number of tile groups
263       0,                                          // mtu_size
264       AOM_TIMING_UNSPECIFIED,       // No picture timing signaling in bitstream
265       0,                            // frame_parallel_decoding_mode
266       1,                            // enable dual filter
267       0,                            // enable delta quant in chroma planes
268       NO_AQ,                        // aq_mode
269       DELTA_Q_OBJECTIVE,            // deltaq_mode
270       100,                          // deltaq_strength
271       0,                            // delta lf mode
272       0,                            // frame_periodic_boost
273       AOM_CONTENT_DEFAULT,          // content
274       AOM_CICP_CP_UNSPECIFIED,      // CICP color primaries
275       AOM_CICP_TC_UNSPECIFIED,      // CICP transfer characteristics
276       AOM_CICP_MC_UNSPECIFIED,      // CICP matrix coefficients
277       AOM_CSP_UNKNOWN,              // chroma sample position
278       0,                            // color range
279       0,                            // render width
280       0,                            // render height
281       AOM_SUPERBLOCK_SIZE_DYNAMIC,  // superblock_size
282       1,                            // this depends on large_scale_tile.
283       0,                            // error_resilient_mode off by default.
284       0,                            // s_frame_mode off by default.
285       0,                            // film_grain_test_vector
286       NULL,                         // film_grain_table_filename
287       0,                            // motion_vector_unit_test
288 #if CONFIG_FPMT_TEST
289       0,  // fpmt_unit_test
290 #endif
291       1,    // CDF update mode
292       1,    // enable rectangular partitions
293       1,    // enable ab shape partitions
294       1,    // enable 1:4 and 4:1 partitions
295       4,    // min_partition_size
296       128,  // max_partition_size
297       1,    // enable intra edge filter
298       1,    // frame order hint
299       1,    // enable 64-pt transform usage
300       1,    // enable flip and identity transform
301       1,    // enable rectangular transform usage
302       1,    // dist-wtd compound
303       7,    // max_reference_frames
304       0,    // enable_reduced_reference_set
305       1,    // enable_ref_frame_mvs sequence level
306       1,    // allow ref_frame_mvs frame level
307       1,    // enable masked compound at sequence level
308       1,    // enable one sided compound at sequence level
309       1,    // enable interintra compound at sequence level
310       1,    // enable smooth interintra mode
311       1,    // enable difference-weighted compound
312       1,    // enable interinter wedge compound
313       1,    // enable interintra wedge compound
314       1,    // enable_global_motion usage
315       1,    // enable_warped_motion at sequence level
316       1,    // allow_warped_motion at frame level
317       1,    // enable filter intra at sequence level
318       1,    // enable smooth intra modes usage for sequence
319       1,    // enable Paeth intra mode usage for sequence
320       1,    // enable CFL uv intra mode usage for sequence
321       1,    // enable directional intra mode usage for sequence
322       1,    // enable D45 to D203 intra mode usage for sequence
323       1,    // superres
324       1,    // enable overlay
325       1,    // enable palette
326       1,    // enable intrabc
327       1,    // enable angle delta
328 #if CONFIG_DENOISE
329       0,   // noise_level
330       32,  // noise_block_size
331       1,   // enable_dnl_denoising
332 #endif
333       0,  // chroma_subsampling_x
334       0,  // chroma_subsampling_y
335       0,  // reduced_tx_type_set
336       0,  // use_intra_dct_only
337       0,  // use_inter_dct_only
338       0,  // use_intra_default_tx_only
339       1,  // enable_tx_size_search
340       0,  // quant_b_adapt
341       0,  // vbr_corpus_complexity_lap
342       {
343           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
344           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
345           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
346           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
347           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
348           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
349           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
350           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
351       },               // target_seq_level_idx
352       0,               // tier_mask
353       0,               // min_cr
354       COST_UPD_SB,     // coeff_cost_upd_freq
355       COST_UPD_SB,     // mode_cost_upd_freq
356       COST_UPD_SB,     // mv_cost_upd_freq
357       COST_UPD_SB,     // dv_cost_upd_freq
358       0,               // ext_tile_debug
359       0,               // sb_multipass_unit_test
360       -1,              // passes
361       -1,              // fwd_kf_dist
362       LOOPFILTER_ALL,  // loopfilter_control
363       0,               // skip_postproc_filtering
364       NULL,            // two_pass_output
365       NULL,            // second_pass_log
366       0,               // auto_intra_tools_off
367       0,               // strict_level_conformance
368       -1,              // kf_max_pyr_height
369       0,               // sb_qp_sweep
370   },
371 #endif  // !CONFIG_REALTIME_ONLY
372   {
373       AOM_USAGE_REALTIME,  // usage
374       10,                  // cpu_used
375       1,                   // enable_auto_alt_ref
376       0,                   // enable_auto_bwd_ref
377       0,                   // noise_sensitivity
378       0,                   // sharpness
379       0,                   // static_thresh
380       1,                   // row_mt
381       0,                   // fp_mt
382       0,                   // tile_columns
383       0,                   // tile_rows
384       0,                   // auto_tiles
385       0,                   // enable_tpl_model
386       0,                   // enable_keyframe_filtering
387       7,                   // arnr_max_frames
388       5,                   // arnr_strength
389       0,                   // min_gf_interval; 0 -> default decision
390       0,                   // max_gf_interval; 0 -> default decision
391       0,                   // gf_min_pyr_height
392       5,                   // gf_max_pyr_height
393       AOM_TUNE_PSNR,       // tuning
394       "/usr/local/share/model/vmaf_v0.6.1.json",  // VMAF model path
395       ".",                                        // partition info path
396       0,                                          // enable rate guide deltaq
397       "./rate_map.txt",                           // rate distribution input
398       AOM_DIST_METRIC_PSNR,                       // dist_metric
399       10,                                         // cq_level
400       300,                                        // rc_max_intra_bitrate_pct
401       0,                                          // rc_max_inter_bitrate_pct
402       0,                                          // gf_cbr_boost_pct
403       0,                                          // lossless
404       1,                                          // enable_cdef
405       0,                                          // enable_restoration
406       0,                                          // force_video_mode
407       0,                                          // enable_obmc
408       3,                                          // disable_trellis_quant
409       0,                                          // enable_qm
410       DEFAULT_QM_Y,                               // qm_y
411       DEFAULT_QM_U,                               // qm_u
412       DEFAULT_QM_V,                               // qm_v
413       DEFAULT_QM_FIRST,                           // qm_min
414       DEFAULT_QM_LAST,                            // qm_max
415       1,                                          // max number of tile groups
416       0,                                          // mtu_size
417       AOM_TIMING_UNSPECIFIED,       // No picture timing signaling in bitstream
418       0,                            // frame_parallel_decoding_mode
419       0,                            // enable dual filter
420       0,                            // enable delta quant in chroma planes
421       CYCLIC_REFRESH_AQ,            // aq_mode
422       NO_DELTA_Q,                   // deltaq_mode
423       100,                          // deltaq_strength
424       0,                            // delta lf mode
425       0,                            // frame_periodic_boost
426       AOM_CONTENT_DEFAULT,          // content
427       AOM_CICP_CP_UNSPECIFIED,      // CICP color primaries
428       AOM_CICP_TC_UNSPECIFIED,      // CICP transfer characteristics
429       AOM_CICP_MC_UNSPECIFIED,      // CICP matrix coefficients
430       AOM_CSP_UNKNOWN,              // chroma sample position
431       0,                            // color range
432       0,                            // render width
433       0,                            // render height
434       AOM_SUPERBLOCK_SIZE_DYNAMIC,  // superblock_size
435       1,                            // this depends on large_scale_tile.
436       0,                            // error_resilient_mode off by default.
437       0,                            // s_frame_mode off by default.
438       0,                            // film_grain_test_vector
439       NULL,                         // film_grain_table_filename
440       0,                            // motion_vector_unit_test
441 #if CONFIG_FPMT_TEST
442       0,  // fpmt_unit_test
443 #endif
444       1,    // CDF update mode
445       0,    // enable rectangular partitions
446       0,    // enable ab shape partitions
447       0,    // enable 1:4 and 4:1 partitions
448       4,    // min_partition_size
449       128,  // max_partition_size
450       0,    // enable intra edge filter
451       0,    // frame order hint
452       0,    // enable 64-pt transform usage
453       1,    // enable flip and identity transform
454       1,    // enable rectangular transform usage
455       0,    // dist-wtd compound
456       3,    // max_reference_frames
457       0,    // enable_reduced_reference_set
458       0,    // enable_ref_frame_mvs sequence level
459       0,    // allow ref_frame_mvs frame level
460       0,    // enable masked compound at sequence level
461       0,    // enable one sided compound at sequence level
462       0,    // enable interintra compound at sequence level
463       0,    // enable smooth interintra mode
464       0,    // enable difference-weighted compound
465       0,    // enable interinter wedge compound
466       0,    // enable interintra wedge compound
467       0,    // enable_global_motion usage
468       0,    // enable_warped_motion at sequence level
469       0,    // allow_warped_motion at frame level
470       0,    // enable filter intra at sequence level
471       0,    // enable smooth intra modes usage for sequence
472       0,    // enable Paeth intra mode usage for sequence
473       0,    // enable CFL uv intra mode usage for sequence
474       1,    // enable directional intra mode usage for sequence
475       1,    // enable D45 to D203 intra mode usage for sequence
476       0,    // superres
477       0,    // enable overlay
478       1,    // enable palette
479       0,    // enable intrabc
480       0,    // enable angle delta
481 #if CONFIG_DENOISE
482       0,   // noise_level
483       32,  // noise_block_size
484       1,   // enable_dnl_denoising
485 #endif
486       0,  // chroma_subsampling_x
487       0,  // chroma_subsampling_y
488       0,  // reduced_tx_type_set
489       0,  // use_intra_dct_only
490       0,  // use_inter_dct_only
491       1,  // use_intra_default_tx_only
492       1,  // enable_tx_size_search
493       0,  // quant_b_adapt
494       0,  // vbr_corpus_complexity_lap
495       {
496           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
497           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
498           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
499           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
500           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
501           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
502           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
503           SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX, SEQ_LEVEL_MAX,
504       },               // target_seq_level_idx
505       0,               // tier_mask
506       0,               // min_cr
507       COST_UPD_OFF,    // coeff_cost_upd_freq
508       COST_UPD_OFF,    // mode_cost_upd_freq
509       COST_UPD_OFF,    // mv_cost_upd_freq
510       COST_UPD_OFF,    // dv_cost_upd_freq
511       0,               // ext_tile_debug
512       0,               // sb_multipass_unit_test
513       -1,              // passes
514       -1,              // fwd_kf_dist
515       LOOPFILTER_ALL,  // loopfilter_control
516       0,               // skip_postproc_filtering
517       NULL,            // two_pass_output
518       NULL,            // second_pass_log
519       0,               // auto_intra_tools_off
520       0,               // strict_level_conformance
521       -1,              // kf_max_pyr_height
522       0,               // sb_qp_sweep
523   },
524 };
525 
526 struct aom_codec_alg_priv {
527   aom_codec_priv_t base;
528   aom_codec_enc_cfg_t cfg;
529   struct av1_extracfg extra_cfg;
530   aom_rational64_t timestamp_ratio;
531   aom_codec_pts_t pts_offset;
532   unsigned char pts_offset_initialized;
533   AV1EncoderConfig oxcf;
534   AV1_PRIMARY *ppi;
535   unsigned char *cx_data;
536   size_t cx_data_sz;
537   size_t pending_cx_data_sz;
538   aom_image_t preview_img;
539   aom_enc_frame_flags_t next_frame_flags;
540   aom_codec_pkt_list_decl(256) pkt_list;
541   unsigned int fixed_kf_cntr;
542   // BufferPool that holds all reference frames.
543   BufferPool *buffer_pool;
544 
545   // lookahead instance variables
546   BufferPool *buffer_pool_lap;
547   FIRSTPASS_STATS *frame_stats_buffer;
548   // Number of stats buffers required for look ahead
549   int num_lap_buffers;
550   STATS_BUFFER_CTX stats_buf_context;
551   bool monochrome_on_init;
552 };
553 
gcd(int64_t a,int b)554 static inline int gcd(int64_t a, int b) {
555   int remainder;
556   while (b > 0) {
557     remainder = (int)(a % b);
558     a = b;
559     b = remainder;
560   }
561 
562   return (int)a;
563 }
564 
reduce_ratio(aom_rational64_t * ratio)565 static void reduce_ratio(aom_rational64_t *ratio) {
566   const int denom = gcd(ratio->num, ratio->den);
567   ratio->num /= denom;
568   ratio->den /= denom;
569 }
570 
571 // Called by encoder_encode() only. Must not be called by encoder_init()
572 // because the `error` paramerer will be destroyed by aom_codec_enc_init_ver()
573 // after encoder_init() returns an error. See the "IMPORTANT" comment in
574 // aom_codec_enc_init_ver().
update_error_state(aom_codec_alg_priv_t * ctx,const struct aom_internal_error_info * error)575 static aom_codec_err_t update_error_state(
576     aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
577   const aom_codec_err_t res = error->error_code;
578 
579   if (res != AOM_CODEC_OK)
580     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
581 
582   return res;
583 }
584 
585 // This function deep copies a string src to *dst. For default string we will
586 // use a string literal, and otherwise we will allocate memory for the string.
allocate_and_set_string(const char * src,const char * default_src,const char ** dst,char * err_detail)587 static aom_codec_err_t allocate_and_set_string(const char *src,
588                                                const char *default_src,
589                                                const char **dst,
590                                                char *err_detail) {
591   if (!src) {
592     snprintf(err_detail, ARG_ERR_MSG_MAX_LEN,
593              "Null pointer given to a string parameter.");
594     return AOM_CODEC_INVALID_PARAM;
595   }
596   if (*dst && strcmp(src, *dst) == 0) return AOM_CODEC_OK;
597   // If the input is exactly the same as default, we will use the string
598   // literal, so do not free here.
599   if (*dst != default_src) {
600     aom_free((void *)*dst);
601   }
602 
603   if (default_src && strcmp(src, default_src) == 0) {
604     // default_src should be a string literal
605     *dst = default_src;
606   } else {
607     size_t len = strlen(src) + 1;
608     char *tmp = aom_malloc(len * sizeof(*tmp));
609     if (!tmp) {
610       snprintf(err_detail, ARG_ERR_MSG_MAX_LEN,
611                "Failed to allocate memory for copying parameters.");
612       return AOM_CODEC_MEM_ERROR;
613     }
614     memcpy(tmp, src, len);
615     *dst = tmp;
616   }
617   return 0;
618 }
619 
620 #undef ERROR
621 #define ERROR(str)                  \
622   do {                              \
623     ctx->base.err_detail = str;     \
624     return AOM_CODEC_INVALID_PARAM; \
625   } while (0)
626 
627 #define RANGE_CHECK(p, memb, lo, hi)                   \
628   do {                                                 \
629     if (!((p)->memb >= (lo) && (p)->memb <= (hi)))     \
630       ERROR(#memb " out of range [" #lo ".." #hi "]"); \
631   } while (0)
632 
633 #define RANGE_CHECK_HI(p, memb, hi)                                     \
634   do {                                                                  \
635     if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
636   } while (0)
637 
638 #define RANGE_CHECK_BOOL(p, memb)                                     \
639   do {                                                                \
640     if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
641   } while (0)
642 
validate_config(aom_codec_alg_priv_t * ctx,const aom_codec_enc_cfg_t * cfg,const struct av1_extracfg * extra_cfg)643 static aom_codec_err_t validate_config(aom_codec_alg_priv_t *ctx,
644                                        const aom_codec_enc_cfg_t *cfg,
645                                        const struct av1_extracfg *extra_cfg) {
646   RANGE_CHECK(cfg, g_w, 1, 65536);                        // 16 bits available
647   RANGE_CHECK(cfg, g_h, 1, 65536);                        // 16 bits available
648   RANGE_CHECK_HI(cfg, g_forced_max_frame_width, 65536);   // 16 bits available
649   RANGE_CHECK_HI(cfg, g_forced_max_frame_height, 65536);  // 16 bits available
650   if (cfg->g_forced_max_frame_width) {
651     RANGE_CHECK_HI(cfg, g_w, cfg->g_forced_max_frame_width);
652   }
653   if (cfg->g_forced_max_frame_height) {
654     RANGE_CHECK_HI(cfg, g_h, cfg->g_forced_max_frame_height);
655   }
656   // To avoid integer overflows when multiplying width by height (or values
657   // derived from width and height) using the int type, impose a maximum frame
658   // area (width * height) of 2^30.
659   const unsigned int max_frame_width =
660       cfg->g_forced_max_frame_width ? cfg->g_forced_max_frame_width : cfg->g_w;
661   const unsigned int max_frame_height = cfg->g_forced_max_frame_height
662                                             ? cfg->g_forced_max_frame_height
663                                             : cfg->g_h;
664   const int64_t max_frame_area = (int64_t)max_frame_width * max_frame_height;
665   if (max_frame_area > (1 << 30)) {
666     ERROR("max_frame_area out of range [..2^30]");
667   }
668   RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
669   RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
670   RANGE_CHECK_HI(cfg, g_profile, MAX_PROFILES - 1);
671 
672   RANGE_CHECK_HI(cfg, rc_target_bitrate, 2000000);
673   RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
674   RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
675   RANGE_CHECK_BOOL(extra_cfg, lossless);
676   RANGE_CHECK_HI(extra_cfg, aq_mode, AQ_MODE_COUNT - 1);
677   RANGE_CHECK_HI(extra_cfg, deltaq_mode, DELTA_Q_MODE_COUNT - 1);
678   RANGE_CHECK_HI(extra_cfg, deltalf_mode, 1);
679   RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
680 #if CONFIG_REALTIME_ONLY
681   RANGE_CHECK(cfg, g_usage, AOM_USAGE_REALTIME, AOM_USAGE_REALTIME);
682 #else
683   RANGE_CHECK_HI(cfg, g_usage, AOM_USAGE_ALL_INTRA);
684 #endif
685   RANGE_CHECK_HI(cfg, g_threads, MAX_NUM_THREADS);
686   RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
687   RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
688   RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
689   RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
690   RANGE_CHECK(cfg, kf_mode, AOM_KF_DISABLED, AOM_KF_AUTO);
691   RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
692   RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_THIRD_PASS);
693   RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
694   if (cfg->g_usage == AOM_USAGE_ALL_INTRA) {
695     RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
696     RANGE_CHECK_HI(cfg, kf_max_dist, 0);
697   }
698   RANGE_CHECK_HI(extra_cfg, min_gf_interval, MAX_LAG_BUFFERS - 1);
699   RANGE_CHECK_HI(extra_cfg, max_gf_interval, MAX_LAG_BUFFERS - 1);
700   if (extra_cfg->max_gf_interval > 0) {
701     RANGE_CHECK(extra_cfg, max_gf_interval,
702                 AOMMAX(2, extra_cfg->min_gf_interval), (MAX_LAG_BUFFERS - 1));
703   }
704   RANGE_CHECK_HI(extra_cfg, gf_min_pyr_height, 5);
705   RANGE_CHECK_HI(extra_cfg, gf_max_pyr_height, 5);
706   if (extra_cfg->gf_min_pyr_height > extra_cfg->gf_max_pyr_height) {
707     ERROR(
708         "gf_min_pyr_height must be less than or equal to "
709         "gf_max_pyramid_height");
710   }
711 
712   RANGE_CHECK_HI(cfg, rc_resize_mode, RESIZE_MODES - 1);
713   RANGE_CHECK(cfg, rc_resize_denominator, SCALE_NUMERATOR,
714               SCALE_NUMERATOR << 1);
715   RANGE_CHECK(cfg, rc_resize_kf_denominator, SCALE_NUMERATOR,
716               SCALE_NUMERATOR << 1);
717   RANGE_CHECK_HI(cfg, rc_superres_mode, AOM_SUPERRES_AUTO);
718   RANGE_CHECK(cfg, rc_superres_denominator, SCALE_NUMERATOR,
719               SCALE_NUMERATOR << 1);
720   RANGE_CHECK(cfg, rc_superres_kf_denominator, SCALE_NUMERATOR,
721               SCALE_NUMERATOR << 1);
722   RANGE_CHECK(cfg, rc_superres_qthresh, 1, 63);
723   RANGE_CHECK(cfg, rc_superres_kf_qthresh, 1, 63);
724   RANGE_CHECK_HI(extra_cfg, cdf_update_mode, 2);
725 
726   RANGE_CHECK_HI(extra_cfg, motion_vector_unit_test, 2);
727 #if CONFIG_FPMT_TEST
728   RANGE_CHECK_HI(extra_cfg, fpmt_unit_test, 1);
729 #endif
730   RANGE_CHECK_HI(extra_cfg, sb_multipass_unit_test, 1);
731   RANGE_CHECK_HI(extra_cfg, ext_tile_debug, 1);
732   RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 1);
733   RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
734   RANGE_CHECK(extra_cfg, cpu_used, 0,
735               (cfg->g_usage == AOM_USAGE_REALTIME) ? 11 : 9);
736   RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
737   RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
738               AOM_SUPERBLOCK_SIZE_DYNAMIC);
739   RANGE_CHECK_HI(cfg, large_scale_tile, 1);
740   RANGE_CHECK_HI(extra_cfg, single_tile_decoding, 1);
741   RANGE_CHECK_HI(extra_cfg, enable_rate_guide_deltaq, 1);
742 
743   RANGE_CHECK_HI(extra_cfg, row_mt, 1);
744   RANGE_CHECK_HI(extra_cfg, fp_mt, 1);
745 
746   RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
747   RANGE_CHECK_HI(extra_cfg, tile_rows, 6);
748   RANGE_CHECK_HI(extra_cfg, auto_tiles, 1);
749 
750   RANGE_CHECK_HI(cfg, monochrome, 1);
751 
752   if (cfg->large_scale_tile && extra_cfg->aq_mode)
753     ERROR(
754         "Adaptive quantization are not supported in large scale tile "
755         "coding.");
756 
757   RANGE_CHECK_HI(extra_cfg, sharpness, 7);
758   RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
759   RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
760   RANGE_CHECK_HI(extra_cfg, cq_level, 63);
761   RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
762   RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
763   RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
764 
765   if (cfg->g_pass >= AOM_RC_SECOND_PASS) {
766     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
767     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
768     const FIRSTPASS_STATS *stats;
769 
770     if (cfg->rc_twopass_stats_in.buf == NULL)
771       ERROR("rc_twopass_stats_in.buf not set.");
772 
773     if (cfg->rc_twopass_stats_in.sz % packet_sz)
774       ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
775 
776     if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
777       ERROR("rc_twopass_stats_in requires at least two packets.");
778 
779     stats =
780         (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
781 
782     if ((int)(stats->count + 0.5) != n_packets - 1)
783       ERROR("rc_twopass_stats_in missing EOS stats packet");
784   }
785 
786   if (extra_cfg->passes != -1 && cfg->g_pass == AOM_RC_ONE_PASS &&
787       extra_cfg->passes != 1) {
788     ERROR("One pass encoding but passes != 1.");
789   }
790 
791   if (extra_cfg->passes != -1 && (int)cfg->g_pass > extra_cfg->passes) {
792     ERROR("Current pass is larger than total number of passes.");
793   }
794 
795   if (cfg->g_profile == (unsigned int)PROFILE_1 && cfg->monochrome) {
796     ERROR("Monochrome is not supported in profile 1");
797   }
798 
799   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
800       cfg->g_bit_depth > AOM_BITS_10) {
801     ERROR("Codec bit-depth 12 not supported in profile < 2");
802   }
803   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
804       cfg->g_input_bit_depth > 10) {
805     ERROR("Source bit-depth 12 not supported in profile < 2");
806   }
807 
808   if (cfg->rc_end_usage == AOM_Q) {
809     RANGE_CHECK_HI(cfg, use_fixed_qp_offsets, 1);
810   } else {
811     if (cfg->use_fixed_qp_offsets > 0) {
812       ERROR("--use_fixed_qp_offsets can only be used with --end-usage=q");
813     }
814   }
815 
816   RANGE_CHECK(extra_cfg, color_primaries, AOM_CICP_CP_BT_709,
817               AOM_CICP_CP_EBU_3213);  // Need to check range more precisely to
818                                       // check for reserved values?
819   RANGE_CHECK(extra_cfg, transfer_characteristics, AOM_CICP_TC_BT_709,
820               AOM_CICP_TC_HLG);
821   RANGE_CHECK(extra_cfg, matrix_coefficients, AOM_CICP_MC_IDENTITY,
822               AOM_CICP_MC_ICTCP);
823   RANGE_CHECK(extra_cfg, color_range, 0, 1);
824 
825   /* Average corpus complexity is supported only in the case of single pass
826    * VBR*/
827   if (cfg->g_pass == AOM_RC_ONE_PASS && cfg->rc_end_usage == AOM_VBR)
828     RANGE_CHECK_HI(extra_cfg, vbr_corpus_complexity_lap,
829                    MAX_VBR_CORPUS_COMPLEXITY);
830   else if (extra_cfg->vbr_corpus_complexity_lap != 0)
831     ERROR(
832         "VBR corpus complexity is supported only in the case of single pass "
833         "VBR mode.");
834 
835 #if !CONFIG_TUNE_BUTTERAUGLI
836   if (extra_cfg->tuning == AOM_TUNE_BUTTERAUGLI) {
837     ERROR(
838         "This error may be related to the wrong configuration options: try to "
839         "set -DCONFIG_TUNE_BUTTERAUGLI=1 at the time CMake is run.");
840   }
841 #endif
842 
843 #if !CONFIG_TUNE_VMAF
844   if (extra_cfg->tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
845       extra_cfg->tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
846     ERROR(
847         "This error may be related to the wrong configuration options: try to "
848         "set -DCONFIG_TUNE_VMAF=1 at the time CMake is run.");
849   }
850 #endif
851 
852   RANGE_CHECK(extra_cfg, tuning, AOM_TUNE_PSNR, AOM_TUNE_VMAF_SALIENCY_MAP);
853 
854   RANGE_CHECK(extra_cfg, dist_metric, AOM_DIST_METRIC_PSNR,
855               AOM_DIST_METRIC_QM_PSNR);
856 
857   RANGE_CHECK(extra_cfg, timing_info_type, AOM_TIMING_UNSPECIFIED,
858               AOM_TIMING_DEC_MODEL);
859 
860   RANGE_CHECK(extra_cfg, film_grain_test_vector, 0, 16);
861 
862   if (extra_cfg->lossless) {
863     if (extra_cfg->aq_mode != 0)
864       ERROR("Only --aq_mode=0 can be used with --lossless=1.");
865     if (extra_cfg->enable_chroma_deltaq)
866       ERROR("Only --enable_chroma_deltaq=0 can be used with --lossless=1.");
867   }
868 
869   RANGE_CHECK(extra_cfg, max_reference_frames, 3, 7);
870   RANGE_CHECK(extra_cfg, enable_reduced_reference_set, 0, 1);
871   RANGE_CHECK_HI(extra_cfg, chroma_subsampling_x, 1);
872   RANGE_CHECK_HI(extra_cfg, chroma_subsampling_y, 1);
873 
874   RANGE_CHECK_HI(extra_cfg, disable_trellis_quant, 3);
875   RANGE_CHECK(extra_cfg, coeff_cost_upd_freq, 0, 3);
876   RANGE_CHECK(extra_cfg, mode_cost_upd_freq, 0, 3);
877   RANGE_CHECK(extra_cfg, mv_cost_upd_freq, 0, 3);
878   RANGE_CHECK(extra_cfg, dv_cost_upd_freq, 0, 3);
879 
880   RANGE_CHECK(extra_cfg, min_partition_size, 4, 128);
881   RANGE_CHECK(extra_cfg, max_partition_size, 4, 128);
882   RANGE_CHECK_HI(extra_cfg, min_partition_size, extra_cfg->max_partition_size);
883 
884   for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
885     const int level_idx = extra_cfg->target_seq_level_idx[i];
886     if (!is_valid_seq_level_idx(level_idx) &&
887         level_idx != SEQ_LEVEL_KEEP_STATS) {
888       ERROR("Target sequence level index is invalid");
889     }
890   }
891 
892   RANGE_CHECK(extra_cfg, deltaq_strength, 0, 1000);
893   RANGE_CHECK_HI(extra_cfg, loopfilter_control, 3);
894   RANGE_CHECK_BOOL(extra_cfg, skip_postproc_filtering);
895   RANGE_CHECK_HI(extra_cfg, enable_cdef, 2);
896   RANGE_CHECK_BOOL(extra_cfg, auto_intra_tools_off);
897   RANGE_CHECK_BOOL(extra_cfg, strict_level_conformance);
898   RANGE_CHECK_BOOL(extra_cfg, sb_qp_sweep);
899 
900   RANGE_CHECK(extra_cfg, kf_max_pyr_height, -1, 5);
901   if (extra_cfg->kf_max_pyr_height != -1 &&
902       extra_cfg->kf_max_pyr_height < (int)extra_cfg->gf_min_pyr_height) {
903     ERROR(
904         "The value of kf-max-pyr-height should not be smaller than "
905         "gf-min-pyr-height");
906   }
907 
908   return AOM_CODEC_OK;
909 }
910 
validate_img(aom_codec_alg_priv_t * ctx,const aom_image_t * img)911 static aom_codec_err_t validate_img(aom_codec_alg_priv_t *ctx,
912                                     const aom_image_t *img) {
913   switch (img->fmt) {
914     case AOM_IMG_FMT_YV12:
915     case AOM_IMG_FMT_NV12:
916     case AOM_IMG_FMT_I420:
917     case AOM_IMG_FMT_YV1216:
918     case AOM_IMG_FMT_I42016: break;
919     case AOM_IMG_FMT_I444:
920     case AOM_IMG_FMT_I44416:
921       if (ctx->cfg.g_profile == (unsigned int)PROFILE_0 &&
922           !ctx->cfg.monochrome) {
923         ERROR("Invalid image format. I444 images not supported in profile.");
924       }
925       break;
926     case AOM_IMG_FMT_I422:
927     case AOM_IMG_FMT_I42216:
928       if (ctx->cfg.g_profile != (unsigned int)PROFILE_2) {
929         ERROR("Invalid image format. I422 images not supported in profile.");
930       }
931       break;
932     default:
933       ERROR(
934           "Invalid image format. Only YV12, NV12, I420, I422, I444 images are "
935           "supported.");
936       break;
937   }
938 
939   if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
940     ERROR("Image size must match encoder init configuration size");
941 
942 #if CONFIG_TUNE_BUTTERAUGLI
943   if (ctx->extra_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
944     if (img->bit_depth > 8) {
945       ERROR("Only 8 bit depth images supported in tune=butteraugli mode.");
946     }
947     if (img->mc != 0 && img->mc != AOM_CICP_MC_BT_709 &&
948         img->mc != AOM_CICP_MC_BT_601 && img->mc != AOM_CICP_MC_BT_470_B_G) {
949       ERROR(
950           "Only BT.709 and BT.601 matrix coefficients supported in "
951           "tune=butteraugli mode. Identity matrix is treated as BT.601.");
952     }
953   }
954 #endif
955 
956   return AOM_CODEC_OK;
957 }
958 
get_image_bps(const aom_image_t * img)959 static int get_image_bps(const aom_image_t *img) {
960   switch (img->fmt) {
961     case AOM_IMG_FMT_YV12:
962     case AOM_IMG_FMT_NV12:
963     case AOM_IMG_FMT_I420: return 12;
964     case AOM_IMG_FMT_I422: return 16;
965     case AOM_IMG_FMT_I444: return 24;
966     case AOM_IMG_FMT_YV1216:
967     case AOM_IMG_FMT_I42016: return 24;
968     case AOM_IMG_FMT_I42216: return 32;
969     case AOM_IMG_FMT_I44416: return 48;
970     default: assert(0 && "Invalid image format"); break;
971   }
972   return 0;
973 }
974 
975 // Set appropriate options to disable frame super-resolution.
disable_superres(SuperResCfg * const superres_cfg)976 static void disable_superres(SuperResCfg *const superres_cfg) {
977   superres_cfg->superres_mode = AOM_SUPERRES_NONE;
978   superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
979   superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
980   superres_cfg->superres_qthresh = 255;
981   superres_cfg->superres_kf_qthresh = 255;
982 }
983 
set_auto_tiles(TileConfig * const tile_cfg,unsigned int width,unsigned int height,unsigned int threads)984 static void set_auto_tiles(TileConfig *const tile_cfg, unsigned int width,
985                            unsigned int height, unsigned int threads) {
986   int tile_cols_log2 = 0;
987   int tile_rows_log2 = 0;
988   if (threads < 2) return;
989   // Avoid small tiles because they are particularly bad for coding.
990   // Use no more tiles than the number of threads. Aim for one tile per
991   // thread. Using more than one thread inside one tile could be less
992   // efficient. Using more tiles than the number of threads would result
993   // in a compression penalty without much benefit.
994   const uint32_t kMinTileArea = 128 * 128;
995   const uint32_t kMaxTiles = 32;
996   uint32_t frame_area = width * height;
997   uint32_t tiles = (frame_area + kMinTileArea - 1) / kMinTileArea;
998   if (tiles > kMaxTiles) {
999     tiles = kMaxTiles;
1000   }
1001   if (tiles > threads) {
1002     tiles = threads;
1003   }
1004   int tiles_log2 = (int)log2(tiles);
1005   // If the frame width is equal or greater than the height, use more tile
1006   // columns than tile rows.
1007   if (width >= height) {
1008     tile_cols_log2 = (tiles_log2 + 1) / 2;
1009     tile_rows_log2 = tiles_log2 - tile_cols_log2;
1010   } else {
1011     tile_rows_log2 = (tiles_log2 + 1) / 2;
1012     tile_cols_log2 = tiles_log2 - tile_rows_log2;
1013   }
1014   tile_cfg->tile_columns = tile_cols_log2;
1015   tile_cfg->tile_rows = tile_rows_log2;
1016 }
1017 
update_default_encoder_config(const cfg_options_t * cfg,struct av1_extracfg * extra_cfg)1018 static void update_default_encoder_config(const cfg_options_t *cfg,
1019                                           struct av1_extracfg *extra_cfg) {
1020   extra_cfg->enable_cdef = (cfg->disable_cdef == 0) ? 1 : 0;
1021   extra_cfg->enable_restoration = (cfg->disable_lr == 0);
1022   extra_cfg->superblock_size =
1023       (cfg->super_block_size == 64)    ? AOM_SUPERBLOCK_SIZE_64X64
1024       : (cfg->super_block_size == 128) ? AOM_SUPERBLOCK_SIZE_128X128
1025                                        : AOM_SUPERBLOCK_SIZE_DYNAMIC;
1026   extra_cfg->enable_warped_motion = (cfg->disable_warp_motion == 0);
1027   extra_cfg->enable_dist_wtd_comp = (cfg->disable_dist_wtd_comp == 0);
1028   extra_cfg->enable_diff_wtd_comp = (cfg->disable_diff_wtd_comp == 0);
1029   extra_cfg->enable_dual_filter = (cfg->disable_dual_filter == 0);
1030   extra_cfg->enable_angle_delta = (cfg->disable_intra_angle_delta == 0);
1031   extra_cfg->enable_rect_partitions = (cfg->disable_rect_partition_type == 0);
1032   extra_cfg->enable_ab_partitions = (cfg->disable_ab_partition_type == 0);
1033   extra_cfg->enable_1to4_partitions = (cfg->disable_1to4_partition_type == 0);
1034   extra_cfg->max_partition_size = cfg->max_partition_size;
1035   extra_cfg->min_partition_size = cfg->min_partition_size;
1036   extra_cfg->enable_intra_edge_filter = (cfg->disable_intra_edge_filter == 0);
1037   extra_cfg->enable_tx64 = (cfg->disable_tx_64x64 == 0);
1038   extra_cfg->enable_flip_idtx = (cfg->disable_flip_idtx == 0);
1039   extra_cfg->enable_masked_comp = (cfg->disable_masked_comp == 0);
1040   extra_cfg->enable_interintra_comp = (cfg->disable_inter_intra_comp == 0);
1041   extra_cfg->enable_smooth_interintra = (cfg->disable_smooth_inter_intra == 0);
1042   extra_cfg->enable_interinter_wedge = (cfg->disable_inter_inter_wedge == 0);
1043   extra_cfg->enable_interintra_wedge = (cfg->disable_inter_intra_wedge == 0);
1044   extra_cfg->enable_global_motion = (cfg->disable_global_motion == 0);
1045   extra_cfg->enable_filter_intra = (cfg->disable_filter_intra == 0);
1046   extra_cfg->enable_smooth_intra = (cfg->disable_smooth_intra == 0);
1047   extra_cfg->enable_paeth_intra = (cfg->disable_paeth_intra == 0);
1048   extra_cfg->enable_cfl_intra = (cfg->disable_cfl == 0);
1049   extra_cfg->enable_obmc = (cfg->disable_obmc == 0);
1050   extra_cfg->enable_palette = (cfg->disable_palette == 0);
1051   extra_cfg->enable_intrabc = (cfg->disable_intrabc == 0);
1052   extra_cfg->disable_trellis_quant = cfg->disable_trellis_quant;
1053   extra_cfg->allow_ref_frame_mvs = (cfg->disable_ref_frame_mv == 0);
1054   extra_cfg->enable_ref_frame_mvs = (cfg->disable_ref_frame_mv == 0);
1055   extra_cfg->enable_onesided_comp = (cfg->disable_one_sided_comp == 0);
1056   extra_cfg->enable_reduced_reference_set = cfg->reduced_reference_set;
1057   extra_cfg->reduced_tx_type_set = cfg->reduced_tx_type_set;
1058 }
1059 
set_encoder_config(AV1EncoderConfig * oxcf,const aom_codec_enc_cfg_t * cfg,struct av1_extracfg * extra_cfg)1060 static void set_encoder_config(AV1EncoderConfig *oxcf,
1061                                const aom_codec_enc_cfg_t *cfg,
1062                                struct av1_extracfg *extra_cfg) {
1063   if (cfg->encoder_cfg.init_by_cfg_file) {
1064     update_default_encoder_config(&cfg->encoder_cfg, extra_cfg);
1065   }
1066 
1067   TuneCfg *const tune_cfg = &oxcf->tune_cfg;
1068   FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
1069   TileConfig *const tile_cfg = &oxcf->tile_cfg;
1070   ResizeCfg *const resize_cfg = &oxcf->resize_cfg;
1071   GFConfig *const gf_cfg = &oxcf->gf_cfg;
1072   PartitionCfg *const part_cfg = &oxcf->part_cfg;
1073   IntraModeCfg *const intra_mode_cfg = &oxcf->intra_mode_cfg;
1074   TxfmSizeTypeCfg *const txfm_cfg = &oxcf->txfm_cfg;
1075   CompoundTypeCfg *const comp_type_cfg = &oxcf->comp_type_cfg;
1076   SuperResCfg *const superres_cfg = &oxcf->superres_cfg;
1077   KeyFrameCfg *const kf_cfg = &oxcf->kf_cfg;
1078   DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
1079   RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
1080   QuantizationCfg *const q_cfg = &oxcf->q_cfg;
1081   ColorCfg *const color_cfg = &oxcf->color_cfg;
1082   InputCfg *const input_cfg = &oxcf->input_cfg;
1083   AlgoCfg *const algo_cfg = &oxcf->algo_cfg;
1084   ToolCfg *const tool_cfg = &oxcf->tool_cfg;
1085 
1086   const int is_vbr = cfg->rc_end_usage == AOM_VBR;
1087   oxcf->profile = cfg->g_profile;
1088   oxcf->max_threads = (int)cfg->g_threads;
1089 
1090   switch (cfg->g_usage) {
1091     case AOM_USAGE_REALTIME: oxcf->mode = REALTIME; break;
1092     case AOM_USAGE_ALL_INTRA: oxcf->mode = ALLINTRA; break;
1093     default: oxcf->mode = GOOD; break;
1094   }
1095 
1096   // Set frame-dimension related configuration.
1097   frm_dim_cfg->width = cfg->g_w;
1098   frm_dim_cfg->height = cfg->g_h;
1099   frm_dim_cfg->forced_max_frame_width = cfg->g_forced_max_frame_width;
1100   frm_dim_cfg->forced_max_frame_height = cfg->g_forced_max_frame_height;
1101   frm_dim_cfg->render_width = extra_cfg->render_width;
1102   frm_dim_cfg->render_height = extra_cfg->render_height;
1103 
1104   // Set input video related configuration.
1105   input_cfg->input_bit_depth = cfg->g_input_bit_depth;
1106   // guess a frame rate if out of whack, use 30
1107   input_cfg->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
1108   if (cfg->g_pass >= AOM_RC_SECOND_PASS) {
1109     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1110     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
1111     input_cfg->limit = n_packets - 1;
1112   } else {
1113     input_cfg->limit = cfg->g_limit;
1114   }
1115   input_cfg->chroma_subsampling_x = extra_cfg->chroma_subsampling_x;
1116   input_cfg->chroma_subsampling_y = extra_cfg->chroma_subsampling_y;
1117   if (input_cfg->init_framerate > 180) {
1118     input_cfg->init_framerate = 30;
1119     dec_model_cfg->timing_info_present = 0;
1120   }
1121 
1122   // Set Decoder model configuration.
1123   if (extra_cfg->timing_info_type == AOM_TIMING_EQUAL ||
1124       extra_cfg->timing_info_type == AOM_TIMING_DEC_MODEL) {
1125     dec_model_cfg->timing_info_present = 1;
1126     dec_model_cfg->timing_info.num_units_in_display_tick = cfg->g_timebase.num;
1127     dec_model_cfg->timing_info.time_scale = cfg->g_timebase.den;
1128     dec_model_cfg->timing_info.num_ticks_per_picture = 1;
1129   } else {
1130     dec_model_cfg->timing_info_present = 0;
1131   }
1132   if (extra_cfg->timing_info_type == AOM_TIMING_EQUAL) {
1133     dec_model_cfg->timing_info.equal_picture_interval = 1;
1134     dec_model_cfg->decoder_model_info_present_flag = 0;
1135     dec_model_cfg->display_model_info_present_flag = 1;
1136   } else if (extra_cfg->timing_info_type == AOM_TIMING_DEC_MODEL) {
1137     dec_model_cfg->num_units_in_decoding_tick = cfg->g_timebase.num;
1138     dec_model_cfg->timing_info.equal_picture_interval = 0;
1139     dec_model_cfg->decoder_model_info_present_flag = 1;
1140     dec_model_cfg->display_model_info_present_flag = 1;
1141   }
1142 
1143   oxcf->pass = cfg->g_pass;
1144   // For backward compatibility, assume that if extra_cfg->passes==-1, then
1145   // passes = 1 or 2.
1146   if (extra_cfg->passes == -1) {
1147     if (cfg->g_pass == AOM_RC_ONE_PASS) {
1148       oxcf->passes = 1;
1149     } else {
1150       oxcf->passes = 2;
1151     }
1152   } else {
1153     oxcf->passes = extra_cfg->passes;
1154   }
1155 
1156   // Set Rate Control configuration.
1157   rc_cfg->max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
1158   rc_cfg->max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
1159   rc_cfg->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
1160   rc_cfg->mode = cfg->rc_end_usage;
1161   rc_cfg->min_cr = extra_cfg->min_cr;
1162   rc_cfg->best_allowed_q =
1163       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
1164   rc_cfg->worst_allowed_q =
1165       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_max_quantizer);
1166   rc_cfg->cq_level = av1_quantizer_to_qindex(extra_cfg->cq_level);
1167   rc_cfg->under_shoot_pct = cfg->rc_undershoot_pct;
1168   rc_cfg->over_shoot_pct = cfg->rc_overshoot_pct;
1169   rc_cfg->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
1170   rc_cfg->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
1171   rc_cfg->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
1172   // Convert target bandwidth from Kbit/s to Bit/s
1173   rc_cfg->target_bandwidth = 1000 * cfg->rc_target_bitrate;
1174   rc_cfg->drop_frames_water_mark = cfg->rc_dropframe_thresh;
1175   rc_cfg->vbr_corpus_complexity_lap = extra_cfg->vbr_corpus_complexity_lap;
1176   rc_cfg->vbrbias = cfg->rc_2pass_vbr_bias_pct;
1177   rc_cfg->vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
1178   rc_cfg->vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
1179 
1180   // Set Toolset related configuration.
1181   tool_cfg->bit_depth = cfg->g_bit_depth;
1182   tool_cfg->cdef_control = (CDEF_CONTROL)extra_cfg->enable_cdef;
1183   tool_cfg->enable_restoration =
1184       (cfg->g_usage == AOM_USAGE_REALTIME) ? 0 : extra_cfg->enable_restoration;
1185   tool_cfg->force_video_mode = extra_cfg->force_video_mode;
1186   tool_cfg->enable_palette = extra_cfg->enable_palette;
1187   // FIXME(debargha): Should this be:
1188   // tool_cfg->enable_ref_frame_mvs  = extra_cfg->allow_ref_frame_mvs &
1189   //                                         extra_cfg->enable_order_hint ?
1190   // Disallow using temporal MVs while large_scale_tile = 1.
1191   tool_cfg->enable_ref_frame_mvs =
1192       extra_cfg->allow_ref_frame_mvs && !cfg->large_scale_tile;
1193   tool_cfg->superblock_size = extra_cfg->superblock_size;
1194   tool_cfg->enable_monochrome = cfg->monochrome;
1195   tool_cfg->full_still_picture_hdr = cfg->full_still_picture_hdr != 0;
1196   tool_cfg->enable_dual_filter = extra_cfg->enable_dual_filter;
1197   tool_cfg->enable_order_hint = extra_cfg->enable_order_hint;
1198   tool_cfg->enable_interintra_comp = extra_cfg->enable_interintra_comp;
1199   tool_cfg->ref_frame_mvs_present =
1200       extra_cfg->enable_ref_frame_mvs & extra_cfg->enable_order_hint;
1201 
1202   // Explicitly disable global motion in a few cases:
1203   // * For realtime mode, we never search global motion, and disabling
1204   //   it here prevents later code from allocating buffers we don't need
1205   // * For large scale tile mode, some of the intended use cases expect
1206   //   all frame headers to be identical. This breaks if global motion is
1207   //   used, since global motion data is stored in the frame header.
1208   //   eg, see test/lightfield_test.sh, which checks that all frame headers
1209   //   are the same.
1210   tool_cfg->enable_global_motion = extra_cfg->enable_global_motion &&
1211                                    cfg->g_usage != AOM_USAGE_REALTIME &&
1212                                    !cfg->large_scale_tile;
1213 
1214   tool_cfg->error_resilient_mode =
1215       cfg->g_error_resilient | extra_cfg->error_resilient_mode;
1216   tool_cfg->frame_parallel_decoding_mode =
1217       extra_cfg->frame_parallel_decoding_mode;
1218 
1219   // Set Quantization related configuration.
1220   q_cfg->using_qm = extra_cfg->enable_qm;
1221   q_cfg->qm_minlevel = extra_cfg->qm_min;
1222   q_cfg->qm_maxlevel = extra_cfg->qm_max;
1223   q_cfg->quant_b_adapt = extra_cfg->quant_b_adapt;
1224   q_cfg->enable_chroma_deltaq = extra_cfg->enable_chroma_deltaq;
1225   q_cfg->aq_mode = extra_cfg->aq_mode;
1226   q_cfg->deltaq_mode = extra_cfg->deltaq_mode;
1227   q_cfg->deltaq_strength = extra_cfg->deltaq_strength;
1228   q_cfg->use_fixed_qp_offsets =
1229       cfg->use_fixed_qp_offsets && (rc_cfg->mode == AOM_Q);
1230   q_cfg->enable_hdr_deltaq =
1231       (q_cfg->deltaq_mode == DELTA_Q_HDR) &&
1232       (cfg->g_bit_depth == AOM_BITS_10) &&
1233       (extra_cfg->color_primaries == AOM_CICP_CP_BT_2020);
1234 
1235   tool_cfg->enable_deltalf_mode =
1236       (q_cfg->deltaq_mode != NO_DELTA_Q) && extra_cfg->deltalf_mode;
1237 
1238   // Set cost update frequency configuration.
1239   oxcf->cost_upd_freq.coeff = (COST_UPDATE_TYPE)extra_cfg->coeff_cost_upd_freq;
1240   oxcf->cost_upd_freq.mode = (COST_UPDATE_TYPE)extra_cfg->mode_cost_upd_freq;
1241   // Avoid MV cost update for allintra encoding mode.
1242   oxcf->cost_upd_freq.mv = (cfg->kf_max_dist != 0)
1243                                ? (COST_UPDATE_TYPE)extra_cfg->mv_cost_upd_freq
1244                                : COST_UPD_OFF;
1245   oxcf->cost_upd_freq.dv = (COST_UPDATE_TYPE)extra_cfg->dv_cost_upd_freq;
1246 
1247   // Set frame resize mode configuration.
1248   resize_cfg->resize_mode = (RESIZE_MODE)cfg->rc_resize_mode;
1249   resize_cfg->resize_scale_denominator = (uint8_t)cfg->rc_resize_denominator;
1250   resize_cfg->resize_kf_scale_denominator =
1251       (uint8_t)cfg->rc_resize_kf_denominator;
1252   if (resize_cfg->resize_mode == RESIZE_FIXED &&
1253       resize_cfg->resize_scale_denominator == SCALE_NUMERATOR &&
1254       resize_cfg->resize_kf_scale_denominator == SCALE_NUMERATOR)
1255     resize_cfg->resize_mode = RESIZE_NONE;
1256 
1257   // Set encoder algorithm related configuration.
1258   algo_cfg->enable_overlay = extra_cfg->enable_overlay;
1259   algo_cfg->disable_trellis_quant = extra_cfg->disable_trellis_quant;
1260   algo_cfg->sharpness = extra_cfg->sharpness;
1261   algo_cfg->arnr_max_frames = extra_cfg->arnr_max_frames;
1262   algo_cfg->arnr_strength = extra_cfg->arnr_strength;
1263   algo_cfg->cdf_update_mode = (uint8_t)extra_cfg->cdf_update_mode;
1264   // TODO(any): Fix and Enable TPL for resize-mode > 0
1265   algo_cfg->enable_tpl_model =
1266       resize_cfg->resize_mode ? 0 : extra_cfg->enable_tpl_model;
1267   algo_cfg->loopfilter_control = extra_cfg->loopfilter_control;
1268   algo_cfg->skip_postproc_filtering = extra_cfg->skip_postproc_filtering;
1269 
1270   // Set two-pass stats configuration.
1271   oxcf->twopass_stats_in = cfg->rc_twopass_stats_in;
1272 
1273   if (extra_cfg->two_pass_output)
1274     oxcf->two_pass_output = extra_cfg->two_pass_output;
1275 
1276   oxcf->second_pass_log = extra_cfg->second_pass_log;
1277 
1278   // Set Key frame configuration.
1279   kf_cfg->fwd_kf_enabled = cfg->fwd_kf_enabled;
1280   kf_cfg->auto_key =
1281       cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
1282   kf_cfg->key_freq_min = cfg->kf_min_dist;
1283   kf_cfg->key_freq_max = cfg->kf_max_dist;
1284   kf_cfg->sframe_dist = cfg->sframe_dist;
1285   kf_cfg->sframe_mode = cfg->sframe_mode;
1286   kf_cfg->enable_sframe = extra_cfg->s_frame_mode;
1287   kf_cfg->enable_keyframe_filtering = extra_cfg->enable_keyframe_filtering;
1288   kf_cfg->fwd_kf_dist = extra_cfg->fwd_kf_dist;
1289   // Disable key frame filtering in all intra mode.
1290   if (cfg->kf_max_dist == 0) {
1291     kf_cfg->enable_keyframe_filtering = 0;
1292   }
1293   kf_cfg->enable_intrabc = extra_cfg->enable_intrabc;
1294 
1295   oxcf->speed = extra_cfg->cpu_used;
1296   // TODO(yunqingwang, any) In REALTIME mode, 1080p performance at speed 5 & 6
1297   // is quite bad. Force to use speed 7 for now. Will investigate it when we
1298   // work on rd path optimization later.
1299   if (oxcf->mode == REALTIME && AOMMIN(cfg->g_w, cfg->g_h) >= 1080 &&
1300       oxcf->speed < 7)
1301     oxcf->speed = 7;
1302 
1303   // Set Color related configuration.
1304   color_cfg->color_primaries = extra_cfg->color_primaries;
1305   color_cfg->transfer_characteristics = extra_cfg->transfer_characteristics;
1306   color_cfg->matrix_coefficients = extra_cfg->matrix_coefficients;
1307   color_cfg->color_range = extra_cfg->color_range;
1308   color_cfg->chroma_sample_position = extra_cfg->chroma_sample_position;
1309 
1310   // Set Group of frames configuration.
1311   // Force lag_in_frames to 0 for REALTIME mode
1312   gf_cfg->lag_in_frames = (oxcf->mode == REALTIME)
1313                               ? 0
1314                               : clamp(cfg->g_lag_in_frames, 0, MAX_LAG_BUFFERS);
1315   gf_cfg->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
1316   gf_cfg->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
1317   gf_cfg->min_gf_interval = extra_cfg->min_gf_interval;
1318   gf_cfg->max_gf_interval = extra_cfg->max_gf_interval;
1319   gf_cfg->gf_min_pyr_height = extra_cfg->gf_min_pyr_height;
1320   gf_cfg->gf_max_pyr_height = extra_cfg->gf_max_pyr_height;
1321 
1322   // Set tune related configuration.
1323   tune_cfg->tuning = extra_cfg->tuning;
1324   tune_cfg->vmaf_model_path = extra_cfg->vmaf_model_path;
1325   tune_cfg->content = extra_cfg->content;
1326   if (cfg->large_scale_tile) {
1327     tune_cfg->film_grain_test_vector = 0;
1328     tune_cfg->film_grain_table_filename = NULL;
1329   } else {
1330     tune_cfg->film_grain_test_vector = extra_cfg->film_grain_test_vector;
1331     tune_cfg->film_grain_table_filename = extra_cfg->film_grain_table_filename;
1332   }
1333   tune_cfg->dist_metric = extra_cfg->dist_metric;
1334 #if CONFIG_DENOISE
1335   oxcf->noise_level = extra_cfg->noise_level;
1336   oxcf->noise_block_size = extra_cfg->noise_block_size;
1337   oxcf->enable_dnl_denoising = extra_cfg->enable_dnl_denoising;
1338 #endif
1339 
1340 #if CONFIG_AV1_TEMPORAL_DENOISING
1341   // Temporal denoiser is for nonrd pickmode so disable it for speed < 7.
1342   // Also disable it for speed 7 for now since it needs to be modified for
1343   // the check_partition_merge_mode feature.
1344   if (cfg->g_bit_depth == AOM_BITS_8 && oxcf->speed > 7) {
1345     oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
1346   } else {
1347     oxcf->noise_sensitivity = 0;
1348   }
1349 #endif
1350   // Set Tile related configuration.
1351   tile_cfg->num_tile_groups = extra_cfg->num_tg;
1352   // In large-scale tile encoding mode, num_tile_groups is always 1.
1353   if (cfg->large_scale_tile) tile_cfg->num_tile_groups = 1;
1354   tile_cfg->mtu = extra_cfg->mtu_size;
1355   tile_cfg->enable_large_scale_tile = cfg->large_scale_tile;
1356   tile_cfg->enable_single_tile_decoding =
1357       (tile_cfg->enable_large_scale_tile) ? extra_cfg->single_tile_decoding : 0;
1358   if (extra_cfg->auto_tiles) {
1359     set_auto_tiles(tile_cfg, cfg->g_w, cfg->g_h, cfg->g_threads);
1360     extra_cfg->tile_columns = tile_cfg->tile_columns;
1361     extra_cfg->tile_rows = tile_cfg->tile_rows;
1362   } else {
1363     tile_cfg->tile_columns = extra_cfg->tile_columns;
1364     tile_cfg->tile_rows = extra_cfg->tile_rows;
1365   }
1366   tile_cfg->tile_width_count = AOMMIN(cfg->tile_width_count, MAX_TILE_COLS);
1367   tile_cfg->tile_height_count = AOMMIN(cfg->tile_height_count, MAX_TILE_ROWS);
1368   for (int i = 0; i < tile_cfg->tile_width_count; i++) {
1369     tile_cfg->tile_widths[i] = cfg->tile_widths[i];
1370   }
1371   for (int i = 0; i < tile_cfg->tile_height_count; i++) {
1372     tile_cfg->tile_heights[i] = cfg->tile_heights[i];
1373   }
1374   tile_cfg->enable_ext_tile_debug = extra_cfg->ext_tile_debug;
1375 
1376   if (tile_cfg->enable_large_scale_tile) {
1377     // The superblock_size can only be AOM_SUPERBLOCK_SIZE_64X64 or
1378     // AOM_SUPERBLOCK_SIZE_128X128 while tile_cfg->enable_large_scale_tile = 1.
1379     // If superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC, hard set it to
1380     // AOM_SUPERBLOCK_SIZE_64X64(default value in large_scale_tile).
1381     if (extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_64X64 &&
1382         extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_128X128)
1383       tool_cfg->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
1384   }
1385 
1386   // Set reference frame related configuration.
1387   oxcf->ref_frm_cfg.max_reference_frames = extra_cfg->max_reference_frames;
1388   oxcf->ref_frm_cfg.enable_reduced_reference_set =
1389       extra_cfg->enable_reduced_reference_set;
1390   oxcf->ref_frm_cfg.enable_onesided_comp = extra_cfg->enable_onesided_comp;
1391 
1392   oxcf->row_mt = extra_cfg->row_mt;
1393   oxcf->fp_mt = extra_cfg->fp_mt;
1394 
1395   // Set motion mode related configuration.
1396   oxcf->motion_mode_cfg.enable_obmc = extra_cfg->enable_obmc;
1397   oxcf->motion_mode_cfg.enable_warped_motion = extra_cfg->enable_warped_motion;
1398 #if !CONFIG_REALTIME_ONLY
1399   if (cfg->g_usage == AOM_USAGE_REALTIME && oxcf->speed >= 7 &&
1400       oxcf->tune_cfg.content == AOM_CONTENT_SCREEN) {
1401     // TODO(marpan): warped motion is causing a crash for RT mode with screen
1402     // in nonrd (speed >= 7), for non-realtime build.
1403     // Re-enable/allow when the issue is fixed.
1404     oxcf->motion_mode_cfg.enable_warped_motion = 0;
1405     oxcf->motion_mode_cfg.allow_warped_motion = 0;
1406   } else {
1407     oxcf->motion_mode_cfg.allow_warped_motion =
1408         (extra_cfg->allow_warped_motion & extra_cfg->enable_warped_motion);
1409   }
1410 #else
1411   oxcf->motion_mode_cfg.allow_warped_motion =
1412       (cfg->g_usage == AOM_USAGE_REALTIME && oxcf->speed >= 7)
1413           ? false
1414           : (extra_cfg->allow_warped_motion & extra_cfg->enable_warped_motion);
1415 #endif
1416 
1417   // Set partition related configuration.
1418   part_cfg->enable_rect_partitions = extra_cfg->enable_rect_partitions;
1419   part_cfg->enable_ab_partitions = extra_cfg->enable_ab_partitions;
1420   part_cfg->enable_1to4_partitions = extra_cfg->enable_1to4_partitions;
1421   part_cfg->min_partition_size = extra_cfg->min_partition_size;
1422   part_cfg->max_partition_size = extra_cfg->max_partition_size;
1423 
1424   // Set intra mode configuration.
1425   intra_mode_cfg->enable_angle_delta = extra_cfg->enable_angle_delta;
1426   intra_mode_cfg->enable_intra_edge_filter =
1427       extra_cfg->enable_intra_edge_filter;
1428   intra_mode_cfg->enable_filter_intra = extra_cfg->enable_filter_intra;
1429   intra_mode_cfg->enable_smooth_intra = extra_cfg->enable_smooth_intra;
1430   intra_mode_cfg->enable_paeth_intra = extra_cfg->enable_paeth_intra;
1431   intra_mode_cfg->enable_cfl_intra = extra_cfg->enable_cfl_intra;
1432   intra_mode_cfg->enable_directional_intra =
1433       extra_cfg->enable_directional_intra;
1434   intra_mode_cfg->enable_diagonal_intra = extra_cfg->enable_diagonal_intra;
1435   intra_mode_cfg->auto_intra_tools_off = extra_cfg->auto_intra_tools_off;
1436 
1437   // Set transform size/type configuration.
1438   txfm_cfg->enable_tx64 = extra_cfg->enable_tx64;
1439   txfm_cfg->enable_flip_idtx = extra_cfg->enable_flip_idtx;
1440   txfm_cfg->enable_rect_tx = extra_cfg->enable_rect_tx;
1441   txfm_cfg->reduced_tx_type_set = extra_cfg->reduced_tx_type_set;
1442   txfm_cfg->use_intra_dct_only = extra_cfg->use_intra_dct_only;
1443   txfm_cfg->use_inter_dct_only = extra_cfg->use_inter_dct_only;
1444   txfm_cfg->use_intra_default_tx_only = extra_cfg->use_intra_default_tx_only;
1445   txfm_cfg->enable_tx_size_search = extra_cfg->enable_tx_size_search;
1446 
1447   // Set compound type configuration.
1448   comp_type_cfg->enable_dist_wtd_comp =
1449       extra_cfg->enable_dist_wtd_comp & extra_cfg->enable_order_hint;
1450   comp_type_cfg->enable_masked_comp = extra_cfg->enable_masked_comp;
1451   comp_type_cfg->enable_diff_wtd_comp =
1452       extra_cfg->enable_masked_comp & extra_cfg->enable_diff_wtd_comp;
1453   comp_type_cfg->enable_interinter_wedge =
1454       extra_cfg->enable_masked_comp & extra_cfg->enable_interinter_wedge;
1455   comp_type_cfg->enable_smooth_interintra =
1456       extra_cfg->enable_interintra_comp && extra_cfg->enable_smooth_interintra;
1457   comp_type_cfg->enable_interintra_wedge =
1458       extra_cfg->enable_interintra_comp & extra_cfg->enable_interintra_wedge;
1459 
1460   // Set Super-resolution mode configuration.
1461   if (extra_cfg->lossless || cfg->large_scale_tile) {
1462     disable_superres(superres_cfg);
1463   } else {
1464     superres_cfg->superres_mode = cfg->rc_superres_mode;
1465     superres_cfg->superres_scale_denominator =
1466         (uint8_t)cfg->rc_superres_denominator;
1467     superres_cfg->superres_kf_scale_denominator =
1468         (uint8_t)cfg->rc_superres_kf_denominator;
1469     superres_cfg->superres_qthresh =
1470         av1_quantizer_to_qindex(cfg->rc_superres_qthresh);
1471     superres_cfg->superres_kf_qthresh =
1472         av1_quantizer_to_qindex(cfg->rc_superres_kf_qthresh);
1473     if (superres_cfg->superres_mode == AOM_SUPERRES_FIXED &&
1474         superres_cfg->superres_scale_denominator == SCALE_NUMERATOR &&
1475         superres_cfg->superres_kf_scale_denominator == SCALE_NUMERATOR) {
1476       disable_superres(superres_cfg);
1477     }
1478     if (superres_cfg->superres_mode == AOM_SUPERRES_QTHRESH &&
1479         superres_cfg->superres_qthresh == 255 &&
1480         superres_cfg->superres_kf_qthresh == 255) {
1481       disable_superres(superres_cfg);
1482     }
1483   }
1484 
1485   superres_cfg->enable_superres =
1486       (superres_cfg->superres_mode != AOM_SUPERRES_NONE) &&
1487       extra_cfg->enable_superres;
1488   if (!superres_cfg->enable_superres) {
1489     disable_superres(superres_cfg);
1490   }
1491 
1492   if (input_cfg->limit == 1) {
1493     // still picture mode, display model and timing is meaningless
1494     dec_model_cfg->display_model_info_present_flag = 0;
1495     dec_model_cfg->timing_info_present = 0;
1496   }
1497 
1498   oxcf->save_as_annexb = cfg->save_as_annexb;
1499 
1500   // Set unit test related configuration.
1501   oxcf->unit_test_cfg.motion_vector_unit_test =
1502       extra_cfg->motion_vector_unit_test;
1503   oxcf->unit_test_cfg.sb_multipass_unit_test =
1504       extra_cfg->sb_multipass_unit_test;
1505 
1506   oxcf->border_in_pixels =
1507       av1_get_enc_border_size(av1_is_resize_needed(oxcf),
1508                               (oxcf->kf_cfg.key_freq_max == 0), BLOCK_128X128);
1509   memcpy(oxcf->target_seq_level_idx, extra_cfg->target_seq_level_idx,
1510          sizeof(oxcf->target_seq_level_idx));
1511   oxcf->tier_mask = extra_cfg->tier_mask;
1512 
1513   oxcf->partition_info_path = extra_cfg->partition_info_path;
1514 
1515   oxcf->enable_rate_guide_deltaq = extra_cfg->enable_rate_guide_deltaq;
1516   oxcf->rate_distribution_info = extra_cfg->rate_distribution_info;
1517 
1518   oxcf->strict_level_conformance = extra_cfg->strict_level_conformance;
1519 
1520   oxcf->kf_max_pyr_height = extra_cfg->kf_max_pyr_height;
1521 
1522   oxcf->sb_qp_sweep = extra_cfg->sb_qp_sweep;
1523 }
1524 
av1_get_encoder_config(const aom_codec_enc_cfg_t * cfg)1525 AV1EncoderConfig av1_get_encoder_config(const aom_codec_enc_cfg_t *cfg) {
1526   AV1EncoderConfig oxcf;
1527   struct av1_extracfg extra_cfg = default_extra_cfg[0];
1528   set_encoder_config(&oxcf, cfg, &extra_cfg);
1529   return oxcf;
1530 }
1531 
encoder_set_config(aom_codec_alg_priv_t * ctx,const aom_codec_enc_cfg_t * cfg)1532 static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
1533                                           const aom_codec_enc_cfg_t *cfg) {
1534   aom_codec_err_t res;
1535   int force_key = 0;
1536 
1537   if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
1538     if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
1539       ERROR("Cannot change width or height after initialization");
1540     // Note: function encoder_set_config() is allowed to be called multiple
1541     // times. However, when the original frame width or height is less than two
1542     // times of the new frame width or height, a forced key frame should be
1543     // used. To make sure the correct detection of a forced key frame, we need
1544     // to update the frame width and height only when the actual encoding is
1545     // performed. cpi->last_coded_width and cpi->last_coded_height are used to
1546     // track the actual coded frame size.
1547     if (ctx->ppi->cpi->last_coded_width && ctx->ppi->cpi->last_coded_height &&
1548         (!valid_ref_frame_size(ctx->ppi->cpi->last_coded_width,
1549                                ctx->ppi->cpi->last_coded_height, cfg->g_w,
1550                                cfg->g_h) ||
1551          ((int)cfg->g_w > ctx->ppi->cpi->last_coded_width) ||
1552          ((int)cfg->g_h > ctx->ppi->cpi->last_coded_height))) {
1553       force_key = 1;
1554     }
1555   }
1556 
1557   if (ctx->monochrome_on_init && cfg->monochrome == 0) {
1558     // TODO(aomedia:3465): Allow this case to work without requiring re-init
1559     // of encoder.
1560     ERROR("Cannot change to monochrome = 0 after init with monochrome");
1561   }
1562 
1563   // Prevent increasing lag_in_frames. This check is stricter than it needs
1564   // to be -- the limit is not increasing past the first lag_in_frames
1565   // value, but we don't track the initial config, only the last successful
1566   // config.
1567   if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
1568     ERROR("Cannot increase lag_in_frames");
1569   // Prevent changing lag_in_frames if Lookahead Processing is enabled
1570   if (cfg->g_lag_in_frames != ctx->cfg.g_lag_in_frames &&
1571       ctx->num_lap_buffers > 0)
1572     ERROR("Cannot change lag_in_frames if LAP is enabled");
1573 
1574   res = validate_config(ctx, cfg, &ctx->extra_cfg);
1575 
1576   if (res == AOM_CODEC_OK) {
1577     ctx->cfg = *cfg;
1578     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
1579     // On profile change, request a key frame
1580     force_key |= ctx->ppi->seq_params.profile != ctx->oxcf.profile;
1581     bool is_sb_size_changed = false;
1582     av1_change_config_seq(ctx->ppi, &ctx->oxcf, &is_sb_size_changed);
1583     for (int i = 0; i < ctx->ppi->num_fp_contexts; i++) {
1584       av1_change_config(ctx->ppi->parallel_cpi[i], &ctx->oxcf,
1585                         is_sb_size_changed);
1586     }
1587     if (ctx->ppi->cpi_lap != NULL) {
1588       av1_change_config(ctx->ppi->cpi_lap, &ctx->oxcf, is_sb_size_changed);
1589     }
1590   }
1591 
1592   if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
1593 
1594   return res;
1595 }
1596 
encoder_get_global_headers(aom_codec_alg_priv_t * ctx)1597 static aom_fixed_buf_t *encoder_get_global_headers(aom_codec_alg_priv_t *ctx) {
1598   return av1_get_global_headers(ctx->ppi);
1599 }
1600 
ctrl_get_quantizer(aom_codec_alg_priv_t * ctx,va_list args)1601 static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
1602                                           va_list args) {
1603   int *const arg = va_arg(args, int *);
1604   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1605   *arg = av1_get_quantizer(ctx->ppi->cpi);
1606   return AOM_CODEC_OK;
1607 }
1608 
ctrl_get_quantizer64(aom_codec_alg_priv_t * ctx,va_list args)1609 static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
1610                                             va_list args) {
1611   int *const arg = va_arg(args, int *);
1612   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1613   *arg = av1_qindex_to_quantizer(av1_get_quantizer(ctx->ppi->cpi));
1614   return AOM_CODEC_OK;
1615 }
1616 
ctrl_get_loopfilter_level(aom_codec_alg_priv_t * ctx,va_list args)1617 static aom_codec_err_t ctrl_get_loopfilter_level(aom_codec_alg_priv_t *ctx,
1618                                                  va_list args) {
1619   int *const arg = va_arg(args, int *);
1620   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1621   *arg = ctx->ppi->cpi->common.lf.filter_level[0];
1622   return AOM_CODEC_OK;
1623 }
1624 
ctrl_get_baseline_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)1625 static aom_codec_err_t ctrl_get_baseline_gf_interval(aom_codec_alg_priv_t *ctx,
1626                                                      va_list args) {
1627   int *const arg = va_arg(args, int *);
1628   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1629   *arg = ctx->ppi->p_rc.baseline_gf_interval;
1630   return AOM_CODEC_OK;
1631 }
1632 
update_encoder_cfg(aom_codec_alg_priv_t * ctx)1633 static aom_codec_err_t update_encoder_cfg(aom_codec_alg_priv_t *ctx) {
1634   set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
1635   av1_check_fpmt_config(ctx->ppi, &ctx->oxcf);
1636   bool is_sb_size_changed = false;
1637   av1_change_config_seq(ctx->ppi, &ctx->oxcf, &is_sb_size_changed);
1638   for (int i = 0; i < ctx->ppi->num_fp_contexts; i++) {
1639     AV1_COMP *const cpi = ctx->ppi->parallel_cpi[i];
1640     struct aom_internal_error_info *const error = cpi->common.error;
1641     if (setjmp(error->jmp)) {
1642       error->setjmp = 0;
1643       return error->error_code;
1644     }
1645     error->setjmp = 1;
1646     av1_change_config(cpi, &ctx->oxcf, is_sb_size_changed);
1647     error->setjmp = 0;
1648   }
1649   if (ctx->ppi->cpi_lap != NULL) {
1650     AV1_COMP *const cpi_lap = ctx->ppi->cpi_lap;
1651     struct aom_internal_error_info *const error = cpi_lap->common.error;
1652     if (setjmp(error->jmp)) {
1653       error->setjmp = 0;
1654       return error->error_code;
1655     }
1656     error->setjmp = 1;
1657     av1_change_config(cpi_lap, &ctx->oxcf, is_sb_size_changed);
1658     error->setjmp = 0;
1659   }
1660   return AOM_CODEC_OK;
1661 }
1662 
update_extra_cfg(aom_codec_alg_priv_t * ctx,const struct av1_extracfg * extra_cfg)1663 static aom_codec_err_t update_extra_cfg(aom_codec_alg_priv_t *ctx,
1664                                         const struct av1_extracfg *extra_cfg) {
1665   const aom_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
1666   if (res == AOM_CODEC_OK) {
1667     ctx->extra_cfg = *extra_cfg;
1668     return update_encoder_cfg(ctx);
1669   }
1670   return res;
1671 }
1672 
ctrl_set_cpuused(aom_codec_alg_priv_t * ctx,va_list args)1673 static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
1674                                         va_list args) {
1675   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1676   extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
1677   return update_extra_cfg(ctx, &extra_cfg);
1678 }
1679 
ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t * ctx,va_list args)1680 static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
1681                                                     va_list args) {
1682   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1683   extra_cfg.enable_auto_alt_ref = CAST(AOME_SET_ENABLEAUTOALTREF, args);
1684   return update_extra_cfg(ctx, &extra_cfg);
1685 }
1686 
ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t * ctx,va_list args)1687 static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
1688                                                     va_list args) {
1689   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1690   extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
1691   return update_extra_cfg(ctx, &extra_cfg);
1692 }
1693 
ctrl_set_noise_sensitivity(aom_codec_alg_priv_t * ctx,va_list args)1694 static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
1695                                                   va_list args) {
1696   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1697   extra_cfg.noise_sensitivity = CAST(AV1E_SET_NOISE_SENSITIVITY, args);
1698   return update_extra_cfg(ctx, &extra_cfg);
1699 }
1700 
ctrl_set_sharpness(aom_codec_alg_priv_t * ctx,va_list args)1701 static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
1702                                           va_list args) {
1703   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1704   extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
1705   return update_extra_cfg(ctx, &extra_cfg);
1706 }
1707 
ctrl_set_static_thresh(aom_codec_alg_priv_t * ctx,va_list args)1708 static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
1709                                               va_list args) {
1710   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1711   extra_cfg.static_thresh = CAST(AOME_SET_STATIC_THRESHOLD, args);
1712   return update_extra_cfg(ctx, &extra_cfg);
1713 }
1714 
ctrl_set_row_mt(aom_codec_alg_priv_t * ctx,va_list args)1715 static aom_codec_err_t ctrl_set_row_mt(aom_codec_alg_priv_t *ctx,
1716                                        va_list args) {
1717   unsigned int row_mt = CAST(AV1E_SET_ROW_MT, args);
1718   if (row_mt == ctx->extra_cfg.row_mt) return AOM_CODEC_OK;
1719   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1720   extra_cfg.row_mt = row_mt;
1721   return update_extra_cfg(ctx, &extra_cfg);
1722 }
1723 
ctrl_set_tile_columns(aom_codec_alg_priv_t * ctx,va_list args)1724 static aom_codec_err_t ctrl_set_tile_columns(aom_codec_alg_priv_t *ctx,
1725                                              va_list args) {
1726   // If the control AUTO_TILES is used (set to 1) then don't override
1727   // the tile_columns set via the AUTO_TILES control.
1728   if (ctx->extra_cfg.auto_tiles) {
1729     ERROR("AUTO_TILES is set so AV1E_SET_TILE_COLUMNS should not be called.");
1730   }
1731   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1732   unsigned int tile_columns = CAST(AV1E_SET_TILE_COLUMNS, args);
1733   if (tile_columns == extra_cfg.tile_columns) return AOM_CODEC_OK;
1734   extra_cfg.tile_columns = tile_columns;
1735   return update_extra_cfg(ctx, &extra_cfg);
1736 }
1737 
ctrl_set_tile_rows(aom_codec_alg_priv_t * ctx,va_list args)1738 static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
1739                                           va_list args) {
1740   // If the control AUTO_TILES is used (set to 1) then don't override
1741   // the tile_rows set via the AUTO_TILES control.
1742   if (ctx->extra_cfg.auto_tiles) {
1743     ERROR("AUTO_TILES is set so AV1E_SET_TILE_ROWS should not be called.");
1744   }
1745   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1746   unsigned int tile_rows = CAST(AV1E_SET_TILE_ROWS, args);
1747   if (tile_rows == extra_cfg.tile_rows) return AOM_CODEC_OK;
1748   extra_cfg.tile_rows = tile_rows;
1749   return update_extra_cfg(ctx, &extra_cfg);
1750 }
1751 
ctrl_set_enable_tpl_model(aom_codec_alg_priv_t * ctx,va_list args)1752 static aom_codec_err_t ctrl_set_enable_tpl_model(aom_codec_alg_priv_t *ctx,
1753                                                  va_list args) {
1754   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1755   const unsigned int tpl_model_arg = CAST(AV1E_SET_ENABLE_TPL_MODEL, args);
1756 #if CONFIG_REALTIME_ONLY
1757   if (tpl_model_arg) {
1758     ERROR("TPL model can't be turned on in realtime only build.");
1759   }
1760 #endif
1761   extra_cfg.enable_tpl_model = tpl_model_arg;
1762   return update_extra_cfg(ctx, &extra_cfg);
1763 }
1764 
ctrl_set_enable_keyframe_filtering(aom_codec_alg_priv_t * ctx,va_list args)1765 static aom_codec_err_t ctrl_set_enable_keyframe_filtering(
1766     aom_codec_alg_priv_t *ctx, va_list args) {
1767   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1768   extra_cfg.enable_keyframe_filtering =
1769       CAST(AV1E_SET_ENABLE_KEYFRAME_FILTERING, args);
1770   return update_extra_cfg(ctx, &extra_cfg);
1771 }
1772 
ctrl_set_arnr_max_frames(aom_codec_alg_priv_t * ctx,va_list args)1773 static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
1774                                                 va_list args) {
1775   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1776   extra_cfg.arnr_max_frames = CAST(AOME_SET_ARNR_MAXFRAMES, args);
1777   return update_extra_cfg(ctx, &extra_cfg);
1778 }
1779 
ctrl_set_arnr_strength(aom_codec_alg_priv_t * ctx,va_list args)1780 static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
1781                                               va_list args) {
1782   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1783   extra_cfg.arnr_strength = CAST(AOME_SET_ARNR_STRENGTH, args);
1784   return update_extra_cfg(ctx, &extra_cfg);
1785 }
1786 
ctrl_set_tuning(aom_codec_alg_priv_t * ctx,va_list args)1787 static aom_codec_err_t ctrl_set_tuning(aom_codec_alg_priv_t *ctx,
1788                                        va_list args) {
1789   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1790   extra_cfg.tuning = CAST(AOME_SET_TUNING, args);
1791   return update_extra_cfg(ctx, &extra_cfg);
1792 }
1793 
ctrl_set_cq_level(aom_codec_alg_priv_t * ctx,va_list args)1794 static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
1795                                          va_list args) {
1796   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1797   extra_cfg.cq_level = CAST(AOME_SET_CQ_LEVEL, args);
1798   return update_extra_cfg(ctx, &extra_cfg);
1799 }
1800 
ctrl_set_rc_max_intra_bitrate_pct(aom_codec_alg_priv_t * ctx,va_list args)1801 static aom_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
1802     aom_codec_alg_priv_t *ctx, va_list args) {
1803   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1804   extra_cfg.rc_max_intra_bitrate_pct =
1805       CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
1806   return update_extra_cfg(ctx, &extra_cfg);
1807 }
1808 
ctrl_set_rc_max_inter_bitrate_pct(aom_codec_alg_priv_t * ctx,va_list args)1809 static aom_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
1810     aom_codec_alg_priv_t *ctx, va_list args) {
1811   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1812   extra_cfg.rc_max_inter_bitrate_pct =
1813       CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
1814   return update_extra_cfg(ctx, &extra_cfg);
1815 }
1816 
ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t * ctx,va_list args)1817 static aom_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t *ctx,
1818                                                     va_list args) {
1819   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1820   extra_cfg.gf_cbr_boost_pct = CAST(AV1E_SET_GF_CBR_BOOST_PCT, args);
1821   return update_extra_cfg(ctx, &extra_cfg);
1822 }
1823 
ctrl_set_lossless(aom_codec_alg_priv_t * ctx,va_list args)1824 static aom_codec_err_t ctrl_set_lossless(aom_codec_alg_priv_t *ctx,
1825                                          va_list args) {
1826   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1827   extra_cfg.lossless = CAST(AV1E_SET_LOSSLESS, args);
1828   return update_extra_cfg(ctx, &extra_cfg);
1829 }
1830 
ctrl_set_enable_cdef(aom_codec_alg_priv_t * ctx,va_list args)1831 static aom_codec_err_t ctrl_set_enable_cdef(aom_codec_alg_priv_t *ctx,
1832                                             va_list args) {
1833   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1834   extra_cfg.enable_cdef = CAST(AV1E_SET_ENABLE_CDEF, args);
1835   return update_extra_cfg(ctx, &extra_cfg);
1836 }
1837 
ctrl_set_enable_restoration(aom_codec_alg_priv_t * ctx,va_list args)1838 static aom_codec_err_t ctrl_set_enable_restoration(aom_codec_alg_priv_t *ctx,
1839                                                    va_list args) {
1840   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1841   const unsigned int restoration_arg = CAST(AV1E_SET_ENABLE_RESTORATION, args);
1842 #if CONFIG_REALTIME_ONLY
1843   if (restoration_arg) {
1844     ERROR("Restoration can't be turned on in realtime only build.");
1845   }
1846 #endif
1847   extra_cfg.enable_restoration = restoration_arg;
1848   return update_extra_cfg(ctx, &extra_cfg);
1849 }
1850 
ctrl_set_force_video_mode(aom_codec_alg_priv_t * ctx,va_list args)1851 static aom_codec_err_t ctrl_set_force_video_mode(aom_codec_alg_priv_t *ctx,
1852                                                  va_list args) {
1853   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1854   extra_cfg.force_video_mode = CAST(AV1E_SET_FORCE_VIDEO_MODE, args);
1855   return update_extra_cfg(ctx, &extra_cfg);
1856 }
1857 
ctrl_set_enable_obmc(aom_codec_alg_priv_t * ctx,va_list args)1858 static aom_codec_err_t ctrl_set_enable_obmc(aom_codec_alg_priv_t *ctx,
1859                                             va_list args) {
1860   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1861   const unsigned int obmc_arg = CAST(AV1E_SET_ENABLE_OBMC, args);
1862 #if CONFIG_REALTIME_ONLY
1863   if (obmc_arg) {
1864     ERROR("OBMC can't be enabled in realtime only build.");
1865   }
1866 #endif
1867   extra_cfg.enable_obmc = obmc_arg;
1868   return update_extra_cfg(ctx, &extra_cfg);
1869 }
1870 
ctrl_set_disable_trellis_quant(aom_codec_alg_priv_t * ctx,va_list args)1871 static aom_codec_err_t ctrl_set_disable_trellis_quant(aom_codec_alg_priv_t *ctx,
1872                                                       va_list args) {
1873   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1874   extra_cfg.disable_trellis_quant = CAST(AV1E_SET_DISABLE_TRELLIS_QUANT, args);
1875   return update_extra_cfg(ctx, &extra_cfg);
1876 }
1877 
ctrl_set_enable_qm(aom_codec_alg_priv_t * ctx,va_list args)1878 static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
1879                                           va_list args) {
1880   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1881   extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
1882 #if !CONFIG_QUANT_MATRIX
1883   if (extra_cfg.enable_qm) {
1884     ERROR("QM can't be enabled with CONFIG_QUANT_MATRIX=0.");
1885   }
1886 #endif
1887   return update_extra_cfg(ctx, &extra_cfg);
1888 }
ctrl_set_qm_y(aom_codec_alg_priv_t * ctx,va_list args)1889 static aom_codec_err_t ctrl_set_qm_y(aom_codec_alg_priv_t *ctx, va_list args) {
1890   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1891   extra_cfg.qm_y = CAST(AV1E_SET_QM_Y, args);
1892   return update_extra_cfg(ctx, &extra_cfg);
1893 }
ctrl_set_qm_u(aom_codec_alg_priv_t * ctx,va_list args)1894 static aom_codec_err_t ctrl_set_qm_u(aom_codec_alg_priv_t *ctx, va_list args) {
1895   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1896   extra_cfg.qm_u = CAST(AV1E_SET_QM_U, args);
1897   return update_extra_cfg(ctx, &extra_cfg);
1898 }
ctrl_set_qm_v(aom_codec_alg_priv_t * ctx,va_list args)1899 static aom_codec_err_t ctrl_set_qm_v(aom_codec_alg_priv_t *ctx, va_list args) {
1900   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1901   extra_cfg.qm_v = CAST(AV1E_SET_QM_V, args);
1902   return update_extra_cfg(ctx, &extra_cfg);
1903 }
ctrl_set_qm_min(aom_codec_alg_priv_t * ctx,va_list args)1904 static aom_codec_err_t ctrl_set_qm_min(aom_codec_alg_priv_t *ctx,
1905                                        va_list args) {
1906   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1907   extra_cfg.qm_min = CAST(AV1E_SET_QM_MIN, args);
1908   return update_extra_cfg(ctx, &extra_cfg);
1909 }
1910 
ctrl_set_qm_max(aom_codec_alg_priv_t * ctx,va_list args)1911 static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
1912                                        va_list args) {
1913   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1914   extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
1915   return update_extra_cfg(ctx, &extra_cfg);
1916 }
1917 
ctrl_set_num_tg(aom_codec_alg_priv_t * ctx,va_list args)1918 static aom_codec_err_t ctrl_set_num_tg(aom_codec_alg_priv_t *ctx,
1919                                        va_list args) {
1920   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1921   extra_cfg.num_tg = CAST(AV1E_SET_NUM_TG, args);
1922   return update_extra_cfg(ctx, &extra_cfg);
1923 }
1924 
ctrl_set_mtu(aom_codec_alg_priv_t * ctx,va_list args)1925 static aom_codec_err_t ctrl_set_mtu(aom_codec_alg_priv_t *ctx, va_list args) {
1926   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1927   extra_cfg.mtu_size = CAST(AV1E_SET_MTU, args);
1928   return update_extra_cfg(ctx, &extra_cfg);
1929 }
ctrl_set_timing_info_type(aom_codec_alg_priv_t * ctx,va_list args)1930 static aom_codec_err_t ctrl_set_timing_info_type(aom_codec_alg_priv_t *ctx,
1931                                                  va_list args) {
1932   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1933   extra_cfg.timing_info_type = CAST(AV1E_SET_TIMING_INFO_TYPE, args);
1934   return update_extra_cfg(ctx, &extra_cfg);
1935 }
1936 
ctrl_set_enable_dual_filter(aom_codec_alg_priv_t * ctx,va_list args)1937 static aom_codec_err_t ctrl_set_enable_dual_filter(aom_codec_alg_priv_t *ctx,
1938                                                    va_list args) {
1939   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1940   extra_cfg.enable_dual_filter = CAST(AV1E_SET_ENABLE_DUAL_FILTER, args);
1941   return update_extra_cfg(ctx, &extra_cfg);
1942 }
1943 
ctrl_set_enable_chroma_deltaq(aom_codec_alg_priv_t * ctx,va_list args)1944 static aom_codec_err_t ctrl_set_enable_chroma_deltaq(aom_codec_alg_priv_t *ctx,
1945                                                      va_list args) {
1946   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1947   extra_cfg.enable_chroma_deltaq = CAST(AV1E_SET_ENABLE_CHROMA_DELTAQ, args);
1948   return update_extra_cfg(ctx, &extra_cfg);
1949 }
1950 
ctrl_set_enable_rect_partitions(aom_codec_alg_priv_t * ctx,va_list args)1951 static aom_codec_err_t ctrl_set_enable_rect_partitions(
1952     aom_codec_alg_priv_t *ctx, va_list args) {
1953   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1954   extra_cfg.enable_rect_partitions =
1955       CAST(AV1E_SET_ENABLE_RECT_PARTITIONS, args);
1956   return update_extra_cfg(ctx, &extra_cfg);
1957 }
1958 
ctrl_set_enable_ab_partitions(aom_codec_alg_priv_t * ctx,va_list args)1959 static aom_codec_err_t ctrl_set_enable_ab_partitions(aom_codec_alg_priv_t *ctx,
1960                                                      va_list args) {
1961   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1962   extra_cfg.enable_ab_partitions = CAST(AV1E_SET_ENABLE_AB_PARTITIONS, args);
1963   return update_extra_cfg(ctx, &extra_cfg);
1964 }
1965 
ctrl_set_enable_1to4_partitions(aom_codec_alg_priv_t * ctx,va_list args)1966 static aom_codec_err_t ctrl_set_enable_1to4_partitions(
1967     aom_codec_alg_priv_t *ctx, va_list args) {
1968   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1969   extra_cfg.enable_1to4_partitions =
1970       CAST(AV1E_SET_ENABLE_1TO4_PARTITIONS, args);
1971   return update_extra_cfg(ctx, &extra_cfg);
1972 }
1973 
ctrl_set_min_partition_size(aom_codec_alg_priv_t * ctx,va_list args)1974 static aom_codec_err_t ctrl_set_min_partition_size(aom_codec_alg_priv_t *ctx,
1975                                                    va_list args) {
1976   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1977   extra_cfg.min_partition_size = CAST(AV1E_SET_MIN_PARTITION_SIZE, args);
1978   return update_extra_cfg(ctx, &extra_cfg);
1979 }
1980 
ctrl_set_max_partition_size(aom_codec_alg_priv_t * ctx,va_list args)1981 static aom_codec_err_t ctrl_set_max_partition_size(aom_codec_alg_priv_t *ctx,
1982                                                    va_list args) {
1983   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1984   extra_cfg.max_partition_size = CAST(AV1E_SET_MAX_PARTITION_SIZE, args);
1985   return update_extra_cfg(ctx, &extra_cfg);
1986 }
1987 
ctrl_set_enable_intra_edge_filter(aom_codec_alg_priv_t * ctx,va_list args)1988 static aom_codec_err_t ctrl_set_enable_intra_edge_filter(
1989     aom_codec_alg_priv_t *ctx, va_list args) {
1990   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1991   extra_cfg.enable_intra_edge_filter =
1992       CAST(AV1E_SET_ENABLE_INTRA_EDGE_FILTER, args);
1993   return update_extra_cfg(ctx, &extra_cfg);
1994 }
1995 
ctrl_set_enable_order_hint(aom_codec_alg_priv_t * ctx,va_list args)1996 static aom_codec_err_t ctrl_set_enable_order_hint(aom_codec_alg_priv_t *ctx,
1997                                                   va_list args) {
1998   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1999   extra_cfg.enable_order_hint = CAST(AV1E_SET_ENABLE_ORDER_HINT, args);
2000   return update_extra_cfg(ctx, &extra_cfg);
2001 }
2002 
ctrl_set_enable_tx64(aom_codec_alg_priv_t * ctx,va_list args)2003 static aom_codec_err_t ctrl_set_enable_tx64(aom_codec_alg_priv_t *ctx,
2004                                             va_list args) {
2005   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2006   extra_cfg.enable_tx64 = CAST(AV1E_SET_ENABLE_TX64, args);
2007   return update_extra_cfg(ctx, &extra_cfg);
2008 }
2009 
ctrl_set_enable_flip_idtx(aom_codec_alg_priv_t * ctx,va_list args)2010 static aom_codec_err_t ctrl_set_enable_flip_idtx(aom_codec_alg_priv_t *ctx,
2011                                                  va_list args) {
2012   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2013   extra_cfg.enable_flip_idtx = CAST(AV1E_SET_ENABLE_FLIP_IDTX, args);
2014   return update_extra_cfg(ctx, &extra_cfg);
2015 }
2016 
ctrl_set_enable_rect_tx(aom_codec_alg_priv_t * ctx,va_list args)2017 static aom_codec_err_t ctrl_set_enable_rect_tx(aom_codec_alg_priv_t *ctx,
2018                                                va_list args) {
2019   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2020   extra_cfg.enable_rect_tx = CAST(AV1E_SET_ENABLE_RECT_TX, args);
2021   return update_extra_cfg(ctx, &extra_cfg);
2022 }
2023 
ctrl_set_enable_dist_wtd_comp(aom_codec_alg_priv_t * ctx,va_list args)2024 static aom_codec_err_t ctrl_set_enable_dist_wtd_comp(aom_codec_alg_priv_t *ctx,
2025                                                      va_list args) {
2026   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2027   extra_cfg.enable_dist_wtd_comp = CAST(AV1E_SET_ENABLE_DIST_WTD_COMP, args);
2028   return update_extra_cfg(ctx, &extra_cfg);
2029 }
2030 
ctrl_set_max_reference_frames(aom_codec_alg_priv_t * ctx,va_list args)2031 static aom_codec_err_t ctrl_set_max_reference_frames(aom_codec_alg_priv_t *ctx,
2032                                                      va_list args) {
2033   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2034   extra_cfg.max_reference_frames = CAST(AV1E_SET_MAX_REFERENCE_FRAMES, args);
2035   return update_extra_cfg(ctx, &extra_cfg);
2036 }
2037 
ctrl_set_enable_reduced_reference_set(aom_codec_alg_priv_t * ctx,va_list args)2038 static aom_codec_err_t ctrl_set_enable_reduced_reference_set(
2039     aom_codec_alg_priv_t *ctx, va_list args) {
2040   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2041   extra_cfg.enable_reduced_reference_set =
2042       CAST(AV1E_SET_REDUCED_REFERENCE_SET, args);
2043   return update_extra_cfg(ctx, &extra_cfg);
2044 }
2045 
ctrl_set_enable_ref_frame_mvs(aom_codec_alg_priv_t * ctx,va_list args)2046 static aom_codec_err_t ctrl_set_enable_ref_frame_mvs(aom_codec_alg_priv_t *ctx,
2047                                                      va_list args) {
2048   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2049   extra_cfg.enable_ref_frame_mvs = CAST(AV1E_SET_ENABLE_REF_FRAME_MVS, args);
2050   return update_extra_cfg(ctx, &extra_cfg);
2051 }
2052 
ctrl_set_allow_ref_frame_mvs(aom_codec_alg_priv_t * ctx,va_list args)2053 static aom_codec_err_t ctrl_set_allow_ref_frame_mvs(aom_codec_alg_priv_t *ctx,
2054                                                     va_list args) {
2055   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2056   extra_cfg.allow_ref_frame_mvs = CAST(AV1E_SET_ALLOW_REF_FRAME_MVS, args);
2057   return update_extra_cfg(ctx, &extra_cfg);
2058 }
2059 
ctrl_set_enable_masked_comp(aom_codec_alg_priv_t * ctx,va_list args)2060 static aom_codec_err_t ctrl_set_enable_masked_comp(aom_codec_alg_priv_t *ctx,
2061                                                    va_list args) {
2062   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2063   extra_cfg.enable_masked_comp = CAST(AV1E_SET_ENABLE_MASKED_COMP, args);
2064   return update_extra_cfg(ctx, &extra_cfg);
2065 }
2066 
ctrl_set_enable_onesided_comp(aom_codec_alg_priv_t * ctx,va_list args)2067 static aom_codec_err_t ctrl_set_enable_onesided_comp(aom_codec_alg_priv_t *ctx,
2068                                                      va_list args) {
2069   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2070   extra_cfg.enable_onesided_comp = CAST(AV1E_SET_ENABLE_ONESIDED_COMP, args);
2071   return update_extra_cfg(ctx, &extra_cfg);
2072 }
2073 
ctrl_set_enable_interintra_comp(aom_codec_alg_priv_t * ctx,va_list args)2074 static aom_codec_err_t ctrl_set_enable_interintra_comp(
2075     aom_codec_alg_priv_t *ctx, va_list args) {
2076   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2077   extra_cfg.enable_interintra_comp =
2078       CAST(AV1E_SET_ENABLE_INTERINTRA_COMP, args);
2079   return update_extra_cfg(ctx, &extra_cfg);
2080 }
2081 
ctrl_set_enable_smooth_interintra(aom_codec_alg_priv_t * ctx,va_list args)2082 static aom_codec_err_t ctrl_set_enable_smooth_interintra(
2083     aom_codec_alg_priv_t *ctx, va_list args) {
2084   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2085   extra_cfg.enable_smooth_interintra =
2086       CAST(AV1E_SET_ENABLE_SMOOTH_INTERINTRA, args);
2087   return update_extra_cfg(ctx, &extra_cfg);
2088 }
2089 
ctrl_set_enable_diff_wtd_comp(aom_codec_alg_priv_t * ctx,va_list args)2090 static aom_codec_err_t ctrl_set_enable_diff_wtd_comp(aom_codec_alg_priv_t *ctx,
2091                                                      va_list args) {
2092   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2093   extra_cfg.enable_diff_wtd_comp = CAST(AV1E_SET_ENABLE_DIFF_WTD_COMP, args);
2094   return update_extra_cfg(ctx, &extra_cfg);
2095 }
2096 
ctrl_set_enable_interinter_wedge(aom_codec_alg_priv_t * ctx,va_list args)2097 static aom_codec_err_t ctrl_set_enable_interinter_wedge(
2098     aom_codec_alg_priv_t *ctx, va_list args) {
2099   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2100   extra_cfg.enable_interinter_wedge =
2101       CAST(AV1E_SET_ENABLE_INTERINTER_WEDGE, args);
2102   return update_extra_cfg(ctx, &extra_cfg);
2103 }
2104 
ctrl_set_enable_interintra_wedge(aom_codec_alg_priv_t * ctx,va_list args)2105 static aom_codec_err_t ctrl_set_enable_interintra_wedge(
2106     aom_codec_alg_priv_t *ctx, va_list args) {
2107   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2108   extra_cfg.enable_interintra_wedge =
2109       CAST(AV1E_SET_ENABLE_INTERINTRA_WEDGE, args);
2110   return update_extra_cfg(ctx, &extra_cfg);
2111 }
2112 
ctrl_set_enable_global_motion(aom_codec_alg_priv_t * ctx,va_list args)2113 static aom_codec_err_t ctrl_set_enable_global_motion(aom_codec_alg_priv_t *ctx,
2114                                                      va_list args) {
2115   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2116   const int global_motion_arg = CAST(AV1E_SET_ENABLE_GLOBAL_MOTION, args);
2117 #if CONFIG_REALTIME_ONLY
2118   if (global_motion_arg) {
2119     ERROR("Global motion can't be enabled in realtime only build.");
2120   }
2121 #endif
2122   extra_cfg.enable_global_motion = global_motion_arg;
2123   return update_extra_cfg(ctx, &extra_cfg);
2124 }
2125 
ctrl_set_enable_warped_motion(aom_codec_alg_priv_t * ctx,va_list args)2126 static aom_codec_err_t ctrl_set_enable_warped_motion(aom_codec_alg_priv_t *ctx,
2127                                                      va_list args) {
2128   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2129   const int warped_motion_arg = CAST(AV1E_SET_ENABLE_WARPED_MOTION, args);
2130 #if CONFIG_REALTIME_ONLY
2131   if (warped_motion_arg) {
2132     ERROR("Warped motion can't be enabled in realtime only build.");
2133   }
2134 #endif
2135   extra_cfg.enable_warped_motion = warped_motion_arg;
2136   return update_extra_cfg(ctx, &extra_cfg);
2137 }
2138 
ctrl_set_allow_warped_motion(aom_codec_alg_priv_t * ctx,va_list args)2139 static aom_codec_err_t ctrl_set_allow_warped_motion(aom_codec_alg_priv_t *ctx,
2140                                                     va_list args) {
2141   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2142   extra_cfg.allow_warped_motion = CAST(AV1E_SET_ALLOW_WARPED_MOTION, args);
2143   return update_extra_cfg(ctx, &extra_cfg);
2144 }
2145 
ctrl_set_enable_filter_intra(aom_codec_alg_priv_t * ctx,va_list args)2146 static aom_codec_err_t ctrl_set_enable_filter_intra(aom_codec_alg_priv_t *ctx,
2147                                                     va_list args) {
2148   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2149   extra_cfg.enable_filter_intra = CAST(AV1E_SET_ENABLE_FILTER_INTRA, args);
2150   return update_extra_cfg(ctx, &extra_cfg);
2151 }
2152 
ctrl_set_enable_smooth_intra(aom_codec_alg_priv_t * ctx,va_list args)2153 static aom_codec_err_t ctrl_set_enable_smooth_intra(aom_codec_alg_priv_t *ctx,
2154                                                     va_list args) {
2155   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2156   extra_cfg.enable_smooth_intra = CAST(AV1E_SET_ENABLE_SMOOTH_INTRA, args);
2157   return update_extra_cfg(ctx, &extra_cfg);
2158 }
2159 
ctrl_set_enable_directional_intra(aom_codec_alg_priv_t * ctx,va_list args)2160 static aom_codec_err_t ctrl_set_enable_directional_intra(
2161     aom_codec_alg_priv_t *ctx, va_list args) {
2162   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2163   extra_cfg.enable_directional_intra =
2164       CAST(AV1E_SET_ENABLE_DIRECTIONAL_INTRA, args);
2165   return update_extra_cfg(ctx, &extra_cfg);
2166 }
2167 
ctrl_set_enable_diagonal_intra(aom_codec_alg_priv_t * ctx,va_list args)2168 static aom_codec_err_t ctrl_set_enable_diagonal_intra(aom_codec_alg_priv_t *ctx,
2169                                                       va_list args) {
2170   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2171   extra_cfg.enable_diagonal_intra = CAST(AV1E_SET_ENABLE_DIAGONAL_INTRA, args);
2172   return update_extra_cfg(ctx, &extra_cfg);
2173 }
2174 
ctrl_set_enable_paeth_intra(aom_codec_alg_priv_t * ctx,va_list args)2175 static aom_codec_err_t ctrl_set_enable_paeth_intra(aom_codec_alg_priv_t *ctx,
2176                                                    va_list args) {
2177   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2178   extra_cfg.enable_paeth_intra = CAST(AV1E_SET_ENABLE_PAETH_INTRA, args);
2179   return update_extra_cfg(ctx, &extra_cfg);
2180 }
2181 
ctrl_set_enable_cfl_intra(aom_codec_alg_priv_t * ctx,va_list args)2182 static aom_codec_err_t ctrl_set_enable_cfl_intra(aom_codec_alg_priv_t *ctx,
2183                                                  va_list args) {
2184   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2185   extra_cfg.enable_cfl_intra = CAST(AV1E_SET_ENABLE_CFL_INTRA, args);
2186 #if CONFIG_REALTIME_ONLY
2187   if (extra_cfg.enable_cfl_intra) {
2188     ERROR("cfl can't be turned on in realtime only build.");
2189   }
2190 #endif
2191   return update_extra_cfg(ctx, &extra_cfg);
2192 }
2193 
ctrl_set_enable_superres(aom_codec_alg_priv_t * ctx,va_list args)2194 static aom_codec_err_t ctrl_set_enable_superres(aom_codec_alg_priv_t *ctx,
2195                                                 va_list args) {
2196   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2197   extra_cfg.enable_superres = CAST(AV1E_SET_ENABLE_SUPERRES, args);
2198   return update_extra_cfg(ctx, &extra_cfg);
2199 }
2200 
ctrl_set_enable_overlay(aom_codec_alg_priv_t * ctx,va_list args)2201 static aom_codec_err_t ctrl_set_enable_overlay(aom_codec_alg_priv_t *ctx,
2202                                                va_list args) {
2203   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2204   extra_cfg.enable_overlay = CAST(AV1E_SET_ENABLE_OVERLAY, args);
2205   return update_extra_cfg(ctx, &extra_cfg);
2206 }
2207 
ctrl_set_enable_palette(aom_codec_alg_priv_t * ctx,va_list args)2208 static aom_codec_err_t ctrl_set_enable_palette(aom_codec_alg_priv_t *ctx,
2209                                                va_list args) {
2210   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2211   extra_cfg.enable_palette = CAST(AV1E_SET_ENABLE_PALETTE, args);
2212   return update_extra_cfg(ctx, &extra_cfg);
2213 }
2214 
ctrl_set_enable_intrabc(aom_codec_alg_priv_t * ctx,va_list args)2215 static aom_codec_err_t ctrl_set_enable_intrabc(aom_codec_alg_priv_t *ctx,
2216                                                va_list args) {
2217   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2218   extra_cfg.enable_intrabc = CAST(AV1E_SET_ENABLE_INTRABC, args);
2219   return update_extra_cfg(ctx, &extra_cfg);
2220 }
2221 
ctrl_set_enable_angle_delta(aom_codec_alg_priv_t * ctx,va_list args)2222 static aom_codec_err_t ctrl_set_enable_angle_delta(aom_codec_alg_priv_t *ctx,
2223                                                    va_list args) {
2224   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2225   extra_cfg.enable_angle_delta = CAST(AV1E_SET_ENABLE_ANGLE_DELTA, args);
2226   return update_extra_cfg(ctx, &extra_cfg);
2227 }
2228 
ctrl_set_error_resilient_mode(aom_codec_alg_priv_t * ctx,va_list args)2229 static aom_codec_err_t ctrl_set_error_resilient_mode(aom_codec_alg_priv_t *ctx,
2230                                                      va_list args) {
2231   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2232   extra_cfg.error_resilient_mode = CAST(AV1E_SET_ERROR_RESILIENT_MODE, args);
2233   return update_extra_cfg(ctx, &extra_cfg);
2234 }
2235 
ctrl_set_s_frame_mode(aom_codec_alg_priv_t * ctx,va_list args)2236 static aom_codec_err_t ctrl_set_s_frame_mode(aom_codec_alg_priv_t *ctx,
2237                                              va_list args) {
2238   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2239   extra_cfg.s_frame_mode = CAST(AV1E_SET_S_FRAME_MODE, args);
2240   return update_extra_cfg(ctx, &extra_cfg);
2241 }
2242 
ctrl_set_frame_parallel_decoding_mode(aom_codec_alg_priv_t * ctx,va_list args)2243 static aom_codec_err_t ctrl_set_frame_parallel_decoding_mode(
2244     aom_codec_alg_priv_t *ctx, va_list args) {
2245   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2246   extra_cfg.frame_parallel_decoding_mode =
2247       CAST(AV1E_SET_FRAME_PARALLEL_DECODING, args);
2248   return update_extra_cfg(ctx, &extra_cfg);
2249 }
2250 
ctrl_set_single_tile_decoding(aom_codec_alg_priv_t * ctx,va_list args)2251 static aom_codec_err_t ctrl_set_single_tile_decoding(aom_codec_alg_priv_t *ctx,
2252                                                      va_list args) {
2253   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2254   extra_cfg.single_tile_decoding = CAST(AV1E_SET_SINGLE_TILE_DECODING, args);
2255   return update_extra_cfg(ctx, &extra_cfg);
2256 }
2257 
ctrl_set_aq_mode(aom_codec_alg_priv_t * ctx,va_list args)2258 static aom_codec_err_t ctrl_set_aq_mode(aom_codec_alg_priv_t *ctx,
2259                                         va_list args) {
2260   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2261   extra_cfg.aq_mode = CAST(AV1E_SET_AQ_MODE, args);
2262 
2263   // Skip AQ mode if using fixed QP for current frame.
2264   if (ctx->ppi->cpi->rc.use_external_qp_one_pass) extra_cfg.aq_mode = 0;
2265   return update_extra_cfg(ctx, &extra_cfg);
2266 }
2267 
ctrl_set_reduced_tx_type_set(aom_codec_alg_priv_t * ctx,va_list args)2268 static aom_codec_err_t ctrl_set_reduced_tx_type_set(aom_codec_alg_priv_t *ctx,
2269                                                     va_list args) {
2270   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2271   extra_cfg.reduced_tx_type_set = CAST(AV1E_SET_REDUCED_TX_TYPE_SET, args);
2272   return update_extra_cfg(ctx, &extra_cfg);
2273 }
2274 
ctrl_set_intra_dct_only(aom_codec_alg_priv_t * ctx,va_list args)2275 static aom_codec_err_t ctrl_set_intra_dct_only(aom_codec_alg_priv_t *ctx,
2276                                                va_list args) {
2277   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2278   extra_cfg.use_intra_dct_only = CAST(AV1E_SET_INTRA_DCT_ONLY, args);
2279   return update_extra_cfg(ctx, &extra_cfg);
2280 }
2281 
ctrl_set_inter_dct_only(aom_codec_alg_priv_t * ctx,va_list args)2282 static aom_codec_err_t ctrl_set_inter_dct_only(aom_codec_alg_priv_t *ctx,
2283                                                va_list args) {
2284   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2285   extra_cfg.use_inter_dct_only = CAST(AV1E_SET_INTER_DCT_ONLY, args);
2286   return update_extra_cfg(ctx, &extra_cfg);
2287 }
2288 
ctrl_set_intra_default_tx_only(aom_codec_alg_priv_t * ctx,va_list args)2289 static aom_codec_err_t ctrl_set_intra_default_tx_only(aom_codec_alg_priv_t *ctx,
2290                                                       va_list args) {
2291   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2292   extra_cfg.use_intra_default_tx_only =
2293       CAST(AV1E_SET_INTRA_DEFAULT_TX_ONLY, args);
2294   return update_extra_cfg(ctx, &extra_cfg);
2295 }
2296 
ctrl_set_enable_tx_size_search(aom_codec_alg_priv_t * ctx,va_list args)2297 static aom_codec_err_t ctrl_set_enable_tx_size_search(aom_codec_alg_priv_t *ctx,
2298                                                       va_list args) {
2299   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2300   extra_cfg.enable_tx_size_search = CAST(AV1E_SET_ENABLE_TX_SIZE_SEARCH, args);
2301   return update_extra_cfg(ctx, &extra_cfg);
2302 }
2303 
ctrl_set_quant_b_adapt(aom_codec_alg_priv_t * ctx,va_list args)2304 static aom_codec_err_t ctrl_set_quant_b_adapt(aom_codec_alg_priv_t *ctx,
2305                                               va_list args) {
2306 #if CONFIG_REALTIME_ONLY
2307   (void)ctx;
2308   (void)args;
2309   return AOM_CODEC_INCAPABLE;
2310 #else
2311   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2312   extra_cfg.quant_b_adapt = CAST(AV1E_SET_QUANT_B_ADAPT, args);
2313   return update_extra_cfg(ctx, &extra_cfg);
2314 #endif
2315 }
2316 
ctrl_set_vbr_corpus_complexity_lap(aom_codec_alg_priv_t * ctx,va_list args)2317 static aom_codec_err_t ctrl_set_vbr_corpus_complexity_lap(
2318     aom_codec_alg_priv_t *ctx, va_list args) {
2319   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2320   extra_cfg.vbr_corpus_complexity_lap =
2321       CAST(AV1E_SET_VBR_CORPUS_COMPLEXITY_LAP, args);
2322   return update_extra_cfg(ctx, &extra_cfg);
2323 }
ctrl_set_coeff_cost_upd_freq(aom_codec_alg_priv_t * ctx,va_list args)2324 static aom_codec_err_t ctrl_set_coeff_cost_upd_freq(aom_codec_alg_priv_t *ctx,
2325                                                     va_list args) {
2326   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2327   extra_cfg.coeff_cost_upd_freq = CAST(AV1E_SET_COEFF_COST_UPD_FREQ, args);
2328   return update_extra_cfg(ctx, &extra_cfg);
2329 }
2330 
ctrl_set_mode_cost_upd_freq(aom_codec_alg_priv_t * ctx,va_list args)2331 static aom_codec_err_t ctrl_set_mode_cost_upd_freq(aom_codec_alg_priv_t *ctx,
2332                                                    va_list args) {
2333   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2334   extra_cfg.mode_cost_upd_freq = CAST(AV1E_SET_MODE_COST_UPD_FREQ, args);
2335   return update_extra_cfg(ctx, &extra_cfg);
2336 }
2337 
ctrl_set_mv_cost_upd_freq(aom_codec_alg_priv_t * ctx,va_list args)2338 static aom_codec_err_t ctrl_set_mv_cost_upd_freq(aom_codec_alg_priv_t *ctx,
2339                                                  va_list args) {
2340   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2341   extra_cfg.mv_cost_upd_freq = CAST(AV1E_SET_MV_COST_UPD_FREQ, args);
2342   return update_extra_cfg(ctx, &extra_cfg);
2343 }
2344 
ctrl_set_dv_cost_upd_freq(aom_codec_alg_priv_t * ctx,va_list args)2345 static aom_codec_err_t ctrl_set_dv_cost_upd_freq(aom_codec_alg_priv_t *ctx,
2346                                                  va_list args) {
2347   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2348   extra_cfg.dv_cost_upd_freq = CAST(AV1E_SET_DV_COST_UPD_FREQ, args);
2349   return update_extra_cfg(ctx, &extra_cfg);
2350 }
2351 
ctrl_set_vmaf_model_path(aom_codec_alg_priv_t * ctx,va_list args)2352 static aom_codec_err_t ctrl_set_vmaf_model_path(aom_codec_alg_priv_t *ctx,
2353                                                 va_list args) {
2354   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2355   const char *str = CAST(AV1E_SET_VMAF_MODEL_PATH, args);
2356   const aom_codec_err_t ret = allocate_and_set_string(
2357       str, default_extra_cfg[0].vmaf_model_path, &extra_cfg.vmaf_model_path,
2358       ctx->ppi->error.detail);
2359   if (ret != AOM_CODEC_OK) return ret;
2360   return update_extra_cfg(ctx, &extra_cfg);
2361 }
2362 
ctrl_set_partition_info_path(aom_codec_alg_priv_t * ctx,va_list args)2363 static aom_codec_err_t ctrl_set_partition_info_path(aom_codec_alg_priv_t *ctx,
2364                                                     va_list args) {
2365   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2366   const char *str = CAST(AV1E_SET_PARTITION_INFO_PATH, args);
2367   const aom_codec_err_t ret = allocate_and_set_string(
2368       str, default_extra_cfg[0].partition_info_path,
2369       &extra_cfg.partition_info_path, ctx->ppi->error.detail);
2370   if (ret != AOM_CODEC_OK) return ret;
2371   return update_extra_cfg(ctx, &extra_cfg);
2372 }
2373 
ctrl_enable_rate_guide_deltaq(aom_codec_alg_priv_t * ctx,va_list args)2374 static aom_codec_err_t ctrl_enable_rate_guide_deltaq(aom_codec_alg_priv_t *ctx,
2375                                                      va_list args) {
2376   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2377   extra_cfg.enable_rate_guide_deltaq =
2378       CAST(AV1E_ENABLE_RATE_GUIDE_DELTAQ, args);
2379   return update_extra_cfg(ctx, &extra_cfg);
2380 }
2381 
ctrl_set_rate_distribution_info(aom_codec_alg_priv_t * ctx,va_list args)2382 static aom_codec_err_t ctrl_set_rate_distribution_info(
2383     aom_codec_alg_priv_t *ctx, va_list args) {
2384   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2385   const char *str = CAST(AV1E_SET_RATE_DISTRIBUTION_INFO, args);
2386   const aom_codec_err_t ret = allocate_and_set_string(
2387       str, default_extra_cfg[0].rate_distribution_info,
2388       &extra_cfg.rate_distribution_info, ctx->ppi->error.detail);
2389   if (ret != AOM_CODEC_OK) return ret;
2390   return update_extra_cfg(ctx, &extra_cfg);
2391 }
2392 
ctrl_set_film_grain_test_vector(aom_codec_alg_priv_t * ctx,va_list args)2393 static aom_codec_err_t ctrl_set_film_grain_test_vector(
2394     aom_codec_alg_priv_t *ctx, va_list args) {
2395   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2396   extra_cfg.film_grain_test_vector =
2397       CAST(AV1E_SET_FILM_GRAIN_TEST_VECTOR, args);
2398   return update_extra_cfg(ctx, &extra_cfg);
2399 }
2400 
ctrl_set_film_grain_table(aom_codec_alg_priv_t * ctx,va_list args)2401 static aom_codec_err_t ctrl_set_film_grain_table(aom_codec_alg_priv_t *ctx,
2402                                                  va_list args) {
2403   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2404   const char *str = CAST(AV1E_SET_FILM_GRAIN_TABLE, args);
2405   if (str == NULL) {
2406     // this parameter allows NULL as its value
2407     extra_cfg.film_grain_table_filename = str;
2408   } else {
2409 #if CONFIG_REALTIME_ONLY
2410     ERROR("film_grain removed from realtime only build.");
2411 #endif
2412     const aom_codec_err_t ret = allocate_and_set_string(
2413         str, default_extra_cfg[0].film_grain_table_filename,
2414         &extra_cfg.film_grain_table_filename, ctx->ppi->error.detail);
2415     if (ret != AOM_CODEC_OK) return ret;
2416   }
2417   return update_extra_cfg(ctx, &extra_cfg);
2418 }
2419 
ctrl_set_denoise_noise_level(aom_codec_alg_priv_t * ctx,va_list args)2420 static aom_codec_err_t ctrl_set_denoise_noise_level(aom_codec_alg_priv_t *ctx,
2421                                                     va_list args) {
2422 #if !CONFIG_DENOISE
2423   (void)ctx;
2424   (void)args;
2425   return AOM_CODEC_INCAPABLE;
2426 #else
2427   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2428   extra_cfg.noise_level =
2429       ((float)CAST(AV1E_SET_DENOISE_NOISE_LEVEL, args)) / 10.0f;
2430   return update_extra_cfg(ctx, &extra_cfg);
2431 #endif
2432 }
2433 
ctrl_set_denoise_block_size(aom_codec_alg_priv_t * ctx,va_list args)2434 static aom_codec_err_t ctrl_set_denoise_block_size(aom_codec_alg_priv_t *ctx,
2435                                                    va_list args) {
2436 #if !CONFIG_DENOISE
2437   (void)ctx;
2438   (void)args;
2439   return AOM_CODEC_INCAPABLE;
2440 #else
2441   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2442   extra_cfg.noise_block_size = CAST(AV1E_SET_DENOISE_BLOCK_SIZE, args);
2443   return update_extra_cfg(ctx, &extra_cfg);
2444 #endif
2445 }
2446 
ctrl_set_enable_dnl_denoising(aom_codec_alg_priv_t * ctx,va_list args)2447 static aom_codec_err_t ctrl_set_enable_dnl_denoising(aom_codec_alg_priv_t *ctx,
2448                                                      va_list args) {
2449 #if !CONFIG_DENOISE
2450   (void)ctx;
2451   (void)args;
2452   return AOM_CODEC_INCAPABLE;
2453 #else
2454   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2455   extra_cfg.enable_dnl_denoising = CAST(AV1E_SET_ENABLE_DNL_DENOISING, args);
2456   return update_extra_cfg(ctx, &extra_cfg);
2457 #endif
2458 }
2459 
ctrl_set_deltaq_mode(aom_codec_alg_priv_t * ctx,va_list args)2460 static aom_codec_err_t ctrl_set_deltaq_mode(aom_codec_alg_priv_t *ctx,
2461                                             va_list args) {
2462   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2463   const DELTAQ_MODE deltaq_arg = CAST(AV1E_SET_DELTAQ_MODE, args);
2464 #if CONFIG_REALTIME_ONLY
2465   if (deltaq_arg > NO_DELTA_Q) {
2466     ERROR("Delta Q mode can't be enabled in realtime only build.");
2467   }
2468 #endif
2469   extra_cfg.deltaq_mode = deltaq_arg;
2470   return update_extra_cfg(ctx, &extra_cfg);
2471 }
2472 
ctrl_set_deltaq_strength(aom_codec_alg_priv_t * ctx,va_list args)2473 static aom_codec_err_t ctrl_set_deltaq_strength(aom_codec_alg_priv_t *ctx,
2474                                                 va_list args) {
2475   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2476   extra_cfg.deltaq_strength = CAST(AV1E_SET_DELTAQ_STRENGTH, args);
2477   return update_extra_cfg(ctx, &extra_cfg);
2478 }
2479 
ctrl_set_deltalf_mode(aom_codec_alg_priv_t * ctx,va_list args)2480 static aom_codec_err_t ctrl_set_deltalf_mode(aom_codec_alg_priv_t *ctx,
2481                                              va_list args) {
2482   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2483   extra_cfg.deltalf_mode = CAST(AV1E_SET_DELTALF_MODE, args);
2484   return update_extra_cfg(ctx, &extra_cfg);
2485 }
2486 
ctrl_set_min_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)2487 static aom_codec_err_t ctrl_set_min_gf_interval(aom_codec_alg_priv_t *ctx,
2488                                                 va_list args) {
2489   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2490   extra_cfg.min_gf_interval = CAST(AV1E_SET_MIN_GF_INTERVAL, args);
2491   return update_extra_cfg(ctx, &extra_cfg);
2492 }
2493 
ctrl_set_max_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)2494 static aom_codec_err_t ctrl_set_max_gf_interval(aom_codec_alg_priv_t *ctx,
2495                                                 va_list args) {
2496   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2497   extra_cfg.max_gf_interval = CAST(AV1E_SET_MAX_GF_INTERVAL, args);
2498   return update_extra_cfg(ctx, &extra_cfg);
2499 }
2500 
ctrl_set_gf_min_pyr_height(aom_codec_alg_priv_t * ctx,va_list args)2501 static aom_codec_err_t ctrl_set_gf_min_pyr_height(aom_codec_alg_priv_t *ctx,
2502                                                   va_list args) {
2503   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2504   extra_cfg.gf_min_pyr_height = CAST(AV1E_SET_GF_MIN_PYRAMID_HEIGHT, args);
2505   return update_extra_cfg(ctx, &extra_cfg);
2506 }
2507 
ctrl_set_gf_max_pyr_height(aom_codec_alg_priv_t * ctx,va_list args)2508 static aom_codec_err_t ctrl_set_gf_max_pyr_height(aom_codec_alg_priv_t *ctx,
2509                                                   va_list args) {
2510   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2511   extra_cfg.gf_max_pyr_height = CAST(AV1E_SET_GF_MAX_PYRAMID_HEIGHT, args);
2512   return update_extra_cfg(ctx, &extra_cfg);
2513 }
2514 
ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t * ctx,va_list args)2515 static aom_codec_err_t ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t *ctx,
2516                                                      va_list args) {
2517   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2518   extra_cfg.frame_periodic_boost = CAST(AV1E_SET_FRAME_PERIODIC_BOOST, args);
2519   return update_extra_cfg(ctx, &extra_cfg);
2520 }
2521 
ctrl_enable_motion_vector_unit_test(aom_codec_alg_priv_t * ctx,va_list args)2522 static aom_codec_err_t ctrl_enable_motion_vector_unit_test(
2523     aom_codec_alg_priv_t *ctx, va_list args) {
2524   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2525   extra_cfg.motion_vector_unit_test =
2526       CAST(AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
2527   return update_extra_cfg(ctx, &extra_cfg);
2528 }
2529 
ctrl_enable_fpmt_unit_test(aom_codec_alg_priv_t * ctx,va_list args)2530 static aom_codec_err_t ctrl_enable_fpmt_unit_test(aom_codec_alg_priv_t *ctx,
2531                                                   va_list args) {
2532 #if !CONFIG_FPMT_TEST
2533   (void)args;
2534   (void)ctx;
2535   return AOM_CODEC_INCAPABLE;
2536 #else
2537   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2538   extra_cfg.fpmt_unit_test = CAST(AV1E_SET_FP_MT_UNIT_TEST, args);
2539   ctx->ppi->fpmt_unit_test_cfg = (extra_cfg.fpmt_unit_test == 1)
2540                                      ? PARALLEL_ENCODE
2541                                      : PARALLEL_SIMULATION_ENCODE;
2542   return update_extra_cfg(ctx, &extra_cfg);
2543 #endif
2544 }
2545 
ctrl_enable_ext_tile_debug(aom_codec_alg_priv_t * ctx,va_list args)2546 static aom_codec_err_t ctrl_enable_ext_tile_debug(aom_codec_alg_priv_t *ctx,
2547                                                   va_list args) {
2548   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2549   extra_cfg.ext_tile_debug = CAST(AV1E_ENABLE_EXT_TILE_DEBUG, args);
2550   return update_extra_cfg(ctx, &extra_cfg);
2551 }
2552 
ctrl_set_target_seq_level_idx(aom_codec_alg_priv_t * ctx,va_list args)2553 static aom_codec_err_t ctrl_set_target_seq_level_idx(aom_codec_alg_priv_t *ctx,
2554                                                      va_list args) {
2555   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2556   const int val = CAST(AV1E_SET_TARGET_SEQ_LEVEL_IDX, args);
2557   const int level = val % 100;
2558   const int operating_point_idx = val / 100;
2559   if (operating_point_idx < 0 ||
2560       operating_point_idx >= MAX_NUM_OPERATING_POINTS) {
2561     char *const err_string = ctx->ppi->error.detail;
2562     snprintf(err_string, ARG_ERR_MSG_MAX_LEN,
2563              "Invalid operating point index: %d", operating_point_idx);
2564     ctx->base.err_detail = err_string;
2565     return AOM_CODEC_INVALID_PARAM;
2566   }
2567   extra_cfg.target_seq_level_idx[operating_point_idx] = (AV1_LEVEL)level;
2568   return update_extra_cfg(ctx, &extra_cfg);
2569 }
2570 
ctrl_set_tier_mask(aom_codec_alg_priv_t * ctx,va_list args)2571 static aom_codec_err_t ctrl_set_tier_mask(aom_codec_alg_priv_t *ctx,
2572                                           va_list args) {
2573   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2574   extra_cfg.tier_mask = CAST(AV1E_SET_TIER_MASK, args);
2575   return update_extra_cfg(ctx, &extra_cfg);
2576 }
2577 
ctrl_set_min_cr(aom_codec_alg_priv_t * ctx,va_list args)2578 static aom_codec_err_t ctrl_set_min_cr(aom_codec_alg_priv_t *ctx,
2579                                        va_list args) {
2580   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2581   extra_cfg.min_cr = CAST(AV1E_SET_MIN_CR, args);
2582   return update_extra_cfg(ctx, &extra_cfg);
2583 }
2584 
ctrl_enable_sb_multipass_unit_test(aom_codec_alg_priv_t * ctx,va_list args)2585 static aom_codec_err_t ctrl_enable_sb_multipass_unit_test(
2586     aom_codec_alg_priv_t *ctx, va_list args) {
2587   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2588   extra_cfg.sb_multipass_unit_test =
2589       CAST(AV1E_ENABLE_SB_MULTIPASS_UNIT_TEST, args);
2590   return update_extra_cfg(ctx, &extra_cfg);
2591 }
2592 
ctrl_enable_sb_qp_sweep(aom_codec_alg_priv_t * ctx,va_list args)2593 static aom_codec_err_t ctrl_enable_sb_qp_sweep(aom_codec_alg_priv_t *ctx,
2594                                                va_list args) {
2595   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2596   extra_cfg.sb_qp_sweep = CAST(AV1E_ENABLE_SB_QP_SWEEP, args);
2597   return update_extra_cfg(ctx, &extra_cfg);
2598 }
2599 
ctrl_set_external_partition(aom_codec_alg_priv_t * ctx,va_list args)2600 static aom_codec_err_t ctrl_set_external_partition(aom_codec_alg_priv_t *ctx,
2601                                                    va_list args) {
2602   AV1_COMP *const cpi = ctx->ppi->cpi;
2603   aom_ext_part_funcs_t funcs = *CAST(AV1E_SET_EXTERNAL_PARTITION, args);
2604   aom_ext_part_config_t config;
2605   // TODO(chengchen): verify the sb_size has been set at this point.
2606   config.superblock_size = cpi->common.seq_params->sb_size;
2607   const aom_codec_err_t status =
2608       av1_ext_part_create(funcs, config, &cpi->ext_part_controller);
2609   return status;
2610 }
2611 
ctrl_set_loopfilter_control(aom_codec_alg_priv_t * ctx,va_list args)2612 static aom_codec_err_t ctrl_set_loopfilter_control(aom_codec_alg_priv_t *ctx,
2613                                                    va_list args) {
2614   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2615   extra_cfg.loopfilter_control = CAST(AV1E_SET_LOOPFILTER_CONTROL, args);
2616   return update_extra_cfg(ctx, &extra_cfg);
2617 }
2618 
ctrl_set_skip_postproc_filtering(aom_codec_alg_priv_t * ctx,va_list args)2619 static aom_codec_err_t ctrl_set_skip_postproc_filtering(
2620     aom_codec_alg_priv_t *ctx, va_list args) {
2621   // Skipping the application of post-processing filters is allowed only
2622   // for ALLINTRA mode.
2623   if (ctx->cfg.g_usage != AOM_USAGE_ALL_INTRA) return AOM_CODEC_INCAPABLE;
2624   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2625   extra_cfg.skip_postproc_filtering =
2626       CAST(AV1E_SET_SKIP_POSTPROC_FILTERING, args);
2627   return update_extra_cfg(ctx, &extra_cfg);
2628 }
2629 
ctrl_set_rtc_external_rc(aom_codec_alg_priv_t * ctx,va_list args)2630 static aom_codec_err_t ctrl_set_rtc_external_rc(aom_codec_alg_priv_t *ctx,
2631                                                 va_list args) {
2632   ctx->ppi->cpi->rc.rtc_external_ratectrl =
2633       CAST(AV1E_SET_RTC_EXTERNAL_RC, args);
2634   return AOM_CODEC_OK;
2635 }
2636 
ctrl_set_quantizer_one_pass(aom_codec_alg_priv_t * ctx,va_list args)2637 static aom_codec_err_t ctrl_set_quantizer_one_pass(aom_codec_alg_priv_t *ctx,
2638                                                    va_list args) {
2639   const int qp = CAST(AV1E_SET_QUANTIZER_ONE_PASS, args);
2640 
2641   if (qp < 0 || qp > 63) return AOM_CODEC_INVALID_PARAM;
2642 
2643   aom_codec_enc_cfg_t *cfg = &ctx->cfg;
2644   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2645   cfg->rc_min_quantizer = cfg->rc_max_quantizer = qp;
2646   extra_cfg.aq_mode = 0;
2647   ctx->ppi->cpi->rc.use_external_qp_one_pass = 1;
2648 
2649   return update_extra_cfg(ctx, &extra_cfg);
2650 }
2651 
ctrl_set_bitrate_one_pass_cbr(aom_codec_alg_priv_t * ctx,va_list args)2652 static aom_codec_err_t ctrl_set_bitrate_one_pass_cbr(aom_codec_alg_priv_t *ctx,
2653                                                      va_list args) {
2654   AV1_PRIMARY *const ppi = ctx->ppi;
2655   AV1_COMP *const cpi = ppi->cpi;
2656   AV1EncoderConfig *oxcf = &cpi->oxcf;
2657   if (!is_one_pass_rt_params(cpi) || oxcf->rc_cfg.mode != AOM_CBR ||
2658       cpi->ppi->use_svc || ppi->num_fp_contexts != 1 || ppi->cpi_lap != NULL) {
2659     return AOM_CODEC_INVALID_PARAM;
2660   }
2661   const int new_bitrate = CAST(AV1E_SET_BITRATE_ONE_PASS_CBR, args);
2662   ctx->cfg.rc_target_bitrate = new_bitrate;
2663   oxcf->rc_cfg.target_bandwidth = new_bitrate * 1000;
2664   set_primary_rc_buffer_sizes(oxcf, ppi);
2665   av1_new_framerate(cpi, cpi->framerate);
2666   check_reset_rc_flag(cpi);
2667   return AOM_CODEC_OK;
2668 }
2669 
ctrl_set_max_consec_frame_drop_cbr(aom_codec_alg_priv_t * ctx,va_list args)2670 static aom_codec_err_t ctrl_set_max_consec_frame_drop_cbr(
2671     aom_codec_alg_priv_t *ctx, va_list args) {
2672   AV1_PRIMARY *const ppi = ctx->ppi;
2673   AV1_COMP *const cpi = ppi->cpi;
2674   const int max_consec_drop = CAST(AV1E_SET_MAX_CONSEC_FRAME_DROP_CBR, args);
2675   if (max_consec_drop < 0) return AOM_CODEC_INVALID_PARAM;
2676   cpi->rc.max_consec_drop = max_consec_drop;
2677   cpi->rc.drop_count_consec = 0;
2678   return AOM_CODEC_OK;
2679 }
2680 
ctrl_set_max_consec_frame_drop_ms_cbr(aom_codec_alg_priv_t * ctx,va_list args)2681 static aom_codec_err_t ctrl_set_max_consec_frame_drop_ms_cbr(
2682     aom_codec_alg_priv_t *ctx, va_list args) {
2683   AV1_PRIMARY *const ppi = ctx->ppi;
2684   AV1_COMP *const cpi = ppi->cpi;
2685   const int max_consec_drop_ms =
2686       CAST(AV1E_SET_MAX_CONSEC_FRAME_DROP_MS_CBR, args);
2687   if (max_consec_drop_ms < 0) return AOM_CODEC_INVALID_PARAM;
2688   // max_consec_drop_ms will be converted to frame units inside encoder
2689   // based on framerate (which can change dynamically).
2690   ctx->oxcf.rc_cfg.max_consec_drop_ms = max_consec_drop_ms;
2691   cpi->rc.drop_count_consec = 0;
2692   return AOM_CODEC_OK;
2693 }
2694 
ctrl_set_svc_frame_drop_mode(aom_codec_alg_priv_t * ctx,va_list args)2695 static aom_codec_err_t ctrl_set_svc_frame_drop_mode(aom_codec_alg_priv_t *ctx,
2696                                                     va_list args) {
2697   AV1_PRIMARY *const ppi = ctx->ppi;
2698   AV1_COMP *const cpi = ppi->cpi;
2699   cpi->svc.framedrop_mode = CAST(AV1E_SET_SVC_FRAME_DROP_MODE, args);
2700   if (cpi->svc.framedrop_mode != AOM_LAYER_DROP &&
2701       cpi->svc.framedrop_mode != AOM_FULL_SUPERFRAME_DROP)
2702     return AOM_CODEC_INVALID_PARAM;
2703   else
2704     return AOM_CODEC_OK;
2705 }
2706 
ctrl_set_auto_tiles(aom_codec_alg_priv_t * ctx,va_list args)2707 static aom_codec_err_t ctrl_set_auto_tiles(aom_codec_alg_priv_t *ctx,
2708                                            va_list args) {
2709   unsigned int auto_tiles = CAST(AV1E_SET_AUTO_TILES, args);
2710   if (auto_tiles == ctx->extra_cfg.auto_tiles) return AOM_CODEC_OK;
2711   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2712   extra_cfg.auto_tiles = auto_tiles;
2713   return update_extra_cfg(ctx, &extra_cfg);
2714 }
2715 
ctrl_set_postencode_drop_rtc(aom_codec_alg_priv_t * ctx,va_list args)2716 static aom_codec_err_t ctrl_set_postencode_drop_rtc(aom_codec_alg_priv_t *ctx,
2717                                                     va_list args) {
2718   AV1_PRIMARY *const ppi = ctx->ppi;
2719   AV1_COMP *const cpi = ppi->cpi;
2720   int enable_postencode_drop = CAST(AV1E_SET_POSTENCODE_DROP_RTC, args);
2721   if (enable_postencode_drop > 1 || enable_postencode_drop < 0)
2722     return AOM_CODEC_INVALID_PARAM;
2723   cpi->rc.postencode_drop = enable_postencode_drop;
2724   return AOM_CODEC_OK;
2725 }
2726 
2727 #if !CONFIG_REALTIME_ONLY
create_stats_buffer(FIRSTPASS_STATS ** frame_stats_buffer,STATS_BUFFER_CTX * stats_buf_context,int num_lap_buffers)2728 static aom_codec_err_t create_stats_buffer(FIRSTPASS_STATS **frame_stats_buffer,
2729                                            STATS_BUFFER_CTX *stats_buf_context,
2730                                            int num_lap_buffers) {
2731   aom_codec_err_t res = AOM_CODEC_OK;
2732 
2733   int size = get_stats_buf_size(num_lap_buffers, MAX_LAG_BUFFERS);
2734   *frame_stats_buffer =
2735       (FIRSTPASS_STATS *)aom_calloc(size, sizeof(FIRSTPASS_STATS));
2736   if (*frame_stats_buffer == NULL) return AOM_CODEC_MEM_ERROR;
2737 
2738   stats_buf_context->stats_in_start = *frame_stats_buffer;
2739   stats_buf_context->stats_in_end = stats_buf_context->stats_in_start;
2740   stats_buf_context->stats_in_buf_end =
2741       stats_buf_context->stats_in_start + size;
2742 
2743   stats_buf_context->total_left_stats = aom_calloc(1, sizeof(FIRSTPASS_STATS));
2744   if (stats_buf_context->total_left_stats == NULL) return AOM_CODEC_MEM_ERROR;
2745   av1_twopass_zero_stats(stats_buf_context->total_left_stats);
2746   stats_buf_context->total_stats = aom_calloc(1, sizeof(FIRSTPASS_STATS));
2747   if (stats_buf_context->total_stats == NULL) return AOM_CODEC_MEM_ERROR;
2748   av1_twopass_zero_stats(stats_buf_context->total_stats);
2749   return res;
2750 }
2751 #endif
2752 
av1_create_context_and_bufferpool(AV1_PRIMARY * ppi,AV1_COMP ** p_cpi,BufferPool ** p_buffer_pool,const AV1EncoderConfig * oxcf,COMPRESSOR_STAGE stage,int lap_lag_in_frames)2753 aom_codec_err_t av1_create_context_and_bufferpool(AV1_PRIMARY *ppi,
2754                                                   AV1_COMP **p_cpi,
2755                                                   BufferPool **p_buffer_pool,
2756                                                   const AV1EncoderConfig *oxcf,
2757                                                   COMPRESSOR_STAGE stage,
2758                                                   int lap_lag_in_frames) {
2759   aom_codec_err_t res = AOM_CODEC_OK;
2760   BufferPool *buffer_pool = *p_buffer_pool;
2761 
2762   if (buffer_pool == NULL) {
2763     buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
2764     if (buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
2765     buffer_pool->num_frame_bufs =
2766         (oxcf->mode == ALLINTRA) ? FRAME_BUFFERS_ALLINTRA : FRAME_BUFFERS;
2767     buffer_pool->frame_bufs = (RefCntBuffer *)aom_calloc(
2768         buffer_pool->num_frame_bufs, sizeof(*buffer_pool->frame_bufs));
2769     if (buffer_pool->frame_bufs == NULL) {
2770       buffer_pool->num_frame_bufs = 0;
2771       aom_free(buffer_pool);
2772       return AOM_CODEC_MEM_ERROR;
2773     }
2774 #if CONFIG_MULTITHREAD
2775     if (pthread_mutex_init(&buffer_pool->pool_mutex, NULL)) {
2776       aom_free(buffer_pool->frame_bufs);
2777       buffer_pool->frame_bufs = NULL;
2778       buffer_pool->num_frame_bufs = 0;
2779       aom_free(buffer_pool);
2780       return AOM_CODEC_MEM_ERROR;
2781     }
2782 #endif
2783     *p_buffer_pool = buffer_pool;
2784   }
2785   *p_cpi =
2786       av1_create_compressor(ppi, oxcf, buffer_pool, stage, lap_lag_in_frames);
2787   if (*p_cpi == NULL) res = AOM_CODEC_MEM_ERROR;
2788 
2789   return res;
2790 }
2791 
ctrl_set_fp_mt(aom_codec_alg_priv_t * ctx,va_list args)2792 static aom_codec_err_t ctrl_set_fp_mt(aom_codec_alg_priv_t *ctx, va_list args) {
2793   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2794   extra_cfg.fp_mt = CAST(AV1E_SET_FP_MT, args);
2795   const aom_codec_err_t result = update_extra_cfg(ctx, &extra_cfg);
2796   int num_fp_contexts = 1;
2797   if (ctx->ppi->num_fp_contexts == 1) {
2798     num_fp_contexts =
2799         av1_compute_num_fp_contexts(ctx->ppi, &ctx->ppi->parallel_cpi[0]->oxcf);
2800     if (num_fp_contexts > 1) {
2801       int i;
2802       for (i = 1; i < num_fp_contexts; i++) {
2803         int res = av1_create_context_and_bufferpool(
2804             ctx->ppi, &ctx->ppi->parallel_cpi[i], &ctx->buffer_pool, &ctx->oxcf,
2805             ENCODE_STAGE, -1);
2806         if (res != AOM_CODEC_OK) {
2807           return res;
2808         }
2809 #if !CONFIG_REALTIME_ONLY
2810         ctx->ppi->parallel_cpi[i]->twopass_frame.stats_in =
2811             ctx->ppi->twopass.stats_buf_ctx->stats_in_start;
2812 #endif
2813       }
2814     }
2815   }
2816   ctx->ppi->num_fp_contexts = num_fp_contexts;
2817   return result;
2818 }
2819 
ctrl_set_auto_intra_tools_off(aom_codec_alg_priv_t * ctx,va_list args)2820 static aom_codec_err_t ctrl_set_auto_intra_tools_off(aom_codec_alg_priv_t *ctx,
2821                                                      va_list args) {
2822   struct av1_extracfg extra_cfg = ctx->extra_cfg;
2823   extra_cfg.auto_intra_tools_off = CAST(AV1E_SET_AUTO_INTRA_TOOLS_OFF, args);
2824   return update_extra_cfg(ctx, &extra_cfg);
2825 }
2826 
encoder_init(aom_codec_ctx_t * ctx)2827 static aom_codec_err_t encoder_init(aom_codec_ctx_t *ctx) {
2828   aom_codec_err_t res = AOM_CODEC_OK;
2829 
2830   if (ctx->priv == NULL) {
2831     aom_codec_alg_priv_t *const priv = aom_calloc(1, sizeof(*priv));
2832     if (priv == NULL) return AOM_CODEC_MEM_ERROR;
2833 
2834     ctx->priv = (aom_codec_priv_t *)priv;
2835     ctx->priv->init_flags = ctx->init_flags;
2836 
2837     // Update the reference to the config structure to an internal copy.
2838     assert(ctx->config.enc);
2839     priv->cfg = *ctx->config.enc;
2840     ctx->config.enc = &priv->cfg;
2841 
2842     priv->extra_cfg = default_extra_cfg[0];
2843     // Special handling:
2844     // By default, if omitted, --enable-cdef = 1.
2845     // Here we set its default value to 0 when --allintra is turned on.
2846     // However, if users set --enable-cdef = 1 from command line,
2847     // The encoder still respects it.
2848     if (priv->cfg.g_usage == AOM_USAGE_ALL_INTRA) {
2849       priv->extra_cfg.enable_cdef = 0;
2850     }
2851     av1_initialize_enc(priv->cfg.g_usage, priv->cfg.rc_end_usage);
2852 
2853     res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
2854 
2855     if (res == AOM_CODEC_OK) {
2856       int *num_lap_buffers = &priv->num_lap_buffers;
2857       int lap_lag_in_frames = 0;
2858       *num_lap_buffers = 0;
2859       priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
2860       priv->timestamp_ratio.num =
2861           (int64_t)priv->cfg.g_timebase.num * TICKS_PER_SEC;
2862       reduce_ratio(&priv->timestamp_ratio);
2863 
2864       set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
2865       if (priv->oxcf.rc_cfg.mode != AOM_CBR &&
2866           priv->oxcf.pass == AOM_RC_ONE_PASS && priv->oxcf.mode == GOOD) {
2867         // Enable look ahead - enabled for AOM_Q, AOM_CQ, AOM_VBR
2868         *num_lap_buffers =
2869             AOMMIN((int)priv->cfg.g_lag_in_frames,
2870                    AOMMIN(MAX_LAP_BUFFERS, priv->oxcf.kf_cfg.key_freq_max +
2871                                                SCENE_CUT_KEY_TEST_INTERVAL));
2872         if ((int)priv->cfg.g_lag_in_frames - (*num_lap_buffers) >=
2873             LAP_LAG_IN_FRAMES) {
2874           lap_lag_in_frames = LAP_LAG_IN_FRAMES;
2875         }
2876       }
2877       priv->oxcf.use_highbitdepth =
2878           (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
2879 
2880       priv->monochrome_on_init = priv->cfg.monochrome;
2881 
2882       priv->ppi = av1_create_primary_compressor(&priv->pkt_list.head,
2883                                                 *num_lap_buffers, &priv->oxcf);
2884       if (!priv->ppi) return AOM_CODEC_MEM_ERROR;
2885 
2886 #if !CONFIG_REALTIME_ONLY
2887       res = create_stats_buffer(&priv->frame_stats_buffer,
2888                                 &priv->stats_buf_context, *num_lap_buffers);
2889       if (res != AOM_CODEC_OK) return res;
2890 
2891       assert(MAX_LAP_BUFFERS >= MAX_LAG_BUFFERS);
2892       int size = get_stats_buf_size(*num_lap_buffers, MAX_LAG_BUFFERS);
2893       for (int i = 0; i < size; i++)
2894         priv->ppi->twopass.frame_stats_arr[i] = &priv->frame_stats_buffer[i];
2895 
2896       priv->ppi->twopass.stats_buf_ctx = &priv->stats_buf_context;
2897 #endif
2898 
2899       assert(priv->ppi->num_fp_contexts >= 1);
2900       res = av1_create_context_and_bufferpool(
2901           priv->ppi, &priv->ppi->parallel_cpi[0], &priv->buffer_pool,
2902           &priv->oxcf, ENCODE_STAGE, -1);
2903       if (res != AOM_CODEC_OK) {
2904         priv->base.err_detail = "av1_create_context_and_bufferpool() failed";
2905         return res;
2906       }
2907 #if !CONFIG_REALTIME_ONLY
2908       priv->ppi->parallel_cpi[0]->twopass_frame.stats_in =
2909           priv->ppi->twopass.stats_buf_ctx->stats_in_start;
2910 #endif
2911       priv->ppi->cpi = priv->ppi->parallel_cpi[0];
2912 
2913       // Create another compressor if look ahead is enabled
2914       if (res == AOM_CODEC_OK && *num_lap_buffers) {
2915         res = av1_create_context_and_bufferpool(
2916             priv->ppi, &priv->ppi->cpi_lap, &priv->buffer_pool_lap, &priv->oxcf,
2917             LAP_STAGE, clamp(lap_lag_in_frames, 0, MAX_LAG_BUFFERS));
2918       }
2919     }
2920   }
2921 
2922   return res;
2923 }
2924 
av1_destroy_context_and_bufferpool(AV1_COMP * cpi,BufferPool ** p_buffer_pool)2925 void av1_destroy_context_and_bufferpool(AV1_COMP *cpi,
2926                                         BufferPool **p_buffer_pool) {
2927   av1_remove_compressor(cpi);
2928   if (*p_buffer_pool) {
2929     av1_free_ref_frame_buffers(*p_buffer_pool);
2930 #if CONFIG_MULTITHREAD
2931     pthread_mutex_destroy(&(*p_buffer_pool)->pool_mutex);
2932 #endif
2933     aom_free(*p_buffer_pool);
2934     *p_buffer_pool = NULL;
2935   }
2936 }
2937 
destroy_stats_buffer(STATS_BUFFER_CTX * stats_buf_context,FIRSTPASS_STATS * frame_stats_buffer)2938 static void destroy_stats_buffer(STATS_BUFFER_CTX *stats_buf_context,
2939                                  FIRSTPASS_STATS *frame_stats_buffer) {
2940   aom_free(stats_buf_context->total_left_stats);
2941   aom_free(stats_buf_context->total_stats);
2942   aom_free(frame_stats_buffer);
2943 }
2944 
check_and_free_string(const char * default_str,const char ** ptr)2945 static void check_and_free_string(const char *default_str, const char **ptr) {
2946   if (*ptr == default_str) {
2947     // Default should be a literal. Do not free.
2948     return;
2949   }
2950   aom_free((void *)*ptr);
2951   *ptr = NULL;
2952 }
2953 
destroy_extra_config(struct av1_extracfg * extra_cfg)2954 static void destroy_extra_config(struct av1_extracfg *extra_cfg) {
2955 #if CONFIG_TUNE_VMAF
2956   check_and_free_string(default_extra_cfg[0].vmaf_model_path,
2957                         &extra_cfg->vmaf_model_path);
2958 #endif
2959   check_and_free_string(default_extra_cfg[0].two_pass_output,
2960                         &extra_cfg->two_pass_output);
2961   check_and_free_string(default_extra_cfg[0].two_pass_output,
2962                         &extra_cfg->second_pass_log);
2963   check_and_free_string(default_extra_cfg[0].partition_info_path,
2964                         &extra_cfg->partition_info_path);
2965   check_and_free_string(default_extra_cfg[0].rate_distribution_info,
2966                         &extra_cfg->rate_distribution_info);
2967   check_and_free_string(default_extra_cfg[0].film_grain_table_filename,
2968                         &extra_cfg->film_grain_table_filename);
2969 }
2970 
encoder_destroy(aom_codec_alg_priv_t * ctx)2971 static aom_codec_err_t encoder_destroy(aom_codec_alg_priv_t *ctx) {
2972   free(ctx->cx_data);
2973   destroy_extra_config(&ctx->extra_cfg);
2974 
2975   if (ctx->ppi) {
2976     AV1_PRIMARY *ppi = ctx->ppi;
2977     for (int i = 0; i < MAX_PARALLEL_FRAMES - 1; i++) {
2978       if (ppi->parallel_frames_data[i].cx_data) {
2979         free(ppi->parallel_frames_data[i].cx_data);
2980       }
2981     }
2982 #if CONFIG_ENTROPY_STATS
2983     print_entropy_stats(ppi);
2984 #endif
2985 #if CONFIG_INTERNAL_STATS
2986     print_internal_stats(ppi);
2987 #endif
2988 
2989     for (int i = 0; i < MAX_PARALLEL_FRAMES; i++) {
2990       av1_destroy_context_and_bufferpool(ppi->parallel_cpi[i],
2991                                          &ctx->buffer_pool);
2992     }
2993     ppi->cpi = NULL;
2994 
2995     if (ppi->cpi_lap) {
2996       av1_destroy_context_and_bufferpool(ppi->cpi_lap, &ctx->buffer_pool_lap);
2997     }
2998     av1_remove_primary_compressor(ppi);
2999   }
3000   destroy_stats_buffer(&ctx->stats_buf_context, ctx->frame_stats_buffer);
3001   aom_free(ctx);
3002   return AOM_CODEC_OK;
3003 }
3004 
get_frame_pkt_flags(const AV1_COMP * cpi,unsigned int lib_flags)3005 static aom_codec_frame_flags_t get_frame_pkt_flags(const AV1_COMP *cpi,
3006                                                    unsigned int lib_flags) {
3007   aom_codec_frame_flags_t flags = lib_flags << 16;
3008   if (lib_flags & FRAMEFLAGS_KEY) flags |= AOM_FRAME_IS_KEY;
3009   if (lib_flags & FRAMEFLAGS_INTRAONLY) flags |= AOM_FRAME_IS_INTRAONLY;
3010   if (lib_flags & FRAMEFLAGS_SWITCH) flags |= AOM_FRAME_IS_SWITCH;
3011   if (lib_flags & FRAMEFLAGS_ERROR_RESILIENT)
3012     flags |= AOM_FRAME_IS_ERROR_RESILIENT;
3013   if (cpi->droppable) flags |= AOM_FRAME_IS_DROPPABLE;
3014 
3015   return flags;
3016 }
3017 
get_src_border_in_pixels(AV1_COMP * cpi,BLOCK_SIZE sb_size)3018 static inline int get_src_border_in_pixels(AV1_COMP *cpi, BLOCK_SIZE sb_size) {
3019   if (cpi->oxcf.mode != REALTIME || av1_is_resize_needed(&cpi->oxcf))
3020     return cpi->oxcf.border_in_pixels;
3021 
3022   const int sb_size_in_pixels_log2 = mi_size_wide_log2[sb_size] + MI_SIZE_LOG2;
3023   const int sb_aligned_width =
3024       ALIGN_POWER_OF_TWO(cpi->oxcf.frm_dim_cfg.width, sb_size_in_pixels_log2);
3025   const int sb_aligned_height =
3026       ALIGN_POWER_OF_TWO(cpi->oxcf.frm_dim_cfg.height, sb_size_in_pixels_log2);
3027   // Align the border pixels to a multiple of 32.
3028   const int border_pixels_width =
3029       ALIGN_POWER_OF_TWO(sb_aligned_width - cpi->oxcf.frm_dim_cfg.width, 5);
3030   const int border_pixels_height =
3031       ALIGN_POWER_OF_TWO(sb_aligned_height - cpi->oxcf.frm_dim_cfg.height, 5);
3032   const int border_in_pixels =
3033       AOMMAX(AOMMAX(border_pixels_width, border_pixels_height), 32);
3034   return border_in_pixels;
3035 }
3036 
3037 // TODO(Mufaddal): Check feasibility of abstracting functions related to LAP
3038 // 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)3039 static aom_codec_err_t encoder_encode(aom_codec_alg_priv_t *ctx,
3040                                       const aom_image_t *img,
3041                                       aom_codec_pts_t pts,
3042                                       unsigned long duration,
3043                                       aom_enc_frame_flags_t enc_flags) {
3044   const size_t kMinCompressedSize = 8192;
3045   volatile aom_codec_err_t res = AOM_CODEC_OK;
3046   AV1_PRIMARY *const ppi = ctx->ppi;
3047   volatile aom_codec_pts_t ptsvol = pts;
3048   AV1_COMP_DATA cpi_data = { 0 };
3049 
3050   cpi_data.timestamp_ratio = &ctx->timestamp_ratio;
3051   cpi_data.flush = !img;
3052   // LAP context
3053   AV1_COMP *cpi_lap = ppi->cpi_lap;
3054   if (ppi->cpi == NULL) return AOM_CODEC_INVALID_PARAM;
3055 
3056   ppi->cpi->last_coded_width = ppi->cpi->oxcf.frm_dim_cfg.width;
3057   ppi->cpi->last_coded_height = ppi->cpi->oxcf.frm_dim_cfg.height;
3058 
3059   if (ppi->lap_enabled && cpi_lap == NULL &&
3060       ppi->cpi->oxcf.pass == AOM_RC_ONE_PASS)
3061     return AOM_CODEC_INVALID_PARAM;
3062 
3063   if (img != NULL) {
3064     res = validate_img(ctx, img);
3065     if (res == AOM_CODEC_OK) {
3066       const uint64_t uncompressed_frame_sz64 =
3067           (uint64_t)ALIGN_POWER_OF_TWO_UNSIGNED(ctx->cfg.g_w, 5) *
3068           ALIGN_POWER_OF_TWO_UNSIGNED(ctx->cfg.g_h, 5) * get_image_bps(img) / 8;
3069 #if UINT64_MAX > SIZE_MAX
3070       if (uncompressed_frame_sz64 > SIZE_MAX) return AOM_CODEC_MEM_ERROR;
3071 #endif
3072       const size_t uncompressed_frame_sz = (size_t)uncompressed_frame_sz64;
3073 
3074       // Due to the presence of no-show frames, the ctx->cx_data buffer holds
3075       // compressed data corresponding to multiple frames. As no-show frames are
3076       // not possible for all intra frame encoding with no forward key frames,
3077       // the buffer is allocated with a smaller size in this case.
3078       //
3079       // For pseudo random input, the compressed frame size is seen to exceed
3080       // the uncompressed frame size, but is less than 2 times the uncompressed
3081       // frame size. Hence the size of the buffer is chosen as 2 times the
3082       // uncompressed frame size.
3083       int multiplier = 8;
3084       if (ppi->cpi->oxcf.kf_cfg.key_freq_max == 0 &&
3085           !ppi->cpi->oxcf.kf_cfg.fwd_kf_enabled)
3086         multiplier = 2;
3087       if (uncompressed_frame_sz > SIZE_MAX / multiplier)
3088         return AOM_CODEC_MEM_ERROR;
3089       size_t data_sz = uncompressed_frame_sz * multiplier;
3090       if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
3091       if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
3092         ctx->cx_data_sz = data_sz;
3093         free(ctx->cx_data);
3094         ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
3095         if (ctx->cx_data == NULL) {
3096           ctx->cx_data_sz = 0;
3097           return AOM_CODEC_MEM_ERROR;
3098         }
3099       }
3100       for (int i = 0; i < ppi->num_fp_contexts - 1; i++) {
3101         if (ppi->parallel_frames_data[i].cx_data == NULL ||
3102             ppi->parallel_frames_data[i].cx_data_sz < data_sz) {
3103           ppi->parallel_frames_data[i].cx_data_sz = data_sz;
3104           free(ppi->parallel_frames_data[i].cx_data);
3105           ppi->parallel_frames_data[i].frame_size = 0;
3106           ppi->parallel_frames_data[i].cx_data =
3107               (unsigned char *)malloc(ppi->parallel_frames_data[i].cx_data_sz);
3108           if (ppi->parallel_frames_data[i].cx_data == NULL) {
3109             ppi->parallel_frames_data[i].cx_data_sz = 0;
3110             return AOM_CODEC_MEM_ERROR;
3111           }
3112         }
3113       }
3114     }
3115   }
3116 
3117   aom_codec_pkt_list_init(&ctx->pkt_list);
3118 
3119   volatile aom_enc_frame_flags_t flags = enc_flags;
3120 
3121   // The jmp_buf is valid only for the duration of the function that calls
3122   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3123   // before it returns.
3124   if (setjmp(ppi->error.jmp)) {
3125     ppi->error.setjmp = 0;
3126     res = update_error_state(ctx, &ppi->error);
3127     return res;
3128   }
3129   ppi->error.setjmp = 1;
3130 
3131   if (ppi->use_svc && ppi->cpi->svc.use_flexible_mode == 0 && flags == 0)
3132     av1_set_svc_fixed_mode(ppi->cpi);
3133 
3134   // Note(yunqing): While applying encoding flags, always start from enabling
3135   // all, and then modifying according to the flags. Previous frame's flags are
3136   // overwritten.
3137   av1_apply_encoding_flags(ppi->cpi, flags);
3138   if (cpi_lap != NULL) {
3139     av1_apply_encoding_flags(cpi_lap, flags);
3140   }
3141 
3142 #if CONFIG_TUNE_VMAF
3143   if (ctx->extra_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
3144       ctx->extra_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
3145     aom_init_vmaf_model(&ppi->cpi->vmaf_info.vmaf_model,
3146                         ppi->cpi->oxcf.tune_cfg.vmaf_model_path);
3147   }
3148 #endif
3149 
3150   // Handle fixed keyframe intervals
3151   if (is_stat_generation_stage(ppi->cpi) || is_one_pass_rt_params(ppi->cpi)) {
3152     if (ctx->cfg.kf_mode == AOM_KF_AUTO &&
3153         ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
3154       if (ppi->cpi->common.spatial_layer_id == 0 &&
3155           ++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
3156         flags |= AOM_EFLAG_FORCE_KF;
3157         ctx->fixed_kf_cntr = 1;
3158       }
3159     }
3160   }
3161 
3162   if (res == AOM_CODEC_OK) {
3163     AV1_COMP *cpi = ppi->cpi;
3164 
3165     // Set up internal flags
3166     if (ctx->base.init_flags & AOM_CODEC_USE_PSNR) ppi->b_calculate_psnr = 1;
3167 
3168     if (img != NULL) {
3169       if (!ctx->pts_offset_initialized) {
3170         ctx->pts_offset = ptsvol;
3171         ctx->pts_offset_initialized = 1;
3172       }
3173       if (ptsvol < ctx->pts_offset) {
3174         aom_internal_error(&ppi->error, AOM_CODEC_INVALID_PARAM,
3175                            "pts is smaller than initial pts");
3176       }
3177       ptsvol -= ctx->pts_offset;
3178       if (ptsvol > INT64_MAX / cpi_data.timestamp_ratio->num) {
3179         aom_internal_error(
3180             &ppi->error, AOM_CODEC_INVALID_PARAM,
3181             "conversion of relative pts to ticks would overflow");
3182       }
3183       int64_t src_time_stamp =
3184           timebase_units_to_ticks(cpi_data.timestamp_ratio, ptsvol);
3185 #if ULONG_MAX > INT64_MAX
3186       if (duration > INT64_MAX) {
3187         aom_internal_error(&ppi->error, AOM_CODEC_INVALID_PARAM,
3188                            "duration is too big");
3189       }
3190 #endif
3191       if (ptsvol > INT64_MAX - (int64_t)duration) {
3192         aom_internal_error(&ppi->error, AOM_CODEC_INVALID_PARAM,
3193                            "relative pts + duration is too big");
3194       }
3195       aom_codec_pts_t pts_end = ptsvol + (int64_t)duration;
3196       if (pts_end > INT64_MAX / cpi_data.timestamp_ratio->num) {
3197         aom_internal_error(
3198             &ppi->error, AOM_CODEC_INVALID_PARAM,
3199             "conversion of relative pts + duration to ticks would overflow");
3200       }
3201       int64_t src_end_time_stamp =
3202           timebase_units_to_ticks(cpi_data.timestamp_ratio, pts_end);
3203 
3204       YV12_BUFFER_CONFIG sd;
3205       res = image2yuvconfig(img, &sd);
3206       // When generating a monochrome stream, make |sd| a monochrome image.
3207       if (ctx->cfg.monochrome) {
3208         sd.u_buffer = sd.v_buffer = NULL;
3209         sd.uv_stride = 0;
3210         sd.monochrome = 1;
3211       }
3212       int use_highbitdepth = (sd.flags & YV12_FLAG_HIGHBITDEPTH) != 0;
3213       int subsampling_x = sd.subsampling_x;
3214       int subsampling_y = sd.subsampling_y;
3215 
3216       if (!ppi->lookahead) {
3217         int lag_in_frames = cpi_lap != NULL ? cpi_lap->oxcf.gf_cfg.lag_in_frames
3218                                             : cpi->oxcf.gf_cfg.lag_in_frames;
3219         AV1EncoderConfig *oxcf = &cpi->oxcf;
3220         const BLOCK_SIZE sb_size = av1_select_sb_size(
3221             oxcf, oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
3222             ppi->number_spatial_layers);
3223         oxcf->border_in_pixels =
3224             av1_get_enc_border_size(av1_is_resize_needed(oxcf),
3225                                     oxcf->kf_cfg.key_freq_max == 0, sb_size);
3226         for (int i = 0; i < ppi->num_fp_contexts; i++) {
3227           ppi->parallel_cpi[i]->oxcf.border_in_pixels = oxcf->border_in_pixels;
3228         }
3229 
3230         const int src_border_in_pixels = get_src_border_in_pixels(cpi, sb_size);
3231         ppi->lookahead = av1_lookahead_init(
3232             cpi->oxcf.frm_dim_cfg.width, cpi->oxcf.frm_dim_cfg.height,
3233             subsampling_x, subsampling_y, use_highbitdepth, lag_in_frames,
3234             src_border_in_pixels, cpi->common.features.byte_alignment,
3235             ctx->num_lap_buffers, (cpi->oxcf.kf_cfg.key_freq_max == 0),
3236             cpi->alloc_pyramid);
3237       }
3238       if (!ppi->lookahead)
3239         aom_internal_error(&ppi->error, AOM_CODEC_MEM_ERROR,
3240                            "Failed to allocate lag buffers");
3241       for (int i = 0; i < ppi->num_fp_contexts; i++) {
3242         aom_codec_err_t err =
3243             av1_check_initial_width(ppi->parallel_cpi[i], use_highbitdepth,
3244                                     subsampling_x, subsampling_y);
3245         if (err != AOM_CODEC_OK) {
3246           aom_internal_error(&ppi->error, err,
3247                              "av1_check_initial_width() failed");
3248         }
3249       }
3250       if (cpi_lap != NULL) {
3251         aom_codec_err_t err = av1_check_initial_width(
3252             cpi_lap, use_highbitdepth, subsampling_x, subsampling_y);
3253         if (err != AOM_CODEC_OK) {
3254           aom_internal_error(&ppi->error, err,
3255                              "av1_check_initial_width() failed");
3256         }
3257       }
3258 
3259       // Store the original flags in to the frame buffer. Will extract the
3260       // key frame flag when we actually encode this frame.
3261       if (av1_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
3262                                 src_time_stamp, src_end_time_stamp)) {
3263         res = update_error_state(ctx, cpi->common.error);
3264       }
3265       ctx->next_frame_flags = 0;
3266     }
3267 
3268     cpi_data.cx_data = ctx->cx_data;
3269     cpi_data.cx_data_sz = ctx->cx_data_sz;
3270 
3271     /* Any pending invisible frames? */
3272     if (ctx->pending_cx_data_sz) {
3273       cpi_data.cx_data += ctx->pending_cx_data_sz;
3274       cpi_data.cx_data_sz -= ctx->pending_cx_data_sz;
3275 
3276       /* TODO: this is a minimal check, the underlying codec doesn't respect
3277        * the buffer size anyway.
3278        */
3279       if (cpi_data.cx_data_sz < ctx->cx_data_sz / 2) {
3280         aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
3281                            "Compressed data buffer too small");
3282       }
3283     }
3284 
3285     int is_frame_visible = 0;
3286     int has_no_show_keyframe = 0;
3287     int num_workers = 0;
3288 
3289     if (cpi->oxcf.pass == AOM_RC_FIRST_PASS) {
3290 #if !CONFIG_REALTIME_ONLY
3291       num_workers = ppi->p_mt_info.num_mod_workers[MOD_FP] =
3292           av1_fp_compute_num_enc_workers(cpi);
3293 #endif
3294     } else {
3295       av1_compute_num_workers_for_mt(cpi);
3296       num_workers = av1_get_max_num_workers(cpi);
3297     }
3298     if (num_workers > 1 && ppi->p_mt_info.num_workers < num_workers) {
3299       // Obtain the maximum no. of frames that can be supported in a parallel
3300       // encode set.
3301       if (is_stat_consumption_stage(cpi)) {
3302         ppi->num_fp_contexts = av1_compute_num_fp_contexts(ppi, &cpi->oxcf);
3303       }
3304       if (ppi->p_mt_info.num_workers > 0) {
3305         av1_terminate_workers(ppi);
3306         free_thread_data(ppi);
3307         aom_free(ppi->p_mt_info.tile_thr_data);
3308         ppi->p_mt_info.tile_thr_data = NULL;
3309         aom_free(ppi->p_mt_info.workers);
3310         ppi->p_mt_info.workers = NULL;
3311         ppi->p_mt_info.num_workers = 0;
3312         for (int j = 0; j < ppi->num_fp_contexts; j++) {
3313           aom_free(ppi->parallel_cpi[j]->td.tctx);
3314           ppi->parallel_cpi[j]->td.tctx = NULL;
3315         }
3316       }
3317       av1_create_workers(ppi, num_workers);
3318       av1_init_tile_thread_data(ppi, cpi->oxcf.pass == AOM_RC_FIRST_PASS);
3319     }
3320 
3321     // Re-allocate thread data if workers for encoder multi-threading stage
3322     // exceeds prev_num_enc_workers.
3323     const int num_enc_workers =
3324         av1_get_num_mod_workers_for_alloc(&ppi->p_mt_info, MOD_ENC);
3325     if (ppi->p_mt_info.prev_num_enc_workers < num_enc_workers &&
3326         num_enc_workers <= ppi->p_mt_info.num_workers) {
3327       free_thread_data(ppi);
3328       for (int j = 0; j < ppi->num_fp_contexts; j++) {
3329         aom_free(ppi->parallel_cpi[j]->td.tctx);
3330         ppi->parallel_cpi[j]->td.tctx = NULL;
3331       }
3332       av1_init_tile_thread_data(ppi, cpi->oxcf.pass == AOM_RC_FIRST_PASS);
3333     }
3334 
3335     for (int i = 0; i < ppi->num_fp_contexts; i++) {
3336       av1_init_frame_mt(ppi, ppi->parallel_cpi[i]);
3337     }
3338     if (cpi_lap != NULL) {
3339       av1_init_frame_mt(ppi, cpi_lap);
3340     }
3341 #if CONFIG_MULTITHREAD
3342     if (ppi->p_mt_info.num_workers > 1) {
3343       for (int i = 0; i < ppi->num_fp_contexts; i++) {
3344         av1_init_mt_sync(ppi->parallel_cpi[i],
3345                          ppi->parallel_cpi[i]->oxcf.pass == AOM_RC_FIRST_PASS);
3346       }
3347       if (cpi_lap != NULL) {
3348         av1_init_mt_sync(cpi_lap, 1);
3349       }
3350     }
3351 #endif  // CONFIG_MULTITHREAD
3352 
3353     // Call for LAP stage
3354     if (cpi_lap != NULL) {
3355       AV1_COMP_DATA cpi_lap_data = { 0 };
3356       cpi_lap_data.flush = !img;
3357       cpi_lap_data.timestamp_ratio = &ctx->timestamp_ratio;
3358       const int status = av1_get_compressed_data(cpi_lap, &cpi_lap_data);
3359       if (status > AOM_CODEC_OK) {
3360         aom_internal_error_copy(&ppi->error, cpi_lap->common.error);
3361       }
3362       av1_post_encode_updates(cpi_lap, &cpi_lap_data);
3363     }
3364 
3365     // Recalculate the maximum number of frames that can be encoded in
3366     // parallel at the beginning of sub gop.
3367     if (is_stat_consumption_stage(cpi) && ppi->gf_group.size > 0 &&
3368         cpi->gf_frame_index == ppi->gf_group.size) {
3369       ppi->num_fp_contexts = av1_compute_num_fp_contexts(ppi, &cpi->oxcf);
3370     }
3371 
3372     // Get the next visible frame. Invisible frames get packed with the next
3373     // visible frame.
3374     while (cpi_data.cx_data_sz >= ctx->cx_data_sz / 2 && !is_frame_visible) {
3375       int simulate_parallel_frame = 0;
3376       int status = -1;
3377       cpi->do_frame_data_update = true;
3378       cpi->ref_idx_to_skip = INVALID_IDX;
3379       cpi->ref_refresh_index = INVALID_IDX;
3380       cpi->refresh_idx_available = false;
3381 
3382 #if CONFIG_FPMT_TEST
3383       simulate_parallel_frame =
3384           cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0;
3385       if (simulate_parallel_frame) {
3386         if (ppi->num_fp_contexts > 1 && ppi->gf_group.size > 1) {
3387           if (cpi->gf_frame_index < ppi->gf_group.size) {
3388             calc_frame_data_update_flag(&ppi->gf_group, cpi->gf_frame_index,
3389                                         &cpi->do_frame_data_update);
3390           }
3391         }
3392         status = av1_get_compressed_data(cpi, &cpi_data);
3393       }
3394 
3395 #endif  // CONFIG_FPMT_TEST
3396       if (!simulate_parallel_frame) {
3397         if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
3398           status = av1_get_compressed_data(cpi, &cpi_data);
3399         } else if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] ==
3400                    1) {
3401           // In case of an error, longjmp() would be invoked and hence "status"
3402           // is set to AOM_CODEC_OK here.
3403           av1_compress_parallel_frames(ppi, &cpi_data);
3404           status = AOM_CODEC_OK;
3405         } else {
3406           // No possibility of failures from this function and hence "status" is
3407           // set to AOM_CODEC_OK here.
3408           cpi = av1_get_parallel_frame_enc_data(ppi, &cpi_data);
3409           status = AOM_CODEC_OK;
3410         }
3411       }
3412       if (status == -1) break;
3413       if (status != AOM_CODEC_OK) {
3414         aom_internal_error_copy(&ppi->error, cpi->common.error);
3415       }
3416       if (ppi->num_fp_contexts > 0 && frame_is_intra_only(&cpi->common)) {
3417         av1_init_sc_decisions(ppi);
3418       }
3419 
3420       ppi->seq_params_locked = 1;
3421       av1_post_encode_updates(cpi, &cpi_data);
3422 
3423 #if CONFIG_ENTROPY_STATS
3424       if (ppi->cpi->oxcf.pass != 1 && !cpi->common.show_existing_frame)
3425         av1_accumulate_frame_counts(&ppi->aggregate_fc, &cpi->counts);
3426 #endif
3427 #if CONFIG_INTERNAL_STATS
3428       if (ppi->cpi->oxcf.pass != 1) {
3429         ppi->total_time_compress_data += cpi->time_compress_data;
3430         ppi->total_recode_hits += cpi->frame_recode_hits;
3431         ppi->total_bytes += (uint64_t)cpi->bytes;
3432         for (int i = 0; i < MAX_MODES; i++) {
3433           ppi->total_mode_chosen_counts[i] += cpi->mode_chosen_counts[i];
3434         }
3435       }
3436 #endif  // CONFIG_INTERNAL_STATS
3437 
3438       if (!cpi_data.frame_size) continue;
3439       assert(cpi_data.cx_data != NULL && cpi_data.cx_data_sz != 0);
3440       if (cpi_data.frame_size > cpi_data.cx_data_sz) {
3441         aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
3442                            "cpi_data.cx_data buffer overflow");
3443       }
3444       const int write_temporal_delimiter =
3445           !cpi->common.spatial_layer_id && !ctx->pending_cx_data_sz;
3446 
3447       if (write_temporal_delimiter) {
3448         uint32_t obu_header_size = 1;
3449         const uint32_t obu_payload_size = 0;
3450         const size_t length_field_size =
3451             aom_uleb_size_in_bytes(obu_payload_size);
3452 
3453         const size_t move_offset = obu_header_size + length_field_size;
3454         assert(ctx->cx_data_sz == cpi_data.cx_data_sz);
3455         if (move_offset > ctx->cx_data_sz - cpi_data.frame_size) {
3456           aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
3457                              "ctx->cx_data buffer full");
3458         }
3459         memmove(ctx->cx_data + move_offset, ctx->cx_data, cpi_data.frame_size);
3460         obu_header_size = av1_write_obu_header(
3461             &ppi->level_params, &cpi->frame_header_count,
3462             OBU_TEMPORAL_DELIMITER,
3463             ppi->seq_params.has_nonzero_operating_point_idc, 0, ctx->cx_data);
3464         if (obu_header_size != 1) {
3465           aom_internal_error(&ppi->error, AOM_CODEC_ERROR, NULL);
3466         }
3467 
3468         // OBUs are preceded/succeeded by an unsigned leb128 coded integer.
3469         if (av1_write_uleb_obu_size(obu_payload_size,
3470                                     ctx->cx_data + obu_header_size,
3471                                     length_field_size) != AOM_CODEC_OK) {
3472           aom_internal_error(&ppi->error, AOM_CODEC_ERROR, NULL);
3473         }
3474 
3475         cpi_data.frame_size += move_offset;
3476       }
3477 
3478       if (ctx->oxcf.save_as_annexb) {
3479         if (av1_convert_sect5obus_to_annexb(
3480                 cpi_data.cx_data, cpi_data.cx_data_sz, &cpi_data.frame_size) !=
3481             AOM_CODEC_OK) {
3482           aom_internal_error(&ppi->error, AOM_CODEC_ERROR, NULL);
3483         }
3484 
3485         // B_PRIME (add frame size)
3486         const size_t length_field_size =
3487             aom_uleb_size_in_bytes(cpi_data.frame_size);
3488         if (length_field_size > cpi_data.cx_data_sz - cpi_data.frame_size) {
3489           aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
3490                              "cpi_data.cx_data buffer full");
3491         }
3492         memmove(cpi_data.cx_data + length_field_size, cpi_data.cx_data,
3493                 cpi_data.frame_size);
3494         if (av1_write_uleb_obu_size(cpi_data.frame_size, cpi_data.cx_data,
3495                                     length_field_size) != AOM_CODEC_OK) {
3496           aom_internal_error(&ppi->error, AOM_CODEC_ERROR, NULL);
3497         }
3498         cpi_data.frame_size += length_field_size;
3499       }
3500 
3501       ctx->pending_cx_data_sz += cpi_data.frame_size;
3502 
3503       cpi_data.cx_data += cpi_data.frame_size;
3504       cpi_data.cx_data_sz -= cpi_data.frame_size;
3505 
3506       is_frame_visible = cpi->common.show_frame;
3507 
3508       has_no_show_keyframe |=
3509           (!is_frame_visible &&
3510            cpi->common.current_frame.frame_type == KEY_FRAME);
3511     }
3512     if (is_frame_visible) {
3513       // Add the frame packet to the list of returned packets.
3514       aom_codec_cx_pkt_t pkt;
3515 
3516       // decrement frames_left counter
3517       ppi->frames_left = AOMMAX(0, ppi->frames_left - 1);
3518       if (ctx->oxcf.save_as_annexb) {
3519         //  B_PRIME (add TU size)
3520         size_t tu_size = ctx->pending_cx_data_sz;
3521         const size_t length_field_size = aom_uleb_size_in_bytes(tu_size);
3522         if (tu_size > ctx->cx_data_sz) {
3523           aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
3524                              "ctx->cx_data buffer overflow");
3525         }
3526         if (length_field_size > ctx->cx_data_sz - tu_size) {
3527           aom_internal_error(&ppi->error, AOM_CODEC_ERROR,
3528                              "ctx->cx_data buffer full");
3529         }
3530         memmove(ctx->cx_data + length_field_size, ctx->cx_data, tu_size);
3531         if (av1_write_uleb_obu_size(tu_size, ctx->cx_data, length_field_size) !=
3532             AOM_CODEC_OK) {
3533           aom_internal_error(&ppi->error, AOM_CODEC_ERROR, NULL);
3534         }
3535         ctx->pending_cx_data_sz += length_field_size;
3536       }
3537 
3538       pkt.kind = AOM_CODEC_CX_FRAME_PKT;
3539 
3540       pkt.data.frame.buf = ctx->cx_data;
3541       pkt.data.frame.sz = ctx->pending_cx_data_sz;
3542       pkt.data.frame.partition_id = -1;
3543       pkt.data.frame.vis_frame_size = cpi_data.frame_size;
3544 
3545       pkt.data.frame.pts = ticks_to_timebase_units(cpi_data.timestamp_ratio,
3546                                                    cpi_data.ts_frame_start) +
3547                            ctx->pts_offset;
3548       pkt.data.frame.flags = get_frame_pkt_flags(cpi, cpi_data.lib_flags);
3549       if (has_no_show_keyframe) {
3550         // If one of the invisible frames in the packet is a keyframe, set
3551         // the delayed random access point flag.
3552         pkt.data.frame.flags |= AOM_FRAME_IS_DELAYED_RANDOM_ACCESS_POINT;
3553       }
3554       const int64_t duration64 = ticks_to_timebase_units(
3555           cpi_data.timestamp_ratio,
3556           cpi_data.ts_frame_end - cpi_data.ts_frame_start);
3557       if (duration64 > UINT32_MAX) {
3558         aom_internal_error(&ppi->error, AOM_CODEC_ERROR, NULL);
3559       }
3560       pkt.data.frame.duration = (uint32_t)duration64;
3561 
3562       aom_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
3563 
3564       ctx->pending_cx_data_sz = 0;
3565     }
3566   }
3567 
3568   ppi->error.setjmp = 0;
3569   return res;
3570 }
3571 
encoder_get_cxdata(aom_codec_alg_priv_t * ctx,aom_codec_iter_t * iter)3572 static const aom_codec_cx_pkt_t *encoder_get_cxdata(aom_codec_alg_priv_t *ctx,
3573                                                     aom_codec_iter_t *iter) {
3574   return aom_codec_pkt_list_get(&ctx->pkt_list.head, iter);
3575 }
3576 
ctrl_set_reference(aom_codec_alg_priv_t * ctx,va_list args)3577 static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
3578                                           va_list args) {
3579   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
3580 
3581   if (frame != NULL) {
3582     YV12_BUFFER_CONFIG sd;
3583 
3584     image2yuvconfig(&frame->img, &sd);
3585     av1_set_reference_enc(ctx->ppi->cpi, frame->idx, &sd);
3586     return AOM_CODEC_OK;
3587   } else {
3588     return AOM_CODEC_INVALID_PARAM;
3589   }
3590 }
3591 
ctrl_copy_reference(aom_codec_alg_priv_t * ctx,va_list args)3592 static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
3593                                            va_list args) {
3594   if (ctx->ppi->cpi->oxcf.algo_cfg.skip_postproc_filtering)
3595     return AOM_CODEC_INCAPABLE;
3596   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
3597 
3598   if (frame != NULL) {
3599     YV12_BUFFER_CONFIG sd;
3600 
3601     image2yuvconfig(&frame->img, &sd);
3602     av1_copy_reference_enc(ctx->ppi->cpi, frame->idx, &sd);
3603     return AOM_CODEC_OK;
3604   } else {
3605     return AOM_CODEC_INVALID_PARAM;
3606   }
3607 }
3608 
ctrl_get_reference(aom_codec_alg_priv_t * ctx,va_list args)3609 static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
3610                                           va_list args) {
3611   if (ctx->ppi->cpi->oxcf.algo_cfg.skip_postproc_filtering)
3612     return AOM_CODEC_INCAPABLE;
3613   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
3614 
3615   if (frame != NULL) {
3616     YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->ppi->cpi->common, frame->idx);
3617     if (fb == NULL) return AOM_CODEC_ERROR;
3618 
3619     yuvconfig2image(&frame->img, fb, NULL);
3620     return AOM_CODEC_OK;
3621   } else {
3622     return AOM_CODEC_INVALID_PARAM;
3623   }
3624 }
3625 
ctrl_get_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)3626 static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
3627                                                 va_list args) {
3628   aom_image_t *const new_img = va_arg(args, aom_image_t *);
3629 
3630   if (new_img != NULL) {
3631     YV12_BUFFER_CONFIG new_frame;
3632 
3633     if (av1_get_last_show_frame(ctx->ppi->cpi, &new_frame) == 0) {
3634       yuvconfig2image(new_img, &new_frame, NULL);
3635       return AOM_CODEC_OK;
3636     } else {
3637       return AOM_CODEC_ERROR;
3638     }
3639   } else {
3640     return AOM_CODEC_INVALID_PARAM;
3641   }
3642 }
3643 
ctrl_copy_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)3644 static aom_codec_err_t ctrl_copy_new_frame_image(aom_codec_alg_priv_t *ctx,
3645                                                  va_list args) {
3646   aom_image_t *const new_img = va_arg(args, aom_image_t *);
3647 
3648   if (new_img != NULL) {
3649     YV12_BUFFER_CONFIG new_frame;
3650 
3651     if (av1_get_last_show_frame(ctx->ppi->cpi, &new_frame) == 0) {
3652       YV12_BUFFER_CONFIG sd;
3653       image2yuvconfig(new_img, &sd);
3654       return av1_copy_new_frame_enc(&ctx->ppi->cpi->common, &new_frame, &sd);
3655     } else {
3656       return AOM_CODEC_ERROR;
3657     }
3658   } else {
3659     return AOM_CODEC_INVALID_PARAM;
3660   }
3661 }
3662 
encoder_get_preview(aom_codec_alg_priv_t * ctx)3663 static aom_image_t *encoder_get_preview(aom_codec_alg_priv_t *ctx) {
3664   YV12_BUFFER_CONFIG sd;
3665 
3666   if (av1_get_preview_raw_frame(ctx->ppi->cpi, &sd) == 0) {
3667     yuvconfig2image(&ctx->preview_img, &sd, NULL);
3668     return &ctx->preview_img;
3669   } else {
3670     return NULL;
3671   }
3672 }
3673 
ctrl_use_reference(aom_codec_alg_priv_t * ctx,va_list args)3674 static aom_codec_err_t ctrl_use_reference(aom_codec_alg_priv_t *ctx,
3675                                           va_list args) {
3676   const int reference_flag = va_arg(args, int);
3677 
3678   av1_use_as_reference(&ctx->ppi->cpi->ext_flags.ref_frame_flags,
3679                        reference_flag);
3680   return AOM_CODEC_OK;
3681 }
3682 
ctrl_set_roi_map(aom_codec_alg_priv_t * ctx,va_list args)3683 static aom_codec_err_t ctrl_set_roi_map(aom_codec_alg_priv_t *ctx,
3684                                         va_list args) {
3685   (void)ctx;
3686   (void)args;
3687 
3688   // TODO(yaowu): Need to re-implement and test for AV1.
3689   return AOM_CODEC_INVALID_PARAM;
3690 }
3691 
ctrl_set_active_map(aom_codec_alg_priv_t * ctx,va_list args)3692 static aom_codec_err_t ctrl_set_active_map(aom_codec_alg_priv_t *ctx,
3693                                            va_list args) {
3694   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
3695 
3696   if (map) {
3697     if (!av1_set_active_map(ctx->ppi->cpi, map->active_map, (int)map->rows,
3698                             (int)map->cols))
3699       return AOM_CODEC_OK;
3700     else
3701       return AOM_CODEC_INVALID_PARAM;
3702   } else {
3703     return AOM_CODEC_INVALID_PARAM;
3704   }
3705 }
3706 
ctrl_get_active_map(aom_codec_alg_priv_t * ctx,va_list args)3707 static aom_codec_err_t ctrl_get_active_map(aom_codec_alg_priv_t *ctx,
3708                                            va_list args) {
3709   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
3710 
3711   if (map) {
3712     if (!av1_get_active_map(ctx->ppi->cpi, map->active_map, (int)map->rows,
3713                             (int)map->cols))
3714       return AOM_CODEC_OK;
3715     else
3716       return AOM_CODEC_INVALID_PARAM;
3717   } else {
3718     return AOM_CODEC_INVALID_PARAM;
3719   }
3720 }
3721 
ctrl_set_scale_mode(aom_codec_alg_priv_t * ctx,va_list args)3722 static aom_codec_err_t ctrl_set_scale_mode(aom_codec_alg_priv_t *ctx,
3723                                            va_list args) {
3724   aom_scaling_mode_t *const mode = va_arg(args, aom_scaling_mode_t *);
3725 
3726   if (mode) {
3727     AV1EncoderConfig *const oxcf =
3728         ctx->ppi->seq_params_locked ? &ctx->ppi->cpi->oxcf : &ctx->oxcf;
3729     const int res =
3730         av1_set_internal_size(oxcf, &ctx->ppi->cpi->resize_pending_params,
3731                               mode->h_scaling_mode, mode->v_scaling_mode);
3732     if (res == 0) {
3733       // update_encoder_cfg() is somewhat costly and this control may be called
3734       // multiple times, so update_encoder_cfg() is only called to ensure frame
3735       // and superblock sizes are updated before they're fixed by the first
3736       // encode call.
3737       if (ctx->ppi->seq_params_locked) {
3738         av1_check_fpmt_config(ctx->ppi, &ctx->ppi->cpi->oxcf);
3739         return AOM_CODEC_OK;
3740       }
3741       return update_encoder_cfg(ctx);
3742     }
3743     return AOM_CODEC_INVALID_PARAM;
3744   } else {
3745     return AOM_CODEC_INVALID_PARAM;
3746   }
3747 }
3748 
ctrl_set_spatial_layer_id(aom_codec_alg_priv_t * ctx,va_list args)3749 static aom_codec_err_t ctrl_set_spatial_layer_id(aom_codec_alg_priv_t *ctx,
3750                                                  va_list args) {
3751   const int spatial_layer_id = va_arg(args, int);
3752   if (spatial_layer_id >= MAX_NUM_SPATIAL_LAYERS)
3753     return AOM_CODEC_INVALID_PARAM;
3754   ctx->ppi->cpi->common.spatial_layer_id = spatial_layer_id;
3755   return AOM_CODEC_OK;
3756 }
3757 
ctrl_set_number_spatial_layers(aom_codec_alg_priv_t * ctx,va_list args)3758 static aom_codec_err_t ctrl_set_number_spatial_layers(aom_codec_alg_priv_t *ctx,
3759                                                       va_list args) {
3760   const int number_spatial_layers = va_arg(args, int);
3761   if (number_spatial_layers > MAX_NUM_SPATIAL_LAYERS)
3762     return AOM_CODEC_INVALID_PARAM;
3763   ctx->ppi->number_spatial_layers = number_spatial_layers;
3764   // update_encoder_cfg() is somewhat costly and this control may be called
3765   // multiple times, so update_encoder_cfg() is only called to ensure frame and
3766   // superblock sizes are updated before they're fixed by the first encode
3767   // call.
3768   if (!ctx->ppi->seq_params_locked) {
3769     return update_encoder_cfg(ctx);
3770   }
3771   return AOM_CODEC_OK;
3772 }
3773 
ctrl_set_layer_id(aom_codec_alg_priv_t * ctx,va_list args)3774 static aom_codec_err_t ctrl_set_layer_id(aom_codec_alg_priv_t *ctx,
3775                                          va_list args) {
3776   aom_svc_layer_id_t *const data = va_arg(args, aom_svc_layer_id_t *);
3777   ctx->ppi->cpi->common.spatial_layer_id = data->spatial_layer_id;
3778   ctx->ppi->cpi->common.temporal_layer_id = data->temporal_layer_id;
3779   ctx->ppi->cpi->svc.spatial_layer_id = data->spatial_layer_id;
3780   ctx->ppi->cpi->svc.temporal_layer_id = data->temporal_layer_id;
3781   return AOM_CODEC_OK;
3782 }
3783 
ctrl_set_svc_params(aom_codec_alg_priv_t * ctx,va_list args)3784 static aom_codec_err_t ctrl_set_svc_params(aom_codec_alg_priv_t *ctx,
3785                                            va_list args) {
3786   AV1_PRIMARY *const ppi = ctx->ppi;
3787   AV1_COMP *const cpi = ppi->cpi;
3788   aom_svc_params_t *const params = va_arg(args, aom_svc_params_t *);
3789   int64_t target_bandwidth = 0;
3790   ppi->number_spatial_layers = params->number_spatial_layers;
3791   ppi->number_temporal_layers = params->number_temporal_layers;
3792   cpi->svc.number_spatial_layers = params->number_spatial_layers;
3793   cpi->svc.number_temporal_layers = params->number_temporal_layers;
3794   if (ppi->number_spatial_layers > 1 || ppi->number_temporal_layers > 1) {
3795     unsigned int sl, tl;
3796     ctx->ppi->use_svc = 1;
3797     const int num_layers =
3798         ppi->number_spatial_layers * ppi->number_temporal_layers;
3799     for (int layer = 0; layer < num_layers; ++layer) {
3800       if (params->max_quantizers[layer] > 63 ||
3801           params->min_quantizers[layer] < 0 ||
3802           params->min_quantizers[layer] > params->max_quantizers[layer]) {
3803         return AOM_CODEC_INVALID_PARAM;
3804       }
3805     }
3806     if (!av1_alloc_layer_context(cpi, num_layers)) return AOM_CODEC_MEM_ERROR;
3807 
3808     for (sl = 0; sl < ppi->number_spatial_layers; ++sl) {
3809       for (tl = 0; tl < ppi->number_temporal_layers; ++tl) {
3810         const int layer = LAYER_IDS_TO_IDX(sl, tl, ppi->number_temporal_layers);
3811         LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
3812         lc->max_q = params->max_quantizers[layer];
3813         lc->min_q = params->min_quantizers[layer];
3814         lc->scaling_factor_num = AOMMAX(1, params->scaling_factor_num[sl]);
3815         lc->scaling_factor_den = AOMMAX(1, params->scaling_factor_den[sl]);
3816         const int layer_target_bitrate = params->layer_target_bitrate[layer];
3817         if (layer_target_bitrate > INT_MAX / 1000) {
3818           lc->layer_target_bitrate = INT_MAX;
3819         } else {
3820           lc->layer_target_bitrate = 1000 * layer_target_bitrate;
3821         }
3822         lc->framerate_factor = params->framerate_factor[tl];
3823         if (tl == ppi->number_temporal_layers - 1)
3824           target_bandwidth += lc->layer_target_bitrate;
3825       }
3826     }
3827 
3828     if (ppi->seq_params_locked) {
3829       AV1EncoderConfig *const oxcf = &cpi->oxcf;
3830       // Keep ctx->oxcf in sync in case further codec controls are made prior
3831       // to encoding.
3832       ctx->oxcf.rc_cfg.target_bandwidth = oxcf->rc_cfg.target_bandwidth =
3833           target_bandwidth;
3834       set_primary_rc_buffer_sizes(oxcf, ppi);
3835       av1_update_layer_context_change_config(cpi, target_bandwidth);
3836       check_reset_rc_flag(cpi);
3837     } else {
3838       // Note av1_init_layer_context() relies on cpi->oxcf. The order of that
3839       // call and the ones in the other half of this block (which
3840       // update_encoder_cfg() transitively makes) is important. So we keep
3841       // ctx->oxcf and cpi->oxcf in sync here as update_encoder_cfg() will
3842       // overwrite cpi->oxcf with ctx->oxcf.
3843       ctx->oxcf.rc_cfg.target_bandwidth = cpi->oxcf.rc_cfg.target_bandwidth =
3844           target_bandwidth;
3845       SequenceHeader *const seq_params = &ppi->seq_params;
3846       seq_params->operating_points_cnt_minus_1 =
3847           ppi->number_spatial_layers * ppi->number_temporal_layers - 1;
3848 
3849       av1_init_layer_context(cpi);
3850       // update_encoder_cfg() is somewhat costly and this control may be called
3851       // multiple times, so update_encoder_cfg() is only called to ensure frame
3852       // and superblock sizes are updated before they're fixed by the first
3853       // encode call.
3854       return update_encoder_cfg(ctx);
3855     }
3856   } else if (!ppi->seq_params_locked) {
3857     // Ensure frame and superblock sizes are updated.
3858     return update_encoder_cfg(ctx);
3859   }
3860   av1_check_fpmt_config(ctx->ppi, &ctx->ppi->cpi->oxcf);
3861   return AOM_CODEC_OK;
3862 }
3863 
ctrl_set_svc_ref_frame_config(aom_codec_alg_priv_t * ctx,va_list args)3864 static aom_codec_err_t ctrl_set_svc_ref_frame_config(aom_codec_alg_priv_t *ctx,
3865                                                      va_list args) {
3866   AV1_COMP *const cpi = ctx->ppi->cpi;
3867   aom_svc_ref_frame_config_t *const data =
3868       va_arg(args, aom_svc_ref_frame_config_t *);
3869   cpi->ppi->rtc_ref.set_ref_frame_config = 1;
3870   for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3871     if (data->reference[i] != 0 && data->reference[i] != 1)
3872       return AOM_CODEC_INVALID_PARAM;
3873     if (data->ref_idx[i] > 7 || data->ref_idx[i] < 0)
3874       return AOM_CODEC_INVALID_PARAM;
3875     cpi->ppi->rtc_ref.reference[i] = data->reference[i];
3876     cpi->ppi->rtc_ref.ref_idx[i] = data->ref_idx[i];
3877   }
3878   for (unsigned int i = 0; i < REF_FRAMES; ++i) {
3879     if (data->refresh[i] != 0 && data->refresh[i] != 1)
3880       return AOM_CODEC_INVALID_PARAM;
3881     cpi->ppi->rtc_ref.refresh[i] = data->refresh[i];
3882   }
3883   cpi->svc.use_flexible_mode = 1;
3884   cpi->svc.ksvc_fixed_mode = 0;
3885   return AOM_CODEC_OK;
3886 }
3887 
ctrl_set_svc_ref_frame_comp_pred(aom_codec_alg_priv_t * ctx,va_list args)3888 static aom_codec_err_t ctrl_set_svc_ref_frame_comp_pred(
3889     aom_codec_alg_priv_t *ctx, va_list args) {
3890   AV1_COMP *const cpi = ctx->ppi->cpi;
3891   aom_svc_ref_frame_comp_pred_t *const data =
3892       va_arg(args, aom_svc_ref_frame_comp_pred_t *);
3893   cpi->ppi->rtc_ref.ref_frame_comp[0] = data->use_comp_pred[0];
3894   cpi->ppi->rtc_ref.ref_frame_comp[1] = data->use_comp_pred[1];
3895   cpi->ppi->rtc_ref.ref_frame_comp[2] = data->use_comp_pred[2];
3896   return AOM_CODEC_OK;
3897 }
3898 
ctrl_set_tune_content(aom_codec_alg_priv_t * ctx,va_list args)3899 static aom_codec_err_t ctrl_set_tune_content(aom_codec_alg_priv_t *ctx,
3900                                              va_list args) {
3901   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3902   extra_cfg.content = CAST(AV1E_SET_TUNE_CONTENT, args);
3903   return update_extra_cfg(ctx, &extra_cfg);
3904 }
3905 
ctrl_set_cdf_update_mode(aom_codec_alg_priv_t * ctx,va_list args)3906 static aom_codec_err_t ctrl_set_cdf_update_mode(aom_codec_alg_priv_t *ctx,
3907                                                 va_list args) {
3908   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3909   extra_cfg.cdf_update_mode = CAST(AV1E_SET_CDF_UPDATE_MODE, args);
3910   return update_extra_cfg(ctx, &extra_cfg);
3911 }
3912 
ctrl_set_color_primaries(aom_codec_alg_priv_t * ctx,va_list args)3913 static aom_codec_err_t ctrl_set_color_primaries(aom_codec_alg_priv_t *ctx,
3914                                                 va_list args) {
3915   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3916   extra_cfg.color_primaries = CAST(AV1E_SET_COLOR_PRIMARIES, args);
3917   return update_extra_cfg(ctx, &extra_cfg);
3918 }
3919 
ctrl_set_transfer_characteristics(aom_codec_alg_priv_t * ctx,va_list args)3920 static aom_codec_err_t ctrl_set_transfer_characteristics(
3921     aom_codec_alg_priv_t *ctx, va_list args) {
3922   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3923   extra_cfg.transfer_characteristics =
3924       CAST(AV1E_SET_TRANSFER_CHARACTERISTICS, args);
3925   return update_extra_cfg(ctx, &extra_cfg);
3926 }
3927 
ctrl_set_matrix_coefficients(aom_codec_alg_priv_t * ctx,va_list args)3928 static aom_codec_err_t ctrl_set_matrix_coefficients(aom_codec_alg_priv_t *ctx,
3929                                                     va_list args) {
3930   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3931   extra_cfg.matrix_coefficients = CAST(AV1E_SET_MATRIX_COEFFICIENTS, args);
3932   return update_extra_cfg(ctx, &extra_cfg);
3933 }
3934 
ctrl_set_chroma_sample_position(aom_codec_alg_priv_t * ctx,va_list args)3935 static aom_codec_err_t ctrl_set_chroma_sample_position(
3936     aom_codec_alg_priv_t *ctx, va_list args) {
3937   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3938   extra_cfg.chroma_sample_position =
3939       CAST(AV1E_SET_CHROMA_SAMPLE_POSITION, args);
3940   return update_extra_cfg(ctx, &extra_cfg);
3941 }
3942 
ctrl_set_color_range(aom_codec_alg_priv_t * ctx,va_list args)3943 static aom_codec_err_t ctrl_set_color_range(aom_codec_alg_priv_t *ctx,
3944                                             va_list args) {
3945   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3946   extra_cfg.color_range = CAST(AV1E_SET_COLOR_RANGE, args);
3947   return update_extra_cfg(ctx, &extra_cfg);
3948 }
3949 
ctrl_set_render_size(aom_codec_alg_priv_t * ctx,va_list args)3950 static aom_codec_err_t ctrl_set_render_size(aom_codec_alg_priv_t *ctx,
3951                                             va_list args) {
3952   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3953   int *const render_size = va_arg(args, int *);
3954   extra_cfg.render_width = render_size[0];
3955   extra_cfg.render_height = render_size[1];
3956   return update_extra_cfg(ctx, &extra_cfg);
3957 }
3958 
ctrl_set_superblock_size(aom_codec_alg_priv_t * ctx,va_list args)3959 static aom_codec_err_t ctrl_set_superblock_size(aom_codec_alg_priv_t *ctx,
3960                                                 va_list args) {
3961   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3962   extra_cfg.superblock_size = CAST(AV1E_SET_SUPERBLOCK_SIZE, args);
3963   return update_extra_cfg(ctx, &extra_cfg);
3964 }
3965 
ctrl_set_chroma_subsampling_x(aom_codec_alg_priv_t * ctx,va_list args)3966 static aom_codec_err_t ctrl_set_chroma_subsampling_x(aom_codec_alg_priv_t *ctx,
3967                                                      va_list args) {
3968   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3969   extra_cfg.chroma_subsampling_x = CAST(AV1E_SET_CHROMA_SUBSAMPLING_X, args);
3970   return update_extra_cfg(ctx, &extra_cfg);
3971 }
3972 
ctrl_set_chroma_subsampling_y(aom_codec_alg_priv_t * ctx,va_list args)3973 static aom_codec_err_t ctrl_set_chroma_subsampling_y(aom_codec_alg_priv_t *ctx,
3974                                                      va_list args) {
3975   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3976   extra_cfg.chroma_subsampling_y = CAST(AV1E_SET_CHROMA_SUBSAMPLING_Y, args);
3977   return update_extra_cfg(ctx, &extra_cfg);
3978 }
3979 
encoder_set_option(aom_codec_alg_priv_t * ctx,const char * name,const char * value)3980 static aom_codec_err_t encoder_set_option(aom_codec_alg_priv_t *ctx,
3981                                           const char *name, const char *value) {
3982   if (ctx == NULL || name == NULL || value == NULL)
3983     return AOM_CODEC_INVALID_PARAM;
3984   struct av1_extracfg extra_cfg = ctx->extra_cfg;
3985   // Used to mock the argv with just one string "--{name}={value}"
3986   char *argv[2] = { NULL, "" };
3987   size_t len = strlen(name) + strlen(value) + 4;
3988   char *const err_string = ctx->ppi->error.detail;
3989 
3990 #if __STDC_VERSION__ >= 201112L
3991   // We use the keyword _Static_assert because clang-cl does not allow the
3992   // convenience macro static_assert to be used in function scope. See
3993   // https://bugs.llvm.org/show_bug.cgi?id=48904.
3994   _Static_assert(sizeof(ctx->ppi->error.detail) >= ARG_ERR_MSG_MAX_LEN,
3995                  "The size of the err_msg buffer for arg_match_helper must be "
3996                  "at least ARG_ERR_MSG_MAX_LEN");
3997 #else
3998   assert(sizeof(ctx->ppi->error.detail) >= ARG_ERR_MSG_MAX_LEN);
3999 #endif
4000 
4001   argv[0] = aom_malloc(len * sizeof(argv[1][0]));
4002   if (!argv[0]) return AOM_CODEC_MEM_ERROR;
4003   snprintf(argv[0], len, "--%s=%s", name, value);
4004   struct arg arg;
4005   aom_codec_err_t err = AOM_CODEC_OK;
4006 
4007   int match = 1;
4008   if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_keyframe_filtering,
4009                        argv, err_string)) {
4010     extra_cfg.enable_keyframe_filtering =
4011         arg_parse_uint_helper(&arg, err_string);
4012   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.min_gf_interval, argv,
4013                               err_string)) {
4014     extra_cfg.min_gf_interval = arg_parse_uint_helper(&arg, err_string);
4015   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_gf_interval, argv,
4016                               err_string)) {
4017     extra_cfg.max_gf_interval = arg_parse_uint_helper(&arg, err_string);
4018   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.gf_min_pyr_height,
4019                               argv, err_string)) {
4020     extra_cfg.gf_min_pyr_height = arg_parse_uint_helper(&arg, err_string);
4021   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.gf_max_pyr_height,
4022                               argv, err_string)) {
4023     extra_cfg.gf_max_pyr_height = arg_parse_uint_helper(&arg, err_string);
4024   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.cpu_used_av1, argv,
4025                               err_string)) {
4026     extra_cfg.cpu_used = arg_parse_uint_helper(&arg, err_string);
4027   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.auto_altref, argv,
4028                               err_string)) {
4029     extra_cfg.enable_auto_alt_ref = arg_parse_uint_helper(&arg, err_string);
4030   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.noise_sens, argv,
4031                               err_string)) {
4032     extra_cfg.noise_sensitivity = arg_parse_uint_helper(&arg, err_string);
4033   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.sharpness, argv,
4034                               err_string)) {
4035     extra_cfg.sharpness = arg_parse_uint_helper(&arg, err_string);
4036   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.static_thresh, argv,
4037                               err_string)) {
4038     extra_cfg.static_thresh = arg_parse_uint_helper(&arg, err_string);
4039   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.rowmtarg, argv,
4040                               err_string)) {
4041     extra_cfg.row_mt = arg_parse_uint_helper(&arg, err_string);
4042   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.fpmtarg, argv,
4043                               err_string)) {
4044     extra_cfg.fp_mt = arg_parse_uint_helper(&arg, err_string);
4045   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tile_cols, argv,
4046                               err_string)) {
4047     extra_cfg.tile_columns = arg_parse_uint_helper(&arg, err_string);
4048     if (extra_cfg.auto_tiles) {
4049       snprintf(err_string, ARG_ERR_MSG_MAX_LEN,
4050                "Cannot set tile-cols because auto-tiles is already set.");
4051       err = AOM_CODEC_INVALID_PARAM;
4052     }
4053   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tile_rows, argv,
4054                               err_string)) {
4055     extra_cfg.tile_rows = arg_parse_uint_helper(&arg, err_string);
4056     if (extra_cfg.auto_tiles) {
4057       snprintf(err_string, ARG_ERR_MSG_MAX_LEN,
4058                "Cannot set tile-rows because auto-tiles is already set.");
4059       err = AOM_CODEC_INVALID_PARAM;
4060     }
4061   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.auto_tiles, argv,
4062                               err_string)) {
4063     extra_cfg.auto_tiles = arg_parse_uint_helper(&arg, err_string);
4064   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_tpl_model,
4065                               argv, err_string)) {
4066     extra_cfg.enable_tpl_model = arg_parse_uint_helper(&arg, err_string);
4067   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.arnr_maxframes, argv,
4068                               err_string)) {
4069     extra_cfg.arnr_max_frames = arg_parse_uint_helper(&arg, err_string);
4070   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.arnr_strength, argv,
4071                               err_string)) {
4072     extra_cfg.arnr_strength = arg_parse_uint_helper(&arg, err_string);
4073   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tune_metric, argv,
4074                               err_string)) {
4075     extra_cfg.tuning = arg_parse_enum_helper(&arg, err_string);
4076   }
4077 #if CONFIG_TUNE_VMAF
4078   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.vmaf_model_path, argv,
4079                             err_string)) {
4080     err = allocate_and_set_string(value, default_extra_cfg[0].vmaf_model_path,
4081                                   &extra_cfg.vmaf_model_path, err_string);
4082   }
4083 #endif
4084   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.partition_info_path,
4085                             argv, err_string)) {
4086     err =
4087         allocate_and_set_string(value, default_extra_cfg[0].partition_info_path,
4088                                 &extra_cfg.partition_info_path, err_string);
4089   } else if (arg_match_helper(&arg,
4090                               &g_av1_codec_arg_defs.enable_rate_guide_deltaq,
4091                               argv, err_string)) {
4092     extra_cfg.enable_rate_guide_deltaq =
4093         arg_parse_uint_helper(&arg, err_string);
4094   } else if (arg_match_helper(&arg,
4095                               &g_av1_codec_arg_defs.rate_distribution_info,
4096                               argv, err_string)) {
4097     err = allocate_and_set_string(
4098         value, default_extra_cfg[0].rate_distribution_info,
4099         &extra_cfg.rate_distribution_info, err_string);
4100   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.dist_metric, argv,
4101                               err_string)) {
4102     extra_cfg.dist_metric = arg_parse_enum_helper(&arg, err_string);
4103   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.cq_level, argv,
4104                               err_string)) {
4105     extra_cfg.cq_level = arg_parse_uint_helper(&arg, err_string);
4106   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_intra_rate_pct,
4107                               argv, err_string)) {
4108     extra_cfg.rc_max_intra_bitrate_pct =
4109         arg_parse_uint_helper(&arg, err_string);
4110   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_inter_rate_pct,
4111                               argv, err_string)) {
4112     extra_cfg.rc_max_inter_bitrate_pct =
4113         arg_parse_uint_helper(&arg, err_string);
4114   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.gf_cbr_boost_pct,
4115                               argv, err_string)) {
4116     extra_cfg.gf_cbr_boost_pct = arg_parse_uint_helper(&arg, err_string);
4117   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.lossless, argv,
4118                               err_string)) {
4119     extra_cfg.lossless = arg_parse_uint_helper(&arg, err_string);
4120   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_cdef, argv,
4121                               err_string)) {
4122     extra_cfg.enable_cdef = arg_parse_uint_helper(&arg, err_string);
4123   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_restoration,
4124                               argv, err_string)) {
4125     extra_cfg.enable_restoration = arg_parse_uint_helper(&arg, err_string);
4126   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.force_video_mode,
4127                               argv, err_string)) {
4128     extra_cfg.force_video_mode = arg_parse_uint_helper(&arg, err_string);
4129   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_obmc, argv,
4130                               err_string)) {
4131     extra_cfg.enable_obmc = arg_parse_uint_helper(&arg, err_string);
4132   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.disable_trellis_quant,
4133                               argv, err_string)) {
4134     extra_cfg.disable_trellis_quant = arg_parse_uint_helper(&arg, err_string);
4135   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_qm, argv,
4136                               err_string)) {
4137     extra_cfg.enable_qm = arg_parse_uint_helper(&arg, err_string);
4138   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.qm_max, argv,
4139                               err_string)) {
4140     extra_cfg.qm_max = arg_parse_uint_helper(&arg, err_string);
4141   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.qm_min, argv,
4142                               err_string)) {
4143     extra_cfg.qm_min = arg_parse_uint_helper(&arg, err_string);
4144   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.num_tg, argv,
4145                               err_string)) {
4146     extra_cfg.num_tg = arg_parse_uint_helper(&arg, err_string);
4147   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.mtu_size, argv,
4148                               err_string)) {
4149     extra_cfg.mtu_size = arg_parse_uint_helper(&arg, err_string);
4150   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.timing_info, argv,
4151                               err_string)) {
4152     extra_cfg.timing_info_type = arg_parse_enum_helper(&arg, err_string);
4153   } else if (arg_match_helper(&arg,
4154                               &g_av1_codec_arg_defs.frame_parallel_decoding,
4155                               argv, err_string)) {
4156     extra_cfg.frame_parallel_decoding_mode =
4157         arg_parse_uint_helper(&arg, err_string);
4158   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_dual_filter,
4159                               argv, err_string)) {
4160     extra_cfg.enable_dual_filter = arg_parse_uint_helper(&arg, err_string);
4161   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_chroma_deltaq,
4162                               argv, err_string)) {
4163     extra_cfg.enable_chroma_deltaq = arg_parse_uint_helper(&arg, err_string);
4164   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.aq_mode, argv,
4165                               err_string)) {
4166     extra_cfg.aq_mode = arg_parse_uint_helper(&arg, err_string);
4167   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.deltaq_mode, argv,
4168                               err_string)) {
4169     extra_cfg.deltaq_mode = arg_parse_uint_helper(&arg, err_string);
4170   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.deltaq_strength, argv,
4171                               err_string)) {
4172     extra_cfg.deltaq_strength = arg_parse_uint_helper(&arg, err_string);
4173   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.deltalf_mode, argv,
4174                               err_string)) {
4175     extra_cfg.deltalf_mode = arg_parse_uint_helper(&arg, err_string);
4176   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.frame_periodic_boost,
4177                               argv, err_string)) {
4178     extra_cfg.frame_periodic_boost = arg_parse_uint_helper(&arg, err_string);
4179   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tune_content, argv,
4180                               err_string)) {
4181     extra_cfg.content = arg_parse_enum_helper(&arg, err_string);
4182   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.input_color_primaries,
4183                               argv, err_string)) {
4184     extra_cfg.color_primaries = arg_parse_enum_helper(&arg, err_string);
4185   } else if (arg_match_helper(
4186                  &arg, &g_av1_codec_arg_defs.input_transfer_characteristics,
4187                  argv, err_string)) {
4188     extra_cfg.transfer_characteristics =
4189         arg_parse_enum_helper(&arg, err_string);
4190   } else if (arg_match_helper(&arg,
4191                               &g_av1_codec_arg_defs.input_matrix_coefficients,
4192                               argv, err_string)) {
4193     extra_cfg.matrix_coefficients = arg_parse_enum_helper(&arg, err_string);
4194   } else if (arg_match_helper(
4195                  &arg, &g_av1_codec_arg_defs.input_chroma_sample_position, argv,
4196                  err_string)) {
4197     extra_cfg.chroma_sample_position = arg_parse_enum_helper(&arg, err_string);
4198   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.superblock_size, argv,
4199                               err_string)) {
4200     extra_cfg.superblock_size = arg_parse_enum_helper(&arg, err_string);
4201   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.error_resilient_mode,
4202                               argv, err_string)) {
4203     extra_cfg.error_resilient_mode = arg_parse_int_helper(&arg, err_string);
4204   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.sframe_mode, argv,
4205                               err_string)) {
4206     extra_cfg.s_frame_mode = arg_parse_int_helper(&arg, err_string);
4207   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.film_grain_test, argv,
4208                               err_string)) {
4209     extra_cfg.film_grain_test_vector = arg_parse_int_helper(&arg, err_string);
4210   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.film_grain_table,
4211                               argv, err_string)) {
4212     if (value == NULL) {
4213       // this parameter allows NULL as its value
4214       extra_cfg.film_grain_table_filename = value;
4215     } else {
4216       err = allocate_and_set_string(
4217           value, default_extra_cfg[0].film_grain_table_filename,
4218           &extra_cfg.film_grain_table_filename, err_string);
4219     }
4220   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.cdf_update_mode, argv,
4221                               err_string)) {
4222     extra_cfg.cdf_update_mode = arg_parse_int_helper(&arg, err_string);
4223   } else if (arg_match_helper(&arg,
4224                               &g_av1_codec_arg_defs.enable_rect_partitions,
4225                               argv, err_string)) {
4226     extra_cfg.enable_rect_partitions = arg_parse_int_helper(&arg, err_string);
4227   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_ab_partitions,
4228                               argv, err_string)) {
4229     extra_cfg.enable_ab_partitions = arg_parse_int_helper(&arg, err_string);
4230   } else if (arg_match_helper(&arg,
4231                               &g_av1_codec_arg_defs.enable_1to4_partitions,
4232                               argv, err_string)) {
4233     extra_cfg.enable_1to4_partitions = arg_parse_int_helper(&arg, err_string);
4234   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.min_partition_size,
4235                               argv, err_string)) {
4236     extra_cfg.min_partition_size = arg_parse_int_helper(&arg, err_string);
4237   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_partition_size,
4238                               argv, err_string)) {
4239     extra_cfg.max_partition_size = arg_parse_int_helper(&arg, err_string);
4240   } else if (arg_match_helper(&arg,
4241                               &g_av1_codec_arg_defs.enable_intra_edge_filter,
4242                               argv, err_string)) {
4243     extra_cfg.enable_intra_edge_filter =
4244         arg_parse_uint_helper(&arg, err_string);
4245   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_order_hint,
4246                               argv, err_string)) {
4247     extra_cfg.enable_order_hint = arg_parse_int_helper(&arg, err_string);
4248   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_tx64, argv,
4249                               err_string)) {
4250     extra_cfg.enable_tx64 = arg_parse_int_helper(&arg, err_string);
4251   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_flip_idtx,
4252                               argv, err_string)) {
4253     extra_cfg.enable_flip_idtx = arg_parse_int_helper(&arg, err_string);
4254   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_rect_tx, argv,
4255                               err_string)) {
4256     extra_cfg.enable_rect_tx = arg_parse_int_helper(&arg, err_string);
4257   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_dist_wtd_comp,
4258                               argv, err_string)) {
4259     extra_cfg.enable_dist_wtd_comp = arg_parse_int_helper(&arg, err_string);
4260   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.max_reference_frames,
4261                               argv, err_string)) {
4262     extra_cfg.max_reference_frames = arg_parse_int_helper(&arg, err_string);
4263   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.reduced_reference_set,
4264                               argv, err_string)) {
4265     extra_cfg.enable_reduced_reference_set =
4266         arg_parse_int_helper(&arg, err_string);
4267   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_ref_frame_mvs,
4268                               argv, err_string)) {
4269     extra_cfg.enable_ref_frame_mvs = arg_parse_int_helper(&arg, err_string);
4270   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_masked_comp,
4271                               argv, err_string)) {
4272     extra_cfg.enable_masked_comp = arg_parse_int_helper(&arg, err_string);
4273   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_onesided_comp,
4274                               argv, err_string)) {
4275     extra_cfg.enable_onesided_comp = arg_parse_int_helper(&arg, err_string);
4276   } else if (arg_match_helper(&arg,
4277                               &g_av1_codec_arg_defs.enable_interintra_comp,
4278                               argv, err_string)) {
4279     extra_cfg.enable_interintra_comp = arg_parse_int_helper(&arg, err_string);
4280   } else if (arg_match_helper(&arg,
4281                               &g_av1_codec_arg_defs.enable_smooth_interintra,
4282                               argv, err_string)) {
4283     extra_cfg.enable_smooth_interintra = arg_parse_int_helper(&arg, err_string);
4284   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_diff_wtd_comp,
4285                               argv, err_string)) {
4286     extra_cfg.enable_diff_wtd_comp = arg_parse_int_helper(&arg, err_string);
4287   } else if (arg_match_helper(&arg,
4288                               &g_av1_codec_arg_defs.enable_interinter_wedge,
4289                               argv, err_string)) {
4290     extra_cfg.enable_interinter_wedge = arg_parse_int_helper(&arg, err_string);
4291   } else if (arg_match_helper(&arg,
4292                               &g_av1_codec_arg_defs.enable_interintra_wedge,
4293                               argv, err_string)) {
4294     extra_cfg.enable_interintra_wedge = arg_parse_int_helper(&arg, err_string);
4295   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_global_motion,
4296                               argv, err_string)) {
4297     extra_cfg.enable_global_motion = arg_parse_int_helper(&arg, err_string);
4298   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_warped_motion,
4299                               argv, err_string)) {
4300     extra_cfg.enable_warped_motion = arg_parse_int_helper(&arg, err_string);
4301   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_filter_intra,
4302                               argv, err_string)) {
4303     extra_cfg.enable_filter_intra = arg_parse_int_helper(&arg, err_string);
4304   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_smooth_intra,
4305                               argv, err_string)) {
4306     extra_cfg.enable_smooth_intra = arg_parse_int_helper(&arg, err_string);
4307   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_paeth_intra,
4308                               argv, err_string)) {
4309     extra_cfg.enable_paeth_intra = arg_parse_int_helper(&arg, err_string);
4310   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_cfl_intra,
4311                               argv, err_string)) {
4312     extra_cfg.enable_cfl_intra = arg_parse_int_helper(&arg, err_string);
4313   } else if (arg_match_helper(&arg,
4314                               &g_av1_codec_arg_defs.enable_directional_intra,
4315                               argv, err_string)) {
4316     extra_cfg.enable_directional_intra = arg_parse_int_helper(&arg, err_string);
4317   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_diagonal_intra,
4318                               argv, err_string)) {
4319     extra_cfg.enable_diagonal_intra = arg_parse_int_helper(&arg, err_string);
4320   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_overlay, argv,
4321                               err_string)) {
4322     extra_cfg.enable_overlay = arg_parse_int_helper(&arg, err_string);
4323   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_palette, argv,
4324                               err_string)) {
4325     extra_cfg.enable_palette = arg_parse_int_helper(&arg, err_string);
4326   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_intrabc, argv,
4327                               err_string)) {
4328     extra_cfg.enable_intrabc = arg_parse_int_helper(&arg, err_string);
4329   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_angle_delta,
4330                               argv, err_string)) {
4331     extra_cfg.enable_angle_delta = arg_parse_int_helper(&arg, err_string);
4332   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.reduced_tx_type_set,
4333                               argv, err_string)) {
4334     extra_cfg.reduced_tx_type_set = arg_parse_int_helper(&arg, err_string);
4335   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.use_intra_dct_only,
4336                               argv, err_string)) {
4337     extra_cfg.use_intra_dct_only = arg_parse_int_helper(&arg, err_string);
4338   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.use_inter_dct_only,
4339                               argv, err_string)) {
4340     extra_cfg.use_inter_dct_only = arg_parse_int_helper(&arg, err_string);
4341   } else if (arg_match_helper(&arg,
4342                               &g_av1_codec_arg_defs.use_intra_default_tx_only,
4343                               argv, err_string)) {
4344     extra_cfg.use_intra_default_tx_only =
4345         arg_parse_int_helper(&arg, err_string);
4346   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.quant_b_adapt, argv,
4347                               err_string)) {
4348     extra_cfg.quant_b_adapt = arg_parse_int_helper(&arg, err_string);
4349   } else if (arg_match_helper(&arg,
4350                               &g_av1_codec_arg_defs.vbr_corpus_complexity_lap,
4351                               argv, err_string)) {
4352     extra_cfg.vbr_corpus_complexity_lap =
4353         arg_parse_uint_helper(&arg, err_string);
4354   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.set_tier_mask, argv,
4355                               err_string)) {
4356     extra_cfg.tier_mask = arg_parse_uint_helper(&arg, err_string);
4357   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.set_min_cr, argv,
4358                               err_string)) {
4359     extra_cfg.min_cr = arg_parse_uint_helper(&arg, err_string);
4360   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.coeff_cost_upd_freq,
4361                               argv, err_string)) {
4362     extra_cfg.coeff_cost_upd_freq = arg_parse_uint_helper(&arg, err_string);
4363   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.mode_cost_upd_freq,
4364                               argv, err_string)) {
4365     extra_cfg.mode_cost_upd_freq = arg_parse_uint_helper(&arg, err_string);
4366   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.mv_cost_upd_freq,
4367                               argv, err_string)) {
4368     extra_cfg.mv_cost_upd_freq = arg_parse_uint_helper(&arg, err_string);
4369   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.dv_cost_upd_freq,
4370                               argv, err_string)) {
4371     extra_cfg.dv_cost_upd_freq = arg_parse_uint_helper(&arg, err_string);
4372   }
4373 #if CONFIG_DENOISE
4374   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.denoise_noise_level,
4375                             argv, err_string)) {
4376     extra_cfg.noise_level =
4377         (float)arg_parse_int_helper(&arg, err_string) / 10.0f;
4378   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.denoise_block_size,
4379                               argv, err_string)) {
4380     extra_cfg.noise_block_size = arg_parse_uint_helper(&arg, err_string);
4381   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.enable_dnl_denoising,
4382                               argv, err_string)) {
4383     extra_cfg.enable_dnl_denoising = arg_parse_uint_helper(&arg, err_string);
4384   }
4385 #endif
4386   else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.target_seq_level_idx,
4387                             argv, err_string)) {
4388     const int val = arg_parse_int_helper(&arg, err_string);
4389     const int level = val % 100;
4390     const int operating_point_idx = val / 100;
4391     if (operating_point_idx < 0 ||
4392         operating_point_idx >= MAX_NUM_OPERATING_POINTS) {
4393       snprintf(err_string, ARG_ERR_MSG_MAX_LEN,
4394                "Invalid operating point index: %d", operating_point_idx);
4395       err = AOM_CODEC_INVALID_PARAM;
4396     } else {
4397       extra_cfg.target_seq_level_idx[operating_point_idx] = (AV1_LEVEL)level;
4398     }
4399   } else if (arg_match_helper(&arg,
4400                               &g_av1_codec_arg_defs.input_chroma_subsampling_x,
4401                               argv, err_string)) {
4402     extra_cfg.chroma_subsampling_x = arg_parse_uint_helper(&arg, err_string);
4403   } else if (arg_match_helper(&arg,
4404                               &g_av1_codec_arg_defs.input_chroma_subsampling_y,
4405                               argv, err_string)) {
4406     extra_cfg.chroma_subsampling_y = arg_parse_uint_helper(&arg, err_string);
4407   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.passes, argv,
4408                               err_string)) {
4409     extra_cfg.passes = arg_parse_int_helper(&arg, err_string);
4410   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.fwd_kf_dist, argv,
4411                               err_string)) {
4412     extra_cfg.fwd_kf_dist = arg_parse_int_helper(&arg, err_string);
4413   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.two_pass_output, argv,
4414                               err_string)) {
4415     err = allocate_and_set_string(value, default_extra_cfg[0].two_pass_output,
4416                                   &extra_cfg.two_pass_output, err_string);
4417   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.second_pass_log, argv,
4418                               err_string)) {
4419     err = allocate_and_set_string(value, default_extra_cfg[0].second_pass_log,
4420                                   &extra_cfg.second_pass_log, err_string);
4421   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.loopfilter_control,
4422                               argv, err_string)) {
4423     extra_cfg.loopfilter_control = arg_parse_int_helper(&arg, err_string);
4424   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.auto_intra_tools_off,
4425                               argv, err_string)) {
4426     extra_cfg.auto_intra_tools_off = arg_parse_uint_helper(&arg, err_string);
4427   } else if (arg_match_helper(&arg,
4428                               &g_av1_codec_arg_defs.strict_level_conformance,
4429                               argv, err_string)) {
4430     extra_cfg.strict_level_conformance = arg_parse_int_helper(&arg, err_string);
4431   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.sb_qp_sweep, argv,
4432                               err_string)) {
4433     extra_cfg.sb_qp_sweep = arg_parse_int_helper(&arg, err_string);
4434   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.kf_max_pyr_height,
4435                               argv, err_string)) {
4436     extra_cfg.kf_max_pyr_height = arg_parse_int_helper(&arg, err_string);
4437   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tile_width, argv,
4438                               err_string)) {
4439     ctx->cfg.tile_width_count = arg_parse_list_helper(
4440         &arg, ctx->cfg.tile_widths, MAX_TILE_WIDTHS, err_string);
4441   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.tile_height, argv,
4442                               err_string)) {
4443     ctx->cfg.tile_height_count = arg_parse_list_helper(
4444         &arg, ctx->cfg.tile_heights, MAX_TILE_HEIGHTS, err_string);
4445   } else {
4446     match = 0;
4447     snprintf(err_string, ARG_ERR_MSG_MAX_LEN, "Cannot find aom option %s",
4448              name);
4449   }
4450   aom_free(argv[0]);
4451 
4452   if (err != AOM_CODEC_OK) {
4453     ctx->base.err_detail = err_string;
4454     return err;
4455   }
4456 
4457   if (strlen(err_string) != 0) {
4458     ctx->base.err_detail = err_string;
4459     return AOM_CODEC_INVALID_PARAM;
4460   }
4461 
4462   ctx->base.err_detail = NULL;
4463 
4464   if (!match) {
4465     return AOM_CODEC_INVALID_PARAM;
4466   }
4467   return update_extra_cfg(ctx, &extra_cfg);
4468 }
4469 
ctrl_get_seq_level_idx(aom_codec_alg_priv_t * ctx,va_list args)4470 static aom_codec_err_t ctrl_get_seq_level_idx(aom_codec_alg_priv_t *ctx,
4471                                               va_list args) {
4472   int *const arg = va_arg(args, int *);
4473   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
4474   return av1_get_seq_level_idx(&ctx->ppi->seq_params, &ctx->ppi->level_params,
4475                                arg);
4476 }
4477 
ctrl_get_target_seq_level_idx(aom_codec_alg_priv_t * ctx,va_list args)4478 static aom_codec_err_t ctrl_get_target_seq_level_idx(aom_codec_alg_priv_t *ctx,
4479                                                      va_list args) {
4480   int *const arg = va_arg(args, int *);
4481   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
4482   return av1_get_target_seq_level_idx(&ctx->ppi->seq_params,
4483                                       &ctx->ppi->level_params, arg);
4484 }
4485 
ctrl_get_num_operating_points(aom_codec_alg_priv_t * ctx,va_list args)4486 static aom_codec_err_t ctrl_get_num_operating_points(aom_codec_alg_priv_t *ctx,
4487                                                      va_list args) {
4488   int *const arg = va_arg(args, int *);
4489   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
4490   *arg = ctx->ppi->seq_params.operating_points_cnt_minus_1 + 1;
4491   return AOM_CODEC_OK;
4492 }
4493 
ctrl_get_luma_cdef_strength(aom_codec_alg_priv_t * ctx,va_list args)4494 static aom_codec_err_t ctrl_get_luma_cdef_strength(aom_codec_alg_priv_t *ctx,
4495                                                    va_list args) {
4496   int *arg = va_arg(args, int *);
4497   AV1_COMMON const *cm = &ctx->ppi->cpi->common;
4498   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
4499   memcpy(arg, cm->cdef_info.cdef_strengths, CDEF_MAX_STRENGTHS * sizeof(*arg));
4500 
4501   return AOM_CODEC_OK;
4502 }
4503 
ctrl_get_high_motion_content_screen_rtc(aom_codec_alg_priv_t * ctx,va_list args)4504 static aom_codec_err_t ctrl_get_high_motion_content_screen_rtc(
4505     aom_codec_alg_priv_t *ctx, va_list args) {
4506   int *arg = va_arg(args, int *);
4507   AV1_COMP *const cpi = ctx->ppi->cpi;
4508   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
4509   *arg = cpi->rc.high_motion_content_screen_rtc;
4510   return AOM_CODEC_OK;
4511 }
4512 
4513 static aom_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
4514   { AV1_COPY_REFERENCE, ctrl_copy_reference },
4515   { AOME_USE_REFERENCE, ctrl_use_reference },
4516 
4517   // Setters
4518   { AV1_SET_REFERENCE, ctrl_set_reference },
4519   { AOME_SET_ROI_MAP, ctrl_set_roi_map },
4520   { AOME_SET_ACTIVEMAP, ctrl_set_active_map },
4521   { AOME_SET_SCALEMODE, ctrl_set_scale_mode },
4522   { AOME_SET_SPATIAL_LAYER_ID, ctrl_set_spatial_layer_id },
4523   { AOME_SET_CPUUSED, ctrl_set_cpuused },
4524   { AOME_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
4525   { AOME_SET_ENABLEAUTOBWDREF, ctrl_set_enable_auto_bwd_ref },
4526   { AOME_SET_SHARPNESS, ctrl_set_sharpness },
4527   { AOME_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
4528   { AV1E_SET_ROW_MT, ctrl_set_row_mt },
4529   { AV1E_SET_FP_MT, ctrl_set_fp_mt },
4530   { AV1E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
4531   { AV1E_SET_TILE_ROWS, ctrl_set_tile_rows },
4532   { AV1E_SET_ENABLE_TPL_MODEL, ctrl_set_enable_tpl_model },
4533   { AV1E_SET_ENABLE_KEYFRAME_FILTERING, ctrl_set_enable_keyframe_filtering },
4534   { AOME_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
4535   { AOME_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
4536   { AOME_SET_TUNING, ctrl_set_tuning },
4537   { AOME_SET_CQ_LEVEL, ctrl_set_cq_level },
4538   { AOME_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
4539   { AOME_SET_NUMBER_SPATIAL_LAYERS, ctrl_set_number_spatial_layers },
4540   { AV1E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
4541   { AV1E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
4542   { AV1E_SET_LOSSLESS, ctrl_set_lossless },
4543   { AV1E_SET_ENABLE_CDEF, ctrl_set_enable_cdef },
4544   { AV1E_SET_ENABLE_RESTORATION, ctrl_set_enable_restoration },
4545   { AV1E_SET_FORCE_VIDEO_MODE, ctrl_set_force_video_mode },
4546   { AV1E_SET_ENABLE_OBMC, ctrl_set_enable_obmc },
4547   { AV1E_SET_DISABLE_TRELLIS_QUANT, ctrl_set_disable_trellis_quant },
4548   { AV1E_SET_ENABLE_QM, ctrl_set_enable_qm },
4549   { AV1E_SET_QM_Y, ctrl_set_qm_y },
4550   { AV1E_SET_QM_U, ctrl_set_qm_u },
4551   { AV1E_SET_QM_V, ctrl_set_qm_v },
4552   { AV1E_SET_QM_MIN, ctrl_set_qm_min },
4553   { AV1E_SET_QM_MAX, ctrl_set_qm_max },
4554   { AV1E_SET_NUM_TG, ctrl_set_num_tg },
4555   { AV1E_SET_MTU, ctrl_set_mtu },
4556   { AV1E_SET_TIMING_INFO_TYPE, ctrl_set_timing_info_type },
4557   { AV1E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
4558   { AV1E_SET_ERROR_RESILIENT_MODE, ctrl_set_error_resilient_mode },
4559   { AV1E_SET_S_FRAME_MODE, ctrl_set_s_frame_mode },
4560   { AV1E_SET_ENABLE_RECT_PARTITIONS, ctrl_set_enable_rect_partitions },
4561   { AV1E_SET_ENABLE_AB_PARTITIONS, ctrl_set_enable_ab_partitions },
4562   { AV1E_SET_ENABLE_1TO4_PARTITIONS, ctrl_set_enable_1to4_partitions },
4563   { AV1E_SET_MIN_PARTITION_SIZE, ctrl_set_min_partition_size },
4564   { AV1E_SET_MAX_PARTITION_SIZE, ctrl_set_max_partition_size },
4565   { AV1E_SET_ENABLE_DUAL_FILTER, ctrl_set_enable_dual_filter },
4566   { AV1E_SET_ENABLE_CHROMA_DELTAQ, ctrl_set_enable_chroma_deltaq },
4567   { AV1E_SET_ENABLE_INTRA_EDGE_FILTER, ctrl_set_enable_intra_edge_filter },
4568   { AV1E_SET_ENABLE_ORDER_HINT, ctrl_set_enable_order_hint },
4569   { AV1E_SET_ENABLE_TX64, ctrl_set_enable_tx64 },
4570   { AV1E_SET_ENABLE_FLIP_IDTX, ctrl_set_enable_flip_idtx },
4571   { AV1E_SET_ENABLE_RECT_TX, ctrl_set_enable_rect_tx },
4572   { AV1E_SET_ENABLE_DIST_WTD_COMP, ctrl_set_enable_dist_wtd_comp },
4573   { AV1E_SET_MAX_REFERENCE_FRAMES, ctrl_set_max_reference_frames },
4574   { AV1E_SET_REDUCED_REFERENCE_SET, ctrl_set_enable_reduced_reference_set },
4575   { AV1E_SET_ENABLE_REF_FRAME_MVS, ctrl_set_enable_ref_frame_mvs },
4576   { AV1E_SET_ALLOW_REF_FRAME_MVS, ctrl_set_allow_ref_frame_mvs },
4577   { AV1E_SET_ENABLE_MASKED_COMP, ctrl_set_enable_masked_comp },
4578   { AV1E_SET_ENABLE_ONESIDED_COMP, ctrl_set_enable_onesided_comp },
4579   { AV1E_SET_ENABLE_INTERINTRA_COMP, ctrl_set_enable_interintra_comp },
4580   { AV1E_SET_ENABLE_SMOOTH_INTERINTRA, ctrl_set_enable_smooth_interintra },
4581   { AV1E_SET_ENABLE_DIFF_WTD_COMP, ctrl_set_enable_diff_wtd_comp },
4582   { AV1E_SET_ENABLE_INTERINTER_WEDGE, ctrl_set_enable_interinter_wedge },
4583   { AV1E_SET_ENABLE_INTERINTRA_WEDGE, ctrl_set_enable_interintra_wedge },
4584   { AV1E_SET_ENABLE_GLOBAL_MOTION, ctrl_set_enable_global_motion },
4585   { AV1E_SET_ENABLE_WARPED_MOTION, ctrl_set_enable_warped_motion },
4586   { AV1E_SET_ALLOW_WARPED_MOTION, ctrl_set_allow_warped_motion },
4587   { AV1E_SET_ENABLE_FILTER_INTRA, ctrl_set_enable_filter_intra },
4588   { AV1E_SET_ENABLE_SMOOTH_INTRA, ctrl_set_enable_smooth_intra },
4589   { AV1E_SET_ENABLE_PAETH_INTRA, ctrl_set_enable_paeth_intra },
4590   { AV1E_SET_ENABLE_CFL_INTRA, ctrl_set_enable_cfl_intra },
4591   { AV1E_SET_ENABLE_DIRECTIONAL_INTRA, ctrl_set_enable_directional_intra },
4592   { AV1E_SET_ENABLE_DIAGONAL_INTRA, ctrl_set_enable_diagonal_intra },
4593   { AV1E_SET_ENABLE_SUPERRES, ctrl_set_enable_superres },
4594   { AV1E_SET_ENABLE_OVERLAY, ctrl_set_enable_overlay },
4595   { AV1E_SET_ENABLE_PALETTE, ctrl_set_enable_palette },
4596   { AV1E_SET_ENABLE_INTRABC, ctrl_set_enable_intrabc },
4597   { AV1E_SET_ENABLE_ANGLE_DELTA, ctrl_set_enable_angle_delta },
4598   { AV1E_SET_AQ_MODE, ctrl_set_aq_mode },
4599   { AV1E_SET_REDUCED_TX_TYPE_SET, ctrl_set_reduced_tx_type_set },
4600   { AV1E_SET_INTRA_DCT_ONLY, ctrl_set_intra_dct_only },
4601   { AV1E_SET_INTER_DCT_ONLY, ctrl_set_inter_dct_only },
4602   { AV1E_SET_INTRA_DEFAULT_TX_ONLY, ctrl_set_intra_default_tx_only },
4603   { AV1E_SET_QUANT_B_ADAPT, ctrl_set_quant_b_adapt },
4604   { AV1E_SET_COEFF_COST_UPD_FREQ, ctrl_set_coeff_cost_upd_freq },
4605   { AV1E_SET_MODE_COST_UPD_FREQ, ctrl_set_mode_cost_upd_freq },
4606   { AV1E_SET_MV_COST_UPD_FREQ, ctrl_set_mv_cost_upd_freq },
4607   { AV1E_SET_DELTAQ_MODE, ctrl_set_deltaq_mode },
4608   { AV1E_SET_DELTAQ_STRENGTH, ctrl_set_deltaq_strength },
4609   { AV1E_SET_DELTALF_MODE, ctrl_set_deltalf_mode },
4610   { AV1E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
4611   { AV1E_SET_TUNE_CONTENT, ctrl_set_tune_content },
4612   { AV1E_SET_CDF_UPDATE_MODE, ctrl_set_cdf_update_mode },
4613   { AV1E_SET_COLOR_PRIMARIES, ctrl_set_color_primaries },
4614   { AV1E_SET_TRANSFER_CHARACTERISTICS, ctrl_set_transfer_characteristics },
4615   { AV1E_SET_MATRIX_COEFFICIENTS, ctrl_set_matrix_coefficients },
4616   { AV1E_SET_CHROMA_SAMPLE_POSITION, ctrl_set_chroma_sample_position },
4617   { AV1E_SET_COLOR_RANGE, ctrl_set_color_range },
4618   { AV1E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
4619   { AV1E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
4620   { AV1E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
4621   { AV1E_SET_GF_MIN_PYRAMID_HEIGHT, ctrl_set_gf_min_pyr_height },
4622   { AV1E_SET_GF_MAX_PYRAMID_HEIGHT, ctrl_set_gf_max_pyr_height },
4623   { AV1E_SET_RENDER_SIZE, ctrl_set_render_size },
4624   { AV1E_SET_SUPERBLOCK_SIZE, ctrl_set_superblock_size },
4625   { AV1E_SET_SINGLE_TILE_DECODING, ctrl_set_single_tile_decoding },
4626   { AV1E_SET_VMAF_MODEL_PATH, ctrl_set_vmaf_model_path },
4627   { AV1E_SET_PARTITION_INFO_PATH, ctrl_set_partition_info_path },
4628   { AV1E_ENABLE_RATE_GUIDE_DELTAQ, ctrl_enable_rate_guide_deltaq },
4629   { AV1E_SET_RATE_DISTRIBUTION_INFO, ctrl_set_rate_distribution_info },
4630   { AV1E_SET_FILM_GRAIN_TEST_VECTOR, ctrl_set_film_grain_test_vector },
4631   { AV1E_SET_FILM_GRAIN_TABLE, ctrl_set_film_grain_table },
4632   { AV1E_SET_DENOISE_NOISE_LEVEL, ctrl_set_denoise_noise_level },
4633   { AV1E_SET_DENOISE_BLOCK_SIZE, ctrl_set_denoise_block_size },
4634   { AV1E_SET_ENABLE_DNL_DENOISING, ctrl_set_enable_dnl_denoising },
4635   { AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
4636   { AV1E_SET_FP_MT_UNIT_TEST, ctrl_enable_fpmt_unit_test },
4637   { AV1E_ENABLE_EXT_TILE_DEBUG, ctrl_enable_ext_tile_debug },
4638   { AV1E_SET_TARGET_SEQ_LEVEL_IDX, ctrl_set_target_seq_level_idx },
4639   { AV1E_SET_TIER_MASK, ctrl_set_tier_mask },
4640   { AV1E_SET_MIN_CR, ctrl_set_min_cr },
4641   { AV1E_SET_SVC_LAYER_ID, ctrl_set_layer_id },
4642   { AV1E_SET_SVC_PARAMS, ctrl_set_svc_params },
4643   { AV1E_SET_SVC_REF_FRAME_CONFIG, ctrl_set_svc_ref_frame_config },
4644   { AV1E_SET_SVC_REF_FRAME_COMP_PRED, ctrl_set_svc_ref_frame_comp_pred },
4645   { AV1E_SET_VBR_CORPUS_COMPLEXITY_LAP, ctrl_set_vbr_corpus_complexity_lap },
4646   { AV1E_ENABLE_SB_MULTIPASS_UNIT_TEST, ctrl_enable_sb_multipass_unit_test },
4647   { AV1E_ENABLE_SB_QP_SWEEP, ctrl_enable_sb_qp_sweep },
4648   { AV1E_SET_DV_COST_UPD_FREQ, ctrl_set_dv_cost_upd_freq },
4649   { AV1E_SET_EXTERNAL_PARTITION, ctrl_set_external_partition },
4650   { AV1E_SET_ENABLE_TX_SIZE_SEARCH, ctrl_set_enable_tx_size_search },
4651   { AV1E_SET_LOOPFILTER_CONTROL, ctrl_set_loopfilter_control },
4652   { AV1E_SET_SKIP_POSTPROC_FILTERING, ctrl_set_skip_postproc_filtering },
4653   { AV1E_SET_AUTO_INTRA_TOOLS_OFF, ctrl_set_auto_intra_tools_off },
4654   { AV1E_SET_RTC_EXTERNAL_RC, ctrl_set_rtc_external_rc },
4655   { AV1E_SET_QUANTIZER_ONE_PASS, ctrl_set_quantizer_one_pass },
4656   { AV1E_SET_BITRATE_ONE_PASS_CBR, ctrl_set_bitrate_one_pass_cbr },
4657   { AV1E_SET_MAX_CONSEC_FRAME_DROP_CBR, ctrl_set_max_consec_frame_drop_cbr },
4658   { AV1E_SET_SVC_FRAME_DROP_MODE, ctrl_set_svc_frame_drop_mode },
4659   { AV1E_SET_AUTO_TILES, ctrl_set_auto_tiles },
4660   { AV1E_SET_POSTENCODE_DROP_RTC, ctrl_set_postencode_drop_rtc },
4661   { AV1E_SET_MAX_CONSEC_FRAME_DROP_MS_CBR,
4662     ctrl_set_max_consec_frame_drop_ms_cbr },
4663 
4664   // Getters
4665   { AOME_GET_LAST_QUANTIZER, ctrl_get_quantizer },
4666   { AOME_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
4667   { AOME_GET_LOOPFILTER_LEVEL, ctrl_get_loopfilter_level },
4668   { AV1_GET_REFERENCE, ctrl_get_reference },
4669   { AV1E_GET_ACTIVEMAP, ctrl_get_active_map },
4670   { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
4671   { AV1_COPY_NEW_FRAME_IMAGE, ctrl_copy_new_frame_image },
4672   { AV1E_SET_CHROMA_SUBSAMPLING_X, ctrl_set_chroma_subsampling_x },
4673   { AV1E_SET_CHROMA_SUBSAMPLING_Y, ctrl_set_chroma_subsampling_y },
4674   { AV1E_GET_SEQ_LEVEL_IDX, ctrl_get_seq_level_idx },
4675   { AV1E_GET_BASELINE_GF_INTERVAL, ctrl_get_baseline_gf_interval },
4676   { AV1E_GET_TARGET_SEQ_LEVEL_IDX, ctrl_get_target_seq_level_idx },
4677   { AV1E_GET_NUM_OPERATING_POINTS, ctrl_get_num_operating_points },
4678   { AV1E_GET_LUMA_CDEF_STRENGTH, ctrl_get_luma_cdef_strength },
4679   { AV1E_GET_HIGH_MOTION_CONTENT_SCREEN_RTC,
4680     ctrl_get_high_motion_content_screen_rtc },
4681 
4682   CTRL_MAP_END,
4683 };
4684 
4685 static const aom_codec_enc_cfg_t encoder_usage_cfg[] = {
4686 #if !CONFIG_REALTIME_ONLY
4687   {
4688       // NOLINT
4689       AOM_USAGE_GOOD_QUALITY,  // g_usage - non-realtime usage
4690       0,                       // g_threads
4691       0,                       // g_profile
4692 
4693       320,         // g_w
4694       240,         // g_h
4695       0,           // g_limit
4696       0,           // g_forced_max_frame_width
4697       0,           // g_forced_max_frame_height
4698       AOM_BITS_8,  // g_bit_depth
4699       8,           // g_input_bit_depth
4700 
4701       { 1, 30 },  // g_timebase
4702 
4703       0,  // g_error_resilient
4704 
4705       AOM_RC_ONE_PASS,  // g_pass
4706 
4707       35,  // g_lag_in_frames
4708 
4709       0,                // rc_dropframe_thresh
4710       RESIZE_NONE,      // rc_resize_mode
4711       SCALE_NUMERATOR,  // rc_resize_denominator
4712       SCALE_NUMERATOR,  // rc_resize_kf_denominator
4713 
4714       AOM_SUPERRES_NONE,  // rc_superres_mode
4715       SCALE_NUMERATOR,    // rc_superres_denominator
4716       SCALE_NUMERATOR,    // rc_superres_kf_denominator
4717       63,                 // rc_superres_qthresh
4718       32,                 // rc_superres_kf_qthresh
4719 
4720       AOM_VBR,      // rc_end_usage
4721       { NULL, 0 },  // rc_twopass_stats_in
4722       { NULL, 0 },  // rc_firstpass_mb_stats_in
4723       256,          // rc_target_bitrate
4724       0,            // rc_min_quantizer
4725       63,           // rc_max_quantizer
4726       25,           // rc_undershoot_pct
4727       25,           // rc_overshoot_pct
4728 
4729       6000,  // rc_buf_sz
4730       4000,  // rc_buf_initial_sz
4731       5000,  // rc_buf_optimal_sz
4732 
4733       50,    // rc_2pass_vbr_bias_pct
4734       0,     // rc_2pass_vbr_minsection_pct
4735       2000,  // rc_2pass_vbr_maxsection_pct
4736 
4737       // keyframing settings (kf)
4738       0,                       // fwd_kf_enabled
4739       AOM_KF_AUTO,             // kf_mode
4740       0,                       // kf_min_dist
4741       9999,                    // kf_max_dist
4742       0,                       // sframe_dist
4743       1,                       // sframe_mode
4744       0,                       // large_scale_tile
4745       0,                       // monochrome
4746       0,                       // full_still_picture_hdr
4747       0,                       // save_as_annexb
4748       0,                       // tile_width_count
4749       0,                       // tile_height_count
4750       { 0 },                   // tile_widths
4751       { 0 },                   // tile_heights
4752       0,                       // use_fixed_qp_offsets
4753       { -1, -1, -1, -1, -1 },  // fixed_qp_offsets
4754       { 0, 128, 128, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4755         0, 0,   0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  // encoder_cfg
4756   },
4757 #endif  // !CONFIG_REALTIME_ONLY
4758   {
4759       // NOLINT
4760       AOM_USAGE_REALTIME,  // g_usage - real-time usage
4761       0,                   // g_threads
4762       0,                   // g_profile
4763 
4764       320,         // g_w
4765       240,         // g_h
4766       0,           // g_limit
4767       0,           // g_forced_max_frame_width
4768       0,           // g_forced_max_frame_height
4769       AOM_BITS_8,  // g_bit_depth
4770       8,           // g_input_bit_depth
4771 
4772       { 1, 30 },  // g_timebase
4773 
4774       0,  // g_error_resilient
4775 
4776       AOM_RC_ONE_PASS,  // g_pass
4777 
4778       0,  // g_lag_in_frames
4779 
4780       0,                // rc_dropframe_thresh
4781       RESIZE_NONE,      // rc_resize_mode
4782       SCALE_NUMERATOR,  // rc_resize_denominator
4783       SCALE_NUMERATOR,  // rc_resize_kf_denominator
4784 
4785       AOM_SUPERRES_NONE,  // rc_superres_mode
4786       SCALE_NUMERATOR,    // rc_superres_denominator
4787       SCALE_NUMERATOR,    // rc_superres_kf_denominator
4788       63,                 // rc_superres_qthresh
4789       32,                 // rc_superres_kf_qthresh
4790 
4791       AOM_CBR,      // rc_end_usage
4792       { NULL, 0 },  // rc_twopass_stats_in
4793       { NULL, 0 },  // rc_firstpass_mb_stats_in
4794       256,          // rc_target_bitrate
4795       0,            // rc_min_quantizer
4796       63,           // rc_max_quantizer
4797       50,           // rc_undershoot_pct
4798       50,           // rc_overshoot_pct
4799 
4800       1000,  // rc_buf_sz
4801       600,   // rc_buf_initial_sz
4802       600,   // rc_buf_optimal_sz
4803 
4804       50,    // rc_2pass_vbr_bias_pct
4805       0,     // rc_2pass_vbr_minsection_pct
4806       2000,  // rc_2pass_vbr_maxsection_pct
4807 
4808       // keyframing settings (kf)
4809       0,                       // fwd_kf_enabled
4810       AOM_KF_AUTO,             // kf_mode
4811       0,                       // kf_min_dist
4812       9999,                    // kf_max_dist
4813       0,                       // sframe_dist
4814       1,                       // sframe_mode
4815       0,                       // large_scale_tile
4816       0,                       // monochrome
4817       0,                       // full_still_picture_hdr
4818       0,                       // save_as_annexb
4819       0,                       // tile_width_count
4820       0,                       // tile_height_count
4821       { 0 },                   // tile_widths
4822       { 0 },                   // tile_heights
4823       0,                       // use_fixed_qp_offsets
4824       { -1, -1, -1, -1, -1 },  // fixed_qp_offsets
4825       { 0, 128, 128, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4826         0, 0,   0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  // encoder_cfg
4827   },
4828 #if !CONFIG_REALTIME_ONLY
4829   {
4830       // NOLINT
4831       AOM_USAGE_ALL_INTRA,  // g_usage - all intra usage
4832       0,                    // g_threads
4833       0,                    // g_profile
4834 
4835       320,         // g_w
4836       240,         // g_h
4837       0,           // g_limit
4838       0,           // g_forced_max_frame_width
4839       0,           // g_forced_max_frame_height
4840       AOM_BITS_8,  // g_bit_depth
4841       8,           // g_input_bit_depth
4842 
4843       { 1, 30 },  // g_timebase
4844 
4845       0,  // g_error_resilient
4846 
4847       AOM_RC_ONE_PASS,  // g_pass
4848 
4849       0,  // g_lag_in_frames
4850 
4851       0,                // rc_dropframe_thresh
4852       RESIZE_NONE,      // rc_resize_mode
4853       SCALE_NUMERATOR,  // rc_resize_denominator
4854       SCALE_NUMERATOR,  // rc_resize_kf_denominator
4855 
4856       AOM_SUPERRES_NONE,  // rc_superres_mode
4857       SCALE_NUMERATOR,    // rc_superres_denominator
4858       SCALE_NUMERATOR,    // rc_superres_kf_denominator
4859       63,                 // rc_superres_qthresh
4860       32,                 // rc_superres_kf_qthresh
4861 
4862       AOM_Q,        // rc_end_usage
4863       { NULL, 0 },  // rc_twopass_stats_in
4864       { NULL, 0 },  // rc_firstpass_mb_stats_in
4865       256,          // rc_target_bitrate
4866       0,            // rc_min_quantizer
4867       63,           // rc_max_quantizer
4868       25,           // rc_undershoot_pct
4869       25,           // rc_overshoot_pct
4870 
4871       6000,  // rc_buf_sz
4872       4000,  // rc_buf_initial_sz
4873       5000,  // rc_buf_optimal_sz
4874 
4875       50,    // rc_2pass_vbr_bias_pct
4876       0,     // rc_2pass_vbr_minsection_pct
4877       2000,  // rc_2pass_vbr_maxsection_pct
4878 
4879       // keyframing settings (kf)
4880       0,                       // fwd_kf_enabled
4881       AOM_KF_DISABLED,         // kf_mode
4882       0,                       // kf_min_dist
4883       0,                       // kf_max_dist
4884       0,                       // sframe_dist
4885       1,                       // sframe_mode
4886       0,                       // large_scale_tile
4887       0,                       // monochrome
4888       0,                       // full_still_picture_hdr
4889       0,                       // save_as_annexb
4890       0,                       // tile_width_count
4891       0,                       // tile_height_count
4892       { 0 },                   // tile_widths
4893       { 0 },                   // tile_heights
4894       0,                       // use_fixed_qp_offsets
4895       { -1, -1, -1, -1, -1 },  // fixed_qp_offsets
4896       { 0, 128, 128, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4897         0, 0,   0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  // encoder_cfg
4898   },
4899 #endif  // !CONFIG_REALTIME_ONLY
4900 };
4901 
4902 // This data structure and function are exported in aom/aomcx.h
4903 #ifndef VERSION_STRING
4904 #define VERSION_STRING
4905 #endif
4906 aom_codec_iface_t aom_codec_av1_cx_algo = {
4907   "AOMedia Project AV1 Encoder" VERSION_STRING,
4908   AOM_CODEC_INTERNAL_ABI_VERSION,
4909   (CONFIG_AV1_HIGHBITDEPTH ? AOM_CODEC_CAP_HIGHBITDEPTH : 0) |
4910       AOM_CODEC_CAP_ENCODER | AOM_CODEC_CAP_PSNR,  // aom_codec_caps_t
4911   encoder_init,                                    // aom_codec_init_fn_t
4912   encoder_destroy,                                 // aom_codec_destroy_fn_t
4913   encoder_ctrl_maps,                               // aom_codec_ctrl_fn_map_t
4914   {
4915       // NOLINT
4916       NULL,  // aom_codec_peek_si_fn_t
4917       NULL,  // aom_codec_get_si_fn_t
4918       NULL,  // aom_codec_decode_fn_t
4919       NULL,  // aom_codec_get_frame_fn_t
4920       NULL   // aom_codec_set_fb_fn_t
4921   },
4922   {
4923       // NOLINT
4924       NELEMENTS(encoder_usage_cfg),  // cfg_count
4925       encoder_usage_cfg,             // aom_codec_enc_cfg_t
4926       encoder_encode,                // aom_codec_encode_fn_t
4927       encoder_get_cxdata,            // aom_codec_get_cx_data_fn_t
4928       encoder_set_config,            // aom_codec_enc_config_set_fn_t
4929       encoder_get_global_headers,    // aom_codec_get_global_headers_fn_t
4930       encoder_get_preview            // aom_codec_get_preview_frame_fn_t
4931   },
4932   encoder_set_option  // aom_codec_set_option_fn_t
4933 };
4934 
aom_codec_av1_cx(void)4935 aom_codec_iface_t *aom_codec_av1_cx(void) { return &aom_codec_av1_cx_algo; }
4936