xref: /aosp_15_r20/external/libhevc/decoder/ihevcd_parse_slice.c (revision c83a76b084498d55f252f48b2e3786804cdf24b7)
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_slice.c
22  *
23  * @brief
24  *  Contains functions for parsing slice data
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 #include "ithread.h"
50 
51 #include "ihevc_defs.h"
52 #include "ihevc_debug.h"
53 #include "ihevc_structs.h"
54 #include "ihevc_macros.h"
55 #include "ihevc_mem_fns.h"
56 #include "ihevc_platform_macros.h"
57 
58 #include "ihevc_common_tables.h"
59 #include "ihevc_error.h"
60 #include "ihevc_cabac_tables.h"
61 
62 #include "ihevcd_trace.h"
63 #include "ihevcd_defs.h"
64 #include "ihevcd_function_selector.h"
65 #include "ihevcd_structs.h"
66 #include "ihevcd_error.h"
67 #include "ihevcd_nal.h"
68 #include "ihevcd_bitstream.h"
69 #include "ihevcd_utils.h"
70 #include "ihevcd_parse_slice.h"
71 #include "ihevcd_parse_residual.h"
72 #include "ihevcd_cabac.h"
73 #include "ihevcd_job_queue.h"
74 #include "ihevcd_intra_pred_mode_prediction.h"
75 #include "ihevcd_common_tables.h"
76 #include "ihevcd_process_slice.h"
77 #include "ihevcd_debug.h"
78 #include "ihevcd_get_mv.h"
79 #include "ihevcd_boundary_strength.h"
80 #include "ihevcd_ilf_padding.h"
81 #include "ihevcd_statistics.h"
82 /* Bit stream offset threshold */
83 #define BITSTRM_OFF_THRS 8
84 
85 #define MIN_CU_QP_DELTA_ABS(x) (-26 + ((x) * 6) / 2)
86 #define MAX_CU_QP_DELTA_ABS(x) (25 + ((x) * 6) / 2)
87 
88 /**
89  * Table used to decode part_mode if AMP is enabled and current CU is not min CU
90  */
91 const UWORD8 gau1_part_mode_amp[] = { PART_nLx2N, PART_nRx2N, PART_Nx2N, 0xFF, PART_2NxnU, PART_2NxnD, PART_2NxN, 0xFF };
92 
93 const UWORD32 gau4_ct_depth_mask[] = { 0x0, 0x55555555, 0xAAAAAAAA, 0xFFFFFFFF };
94 
95 
96 
97 /**
98  *******************************************************************************
99  *
100  * @brief
101  *  Parses Transform tree syntax
102  *
103  * @par Description:
104  *  Parses Transform tree syntax as per Section:7.3.9.8
105  *
106  * @param[in] ps_codec
107  *  Pointer to codec context
108  *
109  * @returns  Status
110  *
111  * @remarks
112  *
113  *
114  *******************************************************************************
115  */
116 
ihevcd_parse_transform_tree(codec_t * ps_codec,WORD32 x0,WORD32 y0,WORD32 cu_x_base,WORD32 cu_y_base,WORD32 log2_trafo_size,WORD32 trafo_depth,WORD32 blk_idx,WORD32 intra_pred_mode)117 WORD32 ihevcd_parse_transform_tree(codec_t *ps_codec,
118                                    WORD32 x0, WORD32 y0,
119                                    WORD32 cu_x_base, WORD32 cu_y_base,
120                                    WORD32 log2_trafo_size,
121                                    WORD32 trafo_depth,
122                                    WORD32 blk_idx,
123                                    WORD32 intra_pred_mode)
124 {
125     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
126     sps_t *ps_sps;
127     pps_t *ps_pps;
128     WORD32 value;
129     WORD32 x1, y1;
130     WORD32 max_trafo_depth;
131 
132     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
133     WORD32 intra_split_flag;
134     WORD32 split_transform_flag;
135     WORD32 ctxt_idx;
136     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
137 
138     max_trafo_depth = ps_codec->s_parse.s_cu.i4_max_trafo_depth;
139     ps_sps = ps_codec->s_parse.ps_sps;
140     ps_pps = ps_codec->s_parse.ps_pps;
141     intra_split_flag = ps_codec->s_parse.s_cu.i4_intra_split_flag;
142 
143     {
144         split_transform_flag = 0;
145         if((log2_trafo_size <= ps_sps->i1_log2_max_transform_block_size) &&
146                         (log2_trafo_size > ps_sps->i1_log2_min_transform_block_size) &&
147                         (trafo_depth < max_trafo_depth) &&
148                         !(intra_split_flag && (trafo_depth == 0)))
149         {
150             /* encode the split transform flag, context derived as per Table9-37 */
151             ctxt_idx = IHEVC_CAB_SPLIT_TFM + (5 - log2_trafo_size);
152 
153             TRACE_CABAC_CTXT("split_transform_flag", ps_cabac->u4_range, ctxt_idx);
154             split_transform_flag = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
155             AEV_TRACE("split_transform_flag", split_transform_flag,
156                       ps_cabac->u4_range);
157 
158         }
159         else
160         {
161             WORD32 inter_split_flag = 0;
162 
163             if((0 == ps_sps->i1_max_transform_hierarchy_depth_inter) &&
164                             (PRED_MODE_INTER == ps_codec->s_parse.s_cu.i4_pred_mode) &&
165                             (PART_2Nx2N != ps_codec->s_parse.s_cu.i4_part_mode) &&
166                             (0 == trafo_depth))
167             {
168                 inter_split_flag = 1;
169             }
170 
171             if((log2_trafo_size > ps_sps->i1_log2_max_transform_block_size) ||
172                             ((1 == intra_split_flag) && (0 == trafo_depth)) ||
173                             (1 == inter_split_flag))
174             {
175                 split_transform_flag = 1;
176             }
177         }
178 
179         if(0 == trafo_depth)
180         {
181             ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth] = 0;
182             ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth] = 0;
183         }
184         else
185         {
186             ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth] = ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth - 1];
187             ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth] = ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth - 1];
188         }
189         if(trafo_depth == 0 || log2_trafo_size > 2)
190         {
191             ctxt_idx = IHEVC_CAB_CBCR_IDX + trafo_depth;
192             /* CBF for Cb/Cr is sent only if the parent CBF for Cb/Cr is non-zero */
193             if((trafo_depth == 0) || ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth - 1])
194             {
195                 TRACE_CABAC_CTXT("cbf_cb", ps_cabac->u4_range, ctxt_idx);
196                 value = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
197                 AEV_TRACE("cbf_cb", value, ps_cabac->u4_range);
198                 ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth] = value;
199             }
200 
201             if((trafo_depth == 0) || ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth - 1])
202             {
203                 TRACE_CABAC_CTXT("cbf_cr", ps_cabac->u4_range, ctxt_idx);
204                 value = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
205                 AEV_TRACE("cbf_cr", value, ps_cabac->u4_range);
206                 ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth] = value;
207             }
208         }
209         if(split_transform_flag)
210         {
211             WORD32 intra_pred_mode_tmp;
212             x1 = x0 + ((1 << log2_trafo_size) >> 1);
213             y1 = y0 + ((1 << log2_trafo_size) >> 1);
214 
215             /* For transform depth of zero, intra pred mode as decoded at CU */
216             /* level is sent to the transform tree nodes */
217             /* When depth is non-zero intra pred mode of parent node is sent */
218             /* This takes care of passing correct mode to all the child nodes */
219             intra_pred_mode_tmp = trafo_depth ? intra_pred_mode : ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[0];
220             ret = ihevcd_parse_transform_tree(ps_codec, x0, y0, x0, y0, log2_trafo_size - 1, trafo_depth + 1, 0, intra_pred_mode_tmp);
221             RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
222 
223             intra_pred_mode_tmp = trafo_depth ? intra_pred_mode : ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[1];
224             ret = ihevcd_parse_transform_tree(ps_codec, x1, y0, x0, y0, log2_trafo_size - 1, trafo_depth + 1, 1, intra_pred_mode_tmp);
225             RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
226 
227             intra_pred_mode_tmp = trafo_depth ? intra_pred_mode : ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[2];
228             ret = ihevcd_parse_transform_tree(ps_codec, x0, y1, x0, y0, log2_trafo_size - 1, trafo_depth + 1, 2, intra_pred_mode_tmp);
229             RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
230 
231             intra_pred_mode_tmp = trafo_depth ? intra_pred_mode : ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[3];
232             ret = ihevcd_parse_transform_tree(ps_codec, x1, y1, x0, y0, log2_trafo_size - 1, trafo_depth + 1, 3, intra_pred_mode_tmp);
233             RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
234 
235         }
236         else
237         {
238             WORD32 ctb_x_base;
239             WORD32 ctb_y_base;
240             WORD32 cu_qp_delta_abs;
241 
242 
243 
244             tu_t *ps_tu = ps_codec->s_parse.ps_tu;
245             cu_qp_delta_abs = 0;
246             ctb_x_base = ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size;
247             ctb_y_base = ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size;
248 
249             if((ps_codec->s_parse.s_cu.i4_pred_mode == PRED_MODE_INTRA) ||
250                             (trafo_depth != 0) ||
251                             (ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth]) ||
252                             (ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth]))
253             {
254                 ctxt_idx = IHEVC_CAB_CBF_LUMA_IDX;
255                 ctxt_idx += (trafo_depth == 0) ? 1 : 0;
256 
257                 TRACE_CABAC_CTXT("cbf_luma", ps_cabac->u4_range, ctxt_idx);
258                 value = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
259                 AEV_TRACE("cbf_luma", value, ps_cabac->u4_range);
260 
261                 ps_codec->s_parse.s_cu.i1_cbf_luma = value;
262             }
263             else
264             {
265                 ps_codec->s_parse.s_cu.i1_cbf_luma = 1;
266             }
267 
268             /* Initialize ps_tu to default values */
269             /* If required change this to WORD32 packed write */
270             ps_tu->b1_cb_cbf = 0;
271             ps_tu->b1_cr_cbf = 0;
272             ps_tu->b1_y_cbf = 0;
273             ps_tu->b4_pos_x = ((x0 - ctb_x_base) >> 2);
274             ps_tu->b4_pos_y = ((y0 - ctb_y_base) >> 2);
275             ps_tu->b1_transquant_bypass = ps_codec->s_parse.s_cu.i4_cu_transquant_bypass;
276             ps_tu->b3_size = (log2_trafo_size - 2);
277             ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
278 
279             ps_tu->b6_luma_intra_mode = intra_pred_mode;
280             ps_tu->b3_chroma_intra_mode_idx = ps_codec->s_parse.s_cu.i4_intra_chroma_pred_mode_idx;
281 
282             /* Section:7.3.12  Transform unit syntax inlined here */
283             if(ps_codec->s_parse.s_cu.i1_cbf_luma ||
284                             ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth] ||
285                             ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth])
286             {
287                 WORD32 intra_pred_mode_chroma;
288                 if(ps_pps->i1_cu_qp_delta_enabled_flag && !ps_codec->s_parse.i4_is_cu_qp_delta_coded)
289                 {
290 
291 
292                     WORD32 c_max        = TU_MAX_QP_DELTA_ABS;
293                     WORD32 ctxt_inc     = IHEVC_CAB_QP_DELTA_ABS;
294                     WORD32 ctxt_inc_max = CTXT_MAX_QP_DELTA_ABS;
295 
296                     TRACE_CABAC_CTXT("cu_qp_delta_abs", ps_cabac->u4_range, ctxt_inc);
297                     /* qp_delta_abs is coded as combination of tunary and eg0 code  */
298                     /* See Table 9-32 and Table 9-37 for details on cu_qp_delta_abs */
299                     cu_qp_delta_abs = ihevcd_cabac_decode_bins_tunary(ps_cabac,
300                                                                       ps_bitstrm,
301                                                                       c_max,
302                                                                       ctxt_inc,
303                                                                       0,
304                                                                       ctxt_inc_max);
305                     if(cu_qp_delta_abs >= c_max)
306                     {
307                         value = ihevcd_cabac_decode_bypass_bins_egk(ps_cabac, ps_bitstrm, 0);
308                         cu_qp_delta_abs += value;
309                     }
310                     AEV_TRACE("cu_qp_delta_abs", cu_qp_delta_abs, ps_cabac->u4_range);
311 
312                     ps_codec->s_parse.i4_is_cu_qp_delta_coded = 1;
313 
314 
315                     if(cu_qp_delta_abs)
316                     {
317                         value = ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
318                         AEV_TRACE("cu_qp_delta_sign", value, ps_cabac->u4_range);
319 
320                         if(value)
321                             cu_qp_delta_abs = -cu_qp_delta_abs;
322 
323                     }
324 
325                     if (cu_qp_delta_abs < MIN_CU_QP_DELTA_ABS(ps_sps->i1_bit_depth_luma_minus8)
326                                     || cu_qp_delta_abs > MAX_CU_QP_DELTA_ABS(ps_sps->i1_bit_depth_luma_minus8))
327                     {
328                         return IHEVCD_INVALID_PARAMETER;
329                     }
330 
331                     ps_codec->s_parse.s_cu.i4_cu_qp_delta = cu_qp_delta_abs;
332 
333                 }
334 
335                 if(ps_codec->s_parse.s_cu.i1_cbf_luma)
336                 {
337                     ps_tu->b1_y_cbf = 1;
338                     ihevcd_parse_residual_coding(ps_codec, x0, y0, log2_trafo_size, 0, intra_pred_mode);
339                 }
340 
341                 if(4 == ps_codec->s_parse.s_cu.i4_intra_chroma_pred_mode_idx)
342                     intra_pred_mode_chroma = ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[0];
343                 else
344                 {
345                     intra_pred_mode_chroma = gau1_intra_pred_chroma_modes[ps_codec->s_parse.s_cu.i4_intra_chroma_pred_mode_idx];
346 
347                     if(intra_pred_mode_chroma ==
348                                     ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[0])
349                     {
350                         intra_pred_mode_chroma = INTRA_ANGULAR(34);
351                     }
352 
353                 }
354                 if(log2_trafo_size > 2)
355                 {
356                     if(ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth])
357                     {
358                         ps_tu->b1_cb_cbf = 1;
359                         ihevcd_parse_residual_coding(ps_codec, x0, y0, log2_trafo_size - 1, 1, intra_pred_mode_chroma);
360                     }
361 
362                     if(ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth])
363                     {
364                         ps_tu->b1_cr_cbf = 1;
365                         ihevcd_parse_residual_coding(ps_codec, x0, y0, log2_trafo_size - 1, 2, intra_pred_mode_chroma);
366                     }
367                 }
368                 else if(blk_idx == 3)
369                 {
370                     if(ps_codec->s_parse.s_cu.ai1_cbf_cb[trafo_depth])
371                     {
372                         ps_tu->b1_cb_cbf = 1;
373                         ihevcd_parse_residual_coding(ps_codec, cu_x_base, cu_y_base, log2_trafo_size, 1, intra_pred_mode_chroma);
374                     }
375 
376                     if(ps_codec->s_parse.s_cu.ai1_cbf_cr[trafo_depth])
377                     {
378                         ps_tu->b1_cr_cbf = 1;
379                         ihevcd_parse_residual_coding(ps_codec, cu_x_base, cu_y_base, log2_trafo_size, 2, intra_pred_mode_chroma);
380                     }
381                 }
382                 else
383                 {
384                     //ps_tu->b1_chroma_present = 0;
385                     ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
386                 }
387             }
388             else
389             {
390                 if((3 != blk_idx) && (2 == log2_trafo_size))
391                 {
392                     ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
393                 }
394             }
395 
396             /* Set the first TU in CU flag */
397             {
398                 if((ps_codec->s_parse.s_cu.i4_pos_x << 3) == (ps_tu->b4_pos_x << 2) &&
399                                 (ps_codec->s_parse.s_cu.i4_pos_y << 3) == (ps_tu->b4_pos_y << 2))
400                 {
401                     ps_tu->b1_first_tu_in_cu = 1;
402                 }
403                 else
404                 {
405                     ps_tu->b1_first_tu_in_cu = 0;
406                 }
407             }
408             ps_codec->s_parse.ps_tu++;
409             ps_codec->s_parse.s_cu.i4_tu_cnt++;
410             ps_codec->s_parse.i4_pic_tu_idx++;
411         }
412     }
413     return ret;
414 }
415 /**
416  *******************************************************************************
417  *
418  * @brief
419  *  Parses Motion vector difference
420  *
421  * @par Description:
422  *  Parses Motion vector difference as per Section:7.3.9.9
423  *
424  * @param[in] ps_codec
425  *  Pointer to codec context
426  *
427  * @returns  Error from IHEVCD_ERROR_T
428  *
429  * @remarks
430  *
431  *
432  *******************************************************************************
433  */
ihevcd_parse_mvd(codec_t * ps_codec,mv_t * ps_mv)434 IHEVCD_ERROR_T ihevcd_parse_mvd(codec_t *ps_codec, mv_t *ps_mv)
435 {
436     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
437     WORD32 value;
438     WORD32 abs_mvd;
439     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
440     WORD32 abs_mvd_greater0_flag[2];
441     WORD32 abs_mvd_greater1_flag[2];
442     WORD32 ctxt_idx;
443     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
444 
445 
446     ctxt_idx  = IHEVC_CAB_MVD_GRT0;
447     /* encode absmvd_x > 0 */
448     TRACE_CABAC_CTXT("abs_mvd_greater0_flag[0]", ps_cabac->u4_range, ctxt_idx);
449     abs_mvd_greater0_flag[0] = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
450     AEV_TRACE("abs_mvd_greater0_flag[0]", abs_mvd_greater0_flag[0], ps_cabac->u4_range);
451 
452     /* encode absmvd_y > 0 */
453     TRACE_CABAC_CTXT("abs_mvd_greater0_flag[1]", ps_cabac->u4_range, ctxt_idx);
454     abs_mvd_greater0_flag[1] = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
455     AEV_TRACE("abs_mvd_greater0_flag[1]", abs_mvd_greater0_flag[1], ps_cabac->u4_range);
456 
457     ctxt_idx  = IHEVC_CAB_MVD_GRT1;
458     abs_mvd_greater1_flag[0] = 0;
459     abs_mvd_greater1_flag[1] = 0;
460 
461     if(abs_mvd_greater0_flag[0])
462     {
463         TRACE_CABAC_CTXT("abs_mvd_greater1_flag[0]", ps_cabac->u4_range, ctxt_idx);
464         abs_mvd_greater1_flag[0] = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
465         AEV_TRACE("abs_mvd_greater1_flag[0]", abs_mvd_greater1_flag[0], ps_cabac->u4_range);
466     }
467     if(abs_mvd_greater0_flag[1])
468     {
469         TRACE_CABAC_CTXT("abs_mvd_greater1_flag[1]", ps_cabac->u4_range, ctxt_idx);
470         abs_mvd_greater1_flag[1] = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
471         AEV_TRACE("abs_mvd_greater1_flag[1]", abs_mvd_greater1_flag[1], ps_cabac->u4_range);
472     }
473     abs_mvd = 0;
474     if(abs_mvd_greater0_flag[0])
475     {
476         abs_mvd = 1;
477         if(abs_mvd_greater1_flag[0])
478         {
479             value = ihevcd_cabac_decode_bypass_bins_egk(ps_cabac, ps_bitstrm, 1);
480             AEV_TRACE("abs_mvd_minus2[0]", value, ps_cabac->u4_range);
481             abs_mvd = value + 2;
482         }
483         value = ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
484         AEV_TRACE("mvd_sign_flag[0]", value, ps_cabac->u4_range);
485         if(value)
486         {
487             abs_mvd = -abs_mvd;
488         }
489 
490     }
491     ps_mv->i2_mvx = abs_mvd;
492     abs_mvd = 0;
493     if(abs_mvd_greater0_flag[1])
494     {
495         abs_mvd = 1;
496         if(abs_mvd_greater1_flag[1])
497         {
498             value = ihevcd_cabac_decode_bypass_bins_egk(ps_cabac, ps_bitstrm, 1);
499             AEV_TRACE("abs_mvd_minus2[1]", value, ps_cabac->u4_range);
500             abs_mvd = value + 2;
501 
502         }
503         value = ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
504         AEV_TRACE("mvd_sign_flag[1]", value, ps_cabac->u4_range);
505 
506         if(value)
507         {
508             abs_mvd = -abs_mvd;
509         }
510     }
511     ps_mv->i2_mvy = abs_mvd;
512 
513     return ret;
514 }
515 
516 /**
517  *******************************************************************************
518  *
519  * @brief
520  *  Parses PCM sample
521  *
522  *
523  * @par Description:
524  *  Parses PCM sample as per Section:7.3.9.7 Pcm sample syntax
525  *
526  * @param[in] ps_codec
527  *  Pointer to codec context
528  *
529  * @returns  Error from IHEVCD_ERROR_T
530  *
531  * @remarks
532  *
533  *
534  *******************************************************************************
535  */
536 
ihevcd_parse_pcm_sample(codec_t * ps_codec,WORD32 x0,WORD32 y0,WORD32 log2_cb_size)537 IHEVCD_ERROR_T  ihevcd_parse_pcm_sample(codec_t *ps_codec,
538                                         WORD32 x0,
539                                         WORD32 y0,
540                                         WORD32 log2_cb_size)
541 {
542     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
543     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
544     sps_t *ps_sps;
545 
546     WORD32 value;
547     WORD32 i;
548 
549     WORD32 num_bits;
550     UWORD32 u4_sig_coeff_map;
551     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
552     tu_t *ps_tu = ps_codec->s_parse.ps_tu;
553     tu_sblk_coeff_data_t *ps_tu_sblk_coeff_data;
554     UWORD8 *pu1_coeff_data;
555     ps_sps = ps_codec->s_parse.ps_sps;
556 
557     UNUSED(value);
558     UNUSED(ps_tu);
559     UNUSED(ps_cabac);
560     UNUSED(x0);
561     UNUSED(y0);
562 
563     {
564         WORD8 *pi1_scan_idx;
565         WORD8 *pi1_buf = (WORD8 *)ps_codec->s_parse.pv_tu_coeff_data;
566         WORD8 *pi1_num_coded_subblks;
567 
568         /* First WORD8 gives number of coded subblocks */
569         pi1_num_coded_subblks = pi1_buf++;
570 
571         /* Set number of coded subblocks in the current TU to zero */
572         /* For PCM there will be only one subblock which is the same size as CU */
573         *pi1_num_coded_subblks = 1;
574 
575         /* Second WORD8 gives (scan idx << 1) | trans_skip */
576         pi1_scan_idx = pi1_buf++;
577         *pi1_scan_idx = (0 << 1) | 1;
578 
579         /* Store the incremented pointer in pv_tu_coeff_data */
580         ps_codec->s_parse.pv_tu_coeff_data = pi1_buf;
581 
582     }
583 
584     u4_sig_coeff_map = 0xFFFFFFFF;
585     ps_tu_sblk_coeff_data = (tu_sblk_coeff_data_t *)ps_codec->s_parse.pv_tu_coeff_data;
586     ps_tu_sblk_coeff_data->u2_sig_coeff_map = u4_sig_coeff_map;
587     ps_tu_sblk_coeff_data->u2_subblk_pos = 0;
588 
589     pu1_coeff_data = (UWORD8 *)&ps_tu_sblk_coeff_data->ai2_level[0];
590 
591     num_bits = ps_sps->i1_pcm_sample_bit_depth_luma;
592 
593     for(i = 0; i < 1 << (log2_cb_size << 1); i++)
594     {
595         TRACE_CABAC_CTXT("pcm_sample_luma", ps_cabac->u4_range, 0);
596         BITS_PARSE("pcm_sample_luma", value, ps_bitstrm, num_bits);
597 
598         //ps_pcmsample_t->i1_pcm_sample_luma[i] = value;
599         *pu1_coeff_data++ = value << (BIT_DEPTH_LUMA - num_bits);
600     }
601 
602     num_bits = ps_sps->i1_pcm_sample_bit_depth_chroma;
603 
604     for(i = 0; i < (1 << (log2_cb_size << 1)) >> 1; i++)
605     {
606         TRACE_CABAC_CTXT("pcm_sample_chroma", ps_cabac->u4_range, 0);
607         BITS_PARSE("pcm_sample_chroma", value, ps_bitstrm, num_bits);
608 
609         // ps_pcmsample_t->i1_pcm_sample_chroma[i] = value;
610         *pu1_coeff_data++ = value << (BIT_DEPTH_CHROMA - num_bits);
611     }
612 
613     ps_codec->s_parse.pv_tu_coeff_data = pu1_coeff_data;
614 
615     return ret;
616 }
617 /**
618  *******************************************************************************
619  *
620  * @brief
621  *  Parses Prediction unit
622  *
623  * @par Description:
624  *  Parses Prediction unit as per Section:7.3.9.6
625  *
626  * @param[in] ps_codec
627  *  Pointer to codec context
628  *
629  * @returns  Error from IHEVCD_ERROR_T
630  *
631  * @remarks
632  *
633  *
634  *******************************************************************************
635  */
636 
ihevcd_parse_pu_mvp(codec_t * ps_codec,pu_t * ps_pu)637 IHEVCD_ERROR_T  ihevcd_parse_pu_mvp(codec_t *ps_codec, pu_t *ps_pu)
638 {
639     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
640     WORD32 value;
641     slice_header_t *ps_slice_hdr;
642     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
643     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
644     WORD32 inter_pred_idc;
645 
646     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
647 
648     if(ps_slice_hdr->i1_slice_type == BSLICE)
649     {
650         WORD32 pu_w_plus_pu_h;
651         WORD32 ctxt_idx;
652         /* required to check if w+h==12 case */
653         pu_w_plus_pu_h = ((ps_pu->b4_wd + 1) << 2) + ((ps_pu->b4_ht + 1) << 2);
654         if(12 == pu_w_plus_pu_h)
655         {
656             ctxt_idx = IHEVC_CAB_INTER_PRED_IDC + 4;
657             TRACE_CABAC_CTXT("inter_pred_idc", ps_cabac->u4_range, ctxt_idx);
658             inter_pred_idc = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm,
659                                                      ctxt_idx);
660         }
661         else
662         {
663             /* larger PUs can be encoded as bi_pred/l0/l1 inter_pred_idc */
664             WORD32 is_bipred;
665 
666             ctxt_idx = IHEVC_CAB_INTER_PRED_IDC + ps_codec->s_parse.i4_ct_depth;
667             TRACE_CABAC_CTXT("inter_pred_idc", ps_cabac->u4_range, ctxt_idx);
668             is_bipred = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
669             inter_pred_idc = PRED_BI;
670             if(!is_bipred)
671             {
672                 ctxt_idx = IHEVC_CAB_INTER_PRED_IDC + 4;
673                 inter_pred_idc = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm,
674                                                          ctxt_idx);
675             }
676         }
677 
678         AEV_TRACE("inter_pred_idc", inter_pred_idc, ps_cabac->u4_range);
679     }
680     else
681         inter_pred_idc = PRED_L0;
682     ps_pu->mv.i1_l0_ref_idx = 0;
683     ps_pu->mv.i1_l1_ref_idx = 0;
684     /* Decode MVD for L0 for PRED_L0 or PRED_BI */
685     if(inter_pred_idc != PRED_L1)
686     {
687         WORD32 active_refs = ps_slice_hdr->i1_num_ref_idx_l0_active;
688         WORD32 ref_idx = 0;
689         WORD32 ctxt_idx;
690 
691         if(active_refs > 1)
692         {
693             ctxt_idx = IHEVC_CAB_INTER_REF_IDX;
694             /* encode the context modelled first bin */
695             TRACE_CABAC_CTXT("ref_idx", ps_cabac->u4_range, ctxt_idx);
696             ref_idx = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
697 
698             if((active_refs > 2) && ref_idx)
699             {
700                 WORD32 value;
701                 /* encode the context modelled second bin */
702                 ctxt_idx++;
703                 value = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
704                 ref_idx += value;
705                 if((active_refs > 3) && value)
706                 {
707                     /* encode remaining bypass bins */
708                     ref_idx = ihevcd_cabac_decode_bypass_bins_tunary(ps_cabac,
709                                                                      ps_bitstrm,
710                                                                      (active_refs - 3)
711                     );
712                     ref_idx += 2;
713                 }
714             }
715             AEV_TRACE("ref_idx", ref_idx, ps_cabac->u4_range);
716         }
717 
718         ref_idx = CLIP3(ref_idx, 0, MAX_DPB_SIZE - 1);
719         ps_pu->mv.i1_l0_ref_idx = ref_idx;
720 
721         ihevcd_parse_mvd(ps_codec, &ps_pu->mv.s_l0_mv);
722 
723         ctxt_idx = IHEVC_CAB_MVP_L0L1;
724         value = ihevcd_cabac_decode_bin(ps_cabac,
725                                         ps_bitstrm,
726                                         ctxt_idx);
727 
728         AEV_TRACE("mvp_l0/l1_flag", value, ps_cabac->u4_range);
729 
730         ps_pu->b1_l0_mvp_idx = value;
731 
732     }
733     /* Decode MVD for L1 for PRED_L1 or PRED_BI */
734     if(inter_pred_idc != PRED_L0)
735     {
736         WORD32 active_refs = ps_slice_hdr->i1_num_ref_idx_l1_active;
737         WORD32 ref_idx = 0;
738         WORD32 ctxt_idx;
739 
740         if(active_refs > 1)
741         {
742 
743             ctxt_idx = IHEVC_CAB_INTER_REF_IDX;
744             TRACE_CABAC_CTXT("ref_idx", ps_cabac->u4_range, ctxt_idx);
745             /* encode the context modelled first bin */
746             ref_idx = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
747 
748             if((active_refs > 2) && ref_idx)
749             {
750                 WORD32 value;
751                 /* encode the context modelled second bin */
752                 ctxt_idx++;
753                 value = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
754                 ref_idx += value;
755                 if((active_refs > 3) && value)
756                 {
757                     /* encode remaining bypass bins */
758                     ref_idx = ihevcd_cabac_decode_bypass_bins_tunary(ps_cabac,
759                                                                      ps_bitstrm,
760                                                                      (active_refs - 3)
761                     );
762                     ref_idx += 2;
763                 }
764             }
765 
766             AEV_TRACE("ref_idx", ref_idx, ps_cabac->u4_range);
767         }
768 
769         ref_idx = CLIP3(ref_idx, 0, MAX_DPB_SIZE - 1);
770         ps_pu->mv.i1_l1_ref_idx = ref_idx;
771 
772         if(ps_slice_hdr->i1_mvd_l1_zero_flag && inter_pred_idc == PRED_BI)
773         {
774             ps_pu->mv.s_l1_mv.i2_mvx = 0;
775             ps_pu->mv.s_l1_mv.i2_mvy = 0;
776         }
777         else
778         {
779             ihevcd_parse_mvd(ps_codec, &ps_pu->mv.s_l1_mv);
780         }
781 
782         ctxt_idx = IHEVC_CAB_MVP_L0L1;
783         value = ihevcd_cabac_decode_bin(ps_cabac,
784                                         ps_bitstrm,
785                                         ctxt_idx);
786 
787         AEV_TRACE("mvp_l0/l1_flag", value, ps_cabac->u4_range);
788         ps_pu->b1_l1_mvp_idx = value;
789 
790     }
791 
792     ps_pu->b2_pred_mode = inter_pred_idc;
793     return ret;
794 }
795 /**
796  *******************************************************************************
797  *
798  * @brief
799  *  Parses Prediction unit
800  *
801  * @par Description:
802  *  Parses Prediction unit as per Section:7.3.9.6
803  *
804  * @param[in] ps_codec
805  *  Pointer to codec context
806  *
807  * @returns  Error from IHEVCD_ERROR_T
808  *
809  * @remarks
810  *
811  *
812  *******************************************************************************
813  */
814 
ihevcd_parse_prediction_unit(codec_t * ps_codec,WORD32 x0,WORD32 y0,WORD32 wd,WORD32 ht)815 IHEVCD_ERROR_T  ihevcd_parse_prediction_unit(codec_t *ps_codec,
816                                              WORD32 x0,
817                                              WORD32 y0,
818                                              WORD32 wd,
819                                              WORD32 ht)
820 {
821     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
822     slice_header_t *ps_slice_hdr;
823     sps_t *ps_sps;
824     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
825     WORD32 ctb_x_base;
826     WORD32 ctb_y_base;
827 
828     pu_t *ps_pu = ps_codec->s_parse.ps_pu;
829     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
830 
831     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
832 
833     /* Set PU structure to default values */
834     memset(ps_pu, 0, sizeof(pu_t));
835 
836     ps_sps = ps_codec->s_parse.ps_sps;
837     ctb_x_base = ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size;
838     ctb_y_base = ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size;
839 
840     ps_pu->b4_pos_x = (x0 - ctb_x_base) >> 2;
841     ps_pu->b4_pos_y = (y0 - ctb_y_base) >> 2;
842     ps_pu->b4_wd = (wd >> 2) - 1;
843     ps_pu->b4_ht = (ht >> 2) - 1;
844 
845     ps_pu->b1_intra_flag = 0;
846     ps_pu->b3_part_mode = ps_codec->s_parse.s_cu.i4_part_mode;
847 
848     if(PRED_MODE_SKIP == ps_codec->s_parse.s_cu.i4_pred_mode)
849     {
850         WORD32 merge_idx = 0;
851         if(ps_slice_hdr->i1_max_num_merge_cand > 1)
852         {
853             WORD32 ctxt_idx = IHEVC_CAB_MERGE_IDX_EXT;
854             WORD32 bin;
855 
856             TRACE_CABAC_CTXT("merge_idx", ps_cabac->u4_range, ctxt_idx);
857             bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
858             if(bin)
859             {
860                 if(ps_slice_hdr->i1_max_num_merge_cand > 2)
861                 {
862                     merge_idx = ihevcd_cabac_decode_bypass_bins_tunary(
863                                     ps_cabac, ps_bitstrm,
864                                     (ps_slice_hdr->i1_max_num_merge_cand - 2));
865                 }
866                 merge_idx++;
867             }
868             AEV_TRACE("merge_idx", merge_idx, ps_cabac->u4_range);
869         }
870         ps_pu->b1_merge_flag = 1;
871         ps_pu->b3_merge_idx = merge_idx;
872 
873     }
874     else
875     {
876         /* MODE_INTER */
877         WORD32 merge_flag;
878         WORD32 ctxt_idx = IHEVC_CAB_MERGE_FLAG_EXT;
879         TRACE_CABAC_CTXT("merge_flag", ps_cabac->u4_range, ctxt_idx);
880         merge_flag = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
881         AEV_TRACE("merge_flag", merge_flag, ps_cabac->u4_range);
882 
883         ps_pu->b1_merge_flag = merge_flag;
884 
885         if(merge_flag)
886         {
887             WORD32 merge_idx = 0;
888             if(ps_slice_hdr->i1_max_num_merge_cand > 1)
889             {
890                 WORD32 ctxt_idx = IHEVC_CAB_MERGE_IDX_EXT;
891                 WORD32 bin;
892                 TRACE_CABAC_CTXT("merge_idx", ps_cabac->u4_range, ctxt_idx);
893                 bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
894                 if(bin)
895                 {
896                     if(ps_slice_hdr->i1_max_num_merge_cand > 2)
897                     {
898                         merge_idx = ihevcd_cabac_decode_bypass_bins_tunary(
899                                         ps_cabac, ps_bitstrm,
900                                         (ps_slice_hdr->i1_max_num_merge_cand - 2));
901                     }
902                     merge_idx++;
903                 }
904                 AEV_TRACE("merge_idx", merge_idx, ps_cabac->u4_range);
905             }
906 
907             ps_pu->b3_merge_idx = merge_idx;
908         }
909         else
910         {
911             ihevcd_parse_pu_mvp(ps_codec, ps_pu);
912         }
913 
914     }
915     STATS_UPDATE_PU_SIZE(ps_pu);
916     /* Increment PU pointer */
917     ps_codec->s_parse.ps_pu++;
918     ps_codec->s_parse.i4_pic_pu_idx++;
919     return ret;
920 }
921 
922 
ihevcd_parse_part_mode_amp(cab_ctxt_t * ps_cabac,bitstrm_t * ps_bitstrm)923 WORD32 ihevcd_parse_part_mode_amp(cab_ctxt_t *ps_cabac, bitstrm_t *ps_bitstrm)
924 {
925     WORD32 ctxt_idx = IHEVC_CAB_PART_MODE;
926     WORD32 part_mode_idx;
927     WORD32 part_mode;
928     WORD32 bin;
929 
930     part_mode = 0;
931     TRACE_CABAC_CTXT("part_mode", ps_cabac->u4_range, ctxt_idx);
932     bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx++);
933 
934     if(!bin)
935     {
936         bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx++);
937         part_mode_idx = bin;
938         part_mode_idx <<= 1;
939 
940         /* Following takes of handling context increment for 3rd bin in part_mode */
941         /* When AMP is enabled and the current is not min CB */
942         /* Context for 3rd bin is 3 and not 2 */
943         ctxt_idx += 1;
944 
945         bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
946         part_mode_idx |= bin;
947 
948         part_mode_idx <<= 1;
949         if(!bin)
950         {
951 
952             bin = ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
953             part_mode_idx |= bin;
954         }
955         part_mode = gau1_part_mode_amp[part_mode_idx];
956     }
957     return part_mode;
958 }
ihevcd_parse_coding_unit_intra(codec_t * ps_codec,WORD32 x0,WORD32 y0,WORD32 log2_cb_size)959 IHEVCD_ERROR_T ihevcd_parse_coding_unit_intra(codec_t *ps_codec,
960                                               WORD32 x0,
961                                               WORD32 y0,
962                                               WORD32 log2_cb_size)
963 {
964     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
965     sps_t *ps_sps;
966     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
967     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
968     WORD32 pcm_flag;
969     WORD32 value;
970     WORD32 cb_size = 1 << log2_cb_size;
971     WORD32 part_mode =  ps_codec->s_parse.s_cu.i4_part_mode;
972     tu_t *ps_tu = ps_codec->s_parse.ps_tu;
973     pu_t *ps_pu = ps_codec->s_parse.ps_pu;
974     WORD32 ctb_x_base;
975     WORD32 ctb_y_base;
976     ps_sps = ps_codec->s_parse.ps_sps;
977     ctb_x_base = ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size;
978     ctb_y_base = ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size;
979 
980     memset(ps_pu, 0, sizeof(pu_t));
981     ps_pu->b1_intra_flag = 1;
982     ps_pu->b4_wd = (cb_size >> 2) - 1;
983     ps_pu->b4_ht = (cb_size >> 2) - 1;
984     ps_pu->b4_pos_x = (x0 - ctb_x_base) >> 2;
985     ps_pu->b4_pos_y = (y0 - ctb_y_base) >> 2;
986 
987     pcm_flag = 0;
988     if((PART_2Nx2N == part_mode) && (ps_sps->i1_pcm_enabled_flag)
989                     && (log2_cb_size
990                                     >= ps_sps->i1_log2_min_pcm_coding_block_size)
991                     && (log2_cb_size
992                                     <= (ps_sps->i1_log2_min_pcm_coding_block_size + ps_sps->i1_log2_diff_max_min_pcm_coding_block_size)))
993     {
994         TRACE_CABAC_CTXT("pcm_flag", ps_cabac->u4_range, 0);
995         pcm_flag = ihevcd_cabac_decode_terminate(ps_cabac, ps_bitstrm);
996         AEV_TRACE("pcm_flag", pcm_flag, ps_cabac->u4_range);
997     }
998 
999     ps_codec->s_parse.i4_cu_pcm_flag = pcm_flag;
1000     if(pcm_flag)
1001     {
1002         UWORD8 *pu1_luma_intra_pred_mode_top, *pu1_luma_intra_pred_mode_left;
1003         WORD32 i,  num_pred_blocks;
1004 
1005         if(ps_codec->s_parse.s_bitstrm.u4_bit_ofst % 8)
1006         {
1007             TRACE_CABAC_CTXT("pcm_alignment_zero_bit", ps_cabac->u4_range, 0);
1008             ihevcd_bits_flush_to_byte_boundary(&ps_codec->s_parse.s_bitstrm);
1009             AEV_TRACE("pcm_alignment_zero_bit", 0, ps_cabac->u4_range);
1010         }
1011 
1012         ihevcd_parse_pcm_sample(ps_codec, x0, y0, log2_cb_size);
1013 
1014         ihevcd_cabac_reset(&ps_codec->s_parse.s_cabac,
1015                            &ps_codec->s_parse.s_bitstrm);
1016 
1017         ps_tu = ps_codec->s_parse.ps_tu;
1018         ps_tu->b1_cb_cbf = 1;
1019         ps_tu->b1_cr_cbf = 1;
1020         ps_tu->b1_y_cbf = 1;
1021         ps_tu->b4_pos_x = ((x0 - ctb_x_base) >> 2);
1022         ps_tu->b4_pos_y = ((y0 - ctb_y_base) >> 2);
1023         ps_tu->b1_transquant_bypass = 1;
1024         ps_tu->b3_size = (log2_cb_size - 2);
1025         ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
1026         ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
1027         ps_tu->b6_luma_intra_mode   = INTRA_PRED_NONE;
1028 
1029         /* Set the first TU in CU flag */
1030         {
1031             if((ps_codec->s_parse.s_cu.i4_pos_x << 3) == (ps_tu->b4_pos_x << 2) &&
1032                             (ps_codec->s_parse.s_cu.i4_pos_y << 3) == (ps_tu->b4_pos_y << 2))
1033             {
1034                 ps_tu->b1_first_tu_in_cu = 1;
1035             }
1036             else
1037             {
1038                 ps_tu->b1_first_tu_in_cu = 0;
1039             }
1040         }
1041 
1042         /* Update the intra pred mode for PCM to INTRA_DC(default mode) */
1043         pu1_luma_intra_pred_mode_top = ps_codec->s_parse.pu1_luma_intra_pred_mode_top
1044                         + (ps_codec->s_parse.s_cu.i4_pos_x * 2);
1045 
1046         pu1_luma_intra_pred_mode_left = ps_codec->s_parse.pu1_luma_intra_pred_mode_left
1047                         + (ps_codec->s_parse.s_cu.i4_pos_y * 2);
1048 
1049         num_pred_blocks = 1; /* Because PCM part mode will be 2Nx2N */
1050 
1051         ps_codec->s_func_selector.ihevc_memset_fptr(pu1_luma_intra_pred_mode_left, INTRA_DC, (cb_size / num_pred_blocks) / MIN_PU_SIZE);
1052         ps_codec->s_func_selector.ihevc_memset_fptr(pu1_luma_intra_pred_mode_top, INTRA_DC, (cb_size / num_pred_blocks) / MIN_PU_SIZE);
1053 
1054 
1055         /* Set no_loop_filter appropriately */
1056         if(1 == ps_sps->i1_pcm_loop_filter_disable_flag)
1057         {
1058             UWORD8 *pu1_pic_no_loop_filter_flag;
1059             WORD32 numbytes_row;
1060             UWORD32 u4_mask;
1061 
1062             pu1_pic_no_loop_filter_flag = ps_codec->s_parse.pu1_pic_no_loop_filter_flag;
1063             numbytes_row =  (ps_sps->i2_pic_width_in_luma_samples + 63) / 64;
1064             pu1_pic_no_loop_filter_flag += (y0 / 8) * numbytes_row;
1065             pu1_pic_no_loop_filter_flag += (x0 / 64);
1066             /* Generate (cb_size / 8) number of 1s */
1067             /* i.e (log2_cb_size - 2) number of 1s */
1068             u4_mask = LSB_ONES((cb_size >> 3));
1069             for(i = 0; i < (cb_size / 8); i++)
1070             {
1071                 *pu1_pic_no_loop_filter_flag |= (u4_mask << (((x0) / 8) % 8));
1072                 pu1_pic_no_loop_filter_flag += numbytes_row;
1073             }
1074         }
1075         /* Increment ps_tu and tu_idx */
1076         ps_codec->s_parse.ps_tu++;
1077         ps_codec->s_parse.s_cu.i4_tu_cnt++;
1078         ps_codec->s_parse.i4_pic_tu_idx++;
1079 
1080     }
1081     else
1082     {
1083         WORD32 cnt = 0;
1084         WORD32 i;
1085         WORD32 part_cnt;
1086 
1087         part_cnt = (part_mode == PART_NxN) ? 4 : 1;
1088 
1089         for(i = 0; i < part_cnt; i++)
1090         {
1091             TRACE_CABAC_CTXT("prev_intra_pred_luma_flag", ps_cabac->u4_range, IHEVC_CAB_INTRA_LUMA_PRED_FLAG);
1092             value = ihevcd_cabac_decode_bin(ps_cabac,
1093                                             ps_bitstrm,
1094                                             IHEVC_CAB_INTRA_LUMA_PRED_FLAG);
1095 
1096             ps_codec->s_parse.s_cu.ai4_prev_intra_luma_pred_flag[i] =
1097                             value;
1098             AEV_TRACE("prev_intra_pred_luma_flag", value, ps_cabac->u4_range);
1099         }
1100 
1101         for(i = 0; i < part_cnt; i++)
1102         {
1103             if(ps_codec->s_parse.s_cu.ai4_prev_intra_luma_pred_flag[cnt])
1104             {
1105                 value = ihevcd_cabac_decode_bypass_bins_tunary(ps_cabac, ps_bitstrm, 2);
1106                 AEV_TRACE("mpm_idx", value, ps_cabac->u4_range);
1107                 ps_codec->s_parse.s_cu.ai4_mpm_idx[cnt] = value;
1108             }
1109             else
1110             {
1111                 value = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, 5);
1112                 AEV_TRACE("rem_intra_luma_pred_mode", value,
1113                           ps_cabac->u4_range);
1114                 ps_codec->s_parse.s_cu.ai4_rem_intra_luma_pred_mode[cnt] =
1115                                 value;
1116             }
1117             cnt++;
1118         }
1119         TRACE_CABAC_CTXT("intra_chroma_pred_mode", ps_cabac->u4_range, IHEVC_CAB_CHROMA_PRED_MODE);
1120         value = ihevcd_cabac_decode_bin(ps_cabac,
1121                                         ps_bitstrm,
1122                                         IHEVC_CAB_CHROMA_PRED_MODE);
1123         ps_codec->s_parse.s_cu.i4_intra_chroma_pred_mode_idx = 4;
1124         if(value)
1125         {
1126             ps_codec->s_parse.s_cu.i4_intra_chroma_pred_mode_idx =
1127                             ihevcd_cabac_decode_bypass_bins(ps_cabac,
1128                                                             ps_bitstrm, 2);
1129         }
1130         AEV_TRACE("intra_chroma_pred_mode",
1131                   ps_codec->s_parse.s_cu.i4_intra_chroma_pred_mode_idx,
1132                   ps_cabac->u4_range);
1133 
1134 
1135         ihevcd_intra_pred_mode_prediction(ps_codec, log2_cb_size, x0, y0);
1136     }
1137     STATS_UPDATE_PU_SIZE(ps_pu);
1138     /* Increment PU pointer */
1139     ps_codec->s_parse.ps_pu++;
1140     ps_codec->s_parse.i4_pic_pu_idx++;
1141 
1142     return ret;
1143 }
1144 /**
1145  *******************************************************************************
1146  *
1147  * @brief
1148  *  Parses coding unit
1149  *
1150  * @par Description:
1151  *  Parses coding unit as per Section:7.3.9.5
1152  *
1153  * @param[in] ps_codec
1154  *  Pointer to codec context
1155  *
1156  * @returns  Error from IHEVCD_ERROR_T
1157  *
1158  * @remarks
1159  *
1160  *
1161  *******************************************************************************
1162  */
1163 
ihevcd_parse_coding_unit(codec_t * ps_codec,WORD32 x0,WORD32 y0,WORD32 log2_cb_size)1164 IHEVCD_ERROR_T  ihevcd_parse_coding_unit(codec_t *ps_codec,
1165                                          WORD32 x0,
1166                                          WORD32 y0,
1167                                          WORD32 log2_cb_size)
1168 {
1169     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1170     sps_t *ps_sps;
1171     pps_t *ps_pps;
1172     WORD32 cb_size;
1173     slice_header_t *ps_slice_hdr;
1174     WORD32 skip_flag;
1175     WORD32 pcm_flag;
1176     UWORD32 *pu4_skip_top = ps_codec->s_parse.pu4_skip_cu_top;
1177     UWORD32 u4_skip_left = ps_codec->s_parse.u4_skip_cu_left;
1178     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1179     tu_t *ps_tu = ps_codec->s_parse.ps_tu;
1180 
1181     WORD32 cu_pos_x;
1182     WORD32 cu_pos_y;
1183     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
1184 
1185     ASSERT(0 == (x0 % 8));
1186     ASSERT(0 == (y0 % 8));
1187 
1188     ps_codec->s_parse.s_cu.i4_tu_cnt = 0;
1189     ps_sps = ps_codec->s_parse.ps_sps;
1190     ps_pps = ps_codec->s_parse.ps_pps;
1191 
1192     cu_pos_x = ps_codec->s_parse.s_cu.i4_pos_x;
1193     cu_pos_y = ps_codec->s_parse.s_cu.i4_pos_y;
1194 
1195 
1196 
1197     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
1198 
1199 
1200     cb_size = 1 << log2_cb_size;
1201 
1202     ps_codec->s_parse.s_cu.i4_cu_transquant_bypass = 0;
1203 
1204     if(ps_pps->i1_transquant_bypass_enable_flag)
1205     {
1206         TRACE_CABAC_CTXT("cu_transquant_bypass_flag", ps_cabac->u4_range, IHEVC_CAB_CU_TQ_BYPASS_FLAG);
1207         ps_codec->s_parse.s_cu.i4_cu_transquant_bypass =
1208                         ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm,
1209                                                 IHEVC_CAB_CU_TQ_BYPASS_FLAG);
1210         /* Update transquant_bypass in ps_tu */
1211 
1212         AEV_TRACE("cu_transquant_bypass_flag", ps_codec->s_parse.s_cu.i4_cu_transquant_bypass,
1213                   ps_cabac->u4_range);
1214 
1215         if(ps_codec->s_parse.s_cu.i4_cu_transquant_bypass)
1216         {
1217             UWORD8 *pu1_pic_no_loop_filter_flag = ps_codec->s_parse.pu1_pic_no_loop_filter_flag;
1218             UWORD32 u4_mask;
1219             WORD32 i;
1220             WORD32 numbytes_row;
1221             numbytes_row =  (ps_sps->i2_pic_width_in_luma_samples + 63) / 64;
1222             pu1_pic_no_loop_filter_flag += (y0 / 8) * numbytes_row;
1223             pu1_pic_no_loop_filter_flag += (x0 / 64);
1224 
1225             /* Generate (cb_size / 8) number of 1s */
1226             /* i.e (log2_cb_size - 2) number of 1s */
1227             u4_mask = LSB_ONES((cb_size >> 3));
1228             for(i = 0; i < (cb_size / 8); i++)
1229             {
1230                 *pu1_pic_no_loop_filter_flag |= (u4_mask << (((x0) / 8) % 8));
1231                 pu1_pic_no_loop_filter_flag += numbytes_row;
1232             }
1233         }
1234     }
1235 
1236     {
1237         UWORD32 u4_skip_top = 0;
1238         UWORD32 u4_mask;
1239         UWORD32 u4_top_mask, u4_left_mask;
1240         UWORD32 u4_min_cu_x = x0 / 8;
1241         UWORD32 u4_min_cu_y = y0 / 8;
1242 
1243         pu4_skip_top += (u4_min_cu_x / 32);
1244 
1245 
1246         if(ps_slice_hdr->i1_slice_type != ISLICE)
1247         {
1248             WORD32 ctx_idx_inc;
1249             ctx_idx_inc = 0;
1250 
1251             if((0 != cu_pos_y) ||
1252                             ((0 != ps_codec->s_parse.i4_ctb_slice_y) &&
1253                                             (0 != ps_codec->s_parse.i4_ctb_tile_y)))
1254             {
1255                 u4_skip_top = *pu4_skip_top;
1256                 u4_skip_top >>= (u4_min_cu_x % 32);
1257                 if(u4_skip_top & 1)
1258                     ctx_idx_inc++;
1259             }
1260 
1261             /*****************************************************************/
1262             /* If cu_pos_x is non-zero then left is available                */
1263             /* If cu_pos_x is zero then ensure both the following are true   */
1264             /*    Current CTB is not the first CTB in a tile row             */
1265             /*    Current CTB is not the first CTB in a slice                */
1266             /*****************************************************************/
1267             if((0 != cu_pos_x) ||
1268                             (((0 != ps_codec->s_parse.i4_ctb_slice_x) || (0 != ps_codec->s_parse.i4_ctb_slice_y)) &&
1269                                             (0 != ps_codec->s_parse.i4_ctb_tile_x)))
1270             {
1271                 u4_skip_left >>= (u4_min_cu_y % 32);
1272                 if(u4_skip_left & 1)
1273                     ctx_idx_inc++;
1274             }
1275             TRACE_CABAC_CTXT("cu_skip_flag", ps_cabac->u4_range, (IHEVC_CAB_SKIP_FLAG + ctx_idx_inc));
1276             skip_flag = ihevcd_cabac_decode_bin(ps_cabac,
1277                                                 ps_bitstrm,
1278                                                 (IHEVC_CAB_SKIP_FLAG + ctx_idx_inc));
1279 
1280             AEV_TRACE("cu_skip_flag", skip_flag, ps_cabac->u4_range);
1281         }
1282         else
1283             skip_flag = 0;
1284 
1285         /* Update top skip_flag */
1286         u4_skip_top = *pu4_skip_top;
1287         /* Since Max cb_size is 64, maximum of 8 bits will be set or reset */
1288         /* Also since Coding block will be within 64x64 grid, only 8bits within a WORD32
1289          * need to be updated. These 8 bits will not cross 8 bit boundaries
1290          */
1291         u4_mask = LSB_ONES(cb_size / 8);
1292         u4_top_mask = u4_mask << (u4_min_cu_x % 32);
1293 
1294 
1295         if(skip_flag)
1296         {
1297             u4_skip_top |= u4_top_mask;
1298         }
1299         else
1300         {
1301             u4_skip_top &= ~u4_top_mask;
1302         }
1303         *pu4_skip_top = u4_skip_top;
1304 
1305         /* Update left skip_flag */
1306         u4_skip_left = ps_codec->s_parse.u4_skip_cu_left;
1307         u4_mask = LSB_ONES(cb_size / 8);
1308         u4_left_mask = u4_mask << (u4_min_cu_y % 32);
1309 
1310         if(skip_flag)
1311         {
1312             u4_skip_left |= u4_left_mask;
1313         }
1314         else
1315         {
1316             u4_skip_left &= ~u4_left_mask;
1317         }
1318         ps_codec->s_parse.u4_skip_cu_left = u4_skip_left;
1319     }
1320     ps_codec->s_parse.i4_cu_pcm_flag = 0;
1321 
1322     if(skip_flag)
1323     {
1324         WORD32 ctb_x_base;
1325         WORD32 ctb_y_base;
1326 
1327         ctb_x_base = ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size;
1328         ctb_y_base = ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size;
1329 
1330         ps_tu->b1_cb_cbf = 0;
1331         ps_tu->b1_cr_cbf = 0;
1332         ps_tu->b1_y_cbf = 0;
1333         ps_tu->b4_pos_x = ((x0 - ctb_x_base) >> 2);
1334         ps_tu->b4_pos_y = ((y0 - ctb_y_base) >> 2);
1335         ps_tu->b1_transquant_bypass = 0;
1336         ps_tu->b3_size = (log2_cb_size - 2);
1337         ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
1338         ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
1339         ps_tu->b6_luma_intra_mode   = INTRA_PRED_NONE;
1340 
1341         /* Set the first TU in CU flag */
1342         {
1343             if((ps_codec->s_parse.s_cu.i4_pos_x << 3) == (ps_tu->b4_pos_x << 2) &&
1344                             (ps_codec->s_parse.s_cu.i4_pos_y << 3) == (ps_tu->b4_pos_y << 2))
1345             {
1346                 ps_tu->b1_first_tu_in_cu = 1;
1347             }
1348             else
1349             {
1350                 ps_tu->b1_first_tu_in_cu = 0;
1351             }
1352         }
1353 
1354         ps_codec->s_parse.ps_tu++;
1355         ps_codec->s_parse.s_cu.i4_tu_cnt++;
1356         ps_codec->s_parse.i4_pic_tu_idx++;
1357 
1358         ps_codec->s_parse.s_cu.i4_pred_mode = PRED_MODE_SKIP;
1359         ps_codec->s_parse.s_cu.i4_part_mode = PART_2Nx2N;
1360         {
1361             pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1362             ps_pu->b2_part_idx = 0;
1363             ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size, cb_size);
1364             STATS_UPDATE_PU_SKIP_SIZE(ps_pu);
1365         }
1366     }
1367     else
1368     {
1369         WORD32 pred_mode;
1370         WORD32 part_mode;
1371         WORD32 intra_split_flag;
1372         WORD32 is_mincb;
1373         cb_size = (1 << log2_cb_size);
1374         is_mincb = (cb_size == (1 << ps_sps->i1_log2_min_coding_block_size));
1375         pcm_flag = 0;
1376         if(ps_slice_hdr->i1_slice_type != ISLICE)
1377         {
1378             TRACE_CABAC_CTXT("pred_mode_flag", ps_cabac->u4_range, IHEVC_CAB_PRED_MODE);
1379             pred_mode = ihevcd_cabac_decode_bin(ps_cabac,
1380                                                 ps_bitstrm,
1381                                                 IHEVC_CAB_PRED_MODE);
1382 
1383             AEV_TRACE("pred_mode_flag", pred_mode, ps_cabac->u4_range);
1384         }
1385         else
1386         {
1387             pred_mode = PRED_MODE_INTRA;
1388         }
1389 
1390         /* If current CU is intra then set corresponging bit in picture level intra map */
1391         if(PRED_MODE_INTRA == pred_mode)
1392         {
1393             UWORD8 *pu1_pic_intra_flag = ps_codec->s_parse.pu1_pic_intra_flag;
1394             UWORD32 u4_mask;
1395             WORD32 i;
1396             WORD32 numbytes_row;
1397             numbytes_row =  (ps_sps->i2_pic_width_in_luma_samples + 63) / 64;
1398             pu1_pic_intra_flag += (y0 / 8) * numbytes_row;
1399             pu1_pic_intra_flag += (x0 / 64);
1400 
1401             /* Generate (cb_size / 8) number of 1s */
1402             /* i.e (log2_cb_size - 2) number of 1s */
1403             u4_mask = LSB_ONES((cb_size >> 3));
1404             for(i = 0; i < (cb_size / 8); i++)
1405             {
1406                 *pu1_pic_intra_flag |= (u4_mask << (((x0) / 8) % 8));
1407                 pu1_pic_intra_flag += numbytes_row;
1408             }
1409         }
1410 
1411         ps_codec->s_parse.s_cu.i4_pred_mode = pred_mode;
1412         intra_split_flag = 0;
1413         if((PRED_MODE_INTRA != pred_mode) ||
1414                         is_mincb)
1415         {
1416             UWORD32 bin;
1417             if(PRED_MODE_INTRA == pred_mode)
1418             {
1419                 TRACE_CABAC_CTXT("part_mode", ps_cabac->u4_range, IHEVC_CAB_PART_MODE);
1420                 bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, IHEVC_CAB_PART_MODE);
1421                 part_mode = (bin) ? PART_2Nx2N : PART_NxN;
1422             }
1423             else
1424             {
1425                 WORD32 amp_enabled = ps_sps->i1_amp_enabled_flag;
1426 
1427                 UWORD32 u4_max_bin_cnt = 0;
1428 
1429 
1430 
1431                 if(amp_enabled && !is_mincb)
1432                 {
1433                     part_mode = ihevcd_parse_part_mode_amp(ps_cabac, ps_bitstrm);
1434                 }
1435                 else
1436                 {
1437                     WORD32 ctxt_inc = IHEVC_CAB_PART_MODE;
1438 
1439                     u4_max_bin_cnt = 2;
1440                     if((is_mincb) && (cb_size > 8))
1441                     {
1442                         u4_max_bin_cnt++;
1443                     }
1444 
1445                     part_mode = -1;
1446                     TRACE_CABAC_CTXT("part_mode", ps_cabac->u4_range, IHEVC_CAB_PART_MODE);
1447                     do
1448                     {
1449                         bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm,
1450                                                       ctxt_inc++);
1451                         part_mode++;
1452                     }while(--u4_max_bin_cnt && !bin);
1453 
1454                     /* If the last bin was zero, then increment part mode by 1 */
1455                     if(!bin)
1456                         part_mode++;
1457                 }
1458 
1459 
1460             }
1461 
1462             AEV_TRACE("part_mode", part_mode, ps_cabac->u4_range);
1463 
1464         }
1465         else
1466         {
1467             part_mode = 0;
1468             intra_split_flag = 0;
1469         }
1470         ps_codec->s_parse.s_cu.i4_part_mode = part_mode;
1471 
1472         if((PRED_MODE_INTRA == ps_codec->s_parse.s_cu.i4_pred_mode) &&
1473                         (PART_NxN == ps_codec->s_parse.s_cu.i4_part_mode))
1474         {
1475             intra_split_flag = 1;
1476         }
1477         ps_codec->s_parse.s_cu.i4_part_mode = part_mode;
1478         ps_codec->s_parse.s_cu.i4_intra_split_flag = intra_split_flag;
1479         if(pred_mode == PRED_MODE_INTRA)
1480         {
1481             ps_codec->s_parse.i4_cu_pcm_flag = 0;
1482             ihevcd_parse_coding_unit_intra(ps_codec, x0, y0, log2_cb_size);
1483             pcm_flag = ps_codec->s_parse.i4_cu_pcm_flag;
1484 
1485         }
1486         else
1487         {
1488             if(part_mode == PART_2Nx2N)
1489             {
1490                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1491                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size, cb_size);
1492                 ps_pu->b2_part_idx = 0;
1493             }
1494             else if(part_mode == PART_2NxN)
1495             {
1496                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1497 
1498                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size, cb_size / 2);
1499                 ps_pu->b2_part_idx = 0;
1500 
1501                 ps_pu = ps_codec->s_parse.ps_pu;
1502                 ihevcd_parse_prediction_unit(ps_codec, x0, y0 + (cb_size / 2), cb_size, cb_size / 2);
1503 
1504                 ps_pu->b2_part_idx = 1;
1505             }
1506             else if(part_mode == PART_Nx2N)
1507             {
1508                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1509                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size / 2, cb_size);
1510                 ps_pu->b2_part_idx = 0;
1511                 ps_pu = ps_codec->s_parse.ps_pu;
1512                 ihevcd_parse_prediction_unit(ps_codec, x0 + (cb_size / 2), y0, cb_size / 2, cb_size);
1513 
1514                 ps_pu->b2_part_idx = 1;
1515             }
1516             else if(part_mode == PART_2NxnU)
1517             {
1518                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1519                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size, cb_size / 4);
1520                 ps_pu->b2_part_idx = 0;
1521                 ps_pu = ps_codec->s_parse.ps_pu;
1522                 ihevcd_parse_prediction_unit(ps_codec, x0, y0 + (cb_size / 4), cb_size, cb_size * 3 / 4);
1523 
1524                 ps_pu->b2_part_idx = 1;
1525             }
1526             else if(part_mode == PART_2NxnD)
1527             {
1528                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1529                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size, cb_size * 3 / 4);
1530                 ps_pu->b2_part_idx = 0;
1531                 ps_pu = ps_codec->s_parse.ps_pu;
1532                 ihevcd_parse_prediction_unit(ps_codec, x0, y0 + (cb_size * 3 / 4), cb_size, cb_size / 4);
1533 
1534                 ps_pu->b2_part_idx = 1;
1535             }
1536             else if(part_mode == PART_nLx2N)
1537             {
1538                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1539                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size / 4, cb_size);
1540                 ps_pu->b2_part_idx = 0;
1541                 ps_pu = ps_codec->s_parse.ps_pu;
1542                 ihevcd_parse_prediction_unit(ps_codec, x0 + (cb_size / 4), y0, cb_size * 3 / 4, cb_size);
1543 
1544                 ps_pu->b2_part_idx = 1;
1545             }
1546             else if(part_mode == PART_nRx2N)
1547             {
1548                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1549                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size * 3 / 4, cb_size);
1550                 ps_pu->b2_part_idx = 0;
1551                 ps_pu = ps_codec->s_parse.ps_pu;
1552                 ihevcd_parse_prediction_unit(ps_codec, x0 + (cb_size * 3 / 4), y0, cb_size / 4, cb_size);
1553                 ps_pu->b2_part_idx = 1;
1554             }
1555             else
1556             { /* PART_NxN */
1557                 pu_t *ps_pu = ps_codec->s_parse.ps_pu;
1558 
1559                 ihevcd_parse_prediction_unit(ps_codec, x0, y0, cb_size / 2, cb_size / 2);
1560                 ps_pu->b2_part_idx = 0;
1561                 ps_pu = ps_codec->s_parse.ps_pu;
1562                 ihevcd_parse_prediction_unit(ps_codec, x0 + (cb_size / 2), y0, cb_size / 2, cb_size / 2);
1563 
1564                 ps_pu->b2_part_idx = 1;
1565                 ps_pu = ps_codec->s_parse.ps_pu;
1566                 ihevcd_parse_prediction_unit(ps_codec, x0, y0 + (cb_size / 2), cb_size / 2, cb_size / 2);
1567 
1568                 ps_pu->b2_part_idx = 2;
1569                 ps_pu = ps_codec->s_parse.ps_pu;
1570                 ihevcd_parse_prediction_unit(ps_codec, x0 + (cb_size / 2), y0 + (cb_size / 2), cb_size / 2, cb_size / 2);
1571 
1572                 ps_pu->b2_part_idx = 3;
1573             }
1574         }
1575 
1576         if(!pcm_flag)
1577         {
1578             WORD32 no_residual_syntax_flag = 0;
1579             pu_t *ps_pu;
1580             /* Since ps_pu is incremented for each PU parsed, decrement by 1 to
1581              *  access last decoded PU
1582              */
1583             ps_pu = ps_codec->s_parse.ps_pu - 1;
1584             if((PRED_MODE_INTRA != pred_mode) &&
1585                             (!((part_mode == PART_2Nx2N) && ps_pu->b1_merge_flag)))
1586             {
1587 
1588                 TRACE_CABAC_CTXT("rqt_root_cbf", ps_cabac->u4_range, IHEVC_CAB_NORES_IDX);
1589                 no_residual_syntax_flag = ihevcd_cabac_decode_bin(ps_cabac,
1590                                                                   ps_bitstrm,
1591                                                                   IHEVC_CAB_NORES_IDX);
1592 
1593                 AEV_TRACE("rqt_root_cbf", no_residual_syntax_flag,
1594                           ps_cabac->u4_range);
1595                 /* TODO: HACK FOR COMPLIANCE WITH HM REFERENCE DECODER */
1596                 /*********************************************************/
1597                 /* currently the HM decoder expects qtroot cbf instead of */
1598                 /* no_residue_flag which has opposite meaning             */
1599                 /* This will be fixed once the software / spec is fixed   */
1600                 /*********************************************************/
1601                 no_residual_syntax_flag = 1 - no_residual_syntax_flag;
1602             }
1603 
1604             if(!no_residual_syntax_flag)
1605             {
1606 
1607                 ps_codec->s_parse.s_cu.i4_max_trafo_depth = (pred_mode == PRED_MODE_INTRA) ?
1608                                 (ps_sps->i1_max_transform_hierarchy_depth_intra + intra_split_flag) :
1609                                 (ps_sps->i1_max_transform_hierarchy_depth_inter);
1610                 ret = ihevcd_parse_transform_tree(ps_codec, x0, y0, x0, y0,
1611                                                   log2_cb_size, 0, 0,
1612                                                   ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[0]);
1613                 RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
1614             }
1615             else
1616             {
1617                 WORD32 ctb_x_base;
1618                 WORD32 ctb_y_base;
1619 
1620                 ctb_x_base = ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size;
1621                 ctb_y_base = ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size;
1622 
1623                 ps_tu = ps_codec->s_parse.ps_tu;
1624                 ps_tu->b1_cb_cbf = 0;
1625                 ps_tu->b1_cr_cbf = 0;
1626                 ps_tu->b1_y_cbf = 0;
1627                 ps_tu->b4_pos_x = ((x0 - ctb_x_base) >> 2);
1628                 ps_tu->b4_pos_y = ((y0 - ctb_y_base) >> 2);
1629                 ps_tu->b1_transquant_bypass = 0;
1630                 ps_tu->b3_size = (log2_cb_size - 2);
1631                 ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
1632                 ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
1633                 ps_tu->b6_luma_intra_mode   = ps_codec->s_parse.s_cu.ai4_intra_luma_pred_mode[0];
1634 
1635                 /* Set the first TU in CU flag */
1636                 {
1637                     if((ps_codec->s_parse.s_cu.i4_pos_x << 3) == (ps_tu->b4_pos_x << 2) &&
1638                                     (ps_codec->s_parse.s_cu.i4_pos_y << 3) == (ps_tu->b4_pos_y << 2))
1639                     {
1640                         ps_tu->b1_first_tu_in_cu = 1;
1641                     }
1642                     else
1643                     {
1644                         ps_tu->b1_first_tu_in_cu = 0;
1645                     }
1646                 }
1647                 ps_codec->s_parse.ps_tu++;
1648                 ps_codec->s_parse.s_cu.i4_tu_cnt++;
1649                 ps_codec->s_parse.i4_pic_tu_idx++;
1650 
1651             }
1652         }
1653 
1654     }
1655 
1656 
1657 
1658 
1659     return ret;
1660 }
1661 
1662 
1663 
1664 
1665 /**
1666  *******************************************************************************
1667  *
1668  * @brief
1669  *  Parses Coding Quad Tree
1670  *
1671  * @par Description:
1672  *  Parses Coding Quad Tree as per Section:7.3.9.4
1673  *
1674  * @param[in] ps_codec
1675  *  Pointer to codec context
1676  *
1677  * @returns  Error from IHEVCD_ERROR_T
1678  *
1679  * @remarks
1680  *
1681  *
1682  *******************************************************************************
1683  */
ihevcd_parse_coding_quadtree(codec_t * ps_codec,WORD32 x0,WORD32 y0,WORD32 log2_cb_size,WORD32 ct_depth)1684 IHEVCD_ERROR_T ihevcd_parse_coding_quadtree(codec_t *ps_codec,
1685                                             WORD32 x0,
1686                                             WORD32 y0,
1687                                             WORD32 log2_cb_size,
1688                                             WORD32 ct_depth)
1689 {
1690     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1691     sps_t *ps_sps;
1692     pps_t *ps_pps;
1693     WORD32 split_cu_flag;
1694     WORD32 x1, y1;
1695     WORD32 cu_pos_x;
1696     WORD32 cu_pos_y;
1697     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1698     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
1699     WORD32 cb_size = 1 << log2_cb_size;
1700     ps_sps = ps_codec->s_parse.ps_sps;
1701     ps_pps = ps_codec->s_parse.ps_pps;
1702 
1703     /* Compute CU position with respect to current CTB in (8x8) units */
1704     cu_pos_x = (x0 - (ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size)) >> 3;
1705     cu_pos_y = (y0 - (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size)) >> 3;
1706 
1707     ps_codec->s_parse.s_cu.i4_pos_x = cu_pos_x;
1708     ps_codec->s_parse.s_cu.i4_pos_y = cu_pos_y;
1709 
1710     ps_codec->s_parse.s_cu.i4_log2_cb_size = log2_cb_size;
1711 
1712     ps_codec->s_parse.i4_ct_depth = ct_depth;
1713     {
1714         UWORD32 *pu4_ct_depth_top = ps_codec->s_parse.pu4_ct_depth_top;
1715         UWORD32 u4_ct_depth_left = ps_codec->s_parse.u4_ct_depth_left;
1716         UWORD32 u4_ct_depth_top = 0;
1717         UWORD32 u4_mask;
1718         UWORD32 u4_top_mask, u4_left_mask;
1719         WORD32  ctxt_idx;
1720         UWORD32 u4_min_cu_x = x0 / 8;
1721         UWORD32 u4_min_cu_y = y0 / 8;
1722 
1723         pu4_ct_depth_top += (u4_min_cu_x / 16);
1724 
1725 
1726 
1727 
1728         if(((x0 + (1 << log2_cb_size)) <= ps_sps->i2_pic_width_in_luma_samples) &&
1729                         ((y0 + (1 << log2_cb_size)) <= ps_sps->i2_pic_height_in_luma_samples) &&
1730                         (log2_cb_size > ps_sps->i1_log2_min_coding_block_size))
1731         {
1732 
1733             ctxt_idx = IHEVC_CAB_SPLIT_CU_FLAG;
1734             /* Split cu context increment is decided based on left and top Coding tree
1735              * depth which is stored at frame level
1736              */
1737             /* Check if the CTB is in first row in the current slice or tile */
1738             if((0 != cu_pos_y) ||
1739                             ((0 != ps_codec->s_parse.i4_ctb_slice_y) &&
1740                                             (0 != ps_codec->s_parse.i4_ctb_tile_y)))
1741             {
1742                 u4_ct_depth_top = *pu4_ct_depth_top;
1743                 u4_ct_depth_top >>= ((u4_min_cu_x % 16) * 2);
1744                 u4_ct_depth_top &= 3;
1745 
1746                 if((WORD32)u4_ct_depth_top > ct_depth)
1747                     ctxt_idx++;
1748             }
1749 
1750             /* Check if the CTB is in first column in the current slice or tile */
1751             /*****************************************************************/
1752             /* If cu_pos_x is non-zero then left is available                */
1753             /* If cu_pos_x is zero then ensure both the following are true   */
1754             /*    Current CTB is not the first CTB in a tile row             */
1755             /*    Current CTB is not the first CTB in a slice                */
1756             /*****************************************************************/
1757             if((0 != cu_pos_x) ||
1758                             (((0 != ps_codec->s_parse.i4_ctb_slice_x) || (0 != ps_codec->s_parse.i4_ctb_slice_y)) &&
1759                                             (0 != ps_codec->s_parse.i4_ctb_tile_x)))
1760             {
1761                 u4_ct_depth_left >>= ((u4_min_cu_y % 16) * 2);
1762                 u4_ct_depth_left &= 3;
1763                 if((WORD32)u4_ct_depth_left > ct_depth)
1764                     ctxt_idx++;
1765             }
1766             TRACE_CABAC_CTXT("split_cu_flag", ps_cabac->u4_range, ctxt_idx);
1767             split_cu_flag = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
1768             AEV_TRACE("split_cu_flag", split_cu_flag, ps_cabac->u4_range);
1769         }
1770         else
1771         {
1772             if(log2_cb_size > ps_sps->i1_log2_min_coding_block_size)
1773                 split_cu_flag = 1;
1774             else
1775                 split_cu_flag = 0;
1776         }
1777 
1778         if(0 == split_cu_flag)
1779         {
1780             /* Update top ct_depth */
1781             u4_ct_depth_top = *pu4_ct_depth_top;
1782             /* Since Max cb_size is 64, maximum of 8 bits will be set or reset */
1783             /* Also since Coding block will be within 64x64 grid, only 8bits within a WORD32
1784              * need to be updated. These 8 bits will not cross 8 bit boundaries
1785              */
1786             u4_mask = DUP_LSB_11(cb_size / 8);
1787 
1788             u4_top_mask = u4_mask << ((u4_min_cu_x % 16) * 2);
1789             u4_ct_depth_top &= ~u4_top_mask;
1790 
1791             if(ct_depth)
1792             {
1793                 u4_top_mask = gau4_ct_depth_mask[ct_depth] & u4_mask;
1794 
1795                 u4_top_mask = u4_top_mask << ((u4_min_cu_x % 16) * 2);
1796                 u4_ct_depth_top |= u4_top_mask;
1797             }
1798 
1799             *pu4_ct_depth_top = u4_ct_depth_top;
1800 
1801             /* Update left ct_depth */
1802             u4_ct_depth_left = ps_codec->s_parse.u4_ct_depth_left;
1803 
1804             u4_left_mask = u4_mask << ((u4_min_cu_y % 16) * 2);
1805 
1806             u4_ct_depth_left &= ~u4_left_mask;
1807             if(ct_depth)
1808             {
1809                 u4_left_mask = gau4_ct_depth_mask[ct_depth] & u4_mask;
1810 
1811                 u4_left_mask = u4_left_mask << ((u4_min_cu_y % 16) * 2);
1812                 u4_ct_depth_left |= u4_left_mask;
1813             }
1814 
1815             ps_codec->s_parse.u4_ct_depth_left = u4_ct_depth_left;
1816         }
1817     }
1818     if((ps_pps->i1_cu_qp_delta_enabled_flag) &&
1819                     (log2_cb_size >= ps_pps->i1_log2_min_cu_qp_delta_size))
1820     {
1821         ps_codec->s_parse.i4_is_cu_qp_delta_coded = 0;
1822         ps_codec->s_parse.i4_cu_qp_delta = 0;
1823     }
1824     if(split_cu_flag)
1825     {
1826         x1 = x0 + ((1 << log2_cb_size) >> 1);
1827         y1 = y0 + ((1 << log2_cb_size) >> 1);
1828 
1829         ret = ihevcd_parse_coding_quadtree(ps_codec, x0, y0, log2_cb_size - 1, ct_depth + 1);
1830         RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
1831 
1832         /* At frame boundaries coding quadtree nodes are sent only if they fall within the frame */
1833         if(x1 < ps_sps->i2_pic_width_in_luma_samples)
1834         {
1835             ret = ihevcd_parse_coding_quadtree(ps_codec, x1, y0, log2_cb_size - 1, ct_depth + 1);
1836             RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
1837         }
1838 
1839         if(y1 < ps_sps->i2_pic_height_in_luma_samples)
1840         {
1841             ret = ihevcd_parse_coding_quadtree(ps_codec, x0, y1, log2_cb_size - 1, ct_depth + 1);
1842             RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
1843         }
1844 
1845         if((x1 < ps_sps->i2_pic_width_in_luma_samples) &&
1846                         (y1 < ps_sps->i2_pic_height_in_luma_samples))
1847         {
1848             ret = ihevcd_parse_coding_quadtree(ps_codec, x1, y1, log2_cb_size - 1, ct_depth + 1);
1849             RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
1850         }
1851     }
1852     else
1853     {
1854         WORD32 qp = ps_codec->s_parse.u4_qp;
1855         /* Set current group QP if current CU is aligned with the group */
1856         {
1857             WORD32 cu_pos_x = ps_codec->s_parse.s_cu.i4_pos_x << 3;
1858             WORD32 cu_pos_y = ps_codec->s_parse.s_cu.i4_pos_y << 3;
1859 
1860             WORD32 qpg_x = (cu_pos_x - (cu_pos_x & ((1 << ps_pps->i1_log2_min_cu_qp_delta_size) - 1)));
1861             WORD32 qpg_y = (cu_pos_y - (cu_pos_y & ((1 << ps_pps->i1_log2_min_cu_qp_delta_size) - 1)));
1862 
1863             if((cu_pos_x == qpg_x) &&
1864                             (cu_pos_y == qpg_y))
1865             {
1866                 ps_codec->s_parse.u4_qpg = ps_codec->s_parse.u4_qp;
1867 
1868                 ps_codec->s_parse.s_cu.i4_cu_qp_delta = 0;
1869 
1870             }
1871         }
1872 
1873         ret = ihevcd_parse_coding_unit(ps_codec, x0, y0, log2_cb_size);
1874         RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
1875 
1876         if(ps_pps->i1_cu_qp_delta_enabled_flag)
1877         {
1878             WORD32 qp_pred, qp_left, qp_top;
1879             WORD32 cu_pos_x;
1880             WORD32 cu_pos_y;
1881             WORD32 qpg_x;
1882             WORD32 qpg_y;
1883             WORD32 i, j;
1884             WORD32 cur_cu_offset;
1885             tu_t *ps_tu = ps_codec->s_parse.ps_tu;
1886             WORD32 cb_size = 1 << ps_codec->s_parse.s_cu.i4_log2_cb_size;
1887 
1888             cu_pos_x = ps_codec->s_parse.s_cu.i4_pos_x << 3;
1889             cu_pos_y = ps_codec->s_parse.s_cu.i4_pos_y << 3;
1890 
1891             qpg_x = (cu_pos_x - (cu_pos_x & ((1 << ps_pps->i1_log2_min_cu_qp_delta_size) - 1))) >> 3;
1892             qpg_y = (cu_pos_y - (cu_pos_y & ((1 << ps_pps->i1_log2_min_cu_qp_delta_size) - 1))) >> 3;
1893 
1894             /*previous coded Qp*/
1895             qp_left = ps_codec->s_parse.u4_qpg;
1896             qp_top = ps_codec->s_parse.u4_qpg;
1897 
1898             if(qpg_x > 0)
1899             {
1900                 qp_left = ps_codec->s_parse.ai1_8x8_cu_qp[qpg_x - 1 + (qpg_y * 8)];
1901             }
1902             if(qpg_y > 0)
1903             {
1904                 qp_top = ps_codec->s_parse.ai1_8x8_cu_qp[qpg_x + ((qpg_y - 1) * 8)];
1905             }
1906 
1907             qp_pred = (qp_left + qp_top + 1) >> 1;
1908             /* Since qp_pred + ps_codec->s_parse.s_cu.i4_cu_qp_delta can be negative,
1909             52 is added before taking modulo 52 */
1910             qp = (qp_pred + ps_codec->s_parse.s_cu.i4_cu_qp_delta + 52) % 52;
1911 
1912             cur_cu_offset = (cu_pos_x >> 3) + cu_pos_y;
1913             for(i = 0; i < (cb_size >> 3); i++)
1914             {
1915                 for(j = 0; j < (cb_size >> 3); j++)
1916                 {
1917                     ps_codec->s_parse.ai1_8x8_cu_qp[cur_cu_offset + (i * 8) + j] = qp;
1918                 }
1919             }
1920 
1921             ps_codec->s_parse.u4_qp = qp;
1922             ps_codec->s_parse.s_cu.i4_qp = qp;
1923 
1924 
1925             /* When change in QP is signaled, update the QP in TUs that are already parsed in the CU */
1926             {
1927                 tu_t *ps_tu_tmp;
1928                 ps_tu_tmp = ps_tu - ps_codec->s_parse.s_cu.i4_tu_cnt;
1929                 ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
1930                 while(ps_tu_tmp != ps_tu)
1931                 {
1932                     ps_tu_tmp->b7_qp = ps_codec->s_parse.u4_qp;
1933 
1934                     ps_tu_tmp++;
1935                 }
1936             }
1937             if(ps_codec->s_parse.s_cu.i4_cu_qp_delta)
1938             {
1939                 WORD32 ctb_indx;
1940                 ctb_indx = ps_codec->s_parse.i4_ctb_x + ps_sps->i2_pic_wd_in_ctb * ps_codec->s_parse.i4_ctb_y;
1941                 ps_codec->s_parse.s_bs_ctxt.pu1_pic_qp_const_in_ctb[ctb_indx >> 3] &= (~(1 << (ctb_indx & 7)));
1942             }
1943 
1944         }
1945         /* Populate CU_info maps for current CU*/
1946         if(ps_codec->u1_enable_cu_info)
1947         {
1948             WORD32 cu_info_map_stride = ALIGN64(ps_codec->i4_wd) >> 3;
1949             UWORD8 num_8x8_blks = 1 << (log2_cb_size - 3);
1950             WORD32 vert_8x8, horz_8x8;
1951             UWORD8 *pu1_cur_cu_type_map =
1952                 ps_codec->as_buf_id_info_map[ps_codec->as_process[0].
1953                     i4_cur_pic_buf_id].pu1_cu_type_map;
1954             UWORD8 *pu1_cur_qp_map =
1955                 ps_codec->as_buf_id_info_map[ps_codec->as_process[0].i4_cur_pic_buf_id].pu1_qp_map;
1956             UWORD8 *pu1_cur_type_cu = pu1_cur_cu_type_map + (x0 >> 3) +
1957                 (y0 >> 3) * cu_info_map_stride;
1958             UWORD8 *pu1_cur_qp_cu = pu1_cur_qp_map + (x0 >> 3) +
1959                 (y0 >> 3) * cu_info_map_stride;
1960             for(vert_8x8 = 0; vert_8x8 < num_8x8_blks; vert_8x8++)
1961             {
1962                 for(horz_8x8 = 0; horz_8x8 < num_8x8_blks; horz_8x8++)
1963                 {
1964                     pu1_cur_qp_cu[horz_8x8] = qp;
1965                     pu1_cur_type_cu[horz_8x8] = ps_codec->s_parse.s_cu.i4_pred_mode;
1966                 }
1967                 pu1_cur_qp_cu += cu_info_map_stride;
1968                 pu1_cur_type_cu += cu_info_map_stride;
1969             }
1970         }
1971 
1972     }
1973 
1974 
1975 
1976 
1977     return ret;
1978 }
1979 
1980 
1981 /**
1982  *******************************************************************************
1983  *
1984  * @brief
1985  *  Parses SAO (Sample adaptive offset syntax)
1986  *
1987  * @par Description:
1988  *  Parses SAO (Sample adaptive offset syntax) as per  Section:7.3.9.3
1989  *
1990  * @param[in] ps_codec
1991  *  Pointer to codec context
1992  *
1993  * @returns  Error from IHEVCD_ERROR_T
1994  *
1995  * @remarks
1996  *
1997  *
1998  *******************************************************************************
1999  */
ihevcd_parse_sao(codec_t * ps_codec)2000 IHEVCD_ERROR_T  ihevcd_parse_sao(codec_t *ps_codec)
2001 {
2002     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2003     sps_t *ps_sps;
2004     sao_t *ps_sao;
2005     WORD32 rx;
2006     WORD32 ry;
2007     WORD32 value;
2008     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
2009     WORD32 sao_merge_left_flag;
2010     WORD32 sao_merge_up_flag;
2011     slice_header_t *ps_slice_hdr;
2012     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
2013     WORD32 ctxt_idx;
2014 
2015     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base;
2016     ps_slice_hdr += (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1));
2017 
2018     ps_sps = (ps_codec->s_parse.ps_sps);
2019     rx = ps_codec->s_parse.i4_ctb_x;
2020     ry = ps_codec->s_parse.i4_ctb_y;
2021 
2022     ps_sao = ps_codec->s_parse.ps_pic_sao + rx + ry * ps_sps->i2_pic_wd_in_ctb;
2023 
2024     /* Default values */
2025     ps_sao->b3_y_type_idx = 0;
2026     ps_sao->b3_cb_type_idx = 0;
2027     ps_sao->b3_cr_type_idx = 0;
2028 
2029     UNUSED(value);
2030     ctxt_idx = IHEVC_CAB_SAO_MERGE;
2031     sao_merge_left_flag = 0;
2032     sao_merge_up_flag = 0;
2033     if(rx > 0)
2034     {
2035         /*TODO:Implemented only for slice. condition for tile is not tested*/
2036         if(((0 != ps_codec->s_parse.i4_ctb_slice_x) || (0 != ps_codec->s_parse.i4_ctb_slice_y)) &&
2037                         (0 != ps_codec->s_parse.i4_ctb_tile_x))
2038         {
2039 
2040             TRACE_CABAC_CTXT("sao_merge_flag", ps_cabac->u4_range, ctxt_idx);
2041             sao_merge_left_flag = ihevcd_cabac_decode_bin(ps_cabac,
2042                                                           ps_bitstrm,
2043                                                           ctxt_idx);
2044             AEV_TRACE("sao_merge_flag", sao_merge_left_flag, ps_cabac->u4_range);
2045         }
2046 
2047     }
2048     if(ry > 0 && !sao_merge_left_flag)
2049     {
2050         if((ps_codec->s_parse.i4_ctb_slice_y > 0) && (ps_codec->s_parse.i4_ctb_tile_y > 0))
2051         {
2052             TRACE_CABAC_CTXT("sao_merge_flag", ps_cabac->u4_range, ctxt_idx);
2053             sao_merge_up_flag = ihevcd_cabac_decode_bin(ps_cabac,
2054                                                         ps_bitstrm,
2055                                                         ctxt_idx);
2056             AEV_TRACE("sao_merge_flag", sao_merge_up_flag, ps_cabac->u4_range);
2057         }
2058     }
2059     ctxt_idx = IHEVC_CAB_SAO_TYPE;
2060 
2061     if(sao_merge_left_flag)
2062     {
2063         *ps_sao = *(ps_sao - 1);
2064     }
2065     else if(sao_merge_up_flag)
2066     {
2067         *ps_sao = *(ps_sao - ps_sps->i2_pic_wd_in_ctb);
2068     }
2069     else // if(!sao_merge_up_flag && !sao_merge_left_flag)
2070     {
2071         WORD32 c_idx;
2072         WORD32 sao_type_idx = 0;
2073         for(c_idx = 0; c_idx < 3; c_idx++)
2074         {
2075             if((ps_slice_hdr->i1_slice_sao_luma_flag && c_idx == 0) || (ps_slice_hdr->i1_slice_sao_chroma_flag && c_idx > 0))
2076             {
2077 
2078 
2079                 /* sao_type_idx will be same for c_idx == 1 and c_idx == 2 - hence not initialized to zero for c_idx == 2*/
2080 
2081                 if(c_idx == 0)
2082                 {
2083                     sao_type_idx = 0;
2084                     TRACE_CABAC_CTXT("sao_type_idx", ps_cabac->u4_range, ctxt_idx);
2085                     sao_type_idx = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
2086 
2087                     if(sao_type_idx)
2088                     {
2089                         sao_type_idx += ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
2090                     }
2091                     AEV_TRACE("sao_type_idx", sao_type_idx,  ps_cabac->u4_range);
2092 
2093                     ps_sao->b3_y_type_idx = sao_type_idx;
2094                 }
2095                 if(c_idx == 1)
2096                 {
2097                     sao_type_idx = 0;
2098                     TRACE_CABAC_CTXT("sao_type_idx", ps_cabac->u4_range, ctxt_idx);
2099                     sao_type_idx = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
2100                     if(sao_type_idx)
2101                     {
2102                         sao_type_idx += ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
2103                     }
2104 
2105                     AEV_TRACE("sao_type_idx", sao_type_idx,  ps_cabac->u4_range);
2106 
2107                     ps_sao->b3_cb_type_idx = sao_type_idx;
2108                     ps_sao->b3_cr_type_idx = sao_type_idx;
2109                 }
2110 
2111                 if(sao_type_idx != 0)
2112                 {
2113                     WORD32 i;
2114                     WORD32 sao_offset[4];
2115                     WORD32 sao_band_position = 0;
2116                     WORD32 c_max =  (1 << (MIN(BIT_DEPTH, 10) - 5)) - 1;
2117                     for(i = 0; i < 4; i++)
2118                     {
2119                         sao_offset[i] = ihevcd_cabac_decode_bypass_bins_tunary(ps_cabac, ps_bitstrm, c_max);
2120                         AEV_TRACE("sao_offset_abs", sao_offset[i], ps_cabac->u4_range);
2121 
2122                         if((2 == sao_type_idx) && (i > 1))
2123                         {
2124                             sao_offset[i] = -sao_offset[i];
2125                         }
2126                     }
2127 
2128                     if(sao_type_idx == 1)
2129                     {
2130                         for(i = 0; i < 4; i++)
2131                         {
2132                             if(sao_offset[i] != 0)
2133                             {
2134                                 value = ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
2135                                 AEV_TRACE("sao_offset_sign", value, ps_cabac->u4_range);
2136 
2137                                 if(value)
2138                                 {
2139                                     sao_offset[i] = -sao_offset[i];
2140                                 }
2141                             }
2142                         }
2143                         value = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, 5);
2144                         AEV_TRACE("sao_band_position", value, ps_cabac->u4_range);
2145 
2146                         sao_band_position = value;
2147                     }
2148                     else
2149                     {
2150                         if(c_idx == 0)
2151                         {
2152                             value = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, 2);
2153                             AEV_TRACE("sao_eo_class", value, ps_cabac->u4_range);
2154 
2155                             ps_sao->b3_y_type_idx += value;
2156                         }
2157 
2158                         if(c_idx == 1)
2159                         {
2160                             value = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, 2);
2161                             AEV_TRACE("sao_eo_class", value, ps_cabac->u4_range);
2162 
2163                             ps_sao->b3_cb_type_idx += value;
2164                             ps_sao->b3_cr_type_idx += value;
2165                         }
2166                     }
2167 
2168                     if(0 == c_idx)
2169                     {
2170                         ps_sao->b4_y_offset_1 = sao_offset[0];
2171                         ps_sao->b4_y_offset_2 = sao_offset[1];
2172                         ps_sao->b4_y_offset_3 = sao_offset[2];
2173                         ps_sao->b4_y_offset_4 = sao_offset[3];
2174 
2175                         ps_sao->b5_y_band_pos = sao_band_position;
2176                     }
2177                     else if(1 == c_idx)
2178                     {
2179                         ps_sao->b4_cb_offset_1 = sao_offset[0];
2180                         ps_sao->b4_cb_offset_2 = sao_offset[1];
2181                         ps_sao->b4_cb_offset_3 = sao_offset[2];
2182                         ps_sao->b4_cb_offset_4 = sao_offset[3];
2183 
2184                         ps_sao->b5_cb_band_pos = sao_band_position;
2185                     }
2186                     else // 2 == c_idx
2187                     {
2188                         ps_sao->b4_cr_offset_1 = sao_offset[0];
2189                         ps_sao->b4_cr_offset_2 = sao_offset[1];
2190                         ps_sao->b4_cr_offset_3 = sao_offset[2];
2191                         ps_sao->b4_cr_offset_4 = sao_offset[3];
2192 
2193                         ps_sao->b5_cr_band_pos = sao_band_position;
2194                     }
2195                 }
2196             }
2197         }
2198     }
2199 
2200     return ret;
2201 }
2202 /**
2203  *******************************************************************************
2204  *
2205  * @brief
2206  *  Set ctb skip
2207  *
2208  * @par Description:
2209  *  During error, sets tu and pu params of a ctb as skip.
2210  *
2211  * @param[in] ps_codec
2212  *  Pointer to codec context
2213  *
2214  * @returns  None
2215  *
2216  * @remarks
2217  *
2218  *
2219  *******************************************************************************
2220  */
ihevcd_set_ctb_skip(codec_t * ps_codec)2221 void ihevcd_set_ctb_skip(codec_t *ps_codec)
2222 {
2223     tu_t *ps_tu;
2224     pu_t *ps_pu;
2225     sps_t *ps_sps = ps_codec->s_parse.ps_sps;
2226     WORD32 ctb_size = 1 << ps_sps->i1_log2_ctb_size;
2227     WORD32 ctb_skip_wd, ctb_skip_ht;
2228     WORD32 rows_remaining, cols_remaining;
2229     WORD32 tu_abs_x, tu_abs_y;
2230     WORD32 numbytes_row =  (ps_sps->i2_pic_width_in_luma_samples + 63) / 64;
2231     UWORD8 *pu1_pic_intra_flag;
2232     UWORD32 u4_mask;
2233     WORD32 pu_x,pu_y;
2234 
2235     /* Set pu wd and ht based on whether the ctb is complete or not */
2236     rows_remaining = ps_sps->i2_pic_height_in_luma_samples
2237                     - (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size);
2238     ctb_skip_ht = MIN(ctb_size, rows_remaining);
2239 
2240     cols_remaining = ps_sps->i2_pic_width_in_luma_samples
2241                     - (ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size);
2242     ctb_skip_wd = MIN(ctb_size, cols_remaining);
2243 
2244     ps_codec->s_parse.s_cu.i4_pred_mode = PRED_MODE_SKIP;
2245     ps_codec->s_parse.s_cu.i4_part_mode = PART_2Nx2N;
2246 
2247     for (pu_y = 0; pu_y < ctb_skip_ht ; pu_y += MIN_CU_SIZE)
2248     {
2249         for (pu_x = 0; pu_x < ctb_skip_wd ; pu_x += MIN_CU_SIZE)
2250         {
2251             ps_tu = ps_codec->s_parse.ps_tu;
2252             ps_tu->b1_cb_cbf = 0;
2253             ps_tu->b1_cr_cbf = 0;
2254             ps_tu->b1_y_cbf = 0;
2255             ps_tu->b4_pos_x = pu_x >> 2;
2256             ps_tu->b4_pos_y = pu_y >> 2;
2257             ps_tu->b1_transquant_bypass = 0;
2258             ps_tu->b3_size = 1;
2259             ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
2260             ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
2261             ps_tu->b6_luma_intra_mode   = INTRA_PRED_NONE;
2262             ps_tu->b1_first_tu_in_cu = 1;
2263 
2264             ps_codec->s_parse.ps_tu++;
2265             ps_codec->s_parse.s_cu.i4_tu_cnt++;
2266             ps_codec->s_parse.i4_pic_tu_idx++;
2267 
2268             tu_abs_x = (ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size) + pu_x;
2269             tu_abs_y = (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size) + pu_y;
2270             pu1_pic_intra_flag = ps_codec->s_parse.pu1_pic_intra_flag;
2271             pu1_pic_intra_flag += (tu_abs_y >> 3) * numbytes_row;
2272             pu1_pic_intra_flag += (tu_abs_x >> 6);
2273             u4_mask = (LSB_ONES((MIN_CU_SIZE >> 3)) << (((tu_abs_x) / 8) % 8));
2274             u4_mask = ~u4_mask;
2275             *pu1_pic_intra_flag &= u4_mask;
2276 
2277             ps_pu = ps_codec->s_parse.ps_pu;
2278             ps_pu->b2_part_idx = 0;
2279             ps_pu->b4_pos_x = pu_x >> 2;
2280             ps_pu->b4_pos_y = pu_y >> 2;
2281             ps_pu->b4_wd = 1;
2282             ps_pu->b4_ht = 1;
2283             ps_pu->b1_intra_flag = 0;
2284             ps_pu->b3_part_mode = ps_codec->s_parse.s_cu.i4_part_mode;
2285             ps_pu->b1_merge_flag = 1;
2286             ps_pu->b3_merge_idx = 0;
2287 
2288             ps_codec->s_parse.ps_pu++;
2289             ps_codec->s_parse.i4_pic_pu_idx++;
2290         }
2291     }
2292 }
2293 
2294 /**
2295  *******************************************************************************
2296  *
2297  * @brief
2298  *  Parses Slice data syntax
2299  *
2300  * @par Description:
2301  *  Parses Slice data syntax as per Section:7.3.9.1
2302  *
2303  * @param[in] ps_codec
2304  *  Pointer to codec context
2305  *
2306  * @returns  Error from IHEVCD_ERROR_T
2307  *
2308  * @remarks
2309  *
2310  *
2311  *******************************************************************************
2312  */
ihevcd_parse_slice_data(codec_t * ps_codec)2313 IHEVCD_ERROR_T ihevcd_parse_slice_data(codec_t *ps_codec)
2314 {
2315 
2316     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2317     WORD32 end_of_slice_flag = 0;
2318     sps_t *ps_sps;
2319     pps_t *ps_pps;
2320     slice_header_t *ps_slice_hdr;
2321     WORD32 end_of_pic;
2322     tile_t *ps_tile, *ps_tile_prev;
2323     WORD32 i;
2324     WORD32 ctb_addr;
2325     WORD32 tile_idx;
2326     WORD32 cabac_init_idc;
2327     WORD32 ctb_size;
2328     WORD32 num_ctb_in_row;
2329     WORD32 num_min4x4_in_ctb;
2330     WORD32 slice_qp;
2331     WORD32 slice_start_ctb_idx;
2332     WORD32 tile_start_ctb_idx;
2333 
2334 
2335     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base;
2336     ps_pps = ps_codec->s_parse.ps_pps_base;
2337     ps_sps = ps_codec->s_parse.ps_sps_base;
2338 
2339     /* Get current slice header, pps and sps */
2340     ps_slice_hdr += (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1));
2341     ps_pps  += ps_slice_hdr->i1_pps_id;
2342     ps_sps  += ps_pps->i1_sps_id;
2343 
2344     if(0 != ps_codec->s_parse.i4_cur_slice_idx)
2345     {
2346         if(!ps_slice_hdr->i1_dependent_slice_flag)
2347         {
2348             ps_codec->s_parse.i4_cur_independent_slice_idx =
2349                     ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1);
2350         }
2351     }
2352 
2353 
2354     ctb_size = 1 << ps_sps->i1_log2_ctb_size;
2355     num_min4x4_in_ctb = (ctb_size / 4) * (ctb_size / 4);
2356     num_ctb_in_row = ps_sps->i2_pic_wd_in_ctb;
2357 
2358     /* Update the parse context */
2359     if(0 == ps_codec->i4_slice_error)
2360     {
2361         ps_codec->s_parse.i4_ctb_x = ps_slice_hdr->i2_ctb_x;
2362         ps_codec->s_parse.i4_ctb_y = ps_slice_hdr->i2_ctb_y;
2363     }
2364     ps_codec->s_parse.ps_pps = ps_pps;
2365     ps_codec->s_parse.ps_sps = ps_sps;
2366     ps_codec->s_parse.ps_slice_hdr = ps_slice_hdr;
2367 
2368     /* Derive Tile positions for the current CTB */
2369     /* Change this to lookup if required */
2370     ihevcd_get_tile_pos(ps_pps, ps_sps, ps_codec->s_parse.i4_ctb_x,
2371                         ps_codec->s_parse.i4_ctb_y,
2372                         &ps_codec->s_parse.i4_ctb_tile_x,
2373                         &ps_codec->s_parse.i4_ctb_tile_y,
2374                         &tile_idx);
2375     ps_codec->s_parse.ps_tile = ps_pps->ps_tile + tile_idx;
2376     ps_codec->s_parse.i4_cur_tile_idx = tile_idx;
2377     ps_tile = ps_codec->s_parse.ps_tile;
2378     if(tile_idx)
2379         ps_tile_prev = ps_tile - 1;
2380     else
2381         ps_tile_prev = ps_tile;
2382 
2383     /* If the present slice is dependent, then store the previous
2384      * independent slices' ctb x and y values for decoding process */
2385     if(0 == ps_codec->i4_slice_error)
2386     {
2387         if(1 == ps_slice_hdr->i1_dependent_slice_flag)
2388         {
2389             /*If slice is present at the start of a new tile*/
2390             if((0 == ps_codec->s_parse.i4_ctb_tile_x) && (0 == ps_codec->s_parse.i4_ctb_tile_y))
2391             {
2392                 ps_codec->s_parse.i4_ctb_slice_x = 0;
2393                 ps_codec->s_parse.i4_ctb_slice_y = 0;
2394             }
2395         }
2396 
2397         if(!ps_slice_hdr->i1_dependent_slice_flag)
2398         {
2399             ps_codec->s_parse.i4_ctb_slice_x = 0;
2400             ps_codec->s_parse.i4_ctb_slice_y = 0;
2401         }
2402     }
2403 
2404     /* Frame level initializations */
2405     if((0 == ps_codec->s_parse.i4_ctb_y) &&
2406                     (0 == ps_codec->s_parse.i4_ctb_x))
2407     {
2408         ret = ihevcd_parse_pic_init(ps_codec);
2409         RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
2410 
2411         ps_codec->s_parse.pu4_pic_tu_idx[0] = 0;
2412         ps_codec->s_parse.pu4_pic_pu_idx[0] = 0;
2413         ps_codec->s_parse.i4_cur_independent_slice_idx = 0;
2414         ps_codec->s_parse.i4_ctb_tile_x = 0;
2415         ps_codec->s_parse.i4_ctb_tile_y = 0;
2416     }
2417 
2418     {
2419         /* Updating the poc list of current slice to ps_mv_buf */
2420         mv_buf_t *ps_mv_buf = ps_codec->s_parse.ps_cur_mv_buf;
2421 
2422         if(ps_slice_hdr->i1_num_ref_idx_l1_active != 0)
2423         {
2424             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
2425             {
2426                 ps_mv_buf->ai4_l1_collocated_poc[(ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1))][i] = ((pic_buf_t *)ps_slice_hdr->as_ref_pic_list1[i].pv_pic_buf)->i4_abs_poc;
2427                 ps_mv_buf->ai1_l1_collocated_poc_lt[(ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1))][i] = ((pic_buf_t *)ps_slice_hdr->as_ref_pic_list1[i].pv_pic_buf)->u1_used_as_ref;
2428             }
2429         }
2430 
2431         if(ps_slice_hdr->i1_num_ref_idx_l0_active != 0)
2432         {
2433             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
2434             {
2435                 ps_mv_buf->ai4_l0_collocated_poc[(ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1))][i] = ((pic_buf_t *)ps_slice_hdr->as_ref_pic_list0[i].pv_pic_buf)->i4_abs_poc;
2436                 ps_mv_buf->ai1_l0_collocated_poc_lt[(ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1))][i] = ((pic_buf_t *)ps_slice_hdr->as_ref_pic_list0[i].pv_pic_buf)->u1_used_as_ref;
2437             }
2438         }
2439     }
2440 
2441     /*Initialize the low delay flag at the beginning of every slice*/
2442     if((0 == ps_codec->s_parse.i4_ctb_slice_x) || (0 == ps_codec->s_parse.i4_ctb_slice_y))
2443     {
2444         /* Lowdelay flag */
2445         WORD32 cur_poc, ref_list_poc, flag = 1;
2446         cur_poc = ps_slice_hdr->i4_abs_pic_order_cnt;
2447         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
2448         {
2449             ref_list_poc = ((mv_buf_t *)ps_slice_hdr->as_ref_pic_list0[i].pv_mv_buf)->i4_abs_poc;
2450             if(ref_list_poc > cur_poc)
2451             {
2452                 flag = 0;
2453                 break;
2454             }
2455         }
2456         if(flag && (ps_slice_hdr->i1_slice_type == BSLICE))
2457         {
2458             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
2459             {
2460                 ref_list_poc = ((mv_buf_t *)ps_slice_hdr->as_ref_pic_list1[i].pv_mv_buf)->i4_abs_poc;
2461                 if(ref_list_poc > cur_poc)
2462                 {
2463                     flag = 0;
2464                     break;
2465                 }
2466             }
2467         }
2468         ps_slice_hdr->i1_low_delay_flag = flag;
2469     }
2470 
2471     /* initialize the cabac init idc based on slice type */
2472     if(ps_slice_hdr->i1_slice_type == ISLICE)
2473     {
2474         cabac_init_idc = 0;
2475     }
2476     else if(ps_slice_hdr->i1_slice_type == PSLICE)
2477     {
2478         cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 2 : 1;
2479     }
2480     else
2481     {
2482         cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 1 : 2;
2483     }
2484 
2485     slice_qp = ps_slice_hdr->i1_slice_qp_delta + ps_pps->i1_pic_init_qp;
2486     slice_qp = CLIP3(slice_qp, 0, 51);
2487 
2488     /*Update QP value for every indepndent slice or for every dependent slice that begins at the start of a new tile*/
2489     if((0 == ps_slice_hdr->i1_dependent_slice_flag) ||
2490                     ((1 == ps_slice_hdr->i1_dependent_slice_flag) && ((0 == ps_codec->s_parse.i4_ctb_tile_x) && (0 == ps_codec->s_parse.i4_ctb_tile_y))))
2491     {
2492         ps_codec->s_parse.u4_qp = slice_qp;
2493     }
2494 
2495     /*Cabac init at the beginning of a slice*/
2496     //If the slice is a dependent slice, not present at the start of a tile
2497     if(0 == ps_codec->i4_slice_error)
2498     {
2499         if((1 == ps_slice_hdr->i1_dependent_slice_flag) && (!((ps_codec->s_parse.i4_ctb_tile_x == 0) && (ps_codec->s_parse.i4_ctb_tile_y == 0))))
2500         {
2501             if((0 == ps_pps->i1_entropy_coding_sync_enabled_flag) || (ps_pps->i1_entropy_coding_sync_enabled_flag && (0 != ps_codec->s_parse.i4_ctb_x)))
2502             {
2503                 ihevcd_cabac_reset(&ps_codec->s_parse.s_cabac,
2504                                    &ps_codec->s_parse.s_bitstrm);
2505             }
2506         }
2507         else if((0 == ps_pps->i1_entropy_coding_sync_enabled_flag) || (ps_pps->i1_entropy_coding_sync_enabled_flag && (0 != ps_codec->s_parse.i4_ctb_x)))
2508         {
2509             ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2510                                     &ps_codec->s_parse.s_bitstrm,
2511                                     slice_qp,
2512                                     cabac_init_idc,
2513                                     &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0]);
2514             if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2515             {
2516                 ps_codec->i4_slice_error = 1;
2517                 end_of_slice_flag = 1;
2518                 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2519             }
2520         }
2521     }
2522 
2523 
2524     do
2525     {
2526 
2527         {
2528             WORD32 cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
2529                             + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
2530             if(1 == ps_codec->i4_num_cores && 0 == cur_ctb_idx % RESET_TU_BUF_NCTB)
2531             {
2532                 ps_codec->s_parse.ps_tu = ps_codec->s_parse.ps_pic_tu;
2533                 ps_codec->s_parse.i4_pic_tu_idx = 0;
2534             }
2535         }
2536 
2537         end_of_pic = 0;
2538         /* Section:7.3.7 Coding tree unit syntax */
2539         /* coding_tree_unit() inlined here */
2540         /* If number of cores is greater than 1, then add job to the queue */
2541         //TODO: Dual core implementation might need a different algo for better load balancing
2542         /* At the start of ctb row parsing in a tile, queue a job for processing the current tile row */
2543         ps_codec->s_parse.i4_ctb_num_pcm_blks = 0;
2544 
2545 
2546         /*At the beginning of each tile-which is not the beginning of a slice, cabac context must be initialized.
2547          * Hence, check for the tile beginning here */
2548         if(((0 == ps_codec->s_parse.i4_ctb_tile_x) && (0 == ps_codec->s_parse.i4_ctb_tile_y))
2549                         && (!((ps_tile->u1_pos_x == 0) && (ps_tile->u1_pos_y == 0)))
2550                         && (!((0 == ps_codec->s_parse.i4_ctb_slice_x) && (0 == ps_codec->s_parse.i4_ctb_slice_y))))
2551         {
2552             slice_qp = ps_slice_hdr->i1_slice_qp_delta + ps_pps->i1_pic_init_qp;
2553             slice_qp = CLIP3(slice_qp, 0, 51);
2554             ps_codec->s_parse.u4_qp = slice_qp;
2555 
2556             ihevcd_get_tile_pos(ps_pps, ps_sps, ps_codec->s_parse.i4_ctb_x,
2557                                 ps_codec->s_parse.i4_ctb_y,
2558                                 &ps_codec->s_parse.i4_ctb_tile_x,
2559                                 &ps_codec->s_parse.i4_ctb_tile_y,
2560                                 &tile_idx);
2561 
2562             ps_codec->s_parse.ps_tile = ps_pps->ps_tile + tile_idx;
2563             ps_codec->s_parse.i4_cur_tile_idx = tile_idx;
2564             ps_tile_prev = ps_tile - 1;
2565 
2566             tile_start_ctb_idx = ps_tile->u1_pos_x
2567                             + ps_tile->u1_pos_y * (ps_sps->i2_pic_wd_in_ctb);
2568 
2569             slice_start_ctb_idx =  ps_slice_hdr->i2_ctb_x
2570                             + ps_slice_hdr->i2_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
2571 
2572             /*For slices that span across multiple tiles*/
2573             if(slice_start_ctb_idx < tile_start_ctb_idx)
2574             {       /* 2 Cases
2575              * 1 - slice spans across frame-width- but does not start from 1st column
2576              * 2 - Slice spans across multiple tiles anywhere is a frame
2577              */
2578                 ps_codec->s_parse.i4_ctb_slice_y = ps_tile->u1_pos_y - ps_slice_hdr->i2_ctb_y;
2579                 if(!(((ps_slice_hdr->i2_ctb_x + ps_tile_prev->u2_wd) % ps_sps->i2_pic_wd_in_ctb) == ps_tile->u1_pos_x)) //Case 2
2580                 {
2581                     if(ps_slice_hdr->i2_ctb_y <= ps_tile->u1_pos_y)
2582                     {
2583                         //Check if ctb x is before or after
2584                         if(ps_slice_hdr->i2_ctb_x > ps_tile->u1_pos_x)
2585                         {
2586                             ps_codec->s_parse.i4_ctb_slice_y -= 1;
2587                         }
2588                     }
2589                 }
2590                 /*ps_codec->s_parse.i4_ctb_slice_y = ps_tile->u1_pos_y - ps_slice_hdr->i2_ctb_y;
2591                 if (ps_slice_hdr->i2_ctb_y <= ps_tile->u1_pos_y)
2592                 {
2593                     //Check if ctb x is before or after
2594                     if (ps_slice_hdr->i2_ctb_x > ps_tile->u1_pos_x )
2595                     {
2596                         ps_codec->s_parse.i4_ctb_slice_y -= 1 ;
2597                     }
2598                 }*/
2599             }
2600 
2601             /* Cabac init is done unconditionally at the start of the tile irrespective
2602              * of whether it is a dependent or an independent slice */
2603             if(0 == ps_codec->i4_slice_error)
2604             {
2605                 ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2606                                         &ps_codec->s_parse.s_bitstrm,
2607                                         slice_qp,
2608                                         cabac_init_idc,
2609                                         &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0]);
2610                 if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2611                 {
2612                     ps_codec->i4_slice_error = 1;
2613                     end_of_slice_flag = 1;
2614                     ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2615                 }
2616 
2617             }
2618         }
2619         /* If number of cores is greater than 1, then add job to the queue */
2620         //TODO: Dual core implementation might need a different algo for better load balancing
2621         /* At the start of ctb row parsing in a tile, queue a job for processing the current tile row */
2622 
2623         if(0 == ps_codec->s_parse.i4_ctb_tile_x)
2624         {
2625 
2626             if(1 < ps_codec->i4_num_cores)
2627             {
2628                 proc_job_t s_job;
2629                 IHEVCD_ERROR_T ret;
2630                 s_job.i4_cmd    = CMD_PROCESS;
2631                 s_job.i2_ctb_cnt = (WORD16)ps_tile->u2_wd;
2632                 s_job.i2_ctb_x = (WORD16)ps_codec->s_parse.i4_ctb_x;
2633                 s_job.i2_ctb_y = (WORD16)ps_codec->s_parse.i4_ctb_y;
2634                 s_job.i2_slice_idx = (WORD16)ps_codec->s_parse.i4_cur_slice_idx;
2635                 s_job.i4_tu_coeff_data_ofst = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data -
2636                                 (UWORD8 *)ps_codec->s_parse.pv_pic_tu_coeff_data;
2637                 ret = ihevcd_jobq_queue((jobq_t *)ps_codec->s_parse.pv_proc_jobq, &s_job, sizeof(proc_job_t), 1);
2638 
2639                 if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2640                     return ret;
2641             }
2642             else
2643             {
2644                 process_ctxt_t *ps_proc = &ps_codec->as_process[0];
2645                 WORD32 tu_coeff_data_ofst = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data -
2646                                 (UWORD8 *)ps_codec->s_parse.pv_pic_tu_coeff_data;
2647 
2648                 /* If the codec is running in single core mode,
2649                  * initialize zeroth process context
2650                  * TODO: Dual core mode might need a different implementation instead of jobq
2651                  */
2652 
2653                 ps_proc->i4_ctb_cnt = ps_tile->u2_wd;
2654                 ps_proc->i4_ctb_x   = ps_codec->s_parse.i4_ctb_x;
2655                 ps_proc->i4_ctb_y   = ps_codec->s_parse.i4_ctb_y;
2656                 ps_proc->i4_cur_slice_idx = ps_codec->s_parse.i4_cur_slice_idx;
2657 
2658                 ihevcd_init_proc_ctxt(ps_proc, tu_coeff_data_ofst);
2659             }
2660         }
2661 
2662 
2663         /* Restore cabac context model from top right CTB if entropy sync is enabled */
2664         if(ps_pps->i1_entropy_coding_sync_enabled_flag)
2665         {
2666             /*TODO Handle single CTB and top-right belonging to a different slice */
2667             if(0 == ps_codec->s_parse.i4_ctb_x && 0 == ps_codec->i4_slice_error)
2668             {
2669                 //WORD32 size = sizeof(ps_codec->s_parse.s_cabac.au1_ctxt_models);
2670                 WORD32 default_ctxt = 0;
2671 
2672                 if((0 == ps_codec->s_parse.i4_ctb_slice_y) && (!ps_slice_hdr->i1_dependent_slice_flag))
2673                     default_ctxt = 1;
2674                 if(1 == ps_sps->i2_pic_wd_in_ctb)
2675                     default_ctxt = 1;
2676 
2677                 ps_codec->s_parse.u4_qp = slice_qp;
2678                 if(default_ctxt)
2679                 {
2680                     //memcpy(&ps_codec->s_parse.s_cabac.au1_ctxt_models, &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0], size);
2681                     ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2682                                             &ps_codec->s_parse.s_bitstrm,
2683                                             slice_qp,
2684                                             cabac_init_idc,
2685                                             &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0]);
2686 
2687                     if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2688                     {
2689                         ps_codec->i4_slice_error = 1;
2690                         end_of_slice_flag = 1;
2691                         ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2692                     }
2693                 }
2694                 else
2695                 {
2696                     //memcpy(&ps_codec->s_parse.s_cabac.au1_ctxt_models, &ps_codec->s_parse.s_cabac.au1_ctxt_models_sync, size);
2697                     ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2698                                             &ps_codec->s_parse.s_bitstrm,
2699                                             slice_qp,
2700                                             cabac_init_idc,
2701                                             (const UWORD8 *)&ps_codec->s_parse.s_cabac.au1_ctxt_models_sync);
2702 
2703                     if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2704                     {
2705                         ps_codec->i4_slice_error = 1;
2706                         end_of_slice_flag = 1;
2707                         ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2708                     }
2709                 }
2710             }
2711         }
2712 
2713 
2714 
2715         if(0 == ps_codec->i4_slice_error)
2716         {
2717             if(ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag)
2718                 ihevcd_parse_sao(ps_codec);
2719         }
2720         else
2721         {
2722             sao_t *ps_sao = ps_codec->s_parse.ps_pic_sao +
2723                             ps_codec->s_parse.i4_ctb_x +
2724                             ps_codec->s_parse.i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
2725 
2726             /* Default values */
2727             ps_sao->b3_y_type_idx = 0;
2728             ps_sao->b3_cb_type_idx = 0;
2729             ps_sao->b3_cr_type_idx = 0;
2730         }
2731 
2732         //AEV_TRACE("CTB x", ps_codec->s_parse.i4_ctb_x, 0);
2733         //AEV_TRACE("CTB y", ps_codec->s_parse.i4_ctb_y, 0);
2734 
2735         {
2736             WORD32 ctb_indx;
2737             ctb_indx = ps_codec->s_parse.i4_ctb_x + ps_sps->i2_pic_wd_in_ctb * ps_codec->s_parse.i4_ctb_y;
2738             ps_codec->s_parse.s_bs_ctxt.pu1_pic_qp_const_in_ctb[ctb_indx >> 3] |= (1 << (ctb_indx & 7));
2739             {
2740                 UWORD16 *pu1_slice_idx = ps_codec->s_parse.pu1_slice_idx;
2741                 pu1_slice_idx[ctb_indx] = ps_codec->s_parse.i4_cur_independent_slice_idx;
2742             }
2743         }
2744 
2745         if(0 == ps_codec->i4_slice_error)
2746         {
2747             tu_t *ps_tu = ps_codec->s_parse.ps_tu;
2748             WORD32 i4_tu_cnt = ps_codec->s_parse.s_cu.i4_tu_cnt;
2749             WORD32 i4_pic_tu_idx = ps_codec->s_parse.i4_pic_tu_idx;
2750 
2751             pu_t *ps_pu = ps_codec->s_parse.ps_pu;
2752             WORD32 i4_pic_pu_idx = ps_codec->s_parse.i4_pic_pu_idx;
2753 
2754             UWORD8 *pu1_tu_coeff_data = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data;
2755 
2756             ret = ihevcd_parse_coding_quadtree(ps_codec,
2757                                                (ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size),
2758                                                (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size),
2759                                                ps_sps->i1_log2_ctb_size,
2760                                                0);
2761             /* Check for error */
2762             if (ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2763             {
2764                 /* Reset tu and pu parameters, and signal current ctb as skip */
2765                 WORD32 tu_coeff_data_reset_size;
2766 
2767                 ps_codec->s_parse.ps_tu = ps_tu;
2768                 ps_codec->s_parse.s_cu.i4_tu_cnt = i4_tu_cnt;
2769                 ps_codec->s_parse.i4_pic_tu_idx = i4_pic_tu_idx;
2770 
2771                 ps_codec->s_parse.ps_pu = ps_pu;
2772                 ps_codec->s_parse.i4_pic_pu_idx = i4_pic_pu_idx;
2773 
2774                 tu_coeff_data_reset_size = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data - pu1_tu_coeff_data;
2775                 memset(pu1_tu_coeff_data, 0, tu_coeff_data_reset_size);
2776                 ps_codec->s_parse.pv_tu_coeff_data = (void *)pu1_tu_coeff_data;
2777 
2778                 ihevcd_set_ctb_skip(ps_codec);
2779 
2780                 /* Set slice error to suppress further parsing and
2781                  * signal end of slice.
2782                  */
2783                 ps_codec->i4_slice_error = 1;
2784                 end_of_slice_flag = 1;
2785                 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2786             }
2787         }
2788         else
2789         {
2790             ihevcd_set_ctb_skip(ps_codec);
2791         }
2792 
2793         if(0 == ps_codec->i4_slice_error)
2794             end_of_slice_flag = ihevcd_cabac_decode_terminate(&ps_codec->s_parse.s_cabac, &ps_codec->s_parse.s_bitstrm);
2795 
2796         AEV_TRACE("end_of_slice_flag", end_of_slice_flag, ps_codec->s_parse.s_cabac.u4_range);
2797 
2798 
2799         /* In case of tiles or entropy sync, terminate cabac and copy cabac context backed up at the end of top-right CTB */
2800         if(ps_pps->i1_tiles_enabled_flag || ps_pps->i1_entropy_coding_sync_enabled_flag)
2801         {
2802             WORD32 end_of_tile = 0;
2803             WORD32 end_of_tile_row = 0;
2804 
2805             /* Take a back up of cabac context models if entropy sync is enabled */
2806             if(ps_pps->i1_entropy_coding_sync_enabled_flag || ps_pps->i1_tiles_enabled_flag)
2807             {
2808                 if(1 == ps_codec->s_parse.i4_ctb_x)
2809                 {
2810                     WORD32 size = sizeof(ps_codec->s_parse.s_cabac.au1_ctxt_models);
2811                     memcpy(&ps_codec->s_parse.s_cabac.au1_ctxt_models_sync, &ps_codec->s_parse.s_cabac.au1_ctxt_models, size);
2812                 }
2813             }
2814 
2815             /* Since tiles and entropy sync are not enabled simultaneously, the following will not result in any problems */
2816             if((ps_codec->s_parse.i4_ctb_tile_x + 1) == (ps_tile->u2_wd))
2817             {
2818                 end_of_tile_row = 1;
2819                 if((ps_codec->s_parse.i4_ctb_tile_y + 1) == ps_tile->u2_ht)
2820                     end_of_tile = 1;
2821             }
2822             if((0 == end_of_slice_flag) && (0 == ps_codec->i4_slice_error) &&
2823                             ((ps_pps->i1_tiles_enabled_flag && end_of_tile) ||
2824                                             (ps_pps->i1_entropy_coding_sync_enabled_flag && end_of_tile_row)))
2825             {
2826                 WORD32 end_of_sub_stream_one_bit;
2827                 end_of_sub_stream_one_bit = ihevcd_cabac_decode_terminate(&ps_codec->s_parse.s_cabac, &ps_codec->s_parse.s_bitstrm);
2828                 AEV_TRACE("end_of_sub_stream_one_bit", end_of_sub_stream_one_bit, ps_codec->s_parse.s_cabac.u4_range);
2829 
2830                 /* TODO: Remove the check for offset when HM is updated to include a byte unconditionally even for aligned location */
2831                 /* For Ittiam streams this check should not be there, for HM9.1 streams this should be there */
2832                 if(ps_codec->s_parse.s_bitstrm.u4_bit_ofst % 8)
2833                     ihevcd_bits_flush_to_byte_boundary(&ps_codec->s_parse.s_bitstrm);
2834 
2835                 UNUSED(end_of_sub_stream_one_bit);
2836             }
2837         }
2838         {
2839             WORD32 ctb_indx;
2840 
2841             ctb_addr = ps_codec->s_parse.i4_ctb_y * num_ctb_in_row + ps_codec->s_parse.i4_ctb_x;
2842 
2843             ctb_indx = ++ctb_addr;
2844 
2845             /* Store pu_idx for next CTB in frame level pu_idx array */
2846 
2847             //In case of multiple tiles, if end-of-tile row is reached
2848             if((ps_tile->u2_wd == (ps_codec->s_parse.i4_ctb_tile_x + 1)) && (ps_tile->u2_wd != ps_sps->i2_pic_wd_in_ctb))
2849             {
2850                 ctb_indx = (ps_sps->i2_pic_wd_in_ctb * (ps_codec->s_parse.i4_ctb_tile_y + 1 + ps_tile->u1_pos_y)) + ps_tile->u1_pos_x; //idx is the beginning of next row in current tile.
2851                 if(ps_tile->u2_ht == (ps_codec->s_parse.i4_ctb_tile_y + 1))
2852                 {
2853                     //If the current ctb is the last tile's last ctb
2854                     if((ps_tile->u2_wd + ps_tile->u1_pos_x == ps_sps->i2_pic_wd_in_ctb) && ((ps_tile->u2_ht + ps_tile->u1_pos_y == ps_sps->i2_pic_ht_in_ctb)))
2855                     {
2856                         ctb_indx = ctb_addr; //Next continuous ctb address
2857                     }
2858                     else //Not last tile's end , but a tile end
2859                     {
2860                         tile_t *ps_next_tile = ps_codec->s_parse.ps_tile + 1;
2861                         ctb_indx = ps_next_tile->u1_pos_x + (ps_next_tile->u1_pos_y * ps_sps->i2_pic_wd_in_ctb); //idx is the beginning of first row in next tile.
2862                     }
2863                 }
2864             }
2865 
2866             ps_codec->s_parse.pu4_pic_pu_idx[ctb_indx] = ps_codec->s_parse.i4_pic_pu_idx;
2867             ps_codec->s_parse.i4_next_pu_ctb_cnt = ctb_indx;
2868 
2869             ps_codec->s_parse.pu1_pu_map += num_min4x4_in_ctb;
2870 
2871             /* Store tu_idx for next CTB in frame level tu_idx array */
2872             if(1 == ps_codec->i4_num_cores)
2873             {
2874                 ctb_indx = (0 == ctb_addr % RESET_TU_BUF_NCTB) ?
2875                                 RESET_TU_BUF_NCTB : ctb_addr % RESET_TU_BUF_NCTB;
2876 
2877                 //In case of multiple tiles, if end-of-tile row is reached
2878                 if((ps_tile->u2_wd == (ps_codec->s_parse.i4_ctb_tile_x + 1)) && (ps_tile->u2_wd != ps_sps->i2_pic_wd_in_ctb))
2879                 {
2880                     ctb_indx = (ps_sps->i2_pic_wd_in_ctb * (ps_codec->s_parse.i4_ctb_tile_y + 1 + ps_tile->u1_pos_y)) + ps_tile->u1_pos_x; //idx is the beginning of next row in current tile.
2881                     if(ps_tile->u2_ht == (ps_codec->s_parse.i4_ctb_tile_y + 1))
2882                     {
2883                         //If the current ctb is the last tile's last ctb
2884                         if((ps_tile->u2_wd + ps_tile->u1_pos_x == ps_sps->i2_pic_wd_in_ctb) && ((ps_tile->u2_ht + ps_tile->u1_pos_y == ps_sps->i2_pic_ht_in_ctb)))
2885                         {
2886                             ctb_indx = (0 == ctb_addr % RESET_TU_BUF_NCTB) ?
2887                                             RESET_TU_BUF_NCTB : ctb_addr % RESET_TU_BUF_NCTB;
2888                         }
2889                         else  //Not last tile's end , but a tile end
2890                         {
2891                             tile_t *ps_next_tile = ps_codec->s_parse.ps_tile + 1;
2892                             ctb_indx =  ps_next_tile->u1_pos_x + (ps_next_tile->u1_pos_y * ps_sps->i2_pic_wd_in_ctb); //idx is the beginning of first row in next tile.
2893                         }
2894                     }
2895                 }
2896                 ps_codec->s_parse.i4_next_tu_ctb_cnt = ctb_indx;
2897                 ps_codec->s_parse.pu4_pic_tu_idx[ctb_indx] = ps_codec->s_parse.i4_pic_tu_idx;
2898             }
2899             else
2900             {
2901                 ctb_indx = ctb_addr;
2902                 if((ps_tile->u2_wd == (ps_codec->s_parse.i4_ctb_tile_x + 1)) && (ps_tile->u2_wd != ps_sps->i2_pic_wd_in_ctb))
2903                 {
2904                     ctb_indx = (ps_sps->i2_pic_wd_in_ctb * (ps_codec->s_parse.i4_ctb_tile_y + 1 + ps_tile->u1_pos_y)) + ps_tile->u1_pos_x; //idx is the beginning of next row in current tile.
2905                     if(ps_tile->u2_ht == (ps_codec->s_parse.i4_ctb_tile_y + 1))
2906                     {
2907                         //If the current ctb is the last tile's last ctb
2908                         if((ps_tile->u2_wd + ps_tile->u1_pos_x == ps_sps->i2_pic_wd_in_ctb) && ((ps_tile->u2_ht + ps_tile->u1_pos_y == ps_sps->i2_pic_ht_in_ctb)))
2909                         {
2910                             ctb_indx = ctb_addr;
2911                         }
2912                         else  //Not last tile's end , but a tile end
2913                         {
2914                             tile_t *ps_next_tile = ps_codec->s_parse.ps_tile + 1;
2915                             ctb_indx =  ps_next_tile->u1_pos_x + (ps_next_tile->u1_pos_y * ps_sps->i2_pic_wd_in_ctb); //idx is the beginning of first row in next tile.
2916                         }
2917                     }
2918                 }
2919                 ps_codec->s_parse.i4_next_tu_ctb_cnt = ctb_indx;
2920                 ps_codec->s_parse.pu4_pic_tu_idx[ctb_indx] = ps_codec->s_parse.i4_pic_tu_idx;
2921             }
2922             ps_codec->s_parse.pu1_tu_map += num_min4x4_in_ctb;
2923         }
2924 
2925         /* QP array population has to be done if deblocking is enabled in the picture
2926          * but some of the slices in the pic have it disabled */
2927         if((0 != ps_codec->i4_disable_deblk_pic) &&
2928                 (1 == ps_slice_hdr->i1_slice_disable_deblocking_filter_flag))
2929         {
2930             bs_ctxt_t *ps_bs_ctxt = &ps_codec->s_parse.s_bs_ctxt;
2931             WORD32 log2_ctb_size = ps_sps->i1_log2_ctb_size;
2932             UWORD8 *pu1_qp;
2933             WORD32 qp_strd;
2934             WORD32 u4_qp_const_in_ctb;
2935             WORD32 cur_ctb_idx;
2936             WORD32 next_ctb_idx;
2937             WORD32 cur_tu_idx;
2938             WORD32 i4_ctb_tu_cnt;
2939             tu_t *ps_tu;
2940 
2941             cur_ctb_idx = ps_codec->s_parse.i4_ctb_x + ps_sps->i2_pic_wd_in_ctb * ps_codec->s_parse.i4_ctb_y;
2942             /* ctb_size/8 elements per CTB */
2943             qp_strd = ps_sps->i2_pic_wd_in_ctb << (log2_ctb_size - 3);
2944             pu1_qp = ps_bs_ctxt->pu1_pic_qp + ((ps_codec->s_parse.i4_ctb_x + ps_codec->s_parse.i4_ctb_y * qp_strd) << (log2_ctb_size - 3));
2945 
2946             u4_qp_const_in_ctb = ps_bs_ctxt->pu1_pic_qp_const_in_ctb[cur_ctb_idx >> 3] & (1 << (cur_ctb_idx & 7));
2947 
2948             next_ctb_idx = ps_codec->s_parse.i4_next_tu_ctb_cnt;
2949             if(1 == ps_codec->i4_num_cores)
2950             {
2951                 i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
2952                                 ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
2953 
2954                 cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
2955             }
2956             else
2957             {
2958                 i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
2959                                 ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
2960 
2961                 cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
2962             }
2963 
2964             ps_tu = &ps_codec->s_parse.ps_pic_tu[cur_tu_idx];
2965 
2966             if(u4_qp_const_in_ctb)
2967             {
2968                 pu1_qp[0] = ps_tu->b7_qp;
2969             }
2970             else
2971             {
2972                 for(i = 0; i < i4_ctb_tu_cnt; i++, ps_tu++)
2973                 {
2974                     WORD32 start_pos_x;
2975                     WORD32 start_pos_y;
2976                     WORD32 tu_size;
2977 
2978                     /* start_pos_x and start_pos_y are in units of min TU size (4x4) */
2979                     start_pos_x = ps_tu->b4_pos_x;
2980                     start_pos_y = ps_tu->b4_pos_y;
2981 
2982                     tu_size = 1 << (ps_tu->b3_size + 2);
2983                     tu_size >>= 2; /* TU size divided by 4 */
2984 
2985                     if(0 == (start_pos_x & 1) && 0 == (start_pos_y & 1))
2986                     {
2987                         WORD32 row, col;
2988                         for(row = start_pos_y; row < start_pos_y + tu_size; row += 2)
2989                         {
2990                             for(col = start_pos_x; col < start_pos_x + tu_size; col += 2)
2991                             {
2992                                 pu1_qp[(row >> 1) * qp_strd + (col >> 1)] = ps_tu->b7_qp;
2993                             }
2994                         }
2995                     }
2996                 }
2997             }
2998         }
2999 
3000         if(ps_codec->i4_num_cores <= MV_PRED_NUM_CORES_THRESHOLD)
3001         {
3002             /*************************************************/
3003             /****************   MV pred **********************/
3004             /*************************************************/
3005             WORD8 u1_top_ctb_avail = 1;
3006             WORD8 u1_left_ctb_avail = 1;
3007             WORD8 u1_top_lt_ctb_avail = 1;
3008             WORD8 u1_top_rt_ctb_avail = 1;
3009             WORD16 i2_wd_in_ctb;
3010 
3011             tile_start_ctb_idx = ps_tile->u1_pos_x
3012                             + ps_tile->u1_pos_y * (ps_sps->i2_pic_wd_in_ctb);
3013 
3014             slice_start_ctb_idx =  ps_slice_hdr->i2_ctb_x
3015                             + ps_slice_hdr->i2_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3016 
3017             if((slice_start_ctb_idx < tile_start_ctb_idx))
3018             {
3019                 //Slices span across multiple tiles.
3020                 i2_wd_in_ctb = ps_sps->i2_pic_wd_in_ctb;
3021             }
3022             else
3023             {
3024                 i2_wd_in_ctb = ps_tile->u2_wd;
3025             }
3026             /* slice and tile boundaries */
3027             if((0 == ps_codec->s_parse.i4_ctb_y) || (0 == ps_codec->s_parse.i4_ctb_tile_y))
3028             {
3029                 u1_top_ctb_avail = 0;
3030                 u1_top_lt_ctb_avail = 0;
3031                 u1_top_rt_ctb_avail = 0;
3032             }
3033 
3034             if((0 == ps_codec->s_parse.i4_ctb_x) || (0 == ps_codec->s_parse.i4_ctb_tile_x))
3035             {
3036                 u1_left_ctb_avail = 0;
3037                 u1_top_lt_ctb_avail = 0;
3038                 if((0 == ps_codec->s_parse.i4_ctb_slice_y) || (0 == ps_codec->s_parse.i4_ctb_tile_y))
3039                 {
3040                     u1_top_ctb_avail = 0;
3041                     if((i2_wd_in_ctb - 1) != ps_codec->s_parse.i4_ctb_slice_x) //TODO: For tile, not implemented
3042                     {
3043                         u1_top_rt_ctb_avail = 0;
3044                     }
3045                 }
3046             }
3047             /*For slices not beginning at start of a ctb row*/
3048             else if(ps_codec->s_parse.i4_ctb_x > 0)
3049             {
3050                 if((0 == ps_codec->s_parse.i4_ctb_slice_y) || (0 == ps_codec->s_parse.i4_ctb_tile_y))
3051                 {
3052                     u1_top_ctb_avail = 0;
3053                     u1_top_lt_ctb_avail = 0;
3054                     if(0 == ps_codec->s_parse.i4_ctb_slice_x)
3055                     {
3056                         u1_left_ctb_avail = 0;
3057                     }
3058                     if((i2_wd_in_ctb - 1) != ps_codec->s_parse.i4_ctb_slice_x)
3059                     {
3060                         u1_top_rt_ctb_avail = 0;
3061                     }
3062                 }
3063                 else if((1 == ps_codec->s_parse.i4_ctb_slice_y) && (0 == ps_codec->s_parse.i4_ctb_slice_x))
3064                 {
3065                     u1_top_lt_ctb_avail = 0;
3066                 }
3067             }
3068 
3069             if(((ps_sps->i2_pic_wd_in_ctb - 1) == ps_codec->s_parse.i4_ctb_x) || ((ps_tile->u2_wd - 1) == ps_codec->s_parse.i4_ctb_tile_x))
3070             {
3071                 u1_top_rt_ctb_avail = 0;
3072             }
3073 
3074             if(PSLICE == ps_slice_hdr->i1_slice_type
3075                             || BSLICE == ps_slice_hdr->i1_slice_type)
3076             {
3077                 mv_ctxt_t s_mv_ctxt;
3078                 process_ctxt_t *ps_proc;
3079                 UWORD32 *pu4_ctb_top_pu_idx;
3080                 UWORD32 *pu4_ctb_left_pu_idx;
3081                 UWORD32 *pu4_ctb_top_left_pu_idx;
3082                 WORD32 i4_ctb_pu_cnt;
3083                 WORD32 cur_ctb_idx;
3084                 WORD32 next_ctb_idx;
3085                 WORD32 cur_pu_idx;
3086                 ps_proc = &ps_codec->as_process[(ps_codec->i4_num_cores == 1) ? 1 : (ps_codec->i4_num_cores - 1)];
3087                 cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
3088                                 + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3089                 next_ctb_idx = ps_codec->s_parse.i4_next_pu_ctb_cnt;
3090                 i4_ctb_pu_cnt = ps_codec->s_parse.pu4_pic_pu_idx[next_ctb_idx]
3091                                 - ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3092 
3093                 cur_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3094 
3095                 pu4_ctb_top_pu_idx = ps_proc->pu4_pic_pu_idx_top
3096                                 + (ps_codec->s_parse.i4_ctb_x * ctb_size / MIN_PU_SIZE);
3097                 pu4_ctb_left_pu_idx = ps_proc->pu4_pic_pu_idx_left;
3098                 pu4_ctb_top_left_pu_idx = &ps_proc->u4_ctb_top_left_pu_idx;
3099 
3100                 /* Initializing s_mv_ctxt */
3101                 {
3102                     s_mv_ctxt.ps_pps = ps_pps;
3103                     s_mv_ctxt.ps_sps = ps_sps;
3104                     s_mv_ctxt.ps_slice_hdr = ps_slice_hdr;
3105                     s_mv_ctxt.i4_ctb_x = ps_codec->s_parse.i4_ctb_x;
3106                     s_mv_ctxt.i4_ctb_y = ps_codec->s_parse.i4_ctb_y;
3107                     s_mv_ctxt.ps_pu = &ps_codec->s_parse.ps_pic_pu[cur_pu_idx];
3108                     s_mv_ctxt.ps_pic_pu = ps_codec->s_parse.ps_pic_pu;
3109                     s_mv_ctxt.ps_tile = ps_tile;
3110                     s_mv_ctxt.pu4_pic_pu_idx_map = ps_proc->pu4_pic_pu_idx_map;
3111                     s_mv_ctxt.pu4_pic_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx;
3112                     s_mv_ctxt.pu1_pic_pu_map = ps_codec->s_parse.pu1_pic_pu_map;
3113                     s_mv_ctxt.i4_ctb_pu_cnt = i4_ctb_pu_cnt;
3114                     s_mv_ctxt.i4_ctb_start_pu_idx = cur_pu_idx;
3115                     s_mv_ctxt.u1_top_ctb_avail = u1_top_ctb_avail;
3116                     s_mv_ctxt.u1_top_rt_ctb_avail = u1_top_rt_ctb_avail;
3117                     s_mv_ctxt.u1_top_lt_ctb_avail = u1_top_lt_ctb_avail;
3118                     s_mv_ctxt.u1_left_ctb_avail = u1_left_ctb_avail;
3119                 }
3120 
3121                 ihevcd_get_mv_ctb(&s_mv_ctxt, pu4_ctb_top_pu_idx,
3122                                   pu4_ctb_left_pu_idx, pu4_ctb_top_left_pu_idx);
3123 
3124             }
3125             else
3126             {
3127                 WORD32 num_minpu_in_ctb = (ctb_size / MIN_PU_SIZE) * (ctb_size / MIN_PU_SIZE);
3128                 UWORD8 *pu1_pic_pu_map_ctb = ps_codec->s_parse.pu1_pic_pu_map +
3129                                 (ps_codec->s_parse.i4_ctb_x + ps_codec->s_parse.i4_ctb_y * ps_sps->i2_pic_wd_in_ctb) * num_minpu_in_ctb;
3130                 process_ctxt_t *ps_proc = &ps_codec->as_process[(ps_codec->i4_num_cores == 1) ? 1 : (ps_codec->i4_num_cores - 1)];
3131                 WORD32 row, col;
3132                 WORD32 pu_cnt;
3133                 WORD32 num_pu_per_ctb;
3134                 WORD32 cur_ctb_idx;
3135                 WORD32 next_ctb_idx;
3136                 WORD32 ctb_start_pu_idx;
3137                 UWORD32 *pu4_nbr_pu_idx = ps_proc->pu4_pic_pu_idx_map;
3138                 WORD32 nbr_pu_idx_strd = MAX_CTB_SIZE / MIN_PU_SIZE + 2;
3139                 pu_t *ps_pu;
3140                 WORD32 ctb_size_in_min_pu = (ctb_size / MIN_PU_SIZE);
3141 
3142                 /* Neighbor PU idx update inside CTB */
3143                 /* 1byte per 4x4. Indicates the PU idx that 4x4 block belongs to */
3144 
3145                 cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
3146                                 + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3147                 next_ctb_idx = ps_codec->s_parse.i4_next_pu_ctb_cnt;
3148                 num_pu_per_ctb = ps_codec->s_parse.pu4_pic_pu_idx[next_ctb_idx]
3149                                 - ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3150                 ctb_start_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3151                 ps_pu = &ps_codec->s_parse.ps_pic_pu[ctb_start_pu_idx];
3152 
3153                 for(pu_cnt = 0; pu_cnt < num_pu_per_ctb; pu_cnt++, ps_pu++)
3154                 {
3155                     UWORD32 cur_pu_idx;
3156                     WORD32 pu_ht = (ps_pu->b4_ht + 1) << 2;
3157                     WORD32 pu_wd = (ps_pu->b4_wd + 1) << 2;
3158 
3159                     cur_pu_idx = ctb_start_pu_idx + pu_cnt;
3160 
3161                     for(row = 0; row < pu_ht / MIN_PU_SIZE; row++)
3162                         for(col = 0; col < pu_wd / MIN_PU_SIZE; col++)
3163                             pu4_nbr_pu_idx[(1 + ps_pu->b4_pos_x + col)
3164                                             + (1 + ps_pu->b4_pos_y + row)
3165                                             * nbr_pu_idx_strd] =
3166                                             cur_pu_idx;
3167                 }
3168 
3169                 /* Updating Top and Left pointers */
3170                 {
3171                     WORD32 rows_remaining = ps_sps->i2_pic_height_in_luma_samples
3172                                     - (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size);
3173                     WORD32 ctb_size_left = MIN(ctb_size, rows_remaining);
3174 
3175                     /* Top Left */
3176                     /* saving top left before updating top ptr, as updating top ptr will overwrite the top left for the next ctb */
3177                     ps_proc->u4_ctb_top_left_pu_idx = ps_proc->pu4_pic_pu_idx_top[(ps_codec->s_parse.i4_ctb_x * ctb_size / MIN_PU_SIZE) + ctb_size / MIN_PU_SIZE - 1];
3178                     for(i = 0; i < ctb_size / MIN_PU_SIZE; i++)
3179                     {
3180                         /* Left */
3181                         /* Last column of au4_nbr_pu_idx */
3182                         ps_proc->pu4_pic_pu_idx_left[i] = pu4_nbr_pu_idx[(ctb_size / MIN_PU_SIZE)
3183                                         + (i + 1) * nbr_pu_idx_strd];
3184                         /* Top */
3185                         /* Last row of au4_nbr_pu_idx */
3186                         ps_proc->pu4_pic_pu_idx_top[(ps_codec->s_parse.i4_ctb_x * ctb_size / MIN_PU_SIZE) + i] =
3187                                         pu4_nbr_pu_idx[(ctb_size_left / MIN_PU_SIZE) * nbr_pu_idx_strd + i + 1];
3188 
3189                     }
3190                 }
3191 
3192                 /* Updating the CTB level PU idx (Used for collocated MV pred)*/
3193                 {
3194                     WORD32 ctb_row, ctb_col, index_pic_map, index_nbr_map;
3195                     WORD32 first_pu_of_ctb;
3196                     first_pu_of_ctb = pu4_nbr_pu_idx[1 + nbr_pu_idx_strd];
3197                     UWORD32 cur_ctb_ht_in_min_pu = MIN(((ps_sps->i2_pic_height_in_luma_samples
3198                             - (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size)) / MIN_PU_SIZE), ctb_size_in_min_pu);
3199                     UWORD32 cur_ctb_wd_in_min_pu = MIN(((ps_sps->i2_pic_width_in_luma_samples
3200                                 - (ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size)) / MIN_PU_SIZE), ctb_size_in_min_pu);
3201 
3202                     index_pic_map = 0 * ctb_size_in_min_pu + 0;
3203                     index_nbr_map = (0 + 1) * nbr_pu_idx_strd + (0 + 1);
3204 
3205                     for(ctb_row = 0; ctb_row < cur_ctb_ht_in_min_pu; ctb_row++)
3206                     {
3207                         for(ctb_col = 0; ctb_col < cur_ctb_wd_in_min_pu; ctb_col++)
3208                         {
3209                             pu1_pic_pu_map_ctb[index_pic_map + ctb_col] = pu4_nbr_pu_idx[index_nbr_map + ctb_col]
3210                                             - first_pu_of_ctb;
3211                         }
3212                         index_pic_map += ctb_size_in_min_pu;
3213                         index_nbr_map += nbr_pu_idx_strd;
3214                     }
3215                 }
3216             }
3217 
3218             /*************************************************/
3219             /******************  BS, QP  *********************/
3220             /*************************************************/
3221             /* Check if deblock is disabled for the current slice or if it is disabled for the current picture
3222              * because of disable deblock api
3223              */
3224             if(0 == ps_codec->i4_disable_deblk_pic)
3225             {
3226                 /* Boundary strength calculation is done irrespective of whether deblocking is disabled
3227                  * in the slice or not, to handle deblocking slice boundaries */
3228                 if((0 == ps_codec->i4_slice_error))
3229                 {
3230                     WORD32 i4_ctb_tu_cnt;
3231                     WORD32 cur_ctb_idx, next_ctb_idx;
3232                     WORD32 cur_pu_idx;
3233                     WORD32 cur_tu_idx;
3234                     process_ctxt_t *ps_proc;
3235 
3236                     ps_proc = &ps_codec->as_process[(ps_codec->i4_num_cores == 1) ? 1 : (ps_codec->i4_num_cores - 1)];
3237                     cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
3238                                     + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3239 
3240                     cur_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3241                     next_ctb_idx = ps_codec->s_parse.i4_next_tu_ctb_cnt;
3242                     if(1 == ps_codec->i4_num_cores)
3243                     {
3244                         i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
3245                                         ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
3246 
3247                         cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
3248                     }
3249                     else
3250                     {
3251                         i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
3252                                         ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
3253 
3254                         cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
3255                     }
3256 
3257                     ps_codec->s_parse.s_bs_ctxt.ps_pps = ps_codec->s_parse.ps_pps;
3258                     ps_codec->s_parse.s_bs_ctxt.ps_sps = ps_codec->s_parse.ps_sps;
3259                     ps_codec->s_parse.s_bs_ctxt.ps_codec = ps_codec;
3260                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_tu_cnt = i4_ctb_tu_cnt;
3261                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_x = ps_codec->s_parse.i4_ctb_x;
3262                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_y = ps_codec->s_parse.i4_ctb_y;
3263                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_tile_x = ps_codec->s_parse.i4_ctb_tile_x;
3264                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_tile_y = ps_codec->s_parse.i4_ctb_tile_y;
3265                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_slice_x = ps_codec->s_parse.i4_ctb_slice_x;
3266                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_slice_y = ps_codec->s_parse.i4_ctb_slice_y;
3267                     ps_codec->s_parse.s_bs_ctxt.ps_tu = &ps_codec->s_parse.ps_pic_tu[cur_tu_idx];
3268                     ps_codec->s_parse.s_bs_ctxt.ps_pu = &ps_codec->s_parse.ps_pic_pu[cur_pu_idx];
3269                     ps_codec->s_parse.s_bs_ctxt.pu4_pic_pu_idx_map = ps_proc->pu4_pic_pu_idx_map;
3270                     ps_codec->s_parse.s_bs_ctxt.i4_next_pu_ctb_cnt = ps_codec->s_parse.i4_next_pu_ctb_cnt;
3271                     ps_codec->s_parse.s_bs_ctxt.i4_next_tu_ctb_cnt = ps_codec->s_parse.i4_next_tu_ctb_cnt;
3272                     ps_codec->s_parse.s_bs_ctxt.pu1_slice_idx = ps_codec->s_parse.pu1_slice_idx;
3273                     ps_codec->s_parse.s_bs_ctxt.ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
3274                     ps_codec->s_parse.s_bs_ctxt.ps_tile = ps_codec->s_parse.ps_tile;
3275 
3276                     if(ISLICE == ps_slice_hdr->i1_slice_type)
3277                     {
3278                         ihevcd_ctb_boundary_strength_islice(&ps_codec->s_parse.s_bs_ctxt);
3279                     }
3280                     else
3281                     {
3282                         ihevcd_ctb_boundary_strength_pbslice(&ps_codec->s_parse.s_bs_ctxt);
3283                     }
3284                 }
3285 
3286                 /* Boundary strength is set to zero if deblocking is disabled for the current slice */
3287                 if(0 != ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
3288                 {
3289                     WORD32 bs_strd = (ps_sps->i2_pic_wd_in_ctb + 1) * (ctb_size * ctb_size / 8 / 16);
3290 
3291                     UWORD32 *pu4_vert_bs = (UWORD32 *)((UWORD8 *)ps_codec->s_parse.s_bs_ctxt.pu4_pic_vert_bs +
3292                                     ps_codec->s_parse.i4_ctb_x * (ctb_size * ctb_size / 8 / 16) +
3293                                     ps_codec->s_parse.i4_ctb_y * bs_strd);
3294                     UWORD32 *pu4_horz_bs = (UWORD32 *)((UWORD8 *)ps_codec->s_parse.s_bs_ctxt.pu4_pic_horz_bs +
3295                                     ps_codec->s_parse.i4_ctb_x * (ctb_size * ctb_size / 8 / 16) +
3296                                     ps_codec->s_parse.i4_ctb_y * bs_strd);
3297 
3298                     memset(pu4_vert_bs, 0, (ctb_size / 8) * (ctb_size / 4) / 8 * 2);
3299                     memset(pu4_horz_bs, 0, (ctb_size / 8) * (ctb_size / 4) / 8 * 2);
3300                 }
3301             }
3302 
3303         }
3304 
3305         DATA_SYNC();
3306 
3307         /* Update the parse status map */
3308         {
3309             sps_t *ps_sps = ps_codec->s_parse.ps_sps;
3310             UWORD8 *pu1_buf;
3311             WORD32 idx;
3312             idx = (ps_codec->s_parse.i4_ctb_x);
3313             idx += ((ps_codec->s_parse.i4_ctb_y) * ps_sps->i2_pic_wd_in_ctb);
3314             pu1_buf = (ps_codec->pu1_parse_map + idx);
3315             *pu1_buf = 1;
3316         }
3317 
3318         /* Increment CTB x and y positions */
3319         ps_codec->s_parse.i4_ctb_tile_x++;
3320         ps_codec->s_parse.i4_ctb_x++;
3321         ps_codec->s_parse.i4_ctb_slice_x++;
3322 
3323         /*If tiles are enabled, handle the slice counters differently*/
3324         if(ps_pps->i1_tiles_enabled_flag)
3325         {
3326             //Indicates multiple tiles in a slice case
3327             tile_start_ctb_idx = ps_tile->u1_pos_x
3328                             + ps_tile->u1_pos_y * (ps_sps->i2_pic_wd_in_ctb);
3329 
3330             slice_start_ctb_idx =  ps_slice_hdr->i2_ctb_x
3331                             + ps_slice_hdr->i2_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3332 
3333             if((slice_start_ctb_idx < tile_start_ctb_idx))
3334             {
3335                 if(ps_codec->s_parse.i4_ctb_slice_x == (ps_tile->u1_pos_x + ps_tile->u2_wd))
3336                 {
3337                     /* Reached end of slice row within a tile /frame */
3338                     ps_codec->s_parse.i4_ctb_slice_y++;
3339                     ps_codec->s_parse.i4_ctb_slice_x = ps_tile->u1_pos_x; //todo:Check
3340                 }
3341             }
3342             //Indicates multiple slices in a tile case - hence, reset slice_x
3343             else if(ps_codec->s_parse.i4_ctb_slice_x == (ps_tile->u2_wd))
3344             {
3345                 ps_codec->s_parse.i4_ctb_slice_y++;
3346                 ps_codec->s_parse.i4_ctb_slice_x = 0;
3347             }
3348         }
3349         else
3350         {
3351             if(ps_codec->s_parse.i4_ctb_slice_x == ps_tile->u2_wd)
3352             {
3353                 /* Reached end of slice row within a tile /frame */
3354                 ps_codec->s_parse.i4_ctb_slice_y++;
3355                 ps_codec->s_parse.i4_ctb_slice_x = 0;
3356             }
3357         }
3358 
3359 
3360         if(ps_codec->s_parse.i4_ctb_tile_x == (ps_tile->u2_wd))
3361         {
3362             /* Reached end of tile row */
3363             ps_codec->s_parse.i4_ctb_tile_x = 0;
3364             ps_codec->s_parse.i4_ctb_x = ps_tile->u1_pos_x;
3365 
3366             ps_codec->s_parse.i4_ctb_tile_y++;
3367             ps_codec->s_parse.i4_ctb_y++;
3368 
3369             if(ps_codec->s_parse.i4_ctb_tile_y == (ps_tile->u2_ht))
3370             {
3371                 /* Reached End of Tile */
3372                 ps_codec->s_parse.i4_ctb_tile_y = 0;
3373                 ps_codec->s_parse.i4_ctb_tile_x = 0;
3374                 ps_codec->s_parse.ps_tile++;
3375 
3376                 if((ps_tile->u2_ht + ps_tile->u1_pos_y  ==  ps_sps->i2_pic_ht_in_ctb) && (ps_tile->u2_wd + ps_tile->u1_pos_x  ==  ps_sps->i2_pic_wd_in_ctb))
3377                 {
3378                     /* Reached end of frame */
3379                     end_of_pic = 1;
3380                     ps_codec->s_parse.i4_ctb_x = 0;
3381                     ps_codec->s_parse.i4_ctb_y = ps_sps->i2_pic_ht_in_ctb;
3382                 }
3383                 else
3384                 {
3385                     /* Initialize ctb_x and ctb_y to start of next tile */
3386                     ps_tile = ps_codec->s_parse.ps_tile;
3387                     ps_codec->s_parse.i4_ctb_x = ps_tile->u1_pos_x;
3388                     ps_codec->s_parse.i4_ctb_y = ps_tile->u1_pos_y;
3389                     ps_codec->s_parse.i4_ctb_tile_y = 0;
3390                     ps_codec->s_parse.i4_ctb_tile_x = 0;
3391                     ps_codec->s_parse.i4_ctb_slice_x = ps_tile->u1_pos_x;
3392                     ps_codec->s_parse.i4_ctb_slice_y = ps_tile->u1_pos_y;
3393 
3394                 }
3395             }
3396 
3397         }
3398 
3399         ps_codec->s_parse.i4_next_ctb_indx = ps_codec->s_parse.i4_ctb_x +
3400                         ps_codec->s_parse.i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
3401 
3402         /* If the current slice is in error, check if the next slice's address
3403          * is reached and mark the end_of_slice flag */
3404         if(ps_codec->i4_slice_error)
3405         {
3406             slice_header_t *ps_slice_hdr_next = ps_slice_hdr + 1;
3407             WORD32 next_slice_addr = ps_slice_hdr_next->i2_ctb_x +
3408                             ps_slice_hdr_next->i2_ctb_y * ps_sps->i2_pic_wd_in_ctb;
3409 
3410             if(ps_codec->s_parse.i4_next_ctb_indx == next_slice_addr)
3411                 end_of_slice_flag = 1;
3412         }
3413 
3414         /* If the codec is running in single core mode
3415          * then call process function for current CTB
3416          */
3417         if((1 == ps_codec->i4_num_cores) && (ps_codec->s_parse.i4_ctb_tile_x == 0))
3418         {
3419             process_ctxt_t *ps_proc = &ps_codec->as_process[0];
3420 //          ps_proc->i4_ctb_cnt = ihevcd_nctb_cnt(ps_codec, ps_sps);
3421             ps_proc->i4_ctb_cnt = ps_proc->ps_tile->u2_wd;
3422             ihevcd_process(ps_proc);
3423         }
3424 
3425         /* If the bytes for the current slice are exhausted
3426          * set end_of_slice flag to 1
3427          * This slice will be treated as incomplete */
3428         if((UWORD8 *)ps_codec->s_parse.s_bitstrm.pu1_buf_max + BITSTRM_OFF_THRS <
3429                                         ((UWORD8 *)ps_codec->s_parse.s_bitstrm.pu4_buf + (ps_codec->s_parse.s_bitstrm.u4_bit_ofst / 8)))
3430         {
3431             // end_of_slice_flag = ps_codec->i4_slice_error ? 0 : 1;
3432 
3433             if(0 == ps_codec->i4_slice_error)
3434                 end_of_slice_flag = 1;
3435         }
3436 
3437 
3438         if(end_of_pic)
3439             break;
3440     } while(!end_of_slice_flag);
3441 
3442     /* Reset slice error */
3443     ps_codec->i4_slice_error = 0;
3444 
3445     /* Increment the slice index for parsing next slice */
3446     if(0 == end_of_pic)
3447     {
3448         while(1)
3449         {
3450 
3451             WORD32 parse_slice_idx;
3452             parse_slice_idx = ps_codec->s_parse.i4_cur_slice_idx;
3453             parse_slice_idx++;
3454 
3455             {
3456                 /* If the next slice header is not initialized, update cur_slice_idx and break */
3457                 if((1 == ps_codec->i4_num_cores) || (0 != (parse_slice_idx & (MAX_SLICE_HDR_CNT - 1))))
3458                 {
3459                     ps_codec->s_parse.i4_cur_slice_idx = parse_slice_idx;
3460                     break;
3461                 }
3462 
3463                 /* If the next slice header is initialised, wait for the parsed slices to be processed */
3464                 else
3465                 {
3466                     WORD32 ctb_indx = 0;
3467 
3468                     while(ctb_indx != ps_sps->i4_pic_size_in_ctb)
3469                     {
3470                         WORD32 parse_status = *(ps_codec->pu1_parse_map + ctb_indx);
3471                         volatile WORD32 proc_status = *(ps_codec->pu1_proc_map + ctb_indx) & 1;
3472 
3473                         if(parse_status == proc_status)
3474                             ctb_indx++;
3475                     }
3476                     ps_codec->s_parse.i4_cur_slice_idx = parse_slice_idx;
3477                     break;
3478                 }
3479 
3480             }
3481         }
3482 
3483     }
3484     else
3485     {
3486 #if FRAME_ILF_PAD
3487         if(FRAME_ILF_PAD && 1 == ps_codec->i4_num_cores)
3488         {
3489             if(ps_slice_hdr->i4_abs_pic_order_cnt == 0)
3490             {
3491                 DUMP_PRE_ILF(ps_codec->as_process[0].pu1_cur_pic_luma,
3492                              ps_codec->as_process[0].pu1_cur_pic_chroma,
3493                              ps_sps->i2_pic_width_in_luma_samples,
3494                              ps_sps->i2_pic_height_in_luma_samples,
3495                              ps_codec->i4_strd);
3496 
3497                 DUMP_BS(ps_codec->as_process[0].s_bs_ctxt.pu4_pic_vert_bs,
3498                         ps_codec->as_process[0].s_bs_ctxt.pu4_pic_horz_bs,
3499                         ps_sps->i2_pic_wd_in_ctb * (ctb_size * ctb_size / 8 / 16) * ps_sps->i2_pic_ht_in_ctb,
3500                         (ps_sps->i2_pic_wd_in_ctb + 1) * (ctb_size * ctb_size / 8 / 16) * ps_sps->i2_pic_ht_in_ctb);
3501 
3502                 DUMP_QP(ps_codec->as_process[0].s_bs_ctxt.pu1_pic_qp,
3503                         (ps_sps->i2_pic_height_in_luma_samples * ps_sps->i2_pic_width_in_luma_samples) / (MIN_CU_SIZE * MIN_CU_SIZE));
3504 
3505                 DUMP_QP_CONST_IN_CTB(ps_codec->as_process[0].s_bs_ctxt.pu1_pic_qp_const_in_ctb,
3506                                      (ps_sps->i2_pic_height_in_luma_samples * ps_sps->i2_pic_width_in_luma_samples) / (MIN_CTB_SIZE * MIN_CTB_SIZE) / 8);
3507 
3508                 DUMP_NO_LOOP_FILTER(ps_codec->as_process[0].pu1_pic_no_loop_filter_flag,
3509                                     (ps_sps->i2_pic_width_in_luma_samples / MIN_CU_SIZE) * (ps_sps->i2_pic_height_in_luma_samples / MIN_CU_SIZE) / 8);
3510 
3511                 DUMP_OFFSETS(ps_slice_hdr->i1_beta_offset_div2,
3512                              ps_slice_hdr->i1_tc_offset_div2,
3513                              ps_pps->i1_pic_cb_qp_offset,
3514                              ps_pps->i1_pic_cr_qp_offset);
3515             }
3516             ps_codec->s_parse.s_deblk_ctxt.ps_pps = ps_codec->s_parse.ps_pps;
3517             ps_codec->s_parse.s_deblk_ctxt.ps_sps = ps_codec->s_parse.ps_sps;
3518             ps_codec->s_parse.s_deblk_ctxt.ps_codec = ps_codec;
3519             ps_codec->s_parse.s_deblk_ctxt.ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
3520             ps_codec->s_parse.s_deblk_ctxt.is_chroma_yuv420sp_vu = (ps_codec->e_ref_chroma_fmt == IV_YUV_420SP_VU);
3521 
3522             ps_codec->s_parse.s_sao_ctxt.ps_pps = ps_codec->s_parse.ps_pps;
3523             ps_codec->s_parse.s_sao_ctxt.ps_sps = ps_codec->s_parse.ps_sps;
3524             ps_codec->s_parse.s_sao_ctxt.ps_codec = ps_codec;
3525             ps_codec->s_parse.s_sao_ctxt.ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
3526 
3527             ihevcd_ilf_pad_frame(&ps_codec->s_parse.s_deblk_ctxt, &ps_codec->s_parse.s_sao_ctxt);
3528 
3529         }
3530 #endif
3531         ps_codec->s_parse.i4_end_of_frame = 1;
3532     }
3533     return ret;
3534 }
3535 
3536 
3537 
3538 
3539 
3540 
3541 
3542 
3543