xref: /aosp_15_r20/external/libavc/decoder/svc/isvcd_residual_resamp.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2022 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 /**
21  *******************************************************************************
22  * @file
23  *  isvcd_residual_resamp.c
24  *
25  * @brief
26  *  Contains routines that resample for SVC resampling
27  *
28  * @author
29  *  Kishore
30  *
31  * @par List of Functions:
32  *  - isvcd_residual_chroma_dyadic_alt()
33  *  - isvcd_residual_chroma_dyadic()
34  *  - isvcd_residual_luma_dyadic()
35  *  - isvcd_ref_layer_ptr_incr()
36  *  - isvcd_residual_reflayer_const_non_boundary_mb()
37  *  - isvcd_residual_reflayer_const_boundary_mb()
38  *  - isvcd_residual_reflayer_const()
39  *  - isvcd_interpolate_residual()
40  *  - isvcd_residual_samp_mb()
41  *  - isvcd_residual_samp_mb_dyadic()
42  *  - isvcd_residual_samp_populate_list()
43  *  - isvcd_residual_samp_res_init()
44  *
45  * @remarks
46  *  None
47  *
48  *******************************************************************************
49  */
50 
51 #include <assert.h>
52 #include <string.h>
53 #include "ih264_typedefs.h"
54 #include "ih264_macros.h"
55 #include "ih264_platform_macros.h"
56 #include "ih264d_bitstrm.h"
57 #include "ih264d_defs.h"
58 #include "ih264d_debug.h"
59 #include "isvcd_structs.h"
60 #include "ih264d_defs.h"
61 #include "ih264d_parse_cavlc.h"
62 #include "ih264d_mb_utils.h"
63 #include "ih264d_deblocking.h"
64 #include "ih264d_dpb_manager.h"
65 #include "ih264d_mvpred.h"
66 #include "ih264d_inter_pred.h"
67 #include "ih264d_process_pslice.h"
68 #include "ih264d_error_handler.h"
69 #include "ih264d_cabac.h"
70 #include "ih264d_debug.h"
71 #include "ih264d_tables.h"
72 #include "ih264d_parse_slice.h"
73 #include "ih264d_utils.h"
74 #include "ih264d_parse_islice.h"
75 #include "ih264d_process_bslice.h"
76 #include "ih264d_process_intra_mb.h"
77 #include "ih264_debug.h"
78 
79 /*****************************************************************************/
80 /*                                                                           */
81 /*  Function Name : isvcd_residual_chroma_dyadic_alt                          */
82 /*                                                                           */
83 /*  Description   : this fucntion does the upsampling of chroma residuals for*/
84 /*                  Dyadic cases and specific chroma phase cases             */
85 /*                                                                           */
86 /*  Inputs        : pv_residual_samp_ctxt : Residual upsampling context      */
87 /*                  pu1_inp_data : input 8 bit data pointer                  */
88 /*                  i4_inp_data_stride : input buffer stride                 */
89 /*                  pi2_out_res : output 16 bit buffer pointer               */
90 /*                  i4_out_res_stride : Output buffer stride                 */
91 /*                  pu1_inp_bitmap : input packed sign bit data pointer      */
92 /*                  i4_inp_bitmap_stride : sign bit buffer stride            */
93 /*                  i4_start_bit_pos : bit position in the byte of packed    */
94 /*                                      sign values                          */
95 /*  Globals       : none                                                     */
96 /*  Processing    : it does the upsampling with intial phase values          */
97 /*                                                                           */
98 /*  Outputs       : Upsampled residuals for chroma                           */
99 /*  Returns       : none                                                     */
100 /*                                                                           */
101 /*  Issues        : none                                                     */
102 /*                                                                           */
103 /*  Revision History:                                                        */
104 /*                                                                           */
105 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
106 /*         25 09 2021   vijayakumar          creation                        */
107 /*                                                                           */
108 /*****************************************************************************/
isvcd_residual_chroma_dyadic_alt(void * pv_residual_samp_ctxt,UWORD16 u2_mb_x,UWORD16 u2_mb_y,mem_element_t * ps_ref_mb_mode,WORD16 * pi2_inp_data,WORD32 i4_inp_data_stride,WORD16 * pi2_out_res,WORD32 i4_out_res_stride,WORD32 i4_cr_flag)109 void isvcd_residual_chroma_dyadic_alt(void *pv_residual_samp_ctxt, UWORD16 u2_mb_x, UWORD16 u2_mb_y,
110                                       mem_element_t *ps_ref_mb_mode, WORD16 *pi2_inp_data,
111                                       WORD32 i4_inp_data_stride, WORD16 *pi2_out_res,
112                                       WORD32 i4_out_res_stride, WORD32 i4_cr_flag)
113 {
114     residual_sampling_ctxt_t *ps_ctxt;
115     res_lyr_ctxt *ps_lyr_ctxt;
116     ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
117     ps_lyr_ctxt = &ps_ctxt->as_res_lyrs[ps_ctxt->i4_res_lyr_id];
118     /* ----------------- Processing ------------------------------- */
119     {
120         ref_pixel_map_t *ps_pos_phase;
121         residual_samp_map_ctxt_t *ps_chroma_map;
122         ref_mb_map_t *ps_x_off_len_chroma;
123         ref_mb_map_t *ps_y_off_len_chroma;
124         WORD32 i4_i;
125         WORD16 *pi2_ref_data_byte;
126         WORD32 *pi4_ref_array;
127         WORD32 i4_phase1, i4_phase2;
128         WORD32 i4_offset_x, i4_offset_y;
129         WORD32 i4_chrm_horz_int_mode, i4_chrm_vert_int_mode;
130         WORD32 i4_horz_intp_ctr = SUB_BLOCK_HEIGHT;
131 
132         ps_chroma_map = &ps_lyr_ctxt->s_chroma_map_ctxt;
133         ps_x_off_len_chroma = ps_chroma_map->ps_x_offset_length;
134         ps_y_off_len_chroma = ps_chroma_map->ps_y_offset_length;
135 
136         /* get the actual offset for the buffers */
137         i4_offset_x = ps_x_off_len_chroma[u2_mb_x].i2_offset;
138         i4_offset_y = ps_y_off_len_chroma[u2_mb_y].i2_offset;
139 
140         {
141             UWORD8 u1_mask;
142             WORD32 i4_mb_x, i4_mb_y;
143             WORD32 i4_chrm_nnz;
144             WORD32 i4_num_element_stride;
145             inter_lyr_mb_prms_t *ps_inter_lyr_mb_prms, *ps_inter_lyr_mb_prms_curr;
146 
147             u1_mask = (SVCD_TRUE == i4_cr_flag) ? 0xF0 : 0x0F;
148 
149             /* Top Left */
150             i4_mb_x = i4_offset_x >> 3;
151             i4_mb_y = i4_offset_y >> 3;
152 
153             /* get the location of the byte which has the current mb mode */
154             ps_inter_lyr_mb_prms = ps_ref_mb_mode->pv_buffer;
155             i4_num_element_stride = ps_ref_mb_mode->i4_num_element_stride;
156 
157             ps_inter_lyr_mb_prms_curr = ps_inter_lyr_mb_prms + i4_mb_x;
158             ps_inter_lyr_mb_prms_curr += i4_mb_y * i4_num_element_stride;
159             i4_chrm_nnz = ps_inter_lyr_mb_prms_curr->u1_chroma_nnz & u1_mask;
160 
161             /* Top Right */
162             i4_mb_x = (i4_offset_x + 4) >> 3;
163             ps_inter_lyr_mb_prms_curr = ps_inter_lyr_mb_prms + i4_mb_x;
164             ps_inter_lyr_mb_prms_curr += i4_mb_y * i4_num_element_stride;
165             i4_chrm_nnz |= ps_inter_lyr_mb_prms_curr->u1_chroma_nnz & u1_mask;
166 
167             /* Bottom Left */
168             i4_mb_x = i4_offset_x >> 3;
169             i4_mb_y = (i4_offset_y + 4) >> 3;
170             ps_inter_lyr_mb_prms_curr = ps_inter_lyr_mb_prms + i4_mb_x;
171             ps_inter_lyr_mb_prms_curr += i4_mb_y * i4_num_element_stride;
172             i4_chrm_nnz |= ps_inter_lyr_mb_prms_curr->u1_chroma_nnz & u1_mask;
173 
174             /* Bottom Right */
175             i4_mb_x = (i4_offset_x + 4) >> 3;
176             ps_inter_lyr_mb_prms_curr = ps_inter_lyr_mb_prms + i4_mb_x;
177             ps_inter_lyr_mb_prms_curr += i4_mb_y * i4_num_element_stride;
178 
179             i4_chrm_nnz |= ps_inter_lyr_mb_prms_curr->u1_chroma_nnz & u1_mask;
180             if(0 == i4_chrm_nnz)
181             {
182                 return;
183             }
184         }
185 
186         i4_chrm_horz_int_mode = ps_lyr_ctxt->i4_chrm_horz_int_mode;
187         i4_chrm_vert_int_mode = ps_lyr_ctxt->i4_chrm_vert_int_mode;
188 
189         if(0 == i4_chrm_horz_int_mode)
190         {
191             if(i4_offset_x >= 0)
192             {
193                 pi2_inp_data++;
194             }
195         }
196 
197         if(0 == i4_chrm_vert_int_mode)
198         {
199             if(i4_offset_y >= 0)
200             {
201                 pi2_inp_data += i4_inp_data_stride;
202             }
203         }
204         else
205         {
206             /* extra additional row of interpolation required for this case */
207             i4_horz_intp_ctr++;
208         }
209 
210         /* ----------- Horizontal Interpolation ---------------- */
211         pi2_ref_data_byte = pi2_inp_data;
212         ps_pos_phase = ps_lyr_ctxt->s_chroma_map_ctxt.ps_x_pos_phase;
213 
214         pi4_ref_array = (WORD32 *) ps_ctxt->pi2_refarray_buffer;
215         i4_phase1 = ps_pos_phase[0].i2_phase;
216         i4_phase2 = (i4_phase1 + 8) & 0x0F;
217 
218         /* interchange the phase values for corner case */
219         if(1 == i4_chrm_horz_int_mode)
220         {
221             WORD32 i4_temp;
222             i4_temp = i4_phase1;
223             i4_phase1 = i4_phase2;
224             i4_phase2 = i4_temp;
225         }
226 
227         for(i4_i = 0; i4_i < i4_horz_intp_ctr; i4_i++)
228         {
229             WORD16 i2_coeff1, i2_coeff2;
230 
231             i2_coeff1 = (WORD16) (pi2_ref_data_byte[0]);
232 
233             if(0 == i4_chrm_horz_int_mode)
234             {
235                 /* populate the first inter sample */
236                 *pi4_ref_array++ = i2_coeff1 << 4;
237             }
238 
239             /* unroll count 1 */
240             i2_coeff2 = (WORD16) (pi2_ref_data_byte[2]);
241 
242             /* populate 2 samples based on current coeffs */
243             *pi4_ref_array++ = ((16 - i4_phase2) * i2_coeff1 + i4_phase2 * i2_coeff2);
244             *pi4_ref_array++ = ((16 - i4_phase1) * i2_coeff1 + i4_phase1 * i2_coeff2);
245 
246             /* unroll count 2 */
247             i2_coeff1 = (WORD16) (pi2_ref_data_byte[4]);
248 
249             /* populate 2 samples based on current coeffs */
250             *pi4_ref_array++ = ((16 - i4_phase2) * i2_coeff2 + i4_phase2 * i2_coeff1);
251             *pi4_ref_array++ = ((16 - i4_phase1) * i2_coeff2 + i4_phase1 * i2_coeff1);
252 
253             /* unroll count 3 */
254             i2_coeff2 = (WORD16) (pi2_ref_data_byte[6]);
255 
256             /* populate 2 samples based on current coeffs */
257             *pi4_ref_array++ = ((16 - i4_phase2) * i2_coeff1 + i4_phase2 * i2_coeff2);
258             *pi4_ref_array++ = ((16 - i4_phase1) * i2_coeff1 + i4_phase1 * i2_coeff2);
259 
260             /* populate the last inter sample */
261             *pi4_ref_array++ = i2_coeff2 << 4;
262 
263             if(1 == i4_chrm_horz_int_mode)
264             {
265                 i2_coeff1 = (WORD16) (pi2_ref_data_byte[4]);
266 
267                 /* populate the last inter sample */
268                 *pi4_ref_array++ = i2_coeff1 << 4;
269             }
270 
271             /* vertical loop updates */
272             pi2_ref_data_byte = pi2_inp_data + ((i4_i + 1) * i4_inp_data_stride);
273         }
274 
275         /* ----------- Vertical Interpolation ---------------- */
276         pi4_ref_array = (WORD32 *) ps_ctxt->pi2_refarray_buffer;
277         ps_pos_phase = ps_lyr_ctxt->s_chroma_map_ctxt.ps_y_pos_phase;
278         i4_phase1 = ps_pos_phase[0].i2_phase;
279         i4_phase2 = (i4_phase1 + 8) & 0x0F;
280 
281         /* interchange the phase values for corner case */
282         if(0 != i4_chrm_vert_int_mode)
283         {
284             WORD32 i4_temp;
285             i4_temp = i4_phase1;
286             i4_phase1 = i4_phase2;
287             i4_phase2 = i4_temp;
288         }
289 
290         for(i4_i = 0; i4_i < BLOCK_WIDTH; i4_i++)
291         {
292             WORD16 *pi2_out;
293             WORD32 *pi4_ref_array_temp;
294             WORD32 i4_horz_samp_1, i4_horz_samp_2;
295             pi2_out = pi2_out_res;
296             pi4_ref_array_temp = pi4_ref_array;
297 
298             /* populate the first inter sample */
299             i4_horz_samp_1 = *pi4_ref_array_temp;
300             pi4_ref_array_temp += BLOCK_WIDTH;
301 
302             if(1 != i4_chrm_vert_int_mode)
303             {
304                 *pi2_out = (i4_horz_samp_1 + 8) >> 4;
305                 pi2_out += i4_out_res_stride;
306             }
307 
308             if(2 == i4_chrm_vert_int_mode)
309             {
310                 i4_horz_samp_1 = *pi4_ref_array_temp;
311                 pi4_ref_array_temp += BLOCK_WIDTH;
312                 *pi2_out = (i4_horz_samp_1 + 8) >> 4;
313                 pi2_out += i4_out_res_stride;
314             }
315 
316             /* unroll count 1 */
317             i4_horz_samp_2 = *pi4_ref_array_temp;
318             pi4_ref_array_temp += BLOCK_WIDTH;
319 
320             /* populate 2 samples based on current coeffs */
321             *pi2_out = ((16 - i4_phase2) * i4_horz_samp_1 + i4_phase2 * i4_horz_samp_2 + 128) >> 8;
322             pi2_out += i4_out_res_stride;
323 
324             /* unroll count 2 */
325             *pi2_out = ((16 - i4_phase1) * i4_horz_samp_1 + i4_phase1 * i4_horz_samp_2 + 128) >> 8;
326             pi2_out += i4_out_res_stride;
327 
328             /* unroll count 3 */
329             i4_horz_samp_1 = *pi4_ref_array_temp;
330             pi4_ref_array_temp += BLOCK_WIDTH;
331 
332             /* populate 2 samples based on current coeffs */
333             *pi2_out = ((16 - i4_phase2) * i4_horz_samp_2 + i4_phase2 * i4_horz_samp_1 + 128) >> 8;
334             pi2_out += i4_out_res_stride;
335 
336             /* unroll count 4 */
337             *pi2_out = ((16 - i4_phase1) * i4_horz_samp_2 + i4_phase1 * i4_horz_samp_1 + 128) >> 8;
338             pi2_out += i4_out_res_stride;
339 
340             /* unroll count 5 */
341             i4_horz_samp_2 = *pi4_ref_array_temp;
342 
343             /* populate 2 samples based on current coeffs */
344             *pi2_out = ((16 - i4_phase2) * i4_horz_samp_1 + i4_phase2 * i4_horz_samp_2 + 128) >> 8;
345             pi2_out += i4_out_res_stride;
346 
347             /* unroll count 6 */
348             *pi2_out = ((16 - i4_phase1) * i4_horz_samp_1 + i4_phase1 * i4_horz_samp_2 + 128) >> 8;
349             pi2_out += i4_out_res_stride;
350 
351             if(2 != i4_chrm_vert_int_mode)
352             {
353                 /* populate the last inter sample */
354                 *pi2_out = (i4_horz_samp_2 + 8) >> 4;
355 
356                 if(1 == i4_chrm_vert_int_mode)
357                 {
358                     pi2_out += i4_out_res_stride;
359                     pi4_ref_array_temp += BLOCK_WIDTH;
360                     i4_horz_samp_1 = *pi4_ref_array_temp;
361 
362                     /* populate the last inter sample */
363                     *pi2_out = (i4_horz_samp_1 + 8) >> 4;
364                 }
365             }
366             /* horizontal loop updates */
367             pi4_ref_array++;
368             pi2_out_res += 2;
369         }
370     }
371     return;
372 }
373 
374 /*****************************************************************************/
375 /*                                                                           */
376 /*  Function Name : isvcd_residual_chroma_dyadic                              */
377 /*                                                                           */
378 /*  Description   : this fucntion does the upsampling of chroma residuals for*/
379 /*                  Dyadic cases                                             */
380 /*                                                                           */
381 /*  Inputs        : pv_residual_samp_ctxt : Residual upsampling context      */
382 /*                  pu1_inp_data : input 8 bit data pointer                  */
383 /*                  i4_inp_data_stride : input buffer stride                 */
384 /*                  pi2_out_res : output 16 bit buffer pointer               */
385 /*                  i4_out_res_stride : Output buffer stride                 */
386 /*                  pu1_inp_bitmap : input packed sign bit data pointer      */
387 /*                  i4_inp_bitmap_stride : sign bit buffer stride            */
388 /*                  i4_start_bit_pos : bit position in the byte of packed    */
389 /*                                      sign values                          */
390 /*  Globals       : none                                                     */
391 /*  Processing    : it does the upsampling with intial phase values          */
392 /*                                                                           */
393 /*  Outputs       : Upsampled residuals for chroma                           */
394 /*  Returns       : none                                                     */
395 /*                                                                           */
396 /*  Issues        : none                                                     */
397 /*                                                                           */
398 /*  Revision History:                                                        */
399 /*                                                                           */
400 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
401 /*         25 09 2021   vijayakumar          creation                        */
402 /*                                                                           */
403 /*****************************************************************************/
isvcd_residual_chroma_dyadic(void * pv_residual_samp_ctxt,WORD16 * pi2_inp_data,WORD32 i4_inp_data_stride,WORD16 * pi2_out_res,WORD32 i4_out_res_stride)404 void isvcd_residual_chroma_dyadic(void *pv_residual_samp_ctxt, WORD16 *pi2_inp_data,
405                                   WORD32 i4_inp_data_stride, WORD16 *pi2_out_res,
406                                   WORD32 i4_out_res_stride)
407 {
408     residual_sampling_ctxt_t *ps_ctxt;
409     res_lyr_ctxt *ps_lyr_ctxt;
410     ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
411     ps_lyr_ctxt = &ps_ctxt->as_res_lyrs[ps_ctxt->i4_res_lyr_id];
412     /* ----------------- Processing ------------------------------- */
413     {
414         WORD32 i4_i;
415         WORD16 *pi2_ref_data_byte;
416         WORD32 *pi4_ref_array;
417         ref_pixel_map_t *ps_pos_phase;
418         WORD32 i4_phase1, i4_phase2;
419 
420         pi2_ref_data_byte = pi2_inp_data;
421         ps_pos_phase = ps_lyr_ctxt->s_chroma_map_ctxt.ps_x_pos_phase;
422 
423         /* ----------- Horizontal Interpolation ---------------- */
424         pi4_ref_array = (WORD32 *) ps_ctxt->pi2_refarray_buffer;
425         i4_phase1 = ps_pos_phase[0].i2_phase;
426         i4_phase2 = (i4_phase1 + 8) & 0x0F;
427 
428         for(i4_i = 0; i4_i < SUB_BLOCK_HEIGHT; i4_i++)
429         {
430             WORD16 i2_coeff1, i2_coeff2;
431             i2_coeff1 = (WORD16) (pi2_ref_data_byte[0]);
432 
433             /* populate the first inter sample */
434             *pi4_ref_array++ = i2_coeff1 << 4;
435 
436             /* unroll count 1 */
437             i2_coeff2 = (WORD16) (pi2_ref_data_byte[2]);
438 
439             /* populate 2 samples based on current coeffs */
440             *pi4_ref_array++ = ((16 - i4_phase2) * i2_coeff1 + i4_phase2 * i2_coeff2);
441 
442             /* unroll count 2 */
443             *pi4_ref_array++ = ((16 - i4_phase1) * i2_coeff1 + i4_phase1 * i2_coeff2);
444 
445             /* unroll count 3 */
446             i2_coeff1 = (WORD16) (pi2_ref_data_byte[4]);
447 
448             /* populate 2 samples based on current coeffs */
449             *pi4_ref_array++ = ((16 - i4_phase2) * i2_coeff2 + i4_phase2 * i2_coeff1);
450 
451             /* unroll count 4 */
452             *pi4_ref_array++ = ((16 - i4_phase1) * i2_coeff2 + i4_phase1 * i2_coeff1);
453 
454             /* unroll count 5 */
455             i2_coeff2 = (WORD16) (pi2_ref_data_byte[6]);
456 
457             /* populate 2 samples based on current coeffs */
458             *pi4_ref_array++ = ((16 - i4_phase2) * i2_coeff1 + i4_phase2 * i2_coeff2);
459 
460             /* unroll count 6 */
461             *pi4_ref_array++ = ((16 - i4_phase1) * i2_coeff1 + i4_phase1 * i2_coeff2);
462 
463             /* populate the last inter sample */
464             *pi4_ref_array++ = i2_coeff2 << 4;
465 
466             /* vertical loop uopdates */
467             pi2_ref_data_byte = pi2_inp_data + ((i4_i + 1) * i4_inp_data_stride);
468         }
469 
470         /* ----------- Vertical Interpolation ---------------- */
471         pi4_ref_array = (WORD32 *) ps_ctxt->pi2_refarray_buffer;
472         ps_pos_phase = ps_lyr_ctxt->s_chroma_map_ctxt.ps_y_pos_phase;
473         i4_phase1 = ps_pos_phase[0].i2_phase;
474         i4_phase2 = (i4_phase1 + 8) & 0x0F;
475 
476         for(i4_i = 0; i4_i < BLOCK_WIDTH; i4_i++)
477         {
478             WORD16 *pi2_out;
479             WORD32 *pi4_ref_array_temp;
480             WORD32 i4_horz_samp_1, i4_horz_samp_2;
481             pi2_out = pi2_out_res;
482             pi4_ref_array_temp = pi4_ref_array;
483 
484             /* populate the first inter sample */
485             i4_horz_samp_1 = *pi4_ref_array_temp;
486             pi4_ref_array_temp += BLOCK_WIDTH;
487             *pi2_out = (i4_horz_samp_1 + 8) >> 4;
488             pi2_out += i4_out_res_stride;
489 
490             /* unroll count 1 */
491             i4_horz_samp_2 = *pi4_ref_array_temp;
492             pi4_ref_array_temp += BLOCK_WIDTH;
493 
494             /* populate 2 samples based on current coeffs */
495             *pi2_out = ((16 - i4_phase2) * i4_horz_samp_1 + i4_phase2 * i4_horz_samp_2 + 128) >> 8;
496             pi2_out += i4_out_res_stride;
497 
498             /* unroll count 2 */
499             *pi2_out = ((16 - i4_phase1) * i4_horz_samp_1 + i4_phase1 * i4_horz_samp_2 + 128) >> 8;
500             pi2_out += i4_out_res_stride;
501 
502             /* unroll count 3 */
503             i4_horz_samp_1 = *pi4_ref_array_temp;
504             pi4_ref_array_temp += BLOCK_WIDTH;
505 
506             /* populate 2 samples based on current coeffs */
507             *pi2_out = ((16 - i4_phase2) * i4_horz_samp_2 + i4_phase2 * i4_horz_samp_1 + 128) >> 8;
508             pi2_out += i4_out_res_stride;
509 
510             /* unroll count 4 */
511             *pi2_out = ((16 - i4_phase1) * i4_horz_samp_2 + i4_phase1 * i4_horz_samp_1 + 128) >> 8;
512             pi2_out += i4_out_res_stride;
513 
514             /* unroll count 5 */
515             i4_horz_samp_2 = *pi4_ref_array_temp;
516 
517             /* populate 2 samples based on current coeffs */
518             *pi2_out = ((16 - i4_phase2) * i4_horz_samp_1 + i4_phase2 * i4_horz_samp_2 + 128) >> 8;
519             pi2_out += i4_out_res_stride;
520 
521             /* unroll count 6 */
522             *pi2_out = ((16 - i4_phase1) * i4_horz_samp_1 + i4_phase1 * i4_horz_samp_2 + 128) >> 8;
523             pi2_out += i4_out_res_stride;
524 
525             /* populate the last inter sample */
526             *pi2_out = (i4_horz_samp_2 + 8) >> 4;
527 
528             /* horizontal loop updates */
529             pi4_ref_array++;
530             pi2_out_res += 2;
531         }
532     }
533     return;
534 }
535 
536 /*****************************************************************************/
537 /*                                                                           */
538 /*  Function Name : isvcd_residual_luma_dyadic                                */
539 /*                                                                           */
540 /*  Description   : this fucntion does the upsampling of luma residuals for  */
541 /*                  Dyadic cases                                             */
542 /*                                                                           */
543 /*  Inputs        : pv_residual_samp_ctxt : Residual upsampling context      */
544 /*                  pu1_inp_data : input 8 bit data pointer                  */
545 /*                  i4_inp_data_stride : input buffer stride                 */
546 /*                  pi2_out_res : output 16 bit buffer pointer               */
547 /*                  i4_out_res_stride : Output buffer stride                 */
548 /*                  pu1_inp_bitmap : input packed sign bit data pointer      */
549 /*                  i4_inp_bitmap_stride : sign bit buffer stride            */
550 /*                  ps_ref_mb_mode : reference mb mode pointer of base layer */
551 /*                  ps_coord : mb co-ordinate pointer                        */
552 /*  Globals       : none                                                     */
553 /*  Processing    : it does the upsampling with fixed phase values and       */
554 /*                  reference layer transform size                           */
555 /*  Outputs       : Upsampled residuals for luma                             */
556 /*  Returns       : none                                                     */
557 /*                                                                           */
558 /*  Issues        : none                                                     */
559 /*                                                                           */
560 /*  Revision History:                                                        */
561 /*                                                                           */
562 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
563 /*         25 09 2021   vijayakumar          creation                        */
564 /*                                                                           */
565 /*****************************************************************************/
isvcd_residual_luma_dyadic(void * pv_residual_samp_ctxt,WORD16 * pi2_inp_data,WORD32 i4_inp_data_stride,WORD16 * pi2_out_res,WORD32 i4_out_res_stride,mem_element_t * ps_ref_mb_mode,UWORD16 u2_mb_x,UWORD16 u2_mb_y,WORD32 i4_ref_nnz,WORD32 i4_ref_tx_size)566 void isvcd_residual_luma_dyadic(void *pv_residual_samp_ctxt, WORD16 *pi2_inp_data,
567                                 WORD32 i4_inp_data_stride, WORD16 *pi2_out_res,
568                                 WORD32 i4_out_res_stride, mem_element_t *ps_ref_mb_mode,
569                                 UWORD16 u2_mb_x, UWORD16 u2_mb_y, WORD32 i4_ref_nnz,
570                                 WORD32 i4_ref_tx_size)
571 
572 {
573     WORD16 *pi2_refarray_buffer;
574     WORD32 i4_blk_ctr;
575     residual_sampling_ctxt_t *ps_ctxt;
576 
577     UNUSED(ps_ref_mb_mode);
578     UNUSED(u2_mb_x);
579     UNUSED(u2_mb_y);
580 
581     ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
582     pi2_refarray_buffer = ps_ctxt->pi2_refarray_buffer;
583 
584     /* based on transform size the counter and interpolation width and */
585     /* height are intialised as follows                                */
586     if((i4_ref_tx_size) && (0 != i4_ref_nnz))
587     {
588         WORD16 *pi2_ref_data_byte;
589         WORD32 *pi4_ref_array;
590         WORD32 i4_i, i4_j;
591 
592         pi2_ref_data_byte = pi2_inp_data;
593 
594         /* ----------- Horizontal Interpolation ---------------- */
595         pi4_ref_array = (WORD32 *) pi2_refarray_buffer;
596         for(i4_i = 0; i4_i < BLOCK_HEIGHT; i4_i++)
597         {
598             WORD16 i2_coeff1, i2_coeff2;
599             i2_coeff1 = (WORD16) (*pi2_ref_data_byte++);
600 
601             /* populate the first inter sample */
602             *pi4_ref_array++ = i2_coeff1 << 2;
603 
604             for(i4_j = 0; i4_j < 14; i4_j += 2)
605             {
606                 i2_coeff2 = (WORD16) (*pi2_ref_data_byte++);
607 
608                 /* populate 2 samples based on current coeffs */
609                 *pi4_ref_array++ = ((i2_coeff1 << 1) + (i2_coeff1) + (i2_coeff2));
610                 *pi4_ref_array++ = ((i2_coeff2 << 1) + (i2_coeff2) + (i2_coeff1));
611 
612                 /* store the coeff 2 to coeff 1 */
613                 /* (used in next iteration)     */
614                 i2_coeff1 = i2_coeff2;
615             }
616 
617             /* populate the last inter sample */
618             *pi4_ref_array++ = i2_coeff1 << 2;
619 
620             /* vertical loop uopdates */
621             pi2_ref_data_byte = pi2_inp_data + ((i4_i + 1) * i4_inp_data_stride);
622         }
623 
624         /* ----------- Vertical Interpolation ---------------- */
625         pi4_ref_array = (WORD32 *) pi2_refarray_buffer;
626 
627         for(i4_i = 0; i4_i < MB_WIDTH; i4_i++)
628         {
629             WORD32 *pi4_ref_array_temp;
630             WORD16 *pi2_out;
631             WORD32 i4_horz_samp_1, i4_horz_samp_2;
632 
633             pi4_ref_array_temp = pi4_ref_array;
634             pi2_out = pi2_out_res;
635             i4_horz_samp_1 = *pi4_ref_array_temp;
636 
637             /* populate the first inter sample */
638             *pi2_out = (i4_horz_samp_1 + 2) >> 2;
639             pi2_out += i4_out_res_stride;
640 
641             for(i4_j = 0; i4_j < 14; i4_j += 2)
642             {
643                 pi4_ref_array_temp += MB_WIDTH;
644                 i4_horz_samp_2 = *pi4_ref_array_temp;
645 
646                 /* populate 2 samples based on current coeffs */
647                 *pi2_out = ((i4_horz_samp_1 << 1) + (i4_horz_samp_1) + (i4_horz_samp_2) + 8) >> 4;
648                 pi2_out += i4_out_res_stride;
649                 *pi2_out = ((i4_horz_samp_2 << 1) + (i4_horz_samp_2) + (i4_horz_samp_1) + 8) >> 4;
650                 pi2_out += i4_out_res_stride;
651 
652                 /* store the coeff 2 to coeff 1 */
653                 /* (used in next iteration)     */
654                 i4_horz_samp_1 = i4_horz_samp_2;
655             }
656 
657             /* populate the first inter sample */
658             *pi2_out = (i4_horz_samp_1 + 2) >> 2;
659 
660             /* horizontal loop updates */
661             pi4_ref_array++;
662             pi2_out_res++;
663         }
664     }
665     else
666     {
667         /* ----------------------------------------------------------------- */
668         /* LOOP over number of blocks                                        */
669         /* ----------------------------------------------------------------- */
670         for(i4_blk_ctr = 0; i4_blk_ctr < 4; i4_blk_ctr++)
671         {
672             WORD16 *pi2_ref_data_byte;
673             WORD32 *pi4_ref_array;
674             WORD32 i4_i;
675 
676             /* if reference layer is not coded then no processing */
677             if(0 != (i4_ref_nnz & 0x1))
678             {
679                 pi2_ref_data_byte = pi2_inp_data;
680 
681                 /* ----------- Horizontal Interpolation ---------------- */
682                 pi4_ref_array = (WORD32 *) pi2_refarray_buffer;
683 
684                 for(i4_i = 0; i4_i < SUB_BLOCK_HEIGHT; i4_i++)
685                 {
686                     WORD16 i2_coeff1, i2_coeff2;
687                     i2_coeff1 = (WORD16) (*pi2_ref_data_byte++);
688 
689                     /* populate the first inter sample */
690                     *pi4_ref_array++ = i2_coeff1 << 2;
691 
692                     i2_coeff2 = (WORD16) (*pi2_ref_data_byte++);
693 
694                     /* populate 2 samples based on current coeffs */
695                     *pi4_ref_array++ = ((i2_coeff1 << 1) + (i2_coeff1) + (i2_coeff2));
696                     *pi4_ref_array++ = ((i2_coeff2 << 1) + (i2_coeff2) + (i2_coeff1));
697 
698                     i2_coeff1 = (WORD16) (*pi2_ref_data_byte++);
699 
700                     /* populate 2 samples based on current coeffs */
701                     *pi4_ref_array++ = ((i2_coeff2 << 1) + (i2_coeff2) + (i2_coeff1));
702                     *pi4_ref_array++ = ((i2_coeff1 << 1) + (i2_coeff1) + (i2_coeff2));
703 
704                     i2_coeff2 = (WORD16) (*pi2_ref_data_byte++);
705 
706                     /* populate 2 samples based on current coeffs */
707                     *pi4_ref_array++ = ((i2_coeff1 << 1) + (i2_coeff1) + (i2_coeff2));
708                     *pi4_ref_array++ = ((i2_coeff2 << 1) + (i2_coeff2) + (i2_coeff1));
709 
710                     /* populate the last inter sample */
711                     *pi4_ref_array++ = i2_coeff2 << 2;
712 
713                     /* vertical loop uopdates */
714                     pi2_ref_data_byte = pi2_inp_data + ((i4_i + 1) * i4_inp_data_stride);
715                 }
716 
717                 /* ----------- Vertical Interpolation ---------------- */
718                 pi4_ref_array = (WORD32 *) pi2_refarray_buffer;
719 
720                 for(i4_i = 0; i4_i < BLOCK_WIDTH; i4_i++)
721                 {
722                     WORD32 *pi4_ref_array_temp;
723                     WORD16 *pi2_out;
724                     WORD32 i4_horz_samp_1, i4_horz_samp_2;
725 
726                     pi4_ref_array_temp = pi4_ref_array;
727                     pi2_out = pi2_out_res;
728                     i4_horz_samp_1 = *pi4_ref_array_temp;
729 
730                     /* populate the first inter sample */
731                     *pi2_out = (i4_horz_samp_1 + 2) >> 2;
732                     pi2_out += i4_out_res_stride;
733 
734                     /* unroll loop count 1 */
735                     pi4_ref_array_temp += BLOCK_WIDTH;
736                     i4_horz_samp_2 = *pi4_ref_array_temp;
737 
738                     /* populate 2 samples based on current coeffs */
739                     *pi2_out =
740                         ((i4_horz_samp_1 << 1) + (i4_horz_samp_1) + (i4_horz_samp_2) + 8) >> 4;
741                     pi2_out += i4_out_res_stride;
742                     *pi2_out =
743                         ((i4_horz_samp_2 << 1) + (i4_horz_samp_2) + (i4_horz_samp_1) + 8) >> 4;
744                     pi2_out += i4_out_res_stride;
745 
746                     /* unroll loop count 2 */
747                     pi4_ref_array_temp += BLOCK_WIDTH;
748                     i4_horz_samp_1 = *pi4_ref_array_temp;
749 
750                     /* populate 2 samples based on current coeffs */
751                     *pi2_out =
752                         ((i4_horz_samp_2 << 1) + (i4_horz_samp_2) + (i4_horz_samp_1) + 8) >> 4;
753                     pi2_out += i4_out_res_stride;
754                     *pi2_out =
755                         ((i4_horz_samp_1 << 1) + (i4_horz_samp_1) + (i4_horz_samp_2) + 8) >> 4;
756                     pi2_out += i4_out_res_stride;
757 
758                     /* unroll loop count 3 */
759                     pi4_ref_array_temp += BLOCK_WIDTH;
760                     i4_horz_samp_2 = *pi4_ref_array_temp;
761 
762                     /* populate 2 samples based on current coeffs */
763                     *pi2_out =
764                         ((i4_horz_samp_1 << 1) + (i4_horz_samp_1) + (i4_horz_samp_2) + 8) >> 4;
765                     pi2_out += i4_out_res_stride;
766                     *pi2_out =
767                         ((i4_horz_samp_2 << 1) + (i4_horz_samp_2) + (i4_horz_samp_1) + 8) >> 4;
768                     pi2_out += i4_out_res_stride;
769 
770                     /* populate the last inter sample */
771                     *pi2_out = (i4_horz_samp_2 + 2) >> 2;
772 
773                     /* horizontal loop updates */
774                     pi4_ref_array++;
775                     pi2_out_res++;
776                 }
777             }
778             else
779             {
780                 pi2_out_res += BLOCK_WIDTH;
781             }
782 
783             /* Block level loop updates */
784             if(1 == i4_blk_ctr)
785             {
786                 pi2_inp_data -= SUB_BLOCK_WIDTH;
787                 pi2_inp_data += (i4_inp_data_stride * SUB_BLOCK_HEIGHT);
788                 pi2_out_res -= MB_WIDTH;
789                 pi2_out_res += (i4_out_res_stride * BLOCK_HEIGHT);
790                 i4_ref_nnz >>= 2;
791             }
792             else
793             {
794                 pi2_inp_data += SUB_BLOCK_WIDTH;
795             }
796 
797             i4_ref_nnz >>= 1;
798 
799         } /* end of loop over all the blocks */
800     }
801     return;
802 }
803 
804 /*****************************************************************************/
805 /*                                                                           */
806 /*  Function Name :  isvcd_ref_layer_ptr_incr                                 */
807 /*                                                                           */
808 /*  Description   :  this function returns the pointer increments for        */
809 /*                   the operand2 of the bilinear interpolation              */
810 /*  Inputs        : pi1_ref_mb_modes : reference mb modes                    */
811 /*                  i4_ref_mode_stride : mb mode buffer stride               */
812 /*                    i4_element_size : size of reference mb mode            */
813 /*                  i4_x_offset : ref offset x                               */
814 /*                  i4_y_offset : ref offset y                               */
815 /*                    i4_refary_wd : reference array width                   */
816 /*                    i4_refary_ht : reference array height                  */
817 /*                  pu1_ref_x_ptr_incr : ptr increment buffer for x          */
818 /*                  pu1_ref_y_ptr_incr : ptr increment buffer for y          */
819 /*                    i4_chroma_flag : chroma processing flag                */
820 /*  Globals       : none                                                     */
821 /*  Processing    : it calculates the increment as per the transform size    */
822 /*                                                                           */
823 /*  Outputs       : none                                                     */
824 /*  Returns       : none                                                     */
825 /*                                                                           */
826 /*  Issues        : none                                                     */
827 /*                                                                           */
828 /*  Revision History:                                                        */
829 /*                                                                           */
830 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
831 /*         18 08 2021   Kishore              creation                        */
832 /*                                                                           */
833 /*****************************************************************************/
isvcd_ref_layer_ptr_incr(WORD8 * pi1_ref_mb_modes,WORD32 i4_ref_mode_stride,WORD32 i4_element_size,WORD32 i4_x_offset,WORD32 i4_y_offset,WORD32 i4_refary_wd,WORD32 i4_refary_ht,UWORD8 * pu1_ref_x_ptr_incr,UWORD8 * pu1_ref_y_ptr_incr,WORD32 i4_chroma_flag)834 WORD32 isvcd_ref_layer_ptr_incr(WORD8 *pi1_ref_mb_modes, WORD32 i4_ref_mode_stride,
835                                 WORD32 i4_element_size, WORD32 i4_x_offset, WORD32 i4_y_offset,
836                                 WORD32 i4_refary_wd, WORD32 i4_refary_ht,
837                                 UWORD8 *pu1_ref_x_ptr_incr, UWORD8 *pu1_ref_y_ptr_incr,
838                                 WORD32 i4_chroma_flag)
839 {
840     WORD32 i4_x, i4_y;
841     WORD32 i4_x_idx, i4_y_idx;
842     WORD32 i4_prev_x, i4_prev_y;
843     WORD32 i4_const_val;
844     WORD32 i4_pos_x, i4_pos_y;
845     WORD32 i4_trans_size;
846     WORD32 i4_act_ary_wd, i4_act_ary_ht;
847     WORD32 i4_and_const;
848     UWORD8 *pu1_incr_x, *pu1_incr_y;
849     WORD32 i4_mb_sft;
850     WORD32 i4_mb_x, i4_mb_y;
851     WORD8 *pi1_ref_mb_modes_incr;
852     WORD8 *pi1_ref_mb_modes_incr_temp;
853     inter_lyr_mb_prms_t *ps_inter_lyr_mb_prms;
854     WORD32 i4_mb_x_strt, i4_mb_y_strt;
855     WORD32 i4_mb_quard1_part_x, i4_mb_quard1_part_y;
856     WORD32 i4_x_ref, i4_y_ref;
857     WORD32 i4_tx_size, i4_tx_size_q0, i4_tx_size_q1, i4_tx_size_q2, i4_tx_size_q3;
858     WORD8 i1_mb_mode_q0, i1_mb_mode_q1, i1_mb_mode_q2, i1_mb_mode_q3;
859     WORD32 i4_mb_wd;
860     WORD32 i4_mb_ht;
861 
862     i4_mb_wd = MB_WIDTH >> i4_chroma_flag;
863     i4_mb_ht = MB_HEIGHT >> i4_chroma_flag;
864 
865     /* Memset to 1 the increment buffers */
866     memset(pu1_ref_x_ptr_incr, 1, (i4_refary_wd * i4_refary_ht));
867     memset(pu1_ref_y_ptr_incr, 1, (i4_refary_wd * i4_refary_ht));
868 
869     /* Initialise actual width and height */
870     i4_act_ary_wd = i4_refary_wd;
871     i4_act_ary_ht = i4_refary_ht;
872 
873     /* Initialize x and y */
874     i4_x = 0;
875     i4_y = 0;
876     i4_prev_y = 0;
877     i4_mb_sft = (MB_WIDTH_SHIFT - i4_chroma_flag);
878 
879     /* Loop over all MBs in the reference array */
880     if(0 == i4_chroma_flag)
881     {
882         i4_x_ref = i4_x_offset + 0;
883         i4_y_ref = i4_y_offset + 0;
884         i4_mb_x_strt = i4_x_ref % i4_mb_wd;
885         i4_mb_y_strt = i4_y_ref % i4_mb_ht;
886         i4_mb_quard1_part_x = i4_mb_wd - i4_mb_x_strt;
887         i4_mb_quard1_part_y = i4_mb_ht - i4_mb_y_strt;
888 
889         if(!(i4_mb_quard1_part_x >= 0))
890         {
891             return NOT_OK;
892         }
893         if(!(i4_mb_quard1_part_y >= 0))
894         {
895             return NOT_OK;
896         }
897 
898         /* Take care of negative offsets */
899         if(i4_x_ref > 0)
900         {
901             i4_mb_x = (i4_x_ref >> i4_mb_sft);
902         }
903         else
904         {
905             i4_mb_x = 0;
906         }
907         if(i4_y_ref > 0)
908         {
909             i4_mb_y = (i4_y_ref >> i4_mb_sft);
910         }
911         else
912         {
913             i4_mb_y = 0;
914         }
915 
916         /* get the location of the byte which has the current mb mode */
917         pi1_ref_mb_modes_incr = pi1_ref_mb_modes + (i4_mb_y * i4_ref_mode_stride * i4_element_size);
918         pi1_ref_mb_modes_incr += (i4_mb_x * i4_element_size);
919         ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr;
920         i1_mb_mode_q0 = ps_inter_lyr_mb_prms->i1_mb_mode;
921         i4_tx_size_q0 =
922             (i1_mb_mode_q0 <= SVC_INTER_MB)
923                 ? ((ps_inter_lyr_mb_prms->i1_tx_size < 0) ? 1 : ps_inter_lyr_mb_prms->i1_tx_size)
924                 : 1;
925 
926         pi1_ref_mb_modes_incr_temp = pi1_ref_mb_modes_incr;
927         if(i4_mb_quard1_part_x > 0)
928         {
929             pi1_ref_mb_modes_incr_temp = pi1_ref_mb_modes_incr + i4_element_size;
930             ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr_temp;
931             i1_mb_mode_q1 = ps_inter_lyr_mb_prms->i1_mb_mode;
932             i4_tx_size_q1 =
933                 (i1_mb_mode_q1 <= SVC_INTER_MB)
934                     ? ((ps_inter_lyr_mb_prms->i1_tx_size < 0) ? 1
935                                                               : ps_inter_lyr_mb_prms->i1_tx_size)
936                     : 1;
937         }
938 
939         if(i4_mb_quard1_part_y > 0)
940         {
941             pi1_ref_mb_modes_incr_temp =
942                 pi1_ref_mb_modes_incr + (i4_ref_mode_stride * i4_element_size);
943             ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr_temp;
944             i1_mb_mode_q2 = ps_inter_lyr_mb_prms->i1_mb_mode;
945             i4_tx_size_q2 =
946                 (i1_mb_mode_q2 <= SVC_INTER_MB)
947                     ? ((ps_inter_lyr_mb_prms->i1_tx_size < 0) ? 1
948                                                               : ps_inter_lyr_mb_prms->i1_tx_size)
949                     : 1;
950         }
951 
952         if((i4_mb_quard1_part_x > 0) && (i4_mb_quard1_part_y > 0))
953         {
954             pi1_ref_mb_modes_incr_temp =
955                 pi1_ref_mb_modes_incr + (i4_ref_mode_stride * i4_element_size) + i4_element_size;
956             ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr_temp;
957             i1_mb_mode_q3 = ps_inter_lyr_mb_prms->i1_mb_mode;
958             i4_tx_size_q3 =
959                 (i1_mb_mode_q3 <= SVC_INTER_MB)
960                     ? ((ps_inter_lyr_mb_prms->i1_tx_size < 0) ? 1
961                                                               : ps_inter_lyr_mb_prms->i1_tx_size)
962                     : 1;
963         }
964 
965         do
966         {
967             WORD32 i4_idx;
968             WORD32 i4_wd, i4_ht;
969             WORD32 i4_max_pos_x, i4_max_pos_y;
970 
971             i4_prev_x = i4_x;
972             i4_x_ref = i4_x_offset + i4_x;
973             i4_y_ref = i4_y_offset + i4_y;
974             i4_tx_size = i4_tx_size_q0;
975             if(i4_x >= i4_mb_quard1_part_x)
976             {
977                 if(i4_y < i4_mb_quard1_part_y)
978                 {
979                     i4_tx_size = i4_tx_size_q1;
980                 }
981                 else if(i4_y >= i4_mb_quard1_part_y)
982                 {
983                     i4_tx_size = i4_tx_size_q3;
984                 }
985             }
986             else if(i4_x < i4_mb_quard1_part_x)
987             {
988                 if(i4_y >= i4_mb_quard1_part_y)
989                 {
990                     i4_tx_size = i4_tx_size_q2;
991                 }
992             }
993 
994             /* Get the transform size as 4 or 8 */
995             i4_trans_size = ((i4_tx_size + 1) << 2);
996             i4_const_val = i4_trans_size - 1;
997             i4_and_const = i4_const_val;
998 
999             /* Fill horizontal tx block edges of current reference mb with 0 */
1000             pu1_incr_x = pu1_ref_x_ptr_incr + i4_x;
1001             pu1_incr_x += (i4_y * i4_refary_wd);
1002             i4_ht = (16 - (i4_y_ref & 0xF));
1003             i4_ht = MIN((i4_act_ary_ht - i4_y), i4_ht);
1004 
1005             i4_x_idx = i4_x;
1006             i4_pos_x = i4_x_ref & 0xF;
1007             i4_max_pos_x = 16;
1008             i4_x += (16 - i4_pos_x);
1009 
1010             /* Get the transform block edge pos */
1011             i4_idx = (i4_const_val - (i4_pos_x & i4_and_const));
1012             i4_x_idx += i4_idx;
1013 
1014             while((i4_pos_x < i4_max_pos_x) && (i4_x_idx < i4_act_ary_wd))
1015             {
1016                 WORD32 i4_i;
1017                 UWORD8 *pu1_incr;
1018 
1019                 pu1_incr = pu1_incr_x + i4_idx;
1020                 for(i4_i = 0; i4_i < i4_ht; i4_i++)
1021                 {
1022                     /* Fill the block edge with 0s */
1023                     *pu1_incr = 0;
1024                     pu1_incr += i4_refary_wd;
1025                 }
1026 
1027                 /* Updates */
1028                 i4_pos_x += i4_trans_size;
1029                 pu1_incr_x += i4_trans_size;
1030                 i4_x_idx += MIN(i4_trans_size, (i4_act_ary_wd - i4_x_idx));
1031             }
1032 
1033             /* Fill vertical tx block edges of current reference mb with 0 */
1034             pu1_incr_y = pu1_ref_y_ptr_incr + i4_prev_x;
1035             pu1_incr_y += (i4_y * i4_refary_wd);
1036             i4_wd = (16 - (i4_x_ref & 0xF));
1037             i4_wd = MIN((i4_act_ary_wd - i4_prev_x), i4_wd);
1038             i4_y_idx = i4_y;
1039             i4_pos_y = i4_y_ref & 0xF;
1040             i4_max_pos_y = 16;
1041             i4_y += (16 - i4_pos_y);
1042 
1043             /* Get the transform block edge pos */
1044             i4_idx = (i4_const_val - (i4_pos_y & i4_and_const));
1045             i4_y_idx += i4_idx;
1046 
1047             while((i4_pos_y < i4_max_pos_y) && (i4_y_idx < i4_act_ary_ht))
1048             {
1049                 WORD32 i4_i;
1050                 UWORD8 *pu1_incr;
1051 
1052                 pu1_incr = pu1_incr_y + i4_idx * i4_refary_wd;
1053                 for(i4_i = 0; i4_i < i4_wd; i4_i++)
1054                 {
1055                     /* Fill the block edge with 0s */
1056                     *pu1_incr = 0;
1057                     pu1_incr++;
1058                 }
1059 
1060                 /* Updates */
1061                 i4_pos_y += i4_trans_size;
1062                 pu1_incr_y += i4_trans_size * i4_refary_wd;
1063                 i4_y_idx += MIN(i4_trans_size, (i4_act_ary_ht - i4_y_idx));
1064             }
1065 
1066             /* Loop updates */
1067             if(i4_x < i4_act_ary_wd)
1068             {
1069                 i4_y = i4_prev_y;
1070             }
1071             else if(i4_y < i4_act_ary_ht)
1072             {
1073                 i4_prev_y = i4_y;
1074                 i4_x = 0;
1075             }
1076 
1077         } while((i4_y < i4_act_ary_ht) || (i4_x < i4_act_ary_wd));
1078 
1079     } /* End of if 0 == i4_chroma_flag */
1080     else
1081     {
1082         /* Set the transform size as 4 */
1083         i4_trans_size = 4;
1084         i4_const_val = 3;
1085 
1086         do
1087         {
1088             WORD32 i4_x_ref, i4_y_ref;
1089             WORD32 i4_idx;
1090             WORD32 i4_wd, i4_ht;
1091             WORD32 i4_max_pos_x, i4_max_pos_y;
1092 
1093             i4_prev_x = i4_x;
1094             i4_x_ref = i4_x_offset + i4_x;
1095             i4_y_ref = i4_y_offset + i4_y;
1096 
1097             /* Fill horizontal tx block edges of current reference mb with 0 */
1098             pu1_incr_x = pu1_ref_x_ptr_incr + i4_x;
1099             pu1_incr_x += (i4_y * i4_refary_wd);
1100             i4_ht = (8 - (i4_y_ref & 0x7));
1101             i4_ht = MIN((i4_act_ary_ht - i4_y), i4_ht);
1102             i4_x_idx = i4_x;
1103             i4_pos_x = i4_x_ref & 0x7;
1104             i4_max_pos_x = 8;
1105             i4_x += (8 - i4_pos_x);
1106 
1107             /* Get the transform block edge pos */
1108             i4_idx = (i4_const_val - (i4_pos_x & 0x3));
1109             i4_x_idx += i4_idx;
1110 
1111             while((i4_pos_x < i4_max_pos_x) && (i4_x_idx < i4_act_ary_wd))
1112             {
1113                 WORD32 i4_i;
1114                 UWORD8 *pu1_incr;
1115 
1116                 pu1_incr = pu1_incr_x + i4_idx;
1117                 for(i4_i = 0; i4_i < i4_ht; i4_i++)
1118                 {
1119                     /* Fill the block edge with 0s */
1120                     *pu1_incr = 0;
1121                     pu1_incr += i4_refary_wd;
1122                 }
1123 
1124                 /* Updates */
1125                 i4_pos_x += i4_trans_size;
1126                 pu1_incr_x += i4_trans_size;
1127                 i4_x_idx += MIN(i4_trans_size, (i4_act_ary_wd - i4_x_idx));
1128             }
1129 
1130             /* Fill vertical tx block edges of current reference mb with 0 */
1131             pu1_incr_y = pu1_ref_y_ptr_incr + i4_prev_x;
1132             pu1_incr_y += (i4_y * i4_refary_wd);
1133             i4_wd = (8 - (i4_x_ref & 0x7));
1134             i4_wd = MIN((i4_act_ary_wd - i4_prev_x), i4_wd);
1135             i4_y_idx = i4_y;
1136             i4_pos_y = i4_y_ref & 0x7;
1137             i4_max_pos_y = 8;
1138             i4_y += (8 - i4_pos_y);
1139 
1140             /* Get the transform block edge pos */
1141             i4_idx = (i4_const_val - (i4_pos_y & 0x3));
1142             i4_y_idx += i4_idx;
1143 
1144             while((i4_pos_y < i4_max_pos_y) && (i4_y_idx < i4_act_ary_ht))
1145             {
1146                 WORD32 i4_i;
1147                 UWORD8 *pu1_incr;
1148 
1149                 pu1_incr = pu1_incr_y + i4_idx * i4_refary_wd;
1150                 for(i4_i = 0; i4_i < i4_wd; i4_i++)
1151                 {
1152                     /* Fill the block edge with 0s */
1153                     *pu1_incr = 0;
1154                     pu1_incr++;
1155                 }
1156 
1157                 /* Updates */
1158                 i4_pos_y += i4_trans_size;
1159                 pu1_incr_y += i4_trans_size * i4_refary_wd;
1160                 i4_y_idx += MIN(i4_trans_size, (i4_act_ary_ht - i4_y_idx));
1161             }
1162 
1163             /* Loop updates */
1164             if(i4_x < i4_act_ary_wd)
1165             {
1166                 i4_y = i4_prev_y;
1167             }
1168             else if(i4_y < i4_act_ary_ht)
1169             {
1170                 i4_prev_y = i4_y;
1171                 i4_x = 0;
1172             }
1173 
1174         } while((i4_y < i4_act_ary_ht) || (i4_x < i4_act_ary_wd));
1175 
1176     } /* End of chroma */
1177     return OK;
1178 
1179 } /* End of "isvcd_ref_layer_ptr_incr" */
1180 
isvcd_residual_reflayer_const_non_boundary_mb(WORD16 * pi2_inp_data,WORD32 i4_inp_data_stride,WORD16 * pi2_ref_array,WORD32 i4_refarray_wd,WORD32 i4_refarray_ht,WORD32 i4_ref_mb_type_q0,WORD32 i4_ref_mb_type_q1,WORD32 i4_ref_mb_type_q2,WORD32 i4_ref_mb_type_q3,WORD32 i4_mb_quard1_part_x,WORD32 i4_mb_quard1_part_y,WORD32 i4_chroma_flag)1181 void isvcd_residual_reflayer_const_non_boundary_mb(
1182     WORD16 *pi2_inp_data, WORD32 i4_inp_data_stride, WORD16 *pi2_ref_array, WORD32 i4_refarray_wd,
1183     WORD32 i4_refarray_ht, WORD32 i4_ref_mb_type_q0, WORD32 i4_ref_mb_type_q1,
1184     WORD32 i4_ref_mb_type_q2, WORD32 i4_ref_mb_type_q3, WORD32 i4_mb_quard1_part_x,
1185     WORD32 i4_mb_quard1_part_y, WORD32 i4_chroma_flag)
1186 {
1187     WORD32 i4_x_ref, i4_y_ref;
1188     WORD32 i4_x, i4_y;
1189     WORD32 i4_ref_mb_type;
1190     WORD16 *pi2_ref_data_byte;
1191     WORD16 *pi2_ref_array_temp;
1192 
1193     for(i4_y = 0; i4_y < i4_refarray_ht; i4_y++)
1194     {
1195         for(i4_x = 0; i4_x < i4_refarray_wd; i4_x++)
1196         {
1197             i4_y_ref = i4_y;
1198             i4_x_ref = i4_x;
1199 
1200             i4_ref_mb_type = i4_ref_mb_type_q0;
1201             if(i4_x >= i4_mb_quard1_part_x)
1202             {
1203                 if(i4_y < i4_mb_quard1_part_y)
1204                 {
1205                     i4_ref_mb_type = i4_ref_mb_type_q1;
1206                 }
1207                 else if(i4_y >= i4_mb_quard1_part_y)
1208                 {
1209                     i4_ref_mb_type = i4_ref_mb_type_q3;
1210                 }
1211             }
1212             else if(i4_x < i4_mb_quard1_part_x)
1213             {
1214                 if(i4_y >= i4_mb_quard1_part_y)
1215                 {
1216                     i4_ref_mb_type = i4_ref_mb_type_q2;
1217                 }
1218             }
1219 
1220             /****************************************************************/
1221             /* Reference layer Residual Buffer is maintained as 8-bit data  */
1222             /* Buffer and 1-bit sign bit packed buffer. Sign Byte is read   */
1223             /* and sign of the data sample is extracted depending upon bit  */
1224             /* postition.                                                   */
1225             /****************************************************************/
1226 
1227             /* update the buffer pointers to appropriate locations */
1228             pi2_ref_array_temp = pi2_ref_array + i4_x;
1229             pi2_ref_array_temp += i4_y * i4_refarray_wd;
1230 
1231             /* extract the residual value and fill the buffer */
1232             if(SVC_INTER_MB == i4_ref_mb_type)
1233             {
1234                 /* derive the reference data pointers */
1235                 pi2_ref_data_byte = pi2_inp_data + (i4_x_ref << i4_chroma_flag);
1236                 pi2_ref_data_byte += i4_y_ref * i4_inp_data_stride;
1237 
1238                 /* store the residual value */
1239                 *pi2_ref_array_temp = (WORD16) (*pi2_ref_data_byte);
1240             }
1241             else
1242             {
1243                 /* if non inter MB then store the 0 */
1244                 *pi2_ref_array_temp = 0;
1245             }
1246         }
1247     }
1248 }
1249 
isvcd_residual_reflayer_const_boundary_mb(WORD16 * pi2_inp_data,WORD32 i4_inp_data_stride,WORD16 * pi2_ref_array,WORD32 i4_refarray_wd,WORD32 i4_refarray_ht,WORD32 i4_ref_wd,WORD32 i4_ref_ht,WORD32 i4_x_offset,WORD32 i4_y_offset,WORD32 i4_ref_mb_type_q0,WORD32 i4_ref_mb_type_q1,WORD32 i4_ref_mb_type_q2,WORD32 i4_ref_mb_type_q3,WORD32 i4_mb_quard1_part_x,WORD32 i4_mb_quard1_part_y,WORD32 i4_chroma_flag)1250 void isvcd_residual_reflayer_const_boundary_mb(WORD16 *pi2_inp_data, WORD32 i4_inp_data_stride,
1251                                                WORD16 *pi2_ref_array, WORD32 i4_refarray_wd,
1252                                                WORD32 i4_refarray_ht, WORD32 i4_ref_wd,
1253                                                WORD32 i4_ref_ht, WORD32 i4_x_offset,
1254                                                WORD32 i4_y_offset, WORD32 i4_ref_mb_type_q0,
1255                                                WORD32 i4_ref_mb_type_q1, WORD32 i4_ref_mb_type_q2,
1256                                                WORD32 i4_ref_mb_type_q3, WORD32 i4_mb_quard1_part_x,
1257                                                WORD32 i4_mb_quard1_part_y, WORD32 i4_chroma_flag)
1258 {
1259     WORD32 i4_x_ref, i4_y_ref;
1260     WORD32 i4_x, i4_y;
1261     WORD16 *pi2_ref_data_byte;
1262     WORD16 *pi2_ref_array_temp;
1263 
1264     /*Quard 0*/
1265     for(i4_y = 0; i4_y < i4_mb_quard1_part_y; i4_y++)
1266     {
1267         for(i4_x = 0; i4_x < i4_mb_quard1_part_x; i4_x++)
1268         {
1269             i4_y_ref = MAX(0, MIN(i4_ref_ht - 1, i4_y + i4_y_offset));
1270             i4_x_ref = MAX(0, MIN(i4_ref_wd - 1, i4_x + i4_x_offset));
1271 
1272             /****************************************************************/
1273             /* Reference layer Residual Buffer is maintained as 8-bit data  */
1274             /* Buffer and 1-bit sign bit packed buffer. Sign Byte is read   */
1275             /* and sign of the data sample is extracted depending upon bit  */
1276             /* postition.                                                   */
1277             /****************************************************************/
1278 
1279             /* update the buffer pointers to appropriate locations */
1280             pi2_ref_array_temp = pi2_ref_array + i4_x;
1281             pi2_ref_array_temp += i4_y * i4_refarray_wd;
1282 
1283             /* extract the residual value and fill the buffer */
1284             if(SVC_INTER_MB == i4_ref_mb_type_q0)
1285             {
1286                 /* input pointer will be pointing to (xoffset,yoffset) */
1287                 /* So subtract the correction to reference location    */
1288                 if(0 <= i4_x_offset)
1289                 {
1290                     /* if only inside frame dimension */
1291                     i4_x_ref = i4_x_ref - i4_x_offset;
1292                 }
1293 
1294                 if(0 <= i4_y_offset)
1295                 {
1296                     /* if only inside frame dimension */
1297                     i4_y_ref = i4_y_ref - i4_y_offset;
1298                 }
1299                 /* derive the reference data pointers */
1300 
1301                 pi2_ref_data_byte = pi2_inp_data + (i4_x_ref << i4_chroma_flag);
1302                 pi2_ref_data_byte += i4_y_ref * i4_inp_data_stride;
1303 
1304                 /* store the residual value */
1305                 *pi2_ref_array_temp = (WORD16) (*pi2_ref_data_byte);
1306             }
1307             else
1308             {
1309                 /* if non inter MB then store the 0 */
1310                 *pi2_ref_array_temp = 0;
1311             }
1312         }
1313     }
1314 
1315     /*Quard 1*/
1316     for(i4_y = 0; i4_y < i4_mb_quard1_part_y; i4_y++)
1317     {
1318         for(i4_x = i4_mb_quard1_part_x; i4_x < i4_refarray_wd; i4_x++)
1319         {
1320             i4_y_ref = MAX(0, MIN(i4_ref_ht - 1, i4_y + i4_y_offset));
1321             i4_x_ref = MAX(0, MIN(i4_ref_wd - 1, i4_x + i4_x_offset));
1322 
1323             /****************************************************************/
1324             /* Reference layer Residual Buffer is maintained as 8-bit data  */
1325             /* Buffer and 1-bit sign bit packed buffer. Sign Byte is read   */
1326             /* and sign of the data sample is extracted depending upon bit  */
1327             /* postition.                                                   */
1328             /****************************************************************/
1329 
1330             /* update the buffer pointers to appropriate locations */
1331             pi2_ref_array_temp = pi2_ref_array + i4_x;
1332             pi2_ref_array_temp += i4_y * i4_refarray_wd;
1333 
1334             /* extract the residual value and fill the buffer */
1335             if(SVC_INTER_MB == i4_ref_mb_type_q1)
1336             {
1337                 /* input pointer will be pointing to (xoffset,yoffset) */
1338                 /* So subtract the correction to reference location    */
1339                 if(0 <= i4_x_offset)
1340                 {
1341                     /* if only inside frame dimension */
1342                     i4_x_ref = i4_x_ref - i4_x_offset;
1343                 }
1344                 if(0 <= i4_y_offset)
1345                 {
1346                     /* if only inside frame dimension */
1347                     i4_y_ref = i4_y_ref - i4_y_offset;
1348                 }
1349                 /* derive the reference data pointers */
1350 
1351                 pi2_ref_data_byte = pi2_inp_data + (i4_x_ref << i4_chroma_flag);
1352                 pi2_ref_data_byte += i4_y_ref * i4_inp_data_stride;
1353 
1354                 /* store the residual value */
1355                 *pi2_ref_array_temp = (WORD16) (*pi2_ref_data_byte);
1356             }
1357             else
1358             {
1359                 /* if non inter MB then store the 0 */
1360                 *pi2_ref_array_temp = 0;
1361             }
1362         }
1363     }
1364 
1365     /*Quard 2*/
1366     for(i4_y = i4_mb_quard1_part_y; i4_y < i4_refarray_ht; i4_y++)
1367     {
1368         for(i4_x = 0; i4_x < i4_mb_quard1_part_x; i4_x++)
1369         {
1370             i4_y_ref = MAX(0, MIN(i4_ref_ht - 1, i4_y + i4_y_offset));
1371             i4_x_ref = MAX(0, MIN(i4_ref_wd - 1, i4_x + i4_x_offset));
1372 
1373             /****************************************************************/
1374             /* Reference layer Residual Buffer is maintained as 8-bit data  */
1375             /* Buffer and 1-bit sign bit packed buffer. Sign Byte is read   */
1376             /* and sign of the data sample is extracted depending upon bit  */
1377             /* postition.                                                   */
1378             /****************************************************************/
1379 
1380             /* update the buffer pointers to appropriate locations */
1381             pi2_ref_array_temp = pi2_ref_array + i4_x;
1382             pi2_ref_array_temp += i4_y * i4_refarray_wd;
1383 
1384             /* extract the residual value and fill the buffer */
1385             if(SVC_INTER_MB == i4_ref_mb_type_q2)
1386             {
1387                 /* input pointer will be pointing to (xoffset,yoffset) */
1388                 /* So subtract the correction to reference location    */
1389                 if(0 <= i4_x_offset)
1390                 {
1391                     /* if only inside frame dimension */
1392                     i4_x_ref = i4_x_ref - i4_x_offset;
1393                 }
1394                 if(0 <= i4_y_offset)
1395                 {
1396                     /* if only inside frame dimension */
1397                     i4_y_ref = i4_y_ref - i4_y_offset;
1398                 }
1399                 /* derive the reference data pointers */
1400                 pi2_ref_data_byte = pi2_inp_data + (i4_x_ref << i4_chroma_flag);
1401                 pi2_ref_data_byte += i4_y_ref * i4_inp_data_stride;
1402 
1403                 /* store the residual value */
1404                 *pi2_ref_array_temp = (WORD16) (*pi2_ref_data_byte);
1405             }
1406             else
1407             {
1408                 /* if non inter MB then store the 0 */
1409                 *pi2_ref_array_temp = 0;
1410             }
1411         }
1412     }
1413 
1414     /*Quard 3*/
1415     for(i4_y = i4_mb_quard1_part_y; i4_y < i4_refarray_ht; i4_y++)
1416     {
1417         for(i4_x = i4_mb_quard1_part_x; i4_x < i4_refarray_wd; i4_x++)
1418         {
1419             i4_y_ref = MAX(0, MIN(i4_ref_ht - 1, i4_y + i4_y_offset));
1420             i4_x_ref = MAX(0, MIN(i4_ref_wd - 1, i4_x + i4_x_offset));
1421 
1422             /****************************************************************/
1423             /* Reference layer Residual Buffer is maintained as 8-bit data  */
1424             /* Buffer and 1-bit sign bit packed buffer. Sign Byte is read   */
1425             /* and sign of the data sample is extracted depending upon bit  */
1426             /* postition.                                                   */
1427             /****************************************************************/
1428 
1429             /* update the buffer pointers to appropriate locations */
1430             pi2_ref_array_temp = pi2_ref_array + i4_x;
1431             pi2_ref_array_temp += i4_y * i4_refarray_wd;
1432 
1433             /* extract the residual value and fill the buffer */
1434             if(SVC_INTER_MB == i4_ref_mb_type_q3)
1435             {
1436                 /* input pointer will be pointing to (xoffset,yoffset) */
1437                 /* So subtract the correction to reference location    */
1438                 if(0 <= i4_x_offset)
1439                 {
1440                     /* if only inside frame dimension */
1441                     i4_x_ref = i4_x_ref - i4_x_offset;
1442                 }
1443                 if(0 <= i4_y_offset)
1444                 {
1445                     /* if only inside frame dimension */
1446                     i4_y_ref = i4_y_ref - i4_y_offset;
1447                 }
1448                 /* derive the reference data pointers */
1449                 pi2_ref_data_byte = pi2_inp_data + (i4_x_ref << i4_chroma_flag);
1450                 pi2_ref_data_byte += i4_y_ref * i4_inp_data_stride;
1451 
1452                 /* store the residual value */
1453                 *pi2_ref_array_temp = (WORD16) (*pi2_ref_data_byte);
1454             }
1455             else
1456             {
1457                 /* if non inter MB then store the 0 */
1458                 *pi2_ref_array_temp = 0;
1459             }
1460         }
1461     }
1462 }
1463 
1464 /*****************************************************************************/
1465 /*                                                                           */
1466 /*  Function Name : isvcd_residual_reflayer_const                             */
1467 /*                                                                           */
1468 /*  Description   :  This function constructs the reference array buffer     */
1469 /*                    used for residual resampling of a component in an MB   */
1470 /*                                                                           */
1471 /*  Inputs        : pv_residual_samp_ctxt: intra sampling context            */
1472 /*                  pu1_inp : input (reference layer data)                   */
1473 /*                  i4_inp_stride : input buffer stride                      */
1474 /*                  pu1_inp_bitmap : input (reference layer sign bits)       */
1475 /*                  i4_inp_stride : input buffer stride                      */
1476 /*                  ps_ref_mb_mode : ref layer mb mode buffer desc           */
1477 /*                  pi4_refarr_wd : pointer to store the reference array WD  */
1478 /*                  pi4_refarr_ht : pointer to store the reference array HT  */
1479 /*                  pi4_x_offset : pointer to store the reference X offset   */
1480 /*                  pi4_y_offset : pointer to store the reference Y offset   */
1481 /*                  ps_coord     : mb co-ordinate structure                  */
1482 /*                  i4_chroma_flag : chroma processing flag                  */
1483 /*  Globals       : none                                                     */
1484 /*  Processing    : it fills the reference layer data if they are falling in */
1485 /*                  INTRA MB region. If all the pixels are not filled  it    */
1486 /*                  calls the border extension algorithm to fill them        */
1487 /*  Outputs       : none                                                     */
1488 /*  Returns       : none                                                     */
1489 /*                                                                           */
1490 /*  Issues        : none                                                     */
1491 /*                                                                           */
1492 /*  Revision History:                                                        */
1493 /*                                                                           */
1494 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1495 /*         06 07 2021   vijayakumar          creation                        */
1496 /*                                                                           */
1497 /*****************************************************************************/
isvcd_residual_reflayer_const(void * pv_residual_samp_ctxt,WORD16 * pi2_inp_data,WORD32 i4_inp_data_stride,mem_element_t * ps_ref_mb_mode,WORD32 * pi4_refarr_wd,mb_coord_t * ps_coord,WORD32 i4_chroma_flag)1498 WORD32 isvcd_residual_reflayer_const(void *pv_residual_samp_ctxt, WORD16 *pi2_inp_data,
1499                                      WORD32 i4_inp_data_stride, mem_element_t *ps_ref_mb_mode,
1500                                      WORD32 *pi4_refarr_wd, mb_coord_t *ps_coord,
1501                                      WORD32 i4_chroma_flag)
1502 {
1503     residual_sampling_ctxt_t *ps_ctxt;
1504     res_lyr_ctxt *ps_lyr_ctxt;
1505     WORD8 *pi1_ref_mb_modes;
1506     WORD32 i4_ref_mode_stride;
1507     WORD32 i4_element_size;
1508     WORD32 i4_ref_wd;
1509     WORD32 i4_ref_ht;
1510     WORD32 i4_x_offset;
1511     WORD32 i4_y_offset;
1512     WORD32 i4_refarray_wd;
1513     WORD32 i4_refarray_ht;
1514     WORD8 i1_edge_mb;
1515     WORD16 *pi2_ref_array;
1516     WORD32 i4_mb_sft;
1517     WORD32 i4_mb_x, i4_mb_y;
1518     WORD32 i4_mb_x_strt, i4_mb_y_strt;
1519     WORD32 i4_mb_quard1_part_x, i4_mb_quard1_part_y;
1520     WORD8 *pi1_ref_mb_modes_incr;
1521     WORD8 *pi1_ref_mb_modes_incr_temp;
1522     inter_lyr_mb_prms_t *ps_inter_lyr_mb_prms;
1523 
1524     WORD32 i4_mb_wd;
1525     WORD32 i4_mb_ht;
1526     WORD32 i4_x_ref, i4_y_ref;
1527     WORD32 i4_ref_mb_type_q0, i4_ref_mb_type_q1, i4_ref_mb_type_q2, i4_ref_mb_type_q3;
1528     WORD8 i1_mb_mode_q0, i1_mb_mode_q1, i1_mb_mode_q2, i1_mb_mode_q3;
1529     WORD32 ret;
1530 
1531     ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
1532     ps_lyr_ctxt = &ps_ctxt->as_res_lyrs[ps_ctxt->i4_res_lyr_id];
1533     pi2_ref_array = ps_ctxt->pi2_refarray_buffer;
1534 
1535     pi1_ref_mb_modes = (WORD8 *) ps_ref_mb_mode->pv_buffer;
1536     i4_ref_mode_stride = ps_ref_mb_mode->i4_num_element_stride;
1537     i4_element_size = ps_ref_mb_mode->i4_element_size;
1538 
1539     if(NULL == pi1_ref_mb_modes)
1540     {
1541         return NOT_OK;
1542     }
1543 
1544     i4_mb_wd = MB_WIDTH >> i4_chroma_flag;
1545     i4_mb_ht = MB_HEIGHT >> i4_chroma_flag;
1546 
1547     /* ----------------------------------------------------------------- */
1548     /* Deriving the parameters required for further processing           */
1549     /* ----------------------------------------------------------------- */
1550     {
1551         ref_mb_map_t *ps_x_off_len;
1552         ref_mb_map_t *ps_y_off_len;
1553         WORD32 i4_mbaddr_x;
1554         WORD32 i4_mbaddr_y;
1555         WORD32 i4_base_width;
1556         WORD32 i4_base_height;
1557         residual_samp_map_ctxt_t *ps_map_ctxt;
1558 
1559         if(1 == i4_chroma_flag)
1560             ps_map_ctxt = &ps_lyr_ctxt->s_chroma_map_ctxt;
1561         else
1562             ps_map_ctxt = &ps_lyr_ctxt->s_luma_map_ctxt;
1563 
1564         i4_mbaddr_y = ps_coord->u2_mb_y;
1565         i4_mbaddr_x = ps_coord->u2_mb_x;
1566         i4_base_width = ps_lyr_ctxt->i4_ref_width;
1567         i4_base_height = ps_lyr_ctxt->i4_ref_height;
1568         i4_ref_wd = i4_base_width >> i4_chroma_flag;
1569         i4_ref_ht = i4_base_height >> i4_chroma_flag;
1570 
1571         /* --------------------------------------------------------------------- */
1572         /* Extracting information from the mapping context                       */
1573         /* --------------------------------------------------------------------- */
1574         ps_x_off_len = ps_map_ctxt->ps_x_offset_length;
1575         ps_y_off_len = ps_map_ctxt->ps_y_offset_length;
1576         i4_x_offset = ps_x_off_len[i4_mbaddr_x].i2_offset;
1577         i4_y_offset = ps_y_off_len[i4_mbaddr_y].i2_offset;
1578         i4_refarray_wd = ps_x_off_len[i4_mbaddr_x].i2_length;
1579         i4_refarray_ht = ps_y_off_len[i4_mbaddr_y].i2_length;
1580     }
1581 
1582     /* Call the module to fill the increments based on transform blocks */
1583     ret = isvcd_ref_layer_ptr_incr(pi1_ref_mb_modes, i4_ref_mode_stride, i4_element_size,
1584                                    i4_x_offset, i4_y_offset, i4_refarray_wd, i4_refarray_ht,
1585                                    ps_ctxt->pu1_ref_x_ptr_incr, ps_ctxt->pu1_ref_y_ptr_incr,
1586                                    i4_chroma_flag);
1587 
1588     if(ret != OK)
1589     {
1590         return ret;
1591     }
1592     i4_mb_sft = (MB_WIDTH_SHIFT - i4_chroma_flag);
1593 
1594     /* --------------------------------------------------------------------- */
1595     /* MB Level Resampling for the MB - Pointers sent for MB in both layers  */
1596     /* This has been written according to the dyadic case                    */
1597     /* --------------------------------------------------------------------- */
1598     i4_y_ref = MAX(0, MIN(i4_ref_ht - 1, 0 + i4_y_offset));
1599     i4_x_ref = MAX(0, MIN(i4_ref_wd - 1, 0 + i4_x_offset));
1600     i4_mb_x_strt = i4_x_ref % i4_mb_wd;
1601     i4_mb_y_strt = i4_y_ref % i4_mb_ht;
1602 
1603     i4_mb_quard1_part_x = i4_mb_wd - i4_mb_x_strt;
1604     i4_mb_quard1_part_y = i4_mb_ht - i4_mb_y_strt;
1605     if(!(i4_mb_quard1_part_x >= 0))
1606     {
1607         return NOT_OK;
1608     }
1609     if(!(i4_mb_quard1_part_y >= 0))
1610     {
1611         return NOT_OK;
1612     }
1613 
1614     i4_mb_x = (i4_x_ref >> i4_mb_sft);
1615     i4_mb_y = (i4_y_ref >> i4_mb_sft);
1616 
1617     /* get the location of the byte which has the current mb mode */
1618     pi1_ref_mb_modes_incr = pi1_ref_mb_modes + (i4_mb_y * i4_ref_mode_stride * i4_element_size);
1619     pi1_ref_mb_modes_incr += (i4_mb_x * i4_element_size);
1620     ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr;
1621     i1_mb_mode_q0 = ps_inter_lyr_mb_prms->i1_mb_mode;
1622     i1_mb_mode_q1 = i1_mb_mode_q0;
1623     i1_mb_mode_q2 = i1_mb_mode_q0;
1624     i1_mb_mode_q3 = i1_mb_mode_q0;
1625 
1626     pi1_ref_mb_modes_incr_temp = pi1_ref_mb_modes_incr;
1627     if(i4_mb_quard1_part_x > 0)
1628     {
1629         pi1_ref_mb_modes_incr_temp = pi1_ref_mb_modes_incr + i4_element_size;
1630         ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr_temp;
1631         i1_mb_mode_q1 = ps_inter_lyr_mb_prms->i1_mb_mode;
1632     }
1633 
1634     if(i4_mb_quard1_part_y > 0)
1635     {
1636         pi1_ref_mb_modes_incr_temp = pi1_ref_mb_modes_incr + (i4_ref_mode_stride * i4_element_size);
1637         ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr_temp;
1638         i1_mb_mode_q2 = ps_inter_lyr_mb_prms->i1_mb_mode;
1639     }
1640 
1641     if((i4_mb_quard1_part_x > 0) && (i4_mb_quard1_part_y > 0))
1642     {
1643         pi1_ref_mb_modes_incr_temp =
1644             pi1_ref_mb_modes_incr + (i4_ref_mode_stride * i4_element_size) + i4_element_size;
1645         ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) pi1_ref_mb_modes_incr_temp;
1646         i1_mb_mode_q3 = ps_inter_lyr_mb_prms->i1_mb_mode;
1647     }
1648 
1649     i4_ref_mb_type_q0 = (i1_mb_mode_q0 <= SVC_INTER_MB) ? SVC_INTER_MB : SVC_INTRA_MB;
1650     i4_ref_mb_type_q1 = (i1_mb_mode_q1 <= SVC_INTER_MB) ? SVC_INTER_MB : SVC_INTRA_MB;
1651     i4_ref_mb_type_q2 = (i1_mb_mode_q2 <= SVC_INTER_MB) ? SVC_INTER_MB : SVC_INTRA_MB;
1652     i4_ref_mb_type_q3 = (i1_mb_mode_q3 <= SVC_INTER_MB) ? SVC_INTER_MB : SVC_INTRA_MB;
1653 
1654     i1_edge_mb = (ps_coord->u2_mb_x == 0 || ps_coord->u2_mb_y == 0 ||
1655                   (ps_coord->u2_mb_x == ((ps_lyr_ctxt->i4_curr_width >> MB_WIDTH_SHIFT) - 1)) ||
1656                   (ps_coord->u2_mb_y == ((ps_lyr_ctxt->i4_curr_height >> MB_HEIGHT_SHIFT) - 1)));
1657     if(i1_edge_mb)
1658     {
1659         ps_ctxt->pf_residual_reflayer_const_boundary_mb(
1660             pi2_inp_data, i4_inp_data_stride, pi2_ref_array, i4_refarray_wd, i4_refarray_ht,
1661             i4_ref_wd, i4_ref_ht, i4_x_offset, i4_y_offset, i4_ref_mb_type_q0, i4_ref_mb_type_q1,
1662             i4_ref_mb_type_q2, i4_ref_mb_type_q3, i4_mb_quard1_part_x, i4_mb_quard1_part_y,
1663             i4_chroma_flag);
1664     }
1665     else
1666     {
1667         ps_ctxt->pf_residual_reflayer_const_non_boundary_mb(
1668             pi2_inp_data, i4_inp_data_stride, pi2_ref_array, i4_refarray_wd, i4_refarray_ht,
1669             i4_ref_mb_type_q0, i4_ref_mb_type_q1, i4_ref_mb_type_q2, i4_ref_mb_type_q3,
1670             i4_mb_quard1_part_x, i4_mb_quard1_part_y, i4_chroma_flag);
1671     }
1672     /* store the values into the place holders */
1673     *pi4_refarr_wd = i4_refarray_wd;
1674 
1675     return OK;
1676 }
1677 
1678 /*****************************************************************************/
1679 /*                                                                           */
1680 /*  Function Name : isvcd_interpolate_residual                                */
1681 /*                                                                           */
1682 /*  Description   : This function takes the refernce array buffer and perform*/
1683 /*                    interpolation of a component to find the residual      */
1684 /*                     resampled value                                       */
1685 /*  Inputs        : pv_residual_samp_ctxt : residual sampling context        */
1686 /*                  pi2_out : output buffer pointer                          */
1687 /*                  i4_out_stride : output buffer stride                     */
1688 /*                  i4_refarray_wd : reference array width                   */
1689 /*                  i4_x_offset : offset in reference layer in horz direction*/
1690 /*                  ps_coord : current mb co-ordinate                        */
1691 /*                  i4_chroma_flag : chroma processing flag                  */
1692 /*  Globals       : none                                                     */
1693 /*  Processing    : it does the interpolation in vertical direction followed */
1694 /*                  by horizontal direction                                  */
1695 /*  Outputs       : resampled pixels                                         */
1696 /*  Returns       : none                                                     */
1697 /*                                                                           */
1698 /*  Issues        : none                                                     */
1699 /*                                                                           */
1700 /*  Revision History:                                                        */
1701 /*                                                                           */
1702 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1703 /*         06 07 2021   vijayakumar          creation                        */
1704 /*                                                                           */
1705 /*****************************************************************************/
isvcd_interpolate_residual(void * pv_residual_samp_ctxt,WORD16 * pi2_out,WORD32 i4_out_stride,WORD32 i4_refarray_wd,UWORD16 u2_mb_x,UWORD16 u2_mb_y,WORD32 i4_chroma_flag)1706 void isvcd_interpolate_residual(void *pv_residual_samp_ctxt, WORD16 *pi2_out, WORD32 i4_out_stride,
1707                                 WORD32 i4_refarray_wd, UWORD16 u2_mb_x, UWORD16 u2_mb_y,
1708                                 WORD32 i4_chroma_flag)
1709 {
1710     residual_sampling_ctxt_t *ps_ctxt;
1711     residual_samp_map_ctxt_t *ps_map_ctxt;
1712     res_lyr_ctxt *ps_lyr_ctxt;
1713     ref_pixel_map_t *ps_x_pos_phase;
1714     ref_pixel_map_t *ps_y_pos_phase;
1715 
1716     WORD32 i4_x, i4_y;
1717     WORD32 i4_frm_mb_x, i4_frm_mb_y;
1718     WORD32 i4_temp_array_ht;
1719     WORD32 i4_mb_wd;
1720     WORD32 i4_mb_ht;
1721     WORD16 *pi2_ref_array;
1722     UWORD8 *pu1_ref_x_ptr_incr, *pu1_ref_y_ptr_incr;
1723 
1724     ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
1725     ps_lyr_ctxt = &ps_ctxt->as_res_lyrs[ps_ctxt->i4_res_lyr_id];
1726     pi2_ref_array = ps_ctxt->pi2_refarray_buffer;
1727     pu1_ref_x_ptr_incr = ps_ctxt->pu1_ref_x_ptr_incr;
1728     pu1_ref_y_ptr_incr = ps_ctxt->pu1_ref_y_ptr_incr;
1729 
1730     /* --------------------------------------------------------------------- */
1731     /* Extracting information from the mapping context                       */
1732     /* --------------------------------------------------------------------- */
1733     if(1 == i4_chroma_flag)
1734         ps_map_ctxt = &ps_lyr_ctxt->s_chroma_map_ctxt;
1735     else
1736         ps_map_ctxt = &ps_lyr_ctxt->s_luma_map_ctxt;
1737 
1738     i4_mb_wd = MB_WIDTH >> i4_chroma_flag;
1739     i4_mb_ht = MB_HEIGHT >> i4_chroma_flag;
1740 
1741     ps_x_pos_phase = ps_map_ctxt->ps_x_pos_phase;
1742     ps_y_pos_phase = ps_map_ctxt->ps_y_pos_phase;
1743     i4_temp_array_ht = i4_mb_ht;
1744     i4_frm_mb_y = u2_mb_y * i4_mb_ht;
1745     i4_frm_mb_x = u2_mb_x * i4_mb_wd;
1746 
1747     /* --------------------------------------------------------------------- */
1748     /* Loop for interpolation                                                */
1749     /* --------------------------------------------------------------------- */
1750     for(i4_y = 0; i4_y < (i4_temp_array_ht); i4_y++)
1751     {
1752         for(i4_x = 0; i4_x < (i4_mb_wd); i4_x++)
1753         {
1754             WORD32 i4_i;
1755             WORD32 i4_y_ref;
1756             WORD32 i4_y_phase;
1757             WORD32 i4_x_ref;
1758             WORD32 i4_x_phase;
1759             WORD32 i4_x_ref_round;
1760             WORD16 *pi2_out_curr;
1761             WORD32 ai4_temp_pred[2] = {0};
1762             UWORD8 *pu1_ref_y_ptr_incr_temp;
1763             WORD32 *pi4_temp_pred;
1764             UWORD8 u1_incr_y;
1765             WORD16 i2_res;
1766 
1767             /* derive the current output pointer */
1768             pi2_out_curr = pi2_out + (i4_x << i4_chroma_flag) + (i4_y * i4_out_stride);
1769 
1770             /* -------------------------------------------------------------- */
1771             /* Finding the offset                                             */
1772             /* -------------------------------------------------------------- */
1773             i4_y_ref = ps_y_pos_phase[i4_y + i4_frm_mb_y].i2_ref_pos;
1774             i4_y_phase = ps_y_pos_phase[i4_y + i4_frm_mb_y].i2_phase;
1775             i4_x_ref = ps_x_pos_phase[i4_x + i4_frm_mb_x].i2_ref_pos;
1776             i4_x_phase = ps_x_pos_phase[i4_x + i4_frm_mb_x].i2_phase;
1777 
1778             /* horizontal processing*/
1779             for(i4_i = 0; i4_i < 2; i4_i++)
1780             {
1781                 UWORD8 *pu1_ref_x_ptr_incr_temp;
1782                 UWORD8 u1_incr;
1783                 WORD16 *pi2_ref_array_1, *pi2_ref_array_2;
1784 
1785                 /* derive appropriate pointers */
1786                 pu1_ref_x_ptr_incr_temp = pu1_ref_x_ptr_incr + i4_x_ref;
1787                 pu1_ref_x_ptr_incr_temp += ((i4_y_ref + i4_i) * i4_refarray_wd);
1788                 u1_incr = *pu1_ref_x_ptr_incr_temp;
1789                 pi2_ref_array_1 = pi2_ref_array + i4_x_ref;
1790                 pi2_ref_array_1 += ((i4_y_ref + i4_i) * i4_refarray_wd);
1791 
1792                 if(!u1_incr)
1793                 {
1794                     pi2_ref_array_1 += (i4_x_phase >> 3);
1795                 }
1796 
1797                 pi2_ref_array_2 = pi2_ref_array_1 + u1_incr;
1798                 ai4_temp_pred[i4_i] =
1799                     (16 - i4_x_phase) * (*pi2_ref_array_1) + i4_x_phase * (*pi2_ref_array_2);
1800             }
1801 
1802             /* vertical processing */
1803             i4_x_ref_round = (i4_x_ref + (i4_x_phase >> 3));
1804             pu1_ref_y_ptr_incr_temp =
1805                 pu1_ref_y_ptr_incr + i4_x_ref_round + (i4_y_ref * i4_refarray_wd);
1806             u1_incr_y = *pu1_ref_y_ptr_incr_temp;
1807 
1808             pi4_temp_pred = &ai4_temp_pred[0];
1809             if(!u1_incr_y)
1810             {
1811                 pi4_temp_pred += (i4_y_phase >> 3);
1812             }
1813 
1814             i2_res = (((16 - i4_y_phase) * pi4_temp_pred[0] +
1815                        i4_y_phase * pi4_temp_pred[u1_incr_y] + 128) >>
1816                       8);
1817 
1818             /* store back the final residual */
1819             *pi2_out_curr = i2_res;
1820         } /* end of loop over width */
1821     }     /* end of loop over height */
1822 
1823     return;
1824 } /* End of Interpolation Function */
1825 /*****************************************************************************/
1826 /*                                                                           */
1827 /*  Function Name : isvcd_residual_samp_mb                                    */
1828 /*                                                                           */
1829 /*  Description   : MB level function whcih perform the residual resampling  */
1830 /*                  of data of an MB (luma and chroma insclusive)            */
1831 /*                                                                           */
1832 /*  Inputs        : pv_residual_samp_ctxt : residual sampling context        */
1833 /*                  ps_ref_luma : reference layer luma data buffer desc      */
1834 /*                  ps_ref_chroma : reference layer chroma data buffer desc  */
1835 /*                  ps_ref_luma_bitmap : ref layer luma bit map buffer desc  */
1836 /*                  ps_ref_chroma_bitmap : ref layer chroma bit map buff des */
1837 /*                  ps_ref_mb_mode : ref layer mb mode map buff desc         */
1838 /*                  ps_out_luma : current layer out luma buffer desc         */
1839 /*                  ps_out_chroma : current layer out chroma buffer desc     */
1840 /*                  ps_mb_coord : current mb coorinate                       */
1841 /*  Globals       : none                                                     */
1842 /*  Processing    : it calls the reference layer construction followed by    */
1843 /*                   interplaotion function for luma and cb and cr           */
1844 /*  Outputs       : inter resampled data of current MB                       */
1845 /*  Returns       : none                                                     */
1846 /*                                                                           */
1847 /*  Issues        : none                                                     */
1848 /*                                                                           */
1849 /*  Revision History:                                                        */
1850 /*                                                                           */
1851 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1852 /*         26 06 2021   vijayakumar          creation                        */
1853 /*                                                                           */
1854 /*****************************************************************************/
isvcd_residual_samp_mb(void * pv_residual_samp_ctxt,mem_element_t * ps_ref_luma,mem_element_t * ps_ref_chroma,mem_element_t * ps_ref_mb_mode,mem_element_t * ps_out_luma,mem_element_t * ps_out_chroma,UWORD16 u2_mb_x,UWORD16 u2_mb_y)1855 WORD32 isvcd_residual_samp_mb(void *pv_residual_samp_ctxt, mem_element_t *ps_ref_luma,
1856                               mem_element_t *ps_ref_chroma, mem_element_t *ps_ref_mb_mode,
1857                               mem_element_t *ps_out_luma, mem_element_t *ps_out_chroma,
1858                               UWORD16 u2_mb_x, UWORD16 u2_mb_y)
1859 {
1860     /* --------------------------------------------------------------------- */
1861     /* I/O buffer params                                                     */
1862     /* --------------------------------------------------------------------- */
1863     residual_sampling_ctxt_t *ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
1864     WORD16 *pi2_inp;
1865     WORD16 *pi2_out;
1866     WORD32 i4_inp_stride;
1867     WORD32 i4_out_stride;
1868     WORD32 i4_refarray_wd;
1869     mb_coord_t s_mb_coord = {0};
1870     WORD32 ret;
1871     s_mb_coord.u2_mb_x = u2_mb_x;
1872     s_mb_coord.u2_mb_y = u2_mb_y;
1873 
1874     /* --------------------------------------------------------------------- */
1875     /* LUMA PROCESSING                                                        */
1876     /* --------------------------------------------------------------------- */
1877     pi2_inp = (WORD16 *) ps_ref_luma->pv_buffer;
1878     pi2_out = (WORD16 *) ps_out_luma->pv_buffer;
1879     i4_inp_stride = ps_ref_luma->i4_num_element_stride;
1880     i4_out_stride = ps_out_luma->i4_num_element_stride;
1881 
1882     /* ------- Constructing refSampleArray ----------------------- */
1883     ret = isvcd_residual_reflayer_const(pv_residual_samp_ctxt, pi2_inp, i4_inp_stride,
1884                                         ps_ref_mb_mode, &i4_refarray_wd, &s_mb_coord, 0);
1885 
1886     if(ret != OK) return ret;
1887     /* ---- Interpolation process for Residual prediction     ------ */
1888     ps_ctxt->pf_interpolate_residual(pv_residual_samp_ctxt, pi2_out, i4_out_stride, i4_refarray_wd,
1889                                      s_mb_coord.u2_mb_x, s_mb_coord.u2_mb_y, 0);
1890 
1891     /* --------------------------------------------------------------------- */
1892     /* CHROMA PROCESSING                                                       */
1893     /* --------------------------------------------------------------------- */
1894     /* CB */
1895     pi2_inp = (WORD16 *) ps_ref_chroma->pv_buffer;
1896     pi2_out = (WORD16 *) ps_out_chroma->pv_buffer;
1897     i4_inp_stride = ps_ref_chroma->i4_num_element_stride;
1898     i4_out_stride = ps_out_chroma->i4_num_element_stride;
1899 
1900     /* ------- Constructing refSampleArray ----------------------- */
1901     ret = isvcd_residual_reflayer_const(pv_residual_samp_ctxt, pi2_inp, i4_inp_stride,
1902                                         ps_ref_mb_mode, &i4_refarray_wd, &s_mb_coord, 1);
1903 
1904     if(ret != OK) return ret;
1905     /* ---- Interpolation process for Residual prediction     ------ */
1906     ps_ctxt->pf_interpolate_residual(pv_residual_samp_ctxt, pi2_out, i4_out_stride, i4_refarray_wd,
1907                                      s_mb_coord.u2_mb_x, s_mb_coord.u2_mb_y, 1);
1908 
1909     /* CR */
1910     pi2_inp += 1;
1911     pi2_out += 1;
1912 
1913     /* ------- Constructing refSampleArray ----------------------- */
1914     ret = isvcd_residual_reflayer_const(pv_residual_samp_ctxt, pi2_inp, i4_inp_stride,
1915                                         ps_ref_mb_mode, &i4_refarray_wd, &s_mb_coord, 1);
1916 
1917     if(ret != OK) return ret;
1918     /* ---- Interpolation process for Residual prediction --------- */
1919     ps_ctxt->pf_interpolate_residual(pv_residual_samp_ctxt, pi2_out, i4_out_stride, i4_refarray_wd,
1920                                      s_mb_coord.u2_mb_x, s_mb_coord.u2_mb_y, 1);
1921     return OK;
1922 }
1923 /*****************************************************************************/
1924 /*                                                                           */
1925 /*  Function Name : isvcd_residual_samp_mb_dyadic                             */
1926 /*                                                                           */
1927 /*  Description   : MB level function whcih perform the residual resampling  */
1928 /*                  of data of an MB (luma and chroma insclusive)            */
1929 /*                  for Dyadic cases                                         */
1930 /*  Inputs        : pv_residual_samp_ctxt : residual sampling context        */
1931 /*                  ps_ref_luma : reference layer luma data buffer desc      */
1932 /*                  ps_ref_chroma : reference layer chroma data buffer desc  */
1933 /*                  ps_ref_luma_bitmap : ref layer luma bit map buffer desc  */
1934 /*                  ps_ref_chroma_bitmap : ref layer chroma bit map buff des */
1935 /*                  ps_ref_mb_mode : ref layer mb mode map buff desc         */
1936 /*                  ps_out_luma : current layer out luma buffer desc         */
1937 /*                  ps_out_chroma : current layer out chroma buffer desc     */
1938 /*                  ps_mb_coord : current mb coorinate                       */
1939 /*  Globals       : none                                                     */
1940 /*  Processing    : it calls the reference layer construction followed by    */
1941 /*                   interplaotion function for luma and cb and cr           */
1942 /*  Outputs       : inter resampled data of current MB                       */
1943 /*  Returns       : none                                                     */
1944 /*                                                                           */
1945 /*  Issues        : none                                                     */
1946 /*                                                                           */
1947 /*  Revision History:                                                        */
1948 /*                                                                           */
1949 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1950 /*         26 06 2021   vijayakumar          creation                        */
1951 /*                                                                           */
1952 /*****************************************************************************/
isvcd_residual_samp_mb_dyadic(void * pv_residual_samp_ctxt,mem_element_t * ps_ref_luma,mem_element_t * ps_ref_chroma,mem_element_t * ps_ref_mb_mode,mem_element_t * ps_out_luma,mem_element_t * ps_out_chroma,UWORD16 u2_mb_x,UWORD16 u2_mb_y)1953 WORD32 isvcd_residual_samp_mb_dyadic(void *pv_residual_samp_ctxt, mem_element_t *ps_ref_luma,
1954                                      mem_element_t *ps_ref_chroma, mem_element_t *ps_ref_mb_mode,
1955                                      mem_element_t *ps_out_luma, mem_element_t *ps_out_chroma,
1956                                      UWORD16 u2_mb_x, UWORD16 u2_mb_y)
1957 {
1958     residual_sampling_ctxt_t *ps_ctxt;
1959     res_lyr_ctxt *ps_lyr_ctxt;
1960     /* --------------------------------------------------------------------- */
1961     /* I/O buffer params                                                     */
1962     /* --------------------------------------------------------------------- */
1963     WORD16 *pi2_inp;
1964     WORD16 *pi2_out;
1965     WORD32 i4_inp_stride;
1966     WORD32 i4_out_stride;
1967     WORD32 i4_luma_nnz;
1968     WORD32 i4_chroma_nnz;
1969     WORD32 i4_tx_size;
1970 
1971     ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
1972     ps_lyr_ctxt = &ps_ctxt->as_res_lyrs[ps_ctxt->i4_res_lyr_id];
1973 
1974     /* --------------------------------------------------------------------- */
1975     /* LUMA PROCESSING                                                        */
1976     /* --------------------------------------------------------------------- */
1977     pi2_inp = (WORD16 *) ps_ref_luma->pv_buffer;
1978     pi2_out = (WORD16 *) ps_out_luma->pv_buffer;
1979     i4_inp_stride = ps_ref_luma->i4_num_element_stride;
1980     i4_out_stride = ps_out_luma->i4_num_element_stride;
1981 
1982     {
1983         WORD32 i4_offset_x, i4_offset_y;
1984         residual_samp_map_ctxt_t *ps_luma_map;
1985         ref_mb_map_t *ps_x_off_len_luma;
1986         ref_mb_map_t *ps_y_off_len_luma;
1987 
1988         ps_luma_map = &ps_lyr_ctxt->s_luma_map_ctxt;
1989         ps_x_off_len_luma = ps_luma_map->ps_x_offset_length;
1990         ps_y_off_len_luma = ps_luma_map->ps_y_offset_length;
1991 
1992         /* get the actual offset for the buffers */
1993         i4_offset_x = ps_x_off_len_luma[u2_mb_x].i2_offset;
1994         i4_offset_y = ps_y_off_len_luma[u2_mb_y].i2_offset;
1995 
1996         {
1997             inter_lyr_mb_prms_t *ps_inter_lyr_mb_prms;
1998             WORD32 i4_mb_x, i4_mb_y;
1999             UWORD16 u2_luma_mask = 0x0033;
2000             UWORD8 u1_chrm_mask = 0x11;
2001             WORD32 i4_luma_rt_sft_amt = 0;
2002             WORD32 i4_chrm_rt_sft_amt = 0;
2003 
2004             i4_mb_x = ((i4_offset_x + 1) >> MB_WIDTH_SHIFT);
2005             i4_mb_y = ((i4_offset_y + 1) >> MB_HEIGHT_SHIFT);
2006 
2007             /* get the location of the byte which has the current mb mode */
2008             ps_inter_lyr_mb_prms = (inter_lyr_mb_prms_t *) ps_ref_mb_mode->pv_buffer;
2009             ps_inter_lyr_mb_prms += i4_mb_x;
2010             ps_inter_lyr_mb_prms += i4_mb_y * ps_ref_mb_mode->i4_num_element_stride;
2011 
2012             /* get the approp block in base layer in horz direction */
2013             if(0 != ((i4_offset_x + 1) & 15))
2014             {
2015                 u2_luma_mask <<= 2;
2016                 i4_luma_rt_sft_amt += 2;
2017                 u1_chrm_mask <<= 1;
2018                 i4_chrm_rt_sft_amt += 1;
2019             }
2020             /* get the approp block in base layer in vert direction */
2021             if(0 != ((i4_offset_y + 1) & 15))
2022             {
2023                 u2_luma_mask <<= 8;
2024                 i4_luma_rt_sft_amt += 8;
2025 
2026                 u1_chrm_mask <<= 2;
2027                 i4_chrm_rt_sft_amt += 2;
2028             }
2029 
2030             /* extract the nnz and store it */
2031             i4_luma_nnz = (ps_inter_lyr_mb_prms->u2_luma_nnz & u2_luma_mask) >> i4_luma_rt_sft_amt;
2032             i4_chroma_nnz =
2033                 (ps_inter_lyr_mb_prms->u1_chroma_nnz & u1_chrm_mask) >> i4_chrm_rt_sft_amt;
2034             i4_tx_size =
2035                 (ps_inter_lyr_mb_prms->i1_tx_size < 0) ? 1 : ps_inter_lyr_mb_prms->i1_tx_size;
2036         }
2037 
2038         /* since in dyadic case the window width and height will be 10x10   */
2039         /* and the window start offsets will be always 1 column left and    */
2040         /* 1 row above the block boundary. so the pointer and the required  */
2041         /* positions are appropriately modified                             */
2042         if(i4_offset_x >= 0)
2043         {
2044             pi2_inp++;
2045         }
2046 
2047         if(i4_offset_y >= 0)
2048         {
2049             pi2_inp += i4_inp_stride;
2050         }
2051 
2052         ps_ctxt->pf_residual_luma_dyadic(pv_residual_samp_ctxt, pi2_inp, i4_inp_stride, pi2_out,
2053                                          i4_out_stride, ps_ref_mb_mode, u2_mb_x, u2_mb_y,
2054                                          i4_luma_nnz, i4_tx_size);
2055     }
2056 
2057     /* --------------------------------------------------------------------- */
2058     /* CHROMA PROCESSING                                                       */
2059     /* --------------------------------------------------------------------- */
2060     /* CB */
2061     pi2_inp = (WORD16 *) ps_ref_chroma->pv_buffer;
2062     pi2_out = (WORD16 *) ps_out_chroma->pv_buffer;
2063     i4_inp_stride = ps_ref_chroma->i4_num_element_stride;
2064     i4_out_stride = ps_out_chroma->i4_num_element_stride;
2065 
2066     /* choose the appropriate chroma processing routine */
2067     if(SVCD_FALSE == ps_lyr_ctxt->i4_chrm_alt_proc)
2068     {
2069         WORD32 i4_offset_x, i4_offset_y;
2070         residual_samp_map_ctxt_t *ps_chroma_map;
2071         ref_mb_map_t *ps_x_off_len_chroma;
2072         ref_mb_map_t *ps_y_off_len_chroma;
2073 
2074         ps_chroma_map = &ps_lyr_ctxt->s_chroma_map_ctxt;
2075         ps_x_off_len_chroma = ps_chroma_map->ps_x_offset_length;
2076         ps_y_off_len_chroma = ps_chroma_map->ps_y_offset_length;
2077 
2078         /* get the actual offset for the buffers */
2079         i4_offset_x = ps_x_off_len_chroma[u2_mb_x].i2_offset;
2080         i4_offset_y = ps_y_off_len_chroma[u2_mb_y].i2_offset;
2081 
2082         /* since in dyadic case the window width and height will be 6x6     */
2083         /* and the window start offsets will be always 1 column left and    */
2084         /* 1 row above the block boundary. so the pointer and the required  */
2085         /* positions are appropriately modified                             */
2086         if(i4_offset_x >= 0)
2087         {
2088             pi2_inp += 2;
2089         }
2090 
2091         if(i4_offset_y >= 0)
2092         {
2093             pi2_inp += i4_inp_stride;
2094         }
2095 
2096         if(0 != (i4_chroma_nnz & 0x01))
2097         {
2098             ps_ctxt->pf_residual_chroma_dyadic(pv_residual_samp_ctxt, pi2_inp, i4_inp_stride,
2099                                                pi2_out, i4_out_stride);
2100         }
2101     }
2102     else
2103     {
2104         ps_ctxt->pf_residual_chroma_dyadic_alt(pv_residual_samp_ctxt, u2_mb_x, u2_mb_y,
2105                                                ps_ref_mb_mode, pi2_inp, i4_inp_stride, pi2_out,
2106                                                i4_out_stride, SVCD_FALSE);
2107     }
2108 
2109     /* CR */
2110     pi2_inp += 1;
2111     pi2_out += 1;
2112 
2113     if(SVCD_FALSE == ps_lyr_ctxt->i4_chrm_alt_proc)
2114     {
2115         if(0 != (i4_chroma_nnz & 0x10))
2116         {
2117             ps_ctxt->pf_residual_chroma_dyadic(pv_residual_samp_ctxt, pi2_inp, i4_inp_stride,
2118                                                pi2_out, i4_out_stride);
2119         }
2120     }
2121     else
2122     {
2123         ps_ctxt->pf_residual_chroma_dyadic_alt(pv_residual_samp_ctxt, u2_mb_x, u2_mb_y,
2124                                                ps_ref_mb_mode, pi2_inp, i4_inp_stride, pi2_out,
2125                                                i4_out_stride, SVCD_TRUE);
2126     }
2127     return OK;
2128 }
2129 
2130 /*****************************************************************************/
2131 /*                                                                           */
2132 /*  Function Name : isvcd_residual_samp_populate_list                         */
2133 /*                                                                           */
2134 /*  Description   : This is a seq or frame level init function which fills   */
2135 /*                  all offsets, projected locations arrays based on         */
2136 /*                  the two resolutions  and cropping parameters             */
2137 /*  Inputs        : refer to doxygen comments below                          */
2138 /*  Globals       : none                                                     */
2139 /*  Processing    : it projects the locations and computes the values        */
2140 /*                                                                           */
2141 /*  Outputs       : none                                                     */
2142 /*  Returns       : none                                                     */
2143 /*                                                                           */
2144 /*  Issues        : none                                                     */
2145 /*                                                                           */
2146 /*  Revision History:                                                        */
2147 /*                                                                           */
2148 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
2149 /*         06 07 2021   vijayakumar          creation                        */
2150 /*                                                                           */
2151 /*****************************************************************************/
isvcd_residual_samp_populate_list(residual_samp_map_ctxt_t * ps_map_ctxt,dec_seq_params_t * ps_sps,dec_svc_seq_params_t * ps_subset_sps,res_prms_t * ps_curr_res_prms,res_prms_t * ps_ref_res_prms,WORD32 i4_chroma_flag)2152 void isvcd_residual_samp_populate_list(residual_samp_map_ctxt_t *ps_map_ctxt,
2153                                        dec_seq_params_t *ps_sps,
2154                                        dec_svc_seq_params_t *ps_subset_sps,
2155                                        res_prms_t *ps_curr_res_prms, res_prms_t *ps_ref_res_prms,
2156                                        WORD32 i4_chroma_flag)
2157 {
2158     /* --------------------------------------------------------------------- */
2159     /* Local variables required for finding the mapping between the layers     */
2160     /* --------------------------------------------------------------------- */
2161     UWORD32 u4_shift_x;
2162     UWORD32 u4_shift_y;
2163     UWORD32 u4_scale_x;
2164     UWORD32 u4_scale_y;
2165     WORD32 i4_offset_x;
2166     WORD32 i4_offset_y;
2167     WORD32 i4_add_x;
2168     WORD32 i4_add_y;
2169     WORD32 i4_delta_x;
2170     WORD32 i4_delta_y;
2171     WORD32 i4_refphase_x;
2172     WORD32 i4_refphase_y;
2173     WORD32 i4_phase_x;
2174     WORD32 i4_phase_y;
2175     WORD32 i4_sub_wd;
2176     WORD32 i4_sub_ht;
2177     WORD32 i4_mb_wd;
2178     WORD32 i4_mb_ht;
2179     /* --------------------------------------------------------------------- */
2180     /* Local Pointer Declaration for arrays in Mapping context                 */
2181     /* --------------------------------------------------------------------- */
2182     ref_mb_map_t *ps_x_off_len;
2183     ref_mb_map_t *ps_y_off_len;
2184     UWORD32 i4_ref_wd;
2185     UWORD32 i4_ref_ht;
2186     UWORD32 i4_scaled_wd;
2187     UWORD32 i4_scaled_ht;
2188     WORD32 i4_curr_lyr_width;
2189     WORD32 i4_curr_lyr_height;
2190 
2191     /* --------------------------------------------------------------------- */
2192     /* Local Flag Declaration                                                 */
2193     /* --------------------------------------------------------------------- */
2194     WORD32 i4_ref_layer_field_pic_flag;
2195     WORD32 i4_field_pic_flag;
2196     WORD32 i4_frame_mbs_only_flag;
2197     WORD32 i4_ref_layer_frame_Mbs_only_flag;
2198     WORD32 i4_field_Mb_flag;
2199     WORD32 i4_bot_field_flag;
2200 
2201     /* --------------------------------------------------------------------- */
2202     /* Cropping Parameters Declaration                                         */
2203     /* --------------------------------------------------------------------- */
2204     WORD32 i4_scaled_ref_layer_left_offset;
2205     WORD32 i4_scaled_ref_layer_top_offset;
2206 
2207     /* --------------------------------------------------------------------- */
2208     /* Hardcoding flag information    (assuming no field support) */
2209     /* --------------------------------------------------------------------- */
2210     i4_ref_layer_field_pic_flag = SVCD_FALSE;
2211     i4_field_pic_flag = SVCD_FALSE;
2212     i4_frame_mbs_only_flag = SVCD_TRUE;
2213     i4_field_Mb_flag = SVCD_FALSE;
2214     i4_bot_field_flag = SVCD_FALSE;
2215     i4_ref_layer_frame_Mbs_only_flag = SVCD_TRUE;
2216 
2217     /* --------------------------------------------------------------------- */
2218     /* Pointer and Paramater are intialized    - Chroma and Luma */
2219     /* --------------------------------------------------------------------- */
2220     {
2221         WORD32 i4_base_width;
2222         WORD32 i4_base_height;
2223         WORD32 i4_ref_layer_chroma_phase_x_plus1_flag;
2224         WORD32 i4_ref_layer_chroma_phase_y_plus1;
2225         WORD32 i4_chroma_phase_x_plus1_flag;
2226         WORD32 i4_chroma_phase_y_plus1;
2227 
2228         /* ------------------------------------------------------------- */
2229         /* HARD CODED FOR 420                                             */
2230         /* ------------------------------------------------------------- */
2231         WORD32 i4_sub_wd_chroma = 2;
2232         WORD32 i4_sub_ht_chroma = 2;
2233 
2234         i4_base_width = ps_ref_res_prms->i4_res_width;
2235         i4_base_height = ps_ref_res_prms->i4_res_height;
2236 
2237         i4_ref_layer_chroma_phase_x_plus1_flag =
2238             ps_curr_res_prms->i1_ref_lyr_chroma_phase_x_plus1_flag;
2239         i4_ref_layer_chroma_phase_y_plus1 = ps_curr_res_prms->i1_ref_lyr_chroma_phase_y_plus1;
2240         i4_chroma_phase_x_plus1_flag =
2241             ps_subset_sps->s_sps_svc_ext.u1_seq_ref_layer_chroma_phase_x_plus1_flag;
2242         i4_chroma_phase_y_plus1 =
2243             ps_subset_sps->s_sps_svc_ext.u1_seq_ref_layer_chroma_phase_y_plus1;
2244         i4_scaled_ref_layer_left_offset = ps_curr_res_prms->s_ref_lyr_scaled_offset.i2_left;
2245         i4_scaled_ref_layer_top_offset = ps_curr_res_prms->s_ref_lyr_scaled_offset.i2_top;
2246 
2247         /* ----------------------------------------------------------------- */
2248         /* Computing Effective Frame Dimensions                                 */
2249         /* ------------------------------------------------------------------*/
2250         i4_ref_wd = (i4_base_width >> i4_chroma_flag);
2251         i4_ref_ht = (i4_base_height >> i4_chroma_flag) * (1 + i4_ref_layer_field_pic_flag);
2252         i4_scaled_wd = ps_curr_res_prms->u2_scaled_ref_width;
2253         i4_scaled_ht = ps_curr_res_prms->u2_scaled_ref_height;
2254         i4_scaled_wd = (i4_scaled_wd >> i4_chroma_flag);
2255         i4_scaled_ht = (i4_scaled_ht >> i4_chroma_flag) * (1 + i4_field_pic_flag);
2256 
2257         if(1 == i4_chroma_flag)
2258         {
2259             i4_refphase_x = i4_ref_layer_chroma_phase_x_plus1_flag - 1;
2260             i4_refphase_y = i4_ref_layer_chroma_phase_y_plus1 - 1;
2261             i4_phase_x = i4_chroma_phase_x_plus1_flag - 1;
2262             i4_phase_y = i4_chroma_phase_y_plus1 - 1;
2263             i4_sub_wd = i4_sub_wd_chroma;
2264             i4_sub_ht = i4_sub_ht_chroma;
2265             i4_mb_wd = MB_WIDTH >> 1;
2266             i4_mb_ht = MB_HEIGHT >> 1;
2267         }
2268         else
2269         {
2270             i4_refphase_x = 0;
2271             i4_refphase_y = 0;
2272             i4_phase_x = 0;
2273             i4_phase_y = 0;
2274             i4_sub_wd = 1;
2275             i4_sub_ht = 1;
2276             i4_mb_wd = MB_WIDTH;
2277             i4_mb_ht = MB_HEIGHT;
2278         }
2279     }
2280 
2281     /* --------------------------------------------------------------------- */
2282     /* Derive shift x and y based on level idd                               */
2283     /* --------------------------------------------------------------------- */
2284     if(ps_sps->u1_level_idc <= 30)
2285     {
2286         u4_shift_x = 16;
2287         u4_shift_y = 16;
2288     }
2289     else
2290     {
2291         u4_shift_x = 31 - isvcd_get_ceil_log2(i4_ref_wd);
2292         u4_shift_y = 31 - isvcd_get_ceil_log2(i4_ref_ht);
2293     }
2294 
2295     /* --------------------------------------------------------------------- */
2296     /* The following condition is not true in our case for time being         */
2297     /* --------------------------------------------------------------------- */
2298     if((SVCD_FALSE == i4_frame_mbs_only_flag) || (SVCD_FALSE == i4_ref_layer_frame_Mbs_only_flag))
2299     {
2300         i4_phase_y = i4_phase_y + 4 * i4_bot_field_flag;
2301 
2302         if(1 == i4_ref_layer_frame_Mbs_only_flag)
2303             i4_refphase_y = (2 * i4_refphase_y) + 2;
2304         else
2305             i4_refphase_y = i4_refphase_y + (4 * i4_bot_field_flag);
2306     }
2307 
2308     /* --------------------------------------------------------------------- */
2309     /* Dx and Dy Computation - Ratio of the base and enhance layer width     */
2310     /* --------------------------------------------------------------------- */
2311     u4_scale_x = ((i4_ref_wd << u4_shift_x) + (i4_scaled_wd >> 1)) / (i4_scaled_wd);
2312 
2313     u4_scale_y = ((i4_ref_ht << u4_shift_y) + (i4_scaled_ht >> 1)) / (i4_scaled_ht);
2314 
2315     i4_offset_x = i4_scaled_ref_layer_left_offset / i4_sub_wd;
2316     i4_add_x = (((i4_ref_wd * (2 + i4_phase_x)) << (u4_shift_x - 2)) + (i4_scaled_wd >> 1)) /
2317                    i4_scaled_wd +
2318                (1 << (u4_shift_x - 5));
2319     i4_delta_x = 4 * (2 + i4_refphase_x);
2320 
2321     if((SVCD_TRUE == i4_frame_mbs_only_flag) && (SVCD_TRUE == i4_ref_layer_frame_Mbs_only_flag))
2322     {
2323         i4_offset_y = i4_scaled_ref_layer_top_offset / i4_sub_ht;
2324         i4_add_y = (((i4_ref_ht * (2 + i4_phase_y)) << (u4_shift_y - 2)) + (i4_scaled_ht >> 1)) /
2325                        i4_scaled_ht +
2326                    (1 << (u4_shift_y - 5));
2327         i4_delta_y = 4 * (2 + i4_refphase_y);
2328     }
2329     else
2330     {
2331         i4_offset_y = i4_scaled_ref_layer_top_offset / (2 * i4_sub_ht);
2332         i4_add_y = (((i4_ref_ht * (2 + i4_phase_y)) << (u4_shift_y - 3)) + (i4_scaled_ht >> 1)) /
2333                        i4_scaled_ht +
2334                    (1 << (u4_shift_y - 5));
2335         i4_delta_y = 2 * (2 + i4_refphase_y);
2336     }
2337 
2338     /* --------------------------------------------------------------------- */
2339     /* Intializing Local Pointers    - Chroma and Luma                       */
2340     /* --------------------------------------------------------------------- */
2341     ps_x_off_len = ps_map_ctxt->ps_x_offset_length;
2342     ps_y_off_len = ps_map_ctxt->ps_y_offset_length;
2343     i4_curr_lyr_width = ps_curr_res_prms->i4_res_width >> i4_chroma_flag;
2344     i4_curr_lyr_height = ps_curr_res_prms->i4_res_height >> i4_chroma_flag;
2345 
2346     {
2347         WORD32 i4_i, i4_j;
2348 
2349         /* ----------------------------------------------------------------- */
2350         /* Computation of offsetX refArrayW Xmin and Xmax Lists               */
2351         /* ----------------------------------------------------------------- */
2352         for(i4_i = 0; i4_i < i4_curr_lyr_width; i4_i = i4_i + i4_mb_wd)
2353         {
2354             WORD32 i4_x_refmin16;
2355             WORD32 i4_x_refmax16;
2356             WORD32 i4_x_offset;
2357 
2358             i4_x_refmin16 = (WORD64) (((WORD64) ((i4_i - i4_offset_x) * u4_scale_x) + i4_add_x) >>
2359                                       ((WORD32) (u4_shift_x - 4))) -
2360                             i4_delta_x;
2361 
2362             i4_x_refmax16 =
2363                 (WORD64) (((WORD64) (i4_i + i4_mb_wd - 1 - i4_offset_x) * u4_scale_x + i4_add_x) >>
2364                           ((WORD32) (u4_shift_x - 4))) -
2365                 i4_delta_x;
2366 
2367             /* AC205 */
2368             i4_x_offset = i4_x_refmin16 >> 4;
2369             ps_x_off_len->i2_offset = i4_x_offset;
2370             ps_x_off_len->i2_length = (i4_x_refmax16 >> 4) - i4_x_offset + 2;
2371 
2372             /* increment the pointer */
2373             ps_x_off_len++;
2374 
2375         } /* end of loop over current layer width */
2376 
2377         /* ----------------------------------------------------------------- */
2378         /* Computation of offsetY refArrayH Ymin and Ymax Lists              */
2379         /* ----------------------------------------------------------------- */
2380         for(i4_j = 0; i4_j < i4_curr_lyr_height; i4_j = i4_j + i4_mb_ht)
2381         {
2382             WORD32 i4_y_refmin16;
2383             WORD32 i4_y_refmax16;
2384             WORD32 i4_y_offset;
2385 
2386             i4_y_refmin16 = (WORD64) (((WORD64) (i4_j - i4_offset_y) * u4_scale_y + i4_add_y) >>
2387                                       ((WORD32) (u4_shift_y - 4))) -
2388                             i4_delta_y;
2389 
2390             i4_y_refmax16 =
2391                 (WORD64) (((WORD64) (i4_j + i4_mb_ht - 1 - i4_offset_y) * u4_scale_y + i4_add_y) >>
2392                           ((WORD32) (u4_shift_y - 4))) -
2393                 i4_delta_y;
2394 
2395             /* AC205 */
2396             i4_y_offset = i4_y_refmin16 >> 4;
2397             ps_y_off_len->i2_offset = i4_y_offset;
2398             ps_y_off_len->i2_length = (i4_y_refmax16 >> 4) - i4_y_offset + 2;
2399 
2400             /* increment the pointer */
2401             ps_y_off_len++;
2402 
2403         } /* end of loop over current layer height */
2404     }
2405 
2406     /* --------------------------------------------------------------------- */
2407     /* Computation of Xref and Xphase List as per standard                     */
2408     /* --------------------------------------------------------------------- */
2409     ps_x_off_len = ps_map_ctxt->ps_x_offset_length;
2410     ps_y_off_len = ps_map_ctxt->ps_y_offset_length;
2411 
2412     {
2413         WORD32 i4_xc;
2414         WORD32 i4_offset_x_index;
2415         ref_pixel_map_t *ps_x_pos_phase;
2416 
2417         ps_x_pos_phase = ps_map_ctxt->ps_x_pos_phase;
2418 
2419         for(i4_xc = 0; i4_xc < i4_curr_lyr_width; i4_xc++)
2420         {
2421             WORD32 i4_x_offset;
2422             WORD32 i4_x_ref16;
2423 
2424             i4_offset_x_index = i4_xc / i4_mb_wd;
2425             i4_x_offset = ps_x_off_len[i4_offset_x_index].i2_offset;
2426             i4_x_ref16 = (WORD64) (((WORD64) (i4_xc - i4_offset_x) * u4_scale_x + i4_add_x) >>
2427                                    ((WORD32) (u4_shift_x - 4))) -
2428                          i4_delta_x;
2429 
2430             /* store the values */
2431             ps_x_pos_phase->i2_ref_pos = (i4_x_ref16 >> 4) - i4_x_offset;
2432             ps_x_pos_phase->i2_phase = (i4_x_ref16 - (16 * i4_x_offset)) & 15;
2433 
2434             /* increment the pointer */
2435             ps_x_pos_phase++;
2436         } /* end of loop over scaled width */
2437     }
2438 
2439     /* --------------------------------------------------------------------- */
2440     /* Computation of Yref and Yphase List as per standard                     */
2441     /* --------------------------------------------------------------------- */
2442     {
2443         WORD32 i4_yc;
2444         WORD32 i4_offset_y_index;
2445         ref_pixel_map_t *ps_y_pos_phase;
2446 
2447         ps_y_pos_phase = ps_map_ctxt->ps_y_pos_phase;
2448 
2449         for(i4_yc = 0; i4_yc < i4_curr_lyr_height; i4_yc++)
2450         {
2451             WORD32 i4_y_offset;
2452             WORD32 i4_y_ref16;
2453 
2454             i4_offset_y_index = i4_yc / i4_mb_ht;
2455             i4_y_offset = ps_y_off_len[i4_offset_y_index].i2_offset;
2456 
2457             if((SVCD_FALSE == i4_frame_mbs_only_flag) ||
2458                (SVCD_FALSE == i4_ref_layer_frame_Mbs_only_flag))
2459             {
2460                 i4_yc = i4_yc >> (1 - i4_field_Mb_flag);
2461             }
2462 
2463             i4_y_ref16 = (WORD64) ((((WORD64) (i4_yc - i4_offset_y) * u4_scale_y + i4_add_y) >>
2464                                     ((WORD32) (u4_shift_y - 4))) -
2465                                    i4_delta_y);
2466             ps_y_pos_phase->i2_ref_pos = (i4_y_ref16 >> 4) - i4_y_offset;
2467             ps_y_pos_phase->i2_phase = (i4_y_ref16 - (16 * i4_y_offset)) & 15;
2468 
2469             /* increment the pointer */
2470             ps_y_pos_phase++;
2471         } /* end of loop over scaled height */
2472     }
2473     return;
2474 }
2475 
2476 /*****************************************************************************/
2477 /*                                                                           */
2478 /*  Function Name : isvcd_residual_samp_res_init                              */
2479 /*                                                                           */
2480 /*  Description   : this function calculates the scale factors and initialise*/
2481 /*                  the context structure                                    */
2482 /*                                                                           */
2483 /*  Inputs        : pv_residual_samp_ctxt: handle to private structure       */
2484 /*                  ps_curr_lyr_res_prms: pointer to current resolution      */
2485 /*                                               params                      */
2486 /*  Globals       : none                                                     */
2487 /*  Processing    : it stores the layer dimensions                           */
2488 /*                                                                           */
2489 /*  Outputs       : none                                                     */
2490 /*  Returns       : none                                                     */
2491 /*                                                                           */
2492 /*  Issues        : none                                                     */
2493 /*                                                                           */
2494 /*  Revision History:                                                        */
2495 /*                                                                           */
2496 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
2497 /*         26 06 2021   vijayakumar          creation                        */
2498 /*                                                                           */
2499 /*****************************************************************************/
isvcd_residual_samp_res_init(void * pv_svc_dec)2500 WORD32 isvcd_residual_samp_res_init(void *pv_svc_dec)
2501 {
2502     residual_sampling_ctxt_t *ps_ctxt;
2503     res_lyr_ctxt *ps_lyr_ctxt;
2504     dec_seq_params_t *ps_sps;
2505     dec_svc_seq_params_t *ps_subset_sps;
2506     svc_dec_lyr_struct_t *ps_svc_lyr_dec = (svc_dec_lyr_struct_t *) pv_svc_dec;
2507     dec_struct_t *ps_dec = &ps_svc_lyr_dec->s_dec;
2508 
2509     void *pv_residual_samp_ctxt = ps_svc_lyr_dec->pv_residual_sample_ctxt;
2510     res_prms_t *ps_curr_lyr_res_prms = &ps_svc_lyr_dec->s_res_prms;
2511     ref_mb_map_t **pps_luma_map_horz = &ps_svc_lyr_dec->ps_ressam_luma_map_horz;
2512     ref_mb_map_t **pps_chroma_map_horz = &ps_svc_lyr_dec->ps_ressam_chroma_map_horz;
2513     ref_mb_map_t **pps_luma_map_vert = &ps_svc_lyr_dec->ps_ressam_luma_map_vert;
2514     ref_mb_map_t **pps_chroma_map_vert = &ps_svc_lyr_dec->ps_ressam_chroma_map_vert;
2515 
2516     if((NULL == pv_residual_samp_ctxt) || (NULL == ps_curr_lyr_res_prms) ||
2517        (NULL == pps_luma_map_horz) || (NULL == pps_chroma_map_horz) ||
2518        (NULL == pps_luma_map_vert) || (NULL == pps_chroma_map_vert))
2519     {
2520         return NOT_OK;
2521     }
2522 
2523     ps_ctxt = (residual_sampling_ctxt_t *) pv_residual_samp_ctxt;
2524 
2525     /* if called for base resolution store deafult values */
2526     if(SVCD_TRUE == ps_svc_lyr_dec->u1_base_res_flag)
2527     {
2528         *pps_luma_map_horz = NULL;
2529         *pps_chroma_map_horz = NULL;
2530         *pps_luma_map_vert = NULL;
2531         *pps_chroma_map_vert = NULL;
2532         ps_ctxt->i4_res_lyr_id = -1;
2533         ps_ctxt->i4_ref_width = ps_curr_lyr_res_prms->i4_res_width;
2534         ps_ctxt->i4_ref_height = ps_curr_lyr_res_prms->i4_res_height;
2535         return OK;
2536     }
2537 
2538     /* derive the current sps */
2539     ps_sps = ps_dec->ps_cur_sps;
2540     ps_subset_sps = ps_svc_lyr_dec->ps_cur_subset_sps;
2541 
2542     /* store the res id appropriately */
2543     ps_ctxt->i4_res_lyr_id = ps_svc_lyr_dec->u1_layer_id - 1;
2544 
2545     /* get the current layer ctxt */
2546     ps_lyr_ctxt = &ps_ctxt->as_res_lyrs[ps_svc_lyr_dec->u1_layer_id - 1];
2547 
2548     /* get the width and heights */
2549     ps_lyr_ctxt->i4_curr_width = ps_curr_lyr_res_prms->i4_res_width;
2550     ps_lyr_ctxt->i4_curr_height = ps_curr_lyr_res_prms->i4_res_height;
2551     ps_lyr_ctxt->i4_ref_width = ps_ctxt->i4_ref_width;
2552     ps_lyr_ctxt->i4_ref_height = ps_ctxt->i4_ref_height;
2553 
2554     /* store the strcuture pointer containing projected locations */
2555     *pps_luma_map_horz = ps_lyr_ctxt->s_luma_map_ctxt.ps_x_offset_length;
2556     *pps_chroma_map_horz = ps_lyr_ctxt->s_chroma_map_ctxt.ps_x_offset_length;
2557     *pps_luma_map_vert = ps_lyr_ctxt->s_luma_map_ctxt.ps_y_offset_length;
2558     *pps_chroma_map_vert = ps_lyr_ctxt->s_chroma_map_ctxt.ps_y_offset_length;
2559 
2560     /* check for recomputation of mapping required */
2561     if(SVCD_TRUE == ps_curr_lyr_res_prms->u1_remap_req_flag)
2562     {
2563         res_prms_t s_ref_res_prms = {0};
2564 
2565         /* store the reference layer resolution width and height */
2566         s_ref_res_prms.i4_res_width = ps_ctxt->i4_ref_width;
2567         s_ref_res_prms.i4_res_height = ps_ctxt->i4_ref_height;
2568 
2569         /* call the frame level projections calculation function */
2570         isvcd_residual_samp_populate_list(&ps_lyr_ctxt->s_luma_map_ctxt, ps_sps, ps_subset_sps,
2571                                           ps_curr_lyr_res_prms, &s_ref_res_prms, 0);
2572         isvcd_residual_samp_populate_list(&ps_lyr_ctxt->s_chroma_map_ctxt, ps_sps, ps_subset_sps,
2573                                           ps_curr_lyr_res_prms, &s_ref_res_prms, 1);
2574 
2575         /* default values for flags */
2576         ps_lyr_ctxt->pf_residual_samp_mb = &isvcd_residual_samp_mb;
2577         ps_lyr_ctxt->i4_chrm_horz_int_mode = 0;
2578         ps_lyr_ctxt->i4_chrm_vert_int_mode = 0;
2579         ps_lyr_ctxt->i4_chrm_alt_proc = SVCD_FALSE;
2580 
2581         /* Store the Dyadic flag */
2582         ps_lyr_ctxt->i4_dyadic_flag = ps_curr_lyr_res_prms->u1_dyadic_flag;
2583 
2584         /* set the appropriate chroma processing routine based on */
2585         /* phase values */
2586         if(SVCD_TRUE == ps_curr_lyr_res_prms->u1_dyadic_flag)
2587         {
2588             WORD32 i4_ref_layer_chroma_phase_x_plus1_flag;
2589             WORD32 i4_ref_layer_chroma_phase_y_plus1;
2590             WORD32 i4_chroma_phase_x_plus1_flag;
2591             WORD32 i4_chroma_phase_y_plus1;
2592 
2593             ps_lyr_ctxt->pf_residual_samp_mb = &isvcd_residual_samp_mb_dyadic;
2594             i4_ref_layer_chroma_phase_x_plus1_flag =
2595                 ps_curr_lyr_res_prms->i1_ref_lyr_chroma_phase_x_plus1_flag;
2596             i4_ref_layer_chroma_phase_y_plus1 =
2597                 ps_curr_lyr_res_prms->i1_ref_lyr_chroma_phase_y_plus1;
2598             i4_chroma_phase_x_plus1_flag =
2599                 ps_subset_sps->s_sps_svc_ext.u1_seq_ref_layer_chroma_phase_x_plus1_flag;
2600             i4_chroma_phase_y_plus1 =
2601                 ps_subset_sps->s_sps_svc_ext.u1_seq_ref_layer_chroma_phase_y_plus1;
2602             if((0 == i4_ref_layer_chroma_phase_x_plus1_flag) && (1 == i4_chroma_phase_x_plus1_flag))
2603             {
2604                 ps_lyr_ctxt->i4_chrm_horz_int_mode = 1;
2605                 ps_lyr_ctxt->i4_chrm_alt_proc = SVCD_TRUE;
2606             }
2607 
2608             if((0 == i4_ref_layer_chroma_phase_y_plus1) && (1 == i4_chroma_phase_y_plus1))
2609             {
2610                 ps_lyr_ctxt->i4_chrm_vert_int_mode = 1;
2611                 ps_lyr_ctxt->i4_chrm_alt_proc = SVCD_TRUE;
2612             }
2613 
2614             if((0 == i4_ref_layer_chroma_phase_y_plus1) && (2 == i4_chroma_phase_y_plus1))
2615             {
2616                 ps_lyr_ctxt->i4_chrm_vert_int_mode = 1;
2617                 ps_lyr_ctxt->i4_chrm_alt_proc = SVCD_TRUE;
2618             }
2619 
2620             if((2 == i4_ref_layer_chroma_phase_y_plus1) && (0 == i4_chroma_phase_y_plus1))
2621             {
2622                 ps_lyr_ctxt->i4_chrm_vert_int_mode = 2;
2623                 ps_lyr_ctxt->i4_chrm_alt_proc = SVCD_TRUE;
2624             }
2625         }
2626     }
2627     else
2628     {
2629         /* should take false value */
2630         if(SVCD_FALSE != ps_curr_lyr_res_prms->u1_remap_req_flag)
2631         {
2632             return NOT_OK;
2633         }
2634     }
2635 
2636     /* store the current layer width and height to context */
2637     ps_ctxt->i4_ref_width = ps_curr_lyr_res_prms->i4_res_width;
2638     ps_ctxt->i4_ref_height = ps_curr_lyr_res_prms->i4_res_height;
2639 
2640     /* assert on max ranges of width and shift values */
2641     if((ps_lyr_ctxt->i4_curr_width > H264_MAX_FRAME_WIDTH) ||
2642        (ps_lyr_ctxt->i4_ref_width > H264_MAX_FRAME_WIDTH) ||
2643        (ps_lyr_ctxt->i4_curr_height > H264_MAX_FRAME_HEIGHT) ||
2644        (ps_lyr_ctxt->i4_ref_height > H264_MAX_FRAME_HEIGHT))
2645     {
2646         return NOT_OK;
2647     }
2648     return OK;
2649 }