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/pcs.c
Line
Count
Source
1
/*
2
* Copyright(c) 2019 Intel Corporation
3
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
4
*
5
* This source code is subject to the terms of the BSD 2 Clause License and
6
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7
* was not distributed with this source code in the LICENSE file, you can
8
* obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9
* Media Patent License 1.0 was not distributed with this source code in the
10
* PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11
*/
12
13
#include <stdlib.h>
14
15
#include "definitions.h"
16
#include "pcs.h"
17
#include "sequence_control_set.h"
18
#include "pic_buffer_desc.h"
19
#include "utility.h"
20
#include "resource_coordination_process.h"
21
#include "md_config_process.h"
22
#include "enc_mode_config.h"
23
24
void svt_aom_set_tile_info(PictureParentControlSet* pcs);
25
26
EbErrorType svt_av1_alloc_restoration_buffers(PictureControlSet* pcs, Av1Common* cm);
27
EbErrorType svt_av1_hash_table_create(HashTable* p_hash_table);
28
29
0
static void set_restoration_unit_size(int32_t width, int32_t height, int32_t sx, int32_t sy, RestorationInfo* rst) {
30
0
    (void)width;
31
0
    (void)height;
32
0
    (void)sx;
33
0
    (void)sy;
34
35
0
    int32_t s = 0;
36
37
0
    rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
38
39
0
    rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
40
0
    rst[2].restoration_unit_size = rst[1].restoration_unit_size;
41
0
}
42
43
1.94k
static void dg_detector_seg_dctor(EbPtr p) {
44
1.94k
    DGDetectorSeg* obj = (DGDetectorSeg*)p;
45
46
1.94k
    EB_DESTROY_SEMAPHORE(obj->frame_done_sem);
47
1.94k
    EB_DESTROY_MUTEX(obj->metrics_mutex);
48
1.94k
}
49
50
1.94k
EbErrorType svt_aom_dg_detector_seg_ctor(DGDetectorSeg* obj_ptr) {
51
1.94k
    obj_ptr->dctor = dg_detector_seg_dctor;
52
53
1.94k
    EB_CREATE_SEMAPHORE(obj_ptr->frame_done_sem, 0, 1);
54
1.94k
    EB_CREATE_MUTEX(obj_ptr->metrics_mutex);
55
1.94k
    return EB_ErrorNone;
56
1.94k
}
57
58
485
static void segmentation_map_dctor(EbPtr p) {
59
485
    SegmentationNeighborMap* obj = (SegmentationNeighborMap*)p;
60
485
    EB_FREE_ARRAY(obj->data);
61
485
}
62
63
0
static void svt_pcs_sb_structs_dctor(EbPtr p) {
64
0
    PictureParentControlSet* obj = (PictureParentControlSet*)p;
65
0
    EB_FREE_ARRAY(obj->b64_geom);
66
0
    free_sb_geoms(obj->sb_geom);
67
0
}
68
69
485
EbErrorType segmentation_map_ctor(SegmentationNeighborMap* seg_neighbor_map, uint16_t pic_width, uint16_t pic_height) {
70
485
    uint32_t num_elements = (pic_width >> MI_SIZE_LOG2) * (pic_height >> MI_SIZE_LOG2);
71
72
485
    seg_neighbor_map->dctor = segmentation_map_dctor;
73
74
485
    seg_neighbor_map->map_size = num_elements;
75
485
    EB_CALLOC_ARRAY(seg_neighbor_map->data, num_elements);
76
485
    return EB_ErrorNone;
77
485
}
78
79
0
static void me_sb_results_dctor(EbPtr p) {
80
    // me_mv_array / me_candidate_array / total_me_candidate_index are borrowed from per-parent-PCS
81
    // pools (freed in the parent-PCS dctor).
82
0
    (void)p;
83
0
}
84
85
/*
86
  controls how many references are needed for ME results allocation
87
*/
88
void svt_aom_get_max_allocated_me_refs(uint8_t ref_count_used_list0, uint8_t ref_count_used_list1,
89
485
                                       uint8_t* max_ref_to_alloc, uint8_t* max_cand_to_alloc) {
90
485
    *max_ref_to_alloc  = ref_count_used_list0 + ref_count_used_list1;
91
485
    *max_cand_to_alloc = ref_count_used_list0 + ref_count_used_list1 + (ref_count_used_list0 * ref_count_used_list1) +
92
485
        (ref_count_used_list0 - 1) + (ref_count_used_list1 == 3 ? 1 : 0);
93
485
}
94
95
EbErrorType svt_aom_me_sb_results_ctor(MeSbResults* obj_ptr, PictureControlSetInitData* init_data_ptr,
96
0
                                       MotionEstimationData* me_data, uint16_t sb_index, uint16_t all_sb) {
97
0
    obj_ptr->dctor = me_sb_results_dctor;
98
99
0
    uint8_t max_ref_to_alloc, max_cand_to_alloc;
100
0
    svt_aom_get_max_allocated_me_refs(init_data_ptr->ref_count_used_list0,
101
0
                                      init_data_ptr->ref_count_used_list1,
102
0
                                      &max_ref_to_alloc,
103
0
                                      &max_cand_to_alloc);
104
0
    ResolutionRange resolution;
105
0
    svt_aom_derive_input_resolution(&resolution, init_data_ptr->picture_width * init_data_ptr->picture_height);
106
0
    uint8_t number_of_pus = svt_aom_get_enable_me_16x16(init_data_ptr->enc_mode)
107
0
        ? svt_aom_get_enable_me_8x8(init_data_ptr->enc_mode, resolution, init_data_ptr->static_config.rtc)
108
0
            ? SQUARE_PU_COUNT
109
0
            : MAX_SB64_PU_COUNT_NO_8X8
110
0
        : MAX_SB64_PU_COUNT_WO_16X16;
111
112
    // All SBs resolve to identical number_of_pus, so back the whole me_results array with one
113
    // allocation each (created on the first SB) and hand each SB a slice.
114
0
    if (sb_index == 0) {
115
0
        EB_MALLOC_ARRAY(me_data->me_sb_mv_pool, (size_t)all_sb * number_of_pus * max_ref_to_alloc);
116
0
        EB_MALLOC_ARRAY(me_data->me_sb_cand_pool, (size_t)all_sb * number_of_pus * max_cand_to_alloc);
117
0
        EB_MALLOC_ARRAY(me_data->me_sb_totidx_pool, (size_t)all_sb * number_of_pus);
118
0
    }
119
0
    obj_ptr->me_mv_array              = me_data->me_sb_mv_pool + (size_t)sb_index * number_of_pus * max_ref_to_alloc;
120
0
    obj_ptr->me_candidate_array       = me_data->me_sb_cand_pool + (size_t)sb_index * number_of_pus * max_cand_to_alloc;
121
0
    obj_ptr->total_me_candidate_index = me_data->me_sb_totidx_pool + (size_t)sb_index * number_of_pus;
122
0
    return EB_ErrorNone;
123
0
}
124
125
485
void recon_coef_dctor(EbPtr p) {
126
485
    EncDecSet* obj = (EncDecSet*)p;
127
485
    EB_DELETE(obj->recon_pic_16bit);
128
485
    EB_DELETE(obj->recon_pic);
129
    // quantized_coeff[] descriptors are borrowed from the pool; free the pool backing +
130
    // descriptor storage once, then the (alias) pointer array.
131
485
    svt_aom_pic_buf_desc_pool_dctor(&obj->quantized_coeff_pool);
132
485
    EB_FREE_ARRAY(obj->quantized_coeff);
133
485
}
134
135
485
static void picture_control_set_dctor(EbPtr p) {
136
485
    PictureControlSet* obj      = (PictureControlSet*)p;
137
485
    uint16_t           tile_cnt = obj->tile_row_count * obj->tile_column_count;
138
485
    uint8_t            depth;
139
485
    svt_av1_hash_table_destroy(&obj->hash_table);
140
485
    EB_FREE_ALIGNED_ARRAY(obj->tpl_mvs);
141
485
    EB_DELETE_PTR_ARRAY(obj->enc_dec_segment_ctrl, tile_cnt);
142
485
    EB_DELETE_PTR_ARRAY(obj->ep_luma_recon_na, tile_cnt);
143
485
    EB_DELETE_PTR_ARRAY(obj->ep_cb_recon_na, tile_cnt);
144
485
    EB_DELETE_PTR_ARRAY(obj->ep_cr_recon_na, tile_cnt);
145
485
    EB_DELETE_PTR_ARRAY(obj->ep_luma_dc_sign_level_coeff_na, tile_cnt);
146
485
    EB_DELETE_PTR_ARRAY(obj->ep_cb_dc_sign_level_coeff_na, tile_cnt);
147
485
    EB_DELETE_PTR_ARRAY(obj->ep_cr_dc_sign_level_coeff_na, tile_cnt);
148
485
    EB_DELETE_PTR_ARRAY(obj->ep_luma_dc_sign_level_coeff_na_update, tile_cnt);
149
485
    EB_DELETE_PTR_ARRAY(obj->ep_cb_dc_sign_level_coeff_na_update, tile_cnt);
150
485
    EB_DELETE_PTR_ARRAY(obj->ep_cr_dc_sign_level_coeff_na_update, tile_cnt);
151
485
    EB_DELETE_PTR_ARRAY(obj->partition_context_na, tile_cnt);
152
485
    EB_DELETE_PTR_ARRAY(obj->luma_dc_sign_level_coeff_na, tile_cnt);
153
485
    EB_DELETE_PTR_ARRAY(obj->cr_dc_sign_level_coeff_na, tile_cnt);
154
485
    EB_DELETE_PTR_ARRAY(obj->cb_dc_sign_level_coeff_na, tile_cnt);
155
485
    EB_DELETE_PTR_ARRAY(obj->txfm_context_array, tile_cnt);
156
485
    EB_DELETE(obj->segmentation_neighbor_map); // Jing, double check here
157
485
    EB_DELETE_PTR_ARRAY(obj->ep_luma_recon_na_16bit, tile_cnt);
158
485
    EB_DELETE_PTR_ARRAY(obj->ep_cb_recon_na_16bit, tile_cnt);
159
485
    EB_DELETE_PTR_ARRAY(obj->ep_cr_recon_na_16bit, tile_cnt);
160
    // EB_DELETE(obj->ep_partition_context_na); //Jing: Double check here
161
485
    EB_DELETE_PTR_ARRAY(obj->ep_partition_context_na, tile_cnt);
162
485
    EB_DELETE_PTR_ARRAY(obj->ep_txfm_context_na, tile_cnt);
163
164
1.94k
    for (depth = 0; depth < NA_TOT_CNT; depth++) {
165
1.45k
        EB_DELETE_PTR_ARRAY(obj->mdleaf_partition_na[depth], tile_cnt);
166
167
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_luma_recon_na_16bit[depth], tile_cnt);
168
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_tx_depth_1_luma_recon_na_16bit[depth], tile_cnt);
169
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_tx_depth_2_luma_recon_na_16bit[depth], tile_cnt);
170
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_cb_recon_na_16bit[depth], tile_cnt);
171
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_cr_recon_na_16bit[depth], tile_cnt);
172
173
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_luma_recon_na[depth], tile_cnt);
174
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_tx_depth_1_luma_recon_na[depth], tile_cnt);
175
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_tx_depth_2_luma_recon_na[depth], tile_cnt);
176
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_cb_recon_na[depth], tile_cnt);
177
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_cr_recon_na[depth], tile_cnt);
178
179
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_y_dcs_na[depth], tile_cnt);
180
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_tx_depth_1_luma_dc_sign_level_coeff_na[depth], tile_cnt);
181
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_cr_dc_sign_level_coeff_na[depth], tile_cnt);
182
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_cb_dc_sign_level_coeff_na[depth], tile_cnt);
183
1.45k
        EB_DELETE_PTR_ARRAY(obj->md_txfm_context_array[depth], tile_cnt);
184
1.45k
    }
185
485
    EB_DELETE_PTR_ARRAY(obj->sb_ptr_array, obj->sb_total_count_unscaled);
186
    // Per-SB pools backing the SuperBlock final_blk_arr / av1xd / ptree (borrowed by each SB).
187
485
    EB_FREE_ARRAY(obj->sb_final_blk_arr_pool);
188
485
    EB_FREE_ARRAY(obj->sb_av1xd_pool);
189
485
    EB_FREE_ARRAY(obj->sb_ptree_pool);
190
485
    EB_FREE_ARRAY(obj->sb_intra);
191
485
    EB_FREE_ARRAY(obj->sb_skip);
192
485
    EB_FREE_ARRAY(obj->sb_64x64_mvp);
193
485
    EB_FREE_ARRAY(obj->b64_me_qindex);
194
485
    EB_FREE_ARRAY(obj->sb_min_sq_size);
195
485
    EB_FREE_ARRAY(obj->sb_max_sq_size);
196
485
    EB_DELETE(obj->bitstream_ptr);
197
485
    EB_DELETE_PTR_ARRAY(obj->ec_info, tile_cnt);
198
199
485
    const int32_t num_planes = 3; // av1_num_planes(cm);
200
1.94k
    for (int32_t pl = 0; pl < num_planes; ++pl) {
201
1.45k
        RestorationInfo*             ri         = obj->rst_info + pl;
202
1.45k
        RestorationStripeBoundaries* boundaries = &ri->boundaries;
203
1.45k
        EB_FREE_ARRAY(ri->unit_info);
204
1.45k
        EB_FREE(boundaries->stripe_boundary_above);
205
1.45k
        EB_FREE(boundaries->stripe_boundary_below);
206
1.45k
    }
207
485
    EB_FREE_ARRAY(obj->rusi_picture[0]);
208
485
    EB_FREE_ARRAY(obj->rusi_picture[1]);
209
485
    EB_FREE_ARRAY(obj->rusi_picture[2]);
210
485
    EB_DELETE(obj->input_frame16bit);
211
212
485
    EB_FREE_ARRAY(obj->mse_seg[0]);
213
485
    EB_FREE_ARRAY(obj->mse_seg[1]);
214
485
    EB_FREE_ARRAY(obj->skip_cdef_seg);
215
485
    EB_FREE_ARRAY(obj->cdef_dir_data);
216
485
    EB_FREE_ARRAY(obj->cdef_fb_list);
217
485
    EB_FREE_ARRAY(obj->cdef_sb_index);
218
485
    EB_FREE_ARRAY(obj->cdef_mse_ptr[0]);
219
485
    EB_FREE_ARRAY(obj->cdef_mse_ptr[1]);
220
485
    svt_aom_free(obj->cdef_row_cdef);
221
1.94k
    for (int cdef_p = 0; cdef_p < 3; cdef_p++) {
222
1.45k
        svt_aom_free(obj->cdef_linebuf[cdef_p]);
223
1.45k
        svt_aom_free(obj->cdef_colbuf[cdef_p]);
224
1.45k
    }
225
485
    EB_FREE_ARRAY(obj->mi_grid_base);
226
485
    EB_FREE_ARRAY(obj->mip);
227
485
    EB_FREE_ARRAY(obj->md_rate_est_ctx);
228
485
    EB_DESTROY_MUTEX(obj->entropy_coding_pic_mutex);
229
485
    EB_DESTROY_MUTEX(obj->intra_mutex);
230
485
    EB_DESTROY_MUTEX(obj->cdef_search_mutex);
231
485
    EB_DESTROY_MUTEX(obj->rest_search_mutex);
232
485
}
233
234
typedef struct InitData {
235
    NeighborArrayUnit** na_unit_dbl_ptr;
236
    uint32_t            max_picture_width;
237
    uint32_t            max_picture_height;
238
    uint32_t            unit_size;
239
    uint8_t             granularity_normal;
240
    uint8_t             type_mask;
241
} InitData;
242
243
36.9k
#define DIM(array) (sizeof(array) / sizeof(array[0]))
244
245
36.9k
static EbErrorType create_neighbor_array_units(InitData* data, size_t count) {
246
295k
    for (size_t i = 0; i < count; i++) {
247
258k
        EB_NEW(*data[i].na_unit_dbl_ptr,
248
258k
               svt_aom_neighbor_array_unit_ctor,
249
258k
               data[i].max_picture_width,
250
258k
               data[i].max_picture_height,
251
258k
               data[i].unit_size,
252
258k
               data[i].granularity_normal,
253
258k
               data[i].type_mask);
254
258k
    }
255
36.9k
    return EB_ErrorNone;
256
36.9k
}
257
258
/*
259
recon_coef_update_param: update the parameters in EncDecSet for changing the resolution on the fly
260
*/
261
0
EbErrorType recon_coef_update_param(EncDecSet* object_ptr, SequenceControlSet* scs) {
262
0
    EbPictureBufferDescInitData input_pic_buf_desc_init_data;
263
0
    bool                        is_16bit = SVT_EFFECTIVE_BIT_DEPTH(scs->encoder_bit_depth) > 8 ? true : false;
264
    // Init Picture Init data
265
0
    input_pic_buf_desc_init_data.max_width          = scs->max_input_luma_width;
266
0
    input_pic_buf_desc_init_data.max_height         = scs->max_input_luma_height;
267
0
    input_pic_buf_desc_init_data.bit_depth          = scs->encoder_bit_depth;
268
0
    input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
269
0
    input_pic_buf_desc_init_data.color_format       = scs->static_config.encoder_color_format;
270
0
    uint16_t padding                                = scs->sb_size + 32;
271
0
    if (scs->static_config.superres_mode > SUPERRES_NONE || scs->static_config.resize_mode > RESIZE_NONE) {
272
0
        padding += scs->sb_size;
273
0
    }
274
0
    input_pic_buf_desc_init_data.border     = padding;
275
0
    input_pic_buf_desc_init_data.split_mode = false;
276
277
    //  Reconstructed Picture Buffer
278
0
    if (is_16bit) {
279
0
        svt_recon_picture_buffer_desc_update(object_ptr->recon_pic_16bit, (EbPtr)&input_pic_buf_desc_init_data);
280
0
        svt_recon_picture_buffer_desc_update(object_ptr->recon_pic, (EbPtr)&input_pic_buf_desc_init_data);
281
0
    } else {
282
0
        svt_recon_picture_buffer_desc_update(object_ptr->recon_pic, (EbPtr)&input_pic_buf_desc_init_data);
283
0
        if (SVT_EFFECTIVE_IS_16BIT_PIPELINE(scs->is_16bit_pipeline)) {
284
0
            input_pic_buf_desc_init_data.bit_depth = EB_SIXTEEN_BIT;
285
0
            svt_recon_picture_buffer_desc_update(object_ptr->recon_pic_16bit, (EbPtr)&input_pic_buf_desc_init_data);
286
0
        }
287
0
    }
288
289
    // SB Array
290
0
    object_ptr->b64_total_count = scs->b64_total_count;
291
292
0
    return EB_ErrorNone;
293
0
}
294
295
485
static EbErrorType recon_coef_ctor(EncDecSet* object_ptr, EbPtr object_init_data_ptr) {
296
485
    PictureControlSetInitData* init_data_ptr = (PictureControlSetInitData*)object_init_data_ptr;
297
298
485
    EbPictureBufferDescInitData input_pic_buf_desc_init_data;
299
300
485
    uint16_t sb_index;
301
485
    bool     is_16bit = init_data_ptr->bit_depth > 8 ? true : false;
302
303
    //object_ptr->tile_row_count  = init_data_ptr->tile_row_count;
304
    //object_ptr->tile_column_count = init_data_ptr->tile_column_count;
305
306
485
    object_ptr->dctor = recon_coef_dctor;
307
308
    // Init Picture Init data
309
485
    input_pic_buf_desc_init_data.max_width          = init_data_ptr->picture_width;
310
485
    input_pic_buf_desc_init_data.max_height         = init_data_ptr->picture_height;
311
485
    input_pic_buf_desc_init_data.bit_depth          = init_data_ptr->bit_depth;
312
485
    input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
313
485
    input_pic_buf_desc_init_data.color_format       = init_data_ptr->color_format;
314
485
    uint16_t padding                                = init_data_ptr->sb_size + 32;
315
485
    if (init_data_ptr->is_scale) {
316
0
        padding += init_data_ptr->sb_size;
317
0
    }
318
485
    input_pic_buf_desc_init_data.border     = padding;
319
485
    input_pic_buf_desc_init_data.split_mode = false;
320
321
485
    object_ptr->recon_pic_16bit = NULL;
322
485
    object_ptr->recon_pic       = NULL; // OMK
323
    // object_ptr->color_format           = init_data_ptr->color_format;
324
    //  Reconstructed Picture Buffer
325
485
    if (is_16bit) {
326
0
        EB_NEW(object_ptr->recon_pic_16bit, svt_recon_picture_buffer_desc_ctor, (EbPtr)&input_pic_buf_desc_init_data);
327
        // Need 8bit NREF recon buffer if bypassing EncDec when using 8bit MD to store RECON for
328
        // NREF picture INTRA prediction
329
        // TODO: Copy to a local buffer in MD instead
330
0
        EB_NEW(object_ptr->recon_pic, svt_recon_picture_buffer_desc_ctor, (EbPtr)&input_pic_buf_desc_init_data);
331
485
    } else {
332
485
        EB_NEW(object_ptr->recon_pic, // OMK
333
485
               svt_recon_picture_buffer_desc_ctor,
334
485
               (EbPtr)&input_pic_buf_desc_init_data);
335
485
        if (SVT_EFFECTIVE_IS_16BIT_PIPELINE(init_data_ptr->is_16bit_pipeline)) {
336
0
            input_pic_buf_desc_init_data.bit_depth = EB_SIXTEEN_BIT;
337
0
            EB_NEW(
338
0
                object_ptr->recon_pic_16bit, svt_recon_picture_buffer_desc_ctor, (EbPtr)&input_pic_buf_desc_init_data);
339
0
        }
340
485
    }
341
342
    // SB Array
343
485
    const uint16_t picture_b64_width = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_width, init_data_ptr->b64_size);
344
485
    const uint16_t picture_b64_height = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_height,
345
485
                                                                  init_data_ptr->b64_size);
346
485
    object_ptr->b64_total_count       = picture_b64_width * picture_b64_height;
347
485
    object_ptr->init_b64_total_count  = object_ptr->b64_total_count;
348
485
    EB_ALLOC_PTR_ARRAY(object_ptr->quantized_coeff, object_ptr->init_b64_total_count);
349
350
    //object_ptr->sb_total_count_pix = all_sb;
351
352
485
    EbPictureBufferDescInitData coeff_init_data;
353
485
    coeff_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
354
485
    coeff_init_data.max_width          = init_data_ptr->sb_size;
355
485
    coeff_init_data.max_height         = init_data_ptr->sb_size;
356
485
    coeff_init_data.bit_depth          = EB_THIRTYTWO_BIT;
357
485
    coeff_init_data.color_format       = init_data_ptr->color_format;
358
485
    coeff_init_data.border             = 0;
359
485
    coeff_init_data.split_mode         = false;
360
485
    coeff_init_data.is_16bit_pipeline  = SVT_EFFECTIVE_IS_16BIT_PIPELINE(init_data_ptr->is_16bit_pipeline);
361
    // One backing allocation for all per-SB coeff descriptors (was one alloc per SB).
362
485
    EbErrorType pool_err = svt_aom_pic_buf_desc_pool_ctor(
363
485
        &object_ptr->quantized_coeff_pool, &coeff_init_data, object_ptr->init_b64_total_count);
364
485
    if (pool_err != EB_ErrorNone) {
365
0
        return pool_err;
366
0
    }
367
7.07k
    for (sb_index = 0; sb_index < object_ptr->init_b64_total_count; ++sb_index) {
368
6.59k
        object_ptr->quantized_coeff[sb_index] = &object_ptr->quantized_coeff_pool.descs[sb_index];
369
6.59k
    }
370
371
485
    return EB_ErrorNone;
372
485
}
373
374
485
uint32_t svt_aom_get_out_buffer_size(uint32_t picture_width, uint32_t picture_height) {
375
    // Compressed frames are far smaller than the raw 4:2:0 frame, so start the bitstream
376
    // capacity at raw/4. The entropy writer grows on demand if a (pathological, very low-QP)
377
    // frame ever exceeds it, so this stays correct while cutting the previous over-provisioning.
378
485
    return picture_width * picture_height * 3 / 2 / 4; // raw 4:2:0 frame size / 4
379
485
}
380
381
/*
382
pcs_update_param: update the parameters in PictureParentControlSet for changing the resolution on the fly
383
*/
384
0
EbErrorType pcs_update_param(PictureControlSet* pcs, int8_t enc_mode) {
385
0
    SequenceControlSet* scs      = pcs->scs;
386
0
    const bool          rtc_tune = scs->static_config.rtc;
387
0
    const bool          allintra = scs->allintra;
388
    // Max/Min CU Sizes
389
0
    const uint32_t max_blk_size = scs->super_block_size;
390
    // SBs
391
0
    uint16_t sb_index;
392
0
    uint16_t sb_origin_x;
393
0
    uint16_t sb_origin_y;
394
395
0
    bool is_16bit = SVT_EFFECTIVE_BIT_DEPTH(scs->encoder_bit_depth) > 8 ? true : false;
396
    // Init Picture Init data
397
0
    EbPictureBufferDescInitData coeff_buffer_desc_init_data;
398
0
    uint16_t                    padding = scs->super_block_size + 32;
399
400
0
    coeff_buffer_desc_init_data.max_width          = scs->max_input_luma_width;
401
0
    coeff_buffer_desc_init_data.max_height         = scs->max_input_luma_height;
402
0
    coeff_buffer_desc_init_data.bit_depth          = EB_SIXTEEN_BIT;
403
0
    coeff_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
404
0
    coeff_buffer_desc_init_data.color_format       = scs->static_config.encoder_color_format;
405
406
0
    coeff_buffer_desc_init_data.border            = padding;
407
0
    coeff_buffer_desc_init_data.split_mode        = false;
408
0
    coeff_buffer_desc_init_data.is_16bit_pipeline = SVT_EFFECTIVE_IS_16BIT_PIPELINE(scs->is_16bit_pipeline);
409
0
    if ((is_16bit) || (SVT_EFFECTIVE_IS_16BIT_PIPELINE(scs->is_16bit_pipeline))) {
410
0
        svt_picture_buffer_desc_update(pcs->input_frame16bit, (EbPtr)&coeff_buffer_desc_init_data);
411
0
    }
412
0
    if (allintra ? svt_aom_get_enable_restoration_allintra(enc_mode, scs->static_config.enable_restoration_filtering)
413
0
            : rtc_tune ? svt_aom_get_enable_restoration_rtc(scs->static_config.enable_restoration_filtering,
414
0
                                                            scs->input_resolution,
415
0
                                                            scs->static_config.fast_decode)
416
0
                       : svt_aom_get_enable_restoration_default(enc_mode,
417
0
                                                                scs->static_config.enable_restoration_filtering,
418
0
                                                                scs->input_resolution,
419
0
                                                                scs->static_config.fast_decode)) {
420
0
        set_restoration_unit_size(scs->max_input_luma_width, scs->max_input_luma_height, 1, 1, pcs->rst_info);
421
0
    }
422
0
    pcs->frame_width  = scs->max_input_luma_width;
423
0
    pcs->frame_height = scs->max_input_luma_height;
424
    // SB Array
425
0
    pcs->b64_total_count = scs->b64_total_count;
426
0
    sb_origin_x          = 0;
427
0
    sb_origin_y          = 0;
428
429
0
    const uint16_t picture_sb_w = (uint16_t)DIVIDE_AND_CEIL(scs->max_input_luma_width, scs->sb_size);
430
0
    const uint16_t picture_sb_h = (uint16_t)DIVIDE_AND_CEIL(scs->max_input_luma_height, scs->sb_size);
431
0
    const uint16_t all_sb       = picture_sb_w * picture_sb_h;
432
0
    pcs->sb_total_count         = scs->sb_total_count;
433
434
0
    for (sb_index = 0; sb_index < all_sb; ++sb_index) {
435
0
        pcs->sb_ptr_array[sb_index]->org_x = (uint16_t)(sb_origin_x * max_blk_size);
436
0
        pcs->sb_ptr_array[sb_index]->org_y = (uint16_t)(sb_origin_y * max_blk_size);
437
438
0
        pcs->sb_ptr_array[sb_index]->index = sb_index;
439
        // Increment the Order in coding order (Raster Scan Order)
440
0
        sb_origin_y = (sb_origin_x == picture_sb_w - 1) ? sb_origin_y + 1 : sb_origin_y;
441
0
        sb_origin_x = (sb_origin_x == picture_sb_w - 1) ? 0 : sb_origin_x + 1;
442
0
    }
443
444
0
    uint32_t mi_stride = picture_sb_w * (scs->sb_size >> MI_SIZE_LOG2);
445
0
    for (uint16_t mi_h = 0; mi_h < picture_sb_h * (scs->sb_size >> MI_SIZE_LOG2); mi_h++) {
446
0
        for (uint16_t mi_w = 0; mi_w < picture_sb_w * (scs->sb_size >> MI_SIZE_LOG2); mi_w++) {
447
0
            uint16_t mi_grid_idx = mi_h * mi_stride + mi_w;
448
0
            uint16_t mip_idx     = (mi_h >> (pcs->disallow_4x4_all_frames + pcs->disallow_8x8_all_frames)) *
449
0
                    (mi_stride >> (pcs->disallow_4x4_all_frames + pcs->disallow_8x8_all_frames)) +
450
0
                (mi_w >> (pcs->disallow_4x4_all_frames + pcs->disallow_8x8_all_frames));
451
0
            pcs->mi_grid_base[mi_grid_idx] = pcs->mip + mip_idx;
452
0
        }
453
0
    }
454
0
    pcs->mi_stride = picture_sb_w * (scs->sb_size >> MI_SIZE_LOG2);
455
0
    return EB_ErrorNone;
456
0
}
457
458
485
static EbErrorType picture_control_set_ctor(PictureControlSet* object_ptr, EbPtr object_init_data_ptr) {
459
485
    PictureControlSetInitData* init_data_ptr = (PictureControlSetInitData*)object_init_data_ptr;
460
461
485
    const bool                  allintra = init_data_ptr->allintra;
462
485
    const bool                  rtc_tune = init_data_ptr->rtc_tune;
463
485
    EbPictureBufferDescInitData coeff_buffer_desc_init_data;
464
465
    // Max/Min CU Sizes
466
485
    const uint32_t max_blk_size = init_data_ptr->sb_size;
467
485
    uint16_t       sb_index;
468
485
    uint16_t       sb_origin_x;
469
485
    uint16_t       sb_origin_y;
470
485
    EbErrorType    return_error;
471
472
485
    bool           is_16bit      = init_data_ptr->bit_depth > 8 ? true : false;
473
485
    const uint16_t subsampling_x = (init_data_ptr->color_format == EB_YUV444 ? 0 : 1);
474
485
    const uint16_t subsampling_y = (init_data_ptr->color_format >= EB_YUV422 ? 0 : 1);
475
476
485
    uint32_t total_tile_cnt = init_data_ptr->tile_row_count * init_data_ptr->tile_column_count;
477
485
    uint32_t tile_idx       = 0;
478
479
485
    uint32_t output_buffer_size   = svt_aom_get_out_buffer_size(init_data_ptr->picture_width,
480
485
                                                              init_data_ptr->picture_height);
481
485
    object_ptr->frame_width       = init_data_ptr->picture_width;
482
485
    object_ptr->frame_height      = init_data_ptr->picture_height;
483
485
    object_ptr->tile_row_count    = init_data_ptr->tile_row_count;
484
485
    object_ptr->tile_column_count = init_data_ptr->tile_column_count;
485
486
485
    object_ptr->dctor = picture_control_set_dctor;
487
488
485
    object_ptr->hash_table.p_lookup_table = NULL;
489
490
    // Init Picture Init data
491
485
    uint16_t padding = init_data_ptr->sb_size + 32;
492
493
485
    coeff_buffer_desc_init_data.max_width          = init_data_ptr->picture_width;
494
485
    coeff_buffer_desc_init_data.max_height         = init_data_ptr->picture_height;
495
485
    coeff_buffer_desc_init_data.bit_depth          = EB_SIXTEEN_BIT;
496
485
    coeff_buffer_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_FULL_MASK;
497
485
    coeff_buffer_desc_init_data.color_format       = init_data_ptr->color_format;
498
499
485
    coeff_buffer_desc_init_data.border            = padding;
500
485
    coeff_buffer_desc_init_data.split_mode        = false;
501
485
    coeff_buffer_desc_init_data.is_16bit_pipeline = SVT_EFFECTIVE_IS_16BIT_PIPELINE(init_data_ptr->is_16bit_pipeline);
502
485
    object_ptr->color_format                      = init_data_ptr->color_format;
503
485
    object_ptr->temp_lf_recon_pic_16bit           = NULL;
504
485
    object_ptr->temp_lf_recon_pic                 = NULL;
505
485
    object_ptr->scaled_input_pic                  = NULL;
506
485
    bool enable_restoration                       = allintra
507
485
                              ? svt_aom_get_enable_restoration_allintra(init_data_ptr->enc_mode,
508
485
                                                  init_data_ptr->static_config.enable_restoration_filtering)
509
485
                              : rtc_tune ? svt_aom_get_enable_restoration_rtc(init_data_ptr->static_config.enable_restoration_filtering,
510
0
                                                        init_data_ptr->input_resolution,
511
0
                                                        init_data_ptr->static_config.fast_decode)
512
0
                                         : svt_aom_get_enable_restoration_default(init_data_ptr->enc_mode,
513
0
                                                            init_data_ptr->static_config.enable_restoration_filtering,
514
0
                                                            init_data_ptr->input_resolution,
515
0
                                                            init_data_ptr->static_config.fast_decode);
516
485
    if (enable_restoration) {
517
0
        set_restoration_unit_size(
518
0
            init_data_ptr->picture_width, init_data_ptr->picture_height, 1, 1, object_ptr->rst_info);
519
520
0
        if (svt_av1_alloc_restoration_buffers(object_ptr, init_data_ptr->av1_cm) != EB_ErrorNone) {
521
0
            return EB_ErrorInsufficientResources;
522
0
        }
523
524
0
        int32_t ntiles[2];
525
0
        for (int32_t is_uv = 0; is_uv < 2; ++is_uv) {
526
0
            ntiles[is_uv] = object_ptr->rst_info[is_uv].units_per_tile; //CHKN res_tiles_in_plane
527
0
        }
528
0
        assert(ntiles[1] <= ntiles[0]);
529
0
        EB_CALLOC_ARRAY(object_ptr->rusi_picture[0], ntiles[0]);
530
0
        EB_CALLOC_ARRAY(object_ptr->rusi_picture[1], ntiles[1]);
531
0
        EB_CALLOC_ARRAY(object_ptr->rusi_picture[2], ntiles[1]);
532
0
    }
533
534
485
    if ((is_16bit) || (SVT_EFFECTIVE_IS_16BIT_PIPELINE(init_data_ptr->is_16bit_pipeline))) {
535
0
        EB_NEW(object_ptr->input_frame16bit, svt_picture_buffer_desc_ctor, (EbPtr)&coeff_buffer_desc_init_data);
536
0
    }
537
    // Entropy Coder
538
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ec_info, total_tile_cnt);
539
5.76k
    for (tile_idx = 0; tile_idx < total_tile_cnt; tile_idx++) {
540
5.28k
        EB_NEW(object_ptr->ec_info[tile_idx], svt_aom_entropy_tile_info_ctor, output_buffer_size / total_tile_cnt);
541
5.28k
    }
542
543
    // Packetization process Bitstream
544
485
    EB_NEW(object_ptr->bitstream_ptr, svt_aom_bitstream_ctor, output_buffer_size);
545
546
    // GOP
547
485
    object_ptr->picture_number       = 0;
548
485
    object_ptr->temporal_layer_index = 0;
549
550
    // SB Array
551
485
    const uint16_t picture_b64_width = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_width, init_data_ptr->b64_size);
552
485
    const uint16_t picture_b64_height = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_height,
553
485
                                                                  init_data_ptr->b64_size);
554
485
    object_ptr->b64_total_count       = picture_b64_width * picture_b64_height;
555
485
    object_ptr->init_b64_total_count  = object_ptr->b64_total_count;
556
485
    EB_MALLOC_ARRAY(object_ptr->sb_64x64_mvp, object_ptr->init_b64_total_count);
557
485
    EB_MALLOC_ARRAY(object_ptr->b64_me_qindex, object_ptr->init_b64_total_count);
558
485
    if (!allintra) {
559
0
        EB_MALLOC_ARRAY(object_ptr->sb_intra, object_ptr->init_b64_total_count);
560
0
        EB_MALLOC_ARRAY(object_ptr->sb_skip, object_ptr->init_b64_total_count);
561
0
        EB_MALLOC_ARRAY(object_ptr->sb_min_sq_size, object_ptr->init_b64_total_count);
562
0
        EB_MALLOC_ARRAY(object_ptr->sb_max_sq_size, object_ptr->init_b64_total_count);
563
0
    }
564
485
    sb_origin_x = 0;
565
485
    sb_origin_y = 0;
566
567
485
    const uint16_t picture_sb_w = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_width, init_data_ptr->sb_size);
568
485
    const uint16_t picture_sb_h = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_height, init_data_ptr->sb_size);
569
485
    const uint16_t all_sb       = picture_sb_w * picture_sb_h;
570
571
485
    object_ptr->sb_total_count          = all_sb;
572
485
    object_ptr->sb_total_count_unscaled = all_sb;
573
485
    EB_ALLOC_PTR_ARRAY(object_ptr->sb_ptr_array, object_ptr->sb_total_count_unscaled);
574
7.07k
    for (sb_index = 0; sb_index < all_sb; ++sb_index) {
575
6.59k
        EB_NEW(object_ptr->sb_ptr_array[sb_index],
576
6.59k
               svt_aom_largest_coding_unit_ctor,
577
6.59k
               (uint8_t)init_data_ptr->sb_size,
578
6.59k
               (uint16_t)(sb_origin_x * max_blk_size),
579
6.59k
               (uint16_t)(sb_origin_y * max_blk_size),
580
6.59k
               (uint16_t)sb_index,
581
6.59k
               init_data_ptr->enc_mode,
582
6.59k
               init_data_ptr->static_config.rtc,
583
6.59k
               allintra,
584
6.59k
               object_ptr);
585
586
        // Increment the Order in coding order (Raster Scan Order)
587
6.59k
        sb_origin_y = (sb_origin_x == picture_sb_w - 1) ? sb_origin_y + 1 : sb_origin_y;
588
6.59k
        sb_origin_x = (sb_origin_x == picture_sb_w - 1) ? 0 : sb_origin_x + 1;
589
6.59k
    }
590
    // MD Rate Estimation Array
591
485
    EB_MALLOC_ARRAY(object_ptr->md_rate_est_ctx, 1);
592
485
    memset(object_ptr->md_rate_est_ctx, 0, sizeof(MdRateEstimationContext));
593
485
    if (SVT_EFFECTIVE_HBD_MD(init_data_ptr->hbd_md) == DEFAULT) {
594
0
        object_ptr->hbd_md = init_data_ptr->hbd_md = 2;
595
485
    } else {
596
485
        object_ptr->hbd_md = SVT_EFFECTIVE_HBD_MD(init_data_ptr->hbd_md);
597
485
    }
598
    // Mode Decision Neighbor Arrays
599
485
    uint8_t depth;
600
1.94k
    for (depth = 0; depth < NA_TOT_CNT; depth++) {
601
1.45k
        EB_ALLOC_PTR_ARRAY(object_ptr->mdleaf_partition_na[depth], total_tile_cnt);
602
1.45k
        EB_ALLOC_PTR_ARRAY(object_ptr->md_y_dcs_na[depth], total_tile_cnt);
603
1.45k
        EB_ALLOC_PTR_ARRAY(object_ptr->md_tx_depth_1_luma_dc_sign_level_coeff_na[depth], total_tile_cnt);
604
1.45k
        EB_ALLOC_PTR_ARRAY(object_ptr->md_cr_dc_sign_level_coeff_na[depth], total_tile_cnt);
605
1.45k
        EB_ALLOC_PTR_ARRAY(object_ptr->md_cb_dc_sign_level_coeff_na[depth], total_tile_cnt);
606
1.45k
        EB_ALLOC_PTR_ARRAY(object_ptr->md_txfm_context_array[depth], total_tile_cnt);
607
1.45k
        if (SVT_EFFECTIVE_HBD_MD(init_data_ptr->hbd_md) != EB_10_BIT_MD) {
608
1.45k
            EB_ALLOC_PTR_ARRAY(object_ptr->md_luma_recon_na[depth], total_tile_cnt);
609
1.45k
            EB_ALLOC_PTR_ARRAY(object_ptr->md_tx_depth_1_luma_recon_na[depth], total_tile_cnt);
610
1.45k
            EB_ALLOC_PTR_ARRAY(object_ptr->md_tx_depth_2_luma_recon_na[depth], total_tile_cnt);
611
1.45k
            EB_ALLOC_PTR_ARRAY(object_ptr->md_cb_recon_na[depth], total_tile_cnt);
612
1.45k
            EB_ALLOC_PTR_ARRAY(object_ptr->md_cr_recon_na[depth], total_tile_cnt);
613
1.45k
        }
614
1.45k
        if (SVT_EFFECTIVE_HBD_MD(init_data_ptr->hbd_md) > EB_8_BIT_MD) {
615
0
            EB_ALLOC_PTR_ARRAY(object_ptr->md_luma_recon_na_16bit[depth], total_tile_cnt);
616
0
            EB_ALLOC_PTR_ARRAY(object_ptr->md_tx_depth_1_luma_recon_na_16bit[depth], total_tile_cnt);
617
0
            EB_ALLOC_PTR_ARRAY(object_ptr->md_tx_depth_2_luma_recon_na_16bit[depth], total_tile_cnt);
618
0
            EB_ALLOC_PTR_ARRAY(object_ptr->md_cb_recon_na_16bit[depth], total_tile_cnt);
619
0
            EB_ALLOC_PTR_ARRAY(object_ptr->md_cr_recon_na_16bit[depth], total_tile_cnt);
620
0
        }
621
1.45k
    }
622
623
485
    const uint32_t na_max_pic_w = init_data_ptr->picture_width + 2 * BLOCK_SIZE_64;
624
485
    const uint32_t na_max_pic_h = init_data_ptr->picture_height + 2 * BLOCK_SIZE_64;
625
626
5.76k
    for (tile_idx = 0; tile_idx < total_tile_cnt; tile_idx++) {
627
21.1k
        for (depth = 0; depth < NA_TOT_CNT; depth++) {
628
15.8k
            InitData data0[] = {
629
15.8k
                {
630
15.8k
                    &object_ptr->mdleaf_partition_na[depth][tile_idx],
631
15.8k
                    na_max_pic_w,
632
15.8k
                    na_max_pic_h,
633
15.8k
                    sizeof(PartitionContextType),
634
15.8k
                    PU_NEIGHBOR_ARRAY_GRANULARITY,
635
15.8k
                    NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
636
15.8k
                },
637
                // for each 4x4
638
15.8k
                {
639
15.8k
                    &object_ptr->md_y_dcs_na[depth][tile_idx],
640
15.8k
                    na_max_pic_w,
641
15.8k
                    na_max_pic_h,
642
15.8k
                    sizeof(uint8_t),
643
15.8k
                    PU_NEIGHBOR_ARRAY_GRANULARITY,
644
15.8k
                    NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
645
15.8k
                },
646
                // for each 4x4
647
15.8k
                {
648
15.8k
                    &object_ptr->md_tx_depth_1_luma_dc_sign_level_coeff_na[depth][tile_idx],
649
15.8k
                    na_max_pic_w,
650
15.8k
                    na_max_pic_h,
651
15.8k
                    sizeof(uint8_t),
652
15.8k
                    PU_NEIGHBOR_ARRAY_GRANULARITY,
653
15.8k
                    NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
654
15.8k
                },
655
                // for each 4x4
656
15.8k
                {
657
15.8k
                    &object_ptr->md_cr_dc_sign_level_coeff_na[depth][tile_idx],
658
15.8k
                    na_max_pic_w,
659
15.8k
                    na_max_pic_h,
660
15.8k
                    sizeof(uint8_t),
661
15.8k
                    PU_NEIGHBOR_ARRAY_GRANULARITY,
662
15.8k
                    NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
663
15.8k
                },
664
                // for each 4x4
665
15.8k
                {
666
15.8k
                    &object_ptr->md_cb_dc_sign_level_coeff_na[depth][tile_idx],
667
15.8k
                    na_max_pic_w,
668
15.8k
                    na_max_pic_h,
669
15.8k
                    sizeof(uint8_t),
670
15.8k
                    PU_NEIGHBOR_ARRAY_GRANULARITY,
671
15.8k
                    NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
672
15.8k
                },
673
15.8k
                {
674
15.8k
                    &object_ptr->md_txfm_context_array[depth][tile_idx],
675
15.8k
                    na_max_pic_w,
676
15.8k
                    na_max_pic_h,
677
15.8k
                    sizeof(TXFM_CONTEXT),
678
15.8k
                    PU_NEIGHBOR_ARRAY_GRANULARITY,
679
15.8k
                    NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
680
15.8k
                },
681
15.8k
            };
682
15.8k
            return_error = create_neighbor_array_units(data0, DIM(data0));
683
15.8k
            if (return_error == EB_ErrorInsufficientResources) {
684
0
                return EB_ErrorInsufficientResources;
685
0
            }
686
15.8k
            if (SVT_EFFECTIVE_HBD_MD(init_data_ptr->hbd_md) != EB_10_BIT_MD) {
687
15.8k
                InitData data[] = {
688
689
15.8k
                    {
690
15.8k
                        &object_ptr->md_luma_recon_na[depth][tile_idx],
691
15.8k
                        na_max_pic_w,
692
15.8k
                        na_max_pic_h,
693
15.8k
                        sizeof(uint8_t),
694
15.8k
                        SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
695
15.8k
                        NEIGHBOR_ARRAY_UNIT_FULL_MASK,
696
15.8k
                    },
697
15.8k
                    {
698
15.8k
                        &object_ptr->md_tx_depth_1_luma_recon_na[depth][tile_idx],
699
15.8k
                        na_max_pic_w,
700
15.8k
                        na_max_pic_h,
701
15.8k
                        sizeof(uint8_t),
702
15.8k
                        SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
703
15.8k
                        NEIGHBOR_ARRAY_UNIT_FULL_MASK,
704
15.8k
                    },
705
15.8k
                    {
706
15.8k
                        &object_ptr->md_tx_depth_2_luma_recon_na[depth][tile_idx],
707
15.8k
                        na_max_pic_w,
708
15.8k
                        na_max_pic_h,
709
15.8k
                        sizeof(uint8_t),
710
15.8k
                        SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
711
15.8k
                        NEIGHBOR_ARRAY_UNIT_FULL_MASK,
712
15.8k
                    },
713
15.8k
                    {
714
15.8k
                        &object_ptr->md_cb_recon_na[depth][tile_idx],
715
15.8k
                        na_max_pic_w >> subsampling_x,
716
15.8k
                        na_max_pic_h >> subsampling_y,
717
15.8k
                        sizeof(uint8_t),
718
15.8k
                        SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
719
15.8k
                        NEIGHBOR_ARRAY_UNIT_FULL_MASK,
720
15.8k
                    },
721
15.8k
                    {
722
15.8k
                        &object_ptr->md_cr_recon_na[depth][tile_idx],
723
15.8k
                        na_max_pic_w >> subsampling_x,
724
15.8k
                        na_max_pic_h >> subsampling_y,
725
15.8k
                        sizeof(uint8_t),
726
15.8k
                        SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
727
15.8k
                        NEIGHBOR_ARRAY_UNIT_FULL_MASK,
728
15.8k
                    }
729
730
15.8k
                };
731
15.8k
                return_error = create_neighbor_array_units(data, DIM(data));
732
15.8k
                if (return_error == EB_ErrorInsufficientResources) {
733
0
                    return EB_ErrorInsufficientResources;
734
0
                }
735
15.8k
            }
736
15.8k
            if (SVT_EFFECTIVE_HBD_MD(init_data_ptr->hbd_md) > EB_8_BIT_MD) {
737
0
                InitData data[] = {{
738
0
                                       &object_ptr->md_luma_recon_na_16bit[depth][tile_idx],
739
0
                                       na_max_pic_w,
740
0
                                       na_max_pic_h,
741
0
                                       sizeof(uint16_t),
742
0
                                       SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
743
0
                                       NEIGHBOR_ARRAY_UNIT_FULL_MASK,
744
0
                                   },
745
0
                                   {
746
0
                                       &object_ptr->md_tx_depth_1_luma_recon_na_16bit[depth][tile_idx],
747
0
                                       na_max_pic_w,
748
0
                                       na_max_pic_h,
749
0
                                       sizeof(uint16_t),
750
0
                                       SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
751
0
                                       NEIGHBOR_ARRAY_UNIT_FULL_MASK,
752
0
                                   },
753
0
                                   {
754
0
                                       &object_ptr->md_tx_depth_2_luma_recon_na_16bit[depth][tile_idx],
755
0
                                       na_max_pic_w,
756
0
                                       na_max_pic_h,
757
0
                                       sizeof(uint16_t),
758
0
                                       SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
759
0
                                       NEIGHBOR_ARRAY_UNIT_FULL_MASK,
760
0
                                   },
761
0
                                   {
762
0
                                       &object_ptr->md_cb_recon_na_16bit[depth][tile_idx],
763
0
                                       na_max_pic_w >> subsampling_x,
764
0
                                       na_max_pic_h >> subsampling_y,
765
0
                                       sizeof(uint16_t),
766
0
                                       SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
767
0
                                       NEIGHBOR_ARRAY_UNIT_FULL_MASK,
768
0
                                   },
769
0
                                   {
770
0
                                       &object_ptr->md_cr_recon_na_16bit[depth][tile_idx],
771
0
                                       na_max_pic_w >> subsampling_x,
772
0
                                       na_max_pic_h >> subsampling_y,
773
0
                                       sizeof(uint16_t),
774
0
                                       SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
775
0
                                       NEIGHBOR_ARRAY_UNIT_FULL_MASK,
776
0
                                   }};
777
0
                return_error    = create_neighbor_array_units(data, DIM(data));
778
0
                if (return_error == EB_ErrorInsufficientResources) {
779
0
                    return EB_ErrorInsufficientResources;
780
0
                }
781
0
            }
782
15.8k
        }
783
5.28k
    }
784
    // EncDec Neighbor
785
    //EncDec
786
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_luma_recon_na, total_tile_cnt);
787
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_cb_recon_na, total_tile_cnt);
788
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_cr_recon_na, total_tile_cnt);
789
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_luma_dc_sign_level_coeff_na, total_tile_cnt);
790
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_cb_dc_sign_level_coeff_na, total_tile_cnt);
791
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_cr_dc_sign_level_coeff_na, total_tile_cnt);
792
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_luma_dc_sign_level_coeff_na_update, total_tile_cnt);
793
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_cb_dc_sign_level_coeff_na_update, total_tile_cnt);
794
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_cr_dc_sign_level_coeff_na_update, total_tile_cnt);
795
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_partition_context_na, total_tile_cnt);
796
485
    EB_ALLOC_PTR_ARRAY(object_ptr->ep_txfm_context_na, total_tile_cnt);
797
    // Entropy
798
485
    EB_ALLOC_PTR_ARRAY(object_ptr->partition_context_na, total_tile_cnt);
799
485
    EB_ALLOC_PTR_ARRAY(object_ptr->luma_dc_sign_level_coeff_na, total_tile_cnt);
800
485
    EB_ALLOC_PTR_ARRAY(object_ptr->cr_dc_sign_level_coeff_na, total_tile_cnt);
801
485
    EB_ALLOC_PTR_ARRAY(object_ptr->cb_dc_sign_level_coeff_na, total_tile_cnt);
802
485
    EB_ALLOC_PTR_ARRAY(object_ptr->txfm_context_array, total_tile_cnt);
803
485
    if ((is_16bit) || (SVT_EFFECTIVE_IS_16BIT_PIPELINE(init_data_ptr->is_16bit_pipeline))) {
804
0
        EB_ALLOC_PTR_ARRAY(object_ptr->ep_luma_recon_na_16bit, total_tile_cnt);
805
0
        EB_ALLOC_PTR_ARRAY(object_ptr->ep_cb_recon_na_16bit, total_tile_cnt);
806
0
        EB_ALLOC_PTR_ARRAY(object_ptr->ep_cr_recon_na_16bit, total_tile_cnt);
807
0
    }
808
809
5.76k
    for (tile_idx = 0; tile_idx < total_tile_cnt; tile_idx++) {
810
5.28k
        InitData data0[] = {
811
5.28k
            {
812
5.28k
                &object_ptr->ep_luma_recon_na[tile_idx],
813
5.28k
                na_max_pic_w,
814
5.28k
                na_max_pic_h,
815
5.28k
                sizeof(uint8_t),
816
5.28k
                SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
817
5.28k
                NEIGHBOR_ARRAY_UNIT_FULL_MASK,
818
5.28k
            },
819
5.28k
            {
820
5.28k
                &object_ptr->ep_cb_recon_na[tile_idx],
821
5.28k
                na_max_pic_w >> subsampling_x,
822
5.28k
                na_max_pic_h >> subsampling_y,
823
5.28k
                sizeof(uint8_t),
824
5.28k
                SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
825
5.28k
                NEIGHBOR_ARRAY_UNIT_FULL_MASK,
826
5.28k
            },
827
5.28k
            {
828
829
5.28k
                &object_ptr->ep_cr_recon_na[tile_idx],
830
5.28k
                na_max_pic_w >> subsampling_x,
831
5.28k
                na_max_pic_h >> subsampling_y,
832
5.28k
                sizeof(uint8_t),
833
5.28k
                SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
834
5.28k
                NEIGHBOR_ARRAY_UNIT_FULL_MASK,
835
5.28k
            },
836
            // for each 4x4
837
5.28k
            {
838
5.28k
                &object_ptr->ep_luma_dc_sign_level_coeff_na[tile_idx],
839
5.28k
                na_max_pic_w,
840
5.28k
                na_max_pic_h,
841
5.28k
                sizeof(uint8_t),
842
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
843
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
844
5.28k
            },
845
            // for each 4x4
846
5.28k
            {
847
5.28k
                &object_ptr->ep_cb_dc_sign_level_coeff_na[tile_idx],
848
5.28k
                na_max_pic_w,
849
5.28k
                na_max_pic_h,
850
5.28k
                sizeof(uint8_t),
851
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
852
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
853
5.28k
            },
854
            // for each 4x4
855
5.28k
            {
856
5.28k
                &object_ptr->ep_cr_dc_sign_level_coeff_na[tile_idx],
857
5.28k
                na_max_pic_w,
858
5.28k
                na_max_pic_h,
859
5.28k
                sizeof(uint8_t),
860
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
861
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
862
5.28k
            },
863
            // for each 4x4
864
5.28k
            {
865
5.28k
                &object_ptr->ep_luma_dc_sign_level_coeff_na_update[tile_idx],
866
5.28k
                na_max_pic_w,
867
5.28k
                na_max_pic_h,
868
5.28k
                sizeof(uint8_t),
869
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
870
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
871
5.28k
            },
872
            // for each 4x4
873
5.28k
            {
874
5.28k
                &object_ptr->ep_cb_dc_sign_level_coeff_na_update[tile_idx],
875
5.28k
                na_max_pic_w,
876
5.28k
                na_max_pic_h,
877
5.28k
                sizeof(uint8_t),
878
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
879
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
880
5.28k
            },
881
            // for each 4x4
882
5.28k
            {
883
5.28k
                &object_ptr->ep_cr_dc_sign_level_coeff_na_update[tile_idx],
884
5.28k
                na_max_pic_w,
885
5.28k
                na_max_pic_h,
886
5.28k
                sizeof(uint8_t),
887
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
888
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
889
5.28k
            },
890
            // Encode pass partition neighbor array
891
5.28k
            {
892
5.28k
                &object_ptr->ep_partition_context_na[tile_idx],
893
5.28k
                na_max_pic_w,
894
5.28k
                na_max_pic_h,
895
5.28k
                sizeof(PartitionContextType),
896
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
897
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
898
5.28k
            },
899
            // Encode pass txfm neighbor array
900
5.28k
            {
901
5.28k
                &object_ptr->ep_txfm_context_na[tile_idx],
902
5.28k
                na_max_pic_w,
903
5.28k
                na_max_pic_h,
904
5.28k
                sizeof(TXFM_CONTEXT),
905
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
906
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
907
5.28k
            },
908
            // Entropy Coding Neighbor Arrays
909
5.28k
            {
910
5.28k
                &object_ptr->partition_context_na[tile_idx],
911
5.28k
                na_max_pic_w,
912
5.28k
                na_max_pic_h,
913
5.28k
                sizeof(PartitionContextType),
914
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
915
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
916
5.28k
            },
917
            // for each 4x4
918
5.28k
            {
919
5.28k
                &object_ptr->luma_dc_sign_level_coeff_na[tile_idx],
920
5.28k
                na_max_pic_w,
921
5.28k
                na_max_pic_h,
922
5.28k
                sizeof(uint8_t),
923
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
924
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
925
5.28k
            },
926
            // for each 4x4
927
5.28k
            {
928
5.28k
                &object_ptr->cr_dc_sign_level_coeff_na[tile_idx],
929
5.28k
                na_max_pic_w,
930
5.28k
                na_max_pic_h,
931
5.28k
                sizeof(uint8_t),
932
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
933
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
934
5.28k
            },
935
            // for each 4x4
936
5.28k
            {
937
5.28k
                &object_ptr->cb_dc_sign_level_coeff_na[tile_idx],
938
5.28k
                na_max_pic_w,
939
5.28k
                na_max_pic_h,
940
5.28k
                sizeof(uint8_t),
941
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
942
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
943
5.28k
            },
944
5.28k
            {
945
5.28k
                &object_ptr->txfm_context_array[tile_idx],
946
5.28k
                na_max_pic_w,
947
5.28k
                na_max_pic_h,
948
5.28k
                sizeof(TXFM_CONTEXT),
949
5.28k
                PU_NEIGHBOR_ARRAY_GRANULARITY,
950
5.28k
                NEIGHBOR_ARRAY_UNIT_TOP_AND_LEFT_ONLY_MASK,
951
5.28k
            },
952
5.28k
        };
953
5.28k
        return_error = create_neighbor_array_units(data0, DIM(data0));
954
5.28k
        if (return_error == EB_ErrorInsufficientResources) {
955
0
            return EB_ErrorInsufficientResources;
956
0
        }
957
958
5.28k
        if ((is_16bit) || (SVT_EFFECTIVE_IS_16BIT_PIPELINE(init_data_ptr->is_16bit_pipeline))) {
959
0
            InitData data[] = {
960
0
                {
961
0
                    &object_ptr->ep_luma_recon_na_16bit[tile_idx],
962
0
                    na_max_pic_w,
963
0
                    na_max_pic_h,
964
0
                    sizeof(uint16_t),
965
0
                    SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
966
0
                    NEIGHBOR_ARRAY_UNIT_FULL_MASK,
967
0
                },
968
0
                {
969
0
                    &object_ptr->ep_cb_recon_na_16bit[tile_idx],
970
0
                    na_max_pic_w >> subsampling_x,
971
0
                    na_max_pic_h >> subsampling_y,
972
0
                    sizeof(uint16_t),
973
0
                    SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
974
0
                    NEIGHBOR_ARRAY_UNIT_FULL_MASK,
975
0
                },
976
0
                {
977
0
                    &object_ptr->ep_cr_recon_na_16bit[tile_idx],
978
0
                    na_max_pic_w >> subsampling_x,
979
0
                    na_max_pic_h >> subsampling_y,
980
0
                    sizeof(uint16_t),
981
0
                    SAMPLE_NEIGHBOR_ARRAY_GRANULARITY,
982
0
                    NEIGHBOR_ARRAY_UNIT_FULL_MASK,
983
0
                },
984
0
            };
985
0
            return_error = create_neighbor_array_units(data, DIM(data));
986
0
            if (return_error == EB_ErrorInsufficientResources) {
987
0
                return EB_ErrorInsufficientResources;
988
0
            }
989
5.28k
        } else {
990
5.28k
            object_ptr->ep_luma_recon_na_16bit = 0;
991
5.28k
            object_ptr->ep_cb_recon_na_16bit   = 0;
992
5.28k
            object_ptr->ep_cr_recon_na_16bit   = 0;
993
5.28k
        }
994
5.28k
    }
995
    //Segmentation neighbor arrays
996
485
    EB_NEW(object_ptr->segmentation_neighbor_map,
997
485
           segmentation_map_ctor,
998
485
           init_data_ptr->picture_width,
999
485
           init_data_ptr->picture_height);
1000
    // Segments
1001
485
    object_ptr->enc_dec_coded_sb_count = 0;
1002
1003
485
    EB_MALLOC_ARRAY(object_ptr->enc_dec_segment_ctrl, total_tile_cnt);
1004
1005
5.76k
    for (tile_idx = 0; tile_idx < total_tile_cnt; tile_idx++) {
1006
5.28k
        EB_NEW(object_ptr->enc_dec_segment_ctrl[tile_idx],
1007
5.28k
               svt_aom_enc_dec_segments_ctor,
1008
5.28k
               init_data_ptr->enc_dec_segment_col,
1009
5.28k
               init_data_ptr->enc_dec_segment_row);
1010
5.28k
    }
1011
1012
    // Entropy Rows
1013
485
    EB_CREATE_MUTEX(object_ptr->entropy_coding_pic_mutex);
1014
1015
485
    EB_CREATE_MUTEX(object_ptr->intra_mutex);
1016
1017
485
    EB_CREATE_MUTEX(object_ptr->cdef_search_mutex);
1018
1019
485
    EB_MALLOC_ARRAY(object_ptr->mse_seg[0], object_ptr->b64_total_count);
1020
485
    EB_MALLOC_ARRAY(object_ptr->mse_seg[1], object_ptr->b64_total_count);
1021
485
    EB_MALLOC_ARRAY(object_ptr->skip_cdef_seg, object_ptr->b64_total_count);
1022
485
    EB_MALLOC_ARRAY(object_ptr->cdef_dir_data, object_ptr->b64_total_count);
1023
485
    EB_MALLOC_ARRAY(object_ptr->cdef_fb_list, object_ptr->b64_total_count);
1024
485
    EB_MALLOC_ARRAY(object_ptr->cdef_sb_index, object_ptr->b64_total_count);
1025
485
    EB_MALLOC_ARRAY(object_ptr->cdef_mse_ptr[0], object_ptr->b64_total_count);
1026
485
    EB_MALLOC_ARRAY(object_ptr->cdef_mse_ptr[1], object_ptr->b64_total_count);
1027
485
    EB_CREATE_MUTEX(object_ptr->rest_search_mutex);
1028
1029
    //the granularity is 4x4
1030
485
    EB_MALLOC_ARRAY(object_ptr->mi_grid_base,
1031
485
                    all_sb * (init_data_ptr->sb_size >> MI_SIZE_LOG2) * (init_data_ptr->sb_size >> MI_SIZE_LOG2));
1032
1033
    // If NSQ is allowed, then may need a 4x4 MI grid because 8x8 NSQ shapes will require 4x4 granularity
1034
485
    bool disallow_4x4 = true;
1035
485
    bool disallow_8x8 = true;
1036
2.91k
    for (uint8_t coeff_lvl = 0; coeff_lvl <= HIGH_LVL + 1; coeff_lvl++) {
1037
2.42k
        if (!disallow_4x4 && !disallow_8x8) {
1038
0
            break;
1039
0
        }
1040
2.42k
        uint8_t nsq_geom_lvl = allintra ? svt_aom_get_nsq_geom_level_allintra(init_data_ptr->enc_mode)
1041
2.42k
            : rtc_tune                  ? svt_aom_get_nsq_geom_level_rtc()
1042
0
                                        : svt_aom_get_nsq_geom_level_default(init_data_ptr->enc_mode, coeff_lvl);
1043
        // nsq_geom_lvl level 0 means NSQ shapes are disallowed so don't adjust based on the level
1044
2.42k
        if (nsq_geom_lvl) {
1045
0
            uint8_t allow_HVA_HVB, allow_HV4, min_nsq_bsize;
1046
0
            svt_aom_set_nsq_geom_ctrls(NULL, nsq_geom_lvl, &allow_HVA_HVB, &allow_HV4, &min_nsq_bsize);
1047
0
            if (min_nsq_bsize < 8 || (min_nsq_bsize < 16 && allow_HV4)) {
1048
0
                disallow_4x4 = false;
1049
0
            }
1050
0
            if (min_nsq_bsize < 16 || (min_nsq_bsize < 32 && allow_HV4)) {
1051
0
                disallow_8x8 = false;
1052
0
            }
1053
0
        }
1054
2.42k
    }
1055
1056
485
    disallow_4x4 = allintra ? MIN(disallow_4x4, svt_aom_get_disallow_4x4_allintra(init_data_ptr->enc_mode))
1057
485
        : rtc_tune          ? MIN(disallow_4x4, svt_aom_get_disallow_4x4_rtc())
1058
0
                            : MIN(disallow_4x4, svt_aom_get_disallow_4x4_default(init_data_ptr->enc_mode));
1059
1060
485
    object_ptr->disallow_4x4_all_frames = disallow_4x4;
1061
485
    disallow_8x8                        = allintra ? MIN(disallow_8x8, svt_aom_get_disallow_8x8_allintra())
1062
485
                               : rtc_tune          ? MIN(disallow_8x8,
1063
0
                         svt_aom_get_disallow_8x8_rtc(
1064
0
                             init_data_ptr->enc_mode, init_data_ptr->picture_width, init_data_ptr->picture_height))
1065
0
                                                   : MIN(disallow_8x8, svt_aom_get_disallow_8x8_default());
1066
485
    object_ptr->disallow_8x8_all_frames = disallow_8x8;
1067
    /* If 4x4 blocks are disallowed for all frames, the the MI blocks only need to be allocated for
1068
    8x8 blocks.  The mi_grid will still be 4x4 so that the data can be accessed the same way throughout
1069
    the code. */
1070
485
    EB_MALLOC_ARRAY(object_ptr->mip,
1071
485
                    all_sb * (init_data_ptr->sb_size >> (MI_SIZE_LOG2 + disallow_4x4 + disallow_8x8)) *
1072
485
                        (init_data_ptr->sb_size >> (MI_SIZE_LOG2 + disallow_4x4 + disallow_8x8)));
1073
1074
485
    memset(object_ptr->mip,
1075
485
           0,
1076
485
           sizeof(MbModeInfo) * all_sb * (init_data_ptr->sb_size >> (MI_SIZE_LOG2 + disallow_4x4 + disallow_8x8)) *
1077
485
               (init_data_ptr->sb_size >> (MI_SIZE_LOG2 + disallow_4x4 + disallow_8x8)));
1078
1079
485
    uint32_t mi_stride = picture_sb_w * (init_data_ptr->sb_size >> MI_SIZE_LOG2);
1080
27.5k
    for (uint32_t mi_h = 0; mi_h < picture_sb_h * (init_data_ptr->sb_size >> MI_SIZE_LOG2); mi_h++) {
1081
1.71M
        for (uint32_t mi_w = 0; mi_w < picture_sb_w * (init_data_ptr->sb_size >> MI_SIZE_LOG2); mi_w++) {
1082
1.68M
            uint32_t mi_grid_idx = mi_h * mi_stride + mi_w;
1083
1.68M
            uint32_t mip_idx = (mi_h >> (disallow_4x4 + disallow_8x8)) * (mi_stride >> (disallow_4x4 + disallow_8x8)) +
1084
1.68M
                (mi_w >> (disallow_4x4 + disallow_8x8));
1085
1.68M
            object_ptr->mi_grid_base[mi_grid_idx] = object_ptr->mip + mip_idx;
1086
1.68M
        }
1087
27.1k
    }
1088
485
    object_ptr->mi_stride = picture_sb_w * (init_data_ptr->sb_size >> MI_SIZE_LOG2);
1089
485
    if (init_data_ptr->mfmv) {
1090
        //MFMV: map is 8x8 based.
1091
485
        uint32_t  mi_rows  = init_data_ptr->picture_height >> MI_SIZE_LOG2;
1092
485
        const int mem_size = ((mi_rows + MAX_MIB_SIZE) >> 1) * (object_ptr->mi_stride >> 1);
1093
1094
485
        EB_CALLOC_ALIGNED_ARRAY(object_ptr->tpl_mvs, mem_size);
1095
485
    }
1096
1097
485
    return EB_ErrorNone;
1098
485
}
1099
1100
485
EbErrorType svt_aom_recon_coef_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1101
485
    EncDecSet* obj;
1102
1103
485
    *object_dbl_ptr = NULL;
1104
485
    EB_NEW(obj, recon_coef_ctor, object_init_data_ptr);
1105
485
    *object_dbl_ptr = obj;
1106
1107
485
    return EB_ErrorNone;
1108
485
}
1109
1110
485
EbErrorType svt_aom_picture_control_set_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1111
485
    PictureControlSet* obj;
1112
1113
485
    *object_dbl_ptr = NULL;
1114
485
    EB_NEW(obj, picture_control_set_ctor, object_init_data_ptr);
1115
485
    *object_dbl_ptr = obj;
1116
1117
485
    return EB_ErrorNone;
1118
485
}
1119
1120
1.94k
static void picture_parent_control_set_dctor(EbPtr ptr) {
1121
1.94k
    PictureParentControlSet* obj = (PictureParentControlSet*)ptr;
1122
1123
1.94k
    if (obj->is_chroma_downsampled_picture_ptr_owner) {
1124
0
        EB_DELETE(obj->chroma_downsampled_pic);
1125
0
    }
1126
1127
1.94k
    if (obj->variance) {
1128
1.94k
        EB_FREE_2D(obj->variance);
1129
1.94k
    }
1130
1131
1.94k
    if (obj->picture_histogram) {
1132
0
        for (int region_in_picture_width_index = 0; region_in_picture_width_index < MAX_NUMBER_OF_REGIONS_IN_WIDTH;
1133
0
             region_in_picture_width_index++) {
1134
0
            if (obj->picture_histogram[region_in_picture_width_index]) {
1135
0
                for (int region_in_picture_height_index = 0;
1136
0
                     region_in_picture_height_index < MAX_NUMBER_OF_REGIONS_IN_HEIGHT;
1137
0
                     region_in_picture_height_index++) {
1138
0
                    EB_FREE_ARRAY(
1139
0
                        obj->picture_histogram[region_in_picture_width_index][region_in_picture_height_index]);
1140
0
                }
1141
0
            }
1142
0
            EB_FREE_PTR_ARRAY(obj->picture_histogram[region_in_picture_width_index], MAX_NUMBER_OF_REGIONS_IN_HEIGHT);
1143
0
        }
1144
0
        EB_FREE_PTR_ARRAY(obj->picture_histogram, MAX_NUMBER_OF_REGIONS_IN_WIDTH);
1145
0
    }
1146
1.94k
    EB_FREE_ARRAY(obj->rc_me_distortion);
1147
1.94k
    EB_FREE_ARRAY(obj->rc_me_allow_gm);
1148
1.94k
    EB_FREE_ARRAY(obj->me_64x64_distortion);
1149
1.94k
    EB_FREE_ARRAY(obj->me_32x32_distortion);
1150
1.94k
    EB_FREE_ARRAY(obj->me_16x16_distortion);
1151
1.94k
    EB_FREE_ARRAY(obj->me_8x8_distortion);
1152
1153
1.94k
    EB_FREE_ARRAY(obj->me_8x8_cost_variance);
1154
1.94k
    if (obj->av1_cm) {
1155
1.94k
        EB_FREE_ARRAY(obj->av1_cm->frame_to_show);
1156
1.94k
        if (obj->av1_cm->rst_frame.buffer_alloc_sz) {
1157
0
            EB_FREE_ARRAY(obj->av1_cm->rst_frame.buffer_alloc);
1158
0
        }
1159
1.94k
        EB_FREE_ARRAY(obj->av1_cm);
1160
1.94k
    }
1161
1162
1.94k
    EB_FREE_ARRAY(obj->av1x);
1163
1.94k
    EB_DESTROY_MUTEX(obj->me_processed_b64_mutex);
1164
1.94k
    EB_DESTROY_SEMAPHORE(obj->temp_filt_done_semaphore);
1165
1.94k
    EB_DESTROY_MUTEX(obj->temp_filt_mutex);
1166
1.94k
    EB_FREE_ARRAY(obj->tile_group_info);
1167
1.94k
    EB_DESTROY_MUTEX(obj->pa_me_done.mutex);
1168
1.94k
    if (obj->is_pcs_sb_params) {
1169
0
        svt_pcs_sb_structs_dctor(obj);
1170
0
    }
1171
1.94k
    if (obj->frame_superres_enabled || obj->frame_resize_enabled) {
1172
0
        EB_DELETE(obj->enhanced_downscaled_pic);
1173
0
    }
1174
1.94k
    EB_DESTROY_SEMAPHORE(obj->tpl_disp_done_semaphore);
1175
1.94k
    EB_DESTROY_MUTEX(obj->tpl_disp_mutex);
1176
1.94k
    uint16_t tile_cnt = 1; /*obj->tile_row_count * obj->tile_column_count;*/
1177
1.94k
    EB_DELETE_PTR_ARRAY(obj->tpl_disp_segment_ctrl, tile_cnt);
1178
1.94k
    if (obj->dg_detector) {
1179
1.94k
        EB_DELETE(obj->dg_detector);
1180
1.94k
    }
1181
1.94k
}
1182
1183
/*
1184
ppcs_update_param: update the parameters in PictureParentControlSet for changing the resolution on the fly
1185
*/
1186
0
EbErrorType ppcs_update_param(PictureParentControlSet* ppcs) {
1187
0
    EbErrorType         return_error = EB_ErrorNone;
1188
0
    SequenceControlSet* scs          = ppcs->scs;
1189
1190
0
    if (ppcs->av1_cm->color_format >= EB_YUV422) {
1191
0
        EbPictureBufferDescInitData input_pic_buf_desc_init_data;
1192
0
        input_pic_buf_desc_init_data.max_width          = scs->max_input_luma_width;
1193
0
        input_pic_buf_desc_init_data.max_height         = scs->max_input_luma_height;
1194
0
        input_pic_buf_desc_init_data.bit_depth          = 8; //Should be 8bit
1195
0
        input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_CHROMA_MASK;
1196
0
        input_pic_buf_desc_init_data.border             = scs->border;
1197
0
        input_pic_buf_desc_init_data.color_format       = EB_YUV420; //set to 420 for MD
1198
0
        input_pic_buf_desc_init_data.split_mode         = false;
1199
0
        svt_picture_buffer_desc_update(ppcs->chroma_downsampled_pic, (EbPtr)&input_pic_buf_desc_init_data);
1200
0
    }
1201
    // GOP
1202
0
    ppcs->b64_total_count   = scs->b64_total_count;
1203
0
    ppcs->av1_cm->mi_stride = scs->pic_width_in_b64 * (BLOCK_SIZE_64 / 4);
1204
1205
0
    ppcs->av1_cm->frm_size.frame_width              = scs->max_input_luma_width - scs->max_input_pad_right;
1206
0
    ppcs->av1_cm->frm_size.frame_height             = scs->max_input_luma_height - scs->max_input_pad_bottom;
1207
0
    ppcs->av1_cm->frm_size.superres_upscaled_width  = scs->max_input_luma_width - scs->max_input_pad_right;
1208
0
    ppcs->av1_cm->frm_size.superres_upscaled_height = scs->max_input_luma_height - scs->max_input_pad_bottom;
1209
0
    ppcs->av1_cm->frm_size.superres_denominator     = SCALE_NUMERATOR;
1210
0
    ppcs->av1_cm->mi_cols                           = scs->max_input_luma_width >> MI_SIZE_LOG2;
1211
0
    ppcs->av1_cm->mi_rows                           = scs->max_input_luma_height >> MI_SIZE_LOG2;
1212
1213
0
    ppcs->aligned_width  = scs->max_input_luma_width;
1214
0
    ppcs->aligned_height = scs->max_input_luma_height;
1215
0
    ppcs->frame_width    = scs->max_input_luma_width;
1216
0
    ppcs->frame_height   = scs->max_input_luma_height;
1217
0
    ppcs->render_width   = scs->max_input_luma_width;
1218
0
    ppcs->render_height  = scs->max_input_luma_height;
1219
1220
0
    return return_error;
1221
0
}
1222
1223
1.94k
static EbErrorType picture_parent_control_set_ctor(PictureParentControlSet* object_ptr, EbPtr object_init_data_ptr) {
1224
1.94k
    PictureControlSetInitData* init_data_ptr = (PictureControlSetInitData*)object_init_data_ptr;
1225
1226
1.94k
    EbErrorType    return_error  = EB_ErrorNone;
1227
1.94k
    const uint16_t subsampling_x = (init_data_ptr->color_format == EB_YUV444 ? 0 : 1);
1228
1.94k
    const uint16_t subsampling_y = (init_data_ptr->color_format >= EB_YUV422 ? 0 : 1);
1229
1230
1.94k
    object_ptr->dctor = picture_parent_control_set_dctor;
1231
1232
1.94k
    object_ptr->input_pic_wrapper       = NULL;
1233
1.94k
    object_ptr->ref_pic_wrapper         = NULL;
1234
1.94k
    object_ptr->enhanced_pic            = NULL;
1235
1.94k
    object_ptr->enhanced_downscaled_pic = NULL;
1236
1.94k
    object_ptr->enhanced_unscaled_pic   = NULL;
1237
1238
1.94k
    if (init_data_ptr->color_format >= EB_YUV422) {
1239
0
        EbPictureBufferDescInitData input_pic_buf_desc_init_data;
1240
0
        input_pic_buf_desc_init_data.max_width          = init_data_ptr->picture_width;
1241
0
        input_pic_buf_desc_init_data.max_height         = init_data_ptr->picture_height;
1242
0
        input_pic_buf_desc_init_data.bit_depth          = 8; //Should be 8bit
1243
0
        input_pic_buf_desc_init_data.buffer_enable_mask = PICTURE_BUFFER_DESC_CHROMA_MASK;
1244
0
        input_pic_buf_desc_init_data.border             = init_data_ptr->border;
1245
0
        input_pic_buf_desc_init_data.color_format       = EB_YUV420; //set to 420 for MD
1246
0
        input_pic_buf_desc_init_data.split_mode         = false;
1247
0
        EB_NEW(object_ptr->chroma_downsampled_pic, svt_picture_buffer_desc_ctor, (EbPtr)&input_pic_buf_desc_init_data);
1248
0
        object_ptr->is_chroma_downsampled_picture_ptr_owner = true;
1249
1.94k
    } else if (init_data_ptr->color_format == EB_YUV420) {
1250
1.94k
        object_ptr->chroma_downsampled_pic = NULL;
1251
1.94k
    } else {
1252
0
        return EB_ErrorBadParameter;
1253
0
    }
1254
    // GOP
1255
1.94k
    object_ptr->pred_struct_index    = 0;
1256
1.94k
    object_ptr->picture_number       = 0;
1257
1.94k
    object_ptr->idr_flag             = false;
1258
1.94k
    object_ptr->temporal_layer_index = 0;
1259
1.94k
    object_ptr->total_num_bits       = 0;
1260
1.94k
    object_ptr->last_idr_picture     = 0;
1261
1.94k
    object_ptr->is_pcs_sb_params     = false;
1262
1263
1.94k
    const uint16_t picture_b64_width = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_width, init_data_ptr->b64_size);
1264
1.94k
    const uint16_t picture_b64_height = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_height,
1265
1.94k
                                                                  init_data_ptr->b64_size);
1266
1.94k
    object_ptr->b64_total_count       = picture_b64_width * picture_b64_height;
1267
1268
1.94k
    if (init_data_ptr->calculate_variance) {
1269
1.94k
        uint8_t block_count;
1270
1.94k
        if (init_data_ptr->allintra || init_data_ptr->aq_mode == 1 || init_data_ptr->variance_octile) {
1271
1.94k
            block_count = 85;
1272
1.94k
        } else {
1273
0
            block_count = 1;
1274
0
        }
1275
1.94k
        EB_MALLOC_2D(object_ptr->variance, object_ptr->b64_total_count, block_count);
1276
1.94k
    }
1277
1.94k
    if (init_data_ptr->calc_hist) {
1278
0
        EB_ALLOC_PTR_ARRAY(object_ptr->picture_histogram, MAX_NUMBER_OF_REGIONS_IN_WIDTH);
1279
1280
0
        for (uint32_t region_in_picture_width_index = 0; region_in_picture_width_index < MAX_NUMBER_OF_REGIONS_IN_WIDTH;
1281
0
             region_in_picture_width_index++) { // loop over horizontal regions
1282
0
            EB_ALLOC_PTR_ARRAY(object_ptr->picture_histogram[region_in_picture_width_index],
1283
0
                               MAX_NUMBER_OF_REGIONS_IN_HEIGHT);
1284
0
            for (uint32_t region_in_picture_height_index = 0;
1285
0
                 region_in_picture_height_index < MAX_NUMBER_OF_REGIONS_IN_HEIGHT;
1286
0
                 region_in_picture_height_index++) {
1287
0
                EB_MALLOC_ARRAY(
1288
0
                    object_ptr->picture_histogram[region_in_picture_width_index][region_in_picture_height_index],
1289
0
                    HISTOGRAM_NUMBER_OF_BINS);
1290
0
            }
1291
0
        }
1292
0
    }
1293
1294
1.94k
    object_ptr->r0 = 0;
1295
1296
1.94k
    EB_MALLOC_ARRAY(object_ptr->rc_me_distortion, object_ptr->b64_total_count);
1297
1.94k
    EB_MALLOC_ARRAY(object_ptr->rc_me_allow_gm, object_ptr->b64_total_count);
1298
1.94k
    EB_CALLOC_ARRAY(object_ptr->me_64x64_distortion, object_ptr->b64_total_count);
1299
1.94k
    EB_MALLOC_ARRAY(object_ptr->me_32x32_distortion, object_ptr->b64_total_count);
1300
1.94k
    EB_MALLOC_ARRAY(object_ptr->me_16x16_distortion, object_ptr->b64_total_count);
1301
1.94k
    EB_MALLOC_ARRAY(object_ptr->me_8x8_distortion, object_ptr->b64_total_count);
1302
1303
1.94k
    EB_MALLOC_ARRAY(object_ptr->me_8x8_cost_variance, object_ptr->b64_total_count);
1304
    // SB noise variance array
1305
1.94k
    EB_CREATE_MUTEX(object_ptr->me_processed_b64_mutex);
1306
1.94k
    EB_CREATE_SEMAPHORE(object_ptr->temp_filt_done_semaphore, 0, 1);
1307
1.94k
    EB_CREATE_MUTEX(object_ptr->temp_filt_mutex);
1308
1.94k
    EB_MALLOC_ARRAY(object_ptr->av1_cm, 1);
1309
1310
1.94k
    EB_CREATE_MUTEX(object_ptr->pa_me_done.mutex);
1311
1312
1.94k
    EB_CREATE_SEMAPHORE(object_ptr->tpl_disp_done_semaphore, 0, 1);
1313
1.94k
    EB_CREATE_MUTEX(object_ptr->tpl_disp_mutex);
1314
1315
1.94k
    EB_MALLOC_ARRAY(object_ptr->tpl_disp_segment_ctrl, 1);
1316
3.88k
    for (uint32_t tile_idx = 0; tile_idx < 1; tile_idx++) {
1317
1.94k
        EB_NEW(object_ptr->tpl_disp_segment_ctrl[tile_idx],
1318
1.94k
               svt_aom_enc_dec_segments_ctor,
1319
1.94k
               init_data_ptr->enc_dec_segment_col,
1320
1.94k
               init_data_ptr->enc_dec_segment_row);
1321
1.94k
    }
1322
1.94k
    object_ptr->av1_cm->mi_stride = picture_b64_width * (BLOCK_SIZE_64 / 4);
1323
1324
1.94k
    EB_MALLOC_ARRAY(object_ptr->av1_cm->frame_to_show, 1);
1325
1326
1.94k
    object_ptr->av1_cm->use_highbitdepth                 = ((init_data_ptr->bit_depth > 8) ||
1327
1.94k
                                            (SVT_EFFECTIVE_IS_16BIT_PIPELINE(init_data_ptr->is_16bit_pipeline)))
1328
1.94k
                        ? 1
1329
1.94k
                        : 0;
1330
1.94k
    object_ptr->av1_cm->bit_depth                        = init_data_ptr->bit_depth;
1331
1.94k
    object_ptr->av1_cm->color_format                     = init_data_ptr->color_format;
1332
1.94k
    object_ptr->av1_cm->subsampling_x                    = subsampling_x;
1333
1.94k
    object_ptr->av1_cm->subsampling_y                    = subsampling_y;
1334
1.94k
    object_ptr->av1_cm->frm_size.frame_width             = init_data_ptr->picture_width - init_data_ptr->non_m8_pad_w;
1335
1.94k
    object_ptr->av1_cm->frm_size.frame_height            = init_data_ptr->picture_height - init_data_ptr->non_m8_pad_h;
1336
1.94k
    object_ptr->av1_cm->frm_size.superres_upscaled_width = init_data_ptr->picture_width - init_data_ptr->non_m8_pad_w;
1337
1.94k
    ;
1338
1.94k
    object_ptr->av1_cm->frm_size.superres_upscaled_height = init_data_ptr->picture_height - init_data_ptr->non_m8_pad_h;
1339
1340
1.94k
    object_ptr->av1_cm->frm_size.superres_denominator = SCALE_NUMERATOR;
1341
1342
1.94k
    object_ptr->av1_cm->mi_cols = init_data_ptr->picture_width >> MI_SIZE_LOG2;
1343
1.94k
    object_ptr->av1_cm->mi_rows = init_data_ptr->picture_height >> MI_SIZE_LOG2;
1344
1345
1.94k
    object_ptr->av1_cm->byte_alignment = 0;
1346
1.94k
    memset(&object_ptr->av1_cm->rst_frame, 0, sizeof(Yv12BufferConfig));
1347
1348
1.94k
    EB_MALLOC_ARRAY(object_ptr->av1x, 1);
1349
1350
    //Jing: need to know the tile split info at pcs initialize stage
1351
1.94k
    object_ptr->log2_tile_rows = init_data_ptr->log2_tile_rows;
1352
1.94k
    object_ptr->log2_tile_cols = init_data_ptr->log2_tile_cols;
1353
1.94k
    object_ptr->log2_sb_size   = init_data_ptr->log2_sb_size;
1354
1.94k
    svt_aom_set_tile_info(object_ptr);
1355
1.94k
    EB_MALLOC_ARRAY(object_ptr->tile_group_info,
1356
1.94k
                    (object_ptr->av1_cm->tiles_info.tile_rows * object_ptr->av1_cm->tiles_info.tile_cols));
1357
1358
1.94k
    object_ptr->frame_superres_enabled = false;
1359
1.94k
    object_ptr->aligned_width          = init_data_ptr->picture_width;
1360
1.94k
    object_ptr->aligned_height         = init_data_ptr->picture_height;
1361
1.94k
    object_ptr->frame_width            = init_data_ptr->picture_width;
1362
1.94k
    object_ptr->frame_height           = init_data_ptr->picture_height;
1363
1.94k
    object_ptr->render_width           = init_data_ptr->picture_width;
1364
1.94k
    object_ptr->render_height          = init_data_ptr->picture_height;
1365
1366
1.94k
    object_ptr->superres_denom             = SCALE_NUMERATOR;
1367
1.94k
    object_ptr->superres_total_recode_loop = 0;
1368
1.94k
    object_ptr->superres_recode_loop       = 0;
1369
1.94k
    memset(&object_ptr->superres_rdcost, 0, sizeof(object_ptr->superres_rdcost));
1370
1.94k
    memset(&object_ptr->superres_denom_array, 0, sizeof(object_ptr->superres_denom_array));
1371
1372
1.94k
    object_ptr->frame_resize_enabled = false;
1373
1.94k
    object_ptr->resize_denom         = SCALE_NUMERATOR;
1374
1375
    // Loop variables
1376
1.94k
    object_ptr->loop_count      = 0;
1377
1.94k
    object_ptr->overshoot_seen  = 0;
1378
1.94k
    object_ptr->undershoot_seen = 0;
1379
1.94k
    object_ptr->low_cr_seen     = 0;
1380
1.94k
    EB_NEW(object_ptr->dg_detector, svt_aom_dg_detector_seg_ctor);
1381
1.94k
    return return_error;
1382
1.94k
}
1383
1384
485
static void me_dctor(EbPtr p) {
1385
485
    MotionEstimationData* obj = (MotionEstimationData*)p;
1386
485
    EB_DELETE_PTR_ARRAY(obj->me_results, obj->init_b64_total_count);
1387
    // Per-SB ME pools backing the MeSbResults arrays (borrowed by each SB).
1388
485
    EB_FREE_ARRAY(obj->me_sb_mv_pool);
1389
485
    EB_FREE_ARRAY(obj->me_sb_cand_pool);
1390
485
    EB_FREE_ARRAY(obj->me_sb_totidx_pool);
1391
485
    if (obj->tpl_stats) {
1392
0
        EB_FREE_2D(obj->tpl_stats);
1393
0
    }
1394
485
    if (obj->tpl_beta) {
1395
0
        EB_FREE_ARRAY(obj->tpl_beta);
1396
0
    }
1397
485
    if (obj->tpl_rdmult_scaling_factors) {
1398
0
        EB_FREE_ARRAY(obj->tpl_rdmult_scaling_factors);
1399
0
    }
1400
485
    if (obj->tpl_sb_rdmult_scaling_factors) {
1401
0
        EB_FREE_ARRAY(obj->tpl_sb_rdmult_scaling_factors);
1402
0
    }
1403
485
    if (obj->tpl_src_stats_buffer) {
1404
0
        EB_FREE_ARRAY(obj->tpl_src_stats_buffer);
1405
0
    }
1406
485
    if (obj->ssim_rdmult_scaling_factors) {
1407
0
        EB_FREE_ARRAY(obj->ssim_rdmult_scaling_factors);
1408
0
    }
1409
485
}
1410
1411
/*
1412
me_update_param: update the parameters in MotionEstimationData for changing the resolution on the fly
1413
*/
1414
485
EbErrorType me_update_param(MotionEstimationData* me_data, SequenceControlSet* scs) {
1415
485
    EbErrorType return_error = EB_ErrorNone;
1416
485
    me_data->b64_total_count = scs->b64_total_count;
1417
1418
485
    return return_error;
1419
485
}
1420
1421
485
static EbErrorType me_ctor(MotionEstimationData* object_ptr, EbPtr object_init_data_ptr) {
1422
485
    PictureControlSetInitData* init_data_ptr = (PictureControlSetInitData*)object_init_data_ptr;
1423
1424
485
    EbErrorType return_error = EB_ErrorNone;
1425
485
    object_ptr->dctor        = me_dctor;
1426
1427
485
    const uint16_t picture_b64_width = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_width, init_data_ptr->b64_size);
1428
485
    const uint16_t picture_b64_height = (uint16_t)DIVIDE_AND_CEIL(init_data_ptr->picture_height,
1429
485
                                                                  init_data_ptr->b64_size);
1430
485
    uint32_t       sb_total_count     = picture_b64_width * picture_b64_height;
1431
485
    object_ptr->b64_total_count       = sb_total_count;
1432
485
    object_ptr->init_b64_total_count  = sb_total_count;
1433
1434
485
    if (!init_data_ptr->allintra) {
1435
0
        EB_ALLOC_PTR_ARRAY(object_ptr->me_results, sb_total_count);
1436
1437
0
        for (uint16_t sb_index = 0; sb_index < sb_total_count; ++sb_index) {
1438
0
            EB_NEW(object_ptr->me_results[sb_index],
1439
0
                   svt_aom_me_sb_results_ctor,
1440
0
                   init_data_ptr,
1441
0
                   object_ptr,
1442
0
                   sb_index,
1443
0
                   sb_total_count);
1444
0
        }
1445
0
    }
1446
485
    uint16_t adaptive_picture_width_in_mb  = (uint16_t)((init_data_ptr->picture_width + 15) / 16);
1447
485
    uint16_t adaptive_picture_height_in_mb = (uint16_t)((init_data_ptr->picture_height + 15) / 16);
1448
485
    if (init_data_ptr->static_config.tune == TUNE_SSIM || init_data_ptr->static_config.tune == TUNE_IQ ||
1449
485
        init_data_ptr->static_config.tune == TUNE_MS_SSIM) {
1450
0
        EB_MALLOC_ARRAY(object_ptr->ssim_rdmult_scaling_factors,
1451
0
                        adaptive_picture_width_in_mb * adaptive_picture_height_in_mb);
1452
485
    } else {
1453
485
        object_ptr->ssim_rdmult_scaling_factors = NULL;
1454
485
    }
1455
485
    if (init_data_ptr->enable_tpl_la) {
1456
0
        const uint16_t picture_width_in_mb  = (uint16_t)((init_data_ptr->picture_width + 15) / 16);
1457
0
        const uint16_t picture_height_in_mb = (uint16_t)((init_data_ptr->picture_height + 15) / 16);
1458
0
        if (init_data_ptr->tpl_synth_size == 8) {
1459
0
            adaptive_picture_width_in_mb  = adaptive_picture_width_in_mb << 1;
1460
0
            adaptive_picture_height_in_mb = adaptive_picture_height_in_mb << 1;
1461
0
        } else if (init_data_ptr->tpl_synth_size == 32) {
1462
0
            adaptive_picture_width_in_mb  = (uint16_t)((init_data_ptr->picture_width + 31) / 32);
1463
0
            adaptive_picture_height_in_mb = (uint16_t)((init_data_ptr->picture_height + 31) / 32);
1464
0
        }
1465
0
        EB_MALLOC_2D(
1466
0
            object_ptr->tpl_stats, (uint32_t)((adaptive_picture_width_in_mb) * (adaptive_picture_height_in_mb)), 1);
1467
0
        if (init_data_ptr->tpl_lad_mg > 0) {
1468
0
            EB_MALLOC_ARRAY(object_ptr->tpl_src_stats_buffer,
1469
0
                            (uint32_t)picture_width_in_mb * (uint32_t)picture_height_in_mb);
1470
0
        } else {
1471
0
            object_ptr->tpl_src_stats_buffer = NULL;
1472
0
        }
1473
0
        EB_MALLOC_ARRAY(object_ptr->tpl_beta, sb_total_count);
1474
0
        EB_MALLOC_ARRAY(object_ptr->tpl_rdmult_scaling_factors,
1475
0
                        adaptive_picture_width_in_mb * adaptive_picture_height_in_mb);
1476
0
        EB_MALLOC_ARRAY(object_ptr->tpl_sb_rdmult_scaling_factors,
1477
0
                        adaptive_picture_width_in_mb * adaptive_picture_height_in_mb);
1478
485
    } else {
1479
485
        object_ptr->tpl_stats                     = NULL;
1480
485
        object_ptr->tpl_beta                      = NULL;
1481
485
        object_ptr->tpl_rdmult_scaling_factors    = NULL;
1482
485
        object_ptr->tpl_sb_rdmult_scaling_factors = NULL;
1483
485
        object_ptr->tpl_src_stats_buffer          = NULL;
1484
485
    }
1485
485
    return return_error;
1486
485
}
1487
1488
485
EbErrorType b64_geom_init(SequenceControlSet* scs, uint16_t width, uint16_t height, B64Geom** b64_geoms) {
1489
485
    EbErrorType return_error = EB_ErrorNone;
1490
1491
485
    uint8_t  b64_size           = scs->b64_size;
1492
485
    uint16_t picture_b64_width  = DIVIDE_AND_CEIL(width, b64_size);
1493
485
    uint16_t picture_b64_height = DIVIDE_AND_CEIL(height, b64_size);
1494
1495
485
    EB_FREE_ARRAY(*b64_geoms);
1496
485
    EB_MALLOC_ARRAY(*b64_geoms, picture_b64_width * picture_b64_height);
1497
1498
7.07k
    for (int b64_idx = 0; b64_idx < picture_b64_width * picture_b64_height; ++b64_idx) {
1499
6.59k
        B64Geom* b64_geom         = &(*b64_geoms)[b64_idx];
1500
6.59k
        uint16_t horizontal_index = (uint16_t)(b64_idx % picture_b64_width);
1501
6.59k
        uint16_t vertical_index   = (uint16_t)(b64_idx / picture_b64_width);
1502
6.59k
        b64_geom->org_x           = horizontal_index * b64_size;
1503
6.59k
        b64_geom->org_y           = vertical_index * b64_size;
1504
6.59k
        b64_geom->width           = (uint8_t)MIN(width - b64_geom->org_x, b64_size);
1505
6.59k
        b64_geom->height          = (uint8_t)MIN(height - b64_geom->org_y, b64_size);
1506
6.59k
        b64_geom->is_complete_b64 = (b64_geom->width == b64_size && b64_geom->height == b64_size) ? 1 : 0;
1507
6.59k
    }
1508
1509
485
    return return_error;
1510
485
}
1511
1512
970
EbErrorType alloc_sb_geoms(SbGeom** geom, int width, int height) {
1513
970
    SbGeom* tmp;
1514
970
    EB_MALLOC_ARRAY(tmp, width * height);
1515
970
    *geom = tmp;
1516
1517
970
    return EB_ErrorNone;
1518
970
}
1519
1520
5.82k
void free_sb_geoms(SbGeom* geom) {
1521
5.82k
    if (geom) {
1522
970
        EB_FREE_ARRAY(geom);
1523
970
    }
1524
5.82k
}
1525
1526
485
void copy_sb_geoms(SbGeom* dst_geom, SbGeom* src_geom, uint16_t width, uint16_t height) {
1527
7.07k
    for (int i = 0; i < width * height; i++) {
1528
6.59k
        dst_geom[i] = src_geom[i];
1529
6.59k
    }
1530
485
}
1531
1532
485
EbErrorType sb_geom_init(SequenceControlSet* scs, uint16_t width, uint16_t height, SbGeom** sb_geoms) {
1533
485
    uint16_t picture_sb_width  = DIVIDE_AND_CEIL(width, scs->sb_size);
1534
485
    uint16_t picture_sb_height = DIVIDE_AND_CEIL(height, scs->sb_size);
1535
485
    free_sb_geoms(*sb_geoms);
1536
485
    EbErrorType ret = alloc_sb_geoms(sb_geoms, picture_sb_width, picture_sb_height);
1537
485
    if (ret != EB_ErrorNone) {
1538
0
        return ret;
1539
0
    }
1540
1541
7.07k
    for (int sb_index = 0; sb_index < picture_sb_width * picture_sb_height; ++sb_index) {
1542
6.59k
        SbGeom*  sb_geom   = &(*sb_geoms)[sb_index];
1543
6.59k
        uint16_t hor_index = sb_index % picture_sb_width;
1544
6.59k
        uint16_t ver_index = sb_index / picture_sb_width;
1545
6.59k
        sb_geom->org_x     = hor_index * scs->sb_size;
1546
6.59k
        sb_geom->org_y     = ver_index * scs->sb_size;
1547
6.59k
        sb_geom->width     = (uint8_t)MIN(width - sb_geom->org_x, scs->sb_size);
1548
6.59k
        sb_geom->height    = (uint8_t)MIN(height - sb_geom->org_y, scs->sb_size);
1549
6.59k
    }
1550
1551
485
    return EB_ErrorNone;
1552
485
}
1553
1554
1.94k
EbErrorType svt_aom_picture_parent_control_set_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1555
1.94k
    PictureParentControlSet* obj;
1556
1557
1.94k
    *object_dbl_ptr = NULL;
1558
1.94k
    EB_NEW(obj, picture_parent_control_set_ctor, object_init_data_ptr);
1559
1.94k
    *object_dbl_ptr = obj;
1560
1561
1.94k
    return EB_ErrorNone;
1562
1.94k
}
1563
1564
485
EbErrorType svt_aom_me_creator(EbPtr* object_dbl_ptr, EbPtr object_init_data_ptr) {
1565
485
    MotionEstimationData* obj;
1566
1567
485
    *object_dbl_ptr = NULL;
1568
485
    EB_NEW(obj, me_ctor, object_init_data_ptr);
1569
485
    *object_dbl_ptr = obj;
1570
1571
485
    return EB_ErrorNone;
1572
485
}