1 // clang-format off
2 /*
3 * Copyright(c) 2019 Netflix, Inc.
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 // SUMMARY
14 //   Contains the Decoder Memory Init functions
15 
16 /**************************************
17  * Includes
18  **************************************/
19 #include <stdlib.h>
20 
21 #include "EbDefinitions.h"
22 #include "EbPictureBufferDesc.h"
23 
24 #include "EbSvtAv1Dec.h"
25 #include "EbDecHandle.h"
26 #include "EbDecProcessFrame.h"
27 
28 #include "EbObuParse.h"
29 #include "EbDecParseFrame.h"
30 
31 #include "EbDecMemInit.h"
32 #include "EbDecInverseQuantize.h"
33 
34 #include "EbDecPicMgr.h"
35 #include "EbDecLF.h"
36 
37 #include "EbUtility.h"
38 
39 /*TODO: Remove and harmonize with encoder. Globals prevent harmonization now! */
40 /*****************************************
41  * svt_recon_picture_buffer_desc_ctor
42  *  Initializes the Buffer Descriptor's
43  *  values that are fixed for the life of
44  *  the descriptor.
45  *****************************************/
dec_eb_recon_picture_buffer_desc_ctor(EbPtr * object_dbl_ptr,EbPtr object_init_data_ptr,EbBool is_16bit_pipeline)46 EbErrorType dec_eb_recon_picture_buffer_desc_ctor(
47     EbPtr  *object_dbl_ptr,
48     EbPtr   object_init_data_ptr,
49     EbBool is_16bit_pipeline /* can be removed as an extra argument once
50                                 EbPictureBufferDescInitData adds the support for this */
51 )
52 {
53     EbPictureBufferDesc          *picture_buffer_desc_ptr;
54     EbPictureBufferDescInitData  *picture_buffer_desc_init_data_ptr = (EbPictureBufferDescInitData*)object_init_data_ptr;
55 
56     EB_MALLOC_DEC(EbPictureBufferDesc*, picture_buffer_desc_ptr, sizeof(EbPictureBufferDesc), EB_N_PTR);
57 
58     uint32_t bytes_per_pixel = (picture_buffer_desc_init_data_ptr->bit_depth > EB_8BIT ||
59         is_16bit_pipeline) ? 2 : 1;
60     picture_buffer_desc_ptr->is_16bit_pipeline = is_16bit_pipeline;
61 
62     // Allocate the PictureBufferDesc Object
63     *object_dbl_ptr = (EbPtr)picture_buffer_desc_ptr;
64 
65     // Set the Picture Buffer Static variables
66     picture_buffer_desc_ptr->max_width = picture_buffer_desc_init_data_ptr->max_width;
67     picture_buffer_desc_ptr->max_height = picture_buffer_desc_init_data_ptr->max_height;
68     picture_buffer_desc_ptr->width = picture_buffer_desc_init_data_ptr->max_width;
69     picture_buffer_desc_ptr->height = picture_buffer_desc_init_data_ptr->max_height;
70     picture_buffer_desc_ptr->bit_depth = picture_buffer_desc_init_data_ptr->bit_depth;
71     picture_buffer_desc_ptr->color_format = picture_buffer_desc_init_data_ptr->color_format;
72     picture_buffer_desc_ptr->stride_y = picture_buffer_desc_init_data_ptr->max_width +
73         picture_buffer_desc_init_data_ptr->left_padding + picture_buffer_desc_init_data_ptr->right_padding;
74     uint32_t height_y = (picture_buffer_desc_init_data_ptr->max_height + picture_buffer_desc_init_data_ptr->top_padding
75         + picture_buffer_desc_init_data_ptr->bot_padding);
76 
77     picture_buffer_desc_ptr->origin_x = picture_buffer_desc_init_data_ptr->left_padding;
78     picture_buffer_desc_ptr->origin_y = picture_buffer_desc_init_data_ptr->top_padding;
79     picture_buffer_desc_ptr->origin_bot_y = picture_buffer_desc_init_data_ptr->bot_padding;
80 
81     picture_buffer_desc_ptr->luma_size = (picture_buffer_desc_ptr->stride_y) * height_y;
82 
83     uint32_t stride_c = 0, height_c = 0;
84     if (picture_buffer_desc_ptr->color_format == EB_YUV420) {// 420
85         stride_c = (picture_buffer_desc_ptr->stride_y + 1) >> 1;
86         height_c = (height_y + 1) >> 1;
87     }
88     else if (picture_buffer_desc_ptr->color_format == EB_YUV422) {// 422
89         stride_c = (picture_buffer_desc_ptr->stride_y + 1) >> 1;
90         height_c = height_y;
91     }
92     else if (picture_buffer_desc_ptr->color_format == EB_YUV444) {// 444
93         stride_c = picture_buffer_desc_ptr->stride_y;
94         height_c = height_y;
95     }
96     picture_buffer_desc_ptr->stride_cb = picture_buffer_desc_ptr->stride_cr = stride_c;
97     picture_buffer_desc_ptr->chroma_size = (stride_c * height_c);
98 
99     picture_buffer_desc_ptr->packed_flag = EB_FALSE;
100 
101     picture_buffer_desc_ptr->stride_bit_inc_y = 0;
102     picture_buffer_desc_ptr->stride_bit_inc_cb = 0;
103     picture_buffer_desc_ptr->stride_bit_inc_cr = 0;
104 
105     // Allocate the Picture Buffers (luma & chroma)
106     if (picture_buffer_desc_init_data_ptr->buffer_enable_mask & PICTURE_BUFFER_DESC_Y_FLAG) {
107         EB_ALLIGN_MALLOC_DEC(EbByte, picture_buffer_desc_ptr->buffer_y, picture_buffer_desc_ptr->luma_size * bytes_per_pixel, EB_A_PTR);
108         memset(picture_buffer_desc_ptr->buffer_y, 0, picture_buffer_desc_ptr->luma_size      * bytes_per_pixel);
109     }
110     else
111         picture_buffer_desc_ptr->buffer_y = 0;
112     if (picture_buffer_desc_init_data_ptr->buffer_enable_mask & PICTURE_BUFFER_DESC_Cb_FLAG) {
113         EB_ALLIGN_MALLOC_DEC(EbByte, picture_buffer_desc_ptr->buffer_cb, picture_buffer_desc_ptr->chroma_size * bytes_per_pixel, EB_A_PTR);
114         memset(picture_buffer_desc_ptr->buffer_cb, 0, picture_buffer_desc_ptr->chroma_size      * bytes_per_pixel);
115     }
116     else
117         picture_buffer_desc_ptr->buffer_cb = 0;
118     if (picture_buffer_desc_init_data_ptr->buffer_enable_mask & PICTURE_BUFFER_DESC_Cr_FLAG) {
119         EB_ALLIGN_MALLOC_DEC(EbByte, picture_buffer_desc_ptr->buffer_cr, picture_buffer_desc_ptr->chroma_size * bytes_per_pixel, EB_A_PTR);
120         memset(picture_buffer_desc_ptr->buffer_cr, 0, picture_buffer_desc_ptr->chroma_size      * bytes_per_pixel);
121     }
122     else
123         picture_buffer_desc_ptr->buffer_cr = 0;
124     return EB_ErrorNone;
125 }
126 
127 /**********************************
128 * Main Frame Buffer containing all frame level bufs like ModeInfo
129 for all the frames in parallel
130 **********************************/
131 
init_main_frame_ctxt(EbDecHandle * dec_handle_ptr)132 static EbErrorType init_main_frame_ctxt(EbDecHandle  *dec_handle_ptr) {
133     EbErrorType return_error = EB_ErrorNone;
134 
135     int32_t i, num_sb;
136     CurFrameBuf *cur_frame_buf;
137     MainFrameBuf  *main_frame_buf = &dec_handle_ptr->main_frame_buf;
138     SeqHeader   *seq_header = &dec_handle_ptr->seq_header;
139 
140     EbBool is_st = dec_handle_ptr->dec_config.threads == 1 ? EB_TRUE : EB_FALSE;
141     ///* 8x8 alignment for various tools like CDEF */
142     //int32_t aligned_width   = ALIGN_POWER_OF_TWO(seq_header->max_frame_width, 3);
143     //int32_t aligned_height  = ALIGN_POWER_OF_TWO(seq_header->max_frame_height, 3);
144     /*int32_t mi_cols = aligned_width >> MI_SIZE_LOG2;
145     int32_t mi_rows = aligned_height >> MI_SIZE_LOG2;*/
146 
147     int32_t sb_size_log2 = seq_header->sb_size_log2;
148     int32_t sb_aligned_width = ALIGN_POWER_OF_TWO(seq_header->max_frame_width,
149                                 sb_size_log2);
150     int32_t sb_aligned_height = ALIGN_POWER_OF_TWO(seq_header->max_frame_height,
151                                 sb_size_log2);
152     int32_t sb_cols = sb_aligned_width >> sb_size_log2;
153     int32_t sb_rows = sb_aligned_height >> sb_size_log2;
154 
155     num_sb = sb_cols * sb_rows;
156 
157     int32_t num_mis_in_sb = (1 << (sb_size_log2 - MI_SIZE_LOG2)) * (1 << (sb_size_log2 - MI_SIZE_LOG2));
158 
159     main_frame_buf->num_mis_in_sb = num_mis_in_sb;
160 
161     //main_frame_buf->mi_cols = mi_cols;
162     //main_frame_buf->mi_rows = mi_rows;
163 
164     main_frame_buf->sb_cols = sb_cols;
165     main_frame_buf->sb_rows = sb_rows;
166 
167     for (i = 0; i < dec_handle_ptr->num_frms_prll; i++) {
168         cur_frame_buf = &main_frame_buf->cur_frame_bufs[i];
169 
170         /* SuperBlock str allocation at SB level */
171         EB_MALLOC_DEC(SBInfo*, cur_frame_buf->sb_info,
172             (num_sb * sizeof(SBInfo)), EB_N_PTR);
173 
174         /* ModeInfo str allocation at 4x4 level */
175         EB_MALLOC_DEC(BlockModeInfo*, cur_frame_buf->mode_info,
176                     (num_sb * num_mis_in_sb * sizeof(BlockModeInfo)), EB_N_PTR);
177 
178         /* TransformInfo str allocation at 4x4 level
179            TO-DO optimize memory based on the chroma subsampling.*/
180         EB_MALLOC_DEC(TransformInfo_t*, cur_frame_buf->trans_info[AOM_PLANE_Y],
181             (num_sb * num_mis_in_sb * sizeof(TransformInfo_t)), EB_N_PTR);
182 
183         /* Coeff buf (1D compact) allocation for entire frame
184             TODO: Should reduce this to save memory and
185             dynammically allocate if needed */
186             /*TODO : Change to macro */
187             /* (16+1) : 1 for Length and 16 for all coeffs in 4x4 */
188         if (is_st) {
189             /*Size of coeff buf reduced to sb_sizesss*/
190             EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_Y],
191                 (num_mis_in_sb * sizeof(int32_t) * (16 + 1)), EB_N_PTR);
192         }
193         else {
194             EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_Y],
195                 (num_sb * num_mis_in_sb * sizeof(int32_t) * (16 + 1)), EB_N_PTR);
196         }
197 
198         /*TODO : Change to macro */
199         EB_MALLOC_DEC(TransformInfo_t*, cur_frame_buf->trans_info[AOM_PLANE_U],
200             (num_sb * num_mis_in_sb * sizeof(TransformInfo_t) * 2), EB_N_PTR);
201 
202         /* Coeff buf (1D compact) allocation for entire frame
203         TODO: Should reduce this to save memory and
204         dynammically allocate if needed */
205         /*TODO : Change to macro */
206         /* (16+1) : 1 for Length and 16 for all coeffs in 4x4 */
207         if (seq_header->color_config.subsampling_x == 1 &&
208             seq_header->color_config.subsampling_y == 1) // 420
209         {
210             if (is_st) {
211                 EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_U],
212                     (num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 2), EB_N_PTR);
213                 EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_V],
214                     (num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 2), EB_N_PTR);
215             }
216             else {
217                 EB_MALLOC_DEC(int32_t*,
218                     cur_frame_buf->coeff[AOM_PLANE_U],
219                     (num_sb * num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 2),
220                     EB_N_PTR);
221                 EB_MALLOC_DEC(int32_t*,
222                     cur_frame_buf->coeff[AOM_PLANE_V],
223                     (num_sb * num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 2),
224                     EB_N_PTR);
225             }
226         }
227         else if (seq_header->color_config.subsampling_x == 1 &&
228                  seq_header->color_config.subsampling_y == 0) // 422
229         {
230             if (is_st) {
231                 EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_U],
232                     (num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 1), EB_N_PTR);
233                 EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_V],
234                     (num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 1), EB_N_PTR);
235             }
236             else {
237                 EB_MALLOC_DEC(int32_t*,
238                     cur_frame_buf->coeff[AOM_PLANE_U],
239                     (num_sb * num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 1),
240                     EB_N_PTR);
241                 EB_MALLOC_DEC(int32_t*,
242                     cur_frame_buf->coeff[AOM_PLANE_V],
243                     (num_sb * num_mis_in_sb * sizeof(int32_t) * (16 + 1) >> 1),
244                     EB_N_PTR);
245             }
246         }
247         else if (seq_header->color_config.subsampling_x == 0 &&
248                  seq_header->color_config.subsampling_y == 0) // 444
249         {
250             if (is_st) {
251                 EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_U],
252                     (num_mis_in_sb * sizeof(int32_t) * (16 + 1)), EB_N_PTR);
253                 EB_MALLOC_DEC(int32_t*, cur_frame_buf->coeff[AOM_PLANE_V],
254                     (num_mis_in_sb * sizeof(int32_t) * (16 + 1)), EB_N_PTR);
255             }
256             else {
257                 EB_MALLOC_DEC(int32_t*,
258                     cur_frame_buf->coeff[AOM_PLANE_U],
259                     (num_sb * num_mis_in_sb * sizeof(int32_t) * (16 + 1)),
260                     EB_N_PTR);
261                 EB_MALLOC_DEC(int32_t*,
262                     cur_frame_buf->coeff[AOM_PLANE_V],
263                     (num_sb * num_mis_in_sb * sizeof(int32_t) * (16 + 1)),
264                     EB_N_PTR);
265             }
266         }
267         else
268             assert(0);
269 
270         /* delta_q allocation at SB level */
271         EB_MALLOC_DEC(int32_t*, cur_frame_buf->delta_q,
272             (num_sb * sizeof(int32_t)), EB_N_PTR);
273 
274         /* cdef_strength allocation at SB level */
275         EB_MALLOC_DEC(int8_t*, cur_frame_buf->cdef_strength,
276             (num_sb * (seq_header->use_128x128_superblock ? 4 : 1) *
277             sizeof(int8_t)), EB_N_PTR);
278         memset(cur_frame_buf->cdef_strength, -1, (num_sb *
279             (seq_header->use_128x128_superblock ? 4 : 1) *
280             sizeof(int8_t)));
281 
282         /* delta_lf allocation at SB level */
283         EB_MALLOC_DEC(int32_t*, cur_frame_buf->delta_lf,
284             (num_sb * FRAME_LF_COUNT * sizeof(int32_t)), EB_N_PTR);
285 
286         /* tile map allocation at SB level */
287         EB_MALLOC_DEC(uint8_t*, cur_frame_buf->tile_map_sb,
288             (num_sb * sizeof(uint8_t)), EB_N_PTR);
289 
290         // Allocating lr_unit based on SB_SIZE as worst case memory.
291         // rest_unit_size cannot be less than SB_size.
292         // if rest_unit_size > SB_size then holes are introduced in-between and
293         // accessing will skip few SB in-between.
294         // if rest_unit_size == SB_size then it's straight forward to access
295         // every SB level loop restoration filter value.
296         LrCtxt *lr_ctxt = (LrCtxt *)dec_handle_ptr->pv_lr_ctxt;
297         for (int32_t plane = 0; plane <= AOM_PLANE_V; plane++) {
298             EB_MALLOC_DEC(RestorationUnitInfo *, cur_frame_buf->lr_unit[plane],
299                 (num_sb * sizeof(RestorationUnitInfo)), EB_N_PTR);
300             lr_ctxt->lr_unit[plane] = cur_frame_buf->lr_unit[plane];
301             lr_ctxt->lr_stride[plane] = sb_cols;
302         }
303     }
304     FrameMiMap *frame_mi_map = &main_frame_buf->frame_mi_map;
305     frame_mi_map->sb_cols = sb_cols;
306     frame_mi_map->sb_rows = sb_rows;
307     frame_mi_map->mi_cols_algnsb = sb_cols * (1 << (sb_size_log2 - MI_SIZE_LOG2));
308     frame_mi_map->mi_rows_algnsb = sb_cols * (1 << (sb_size_log2 - MI_SIZE_LOG2));
309     /* SBInfo pointers for entire frame */
310     EB_MALLOC_DEC(SBInfo**, frame_mi_map->pps_sb_info,
311         sb_rows * sb_cols * sizeof(SBInfo *), EB_N_PTR);
312     /* ModeInfo offset wrt it's SB start for entire frame at 4x4 lvl */
313     EB_MALLOC_DEC(uint16_t*, frame_mi_map->p_mi_offset, frame_mi_map->
314     mi_rows_algnsb * frame_mi_map->mi_cols_algnsb * sizeof(uint16_t), EB_N_PTR);
315     frame_mi_map->sb_size_log2 = sb_size_log2;
316     frame_mi_map->num_mis_in_sb_wd = (1 << (sb_size_log2 - MI_SIZE_LOG2));
317 
318 
319     main_frame_buf->tpl_mvs = NULL;
320     main_frame_buf->tpl_mvs_size = 0;
321 
322     return return_error;
323 }
324 
325 /*TODO: Move to module files */
init_parse_context(EbDecHandle * dec_handle_ptr)326 static EbErrorType init_parse_context (EbDecHandle  *dec_handle_ptr) {
327     EbErrorType return_error = EB_ErrorNone;
328 
329     EB_MALLOC_DEC(void *, dec_handle_ptr->pv_main_parse_ctxt,
330         sizeof(MainParseCtxt), EB_N_PTR);
331     MainParseCtxt *main_parse_ctx =
332         (MainParseCtxt*)dec_handle_ptr->pv_main_parse_ctxt;
333 
334     main_parse_ctx->context_count = 0;
335     main_parse_ctx->tile_parse_ctxt = NULL;
336 
337     main_parse_ctx->parse_above_nbr4x4_ctxt = NULL;
338     main_parse_ctx->parse_left_nbr4x4_ctxt = NULL;
339 
340     main_parse_ctx->num_tiles = 0;
341     main_parse_ctx->parse_tile_data = NULL;
342 
343     return return_error;
344 }
345 
346 /*TODO: Move to module files */
init_dec_mod_ctxt(EbDecHandle * dec_handle_ptr,void ** pp_dec_mod_ctxt)347 EbErrorType init_dec_mod_ctxt(EbDecHandle  *dec_handle_ptr,
348     void **pp_dec_mod_ctxt)
349 {
350     EbErrorType return_error = EB_ErrorNone;
351     SeqHeader *seq_header = &dec_handle_ptr->seq_header;
352     EbColorConfig *color_config = &seq_header->color_config;
353 
354     EB_MALLOC_DEC(void *, *pp_dec_mod_ctxt, sizeof(DecModCtxt), EB_N_PTR);
355 
356     DecModCtxt *p_dec_mod_ctxt = (DecModCtxt*)*pp_dec_mod_ctxt;
357     p_dec_mod_ctxt->dec_handle_ptr  = (void *)dec_handle_ptr;
358     p_dec_mod_ctxt->seq_header      = &dec_handle_ptr->seq_header;
359     p_dec_mod_ctxt->frame_header    = &dec_handle_ptr->frame_header;
360 
361     int32_t sb_size_log2 = seq_header->sb_size_log2;
362 
363     int32_t y_size = (1 << sb_size_log2) * (1 << sb_size_log2);
364     int32_t iq_size = y_size +
365         (color_config->subsampling_x ? y_size >> 2 : y_size) +
366         (color_config->subsampling_y ? y_size >> 2 : y_size);
367 
368     EB_MALLOC_DEC(int32_t*, p_dec_mod_ctxt->sb_iquant_ptr,
369         iq_size * sizeof(int32_t), EB_N_PTR);
370     av1_inverse_qm_init(p_dec_mod_ctxt, seq_header);
371 
372     EbColorConfig *cc = &dec_handle_ptr->seq_header.color_config;
373     uint32_t use_highbd = (cc->bit_depth > EB_8BIT ||
374         dec_handle_ptr->is_16bit_pipeline);
375     int32_t sb_size = 1 << sb_size_log2;
376     uint16_t *hbd_mc_buf[2];
377     for (int ref = 0; ref < 2; ref++) {
378 
379         //EB_MALLOC_DEC(uint8_t**, part_info->mc_buf[ref],
380         //    sizeof(uint8_t*), EB_N_PTR);
381         if (use_highbd) {
382             EB_MALLOC_DEC(uint16_t*, hbd_mc_buf[ref],
383                 ((2 * sb_size) + (AOM_INTERP_EXTEND * 2))*
384                 ((2 * sb_size) + (AOM_INTERP_EXTEND * 2))*
385                 sizeof(uint16_t), EB_N_PTR);
386             p_dec_mod_ctxt->mc_buf[ref] = (uint8_t *)hbd_mc_buf[ref];
387         }
388         else {
389             EB_MALLOC_DEC(uint8_t*, p_dec_mod_ctxt->mc_buf[ref],
390                 ((2 * sb_size) + (AOM_INTERP_EXTEND * 2))*
391                 ((2 * sb_size) + (AOM_INTERP_EXTEND * 2))*
392                 sizeof(uint8_t), EB_N_PTR);
393         }
394     }
395 
396     return return_error;
397 }
398 
399 /*mem init function for LF params*/
init_lf_ctxt(EbDecHandle * dec_handle_ptr)400 static EbErrorType init_lf_ctxt(EbDecHandle  *dec_handle_ptr) {
401 
402     EbErrorType return_error = EB_ErrorNone;
403 
404     SeqHeader *seq_header = &dec_handle_ptr->seq_header;
405     /*Boundary checking of mi_row & mi_col are not done while populating,
406     so more memory is allocated by alligning to sb_size */
407     int32_t aligned_width   = ALIGN_POWER_OF_TWO(seq_header->max_frame_width,
408         MAX_SB_SIZE_LOG2);
409     int32_t aligned_height  = ALIGN_POWER_OF_TWO(seq_header->max_frame_height,
410         MAX_SB_SIZE_LOG2);
411     int32_t mi_cols = aligned_width >> MI_SIZE_LOG2;
412     int32_t mi_rows = aligned_height >> MI_SIZE_LOG2;
413 
414     EB_MALLOC_DEC(void *, dec_handle_ptr->pv_lf_ctxt, sizeof(LfCtxt), EB_N_PTR);
415 
416     LfCtxt *lf_ctxt = (LfCtxt *)dec_handle_ptr->pv_lf_ctxt;
417     /*Mem allocation for luma parmas 4x4 unit*/
418     EB_MALLOC_DEC(TxSize *, lf_ctxt->tx_size_l,
419         mi_rows * mi_cols * sizeof(TxSize), EB_N_PTR);
420 
421     /*Allocation of chroma params at 4x4 luma unit, can be optimized */
422     EB_MALLOC_DEC(TxSize *, lf_ctxt->tx_size_uv,
423         mi_rows * mi_cols * sizeof(TxSize), EB_N_PTR);
424 
425     return return_error;
426 }
427 
init_lr_ctxt(EbDecHandle * dec_handle_ptr)428 static EbErrorType init_lr_ctxt(EbDecHandle  *dec_handle_ptr)
429 {
430     EbErrorType return_error = EB_ErrorNone;
431     EB_MALLOC_DEC(void *, dec_handle_ptr->pv_lr_ctxt, sizeof(LrCtxt), EB_N_PTR);
432 
433     LrCtxt *lr_ctxt = (LrCtxt*)dec_handle_ptr->pv_lr_ctxt;
434     lr_ctxt->dec_handle_ptr = (void *)dec_handle_ptr;
435 
436     int32_t sb_size_h = block_size_high[dec_handle_ptr->seq_header.sb_size];
437     uint32_t picture_height_in_sb = (dec_handle_ptr->seq_header.
438         max_frame_height + sb_size_h - 1) / sb_size_h;
439     EbBool is_mt = dec_handle_ptr->dec_config.threads > 1;
440 
441     picture_height_in_sb = (is_mt == 0) ? 1 : picture_height_in_sb;
442     const int32_t num_planes = av1_num_planes(&dec_handle_ptr->seq_header.
443         color_config);
444 
445     uint32_t num_instances = MIN(picture_height_in_sb,
446         dec_handle_ptr->dec_config.threads);
447 
448     lr_ctxt->is_thread_min = EB_FALSE;
449     if (num_instances == dec_handle_ptr->dec_config.threads)
450         lr_ctxt->is_thread_min = EB_TRUE;
451     EB_MALLOC_DEC(RestorationLineBuffers ***, lr_ctxt->rlbs,
452         num_instances * sizeof(RestorationLineBuffers**), EB_N_PTR);
453     EB_MALLOC_DEC(int32_t **, lr_ctxt->rst_tmpbuf,
454          num_instances * RESTORATION_TMPBUF_SIZE, EB_N_PTR);
455     for (uint32_t i = 0; i < num_instances; i++) {
456         RestorationLineBuffers **p_rlbs;
457         EB_MALLOC_DEC(RestorationLineBuffers**, lr_ctxt->rlbs[i],
458             num_planes * sizeof(RestorationLineBuffers**), EB_N_PTR);
459         p_rlbs = lr_ctxt->rlbs[i];
460         for (int32_t pli = 0; pli < num_planes; pli++) {
461             EB_MALLOC_DEC(RestorationLineBuffers *, p_rlbs[pli],
462                 sizeof(RestorationLineBuffers), EB_N_PTR);
463         }
464     }
465     for (uint32_t i = 0; i < num_instances; i++) {
466         EB_MALLOC_DEC(int32_t *, lr_ctxt->rst_tmpbuf[i],
467             RESTORATION_TMPBUF_SIZE, EB_N_PTR);
468     }
469 
470     int frame_width = dec_handle_ptr->seq_header.max_frame_width;
471     int frame_height = dec_handle_ptr->seq_header.max_frame_height;
472     int sub_x = dec_handle_ptr->seq_header.color_config.subsampling_x;
473 
474     // Allocate memory for Deblocked line buffer around stripe(64) boundary for a frame
475     const int ext_h = RESTORATION_UNIT_OFFSET + frame_height;
476     const int num_stripes = (ext_h + 63) / 64;
477     int use_highbd = (dec_handle_ptr->seq_header.color_config.bit_depth > EB_8BIT ||
478         dec_handle_ptr->is_16bit_pipeline);
479 
480     for (int plane = 0; plane < num_planes; plane++)
481     {
482         const int is_uv = plane > 0;
483         const int ss_x = is_uv && sub_x;
484         const int plane_w = ((frame_width + ss_x) >> ss_x) + 2 * RESTORATION_EXTRA_HORZ;
485         const int stride = ALIGN_POWER_OF_TWO(plane_w, 5);
486         const int buf_size = num_stripes * stride * RESTORATION_CTX_VERT << use_highbd;
487         RestorationStripeBoundaries *boundaries = &lr_ctxt->boundaries[plane];
488 
489         EB_MALLOC_DEC(uint8_t *, boundaries->stripe_boundary_above,
490                       buf_size * sizeof(uint8_t), EB_N_PTR);
491         EB_MALLOC_DEC(uint8_t *, boundaries->stripe_boundary_below,
492                       buf_size * sizeof(uint8_t), EB_N_PTR);
493         boundaries->stripe_boundary_size = buf_size;
494         boundaries->stripe_boundary_stride = stride;
495     }
496     EB_MALLOC_DEC(uint8_t *, lr_ctxt->dst, (MAX_SB_SIZE + 8) *
497         RESTORATION_PROC_UNIT_SIZE * sizeof(uint8_t) << use_highbd, EB_N_PTR);
498     return return_error;
499 }
500 
dec_mem_init(EbDecHandle * dec_handle_ptr)501 EbErrorType dec_mem_init(EbDecHandle  *dec_handle_ptr) {
502     EbErrorType return_error = EB_ErrorNone;
503 
504     if (0 == dec_handle_ptr->seq_header_done)
505         return EB_ErrorNone;
506 
507     /* init module ctxts */
508     return_error |= dec_pic_mgr_init(dec_handle_ptr);
509 
510     return_error |= init_parse_context(dec_handle_ptr);
511 
512     return_error |= init_dec_mod_ctxt(dec_handle_ptr,
513                     &dec_handle_ptr->pv_dec_mod_ctxt);
514 
515     return_error |= init_lf_ctxt(dec_handle_ptr);
516 
517     return_error |= init_lr_ctxt(dec_handle_ptr);
518 
519     /* init frame buffers */
520     return_error |= init_main_frame_ctxt(dec_handle_ptr);
521 
522     /* Initialize the references to NULL */
523     for (int i = 0; i < REF_FRAMES; i++) {
524         dec_handle_ptr->ref_frame_map[i] = NULL;
525         dec_handle_ptr->next_ref_frame_map[i] = NULL;
526         dec_handle_ptr->remapped_ref_idx[i] = INVALID_IDX;
527     }
528     dec_handle_ptr->cur_pic_buf[0] = NULL;
529 
530     dec_handle_ptr->mem_init_done = 1;
531 
532     return return_error;
533 }
534 // clang-format on
535