Coverage Report

Created: 2026-07-25 07:03

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