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