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