Coverage Report

Created: 2026-06-16 07:20

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