1/******************************************************************************
2*
3* Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
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/**
19*******************************************************************************
20* @file
21*  ihevcd_parse_residual.c
22*
23* @brief
24*  Contains functions for parsing residual data at TU level
25*
26* @author
27*  Harish
28*
29* @par List of Functions:
30*
31* @remarks
32*  None
33*
34*******************************************************************************
35*/
36/*****************************************************************************/
37/* File Includes                                                             */
38/*****************************************************************************/
39#include <stdio.h>
40#include <stddef.h>
41#include <stdlib.h>
42#include <string.h>
43#include <assert.h>
44
45#include "ihevc_typedefs.h"
46#include "iv.h"
47#include "ivd.h"
48#include "ihevcd_cxa.h"
49
50#include "ihevc_defs.h"
51#include "ihevc_debug.h"
52#include "ihevc_structs.h"
53#include "ihevc_macros.h"
54#include "ihevc_platform_macros.h"
55
56#include "ihevc_common_tables.h"
57#include "ihevc_error.h"
58#include "ihevc_cabac_tables.h"
59
60#include "ihevcd_trace.h"
61#include "ihevcd_defs.h"
62#include "ihevcd_function_selector.h"
63#include "ihevcd_structs.h"
64#include "ihevcd_error.h"
65#include "ihevcd_nal.h"
66#include "ihevcd_bitstream.h"
67#include "ihevcd_utils.h"
68#include "ihevcd_parse_residual.h"
69#include "ihevcd_cabac.h"
70
71/**
72  *****************************************************************************
73  * @brief  returns context increment for sig coeff based on csbf neigbour
74  *         flags (bottom and right) and current coeff postion in 4x4 block
75  *         See section 9.3.3.1.4 for details on this context increment
76  *
77  * input   : neigbour csbf flags(bit0:rightcsbf, bit1:bottom csbf)
78  *           coeff idx in raster order (0-15)
79  *
80  * output  : context increment for sig coeff flag
81  *
82  *****************************************************************************
83  */
84const UWORD8 gau1_ihevcd_sigcoeff_ctxtinc[3][4][16] =
85{
86
87    {
88        /* nbr csbf = 0:  sigCtx = (xP+yP == 0) ? 2 : (xP+yP < 3) ? 1: 0 */
89        { 2,    1,    1,    1,    1,    1,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0 },
90        /* nbr csbf = 1:  sigCtx = (yP == 0) ? 2 : (yP == 1) ? 1: 0      */
91        { 2,    1,    2,    0,    1,    2,    0,    0,    1,    2,    0,    0,    1,    0,    0,    0 },
92        /* nbr csbf = 2:  sigCtx = (xP == 0) ? 2 : (xP == 1) ? 1: 0      */
93        { 2,    2,    1,    2,    1,    0,    2,    1,    0,    0,    1,    0,    0,    0,    0,    0 },
94        /* nbr csbf = 3:  sigCtx = 2                                     */
95        { 2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2 },
96    },
97    {
98        /* nbr csbf = 0:  sigCtx = (xP+yP == 0) ? 2 : (xP+yP < 3) ? 1: 0 */
99        { 2,    1,    1,    0,    1,    1,    0,    0,    1,    0,    0,    0,    0,    0,    0,    0 },
100        /* nbr csbf = 1:  sigCtx = (yP == 0) ? 2 : (yP == 1) ? 1: 0      */
101        { 2,    2,    2,    2,    1,    1,    1,    1,    0,    0,    0,    0,    0,    0,    0,    0 },
102        /* nbr csbf = 2:  sigCtx = (xP == 0) ? 2 : (xP == 1) ? 1: 0      */
103        { 2,    1,    0,    0,    2,    1,    0,    0,    2,    1,    0,    0,    2,    1,    0,    0 },
104        /* nbr csbf = 3:  sigCtx = 2                                     */
105        { 2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2 },
106    },
107    {
108        /* nbr csbf = 0:  sigCtx = (xP+yP == 0) ? 2 : (xP+yP < 3) ? 1: 0 */
109        { 2,    1,    1,    0,    1,    1,    0,    0,    1,    0,    0,    0,    0,    0,    0,    0 },
110        /* nbr csbf = 1:  sigCtx = (yP == 0) ? 2 : (yP == 1) ? 1: 0      */
111        { 2,    1,    0,    0,    2,    1,    0,    0,    2,    1,    0,    0,    2,    1,    0,    0 },
112        /* nbr csbf = 2:  sigCtx = (xP == 0) ? 2 : (xP == 1) ? 1: 0      */
113        { 2,    2,    2,    2,    1,    1,    1,    1,    0,    0,    0,    0,    0,    0,    0,    0 },
114        /* nbr csbf = 3:  sigCtx = 2                                     */
115        { 2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2,    2 },
116    },
117
118
119};
120
121
122
123/**
124  *****************************************************************************
125  * @brief  returns context increment for sig coeff for 4x4 tranform size as
126  *         per Table 9-39 in section 9.3.3.1.4
127  *
128  * input   : coeff idx in raster order (0-15)
129  *
130  * output  : context increment for sig coeff flag
131  *
132  *****************************************************************************
133  */
134const UWORD8 gau1_ihevcd_sigcoeff_ctxtinc_tr4[3][16] =
135{
136    /* Upright diagonal scan */
137    {
138        0,    2,    1,    6,
139        3,    4,    7,    6,
140        4,    5,    7,    8,
141        5,    8,    8,    8,
142    },
143    /* Horizontal scan */
144    {
145        0,    1,    4,    5,
146        2,    3,    4,    5,
147        6,    6,    8,    8,
148        7,    7,    8,    8,
149    },
150    /* Vertical scan */
151    {
152        0,    2,    6,    7,
153        1,    3,    6,    7,
154        4,    4,    8,    8,
155        5,    5,    8,    8,
156    },
157};
158
159
160/**
161*******************************************************************************
162*
163* @brief
164*  Parses Residual coding
165*
166* @par Description:
167*  Parses Residual coding as per  Section:7.3.13
168*
169* @param[in] ps_codec
170*  Pointer to codec context
171*
172* @returns  error code from IHEVCD_ERROR_T
173*
174* @remarks
175*
176*
177*******************************************************************************
178*/
179
180WORD32 ihevcd_parse_residual_coding(codec_t *ps_codec,
181                                    WORD32 x0, WORD32 y0,
182                                    WORD32 log2_trafo_size,
183                                    WORD32 c_idx,
184                                    WORD32 intra_pred_mode)
185{
186    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
187    WORD32 transform_skip_flag;
188    WORD32 value;
189    pps_t *ps_pps;
190    WORD32 last_scan_pos, last_sub_blk;
191    bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
192    WORD32 last_significant_coeff_x_prefix, last_significant_coeff_y_prefix;
193    WORD32 last_significant_coeff_x, last_significant_coeff_y;
194    const UWORD8 *pu1_scan_blk, *pu1_scan_coeff;
195    WORD32 scan_idx;
196    WORD32 i;
197    WORD32 sign_data_hiding_flag;
198    cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
199    WORD32 gt1_ctxt = 1;
200    WORD32 c_max;
201    UWORD16 au2_csbf[9];
202    tu_sblk_coeff_data_t *ps_tu_sblk_coeff_data;
203    WORD8 *pi1_num_coded_subblks;
204    WORD32 num_subblks;
205    WORD32 sig_coeff_base_ctxt, abs_gt1_base_ctxt;
206    UNUSED(x0);
207    UNUSED(y0);
208    ps_pps = ps_codec->s_parse.ps_pps;
209
210    sign_data_hiding_flag = ps_pps->i1_sign_data_hiding_flag;
211    transform_skip_flag = 0;
212    if(ps_pps->i1_transform_skip_enabled_flag &&
213       !ps_codec->s_parse.s_cu.i4_cu_transquant_bypass &&
214       (log2_trafo_size == 2))
215    {
216        WORD32 ctxt_idx;
217
218        if(!c_idx)
219        {
220            ctxt_idx = IHEVC_CAB_TFM_SKIP0;
221        }
222        else
223        {
224            ctxt_idx = IHEVC_CAB_TFM_SKIP12;
225        }
226        TRACE_CABAC_CTXT("transform_skip_flag", ps_cabac->u4_range, ctxt_idx);
227        value = ihevcd_cabac_decode_bin(ps_cabac,
228                                        ps_bitstrm,
229                                        ctxt_idx);
230        AEV_TRACE("transform_skip_flag", value, ps_cabac->u4_range);
231        transform_skip_flag = value;
232    }
233
234    /* code the last_coeff_x_prefix as tunary binarized code */
235    {
236        WORD32 ctxt_idx_x, ctxt_idx_y, ctx_shift;
237        WORD32 ctx_offset;
238        c_max = (log2_trafo_size << 1) - 1;
239
240        if(!c_idx)
241        {
242            ctx_offset = (3 * (log2_trafo_size - 2)) + ((log2_trafo_size - 1) >> 2);
243            ctxt_idx_x = IHEVC_CAB_COEFFX_PREFIX + ctx_offset;
244            ctxt_idx_y = IHEVC_CAB_COEFFY_PREFIX + ctx_offset;
245            ctx_shift  = (log2_trafo_size + 1) >> 2;
246        }
247        else
248        {
249            ctxt_idx_x = IHEVC_CAB_COEFFX_PREFIX + 15;
250            ctxt_idx_y = IHEVC_CAB_COEFFY_PREFIX + 15;
251            ctx_shift  = log2_trafo_size  - 2;
252        }
253
254        TRACE_CABAC_CTXT("last_coeff_x_prefix", ps_cabac->u4_range, ctxt_idx_x);
255        last_significant_coeff_x_prefix = ihevcd_cabac_decode_bins_tunary(ps_cabac,
256                                                                          ps_bitstrm,
257                                                                          c_max,
258                                                                          ctxt_idx_x,
259                                                                          ctx_shift,
260                                                                          c_max);
261
262        AEV_TRACE("last_coeff_x_prefix", last_significant_coeff_x_prefix, ps_cabac->u4_range);
263
264        TRACE_CABAC_CTXT("last_coeff_y_prefix", ps_cabac->u4_range, ctxt_idx_y);
265        last_significant_coeff_y_prefix = ihevcd_cabac_decode_bins_tunary(ps_cabac,
266                                                                          ps_bitstrm,
267                                                                          c_max,
268                                                                          ctxt_idx_y,
269                                                                          ctx_shift,
270                                                                          c_max);
271
272        AEV_TRACE("last_coeff_y_prefix", last_significant_coeff_y_prefix, ps_cabac->u4_range);
273
274
275        last_significant_coeff_x = last_significant_coeff_x_prefix;
276        if(last_significant_coeff_x_prefix > 3)
277        {
278            WORD32 suf_length = ((last_significant_coeff_x_prefix - 2) >> 1);
279
280            value = ihevcd_cabac_decode_bypass_bins(ps_cabac,
281                                                    ps_bitstrm,
282                                                    suf_length);
283
284            AEV_TRACE("last_coeff_x_suffix", value, ps_cabac->u4_range);
285
286
287            last_significant_coeff_x =
288                            (1 << ((last_significant_coeff_x_prefix >> 1) - 1)) *
289                            (2 + (last_significant_coeff_x_prefix & 1)) + value;
290        }
291
292
293        last_significant_coeff_y = last_significant_coeff_y_prefix;
294        if(last_significant_coeff_y_prefix > 3)
295        {
296            WORD32 suf_length = ((last_significant_coeff_y_prefix - 2) >> 1);
297            value = ihevcd_cabac_decode_bypass_bins(ps_cabac,
298                                                    ps_bitstrm,
299                                                    suf_length);
300
301            AEV_TRACE("last_coeff_y_suffix", value, ps_cabac->u4_range);
302            last_significant_coeff_y =
303                            (1 << ((last_significant_coeff_y_prefix >> 1) - 1)) *
304                            (2 + (last_significant_coeff_y_prefix & 1)) + value;
305        }
306
307    }
308
309    /* Choose a scan matrix based on intra flag, intra pred mode, transform size
310     and luma/chroma */
311    scan_idx = SCAN_DIAG_UPRIGHT;
312    if(PRED_MODE_INTRA == ps_codec->s_parse.s_cu.i4_pred_mode)
313    {
314        if((2 == log2_trafo_size) || ((3 == log2_trafo_size) && (0 == c_idx)))
315        {
316            if((6 <= intra_pred_mode) &&
317               (14 >= intra_pred_mode))
318            {
319                scan_idx = SCAN_VERT;
320            }
321            else if((22 <= intra_pred_mode) &&
322                    (30 >= intra_pred_mode))
323            {
324                scan_idx = SCAN_HORZ;
325            }
326        }
327    }
328
329    /* In case the scan is vertical, then swap  X and Y positions */
330    if(SCAN_VERT == scan_idx)
331    {
332        SWAP(last_significant_coeff_x, last_significant_coeff_y);
333    }
334
335    {
336        WORD8 *pi1_scan_idx;
337        WORD8 *pi1_buf = (WORD8 *)ps_codec->s_parse.pv_tu_coeff_data;
338
339        /* First WORD8 gives number of coded subblocks */
340        pi1_num_coded_subblks = pi1_buf++;
341
342        /* Set number of coded subblocks in the current TU to zero */
343        /* This will be updated later */
344        *pi1_num_coded_subblks = 0;
345
346        /* Second WORD8 gives (scan idx << 1) | trans_skip */
347        pi1_scan_idx = pi1_buf++;
348        *pi1_scan_idx = (scan_idx << 1) | transform_skip_flag;
349
350        /* Store the incremented pointer in pv_tu_coeff_data */
351        ps_codec->s_parse.pv_tu_coeff_data = pi1_buf;
352
353    }
354    /**
355     * Given last_significant_coeff_y and last_significant_coeff_x find last sub block
356     * This is done by ignoring lower two bits of last_significant_coeff_y and last_significant_coeff_x
357     * and using scan matrix for lookup
358     */
359
360    /* If transform is 4x4, last_sub_blk is zero */
361    last_sub_blk = 0;
362
363    /* If transform is larger than 4x4, then based on scan_idx and transform size, choose a scan table */
364
365    if(log2_trafo_size > 2)
366    {
367        WORD32 scan_pos;
368        WORD32 scan_mat_size;
369        pu1_scan_blk = (UWORD8 *)gapv_ihevc_scan[scan_idx * 3 + (log2_trafo_size - 2 - 1)];
370
371
372        /* Divide the current transform to 4x4 subblocks and count number of 4x4 in the first row */
373        /* This will be size of scan matrix to be used for subblock scanning */
374        scan_mat_size = 1 << (log2_trafo_size - 2);
375        scan_pos = ((last_significant_coeff_y >> 2) * scan_mat_size) +
376                        (last_significant_coeff_x >> 2);
377
378        last_sub_blk = pu1_scan_blk[scan_pos];
379    }
380    pu1_scan_coeff  = &gau1_ihevc_scan4x4[scan_idx][0];
381
382    {
383        WORD32 scan_pos;
384
385        scan_pos = ((last_significant_coeff_y & 3) << 2) +
386                        (last_significant_coeff_x & 3);
387
388        last_scan_pos = pu1_scan_coeff[scan_pos];
389    }
390    pu1_scan_blk = (UWORD8 *)gapv_ihevc_invscan[scan_idx * 3 + (log2_trafo_size - 2 - 1)];
391    pu1_scan_coeff  = &gau1_ihevc_invscan4x4[scan_idx][0];
392
393    /* Set CSBF array to zero */
394    {
395        UWORD32 *pu4_csbf;
396        pu4_csbf = (void *)au2_csbf;
397        *pu4_csbf++ = 0;
398        *pu4_csbf++ = 0;
399        *pu4_csbf++ = 0;
400        *pu4_csbf = 0;
401        /* To avoid a check for y pos, 9th WORD16 in the array is set to zero */
402        au2_csbf[8] = 0;
403    }
404
405    /*************************************************************************/
406    /* derive base context index for sig coeff as per section 9.3.3.1.4      */
407    /* TODO; convert to look up based on luma/chroma, scan type and tfr size */
408    /*************************************************************************/
409    if(!c_idx)
410    {
411        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG;
412        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG;
413
414        if(3 == log2_trafo_size)
415        {
416            /* 8x8 transform size */
417            sig_coeff_base_ctxt += (scan_idx == SCAN_DIAG_UPRIGHT) ? 9 : 15;
418        }
419        else  if(3 < log2_trafo_size)
420        {
421            /* larger transform sizes */
422            sig_coeff_base_ctxt += 21;
423        }
424    }
425    else
426    {
427        /* chroma context initializations */
428        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG + 27;
429        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG + 16;
430
431        if(3 == log2_trafo_size)
432        {
433            /* 8x8 transform size */
434            sig_coeff_base_ctxt += 9;
435        }
436        else  if(3 < log2_trafo_size)
437        {
438            /* larger transform sizes */
439            sig_coeff_base_ctxt += 12;
440        }
441    }
442    num_subblks = 0;
443    /* Parse each 4x4 subblocks */
444    for(i = last_sub_blk; i >= 0; i--)
445    {
446        WORD32 sub_blk_pos;
447        WORD32 infer_sig_coeff_flag;
448        WORD32 cur_csbf;
449
450        WORD32 n;
451        WORD32 num_coeff;
452        /* Sig coeff map for 16 entries in raster scan order. Upper 16 bits are used.
453         * MSB gives sig coeff flag for 0th coeff and so on
454         * UWORD16 would have been enough but kept as UWORD32 for code optimizations
455         * In arm unnecessary masking operations are saved
456         */
457        UWORD32 u4_sig_coeff_map_raster;
458        WORD32 sign_hidden;
459
460        /* Sig coeff map in scan order */
461        UWORD32 u4_sig_coeff_map;
462        WORD32 coeff_abs_level_greater2_flag;
463        UWORD32 u4_coeff_abs_level_greater1_map;
464        UWORD32 u4_coeff_abs_level_greater2_map;
465        UWORD32 u4_coeff_sign_map;
466        WORD32 first_sig_scan_pos, last_sig_scan_pos, num_greater1_flag, first_greater1_scan_pos;
467        WORD32  num_sig_coeff, sum_abs_level;
468        WORD32 nbr_csbf;
469
470
471        WORD32 ctxt_set;
472        WORD32 rice_param;
473        WORD32 xs, ys;
474
475
476        sub_blk_pos  = 0;
477        if(i && (log2_trafo_size > 2))
478            sub_blk_pos = pu1_scan_blk[i];
479
480        /* Get xs and ys from scan position */
481        /* This is needed for context modelling of significant coeff flag */
482        xs = sub_blk_pos & ((1 << (log2_trafo_size - 2)) - 1);
483        ys = sub_blk_pos >> (log2_trafo_size - 2);
484
485
486        /* Check if neighbor subblocks are coded */
487        {
488
489            nbr_csbf = 0;
490
491            /* Get Bottom sub blocks CSBF */
492            nbr_csbf |= (au2_csbf[ys + 1] >> xs) & 1;
493            nbr_csbf <<= 1;
494
495            /* Get Right sub blocks CSBF */
496            /* Even if xs is equal to (1 << (log2_trafo_size - 2 )) - 1,
497               since au2_csbf is set to zero at the beginning, csbf for
498               neighbor will be read as 0 */
499
500            nbr_csbf |= (au2_csbf[ys] >> (xs + 1)) & 1;
501
502
503        }
504        cur_csbf = 0;
505
506        /* DC coeff is inferred, only if coded_sub_block is explicitly parsed as 1 */
507        /* i.e. it is not inferred for first and last subblock */
508        infer_sig_coeff_flag = 0;
509        if((i < last_sub_blk) && (i > 0))
510        {
511            WORD32 ctxt_idx  = IHEVC_CAB_CODED_SUBLK_IDX;
512
513            /* ctxt based on right / bottom avail csbf, section 9.3.3.1.3 */
514            ctxt_idx += (nbr_csbf) ? 1 : 0;
515
516            /* Ctxt based on luma or chroma */
517            ctxt_idx += c_idx  ? 2 : 0;
518            TRACE_CABAC_CTXT("coded_sub_block_flag", ps_cabac->u4_range, ctxt_idx);
519            IHEVCD_CABAC_DECODE_BIN(cur_csbf, ps_cabac, ps_bitstrm, ctxt_idx);
520            AEV_TRACE("coded_sub_block_flag", cur_csbf, ps_cabac->u4_range);
521
522            infer_sig_coeff_flag = 1;
523        }
524        else /* if((i == last_sub_blk) || (sub_blk_pos == 0)) */
525        {
526            /* CSBF is set to 1 for first and last subblock */
527            /* Note for these subblocks sig_coeff_map is not inferred but instead parsed */
528            cur_csbf = 1;
529        }
530
531        /* Set current sub blocks CSBF */
532        {
533            UWORD32 u4_mask = 1 << xs;
534            if(cur_csbf)
535                au2_csbf[ys] |= u4_mask;
536            else
537                au2_csbf[ys] &= ~u4_mask;
538
539        }
540
541        /* If current subblock is not coded, proceed to the next subblock */
542        if(0 == cur_csbf)
543            continue;
544
545        n = 15;
546        u4_sig_coeff_map_raster = 0;
547        u4_sig_coeff_map = 0;
548        num_coeff = 0;
549        if(i == last_sub_blk)
550        {
551            WORD32 pos = ((last_significant_coeff_y & 3) << 2) +
552                            (last_significant_coeff_x & 3);
553            n = (last_scan_pos - 1);
554            /* Set Significant coeff map for last significant coeff flag as 1 */
555            u4_sig_coeff_map_raster = 1 << pos;
556            u4_sig_coeff_map = 1 << last_scan_pos;
557            num_coeff = 1;
558        }
559
560        for(; n >= 0; n--)
561        {
562            WORD32 significant_coeff_flag;
563
564            if((n > 0 || !infer_sig_coeff_flag))
565            {
566                //WORD32 coeff_pos;
567                WORD32 sig_ctxinc;
568                WORD32 ctxt_idx;
569
570                /* Coefficient position is needed for deriving context index for significant_coeff_flag */
571                //coeff_pos = pu1_scan_coeff[n];
572                /* derive the context inc as per section 9.3.3.1.4 */
573                sig_ctxinc = 0;
574                if(2 == log2_trafo_size)
575                {
576
577                    /* 4x4 transform size increment uses lookup */
578                    sig_ctxinc = gau1_ihevcd_sigcoeff_ctxtinc_tr4[scan_idx][n];
579                }
580                else if(n || i)
581                {
582                    /* ctxt for AC coeff depends on curpos and neigbour csbf */
583                    sig_ctxinc = gau1_ihevcd_sigcoeff_ctxtinc[scan_idx][nbr_csbf][n];
584
585                    /* based on luma subblock pos */
586                    sig_ctxinc += (i && (!c_idx)) ? 3 : 0;
587
588                }
589                else
590                {
591                    /* DC coeff has fixed context for luma and chroma */
592                    sig_coeff_base_ctxt = (0 == c_idx) ? IHEVC_CAB_COEFF_FLAG :
593                                                         (IHEVC_CAB_COEFF_FLAG + 27);
594                }
595
596                ctxt_idx = sig_ctxinc + sig_coeff_base_ctxt;
597                TRACE_CABAC_CTXT("significant_coeff_flag", ps_cabac->u4_range, ctxt_idx);
598                IHEVCD_CABAC_DECODE_BIN(significant_coeff_flag, ps_cabac,
599                                        ps_bitstrm,
600                                        ctxt_idx);
601                AEV_TRACE("significant_coeff_flag", significant_coeff_flag, ps_cabac->u4_range);
602
603
604                /* If at least one non-zero coeff is signalled then do not infer sig coeff map */
605                /* for (0,0) coeff in the current sub block */
606                if(significant_coeff_flag)
607                    infer_sig_coeff_flag = 0;
608
609//                u4_sig_coeff_map_raster |= significant_coeff_flag
610//                              << coeff_pos;
611                u4_sig_coeff_map |= significant_coeff_flag << n;
612                num_coeff += significant_coeff_flag;
613            }
614
615
616        }
617        /*********************************************************************/
618        /* If infer_sig_coeff_flag is 1 then treat the 0th coeff as non zero */
619        /* If infer_sig_coeff_flag is zero, then last significant_coeff_flag */
620        /* is parsed in the above loop                                       */
621        /*********************************************************************/
622        if(infer_sig_coeff_flag)
623        {
624            u4_sig_coeff_map_raster |= 1;
625            u4_sig_coeff_map |= 1;
626            num_coeff++;
627        }
628
629        /*********************************************************************/
630        /* First subblock does not get an explicit csbf. It is assumed to    */
631        /* be 1. For this subblock there is chance of getting all            */
632        /* sig_coeff_flags to be zero. In such a case proceed to the next    */
633        /* subblock(which is end of parsing for the current transform block) */
634        /*********************************************************************/
635
636        if(0 == num_coeff)
637            continue;
638
639        /* Increment number of coded subblocks for the current TU */
640        num_subblks++;
641
642        /* Set sig coeff map and subblock position */
643        ps_tu_sblk_coeff_data = (tu_sblk_coeff_data_t *)ps_codec->s_parse.pv_tu_coeff_data;
644        ps_tu_sblk_coeff_data->u2_sig_coeff_map = u4_sig_coeff_map;
645        ps_tu_sblk_coeff_data->u2_subblk_pos = (ys << 8) | xs;
646
647        first_sig_scan_pos = 16;
648        last_sig_scan_pos = -1;
649        num_greater1_flag = 0;
650        first_greater1_scan_pos = -1;
651        u4_coeff_abs_level_greater1_map = 0;
652
653
654        /* context set based on luma subblock pos */
655        ctxt_set = (i && (!c_idx)) ? 2 : 0;
656
657        /* See section 9.3.3.1.5           */
658        ctxt_set += (0 == gt1_ctxt) ? 1 : 0;
659
660        gt1_ctxt = 1;
661        /* Instead of initializing n to 15, set it to 31-CLZ(sig coeff map) */
662        {
663            UWORD32 u4_sig_coeff_map_shift;
664            UWORD32 clz;
665            clz = CLZ(u4_sig_coeff_map);
666            n = 31 - clz;
667            u4_sig_coeff_map_shift = u4_sig_coeff_map << clz;
668            /* For loop for n changed to do while to break early if sig_coeff_map_shift becomes zero */
669            do
670            {
671                //WORD32 coeff_pos;
672                WORD32 ctxt_idx;
673
674                //TODO: Scan lookup will be removed later and instead u4_sig_coeff_map will be used
675                //coeff_pos = pu1_scan_coeff[n];
676
677                if((u4_sig_coeff_map_shift >> 31) & 1)
678                {
679
680                    /* abs_level_greater1_flag is sent for only first 8 non-zero levels in a subblock */
681                    if(num_greater1_flag < 8)
682                    {
683                        WORD32 coeff_abs_level_greater1_flag;
684
685                        ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
686
687                        TRACE_CABAC_CTXT("coeff_abs_level_greater1_flag", ps_cabac->u4_range, ctxt_idx);
688                        IHEVCD_CABAC_DECODE_BIN(coeff_abs_level_greater1_flag, ps_cabac, ps_bitstrm, ctxt_idx);
689                        AEV_TRACE("coeff_abs_level_greater1_flag", coeff_abs_level_greater1_flag, ps_cabac->u4_range);
690
691                        u4_coeff_abs_level_greater1_map |= coeff_abs_level_greater1_flag << n;
692                        num_greater1_flag++;
693
694                        /* first_greater1_scan_pos is obtained using CLZ on u4_coeff_abs_level_greater1_map*/
695                        /*  outside the loop instead of the following check inside the loop                */
696                        /* if( coeff_abs_level_greater1_flag && first_greater1_scan_pos == -1) */
697                        /*    first_greater1_scan_pos = n;                                     */
698
699                        if(coeff_abs_level_greater1_flag)
700                        {
701                            gt1_ctxt = 0;
702                        }
703                        else if(gt1_ctxt && (gt1_ctxt < 3))
704                        {
705                            gt1_ctxt++;
706                        }
707
708                    }
709                    else
710                        break;
711
712                    /* instead of computing last and first significan scan position using checks below */
713                    /* They are computed outside the loop using CLZ and CTZ on sig_coeff_map */
714                    /* if(last_sig_scan_pos == -1)                          */
715                    /*    last_sig_scan_pos = n;                            */
716                    /*  first_sig_scan_pos = n;                             */
717                }
718                u4_sig_coeff_map_shift <<= 1;
719                n--;
720                /* If there are zero coeffs, then shift by as many zero coeffs and decrement n */
721                clz = CLZ(u4_sig_coeff_map_shift);
722                u4_sig_coeff_map_shift <<= clz;
723                n -= clz;
724            }while(u4_sig_coeff_map_shift);
725        }
726        /* At this level u4_sig_coeff_map is non-zero i.e. has atleast one non-zero coeff */
727        last_sig_scan_pos = (31 - CLZ(u4_sig_coeff_map));
728        first_sig_scan_pos = CTZ(u4_sig_coeff_map);
729        sign_hidden = (((last_sig_scan_pos - first_sig_scan_pos) > 3) && !ps_codec->s_parse.s_cu.i4_cu_transquant_bypass);
730
731        u4_coeff_abs_level_greater2_map = 0;
732
733        if(u4_coeff_abs_level_greater1_map)
734        {
735            /* Check if the first level > 1 is greater than 2 */
736            WORD32 ctxt_idx;
737            first_greater1_scan_pos = (31 - CLZ(u4_coeff_abs_level_greater1_map));
738
739
740            ctxt_idx = IHEVC_CAB_COEFABS_GRTR2_FLAG;
741
742            ctxt_idx += (!c_idx) ? ctxt_set : (ctxt_set + 4);
743            TRACE_CABAC_CTXT("coeff_abs_level_greater2_flag", ps_cabac->u4_range, ctxt_idx);
744            IHEVCD_CABAC_DECODE_BIN(coeff_abs_level_greater2_flag, ps_cabac, ps_bitstrm, ctxt_idx);
745            AEV_TRACE("coeff_abs_level_greater2_flag", coeff_abs_level_greater2_flag, ps_cabac->u4_range);
746            u4_coeff_abs_level_greater2_map = coeff_abs_level_greater2_flag << first_greater1_scan_pos;
747        }
748
749
750        u4_coeff_sign_map = 0;
751
752        /* Parse sign flags */
753        if(!sign_data_hiding_flag || !sign_hidden)
754        {
755            IHEVCD_CABAC_DECODE_BYPASS_BINS(value, ps_cabac, ps_bitstrm, num_coeff);
756            AEV_TRACE("sign_flags", value, ps_cabac->u4_range);
757            u4_coeff_sign_map = value << (32 - num_coeff);
758        }
759        else
760        {
761            IHEVCD_CABAC_DECODE_BYPASS_BINS(value, ps_cabac, ps_bitstrm, (num_coeff - 1));
762            AEV_TRACE("sign_flags", value, ps_cabac->u4_range);
763            u4_coeff_sign_map = value << (32 - (num_coeff - 1));
764        }
765
766        num_sig_coeff = 0;
767        sum_abs_level = 0;
768        rice_param = 0;
769        {
770            UWORD32 clz;
771            UWORD32 u4_sig_coeff_map_shift;
772            clz = CLZ(u4_sig_coeff_map);
773            n = 31 - clz;
774            u4_sig_coeff_map_shift = u4_sig_coeff_map << clz;
775            /* For loop for n changed to do while to break early if sig_coeff_map_shift becomes zero */
776            do
777            {
778
779                if((u4_sig_coeff_map_shift >> 31) & 1)
780                {
781                    WORD32 base_lvl;
782                    WORD32 coeff_abs_level_remaining;
783                    WORD32 level;
784                    base_lvl = 1;
785
786                    /* Update base_lvl if it is greater than 1 */
787                    if((u4_coeff_abs_level_greater1_map >> n) & 1)
788                        base_lvl++;
789
790                    /* Update base_lvl if it is greater than 2 */
791                    if((u4_coeff_abs_level_greater2_map >> n) & 1)
792                        base_lvl++;
793
794                    /* If level is greater than 3/2/1 based on the greater1 and greater2 maps,
795                     * decode remaining level (level - base_lvl) will be signalled as bypass bins
796                     */
797                    coeff_abs_level_remaining = 0;
798                    if(base_lvl == ((num_sig_coeff < 8) ? ((n == first_greater1_scan_pos) ? 3 : 2) : 1))
799                    {
800                        UWORD32 u4_prefix;
801                        WORD32 bin;
802
803                        u4_prefix = 0;
804
805                        do
806                        {
807                            IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm);
808                            u4_prefix++;
809
810                            if((WORD32)u4_prefix == 19 - rice_param)
811                            {
812                                bin = 1;
813                                break;
814                            }
815
816                        }while(bin);
817
818                        u4_prefix = u4_prefix - 1;
819                        if(u4_prefix < 3)
820                        {
821                            UWORD32 u4_suffix;
822
823                            coeff_abs_level_remaining = (u4_prefix << rice_param);
824                            if(rice_param)
825                            {
826                                IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, rice_param);
827
828                                coeff_abs_level_remaining |= u4_suffix;
829                            }
830                        }
831                        else
832                        {
833                            UWORD32 u4_suffix;
834                            UWORD32 u4_numbins;
835
836                            //u4_prefix = CLIP3(u4_prefix, 0, 19 - rice_param);
837
838                            u4_numbins = (u4_prefix - 3 + rice_param);
839                            coeff_abs_level_remaining = (((1 << (u4_prefix - 3)) + 3 - 1) << rice_param);
840                            if(u4_numbins)
841                            {
842                                IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, u4_numbins);
843                                coeff_abs_level_remaining += u4_suffix;
844                            }
845                        }
846
847
848                        AEV_TRACE("coeff_abs_level_remaining", coeff_abs_level_remaining, ps_cabac->u4_range);
849                        base_lvl += coeff_abs_level_remaining;
850
851                    }
852
853                    /* update the rice param based on coeff level */
854                    if((base_lvl > (3 << rice_param)) && (rice_param < 4))
855                    {
856                        rice_param++;
857                    }
858
859                    /* Compute absolute level */
860                    level = base_lvl;
861
862                    /* Update level with the sign */
863                    if((u4_coeff_sign_map >> 31) & 1)
864                        level = -level;
865
866                    u4_coeff_sign_map <<= 1;
867                    /* Update sign in case sign is hidden */
868                    if(sign_data_hiding_flag && sign_hidden)
869                    {
870                        sum_abs_level += base_lvl;
871
872                        if(n == first_sig_scan_pos && ((sum_abs_level % 2) == 1))
873                            level = -level;
874                    }
875
876                    /* Store the resulting level in non-zero level array */
877                    ps_tu_sblk_coeff_data->ai2_level[num_sig_coeff++] = level;
878                    //AEV_TRACE("level", level, 0);
879                }
880                u4_sig_coeff_map_shift <<= 1;
881                n--;
882                /* If there are zero coeffs, then shift by as many zero coeffs and decrement n */
883                clz = CLZ(u4_sig_coeff_map_shift);
884                u4_sig_coeff_map_shift <<= clz;
885                n -= clz;
886
887
888            }while(u4_sig_coeff_map_shift);
889        }
890
891        /* Increment the pv_tu_sblk_coeff_data */
892        {
893            UWORD8 *pu1_buf = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data;
894            pu1_buf += sizeof(tu_sblk_coeff_data_t) - SUBBLK_COEFF_CNT * sizeof(WORD16);
895            pu1_buf += num_coeff * sizeof(WORD16);
896            ps_codec->s_parse.pv_tu_coeff_data = pu1_buf;
897
898        }
899
900    }
901    /* Set number of coded sub blocks in the current TU */
902    *pi1_num_coded_subblks = num_subblks;
903
904    return ret;
905}
906