1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 /*!
21  ***************************************************************************
22  * \file ih264d_parse_cabac.c
23  *
24  * \brief
25  *    This file contains cabac Residual decoding routines.
26  *
27  * \date
28  *    20/03/2003
29  *
30  * \author  NS
31  ***************************************************************************
32  */
33 
34 #include "ih264_typedefs.h"
35 #include "ih264_macros.h"
36 #include "ih264_platform_macros.h"
37 #include "ih264d_defs.h"
38 #include "ih264d_structs.h"
39 
40 #include "ih264d_cabac.h"
41 #include "ih264d_bitstrm.h"
42 #include "ih264d_parse_mb_header.h"
43 #include "ih264d_debug.h"
44 #include "ih264d_tables.h"
45 #include "ih264d_error_handler.h"
46 #include "ih264d_parse_cabac.h"
47 #include "ih264d_parse_slice.h"
48 #include "ih264d_tables.h"
49 #include "ih264d_mb_utils.h"
50 #include "ih264d_utils.h"
51 
52 /*!
53  ********************************************************************************
54  *   \if Function name : ih264d_read_coeff4x4_cabac \endif
55  *
56  *   \brief  This function encodes residual_block_cabac as defined in 7.3.5.3.2.
57  *
58  *   \return
59  *       Returns the index of last significant coeff.
60  *
61  ********************************************************************************
62  */
63 
ih264d_read_coeff4x4_cabac(dec_bit_stream_t * ps_bitstrm,UWORD32 u4_ctxcat,bin_ctxt_model_t * ps_ctxt_sig_coeff,dec_struct_t * ps_dec,bin_ctxt_model_t * ps_ctxt_coded)64 UWORD8 ih264d_read_coeff4x4_cabac(dec_bit_stream_t *ps_bitstrm,
65                                   UWORD32 u4_ctxcat,
66                                   bin_ctxt_model_t *ps_ctxt_sig_coeff,
67                                   dec_struct_t *ps_dec, /*!< pointer to access global variables*/
68                                   bin_ctxt_model_t *ps_ctxt_coded)
69 {
70 
71     decoding_envirnoment_t *ps_cab_env = &ps_dec->s_cab_dec_env;
72     UWORD32 u4_coded_flag;
73     UWORD32 u4_offset, *pu4_buffer;
74     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
75     tu_sblk4x4_coeff_data_t *ps_tu_4x4;
76     WORD16 *pi2_coeff_data;
77     WORD32 num_sig_coeffs = 0;
78 
79     /*loading from strcuctures*/
80 
81     ps_tu_4x4 = (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
82     ps_tu_4x4->u2_sig_coeff_map = 0;
83     pi2_coeff_data = &ps_tu_4x4->ai2_level[0];
84 
85     u4_offset = ps_bitstrm->u4_ofst;
86     pu4_buffer = ps_bitstrm->pu4_buffer;
87 
88     u4_code_int_range = ps_cab_env->u4_code_int_range;
89     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
90 
91     {
92 
93         /*inilined DecodeDecision_onebin begins*/
94 
95         {
96 
97             UWORD32 u4_qnt_int_range, u4_int_range_lps;
98             UWORD32 u4_symbol, u1_mps_state;
99 
100             UWORD32 table_lookup;
101             const UWORD32 *pu4_table = (const UWORD32 *)ps_cab_env->cabac_table;
102             UWORD32 u4_clz;
103 
104             u1_mps_state = (ps_ctxt_coded->u1_mps_state);
105             u4_clz = CLZ(u4_code_int_range);
106             u4_qnt_int_range = u4_code_int_range << u4_clz;
107             u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
108             table_lookup =
109                             pu4_table[(u1_mps_state << 2) + u4_qnt_int_range];
110             u4_int_range_lps = table_lookup & 0xff;
111             u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
112             u4_code_int_range = u4_code_int_range - u4_int_range_lps;
113             u4_symbol = ((u1_mps_state >> 6) & 0x1);
114             u1_mps_state = (table_lookup >> 8) & 0x7F;
115 
116             CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst, u4_symbol,
117                          u4_int_range_lps, u1_mps_state, table_lookup)
118 
119             if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_8)
120             {
121 
122                 RENORM_RANGE_OFFSET(u4_code_int_range, u4_code_int_val_ofst,
123                                     u4_offset, pu4_buffer)
124             }
125 
126             ps_ctxt_coded->u1_mps_state = u1_mps_state;
127             u4_coded_flag = u4_symbol;
128 
129             /*inilined DecodeDecision_onebin ends*/
130 
131         }
132 
133     }
134 
135     if(u4_coded_flag)
136     {
137 
138         {
139             bin_ctxt_model_t *p_binCtxt_last, *p_binCtxt_last_org;
140             UWORD32 uc_last_coeff_idx;
141             UWORD32 uc_bin;
142             UWORD32 i;
143             WORD32 first_coeff_offset = 0;
144 
145             if((u4_ctxcat == CHROMA_AC_CTXCAT) || (u4_ctxcat == LUMA_AC_CTXCAT))
146             {
147                 first_coeff_offset = 1;
148             }
149 
150             i = 0;
151             if(u4_ctxcat == CHROMA_DC_CTXCAT)
152             {
153                 uc_last_coeff_idx = 3;
154             }
155             else
156             {
157                 UWORD32 u4_start;
158                 u4_start = (u4_ctxcat & 1) + (u4_ctxcat >> 2);
159                 uc_last_coeff_idx = 15 - u4_start;
160             }
161             p_binCtxt_last_org = ps_ctxt_sig_coeff
162                             + LAST_COEFF_CTXT_MINUS_SIG_COEFF_CTXT;
163 
164             do
165             {
166 
167                 /*inilined DecodeDecision_onebin begins*/
168                 {
169 
170                     UWORD32 u4_qnt_int_range, u4_int_range_lps;
171                     UWORD32 u4_symbol, u1_mps_state;
172                     UWORD32 table_lookup;
173                     const UWORD32 *pu4_table =
174                                     (const UWORD32 *)ps_cab_env->cabac_table;
175                     UWORD32 u4_clz;
176 
177                     u1_mps_state = (ps_ctxt_sig_coeff->u1_mps_state);
178 
179                     u4_clz = CLZ(u4_code_int_range);
180 
181                     u4_qnt_int_range = u4_code_int_range << u4_clz;
182                     u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
183 
184                     table_lookup = pu4_table[(u1_mps_state << 2)
185                                     + u4_qnt_int_range];
186 
187                     u4_int_range_lps = table_lookup & 0xff;
188 
189                     u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
190                     u4_code_int_range = u4_code_int_range - u4_int_range_lps;
191                     u4_symbol = ((u1_mps_state >> 6) & 0x1);
192                     u1_mps_state = (table_lookup >> 8) & 0x7F;
193 
194                     CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst,
195                                  u4_symbol, u4_int_range_lps, u1_mps_state,
196                                  table_lookup)
197 
198                     if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_14)
199                     {
200 
201                         UWORD32 read_bits, u4_clz;
202                         u4_clz = CLZ(u4_code_int_range);
203                         NEXTBITS(read_bits, (u4_offset + 23), pu4_buffer,
204                                  u4_clz)
205                         FLUSHBITS(u4_offset, (u4_clz))
206                         u4_code_int_range = u4_code_int_range << u4_clz;
207                         u4_code_int_val_ofst = (u4_code_int_val_ofst << u4_clz)
208                                         | read_bits;
209                     }
210 
211                     INC_BIN_COUNT(
212                                     ps_cab_env)
213 
214                     ps_ctxt_sig_coeff->u1_mps_state = u1_mps_state;
215                     uc_bin = u4_symbol;
216 
217                 }
218                 /*incrementing pointer to point to the context of the next bin*/
219                 ps_ctxt_sig_coeff++;
220 
221                 /*inilined DecodeDecision_onebin ends*/
222 
223                 if(uc_bin)
224                 {
225                     num_sig_coeffs++;
226                     SET_BIT(ps_tu_4x4->u2_sig_coeff_map, (i + first_coeff_offset));
227 
228                     p_binCtxt_last = p_binCtxt_last_org + i;
229 
230                     /*inilined DecodeDecision_onebin begins*/
231 
232                     {
233 
234                         UWORD32 u4_qnt_int_range, u4_int_range_lps;
235                         UWORD32 u4_symbol, u1_mps_state;
236                         UWORD32 table_lookup;
237                         const UWORD32 *pu4_table =
238                                         (const UWORD32 *)ps_cab_env->cabac_table;
239                         UWORD32 u4_clz;
240 
241                         u1_mps_state = (p_binCtxt_last->u1_mps_state);
242 
243                         u4_clz = CLZ(u4_code_int_range);
244                         u4_qnt_int_range = u4_code_int_range << u4_clz;
245                         u4_qnt_int_range = (u4_qnt_int_range >> 29)
246                                         & 0x3;
247 
248                         table_lookup = pu4_table[(u1_mps_state << 2)
249                                         + u4_qnt_int_range];
250                         u4_int_range_lps = table_lookup & 0xff;
251 
252                         u4_int_range_lps = u4_int_range_lps
253                                         << (23 - u4_clz);
254 
255                         u4_code_int_range = u4_code_int_range
256                                         - u4_int_range_lps;
257                         u4_symbol = ((u1_mps_state >> 6) & 0x1);
258                         u1_mps_state = (table_lookup >> 8) & 0x7F;
259 
260                         CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst,
261                                      u4_symbol, u4_int_range_lps,
262                                      u1_mps_state, table_lookup)
263 
264                         INC_BIN_COUNT(ps_cab_env)
265 
266                         p_binCtxt_last->u1_mps_state = u1_mps_state;
267                         uc_bin = u4_symbol;
268 
269                     }
270 
271                     /*inilined DecodeDecision_onebin ends*/
272                     if(uc_bin == 1)
273                         goto label_read_levels;
274 
275                 }
276 
277                 i = i + 1;
278 
279             }
280             while(i < uc_last_coeff_idx);
281 
282             num_sig_coeffs++;
283             SET_BIT(ps_tu_4x4->u2_sig_coeff_map, (i + first_coeff_offset));
284 
285             label_read_levels: ;
286 
287         }
288 
289         /// VALUE of No of Coeff in BLOCK = i + 1 for second case else i;
290 
291         /* Decode coeff_abs_level_minus1 and coeff_sign_flag */
292         {
293 
294             WORD32 i2_abs_lvl;
295             UWORD32 u1_abs_level_equal1 = 1, u1_abs_level_gt1 = 0;
296 
297             UWORD32 u4_ctx_inc;
298             UWORD32 ui_prefix;
299         bin_ctxt_model_t *p_ctxt_abs_level;
300 
301 
302         p_ctxt_abs_level = ps_dec->p_coeff_abs_level_minus1_t[u4_ctxcat];
303         u4_ctx_inc = ((0x51));
304 
305         /*****************************************************/
306         /* Main Loop runs for no. of Significant coefficient */
307         /*****************************************************/
308 
309 
310         do
311             {
312 
313                 {
314                     INC_SYM_COUNT(&(ps_dec.s_cab_dec_env));
315 
316                     /*****************************************************/
317                     /* inilining a modified ih264d_decode_bins_unary     */
318                     /*****************************************************/
319 
320                     {
321                         UWORD32 u4_value;
322                         UWORD32 u4_symbol;
323                         bin_ctxt_model_t *ps_bin_ctxt;
324                         UWORD32 u4_ctx_Inc;
325 
326                         u4_value = 0;
327 
328                         u4_ctx_Inc = u4_ctx_inc & 0xf;
329                         ps_bin_ctxt = p_ctxt_abs_level + u4_ctx_Inc;
330 
331                         do
332                         {
333 
334                             {
335 
336                                 UWORD32 u4_qnt_int_range,
337                                                 u4_int_range_lps;
338                                 UWORD32 u1_mps_state;
339                                 UWORD32 table_lookup;
340                                 const UWORD32 *pu4_table =
341                                                 (const UWORD32 *)ps_cab_env->cabac_table;
342                                 UWORD32 u4_clz;
343 
344                                 u1_mps_state = (ps_bin_ctxt->u1_mps_state);
345                                 u4_clz = CLZ(u4_code_int_range);
346                                 u4_qnt_int_range = u4_code_int_range
347                                                 << u4_clz;
348                                 u4_qnt_int_range = (u4_qnt_int_range
349                                                 >> 29) & 0x3;
350                                 table_lookup = pu4_table[(u1_mps_state << 2)
351                                                 + u4_qnt_int_range];
352                                 u4_int_range_lps = table_lookup & 0xff;
353 
354                                 u4_int_range_lps = u4_int_range_lps
355                                                 << (23 - u4_clz);
356                                 u4_code_int_range = u4_code_int_range
357                                                 - u4_int_range_lps;
358                                 u4_symbol = ((u1_mps_state >> 6) & 0x1);
359                                 u1_mps_state = (table_lookup >> 8) & 0x7F;
360 
361                                 CHECK_IF_LPS(u4_code_int_range,
362                                              u4_code_int_val_ofst, u4_symbol,
363                                              u4_int_range_lps, u1_mps_state,
364                                              table_lookup)
365 
366                                 if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_9)
367                                 {
368 
369                                     RENORM_RANGE_OFFSET(u4_code_int_range,
370                                                         u4_code_int_val_ofst,
371                                                         u4_offset, pu4_buffer)
372                                 }
373 
374                                 INC_BIN_COUNT(ps_cab_env);
375 
376                                 ps_bin_ctxt->u1_mps_state = u1_mps_state;
377                             }
378 
379                             INC_BIN_COUNT(ps_cab_env);INC_DECISION_BINS(ps_cab_env);
380 
381                             u4_value++;
382                             ps_bin_ctxt = p_ctxt_abs_level + (u4_ctx_inc >> 4);
383 
384                         }
385                         while(u4_symbol && (u4_value < UCOFF_LEVEL));
386 
387                         ui_prefix = u4_value - 1 + u4_symbol;
388 
389                     }
390 
391                     if(ui_prefix == UCOFF_LEVEL)
392                     {
393                         UWORD32 ui16_sufS = 0;
394                         UWORD32 u1_max_bins;
395                         UWORD32 u4_value;
396 
397                         i2_abs_lvl = UCOFF_LEVEL;
398                         /*inlining ih264d_decode_bypass_bins_unary begins*/
399 
400                         {
401                             UWORD32 uc_bin;
402                             UWORD32 bits_to_flush;
403 
404 
405                             bits_to_flush = 0;
406                             /*renormalize to ensure there 23 bits more in the u4_code_int_val_ofst*/
407                             {
408                                 UWORD32 u4_clz, read_bits;
409 
410                                 u4_clz = CLZ(u4_code_int_range);
411                                 FLUSHBITS(u4_offset, u4_clz)
412                                 NEXTBITS(read_bits, u4_offset, pu4_buffer, CABAC_BITS_TO_READ)
413                                 u4_code_int_range = u4_code_int_range << u4_clz;
414                                 u4_code_int_val_ofst = (u4_code_int_val_ofst
415                                                 << u4_clz) | read_bits;
416 
417                             }
418 
419                             do
420                             {
421                                 bits_to_flush++;
422 
423                                 u4_code_int_range = u4_code_int_range >> 1;
424 
425                                 if(u4_code_int_val_ofst >= u4_code_int_range)
426                                 {
427                                     /* S=1 */
428                                     uc_bin = 1;
429                                     u4_code_int_val_ofst -= u4_code_int_range;
430                                 }
431                                 else
432                                 {
433                                     /* S=0 */
434                                     uc_bin = 0;
435                                 }
436 
437                                 INC_BIN_COUNT(
438                                                 ps_cab_env);INC_BYPASS_BINS(ps_cab_env);
439 
440                             }
441                             while(uc_bin && (bits_to_flush < CABAC_BITS_TO_READ));
442 
443                             u4_value = (bits_to_flush - 1);
444 
445                         }
446                         /*inlining ih264d_decode_bypass_bins_unary ends*/
447 
448                         ui16_sufS = (1 << u4_value);
449                         u1_max_bins = u4_value;
450 
451                         if(u4_value > 0)
452                         {
453 
454                             /*inline bypassbins_flc begins*/
455 
456                             if(u4_value > 10)
457                             {
458                                 UWORD32 u4_clz, read_bits;
459 
460                                 u4_clz = CLZ(u4_code_int_range);
461                                 FLUSHBITS(u4_offset, u4_clz)
462                                 NEXTBITS(read_bits, u4_offset, pu4_buffer, CABAC_BITS_TO_READ)
463                                 u4_code_int_range = u4_code_int_range << u4_clz;
464                                 u4_code_int_val_ofst = (u4_code_int_val_ofst
465                                                 << u4_clz) | read_bits;
466                             }
467 
468                             {
469                                 UWORD32 ui_bins;
470                                 UWORD32 uc_bin;
471                                 UWORD32 bits_to_flush;
472 
473                                 ui_bins = 0;
474                                 bits_to_flush = 0;
475 
476                                 do
477                                 {
478                                     bits_to_flush++;
479 
480                                     u4_code_int_range = u4_code_int_range >> 1;
481 
482                                     if(u4_code_int_val_ofst
483                                                     >= u4_code_int_range)
484                                     {
485                                         /* S=1 */
486                                         uc_bin = 1;
487                                         u4_code_int_val_ofst -=
488                                                         u4_code_int_range;
489                                     }
490                                     else
491                                     {
492                                         /* S=0 */
493                                         uc_bin = 0;
494                                     }
495 
496                                     INC_BIN_COUNT(
497                                                     ps_cab_env);INC_BYPASS_BINS(ps_cab_env);
498 
499                                     ui_bins = ((ui_bins << 1) | uc_bin);
500 
501                                 }
502                                 while(bits_to_flush < u1_max_bins);
503 
504                                 u4_value = ui_bins;
505                             }
506 
507                             /*inline bypassbins_flc ends*/
508 
509                         }
510 
511                         //Value of K
512                         ui16_sufS += u4_value;
513                         i2_abs_lvl += ui16_sufS;
514 
515                     }
516                     else
517                         i2_abs_lvl = 1 + ui_prefix;
518 
519                     if(i2_abs_lvl > 1)
520                     {
521                         u1_abs_level_gt1++;
522                     }
523                     if(!u1_abs_level_gt1)
524                     {
525                         u1_abs_level_equal1++;
526                         u4_ctx_inc = (5 << 4) + MIN(u1_abs_level_equal1, 4);
527                     }
528                     else
529                         u4_ctx_inc = (5 + MIN(u1_abs_level_gt1, 4)) << 4;
530 
531                     /*u4_ctx_inc = g_table_temp[u1_abs_level_gt1][u1_abs_level_equal1];*/
532 
533                     /* encode coeff_sign_flag[i] */
534 
535                     {
536                         u4_code_int_range = u4_code_int_range >> 1;
537 
538                         if(u4_code_int_val_ofst >= (u4_code_int_range))
539                         {
540                             /* S=1 */
541                             u4_code_int_val_ofst -= u4_code_int_range;
542                             i2_abs_lvl = (-i2_abs_lvl);
543                         }
544 
545                     }
546                     num_sig_coeffs--;
547                     *pi2_coeff_data++ = i2_abs_lvl;
548                 }
549             }
550             while(num_sig_coeffs > 0);
551         }
552     }
553 
554     if(u4_coded_flag)
555     {
556         WORD32 offset;
557         offset = (UWORD8 *)pi2_coeff_data - (UWORD8 *)ps_tu_4x4;
558         offset = ALIGN4(offset);
559         ps_dec->pv_parse_tu_coeff_data = (void *)((UWORD8 *)ps_dec->pv_parse_tu_coeff_data + offset);
560     }
561 
562 
563     /*updating structures*/
564     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
565     ps_cab_env->u4_code_int_range = u4_code_int_range;
566     ps_bitstrm->u4_ofst = u4_offset;
567     return (u4_coded_flag);
568 }
569 /*!
570  ********************************************************************************
571  *   \if Function name : ih264d_read_coeff8x8_cabac \endif
572  *
573  *   \brief  This function encodes residual_block_cabac as defined in 7.3.5.3.2.
574  when transform_8x8_flag  = 1
575  *
576  *   \return
577  *       Returns the index of last significant coeff.
578  *
579  ********************************************************************************
580  */
581 
ih264d_read_coeff8x8_cabac(dec_bit_stream_t * ps_bitstrm,dec_struct_t * ps_dec,dec_mb_info_t * ps_cur_mb_info)582 void ih264d_read_coeff8x8_cabac(dec_bit_stream_t *ps_bitstrm,
583                                 dec_struct_t *ps_dec, /*!< pointer to access global variables*/
584                                 dec_mb_info_t *ps_cur_mb_info)
585 {
586     decoding_envirnoment_t *ps_cab_env = &ps_dec->s_cab_dec_env;
587     UWORD32 u4_offset, *pu4_buffer;
588     UWORD32 u4_code_int_range, u4_code_int_val_ofst;
589 
590     /* High profile related declarations */
591     UWORD8 u1_field_coding_flag = ps_cur_mb_info->ps_curmb->u1_mb_fld;
592     const UWORD8 *pu1_lastcoeff_context_inc =
593                     (UWORD8 *)gau1_ih264d_lastcoeff_context_inc;
594     const UWORD8 *pu1_sigcoeff_context_inc;
595     bin_ctxt_model_t *ps_ctxt_sig_coeff;
596     WORD32 num_sig_coeffs = 0;
597     tu_blk8x8_coeff_data_t *ps_tu_8x8;
598     WORD16 *pi2_coeff_data;
599 
600     /*loading from strcuctures*/
601 
602     ps_tu_8x8 = (tu_blk8x8_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
603     ps_tu_8x8->au4_sig_coeff_map[0] = 0;
604     ps_tu_8x8->au4_sig_coeff_map[1] = 0;
605     pi2_coeff_data = &ps_tu_8x8->ai2_level[0];
606 
607 
608     if(!u1_field_coding_flag)
609     {
610         pu1_sigcoeff_context_inc =
611                         (UWORD8 *)gau1_ih264d_sigcoeff_context_inc_frame;
612 
613         /*******************************************************************/
614         /* last coefficient context is derived from significant coeff u4_flag */
615         /* only significant coefficient matrix need to be initialized      */
616         /*******************************************************************/
617         ps_ctxt_sig_coeff = ps_dec->s_high_profile.ps_sigcoeff_8x8_frame;
618     }
619     else
620     {
621         pu1_sigcoeff_context_inc =
622                         (UWORD8 *)gau1_ih264d_sigcoeff_context_inc_field;
623 
624         /*******************************************************************/
625         /* last coefficient context is derived from significant coeff u4_flag */
626         /* only significant coefficient matrix need to be initialized      */
627         /*******************************************************************/
628         ps_ctxt_sig_coeff = ps_dec->s_high_profile.ps_sigcoeff_8x8_field;
629     }
630 
631     /*loading from strcuctures*/
632 
633     u4_offset = ps_bitstrm->u4_ofst;
634     pu4_buffer = ps_bitstrm->pu4_buffer;
635 
636     u4_code_int_range = ps_cab_env->u4_code_int_range;
637     u4_code_int_val_ofst = ps_cab_env->u4_code_int_val_ofst;
638 
639     {
640         {
641             bin_ctxt_model_t *p_binCtxt_last, *p_binCtxt_last_org,
642                             *p_ctxt_sig_coeff_org;
643             UWORD32 uc_last_coeff_idx;
644             UWORD32 uc_bin;
645             UWORD32 i;
646 
647             i = 0;
648 
649             uc_last_coeff_idx = 63;
650 
651             p_binCtxt_last_org = ps_ctxt_sig_coeff
652                             + LAST_COEFF_CTXT_MINUS_SIG_COEFF_CTXT_8X8;
653 
654             p_ctxt_sig_coeff_org = ps_ctxt_sig_coeff;
655 
656             do
657             {
658                 /*inilined DecodeDecision_onebin begins*/
659                 {
660                     UWORD32 u4_qnt_int_range, u4_int_range_lps;
661                     UWORD32 u4_symbol, u1_mps_state;
662                     UWORD32 table_lookup;
663                     const UWORD32 *pu4_table =
664                                     (const UWORD32 *)ps_cab_env->cabac_table;
665                     UWORD32 u4_clz;
666 
667                     u1_mps_state = (ps_ctxt_sig_coeff->u1_mps_state);
668 
669                     u4_clz = CLZ(u4_code_int_range);
670 
671                     u4_qnt_int_range = u4_code_int_range << u4_clz;
672                     u4_qnt_int_range = (u4_qnt_int_range >> 29) & 0x3;
673 
674                     table_lookup = pu4_table[(u1_mps_state << 2)
675                                     + u4_qnt_int_range];
676 
677                     u4_int_range_lps = table_lookup & 0xff;
678 
679                     u4_int_range_lps = u4_int_range_lps << (23 - u4_clz);
680                     u4_code_int_range = u4_code_int_range - u4_int_range_lps;
681                     u4_symbol = ((u1_mps_state >> 6) & 0x1);
682                     u1_mps_state = (table_lookup >> 8) & 0x7F;
683 
684                     CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst,
685                                  u4_symbol, u4_int_range_lps, u1_mps_state,
686                                  table_lookup)
687 
688                     if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_14)
689                     {
690                         UWORD32 read_bits, u4_clz;
691                         u4_clz = CLZ(u4_code_int_range);
692                         NEXTBITS(read_bits, (u4_offset + 23), pu4_buffer,
693                                  u4_clz)
694                         FLUSHBITS(u4_offset, (u4_clz))
695                         u4_code_int_range = u4_code_int_range << u4_clz;
696                         u4_code_int_val_ofst = (u4_code_int_val_ofst << u4_clz)
697                                         | read_bits;
698                     }
699 
700                     ps_ctxt_sig_coeff->u1_mps_state = u1_mps_state;
701                     uc_bin = u4_symbol;
702                 }
703                 /*incrementing pointer to point to the context of the next bin*/
704                 ps_ctxt_sig_coeff = p_ctxt_sig_coeff_org
705                                 + pu1_sigcoeff_context_inc[i + 1];
706 
707                 /*inilined DecodeDecision_onebin ends*/
708                 if(uc_bin)
709                 {
710                     num_sig_coeffs++;
711                     SET_BIT(ps_tu_8x8->au4_sig_coeff_map[i>31], (i > 31 ? i - 32:i));
712 
713                     p_binCtxt_last = p_binCtxt_last_org
714                                     + pu1_lastcoeff_context_inc[i];
715 
716                     /*inilined DecodeDecision_onebin begins*/
717 
718                     {
719                         UWORD32 u4_qnt_int_range, u4_int_range_lps;
720                         UWORD32 u4_symbol, u1_mps_state;
721                         UWORD32 table_lookup;
722                         const UWORD32 *pu4_table =
723                                         (const UWORD32 *)ps_cab_env->cabac_table;
724                         UWORD32 u4_clz;
725 
726                         u1_mps_state = (p_binCtxt_last->u1_mps_state);
727 
728                         u4_clz = CLZ(u4_code_int_range);
729                         u4_qnt_int_range = u4_code_int_range << u4_clz;
730                         u4_qnt_int_range = (u4_qnt_int_range >> 29)
731                                         & 0x3;
732 
733                         table_lookup = pu4_table[(u1_mps_state << 2)
734                                         + u4_qnt_int_range];
735                         u4_int_range_lps = table_lookup & 0xff;
736 
737                         u4_int_range_lps = u4_int_range_lps
738                                         << (23 - u4_clz);
739 
740                         u4_code_int_range = u4_code_int_range
741                                         - u4_int_range_lps;
742                         u4_symbol = ((u1_mps_state >> 6) & 0x1);
743                         u1_mps_state = (table_lookup >> 8) & 0x7F;
744 
745                         CHECK_IF_LPS(u4_code_int_range, u4_code_int_val_ofst,
746                                      u4_symbol, u4_int_range_lps,
747                                      u1_mps_state, table_lookup)
748 
749                         p_binCtxt_last->u1_mps_state = u1_mps_state;
750                         uc_bin = u4_symbol;
751                     }
752 
753                     /*inilined DecodeDecision_onebin ends*/
754                     if(uc_bin == 1)
755                         goto label_read_levels;
756 
757                 }
758 
759                 i = i + 1;
760 
761             }
762             while(i < uc_last_coeff_idx);
763 
764             num_sig_coeffs++;
765             SET_BIT(ps_tu_8x8->au4_sig_coeff_map[i>31], (i > 31 ? i - 32:i));
766 
767             label_read_levels: ;
768         }
769 
770         /// VALUE of No of Coeff in BLOCK = i + 1 for second case else i;
771 
772         /* Decode coeff_abs_level_minus1 and coeff_sign_flag */
773         {
774             WORD32 i2_abs_lvl;
775             UWORD32 u1_abs_level_equal1 = 1, u1_abs_level_gt1 = 0;
776 
777             UWORD32 u4_ctx_inc;
778             UWORD32 ui_prefix;
779             bin_ctxt_model_t *p_ctxt_abs_level;
780 
781             p_ctxt_abs_level =
782                             ps_dec->p_coeff_abs_level_minus1_t[LUMA_8X8_CTXCAT];
783             u4_ctx_inc = ((0x51));
784 
785             /*****************************************************/
786             /* Main Loop runs for no. of Significant coefficient */
787             /*****************************************************/
788             do
789             {
790                 {
791 
792                     /*****************************************************/
793                     /* inilining a modified ih264d_decode_bins_unary     */
794                     /*****************************************************/
795 
796                     {
797                         UWORD32 u4_value;
798                         UWORD32 u4_symbol;
799                         bin_ctxt_model_t *ps_bin_ctxt;
800                         UWORD32 u4_ctx_Inc;
801                         u4_value = 0;
802 
803                         u4_ctx_Inc = u4_ctx_inc & 0xf;
804                         ps_bin_ctxt = p_ctxt_abs_level + u4_ctx_Inc;
805 
806                         do
807                         {
808                             {
809                                 UWORD32 u4_qnt_int_range,
810                                                 u4_int_range_lps;
811                                 UWORD32 u1_mps_state;
812                                 UWORD32 table_lookup;
813                                 const UWORD32 *pu4_table =
814                                                 (const UWORD32 *)ps_cab_env->cabac_table;
815                                 UWORD32 u4_clz;
816 
817                                 u1_mps_state = (ps_bin_ctxt->u1_mps_state);
818                                 u4_clz = CLZ(u4_code_int_range);
819                                 u4_qnt_int_range = u4_code_int_range
820                                                 << u4_clz;
821                                 u4_qnt_int_range = (u4_qnt_int_range
822                                                 >> 29) & 0x3;
823                                 table_lookup = pu4_table[(u1_mps_state << 2)
824                                                 + u4_qnt_int_range];
825                                 u4_int_range_lps = table_lookup & 0xff;
826 
827                                 u4_int_range_lps = u4_int_range_lps
828                                                 << (23 - u4_clz);
829                                 u4_code_int_range = u4_code_int_range
830                                                 - u4_int_range_lps;
831                                 u4_symbol = ((u1_mps_state >> 6) & 0x1);
832                                 u1_mps_state = (table_lookup >> 8) & 0x7F;
833 
834                                 CHECK_IF_LPS(u4_code_int_range,
835                                              u4_code_int_val_ofst, u4_symbol,
836                                              u4_int_range_lps, u1_mps_state,
837                                              table_lookup)
838 
839                                 if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_9)
840                                 {
841 
842                                     RENORM_RANGE_OFFSET(u4_code_int_range,
843                                                         u4_code_int_val_ofst,
844                                                         u4_offset, pu4_buffer)
845                                 }
846 
847                                 ps_bin_ctxt->u1_mps_state = u1_mps_state;
848                             }
849 
850                             u4_value++;
851                             ps_bin_ctxt = p_ctxt_abs_level + (u4_ctx_inc >> 4);
852 
853                         }
854                         while(u4_symbol && (u4_value < UCOFF_LEVEL));
855 
856                         ui_prefix = u4_value - 1 + u4_symbol;
857                     }
858 
859                     if(ui_prefix == UCOFF_LEVEL)
860                     {
861                         UWORD32 ui16_sufS = 0;
862                         UWORD32 u1_max_bins;
863                         UWORD32 u4_value;
864 
865                         i2_abs_lvl = UCOFF_LEVEL;
866                         /*inlining ih264d_decode_bypass_bins_unary begins*/
867 
868                         {
869                             UWORD32 uc_bin;
870                             UWORD32 bits_to_flush;
871 
872 
873                             bits_to_flush = 0;
874                             /*renormalize to ensure there 23 bits more in the u4_code_int_val_ofst*/
875                             {
876                                 UWORD32 u4_clz, read_bits;
877 
878                                 u4_clz = CLZ(u4_code_int_range);
879                                 FLUSHBITS(u4_offset, u4_clz)
880                                 NEXTBITS(read_bits, u4_offset, pu4_buffer, CABAC_BITS_TO_READ)
881                                 u4_code_int_range = u4_code_int_range << u4_clz;
882                                 u4_code_int_val_ofst = (u4_code_int_val_ofst
883                                                 << u4_clz) | read_bits;
884                             }
885 
886                             do
887                             {
888                                 bits_to_flush++;
889 
890                                 u4_code_int_range = u4_code_int_range >> 1;
891 
892                                 if(u4_code_int_val_ofst >= u4_code_int_range)
893                                 {
894                                     /* S=1 */
895                                     uc_bin = 1;
896                                     u4_code_int_val_ofst -= u4_code_int_range;
897                                 }
898                                 else
899                                 {
900                                     /* S=0 */
901                                     uc_bin = 0;
902                                 }
903 
904                             }
905                             while(uc_bin && (bits_to_flush < CABAC_BITS_TO_READ));
906 
907                             u4_value = (bits_to_flush - 1);
908                         }
909                         /*inlining ih264d_decode_bypass_bins_unary ends*/
910 
911                         ui16_sufS = (1 << u4_value);
912                         u1_max_bins = u4_value;
913 
914                         if(u4_value > 0)
915                         {
916                             /*inline bypassbins_flc begins*/
917 
918                             if(u4_value > 10)
919                             {
920                                 UWORD32 u4_clz, read_bits;
921 
922                                 u4_clz = CLZ(u4_code_int_range);
923                                 FLUSHBITS(u4_offset, u4_clz)
924                                 NEXTBITS(read_bits, u4_offset, pu4_buffer, CABAC_BITS_TO_READ)
925                                 u4_code_int_range = u4_code_int_range << u4_clz;
926                                 u4_code_int_val_ofst = (u4_code_int_val_ofst
927                                                 << u4_clz) | read_bits;
928                             }
929 
930                             {
931                                 UWORD32 ui_bins;
932                                 UWORD32 uc_bin;
933                                 UWORD32 bits_to_flush;
934 
935                                 ui_bins = 0;
936                                 bits_to_flush = 0;
937 
938                                 do
939                                 {
940                                     bits_to_flush++;
941 
942                                     u4_code_int_range = u4_code_int_range >> 1;
943 
944                                     if(u4_code_int_val_ofst
945                                                     >= u4_code_int_range)
946                                     {
947                                         /* S=1 */
948                                         uc_bin = 1;
949                                         u4_code_int_val_ofst -=
950                                                         u4_code_int_range;
951                                     }
952                                     else
953                                     {
954                                         /* S=0 */
955                                         uc_bin = 0;
956                                     }
957 
958                                     ui_bins = ((ui_bins << 1) | uc_bin);
959 
960                                 }
961                                 while(bits_to_flush < u1_max_bins);
962 
963                                 u4_value = ui_bins;
964                             }
965                             /*inline bypassbins_flc ends*/
966                         }
967 
968                         //Value of K
969                         ui16_sufS += u4_value;
970                         i2_abs_lvl += (WORD32)ui16_sufS;
971                     }
972                     else
973                     {
974                         i2_abs_lvl = 1 + ui_prefix;
975                     }
976 
977                     if(i2_abs_lvl > 1)
978                     {
979                         u1_abs_level_gt1++;
980                     }
981                     if(!u1_abs_level_gt1)
982                     {
983                         u1_abs_level_equal1++;
984                         u4_ctx_inc = (5 << 4) + MIN(u1_abs_level_equal1, 4);
985                     }
986                     else
987                     {
988                         u4_ctx_inc = (5 + MIN(u1_abs_level_gt1, 4)) << 4;
989                     }
990 
991                     /*u4_ctx_inc = g_table_temp[u1_abs_level_gt1][u1_abs_level_equal1];*/
992 
993                     /* encode coeff_sign_flag[i] */
994 
995                     {
996                         u4_code_int_range = u4_code_int_range >> 1;
997 
998                         if(u4_code_int_val_ofst >= (u4_code_int_range))
999                         {
1000                             /* S=1 */
1001                             u4_code_int_val_ofst -= u4_code_int_range;
1002                             i2_abs_lvl = (-i2_abs_lvl);
1003                         }
1004                     }
1005 
1006                     *pi2_coeff_data++ = i2_abs_lvl;
1007                     num_sig_coeffs--;
1008                 }
1009             }
1010             while(num_sig_coeffs > 0);
1011         }
1012     }
1013 
1014     {
1015         WORD32 offset;
1016         offset = (UWORD8 *)pi2_coeff_data - (UWORD8 *)ps_tu_8x8;
1017         offset = ALIGN4(offset);
1018         ps_dec->pv_parse_tu_coeff_data = (void *)((UWORD8 *)ps_dec->pv_parse_tu_coeff_data + offset);
1019     }
1020 
1021     /*updating structures*/
1022     ps_cab_env->u4_code_int_val_ofst = u4_code_int_val_ofst;
1023     ps_cab_env->u4_code_int_range = u4_code_int_range;
1024     ps_bitstrm->u4_ofst = u4_offset;
1025 }
1026 
1027 /*****************************************************************************/
1028 /*                                                                           */
1029 /*  Function Name : ih264d_cabac_parse_8x8block                                     */
1030 /*                                                                           */
1031 /*  Description   : This function does the residual parsing of 4 subblocks   */
1032 /*                  in a 8x8 block.                                          */
1033 /*                                                                           */
1034 /*  Inputs        : pi2_coeff_block : pointer to residual block where        */
1035 /*                  decoded and inverse scan coefficients are updated        */
1036 /*                                                                           */
1037 /*                  u4_sub_block_strd : indicates the number of sublocks    */
1038 /*                  in a row. It is 4 for luma and 2 for chroma.             */
1039 /*                                                                           */
1040 /*                  u4_ctx_cat : inidicates context category for residual    */
1041 /*                  decoding.                                                */
1042 /*                                                                           */
1043 /*                  ps_dec : pointer to Decstruct (decoder context)          */
1044 /*                                                                           */
1045 /*                  pu1_top_nnz : top nnz pointer                            */
1046 /*                                                                           */
1047 /*                  pu1_left_nnz : left nnz pointer                          */
1048 /*                                                                           */
1049 /*  Globals       : No                                                       */
1050 /*  Processing    : Parsing for four subblocks in unrolled, top and left nnz */
1051 /*                  are updated on the fly. csbp is set in accordance to     */
1052 /*                  decoded numcoeff for the subblock index in raster order  */
1053 /*                                                                           */
1054 /*  Outputs       : The updated residue buffer, nnzs and csbp current block  */
1055 /*                                                                           */
1056 /*  Returns       : Returns the coded sub block pattern csbp for the block   */
1057 /*                                                                           */
1058 /*  Issues        : <List any issues or problems with this function>         */
1059 /*                                                                           */
1060 /*  Revision History:                                                        */
1061 /*                                                                           */
1062 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1063 /*         09 10 2008   Jay          Draft                                   */
1064 /*                                                                           */
1065 /*****************************************************************************/
ih264d_cabac_parse_8x8block(WORD16 * pi2_coeff_block,UWORD32 u4_sub_block_strd,UWORD32 u4_ctx_cat,dec_struct_t * ps_dec,UWORD8 * pu1_top_nnz,UWORD8 * pu1_left_nnz)1066 UWORD32 ih264d_cabac_parse_8x8block(WORD16 *pi2_coeff_block,
1067                                     UWORD32 u4_sub_block_strd,
1068                                     UWORD32 u4_ctx_cat,
1069                                     dec_struct_t * ps_dec,
1070                                     UWORD8 *pu1_top_nnz,
1071                                     UWORD8 *pu1_left_nnz)
1072 {
1073     UWORD32 u4_ctxinc, u4_subblock_coded;
1074     UWORD32 u4_top0, u4_top1;
1075     UWORD32 u4_csbp = 0;
1076     UWORD32 u4_idx = 0;
1077     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1078     bin_ctxt_model_t * const ps_cbf = ps_dec->p_cbf_t[u4_ctx_cat];
1079     bin_ctxt_model_t *ps_src_bin_ctxt;
1080     bin_ctxt_model_t * const ps_sig_coeff_flag =
1081                     ps_dec->p_significant_coeff_flag_t[u4_ctx_cat];
1082 
1083     UWORD8 *pu1_inv_scan = ps_dec->pu1_inv_scan;
1084 
1085     /*------------------------------------------------------*/
1086     /* Residual 4x4 decoding: SubBlock 0                    */
1087     /*------------------------------------------------------*/
1088     u4_ctxinc = ((!!pu1_top_nnz[0]) << 1) + (!!pu1_left_nnz[0]);
1089 
1090     ps_src_bin_ctxt = ps_cbf + u4_ctxinc;
1091 
1092     u4_top0 = ih264d_read_coeff4x4_cabac( ps_bitstrm,
1093                                          u4_ctx_cat, ps_sig_coeff_flag, ps_dec,
1094                                          ps_src_bin_ctxt);
1095 
1096     INSERT_BIT(u4_csbp, u4_idx, u4_top0);
1097 
1098     /*------------------------------------------------------*/
1099     /* Residual 4x4 decoding: SubBlock 1                    */
1100     /*------------------------------------------------------*/
1101     u4_idx++;
1102     pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
1103     u4_ctxinc = ((!!pu1_top_nnz[1]) << 1) + u4_top0;
1104 
1105     ps_src_bin_ctxt = ps_cbf + u4_ctxinc;
1106 
1107     u4_top1 = ih264d_read_coeff4x4_cabac(ps_bitstrm,
1108                                          u4_ctx_cat, ps_sig_coeff_flag, ps_dec,
1109                                          ps_src_bin_ctxt);
1110 
1111     INSERT_BIT(u4_csbp, u4_idx, u4_top1);
1112     pu1_left_nnz[0] = u4_top1;
1113 
1114     /*------------------------------------------------------*/
1115     /* Residual 4x4 decoding: SubBlock 2                    */
1116     /*------------------------------------------------------*/
1117     u4_idx += (u4_sub_block_strd - 1);
1118     pi2_coeff_block += ((u4_sub_block_strd - 1) * NUM_COEFFS_IN_4x4BLK);
1119     u4_ctxinc = (u4_top0 << 1) + (!!pu1_left_nnz[1]);
1120 
1121     ps_src_bin_ctxt = ps_cbf + u4_ctxinc;
1122 
1123     u4_subblock_coded = ih264d_read_coeff4x4_cabac(ps_bitstrm, u4_ctx_cat,
1124                                                    ps_sig_coeff_flag, ps_dec,
1125                                                    ps_src_bin_ctxt);
1126 
1127     INSERT_BIT(u4_csbp, u4_idx, u4_subblock_coded);
1128     pu1_top_nnz[0] = u4_subblock_coded;
1129 
1130     /*------------------------------------------------------*/
1131     /* Residual 4x4 decoding: SubBlock 3                    */
1132     /*------------------------------------------------------*/
1133     u4_idx++;
1134     pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
1135     u4_ctxinc = (u4_top1 << 1) + u4_subblock_coded;
1136 
1137     ps_src_bin_ctxt = ps_cbf + u4_ctxinc;
1138 
1139     u4_subblock_coded = ih264d_read_coeff4x4_cabac(ps_bitstrm, u4_ctx_cat,
1140                                                    ps_sig_coeff_flag, ps_dec,
1141                                                    ps_src_bin_ctxt);
1142 
1143     INSERT_BIT(u4_csbp, u4_idx, u4_subblock_coded);
1144     pu1_top_nnz[1] = pu1_left_nnz[1] = u4_subblock_coded;
1145 
1146     return (u4_csbp);
1147 }
1148 
1149 /*!
1150  **************************************************************************
1151  * \if Function name : ih264d_parse_residual4x4_cabac \endif
1152  *
1153  * \brief
1154  *    This function parses CABAC syntax of a Luma and Chroma AC Residuals.
1155  *
1156  * \return
1157  *    0 on Success and Error code otherwise
1158  **************************************************************************
1159  */
1160 
ih264d_parse_residual4x4_cabac(dec_struct_t * ps_dec,dec_mb_info_t * ps_cur_mb_info,UWORD8 u1_offset)1161 WORD32 ih264d_parse_residual4x4_cabac(dec_struct_t * ps_dec,
1162                                       dec_mb_info_t *ps_cur_mb_info,
1163                                       UWORD8 u1_offset)
1164 {
1165     UWORD8 u1_cbp = ps_cur_mb_info->u1_cbp;
1166     UWORD16 ui16_csbp = 0;
1167     WORD16 *pi2_residual_buf;
1168     UWORD8 uc_ctx_cat;
1169     UWORD8 *pu1_top_nnz = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
1170     UWORD8 *pu1_left_nnz = ps_dec->pu1_left_nnz_y;
1171     UWORD8 *pu1_top_nnz_uv = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
1172     ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
1173     ctxt_inc_mb_info_t *ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
1174     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1175     UWORD32 u4_nbr_avail = ps_dec->u1_mb_ngbr_availablity;
1176     WORD16 *pi2_coeff_block = NULL;
1177     bin_ctxt_model_t *ps_src_bin_ctxt;
1178 
1179     UWORD8 u1_top_dc_csbp = (ps_top_ctxt->u1_yuv_dc_csbp) >> 1;
1180     UWORD8 u1_left_dc_csbp = (ps_dec->pu1_left_yuv_dc_csbp[0]) >> 1;
1181 
1182 
1183     if(!(u4_nbr_avail & TOP_MB_AVAILABLE_MASK))
1184     {
1185         if(p_curr_ctxt->u1_mb_type & CAB_INTRA_MASK)
1186         {
1187             *(UWORD32 *)pu1_top_nnz = 0;
1188             u1_top_dc_csbp = 0;
1189             *(UWORD32 *)pu1_top_nnz_uv = 0;
1190         }
1191         else
1192         {
1193             *(UWORD32 *)pu1_top_nnz = 0x01010101;
1194             u1_top_dc_csbp = 0x3;
1195             *(UWORD32 *)pu1_top_nnz_uv = 0x01010101;
1196         }
1197     }
1198     else
1199     {
1200         UWORD32 *pu4_buf;
1201         UWORD8 *pu1_buf;
1202         pu1_buf = ps_cur_mb_info->ps_top_mb->pu1_nnz_y;
1203         pu4_buf = (UWORD32 *)pu1_buf;
1204         *(UWORD32 *)(pu1_top_nnz) = *pu4_buf;
1205 
1206         pu1_buf = ps_cur_mb_info->ps_top_mb->pu1_nnz_uv;
1207         pu4_buf = (UWORD32 *)pu1_buf;
1208         *(UWORD32 *)(pu1_top_nnz_uv) = *pu4_buf;
1209 
1210     }
1211 
1212     if(!(u4_nbr_avail & LEFT_MB_AVAILABLE_MASK))
1213     {
1214         if(p_curr_ctxt->u1_mb_type & CAB_INTRA_MASK)
1215         {
1216             UWORD32 *pu4_buf;
1217             UWORD8 *pu1_buf;
1218             *(UWORD32 *)pu1_left_nnz = 0;
1219             u1_left_dc_csbp = 0;
1220             pu1_buf = ps_dec->pu1_left_nnz_uv;
1221             pu4_buf = (UWORD32 *)pu1_buf;
1222             *pu4_buf = 0;
1223         }
1224         else
1225         {
1226             UWORD32 *pu4_buf;
1227             UWORD8 *pu1_buf;
1228             *(UWORD32 *)pu1_left_nnz = 0x01010101;
1229             u1_left_dc_csbp = 0x3;
1230             pu1_buf = ps_dec->pu1_left_nnz_uv;
1231             pu4_buf = (UWORD32 *)pu1_buf;
1232             *pu4_buf = 0x01010101;
1233         }
1234     }
1235 
1236     uc_ctx_cat = u1_offset ? LUMA_AC_CTXCAT : LUMA_4X4_CTXCAT;
1237 
1238     ps_cur_mb_info->u1_qp_div6 = ps_dec->u1_qp_y_div6;
1239     ps_cur_mb_info->u1_qpc_div6 = ps_dec->u1_qp_u_div6;
1240     ps_cur_mb_info->u1_qp_rem6 = ps_dec->u1_qp_y_rem6;
1241     ps_cur_mb_info->u1_qpc_rem6 = ps_dec->u1_qp_u_rem6;
1242     // CHECK_THIS
1243     ps_cur_mb_info->u1_qpcr_div6 = ps_dec->u1_qp_v_div6;
1244     ps_cur_mb_info->u1_qpcr_rem6 = ps_dec->u1_qp_v_rem6;
1245 
1246     if(u1_cbp & 0x0f)
1247     {
1248         if(ps_cur_mb_info->u1_tran_form8x8 == 0)
1249         {
1250             /*******************************************************************/
1251             /* Block 0 residual decoding, check cbp and proceed (subblock = 0) */
1252             /*******************************************************************/
1253             if(!(u1_cbp & 0x1))
1254             {
1255                 *(UWORD16 *)(pu1_top_nnz) = 0;
1256                 *(UWORD16 *)(pu1_left_nnz) = 0;
1257             }
1258             else
1259             {
1260                 ui16_csbp = ih264d_cabac_parse_8x8block(pi2_coeff_block, 4,
1261                                                         uc_ctx_cat, ps_dec,
1262                                                         pu1_top_nnz,
1263                                                         pu1_left_nnz);
1264             }
1265 
1266             /*******************************************************************/
1267             /* Block 1 residual decoding, check cbp and proceed (subblock = 2) */
1268             /*******************************************************************/
1269             pi2_coeff_block += (2 * NUM_COEFFS_IN_4x4BLK);
1270             if(!(u1_cbp & 0x2))
1271             {
1272                 *(UWORD16 *)(pu1_top_nnz + 2) = 0;
1273                 *(UWORD16 *)(pu1_left_nnz) = 0;
1274             }
1275             else
1276             {
1277                 UWORD32 u4_temp = ih264d_cabac_parse_8x8block(pi2_coeff_block,
1278                                                               4, uc_ctx_cat,
1279                                                               ps_dec,
1280                                                               (pu1_top_nnz + 2),
1281                                                               pu1_left_nnz);
1282                 ui16_csbp |= (u4_temp << 2);
1283             }
1284 
1285             /*******************************************************************/
1286             /* Block 2 residual decoding, check cbp and proceed (subblock = 8) */
1287             /*******************************************************************/
1288             pi2_coeff_block += (6 * NUM_COEFFS_IN_4x4BLK);
1289             if(!(u1_cbp & 0x4))
1290             {
1291                 *(UWORD16 *)(pu1_top_nnz) = 0;
1292                 *(UWORD16 *)(pu1_left_nnz + 2) = 0;
1293             }
1294             else
1295             {
1296                 UWORD32 u4_temp = ih264d_cabac_parse_8x8block(
1297                                 pi2_coeff_block, 4, uc_ctx_cat, ps_dec,
1298                                 pu1_top_nnz, (pu1_left_nnz + 2));
1299                 ui16_csbp |= (u4_temp << 8);
1300             }
1301 
1302             /*******************************************************************/
1303             /* Block 3 residual decoding, check cbp and proceed (subblock = 10)*/
1304             /*******************************************************************/
1305             pi2_coeff_block += (2 * NUM_COEFFS_IN_4x4BLK);
1306             if(!(u1_cbp & 0x8))
1307             {
1308                 *(UWORD16 *)(pu1_top_nnz + 2) = 0;
1309                 *(UWORD16 *)(pu1_left_nnz + 2) = 0;
1310             }
1311             else
1312             {
1313                 UWORD32 u4_temp = ih264d_cabac_parse_8x8block(
1314                                 pi2_coeff_block, 4, uc_ctx_cat, ps_dec,
1315                                 (pu1_top_nnz + 2), (pu1_left_nnz + 2));
1316                 ui16_csbp |= (u4_temp << 10);
1317             }
1318 
1319         }
1320         else
1321         {
1322             ui16_csbp = 0;
1323 
1324             /*******************************************************************/
1325             /* Block 0 residual decoding, check cbp and proceed (subblock = 0) */
1326             /*******************************************************************/
1327             if(!(u1_cbp & 0x1))
1328             {
1329                 *(UWORD16 *)(pu1_top_nnz) = 0;
1330                 *(UWORD16 *)(pu1_left_nnz) = 0;
1331             }
1332             else
1333             {
1334 
1335                 dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1336 
1337                 ih264d_read_coeff8x8_cabac( ps_bitstrm,
1338                                            ps_dec, ps_cur_mb_info);
1339 
1340                 pu1_left_nnz[0] = 1;
1341                 pu1_left_nnz[1] = 1;
1342 
1343                 pu1_top_nnz[0] = 1;
1344                 pu1_top_nnz[1] = 1;
1345 
1346                 /* added to be used by BS computation module */
1347                 ui16_csbp |= 0x0033;
1348             }
1349 
1350             /*******************************************************************/
1351             /* Block 1 residual decoding, check cbp and proceed (subblock = 2) */
1352             /*******************************************************************/
1353             pi2_coeff_block += 64;
1354 
1355             if(!(u1_cbp & 0x2))
1356             {
1357                 *(UWORD16 *)(pu1_top_nnz + 2) = 0;
1358                 *(UWORD16 *)(pu1_left_nnz) = 0;
1359             }
1360             else
1361             {
1362 
1363 
1364                 dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1365 
1366                 ih264d_read_coeff8x8_cabac(ps_bitstrm,
1367                                            ps_dec, ps_cur_mb_info);
1368 
1369                 pu1_left_nnz[0] = 1;
1370                 pu1_left_nnz[1] = 1;
1371 
1372                 pu1_top_nnz[2] = 1;
1373                 pu1_top_nnz[3] = 1;
1374 
1375                 /* added to be used by BS computation module */
1376                 ui16_csbp |= 0x00CC;
1377 
1378             }
1379 
1380             /*******************************************************************/
1381             /* Block 2 residual decoding, check cbp and proceed (subblock = 8) */
1382             /*******************************************************************/
1383             pi2_coeff_block += 64;
1384             if(!(u1_cbp & 0x4))
1385             {
1386                 *(UWORD16 *)(pu1_top_nnz) = 0;
1387                 *(UWORD16 *)(pu1_left_nnz + 2) = 0;
1388             }
1389             else
1390             {
1391 
1392                 dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1393 
1394                 ih264d_read_coeff8x8_cabac(ps_bitstrm,
1395                                            ps_dec, ps_cur_mb_info);
1396 
1397                 pu1_left_nnz[2] = 1;
1398                 pu1_left_nnz[3] = 1;
1399 
1400                 pu1_top_nnz[0] = 1;
1401                 pu1_top_nnz[1] = 1;
1402 
1403                 /* added to be used by BS computation module */
1404                 ui16_csbp |= 0x3300;
1405             }
1406 
1407             /*******************************************************************/
1408             /* Block 3 residual decoding, check cbp and proceed (subblock = 10)*/
1409             /*******************************************************************/
1410             pi2_coeff_block += 64;
1411 
1412             if(!(u1_cbp & 0x8))
1413             {
1414                 *(UWORD16 *)(pu1_top_nnz + 2) = 0;
1415                 *(UWORD16 *)(pu1_left_nnz + 2) = 0;
1416             }
1417             else
1418             {
1419 
1420                 dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
1421 
1422                 ih264d_read_coeff8x8_cabac(ps_bitstrm,
1423                                            ps_dec, ps_cur_mb_info);
1424 
1425                 pu1_left_nnz[2] = 1;
1426                 pu1_left_nnz[3] = 1;
1427 
1428                 pu1_top_nnz[2] = 1;
1429                 pu1_top_nnz[3] = 1;
1430 
1431                 /* added to be used by BS computation module */
1432                 ui16_csbp |= 0xCC00;
1433             }
1434         }
1435     }
1436     else
1437     {
1438         *(UWORD32 *)(pu1_top_nnz) = 0;
1439         *(UWORD32 *)(pu1_left_nnz) = 0;
1440     }
1441     /*--------------------------------------------------------------------*/
1442     /* Store the last row of N values to top row                          */
1443     /*--------------------------------------------------------------------*/
1444     ps_cur_mb_info->u2_luma_csbp = ui16_csbp;
1445     ps_cur_mb_info->ps_curmb->u2_luma_csbp = ui16_csbp;
1446     {
1447         WORD8 i;
1448         UWORD16 u2_chroma_csbp = 0;
1449         ps_cur_mb_info->u2_chroma_csbp = 0;
1450 
1451         u1_cbp >>= 4;
1452         pu1_top_nnz = pu1_top_nnz_uv;
1453         pu1_left_nnz = ps_dec->pu1_left_nnz_uv;
1454         /*--------------------------------------------------------------------*/
1455         /* if Chroma Component not present OR no ac values present            */
1456         /* Set the values of N to zero                                        */
1457         /*--------------------------------------------------------------------*/
1458         if(u1_cbp == CBPC_ALLZERO)
1459         {
1460             ps_dec->pu1_left_yuv_dc_csbp[0] &= 0x1;
1461             *(UWORD32 *)(pu1_top_nnz) = 0;
1462             *(UWORD32 *)(pu1_left_nnz) = 0;
1463             p_curr_ctxt->u1_yuv_dc_csbp &= 0x1;
1464             return (0);
1465         }
1466 
1467         /*--------------------------------------------------------------------*/
1468         /* Decode Chroma DC values                                            */
1469         /*--------------------------------------------------------------------*/
1470         for(i = 0; i < 2; i++)
1471         {
1472             UWORD8 uc_a = 1, uc_b = 1;
1473             UWORD32 u4_ctx_inc;
1474             UWORD8 uc_codedBlockFlag;
1475             UWORD8 pu1_inv_scan[4] =
1476                 { 0, 1, 2, 3 };
1477             WORD32 u4_scale;
1478             WORD32 i4_mb_inter_inc;
1479             tu_sblk4x4_coeff_data_t *ps_tu_4x4 =
1480                             (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
1481             WORD16 *pi2_coeff_data =
1482                             (WORD16 *)ps_dec->pv_parse_tu_coeff_data;
1483             WORD16 ai2_dc_coef[4];
1484 
1485             INC_SYM_COUNT(&(ps_dec->s_cab_dec_env));
1486             u4_scale = (i) ?
1487                             (ps_dec->pu2_quant_scale_v[0]
1488                                             << ps_dec->u1_qp_v_div6) :
1489                             (ps_dec->pu2_quant_scale_u[0]
1490                                             << ps_dec->u1_qp_u_div6);
1491 
1492             /*--------------------------------------------------------------------*/
1493             /* Decode Bitstream to get the DC coeff                               */
1494             /*--------------------------------------------------------------------*/
1495             uc_a = (u1_left_dc_csbp >> i) & 0x01;
1496             uc_b = (u1_top_dc_csbp >> i) & 0x01;
1497             u4_ctx_inc = (uc_a + (uc_b << 1));
1498 
1499             ps_src_bin_ctxt = (ps_dec->p_cbf_t[CHROMA_DC_CTXCAT]) + u4_ctx_inc;
1500 
1501             uc_codedBlockFlag =
1502                             ih264d_read_coeff4x4_cabac(ps_bitstrm,
1503                                             CHROMA_DC_CTXCAT,
1504                                             ps_dec->p_significant_coeff_flag_t[CHROMA_DC_CTXCAT],
1505                                             ps_dec, ps_src_bin_ctxt);
1506 
1507             i4_mb_inter_inc = (!((ps_cur_mb_info->ps_curmb->u1_mb_type == I_4x4_MB)
1508                             || (ps_cur_mb_info->ps_curmb->u1_mb_type == I_16x16_MB)))
1509                             * 3;
1510 
1511             if(ps_dec->s_high_profile.u1_scaling_present)
1512             {
1513                 u4_scale *=
1514                                 ps_dec->s_high_profile.i2_scalinglist4x4[i4_mb_inter_inc
1515                                                 + 1 + i][0];
1516 
1517             }
1518             else
1519             {
1520                 u4_scale <<= 4;
1521             }
1522 
1523             if(uc_codedBlockFlag)
1524             {
1525                 WORD32 i_z0, i_z1, i_z2, i_z3;
1526                 WORD32 *pi4_scale;
1527 
1528                 SET_BIT(u1_top_dc_csbp, i);
1529                 SET_BIT(u1_left_dc_csbp, i);
1530 
1531                 ai2_dc_coef[0] = 0;
1532                 ai2_dc_coef[1] = 0;
1533                 ai2_dc_coef[2] = 0;
1534                 ai2_dc_coef[3] = 0;
1535 
1536                 ih264d_unpack_coeff4x4_dc_4x4blk(ps_tu_4x4,
1537                                                  ai2_dc_coef,
1538                                                  pu1_inv_scan);
1539                 i_z0 = (ai2_dc_coef[0] + ai2_dc_coef[2]);
1540                 i_z1 = (ai2_dc_coef[0] - ai2_dc_coef[2]);
1541                 i_z2 = (ai2_dc_coef[1] - ai2_dc_coef[3]);
1542                 i_z3 = (ai2_dc_coef[1] + ai2_dc_coef[3]);
1543 
1544                 /*-----------------------------------------------------------*/
1545                 /* Scaling and storing the values back                       */
1546                 /*-----------------------------------------------------------*/
1547                 *pi2_coeff_data++ = ((i_z0 + i_z3) * u4_scale) >> 5;
1548                 *pi2_coeff_data++ = ((i_z0 - i_z3) * u4_scale) >> 5;
1549                 *pi2_coeff_data++ = ((i_z1 + i_z2) * u4_scale) >> 5;
1550                 *pi2_coeff_data++ = ((i_z1 - i_z2) * u4_scale) >> 5;
1551 
1552                 ps_dec->pv_parse_tu_coeff_data = (void *)pi2_coeff_data;
1553 
1554                 SET_BIT(ps_cur_mb_info->u1_yuv_dc_block_flag,(i+1));
1555             }
1556             else
1557             {
1558                 CLEARBIT(u1_top_dc_csbp, i);
1559                 CLEARBIT(u1_left_dc_csbp, i);
1560             }
1561         }
1562 
1563         /*********************************************************************/
1564         /*                   Update the DC csbp                              */
1565         /*********************************************************************/
1566         ps_dec->pu1_left_yuv_dc_csbp[0] &= 0x1;
1567         p_curr_ctxt->u1_yuv_dc_csbp &= 0x1;
1568         ps_dec->pu1_left_yuv_dc_csbp[0] |= (u1_left_dc_csbp << 1);
1569         p_curr_ctxt->u1_yuv_dc_csbp |= (u1_top_dc_csbp << 1);
1570         if(u1_cbp == CBPC_ACZERO)
1571         {
1572             *(UWORD32 *)(pu1_top_nnz) = 0;
1573             *(UWORD32 *)(pu1_left_nnz) = 0;
1574             return (0);
1575         }
1576         /*--------------------------------------------------------------------*/
1577         /* Decode Chroma AC values                                            */
1578         /*--------------------------------------------------------------------*/
1579         {
1580             UWORD32 u4_temp;
1581             /*****************************************************************/
1582             /* U Block  residual decoding, check cbp and proceed (subblock=0)*/
1583             /*****************************************************************/
1584             u2_chroma_csbp = ih264d_cabac_parse_8x8block(pi2_coeff_block, 2,
1585             CHROMA_AC_CTXCAT,
1586                                                          ps_dec, pu1_top_nnz,
1587                                                          pu1_left_nnz);
1588 
1589             pi2_coeff_block += MB_CHROM_SIZE;
1590             /*****************************************************************/
1591             /* V Block  residual decoding, check cbp and proceed (subblock=1)*/
1592             /*****************************************************************/
1593             u4_temp = ih264d_cabac_parse_8x8block(pi2_coeff_block, 2,
1594             CHROMA_AC_CTXCAT,
1595                                                   ps_dec, (pu1_top_nnz + 2),
1596                                                   (pu1_left_nnz + 2));
1597             u2_chroma_csbp |= (u4_temp << 4);
1598         }
1599         /*********************************************************************/
1600         /*                   Update the AC csbp                              */
1601         /*********************************************************************/
1602         ps_cur_mb_info->u2_chroma_csbp = u2_chroma_csbp;
1603     }
1604 
1605     return (0);
1606 }
1607 
1608