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