Coverage Report

Created: 2026-07-16 06:32

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