Coverage Report

Created: 2026-07-30 06:27

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.56k
static void mode_decision_context_dctor(EbPtr p) {
36
1.56k
    ModeDecisionContext* obj = (ModeDecisionContext*)p;
37
38
    // MD palette search
39
1.56k
    if (obj->palette_buffer) {
40
0
        EB_FREE(obj->palette_buffer);
41
0
    }
42
1.56k
    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.56k
    if (obj->palette_size_array_0) {
53
0
        EB_FREE_ARRAY(obj->palette_size_array_0);
54
0
    }
55
9.36k
    for (CandClass cand_class_it = CAND_CLASS_0; cand_class_it < CAND_CLASS_TOTAL; cand_class_it++) {
56
7.80k
        EB_FREE_ARRAY(obj->cand_buff_indices[cand_class_it]);
57
7.80k
    }
58
1.56k
    EB_FREE_ARRAY(obj->best_candidate_index_array);
59
60
1.56k
    EB_FREE_ARRAY(obj->above_txfm_context);
61
1.56k
    EB_FREE_ARRAY(obj->left_txfm_context);
62
    // Per-block coeff_tmp/recon_tmp are borrowed from these pools (freed once each).
63
1.56k
    svt_aom_pic_buf_desc_pool_dctor(&obj->coeff_tmp_pool);
64
1.56k
    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.56k
    EB_FREE_ARRAY(obj->cand_bf_pool);
68
1.56k
    EB_FREE_ARRAY(obj->cand_bf_ptr_array);
69
    // Candidate pred/rec_coeff/quant are borrowed from these pools (freed once each).
70
1.56k
    svt_aom_pic_buf_desc_pool_dctor(&obj->cand_pred_pool);
71
1.56k
    svt_aom_pic_buf_desc_pool_dctor(&obj->cand_rec_coeff_pool);
72
1.56k
    svt_aom_pic_buf_desc_pool_dctor(&obj->cand_quant_pool);
73
1.56k
    EB_FREE_ARRAY(obj->cand_bf_tx_depth_1->cand);
74
1.56k
    EB_DELETE(obj->cand_bf_tx_depth_1);
75
1.56k
    EB_FREE_ARRAY(obj->cand_bf_tx_depth_2->cand);
76
1.56k
    EB_DELETE(obj->cand_bf_tx_depth_2);
77
1.56k
    EB_FREE_ALIGNED_ARRAY(obj->cfl_temp_luma_recon16bit);
78
1.56k
    EB_FREE_ALIGNED_ARRAY(obj->cfl_temp_luma_recon);
79
1.56k
    EB_FREE_ALIGNED_ARRAY(obj->pred_buf_q3);
80
1.56k
    EB_FREE_ALIGNED_ARRAY(obj->tmp_conv_buf);
81
1.56k
    EB_FREE_ALIGNED_ARRAY(obj->seg_mask_buf);
82
1.56k
    EB_FREE_ARRAY(obj->fast_cand_array);
83
1.56k
    EB_FREE_2D(obj->injected_mvs);
84
1.56k
    EB_FREE_ARRAY(obj->injected_ref_types);
85
1.56k
    EB_FREE_ARRAY(obj->fast_cost_array);
86
1.56k
    EB_FREE_ARRAY(obj->full_cost_array);
87
1.56k
    if (obj->md_blk_arr_nsq) {
88
6.24k
        for (int i = 0; i < 3; i++) {
89
4.68k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_left_recon_16bit[i]);
90
4.68k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_top_recon_16bit[i]);
91
4.68k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_left_recon[i]);
92
4.68k
            EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].neigh_top_recon[i]);
93
4.68k
        }
94
1.56k
    }
95
1.56k
    if (obj->md_blk_arr_nsq) {
96
1.56k
        EB_FREE_ARRAY(obj->md_blk_arr_nsq[0].av1xd);
97
1.56k
    }
98
1.56k
    EB_FREE_ARRAY(obj->mds);
99
1.56k
    EB_FREE_ARRAY(obj->pc_tree);
100
1.56k
    EB_FREE_ARRAY(obj->tested_blk);
101
1.56k
    EB_FREE_ARRAY(obj->uv_cand_buff_indices);
102
1.56k
    obj->blocks_to_alloc = 0;
103
1.56k
    EB_FREE_ARRAY(obj->md_blk_arr_nsq);
104
1.56k
    EB_FREE_ARRAY(obj->pd0_mds0_best_cost);
105
1.56k
    if (obj->rate_est_table) {
106
0
        EB_FREE_ARRAY(obj->rate_est_table);
107
0
    }
108
109
7.80k
    for (int i = 0; i < NEAREST_NEAR_MV_CNT; i++) {
110
6.24k
        if (obj->cmp_store.pred0_buf[i]) {
111
0
            EB_FREE(obj->cmp_store.pred0_buf[i]);
112
0
        }
113
6.24k
        if (obj->cmp_store.pred1_buf[i]) {
114
0
            EB_FREE(obj->cmp_store.pred1_buf[i]);
115
0
        }
116
6.24k
    }
117
1.56k
    if (obj->residual1) {
118
0
        EB_FREE(obj->residual1);
119
0
    }
120
1.56k
    if (obj->diff10) {
121
0
        EB_FREE(obj->diff10);
122
0
    }
123
124
1.56k
    if (obj->intrapred_buf) {
125
0
        EB_FREE_2D(obj->intrapred_buf);
126
0
    }
127
128
1.56k
    if (obj->obmc_buff_0) {
129
0
        EB_FREE(obj->obmc_buff_0);
130
0
    }
131
1.56k
    if (obj->obmc_buff_1) {
132
0
        EB_FREE(obj->obmc_buff_1);
133
0
    }
134
1.56k
    if (obj->obmc_conv_buf) {
135
0
        EB_FREE(obj->obmc_conv_buf);
136
0
    }
137
1.56k
    if (obj->wsrc_buf) {
138
0
        EB_FREE(obj->wsrc_buf);
139
0
    }
140
1.56k
    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.56k
    svt_aom_pic_buf_desc_pool_dctor(&obj->tx_recon_coeff_pool);
145
1.56k
    svt_aom_pic_buf_desc_pool_dctor(&obj->tx_recon_pool);
146
1.56k
    svt_aom_pic_buf_desc_pool_dctor(&obj->tx_quant_coeff_pool);
147
1.56k
    EB_DELETE(obj->tx_coeffs);
148
1.56k
    EB_DELETE(obj->scratch_prediction_ptr);
149
1.56k
    EB_DELETE(obj->temp_residual);
150
1.56k
    EB_DELETE(obj->temp_recon_ptr);
151
1.56k
    EB_FREE_ARRAY(obj->full_cost_ssim_array);
152
1.56k
}
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
132k
                      const int min_sq_size) {
160
132k
    mds->mds_idx = *mds_idx;
161
132k
    mds->bsize   = bsize;
162
132k
    mds->index   = index;
163
164
    // If applicable, add split depths
165
132k
    const BlockGeom* blk_geom = get_blk_geom_mds(scs->blk_geom_mds, *mds_idx);
166
132k
    const int        sq_size  = block_size_wide[bsize];
167
132k
    if (sq_size > min_sq_size) {
168
32.7k
        const BlockSize subsize             = get_partition_subsize(bsize, PARTITION_SPLIT);
169
32.7k
        const int       sq_subsize          = block_size_wide[subsize];
170
32.7k
        int             blocks_per_subdepth = (sq_subsize / min_sq_size) * (sq_subsize / min_sq_size);
171
32.7k
        int             blocks_to_skip      = 0;
172
173
74.8k
        for (int i = min_sq_size; i <= sq_subsize; i <<= 1, blocks_per_subdepth >>= 2) {
174
42.1k
            blocks_to_skip += blocks_per_subdepth;
175
42.1k
        }
176
177
32.7k
        *mds_idx += blk_geom->d1_depth_offset;
178
163k
        for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
179
131k
            mds->split[i] = mds + i * blocks_to_skip + 1;
180
131k
            setup_mds(scs, mds->split[i], mds_idx, i, subsize, min_sq_size);
181
131k
        }
182
99.8k
    } else {
183
99.8k
        *mds_idx += blk_geom->ns_depth_offset;
184
99.8k
    }
185
132k
}
186
187
static void setup_pc_tree(PC_TREE* pc_tree, bool (*test_blk_array)[PART_S][4], int index, BlockSize bsize,
188
132k
                          const int min_sq_size) {
189
132k
    pc_tree->bsize      = bsize;
190
132k
    pc_tree->index      = index;
191
132k
    pc_tree->tested_blk = test_blk_array[0];
192
193
    // If applicable, add split depths
194
132k
    const int sq_size = block_size_wide[bsize];
195
132k
    if (sq_size > min_sq_size) {
196
32.7k
        const BlockSize subsize             = get_partition_subsize(bsize, PARTITION_SPLIT);
197
32.7k
        const int       sq_subsize          = block_size_wide[subsize];
198
32.7k
        int             blocks_per_subdepth = (sq_subsize / min_sq_size) * (sq_subsize / min_sq_size);
199
32.7k
        int             blocks_to_skip      = 0;
200
201
74.8k
        for (int i = min_sq_size; i <= sq_subsize; i <<= 1, blocks_per_subdepth >>= 2) {
202
42.1k
            blocks_to_skip += blocks_per_subdepth;
203
42.1k
        }
204
205
163k
        for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
206
131k
            pc_tree->split[i]         = pc_tree + i * blocks_to_skip + 1;
207
131k
            pc_tree->split[i]->parent = pc_tree;
208
131k
            setup_pc_tree(pc_tree->split[i], test_blk_array + i * blocks_to_skip + 1, i, subsize, min_sq_size);
209
131k
        }
210
32.7k
    }
211
132k
}
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.56k
                                               uint8_t seq_qp_mod) {
222
1.56k
    const bool allintra = scs->allintra;
223
1.56k
    const bool rtc_tune = scs->static_config.rtc;
224
1.56k
    uint32_t   buffer_index;
225
1.56k
    uint32_t   cand_index;
226
227
1.56k
    ctx->init_max_block_cnt     = max_block_cnt;
228
1.56k
    uint32_t block_max_count_sb = max_block_cnt;
229
230
1.56k
    ctx->sb_size = sb_size;
231
1.56k
    (void)color_format;
232
233
1.56k
    ctx->dctor  = mode_decision_context_dctor;
234
1.56k
    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.56k
    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.56k
    ctx->mode_decision_configuration_input_fifo_ptr = mode_decision_configuration_input_fifo_ptr;
242
1.56k
    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.56k
    uint8_t min_nic_scaling_level = NICS_SCALING_LEVELS - 1;
248
1.56k
    uint8_t stage1_scaling_num;
249
1.56k
    if (allintra) {
250
1.56k
        uint8_t nic_level  = svt_aom_get_nic_level_allintra(enc_mode);
251
1.56k
        stage1_scaling_num = MD_STAGE_NICS_SCAL_NUM[svt_aom_set_nic_controls(NULL, nic_level)][MD_STAGE_1];
252
1.56k
    } 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.56k
    uint32_t max_nics = 0;
266
1.56k
    {
267
1.56k
        NicScalingCtrls scaling_ctrls;
268
1.56k
        scaling_ctrls.stage1_scaling_num = stage1_scaling_num;
269
1.56k
        scaling_ctrls.stage2_scaling_num = stage1_scaling_num;
270
1.56k
        scaling_ctrls.stage3_scaling_num = stage1_scaling_num;
271
1.56k
        uint32_t mds1_count[CAND_CLASS_TOTAL];
272
1.56k
        uint32_t mds2_count[CAND_CLASS_TOTAL];
273
1.56k
        uint32_t mds3_count[CAND_CLASS_TOTAL];
274
6.24k
        for (uint8_t pic_type = 0; pic_type < NICS_PIC_TYPE; pic_type++) {
275
304k
            for (uint8_t qp = MIN_QP_VALUE; qp <= MAX_QP_VALUE; qp++) {
276
299k
                svt_aom_set_nics(scs, &scaling_ctrls, mds1_count, mds2_count, mds3_count, pic_type, qp);
277
278
299k
                uint32_t nics = 0;
279
1.79M
                for (CandClass cidx = CAND_CLASS_0; cidx < CAND_CLASS_TOTAL; cidx++) {
280
1.49M
                    nics += mds1_count[cidx];
281
1.49M
                }
282
299k
                max_nics = MAX(max_nics, nics);
283
299k
            }
284
4.68k
        }
285
1.56k
    }
286
287
    // If independent chroma search is used, need to allocate additional 84 candidate buffers
288
1.56k
    bool is_chroma_mode_0;
289
1.56k
    if (allintra) {
290
1.56k
        is_chroma_mode_0 = svt_aom_set_chroma_controls(NULL, svt_aom_get_chroma_level_allintra(enc_mode)) ==
291
1.56k
            CHROMA_MODE_0;
292
1.56k
    } 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.56k
    const uint8_t ind_uv_cands = is_chroma_mode_0 ? 84 : 0;
304
1.56k
    max_nics += CAND_CLASS_TOTAL; //need one extra temp buffer for each fast loop call
305
1.56k
    ctx->max_nics    = max_nics;
306
1.56k
    ctx->max_nics_uv = max_nics + ind_uv_cands;
307
1.56k
    EB_MALLOC_ARRAY(ctx->uv_cand_buff_indices, ctx->max_nics_uv);
308
    // Cfl scratch memory
309
1.56k
    if (SVT_EFFECTIVE_HBD_MD(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.56k
    if (SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) != EB_10_BIT_MD) {
313
1.56k
        EB_MALLOC_ALIGNED(ctx->cfl_temp_luma_recon, sizeof(uint8_t) * sb_size * sb_size);
314
1.56k
    }
315
1.56k
    EB_MALLOC_ALIGNED(ctx->pred_buf_q3, CFL_BUF_SQUARE);
316
    // Hoisted inter-prediction scratch (previously large on-stack arrays).
317
1.56k
    EB_MALLOC_ALIGNED_ARRAY(ctx->tmp_conv_buf, MAX_SB_SQUARE);
318
1.56k
    EB_MALLOC_ALIGNED_ARRAY(ctx->seg_mask_buf, 2 * MAX_SB_SQUARE);
319
1.56k
    uint8_t use_update_cdf = 0;
320
1.56k
    if (allintra) {
321
1.56k
        use_update_cdf = svt_aom_get_update_cdf_level_allintra(enc_mode);
322
1.56k
    } 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.56k
    if (use_update_cdf) {
340
0
        EB_CALLOC_ARRAY(ctx->rate_est_table, 1);
341
1.56k
    } else {
342
1.56k
        ctx->rate_est_table = NULL;
343
1.56k
    }
344
    // Allocate buffer for inter-inter compound prediction
345
1.56k
    if (get_inter_compound_level(enc_mode)) {
346
0
        const uint8_t bits = SVT_EFFECTIVE_HBD_MD(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.56k
    uint8_t ii_allowed = 0;
357
4.68k
    for (uint8_t transition_present = 0; transition_present < 2; transition_present++) {
358
3.12k
        if (ii_allowed) {
359
0
            break;
360
0
        }
361
3.12k
        ii_allowed |= svt_aom_get_inter_intra_level(enc_mode, transition_present);
362
3.12k
    }
363
1.56k
    if (ii_allowed) {
364
0
        const uint8_t bits = SVT_EFFECTIVE_HBD_MD(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.56k
    uint8_t obmc_allowed = 0;
371
4.68k
    for (uint8_t is_base = 0; is_base < 2; is_base++) {
372
202k
        for (uint8_t qp = MIN_QP_VALUE; qp <= MAX_QP_VALUE; qp++) {
373
199k
            if (obmc_allowed) {
374
0
                break;
375
0
            }
376
199k
            obmc_allowed |= svt_aom_get_obmc_level(enc_mode, qp, seq_qp_mod);
377
199k
        }
378
3.12k
    }
379
1.56k
    if (obmc_allowed) {
380
0
        const uint8_t bits = SVT_EFFECTIVE_HBD_MD(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.56k
    EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq, block_max_count_sb);
388
1.56k
    EB_MALLOC_ARRAY(ctx->pd0_mds0_best_cost, block_max_count_sb);
389
    // Fast Candidate Array
390
1.56k
    uint16_t max_can_count = svt_aom_get_max_can_count(enc_mode, rtc_tune) + ind_uv_cands;
391
1.56k
    EB_MALLOC_ARRAY(ctx->fast_cand_array, max_can_count);
392
393
297k
    for (cand_index = 0; cand_index < max_can_count; ++cand_index) {
394
296k
        ctx->fast_cand_array[cand_index].palette_info = NULL;
395
296k
    }
396
1.56k
    svt_aom_assert_err(max_can_count > ind_uv_cands, "Max. candidates is too low");
397
1.56k
    EB_MALLOC_2D(ctx->injected_mvs, (uint16_t)(max_can_count - ind_uv_cands), 2);
398
1.56k
    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.56k
    ctx->palette_buffer       = NULL;
402
1.56k
    ctx->palette_cand_array   = NULL;
403
1.56k
    ctx->palette_size_array_0 = NULL;
404
405
    // Cost Arrays
406
1.56k
    EB_MALLOC_ARRAY(ctx->fast_cost_array, ctx->max_nics_uv);
407
1.56k
    EB_MALLOC_ARRAY(ctx->full_cost_array, ctx->max_nics_uv);
408
1.56k
    EB_MALLOC_ARRAY(ctx->full_cost_ssim_array, ctx->max_nics_uv);
409
    // Candidate Buffers
410
1.56k
    EB_NEW(ctx->cand_bf_tx_depth_1,
411
1.56k
           svt_aom_mode_decision_scratch_cand_bf_ctor,
412
1.56k
           sb_size,
413
1.56k
           SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT);
414
415
1.56k
    EB_ALLOC_PTR_ARRAY(ctx->cand_bf_tx_depth_1->cand, 1);
416
1.56k
    EB_NEW(ctx->cand_bf_tx_depth_2,
417
1.56k
           svt_aom_mode_decision_scratch_cand_bf_ctor,
418
1.56k
           sb_size,
419
1.56k
           SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT);
420
421
1.56k
    EB_ALLOC_PTR_ARRAY(ctx->cand_bf_tx_depth_2->cand, 1);
422
6.24k
    for (int i = 0; i < 3; i++) {
423
4.68k
        ctx->md_blk_arr_nsq[0].neigh_left_recon[i]       = NULL;
424
4.68k
        ctx->md_blk_arr_nsq[0].neigh_top_recon[i]        = NULL;
425
4.68k
        ctx->md_blk_arr_nsq[0].neigh_left_recon_16bit[i] = NULL;
426
4.68k
        ctx->md_blk_arr_nsq[0].neigh_top_recon_16bit[i]  = NULL;
427
4.68k
    }
428
1.56k
    uint32_t coded_leaf_index;
429
1.56k
    uint16_t sz = sizeof(uint16_t);
430
1.56k
    if (SVT_EFFECTIVE_HBD_MD(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.56k
    if (SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) != EB_10_BIT_MD) {
456
1.56k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon[0], block_max_count_sb * sb_size);
457
1.56k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon[0], block_max_count_sb * sb_size);
458
1.56k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon[1], block_max_count_sb * sb_size >> 1);
459
1.56k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon[1], block_max_count_sb * sb_size >> 1);
460
1.56k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_left_recon[2], block_max_count_sb * sb_size >> 1);
461
1.56k
        EB_MALLOC_ARRAY(ctx->md_blk_arr_nsq[0].neigh_top_recon[2], block_max_count_sb * sb_size >> 1);
462
463
134k
        for (coded_leaf_index = 0; coded_leaf_index < block_max_count_sb; ++coded_leaf_index) {
464
132k
            size_t offset                                             = coded_leaf_index * sb_size;
465
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon[0] = ctx->md_blk_arr_nsq[0].neigh_left_recon[0] +
466
132k
                offset;
467
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon[0] = ctx->md_blk_arr_nsq[0].neigh_top_recon[0] +
468
132k
                offset;
469
132k
            offset >>= 1;
470
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon[1] = ctx->md_blk_arr_nsq[0].neigh_left_recon[1] +
471
132k
                offset;
472
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon[1] = ctx->md_blk_arr_nsq[0].neigh_top_recon[1] +
473
132k
                offset;
474
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_left_recon[2] = ctx->md_blk_arr_nsq[0].neigh_left_recon[2] +
475
132k
                offset;
476
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].neigh_top_recon[2] = ctx->md_blk_arr_nsq[0].neigh_top_recon[2] +
477
132k
                offset;
478
132k
        }
479
1.56k
    }
480
1.56k
    ctx->md_blk_arr_nsq[0].av1xd = NULL;
481
1.56k
    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.56k
    bool    disallow_4x4     = allintra ? svt_aom_get_disallow_4x4_allintra(enc_mode)
485
1.56k
               : rtc_tune               ? svt_aom_get_disallow_4x4_rtc()
486
0
                                        : svt_aom_get_disallow_4x4_default(enc_mode);
487
1.56k
    bool    disallow_8x8     = allintra ? svt_aom_get_disallow_8x8_allintra()
488
1.56k
               : 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.56k
    uint8_t min_bsize        = disallow_8x8 ? 16 : disallow_4x4 ? 8 : 4;
491
1.56k
    int     blocks_per_depth = (sb_size / min_bsize) * (sb_size / min_bsize);
492
1.56k
    int     blocks_to_alloc  = 0;
493
494
7.80k
    for (int i = min_bsize; i <= sb_size; i <<= 1, blocks_per_depth >>= 2) {
495
6.24k
        blocks_to_alloc += blocks_per_depth;
496
6.24k
    }
497
1.56k
    EB_CALLOC_ARRAY(ctx->mds, blocks_to_alloc);
498
1.56k
    uint32_t mds_idx = 0;
499
1.56k
    setup_mds(scs, ctx->mds, &mds_idx, 0, scs->seq_header.sb_size, min_bsize);
500
1.56k
    EB_CALLOC_ARRAY(ctx->pc_tree, blocks_to_alloc);
501
1.56k
    EB_MALLOC_ARRAY(ctx->tested_blk, blocks_to_alloc);
502
1.56k
    setup_pc_tree(ctx->pc_tree, ctx->tested_blk, 0, scs->seq_header.sb_size, min_bsize);
503
1.56k
    ctx->blocks_to_alloc = blocks_to_alloc;
504
505
1.56k
    bool bypass_encdec = allintra ? svt_aom_get_bypass_encdec_allintra(enc_mode)
506
1.56k
        : 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.56k
    EbPictureBufferDescInitData* coeff_tmp_id = NULL;
511
1.56k
    EbPictureBufferDescInitData* recon_tmp_id = NULL;
512
1.56k
    if (bypass_encdec) {
513
1.56k
        EB_MALLOC_ARRAY(coeff_tmp_id, block_max_count_sb);
514
1.56k
        EB_MALLOC_ARRAY(recon_tmp_id, block_max_count_sb);
515
1.56k
    }
516
134k
    for (coded_leaf_index = 0; coded_leaf_index < block_max_count_sb; ++coded_leaf_index) {
517
132k
        ctx->md_blk_arr_nsq[coded_leaf_index].av1xd      = ctx->md_blk_arr_nsq[0].av1xd + coded_leaf_index;
518
132k
        ctx->md_blk_arr_nsq[coded_leaf_index].segment_id = 0;
519
132k
        const BlockGeom* blk_geom                        = get_blk_geom_mds(scs->blk_geom_mds, coded_leaf_index);
520
132k
        if (bypass_encdec) {
521
132k
            EbPictureBufferDescInitData* ci = &coeff_tmp_id[coded_leaf_index];
522
132k
            ci->buffer_enable_mask          = PICTURE_BUFFER_DESC_FULL_MASK;
523
132k
            ci->max_width                   = blk_geom->bwidth;
524
132k
            ci->max_height                  = blk_geom->bheight;
525
132k
            ci->bit_depth                   = EB_THIRTYTWO_BIT;
526
132k
            ci->color_format                = (blk_geom->bwidth > 4 && blk_geom->bheight > 4)
527
132k
                               ? EB_YUV420
528
132k
                               : EB_YUV444; // PW - must have at least 4x4 for chroma coeffs
529
132k
            ci->border                      = 0;
530
132k
            ci->split_mode                  = false;
531
532
132k
            EbPictureBufferDescInitData* ri = &recon_tmp_id[coded_leaf_index];
533
132k
            ri->buffer_enable_mask          = PICTURE_BUFFER_DESC_FULL_MASK;
534
132k
            ri->max_width                   = blk_geom->bwidth;
535
132k
            ri->max_height                  = blk_geom->bheight;
536
132k
            ri->bit_depth                   = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT;
537
132k
            ri->color_format                = (blk_geom->bwidth > 4 && blk_geom->bheight > 4) ? EB_YUV420 : EB_YUV444;
538
132k
            ri->border                      = 0;
539
132k
            ri->split_mode                  = false;
540
132k
        } 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
132k
    }
545
1.56k
    if (bypass_encdec) {
546
1.56k
        EbErrorType c_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->coeff_tmp_pool, coeff_tmp_id, block_max_count_sb);
547
1.56k
        EbErrorType r_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->recon_tmp_pool, recon_tmp_id, block_max_count_sb);
548
1.56k
        EB_FREE_ARRAY(coeff_tmp_id);
549
1.56k
        EB_FREE_ARRAY(recon_tmp_id);
550
1.56k
        if (c_err != EB_ErrorNone) {
551
0
            return c_err;
552
0
        }
553
1.56k
        if (r_err != EB_ErrorNone) {
554
0
            return r_err;
555
0
        }
556
134k
        for (coded_leaf_index = 0; coded_leaf_index < block_max_count_sb; ++coded_leaf_index) {
557
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].coeff_tmp = &ctx->coeff_tmp_pool.descs[coded_leaf_index];
558
132k
            ctx->md_blk_arr_nsq[coded_leaf_index].recon_tmp = &ctx->recon_tmp_pool.descs[coded_leaf_index];
559
132k
        }
560
1.56k
    }
561
9.36k
    for (CandClass cand_class_it = CAND_CLASS_0; cand_class_it < CAND_CLASS_TOTAL; cand_class_it++) {
562
7.80k
        EB_MALLOC_ARRAY(ctx->cand_buff_indices[cand_class_it], ctx->max_nics_uv);
563
7.80k
    }
564
565
1.56k
    EB_MALLOC_ARRAY(ctx->best_candidate_index_array, ctx->max_nics_uv);
566
1.56k
    EB_MALLOC_ARRAY(ctx->above_txfm_context, (sb_size >> MI_SIZE_LOG2));
567
1.56k
    EB_MALLOC_ARRAY(ctx->left_txfm_context, (sb_size >> MI_SIZE_LOG2));
568
1.56k
    EbPictureBufferDescInitData thirty_two_width_picture_buffer_desc_init_data;
569
1.56k
    EbPictureBufferDescInitData picture_buffer_desc_init_data;
570
571
1.56k
    picture_buffer_desc_init_data.max_width          = sb_size;
572
1.56k
    picture_buffer_desc_init_data.max_height         = sb_size;
573
1.56k
    picture_buffer_desc_init_data.bit_depth          = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT;
574
1.56k
    picture_buffer_desc_init_data.color_format       = EB_YUV420;
575
1.56k
    picture_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
576
1.56k
    picture_buffer_desc_init_data.border             = 0;
577
1.56k
    picture_buffer_desc_init_data.split_mode         = false;
578
1.56k
    picture_buffer_desc_init_data.is_16bit_pipeline  = false;
579
580
1.56k
    thirty_two_width_picture_buffer_desc_init_data.max_width          = sb_size;
581
1.56k
    thirty_two_width_picture_buffer_desc_init_data.max_height         = sb_size;
582
1.56k
    thirty_two_width_picture_buffer_desc_init_data.bit_depth          = EB_THIRTYTWO_BIT;
583
1.56k
    thirty_two_width_picture_buffer_desc_init_data.color_format       = EB_YUV420;
584
1.56k
    thirty_two_width_picture_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
585
1.56k
    thirty_two_width_picture_buffer_desc_init_data.border             = 0;
586
1.56k
    thirty_two_width_picture_buffer_desc_init_data.split_mode         = false;
587
1.56k
    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.56k
    EbErrorType tx_err;
590
1.56k
    if ((tx_err = svt_aom_pic_buf_desc_pool_ctor(
591
1.56k
             &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.56k
    if ((tx_err = svt_aom_pic_buf_desc_pool_ctor(&ctx->tx_recon_pool, &picture_buffer_desc_init_data, TX_TYPES)) !=
595
1.56k
        EB_ErrorNone) {
596
0
        return tx_err;
597
0
    }
598
1.56k
    if ((tx_err = svt_aom_pic_buf_desc_pool_ctor(
599
1.56k
             &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
26.5k
    for (uint32_t txt_itr = 0; txt_itr < TX_TYPES; ++txt_itr) {
603
24.9k
        ctx->recon_coeff_ptr[txt_itr] = &ctx->tx_recon_coeff_pool.descs[txt_itr];
604
24.9k
        ctx->recon_ptr[txt_itr]       = &ctx->tx_recon_pool.descs[txt_itr];
605
24.9k
        ctx->quant_coeff_ptr[txt_itr] = &ctx->tx_quant_coeff_pool.descs[txt_itr];
606
24.9k
    }
607
1.56k
    EB_NEW(ctx->tx_coeffs, svt_picture_buffer_desc_ctor, (EbPtr)&thirty_two_width_picture_buffer_desc_init_data);
608
1.56k
    EB_NEW(ctx->scratch_prediction_ptr, svt_picture_buffer_desc_ctor, (EbPtr)&picture_buffer_desc_init_data);
609
1.56k
    EbPictureBufferDescInitData double_width_picture_buffer_desc_init_data;
610
1.56k
    double_width_picture_buffer_desc_init_data.max_width          = sb_size;
611
1.56k
    double_width_picture_buffer_desc_init_data.max_height         = sb_size;
612
1.56k
    double_width_picture_buffer_desc_init_data.bit_depth          = EB_SIXTEEN_BIT;
613
1.56k
    double_width_picture_buffer_desc_init_data.color_format       = EB_YUV420;
614
1.56k
    double_width_picture_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
615
1.56k
    double_width_picture_buffer_desc_init_data.border             = 0;
616
1.56k
    double_width_picture_buffer_desc_init_data.split_mode         = false;
617
1.56k
    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.56k
    EB_NEW(ctx->temp_recon_ptr, svt_picture_buffer_desc_ctor, (EbPtr)&picture_buffer_desc_init_data);
622
1.56k
    EB_NEW(ctx->temp_residual, svt_picture_buffer_desc_ctor, (EbPtr)&double_width_picture_buffer_desc_init_data);
623
624
    // Candidate Buffers
625
1.56k
    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.56k
    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.56k
    EbPictureBufferDescInitData* pred_id = NULL;
634
1.56k
    EbPictureBufferDescInitData* rc_id   = NULL;
635
1.56k
    EbPictureBufferDescInitData* q_id    = NULL;
636
1.56k
    EB_MALLOC_ARRAY(pred_id, ctx->max_nics_uv);
637
1.56k
    EB_MALLOC_ARRAY(rc_id, ctx->max_nics_uv);
638
1.56k
    EB_MALLOC_ARRAY(q_id, ctx->max_nics_uv);
639
17.1k
    for (buffer_index = 0; buffer_index < ctx->max_nics_uv; ++buffer_index) {
640
15.6k
        const uint32_t mask                      = (buffer_index < ctx->max_nics) ? PICTURE_BUFFER_DESC_FULL_MASK
641
15.6k
                                                                                  : PICTURE_BUFFER_DESC_CHROMA_MASK;
642
15.6k
        pred_id[buffer_index].max_width          = sb_size;
643
15.6k
        pred_id[buffer_index].max_height         = sb_size;
644
15.6k
        pred_id[buffer_index].bit_depth          = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) ? EB_TEN_BIT : EB_EIGHT_BIT;
645
15.6k
        pred_id[buffer_index].color_format       = EB_YUV420;
646
15.6k
        pred_id[buffer_index].buffer_enable_mask = mask;
647
15.6k
        pred_id[buffer_index].border             = 0;
648
15.6k
        pred_id[buffer_index].split_mode         = false;
649
15.6k
        pred_id[buffer_index].is_16bit_pipeline  = SVT_EFFECTIVE_HBD_MD(ctx->hbd_md) > EB_8_BIT_MD;
650
        // rec_coeff / quant are identical but 32-bit.
651
15.6k
        rc_id[buffer_index]                   = pred_id[buffer_index];
652
15.6k
        rc_id[buffer_index].bit_depth         = EB_THIRTYTWO_BIT;
653
15.6k
        rc_id[buffer_index].is_16bit_pipeline = true;
654
15.6k
        q_id[buffer_index]                    = rc_id[buffer_index];
655
15.6k
    }
656
1.56k
    EbErrorType cand_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->cand_pred_pool, pred_id, ctx->max_nics_uv);
657
1.56k
    if (cand_err == EB_ErrorNone) {
658
1.56k
        cand_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->cand_rec_coeff_pool, rc_id, ctx->max_nics_uv);
659
1.56k
    }
660
1.56k
    if (cand_err == EB_ErrorNone) {
661
1.56k
        cand_err = svt_aom_pic_buf_desc_pool_ctor_var(&ctx->cand_quant_pool, q_id, ctx->max_nics_uv);
662
1.56k
    }
663
1.56k
    EB_FREE_ARRAY(pred_id);
664
1.56k
    EB_FREE_ARRAY(rc_id);
665
1.56k
    EB_FREE_ARRAY(q_id);
666
1.56k
    if (cand_err != EB_ErrorNone) {
667
0
        return cand_err;
668
0
    }
669
670
17.1k
    for (buffer_index = 0; buffer_index < ctx->max_nics_uv; ++buffer_index) {
671
15.6k
        ctx->cand_bf_ptr_array[buffer_index] = &ctx->cand_bf_pool[buffer_index];
672
15.6k
        EbErrorType cbf_err                  = svt_aom_mode_decision_cand_bf_ctor(ctx->cand_bf_ptr_array[buffer_index],
673
15.6k
                                                                 &ctx->cand_pred_pool.descs[buffer_index],
674
15.6k
                                                                 &ctx->cand_rec_coeff_pool.descs[buffer_index],
675
15.6k
                                                                 &ctx->cand_quant_pool.descs[buffer_index],
676
15.6k
                                                                 ctx->temp_residual,
677
15.6k
                                                                 ctx->temp_recon_ptr,
678
15.6k
                                                                 &(ctx->fast_cost_array[buffer_index]),
679
15.6k
                                                                 &(ctx->full_cost_array[buffer_index]),
680
15.6k
                                                                 &(ctx->full_cost_ssim_array[buffer_index]));
681
15.6k
        if (cbf_err != EB_ErrorNone) {
682
0
            return cbf_err;
683
0
        }
684
15.6k
    }
685
686
1.56k
    return EB_ErrorNone;
687
1.56k
}
688
689
/**************************************************
690
 * Reset Mode Decision Neighbor Arrays
691
 *************************************************/
692
5.28k
void svt_aom_reset_mode_decision_neighbor_arrays(PictureControlSet* pcs, uint16_t tile_idx) {
693
5.28k
    uint8_t depth;
694
21.1k
    for (depth = 0; depth < NA_TOT_CNT; depth++) {
695
15.8k
        svt_aom_neighbor_array_unit_reset(pcs->mdleaf_partition_na[depth][tile_idx]);
696
15.8k
        if (SVT_EFFECTIVE_HBD_MD(pcs->hbd_md) != EB_10_BIT_MD) {
697
15.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_luma_recon_na[depth][tile_idx]);
698
15.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_1_luma_recon_na[depth][tile_idx]);
699
15.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_2_luma_recon_na[depth][tile_idx]);
700
15.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_cb_recon_na[depth][tile_idx]);
701
15.8k
            svt_aom_neighbor_array_unit_reset(pcs->md_cr_recon_na[depth][tile_idx]);
702
15.8k
        }
703
15.8k
        if (SVT_EFFECTIVE_HBD_MD(pcs->hbd_md) > EB_8_BIT_MD ||
704
15.8k
            (SVT_EFFECTIVE_BIT_DEPTH(pcs->scs->encoder_bit_depth) > EB_EIGHT_BIT && pcs->pic_bypass_encdec)) {
705
0
            svt_aom_neighbor_array_unit_reset(pcs->md_luma_recon_na_16bit[depth][tile_idx]);
706
0
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_1_luma_recon_na_16bit[depth][tile_idx]);
707
0
            svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_2_luma_recon_na_16bit[depth][tile_idx]);
708
0
            svt_aom_neighbor_array_unit_reset(pcs->md_cb_recon_na_16bit[depth][tile_idx]);
709
0
            svt_aom_neighbor_array_unit_reset(pcs->md_cr_recon_na_16bit[depth][tile_idx]);
710
0
        }
711
712
15.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_y_dcs_na[depth][tile_idx]);
713
15.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_tx_depth_1_luma_dc_sign_level_coeff_na[depth][tile_idx]);
714
15.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_cb_dc_sign_level_coeff_na[depth][tile_idx]);
715
15.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_cr_dc_sign_level_coeff_na[depth][tile_idx]);
716
15.8k
        svt_aom_neighbor_array_unit_reset(pcs->md_txfm_context_array[depth][tile_idx]);
717
15.8k
    }
718
719
5.28k
    return;
720
5.28k
}
721
722
// If the ref intra percentage is below the TH, applying modulation to the MD lambda
723
0
#define LAMBDA_MOD_INTRA_TH 50
724
0
#define LAMBDA_MOD_INTRA_SCALING_FACTOR 138
725
726
// Set the lambda for each sb.
727
// When lambda tuning is on (blk_lambda_tuning), lambda of each block is set separately (full_lambda_md/fast_lambda_md)
728
// later in svt_aom_set_tuned_blk_lambda
729
// Testing showed that updating SAD lambda based on frame info was not helpful; therefore, the SAD lambda generation is not changed.
730
6.59k
static void av1_lambda_assign_md(PictureControlSet* pcs, ModeDecisionContext* ctx) {
731
6.59k
    ctx->full_lambda_md[0] = svt_aom_compute_rd_mult(pcs, ctx->qp_index, ctx->me_q_index, EB_EIGHT_BIT);
732
6.59k
    ctx->fast_lambda_md[0] = svt_aom_compute_fast_lambda(pcs, ctx->qp_index, ctx->me_q_index, EB_EIGHT_BIT);
733
6.59k
    ctx->full_lambda_md[1] = svt_aom_compute_rd_mult(pcs, ctx->qp_index, ctx->me_q_index, EB_TEN_BIT);
734
6.59k
    ctx->fast_lambda_md[1] = svt_aom_compute_fast_lambda(pcs, ctx->qp_index, ctx->me_q_index, EB_TEN_BIT);
735
736
6.59k
    if (!pcs->scs->static_config.rtc && pcs->scs->stats_based_sb_lambda_modulation) {
737
6.59k
        if (pcs->temporal_layer_index > 0) {
738
0
            if (pcs->ref_intra_percentage < LAMBDA_MOD_INTRA_TH) {
739
0
                ctx->full_lambda_md[0] = (ctx->full_lambda_md[0] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
740
0
                ctx->fast_lambda_md[0] = (ctx->fast_lambda_md[0] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
741
0
                ctx->full_lambda_md[1] = (ctx->full_lambda_md[1] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
742
0
                ctx->fast_lambda_md[1] = (ctx->fast_lambda_md[1] * LAMBDA_MOD_INTRA_SCALING_FACTOR) >> 7;
743
0
            }
744
0
        }
745
6.59k
    }
746
747
6.59k
    if (pcs->lambda_weight) {
748
3.49k
        ctx->full_lambda_md[0] = (uint32_t)((ctx->full_lambda_md[0] * (uint64_t)pcs->lambda_weight) >> 7);
749
3.49k
        ctx->fast_lambda_md[0] = (uint32_t)((ctx->fast_lambda_md[0] * (uint64_t)pcs->lambda_weight) >> 7);
750
3.49k
        ctx->full_lambda_md[1] = (uint32_t)((ctx->full_lambda_md[1] * (uint64_t)pcs->lambda_weight) >> 7);
751
3.49k
        ctx->fast_lambda_md[1] = (uint32_t)((ctx->fast_lambda_md[1] * (uint64_t)pcs->lambda_weight) >> 7);
752
3.49k
    }
753
6.59k
    ctx->full_lambda_md[1] *= 16;
754
6.59k
    ctx->fast_lambda_md[1] *= 4;
755
756
6.59k
    SequenceControlSet* scs          = pcs->scs;
757
6.59k
    uint64_t            scale_factor = scs->static_config.lambda_scale_factors[pcs->ppcs->update_type];
758
6.59k
    ctx->full_lambda_md[0]           = (uint32_t)((ctx->full_lambda_md[0] * scale_factor) >> 7);
759
6.59k
    ctx->full_lambda_md[1]           = (uint32_t)((ctx->full_lambda_md[1] * scale_factor) >> 7);
760
6.59k
    ctx->fast_lambda_md[0]           = (uint32_t)((ctx->fast_lambda_md[0] * scale_factor) >> 7);
761
6.59k
    ctx->fast_lambda_md[1]           = (uint32_t)((ctx->fast_lambda_md[1] * scale_factor) >> 7);
762
763
6.59k
    ctx->full_sb_lambda_md[0] = ctx->full_lambda_md[0];
764
6.59k
    ctx->full_sb_lambda_md[1] = ctx->full_lambda_md[1];
765
6.59k
}
766
767
void svt_aom_reset_mode_decision(SequenceControlSet* scs, ModeDecisionContext* ctx, PictureControlSet* pcs,
768
5.55k
                                 uint16_t tile_group_idx, uint32_t segment_index) {
769
5.55k
    ctx->hbd_md = SVT_EFFECTIVE_HBD_MD(pcs->hbd_md);
770
    // Reset MD rate Estimation table to initial values by copying from md_rate_est_ctx
771
5.55k
    ctx->md_rate_est_ctx = pcs->md_rate_est_ctx;
772
    // Reset CABAC Contexts
773
774
    // Reset Neighbor Arrays at start of new Segment / Picture
775
5.55k
    if (segment_index == 0) {
776
485
        for (uint16_t r = pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_start_y;
777
2.04k
             r < pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_end_y;
778
1.56k
             r++) {
779
1.56k
            for (uint16_t c = pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_start_x;
780
6.84k
                 c < pcs->ppcs->tile_group_info[tile_group_idx].tile_group_tile_end_x;
781
5.28k
                 c++) {
782
5.28k
                uint16_t tile_idx = c + r * pcs->ppcs->av1_cm->tiles_info.tile_cols;
783
5.28k
                svt_aom_reset_mode_decision_neighbor_arrays(pcs, tile_idx);
784
5.28k
            }
785
1.56k
        }
786
485
        (void)scs;
787
485
    }
788
    //each segment enherits the bypass encdec from the picture level
789
5.55k
    ctx->bypass_encdec = pcs->pic_bypass_encdec;
790
5.55k
    return;
791
5.55k
}
792
793
/******************************************************
794
 * Mode Decision Configure SB
795
 ******************************************************/
796
void svt_aom_mode_decision_configure_sb(ModeDecisionContext* ctx, PictureControlSet* pcs, uint8_t sb_qp,
797
6.59k
                                        uint8_t me_sb_qp) {
798
    /* 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 */
799
800
    // Lambda Assignement
801
6.59k
    ctx->qp_index = pcs->ppcs->frm_hdr.delta_q_params.delta_q_present || pcs->ppcs->r0_delta_qp_md
802
6.59k
        ? sb_qp
803
6.59k
        : (uint8_t)pcs->ppcs->frm_hdr.quantization_params.base_q_idx;
804
805
6.59k
    ctx->me_q_index = me_sb_qp;
806
807
6.59k
    av1_lambda_assign_md(pcs, ctx);
808
809
6.59k
    ctx->hbd_pack_done = 0;
810
811
6.59k
    return;
812
6.59k
}