Coverage Report

Created: 2026-05-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Globals/enc_handle.c
Line
Count
Source
1
/*
2
* Copyright(c) 2019 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 API component functions
13
14
/**************************************
15
 * Includes
16
 **************************************/
17
#include <stdlib.h>
18
#include <stdio.h>
19
#include <stdint.h>
20
21
#include "EbVersion.h"
22
#include "svt_threads.h"
23
#include "utility.h"
24
#include "enc_handle.h"
25
#include "enc_settings.h"
26
#include "pcs.h"
27
#include "pic_operators.h"
28
#include "reference_object.h"
29
#include "resource_coordination_process.h"
30
#include "pic_analysis_process.h"
31
#include "pd_process.h"
32
#include "me_process.h"
33
#include "initial_rc_process.h"
34
#include "src_ops_process.h"
35
#include "pic_manager_process.h"
36
#include "rc_process.h"
37
#include "md_config_process.h"
38
#include "enc_dec_process.h"
39
#include "ec_process.h"
40
#include "packetization_process.h"
41
#include "resource_coordination_results.h"
42
#include "pic_analysis_results.h"
43
#include "pd_results.h"
44
#include "me_results.h"
45
#include "initial_rc_results.h"
46
#include "pic_demux_results.h"
47
#include "rc_tasks.h"
48
#include "enc_dec_tasks.h"
49
#include "enc_dec_results.h"
50
#include "ec_results.h"
51
#include "pred_structure.h"
52
#include "rest_process.h"
53
#include "cdef_process.h"
54
#include "dlf_process.h"
55
#include "rc_results.h"
56
#include "definitions.h"
57
#include "metadata_handle.h"
58
59
#include "pack_unpack_c.h"
60
#include "enc_mode_config.h"
61
62
#ifdef ARCH_X86_64
63
#include <immintrin.h>
64
#endif
65
#include "svt_log.h"
66
67
#ifdef _WIN32
68
#include <windows.h>
69
#else
70
#include <errno.h>
71
#include <unistd.h>
72
#endif
73
74
#include "aom_dsp_rtcd.h"
75
#include "common_dsp_rtcd.h"
76
77
/**************************************
78
  * Defines
79
  **************************************/
80
// clang-format off
81
// Config Set Initial Count
82
533
#define EB_SequenceControlSetPoolInitCount              10
83
// Process Instantiation Initial Counts
84
#define EB_ResourceCoordinationProcessInitCount         1
85
#define EB_PictureDecisionProcessInitCount              1
86
#define EB_InitialRateControlProcessInitCount           1
87
533
#define EB_PictureManagerProcessInitCount               1
88
#define EB_RateControlProcessInitCount                  1
89
1.06k
#define EB_PacketizationProcessInitCount                1
90
91
// Output Buffer Transfer Parameters
92
533
#define TPL_INPUT_PORT_SOP                              0
93
533
#define TPL_INPUT_PORT_TPL                              1
94
3.26k
#define TPL_INPUT_PORT_INVALID                          -1
95
533
#define ENCDEC_INPUT_PORT_MDC                           0
96
533
#define ENCDEC_INPUT_PORT_ENCDEC                        1
97
3.26k
#define ENCDEC_INPUT_PORT_INVALID                       -1
98
99
1.06k
#define ENCODE_FIRST_PASS                               1
100
// clang-format on
101
102
uint8_t svt_aom_get_tpl_synthesizer_block_size(int8_t tpl_level, uint32_t picture_width, uint32_t picture_height);
103
104
/* count number of refs in a steady state MG*/
105
533
static uint16_t get_num_refs_in_one_mg(uint32_t hierarchical_levels, uint32_t referencing_scheme) {
106
533
    if (hierarchical_levels == 0) {
107
0
        return 1;
108
0
    }
109
110
    // All internal layer pics will be used as references.  Only top layer pics can be
111
    // not used as refs.
112
533
    uint16_t tot_refs = 1 << (hierarchical_levels - 1);
113
114
    // Top layer pics start at pic_idx 0 and every second pic is a top layer pic
115
1.59k
    for (uint16_t pic_idx = 0; pic_idx < (uint32_t)(1 << hierarchical_levels); pic_idx += 2) {
116
1.06k
        tot_refs += svt_aom_is_pic_used_as_ref(
117
1.06k
            hierarchical_levels, hierarchical_levels, pic_idx, referencing_scheme, 0);
118
1.06k
    }
119
120
533
    return tot_refs;
121
533
}
122
123
1.06k
static const char* get_asm_level_name_str(EbCpuFlags cpu_flags) {
124
    // clang-format off
125
1.06k
    static const struct {
126
1.06k
        const char *name;
127
1.06k
        EbCpuFlags  flags;
128
1.06k
    } param_maps[] = {
129
1.06k
        {"c",            0},
130
#ifdef ARCH_X86_64
131
        {"mmx",          EB_CPU_FLAGS_MMX},
132
        {"sse",          EB_CPU_FLAGS_SSE},
133
        {"sse2",         EB_CPU_FLAGS_SSE2},
134
        {"sse3",         EB_CPU_FLAGS_SSE3},
135
        {"ssse3",        EB_CPU_FLAGS_SSSE3},
136
        {"sse4_1",       EB_CPU_FLAGS_SSE4_1},
137
        {"sse4_2",       EB_CPU_FLAGS_SSE4_2},
138
        {"avx",          EB_CPU_FLAGS_AVX},
139
        {"avx2",         EB_CPU_FLAGS_AVX2},
140
        {"avx512",       EB_CPU_FLAGS_AVX512F},
141
        {"avx512icl",    EB_CPU_FLAGS_AVX512ICL},
142
#elif defined(ARCH_AARCH64)
143
        {"neon",         EB_CPU_FLAGS_NEON},
144
        {"crc32",        EB_CPU_FLAGS_ARM_CRC32},
145
        {"neon_dotprod", EB_CPU_FLAGS_NEON_DOTPROD},
146
        {"neon_i8mm",    EB_CPU_FLAGS_NEON_I8MM},
147
        {"sve",          EB_CPU_FLAGS_SVE},
148
        {"sve2",         EB_CPU_FLAGS_SVE2}
149
#endif
150
1.06k
    };
151
    // clang-format on
152
1.06k
    static const uint32_t para_map_size = sizeof(param_maps) / sizeof(param_maps[0]);
153
154
2.13k
    for (int32_t i = para_map_size - 1; i >= 0; --i) {
155
1.06k
        if (param_maps[i].flags & cpu_flags) {
156
0
            return param_maps[i].name;
157
0
        }
158
1.06k
    }
159
1.06k
    return "c";
160
1.06k
}
161
162
//Get Number of logical processors
163
533
static uint32_t get_num_processors() {
164
#ifdef _WIN32
165
    return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
166
#else
167
533
    return sysconf(_SC_NPROCESSORS_ONLN);
168
533
#endif
169
533
}
170
171
void    svt_aom_asm_set_convolve_asm_table(void);
172
void    svt_aom_asm_set_convolve_hbd_asm_table(void);
173
void    svt_aom_init_intra_dc_predictors_c_internal(void);
174
void    svt_aom_init_intra_predictors_internal(void);
175
void    svt_av1_init_me_luts(void);
176
uint8_t svt_aom_get_tpl_group_level(uint8_t tpl, int8_t enc_mode);
177
uint8_t svt_aom_set_tpl_group(PictureParentControlSet* pcs, uint8_t tpl_group_level, uint32_t source_width,
178
                              uint32_t source_height);
179
180
533
static void enc_switch_to_real_time() {
181
533
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
182
533
    if (!geteuid()) {
183
533
        (void)pthread_setschedparam(pthread_self(), SCHED_FIFO, &(struct sched_param){.sched_priority = 99});
184
533
    }
185
533
#endif
186
533
}
187
188
typedef enum ParallelLevel {
189
    PARALLEL_LEVEL_1     = 1,
190
    PARALLEL_LEVEL_2     = 2,
191
    PARALLEL_LEVEL_3     = 3,
192
    PARALLEL_LEVEL_4     = 4,
193
    PARALLEL_LEVEL_5     = 5,
194
    PARALLEL_LEVEL_6     = 6,
195
    PARALLEL_LEVEL_COUNT = 7
196
} ParallelLevel;
197
198
// When level of parallelism is not specified, the level will be determined
199
// based on the core count of the machine
200
0
#define PARALLEL_LEVEL_1_RANGE 1 // single core count
201
0
#define PARALLEL_LEVEL_2_RANGE 2
202
0
#define PARALLEL_LEVEL_3_RANGE 5
203
0
#define PARALLEL_LEVEL_4_RANGE 11
204
0
#define PARALLEL_LEVEL_5_RANGE 23
205
#define PARALLEL_LEVEL_6_RANGE 47
206
207
//return max wavefronts in a given picture
208
1.06k
static uint32_t get_max_wavefronts(uint32_t width, uint32_t height, uint32_t blk_size) {
209
1.06k
    assert(width > 0 && height > 0);
210
1.06k
    return (MIN(width, height) + (blk_size - 1)) / blk_size;
211
1.06k
}
212
213
/*
214
* When the picture dimension is a single SB, must use a single segment (EncDec segments
215
* assume a width of at least 2 SBs)
216
*
217
* Return true if the pic dimension is a single SB width
218
*/
219
1.59k
static bool is_pic_dimension_single_sb(uint32_t sb_size, uint16_t pic_dimension) {
220
1.59k
    return ((pic_dimension + sb_size - 1) / sb_size) == 1;
221
1.59k
}
222
223
/*********************************************************************************
224
* set_segments_numbers: Set the segment numbers for difference processes
225
***********************************************************************************/
226
533
void set_segments_numbers(SequenceControlSet* scs) {
227
533
    const uint32_t lp = scs->lp;
228
229
533
    scs->enc_dec_segment_row_count_array =
230
533
        (lp == PARALLEL_LEVEL_1 || is_pic_dimension_single_sb(scs->super_block_size, scs->max_input_luma_width)) ? 1
231
533
        : (scs->super_block_size == 128) ? MAX((int32_t)((scs->max_input_luma_height + 64) / 128), 1)
232
504
                                         : MAX((int32_t)((scs->max_input_luma_height + 32) / 64), 1);
233
533
    scs->enc_dec_segment_col_count_array =
234
533
        (lp == PARALLEL_LEVEL_1 || is_pic_dimension_single_sb(scs->super_block_size, scs->max_input_luma_height)) ? 1
235
533
        : (scs->super_block_size == 128) ? MAX((int32_t)((scs->max_input_luma_width + 64) / 128), 1)
236
506
                                         : MAX((int32_t)((scs->max_input_luma_width + 32) / 64), 1);
237
238
533
    scs->me_segment_row_count_array = scs->tf_segment_row_count = (lp == PARALLEL_LEVEL_1) ? 1
239
533
        : (((scs->max_input_luma_height + 32) / BLOCK_SIZE_64) < 6)                        ? 1
240
533
                                                                                           : 8;
241
533
    scs->me_segment_col_count_array = scs->tf_segment_column_count = (lp == PARALLEL_LEVEL_1) ? 1
242
533
        : (((scs->max_input_luma_width + 32) / BLOCK_SIZE_64) < 10)                           ? 1
243
533
                                                                                              : 6;
244
245
    // Jing:
246
    // A tile group can be consisted by 1 tile or NxM tiles.
247
    // Segments will be parallelized within a tile group
248
    // We can use tile group to control the threads/parallelism in ED stage
249
    // NOTE:1 col will have better perf for segments for large resolutions
250
    //by default, do not use tile prallel. to enable, one can set one tile-group per tile.
251
533
    scs->tile_group_col_count_array = 1;
252
533
    scs->tile_group_row_count_array = 1;
253
254
    // TPL processed in 64x64 blocks, so check width against 64x64 block size (even if SB is 128x128)
255
533
    scs->tpl_segment_row_count_array = (lp == PARALLEL_LEVEL_1 ||
256
533
                                        is_pic_dimension_single_sb(64, scs->max_input_luma_width))
257
533
        ? 1
258
533
        : MAX((int32_t)((scs->max_input_luma_height + 32) / 64), 1);
259
260
533
    scs->tpl_segment_col_count_array = (lp == PARALLEL_LEVEL_1)
261
533
        ? 1
262
533
        : MAX((int32_t)((scs->max_input_luma_width + 32) / 64), 1);
263
264
533
    scs->cdef_segment_row_count    = (lp == PARALLEL_LEVEL_1)          ? 1
265
533
           : (((scs->max_input_luma_height + 32) / BLOCK_SIZE_64) < 6) ? 1
266
533
           : (scs->input_resolution <= INPUT_SIZE_1080p_RANGE)         ? 2
267
0
                                                                       : 4;
268
533
    scs->cdef_segment_column_count = (lp == PARALLEL_LEVEL_1)       ? 1
269
533
        : (((scs->max_input_luma_width + 32) / BLOCK_SIZE_64) < 10) ? 1
270
533
        : (scs->input_resolution <= INPUT_SIZE_1080p_RANGE)         ? 3
271
0
                                                                    : 6;
272
273
    //since restoration unit size is same for Luma and Chroma, Luma segments and chroma segments do not correspond to the same area!
274
    //to keep proper processing, segments have to be configured based on chroma resolution.
275
533
    const uint32_t unit_size       = RESTORATION_UNITSIZE_MAX;
276
533
    const uint32_t rest_seg_w      = MAX((scs->max_input_luma_width / 2 + (unit_size >> 1)) / unit_size, 1);
277
533
    const uint32_t rest_seg_h      = MAX((scs->max_input_luma_height / 2 + (unit_size >> 1)) / unit_size, 1);
278
533
    scs->rest_segment_column_count = (lp == PARALLEL_LEVEL_1) ? 1
279
533
        : scs->input_resolution <= INPUT_SIZE_1080p_RANGE     ? MIN(rest_seg_w, 6)
280
533
                                                              : MIN(rest_seg_w, 9);
281
533
    scs->rest_segment_row_count    = (lp == PARALLEL_LEVEL_1) ? 1
282
533
           : scs->input_resolution <= INPUT_SIZE_1080p_RANGE  ? MIN(rest_seg_h, 4)
283
533
                                                              : MIN(rest_seg_h, 6);
284
533
}
285
286
533
static EbErrorType load_default_buffer_configuration_settings(SequenceControlSet* scs) {
287
533
    EbErrorType return_error = EB_ErrorNone;
288
533
    uint32_t    core_count   = get_num_processors();
289
290
533
    uint32_t lp = scs->static_config.level_of_parallelism;
291
533
    if (lp == 0) {
292
        // In the default config (lp == 0) the core count will determine the
293
        // amount of parallelism used
294
0
        if (core_count <= PARALLEL_LEVEL_1_RANGE) {
295
0
            lp = PARALLEL_LEVEL_1;
296
0
        } else if (core_count <= PARALLEL_LEVEL_2_RANGE) {
297
0
            lp = PARALLEL_LEVEL_2;
298
0
        } else if (core_count <= PARALLEL_LEVEL_3_RANGE) {
299
0
            lp = PARALLEL_LEVEL_3;
300
0
        } else if (core_count <= PARALLEL_LEVEL_4_RANGE) {
301
0
            lp = PARALLEL_LEVEL_4;
302
0
        } else if (core_count <= PARALLEL_LEVEL_5_RANGE) {
303
0
            lp = PARALLEL_LEVEL_5;
304
0
        } else {
305
0
            lp = PARALLEL_LEVEL_6;
306
0
        }
307
0
    }
308
533
    scs->lp = lp;
309
533
    set_segments_numbers(scs);
310
311
533
    const bool is_low_delay = (scs->static_config.pred_structure == LOW_DELAY);
312
    // adjust buffer count for superres
313
533
    uint32_t superres_count = (scs->static_config.superres_mode == SUPERRES_AUTO &&
314
0
                               (scs->static_config.superres_auto_search_type == SUPERRES_AUTO_DUAL ||
315
0
                                scs->static_config.superres_auto_search_type == SUPERRES_AUTO_ALL))
316
533
        ? 1
317
533
        : 0;
318
319
    //#====================== Data Structures and Picture Buffers ======================
320
321
533
    uint32_t min_input, min_parent, min_child, min_paref, min_ref, min_tpl_ref, min_overlay, min_recon, min_me;
322
533
    uint32_t max_input, max_parent, max_child, max_paref, max_me, max_recon;
323
324
    /*Look-Ahead. Picture-Decision outputs pictures by group of mini-gops so
325
        the needed pictures for a certain look-ahead distance (LAD) should be rounded up to the next multiple of MiniGopSize.*/
326
533
    uint32_t      mg_size = 1 << scs->static_config.hierarchical_levels;
327
533
    const uint8_t overlay = scs->static_config.enable_overlays ? 1 : 0;
328
329
    /*To accomodate FFMPEG EOS, 1 frame delay is needed in Resource coordination for RA (for the low delay mode, buffering for receiving EOS does not happen).
330
      Note that we have the option to not add 1 frame delay of Resource Coordination. In this case we have wait for first I frame
331
      to be released back to be able to start first base(16). Anyway poc16 needs to wait for poc0 to finish.*/
332
533
    const uint8_t eos_delay = is_low_delay || scs->allintra ? 0 : 1;
333
334
    //Minimum input pictures needed in the pipeline
335
533
    uint16_t lad_mg_pictures = (1 + mg_size + overlay) *
336
533
        scs->lad_mg; //Unit= 1(provision for a potential delayI) + prediction struct + potential overlay        return_ppcs = (1 + mg_size) * (scs->lad_mg + 1)  + scs->scd_delay + eos_delay;
337
533
    uint32_t return_ppcs = (1 + mg_size) * (scs->lad_mg + 1) + scs->scd_delay + eos_delay;
338
    //scs->input_buffer_fifo_init_count = return_ppcs;
339
340
533
    min_input = return_ppcs;
341
342
533
    min_parent = return_ppcs;
343
344
    // If overlay frames are used, each input will be assigned 2 ppcs: one for the regular frame, and one for the potential alt-ref frame
345
533
    if (scs->static_config.enable_overlays) {
346
0
        min_parent *= 2;
347
0
    }
348
349
    //Pic-Manager will inject one child at a time.
350
533
    min_child                          = 1;
351
533
    const uint16_t num_ref_from_cur_mg = get_num_refs_in_one_mg(scs->static_config.hierarchical_levels,
352
533
                                                                scs->mrp_ctrls.referencing_scheme) +
353
533
        1; //+1: to accomodate one for a delayed-I
354
533
    const uint16_t num_ref_lad_mgs = num_ref_from_cur_mg * scs->lad_mg;
355
533
    const uint8_t  dpb_frames =
356
533
        REF_FRAMES; // up to dpb_frame refs from prev MGs can be used (AV1 spec allows holding up to 8 frames for references)
357
533
    min_ref     = (scs->enable_dec_order) ? dpb_frames + 1 : num_ref_from_cur_mg + num_ref_lad_mgs + dpb_frames;
358
533
    min_tpl_ref = scs->tpl ? dpb_frames + 1 : 0; // TPL pictures are processed in decode order
359
533
    if (scs->tpl) {
360
        // PictureDecisionContext.mg_size = mg_size + overlay; see EbPictureDecisionProcess.c line 5680
361
0
        min_me = 1 + // potential delay I
362
0
            lad_mg_pictures + // 16 + 1 ME data used in store_tpl_pictures() at line 5717
363
0
            (mg_size + overlay); // 16 + 1 ME data used in store_tpl_pictures() at line 5729
364
533
    } else {
365
533
        min_me = 1;
366
533
    }
367
368
    //PA REF
369
533
    const uint16_t num_pa_ref_from_cur_mg =
370
533
        mg_size; //ref+nref; nRef PA buffers are processed in PicAnalysis and used in TF
371
533
    min_paref = num_pa_ref_from_cur_mg + lad_mg_pictures + scs->scd_delay + eos_delay + dpb_frames;
372
373
533
    if (scs->static_config.enable_overlays) {
374
        // Need an extra PA ref buffer for each overlay picture. Overlay pics use the same DPB as
375
        // regular pics, so no need to allocate an extra dpb_frames buffers for the ref pics
376
0
        min_paref += num_pa_ref_from_cur_mg + lad_mg_pictures + scs->scd_delay + eos_delay;
377
0
    }
378
    //Overlays
379
    // Each input pic will assign a ppcs and for each potential overlay, will assign a buffer to store the unfiltered input picture
380
533
    min_overlay = scs->static_config.enable_overlays ? return_ppcs : 0;
381
533
    min_recon   = min_ref;
382
383
533
    if (is_low_delay) {
384
0
        uint32_t low_delay_tf_frames = scs->tf_params_per_type[1].max_num_past_pics;
385
0
        min_input = min_parent = 1 + low_delay_tf_frames + scs->scd_delay + eos_delay;
386
0
        min_child              = 1; // max_child is 1 for LD
387
0
        uint8_t max_refs       = dpb_frames;
388
        // For special, known, RPS structures and ref frame counts, we can reduce the number of ref buffers
389
0
        if (scs->use_flat_ipp) {
390
0
            max_refs = scs->mrp_ctrls.flat_max_refs;
391
            // For flat IPP the previous frame is always used as a reference. Therefore, that picture does
392
            // not require a special buffer for use as a TF ref.
393
0
            if (low_delay_tf_frames) {
394
0
                low_delay_tf_frames -= 1;
395
0
            }
396
0
        } else if (scs->mrp_ctrls.ld_reduce_ref_buffs == 1) {
397
0
            max_refs = 4;
398
0
        } else if (scs->mrp_ctrls.ld_reduce_ref_buffs == 2) {
399
0
            max_refs = 2;
400
0
        }
401
402
0
        min_ref   = 1 /*current pic*/ + max_refs;
403
0
        min_me    = 1;
404
0
        min_paref = 1 /*current pic*/ + low_delay_tf_frames + max_refs + scs->scd_delay + eos_delay;
405
0
    }
406
    //Configure max needed buffers to process 1+n_extra_mg Mini-Gops in the pipeline. n extra MGs to feed to picMgr on top of current one.
407
    // Low delay mode has no extra minigops to process.
408
533
    uint32_t n_extra_mg;
409
533
    if (lp <= PARALLEL_LEVEL_3 || is_low_delay) {
410
0
        n_extra_mg = 0;
411
533
    } else if (lp <= PARALLEL_LEVEL_4) {
412
533
        n_extra_mg = 1;
413
533
    } else if (lp <= PARALLEL_LEVEL_5) {
414
0
        n_extra_mg = 2;
415
0
    } else {
416
0
        n_extra_mg = scs->input_resolution <= INPUT_SIZE_4K_RANGE ? 7
417
0
            : scs->input_resolution <= INPUT_SIZE_8K_RANGE        ? 5
418
0
                                                                  : 0;
419
0
    }
420
421
533
    max_input  = min_input + (1 + mg_size) * n_extra_mg;
422
533
    max_parent = max_input;
423
533
    max_child  = (mg_size / 2) * (n_extra_mg + 1);
424
533
    max_child  = MAX(max_child, 1); //have at least one child for mg_size<2
425
    // In low delay mode, will only have one picture at a time to process
426
533
    if (is_low_delay) {
427
0
        max_child = 1;
428
0
    }
429
430
    // max_ref defines here to avoid cppcheck warning
431
533
    uint32_t max_ref = min_ref + num_ref_from_cur_mg * n_extra_mg;
432
533
    max_paref        = min_paref + (1 + mg_size) * n_extra_mg;
433
533
    max_me           = min_me + (1 + mg_size) * n_extra_mg;
434
533
    max_recon        = max_ref;
435
    // if tpl_la is disabled when super-res fix/random, input speed is much faster than recon output speed,
436
    // recon_output_fifo might be full and freeze at svt_aom_recon_output()
437
533
    if (!scs->tpl && scs->static_config.recon_enabled) {
438
0
        max_recon = min_recon = MAX(max_ref, 30);
439
0
    }
440
441
    //#====================== Process Buffers ======================
442
533
    scs->input_buffer_fifo_init_count            = clamp(max_input, min_input, max_input);
443
533
    scs->picture_control_set_pool_init_count     = clamp(max_parent, min_parent, max_parent);
444
533
    scs->pa_reference_picture_buffer_init_count  = clamp(max_paref, min_paref, max_paref);
445
533
    scs->tpl_reference_picture_buffer_init_count = min_tpl_ref;
446
533
    scs->output_recon_buffer_fifo_init_count = scs->reference_picture_buffer_init_count = clamp(
447
533
        max_recon, min_recon, max_recon);
448
533
    scs->me_pool_init_count                      = clamp(max_me, min_me, max_me);
449
533
    scs->overlay_input_picture_buffer_init_count = min_overlay;
450
451
533
    if (lp <= PARALLEL_LEVEL_1 || MIN_PIC_PARALLELIZATION) {
452
0
        scs->input_buffer_fifo_init_count              = min_input;
453
0
        scs->picture_control_set_pool_init_count       = min_parent;
454
0
        scs->pa_reference_picture_buffer_init_count    = min_paref;
455
0
        scs->tpl_reference_picture_buffer_init_count   = min_tpl_ref;
456
0
        scs->reference_picture_buffer_init_count       = min_ref;
457
0
        scs->picture_control_set_pool_init_count_child = min_child;
458
0
        scs->enc_dec_pool_init_count                   = min_child;
459
0
        scs->me_pool_init_count                        = min_me;
460
0
        scs->overlay_input_picture_buffer_init_count   = min_overlay;
461
462
0
        scs->output_recon_buffer_fifo_init_count = MAX(scs->reference_picture_buffer_init_count, min_recon);
463
0
#if CONFIG_SINGLE_THREAD_KERNEL
464
        // In ST mode the dispatcher processes a full mini-GOP before returning
465
        // to the app.  svt_aom_recon_output() is called from both REST and
466
        // Packetization for each non-alt-ref frame, consuming 2 buffers per
467
        // frame.  The pool must hold enough for the full mini-GOP.
468
0
        if (scs->static_config.recon_enabled) {
469
0
            scs->output_recon_buffer_fifo_init_count = MAX(scs->output_recon_buffer_fifo_init_count, return_ppcs);
470
0
        }
471
0
#endif
472
533
    } else if (lp <= PARALLEL_LEVEL_2) {
473
0
        scs->picture_control_set_pool_init_count_child = scs->enc_dec_pool_init_count = clamp(2, min_child, max_child) +
474
0
            superres_count;
475
533
    } else if (lp <= PARALLEL_LEVEL_3) {
476
0
        scs->picture_control_set_pool_init_count_child = scs->enc_dec_pool_init_count = clamp(8, min_child, max_child) +
477
0
            superres_count;
478
533
    } else if (lp <= PARALLEL_LEVEL_4) {
479
533
        scs->picture_control_set_pool_init_count_child = scs->enc_dec_pool_init_count = clamp(
480
533
                                                                                            12, min_child, max_child) +
481
533
            superres_count;
482
533
    } else if (lp <= PARALLEL_LEVEL_5) {
483
0
        scs->picture_control_set_pool_init_count_child = scs->enc_dec_pool_init_count = clamp(
484
0
                                                                                            16, min_child, max_child) +
485
0
            superres_count;
486
0
    } else {
487
0
        const uint8_t pcs_processes                    = scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_VBR &&
488
0
                scs->static_config.pass == ENC_SECOND_PASS
489
0
                               ? 24
490
0
                               : 20;
491
0
        scs->picture_control_set_pool_init_count_child = scs->enc_dec_pool_init_count =
492
0
            clamp(pcs_processes, min_child, max_child) + superres_count;
493
0
    }
494
495
533
    if (scs->static_config.avif) {
496
533
        scs->input_buffer_fifo_init_count              = 4;
497
533
        scs->picture_control_set_pool_init_count       = 4;
498
533
        scs->pa_reference_picture_buffer_init_count    = 4;
499
533
        scs->tpl_reference_picture_buffer_init_count   = 0;
500
533
        scs->output_recon_buffer_fifo_init_count       = 1;
501
533
        scs->reference_picture_buffer_init_count       = 2;
502
533
        scs->picture_control_set_pool_init_count_child = 1;
503
533
        scs->enc_dec_pool_init_count                   = 1;
504
533
        scs->me_pool_init_count                        = 1;
505
533
        scs->overlay_input_picture_buffer_init_count   = 0;
506
533
        scs->allintra                                  = true;
507
533
        scs->static_config.intra_period_length         = 0;
508
533
    }
509
510
    //#====================== Inter process Fifos ======================
511
533
    const uint32_t tot_tf_segs  = scs->tf_segment_column_count * scs->tf_segment_row_count;
512
533
    const uint32_t tot_me_segs  = scs->me_segment_col_count_array * scs->me_segment_row_count_array;
513
533
    const uint32_t tot_tpl_segs = scs->tpl_segment_col_count_array * scs->tpl_segment_row_count_array;
514
    // For VBR/Capped-CRF and superres, we may use a recode loop, in which case, the enc dec fifo queue should be increased
515
    // to account for the extra potential calls.
516
533
    const bool     allow_recode     = scs->static_config.recode_loop != DISALLOW_RECODE;
517
533
    const bool     is_superres      = scs->static_config.superres_mode != SUPERRES_NONE;
518
533
    const uint32_t tot_enc_dec_segs = scs->enc_dec_segment_col_count_array * scs->enc_dec_segment_row_count_array;
519
533
    const uint32_t tot_cdef_segs    = scs->cdef_segment_column_count * scs->cdef_segment_row_count;
520
533
    const uint32_t tot_rest_segs    = scs->rest_segment_column_count * scs->rest_segment_row_count;
521
533
    const uint32_t tot_tiles        = MIN(9,
522
533
                                   (1 << scs->static_config.tile_columns) *
523
533
                                       (1 << scs->static_config.tile_rows)); //Jing: Too many tiles may drain the fifo
524
533
    const uint32_t max_fifo         = 300;
525
526
    // Open loop
527
533
    scs->resource_coordination_fifo_init_count = MIN(
528
533
        max_fifo, scs->picture_control_set_pool_init_count); // outputs one pic @ a time to pic analysis (no segments)
529
533
    scs->picture_analysis_fifo_init_count = MIN(
530
533
        max_fifo, scs->picture_control_set_pool_init_count); // output from pic analysis to PD process (single threaded)
531
533
    scs->enc_ctx->picture_decision_reorder_queue_size = scs->picture_control_set_pool_init_count;
532
533
    scs->picture_decision_fifo_init_count             = MIN(
533
533
        max_fifo, scs->picture_control_set_pool_init_count * MAX(tot_me_segs, tot_tf_segs));
534
533
    scs->motion_estimation_fifo_init_count = MIN(
535
533
        max_fifo, scs->picture_control_set_pool_init_count); // output from ME to init_rc_process (single threaded)
536
533
    scs->initial_rate_control_fifo_init_count = MIN(
537
533
        max_fifo, scs->picture_control_set_pool_init_count); // output from irc to src_ops (single threaded)
538
533
    scs->tpl_disp_fifo_init_count = MIN(max_fifo,
539
533
                                        scs->picture_control_set_pool_init_count * tot_tpl_segs); // TPL dispenser
540
541
    // Pic manager (pass through from open loop to closed loop)
542
533
    scs->picture_demux_fifo_init_count = MIN(
543
533
        max_fifo,
544
533
        scs->picture_control_set_pool_init_count +
545
533
            2 * scs->picture_control_set_pool_init_count_child); // input to pic mgr from src ops, rest, and packetization
546
533
    scs->enc_ctx->pic_mgr_input_pic_list_size = scs->picture_control_set_pool_init_count;
547
548
    // Closed loop
549
533
    scs->rate_control_tasks_fifo_init_count = MIN(
550
533
        max_fifo,
551
533
        2 * scs->picture_control_set_pool_init_count_child); // inputs to rc form pic manager and EC/packetization
552
533
    scs->rate_control_fifo_init_count                = MIN(max_fifo,
553
533
                                            scs->picture_control_set_pool_init_count_child); // inputs to MDC from rc
554
533
    scs->mode_decision_configuration_fifo_init_count = MIN(
555
533
        max_fifo,
556
533
        scs->picture_control_set_pool_init_count_child * tot_tiles * tot_enc_dec_segs *
557
533
            (1 + allow_recode + is_superres)); // input to MD from MDC
558
533
    scs->enc_dec_fifo_init_count = MIN(max_fifo,
559
533
                                       scs->picture_control_set_pool_init_count_child); // TODO: Add DLF segments
560
533
    scs->dlf_fifo_init_count     = MIN(
561
533
        max_fifo, scs->picture_control_set_pool_init_count_child * tot_cdef_segs); // input to CDEF from DLF
562
533
    scs->cdef_fifo_init_count = MIN(
563
533
        max_fifo, scs->picture_control_set_pool_init_count_child * tot_rest_segs); // input to rest from CDEF
564
533
    scs->rest_fifo_init_count = MIN(
565
533
        max_fifo, scs->picture_control_set_pool_init_count_child * tot_tiles); // input to EC from rest
566
533
    scs->entropy_coding_fifo_init_count = MIN(
567
533
        max_fifo, scs->picture_control_set_pool_init_count_child); // EC outputs to packetization (single threaded)
568
533
    scs->enc_ctx->packetization_reorder_queue_size = scs->picture_control_set_pool_init_count;
569
    // bistream buffer will be allocated at run time. app will free the buffer once written to file.
570
533
    scs->output_stream_buffer_fifo_init_count = scs->picture_control_set_pool_init_count +
571
533
        2; // +2 b/c used to signal EOS @ resource coord and packetization
572
    //#====================== Processes number ======================
573
533
    scs->total_process_init_count = 0;
574
575
533
    uint32_t max_pa_proc, max_me_proc, max_tpl_proc, max_mdc_proc, max_md_proc, max_ec_proc, max_dlf_proc,
576
533
        max_cdef_proc, max_rest_proc;
577
578
533
    max_pa_proc  = max_input;
579
533
    max_me_proc  = max_me * tot_me_segs;
580
533
    max_tpl_proc = get_max_wavefronts(scs->max_input_luma_width, scs->max_input_luma_height, 64);
581
533
    max_mdc_proc = scs->picture_control_set_pool_init_count_child;
582
533
    max_md_proc  = scs->picture_control_set_pool_init_count_child *
583
533
        get_max_wavefronts(scs->max_input_luma_width, scs->max_input_luma_height, scs->super_block_size);
584
533
    max_ec_proc   = scs->picture_control_set_pool_init_count_child;
585
533
    max_dlf_proc  = scs->picture_control_set_pool_init_count_child;
586
533
    max_cdef_proc = scs->picture_control_set_pool_init_count_child * scs->cdef_segment_column_count *
587
533
        scs->cdef_segment_row_count;
588
533
    max_rest_proc = scs->picture_control_set_pool_init_count_child * scs->rest_segment_column_count *
589
533
        scs->rest_segment_row_count;
590
591
533
    if (lp <= PARALLEL_LEVEL_1) {
592
0
        scs->total_process_init_count += (scs->picture_analysis_process_init_count = 1);
593
0
        scs->total_process_init_count += (scs->motion_estimation_process_init_count = 1);
594
0
        scs->total_process_init_count += (scs->source_based_operations_process_init_count = 1);
595
0
        scs->total_process_init_count += (scs->tpl_disp_process_init_count = 1);
596
0
        scs->total_process_init_count += (scs->mode_decision_configuration_process_init_count = 1);
597
0
        scs->total_process_init_count += (scs->enc_dec_process_init_count = 1);
598
0
        scs->total_process_init_count += (scs->entropy_coding_process_init_count = 1);
599
0
        scs->total_process_init_count += (scs->dlf_process_init_count = 1);
600
0
        scs->total_process_init_count += (scs->cdef_process_init_count = 1);
601
0
        scs->total_process_init_count += (scs->rest_process_init_count = 1);
602
533
    } else if (lp <= PARALLEL_LEVEL_2) {
603
0
        const uint8_t pa_processes = scs->static_config.film_grain_denoise_strength ? 16 : 1;
604
0
        scs->total_process_init_count += (scs->source_based_operations_process_init_count = 1);
605
0
        scs->total_process_init_count += (scs->picture_analysis_process_init_count = clamp(
606
0
                                              pa_processes, 1, max_pa_proc));
607
0
        scs->total_process_init_count += (scs->motion_estimation_process_init_count = clamp(20, 1, max_me_proc));
608
0
        scs->total_process_init_count += (scs->tpl_disp_process_init_count = clamp(6, 1, max_tpl_proc));
609
0
        scs->total_process_init_count += (scs->mode_decision_configuration_process_init_count = clamp(
610
0
                                              1, 1, max_mdc_proc));
611
0
        scs->total_process_init_count += (scs->enc_dec_process_init_count = clamp(
612
0
                                              3, scs->picture_control_set_pool_init_count_child, max_md_proc));
613
0
        scs->total_process_init_count += (scs->entropy_coding_process_init_count = clamp(1, 1, max_ec_proc));
614
0
        scs->total_process_init_count += (scs->dlf_process_init_count = clamp(1, 1, max_dlf_proc));
615
0
        scs->total_process_init_count += (scs->cdef_process_init_count = clamp(6, 1, max_cdef_proc));
616
0
        scs->total_process_init_count += (scs->rest_process_init_count = clamp(1, 1, max_rest_proc));
617
533
    } else if (lp <= PARALLEL_LEVEL_3) {
618
0
        const uint8_t pa_processes = scs->static_config.film_grain_denoise_strength ? 16 : 1;
619
0
        scs->total_process_init_count += (scs->source_based_operations_process_init_count = 1);
620
0
        scs->total_process_init_count += (scs->picture_analysis_process_init_count = clamp(
621
0
                                              pa_processes, 1, max_pa_proc));
622
0
        scs->total_process_init_count += (scs->motion_estimation_process_init_count = clamp(25, 1, max_me_proc));
623
0
        scs->total_process_init_count += (scs->tpl_disp_process_init_count = clamp(6, 1, max_tpl_proc));
624
0
        scs->total_process_init_count += (scs->mode_decision_configuration_process_init_count = clamp(
625
0
                                              2, 1, max_mdc_proc));
626
0
        scs->total_process_init_count += (scs->enc_dec_process_init_count = clamp(
627
0
                                              5, scs->picture_control_set_pool_init_count_child, max_md_proc));
628
0
        scs->total_process_init_count += (scs->entropy_coding_process_init_count = clamp(2, 1, max_ec_proc));
629
0
        scs->total_process_init_count += (scs->dlf_process_init_count = clamp(2, 1, max_dlf_proc));
630
0
        scs->total_process_init_count += (scs->cdef_process_init_count = clamp(6, 1, max_cdef_proc));
631
0
        scs->total_process_init_count += (scs->rest_process_init_count = clamp(2, 1, max_rest_proc));
632
533
    } else if (lp <= PARALLEL_LEVEL_5 || scs->input_resolution <= INPUT_SIZE_1080p_RANGE) {
633
533
        uint8_t pa_processes = scs->static_config.film_grain_denoise_strength ? 20 : 4;
634
533
        if (scs->static_config.pass == ENC_FIRST_PASS) {
635
0
            pa_processes = lp <= PARALLEL_LEVEL_5 ? 12 : 20;
636
0
        }
637
533
        scs->total_process_init_count += (scs->source_based_operations_process_init_count = 1);
638
533
        scs->total_process_init_count += (scs->picture_analysis_process_init_count = clamp(
639
533
                                              pa_processes, 1, max_pa_proc));
640
533
        scs->total_process_init_count += (scs->motion_estimation_process_init_count = clamp(25, 1, max_me_proc));
641
533
        scs->total_process_init_count += (scs->tpl_disp_process_init_count = clamp(6, 1, max_tpl_proc));
642
533
        scs->total_process_init_count += (scs->mode_decision_configuration_process_init_count = clamp(
643
533
                                              3, 1, max_mdc_proc));
644
533
        scs->total_process_init_count += (scs->enc_dec_process_init_count = clamp(
645
533
                                              6, scs->picture_control_set_pool_init_count_child, max_md_proc));
646
533
        scs->total_process_init_count += (scs->entropy_coding_process_init_count = clamp(4, 1, max_ec_proc));
647
533
        scs->total_process_init_count += (scs->dlf_process_init_count = clamp(3, 1, max_dlf_proc));
648
533
        scs->total_process_init_count += (scs->cdef_process_init_count = clamp(6, 1, max_cdef_proc));
649
533
        scs->total_process_init_count += (scs->rest_process_init_count = clamp(4, 1, max_rest_proc));
650
533
    } else {
651
0
        const uint8_t pa_processes = (scs->static_config.pass == ENC_FIRST_PASS ||
652
0
                                      scs->static_config.film_grain_denoise_strength)
653
0
            ? 20
654
0
            : 16;
655
0
        scs->total_process_init_count += (scs->source_based_operations_process_init_count = 1);
656
0
        scs->total_process_init_count += (scs->picture_analysis_process_init_count = clamp(
657
0
                                              pa_processes, 1, max_pa_proc));
658
0
        scs->total_process_init_count += (scs->motion_estimation_process_init_count = clamp(25, 1, max_me_proc));
659
0
        scs->total_process_init_count += (scs->tpl_disp_process_init_count = clamp(12, 1, max_tpl_proc));
660
0
        scs->total_process_init_count += (scs->mode_decision_configuration_process_init_count = clamp(
661
0
                                              8, 1, max_mdc_proc));
662
0
        scs->total_process_init_count += (scs->enc_dec_process_init_count = clamp(
663
0
                                              8, scs->picture_control_set_pool_init_count_child, max_md_proc));
664
0
        scs->total_process_init_count += (scs->entropy_coding_process_init_count = clamp(10, 1, max_ec_proc));
665
0
        scs->total_process_init_count += (scs->dlf_process_init_count = clamp(8, 1, max_dlf_proc));
666
0
        scs->total_process_init_count += (scs->cdef_process_init_count = clamp(8, 1, max_cdef_proc));
667
0
        scs->total_process_init_count += (scs->rest_process_init_count = clamp(10, 1, max_rest_proc));
668
0
    }
669
670
533
    scs->total_process_init_count += 6; // single processes count
671
533
    if (scs->static_config.pass == 0 || scs->static_config.pass == 2) {
672
533
        SVT_INFO("Level of Parallelism: %u\n", lp);
673
533
        SVT_INFO("Number of PPCS %u\n", scs->picture_control_set_pool_init_count);
674
675
        /******************************************************************
676
        * Platform detection, limit cpu flags to hardware available CPU
677
        ******************************************************************/
678
#if defined ARCH_X86_64 || defined ARCH_AARCH64
679
        const EbCpuFlags cpu_flags        = svt_aom_get_cpu_flags();
680
        const EbCpuFlags cpu_flags_to_use = svt_aom_get_cpu_flags_to_use();
681
        scs->static_config.use_cpu_flags &= cpu_flags_to_use;
682
        SVT_INFO("[asm level on system : up to %s]\n", get_asm_level_name_str(cpu_flags));
683
        SVT_INFO("[asm level selected : up to %s]\n", get_asm_level_name_str(scs->static_config.use_cpu_flags));
684
#else
685
533
        scs->static_config.use_cpu_flags &= 0;
686
533
        SVT_INFO("[asm level on system : up to %s]\n", get_asm_level_name_str(0));
687
533
        SVT_INFO("[asm level selected : up to %s]\n", get_asm_level_name_str(scs->static_config.use_cpu_flags));
688
533
#endif
689
533
    }
690
533
    return return_error;
691
533
}
692
693
// clang-format off
694
static RateControlPorts rate_control_ports[] = {
695
    {RATE_CONTROL_INPUT_PORT_INLME,         0},
696
    {RATE_CONTROL_INPUT_PORT_PACKETIZATION, 0},
697
    {RATE_CONTROL_INPUT_PORT_INVALID,       0}
698
};
699
700
static PicMgrPorts pic_mgr_ports[] = {
701
    {PIC_MGR_INPUT_PORT_SOP,           0},
702
    {PIC_MGR_INPUT_PORT_PACKETIZATION, 0},
703
    {PIC_MGR_INPUT_PORT_REST,          0},
704
    {PIC_MGR_INPUT_PORT_INVALID,       0}
705
};
706
707
typedef struct {
708
    int32_t  type;
709
    uint32_t count;
710
} EncDecPorts_t;
711
712
static EncDecPorts_t enc_dec_ports[] = {
713
    {ENCDEC_INPUT_PORT_MDC,     0},
714
    {ENCDEC_INPUT_PORT_ENCDEC,  0},
715
    {ENCDEC_INPUT_PORT_INVALID, 0}
716
};
717
static EncDecPorts_t tpl_ports[] = {
718
    {TPL_INPUT_PORT_SOP,     0},
719
    {TPL_INPUT_PORT_TPL,     0},
720
    {TPL_INPUT_PORT_INVALID, 0}
721
};
722
// clang-format on
723
724
// Rate Control
725
1.06k
static uint32_t rate_control_port_lookup(RateControlInputPortTypes type, uint32_t port_type_index) {
726
1.06k
    uint32_t port_index = 0;
727
1.06k
    uint32_t port_count = 0;
728
729
1.59k
    while ((type != rate_control_ports[port_index].type) && (type != RATE_CONTROL_INPUT_PORT_INVALID)) {
730
533
        port_count += rate_control_ports[port_index++].count;
731
533
    }
732
1.06k
    return (port_count + port_type_index);
733
1.06k
}
734
735
// Rate Control
736
533
static uint32_t rate_control_port_total_count(void) {
737
533
    uint32_t port_index  = 0;
738
533
    uint32_t total_count = 0;
739
740
1.59k
    while (rate_control_ports[port_index].type != RATE_CONTROL_INPUT_PORT_INVALID) {
741
1.06k
        total_count += rate_control_ports[port_index++].count;
742
1.06k
    }
743
533
    return total_count;
744
533
}
745
746
1.59k
static uint32_t pic_mgr_port_lookup(PicMgrInputPortTypes type, uint32_t port_type_index) {
747
1.59k
    uint32_t port_index = 0;
748
1.59k
    uint32_t port_count = 0;
749
750
3.19k
    while ((type != pic_mgr_ports[port_index].type) && (type != PIC_MGR_INPUT_PORT_INVALID)) {
751
1.59k
        port_count += pic_mgr_ports[port_index++].count;
752
1.59k
    }
753
1.59k
    return (port_count + port_type_index);
754
1.59k
}
755
756
533
static uint32_t pic_mgr_port_total_count(void) {
757
533
    uint32_t port_index  = 0;
758
533
    uint32_t total_count = 0;
759
760
2.13k
    while (pic_mgr_ports[port_index].type != PIC_MGR_INPUT_PORT_INVALID) {
761
1.59k
        total_count += pic_mgr_ports[port_index++].count;
762
1.59k
    }
763
533
    return total_count;
764
533
}
765
766
// TPL
767
2.19k
static uint32_t tpl_port_lookup(int32_t type, uint32_t port_type_index) {
768
2.19k
    uint32_t port_index = 0;
769
2.19k
    uint32_t port_count = 0;
770
771
3.86k
    while ((type != tpl_ports[port_index].type) && (type != TPL_INPUT_PORT_INVALID)) {
772
1.66k
        port_count += tpl_ports[port_index++].count;
773
1.66k
    }
774
2.19k
    return (port_count + port_type_index);
775
2.19k
}
776
777
533
static uint32_t tpl_port_total_count(void) {
778
533
    uint32_t port_index  = 0;
779
533
    uint32_t total_count = 0;
780
781
1.59k
    while (tpl_ports[port_index].type != TPL_INPUT_PORT_INVALID) {
782
1.06k
        total_count += tpl_ports[port_index++].count;
783
1.06k
    }
784
533
    return total_count;
785
533
}
786
787
/*****************************************
788
 * Input Port Lookup
789
 *****************************************/
790
// EncDec
791
2.19k
static uint32_t enc_dec_port_lookup(int32_t type, uint32_t port_type_index) {
792
2.19k
    uint32_t port_index = 0;
793
2.19k
    uint32_t port_count = 0;
794
795
3.86k
    while ((type != enc_dec_ports[port_index].type) && (type != ENCDEC_INPUT_PORT_INVALID)) {
796
1.66k
        port_count += enc_dec_ports[port_index++].count;
797
1.66k
    }
798
2.19k
    return (port_count + port_type_index);
799
2.19k
}
800
801
// EncDec
802
533
static uint32_t enc_dec_port_total_count(void) {
803
533
    uint32_t port_index  = 0;
804
533
    uint32_t total_count = 0;
805
806
1.59k
    while (enc_dec_ports[port_index].type != ENCDEC_INPUT_PORT_INVALID) {
807
1.06k
        total_count += enc_dec_ports[port_index++].count;
808
1.06k
    }
809
533
    return total_count;
810
533
}
811
812
/*****************************************
813
 * Input Port Total Count
814
 *****************************************/
815
816
static void lib_svt_encoder_send_error_exit(EbPtr hComponent, uint32_t error_code);
817
818
533
static void svt_enc_handle_stop_threads(EbEncHandle* enc_handle_ptr) {
819
533
    SequenceControlSet* scs = enc_handle_ptr->scs_instance->scs;
820
    // Resource Coordination
821
533
    EB_DESTROY_THREAD(enc_handle_ptr->resource_coordination_thread_handle);
822
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->picture_analysis_thread_handle_array,
823
533
                            scs->picture_analysis_process_init_count);
824
825
    // Picture Decision
826
533
    EB_DESTROY_THREAD(enc_handle_ptr->picture_decision_thread_handle);
827
828
    // Motion Estimation
829
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->motion_estimation_thread_handle_array,
830
533
                            scs->motion_estimation_process_init_count);
831
832
    // Initial Rate Control
833
533
    EB_DESTROY_THREAD(enc_handle_ptr->initial_rate_control_thread_handle);
834
835
    // Source Based Oprations
836
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->source_based_operations_thread_handle_array,
837
533
                            scs->source_based_operations_process_init_count);
838
839
    // TPL dispenser ME
840
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->tpl_disp_thread_handle_array, scs->tpl_disp_process_init_count);
841
842
    // Picture Manager
843
533
    EB_DESTROY_THREAD(enc_handle_ptr->picture_manager_thread_handle);
844
845
    // Rate Control
846
533
    EB_DESTROY_THREAD(enc_handle_ptr->rate_control_thread_handle);
847
848
    // Mode Decision Configuration Process
849
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->mode_decision_configuration_thread_handle_array,
850
533
                            scs->mode_decision_configuration_process_init_count);
851
852
    // EncDec Process
853
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->enc_dec_thread_handle_array, scs->enc_dec_process_init_count);
854
855
    // Dlf Process
856
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->dlf_thread_handle_array, scs->dlf_process_init_count);
857
858
    // Cdef Process
859
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->cdef_thread_handle_array, scs->cdef_process_init_count);
860
861
    // Rest Process
862
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->rest_thread_handle_array, scs->rest_process_init_count);
863
864
    // Entropy Coding Process
865
533
    EB_DESTROY_THREAD_ARRAY(enc_handle_ptr->entropy_coding_thread_handle_array, scs->entropy_coding_process_init_count);
866
867
    // Packetization
868
533
    EB_DESTROY_THREAD(enc_handle_ptr->packetization_thread_handle);
869
533
}
870
871
/**********************************
872
* Encoder Library Handle Deonstructor
873
**********************************/
874
533
static void svt_enc_handle_dctor(EbPtr p) {
875
533
    EbEncHandle* enc_handle_ptr = (EbEncHandle*)p;
876
533
    svt_enc_handle_stop_threads(enc_handle_ptr);
877
533
    EB_FREE(enc_handle_ptr->app_callback_ptr);
878
533
    EB_DELETE(enc_handle_ptr->scs_pool_ptr);
879
533
    EB_DELETE(enc_handle_ptr->picture_parent_control_set_pool_ptr);
880
533
    EB_DELETE(enc_handle_ptr->me_pool_ptr);
881
533
    EB_DELETE(enc_handle_ptr->picture_control_set_pool_ptr);
882
533
    EB_DELETE(enc_handle_ptr->enc_dec_pool_ptr);
883
533
    EB_DELETE(enc_handle_ptr->pa_reference_picture_pool_ptr);
884
533
    EB_DELETE(enc_handle_ptr->tpl_reference_picture_pool_ptr);
885
533
    EB_DELETE(enc_handle_ptr->overlay_input_picture_pool_ptr);
886
533
    EB_DELETE(enc_handle_ptr->reference_picture_pool_ptr);
887
533
    EB_DELETE(enc_handle_ptr->input_cmd_resource_ptr);
888
533
    EB_DELETE(enc_handle_ptr->input_y8b_buffer_resource_ptr);
889
890
    //all y_buffer have been redirected to y8b location that just got released.
891
    //to prevent releasing twice, we need to reset the buffer back to NULL
892
533
    if (enc_handle_ptr->input_buffer_resource_ptr) {
893
2.66k
        for (uint32_t w_i = 0; w_i < enc_handle_ptr->input_buffer_resource_ptr->object_total_count; ++w_i) {
894
2.13k
            EbObjectWrapper*     wrp  = enc_handle_ptr->input_buffer_resource_ptr->wrapper_ptr_pool[w_i];
895
2.13k
            EbBufferHeaderType*  obj  = (EbBufferHeaderType*)wrp->object_ptr;
896
2.13k
            EbPictureBufferDesc* desc = (EbPictureBufferDesc*)obj->p_buffer;
897
2.13k
            desc->y_buffer            = 0;
898
2.13k
        }
899
533
    }
900
533
    EB_DELETE(enc_handle_ptr->input_buffer_resource_ptr);
901
533
    EB_DELETE(enc_handle_ptr->output_stream_buffer_resource_ptr);
902
533
    EB_DELETE(enc_handle_ptr->output_recon_buffer_resource_ptr);
903
533
    EB_DELETE(enc_handle_ptr->resource_coordination_results_resource_ptr);
904
533
    EB_DELETE(enc_handle_ptr->picture_analysis_results_resource_ptr);
905
533
    EB_DELETE(enc_handle_ptr->picture_decision_results_resource_ptr);
906
533
    EB_DELETE(enc_handle_ptr->motion_estimation_results_resource_ptr);
907
533
    EB_DELETE(enc_handle_ptr->initial_rate_control_results_resource_ptr);
908
533
    EB_DELETE(enc_handle_ptr->picture_demux_results_resource_ptr);
909
533
    EB_DELETE(enc_handle_ptr->tpl_disp_res_srm);
910
533
    EB_DELETE(enc_handle_ptr->rate_control_tasks_resource_ptr);
911
533
    EB_DELETE(enc_handle_ptr->rate_control_results_resource_ptr);
912
533
    EB_DELETE(enc_handle_ptr->enc_dec_tasks_resource_ptr);
913
533
    EB_DELETE(enc_handle_ptr->enc_dec_results_resource_ptr);
914
533
    EB_DELETE(enc_handle_ptr->dlf_results_resource_ptr);
915
533
    EB_DELETE(enc_handle_ptr->cdef_results_resource_ptr);
916
533
    EB_DELETE(enc_handle_ptr->rest_results_resource_ptr);
917
533
    EB_DELETE(enc_handle_ptr->entropy_coding_results_resource_ptr);
918
919
533
    EB_DELETE(enc_handle_ptr->resource_coordination_context_ptr);
920
533
    SequenceControlSet* scs = enc_handle_ptr->scs_instance->scs;
921
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->picture_analysis_context_ptr_array, scs->picture_analysis_process_init_count);
922
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->motion_estimation_context_ptr_array, scs->motion_estimation_process_init_count);
923
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->tpl_disp_context_ptr_array, scs->tpl_disp_process_init_count);
924
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->source_based_operations_context_ptr_array,
925
533
                        scs->source_based_operations_process_init_count);
926
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->mode_decision_configuration_context_ptr_array,
927
533
                        scs->mode_decision_configuration_process_init_count);
928
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->enc_dec_context_ptr_array, scs->enc_dec_process_init_count);
929
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->dlf_context_ptr_array, scs->dlf_process_init_count);
930
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->cdef_context_ptr_array, scs->cdef_process_init_count);
931
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->rest_context_ptr_array, scs->rest_process_init_count);
932
533
    EB_DELETE_PTR_ARRAY(enc_handle_ptr->entropy_coding_context_ptr_array, scs->entropy_coding_process_init_count);
933
533
    EB_DELETE(enc_handle_ptr->scs_instance);
934
533
    EB_DELETE(enc_handle_ptr->picture_decision_context_ptr);
935
533
    EB_DELETE(enc_handle_ptr->initial_rate_control_context_ptr);
936
533
    EB_DELETE(enc_handle_ptr->picture_manager_context_ptr);
937
533
    EB_DELETE(enc_handle_ptr->rate_control_context_ptr);
938
533
    EB_DELETE(enc_handle_ptr->packetization_context_ptr);
939
533
}
940
941
/**********************************
942
* Encoder Library Handle Constructor
943
**********************************/
944
533
static EbErrorType svt_enc_handle_ctor(EbEncHandle* enc_handle_ptr, EbComponentType* ebHandlePtr) {
945
533
    enc_handle_ptr->dctor = svt_enc_handle_dctor;
946
947
    // Initialize Callbacks
948
533
    EB_MALLOC_OBJECT(enc_handle_ptr->app_callback_ptr);
949
533
    enc_handle_ptr->app_callback_ptr->error_handler = lib_svt_encoder_send_error_exit;
950
533
    enc_handle_ptr->app_callback_ptr->handle        = ebHandlePtr;
951
952
    // Config Set Count
953
533
    enc_handle_ptr->scs_pool_total_count = EB_SequenceControlSetPoolInitCount;
954
    // Initialize Sequence Control Set Instance
955
533
    EB_NEW(enc_handle_ptr->scs_instance, svt_sequence_control_set_instance_ctor);
956
957
533
    enc_handle_ptr->eos_received   = false;
958
533
    enc_handle_ptr->eos_sent       = false;
959
533
    enc_handle_ptr->frame_received = false;
960
533
    enc_handle_ptr->is_prev_valid  = true;
961
533
    return EB_ErrorNone;
962
533
}
963
964
EbErrorType svt_input_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr);
965
966
EbErrorType svt_output_recon_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr);
967
968
EbErrorType svt_overlay_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr);
969
970
EbErrorType svt_output_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr);
971
972
void svt_input_buffer_header_destroyer(EbPtr p);
973
void svt_output_recon_buffer_header_destroyer(EbPtr p);
974
void svt_output_buffer_header_destroyer(EbPtr p);
975
976
EbErrorType svt_input_y8b_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr);
977
void        svt_input_y8b_destroyer(EbPtr p);
978
979
2.13k
static EbErrorType in_cmd_ctor(InputCommand* context_ptr, EbPtr object_init_data_ptr) {
980
2.13k
    (void)context_ptr;
981
2.13k
    (void)object_init_data_ptr;
982
983
2.13k
    return EB_ErrorNone;
984
2.13k
}
985
986
/*
987
* Input Command Constructor
988
*/
989
2.13k
EbErrorType svt_input_cmd_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
990
2.13k
    InputCommand* obj;
991
992
2.13k
    *object_dbl_ptr = NULL;
993
2.13k
    EB_NEW(obj, in_cmd_ctor, object_init_data_ptr);
994
2.13k
    *object_dbl_ptr = obj;
995
996
2.13k
    return EB_ErrorNone;
997
2.13k
}
998
999
533
static EbErrorType dlf_results_ctor(DlfResults* context_ptr, EbPtr object_init_data_ptr) {
1000
533
    (void)context_ptr;
1001
533
    (void)object_init_data_ptr;
1002
1003
533
    return EB_ErrorNone;
1004
533
}
1005
1006
533
static EbErrorType dlf_results_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1007
533
    DlfResults* obj;
1008
1009
533
    *object_dbl_ptr = NULL;
1010
533
    EB_NEW(obj, dlf_results_ctor, object_init_data_ptr);
1011
533
    *object_dbl_ptr = obj;
1012
1013
533
    return EB_ErrorNone;
1014
533
}
1015
1016
/*
1017
   TPL results ctor
1018
*/
1019
22.0k
EbErrorType tpl_disp_results_ctor(TplDispResults* context_ptr, EbPtr object_init_data_ptr) {
1020
22.0k
    (void)context_ptr;
1021
22.0k
    (void)object_init_data_ptr;
1022
1023
22.0k
    return EB_ErrorNone;
1024
22.0k
}
1025
1026
/*
1027
   TPL results creator
1028
*/
1029
22.0k
static EbErrorType tpl_disp_results_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1030
22.0k
    TplDispResults* obj;
1031
1032
22.0k
    *object_dbl_ptr = NULL;
1033
22.0k
    EB_NEW(obj, tpl_disp_results_ctor, object_init_data_ptr);
1034
22.0k
    *object_dbl_ptr = obj;
1035
1036
22.0k
    return EB_ErrorNone;
1037
22.0k
}
1038
1039
533
static EbErrorType cdef_results_ctor(CdefResults* context_ptr, EbPtr object_init_data_ptr) {
1040
533
    (void)context_ptr;
1041
533
    (void)object_init_data_ptr;
1042
1043
533
    return EB_ErrorNone;
1044
533
}
1045
1046
533
static EbErrorType cdef_results_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1047
533
    CdefResults* obj;
1048
1049
533
    *object_dbl_ptr = NULL;
1050
533
    EB_NEW(obj, cdef_results_ctor, object_init_data_ptr);
1051
533
    *object_dbl_ptr = obj;
1052
1053
533
    return EB_ErrorNone;
1054
533
}
1055
1056
4.79k
EbErrorType rest_results_ctor(RestResults* context_ptr, EbPtr object_init_data_ptr) {
1057
4.79k
    (void)context_ptr;
1058
4.79k
    (void)object_init_data_ptr;
1059
1060
4.79k
    return EB_ErrorNone;
1061
4.79k
}
1062
1063
4.79k
static EbErrorType rest_results_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1064
4.79k
    RestResults* obj;
1065
1066
4.79k
    *object_dbl_ptr = NULL;
1067
4.79k
    EB_NEW(obj, rest_results_ctor, object_init_data_ptr);
1068
4.79k
    *object_dbl_ptr = obj;
1069
1070
4.79k
    return EB_ErrorNone;
1071
4.79k
}
1072
1073
533
static int create_pa_ref_buf_descs(EbEncHandle* enc_handle_ptr) {
1074
533
    SequenceControlSet*             scs = enc_handle_ptr->scs_instance->scs;
1075
533
    EbPaReferenceObjectDescInitData eb_pa_ref_obj_ect_desc_init_data_structure;
1076
533
    EbPictureBufferDescInitData     ref_pic_buf_desc_init_data;
1077
533
    EbPictureBufferDescInitData     quart_pic_buf_desc_init_data;
1078
533
    EbPictureBufferDescInitData     sixteenth_pic_buf_desc_init_data;
1079
533
    const bool                      allintra = scs->allintra;
1080
    // PA Reference Picture Buffers
1081
    // Currently, only Luma samples are needed in the PA
1082
533
    ref_pic_buf_desc_init_data.max_width    = scs->max_input_luma_width;
1083
533
    ref_pic_buf_desc_init_data.max_height   = scs->max_input_luma_height;
1084
533
    ref_pic_buf_desc_init_data.bit_depth    = EB_EIGHT_BIT;
1085
533
    ref_pic_buf_desc_init_data.color_format = EB_YUV420; //use 420 for picture analysis
1086
    //No full-resolution pixel data is allocated for PA REF,
1087
    // it points directly to the Luma input samples of the app data
1088
533
    ref_pic_buf_desc_init_data.buffer_enable_mask = 0;
1089
1090
533
    ref_pic_buf_desc_init_data.border              = scs->border;
1091
533
    ref_pic_buf_desc_init_data.split_mode          = false;
1092
533
    ref_pic_buf_desc_init_data.rest_units_per_tile = scs->rest_units_per_tile;
1093
533
    ref_pic_buf_desc_init_data.mfmv                = 0;
1094
533
    ref_pic_buf_desc_init_data.is_16bit_pipeline   = false;
1095
533
    ref_pic_buf_desc_init_data.sb_total_count      = scs->sb_total_count;
1096
1097
533
    quart_pic_buf_desc_init_data.max_width           = scs->max_input_luma_width >> 1;
1098
533
    quart_pic_buf_desc_init_data.max_height          = scs->max_input_luma_height >> 1;
1099
533
    quart_pic_buf_desc_init_data.bit_depth           = EB_EIGHT_BIT;
1100
533
    quart_pic_buf_desc_init_data.color_format        = EB_YUV420;
1101
533
    quart_pic_buf_desc_init_data.buffer_enable_mask  = allintra ? 0 : PICTURE_BUFFER_DESC_LUMA_MASK;
1102
533
    quart_pic_buf_desc_init_data.border              = scs->b64_size >> 1;
1103
533
    quart_pic_buf_desc_init_data.split_mode          = false;
1104
533
    quart_pic_buf_desc_init_data.rest_units_per_tile = scs->rest_units_per_tile;
1105
533
    quart_pic_buf_desc_init_data.mfmv                = 0;
1106
533
    quart_pic_buf_desc_init_data.is_16bit_pipeline   = false;
1107
533
    quart_pic_buf_desc_init_data.sb_total_count      = scs->sb_total_count;
1108
1109
533
    sixteenth_pic_buf_desc_init_data.max_width           = scs->max_input_luma_width >> 2;
1110
533
    sixteenth_pic_buf_desc_init_data.max_height          = scs->max_input_luma_height >> 2;
1111
533
    sixteenth_pic_buf_desc_init_data.bit_depth           = EB_EIGHT_BIT;
1112
533
    sixteenth_pic_buf_desc_init_data.color_format        = EB_YUV420;
1113
533
    sixteenth_pic_buf_desc_init_data.buffer_enable_mask  = allintra ? 0 : PICTURE_BUFFER_DESC_LUMA_MASK;
1114
533
    sixteenth_pic_buf_desc_init_data.border              = scs->b64_size >> 2;
1115
533
    sixteenth_pic_buf_desc_init_data.split_mode          = false;
1116
533
    sixteenth_pic_buf_desc_init_data.rest_units_per_tile = scs->rest_units_per_tile;
1117
533
    sixteenth_pic_buf_desc_init_data.mfmv                = 0;
1118
533
    sixteenth_pic_buf_desc_init_data.is_16bit_pipeline   = false;
1119
533
    sixteenth_pic_buf_desc_init_data.sb_total_count      = scs->sb_total_count;
1120
1121
533
    eb_pa_ref_obj_ect_desc_init_data_structure.reference_picture_desc_init_data = ref_pic_buf_desc_init_data;
1122
533
    eb_pa_ref_obj_ect_desc_init_data_structure.quarter_picture_desc_init_data   = quart_pic_buf_desc_init_data;
1123
533
    eb_pa_ref_obj_ect_desc_init_data_structure.sixteenth_picture_desc_init_data = sixteenth_pic_buf_desc_init_data;
1124
    // Reference Picture Buffers
1125
533
    EB_NEW(enc_handle_ptr->pa_reference_picture_pool_ptr,
1126
533
           svt_system_resource_ctor,
1127
533
           scs->pa_reference_picture_buffer_init_count,
1128
533
           EB_PictureDecisionProcessInitCount,
1129
533
           0,
1130
533
           svt_pa_reference_object_creator,
1131
533
           &(eb_pa_ref_obj_ect_desc_init_data_structure),
1132
533
           NULL,
1133
533
           (scs->lp == 1));
1134
    // Set the SequenceControlSet Picture Pool Fifo Ptrs
1135
533
    enc_handle_ptr->scs_instance->enc_ctx->pa_reference_picture_pool_fifo_ptr = svt_system_resource_get_producer_fifo(
1136
533
        enc_handle_ptr->pa_reference_picture_pool_ptr, 0);
1137
#if SRM_REPORT
1138
    enc_handle_ptr->scs_instance->enc_ctx->pa_reference_picture_pool_fifo_ptr->queue_ptr->log = 0;
1139
#endif
1140
533
    return 0;
1141
533
}
1142
1143
0
static int create_tpl_ref_buf_descs(EbEncHandle* enc_handle_ptr) {
1144
0
    SequenceControlSet*              scs = enc_handle_ptr->scs_instance->scs;
1145
0
    EbTplReferenceObjectDescInitData eb_tpl_ref_obj_ect_desc_init_data_structure;
1146
0
    EbPictureBufferDescInitData      ref_pic_buf_desc_init_data;
1147
    // PA Reference Picture Buffers
1148
    // Currently, only Luma samples are needed in the PA
1149
0
    ref_pic_buf_desc_init_data.max_width    = scs->max_input_luma_width;
1150
0
    ref_pic_buf_desc_init_data.max_height   = scs->max_input_luma_height;
1151
0
    ref_pic_buf_desc_init_data.bit_depth    = EB_EIGHT_BIT;
1152
0
    ref_pic_buf_desc_init_data.color_format = EB_YUV420; //use 420 for picture analysis
1153
1154
    // Allocate one ref pic to be used in TPL
1155
0
    ref_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_Y_FLAG;
1156
1157
0
    ref_pic_buf_desc_init_data.border            = TPL_PAD;
1158
0
    ref_pic_buf_desc_init_data.split_mode        = false;
1159
0
    ref_pic_buf_desc_init_data.mfmv              = 0;
1160
0
    ref_pic_buf_desc_init_data.is_16bit_pipeline = false;
1161
1162
0
    ref_pic_buf_desc_init_data.rest_units_per_tile = 0; // rest not needed in tpl scs->rest_units_per_tile;
1163
0
    ref_pic_buf_desc_init_data.sb_total_count      = scs->sb_total_count;
1164
1165
0
    eb_tpl_ref_obj_ect_desc_init_data_structure.reference_picture_desc_init_data = ref_pic_buf_desc_init_data;
1166
1167
    // Reference Picture Buffers
1168
0
    EB_NEW(enc_handle_ptr->tpl_reference_picture_pool_ptr,
1169
0
           svt_system_resource_ctor,
1170
0
           scs->tpl_reference_picture_buffer_init_count,
1171
0
           EB_PictureDecisionProcessInitCount,
1172
0
           0,
1173
0
           svt_tpl_reference_object_creator,
1174
0
           &(eb_tpl_ref_obj_ect_desc_init_data_structure),
1175
0
           NULL,
1176
0
           (scs->lp == 1));
1177
    // Set the SequenceControlSet Picture Pool Fifo Ptrs
1178
0
    enc_handle_ptr->scs_instance->enc_ctx->tpl_reference_picture_pool_fifo_ptr = svt_system_resource_get_producer_fifo(
1179
0
        enc_handle_ptr->tpl_reference_picture_pool_ptr, 0);
1180
#if SRM_REPORT
1181
    enc_handle_ptr->scs_instance->enc_ctx->tpl_reference_picture_pool_fifo_ptr->queue_ptr->log = 0;
1182
#endif
1183
0
    return 0;
1184
0
}
1185
1186
533
static int create_ref_buf_descs(EbEncHandle* enc_handle_ptr) {
1187
533
    EbReferenceObjectDescInitData eb_ref_obj_ect_desc_init_data_structure;
1188
533
    EbPictureBufferDescInitData   ref_pic_buf_desc_init_data;
1189
533
    SequenceControlSet*           scs      = enc_handle_ptr->scs_instance->scs;
1190
533
    bool                          is_16bit = scs->static_config.encoder_bit_depth > EB_EIGHT_BIT;
1191
    // Initialize the various Picture types
1192
533
    ref_pic_buf_desc_init_data.max_width           = scs->max_input_luma_width;
1193
533
    ref_pic_buf_desc_init_data.max_height          = scs->max_input_luma_height;
1194
533
    ref_pic_buf_desc_init_data.bit_depth           = scs->encoder_bit_depth;
1195
533
    ref_pic_buf_desc_init_data.color_format        = scs->static_config.encoder_color_format;
1196
533
    ref_pic_buf_desc_init_data.buffer_enable_mask  = PICTURE_BUFFER_DESC_FULL_MASK;
1197
533
    ref_pic_buf_desc_init_data.rest_units_per_tile = scs->rest_units_per_tile;
1198
533
    ref_pic_buf_desc_init_data.sb_total_count      = scs->b64_total_count;
1199
533
    uint16_t padding                               = scs->super_block_size + 32;
1200
533
    if (scs->static_config.superres_mode > SUPERRES_NONE || scs->static_config.resize_mode > RESIZE_NONE) {
1201
0
        padding += scs->super_block_size;
1202
0
    }
1203
1204
533
    ref_pic_buf_desc_init_data.border            = padding;
1205
533
    ref_pic_buf_desc_init_data.mfmv              = scs->mfmv_enabled;
1206
533
    ref_pic_buf_desc_init_data.is_16bit_pipeline = scs->is_16bit_pipeline;
1207
    // Hsan: split_mode is set @ eb_reference_object_ctor() as both unpacked reference and packed reference are needed for a 10BIT input; unpacked reference @ MD, and packed reference @ EP
1208
1209
533
    ref_pic_buf_desc_init_data.split_mode = false;
1210
533
    if (is_16bit) {
1211
0
        ref_pic_buf_desc_init_data.bit_depth = EB_TEN_BIT;
1212
0
    }
1213
1214
533
    eb_ref_obj_ect_desc_init_data_structure.reference_picture_desc_init_data = ref_pic_buf_desc_init_data;
1215
533
    eb_ref_obj_ect_desc_init_data_structure.hbd_md                           = scs->enable_hbd_mode_decision;
1216
533
    eb_ref_obj_ect_desc_init_data_structure.static_config                    = &scs->static_config;
1217
    // Reference Picture Buffers
1218
533
    EB_NEW(enc_handle_ptr->reference_picture_pool_ptr,
1219
533
           svt_system_resource_ctor,
1220
533
           scs->reference_picture_buffer_init_count,
1221
533
           EB_PictureManagerProcessInitCount,
1222
533
           0,
1223
533
           svt_reference_object_creator,
1224
533
           &(eb_ref_obj_ect_desc_init_data_structure),
1225
533
           NULL,
1226
533
           (scs->lp == 1));
1227
1228
    // Create reference list for Picture Manager
1229
    // When decode-order is not enforced at pic mgr, each reference picture must have an allocated reference buffer (for at least one mini-gop) so the
1230
    // list can be enough to hold only the reference buffers.  When decode-order is enforced, only 9 reference buffers are used, so the list must be at least 1 mini-gop
1231
    // otherwise ref_buffer_available_semaphore will block all required pics from being passed to pic mgr.
1232
533
    const uint32_t ref_pic_list_length = scs->enable_dec_order ? scs->pa_reference_picture_buffer_init_count
1233
533
                                                               : scs->reference_picture_buffer_init_count;
1234
533
    enc_handle_ptr->scs_instance->enc_ctx->ref_pic_list_length = ref_pic_list_length;
1235
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->scs_instance->enc_ctx->ref_pic_list, ref_pic_list_length);
1236
1237
1.59k
    for (uint32_t idx = 0; idx < ref_pic_list_length; ++idx) {
1238
1.06k
        EB_NEW(enc_handle_ptr->scs_instance->enc_ctx->ref_pic_list[idx], svt_aom_reference_queue_entry_ctor);
1239
1.06k
    }
1240
533
#if CONFIG_SINGLE_THREAD_KERNEL
1241
533
    if (scs->lp > 1)
1242
533
#endif
1243
533
    {
1244
533
        EB_CREATE_SEMAPHORE(scs->ref_buffer_available_semaphore, ref_pic_list_length, ref_pic_list_length);
1245
533
    }
1246
533
    enc_handle_ptr->scs_instance->enc_ctx->reference_picture_pool_fifo_ptr = svt_system_resource_get_producer_fifo(
1247
533
        enc_handle_ptr->reference_picture_pool_ptr, 0);
1248
1249
#if SRM_REPORT
1250
    enc_handle_ptr->scs_instance->enc_ctx->reference_picture_pool_fifo_ptr->queue_ptr->log = 0;
1251
#endif
1252
1253
533
    return 0;
1254
533
}
1255
1256
void init_fn_ptr(void);
1257
void svt_av1_init_wedge_masks(void);
1258
void init_ii_masks(void);
1259
1260
1
static ONCE_ROUTINE(init_global_tables) {
1261
1
    svt_aom_asm_set_convolve_asm_table();
1262
1
    svt_aom_init_intra_dc_predictors_c_internal();
1263
1
    svt_aom_asm_set_convolve_hbd_asm_table();
1264
1
    svt_aom_init_intra_predictors_internal();
1265
1
    svt_av1_init_me_luts();
1266
1
    init_fn_ptr();
1267
1
    svt_av1_init_wedge_masks();
1268
1
    init_ii_masks();
1269
1
    ONCE_ROUTINE_EPILOG;
1270
1
}
1271
1272
DEFINE_ONCE(global_tables_once);
1273
1274
/**********************************
1275
* Initialize Encoder Library
1276
**********************************/
1277
533
EB_API EbErrorType svt_av1_enc_init(EbComponentType* svt_enc_component) {
1278
533
    if (svt_enc_component == NULL) {
1279
0
        return EB_ErrorBadParameter;
1280
0
    }
1281
533
    EbEncHandle*        enc_handle_ptr = (EbEncHandle*)svt_enc_component->p_component_private;
1282
533
    EbErrorType         return_error   = EB_ErrorNone;
1283
533
    SequenceControlSet* scs            = enc_handle_ptr->scs_instance->scs;
1284
533
    EbColorFormat       color_format   = scs->static_config.encoder_color_format;
1285
533
    const bool          single_thread  = (scs->lp == 1);
1286
1287
533
    svt_aom_setup_common_rtcd_internal(scs->static_config.use_cpu_flags);
1288
533
    svt_aom_setup_rtcd_internal(scs->static_config.use_cpu_flags);
1289
533
    svt_run_once(&global_tables_once, init_global_tables);
1290
1291
    // Per-instance block geometry table allocation
1292
533
    EB_MALLOC_ARRAY(scs->blk_geom_mds, scs->max_block_cnt);
1293
533
    svt_aom_build_blk_geom(scs->svt_aom_geom_idx, scs->blk_geom_mds);
1294
    /************************************
1295
     * Sequence Control Set
1296
     ************************************/
1297
533
    EB_NEW(enc_handle_ptr->scs_pool_ptr,
1298
533
           svt_system_resource_ctor,
1299
533
           enc_handle_ptr->scs_pool_total_count,
1300
533
           1,
1301
533
           0,
1302
533
           svt_aom_scs_set_creator,
1303
533
           NULL,
1304
533
           NULL,
1305
533
           single_thread);
1306
    /************************************
1307
    * Picture Control Set: Parent
1308
    ************************************/
1309
533
    {
1310
        // The segment Width & Height Arrays are in units of SBs, not samples
1311
533
        PictureControlSetInitData input_data;
1312
533
        input_data.picture_width        = scs->max_input_luma_width;
1313
533
        input_data.picture_height       = scs->max_input_luma_height;
1314
533
        input_data.border               = scs->border;
1315
533
        input_data.color_format         = color_format;
1316
533
        input_data.b64_size             = scs->b64_size;
1317
533
        input_data.enc_mode             = scs->static_config.enc_mode;
1318
533
        input_data.hbd_md               = scs->enable_hbd_mode_decision;
1319
533
        input_data.bit_depth            = scs->static_config.encoder_bit_depth;
1320
533
        input_data.log2_tile_rows       = scs->static_config.tile_rows;
1321
533
        input_data.log2_tile_cols       = scs->static_config.tile_columns;
1322
533
        input_data.log2_sb_size         = (scs->super_block_size == 128) ? 5 : 4;
1323
533
        input_data.is_16bit_pipeline    = scs->is_16bit_pipeline;
1324
533
        input_data.non_m8_pad_w         = scs->max_input_pad_right;
1325
533
        input_data.non_m8_pad_h         = scs->max_input_pad_bottom;
1326
533
        input_data.enable_tpl_la        = scs->tpl;
1327
533
        input_data.enc_dec_segment_col  = (uint16_t)scs->tpl_segment_col_count_array;
1328
533
        input_data.enc_dec_segment_row  = (uint16_t)scs->tpl_segment_row_count_array;
1329
533
        MrpCtrls* mrp_ctrl              = &(scs->mrp_ctrls);
1330
533
#if TUNE_SIMPLIFY_SETTINGS
1331
533
        input_data.ref_count_used_list0 = MAX(mrp_ctrl->base_ref_list0_count, mrp_ctrl->non_base_ref_list0_count);
1332
533
        input_data.ref_count_used_list1 = MAX(mrp_ctrl->base_ref_list1_count, mrp_ctrl->non_base_ref_list1_count);
1333
#else
1334
        input_data.ref_count_used_list0 = MAX(
1335
            mrp_ctrl->sc_base_ref_list0_count,
1336
            MAX(mrp_ctrl->base_ref_list0_count,
1337
                MAX(mrp_ctrl->sc_non_base_ref_list0_count, mrp_ctrl->non_base_ref_list0_count)));
1338
        input_data.ref_count_used_list1 = MAX(
1339
            mrp_ctrl->sc_base_ref_list1_count,
1340
            MAX(mrp_ctrl->base_ref_list1_count,
1341
                MAX(mrp_ctrl->sc_non_base_ref_list1_count, mrp_ctrl->non_base_ref_list1_count)));
1342
#endif
1343
533
        input_data.tpl_synth_size = svt_aom_set_tpl_group(NULL,
1344
533
                                                          svt_aom_get_tpl_group_level(1, scs->static_config.enc_mode),
1345
533
                                                          input_data.picture_width,
1346
533
                                                          input_data.picture_height);
1347
533
        input_data.aq_mode        = scs->static_config.aq_mode;
1348
1349
533
        input_data.calculate_variance = scs->calculate_variance;
1350
533
        input_data.calc_hist = scs->calc_hist = scs->allintra == false &&
1351
0
            (scs->static_config.scene_change_detection || scs->vq_ctrls.sharpness_ctrls.scene_transition ||
1352
0
             scs->tf_params_per_type[0].enabled || scs->tf_params_per_type[1].enabled ||
1353
0
             scs->tf_params_per_type[2].enabled);
1354
533
        input_data.tpl_lad_mg       = scs->tpl_lad_mg;
1355
533
        input_data.input_resolution = scs->input_resolution;
1356
533
        input_data.is_scale         = scs->static_config.superres_mode > SUPERRES_NONE ||
1357
533
            scs->static_config.resize_mode > RESIZE_NONE;
1358
533
        input_data.rtc_tune            = scs->static_config.rtc;
1359
533
        input_data.variance_octile     = scs->static_config.variance_octile;
1360
533
        input_data.adaptive_film_grain = scs->static_config.adaptive_film_grain;
1361
533
        input_data.hbd_mds             = scs->static_config.hbd_mds;
1362
533
        input_data.static_config       = scs->static_config;
1363
533
        input_data.allintra            = scs->allintra;
1364
533
        input_data.use_flat_ipp        = scs->use_flat_ipp;
1365
533
        EB_NEW(enc_handle_ptr->picture_parent_control_set_pool_ptr,
1366
533
               svt_system_resource_ctor,
1367
533
               scs->picture_control_set_pool_init_count, //enc_handle_ptr->pcs_pool_total_count,
1368
533
               1,
1369
533
               0,
1370
533
               svt_aom_picture_parent_control_set_creator,
1371
533
               &input_data,
1372
533
               NULL,
1373
533
               single_thread);
1374
#if SRM_REPORT
1375
        enc_handle_ptr->picture_parent_control_set_pool_ptr->empty_queue->log = 0;
1376
#endif
1377
533
        EB_NEW(enc_handle_ptr->me_pool_ptr,
1378
533
               svt_system_resource_ctor,
1379
533
               scs->me_pool_init_count,
1380
533
               1,
1381
533
               0,
1382
533
               svt_aom_me_creator,
1383
533
               &input_data,
1384
533
               NULL,
1385
533
               single_thread);
1386
#if SRM_REPORT
1387
        enc_handle_ptr->me_pool_ptr->empty_queue->log = 0;
1388
        dump_srm_content(enc_handle_ptr->me_pool_ptr, false);
1389
#endif
1390
533
    }
1391
1392
    /************************************
1393
    * Enc Dec
1394
    ************************************/
1395
533
    {
1396
        // The segment Width & Height Arrays are in units of SBs, not samples
1397
533
        PictureControlSetInitData input_data;
1398
533
        input_data.enc_dec_segment_col = (uint16_t)scs->enc_dec_segment_col_count_array;
1399
533
        input_data.enc_dec_segment_row = (uint16_t)scs->enc_dec_segment_row_count_array;
1400
1401
533
        input_data.picture_width  = scs->max_input_luma_width;
1402
533
        input_data.picture_height = scs->max_input_luma_height;
1403
533
        input_data.border         = scs->border;
1404
533
        input_data.bit_depth      = scs->encoder_bit_depth;
1405
533
        input_data.color_format   = color_format;
1406
533
        input_data.b64_size       = scs->b64_size;
1407
533
        input_data.sb_size        = scs->super_block_size;
1408
533
        input_data.hbd_md         = scs->enable_hbd_mode_decision;
1409
533
        input_data.mfmv           = scs->mfmv_enabled;
1410
        //Jing: Get tile info from parent_pcs
1411
533
        PictureParentControlSet* parent_pcs =
1412
533
            (PictureParentControlSet*)enc_handle_ptr->picture_parent_control_set_pool_ptr->wrapper_ptr_pool[0]
1413
533
                ->object_ptr;
1414
533
        input_data.tile_row_count    = parent_pcs->av1_cm->tiles_info.tile_rows;
1415
533
        input_data.tile_column_count = parent_pcs->av1_cm->tiles_info.tile_cols;
1416
533
        input_data.is_16bit_pipeline = scs->is_16bit_pipeline;
1417
533
        input_data.av1_cm            = parent_pcs->av1_cm;
1418
533
        input_data.enc_mode          = scs->static_config.enc_mode;
1419
1420
533
        input_data.input_resolution = scs->input_resolution;
1421
533
        input_data.is_scale         = scs->static_config.superres_mode > SUPERRES_NONE ||
1422
533
            scs->static_config.resize_mode > RESIZE_NONE;
1423
1424
533
        input_data.rtc_tune     = scs->static_config.rtc;
1425
533
        input_data.allintra     = scs->allintra;
1426
533
        input_data.use_flat_ipp = scs->use_flat_ipp;
1427
533
        EB_NEW(enc_handle_ptr->enc_dec_pool_ptr,
1428
533
               svt_system_resource_ctor,
1429
533
               scs->enc_dec_pool_init_count, //EB_PictureControlSetPoolInitCountChild,
1430
533
               1,
1431
533
               0,
1432
533
               svt_aom_recon_coef_creator,
1433
533
               &input_data,
1434
533
               NULL,
1435
533
               single_thread);
1436
533
    }
1437
1438
    /************************************
1439
        * Picture Control Set: Child
1440
        ************************************/
1441
533
    {
1442
        // The segment Width & Height Arrays are in units of SBs, not samples
1443
533
        PictureControlSetInitData input_data;
1444
533
        input_data.enc_dec_segment_col = (uint16_t)scs->enc_dec_segment_col_count_array;
1445
533
        input_data.enc_dec_segment_row = (uint16_t)scs->enc_dec_segment_row_count_array;
1446
533
        input_data.picture_width       = scs->max_input_luma_width;
1447
533
        input_data.picture_height      = scs->max_input_luma_height;
1448
533
        input_data.border              = scs->border;
1449
533
        input_data.bit_depth           = scs->encoder_bit_depth;
1450
533
        input_data.color_format        = color_format;
1451
533
        input_data.b64_size            = scs->b64_size;
1452
533
        input_data.sb_size             = scs->super_block_size;
1453
533
        input_data.hbd_md              = scs->enable_hbd_mode_decision;
1454
533
        input_data.mfmv                = scs->mfmv_enabled;
1455
        //Jing: Get tile info from parent_pcs
1456
533
        PictureParentControlSet* parent_pcs =
1457
533
            (PictureParentControlSet*)enc_handle_ptr->picture_parent_control_set_pool_ptr->wrapper_ptr_pool[0]
1458
533
                ->object_ptr;
1459
533
        input_data.tile_row_count    = parent_pcs->av1_cm->tiles_info.tile_rows;
1460
533
        input_data.tile_column_count = parent_pcs->av1_cm->tiles_info.tile_cols;
1461
533
        input_data.is_16bit_pipeline = scs->is_16bit_pipeline;
1462
533
        input_data.av1_cm            = parent_pcs->av1_cm;
1463
533
        input_data.enc_mode          = scs->static_config.enc_mode;
1464
533
        input_data.static_config     = scs->static_config;
1465
1466
533
        input_data.input_resolution = scs->input_resolution;
1467
533
        input_data.is_scale         = scs->static_config.superres_mode > SUPERRES_NONE ||
1468
533
            scs->static_config.resize_mode > RESIZE_NONE;
1469
1470
533
        input_data.rtc_tune     = scs->static_config.rtc;
1471
533
        input_data.allintra     = scs->allintra;
1472
533
        input_data.use_flat_ipp = scs->use_flat_ipp;
1473
533
        EB_NEW(enc_handle_ptr->picture_control_set_pool_ptr,
1474
533
               svt_system_resource_ctor,
1475
533
               scs->picture_control_set_pool_init_count_child, //EB_PictureControlSetPoolInitCountChild,
1476
533
               1,
1477
533
               0,
1478
533
               svt_aom_picture_control_set_creator,
1479
533
               &input_data,
1480
533
               NULL,
1481
533
               single_thread);
1482
533
    }
1483
1484
    /************************************
1485
    * Picture Buffers
1486
    ************************************/
1487
    // Allocate Resource Arrays
1488
533
    pic_mgr_ports[PIC_MGR_INPUT_PORT_SOP].count           = scs->source_based_operations_process_init_count;
1489
533
    pic_mgr_ports[PIC_MGR_INPUT_PORT_PACKETIZATION].count = EB_PacketizationProcessInitCount;
1490
533
    pic_mgr_ports[PIC_MGR_INPUT_PORT_REST].count          = scs->rest_process_init_count;
1491
    // Rate Control
1492
533
    rate_control_ports[RATE_CONTROL_INPUT_PORT_INLME].count         = EB_PictureManagerProcessInitCount;
1493
533
    rate_control_ports[RATE_CONTROL_INPUT_PORT_PACKETIZATION].count = EB_PacketizationProcessInitCount;
1494
1495
533
    enc_dec_ports[ENCDEC_INPUT_PORT_MDC].count    = scs->mode_decision_configuration_process_init_count;
1496
533
    enc_dec_ports[ENCDEC_INPUT_PORT_ENCDEC].count = scs->enc_dec_process_init_count;
1497
533
    tpl_ports[TPL_INPUT_PORT_SOP].count           = scs->source_based_operations_process_init_count;
1498
533
    tpl_ports[TPL_INPUT_PORT_TPL].count           = scs->tpl_disp_process_init_count;
1499
533
    {
1500
        // Must always allocate mem b/c don't know if restoration is on or off at this point
1501
        // The restoration assumes only 1 tile is used, so only allocate for 1 tile... see svt_av1_alloc_restoration_struct()
1502
533
        PictureControlSet* pcs =
1503
533
            (PictureControlSet*)enc_handle_ptr->picture_control_set_pool_ptr->wrapper_ptr_pool[0]->object_ptr;
1504
533
        scs->rest_units_per_tile = pcs->rst_info[0 /*Y-plane*/].units_per_tile;
1505
533
        scs->b64_total_count     = pcs->b64_total_count;
1506
533
        create_ref_buf_descs(enc_handle_ptr);
1507
533
        if (scs->tpl) {
1508
0
            create_tpl_ref_buf_descs(enc_handle_ptr);
1509
0
        }
1510
1511
533
        create_pa_ref_buf_descs(enc_handle_ptr);
1512
1513
533
        if (scs->static_config.enable_overlays) {
1514
            // Overlay Input Picture Buffers
1515
0
            EB_NEW(enc_handle_ptr->overlay_input_picture_pool_ptr,
1516
0
                   svt_system_resource_ctor,
1517
0
                   scs->overlay_input_picture_buffer_init_count,
1518
0
                   1,
1519
0
                   0,
1520
0
                   svt_overlay_buffer_header_creator,
1521
0
                   scs,
1522
0
                   svt_input_buffer_header_destroyer,
1523
0
                   single_thread);
1524
            // Set the SequenceControlSet Overlay input Picture Pool Fifo Ptrs
1525
0
            enc_handle_ptr->scs_instance->enc_ctx->overlay_input_picture_pool_fifo_ptr =
1526
0
                svt_system_resource_get_producer_fifo(enc_handle_ptr->overlay_input_picture_pool_ptr, 0);
1527
0
        }
1528
533
    }
1529
    /************************************
1530
    * System Resource Managers & Fifos
1531
    ************************************/
1532
    //SRM to link App to Ress-Coordination via Input commands. an Input Command holds 2 picture buffers: y8bit and rest(uv8b + yuv2b)
1533
533
    EB_NEW(enc_handle_ptr->input_cmd_resource_ptr,
1534
533
           svt_system_resource_ctor,
1535
533
           scs->resource_coordination_fifo_init_count,
1536
533
           1,
1537
533
           EB_ResourceCoordinationProcessInitCount,
1538
533
           svt_input_cmd_creator,
1539
533
           scs,
1540
533
           NULL,
1541
533
           single_thread);
1542
533
    enc_handle_ptr->input_cmd_producer_fifo_ptr = svt_system_resource_get_producer_fifo(
1543
533
        enc_handle_ptr->input_cmd_resource_ptr, 0);
1544
1545
    //Picture Buffer SRM to hold (uv8b + yuv2b)
1546
533
    EB_NEW(enc_handle_ptr->input_buffer_resource_ptr,
1547
533
           svt_system_resource_ctor,
1548
533
           scs->input_buffer_fifo_init_count,
1549
533
           1,
1550
533
           0, //1/2 SRM; no consumer FIFO
1551
533
           svt_input_buffer_header_creator,
1552
533
           scs,
1553
533
           svt_input_buffer_header_destroyer,
1554
533
           single_thread);
1555
533
    enc_handle_ptr->input_buffer_producer_fifo_ptr = svt_system_resource_get_producer_fifo(
1556
533
        enc_handle_ptr->input_buffer_resource_ptr, 0);
1557
1558
    //Picture Buffer SRM to hold y8b to be shared by Pcs->enhanced and Pa_ref
1559
533
    EB_NEW(enc_handle_ptr->input_y8b_buffer_resource_ptr,
1560
533
           svt_system_resource_ctor,
1561
533
           MAX(scs->input_buffer_fifo_init_count, scs->pa_reference_picture_buffer_init_count),
1562
533
           1,
1563
533
           0, //1/2 SRM; no consumer FIFO
1564
533
           svt_input_y8b_creator,
1565
533
           scs,
1566
533
           svt_input_y8b_destroyer,
1567
533
           single_thread);
1568
1569
#if SRM_REPORT
1570
    enc_handle_ptr->input_y8b_buffer_resource_ptr->empty_queue->log = 1;
1571
#endif
1572
533
    enc_handle_ptr->input_y8b_buffer_producer_fifo_ptr = svt_system_resource_get_producer_fifo(
1573
533
        enc_handle_ptr->input_y8b_buffer_resource_ptr, 0);
1574
1575
    // EbBufferHeaderType Output Stream
1576
533
    {
1577
533
        EB_NEW(enc_handle_ptr->output_stream_buffer_resource_ptr,
1578
533
               svt_system_resource_ctor,
1579
533
               scs->output_stream_buffer_fifo_init_count,
1580
533
               scs->total_process_init_count, //EB_PacketizationProcessInitCount,
1581
533
               1,
1582
533
               svt_output_buffer_header_creator,
1583
533
               &scs->static_config,
1584
533
               svt_output_buffer_header_destroyer,
1585
533
               single_thread);
1586
533
    }
1587
533
    enc_handle_ptr->output_stream_buffer_consumer_fifo_ptr = svt_system_resource_get_consumer_fifo(
1588
533
        enc_handle_ptr->output_stream_buffer_resource_ptr, 0);
1589
533
    if (scs->static_config.recon_enabled) {
1590
        // EbBufferHeaderType Output Recon
1591
0
        {
1592
0
            EB_NEW(enc_handle_ptr->output_recon_buffer_resource_ptr,
1593
0
                   svt_system_resource_ctor,
1594
0
                   scs->output_recon_buffer_fifo_init_count,
1595
0
                   scs->enc_dec_process_init_count,
1596
0
                   1,
1597
0
                   svt_output_recon_buffer_header_creator,
1598
0
                   scs,
1599
0
                   svt_output_recon_buffer_header_destroyer,
1600
0
                   single_thread);
1601
0
        }
1602
0
        enc_handle_ptr->output_recon_buffer_consumer_fifo_ptr = svt_system_resource_get_consumer_fifo(
1603
0
            enc_handle_ptr->output_recon_buffer_resource_ptr, 0);
1604
0
    }
1605
1606
    // Resource Coordination Results
1607
533
    {
1608
533
        ResourceCoordinationResultInitData resource_coordination_result_init_data;
1609
533
        EB_NEW(enc_handle_ptr->resource_coordination_results_resource_ptr,
1610
533
               svt_system_resource_ctor,
1611
533
               scs->resource_coordination_fifo_init_count,
1612
533
               EB_ResourceCoordinationProcessInitCount,
1613
533
               scs->picture_analysis_process_init_count,
1614
533
               svt_aom_resource_coordination_result_creator,
1615
533
               &resource_coordination_result_init_data,
1616
533
               NULL,
1617
533
               single_thread);
1618
533
    }
1619
1620
    // Picture Analysis Results
1621
533
    {
1622
533
        PictureAnalysisResultInitData picture_analysis_result_init_data;
1623
533
        EB_NEW(enc_handle_ptr->picture_analysis_results_resource_ptr,
1624
533
               svt_system_resource_ctor,
1625
533
               scs->picture_analysis_fifo_init_count,
1626
533
               scs->picture_analysis_process_init_count,
1627
533
               EB_PictureDecisionProcessInitCount,
1628
533
               svt_aom_picture_analysis_result_creator,
1629
533
               &picture_analysis_result_init_data,
1630
533
               NULL,
1631
533
               single_thread);
1632
533
    }
1633
1634
    // Picture Decision Results
1635
533
    {
1636
533
        PictureDecisionResultInitData picture_decision_result_init_data;
1637
533
        EB_NEW(enc_handle_ptr->picture_decision_results_resource_ptr,
1638
533
               svt_system_resource_ctor,
1639
533
               scs->picture_decision_fifo_init_count,
1640
533
               EB_PictureDecisionProcessInitCount +
1641
533
                   2, // 1 for rate control, another 1 for packetization when superres recoding is on
1642
533
               scs->motion_estimation_process_init_count,
1643
533
               svt_aom_picture_decision_result_creator,
1644
533
               &picture_decision_result_init_data,
1645
533
               NULL,
1646
533
               single_thread);
1647
533
        EB_ALLOC_PTR_ARRAY(scs->enc_ctx->picture_decision_reorder_queue,
1648
533
                           scs->enc_ctx->picture_decision_reorder_queue_size);
1649
1650
2.66k
        for (uint32_t picture_index = 0; picture_index < scs->enc_ctx->picture_decision_reorder_queue_size;
1651
2.13k
             ++picture_index) {
1652
2.13k
            EB_NEW(scs->enc_ctx->picture_decision_reorder_queue[picture_index],
1653
2.13k
                   svt_aom_picture_decision_reorder_entry_ctor,
1654
2.13k
                   picture_index);
1655
2.13k
        }
1656
533
    }
1657
1658
    // Motion Estimation Results
1659
533
    {
1660
533
        MotionEstimationResultsInitData motion_estimation_result_init_data;
1661
533
        EB_NEW(enc_handle_ptr->motion_estimation_results_resource_ptr,
1662
533
               svt_system_resource_ctor,
1663
533
               scs->motion_estimation_fifo_init_count,
1664
533
               scs->motion_estimation_process_init_count,
1665
533
               EB_InitialRateControlProcessInitCount,
1666
533
               svt_aom_motion_estimation_results_creator,
1667
533
               &motion_estimation_result_init_data,
1668
533
               NULL,
1669
533
               single_thread);
1670
533
    }
1671
1672
    // Initial Rate Control Results
1673
533
    {
1674
533
        InitialRateControlResultInitData initial_rate_control_result_init_data;
1675
533
        EB_NEW(enc_handle_ptr->initial_rate_control_results_resource_ptr,
1676
533
               svt_system_resource_ctor,
1677
533
               scs->initial_rate_control_fifo_init_count,
1678
533
               EB_InitialRateControlProcessInitCount,
1679
533
               scs->source_based_operations_process_init_count,
1680
533
               svt_aom_initial_rate_control_results_creator,
1681
533
               &initial_rate_control_result_init_data,
1682
533
               NULL,
1683
533
               single_thread);
1684
533
    }
1685
1686
    // Picture Demux Results
1687
533
    {
1688
533
        PictureResultInitData picture_result_init_data;
1689
533
        EB_NEW(enc_handle_ptr->picture_demux_results_resource_ptr,
1690
533
               svt_system_resource_ctor,
1691
533
               scs->picture_demux_fifo_init_count,
1692
533
               pic_mgr_port_total_count(),
1693
533
               EB_PictureManagerProcessInitCount,
1694
533
               svt_aom_picture_results_creator,
1695
533
               &picture_result_init_data,
1696
533
               NULL,
1697
533
               single_thread);
1698
1699
533
        EB_ALLOC_PTR_ARRAY(scs->enc_ctx->pic_mgr_input_pic_list, scs->enc_ctx->pic_mgr_input_pic_list_size);
1700
1701
2.66k
        for (uint32_t picture_index = 0; picture_index < scs->enc_ctx->pic_mgr_input_pic_list_size; ++picture_index) {
1702
2.13k
            EB_NEW(scs->enc_ctx->pic_mgr_input_pic_list[picture_index], svt_aom_input_queue_entry_ctor);
1703
2.13k
        }
1704
533
    }
1705
1706
    // TPL dispenser Results
1707
533
    {
1708
533
        EntropyCodingResultsInitData tpl_disp_result_init_data;
1709
        //TPL Dispenser tasks
1710
533
        EB_NEW(enc_handle_ptr->tpl_disp_res_srm,
1711
533
               svt_system_resource_ctor,
1712
533
               scs->tpl_disp_fifo_init_count,
1713
533
               tpl_port_total_count(),
1714
533
               scs->tpl_disp_process_init_count,
1715
533
               tpl_disp_results_creator,
1716
533
               &tpl_disp_result_init_data,
1717
533
               NULL,
1718
533
               single_thread);
1719
533
    }
1720
1721
    // Rate Control Tasks
1722
533
    {
1723
533
        RateControlTasksInitData rate_control_tasks_init_data;
1724
533
        EB_NEW(enc_handle_ptr->rate_control_tasks_resource_ptr,
1725
533
               svt_system_resource_ctor,
1726
533
               scs->rate_control_tasks_fifo_init_count,
1727
533
               rate_control_port_total_count(),
1728
533
               EB_RateControlProcessInitCount,
1729
533
               svt_aom_rate_control_tasks_creator,
1730
533
               &rate_control_tasks_init_data,
1731
533
               NULL,
1732
533
               single_thread);
1733
533
    }
1734
1735
    // Rate Control Results
1736
533
    {
1737
533
        RateControlResultsInitData rate_control_result_init_data;
1738
533
        EB_NEW(enc_handle_ptr->rate_control_results_resource_ptr,
1739
533
               svt_system_resource_ctor,
1740
533
               scs->rate_control_fifo_init_count,
1741
533
               EB_RateControlProcessInitCount,
1742
533
               scs->mode_decision_configuration_process_init_count,
1743
533
               svt_aom_rate_control_results_creator,
1744
533
               &rate_control_result_init_data,
1745
533
               NULL,
1746
533
               single_thread);
1747
533
    }
1748
    // EncDec Tasks
1749
533
    {
1750
533
        EncDecTasksInitData mode_decision_result_init_data;
1751
533
        mode_decision_result_init_data.enc_dec_segment_row_count = scs->enc_dec_segment_row_count_array;
1752
533
        EB_NEW(enc_handle_ptr->enc_dec_tasks_resource_ptr,
1753
533
               svt_system_resource_ctor,
1754
533
               scs->mode_decision_configuration_fifo_init_count,
1755
533
               enc_dec_port_total_count(),
1756
533
               scs->enc_dec_process_init_count,
1757
533
               svt_aom_enc_dec_tasks_creator,
1758
533
               &mode_decision_result_init_data,
1759
533
               NULL,
1760
533
               single_thread);
1761
533
    }
1762
1763
    // EncDec Results
1764
533
    {
1765
533
        EncDecResultsInitData enc_dec_result_init_data;
1766
533
        EB_NEW(enc_handle_ptr->enc_dec_results_resource_ptr,
1767
533
               svt_system_resource_ctor,
1768
533
               scs->enc_dec_fifo_init_count,
1769
533
               scs->enc_dec_process_init_count,
1770
533
               scs->dlf_process_init_count,
1771
533
               svt_aom_enc_dec_results_creator,
1772
533
               &enc_dec_result_init_data,
1773
533
               NULL,
1774
533
               single_thread);
1775
533
    }
1776
1777
    //DLF results
1778
533
    {
1779
533
        EntropyCodingResultsInitData delf_result_init_data;
1780
533
        EB_NEW(enc_handle_ptr->dlf_results_resource_ptr,
1781
533
               svt_system_resource_ctor,
1782
533
               scs->dlf_fifo_init_count,
1783
533
               scs->dlf_process_init_count,
1784
533
               scs->cdef_process_init_count,
1785
533
               dlf_results_creator,
1786
533
               &delf_result_init_data,
1787
533
               NULL,
1788
533
               single_thread);
1789
533
    }
1790
    //CDEF results
1791
533
    {
1792
533
        EntropyCodingResultsInitData cdef_result_init_data;
1793
533
        EB_NEW(enc_handle_ptr->cdef_results_resource_ptr,
1794
533
               svt_system_resource_ctor,
1795
533
               scs->cdef_fifo_init_count,
1796
533
               scs->cdef_process_init_count,
1797
533
               scs->rest_process_init_count,
1798
533
               cdef_results_creator,
1799
533
               &cdef_result_init_data,
1800
533
               NULL,
1801
533
               single_thread);
1802
533
    }
1803
    //REST results
1804
533
    {
1805
533
        EntropyCodingResultsInitData rest_result_init_data;
1806
533
        EB_NEW(enc_handle_ptr->rest_results_resource_ptr,
1807
533
               svt_system_resource_ctor,
1808
533
               scs->rest_fifo_init_count,
1809
533
               scs->rest_process_init_count,
1810
533
               scs->entropy_coding_process_init_count,
1811
533
               rest_results_creator,
1812
533
               &rest_result_init_data,
1813
533
               NULL,
1814
533
               single_thread);
1815
533
    }
1816
1817
    // Entropy Coding Results
1818
533
    {
1819
533
        EntropyCodingResultsInitData entropy_coding_results_init_data;
1820
533
        EB_NEW(enc_handle_ptr->entropy_coding_results_resource_ptr,
1821
533
               svt_system_resource_ctor,
1822
533
               scs->entropy_coding_fifo_init_count,
1823
533
               scs->entropy_coding_process_init_count,
1824
533
               EB_PacketizationProcessInitCount,
1825
533
               svt_aom_entropy_coding_results_creator,
1826
533
               &entropy_coding_results_init_data,
1827
533
               NULL,
1828
533
               single_thread);
1829
533
        EB_ALLOC_PTR_ARRAY(scs->enc_ctx->packetization_reorder_queue, scs->enc_ctx->packetization_reorder_queue_size);
1830
1831
2.66k
        for (uint32_t picture_index = 0; picture_index < scs->enc_ctx->packetization_reorder_queue_size;
1832
2.13k
             ++picture_index) {
1833
2.13k
            EB_NEW(scs->enc_ctx->packetization_reorder_queue[picture_index],
1834
2.13k
                   svt_aom_packetization_reorder_entry_ctor,
1835
2.13k
                   picture_index);
1836
2.13k
        }
1837
533
    }
1838
1839
    /************************************
1840
    * App Callbacks
1841
    ************************************/
1842
533
    enc_handle_ptr->scs_instance->enc_ctx->app_callback_ptr = enc_handle_ptr->app_callback_ptr;
1843
    // svt Output Buffer Fifo Ptrs
1844
533
    enc_handle_ptr->scs_instance->enc_ctx->stream_output_fifo_ptr = svt_system_resource_get_producer_fifo(
1845
533
        enc_handle_ptr->output_stream_buffer_resource_ptr, 0);
1846
533
    if (enc_handle_ptr->scs_instance->scs->static_config.recon_enabled) {
1847
0
        enc_handle_ptr->scs_instance->enc_ctx->recon_output_fifo_ptr = svt_system_resource_get_producer_fifo(
1848
0
            enc_handle_ptr->output_recon_buffer_resource_ptr, 0);
1849
0
    }
1850
1851
    /************************************
1852
    * Contexts
1853
    ************************************/
1854
    // Resource Coordination Context
1855
533
    EB_NEW(
1856
533
        enc_handle_ptr->resource_coordination_context_ptr, svt_aom_resource_coordination_context_ctor, enc_handle_ptr);
1857
1858
    // Picture Analysis Context
1859
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->picture_analysis_context_ptr_array, scs->picture_analysis_process_init_count);
1860
2.66k
    for (uint32_t process_index = 0; process_index < scs->picture_analysis_process_init_count; process_index++) {
1861
2.13k
        EB_NEW(enc_handle_ptr->picture_analysis_context_ptr_array[process_index],
1862
2.13k
               svt_aom_picture_analysis_context_ctor,
1863
2.13k
               enc_handle_ptr,
1864
2.13k
               process_index);
1865
2.13k
    }
1866
1867
    // Picture Decision Context
1868
533
    EB_NEW(enc_handle_ptr->picture_decision_context_ptr,
1869
533
           svt_aom_picture_decision_context_ctor,
1870
533
           enc_handle_ptr,
1871
533
           scs->calc_hist);
1872
1873
    // Motion Analysis Context
1874
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->motion_estimation_context_ptr_array, scs->motion_estimation_process_init_count);
1875
3.73k
    for (uint32_t process_index = 0; process_index < scs->motion_estimation_process_init_count; process_index++) {
1876
3.19k
        EB_NEW(enc_handle_ptr->motion_estimation_context_ptr_array[process_index],
1877
3.19k
               svt_aom_motion_estimation_context_ctor,
1878
3.19k
               enc_handle_ptr,
1879
3.19k
               process_index);
1880
3.19k
    }
1881
1882
    // Initial Rate Control Context
1883
533
    EB_NEW(enc_handle_ptr->initial_rate_control_context_ptr,
1884
533
           svt_aom_initial_rate_control_context_ctor,
1885
533
           enc_handle_ptr,
1886
533
           scs->picture_control_set_pool_init_count);
1887
1888
    // Source Based Operations Context
1889
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->source_based_operations_context_ptr_array,
1890
533
                       scs->source_based_operations_process_init_count);
1891
1.06k
    for (uint32_t process_index = 0; process_index < scs->source_based_operations_process_init_count; process_index++) {
1892
533
        EB_NEW(enc_handle_ptr->source_based_operations_context_ptr_array[process_index],
1893
533
               svt_aom_source_based_operations_context_ctor,
1894
533
               enc_handle_ptr,
1895
533
               tpl_port_lookup(TPL_INPUT_PORT_SOP, process_index),
1896
533
               pic_mgr_port_lookup(PIC_MGR_INPUT_PORT_SOP, process_index));
1897
533
    }
1898
1899
    // TPL dispenser
1900
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->tpl_disp_context_ptr_array, scs->tpl_disp_process_init_count);
1901
2.19k
    for (uint32_t process_index = 0; process_index < scs->tpl_disp_process_init_count; process_index++) {
1902
1.66k
        EB_NEW(enc_handle_ptr->tpl_disp_context_ptr_array[process_index],
1903
1.66k
               svt_aom_tpl_disp_context_ctor,
1904
1.66k
               enc_handle_ptr,
1905
1.66k
               process_index,
1906
1.66k
               tpl_port_lookup(TPL_INPUT_PORT_TPL, process_index));
1907
1.66k
    }
1908
1909
    // Picture Manager Context
1910
533
    EB_NEW(enc_handle_ptr->picture_manager_context_ptr,
1911
533
           svt_aom_picture_manager_context_ctor,
1912
533
           enc_handle_ptr,
1913
533
           rate_control_port_lookup(RATE_CONTROL_INPUT_PORT_INLME, 0), //Pic-Mgr uses the first Port
1914
533
           scs->picture_control_set_pool_init_count);
1915
1916
    // Rate Control Context
1917
533
    EB_NEW(enc_handle_ptr->rate_control_context_ptr,
1918
533
           svt_aom_rate_control_context_ctor,
1919
533
           enc_handle_ptr,
1920
533
           EB_PictureDecisionProcessInitCount); // me_port_index
1921
1922
    // Mode Decision Configuration Contexts
1923
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->mode_decision_configuration_context_ptr_array,
1924
533
                       scs->mode_decision_configuration_process_init_count);
1925
1.06k
    for (uint32_t process_index = 0; process_index < scs->mode_decision_configuration_process_init_count;
1926
533
         process_index++) {
1927
533
        EB_NEW(enc_handle_ptr->mode_decision_configuration_context_ptr_array[process_index],
1928
533
               svt_aom_mode_decision_configuration_context_ctor,
1929
533
               enc_handle_ptr,
1930
533
               process_index,
1931
533
               enc_dec_port_lookup(ENCDEC_INPUT_PORT_MDC, process_index));
1932
533
    }
1933
1934
    // EncDec Contexts
1935
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->enc_dec_context_ptr_array, scs->enc_dec_process_init_count);
1936
2.19k
    for (uint32_t process_index = 0; process_index < scs->enc_dec_process_init_count; process_index++) {
1937
1.66k
        EB_NEW(enc_handle_ptr->enc_dec_context_ptr_array[process_index],
1938
1.66k
               svt_aom_enc_dec_context_ctor,
1939
1.66k
               enc_handle_ptr,
1940
1.66k
               process_index,
1941
1.66k
               enc_dec_port_lookup(ENCDEC_INPUT_PORT_ENCDEC, process_index));
1942
1.66k
    }
1943
1944
    // Dlf Contexts
1945
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->dlf_context_ptr_array, scs->dlf_process_init_count);
1946
1.06k
    for (uint32_t process_index = 0; process_index < scs->dlf_process_init_count; process_index++) {
1947
533
        EB_NEW(enc_handle_ptr->dlf_context_ptr_array[process_index],
1948
533
               svt_aom_dlf_context_ctor,
1949
533
               enc_handle_ptr,
1950
533
               process_index);
1951
533
    }
1952
1953
    //CDEF Contexts
1954
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->cdef_context_ptr_array, scs->cdef_process_init_count);
1955
1.06k
    for (uint32_t process_index = 0; process_index < scs->cdef_process_init_count; process_index++) {
1956
533
        EB_NEW(enc_handle_ptr->cdef_context_ptr_array[process_index],
1957
533
               svt_aom_cdef_context_ctor,
1958
533
               enc_handle_ptr,
1959
533
               process_index);
1960
533
    }
1961
1962
    //Rest Contexts
1963
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->rest_context_ptr_array, scs->rest_process_init_count);
1964
1.06k
    for (uint32_t process_index = 0; process_index < scs->rest_process_init_count; process_index++) {
1965
533
        EB_NEW(enc_handle_ptr->rest_context_ptr_array[process_index],
1966
533
               svt_aom_rest_context_ctor,
1967
533
               enc_handle_ptr,
1968
533
               process_index,
1969
533
               pic_mgr_port_lookup(PIC_MGR_INPUT_PORT_REST, process_index));
1970
533
    }
1971
1972
    // Entropy Coding Contexts
1973
533
    EB_ALLOC_PTR_ARRAY(enc_handle_ptr->entropy_coding_context_ptr_array, scs->entropy_coding_process_init_count);
1974
1.06k
    for (uint32_t process_index = 0; process_index < scs->entropy_coding_process_init_count; process_index++) {
1975
533
        EB_NEW(enc_handle_ptr->entropy_coding_context_ptr_array[process_index],
1976
533
               svt_aom_entropy_coding_context_ctor,
1977
533
               enc_handle_ptr,
1978
533
               process_index);
1979
533
    }
1980
1981
    // Packetization Context
1982
533
    EB_NEW(enc_handle_ptr->packetization_context_ptr,
1983
533
           svt_aom_packetization_context_ctor,
1984
533
           enc_handle_ptr,
1985
533
           rate_control_port_lookup(RATE_CONTROL_INPUT_PORT_PACKETIZATION, 0),
1986
533
           pic_mgr_port_lookup(PIC_MGR_INPUT_PORT_PACKETIZATION, 0),
1987
533
           EB_PictureDecisionProcessInitCount + EB_RateControlProcessInitCount); // me_port_index
1988
1989
    /************************************
1990
    * Thread Handles
1991
    ************************************/
1992
533
#if CONFIG_SINGLE_THREAD_KERNEL
1993
    // Single-thread kernel dispatch: at lp=1, register all kernels for
1994
    // cooperative dispatch instead of creating 16 OS threads.
1995
533
    svt_kernel_dispatcher_init(&enc_handle_ptr->kernel_dispatcher);
1996
533
    if (scs->lp == 1) {
1997
0
        enc_handle_ptr->kernel_dispatcher.active = true;
1998
1999
        // Enable non-blocking FIFO mode on all system resources
2000
0
#define SRM_SET_ST(member) \
2001
0
    svt_system_resource_set_single_thread_mode(enc_handle_ptr->member, &enc_handle_ptr->kernel_dispatcher)
2002
0
        SRM_SET_ST(scs_pool_ptr);
2003
0
        SRM_SET_ST(picture_parent_control_set_pool_ptr);
2004
0
        SRM_SET_ST(me_pool_ptr);
2005
0
        SRM_SET_ST(picture_control_set_pool_ptr);
2006
0
        SRM_SET_ST(enc_dec_pool_ptr);
2007
0
        SRM_SET_ST(reference_picture_pool_ptr);
2008
0
        SRM_SET_ST(tpl_reference_picture_pool_ptr);
2009
0
        SRM_SET_ST(pa_reference_picture_pool_ptr);
2010
0
        SRM_SET_ST(overlay_input_picture_pool_ptr);
2011
0
        SRM_SET_ST(input_buffer_resource_ptr);
2012
0
        SRM_SET_ST(input_y8b_buffer_resource_ptr);
2013
0
        SRM_SET_ST(input_cmd_resource_ptr);
2014
0
        SRM_SET_ST(output_stream_buffer_resource_ptr);
2015
0
        if (enc_handle_ptr->output_recon_buffer_resource_ptr) {
2016
0
            SRM_SET_ST(output_recon_buffer_resource_ptr);
2017
0
        }
2018
0
        SRM_SET_ST(resource_coordination_results_resource_ptr);
2019
0
        SRM_SET_ST(picture_analysis_results_resource_ptr);
2020
0
        SRM_SET_ST(picture_decision_results_resource_ptr);
2021
0
        SRM_SET_ST(motion_estimation_results_resource_ptr);
2022
0
        SRM_SET_ST(initial_rate_control_results_resource_ptr);
2023
0
        SRM_SET_ST(picture_demux_results_resource_ptr);
2024
0
        SRM_SET_ST(tpl_disp_res_srm);
2025
0
        SRM_SET_ST(rate_control_tasks_resource_ptr);
2026
0
        SRM_SET_ST(rate_control_results_resource_ptr);
2027
0
        SRM_SET_ST(enc_dec_tasks_resource_ptr);
2028
0
        SRM_SET_ST(enc_dec_results_resource_ptr);
2029
0
        SRM_SET_ST(entropy_coding_results_resource_ptr);
2030
0
        SRM_SET_ST(dlf_results_resource_ptr);
2031
0
        SRM_SET_ST(cdef_results_resource_ptr);
2032
0
        SRM_SET_ST(rest_results_resource_ptr);
2033
0
#undef SRM_SET_ST
2034
2035
        // Register all 16 pipeline kernels in stage order
2036
0
        SvtKernelDispatcher* d = &enc_handle_ptr->kernel_dispatcher;
2037
0
#define CONSUMER_FIFO(res) svt_system_resource_get_consumer_fifo(enc_handle_ptr->res, 0)
2038
2039
0
        svt_kernel_dispatcher_register(d,
2040
0
                                       svt_aom_resource_coordination_kernel_iter,
2041
0
                                       enc_handle_ptr->resource_coordination_context_ptr->priv,
2042
0
                                       CONSUMER_FIFO(input_cmd_resource_ptr),
2043
0
                                       "ResCoord");
2044
0
        svt_kernel_dispatcher_register(d,
2045
0
                                       svt_aom_picture_analysis_kernel_iter,
2046
0
                                       enc_handle_ptr->picture_analysis_context_ptr_array[0]->priv,
2047
0
                                       CONSUMER_FIFO(resource_coordination_results_resource_ptr),
2048
0
                                       "PicAnalysis");
2049
0
        svt_kernel_dispatcher_register(d,
2050
0
                                       svt_aom_picture_decision_kernel_iter,
2051
0
                                       enc_handle_ptr->picture_decision_context_ptr->priv,
2052
0
                                       CONSUMER_FIFO(picture_analysis_results_resource_ptr),
2053
0
                                       "PicDecision");
2054
0
        svt_kernel_dispatcher_register(d,
2055
0
                                       svt_aom_motion_estimation_kernel_iter,
2056
0
                                       enc_handle_ptr->motion_estimation_context_ptr_array[0]->priv,
2057
0
                                       CONSUMER_FIFO(picture_decision_results_resource_ptr),
2058
0
                                       "ME");
2059
0
        svt_kernel_dispatcher_register(d,
2060
0
                                       svt_aom_initial_rate_control_kernel_iter,
2061
0
                                       enc_handle_ptr->initial_rate_control_context_ptr->priv,
2062
0
                                       CONSUMER_FIFO(motion_estimation_results_resource_ptr),
2063
0
                                       "InitRC");
2064
0
        svt_kernel_dispatcher_register(d,
2065
0
                                       svt_aom_source_based_operations_kernel_iter,
2066
0
                                       enc_handle_ptr->source_based_operations_context_ptr_array[0]->priv,
2067
0
                                       CONSUMER_FIFO(initial_rate_control_results_resource_ptr),
2068
0
                                       "SrcOps");
2069
0
        svt_kernel_dispatcher_register(d,
2070
0
                                       svt_aom_tpl_disp_kernel_iter,
2071
0
                                       enc_handle_ptr->tpl_disp_context_ptr_array[0]->priv,
2072
0
                                       CONSUMER_FIFO(tpl_disp_res_srm),
2073
0
                                       "TPL");
2074
0
        svt_kernel_dispatcher_register(d,
2075
0
                                       svt_aom_picture_manager_kernel_iter,
2076
0
                                       enc_handle_ptr->picture_manager_context_ptr->priv,
2077
0
                                       CONSUMER_FIFO(picture_demux_results_resource_ptr),
2078
0
                                       "PicMgr");
2079
0
        svt_kernel_dispatcher_register(d,
2080
0
                                       svt_aom_rate_control_kernel_iter,
2081
0
                                       enc_handle_ptr->rate_control_context_ptr->priv,
2082
0
                                       CONSUMER_FIFO(rate_control_tasks_resource_ptr),
2083
0
                                       "RC");
2084
0
        svt_kernel_dispatcher_register(d,
2085
0
                                       svt_aom_mode_decision_configuration_kernel_iter,
2086
0
                                       enc_handle_ptr->mode_decision_configuration_context_ptr_array[0]->priv,
2087
0
                                       CONSUMER_FIFO(rate_control_results_resource_ptr),
2088
0
                                       "MDConfig");
2089
0
        svt_kernel_dispatcher_register(d,
2090
0
                                       svt_aom_mode_decision_kernel_iter,
2091
0
                                       enc_handle_ptr->enc_dec_context_ptr_array[0]->priv,
2092
0
                                       CONSUMER_FIFO(enc_dec_tasks_resource_ptr),
2093
0
                                       "EncDec");
2094
0
        svt_kernel_dispatcher_register(d,
2095
0
                                       svt_aom_dlf_kernel_iter,
2096
0
                                       enc_handle_ptr->dlf_context_ptr_array[0]->priv,
2097
0
                                       CONSUMER_FIFO(enc_dec_results_resource_ptr),
2098
0
                                       "DLF");
2099
0
        svt_kernel_dispatcher_register(d,
2100
0
                                       svt_aom_cdef_kernel_iter,
2101
0
                                       enc_handle_ptr->cdef_context_ptr_array[0]->priv,
2102
0
                                       CONSUMER_FIFO(dlf_results_resource_ptr),
2103
0
                                       "CDEF");
2104
0
        svt_kernel_dispatcher_register(d,
2105
0
                                       svt_aom_rest_kernel_iter,
2106
0
                                       enc_handle_ptr->rest_context_ptr_array[0]->priv,
2107
0
                                       CONSUMER_FIFO(cdef_results_resource_ptr),
2108
0
                                       "Rest");
2109
0
        svt_kernel_dispatcher_register(d,
2110
0
                                       svt_aom_entropy_coding_kernel_iter,
2111
0
                                       enc_handle_ptr->entropy_coding_context_ptr_array[0]->priv,
2112
0
                                       CONSUMER_FIFO(rest_results_resource_ptr),
2113
0
                                       "EC");
2114
0
        svt_kernel_dispatcher_register(d,
2115
0
                                       svt_aom_packetization_kernel_iter,
2116
0
                                       enc_handle_ptr->packetization_context_ptr->priv,
2117
0
                                       CONSUMER_FIFO(entropy_coding_results_resource_ptr),
2118
0
                                       "Pack");
2119
2120
0
#undef CONSUMER_FIFO
2121
2122
        // Store ME context for inline TF/MCTF processing in PD
2123
0
        scs->enc_ctx->st_me_context = enc_handle_ptr->motion_estimation_context_ptr_array[0]->priv;
2124
0
    } else
2125
533
#endif
2126
533
    {
2127
533
        EB_CREATE_THREAD(enc_handle_ptr->resource_coordination_thread_handle,
2128
533
                         svt_aom_resource_coordination_kernel,
2129
533
                         enc_handle_ptr->resource_coordination_context_ptr);
2130
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->picture_analysis_thread_handle_array,
2131
533
                               scs->picture_analysis_process_init_count,
2132
533
                               svt_aom_picture_analysis_kernel,
2133
533
                               enc_handle_ptr->picture_analysis_context_ptr_array,
2134
533
                               "svt-picana");
2135
2136
        // Picture Decision
2137
533
        EB_CREATE_THREAD(enc_handle_ptr->picture_decision_thread_handle,
2138
533
                         svt_aom_picture_decision_kernel,
2139
533
                         enc_handle_ptr->picture_decision_context_ptr);
2140
2141
        // Motion Estimation
2142
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->motion_estimation_thread_handle_array,
2143
533
                               scs->motion_estimation_process_init_count,
2144
533
                               svt_aom_motion_estimation_kernel,
2145
533
                               enc_handle_ptr->motion_estimation_context_ptr_array,
2146
533
                               "svt-me");
2147
2148
        // Initial Rate Control
2149
533
        EB_CREATE_THREAD(enc_handle_ptr->initial_rate_control_thread_handle,
2150
533
                         svt_aom_initial_rate_control_kernel,
2151
533
                         enc_handle_ptr->initial_rate_control_context_ptr);
2152
2153
        // Source Based Oprations
2154
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->source_based_operations_thread_handle_array,
2155
533
                               scs->source_based_operations_process_init_count,
2156
533
                               svt_aom_source_based_operations_kernel,
2157
533
                               enc_handle_ptr->source_based_operations_context_ptr_array,
2158
533
                               "svt-srcops");
2159
2160
        // TPL dispenser
2161
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->tpl_disp_thread_handle_array,
2162
533
                               scs->tpl_disp_process_init_count,
2163
533
                               svt_aom_tpl_disp_kernel, //TODOOMK
2164
533
                               enc_handle_ptr->tpl_disp_context_ptr_array,
2165
533
                               "svt-tpl");
2166
        // Picture Manager
2167
533
        EB_CREATE_THREAD(enc_handle_ptr->picture_manager_thread_handle,
2168
533
                         svt_aom_picture_manager_kernel,
2169
533
                         enc_handle_ptr->picture_manager_context_ptr);
2170
        // Rate Control
2171
533
        EB_CREATE_THREAD(enc_handle_ptr->rate_control_thread_handle,
2172
533
                         svt_aom_rate_control_kernel,
2173
533
                         enc_handle_ptr->rate_control_context_ptr);
2174
2175
        // Mode Decision Configuration Process
2176
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->mode_decision_configuration_thread_handle_array,
2177
533
                               scs->mode_decision_configuration_process_init_count,
2178
533
                               svt_aom_mode_decision_configuration_kernel,
2179
533
                               enc_handle_ptr->mode_decision_configuration_context_ptr_array,
2180
533
                               "svt-mdcfg");
2181
2182
        // EncDec Process
2183
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->enc_dec_thread_handle_array,
2184
533
                               scs->enc_dec_process_init_count,
2185
533
                               svt_aom_mode_decision_kernel,
2186
533
                               enc_handle_ptr->enc_dec_context_ptr_array,
2187
533
                               "svt-md");
2188
2189
        // Dlf Process
2190
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->dlf_thread_handle_array,
2191
533
                               scs->dlf_process_init_count,
2192
533
                               svt_aom_dlf_kernel,
2193
533
                               enc_handle_ptr->dlf_context_ptr_array,
2194
533
                               "svt-dlf");
2195
2196
        // Cdef Process
2197
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->cdef_thread_handle_array,
2198
533
                               scs->cdef_process_init_count,
2199
533
                               svt_aom_cdef_kernel,
2200
533
                               enc_handle_ptr->cdef_context_ptr_array,
2201
533
                               "svt-cdef");
2202
2203
        // Rest Process
2204
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->rest_thread_handle_array,
2205
533
                               scs->rest_process_init_count,
2206
533
                               svt_aom_rest_kernel,
2207
533
                               enc_handle_ptr->rest_context_ptr_array,
2208
533
                               "svt-rest");
2209
2210
        // Entropy Coding Process
2211
533
        EB_CREATE_THREAD_ARRAY(enc_handle_ptr->entropy_coding_thread_handle_array,
2212
533
                               scs->entropy_coding_process_init_count,
2213
533
                               svt_aom_entropy_coding_kernel,
2214
533
                               enc_handle_ptr->entropy_coding_context_ptr_array,
2215
533
                               "svt-ec");
2216
        // Packetization
2217
533
        EB_CREATE_THREAD(enc_handle_ptr->packetization_thread_handle,
2218
533
                         svt_aom_packetization_kernel,
2219
533
                         enc_handle_ptr->packetization_context_ptr);
2220
533
    } // end of thread creation block
2221
2222
533
    svt_print_memory_usage();
2223
2224
533
    return return_error;
2225
533
}
2226
2227
533
static EbErrorType enc_drain_queue(EbComponentType* svt_enc_component) {
2228
533
    bool eos = false;
2229
533
    do {
2230
533
        EbBufferHeaderType* receive_buffer = NULL;
2231
533
        EbErrorType         return_error;
2232
533
        switch ((return_error = svt_av1_enc_get_packet(svt_enc_component, &receive_buffer, 1))) {
2233
0
        case EB_ErrorMax:
2234
0
            return EB_ErrorMax;
2235
533
        case EB_NoErrorEmptyQueue:
2236
533
            eos = true;
2237
533
            break;
2238
0
        default:
2239
0
            break;
2240
533
        }
2241
533
        if (receive_buffer) {
2242
0
            eos = receive_buffer->flags & EB_BUFFERFLAG_EOS;
2243
0
            svt_av1_enc_release_out_buffer(&receive_buffer);
2244
0
            receive_buffer = NULL;
2245
0
        }
2246
533
    } while (!eos);
2247
533
    return EB_ErrorNone;
2248
533
}
2249
2250
/**********************************
2251
* DeInitialize Encoder Library
2252
**********************************/
2253
533
EB_API EbErrorType svt_av1_enc_deinit(EbComponentType* svt_enc_component) {
2254
533
    if (!svt_enc_component || !svt_enc_component->p_component_private) {
2255
0
        return EB_ErrorBadParameter;
2256
0
    }
2257
2258
533
    EbEncHandle* handle = svt_enc_component->p_component_private;
2259
2260
533
    if (handle->input_y8b_buffer_producer_fifo_ptr && handle->frame_received) {
2261
533
        if (!handle->eos_received) {
2262
0
            SVT_ERROR("deinit called without sending EOS!\n");
2263
0
            svt_av1_enc_send_picture(svt_enc_component, &(EbBufferHeaderType){.flags = EB_BUFFERFLAG_EOS});
2264
0
        }
2265
2266
533
        EbErrorType return_error = enc_drain_queue(svt_enc_component);
2267
533
        if (return_error != EB_ErrorNone) {
2268
0
            return return_error;
2269
0
        }
2270
533
    }
2271
2272
    // Free per-instance block geometry table
2273
533
    if (handle->scs_instance && handle->scs_instance->scs && handle->scs_instance->scs->blk_geom_mds != NULL) {
2274
533
        EB_FREE_ARRAY(handle->scs_instance->scs->blk_geom_mds);
2275
533
    }
2276
2277
533
    svt_shutdown_process(handle->input_buffer_resource_ptr);
2278
533
    svt_shutdown_process(handle->input_cmd_resource_ptr);
2279
533
    svt_shutdown_process(handle->resource_coordination_results_resource_ptr);
2280
533
    svt_shutdown_process(handle->picture_analysis_results_resource_ptr);
2281
533
    svt_shutdown_process(handle->picture_decision_results_resource_ptr);
2282
533
    svt_shutdown_process(handle->motion_estimation_results_resource_ptr);
2283
533
    svt_shutdown_process(handle->initial_rate_control_results_resource_ptr);
2284
533
    svt_shutdown_process(handle->picture_demux_results_resource_ptr);
2285
533
    svt_shutdown_process(handle->tpl_disp_res_srm);
2286
533
    svt_shutdown_process(handle->rate_control_tasks_resource_ptr);
2287
533
    svt_shutdown_process(handle->rate_control_results_resource_ptr);
2288
533
    svt_shutdown_process(handle->enc_dec_tasks_resource_ptr);
2289
533
    svt_shutdown_process(handle->enc_dec_results_resource_ptr);
2290
533
    svt_shutdown_process(handle->entropy_coding_results_resource_ptr);
2291
533
    svt_shutdown_process(handle->dlf_results_resource_ptr);
2292
533
    svt_shutdown_process(handle->cdef_results_resource_ptr);
2293
533
    svt_shutdown_process(handle->rest_results_resource_ptr);
2294
2295
533
    return EB_ErrorNone;
2296
533
}
2297
2298
static EbErrorType init_svt_av1_encoder_handle(EbComponentType* hComponent);
2299
2300
/**********************************
2301
* GetHandle
2302
**********************************/
2303
EB_API EbErrorType svt_av1_enc_init_handle(
2304
    EbComponentType**         p_handle, // Function to be called in the future for manipulating the component
2305
    EbSvtAv1EncConfiguration* config_ptr) // pointer passed back to the client during callbacks
2306
2307
533
{
2308
533
    if (p_handle == NULL) {
2309
0
        return EB_ErrorBadParameter;
2310
0
    }
2311
2312
533
    EB_MALLOC_OBJECT(*p_handle);
2313
    // Init Component OS objects (threads, semaphores, etc.)
2314
    // also links the various Component control functions
2315
533
    EbErrorType return_error = init_svt_av1_encoder_handle(*p_handle);
2316
2317
533
    if (return_error == EB_ErrorNone) {
2318
533
        return_error = svt_av1_set_default_params(config_ptr);
2319
533
    }
2320
533
    if (return_error != EB_ErrorNone) {
2321
0
        svt_av1_enc_deinit(*p_handle);
2322
0
        EB_FREE(*p_handle);
2323
0
        *p_handle = NULL;
2324
0
        return return_error;
2325
0
    }
2326
533
    svt_increase_component_count();
2327
533
    return return_error;
2328
533
}
2329
2330
/**********************************
2331
* Encoder Componenet DeInit
2332
**********************************/
2333
533
EbErrorType svt_av1_enc_component_de_init(EbComponentType* svt_enc_component) {
2334
533
    EbErrorType return_error = EB_ErrorNone;
2335
2336
533
    if (svt_enc_component->p_component_private) {
2337
533
        EbEncHandle* handle = (EbEncHandle*)svt_enc_component->p_component_private;
2338
533
        EB_DELETE(handle);
2339
533
        svt_enc_component->p_component_private = NULL;
2340
533
    } else {
2341
0
        return_error = EB_ErrorUndefined;
2342
0
    }
2343
533
    return return_error;
2344
533
}
2345
2346
/**********************************
2347
* svt_av1_enc_deinit_handle
2348
**********************************/
2349
533
EB_API EbErrorType svt_av1_enc_deinit_handle(EbComponentType* svt_enc_component) {
2350
533
    if (svt_enc_component) {
2351
533
        EbErrorType return_error = svt_av1_enc_component_de_init(svt_enc_component);
2352
2353
533
        EB_FREE(svt_enc_component);
2354
533
        svt_decrease_component_count();
2355
533
        return return_error;
2356
533
    }
2357
0
    return EB_ErrorInvalidComponent;
2358
533
}
2359
2360
// Sets the default intra period the closest possible to 1 second without breaking the minigop
2361
0
static int32_t compute_default_intra_period(SequenceControlSet* scs) {
2362
0
    EbSvtAv1EncConfiguration* config = &scs->static_config;
2363
2364
0
    double  fps           = scs->frame_rate;
2365
0
    int32_t mini_gop_size = (1 << (config->hierarchical_levels));
2366
2367
    // use a 5-sec gop by default.
2368
0
    int32_t intra_period = (int)((fps + mini_gop_size) / mini_gop_size) * mini_gop_size * 5;
2369
0
    if (config->intra_refresh_type == 1) {
2370
0
        intra_period -= 1;
2371
0
    }
2372
2373
0
    return intra_period;
2374
0
}
2375
2376
/*
2377
Calculates the default LAD value
2378
*/
2379
533
static uint32_t compute_default_look_ahead(EbSvtAv1EncConfiguration* config) {
2380
533
    int32_t  lad;
2381
533
    uint32_t mg_size = 1 << config->hierarchical_levels;
2382
2383
    /*To accomodate FFMPEG EOS, 1 frame delay is needed in Resource coordination.
2384
       note that we have the option to not add 1 frame delay of Resource Coordination. In this case we have wait for first I frame
2385
       to be released back to be able to start first base(16). Anyway poc16 needs to wait for poc0 to finish.*/
2386
533
    uint32_t eos_delay    = 1;
2387
533
    uint32_t max_tf_delay = 6;
2388
2389
533
    if (config->rate_control_mode == SVT_AV1_RC_MODE_CQP_OR_CRF) {
2390
533
        lad = (1 + mg_size) * (1 + MIN_LAD_MG) + max_tf_delay + eos_delay;
2391
533
    } else {
2392
0
        lad = (1 + mg_size) * (1 + RC_DEFAULT_LAD_MG) + max_tf_delay + eos_delay;
2393
0
    }
2394
2395
533
    lad = lad > MAX_LAD ? MAX_LAD : lad; // clip to max allowed lad
2396
533
    return lad;
2397
533
}
2398
2399
/*
2400
Updates the LAD value
2401
*/
2402
0
static void update_look_ahead(SequenceControlSet* scs) {
2403
    /*To accomodate FFMPEG EOS, 1 frame delay is needed in Resource coordination.
2404
           note that we have the option to not add 1 frame delay of Resource Coordination. In this case we have wait for first I frame
2405
           to be released back to be able to start first base(16). Anyway poc16 needs to wait for poc0 to finish.*/
2406
0
    uint32_t eos_delay = 1;
2407
2408
0
    uint32_t mg_size = 1 << scs->static_config.hierarchical_levels;
2409
0
    if ((int32_t)(scs->static_config.look_ahead_distance - (eos_delay + scs->scd_delay)) < (int32_t)(mg_size + 1)) {
2410
        // Not enough pictures to form the minigop. update mg_size
2411
0
        scs->static_config.look_ahead_distance = mg_size + 1 + (eos_delay + scs->scd_delay);
2412
0
        SVT_WARN("Minimum lookahead distance to run %dL with TF %d is %d. Force the look_ahead_distance to be %d\n",
2413
0
                 scs->static_config.hierarchical_levels + 1,
2414
0
                 scs->static_config.enable_tf,
2415
0
                 scs->static_config.look_ahead_distance,
2416
0
                 scs->static_config.look_ahead_distance);
2417
0
    }
2418
2419
0
    int32_t picture_in_future = scs->static_config.look_ahead_distance;
2420
    // Subtract pictures used for scd_delay and eos_delay
2421
0
    picture_in_future = MAX(0, (int32_t)(picture_in_future - eos_delay - scs->scd_delay));
2422
    // Subtract pictures used for minigop formation. Unit= 1(provision for a potential delayI)
2423
0
    picture_in_future = MAX(0, (int32_t)(picture_in_future - (1 + mg_size)));
2424
    // Specify the number of mini-gops to be used in the sliding window. 0: 1 mini-gop, 1: 2 mini-gops and 3: 3 mini-gops
2425
0
    scs->lad_mg = (picture_in_future + (mg_size + 1) / 2) / (mg_size + 1);
2426
    // Since TPL is tuned for 0, 1 and 2 mini-gops, we make sure lad_mg is not smaller than tpl_lad_mg
2427
0
    if (scs->lad_mg < scs->tpl_lad_mg) {
2428
0
        scs->lad_mg                            = scs->tpl_lad_mg;
2429
0
        scs->static_config.look_ahead_distance = (1 + mg_size) * (scs->lad_mg + 1) + scs->scd_delay + eos_delay;
2430
0
        SVT_WARN(
2431
0
            "Lookahead distance is not long enough to get best bdrate trade off. Force the look_ahead_distance to be "
2432
0
            "%d\n",
2433
0
            scs->static_config.look_ahead_distance);
2434
0
    } else if (scs->lad_mg > scs->tpl_lad_mg &&
2435
0
               (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CQP_OR_CRF ||
2436
0
                scs->static_config.pass == ENC_FIRST_PASS || scs->static_config.pass == ENC_SECOND_PASS)) {
2437
0
        scs->lad_mg                            = scs->tpl_lad_mg;
2438
0
        scs->static_config.look_ahead_distance = (1 + mg_size) * (scs->lad_mg + 1) + scs->scd_delay + eos_delay;
2439
0
        SVT_WARN(
2440
0
            "For CRF or 2PASS RC mode, the maximum needed Lookahead distance is %d. Force the look_ahead_distance to "
2441
0
            "be %d\n",
2442
0
            scs->static_config.look_ahead_distance,
2443
0
            scs->static_config.look_ahead_distance);
2444
0
    }
2445
0
}
2446
2447
/*
2448
 * Control TF
2449
 */
2450
uint8_t svt_aom_tf_max_ref_per_struct(uint32_t hierarchical_levels, uint8_t type /*I_SLICE, BASE, L1*/,
2451
0
                                      bool direction /*Past, Future*/) {
2452
0
    uint8_t max_ref_per;
2453
0
    (void)direction;
2454
0
    if (type == 0) { // I_SLICE
2455
0
        max_ref_per = 1 << hierarchical_levels;
2456
0
    } else if (type == 1) { // BASE
2457
0
        max_ref_per = TF_MAX_BASE_REF_PICS;
2458
0
    } else { // L1
2459
0
        max_ref_per = hierarchical_levels < 5 ? TF_MAX_L1_REF_PICS_SUB_6L : TF_MAX_L1_REF_PICS_6L;
2460
0
    }
2461
2462
0
    return max_ref_per;
2463
0
}
2464
2465
/******************************************************************************
2466
* tf_ld_controls
2467
* TF control functions for low delay mode
2468
*******************************************************************************/
2469
0
static void tf_ld_controls(SequenceControlSet* scs, uint8_t tf_level) {
2470
0
    switch (tf_level) {
2471
0
    case 0:
2472
        // I_SLICE TF Params
2473
0
        scs->tf_params_per_type[0].enabled = 0;
2474
2475
        // BASE TF Params
2476
0
        scs->tf_params_per_type[1].enabled = 0;
2477
2478
        // L1 TF Params
2479
0
        scs->tf_params_per_type[2].enabled = 0;
2480
0
        break;
2481
2482
0
    case 1:
2483
        // I_SLICE TF Params
2484
0
        scs->tf_params_per_type[0].enabled = 0;
2485
        // BASE TF Params
2486
0
        scs->tf_params_per_type[1].enabled                 = 1;
2487
0
        scs->tf_params_per_type[1].num_past_pics           = 1;
2488
0
        scs->tf_params_per_type[1].num_future_pics         = 0;
2489
0
        scs->tf_params_per_type[1].modulate_pics           = 0;
2490
0
        scs->tf_params_per_type[1].max_num_past_pics       = 1;
2491
0
        scs->tf_params_per_type[1].max_num_future_pics     = 0;
2492
0
        scs->tf_params_per_type[1].hme_me_level            = 4;
2493
0
        scs->tf_params_per_type[1].half_pel_mode           = 0;
2494
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 0;
2495
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
2496
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
2497
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 20 * 32 * 32;
2498
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
2499
0
        scs->tf_params_per_type[1].use_zz_based_filter     = 1;
2500
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
2501
0
        scs->tf_params_per_type[1].use_2tap                = 0;
2502
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 0;
2503
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 0;
2504
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
2505
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
2506
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 1;
2507
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
2508
0
        scs->tf_params_per_type[1].qp_opt                  = 0;
2509
        // L1 TF Params
2510
0
        scs->tf_params_per_type[2].enabled = 0;
2511
0
        break;
2512
0
    case 2:
2513
        // I_SLICE TF Params
2514
0
        scs->tf_params_per_type[0].enabled = 0;
2515
        // BASE TF Params
2516
0
        scs->tf_params_per_type[1].enabled                 = 1;
2517
0
        scs->tf_params_per_type[1].num_past_pics           = 1;
2518
0
        scs->tf_params_per_type[1].num_future_pics         = 0;
2519
0
        scs->tf_params_per_type[1].modulate_pics           = 0;
2520
0
        scs->tf_params_per_type[1].max_num_past_pics       = 1;
2521
0
        scs->tf_params_per_type[1].max_num_future_pics     = 0;
2522
0
        scs->tf_params_per_type[1].hme_me_level            = 4;
2523
0
        scs->tf_params_per_type[1].half_pel_mode           = 0;
2524
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 0;
2525
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
2526
0
        scs->tf_params_per_type[1].chroma_lvl              = 2;
2527
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = (uint64_t)~0;
2528
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
2529
0
        scs->tf_params_per_type[1].use_zz_based_filter     = 1;
2530
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
2531
0
        scs->tf_params_per_type[1].use_2tap                = 0;
2532
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 0;
2533
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 0;
2534
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
2535
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
2536
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 0;
2537
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
2538
0
        scs->tf_params_per_type[1].qp_opt                  = 0;
2539
        // L1 TF Params
2540
0
        scs->tf_params_per_type[2].enabled = 0;
2541
0
        break;
2542
2543
0
    default:
2544
0
        assert(0);
2545
0
        break;
2546
0
    }
2547
    // 8x8 path not supported in LD TF
2548
0
    scs->tf_params_per_type[0].enable_8x8_pred = 0;
2549
0
    scs->tf_params_per_type[1].enable_8x8_pred = 0;
2550
0
    scs->tf_params_per_type[2].enable_8x8_pred = 0;
2551
0
}
2552
2553
533
void tf_controls(SequenceControlSet* scs, uint8_t tf_level) {
2554
533
    switch (tf_level) {
2555
533
    case 0:
2556
        // I_SLICE TF Params
2557
533
        scs->tf_params_per_type[0].enabled = 0;
2558
2559
        // BASE TF Params
2560
533
        scs->tf_params_per_type[1].enabled = 0;
2561
2562
        // L1 TF Params
2563
533
        scs->tf_params_per_type[2].enabled = 0;
2564
533
        break;
2565
2566
0
    case 1:
2567
        // I_SLICE TF Params
2568
0
        scs->tf_params_per_type[0].enabled             = 1;
2569
0
        scs->tf_params_per_type[0].num_future_pics     = 24;
2570
0
        scs->tf_params_per_type[0].modulate_pics       = 1;
2571
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
2572
0
            (1 << scs->static_config.hierarchical_levels),
2573
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
2574
0
        scs->tf_params_per_type[0].hme_me_level            = 1;
2575
0
        scs->tf_params_per_type[0].half_pel_mode           = 1;
2576
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
2577
0
        scs->tf_params_per_type[0].eight_pel_mode          = 1;
2578
0
        scs->tf_params_per_type[0].chroma_lvl              = 1;
2579
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = 0;
2580
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 1;
2581
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 0;
2582
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 0;
2583
0
        scs->tf_params_per_type[0].use_2tap                = 0;
2584
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 0;
2585
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
2586
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 0;
2587
0
        scs->tf_params_per_type[0].me_exit_th              = 0;
2588
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 0;
2589
0
        scs->tf_params_per_type[0].ref_frame_factor        = 1;
2590
0
        scs->tf_params_per_type[0].qp_opt                  = 0;
2591
        // BASE TF Params
2592
0
        scs->tf_params_per_type[1].enabled           = 1;
2593
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
2594
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
2595
0
        scs->tf_params_per_type[1].modulate_pics     = 1;
2596
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
2597
0
            (1 << scs->static_config.hierarchical_levels),
2598
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
2599
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
2600
0
            (1 << scs->static_config.hierarchical_levels),
2601
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
2602
0
        scs->tf_params_per_type[1].hme_me_level            = 1;
2603
0
        scs->tf_params_per_type[1].half_pel_mode           = 1;
2604
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
2605
0
        scs->tf_params_per_type[1].eight_pel_mode          = 1;
2606
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
2607
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 0;
2608
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 1;
2609
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
2610
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
2611
0
        scs->tf_params_per_type[1].use_2tap                = 0;
2612
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 0;
2613
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
2614
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
2615
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
2616
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 0;
2617
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
2618
0
        scs->tf_params_per_type[1].qp_opt                  = 0;
2619
        // L1 TF Params
2620
0
        scs->tf_params_per_type[2].enabled           = 1;
2621
0
        scs->tf_params_per_type[2].num_past_pics     = 1;
2622
0
        scs->tf_params_per_type[2].num_future_pics   = 1;
2623
0
        scs->tf_params_per_type[2].modulate_pics     = 1;
2624
0
        scs->tf_params_per_type[2].max_num_past_pics = MIN(
2625
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2626
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 0));
2627
0
        scs->tf_params_per_type[2].max_num_future_pics = MIN(
2628
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2629
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 1));
2630
0
        scs->tf_params_per_type[2].hme_me_level            = 1;
2631
0
        scs->tf_params_per_type[2].half_pel_mode           = 1;
2632
0
        scs->tf_params_per_type[2].quarter_pel_mode        = 1;
2633
0
        scs->tf_params_per_type[2].eight_pel_mode          = 1;
2634
0
        scs->tf_params_per_type[2].chroma_lvl              = 1;
2635
0
        scs->tf_params_per_type[2].pred_error_32x32_th     = 0;
2636
0
        scs->tf_params_per_type[2].enable_8x8_pred         = 1;
2637
0
        scs->tf_params_per_type[2].sub_sampling_shift      = 0;
2638
0
        scs->tf_params_per_type[2].avoid_2d_qpel           = 0;
2639
0
        scs->tf_params_per_type[2].use_2tap                = 0;
2640
0
        scs->tf_params_per_type[2].use_intra_for_noise_est = 0;
2641
0
        scs->tf_params_per_type[2].use_8bit_subpel         = 1;
2642
0
        scs->tf_params_per_type[2].use_pred_64x64_only_th  = 0;
2643
0
        scs->tf_params_per_type[2].me_exit_th              = 0;
2644
0
        scs->tf_params_per_type[2].subpel_early_exit_th    = 0;
2645
0
        scs->tf_params_per_type[2].ref_frame_factor        = 1;
2646
0
        scs->tf_params_per_type[2].qp_opt                  = 0;
2647
0
        break;
2648
2649
0
    case 2:
2650
        // I_SLICE TF Params
2651
0
        scs->tf_params_per_type[0].enabled             = 1;
2652
0
        scs->tf_params_per_type[0].num_future_pics     = 24;
2653
0
        scs->tf_params_per_type[0].modulate_pics       = 1;
2654
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
2655
0
            (1 << scs->static_config.hierarchical_levels),
2656
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
2657
0
        scs->tf_params_per_type[0].hme_me_level            = 1;
2658
0
        scs->tf_params_per_type[0].half_pel_mode           = 1;
2659
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
2660
0
        scs->tf_params_per_type[0].eight_pel_mode          = 1;
2661
0
        scs->tf_params_per_type[0].chroma_lvl              = 1;
2662
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = 8 * 32 * 32;
2663
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 1;
2664
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 0;
2665
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 0;
2666
0
        scs->tf_params_per_type[0].use_2tap                = 0;
2667
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 0;
2668
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
2669
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 0;
2670
0
        scs->tf_params_per_type[0].me_exit_th              = 0;
2671
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 0;
2672
0
        scs->tf_params_per_type[0].ref_frame_factor        = 1;
2673
0
        scs->tf_params_per_type[0].qp_opt                  = 0;
2674
        // BASE TF Params
2675
0
        scs->tf_params_per_type[1].enabled           = 1;
2676
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
2677
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
2678
0
        scs->tf_params_per_type[1].modulate_pics     = 2;
2679
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
2680
0
            (1 << scs->static_config.hierarchical_levels),
2681
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
2682
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
2683
0
            (1 << scs->static_config.hierarchical_levels),
2684
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
2685
0
        scs->tf_params_per_type[1].hme_me_level            = 1;
2686
0
        scs->tf_params_per_type[1].half_pel_mode           = 1;
2687
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
2688
0
        scs->tf_params_per_type[1].eight_pel_mode          = 1;
2689
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
2690
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 8 * 32 * 32;
2691
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 1;
2692
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
2693
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
2694
0
        scs->tf_params_per_type[1].use_2tap                = 0;
2695
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 0;
2696
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
2697
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
2698
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
2699
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 0;
2700
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
2701
0
        scs->tf_params_per_type[1].qp_opt                  = 0;
2702
        // L1 TF Params
2703
0
        scs->tf_params_per_type[2].enabled           = 1;
2704
0
        scs->tf_params_per_type[2].num_past_pics     = 1;
2705
0
        scs->tf_params_per_type[2].num_future_pics   = 1;
2706
0
        scs->tf_params_per_type[2].modulate_pics     = 1;
2707
0
        scs->tf_params_per_type[2].max_num_past_pics = MIN(
2708
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2709
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 0));
2710
0
        scs->tf_params_per_type[2].max_num_future_pics = MIN(
2711
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2712
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 1));
2713
0
        scs->tf_params_per_type[2].hme_me_level            = 1;
2714
0
        scs->tf_params_per_type[2].half_pel_mode           = 1;
2715
0
        scs->tf_params_per_type[2].quarter_pel_mode        = 1;
2716
0
        scs->tf_params_per_type[2].eight_pel_mode          = 1;
2717
0
        scs->tf_params_per_type[2].chroma_lvl              = 1;
2718
0
        scs->tf_params_per_type[2].pred_error_32x32_th     = 8 * 32 * 32;
2719
0
        scs->tf_params_per_type[2].enable_8x8_pred         = 1;
2720
0
        scs->tf_params_per_type[2].sub_sampling_shift      = 0;
2721
0
        scs->tf_params_per_type[2].avoid_2d_qpel           = 0;
2722
0
        scs->tf_params_per_type[2].use_2tap                = 0;
2723
0
        scs->tf_params_per_type[2].use_intra_for_noise_est = 0;
2724
0
        scs->tf_params_per_type[2].use_8bit_subpel         = 1;
2725
0
        scs->tf_params_per_type[2].use_pred_64x64_only_th  = 0;
2726
0
        scs->tf_params_per_type[2].me_exit_th              = 0;
2727
0
        scs->tf_params_per_type[2].subpel_early_exit_th    = 0;
2728
0
        scs->tf_params_per_type[2].ref_frame_factor        = 1;
2729
0
        scs->tf_params_per_type[2].qp_opt                  = 0;
2730
0
        break;
2731
2732
0
    case 3:
2733
        // I_SLICE TF Params
2734
0
        scs->tf_params_per_type[0].enabled             = 1;
2735
0
        scs->tf_params_per_type[0].num_future_pics     = 24;
2736
0
        scs->tf_params_per_type[0].modulate_pics       = 1;
2737
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
2738
0
            (1 << scs->static_config.hierarchical_levels),
2739
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
2740
0
        scs->tf_params_per_type[0].hme_me_level            = 1;
2741
0
        scs->tf_params_per_type[0].half_pel_mode           = 1;
2742
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
2743
0
        scs->tf_params_per_type[0].eight_pel_mode          = 1;
2744
0
        scs->tf_params_per_type[0].chroma_lvl              = 1;
2745
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = 8 * 32 * 32;
2746
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 1;
2747
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 0;
2748
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 0;
2749
0
        scs->tf_params_per_type[0].use_2tap                = 0;
2750
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 0;
2751
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
2752
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 0;
2753
0
        scs->tf_params_per_type[0].me_exit_th              = 0;
2754
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 0;
2755
0
        scs->tf_params_per_type[0].ref_frame_factor        = 1;
2756
0
        scs->tf_params_per_type[0].qp_opt                  = 1;
2757
2758
        // BASE TF Params
2759
0
        scs->tf_params_per_type[1].enabled           = 1;
2760
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
2761
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
2762
0
        scs->tf_params_per_type[1].modulate_pics     = 2;
2763
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
2764
0
            (1 << scs->static_config.hierarchical_levels),
2765
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
2766
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
2767
0
            (1 << scs->static_config.hierarchical_levels),
2768
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
2769
0
        scs->tf_params_per_type[1].hme_me_level            = 1;
2770
0
        scs->tf_params_per_type[1].half_pel_mode           = 1;
2771
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
2772
0
        scs->tf_params_per_type[1].eight_pel_mode          = 1;
2773
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
2774
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 8 * 32 * 32;
2775
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 0;
2776
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
2777
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
2778
0
        scs->tf_params_per_type[1].use_2tap                = 0;
2779
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 0;
2780
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
2781
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
2782
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
2783
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 0;
2784
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
2785
0
        scs->tf_params_per_type[1].qp_opt                  = 1;
2786
2787
        // L1 TF Params
2788
0
        scs->tf_params_per_type[2].enabled           = 1;
2789
0
        scs->tf_params_per_type[2].num_past_pics     = 1;
2790
0
        scs->tf_params_per_type[2].num_future_pics   = 1;
2791
0
        scs->tf_params_per_type[2].modulate_pics     = 1;
2792
0
        scs->tf_params_per_type[2].max_num_past_pics = MIN(
2793
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2794
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 0));
2795
0
        scs->tf_params_per_type[2].max_num_future_pics = MIN(
2796
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2797
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 1));
2798
0
        scs->tf_params_per_type[2].hme_me_level            = 1;
2799
0
        scs->tf_params_per_type[2].half_pel_mode           = 1;
2800
0
        scs->tf_params_per_type[2].quarter_pel_mode        = 1;
2801
0
        scs->tf_params_per_type[2].eight_pel_mode          = 1;
2802
0
        scs->tf_params_per_type[2].chroma_lvl              = 1;
2803
0
        scs->tf_params_per_type[2].pred_error_32x32_th     = 8 * 32 * 32;
2804
0
        scs->tf_params_per_type[2].enable_8x8_pred         = 0;
2805
0
        scs->tf_params_per_type[2].sub_sampling_shift      = 0;
2806
0
        scs->tf_params_per_type[2].avoid_2d_qpel           = 0;
2807
0
        scs->tf_params_per_type[2].use_2tap                = 0;
2808
0
        scs->tf_params_per_type[2].use_intra_for_noise_est = 0;
2809
0
        scs->tf_params_per_type[2].use_8bit_subpel         = 1;
2810
0
        scs->tf_params_per_type[2].use_pred_64x64_only_th  = 0;
2811
0
        scs->tf_params_per_type[2].me_exit_th              = 0;
2812
0
        scs->tf_params_per_type[2].subpel_early_exit_th    = 0;
2813
0
        scs->tf_params_per_type[2].ref_frame_factor        = 1;
2814
0
        scs->tf_params_per_type[2].qp_opt                  = 1;
2815
0
        break;
2816
0
    case 4:
2817
        // I_SLICE TF Params
2818
0
        scs->tf_params_per_type[0].enabled             = 1;
2819
0
        scs->tf_params_per_type[0].num_future_pics     = 24;
2820
0
        scs->tf_params_per_type[0].modulate_pics       = 1;
2821
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
2822
0
            (1 << scs->static_config.hierarchical_levels),
2823
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
2824
0
        scs->tf_params_per_type[0].hme_me_level            = 2;
2825
0
        scs->tf_params_per_type[0].half_pel_mode           = 1;
2826
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
2827
0
        scs->tf_params_per_type[0].eight_pel_mode          = 0;
2828
0
        scs->tf_params_per_type[0].chroma_lvl              = 1;
2829
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = 8 * 32 * 32;
2830
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 0;
2831
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 0;
2832
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 0;
2833
0
        scs->tf_params_per_type[0].use_2tap                = 0;
2834
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 0;
2835
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
2836
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 0;
2837
0
        scs->tf_params_per_type[0].me_exit_th              = 0;
2838
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 1;
2839
0
        scs->tf_params_per_type[0].ref_frame_factor        = 1;
2840
0
        scs->tf_params_per_type[0].qp_opt                  = 1;
2841
2842
        // BASE TF Params
2843
0
        scs->tf_params_per_type[1].enabled           = 1;
2844
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
2845
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
2846
0
        scs->tf_params_per_type[1].modulate_pics     = 2;
2847
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
2848
0
            (1 << scs->static_config.hierarchical_levels),
2849
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
2850
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
2851
0
            (1 << scs->static_config.hierarchical_levels),
2852
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
2853
0
        scs->tf_params_per_type[1].hme_me_level            = 2;
2854
0
        scs->tf_params_per_type[1].half_pel_mode           = 1;
2855
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
2856
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
2857
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
2858
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 8 * 32 * 32;
2859
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 0;
2860
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
2861
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
2862
0
        scs->tf_params_per_type[1].use_2tap                = 0;
2863
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 0;
2864
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
2865
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
2866
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
2867
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 1;
2868
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
2869
0
        scs->tf_params_per_type[1].qp_opt                  = 1;
2870
2871
        // L1 TF Params
2872
0
        scs->tf_params_per_type[2].enabled           = 1;
2873
0
        scs->tf_params_per_type[2].num_past_pics     = 1;
2874
0
        scs->tf_params_per_type[2].num_future_pics   = 1;
2875
0
        scs->tf_params_per_type[2].modulate_pics     = 1;
2876
0
        scs->tf_params_per_type[2].max_num_past_pics = MIN(
2877
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2878
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 0));
2879
0
        scs->tf_params_per_type[2].max_num_future_pics = MIN(
2880
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2881
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 1));
2882
0
        scs->tf_params_per_type[2].hme_me_level            = 2;
2883
0
        scs->tf_params_per_type[2].half_pel_mode           = 1;
2884
0
        scs->tf_params_per_type[2].quarter_pel_mode        = 1;
2885
0
        scs->tf_params_per_type[2].eight_pel_mode          = 1;
2886
0
        scs->tf_params_per_type[2].chroma_lvl              = 1;
2887
0
        scs->tf_params_per_type[2].pred_error_32x32_th     = 8 * 32 * 32;
2888
0
        scs->tf_params_per_type[2].enable_8x8_pred         = 0;
2889
0
        scs->tf_params_per_type[2].sub_sampling_shift      = 0;
2890
0
        scs->tf_params_per_type[2].avoid_2d_qpel           = 0;
2891
0
        scs->tf_params_per_type[2].use_2tap                = 0;
2892
0
        scs->tf_params_per_type[2].use_intra_for_noise_est = 0;
2893
0
        scs->tf_params_per_type[2].use_8bit_subpel         = 1;
2894
0
        scs->tf_params_per_type[2].use_pred_64x64_only_th  = 0;
2895
0
        scs->tf_params_per_type[2].me_exit_th              = 0;
2896
0
        scs->tf_params_per_type[2].subpel_early_exit_th    = 0;
2897
0
        scs->tf_params_per_type[2].ref_frame_factor        = 1;
2898
0
        scs->tf_params_per_type[2].qp_opt                  = 1;
2899
0
        break;
2900
0
    case 5:
2901
        // I_SLICE TF Params
2902
0
        scs->tf_params_per_type[0].enabled             = 1;
2903
0
        scs->tf_params_per_type[0].num_future_pics     = 24;
2904
0
        scs->tf_params_per_type[0].modulate_pics       = 1;
2905
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
2906
0
            (1 << scs->static_config.hierarchical_levels),
2907
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
2908
0
        scs->tf_params_per_type[0].hme_me_level            = 2;
2909
0
        scs->tf_params_per_type[0].half_pel_mode           = 2;
2910
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
2911
0
        scs->tf_params_per_type[0].eight_pel_mode          = 0;
2912
0
        scs->tf_params_per_type[0].chroma_lvl              = 1;
2913
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = 20 * 32 * 32;
2914
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 0;
2915
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 0;
2916
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 0;
2917
0
        scs->tf_params_per_type[0].use_2tap                = 1;
2918
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 0;
2919
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
2920
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 0;
2921
0
        scs->tf_params_per_type[0].me_exit_th              = 0;
2922
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 1;
2923
0
        scs->tf_params_per_type[0].ref_frame_factor        = 1;
2924
0
        scs->tf_params_per_type[0].qp_opt                  = 1;
2925
2926
        // BASE TF Params
2927
0
        scs->tf_params_per_type[1].enabled           = 1;
2928
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
2929
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
2930
0
        scs->tf_params_per_type[1].modulate_pics     = 3;
2931
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
2932
0
            (1 << scs->static_config.hierarchical_levels),
2933
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
2934
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
2935
0
            (1 << scs->static_config.hierarchical_levels),
2936
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
2937
0
        scs->tf_params_per_type[1].hme_me_level            = 2;
2938
0
        scs->tf_params_per_type[1].half_pel_mode           = 2;
2939
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
2940
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
2941
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
2942
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 20 * 32 * 32;
2943
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 0;
2944
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
2945
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
2946
0
        scs->tf_params_per_type[1].use_2tap                = 1;
2947
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 0;
2948
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
2949
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
2950
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
2951
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 1;
2952
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
2953
0
        scs->tf_params_per_type[1].qp_opt                  = 1;
2954
2955
        // L1 TF Params
2956
0
        scs->tf_params_per_type[2].enabled           = 1;
2957
0
        scs->tf_params_per_type[2].num_past_pics     = 1;
2958
0
        scs->tf_params_per_type[2].num_future_pics   = 1;
2959
0
        scs->tf_params_per_type[2].modulate_pics     = 2;
2960
0
        scs->tf_params_per_type[2].max_num_past_pics = MIN(
2961
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2962
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 0));
2963
0
        scs->tf_params_per_type[2].max_num_future_pics = MIN(
2964
0
            (1 << scs->static_config.hierarchical_levels) / 2,
2965
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 2, 1));
2966
0
        scs->tf_params_per_type[2].hme_me_level            = 2;
2967
0
        scs->tf_params_per_type[2].half_pel_mode           = 2;
2968
0
        scs->tf_params_per_type[2].quarter_pel_mode        = 1;
2969
0
        scs->tf_params_per_type[2].eight_pel_mode          = 0;
2970
0
        scs->tf_params_per_type[2].chroma_lvl              = 1;
2971
0
        scs->tf_params_per_type[2].pred_error_32x32_th     = 20 * 32 * 32;
2972
0
        scs->tf_params_per_type[2].enable_8x8_pred         = 0;
2973
0
        scs->tf_params_per_type[2].sub_sampling_shift      = 0;
2974
0
        scs->tf_params_per_type[2].avoid_2d_qpel           = 0;
2975
0
        scs->tf_params_per_type[2].use_2tap                = 1;
2976
0
        scs->tf_params_per_type[2].use_intra_for_noise_est = 0;
2977
0
        scs->tf_params_per_type[2].use_8bit_subpel         = 1;
2978
0
        scs->tf_params_per_type[2].use_pred_64x64_only_th  = 0;
2979
0
        scs->tf_params_per_type[2].me_exit_th              = 0;
2980
0
        scs->tf_params_per_type[2].subpel_early_exit_th    = 1;
2981
0
        scs->tf_params_per_type[2].ref_frame_factor        = 1;
2982
0
        scs->tf_params_per_type[2].qp_opt                  = 1;
2983
0
        break;
2984
0
    case 6:
2985
        // I_SLICE TF Params
2986
0
        scs->tf_params_per_type[0].enabled             = 1;
2987
0
        scs->tf_params_per_type[0].num_future_pics     = (scs->static_config.hierarchical_levels < 5) ? 8 : 16;
2988
0
        scs->tf_params_per_type[0].modulate_pics       = 0;
2989
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
2990
0
            (1 << scs->static_config.hierarchical_levels),
2991
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
2992
0
        scs->tf_params_per_type[0].hme_me_level            = 2;
2993
0
        scs->tf_params_per_type[0].half_pel_mode           = 2;
2994
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
2995
0
        scs->tf_params_per_type[0].eight_pel_mode          = 0;
2996
0
        scs->tf_params_per_type[0].chroma_lvl              = 0;
2997
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = (uint64_t)~0;
2998
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 0;
2999
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 1;
3000
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 1;
3001
0
        scs->tf_params_per_type[0].use_2tap                = 1;
3002
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 1;
3003
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
3004
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 0;
3005
0
        scs->tf_params_per_type[0].me_exit_th              = 0;
3006
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 1;
3007
0
        scs->tf_params_per_type[0].ref_frame_factor        = 1;
3008
0
        scs->tf_params_per_type[0].qp_opt                  = 1;
3009
        // BASE TF Params
3010
0
        scs->tf_params_per_type[1].enabled           = 1;
3011
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
3012
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
3013
0
        scs->tf_params_per_type[1].modulate_pics     = 3;
3014
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
3015
0
            (1 << scs->static_config.hierarchical_levels),
3016
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
3017
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
3018
0
            (1 << scs->static_config.hierarchical_levels),
3019
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
3020
0
        scs->tf_params_per_type[1].hme_me_level            = 2;
3021
0
        scs->tf_params_per_type[1].half_pel_mode           = 2;
3022
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
3023
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
3024
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
3025
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 20 * 32 * 32;
3026
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 0;
3027
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
3028
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
3029
0
        scs->tf_params_per_type[1].use_2tap                = 1;
3030
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 1;
3031
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
3032
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 0;
3033
0
        scs->tf_params_per_type[1].me_exit_th              = 0;
3034
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 1;
3035
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
3036
0
        scs->tf_params_per_type[1].qp_opt                  = 1;
3037
        // L1 TF Params
3038
0
        scs->tf_params_per_type[2].enabled = 0;
3039
0
        break;
3040
0
    case 7:
3041
        // I_SLICE TF Params
3042
0
        scs->tf_params_per_type[0].enabled             = 1;
3043
0
        scs->tf_params_per_type[0].num_future_pics     = (scs->static_config.hierarchical_levels < 5) ? 8 : 16;
3044
0
        scs->tf_params_per_type[0].modulate_pics       = 0;
3045
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
3046
0
            (1 << scs->static_config.hierarchical_levels),
3047
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
3048
0
        scs->tf_params_per_type[0].hme_me_level            = 2;
3049
0
        scs->tf_params_per_type[0].half_pel_mode           = 2;
3050
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
3051
0
        scs->tf_params_per_type[0].eight_pel_mode          = 0;
3052
0
        scs->tf_params_per_type[0].chroma_lvl              = 0;
3053
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = (uint64_t)~0;
3054
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 0;
3055
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 1;
3056
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 1;
3057
0
        scs->tf_params_per_type[0].use_2tap                = 1;
3058
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 1;
3059
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
3060
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 35;
3061
0
        scs->tf_params_per_type[0].me_exit_th              = 16 * 16;
3062
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 1;
3063
0
        scs->tf_params_per_type[0].ref_frame_factor        = 1;
3064
0
        scs->tf_params_per_type[0].qp_opt                  = 1;
3065
        // BASE TF Params
3066
0
        scs->tf_params_per_type[1].enabled           = 1;
3067
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
3068
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
3069
0
        scs->tf_params_per_type[1].modulate_pics     = 3;
3070
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
3071
0
            (1 << scs->static_config.hierarchical_levels),
3072
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
3073
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
3074
0
            (1 << scs->static_config.hierarchical_levels),
3075
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
3076
0
        scs->tf_params_per_type[1].hme_me_level            = 2;
3077
0
        scs->tf_params_per_type[1].half_pel_mode           = 2;
3078
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
3079
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
3080
0
        scs->tf_params_per_type[1].chroma_lvl              = 1;
3081
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = 20 * 32 * 32;
3082
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 0;
3083
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 0;
3084
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 0;
3085
0
        scs->tf_params_per_type[1].use_2tap                = 1;
3086
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 1;
3087
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
3088
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 35;
3089
0
        scs->tf_params_per_type[1].me_exit_th              = 16 * 16;
3090
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 1;
3091
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
3092
0
        scs->tf_params_per_type[1].qp_opt                  = 1;
3093
        // L1 TF Params
3094
0
        scs->tf_params_per_type[2].enabled = 0;
3095
0
        break;
3096
0
    case 8:
3097
        // I_SLICE TF Params
3098
0
        scs->tf_params_per_type[0].enabled             = 1;
3099
0
        scs->tf_params_per_type[0].num_future_pics     = 8;
3100
0
        scs->tf_params_per_type[0].modulate_pics       = 0;
3101
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
3102
0
            (1 << scs->static_config.hierarchical_levels),
3103
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
3104
0
        scs->tf_params_per_type[0].hme_me_level            = 2;
3105
0
        scs->tf_params_per_type[0].half_pel_mode           = 2;
3106
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
3107
0
        scs->tf_params_per_type[0].eight_pel_mode          = 0;
3108
0
        scs->tf_params_per_type[0].chroma_lvl              = 0;
3109
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = (uint64_t)~0;
3110
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 0;
3111
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 1;
3112
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 1;
3113
0
        scs->tf_params_per_type[0].use_2tap                = 1;
3114
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 1;
3115
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
3116
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 35;
3117
0
        scs->tf_params_per_type[0].me_exit_th              = 16 * 16;
3118
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 4;
3119
0
        scs->tf_params_per_type[0].ref_frame_factor        = 2;
3120
0
        scs->tf_params_per_type[0].qp_opt                  = 1;
3121
        // BASE TF Params
3122
0
        scs->tf_params_per_type[1].enabled           = 1;
3123
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
3124
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
3125
0
        scs->tf_params_per_type[1].modulate_pics     = 4;
3126
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
3127
0
            (1 << scs->static_config.hierarchical_levels),
3128
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
3129
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
3130
0
            (1 << scs->static_config.hierarchical_levels),
3131
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
3132
0
        scs->tf_params_per_type[1].hme_me_level            = 2;
3133
0
        scs->tf_params_per_type[1].half_pel_mode           = 2;
3134
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
3135
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
3136
0
        scs->tf_params_per_type[1].chroma_lvl              = 0;
3137
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = (uint64_t)~0;
3138
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 0;
3139
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 1;
3140
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 1;
3141
0
        scs->tf_params_per_type[1].use_2tap                = 1;
3142
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 1;
3143
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
3144
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 35;
3145
0
        scs->tf_params_per_type[1].me_exit_th              = 16 * 16;
3146
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 4;
3147
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
3148
0
        scs->tf_params_per_type[1].qp_opt                  = 1;
3149
        // L1 TF Params
3150
0
        scs->tf_params_per_type[2].enabled = 0;
3151
0
        break;
3152
0
    case 9:
3153
        // I_SLICE TF Params
3154
0
        scs->tf_params_per_type[0].enabled             = 1;
3155
0
        scs->tf_params_per_type[0].num_future_pics     = 8;
3156
0
        scs->tf_params_per_type[0].modulate_pics       = 0;
3157
0
        scs->tf_params_per_type[0].max_num_future_pics = MIN(
3158
0
            (1 << scs->static_config.hierarchical_levels),
3159
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 0, 1));
3160
0
        scs->tf_params_per_type[0].hme_me_level            = 3;
3161
0
        scs->tf_params_per_type[0].half_pel_mode           = 2;
3162
0
        scs->tf_params_per_type[0].quarter_pel_mode        = 1;
3163
0
        scs->tf_params_per_type[0].eight_pel_mode          = 0;
3164
0
        scs->tf_params_per_type[0].chroma_lvl              = 0;
3165
0
        scs->tf_params_per_type[0].pred_error_32x32_th     = (uint64_t)~0;
3166
0
        scs->tf_params_per_type[0].enable_8x8_pred         = 0;
3167
0
        scs->tf_params_per_type[0].sub_sampling_shift      = 1;
3168
0
        scs->tf_params_per_type[0].avoid_2d_qpel           = 1;
3169
0
        scs->tf_params_per_type[0].use_2tap                = 1;
3170
0
        scs->tf_params_per_type[0].use_intra_for_noise_est = 1;
3171
0
        scs->tf_params_per_type[0].use_8bit_subpel         = 1;
3172
0
        scs->tf_params_per_type[0].use_pred_64x64_only_th  = 35;
3173
0
        scs->tf_params_per_type[0].me_exit_th              = 16 * 16;
3174
0
        scs->tf_params_per_type[0].subpel_early_exit_th    = 4;
3175
0
        scs->tf_params_per_type[0].ref_frame_factor        = 2;
3176
0
        scs->tf_params_per_type[0].qp_opt                  = 1;
3177
        // BASE TF Params
3178
0
        scs->tf_params_per_type[1].enabled           = 1;
3179
0
        scs->tf_params_per_type[1].num_past_pics     = 1;
3180
0
        scs->tf_params_per_type[1].num_future_pics   = 1;
3181
0
        scs->tf_params_per_type[1].modulate_pics     = 4;
3182
0
        scs->tf_params_per_type[1].max_num_past_pics = MIN(
3183
0
            (1 << scs->static_config.hierarchical_levels),
3184
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 0));
3185
0
        scs->tf_params_per_type[1].max_num_future_pics = MIN(
3186
0
            (1 << scs->static_config.hierarchical_levels),
3187
0
            svt_aom_tf_max_ref_per_struct(scs->static_config.hierarchical_levels, 1, 1));
3188
0
        scs->tf_params_per_type[1].hme_me_level            = 3;
3189
0
        scs->tf_params_per_type[1].half_pel_mode           = 2;
3190
0
        scs->tf_params_per_type[1].quarter_pel_mode        = 1;
3191
0
        scs->tf_params_per_type[1].eight_pel_mode          = 0;
3192
0
        scs->tf_params_per_type[1].chroma_lvl              = 0;
3193
0
        scs->tf_params_per_type[1].pred_error_32x32_th     = (uint64_t)~0;
3194
0
        scs->tf_params_per_type[1].enable_8x8_pred         = 0;
3195
0
        scs->tf_params_per_type[1].sub_sampling_shift      = 1;
3196
0
        scs->tf_params_per_type[1].avoid_2d_qpel           = 1;
3197
0
        scs->tf_params_per_type[1].use_2tap                = 1;
3198
0
        scs->tf_params_per_type[1].use_intra_for_noise_est = 1;
3199
0
        scs->tf_params_per_type[1].use_8bit_subpel         = 1;
3200
0
        scs->tf_params_per_type[1].use_pred_64x64_only_th  = 35;
3201
0
        scs->tf_params_per_type[1].me_exit_th              = 16 * 16;
3202
0
        scs->tf_params_per_type[1].subpel_early_exit_th    = 4;
3203
0
        scs->tf_params_per_type[1].ref_frame_factor        = 1;
3204
0
        scs->tf_params_per_type[1].qp_opt                  = 1;
3205
        // L1 TF Params
3206
0
        scs->tf_params_per_type[2].enabled = 0;
3207
0
        break;
3208
0
    default:
3209
0
        assert(0);
3210
0
        break;
3211
533
    }
3212
533
    scs->tf_params_per_type[0].use_zz_based_filter = 0;
3213
533
    scs->tf_params_per_type[1].use_zz_based_filter = 0;
3214
533
    scs->tf_params_per_type[2].use_zz_based_filter = 0;
3215
533
}
3216
3217
/*
3218
 * Derive tune Params; 0: use objective mode params, 1: use subjective mode params
3219
 */
3220
533
static void derive_vq_params(SequenceControlSet* scs) {
3221
533
    VqCtrls* vq_ctrl = &scs->vq_ctrls;
3222
3223
533
    if (scs->static_config.tune == TUNE_VQ) {
3224
        // Sharpness
3225
0
        vq_ctrl->sharpness_ctrls.scene_transition = 1;
3226
0
        vq_ctrl->sharpness_ctrls.tf               = 1;
3227
0
        vq_ctrl->sharpness_ctrls.unipred_bias     = 1;
3228
0
        vq_ctrl->sharpness_ctrls.ifs              = 1;
3229
0
        vq_ctrl->sharpness_ctrls.cdef             = 1;
3230
0
        vq_ctrl->sharpness_ctrls.restoration      = 1;
3231
0
        vq_ctrl->sharpness_ctrls.rdoq             = 1;
3232
533
    } else {
3233
        // Sharpness
3234
533
        vq_ctrl->sharpness_ctrls.scene_transition = 1;
3235
533
        vq_ctrl->sharpness_ctrls.tf               = 0;
3236
533
        vq_ctrl->sharpness_ctrls.unipred_bias     = 0;
3237
533
        vq_ctrl->sharpness_ctrls.ifs              = 0;
3238
533
        vq_ctrl->sharpness_ctrls.cdef             = 0;
3239
533
        vq_ctrl->sharpness_ctrls.restoration      = 0;
3240
533
        vq_ctrl->sharpness_ctrls.rdoq             = 0;
3241
533
    }
3242
    // Do not use scene_transition if LD or 1st pass or middle pass
3243
533
    if (scs->static_config.pred_structure != RANDOM_ACCESS || scs->static_config.pass == ENC_FIRST_PASS) {
3244
533
        vq_ctrl->sharpness_ctrls.scene_transition = 0;
3245
533
    }
3246
533
}
3247
3248
/*
3249
 * Derive TF Params
3250
 */
3251
533
static void derive_tf_params(SequenceControlSet* scs) {
3252
533
    const uint32_t hierarchical_levels = scs->static_config.hierarchical_levels;
3253
    // Do not perform TF if LD or 1 Layer or 1st pass
3254
533
    const bool    do_tf    = scs->static_config.enable_tf && hierarchical_levels >= 1 && !scs->static_config.lossless;
3255
533
    const EncMode enc_mode = scs->static_config.enc_mode;
3256
533
    uint8_t       tf_level = 0;
3257
533
    if (scs->static_config.pred_structure == LOW_DELAY) {
3258
0
#if TUNE_SIMPLIFY_SETTINGS
3259
        // TF disabled for all LD
3260
0
        tf_level = 0;
3261
#else
3262
        // For LD, only use TF for non-SC content in RTC mode; the TF is tuned for RTC content
3263
        if (!do_tf || scs->static_config.screen_content_mode == 1 || !scs->static_config.rtc) {
3264
            tf_level = 0;
3265
        } else if ((!scs->use_flat_ipp && enc_mode <= ENC_M7) || (scs->use_flat_ipp && enc_mode <= ENC_M6)) {
3266
            tf_level = 1;
3267
        } else if ((!scs->use_flat_ipp && enc_mode <= ENC_M8) || (scs->use_flat_ipp && enc_mode <= ENC_M7)) {
3268
            tf_level = 2;
3269
        } else {
3270
            tf_level = 0;
3271
        }
3272
#endif
3273
0
        tf_ld_controls(scs, tf_level);
3274
0
        return;
3275
0
    }
3276
533
    if (do_tf == 0) {
3277
533
        tf_level = 0;
3278
533
    } else if (enc_mode <= ENC_M1) {
3279
0
        tf_level = 1;
3280
0
    } else if (enc_mode <= ENC_M2) {
3281
0
        tf_level = 2;
3282
0
    } else if (enc_mode <= ENC_M7) {
3283
0
        tf_level = 5;
3284
0
    } else {
3285
0
        tf_level = 9;
3286
0
    }
3287
533
    tf_controls(scs, tf_level);
3288
533
}
3289
3290
/*
3291
 * Set the MRP control
3292
 */
3293
533
static void set_mrp_ctrl(SequenceControlSet* scs, uint8_t mrp_level) {
3294
533
    MrpCtrls* mrp_ctrl = &scs->mrp_ctrls;
3295
3296
533
    switch (mrp_level) {
3297
0
    case 0:
3298
0
        mrp_ctrl->referencing_scheme = 0;
3299
#if !TUNE_SIMPLIFY_SETTINGS
3300
        mrp_ctrl->sc_base_ref_list0_count     = 1;
3301
        mrp_ctrl->sc_base_ref_list1_count     = 0;
3302
        mrp_ctrl->sc_non_base_ref_list0_count = 1;
3303
        mrp_ctrl->sc_non_base_ref_list1_count = 0;
3304
#endif
3305
0
        mrp_ctrl->base_ref_list0_count        = 1;
3306
0
        mrp_ctrl->base_ref_list1_count        = 0;
3307
0
        mrp_ctrl->non_base_ref_list0_count    = 1;
3308
0
        mrp_ctrl->non_base_ref_list1_count    = 0;
3309
0
        mrp_ctrl->more_5L_refs                = 0;
3310
0
        mrp_ctrl->safe_limit_nref             = 0;
3311
0
        mrp_ctrl->safe_limit_zz_th            = 0;
3312
0
        mrp_ctrl->only_l_bwd                  = 0;
3313
0
        mrp_ctrl->pme_ref0_only               = 0;
3314
0
        mrp_ctrl->use_best_references         = 0;
3315
0
        break;
3316
3317
0
    case 1:
3318
0
        mrp_ctrl->referencing_scheme = 1;
3319
#if !TUNE_SIMPLIFY_SETTINGS
3320
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3321
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3322
        mrp_ctrl->sc_non_base_ref_list0_count = 2;
3323
        mrp_ctrl->sc_non_base_ref_list1_count = 2;
3324
#endif
3325
0
        mrp_ctrl->base_ref_list0_count        = 4;
3326
0
        mrp_ctrl->base_ref_list1_count        = 3;
3327
0
        mrp_ctrl->non_base_ref_list0_count    = 4;
3328
0
        mrp_ctrl->non_base_ref_list1_count    = 3;
3329
0
        mrp_ctrl->more_5L_refs                = 1;
3330
0
        mrp_ctrl->safe_limit_nref             = 0;
3331
0
        mrp_ctrl->safe_limit_zz_th            = 0;
3332
0
        mrp_ctrl->only_l_bwd                  = 0;
3333
0
        mrp_ctrl->pme_ref0_only               = 0;
3334
0
        mrp_ctrl->use_best_references         = 0;
3335
0
        break;
3336
3337
0
    case 2:
3338
0
        mrp_ctrl->referencing_scheme = 1;
3339
#if !TUNE_SIMPLIFY_SETTINGS
3340
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3341
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3342
        mrp_ctrl->sc_non_base_ref_list0_count = 2;
3343
        mrp_ctrl->sc_non_base_ref_list1_count = 2;
3344
#endif
3345
0
        mrp_ctrl->base_ref_list0_count        = 4;
3346
0
        mrp_ctrl->base_ref_list1_count        = 3;
3347
0
        mrp_ctrl->non_base_ref_list0_count    = 4;
3348
0
        mrp_ctrl->non_base_ref_list1_count    = 3;
3349
0
        mrp_ctrl->more_5L_refs                = 1;
3350
0
        mrp_ctrl->safe_limit_nref             = 0;
3351
0
        mrp_ctrl->safe_limit_zz_th            = 0;
3352
0
        mrp_ctrl->only_l_bwd                  = 1;
3353
0
        mrp_ctrl->pme_ref0_only               = 0;
3354
0
        mrp_ctrl->use_best_references         = 0;
3355
0
        break;
3356
0
    case 3:
3357
0
        mrp_ctrl->referencing_scheme = 1;
3358
#if !TUNE_SIMPLIFY_SETTINGS
3359
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3360
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3361
        mrp_ctrl->sc_non_base_ref_list0_count = 2;
3362
        mrp_ctrl->sc_non_base_ref_list1_count = 2;
3363
#endif
3364
0
        mrp_ctrl->base_ref_list0_count        = 4;
3365
0
        mrp_ctrl->base_ref_list1_count        = 3;
3366
0
        mrp_ctrl->non_base_ref_list0_count    = 4;
3367
0
        mrp_ctrl->non_base_ref_list1_count    = 3;
3368
0
        mrp_ctrl->more_5L_refs                = 1;
3369
0
        mrp_ctrl->safe_limit_nref             = 0;
3370
0
        mrp_ctrl->safe_limit_zz_th            = 0;
3371
0
        mrp_ctrl->only_l_bwd                  = 1;
3372
0
        mrp_ctrl->pme_ref0_only               = 0;
3373
0
        mrp_ctrl->use_best_references         = 2;
3374
0
        break;
3375
0
    case 4:
3376
0
        mrp_ctrl->referencing_scheme = 1;
3377
#if !TUNE_SIMPLIFY_SETTINGS
3378
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3379
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3380
        mrp_ctrl->sc_non_base_ref_list0_count = 2;
3381
        mrp_ctrl->sc_non_base_ref_list1_count = 2;
3382
#endif
3383
0
        mrp_ctrl->base_ref_list0_count        = 4;
3384
0
        mrp_ctrl->base_ref_list1_count        = 3;
3385
0
        mrp_ctrl->non_base_ref_list0_count    = 4;
3386
0
        mrp_ctrl->non_base_ref_list1_count    = 3;
3387
0
        mrp_ctrl->more_5L_refs                = 1;
3388
0
        mrp_ctrl->safe_limit_nref             = 1;
3389
0
        mrp_ctrl->safe_limit_zz_th            = 60000;
3390
0
        mrp_ctrl->only_l_bwd                  = 1;
3391
0
        mrp_ctrl->pme_ref0_only               = 1;
3392
0
        mrp_ctrl->use_best_references         = 3;
3393
0
        break;
3394
0
    case 5:
3395
0
        mrp_ctrl->referencing_scheme = 0;
3396
#if !TUNE_SIMPLIFY_SETTINGS
3397
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3398
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3399
        mrp_ctrl->sc_non_base_ref_list0_count = 2;
3400
        mrp_ctrl->sc_non_base_ref_list1_count = 2;
3401
#endif
3402
0
        mrp_ctrl->base_ref_list0_count        = 4;
3403
0
        mrp_ctrl->base_ref_list1_count        = 3;
3404
0
        mrp_ctrl->non_base_ref_list0_count    = 4;
3405
0
        mrp_ctrl->non_base_ref_list1_count    = 3;
3406
0
        mrp_ctrl->more_5L_refs                = 0;
3407
0
        mrp_ctrl->safe_limit_nref             = 2;
3408
0
        mrp_ctrl->safe_limit_zz_th            = 60000;
3409
0
        mrp_ctrl->only_l_bwd                  = 1;
3410
0
        mrp_ctrl->pme_ref0_only               = 1;
3411
0
        mrp_ctrl->use_best_references         = 3;
3412
0
        break;
3413
0
    case 6:
3414
0
        mrp_ctrl->referencing_scheme = 0;
3415
#if !TUNE_SIMPLIFY_SETTINGS
3416
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3417
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3418
        mrp_ctrl->sc_non_base_ref_list0_count = 1;
3419
        mrp_ctrl->sc_non_base_ref_list1_count = 1;
3420
#endif
3421
0
        mrp_ctrl->base_ref_list0_count        = 3;
3422
0
        mrp_ctrl->base_ref_list1_count        = 2;
3423
0
        mrp_ctrl->non_base_ref_list0_count    = 3;
3424
0
        mrp_ctrl->non_base_ref_list1_count    = 2;
3425
0
        mrp_ctrl->more_5L_refs                = 0;
3426
0
        mrp_ctrl->safe_limit_nref             = 2;
3427
0
        mrp_ctrl->safe_limit_zz_th            = 60000;
3428
0
        mrp_ctrl->only_l_bwd                  = 1;
3429
0
        mrp_ctrl->pme_ref0_only               = 1;
3430
0
        mrp_ctrl->use_best_references         = 3;
3431
0
        break;
3432
0
    case 7:
3433
0
        mrp_ctrl->referencing_scheme = 0;
3434
#if !TUNE_SIMPLIFY_SETTINGS
3435
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3436
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3437
        mrp_ctrl->sc_non_base_ref_list0_count = 1;
3438
        mrp_ctrl->sc_non_base_ref_list1_count = 1;
3439
#endif
3440
0
        mrp_ctrl->base_ref_list0_count        = 3;
3441
0
        mrp_ctrl->base_ref_list1_count        = 2;
3442
0
        mrp_ctrl->non_base_ref_list0_count    = 2;
3443
0
        mrp_ctrl->non_base_ref_list1_count    = 2;
3444
0
        mrp_ctrl->more_5L_refs                = 0;
3445
0
        mrp_ctrl->safe_limit_nref             = 2;
3446
0
        mrp_ctrl->safe_limit_zz_th            = 60000;
3447
0
        mrp_ctrl->only_l_bwd                  = 1;
3448
0
        mrp_ctrl->pme_ref0_only               = 1;
3449
0
        mrp_ctrl->use_best_references         = 3;
3450
0
        break;
3451
0
    case 8:
3452
0
        mrp_ctrl->referencing_scheme = 0;
3453
#if !TUNE_SIMPLIFY_SETTINGS
3454
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3455
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3456
        mrp_ctrl->sc_non_base_ref_list0_count = 1;
3457
        mrp_ctrl->sc_non_base_ref_list1_count = 1;
3458
#endif
3459
0
        mrp_ctrl->base_ref_list0_count        = 2;
3460
0
        mrp_ctrl->base_ref_list1_count        = 2;
3461
0
        mrp_ctrl->non_base_ref_list0_count    = 2;
3462
0
        mrp_ctrl->non_base_ref_list1_count    = 2;
3463
0
        mrp_ctrl->more_5L_refs                = 0;
3464
0
        mrp_ctrl->safe_limit_nref             = 2;
3465
0
        mrp_ctrl->safe_limit_zz_th            = 60000;
3466
0
        mrp_ctrl->only_l_bwd                  = 1;
3467
0
        mrp_ctrl->pme_ref0_only               = 1;
3468
0
        mrp_ctrl->use_best_references         = 3;
3469
0
        break;
3470
533
    case 9:
3471
533
        mrp_ctrl->referencing_scheme = 0;
3472
#if !TUNE_SIMPLIFY_SETTINGS
3473
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3474
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3475
        mrp_ctrl->sc_non_base_ref_list0_count = 1;
3476
        mrp_ctrl->sc_non_base_ref_list1_count = 1;
3477
#endif
3478
533
        mrp_ctrl->base_ref_list0_count        = 3;
3479
533
        mrp_ctrl->base_ref_list1_count        = 2;
3480
533
        mrp_ctrl->non_base_ref_list0_count    = 1;
3481
533
        mrp_ctrl->non_base_ref_list1_count    = 1;
3482
533
        mrp_ctrl->more_5L_refs                = 0;
3483
533
        mrp_ctrl->safe_limit_nref             = 2;
3484
533
        mrp_ctrl->safe_limit_zz_th            = 60000;
3485
533
        mrp_ctrl->only_l_bwd                  = 1;
3486
533
        mrp_ctrl->pme_ref0_only               = 1;
3487
533
        mrp_ctrl->use_best_references         = 3;
3488
533
        break;
3489
0
    case 10:
3490
0
        mrp_ctrl->referencing_scheme = 0;
3491
#if !TUNE_SIMPLIFY_SETTINGS
3492
        mrp_ctrl->sc_base_ref_list0_count     = 2;
3493
        mrp_ctrl->sc_base_ref_list1_count     = 2;
3494
        mrp_ctrl->sc_non_base_ref_list0_count = 1;
3495
        mrp_ctrl->sc_non_base_ref_list1_count = 1;
3496
#endif
3497
0
        mrp_ctrl->base_ref_list0_count        = 2;
3498
0
        mrp_ctrl->base_ref_list1_count        = 2;
3499
0
        mrp_ctrl->non_base_ref_list0_count    = 1;
3500
0
        mrp_ctrl->non_base_ref_list1_count    = 1;
3501
0
        mrp_ctrl->more_5L_refs                = 0;
3502
0
        mrp_ctrl->safe_limit_nref             = 2;
3503
0
        mrp_ctrl->safe_limit_zz_th            = 60000;
3504
0
        mrp_ctrl->only_l_bwd                  = 1;
3505
0
        mrp_ctrl->pme_ref0_only               = 1;
3506
0
        mrp_ctrl->use_best_references         = 3;
3507
0
        break;
3508
0
    case 11:
3509
0
        mrp_ctrl->referencing_scheme = 0;
3510
#if !TUNE_SIMPLIFY_SETTINGS
3511
        mrp_ctrl->sc_base_ref_list0_count     = 1;
3512
        mrp_ctrl->sc_base_ref_list1_count     = 1;
3513
        mrp_ctrl->sc_non_base_ref_list0_count = 1;
3514
        mrp_ctrl->sc_non_base_ref_list1_count = 1;
3515
#endif
3516
0
        mrp_ctrl->base_ref_list0_count        = 1;
3517
0
        mrp_ctrl->base_ref_list1_count        = 1;
3518
0
        mrp_ctrl->non_base_ref_list0_count    = 1;
3519
0
        mrp_ctrl->non_base_ref_list1_count    = 1;
3520
0
        mrp_ctrl->more_5L_refs                = 0;
3521
0
        mrp_ctrl->safe_limit_nref             = 0;
3522
0
        mrp_ctrl->safe_limit_zz_th            = 0;
3523
0
        mrp_ctrl->only_l_bwd                  = 0;
3524
0
        mrp_ctrl->pme_ref0_only               = 0;
3525
0
        mrp_ctrl->use_best_references         = 0;
3526
0
        break;
3527
0
    default:
3528
0
        assert(0);
3529
0
        break;
3530
533
    }
3531
    // For low delay mode, list1 references are not used
3532
533
    if (scs->static_config.pred_structure == LOW_DELAY && scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR) {
3533
#if !TUNE_SIMPLIFY_SETTINGS
3534
        mrp_ctrl->sc_base_ref_list1_count     = 0;
3535
        mrp_ctrl->sc_non_base_ref_list1_count = 0;
3536
#endif
3537
0
        mrp_ctrl->base_ref_list1_count     = 0;
3538
0
        mrp_ctrl->non_base_ref_list1_count = 0;
3539
0
        if (scs->use_flat_ipp) {
3540
0
            mrp_ctrl->referencing_scheme  = 0;
3541
0
            mrp_ctrl->more_5L_refs        = 0;
3542
0
            mrp_ctrl->safe_limit_nref     = 0;
3543
0
            mrp_ctrl->only_l_bwd          = 0;
3544
0
            mrp_ctrl->pme_ref0_only       = 0;
3545
0
            mrp_ctrl->use_best_references = 0;
3546
0
        }
3547
0
    }
3548
533
    if (scs->static_config.pred_structure == LOW_DELAY) {
3549
0
        if (scs->use_flat_ipp) {
3550
0
#if TUNE_SIMPLIFY_SETTINGS
3551
0
            mrp_ctrl->flat_max_refs = MAX(MAX(mrp_ctrl->base_ref_list0_count, mrp_ctrl->base_ref_list1_count),
3552
0
                                          MAX(mrp_ctrl->non_base_ref_list0_count, mrp_ctrl->non_base_ref_list1_count));
3553
#else
3554
            const uint8_t max_sc_refs = MAX(
3555
                MAX(scs->mrp_ctrls.sc_non_base_ref_list0_count, scs->mrp_ctrls.sc_non_base_ref_list1_count),
3556
                MAX(scs->mrp_ctrls.sc_base_ref_list0_count, scs->mrp_ctrls.sc_base_ref_list1_count));
3557
            const uint8_t max_nsc_refs = MAX(
3558
                MAX(scs->mrp_ctrls.base_ref_list0_count, scs->mrp_ctrls.base_ref_list1_count),
3559
                MAX(scs->mrp_ctrls.non_base_ref_list0_count, scs->mrp_ctrls.non_base_ref_list1_count));
3560
            mrp_ctrl->flat_max_refs = scs->static_config.screen_content_mode == 2 ? MAX(max_sc_refs, max_nsc_refs)
3561
                : scs->static_config.screen_content_mode == 1                     ? max_sc_refs
3562
                                                                                  : max_nsc_refs;
3563
#endif
3564
0
        }
3565
3566
0
#if TUNE_SIMPLIFY_SETTINGS
3567
0
        mrp_ctrl->ld_reduce_ref_buffs = (mrp_ctrl->base_ref_list0_count <= 1 && mrp_ctrl->base_ref_list1_count <= 1 &&
3568
0
                                         mrp_ctrl->non_base_ref_list0_count <= 1 &&
3569
0
                                         mrp_ctrl->non_base_ref_list1_count <= 1)
3570
0
            ? 2
3571
0
            : (mrp_ctrl->base_ref_list0_count <= 2 && mrp_ctrl->base_ref_list1_count <= 2 &&
3572
0
               mrp_ctrl->non_base_ref_list0_count <= 2 && mrp_ctrl->non_base_ref_list1_count <= 2)
3573
0
            ? 1
3574
0
            : 0;
3575
#else
3576
        // If content type (SC/NSC) is known, can allocate refs based on settings, else consider worst-case
3577
        if (scs->static_config.screen_content_mode == 1) {
3578
            mrp_ctrl->ld_reduce_ref_buffs = (mrp_ctrl->sc_base_ref_list0_count <= 1 &&
3579
                                             mrp_ctrl->sc_base_ref_list1_count <= 1 &&
3580
                                             mrp_ctrl->sc_non_base_ref_list0_count <= 1 &&
3581
                                             mrp_ctrl->sc_non_base_ref_list1_count <= 1)
3582
                ? 2
3583
                : (mrp_ctrl->sc_base_ref_list0_count <= 2 && mrp_ctrl->sc_base_ref_list1_count <= 2 &&
3584
                   mrp_ctrl->sc_non_base_ref_list0_count <= 2 && mrp_ctrl->sc_non_base_ref_list1_count <= 2)
3585
                ? 1
3586
                : 0;
3587
        } else if (scs->static_config.screen_content_mode == 0) {
3588
            mrp_ctrl->ld_reduce_ref_buffs = (mrp_ctrl->base_ref_list0_count <= 1 &&
3589
                                             mrp_ctrl->base_ref_list1_count <= 1 &&
3590
                                             mrp_ctrl->non_base_ref_list0_count <= 1 &&
3591
                                             mrp_ctrl->non_base_ref_list1_count <= 1)
3592
                ? 2
3593
                : (mrp_ctrl->base_ref_list0_count <= 2 && mrp_ctrl->base_ref_list1_count <= 2 &&
3594
                   mrp_ctrl->non_base_ref_list0_count <= 2 && mrp_ctrl->non_base_ref_list1_count <= 2)
3595
                ? 1
3596
                : 0;
3597
        } else {
3598
            mrp_ctrl->ld_reduce_ref_buffs = (mrp_ctrl->sc_base_ref_list0_count <= 1 &&
3599
                                             mrp_ctrl->sc_base_ref_list1_count <= 1 &&
3600
                                             mrp_ctrl->sc_non_base_ref_list0_count <= 1 &&
3601
                                             mrp_ctrl->sc_non_base_ref_list1_count <= 1) &&
3602
                    (mrp_ctrl->base_ref_list0_count <= 1 && mrp_ctrl->base_ref_list1_count <= 1 &&
3603
                     mrp_ctrl->non_base_ref_list0_count <= 1 && mrp_ctrl->non_base_ref_list1_count <= 1)
3604
                ? 2
3605
                : (mrp_ctrl->sc_base_ref_list0_count <= 2 && mrp_ctrl->sc_base_ref_list1_count <= 2 &&
3606
                   mrp_ctrl->sc_non_base_ref_list0_count <= 2 && mrp_ctrl->sc_non_base_ref_list1_count <= 2) &&
3607
                    (mrp_ctrl->base_ref_list0_count <= 2 && mrp_ctrl->base_ref_list1_count <= 2 &&
3608
                     mrp_ctrl->non_base_ref_list0_count <= 2 && mrp_ctrl->non_base_ref_list1_count <= 2)
3609
                ? 1
3610
                : 0;
3611
        }
3612
#endif
3613
533
    } else {
3614
533
        mrp_ctrl->ld_reduce_ref_buffs = 0;
3615
533
    }
3616
533
}
3617
3618
static uint8_t get_tpl(uint8_t pred_structure, uint8_t superres_mode, uint8_t resize_mode, uint8_t aq_mode,
3619
533
                       bool allintra) {
3620
533
    if (allintra) {
3621
533
        SVT_WARN("TPL is disabled for all-intra coding\n");
3622
533
        return 0;
3623
533
    } else if (aq_mode == 0) {
3624
0
        SVT_WARN("TPL is disabled for aq_mode 0\n");
3625
0
        return 0;
3626
0
    } else if (pred_structure == LOW_DELAY) {
3627
0
        SVT_WARN("TPL is disabled in low delay applications.\n");
3628
0
        return 0;
3629
0
    }
3630
    // allow TPL with auto-dual and auto-all
3631
0
    else if (superres_mode > SUPERRES_NONE && superres_mode != SUPERRES_AUTO && superres_mode != SUPERRES_QTHRESH) {
3632
0
        SVT_WARN("TPL will be disabled when super resolution is enabled!\n");
3633
0
        return 0;
3634
0
    } else if (resize_mode > RESIZE_NONE) {
3635
0
        SVT_WARN("TPL will be disabled when reference scalings (resize) is enabled!\n");
3636
0
        return 0;
3637
0
    } else {
3638
0
        return 1;
3639
0
    }
3640
533
}
3641
3642
/*
3643
* Set multi Pass Params
3644
*/
3645
533
void set_multi_pass_params(SequenceControlSet* scs) {
3646
533
    EbSvtAv1EncConfiguration* config = &scs->static_config;
3647
3648
    // Update passes
3649
533
    if (scs->static_config.pass != ENC_SINGLE_PASS) {
3650
0
        scs->passes = MAX_ENCODE_PASS;
3651
533
    } else {
3652
533
        scs->passes = 1;
3653
533
    }
3654
3655
533
    switch (config->pass) {
3656
533
    case ENC_SINGLE_PASS: {
3657
533
        scs->first_pass_downsample = false;
3658
533
        scs->final_pass_preset     = config->enc_mode;
3659
533
        break;
3660
0
    }
3661
0
    case ENC_FIRST_PASS: {
3662
0
        scs->first_pass_downsample = config->enc_mode > ENC_M8;
3663
0
        scs->final_pass_preset     = config->enc_mode;
3664
0
        if (scs->final_pass_preset <= ENC_M6) {
3665
0
            scs->static_config.enc_mode = ENC_M9;
3666
0
        } else {
3667
0
            scs->static_config.enc_mode = MAX_ENC_PRESET;
3668
0
        }
3669
0
        scs->static_config.rate_control_mode  = SVT_AV1_RC_MODE_CQP_OR_CRF;
3670
0
        scs->static_config.qp                 = 43;
3671
0
        scs->static_config.intra_refresh_type = SVT_AV1_KF_REFRESH;
3672
0
        scs->static_config.max_bit_rate       = 0;
3673
0
        break;
3674
0
    }
3675
0
    case ENC_SECOND_PASS: {
3676
0
        scs->final_pass_preset                = config->enc_mode;
3677
0
        scs->static_config.intra_refresh_type = SVT_AV1_KF_REFRESH;
3678
0
        break;
3679
0
    }
3680
0
    default: {
3681
0
        assert(0);
3682
0
        break;
3683
0
    }
3684
533
    }
3685
3686
533
    int do_downsample = scs->first_pass_downsample && scs->max_input_luma_width >= 128 &&
3687
0
            scs->max_input_luma_height >= 128
3688
533
        ? 1
3689
533
        : 0;
3690
3691
533
    if (do_downsample) {
3692
        // To make sure the down scaled video has width and height of multiple of 2
3693
0
        scs->max_input_luma_width  = (scs->max_input_luma_width >> 2) << 1;
3694
0
        scs->max_input_luma_height = (scs->max_input_luma_height >> 2) << 1;
3695
0
    }
3696
3697
533
    if (scs->lap_rc) {
3698
0
        scs->static_config.intra_refresh_type = SVT_AV1_KF_REFRESH;
3699
0
    }
3700
533
    if (scs->static_config.pass == ENC_FIRST_PASS && scs->final_pass_preset > ENC_M6) {
3701
0
        scs->rc_stat_gen_pass_mode = 1;
3702
533
    } else {
3703
533
        scs->rc_stat_gen_pass_mode = 0;
3704
533
    }
3705
3706
533
    if (scs->static_config.recode_loop > 0 &&
3707
533
        ((scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CQP_OR_CRF && scs->static_config.max_bit_rate == 0) ||
3708
533
         (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR && !scs->static_config.rtc))) {
3709
        // Only allow re-encoding for VBR, RTC CBR or capped CRF, otherwise force recode_loop to DISALLOW_RECODE
3710
533
        scs->static_config.recode_loop = DISALLOW_RECODE;
3711
533
    } else if (scs->static_config.recode_loop == ALLOW_RECODE_DEFAULT) {
3712
        // capped CRF has reencde enabled for base layer frames for all presets
3713
0
        if (!scs->static_config.rate_control_mode && scs->static_config.max_bit_rate) {
3714
0
            scs->static_config.recode_loop = ALLOW_RECODE_KFARFGF;
3715
0
        } else {
3716
0
            scs->static_config.recode_loop = scs->static_config.enc_mode <= ENC_M3 ? ALLOW_RECODE_KFARFGF
3717
0
                                                                                   : ALLOW_RECODE_KFMAXBW;
3718
0
        }
3719
0
    }
3720
3721
533
    scs->enc_ctx->recode_loop = scs->static_config.recode_loop;
3722
533
}
3723
3724
533
static void validate_scaling_params(SequenceControlSet* scs) {
3725
533
    if (scs->static_config.superres_mode == SUPERRES_FIXED && scs->static_config.superres_denom == SCALE_NUMERATOR &&
3726
0
        scs->static_config.superres_kf_denom == SCALE_NUMERATOR) {
3727
0
        scs->static_config.superres_mode = SUPERRES_NONE;
3728
0
    }
3729
533
    if (scs->static_config.resize_mode == RESIZE_DYNAMIC) {
3730
0
        if (scs->static_config.pred_structure != LOW_DELAY || scs->static_config.pass != ENC_SINGLE_PASS ||
3731
0
            scs->static_config.rate_control_mode != SVT_AV1_RC_MODE_CBR) {
3732
0
            SVT_WARN("Resize dynamic mode only works at 1-pass CBR low delay mode!\n");
3733
0
            scs->static_config.resize_mode = RESIZE_NONE;
3734
0
        }
3735
0
    }
3736
533
    if (scs->static_config.superres_mode == SUPERRES_QTHRESH && scs->static_config.superres_qthres == MAX_QP_VALUE &&
3737
0
        scs->static_config.superres_kf_qthres == MAX_QP_VALUE) {
3738
0
        scs->static_config.superres_mode = SUPERRES_NONE;
3739
0
    }
3740
533
    if (scs->static_config.resize_mode == RESIZE_FIXED && scs->static_config.resize_denom == SCALE_NUMERATOR &&
3741
0
        scs->static_config.resize_kf_denom == SCALE_NUMERATOR) {
3742
0
        scs->static_config.resize_mode = RESIZE_NONE;
3743
0
    }
3744
533
}
3745
3746
0
void set_qp_based_th_scaling_ctrls_default(SequenceControlSet* scs) {
3747
0
    QpBasedThScaling* qp_ctrls = &scs->qp_based_th_scaling_ctrls;
3748
0
    const EncMode     enc_mode = scs->static_config.enc_mode;
3749
3750
0
    if (enc_mode <= ENC_MR) {
3751
0
        qp_ctrls->tf_me_qp_based_th_scaling        = 0;
3752
0
        qp_ctrls->tf_ref_qp_based_th_scaling       = 0;
3753
0
        qp_ctrls->depths_qp_based_th_scaling       = 0;
3754
0
        qp_ctrls->hme_qp_based_th_scaling          = 0;
3755
0
        qp_ctrls->me_qp_based_th_scaling           = 0;
3756
0
        qp_ctrls->nsq_qp_based_th_scaling          = 0;
3757
0
        qp_ctrls->nic_max_qp_based_th_scaling      = 0;
3758
0
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 0;
3759
0
        qp_ctrls->pme_qp_based_th_scaling          = 0;
3760
0
        qp_ctrls->txt_qp_based_th_scaling          = 0;
3761
0
        qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3762
0
        qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3763
0
        qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3764
0
    } else {
3765
0
        qp_ctrls->tf_me_qp_based_th_scaling        = 1;
3766
0
        qp_ctrls->tf_ref_qp_based_th_scaling       = 1;
3767
0
        qp_ctrls->depths_qp_based_th_scaling       = 1;
3768
0
        qp_ctrls->hme_qp_based_th_scaling          = 1;
3769
0
        qp_ctrls->me_qp_based_th_scaling           = 1;
3770
0
        qp_ctrls->nsq_qp_based_th_scaling          = 1;
3771
0
        qp_ctrls->nic_max_qp_based_th_scaling      = 1;
3772
0
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 1;
3773
0
        qp_ctrls->pme_qp_based_th_scaling          = 1;
3774
0
        qp_ctrls->txt_qp_based_th_scaling          = 1;
3775
0
        qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3776
0
        qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3777
0
        qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3778
0
    }
3779
0
}
3780
3781
0
void set_qp_based_th_scaling_ctrls_rtc(SequenceControlSet* scs) {
3782
0
    QpBasedThScaling* qp_ctrls = &scs->qp_based_th_scaling_ctrls;
3783
0
#if TUNE_SIMPLIFY_SETTINGS
3784
0
    qp_ctrls->tf_me_qp_based_th_scaling        = 1;
3785
0
    qp_ctrls->tf_ref_qp_based_th_scaling       = 1;
3786
0
    qp_ctrls->depths_qp_based_th_scaling       = 1;
3787
0
    qp_ctrls->hme_qp_based_th_scaling          = 1;
3788
0
    qp_ctrls->me_qp_based_th_scaling           = 1;
3789
0
    qp_ctrls->nsq_qp_based_th_scaling          = 1;
3790
0
    qp_ctrls->nic_max_qp_based_th_scaling      = 1;
3791
0
    qp_ctrls->nic_pruning_qp_based_th_scaling  = 1;
3792
0
    qp_ctrls->pme_qp_based_th_scaling          = 1;
3793
0
    qp_ctrls->txt_qp_based_th_scaling          = 1;
3794
0
    qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3795
0
    qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3796
0
    qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3797
#else
3798
    const EncMode enc_mode = scs->static_config.enc_mode;
3799
3800
    if (enc_mode <= ENC_MR) {
3801
        qp_ctrls->tf_me_qp_based_th_scaling        = 0;
3802
        qp_ctrls->tf_ref_qp_based_th_scaling       = 0;
3803
        qp_ctrls->depths_qp_based_th_scaling       = 0;
3804
        qp_ctrls->hme_qp_based_th_scaling          = 0;
3805
        qp_ctrls->me_qp_based_th_scaling           = 0;
3806
        qp_ctrls->nsq_qp_based_th_scaling          = 0;
3807
        qp_ctrls->nic_max_qp_based_th_scaling      = 0;
3808
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 0;
3809
        qp_ctrls->pme_qp_based_th_scaling          = 0;
3810
        qp_ctrls->txt_qp_based_th_scaling          = 0;
3811
        qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3812
        qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3813
        qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3814
    } else {
3815
        qp_ctrls->tf_me_qp_based_th_scaling        = 1;
3816
        qp_ctrls->tf_ref_qp_based_th_scaling       = 1;
3817
        qp_ctrls->depths_qp_based_th_scaling       = 1;
3818
        qp_ctrls->hme_qp_based_th_scaling          = 1;
3819
        qp_ctrls->me_qp_based_th_scaling           = 1;
3820
        qp_ctrls->nsq_qp_based_th_scaling          = 1;
3821
        qp_ctrls->nic_max_qp_based_th_scaling      = 1;
3822
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 1;
3823
        qp_ctrls->pme_qp_based_th_scaling          = 1;
3824
        qp_ctrls->txt_qp_based_th_scaling          = 1;
3825
        qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3826
        qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3827
        qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3828
    }
3829
#endif
3830
0
}
3831
3832
533
void set_qp_based_th_scaling_ctrls_all_intra(SequenceControlSet* scs) {
3833
533
    QpBasedThScaling* qp_ctrls = &scs->qp_based_th_scaling_ctrls;
3834
533
    const EncMode     enc_mode = scs->static_config.enc_mode;
3835
3836
533
#if FIX_MR_STILL_IMAGE
3837
533
    if (enc_mode <= ENC_MR) {
3838
0
        qp_ctrls->tf_me_qp_based_th_scaling        = 0;
3839
0
        qp_ctrls->tf_ref_qp_based_th_scaling       = 0;
3840
0
        qp_ctrls->depths_qp_based_th_scaling       = 0;
3841
0
        qp_ctrls->hme_qp_based_th_scaling          = 0;
3842
0
        qp_ctrls->me_qp_based_th_scaling           = 0;
3843
0
        qp_ctrls->nsq_qp_based_th_scaling          = 0;
3844
0
        qp_ctrls->nic_max_qp_based_th_scaling      = 1;
3845
0
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 1;
3846
0
        qp_ctrls->pme_qp_based_th_scaling          = 0;
3847
0
        qp_ctrls->txt_qp_based_th_scaling          = 1;
3848
0
        qp_ctrls->cap_max_size_qp_based_th_scaling = 0;
3849
0
        qp_ctrls->lpd0_qp_based_th_scaling         = 0;
3850
0
        qp_ctrls->intra_bc_mesh_qp_scaling         = 0;
3851
533
    } else if (enc_mode <= ENC_M3) {
3852
#else
3853
    if (enc_mode <= ENC_M3) {
3854
#endif
3855
0
        qp_ctrls->tf_me_qp_based_th_scaling        = 0;
3856
0
        qp_ctrls->tf_ref_qp_based_th_scaling       = 0;
3857
0
        qp_ctrls->depths_qp_based_th_scaling       = 0;
3858
0
        qp_ctrls->hme_qp_based_th_scaling          = 0;
3859
0
        qp_ctrls->me_qp_based_th_scaling           = 0;
3860
0
        qp_ctrls->nsq_qp_based_th_scaling          = 0;
3861
0
        qp_ctrls->nic_max_qp_based_th_scaling      = 1;
3862
0
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 1;
3863
0
        qp_ctrls->pme_qp_based_th_scaling          = 0;
3864
0
        qp_ctrls->txt_qp_based_th_scaling          = 1;
3865
0
        qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3866
0
        qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3867
0
        qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3868
533
    } else if (enc_mode <= ENC_M6) {
3869
0
        qp_ctrls->tf_me_qp_based_th_scaling        = 0;
3870
0
        qp_ctrls->tf_ref_qp_based_th_scaling       = 0;
3871
0
        qp_ctrls->depths_qp_based_th_scaling       = 0;
3872
0
        qp_ctrls->hme_qp_based_th_scaling          = 0;
3873
0
        qp_ctrls->me_qp_based_th_scaling           = 0;
3874
0
        qp_ctrls->nsq_qp_based_th_scaling          = 1;
3875
0
        qp_ctrls->nic_max_qp_based_th_scaling      = 1;
3876
0
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 1;
3877
0
        qp_ctrls->pme_qp_based_th_scaling          = 0;
3878
0
        qp_ctrls->txt_qp_based_th_scaling          = 1;
3879
0
        qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3880
0
        qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3881
0
        qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3882
533
    } else {
3883
533
        qp_ctrls->tf_me_qp_based_th_scaling        = 1;
3884
533
        qp_ctrls->tf_ref_qp_based_th_scaling       = 1;
3885
533
        qp_ctrls->depths_qp_based_th_scaling       = 1;
3886
533
        qp_ctrls->hme_qp_based_th_scaling          = 1;
3887
533
        qp_ctrls->me_qp_based_th_scaling           = 1;
3888
533
        qp_ctrls->nsq_qp_based_th_scaling          = 1;
3889
533
        qp_ctrls->nic_max_qp_based_th_scaling      = 1;
3890
533
        qp_ctrls->nic_pruning_qp_based_th_scaling  = 1;
3891
533
        qp_ctrls->pme_qp_based_th_scaling          = 1;
3892
533
        qp_ctrls->txt_qp_based_th_scaling          = 1;
3893
533
        qp_ctrls->cap_max_size_qp_based_th_scaling = 1;
3894
533
        qp_ctrls->lpd0_qp_based_th_scaling         = 1;
3895
533
        qp_ctrls->intra_bc_mesh_qp_scaling         = 1;
3896
533
    }
3897
533
}
3898
3899
533
static void set_param_based_on_input(SequenceControlSet* scs) {
3900
533
    const bool allintra = scs->allintra;
3901
533
    const bool rtc_tune = scs->static_config.rtc;
3902
533
#if TUNE_SIMPLIFY_SETTINGS
3903
533
    const bool use_flat_ipp = scs->use_flat_ipp;
3904
533
#endif
3905
533
    set_multi_pass_params(scs);
3906
3907
    // superres_mode and resize_mode may be updated,
3908
    // so should call get_tpl_level() after validate_scaling_params()
3909
533
    validate_scaling_params(scs);
3910
533
    scs->tpl               = get_tpl(scs->static_config.pred_structure,
3911
533
                       scs->static_config.superres_mode,
3912
533
                       scs->static_config.resize_mode,
3913
533
                       scs->static_config.aq_mode,
3914
533
                       allintra);
3915
533
    uint16_t subsampling_x = scs->subsampling_x;
3916
533
    uint16_t subsampling_y = scs->subsampling_y;
3917
    // Update picture width, and picture height
3918
533
    if (scs->max_input_luma_width % MIN_BLOCK_SIZE) {
3919
380
        scs->max_input_pad_right  = MIN_BLOCK_SIZE - (scs->max_input_luma_width % MIN_BLOCK_SIZE);
3920
380
        scs->max_input_luma_width = scs->max_input_luma_width + scs->max_input_pad_right;
3921
380
    } else {
3922
153
        scs->max_input_pad_right = 0;
3923
153
    }
3924
3925
533
    if (scs->max_input_luma_height % MIN_BLOCK_SIZE) {
3926
359
        scs->max_input_pad_bottom  = MIN_BLOCK_SIZE - (scs->max_input_luma_height % MIN_BLOCK_SIZE);
3927
359
        scs->max_input_luma_height = scs->max_input_luma_height + scs->max_input_pad_bottom;
3928
359
    } else {
3929
174
        scs->max_input_pad_bottom = 0;
3930
174
    }
3931
533
    scs->max_initial_input_luma_width  = scs->max_input_luma_width;
3932
533
    scs->max_initial_input_luma_height = scs->max_input_luma_height;
3933
533
    scs->max_initial_input_pad_bottom  = scs->max_input_pad_bottom;
3934
533
    scs->max_initial_input_pad_right   = scs->max_input_pad_right;
3935
3936
533
    scs->chroma_width  = scs->max_input_luma_width >> subsampling_x;
3937
533
    scs->chroma_height = scs->max_input_luma_height >> subsampling_y;
3938
3939
533
    scs->static_config.source_width  = scs->max_input_luma_width;
3940
533
    scs->static_config.source_height = scs->max_input_luma_height;
3941
533
    if (scs->static_config.superres_mode > SUPERRES_NONE) {
3942
0
        if (scs->static_config.tile_rows || scs->static_config.tile_columns) {
3943
            // disable tiles if super-res is on
3944
0
            SVT_WARN("Tiles will be disabled when super resolution is enabled!\n");
3945
0
            scs->static_config.tile_rows    = 0;
3946
0
            scs->static_config.tile_columns = 0;
3947
0
        }
3948
0
        if (scs->static_config.superres_mode == SUPERRES_RANDOM) {
3949
0
            SVT_WARN(
3950
0
                "Super resolution random mode is designed for test and debugging purpose,\n"
3951
0
                "it creates array of picture buffers for all scaling denominators (up to 8) of each reference frame.\n"
3952
0
                "This mode retains a significant amount of memory, much more than other modes!\n");
3953
0
        }
3954
0
    }
3955
533
    if (scs->static_config.resize_mode > RESIZE_NONE) {
3956
0
        if (scs->static_config.tile_rows || scs->static_config.tile_columns) {
3957
            // disable tiles if resize is on
3958
0
            SVT_WARN("Tiles will be disabled when resize is enabled!\n");
3959
0
            scs->static_config.tile_rows    = 0;
3960
0
            scs->static_config.tile_columns = 0;
3961
0
        }
3962
0
        if (scs->static_config.resize_mode == RESIZE_RANDOM) {
3963
0
            SVT_WARN(
3964
0
                "Resize random mode is designed for test and debugging purpose,\n"
3965
0
                "it creates array of picture buffers for all scaling denominators (up to 8) of each reference frame.\n"
3966
0
                "This mode retains a significant amount of memory, much more than other modes!\n");
3967
0
        }
3968
0
    }
3969
    // Set initial qp for vbr and middle pass
3970
533
    if ((scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_VBR) || (scs->static_config.pass == ENC_FIRST_PASS)) {
3971
0
        if (scs->static_config.qp != DEFAULT_QP) {
3972
0
            SVT_WARN("The input q value is ignored in vbr mode %d\n", scs->static_config.qp);
3973
0
        }
3974
0
        const uint8_t  tbr_bands[5]    = {1, 2, 4, 6, 8};
3975
0
        const uint64_t src_samples     = (uint64_t)(scs->seq_header.max_frame_width * scs->seq_header.max_frame_height);
3976
0
        const uint64_t target_bit_rate = (uint64_t)((double)scs->static_config.target_bit_rate * 60.0 /
3977
0
                                                    (scs->frame_rate * (double)src_samples));
3978
0
        if (target_bit_rate < tbr_bands[0]) {
3979
0
            scs->static_config.qp = 55;
3980
0
        } else if (target_bit_rate < tbr_bands[1]) {
3981
0
            scs->static_config.qp = 50;
3982
0
        } else if (target_bit_rate < tbr_bands[2]) {
3983
0
            scs->static_config.qp = 45;
3984
0
        } else if (target_bit_rate < tbr_bands[3]) {
3985
0
            scs->static_config.qp = 40;
3986
0
        } else if (target_bit_rate < tbr_bands[4]) {
3987
0
            scs->static_config.qp = 35;
3988
0
        } else {
3989
0
            scs->static_config.qp = 30;
3990
0
        }
3991
0
    }
3992
533
    svt_aom_derive_input_resolution(&scs->input_resolution, scs->max_input_luma_width * scs->max_input_luma_height);
3993
3994
533
    scs->seq_qp_mod = 2;
3995
3996
533
    (allintra       ? set_qp_based_th_scaling_ctrls_all_intra
3997
533
         : rtc_tune ? set_qp_based_th_scaling_ctrls_rtc
3998
0
                    : set_qp_based_th_scaling_ctrls_default)(scs);
3999
4000
    // Set tune params
4001
533
    derive_vq_params(scs);
4002
4003
    // Set TF level
4004
533
    derive_tf_params(scs);
4005
4006
    //Future frames window in Scene Change Detection (SCD) / TemporalFiltering
4007
533
    scs->scd_delay = 0;
4008
4009
    // Update the scd_delay based on the the number of future frames @ ISLICE
4010
    // This case is needed for non-delayed Intra (intra_period_length == 0)
4011
533
    uint32_t scd_delay_islice = 0;
4012
533
    if (scs->static_config.intra_period_length == 0) {
4013
533
        if (scs->tf_params_per_type[0].enabled) {
4014
0
            scd_delay_islice = MIN(
4015
0
                scs->tf_params_per_type[0].num_future_pics +
4016
0
                    (scs->tf_params_per_type[0].modulate_pics
4017
0
                         ? TF_MAX_EXTENSION
4018
0
                         : 0), // number of future picture(s) used for ISLICE + max picture(s) after noise-based adjustement (=6)
4019
0
                scs->tf_params_per_type[0].max_num_future_pics);
4020
0
        }
4021
533
    }
4022
4023
    // Update the scd_delay based on the the number of future frames @ BASE
4024
533
    uint32_t scd_delay_base = 0;
4025
533
    if (scs->tf_params_per_type[1].enabled) {
4026
0
        scd_delay_base = MIN(
4027
0
            scs->tf_params_per_type[1].num_future_pics +
4028
0
                (scs->tf_params_per_type[1].modulate_pics
4029
0
                     ? TF_MAX_EXTENSION
4030
0
                     : 0), // number of future picture(s) used for BASE + max picture(s) after filtered adjustement (=3)
4031
0
            scs->tf_params_per_type[1].max_num_future_pics);
4032
0
    }
4033
533
    scs->scd_delay = MAX(scd_delay_islice, scd_delay_base);
4034
    // Update the scd_delay based on SCD, 1first pass
4035
    // Delay needed for SCD , 1first pass of (2pass and 1pass VBR)
4036
533
    if (scs->static_config.scene_change_detection || scs->vq_ctrls.sharpness_ctrls.scene_transition || scs->lap_rc) {
4037
0
        scs->scd_delay = MAX(scs->scd_delay, 2);
4038
0
    }
4039
4040
    // no future minigop is used for lowdelay prediction structure
4041
533
    if (allintra || scs->static_config.pred_structure == LOW_DELAY) {
4042
533
        scs->lad_mg = scs->tpl_lad_mg = 0;
4043
533
    } else {
4044
0
        uint8_t tpl_lad_mg =
4045
0
            1; // Specify the number of mini-gops to be used as LAD. 0: 1 mini-gop, 1: 2 mini-gops and 3: 3 mini-gops
4046
0
        uint32_t mg_size = 1 << scs->static_config.hierarchical_levels;
4047
        // If the lookahead is specified to be less than one mini-gop, then use only the current mini-gop for TPL (the current MG is always required to encode).
4048
        // Otherwise, set tpl_lad_mg to 1 when TPL is used, regardless of the specified lookahead, because TPL has been optimized to use 1 MG lookahead. Using
4049
        // more lookahead MGs may result in disadvantageous trade-offs (speed/BDR/memory).
4050
0
        if (scs->static_config.look_ahead_distance < mg_size) {
4051
0
            tpl_lad_mg = 0;
4052
0
        } else if (scs->tpl) {
4053
0
            tpl_lad_mg = 1;
4054
0
        } else {
4055
0
            tpl_lad_mg = 0;
4056
0
        }
4057
4058
        // special conditions for higher resolutions in order to decrease memory usage for tpl_lad_mg
4059
0
        if (scs->input_resolution >= INPUT_SIZE_8K_RANGE) {
4060
0
            tpl_lad_mg = 0;
4061
0
        }
4062
0
        scs->tpl_lad_mg = MIN(
4063
0
            2, tpl_lad_mg); // lad_mg is capped to 2 because tpl was optimised only for 1,2 and 3 mini-gops
4064
0
        if (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CQP_OR_CRF) {
4065
0
            scs->lad_mg = scs->tpl_lad_mg;
4066
0
        } else {
4067
            // update the look ahead size
4068
0
            update_look_ahead(scs);
4069
0
        }
4070
0
    }
4071
    // when resize mode is used, use sb 64 because of a r2r when 128 is used
4072
    // In low delay mode, sb size is set to 64
4073
    // in 240P resolution, sb size is set to 64
4074
533
    if ((scs->static_config.fast_decode && scs->static_config.qp <= 56 &&
4075
0
         !(scs->input_resolution <= INPUT_SIZE_360p_RANGE)) ||
4076
533
        scs->static_config.resize_mode > RESIZE_NONE || scs->static_config.rtc ||
4077
533
        (scs->input_resolution == INPUT_SIZE_240p_RANGE) || scs->static_config.enable_variance_boost) {
4078
533
        scs->super_block_size = 64;
4079
533
    } else if (allintra) {
4080
0
        if (scs->static_config.enc_mode <= ENC_M1) {
4081
0
            scs->super_block_size = 128;
4082
0
        } else {
4083
0
            scs->super_block_size = 64;
4084
0
        }
4085
0
    } else if (scs->static_config.enc_mode <= ENC_MR) {
4086
0
        scs->super_block_size = 128;
4087
0
    } else if (scs->static_config.enc_mode <= ENC_M5) {
4088
0
        if (scs->static_config.qp <= 57) {
4089
0
            scs->super_block_size = 64;
4090
0
        } else {
4091
0
            scs->super_block_size = 128;
4092
0
        }
4093
0
    } else {
4094
0
        scs->super_block_size = 64;
4095
0
    }
4096
    // When switch frame is on, all renditions must have same super block size. See spec 5.5.1, 5.9.15.
4097
533
    if (scs->static_config.sframe_dist != 0 || scs->static_config.sframe_posi.sframe_posis) {
4098
0
        scs->super_block_size = 64;
4099
0
    }
4100
    // Set config info related to SB size
4101
533
    if (scs->super_block_size == 128) {
4102
0
        scs->seq_header.sb_size      = BLOCK_128X128;
4103
0
        scs->sb_size                 = 128;
4104
0
        scs->seq_header.sb_mi_size   = 32; // Size of the superblock in units of MI blocks
4105
0
        scs->seq_header.sb_size_log2 = 5;
4106
533
    } else {
4107
533
        scs->seq_header.sb_size      = BLOCK_64X64;
4108
533
        scs->sb_size                 = 64;
4109
533
        scs->seq_header.sb_mi_size   = 16; // Size of the superblock in units of MI blocks
4110
533
        scs->seq_header.sb_size_log2 = 4;
4111
533
    }
4112
4113
533
    if (scs->static_config.enable_variance_boost && scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR) {
4114
0
        scs->static_config.enable_variance_boost = false;
4115
0
        SVT_WARN("Variance Boost is incompatible with CBR rate control, disabling Variance Boost\n");
4116
0
    }
4117
533
    if (scs->static_config.enable_variance_boost && scs->static_config.aq_mode == 1) {
4118
0
        scs->static_config.enable_variance_boost = false;
4119
0
        SVT_WARN("Variance AQ based on segmentation with Variance Boost not supported, disabling Variance Boost\n");
4120
0
    }
4121
533
    if (scs->static_config.variance_boost_strength >= 4) {
4122
0
        SVT_WARN(
4123
0
            "Aggressive Variance Boost strength used. This is a curve that's only useful under specific situations. "
4124
0
            "Use with caution!\n");
4125
0
    }
4126
533
    if (scs->static_config.max_tx_size == 32 && scs->static_config.qp >= 25 && scs->static_config.tune != 3) {
4127
0
        SVT_WARN(
4128
0
            "Restricting transform sizes to a max of 32x32 might reduce coding efficiency at low to medium fidelity "
4129
0
            "settings. Use with caution!\n");
4130
0
    }
4131
533
    if (scs->static_config.intra_refresh_type == SVT_AV1_FWDKF_REFRESH && scs->static_config.hierarchical_levels != 4) {
4132
0
        scs->static_config.hierarchical_levels = 4;
4133
0
        SVT_WARN(
4134
0
            "Fwd key frame is only supported for hierarchical levels 4 at this point. Hierarchical levels are set to "
4135
0
            "4\n");
4136
0
    }
4137
533
    bool    disallow_nsq  = true;
4138
533
    uint8_t allow_HVA_HVB = 0;
4139
533
    uint8_t allow_HV4     = 0;
4140
533
    uint8_t h_v_only      = 1;
4141
533
    uint8_t min_nsq_bsize = 0;
4142
533
    uint8_t no_8x4_4x8    = 1;
4143
533
    uint8_t no_16x8_8x16  = 1;
4144
3.19k
    for (uint8_t coeff_lvl = 0; coeff_lvl <= HIGH_LVL + 1; coeff_lvl++) {
4145
2.66k
        uint8_t nsq_geom_level;
4146
2.66k
        if (scs->allintra) {
4147
2.66k
            nsq_geom_level = svt_aom_get_nsq_geom_level_allintra(scs->static_config.enc_mode);
4148
2.66k
        } else if (scs->static_config.rtc) {
4149
0
#if TUNE_SIMPLIFY_SETTINGS
4150
0
            nsq_geom_level = svt_aom_get_nsq_geom_level_rtc();
4151
#else
4152
            nsq_geom_level = svt_aom_get_nsq_geom_level_rtc(scs->static_config.enc_mode);
4153
#endif
4154
0
        } else {
4155
0
            nsq_geom_level = svt_aom_get_nsq_geom_level_default(scs->static_config.enc_mode, coeff_lvl);
4156
0
        }
4157
2.66k
        disallow_nsq               = MIN(disallow_nsq, nsq_geom_level == 0);
4158
2.66k
        uint8_t temp_allow_HVA_HVB = 0, temp_allow_HV4 = 0;
4159
2.66k
        svt_aom_set_nsq_geom_ctrls(NULL, nsq_geom_level, &temp_allow_HVA_HVB, &temp_allow_HV4, &min_nsq_bsize);
4160
2.66k
        allow_HVA_HVB |= temp_allow_HVA_HVB;
4161
2.66k
        allow_HV4 |= temp_allow_HV4;
4162
2.66k
        h_v_only     = h_v_only && !allow_HVA_HVB && !allow_HV4;
4163
2.66k
        no_8x4_4x8   = no_8x4_4x8 && min_nsq_bsize >= 8;
4164
2.66k
        no_16x8_8x16 = no_16x8_8x16 && min_nsq_bsize >= 16;
4165
2.66k
    }
4166
4167
533
    bool disallow_8x8;
4168
533
    bool disallow_4x4;
4169
533
    if (scs->allintra) {
4170
533
        disallow_4x4 = svt_aom_get_disallow_4x4_allintra(scs->static_config.enc_mode);
4171
533
        disallow_8x8 = svt_aom_get_disallow_8x8_allintra();
4172
533
    } else if (scs->static_config.rtc) {
4173
0
#if TUNE_SIMPLIFY_SETTINGS
4174
0
        disallow_4x4 = svt_aom_get_disallow_4x4_rtc();
4175
#else
4176
        disallow_4x4 = svt_aom_get_disallow_4x4_rtc(scs->static_config.enc_mode);
4177
#endif
4178
0
        disallow_8x8 = svt_aom_get_disallow_8x8_rtc(
4179
0
            scs->static_config.enc_mode, scs->max_input_luma_width, scs->max_input_luma_height);
4180
0
    } else {
4181
0
        disallow_4x4 = svt_aom_get_disallow_4x4_default(scs->static_config.enc_mode);
4182
0
        disallow_8x8 = svt_aom_get_disallow_8x8_default();
4183
0
    }
4184
533
    if (scs->super_block_size == 128) {
4185
0
        if (!allow_HVA_HVB && disallow_4x4) {
4186
0
            scs->svt_aom_geom_idx = GEOM_10;
4187
0
            scs->max_block_cnt    = 2377;
4188
0
        } else {
4189
0
            scs->svt_aom_geom_idx = GEOM_9;
4190
0
            scs->max_block_cnt    = 4421;
4191
0
        }
4192
533
    } else {
4193
        //SB 64x64
4194
533
        if (disallow_nsq && disallow_4x4 && disallow_8x8) {
4195
0
            scs->svt_aom_geom_idx = GEOM_0;
4196
0
            scs->max_block_cnt    = 21;
4197
533
        } else if (h_v_only && disallow_4x4 && disallow_8x8 && no_16x8_8x16) {
4198
0
            scs->svt_aom_geom_idx = GEOM_1;
4199
0
            scs->max_block_cnt    = 41;
4200
533
        } else if (disallow_nsq && disallow_4x4) {
4201
533
            scs->svt_aom_geom_idx = GEOM_2;
4202
533
            scs->max_block_cnt    = 85;
4203
533
        } else if (h_v_only && disallow_4x4 && no_16x8_8x16) {
4204
0
            scs->svt_aom_geom_idx = GEOM_3;
4205
0
            scs->max_block_cnt    = 105;
4206
0
        } else if (h_v_only && disallow_4x4 && no_8x4_4x8) {
4207
0
            scs->svt_aom_geom_idx = GEOM_4;
4208
0
            scs->max_block_cnt    = 169;
4209
0
        } else if (h_v_only && disallow_4x4) {
4210
0
            scs->svt_aom_geom_idx = GEOM_5;
4211
0
            scs->max_block_cnt    = 425;
4212
0
        } else if (h_v_only) {
4213
0
            scs->svt_aom_geom_idx = GEOM_6;
4214
0
            scs->max_block_cnt    = 681;
4215
0
        } else if (!allow_HVA_HVB) {
4216
0
            scs->svt_aom_geom_idx = GEOM_7;
4217
0
            scs->max_block_cnt    = 849;
4218
0
        } else {
4219
0
            scs->svt_aom_geom_idx = GEOM_8;
4220
0
            scs->max_block_cnt    = 1101;
4221
0
        }
4222
533
    }
4223
    // Configure the padding
4224
533
    scs->border = BLOCK_SIZE_64 + 4;
4225
4226
    //for 10bit,  increase the pad of source from 68 to 72 (mutliple of 8) to accomodate 2bit-compression flow
4227
    //we actually need to change the horizontal dimension only, but for simplicity/uniformity we do all directions
4228
    // if (scs->static_config.encoder_bit_depth != EB_EIGHT_BIT)
4229
533
    { scs->border += 4; }
4230
4231
533
    scs->static_config.enable_overlays = !scs->static_config.enable_tf ||
4232
0
            (scs->static_config.rate_control_mode != SVT_AV1_RC_MODE_CQP_OR_CRF)
4233
533
        ? 0
4234
533
        : scs->static_config.enable_overlays;
4235
4236
    // Enforce starting frame in decode order (at PicMgr)
4237
    // Does not wait for feedback from PKT
4238
533
    if (scs->static_config.level_of_parallelism == 1 || scs->static_config.pred_structure == LOW_DELAY) {
4239
0
        scs->enable_pic_mgr_dec_order = 1;
4240
533
    } else {
4241
533
        scs->enable_pic_mgr_dec_order = 0;
4242
533
    }
4243
    // Enforce encoding frame in decode order
4244
    // Wait for feedback from PKT
4245
#if RC_NO_R2R
4246
    scs->enable_dec_order = 1;
4247
#else
4248
533
    if (scs->static_config.level_of_parallelism == 1 || scs->static_config.pred_structure == LOW_DELAY) {
4249
0
        scs->enable_dec_order = 1;
4250
533
    } else {
4251
533
        scs->enable_dec_order = 0;
4252
533
    }
4253
533
#endif
4254
    // 1: Use boundary pixels in restoration filter search.
4255
    // 0: Do not use boundary pixels in the restoration filter search.
4256
533
    scs->use_boundaries_in_rest_search = 0;
4257
4258
533
    svt_aom_set_mfmv_config(scs, scs->static_config.enc_mode);
4259
4260
533
    scs->list0_only_base = scs->static_config.enc_mode > ENC_M2;
4261
4262
533
    if (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_VBR ||
4263
533
        scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR || scs->input_resolution >= INPUT_SIZE_4K_RANGE ||
4264
533
        scs->static_config.pred_structure != RANDOM_ACCESS || scs->static_config.pass != ENC_SINGLE_PASS) {
4265
533
        scs->enable_dg = 0;
4266
533
    } else {
4267
0
        scs->enable_dg = scs->static_config.enable_dg;
4268
0
    }
4269
    // Set hbd_md OFF for high encode modes or bitdepth < 10
4270
533
    if (scs->static_config.encoder_bit_depth < 10) {
4271
533
        scs->enable_hbd_mode_decision = 0;
4272
533
    }
4273
4274
    // Throws a warning when scene change is on, as the feature is not optimal and may produce false detections
4275
533
    if (scs->static_config.scene_change_detection == 1) {
4276
0
        SVT_WARN("Scene Change is not optimal and may produce suboptimal keyframe placements\n");
4277
0
    }
4278
    // MRP level
4279
533
    uint8_t mrp_level;
4280
533
#if TUNE_SIMPLIFY_SETTINGS
4281
533
    if (rtc_tune) {
4282
0
        if (use_flat_ipp) {
4283
0
#if TUNE_SHIFT_PRESETS_RTC
4284
0
            if (scs->static_config.enc_mode <= ENC_M8) {
4285
0
                mrp_level = 6;
4286
0
            } else {
4287
0
                mrp_level = 0;
4288
0
            }
4289
#else
4290
            if (scs->static_config.enc_mode <= ENC_M8) {
4291
                mrp_level = 6;
4292
            } else if (scs->static_config.enc_mode <= ENC_M9) {
4293
                mrp_level = 9;
4294
            } else {
4295
                mrp_level = 0;
4296
            }
4297
#endif
4298
0
        } else {
4299
0
#if TUNE_SHIFT_PRESETS_RTC
4300
0
            if (scs->static_config.enc_mode <= ENC_M9) {
4301
#else
4302
            if (scs->static_config.enc_mode <= ENC_M10) {
4303
#endif
4304
0
                mrp_level = 6;
4305
0
#if TUNE_SHIFT_PRESETS_RTC
4306
0
            } else if (scs->static_config.enc_mode <= ENC_M10) {
4307
#else
4308
            } else if (scs->static_config.enc_mode <= ENC_M11) {
4309
#endif
4310
0
                mrp_level = 9;
4311
0
            } else {
4312
0
                mrp_level = 0;
4313
0
            }
4314
0
        }
4315
#else
4316
    if (scs->static_config.rtc) {
4317
        if (scs->static_config.enc_mode <= ENC_M8 || (!scs->use_flat_ipp && scs->static_config.enc_mode <= ENC_M10)) {
4318
            mrp_level = 6;
4319
        } else if ((scs->use_flat_ipp && scs->static_config.enc_mode <= ENC_M9) ||
4320
                   (!scs->use_flat_ipp && scs->static_config.enc_mode <= ENC_M11)) {
4321
            mrp_level = 9;
4322
        } else {
4323
            mrp_level = 0;
4324
        }
4325
#endif
4326
0
    }
4327
4328
533
    else {
4329
533
        if (scs->static_config.enc_mode <= ENC_MR) {
4330
0
            mrp_level = 1;
4331
533
        } else if (scs->static_config.enc_mode <= ENC_M2) {
4332
0
            mrp_level = 2;
4333
533
        } else if (scs->static_config.enc_mode <= ENC_M4) {
4334
0
            mrp_level = 4;
4335
533
        } else if (scs->static_config.enc_mode <= ENC_M8) {
4336
0
            mrp_level = 6;
4337
533
        } else if (scs->static_config.enc_mode <= ENC_M9) {
4338
533
            mrp_level = scs->static_config.pred_structure == RANDOM_ACCESS ? 7 : 9;
4339
533
        } else {
4340
0
            if (scs->static_config.encoder_bit_depth == EB_EIGHT_BIT) {
4341
0
                mrp_level = scs->static_config.pred_structure == RANDOM_ACCESS ? 11 : 0;
4342
0
            } else {
4343
0
                mrp_level = scs->static_config.pred_structure == RANDOM_ACCESS ? 7 : 0;
4344
0
            }
4345
0
        }
4346
533
    }
4347
533
    set_mrp_ctrl(scs, mrp_level);
4348
533
    scs->is_short_clip = scs->static_config.gop_constraint_rc
4349
533
        ? 1
4350
533
        : 0; // set to 1 if multipass and less than 200 frames in resourcecordination
4351
533
    if (allintra || scs->static_config.aq_mode == 1 || scs->static_config.scene_change_detection == 1 ||
4352
533
        scs->vq_ctrls.sharpness_ctrls.tf == 1 || scs->static_config.enable_variance_boost) {
4353
533
        scs->calculate_variance = 1;
4354
533
    } else {
4355
0
        scs->calculate_variance = 0;
4356
0
    }
4357
4358
533
    scs->resize_pending_params.resize_state = ORIG;
4359
533
    scs->resize_pending_params.resize_denom = SCALE_NUMERATOR;
4360
4361
533
#if OPT_GATE_SB_LAMBDA_MOD
4362
533
#if TUNE_SHIFT_PRESETS_RTC
4363
533
    scs->stats_based_sb_lambda_modulation = (scs->static_config.enc_mode <= (rtc_tune ? ENC_M10 : ENC_M11)) ? 1 : 0;
4364
#else
4365
    scs->stats_based_sb_lambda_modulation = (scs->static_config.enc_mode <= ENC_M11) ? 1 : 0;
4366
#endif
4367
#else
4368
    scs->stats_based_sb_lambda_modulation = 1;
4369
#endif
4370
4371
533
    scs->fast_aa_aware_screen_detection_mode = (scs->static_config.enc_mode >= ENC_M3) ? 1 : 0;
4372
533
}
4373
4374
533
static void copy_api_from_app(SequenceControlSet* scs, EbSvtAv1EncConfiguration* config_struct) {
4375
533
    scs->max_input_luma_width  = config_struct->source_width;
4376
533
    scs->max_input_luma_height = config_struct->source_height;
4377
    // SB Definitions
4378
533
    scs->static_config.pred_structure = config_struct->pred_structure;
4379
533
    scs->static_config.rtc            = config_struct->rtc;
4380
533
    if (scs->static_config.rtc && scs->static_config.pred_structure != LOW_DELAY) {
4381
0
        scs->static_config.pred_structure = LOW_DELAY;
4382
0
        SVT_WARN("Instance %u: Force low delay pred structure to be used for rtc.\n");
4383
0
    }
4384
533
    scs->enable_qp_scaling_flag = scs->allintra ? 0 : 1;
4385
    // Set Picture Parameters for statistics gathering
4386
533
    scs->picture_analysis_number_of_regions_per_width  = scs->max_input_luma_width >= 64
4387
533
         ? HIGHER_THAN_CLASS_1_REGION_SPLIT_PER_WIDTH
4388
533
         : 1;
4389
533
    scs->picture_analysis_number_of_regions_per_height = scs->max_input_luma_height >= 64
4390
533
        ? HIGHER_THAN_CLASS_1_REGION_SPLIT_PER_HEIGHT
4391
533
        : 1;
4392
4393
533
    scs->pic_based_rate_est   = false;
4394
533
    scs->block_mean_calc_prec = BLOCK_MEAN_PREC_SUB;
4395
533
    scs->speed_control_flag   = 0;
4396
    // Padding Offsets
4397
533
    scs->b64_size                          = 64;
4398
533
    scs->static_config.intra_period_length = config_struct->intra_period_length;
4399
533
    scs->static_config.avif                = config_struct->avif;
4400
533
    scs->allintra                          = (scs->static_config.intra_period_length == 0 || scs->static_config.avif ||
4401
0
                     scs->static_config.pred_structure == ALL_INTRA);
4402
533
    if (scs->allintra) {
4403
533
        scs->static_config.pred_structure      = ALL_INTRA;
4404
533
        scs->static_config.intra_period_length = 0;
4405
533
    }
4406
533
    scs->static_config.multiply_keyint    = config_struct->multiply_keyint;
4407
533
    scs->static_config.intra_refresh_type = config_struct->intra_refresh_type;
4408
533
    scs->static_config.enc_mode           = config_struct->enc_mode;
4409
533
    if (scs->allintra) {
4410
533
#if FIX_MR_STILL_IMAGE
4411
533
        if (scs->static_config.enc_mode > ENC_M9) {
4412
#else
4413
        if (scs->static_config.enc_mode == ENC_MR) {
4414
            SVT_WARN("The lowest supported preset for all-intra and still-image is M0.\n");
4415
            scs->static_config.enc_mode = ENC_M0;
4416
        } else if (scs->static_config.enc_mode > ENC_M9) {
4417
#endif
4418
533
            SVT_WARN("Preset M%d is mapped to M9.\n", scs->static_config.enc_mode);
4419
533
            scs->static_config.enc_mode = ENC_M9;
4420
533
        }
4421
533
    } else if (scs->static_config.rtc) {
4422
0
#if TUNE_SHIFT_PRESETS_RTC
4423
0
        if (scs->static_config.enc_mode > ENC_M11) {
4424
0
            SVT_WARN("Preset M%d is mapped to M11.\n", scs->static_config.enc_mode);
4425
0
            scs->static_config.enc_mode = ENC_M11;
4426
0
        }
4427
#else
4428
        if (scs->static_config.enc_mode > ENC_M12) {
4429
            SVT_WARN("Preset M%d is mapped to M12.\n", scs->static_config.enc_mode);
4430
            scs->static_config.enc_mode = ENC_M12;
4431
        }
4432
#if TUNE_SIMPLIFY_SETTINGS
4433
        else if (scs->static_config.enc_mode == ENC_M9) {
4434
            SVT_WARN("Preset M%d is temporarily mapped to M10 (placeholder).\n", scs->static_config.enc_mode);
4435
            scs->static_config.enc_mode = ENC_M10;
4436
        }
4437
#endif
4438
#endif
4439
0
    }
4440
4441
0
    else if (scs->static_config.enc_mode > ENC_M11) {
4442
0
        SVT_WARN("Preset M%d is mapped to M11.\n", scs->static_config.enc_mode);
4443
0
        scs->static_config.enc_mode = ENC_M11;
4444
0
    }
4445
4446
533
    ResolutionRange input_resolution;
4447
533
    svt_aom_derive_input_resolution(&input_resolution, scs->max_input_luma_width * scs->max_input_luma_height);
4448
533
    if (!scs->allintra && scs->static_config.pred_structure == RANDOM_ACCESS && scs->static_config.enc_mode > ENC_M9 &&
4449
0
        input_resolution >= INPUT_SIZE_4K_RANGE) {
4450
0
        scs->static_config.enc_mode = ENC_M9;
4451
0
        SVT_WARN(
4452
0
            "Setting preset to M9 as it is the highest supported preset for 4k and higher resolutions in Random Access "
4453
0
            "mode\n");
4454
0
    }
4455
4456
533
    scs->static_config.use_qp_file                    = config_struct->use_qp_file;
4457
533
    scs->static_config.use_fixed_qindex_offsets       = config_struct->use_fixed_qindex_offsets;
4458
533
    scs->static_config.key_frame_chroma_qindex_offset = config_struct->key_frame_chroma_qindex_offset;
4459
533
    scs->static_config.key_frame_qindex_offset        = config_struct->key_frame_qindex_offset;
4460
533
    if (scs->static_config.use_fixed_qindex_offsets) {
4461
0
        scs->enable_qp_scaling_flag    = scs->static_config.use_fixed_qindex_offsets == 1
4462
0
               ? 0
4463
0
               : 1; // do not shut the auto QPS if use_fixed_qindex_offsets 2
4464
0
        scs->static_config.use_qp_file = 0;
4465
0
        memcpy(scs->static_config.qindex_offsets, config_struct->qindex_offsets, MAX_TEMPORAL_LAYERS * sizeof(int32_t));
4466
0
    }
4467
533
    memcpy(scs->static_config.chroma_qindex_offsets,
4468
533
           config_struct->chroma_qindex_offsets,
4469
533
           MAX_TEMPORAL_LAYERS * sizeof(int32_t));
4470
4471
533
    scs->static_config.lossless = config_struct->lossless;
4472
533
    if (scs->static_config.lossless) {
4473
221
        scs->static_config.luma_y_dc_qindex_offset   = 0;
4474
221
        scs->static_config.chroma_u_dc_qindex_offset = 0;
4475
221
        scs->static_config.chroma_u_ac_qindex_offset = 0;
4476
221
        scs->static_config.chroma_v_dc_qindex_offset = 0;
4477
221
        scs->static_config.chroma_v_ac_qindex_offset = 0;
4478
312
    } else {
4479
312
        scs->static_config.luma_y_dc_qindex_offset   = config_struct->luma_y_dc_qindex_offset;
4480
312
        scs->static_config.chroma_u_dc_qindex_offset = config_struct->chroma_u_dc_qindex_offset;
4481
312
        scs->static_config.chroma_u_ac_qindex_offset = config_struct->chroma_u_ac_qindex_offset;
4482
312
        scs->static_config.chroma_v_dc_qindex_offset = config_struct->chroma_v_dc_qindex_offset;
4483
312
        scs->static_config.chroma_v_ac_qindex_offset = config_struct->chroma_v_ac_qindex_offset;
4484
312
    }
4485
533
    memcpy(scs->static_config.lambda_scale_factors,
4486
533
           config_struct->lambda_scale_factors,
4487
533
           SVT_AV1_FRAME_UPDATE_TYPES * sizeof(int32_t));
4488
4489
533
    scs->static_config.rc_stats_buffer = config_struct->rc_stats_buffer;
4490
533
    scs->static_config.pass            = config_struct->pass;
4491
    // Deblock Filter
4492
533
    scs->static_config.enable_dlf_flag = config_struct->enable_dlf_flag;
4493
4494
    // CDEF
4495
533
    scs->static_config.cdef_level = config_struct->cdef_level;
4496
4497
    // Intra Block Copy
4498
533
    scs->static_config.enable_intrabc = config_struct->enable_intrabc;
4499
4500
    // Restoration filtering
4501
533
    scs->static_config.enable_restoration_filtering = config_struct->enable_restoration_filtering;
4502
4503
    // motion field motion vector
4504
533
    scs->static_config.enable_mfmv = config_struct->enable_mfmv;
4505
4506
    // Dynamic GoP
4507
533
    scs->static_config.enable_dg = config_struct->enable_dg;
4508
4509
    // Decoder Optimization Flag
4510
533
    scs->static_config.fast_decode = config_struct->fast_decode;
4511
4512
    //Film Grain
4513
533
    scs->static_config.film_grain_denoise_strength = config_struct->film_grain_denoise_strength;
4514
533
    scs->static_config.film_grain_denoise_apply    = config_struct->film_grain_denoise_apply;
4515
533
    if (scs->static_config.film_grain_denoise_strength == 0 && scs->static_config.film_grain_denoise_apply == 1) {
4516
0
        SVT_WARN("Film grain denoise apply signal is going to be ignored when film grain is off.\n");
4517
0
    }
4518
533
    scs->seq_header.film_grain_params_present = (uint8_t)(scs->static_config.film_grain_denoise_strength > 0);
4519
533
    scs->static_config.fgs_table              = config_struct->fgs_table;
4520
4521
    // MD Parameters
4522
533
    scs->enable_hbd_mode_decision = config_struct->encoder_bit_depth > 8 ? DEFAULT : 0;
4523
533
    {
4524
533
        if (config_struct->tile_rows == DEFAULT && config_struct->tile_columns == DEFAULT) {
4525
0
            scs->static_config.tile_rows    = 0;
4526
0
            scs->static_config.tile_columns = 0;
4527
4528
533
        } else {
4529
533
            if (config_struct->tile_rows == DEFAULT) {
4530
0
                scs->static_config.tile_rows    = 0;
4531
0
                scs->static_config.tile_columns = config_struct->tile_columns;
4532
533
            } else if (config_struct->tile_columns == DEFAULT) {
4533
0
                scs->static_config.tile_rows    = config_struct->tile_rows;
4534
0
                scs->static_config.tile_columns = 0;
4535
533
            } else {
4536
533
                scs->static_config.tile_rows    = config_struct->tile_rows;
4537
533
                scs->static_config.tile_columns = config_struct->tile_columns;
4538
533
            }
4539
533
        }
4540
533
    }
4541
4542
    // Rate Control
4543
533
    scs->static_config.scene_change_detection = config_struct->scene_change_detection;
4544
533
    if (config_struct->lossless && config_struct->rate_control_mode) {
4545
0
        scs->static_config.rate_control_mode = SVT_AV1_RC_MODE_CQP_OR_CRF;
4546
0
        SVT_WARN("Switched to CQP mode since lossless coding is enabled\n");
4547
533
    } else {
4548
533
        scs->static_config.rate_control_mode = config_struct->rate_control_mode;
4549
533
    }
4550
533
    if (scs->static_config.pass == ENC_SINGLE_PASS && scs->static_config.rtc) {
4551
0
        if (scs->static_config.enc_mode < ENC_M7) {
4552
0
            scs->static_config.enc_mode = ENC_M7;
4553
0
            SVT_WARN("rtc mode only supports presets [7-%d]. Forcing preset to 7.\n", ENC_M13);
4554
0
        }
4555
0
    }
4556
533
    scs->static_config.tune                = config_struct->tune;
4557
533
    scs->static_config.hierarchical_levels = config_struct->hierarchical_levels;
4558
4559
533
    if (scs->static_config.rtc && scs->static_config.hierarchical_levels == 0) {
4560
0
        scs->static_config.hierarchical_levels = HIERARCHICAL_LEVELS_AUTO;
4561
        // Mimic flat prediction structure
4562
0
        scs->use_flat_ipp = 1;
4563
0
    }
4564
    // Set the default hierarchical levels
4565
533
    if (scs->static_config.hierarchical_levels == HIERARCHICAL_LEVELS_AUTO) {
4566
533
        scs->static_config.hierarchical_levels = scs->static_config.pred_structure == LOW_DELAY &&
4567
0
                (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR ||
4568
0
                 !(scs->static_config.enc_mode <= ENC_M9))
4569
533
            ? 2
4570
533
            : scs->static_config.pred_structure == LOW_DELAY ? 3
4571
533
            : scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_VBR ||
4572
533
                scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR ||
4573
533
                (input_resolution >= INPUT_SIZE_1080p_RANGE && scs->static_config.enc_mode >= ENC_M8) ||
4574
533
                !(scs->static_config.enc_mode <= ENC_M8) || input_resolution >= INPUT_SIZE_8K_RANGE
4575
533
            ? 4
4576
533
            : 5;
4577
533
    }
4578
533
    if (scs->static_config.pass == ENC_SINGLE_PASS && scs->static_config.pred_structure == LOW_DELAY) {
4579
0
        if (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR &&
4580
0
            scs->static_config.hierarchical_levels != 2) {
4581
0
            scs->static_config.hierarchical_levels = 2;
4582
0
            SVT_WARN("Forced Low delay CBR mode to use HierarchicalLevels = 2\n");
4583
0
        }
4584
0
    }
4585
    // Set hierarchical_levels to 2 to reduce memory allocation; 2 is the minimum currently supported
4586
533
    if (scs->allintra) {
4587
533
        scs->static_config.hierarchical_levels = 2;
4588
533
    }
4589
533
    scs->max_temporal_layers                  = scs->static_config.hierarchical_levels;
4590
533
    scs->static_config.look_ahead_distance    = config_struct->look_ahead_distance;
4591
533
    scs->static_config.frame_rate_denominator = config_struct->frame_rate_denominator;
4592
533
    scs->static_config.frame_rate_numerator   = config_struct->frame_rate_numerator;
4593
4594
533
    scs->static_config.target_bit_rate = config_struct->target_bit_rate;
4595
533
    scs->static_config.max_bit_rate    = config_struct->max_bit_rate;
4596
    //TODO: check RC mode and set only when RC is enabled in the final version.
4597
533
    scs->static_config.aq_mode = scs->static_config.lossless ? 0 : config_struct->aq_mode;
4598
4599
    // TPL is disabled for allintra and LD encoding, and when aq_mode is 0
4600
533
    if (scs->static_config.max_bit_rate &&
4601
0
        (scs->static_config.aq_mode == 0 || scs->allintra || scs->static_config.pred_structure == LOW_DELAY)) {
4602
0
        scs->static_config.max_bit_rate = 0;
4603
0
        SVT_WARN("Maximum bit rate only supported with tpl on. max bit rate 0 is used instead.\n");
4604
0
    }
4605
4606
533
    scs->static_config.max_qp_allowed = scs->static_config.lossless ? MIN_QP_VALUE : config_struct->max_qp_allowed;
4607
4608
533
    scs->static_config.min_qp_allowed      = scs->static_config.lossless ? MIN_QP_VALUE
4609
533
             : config_struct->min_qp_allowed == MIN_QP_AUTO ? scs->static_config.rate_control_mode ? 4 : MIN_QP_VALUE
4610
312
                                                            : config_struct->min_qp_allowed;
4611
533
    scs->static_config.vbr_min_section_pct = config_struct->vbr_min_section_pct;
4612
533
    scs->static_config.vbr_max_section_pct = config_struct->vbr_max_section_pct;
4613
533
    scs->static_config.under_shoot_pct     = config_struct->under_shoot_pct;
4614
533
    scs->static_config.over_shoot_pct      = config_struct->over_shoot_pct;
4615
533
    if (scs->static_config.under_shoot_pct == (uint32_t)DEFAULT) {
4616
533
        if (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_VBR) {
4617
0
            scs->static_config.under_shoot_pct = 50;
4618
533
        } else {
4619
533
            scs->static_config.under_shoot_pct = 25;
4620
533
        }
4621
533
    }
4622
4623
533
    if (scs->static_config.over_shoot_pct == (uint32_t)DEFAULT) {
4624
533
        scs->static_config.over_shoot_pct = 25;
4625
533
    }
4626
533
    scs->static_config.mbr_over_shoot_pct       = config_struct->mbr_over_shoot_pct;
4627
533
    scs->static_config.max_intra_bitrate_pct    = config_struct->max_intra_bitrate_pct;
4628
533
    scs->static_config.max_inter_bitrate_pct    = config_struct->max_inter_bitrate_pct;
4629
533
    scs->static_config.gop_constraint_rc        = config_struct->gop_constraint_rc;
4630
533
    scs->static_config.maximum_buffer_size_ms   = config_struct->maximum_buffer_size_ms;
4631
533
    scs->static_config.starting_buffer_level_ms = config_struct->starting_buffer_level_ms;
4632
533
    scs->static_config.optimal_buffer_level_ms  = config_struct->optimal_buffer_level_ms;
4633
533
    scs->static_config.recode_loop              = config_struct->recode_loop;
4634
533
    if (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_VBR && scs->static_config.pass == ENC_SINGLE_PASS) {
4635
0
        scs->lap_rc = 1;
4636
533
    } else {
4637
533
        scs->lap_rc = 0;
4638
533
    }
4639
    // Misc
4640
533
    scs->static_config.encoder_bit_depth    = config_struct->encoder_bit_depth;
4641
533
    scs->static_config.encoder_color_format = config_struct->encoder_color_format;
4642
4643
533
    scs->chroma_format_idc = (uint32_t)(scs->static_config.encoder_color_format);
4644
533
    scs->encoder_bit_depth = (uint32_t)(scs->static_config.encoder_bit_depth);
4645
    //16bit pipeline
4646
533
    scs->is_16bit_pipeline = ((config_struct->encoder_bit_depth) > EB_EIGHT_BIT) ? true : false;
4647
533
    scs->subsampling_x     = (scs->chroma_format_idc == EB_YUV444 ? 0 : 1);
4648
533
    scs->subsampling_y     = (scs->chroma_format_idc >= EB_YUV422 ? 0 : 1);
4649
    // Force screen-content detection OFF when allintra
4650
533
    const bool allintra = scs->allintra;
4651
533
#if TUNE_SIMPLIFY_SETTINGS
4652
533
    const bool rtc = scs->static_config.rtc;
4653
533
#endif
4654
533
#if OPT_SC_STILL_IMAGE
4655
533
    if (allintra) {
4656
533
        if (config_struct->screen_content_mode <= 1) {
4657
0
            scs->static_config.screen_content_mode = config_struct->screen_content_mode;
4658
533
        } else if (scs->static_config.enc_mode <= ENC_M7) {
4659
0
            scs->static_config.screen_content_mode = 3;
4660
533
        } else {
4661
533
            scs->static_config.screen_content_mode = 0;
4662
533
            SVT_WARN(
4663
533
                "Screen-content detection and tools are disabled for all-intra coding at M8 and above; forcing NSC "
4664
533
                "path\n");
4665
533
        }
4666
#else
4667
    if (allintra && config_struct->screen_content_mode > 1) {
4668
        scs->static_config.screen_content_mode = 0;
4669
        SVT_WARN("Screen-content detection is disabled for all-intra coding; forcing NSC path\n");
4670
#endif
4671
533
    }
4672
0
#if TUNE_SIMPLIFY_SETTINGS
4673
0
    else if (rtc) {
4674
0
        if (config_struct->screen_content_mode <= 1 && scs->static_config.enc_mode <= ENC_M8) {
4675
0
            scs->static_config.screen_content_mode = config_struct->screen_content_mode;
4676
0
        } else if (scs->static_config.enc_mode <= ENC_M8) {
4677
0
            scs->static_config.screen_content_mode = 3;
4678
0
        } else {
4679
0
            scs->static_config.screen_content_mode = 0;
4680
0
            SVT_WARN(
4681
0
                "Screen-content detection and tools are disabled for RTC mode coding at M9 and above; forcing NSC "
4682
0
                "path\n");
4683
0
        }
4684
0
    }
4685
0
#endif
4686
0
    else {
4687
0
#if TUNE_SIMPLIFY_SETTINGS
4688
0
        if (config_struct->screen_content_mode <= 1 && scs->static_config.enc_mode <= ENC_M8) {
4689
0
            scs->static_config.screen_content_mode = config_struct->screen_content_mode;
4690
0
        } else if (scs->static_config.enc_mode <= ENC_M8) {
4691
0
            scs->static_config.screen_content_mode = 3;
4692
0
        } else {
4693
0
            scs->static_config.screen_content_mode = 0;
4694
0
            SVT_WARN(
4695
0
                "Screen-content detection and tools are disabled for RA mode coding at M9 and above; forcing NSC "
4696
0
                "path\n");
4697
0
        }
4698
#else
4699
        scs->static_config.screen_content_mode = config_struct->screen_content_mode;
4700
#endif
4701
0
    }
4702
    // Annex A parameters
4703
533
    scs->static_config.profile     = config_struct->profile;
4704
533
    scs->static_config.tier        = config_struct->tier;
4705
533
    scs->static_config.level       = config_struct->level;
4706
533
    scs->static_config.stat_report = config_struct->stat_report;
4707
4708
    // Buffers - Hardcoded(Cleanup)
4709
533
    scs->static_config.use_cpu_flags = config_struct->use_cpu_flags;
4710
4711
533
    scs->static_config.level_of_parallelism = config_struct->level_of_parallelism;
4712
533
    if (scs->static_config.level_of_parallelism >= PARALLEL_LEVEL_COUNT) {
4713
0
        SVT_WARN("Level of parallelism supports levels [0-%d]. Setting maximum parallelism level.\n",
4714
0
                 PARALLEL_LEVEL_COUNT - 1);
4715
0
        SVT_WARN(
4716
0
            "Level of parallelism does not correspond to a target number of processors to use. See Docs/Parameters.md "
4717
0
            "for info.\n");
4718
0
        scs->static_config.level_of_parallelism = PARALLEL_LEVEL_6;
4719
0
    }
4720
4721
533
    scs->static_config.qp            = config_struct->qp;
4722
533
    scs->static_config.recon_enabled = config_struct->recon_enabled;
4723
4724
    // Numerator and Denominator already checked to be non 0
4725
533
    scs->frame_rate = (double)scs->static_config.frame_rate_numerator /
4726
533
        (double)scs->static_config.frame_rate_denominator;
4727
4728
    // Get Default Intra Period if not specified
4729
533
    if (scs->static_config.intra_period_length == -2) {
4730
0
        scs->static_config.intra_period_length = compute_default_intra_period(scs);
4731
0
        scs->allintra = (scs->static_config.intra_period_length == 0 || scs->static_config.avif);
4732
533
    } else if (scs->static_config.multiply_keyint) {
4733
0
        scs->static_config.intra_period_length = (int32_t)(scs->frame_rate * scs->static_config.intra_period_length);
4734
0
    }
4735
533
    if (scs->static_config.look_ahead_distance == (uint32_t)~0) {
4736
533
        scs->static_config.look_ahead_distance = compute_default_look_ahead(&scs->static_config);
4737
533
    }
4738
533
    scs->static_config.enable_tf          = scs->allintra ? 0 : config_struct->enable_tf;
4739
533
    scs->static_config.enable_tf_key      = config_struct->enable_tf && config_struct->enable_tf_key;
4740
533
    scs->static_config.enable_overlays    = config_struct->enable_overlays;
4741
533
    scs->static_config.superres_mode      = config_struct->superres_mode;
4742
533
    scs->static_config.superres_denom     = config_struct->superres_denom;
4743
533
    scs->static_config.superres_kf_denom  = config_struct->superres_kf_denom;
4744
533
    scs->static_config.superres_qthres    = config_struct->superres_qthres;
4745
533
    scs->static_config.superres_kf_qthres = config_struct->superres_kf_qthres;
4746
4747
533
    if (scs->static_config.superres_mode == SUPERRES_AUTO) {
4748
        // TODO: set search mode based on preset
4749
        //scs->static_config.superres_auto_search_type = SUPERRES_AUTO_SOLO;
4750
0
        scs->static_config.superres_auto_search_type = SUPERRES_AUTO_DUAL;
4751
        //scs->static_config.superres_auto_search_type = SUPERRES_AUTO_ALL;
4752
0
    }
4753
4754
533
    scs->static_config.resize_mode     = config_struct->resize_mode;
4755
533
    scs->static_config.resize_denom    = config_struct->resize_denom;
4756
533
    scs->static_config.resize_kf_denom = config_struct->resize_kf_denom;
4757
533
    if (config_struct->frame_scale_evts.start_frame_nums) {
4758
0
        EB_NO_THROW_MALLOC(scs->static_config.frame_scale_evts.start_frame_nums,
4759
0
                           sizeof(int64_t) * config_struct->frame_scale_evts.evt_num);
4760
0
        memcpy(scs->static_config.frame_scale_evts.start_frame_nums,
4761
0
               config_struct->frame_scale_evts.start_frame_nums,
4762
0
               sizeof(int64_t) * config_struct->frame_scale_evts.evt_num);
4763
0
    }
4764
533
    if (config_struct->frame_scale_evts.resize_kf_denoms) {
4765
0
        EB_NO_THROW_MALLOC(scs->static_config.frame_scale_evts.resize_kf_denoms,
4766
0
                           sizeof(int32_t) * config_struct->frame_scale_evts.evt_num);
4767
0
        memcpy(scs->static_config.frame_scale_evts.resize_kf_denoms,
4768
0
               config_struct->frame_scale_evts.resize_kf_denoms,
4769
0
               sizeof(int32_t) * config_struct->frame_scale_evts.evt_num);
4770
0
    }
4771
533
    if (config_struct->frame_scale_evts.resize_denoms) {
4772
0
        EB_NO_THROW_MALLOC(scs->static_config.frame_scale_evts.resize_denoms,
4773
0
                           sizeof(int32_t) * config_struct->frame_scale_evts.evt_num);
4774
0
        memcpy(scs->static_config.frame_scale_evts.resize_denoms,
4775
0
               config_struct->frame_scale_evts.resize_denoms,
4776
0
               sizeof(int32_t) * config_struct->frame_scale_evts.evt_num);
4777
0
    }
4778
533
    scs->static_config.frame_scale_evts.evt_num = config_struct->frame_scale_evts.evt_num;
4779
533
    if (config_struct->sframe_posi.sframe_posis) {
4780
0
        EB_NO_THROW_MALLOC(scs->static_config.sframe_posi.sframe_posis,
4781
0
                           sizeof(uint64_t) * config_struct->sframe_posi.sframe_num);
4782
0
        memcpy(scs->static_config.sframe_posi.sframe_posis,
4783
0
               config_struct->sframe_posi.sframe_posis,
4784
0
               sizeof(uint64_t) * config_struct->sframe_posi.sframe_num);
4785
0
    }
4786
533
    scs->static_config.sframe_posi.sframe_num = config_struct->sframe_posi.sframe_num;
4787
533
    if (config_struct->sframe_posi.sframe_qps) {
4788
0
        EB_NO_THROW_MALLOC(scs->static_config.sframe_posi.sframe_qps,
4789
0
                           sizeof(config_struct->sframe_posi.sframe_qps[0]) * config_struct->sframe_posi.sframe_qp_num);
4790
0
        memcpy(scs->static_config.sframe_posi.sframe_qps,
4791
0
               config_struct->sframe_posi.sframe_qps,
4792
0
               sizeof(config_struct->sframe_posi.sframe_qps[0]) * config_struct->sframe_posi.sframe_qp_num);
4793
0
    }
4794
533
    if (config_struct->sframe_posi.sframe_qp_offsets) {
4795
0
        EB_NO_THROW_MALLOC(
4796
0
            scs->static_config.sframe_posi.sframe_qp_offsets,
4797
0
            sizeof(config_struct->sframe_posi.sframe_qp_offsets[0]) * config_struct->sframe_posi.sframe_qp_num);
4798
0
        memcpy(scs->static_config.sframe_posi.sframe_qp_offsets,
4799
0
               config_struct->sframe_posi.sframe_qp_offsets,
4800
0
               sizeof(config_struct->sframe_posi.sframe_qp_offsets[0]) * config_struct->sframe_posi.sframe_qp_num);
4801
0
    }
4802
533
    scs->static_config.sframe_posi.sframe_qp_num = config_struct->sframe_posi.sframe_qp_num;
4803
4804
    // Color description
4805
533
    scs->static_config.color_primaries          = config_struct->color_primaries;
4806
533
    scs->static_config.transfer_characteristics = config_struct->transfer_characteristics;
4807
533
    scs->static_config.matrix_coefficients      = config_struct->matrix_coefficients;
4808
533
    scs->static_config.color_range              = config_struct->color_range;
4809
533
    scs->static_config.chroma_sample_position   = config_struct->chroma_sample_position;
4810
533
    scs->static_config.mastering_display        = config_struct->mastering_display;
4811
533
    scs->static_config.content_light_level      = config_struct->content_light_level;
4812
4813
    // switch frame
4814
533
    scs->static_config.sframe_dist      = config_struct->sframe_dist;
4815
533
    scs->static_config.sframe_mode      = config_struct->sframe_mode;
4816
533
    scs->static_config.sframe_qp        = config_struct->sframe_qp;
4817
533
    scs->static_config.sframe_qp_offset = config_struct->sframe_qp_offset;
4818
533
    scs->seq_header.max_frame_width  = config_struct->forced_max_frame_width > 0 ? config_struct->forced_max_frame_width
4819
533
         : scs->static_config.sframe_dist > 0 || scs->static_config.sframe_posi.sframe_posis ? 16384
4820
533
                                                                                             : scs->max_input_luma_width;
4821
533
    scs->seq_header.max_frame_height = config_struct->forced_max_frame_height > 0
4822
533
        ? config_struct->forced_max_frame_height
4823
533
        : scs->static_config.sframe_dist > 0 || scs->static_config.sframe_posi.sframe_posis
4824
533
        ? 8704
4825
533
        : scs->max_input_luma_height;
4826
533
    scs->static_config.force_key_frames = config_struct->force_key_frames;
4827
4828
    // QM
4829
533
    scs->static_config.enable_qm           = config_struct->enable_qm;
4830
533
    scs->static_config.min_qm_level        = config_struct->min_qm_level;
4831
533
    scs->static_config.max_qm_level        = config_struct->max_qm_level;
4832
533
    scs->static_config.min_chroma_qm_level = config_struct->min_chroma_qm_level;
4833
533
    scs->static_config.max_chroma_qm_level = config_struct->max_chroma_qm_level;
4834
533
    if (scs->static_config.enable_qm && scs->static_config.min_qm_level == 15 &&
4835
0
        scs->static_config.max_qm_level == 15 && scs->static_config.min_chroma_qm_level == 15 &&
4836
0
        scs->static_config.max_chroma_qm_level == 15) {
4837
0
        SVT_WARN("Quantization matrices will be forced off since all min/max quant matrix levels are set to 15\n");
4838
0
        scs->static_config.enable_qm = 0;
4839
0
    }
4840
533
    if (scs->static_config.enable_qm && scs->static_config.lossless) {
4841
0
        SVT_WARN("Quantization matrices will be forced off since lossless coding is applied\n");
4842
0
        scs->static_config.enable_qm = 0;
4843
0
    }
4844
533
    scs->static_config.startup_mg_size   = config_struct->startup_mg_size;
4845
533
    scs->static_config.startup_qp_offset = config_struct->startup_qp_offset;
4846
533
    scs->static_config.enable_roi_map    = config_struct->enable_roi_map;
4847
4848
    // Variance Boost
4849
533
    scs->static_config.enable_variance_boost   = config_struct->enable_variance_boost;
4850
533
    scs->static_config.variance_boost_strength = config_struct->variance_boost_strength;
4851
#if OPT_OPERATIONS_BIS
4852
    scs->static_config.variance_octile = scs->static_config.enable_variance_boost ? config_struct->variance_octile : 0;
4853
#else
4854
533
    scs->static_config.variance_octile = config_struct->variance_octile;
4855
533
#endif
4856
533
    scs->static_config.variance_boost_curve = config_struct->variance_boost_curve;
4857
4858
    // Temporal filtering strength
4859
533
    scs->static_config.tf_strength = config_struct->tf_strength;
4860
4861
    // Frame-level luminance-based QP bias
4862
533
    scs->static_config.luminance_qp_bias = config_struct->luminance_qp_bias;
4863
4864
    // Sharpness
4865
533
    scs->static_config.sharpness = config_struct->sharpness;
4866
4867
    // QP scaling compression
4868
533
    scs->static_config.qp_scale_compress_strength = config_struct->qp_scale_compress_strength;
4869
4870
    // Adaptive film grain
4871
533
    scs->static_config.adaptive_film_grain = config_struct->adaptive_film_grain;
4872
4873
    // Max TX size
4874
533
    scs->static_config.max_tx_size = config_struct->max_tx_size;
4875
4876
    // Extended CRF
4877
533
    scs->static_config.extended_crf_qindex_offset = config_struct->extended_crf_qindex_offset;
4878
4879
    // AC bias
4880
533
    scs->static_config.ac_bias = config_struct->ac_bias;
4881
4882
    // HBD-MDS
4883
533
    scs->static_config.hbd_mds = config_struct->hbd_mds;
4884
4885
    // Override settings for Still IQ tune
4886
533
    if (scs->static_config.tune == TUNE_IQ) {
4887
0
        SVT_WARN(
4888
0
            "Tune IQ overrides: sharpness, Var. Boost strength/curve, enable-qm and min/max level, max TX size and "
4889
0
            "SCM\n");
4890
0
        scs->static_config.enable_qm               = 1;
4891
0
        scs->static_config.min_qm_level            = 4;
4892
0
        scs->static_config.max_qm_level            = 10;
4893
0
        scs->static_config.min_chroma_qm_level     = 4;
4894
0
        scs->static_config.max_chroma_qm_level     = 10;
4895
0
        scs->static_config.sharpness               = 7;
4896
0
        scs->static_config.enable_variance_boost   = 1;
4897
0
        scs->static_config.variance_boost_strength = 3;
4898
0
        scs->static_config.variance_boost_curve    = 2;
4899
0
        scs->static_config.max_tx_size             = scs->static_config.qp <= 45 ? 32 : 64;
4900
0
        scs->static_config.screen_content_mode     = 3;
4901
533
    } else if (scs->static_config.tune == TUNE_MS_SSIM) {
4902
0
        SVT_WARN("Tune MS_SSIM overrides: sharpness, Var. Boost strength/curve, enable-qm and min/max level\n");
4903
0
        scs->static_config.enable_qm               = 1;
4904
0
        scs->static_config.min_qm_level            = 4;
4905
0
        scs->static_config.max_qm_level            = 10;
4906
0
        scs->static_config.min_chroma_qm_level     = 4;
4907
0
        scs->static_config.max_chroma_qm_level     = 10;
4908
0
        scs->static_config.sharpness               = 7;
4909
0
        scs->static_config.enable_variance_boost   = 1;
4910
0
        scs->static_config.variance_boost_strength = 3;
4911
0
        scs->static_config.variance_boost_curve    = 2;
4912
0
    }
4913
533
#if FTR_TUNE_VMAF
4914
533
    else if (scs->static_config.tune == TUNE_VMAF) {
4915
0
        SVT_WARN("Tune VMAF: a pre-processing / unsharp masking is applied\n");
4916
0
    }
4917
533
#endif
4918
533
    return;
4919
533
}
4920
4921
/**********************************
4922
4923
* Set Parameter
4924
**********************************/
4925
EB_API EbErrorType svt_av1_enc_set_parameter(EbComponentType*          svt_enc_component,
4926
533
                                             EbSvtAv1EncConfiguration* config_struct) {
4927
533
    if (svt_enc_component == NULL) {
4928
0
        return EB_ErrorBadParameter;
4929
0
    }
4930
4931
533
    EbEncHandle*        enc_handle = (EbEncHandle*)svt_enc_component->p_component_private;
4932
533
    SequenceControlSet* scs        = enc_handle->scs_instance->scs;
4933
533
    copy_api_from_app(scs, config_struct);
4934
4935
533
    EbErrorType return_error = svt_av1_verify_settings(scs);
4936
4937
533
    if (return_error == EB_ErrorBadParameter) {
4938
0
        return EB_ErrorBadParameter;
4939
0
    }
4940
4941
533
    if (scs->static_config.avif) {
4942
533
        scs->seq_header.still_picture                = 1;
4943
533
        scs->seq_header.reduced_still_picture_header = 1;
4944
533
    }
4945
533
    set_param_based_on_input(scs);
4946
    // Initialize the Prediction Structure Group
4947
533
    EB_NO_THROW_NEW(enc_handle->scs_instance->enc_ctx->prediction_structure_group_ptr,
4948
533
                    svt_aom_prediction_structure_group_ctor);
4949
533
    if (!enc_handle->scs_instance->enc_ctx->prediction_structure_group_ptr) {
4950
0
        return EB_ErrorInsufficientResources;
4951
0
    }
4952
533
    return_error = load_default_buffer_configuration_settings(scs);
4953
4954
533
    svt_av1_print_lib_params(scs);
4955
4956
    // free frame scale events after copy to encoder
4957
533
    if (config_struct->frame_scale_evts.resize_denoms) {
4958
0
        EB_FREE(config_struct->frame_scale_evts.resize_denoms);
4959
0
    }
4960
533
    if (config_struct->frame_scale_evts.resize_kf_denoms) {
4961
0
        EB_FREE(config_struct->frame_scale_evts.resize_kf_denoms);
4962
0
    }
4963
533
    if (config_struct->frame_scale_evts.start_frame_nums) {
4964
0
        EB_FREE(config_struct->frame_scale_evts.start_frame_nums);
4965
0
    }
4966
533
    memset(&config_struct->frame_scale_evts, 0, sizeof(SvtAv1FrameScaleEvts));
4967
4968
    // free sframe position list
4969
533
    if (config_struct->sframe_posi.sframe_qps) {
4970
0
        EB_FREE(config_struct->sframe_posi.sframe_qps);
4971
0
    }
4972
533
    if (config_struct->sframe_posi.sframe_qp_offsets) {
4973
0
        EB_FREE(config_struct->sframe_posi.sframe_qp_offsets);
4974
0
    }
4975
533
    if (config_struct->sframe_posi.sframe_posis) {
4976
0
        EB_FREE(config_struct->sframe_posi.sframe_posis);
4977
0
    }
4978
533
    memset(&config_struct->sframe_posi, 0, sizeof(SvtAv1SFramePositions));
4979
4980
533
    return return_error;
4981
533
}
4982
4983
EB_API EbErrorType svt_av1_enc_stream_header(EbComponentType*     svt_enc_component,
4984
0
                                             EbBufferHeaderType** output_stream_ptr) {
4985
0
    EbErrorType return_error = EB_ErrorNone;
4986
4987
0
    if (!svt_enc_component) {
4988
0
        return EB_ErrorBadParameter;
4989
0
    }
4990
4991
0
    EbEncHandle*        enc_handle = (EbEncHandle*)svt_enc_component->p_component_private;
4992
0
    SequenceControlSet* scs        = enc_handle->scs_instance->scs;
4993
0
    Bitstream           bitstream;
4994
0
    OutputBitstreamUnit output_bitstream;
4995
0
    EbBufferHeaderType* output_stream_buffer;
4996
0
    uint32_t output_buffer_size = svt_aom_get_out_buffer_size(scs->max_input_luma_width, scs->max_input_luma_height);
4997
0
    memset(&bitstream, 0, sizeof(Bitstream));
4998
0
    memset(&output_bitstream, 0, sizeof(OutputBitstreamUnit));
4999
0
    bitstream.output_bitstream_ptr = &output_bitstream;
5000
0
    EB_MALLOC_OBJECT(output_stream_buffer);
5001
0
    EB_MALLOC_ARRAY_NO_CHECK(output_stream_buffer->p_buffer, output_buffer_size);
5002
0
    if (!output_stream_buffer->p_buffer) {
5003
0
        EB_FREE(output_stream_buffer);
5004
0
        return EB_ErrorInsufficientResources;
5005
0
    }
5006
5007
0
    output_stream_buffer->size          = sizeof(EbBufferHeaderType);
5008
0
    output_stream_buffer->n_alloc_len   = output_buffer_size;
5009
0
    output_stream_buffer->p_app_private = NULL;
5010
0
    output_stream_buffer->pic_type      = EB_AV1_INVALID_PICTURE;
5011
0
    output_stream_buffer->n_filled_len  = 0;
5012
5013
0
    bitstream.output_bitstream_ptr->buffer_begin_av1 = output_stream_buffer->p_buffer;
5014
5015
0
    svt_aom_output_bitstream_reset(bitstream.output_bitstream_ptr);
5016
5017
    // Code the SPS
5018
0
    svt_aom_encode_sps_av1(&bitstream, scs);
5019
5020
0
    output_stream_buffer->n_filled_len = (uint32_t)(bitstream.output_bitstream_ptr->buffer_av1 -
5021
0
                                                    bitstream.output_bitstream_ptr->buffer_begin_av1);
5022
5023
0
    *output_stream_ptr = output_stream_buffer;
5024
5025
0
    return return_error;
5026
0
}
5027
5028
0
EB_API EbErrorType svt_av1_enc_stream_header_release(EbBufferHeaderType* stream_header_ptr) {
5029
0
    EbErrorType return_error = EB_ErrorNone;
5030
5031
0
    if (!stream_header_ptr || !(stream_header_ptr->p_buffer)) {
5032
0
        return EB_ErrorBadParameter;
5033
0
    }
5034
5035
0
    EB_FREE_ARRAY(stream_header_ptr->p_buffer);
5036
0
    EB_FREE(stream_header_ptr);
5037
5038
0
    return return_error;
5039
0
}
5040
5041
/***********************************************
5042
**** Copy the input buffer from the
5043
**** sample application to the library buffers
5044
************************************************/
5045
/*
5046
 Down sample and Copy the input buffer
5047
from the sample application to the library buffers
5048
*/
5049
/********************************************
5050
 * downsample_2d_c_16_zero2bit_skipall
5051
 *      downsample the input by skipping three pixels and zero out the two LSB bit
5052
 ********************************************/
5053
static void downsample_2d_c_16_zero2bit_skipall(uint16_t* input_samples, // input parameter, input samples Ptr
5054
                                                uint32_t  input_stride, // input parameter, input stride
5055
                                                uint32_t  input_area_width, // input parameter, input area width
5056
                                                uint32_t  input_area_height, // input parameter, input area height
5057
                                                uint8_t*  decim_8b_samples, // output parameter, decimated samples Ptr
5058
                                                uint32_t  decim_stride, // input parameter, output stride
5059
                                                uint32_t  decim_step) // input parameter, decimation amount in pixels
5060
0
{
5061
0
    uint32_t       horizontal_index;
5062
0
    uint32_t       vertical_index;
5063
0
    uint32_t       input_stripe_stride = input_stride * decim_step;
5064
0
    uint32_t       decim_horizontal_index;
5065
0
    const uint32_t half_decim_step = decim_step >> 1;
5066
5067
0
    for (input_samples += half_decim_step * input_stride, vertical_index = half_decim_step;
5068
0
         vertical_index < input_area_height;
5069
0
         vertical_index += decim_step) {
5070
0
        uint16_t* prev_input_line = input_samples - input_stride;
5071
0
        for (horizontal_index = half_decim_step, decim_horizontal_index = 0; horizontal_index < input_area_width;
5072
0
             horizontal_index += decim_step, decim_horizontal_index++) {
5073
0
            decim_8b_samples[decim_horizontal_index] = (uint8_t)((prev_input_line[horizontal_index - 1]) >> 2);
5074
0
        }
5075
0
        input_samples += input_stripe_stride;
5076
0
        decim_8b_samples += decim_stride;
5077
0
    }
5078
5079
0
    return;
5080
0
}
5081
5082
/********************************************
5083
 * downsample_2d_c_skipall
5084
 *      downsample the input by skipping three pixels
5085
 ********************************************/
5086
static void downsample_2d_c_skipall(uint8_t* input_samples, // input parameter, input samples Ptr
5087
                                    uint32_t input_stride, // input parameter, input stride
5088
                                    uint32_t input_area_width, // input parameter, input area width
5089
                                    uint32_t input_area_height, // input parameter, input area height
5090
                                    uint8_t* decim_samples, // output parameter, decimated samples Ptr
5091
                                    uint32_t decim_stride, // input parameter, output stride
5092
                                    uint32_t decim_step) // input parameter, decimation amount in pixels
5093
0
{
5094
0
    uint32_t       horizontal_index;
5095
0
    uint32_t       vertical_index;
5096
0
    uint32_t       input_stripe_stride = input_stride * decim_step;
5097
0
    uint32_t       decim_horizontal_index;
5098
0
    const uint32_t half_decim_step = decim_step >> 1;
5099
5100
0
    for (input_samples += half_decim_step * input_stride, vertical_index = half_decim_step;
5101
0
         vertical_index < input_area_height;
5102
0
         vertical_index += decim_step) {
5103
0
        uint8_t* prev_input_line = input_samples - input_stride;
5104
0
        for (horizontal_index = half_decim_step, decim_horizontal_index = 0; horizontal_index < input_area_width;
5105
0
             horizontal_index += decim_step, decim_horizontal_index++) {
5106
0
            decim_samples[decim_horizontal_index] = (uint32_t)prev_input_line[horizontal_index - 1];
5107
0
        }
5108
0
        input_samples += input_stripe_stride;
5109
0
        decim_samples += decim_stride;
5110
0
    }
5111
5112
0
    return;
5113
0
}
5114
5115
/***********************************************
5116
 Down sample and Copy the input buffer
5117
from the sample application to the library buffers
5118
************************************************/
5119
static EbErrorType downsample_copy_frame_buffer(SequenceControlSet* scs, uint8_t* destination, uint8_t* destination_y8b,
5120
0
                                                uint8_t* source, int pass) {
5121
0
    EbErrorType return_error = EB_ErrorNone;
5122
5123
0
    EbPictureBufferDesc* input_pic             = (EbPictureBufferDesc*)destination;
5124
0
    EbPictureBufferDesc* y8b_input_picture_ptr = (EbPictureBufferDesc*)destination_y8b;
5125
0
    EbSvtIOFormat*       input_ptr             = (EbSvtIOFormat*)source;
5126
5127
0
    uint32_t luma_width  = (uint32_t)(input_pic->width - scs->max_input_pad_right);
5128
0
    uint32_t luma_height = (uint32_t)(input_pic->height - scs->max_input_pad_bottom);
5129
5130
0
    const uint8_t  subsampling_x = (input_pic->color_format == EB_YUV444 ? 0 : 1);
5131
0
    const uint8_t  subsampling_y = ((input_pic->color_format == EB_YUV444 || input_pic->color_format == EB_YUV422) ? 0
5132
0
                                                                                                                   : 1);
5133
0
    const uint32_t chroma_width  = (luma_width + subsampling_x) >> subsampling_x;
5134
0
    const uint32_t chroma_height = (luma_height + subsampling_y) >> subsampling_y;
5135
5136
0
    if (scs->static_config.encoder_bit_depth == EB_EIGHT_BIT) {
5137
0
        downsample_2d_c_skipall(input_ptr->luma,
5138
0
                                input_ptr->y_stride,
5139
0
                                luma_width << 1,
5140
0
                                luma_height << 1,
5141
0
                                y8b_input_picture_ptr->y_buffer,
5142
0
                                input_pic->y_stride,
5143
0
                                2);
5144
5145
0
        if (pass != ENCODE_FIRST_PASS) {
5146
0
            downsample_2d_c_skipall(input_ptr->cb,
5147
0
                                    input_ptr->cb_stride,
5148
0
                                    chroma_width << 1,
5149
0
                                    chroma_height << 1,
5150
0
                                    input_pic->u_buffer,
5151
0
                                    input_pic->u_stride,
5152
0
                                    2);
5153
0
            downsample_2d_c_skipall(input_ptr->cr,
5154
0
                                    input_ptr->cr_stride,
5155
0
                                    chroma_width << 1,
5156
0
                                    chroma_height << 1,
5157
0
                                    input_pic->v_buffer,
5158
0
                                    input_pic->v_stride,
5159
0
                                    2);
5160
0
        }
5161
0
    } else { // 10bit packed
5162
0
        downsample_2d_c_16_zero2bit_skipall((uint16_t*)input_ptr->luma,
5163
0
                                            input_ptr->y_stride,
5164
0
                                            luma_width << 1,
5165
0
                                            luma_height << 1,
5166
0
                                            y8b_input_picture_ptr->y_buffer,
5167
0
                                            y8b_input_picture_ptr->y_stride,
5168
0
                                            2);
5169
5170
0
        memset(
5171
0
            input_pic->y_buffer_bit_inc - ((input_pic->border + (input_pic->y_stride_bit_inc * input_pic->border)) / 4),
5172
0
            0,
5173
0
            input_pic->luma_size / 4);
5174
5175
0
        if (pass != ENCODE_FIRST_PASS) {
5176
0
            downsample_2d_c_16_zero2bit_skipall((uint16_t*)input_ptr->cb,
5177
0
                                                input_ptr->cb_stride,
5178
0
                                                chroma_width << 1,
5179
0
                                                chroma_height << 1,
5180
0
                                                input_pic->u_buffer,
5181
0
                                                y8b_input_picture_ptr->u_stride,
5182
0
                                                2);
5183
5184
0
            memset(input_pic->u_buffer_bit_inc -
5185
0
                       (((input_pic->border >> subsampling_x) +
5186
0
                         (input_pic->u_stride_bit_inc * (input_pic->border >> subsampling_y))) /
5187
0
                        4),
5188
0
                   0,
5189
0
                   input_pic->chroma_size / 4);
5190
5191
0
            downsample_2d_c_16_zero2bit_skipall((uint16_t*)input_ptr->cr,
5192
0
                                                input_ptr->cr_stride,
5193
0
                                                chroma_width << 1,
5194
0
                                                chroma_height << 1,
5195
0
                                                input_pic->v_buffer,
5196
0
                                                y8b_input_picture_ptr->v_stride,
5197
0
                                                2);
5198
5199
0
            memset(input_pic->v_buffer_bit_inc -
5200
0
                       (((input_pic->border >> subsampling_x) +
5201
0
                         (input_pic->v_stride_bit_inc * (input_pic->border >> subsampling_y))) /
5202
0
                        4),
5203
0
                   0,
5204
0
                   input_pic->chroma_size / 4);
5205
0
        }
5206
0
    }
5207
0
    return return_error;
5208
0
}
5209
5210
/*
5211
 Copy the input buffer
5212
from the sample application to the library buffers
5213
*/
5214
5215
static EbErrorType copy_frame_buffer(SequenceControlSet* scs, uint8_t* destination, uint8_t* destination_y8b,
5216
533
                                     uint8_t* source, int pass) {
5217
533
    EbErrorType return_error = EB_ErrorNone;
5218
5219
533
    EbPictureBufferDesc* input_pic             = (EbPictureBufferDesc*)destination;
5220
533
    EbPictureBufferDesc* y8b_input_picture_ptr = (EbPictureBufferDesc*)destination_y8b;
5221
533
    EbSvtIOFormat*       input_ptr             = (EbSvtIOFormat*)source;
5222
5223
533
    uint32_t luma_width  = (uint32_t)(input_pic->width - scs->max_input_pad_right);
5224
533
    uint32_t luma_height = (uint32_t)(input_pic->height - scs->max_input_pad_bottom);
5225
5226
533
    const uint8_t  subsampling_x = (input_pic->color_format == EB_YUV444 ? 0 : 1);
5227
533
    const uint8_t  subsampling_y = ((input_pic->color_format == EB_YUV444 || input_pic->color_format == EB_YUV422) ? 0
5228
533
                                                                                                                   : 1);
5229
533
    const uint32_t chroma_width  = (luma_width + subsampling_x) >> subsampling_x;
5230
533
    const uint32_t chroma_height = (luma_height + subsampling_y) >> subsampling_y;
5231
5232
533
    if (scs->static_config.encoder_bit_depth == EB_EIGHT_BIT) {
5233
533
        svt_av1_copy_wxh_8bit(input_ptr->luma,
5234
533
                              input_ptr->y_stride,
5235
533
                              y8b_input_picture_ptr->y_buffer,
5236
533
                              input_pic->y_stride,
5237
533
                              luma_height,
5238
533
                              luma_width);
5239
533
        svt_av1_copy_wxh_8bit(
5240
533
            input_ptr->cb, input_ptr->cb_stride, input_pic->u_buffer, input_pic->u_stride, chroma_height, chroma_width);
5241
533
        svt_av1_copy_wxh_8bit(
5242
533
            input_ptr->cr, input_ptr->cr_stride, input_pic->v_buffer, input_pic->v_stride, chroma_height, chroma_width);
5243
533
    } else { // 10bit packed
5244
0
        uint32_t comp_stride_y = input_pic->y_stride / 4;
5245
5246
0
        uint32_t comp_stride_uv = input_pic->u_stride / 4;
5247
5248
0
        svt_unpack_and_2bcompress((uint16_t*)input_ptr->luma,
5249
0
                                  input_ptr->y_stride,
5250
0
                                  y8b_input_picture_ptr->y_buffer,
5251
0
                                  y8b_input_picture_ptr->y_stride,
5252
0
                                  input_pic->y_buffer_bit_inc,
5253
0
                                  comp_stride_y,
5254
0
                                  luma_width,
5255
0
                                  luma_height);
5256
0
        if (pass != ENCODE_FIRST_PASS) {
5257
0
            svt_unpack_and_2bcompress((uint16_t*)input_ptr->cb,
5258
0
                                      input_ptr->cb_stride,
5259
0
                                      input_pic->u_buffer,
5260
0
                                      input_pic->u_stride,
5261
0
                                      input_pic->u_buffer_bit_inc,
5262
0
                                      comp_stride_uv,
5263
0
                                      chroma_width,
5264
0
                                      chroma_height);
5265
5266
0
            svt_unpack_and_2bcompress((uint16_t*)input_ptr->cr,
5267
0
                                      input_ptr->cr_stride,
5268
0
                                      input_pic->v_buffer,
5269
0
                                      input_pic->v_stride,
5270
0
                                      input_pic->v_buffer_bit_inc,
5271
0
                                      comp_stride_uv,
5272
0
                                      chroma_width,
5273
0
                                      chroma_height);
5274
0
        }
5275
0
    }
5276
533
    return return_error;
5277
533
}
5278
5279
0
static EbErrorType copy_private_data_list(EbBufferHeaderType* dst, EbBufferHeaderType* src) {
5280
0
    EbErrorType     return_error = EB_ErrorNone;
5281
0
    EbPrivDataNode* p_src_node   = (EbPrivDataNode*)src->p_app_private;
5282
0
    EbPrivDataNode* p_first_node = NULL;
5283
0
    EbPrivDataNode* p_new_node   = NULL;
5284
0
    while (p_src_node) {
5285
        // skip undefined data type and throw an error in debugging
5286
0
        if (p_src_node->node_type < PRIVATE_DATA || p_src_node->node_type >= PRIVATE_DATA_TYPES) {
5287
0
            svt_aom_assert_err(0, "unknown private data types inserted!");
5288
0
            continue;
5289
0
        }
5290
0
        if (p_first_node == NULL) {
5291
0
            EB_MALLOC(p_new_node, sizeof(*p_src_node));
5292
0
            p_first_node = p_new_node;
5293
0
        } else {
5294
0
            EB_MALLOC(p_new_node->next, sizeof(*p_src_node));
5295
0
            p_new_node = p_new_node->next;
5296
0
        }
5297
0
        p_new_node->node_type = p_src_node->node_type;
5298
0
        p_new_node->size      = p_src_node->size;
5299
        // not copy data from the private data pass through the encoder
5300
0
        if (p_src_node->node_type == PRIVATE_DATA || p_src_node->node_type == ROI_MAP_EVENT) {
5301
0
            p_new_node->data = p_src_node->data;
5302
0
        } else {
5303
0
            EB_MALLOC(p_new_node->data, p_src_node->size);
5304
0
            memcpy(p_new_node->data, p_src_node->data, p_src_node->size);
5305
0
        }
5306
0
        p_new_node->next = NULL;
5307
0
        p_src_node       = p_src_node->next;
5308
0
    }
5309
0
    dst->p_app_private = p_first_node;
5310
0
    return return_error;
5311
0
}
5312
5313
/**************************************
5314
* svt_input_buffer_header_update: update the parameters in input_buffer_header for changing the resolution on the fly
5315
**************************************/
5316
0
EbErrorType svt_input_buffer_header_update(EbBufferHeaderType* input_buffer, SequenceControlSet* scs, bool noy8b) {
5317
0
    EbPictureBufferDescInitData input_pic_buf_desc_init_data;
5318
0
    EbSvtAv1EncConfiguration*   config   = &scs->static_config;
5319
0
    uint8_t                     is_16bit = config->encoder_bit_depth > 8 ? 1 : 0;
5320
5321
0
    input_pic_buf_desc_init_data.max_width = !(scs->max_input_luma_width % 8)
5322
0
        ? scs->max_input_luma_width
5323
0
        : scs->max_input_luma_width + (scs->max_input_luma_width % 8);
5324
5325
0
    input_pic_buf_desc_init_data.max_height = !(scs->max_input_luma_height % 8)
5326
0
        ? scs->max_input_luma_height
5327
0
        : scs->max_input_luma_height + (scs->max_input_luma_height % 8);
5328
5329
0
    input_pic_buf_desc_init_data.bit_depth    = (EbBitDepth)config->encoder_bit_depth;
5330
0
    input_pic_buf_desc_init_data.color_format = (EbColorFormat)config->encoder_color_format;
5331
5332
0
    input_pic_buf_desc_init_data.border = scs->border;
5333
5334
0
    input_pic_buf_desc_init_data.split_mode = is_16bit ? true : false;
5335
5336
0
    input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
5337
0
    input_pic_buf_desc_init_data.is_16bit_pipeline  = 0;
5338
5339
    // Enhanced Picture Buffer
5340
0
    if (!noy8b) {
5341
0
        svt_picture_buffer_desc_update((EbPictureBufferDesc*)input_buffer->p_buffer,
5342
0
                                       (EbPtr)&input_pic_buf_desc_init_data);
5343
0
    } else {
5344
0
        svt_picture_buffer_desc_noy8b_update((EbPictureBufferDesc*)input_buffer->p_buffer,
5345
0
                                             (EbPtr)&input_pic_buf_desc_init_data);
5346
0
    }
5347
5348
0
    return EB_ErrorNone;
5349
0
}
5350
5351
/**************************************
5352
* svt_input_y8b_update: update the parameters in input_y8b for changing the resolution on the fly
5353
**************************************/
5354
0
EbErrorType svt_input_y8b_update(EbBufferHeaderType* input_buffer, SequenceControlSet* scs) {
5355
0
    EbPictureBufferDescInitData input_pic_buf_desc_init_data;
5356
0
    EbSvtAv1EncConfiguration*   config   = &scs->static_config;
5357
0
    uint8_t                     is_16bit = 0;
5358
5359
0
    input_pic_buf_desc_init_data.max_width = !(scs->max_input_luma_width % 8)
5360
0
        ? scs->max_input_luma_width
5361
0
        : scs->max_input_luma_width + (scs->max_input_luma_width % 8);
5362
5363
0
    input_pic_buf_desc_init_data.max_height   = !(scs->max_input_luma_height % 8)
5364
0
          ? scs->max_input_luma_height
5365
0
          : scs->max_input_luma_height + (scs->max_input_luma_height % 8);
5366
0
    input_pic_buf_desc_init_data.bit_depth    = EB_EIGHT_BIT;
5367
0
    input_pic_buf_desc_init_data.color_format = (EbColorFormat)config->encoder_color_format;
5368
5369
0
    input_pic_buf_desc_init_data.border = scs->border;
5370
5371
0
    input_pic_buf_desc_init_data.split_mode = is_16bit ? true : false;
5372
5373
0
    input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_LUMA_MASK; //allocate for 8bit Luma only
5374
0
    input_pic_buf_desc_init_data.is_16bit_pipeline  = 0;
5375
5376
    // Enhanced Picture Buffer
5377
0
    svt_picture_buffer_desc_update((EbPictureBufferDesc*)input_buffer->p_buffer, (EbPtr)&input_pic_buf_desc_init_data);
5378
5379
0
    return EB_ErrorNone;
5380
0
}
5381
5382
/*
5383
    memset the library input buffer(s)
5384
*/
5385
static void memset_input_buffer(SequenceControlSet* scs, EbBufferHeaderType* dst, EbBufferHeaderType* dst_y8b,
5386
0
                                EbBufferHeaderType* src, int pass) {
5387
    // Copy the higher level structure
5388
0
    dst->n_alloc_len  = src->n_alloc_len;
5389
0
    dst->n_filled_len = src->n_filled_len;
5390
0
    dst->flags        = src->flags;
5391
0
    dst->pts          = src->pts;
5392
0
    dst->n_tick_count = src->n_tick_count;
5393
0
    dst->size         = src->size;
5394
0
    dst->qp           = src->qp;
5395
0
    dst->pic_type     = src->pic_type;
5396
0
    if (scs->first_pass_downsample) {
5397
        // memset the picture buffer
5398
0
        if (src->p_buffer != NULL) {
5399
0
            EbPictureBufferDesc* y8b_input_picture_ptr = (EbPictureBufferDesc*)dst_y8b->p_buffer;
5400
0
            EbPictureBufferDesc* input_pic             = (EbPictureBufferDesc*)dst->p_buffer;
5401
0
            memset(y8b_input_picture_ptr->buffer_alloc, 0, y8b_input_picture_ptr->buffer_alloc_sz);
5402
0
            memset(input_pic->buffer_alloc, 0, input_pic->buffer_alloc_sz);
5403
0
        }
5404
0
    } else if (pass != ENCODE_FIRST_PASS) {
5405
        // memset the picture buffer
5406
0
        if (src->p_buffer != NULL) {
5407
0
            EbPictureBufferDesc* y8b_input_picture_ptr = (EbPictureBufferDesc*)dst_y8b->p_buffer;
5408
0
            EbPictureBufferDesc* input_pic             = (EbPictureBufferDesc*)dst->p_buffer;
5409
0
            memset(y8b_input_picture_ptr->buffer_alloc, 0, y8b_input_picture_ptr->buffer_alloc_sz);
5410
0
            memset(input_pic->buffer_alloc, 0, input_pic->buffer_alloc_sz);
5411
            // Copy the metadata array
5412
0
            if (svt_aom_copy_metadata_buffer(dst, src->metadata) != EB_ErrorNone) {
5413
0
                dst->metadata = NULL;
5414
0
            }
5415
0
        }
5416
0
    }
5417
5418
    // Copy the private data list
5419
0
    if (src->p_app_private) {
5420
0
        copy_private_data_list(dst, src);
5421
0
    } else {
5422
0
        dst->p_app_private = NULL;
5423
0
    }
5424
0
}
5425
5426
/*
5427
 Copy the input buffer header content
5428
from the sample application to the library buffers
5429
*/
5430
static void copy_input_buffer(SequenceControlSet* scs, EbBufferHeaderType* dst, EbBufferHeaderType* dst_y8b,
5431
1.06k
                              EbBufferHeaderType* src, int pass) {
5432
    // Copy the higher level structure
5433
1.06k
    dst->n_alloc_len  = src->n_alloc_len;
5434
1.06k
    dst->n_filled_len = src->n_filled_len;
5435
1.06k
    dst->flags        = src->flags;
5436
1.06k
    dst->pts          = src->pts;
5437
1.06k
    dst->n_tick_count = src->n_tick_count;
5438
1.06k
    dst->size         = src->size;
5439
1.06k
    dst->qp           = src->qp;
5440
1.06k
    dst->pic_type     = src->pic_type;
5441
1.06k
    if (scs->first_pass_downsample) {
5442
        // Copy the picture buffer
5443
0
        if (src->p_buffer != NULL) {
5444
0
            downsample_copy_frame_buffer(scs, dst->p_buffer, dst_y8b->p_buffer, src->p_buffer, pass);
5445
0
        }
5446
1.06k
    } else if (pass != ENCODE_FIRST_PASS) {
5447
        // Bypass copy for the unecessary picture in IPPP pass
5448
        // Copy the picture buffer
5449
1.06k
        if (src->p_buffer != NULL) {
5450
533
            copy_frame_buffer(scs, dst->p_buffer, dst_y8b->p_buffer, src->p_buffer, pass);
5451
            // Copy the metadata array
5452
533
            if (svt_aom_copy_metadata_buffer(dst, src->metadata) != EB_ErrorNone) {
5453
533
                dst->metadata = NULL;
5454
533
            }
5455
533
        }
5456
1.06k
    }
5457
5458
    // Copy the private data list
5459
1.06k
    if (src->p_app_private) {
5460
0
        copy_private_data_list(dst, src);
5461
1.06k
    } else {
5462
1.06k
        dst->p_app_private = NULL;
5463
1.06k
    }
5464
1.06k
}
5465
5466
// Update the input picture definitions: resolution of the sequence
5467
static EbErrorType validate_on_the_fly_settings(EbBufferHeaderType* input_ptr, SequenceControlSet* scs,
5468
1.06k
                                                EbHandle config_mutex) {
5469
1.06k
    EbPrivDataNode* node = (EbPrivDataNode*)input_ptr->p_app_private;
5470
1.06k
    while (node) {
5471
0
        if (node->node_type == RES_CHANGE_EVENT) {
5472
0
            SvtAv1InputPicDef* node_data = (SvtAv1InputPicDef*)node->data;
5473
0
            if (input_ptr->pic_type != EB_AV1_KEY_PICTURE) {
5474
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5475
0
                SVT_ERROR("Resolution change on the fly not supported for non key frames\n");
5476
0
                return EB_ErrorBadParameter;
5477
0
            } else if ((node_data->input_luma_height > scs->max_initial_input_luma_height) ||
5478
0
                       (node_data->input_luma_width > scs->max_initial_input_luma_width)) {
5479
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5480
0
                SVT_ERROR(
5481
0
                    "Resolution cannot be changed to anything greater than the original picture width and height\n");
5482
0
                return EB_ErrorBadParameter;
5483
0
            } else if (scs->static_config.superres_mode > SUPERRES_NONE) {
5484
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5485
0
                SVT_ERROR("Resolution change on the fly is not supported when Super-Resolution mode is on\n");
5486
0
                return EB_ErrorBadParameter;
5487
0
            } else if (scs->static_config.resize_mode != RESIZE_NONE) {
5488
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5489
0
                SVT_ERROR("Resolution change on the fly is not supported when Reference Scaling mode is on\n");
5490
0
                return EB_ErrorBadParameter;
5491
0
            } else if (scs->static_config.pred_structure != LOW_DELAY) {
5492
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5493
0
                SVT_ERROR("Resolution change on the fly is only supported for Low-Delay mode\n");
5494
0
                return EB_ErrorBadParameter;
5495
0
            } else if (scs->static_config.pass != ENC_SINGLE_PASS) {
5496
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5497
0
                SVT_ERROR("Resolution change on the fly is only supported for single pass encoding\n");
5498
0
                return EB_ErrorBadParameter;
5499
0
            } else if (scs->static_config.tile_rows || scs->static_config.tile_columns) {
5500
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5501
0
                SVT_ERROR("Resolution change on the fly is not supported when tiles are being used\n");
5502
0
                return EB_ErrorBadParameter;
5503
0
            } else if (scs->static_config.aq_mode == 1) {
5504
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5505
0
                SVT_ERROR(
5506
0
                    "Resolution change on the fly is not supported for segment based adaptive quantization (--aq-mode "
5507
0
                    "== 1)\n");
5508
0
                return EB_ErrorBadParameter;
5509
0
            } else if (node_data->input_luma_width < 64) {
5510
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5511
0
                SVT_ERROR("Resolution change on the fly is not supported for luma width less than 64\n");
5512
0
                return EB_ErrorBadParameter;
5513
0
            } else if (node_data->input_luma_height < 64) {
5514
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5515
0
                SVT_ERROR("Resolution change on the fly is not supported for luma height less than 64\n");
5516
0
                return EB_ErrorBadParameter;
5517
0
            } else if (scs->static_config.encoder_bit_depth == EB_TEN_BIT) {
5518
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5519
0
                SVT_ERROR("Resolution change on the fly is not supported for 10-bit encoding\n");
5520
0
                return EB_ErrorBadParameter;
5521
0
            } else {
5522
0
                svt_aom_assert_err(node->size == sizeof(SvtAv1InputPicDef),
5523
0
                                   "invalid private data of type RES_CHANGE_EVENT");
5524
0
                SvtAv1InputPicDef* input_pic_def = (SvtAv1InputPicDef*)node->data;
5525
0
                svt_block_on_mutex(config_mutex);
5526
                // Check if a resolution change occurred
5527
0
                scs->max_input_luma_width  = input_pic_def->input_luma_width;
5528
0
                scs->max_input_luma_height = input_pic_def->input_luma_height;
5529
0
                scs->max_input_pad_right   = input_pic_def->input_pad_right;
5530
0
                scs->max_input_pad_bottom  = input_pic_def->input_pad_bottom;
5531
0
                svt_release_mutex(config_mutex);
5532
0
            }
5533
0
        } else if (node->node_type == RATE_CHANGE_EVENT) {
5534
0
            SvtAv1RateInfo* node_data = (SvtAv1RateInfo*)node->data;
5535
0
            if ((scs->static_config.target_bit_rate != node_data->target_bit_rate) &&
5536
0
                !(scs->static_config.rtc && scs->static_config.pred_structure == LOW_DELAY &&
5537
0
                  scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR)) {
5538
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5539
0
                SVT_ERROR("TBR change on the fly not supported for any mode other than RTC Low-Delay CBR\n");
5540
0
                return EB_ErrorBadParameter;
5541
0
            }
5542
0
            if (node_data->seq_qp != 0) {
5543
0
                if (node_data->seq_qp > MAX_QP_VALUE) {
5544
0
                    input_ptr->flags = EB_BUFFERFLAG_EOS;
5545
0
                    SVT_ERROR("QP change on the fly requires a QP value less than or equal to 63\n");
5546
0
                    return EB_ErrorBadParameter;
5547
0
                }
5548
0
            }
5549
0
            if (node_data->target_bit_rate > 100000000) {
5550
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5551
0
                SVT_ERROR("TBR change on the fly requires that the target bit rate must be between [0, 100000] kbps\n");
5552
0
                return EB_ErrorBadParameter;
5553
0
            }
5554
0
        } else if (node->node_type == FRAME_RATE_CHANGE_EVENT) {
5555
0
            SvtAv1FrameRateInfo* node_data = (SvtAv1FrameRateInfo*)node->data;
5556
0
            if (!((scs->static_config.pred_structure == LOW_DELAY) &&
5557
0
                  (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR))) {
5558
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5559
0
                SVT_ERROR("Frame rate change on the fly not supported for any mode other than Low-Delay CBR\n");
5560
0
                return EB_ErrorBadParameter;
5561
0
            }
5562
0
            if (node_data->frame_rate_numerator == 0 || node_data->frame_rate_denominator == 0) {
5563
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5564
0
                SVT_ERROR(
5565
0
                    "Frame rate change on the fly requires that he frame_rate_numerator and frame_rate_denominator "
5566
0
                    "must be greater than 0\n");
5567
0
                return EB_ErrorBadParameter;
5568
0
            }
5569
0
        } else if (node->node_type == PRESET_CHANGE_EVENT) {
5570
0
            SvtAv1PresetInfo* node_data = (SvtAv1PresetInfo*)node->data;
5571
0
            if (!((scs->static_config.pred_structure == LOW_DELAY) &&
5572
0
                  (scs->static_config.rate_control_mode == SVT_AV1_RC_MODE_CBR) && scs->static_config.rtc)) {
5573
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5574
0
                SVT_ERROR("Preset change on the fly not supported for any mode other than RTC Low-Delay CBR\n");
5575
0
                return EB_ErrorBadParameter;
5576
0
            }
5577
0
            if (node_data->enc_mode < scs->static_config.enc_mode || node_data->enc_mode > MAX_ENC_PRESET) {
5578
0
                input_ptr->flags = EB_BUFFERFLAG_EOS;
5579
0
                SVT_ERROR("Preset change on the fly requires enc_mode in range [%d, %d]\n",
5580
0
                          scs->static_config.enc_mode,
5581
0
                          MAX_ENC_PRESET);
5582
0
                return EB_ErrorBadParameter;
5583
0
            }
5584
0
        }
5585
0
        node = node->next;
5586
0
    }
5587
1.06k
    return EB_ErrorNone;
5588
1.06k
}
5589
5590
/**********************************
5591
* Empty This Buffer
5592
**********************************/
5593
1.06k
EB_API EbErrorType svt_av1_enc_send_picture(EbComponentType* svt_enc_component, EbBufferHeaderType* p_buffer) {
5594
1.06k
    EbErrorType         return_val     = EB_ErrorNone;
5595
1.06k
    EbEncHandle*        enc_handle_ptr = (EbEncHandle*)svt_enc_component->p_component_private;
5596
1.06k
    EbObjectWrapper*    eb_wrapper_ptr;
5597
1.06k
    EbBufferHeaderType* app_hdr    = p_buffer;
5598
1.06k
    enc_handle_ptr->frame_received = true;
5599
5600
1.06k
    SequenceControlSet* scs = enc_handle_ptr->scs_instance->scs;
5601
1.06k
    if (scs->static_config.avif && (p_buffer->flags & EB_BUFFERFLAG_EOS) != EB_BUFFERFLAG_EOS && p_buffer->pts == 3) {
5602
0
        p_buffer->flags              = EB_BUFFERFLAG_EOS;
5603
0
        p_buffer->pic_type           = EB_AV1_INVALID_PICTURE;
5604
0
        enc_handle_ptr->eos_received = 1;
5605
0
        return_val                   = EB_ErrorBadParameter;
5606
0
        SVT_ERROR(
5607
0
            "AVIF flag is specified, but more than 3 frames were sent. This will not produce an AVIF image sequence "
5608
0
            "(avis)!\n");
5609
0
    }
5610
5611
    // Exit the library if we detect an invalid API input buffer @ the previous library call
5612
1.06k
    if (enc_handle_ptr->is_prev_valid == false) {
5613
0
        p_buffer->flags              = EB_BUFFERFLAG_EOS;
5614
0
        p_buffer->pic_type           = EB_AV1_INVALID_PICTURE;
5615
0
        enc_handle_ptr->eos_received = 1;
5616
0
        return_val                   = EB_ErrorBadParameter;
5617
0
        SVT_ERROR("Invalid API input buffer size detected. Please ignore the output stream\n");
5618
0
    }
5619
5620
    // Get new Luma-8b buffer & a new (Chroma-8b + Luma-Chroma-2bit) buffers; Lib will release once done.
5621
1.06k
    EbObjectWrapper* y8b_wrapper;
5622
1.06k
    svt_get_empty_object(enc_handle_ptr->input_y8b_buffer_producer_fifo_ptr, &y8b_wrapper);
5623
    // Update the input picture definitions: resolution of the sequence
5624
1.06k
    if (validate_on_the_fly_settings(p_buffer, scs, enc_handle_ptr->scs_instance->config_mutex)) {
5625
0
        return_val                   = EB_ErrorBadParameter;
5626
0
        enc_handle_ptr->eos_received = 1;
5627
0
    }
5628
    // if resolution has changed, and the y8b_wrapper settings do not match scs settings, update y8b_wrapper settings
5629
1.06k
    if (buffer_update_needed((EbBufferHeaderType*)y8b_wrapper->object_ptr, scs)) {
5630
0
        svt_input_y8b_update((EbBufferHeaderType*)y8b_wrapper->object_ptr, scs);
5631
0
    }
5632
    //set live count to 1 to be decremented at the end of the encode in RC
5633
1.06k
    svt_object_inc_live_count(y8b_wrapper, 1);
5634
5635
    // svt_object_inc_live_count(y8b_wrapper, 1);
5636
5637
1.06k
    svt_get_empty_object(enc_handle_ptr->input_buffer_producer_fifo_ptr, &eb_wrapper_ptr);
5638
    // if resolution has changed, and the input_buffer settings do not match scs settings, update input_buffer settings
5639
1.06k
    if (buffer_update_needed((EbBufferHeaderType*)eb_wrapper_ptr->object_ptr, scs)) {
5640
0
        svt_input_buffer_header_update((EbBufferHeaderType*)eb_wrapper_ptr->object_ptr, scs, true);
5641
0
    }
5642
5643
    //set live count to 1 to be decremented at the end of the encode in RC, and released
5644
    //this would also allow low delay TF to retain pictures
5645
1.06k
    svt_object_inc_live_count(eb_wrapper_ptr, 1);
5646
5647
1.06k
    enc_handle_ptr->eos_received += p_buffer->flags & EB_BUFFERFLAG_EOS;
5648
5649
    // copy the Luma 8bit part into y8b buffer and the rest of samples into the regular buffer
5650
1.06k
    EbBufferHeaderType* lib_y8b_hdr = (EbBufferHeaderType*)y8b_wrapper->object_ptr;
5651
1.06k
    EbBufferHeaderType* lib_reg_hdr = (EbBufferHeaderType*)eb_wrapper_ptr->object_ptr;
5652
5653
    // check whether the n_filled_len has enough samples to be processed
5654
1.06k
    EbPictureBufferDesc*      input_pic      = (EbPictureBufferDesc*)lib_y8b_hdr->p_buffer;
5655
1.06k
    EbSvtAv1EncConfiguration* config         = &scs->static_config;
5656
1.06k
    bool                      is_16bit_input = config->encoder_bit_depth > EB_EIGHT_BIT;
5657
5658
1.06k
    const uint8_t subsampling_x = (config->encoder_color_format == EB_YUV444 ? 0 : 1);
5659
1.06k
    const uint8_t subsampling_y =
5660
1.06k
        ((config->encoder_color_format == EB_YUV444 || config->encoder_color_format == EB_YUV422) ? 0 : 1);
5661
1.06k
    const size_t luma_width    = input_pic->width - scs->max_input_pad_right;
5662
1.06k
    const size_t luma_height   = input_pic->height - scs->max_input_pad_bottom;
5663
1.06k
    const size_t chroma_width  = (luma_width + subsampling_x) >> subsampling_x;
5664
1.06k
    const size_t chroma_height = (luma_height + subsampling_y) >> subsampling_y;
5665
1.06k
    const size_t read_size     = (luma_width * luma_height + 2 * chroma_width * chroma_height) << is_16bit_input;
5666
5667
1.06k
    if (app_hdr->p_buffer != NULL && read_size > app_hdr->n_filled_len) {
5668
        // memset the library input buffer(s) if the API input buffer is not large enough
5669
        // this operation is necessary to avoid a potential crash when processing an invalid input
5670
        // the library will still process the current input and then exit
5671
0
        memset_input_buffer(scs, lib_reg_hdr, lib_y8b_hdr, app_hdr, 0);
5672
0
        enc_handle_ptr->is_prev_valid = false;
5673
1.06k
    } else {
5674
1.06k
        copy_input_buffer(scs, lib_reg_hdr, lib_y8b_hdr, app_hdr, 0);
5675
1.06k
    }
5676
5677
    //Take a new App-RessCoord command
5678
1.06k
    EbObjectWrapper* input_cmd_wrp;
5679
1.06k
    svt_get_empty_object(enc_handle_ptr->input_cmd_producer_fifo_ptr, &input_cmd_wrp);
5680
1.06k
    InputCommand* input_cmd_obj = (InputCommand*)input_cmd_wrp->object_ptr;
5681
    //Fill the command with two picture buffers
5682
1.06k
    input_cmd_obj->eb_input_wrapper_ptr = eb_wrapper_ptr;
5683
1.06k
    input_cmd_obj->y8b_wrapper          = y8b_wrapper;
5684
    //Send to Lib
5685
1.06k
    svt_post_full_object(input_cmd_wrp);
5686
5687
1.06k
#if CONFIG_SINGLE_THREAD_KERNEL
5688
    // In single-thread mode, run the entire pipeline synchronously
5689
1.06k
    if (enc_handle_ptr->kernel_dispatcher.active) {
5690
0
        svt_kernel_dispatcher_run(&enc_handle_ptr->kernel_dispatcher);
5691
0
    }
5692
1.06k
#endif
5693
5694
1.06k
    return return_val;
5695
1.06k
}
5696
5697
0
static void copy_output_recon_buffer(EbBufferHeaderType* dst, EbBufferHeaderType* src) {
5698
    // copy output Bitstream fileds
5699
0
    dst->size          = src->size;
5700
0
    dst->n_alloc_len   = src->n_alloc_len;
5701
0
    dst->n_filled_len  = src->n_filled_len;
5702
0
    dst->p_app_private = src->p_app_private;
5703
0
    dst->n_tick_count  = src->n_tick_count;
5704
0
    dst->pts           = src->pts;
5705
0
    dst->dts           = src->dts;
5706
0
    dst->flags         = src->flags;
5707
0
    dst->pic_type      = src->pic_type;
5708
5709
    // Copy the metadata array
5710
0
    if (svt_aom_copy_metadata_buffer(dst, src->metadata) != EB_ErrorNone) {
5711
0
        dst->metadata = NULL;
5712
0
    }
5713
5714
    // Copy the picture buffer
5715
0
    if (src->p_buffer) {
5716
0
        svt_memcpy(dst->p_buffer, src->p_buffer, src->n_filled_len);
5717
0
    }
5718
5719
0
    return;
5720
0
}
5721
5722
/**********************************
5723
* svt_av1_enc_get_packet sends out packet
5724
**********************************/
5725
EB_API EbErrorType svt_av1_enc_get_packet(EbComponentType* svt_enc_component, EbBufferHeaderType** p_buffer,
5726
2.13k
                                          unsigned char pic_send_done) {
5727
2.13k
    EbErrorType                     return_error   = EB_ErrorNone;
5728
2.13k
    EbEncHandle*                    enc_handle     = (EbEncHandle*)svt_enc_component->p_component_private;
5729
2.13k
    EbObjectWrapper*                eb_wrapper_ptr = NULL;
5730
2.13k
    EbBufferHeaderType*             packet;
5731
2.13k
    const EbSvtAv1EncConfiguration* cfg = &enc_handle->scs_instance->scs->static_config;
5732
5733
    // check if the user is claiming that the last picture has been sent
5734
    // without actually signalling it through svt_av1_enc_send_picture()
5735
2.13k
    assert(!(!enc_handle->eos_received && pic_send_done));
5736
5737
    // if we have already sent out an EOS, then the user should not be calling
5738
    // this function again, as it will just block inside svt_get_full_object()
5739
2.13k
    if (enc_handle->eos_sent) {
5740
533
        *p_buffer = NULL;
5741
533
        return EB_NoErrorEmptyQueue;
5742
533
    }
5743
5744
1.59k
    if (pic_send_done || cfg->pred_structure == LOW_DELAY) {
5745
1.06k
        svt_get_full_object(enc_handle->output_stream_buffer_consumer_fifo_ptr, &eb_wrapper_ptr);
5746
1.06k
    } else {
5747
533
        svt_get_full_object_non_blocking(enc_handle->output_stream_buffer_consumer_fifo_ptr, &eb_wrapper_ptr);
5748
533
    }
5749
5750
1.59k
    if (eb_wrapper_ptr) {
5751
1.06k
        packet = (EbBufferHeaderType*)eb_wrapper_ptr->object_ptr;
5752
1.06k
        if (packet->flags & 0xfffffff0) {
5753
0
            return_error = EB_ErrorMax;
5754
0
        }
5755
        // return the output stream buffer
5756
1.06k
        *p_buffer = packet;
5757
5758
        // check if we have reached the end of the output stream
5759
1.06k
        enc_handle->eos_sent += packet->flags & EB_BUFFERFLAG_EOS;
5760
5761
        // save the wrapper pointer for the release
5762
1.06k
        (*p_buffer)->wrapper_ptr = (void*)eb_wrapper_ptr;
5763
1.06k
    } else {
5764
533
        return_error = EB_NoErrorEmptyQueue;
5765
533
    }
5766
1.59k
    return return_error;
5767
2.13k
}
5768
5769
1.06k
EB_API void svt_av1_enc_release_out_buffer(EbBufferHeaderType** p_buffer) {
5770
1.06k
    if (p_buffer && (*p_buffer)->wrapper_ptr) {
5771
1.06k
        if ((*p_buffer)->p_buffer) {
5772
533
            EB_FREE((*p_buffer)->p_buffer);
5773
533
        }
5774
        // Release out put buffer back into the pool
5775
1.06k
        svt_release_object((EbObjectWrapper*)(*p_buffer)->wrapper_ptr);
5776
1.06k
    }
5777
1.06k
    return;
5778
1.06k
}
5779
5780
/**********************************
5781
* Fill This Buffer
5782
**********************************/
5783
0
EB_API EbErrorType svt_av1_get_recon(EbComponentType* svt_enc_component, EbBufferHeaderType* p_buffer) {
5784
0
    EbErrorType      return_error   = EB_ErrorNone;
5785
0
    EbEncHandle*     enc_handle     = (EbEncHandle*)svt_enc_component->p_component_private;
5786
0
    EbObjectWrapper* eb_wrapper_ptr = NULL;
5787
5788
0
    if (enc_handle->scs_instance->scs->static_config.recon_enabled) {
5789
0
        svt_get_full_object_non_blocking(enc_handle->output_recon_buffer_consumer_fifo_ptr, &eb_wrapper_ptr);
5790
5791
0
        if (eb_wrapper_ptr) {
5792
0
            EbBufferHeaderType* obj_ptr = (EbBufferHeaderType*)eb_wrapper_ptr->object_ptr;
5793
0
            copy_output_recon_buffer(p_buffer, obj_ptr);
5794
5795
0
            if (p_buffer->flags != EB_BUFFERFLAG_EOS && p_buffer->flags != 0) {
5796
0
                return_error = EB_ErrorMax;
5797
0
            }
5798
0
            if (obj_ptr->metadata) {
5799
0
                svt_metadata_array_free(&obj_ptr->metadata);
5800
0
            }
5801
0
            svt_release_object((EbObjectWrapper*)eb_wrapper_ptr);
5802
0
        } else {
5803
0
            return_error = EB_NoErrorEmptyQueue;
5804
0
        }
5805
0
    } else {
5806
        // recon is not enabled
5807
0
        return_error = EB_ErrorMax;
5808
0
    }
5809
5810
0
    return return_error;
5811
0
}
5812
5813
/**********************************
5814
* Encoder Error Handling
5815
**********************************/
5816
0
static void lib_svt_encoder_send_error_exit(EbPtr hComponent, uint32_t error_code) {
5817
0
    EbComponentType*    svt_enc_component = (EbComponentType*)hComponent;
5818
0
    EbEncHandle*        enc_handle        = (EbEncHandle*)svt_enc_component->p_component_private;
5819
0
    EbObjectWrapper*    eb_wrapper_ptr    = NULL;
5820
0
    EbBufferHeaderType* output_packet;
5821
5822
0
    svt_get_empty_object(enc_handle->output_stream_buffer_consumer_fifo_ptr, &eb_wrapper_ptr);
5823
5824
0
    output_packet = (EbBufferHeaderType*)eb_wrapper_ptr->object_ptr;
5825
5826
0
    output_packet->size     = 0;
5827
0
    output_packet->flags    = error_code;
5828
0
    output_packet->p_buffer = NULL;
5829
5830
0
    svt_post_full_object(eb_wrapper_ptr);
5831
0
}
5832
5833
0
EB_API const char* svt_av1_get_version(void) {
5834
0
    return SVT_AV1_CVS_VERSION;
5835
0
}
5836
5837
533
EB_API void svt_av1_print_version(void) {
5838
533
    SVT_INFO("-------------------------------------------\n");
5839
533
    SVT_INFO("SVT [version]:\tSVT-AV1 Encoder Lib %s\n", SVT_AV1_CVS_VERSION);
5840
533
    const char* compiler =
5841
533
#if defined(__clang__)
5842
533
        __VERSION__ "\t"
5843
#elif defined(__GNUC__)
5844
        "GCC " __VERSION__ "\t"
5845
#elif defined(_MSC_VER) && (_MSC_VER >= 1930)
5846
        "Visual Studio 2022"
5847
#elif defined(_MSC_VER) && (_MSC_VER >= 1920)
5848
        "Visual Studio 2019"
5849
#elif defined(_MSC_VER) && (_MSC_VER >= 1910)
5850
        "Visual Studio 2017"
5851
#elif defined(_MSC_VER) && (_MSC_VER >= 1900)
5852
        "Visual Studio 2015"
5853
#elif defined(_MSC_VER)
5854
        "Visual Studio (old)"
5855
#else
5856
        "unknown compiler"
5857
#endif
5858
533
        ;
5859
533
    SVT_INFO("SVT [build]  :\t%s %zu bit\n", compiler, sizeof(void*) * 8);
5860
533
#if !REPRODUCIBLE_BUILDS
5861
533
    SVT_INFO("LIB Build date: %s %s\n", __DATE__, __TIME__);
5862
533
#endif
5863
533
    SVT_INFO("-------------------------------------------\n");
5864
533
}
5865
5866
/**
5867
 * Set log callback, wrapper around internal function to ensure public functions are stored in one place.
5868
 */
5869
0
EB_API void svt_av1_set_log_callback(SvtAv1LogCallback callback, void* context) {
5870
0
#if !CONFIG_LOG_QUIET
5871
0
    svt_aom_log_set_callback(callback, context);
5872
#else
5873
    UNUSED(callback);
5874
    UNUSED(context);
5875
#endif
5876
0
}
5877
5878
/**********************************
5879
* Encoder Handle Initialization
5880
**********************************/
5881
533
static EbErrorType init_svt_av1_encoder_handle(EbComponentType* hComponent) {
5882
533
    EbErrorType      return_error      = EB_ErrorNone;
5883
533
    EbComponentType* svt_enc_component = hComponent;
5884
533
    EbEncHandle*     handle;
5885
533
    svt_av1_print_version();
5886
5887
533
    enc_switch_to_real_time();
5888
5889
    // Set Component Size & Version
5890
533
    svt_enc_component->size = sizeof(EbComponentType);
5891
5892
533
    EB_NEW(handle, svt_enc_handle_ctor, svt_enc_component);
5893
533
    svt_enc_component->p_component_private = handle;
5894
5895
533
    return return_error;
5896
533
}
5897
5898
2.13k
static EbErrorType allocate_frame_buffer(SequenceControlSet* scs, EbBufferHeaderType* input_buffer, bool noy8b) {
5899
2.13k
    EbErrorType                 return_error = EB_ErrorNone;
5900
2.13k
    EbPictureBufferDescInitData input_pic_buf_desc_init_data;
5901
2.13k
    EbSvtAv1EncConfiguration*   config   = &scs->static_config;
5902
2.13k
    uint8_t                     is_16bit = config->encoder_bit_depth > 8 ? 1 : 0;
5903
5904
2.13k
    input_pic_buf_desc_init_data.max_width = !(scs->max_input_luma_width % 8)
5905
2.13k
        ? scs->max_input_luma_width
5906
2.13k
        : scs->max_input_luma_width + (scs->max_input_luma_width % 8);
5907
5908
2.13k
    input_pic_buf_desc_init_data.max_height = !(scs->max_input_luma_height % 8)
5909
2.13k
        ? scs->max_input_luma_height
5910
2.13k
        : scs->max_input_luma_height + (scs->max_input_luma_height % 8);
5911
5912
2.13k
    input_pic_buf_desc_init_data.bit_depth    = (EbBitDepth)config->encoder_bit_depth;
5913
2.13k
    input_pic_buf_desc_init_data.color_format = (EbColorFormat)config->encoder_color_format;
5914
5915
2.13k
    input_pic_buf_desc_init_data.border = scs->border;
5916
5917
2.13k
    input_pic_buf_desc_init_data.split_mode = is_16bit ? true : false;
5918
5919
2.13k
    input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
5920
2.13k
    input_pic_buf_desc_init_data.is_16bit_pipeline  = 0;
5921
5922
    // Enhanced Picture Buffer
5923
2.13k
    {
5924
2.13k
        EbPictureBufferDesc* buf;
5925
2.13k
        if (!noy8b) {
5926
0
            EB_NEW(buf, svt_picture_buffer_desc_ctor, (EbPtr)&input_pic_buf_desc_init_data);
5927
2.13k
        } else {
5928
2.13k
            EB_NEW(buf, svt_picture_buffer_desc_ctor_noy8b, (EbPtr)&input_pic_buf_desc_init_data);
5929
2.13k
        }
5930
2.13k
        input_buffer->p_buffer = (uint8_t*)buf;
5931
2.13k
    }
5932
5933
0
    return return_error;
5934
2.13k
}
5935
5936
/*
5937
  allocate an input sample Luma-8bit buffer
5938
*/
5939
2.13k
static EbErrorType allocate_y8b_frame_buffer(SequenceControlSet* scs, EbBufferHeaderType* input_buffer) {
5940
2.13k
    EbErrorType                 return_error = EB_ErrorNone;
5941
2.13k
    EbPictureBufferDescInitData input_pic_buf_desc_init_data;
5942
2.13k
    EbSvtAv1EncConfiguration*   config   = &scs->static_config;
5943
2.13k
    uint8_t                     is_16bit = 0;
5944
5945
2.13k
    input_pic_buf_desc_init_data.max_width = !(scs->max_input_luma_width % 8)
5946
2.13k
        ? scs->max_input_luma_width
5947
2.13k
        : scs->max_input_luma_width + (scs->max_input_luma_width % 8);
5948
5949
2.13k
    input_pic_buf_desc_init_data.max_height   = !(scs->max_input_luma_height % 8)
5950
2.13k
          ? scs->max_input_luma_height
5951
2.13k
          : scs->max_input_luma_height + (scs->max_input_luma_height % 8);
5952
2.13k
    input_pic_buf_desc_init_data.bit_depth    = EB_EIGHT_BIT;
5953
2.13k
    input_pic_buf_desc_init_data.color_format = (EbColorFormat)config->encoder_color_format;
5954
5955
2.13k
    input_pic_buf_desc_init_data.border = scs->border;
5956
5957
2.13k
    input_pic_buf_desc_init_data.split_mode = is_16bit ? true : false;
5958
5959
2.13k
    input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_LUMA_MASK; //allocate for 8bit Luma only
5960
2.13k
    input_pic_buf_desc_init_data.is_16bit_pipeline  = 0;
5961
5962
    // Enhanced Picture Buffer
5963
2.13k
    {
5964
2.13k
        EbPictureBufferDesc* buf;
5965
2.13k
        EB_NEW(buf, svt_picture_buffer_desc_ctor, (EbPtr)&input_pic_buf_desc_init_data);
5966
2.13k
        input_buffer->p_buffer = (uint8_t*)buf;
5967
2.13k
    }
5968
5969
0
    return return_error;
5970
2.13k
}
5971
5972
/*
5973
  create a luma 8bit buffer descriptor
5974
*/
5975
2.13k
EbErrorType svt_input_y8b_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
5976
2.13k
    EbBufferHeaderType* input_buffer;
5977
2.13k
    SequenceControlSet* scs = (SequenceControlSet*)object_init_data_ptr;
5978
5979
2.13k
    *object_dbl_ptr = NULL;
5980
2.13k
    EB_CALLOC(input_buffer, 1, sizeof(EbBufferHeaderType));
5981
2.13k
    *object_dbl_ptr = (EbPtr)input_buffer;
5982
    // Initialize Header
5983
2.13k
    input_buffer->size = sizeof(EbBufferHeaderType);
5984
5985
2.13k
    EbErrorType return_error = allocate_y8b_frame_buffer(scs, input_buffer);
5986
2.13k
    if (return_error != EB_ErrorNone) {
5987
0
        return return_error;
5988
0
    }
5989
5990
2.13k
    input_buffer->p_app_private = NULL;
5991
5992
2.13k
    return EB_ErrorNone;
5993
2.13k
}
5994
5995
/*
5996
  free a luma 8bit buffer descriptor
5997
*/
5998
2.13k
void svt_input_y8b_destroyer(EbPtr p) {
5999
2.13k
    EbBufferHeaderType*  obj = (EbBufferHeaderType*)p;
6000
2.13k
    EbPictureBufferDesc* buf = (EbPictureBufferDesc*)obj->p_buffer;
6001
2.13k
    if (buf) {
6002
2.13k
        EB_FREE_ALIGNED_ARRAY(buf->y_buffer_bit_inc);
6003
2.13k
        EB_FREE_ALIGNED_ARRAY(buf->u_buffer_bit_inc);
6004
2.13k
        EB_FREE_ALIGNED_ARRAY(buf->v_buffer_bit_inc);
6005
2.13k
    }
6006
6007
2.13k
    EB_DELETE(buf);
6008
2.13k
    EB_FREE(obj);
6009
2.13k
}
6010
6011
/**************************************
6012
* EbBufferHeaderType Constructor
6013
**************************************/
6014
2.13k
EbErrorType svt_input_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
6015
2.13k
    EbBufferHeaderType* input_buffer;
6016
2.13k
    SequenceControlSet* scs = (SequenceControlSet*)object_init_data_ptr;
6017
6018
2.13k
    *object_dbl_ptr = NULL;
6019
2.13k
    EB_CALLOC(input_buffer, 1, sizeof(EbBufferHeaderType));
6020
2.13k
    *object_dbl_ptr = (EbPtr)input_buffer;
6021
    // Initialize Header
6022
2.13k
    input_buffer->size = sizeof(EbBufferHeaderType);
6023
6024
2.13k
    EbErrorType return_error = allocate_frame_buffer(scs, input_buffer, true);
6025
2.13k
    if (return_error != EB_ErrorNone) {
6026
0
        return return_error;
6027
0
    }
6028
6029
2.13k
    input_buffer->p_app_private = NULL;
6030
6031
2.13k
    return EB_ErrorNone;
6032
2.13k
}
6033
6034
2.13k
void svt_input_buffer_header_destroyer(EbPtr p) {
6035
2.13k
    EbBufferHeaderType*  obj = (EbBufferHeaderType*)p;
6036
2.13k
    EbPictureBufferDesc* buf = (EbPictureBufferDesc*)obj->p_buffer;
6037
2.13k
    if (buf) {
6038
2.13k
        EB_FREE_ALIGNED_ARRAY(buf->buffer_alloc);
6039
2.13k
        buf->buffer_alloc_sz  = 0;
6040
2.13k
        buf->y_buffer         = NULL;
6041
2.13k
        buf->u_buffer         = NULL;
6042
2.13k
        buf->v_buffer         = NULL;
6043
2.13k
        buf->y_buffer_bit_inc = NULL;
6044
2.13k
        buf->u_buffer_bit_inc = NULL;
6045
2.13k
        buf->v_buffer_bit_inc = NULL;
6046
2.13k
    }
6047
6048
2.13k
    EB_DELETE(buf);
6049
2.13k
    EB_FREE(obj);
6050
2.13k
}
6051
6052
0
EbErrorType svt_overlay_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
6053
0
    EbBufferHeaderType* input_buffer;
6054
0
    SequenceControlSet* scs = (SequenceControlSet*)object_init_data_ptr;
6055
6056
0
    *object_dbl_ptr = NULL;
6057
0
    EB_CALLOC(input_buffer, 1, sizeof(EbBufferHeaderType));
6058
0
    *object_dbl_ptr = (EbPtr)input_buffer;
6059
    // Initialize Header
6060
0
    input_buffer->size = sizeof(EbBufferHeaderType);
6061
6062
0
    EbErrorType return_error = allocate_frame_buffer(scs, input_buffer, false);
6063
0
    if (return_error != EB_ErrorNone) {
6064
0
        return return_error;
6065
0
    }
6066
6067
0
    input_buffer->p_app_private = NULL;
6068
6069
0
    return EB_ErrorNone;
6070
0
}
6071
6072
/**************************************
6073
* EbBufferHeaderType Constructor
6074
**************************************/
6075
3.19k
EbErrorType svt_output_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
6076
3.19k
    (void)object_init_data_ptr;
6077
3.19k
    EbBufferHeaderType* out_buf_ptr;
6078
6079
3.19k
    *object_dbl_ptr = NULL;
6080
3.19k
    EB_CALLOC(out_buf_ptr, 1, sizeof(EbBufferHeaderType));
6081
3.19k
    *object_dbl_ptr = (EbPtr)out_buf_ptr;
6082
6083
    // Initialize Header
6084
3.19k
    out_buf_ptr->size = sizeof(EbBufferHeaderType);
6085
    // p_buffer and n_alloc_len are dynamically set in EbPacketizationProcess
6086
    // out_buf_ptr->n_alloc_len;
6087
3.19k
    out_buf_ptr->p_app_private = NULL;
6088
6089
3.19k
    return EB_ErrorNone;
6090
3.19k
}
6091
6092
3.19k
void svt_output_buffer_header_destroyer(EbPtr p) {
6093
3.19k
    EbBufferHeaderType* obj = (EbBufferHeaderType*)p;
6094
3.19k
    EB_FREE(obj);
6095
3.19k
}
6096
6097
/**************************************
6098
* EbBufferHeaderType Constructor
6099
**************************************/
6100
0
EbErrorType svt_output_recon_buffer_header_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
6101
0
    EbBufferHeaderType* recon_buffer;
6102
0
    SequenceControlSet* scs       = (SequenceControlSet*)object_init_data_ptr;
6103
0
    const uint32_t      luma_size = scs->seq_header.max_frame_width * scs->seq_header.max_frame_height;
6104
    // both u and v
6105
0
    const uint32_t chroma_size = luma_size >> 1;
6106
0
    const uint32_t ten_bit     = (scs->static_config.encoder_bit_depth > 8);
6107
0
    const uint32_t frame_size  = (luma_size + chroma_size) << ten_bit;
6108
6109
0
    *object_dbl_ptr = NULL;
6110
0
    EB_CALLOC(recon_buffer, 1, sizeof(EbBufferHeaderType));
6111
0
    *object_dbl_ptr = (EbPtr)recon_buffer;
6112
6113
    // Initialize Header
6114
0
    recon_buffer->size = sizeof(EbBufferHeaderType);
6115
6116
    // Assign the variables
6117
0
    EB_MALLOC(recon_buffer->p_buffer, frame_size);
6118
6119
0
    recon_buffer->n_alloc_len   = frame_size;
6120
0
    recon_buffer->p_app_private = NULL;
6121
6122
0
    return EB_ErrorNone;
6123
0
}
6124
6125
0
void svt_output_recon_buffer_header_destroyer(EbPtr p) {
6126
0
    EbBufferHeaderType* obj = (EbBufferHeaderType*)p;
6127
0
    EB_FREE(obj->p_buffer);
6128
0
    EB_FREE(obj);
6129
0
}
6130
6131
/**********************************
6132
* svt_av1_enc_get_stream_info get stream information from encoder
6133
**********************************/
6134
EB_API EbErrorType svt_av1_enc_get_stream_info(EbComponentType* svt_enc_component, uint32_t stream_info_id,
6135
0
                                               void* info) {
6136
0
    if (stream_info_id >= SVT_AV1_STREAM_INFO_END || stream_info_id < SVT_AV1_STREAM_INFO_START) {
6137
0
        return EB_ErrorBadParameter;
6138
0
    }
6139
0
    EbEncHandle*    enc_handle       = svt_enc_component->p_component_private;
6140
0
    EncodeContext*  context          = enc_handle->scs_instance->enc_ctx;
6141
0
    SvtAv1FixedBuf* first_pass_stats = info;
6142
0
    first_pass_stats->buf            = context->stats_out.stat;
6143
0
    first_pass_stats->sz             = context->stats_out.size * sizeof(FIRSTPASS_STATS);
6144
0
    return EB_ErrorNone;
6145
0
}