xref: /aosp_15_r20/external/libavc/decoder/ih264d_parse_islice.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /*!
22  **************************************************************************
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         if(ps_dec->u1_enable_mb_info)
869         {
870             ih264d_populate_mb_info_map(ps_dec, ps_cur_mb_info, ps_cur_mb_info->u2_mbx << 1,
871                                         ps_cur_mb_info->u2_mby << 1, ps_cur_deblk_mb->u1_mb_qp);
872         }
873 
874         uc_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
875 
876         if(u1_mbaff)
877         {
878             ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
879             if(!uc_more_data_flag && (0 == (i2_cur_mb_addr & 1)))
880             {
881                 return ERROR_EOB_FLUSHBITS_T;
882             }
883         }
884         /**************************************************************/
885         /* Get next Macroblock address                                */
886         /**************************************************************/
887 
888         i2_cur_mb_addr++;
889 
890 
891         /* Store the colocated information */
892         {
893             mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_num_mbs << 4);
894 
895             mv_pred_t s_mvPred =
896                 {
897                     { 0, 0, 0, 0 },
898                       { -1, -1 }, 0, 0};
899             ih264d_rep_mv_colz(ps_dec, &s_mvPred, ps_mv_nmb_start, 0,
900                                (UWORD8)(ps_dec->u1_cur_mb_fld_dec_flag << 1), 4,
901                                4);
902         }
903 
904         /*if num _cores is set to 3,compute bs will be done in another thread*/
905         if(ps_dec->u4_num_cores < 3)
906         {
907             if(ps_dec->u4_app_disable_deblk_frm == 0)
908                 ps_dec->pf_compute_bs(ps_dec, ps_cur_mb_info,
909                                      (UWORD16)(u1_num_mbs >> u1_mbaff));
910         }
911         u1_num_mbs++;
912 
913         /****************************************************************/
914         /* Check for End Of Row                                         */
915         /****************************************************************/
916         u1_num_mbs_next = i2_pic_wdin_mbs - ps_dec->u2_mbx - 1;
917         u1_end_of_row = (!u1_num_mbs_next) && (!(u1_mbaff && (u1_num_mbs & 0x01)));
918         u1_tfr_n_mb = (u1_num_mbs == ps_dec->u1_recon_mb_grp) || u1_end_of_row
919                         || (!uc_more_data_flag);
920         ps_cur_mb_info->u1_end_of_slice = (!uc_more_data_flag);
921 
922         /*H264_DEC_DEBUG_PRINT("Pic: %d Mb_X=%d Mb_Y=%d",
923          ps_slice->i4_poc >> ps_slice->u1_field_pic_flag,
924          ps_dec->u2_mbx,ps_dec->u2_mby + (1 - ps_cur_mb_info->u1_topmb));
925          H264_DEC_DEBUG_PRINT("u1_tfr_n_mb || (!uc_more_data_flag): %d", u1_tfr_n_mb || (!uc_more_data_flag));*/
926         if(u1_tfr_n_mb || (!uc_more_data_flag))
927         {
928 
929             if(ps_dec->u1_separate_parse)
930             {
931                 ih264d_parse_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
932                                      u1_num_mbs_next, u1_tfr_n_mb, u1_end_of_row);
933                 ps_dec->ps_nmb_info +=  u1_num_mbs;
934             }
935             else
936             {
937                 ih264d_decode_recon_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
938                                             u1_num_mbs_next, u1_tfr_n_mb,
939                                             u1_end_of_row);
940             }
941             ps_dec->u2_total_mbs_coded += u1_num_mbs;
942             if(u1_tfr_n_mb)
943                 u1_num_mbs = 0;
944             u1_mb_idx = u1_num_mbs;
945             ps_dec->u1_mb_idx = u1_num_mbs;
946 
947         }
948     }
949     while(uc_more_data_flag);
950 
951     ps_dec->u4_num_mbs_cur_nmb = 0;
952     ps_dec->ps_cur_slice->u4_mbs_in_slice = i2_cur_mb_addr
953 
954                         - (u2_first_mb_in_slice << u1_mbaff);
955 
956     return ret;
957 }
958 
959 /*****************************************************************************/
960 /*                                                                           */
961 /*  Function Name : ih264d_parse_islice_data_cabac                                  */
962 /*                                                                           */
963 /*  Description   : This function parses cabac syntax of a inter slice on    */
964 /*                  N MB basis.                                              */
965 /*                                                                           */
966 /*  Inputs        : ps_dec                                                   */
967 /*                  sliceparams                                              */
968 /*                  firstMbInSlice                                           */
969 /*                                                                           */
970 /*  Processing    : 1. After parsing syntax for N MBs those N MBs are        */
971 /*                     decoded till the end of slice.                        */
972 /*                                                                           */
973 /*  Returns       : 0                                                        */
974 /*                                                                           */
975 /*  Issues        : <List any issues or problems with this function>         */
976 /*                                                                           */
977 /*  Revision History:                                                        */
978 /*                                                                           */
979 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
980 /*         24 06 2005   ARNY            Draft                                */
981 /*                                                                           */
982 /*****************************************************************************/
ih264d_parse_islice_data_cabac(dec_struct_t * ps_dec,dec_slice_params_t * ps_slice,UWORD16 u2_first_mb_in_slice)983 WORD32 ih264d_parse_islice_data_cabac(dec_struct_t * ps_dec,
984                                       dec_slice_params_t * ps_slice,
985                                       UWORD16 u2_first_mb_in_slice)
986 {
987     UWORD8 uc_more_data_flag;
988     UWORD8 u1_num_mbs, u1_mb_idx;
989     dec_mb_info_t *ps_cur_mb_info;
990     deblk_mb_t *ps_cur_deblk_mb;
991 
992     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
993     UWORD16 i2_pic_wdin_mbs = ps_dec->u2_frm_wd_in_mbs;
994     WORD16 i2_cur_mb_addr;
995     UWORD8 u1_mbaff;
996     UWORD8 u1_num_mbs_next, u1_end_of_row, u1_tfr_n_mb;
997     WORD32 ret = OK;
998 
999     ps_dec->u1_qp = ps_slice->u1_slice_qp;
1000     ih264d_update_qp(ps_dec, 0);
1001     u1_mbaff = ps_slice->u1_mbaff_frame_flag;
1002 
1003     if(ps_bitstrm->u4_ofst & 0x07)
1004     {
1005         ps_bitstrm->u4_ofst += 8;
1006         ps_bitstrm->u4_ofst &= 0xFFFFFFF8;
1007     }
1008     ret = ih264d_init_cabac_dec_envirnoment(&(ps_dec->s_cab_dec_env), ps_bitstrm);
1009     if(ret != OK)
1010         return ret;
1011     ih264d_init_cabac_contexts(I_SLICE, ps_dec);
1012 
1013     ps_dec->i1_prev_mb_qp_delta = 0;
1014 
1015     /* initializations */
1016     u1_mb_idx = ps_dec->u1_mb_idx;
1017     u1_num_mbs = u1_mb_idx;
1018 
1019     uc_more_data_flag = 1;
1020     i2_cur_mb_addr = u2_first_mb_in_slice << u1_mbaff;
1021     do
1022     {
1023         UWORD16 u2_mbx;
1024 
1025         ps_dec->pv_prev_mb_parse_tu_coeff_data = ps_dec->pv_parse_tu_coeff_data;
1026 
1027         if(i2_cur_mb_addr > ps_dec->ps_cur_sps->u2_max_mb_addr)
1028         {
1029             break;
1030         }
1031 
1032         {
1033             UWORD8 u1_mb_type;
1034 
1035             ps_cur_mb_info = ps_dec->ps_nmb_info + u1_num_mbs;
1036             ps_dec->u4_num_mbs_cur_nmb = u1_num_mbs;
1037             ps_dec->u4_num_pmbair = (u1_num_mbs >> u1_mbaff);
1038 
1039             ps_cur_mb_info->u1_end_of_slice = 0;
1040 
1041             /***************************************************************/
1042             /* Get the required information for decoding of MB                  */
1043             /* mb_x, mb_y , neighbour availablity,                              */
1044             /***************************************************************/
1045             ps_dec->pf_get_mb_info(ps_dec, i2_cur_mb_addr, ps_cur_mb_info, 0);
1046             u2_mbx = ps_dec->u2_mbx;
1047 
1048             /*********************************************************************/
1049             /* initialize u1_tran_form8x8 to zero to aviod uninitialized accesses */
1050             /*********************************************************************/
1051             ps_cur_mb_info->u1_tran_form8x8 = 0;
1052             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
1053 
1054             /***************************************************************/
1055             /* Set the deblocking parameters for this MB                   */
1056             /***************************************************************/
1057             ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + u1_num_mbs;
1058             if(ps_dec->u4_app_disable_deblk_frm == 0)
1059                 ih264d_set_deblocking_parameters(
1060                                 ps_cur_deblk_mb, ps_slice,
1061                                 ps_dec->u1_mb_ngbr_availablity,
1062                                 ps_dec->u1_cur_mb_fld_dec_flag);
1063 
1064             ps_cur_deblk_mb->u1_mb_type = ps_cur_deblk_mb->u1_mb_type
1065                             | D_INTRA_MB;
1066 
1067             /* Macroblock Layer Begins */
1068             /* Decode the u1_mb_type */
1069             u1_mb_type = ih264d_parse_mb_type_intra_cabac(0, ps_dec);
1070             if(u1_mb_type > 25)
1071                 return ERROR_MB_TYPE;
1072             ps_cur_mb_info->u1_mb_type = u1_mb_type;
1073             COPYTHECONTEXT("u1_mb_type", u1_mb_type);
1074 
1075             /* Parse Macroblock Data */
1076             if(25 == u1_mb_type)
1077             {
1078                 /* I_PCM_MB */
1079                 ps_cur_mb_info->ps_curmb->u1_mb_type = I_PCM_MB;
1080                 ret = ih264d_parse_ipcm_mb(ps_dec, ps_cur_mb_info, u1_num_mbs);
1081                 if(ret != OK)
1082                     return ret;
1083                 ps_cur_deblk_mb->u1_mb_qp = 0;
1084             }
1085             else
1086             {
1087                 ret = ih264d_parse_imb_cabac(ps_dec, ps_cur_mb_info, u1_mb_type);
1088                 if(ret != OK)
1089                     return ret;
1090                 ps_cur_deblk_mb->u1_mb_qp = ps_dec->u1_qp;
1091             }
1092 
1093             if(ps_dec->u1_enable_mb_info)
1094             {
1095                 ih264d_populate_mb_info_map(ps_dec, ps_cur_mb_info, ps_cur_mb_info->u2_mbx << 1,
1096                                             ps_cur_mb_info->u2_mby << 1, ps_cur_deblk_mb->u1_mb_qp);
1097             }
1098 
1099             if(u1_mbaff)
1100             {
1101                 ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
1102             }
1103 
1104 
1105             if(ps_cur_mb_info->u1_topmb && u1_mbaff)
1106                 uc_more_data_flag = 1;
1107             else
1108             {
1109                 uc_more_data_flag = ih264d_decode_terminate(&ps_dec->s_cab_dec_env,
1110                                                           ps_bitstrm);
1111                 uc_more_data_flag = !uc_more_data_flag;
1112                 COPYTHECONTEXT("Decode Sliceterm",!uc_more_data_flag);
1113             }
1114 
1115             if(u1_mbaff)
1116             {
1117                 if(!uc_more_data_flag && (0 == (i2_cur_mb_addr & 1)))
1118                 {
1119                     return ERROR_EOB_FLUSHBITS_T;
1120                 }
1121             }
1122             /* Next macroblock information */
1123             i2_cur_mb_addr++;
1124             /* Store the colocated information */
1125             {
1126 
1127                 mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_num_mbs << 4);
1128                 mv_pred_t s_mvPred =
1129                     {
1130                         { 0, 0, 0, 0 },
1131                           { -1, -1 }, 0, 0};
1132                 ih264d_rep_mv_colz(
1133                                 ps_dec, &s_mvPred, ps_mv_nmb_start, 0,
1134                                 (UWORD8)(ps_dec->u1_cur_mb_fld_dec_flag << 1),
1135                                 4, 4);
1136             }
1137             /*if num _cores is set to 3,compute bs will be done in another thread*/
1138             if(ps_dec->u4_num_cores < 3)
1139             {
1140                 if(ps_dec->u4_app_disable_deblk_frm == 0)
1141                     ps_dec->pf_compute_bs(ps_dec, ps_cur_mb_info,
1142                                          (UWORD16)(u1_num_mbs >> u1_mbaff));
1143             }
1144             u1_num_mbs++;
1145 
1146         }
1147 
1148         /****************************************************************/
1149         /* Check for End Of Row                                         */
1150         /****************************************************************/
1151         u1_num_mbs_next = i2_pic_wdin_mbs - u2_mbx - 1;
1152         u1_end_of_row = (!u1_num_mbs_next) && (!(u1_mbaff && (u1_num_mbs & 0x01)));
1153         u1_tfr_n_mb = (u1_num_mbs == ps_dec->u1_recon_mb_grp) || u1_end_of_row
1154                         || (!uc_more_data_flag);
1155         ps_cur_mb_info->u1_end_of_slice = (!uc_more_data_flag);
1156 
1157         if(u1_tfr_n_mb || (!uc_more_data_flag))
1158         {
1159 
1160 
1161             if(ps_dec->u1_separate_parse)
1162             {
1163                 ih264d_parse_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
1164                                      u1_num_mbs_next, u1_tfr_n_mb, u1_end_of_row);
1165                 ps_dec->ps_nmb_info +=  u1_num_mbs;
1166             }
1167             else
1168             {
1169                 ih264d_decode_recon_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
1170                                             u1_num_mbs_next, u1_tfr_n_mb,
1171                                             u1_end_of_row);
1172             }
1173             ps_dec->u2_total_mbs_coded += u1_num_mbs;
1174             if(u1_tfr_n_mb)
1175                 u1_num_mbs = 0;
1176             u1_mb_idx = u1_num_mbs;
1177             ps_dec->u1_mb_idx = u1_num_mbs;
1178 
1179         }
1180     }
1181     while(uc_more_data_flag);
1182 
1183     ps_dec->u4_num_mbs_cur_nmb = 0;
1184     ps_dec->ps_cur_slice->u4_mbs_in_slice = i2_cur_mb_addr
1185 
1186                         - (u2_first_mb_in_slice << u1_mbaff);
1187 
1188     return ret;
1189 }
1190 
1191 /*****************************************************************************/
1192 /*                                                                           */
1193 /*  Function Name : ih264d_parse_ipcm_mb                                       */
1194 /*                                                                           */
1195 /*  Description   : This function decodes the pixel values of I_PCM Mb.      */
1196 /*                                                                           */
1197 /*  Inputs        : ps_dec,  ps_cur_mb_info and mb number                          */
1198 /*                                                                           */
1199 /*  Description   : This function reads the luma and chroma pixels directly  */
1200 /*                  from the bitstream when the mbtype is I_PCM and stores   */
1201 /*                  them in recon buffer. If the entropy coding mode is      */
1202 /*                  cabac, decoding engine is re-initialized. The nnzs and   */
1203 /*                  cabac contexts are appropriately modified.               */
1204 /*  Returns       : void                                                     */
1205 /*                                                                           */
1206 /*  Revision History:                                                        */
1207 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1208 /*         13 07 2002   Jay                                                  */
1209 /*                                                                           */
1210 /*****************************************************************************/
1211 
ih264d_parse_ipcm_mb(dec_struct_t * ps_dec,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_mbNum)1212 WORD32 ih264d_parse_ipcm_mb(dec_struct_t * ps_dec,
1213                           dec_mb_info_t *ps_cur_mb_info,
1214                           UWORD8 u1_mbNum)
1215 {
1216     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1217     UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
1218     UWORD8 *pu1_y, *pu1_u, *pu1_v;
1219     WORD32 ret;
1220 
1221     UWORD32 u4_rec_width_y, u4_rec_width_uv;
1222     UWORD32 u1_num_mb_pair;
1223     UWORD8 u1_x, u1_y;
1224     /* CHANGED CODE */
1225     tfr_ctxt_t *ps_frame_buf;
1226     UWORD8 u1_mb_field_decoding_flag;
1227     UWORD32 *pu4_buf;
1228     UWORD8 *pu1_buf;
1229     /* CHANGED CODE */
1230 
1231     if(ps_dec->u1_separate_parse)
1232     {
1233         ps_frame_buf = &ps_dec->s_tran_addrecon_parse;
1234     }
1235     else
1236     {
1237         ps_frame_buf = &ps_dec->s_tran_addrecon;
1238     }
1239     /* align bistream to byte boundary. */
1240     /* pcm_alignment_zero_bit discarded */
1241     /* For XX GotoByteBoundary */
1242     if(ps_bitstrm->u4_ofst & 0x07)
1243     {
1244         ps_bitstrm->u4_ofst += 8;
1245         ps_bitstrm->u4_ofst &= 0xFFFFFFF8;
1246     }
1247 
1248     /*  Store left Nnz as 16 for each 4x4 blk */
1249 
1250     pu1_buf = ps_dec->pu1_left_nnz_y;
1251     pu4_buf = (UWORD32 *)pu1_buf;
1252     *pu4_buf = 0x10101010;
1253     pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
1254     pu4_buf = (UWORD32 *)pu1_buf;
1255     *pu4_buf = 0x10101010;
1256     pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
1257     pu4_buf = (UWORD32 *)pu1_buf;
1258     *pu4_buf = 0x10101010;
1259     pu1_buf = ps_dec->pu1_left_nnz_uv;
1260     pu4_buf = (UWORD32 *)pu1_buf;
1261     *pu4_buf = 0x10101010;
1262     ps_cur_mb_info->u1_cbp = 0xff;
1263 
1264     ps_dec->i1_prev_mb_qp_delta = 0;
1265     /* Get neighbour MB's */
1266     u1_num_mb_pair = (u1_mbNum >> u1_mbaff);
1267 
1268     /*****************************************************************************/
1269     /* calculate the RECON buffer YUV pointers for the PCM data                  */
1270     /*****************************************************************************/
1271     /* CHANGED CODE  */
1272     u1_mb_field_decoding_flag = ps_cur_mb_info->u1_mb_field_decodingflag;
1273     pu1_y = ps_frame_buf->pu1_dest_y + (u1_num_mb_pair << 4);
1274     pu1_u = ps_frame_buf->pu1_dest_u + (u1_num_mb_pair << 4);
1275     pu1_v = pu1_u + 1;
1276 
1277     u4_rec_width_y = ps_dec->u2_frm_wd_y << u1_mb_field_decoding_flag;
1278     u4_rec_width_uv = ps_dec->u2_frm_wd_uv << u1_mb_field_decoding_flag;
1279     /* CHANGED CODE  */
1280 
1281     if(u1_mbaff)
1282     {
1283         UWORD8 u1_top_mb;
1284 
1285         u1_top_mb = ps_cur_mb_info->u1_topmb;
1286 
1287         if(u1_top_mb == 0)
1288         {
1289             pu1_y += (u1_mb_field_decoding_flag ?
1290                             (u4_rec_width_y >> 1) : (u4_rec_width_y << 4));
1291             pu1_u += (u1_mb_field_decoding_flag ?
1292                             (u4_rec_width_uv) : (u4_rec_width_uv << 4));
1293             pu1_v = pu1_u + 1;
1294         }
1295     }
1296 
1297     /* Read Luma samples */
1298     for(u1_y = 0; u1_y < 16; u1_y++)
1299     {
1300         for(u1_x = 0; u1_x < 16; u1_x++)
1301             pu1_y[u1_x] = ih264d_get_bits_h264(ps_bitstrm, 8);
1302 
1303         pu1_y += u4_rec_width_y;
1304     }
1305 
1306     /* Read Chroma samples */
1307     for(u1_y = 0; u1_y < 8; u1_y++)
1308     {
1309         for(u1_x = 0; u1_x < 8; u1_x++)
1310             pu1_u[u1_x * YUV420SP_FACTOR] = ih264d_get_bits_h264(ps_bitstrm, 8);
1311 
1312         pu1_u += u4_rec_width_uv;
1313     }
1314 
1315     for(u1_y = 0; u1_y < 8; u1_y++)
1316     {
1317         for(u1_x = 0; u1_x < 8; u1_x++)
1318             pu1_v[u1_x * YUV420SP_FACTOR] = ih264d_get_bits_h264(ps_bitstrm, 8);
1319 
1320         pu1_v += u4_rec_width_uv;
1321     }
1322 
1323     if(CABAC == ps_dec->ps_cur_pps->u1_entropy_coding_mode)
1324     {
1325         UWORD32 *pu4_buf;
1326         UWORD8 *pu1_buf;
1327         ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
1328         /* Re-initialize the cabac decoding engine. */
1329         ret = ih264d_init_cabac_dec_envirnoment(&(ps_dec->s_cab_dec_env), ps_bitstrm);
1330         if(ret != OK)
1331             return ret;
1332         /* update the cabac contetxs */
1333         p_curr_ctxt->u1_mb_type = CAB_I_PCM;
1334         p_curr_ctxt->u1_cbp = 47;
1335         p_curr_ctxt->u1_intra_chroma_pred_mode = 0;
1336         p_curr_ctxt->u1_transform8x8_ctxt = 0;
1337         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
1338 
1339         pu1_buf = ps_dec->pu1_left_nnz_y;
1340         pu4_buf = (UWORD32 *)pu1_buf;
1341         *pu4_buf = 0x01010101;
1342 
1343         pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
1344         pu4_buf = (UWORD32 *)pu1_buf;
1345         *pu4_buf = 0x01010101;
1346 
1347         pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
1348         pu4_buf = (UWORD32 *)pu1_buf;
1349         *pu4_buf = 0x01010101;
1350 
1351         pu1_buf = ps_dec->pu1_left_nnz_uv;
1352         pu4_buf = (UWORD32 *)pu1_buf;
1353         *pu4_buf = 0x01010101;
1354 
1355         p_curr_ctxt->u1_yuv_dc_csbp = 0x7;
1356         ps_dec->pu1_left_yuv_dc_csbp[0] = 0x7;
1357         if(ps_dec->ps_cur_slice->u1_slice_type != I_SLICE)
1358         {
1359 
1360             MEMSET_16BYTES(&ps_dec->pu1_left_mv_ctxt_inc[0][0], 0);
1361             memset(ps_dec->pi1_left_ref_idx_ctxt_inc, 0, 4);
1362             MEMSET_16BYTES(p_curr_ctxt->u1_mv, 0);
1363             memset(p_curr_ctxt->i1_ref_idx, 0, 4);
1364 
1365         }
1366     }
1367     return OK;
1368 }
1369 
1370 /*!
1371  **************************************************************************
1372  * \if Function name : ih264d_decode_islice \endif
1373  *
1374  * \brief
1375  *    Decodes an I Slice
1376  *
1377  *
1378  * \return
1379  *    0 on Success and Error code otherwise
1380  **************************************************************************
1381  */
ih264d_parse_islice(dec_struct_t * ps_dec,UWORD16 u2_first_mb_in_slice)1382 WORD32 ih264d_parse_islice(dec_struct_t *ps_dec,
1383                             UWORD16 u2_first_mb_in_slice)
1384 {
1385     dec_pic_params_t * ps_pps = ps_dec->ps_cur_pps;
1386     dec_slice_params_t * ps_slice = ps_dec->ps_cur_slice;
1387     UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
1388     UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;
1389     UWORD32 u4_temp;
1390     WORD32 i_temp;
1391     WORD64 i8_temp;
1392     WORD32 ret;
1393 
1394     /*--------------------------------------------------------------------*/
1395     /* Read remaining contents of the slice header                        */
1396     /*--------------------------------------------------------------------*/
1397     /* dec_ref_pic_marking function */
1398     /* G050 */
1399     if(ps_slice->u1_nal_ref_idc != 0)
1400     {
1401         if(!ps_dec->ps_dpb_cmds->u1_dpb_commands_read)
1402         {
1403             i_temp = ih264d_read_mmco_commands(ps_dec);
1404             if (i_temp < 0)
1405             {
1406                 return ERROR_DBP_MANAGER_T;
1407             }
1408             ps_dec->u4_bitoffset = i_temp;
1409         }
1410         else
1411             ps_dec->ps_bitstrm->u4_ofst += ps_dec->u4_bitoffset;
1412     }
1413     /* G050 */
1414 
1415     /* Read slice_qp_delta */
1416     i8_temp = (WORD64)ps_pps->u1_pic_init_qp
1417                         + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1418     if((i8_temp < MIN_H264_QP) || (i8_temp > MAX_H264_QP))
1419         return ERROR_INV_RANGE_QP_T;
1420     ps_slice->u1_slice_qp = i8_temp;
1421     COPYTHECONTEXT("SH: slice_qp_delta",
1422                     ps_slice->u1_slice_qp - ps_pps->u1_pic_init_qp);
1423 
1424     if(ps_pps->u1_deblocking_filter_parameters_present_flag == 1)
1425     {
1426         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
1427         COPYTHECONTEXT("SH: disable_deblocking_filter_idc", u4_temp);
1428 
1429         if(u4_temp > SLICE_BOUNDARY_DBLK_DISABLED)
1430         {
1431             return ERROR_INV_SLICE_HDR_T;
1432         }
1433         ps_slice->u1_disable_dblk_filter_idc = u4_temp;
1434         if(u4_temp != 1)
1435         {
1436             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
1437                             << 1;
1438             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
1439             {
1440                 return ERROR_INV_SLICE_HDR_T;
1441             }
1442             ps_slice->i1_slice_alpha_c0_offset = i_temp;
1443             COPYTHECONTEXT("SH: slice_alpha_c0_offset_div2",
1444                             ps_slice->i1_slice_alpha_c0_offset >> 1);
1445 
1446             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
1447                             << 1;
1448             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
1449             {
1450                 return ERROR_INV_SLICE_HDR_T;
1451             }
1452             ps_slice->i1_slice_beta_offset = i_temp;
1453             COPYTHECONTEXT("SH: slice_beta_offset_div2",
1454                             ps_slice->i1_slice_beta_offset >> 1);
1455 
1456         }
1457         else
1458         {
1459             ps_slice->i1_slice_alpha_c0_offset = 0;
1460             ps_slice->i1_slice_beta_offset = 0;
1461         }
1462     }
1463     else
1464     {
1465         ps_slice->u1_disable_dblk_filter_idc = 0;
1466         ps_slice->i1_slice_alpha_c0_offset = 0;
1467         ps_slice->i1_slice_beta_offset = 0;
1468     }
1469 
1470     /* Initialization to check if number of motion vector per 2 Mbs */
1471     /* are exceeding the range or not */
1472     ps_dec->u2_mv_2mb[0] = 0;
1473     ps_dec->u2_mv_2mb[1] = 0;
1474 
1475 
1476     /*set slice header cone to 2 ,to indicate  correct header*/
1477     ps_dec->u1_slice_header_done = 2;
1478 
1479     if(ps_pps->u1_entropy_coding_mode)
1480     {
1481         SWITCHOFFTRACE; SWITCHONTRACECABAC;
1482         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
1483         {
1484             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_mbaff;
1485         }
1486         else
1487             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_nonmbaff;
1488 
1489         ret = ih264d_parse_islice_data_cabac(ps_dec, ps_slice,
1490                                              u2_first_mb_in_slice);
1491         if(ret != OK)
1492             return ret;
1493         SWITCHONTRACE; SWITCHOFFTRACECABAC;
1494     }
1495     else
1496     {
1497         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
1498         {
1499             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_mbaff;
1500         }
1501         else
1502             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_nonmbaff;
1503         ret = ih264d_parse_islice_data_cavlc(ps_dec, ps_slice,
1504                                        u2_first_mb_in_slice);
1505         if(ret != OK)
1506             return ret;
1507     }
1508 
1509     return OK;
1510 }
1511