Coverage Report

Created: 2026-07-16 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Globals/enc_settings.c
Line
Count
Source
1
/*
2
* Copyright(c) 2022 Intel Corporation
3
*
4
* This source code is subject to the terms of the BSD 3-Clause Clear License and
5
* the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear License
6
* was not distributed with this source code in the LICENSE file, you can
7
* obtain it at https://www.aomedia.org/license. 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 https://www.aomedia.org/license/patent-license.
10
*/
11
// SUMMARY
12
//   Contains the encoder settings API functions
13
14
/**************************************
15
 * Includes
16
 **************************************/
17
#include <stdbool.h>
18
#include <stdlib.h>
19
#include <stdio.h>
20
#include <stdint.h>
21
#include "EbVersion.h"
22
#include "definitions.h"
23
#include "EbSvtAv1Enc.h"
24
#include "EbSvtAv1Metadata.h"
25
#include "enc_settings.h"
26
#include "entropy_coding.h"
27
28
#include "svt_log.h"
29
#include "utility.h"
30
31
#ifdef _WIN32
32
#include <windows.h>
33
#else
34
#include <errno.h>
35
#include <pthread.h>
36
#include <unistd.h>
37
#endif
38
39
/******************************************
40
* Verify Settings
41
******************************************/
42
448
EbErrorType svt_av1_verify_settings(SequenceControlSet* scs) {
43
448
    EbErrorType               return_error = EB_ErrorNone;
44
448
    EbSvtAv1EncConfiguration* config       = &scs->static_config;
45
448
    if (config->enc_mode > MAX_ENC_PRESET || config->enc_mode < MIN_ENC_PRESET) {
46
0
        SVT_ERROR("EncoderMode must be in the range of [%d-%d]\n", MIN_ENC_PRESET, MAX_ENC_PRESET);
47
0
        return_error = EB_ErrorBadParameter;
48
0
    }
49
448
    if (scs->max_input_luma_width < 1) {
50
0
        SVT_ERROR("Source Width must be at least 1\n");
51
0
        return_error = EB_ErrorBadParameter;
52
0
    }
53
448
    if (scs->max_input_luma_height < 1) {
54
0
        SVT_ERROR("Source Height must be at least 1\n");
55
0
        return_error = EB_ErrorBadParameter;
56
0
    }
57
448
    if (config->pred_structure > RANDOM_ACCESS) {
58
0
        SVT_ERROR("Pred Structure must be [%d (ALL_INTRA), %d (LOW_DELAY), or %d (RANDOM_ACCESS)]\n",
59
0
                  ALL_INTRA,
60
0
                  LOW_DELAY,
61
0
                  RANDOM_ACCESS);
62
0
        return_error = EB_ErrorBadParameter;
63
0
    }
64
448
    if (config->pred_structure == LOW_DELAY && config->pass > 0) {
65
0
        SVT_ERROR("Multi-passes is not support with Low Delay mode \n");
66
0
        return_error = EB_ErrorBadParameter;
67
0
    }
68
448
    if (config->maximum_buffer_size_ms < 20 || config->maximum_buffer_size_ms > 10000) {
69
0
        SVT_ERROR("The maximum buffer size must be between [20, 10000]\n");
70
0
        return_error = EB_ErrorBadParameter;
71
0
    }
72
73
448
    if (config->starting_buffer_level_ms < 20 || config->starting_buffer_level_ms > 10000) {
74
0
        SVT_ERROR("The initial buffer size must be between [20, 10000] \n");
75
0
        return_error = EB_ErrorBadParameter;
76
448
    } else if (config->starting_buffer_level_ms >= config->maximum_buffer_size_ms) {
77
0
        SVT_WARN(
78
0
            "The initial buffer size must be less than maximum buffer size. Defaulting optimal "
79
0
            "buffer size to maximum buffer size - 1 (%u)\n",
80
0
            config->maximum_buffer_size_ms - 1);
81
0
        config->starting_buffer_level_ms = (config->maximum_buffer_size_ms - 1);
82
0
    }
83
84
448
    if (config->optimal_buffer_level_ms < 20 || config->optimal_buffer_level_ms > 10000) {
85
0
        SVT_ERROR("The optimal buffer size must be between [20, 10000]\n");
86
0
        return_error = EB_ErrorBadParameter;
87
448
    } else if (config->optimal_buffer_level_ms >= config->maximum_buffer_size_ms) {
88
0
        SVT_WARN(
89
0
            "The optimal buffer size must be less than maximum buffer size. Defaulting optimal "
90
0
            "buffer size to maximum buffer size - 1 (%u)\n",
91
0
            config->maximum_buffer_size_ms - 1);
92
0
        config->optimal_buffer_level_ms = (config->maximum_buffer_size_ms - 1);
93
0
    }
94
95
448
    if (config->over_shoot_pct > 100) {
96
0
        SVT_ERROR("The overshoot percentage must be between [0, 100] \n");
97
0
        return_error = EB_ErrorBadParameter;
98
0
    }
99
100
448
    if (config->mbr_over_shoot_pct > 100) {
101
0
        SVT_ERROR("The max bitrate overshoot percentage must be between [0, 100] \n");
102
0
        return_error = EB_ErrorBadParameter;
103
0
    }
104
105
448
    if (config->under_shoot_pct > 100) {
106
0
        SVT_ERROR("The undershoot percentage must be between [0, 100] \n");
107
0
        return_error = EB_ErrorBadParameter;
108
0
    }
109
110
448
    if (config->target_bit_rate > 100000000) {
111
0
        SVT_ERROR("The target bit rate must be between [0, 100000] kbps \n");
112
0
        return_error = EB_ErrorBadParameter;
113
0
    }
114
448
    if (config->max_bit_rate > 100000000) {
115
0
        SVT_ERROR("The maximum bit rate must be between [0, 100000] kbps \n");
116
0
        return_error = EB_ErrorBadParameter;
117
0
    }
118
448
    if (config->vbr_max_section_pct > 10000) {
119
0
        SVT_ERROR("The max section percentage must be between [0, 10000] \n");
120
0
        return_error = EB_ErrorBadParameter;
121
0
    }
122
123
448
    if (config->vbr_min_section_pct > 100) {
124
0
        SVT_ERROR("he min section percentage must be between [0, 100] \n");
125
0
        return_error = EB_ErrorBadParameter;
126
0
    }
127
448
    if (config->gop_constraint_rc &&
128
0
        ((config->rate_control_mode != SVT_AV1_RC_MODE_VBR) || config->intra_period_length < 119)) {
129
0
        SVT_ERROR(
130
0
            "Gop constraint rc is only supported with VBR mode when Gop size is "
131
0
            "greater than 119 \n");
132
0
        return_error = EB_ErrorBadParameter;
133
0
    }
134
448
    if (config->gop_constraint_rc) {
135
0
        SVT_WARN(
136
0
            "The GoP constraint RC mode is a work-in-progress project, and is only "
137
0
            "available for demos, experimentation, and further development uses and should not be "
138
0
            "used for benchmarking until fully implemented.\n");
139
0
    }
140
141
448
    if (config->force_key_frames &&
142
0
        (config->rate_control_mode == SVT_AV1_RC_MODE_CBR || config->pred_structure == LOW_DELAY)) {
143
0
        SVT_WARN(
144
0
            "Force key frames is now supported for lowdelay but the force_key_frames flag"
145
0
            " does not need to be set be on. Please follow the app samples shown by the FTR_KF_ON_FLY_SAMPLE"
146
0
            " macro on how to use it. force_key_frames will now be set to 0 \n");
147
0
        config->force_key_frames = 0;
148
0
    }
149
448
    if (config->force_key_frames && config->rate_control_mode == SVT_AV1_RC_MODE_VBR) {
150
0
        SVT_ERROR("Force key frames is not supported for VBR mode \n");
151
0
        return_error = EB_ErrorBadParameter;
152
0
    }
153
448
    if (config->rate_control_mode != SVT_AV1_RC_MODE_CQP_OR_CRF && (config->max_bit_rate != 0)) {
154
0
        SVT_ERROR("Max Bitrate only supported with CRF mode\n");
155
0
        return_error = EB_ErrorBadParameter;
156
0
    }
157
448
    if (config->rate_control_mode == SVT_AV1_RC_MODE_CBR && config->pred_structure != LOW_DELAY) {
158
0
        SVT_ERROR("CBR Rate control is currently not supported for RANDOM_ACCESS/ALL_INTRA, use VBR mode\n");
159
0
        return_error = EB_ErrorBadParameter;
160
0
    }
161
    // Ref-frame management validation: ABI cap + LD-CBR-only when enabled.
162
448
    if (config->max_managed_refs > 4) {
163
0
        SVT_ERROR("max_managed_refs must be in [0, 4] (got %u)\n", (unsigned)config->max_managed_refs);
164
0
        return_error = EB_ErrorBadParameter;
165
0
    }
166
448
    if (config->max_managed_refs > 0 && config->pred_structure != LOW_DELAY) {
167
0
        SVT_ERROR("max_managed_refs > 0 requires pred_structure == LOW_DELAY\n");
168
0
        return_error = EB_ErrorBadParameter;
169
0
    }
170
448
    if (config->max_managed_refs > 0 && config->rate_control_mode != SVT_AV1_RC_MODE_CBR) {
171
        // Only LD-CBR is implemented; LD-CRF's shifted lay1_offset would
172
        // collide with the STORE-safe slot pool. See pd_process.c.
173
0
        SVT_ERROR("max_managed_refs > 0 requires rate_control_mode == CBR (got %u)\n",
174
0
                  (unsigned)config->rate_control_mode);
175
0
        return_error = EB_ErrorBadParameter;
176
0
    }
177
448
    if (config->rate_control_mode == SVT_AV1_RC_MODE_VBR && config->pred_structure == LOW_DELAY) {
178
0
        SVT_ERROR("VBR Rate control is currently not supported for LOW_DELAY, use CBR mode\n");
179
0
        return_error = EB_ErrorBadParameter;
180
0
    }
181
448
    if (config->rate_control_mode == SVT_AV1_RC_MODE_CQP_OR_CRF && config->target_bit_rate != DEFAULT_TBR) {
182
0
        SVT_ERROR("Target Bitrate only supported when --rc is  1/2 (VBR/CBR). Current --rc: %d\n",
183
0
                  config->rate_control_mode);
184
0
        return_error = EB_ErrorBadParameter;
185
0
    }
186
187
448
    if (scs->max_input_luma_width > 16384) {
188
0
        SVT_ERROR("Source Width must be less than or equal to 16384\n");
189
0
        return_error = EB_ErrorBadParameter;
190
0
    }
191
192
448
    if (scs->max_input_luma_height > 8704) {
193
0
        SVT_ERROR("Source Height must be less than or equal to 8704)\n");
194
0
        return_error = EB_ErrorBadParameter;
195
0
    }
196
197
448
    if (scs->seq_header.max_frame_width < 1) {
198
0
        SVT_ERROR("Forced Max Width must be at least 1\n");
199
0
        return_error = EB_ErrorBadParameter;
200
0
    }
201
448
    if (scs->seq_header.max_frame_height < 1) {
202
0
        SVT_ERROR("Forced Max Height must be at least 1\n");
203
0
        return_error = EB_ErrorBadParameter;
204
0
    }
205
448
    if (scs->seq_header.max_frame_width > 16384) {
206
0
        SVT_ERROR("Forced Max Width must be less than or equal to 16384\n");
207
0
        return_error = EB_ErrorBadParameter;
208
0
    }
209
448
    if (scs->seq_header.max_frame_height > 8704) {
210
0
        SVT_ERROR("Forced Max Height must be less than or equal to 8704)\n");
211
0
        return_error = EB_ErrorBadParameter;
212
0
    }
213
214
    // This is not an AV1 spec limitation, but an implementation limitation in the encoder
215
    // This check will stay in place until restoration filtering can handle these dimensions
216
448
    if ((scs->max_input_luma_width >= 1 && scs->max_input_luma_width < 64) ||
217
448
        (scs->max_input_luma_height >= 1 && scs->max_input_luma_height < 64)) {
218
0
        if (config->aq_mode != 0) {
219
0
            SVT_WARN("AQ mode %i is unsupported with source dimensions (%i / %i), setting AQ mode to 0\n",
220
0
                     config->aq_mode,
221
0
                     scs->max_input_luma_width,
222
0
                     scs->max_input_luma_height);
223
0
            config->aq_mode = 0;
224
0
        }
225
0
        if (config->enable_restoration_filtering != 0) {
226
0
            SVT_WARN(
227
0
                "Restoration Filtering is unsupported with source dimensions (%i / %i), disabling "
228
0
                "Restoration Filtering\n",
229
0
                scs->max_input_luma_width,
230
0
                scs->max_input_luma_height);
231
0
            config->enable_restoration_filtering = 0;
232
0
        }
233
0
    }
234
235
448
    if ((scs->max_input_luma_width > scs->seq_header.max_frame_width) ||
236
448
        (scs->max_input_luma_height > scs->seq_header.max_frame_height)) {
237
0
        SVT_ERROR(
238
0
            "Source Width/Height must be less than or equal to Forced Max "
239
0
            "Width/Height\n");
240
0
        return_error = EB_ErrorBadParameter;
241
0
    }
242
243
448
    if (config->level != 0) {
244
0
        BitstreamLevel bl;
245
0
        bl.major = config->level / 10;
246
0
        bl.minor = config->level % 10;
247
        // Defined AV1 levels only have major versions 2-9 and minor versions 0-3
248
0
        if (bl.minor > LEVEL_MINOR_MAX || bl.major < LEVEL_MAJOR_MIN || bl.major > LEVEL_MAJOR_MAX) {
249
0
            SVT_ERROR("Invalid or undefined level specified: %d.%d. See AV1 spec Annex A for defined levels.\n",
250
0
                      bl.major,
251
0
                      bl.minor);
252
0
            return_error = EB_ErrorBadParameter;
253
0
        } else {
254
            // Some levels in the allowable range are undefined by the spec.
255
0
            const uint8_t seq_level_idx = major_minor_to_seq_level_idx(bl);
256
0
            if (!is_valid_seq_level_idx(seq_level_idx)) {
257
0
                SVT_ERROR("Invalid or undefined level specified: %d.%d. See AV1 spec Annex A for defined levels.\n",
258
0
                          bl.major,
259
0
                          bl.minor);
260
0
                return_error = EB_ErrorBadParameter;
261
0
            }
262
0
        }
263
0
    }
264
265
448
    if (config->qp > MAX_QP_VALUE) {
266
0
        SVT_ERROR("%s must be [0 - %d]\n", config->aq_mode ? "CRF" : "QP", MAX_QP_VALUE);
267
0
        return_error = EB_ErrorBadParameter;
268
0
    }
269
270
448
    if ((config->qp == MAX_QP_VALUE && config->extended_crf_qindex_offset > (7 * 4))) {
271
0
        SVT_ERROR("%s must be [0 - %d]\n", "CRF", 70);
272
0
        return_error = EB_ErrorBadParameter;
273
0
    }
274
275
448
    if (config->hierarchical_levels > 5) {
276
0
        SVT_ERROR("Hierarchical Levels supported: [0-5]\n");
277
0
        return_error = EB_ErrorBadParameter;
278
448
    } else {
279
1.79k
        for (uint8_t i = 0; i < config->hierarchical_levels + 1; ++i) {
280
1.34k
            if (config->qindex_offsets[i] < -64 || config->qindex_offsets[i] > 63) {
281
0
                SVT_ERROR(
282
0
                    "Invalid qindex_offsets for hierarchical level %d. Found %d, qindex_offsets must be [-64 - 63]\n",
283
0
                    i,
284
0
                    config->qindex_offsets[i]);
285
0
                return_error = EB_ErrorBadParameter;
286
0
            }
287
1.34k
        }
288
1.79k
        for (uint8_t i = 0; i < config->hierarchical_levels + 1; ++i) {
289
1.34k
            if (config->chroma_qindex_offsets[i] < -64 || config->chroma_qindex_offsets[i] > 63) {
290
0
                SVT_ERROR(
291
0
                    "Invalid chroma_qindex_offsets for hierarchical level %d. Found %d, chroma_qindex_offsets must be "
292
0
                    "[-64 - 63]\n",
293
0
                    i,
294
0
                    config->chroma_qindex_offsets[i]);
295
0
                return_error = EB_ErrorBadParameter;
296
0
            }
297
1.34k
        }
298
448
    }
299
448
    if ((config->intra_period_length < -2 || config->intra_period_length > 2 * ((1 << 30) - 1)) &&
300
0
        config->rate_control_mode == SVT_AV1_RC_MODE_CQP_OR_CRF) {
301
0
        SVT_ERROR("The intra period must be [-2, 2^31-2]\n");
302
0
        return_error = EB_ErrorBadParameter;
303
0
    }
304
448
    if ((config->intra_period_length < 0) && config->rate_control_mode == SVT_AV1_RC_MODE_VBR) {
305
0
        SVT_ERROR("The intra period must be > 0 for RateControlMode %d\n", config->rate_control_mode);
306
0
        return_error = EB_ErrorBadParameter;
307
0
    }
308
309
448
    if (config->intra_refresh_type > 2 || config->intra_refresh_type < 1) {
310
0
        SVT_ERROR("Invalid intra Refresh Type [1-2]\n");
311
0
        return_error = EB_ErrorBadParameter;
312
0
    }
313
314
448
    if (config->enable_dlf_flag > 2) {
315
0
        SVT_ERROR("Invalid LoopFilterEnable. LoopFilterEnable must be [0 - 2]\n");
316
0
        return_error = EB_ErrorBadParameter;
317
0
    }
318
319
448
    if (config->rate_control_mode > SVT_AV1_RC_MODE_CBR &&
320
0
        (config->pass == ENC_FIRST_PASS || config->rc_stats_buffer.buf)) {
321
0
        SVT_ERROR("Only rate control mode 0~2 are supported for 2-pass \n");
322
0
        return_error = EB_ErrorBadParameter;
323
0
    }
324
    // Stats file is checked by the app in handle_stats_file, but must be re-checked here since ffmpeg calls
325
    // the library but not the app
326
448
    if (config->rate_control_mode == SVT_AV1_RC_MODE_VBR && config->pass == ENC_SECOND_PASS) {
327
0
        if (!config->rc_stats_buffer.buf) {
328
0
            SVT_ERROR("RC stats buffer not available \n");
329
0
            return_error = EB_ErrorBadParameter;
330
0
        } else if (config->rc_stats_buffer.sz == 0) {
331
0
            SVT_ERROR("RC stats buffer size is 0 \n");
332
0
            return_error = EB_ErrorBadParameter;
333
0
        }
334
0
    }
335
448
    if (config->profile > 2) {
336
0
        SVT_ERROR("The maximum allowed profile value is 2 \n");
337
0
        return_error = EB_ErrorBadParameter;
338
0
    }
339
340
    // Check if the current input video is conformant with the Level constraint
341
448
    if (scs->frame_rate > 240) {
342
0
        SVT_ERROR("The maximum allowed frame rate is 240 fps\n");
343
0
        return_error = EB_ErrorBadParameter;
344
0
    }
345
    // Check that the frame_rate is non-zero
346
448
    if (!scs->frame_rate) {
347
0
        SVT_ERROR("The frame rate should be greater than 0 fps \n");
348
0
        return_error = EB_ErrorBadParameter;
349
0
    }
350
448
    if (scs->static_config.frame_rate_numerator == 0 || scs->static_config.frame_rate_denominator == 0) {
351
0
        SVT_ERROR("The frame_rate_numerator and frame_rate_denominator must be greater than 0\n");
352
0
        return_error = EB_ErrorBadParameter;
353
0
    }
354
448
    if (config->recode_loop > 4) {
355
0
        SVT_ERROR("The recode_loop must be [0 - 4] \n");
356
0
        return_error = EB_ErrorBadParameter;
357
0
    }
358
448
    if (config->rate_control_mode > SVT_AV1_RC_MODE_CBR) {
359
0
        SVT_ERROR("The rate control mode must be [0 - 2] \n");
360
0
        return_error = EB_ErrorBadParameter;
361
0
    }
362
448
    if (config->look_ahead_distance > MAX_LAD && config->look_ahead_distance != (uint32_t)~0) {
363
0
        SVT_ERROR("The lookahead distance must be [0 - %d] \n", MAX_LAD);
364
365
0
        return_error = EB_ErrorBadParameter;
366
0
    }
367
448
    if ((unsigned)config->tile_rows > 6 || (unsigned)config->tile_columns > 6) {
368
0
        SVT_ERROR("Log2Tile rows/cols must be [0 - 6] \n");
369
0
        return_error = EB_ErrorBadParameter;
370
0
    }
371
448
    if ((1u << config->tile_rows) * (1u << config->tile_columns) > 128 || config->tile_columns > 4) {
372
0
        SVT_ERROR("MaxTiles is 128 and MaxTileCols is 16 (Annex A.3) \n");
373
0
        return_error = EB_ErrorBadParameter;
374
0
    }
375
448
    if (config->max_qp_allowed > MAX_QP_VALUE) {
376
0
        SVT_ERROR("MaxQpAllowed must be [0 - %d]\n", MAX_QP_VALUE);
377
0
        return_error = EB_ErrorBadParameter;
378
448
    } else if (config->min_qp_allowed > MAX_QP_VALUE) {
379
0
        SVT_ERROR("MinQpAllowed must be [0 - %d]\n", MAX_QP_VALUE);
380
0
        return_error = EB_ErrorBadParameter;
381
448
    } else if (config->min_qp_allowed > config->max_qp_allowed) {
382
0
        SVT_ERROR("MinQpAllowed must be smaller than or equal to MaxQpAllowed\n");
383
0
        return_error = EB_ErrorBadParameter;
384
0
    }
385
448
    if (config->use_fixed_qindex_offsets > 2) {
386
0
        SVT_ERROR("The use_fixed_qindex_offsets must be [0 - 2] \n");
387
0
        return_error = EB_ErrorBadParameter;
388
0
    }
389
390
448
    if (config->key_frame_qindex_offset < -64 || config->key_frame_qindex_offset > 63) {
391
0
        SVT_ERROR("Invalid key_frame_qindex_offset. key_frame_qindex_offset must be [-64 - 63]\n");
392
0
        return_error = EB_ErrorBadParameter;
393
0
    }
394
395
448
    if (config->key_frame_chroma_qindex_offset < -64 || config->key_frame_chroma_qindex_offset > 63) {
396
0
        SVT_ERROR(
397
0
            "Invalid key_frame_chroma_qindex_offset. key_frame_chroma_qindex_offset "
398
0
            "must be [-64 - 63]\n");
399
0
        return_error = EB_ErrorBadParameter;
400
0
    }
401
448
    if (config->luma_y_dc_qindex_offset < -64 || config->luma_y_dc_qindex_offset > 63) {
402
0
        SVT_ERROR(
403
0
            "Invalid luma_y_dc_qindex_offset. luma_y_dc_qindex_offset "
404
0
            "must be [-64 - 63]\n");
405
0
        return_error = EB_ErrorBadParameter;
406
0
    }
407
448
    if (config->chroma_u_dc_qindex_offset < -64 || config->chroma_u_dc_qindex_offset > 63) {
408
0
        SVT_ERROR(
409
0
            "Invalid chroma_u_dc_qindex_offset. chroma_u_dc_qindex_offset "
410
0
            "must be [-64 - 63]\n");
411
0
        return_error = EB_ErrorBadParameter;
412
0
    }
413
448
    if (config->chroma_u_ac_qindex_offset < -64 || config->chroma_u_ac_qindex_offset > 63) {
414
0
        SVT_ERROR(
415
0
            "Invalid chroma_u_ac_qindex_offset. chroma_u_ac_qindex_offset "
416
0
            "must be [-64 - 63]\n");
417
0
        return_error = EB_ErrorBadParameter;
418
0
    }
419
448
    if (config->chroma_v_dc_qindex_offset < -64 || config->chroma_v_dc_qindex_offset > 63) {
420
0
        SVT_ERROR(
421
0
            "Invalid chroma_v_dc_qindex_offset. chroma_v_dc_qindex_offset "
422
0
            "must be [-64 - 63]\n");
423
0
        return_error = EB_ErrorBadParameter;
424
0
    }
425
448
    if (config->chroma_v_ac_qindex_offset < -64 || config->chroma_v_ac_qindex_offset > 63) {
426
0
        SVT_ERROR(
427
0
            "Invalid chroma_v_ac_qindex_offset. chroma_v_ac_qindex_offset "
428
0
            "must be [-64 - 63]\n");
429
0
        return_error = EB_ErrorBadParameter;
430
0
    }
431
432
448
    if (config->startup_qp_offset < -63 || config->startup_qp_offset > 63) {
433
0
        SVT_ERROR("Invalid startup_qp_offset. startup_qp_offset must be [-63 - 63]\n");
434
0
        return_error = EB_ErrorBadParameter;
435
0
    }
436
448
    if (config->stat_report == 1) {
437
0
        SVT_WARN("Enabling StatReport can decrease encoding speed\n");
438
0
    }
439
440
448
    if (config->stat_report > 1) {
441
0
        SVT_ERROR("Invalid StatReport. StatReport must be [0 - 1]\n");
442
0
        return_error = EB_ErrorBadParameter;
443
0
    }
444
448
    if (config->screen_content_mode > 3) {
445
0
        SVT_ERROR("Invalid screen_content_mode. screen_content_mode must be [0 - 3]\n");
446
0
        return_error = EB_ErrorBadParameter;
447
0
    }
448
449
448
    if (scs->static_config.aq_mode > 2) {
450
0
        SVT_ERROR("Invalid adaptive quantization (AQ) mode. AQ mode must be [0-2]\n");
451
0
        return_error = EB_ErrorBadParameter;
452
0
    }
453
454
448
    if ((config->encoder_bit_depth != 8) && (config->encoder_bit_depth != 10)) {
455
0
        SVT_ERROR("Encoder Bit Depth shall be only 8 or 10 \n");
456
0
        return_error = EB_ErrorBadParameter;
457
0
    }
458
    // Check if the EncoderBitDepth is conformant with the Profile constraint
459
448
    if ((config->profile == 0 || config->profile == 1) && config->encoder_bit_depth > 10) {
460
0
        SVT_ERROR("The encoder bit depth shall be equal to 8 or 10 for Main/High Profile\n");
461
0
        return_error = EB_ErrorBadParameter;
462
0
    }
463
464
448
    if (config->encoder_color_format != EB_YUV420) {
465
0
        SVT_ERROR("Only support 420 now \n");
466
0
        return_error = EB_ErrorBadParameter;
467
0
    }
468
469
448
    if (config->profile == 0 && config->encoder_color_format > EB_YUV420) {
470
0
        SVT_ERROR("Non 420 color format requires profile 1 or 2\n");
471
0
        return_error = EB_ErrorBadParameter;
472
0
    }
473
474
448
    if (config->profile == 1 && config->encoder_color_format != EB_YUV444) {
475
0
        SVT_ERROR("Profile 1 requires 4:4:4 color format\n");
476
0
        return_error = EB_ErrorBadParameter;
477
0
    }
478
479
448
    if (config->profile == 2 && config->encoder_bit_depth <= 10 && config->encoder_color_format != EB_YUV422) {
480
0
        SVT_ERROR("Profile 2 bit-depth < 10 requires 4:2:2 color format\n");
481
0
        return_error = EB_ErrorBadParameter;
482
0
    }
483
484
448
    if (config->use_cpu_flags & EB_CPU_FLAGS_INVALID) {
485
#ifdef ARCH_AARCH64
486
        SVT_ERROR(
487
            "param '--asm' have invalid value.\n"
488
            "Value should be [0 - 6] or [c, neon, crc32, neon_dotprod, neon_i8mm, sve, sve2, max]\n");
489
#else
490
0
        SVT_ERROR(
491
0
            "param '--asm' have invalid value.\n"
492
0
            "Value should be [0 - 11] or [c, mmx, sse, sse2, sse3, ssse3, sse4_1, sse4_2, avx, "
493
0
            "avx2, avx512, avx512icl, max]\n");
494
0
#endif
495
0
        return_error = EB_ErrorBadParameter;
496
0
    }
497
498
    // HBD mode decision
499
448
    if (scs->enable_hbd_mode_decision < (int8_t)(-1) || scs->enable_hbd_mode_decision > 2) {
500
0
        SVT_ERROR("Invalid HBD mode decision flag [-1 - 2], your input: %d\n", scs->enable_hbd_mode_decision);
501
0
        return_error = EB_ErrorBadParameter;
502
0
    }
503
504
    // CDEF
505
448
    if (config->cdef_level > 4 || config->cdef_level < -1) {
506
0
        SVT_ERROR("Invalid CDEF level [0 - 4, -1 for auto], your input: %d\n", config->cdef_level);
507
0
        return_error = EB_ErrorBadParameter;
508
0
    }
509
510
    // Restoration Filtering
511
448
    if (config->enable_restoration_filtering != 0 && config->enable_restoration_filtering != 1 &&
512
448
        config->enable_restoration_filtering != -1) {
513
0
        SVT_ERROR("Invalid restoration flag [0 - 1, -1 for auto], your input: %d\n",
514
0
                  config->enable_restoration_filtering);
515
0
        return_error = EB_ErrorBadParameter;
516
0
    }
517
518
448
    if (config->enable_mfmv != 0 && config->enable_mfmv != 1 && config->enable_mfmv != -1) {
519
0
        SVT_ERROR(
520
0
            "Invalid motion field motion vector flag [0/1 or -1 for auto], your "
521
0
            "input: %d\n",
522
0
            config->enable_mfmv);
523
0
        return_error = EB_ErrorBadParameter;
524
0
    }
525
448
    if (config->fast_decode > 2) {
526
0
        SVT_ERROR(
527
0
            "Invalid fast decode flag [0 - 2, 0 for no decoder-targeted optimization], your "
528
0
            "input: %d\n",
529
0
            config->fast_decode);
530
0
        return_error = EB_ErrorBadParameter;
531
0
    }
532
448
    if (config->tune > TUNE_VMAF) {
533
0
        SVT_ERROR(
534
0
            "Invalid tune flag [0 - 5, 0 for VQ, 1 for PSNR, 2 for SSIM, 3 for IQ, 4 for MS_SSIM, and 5 for VMAF], "
535
0
            "your input: "
536
0
            "%d\n",
537
0
            config->tune);
538
0
        return_error = EB_ErrorBadParameter;
539
0
    }
540
    // RC: SSIM, IQ, MS_SSIM, VMAF -> CRF only (VBR, CBR not supported)
541
448
    if (config->tune == TUNE_SSIM || config->tune == TUNE_IQ || config->tune == TUNE_MS_SSIM ||
542
448
        config->tune == TUNE_VMAF) {
543
0
        if (config->rate_control_mode != 0) {
544
0
            SVT_ERROR("Tune %s only supports CRF rate control mode\n",
545
0
                      config->tune == TUNE_SSIM       ? "SSIM"
546
0
                          : config->tune == TUNE_IQ   ? "IQ"
547
0
                          : config->tune == TUNE_VMAF ? "VMAF"
548
0
                                                      : "MS_SSIM");
549
0
            return_error = EB_ErrorBadParameter;
550
0
        }
551
0
    }
552
553
    // pred_struct: SSIM, MS_SSIM -> ALL_INTRA and RA only (LOW_DELAY not supported)
554
448
    if (config->tune == TUNE_SSIM || config->tune == TUNE_MS_SSIM) {
555
0
        if (config->pred_structure == LOW_DELAY) {
556
0
            SVT_ERROR("Tune %s only supports all-intra and random access prediction structures\n",
557
0
                      config->tune == TUNE_SSIM ? "SSIM" : "MS_SSIM");
558
0
            return_error = EB_ErrorBadParameter;
559
0
        }
560
0
    }
561
562
    // pred_struct: VMAF -> RA only (ALL_INTRA and LOW_DELAY not supported)
563
448
    if (config->tune == TUNE_VMAF && (config->pred_structure == ALL_INTRA || config->pred_structure == LOW_DELAY)) {
564
0
        SVT_ERROR("Tune VMAF only supports random access prediction structure\n");
565
0
        return_error = EB_ErrorBadParameter;
566
0
    }
567
568
    // pred_struct: IQ -> ALL_INTRA and LOW_DELAY only (RA not supported); LOW_DELAY is experimental
569
448
    if (config->tune == TUNE_IQ && config->pred_structure == RANDOM_ACCESS) {
570
0
        SVT_ERROR("Tune IQ only supports all-intra and low delay (experimental) prediction structures\n");
571
0
        return_error = EB_ErrorBadParameter;
572
0
    }
573
448
    if (config->tune == TUNE_IQ && config->pred_structure == LOW_DELAY) {
574
0
        SVT_WARN("Tune IQ with low delay prediction structure is experimental\n");
575
0
    }
576
577
448
    if (config->superres_mode > SUPERRES_AUTO) {
578
0
        SVT_ERROR("invalid superres-mode %d, should be in the range [%d - %d]\n",
579
0
                  config->superres_mode,
580
0
                  SUPERRES_NONE,
581
0
                  SUPERRES_AUTO);
582
0
        return_error = EB_ErrorBadParameter;
583
0
    }
584
448
    if (config->superres_mode > 0 && ((config->rc_stats_buffer.sz || config->pass == ENC_FIRST_PASS))) {
585
0
        SVT_ERROR("superres is not supported for 2-pass\n");
586
0
        return_error = EB_ErrorBadParameter;
587
0
    }
588
589
448
    if (config->superres_qthres > MAX_QP_VALUE) {
590
0
        SVT_ERROR("Invalid superres-qthres %d, should be in the range [%d - %d]\n",
591
0
                  config->superres_qthres,
592
0
                  MIN_QP_VALUE,
593
0
                  MAX_QP_VALUE);
594
0
        return_error = EB_ErrorBadParameter;
595
0
    }
596
597
448
    if (config->superres_kf_qthres > MAX_QP_VALUE) {
598
0
        SVT_ERROR("Invalid superres-kf-qthres %d, should be in the range [%d - %d]\n",
599
0
                  config->superres_kf_qthres,
600
0
                  MIN_QP_VALUE,
601
0
                  MAX_QP_VALUE);
602
0
        return_error = EB_ErrorBadParameter;
603
0
    }
604
605
448
    if (config->superres_kf_denom < MIN_SUPERRES_DENOM || config->superres_kf_denom > MAX_SUPERRES_DENOM) {
606
0
        SVT_ERROR("Invalid superres-kf-denom %d, should be in the range [%d - %d]\n",
607
0
                  config->superres_kf_denom,
608
0
                  MIN_SUPERRES_DENOM,
609
0
                  MAX_SUPERRES_DENOM);
610
0
        return_error = EB_ErrorBadParameter;
611
0
    }
612
613
448
    if (config->superres_denom < MIN_SUPERRES_DENOM || config->superres_denom > MAX_SUPERRES_DENOM) {
614
0
        SVT_ERROR("Invalid superres-denom %d, should be in the range [%d - %d]\n",
615
0
                  config->superres_denom,
616
0
                  MIN_SUPERRES_DENOM,
617
0
                  MAX_SUPERRES_DENOM);
618
0
        return_error = EB_ErrorBadParameter;
619
0
    }
620
621
448
    if (config->resize_mode > RESIZE_RANDOM_ACCESS) {
622
0
        SVT_ERROR("Invalid resize-mode %d, should be in the range [%d - %d]\n",
623
0
                  config->resize_mode,
624
0
                  RESIZE_NONE,
625
0
                  RESIZE_RANDOM_ACCESS);
626
0
        return_error = EB_ErrorBadParameter;
627
0
    }
628
629
448
    if (config->resize_kf_denom < MIN_RESIZE_DENOM || config->resize_kf_denom > MAX_RESIZE_DENOM) {
630
0
        SVT_ERROR("Invalid resize-kf-denom %d, should be in the range [%d - %d]\n",
631
0
                  config->resize_kf_denom,
632
0
                  MIN_RESIZE_DENOM,
633
0
                  MAX_RESIZE_DENOM);
634
0
        return_error = EB_ErrorBadParameter;
635
0
    }
636
637
448
    if (config->resize_denom < MIN_RESIZE_DENOM || config->resize_denom > MAX_RESIZE_DENOM) {
638
0
        SVT_ERROR("Invalid resize-denom %d, should be in the range [%d - %d]\n",
639
0
                  config->resize_denom,
640
0
                  MIN_RESIZE_DENOM,
641
0
                  MAX_RESIZE_DENOM);
642
0
        return_error = EB_ErrorBadParameter;
643
0
    }
644
645
448
    if (config->matrix_coefficients == 0 && config->encoder_color_format != EB_YUV444) {
646
0
        SVT_ERROR(
647
0
            "Identity matrix (matrix_coefficient = 0) may be used only with 4:4:4 "
648
0
            "color format.\n");
649
0
        return_error = EB_ErrorBadParameter;
650
0
    }
651
652
448
    if (config->rate_control_mode == SVT_AV1_RC_MODE_VBR && config->intra_period_length == -1) {
653
0
        SVT_ERROR(
654
0
            "keyint = -1 is not supported for modes other than CRF rate control "
655
0
            "encoding modes.\n");
656
0
        return_error = EB_ErrorBadParameter;
657
0
    }
658
    // Block the use of M4 or lower for resolutions higher than 4K, unless allintra coding is used (due to memory constraints)
659
448
    if (!scs->allintra && (uint64_t)(scs->max_input_luma_width * scs->max_input_luma_height) > INPUT_SIZE_4K_TH &&
660
0
        config->enc_mode <= ENC_M4) {
661
0
        SVT_ERROR("8k+ resolution support is limited to M5 and faster presets.\n");
662
0
        return_error = EB_ErrorBadParameter;
663
0
    }
664
448
    if (config->pass > 0 && scs->static_config.enable_overlays) {
665
0
        SVT_ERROR(
666
0
            "The overlay frames feature is currently not supported with multi-pass "
667
0
            "encoding\n");
668
0
        return_error = EB_ErrorBadParameter;
669
0
    }
670
448
    int pass = config->pass;
671
672
448
    if (pass < 0 || pass > 2) {
673
0
        SVT_ERROR("%d pass encode is not supported. --pass has a range of [0-2]\n", pass);
674
0
        return_error = EB_ErrorBadParameter;
675
0
    }
676
677
448
    if (config->intra_refresh_type != 2 && pass > 0) {
678
0
        SVT_ERROR("Multi-pass encode only supports closed-gop configurations.\n");
679
0
        return_error = EB_ErrorBadParameter;
680
0
    }
681
448
    if (config->pass > 1 && config->rate_control_mode == SVT_AV1_RC_MODE_CQP_OR_CRF) {
682
0
        SVT_ERROR("CRF does not support multi-pass. Use single pass.\n");
683
0
        return_error = EB_ErrorBadParameter;
684
0
    }
685
686
448
    if (config->aq_mode == 0 && config->rate_control_mode) {
687
0
        SVT_ERROR("Adaptive quantization can not be turned OFF when RC ON\n");
688
0
        return_error = EB_ErrorBadParameter;
689
0
    }
690
691
448
    if (config->sframe_dist < 0) {
692
0
        SVT_ERROR("Switch frame interval must be >= 0\n");
693
0
        return_error = EB_ErrorBadParameter;
694
0
    }
695
448
    if (config->sframe_dist > 0 && config->hierarchical_levels == 0) {
696
0
        SVT_ERROR("Switch frame feature does not support flat IPPP\n");
697
0
        return_error = EB_ErrorBadParameter;
698
0
    }
699
448
    if (config->sframe_dist > 0 &&
700
0
        (config->sframe_mode < SFRAME_STRICT_BASE || config->sframe_mode > SFRAME_DEC_POSI_BASE)) {
701
0
        SVT_ERROR("Invalid switch frame mode %d, should be in the range [%d - %d]\n",
702
0
                  config->sframe_mode,
703
0
                  SFRAME_STRICT_BASE,
704
0
                  SFRAME_DEC_POSI_BASE);
705
0
        return_error = EB_ErrorBadParameter;
706
0
    }
707
448
    if (config->sframe_posi.sframe_posis && !IS_SFRAME_FLEXIBLE_INSERT(config->sframe_mode)) {
708
0
        SVT_ERROR("S-Frame positions are only supported in S-Frame Flexible ARF mode\n");
709
0
        return_error = EB_ErrorBadParameter;
710
0
    }
711
448
    if (config->sframe_posi.sframe_qp_num && config->rate_control_mode != SVT_AV1_RC_MODE_CQP_OR_CRF) {
712
0
        SVT_ERROR("S-Frame QP feature only supports CRF/CQP rate control mode\n");
713
0
        return_error = EB_ErrorBadParameter;
714
0
    }
715
448
    if ((config->sframe_posi.sframe_qps && config->sframe_posi.sframe_qp_offsets) ||
716
448
        (config->sframe_qp > 0 && config->sframe_qp_offset != 0)) {
717
0
        SVT_ERROR("S-Frame QP feature cannot support QP value and QP offset at same time\n");
718
0
        return_error = EB_ErrorBadParameter;
719
0
    }
720
721
    /* Warnings about the use of features that are incomplete */
722
448
    if (config->aq_mode == 1) {
723
0
        SVT_WARN(
724
0
            "The adaptive quantization mode using segmentation is at a support level "
725
0
            "only to be available for demos, experimentation, and further development uses and "
726
0
            "should not be used for benchmarking until fully implemented.\n");
727
0
    }
728
729
    // color description
730
448
    if (config->color_primaries == 0 || config->color_primaries == 3 ||
731
448
        (config->color_primaries >= 13 && config->color_primaries <= 21) || config->color_primaries > 22) {
732
0
        SVT_WARN("Value %u for color_primaries is reserved and not recommended for usage.\n", config->color_primaries);
733
0
    }
734
448
    if (config->transfer_characteristics == 0 || config->transfer_characteristics == 3 ||
735
448
        config->transfer_characteristics > 18) {
736
0
        SVT_WARN("Value %u for transfer_characteristics is reserved and not recommended for usage.\n",
737
0
                 config->transfer_characteristics);
738
0
    }
739
740
448
    if (config->matrix_coefficients == 3 || config->matrix_coefficients > 14) {
741
0
        SVT_WARN("Value %u for matrix_coefficients is reserved and not recommended for usage.\n",
742
0
                 config->matrix_coefficients);
743
0
    }
744
745
448
    if (config->chroma_sample_position < EB_CSP_UNKNOWN || config->chroma_sample_position > EB_CSP_COLOCATED) {
746
0
        if (config->chroma_sample_position != EB_CSP_RESERVED) {
747
0
            SVT_ERROR("Chroma sample position %d is unknown.\n", config->chroma_sample_position);
748
0
            return_error = EB_ErrorBadParameter;
749
0
        } else {
750
0
            SVT_WARN(
751
0
                "Value %d for chroma_sample_position is reserved "
752
0
                "and not recommended for usage.\n",
753
0
                config->chroma_sample_position);
754
0
        }
755
0
    }
756
757
448
    if (config->film_grain_denoise_strength > 0 && config->enc_mode > 6) {
758
0
        SVT_WARN(
759
0
            "It is recommended to not use Film Grain for presets greater than 6 as it "
760
0
            "produces a significant compute overhead. This combination should only be used for "
761
0
            "debug purposes.\n");
762
0
    }
763
764
448
    if (config->film_grain_denoise_strength > 50) {
765
0
        SVT_ERROR(
766
0
            "Film grain denoise strength is only supported for values between "
767
0
            "[0,50]\n");
768
0
        return_error = EB_ErrorBadParameter;
769
0
    }
770
771
448
    if (config->film_grain_denoise_apply != 0 && config->film_grain_denoise_apply != 1) {
772
0
        SVT_ERROR("The film grain denoise apply signal can only have a value of 0 or 1\n");
773
0
        return_error = EB_ErrorBadParameter;
774
0
    }
775
776
    // Limit 8K & 16K support
777
448
    if ((uint64_t)(scs->max_input_luma_width * scs->max_input_luma_height) > INPUT_SIZE_4K_TH) {
778
0
        SVT_WARN(
779
0
            "8K and higher resolution support is currently a work-in-progress "
780
0
            "project, and is only available for demos, experimentation, and further development "
781
0
            "uses and should not be used for benchmarking until fully implemented.\n");
782
0
    }
783
784
448
    if (config->pred_structure == LOW_DELAY) {
785
0
        if (config->tune == TUNE_VQ) {
786
0
            SVT_WARN("Tune 0 is not applicable for low-delay, tune will be forced to 1.\n");
787
0
            config->tune = TUNE_PSNR;
788
0
        }
789
790
0
        if (config->superres_mode != 0) {
791
0
            SVT_ERROR("Superres is not supported for low-delay.\n");
792
0
            return_error = EB_ErrorBadParameter;
793
0
        }
794
795
0
        if (config->enable_overlays) {
796
0
            SVT_ERROR("Overlay is not supported for low-delay.\n");
797
0
            return_error = EB_ErrorBadParameter;
798
0
        }
799
0
    }
800
801
448
    if (!config->rtc && config->enc_mode >= ENC_M10) {
802
0
        SVT_WARN("Non-RTC M10+ are meant for automation tooling usage. Visual artifacts may occur otherwise.\n");
803
0
    }
804
805
448
    if (scs->static_config.scene_change_detection) {
806
0
        scs->static_config.scene_change_detection = 0;
807
0
        SVT_WARN(
808
0
            "SVT-AV1 has an integrated mode decision mechanism to handle scene changes and will "
809
0
            "not insert a key frame at scene changes\n");
810
0
    }
811
448
    if ((config->tile_columns > 0 || config->tile_rows > 0)) {
812
448
        SVT_WARN(
813
448
            "If you are using tiles with the intent of increasing the decoder speed, please also "
814
448
            "consider using --fast-decode 1 or 2, especially if the intended decoder is running with "
815
448
            "limited multi-threading capabilities.\n");
816
448
    }
817
448
    if (config->tune == TUNE_VQ && config->fast_decode > 0) {
818
0
        SVT_WARN(
819
0
            "--fast-decode has been developed and optimized with --tune 1. "
820
0
            "Please use it with caution when encoding with --tune 0. You can also consider using "
821
0
            "--tile-columns 1 if you are targeting a high quality encode and a multi-core "
822
0
            "high-performance decoder HW\n");
823
0
    }
824
448
    if (config->enable_qm && config->min_qm_level > config->max_qm_level) {
825
0
        SVT_ERROR("Min quant matrix level must not greater than max quant matrix level\n");
826
0
        return_error = EB_ErrorBadParameter;
827
0
    }
828
448
    if (config->enable_qm && config->min_chroma_qm_level > config->max_chroma_qm_level) {
829
0
        SVT_ERROR("Min chroma quant matrix level must not greater than max chroma quant matrix level\n");
830
0
        return_error = EB_ErrorBadParameter;
831
0
    }
832
448
    if (config->startup_mg_size != 0 && config->startup_mg_size != 2 && config->startup_mg_size != 3 &&
833
0
        config->startup_mg_size != 4) {
834
0
        SVT_ERROR("Startup MG size supported [0, 2, 3, 4]\n");
835
0
        return_error = EB_ErrorBadParameter;
836
0
    }
837
448
    if (config->startup_mg_size > config->hierarchical_levels) {
838
0
        SVT_ERROR("Startup MG size must less than or equal to hierarchical levels\n");
839
0
        return_error = EB_ErrorBadParameter;
840
0
    }
841
448
    if (config->startup_mg_size != 0 && config->rate_control_mode != SVT_AV1_RC_MODE_CQP_OR_CRF) {
842
0
        SVT_ERROR("Startup MG size feature only supports CRF/CQP rate control mode\n");
843
0
        return_error = EB_ErrorBadParameter;
844
0
    }
845
448
    if (config->startup_qp_offset != 0 && config->rate_control_mode != 0) {
846
0
        SVT_ERROR("Startup QP offset only supports CRF/CQP rate control mode\n");
847
0
        return_error = EB_ErrorBadParameter;
848
0
    }
849
850
448
    if (config->variance_boost_strength < 1 || config->variance_boost_strength > 4) {
851
0
        SVT_ERROR("Variance Boost strength must be between 1 and 4\n");
852
0
        return_error = EB_ErrorBadParameter;
853
0
    }
854
855
448
    if (config->variance_octile < 1 || config->variance_octile > 8) {
856
0
        SVT_ERROR("Variance Boost octile must be between 1 and 8\n");
857
0
        return_error = EB_ErrorBadParameter;
858
0
    }
859
860
448
    if (config->tf_strength > 4) {
861
0
        SVT_ERROR("Temporal filtering strength must be between 0 and 4\n");
862
0
        return_error = EB_ErrorBadParameter;
863
0
    }
864
865
448
    if (config->variance_boost_curve > 2) {
866
0
        SVT_ERROR("Variance Boost curve must be between 0 and 2\n");
867
0
        return_error = EB_ErrorBadParameter;
868
0
    }
869
870
448
    if (config->luminance_qp_bias > 100) {
871
0
        SVT_ERROR("Luminance-based QP bias value must be between 0 and 100\n");
872
0
        return_error = EB_ErrorBadParameter;
873
0
    }
874
875
448
    if (config->sharpness > 7 || config->sharpness < -7) {
876
0
        SVT_ERROR("Sharpness level must be between -7 and 7\n");
877
0
        return_error = EB_ErrorBadParameter;
878
0
    }
879
880
448
    if (config->qp_scale_compress_strength > 3) {
881
0
        SVT_ERROR("QP scale compress strength must be between 0 and 3\n");
882
0
        return_error = EB_ErrorBadParameter;
883
0
    }
884
885
448
    if (config->max_tx_size != 32 && config->max_tx_size != 64) {
886
0
        SVT_ERROR("Supported Max TX size values are 32 and 64\n");
887
0
        return_error = EB_ErrorBadParameter;
888
0
    }
889
890
448
    if (config->ac_bias > 8.0 || config->ac_bias < 0.0) {
891
0
        SVT_ERROR("AC bias strength must be between 0.0 and 8.0\n");
892
0
        return_error = EB_ErrorBadParameter;
893
0
    }
894
895
    //User configurable High Bit Depth Mode Decision Setting
896
448
    if (config->hbd_mds < -1 || config->hbd_mds > 2) {
897
0
        SVT_ERROR("hbd-mds must be -1 (preset default), 0, 1, or 2\n");
898
0
        return_error = EB_ErrorBadParameter;
899
0
    }
900
901
448
    if (config->encoder_bit_depth == 8 && (config->hbd_mds == 1 || config->hbd_mds == 2)) {
902
0
        SVT_WARN("Please use 10-bit encoding if you want to take advantage of hbd-mds 1 and 2.\n");
903
0
        SVT_ERROR("Full high bit depth and hybrid 8/10 mode decision are not supported when encoder bit depth is 8\n");
904
0
        return_error = EB_ErrorBadParameter;
905
0
    }
906
907
448
    return return_error;
908
448
}
909
910
/**********************************
911
Set Default Library Params
912
**********************************/
913
448
EbErrorType svt_av1_set_default_params(EbSvtAv1EncConfiguration* config_ptr) {
914
448
    EbErrorType return_error = EB_ErrorNone;
915
916
448
    if (!config_ptr) {
917
0
        SVT_ERROR("The EbSvtAv1EncConfiguration structure is empty!\n");
918
0
        return EB_ErrorBadParameter;
919
0
    }
920
448
    config_ptr->frame_rate_numerator     = 60000;
921
448
    config_ptr->frame_rate_denominator   = 1000;
922
448
    config_ptr->encoder_bit_depth        = 8;
923
448
    config_ptr->source_width             = 0;
924
448
    config_ptr->source_height            = 0;
925
448
    config_ptr->forced_max_frame_width   = 0;
926
448
    config_ptr->forced_max_frame_height  = 0;
927
448
    config_ptr->stat_report              = 0;
928
448
    config_ptr->tile_rows                = DEFAULT;
929
448
    config_ptr->tile_columns             = DEFAULT;
930
448
    config_ptr->qp                       = DEFAULT_QP;
931
448
    config_ptr->use_qp_file              = false;
932
448
    config_ptr->use_fixed_qindex_offsets = 0;
933
448
    memset(config_ptr->qindex_offsets, 0, sizeof(config_ptr->qindex_offsets));
934
448
    config_ptr->key_frame_chroma_qindex_offset = 0;
935
448
    config_ptr->key_frame_qindex_offset        = 0;
936
448
    memset(config_ptr->chroma_qindex_offsets, 0, sizeof(config_ptr->chroma_qindex_offsets));
937
448
    config_ptr->luma_y_dc_qindex_offset   = 0;
938
448
    config_ptr->chroma_u_dc_qindex_offset = 0;
939
448
    config_ptr->chroma_u_ac_qindex_offset = 0;
940
448
    config_ptr->chroma_v_dc_qindex_offset = 0;
941
448
    config_ptr->chroma_v_ac_qindex_offset = 0;
942
943
3.58k
    for (int i = 0; i < SVT_AV1_FRAME_UPDATE_TYPES; i++) {
944
3.13k
        config_ptr->lambda_scale_factors[i] = 128;
945
3.13k
    }
946
947
448
    config_ptr->scene_change_detection       = 0;
948
448
    config_ptr->rate_control_mode            = SVT_AV1_RC_MODE_CQP_OR_CRF;
949
448
    config_ptr->look_ahead_distance          = (uint32_t)~0;
950
448
    config_ptr->target_bit_rate              = DEFAULT_TBR;
951
448
    config_ptr->max_bit_rate                 = 0;
952
448
    config_ptr->max_qp_allowed               = 63;
953
448
    config_ptr->min_qp_allowed               = MIN_QP_AUTO;
954
448
    config_ptr->aq_mode                      = 2;
955
448
    config_ptr->enc_mode                     = ENC_M8;
956
448
    config_ptr->intra_period_length          = -2;
957
448
    config_ptr->multiply_keyint              = false;
958
448
    config_ptr->intra_refresh_type           = 2;
959
448
    config_ptr->hierarchical_levels          = HIERARCHICAL_LEVELS_AUTO;
960
448
    config_ptr->pred_structure               = RANDOM_ACCESS;
961
448
    config_ptr->enable_dlf_flag              = 1;
962
448
    config_ptr->cdef_level                   = DEFAULT;
963
448
    config_ptr->enable_restoration_filtering = DEFAULT;
964
448
    config_ptr->enable_mfmv                  = DEFAULT;
965
448
    config_ptr->enable_dg                    = 1;
966
448
    config_ptr->fast_decode                  = 0;
967
448
    config_ptr->encoder_color_format         = EB_YUV420;
968
448
    config_ptr->rtc                          = 0;
969
    // Rate control options
970
    // Set the default value toward more flexible rate allocation
971
448
    config_ptr->vbr_min_section_pct      = 0;
972
448
    config_ptr->vbr_max_section_pct      = 2000;
973
448
    config_ptr->under_shoot_pct          = (uint32_t)DEFAULT;
974
448
    config_ptr->over_shoot_pct           = (uint32_t)DEFAULT;
975
448
    config_ptr->mbr_over_shoot_pct       = 50;
976
448
    config_ptr->max_intra_bitrate_pct    = 300;
977
448
    config_ptr->max_inter_bitrate_pct    = 0;
978
448
    config_ptr->gop_constraint_rc        = 0;
979
448
    config_ptr->maximum_buffer_size_ms   = 1000; // default settings for CBR
980
448
    config_ptr->starting_buffer_level_ms = 600; // default settings for CBR
981
448
    config_ptr->optimal_buffer_level_ms  = 600; // default settings for CBR
982
448
    config_ptr->recode_loop              = ALLOW_RECODE_DEFAULT;
983
448
    config_ptr->screen_content_mode      = 2;
984
448
    config_ptr->enable_intrabc           = true;
985
986
    // Annex A parameters
987
448
    config_ptr->profile = 0;
988
448
    config_ptr->tier    = 0;
989
448
    config_ptr->level   = 0;
990
991
    // Film grain denoising
992
448
    config_ptr->film_grain_denoise_strength = 0;
993
448
    config_ptr->film_grain_denoise_apply    = 0;
994
995
    // CPU Flags
996
448
    config_ptr->use_cpu_flags = EB_CPU_FLAGS_ALL;
997
998
    // Channel info
999
448
    config_ptr->level_of_parallelism = 0;
1000
1001
    // Debug info
1002
448
    config_ptr->recon_enabled = 0;
1003
1004
    // Alt-Ref default values
1005
448
    config_ptr->enable_tf       = 1;
1006
448
    config_ptr->enable_tf_key   = 1;
1007
448
    config_ptr->enable_overlays = false;
1008
448
    config_ptr->tune            = 1;
1009
    // Super-resolution default values
1010
448
    config_ptr->superres_mode      = SUPERRES_NONE;
1011
448
    config_ptr->superres_denom     = SCALE_NUMERATOR;
1012
448
    config_ptr->superres_kf_denom  = SCALE_NUMERATOR;
1013
448
    config_ptr->superres_qthres    = 43; // random threshold, change
1014
448
    config_ptr->superres_kf_qthres = 43; // random threshold, change
1015
1016
    // Reference Scaling default values
1017
448
    config_ptr->resize_mode     = RESIZE_NONE;
1018
448
    config_ptr->resize_denom    = SCALE_NUMERATOR;
1019
448
    config_ptr->resize_kf_denom = SCALE_NUMERATOR;
1020
1021
    // Color description default values
1022
448
    config_ptr->color_primaries          = 2;
1023
448
    config_ptr->transfer_characteristics = 2;
1024
448
    config_ptr->matrix_coefficients      = 2;
1025
448
    config_ptr->color_range              = EB_CR_STUDIO_RANGE;
1026
448
    config_ptr->chroma_sample_position   = EB_CSP_UNKNOWN;
1027
448
    config_ptr->pass                     = 0;
1028
448
    memset(&config_ptr->mastering_display, 0, sizeof(config_ptr->mastering_display));
1029
448
    memset(&config_ptr->content_light_level, 0, sizeof(config_ptr->content_light_level));
1030
1031
    // Switch frame default values
1032
448
    config_ptr->sframe_dist      = 0;
1033
448
    config_ptr->sframe_mode      = SFRAME_NEAREST_BASE;
1034
448
    config_ptr->force_key_frames = 0;
1035
1036
    // Quant Matrices (QM)
1037
448
    config_ptr->enable_qm           = 0;
1038
448
    config_ptr->min_qm_level        = 8;
1039
448
    config_ptr->max_qm_level        = 15;
1040
448
    config_ptr->min_chroma_qm_level = 8;
1041
448
    config_ptr->max_chroma_qm_level = 15;
1042
1043
448
    config_ptr->startup_mg_size                   = 0;
1044
448
    config_ptr->startup_qp_offset                 = 0;
1045
448
    config_ptr->frame_scale_evts.evt_num          = 0;
1046
448
    config_ptr->frame_scale_evts.resize_denoms    = NULL;
1047
448
    config_ptr->frame_scale_evts.resize_kf_denoms = NULL;
1048
448
    config_ptr->frame_scale_evts.start_frame_nums = NULL;
1049
448
    config_ptr->enable_roi_map                    = false;
1050
448
    config_ptr->fgs_table                         = NULL;
1051
448
    config_ptr->enable_variance_boost             = false;
1052
448
    config_ptr->variance_boost_strength           = 2;
1053
448
    config_ptr->variance_octile                   = 5;
1054
448
    config_ptr->tf_strength                       = 3;
1055
448
    config_ptr->variance_boost_curve              = 0;
1056
448
    config_ptr->luminance_qp_bias                 = 0;
1057
448
    config_ptr->sharpness                         = 0;
1058
448
    config_ptr->lossless                          = false;
1059
448
    config_ptr->avif                              = false;
1060
448
    config_ptr->qp_scale_compress_strength        = 0;
1061
448
    config_ptr->sframe_posi.sframe_num            = 0;
1062
448
    config_ptr->sframe_posi.sframe_posis          = NULL;
1063
448
    config_ptr->sframe_posi.sframe_qp_num         = 0;
1064
448
    config_ptr->sframe_posi.sframe_qps            = NULL;
1065
448
    config_ptr->sframe_posi.sframe_qp_offsets     = NULL;
1066
448
    config_ptr->sframe_qp                         = 0;
1067
448
    config_ptr->sframe_qp_offset                  = 0;
1068
448
    config_ptr->adaptive_film_grain               = true;
1069
448
    config_ptr->max_tx_size                       = 64;
1070
448
    config_ptr->extended_crf_qindex_offset        = 0;
1071
448
    config_ptr->ac_bias                           = 0.0;
1072
448
    config_ptr->hbd_mds                           = DEFAULT;
1073
1074
    // Ref-frame management disabled by default → legacy bit-exact behavior
1075
    // and no extra ref-buffer memory allocated.
1076
448
    config_ptr->max_managed_refs = 0;
1077
1078
448
    return return_error;
1079
448
}
1080
1081
448
static const char* tier_to_str(unsigned in) {
1082
448
    if (!in) {
1083
448
        return "(auto)";
1084
448
    }
1085
0
    static char ret[11];
1086
0
    snprintf(ret, 11, "%u", in);
1087
0
    return ret;
1088
448
}
1089
1090
448
static const char* level_to_str(unsigned in) {
1091
448
    if (!in) {
1092
448
        return "(auto)";
1093
448
    }
1094
0
    static char ret[313];
1095
0
    snprintf(ret, 313, "%.1f", in / 10.0);
1096
0
    return ret;
1097
448
}
1098
1099
248
static double get_extended_crf(EbSvtAv1EncConfiguration* config_ptr) {
1100
248
    return (double)config_ptr->qp + (double)config_ptr->extended_crf_qindex_offset / 4;
1101
248
}
1102
1103
//#define DEBUG_BUFFERS
1104
448
void svt_av1_print_lib_params(SequenceControlSet* scs) {
1105
448
    EbSvtAv1EncConfiguration* config = &scs->static_config;
1106
1107
448
    SVT_INFO("-------------------------------------------\n");
1108
448
    if (config->pass == ENC_FIRST_PASS) {
1109
0
        SVT_INFO("SVT [config]: preset \t\t\t\t\t\t\t: Pass 1\n");
1110
448
    } else {
1111
448
        SVT_INFO("SVT [config]: %s\ttier %s\tlevel %s\n",
1112
448
                 config->profile == MAIN_PROFILE               ? "main profile"
1113
448
                     : config->profile == HIGH_PROFILE         ? "high profile"
1114
448
                     : config->profile == PROFESSIONAL_PROFILE ? "professional profile"
1115
448
                                                               : "Unknown profile",
1116
448
                 tier_to_str(config->tier),
1117
448
                 level_to_str(config->level));
1118
448
        SVT_INFO(
1119
448
            "SVT [config]: width / height / fps numerator / fps denominator \t\t: %d / %d / %d / "
1120
448
            "%d\n",
1121
448
            config->source_width,
1122
448
            config->source_height,
1123
448
            config->frame_rate_numerator,
1124
448
            config->frame_rate_denominator);
1125
448
        SVT_INFO(
1126
448
            "SVT [config]: bit-depth / color format \t\t\t\t\t: %d / "
1127
448
            "%s\n",
1128
448
            config->encoder_bit_depth,
1129
448
            config->encoder_color_format == EB_YUV400       ? "YUV400"
1130
448
                : config->encoder_color_format == EB_YUV420 ? "YUV420"
1131
448
                : config->encoder_color_format == EB_YUV422 ? "YUV422"
1132
448
                : config->encoder_color_format == EB_YUV444 ? "YUV444"
1133
448
                                                            : "Unknown color format");
1134
1135
448
        SVT_INFO("SVT [config]: preset / tune / pred struct \t\t\t\t\t: %d / %s / %s\n",
1136
448
                 config->enc_mode,
1137
448
                 config->tune == TUNE_VQ            ? "VQ"
1138
448
                     : config->tune == TUNE_PSNR    ? "PSNR"
1139
448
                     : config->tune == TUNE_SSIM    ? "SSIM"
1140
448
                     : config->tune == TUNE_MS_SSIM ? "MS_SSIM"
1141
448
                     : config->tune == TUNE_VMAF    ? "VMAF"
1142
448
                                                    : "IQ",
1143
448
                 config->pred_structure == LOW_DELAY           ? "low delay"
1144
448
                     : config->pred_structure == RANDOM_ACCESS ? "random access"
1145
448
                     : config->pred_structure == ALL_INTRA     ? "all intra"
1146
448
                                                               : "Unknown pred structure");
1147
448
        SVT_INFO(
1148
448
            "SVT [config]: gop size / mini-gop size / key-frame type \t\t\t: "
1149
448
            "%d / %d / %s\n",
1150
448
            config->intra_period_length + 1,
1151
448
            (1 << config->hierarchical_levels),
1152
448
            config->intra_refresh_type == SVT_AV1_FWDKF_REFRESH    ? "FWD key frame"
1153
448
                : config->intra_refresh_type == SVT_AV1_KF_REFRESH ? "key frame"
1154
448
                                                                   : "Unknown key frame type");
1155
448
        if (config->lossless) {
1156
200
            SVT_INFO("SVT [config]: BRC mode\t\t\t\t\t\t\t: Lossless Coding \n");
1157
248
        } else {
1158
248
            switch (config->rate_control_mode) {
1159
248
            case SVT_AV1_RC_MODE_CQP_OR_CRF:
1160
248
                if (config->max_bit_rate) {
1161
0
                    SVT_INFO(
1162
0
                        "SVT [config]: BRC mode / %s / max bitrate (kbps)\t\t\t: %s / %.2f / "
1163
0
                        "%d\n",
1164
0
                        scs->tpl || scs->static_config.enable_variance_boost ? "rate factor" : "CQP Assignment",
1165
0
                        scs->tpl || scs->static_config.enable_variance_boost ? "capped CRF" : "CQP",
1166
0
                        get_extended_crf(config),
1167
0
                        (int)config->max_bit_rate / 1000);
1168
248
                } else {
1169
248
                    SVT_INFO("SVT [config]: BRC mode / %s \t\t\t\t\t: %s / %.2f \n",
1170
248
                             scs->tpl || scs->static_config.enable_variance_boost ? "rate factor" : "CQP Assignment",
1171
248
                             scs->tpl || scs->static_config.enable_variance_boost ? "CRF" : "CQP",
1172
248
                             get_extended_crf(config));
1173
248
                }
1174
248
                break;
1175
0
            case SVT_AV1_RC_MODE_VBR:
1176
0
                SVT_INFO("SVT [config]: BRC mode / target bitrate (kbps)\t\t\t\t: VBR / %d \n",
1177
0
                         (int)config->target_bit_rate / 1000);
1178
0
                break;
1179
0
            case SVT_AV1_RC_MODE_CBR:
1180
0
                SVT_INFO(
1181
0
                    "SVT [config]: BRC mode / target bitrate (kbps)\t\t\t\t: CBR "
1182
0
                    "/ %d\n",
1183
0
                    (int)config->target_bit_rate / 1000);
1184
0
                break;
1185
248
            }
1186
248
        }
1187
448
        if (config->rate_control_mode != SVT_AV1_RC_MODE_CBR) {
1188
448
            if (!config->enable_variance_boost) {
1189
448
                SVT_INFO("SVT [config]: AQ mode / Variance Boost \t\t\t\t\t: %d / %d\n",
1190
448
                         config->aq_mode,
1191
448
                         config->enable_variance_boost);
1192
448
            } else {
1193
0
                SVT_INFO("SVT [config]: AQ mode / Variance Boost strength / octile / curve \t\t: %d / %d / %d / %d\n",
1194
0
                         config->aq_mode,
1195
0
                         config->variance_boost_strength,
1196
0
                         config->variance_octile,
1197
0
                         config->variance_boost_curve);
1198
0
            }
1199
448
        }
1200
1201
448
        if (config->film_grain_denoise_strength != 0) {
1202
0
            if (config->adaptive_film_grain) {
1203
0
                SVT_INFO(
1204
0
                    "SVT [config]: film grain synth / denoising / level / adaptive blocksize \t: %d / %d / %d / True\n",
1205
0
                    1,
1206
0
                    config->film_grain_denoise_apply,
1207
0
                    config->film_grain_denoise_strength);
1208
0
            } else {
1209
0
                SVT_INFO(
1210
0
                    "SVT [config]: film grain synth / denoising / level / adaptive blocksize \t: %d / %d / %d / "
1211
0
                    "False\n",
1212
0
                    1,
1213
0
                    config->film_grain_denoise_apply,
1214
0
                    config->film_grain_denoise_strength);
1215
0
            }
1216
0
        }
1217
448
        SVT_INFO("SVT [config]: sharpness / luminance-based QP bias \t\t\t\t: %d / %d\n",
1218
448
                 config->sharpness,
1219
448
                 config->luminance_qp_bias);
1220
1221
448
        switch (config->enable_tf) {
1222
0
        case 1:
1223
0
            if (config->tf_strength != 3) {
1224
0
                SVT_INFO("SVT [config]: temporal filtering strength \t\t\t\t\t: %d\n", config->tf_strength);
1225
0
            }
1226
0
            break;
1227
0
        case 2:
1228
0
            SVT_INFO("SVT [config]: temporal filtering strength \t\t\t\t\t: auto\n");
1229
0
            break;
1230
448
        default:
1231
448
            break;
1232
448
        }
1233
1234
448
        SVT_INFO("SVT [config]: QP scale compress strength \t\t\t\t\t: %d\n", config->qp_scale_compress_strength);
1235
1236
448
        if (config->ac_bias) {
1237
0
            SVT_INFO("SVT [config]: AC Bias Strength \t\t\t\t\t\t: %.2f\n", config->ac_bias);
1238
0
        }
1239
1240
448
        if (config->hbd_mds != DEFAULT) {
1241
0
            SVT_INFO("SVT [config]: High Bit Depth Mode Decision setting \t\t\t\t\t: %d\n", config->hbd_mds);
1242
0
        }
1243
448
    }
1244
#if DEBUG_BUFFERS
1245
    SVT_INFO("SVT [config]: INPUT / OUTPUT \t\t\t\t\t\t\t: %d / %d\n",
1246
             scs->input_buffer_fifo_init_count,
1247
             scs->output_stream_buffer_fifo_init_count);
1248
    SVT_INFO("SVT [config]: CPCS / PAREF / REF / ME\t\t\t\t\t\t: %d / %d / %d / %d\n",
1249
             scs->picture_control_set_pool_init_count_child,
1250
             scs->pa_reference_picture_buffer_init_count,
1251
             scs->reference_picture_buffer_init_count,
1252
             scs->me_pool_init_count);
1253
    SVT_INFO("SVT [config]: ME_SEG_W / ME_SEG_H \t\t\t: %u / %u\n",
1254
             scs->me_segment_col_count_array,
1255
             scs->me_segment_row_count_array);
1256
    SVT_INFO("SVT [config]: ENC_DEC_SEG_W / ENC_DEC_SEG_H \t\t\t: %u / %u\n",
1257
             scs->enc_dec_segment_col_count_array,
1258
             scs->enc_dec_segment_row_count_array);
1259
    SVT_INFO(
1260
        "SVT [config]: PA_P / ME_P / SBO_P / MDC_P / ED_P / EC_P \t\t\t: %d / %d / %d / %d / %d / "
1261
        "%d\n",
1262
        scs->picture_analysis_process_init_count,
1263
        scs->motion_estimation_process_init_count,
1264
        scs->source_based_operations_process_init_count,
1265
        scs->mode_decision_configuration_process_init_count,
1266
        scs->enc_dec_process_init_count,
1267
        scs->entropy_coding_process_init_count);
1268
    SVT_INFO("SVT [config]: DLF_P / CDEF_P / REST_P \t\t\t\t\t\t: %d / %d / %d\n",
1269
             scs->dlf_process_init_count,
1270
             scs->cdef_process_init_count,
1271
             scs->rest_process_init_count);
1272
#endif
1273
448
    SVT_INFO("-------------------------------------------\n");
1274
1275
448
    fflush(stdout);
1276
448
}
1277
1278
/**********************************
1279
* Parse Single Parameter
1280
**********************************/
1281
1282
0
static EbErrorType str_to_int64(const char* nptr, int64_t* out, char** nextptr) {
1283
0
    char*   endptr;
1284
0
    int64_t val;
1285
1286
0
    val = strtoll(nptr, &endptr, 0);
1287
1288
0
    if (endptr == nptr || (!nextptr && *endptr)) {
1289
0
        return EB_ErrorBadParameter;
1290
0
    }
1291
1292
0
    *out = val;
1293
0
    if (nextptr) {
1294
0
        *nextptr = endptr;
1295
0
    }
1296
0
    return EB_ErrorNone;
1297
0
}
1298
1299
0
static EbErrorType str_to_int(const char* nptr, int32_t* out, char** nextptr) {
1300
0
    char*   endptr;
1301
0
    int32_t val;
1302
1303
0
    val = strtol(nptr, &endptr, 0);
1304
1305
0
    if (endptr == nptr || (!nextptr && *endptr)) {
1306
0
        return EB_ErrorBadParameter;
1307
0
    }
1308
1309
0
    *out = val;
1310
0
    if (nextptr) {
1311
0
        *nextptr = endptr;
1312
0
    }
1313
0
    return EB_ErrorNone;
1314
0
}
1315
1316
0
static EbErrorType str_to_uint64(const char* nptr, uint64_t* out, char** nextptr) {
1317
0
    char*    endptr;
1318
0
    uint64_t val;
1319
1320
0
    if (strtoll(nptr, NULL, 0) < 0) {
1321
0
        return EB_ErrorBadParameter;
1322
0
    }
1323
1324
0
    val = strtoull(nptr, &endptr, 0);
1325
1326
0
    if (endptr == nptr || (!nextptr && *endptr)) {
1327
0
        return EB_ErrorBadParameter;
1328
0
    }
1329
1330
0
    *out = val;
1331
0
    if (nextptr) {
1332
0
        *nextptr = endptr;
1333
0
    }
1334
0
    return EB_ErrorNone;
1335
0
}
1336
1337
0
static EbErrorType str_to_uint(const char* nptr, uint32_t* out, char** nextptr) {
1338
0
    char*    endptr;
1339
0
    uint32_t val;
1340
1341
0
    if (strtol(nptr, NULL, 0) < 0) {
1342
0
        return EB_ErrorBadParameter;
1343
0
    }
1344
1345
0
    val = strtoul(nptr, &endptr, 0);
1346
1347
0
    if (endptr == nptr || (!nextptr && *endptr)) {
1348
0
        return EB_ErrorBadParameter;
1349
0
    }
1350
1351
0
    *out = val;
1352
0
    if (nextptr) {
1353
0
        *nextptr = endptr;
1354
0
    }
1355
0
    return EB_ErrorNone;
1356
0
}
1357
1358
0
static EbErrorType str_to_int8(const char* nptr, int8_t* out, char** nextptr) {
1359
0
    char*   endptr;
1360
0
    int32_t val;
1361
1362
0
    val = strtol(nptr, &endptr, 0);
1363
1364
0
    if (endptr == nptr || (!nextptr && *endptr)) {
1365
0
        return EB_ErrorBadParameter;
1366
0
    }
1367
1368
    // check for the range
1369
0
    if (val < INT8_MIN || val > INT8_MAX) {
1370
0
        return EB_ErrorBadParameter;
1371
0
    }
1372
1373
0
    *out = (int8_t)val;
1374
0
    if (nextptr) {
1375
0
        *nextptr = endptr;
1376
0
    }
1377
0
    return EB_ErrorNone;
1378
0
}
1379
1380
0
static EbErrorType str_to_uint8(const char* nptr, uint8_t* out, char** nextptr) {
1381
0
    char*    endptr;
1382
0
    uint32_t val;
1383
1384
0
    if (strtol(nptr, NULL, 0) < 0) {
1385
0
        return EB_ErrorBadParameter;
1386
0
    }
1387
1388
0
    val = strtoul(nptr, &endptr, 0);
1389
1390
0
    if (endptr == nptr || (!nextptr && *endptr)) {
1391
0
        return EB_ErrorBadParameter;
1392
0
    }
1393
1394
    // check for the range
1395
0
    if (val > UINT8_MAX) {
1396
0
        return EB_ErrorBadParameter;
1397
0
    }
1398
1399
0
    *out = (uint8_t)val;
1400
0
    if (nextptr) {
1401
0
        *nextptr = endptr;
1402
0
    }
1403
0
    return EB_ErrorNone;
1404
0
}
1405
1406
0
#define str_to_int32 str_to_int
1407
0
#define str_to_uint32 str_to_uint
1408
1409
0
static EbErrorType str_to_double(const char* nptr, double* out, char** nextptr) {
1410
0
    char*  endptr;
1411
0
    double val;
1412
1413
0
    val = strtod(nptr, &endptr);
1414
1415
0
    if (endptr == nptr || (!nextptr && *endptr)) {
1416
0
        return EB_ErrorBadParameter;
1417
0
    }
1418
1419
0
    *out = val;
1420
0
    if (nextptr) {
1421
0
        *nextptr = endptr;
1422
0
    }
1423
0
    return EB_ErrorNone;
1424
0
}
1425
1426
//assume the input list of values are in the format of "[v1,v2,v3,...]"
1427
#define PARSE_LIST(list_type)                                                                    \
1428
0
    static EbErrorType parse_list_##list_type(const char* nptr, list_type##_t* list, size_t n) { \
1429
0
        const char* ptr = nptr;                                                                  \
1430
0
        char*       endptr;                                                                      \
1431
0
        size_t      i = 0;                                                                       \
1432
0
        memset(list, 0, n * sizeof(*list));                                                      \
1433
0
        while (*ptr) {                                                                           \
1434
0
            if (*ptr == '[' || *ptr == ']') {                                                    \
1435
0
                ptr++;                                                                           \
1436
0
                continue;                                                                        \
1437
0
            }                                                                                    \
1438
0
            list_type##_t rawval;                                                                \
1439
0
            EbErrorType   err = str_to_##list_type(ptr, &rawval, &endptr);                       \
1440
0
            if (err != EB_ErrorNone)                                                             \
1441
0
                return err;                                                                      \
1442
0
            if (i >= n) {                                                                        \
1443
0
                return EB_ErrorBadParameter;                                                     \
1444
0
            } else if (*endptr == ',' || *endptr == ']') {                                       \
1445
0
                endptr++;                                                                        \
1446
0
            } else if (*endptr) {                                                                \
1447
0
                return EB_ErrorBadParameter;                                                     \
1448
0
            }                                                                                    \
1449
0
            list[i++] = rawval;                                                                  \
1450
0
            ptr       = endptr;                                                                  \
1451
0
        }                                                                                        \
1452
0
        return EB_ErrorNone;                                                                     \
1453
0
    }
Unexecuted instantiation: enc_settings.c:parse_list_int32
Unexecuted instantiation: enc_settings.c:parse_list_uint64
Unexecuted instantiation: enc_settings.c:parse_list_uint32
Unexecuted instantiation: enc_settings.c:parse_list_uint8
Unexecuted instantiation: enc_settings.c:parse_list_int8
1454
PARSE_LIST(int8)
1455
PARSE_LIST(uint8)
1456
PARSE_LIST(int32)
1457
PARSE_LIST(uint32)
1458
PARSE_LIST(uint64)
1459
1460
0
static uint32_t count_params(const char* nptr) {
1461
0
    const char* ptr = nptr;
1462
0
    char*       endptr;
1463
0
    uint32_t    i = 0;
1464
0
    while (*ptr) {
1465
0
        if (*ptr == '[' || *ptr == ']') {
1466
0
            ptr++;
1467
0
            continue;
1468
0
        }
1469
1470
0
        strtoll(ptr, &endptr, 10);
1471
0
        if (*endptr == ',' || *endptr == ']') {
1472
0
            endptr++;
1473
0
        } else if (*endptr) {
1474
0
            return i;
1475
0
        }
1476
0
        i++;
1477
0
        ptr = endptr;
1478
0
    }
1479
0
    return i;
1480
0
}
1481
1482
#ifdef _MSC_VER
1483
#define strcasecmp _stricmp
1484
#endif
1485
0
static EbErrorType str_to_bool(const char* nptr, bool* out) {
1486
0
    bool val;
1487
0
    if (!strcmp(nptr, "1") || !strcasecmp(nptr, "true") || !strcasecmp(nptr, "yes")) {
1488
0
        val = true;
1489
0
    } else if (!strcmp(nptr, "0") || !strcasecmp(nptr, "false") || !strcasecmp(nptr, "no")) {
1490
0
        val = false;
1491
0
    } else {
1492
0
        return EB_ErrorBadParameter;
1493
0
    }
1494
1495
0
    *out = val;
1496
0
    return EB_ErrorNone;
1497
0
}
1498
1499
0
static EbErrorType str_to_crf(const char* nptr, EbSvtAv1EncConfiguration* config_struct) {
1500
0
    double      crf;
1501
0
    EbErrorType return_error;
1502
1503
0
    return_error = str_to_double(nptr, &crf, NULL);
1504
1505
0
    if (return_error == EB_ErrorBadParameter) {
1506
0
        return return_error;
1507
0
    }
1508
0
    if (crf < 0) {
1509
0
        return EB_ErrorBadParameter;
1510
0
    }
1511
1512
0
    uint32_t extended_q_index           = (uint32_t)(crf * 4);
1513
0
    uint32_t qp                         = AOMMIN(MAX_QP_VALUE, (uint32_t)crf);
1514
0
    uint32_t extended_crf_qindex_offset = extended_q_index - qp * 4;
1515
1516
0
    config_struct->qp                         = qp;
1517
0
    config_struct->rate_control_mode          = SVT_AV1_RC_MODE_CQP_OR_CRF;
1518
0
    config_struct->aq_mode                    = 2;
1519
0
    config_struct->extended_crf_qindex_offset = extended_crf_qindex_offset;
1520
1521
0
    return EB_ErrorNone;
1522
0
}
1523
1524
0
static EbErrorType str_to_cqp(const char* nptr, EbSvtAv1EncConfiguration* config_struct) {
1525
0
    double      cqp;
1526
0
    EbErrorType return_error;
1527
1528
0
    return_error = str_to_double(nptr, &cqp, NULL);
1529
1530
0
    if (return_error == EB_ErrorBadParameter) {
1531
0
        return return_error;
1532
0
    }
1533
0
    if (cqp < 0) {
1534
0
        return EB_ErrorBadParameter;
1535
0
    }
1536
1537
0
    uint32_t extended_q_index           = (uint32_t)(cqp * 4);
1538
0
    uint32_t qp                         = AOMMIN(MAX_QP_VALUE, (uint32_t)cqp);
1539
0
    uint32_t extended_crf_qindex_offset = extended_q_index - qp * 4;
1540
1541
0
    config_struct->qp                         = qp;
1542
0
    config_struct->rate_control_mode          = SVT_AV1_RC_MODE_CQP_OR_CRF;
1543
0
    config_struct->aq_mode                    = 0;
1544
0
    config_struct->extended_crf_qindex_offset = extended_crf_qindex_offset;
1545
1546
0
    return EB_ErrorNone;
1547
0
}
1548
1549
0
static EbErrorType str_to_keyint(const char* nptr, int32_t* out, bool* multi) {
1550
0
    char*      suff;
1551
0
    const long keyint = strtol(nptr, &suff, 0);
1552
1553
0
    if (keyint > INT32_MAX || keyint < -2) {
1554
0
        return EB_ErrorBadParameter;
1555
0
    }
1556
1557
0
    switch (*suff) {
1558
0
    case 's':
1559
        // signal we need to multiply keyint * frame_rate
1560
0
        *multi = true;
1561
0
        *out   = keyint;
1562
0
        break;
1563
0
    case '\0':
1564
0
        *multi = false;
1565
0
        *out   = keyint < 0 ? keyint : keyint - 1;
1566
0
        break;
1567
0
    default:
1568
        // else leave as untouched, we have an invalid keyint
1569
0
        SVT_ERROR("Invalid keyint value: %s\n", nptr);
1570
0
        return EB_ErrorBadParameter;
1571
0
    }
1572
1573
0
    return EB_ErrorNone;
1574
0
}
1575
1576
0
static EbErrorType str_to_bitrate(const char* nptr, uint32_t* out) {
1577
0
    char*        suff;
1578
0
    const double bitrate = strtod(nptr, &suff);
1579
1580
0
    if (bitrate < 0 || bitrate > UINT32_MAX) {
1581
0
        SVT_ERROR("Invalid bitrate value: %s\n", nptr);
1582
0
        return EB_ErrorBadParameter;
1583
0
    }
1584
1585
0
    switch (*suff) {
1586
0
    case 'b':
1587
0
    case 'B':
1588
0
        *out = (uint32_t)bitrate;
1589
0
        break;
1590
0
    case '\0':
1591
0
    case 'k':
1592
0
    case 'K':
1593
0
        *out = (uint32_t)(1000 * bitrate);
1594
0
        break;
1595
0
    case 'm':
1596
0
    case 'M':
1597
0
        *out = (uint32_t)(1000000 * bitrate);
1598
0
        break;
1599
0
    default:
1600
0
        return EB_ErrorBadParameter;
1601
0
    }
1602
0
    if (*out > 100000000) {
1603
0
        *out = 100000000;
1604
0
        SVT_WARN("Bitrate value: %s has been set to 100000000\n", nptr);
1605
0
    }
1606
0
    return EB_ErrorNone;
1607
0
}
1608
1609
0
static EbErrorType str_to_profile(const char* nptr, EbAv1SeqProfile* out) {
1610
0
    const struct {
1611
0
        const char*     name;
1612
0
        EbAv1SeqProfile profile;
1613
0
    } profiles[] = {
1614
0
        {"main", MAIN_PROFILE},
1615
0
        {"high", HIGH_PROFILE},
1616
0
        {"professional", PROFESSIONAL_PROFILE},
1617
0
    };
1618
1619
0
    const size_t profiles_size = sizeof(profiles) / sizeof(profiles[0]);
1620
1621
0
    for (size_t i = 0; i < profiles_size; i++) {
1622
0
        if (!strcmp(nptr, profiles[i].name)) {
1623
0
            *out = profiles[i].profile;
1624
0
            return EB_ErrorNone;
1625
0
        }
1626
0
    }
1627
1628
0
    return EB_ErrorBadParameter;
1629
0
}
1630
1631
0
static EbErrorType str_to_color_fmt(const char* nptr, EbColorFormat* out) {
1632
0
    const struct {
1633
0
        const char*   name;
1634
0
        EbColorFormat fmt;
1635
0
    } color_formats[] = {
1636
0
        {"mono", EB_YUV400},
1637
0
        {"400", EB_YUV400},
1638
0
        {"420", EB_YUV420},
1639
0
        {"422", EB_YUV422},
1640
0
        {"444", EB_YUV444},
1641
0
    };
1642
1643
0
    const size_t color_format_size = sizeof(color_formats) / sizeof(color_formats[0]);
1644
1645
0
    for (size_t i = 0; i < color_format_size; i++) {
1646
0
        if (!strcmp(nptr, color_formats[i].name)) {
1647
0
            *out = color_formats[i].fmt;
1648
0
            return EB_ErrorNone;
1649
0
        }
1650
0
    }
1651
1652
0
    return EB_ErrorBadParameter;
1653
0
}
1654
1655
0
static EbErrorType str_to_intra_rt(const char* nptr, SvtAv1IntraRefreshType* out) {
1656
0
    const struct {
1657
0
        const char*            name;
1658
0
        SvtAv1IntraRefreshType type;
1659
0
    } refresh_types[] = {
1660
0
        {"cra", SVT_AV1_FWDKF_REFRESH},
1661
0
        {"fwdkf", SVT_AV1_FWDKF_REFRESH},
1662
0
        {"idr", SVT_AV1_KF_REFRESH},
1663
0
        {"kf", SVT_AV1_KF_REFRESH},
1664
0
    };
1665
1666
0
    const size_t refresh_type_size = sizeof(refresh_types) / sizeof(refresh_types[0]);
1667
1668
0
    for (size_t i = 0; i < refresh_type_size; i++) {
1669
0
        if (!strcmp(nptr, refresh_types[i].name)) {
1670
0
            *out = refresh_types[i].type;
1671
0
            return EB_ErrorNone;
1672
0
        }
1673
0
    }
1674
1675
0
    return EB_ErrorBadParameter;
1676
0
}
1677
1678
0
static EbErrorType str_to_asm(const char* nptr, EbCpuFlags* out) {
1679
    // need to handle numbers in here since the numbers to no match the
1680
    // internal representation
1681
0
    const struct {
1682
0
        const char* name;
1683
0
        EbCpuFlags  flag;
1684
0
    } simds[] = {
1685
0
        {"c", 0},
1686
0
        {"0", 0},
1687
#ifdef ARCH_X86_64
1688
        {"mmx", (EB_CPU_FLAGS_MMX << 1) - 1},
1689
        {"1", (EB_CPU_FLAGS_MMX << 1) - 1},
1690
        {"sse", (EB_CPU_FLAGS_SSE << 1) - 1},
1691
        {"2", (EB_CPU_FLAGS_SSE << 1) - 1},
1692
        {"sse2", (EB_CPU_FLAGS_SSE2 << 1) - 1},
1693
        {"3", (EB_CPU_FLAGS_SSE2 << 1) - 1},
1694
        {"sse3", (EB_CPU_FLAGS_SSE3 << 1) - 1},
1695
        {"4", (EB_CPU_FLAGS_SSE3 << 1) - 1},
1696
        {"ssse3", (EB_CPU_FLAGS_SSSE3 << 1) - 1},
1697
        {"5", (EB_CPU_FLAGS_SSSE3 << 1) - 1},
1698
        {"sse4_1", (EB_CPU_FLAGS_SSE4_1 << 1) - 1},
1699
        {"6", (EB_CPU_FLAGS_SSE4_1 << 1) - 1},
1700
        {"sse4_2", (EB_CPU_FLAGS_SSE4_2 << 1) - 1},
1701
        {"7", (EB_CPU_FLAGS_SSE4_2 << 1) - 1},
1702
        {"avx", (EB_CPU_FLAGS_AVX << 1) - 1},
1703
        {"8", (EB_CPU_FLAGS_AVX << 1) - 1},
1704
        {"avx2", (EB_CPU_FLAGS_AVX2 << 1) - 1},
1705
        {"9", (EB_CPU_FLAGS_AVX2 << 1) - 1},
1706
        {"avx512", (EB_CPU_FLAGS_AVX512VL << 1) - 1},
1707
        {"10", (EB_CPU_FLAGS_AVX512VL << 1) - 1},
1708
        {"avx512icl", (EB_CPU_FLAGS_AVX512ICL << 1) - 1},
1709
        {"11", (EB_CPU_FLAGS_AVX512ICL << 1) - 1},
1710
#elif defined(ARCH_AARCH64)
1711
        {"neon", (EB_CPU_FLAGS_NEON << 1) - 1},
1712
        {"1", (EB_CPU_FLAGS_NEON << 1) - 1},
1713
        {"crc32", (EB_CPU_FLAGS_ARM_CRC32 << 1) - 1},
1714
        {"2", (EB_CPU_FLAGS_ARM_CRC32 << 1) - 1},
1715
        {"neon_dotprod", (EB_CPU_FLAGS_NEON_DOTPROD << 1) - 1},
1716
        {"3", (EB_CPU_FLAGS_NEON_DOTPROD << 1) - 1},
1717
        {"neon_i8mm", (EB_CPU_FLAGS_NEON_I8MM << 1) - 1},
1718
        {"4", (EB_CPU_FLAGS_NEON_I8MM << 1) - 1},
1719
        {"sve", (EB_CPU_FLAGS_SVE << 1) - 1},
1720
        {"5", (EB_CPU_FLAGS_SVE << 1) - 1},
1721
        {"sve2", (EB_CPU_FLAGS_SVE2 << 1) - 1},
1722
        {"6", (EB_CPU_FLAGS_SVE2 << 1) - 1},
1723
#endif
1724
0
        {"max", EB_CPU_FLAGS_ALL},
1725
0
        {"100", EB_CPU_FLAGS_ALL},
1726
0
    };
1727
0
    const size_t simds_size = sizeof(simds) / sizeof(simds[0]);
1728
1729
0
    for (size_t i = 0; i < simds_size; i++) {
1730
0
        if (!strcmp(nptr, simds[i].name)) {
1731
0
            *out = simds[i].flag;
1732
0
            return EB_ErrorNone;
1733
0
        }
1734
0
    }
1735
1736
0
    *out = EB_CPU_FLAGS_INVALID;
1737
1738
0
    return EB_ErrorBadParameter;
1739
0
}
1740
1741
0
static EbErrorType str_to_color_primaries(const char* nptr, EbColorPrimaries* out) {
1742
0
    const struct {
1743
0
        const char*      name;
1744
0
        EbColorPrimaries primaries;
1745
0
    } color_primaries[] = {
1746
0
        {"bt709", EB_CICP_CP_BT_709},
1747
0
        {"bt470m", EB_CICP_CP_BT_470_M},
1748
0
        {"bt470bg", EB_CICP_CP_BT_470_B_G},
1749
0
        {"bt601", EB_CICP_CP_BT_601},
1750
0
        {"smpte240", EB_CICP_CP_SMPTE_240},
1751
0
        {"film", EB_CICP_CP_GENERIC_FILM},
1752
0
        {"bt2020", EB_CICP_CP_BT_2020},
1753
0
        {"xyz", EB_CICP_CP_XYZ},
1754
0
        {"smpte431", EB_CICP_CP_SMPTE_431},
1755
0
        {"smpte432", EB_CICP_CP_SMPTE_432},
1756
0
        {"ebu3213", EB_CICP_CP_EBU_3213},
1757
0
    };
1758
1759
0
    const size_t color_primaries_size = sizeof(color_primaries) / sizeof(color_primaries[0]);
1760
1761
0
    for (size_t i = 0; i < color_primaries_size; i++) {
1762
0
        if (!strcmp(nptr, color_primaries[i].name)) {
1763
0
            *out = color_primaries[i].primaries;
1764
0
            return EB_ErrorNone;
1765
0
        }
1766
0
    }
1767
1768
0
    return EB_ErrorBadParameter;
1769
0
}
1770
1771
0
static EbErrorType str_to_transfer_characteristics(const char* nptr, EbTransferCharacteristics* out) {
1772
0
    const struct {
1773
0
        const char*               name;
1774
0
        EbTransferCharacteristics tfc;
1775
0
    } transfer_characteristics[] = {
1776
0
        {"bt709", EB_CICP_TC_BT_709},
1777
0
        {"bt470m", EB_CICP_TC_BT_470_M},
1778
0
        {"bt470bg", EB_CICP_TC_BT_470_B_G},
1779
0
        {"bt601", EB_CICP_TC_BT_601},
1780
0
        {"smpte240", EB_CICP_TC_SMPTE_240},
1781
0
        {"linear", EB_CICP_TC_LINEAR},
1782
0
        {"log100", EB_CICP_TC_LOG_100},
1783
0
        {"log100-sqrt10", EB_CICP_TC_LOG_100_SQRT10},
1784
0
        {"iec61966", EB_CICP_TC_IEC_61966},
1785
0
        {"bt1361", EB_CICP_TC_BT_1361},
1786
0
        {"srgb", EB_CICP_TC_SRGB},
1787
0
        {"bt2020-10", EB_CICP_TC_BT_2020_10_BIT},
1788
0
        {"bt2020-12", EB_CICP_TC_BT_2020_12_BIT},
1789
0
        {"smpte2084", EB_CICP_TC_SMPTE_2084},
1790
0
        {"smpte428", EB_CICP_TC_SMPTE_428},
1791
0
        {"hlg", EB_CICP_TC_HLG},
1792
0
    };
1793
1794
0
    const size_t transfer_characteristics_size = sizeof(transfer_characteristics) / sizeof(transfer_characteristics[0]);
1795
1796
0
    for (size_t i = 0; i < transfer_characteristics_size; i++) {
1797
0
        if (!strcmp(nptr, transfer_characteristics[i].name)) {
1798
0
            *out = transfer_characteristics[i].tfc;
1799
0
            return EB_ErrorNone;
1800
0
        }
1801
0
    }
1802
1803
0
    return EB_ErrorBadParameter;
1804
0
}
1805
1806
0
static EbErrorType str_to_matrix_coefficients(const char* nptr, EbMatrixCoefficients* out) {
1807
0
    const struct {
1808
0
        const char*          name;
1809
0
        EbMatrixCoefficients coeff;
1810
0
    } matrix_coefficients[] = {
1811
0
        {"identity", EB_CICP_MC_IDENTITY},
1812
0
        {"bt709", EB_CICP_MC_BT_709},
1813
0
        {"fcc", EB_CICP_MC_FCC},
1814
0
        {"bt470bg", EB_CICP_MC_BT_470_B_G},
1815
0
        {"bt601", EB_CICP_MC_BT_601},
1816
0
        {"smpte240", EB_CICP_MC_SMPTE_240},
1817
0
        {"ycgco", EB_CICP_MC_SMPTE_YCGCO},
1818
0
        {"bt2020-ncl", EB_CICP_MC_BT_2020_NCL},
1819
0
        {"bt2020-cl", EB_CICP_MC_BT_2020_CL},
1820
0
        {"smpte2085", EB_CICP_MC_SMPTE_2085},
1821
0
        {"chroma-ncl", EB_CICP_MC_CHROMAT_NCL},
1822
0
        {"chroma-cl", EB_CICP_MC_CHROMAT_CL},
1823
0
        {"ictcp", EB_CICP_MC_ICTCP},
1824
0
    };
1825
1826
0
    const size_t matrix_coefficients_size = sizeof(matrix_coefficients) / sizeof(matrix_coefficients[0]);
1827
1828
0
    for (size_t i = 0; i < matrix_coefficients_size; i++) {
1829
0
        if (!strcmp(nptr, matrix_coefficients[i].name)) {
1830
0
            *out = matrix_coefficients[i].coeff;
1831
0
            return EB_ErrorNone;
1832
0
        }
1833
0
    }
1834
1835
0
    return EB_ErrorBadParameter;
1836
0
}
1837
1838
0
static EbErrorType str_to_color_range(const char* nptr, EbColorRange* out) {
1839
0
    const struct {
1840
0
        const char*  name;
1841
0
        EbColorRange range;
1842
0
    } color_range[] = {
1843
0
        {"studio", EB_CR_STUDIO_RANGE},
1844
0
        {"full", EB_CR_FULL_RANGE},
1845
0
    };
1846
1847
0
    const size_t color_range_size = sizeof(color_range) / sizeof(color_range[0]);
1848
1849
0
    for (size_t i = 0; i < color_range_size; i++) {
1850
0
        if (!strcmp(nptr, color_range[i].name)) {
1851
0
            *out = color_range[i].range;
1852
0
            return EB_ErrorNone;
1853
0
        }
1854
0
    }
1855
1856
0
    return EB_ErrorBadParameter;
1857
0
}
1858
1859
0
static EbErrorType str_to_chroma_sample_position(const char* nptr, EbChromaSamplePosition* out) {
1860
0
    const struct {
1861
0
        const char*            name;
1862
0
        EbChromaSamplePosition pos;
1863
0
    } chroma_sample_positions[] = {
1864
0
        {"unknown", EB_CSP_UNKNOWN},
1865
0
        {"vertical", EB_CSP_VERTICAL},
1866
0
        {"left", EB_CSP_VERTICAL},
1867
0
        {"colocated", EB_CSP_COLOCATED},
1868
0
        {"topleft", EB_CSP_COLOCATED},
1869
0
    };
1870
1871
0
    const size_t chroma_sample_positions_size = sizeof(chroma_sample_positions) / sizeof(chroma_sample_positions[0]);
1872
1873
0
    for (size_t i = 0; i < chroma_sample_positions_size; i++) {
1874
0
        if (!strcmp(nptr, chroma_sample_positions[i].name)) {
1875
0
            *out = chroma_sample_positions[i].pos;
1876
0
            return EB_ErrorNone;
1877
0
        }
1878
0
    }
1879
1880
0
    return EB_ErrorBadParameter;
1881
0
}
1882
1883
0
static EbErrorType str_to_sframe_mode(const char* nptr, EbSFrameMode* out) {
1884
0
    const struct {
1885
0
        const char*  name;
1886
0
        EbSFrameMode mode;
1887
0
    } sframe_mode[] = {
1888
0
        {"strict", SFRAME_STRICT_BASE},
1889
0
        {"nearest", SFRAME_NEAREST_BASE},
1890
0
        {"flexible", SFRAME_FLEXIBLE_BASE},
1891
0
        {"decposi", SFRAME_DEC_POSI_BASE},
1892
0
    };
1893
1894
0
    const size_t sframe_mode_size = sizeof(sframe_mode) / sizeof(sframe_mode[0]);
1895
1896
0
    for (size_t i = 0; i < sframe_mode_size; i++) {
1897
0
        if (!strcmp(nptr, sframe_mode[i].name)) {
1898
0
            *out = sframe_mode[i].mode;
1899
0
            return EB_ErrorNone;
1900
0
        }
1901
0
    }
1902
1903
0
    return EB_ErrorBadParameter;
1904
0
}
1905
1906
0
static EbErrorType str_to_rc_mode(const char* nptr, uint8_t* out, uint8_t* aq_mode) {
1907
    // separate rc mode enum to distinguish between cqp and crf modes
1908
0
    enum rc_modes {
1909
0
        RC_MODE_ZERO = 0, // unique mode in case user passes a literal 0
1910
0
        RC_MODE_CQP,
1911
0
        RC_MODE_CRF,
1912
0
        RC_MODE_VBR,
1913
0
        RC_MODE_CBR,
1914
0
        RC_MODE_INVALID,
1915
0
    };
1916
1917
0
    const struct {
1918
0
        const char* name;
1919
0
        uint32_t    mode;
1920
0
    } rc_mode[] = {
1921
0
        {"0", RC_MODE_ZERO},
1922
0
        {"1", RC_MODE_VBR},
1923
0
        {"2", RC_MODE_CBR},
1924
0
        {"cqp", RC_MODE_CQP},
1925
0
        {"crf", RC_MODE_CRF},
1926
0
        {"vbr", RC_MODE_VBR},
1927
0
        {"cbr", RC_MODE_CBR},
1928
0
    };
1929
1930
0
    const size_t rc_mode_size = sizeof(rc_mode) / sizeof(rc_mode[0]);
1931
1932
0
    enum rc_modes mode = RC_MODE_INVALID;
1933
1934
0
    for (size_t i = 0; i < rc_mode_size; i++) {
1935
0
        if (!strcmp(nptr, rc_mode[i].name)) {
1936
0
            mode = rc_mode[i].mode;
1937
0
            break;
1938
0
        }
1939
0
    }
1940
1941
0
    switch (mode) {
1942
0
    case RC_MODE_ZERO:
1943
0
        *out = 0;
1944
0
        break;
1945
0
    case RC_MODE_CQP:
1946
0
        *out     = SVT_AV1_RC_MODE_CQP_OR_CRF;
1947
0
        *aq_mode = 0;
1948
0
        break;
1949
0
    case RC_MODE_CRF:
1950
0
        *out     = SVT_AV1_RC_MODE_CQP_OR_CRF;
1951
0
        *aq_mode = 2;
1952
0
        break;
1953
0
    case RC_MODE_VBR:
1954
0
        *out = SVT_AV1_RC_MODE_VBR;
1955
0
        break;
1956
0
    case RC_MODE_CBR:
1957
0
        *out = SVT_AV1_RC_MODE_CBR;
1958
0
        break;
1959
0
    default:
1960
0
        SVT_ERROR("Invalid rc mode: %s\n", nptr);
1961
0
        return EB_ErrorBadParameter;
1962
0
    }
1963
0
    return EB_ErrorNone;
1964
0
}
1965
1966
0
static EbErrorType str_to_pred_struct(const char* nptr, PredStructure* pred_structure) {
1967
0
    const struct {
1968
0
        const char*   name;
1969
0
        PredStructure mode;
1970
0
    } pred_structs[] = {{"0", ALL_INTRA},
1971
0
                        {"all-intra", ALL_INTRA},
1972
0
                        {"ai", ALL_INTRA},
1973
0
                        {"1", LOW_DELAY},
1974
0
                        {"low-delay", LOW_DELAY},
1975
0
                        {"ld", LOW_DELAY},
1976
0
                        {"2", RANDOM_ACCESS},
1977
0
                        {"random-access", RANDOM_ACCESS},
1978
0
                        {"ra", RANDOM_ACCESS}};
1979
1980
0
    const size_t pred_structs_size = sizeof(pred_structs) / sizeof(pred_structs[0]);
1981
1982
0
    for (size_t i = 0; i < pred_structs_size; i++) {
1983
0
        if (!strcmp(nptr, pred_structs[i].name)) {
1984
0
            *pred_structure = pred_structs[i].mode;
1985
0
            return EB_ErrorNone;
1986
0
        }
1987
0
    }
1988
1989
0
    SVT_ERROR("Invalid pred struct: %s\n", nptr);
1990
0
    return EB_ErrorBadParameter;
1991
0
}
1992
1993
0
static EbErrorType str_to_frm_resz_evts(const char* nptr, SvtAv1FrameScaleEvts* evts) {
1994
0
    const uint32_t param_count = count_params(nptr);
1995
0
    if ((evts->evt_num != 0 && evts->evt_num != param_count) || param_count == 0) {
1996
0
        SVT_ERROR("Error: Size for the list passed to %s doesn't match %u\n", "frame-resz-events", evts->evt_num);
1997
0
        return EB_ErrorBadParameter;
1998
0
    }
1999
0
    if (evts->start_frame_nums) {
2000
0
        EB_FREE(evts->start_frame_nums);
2001
0
    }
2002
0
    EB_MALLOC(evts->start_frame_nums, param_count * sizeof(uint64_t));
2003
0
    evts->evt_num = param_count;
2004
0
    return parse_list_uint64(nptr, evts->start_frame_nums, param_count);
2005
0
}
2006
2007
0
static EbErrorType str_to_resz_kf_denoms(const char* nptr, SvtAv1FrameScaleEvts* evts) {
2008
0
    const uint32_t param_count = count_params(nptr);
2009
0
    if ((evts->evt_num != 0 && evts->evt_num != param_count) || param_count == 0) {
2010
0
        SVT_ERROR("Error: Size for the list passed to %s doesn't match %u\n", "frame-resz-kf-denoms", evts->evt_num);
2011
0
        return EB_ErrorBadParameter;
2012
0
    }
2013
0
    if (evts->resize_kf_denoms) {
2014
0
        EB_FREE(evts->resize_kf_denoms);
2015
0
    }
2016
0
    EB_MALLOC(evts->resize_kf_denoms, param_count * sizeof(uint32_t));
2017
0
    evts->evt_num = param_count;
2018
0
    return parse_list_uint32(nptr, evts->resize_kf_denoms, param_count);
2019
0
}
2020
2021
0
static EbErrorType str_to_resz_denoms(const char* nptr, SvtAv1FrameScaleEvts* evts) {
2022
0
    const uint32_t param_count = count_params(nptr);
2023
0
    if ((evts->evt_num != 0 && evts->evt_num != param_count) || param_count == 0) {
2024
0
        SVT_ERROR("Error: Size for the list passed to %s doesn't match %u\n", "frame-resz-denoms", evts->evt_num);
2025
0
        return EB_ErrorBadParameter;
2026
0
    }
2027
0
    if (evts->resize_denoms) {
2028
0
        EB_FREE(evts->resize_denoms);
2029
0
    }
2030
0
    EB_MALLOC(evts->resize_denoms, param_count * sizeof(uint32_t));
2031
0
    evts->evt_num = param_count;
2032
0
    return parse_list_uint32(nptr, evts->resize_denoms, param_count);
2033
0
}
2034
2035
0
static EbErrorType str_to_sframe_posi(const char* nptr, SvtAv1SFramePositions* posis) {
2036
0
    const uint32_t param_count = count_params(nptr);
2037
0
    if ((posis->sframe_num != 0 && posis->sframe_num != param_count) || param_count == 0) {
2038
0
        SVT_ERROR("Error: Size for the list passed to %s doesn't match %u\n", "sframe-posi", posis->sframe_num);
2039
0
        return EB_ErrorBadParameter;
2040
0
    }
2041
0
    if (posis->sframe_posis) {
2042
0
        EB_FREE(posis->sframe_posis);
2043
0
    }
2044
0
    EB_MALLOC(posis->sframe_posis, param_count * sizeof(uint64_t));
2045
0
    posis->sframe_num = param_count;
2046
0
    return parse_list_uint64(nptr, posis->sframe_posis, param_count);
2047
0
}
2048
2049
0
static EbErrorType str_to_sframe_qp(const char* nptr, SvtAv1SFramePositions* posis, uint8_t* qp) {
2050
0
    const uint32_t param_count = count_params(nptr);
2051
0
    if ((posis->sframe_num != 0 && posis->sframe_num != param_count && param_count != 1) || param_count == 0) {
2052
0
        SVT_ERROR("Error: Size for the list passed to %s doesn't match %u\n", "sframe-qp", posis->sframe_num);
2053
0
        return EB_ErrorBadParameter;
2054
0
    }
2055
0
    if (posis->sframe_qps) {
2056
0
        EB_FREE(posis->sframe_qps);
2057
0
    }
2058
0
    EB_MALLOC(posis->sframe_qps, param_count * sizeof(uint8_t));
2059
0
    posis->sframe_qp_num = param_count;
2060
0
    EbErrorType err      = parse_list_uint8(nptr, posis->sframe_qps, param_count);
2061
    // check if the QP values are valid
2062
0
    for (uint32_t i = 0; i < posis->sframe_qp_num; ++i) {
2063
0
        if (posis->sframe_qps[i] < 1 || posis->sframe_qps[i] > 63) {
2064
0
            SVT_ERROR("Error: Invalid S-Frame QP value. QPs must be [1 - 63]\n");
2065
0
            EB_FREE(posis->sframe_qps);
2066
0
            posis->sframe_qp_num = 0;
2067
0
            err                  = EB_ErrorBadParameter;
2068
0
        }
2069
0
    }
2070
    // If sframe-qp parameter contains only one value, apply it to all S-frames
2071
0
    if (err == EB_ErrorNone && param_count == 1) {
2072
0
        *qp = posis->sframe_qps[0];
2073
0
    }
2074
0
    return err;
2075
0
}
2076
2077
0
static EbErrorType str_to_sframe_qp_offset(const char* nptr, SvtAv1SFramePositions* posis, int8_t* qp_offset) {
2078
0
    const uint32_t param_count = count_params(nptr);
2079
0
    if ((posis->sframe_num != 0 && posis->sframe_num != param_count && param_count != 1) || param_count == 0) {
2080
0
        SVT_ERROR("Error: Size for the list passed to %s doesn't match %u\n", "sframe-qp-offset", posis->sframe_num);
2081
0
        return EB_ErrorBadParameter;
2082
0
    }
2083
0
    if (posis->sframe_qp_offsets) {
2084
0
        EB_FREE(posis->sframe_qp_offsets);
2085
0
    }
2086
0
    EB_MALLOC(posis->sframe_qp_offsets, param_count * sizeof(int8_t));
2087
0
    posis->sframe_qp_num = param_count;
2088
0
    EbErrorType err      = parse_list_int8(nptr, posis->sframe_qp_offsets, param_count);
2089
    // check if the QP offset values are valid
2090
0
    for (uint32_t i = 0; i < posis->sframe_qp_num; ++i) {
2091
0
        if (posis->sframe_qp_offsets[i] < -63 || posis->sframe_qp_offsets[i] > 63) {
2092
0
            SVT_ERROR("Error: Invalid S-Frame QP offset value. QP offsets must be [-63 - 63]\n");
2093
0
            EB_FREE(posis->sframe_qp_offsets);
2094
0
            posis->sframe_qp_num = 0;
2095
0
            err                  = EB_ErrorBadParameter;
2096
0
        }
2097
0
    }
2098
    // If sframe-qp-offset parameter contains only one value, apply it to all S-frames
2099
0
    if (err == EB_ErrorNone && param_count == 1) {
2100
0
        *qp_offset = posis->sframe_qp_offsets[0];
2101
0
    }
2102
0
    return err;
2103
0
}
2104
2105
#define COLOR_OPT(par, opt)                                          \
2106
0
    do {                                                             \
2107
0
        if (!strcmp(name, par)) {                                    \
2108
0
            return_error = str_to_##opt(value, &config_struct->opt); \
2109
0
            if (return_error == EB_ErrorNone)                        \
2110
0
                return return_error;                                 \
2111
0
            uint32_t val;                                            \
2112
0
            return_error = str_to_uint(value, &val, NULL);           \
2113
0
            if (return_error == EB_ErrorNone)                        \
2114
0
                config_struct->opt = val;                            \
2115
0
            return return_error;                                     \
2116
0
        }                                                            \
2117
0
    } while (0)
2118
2119
#define COLOR_METADATA_OPT(par, opt)                                                                      \
2120
0
    do {                                                                                                  \
2121
0
        if (!strcmp(name, par))                                                                           \
2122
0
            return svt_aom_parse_##opt(&config_struct->opt, value) ? EB_ErrorNone : EB_ErrorBadParameter; \
2123
0
    } while (0)
2124
2125
EB_API EbErrorType svt_av1_enc_parse_parameter(EbSvtAv1EncConfiguration* config_struct, const char* name,
2126
0
                                               const char* value) {
2127
0
    if (config_struct == NULL || name == NULL || value == NULL) {
2128
0
        return EB_ErrorBadParameter;
2129
0
    }
2130
2131
0
    EbErrorType return_error = EB_ErrorBadParameter;
2132
2133
0
    if (!strcmp(name, "keyint")) {
2134
0
        return str_to_keyint(value, &config_struct->intra_period_length, &config_struct->multiply_keyint);
2135
0
    }
2136
2137
0
    if (!strcmp(name, "tbr")) {
2138
0
        return str_to_bitrate(value, &config_struct->target_bit_rate);
2139
0
    }
2140
2141
0
    if (!strcmp(name, "mbr")) {
2142
0
        return str_to_bitrate(value, &config_struct->max_bit_rate);
2143
0
    }
2144
2145
    // options updating more than one field
2146
0
    if (!strcmp(name, "crf")) {
2147
0
        return str_to_crf(value, config_struct);
2148
0
    }
2149
2150
0
    if (!strcmp(name, "cqp")) {
2151
0
        return str_to_cqp(value, config_struct);
2152
0
    }
2153
2154
0
    if (!strcmp(name, "rc")) {
2155
0
        return str_to_rc_mode(value, &config_struct->rate_control_mode, &config_struct->aq_mode);
2156
0
    }
2157
2158
    // custom enum fields
2159
0
    if (!strcmp(name, "profile")) {
2160
0
        return str_to_profile(value, &config_struct->profile) == EB_ErrorBadParameter
2161
0
            ? str_to_uint(value, (uint32_t*)&config_struct->profile, NULL)
2162
0
            : EB_ErrorNone;
2163
0
    }
2164
2165
0
    if (!strcmp(name, "color-format")) {
2166
0
        return str_to_color_fmt(value, &config_struct->encoder_color_format) == EB_ErrorBadParameter
2167
0
            ? str_to_uint(value, (uint32_t*)&config_struct->encoder_color_format, NULL)
2168
0
            : EB_ErrorNone;
2169
0
    }
2170
2171
0
    if (!strcmp(name, "irefresh-type")) {
2172
0
        return str_to_intra_rt(value, &config_struct->intra_refresh_type) == EB_ErrorBadParameter
2173
0
            ? str_to_uint(value, (uint32_t*)&config_struct->intra_refresh_type, NULL)
2174
0
            : EB_ErrorNone;
2175
0
    }
2176
2177
0
    if (!strcmp(name, "sframe-mode")) {
2178
0
        return str_to_sframe_mode(value, &config_struct->sframe_mode) == EB_ErrorBadParameter
2179
0
            ? str_to_uint(value, (uint32_t*)&config_struct->sframe_mode, NULL)
2180
0
            : EB_ErrorNone;
2181
0
    }
2182
2183
0
    if (!strcmp(name, "asm")) {
2184
0
        return str_to_asm(value, &config_struct->use_cpu_flags);
2185
0
    }
2186
2187
0
    if (!strcmp(name, "pred-struct")) {
2188
0
        return str_to_pred_struct(value, &config_struct->pred_structure);
2189
0
    }
2190
2191
0
    COLOR_OPT("color-primaries", color_primaries);
2192
0
    COLOR_OPT("transfer-characteristics", transfer_characteristics);
2193
0
    COLOR_OPT("matrix-coefficients", matrix_coefficients);
2194
0
    COLOR_OPT("color-range", color_range);
2195
0
    COLOR_OPT("chroma-sample-position", chroma_sample_position);
2196
2197
    // custom struct fields
2198
0
    COLOR_METADATA_OPT("mastering-display", mastering_display);
2199
0
    COLOR_METADATA_OPT("content-light", content_light_level);
2200
2201
    // arrays
2202
0
    if (!strcmp(name, "qindex-offsets")) {
2203
0
        return parse_list_int32(value, config_struct->qindex_offsets, EB_MAX_TEMPORAL_LAYERS);
2204
0
    }
2205
2206
0
    if (!strcmp(name, "chroma-qindex-offsets")) {
2207
0
        return parse_list_int32(value, config_struct->chroma_qindex_offsets, EB_MAX_TEMPORAL_LAYERS);
2208
0
    }
2209
2210
0
    if (!strcmp(name, "lambda-scale-factors")) {
2211
0
        return parse_list_int32(value, config_struct->lambda_scale_factors, SVT_AV1_FRAME_UPDATE_TYPES);
2212
0
    }
2213
2214
0
    if (!strcmp(name, "frame-resz-events")) {
2215
0
        return str_to_frm_resz_evts(value, &config_struct->frame_scale_evts);
2216
0
    }
2217
2218
0
    if (!strcmp(name, "frame-resz-kf-denoms")) {
2219
0
        return str_to_resz_kf_denoms(value, &config_struct->frame_scale_evts);
2220
0
    }
2221
2222
0
    if (!strcmp(name, "frame-resz-denoms")) {
2223
0
        return str_to_resz_denoms(value, &config_struct->frame_scale_evts);
2224
0
    }
2225
2226
0
    if (!strcmp(name, "sframe-posi")) {
2227
0
        return str_to_sframe_posi(value, &config_struct->sframe_posi);
2228
0
    }
2229
2230
0
    if (!strcmp(name, "sframe-qp")) {
2231
0
        return str_to_sframe_qp(value, &config_struct->sframe_posi, &config_struct->sframe_qp);
2232
0
    }
2233
2234
0
    if (!strcmp(name, "sframe-qp-offset")) {
2235
0
        return str_to_sframe_qp_offset(value, &config_struct->sframe_posi, &config_struct->sframe_qp_offset);
2236
0
    }
2237
2238
    // uint32_t fields
2239
0
    const struct {
2240
0
        const char* name;
2241
0
        uint32_t*   out;
2242
0
    } uint_opts[] = {
2243
0
        {"w", &config_struct->source_width},
2244
0
        {"width", &config_struct->source_width},
2245
0
        {"h", &config_struct->source_height},
2246
0
        {"height", &config_struct->source_height},
2247
0
        {"q", &config_struct->qp},
2248
0
        {"qp", &config_struct->qp},
2249
0
        {"film-grain", &config_struct->film_grain_denoise_strength},
2250
0
        {"hierarchical-levels", &config_struct->hierarchical_levels},
2251
0
        {"tier", &config_struct->tier},
2252
0
        {"level", &config_struct->level},
2253
0
        {"lp", &config_struct->level_of_parallelism},
2254
0
        {"fps-num", &config_struct->frame_rate_numerator},
2255
0
        {"fps-denom", &config_struct->frame_rate_denominator},
2256
0
        {"lookahead", &config_struct->look_ahead_distance},
2257
0
        {"scd", &config_struct->scene_change_detection},
2258
0
        {"max-qp", &config_struct->max_qp_allowed},
2259
0
        {"min-qp", &config_struct->min_qp_allowed},
2260
0
        {"minsection-pct", &config_struct->vbr_min_section_pct},
2261
0
        {"maxsection-pct", &config_struct->vbr_max_section_pct},
2262
0
        {"undershoot-pct", &config_struct->under_shoot_pct},
2263
0
        {"overshoot-pct", &config_struct->over_shoot_pct},
2264
0
        {"mbr-overshoot-pct", &config_struct->mbr_over_shoot_pct},
2265
0
        {"max-intra-bitrate-pct", &config_struct->max_intra_bitrate_pct},
2266
0
        {"max-inter-bitrate-pct", &config_struct->max_inter_bitrate_pct},
2267
0
        {"recode-loop", &config_struct->recode_loop},
2268
0
        {"enable-stat-report", &config_struct->stat_report},
2269
0
        {"scm", &config_struct->screen_content_mode},
2270
0
        {"input-depth", &config_struct->encoder_bit_depth},
2271
0
        {"forced-max-frame-width", &config_struct->forced_max_frame_width},
2272
0
        {"forced-max-frame-height", &config_struct->forced_max_frame_height},
2273
0
    };
2274
2275
0
    const size_t uint_opts_size = sizeof(uint_opts) / sizeof(uint_opts[0]);
2276
2277
0
    for (size_t i = 0; i < uint_opts_size; i++) {
2278
0
        if (!strcmp(name, uint_opts[i].name)) {
2279
0
            return str_to_uint(value, uint_opts[i].out, NULL);
2280
0
        }
2281
0
    }
2282
2283
    // uint8_t fields
2284
0
    const struct {
2285
0
        const char* name;
2286
0
        uint8_t*    out;
2287
0
    } uint8_opts[] = {
2288
0
        {"aq-mode", &config_struct->aq_mode},
2289
0
        {"superres-mode", &config_struct->superres_mode},
2290
0
        {"superres-qthres", &config_struct->superres_qthres},
2291
0
        {"superres-kf-qthres", &config_struct->superres_kf_qthres},
2292
0
        {"superres-denom", &config_struct->superres_denom},
2293
0
        {"superres-kf-denom", &config_struct->superres_kf_denom},
2294
0
        {"tune", &config_struct->tune},
2295
0
        {"film-grain-denoise", &config_struct->film_grain_denoise_apply},
2296
0
        {"enable-dlf", &config_struct->enable_dlf_flag},
2297
0
        {"resize-mode", &config_struct->resize_mode},
2298
0
        {"resize-denom", &config_struct->resize_denom},
2299
0
        {"resize-kf-denom", &config_struct->resize_kf_denom},
2300
0
        {"qm-min", &config_struct->min_qm_level},
2301
0
        {"qm-max", &config_struct->max_qm_level},
2302
0
        {"chroma-qm-min", &config_struct->min_chroma_qm_level},
2303
0
        {"chroma-qm-max", &config_struct->max_chroma_qm_level},
2304
0
        {"use-fixed-qindex-offsets", &config_struct->use_fixed_qindex_offsets},
2305
0
        {"startup-mg-size", &config_struct->startup_mg_size},
2306
0
        {"variance-boost-strength", &config_struct->variance_boost_strength},
2307
0
        {"variance-octile", &config_struct->variance_octile},
2308
0
        {"variance-boost-curve", &config_struct->variance_boost_curve},
2309
0
        {"qp-scale-compress-strength", &config_struct->qp_scale_compress_strength},
2310
0
        {"fast-decode", &config_struct->fast_decode},
2311
0
        {"luminance-qp-bias", &config_struct->luminance_qp_bias},
2312
0
        {"enable-tf", &config_struct->enable_tf},
2313
0
        {"tf-strength", &config_struct->tf_strength},
2314
0
        {"max-tx-size", &config_struct->max_tx_size},
2315
0
    };
2316
2317
0
    const size_t uint8_opts_size = sizeof(uint8_opts) / sizeof(uint8_opts[0]);
2318
2319
0
    for (size_t i = 0; i < uint8_opts_size; i++) {
2320
0
        if (!strcmp(name, uint8_opts[i].name)) {
2321
0
            uint32_t val;
2322
0
            return_error = str_to_uint(value, &val, NULL);
2323
0
            if (return_error == EB_ErrorNone) {
2324
                // add protection if the input param is roll-over
2325
0
                if (val > 255) {
2326
0
                    return EB_ErrorBadParameter;
2327
0
                }
2328
0
                *uint8_opts[i].out = val;
2329
0
            }
2330
0
            return return_error;
2331
0
        }
2332
0
    }
2333
2334
    // int64_t fields
2335
0
    const struct {
2336
0
        const char* name;
2337
0
        int64_t*    out;
2338
0
    } int64_opts[] = {
2339
0
        {"buf-initial-sz", &config_struct->starting_buffer_level_ms},
2340
0
        {"buf-optimal-sz", &config_struct->optimal_buffer_level_ms},
2341
0
        {"buf-sz", &config_struct->maximum_buffer_size_ms},
2342
0
    };
2343
2344
0
    const size_t int64_opts_size = sizeof(int64_opts) / sizeof(int64_opts[0]);
2345
2346
0
    for (size_t i = 0; i < int64_opts_size; i++) {
2347
0
        if (!strcmp(name, int64_opts[i].name)) {
2348
0
            return str_to_int64(value, int64_opts[i].out, NULL);
2349
0
        }
2350
0
    }
2351
2352
    // double fields
2353
0
    const struct {
2354
0
        const char* name;
2355
0
        double*     out;
2356
0
    } double_opts[] = {
2357
0
        {"ac-bias", &config_struct->ac_bias},
2358
0
    };
2359
2360
0
    const size_t double_opts_size = sizeof(double_opts) / sizeof(double_opts[0]);
2361
2362
0
    for (size_t i = 0; i < double_opts_size; i++) {
2363
0
        if (!strcmp(name, double_opts[i].name)) {
2364
0
            return str_to_double(value, double_opts[i].out, NULL);
2365
0
        }
2366
0
    }
2367
2368
    // int32_t fields
2369
0
    const struct {
2370
0
        const char* name;
2371
0
        int32_t*    out;
2372
0
    } int_opts[] = {
2373
0
        {"key-frame-chroma-qindex-offset", &config_struct->key_frame_chroma_qindex_offset},
2374
0
        {"key-frame-qindex-offset", &config_struct->key_frame_qindex_offset},
2375
0
        {"luma-y-dc-qindex-offset", &config_struct->luma_y_dc_qindex_offset},
2376
0
        {"chroma-u-dc-qindex-offset", &config_struct->chroma_u_dc_qindex_offset},
2377
0
        {"chroma-u-ac-qindex-offset", &config_struct->chroma_u_ac_qindex_offset},
2378
0
        {"chroma-v-dc-qindex-offset", &config_struct->chroma_v_dc_qindex_offset},
2379
0
        {"chroma-v-ac-qindex-offset", &config_struct->chroma_v_ac_qindex_offset},
2380
0
        {"pass", &config_struct->pass},
2381
0
        {"enable-cdef", &config_struct->cdef_level},
2382
0
        {"enable-restoration", &config_struct->enable_restoration_filtering},
2383
0
        {"enable-mfmv", &config_struct->enable_mfmv},
2384
0
        {"intra-period", &config_struct->intra_period_length},
2385
0
        {"tile-rows", &config_struct->tile_rows},
2386
0
        {"tile-columns", &config_struct->tile_columns},
2387
0
        {"sframe-dist", &config_struct->sframe_dist},
2388
0
        {"hbd-mds", &config_struct->hbd_mds},
2389
0
    };
2390
2391
0
    const size_t int_opts_size = sizeof(int_opts) / sizeof(int_opts[0]);
2392
2393
0
    for (size_t i = 0; i < int_opts_size; i++) {
2394
0
        if (!strcmp(name, int_opts[i].name)) {
2395
0
            return str_to_int(value, int_opts[i].out, NULL);
2396
0
        }
2397
0
    }
2398
2399
    // int8_t fields
2400
0
    const struct {
2401
0
        const char* name;
2402
0
        int8_t*     out;
2403
0
    } int8_opts[] = {
2404
0
        {"preset", &config_struct->enc_mode},
2405
0
        {"sharpness", &config_struct->sharpness},
2406
0
        {"startup-qp-offset", &config_struct->startup_qp_offset},
2407
0
    };
2408
2409
0
    const size_t int8_opts_size = sizeof(int8_opts) / sizeof(int8_opts[0]);
2410
2411
0
    for (size_t i = 0; i < int8_opts_size; i++) {
2412
0
        if (!strcmp(name, int8_opts[i].name)) {
2413
0
            int32_t val;
2414
0
            return_error = str_to_int(value, &val, NULL);
2415
0
            if (return_error == EB_ErrorNone) {
2416
                // add protection if the input param is roll-over
2417
0
                if (val > 127 || val < -128) {
2418
0
                    return EB_ErrorBadParameter;
2419
0
                }
2420
0
                *int8_opts[i].out = val;
2421
0
            }
2422
0
            return return_error;
2423
0
        }
2424
0
    }
2425
2426
    // bool fields
2427
0
    const struct {
2428
0
        const char* name;
2429
0
        bool*       out;
2430
0
    } bool_opts[] = {
2431
0
        {"use-q-file", &config_struct->use_qp_file},
2432
0
        {"enable-overlays", &config_struct->enable_overlays},
2433
0
        {"enable-force-key-frames", &config_struct->force_key_frames},
2434
0
#if CONFIG_ENABLE_QUANT_MATRIX
2435
0
        {"enable-qm", &config_struct->enable_qm},
2436
0
#endif
2437
0
        {"enable-dg", &config_struct->enable_dg},
2438
0
        {"gop-constraint-rc", &config_struct->gop_constraint_rc},
2439
0
        {"enable-variance-boost", &config_struct->enable_variance_boost},
2440
0
        {"lossless", &config_struct->lossless},
2441
0
        {"avif", &config_struct->avif},
2442
0
        {"rtc", &config_struct->rtc},
2443
0
        {"adaptive-film-grain", &config_struct->adaptive_film_grain},
2444
0
        {"enable-kf-tf", &config_struct->enable_tf_key},
2445
0
        {"enable-intrabc", &config_struct->enable_intrabc},
2446
0
    };
2447
0
    const size_t bool_opts_size = sizeof(bool_opts) / sizeof(bool_opts[0]);
2448
2449
0
    for (size_t i = 0; i < bool_opts_size; i++) {
2450
0
        if (!strcmp(name, bool_opts[i].name)) {
2451
0
            return str_to_bool(value, bool_opts[i].out);
2452
0
        }
2453
0
    }
2454
2455
0
    return return_error;
2456
0
}