xref: /aosp_15_r20/external/libavc/decoder/ih264d_parse_slice.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
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_parse_slice.c
23  *
24  * \brief
25  *    Contains routines that decodes a slice NAL unit
26  *
27  * \date
28  *    19/12/2002
29  *
30  * \author  AI
31  **************************************************************************
32  */
33 #include <string.h>
34 #include "ih264_typedefs.h"
35 #include "ih264_macros.h"
36 #include "ih264_platform_macros.h"
37 #include "ithread.h"
38 #include "ih264d_structs.h"
39 #include "ih264d_debug.h"
40 #include "ih264d_bitstrm.h"
41 #include "ih264d_parse_mb_header.h"
42 #include "ih264d_process_bslice.h"
43 #include "ih264d_process_pslice.h"
44 #include "ih264d_parse_cavlc.h"
45 #include "ih264d_utils.h"
46 #include "ih264d_deblocking.h"
47 #include "ih264d_defs.h"
48 #include "ih264d_error_handler.h"
49 #include "ih264d_tables.h"
50 #include "ih264d_defs.h"
51 #include "ih264d_mem_request.h"
52 #include "ih264d_parse_islice.h"
53 #include "ih264d_parse_slice.h"
54 #include "ih264d_mvpred.h"
55 #include "ih264d_mb_utils.h"
56 
57 #include "ih264d_defs.h"
58 #include "ih264d_quant_scaling.h"
59 
60 #include "ih264d_inter_pred.h"
61 
62 #include "ih264d_sei.h"
63 #include "ih264d.h"
64 #include "ih264_error.h"
65 #include "ih264_disp_mgr.h"
66 #include "ih264_buf_mgr.h"
67 
68 #include "ih264d_thread_parse_decode.h"
69 #include "ih264d_thread_compute_bs.h"
70 #include "ih264d_dpb_manager.h"
71 #include <assert.h>
72 #include "ih264d_parse_islice.h"
73 #define RET_LAST_SKIP  0x80000000
74 
75 WORD32 check_app_out_buf_size(dec_struct_t *ps_dec);
76 /*!
77  **************************************************************************
78  * \if Function name : ih264d_form_pred_weight_matrix \endif
79  *
80  * \brief
81  *    Forms pred weight matrix.
82  *
83  * \return
84  *    None
85  *
86  **************************************************************************
87  */
88 
ih264d_form_pred_weight_matrix(dec_struct_t * ps_dec)89 void ih264d_form_pred_weight_matrix(dec_struct_t *ps_dec)
90 {
91     dec_slice_params_t *ps_cur_slice;
92     UWORD8 uc_num_ref_idx_l0_active, uc_num_ref_idx_l1_active;
93     UWORD8 i, j;
94     UWORD32 *pu4_mat_iwt_ofst;
95     UWORD16 i2_idx;
96     UWORD32 *pui32_weight_offset_l0, *pui32_weight_offset_l1;
97     UWORD32 u4_temp;
98 
99     ps_cur_slice = ps_dec->ps_cur_slice;
100     uc_num_ref_idx_l0_active = ps_cur_slice->u1_num_ref_idx_lx_active[0];
101     uc_num_ref_idx_l1_active = ps_cur_slice->u1_num_ref_idx_lx_active[1];
102 
103     pu4_mat_iwt_ofst = ps_dec->pu4_wts_ofsts_mat;
104 
105     if(ps_cur_slice->u1_slice_type == B_SLICE)
106     {
107         for(i = 0; i < uc_num_ref_idx_l0_active; i++)
108         {
109             pui32_weight_offset_l0 = ps_cur_slice->u4_wt_ofst_lx[0][i];
110             for(j = 0; j < uc_num_ref_idx_l1_active; j++)
111             {
112                 pui32_weight_offset_l1 = ps_cur_slice->u4_wt_ofst_lx[1][j];
113                 i2_idx = i * uc_num_ref_idx_l0_active + j;
114                 i2_idx = X3(i2_idx);
115                 /*        u4_temp = (pui32_weight_offset_l0[0] | (pui32_weight_offset_l1[0] << 16));
116                  pu4_mat_iwt_ofst[0] = u4_temp;
117                  u4_temp = (pui32_weight_offset_l0[1] | (pui32_weight_offset_l1[1] << 16));
118                  pu4_mat_iwt_ofst[1] = u4_temp;
119                  u4_temp = (pui32_weight_offset_l0[2] | (pui32_weight_offset_l1[2] << 16));
120                  pu4_mat_iwt_ofst[2] = u4_temp;
121                  pu4_mat_iwt_ofst += 3;*/
122                 pu4_mat_iwt_ofst[0] = pui32_weight_offset_l0[0];
123                 pu4_mat_iwt_ofst[1] = pui32_weight_offset_l1[0];
124                 pu4_mat_iwt_ofst[2] = pui32_weight_offset_l0[1];
125                 pu4_mat_iwt_ofst[3] = pui32_weight_offset_l1[1];
126                 pu4_mat_iwt_ofst[4] = pui32_weight_offset_l0[2];
127                 pu4_mat_iwt_ofst[5] = pui32_weight_offset_l1[2];
128                 pu4_mat_iwt_ofst += 6;
129             }
130         }
131     }
132     else
133     {
134         for(i = 0; i < uc_num_ref_idx_l0_active; i++)
135         {
136             pui32_weight_offset_l0 = ps_cur_slice->u4_wt_ofst_lx[0][i];
137             i2_idx = X3(i);
138             u4_temp = (UWORD32)pui32_weight_offset_l0[0];
139             pu4_mat_iwt_ofst[0] = u4_temp;
140             u4_temp = (UWORD32)pui32_weight_offset_l0[1];
141             pu4_mat_iwt_ofst[2] = u4_temp;
142             u4_temp = (UWORD32)pui32_weight_offset_l0[2];
143             pu4_mat_iwt_ofst[4] = u4_temp;
144             pu4_mat_iwt_ofst += 6;
145         }
146     }
147 }
148 
149 
150 /*!
151  **************************************************************************
152  * \if Function name :  init_firstSliceParam \endif
153  *
154  * \brief
155  *    Initialize the Parameter required for all the slices for a picture
156  *
157  * \return           : Nothing
158  *
159  **************************************************************************
160  */
161 
ih264d_start_of_pic(dec_struct_t * ps_dec,WORD32 i4_poc,pocstruct_t * ps_temp_poc,UWORD16 u2_frame_num,dec_pic_params_t * ps_pps)162 WORD32 ih264d_start_of_pic(dec_struct_t *ps_dec,
163                          WORD32 i4_poc,
164                          pocstruct_t *ps_temp_poc,
165                          UWORD16 u2_frame_num,
166                          dec_pic_params_t *ps_pps)
167 {
168     pocstruct_t *ps_prev_poc = &ps_dec->s_cur_pic_poc;
169     pocstruct_t *ps_cur_poc = ps_temp_poc;
170 
171     pic_buffer_t *pic_buf;
172 
173     ivd_video_decode_op_t * ps_dec_output =
174                     (ivd_video_decode_op_t *)ps_dec->pv_dec_out;
175     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
176     dec_seq_params_t *ps_seq = ps_pps->ps_sps;
177     UWORD8 u1_bottom_field_flag = ps_cur_slice->u1_bottom_field_flag;
178     UWORD8 u1_field_pic_flag = ps_cur_slice->u1_field_pic_flag;
179     /* high profile related declarations */
180     high_profile_tools_t s_high_profile;
181     WORD32 ret;
182 
183     H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
184 
185     /* check output buffer size given by the application */
186     if(check_app_out_buf_size(ps_dec) != IV_SUCCESS)
187         return IVD_DISP_FRM_ZERO_OP_BUF_SIZE;
188 
189     ps_prev_poc->i4_pic_order_cnt_lsb = ps_cur_poc->i4_pic_order_cnt_lsb;
190     ps_prev_poc->i4_pic_order_cnt_msb = ps_cur_poc->i4_pic_order_cnt_msb;
191     ps_prev_poc->i4_delta_pic_order_cnt_bottom =
192                     ps_cur_poc->i4_delta_pic_order_cnt_bottom;
193     ps_prev_poc->i4_delta_pic_order_cnt[0] =
194                     ps_cur_poc->i4_delta_pic_order_cnt[0];
195     ps_prev_poc->i4_delta_pic_order_cnt[1] =
196                     ps_cur_poc->i4_delta_pic_order_cnt[1];
197     ps_prev_poc->u1_bot_field = ps_dec->ps_cur_slice->u1_bottom_field_flag;
198     ps_prev_poc->i4_prev_frame_num_ofst = ps_cur_poc->i4_prev_frame_num_ofst;
199     ps_prev_poc->u2_frame_num = u2_frame_num;
200     ps_dec->i1_prev_mb_qp_delta = 0;
201     ps_dec->i1_next_ctxt_idx = 0;
202 
203 
204     ps_dec->u4_nmb_deblk = 0;
205     if(ps_dec->u4_num_cores == 1)
206        ps_dec->u4_nmb_deblk = 1;
207 
208 
209 
210     if(ps_seq->u1_mb_aff_flag == 1)
211     {
212         ps_dec->u4_nmb_deblk = 0;
213         if(ps_dec->u4_num_cores > 2)
214             ps_dec->u4_num_cores = 2;
215     }
216 
217         ps_dec->u4_use_intrapred_line_copy = 0;
218 
219 
220 
221     if (ps_seq->u1_mb_aff_flag == 0)
222     {
223         ps_dec->u4_use_intrapred_line_copy = 1;
224     }
225 
226     ps_dec->u4_app_disable_deblk_frm = 0;
227     /* If degrade is enabled, set the degrade flags appropriately */
228     if(ps_dec->i4_degrade_type && ps_dec->i4_degrade_pics)
229     {
230         WORD32 degrade_pic;
231         ps_dec->i4_degrade_pic_cnt++;
232         degrade_pic = 0;
233 
234         /* If degrade is to be done in all frames, then do not check further */
235         switch(ps_dec->i4_degrade_pics)
236         {
237             case 4:
238             {
239                 degrade_pic = 1;
240                 break;
241             }
242             case 3:
243             {
244                 if(ps_cur_slice->u1_slice_type != I_SLICE)
245                     degrade_pic = 1;
246 
247                 break;
248             }
249             case 2:
250             {
251 
252                 /* If pic count hits non-degrade interval or it is an islice, then do not degrade */
253                 if((ps_cur_slice->u1_slice_type != I_SLICE)
254                                 && (ps_dec->i4_degrade_pic_cnt
255                                                 != ps_dec->i4_nondegrade_interval))
256                     degrade_pic = 1;
257 
258                 break;
259             }
260             case 1:
261             {
262                 /* Check if the current picture is non-ref */
263                 if(0 == ps_cur_slice->u1_nal_ref_idc)
264                 {
265                     degrade_pic = 1;
266                 }
267                 break;
268             }
269 
270         }
271         if(degrade_pic)
272         {
273             if(ps_dec->i4_degrade_type & 0x2)
274                 ps_dec->u4_app_disable_deblk_frm = 1;
275 
276             /* MC degrading is done only for non-ref pictures */
277             if(0 == ps_cur_slice->u1_nal_ref_idc)
278             {
279                 if(ps_dec->i4_degrade_type & 0x4)
280                     ps_dec->i4_mv_frac_mask = 0;
281 
282                 if(ps_dec->i4_degrade_type & 0x8)
283                     ps_dec->i4_mv_frac_mask = 0;
284             }
285         }
286         else
287             ps_dec->i4_degrade_pic_cnt = 0;
288     }
289 
290     {
291         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
292         if((ps_cur_slice->u1_slice_type == I_SLICE)
293                         || (ps_cur_slice->u1_slice_type == SI_SLICE))
294             ps_err->u1_cur_pic_type = PIC_TYPE_I;
295         else
296             ps_err->u1_cur_pic_type = PIC_TYPE_UNKNOWN;
297 
298         if(ps_err->u1_pic_aud_i == PIC_TYPE_I)
299         {
300             ps_err->u1_cur_pic_type = PIC_TYPE_I;
301             ps_err->u1_pic_aud_i = PIC_TYPE_UNKNOWN;
302         }
303 
304         if(ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL)
305         {
306             if(ps_err->u1_err_flag)
307                 ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
308             ps_err->u1_err_flag = ACCEPT_ALL_PICS;
309         }
310     }
311 
312     if(ps_dec->u1_init_dec_flag && ps_dec->s_prev_seq_params.u1_eoseq_pending)
313     {
314         /* Reset the decoder picture buffers */
315         WORD32 j;
316         for(j = 0; j < MAX_DISP_BUFS_NEW; j++)
317         {
318 
319             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
320                                   j,
321                                   BUF_MGR_REF);
322             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
323                                   ps_dec->as_buf_id_info_map[j].mv_buf_id,
324                                   BUF_MGR_REF);
325             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
326                                   j,
327                                   BUF_MGR_IO);
328         }
329 
330         /* reset the decoder structure parameters related to buffer handling */
331         ps_dec->u1_second_field = 0;
332         ps_dec->i4_cur_display_seq = 0;
333 
334         /********************************************************************/
335         /* indicate in the decoder output i4_status that some frames are being */
336         /* dropped, so that it resets timestamp and wait for a new sequence */
337         /********************************************************************/
338 
339         ps_dec->s_prev_seq_params.u1_eoseq_pending = 0;
340     }
341     ret = ih264d_init_pic(ps_dec, u2_frame_num, i4_poc, ps_pps);
342     if(ret != OK)
343         return ret;
344 
345     ps_dec->pv_parse_tu_coeff_data = ps_dec->pv_pic_tu_coeff_data;
346     ps_dec->pv_proc_tu_coeff_data  = ps_dec->pv_pic_tu_coeff_data;
347     ps_dec->ps_nmb_info = ps_dec->ps_frm_mb_info;
348     if(ps_dec->u1_separate_parse)
349     {
350         UWORD16 pic_wd;
351         UWORD16 pic_ht;
352         UWORD32 num_mbs;
353 
354         pic_wd = ps_dec->u2_pic_wd;
355         pic_ht = ps_dec->u2_pic_ht;
356         num_mbs = (pic_wd * pic_ht) >> 8;
357 
358         if(ps_dec->pu1_dec_mb_map)
359         {
360             memset((void *)ps_dec->pu1_dec_mb_map, 0, num_mbs);
361         }
362 
363         if(ps_dec->pu1_recon_mb_map)
364         {
365 
366             memset((void *)ps_dec->pu1_recon_mb_map, 0, num_mbs);
367         }
368 
369         if(ps_dec->pu2_slice_num_map)
370         {
371             memset((void *)ps_dec->pu2_slice_num_map, 0,
372                    (num_mbs * sizeof(UWORD16)));
373         }
374 
375     }
376 
377     ps_dec->ps_parse_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
378     ps_dec->ps_decode_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
379     ps_dec->ps_computebs_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
380     ps_dec->u2_cur_slice_num = 0;
381 
382     /* Initialize all the HP toolsets to zero */
383     ps_dec->s_high_profile.u1_scaling_present = 0;
384     ps_dec->s_high_profile.u1_transform8x8_present = 0;
385 
386     /* Get Next Free Picture */
387     if(1 == ps_dec->u4_share_disp_buf)
388     {
389         UWORD32 i;
390         /* Free any buffer that is in the queue to be freed */
391         for(i = 0; i < MAX_DISP_BUFS_NEW; i++)
392         {
393             if(0 == ps_dec->u4_disp_buf_to_be_freed[i])
394                 continue;
395             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr, i,
396             BUF_MGR_IO);
397             ps_dec->u4_disp_buf_to_be_freed[i] = 0;
398             ps_dec->u4_disp_buf_mapping[i] = 0;
399 
400         }
401     }
402     if(!(u1_field_pic_flag && 0 != ps_dec->u1_top_bottom_decoded)) //ps_dec->u1_second_field))
403     {
404         pic_buffer_t *ps_cur_pic;
405         WORD32 cur_pic_buf_id, cur_mv_buf_id;
406         col_mv_buf_t *ps_col_mv;
407         while(1)
408         {
409             ps_cur_pic = (pic_buffer_t *)ih264_buf_mgr_get_next_free(
410                             (buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
411                             &cur_pic_buf_id);
412 
413             /* In case of IDR slices, if there is no free picture buffer, then release
414              * all buffers from display and reference
415              */
416             if((ps_cur_pic == NULL) && (ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL))
417             {
418                 WORD32 j;
419 
420                 for(j = 0; j < MAX_DISP_BUFS_NEW; j++)
421                 {
422                     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
423                                           j,
424                                           BUF_MGR_REF);
425                     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
426                                           ps_dec->as_buf_id_info_map[j].mv_buf_id,
427                                           BUF_MGR_REF);
428 
429                     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
430                                           j,
431                                           BUF_MGR_IO);
432                 }
433                 ps_cur_pic = (pic_buffer_t *)ih264_buf_mgr_get_next_free(
434                                 (buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
435                                 &cur_pic_buf_id);
436             }
437 
438             if(ps_cur_pic == NULL)
439             {
440                 ps_dec->i4_error_code = ERROR_UNAVAIL_PICBUF_T;
441                 ps_dec->ps_dec_err_status->u1_err_flag |= REJECT_CUR_PIC;
442                 return ERROR_UNAVAIL_PICBUF_T;
443             }
444             if(0 == ps_dec->u4_disp_buf_mapping[cur_pic_buf_id])
445             {
446                 break;
447             }
448 
449         }
450         ps_col_mv = (col_mv_buf_t *)ih264_buf_mgr_get_next_free((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
451                                                                &cur_mv_buf_id);
452         if(ps_col_mv == NULL)
453         {
454             ps_dec->i4_error_code = ERROR_UNAVAIL_MVBUF_T;
455             ps_dec->ps_dec_err_status->u1_err_flag |= REJECT_CUR_PIC;
456             return ERROR_UNAVAIL_MVBUF_T;
457         }
458 
459         ps_dec->ps_cur_pic = ps_cur_pic;
460         ps_dec->u1_pic_buf_id = cur_pic_buf_id;
461         ps_cur_pic->u4_ts = ps_dec->u4_ts;
462 
463         /* Update POC and inter_pic_id in sei structure,
464          * later can be used by application for grain synthesis(SMPTE-RDD5) */
465         if(ps_dec->ps_sei->u1_sei_fgc_params_present_flag)
466         {
467             ps_dec->ps_sei->s_sei_fgc_params.i4_poc = i4_poc;
468             ps_dec->ps_sei->s_sei_fgc_params.u4_idr_pic_id = ps_cur_slice->u4_idr_pic_id;
469         }
470         memcpy(&ps_cur_pic->s_sei_pic, ps_dec->ps_sei, sizeof(sei));
471 
472         ps_cur_pic->u1_mv_buf_id = cur_mv_buf_id;
473         ps_dec->as_buf_id_info_map[cur_pic_buf_id].mv_buf_id = cur_mv_buf_id;
474         if(ps_dec->u1_enable_mb_info)
475         {
476             UWORD32 mb_info_map_size = ps_dec->u4_total_mbs << 2;
477             ps_dec->as_buf_id_info_map[cur_pic_buf_id].pu1_qp_map
478                 = ps_dec->pu1_qp_map_base + cur_pic_buf_id * mb_info_map_size;
479             ps_dec->as_buf_id_info_map[cur_pic_buf_id].pu1_mb_type_map
480                 = ps_dec->pu1_mb_type_map_base + cur_pic_buf_id * mb_info_map_size;
481             memset(ps_dec->as_buf_id_info_map[cur_pic_buf_id].pu1_qp_map, 0, mb_info_map_size);
482             memset(ps_dec->as_buf_id_info_map[cur_pic_buf_id].pu1_mb_type_map, 0, mb_info_map_size);
483         }
484 
485         ps_cur_pic->pu1_col_zero_flag = (UWORD8 *)ps_col_mv->pv_col_zero_flag;
486         ps_cur_pic->ps_mv = (mv_pred_t *)ps_col_mv->pv_mv;
487         ps_dec->au1_pic_buf_ref_flag[cur_pic_buf_id] = 0;
488 
489         {
490             /*make first entry of list0 and list1 point to cur pic,
491              *so that if first slice is in error, ref pic struct will have valid entries*/
492             ps_dec->ps_ref_pic_buf_lx[0] = ps_dec->ps_dpb_mgr->ps_init_dpb[0];
493             ps_dec->ps_ref_pic_buf_lx[1] = ps_dec->ps_dpb_mgr->ps_init_dpb[1];
494             *(ps_dec->ps_dpb_mgr->ps_init_dpb[0][0]) = *ps_cur_pic;
495             /* Initialize for field reference as well */
496             *(ps_dec->ps_dpb_mgr->ps_init_dpb[0][MAX_REF_BUFS]) = *ps_cur_pic;
497 
498             *(ps_dec->ps_dpb_mgr->ps_mod_dpb[0][0]) = *ps_cur_pic;
499             /* Initialize for field reference as well */
500             *(ps_dec->ps_dpb_mgr->ps_mod_dpb[0][MAX_REF_BUFS]) = *ps_cur_pic;
501             *(ps_dec->ps_dpb_mgr->ps_init_dpb[1][0]) = *ps_cur_pic;
502             /* Initialize for field reference as well */
503             *(ps_dec->ps_dpb_mgr->ps_init_dpb[1][MAX_REF_BUFS]) = *ps_cur_pic;
504             *(ps_dec->ps_dpb_mgr->ps_mod_dpb[1][0]) = *ps_cur_pic;
505             /* Initialize for field reference as well */
506             *(ps_dec->ps_dpb_mgr->ps_mod_dpb[1][MAX_REF_BUFS]) = *ps_cur_pic;
507         }
508 
509         ps_dec->ps_cur_pic->u1_picturetype = u1_field_pic_flag;
510         ps_dec->ps_cur_pic->u4_pack_slc_typ = SKIP_NONE;
511         H264_DEC_DEBUG_PRINT("got a buffer\n");
512     }
513     else
514     {
515         H264_DEC_DEBUG_PRINT("did not get a buffer\n");
516     }
517 
518     ps_dec->u4_pic_buf_got = 1;
519 
520     ps_dec->ps_cur_pic->i4_poc = i4_poc;
521     ps_dec->ps_cur_pic->i4_frame_num = u2_frame_num;
522     ps_dec->ps_cur_pic->i4_pic_num = u2_frame_num;
523     ps_dec->ps_cur_pic->i4_top_field_order_cnt = ps_pps->i4_top_field_order_cnt;
524     ps_dec->ps_cur_pic->i4_bottom_field_order_cnt =
525                     ps_pps->i4_bottom_field_order_cnt;
526     ps_dec->ps_cur_pic->i4_avg_poc = ps_pps->i4_avg_poc;
527     ps_dec->ps_cur_pic->u4_time_stamp = ps_dec->u4_pts;
528 
529     ps_dec->s_cur_pic = *(ps_dec->ps_cur_pic);
530     if(u1_field_pic_flag && u1_bottom_field_flag)
531     {
532         WORD32 i4_temp_poc;
533         WORD32 i4_top_field_order_poc, i4_bot_field_order_poc;
534         /* Point to odd lines, since it's bottom field */
535         ps_dec->s_cur_pic.pu1_buf1 += ps_dec->s_cur_pic.u2_frm_wd_y;
536         ps_dec->s_cur_pic.pu1_buf2 += ps_dec->s_cur_pic.u2_frm_wd_uv;
537         ps_dec->s_cur_pic.pu1_buf3 += ps_dec->s_cur_pic.u2_frm_wd_uv;
538         ps_dec->s_cur_pic.ps_mv +=
539                         ((ps_dec->u2_pic_ht * ps_dec->u2_pic_wd) >> 5);
540         ps_dec->s_cur_pic.pu1_col_zero_flag += ((ps_dec->u2_pic_ht
541                         * ps_dec->u2_pic_wd) >> 5);
542         ps_dec->ps_cur_pic->u1_picturetype |= BOT_FLD;
543         i4_top_field_order_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
544         i4_bot_field_order_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
545         i4_temp_poc = MIN(i4_top_field_order_poc,
546                                  i4_bot_field_order_poc);
547         ps_dec->ps_cur_pic->i4_avg_poc = i4_temp_poc;
548     }
549 
550     ps_cur_slice->u1_mbaff_frame_flag = ps_seq->u1_mb_aff_flag
551                     && (!u1_field_pic_flag);
552 
553     ps_dec->ps_cur_pic->u1_picturetype |= (ps_cur_slice->u1_mbaff_frame_flag
554                     << 2);
555 
556     ps_dec->ps_cur_mb_row = ps_dec->ps_nbr_mb_row; //[0];
557     //Increment by 2 ,so that left mb (mbaff decrements by 2)  will always be valid
558     ps_dec->ps_cur_mb_row += 2;
559     ps_dec->ps_top_mb_row = ps_dec->ps_nbr_mb_row;
560     ps_dec->ps_top_mb_row += ((ps_dec->u2_frm_wd_in_mbs + 2) << (1 - ps_dec->ps_cur_sps->u1_frame_mbs_only_flag));
561     //Increment by 2 ,so that left mb (mbaff decrements by 2)  will always be valid
562     ps_dec->ps_top_mb_row += 2;
563 
564     /* CHANGED CODE */
565     ps_dec->ps_mv_cur = ps_dec->s_cur_pic.ps_mv;
566     ps_dec->ps_mv_top = ps_dec->ps_mv_top_p[0];
567     /* CHANGED CODE */
568     ps_dec->u1_mv_top_p = 0;
569     ps_dec->u1_mb_idx = 0;
570     /* CHANGED CODE */
571     ps_dec->ps_mv_left = ps_dec->s_cur_pic.ps_mv;
572     ps_dec->u2_total_mbs_coded = 0;
573     ps_dec->i4_submb_ofst = -(SUB_BLK_SIZE);
574     ps_dec->u4_pred_info_idx = 0;
575     ps_dec->u4_pred_info_pkd_idx = 0;
576     ps_dec->u4_dma_buf_idx = 0;
577     ps_dec->ps_mv = ps_dec->s_cur_pic.ps_mv;
578     ps_dec->ps_mv_bank_cur = ps_dec->s_cur_pic.ps_mv;
579     ps_dec->pu1_col_zero_flag = ps_dec->s_cur_pic.pu1_col_zero_flag;
580     ps_dec->ps_part = ps_dec->ps_parse_part_params;
581     ps_dec->i2_prev_slice_mbx = -1;
582     ps_dec->i2_prev_slice_mby = 0;
583     ps_dec->u2_mv_2mb[0] = 0;
584     ps_dec->u2_mv_2mb[1] = 0;
585     ps_dec->u1_last_pic_not_decoded = 0;
586 
587     ps_dec->u2_cur_slice_num_dec_thread = 0;
588     ps_dec->u2_cur_slice_num_bs = 0;
589     ps_dec->u4_intra_pred_line_ofst = 0;
590     ps_dec->pu1_cur_y_intra_pred_line = ps_dec->pu1_y_intra_pred_line;
591     ps_dec->pu1_cur_u_intra_pred_line = ps_dec->pu1_u_intra_pred_line;
592     ps_dec->pu1_cur_v_intra_pred_line = ps_dec->pu1_v_intra_pred_line;
593 
594     ps_dec->pu1_cur_y_intra_pred_line_base = ps_dec->pu1_y_intra_pred_line;
595     ps_dec->pu1_cur_u_intra_pred_line_base = ps_dec->pu1_u_intra_pred_line;
596     ps_dec->pu1_cur_v_intra_pred_line_base = ps_dec->pu1_v_intra_pred_line;
597 
598 
599 
600 
601 
602     ps_dec->pu1_prev_y_intra_pred_line = ps_dec->pu1_y_intra_pred_line
603                     + (ps_dec->u2_frm_wd_in_mbs * MB_SIZE);
604 
605     ps_dec->pu1_prev_u_intra_pred_line = ps_dec->pu1_u_intra_pred_line
606                     + ps_dec->u2_frm_wd_in_mbs * BLK8x8SIZE * YUV420SP_FACTOR;
607     ps_dec->pu1_prev_v_intra_pred_line = ps_dec->pu1_v_intra_pred_line
608                     + ps_dec->u2_frm_wd_in_mbs * BLK8x8SIZE;
609 
610     ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic;
611     /* Initialize The Function Pointer Depending Upon the Entropy and MbAff Flag */
612     {
613         if(ps_cur_slice->u1_mbaff_frame_flag)
614         {
615             ps_dec->pf_compute_bs = ih264d_compute_bs_mbaff;
616             ps_dec->pf_mvpred = ih264d_mvpred_mbaff;
617         }
618         else
619         {
620             ps_dec->pf_compute_bs = ih264d_compute_bs_non_mbaff;
621             ps_dec->u1_cur_mb_fld_dec_flag = ps_cur_slice->u1_field_pic_flag;
622         }
623     }
624     /* Set up the Parameter for DMA transfer */
625     {
626         UWORD8 u1_field_pic_flag = ps_dec->ps_cur_slice->u1_field_pic_flag;
627 
628         UWORD8 u1_mbaff = ps_cur_slice->u1_mbaff_frame_flag;
629 
630         UWORD8 uc_lastmbs = (((ps_dec->u2_pic_wd) >> 4)
631                         % (ps_dec->u1_recon_mb_grp >> u1_mbaff));
632         UWORD16 ui16_lastmbs_widthY =
633                         (uc_lastmbs ? (uc_lastmbs << 4) : ((ps_dec->u1_recon_mb_grp
634                                         >> u1_mbaff) << 4));
635         UWORD16 ui16_lastmbs_widthUV =
636                         uc_lastmbs ? (uc_lastmbs << 3) : ((ps_dec->u1_recon_mb_grp
637                                         >> u1_mbaff) << 3);
638 
639         ps_dec->s_tran_addrecon.pu1_dest_y = ps_dec->s_cur_pic.pu1_buf1;
640         ps_dec->s_tran_addrecon.pu1_dest_u = ps_dec->s_cur_pic.pu1_buf2;
641         ps_dec->s_tran_addrecon.pu1_dest_v = ps_dec->s_cur_pic.pu1_buf3;
642 
643         ps_dec->s_tran_addrecon.u2_frm_wd_y = ps_dec->u2_frm_wd_y
644                         << u1_field_pic_flag;
645         ps_dec->s_tran_addrecon.u2_frm_wd_uv = ps_dec->u2_frm_wd_uv
646                         << u1_field_pic_flag;
647 
648         if(u1_field_pic_flag)
649         {
650             ui16_lastmbs_widthY += ps_dec->u2_frm_wd_y;
651             ui16_lastmbs_widthUV += ps_dec->u2_frm_wd_uv;
652         }
653 
654         /* Normal Increment of Pointer */
655         ps_dec->s_tran_addrecon.u4_inc_y[0] = ((ps_dec->u1_recon_mb_grp << 4)
656                         >> u1_mbaff);
657         ps_dec->s_tran_addrecon.u4_inc_uv[0] = ((ps_dec->u1_recon_mb_grp << 4)
658                         >> u1_mbaff);
659 
660         /* End of Row Increment */
661         ps_dec->s_tran_addrecon.u4_inc_y[1] = (ui16_lastmbs_widthY
662                         + (PAD_LEN_Y_H << 1)
663                         + ps_dec->s_tran_addrecon.u2_frm_wd_y
664                                         * ((15 << u1_mbaff) + u1_mbaff));
665         ps_dec->s_tran_addrecon.u4_inc_uv[1] = (ui16_lastmbs_widthUV
666                         + (PAD_LEN_UV_H << 2)
667                         + ps_dec->s_tran_addrecon.u2_frm_wd_uv
668                                         * ((15 << u1_mbaff) + u1_mbaff));
669 
670         /* Assign picture numbers to each frame/field  */
671         /* only once per picture.                      */
672         ih264d_assign_pic_num(ps_dec);
673         ps_dec->s_tran_addrecon.u2_mv_top_left_inc = (ps_dec->u1_recon_mb_grp
674                         << 2) - 1 - (u1_mbaff << 2);
675         ps_dec->s_tran_addrecon.u2_mv_left_inc = ((ps_dec->u1_recon_mb_grp
676                         >> u1_mbaff) - 1) << (4 + u1_mbaff);
677     }
678     /**********************************************************************/
679     /* High profile related initialization at pictrue level               */
680     /**********************************************************************/
681     if(ps_seq->u1_profile_idc == HIGH_PROFILE_IDC)
682     {
683         if((ps_seq->i4_seq_scaling_matrix_present_flag)
684                         || (ps_pps->i4_pic_scaling_matrix_present_flag))
685         {
686             ret = ih264d_form_scaling_matrix_picture(ps_seq, ps_pps, ps_dec);
687             ps_dec->s_high_profile.u1_scaling_present = 1;
688         }
689         else
690         {
691             ret = ih264d_form_default_scaling_matrix(ps_dec);
692         }
693 
694         if(ps_pps->i4_transform_8x8_mode_flag)
695         {
696             ps_dec->s_high_profile.u1_transform8x8_present = 1;
697         }
698     }
699     else
700     {
701         ret = ih264d_form_default_scaling_matrix(ps_dec);
702     }
703 
704     if(ret != OK)
705         return ret;
706 
707     /* required while reading the transform_size_8x8 u4_flag */
708     ps_dec->s_high_profile.u1_direct_8x8_inference_flag =
709                     ps_seq->u1_direct_8x8_inference_flag;
710     ps_dec->s_high_profile.s_cavlc_ctxt = ps_dec->s_cavlc_ctxt;
711 
712     ps_dec->i1_recon_in_thread3_flag = 1;
713     ps_dec->ps_frame_buf_ip_recon = &ps_dec->s_tran_addrecon;
714     if(ps_dec->u1_separate_parse)
715     {
716         memcpy(&ps_dec->s_tran_addrecon_parse, &ps_dec->s_tran_addrecon,
717                sizeof(tfr_ctxt_t));
718         if(ps_dec->u4_num_cores >= 3 && ps_dec->i1_recon_in_thread3_flag)
719         {
720             memcpy(&ps_dec->s_tran_iprecon, &ps_dec->s_tran_addrecon,
721                    sizeof(tfr_ctxt_t));
722             ps_dec->ps_frame_buf_ip_recon = &ps_dec->s_tran_iprecon;
723         }
724     }
725 
726 
727     ih264d_init_deblk_tfr_ctxt(ps_dec,&(ps_dec->s_pad_mgr), &(ps_dec->s_tran_addrecon),
728                                ps_dec->u2_frm_wd_in_mbs, 0);
729 
730     ps_dec->ps_cur_deblk_mb = ps_dec->ps_deblk_pic;
731     ps_dec->u4_cur_deblk_mb_num = 0;
732 
733     ps_dec->u4_deblk_mb_x = 0;
734     ps_dec->u4_deblk_mb_y = 0;
735     ps_dec->pu4_wt_ofsts = ps_dec->pu4_wts_ofsts_mat;
736 
737     ps_dec->u4_first_slice_in_pic = 0;
738     H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
739     return OK;
740 }
741 
742 /*!
743  **************************************************************************
744  * \if Function name :  ih264d_deblock_display \endif
745  *
746  * \brief            :  The function callls the deblocking routine and manages
747  :  the Recon buffers and displays .
748  * \return           :  Nothing
749  *
750  **************************************************************************
751  */
ih264d_end_of_pic_dispbuf_mgr(dec_struct_t * ps_dec)752 WORD32 ih264d_end_of_pic_dispbuf_mgr(dec_struct_t * ps_dec)
753 {
754     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
755     UWORD8 u1_num_of_users = 0;
756     WORD32 ret;
757 
758     H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
759     if(1)
760     {
761 
762         {
763             ih264d_delete_nonref_nondisplay_pics(ps_dec->ps_dpb_mgr);
764             if(ps_cur_slice->u1_mmco_equalto5
765                             || (ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL))
766             {
767                 ps_dec->ps_cur_pic->i4_poc = 0;
768                 if(ps_dec->u2_total_mbs_coded
769                                 == (ps_dec->ps_cur_sps->u2_max_mb_addr + 1))
770                     ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
771                 ih264d_release_display_bufs(ps_dec);
772             }
773             if(IVD_DECODE_FRAME_OUT != ps_dec->e_frm_out_mode)
774             {
775                 ret = ih264d_assign_display_seq(ps_dec);
776                 if(ret != OK)
777                     return ret;
778             }
779         }
780 
781         if(ps_cur_slice->u1_nal_ref_idc)
782         {
783             /* Mark pic buf as needed for reference */
784             ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
785                                      ps_dec->u1_pic_buf_id,
786                                      BUF_MGR_REF);
787             /* Mark mv buf as needed for reference */
788             ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
789                                      ps_dec->as_buf_id_info_map[ps_dec->u1_pic_buf_id].mv_buf_id,
790                                      BUF_MGR_REF);
791             ps_dec->au1_pic_buf_ref_flag[ps_dec->u1_pic_buf_id] = 1;
792         }
793 
794         /* 420 consumer */
795         /* Increment the number of users by 1 for display based upon */
796         /*the SEEK KEY FRAME control sent to decoder                 */
797         if(((0 == ps_dec->u1_last_pic_not_decoded)
798                         && (0
799                                         == (ps_dec->ps_cur_pic->u4_pack_slc_typ
800                                                         & ps_dec->u4_skip_frm_mask)))
801                         || (ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL))
802         {
803             /* Mark pic buf as needed for display */
804             ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
805                                      ps_dec->u1_pic_buf_id,
806                                      BUF_MGR_IO);
807 
808         }
809 
810         if(!ps_cur_slice->u1_field_pic_flag
811                         || ((TOP_FIELD_ONLY | BOT_FIELD_ONLY)
812                                         != ps_dec->u1_top_bottom_decoded))
813         {
814             pic_buffer_t *ps_cur_pic = ps_dec->ps_cur_pic;
815             ps_cur_pic->u2_disp_width = ps_dec->u2_disp_width;
816             ps_cur_pic->u2_disp_height = ps_dec->u2_disp_height >> 1;
817 
818             ps_cur_pic->u2_crop_offset_y = ps_dec->u2_crop_offset_y;
819             ps_cur_pic->u2_crop_offset_uv = ps_dec->u2_crop_offset_uv;
820             ps_cur_pic->u1_pic_type = 0;
821             {
822                 WORD64 i8_display_poc;
823                 i8_display_poc = (WORD64)ps_dec->i4_prev_max_display_seq +
824                             ps_dec->ps_cur_pic->i4_poc;
825                 if(IS_OUT_OF_RANGE_S32(i8_display_poc))
826                 {
827                     ps_dec->i4_prev_max_display_seq = 0;
828                 }
829             }
830             ret = ih264d_insert_pic_in_display_list(
831                             ps_dec->ps_dpb_mgr,
832                             ps_dec->u1_pic_buf_id,
833                             ps_dec->i4_prev_max_display_seq
834                                             + ps_dec->ps_cur_pic->i4_poc,
835                             ps_dec->ps_cur_pic->i4_frame_num);
836             if(ret != OK)
837                 return ret;
838 
839             {
840                 ivd_video_decode_op_t * ps_dec_output =
841                                 (ivd_video_decode_op_t *)ps_dec->pv_dec_out;
842 
843                 ps_dec_output->u4_frame_decoded_flag = 1;
844             }
845             if(ps_dec->au1_pic_buf_ref_flag[ps_dec->u1_pic_buf_id] == 0)
846             {
847                 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
848                                       ps_dec->as_buf_id_info_map[ps_dec->u1_pic_buf_id].mv_buf_id,
849                                       BUF_MGR_REF);
850                 ps_dec->au1_pic_buf_ref_flag[ps_dec->u1_pic_buf_id] = 0;
851 
852             }
853         }
854         else
855         {
856             H264_DEC_DEBUG_PRINT("pic not inserted display %d %d\n",
857                                  ps_cur_slice->u1_field_pic_flag,
858                                  ps_dec->u1_second_field);
859         }
860 
861         if(!ps_cur_slice->u1_field_pic_flag
862                         || ((TOP_FIELD_ONLY | BOT_FIELD_ONLY)
863                                         == ps_dec->u1_top_bottom_decoded))
864         {
865             if(IVD_DECODE_FRAME_OUT == ps_dec->e_frm_out_mode)
866             {
867                 ret = ih264d_assign_display_seq(ps_dec);
868                 if(ret != OK)
869                     return ret;
870             }
871         }
872     }
873 
874     H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
875 
876     return OK;
877 }
878 
ih264d_err_pic_dispbuf_mgr(dec_struct_t * ps_dec)879 void ih264d_err_pic_dispbuf_mgr(dec_struct_t *ps_dec)
880 {
881     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
882     ivd_video_decode_op_t * ps_dec_output =
883                     (ivd_video_decode_op_t *)ps_dec->pv_dec_out;
884 
885     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
886                           ps_dec->u1_pic_buf_id,
887                           BUF_MGR_REF);
888     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
889                           ps_dec->as_buf_id_info_map[ps_dec->u1_pic_buf_id].mv_buf_id,
890                           BUF_MGR_REF);
891     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
892                           ps_dec->u1_pic_buf_id,
893                           BUF_MGR_IO);
894 }
895 
ih264d_deblock_picture(void * ptr)896 void ih264d_deblock_picture(void *ptr)
897 {
898     dec_struct_t *ps_dec = (dec_struct_t *)ptr;
899 
900     {
901         /*Deblock picture only if all the mb's in the frame have been decoded*/
902         if(ps_dec->u1_pic_decode_done == 1)
903         {
904             if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag
905                             || ps_dec->ps_cur_slice->u1_field_pic_flag)
906             {
907                 ps_dec->p_DeblockPicture[ps_dec->ps_cur_slice->u1_mbaff_frame_flag](
908                                 ps_dec);
909             }
910             else
911 
912             {
913 
914                 ih264d_deblock_picture_progressive(ps_dec);
915             }
916 
917         }
918     }
919 
920 }
921 
922 /*!
923  **************************************************************************
924  * \if Function name :  ih264d_deblock_display \endif
925  *
926  * \brief            :  The function callls the deblocking routine and manages
927  :  the Recon buffers and displays .
928  * \return           :  Nothing
929  *
930  **************************************************************************
931  */
ih264d_deblock_display(dec_struct_t * ps_dec)932 WORD32 ih264d_deblock_display(dec_struct_t *ps_dec)
933 {
934     WORD32 ret;
935     /* Call deblocking */
936     ih264d_deblock_picture(ps_dec);
937 
938     ret = ih264d_end_of_pic_dispbuf_mgr(ps_dec);
939     if(ret != OK)
940         return ret;
941 
942     return OK;
943 }
944 
945 /*
946  *!
947  **************************************************************************
948  * \if Function name : EndofPoc \endif
949  *
950  * \brief
951  *    EndofPoc Processing
952  *
953  * \return
954  *    0 on Success and Error code otherwise
955  **************************************************************************
956  */
957 
ih264d_end_of_pic(dec_struct_t * ps_dec)958 WORD32 ih264d_end_of_pic(dec_struct_t *ps_dec)
959 {
960     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
961     WORD32 ret;
962 
963     {
964         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
965         if(ps_err->u1_err_flag & REJECT_CUR_PIC)
966         {
967             ih264d_err_pic_dispbuf_mgr(ps_dec);
968             return ERROR_NEW_FRAME_EXPECTED;
969         }
970     }
971 
972     H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
973     ret = ih264d_end_of_pic_processing(ps_dec);
974     if(ret != OK)
975         return ret;
976     /*--------------------------------------------------------------------*/
977     /* ih264d_decode_pic_order_cnt - calculate the Pic Order Cnt                    */
978     /* Needed to detect end of picture                                    */
979     /*--------------------------------------------------------------------*/
980 
981     H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
982 
983     return OK;
984 }
985 
986 
987 /*!
988  **************************************************************************
989  * \if Function name : ih264d_fix_error_in_dpb \endif
990  *
991  * \brief
992  *    fix error in DPB
993  *
994  * \return
995  *    Number of node(s) deleted
996  **************************************************************************
997  */
998 
ih264d_fix_error_in_dpb(dec_struct_t * ps_dec)999 WORD32 ih264d_fix_error_in_dpb(dec_struct_t *ps_dec)
1000 {
1001     /*--------------------------------------------------------------------*/
1002     /* If there is common node in lt_list and st_list then delete it from */
1003     /* st_list                                                            */
1004     /*--------------------------------------------------------------------*/
1005     UWORD8 no_of_nodes_deleted = 0;
1006     UWORD8 lt_ref_num = ps_dec->ps_dpb_mgr->u1_num_lt_ref_bufs;
1007     struct dpb_info_t *ps_lt_curr_dpb = ps_dec->ps_dpb_mgr->ps_dpb_ht_head;
1008     while(lt_ref_num && ps_lt_curr_dpb)
1009     {
1010         if(ps_dec->ps_dpb_mgr->ps_dpb_st_head
1011                 && ((ps_lt_curr_dpb->s_bot_field.u1_reference_info
1012                         | ps_lt_curr_dpb->s_top_field.u1_reference_info)
1013                         == (IS_SHORT_TERM | IS_LONG_TERM)))
1014         {
1015             struct dpb_info_t *ps_st_next_dpb = ps_dec->ps_dpb_mgr->ps_dpb_st_head;
1016             struct dpb_info_t *ps_st_curr_dpb = ps_dec->ps_dpb_mgr->ps_dpb_st_head;
1017             UWORD8 st_ref_num = ps_dec->ps_dpb_mgr->u1_num_st_ref_bufs;
1018             while(st_ref_num && ps_st_curr_dpb)
1019             {
1020                 if(ps_st_curr_dpb == ps_lt_curr_dpb)
1021                 {
1022                     if(st_ref_num == ps_dec->ps_dpb_mgr->u1_num_st_ref_bufs)
1023                     {
1024                         ps_dec->ps_dpb_mgr->ps_dpb_st_head =
1025                                 ps_dec->ps_dpb_mgr->ps_dpb_st_head->ps_prev_short;
1026                         ps_st_curr_dpb = ps_dec->ps_dpb_mgr->ps_dpb_st_head;
1027                     }
1028                     else
1029                     {
1030                         ps_st_next_dpb->ps_prev_short = ps_st_curr_dpb->ps_prev_short;
1031                     }
1032                     ps_dec->ps_dpb_mgr->u1_num_st_ref_bufs--;
1033                     no_of_nodes_deleted++;
1034                     break;
1035                 }
1036                 ps_st_next_dpb = ps_st_curr_dpb;
1037                 ps_st_curr_dpb = ps_st_curr_dpb->ps_prev_short;
1038                 st_ref_num--;
1039             }
1040         }
1041         ps_lt_curr_dpb = ps_lt_curr_dpb->ps_prev_long;
1042         lt_ref_num--;
1043     }
1044     return no_of_nodes_deleted;
1045 }
1046 
1047 
1048 /*!
1049  **************************************************************************
1050  * \if Function name : DecodeSlice \endif
1051  *
1052  * \brief
1053  *    Parses a slice
1054  *
1055  * \return
1056  *    0 on Success and Error code otherwise
1057  **************************************************************************
1058  */
1059 
ih264d_parse_decode_slice(UWORD8 u1_is_idr_slice,UWORD8 u1_nal_ref_idc,dec_struct_t * ps_dec)1060 WORD32 ih264d_parse_decode_slice(UWORD8 u1_is_idr_slice,
1061                                  UWORD8 u1_nal_ref_idc,
1062                                  dec_struct_t *ps_dec /* Decoder parameters */
1063                                  )
1064 {
1065     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
1066     dec_pic_params_t *ps_pps;
1067     dec_seq_params_t *ps_seq;
1068     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
1069     pocstruct_t s_tmp_poc = {0};
1070     WORD32 i_delta_poc[2];
1071     WORD32 i4_poc = 0;
1072     UWORD16 u2_first_mb_in_slice, u2_frame_num;
1073     UWORD8 u1_field_pic_flag, u1_redundant_pic_cnt = 0, u1_slice_type;
1074     UWORD32 u4_idr_pic_id = 0;
1075     UWORD8 u1_bottom_field_flag, u1_pic_order_cnt_type;
1076 
1077     UWORD8 u1_nal_unit_type;
1078     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
1079     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
1080     WORD8 i1_is_end_of_poc;
1081 
1082     WORD32 ret, end_of_frame;
1083     WORD32 prev_slice_err, num_mb_skipped;
1084     UWORD8 u1_mbaff;
1085     pocstruct_t *ps_cur_poc;
1086 
1087     UWORD32 u4_temp;
1088     WORD32 i_temp;
1089     UWORD32 u4_call_end_of_pic = 0;
1090 
1091     /* read FirstMbInSlice  and slice type*/
1092     ps_dec->ps_dpb_cmds->u1_dpb_commands_read_slc = 0;
1093     u2_first_mb_in_slice = ih264d_uev(pu4_bitstrm_ofst,
1094                                      pu4_bitstrm_buf);
1095     if(u2_first_mb_in_slice
1096                     >= (ps_dec->u2_frm_ht_in_mbs * ps_dec->u2_frm_wd_in_mbs))
1097     {
1098 
1099         return ERROR_CORRUPTED_SLICE;
1100     }
1101 
1102     /*we currently don not support ASO*/
1103     if(((u2_first_mb_in_slice << ps_cur_slice->u1_mbaff_frame_flag)
1104                     <= ps_dec->u2_cur_mb_addr) && (ps_dec->u4_first_slice_in_pic == 0))
1105     {
1106         return ERROR_CORRUPTED_SLICE;
1107     }
1108 
1109     COPYTHECONTEXT("SH: first_mb_in_slice",u2_first_mb_in_slice);
1110 
1111     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1112 
1113     if(u4_temp > 9)
1114         return ERROR_INV_SLC_TYPE_T;
1115 
1116     u1_slice_type = u4_temp;
1117     COPYTHECONTEXT("SH: slice_type",(u1_slice_type));
1118     /* Find Out the Slice Type is 5 to 9 or not then Set the Flag   */
1119     /* u1_sl_typ_5_9 = 1 .Which tells that all the slices in the Pic*/
1120     /* will be of same type of current                            */
1121     if(u1_slice_type > 4)
1122     {
1123         u1_slice_type -= 5;
1124     }
1125 
1126     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1127     if(u4_temp & MASK_ERR_PIC_SET_ID)
1128         return ERROR_INV_SLICE_HDR_T;
1129     /* discard slice if pic param is invalid */
1130     COPYTHECONTEXT("SH: pic_parameter_set_id", u4_temp);
1131     ps_pps = &ps_dec->ps_pps[u4_temp];
1132     if(FALSE == ps_pps->u1_is_valid)
1133     {
1134         return ERROR_INV_SLICE_HDR_T;
1135     }
1136     ps_seq = ps_pps->ps_sps;
1137     if(!ps_seq)
1138         return ERROR_INV_SLICE_HDR_T;
1139     if(FALSE == ps_seq->u1_is_valid)
1140         return ERROR_INV_SLICE_HDR_T;
1141 
1142     /* Get the frame num */
1143     u2_frame_num = ih264d_get_bits_h264(ps_bitstrm,
1144                                          ps_seq->u1_bits_in_frm_num);
1145 //    H264_DEC_DEBUG_PRINT("FRAME %d First MB in slice: %d\n", u2_frame_num, u2_first_mb_in_slice);
1146 
1147     COPYTHECONTEXT("SH: frame_num", u2_frame_num);
1148 //    H264_DEC_DEBUG_PRINT("Second field: %d frame num: %d prv_frame_num: %d \n", ps_dec->u1_second_field, u2_frame_num, ps_dec->u2_prv_frame_num);
1149     if(!ps_dec->u1_first_slice_in_stream && ps_dec->u4_first_slice_in_pic)
1150     {
1151         pocstruct_t *ps_prev_poc = &ps_dec->s_prev_pic_poc;
1152         pocstruct_t *ps_cur_poc = &ps_dec->s_cur_pic_poc;
1153 
1154         ps_dec->u2_mbx = 0xffff;
1155         ps_dec->u2_mby = 0;
1156 
1157         if((0 == u1_is_idr_slice) && ps_cur_slice->u1_nal_ref_idc)
1158             ps_dec->u2_prev_ref_frame_num = ps_cur_slice->u2_frame_num;
1159 
1160         if(u1_is_idr_slice || ps_cur_slice->u1_mmco_equalto5)
1161             ps_dec->u2_prev_ref_frame_num = 0;
1162 
1163         if(ps_dec->ps_cur_sps->u1_gaps_in_frame_num_value_allowed_flag)
1164         {
1165             ret = ih264d_decode_gaps_in_frame_num(ps_dec, u2_frame_num);
1166             if (ret != OK) {
1167                 return ERROR_DBP_MANAGER_T;
1168             }
1169         }
1170 
1171         ps_prev_poc->i4_prev_frame_num_ofst = ps_cur_poc->i4_prev_frame_num_ofst;
1172         ps_prev_poc->u2_frame_num = ps_cur_poc->u2_frame_num;
1173         ps_prev_poc->u1_mmco_equalto5 = ps_cur_slice->u1_mmco_equalto5;
1174         if(ps_cur_slice->u1_nal_ref_idc)
1175         {
1176             ps_prev_poc->i4_pic_order_cnt_lsb = ps_cur_poc->i4_pic_order_cnt_lsb;
1177             ps_prev_poc->i4_pic_order_cnt_msb = ps_cur_poc->i4_pic_order_cnt_msb;
1178             ps_prev_poc->i4_delta_pic_order_cnt_bottom =
1179                             ps_cur_poc->i4_delta_pic_order_cnt_bottom;
1180             ps_prev_poc->i4_delta_pic_order_cnt[0] =
1181                             ps_cur_poc->i4_delta_pic_order_cnt[0];
1182             ps_prev_poc->i4_delta_pic_order_cnt[1] =
1183                             ps_cur_poc->i4_delta_pic_order_cnt[1];
1184             ps_prev_poc->u1_bot_field = ps_cur_poc->u1_bot_field;
1185         }
1186 
1187         ps_dec->u2_total_mbs_coded = 0;
1188     }
1189     /* Get the field related flags  */
1190     if(!ps_seq->u1_frame_mbs_only_flag)
1191     {
1192 
1193         u1_field_pic_flag = ih264d_get_bit_h264(ps_bitstrm);
1194         COPYTHECONTEXT("SH: field_pic_flag", u1_field_pic_flag);
1195         u1_bottom_field_flag = 0;
1196 
1197         if(u1_field_pic_flag)
1198         {
1199             ps_dec->pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan_fld;
1200             u1_bottom_field_flag = ih264d_get_bit_h264(ps_bitstrm);
1201             COPYTHECONTEXT("SH: bottom_field_flag", u1_bottom_field_flag);
1202 
1203         }
1204         else
1205         {
1206             ps_dec->pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan;
1207         }
1208     }
1209     else
1210     {
1211         u1_field_pic_flag = 0;
1212         u1_bottom_field_flag = 0;
1213 
1214         ps_dec->pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan;
1215     }
1216 
1217     u1_nal_unit_type = SLICE_NAL;
1218     if(u1_is_idr_slice)
1219     {
1220         u1_nal_unit_type = IDR_SLICE_NAL;
1221         u4_idr_pic_id = ih264d_uev(pu4_bitstrm_ofst,
1222                                    pu4_bitstrm_buf);
1223         if(u4_idr_pic_id > 65535)
1224             return ERROR_INV_SLICE_HDR_T;
1225         COPYTHECONTEXT("SH:  ", u4_idr_pic_id);
1226     }
1227 
1228     /* read delta pic order count information*/
1229     i_delta_poc[0] = i_delta_poc[1] = 0;
1230     s_tmp_poc.i4_pic_order_cnt_lsb = 0;
1231     s_tmp_poc.i4_delta_pic_order_cnt_bottom = 0;
1232     u1_pic_order_cnt_type = ps_seq->u1_pic_order_cnt_type;
1233     if(u1_pic_order_cnt_type == 0)
1234     {
1235         i_temp = ih264d_get_bits_h264(
1236                         ps_bitstrm,
1237                         ps_seq->u1_log2_max_pic_order_cnt_lsb_minus);
1238         if(i_temp < 0 || i_temp >= ps_seq->i4_max_pic_order_cntLsb)
1239             return ERROR_INV_SLICE_HDR_T;
1240         s_tmp_poc.i4_pic_order_cnt_lsb = i_temp;
1241         COPYTHECONTEXT("SH: pic_order_cnt_lsb", s_tmp_poc.i4_pic_order_cnt_lsb);
1242 
1243         if((ps_pps->u1_pic_order_present_flag == 1) && (!u1_field_pic_flag))
1244         {
1245             s_tmp_poc.i4_delta_pic_order_cnt_bottom = ih264d_sev(
1246                             pu4_bitstrm_ofst, pu4_bitstrm_buf);
1247             //if(s_tmp_poc.i4_delta_pic_order_cnt_bottom > ps_seq->i4_max_pic_order_cntLsb)
1248             COPYTHECONTEXT("SH: delta_pic_order_cnt_bottom",
1249                             s_tmp_poc.i4_delta_pic_order_cnt_bottom);
1250         }
1251     }
1252 
1253     s_tmp_poc.i4_delta_pic_order_cnt[0] = 0;
1254     s_tmp_poc.i4_delta_pic_order_cnt[1] = 0;
1255     if(u1_pic_order_cnt_type == 1
1256                     && (!ps_seq->u1_delta_pic_order_always_zero_flag))
1257     {
1258         s_tmp_poc.i4_delta_pic_order_cnt[0] = ih264d_sev(pu4_bitstrm_ofst,
1259                                                          pu4_bitstrm_buf);
1260         COPYTHECONTEXT("SH: delta_pic_order_cnt[0]",
1261                         s_tmp_poc.i4_delta_pic_order_cnt[0]);
1262 
1263         if(ps_pps->u1_pic_order_present_flag && !u1_field_pic_flag)
1264         {
1265             s_tmp_poc.i4_delta_pic_order_cnt[1] = ih264d_sev(
1266                             pu4_bitstrm_ofst, pu4_bitstrm_buf);
1267             COPYTHECONTEXT("SH: delta_pic_order_cnt[1]",
1268                             s_tmp_poc.i4_delta_pic_order_cnt[1]);
1269         }
1270     }
1271 
1272     if(ps_pps->u1_redundant_pic_cnt_present_flag)
1273     {
1274         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1275         if(u4_temp > MAX_REDUNDANT_PIC_CNT)
1276             return ERROR_INV_SLICE_HDR_T;
1277         u1_redundant_pic_cnt = u4_temp;
1278         COPYTHECONTEXT("SH: redundant_pic_cnt", u1_redundant_pic_cnt);
1279     }
1280 
1281     /*--------------------------------------------------------------------*/
1282     /* Check if the slice is part of new picture                          */
1283     /*--------------------------------------------------------------------*/
1284     /* First slice of a picture is always considered as part of new picture */
1285     i1_is_end_of_poc = 1;
1286     ps_dec->ps_dec_err_status->u1_err_flag &= MASK_REJECT_CUR_PIC;
1287 
1288     if(ps_dec->u4_first_slice_in_pic == 0)
1289     {
1290         i1_is_end_of_poc = ih264d_is_end_of_pic(u2_frame_num, u1_nal_ref_idc,
1291                                             &s_tmp_poc, &ps_dec->s_cur_pic_poc,
1292                                             ps_cur_slice, u1_pic_order_cnt_type,
1293                                             u1_nal_unit_type, u4_idr_pic_id,
1294                                             u1_field_pic_flag,
1295                                             u1_bottom_field_flag);
1296         if(i1_is_end_of_poc)
1297         {
1298             ps_dec->u1_first_slice_in_stream = 0;
1299             return ERROR_INCOMPLETE_FRAME;
1300         }
1301 
1302     }
1303 
1304     /*--------------------------------------------------------------------*/
1305     /* Check for error in slice and parse the missing/corrupted MB's      */
1306     /* as skip-MB's in an inserted P-slice                                */
1307     /*--------------------------------------------------------------------*/
1308     u1_mbaff = ps_seq->u1_mb_aff_flag && (!u1_field_pic_flag);
1309     prev_slice_err = 0;
1310 
1311     if(i1_is_end_of_poc || ps_dec->u1_first_slice_in_stream)
1312     {
1313         /* If the current slice is not a field or frame number of the current
1314          * slice doesn't match with previous slice, and decoder is expecting
1315          * to decode a field i.e. ps_dec->u1_top_bottom_decoded is not 0 and
1316          * is not (TOP_FIELD_ONLY | BOT_FIELD_ONLY), treat it as a dangling
1317          * field */
1318         if((u1_field_pic_flag == 0 || u2_frame_num != ps_dec->u2_prv_frame_num)
1319                && ps_dec->u1_top_bottom_decoded != 0
1320                    && ps_dec->u1_top_bottom_decoded
1321                        != (TOP_FIELD_ONLY | BOT_FIELD_ONLY))
1322         {
1323             ps_dec->u1_dangling_field = 1;
1324             if(ps_dec->u4_first_slice_in_pic)
1325             {
1326                 // first slice - dangling field
1327                 prev_slice_err = 1;
1328             }
1329             else
1330             {
1331                 // last slice - dangling field
1332                 prev_slice_err = 2;
1333             }
1334 
1335             if(ps_dec->u1_top_bottom_decoded ==TOP_FIELD_ONLY)
1336                 ps_cur_slice->u1_bottom_field_flag = 1;
1337             else
1338                 ps_cur_slice->u1_bottom_field_flag = 0;
1339 
1340             num_mb_skipped = (ps_dec->u2_frm_ht_in_mbs * ps_dec->u2_frm_wd_in_mbs)
1341                     - ps_dec->u2_total_mbs_coded;
1342             ps_cur_poc = &ps_dec->s_cur_pic_poc;
1343 
1344             u1_is_idr_slice = ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL;
1345         }
1346         else if(ps_dec->u4_first_slice_in_pic)
1347         {
1348             if(u2_first_mb_in_slice > 0)
1349             {
1350                 // first slice - missing/header corruption
1351                 prev_slice_err = 1;
1352                 num_mb_skipped = u2_first_mb_in_slice << u1_mbaff;
1353                 ps_cur_poc = &s_tmp_poc;
1354 
1355                 // initializing slice parameters
1356                 ps_cur_slice->u4_idr_pic_id = u4_idr_pic_id;
1357                 ps_cur_slice->u1_field_pic_flag = u1_field_pic_flag;
1358                 ps_cur_slice->u1_bottom_field_flag = u1_bottom_field_flag;
1359                 ps_cur_slice->i4_pic_order_cnt_lsb =
1360                         s_tmp_poc.i4_pic_order_cnt_lsb;
1361                 ps_cur_slice->u1_nal_unit_type = u1_nal_unit_type;
1362                 ps_cur_slice->u1_redundant_pic_cnt = u1_redundant_pic_cnt;
1363                 ps_cur_slice->u1_nal_ref_idc = u1_nal_ref_idc;
1364                 ps_cur_slice->u1_pic_order_cnt_type = u1_pic_order_cnt_type;
1365                 ps_cur_slice->u1_mbaff_frame_flag = ps_seq->u1_mb_aff_flag
1366                         && (!u1_field_pic_flag);
1367             }
1368         }
1369         else
1370         {
1371             /* since i1_is_end_of_poc is set ,means new frame num is encountered. so conceal the current frame
1372              * completely */
1373             prev_slice_err = 2;
1374             num_mb_skipped = (ps_dec->u2_frm_ht_in_mbs
1375                             * ps_dec->u2_frm_wd_in_mbs)
1376                             - ps_dec->u2_total_mbs_coded;
1377             ps_cur_poc = &s_tmp_poc;
1378         }
1379     }
1380     else
1381     {
1382         if((u2_first_mb_in_slice << u1_mbaff) > ps_dec->u2_total_mbs_coded)
1383         {
1384             // previous slice - missing/corruption
1385             prev_slice_err = 2;
1386             num_mb_skipped = (u2_first_mb_in_slice << u1_mbaff)
1387                     - ps_dec->u2_total_mbs_coded;
1388             ps_cur_poc = &s_tmp_poc;
1389         }
1390         else if((u2_first_mb_in_slice << u1_mbaff) < ps_dec->u2_total_mbs_coded)
1391         {
1392             return ERROR_CORRUPTED_SLICE;
1393         }
1394     }
1395 
1396     if(prev_slice_err)
1397     {
1398         ret = ih264d_mark_err_slice_skip(ps_dec, num_mb_skipped, u1_is_idr_slice, u2_frame_num, ps_cur_poc, prev_slice_err);
1399 
1400         if(ps_dec->u1_dangling_field == 1)
1401         {
1402             ps_dec->u1_second_field = 1 - ps_dec->u1_second_field;
1403             ps_dec->u1_first_slice_in_stream = 0;
1404             ps_dec->u1_top_bottom_decoded = TOP_FIELD_ONLY | BOT_FIELD_ONLY;
1405             return ERROR_DANGLING_FIELD_IN_PIC;
1406         }
1407 
1408         if(prev_slice_err == 2)
1409         {
1410             ps_dec->u1_first_slice_in_stream = 0;
1411             return ERROR_INCOMPLETE_FRAME;
1412         }
1413 
1414         if(ps_dec->u2_total_mbs_coded
1415                 >= ps_dec->u2_frm_ht_in_mbs * ps_dec->u2_frm_wd_in_mbs)
1416         {
1417             /* return if all MBs in frame are parsed*/
1418             ps_dec->u1_first_slice_in_stream = 0;
1419             return ERROR_IN_LAST_SLICE_OF_PIC;
1420         }
1421 
1422         if(ps_dec->ps_dec_err_status->u1_err_flag & REJECT_CUR_PIC)
1423         {
1424             ih264d_err_pic_dispbuf_mgr(ps_dec);
1425             return ERROR_NEW_FRAME_EXPECTED;
1426         }
1427 
1428         if(ret != OK)
1429             return ret;
1430 
1431         i1_is_end_of_poc = 0;
1432     }
1433 
1434     if(u1_field_pic_flag)
1435     {
1436         ps_dec->u2_prv_frame_num = u2_frame_num;
1437     }
1438 
1439     if(ps_cur_slice->u1_mmco_equalto5)
1440     {
1441         WORD64 i8_result;
1442         WORD32 i4_temp_poc;
1443         WORD32 i4_top_field_order_poc, i4_bot_field_order_poc;
1444 
1445         if(!ps_cur_slice->u1_field_pic_flag) // or a complementary field pair
1446         {
1447             i4_top_field_order_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
1448             i4_bot_field_order_poc =
1449                             ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
1450             i4_temp_poc = MIN(i4_top_field_order_poc,
1451                                      i4_bot_field_order_poc);
1452         }
1453         else if(!ps_cur_slice->u1_bottom_field_flag)
1454             i4_temp_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
1455         else
1456             i4_temp_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
1457 
1458         i8_result = (WORD64)i4_temp_poc - ps_dec->ps_cur_pic->i4_top_field_order_cnt;
1459         if(IS_OUT_OF_RANGE_S32(i8_result))
1460         {
1461             return ERROR_INV_POC;
1462         }
1463         ps_dec->ps_cur_pic->i4_top_field_order_cnt = i8_result;
1464         i8_result = (WORD64)i4_temp_poc
1465                         - ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
1466         if(IS_OUT_OF_RANGE_S32(i8_result))
1467         {
1468             return ERROR_INV_POC;
1469         }
1470         ps_dec->ps_cur_pic->i4_bottom_field_order_cnt = i8_result;
1471         ps_dec->ps_cur_pic->i4_poc = i4_temp_poc;
1472         ps_dec->ps_cur_pic->i4_avg_poc = i4_temp_poc;
1473     }
1474     if(ps_dec->u4_first_slice_in_pic)
1475     {
1476         ret = ih264d_decode_pic_order_cnt(u1_is_idr_slice, u2_frame_num,
1477                                           &ps_dec->s_prev_pic_poc,
1478                                           &s_tmp_poc, ps_cur_slice, ps_pps,
1479                                           u1_nal_ref_idc,
1480                                           u1_bottom_field_flag,
1481                                           u1_field_pic_flag, &i4_poc);
1482         if(ret != OK)
1483             return ret;
1484         /* Display seq no calculations */
1485         if(i4_poc >= ps_dec->i4_max_poc)
1486             ps_dec->i4_max_poc = i4_poc;
1487         /* IDR Picture or POC wrap around */
1488         if(i4_poc == 0)
1489         {
1490             WORD64 i8_temp;
1491             i8_temp = (WORD64)ps_dec->i4_prev_max_display_seq
1492                       + ps_dec->i4_max_poc
1493                       + ps_dec->u1_max_dec_frame_buffering + 1;
1494             /*If i4_prev_max_display_seq overflows integer range, reset it */
1495             ps_dec->i4_prev_max_display_seq = IS_OUT_OF_RANGE_S32(i8_temp)?
1496                                               0 : i8_temp;
1497             ps_dec->i4_max_poc = 0;
1498         }
1499     }
1500 
1501     /* Increment only if the current slice has atleast 1 more MB */
1502     if (ps_dec->u4_first_slice_in_pic == 0 &&
1503         (ps_dec->ps_parse_cur_slice->u4_first_mb_in_slice <
1504         (UWORD32)(ps_dec->u2_total_mbs_coded >> ps_dec->ps_cur_slice->u1_mbaff_frame_flag)))
1505     {
1506         ps_dec->ps_parse_cur_slice++;
1507         ps_dec->u2_cur_slice_num++;
1508         // in the case of single core increment ps_decode_cur_slice
1509         if(ps_dec->u1_separate_parse == 0)
1510         {
1511             ps_dec->ps_decode_cur_slice++;
1512         }
1513     }
1514 
1515     ps_dec->u1_slice_header_done = 0;
1516 
1517     /*--------------------------------------------------------------------*/
1518     /* Copy the values read from the bitstream to the slice header and then*/
1519     /* If the slice is first slice in picture, then do Start of Picture   */
1520     /* processing.                                                        */
1521     /*--------------------------------------------------------------------*/
1522     ps_cur_slice->i4_delta_pic_order_cnt[0] = i_delta_poc[0];
1523     ps_cur_slice->i4_delta_pic_order_cnt[1] = i_delta_poc[1];
1524     ps_cur_slice->u4_idr_pic_id = u4_idr_pic_id;
1525     ps_cur_slice->u2_first_mb_in_slice = u2_first_mb_in_slice;
1526     ps_cur_slice->u1_field_pic_flag = u1_field_pic_flag;
1527     ps_cur_slice->u1_bottom_field_flag = u1_bottom_field_flag;
1528     ps_cur_slice->u1_slice_type = u1_slice_type;
1529     ps_cur_slice->i4_pic_order_cnt_lsb = s_tmp_poc.i4_pic_order_cnt_lsb;
1530 
1531     ps_cur_slice->u1_nal_unit_type = u1_nal_unit_type;
1532     ps_cur_slice->u1_redundant_pic_cnt = u1_redundant_pic_cnt;
1533     ps_cur_slice->u1_nal_ref_idc = u1_nal_ref_idc;
1534     ps_cur_slice->u1_pic_order_cnt_type = u1_pic_order_cnt_type;
1535 
1536     if(ps_seq->u1_frame_mbs_only_flag)
1537         ps_cur_slice->u1_direct_8x8_inference_flag =
1538                         ps_seq->u1_direct_8x8_inference_flag;
1539     else
1540         ps_cur_slice->u1_direct_8x8_inference_flag = 1;
1541 
1542     if(u1_slice_type == B_SLICE)
1543     {
1544         ps_cur_slice->u1_direct_spatial_mv_pred_flag = ih264d_get_bit_h264(
1545                         ps_bitstrm);
1546         COPYTHECONTEXT("SH: direct_spatial_mv_pred_flag",
1547                         ps_cur_slice->u1_direct_spatial_mv_pred_flag);
1548 
1549         if(ps_cur_slice->u1_direct_spatial_mv_pred_flag)
1550             ps_cur_slice->pf_decodeDirect = ih264d_decode_spatial_direct;
1551         else
1552             ps_cur_slice->pf_decodeDirect = ih264d_decode_temporal_direct;
1553         if(!((ps_pps->ps_sps->u1_mb_aff_flag) && (!u1_field_pic_flag)))
1554             ps_dec->pf_mvpred = ih264d_mvpred_nonmbaffB;
1555     }
1556     else
1557     {
1558         if(!((ps_pps->ps_sps->u1_mb_aff_flag) && (!u1_field_pic_flag)))
1559             ps_dec->pf_mvpred = ih264d_mvpred_nonmbaff;
1560     }
1561 
1562     if(ps_dec->u4_first_slice_in_pic)
1563     {
1564         if(u2_first_mb_in_slice == 0)
1565         {
1566             ret = ih264d_start_of_pic(ps_dec, i4_poc, &s_tmp_poc, u2_frame_num, ps_pps);
1567             if(ret != OK)
1568                 return ret;
1569         }
1570 
1571         ps_dec->u4_output_present = 0;
1572 
1573         {
1574             ih264d_get_next_display_field(ps_dec,
1575                                           ps_dec->ps_out_buffer,
1576                                           &(ps_dec->s_disp_op));
1577             /* If error code is non-zero then there is no buffer available for display,
1578              hence avoid format conversion */
1579 
1580             if(0 != ps_dec->s_disp_op.u4_error_code)
1581             {
1582                 ps_dec->u4_fmt_conv_cur_row = ps_dec->s_disp_frame_info.u4_y_ht;
1583             }
1584             else
1585                 ps_dec->u4_output_present = 1;
1586         }
1587         if(ps_dec->u1_separate_parse == 1)
1588         {
1589             if(ps_dec->u4_dec_thread_created == 0)
1590             {
1591                 ithread_create(ps_dec->pv_dec_thread_handle, NULL,
1592                                (void *)ih264d_decode_picture_thread,
1593                                (void *)ps_dec);
1594 
1595                 ps_dec->u4_dec_thread_created = 1;
1596             }
1597             if(ps_dec->i4_threads_active)
1598             {
1599                 ret = ithread_mutex_lock(ps_dec->apv_proc_start_mutex[0]);
1600                 RETURN_IF((ret != IV_SUCCESS), ret);
1601 
1602                 ps_dec->ai4_process_start[0] = PROC_START;
1603                 ret = ithread_cond_signal(ps_dec->apv_proc_start_condition[0]);
1604                 RETURN_IF((ret != IV_SUCCESS), ret);
1605 
1606                 ret = ithread_mutex_unlock(ps_dec->apv_proc_start_mutex[0]);
1607                 RETURN_IF((ret != IV_SUCCESS), ret);
1608             }
1609 
1610             if((ps_dec->u4_num_cores == 3) &&
1611                             ((ps_dec->u4_app_disable_deblk_frm == 0) || ps_dec->i1_recon_in_thread3_flag)
1612                             && (ps_dec->u4_bs_deblk_thread_created == 0))
1613             {
1614                 ps_dec->u4_start_recon_deblk = 0;
1615                 ithread_create(ps_dec->pv_bs_deblk_thread_handle, NULL,
1616                                (void *)ih264d_recon_deblk_thread,
1617                                (void *)ps_dec);
1618                 ps_dec->u4_bs_deblk_thread_created = 1;
1619             }
1620             if(ps_dec->i4_threads_active)
1621             {
1622                 if (ps_dec->u4_bs_deblk_thread_created)
1623                 {
1624                     ret = ithread_mutex_lock(ps_dec->apv_proc_start_mutex[1]);
1625                     RETURN_IF((ret != IV_SUCCESS), ret);
1626 
1627                     ps_dec->ai4_process_start[1] = PROC_START;
1628                     ret = ithread_cond_signal(ps_dec->apv_proc_start_condition[1]);
1629                     RETURN_IF((ret != IV_SUCCESS), ret);
1630 
1631                     ret = ithread_mutex_unlock(ps_dec->apv_proc_start_mutex[1]);
1632                     RETURN_IF((ret != IV_SUCCESS), ret);
1633                 }
1634             }
1635         }
1636 
1637     }
1638 
1639     /* INITIALIZATION of fn ptrs for MC and formMbPartInfo functions */
1640     {
1641         UWORD8 uc_nofield_nombaff;
1642 
1643 
1644 
1645         uc_nofield_nombaff = ((ps_dec->ps_cur_slice->u1_field_pic_flag == 0)
1646                         && (ps_dec->ps_cur_slice->u1_mbaff_frame_flag == 0)
1647                         && (u1_slice_type != B_SLICE)
1648                         && (ps_dec->ps_cur_pps->u1_wted_pred_flag == 0));
1649 
1650         /* Initialise MC and formMbPartInfo fn ptrs one time based on profile_idc */
1651 
1652         if(uc_nofield_nombaff)
1653         {
1654             ps_dec->p_form_mb_part_info = ih264d_form_mb_part_info_bp;
1655             ps_dec->p_motion_compensate = ih264d_motion_compensate_bp;
1656         }
1657         else
1658         {
1659             ps_dec->p_form_mb_part_info = ih264d_form_mb_part_info_mp;
1660             ps_dec->p_motion_compensate = ih264d_motion_compensate_mp;
1661         }
1662 
1663 
1664     }
1665 
1666     /*
1667      * Decide whether to decode the current picture or not
1668      */
1669     {
1670         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
1671         if(ps_err->u4_frm_sei_sync == u2_frame_num)
1672         {
1673             ps_err->u1_err_flag = ACCEPT_ALL_PICS;
1674             ps_err->u4_frm_sei_sync = SYNC_FRM_DEFAULT;
1675         }
1676         ps_err->u4_cur_frm = u2_frame_num;
1677     }
1678 
1679     /* Decision for decoding if the picture is to be skipped */
1680     {
1681         WORD32 i4_skip_b_pic, i4_skip_p_pic;
1682 
1683         i4_skip_b_pic = (ps_dec->u4_skip_frm_mask & B_SLC_BIT)
1684                         && (B_SLICE == u1_slice_type) && (0 == u1_nal_ref_idc);
1685 
1686         i4_skip_p_pic = (ps_dec->u4_skip_frm_mask & P_SLC_BIT)
1687                         && (P_SLICE == u1_slice_type) && (0 == u1_nal_ref_idc);
1688 
1689         /**************************************************************/
1690         /* Skip the B picture if skip mask is set for B picture and   */
1691         /* Current B picture is a non reference B picture or there is */
1692         /* no user for reference B picture                            */
1693         /**************************************************************/
1694         if(i4_skip_b_pic)
1695         {
1696             ps_dec->ps_cur_pic->u4_pack_slc_typ |= B_SLC_BIT;
1697             /* Don't decode the picture in SKIP-B mode if that picture is B */
1698             /* and also it is not to be used as a reference picture         */
1699             ps_dec->u1_last_pic_not_decoded = 1;
1700 
1701             return OK;
1702         }
1703         /**************************************************************/
1704         /* Skip the P picture if skip mask is set for P picture and   */
1705         /* Current P picture is a non reference P picture or there is */
1706         /* no user for reference P picture                            */
1707         /**************************************************************/
1708         if(i4_skip_p_pic)
1709         {
1710             ps_dec->ps_cur_pic->u4_pack_slc_typ |= P_SLC_BIT;
1711             /* Don't decode the picture in SKIP-P mode if that picture is P */
1712             /* and also it is not to be used as a reference picture         */
1713             ps_dec->u1_last_pic_not_decoded = 1;
1714 
1715             return OK;
1716         }
1717     }
1718 
1719     {
1720         UWORD16 u2_mb_x, u2_mb_y;
1721 
1722         ps_dec->i4_submb_ofst = ((u2_first_mb_in_slice
1723                         << ps_cur_slice->u1_mbaff_frame_flag) * SUB_BLK_SIZE)
1724                         - SUB_BLK_SIZE;
1725         if(u2_first_mb_in_slice)
1726         {
1727             UWORD8 u1_mb_aff;
1728             UWORD8 u1_field_pic;
1729             UWORD16 u2_frm_wd_in_mbs;
1730             u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
1731             u1_mb_aff = ps_cur_slice->u1_mbaff_frame_flag;
1732             u1_field_pic = ps_cur_slice->u1_field_pic_flag;
1733 
1734             {
1735                 UWORD32 x_offset;
1736                 UWORD32 y_offset;
1737                 UWORD32 u4_frame_stride;
1738                 tfr_ctxt_t *ps_trns_addr; // = &ps_dec->s_tran_addrecon_parse;
1739 
1740                 if(ps_dec->u1_separate_parse)
1741                 {
1742                     ps_trns_addr = &ps_dec->s_tran_addrecon_parse;
1743                 }
1744                 else
1745                 {
1746                     ps_trns_addr = &ps_dec->s_tran_addrecon;
1747                 }
1748                 u2_mb_x = MOD(u2_first_mb_in_slice, u2_frm_wd_in_mbs);
1749                 u2_mb_y = DIV(u2_first_mb_in_slice, u2_frm_wd_in_mbs);
1750 
1751                 u2_mb_y <<= u1_mb_aff;
1752 
1753                 if((u2_mb_x > u2_frm_wd_in_mbs - 1)
1754                                 || (u2_mb_y > ps_dec->u2_frm_ht_in_mbs - 1))
1755                 {
1756                     return ERROR_CORRUPTED_SLICE;
1757                 }
1758 
1759                 u4_frame_stride = ps_dec->u2_frm_wd_y << u1_field_pic;
1760                 x_offset = u2_mb_x << 4;
1761                 y_offset = (u2_mb_y * u4_frame_stride) << 4;
1762 
1763                 ps_trns_addr->pu1_dest_y = ps_dec->s_cur_pic.pu1_buf1 + x_offset
1764                                 + y_offset;
1765 
1766                 u4_frame_stride = ps_dec->u2_frm_wd_uv << u1_field_pic;
1767                 x_offset >>= 1;
1768                 y_offset = (u2_mb_y * u4_frame_stride) << 3;
1769 
1770                 x_offset *= YUV420SP_FACTOR;
1771 
1772                 ps_trns_addr->pu1_dest_u = ps_dec->s_cur_pic.pu1_buf2 + x_offset
1773                                 + y_offset;
1774                 ps_trns_addr->pu1_dest_v = ps_dec->s_cur_pic.pu1_buf3 + x_offset
1775                                 + y_offset;
1776 
1777                 ps_trns_addr->pu1_mb_y = ps_trns_addr->pu1_dest_y;
1778                 ps_trns_addr->pu1_mb_u = ps_trns_addr->pu1_dest_u;
1779                 ps_trns_addr->pu1_mb_v = ps_trns_addr->pu1_dest_v;
1780 
1781 
1782                 // assign the deblock structure pointers to start of slice
1783                 if(ps_dec->u1_separate_parse == 1)
1784                 {
1785                     ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic
1786                                     + (u2_first_mb_in_slice << u1_mb_aff);
1787                 }
1788                 else
1789                 {
1790                         ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic
1791                                         + (u2_first_mb_in_slice << u1_mb_aff);
1792                 }
1793 
1794                 ps_dec->u2_cur_mb_addr = (u2_first_mb_in_slice << u1_mb_aff);
1795 
1796                 ps_dec->ps_mv_cur = ps_dec->s_cur_pic.ps_mv
1797                                 + ((u2_first_mb_in_slice << u1_mb_aff) << 4);
1798             }
1799         }
1800         else
1801         {
1802             tfr_ctxt_t *ps_trns_addr;
1803 
1804             if(ps_dec->u1_separate_parse)
1805             {
1806                 ps_trns_addr = &ps_dec->s_tran_addrecon_parse;
1807             }
1808             else
1809             {
1810                 ps_trns_addr = &ps_dec->s_tran_addrecon;
1811             }
1812 
1813             u2_mb_x = 0xffff;
1814             u2_mb_y = 0;
1815             // assign the deblock structure pointers to start of slice
1816             ps_dec->u2_cur_mb_addr = 0;
1817             ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic;
1818             ps_dec->ps_mv_cur = ps_dec->s_cur_pic.ps_mv;
1819             ps_trns_addr->pu1_dest_y = ps_dec->s_cur_pic.pu1_buf1;
1820             ps_trns_addr->pu1_dest_u = ps_dec->s_cur_pic.pu1_buf2;
1821             ps_trns_addr->pu1_dest_v = ps_dec->s_cur_pic.pu1_buf3;
1822 
1823             ps_trns_addr->pu1_mb_y = ps_trns_addr->pu1_dest_y;
1824             ps_trns_addr->pu1_mb_u = ps_trns_addr->pu1_dest_u;
1825             ps_trns_addr->pu1_mb_v = ps_trns_addr->pu1_dest_v;
1826 
1827         }
1828 
1829         ps_dec->ps_part = ps_dec->ps_parse_part_params;
1830 
1831         ps_dec->u2_mbx =
1832                         (MOD(u2_first_mb_in_slice - 1, ps_seq->u2_frm_wd_in_mbs));
1833         ps_dec->u2_mby =
1834                         (DIV(u2_first_mb_in_slice - 1, ps_seq->u2_frm_wd_in_mbs));
1835         ps_dec->u2_mby <<= ps_cur_slice->u1_mbaff_frame_flag;
1836         ps_dec->i2_prev_slice_mbx = ps_dec->u2_mbx;
1837         ps_dec->i2_prev_slice_mby = ps_dec->u2_mby;
1838     }
1839 
1840     /* RBSP stop bit is used for CABAC decoding*/
1841     ps_bitstrm->u4_max_ofst += ps_dec->ps_cur_pps->u1_entropy_coding_mode;
1842 
1843     ps_dec->u1_B = (u1_slice_type == B_SLICE);
1844     ps_dec->u4_next_mb_skip = 0;
1845 
1846     ps_dec->ps_parse_cur_slice->u4_first_mb_in_slice =
1847                     ps_dec->ps_cur_slice->u2_first_mb_in_slice;
1848     ps_dec->ps_parse_cur_slice->slice_type =
1849                     ps_dec->ps_cur_slice->u1_slice_type;
1850 
1851 
1852     ps_dec->u4_start_recon_deblk = 1;
1853     {
1854         WORD32 num_entries;
1855         WORD32 size;
1856         UWORD8 *pu1_buf;
1857 
1858         num_entries = MAX_FRAMES;
1859         if((1 >= ps_dec->ps_cur_sps->u1_num_ref_frames) &&
1860             (0 == ps_dec->i4_display_delay))
1861         {
1862             num_entries = 1;
1863         }
1864         num_entries = ((2 * num_entries) + 1);
1865         num_entries *= 2;
1866 
1867 
1868         size = num_entries * sizeof(void *);
1869         size += PAD_MAP_IDX_POC * sizeof(void *);
1870 
1871         pu1_buf = (UWORD8 *)ps_dec->pv_map_ref_idx_to_poc_buf;
1872         pu1_buf += size * ps_dec->u2_cur_slice_num;
1873         ps_dec->ps_parse_cur_slice->ppv_map_ref_idx_to_poc = ( void *)pu1_buf;
1874     }
1875 
1876     if(ps_dec->u1_separate_parse)
1877     {
1878         ps_dec->ps_parse_cur_slice->pv_tu_coeff_data_start = ps_dec->pv_parse_tu_coeff_data;
1879     }
1880     else
1881     {
1882         ps_dec->pv_proc_tu_coeff_data = ps_dec->pv_parse_tu_coeff_data;
1883     }
1884 
1885     ret = ih264d_fix_error_in_dpb(ps_dec);
1886     if(ret < 0)
1887         return ERROR_DBP_MANAGER_T;
1888 
1889     if(u1_slice_type == I_SLICE)
1890     {
1891         ps_dec->ps_cur_pic->u4_pack_slc_typ |= I_SLC_BIT;
1892 
1893         ret = ih264d_parse_islice(ps_dec, u2_first_mb_in_slice);
1894         ps_dec->u1_pr_sl_type = u1_slice_type;
1895         if(ps_dec->i4_pic_type != B_SLICE && ps_dec->i4_pic_type != P_SLICE)
1896             ps_dec->i4_pic_type = I_SLICE;
1897 
1898     }
1899     else if(u1_slice_type == P_SLICE)
1900     {
1901         ps_dec->ps_cur_pic->u4_pack_slc_typ |= P_SLC_BIT;
1902         ret = ih264d_parse_pslice(ps_dec, u2_first_mb_in_slice);
1903         ps_dec->u1_pr_sl_type = u1_slice_type;
1904         if(ps_dec->i4_pic_type != B_SLICE)
1905             ps_dec->i4_pic_type = P_SLICE;
1906     }
1907     else if(u1_slice_type == B_SLICE)
1908     {
1909         ps_dec->ps_cur_pic->u4_pack_slc_typ |= B_SLC_BIT;
1910         ret = ih264d_parse_bslice(ps_dec, u2_first_mb_in_slice);
1911         ps_dec->u1_pr_sl_type = u1_slice_type;
1912         ps_dec->i4_pic_type = B_SLICE;
1913     }
1914     else
1915         return ERROR_INV_SLC_TYPE_T;
1916 
1917     if(ps_dec->u1_slice_header_done)
1918     {
1919         /* set to zero to indicate a valid slice has been decoded */
1920         ps_dec->u1_first_slice_in_stream = 0;
1921     }
1922 
1923     if(ret != OK)
1924         return ret;
1925 
1926     if(u1_nal_ref_idc != 0)
1927     {
1928         if(!ps_dec->ps_dpb_cmds->u1_dpb_commands_read)
1929         {
1930             memcpy((void *)ps_dec->ps_dpb_cmds, (void *)(&(ps_dec->s_dpb_cmds_scratch)),
1931                    sizeof(dpb_commands_t));
1932         }
1933     }
1934 
1935     /* storing last Mb X and MbY of the slice */
1936     ps_dec->i2_prev_slice_mbx = ps_dec->u2_mbx;
1937     ps_dec->i2_prev_slice_mby = ps_dec->u2_mby;
1938 
1939     /* End of Picture detection */
1940 
1941     if(ps_dec->u2_total_mbs_coded >= (ps_seq->u2_max_mb_addr + 1))
1942     {
1943         ps_dec->u1_pic_decode_done = 1;
1944 
1945     }
1946 
1947     {
1948         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
1949         if((ps_err->u1_err_flag & REJECT_PB_PICS)
1950                         && (ps_err->u1_cur_pic_type == PIC_TYPE_I))
1951         {
1952             ps_err->u1_err_flag = ACCEPT_ALL_PICS;
1953         }
1954     }
1955 
1956     PRINT_BIN_BIT_RATIO(ps_dec)
1957 
1958     return ret;
1959 }
1960