Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/svt-av1/Source/Lib/Codec/md_process.c
Line
Count
Source
1
/*
2
* Copyright(c) 2019 Intel Corporation
3
*
4
* This source code is subject to the terms of the BSD 2 Clause License and
5
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
* was not distributed with this source code in the LICENSE file, you can
7
* obtain it at https://www.aomedia.org/license/software-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
12
#include <stdlib.h>
13
14
#include "utility.h"
15
#include "md_process.h"
16
#include "lambda_rate_tables.h"
17
#include "rc_process.h"
18
#include "enc_mode_config.h"
19
20
const uint8_t quantizer_to_qindex[64] = {
21
    0,   4,   8,   12,  16,  20,  24,  28,  32,  36,  40,  44,  48,  52,  56,  60,  64,  68,  72,  76,  80,  84,
22
    88,  92,  96,  100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 160, 164, 168, 172,
23
    176, 180, 184, 188, 192, 196, 200, 204, 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255};
24
25
const int percents[2][FIXED_QP_OFFSET_COUNT] = {
26
    {75, 70, 60, 20, 15, 0}, {76, 60, 30, 15, 8, 4} // libaom offsets
27
};
28
29
const uint8_t uni_psy_bias[64] = {
30
    85, 85, 85, 85, 85,  85,  85,  85,  85,  85,  85,  85,  85,  85,  85,  85,  95,  95,  95,  95,  95, 95,
31
    95, 95, 95, 95, 95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95,  95, 95,
32
    95, 95, 95, 95, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
33
};
34
35
1.91k
static void mode_decision_context_dctor(EbPtr p) {
36
1.91k
    ModeDecisionContext* obj = (ModeDecisionContext*)p;
37
38
    // MD palette search
39
1.91k
    if (obj->palette_buffer) {
40
0
        EB_FREE(obj->palette_buffer);
41
0
    }
42
1.91k
    if (obj->palette_cand_array) {
43
        // Free fields in palette_cand_array before freeing palette_cand_array
44
0
        for (int cd = 0; cd < MAX_PAL_CAND; cd++) {
45
0
            if (obj->palette_cand_array[cd].color_idx_map) {
46
0
                EB_FREE_ARRAY(obj->palette_cand_array[cd].color_idx_map);
47
0
            }
48
0
        }
49
50
0
        EB_FREE_ARRAY(obj->palette_cand_array);
51
0
    }
52
1.91k
    if (obj->palette_size_array_0) {
53
0
        EB_FREE_ARRAY(obj->palette_size_array_0);
54
0
    }
55
11.4k
    for (CandClass cand_class_it = CAND_CLASS_0; cand_class_it < CAND_CLASS_TOTAL; cand_class_it++) {
56
9.55k
        EB_FREE_ARRAY(obj->cand_buff_indices[cand_class_it]);
57
9.55k
    }
58
1.91k
    EB_FREE_ARRAY(obj->best_candidate_index_array);
59
60
1.91k
    EB_FREE_ARRAY(obj->above_txfm_context);
61
1.91k
    EB_FREE_ARRAY(obj->left_txfm_context);
62
    // Per-block coeff_tmp/recon_tmp are borrowed from these pools (freed once each).
63
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->coeff_tmp_pool);
64
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->recon_tmp_pool);
65
    // cand_bf_ptr_array[] entries are borrowed from cand_bf_pool; free the pool + the (alias)
66
    // pointer array once each — do NOT per-element delete.
67
1.91k
    EB_FREE_ARRAY(obj->cand_bf_pool);
68
1.91k
    EB_FREE_ARRAY(obj->cand_bf_ptr_array);
69
    // Candidate pred/rec_coeff/quant are borrowed from these pools (freed once each).
70
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->cand_pred_pool);
71
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->cand_rec_coeff_pool);
72
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->cand_quant_pool);
73
1.91k
    EB_FREE_ARRAY(obj->cand_bf_tx_depth_1->cand);
74
1.91k
    EB_DELETE(obj->cand_bf_tx_depth_1);
75
1.91k
    EB_FREE_ARRAY(obj->cand_bf_tx_depth_2->cand);
76
1.91k
    EB_DELETE(obj->cand_bf_tx_depth_2);
77
1.91k
    EB_FREE_ALIGNED_ARRAY(obj->cfl_temp_luma_recon16bit);
78
1.91k
    EB_FREE_ALIGNED_ARRAY(obj->cfl_temp_luma_recon);
79
1.91k
    EB_FREE_ALIGNED_ARRAY(obj->pred_buf_q3);
80
1.91k
    EB_FREE_ALIGNED_ARRAY(obj->tmp_conv_buf);
81
1.91k
    EB_FREE_ALIGNED_ARRAY(obj->seg_mask_buf);
82
1.91k
    EB_FREE_ARRAY(obj->fast_cand_array);
83
1.91k
    EB_FREE_2D(obj->injected_mvs);
84
1.91k
    EB_FREE_ARRAY(obj->injected_ref_types);
85
1.91k
    EB_FREE_ARRAY(obj->fast_cost_array);
86
1.91k
    EB_FREE_ARRAY(obj->full_cost_array);
87
1.91k
    if (obj->md_blk_arr_nsq) {
88
7.64k
        for (int i = 0; i < 3; i++) {
89
5.73k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_left_recon_16bit[i]);
90
5.73k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_top_recon_16bit[i]);
91
5.73k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_left_recon[i]);
92
5.73k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_top_recon[i]);
93
5.73k
        }
94
1.91k
    }
95
1.91k
    if (obj->md_blk_arr_nsq) {
96
1.91k
        EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].av1xd);
97
1.91k
    }
98
1.91k
    EB_FREE_ARRAY(obj->mds);
99
1.91k
    EB_FREE_ARRAY(obj->pc_tree);
100
1.91k
    EB_FREE_ARRAY(obj->tested_blk);
101
1.91k
    EB_FREE_ARRAY(obj->uv_cand_buff_indices);
102
1.91k
    obj->blocks_to_alloc = 0;
103
1.91k
    EB_FREE_ARRAY(obj->md_blk_arr_nsq);
104
1.91k
    EB_FREE_ARRAY(obj->pd0_mds0_best_cost);
105
1.91k
    if (obj->rate_est_table) {
106
0
        EB_FREE_ARRAY(obj->rate_est_table);
107
0
    }
108
109
9.55k
    for (int i = 0; i < NEAREST_NEAR_MV_CNT; i++) {
110
7.64k
        if (obj->cmp_store.pred0_buf[i]) {
111
0
            EB_FREE(obj->cmp_store.pred0_buf[i]);
112
0
        }
113
7.64k
        if (obj->cmp_store.pred1_buf[i]) {
114
0
            EB_FREE(obj->cmp_store.pred1_buf[i]);
115
0
        }
116
7.64k
    }
117
1.91k
    if (obj->residual1) {
118
0
        EB_FREE(obj->residual1);
119
0
    }
120
1.91k
    if (obj->diff10) {
121
0
        EB_FREE(obj->diff10);
122
0
    }
123
124
1.91k
    if (obj->intrapred_buf) {
125
0
        EB_FREE_2D(obj->intrapred_buf);
126
0
    }
127
128
1.91k
    if (obj->obmc_buff_0) {
129
0
        EB_FREE(obj->obmc_buff_0);
130
0
    }
131
1.91k
    if (obj->obmc_buff_1) {
132
0
        EB_FREE(obj->obmc_buff_1);
133
0
    }
134
1.91k
    if (obj->obmc_conv_buf) {
135
0
        EB_FREE(obj->obmc_conv_buf);
136
0
    }
137
1.91k
    if (obj->wsrc_buf) {
138
0
        EB_FREE(obj->wsrc_buf);
139
0
    }
140
1.91k
    if (obj->mask_buf) {
141
0
        EB_FREE(obj->mask_buf);
142
0
    }
143
    // Per-TX recon/coeff buffers are borrowed from these pools (freed once each).
144
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->tx_recon_coeff_pool);
145
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->tx_recon_pool);
146
1.91k
    svt_aom_pic_buf_desc_pool_dctor(&obj->tx_quant_coeff_pool);
147
1.91k
    EB_DELETE(obj->tx_coeffs);
148
1.91k
    EB_DELETE(obj->scratch_prediction_ptr);
149
1.91k
    EB_DELETE(obj->temp_residual);
150
1.91k
    EB_DELETE(obj->temp_recon_ptr);
151
1.91k
    EB_FREE_ARRAY(obj->full_cost_ssim_array);
152
1.91k
}
153
154
void svt_aom_set_nics(SequenceControlSet* scs, NicScalingCtrls* scaling_ctrls, uint32_t mds1_count[CAND_CLASS_TOTAL],
155
                      uint32_t mds2_count[CAND_CLASS_TOTAL], uint32_t mds3_count[CAND_CLASS_TOTAL], uint8_t pic_type,
156
                      uint32_t qp);
157
158
static void setup_mds(SequenceControlSet* scs, MdScan* mds, uint32_t* mds_idx, int index, BlockSize bsize,
159
162k
                      const int min_sq_size) {
160
162k
    mds->mds_idx = *mds_idx;
161
162k
    mds->bsize   = bsize;
162
162k
    mds->index   = index;
163
164
    // If applicable, add split depths
165
162k
    const BlockGeom* blk_geom = get_blk_geom_mds(scs->blk_geom_mds, *mds_idx);
166
162k
    const int        sq_size  = block_size_wide[bsize];
167
162k
    if (sq_size > min_sq_size) {
168
40.1k
        const BlockSize subsize             = get_partition_subsize(bsize, PARTITION_SPLIT);
169
40.1k
        const int       sq_subsize          = block_size_wide[subsize];
170
40.1k
        int             blocks_per_subdepth = (sq_subsize / min_sq_size) * (sq_subsize / min_sq_size);
171
40.1k
        int             blocks_to_skip      = 0;
172
173
91.7k
        for (int i = min_sq_size; i <= sq_subsize; i <<= 1, blocks_per_subdepth >>= 2) {
174
51.5k
            blocks_to_skip += blocks_per_subdepth;
175
51.5k
        }
176
177
40.1k
        *mds_idx += blk_geom->d1_depth_offset;
178
200k
        for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
179
160k
            mds->split[i] = mds + i * blocks_to_skip + 1;
180
160k
            setup_mds(scs, mds->split[i], mds_idx, i, subsize, min_sq_size);
181
160k
        }
182
122k
    } else {
183
122k
        *mds_idx += blk_geom->ns_depth_offset;
184
122k
    }
185
162k
}
186
187
static void setup_pc_tree(PC_TREE* pc_tree, bool (*test_blk_array)[PART_S][4], int index, BlockSize bsize,
188
162k
                          const int min_sq_size) {
189
162k
    pc_tree->bsize      = bsize;
190
162k
    pc_tree->index      = index;
191
162k
    pc_tree->tested_blk = test_blk_array[0];
192
193
    // If applicable, add split depths
194
162k
    const int sq_size = block_size_wide[bsize];
195
162k
    if (sq_size > min_sq_size) {
196
40.1k
        const BlockSize subsize             = get_partition_subsize(bsize, PARTITION_SPLIT);
197
40.1k
        const int       sq_subsize          = block_size_wide[subsize];
198
40.1k
        int             blocks_per_subdepth = (sq_subsize / min_sq_size) * (sq_subsize / min_sq_size);
199
40.1k
        int             blocks_to_skip      = 0;
200
201
91.7k
        for (int i = min_sq_size; i <= sq_subsize; i <<= 1, blocks_per_subdepth >>= 2) {
202
51.5k
            blocks_to_skip += blocks_per_subdepth;
203
51.5k
        }
204
205
200k
        for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
206
160k
            pc_tree->split[i]         = pc_tree + i * blocks_to_skip + 1;
207
160k
            pc_tree->split[i]->parent = pc_tree;
208
160k
            setup_pc_tree(pc_tree->split[i], test_blk_array + i * blocks_to_skip + 1, i, subsize, min_sq_size);
209
160k
        }
210
40.1k
    }
211
162k
}
212
213
/******************************************************
214
 * Mode Decision Context Constructor
215
 ******************************************************/
216
EbErrorType svt_aom_mode_decision_context_ctor(ModeDecisionContext* ctx, SequenceControlSet* scs,
217
                                               EbColorFormat color_format, uint8_t sb_size, EncMode enc_mode,
218
                                               uint16_t max_block_cnt, uint32_t encoder_bit_depth,
219
                                               EbFifo* mode_decision_configuration_input_fifo_ptr,
220
                                               EbFifo* mode_decision_output_fifo_ptr, uint8_t enable_hbd_mode_decision,
221
1.91k
                                               uint8_t seq_qp_mod) {
222
1.91k
    const bool allintra = scs->allintra;
223
1.91k
    const bool rtc_tune = scs->static_config.rtc;
224
1.91k
    uint32_t   buffer_index;
225
1.91k
    uint32_t   cand_index;
226
227
1.91k
    ctx->intrabc_last_dv.as_int = 0; // no last DV yet (seeds the RTC IntraBC BVP fallback)
228
1.91k
    ctx->init_max_block_cnt     = max_block_cnt;
229
1.91k
    uint32_t block_max_count_sb = max_block_cnt;
230
231
1.91k
    ctx->sb_size = sb_size;
232
1.91k
    (void)color_format;
233
234
1.91k
    ctx->dctor  = mode_decision_context_dctor;
235
1.91k
    ctx->hbd_md = enable_hbd_mode_decision;
236
237
#if CONFIG_ENABLE_FAST_DECODE_3
238
    ctx->dec_cost_ctrls = scs->dec_cost_ctrls;
239
#endif
240
241
    // Zero the tail of md_levels_buf once; it serves as permanent bottom-padding
242
    // for set_levels() and is never overwritten by svt_av1_txb_init_levels().
243
1.91k
    memset(ctx->md_levels_buf + LEVELS_TAIL_OFFSET, 0, TX_PAD_2D - LEVELS_TAIL_OFFSET);
244
245
    // Input/Output System Resource Manager FIFOs
246
1.91k
    ctx->mode_decision_configuration_input_fifo_ptr = mode_decision_configuration_input_fifo_ptr;
247
1.91k
    ctx->mode_decision_output_fifo_ptr              = mode_decision_output_fifo_ptr;
248
249
    // Maximum number of candidates MD can support
250
    // determine MAX_NICS for a given preset
251
    // get the min scaling level (the smallest scaling level is the most conservative)
252
1.91k
    uint8_t min_nic_scaling_level = NICS_SCALING_LEVELS - 1;
253
1.91k
    uint8_t stage1_scaling_num;
254
1.91k
    if (allintra) {
255
1.91k
        uint8_t nic_level  = svt_aom_get_nic_level_allintra(enc_mode);
256
1.91k
        stage1_scaling_num = MD_STAGE_NICS_SCAL_NUM[svt_aom_set_nic_controls(NULL, nic_level)][MD_STAGE_1];
257
1.91k
    } else if (rtc_tune) {
258
0
        uint8_t nic_level  = svt_aom_get_nic_level_rtc(enc_mode);
259
0
        stage1_scaling_num = MD_STAGE_NICS_SCAL_NUM[svt_aom_set_nic_controls(NULL, nic_level)][MD_STAGE_1];
260
0
    } else {
261
0
        for (uint8_t is_base = 0; is_base < 2; is_base++) {
262
0
            uint8_t nic_level         = svt_aom_get_nic_level_default(enc_mode, is_base);
263
0
            uint8_t nic_scaling_level = svt_aom_set_nic_controls(NULL, nic_level);
264
0
            min_nic_scaling_level     = MIN(min_nic_scaling_level, nic_scaling_level);
265
0
        }
266
267
0
        stage1_scaling_num = MD_STAGE_NICS_SCAL_NUM[min_nic_scaling_level][MD_STAGE_1];
268
0
    }
269
    // scale max_nics
270
1.91k
    uint32_t max_nics = 0;
271
1.91k
    {
272
1.91k
        NicScalingCtrls scaling_ctrls;
273
1.91k
        scaling_ctrls.stage1_scaling_num = stage1_scaling_num;
274
1.91k
        scaling_ctrls.stage2_scaling_num = stage1_scaling_num;
275
1.91k
        scaling_ctrls.stage3_scaling_num = stage1_scaling_num;
276
1.91k
        uint32_t mds1_count[CAND_CLASS_TOTAL];
277
1.91k
        uint32_t mds2_count[CAND_CLASS_TOTAL];
278
1.91k
        uint32_t mds3_count[CAND_CLASS_TOTAL];
279
7.64k
        for (uint8_t pic_type = 0; pic_type < NICS_PIC_TYPE; pic_type++) {
280
372k
            for (uint8_t qp = MIN_QP_VALUE; qp <= MAX_QP_VALUE; qp++) {
281
366k
                svt_aom_set_nics(scs, &scaling_ctrls, mds1_count, mds2_count, mds3_count, pic_type, qp);
282
283
366k
                uint32_t nics = 0;
284
2.20M
                for (CandClass cidx = CAND_CLASS_0; cidx < CAND_CLASS_TOTAL; cidx++) {
285
1.83M
                    nics += mds1_count[cidx];
286
1.83M
                }
287
366k
                max_nics = MAX(max_nics, nics);
288
366k
            }
289
5.73k
        }
290
1.91k
    }
291
292
    // If independent chroma search is used, need to allocate additional 84 candidate buffers
293
1.91k
    bool is_chroma_mode_0;
294
1.91k
    if (allintra) {
295
1.91k
        is_chroma_mode_0 = svt_aom_set_chroma_controls(NULL, svt_aom_get_chroma_level_allintra(enc_mode)) ==
296
1.91k
            CHROMA_MODE_0;
297
1.91k
    } else if (scs->static_config.rtc) {
298
0
        is_chroma_mode_0 = svt_aom_set_chroma_controls(NULL, svt_aom_get_chroma_level_rtc(enc_mode)) == CHROMA_MODE_0;
299
0
    } else {
300
0
        for (uint8_t is_i_slice = 0; is_i_slice < 2; is_i_slice++) {
301
0
            is_chroma_mode_0 = svt_aom_set_chroma_controls(
302
0
                                   NULL, svt_aom_get_chroma_level_default(enc_mode, is_i_slice)) == CHROMA_MODE_0;
303
0
            if (is_chroma_mode_0) {
304
0
                break;
305
0
            }
306
0
        }
307
0
    }
308
1.91k
    const uint8_t ind_uv_cands = is_chroma_mode_0 ? 84 : 0;
309
1.91k
    max_nics += CAND_CLASS_TOTAL; //need one extra temp buffer for each fast loop call
310
1.91k
    ctx->max_nics    = max_nics;
311
1.91k
    ctx->max_nics_uv = max_nics + ind_uv_cands;
312
1.91k
    EB_MALLOC_ARRAY(ctx->uv_cand_buff_indices, ctx->max_nics_uv);
313
    // Cfl scratch memory
314
1.91k
    if (SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) > EB_8_BIT_MD) {
315
0
        EB_MALLOC_ALIGNED(ctx->cfl_temp_luma_recon16bit, sizeof(uint16_t) * sb_size * sb_size);
316
0
    }
317
1.91k
    if (SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) != EB_10_BIT_MD) {
318
1.91k
        EB_MALLOC_ALIGNED(ctx->cfl_temp_luma_recon, sizeof(uint8_t) * sb_size * sb_size);
319
1.91k
    }
320
1.91k
    EB_MALLOC_ALIGNED(ctx->pred_buf_q3, CFL_BUF_SQUARE);
321
    // Hoisted inter-prediction scratch (previously large on-stack arrays).
322
1.91k
    EB_MALLOC_ALIGNED_ARRAY(ctx->tmp_conv_buf, MAX_SB_SQUARE);
323
1.91k
    EB_MALLOC_ALIGNED_ARRAY(ctx->seg_mask_buf, 2 * MAX_SB_SQUARE);
324
1.91k
    uint8_t use_update_cdf = 0;
325
1.91k
    if (allintra) {
326
1.91k
        use_update_cdf = svt_aom_get_update_cdf_level_allintra(enc_mode);
327
1.91k
    } else if (rtc_tune) {
328
0
        for (uint8_t is_islice = 0; is_islice < 2; is_islice++) {
329
0
            if (use_update_cdf) {
330
0
                break;
331
0
            }
332
0
            use_update_cdf |= svt_aom_get_update_cdf_level_rtc(enc_mode, is_islice);
333
0
        }
334
0
    } else {
335
0
        for (uint8_t is_islice = 0; is_islice < 2; is_islice++) {
336
0
            for (uint8_t is_base = 0; is_base < 2; is_base++) {
337
0
                if (use_update_cdf) {
338
0
                    break;
339
0
                }
340
0
                use_update_cdf |= svt_aom_get_update_cdf_level_default(enc_mode, is_islice, is_base);
341
0
            }
342
0
        }
343
0
    }
344
1.91k
    if (use_update_cdf) {
345
0
        EB_CALLOC_ARRAY(ctx->rate_est_table, 1);
346
1.91k
    } else {
347
1.91k
        ctx->rate_est_table = NULL;
348
1.91k
    }
349
    // Allocate buffer for inter-inter compound prediction
350
1.91k
    if (get_inter_compound_level(enc_mode)) {
351
0
        const uint8_t bits = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) > EB_8_BIT_MD ? 2 : 1;
352
0
        for (int i = 0; i < NEAREST_NEAR_MV_CNT; i++) {
353
0
            EB_MALLOC(ctx->cmp_store.pred0_buf[i], sb_size * sb_size * bits * sizeof(uint8_t));
354
0
            EB_MALLOC(ctx->cmp_store.pred1_buf[i], sb_size * sb_size * bits * sizeof(uint8_t));
355
0
        }
356
0
        EB_MALLOC(ctx->residual1, sb_size * sb_size * sizeof(ctx->residual1[0]));
357
0
        EB_MALLOC(ctx->diff10, sb_size * sb_size * sizeof(ctx->diff10[0]));
358
0
    }
359
360
    // Allocate buffer for inter-intra prediction
361
1.91k
    uint8_t ii_allowed = 0;
362
5.73k
    for (uint8_t transition_present = 0; transition_present < 2; transition_present++) {
363
3.82k
        if (ii_allowed) {
364
0
            break;
365
0
        }
366
3.82k
        ii_allowed |= svt_aom_get_inter_intra_level(enc_mode, transition_present);
367
3.82k
    }
368
1.91k
    if (ii_allowed) {
369
0
        const uint8_t bits = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) > EB_8_BIT_MD ? 2 : 1;
370
        // MAX block size for inter intra is 32x32
371
0
        EB_MALLOC_2D(ctx->intrapred_buf, INTERINTRA_MODES, 32 * 32 * bits * sizeof(ctx->intrapred_buf[0][0]));
372
0
    }
373
374
    // Allocate buffers for obmc prediction
375
1.91k
    uint8_t obmc_allowed = 0;
376
5.73k
    for (uint8_t is_base = 0; is_base < 2; is_base++) {
377
248k
        for (uint8_t qp = MIN_QP_VALUE; qp <= MAX_QP_VALUE; qp++) {
378
244k
            if (obmc_allowed) {
379
0
                break;
380
0
            }
381
244k
            obmc_allowed |= svt_aom_get_obmc_level(enc_mode, qp, seq_qp_mod);
382
244k
        }
383
3.82k
    }
384
1.91k
    if (obmc_allowed) {
385
0
        const uint8_t bits = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) > EB_8_BIT_MD ? 2 : 1;
386
0
        EB_MALLOC(ctx->obmc_buff_0, sb_size * sb_size * bits * MAX_PLANES * sizeof(ctx->obmc_buff_0[0]));
387
0
        EB_MALLOC(ctx->obmc_buff_1, sb_size * sb_size * bits * MAX_PLANES * sizeof(ctx->obmc_buff_1[0]));
388
0
        EB_MALLOC(ctx->obmc_conv_buf, sb_size * sb_size * sizeof(ctx->obmc_conv_buf[0]));
389
0
        EB_MALLOC(ctx->wsrc_buf, sb_size * sb_size * sizeof(ctx->wsrc_buf[0]));
390
0
        EB_MALLOC(ctx->mask_buf, sb_size * sb_size * sizeof(ctx->mask_buf[0]));
391
0
    }
392
1.91k
    EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq, block_max_count_sb);
393
1.91k
    EB_MALLOC_ARRAY(ctx->pd0_mds0_best_cost, block_max_count_sb);
394
    // Fast Candidate Array
395
1.91k
    uint16_t max_can_count = svt_aom_get_max_can_count(enc_mode, rtc_tune) + ind_uv_cands;
396
1.91k
    EB_MALLOC_ARRAY(ctx->fast_cand_array, max_can_count);
397
398
365k
    for (cand_index = 0; cand_index < max_can_count; ++cand_index) {
399
363k
        ctx->fast_cand_array[cand_index].palette_info = NULL;
400
363k
    }
401
1.91k
    svt_aom_assert_err(max_can_count > ind_uv_cands, "Max. candidates is too low");
402
1.91k
    EB_MALLOC_2D(ctx->injected_mvs, (uint16_t)(max_can_count - ind_uv_cands), 2);
403
1.91k
    EB_MALLOC_ARRAY(ctx->injected_ref_types, (max_can_count - ind_uv_cands));
404
405
    // Set buffers for MD palette search to NULL; will be init'd at runtime if needed
406
1.91k
    ctx->palette_buffer       = NULL;
407
1.91k
    ctx->palette_cand_array   = NULL;
408
1.91k
    ctx->palette_size_array_0 = NULL;
409
410
    // Cost Arrays
411
1.91k
    EB_MALLOC_ARRAY(ctx->fast_cost_array, ctx->max_nics_uv);
412
1.91k
    EB_MALLOC_ARRAY(ctx->full_cost_array, ctx->max_nics_uv);
413
1.91k
    EB_MALLOC_ARRAY(ctx->full_cost_ssim_array, ctx->max_nics_uv);
414
    // Candidate Buffers
415
1.91k
    EB_NEW(ctx->cand_bf_tx_depth_1,
416
1.91k
           svt_aom_mode_decision_scratch_cand_bf_ctor,
417
1.91k
           sb_size,
418
1.91k
           SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT);
419
420
1.91k
    EB_ALLOC_PTR_ARRAY(ctx->cand_bf_tx_depth_1->cand, 1);
421
1.91k
    EB_NEW(ctx->cand_bf_tx_depth_2,
422
1.91k
           svt_aom_mode_decision_scratch_cand_bf_ctor,
423
1.91k
           sb_size,
424
1.91k
           SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT);
425
426
1.91k
    EB_ALLOC_PTR_ARRAY(ctx->cand_bf_tx_depth_2->cand, 1);
427
7.64k
    for (int i = 0; i < 3; i++) {
428
5.73k
        ctx->md_blk_arr_nsq[0].neigh_left_recon[i]       = NULL;
429
5.73k
        ctx->md_blk_arr_nsq[0].neigh_top_recon[i]        = NULL;
430
5.73k
        ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[i] = NULL;
431
5.73k
        ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[i]  = NULL;
432
5.73k
    }
433
1.91k
    uint32_t coded_leaf_index;
434
1.91k
    uint16_t sz = sizeof(uint16_t);
435
1.91k
    if (SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) > EB_8_BIT_MD) {
436
0
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[0], block_max_count_sb * sb_size * sz);
437
0
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[0], block_max_count_sb * sb_size * sz);
438
0
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[1], block_max_count_sb * sb_size * sz >> 1);
439
0
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[1], block_max_count_sb * sb_size * sz >> 1);
440
0
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[2], block_max_count_sb * sb_size * sz >> 1);
441
0
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[2], block_max_count_sb * sb_size * sz >> 1);
442
443
0
        for (coded_leaf_index = 0; coded_leaf_index < block_max_count_sb; ++coded_leaf_index) {
444
0
            size_t offset = coded_leaf_index * sb_size * sz;
445
0
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon_16bit[0] =
446
0
                ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[0] + offset;
447
0
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon_16bit[0] =
448
0
                ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[0] + offset;
449
0
            offset >>= 1;
450
0
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon_16bit[1] =
451
0
                ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[1] + offset;
452
0
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon_16bit[1] =
453
0
                ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[1] + offset;
454
0
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon_16bit[2] =
455
0
                ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[2] + offset;
456
0
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon_16bit[2] =
457
0
                ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[2] + offset;
458
0
        }
459
0
    }
460
1.91k
    if (SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) != EB_10_BIT_MD) {
461
1.91k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon[0], block_max_count_sb * sb_size);
462
1.91k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon[0], block_max_count_sb * sb_size);
463
1.91k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon[1], block_max_count_sb * sb_size >> 1);
464
1.91k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon[1], block_max_count_sb * sb_size >> 1);
465
1.91k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon[2], block_max_count_sb * sb_size >> 1);
466
1.91k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon[2], block_max_count_sb * sb_size >> 1);
467
468
164k
        for (coded_leaf_index = 0; coded_leaf_index < block_max_count_sb; ++coded_leaf_index) {
469
162k
            size_t offset                                             = coded_leaf_index * sb_size;
470
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon[0] = ctx->md_blk_arr_nsq[0].neigh_left_recon[0] +
471
162k
                offset;
472
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon[0] = ctx->md_blk_arr_nsq[0].neigh_top_recon[0] +
473
162k
                offset;
474
162k
            offset >>= 1;
475
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon[1] = ctx->md_blk_arr_nsq[0].neigh_left_recon[1] +
476
162k
                offset;
477
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon[1] = ctx->md_blk_arr_nsq[0].neigh_top_recon[1] +
478
162k
                offset;
479
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon[2] = ctx->md_blk_arr_nsq[0].neigh_left_recon[2] +
480
162k
                offset;
481
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon[2] = ctx->md_blk_arr_nsq[0].neigh_top_recon[2] +
482
162k
                offset;
483
162k
        }
484
1.91k
    }
485
1.91k
    ctx->md_blk_arr_nsq[0].av1xd = NULL;
486
1.91k
    EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].av1xd, block_max_count_sb);
487
488
    // Alloc mds and pc_tree, which are used to track tested blocks in MD
489
1.91k
    bool    disallow_4x4     = allintra ? svt_aom_get_disallow_4x4_allintra(enc_mode)
490
1.91k
        : rtc_tune                      ? svt_aom_get_disallow_4x4_rtc()
491
0
                                        : svt_aom_get_disallow_4x4_default(enc_mode);
492
1.91k
    bool    disallow_8x8     = allintra ? svt_aom_get_disallow_8x8_allintra()
493
1.91k
        : rtc_tune ? svt_aom_get_disallow_8x8_rtc(enc_mode, scs->max_input_luma_width, scs->max_input_luma_height)
494
0
                   : svt_aom_get_disallow_8x8_default();
495
1.91k
    uint8_t min_bsize        = disallow_8x8 ? 16 : disallow_4x4 ? 8 : 4;
496
1.91k
    int     blocks_per_depth = (sb_size / min_bsize) * (sb_size / min_bsize);
497
1.91k
    int     blocks_to_alloc  = 0;
498
499
9.55k
    for (int i = min_bsize; i <= sb_size; i <<= 1, blocks_per_depth >>= 2) {
500
7.64k
        blocks_to_alloc += blocks_per_depth;
501
7.64k
    }
502
1.91k
    EB_CALLOC_ARRAY(ctx->mds, blocks_to_alloc);
503
1.91k
    uint32_t mds_idx = 0;
504
1.91k
    setup_mds(scs, ctx->mds, &mds_idx, 0, scs->seq_header.sb_size, min_bsize);
505
1.91k
    EB_CALLOC_ARRAY(ctx->pc_tree, blocks_to_alloc);
506
1.91k
    EB_MALLOC_ARRAY(ctx->tested_blk, blocks_to_alloc);
507
1.91k
    setup_pc_tree(ctx->pc_tree, ctx->tested_blk, 0, scs->seq_header.sb_size, min_bsize);
508
1.91k
    ctx->blocks_to_alloc = blocks_to_alloc;
509
510
1.91k
    bool bypass_encdec = allintra ? svt_aom_get_bypass_encdec_allintra(enc_mode)
511
1.91k
        : rtc_tune                ? svt_aom_get_bypass_encdec_rtc(enc_mode, encoder_bit_depth)
512
0
                                  : svt_aom_get_bypass_encdec_default(enc_mode, encoder_bit_depth);
513
    // Per-block coeff_tmp/recon_tmp (bypass-encdec only) share one backing allocation each,
514
    // sliced per block, instead of one allocation per block.
515
1.91k
    EbPictureBufferDescInitData* coeff_tmp_id = NULL;
516
1.91k
    EbPictureBufferDescInitData* recon_tmp_id = NULL;
517
1.91k
    if (bypass_encdec) {
518
1.91k
        EB_MALLOC_ARRAY(coeff_tmp_id, block_max_count_sb);
519
1.91k
        EB_MALLOC_ARRAY(recon_tmp_id, block_max_count_sb);
520
1.91k
    }
521
164k
    for (coded_leaf_index = 0; coded_leaf_index < block_max_count_sb; ++coded_leaf_index) {
522
162k
        ctx->md_blk_arr_nsq[coded_leaf_index].av1xd      = ctx->md_blk_arr_nsq[0].av1xd + coded_leaf_index;
523
162k
        ctx->md_blk_arr_nsq[coded_leaf_index].segment_id = 0;
524
162k
        const BlockGeom* blk_geom                        = get_blk_geom_mds(scs->blk_geom_mds, coded_leaf_index);
525
162k
        if (bypass_encdec) {
526
162k
            EbPictureBufferDescInitData* ci = &coeff_tmp_id[coded_leaf_index];
527
162k
            ci->buffer_enable_mask          = PICTURE_BUFFER_DESC_FULL_MASK;
528
162k
            ci->max_width                   = blk_geom->bwidth;
529
162k
            ci->max_height                  = blk_geom->bheight;
530
162k
            ci->bit_depth                   = EB_THIRTYTWO_BIT;
531
162k
            ci->color_format                = (blk_geom->bwidth > 4 && blk_geom->bheight > 4)
532
162k
                ? EB_YUV420
533
162k
                : EB_YUV444; // PW - must have at least 4x4 for chroma coeffs
534
162k
            ci->border                      = 0;
535
162k
            ci->split_mode                  = false;
536
537
162k
            EbPictureBufferDescInitData* ri = &recon_tmp_id[coded_leaf_index];
538
162k
            ri->buffer_enable_mask          = PICTURE_BUFFER_DESC_FULL_MASK;
539
162k
            ri->max_width                   = blk_geom->bwidth;
540
162k
            ri->max_height                  = blk_geom->bheight;
541
162k
            ri->bit_depth                   = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT;
542
162k
            ri->color_format                = (blk_geom->bwidth > 4 && blk_geom->bheight > 4) ? EB_YUV420 : EB_YUV444;
543
162k
            ri->border                      = 0;
544
162k
            ri->split_mode                  = false;
545
162k
        } else {
546
0
            ctx->md_blk_arr_nsq[coded_leaf_index].coeff_tmp = NULL;
547
0
            ctx->md_blk_arr_nsq[coded_leaf_index].recon_tmp = NULL;
548
0
        }
549
162k
    }
550
1.91k
    if (bypass_encdec) {
551
1.91k
        EbErrorType c_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->coeff_tmp_pool, coeff_tmp_id, block_max_count_sb);
552
1.91k
        EbErrorType r_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->recon_tmp_pool, recon_tmp_id, block_max_count_sb);
553
1.91k
        EB_FREE_ARRAY(coeff_tmp_id);
554
1.91k
        EB_FREE_ARRAY(recon_tmp_id);
555
1.91k
        if (c_err != EB_ErrorNone) {
556
0
            return c_err;
557
0
        }
558
1.91k
        if (r_err != EB_ErrorNone) {
559
0
            return r_err;
560
0
        }
561
164k
        for (coded_leaf_index = 0; coded_leaf_index < block_max_count_sb; ++coded_leaf_index) {
562
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].coeff_tmp = &ctx->coeff_tmp_pool.descs[coded_leaf_index];
563
162k
            ctx->md_blk_arr_nsq[coded_leaf_index].recon_tmp = &ctx->recon_tmp_pool.descs[coded_leaf_index];
564
162k
        }
565
1.91k
    }
566
11.4k
    for (CandClass cand_class_it = CAND_CLASS_0; cand_class_it < CAND_CLASS_TOTAL; cand_class_it++) {
567
9.55k
        EB_MALLOC_ARRAY(ctx->cand_buff_indices[cand_class_it], ctx->max_nics_uv);
568
9.55k
    }
569
570
1.91k
    EB_MALLOC_ARRAY(ctx->best_candidate_index_array, ctx->max_nics_uv);
571
1.91k
    EB_MALLOC_ARRAY(ctx->above_txfm_context, (sb_size >> MI_SIZE_LOG2));
572
1.91k
    EB_MALLOC_ARRAY(ctx->left_txfm_context, (sb_size >> MI_SIZE_LOG2));
573
1.91k
    EbPictureBufferDescInitData thirty_two_width_picture_buffer_desc_init_data;
574
1.91k
    EbPictureBufferDescInitData picture_buffer_desc_init_data;
575
576
1.91k
    picture_buffer_desc_init_data.max_width          = sb_size;
577
1.91k
    picture_buffer_desc_init_data.max_height         = sb_size;
578
1.91k
    picture_buffer_desc_init_data.bit_depth          = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT;
579
1.91k
    picture_buffer_desc_init_data.color_format       = EB_YUV420;
580
1.91k
    picture_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
581
1.91k
    picture_buffer_desc_init_data.border             = 0;
582
1.91k
    picture_buffer_desc_init_data.split_mode         = false;
583
1.91k
    picture_buffer_desc_init_data.is_16bit_pipeline  = false;
584
585
1.91k
    thirty_two_width_picture_buffer_desc_init_data.max_width          = sb_size;
586
1.91k
    thirty_two_width_picture_buffer_desc_init_data.max_height         = sb_size;
587
1.91k
    thirty_two_width_picture_buffer_desc_init_data.bit_depth          = EB_THIRTYTWO_BIT;
588
1.91k
    thirty_two_width_picture_buffer_desc_init_data.color_format       = EB_YUV420;
589
1.91k
    thirty_two_width_picture_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
590
1.91k
    thirty_two_width_picture_buffer_desc_init_data.border             = 0;
591
1.91k
    thirty_two_width_picture_buffer_desc_init_data.split_mode         = false;
592
1.91k
    thirty_two_width_picture_buffer_desc_init_data.is_16bit_pipeline  = false;
593
    // Per-transform-type recon/coeff buffers share one backing allocation each.
594
1.91k
    EbErrorType tx_err;
595
1.91k
    if ((tx_err = svt_aom_pic_buf_desc_pool_ctor(
596
1.91k
             &ctx->tx_recon_coeff_pool, &thirty_two_width_picture_buffer_desc_init_data, TX_TYPES)) != EB_ErrorNone) {
597
0
        return tx_err;
598
0
    }
599
1.91k
    if ((tx_err = svt_aom_pic_buf_desc_pool_ctor(&ctx->tx_recon_pool, &picture_buffer_desc_init_data, TX_TYPES)) !=
600
1.91k
        EB_ErrorNone) {
601
0
        return tx_err;
602
0
    }
603
1.91k
    if ((tx_err = svt_aom_pic_buf_desc_pool_ctor(
604
1.91k
             &ctx->tx_quant_coeff_pool, &thirty_two_width_picture_buffer_desc_init_data, TX_TYPES)) != EB_ErrorNone) {
605
0
        return tx_err;
606
0
    }
607
32.4k
    for (uint32_t txt_itr = 0; txt_itr < TX_TYPES; ++txt_itr) {
608
30.5k
        ctx->recon_coeff_ptr[txt_itr] = &ctx->tx_recon_coeff_pool.descs[txt_itr];
609
30.5k
        ctx->recon_ptr[txt_itr]       = &ctx->tx_recon_pool.descs[txt_itr];
610
30.5k
        ctx->quant_coeff_ptr[txt_itr] = &ctx->tx_quant_coeff_pool.descs[txt_itr];
611
30.5k
    }
612
1.91k
    EB_NEW(ctx->tx_coeffs, svt_picture_buffer_desc_ctor, (EbPtr)&thirty_two_width_picture_buffer_desc_init_data);
613
1.91k
    EB_NEW(ctx->scratch_prediction_ptr, svt_picture_buffer_desc_ctor, (EbPtr)&picture_buffer_desc_init_data);
614
1.91k
    EbPictureBufferDescInitData double_width_picture_buffer_desc_init_data;
615
1.91k
    double_width_picture_buffer_desc_init_data.max_width          = sb_size;
616
1.91k
    double_width_picture_buffer_desc_init_data.max_height         = sb_size;
617
1.91k
    double_width_picture_buffer_desc_init_data.bit_depth          = EB_SIXTEEN_BIT;
618
1.91k
    double_width_picture_buffer_desc_init_data.color_format       = EB_YUV420;
619
1.91k
    double_width_picture_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
620
1.91k
    double_width_picture_buffer_desc_init_data.border             = 0;
621
1.91k
    double_width_picture_buffer_desc_init_data.split_mode         = false;
622
1.91k
    double_width_picture_buffer_desc_init_data.is_16bit_pipeline  = false;
623
624
    // The temp_recon_ptr and temp_residual will be shared by all candidates
625
    // If you want to do something with residual or recon, you need to create one
626
1.91k
    EB_NEW(ctx->temp_recon_ptr, svt_picture_buffer_desc_ctor, (EbPtr)&picture_buffer_desc_init_data);
627
1.91k
    EB_NEW(ctx->temp_residual, svt_picture_buffer_desc_ctor, (EbPtr)&double_width_picture_buffer_desc_init_data);
628
629
    // Candidate Buffers
630
1.91k
    EB_ALLOC_PTR_ARRAY(ctx->cand_bf_ptr_array, ctx->max_nics_uv);
631
    // The candidate-buffer structs themselves share one backing allocation (calloc to match
632
    // EB_NEW zero-init) instead of one allocation per candidate.
633
1.91k
    EB_CALLOC_ARRAY(ctx->cand_bf_pool, ctx->max_nics_uv);
634
635
    // pred/rec_coeff/quant for all candidates share one backing allocation each (instead of
636
    // 3 allocations per candidate). Slots [0, max_nics) use the full luma+chroma mask; slots
637
    // [max_nics, max_nics_uv) are chroma-only.
638
1.91k
    EbPictureBufferDescInitData* pred_id = NULL;
639
1.91k
    EbPictureBufferDescInitData* rc_id   = NULL;
640
1.91k
    EbPictureBufferDescInitData* q_id    = NULL;
641
1.91k
    EB_MALLOC_ARRAY(pred_id, ctx->max_nics_uv);
642
1.91k
    EB_MALLOC_ARRAY(rc_id, ctx->max_nics_uv);
643
1.91k
    EB_MALLOC_ARRAY(q_id, ctx->max_nics_uv);
644
21.0k
    for (buffer_index = 0; buffer_index < ctx->max_nics_uv; ++buffer_index) {
645
19.1k
        const uint32_t mask                      = (buffer_index < ctx->max_nics) ? PICTURE_BUFFER_DESC_FULL_MASK
646
19.1k
                                                                                  : PICTURE_BUFFER_DESC_CHROMA_MASK;
647
19.1k
        pred_id[buffer_index].max_width          = sb_size;
648
19.1k
        pred_id[buffer_index].max_height         = sb_size;
649
19.1k
        pred_id[buffer_index].bit_depth          = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT;
650
19.1k
        pred_id[buffer_index].color_format       = EB_YUV420;
651
19.1k
        pred_id[buffer_index].buffer_enable_mask = mask;
652
19.1k
        pred_id[buffer_index].border             = 0;
653
19.1k
        pred_id[buffer_index].split_mode         = false;
654
19.1k
        pred_id[buffer_index].is_16bit_pipeline  = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) > EB_8_BIT_MD;
655
        // rec_coeff / quant are identical but 32-bit.
656
19.1k
        rc_id[buffer_index]                   = pred_id[buffer_index];
657
19.1k
        rc_id[buffer_index].bit_depth         = EB_THIRTYTWO_BIT;
658
19.1k
        rc_id[buffer_index].is_16bit_pipeline = true;
659
19.1k
        q_id[buffer_index]                    = rc_id[buffer_index];
660
19.1k
    }
661
1.91k
    EbErrorType cand_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->cand_pred_pool, pred_id, ctx->max_nics_uv);
662
1.91k
    if (cand_err == EB_ErrorNone) {
663
1.91k
        cand_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->cand_rec_coeff_pool, rc_id, ctx->max_nics_uv);
664
1.91k
    }
665
1.91k
    if (cand_err == EB_ErrorNone) {
666
1.91k
        cand_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->cand_quant_pool, q_id, ctx->max_nics_uv);
667
1.91k
    }
668
1.91k
    EB_FREE_ARRAY(pred_id);
669
1.91k
    EB_FREE_ARRAY(rc_id);
670
1.91k
    EB_FREE_ARRAY(q_id);
671
1.91k
    if (cand_err != EB_ErrorNone) {
672
0
        return cand_err;
673
0
    }
674
675
21.0k
    for (buffer_index = 0; buffer_index < ctx->max_nics_uv; ++buffer_index) {
676
19.1k
        ctx->cand_bf_ptr_array[buffer_index] = &ctx->cand_bf_pool[buffer_index];
677
19.1k
        EbErrorType cbf_err = svt_aom_mode_decision_cand_bf_ctor(ctx->cand_bf_ptr_array[buffer_index],
678
19.1k
                                                                 &ctx->cand_pred_pool.descs[buffer_index],
679
19.1k
                                                                 &ctx->cand_rec_coeff_pool.descs[buffer_index],
680
19.1k
                                                                 &ctx->cand_quant_pool.descs[buffer_index],
681
19.1k
                                                                 ctx->temp_residual,
682
19.1k
                                                                 ctx->temp_recon_ptr,
683
19.1k
                                                                 &(ctx->fast_cost_array[buffer_index]),
684
19.1k
                                                                 &(ctx->full_cost_array[buffer_index]),
685
19.1k
                                                                 &(ctx->full_cost_ssim_array[buffer_index]));
686
19.1k
        if (cbf_err != EB_ErrorNone) {
687
0
            return cbf_err;
688
0
        }
689
19.1k
    }
690
691
1.91k
    return EB_ErrorNone;
692
1.91k
}
693
694
/**************************************************
695
 * Reset Mode Decision Neighbor Arrays
696
 *************************************************/
697
6.27k
void svt_aom_reset_mode_decision_neighbor_arrays(PictureControlSet* pcs, uint16_t tile_idx) {
698
6.27k
    uint8_t depth;
699
25.0k
    for (depth = 0; depth < NA_TOT_CNT; depth++) {
700
18.8k
        svt_aom_neighbor_array_unit_reset(pcs->mdleaf_partition_na[depth][tile_idx]);
701
18.8k
        if (SVT_EFFECTIVE_HBD_MD(pcs->hbd_md) != EB_10_BIT_MD) {
702
18.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_luma_recon_na[depth][tile_idx]);
703
18.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_1_luma_recon_na[depth][tile_idx]);
704
18.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_2_luma_recon_na[depth][tile_idx]);
705
18.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_cb_recon_na[depth][tile_idx]);
706
18.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_cr_recon_na[depth][tile_idx]);
707
18.8k
        }
708
18.8k
        if (SVT_EFFECTIVE_HBD_MD(pcs->hbd_md) > EB_8_BIT_MD ||
709
18.8k
            (SVT_EFFECTIVE_BIT_DEPTH(pcs->scs->encoder_bit_depth) > EB_EIGHT_BIT && pcs->pic_bypass_encdec)) {
710
0
            svt_aom_neighbor_array_unit_reset(pcs->md_luma_recon_na_16bit[depth][tile_idx]);
711
0
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_1_luma_recon_na_16bit[depth][tile_idx]);
712
0
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_2_luma_recon_na_16bit[depth][tile_idx]);
713
0
            svt_aom_neighbor_array_unit_reset(pcs->md_cb_recon_na_16bit[depth][tile_idx]);
714
0
            svt_aom_neighbor_array_unit_reset(pcs->md_cr_recon_na_16bit[depth][tile_idx]);
715
0
        }
716
717
18.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_y_dcs_na[depth][tile_idx]);
718
18.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_1_luma_dc_sign_level_coeff_na[depth][tile_idx]);
719
18.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_cb_dc_sign_level_coeff_na[depth][tile_idx]);
720
18.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_cr_dc_sign_level_coeff_na[depth][tile_idx]);
721
18.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_txfm_context_array[depth][tile_idx]);
722
18.8k
    }
723
724
6.27k
    return;
725
6.27k
}
726
727
// If the ref intra percentage is below the TH, applying modulation to the MD lambda
728
0
#define LAMBDA_MOD_INTRA_TH 50
729
0
#define LAMBDA_MOD_INTRA_SCALING_FACTOR 138
730
731
// Set the lambda for each sb.
732
// When lambda tuning is on (blk_lambda_tuning), lambda of each block is set separately (full_lambda_md/fast_lambda_md)
733
// later in svt_aom_set_tuned_blk_lambda
734
// Testing showed that updating SAD lambda based on frame info was not helpful; therefore, the SAD lambda generation is not changed.
735
8.12k
static void av1_lambda_assign_md(PictureControlSet* pcs, ModeDecisionContext* ctx) {
736
8.12k
    ctx->full_lambda_md[0] = svt_aom_compute_rd_mult(pcs, ctx->qp_index, ctx->me_q_index, EB_EIGHT_BIT);
737
8.12k
    ctx->fast_lambda_md[0] = svt_aom_compute_fast_lambda(pcs, ctx->qp_index, ctx->me_q_index, EB_EIGHT_BIT);
738
8.12k
    ctx->full_lambda_md[1] = svt_aom_compute_rd_mult(pcs, ctx->qp_index, ctx->me_q_index, EB_TEN_BIT);
739
8.12k
    ctx->fast_lambda_md[1] = svt_aom_compute_fast_lambda(pcs, ctx->qp_index, ctx->me_q_index, EB_TEN_BIT);
740
741
8.12k
    if (!pcs->scs->static_config.rtc && pcs->scs->stats_based_sb_lambda_modulation) {
742
8.12k
        if (pcs->temporal_layer_index > 0) {
743
0
            if (pcs->ref_intra_percentage < LAMBDA_MOD_INTRA_TH) {
744
0
                ctx->full_lambda_md[0] = (ctx->full_lambda_md[0] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
745
0
                ctx->fast_lambda_md[0] = (ctx->fast_lambda_md[0] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
746
0
                ctx->full_lambda_md[1] = (ctx->full_lambda_md[1] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
747
0
                ctx->fast_lambda_md[1] = (ctx->fast_lambda_md[1] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
748
0
            }
749
0
        }
750
8.12k
    }
751
752
8.12k
    if (pcs->lambda_weight) {
753
3.60k
        ctx->full_lambda_md[0] = (uint32_t)((ctx->full_lambda_md[0] * (uint64_t)pcs->lambda_weight) >> 7);
754
3.60k
        ctx->fast_lambda_md[0] = (uint32_t)((ctx->fast_lambda_md[0] * (uint64_t)pcs->lambda_weight) >> 7);
755
3.60k
        ctx->full_lambda_md[1] = (uint32_t)((ctx->full_lambda_md[1] * (uint64_t)pcs->lambda_weight) >> 7);
756
3.60k
        ctx->fast_lambda_md[1] = (uint32_t)((ctx->fast_lambda_md[1] * (uint64_t)pcs->lambda_weight) >> 7);
757
3.60k
    }
758
8.12k
    ctx->full_lambda_md[1] *= 16;
759
8.12k
    ctx->fast_lambda_md[1] *= 4;
760
761
8.12k
    SequenceControlSet* scs          = pcs->scs;
762
8.12k
    uint64_t            scale_factor = scs->static_config.lambda_scale_factors[pcs->ppcs->update_type];
763
8.12k
    ctx->full_lambda_md[0]           = (uint32_t)((ctx->full_lambda_md[0] * scale_factor) >> 7);
764
8.12k
    ctx->full_lambda_md[1]           = (uint32_t)((ctx->full_lambda_md[1] * scale_factor) >> 7);
765
8.12k
    ctx->fast_lambda_md[0]           = (uint32_t)((ctx->fast_lambda_md[0] * scale_factor) >> 7);
766
8.12k
    ctx->fast_lambda_md[1]           = (uint32_t)((ctx->fast_lambda_md[1] * scale_factor) >> 7);
767
768
8.12k
    ctx->full_sb_lambda_md[0] = ctx->full_lambda_md[0];
769
8.12k
    ctx->full_sb_lambda_md[1] = ctx->full_lambda_md[1];
770
8.12k
}
771
772
void svt_aom_reset_mode_decision(SequenceControlSet* scs, ModeDecisionContext* ctx, PictureControlSet* pcs,
773
6.69k
                                 uint16_t tile_group_idx, uint32_t segment_index) {
774
6.69k
    ctx->hbd_md = SVT_EFFECTIVE_HBD_MD(pcs->hbd_md);
775
    // Reset MD rate Estimation table to initial values by copying from md_rate_est_ctx
776
6.69k
    ctx->md_rate_est_ctx = pcs->md_rate_est_ctx;
777
    // Segments are dispatched to EncDec threads dynamically; scoping the last DV to the segment keeps
778
    // the BVP fallback independent of the SB->thread mapping (deterministic at any --lp).
779
6.69k
    ctx->intrabc_last_dv.as_int = 0;
780
    // Reset CABAC Contexts
781
782
    // Reset Neighbor Arrays at start of new Segment / Picture
783
6.69k
    if (segment_index == 0) {
784
620
        for (uint16_t r = pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_start_y;
785
2.54k
             r < pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_end_y;
786
1.92k
             r++) {
787
1.92k
            for (uint16_t c = pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_start_x;
788
8.19k
                 c < pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_end_x;
789
6.27k
                 c++) {
790
6.27k
                uint16_t tile_idx = c + r * pcs->ppcs->av1_cm->tiles_info.tile_cols;
791
6.27k
                svt_aom_reset_mode_decision_neighbor_arrays(pcs, tile_idx);
792
6.27k
            }
793
1.92k
        }
794
620
        (void)scs;
795
620
    }
796
    //each segment enherits the bypass encdec from the picture level
797
6.69k
    ctx->bypass_encdec = pcs->pic_bypass_encdec;
798
6.69k
    return;
799
6.69k
}
800
801
/******************************************************
802
 * Mode Decision Configure SB
803
 ******************************************************/
804
void svt_aom_mode_decision_configure_sb(ModeDecisionContext* ctx, PictureControlSet* pcs, uint8_t sb_qp,
805
8.12k
                                        uint8_t me_sb_qp) {
806
    /* Note(CHKN) : when Qp modulation varies QP on a sub-SB(CU) basis,  Lamda has to change based on Cu->QP , and then this code has to move inside the CU loop in MD */
807
808
    // Lambda Assignement
809
8.12k
    ctx->qp_index = pcs->ppcs->frm_hdr.delta_q_params.delta_q_present || pcs->ppcs->r0_delta_qp_md
810
8.12k
        ? sb_qp
811
8.12k
        : (uint8_t)pcs->ppcs->frm_hdr.quantization_params.base_q_idx;
812
813
8.12k
    ctx->me_q_index = me_sb_qp;
814
815
8.12k
    av1_lambda_assign_md(pcs, ctx);
816
817
8.12k
    ctx->hbd_pack_done = 0;
818
819
8.12k
    return;
820
8.12k
}