• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_utils.c
23  *
24  * \brief
25  *    Contains routines that handle of start and end of pic processing
26  *
27  * \date
28  *    19/12/2002
29  *
30  * \author  AI
31  **************************************************************************
32  */
33 
34 #include <string.h>
35 #include "ih264_typedefs.h"
36 #include "ithread.h"
37 #include "ih264d_deblocking.h"
38 #include "ih264d_parse_slice.h"
39 #include "ih264d_parse_cavlc.h"
40 #include "ih264d_dpb_manager.h"
41 #include "ih264d_defs.h"
42 #include "ih264d_structs.h"
43 #include "ih264d_mem_request.h"
44 #include "ih264_typedefs.h"
45 #include "ih264_macros.h"
46 #include "ih264_platform_macros.h"
47 #include "ih264d_tables.h"
48 #include "ih264d_debug.h"
49 #include "ih264d_mb_utils.h"
50 #include "ih264d_error_handler.h"
51 #include "ih264d_dpb_manager.h"
52 #include "ih264d_utils.h"
53 #include "ih264d_defs.h"
54 #include "ih264d_tables.h"
55 #include "ih264d_inter_pred.h"
56 #include "ih264d_dpb_manager.h"
57 #include "iv.h"
58 #include "ivd.h"
59 #include "ih264d_format_conv.h"
60 #include "ih264_error.h"
61 #include "ih264_disp_mgr.h"
62 #include "ih264_buf_mgr.h"
63 #include "ih264d_utils.h"
64 
65 /*!
66  **************************************************************************
67  * \if Function name : ih264d_is_end_of_pic \endif
68  *
69  * \brief
70  *    Determines whether current slice is first slice of a new picture as
71  *    defined in 7.4.1.2.4 of 14496-10.
72  *
73  * \return
74  *    Return 1 if current slice is first slice of a new picture
75  *    Otherwise it returns 0
76  **************************************************************************
77  */
ih264d_is_end_of_pic(UWORD16 u2_frame_num,UWORD8 u1_nal_ref_idc,pocstruct_t * ps_cur_poc,pocstruct_t * ps_prev_poc,dec_slice_params_t * ps_prev_slice,UWORD8 u1_pic_order_cnt_type,UWORD8 u1_nal_unit_type,UWORD32 u4_idr_pic_id,UWORD8 u1_field_pic_flag,UWORD8 u1_bottom_field_flag)78 UWORD8 ih264d_is_end_of_pic(UWORD16 u2_frame_num,
79                             UWORD8 u1_nal_ref_idc,
80                             pocstruct_t *ps_cur_poc,
81                             pocstruct_t *ps_prev_poc,
82                             dec_slice_params_t * ps_prev_slice, /*!< Previous slice parameters*/
83                             UWORD8 u1_pic_order_cnt_type,
84                             UWORD8 u1_nal_unit_type,
85                             UWORD32 u4_idr_pic_id,
86                             UWORD8 u1_field_pic_flag,
87                             UWORD8 u1_bottom_field_flag)
88 {
89     WORD8 i1_is_end_of_pic;
90     WORD8 a, b, c, d, e, f, g, h;
91 
92     a = b = c = d = e = f = g = h = 0;
93     a = (ps_prev_slice->u2_frame_num != u2_frame_num);
94     b = (ps_prev_slice->u1_field_pic_flag != u1_field_pic_flag);
95     if(u1_field_pic_flag && ps_prev_slice->u1_field_pic_flag)
96         c = (u1_bottom_field_flag != ps_prev_slice->u1_bottom_field_flag);
97     d =
98                     (u1_nal_ref_idc == 0 && ps_prev_slice->u1_nal_ref_idc != 0)
99                                     || (u1_nal_ref_idc != 0
100                                                     && ps_prev_slice->u1_nal_ref_idc
101                                                                     == 0);
102     if(!a)
103     {
104         if((u1_pic_order_cnt_type == 0)
105                         && (ps_prev_slice->u1_pic_order_cnt_type == 0))
106         {
107             e =
108                             ((ps_cur_poc->i4_pic_order_cnt_lsb
109                                             != ps_prev_poc->i4_pic_order_cnt_lsb)
110                                             || (ps_cur_poc->i4_delta_pic_order_cnt_bottom
111                                                             != ps_prev_poc->i4_delta_pic_order_cnt_bottom));
112         }
113 
114         if((u1_pic_order_cnt_type == 1)
115                         && (ps_prev_slice->u1_pic_order_cnt_type == 1))
116         {
117             f =
118                             ((ps_cur_poc->i4_delta_pic_order_cnt[0]
119                                             != ps_prev_poc->i4_delta_pic_order_cnt[0])
120                                             || (ps_cur_poc->i4_delta_pic_order_cnt[1]
121                                                             != ps_prev_poc->i4_delta_pic_order_cnt[1]));
122         }
123     }
124 
125     if((u1_nal_unit_type == IDR_SLICE_NAL)
126                     && (ps_prev_slice->u1_nal_unit_type == IDR_SLICE_NAL))
127     {
128         g = (u4_idr_pic_id != ps_prev_slice->u4_idr_pic_id);
129     }
130 
131     if((u1_nal_unit_type == IDR_SLICE_NAL)
132                     && (ps_prev_slice->u1_nal_unit_type != IDR_SLICE_NAL))
133     {
134         h = 1;
135     }
136     i1_is_end_of_pic = a + b + c + d + e + f + g + h;
137     return (i1_is_end_of_pic);
138 }
139 
140 /*!
141  **************************************************************************
142  * \if Function name : ih264d_decode_pic_order_cnt \endif
143  *
144  * \brief
145  *    Calculates picture order count of picture.
146  *
147  * \return
148  *    Returns the pic order count of the picture to which current
149  *    Slice belongs.
150  *
151  **************************************************************************
152  */
ih264d_decode_pic_order_cnt(UWORD8 u1_is_idr_slice,UWORD32 u2_frame_num,pocstruct_t * ps_prev_poc,pocstruct_t * ps_cur_poc,dec_slice_params_t * ps_cur_slice,dec_pic_params_t * ps_pps,UWORD8 u1_nal_ref_idc,UWORD8 u1_bottom_field_flag,UWORD8 u1_field_pic_flag,WORD32 * pi4_poc)153 WORD32 ih264d_decode_pic_order_cnt(UWORD8 u1_is_idr_slice,
154                                    UWORD32 u2_frame_num,
155                                    pocstruct_t *ps_prev_poc,
156                                    pocstruct_t *ps_cur_poc,
157                                    dec_slice_params_t *ps_cur_slice, /*!< Pointer to current slice Params*/
158                                    dec_pic_params_t * ps_pps,
159                                    UWORD8 u1_nal_ref_idc,
160                                    UWORD8 u1_bottom_field_flag,
161                                    UWORD8 u1_field_pic_flag,
162                                    WORD32 *pi4_poc)
163 {
164     WORD16 i1_pic_msb;
165     WORD32 i4_top_field_order_cnt = 0, i4_bottom_field_order_cnt = 0;
166     dec_seq_params_t *ps_seq = ps_pps->ps_sps;
167     WORD32 i4_prev_frame_num_ofst;
168 
169     switch(ps_seq->u1_pic_order_cnt_type)
170     {
171         case 0:
172             /* POC TYPE 0 */
173             if(u1_is_idr_slice)
174             {
175                 ps_prev_poc->i4_pic_order_cnt_msb = 0;
176                 ps_prev_poc->i4_pic_order_cnt_lsb = 0;
177             }
178             if(ps_prev_poc->u1_mmco_equalto5)
179             {
180                 if(ps_prev_poc->u1_bot_field != 1)
181                 {
182                     ps_prev_poc->i4_pic_order_cnt_msb = 0;
183                     ps_prev_poc->i4_pic_order_cnt_lsb =
184                                     ps_prev_poc->i4_top_field_order_count;
185                 }
186                 else
187                 {
188                     ps_prev_poc->i4_pic_order_cnt_msb = 0;
189                     ps_prev_poc->i4_pic_order_cnt_lsb = 0;
190                 }
191             }
192 
193             if((ps_cur_poc->i4_pic_order_cnt_lsb
194                             < ps_prev_poc->i4_pic_order_cnt_lsb)
195                             && ((ps_prev_poc->i4_pic_order_cnt_lsb
196                                             - ps_cur_poc->i4_pic_order_cnt_lsb)
197                                             >= (ps_seq->i4_max_pic_order_cntLsb
198                                                             >> 1)))
199             {
200                 i1_pic_msb = ps_prev_poc->i4_pic_order_cnt_msb
201                                 + ps_seq->i4_max_pic_order_cntLsb;
202             }
203             else if((ps_cur_poc->i4_pic_order_cnt_lsb
204                             > ps_prev_poc->i4_pic_order_cnt_lsb)
205                             && ((ps_cur_poc->i4_pic_order_cnt_lsb
206                                             - ps_prev_poc->i4_pic_order_cnt_lsb)
207                                             >= (ps_seq->i4_max_pic_order_cntLsb
208                                                             >> 1)))
209             {
210                 i1_pic_msb = ps_prev_poc->i4_pic_order_cnt_msb
211                                 - ps_seq->i4_max_pic_order_cntLsb;
212             }
213             else
214             {
215                 i1_pic_msb = ps_prev_poc->i4_pic_order_cnt_msb;
216             }
217 
218             if(!u1_field_pic_flag || !u1_bottom_field_flag)
219                 i4_top_field_order_cnt = i1_pic_msb
220                                 + ps_cur_poc->i4_pic_order_cnt_lsb;
221 
222             if(!u1_field_pic_flag)
223             {
224                 i4_bottom_field_order_cnt = i4_top_field_order_cnt
225                                 + ps_cur_poc->i4_delta_pic_order_cnt_bottom;
226             }
227             else if(u1_bottom_field_flag)
228             {
229                 i4_bottom_field_order_cnt = i1_pic_msb
230                                 + ps_cur_poc->i4_pic_order_cnt_lsb;
231             }
232             ps_cur_poc->i4_pic_order_cnt_msb = i1_pic_msb;
233             break;
234 
235         case 1:
236         {
237             /* POC TYPE 1 */
238             UWORD8 i;
239             WORD32 prev_frame_num;
240             WORD32 frame_num_ofst;
241             WORD32 abs_frm_num;
242             WORD32 poc_cycle_cnt, frame_num_in_poc_cycle;
243             WORD64 i8_expected_delta_poc_cycle;
244             WORD32 expected_poc;
245             WORD64 i8_result;
246 
247             prev_frame_num = (WORD32)ps_cur_slice->u2_frame_num;
248             if(!u1_is_idr_slice)
249             {
250                 if(ps_cur_slice->u1_mmco_equalto5)
251                 {
252                     prev_frame_num = 0;
253                     i4_prev_frame_num_ofst = 0;
254                 }
255                 else
256                 {
257                     i4_prev_frame_num_ofst = ps_prev_poc->i4_prev_frame_num_ofst;
258                 }
259             }
260             else
261                 i4_prev_frame_num_ofst = 0;
262 
263             /* 1. Derivation for FrameNumOffset */
264             if(u1_is_idr_slice)
265             {
266                 frame_num_ofst = 0;
267                 ps_cur_poc->i4_delta_pic_order_cnt[0] = 0;
268                 ps_cur_poc->i4_delta_pic_order_cnt[1] = 0;
269             }
270             else if(prev_frame_num > ((WORD32)u2_frame_num))
271             {
272                 frame_num_ofst = i4_prev_frame_num_ofst
273                                 + (WORD32)ps_seq->u2_u4_max_pic_num_minus1 + 1;
274             }
275             else
276                 frame_num_ofst = i4_prev_frame_num_ofst;
277 
278             /* 2. Derivation for absFrameNum */
279             if(0 != ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle)
280                 abs_frm_num = frame_num_ofst + (WORD32)u2_frame_num;
281             else
282                 abs_frm_num = 0;
283             if((u1_nal_ref_idc == 0) && (abs_frm_num > 0))
284                 abs_frm_num = abs_frm_num - 1;
285 
286             /* 4. expectedDeltaPerPicOrderCntCycle is derived as */
287             i8_expected_delta_poc_cycle = 0;
288             for(i = 0; i < ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle;
289                             i++)
290             {
291                 i8_expected_delta_poc_cycle +=
292                                 ps_seq->i4_ofst_for_ref_frame[i];
293             }
294 
295             /* 3. When absFrameNum > 0, picOrderCntCycleCnt and
296              frame_num_in_poc_cycle are derived as : */
297             /* 5. expectedPicOrderCnt is derived as : */
298             if(abs_frm_num > 0)
299             {
300                 poc_cycle_cnt =
301                                 DIV((abs_frm_num - 1),
302                                     ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);
303                 frame_num_in_poc_cycle =
304                                 MOD((abs_frm_num - 1),
305                                     ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);
306 
307                 i8_result = poc_cycle_cnt
308                                 * i8_expected_delta_poc_cycle;
309 
310                 for(i = 0; i <= frame_num_in_poc_cycle; i++)
311                 {
312                     i8_result = i8_result
313                                     + ps_seq->i4_ofst_for_ref_frame[i];
314                 }
315 
316                 if(IS_OUT_OF_RANGE_S32(i8_result))
317                     return ERROR_INV_POC;
318 
319                 expected_poc = (WORD32)i8_result;
320 
321             }
322             else
323                 expected_poc = 0;
324 
325             if(u1_nal_ref_idc == 0)
326             {
327                 i8_result = expected_poc
328                                 + ps_seq->i4_ofst_for_non_ref_pic;
329 
330                 if(IS_OUT_OF_RANGE_S32(i8_result))
331                     return ERROR_INV_POC;
332 
333                 expected_poc = (WORD32)i8_result;
334             }
335 
336             /* 6. TopFieldOrderCnt or BottomFieldOrderCnt are derived as */
337             if(!u1_field_pic_flag)
338             {
339                 i8_result = expected_poc
340                                 + ps_cur_poc->i4_delta_pic_order_cnt[0];
341 
342                 if(IS_OUT_OF_RANGE_S32(i8_result))
343                     return ERROR_INV_POC;
344                 i4_top_field_order_cnt = (WORD32)i8_result;
345 
346                 i8_result = i4_top_field_order_cnt
347                                 + ps_seq->i4_ofst_for_top_to_bottom_field
348                                 + ps_cur_poc->i4_delta_pic_order_cnt[1];
349 
350                 if(IS_OUT_OF_RANGE_S32(i8_result))
351                     return ERROR_INV_POC;
352                 i4_bottom_field_order_cnt = (WORD32)i8_result;
353             }
354             else if(!u1_bottom_field_flag)
355             {
356                 i8_result = expected_poc
357                                 + ps_cur_poc->i4_delta_pic_order_cnt[0];
358 
359                 if(IS_OUT_OF_RANGE_S32(i8_result))
360                     return ERROR_INV_POC;
361                 i4_top_field_order_cnt = (WORD32)i8_result;
362             }
363             else
364             {
365                 i8_result = expected_poc
366                                 + ps_seq->i4_ofst_for_top_to_bottom_field
367                                 + ps_cur_poc->i4_delta_pic_order_cnt[0];
368 
369                 if(IS_OUT_OF_RANGE_S32(i8_result))
370                     return ERROR_INV_POC;
371                 i4_bottom_field_order_cnt = (WORD32)i8_result;
372             }
373             /* Copy the current POC info into Previous POC structure */
374             ps_cur_poc->i4_prev_frame_num_ofst = frame_num_ofst;
375         }
376 
377             break;
378         case 2:
379         {
380             /* POC TYPE 2 */
381             WORD32 prev_frame_num;
382             WORD32 frame_num_ofst;
383             WORD32 tmp_poc;
384 
385             prev_frame_num = (WORD32)ps_cur_slice->u2_frame_num;
386             if(!u1_is_idr_slice)
387             {
388                 if(ps_cur_slice->u1_mmco_equalto5)
389                 {
390                     prev_frame_num = 0;
391                     i4_prev_frame_num_ofst = 0;
392                 }
393                 else
394                     i4_prev_frame_num_ofst = ps_prev_poc->i4_prev_frame_num_ofst;
395             }
396             else
397                 i4_prev_frame_num_ofst = 0;
398 
399             /* 1. Derivation for FrameNumOffset */
400             if(u1_is_idr_slice)
401             {
402                 frame_num_ofst = 0;
403                 ps_cur_poc->i4_delta_pic_order_cnt[0] = 0;
404                 ps_cur_poc->i4_delta_pic_order_cnt[1] = 0;
405             }
406             else if(prev_frame_num > ((WORD32)u2_frame_num))
407             {
408                 frame_num_ofst = i4_prev_frame_num_ofst
409                                 + (WORD32)ps_seq->u2_u4_max_pic_num_minus1 + 1;
410             }
411             else
412                 frame_num_ofst = i4_prev_frame_num_ofst;
413 
414             /* 2. Derivation for tempPicOrderCnt */
415             if(u1_is_idr_slice)
416                 tmp_poc = 0;
417             else if(u1_nal_ref_idc == 0)
418                 tmp_poc = ((frame_num_ofst + (WORD32)u2_frame_num) << 1)
419                                 - 1;
420             else
421                 tmp_poc = ((frame_num_ofst + (WORD32)u2_frame_num) << 1);
422 
423             /* 6. TopFieldOrderCnt or BottomFieldOrderCnt are derived as */
424             if(!u1_field_pic_flag)
425             {
426                 i4_top_field_order_cnt = tmp_poc;
427                 i4_bottom_field_order_cnt = tmp_poc;
428             }
429             else if(!u1_bottom_field_flag)
430                 i4_top_field_order_cnt = tmp_poc;
431             else
432                 i4_bottom_field_order_cnt = tmp_poc;
433 
434             /* Copy the current POC info into Previous POC structure */
435             ps_prev_poc->i4_prev_frame_num_ofst = frame_num_ofst;
436             ps_cur_poc->i4_prev_frame_num_ofst = frame_num_ofst;
437         }
438             break;
439         default:
440             return ERROR_INV_POC_TYPE_T;
441             break;
442     }
443 
444     if(!u1_field_pic_flag) // or a complementary field pair
445     {
446         *pi4_poc = MIN(i4_top_field_order_cnt, i4_bottom_field_order_cnt);
447         ps_pps->i4_top_field_order_cnt = i4_top_field_order_cnt;
448         ps_pps->i4_bottom_field_order_cnt = i4_bottom_field_order_cnt;
449     }
450     else if(!u1_bottom_field_flag)
451     {
452         *pi4_poc = i4_top_field_order_cnt;
453         ps_pps->i4_top_field_order_cnt = i4_top_field_order_cnt;
454     }
455     else
456     {
457         *pi4_poc = i4_bottom_field_order_cnt;
458         ps_pps->i4_bottom_field_order_cnt = i4_bottom_field_order_cnt;
459     }
460 
461     ps_pps->i4_avg_poc = *pi4_poc;
462 
463     return OK;
464 }
465 
466 /*!
467  **************************************************************************
468  * \if Function name : ih264d_end_of_pic_processing \endif
469  *
470  * \brief
471  *    Performs the end of picture processing.
472  *
473  * It performs deblocking on the current picture and sets the i4_status of
474  * current picture as decoded.
475  *
476  * \return
477  *    0 on Success and Error code otherwise.
478  **************************************************************************
479  */
ih264d_end_of_pic_processing(dec_struct_t * ps_dec)480 WORD32 ih264d_end_of_pic_processing(dec_struct_t *ps_dec)
481 {
482     UWORD8 u1_pic_type, u1_nal_ref_idc;
483     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
484 
485     /* If nal_ref_idc is equal to 0 for one slice or slice data partition NAL
486      unit of a particular picture, it shall be equal to 0 for all slice and
487      slice data partition NAL units of the picture. nal_ref_idc greater
488      than 0 indicates that the content of the NAL unit belongs to a decoded
489      picture that is stored and marked for use as a reference picture in the
490      decoded picture buffer. */
491 
492     /* 1. Do MMCO
493      2. Add Cur Pic to list of reference pics.
494      */
495 
496     /* Call MMCO */
497     u1_pic_type = 0;
498     u1_nal_ref_idc = ps_cur_slice->u1_nal_ref_idc;
499 
500     if(u1_nal_ref_idc)
501     {
502         if(ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL)
503         {
504             if(ps_dec->ps_dpb_cmds->u1_long_term_reference_flag == 0)
505             {
506                 ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
507                 /* ignore DPB errors */
508                 ih264d_insert_st_node(ps_dec->ps_dpb_mgr,
509                                       ps_dec->ps_cur_pic,
510                                       ps_dec->u1_pic_buf_id,
511                                       ps_cur_slice->u2_frame_num);
512             }
513             else
514             {
515                 /* Equivalent of inserting a pic directly as longterm Pic */
516 
517                 {
518                     /* ignore DPB errors */
519                     ih264d_insert_st_node(ps_dec->ps_dpb_mgr,
520                                           ps_dec->ps_cur_pic,
521                                           ps_dec->u1_pic_buf_id,
522                                           ps_cur_slice->u2_frame_num);
523 
524                     /* Set longTermIdx = 0, MaxLongTermFrameIdx = 0 */
525                     ih264d_delete_st_node_or_make_lt(
526                                     ps_dec->ps_dpb_mgr,
527                                     ps_cur_slice->u2_frame_num, 0,
528                                     ps_cur_slice->u1_field_pic_flag);
529 
530                     ps_dec->ps_dpb_mgr->u1_max_lt_pic_idx_plus1 = 1;
531                 }
532             }
533         }
534         else
535         {
536 
537             {
538                 UWORD16 u2_pic_num = ps_cur_slice->u2_frame_num;
539 
540                 /* ignore DPB errors */
541                 ih264d_do_mmco_buffer(ps_dec->ps_dpb_cmds, ps_dec->ps_dpb_mgr,
542                               ps_dec->ps_cur_sps->u1_num_ref_frames, u2_pic_num,
543                               (ps_dec->ps_cur_sps->u2_u4_max_pic_num_minus1),
544                               ps_dec->u1_nal_unit_type, ps_dec->ps_cur_pic,
545                               ps_dec->u1_pic_buf_id,
546                               ps_cur_slice->u1_field_pic_flag,
547                               ps_dec->e_dec_status);
548 
549 
550             }
551         }
552         ih264d_update_default_index_list(ps_dec->ps_dpb_mgr);
553     }
554 
555     if(ps_cur_slice->u1_field_pic_flag)
556     {
557         if(ps_cur_slice->u1_bottom_field_flag)
558         {
559             if(u1_nal_ref_idc)
560                 u1_pic_type = u1_pic_type | BOT_REF;
561             u1_pic_type = u1_pic_type | BOT_FLD;
562         }
563         else
564         {
565             if(u1_nal_ref_idc)
566                 u1_pic_type = u1_pic_type | TOP_REF;
567             u1_pic_type = u1_pic_type | TOP_FLD;
568         }
569     }
570     else
571         u1_pic_type = TOP_REF | BOT_REF;
572     ps_dec->ps_cur_pic->u1_pic_type |= u1_pic_type;
573 
574 
575     if(ps_cur_slice->u1_field_pic_flag)
576     {
577         H264_DEC_DEBUG_PRINT("Toggling secondField\n");
578         ps_dec->u1_second_field = 1 - ps_dec->u1_second_field;
579     }
580 
581     return OK;
582 }
583 
584 /*****************************************************************************/
585 /*                                                                           */
586 /*  Function Name : init_dpb_size                                            */
587 /*                                                                           */
588 /*  Description   : This function calculates the DBP i4_size in frames          */
589 /*  Inputs        : ps_seq - current sequence params                         */
590 /*                                                                           */
591 /*  Globals       : None                                                     */
592 /*                                                                           */
593 /*  Outputs       : None                                                     */
594 /*                                                                           */
595 /*  Returns       : DPB in frames                                            */
596 /*                                                                           */
597 /*  Issues        : None                                                     */
598 /*                                                                           */
599 /*  Revision History:                                                        */
600 /*                                                                           */
601 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
602 /*         28 04 2005   NS              Draft                                */
603 /*                                                                           */
604 /*****************************************************************************/
ih264d_get_dpb_size(dec_seq_params_t * ps_seq)605 WORD32 ih264d_get_dpb_size(dec_seq_params_t *ps_seq)
606 {
607     WORD32 i4_size;
608     UWORD8 u1_level_idc;
609 
610     u1_level_idc = ps_seq->u1_level_idc;
611 
612     switch(u1_level_idc)
613     {
614         case 10:
615             i4_size = 152064;
616             break;
617         case 11:
618             i4_size = 345600;
619             break;
620         case 12:
621             i4_size = 912384;
622             break;
623         case 13:
624             i4_size = 912384;
625             break;
626         case 20:
627             i4_size = 912384;
628             break;
629         case 21:
630             i4_size = 1824768;
631             break;
632         case 22:
633             i4_size = 3110400;
634             break;
635         case 30:
636             i4_size = 3110400;
637             break;
638         case 31:
639             i4_size = 6912000;
640             break;
641         case 32:
642             i4_size = 7864320;
643             break;
644         case 40:
645             i4_size = 12582912;
646             break;
647         case 41:
648             i4_size = 12582912;
649             break;
650         case 42:
651             i4_size = 12582912;
652             break;
653         case 50:
654             i4_size = 42393600;
655             break;
656         case 51:
657             i4_size = 70778880;
658             break;
659         case 52:
660             i4_size = 70778880;
661             break;
662         default:
663             i4_size = 70778880;
664             break;
665     }
666 
667     i4_size /= (ps_seq->u2_frm_wd_in_mbs * (ps_seq->u2_frm_ht_in_mbs << (1 - ps_seq->u1_frame_mbs_only_flag)));
668     i4_size /= 384;
669     i4_size = MIN(i4_size, 16);
670     i4_size = MAX(i4_size, 1);
671     return (i4_size);
672 }
673 
674 /**************************************************************************/
675 /* This function initialises the value of ps_dec->u1_recon_mb_grp         */
676 /* ps_dec->u1_recon_mb_grp must satisfy the following criteria            */
677 /*  - multiple of 2 (required for N/2 parse-mvpred design)                */
678 /*  - multiple of 4 (if it is not a frame_mbs_only sequence),             */
679 /*         in this case N/2 itself needs to be even for mbpair processing */
680 /*  - lesser than ps_dec->u2_frm_wd_in_mbs/2 (at least 3 N-Chunks       */
681 /*         should make a row to ensure proper MvTop transferring)         */
682 /**************************************************************************/
ih264d_init_dec_mb_grp(dec_struct_t * ps_dec)683 WORD32 ih264d_init_dec_mb_grp(dec_struct_t *ps_dec)
684 {
685     dec_seq_params_t *ps_seq = ps_dec->ps_cur_sps;
686     UWORD8 u1_frm = ps_seq->u1_frame_mbs_only_flag;
687 
688     ps_dec->u1_recon_mb_grp = ps_dec->u2_frm_wd_in_mbs << ps_seq->u1_mb_aff_flag;
689 
690     ps_dec->u1_recon_mb_grp_pair = ps_dec->u1_recon_mb_grp >> 1;
691 
692     if(!ps_dec->u1_recon_mb_grp)
693     {
694         return ERROR_MB_GROUP_ASSGN_T;
695     }
696 
697     ps_dec->u4_num_mbs_prev_nmb = ps_dec->u1_recon_mb_grp;
698 
699     return OK;
700 }
701 
702 
703 /*!
704  **************************************************************************
705  * \if Function name : ih264d_init_pic \endif
706  *
707  * \brief
708  *    Initializes the picture.
709  *
710  * \return
711  *    0 on Success and Error code otherwise
712  *
713  * \note
714  *    This function is called when first slice of the
715  *    NON -IDR picture is encountered.
716  **************************************************************************
717  */
ih264d_init_pic(dec_struct_t * ps_dec,UWORD16 u2_frame_num,WORD32 i4_poc,dec_pic_params_t * ps_pps)718 WORD32 ih264d_init_pic(dec_struct_t *ps_dec,
719                        UWORD16 u2_frame_num,
720                        WORD32 i4_poc,
721                        dec_pic_params_t *ps_pps)
722 {
723     dec_seq_params_t *ps_seq = ps_pps->ps_sps;
724     prev_seq_params_t * ps_prev_seq_params = &ps_dec->s_prev_seq_params;
725     WORD32 i4_pic_bufs;
726     WORD32 ret;
727 
728     ps_dec->ps_cur_slice->u2_frame_num = u2_frame_num;
729     ps_dec->ps_cur_slice->i4_poc = i4_poc;
730     ps_dec->ps_cur_pps = ps_pps;
731     ps_dec->ps_cur_pps->pv_codec_handle = ps_dec;
732 
733     ps_dec->ps_cur_sps = ps_seq;
734     ps_dec->ps_dpb_mgr->i4_max_frm_num = ps_seq->u2_u4_max_pic_num_minus1
735                     + 1;
736 
737     ps_dec->ps_dpb_mgr->u2_pic_ht = ps_dec->u2_pic_ht;
738     ps_dec->ps_dpb_mgr->u2_pic_wd = ps_dec->u2_pic_wd;
739     ps_dec->i4_pic_type = NA_SLICE;
740     ps_dec->i4_frametype = IV_NA_FRAME;
741     ps_dec->i4_content_type = IV_CONTENTTYPE_NA;
742 
743     /*--------------------------------------------------------------------*/
744     /* Get the value of MaxMbAddress and frmheight in Mbs                 */
745     /*--------------------------------------------------------------------*/
746     ps_seq->u2_max_mb_addr =
747                     (ps_seq->u2_frm_wd_in_mbs
748                                     * (ps_dec->u2_pic_ht
749                                                     >> (4
750                                                                     + ps_dec->ps_cur_slice->u1_field_pic_flag)))
751                                     - 1;
752     ps_dec->u2_frm_ht_in_mbs = (ps_dec->u2_pic_ht
753                     >> (4 + ps_dec->ps_cur_slice->u1_field_pic_flag));
754 
755     /***************************************************************************/
756     /* If change in Level or the required PicBuffers i4_size is more than the  */
757     /* current one FREE the current PicBuffers and allocate affresh            */
758     /***************************************************************************/
759     if(!ps_dec->u1_init_dec_flag)
760     {
761         ps_dec->u1_max_dec_frame_buffering = ih264d_get_dpb_size(ps_seq);
762 
763         ps_dec->i4_display_delay = ps_dec->u1_max_dec_frame_buffering;
764         if((1 == ps_seq->u1_vui_parameters_present_flag) &&
765            (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
766         {
767             if(ps_seq->u1_frame_mbs_only_flag == 1)
768                 ps_dec->i4_display_delay = ps_seq->s_vui.u4_num_reorder_frames + 1;
769             else
770                 ps_dec->i4_display_delay = ps_seq->s_vui.u4_num_reorder_frames * 2 + 2;
771         }
772 
773         if(IVD_DECODE_FRAME_OUT == ps_dec->e_frm_out_mode)
774             ps_dec->i4_display_delay = 0;
775 
776         if(ps_dec->u4_share_disp_buf == 0)
777         {
778             if(ps_seq->u1_frame_mbs_only_flag == 1)
779                 ps_dec->u1_pic_bufs = ps_dec->i4_display_delay + ps_seq->u1_num_ref_frames + 1;
780             else
781                 ps_dec->u1_pic_bufs = ps_dec->i4_display_delay + ps_seq->u1_num_ref_frames * 2 + 2;
782         }
783         else
784         {
785             ps_dec->u1_pic_bufs = (WORD32)ps_dec->u4_num_disp_bufs;
786         }
787 
788         /* Ensure at least two buffers are allocated */
789         ps_dec->u1_pic_bufs = MAX(ps_dec->u1_pic_bufs, 2);
790 
791         if(ps_dec->u4_share_disp_buf == 0)
792             ps_dec->u1_pic_bufs = MIN(ps_dec->u1_pic_bufs,
793                                       (H264_MAX_REF_PICS * 2));
794 
795         ps_dec->u1_max_dec_frame_buffering = MIN(
796                         ps_dec->u1_max_dec_frame_buffering,
797                         ps_dec->u1_pic_bufs);
798 
799         /* Temporary hack to run Tractor Cav/Cab/MbAff Profiler streams  also for CAFI1_SVA_C.264 in conformance*/
800         if(ps_dec->u1_init_dec_flag)
801         {
802             ih264d_release_pics_in_dpb((void *)ps_dec,
803                                        ps_dec->u1_pic_bufs);
804             ih264d_release_display_bufs(ps_dec);
805             ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
806         }
807 
808         /*********************************************************************/
809         /* Configuring decoder parameters based on level and then            */
810         /* fresh pointer initialisation in decoder scratch and state buffers */
811         /*********************************************************************/
812         if(!ps_dec->u1_init_dec_flag ||
813                 ((ps_seq->u1_level_idc < H264_LEVEL_3_0) ^ (ps_prev_seq_params->u1_level_idc < H264_LEVEL_3_0)))
814         {
815             ret = ih264d_init_dec_mb_grp(ps_dec);
816             if(ret != OK)
817                 return ret;
818         }
819 
820         ret = ih264d_allocate_dynamic_bufs(ps_dec);
821         if(ret != OK)
822         {
823             /* Free any dynamic buffers that are allocated */
824             ih264d_free_dynamic_bufs(ps_dec);
825             ps_dec->i4_error_code = IVD_MEM_ALLOC_FAILED;
826             return IVD_MEM_ALLOC_FAILED;
827         }
828 
829         ret = ih264d_create_pic_buffers(ps_dec->u1_pic_bufs,
830                                         ps_dec);
831         if(ret != OK)
832             return ret;
833 
834 
835 
836         ret = ih264d_create_mv_bank(ps_dec, ps_dec->u2_pic_wd,
837                                     ps_dec->u2_pic_ht);
838         if(ret != OK)
839             return ret;
840 
841         /* In shared mode, set all of them as used by display */
842         if(ps_dec->u4_share_disp_buf == 1)
843         {
844             WORD32 i;
845 
846             for(i = 0; i < ps_dec->u1_pic_bufs; i++)
847             {
848                 ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mgr, i,
849                                          BUF_MGR_IO);
850             }
851         }
852 
853         ps_dec->u1_init_dec_flag = 1;
854         ps_prev_seq_params->u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
855         ps_prev_seq_params->u1_level_idc = ps_seq->u1_level_idc;
856         ps_prev_seq_params->u1_profile_idc = ps_seq->u1_profile_idc;
857         ps_prev_seq_params->u2_frm_ht_in_mbs = ps_seq->u2_frm_ht_in_mbs;
858         ps_prev_seq_params->u1_frame_mbs_only_flag =
859                         ps_seq->u1_frame_mbs_only_flag;
860         ps_prev_seq_params->u1_direct_8x8_inference_flag =
861                         ps_seq->u1_direct_8x8_inference_flag;
862 
863         ps_dec->i4_cur_display_seq = 0;
864         ps_dec->i4_prev_max_display_seq = 0;
865         ps_dec->i4_max_poc = 0;
866 
867         {
868             /* 0th entry of CtxtIncMbMap will be always be containing default values
869              for CABAC context representing MB not available */
870             ctxt_inc_mb_info_t *p_DefCtxt = ps_dec->p_ctxt_inc_mb_map - 1;
871             UWORD8 *pu1_temp;
872             WORD8 i;
873             p_DefCtxt->u1_mb_type = CAB_SKIP;
874 
875             p_DefCtxt->u1_cbp = 0x0f;
876             p_DefCtxt->u1_intra_chroma_pred_mode = 0;
877 
878             p_DefCtxt->u1_yuv_dc_csbp = 0x7;
879 
880             p_DefCtxt->u1_transform8x8_ctxt = 0;
881 
882             pu1_temp = (UWORD8*)p_DefCtxt->i1_ref_idx;
883             for(i = 0; i < 4; i++, pu1_temp++)
884                 (*pu1_temp) = 0;
885             pu1_temp = (UWORD8*)p_DefCtxt->u1_mv;
886             for(i = 0; i < 16; i++, pu1_temp++)
887                 (*pu1_temp) = 0;
888             ps_dec->ps_def_ctxt_mb_info = p_DefCtxt;
889         }
890 
891     }
892     /* reset DBP commands read u4_flag */
893     ps_dec->ps_dpb_cmds->u1_dpb_commands_read = 0;
894 
895     return OK;
896 }
897 
898 /*****************************************************************************/
899 /*                                                                           */
900 /*  Function Name : ih264d_get_next_display_field                                   */
901 /*                                                                           */
902 /*  Description   : Application calls this module to get the next field      */
903 /*                  to be displayed                                          */
904 /*                                                                           */
905 /*  Inputs        : 1.   IBUFAPI_Handle Hnadle to the Display buffer         */
906 /*                  2.   IH264DEC_DispUnit    Pointer to the display struct  */
907 /*                                                                           */
908 /*  Globals       :                                                          */
909 /*                                                                           */
910 /*                                                                           */
911 /*  Processing    : None                                                     */
912 /*  Outputs       : None                                                     */
913 /*  Returns       : None                                                     */
914 /*  Issues        : None                                                     */
915 /*                                                                           */
916 /*  Revision History:                                                        */
917 /*                                                                           */
918 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
919 /*         27 05 2005   Ittiam          Draft                                */
920 /*                                                                           */
921 /*****************************************************************************/
922 
ih264d_get_next_display_field(dec_struct_t * ps_dec,ivd_out_bufdesc_t * ps_out_buffer,ivd_get_display_frame_op_t * pv_disp_op)923 WORD32 ih264d_get_next_display_field(dec_struct_t * ps_dec,
924                                   ivd_out_bufdesc_t *ps_out_buffer,
925                                   ivd_get_display_frame_op_t *pv_disp_op)
926 {
927     pic_buffer_t *pic_buf;
928 
929     UWORD8 i1_cur_fld;
930     WORD32 u4_api_ret = -1;
931     WORD32 i4_disp_buf_id;
932     iv_yuv_buf_t *ps_op_frm;
933 
934 
935 
936     ps_op_frm = &(ps_dec->s_disp_frame_info);
937     H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
938     pic_buf = (pic_buffer_t *)ih264_disp_mgr_get(
939                     (disp_mgr_t *)ps_dec->pv_disp_buf_mgr, &i4_disp_buf_id);
940     ps_dec->u4_num_fld_in_frm = 0;
941     u4_api_ret = -1;
942     pv_disp_op->u4_ts = 0;
943     pv_disp_op->e_output_format = ps_dec->u1_chroma_format;
944 
945     pv_disp_op->s_disp_frm_buf.pv_y_buf = ps_out_buffer->pu1_bufs[0];
946     pv_disp_op->s_disp_frm_buf.pv_u_buf = ps_out_buffer->pu1_bufs[1];
947     pv_disp_op->s_disp_frm_buf.pv_v_buf = ps_out_buffer->pu1_bufs[2];
948     if(pic_buf != NULL)
949     {
950         pv_disp_op->e4_fld_type = 0;
951         pv_disp_op->u4_disp_buf_id = i4_disp_buf_id;
952 
953         ps_op_frm->u4_y_ht = pic_buf->u2_disp_height << 1;
954         ps_op_frm->u4_u_ht = ps_op_frm->u4_v_ht = ps_op_frm->u4_y_ht >> 1;
955         ps_op_frm->u4_y_wd = pic_buf->u2_disp_width;
956 
957         ps_op_frm->u4_u_wd = ps_op_frm->u4_v_wd = ps_op_frm->u4_y_wd >> 1;
958 
959         ps_op_frm->u4_y_strd = pic_buf->u2_frm_wd_y;
960         ps_op_frm->u4_u_strd = ps_op_frm->u4_v_strd = pic_buf->u2_frm_wd_uv;
961 
962         /* ! */
963         pv_disp_op->u4_ts = pic_buf->u4_ts;
964 
965         /* set the start of the Y, U and V buffer pointer for display    */
966         ps_op_frm->pv_y_buf = pic_buf->pu1_buf1 + pic_buf->u2_crop_offset_y;
967         ps_op_frm->pv_u_buf = pic_buf->pu1_buf2 + pic_buf->u2_crop_offset_uv;
968         ps_op_frm->pv_v_buf = pic_buf->pu1_buf3 + pic_buf->u2_crop_offset_uv;
969         ps_dec->u4_num_fld_in_frm++;
970         ps_dec->u4_num_fld_in_frm++;
971         u4_api_ret = 0;
972 
973         if(pic_buf->u1_picturetype == 0)
974             pv_disp_op->u4_progressive_frame_flag = 1;
975         else
976             pv_disp_op->u4_progressive_frame_flag = 0;
977 
978     } H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
979     pv_disp_op->u4_error_code = u4_api_ret;
980     pv_disp_op->e_pic_type = 0xFFFFFFFF; //Junk;
981 
982     if(u4_api_ret)
983     {
984         pv_disp_op->u4_error_code = 1; //put a proper error code here
985     }
986     else
987     {
988 
989         //Release the buffer if being sent for display
990         UWORD32 temp;
991         UWORD32 dest_inc_Y = 0, dest_inc_UV = 0;
992 
993         pv_disp_op->s_disp_frm_buf.u4_y_wd = temp = MIN(ps_op_frm->u4_y_wd,
994                                                         ps_op_frm->u4_y_strd);
995         pv_disp_op->s_disp_frm_buf.u4_u_wd = pv_disp_op->s_disp_frm_buf.u4_y_wd
996                         >> 1;
997         pv_disp_op->s_disp_frm_buf.u4_v_wd = pv_disp_op->s_disp_frm_buf.u4_y_wd
998                         >> 1;
999 
1000         pv_disp_op->s_disp_frm_buf.u4_y_ht = ps_op_frm->u4_y_ht;
1001         pv_disp_op->s_disp_frm_buf.u4_u_ht = pv_disp_op->s_disp_frm_buf.u4_y_ht
1002                         >> 1;
1003         pv_disp_op->s_disp_frm_buf.u4_v_ht = pv_disp_op->s_disp_frm_buf.u4_y_ht
1004                         >> 1;
1005         if(0 == ps_dec->u4_share_disp_buf)
1006         {
1007             pv_disp_op->s_disp_frm_buf.u4_y_strd =
1008                             pv_disp_op->s_disp_frm_buf.u4_y_wd;
1009             pv_disp_op->s_disp_frm_buf.u4_u_strd =
1010                             pv_disp_op->s_disp_frm_buf.u4_y_wd >> 1;
1011             pv_disp_op->s_disp_frm_buf.u4_v_strd =
1012                             pv_disp_op->s_disp_frm_buf.u4_y_wd >> 1;
1013 
1014         }
1015         else
1016         {
1017             pv_disp_op->s_disp_frm_buf.u4_y_strd = ps_op_frm->u4_y_strd;
1018         }
1019 
1020         if(ps_dec->u4_app_disp_width)
1021         {
1022             pv_disp_op->s_disp_frm_buf.u4_y_strd = MAX(
1023                             ps_dec->u4_app_disp_width,
1024                             pv_disp_op->s_disp_frm_buf.u4_y_strd);
1025         }
1026 
1027         pv_disp_op->u4_error_code = 0;
1028         if(pv_disp_op->e_output_format == IV_YUV_420P)
1029         {
1030             UWORD32 i;
1031             pv_disp_op->s_disp_frm_buf.u4_u_strd =
1032                             pv_disp_op->s_disp_frm_buf.u4_y_strd >> 1;
1033             pv_disp_op->s_disp_frm_buf.u4_v_strd =
1034                             pv_disp_op->s_disp_frm_buf.u4_y_strd >> 1;
1035 
1036             pv_disp_op->s_disp_frm_buf.u4_u_wd = ps_op_frm->u4_y_wd >> 1;
1037             pv_disp_op->s_disp_frm_buf.u4_v_wd = ps_op_frm->u4_y_wd >> 1;
1038 
1039             if(1 == ps_dec->u4_share_disp_buf)
1040             {
1041                 pv_disp_op->s_disp_frm_buf.pv_y_buf = ps_op_frm->pv_y_buf;
1042 
1043                 for(i = 0; i < MAX_DISP_BUFS_NEW; i++)
1044                 {
1045                     UWORD8 *buf = ps_dec->disp_bufs[i].buf[0];
1046                     buf += ps_dec->disp_bufs[i].u4_ofst[0];
1047                     if(((UWORD8 *)pv_disp_op->s_disp_frm_buf.pv_y_buf
1048                                     - pic_buf->u2_crop_offset_y) == buf)
1049                     {
1050                         buf = ps_dec->disp_bufs[i].buf[1];
1051                         buf += ps_dec->disp_bufs[i].u4_ofst[1];
1052                         pv_disp_op->s_disp_frm_buf.pv_u_buf = buf
1053                                         + (pic_buf->u2_crop_offset_uv
1054                                            / YUV420SP_FACTOR);
1055 
1056                         buf = ps_dec->disp_bufs[i].buf[2];
1057                         buf += ps_dec->disp_bufs[i].u4_ofst[2];
1058                         pv_disp_op->s_disp_frm_buf.pv_v_buf = buf
1059                                         + (pic_buf->u2_crop_offset_uv
1060                                            / YUV420SP_FACTOR);
1061 
1062                     }
1063                 }
1064             }
1065 
1066         }
1067         else if((pv_disp_op->e_output_format == IV_YUV_420SP_UV)
1068                         || (pv_disp_op->e_output_format == IV_YUV_420SP_VU))
1069         {
1070             pv_disp_op->s_disp_frm_buf.u4_u_strd =
1071                             pv_disp_op->s_disp_frm_buf.u4_y_strd;
1072             pv_disp_op->s_disp_frm_buf.u4_v_strd = 0;
1073 
1074             if(1 == ps_dec->u4_share_disp_buf)
1075             {
1076                 UWORD32 i;
1077 
1078                 pv_disp_op->s_disp_frm_buf.pv_y_buf = ps_op_frm->pv_y_buf;
1079 
1080                 for(i = 0; i < MAX_DISP_BUFS_NEW; i++)
1081                 {
1082                     UWORD8 *buf = ps_dec->disp_bufs[i].buf[0];
1083                     buf += ps_dec->disp_bufs[i].u4_ofst[0];
1084                     if((UWORD8 *)pv_disp_op->s_disp_frm_buf.pv_y_buf
1085                                     - pic_buf->u2_crop_offset_y == buf)
1086                     {
1087                         buf = ps_dec->disp_bufs[i].buf[1];
1088                         buf += ps_dec->disp_bufs[i].u4_ofst[1];
1089                         pv_disp_op->s_disp_frm_buf.pv_u_buf = buf
1090                                         + pic_buf->u2_crop_offset_uv;
1091                         ;
1092 
1093                         buf = ps_dec->disp_bufs[i].buf[2];
1094                         buf += ps_dec->disp_bufs[i].u4_ofst[2];
1095                         pv_disp_op->s_disp_frm_buf.pv_v_buf = buf
1096                                         + pic_buf->u2_crop_offset_uv;
1097                         ;
1098                     }
1099                 }
1100             }
1101             pv_disp_op->s_disp_frm_buf.u4_u_wd =
1102                             pv_disp_op->s_disp_frm_buf.u4_y_wd;
1103             pv_disp_op->s_disp_frm_buf.u4_v_wd = 0;
1104 
1105         }
1106         else if((pv_disp_op->e_output_format == IV_RGB_565)
1107                         || (pv_disp_op->e_output_format == IV_YUV_422ILE))
1108         {
1109 
1110             pv_disp_op->s_disp_frm_buf.u4_u_strd = 0;
1111             pv_disp_op->s_disp_frm_buf.u4_v_strd = 0;
1112             pv_disp_op->s_disp_frm_buf.u4_u_wd = 0;
1113             pv_disp_op->s_disp_frm_buf.u4_v_wd = 0;
1114             pv_disp_op->s_disp_frm_buf.u4_u_ht = 0;
1115             pv_disp_op->s_disp_frm_buf.u4_v_ht = 0;
1116 
1117         }
1118 
1119 
1120     }
1121 
1122     return u4_api_ret;
1123 }
1124 
1125 
1126 /*****************************************************************************/
1127 /*  Function Name : ih264d_release_display_field                                         */
1128 /*                                                                           */
1129 /*  Description   : This function releases the display field that was returned   */
1130 /*                  here.                                                    */
1131 /*  Inputs        : ps_dec - Decoder parameters                              */
1132 /*  Globals       : None                                                     */
1133 /*  Processing    : Refer bumping process in the standard                    */
1134 /*  Outputs       : Assigns display sequence number.                         */
1135 /*  Returns       : None                                                     */
1136 /*                                                                           */
1137 /*  Issues        : None                                                     */
1138 /*                                                                           */
1139 /*  Revision History:                                                        */
1140 /*                                                                           */
1141 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1142 /*         27 04 2005   NS              Draft                                */
1143 /*                                                                           */
1144 /*****************************************************************************/
ih264d_release_display_field(dec_struct_t * ps_dec,ivd_get_display_frame_op_t * pv_disp_op)1145 void ih264d_release_display_field(dec_struct_t *ps_dec,
1146                                   ivd_get_display_frame_op_t *pv_disp_op)
1147 {
1148     if(1 == pv_disp_op->u4_error_code)
1149     {
1150         if(1 == ps_dec->u1_flushfrm)
1151         {
1152             UWORD32 i;
1153 
1154             if(1 == ps_dec->u4_share_disp_buf)
1155             {
1156                 H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
1157                 for(i = 0; i < (MAX_DISP_BUFS_NEW); i++)
1158                 {
1159                     if(1 == ps_dec->u4_disp_buf_mapping[i])
1160                     {
1161                         ih264_buf_mgr_release(
1162                                         (buf_mgr_t *)ps_dec->pv_pic_buf_mgr, i,
1163                                         BUF_MGR_IO);
1164                         ps_dec->u4_disp_buf_mapping[i] = 0;
1165                     }
1166                 } H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
1167 
1168                 memset(ps_dec->u4_disp_buf_to_be_freed, 0,
1169                        (MAX_DISP_BUFS_NEW) * sizeof(UWORD32));
1170                 for(i = 0; i < ps_dec->u1_pic_bufs; i++)
1171                     ps_dec->u4_disp_buf_mapping[i] = 1;
1172             }
1173             ps_dec->u1_flushfrm = 0;
1174 
1175         }
1176     }
1177     else
1178     {
1179         H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
1180 
1181         if(0 == ps_dec->u4_share_disp_buf)
1182         {
1183             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
1184                                   pv_disp_op->u4_disp_buf_id,
1185                                   BUF_MGR_IO);
1186 
1187         }
1188         else
1189         {
1190             ps_dec->u4_disp_buf_mapping[pv_disp_op->u4_disp_buf_id] = 1;
1191         } H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
1192 
1193     }
1194 }
1195 /*****************************************************************************/
1196 /*  Function Name : ih264d_assign_display_seq                                         */
1197 /*                                                                           */
1198 /*  Description   : This function implments bumping process. Every outgoing  */
1199 /*                  frame from DPB is assigned a display sequence number     */
1200 /*                  which increases monotonically. System looks for this     */
1201 /*                  number to display a frame.                              */
1202 /*                  here.                                                    */
1203 /*  Inputs        : ps_dec - Decoder parameters                              */
1204 /*  Globals       : None                                                     */
1205 /*  Processing    : Refer bumping process in the standard                    */
1206 /*  Outputs       : Assigns display sequence number.                         */
1207 /*  Returns       : None                                                     */
1208 /*                                                                           */
1209 /*  Issues        : None                                                     */
1210 /*                                                                           */
1211 /*  Revision History:                                                        */
1212 /*                                                                           */
1213 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1214 /*         27 04 2005   NS              Draft                                */
1215 /*                                                                           */
1216 /*****************************************************************************/
ih264d_assign_display_seq(dec_struct_t * ps_dec)1217 WORD32 ih264d_assign_display_seq(dec_struct_t *ps_dec)
1218 {
1219     WORD32 i;
1220     WORD32 i4_min_poc;
1221     WORD32 i4_min_poc_buf_id;
1222     WORD32 i4_min_index;
1223     dpb_manager_t *ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1224     WORD32 (*i4_poc_buf_id_map)[3] = ps_dpb_mgr->ai4_poc_buf_id_map;
1225 
1226     i4_min_poc = 0x7fffffff;
1227     i4_min_poc_buf_id = -1;
1228     i4_min_index = -1;
1229 
1230     if(ps_dpb_mgr->i1_poc_buf_id_entries >= ps_dec->i4_display_delay)
1231     {
1232         for(i = 0; i < MAX_FRAMES; i++)
1233         {
1234             if((i4_poc_buf_id_map[i][0] != -1)
1235                             && (DO_NOT_DISP
1236                                             != ps_dpb_mgr->ai4_poc_buf_id_map[i][0]))
1237             {
1238                 /* Checking for <= is necessary to handle cases where there is one
1239                    valid buffer with poc set to 0x7FFFFFFF. */
1240                 if(i4_poc_buf_id_map[i][1] <= i4_min_poc)
1241                 {
1242                     i4_min_poc = i4_poc_buf_id_map[i][1];
1243                     i4_min_poc_buf_id = i4_poc_buf_id_map[i][0];
1244                     i4_min_index = i;
1245                 }
1246             }
1247         }
1248 
1249         if((i4_min_index != -1) && (DO_NOT_DISP != i4_min_poc_buf_id))
1250         {
1251             ps_dec->i4_cur_display_seq++;
1252             ih264_disp_mgr_add(
1253                             (disp_mgr_t *)ps_dec->pv_disp_buf_mgr,
1254                             i4_min_poc_buf_id, ps_dec->i4_cur_display_seq,
1255                             ps_dec->apv_buf_id_pic_buf_map[i4_min_poc_buf_id]);
1256             i4_poc_buf_id_map[i4_min_index][0] = -1;
1257             i4_poc_buf_id_map[i4_min_index][1] = 0x7fffffff;
1258             ps_dpb_mgr->i1_poc_buf_id_entries--;
1259         }
1260         else if(DO_NOT_DISP == i4_min_poc_buf_id)
1261         {
1262             WORD32 i4_error_code;
1263             i4_error_code = ERROR_GAPS_IN_FRM_NUM;
1264 //          i4_error_code |= 1<<IVD_CORRUPTEDDATA;
1265             return i4_error_code;
1266         }
1267     }
1268     return OK;
1269 }
1270 
1271 /*****************************************************************************/
1272 /*                                                                           */
1273 /*  Function Name : ih264d_release_display_bufs                                       */
1274 /*                                                                           */
1275 /*  Description   : This function implments bumping process when mmco = 5.   */
1276 /*                  Each outgoing frame from DPB is assigned a display       */
1277 /*                  sequence number which increases monotonically. System    */
1278 /*                  looks for this number to display a frame.                */
1279 /*  Inputs        : ps_dec - Decoder parameters                              */
1280 /*  Globals       : None                                                     */
1281 /*  Processing    : Refer bumping process in the standard for mmco = 5       */
1282 /*  Outputs       : Assigns display sequence number.                         */
1283 /*  Returns       : None                                                     */
1284 /*                                                                           */
1285 /*  Issues        : None                                                     */
1286 /*                                                                           */
1287 /*  Revision History:                                                        */
1288 /*                                                                           */
1289 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1290 /*         27 04 2005   NS              Draft                                */
1291 /*                                                                           */
1292 /*****************************************************************************/
ih264d_release_display_bufs(dec_struct_t * ps_dec)1293 void ih264d_release_display_bufs(dec_struct_t *ps_dec)
1294 {
1295     WORD32 i, j;
1296     WORD32 i4_min_poc;
1297     WORD32 i4_min_poc_buf_id;
1298     WORD32 i4_min_index;
1299     UWORD64 u8_temp;
1300     dpb_manager_t *ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1301     WORD32 (*i4_poc_buf_id_map)[3] = ps_dpb_mgr->ai4_poc_buf_id_map;
1302 
1303     i4_min_poc = 0x7fffffff;
1304     i4_min_poc_buf_id = 0;
1305     i4_min_index = 0;
1306 
1307     ih264d_delete_nonref_nondisplay_pics(ps_dpb_mgr);
1308 
1309     for(j = 0; j < ps_dpb_mgr->i1_poc_buf_id_entries; j++)
1310     {
1311         i4_min_poc = 0x7fffffff;
1312         for(i = 0; i < MAX_FRAMES; i++)
1313         {
1314             if(i4_poc_buf_id_map[i][0] != -1)
1315             {
1316                 /* Checking for <= is necessary to handle cases where there is one
1317                    valid buffer with poc set to 0x7FFFFFFF. */
1318                 if(i4_poc_buf_id_map[i][1] <= i4_min_poc)
1319                 {
1320                     i4_min_poc = i4_poc_buf_id_map[i][1];
1321                     i4_min_poc_buf_id = i4_poc_buf_id_map[i][0];
1322                     i4_min_index = i;
1323                 }
1324             }
1325         }
1326 
1327         if(DO_NOT_DISP != i4_min_poc_buf_id)
1328         {
1329             ps_dec->i4_cur_display_seq++;
1330             ih264_disp_mgr_add(
1331                             (disp_mgr_t *)ps_dec->pv_disp_buf_mgr,
1332                             i4_min_poc_buf_id, ps_dec->i4_cur_display_seq,
1333                             ps_dec->apv_buf_id_pic_buf_map[i4_min_poc_buf_id]);
1334             i4_poc_buf_id_map[i4_min_index][0] = -1;
1335             i4_poc_buf_id_map[i4_min_index][1] = 0x7fffffff;
1336             ps_dpb_mgr->ai4_poc_buf_id_map[i4_min_index][2] = 0;
1337         }
1338         else
1339         {
1340             i4_poc_buf_id_map[i4_min_index][0] = -1;
1341             i4_poc_buf_id_map[i4_min_index][1] = 0x7fffffff;
1342             ps_dpb_mgr->ai4_poc_buf_id_map[i4_min_index][2] = 0;
1343         }
1344     }
1345     ps_dpb_mgr->i1_poc_buf_id_entries = 0;
1346     u8_temp = (UWORD64)ps_dec->i4_prev_max_display_seq + ps_dec->i4_max_poc
1347               + ps_dec->u1_max_dec_frame_buffering + 1;
1348     /*If i4_prev_max_display_seq overflows integer range, reset it */
1349     ps_dec->i4_prev_max_display_seq = (u8_temp > 0x7fffffff)?
1350                                       0 : u8_temp;
1351     ps_dec->i4_max_poc = 0;
1352 }
1353 
1354 /*****************************************************************************/
1355 /*                                                                           */
1356 /*  Function Name : ih264d_assign_pic_num                                           */
1357 /*                                                                           */
1358 /*  Description   : This function assigns pic num to each reference frame    */
1359 /*                  depending on the cur_frame_num as speified in section    */
1360 /*                  8.2.4.1                                                  */
1361 /*                                                                           */
1362 /*  Inputs        : ps_dec                                                   */
1363 /*                                                                           */
1364 /*  Globals       : NO globals used                                          */
1365 /*                                                                           */
1366 /*  Processing    : for all ST pictures                                      */
1367 /*                    if( FrameNum > cur_frame_num)                          */
1368 /*                    PicNum = FrameNum - MaxFrameNum                        */
1369 /*                    else                                                   */
1370 /*                    PicNum = FrameNum                                      */
1371 /*                                                                           */
1372 /*  Returns       : void                                                     */
1373 /*                                                                           */
1374 /*  Issues        : NO                                                       */
1375 /*                                                                           */
1376 /*  Revision History:                                                        */
1377 /*                                                                           */
1378 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1379 /*         13 07 2002   Jay             Draft                                */
1380 /*                                                                           */
1381 /*****************************************************************************/
1382 
ih264d_assign_pic_num(dec_struct_t * ps_dec)1383 void ih264d_assign_pic_num(dec_struct_t *ps_dec)
1384 {
1385     dpb_manager_t *ps_dpb_mgr;
1386     struct dpb_info_t *ps_next_dpb;
1387     WORD8 i;
1388     WORD32 i4_cur_frame_num, i4_max_frame_num;
1389     WORD32 i4_ref_frame_num;
1390     UWORD8 u1_fld_pic_flag = ps_dec->ps_cur_slice->u1_field_pic_flag;
1391 
1392     i4_max_frame_num = ps_dec->ps_cur_sps->u2_u4_max_pic_num_minus1 + 1;
1393     i4_cur_frame_num = ps_dec->ps_cur_pic->i4_frame_num;
1394     ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1395 
1396     /* Start from ST head */
1397     ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1398     for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
1399     {
1400         WORD32 i4_pic_num;
1401 
1402         i4_ref_frame_num = ps_next_dpb->ps_pic_buf->i4_frame_num;
1403         if(i4_ref_frame_num > i4_cur_frame_num)
1404         {
1405             /* RefPic Buf frame_num is before Current frame_num in decode order */
1406             i4_pic_num = i4_ref_frame_num - i4_max_frame_num;
1407         }
1408         else
1409         {
1410             /* RefPic Buf frame_num is after Current frame_num in decode order */
1411             i4_pic_num = i4_ref_frame_num;
1412         }
1413 
1414         ps_next_dpb->ps_pic_buf->i4_pic_num = i4_pic_num;
1415         ps_next_dpb->i4_frame_num = i4_pic_num;
1416         ps_next_dpb->ps_pic_buf->u1_long_term_frm_idx = MAX_REF_BUFS + 1;
1417         if(u1_fld_pic_flag)
1418         {
1419             /* Assign the pic num to top fields and bot fields */
1420 
1421             ps_next_dpb->s_top_field.i4_pic_num = i4_pic_num * 2
1422                             + !(ps_dec->ps_cur_slice->u1_bottom_field_flag);
1423             ps_next_dpb->s_bot_field.i4_pic_num = i4_pic_num * 2
1424                             + ps_dec->ps_cur_slice->u1_bottom_field_flag;
1425         }
1426         /* Chase the next link */
1427         ps_next_dpb = ps_next_dpb->ps_prev_short;
1428     }
1429 
1430     if(ps_dec->ps_cur_sps->u1_gaps_in_frame_num_value_allowed_flag
1431                     && ps_dpb_mgr->u1_num_gaps)
1432     {
1433         WORD32 i4_start_frm, i4_end_frm;
1434         /* Assign pic numbers for gaps */
1435         for(i = 0; i < MAX_FRAMES; i++)
1436         {
1437             i4_start_frm = ps_dpb_mgr->ai4_gaps_start_frm_num[i];
1438             if(i4_start_frm != INVALID_FRAME_NUM)
1439             {
1440                 if(i4_start_frm > i4_cur_frame_num)
1441                 {
1442                     /* gap's frame_num is before Current frame_num in
1443                      decode order */
1444                     i4_start_frm -= i4_max_frame_num;
1445                 }
1446                 ps_dpb_mgr->ai4_gaps_start_frm_num[i] = i4_start_frm;
1447                 i4_end_frm = ps_dpb_mgr->ai4_gaps_end_frm_num[i];
1448 
1449                 if(i4_end_frm > i4_cur_frame_num)
1450                 {
1451                     /* gap's frame_num is before Current frame_num in
1452                      decode order */
1453                     i4_end_frm -= i4_max_frame_num;
1454                 }
1455                 ps_dpb_mgr->ai4_gaps_end_frm_num[i] = i4_end_frm;
1456             }
1457         }
1458     }
1459 }
1460 
1461 /*!
1462  **************************************************************************
1463  * \if Function name : ih264d_update_qp \endif
1464  *
1465  * \brief
1466  *    Updates the values of QP and its related entities
1467  *
1468  * \return
1469  *    0 on Success and Error code otherwise
1470  *
1471  **************************************************************************
1472  */
ih264d_update_qp(dec_struct_t * ps_dec,const WORD8 i1_qp)1473 WORD32 ih264d_update_qp(dec_struct_t * ps_dec, const WORD8 i1_qp)
1474 {
1475     WORD32 i_temp;
1476     i_temp = (ps_dec->u1_qp + i1_qp + 52) % 52;
1477 
1478     if((i_temp < 0) || (i_temp > 51) || (i1_qp < -26) || (i1_qp > 25))
1479         return ERROR_INV_RANGE_QP_T;
1480 
1481     ps_dec->u1_qp = i_temp;
1482     ps_dec->u1_qp_y_rem6 = ps_dec->u1_qp % 6;
1483     ps_dec->u1_qp_y_div6 = ps_dec->u1_qp / 6;
1484     i_temp = CLIP3(0, 51, ps_dec->u1_qp + ps_dec->ps_cur_pps->i1_chroma_qp_index_offset);
1485     ps_dec->u1_qp_u_rem6 = MOD(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1486     ps_dec->u1_qp_u_div6 = DIV(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1487 
1488     i_temp = CLIP3(0, 51, ps_dec->u1_qp + ps_dec->ps_cur_pps->i1_second_chroma_qp_index_offset);
1489     ps_dec->u1_qp_v_rem6 = MOD(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1490     ps_dec->u1_qp_v_div6 = DIV(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1491 
1492     ps_dec->pu2_quant_scale_y =
1493                     gau2_ih264_iquant_scale_4x4[ps_dec->u1_qp_y_rem6];
1494     ps_dec->pu2_quant_scale_u =
1495                     gau2_ih264_iquant_scale_4x4[ps_dec->u1_qp_u_rem6];
1496     ps_dec->pu2_quant_scale_v =
1497                     gau2_ih264_iquant_scale_4x4[ps_dec->u1_qp_v_rem6];
1498     return OK;
1499 }
1500 
1501 /*****************************************************************************/
1502 /*                                                                           */
1503 /*  Function Name : ih264d_decode_gaps_in_frame_num                                 */
1504 /*                                                                           */
1505 /*  Description   : This function decodes gaps in frame number               */
1506 /*                                                                           */
1507 /*  Inputs        : ps_dec          Decoder parameters                       */
1508 /*                  u2_frame_num   current frame number                     */
1509 /*                                                                           */
1510 /*  Globals       : None                                                     */
1511 /*  Processing    : This functionality needs to be implemented               */
1512 /*  Outputs       : None                                                     */
1513 /*  Returns       : None                                                     */
1514 /*                                                                           */
1515 /*  Issues        : Not implemented                                          */
1516 /*                                                                           */
1517 /*  Revision History:                                                        */
1518 /*                                                                           */
1519 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1520 /*         06 05 2002   NS              Draft                                */
1521 /*                                                                           */
1522 /*****************************************************************************/
ih264d_decode_gaps_in_frame_num(dec_struct_t * ps_dec,UWORD16 u2_frame_num)1523 WORD32 ih264d_decode_gaps_in_frame_num(dec_struct_t *ps_dec,
1524                                        UWORD16 u2_frame_num)
1525 {
1526     UWORD32 u4_next_frm_num, u4_start_frm_num;
1527     UWORD32 u4_max_frm_num;
1528     pocstruct_t s_tmp_poc;
1529     WORD32 i4_poc;
1530     dec_slice_params_t *ps_cur_slice;
1531 
1532     dec_pic_params_t *ps_pic_params;
1533     WORD8 i1_gap_idx;
1534     WORD32 *i4_gaps_start_frm_num;
1535     dpb_manager_t *ps_dpb_mgr;
1536     WORD32 i4_frame_gaps;
1537     WORD8 *pi1_gaps_per_seq;
1538     WORD32 ret;
1539 
1540     ps_cur_slice = ps_dec->ps_cur_slice;
1541     if(ps_cur_slice->u1_field_pic_flag)
1542     {
1543         if(ps_dec->u2_prev_ref_frame_num == u2_frame_num)
1544             return 0;
1545     }
1546 
1547     u4_next_frm_num = ps_dec->u2_prev_ref_frame_num + 1;
1548     u4_max_frm_num = ps_dec->ps_cur_sps->u2_u4_max_pic_num_minus1 + 1;
1549 
1550     // check
1551     if(u4_next_frm_num >= u4_max_frm_num)
1552     {
1553         u4_next_frm_num -= u4_max_frm_num;
1554     }
1555 
1556     if(u4_next_frm_num == u2_frame_num)
1557     {
1558         return (0);
1559     }
1560 
1561     // check
1562     if((ps_dec->u1_nal_unit_type == IDR_SLICE_NAL)
1563                     && (u4_next_frm_num >= u2_frame_num))
1564     {
1565         return (0);
1566     }
1567     u4_start_frm_num = u4_next_frm_num;
1568 
1569     s_tmp_poc.i4_pic_order_cnt_lsb = 0;
1570     s_tmp_poc.i4_delta_pic_order_cnt_bottom = 0;
1571     s_tmp_poc.i4_pic_order_cnt_lsb = 0;
1572     s_tmp_poc.i4_delta_pic_order_cnt_bottom = 0;
1573     s_tmp_poc.i4_delta_pic_order_cnt[0] = 0;
1574     s_tmp_poc.i4_delta_pic_order_cnt[1] = 0;
1575 
1576     ps_cur_slice = ps_dec->ps_cur_slice;
1577     ps_pic_params = ps_dec->ps_cur_pps;
1578 
1579     i4_frame_gaps = 0;
1580     ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1581 
1582     /* Find a empty slot to store gap seqn info */
1583     i4_gaps_start_frm_num = ps_dpb_mgr->ai4_gaps_start_frm_num;
1584     for(i1_gap_idx = 0; i1_gap_idx < MAX_FRAMES; i1_gap_idx++)
1585     {
1586         if(INVALID_FRAME_NUM == i4_gaps_start_frm_num[i1_gap_idx])
1587             break;
1588     }
1589     if(MAX_FRAMES == i1_gap_idx)
1590     {
1591         UWORD32 i4_error_code;
1592         i4_error_code = ERROR_DBP_MANAGER_T;
1593 //          i4_error_code |= 1<<IVD_CORRUPTEDDATA;
1594         return i4_error_code;
1595     }
1596 
1597     i4_poc = 0;
1598     i4_gaps_start_frm_num[i1_gap_idx] = u4_start_frm_num;
1599     ps_dpb_mgr->ai4_gaps_end_frm_num[i1_gap_idx] = u2_frame_num - 1;
1600     pi1_gaps_per_seq = ps_dpb_mgr->ai1_gaps_per_seq;
1601     pi1_gaps_per_seq[i1_gap_idx] = 0;
1602     while(u4_next_frm_num != u2_frame_num)
1603     {
1604         ih264d_delete_nonref_nondisplay_pics(ps_dpb_mgr);
1605         if(ps_pic_params->ps_sps->u1_pic_order_cnt_type)
1606         {
1607             /* allocate a picture buffer and insert it as ST node */
1608             ret = ih264d_decode_pic_order_cnt(0, u4_next_frm_num,
1609                                               &ps_dec->s_prev_pic_poc,
1610                                               &s_tmp_poc, ps_cur_slice,
1611                                               ps_pic_params, 1, 0, 0,
1612                                               &i4_poc);
1613             if(ret != OK)
1614                 return ret;
1615 
1616             /* Display seq no calculations */
1617             if(i4_poc >= ps_dec->i4_max_poc)
1618                 ps_dec->i4_max_poc = i4_poc;
1619             /* IDR Picture or POC wrap around */
1620             if(i4_poc == 0)
1621             {
1622                 UWORD64 u8_temp;
1623                 u8_temp = (UWORD64)ps_dec->i4_prev_max_display_seq
1624                           + ps_dec->i4_max_poc
1625                           + ps_dec->u1_max_dec_frame_buffering + 1;
1626                 /*If i4_prev_max_display_seq overflows integer range, reset it */
1627                 ps_dec->i4_prev_max_display_seq = (u8_temp > 0x7fffffff)?
1628                                                   0 : u8_temp;
1629                 ps_dec->i4_max_poc = 0;
1630             }
1631 
1632             ps_cur_slice->u1_mmco_equalto5 = 0;
1633             ps_cur_slice->u2_frame_num = u4_next_frm_num;
1634         }
1635 
1636         // check
1637         if(ps_dpb_mgr->i1_poc_buf_id_entries
1638                         >= ps_dec->u1_max_dec_frame_buffering)
1639         {
1640             ret = ih264d_assign_display_seq(ps_dec);
1641             if(ret != OK)
1642                 return ret;
1643         }
1644 
1645         ret = ih264d_insert_pic_in_display_list(
1646                         ps_dec->ps_dpb_mgr, (WORD8) DO_NOT_DISP,
1647                         (WORD32)(ps_dec->i4_prev_max_display_seq + i4_poc),
1648                         u4_next_frm_num);
1649         if(ret != OK)
1650             return ret;
1651 
1652         pi1_gaps_per_seq[i1_gap_idx]++;
1653         ret = ih264d_do_mmco_for_gaps(ps_dpb_mgr,
1654                                 ps_dec->ps_cur_sps->u1_num_ref_frames);
1655         if(ret != OK)
1656             return ret;
1657 
1658         ih264d_delete_nonref_nondisplay_pics(ps_dpb_mgr);
1659 
1660         u4_next_frm_num++;
1661         if(u4_next_frm_num >= u4_max_frm_num)
1662         {
1663             u4_next_frm_num -= u4_max_frm_num;
1664         }
1665 
1666         i4_frame_gaps++;
1667     }
1668 
1669     return OK;
1670 }
1671 
1672 /*!
1673  **************************************************************************
1674  * \if Function name : ih264d_create_pic_buffers \endif
1675  *
1676  * \brief
1677  *    This function creates Picture Buffers.
1678  *
1679  * \return
1680  *    0 on Success and -1 on error
1681  **************************************************************************
1682  */
ih264d_create_pic_buffers(UWORD8 u1_num_of_buf,dec_struct_t * ps_dec)1683 WORD32 ih264d_create_pic_buffers(UWORD8 u1_num_of_buf,
1684                                dec_struct_t *ps_dec)
1685 {
1686     struct pic_buffer_t *ps_pic_buf;
1687     UWORD8 i;
1688     UWORD32 u4_luma_size, u4_chroma_size;
1689     UWORD8 u1_frm = ps_dec->ps_cur_sps->u1_frame_mbs_only_flag;
1690     WORD32 j;
1691     UWORD8 *pu1_buf;
1692 
1693     ps_pic_buf = ps_dec->ps_pic_buf_base;
1694     ih264_disp_mgr_init((disp_mgr_t *)ps_dec->pv_disp_buf_mgr);
1695     ih264_buf_mgr_init((buf_mgr_t *)ps_dec->pv_pic_buf_mgr);
1696     u4_luma_size = ps_dec->u2_frm_wd_y * ps_dec->u2_frm_ht_y;
1697     u4_chroma_size = ps_dec->u2_frm_wd_uv * ps_dec->u2_frm_ht_uv;
1698 
1699     {
1700         if(ps_dec->u4_share_disp_buf == 1)
1701         {
1702             /* In case of buffers getting shared between application and library
1703              there is no need of reference memtabs. Instead of setting the i4_size
1704              to zero, it is reduced to a small i4_size to ensure that changes
1705              in the code are minimal */
1706             if((ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
1707                             || (ps_dec->u1_chroma_format == IV_YUV_420SP_VU)
1708                             || (ps_dec->u1_chroma_format == IV_YUV_420P))
1709             {
1710                 u4_luma_size = 64;
1711             }
1712 
1713             if(ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
1714             {
1715                 u4_chroma_size = 64;
1716             }
1717 
1718         }
1719     }
1720 
1721     pu1_buf = ps_dec->pu1_pic_buf_base;
1722 
1723     /* Allocate memory for refernce buffers */
1724     for(i = 0; i < u1_num_of_buf; i++)
1725     {
1726         UWORD32 u4_offset;
1727         WORD32 buf_ret;
1728         UWORD8 *pu1_luma, *pu1_chroma;
1729         void *pv_mem_ctxt = ps_dec->pv_mem_ctxt;
1730 
1731         pu1_luma = pu1_buf;
1732         pu1_buf += ALIGN64(u4_luma_size);
1733         pu1_chroma = pu1_buf;
1734         pu1_buf += ALIGN64(u4_chroma_size);
1735 
1736         /* Offset to the start of the pic from the top left corner of the frame
1737          buffer */
1738 
1739         if((0 == ps_dec->u4_share_disp_buf)
1740                         || (NULL == ps_dec->disp_bufs[i].buf[0]))
1741         {
1742             UWORD32 pad_len_h, pad_len_v;
1743 
1744             u4_offset = ps_dec->u2_frm_wd_y * (PAD_LEN_Y_V << 1) + PAD_LEN_Y_H;
1745             ps_pic_buf->pu1_buf1 = (UWORD8 *)(pu1_luma) + u4_offset;
1746 
1747             pad_len_h = MAX(PAD_LEN_UV_H, (PAD_LEN_Y_H >> 1));
1748             pad_len_v = MAX(PAD_LEN_UV_V, PAD_LEN_Y_V);
1749 
1750             u4_offset = ps_dec->u2_frm_wd_uv * pad_len_v + pad_len_h;
1751 
1752             ps_pic_buf->pu1_buf2 = (UWORD8 *)(pu1_chroma) + u4_offset;
1753             ps_pic_buf->pu1_buf3 = (UWORD8 *)(NULL) + u4_offset;
1754 
1755         }
1756         else
1757         {
1758             UWORD32 pad_len_h, pad_len_v;
1759             u4_offset = ps_dec->u2_frm_wd_y * (PAD_LEN_Y_V << 1) + PAD_LEN_Y_H;
1760             ps_pic_buf->pu1_buf1 = (UWORD8 *)ps_dec->disp_bufs[i].buf[0]
1761                             + u4_offset;
1762 
1763             ps_dec->disp_bufs[i].u4_ofst[0] = u4_offset;
1764 
1765             if(ps_dec->u1_chroma_format == IV_YUV_420P)
1766             {
1767                 pad_len_h = MAX(PAD_LEN_UV_H * YUV420SP_FACTOR,
1768                                 (PAD_LEN_Y_H >> 1));
1769                 pad_len_v = MAX(PAD_LEN_UV_V, PAD_LEN_Y_V);
1770 
1771                 u4_offset = ps_dec->u2_frm_wd_uv * pad_len_v + pad_len_h;
1772                 ps_pic_buf->pu1_buf2 = (UWORD8 *)(pu1_chroma) + u4_offset;
1773                 ps_pic_buf->pu1_buf3 = (UWORD8 *)(NULL) + u4_offset;
1774 
1775                 ps_dec->disp_bufs[i].u4_ofst[1] = u4_offset;
1776                 ps_dec->disp_bufs[i].u4_ofst[2] = u4_offset;
1777 
1778             }
1779             else
1780             {
1781                 pad_len_h = MAX(PAD_LEN_UV_H * YUV420SP_FACTOR,
1782                                 (PAD_LEN_Y_H >> 1));
1783                 pad_len_v = MAX(PAD_LEN_UV_V, PAD_LEN_Y_V);
1784 
1785                 u4_offset = ps_dec->u2_frm_wd_uv * pad_len_v + pad_len_h;
1786                 ps_pic_buf->pu1_buf2 = (UWORD8 *)(ps_dec->disp_bufs[i].buf[1])
1787                                 + u4_offset;
1788                 ps_pic_buf->pu1_buf3 = (UWORD8 *)(ps_dec->disp_bufs[i].buf[1])
1789                                 + u4_offset;
1790 
1791                 ps_dec->disp_bufs[i].u4_ofst[1] = u4_offset;
1792                 ps_dec->disp_bufs[i].u4_ofst[2] = u4_offset;
1793             }
1794         }
1795 
1796         ps_pic_buf->u2_frm_ht_y = ps_dec->u2_frm_ht_y;
1797         ps_pic_buf->u2_frm_ht_uv = ps_dec->u2_frm_ht_uv;
1798         ps_pic_buf->u2_frm_wd_y = ps_dec->u2_frm_wd_y;
1799         ps_pic_buf->u2_frm_wd_uv = ps_dec->u2_frm_wd_uv;
1800 
1801         ps_pic_buf->u1_pic_buf_id = i;
1802 
1803         buf_ret = ih264_buf_mgr_add((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
1804                                     ps_pic_buf, i);
1805         if(0 != buf_ret)
1806         {
1807             ps_dec->i4_error_code = ERROR_BUF_MGR;
1808             return ERROR_BUF_MGR;
1809         }
1810 
1811         ps_dec->apv_buf_id_pic_buf_map[i] = (void *)ps_pic_buf;
1812         ps_pic_buf++;
1813     }
1814 
1815     if(1 == ps_dec->u4_share_disp_buf)
1816     {
1817         for(i = 0; i < u1_num_of_buf; i++)
1818             ps_dec->u4_disp_buf_mapping[i] = 1;
1819     }
1820     return OK;
1821 }
1822 
1823 /*!
1824  **************************************************************************
1825  * \if Function name : ih264d_allocate_dynamic_bufs \endif
1826  *
1827  * \brief
1828  *    This function allocates memory required by Decoder.
1829  *
1830  * \param ps_dec: Pointer to dec_struct_t.
1831  *
1832  * \return
1833  *    Returns i4_status as returned by MemManager.
1834  *
1835  **************************************************************************
1836  */
ih264d_allocate_dynamic_bufs(dec_struct_t * ps_dec)1837 WORD16 ih264d_allocate_dynamic_bufs(dec_struct_t * ps_dec)
1838 {
1839     struct MemReq s_MemReq;
1840     struct MemBlock *p_MemBlock;
1841 
1842     pred_info_t *ps_pred_frame;
1843     dec_mb_info_t *ps_frm_mb_info;
1844     dec_slice_struct_t *ps_dec_slice_buf;
1845     UWORD8 *pu1_dec_mb_map, *pu1_recon_mb_map;
1846     UWORD16 *pu2_slice_num_map;
1847 
1848     WORD16 *pi16_res_coeff;
1849     WORD16 i16_status = 0;
1850     UWORD8 uc_frmOrFld = (1 - ps_dec->ps_cur_sps->u1_frame_mbs_only_flag);
1851     UWORD16 u4_luma_wd = ps_dec->u2_frm_wd_y;
1852     UWORD16 u4_chroma_wd = ps_dec->u2_frm_wd_uv;
1853     WORD8 c_i = 0;
1854     dec_seq_params_t *ps_sps = ps_dec->ps_cur_sps;
1855     UWORD32 u4_total_mbs = ps_sps->u2_total_num_of_mbs << uc_frmOrFld;
1856     UWORD32 u4_wd_mbs = ps_dec->u2_frm_wd_in_mbs;
1857     UWORD32 u4_ht_mbs = ps_dec->u2_frm_ht_in_mbs;
1858     UWORD32 u4_blk_wd;
1859     UWORD32 ui_size = 0;
1860     UWORD32 u4_int_scratch_size = 0, u4_ref_pred_size = 0;
1861     UWORD8 *pu1_buf;
1862     WORD32 num_entries;
1863     WORD32 size;
1864     void *pv_buf;
1865     UWORD32 u4_num_bufs;
1866     UWORD32 u4_luma_size, u4_chroma_size;
1867     void *pv_mem_ctxt = ps_dec->pv_mem_ctxt;
1868 
1869     size = u4_total_mbs;
1870     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1871     RETURN_IF((NULL == pv_buf), IV_FAIL);
1872     memset(pv_buf, 0, size);
1873     ps_dec->pu1_dec_mb_map = pv_buf;
1874 
1875     size = u4_total_mbs;
1876     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1877     RETURN_IF((NULL == pv_buf), IV_FAIL);
1878     memset(pv_buf, 0, size);
1879     ps_dec->pu1_recon_mb_map = pv_buf;
1880 
1881     size = u4_total_mbs * sizeof(UWORD16);
1882     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1883     RETURN_IF((NULL == pv_buf), IV_FAIL);
1884     memset(pv_buf, 0, size);
1885     ps_dec->pu2_slice_num_map = pv_buf;
1886 
1887     /************************************************************/
1888     /* Post allocation Initialisations                          */
1889     /************************************************************/
1890     ps_dec->ps_parse_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
1891     ps_dec->ps_decode_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
1892     ps_dec->ps_computebs_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
1893 
1894     ps_dec->ps_pred_start = ps_dec->ps_pred;
1895 
1896     size = sizeof(parse_pmbarams_t) * (ps_dec->u1_recon_mb_grp);
1897     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1898     RETURN_IF((NULL == pv_buf), IV_FAIL);
1899     memset(pv_buf, 0, size);
1900     ps_dec->ps_parse_mb_data = pv_buf;
1901 
1902     size = sizeof(parse_part_params_t)
1903                         * ((ps_dec->u1_recon_mb_grp) << 4);
1904     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1905     RETURN_IF((NULL == pv_buf), IV_FAIL);
1906     memset(pv_buf, 0, size);
1907     ps_dec->ps_parse_part_params = pv_buf;
1908 
1909     size = ((u4_wd_mbs * sizeof(deblkmb_neighbour_t)) << uc_frmOrFld);
1910     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1911     RETURN_IF((NULL == pv_buf), IV_FAIL);
1912     memset(pv_buf, 0, size);
1913     ps_dec->ps_deblk_top_mb = pv_buf;
1914 
1915     size = ((sizeof(ctxt_inc_mb_info_t))
1916                         * (((u4_wd_mbs + 1) << uc_frmOrFld) + 1));
1917     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1918     RETURN_IF((NULL == pv_buf), IV_FAIL);
1919     memset(pv_buf, 0, size);
1920     ps_dec->p_ctxt_inc_mb_map = pv_buf;
1921 
1922     /* 0th entry of CtxtIncMbMap will be always be containing default values
1923      for CABAC context representing MB not available */
1924     ps_dec->p_ctxt_inc_mb_map += 1;
1925 
1926     size = (sizeof(mv_pred_t) * ps_dec->u1_recon_mb_grp
1927                         * 16);
1928     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1929     RETURN_IF((NULL == pv_buf), IV_FAIL);
1930     memset(pv_buf, 0, size);
1931     ps_dec->ps_mv_p[0] = pv_buf;
1932 
1933     size = (sizeof(mv_pred_t) * ps_dec->u1_recon_mb_grp
1934                         * 16);
1935     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1936     RETURN_IF((NULL == pv_buf), IV_FAIL);
1937     memset(pv_buf, 0, size);
1938     ps_dec->ps_mv_p[1] = pv_buf;
1939 
1940     {
1941         UWORD8 i;
1942         for(i = 0; i < MV_SCRATCH_BUFS; i++)
1943         {
1944             size = (sizeof(mv_pred_t)
1945                             * ps_dec->u1_recon_mb_grp * 4);
1946             pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1947             RETURN_IF((NULL == pv_buf), IV_FAIL);
1948             memset(pv_buf, 0, size);
1949             ps_dec->ps_mv_top_p[i] = pv_buf;
1950         }
1951     }
1952 
1953     size = sizeof(UWORD8) * ((u4_wd_mbs + 2) * MB_SIZE) * 2;
1954     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1955     RETURN_IF((NULL == pv_buf), IV_FAIL);
1956     ps_dec->pu1_y_intra_pred_line = pv_buf;
1957     memset(ps_dec->pu1_y_intra_pred_line, 0, size);
1958     ps_dec->pu1_y_intra_pred_line += MB_SIZE;
1959 
1960     size = sizeof(UWORD8) * ((u4_wd_mbs + 2) * MB_SIZE) * 2;
1961     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1962     RETURN_IF((NULL == pv_buf), IV_FAIL);
1963     ps_dec->pu1_u_intra_pred_line = pv_buf;
1964     memset(ps_dec->pu1_u_intra_pred_line, 0, size);
1965     ps_dec->pu1_u_intra_pred_line += MB_SIZE;
1966 
1967     size = sizeof(UWORD8) * ((u4_wd_mbs + 2) * MB_SIZE) * 2;
1968     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1969     RETURN_IF((NULL == pv_buf), IV_FAIL);
1970     ps_dec->pu1_v_intra_pred_line = pv_buf;
1971     memset(ps_dec->pu1_v_intra_pred_line, 0, size);
1972     ps_dec->pu1_v_intra_pred_line += MB_SIZE;
1973 
1974     if(ps_dec->u1_separate_parse)
1975     {
1976         /* Needs one extra row of info, to hold top row data */
1977         size = sizeof(mb_neigbour_params_t)
1978                         * 2 * ((u4_wd_mbs + 2) * (u4_ht_mbs + 1));
1979     }
1980     else
1981     {
1982         size = sizeof(mb_neigbour_params_t)
1983                         * 2 * ((u4_wd_mbs + 2) << uc_frmOrFld);
1984     }
1985     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1986     RETURN_IF((NULL == pv_buf), IV_FAIL);
1987 
1988     ps_dec->ps_nbr_mb_row = pv_buf;
1989     memset(ps_dec->ps_nbr_mb_row, 0, size);
1990 
1991     /* Allocate deblock MB info */
1992     size = (u4_total_mbs + u4_wd_mbs) * sizeof(deblk_mb_t);
1993 
1994     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1995     RETURN_IF((NULL == pv_buf), IV_FAIL);
1996     ps_dec->ps_deblk_pic = pv_buf;
1997 
1998     memset(ps_dec->ps_deblk_pic, 0, size);
1999 
2000     /* Allocate frame level mb info */
2001     size = sizeof(dec_mb_info_t) * u4_total_mbs;
2002     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2003     RETURN_IF((NULL == pv_buf), IV_FAIL);
2004     ps_dec->ps_frm_mb_info = pv_buf;
2005     memset(ps_dec->ps_frm_mb_info, 0, size);
2006 
2007     /* Allocate memory for slice headers dec_slice_struct_t */
2008     num_entries = MAX_FRAMES;
2009     if((1 >= ps_dec->ps_cur_sps->u1_num_ref_frames) &&
2010         (0 == ps_dec->i4_display_delay))
2011     {
2012         num_entries = 1;
2013     }
2014     num_entries = ((2 * num_entries) + 1);
2015     num_entries *= 2;
2016 
2017     size = num_entries * sizeof(void *);
2018     size += PAD_MAP_IDX_POC * sizeof(void *);
2019     size *= u4_total_mbs;
2020     size += sizeof(dec_slice_struct_t) * u4_total_mbs;
2021     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2022     RETURN_IF((NULL == pv_buf), IV_FAIL);
2023 
2024     ps_dec->ps_dec_slice_buf = pv_buf;
2025     memset(ps_dec->ps_dec_slice_buf, 0, size);
2026     pu1_buf = (UWORD8 *)ps_dec->ps_dec_slice_buf;
2027     pu1_buf += sizeof(dec_slice_struct_t) * u4_total_mbs;
2028     ps_dec->pv_map_ref_idx_to_poc_buf = (void *)pu1_buf;
2029 
2030     /* Allocate memory for packed pred info */
2031     num_entries = u4_total_mbs;
2032     num_entries *= 16 * 2;
2033 
2034     size = sizeof(pred_info_pkd_t) * num_entries;
2035     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2036     RETURN_IF((NULL == pv_buf), IV_FAIL);
2037     memset(pv_buf, 0, size);
2038     ps_dec->ps_pred_pkd = pv_buf;
2039 
2040     /* Allocate memory for coeff data */
2041     size = MB_LUM_SIZE * sizeof(WORD16);
2042     /*For I16x16 MBs, 16 4x4 AC coeffs and 1 4x4 DC coeff TU blocks will be sent
2043     For all MBs along with 8 4x4 AC coeffs 2 2x2 DC coeff TU blocks will be sent
2044     So use 17 4x4 TU blocks for luma and 9 4x4 TU blocks for chroma */
2045     size += u4_total_mbs * (MAX(17 * sizeof(tu_sblk4x4_coeff_data_t),4 * sizeof(tu_blk8x8_coeff_data_t))
2046                                             + 9 * sizeof(tu_sblk4x4_coeff_data_t));
2047     //32 bytes for each mb to store u1_prev_intra4x4_pred_mode and u1_rem_intra4x4_pred_mode data
2048     size += u4_total_mbs * 32;
2049     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2050     RETURN_IF((NULL == pv_buf), IV_FAIL);
2051     memset(pv_buf, 0, size);
2052 
2053     ps_dec->pi2_coeff_data = pv_buf;
2054 
2055     ps_dec->pv_pic_tu_coeff_data = (void *)(ps_dec->pi2_coeff_data + MB_LUM_SIZE);
2056 
2057     /* Allocate MV bank buffer */
2058     {
2059         UWORD32 col_flag_buffer_size, mvpred_buffer_size;
2060 
2061         col_flag_buffer_size = ((ps_dec->u2_pic_wd * ps_dec->u2_pic_ht) >> 4);
2062         mvpred_buffer_size = sizeof(mv_pred_t)
2063                         * ((ps_dec->u2_pic_wd * (ps_dec->u2_pic_ht + PAD_MV_BANK_ROW)) >> 4);
2064 
2065         u4_num_bufs = ps_dec->ps_cur_sps->u1_num_ref_frames + 1;
2066 
2067         u4_num_bufs = MIN(u4_num_bufs, ps_dec->u1_pic_bufs);
2068         u4_num_bufs = MAX(u4_num_bufs, 2);
2069         size = ALIGN64(mvpred_buffer_size) + ALIGN64(col_flag_buffer_size);
2070         size *= u4_num_bufs;
2071         pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2072         RETURN_IF((NULL == pv_buf), IV_FAIL);
2073         memset(pv_buf, 0, size);
2074         ps_dec->pu1_mv_bank_buf_base = pv_buf;
2075     }
2076 
2077     /* Allocate Pic buffer */
2078     u4_luma_size = ps_dec->u2_frm_wd_y * ps_dec->u2_frm_ht_y;
2079     u4_chroma_size = ps_dec->u2_frm_wd_uv * ps_dec->u2_frm_ht_uv;
2080 
2081     {
2082         if(ps_dec->u4_share_disp_buf == 1)
2083         {
2084             /* In case of buffers getting shared between application and library
2085              there is no need of reference memtabs. Instead of setting the i4_size
2086              to zero, it is reduced to a small i4_size to ensure that changes
2087              in the code are minimal */
2088             if((ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
2089                             || (ps_dec->u1_chroma_format == IV_YUV_420SP_VU)
2090                             || (ps_dec->u1_chroma_format == IV_YUV_420P))
2091             {
2092                 u4_luma_size = 64;
2093             }
2094 
2095             if(ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
2096             {
2097                 u4_chroma_size = 64;
2098             }
2099 
2100         }
2101     }
2102 
2103     size = ALIGN64(u4_luma_size) + ALIGN64(u4_chroma_size);
2104     size *= ps_dec->u1_pic_bufs;
2105     pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2106     RETURN_IF((NULL == pv_buf), IV_FAIL);
2107     memset(pv_buf, 0, size);
2108     ps_dec->pu1_pic_buf_base = pv_buf;
2109 
2110     /* Post allocation Increment Actions */
2111 
2112     /***************************************************************************/
2113     /*Initialize cabac context pointers for every SE that has fixed contextIdx */
2114     /***************************************************************************/
2115     {
2116         bin_ctxt_model_t * const p_cabac_ctxt_table_t =
2117                         ps_dec->p_cabac_ctxt_table_t;
2118         bin_ctxt_model_t * * p_coeff_abs_level_minus1_t =
2119                         ps_dec->p_coeff_abs_level_minus1_t;
2120         bin_ctxt_model_t * * p_cbf_t = ps_dec->p_cbf_t;
2121 
2122         ps_dec->p_mb_field_dec_flag_t = p_cabac_ctxt_table_t
2123                         + MB_FIELD_DECODING_FLAG;
2124         ps_dec->p_prev_intra4x4_pred_mode_flag_t = p_cabac_ctxt_table_t
2125                         + PREV_INTRA4X4_PRED_MODE_FLAG;
2126         ps_dec->p_rem_intra4x4_pred_mode_t = p_cabac_ctxt_table_t
2127                         + REM_INTRA4X4_PRED_MODE;
2128         ps_dec->p_intra_chroma_pred_mode_t = p_cabac_ctxt_table_t
2129                         + INTRA_CHROMA_PRED_MODE;
2130         ps_dec->p_mb_qp_delta_t = p_cabac_ctxt_table_t + MB_QP_DELTA;
2131         ps_dec->p_ref_idx_t = p_cabac_ctxt_table_t + REF_IDX;
2132         ps_dec->p_mvd_x_t = p_cabac_ctxt_table_t + MVD_X;
2133         ps_dec->p_mvd_y_t = p_cabac_ctxt_table_t + MVD_Y;
2134         p_cbf_t[0] = p_cabac_ctxt_table_t + CBF + 0;
2135         p_cbf_t[1] = p_cabac_ctxt_table_t + CBF + 4;
2136         p_cbf_t[2] = p_cabac_ctxt_table_t + CBF + 8;
2137         p_cbf_t[3] = p_cabac_ctxt_table_t + CBF + 12;
2138         p_cbf_t[4] = p_cabac_ctxt_table_t + CBF + 16;
2139         ps_dec->p_cbp_luma_t = p_cabac_ctxt_table_t + CBP_LUMA;
2140         ps_dec->p_cbp_chroma_t = p_cabac_ctxt_table_t + CBP_CHROMA;
2141 
2142         p_coeff_abs_level_minus1_t[LUMA_DC_CTXCAT] = p_cabac_ctxt_table_t
2143                         + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_0_OFFSET;
2144 
2145         p_coeff_abs_level_minus1_t[LUMA_AC_CTXCAT] = p_cabac_ctxt_table_t
2146                         + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_1_OFFSET;
2147 
2148         p_coeff_abs_level_minus1_t[LUMA_4X4_CTXCAT] = p_cabac_ctxt_table_t
2149                         + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_2_OFFSET;
2150 
2151         p_coeff_abs_level_minus1_t[CHROMA_DC_CTXCAT] = p_cabac_ctxt_table_t
2152                         + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_3_OFFSET;
2153 
2154         p_coeff_abs_level_minus1_t[CHROMA_AC_CTXCAT] = p_cabac_ctxt_table_t
2155                         + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_4_OFFSET;
2156 
2157         p_coeff_abs_level_minus1_t[LUMA_8X8_CTXCAT] = p_cabac_ctxt_table_t
2158                         + COEFF_ABS_LEVEL_MINUS1_8X8
2159                         + COEFF_ABS_LEVEL_CAT_5_OFFSET;
2160 
2161         /********************************************************/
2162         /* context for the high profile related syntax elements */
2163         /* This is maintained seperately in s_high_profile     */
2164         /********************************************************/
2165         {
2166 
2167             ps_dec->s_high_profile.ps_transform8x8_flag = p_cabac_ctxt_table_t
2168                             + TRANSFORM_SIZE_8X8_FLAG;
2169 
2170             ps_dec->s_high_profile.ps_sigcoeff_8x8_frame = p_cabac_ctxt_table_t
2171                             + SIGNIFICANT_COEFF_FLAG_8X8_FRAME;
2172 
2173             ps_dec->s_high_profile.ps_last_sigcoeff_8x8_frame =
2174                             p_cabac_ctxt_table_t
2175                                             + LAST_SIGNIFICANT_COEFF_FLAG_8X8_FRAME;
2176 
2177             ps_dec->s_high_profile.ps_coeff_abs_levelminus1 =
2178                             p_cabac_ctxt_table_t + COEFF_ABS_LEVEL_MINUS1_8X8;
2179 
2180             ps_dec->s_high_profile.ps_sigcoeff_8x8_field = p_cabac_ctxt_table_t
2181                             + SIGNIFICANT_COEFF_FLAG_8X8_FIELD;
2182 
2183             ps_dec->s_high_profile.ps_last_sigcoeff_8x8_field =
2184                             p_cabac_ctxt_table_t
2185                                             + LAST_SIGNIFICANT_COEFF_FLAG_8X8_FIELD;
2186         }
2187     }
2188     return (i16_status);
2189 }
2190 
2191 /*!
2192  **************************************************************************
2193  * \if Function name : ih264d_free_dynamic_bufs \endif
2194  *
2195  * \brief
2196  *    This function frees dynamic memory allocated by Decoder.
2197  *
2198  * \param ps_dec: Pointer to dec_struct_t.
2199  *
2200  * \return
2201  *    Returns i4_status as returned by MemManager.
2202  *
2203  **************************************************************************
2204  */
ih264d_free_dynamic_bufs(dec_struct_t * ps_dec)2205 WORD16 ih264d_free_dynamic_bufs(dec_struct_t * ps_dec)
2206 {
2207     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_bits_buf_dynamic);
2208 
2209     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_deblk_pic);
2210     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_dec_mb_map);
2211     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_recon_mb_map);
2212     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu2_slice_num_map);
2213     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_dec_slice_buf);
2214     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_frm_mb_info);
2215     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pi2_coeff_data);
2216     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_parse_mb_data);
2217     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_parse_part_params);
2218     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_deblk_top_mb);
2219 
2220     if(ps_dec->p_ctxt_inc_mb_map)
2221     {
2222         ps_dec->p_ctxt_inc_mb_map -= 1;
2223         PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->p_ctxt_inc_mb_map);
2224     }
2225 
2226     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_mv_p[0]);
2227     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_mv_p[1]);
2228     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_pred_pkd);
2229     {
2230         UWORD8 i;
2231         for(i = 0; i < MV_SCRATCH_BUFS; i++)
2232         {
2233             PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_mv_top_p[i]);
2234         }
2235     }
2236 
2237     if(ps_dec->pu1_y_intra_pred_line)
2238     {
2239         ps_dec->pu1_y_intra_pred_line -= MB_SIZE;
2240     }
2241     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_y_intra_pred_line);
2242 
2243     if(ps_dec->pu1_u_intra_pred_line)
2244     {
2245         ps_dec->pu1_u_intra_pred_line -= MB_SIZE;
2246     }
2247     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_u_intra_pred_line);
2248 
2249     if(ps_dec->pu1_v_intra_pred_line)
2250     {
2251         ps_dec->pu1_v_intra_pred_line -= MB_SIZE;
2252     }
2253     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_v_intra_pred_line);
2254     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_nbr_mb_row);
2255     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_mv_bank_buf_base);
2256     PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_pic_buf_base);
2257     return 0;
2258 }
2259 
2260 /*!
2261  **************************************************************************
2262  * \if Function name : ih264d_create_mv_bank \endif
2263  *
2264  * \brief
2265  *    This function creates MV bank.
2266  *
2267  * \param memType  : Type of memory being handled
2268  *                   0: Display Buffer
2269  *                   1: Decoder Buffer
2270  *                   2: Internal Buffer
2271  * \param u1_num_of_buf: Number of decode or display buffers.
2272  * \param u4_wd : Frame width.
2273  * \param u4_ht : Frame Height.
2274  * \param ps_pic_buf_api : Pointer to Picture Buffer API.
2275  * \param ih264d_dec_mem_manager  : Memory manager utility supplied by system.
2276  *
2277  * \return
2278  *    0 on Success and -1 on error
2279  *
2280  **************************************************************************
2281  */
ih264d_create_mv_bank(void * pv_dec,UWORD32 ui_width,UWORD32 ui_height)2282 WORD32 ih264d_create_mv_bank(void *pv_dec,
2283                              UWORD32 ui_width,
2284                              UWORD32 ui_height)
2285 {
2286     UWORD8  i;
2287     UWORD32 col_flag_buffer_size, mvpred_buffer_size;
2288     UWORD8 *pu1_mv_buf_mgr_base, *pu1_mv_bank_base;
2289     col_mv_buf_t *ps_col_mv;
2290     mv_pred_t *ps_mv;
2291     UWORD8 *pu1_col_zero_flag_buf;
2292     dec_struct_t *ps_dec = (dec_struct_t *)pv_dec;
2293     WORD32 buf_ret;
2294     UWORD32 u4_num_bufs;
2295     UWORD8 *pu1_buf;
2296     WORD32 size;
2297     void *pv_mem_ctxt = ps_dec->pv_mem_ctxt;
2298 
2299     col_flag_buffer_size = ((ui_width * ui_height) >> 4);
2300     mvpred_buffer_size = sizeof(mv_pred_t)
2301                     * ((ui_width * (ui_height + PAD_MV_BANK_ROW)) >> 4);
2302 
2303     ih264_buf_mgr_init((buf_mgr_t *)ps_dec->pv_mv_buf_mgr);
2304 
2305     ps_col_mv = ps_dec->ps_col_mv_base;
2306 
2307     u4_num_bufs = ps_dec->ps_cur_sps->u1_num_ref_frames + 1;
2308 
2309     u4_num_bufs = MIN(u4_num_bufs, ps_dec->u1_pic_bufs);
2310     u4_num_bufs = MAX(u4_num_bufs, 2);
2311     pu1_buf = ps_dec->pu1_mv_bank_buf_base;
2312     for(i = 0 ; i < u4_num_bufs ; i++)
2313     {
2314         pu1_col_zero_flag_buf = pu1_buf;
2315         pu1_buf += ALIGN64(col_flag_buffer_size);
2316 
2317         ps_mv = (mv_pred_t *)pu1_buf;
2318         pu1_buf += ALIGN64(mvpred_buffer_size);
2319 
2320         memset(ps_mv, 0, ((ui_width * OFFSET_MV_BANK_ROW) >> 4) * sizeof(mv_pred_t));
2321         ps_mv += (ui_width*OFFSET_MV_BANK_ROW) >> 4;
2322 
2323         ps_col_mv->pv_col_zero_flag = (void *)pu1_col_zero_flag_buf;
2324         ps_col_mv->pv_mv = (void *)ps_mv;
2325         buf_ret = ih264_buf_mgr_add((buf_mgr_t *)ps_dec->pv_mv_buf_mgr, ps_col_mv, i);
2326         if(0 != buf_ret)
2327         {
2328             ps_dec->i4_error_code = ERROR_BUF_MGR;
2329             return ERROR_BUF_MGR;
2330         }
2331         ps_col_mv++;
2332     }
2333     return OK;
2334 }
2335 
ih264d_unpack_coeff4x4_dc_4x4blk(tu_sblk4x4_coeff_data_t * ps_tu_4x4,WORD16 * pi2_out_coeff_data,UWORD8 * pu1_inv_scan)2336 void ih264d_unpack_coeff4x4_dc_4x4blk(tu_sblk4x4_coeff_data_t *ps_tu_4x4,
2337                                       WORD16 *pi2_out_coeff_data,
2338                                       UWORD8 *pu1_inv_scan)
2339 {
2340     UWORD16 u2_sig_coeff_map = ps_tu_4x4->u2_sig_coeff_map;
2341     WORD32 idx;
2342     WORD16 *pi2_coeff_data = &ps_tu_4x4->ai2_level[0];
2343 
2344     while(u2_sig_coeff_map)
2345     {
2346         idx = CLZ(u2_sig_coeff_map);
2347 
2348         idx = 31 - idx;
2349         RESET_BIT(u2_sig_coeff_map,idx);
2350 
2351         idx = pu1_inv_scan[idx];
2352         pi2_out_coeff_data[idx] = *pi2_coeff_data++;
2353 
2354     }
2355 }
2356