1 /******************************************************************************
2  *
3  * Copyright (C) 2015 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 ih264d_process_bslice.c
23  *
24  * \brief
25  *    Contains routines that decode B slice type
26  *
27  * Detailed_description
28  *
29  * \date
30  *    21/12/2002
31  *
32  * \author  NS
33  **************************************************************************
34  */
35 #include "ih264_typedefs.h"
36 #include "ih264_macros.h"
37 #include "ih264_platform_macros.h"
38 
39 #include <string.h>
40 #include "ih264d_structs.h"
41 #include "ih264d_bitstrm.h"
42 #include "ih264d_parse_cavlc.h"
43 #include "ih264d_mb_utils.h"
44 #include "ih264d_mvpred.h"
45 #include "ih264d_inter_pred.h"
46 #include "ih264d_process_pslice.h"
47 #include "ih264d_error_handler.h"
48 #include "ih264d_tables.h"
49 #include "ih264d_parse_slice.h"
50 #include "ih264d_process_pslice.h"
51 #include "ih264d_process_bslice.h"
52 #include "ih264d_tables.h"
53 #include "ih264d_parse_islice.h"
54 #include "ih264d_mvpred.h"
55 
56 void ih264d_init_cabac_contexts(UWORD8 u1_slice_type, dec_struct_t * ps_dec);
57 //UWORD32 g_hits = 0;
58 //UWORD32 g_miss = 0;
59 /*!
60  **************************************************************************
61  * \if Function name : ih264d_decode_spatial_direct \endif
62  *
63  * \brief
64  *    Decodes spatial direct mode.
65  *
66  * \return
67  *    None.
68  *    Arunoday T
69  **************************************************************************
70  */
ih264d_decode_spatial_direct(dec_struct_t * ps_dec,UWORD8 u1_wd_x,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_mb_num)71 WORD32 ih264d_decode_spatial_direct(dec_struct_t * ps_dec,
72                                     UWORD8 u1_wd_x,
73                                     dec_mb_info_t * ps_cur_mb_info,
74                                     UWORD8 u1_mb_num)
75 {
76     mv_pred_t s_mv_pred, *ps_mv;
77     UWORD8 u1_col_zero_flag, u1_sub_mb_num, u1_direct_zero_pred_flag = 0;
78     UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
79     mv_pred_t *ps_mv_ntop_start;
80     mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_mb_num << 4);
81     UWORD8 partition_size, sub_partition, u1_mb_partw, u1_mb_parth;
82     UWORD8 i;
83     WORD8 i1_pred, i1_ref_frame0, i1_ref_frame1;
84     struct pic_buffer_t *ps_ref_frame = NULL, *ps_col_pic, *ps_pic_buff0 = NULL,
85                     *ps_pic_buff1 = NULL;
86 
87     UWORD8 u1_zero_pred_cond_f, u1_zero_pred_cond_b;
88     WORD16 i2_def_mv[2], i2_spat_pred_mv[4], *pi2_final_mv0, *pi2_final_mv1;
89     UWORD16 ui2_mask_fwd = 0, ui2_mask_bwd = 0, u2_mask = 0;
90     UWORD32 *pui32_weight_ofsts = NULL;
91     directmv_t s_mvdirect;
92     UWORD8 u1_colz;
93     UWORD8 u1_final_ref_idx = 0;
94     const UWORD8 *pu1_mb_parth = (const UWORD8 *)gau1_ih264d_mb_parth;
95     const UWORD8 *pu1_mb_partw = (const UWORD8 *)gau1_ih264d_mb_partw;
96     const UWORD16 sub_mask_table[] =
97         { 0x33, 0x3, 0x11, 0x1 };
98     const UWORD16 mask_table[] =
99         { 0xffff, /*16x16 NA */
100           0xff, /* 16x8*/
101           0x3333, /* 8x16*/
102           0x33 };/* 8x8*/
103     mv_pred_t s_temp_mv_pred;
104     WORD32 ret = 0;
105 
106     /* CHANGED CODE */
107     ps_mv_ntop_start = ps_dec->ps_mv_cur + (u1_mb_num << 4)
108                     - (ps_dec->u2_frm_wd_in_mbs << (4 + u1_mbaff)) + 12;
109 
110     /* assign default values for MotionVector as zero */
111     i2_def_mv[0] = 0;
112     i2_def_mv[1] = 0;
113 
114     u1_direct_zero_pred_flag = ps_dec->pf_mvpred(ps_dec, ps_cur_mb_info, ps_mv_nmb_start,
115                                               ps_mv_ntop_start, &s_mv_pred, 0, 4,
116                                               0, 1, B_DIRECT_SPATIAL);
117 
118     i2_spat_pred_mv[0] = s_mv_pred.i2_mv[0];
119     i2_spat_pred_mv[1] = s_mv_pred.i2_mv[1];
120     i2_spat_pred_mv[2] = s_mv_pred.i2_mv[2];
121     i2_spat_pred_mv[3] = s_mv_pred.i2_mv[3];
122 
123     i1_ref_frame0 = s_mv_pred.i1_ref_frame[0];
124     i1_ref_frame1 = s_mv_pred.i1_ref_frame[1];
125 
126     i1_ref_frame0 = (i1_ref_frame0 < 0) ? -1 : i1_ref_frame0;
127     i1_ref_frame1 = (i1_ref_frame1 < 0) ? -1 : i1_ref_frame1;
128 
129     i1_pred = 0;
130 
131     {
132         WORD8 u1_ref_idx, u1_ref_idx1;
133         UWORD32 uc_Idx, uc_Idx1;
134         UWORD8 u1_scale_ref = (ps_dec->ps_cur_slice->u1_mbaff_frame_flag
135                         && ps_cur_mb_info->u1_mb_field_decodingflag);
136         u1_final_ref_idx = i1_ref_frame0;
137         if(i1_ref_frame0 >= 0)
138         {
139             /* convert RefIdx if it is MbAff */
140             u1_ref_idx = i1_ref_frame0;
141             u1_ref_idx1 = i1_ref_frame0;
142             if(u1_scale_ref)
143             {
144                 u1_ref_idx1 = u1_ref_idx >> 1;
145                 if((u1_ref_idx & 0x01) != (1 - ps_cur_mb_info->u1_topmb))
146                     u1_ref_idx1 += MAX_REF_BUFS;
147             }
148             /* If i1_ref_frame0 < 0 then refIdxCol is obtained from ps_pic_buff1 */
149             ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][u1_ref_idx1];
150             ps_ref_frame = ps_pic_buff0;
151             i1_pred = PRED_L0;
152         }
153 
154         if(i1_ref_frame1 >= 0)
155         {
156             /* convert RefIdx if it is MbAff */
157             u1_ref_idx = i1_ref_frame1;
158             u1_ref_idx1 = i1_ref_frame1;
159             if(u1_scale_ref)
160             {
161                 u1_ref_idx1 = u1_ref_idx >> 1;
162                 if((u1_ref_idx & 0x01) != (1 - ps_cur_mb_info->u1_topmb))
163                     u1_ref_idx1 += MAX_REF_BUFS;
164             }
165             ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][u1_ref_idx1];
166             i1_pred = i1_pred | PRED_L1;
167         }
168         if(i1_ref_frame0 < 0)
169         {
170             ps_ref_frame = ps_pic_buff1;
171             u1_final_ref_idx = i1_ref_frame1;
172         }
173 
174         u1_zero_pred_cond_f = (u1_direct_zero_pred_flag) || (i1_ref_frame0 < 0);
175         u1_zero_pred_cond_b = (u1_direct_zero_pred_flag) || (i1_ref_frame1 < 0);
176 
177         if(ps_dec->ps_cur_pps->u1_wted_bipred_idc)
178         {
179             uc_Idx = ((i1_ref_frame0 < 1) ? 0 : i1_ref_frame0)
180                             * ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
181             if(u1_scale_ref)
182                 uc_Idx >>= 1;
183             uc_Idx1 = (i1_ref_frame1 < 0) ? 0 : i1_ref_frame1;
184             uc_Idx += (u1_scale_ref) ? (uc_Idx1 >> 1) : uc_Idx1;
185             pui32_weight_ofsts =
186                             (UWORD32*)&ps_dec->pu4_wt_ofsts[2 * X3(uc_Idx)];
187 
188             if(i1_ref_frame0 < 0)
189                 pui32_weight_ofsts += 1;
190 
191             if(u1_scale_ref && (ps_dec->ps_cur_pps->u1_wted_bipred_idc == 2))
192             {
193                 WORD16 i2_ref_idx;
194                 i2_ref_idx = MAX(i1_ref_frame0, 0);
195                 i2_ref_idx *= (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
196                                 << 1);
197                 i2_ref_idx += MAX(i1_ref_frame1, 0);
198                 if(!ps_cur_mb_info->u1_topmb)
199                     i2_ref_idx +=
200                                     (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0]
201                                                     << 1)
202                                                     * (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
203                                                                     << 1);
204                 pui32_weight_ofsts = (UWORD32*)&ps_dec->pu4_mbaff_wt_mat[2
205                                 * X3(i2_ref_idx)];
206             }
207         }
208     }
209 
210     s_temp_mv_pred.i1_ref_frame[0] = i1_ref_frame0;
211     s_temp_mv_pred.i1_ref_frame[1] = i1_ref_frame1;
212     s_temp_mv_pred.u1_col_ref_pic_idx = ps_ref_frame->u1_mv_buf_id;
213     s_temp_mv_pred.u1_pic_type = ps_ref_frame->u1_pic_type;
214 
215     /**********************************************************************/
216     /* Call the function which gets the number of partitions and          */
217     /* partition info of colocated Mb                                     */
218     /**********************************************************************/
219 
220     ps_dec->pf_parse_mvdirect(ps_dec, ps_dec->ps_col_pic, &s_mvdirect, u1_wd_x,
221                            ps_dec->i4_submb_ofst, ps_cur_mb_info);
222     ps_col_pic = ps_dec->ps_col_pic;
223     if((s_mvdirect.u1_col_zeroflag_change == 0) || u1_direct_zero_pred_flag)
224     {
225         WORD16 i2_mv_x, i2_mv_y, i2_mvX1, i2_mvY1;
226         /* Most probable case */
227         u1_col_zero_flag = *(ps_col_pic->pu1_col_zero_flag
228                         + s_mvdirect.i4_mv_indices[0]);
229         u1_col_zero_flag = u1_col_zero_flag & 0x01;
230 
231         if(u1_zero_pred_cond_f || ((i1_ref_frame0 == 0) && (u1_col_zero_flag == 1)))
232         {
233             i2_mv_x = 0;
234             i2_mv_y = 0;
235         }
236         else
237         {
238             i2_mv_x = i2_spat_pred_mv[0];
239             i2_mv_y = i2_spat_pred_mv[1];
240 
241         }
242 
243         if(u1_zero_pred_cond_b || ((i1_ref_frame1 == 0) && (u1_col_zero_flag == 1)))
244         {
245             i2_mvX1 = 0;
246             i2_mvY1 = 0;
247         }
248         else
249         {
250             i2_mvX1 = i2_spat_pred_mv[2];
251             i2_mvY1 = i2_spat_pred_mv[3];
252         }
253 
254         u1_sub_mb_num = ps_dec->u1_sub_mb_num;
255         u1_mb_partw = (u1_wd_x >> 2);
256 
257 
258         if(i1_ref_frame0 >= 0)
259         {
260             {
261                pred_info_pkd_t *ps_pred_pkd;
262                WORD16 i2_mv[2];
263                WORD8 i1_ref_idx= 0;
264 
265                i2_mv[0] = i2_mv_x;
266                i2_mv[1] = i2_mv_y;
267 
268                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
269             ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_partw,u1_sub_mb_num,i1_pred,
270                             ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
271                             ps_pic_buff0->u1_pic_type);
272             ps_dec->u4_pred_info_pkd_idx++;
273             ps_cur_mb_info->u1_num_pred_parts++;
274 
275 
276             }
277 
278         }
279 
280         if(i1_ref_frame1 >= 0)
281         {
282             {
283                 pred_info_pkd_t *ps_pred_pkd;
284                WORD16 i2_mv[2];
285                WORD8 i1_ref_idx= 0;
286 
287                i2_mv[0] = i2_mvX1;
288                i2_mv[1] = i2_mvY1;
289 
290                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
291             ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_partw,u1_sub_mb_num,i1_pred,
292                             ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
293                             ps_pic_buff1->u1_pic_type);
294             ps_dec->u4_pred_info_pkd_idx++;
295             ps_cur_mb_info->u1_num_pred_parts++;
296 
297 
298             }
299         }
300 
301 
302         /* Replication optimisation */
303         s_temp_mv_pred.i2_mv[0] = i2_mv_x;
304         s_temp_mv_pred.i2_mv[1] = i2_mv_y;
305         s_temp_mv_pred.i2_mv[2] = i2_mvX1;
306         s_temp_mv_pred.i2_mv[3] = i2_mvY1;
307 
308         /* Calculating colocated zero information */
309         {
310             /*************************************/
311             /* If(bit2 and bit3 set)             */
312             /* then                              */
313             /*  (bit0 and bit1) => submmbmode    */
314             /*  (bit2 and bit3) => mbmode        */
315             /* else                              */
316             /*  (bit0 and bit1) => mbmode        */
317             /*************************************/
318             /*UWORD8 u1_packed_mb_sub_mb_mode = sub_partition ?
319              (s_mvdirect.i1_partitionsize[0]) : ((s_mvdirect.i1_partitionsize[0]) << 2);*/
320             UWORD8 u1_packed_mb_sub_mb_mode = (u1_mb_partw == 2) ? 0x03 : 0;
321 
322             if(i1_ref_frame0 < 0)
323             {
324                 i2_mv_x = i2_mvX1;
325                 i2_mv_y = i2_mvY1;
326             }
327 
328             /* Change from left shift 4 to 6 - Varun */
329             u1_colz = (ps_cur_mb_info->u1_mb_field_decodingflag << 1)
330                             | ((u1_final_ref_idx == 0) && (ABS(i2_mv_x) <= 1)
331                                             && (ABS(i2_mv_y) <= 1));
332             u1_colz |= (u1_packed_mb_sub_mb_mode << 6);
333         }
334         ps_mv = ps_mv_nmb_start + u1_sub_mb_num;
335         ih264d_rep_mv_colz(ps_dec, &s_temp_mv_pred, ps_mv, u1_sub_mb_num, u1_colz,
336                            u1_mb_partw, u1_mb_partw);
337         if(u1_wd_x == MB_SIZE)
338             ps_dec->u1_currB_type = 0;
339 
340 
341 
342         return OK;
343     }
344     /***************************************************************************/
345     /* If present MB is 16x16 and the partition of colocated Mb is >= PRED_8x8 */
346     /* i.e 8x8 or less than 8x8 partitions then set up DMA for (0,0) and       */
347     /* spatially predicted motion vector and do the multiplexing after         */
348     /* motion compensation                                                     */
349     /***************************************************************************/
350 
351 
352     if((u1_wd_x == MB_SIZE) && (s_mvdirect.i1_num_partitions > 2))
353     {
354         ps_cur_mb_info->u1_Mux = 1;
355         if(i1_ref_frame0 >= 0)
356         {
357 
358             {
359                 pred_info_pkd_t *ps_pred_pkd;
360                WORD8 i1_ref_idx= 0;
361 
362                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
363             ih264d_fill_pred_info(&(i2_spat_pred_mv[0]),4,4,0,i1_pred,
364                             ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
365                             ps_pic_buff0->u1_pic_type);
366             ps_dec->u4_pred_info_pkd_idx++;
367             ps_cur_mb_info->u1_num_pred_parts++;
368 
369 
370             }
371 
372             /******    (0,0) Motion vectors DMA     *****/
373             {
374                 pred_info_pkd_t *ps_pred_pkd;
375                WORD16 i2_mv[2];
376                WORD8 i1_ref_idx= 0;
377 
378                i2_mv[0] = 0;
379                i2_mv[1] = 0;
380 
381                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
382             ih264d_fill_pred_info(i2_mv,4,4,0,i1_pred,
383                             ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
384                             ps_pic_buff0->u1_pic_type);
385             ps_dec->u4_pred_info_pkd_idx++;
386             ps_cur_mb_info->u1_num_pred_parts++;
387 
388 
389             }
390         }
391         if(i1_ref_frame1 >= 0)
392         {
393             {
394                 pred_info_pkd_t *ps_pred_pkd;
395                WORD16 i2_mv[2];
396                WORD8 i1_ref_idx= 0;
397 
398                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
399             ih264d_fill_pred_info(&(i2_spat_pred_mv[2]),4,4,0,i1_pred,
400                             ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
401                             ps_pic_buff1->u1_pic_type);
402             ps_dec->u4_pred_info_pkd_idx++;
403             ps_cur_mb_info->u1_num_pred_parts++;
404 
405 
406             }
407 
408             /******    (0,0) Motion vectors DMA     *****/
409 
410             {
411                 pred_info_pkd_t *ps_pred_pkd;
412                WORD16 i2_mv[2];
413                WORD8 i1_ref_idx= 0;
414 
415                i2_mv[0] = 0;
416                i2_mv[1] = 0;
417 
418                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
419             ih264d_fill_pred_info(i2_mv,4,4,0,i1_pred,
420                             ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
421                             ps_pic_buff1->u1_pic_type);
422             ps_dec->u4_pred_info_pkd_idx++;
423             ps_cur_mb_info->u1_num_pred_parts++;
424 
425 
426             }
427         }
428     }
429 
430     /*u1_col = *(ps_col_pic->pu1_col_zero_flag + s_mvdirect.i4_mv_indices[0]);
431      u1_col &= 1;
432      u1_init = 0;*/
433 
434     for(i = 0; i < s_mvdirect.i1_num_partitions; i++)
435     {
436         partition_size = s_mvdirect.i1_partitionsize[i];
437         u1_sub_mb_num = s_mvdirect.i1_submb_num[i];
438 
439         sub_partition = partition_size >> 2;
440         partition_size &= 0x3;
441         u1_mb_partw = pu1_mb_partw[partition_size];
442         u1_mb_parth = pu1_mb_parth[partition_size];
443         u2_mask = mask_table[partition_size];
444         if(sub_partition != 0)
445         {
446             u1_mb_partw >>= 1;
447             u1_mb_parth >>= 1;
448             u2_mask = sub_mask_table[partition_size];
449         }
450 
451         u1_col_zero_flag = *(ps_col_pic->pu1_col_zero_flag
452                         + s_mvdirect.i4_mv_indices[i]);
453         u1_col_zero_flag = u1_col_zero_flag & 0x01;
454 
455         /*if(u1_col != u1_col_zero_flag)
456          u1_init = 1;*/
457 
458         if(u1_zero_pred_cond_f || ((i1_ref_frame0 == 0) && (u1_col_zero_flag == 1)))
459         {
460             pi2_final_mv0 = &i2_def_mv[0];
461             ui2_mask_fwd |= (u2_mask << u1_sub_mb_num);
462         }
463         else
464             pi2_final_mv0 = &i2_spat_pred_mv[0];
465 
466         if(u1_zero_pred_cond_b || ((i1_ref_frame1 == 0) && (u1_col_zero_flag == 1)))
467         {
468             pi2_final_mv1 = &i2_def_mv[0];
469             ui2_mask_bwd |= (u2_mask << u1_sub_mb_num);
470         }
471         else
472             pi2_final_mv1 = &i2_spat_pred_mv[2];
473 
474         if(ps_cur_mb_info->u1_Mux != 1)
475         {
476             /*u1_sub_mb_x = u1_sub_mb_num & 0x03;
477              uc_sub_mb_y = (u1_sub_mb_num >> 2);*/
478             if(i1_ref_frame0 >= 0)
479             {
480 
481                 {
482                     pred_info_pkd_t *ps_pred_pkd;
483                    WORD8 i1_ref_idx= 0;
484 
485                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
486                 ih264d_fill_pred_info(pi2_final_mv0,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,i1_pred,
487                                 ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
488                                 ps_pic_buff0->u1_pic_type);
489                 ps_dec->u4_pred_info_pkd_idx++;
490                 ps_cur_mb_info->u1_num_pred_parts++;
491 
492 
493                 }
494 
495             }
496 
497             if(i1_ref_frame1 >= 0)
498             {
499                 {
500                     pred_info_pkd_t *ps_pred_pkd;
501                    WORD8 i1_ref_idx= 0;
502 
503                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
504                 ih264d_fill_pred_info(pi2_final_mv1,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,i1_pred,
505                                 ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
506                                 ps_pic_buff1->u1_pic_type);
507                 ps_dec->u4_pred_info_pkd_idx++;
508                 ps_cur_mb_info->u1_num_pred_parts++;
509 
510 
511                 }
512             }
513         }
514 
515         /* Replication optimisation */
516         s_temp_mv_pred.i2_mv[0] = pi2_final_mv0[0];
517         s_temp_mv_pred.i2_mv[1] = pi2_final_mv0[1];
518         s_temp_mv_pred.i2_mv[2] = pi2_final_mv1[0];
519         s_temp_mv_pred.i2_mv[3] = pi2_final_mv1[1];
520 
521         /* Calculating colocated zero information */
522         {
523             WORD16 i2_mv_x = 0, i2_mv_y = 0;
524             /*************************************/
525             /* If(bit2 and bit3 set)             */
526             /* then                              */
527             /*  (bit0 and bit1) => submmbmode    */
528             /*  (bit2 and bit3) => mbmode        */
529             /* else                              */
530             /*  (bit0 and bit1) => mbmode        */
531             /*************************************/
532             UWORD8 u1_packed_mb_sub_mb_mode =
533                             sub_partition ? (s_mvdirect.i1_partitionsize[i]) : ((s_mvdirect.i1_partitionsize[i])
534                                                             << 2);
535 
536             if(i1_ref_frame0 >= 0)
537             {
538                 i2_mv_x = pi2_final_mv0[0];
539                 i2_mv_y = pi2_final_mv0[1];
540             }
541             else
542             {
543                 i2_mv_x = pi2_final_mv1[0];
544                 i2_mv_y = pi2_final_mv1[1];
545             }
546 
547             u1_colz = (ps_cur_mb_info->u1_mb_field_decodingflag << 1)
548                             | ((u1_final_ref_idx == 0) && (ABS(i2_mv_x) <= 1)
549                                             && (ABS(i2_mv_y) <= 1));
550             u1_colz |= (u1_packed_mb_sub_mb_mode << 4);
551         }
552         ps_mv = ps_mv_nmb_start + u1_sub_mb_num;
553         ih264d_rep_mv_colz(ps_dec, &s_temp_mv_pred, ps_mv, u1_sub_mb_num, u1_colz,
554                            u1_mb_parth, u1_mb_partw);
555     }
556     i = 0;
557     if(i1_ref_frame0 >= 0)
558         ps_cur_mb_info->u2_mask[i++] = ui2_mask_fwd;
559     if(i1_ref_frame1 >= 0)
560         ps_cur_mb_info->u2_mask[i] = ui2_mask_bwd;
561 
562     /*if(u1_init)
563      H264_DEC_DEBUG_PRINT("hit\n");
564      else
565      H264_DEC_DEBUG_PRINT("miss\n");*/
566 
567     return OK;
568 }
569 
570 /*!
571  **************************************************************************
572  * \if Function name : ih264d_decode_temporal_direct \endif
573  *
574  * \brief
575  *    Decodes temporal direct mode.
576  *
577  * \return
578  *    None.
579  *
580  **************************************************************************
581  */
ih264d_decode_temporal_direct(dec_struct_t * ps_dec,UWORD8 u1_wd_x,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_mb_num)582 WORD32 ih264d_decode_temporal_direct(dec_struct_t * ps_dec,
583                                      UWORD8 u1_wd_x,
584                                      dec_mb_info_t * ps_cur_mb_info,
585                                      UWORD8 u1_mb_num)
586 {
587     struct pic_buffer_t *ps_pic_buff0, *ps_pic_buff1, *ps_col_pic;
588     mv_pred_t *ps_mv, s_temp_mv_pred;
589     UWORD8 u1_sub_mb_num;
590     UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
591     WORD16 i2_mv_x0, i2_mv_y0, i2_mv_x1, i2_mv_y1;
592     UWORD8 u1_mb_partw, u1_mb_parth;
593     UWORD8 i, partition_size, sub_partition;
594     UWORD32 *pui32_weight_ofsts = NULL;
595     directmv_t s_mvdirect;
596     const UWORD8 *pu1_mb_parth = (const UWORD8 *)gau1_ih264d_mb_parth;
597     const UWORD8 *pu1_mb_partw = (const UWORD8 *)gau1_ih264d_mb_partw;
598     WORD8 c_refFrm0, c_refFrm1;
599     UWORD8 u1_ref_idx0, u1_is_cur_mb_fld;
600     UWORD32 pic0_poc, pic1_poc, cur_poc;
601     WORD32 ret = 0;
602 
603     u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
604     ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][0];
605 
606     /**********************************************************************/
607     /* Call the function which gets the number of partitions and          */
608     /* partition info of colocated Mb                                     */
609     /**********************************************************************/
610     ps_dec->pf_parse_mvdirect(ps_dec, ps_dec->ps_col_pic, &s_mvdirect, u1_wd_x,
611                            ps_dec->i4_submb_ofst, ps_cur_mb_info);
612     ps_col_pic = ps_dec->ps_col_pic;
613 
614     for(i = 0; i < s_mvdirect.i1_num_partitions; i++)
615     {
616         UWORD8 u1_colz;
617         partition_size = s_mvdirect.i1_partitionsize[i];
618         u1_sub_mb_num = s_mvdirect.i1_submb_num[i];
619         ps_mv = ps_col_pic->ps_mv + s_mvdirect.i4_mv_indices[i];
620 
621         /* This should be removed to catch unitialized memory read */
622         u1_ref_idx0 = 0;
623 
624         sub_partition = partition_size >> 2;
625         partition_size &= 0x3;
626         u1_mb_partw = pu1_mb_partw[partition_size];
627         u1_mb_parth = pu1_mb_parth[partition_size];
628         if(sub_partition != 0)
629         {
630             u1_mb_partw >>= 1;
631             u1_mb_parth >>= 1;
632         }
633         c_refFrm0 = ps_mv->i1_ref_frame[0];
634         c_refFrm1 = ps_mv->i1_ref_frame[1];
635 
636         if((c_refFrm0 == -1) && (c_refFrm1 == -1))
637         {
638             u1_ref_idx0 = 0;
639             ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][0];
640             if(u1_mbaff && u1_is_cur_mb_fld)
641             {
642                 if(ps_cur_mb_info->u1_topmb)
643                 {
644                     pic0_poc = ps_pic_buff0->i4_top_field_order_cnt;
645                     pic1_poc = ps_pic_buff1->i4_top_field_order_cnt;
646                     cur_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
647                 }
648                 else
649                 {
650                     pic1_poc = ps_pic_buff1->i4_bottom_field_order_cnt;
651                     cur_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
652                     ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
653                     pic0_poc = ps_pic_buff0->i4_bottom_field_order_cnt;
654                     ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][MAX_REF_BUFS];
655                 }
656             }
657             else
658             {
659                 pic0_poc = ps_pic_buff0->i4_avg_poc;
660                 pic1_poc = ps_pic_buff1->i4_avg_poc;
661                 cur_poc = ps_dec->ps_cur_pic->i4_poc;
662             }
663         }
664         else
665         {
666             UWORD8 uc_i, u1_num_frw_ref_pics;
667             UWORD8 buf_id, u1_pic_type;
668             buf_id = ps_mv->u1_col_ref_pic_idx;
669             u1_pic_type = ps_mv->u1_pic_type;
670             if(ps_dec->ps_cur_slice->u1_field_pic_flag)
671             {
672                 if(s_mvdirect.u1_vert_mv_scale == FRM_TO_FLD)
673                 {
674                     u1_pic_type = TOP_FLD;
675                     if(ps_dec->ps_cur_slice->u1_bottom_field_flag)
676                         u1_pic_type = BOT_FLD;
677                 }
678             }
679             u1_num_frw_ref_pics =
680                             ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
681 
682             for(uc_i = 0; uc_i < u1_num_frw_ref_pics; uc_i++)
683             {
684                 if(ps_dec->ps_cur_slice->u1_field_pic_flag)
685                 {
686                     if(ps_dec->ps_ref_pic_buf_lx[0][uc_i]->u1_mv_buf_id == buf_id)
687                     {
688                         if(ps_dec->ps_ref_pic_buf_lx[0][uc_i]->u1_pic_type
689                                         == u1_pic_type)
690                         {
691                             u1_ref_idx0 = uc_i;
692                             break;
693                         }
694                     }
695                 }
696                 else
697                 {
698                     if(ps_dec->ps_ref_pic_buf_lx[0][uc_i]->u1_mv_buf_id == buf_id)
699                     {
700                         u1_ref_idx0 = uc_i;
701                         break;
702                     }
703                 }
704             }
705 
706             ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][u1_ref_idx0];
707             ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][0];
708 
709             if(u1_mbaff && u1_is_cur_mb_fld)
710             {
711                 pic0_poc = ps_pic_buff0->i4_top_field_order_cnt;
712                 u1_ref_idx0 <<= 1;
713                 if(s_mvdirect.u1_vert_mv_scale == ONE_TO_ONE)
714                 {
715                     if(u1_pic_type == BOT_FLD)
716                     {
717                         pic0_poc = ps_pic_buff0->i4_bottom_field_order_cnt;
718                         ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][(u1_ref_idx0
719                                         >> 1) + MAX_REF_BUFS];
720                         if(ps_cur_mb_info->u1_topmb)
721                             u1_ref_idx0++;
722                     }
723                     else
724                     {
725                         if(1 - ps_cur_mb_info->u1_topmb)
726                             u1_ref_idx0++;
727                     }
728                 }
729                 if(s_mvdirect.u1_vert_mv_scale == FRM_TO_FLD)
730                 {
731                     if(1 - ps_cur_mb_info->u1_topmb)
732                     {
733                         pic0_poc = ps_pic_buff0->i4_bottom_field_order_cnt;
734                         ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][(u1_ref_idx0
735                                         >> 1) + MAX_REF_BUFS];
736                     }
737                 }
738                 if(ps_cur_mb_info->u1_topmb)
739                 {
740                     pic1_poc = ps_pic_buff1->i4_top_field_order_cnt;
741                     cur_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
742                 }
743                 else
744                 {
745                     pic1_poc = ps_pic_buff1->i4_bottom_field_order_cnt;
746                     cur_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
747                     ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
748                 }
749             }
750             else
751             {
752                 pic0_poc = ps_pic_buff0->i4_avg_poc;
753                 pic1_poc = ps_pic_buff1->i4_avg_poc;
754                 cur_poc = ps_dec->ps_cur_pic->i4_poc;
755             }
756         }
757         {
758             WORD16 i16_td;
759 
760             if(c_refFrm0 >= 0)
761             {
762                 i2_mv_x0 = ps_mv->i2_mv[0];
763                 i2_mv_y0 = ps_mv->i2_mv[1];
764             }
765             else if(c_refFrm1 >= 0)
766             {
767                 i2_mv_x0 = ps_mv->i2_mv[2];
768                 i2_mv_y0 = ps_mv->i2_mv[3];
769             }
770             else
771             {
772                 i2_mv_x0 = 0;
773                 i2_mv_y0 = 0;
774             }
775             /* If FRM_TO_FLD or FLD_TO_FRM scale the "y" component of the colocated Mv*/
776             if(s_mvdirect.u1_vert_mv_scale == FRM_TO_FLD)
777             {
778                 i2_mv_y0 /= 2;
779             }
780             else if(s_mvdirect.u1_vert_mv_scale == FLD_TO_FRM)
781             {
782                 i2_mv_y0 *= 2;
783             }
784 
785             i16_td = pic1_poc - pic0_poc;
786             if((ps_pic_buff0->u1_is_short == 0) || (i16_td == 0))
787             {
788                 i2_mv_x1 = 0;
789                 i2_mv_y1 = 0;
790             }
791             else
792             {
793                 WORD16 i16_tb, i16_tx, i2_dist_scale_factor, i16_temp;
794 
795                 i16_td = CLIP3(-128, 127, i16_td);
796                 i16_tb = cur_poc - pic0_poc;
797                 i16_tb = CLIP3(-128, 127, i16_tb);
798 
799                 i16_tx = (16384 + ABS(SIGN_POW2_DIV(i16_td, 1))) / i16_td;
800                 i2_dist_scale_factor = CLIP3(-1024, 1023,
801                                             (((i16_tb * i16_tx) + 32) >> 6));
802                 i16_temp = (i2_mv_x0 * i2_dist_scale_factor + 128) >> 8;
803                 i2_mv_x1 = i16_temp - i2_mv_x0;
804                 i2_mv_x0 = i16_temp;
805 
806                 i16_temp = (i2_mv_y0 * i2_dist_scale_factor + 128) >> 8;
807                 i2_mv_y1 = i16_temp - i2_mv_y0;
808                 i2_mv_y0 = i16_temp;
809             }
810             {
811                 mv_pred_t *ps_mv;
812 
813                 /*u1_sub_mb_x = u1_sub_mb_num & 0x03;
814                  uc_sub_mb_y = u1_sub_mb_num >> 2;*/
815                 if(ps_dec->ps_cur_pps->u1_wted_bipred_idc)
816                 {
817                     UWORD8 u1_idx =
818                                     u1_ref_idx0
819                                                     * ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
820                     UWORD8 u1_scale_ref = u1_mbaff && u1_is_cur_mb_fld;
821                     if(u1_scale_ref)
822                         u1_idx >>= 1;
823                     pui32_weight_ofsts = (UWORD32*)&ps_dec->pu4_wt_ofsts[2
824                                     * X3(u1_idx)];
825                     if(u1_scale_ref
826                                     && (ps_dec->ps_cur_pps->u1_wted_bipred_idc
827                                                     == 2))
828                     {
829                         WORD16 i2_ref_idx;
830                         i2_ref_idx = u1_ref_idx0;
831                         i2_ref_idx *=
832                                         (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
833                                                         << 1);
834                         if(!ps_cur_mb_info->u1_topmb)
835                             i2_ref_idx +=
836                                             (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0]
837                                                             << 1)
838                                                             * (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
839                                                                             << 1);
840                         pui32_weight_ofsts =
841                                         (UWORD32*)&ps_dec->pu4_mbaff_wt_mat[2
842                                                         * X3(i2_ref_idx)];
843                     }
844                 }
845                 {
846                     pred_info_pkd_t *ps_pred_pkd;
847                    WORD16 i2_mv[2];
848                    WORD8 i1_ref_idx= 0;
849 
850                    i2_mv[0] = i2_mv_x0;
851                    i2_mv[1] = i2_mv_y0;
852 
853                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
854                 ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,PRED_L0 | PRED_L1,
855                                 ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
856                                 ps_pic_buff0->u1_pic_type);
857                 ps_dec->u4_pred_info_pkd_idx++;
858                 ps_cur_mb_info->u1_num_pred_parts++;
859 
860 
861                 }
862                 {
863                    pred_info_pkd_t *ps_pred_pkd;
864                    WORD16 i2_mv[2];
865                    WORD8 i1_ref_idx= 0;
866 
867                    i2_mv[0] = i2_mv_x1;
868                    i2_mv[1] = i2_mv_y1;
869 
870                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
871                 ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,PRED_L0 | PRED_L1,
872                                 ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
873                                 ps_pic_buff1->u1_pic_type);
874                 ps_dec->u4_pred_info_pkd_idx++;
875                 ps_cur_mb_info->u1_num_pred_parts++;
876 
877 
878                 }
879 
880                 /* Replication optimisation */
881                 s_temp_mv_pred.i2_mv[0] = i2_mv_x0;
882                 s_temp_mv_pred.i2_mv[1] = i2_mv_y0;
883                 s_temp_mv_pred.i2_mv[2] = i2_mv_x1;
884                 s_temp_mv_pred.i2_mv[3] = i2_mv_y1;
885                 s_temp_mv_pred.i1_ref_frame[0] = u1_ref_idx0;
886                 s_temp_mv_pred.i1_ref_frame[1] = 0;
887                 s_temp_mv_pred.u1_col_ref_pic_idx = ps_pic_buff0->u1_mv_buf_id;
888                 s_temp_mv_pred.u1_pic_type = ps_pic_buff0->u1_pic_type;
889                 ps_mv = ps_dec->ps_mv_cur + (u1_mb_num << 4) + u1_sub_mb_num;
890 
891                 {
892                     WORD16 i2_mv_x = 0, i2_mv_y = 0;
893                     UWORD8 u1_packed_mb_sub_mb_mode =
894                                     sub_partition ? (s_mvdirect.i1_partitionsize[i]) : ((s_mvdirect.i1_partitionsize[i])
895                                                                     << 2);
896 
897                     if(c_refFrm0 >= 0)
898                     {
899                         i2_mv_x = i2_mv_x0;
900                         i2_mv_y = i2_mv_y0;
901                     }
902                     else
903                     {
904                         i2_mv_x = i2_mv_x1;
905                         i2_mv_y = i2_mv_y1;
906                     }
907 
908                     u1_colz =
909                                     (ps_cur_mb_info->u1_mb_field_decodingflag << 1)
910                                                     | ((u1_ref_idx0 == 0)
911                                                                     && (ABS(i2_mv_x)
912                                                                                     <= 1)
913                                                                     && (ABS(i2_mv_y)
914                                                                                     <= 1));
915                     u1_colz |= (u1_packed_mb_sub_mb_mode << 4);
916                 }
917                 ih264d_rep_mv_colz(ps_dec, &s_temp_mv_pred, ps_mv, u1_sub_mb_num,
918                                    u1_colz, u1_mb_parth, u1_mb_partw);
919             }
920         }
921     }
922     /* return value set to UWORD8 to make it homogeneous  */
923     /* with decodespatialdirect                           */
924     return OK;
925 }
926 
ih264d_convert_frm_to_fld_list(struct pic_buffer_t * ps_ref_pic_buf_lx,UWORD8 * pu1_L0,dec_struct_t * ps_dec,UWORD8 u1_num_short_term_bufs)927 void ih264d_convert_frm_to_fld_list(struct pic_buffer_t *ps_ref_pic_buf_lx,
928                                     UWORD8 *pu1_L0,
929                                     dec_struct_t *ps_dec,
930                                     UWORD8 u1_num_short_term_bufs)
931 {
932     UWORD8 uc_count = *pu1_L0, i, uc_l1, uc_lx, j;
933     struct pic_buffer_t *ps_ref_lx[2], *ps_ref_pic_lx;
934     UWORD8 u1_bottom_field_flag;
935     dec_slice_params_t *ps_cur_slice;
936     UWORD8 u1_ref[2], u1_fld[2], u1_same_fld, u1_op_fld;
937     UWORD32 ui_half_num_of_sub_mbs;
938 
939     uc_l1 = 0;
940     uc_lx = 0;
941     ps_cur_slice = ps_dec->ps_cur_slice;
942     ps_ref_pic_lx = ps_ref_pic_buf_lx - MAX_REF_BUFS;
943     ps_ref_lx[0] = ps_ref_pic_buf_lx;
944     ps_ref_lx[1] = ps_ref_pic_buf_lx;
945     u1_bottom_field_flag = ps_cur_slice->u1_bottom_field_flag;
946     ui_half_num_of_sub_mbs = ((ps_dec->u2_pic_ht * ps_dec->u2_pic_wd) >> 5);
947     if(u1_bottom_field_flag)
948     {
949         u1_ref[0] = BOT_REF;
950         u1_ref[1] = TOP_REF;
951         u1_fld[0] = BOT_FLD;
952         u1_fld[1] = TOP_FLD;
953         u1_same_fld = BOT_FLD;
954         u1_op_fld = TOP_FLD;
955     }
956     else
957     {
958         u1_ref[0] = TOP_REF;
959         u1_ref[1] = BOT_REF;
960         u1_fld[0] = TOP_FLD;
961         u1_fld[1] = BOT_FLD;
962         u1_same_fld = TOP_FLD;
963         u1_op_fld = BOT_FLD;
964     }
965 
966     /* Create the field list starting with all the short term     */
967     /* frames followed by all the long term frames. No long term  */
968     /* reference field should have a list idx less than a short   */
969     /* term reference field during initiailization.               */
970 
971     for(j = 0; j < 2; j++)
972     {
973         i = ((j == 0) ? 0 : u1_num_short_term_bufs);
974         uc_count = ((j == 0) ? u1_num_short_term_bufs : *pu1_L0);
975         for(; i < uc_count; i++, ps_ref_lx[0]++)
976         {
977             /* Search field of same parity in Frame list */
978             if((ps_ref_lx[0]->u1_pic_type & u1_ref[0])) // || ((ps_ref_lx[0]->u1_picturetype & 0x3) == 0))
979             {
980                 /* Insert PIC of same parity in RefPicList */
981                 ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_lx, ps_ref_lx[0]);
982                 ps_ref_pic_lx->i4_pic_num = (ps_ref_pic_lx->i4_pic_num * 2 + 1);
983                 ps_ref_pic_lx->u1_long_term_pic_num =
984                                 (ps_ref_pic_lx->u1_long_term_frm_idx * 2 + 1);
985                 ps_ref_pic_lx->u1_pic_type = u1_same_fld;
986                 if(u1_fld[0] & BOT_FLD)
987                 {
988                     ps_ref_pic_lx->u1_pic_type = BOT_FLD;
989                     ps_ref_pic_lx->pu1_buf1 += ps_ref_pic_lx->u2_frm_wd_y;
990                     ps_ref_pic_lx->pu1_buf2 += ps_ref_pic_lx->u2_frm_wd_uv;
991                     ps_ref_pic_lx->pu1_buf3 += ps_ref_pic_lx->u2_frm_wd_uv;
992                     if(ps_ref_pic_lx->u1_picturetype & 0x3)
993                     {
994                         ps_ref_pic_lx->pu1_col_zero_flag += ui_half_num_of_sub_mbs;
995                         ps_ref_pic_lx->ps_mv += ui_half_num_of_sub_mbs;
996                     }
997                     ps_ref_pic_lx->i4_poc =
998                                     ps_ref_pic_lx->i4_bottom_field_order_cnt;
999                     ps_ref_pic_lx->i4_avg_poc =
1000                                     ps_ref_pic_lx->i4_bottom_field_order_cnt;
1001                 }
1002                 else
1003                 {
1004                     ps_ref_pic_lx->u1_pic_type = TOP_FLD;
1005                     ps_ref_pic_lx->i4_poc = ps_ref_pic_lx->i4_top_field_order_cnt;
1006                     ps_ref_pic_lx->i4_avg_poc =
1007                                     ps_ref_pic_lx->i4_top_field_order_cnt;
1008                 }
1009 
1010                 ps_ref_pic_lx++;
1011                 uc_lx++;
1012                 /* Find field of opposite parity */
1013                 if(uc_l1 < uc_count && ps_ref_lx[1])
1014                 {
1015                     while(!(ps_ref_lx[1]->u1_pic_type & u1_ref[1]))
1016                     {
1017                         ps_ref_lx[1]++;
1018                         uc_l1++;
1019                         if(uc_l1 >= uc_count)
1020                             ps_ref_lx[1] = 0;
1021                         if(!ps_ref_lx[1])
1022                             break;
1023                     }
1024 
1025                     if(ps_ref_lx[1])
1026                     {
1027                         uc_l1++;
1028                         ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_lx,
1029                                                            ps_ref_lx[1]);
1030                         ps_ref_pic_lx->u1_pic_type = u1_op_fld;
1031                         ps_ref_pic_lx->i4_pic_num = (ps_ref_pic_lx->i4_pic_num * 2);
1032                         ps_ref_pic_lx->u1_long_term_pic_num =
1033                                         (ps_ref_pic_lx->u1_long_term_frm_idx * 2);
1034                         if(u1_fld[1] & BOT_FLD)
1035                         {
1036                             ps_ref_pic_lx->u1_pic_type = BOT_FLD;
1037                             ps_ref_pic_lx->pu1_buf1 += ps_ref_pic_lx->u2_frm_wd_y;
1038                             ps_ref_pic_lx->pu1_buf2 += ps_ref_pic_lx->u2_frm_wd_uv;
1039                             ps_ref_pic_lx->pu1_buf3 += ps_ref_pic_lx->u2_frm_wd_uv;
1040                             if(ps_ref_pic_lx->u1_picturetype & 0x3)
1041                             {
1042                                 ps_ref_pic_lx->pu1_col_zero_flag +=
1043                                                 ui_half_num_of_sub_mbs;
1044                                 ps_ref_pic_lx->ps_mv += ui_half_num_of_sub_mbs;
1045                             }
1046                             ps_ref_pic_lx->i4_poc =
1047                                             ps_ref_pic_lx->i4_bottom_field_order_cnt;
1048                             ps_ref_pic_lx->i4_avg_poc =
1049                                             ps_ref_pic_lx->i4_bottom_field_order_cnt;
1050                         }
1051                         else
1052                         {
1053                             ps_ref_pic_lx->u1_pic_type = TOP_FLD;
1054                             ps_ref_pic_lx->i4_poc =
1055                                             ps_ref_pic_lx->i4_top_field_order_cnt;
1056                             ps_ref_pic_lx->i4_avg_poc =
1057                                             ps_ref_pic_lx->i4_top_field_order_cnt;
1058                         }
1059                         ps_ref_pic_lx++;
1060                         uc_lx++;
1061                         ps_ref_lx[1]++;
1062                     }
1063                 }
1064             }
1065         }
1066 
1067         /* Same parity fields are over, now insert left over opposite parity fields */
1068         /** Added  if(ps_ref_lx[1]) for error checks */
1069         if(ps_ref_lx[1])
1070         {
1071             for(; uc_l1 < uc_count; uc_l1++)
1072             {
1073                 if(ps_ref_lx[1]->u1_pic_type & u1_ref[1])
1074                 {
1075                     /* Insert PIC of opposite parity in RefPicList */
1076                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_lx,
1077                                                        ps_ref_lx[1]);
1078                     ps_ref_pic_lx->u1_pic_type = u1_op_fld;
1079                     ps_ref_pic_lx->i4_pic_num = (ps_ref_pic_lx->i4_pic_num * 2);
1080                     ps_ref_pic_lx->u1_long_term_pic_num =
1081                                     (ps_ref_pic_lx->u1_long_term_frm_idx * 2);
1082                     if(u1_op_fld == BOT_FLD)
1083                     {
1084                         ps_ref_pic_lx->u1_pic_type = BOT_FLD;
1085                         ps_ref_pic_lx->pu1_buf1 += ps_ref_pic_lx->u2_frm_wd_y;
1086                         ps_ref_pic_lx->pu1_buf2 += ps_ref_pic_lx->u2_frm_wd_uv;
1087                         ps_ref_pic_lx->pu1_buf3 += ps_ref_pic_lx->u2_frm_wd_uv;
1088                         if(ps_ref_pic_lx->u1_picturetype & 0x3)
1089                         {
1090                             ps_ref_pic_lx->pu1_col_zero_flag +=
1091                                             ui_half_num_of_sub_mbs;
1092                             ps_ref_pic_lx->ps_mv += ui_half_num_of_sub_mbs;
1093                         }
1094                         ps_ref_pic_lx->i4_poc =
1095                                         ps_ref_pic_lx->i4_bottom_field_order_cnt;
1096                         ps_ref_pic_lx->i4_avg_poc =
1097                                         ps_ref_pic_lx->i4_bottom_field_order_cnt;
1098                     }
1099                     else
1100                     {
1101                         ps_ref_pic_lx->i4_poc =
1102                                         ps_ref_pic_lx->i4_top_field_order_cnt;
1103                         ps_ref_pic_lx->i4_avg_poc =
1104                                         ps_ref_pic_lx->i4_top_field_order_cnt;
1105                     }
1106                     ps_ref_pic_lx++;
1107                     uc_lx++;
1108                     ps_ref_lx[1]++;
1109                 }
1110             }
1111         }
1112     }
1113     *pu1_L0 = uc_lx;
1114 }
1115 
ih264d_convert_frm_mbaff_list(dec_struct_t * ps_dec)1116 void ih264d_convert_frm_mbaff_list(dec_struct_t *ps_dec)
1117 {
1118     struct pic_buffer_t **ps_ref_pic_lx;
1119     UWORD8 u1_max_ref_idx, idx;
1120     UWORD16 u2_frm_wd_y, u2_frm_wd_uv;
1121     struct pic_buffer_t **ps_ref_pic_buf_lx;
1122     UWORD32 u4_half_num_of_sub_mbs = ((ps_dec->u2_pic_ht * ps_dec->u2_pic_wd) >> 5);
1123 
1124     ps_ref_pic_buf_lx = ps_dec->ps_ref_pic_buf_lx[0];
1125     ps_ref_pic_lx = ps_dec->ps_ref_pic_buf_lx[0];
1126     u1_max_ref_idx = ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
1127     for(idx = 0; idx < u1_max_ref_idx; idx++)
1128     {
1129         ps_ref_pic_lx[idx]->u1_pic_type = TOP_FLD;
1130         ps_ref_pic_lx[idx]->i4_poc = ps_ref_pic_lx[idx]->i4_top_field_order_cnt;
1131 
1132     }
1133     u2_frm_wd_y = ps_dec->u2_frm_wd_y;
1134     u2_frm_wd_uv = ps_dec->u2_frm_wd_uv;
1135 
1136     for(idx = 0; idx < u1_max_ref_idx; idx++)
1137     {
1138         *ps_ref_pic_lx[idx + MAX_REF_BUFS] = *ps_ref_pic_buf_lx[idx];
1139         ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf1 =
1140                         ps_ref_pic_buf_lx[idx]->pu1_buf1 + u2_frm_wd_y;
1141         ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf2 =
1142                         ps_ref_pic_buf_lx[idx]->pu1_buf2 + u2_frm_wd_uv;
1143         ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf3 =
1144                         ps_ref_pic_buf_lx[idx]->pu1_buf3 + u2_frm_wd_uv;
1145 
1146         ps_ref_pic_lx[idx + MAX_REF_BUFS]->u1_pic_type = BOT_FLD;
1147         ps_ref_pic_lx[idx + MAX_REF_BUFS]->i4_poc =
1148                         ps_ref_pic_buf_lx[idx]->i4_bottom_field_order_cnt;
1149         if(ps_ref_pic_buf_lx[idx]->u1_picturetype & 0x3)
1150         {
1151             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_col_zero_flag =
1152                             ps_ref_pic_buf_lx[idx]->pu1_col_zero_flag
1153                                             + u4_half_num_of_sub_mbs;
1154             ps_ref_pic_lx[idx + MAX_REF_BUFS]->ps_mv =
1155                             ps_ref_pic_buf_lx[idx]->ps_mv + u4_half_num_of_sub_mbs;
1156         }
1157     }
1158 
1159     if(ps_dec->u1_B)
1160     {
1161         ps_ref_pic_buf_lx = ps_dec->ps_ref_pic_buf_lx[1];
1162         ps_ref_pic_lx = ps_dec->ps_ref_pic_buf_lx[1];
1163         u1_max_ref_idx = ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
1164         for(idx = 0; idx < u1_max_ref_idx; idx++)
1165         {
1166             ps_ref_pic_lx[idx]->u1_pic_type = TOP_FLD;
1167             ps_ref_pic_lx[idx]->i4_poc = ps_ref_pic_lx[idx]->i4_top_field_order_cnt;
1168 
1169         }
1170 
1171         for(idx = 0; idx < u1_max_ref_idx; idx++)
1172         {
1173             *ps_ref_pic_lx[idx + MAX_REF_BUFS] = *ps_ref_pic_buf_lx[idx];
1174             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf1 =
1175                             ps_ref_pic_buf_lx[idx]->pu1_buf1 + u2_frm_wd_y;
1176             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf2 =
1177                             ps_ref_pic_buf_lx[idx]->pu1_buf2 + u2_frm_wd_uv;
1178             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf3 =
1179                             ps_ref_pic_buf_lx[idx]->pu1_buf3 + u2_frm_wd_uv;
1180             ps_ref_pic_lx[idx + MAX_REF_BUFS]->u1_pic_type = BOT_FLD;
1181             ps_ref_pic_lx[idx + MAX_REF_BUFS]->i4_poc =
1182                             ps_ref_pic_buf_lx[idx]->i4_bottom_field_order_cnt;
1183 
1184             if(ps_ref_pic_buf_lx[idx]->u1_picturetype & 0x3)
1185             {
1186                 ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_col_zero_flag =
1187                                 ps_ref_pic_buf_lx[idx]->pu1_col_zero_flag
1188                                                 + u4_half_num_of_sub_mbs;
1189                 ps_ref_pic_lx[idx + MAX_REF_BUFS]->ps_mv =
1190                                 ps_ref_pic_buf_lx[idx]->ps_mv
1191                                                 + u4_half_num_of_sub_mbs;
1192             }
1193         }
1194     }
1195 }
1196 /*!
1197  **************************************************************************
1198  * \if Function name : ih264d_init_ref_idx_lx_b \endif
1199  *
1200  * \brief
1201  *    Initializes forward and backward refernce lists for B slice decoding.
1202  *
1203  *
1204  * \return
1205  *    0 on Success and Error code otherwise
1206  **************************************************************************
1207  */
ih264d_init_ref_idx_lx_b(dec_struct_t * ps_dec)1208 void ih264d_init_ref_idx_lx_b(dec_struct_t *ps_dec)
1209 {
1210     struct pic_buffer_t *ps_ref_pic_buf_lx;
1211     dpb_manager_t *ps_dpb_mgr;
1212     struct dpb_info_t *ps_next_dpb;
1213     WORD32 i_cur_poc, i_max_st_poc, i_min_st_poc, i_ref_poc, i_temp_poc;
1214     WORD8 i;
1215     UWORD8 u1_max_lt_index, u1_min_lt_index, u1_lt_index;
1216     UWORD8 u1_field_pic_flag;
1217     dec_slice_params_t *ps_cur_slice;
1218     UWORD8 u1_L0, u1_L1;
1219     UWORD8 u1_num_short_term_bufs;
1220     UWORD8 u1_max_ref_idx_l0, u1_max_ref_idx_l1;
1221 
1222     ps_cur_slice = ps_dec->ps_cur_slice;
1223     u1_field_pic_flag = ps_cur_slice->u1_field_pic_flag;
1224     u1_max_ref_idx_l0 = ps_cur_slice->u1_num_ref_idx_lx_active[0]
1225                     << u1_field_pic_flag;
1226     u1_max_ref_idx_l1 = ps_cur_slice->u1_num_ref_idx_lx_active[1]
1227                     << u1_field_pic_flag;
1228 
1229     ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1230     /* Get the current POC */
1231     i_cur_poc = ps_dec->ps_cur_pic->i4_poc;
1232 
1233     /* Get MaxStPOC,MinStPOC,MaxLt,MinLt */
1234     i_max_st_poc = i_cur_poc;
1235     i_min_st_poc = i_cur_poc;
1236     u1_max_lt_index = MAX_REF_BUFS + 1;
1237     u1_min_lt_index = MAX_REF_BUFS + 1;
1238     /* Start from ST head */
1239     ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1240     for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
1241     {
1242         i_ref_poc = ps_next_dpb->ps_pic_buf->i4_poc;
1243         if(i_ref_poc < i_cur_poc)
1244         {
1245             /* RefPic Buf POC is before Current POC in display order */
1246             i_min_st_poc = MIN(i_min_st_poc, i_ref_poc);
1247         }
1248         else
1249         {
1250             /* RefPic Buf POC is after Current POC in display order */
1251             i_max_st_poc = MAX(i_max_st_poc, i_ref_poc);
1252         }
1253 
1254         /* Chase the next link */
1255         ps_next_dpb = ps_next_dpb->ps_prev_short;
1256     }
1257 
1258     /* Start from LT head */
1259     ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
1260     if(ps_next_dpb)
1261     {
1262         u1_max_lt_index = ps_next_dpb->u1_lt_idx;
1263         u1_min_lt_index = ps_next_dpb->u1_lt_idx;
1264     }
1265     for(i = 0; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
1266     {
1267         u1_lt_index = ps_next_dpb->u1_lt_idx;
1268         u1_max_lt_index = (UWORD8)(MAX(u1_max_lt_index, u1_lt_index));
1269         u1_min_lt_index = (UWORD8)(MIN(u1_min_lt_index, u1_lt_index));
1270 
1271         /* Chase the next link */
1272         ps_next_dpb = ps_next_dpb->ps_prev_long;
1273     }
1274 
1275     /* 1. Initialize refIdxL0 */
1276     u1_L0 = 0;
1277     if(u1_field_pic_flag)
1278     {
1279         ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[0][0];
1280         ps_ref_pic_buf_lx += MAX_REF_BUFS;
1281         i_temp_poc = i_cur_poc;
1282     }
1283     else
1284     {
1285         ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[0][0];
1286         i_temp_poc = i_cur_poc - 1;
1287     }
1288     /* Arrange all short term buffers in output order as given by POC */
1289     /* 1.1 Arrange POC's less than CurrPOC in the descending POC order starting
1290      from (CurrPOC - 1)*/
1291     for(; i_temp_poc >= i_min_st_poc; i_temp_poc--)
1292     {
1293         /* Start from ST head */
1294         ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1295         for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
1296         {
1297             if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
1298             {
1299                 /* Copy info in pic buffer */
1300                 ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
1301                                                    ps_next_dpb->ps_pic_buf);
1302                 ps_ref_pic_buf_lx++;
1303                 u1_L0++;
1304                 break;
1305             }
1306             ps_next_dpb = ps_next_dpb->ps_prev_short;
1307         }
1308     }
1309 
1310     {
1311         /* 1.2. Arrange POC's more than CurrPOC in the ascending POC order starting
1312          from (CurrPOC + 1)*/
1313         for(i_temp_poc = i_cur_poc + 1; i_temp_poc <= i_max_st_poc; i_temp_poc++)
1314         {
1315             /* Start from ST head */
1316             ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1317             for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
1318             {
1319                 if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
1320                 {
1321                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
1322                                                        ps_next_dpb->ps_pic_buf);
1323                     ps_ref_pic_buf_lx++;
1324                     u1_L0++;
1325                     break;
1326                 }
1327                 ps_next_dpb = ps_next_dpb->ps_prev_short;
1328             }
1329         }
1330     }
1331 
1332     /* 1.3 Arrange all Long term buffers in ascending order, in LongtermIndex */
1333     /* Start from ST head */
1334 
1335     u1_num_short_term_bufs = u1_L0;
1336     for(u1_lt_index = u1_min_lt_index; u1_lt_index <= u1_max_lt_index; u1_lt_index++)
1337     {
1338         ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
1339         for(i = 0; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
1340         {
1341             if(ps_next_dpb->u1_lt_idx == u1_lt_index)
1342             {
1343                 ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
1344                                                    ps_next_dpb->ps_pic_buf);
1345                 ps_ref_pic_buf_lx->u1_long_term_pic_num =
1346                                 ps_ref_pic_buf_lx->u1_long_term_frm_idx;
1347 
1348                 ps_ref_pic_buf_lx++;
1349                 u1_L0++;
1350                 break;
1351             }
1352             ps_next_dpb = ps_next_dpb->ps_prev_long;
1353         }
1354     }
1355 
1356     if(u1_field_pic_flag)
1357     {
1358         /* Initialize the rest of the entries in the */
1359         /* reference list to handle of errors        */
1360         {
1361             UWORD8 u1_i;
1362             pic_buffer_t *ps_ref_pic;
1363 
1364             ps_ref_pic = ps_dpb_mgr->ps_init_dpb[0][0] + MAX_REF_BUFS;
1365 
1366             if(NULL == ps_ref_pic->pu1_buf1)
1367             {
1368                 ps_ref_pic = ps_dec->ps_cur_pic;
1369             }
1370             for(u1_i = u1_L0; u1_i < u1_max_ref_idx_l0; u1_i++)
1371             {
1372                 *ps_ref_pic_buf_lx = *ps_ref_pic;
1373                 ps_ref_pic_buf_lx++;
1374             }
1375         }
1376         ih264d_convert_frm_to_fld_list(
1377                         ps_dpb_mgr->ps_init_dpb[0][0] + MAX_REF_BUFS, &u1_L0,
1378                         ps_dec, u1_num_short_term_bufs);
1379 
1380         ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[0][0] + u1_L0;
1381     }
1382 
1383     ps_dec->ps_cur_slice->u1_initial_list_size[0] = u1_L0;
1384 
1385     /* Initialize the rest of the entries in the */
1386     /* reference list to handle of errors        */
1387     {
1388         UWORD8 u1_i;
1389         pic_buffer_t *ps_ref_pic;
1390 
1391         ps_ref_pic = ps_dpb_mgr->ps_init_dpb[0][0];
1392 
1393         if(NULL == ps_ref_pic->pu1_buf1)
1394         {
1395             ps_ref_pic = ps_dec->ps_cur_pic;
1396         }
1397         for(u1_i = u1_L0; u1_i < u1_max_ref_idx_l0; u1_i++)
1398         {
1399             *ps_ref_pic_buf_lx = *ps_ref_pic;
1400             ps_ref_pic_buf_lx++;
1401         }
1402     }
1403     {
1404         /* 2. Initialize refIdxL1 */
1405         u1_L1 = 0;
1406         if(u1_field_pic_flag)
1407         {
1408             ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[1][0] + MAX_REF_BUFS;
1409         }
1410         else
1411         {
1412             ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[1][0];
1413         }
1414 
1415         /* 2.1. Arrange POC's more than CurrPOC in the ascending POC order starting
1416          from (CurrPOC + 1)*/
1417         for(i_temp_poc = i_cur_poc + 1; i_temp_poc <= i_max_st_poc; i_temp_poc++)
1418         {
1419             /* Start from ST head */
1420             ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1421             for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
1422             {
1423                 if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
1424                 {
1425                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
1426                                                        ps_next_dpb->ps_pic_buf);
1427                     ps_ref_pic_buf_lx++;
1428                     u1_L1++;
1429                     break;
1430                 }
1431                 ps_next_dpb = ps_next_dpb->ps_prev_short;
1432             }
1433         }
1434 
1435         if(u1_field_pic_flag)
1436         {
1437             i_temp_poc = i_cur_poc;
1438         }
1439         else
1440         {
1441             i_temp_poc = i_cur_poc - 1;
1442         }
1443 
1444         /* Arrange all short term buffers in output order as given by POC */
1445         /* 2.2 Arrange POC's less than CurrPOC in the descending POC order starting
1446          from (CurrPOC - 1)*/
1447         for(; i_temp_poc >= i_min_st_poc; i_temp_poc--)
1448         {
1449             /* Start from ST head */
1450             ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1451             for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
1452             {
1453                 if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
1454                 {
1455                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
1456                                                        ps_next_dpb->ps_pic_buf);
1457                     ps_ref_pic_buf_lx++;
1458                     u1_L1++;
1459                     break;
1460                 }
1461                 ps_next_dpb = ps_next_dpb->ps_prev_short;
1462             }
1463         }
1464 
1465         /* 2.3 Arrange all Long term buffers in ascending order, in LongtermIndex */
1466         /* Start from ST head */
1467         u1_num_short_term_bufs = u1_L1;
1468         ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
1469         for(u1_lt_index = u1_min_lt_index; u1_lt_index <= u1_max_lt_index;
1470                         u1_lt_index++)
1471         {
1472             for(i = 0; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
1473             {
1474                 if(ps_next_dpb->u1_lt_idx == u1_lt_index)
1475                 {
1476                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
1477                                                        ps_next_dpb->ps_pic_buf);
1478                     ps_ref_pic_buf_lx->u1_long_term_pic_num =
1479                                     ps_ref_pic_buf_lx->u1_long_term_frm_idx;
1480                     ps_ref_pic_buf_lx++;
1481                     u1_L1++;
1482                     break;
1483                 }
1484                 ps_next_dpb = ps_next_dpb->ps_prev_long;
1485             }
1486         }
1487 
1488         if(u1_field_pic_flag)
1489         {
1490             /* Initialize the rest of the entries in the */
1491             /* reference list to handle of errors        */
1492             {
1493                 UWORD8 u1_i;
1494                 pic_buffer_t *ps_ref_pic;
1495 
1496                 ps_ref_pic = ps_dpb_mgr->ps_init_dpb[1][0] + MAX_REF_BUFS;
1497 
1498                 if(NULL == ps_ref_pic->pu1_buf1)
1499                 {
1500                     ps_ref_pic = ps_dec->ps_cur_pic;
1501                 }
1502                 for(u1_i = u1_L1; u1_i < u1_max_ref_idx_l1; u1_i++)
1503                 {
1504                     *ps_ref_pic_buf_lx = *ps_ref_pic;
1505                     ps_ref_pic_buf_lx++;
1506                 }
1507             }
1508 
1509             ih264d_convert_frm_to_fld_list(
1510                             ps_dpb_mgr->ps_init_dpb[1][0] + MAX_REF_BUFS,
1511                             &u1_L1, ps_dec, u1_num_short_term_bufs);
1512             ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[1][0] + u1_L1;
1513         }
1514 
1515         ps_dec->ps_cur_slice->u1_initial_list_size[1] = u1_L1;
1516 
1517         /* Initialize the rest of the entries in the */
1518         /* reference list to handle of errors        */
1519         {
1520             UWORD8 u1_i;
1521             pic_buffer_t *ps_ref_pic;
1522 
1523             ps_ref_pic = ps_dpb_mgr->ps_init_dpb[1][0];
1524 
1525             if(NULL == ps_ref_pic->pu1_buf1)
1526             {
1527                 ps_ref_pic = ps_dec->ps_cur_pic;
1528             }
1529             for(u1_i = u1_L1; u1_i < u1_max_ref_idx_l1; u1_i++)
1530             {
1531                 *ps_ref_pic_buf_lx = *ps_ref_pic;
1532                 ps_ref_pic_buf_lx++;
1533             }
1534         }
1535 
1536         /* If list0 and list 1 ebtries are same then swap the 0th and 1st entry */
1537         /* of list 1                                                            */
1538         {
1539             struct pic_buffer_t *ps_ref_pic1_buf_l0, *ps_ref_pic1_buf_l1;
1540             struct pic_buffer_t s_ref_pic1_buf_temp;
1541 
1542             ps_ref_pic1_buf_l0 = ps_dpb_mgr->ps_init_dpb[0][0];
1543             ps_ref_pic1_buf_l1 = ps_dpb_mgr->ps_init_dpb[1][0];
1544 
1545             if((u1_L0 == u1_L1) && (u1_L0 > 1))
1546             {
1547                 WORD32 i_index, i_swap;
1548 
1549                 i_swap = 1;
1550 
1551                 for(i_index = 0; i_index < u1_L0; i_index++)
1552                 {
1553                     if((ps_ref_pic1_buf_l0[i_index]).pu1_buf1
1554                                     != (ps_ref_pic1_buf_l1[i_index]).pu1_buf1)
1555                     {
1556                         i_swap = 0;
1557                         break;
1558                     }
1559                 }
1560                 if(1 == i_swap)
1561                 {
1562                     memcpy(&s_ref_pic1_buf_temp, &ps_ref_pic1_buf_l1[1],
1563                            sizeof(struct pic_buffer_t));
1564                     memcpy(&ps_ref_pic1_buf_l1[1], &ps_ref_pic1_buf_l1[0],
1565                            sizeof(struct pic_buffer_t));
1566                     memcpy(&ps_ref_pic1_buf_l1[0], &s_ref_pic1_buf_temp,
1567                            sizeof(struct pic_buffer_t));
1568                 }
1569             }
1570         }
1571     }
1572 }
1573 
1574 
1575 
1576 void ih264d_get_implicit_weights(dec_struct_t *ps_dec);
1577 
1578 /*!
1579  **************************************************************************
1580  * \if Function name : ih264d_one_to_one \endif
1581  *
1582  * \brief
1583  *    Initializes forward and backward refernce lists for B slice decoding.
1584  *
1585  *
1586  * \return
1587  *    0 on Success and Error code otherwise
1588  **************************************************************************
1589  */
ih264d_one_to_one(dec_struct_t * ps_dec,struct pic_buffer_t * ps_col_pic,directmv_t * ps_direct,UWORD8 u1_wd_x,WORD32 u2_sub_mb_ofst,dec_mb_info_t * ps_cur_mb_info)1590 void ih264d_one_to_one(dec_struct_t *ps_dec,
1591                        struct pic_buffer_t *ps_col_pic,
1592                        directmv_t *ps_direct,
1593                        UWORD8 u1_wd_x,
1594                        WORD32 u2_sub_mb_ofst,
1595                        dec_mb_info_t * ps_cur_mb_info)
1596 {
1597     UWORD8 *pu1_col_zero_flag_start, u1_col_mb_pred_mode, u1_num_blks, u1_sub_mb_num;
1598     UWORD8 u1_init_colzero_flag;
1599     UNUSED(ps_cur_mb_info);
1600     pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
1601     u1_col_mb_pred_mode = pu1_col_zero_flag_start[ps_dec->u1_sub_mb_num];
1602     u1_init_colzero_flag = u1_col_mb_pred_mode & 1;
1603     u1_col_mb_pred_mode >>= 6;
1604     ps_direct->u1_vert_mv_scale = ONE_TO_ONE;
1605     ps_direct->u1_col_zeroflag_change = 0;
1606 
1607     if(u1_wd_x == MB_SIZE)
1608     {
1609         ps_dec->u1_currB_type = (!!u1_col_mb_pred_mode);
1610         if(u1_col_mb_pred_mode == PRED_16x16)
1611         {
1612             ps_direct->i1_num_partitions = 1;
1613             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
1614             ps_direct->i1_submb_num[0] = 0;
1615             ps_direct->i1_partitionsize[0] = PRED_16x16;
1616 
1617             return;
1618         }
1619         else if(u1_col_mb_pred_mode < PRED_8x8)
1620         {
1621             ps_direct->i1_num_partitions = 2;
1622             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
1623             ps_direct->i1_submb_num[0] = 0;
1624             ps_direct->i1_partitionsize[0] = u1_col_mb_pred_mode;
1625             u1_sub_mb_num = (u1_col_mb_pred_mode == PRED_16x8) ? 8 : 2;
1626             ps_direct->i1_submb_num[1] = u1_sub_mb_num;
1627             ps_direct->i4_mv_indices[1] = u2_sub_mb_ofst
1628                             + ps_direct->i1_submb_num[1];
1629             ps_direct->i1_partitionsize[1] = u1_col_mb_pred_mode;
1630             if((pu1_col_zero_flag_start[u1_sub_mb_num] & 1) != u1_init_colzero_flag)
1631                 ps_direct->u1_col_zeroflag_change = 1;
1632             return;
1633         }
1634         else
1635         {
1636             u1_num_blks = 4;
1637         }
1638     }
1639     else
1640     {
1641         u1_num_blks = 1;
1642     }
1643 
1644     {
1645         const UWORD8 *pu1_top_lt_mb_part_idx;
1646         UWORD8 u1_col_sub_mb_pred_mode, uc_blk, u1_sub_blk, u1_submb_col = 0;
1647         UWORD8 u1_num_sub_blks, uc_direct8x8inf, *pu1_col_zero_flag, u1_sub_mb_num;
1648         const UWORD8 *pu1_num_sub_mb_part =
1649                         (const UWORD8 *)gau1_ih264d_num_submb_part;
1650         UWORD8 i1_num_partitions = 0, partition_size;
1651         WORD32 mv_index;
1652         const UWORD8 *pu1_top_lt_sub_mb_idx = gau1_ih264d_submb_indx_mod_sp_drct;
1653 
1654         u1_sub_mb_num = ps_dec->u1_sub_mb_num;
1655         uc_direct8x8inf = ps_dec->ps_cur_slice->u1_direct_8x8_inference_flag;
1656         pu1_top_lt_mb_part_idx = gau1_ih264d_top_left_mb_part_indx_mod
1657                         + (PRED_8x8 << 1) + 1;
1658 
1659         for(uc_blk = 0; uc_blk < u1_num_blks; uc_blk++)
1660         {
1661             partition_size = PRED_8x8;
1662             pu1_top_lt_sub_mb_idx = gau1_ih264d_submb_indx_mod_sp_drct;
1663             if(uc_direct8x8inf == 1)
1664             {
1665                 u1_submb_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
1666                 mv_index = u2_sub_mb_ofst + u1_submb_col;
1667                 u1_num_sub_blks = 1;
1668             }
1669             else
1670             {
1671                 /* colMbPart is either 8x8, 8x4, 4x8, 4x4 */
1672                 pu1_col_zero_flag = pu1_col_zero_flag_start + u1_sub_mb_num;
1673                 u1_col_sub_mb_pred_mode = *pu1_col_zero_flag;
1674                 u1_col_sub_mb_pred_mode = (u1_col_sub_mb_pred_mode & 0x30) >> 4;
1675                 partition_size = (UWORD8)((u1_col_sub_mb_pred_mode)
1676                                 | (PRED_8x8 << 2));
1677                 mv_index = u2_sub_mb_ofst + u1_sub_mb_num;
1678                 pu1_top_lt_sub_mb_idx += (u1_col_sub_mb_pred_mode << 1);
1679                 u1_num_sub_blks = pu1_num_sub_mb_part[u1_col_sub_mb_pred_mode];
1680 
1681             }
1682 
1683             for(u1_sub_blk = 0; u1_sub_blk < u1_num_sub_blks;
1684                             u1_sub_blk++, pu1_top_lt_sub_mb_idx++)
1685             {
1686                 u1_sub_mb_num += *pu1_top_lt_sub_mb_idx;
1687                 mv_index += *pu1_top_lt_sub_mb_idx;
1688                 ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
1689                 ps_direct->i1_submb_num[i1_num_partitions] = u1_sub_mb_num;
1690                 ps_direct->i1_partitionsize[i1_num_partitions] = partition_size;
1691                 i1_num_partitions++;
1692                 if(!uc_direct8x8inf)
1693                     u1_submb_col = u1_sub_mb_num;
1694                 if((pu1_col_zero_flag_start[u1_submb_col] & 1)
1695                                 != u1_init_colzero_flag)
1696                     ps_direct->u1_col_zeroflag_change = 1;
1697             }
1698             u1_sub_mb_num = *pu1_top_lt_mb_part_idx++;
1699         }
1700         ps_direct->i1_num_partitions = i1_num_partitions;
1701     }
1702 }
1703 /*!
1704  **************************************************************************
1705  * \if Function name : ih264d_mbaff_cross_pmbair \endif
1706  *
1707  * \brief
1708  *    Initializes forward and backward refernce lists for B slice decoding.
1709  *
1710  *
1711  * \return
1712  *    0 on Success and Error code otherwise
1713  **************************************************************************
1714  */
ih264d_mbaff_cross_pmbair(dec_struct_t * ps_dec,struct pic_buffer_t * ps_col_pic,directmv_t * ps_direct,UWORD8 u1_wd_x,WORD32 u2_sub_mb_ofst,dec_mb_info_t * ps_cur_mb_info)1715 void ih264d_mbaff_cross_pmbair(dec_struct_t *ps_dec,
1716                                struct pic_buffer_t *ps_col_pic,
1717                                directmv_t *ps_direct,
1718                                UWORD8 u1_wd_x,
1719                                WORD32 u2_sub_mb_ofst,
1720                                dec_mb_info_t * ps_cur_mb_info)
1721 {
1722     UWORD8 *pu1_col_zero_flag_start, *pu1_col_zero_flag, u1_sub_mb_num,
1723                     uc_sub_mb_num_col;
1724     UWORD8 *pu1_col_zero_flag_right_half;
1725     WORD32 i4_force_8X8;
1726     UWORD8 u1_num_blks, u1_col_mb_pred_mode, uc_blk, u1_col_sub_mb_pred_mode,
1727                     u1_col_sub_mb_pred_mode_rt;
1728     UWORD8 i1_num_partitions = 0, partition_size;
1729 
1730     WORD32 mv_index;
1731 
1732     UWORD8 u1_num_sub_blks;
1733     UWORD8 u1_is_cur_mb_fld, i;
1734     UWORD8 u1_init_colzero_flag;
1735 
1736     u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
1737     u1_sub_mb_num = ps_dec->u1_sub_mb_num;
1738     ps_direct->u1_col_zeroflag_change = 0;
1739     /*pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
1740      u1_col_mb_pred_mode = pu1_col_zero_flag_start[u1_sub_mb_num];
1741      u1_init_colzero_flag = u1_col_mb_pred_mode & 1;
1742      u1_col_mb_pred_mode >>= 6; */
1743     if(0 == u1_is_cur_mb_fld)
1744     {
1745         ps_direct->u1_vert_mv_scale = FLD_TO_FRM;
1746         if(u1_wd_x == MB_SIZE)
1747         {
1748             pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
1749                             + u2_sub_mb_ofst;
1750             u1_col_mb_pred_mode = pu1_col_zero_flag_start[0];
1751             u1_init_colzero_flag = u1_col_mb_pred_mode & 1;
1752             u1_col_mb_pred_mode >>= 6;
1753 
1754 
1755             if(u1_col_mb_pred_mode & 0x2)
1756             {
1757                 ps_dec->u1_currB_type = 1;
1758                 if(u1_col_mb_pred_mode == PRED_8x16)
1759                 {
1760                     ps_direct->i1_num_partitions = 2;
1761                     ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
1762                     ps_direct->i1_submb_num[0] = 0;
1763                     ps_direct->i1_partitionsize[0] = PRED_8x16;
1764                     ps_direct->i4_mv_indices[1] = u2_sub_mb_ofst + 2;
1765                     ps_direct->i1_submb_num[1] = 2;
1766                     ps_direct->i1_partitionsize[1] = PRED_8x16;
1767                     if((pu1_col_zero_flag_start[2] & 1) != u1_init_colzero_flag)
1768                         ps_direct->u1_col_zeroflag_change = 1;
1769                 }
1770                 else
1771                 {
1772                     pu1_col_zero_flag = pu1_col_zero_flag_start + u1_sub_mb_num;
1773                     u1_col_sub_mb_pred_mode = (*pu1_col_zero_flag & 0x10);/* 8x4 or 4x4 mode */
1774 
1775                     pu1_col_zero_flag_right_half = pu1_col_zero_flag_start
1776                                     + u1_sub_mb_num + 2;
1777                     u1_col_sub_mb_pred_mode_rt =
1778                                     (*pu1_col_zero_flag_right_half & 0x10);/* 8x4 or 4x4 mode */
1779 
1780                     i4_force_8X8 = (u1_col_sub_mb_pred_mode)
1781                                     || (u1_col_sub_mb_pred_mode_rt);
1782                     if(i4_force_8X8)
1783                     {
1784                         u1_num_sub_blks = 2;
1785                         partition_size = PRED_8x8;
1786                     }
1787                     else
1788                     {
1789                         partition_size = PRED_8x16;
1790                         u1_num_sub_blks = 1;
1791                     }
1792 
1793                     for(i = 0; i < 2; i++)
1794                     {
1795                         for(uc_blk = 0; uc_blk < u1_num_sub_blks; uc_blk++)
1796                         {
1797                             uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
1798                             uc_sub_mb_num_col &= 0x7;
1799                             mv_index = u2_sub_mb_ofst + uc_sub_mb_num_col;
1800 
1801                             ps_direct->i4_mv_indices[i1_num_partitions] =
1802                                             mv_index;
1803                             ps_direct->i1_submb_num[i1_num_partitions] =
1804                                             u1_sub_mb_num;
1805                             ps_direct->i1_partitionsize[i1_num_partitions] =
1806                                             partition_size;
1807                             i1_num_partitions++;
1808                             if((pu1_col_zero_flag_start[uc_sub_mb_num_col] & 1)
1809                                             != u1_init_colzero_flag)
1810                                 ps_direct->u1_col_zeroflag_change = 1;
1811                             u1_sub_mb_num += 8;
1812                         }
1813                         u1_sub_mb_num = 2; /* move to second half of Cur MB */
1814                     }
1815                     ps_direct->i1_num_partitions = i1_num_partitions;
1816                     return;
1817                 }
1818             }
1819             else
1820             {
1821                 ps_direct->i1_num_partitions = 1;
1822                 ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
1823                 ps_direct->i1_submb_num[0] = 0;
1824                 ps_direct->i1_partitionsize[0] = PRED_16x16;
1825                 ps_dec->u1_currB_type = 0;
1826                 return;
1827             }
1828         }
1829         else
1830         {
1831             uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
1832             uc_sub_mb_num_col &= 0x7;
1833 
1834             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + uc_sub_mb_num_col;
1835             ps_direct->i1_submb_num[0] = u1_sub_mb_num;
1836             ps_direct->i1_partitionsize[0] = PRED_8x8;
1837             ps_direct->i1_num_partitions = 1;
1838         }
1839     }
1840     else
1841     {
1842         ps_direct->u1_vert_mv_scale = FRM_TO_FLD;
1843         pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
1844         u1_init_colzero_flag = pu1_col_zero_flag_start[0] & 1;
1845 
1846         if(u1_wd_x == MB_SIZE)
1847         {
1848             UWORD8 u1_submb_col;
1849             UWORD8 *puc_colZeroFlagStart_bot_mb, uc_colMbPredMode_bot_mb;
1850 
1851             pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
1852                             + u2_sub_mb_ofst;
1853             u1_col_mb_pred_mode = pu1_col_zero_flag_start[u1_sub_mb_num] >> 6;
1854 
1855             puc_colZeroFlagStart_bot_mb = ps_col_pic->pu1_col_zero_flag
1856                             + u2_sub_mb_ofst + 16;
1857             uc_colMbPredMode_bot_mb = puc_colZeroFlagStart_bot_mb[8] >> 6;
1858 
1859             i4_force_8X8 = (u1_col_mb_pred_mode & 0x2)
1860                             || (uc_colMbPredMode_bot_mb & 0x2);
1861             if(i4_force_8X8)
1862             {
1863                 u1_num_blks = 2;
1864                 partition_size = PRED_8x8;
1865             }
1866             else
1867             {
1868                 u1_num_blks = 1;
1869                 partition_size = PRED_16x8;
1870             }
1871 
1872             ps_dec->u1_currB_type = 1;
1873             /*As this mb is derived from 2 Mbs min no of partitions = 2*/
1874             for(i = 0; i < 2; i++)
1875             {
1876 
1877                 pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
1878                                 + u2_sub_mb_ofst;
1879                 u1_col_mb_pred_mode = pu1_col_zero_flag_start[u1_sub_mb_num] >> 6;
1880 
1881                 for(uc_blk = 0; uc_blk < u1_num_blks; uc_blk++)
1882                 {
1883                     u1_submb_col = (u1_sub_mb_num & 0x7) ? 1 : 0;
1884                     u1_submb_col += u1_sub_mb_num;
1885                     mv_index = u2_sub_mb_ofst + u1_submb_col;
1886 
1887 
1888                     ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
1889                     ps_direct->i1_submb_num[i1_num_partitions] = u1_sub_mb_num;
1890                     ps_direct->i1_partitionsize[i1_num_partitions] =
1891                                     partition_size;
1892                     i1_num_partitions++;
1893                     if((pu1_col_zero_flag_start[u1_submb_col] & 1)
1894                                     != u1_init_colzero_flag)
1895                         ps_direct->u1_col_zeroflag_change = 1;
1896                     u1_sub_mb_num += 2;
1897                 }
1898                 u1_sub_mb_num = 8; /* move to second half of Cur MB */
1899                 u2_sub_mb_ofst += 16;/* move to next Colocated MB */
1900             }
1901             ps_direct->i1_num_partitions = i1_num_partitions;
1902             return;
1903         }
1904         else
1905         {
1906             uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
1907             uc_sub_mb_num_col &= 0xb;
1908             u2_sub_mb_ofst += (u1_sub_mb_num >> 3) ? 16 : 0;
1909 
1910             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + uc_sub_mb_num_col;
1911             ps_direct->i1_submb_num[0] = u1_sub_mb_num;
1912             ps_direct->i1_partitionsize[0] = PRED_8x8;
1913             ps_direct->i1_num_partitions = 1;
1914             return;
1915         }
1916     }
1917 }
1918 /*!
1919  **************************************************************************
1920  * \if Function name : ih264d_cal_col_pic \endif
1921  *
1922  * \brief
1923  *    Finds the colocated picture.
1924  *
1925  *
1926  * \return
1927  *    0 on Success and Error code otherwise
1928  **************************************************************************
1929  */
ih264d_cal_col_pic(dec_struct_t * ps_dec)1930 WORD32 ih264d_cal_col_pic(dec_struct_t *ps_dec)
1931 {
1932     struct pic_buffer_t* ps_col_pic = ps_dec->ps_col_pic;
1933     UWORD8 uc_curpictype, uc_colpictype;
1934     ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][0];
1935     uc_curpictype = (ps_dec->ps_cur_pic->u1_picturetype & 0x7);
1936     uc_colpictype = (ps_col_pic->u1_picturetype & 0x7);
1937     if(uc_curpictype == FRM_PIC)
1938     {
1939         if(uc_colpictype == FRM_PIC)
1940             ps_dec->pf_parse_mvdirect = ih264d_one_to_one;
1941         else if(uc_colpictype == COMP_FLD_PAIR)
1942         {
1943             ps_dec->pf_parse_mvdirect = ih264d_fld_to_frm;
1944             if(ps_col_pic->i4_top_field_order_cnt
1945                             >= ps_col_pic->i4_bottom_field_order_cnt)
1946             {
1947                 struct pic_buffer_t* ps_tempPic = ps_col_pic;
1948                 UWORD32 ui_half_num_of_sub_mbs = ((ps_dec->u2_pic_ht
1949                                 * ps_dec->u2_pic_wd) >> 5);
1950                 ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
1951                 /* memcpy ps_tempPic to ps_col_pic */
1952                 *ps_col_pic = *ps_tempPic;
1953                 ps_col_pic->pu1_buf1 = ps_tempPic->pu1_buf1
1954                                 + ps_tempPic->u2_frm_wd_y;
1955                 ps_col_pic->pu1_buf2 = ps_tempPic->pu1_buf2
1956                                 + ps_tempPic->u2_frm_wd_uv;
1957                 ps_col_pic->pu1_buf3 = ps_tempPic->pu1_buf3
1958                                 + ps_tempPic->u2_frm_wd_uv;
1959                 ps_col_pic->pu1_col_zero_flag = ps_tempPic->pu1_col_zero_flag
1960                                 + ui_half_num_of_sub_mbs;
1961                 ps_col_pic->ps_mv = ps_tempPic->ps_mv + ui_half_num_of_sub_mbs;
1962 
1963 
1964                 ps_col_pic->u1_pic_type = 0;/*complementary reference field pair-refering as frame */
1965 
1966 
1967 
1968             }
1969         }
1970         else
1971         {
1972             UWORD32 i4_error_code;
1973             i4_error_code = ERROR_DBP_MANAGER_T;
1974 //          i4_error_code |= 1<<IVD_CORRUPTEDDATA;
1975             return i4_error_code;
1976         }
1977     }
1978     else if(uc_curpictype == AFRM_PIC)
1979     {
1980         ps_dec->pf_parse_mvdirect = ih264d_fld_to_mbaff;
1981     }
1982     else /* must be a field*/
1983     {
1984         if(uc_colpictype == FRM_PIC)
1985             ps_dec->pf_parse_mvdirect = ih264d_frm_to_fld;
1986         else if(uc_colpictype == AFRM_PIC)
1987             ps_dec->pf_parse_mvdirect = ih264d_mbaff_to_fld;
1988         else
1989             ps_dec->pf_parse_mvdirect = ih264d_one_to_one;
1990     }
1991     ps_dec->ps_col_pic = ps_col_pic;
1992     return OK;
1993 }
1994 
1995 /*!
1996  **************************************************************************
1997  * \if Function name : ih264d_frm_to_fld \endif
1998  *
1999  * \brief
2000  *    Initializes forward and backward refernce lists for B slice decoding.
2001  *
2002  *
2003  * \return
2004  *    0 on Success and Error code otherwise
2005  **************************************************************************
2006  */
ih264d_frm_to_fld(dec_struct_t * ps_dec,struct pic_buffer_t * ps_col_pic,directmv_t * ps_direct,UWORD8 u1_wd_x,WORD32 u2_sub_mb_ofst,dec_mb_info_t * ps_cur_mb_info)2007 void ih264d_frm_to_fld(dec_struct_t *ps_dec,
2008                        struct pic_buffer_t *ps_col_pic,
2009                        directmv_t *ps_direct,
2010                        UWORD8 u1_wd_x,
2011                        WORD32 u2_sub_mb_ofst,
2012                        dec_mb_info_t * ps_cur_mb_info)
2013 {
2014     UWORD8 *pu1_col_zero_flag_start, u1_sub_mb_num;
2015     UWORD8 u1_num_blks, u1_col_mb_pred_mode, uc_blk;
2016     UWORD8 i1_num_partitions = 0, partition_size, i;
2017     WORD32 mv_index;
2018     UWORD32 increment;
2019     WORD32 i4_force_8X8;
2020     UNUSED(ps_cur_mb_info);
2021     ps_direct->u1_col_zeroflag_change = 1;
2022     ps_direct->u1_vert_mv_scale = FRM_TO_FLD;
2023     u1_sub_mb_num = ps_dec->u1_sub_mb_num;
2024 
2025     /* new calculation specific to this function */
2026     if((ps_col_pic->u1_picturetype & 0x7) == FRM_PIC)
2027     {
2028         UWORD16 u2_frm_wd_in_mbs = ps_dec->u2_frm_wd_in_mbs;
2029         increment = (u2_frm_wd_in_mbs << 4);
2030         /*mbAddrCol = mbAddrCol1 */
2031         u2_sub_mb_ofst = (ps_dec->u2_mbx
2032                         + (2 * ps_dec->u2_mby * u2_frm_wd_in_mbs)) << 4;
2033     }
2034     else
2035         increment = 16;
2036 
2037     if(u1_wd_x == MB_SIZE)
2038     {
2039         ps_dec->u1_currB_type = 1;
2040 
2041         {
2042             UWORD8 *puc_colZeroFlagStart_bot_mb, uc_colMbPredMode_bot_mb;
2043 
2044             pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
2045                             + u2_sub_mb_ofst;
2046             u1_col_mb_pred_mode = (*pu1_col_zero_flag_start >> 6);
2047 
2048             puc_colZeroFlagStart_bot_mb = ps_col_pic->pu1_col_zero_flag
2049                             + u2_sub_mb_ofst + increment;
2050             uc_colMbPredMode_bot_mb = (*puc_colZeroFlagStart_bot_mb >> 6);
2051 
2052             i4_force_8X8 = (u1_col_mb_pred_mode & 0x2)
2053                             || (uc_colMbPredMode_bot_mb & 0x2);
2054 
2055             if(i4_force_8X8)
2056             {
2057                 u1_num_blks = 2;
2058                 partition_size = PRED_8x8;
2059             }
2060             else
2061             {
2062                 partition_size = PRED_16x8;
2063                 u1_num_blks = 1;
2064             }
2065         }
2066 
2067         /*As this mb is derived from 2 Mbs, min no of partitions = 2*/
2068         for(i = 0; i < 2; i++)
2069         {
2070             for(uc_blk = 0; uc_blk < u1_num_blks; uc_blk++)
2071             {
2072                 mv_index = u2_sub_mb_ofst + u1_sub_mb_num;
2073                 mv_index += (u1_sub_mb_num & 0x7) ? 1 : 0;
2074 
2075                 ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
2076                 ps_direct->i1_submb_num[i1_num_partitions] = u1_sub_mb_num;
2077                 ps_direct->i1_partitionsize[i1_num_partitions] = partition_size;
2078                 i1_num_partitions++;
2079 
2080                 u1_sub_mb_num += 2;
2081             }
2082             u1_sub_mb_num = 8; /* move to second half of Cur MB */
2083             u2_sub_mb_ofst += increment;/* move to next Colocated MB */
2084         }
2085         ps_direct->i1_num_partitions = i1_num_partitions;
2086         return;
2087     }
2088     else
2089     {
2090         UWORD8 u1_sub_mb_num_col;
2091         u1_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
2092         u1_sub_mb_num_col &= 0xb;
2093         u2_sub_mb_ofst += (u1_sub_mb_num >> 3) ? increment : 0;
2094 
2095         ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + u1_sub_mb_num_col;
2096         ps_direct->i1_submb_num[0] = u1_sub_mb_num;
2097         ps_direct->i1_partitionsize[0] = PRED_8x8;
2098         ps_direct->i1_num_partitions = 1;
2099         return;
2100     }
2101 }
2102 /*!
2103  **************************************************************************
2104  * \if Function name : ih264d_fld_to_frm \endif
2105  *
2106  * \brief
2107  *    Initializes forward and backward refernce lists for B slice decoding.
2108  *
2109  *
2110  * \return
2111  *    0 on Success and Error code otherwise
2112  **************************************************************************
2113  */
ih264d_fld_to_frm(dec_struct_t * ps_dec,struct pic_buffer_t * ps_col_pic,directmv_t * ps_direct,UWORD8 u1_wd_x,WORD32 u2_sub_mb_ofst,dec_mb_info_t * ps_cur_mb_info)2114 void ih264d_fld_to_frm(dec_struct_t *ps_dec,
2115                        struct pic_buffer_t *ps_col_pic,
2116                        directmv_t *ps_direct,
2117                        UWORD8 u1_wd_x,
2118                        WORD32 u2_sub_mb_ofst,
2119                        dec_mb_info_t * ps_cur_mb_info)
2120 {
2121     UWORD8 *pu1_col_zero_flag_start, *pu1_col_zero_flag,
2122                     *pu1_col_zero_flag_right_half, u1_sub_mb_num, uc_sub_mb_num_col;
2123     UWORD8 u1_col_mb_pred_mode, uc_blk;
2124     WORD32 i4_force_8X8;
2125 
2126     UNUSED(ps_cur_mb_info);
2127     ps_direct->u1_vert_mv_scale = FLD_TO_FRM;
2128     ps_direct->u1_col_zeroflag_change = 1;
2129     /* new calculation specific to this function for u2_sub_mb_ofst*/
2130     u2_sub_mb_ofst = (ps_dec->u2_mbx
2131                     + ((ps_dec->u2_mby >> 1) * ps_dec->u2_frm_wd_in_mbs)) << 4;
2132     u2_sub_mb_ofst += ((ps_dec->u2_mby & 1) << 3);
2133 
2134     if(u1_wd_x == MB_SIZE)
2135     {
2136         pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
2137         u1_col_mb_pred_mode = (*pu1_col_zero_flag_start >> 6);
2138         ps_dec->u1_currB_type = (!!u1_col_mb_pred_mode);
2139 
2140         if(u1_col_mb_pred_mode & 0x2)
2141         {
2142             if(u1_col_mb_pred_mode == PRED_8x16)
2143             {
2144                 ps_direct->i1_num_partitions = 2;
2145                 ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
2146                 ps_direct->i1_submb_num[0] = 0;
2147                 ps_direct->i1_partitionsize[0] = PRED_8x16;
2148                 ps_direct->i4_mv_indices[1] = u2_sub_mb_ofst + 2;
2149                 ps_direct->i1_submb_num[1] = 2;
2150                 ps_direct->i1_partitionsize[1] = PRED_8x16;
2151             }
2152             else
2153             {
2154                 UWORD8 i1_num_partitions = 0, partition_size;
2155                 UWORD32 mv_index;
2156                 UWORD8 u1_num_sub_blks, i, u1_col_sub_mb_pred_mode,
2157                                 u1_col_sub_mb_pred_mode_rt;
2158 
2159                 u1_sub_mb_num = ps_dec->u1_sub_mb_num;
2160 
2161                 pu1_col_zero_flag = pu1_col_zero_flag_start + u1_sub_mb_num;
2162                 u1_col_sub_mb_pred_mode = (*pu1_col_zero_flag & 0x10);/* 8x4 or 4x4 mode */
2163 
2164                 pu1_col_zero_flag_right_half = pu1_col_zero_flag_start + u1_sub_mb_num
2165                                 + 2;
2166                 u1_col_sub_mb_pred_mode_rt = (*pu1_col_zero_flag_right_half
2167                                 & 0x10);/* 8x4 or 4x4 mode */
2168 
2169                 i4_force_8X8 = (u1_col_sub_mb_pred_mode)
2170                                 || (u1_col_sub_mb_pred_mode_rt);
2171                 if(i4_force_8X8)
2172                 {
2173                     u1_num_sub_blks = 2;
2174                     partition_size = PRED_8x8;
2175                 }
2176                 else
2177                 {
2178                     partition_size = PRED_8x16;
2179                     u1_num_sub_blks = 1;
2180                 }
2181 
2182                 for(i = 0; i < 2; i++)
2183                 {
2184                     for(uc_blk = 0; uc_blk < u1_num_sub_blks; uc_blk++)
2185                     {
2186                         uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
2187                         uc_sub_mb_num_col &= 0x7;
2188                         mv_index = u2_sub_mb_ofst + uc_sub_mb_num_col;
2189 
2190                         ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
2191                         ps_direct->i1_submb_num[i1_num_partitions] =
2192                                         u1_sub_mb_num;
2193                         ps_direct->i1_partitionsize[i1_num_partitions] =
2194                                         partition_size;
2195                         i1_num_partitions++;
2196                         u1_sub_mb_num += 8;
2197                     }
2198 
2199                     u1_sub_mb_num = 2; /* move to second half of Cur MB */
2200 
2201                 }
2202                 ps_direct->i1_num_partitions = i1_num_partitions;
2203                 return;
2204             }
2205         }
2206         else
2207         {
2208             ps_direct->i1_num_partitions = 1;
2209             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
2210             ps_direct->i1_submb_num[0] = 0;
2211             ps_direct->i1_partitionsize[0] = PRED_16x16;
2212             return;
2213         }
2214     }
2215     else
2216     {
2217         u1_sub_mb_num = ps_dec->u1_sub_mb_num;
2218         uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
2219         uc_sub_mb_num_col &= 0x7;
2220 
2221         ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + uc_sub_mb_num_col;
2222         ps_direct->i1_submb_num[0] = u1_sub_mb_num;
2223         ps_direct->i1_partitionsize[0] = PRED_8x8;
2224         ps_direct->i1_num_partitions = 1;
2225     }
2226 }
2227 /*!
2228  **************************************************************************
2229  * \if Function name : ih264d_one_to_one \endif
2230  *
2231  * \brief
2232  *    Initializes forward and backward refernce lists for B slice decoding.
2233  *
2234  *
2235  * \return
2236  *    0 on Success and Error code otherwise
2237  **************************************************************************
2238  */
ih264d_mbaff_to_fld(dec_struct_t * ps_dec,struct pic_buffer_t * ps_col_pic,directmv_t * ps_direct,UWORD8 u1_wd_x,WORD32 u2_sub_mb_ofst,dec_mb_info_t * ps_cur_mb_info)2239 void ih264d_mbaff_to_fld(dec_struct_t *ps_dec,
2240                          struct pic_buffer_t *ps_col_pic,
2241                          directmv_t *ps_direct,
2242                          UWORD8 u1_wd_x,
2243                          WORD32 u2_sub_mb_ofst,
2244                          dec_mb_info_t * ps_cur_mb_info)
2245 {
2246     UWORD8* pu1_col_zero_flag, u1_iscol_mb_fld;
2247     u2_sub_mb_ofst <<= 1;
2248     pu1_col_zero_flag = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
2249     u1_iscol_mb_fld = (*pu1_col_zero_flag & 0x2) >> 1;
2250     if(u1_iscol_mb_fld)
2251     {
2252         u2_sub_mb_ofst += (ps_dec->ps_cur_slice->u1_bottom_field_flag << 4);
2253         ih264d_one_to_one(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
2254                           u2_sub_mb_ofst, ps_cur_mb_info);
2255     }
2256     else
2257         ih264d_frm_to_fld(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
2258                           u2_sub_mb_ofst, ps_cur_mb_info);
2259 }
2260 /*!
2261  **************************************************************************
2262  * \if Function name : ih264d_one_to_one \endif
2263  *
2264  * \brief
2265  *    Initializes forward and backward refernce lists for B slice decoding.
2266  *
2267  *
2268  * \return
2269  *    0 on Success and Error code otherwise
2270  **************************************************************************
2271  */
ih264d_fld_to_mbaff(dec_struct_t * ps_dec,struct pic_buffer_t * ps_col_pic,directmv_t * ps_direct,UWORD8 u1_wd_x,WORD32 u2_sub_mb_ofst,dec_mb_info_t * ps_cur_mb_info)2272 void ih264d_fld_to_mbaff(dec_struct_t *ps_dec,
2273                          struct pic_buffer_t *ps_col_pic,
2274                          directmv_t *ps_direct,
2275                          UWORD8 u1_wd_x,
2276                          WORD32 u2_sub_mb_ofst,
2277                          dec_mb_info_t * ps_cur_mb_info)
2278 {
2279     if((ps_col_pic->u1_picturetype & 0x7) == COMP_FLD_PAIR)
2280     {
2281         /* first calculate the colocated picture which varies with Mb */
2282         UWORD8 u1_is_cur_mb_fld;
2283         u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
2284         u2_sub_mb_ofst = (u2_sub_mb_ofst & 0xffe0); /* mbaddrCol5 = curmbaddr/2;*/
2285         u2_sub_mb_ofst >>= 1;
2286 
2287         ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][0];
2288         if(u1_is_cur_mb_fld)
2289         {
2290             if(1 - ps_cur_mb_info->u1_topmb)
2291                 ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
2292 
2293             ih264d_one_to_one(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
2294                               u2_sub_mb_ofst, ps_cur_mb_info);
2295         }
2296         else
2297         {
2298 
2299             if(ABS(ps_col_pic->i4_top_field_order_cnt
2300                             - ps_dec->ps_cur_pic->i4_poc) >=
2301                             ABS(ps_dec->ps_cur_pic->i4_poc - ps_col_pic->i4_bottom_field_order_cnt))
2302             {
2303                 ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
2304             }
2305 
2306             if(ps_cur_mb_info->u1_topmb == 0)
2307                 u2_sub_mb_ofst += 8;
2308             ih264d_mbaff_cross_pmbair(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
2309                                       u2_sub_mb_ofst, ps_cur_mb_info);
2310         }
2311         ps_dec->ps_col_pic = ps_col_pic;
2312     }
2313     else
2314     {
2315         UWORD8* pu1_col_zero_flag = ps_col_pic->pu1_col_zero_flag
2316                         + u2_sub_mb_ofst;
2317         UWORD8 temp, u1_is_cur_mb_fld, u1_iscol_mb_fld;
2318 
2319         u1_iscol_mb_fld = (*pu1_col_zero_flag & 0x2) >> 1;
2320         u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
2321         temp = (u1_iscol_mb_fld ^ u1_is_cur_mb_fld);
2322 
2323         if(temp == 0)
2324             ih264d_one_to_one(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
2325                               u2_sub_mb_ofst, ps_cur_mb_info);
2326         else
2327         {
2328             u2_sub_mb_ofst &= 0xffef;
2329             if(u1_is_cur_mb_fld == 0)
2330             {
2331                 if(ABS(ps_col_pic->i4_top_field_order_cnt
2332                                 - ps_dec->ps_cur_pic->i4_poc) >=
2333                                 ABS(ps_dec->ps_cur_pic->i4_poc - ps_col_pic->i4_bottom_field_order_cnt))
2334                 {
2335                     u2_sub_mb_ofst += 0x10;
2336                 }
2337                 if(ps_cur_mb_info->u1_topmb == 0)
2338                     u2_sub_mb_ofst += 8;
2339             }
2340             ih264d_mbaff_cross_pmbair(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
2341                                       u2_sub_mb_ofst, ps_cur_mb_info);
2342         }
2343     }
2344 }
2345 
2346