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  **************************************************************************
23  * \file ih264d_parse_islice.c
24  *
25  * \brief
26  *    Contains routines that decode a I slice type
27  *
28  * Detailed_description
29  *
30  * \date
31  *    07/07/2003
32  *
33  * \author  NS
34  **************************************************************************
35  */
36 #include "ih264d_error_handler.h"
37 #include "ih264d_debug.h"
38 #include <string.h>
39 #include "ih264d_bitstrm.h"
40 #include "ih264d_defs.h"
41 #include "ih264d_debug.h"
42 #include "ih264d_tables.h"
43 #include "ih264d_structs.h"
44 #include "ih264d_defs.h"
45 #include "ih264d_parse_cavlc.h"
46 #include "ih264d_mb_utils.h"
47 #include "ih264d_deblocking.h"
48 #include "ih264d_cabac.h"
49 #include "ih264d_parse_cabac.h"
50 #include "ih264d_parse_mb_header.h"
51 #include "ih264d_parse_slice.h"
52 #include "ih264d_process_pslice.h"
53 #include "ih264d_process_intra_mb.h"
54 #include "ih264d_parse_islice.h"
55 #include "ih264d_error_handler.h"
56 #include "ih264d_mvpred.h"
57 #include "ih264d_defs.h"
58 #include "ih264d_thread_parse_decode.h"
59 #include "ithread.h"
60 #include "ih264d_parse_mb_header.h"
61 #include "assert.h"
62 #include "ih264d_utils.h"
63 #include "ih264d_format_conv.h"
64 
65 void ih264d_init_cabac_contexts(UWORD8 u1_slice_type, dec_struct_t * ps_dec);
66 
67 void ih264d_itrans_recon_luma_dc(dec_struct_t *ps_dec,
68                                  WORD16* pi2_src,
69                                  WORD16* pi2_coeff_block,
70                                  const UWORD16 *pu2_weigh_mat);
71 
72 
73 
74 /*!
75  **************************************************************************
76  * \if Function name : ParseIMb \endif
77  *
78  * \brief
79  *    This function parses CAVLC syntax of a I MB. If 16x16 Luma DC transform
80  *    is also done here. Transformed Luma DC values are copied in their
81  *    0th pixel location of corrosponding CoeffBlock.
82  *
83  * \return
84  *    0 on Success and Error code otherwise
85  **************************************************************************
86  */
ih264d_parse_imb_cavlc(dec_struct_t * ps_dec,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_mb_num,UWORD8 u1_mb_type)87 WORD32 ih264d_parse_imb_cavlc(dec_struct_t * ps_dec,
88                               dec_mb_info_t * ps_cur_mb_info,
89                               UWORD8 u1_mb_num,
90                               UWORD8 u1_mb_type)
91 {
92     WORD32 i4_delta_qp;
93     UWORD32 u4_temp;
94     UWORD32 ui_is_top_mb_available;
95     UWORD32 ui_is_left_mb_available;
96     UWORD32 u4_cbp;
97     UWORD32 u4_offset;
98     UWORD32 *pu4_bitstrm_buf;
99     WORD32 ret;
100 
101     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
102     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
103     UNUSED(u1_mb_num);
104     ps_cur_mb_info->u1_tran_form8x8 = 0;
105     ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
106 
107     ps_cur_mb_info->u1_yuv_dc_block_flag = 0;
108 
109     u4_temp = ps_dec->u1_mb_ngbr_availablity;
110     ui_is_top_mb_available = BOOLEAN(u4_temp & TOP_MB_AVAILABLE_MASK);
111     ui_is_left_mb_available = BOOLEAN(u4_temp & LEFT_MB_AVAILABLE_MASK);
112 
113     pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
114 
115     if(u1_mb_type == I_4x4_MB)
116     {
117         ps_cur_mb_info->ps_curmb->u1_mb_type = I_4x4_MB;
118         u4_offset = 0;
119 
120         /*--------------------------------------------------------------------*/
121         /* Read transform_size_8x8_flag if present                            */
122         /*--------------------------------------------------------------------*/
123         if(ps_dec->s_high_profile.u1_transform8x8_present)
124         {
125             ps_cur_mb_info->u1_tran_form8x8 = ih264d_get_bit_h264(ps_bitstrm);
126             COPYTHECONTEXT("transform_size_8x8_flag", ps_cur_mb_info->u1_tran_form8x8);
127             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = ps_cur_mb_info->u1_tran_form8x8;
128         }
129 
130         /*--------------------------------------------------------------------*/
131         /* Read the IntraPrediction modes for LUMA                            */
132         /*--------------------------------------------------------------------*/
133         if (!ps_cur_mb_info->u1_tran_form8x8)
134         {
135             UWORD8 *pu1_temp;
136             ih264d_read_intra_pred_modes(ps_dec,
137                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
138                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+16),
139                                           ps_cur_mb_info->u1_tran_form8x8);
140             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
141             pu1_temp += 32;
142             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
143         }
144         else
145         {
146             UWORD8 *pu1_temp;
147             ih264d_read_intra_pred_modes(ps_dec,
148                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
149                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+4),
150                                           ps_cur_mb_info->u1_tran_form8x8);
151             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
152             pu1_temp += 8;
153             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
154         }
155         /*--------------------------------------------------------------------*/
156         /* Read the IntraPrediction mode for CHROMA                           */
157         /*--------------------------------------------------------------------*/
158 //Inlined ih264d_uev
159         {
160             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
161             UWORD32 u4_word, u4_ldz, u4_temp;
162 
163             /***************************************************************/
164             /* Find leading zeros in next 32 bits                          */
165             /***************************************************************/
166             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
167             u4_ldz = CLZ(u4_word);
168             /* Flush the ps_bitstrm */
169             u4_bitstream_offset += (u4_ldz + 1);
170             /* Read the suffix from the ps_bitstrm */
171             u4_word = 0;
172             if(u4_ldz)
173             {
174                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
175                         u4_ldz);
176             }
177             *pu4_bitstrm_ofst = u4_bitstream_offset;
178             u4_temp = ((1 << u4_ldz) + u4_word - 1);
179             if(u4_temp > 3)
180             {
181                 return ERROR_CHROMA_PRED_MODE;
182             }
183             ps_cur_mb_info->u1_chroma_pred_mode = u4_temp;
184             COPYTHECONTEXT("intra_chroma_pred_mode", ps_cur_mb_info->u1_chroma_pred_mode);
185         }
186         /*--------------------------------------------------------------------*/
187         /* Read the Coded block pattern                                       */
188         /*--------------------------------------------------------------------*/
189         {
190             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
191             UWORD32 u4_word, u4_ldz;
192 
193             /***************************************************************/
194             /* Find leading zeros in next 32 bits                          */
195             /***************************************************************/
196             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
197             u4_ldz = CLZ(u4_word);
198             /* Flush the ps_bitstrm */
199             u4_bitstream_offset += (u4_ldz + 1);
200             /* Read the suffix from the ps_bitstrm */
201             u4_word = 0;
202             if(u4_ldz)
203             {
204                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
205                         u4_ldz);
206             }
207             *pu4_bitstrm_ofst = u4_bitstream_offset;
208             u4_cbp = ((1 << u4_ldz) + u4_word - 1);
209         }
210         if(u4_cbp > 47)
211         {
212             return ERROR_CBP;
213         }
214 
215         u4_cbp = gau1_ih264d_cbp_table[u4_cbp][0];
216         COPYTHECONTEXT("coded_block_pattern", u1_cbp);
217         ps_cur_mb_info->u1_cbp = u4_cbp;
218 
219         /*--------------------------------------------------------------------*/
220         /* Read mb_qp_delta                                                   */
221         /*--------------------------------------------------------------------*/
222         if(ps_cur_mb_info->u1_cbp)
223         {
224             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
225             UWORD32 u4_word, u4_ldz, u4_abs_val;
226 
227             /***************************************************************/
228             /* Find leading zeros in next 32 bits                          */
229             /***************************************************************/
230             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
231             u4_ldz = CLZ(u4_word);
232 
233             /* Flush the ps_bitstrm */
234             u4_bitstream_offset += (u4_ldz + 1);
235 
236             /* Read the suffix from the ps_bitstrm */
237             u4_word = 0;
238             if(u4_ldz)
239             {
240                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
241                         u4_ldz);
242             }
243 
244             *pu4_bitstrm_ofst = u4_bitstream_offset;
245             u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
246 
247             if(u4_word & 0x1)
248             {
249                 i4_delta_qp = (-(WORD32)u4_abs_val);
250             }
251             else
252             {
253                 i4_delta_qp = (u4_abs_val);
254             }
255 
256             if((i4_delta_qp < -26) || (i4_delta_qp > 25))
257             {
258                 return ERROR_INV_RANGE_QP_T;
259             }
260 
261             COPYTHECONTEXT("mb_qp_delta", i1_delta_qp);
262             if(i4_delta_qp != 0)
263             {
264                 ret = ih264d_update_qp(ps_dec, (WORD8)i4_delta_qp);
265                 if(ret != OK)
266                     return ret;
267             }
268         }
269 
270     }
271     else
272     {
273         u4_offset = 1;
274         ps_cur_mb_info->ps_curmb->u1_mb_type = I_16x16_MB;
275         /*-------------------------------------------------------------------*/
276         /* Read the IntraPrediction mode for CHROMA                          */
277         /*-------------------------------------------------------------------*/
278         {
279             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
280             UWORD32 u4_word, u4_ldz;
281 
282             /***************************************************************/
283             /* Find leading zeros in next 32 bits                          */
284             /***************************************************************/
285             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
286             u4_ldz = CLZ(u4_word);
287             /* Flush the ps_bitstrm */
288             u4_bitstream_offset += (u4_ldz + 1);
289             /* Read the suffix from the ps_bitstrm */
290             u4_word = 0;
291             if(u4_ldz)
292             {
293                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
294                         u4_ldz);
295             }
296             *pu4_bitstrm_ofst = u4_bitstream_offset;
297             u4_temp = ((1 << u4_ldz) + u4_word - 1);
298 
299 //Inlined ih264d_uev
300 
301             if(u4_temp > 3)
302             {
303                 return ERROR_CHROMA_PRED_MODE;
304             }
305             ps_cur_mb_info->u1_chroma_pred_mode = u4_temp;
306             COPYTHECONTEXT("intra_chroma_pred_mode", ps_cur_mb_info->u1_chroma_pred_mode);
307         }
308         /*-------------------------------------------------------------------*/
309         /* Read the Coded block pattern                                      */
310         /*-------------------------------------------------------------------*/
311         u4_cbp = gau1_ih264d_cbp_tab[(u1_mb_type - 1) >> 2];
312         ps_cur_mb_info->u1_cbp = u4_cbp;
313 
314         /*-------------------------------------------------------------------*/
315         /* Read mb_qp_delta                                                  */
316         /*-------------------------------------------------------------------*/
317         {
318             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
319             UWORD32 u4_word, u4_ldz, u4_abs_val;
320 
321             /***************************************************************/
322             /* Find leading zeros in next 32 bits                          */
323             /***************************************************************/
324             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
325             u4_ldz = CLZ(u4_word);
326 
327             /* Flush the ps_bitstrm */
328             u4_bitstream_offset += (u4_ldz + 1);
329 
330             /* Read the suffix from the ps_bitstrm */
331             u4_word = 0;
332             if(u4_ldz)
333                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
334                         u4_ldz);
335 
336             *pu4_bitstrm_ofst = u4_bitstream_offset;
337             u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
338 
339             if(u4_word & 0x1)
340                 i4_delta_qp = (-(WORD32)u4_abs_val);
341             else
342                 i4_delta_qp = (u4_abs_val);
343 
344             if((i4_delta_qp < -26) || (i4_delta_qp > 25))
345                 return ERROR_INV_RANGE_QP_T;
346 
347         }
348 //inlinined ih264d_sev
349         COPYTHECONTEXT("Delta quant", i1_delta_qp);
350 
351         if(i4_delta_qp != 0)
352         {
353             ret = ih264d_update_qp(ps_dec, (WORD8)i4_delta_qp);
354             if(ret != OK)
355                 return ret;
356         }
357 
358         {
359             WORD16 i_scaleFactor;
360             UWORD32 ui_N = 0;
361             WORD16 *pi2_scale_matrix_ptr;
362             /*******************************************************************/
363             /* for luma DC coefficients the scaling is done during the parsing */
364             /* to preserve the precision                                       */
365             /*******************************************************************/
366             if(ps_dec->s_high_profile.u1_scaling_present)
367             {
368                 pi2_scale_matrix_ptr =
369                                 ps_dec->s_high_profile.i2_scalinglist4x4[0];
370             }
371             else
372             {
373                 i_scaleFactor = 16;
374                 pi2_scale_matrix_ptr = &i_scaleFactor;
375             }
376 
377             /*---------------------------------------------------------------*/
378             /* Decode DC coefficients                                        */
379             /*---------------------------------------------------------------*/
380             /*---------------------------------------------------------------*/
381             /* Calculation of N                                              */
382             /*---------------------------------------------------------------*/
383             if(ui_is_left_mb_available)
384             {
385 
386                 if(ui_is_top_mb_available)
387                 {
388                     ui_N = ((ps_cur_mb_info->ps_top_mb->pu1_nnz_y[0]
389                                     + ps_dec->pu1_left_nnz_y[0] + 1) >> 1);
390                 }
391                 else
392                 {
393                     ui_N = ps_dec->pu1_left_nnz_y[0];
394                 }
395             }
396             else if(ui_is_top_mb_available)
397             {
398                 ui_N = ps_cur_mb_info->ps_top_mb->pu1_nnz_y[0];
399             }
400 
401             {
402                 WORD16 pi2_dc_coef[16];
403                 WORD32 pi4_tmp[16];
404                 tu_sblk4x4_coeff_data_t *ps_tu_4x4 =
405                                 (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
406                 WORD16 *pi2_coeff_block =
407                                 (WORD16 *)ps_dec->pv_parse_tu_coeff_data;
408                 UWORD32 u4_num_coeff;
409                 ps_tu_4x4->u2_sig_coeff_map = 0;
410 
411                 ret = ps_dec->pf_cavlc_parse4x4coeff[(ui_N > 7)](pi2_dc_coef, 0, ui_N,
412                                                                  ps_dec, &u4_num_coeff);
413                 if(ret != OK)
414                     return ret;
415 
416                 if(EXCEED_OFFSET(ps_bitstrm))
417                     return ERROR_EOB_TERMINATE_T;
418                 if(ps_tu_4x4->u2_sig_coeff_map)
419                 {
420                     memset(pi2_dc_coef,0,sizeof(pi2_dc_coef));
421                     ih264d_unpack_coeff4x4_dc_4x4blk(ps_tu_4x4,
422                                                      pi2_dc_coef,
423                                                      ps_dec->pu1_inv_scan);
424 
425                     PROFILE_DISABLE_IQ_IT_RECON()
426                     ps_dec->pf_ihadamard_scaling_4x4(pi2_dc_coef,
427                                                      pi2_coeff_block,
428                                                      ps_dec->pu2_quant_scale_y,
429                                                      (UWORD16 *)pi2_scale_matrix_ptr,
430                                                      ps_dec->u1_qp_y_div6,
431                                                      pi4_tmp);
432                     pi2_coeff_block += 16;
433                     ps_dec->pv_parse_tu_coeff_data = (void *)pi2_coeff_block;
434                     SET_BIT(ps_cur_mb_info->u1_yuv_dc_block_flag,0);
435                 }
436 
437             }
438         }
439     }
440 
441 
442     if(u4_cbp)
443     {
444 
445         ret = ih264d_parse_residual4x4_cavlc(ps_dec, ps_cur_mb_info,
446                                        (UWORD8)u4_offset);
447         if(ret != OK)
448             return ret;
449         if(EXCEED_OFFSET(ps_bitstrm))
450             return ERROR_EOB_TERMINATE_T;
451 
452         /* Store Left Mb NNZ and TOP chroma NNZ */
453     }
454     else
455     {
456         ps_cur_mb_info->u1_qp_div6 = ps_dec->u1_qp_y_div6;
457         ps_cur_mb_info->u1_qpc_div6 = ps_dec->u1_qp_u_div6;
458         ps_cur_mb_info->u1_qpcr_div6 = ps_dec->u1_qp_v_div6;
459         ps_cur_mb_info->u1_qp_rem6 = ps_dec->u1_qp_y_rem6;
460         ps_cur_mb_info->u1_qpc_rem6 = ps_dec->u1_qp_u_rem6;
461         ps_cur_mb_info->u1_qpcr_rem6 = ps_dec->u1_qp_v_rem6;
462         ih264d_update_nnz_for_skipmb(ps_dec, ps_cur_mb_info, CAVLC);
463     }
464 
465     return OK;
466 }
467 
468 /*!
469  **************************************************************************
470  * \if Function name : ParseIMbCab \endif
471  *
472  * \brief
473  *    This function parses CABAC syntax of a I MB. If 16x16 Luma DC transform
474  *    is also done here. Transformed Luma DC values are copied in their
475  *    0th pixel location of corrosponding CoeffBlock.
476  *
477  * \return
478  *    0 on Success and Error code otherwise
479  **************************************************************************
480  */
ih264d_parse_imb_cabac(dec_struct_t * ps_dec,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_mb_type)481 WORD32 ih264d_parse_imb_cabac(dec_struct_t * ps_dec,
482                               dec_mb_info_t * ps_cur_mb_info,
483                               UWORD8 u1_mb_type)
484 {
485     WORD8 i1_delta_qp;
486     UWORD8 u1_cbp;
487     UWORD8 u1_offset;
488     /* Variables for handling Cabac contexts */
489     ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
490     ctxt_inc_mb_info_t *ps_left_ctxt = ps_dec->p_left_ctxt_mb_info;
491     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
492     bin_ctxt_model_t *p_bin_ctxt;
493 
494     UWORD8 u1_intra_chrom_pred_mode;
495     UWORD8 u1_dc_block_flag = 0;
496     WORD32 ret;
497 
498     ps_cur_mb_info->u1_yuv_dc_block_flag = 0;
499 
500     if(ps_left_ctxt == ps_dec->ps_def_ctxt_mb_info)
501     {
502         ps_dec->pu1_left_yuv_dc_csbp[0] = 0xf;
503     }
504 
505     if(ps_dec->ps_cur_slice->u1_slice_type != I_SLICE)
506     {
507         WORD32 *pi4_buf;
508         WORD8 *pi1_buf;
509         MEMSET_16BYTES(&ps_dec->pu1_left_mv_ctxt_inc[0][0], 0);
510         *((UWORD32 *)ps_dec->pi1_left_ref_idx_ctxt_inc) = 0;
511         MEMSET_16BYTES(p_curr_ctxt->u1_mv, 0);
512         pi1_buf = p_curr_ctxt->i1_ref_idx;
513         pi4_buf = (WORD32 *)pi1_buf;
514         *pi4_buf = 0;
515     }
516 
517     if(u1_mb_type == I_4x4_MB)
518     {
519         ps_cur_mb_info->ps_curmb->u1_mb_type = I_4x4_MB;
520         p_curr_ctxt->u1_mb_type = CAB_I4x4;
521         u1_offset = 0;
522 
523         ps_cur_mb_info->u1_tran_form8x8 = 0;
524         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
525 
526         /*--------------------------------------------------------------------*/
527         /* Read transform_size_8x8_flag if present                            */
528         /*--------------------------------------------------------------------*/
529         if(ps_dec->s_high_profile.u1_transform8x8_present)
530         {
531             ps_cur_mb_info->u1_tran_form8x8 = ih264d_parse_transform8x8flag_cabac(
532                             ps_dec, ps_cur_mb_info);
533             COPYTHECONTEXT("transform_size_8x8_flag", ps_cur_mb_info->u1_tran_form8x8);
534             p_curr_ctxt->u1_transform8x8_ctxt = ps_cur_mb_info->u1_tran_form8x8;
535             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = ps_cur_mb_info->u1_tran_form8x8;
536         }
537         else
538         {
539             p_curr_ctxt->u1_transform8x8_ctxt = 0;
540         }
541 
542         /*--------------------------------------------------------------------*/
543         /* Read the IntraPrediction modes for LUMA                            */
544         /*--------------------------------------------------------------------*/
545         if (!ps_cur_mb_info->u1_tran_form8x8)
546         {
547             UWORD8 *pu1_temp;
548             ih264d_read_intra_pred_modes_cabac(
549                             ps_dec,
550                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
551                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+16),
552                             ps_cur_mb_info->u1_tran_form8x8);
553             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
554             pu1_temp += 32;
555             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
556         }
557         else
558         {
559             UWORD8 *pu1_temp;
560             ih264d_read_intra_pred_modes_cabac(
561                             ps_dec,
562                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
563                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+4),
564                             ps_cur_mb_info->u1_tran_form8x8);
565             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
566             pu1_temp += 8;
567             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
568         }
569         /*--------------------------------------------------------------------*/
570         /* Read the IntraPrediction mode for CHROMA                           */
571         /*--------------------------------------------------------------------*/
572         u1_intra_chrom_pred_mode = ih264d_parse_chroma_pred_mode_cabac(ps_dec);
573         COPYTHECONTEXT("intra_chroma_pred_mode", u1_intra_chrom_pred_mode);
574         p_curr_ctxt->u1_intra_chroma_pred_mode = ps_cur_mb_info->u1_chroma_pred_mode =
575                         u1_intra_chrom_pred_mode;
576 
577         /*--------------------------------------------------------------------*/
578         /* Read the Coded block pattern                                       */
579         /*--------------------------------------------------------------------*/
580         u1_cbp = ih264d_parse_ctx_cbp_cabac(ps_dec);
581         COPYTHECONTEXT("coded_block_pattern", u1_cbp);
582         ps_cur_mb_info->u1_cbp = u1_cbp;
583         p_curr_ctxt->u1_cbp = u1_cbp;
584 
585         /*--------------------------------------------------------------------*/
586         /* Read mb_qp_delta                                                   */
587         /*--------------------------------------------------------------------*/
588         if(ps_cur_mb_info->u1_cbp)
589         {
590             ret = ih264d_parse_mb_qp_delta_cabac(ps_dec, &i1_delta_qp);
591             if(ret != OK)
592                 return ret;
593             COPYTHECONTEXT("mb_qp_delta", i1_delta_qp);
594             if(i1_delta_qp != 0)
595             {
596                 ret = ih264d_update_qp(ps_dec, i1_delta_qp);
597                 if(ret != OK)
598                     return ret;
599             }
600         }
601         else
602             ps_dec->i1_prev_mb_qp_delta = 0;
603         p_curr_ctxt->u1_yuv_dc_csbp &= 0xFE;
604     }
605     else
606     {
607         u1_offset = 1;
608         ps_cur_mb_info->ps_curmb->u1_mb_type = I_16x16_MB;
609         p_curr_ctxt->u1_mb_type = CAB_I16x16;
610         ps_cur_mb_info->u1_tran_form8x8 = 0;
611         p_curr_ctxt->u1_transform8x8_ctxt = 0;
612         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
613         /*--------------------------------------------------------------------*/
614         /* Read the IntraPrediction mode for CHROMA                           */
615         /*--------------------------------------------------------------------*/
616         u1_intra_chrom_pred_mode = ih264d_parse_chroma_pred_mode_cabac(ps_dec);
617         if(u1_intra_chrom_pred_mode > 3)
618             return ERROR_CHROMA_PRED_MODE;
619 
620         COPYTHECONTEXT("Chroma intra_chroma_pred_mode pred mode", u1_intra_chrom_pred_mode);
621         p_curr_ctxt->u1_intra_chroma_pred_mode = ps_cur_mb_info->u1_chroma_pred_mode =
622                         u1_intra_chrom_pred_mode;
623 
624         /*--------------------------------------------------------------------*/
625         /* Read the Coded block pattern                                       */
626         /*--------------------------------------------------------------------*/
627         u1_cbp = gau1_ih264d_cbp_tab[(u1_mb_type - 1) >> 2];
628         ps_cur_mb_info->u1_cbp = u1_cbp;
629         p_curr_ctxt->u1_cbp = u1_cbp;
630 
631         /*--------------------------------------------------------------------*/
632         /* Read mb_qp_delta                                                   */
633         /*--------------------------------------------------------------------*/
634         ret = ih264d_parse_mb_qp_delta_cabac(ps_dec, &i1_delta_qp);
635         if(ret != OK)
636             return ret;
637         COPYTHECONTEXT("mb_qp_delta", i1_delta_qp);
638         if(i1_delta_qp != 0)
639         {
640             ret = ih264d_update_qp(ps_dec, i1_delta_qp);
641             if(ret != OK)
642                 return ret;
643         }
644 
645         {
646             WORD16 i_scaleFactor;
647             WORD16* pi2_scale_matrix_ptr;
648             /*******************************************************************/
649             /* for luma DC coefficients the scaling is done during the parsing */
650             /* to preserve the precision                                       */
651             /*******************************************************************/
652             if(ps_dec->s_high_profile.u1_scaling_present)
653             {
654                 pi2_scale_matrix_ptr =
655                                 ps_dec->s_high_profile.i2_scalinglist4x4[0];
656 
657             }
658             else
659             {
660                 i_scaleFactor = 16;
661                 pi2_scale_matrix_ptr = &i_scaleFactor;
662             }
663             {
664                 ctxt_inc_mb_info_t *ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
665                 UWORD8 uc_a, uc_b;
666                 UWORD32 u4_ctx_inc;
667 
668                 INC_SYM_COUNT(&(ps_dec->s_cab_dec_env));
669 
670                 /* if MbAddrN not available then CondTermN = 1 */
671                 uc_b = ((ps_top_ctxt->u1_yuv_dc_csbp) & 0x01);
672 
673                 /* if MbAddrN not available then CondTermN = 1 */
674                 uc_a = ((ps_dec->pu1_left_yuv_dc_csbp[0]) & 0x01);
675 
676                 u4_ctx_inc = (uc_a + (uc_b << 1));
677 
678                 {
679                     WORD16 pi2_dc_coef[16];
680                     tu_sblk4x4_coeff_data_t *ps_tu_4x4 =
681                                     (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
682                     WORD16 *pi2_coeff_block =
683                                     (WORD16 *)ps_dec->pv_parse_tu_coeff_data;
684 
685                     p_bin_ctxt = (ps_dec->p_cbf_t[LUMA_DC_CTXCAT]) + u4_ctx_inc;
686 
687                     u1_dc_block_flag =
688                                     ih264d_read_coeff4x4_cabac(ps_bitstrm,
689                                                     LUMA_DC_CTXCAT,
690                                                     ps_dec->p_significant_coeff_flag_t[LUMA_DC_CTXCAT],
691                                                     ps_dec, p_bin_ctxt);
692 
693                     /* Store coded_block_flag */
694                     p_curr_ctxt->u1_yuv_dc_csbp &= 0xFE;
695                     p_curr_ctxt->u1_yuv_dc_csbp |= u1_dc_block_flag;
696                     if(u1_dc_block_flag)
697                     {
698                         WORD32 pi4_tmp[16];
699                         memset(pi2_dc_coef,0,sizeof(pi2_dc_coef));
700                         ih264d_unpack_coeff4x4_dc_4x4blk(ps_tu_4x4,
701                                                          pi2_dc_coef,
702                                                          ps_dec->pu1_inv_scan);
703 
704                         PROFILE_DISABLE_IQ_IT_RECON()
705                         ps_dec->pf_ihadamard_scaling_4x4(pi2_dc_coef,
706                                                          pi2_coeff_block,
707                                                          ps_dec->pu2_quant_scale_y,
708                                                          (UWORD16 *)pi2_scale_matrix_ptr,
709                                                          ps_dec->u1_qp_y_div6,
710                                                          pi4_tmp);
711                         pi2_coeff_block += 16;
712                         ps_dec->pv_parse_tu_coeff_data = (void *)pi2_coeff_block;
713                         SET_BIT(ps_cur_mb_info->u1_yuv_dc_block_flag,0);
714                     }
715 
716                 }
717 
718             }
719         }
720     }
721 
722     ps_dec->pu1_left_yuv_dc_csbp[0] &= 0x6;
723     ps_dec->pu1_left_yuv_dc_csbp[0] |= u1_dc_block_flag;
724 
725     ih264d_parse_residual4x4_cabac(ps_dec, ps_cur_mb_info, u1_offset);
726     if(EXCEED_OFFSET(ps_bitstrm))
727         return ERROR_EOB_TERMINATE_T;
728     return OK;
729 }
730 
731 /*****************************************************************************/
732 /*                                                                           */
733 /*  Function Name : ih264d_parse_islice_data_cavlc                                  */
734 /*                                                                           */
735 /*  Description   : This function parses cabac syntax of a inter slice on    */
736 /*                  N MB basis.                                              */
737 /*                                                                           */
738 /*  Inputs        : ps_dec                                                   */
739 /*                  sliceparams                                              */
740 /*                  firstMbInSlice                                           */
741 /*                                                                           */
742 /*  Processing    : 1. After parsing syntax for N MBs those N MBs are        */
743 /*                     decoded till the end of slice.                        */
744 /*                                                                           */
745 /*  Returns       : 0                                                        */
746 /*                                                                           */
747 /*  Issues        : <List any issues or problems with this function>         */
748 /*                                                                           */
749 /*  Revision History:                                                        */
750 /*                                                                           */
751 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
752 /*         24 06 2005   ARNY            Draft                                */
753 /*                                                                           */
754 /*****************************************************************************/
ih264d_parse_islice_data_cavlc(dec_struct_t * ps_dec,dec_slice_params_t * ps_slice,UWORD16 u2_first_mb_in_slice)755 WORD32 ih264d_parse_islice_data_cavlc(dec_struct_t * ps_dec,
756                                       dec_slice_params_t * ps_slice,
757                                       UWORD16 u2_first_mb_in_slice)
758 {
759     UWORD8 uc_more_data_flag;
760     UWORD8 u1_num_mbs, u1_mb_idx;
761     dec_mb_info_t *ps_cur_mb_info;
762     deblk_mb_t *ps_cur_deblk_mb;
763     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
764     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
765     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
766     UWORD16 i2_pic_wdin_mbs = ps_dec->u2_frm_wd_in_mbs;
767     WORD16 i2_cur_mb_addr;
768     UWORD8 u1_mbaff;
769     UWORD8 u1_num_mbs_next, u1_end_of_row, u1_tfr_n_mb;
770     WORD32 ret = OK;
771 
772     ps_dec->u1_qp = ps_slice->u1_slice_qp;
773     ih264d_update_qp(ps_dec, 0);
774     u1_mbaff = ps_slice->u1_mbaff_frame_flag;
775 
776     /* initializations */
777     u1_mb_idx = ps_dec->u1_mb_idx;
778     u1_num_mbs = u1_mb_idx;
779 
780     uc_more_data_flag = 1;
781     i2_cur_mb_addr = u2_first_mb_in_slice << u1_mbaff;
782 
783     do
784     {
785         UWORD8 u1_mb_type;
786 
787         ps_dec->pv_prev_mb_parse_tu_coeff_data = ps_dec->pv_parse_tu_coeff_data;
788 
789         if(i2_cur_mb_addr > ps_dec->ps_cur_sps->u2_max_mb_addr)
790         {
791             break;
792         }
793 
794         ps_cur_mb_info = ps_dec->ps_nmb_info + u1_num_mbs;
795         ps_dec->u4_num_mbs_cur_nmb = u1_num_mbs;
796         ps_dec->u4_num_pmbair = (u1_num_mbs >> u1_mbaff);
797 
798         ps_cur_mb_info->u1_end_of_slice = 0;
799 
800         /***************************************************************/
801         /* Get the required information for decoding of MB             */
802         /* mb_x, mb_y , neighbour availablity,                         */
803         /***************************************************************/
804         ps_dec->pf_get_mb_info(ps_dec, i2_cur_mb_addr, ps_cur_mb_info, 0);
805 
806         /***************************************************************/
807         /* Set the deblocking parameters for this MB                   */
808         /***************************************************************/
809         ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + u1_num_mbs;
810 
811         if(ps_dec->u4_app_disable_deblk_frm == 0)
812             ih264d_set_deblocking_parameters(ps_cur_deblk_mb, ps_slice,
813                                              ps_dec->u1_mb_ngbr_availablity,
814                                              ps_dec->u1_cur_mb_fld_dec_flag);
815 
816         ps_cur_deblk_mb->u1_mb_type = ps_cur_deblk_mb->u1_mb_type | D_INTRA_MB;
817 
818         /**************************************************************/
819         /* Macroblock Layer Begins, Decode the u1_mb_type                */
820         /**************************************************************/
821 //Inlined ih264d_uev
822         {
823             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
824             UWORD32 u4_word, u4_ldz, u4_temp;
825 
826             /***************************************************************/
827             /* Find leading zeros in next 32 bits                          */
828             /***************************************************************/
829             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
830             u4_ldz = CLZ(u4_word);
831             /* Flush the ps_bitstrm */
832             u4_bitstream_offset += (u4_ldz + 1);
833             /* Read the suffix from the ps_bitstrm */
834             u4_word = 0;
835             if(u4_ldz)
836                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
837                         u4_ldz);
838             *pu4_bitstrm_ofst = u4_bitstream_offset;
839             u4_temp = ((1 << u4_ldz) + u4_word - 1);
840             if(u4_temp > 25)
841                 return ERROR_MB_TYPE;
842             u1_mb_type = u4_temp;
843 
844         }
845 //Inlined ih264d_uev
846         ps_cur_mb_info->u1_mb_type = u1_mb_type;
847         COPYTHECONTEXT("u1_mb_type", u1_mb_type);
848 
849         /**************************************************************/
850         /* Parse Macroblock data                                      */
851         /**************************************************************/
852         if(25 == u1_mb_type)
853         {
854             /* I_PCM_MB */
855             ps_cur_mb_info->ps_curmb->u1_mb_type = I_PCM_MB;
856             ret = ih264d_parse_ipcm_mb(ps_dec, ps_cur_mb_info, u1_num_mbs);
857             if(ret != OK)
858                 return ret;
859             ps_cur_deblk_mb->u1_mb_qp = 0;
860         }
861         else
862         {
863             ret = ih264d_parse_imb_cavlc(ps_dec, ps_cur_mb_info, u1_num_mbs, u1_mb_type);
864             if(ret != OK)
865                 return ret;
866             ps_cur_deblk_mb->u1_mb_qp = ps_dec->u1_qp;
867         }
868 
869         if(u1_mbaff)
870         {
871             ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
872         }
873         /**************************************************************/
874         /* Get next Macroblock address                                */
875         /**************************************************************/
876 
877         i2_cur_mb_addr++;
878         uc_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
879 
880         /* Store the colocated information */
881         {
882             mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_num_mbs << 4);
883 
884             mv_pred_t s_mvPred =
885                 {
886                     { 0, 0, 0, 0 },
887                       { -1, -1 }, 0, 0};
888             ih264d_rep_mv_colz(ps_dec, &s_mvPred, ps_mv_nmb_start, 0,
889                                (UWORD8)(ps_dec->u1_cur_mb_fld_dec_flag << 1), 4,
890                                4);
891         }
892 
893         /*if num _cores is set to 3,compute bs will be done in another thread*/
894         if(ps_dec->u4_num_cores < 3)
895         {
896             if(ps_dec->u4_app_disable_deblk_frm == 0)
897                 ps_dec->pf_compute_bs(ps_dec, ps_cur_mb_info,
898                                      (UWORD16)(u1_num_mbs >> u1_mbaff));
899         }
900         u1_num_mbs++;
901 
902         /****************************************************************/
903         /* Check for End Of Row                                         */
904         /****************************************************************/
905         u1_num_mbs_next = i2_pic_wdin_mbs - ps_dec->u2_mbx - 1;
906         u1_end_of_row = (!u1_num_mbs_next) && (!(u1_mbaff && (u1_num_mbs & 0x01)));
907         u1_tfr_n_mb = (u1_num_mbs == ps_dec->u1_recon_mb_grp) || u1_end_of_row
908                         || (!uc_more_data_flag);
909         ps_cur_mb_info->u1_end_of_slice = (!uc_more_data_flag);
910 
911         /*H264_DEC_DEBUG_PRINT("Pic: %d Mb_X=%d Mb_Y=%d",
912          ps_slice->i4_poc >> ps_slice->u1_field_pic_flag,
913          ps_dec->u2_mbx,ps_dec->u2_mby + (1 - ps_cur_mb_info->u1_topmb));
914          H264_DEC_DEBUG_PRINT("u1_tfr_n_mb || (!uc_more_data_flag): %d", u1_tfr_n_mb || (!uc_more_data_flag));*/
915         if(u1_tfr_n_mb || (!uc_more_data_flag))
916         {
917 
918             if(ps_dec->u1_separate_parse)
919             {
920                 ih264d_parse_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
921                                      u1_num_mbs_next, u1_tfr_n_mb, u1_end_of_row);
922                 ps_dec->ps_nmb_info +=  u1_num_mbs;
923             }
924             else
925             {
926                 ih264d_decode_recon_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
927                                             u1_num_mbs_next, u1_tfr_n_mb,
928                                             u1_end_of_row);
929             }
930             ps_dec->u2_total_mbs_coded += u1_num_mbs;
931             if(u1_tfr_n_mb)
932                 u1_num_mbs = 0;
933             u1_mb_idx = u1_num_mbs;
934             ps_dec->u1_mb_idx = u1_num_mbs;
935 
936         }
937     }
938     while(uc_more_data_flag);
939 
940     ps_dec->u4_num_mbs_cur_nmb = 0;
941     ps_dec->ps_cur_slice->u4_mbs_in_slice = i2_cur_mb_addr
942 
943                         - (u2_first_mb_in_slice << u1_mbaff);
944 
945     return ret;
946 }
947 
948 /*****************************************************************************/
949 /*                                                                           */
950 /*  Function Name : ih264d_parse_islice_data_cabac                                  */
951 /*                                                                           */
952 /*  Description   : This function parses cabac syntax of a inter slice on    */
953 /*                  N MB basis.                                              */
954 /*                                                                           */
955 /*  Inputs        : ps_dec                                                   */
956 /*                  sliceparams                                              */
957 /*                  firstMbInSlice                                           */
958 /*                                                                           */
959 /*  Processing    : 1. After parsing syntax for N MBs those N MBs are        */
960 /*                     decoded till the end of slice.                        */
961 /*                                                                           */
962 /*  Returns       : 0                                                        */
963 /*                                                                           */
964 /*  Issues        : <List any issues or problems with this function>         */
965 /*                                                                           */
966 /*  Revision History:                                                        */
967 /*                                                                           */
968 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
969 /*         24 06 2005   ARNY            Draft                                */
970 /*                                                                           */
971 /*****************************************************************************/
ih264d_parse_islice_data_cabac(dec_struct_t * ps_dec,dec_slice_params_t * ps_slice,UWORD16 u2_first_mb_in_slice)972 WORD32 ih264d_parse_islice_data_cabac(dec_struct_t * ps_dec,
973                                       dec_slice_params_t * ps_slice,
974                                       UWORD16 u2_first_mb_in_slice)
975 {
976     UWORD8 uc_more_data_flag;
977     UWORD8 u1_num_mbs, u1_mb_idx;
978     dec_mb_info_t *ps_cur_mb_info;
979     deblk_mb_t *ps_cur_deblk_mb;
980 
981     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
982     UWORD16 i2_pic_wdin_mbs = ps_dec->u2_frm_wd_in_mbs;
983     WORD16 i2_cur_mb_addr;
984     UWORD8 u1_mbaff;
985     UWORD8 u1_num_mbs_next, u1_end_of_row, u1_tfr_n_mb;
986     WORD32 ret = OK;
987 
988     ps_dec->u1_qp = ps_slice->u1_slice_qp;
989     ih264d_update_qp(ps_dec, 0);
990     u1_mbaff = ps_slice->u1_mbaff_frame_flag;
991 
992     if(ps_bitstrm->u4_ofst & 0x07)
993     {
994         ps_bitstrm->u4_ofst += 8;
995         ps_bitstrm->u4_ofst &= 0xFFFFFFF8;
996     }
997     ret = ih264d_init_cabac_dec_envirnoment(&(ps_dec->s_cab_dec_env), ps_bitstrm);
998     if(ret != OK)
999         return ret;
1000     ih264d_init_cabac_contexts(I_SLICE, ps_dec);
1001 
1002     ps_dec->i1_prev_mb_qp_delta = 0;
1003 
1004     /* initializations */
1005     u1_mb_idx = ps_dec->u1_mb_idx;
1006     u1_num_mbs = u1_mb_idx;
1007 
1008     uc_more_data_flag = 1;
1009     i2_cur_mb_addr = u2_first_mb_in_slice << u1_mbaff;
1010     do
1011     {
1012         UWORD16 u2_mbx;
1013 
1014         ps_dec->pv_prev_mb_parse_tu_coeff_data = ps_dec->pv_parse_tu_coeff_data;
1015 
1016         if(i2_cur_mb_addr > ps_dec->ps_cur_sps->u2_max_mb_addr)
1017         {
1018             break;
1019         }
1020 
1021         {
1022             UWORD8 u1_mb_type;
1023 
1024             ps_cur_mb_info = ps_dec->ps_nmb_info + u1_num_mbs;
1025             ps_dec->u4_num_mbs_cur_nmb = u1_num_mbs;
1026             ps_dec->u4_num_pmbair = (u1_num_mbs >> u1_mbaff);
1027 
1028             ps_cur_mb_info->u1_end_of_slice = 0;
1029 
1030             /***************************************************************/
1031             /* Get the required information for decoding of MB                  */
1032             /* mb_x, mb_y , neighbour availablity,                              */
1033             /***************************************************************/
1034             ps_dec->pf_get_mb_info(ps_dec, i2_cur_mb_addr, ps_cur_mb_info, 0);
1035             u2_mbx = ps_dec->u2_mbx;
1036 
1037             /*********************************************************************/
1038             /* initialize u1_tran_form8x8 to zero to aviod uninitialized accesses */
1039             /*********************************************************************/
1040             ps_cur_mb_info->u1_tran_form8x8 = 0;
1041             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
1042 
1043             /***************************************************************/
1044             /* Set the deblocking parameters for this MB                   */
1045             /***************************************************************/
1046             ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + u1_num_mbs;
1047             if(ps_dec->u4_app_disable_deblk_frm == 0)
1048                 ih264d_set_deblocking_parameters(
1049                                 ps_cur_deblk_mb, ps_slice,
1050                                 ps_dec->u1_mb_ngbr_availablity,
1051                                 ps_dec->u1_cur_mb_fld_dec_flag);
1052 
1053             ps_cur_deblk_mb->u1_mb_type = ps_cur_deblk_mb->u1_mb_type
1054                             | D_INTRA_MB;
1055 
1056             /* Macroblock Layer Begins */
1057             /* Decode the u1_mb_type */
1058             u1_mb_type = ih264d_parse_mb_type_intra_cabac(0, ps_dec);
1059             if(u1_mb_type > 25)
1060                 return ERROR_MB_TYPE;
1061             ps_cur_mb_info->u1_mb_type = u1_mb_type;
1062             COPYTHECONTEXT("u1_mb_type", u1_mb_type);
1063 
1064             /* Parse Macroblock Data */
1065             if(25 == u1_mb_type)
1066             {
1067                 /* I_PCM_MB */
1068                 ps_cur_mb_info->ps_curmb->u1_mb_type = I_PCM_MB;
1069                 ret = ih264d_parse_ipcm_mb(ps_dec, ps_cur_mb_info, u1_num_mbs);
1070                 if(ret != OK)
1071                     return ret;
1072                 ps_cur_deblk_mb->u1_mb_qp = 0;
1073             }
1074             else
1075             {
1076                 ret = ih264d_parse_imb_cabac(ps_dec, ps_cur_mb_info, u1_mb_type);
1077                 if(ret != OK)
1078                     return ret;
1079                 ps_cur_deblk_mb->u1_mb_qp = ps_dec->u1_qp;
1080             }
1081 
1082             if(u1_mbaff)
1083             {
1084                 ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
1085             }
1086             /* Next macroblock information */
1087             i2_cur_mb_addr++;
1088 
1089             if(ps_cur_mb_info->u1_topmb && u1_mbaff)
1090                 uc_more_data_flag = 1;
1091             else
1092             {
1093                 uc_more_data_flag = ih264d_decode_terminate(&ps_dec->s_cab_dec_env,
1094                                                           ps_bitstrm);
1095                 uc_more_data_flag = !uc_more_data_flag;
1096                 COPYTHECONTEXT("Decode Sliceterm",!uc_more_data_flag);
1097             }
1098             /* Store the colocated information */
1099             {
1100 
1101                 mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_num_mbs << 4);
1102                 mv_pred_t s_mvPred =
1103                     {
1104                         { 0, 0, 0, 0 },
1105                           { -1, -1 }, 0, 0};
1106                 ih264d_rep_mv_colz(
1107                                 ps_dec, &s_mvPred, ps_mv_nmb_start, 0,
1108                                 (UWORD8)(ps_dec->u1_cur_mb_fld_dec_flag << 1),
1109                                 4, 4);
1110             }
1111             /*if num _cores is set to 3,compute bs will be done in another thread*/
1112             if(ps_dec->u4_num_cores < 3)
1113             {
1114                 if(ps_dec->u4_app_disable_deblk_frm == 0)
1115                     ps_dec->pf_compute_bs(ps_dec, ps_cur_mb_info,
1116                                          (UWORD16)(u1_num_mbs >> u1_mbaff));
1117             }
1118             u1_num_mbs++;
1119 
1120         }
1121 
1122         /****************************************************************/
1123         /* Check for End Of Row                                         */
1124         /****************************************************************/
1125         u1_num_mbs_next = i2_pic_wdin_mbs - u2_mbx - 1;
1126         u1_end_of_row = (!u1_num_mbs_next) && (!(u1_mbaff && (u1_num_mbs & 0x01)));
1127         u1_tfr_n_mb = (u1_num_mbs == ps_dec->u1_recon_mb_grp) || u1_end_of_row
1128                         || (!uc_more_data_flag);
1129         ps_cur_mb_info->u1_end_of_slice = (!uc_more_data_flag);
1130 
1131         if(u1_tfr_n_mb || (!uc_more_data_flag))
1132         {
1133 
1134 
1135             if(ps_dec->u1_separate_parse)
1136             {
1137                 ih264d_parse_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
1138                                      u1_num_mbs_next, u1_tfr_n_mb, u1_end_of_row);
1139                 ps_dec->ps_nmb_info +=  u1_num_mbs;
1140             }
1141             else
1142             {
1143                 ih264d_decode_recon_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
1144                                             u1_num_mbs_next, u1_tfr_n_mb,
1145                                             u1_end_of_row);
1146             }
1147             ps_dec->u2_total_mbs_coded += u1_num_mbs;
1148             if(u1_tfr_n_mb)
1149                 u1_num_mbs = 0;
1150             u1_mb_idx = u1_num_mbs;
1151             ps_dec->u1_mb_idx = u1_num_mbs;
1152 
1153         }
1154     }
1155     while(uc_more_data_flag);
1156 
1157     ps_dec->u4_num_mbs_cur_nmb = 0;
1158     ps_dec->ps_cur_slice->u4_mbs_in_slice = i2_cur_mb_addr
1159 
1160                         - (u2_first_mb_in_slice << u1_mbaff);
1161 
1162     return ret;
1163 }
1164 
1165 /*****************************************************************************/
1166 /*                                                                           */
1167 /*  Function Name : ih264d_parse_ipcm_mb                                       */
1168 /*                                                                           */
1169 /*  Description   : This function decodes the pixel values of I_PCM Mb.      */
1170 /*                                                                           */
1171 /*  Inputs        : ps_dec,  ps_cur_mb_info and mb number                          */
1172 /*                                                                           */
1173 /*  Description   : This function reads the luma and chroma pixels directly  */
1174 /*                  from the bitstream when the mbtype is I_PCM and stores   */
1175 /*                  them in recon buffer. If the entropy coding mode is      */
1176 /*                  cabac, decoding engine is re-initialized. The nnzs and   */
1177 /*                  cabac contexts are appropriately modified.               */
1178 /*  Returns       : void                                                     */
1179 /*                                                                           */
1180 /*  Revision History:                                                        */
1181 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1182 /*         13 07 2002   Jay                                                  */
1183 /*                                                                           */
1184 /*****************************************************************************/
1185 
ih264d_parse_ipcm_mb(dec_struct_t * ps_dec,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_mbNum)1186 WORD32 ih264d_parse_ipcm_mb(dec_struct_t * ps_dec,
1187                           dec_mb_info_t *ps_cur_mb_info,
1188                           UWORD8 u1_mbNum)
1189 {
1190     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1191     UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
1192     UWORD8 *pu1_y, *pu1_u, *pu1_v;
1193     WORD32 ret;
1194 
1195     UWORD32 u4_rec_width_y, u4_rec_width_uv;
1196     UWORD32 u1_num_mb_pair;
1197     UWORD8 u1_x, u1_y;
1198     /* CHANGED CODE */
1199     tfr_ctxt_t *ps_frame_buf;
1200     UWORD8 u1_mb_field_decoding_flag;
1201     UWORD32 *pu4_buf;
1202     UWORD8 *pu1_buf;
1203     /* CHANGED CODE */
1204 
1205     if(ps_dec->u1_separate_parse)
1206     {
1207         ps_frame_buf = &ps_dec->s_tran_addrecon_parse;
1208     }
1209     else
1210     {
1211         ps_frame_buf = &ps_dec->s_tran_addrecon;
1212     }
1213     /* align bistream to byte boundary. */
1214     /* pcm_alignment_zero_bit discarded */
1215     /* For XX GotoByteBoundary */
1216     if(ps_bitstrm->u4_ofst & 0x07)
1217     {
1218         ps_bitstrm->u4_ofst += 8;
1219         ps_bitstrm->u4_ofst &= 0xFFFFFFF8;
1220     }
1221 
1222     /*  Store left Nnz as 16 for each 4x4 blk */
1223 
1224     pu1_buf = ps_dec->pu1_left_nnz_y;
1225     pu4_buf = (UWORD32 *)pu1_buf;
1226     *pu4_buf = 0x10101010;
1227     pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
1228     pu4_buf = (UWORD32 *)pu1_buf;
1229     *pu4_buf = 0x10101010;
1230     pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
1231     pu4_buf = (UWORD32 *)pu1_buf;
1232     *pu4_buf = 0x10101010;
1233     pu1_buf = ps_dec->pu1_left_nnz_uv;
1234     pu4_buf = (UWORD32 *)pu1_buf;
1235     *pu4_buf = 0x10101010;
1236     ps_cur_mb_info->u1_cbp = 0xff;
1237 
1238     ps_dec->i1_prev_mb_qp_delta = 0;
1239     /* Get neighbour MB's */
1240     u1_num_mb_pair = (u1_mbNum >> u1_mbaff);
1241 
1242     /*****************************************************************************/
1243     /* calculate the RECON buffer YUV pointers for the PCM data                  */
1244     /*****************************************************************************/
1245     /* CHANGED CODE  */
1246     u1_mb_field_decoding_flag = ps_cur_mb_info->u1_mb_field_decodingflag;
1247     pu1_y = ps_frame_buf->pu1_dest_y + (u1_num_mb_pair << 4);
1248     pu1_u = ps_frame_buf->pu1_dest_u + (u1_num_mb_pair << 4);
1249     pu1_v = pu1_u + 1;
1250 
1251     u4_rec_width_y = ps_dec->u2_frm_wd_y << u1_mb_field_decoding_flag;
1252     u4_rec_width_uv = ps_dec->u2_frm_wd_uv << u1_mb_field_decoding_flag;
1253     /* CHANGED CODE  */
1254 
1255     if(u1_mbaff)
1256     {
1257         UWORD8 u1_top_mb;
1258 
1259         u1_top_mb = ps_cur_mb_info->u1_topmb;
1260 
1261         if(u1_top_mb == 0)
1262         {
1263             pu1_y += (u1_mb_field_decoding_flag ?
1264                             (u4_rec_width_y >> 1) : (u4_rec_width_y << 4));
1265             pu1_u += (u1_mb_field_decoding_flag ?
1266                             (u4_rec_width_uv) : (u4_rec_width_uv << 4));
1267             pu1_v = pu1_u + 1;
1268         }
1269     }
1270 
1271     /* Read Luma samples */
1272     for(u1_y = 0; u1_y < 16; u1_y++)
1273     {
1274         for(u1_x = 0; u1_x < 16; u1_x++)
1275             pu1_y[u1_x] = ih264d_get_bits_h264(ps_bitstrm, 8);
1276 
1277         pu1_y += u4_rec_width_y;
1278     }
1279 
1280     /* Read Chroma samples */
1281     for(u1_y = 0; u1_y < 8; u1_y++)
1282     {
1283         for(u1_x = 0; u1_x < 8; u1_x++)
1284             pu1_u[u1_x * YUV420SP_FACTOR] = ih264d_get_bits_h264(ps_bitstrm, 8);
1285 
1286         pu1_u += u4_rec_width_uv;
1287     }
1288 
1289     for(u1_y = 0; u1_y < 8; u1_y++)
1290     {
1291         for(u1_x = 0; u1_x < 8; u1_x++)
1292             pu1_v[u1_x * YUV420SP_FACTOR] = ih264d_get_bits_h264(ps_bitstrm, 8);
1293 
1294         pu1_v += u4_rec_width_uv;
1295     }
1296 
1297     if(CABAC == ps_dec->ps_cur_pps->u1_entropy_coding_mode)
1298     {
1299         UWORD32 *pu4_buf;
1300         UWORD8 *pu1_buf;
1301         ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
1302         /* Re-initialize the cabac decoding engine. */
1303         ret = ih264d_init_cabac_dec_envirnoment(&(ps_dec->s_cab_dec_env), ps_bitstrm);
1304         if(ret != OK)
1305             return ret;
1306         /* update the cabac contetxs */
1307         p_curr_ctxt->u1_mb_type = CAB_I_PCM;
1308         p_curr_ctxt->u1_cbp = 47;
1309         p_curr_ctxt->u1_intra_chroma_pred_mode = 0;
1310         p_curr_ctxt->u1_transform8x8_ctxt = 0;
1311         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
1312 
1313         pu1_buf = ps_dec->pu1_left_nnz_y;
1314         pu4_buf = (UWORD32 *)pu1_buf;
1315         *pu4_buf = 0x01010101;
1316 
1317         pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
1318         pu4_buf = (UWORD32 *)pu1_buf;
1319         *pu4_buf = 0x01010101;
1320 
1321         pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
1322         pu4_buf = (UWORD32 *)pu1_buf;
1323         *pu4_buf = 0x01010101;
1324 
1325         pu1_buf = ps_dec->pu1_left_nnz_uv;
1326         pu4_buf = (UWORD32 *)pu1_buf;
1327         *pu4_buf = 0x01010101;
1328 
1329         p_curr_ctxt->u1_yuv_dc_csbp = 0x7;
1330         ps_dec->pu1_left_yuv_dc_csbp[0] = 0x7;
1331         if(ps_dec->ps_cur_slice->u1_slice_type != I_SLICE)
1332         {
1333 
1334             MEMSET_16BYTES(&ps_dec->pu1_left_mv_ctxt_inc[0][0], 0);
1335             memset(ps_dec->pi1_left_ref_idx_ctxt_inc, 0, 4);
1336             MEMSET_16BYTES(p_curr_ctxt->u1_mv, 0);
1337             memset(p_curr_ctxt->i1_ref_idx, 0, 4);
1338 
1339         }
1340     }
1341     return OK;
1342 }
1343 
1344 /*!
1345  **************************************************************************
1346  * \if Function name : ih264d_decode_islice \endif
1347  *
1348  * \brief
1349  *    Decodes an I Slice
1350  *
1351  *
1352  * \return
1353  *    0 on Success and Error code otherwise
1354  **************************************************************************
1355  */
ih264d_parse_islice(dec_struct_t * ps_dec,UWORD16 u2_first_mb_in_slice)1356 WORD32 ih264d_parse_islice(dec_struct_t *ps_dec,
1357                             UWORD16 u2_first_mb_in_slice)
1358 {
1359     dec_pic_params_t * ps_pps = ps_dec->ps_cur_pps;
1360     dec_slice_params_t * ps_slice = ps_dec->ps_cur_slice;
1361     UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
1362     UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;
1363     UWORD32 u4_temp;
1364     WORD32 i_temp;
1365     WORD32 ret;
1366 
1367     /*--------------------------------------------------------------------*/
1368     /* Read remaining contents of the slice header                        */
1369     /*--------------------------------------------------------------------*/
1370     /* dec_ref_pic_marking function */
1371     /* G050 */
1372     if(ps_slice->u1_nal_ref_idc != 0)
1373     {
1374         if(!ps_dec->ps_dpb_cmds->u1_dpb_commands_read)
1375         {
1376             i_temp = ih264d_read_mmco_commands(ps_dec);
1377             if (i_temp < 0)
1378             {
1379                 return ERROR_DBP_MANAGER_T;
1380             }
1381             ps_dec->u4_bitoffset = i_temp;
1382         }
1383         else
1384             ps_dec->ps_bitstrm->u4_ofst += ps_dec->u4_bitoffset;
1385     }
1386     /* G050 */
1387 
1388     /* Read slice_qp_delta */
1389     i_temp = ps_pps->u1_pic_init_qp
1390                     + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1391     if((i_temp < 0) || (i_temp > 51))
1392         return ERROR_INV_RANGE_QP_T;
1393     ps_slice->u1_slice_qp = i_temp;
1394     COPYTHECONTEXT("SH: slice_qp_delta",
1395                     ps_slice->u1_slice_qp - ps_pps->u1_pic_init_qp);
1396 
1397     if(ps_pps->u1_deblocking_filter_parameters_present_flag == 1)
1398     {
1399         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1400         COPYTHECONTEXT("SH: disable_deblocking_filter_idc", u4_temp);
1401 
1402         if(u4_temp > SLICE_BOUNDARY_DBLK_DISABLED)
1403         {
1404             return ERROR_INV_SLICE_HDR_T;
1405         }
1406         ps_slice->u1_disable_dblk_filter_idc = u4_temp;
1407         if(u4_temp != 1)
1408         {
1409             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
1410                             << 1;
1411             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
1412             {
1413                 return ERROR_INV_SLICE_HDR_T;
1414             }
1415             ps_slice->i1_slice_alpha_c0_offset = i_temp;
1416             COPYTHECONTEXT("SH: slice_alpha_c0_offset_div2",
1417                             ps_slice->i1_slice_alpha_c0_offset >> 1);
1418 
1419             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
1420                             << 1;
1421             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
1422             {
1423                 return ERROR_INV_SLICE_HDR_T;
1424             }
1425             ps_slice->i1_slice_beta_offset = i_temp;
1426             COPYTHECONTEXT("SH: slice_beta_offset_div2",
1427                             ps_slice->i1_slice_beta_offset >> 1);
1428 
1429         }
1430         else
1431         {
1432             ps_slice->i1_slice_alpha_c0_offset = 0;
1433             ps_slice->i1_slice_beta_offset = 0;
1434         }
1435     }
1436     else
1437     {
1438         ps_slice->u1_disable_dblk_filter_idc = 0;
1439         ps_slice->i1_slice_alpha_c0_offset = 0;
1440         ps_slice->i1_slice_beta_offset = 0;
1441     }
1442 
1443     /* Initialization to check if number of motion vector per 2 Mbs */
1444     /* are exceeding the range or not */
1445     ps_dec->u2_mv_2mb[0] = 0;
1446     ps_dec->u2_mv_2mb[1] = 0;
1447 
1448 
1449     /*set slice header cone to 2 ,to indicate  correct header*/
1450     ps_dec->u1_slice_header_done = 2;
1451 
1452     if(ps_pps->u1_entropy_coding_mode)
1453     {
1454         SWITCHOFFTRACE; SWITCHONTRACECABAC;
1455         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
1456         {
1457             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_mbaff;
1458         }
1459         else
1460             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_nonmbaff;
1461 
1462         ret = ih264d_parse_islice_data_cabac(ps_dec, ps_slice,
1463                                              u2_first_mb_in_slice);
1464         if(ret != OK)
1465             return ret;
1466         SWITCHONTRACE; SWITCHOFFTRACECABAC;
1467     }
1468     else
1469     {
1470         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
1471         {
1472             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_mbaff;
1473         }
1474         else
1475             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_nonmbaff;
1476         ret = ih264d_parse_islice_data_cavlc(ps_dec, ps_slice,
1477                                        u2_first_mb_in_slice);
1478         if(ret != OK)
1479             return ret;
1480     }
1481 
1482     return OK;
1483 }
1484