• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         /* Set current group QP if current CU is aligned with the group */
1855         {
1856             WORD32 cu_pos_x = ps_codec->s_parse.s_cu.i4_pos_x << 3;
1857             WORD32 cu_pos_y = ps_codec->s_parse.s_cu.i4_pos_y << 3;
1858 
1859             WORD32 qpg_x = (cu_pos_x - (cu_pos_x & ((1 << ps_pps->i1_log2_min_cu_qp_delta_size) - 1)));
1860             WORD32 qpg_y = (cu_pos_y - (cu_pos_y & ((1 << ps_pps->i1_log2_min_cu_qp_delta_size) - 1)));
1861 
1862             if((cu_pos_x == qpg_x) &&
1863                             (cu_pos_y == qpg_y))
1864             {
1865                 ps_codec->s_parse.u4_qpg = ps_codec->s_parse.u4_qp;
1866 
1867                 ps_codec->s_parse.s_cu.i4_cu_qp_delta = 0;
1868 
1869             }
1870         }
1871 
1872         ret = ihevcd_parse_coding_unit(ps_codec, x0, y0, log2_cb_size);
1873         RETURN_IF((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret, ret);
1874 
1875         if(ps_pps->i1_cu_qp_delta_enabled_flag)
1876         {
1877             WORD32 qp_pred, qp_left, qp_top;
1878             WORD32 cu_pos_x;
1879             WORD32 cu_pos_y;
1880             WORD32 qpg_x;
1881             WORD32 qpg_y;
1882             WORD32 i, j;
1883             WORD32 qp;
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 
1946     }
1947 
1948 
1949 
1950 
1951     return ret;
1952 }
1953 
1954 
1955 /**
1956  *******************************************************************************
1957  *
1958  * @brief
1959  *  Parses SAO (Sample adaptive offset syntax)
1960  *
1961  * @par Description:
1962  *  Parses SAO (Sample adaptive offset syntax) as per  Section:7.3.9.3
1963  *
1964  * @param[in] ps_codec
1965  *  Pointer to codec context
1966  *
1967  * @returns  Error from IHEVCD_ERROR_T
1968  *
1969  * @remarks
1970  *
1971  *
1972  *******************************************************************************
1973  */
ihevcd_parse_sao(codec_t * ps_codec)1974 IHEVCD_ERROR_T  ihevcd_parse_sao(codec_t *ps_codec)
1975 {
1976     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1977     sps_t *ps_sps;
1978     sao_t *ps_sao;
1979     WORD32 rx;
1980     WORD32 ry;
1981     WORD32 value;
1982     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1983     WORD32 sao_merge_left_flag;
1984     WORD32 sao_merge_up_flag;
1985     slice_header_t *ps_slice_hdr;
1986     cab_ctxt_t *ps_cabac = &ps_codec->s_parse.s_cabac;
1987     WORD32 ctxt_idx;
1988 
1989     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base;
1990     ps_slice_hdr += (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1));
1991 
1992     ps_sps = (ps_codec->s_parse.ps_sps);
1993     rx = ps_codec->s_parse.i4_ctb_x;
1994     ry = ps_codec->s_parse.i4_ctb_y;
1995 
1996     ps_sao = ps_codec->s_parse.ps_pic_sao + rx + ry * ps_sps->i2_pic_wd_in_ctb;
1997 
1998     /* Default values */
1999     ps_sao->b3_y_type_idx = 0;
2000     ps_sao->b3_cb_type_idx = 0;
2001     ps_sao->b3_cr_type_idx = 0;
2002 
2003     UNUSED(value);
2004     ctxt_idx = IHEVC_CAB_SAO_MERGE;
2005     sao_merge_left_flag = 0;
2006     sao_merge_up_flag = 0;
2007     if(rx > 0)
2008     {
2009         /*TODO:Implemented only for slice. condition for tile is not tested*/
2010         if(((0 != ps_codec->s_parse.i4_ctb_slice_x) || (0 != ps_codec->s_parse.i4_ctb_slice_y)) &&
2011                         (0 != ps_codec->s_parse.i4_ctb_tile_x))
2012         {
2013 
2014             TRACE_CABAC_CTXT("sao_merge_flag", ps_cabac->u4_range, ctxt_idx);
2015             sao_merge_left_flag = ihevcd_cabac_decode_bin(ps_cabac,
2016                                                           ps_bitstrm,
2017                                                           ctxt_idx);
2018             AEV_TRACE("sao_merge_flag", sao_merge_left_flag, ps_cabac->u4_range);
2019         }
2020 
2021     }
2022     if(ry > 0 && !sao_merge_left_flag)
2023     {
2024         if((ps_codec->s_parse.i4_ctb_slice_y > 0) && (ps_codec->s_parse.i4_ctb_tile_y > 0))
2025         {
2026             TRACE_CABAC_CTXT("sao_merge_flag", ps_cabac->u4_range, ctxt_idx);
2027             sao_merge_up_flag = ihevcd_cabac_decode_bin(ps_cabac,
2028                                                         ps_bitstrm,
2029                                                         ctxt_idx);
2030             AEV_TRACE("sao_merge_flag", sao_merge_up_flag, ps_cabac->u4_range);
2031         }
2032     }
2033     ctxt_idx = IHEVC_CAB_SAO_TYPE;
2034 
2035     if(sao_merge_left_flag)
2036     {
2037         *ps_sao = *(ps_sao - 1);
2038     }
2039     else if(sao_merge_up_flag)
2040     {
2041         *ps_sao = *(ps_sao - ps_sps->i2_pic_wd_in_ctb);
2042     }
2043     else // if(!sao_merge_up_flag && !sao_merge_left_flag)
2044     {
2045         WORD32 c_idx;
2046         WORD32 sao_type_idx = 0;
2047         for(c_idx = 0; c_idx < 3; c_idx++)
2048         {
2049             if((ps_slice_hdr->i1_slice_sao_luma_flag && c_idx == 0) || (ps_slice_hdr->i1_slice_sao_chroma_flag && c_idx > 0))
2050             {
2051 
2052 
2053                 /* sao_type_idx will be same for c_idx == 1 and c_idx == 2 - hence not initialized to zero for c_idx == 2*/
2054 
2055                 if(c_idx == 0)
2056                 {
2057                     sao_type_idx = 0;
2058                     TRACE_CABAC_CTXT("sao_type_idx", ps_cabac->u4_range, ctxt_idx);
2059                     sao_type_idx = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
2060 
2061                     if(sao_type_idx)
2062                     {
2063                         sao_type_idx += ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
2064                     }
2065                     AEV_TRACE("sao_type_idx", sao_type_idx,  ps_cabac->u4_range);
2066 
2067                     ps_sao->b3_y_type_idx = sao_type_idx;
2068                 }
2069                 if(c_idx == 1)
2070                 {
2071                     sao_type_idx = 0;
2072                     TRACE_CABAC_CTXT("sao_type_idx", ps_cabac->u4_range, ctxt_idx);
2073                     sao_type_idx = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_idx);
2074                     if(sao_type_idx)
2075                     {
2076                         sao_type_idx += ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
2077                     }
2078 
2079                     AEV_TRACE("sao_type_idx", sao_type_idx,  ps_cabac->u4_range);
2080 
2081                     ps_sao->b3_cb_type_idx = sao_type_idx;
2082                     ps_sao->b3_cr_type_idx = sao_type_idx;
2083                 }
2084 
2085                 if(sao_type_idx != 0)
2086                 {
2087                     WORD32 i;
2088                     WORD32 sao_offset[4];
2089                     WORD32 sao_band_position = 0;
2090                     WORD32 c_max =  (1 << (MIN(BIT_DEPTH, 10) - 5)) - 1;
2091                     for(i = 0; i < 4; i++)
2092                     {
2093                         sao_offset[i] = ihevcd_cabac_decode_bypass_bins_tunary(ps_cabac, ps_bitstrm, c_max);
2094                         AEV_TRACE("sao_offset_abs", sao_offset[i], ps_cabac->u4_range);
2095 
2096                         if((2 == sao_type_idx) && (i > 1))
2097                         {
2098                             sao_offset[i] = -sao_offset[i];
2099                         }
2100                     }
2101 
2102                     if(sao_type_idx == 1)
2103                     {
2104                         for(i = 0; i < 4; i++)
2105                         {
2106                             if(sao_offset[i] != 0)
2107                             {
2108                                 value = ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
2109                                 AEV_TRACE("sao_offset_sign", value, ps_cabac->u4_range);
2110 
2111                                 if(value)
2112                                 {
2113                                     sao_offset[i] = -sao_offset[i];
2114                                 }
2115                             }
2116                         }
2117                         value = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, 5);
2118                         AEV_TRACE("sao_band_position", value, ps_cabac->u4_range);
2119 
2120                         sao_band_position = value;
2121                     }
2122                     else
2123                     {
2124                         if(c_idx == 0)
2125                         {
2126                             value = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, 2);
2127                             AEV_TRACE("sao_eo_class", value, ps_cabac->u4_range);
2128 
2129                             ps_sao->b3_y_type_idx += value;
2130                         }
2131 
2132                         if(c_idx == 1)
2133                         {
2134                             value = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, 2);
2135                             AEV_TRACE("sao_eo_class", value, ps_cabac->u4_range);
2136 
2137                             ps_sao->b3_cb_type_idx += value;
2138                             ps_sao->b3_cr_type_idx += value;
2139                         }
2140                     }
2141 
2142                     if(0 == c_idx)
2143                     {
2144                         ps_sao->b4_y_offset_1 = sao_offset[0];
2145                         ps_sao->b4_y_offset_2 = sao_offset[1];
2146                         ps_sao->b4_y_offset_3 = sao_offset[2];
2147                         ps_sao->b4_y_offset_4 = sao_offset[3];
2148 
2149                         ps_sao->b5_y_band_pos = sao_band_position;
2150                     }
2151                     else if(1 == c_idx)
2152                     {
2153                         ps_sao->b4_cb_offset_1 = sao_offset[0];
2154                         ps_sao->b4_cb_offset_2 = sao_offset[1];
2155                         ps_sao->b4_cb_offset_3 = sao_offset[2];
2156                         ps_sao->b4_cb_offset_4 = sao_offset[3];
2157 
2158                         ps_sao->b5_cb_band_pos = sao_band_position;
2159                     }
2160                     else // 2 == c_idx
2161                     {
2162                         ps_sao->b4_cr_offset_1 = sao_offset[0];
2163                         ps_sao->b4_cr_offset_2 = sao_offset[1];
2164                         ps_sao->b4_cr_offset_3 = sao_offset[2];
2165                         ps_sao->b4_cr_offset_4 = sao_offset[3];
2166 
2167                         ps_sao->b5_cr_band_pos = sao_band_position;
2168                     }
2169                 }
2170             }
2171         }
2172     }
2173 
2174     return ret;
2175 }
2176 /**
2177  *******************************************************************************
2178  *
2179  * @brief
2180  *  Parses Slice data syntax
2181  *
2182  * @par Description:
2183  *  Parses Slice data syntax as per Section:7.3.9.1
2184  *
2185  * @param[in] ps_codec
2186  *  Pointer to codec context
2187  *
2188  * @returns  Error from IHEVCD_ERROR_T
2189  *
2190  * @remarks
2191  *
2192  *
2193  *******************************************************************************
2194  */
ihevcd_parse_slice_data(codec_t * ps_codec)2195 IHEVCD_ERROR_T ihevcd_parse_slice_data(codec_t *ps_codec)
2196 {
2197 
2198     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2199     WORD32 end_of_slice_flag = 0;
2200     sps_t *ps_sps;
2201     pps_t *ps_pps;
2202     slice_header_t *ps_slice_hdr;
2203     WORD32 end_of_pic;
2204     tile_t *ps_tile, *ps_tile_prev;
2205     WORD32 i;
2206     WORD32 ctb_addr;
2207     WORD32 tile_idx;
2208     WORD32 cabac_init_idc;
2209     WORD32 ctb_size;
2210     WORD32 num_ctb_in_row;
2211     WORD32 num_min4x4_in_ctb;
2212     WORD32 slice_qp;
2213     WORD32 slice_start_ctb_idx;
2214     WORD32 tile_start_ctb_idx;
2215 
2216 
2217     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base;
2218     ps_pps = ps_codec->s_parse.ps_pps_base;
2219     ps_sps = ps_codec->s_parse.ps_sps_base;
2220 
2221     /* Get current slice header, pps and sps */
2222     ps_slice_hdr += (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1));
2223     ps_pps  += ps_slice_hdr->i1_pps_id;
2224     ps_sps  += ps_pps->i1_sps_id;
2225 
2226     if(0 != ps_codec->s_parse.i4_cur_slice_idx)
2227     {
2228         if(!ps_slice_hdr->i1_dependent_slice_flag)
2229         {
2230             ps_codec->s_parse.i4_cur_independent_slice_idx =
2231                     ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1);
2232         }
2233     }
2234 
2235 
2236     ctb_size = 1 << ps_sps->i1_log2_ctb_size;
2237     num_min4x4_in_ctb = (ctb_size / 4) * (ctb_size / 4);
2238     num_ctb_in_row = ps_sps->i2_pic_wd_in_ctb;
2239 
2240     /* Update the parse context */
2241     if(0 == ps_codec->i4_slice_error)
2242     {
2243         ps_codec->s_parse.i4_ctb_x = ps_slice_hdr->i2_ctb_x;
2244         ps_codec->s_parse.i4_ctb_y = ps_slice_hdr->i2_ctb_y;
2245     }
2246     ps_codec->s_parse.ps_pps = ps_pps;
2247     ps_codec->s_parse.ps_sps = ps_sps;
2248     ps_codec->s_parse.ps_slice_hdr = ps_slice_hdr;
2249 
2250     /* Derive Tile positions for the current CTB */
2251     /* Change this to lookup if required */
2252     ihevcd_get_tile_pos(ps_pps, ps_sps, ps_codec->s_parse.i4_ctb_x,
2253                         ps_codec->s_parse.i4_ctb_y,
2254                         &ps_codec->s_parse.i4_ctb_tile_x,
2255                         &ps_codec->s_parse.i4_ctb_tile_y,
2256                         &tile_idx);
2257     ps_codec->s_parse.ps_tile = ps_pps->ps_tile + tile_idx;
2258     ps_codec->s_parse.i4_cur_tile_idx = tile_idx;
2259     ps_tile = ps_codec->s_parse.ps_tile;
2260     if(tile_idx)
2261         ps_tile_prev = ps_tile - 1;
2262     else
2263         ps_tile_prev = ps_tile;
2264 
2265     /* If the present slice is dependent, then store the previous
2266      * independent slices' ctb x and y values for decoding process */
2267     if(0 == ps_codec->i4_slice_error)
2268     {
2269         if(1 == ps_slice_hdr->i1_dependent_slice_flag)
2270         {
2271             /*If slice is present at the start of a new tile*/
2272             if((0 == ps_codec->s_parse.i4_ctb_tile_x) && (0 == ps_codec->s_parse.i4_ctb_tile_y))
2273             {
2274                 ps_codec->s_parse.i4_ctb_slice_x = 0;
2275                 ps_codec->s_parse.i4_ctb_slice_y = 0;
2276             }
2277         }
2278 
2279         if(!ps_slice_hdr->i1_dependent_slice_flag)
2280         {
2281             ps_codec->s_parse.i4_ctb_slice_x = 0;
2282             ps_codec->s_parse.i4_ctb_slice_y = 0;
2283         }
2284     }
2285 
2286     /* Frame level initializations */
2287     if((0 == ps_codec->s_parse.i4_ctb_y) &&
2288                     (0 == ps_codec->s_parse.i4_ctb_x))
2289     {
2290         ret = ihevcd_parse_pic_init(ps_codec);
2291         RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
2292 
2293         ps_codec->s_parse.pu4_pic_tu_idx[0] = 0;
2294         ps_codec->s_parse.pu4_pic_pu_idx[0] = 0;
2295         ps_codec->s_parse.i4_cur_independent_slice_idx = 0;
2296         ps_codec->s_parse.i4_ctb_tile_x = 0;
2297         ps_codec->s_parse.i4_ctb_tile_y = 0;
2298     }
2299 
2300     {
2301         /* Updating the poc list of current slice to ps_mv_buf */
2302         mv_buf_t *ps_mv_buf = ps_codec->s_parse.ps_cur_mv_buf;
2303 
2304         if(ps_slice_hdr->i1_num_ref_idx_l1_active != 0)
2305         {
2306             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
2307             {
2308                 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;
2309                 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;
2310             }
2311         }
2312 
2313         if(ps_slice_hdr->i1_num_ref_idx_l0_active != 0)
2314         {
2315             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
2316             {
2317                 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;
2318                 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;
2319             }
2320         }
2321     }
2322 
2323     /*Initialize the low delay flag at the beginning of every slice*/
2324     if((0 == ps_codec->s_parse.i4_ctb_slice_x) || (0 == ps_codec->s_parse.i4_ctb_slice_y))
2325     {
2326         /* Lowdelay flag */
2327         WORD32 cur_poc, ref_list_poc, flag = 1;
2328         cur_poc = ps_slice_hdr->i4_abs_pic_order_cnt;
2329         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
2330         {
2331             ref_list_poc = ((mv_buf_t *)ps_slice_hdr->as_ref_pic_list0[i].pv_mv_buf)->i4_abs_poc;
2332             if(ref_list_poc > cur_poc)
2333             {
2334                 flag = 0;
2335                 break;
2336             }
2337         }
2338         if(flag && (ps_slice_hdr->i1_slice_type == BSLICE))
2339         {
2340             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
2341             {
2342                 ref_list_poc = ((mv_buf_t *)ps_slice_hdr->as_ref_pic_list1[i].pv_mv_buf)->i4_abs_poc;
2343                 if(ref_list_poc > cur_poc)
2344                 {
2345                     flag = 0;
2346                     break;
2347                 }
2348             }
2349         }
2350         ps_slice_hdr->i1_low_delay_flag = flag;
2351     }
2352 
2353     /* initialize the cabac init idc based on slice type */
2354     if(ps_slice_hdr->i1_slice_type == ISLICE)
2355     {
2356         cabac_init_idc = 0;
2357     }
2358     else if(ps_slice_hdr->i1_slice_type == PSLICE)
2359     {
2360         cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 2 : 1;
2361     }
2362     else
2363     {
2364         cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 1 : 2;
2365     }
2366 
2367     slice_qp = ps_slice_hdr->i1_slice_qp_delta + ps_pps->i1_pic_init_qp;
2368     slice_qp = CLIP3(slice_qp, 0, 51);
2369 
2370     /*Update QP value for every indepndent slice or for every dependent slice that begins at the start of a new tile*/
2371     if((0 == ps_slice_hdr->i1_dependent_slice_flag) ||
2372                     ((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))))
2373     {
2374         ps_codec->s_parse.u4_qp = slice_qp;
2375     }
2376 
2377     /*Cabac init at the beginning of a slice*/
2378     //If the slice is a dependent slice, not present at the start of a tile
2379     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))))
2380     {
2381         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)))
2382         {
2383             ihevcd_cabac_reset(&ps_codec->s_parse.s_cabac,
2384                                &ps_codec->s_parse.s_bitstrm);
2385         }
2386     }
2387     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)))
2388     {
2389         ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2390                                 &ps_codec->s_parse.s_bitstrm,
2391                                 slice_qp,
2392                                 cabac_init_idc,
2393                                 &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0]);
2394         if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2395         {
2396             ps_codec->i4_slice_error = 1;
2397             end_of_slice_flag = 1;
2398             ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2399         }
2400     }
2401 
2402 
2403     do
2404     {
2405 
2406         {
2407             WORD32 cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
2408                             + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
2409             if(1 == ps_codec->i4_num_cores && 0 == cur_ctb_idx % RESET_TU_BUF_NCTB)
2410             {
2411                 ps_codec->s_parse.ps_tu = ps_codec->s_parse.ps_pic_tu;
2412                 ps_codec->s_parse.i4_pic_tu_idx = 0;
2413             }
2414         }
2415 
2416         end_of_pic = 0;
2417         /* Section:7.3.7 Coding tree unit syntax */
2418         /* coding_tree_unit() inlined here */
2419         /* If number of cores is greater than 1, then add job to the queue */
2420         //TODO: Dual core implementation might need a different algo for better load balancing
2421         /* At the start of ctb row parsing in a tile, queue a job for processing the current tile row */
2422         ps_codec->s_parse.i4_ctb_num_pcm_blks = 0;
2423 
2424 
2425         /*At the beginning of each tile-which is not the beginning of a slice, cabac context must be initialized.
2426          * Hence, check for the tile beginning here */
2427         if(((0 == ps_codec->s_parse.i4_ctb_tile_x) && (0 == ps_codec->s_parse.i4_ctb_tile_y))
2428                         && (!((ps_tile->u1_pos_x == 0) && (ps_tile->u1_pos_y == 0)))
2429                         && (!((0 == ps_codec->s_parse.i4_ctb_slice_x) && (0 == ps_codec->s_parse.i4_ctb_slice_y))))
2430         {
2431             slice_qp = ps_slice_hdr->i1_slice_qp_delta + ps_pps->i1_pic_init_qp;
2432             slice_qp = CLIP3(slice_qp, 0, 51);
2433             ps_codec->s_parse.u4_qp = slice_qp;
2434 
2435             ihevcd_get_tile_pos(ps_pps, ps_sps, ps_codec->s_parse.i4_ctb_x,
2436                                 ps_codec->s_parse.i4_ctb_y,
2437                                 &ps_codec->s_parse.i4_ctb_tile_x,
2438                                 &ps_codec->s_parse.i4_ctb_tile_y,
2439                                 &tile_idx);
2440 
2441             ps_codec->s_parse.ps_tile = ps_pps->ps_tile + tile_idx;
2442             ps_codec->s_parse.i4_cur_tile_idx = tile_idx;
2443             ps_tile_prev = ps_tile - 1;
2444 
2445             tile_start_ctb_idx = ps_tile->u1_pos_x
2446                             + ps_tile->u1_pos_y * (ps_sps->i2_pic_wd_in_ctb);
2447 
2448             slice_start_ctb_idx =  ps_slice_hdr->i2_ctb_x
2449                             + ps_slice_hdr->i2_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
2450 
2451             /*For slices that span across multiple tiles*/
2452             if(slice_start_ctb_idx < tile_start_ctb_idx)
2453             {       /* 2 Cases
2454              * 1 - slice spans across frame-width- but does not start from 1st column
2455              * 2 - Slice spans across multiple tiles anywhere is a frame
2456              */
2457                 ps_codec->s_parse.i4_ctb_slice_y = ps_tile->u1_pos_y - ps_slice_hdr->i2_ctb_y;
2458                 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
2459                 {
2460                     if(ps_slice_hdr->i2_ctb_y <= ps_tile->u1_pos_y)
2461                     {
2462                         //Check if ctb x is before or after
2463                         if(ps_slice_hdr->i2_ctb_x > ps_tile->u1_pos_x)
2464                         {
2465                             ps_codec->s_parse.i4_ctb_slice_y -= 1;
2466                         }
2467                     }
2468                 }
2469                 /*ps_codec->s_parse.i4_ctb_slice_y = ps_tile->u1_pos_y - ps_slice_hdr->i2_ctb_y;
2470                 if (ps_slice_hdr->i2_ctb_y <= ps_tile->u1_pos_y)
2471                 {
2472                     //Check if ctb x is before or after
2473                     if (ps_slice_hdr->i2_ctb_x > ps_tile->u1_pos_x )
2474                     {
2475                         ps_codec->s_parse.i4_ctb_slice_y -= 1 ;
2476                     }
2477                 }*/
2478             }
2479 
2480             /* Cabac init is done unconditionally at the start of the tile irrespective
2481              * of whether it is a dependent or an independent slice */
2482             {
2483                 ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2484                                         &ps_codec->s_parse.s_bitstrm,
2485                                         slice_qp,
2486                                         cabac_init_idc,
2487                                         &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0]);
2488                 if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2489                 {
2490                     ps_codec->i4_slice_error = 1;
2491                     end_of_slice_flag = 1;
2492                     ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2493                 }
2494 
2495             }
2496         }
2497         /* If number of cores is greater than 1, then add job to the queue */
2498         //TODO: Dual core implementation might need a different algo for better load balancing
2499         /* At the start of ctb row parsing in a tile, queue a job for processing the current tile row */
2500 
2501         if(0 == ps_codec->s_parse.i4_ctb_tile_x)
2502         {
2503 
2504             if(1 < ps_codec->i4_num_cores)
2505             {
2506                 proc_job_t s_job;
2507                 IHEVCD_ERROR_T ret;
2508                 s_job.i4_cmd    = CMD_PROCESS;
2509                 s_job.i2_ctb_cnt = (WORD16)ps_tile->u2_wd;
2510                 s_job.i2_ctb_x = (WORD16)ps_codec->s_parse.i4_ctb_x;
2511                 s_job.i2_ctb_y = (WORD16)ps_codec->s_parse.i4_ctb_y;
2512                 s_job.i2_slice_idx = (WORD16)ps_codec->s_parse.i4_cur_slice_idx;
2513                 s_job.i4_tu_coeff_data_ofst = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data -
2514                                 (UWORD8 *)ps_codec->s_parse.pv_pic_tu_coeff_data;
2515                 ret = ihevcd_jobq_queue((jobq_t *)ps_codec->s_parse.pv_proc_jobq, &s_job, sizeof(proc_job_t), 1);
2516 
2517                 if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2518                     return ret;
2519             }
2520             else
2521             {
2522                 process_ctxt_t *ps_proc = &ps_codec->as_process[0];
2523                 WORD32 tu_coeff_data_ofst = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data -
2524                                 (UWORD8 *)ps_codec->s_parse.pv_pic_tu_coeff_data;
2525 
2526                 /* If the codec is running in single core mode,
2527                  * initialize zeroth process context
2528                  * TODO: Dual core mode might need a different implementation instead of jobq
2529                  */
2530 
2531                 ps_proc->i4_ctb_cnt = ps_tile->u2_wd;
2532                 ps_proc->i4_ctb_x   = ps_codec->s_parse.i4_ctb_x;
2533                 ps_proc->i4_ctb_y   = ps_codec->s_parse.i4_ctb_y;
2534                 ps_proc->i4_cur_slice_idx = ps_codec->s_parse.i4_cur_slice_idx;
2535 
2536                 ihevcd_init_proc_ctxt(ps_proc, tu_coeff_data_ofst);
2537             }
2538         }
2539 
2540 
2541         /* Restore cabac context model from top right CTB if entropy sync is enabled */
2542         if(ps_pps->i1_entropy_coding_sync_enabled_flag)
2543         {
2544             /*TODO Handle single CTB and top-right belonging to a different slice */
2545             if(0 == ps_codec->s_parse.i4_ctb_x)
2546             {
2547                 //WORD32 size = sizeof(ps_codec->s_parse.s_cabac.au1_ctxt_models);
2548                 WORD32 default_ctxt = 0;
2549 
2550                 if((0 == ps_codec->s_parse.i4_ctb_slice_y) && (!ps_slice_hdr->i1_dependent_slice_flag))
2551                     default_ctxt = 1;
2552                 if(1 == ps_sps->i2_pic_wd_in_ctb)
2553                     default_ctxt = 1;
2554 
2555                 ps_codec->s_parse.u4_qp = slice_qp;
2556                 if(default_ctxt)
2557                 {
2558                     //memcpy(&ps_codec->s_parse.s_cabac.au1_ctxt_models, &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0], size);
2559                     ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2560                                             &ps_codec->s_parse.s_bitstrm,
2561                                             slice_qp,
2562                                             cabac_init_idc,
2563                                             &gau1_ihevc_cab_ctxts[cabac_init_idc][slice_qp][0]);
2564 
2565                     if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2566                     {
2567                         ps_codec->i4_slice_error = 1;
2568                         end_of_slice_flag = 1;
2569                         ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2570                     }
2571                 }
2572                 else
2573                 {
2574                     //memcpy(&ps_codec->s_parse.s_cabac.au1_ctxt_models, &ps_codec->s_parse.s_cabac.au1_ctxt_models_sync, size);
2575                     ret = ihevcd_cabac_init(&ps_codec->s_parse.s_cabac,
2576                                             &ps_codec->s_parse.s_bitstrm,
2577                                             slice_qp,
2578                                             cabac_init_idc,
2579                                             (const UWORD8 *)&ps_codec->s_parse.s_cabac.au1_ctxt_models_sync);
2580 
2581                     if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2582                     {
2583                         ps_codec->i4_slice_error = 1;
2584                         end_of_slice_flag = 1;
2585                         ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2586                     }
2587                 }
2588             }
2589         }
2590 
2591 
2592 
2593         if(0 == ps_codec->i4_slice_error)
2594         {
2595             if(ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag)
2596                 ihevcd_parse_sao(ps_codec);
2597         }
2598         else
2599         {
2600             sao_t *ps_sao = ps_codec->s_parse.ps_pic_sao +
2601                             ps_codec->s_parse.i4_ctb_x +
2602                             ps_codec->s_parse.i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
2603 
2604             /* Default values */
2605             ps_sao->b3_y_type_idx = 0;
2606             ps_sao->b3_cb_type_idx = 0;
2607             ps_sao->b3_cr_type_idx = 0;
2608         }
2609 
2610         //AEV_TRACE("CTB x", ps_codec->s_parse.i4_ctb_x, 0);
2611         //AEV_TRACE("CTB y", ps_codec->s_parse.i4_ctb_y, 0);
2612 
2613         {
2614             WORD32 ctb_indx;
2615             ctb_indx = ps_codec->s_parse.i4_ctb_x + ps_sps->i2_pic_wd_in_ctb * ps_codec->s_parse.i4_ctb_y;
2616             ps_codec->s_parse.s_bs_ctxt.pu1_pic_qp_const_in_ctb[ctb_indx >> 3] |= (1 << (ctb_indx & 7));
2617             {
2618                 UWORD16 *pu1_slice_idx = ps_codec->s_parse.pu1_slice_idx;
2619                 pu1_slice_idx[ctb_indx] = ps_codec->s_parse.i4_cur_independent_slice_idx;
2620             }
2621         }
2622 
2623         if(0 == ps_codec->i4_slice_error)
2624         {
2625             tu_t *ps_tu = ps_codec->s_parse.ps_tu;
2626             WORD32 i4_tu_cnt = ps_codec->s_parse.s_cu.i4_tu_cnt;
2627             WORD32 i4_pic_tu_idx = ps_codec->s_parse.i4_pic_tu_idx;
2628 
2629             pu_t *ps_pu = ps_codec->s_parse.ps_pu;
2630             WORD32 i4_pic_pu_idx = ps_codec->s_parse.i4_pic_pu_idx;
2631 
2632             UWORD8 *pu1_tu_coeff_data = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data;
2633 
2634             ret = ihevcd_parse_coding_quadtree(ps_codec,
2635                                                (ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size),
2636                                                (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size),
2637                                                ps_sps->i1_log2_ctb_size,
2638                                                0);
2639             /* Check for error */
2640             if (ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
2641             {
2642                 /* Reset tu and pu parameters, and signal current ctb as skip */
2643                 WORD32 pu_skip_wd, pu_skip_ht;
2644                 WORD32 rows_remaining, cols_remaining;
2645                 WORD32 tu_coeff_data_reset_size;
2646 
2647                 /* Set pu wd and ht based on whether the ctb is complete or not */
2648                 rows_remaining = ps_sps->i2_pic_height_in_luma_samples
2649                                 - (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size);
2650                 pu_skip_ht = MIN(ctb_size, rows_remaining);
2651 
2652                 cols_remaining = ps_sps->i2_pic_width_in_luma_samples
2653                                 - (ps_codec->s_parse.i4_ctb_x << ps_sps->i1_log2_ctb_size);
2654                 pu_skip_wd = MIN(ctb_size, cols_remaining);
2655 
2656                 ps_codec->s_parse.ps_tu = ps_tu;
2657                 ps_codec->s_parse.s_cu.i4_tu_cnt = i4_tu_cnt;
2658                 ps_codec->s_parse.i4_pic_tu_idx = i4_pic_tu_idx;
2659 
2660                 ps_codec->s_parse.ps_pu = ps_pu;
2661                 ps_codec->s_parse.i4_pic_pu_idx = i4_pic_pu_idx;
2662 
2663                 ps_tu->b1_cb_cbf = 0;
2664                 ps_tu->b1_cr_cbf = 0;
2665                 ps_tu->b1_y_cbf = 0;
2666                 ps_tu->b4_pos_x = 0;
2667                 ps_tu->b4_pos_y = 0;
2668                 ps_tu->b1_transquant_bypass = 0;
2669                 ps_tu->b3_size = (ps_sps->i1_log2_ctb_size - 2);
2670                 ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
2671                 ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
2672                 ps_tu->b6_luma_intra_mode   = INTRA_PRED_NONE;
2673                 ps_tu->b1_first_tu_in_cu = 1;
2674 
2675                 tu_coeff_data_reset_size = (UWORD8 *)ps_codec->s_parse.pv_tu_coeff_data - pu1_tu_coeff_data;
2676                 memset(pu1_tu_coeff_data, 0, tu_coeff_data_reset_size);
2677                 ps_codec->s_parse.pv_tu_coeff_data = (void *)pu1_tu_coeff_data;
2678 
2679                 ps_codec->s_parse.ps_tu++;
2680                 ps_codec->s_parse.s_cu.i4_tu_cnt++;
2681                 ps_codec->s_parse.i4_pic_tu_idx++;
2682 
2683                 ps_codec->s_parse.s_cu.i4_pred_mode = PRED_MODE_SKIP;
2684                 ps_codec->s_parse.s_cu.i4_part_mode = PART_2Nx2N;
2685 
2686                 ps_pu->b2_part_idx = 0;
2687                 ps_pu->b4_pos_x = 0;
2688                 ps_pu->b4_pos_y = 0;
2689                 ps_pu->b4_wd = (pu_skip_wd >> 2) - 1;
2690                 ps_pu->b4_ht = (pu_skip_ht >> 2) - 1;
2691                 ps_pu->b1_intra_flag = 0;
2692                 ps_pu->b3_part_mode = ps_codec->s_parse.s_cu.i4_part_mode;
2693                 ps_pu->b1_merge_flag = 1;
2694                 ps_pu->b3_merge_idx = 0;
2695 
2696                 ps_codec->s_parse.ps_pu++;
2697                 ps_codec->s_parse.i4_pic_pu_idx++;
2698 
2699                 /* Set slice error to suppress further parsing and
2700                  * signal end of slice.
2701                  */
2702                 ps_codec->i4_slice_error = 1;
2703                 end_of_slice_flag = 1;
2704                 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2705             }
2706         }
2707         else
2708         {
2709             tu_t *ps_tu = ps_codec->s_parse.ps_tu;
2710             pu_t *ps_pu = ps_codec->s_parse.ps_pu;
2711 
2712             ps_tu->b1_cb_cbf = 0;
2713             ps_tu->b1_cr_cbf = 0;
2714             ps_tu->b1_y_cbf = 0;
2715             ps_tu->b4_pos_x = 0;
2716             ps_tu->b4_pos_y = 0;
2717             ps_tu->b1_transquant_bypass = 0;
2718             ps_tu->b3_size = (ps_sps->i1_log2_ctb_size - 2);
2719             ps_tu->b7_qp = ps_codec->s_parse.u4_qp;
2720             ps_tu->b3_chroma_intra_mode_idx = INTRA_PRED_CHROMA_IDX_NONE;
2721             ps_tu->b6_luma_intra_mode   = INTRA_PRED_NONE;
2722             ps_tu->b1_first_tu_in_cu = 1;
2723 
2724             ps_codec->s_parse.ps_tu++;
2725             ps_codec->s_parse.s_cu.i4_tu_cnt++;
2726             ps_codec->s_parse.i4_pic_tu_idx++;
2727 
2728             ps_codec->s_parse.s_cu.i4_pred_mode = PRED_MODE_SKIP;
2729             ps_codec->s_parse.s_cu.i4_part_mode = PART_2Nx2N;
2730 
2731             ps_pu->b2_part_idx = 0;
2732             ps_pu->b4_pos_x = 0;
2733             ps_pu->b4_pos_y = 0;
2734             ps_pu->b4_wd = (ctb_size >> 2) - 1;
2735             ps_pu->b4_ht = (ctb_size >> 2) - 1;
2736             ps_pu->b1_intra_flag = 0;
2737             ps_pu->b3_part_mode = ps_codec->s_parse.s_cu.i4_part_mode;
2738             ps_pu->b1_merge_flag = 1;
2739             ps_pu->b3_merge_idx = 0;
2740 
2741             ps_codec->s_parse.ps_pu++;
2742             ps_codec->s_parse.i4_pic_pu_idx++;
2743 
2744         }
2745 
2746         if(0 == ps_codec->i4_slice_error)
2747             end_of_slice_flag = ihevcd_cabac_decode_terminate(&ps_codec->s_parse.s_cabac, &ps_codec->s_parse.s_bitstrm);
2748 
2749         AEV_TRACE("end_of_slice_flag", end_of_slice_flag, ps_codec->s_parse.s_cabac.u4_range);
2750 
2751 
2752         /* In case of tiles or entropy sync, terminate cabac and copy cabac context backed up at the end of top-right CTB */
2753         if(ps_pps->i1_tiles_enabled_flag || ps_pps->i1_entropy_coding_sync_enabled_flag)
2754         {
2755             WORD32 end_of_tile = 0;
2756             WORD32 end_of_tile_row = 0;
2757 
2758             /* Take a back up of cabac context models if entropy sync is enabled */
2759             if(ps_pps->i1_entropy_coding_sync_enabled_flag || ps_pps->i1_tiles_enabled_flag)
2760             {
2761                 if(1 == ps_codec->s_parse.i4_ctb_x)
2762                 {
2763                     WORD32 size = sizeof(ps_codec->s_parse.s_cabac.au1_ctxt_models);
2764                     memcpy(&ps_codec->s_parse.s_cabac.au1_ctxt_models_sync, &ps_codec->s_parse.s_cabac.au1_ctxt_models, size);
2765                 }
2766             }
2767 
2768             /* Since tiles and entropy sync are not enabled simultaneously, the following will not result in any problems */
2769             if((ps_codec->s_parse.i4_ctb_tile_x + 1) == (ps_tile->u2_wd))
2770             {
2771                 end_of_tile_row = 1;
2772                 if((ps_codec->s_parse.i4_ctb_tile_y + 1) == ps_tile->u2_ht)
2773                     end_of_tile = 1;
2774             }
2775             if((0 == end_of_slice_flag) &&
2776                             ((ps_pps->i1_tiles_enabled_flag && end_of_tile) ||
2777                                             (ps_pps->i1_entropy_coding_sync_enabled_flag && end_of_tile_row)))
2778             {
2779                 WORD32 end_of_sub_stream_one_bit;
2780                 end_of_sub_stream_one_bit = ihevcd_cabac_decode_terminate(&ps_codec->s_parse.s_cabac, &ps_codec->s_parse.s_bitstrm);
2781                 AEV_TRACE("end_of_sub_stream_one_bit", end_of_sub_stream_one_bit, ps_codec->s_parse.s_cabac.u4_range);
2782 
2783                 /* TODO: Remove the check for offset when HM is updated to include a byte unconditionally even for aligned location */
2784                 /* For Ittiam streams this check should not be there, for HM9.1 streams this should be there */
2785                 if(ps_codec->s_parse.s_bitstrm.u4_bit_ofst % 8)
2786                     ihevcd_bits_flush_to_byte_boundary(&ps_codec->s_parse.s_bitstrm);
2787 
2788                 UNUSED(end_of_sub_stream_one_bit);
2789             }
2790         }
2791         {
2792             WORD32 ctb_indx;
2793 
2794             ctb_addr = ps_codec->s_parse.i4_ctb_y * num_ctb_in_row + ps_codec->s_parse.i4_ctb_x;
2795 
2796             ctb_indx = ++ctb_addr;
2797 
2798             /* Store pu_idx for next CTB in frame level pu_idx array */
2799 
2800             //In case of multiple tiles, if end-of-tile row is reached
2801             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))
2802             {
2803                 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.
2804                 if(ps_tile->u2_ht == (ps_codec->s_parse.i4_ctb_tile_y + 1))
2805                 {
2806                     //If the current ctb is the last tile's last ctb
2807                     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)))
2808                     {
2809                         ctb_indx = ctb_addr; //Next continuous ctb address
2810                     }
2811                     else //Not last tile's end , but a tile end
2812                     {
2813                         tile_t *ps_next_tile = ps_codec->s_parse.ps_tile + 1;
2814                         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.
2815                     }
2816                 }
2817             }
2818 
2819             ps_codec->s_parse.pu4_pic_pu_idx[ctb_indx] = ps_codec->s_parse.i4_pic_pu_idx;
2820             ps_codec->s_parse.i4_next_pu_ctb_cnt = ctb_indx;
2821 
2822             ps_codec->s_parse.pu1_pu_map += num_min4x4_in_ctb;
2823 
2824             /* Store tu_idx for next CTB in frame level tu_idx array */
2825             if(1 == ps_codec->i4_num_cores)
2826             {
2827                 ctb_indx = (0 == ctb_addr % RESET_TU_BUF_NCTB) ?
2828                                 RESET_TU_BUF_NCTB : ctb_addr % RESET_TU_BUF_NCTB;
2829 
2830                 //In case of multiple tiles, if end-of-tile row is reached
2831                 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))
2832                 {
2833                     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.
2834                     if(ps_tile->u2_ht == (ps_codec->s_parse.i4_ctb_tile_y + 1))
2835                     {
2836                         //If the current ctb is the last tile's last ctb
2837                         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)))
2838                         {
2839                             ctb_indx = (0 == ctb_addr % RESET_TU_BUF_NCTB) ?
2840                                             RESET_TU_BUF_NCTB : ctb_addr % RESET_TU_BUF_NCTB;
2841                         }
2842                         else  //Not last tile's end , but a tile end
2843                         {
2844                             tile_t *ps_next_tile = ps_codec->s_parse.ps_tile + 1;
2845                             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.
2846                         }
2847                     }
2848                 }
2849                 ps_codec->s_parse.i4_next_tu_ctb_cnt = ctb_indx;
2850                 ps_codec->s_parse.pu4_pic_tu_idx[ctb_indx] = ps_codec->s_parse.i4_pic_tu_idx;
2851             }
2852             else
2853             {
2854                 ctb_indx = ctb_addr;
2855                 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))
2856                 {
2857                     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.
2858                     if(ps_tile->u2_ht == (ps_codec->s_parse.i4_ctb_tile_y + 1))
2859                     {
2860                         //If the current ctb is the last tile's last ctb
2861                         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)))
2862                         {
2863                             ctb_indx = ctb_addr;
2864                         }
2865                         else  //Not last tile's end , but a tile end
2866                         {
2867                             tile_t *ps_next_tile = ps_codec->s_parse.ps_tile + 1;
2868                             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.
2869                         }
2870                     }
2871                 }
2872                 ps_codec->s_parse.i4_next_tu_ctb_cnt = ctb_indx;
2873                 ps_codec->s_parse.pu4_pic_tu_idx[ctb_indx] = ps_codec->s_parse.i4_pic_tu_idx;
2874             }
2875             ps_codec->s_parse.pu1_tu_map += num_min4x4_in_ctb;
2876         }
2877 
2878         /* QP array population has to be done if deblocking is enabled in the picture
2879          * but some of the slices in the pic have it disabled */
2880         if((0 != ps_codec->i4_disable_deblk_pic) &&
2881                 (1 == ps_slice_hdr->i1_slice_disable_deblocking_filter_flag))
2882         {
2883             bs_ctxt_t *ps_bs_ctxt = &ps_codec->s_parse.s_bs_ctxt;
2884             WORD32 log2_ctb_size = ps_sps->i1_log2_ctb_size;
2885             UWORD8 *pu1_qp;
2886             WORD32 qp_strd;
2887             WORD32 u4_qp_const_in_ctb;
2888             WORD32 cur_ctb_idx;
2889             WORD32 next_ctb_idx;
2890             WORD32 cur_tu_idx;
2891             WORD32 i4_ctb_tu_cnt;
2892             tu_t *ps_tu;
2893 
2894             cur_ctb_idx = ps_codec->s_parse.i4_ctb_x + ps_sps->i2_pic_wd_in_ctb * ps_codec->s_parse.i4_ctb_y;
2895             /* ctb_size/8 elements per CTB */
2896             qp_strd = ps_sps->i2_pic_wd_in_ctb << (log2_ctb_size - 3);
2897             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));
2898 
2899             u4_qp_const_in_ctb = ps_bs_ctxt->pu1_pic_qp_const_in_ctb[cur_ctb_idx >> 3] & (1 << (cur_ctb_idx & 7));
2900 
2901             next_ctb_idx = ps_codec->s_parse.i4_next_tu_ctb_cnt;
2902             if(1 == ps_codec->i4_num_cores)
2903             {
2904                 i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
2905                                 ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
2906 
2907                 cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
2908             }
2909             else
2910             {
2911                 i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
2912                                 ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
2913 
2914                 cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
2915             }
2916 
2917             ps_tu = &ps_codec->s_parse.ps_pic_tu[cur_tu_idx];
2918 
2919             if(u4_qp_const_in_ctb)
2920             {
2921                 pu1_qp[0] = ps_tu->b7_qp;
2922             }
2923             else
2924             {
2925                 for(i = 0; i < i4_ctb_tu_cnt; i++, ps_tu++)
2926                 {
2927                     WORD32 start_pos_x;
2928                     WORD32 start_pos_y;
2929                     WORD32 tu_size;
2930 
2931                     /* start_pos_x and start_pos_y are in units of min TU size (4x4) */
2932                     start_pos_x = ps_tu->b4_pos_x;
2933                     start_pos_y = ps_tu->b4_pos_y;
2934 
2935                     tu_size = 1 << (ps_tu->b3_size + 2);
2936                     tu_size >>= 2; /* TU size divided by 4 */
2937 
2938                     if(0 == (start_pos_x & 1) && 0 == (start_pos_y & 1))
2939                     {
2940                         WORD32 row, col;
2941                         for(row = start_pos_y; row < start_pos_y + tu_size; row += 2)
2942                         {
2943                             for(col = start_pos_x; col < start_pos_x + tu_size; col += 2)
2944                             {
2945                                 pu1_qp[(row >> 1) * qp_strd + (col >> 1)] = ps_tu->b7_qp;
2946                             }
2947                         }
2948                     }
2949                 }
2950             }
2951         }
2952 
2953         if(ps_codec->i4_num_cores <= MV_PRED_NUM_CORES_THRESHOLD)
2954         {
2955             /*************************************************/
2956             /****************   MV pred **********************/
2957             /*************************************************/
2958             WORD8 u1_top_ctb_avail = 1;
2959             WORD8 u1_left_ctb_avail = 1;
2960             WORD8 u1_top_lt_ctb_avail = 1;
2961             WORD8 u1_top_rt_ctb_avail = 1;
2962             WORD16 i2_wd_in_ctb;
2963 
2964             tile_start_ctb_idx = ps_tile->u1_pos_x
2965                             + ps_tile->u1_pos_y * (ps_sps->i2_pic_wd_in_ctb);
2966 
2967             slice_start_ctb_idx =  ps_slice_hdr->i2_ctb_x
2968                             + ps_slice_hdr->i2_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
2969 
2970             if((slice_start_ctb_idx < tile_start_ctb_idx))
2971             {
2972                 //Slices span across multiple tiles.
2973                 i2_wd_in_ctb = ps_sps->i2_pic_wd_in_ctb;
2974             }
2975             else
2976             {
2977                 i2_wd_in_ctb = ps_tile->u2_wd;
2978             }
2979             /* slice and tile boundaries */
2980             if((0 == ps_codec->s_parse.i4_ctb_y) || (0 == ps_codec->s_parse.i4_ctb_tile_y))
2981             {
2982                 u1_top_ctb_avail = 0;
2983                 u1_top_lt_ctb_avail = 0;
2984                 u1_top_rt_ctb_avail = 0;
2985             }
2986 
2987             if((0 == ps_codec->s_parse.i4_ctb_x) || (0 == ps_codec->s_parse.i4_ctb_tile_x))
2988             {
2989                 u1_left_ctb_avail = 0;
2990                 u1_top_lt_ctb_avail = 0;
2991                 if((0 == ps_codec->s_parse.i4_ctb_slice_y) || (0 == ps_codec->s_parse.i4_ctb_tile_y))
2992                 {
2993                     u1_top_ctb_avail = 0;
2994                     if((i2_wd_in_ctb - 1) != ps_codec->s_parse.i4_ctb_slice_x) //TODO: For tile, not implemented
2995                     {
2996                         u1_top_rt_ctb_avail = 0;
2997                     }
2998                 }
2999             }
3000             /*For slices not beginning at start of a ctb row*/
3001             else if(ps_codec->s_parse.i4_ctb_x > 0)
3002             {
3003                 if((0 == ps_codec->s_parse.i4_ctb_slice_y) || (0 == ps_codec->s_parse.i4_ctb_tile_y))
3004                 {
3005                     u1_top_ctb_avail = 0;
3006                     u1_top_lt_ctb_avail = 0;
3007                     if(0 == ps_codec->s_parse.i4_ctb_slice_x)
3008                     {
3009                         u1_left_ctb_avail = 0;
3010                     }
3011                     if((i2_wd_in_ctb - 1) != ps_codec->s_parse.i4_ctb_slice_x)
3012                     {
3013                         u1_top_rt_ctb_avail = 0;
3014                     }
3015                 }
3016                 else if((1 == ps_codec->s_parse.i4_ctb_slice_y) && (0 == ps_codec->s_parse.i4_ctb_slice_x))
3017                 {
3018                     u1_top_lt_ctb_avail = 0;
3019                 }
3020             }
3021 
3022             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))
3023             {
3024                 u1_top_rt_ctb_avail = 0;
3025             }
3026 
3027             if(PSLICE == ps_slice_hdr->i1_slice_type
3028                             || BSLICE == ps_slice_hdr->i1_slice_type)
3029             {
3030                 mv_ctxt_t s_mv_ctxt;
3031                 process_ctxt_t *ps_proc;
3032                 UWORD32 *pu4_ctb_top_pu_idx;
3033                 UWORD32 *pu4_ctb_left_pu_idx;
3034                 UWORD32 *pu4_ctb_top_left_pu_idx;
3035                 WORD32 i4_ctb_pu_cnt;
3036                 WORD32 cur_ctb_idx;
3037                 WORD32 next_ctb_idx;
3038                 WORD32 cur_pu_idx;
3039                 ps_proc = &ps_codec->as_process[(ps_codec->i4_num_cores == 1) ? 1 : (ps_codec->i4_num_cores - 1)];
3040                 cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
3041                                 + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3042                 next_ctb_idx = ps_codec->s_parse.i4_next_pu_ctb_cnt;
3043                 i4_ctb_pu_cnt = ps_codec->s_parse.pu4_pic_pu_idx[next_ctb_idx]
3044                                 - ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3045 
3046                 cur_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3047 
3048                 pu4_ctb_top_pu_idx = ps_proc->pu4_pic_pu_idx_top
3049                                 + (ps_codec->s_parse.i4_ctb_x * ctb_size / MIN_PU_SIZE);
3050                 pu4_ctb_left_pu_idx = ps_proc->pu4_pic_pu_idx_left;
3051                 pu4_ctb_top_left_pu_idx = &ps_proc->u4_ctb_top_left_pu_idx;
3052 
3053                 /* Initializing s_mv_ctxt */
3054                 {
3055                     s_mv_ctxt.ps_pps = ps_pps;
3056                     s_mv_ctxt.ps_sps = ps_sps;
3057                     s_mv_ctxt.ps_slice_hdr = ps_slice_hdr;
3058                     s_mv_ctxt.i4_ctb_x = ps_codec->s_parse.i4_ctb_x;
3059                     s_mv_ctxt.i4_ctb_y = ps_codec->s_parse.i4_ctb_y;
3060                     s_mv_ctxt.ps_pu = &ps_codec->s_parse.ps_pic_pu[cur_pu_idx];
3061                     s_mv_ctxt.ps_pic_pu = ps_codec->s_parse.ps_pic_pu;
3062                     s_mv_ctxt.ps_tile = ps_tile;
3063                     s_mv_ctxt.pu4_pic_pu_idx_map = ps_proc->pu4_pic_pu_idx_map;
3064                     s_mv_ctxt.pu4_pic_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx;
3065                     s_mv_ctxt.pu1_pic_pu_map = ps_codec->s_parse.pu1_pic_pu_map;
3066                     s_mv_ctxt.i4_ctb_pu_cnt = i4_ctb_pu_cnt;
3067                     s_mv_ctxt.i4_ctb_start_pu_idx = cur_pu_idx;
3068                     s_mv_ctxt.u1_top_ctb_avail = u1_top_ctb_avail;
3069                     s_mv_ctxt.u1_top_rt_ctb_avail = u1_top_rt_ctb_avail;
3070                     s_mv_ctxt.u1_top_lt_ctb_avail = u1_top_lt_ctb_avail;
3071                     s_mv_ctxt.u1_left_ctb_avail = u1_left_ctb_avail;
3072                 }
3073 
3074                 ihevcd_get_mv_ctb(&s_mv_ctxt, pu4_ctb_top_pu_idx,
3075                                   pu4_ctb_left_pu_idx, pu4_ctb_top_left_pu_idx);
3076 
3077             }
3078             else
3079             {
3080                 WORD32 num_minpu_in_ctb = (ctb_size / MIN_PU_SIZE) * (ctb_size / MIN_PU_SIZE);
3081                 UWORD8 *pu1_pic_pu_map_ctb = ps_codec->s_parse.pu1_pic_pu_map +
3082                                 (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;
3083                 process_ctxt_t *ps_proc = &ps_codec->as_process[(ps_codec->i4_num_cores == 1) ? 1 : (ps_codec->i4_num_cores - 1)];
3084                 WORD32 row, col;
3085                 WORD32 pu_cnt;
3086                 WORD32 num_pu_per_ctb;
3087                 WORD32 cur_ctb_idx;
3088                 WORD32 next_ctb_idx;
3089                 WORD32 ctb_start_pu_idx;
3090                 UWORD32 *pu4_nbr_pu_idx = ps_proc->pu4_pic_pu_idx_map;
3091                 WORD32 nbr_pu_idx_strd = MAX_CTB_SIZE / MIN_PU_SIZE + 2;
3092                 pu_t *ps_pu;
3093 
3094                 for(row = 0; row < ctb_size / MIN_PU_SIZE; row++)
3095                 {
3096                     for(col = 0; col < ctb_size / MIN_PU_SIZE; col++)
3097                     {
3098                         pu1_pic_pu_map_ctb[row * ctb_size / MIN_PU_SIZE + col] = 0;
3099                     }
3100                 }
3101 
3102 
3103                 /* Neighbor PU idx update inside CTB */
3104                 /* 1byte per 4x4. Indicates the PU idx that 4x4 block belongs to */
3105 
3106                 cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
3107                                 + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3108                 next_ctb_idx = ps_codec->s_parse.i4_next_pu_ctb_cnt;
3109                 num_pu_per_ctb = ps_codec->s_parse.pu4_pic_pu_idx[next_ctb_idx]
3110                                 - ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3111                 ctb_start_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3112                 ps_pu = &ps_codec->s_parse.ps_pic_pu[ctb_start_pu_idx];
3113 
3114                 for(pu_cnt = 0; pu_cnt < num_pu_per_ctb; pu_cnt++, ps_pu++)
3115                 {
3116                     UWORD32 cur_pu_idx;
3117                     WORD32 pu_ht = (ps_pu->b4_ht + 1) << 2;
3118                     WORD32 pu_wd = (ps_pu->b4_wd + 1) << 2;
3119 
3120                     cur_pu_idx = ctb_start_pu_idx + pu_cnt;
3121 
3122                     for(row = 0; row < pu_ht / MIN_PU_SIZE; row++)
3123                         for(col = 0; col < pu_wd / MIN_PU_SIZE; col++)
3124                             pu4_nbr_pu_idx[(1 + ps_pu->b4_pos_x + col)
3125                                             + (1 + ps_pu->b4_pos_y + row)
3126                                             * nbr_pu_idx_strd] =
3127                                             cur_pu_idx;
3128                 }
3129 
3130                 /* Updating Top and Left pointers */
3131                 {
3132                     WORD32 rows_remaining = ps_sps->i2_pic_height_in_luma_samples
3133                                     - (ps_codec->s_parse.i4_ctb_y << ps_sps->i1_log2_ctb_size);
3134                     WORD32 ctb_size_left = MIN(ctb_size, rows_remaining);
3135 
3136                     /* Top Left */
3137                     /* saving top left before updating top ptr, as updating top ptr will overwrite the top left for the next ctb */
3138                     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];
3139                     for(i = 0; i < ctb_size / MIN_PU_SIZE; i++)
3140                     {
3141                         /* Left */
3142                         /* Last column of au4_nbr_pu_idx */
3143                         ps_proc->pu4_pic_pu_idx_left[i] = pu4_nbr_pu_idx[(ctb_size / MIN_PU_SIZE)
3144                                         + (i + 1) * nbr_pu_idx_strd];
3145                         /* Top */
3146                         /* Last row of au4_nbr_pu_idx */
3147                         ps_proc->pu4_pic_pu_idx_top[(ps_codec->s_parse.i4_ctb_x * ctb_size / MIN_PU_SIZE) + i] =
3148                                         pu4_nbr_pu_idx[(ctb_size_left / MIN_PU_SIZE) * nbr_pu_idx_strd + i + 1];
3149 
3150                     }
3151                 }
3152             }
3153 
3154             /*************************************************/
3155             /******************  BS, QP  *********************/
3156             /*************************************************/
3157             /* Check if deblock is disabled for the current slice or if it is disabled for the current picture
3158              * because of disable deblock api
3159              */
3160             if(0 == ps_codec->i4_disable_deblk_pic)
3161             {
3162                 /* Boundary strength calculation is done irrespective of whether deblocking is disabled
3163                  * in the slice or not, to handle deblocking slice boundaries */
3164                 if((0 == ps_codec->i4_slice_error))
3165                 {
3166                     WORD32 i4_ctb_tu_cnt;
3167                     WORD32 cur_ctb_idx, next_ctb_idx;
3168                     WORD32 cur_pu_idx;
3169                     WORD32 cur_tu_idx;
3170                     process_ctxt_t *ps_proc;
3171 
3172                     ps_proc = &ps_codec->as_process[(ps_codec->i4_num_cores == 1) ? 1 : (ps_codec->i4_num_cores - 1)];
3173                     cur_ctb_idx = ps_codec->s_parse.i4_ctb_x
3174                                     + ps_codec->s_parse.i4_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3175 
3176                     cur_pu_idx = ps_codec->s_parse.pu4_pic_pu_idx[cur_ctb_idx];
3177                     next_ctb_idx = ps_codec->s_parse.i4_next_tu_ctb_cnt;
3178                     if(1 == ps_codec->i4_num_cores)
3179                     {
3180                         i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
3181                                         ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
3182 
3183                         cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx % RESET_TU_BUF_NCTB];
3184                     }
3185                     else
3186                     {
3187                         i4_ctb_tu_cnt = ps_codec->s_parse.pu4_pic_tu_idx[next_ctb_idx] -
3188                                         ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
3189 
3190                         cur_tu_idx = ps_codec->s_parse.pu4_pic_tu_idx[cur_ctb_idx];
3191                     }
3192 
3193                     ps_codec->s_parse.s_bs_ctxt.ps_pps = ps_codec->s_parse.ps_pps;
3194                     ps_codec->s_parse.s_bs_ctxt.ps_sps = ps_codec->s_parse.ps_sps;
3195                     ps_codec->s_parse.s_bs_ctxt.ps_codec = ps_codec;
3196                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_tu_cnt = i4_ctb_tu_cnt;
3197                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_x = ps_codec->s_parse.i4_ctb_x;
3198                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_y = ps_codec->s_parse.i4_ctb_y;
3199                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_tile_x = ps_codec->s_parse.i4_ctb_tile_x;
3200                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_tile_y = ps_codec->s_parse.i4_ctb_tile_y;
3201                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_slice_x = ps_codec->s_parse.i4_ctb_slice_x;
3202                     ps_codec->s_parse.s_bs_ctxt.i4_ctb_slice_y = ps_codec->s_parse.i4_ctb_slice_y;
3203                     ps_codec->s_parse.s_bs_ctxt.ps_tu = &ps_codec->s_parse.ps_pic_tu[cur_tu_idx];
3204                     ps_codec->s_parse.s_bs_ctxt.ps_pu = &ps_codec->s_parse.ps_pic_pu[cur_pu_idx];
3205                     ps_codec->s_parse.s_bs_ctxt.pu4_pic_pu_idx_map = ps_proc->pu4_pic_pu_idx_map;
3206                     ps_codec->s_parse.s_bs_ctxt.i4_next_pu_ctb_cnt = ps_codec->s_parse.i4_next_pu_ctb_cnt;
3207                     ps_codec->s_parse.s_bs_ctxt.i4_next_tu_ctb_cnt = ps_codec->s_parse.i4_next_tu_ctb_cnt;
3208                     ps_codec->s_parse.s_bs_ctxt.pu1_slice_idx = ps_codec->s_parse.pu1_slice_idx;
3209                     ps_codec->s_parse.s_bs_ctxt.ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
3210                     ps_codec->s_parse.s_bs_ctxt.ps_tile = ps_codec->s_parse.ps_tile;
3211 
3212                     if(ISLICE == ps_slice_hdr->i1_slice_type)
3213                     {
3214                         ihevcd_ctb_boundary_strength_islice(&ps_codec->s_parse.s_bs_ctxt);
3215                     }
3216                     else
3217                     {
3218                         ihevcd_ctb_boundary_strength_pbslice(&ps_codec->s_parse.s_bs_ctxt);
3219                     }
3220                 }
3221 
3222                 /* Boundary strength is set to zero if deblocking is disabled for the current slice */
3223                 if(0 != ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
3224                 {
3225                     WORD32 bs_strd = (ps_sps->i2_pic_wd_in_ctb + 1) * (ctb_size * ctb_size / 8 / 16);
3226 
3227                     UWORD32 *pu4_vert_bs = (UWORD32 *)((UWORD8 *)ps_codec->s_parse.s_bs_ctxt.pu4_pic_vert_bs +
3228                                     ps_codec->s_parse.i4_ctb_x * (ctb_size * ctb_size / 8 / 16) +
3229                                     ps_codec->s_parse.i4_ctb_y * bs_strd);
3230                     UWORD32 *pu4_horz_bs = (UWORD32 *)((UWORD8 *)ps_codec->s_parse.s_bs_ctxt.pu4_pic_horz_bs +
3231                                     ps_codec->s_parse.i4_ctb_x * (ctb_size * ctb_size / 8 / 16) +
3232                                     ps_codec->s_parse.i4_ctb_y * bs_strd);
3233 
3234                     memset(pu4_vert_bs, 0, (ctb_size / 8) * (ctb_size / 4) / 8 * 2);
3235                     memset(pu4_horz_bs, 0, (ctb_size / 8) * (ctb_size / 4) / 8 * 2);
3236                 }
3237             }
3238 
3239         }
3240 
3241         DATA_SYNC();
3242 
3243         /* Update the parse status map */
3244         {
3245             sps_t *ps_sps = ps_codec->s_parse.ps_sps;
3246             UWORD8 *pu1_buf;
3247             WORD32 idx;
3248             idx = (ps_codec->s_parse.i4_ctb_x);
3249             idx += ((ps_codec->s_parse.i4_ctb_y) * ps_sps->i2_pic_wd_in_ctb);
3250             pu1_buf = (ps_codec->pu1_parse_map + idx);
3251             *pu1_buf = 1;
3252         }
3253 
3254         /* Increment CTB x and y positions */
3255         ps_codec->s_parse.i4_ctb_tile_x++;
3256         ps_codec->s_parse.i4_ctb_x++;
3257         ps_codec->s_parse.i4_ctb_slice_x++;
3258 
3259         /*If tiles are enabled, handle the slice counters differently*/
3260         if(ps_pps->i1_tiles_enabled_flag)
3261         {
3262             //Indicates multiple tiles in a slice case
3263             tile_start_ctb_idx = ps_tile->u1_pos_x
3264                             + ps_tile->u1_pos_y * (ps_sps->i2_pic_wd_in_ctb);
3265 
3266             slice_start_ctb_idx =  ps_slice_hdr->i2_ctb_x
3267                             + ps_slice_hdr->i2_ctb_y * (ps_sps->i2_pic_wd_in_ctb);
3268 
3269             if((slice_start_ctb_idx < tile_start_ctb_idx))
3270             {
3271                 if(ps_codec->s_parse.i4_ctb_slice_x == (ps_tile->u1_pos_x + ps_tile->u2_wd))
3272                 {
3273                     /* Reached end of slice row within a tile /frame */
3274                     ps_codec->s_parse.i4_ctb_slice_y++;
3275                     ps_codec->s_parse.i4_ctb_slice_x = ps_tile->u1_pos_x; //todo:Check
3276                 }
3277             }
3278             //Indicates multiple slices in a tile case - hence, reset slice_x
3279             else if(ps_codec->s_parse.i4_ctb_slice_x == (ps_tile->u2_wd))
3280             {
3281                 ps_codec->s_parse.i4_ctb_slice_y++;
3282                 ps_codec->s_parse.i4_ctb_slice_x = 0;
3283             }
3284         }
3285         else
3286         {
3287             if(ps_codec->s_parse.i4_ctb_slice_x == ps_tile->u2_wd)
3288             {
3289                 /* Reached end of slice row within a tile /frame */
3290                 ps_codec->s_parse.i4_ctb_slice_y++;
3291                 ps_codec->s_parse.i4_ctb_slice_x = 0;
3292             }
3293         }
3294 
3295 
3296         if(ps_codec->s_parse.i4_ctb_tile_x == (ps_tile->u2_wd))
3297         {
3298             /* Reached end of tile row */
3299             ps_codec->s_parse.i4_ctb_tile_x = 0;
3300             ps_codec->s_parse.i4_ctb_x = ps_tile->u1_pos_x;
3301 
3302             ps_codec->s_parse.i4_ctb_tile_y++;
3303             ps_codec->s_parse.i4_ctb_y++;
3304 
3305             if(ps_codec->s_parse.i4_ctb_tile_y == (ps_tile->u2_ht))
3306             {
3307                 /* Reached End of Tile */
3308                 ps_codec->s_parse.i4_ctb_tile_y = 0;
3309                 ps_codec->s_parse.i4_ctb_tile_x = 0;
3310                 ps_codec->s_parse.ps_tile++;
3311 
3312                 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))
3313                 {
3314                     /* Reached end of frame */
3315                     end_of_pic = 1;
3316                     ps_codec->s_parse.i4_ctb_x = 0;
3317                     ps_codec->s_parse.i4_ctb_y = ps_sps->i2_pic_ht_in_ctb;
3318                 }
3319                 else
3320                 {
3321                     /* Initialize ctb_x and ctb_y to start of next tile */
3322                     ps_tile = ps_codec->s_parse.ps_tile;
3323                     ps_codec->s_parse.i4_ctb_x = ps_tile->u1_pos_x;
3324                     ps_codec->s_parse.i4_ctb_y = ps_tile->u1_pos_y;
3325                     ps_codec->s_parse.i4_ctb_tile_y = 0;
3326                     ps_codec->s_parse.i4_ctb_tile_x = 0;
3327                     ps_codec->s_parse.i4_ctb_slice_x = ps_tile->u1_pos_x;
3328                     ps_codec->s_parse.i4_ctb_slice_y = ps_tile->u1_pos_y;
3329 
3330                 }
3331             }
3332 
3333         }
3334 
3335         ps_codec->s_parse.i4_next_ctb_indx = ps_codec->s_parse.i4_ctb_x +
3336                         ps_codec->s_parse.i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
3337 
3338         /* If the current slice is in error, check if the next slice's address
3339          * is reached and mark the end_of_slice flag */
3340         if(ps_codec->i4_slice_error)
3341         {
3342             slice_header_t *ps_slice_hdr_next = ps_slice_hdr + 1;
3343             WORD32 next_slice_addr = ps_slice_hdr_next->i2_ctb_x +
3344                             ps_slice_hdr_next->i2_ctb_y * ps_sps->i2_pic_wd_in_ctb;
3345 
3346             if(ps_codec->s_parse.i4_next_ctb_indx == next_slice_addr)
3347                 end_of_slice_flag = 1;
3348         }
3349 
3350         /* If the codec is running in single core mode
3351          * then call process function for current CTB
3352          */
3353         if((1 == ps_codec->i4_num_cores) && (ps_codec->s_parse.i4_ctb_tile_x == 0))
3354         {
3355             process_ctxt_t *ps_proc = &ps_codec->as_process[0];
3356 //          ps_proc->i4_ctb_cnt = ihevcd_nctb_cnt(ps_codec, ps_sps);
3357             ps_proc->i4_ctb_cnt = ps_proc->ps_tile->u2_wd;
3358             ihevcd_process(ps_proc);
3359         }
3360 
3361         /* If the bytes for the current slice are exhausted
3362          * set end_of_slice flag to 1
3363          * This slice will be treated as incomplete */
3364         if((UWORD8 *)ps_codec->s_parse.s_bitstrm.pu1_buf_max + BITSTRM_OFF_THRS <
3365                                         ((UWORD8 *)ps_codec->s_parse.s_bitstrm.pu4_buf + (ps_codec->s_parse.s_bitstrm.u4_bit_ofst / 8)))
3366         {
3367             // end_of_slice_flag = ps_codec->i4_slice_error ? 0 : 1;
3368 
3369             if(0 == ps_codec->i4_slice_error)
3370                 end_of_slice_flag = 1;
3371         }
3372 
3373 
3374         if(end_of_pic)
3375             break;
3376     } while(!end_of_slice_flag);
3377 
3378     /* Reset slice error */
3379     ps_codec->i4_slice_error = 0;
3380 
3381     /* Increment the slice index for parsing next slice */
3382     if(0 == end_of_pic)
3383     {
3384         while(1)
3385         {
3386 
3387             WORD32 parse_slice_idx;
3388             parse_slice_idx = ps_codec->s_parse.i4_cur_slice_idx;
3389             parse_slice_idx++;
3390 
3391             {
3392                 /* If the next slice header is not initialized, update cur_slice_idx and break */
3393                 if((1 == ps_codec->i4_num_cores) || (0 != (parse_slice_idx & (MAX_SLICE_HDR_CNT - 1))))
3394                 {
3395                     ps_codec->s_parse.i4_cur_slice_idx = parse_slice_idx;
3396                     break;
3397                 }
3398 
3399                 /* If the next slice header is initialised, wait for the parsed slices to be processed */
3400                 else
3401                 {
3402                     WORD32 ctb_indx = 0;
3403 
3404                     while(ctb_indx != ps_sps->i4_pic_size_in_ctb)
3405                     {
3406                         WORD32 parse_status = *(ps_codec->pu1_parse_map + ctb_indx);
3407                         volatile WORD32 proc_status = *(ps_codec->pu1_proc_map + ctb_indx) & 1;
3408 
3409                         if(parse_status == proc_status)
3410                             ctb_indx++;
3411                     }
3412                     ps_codec->s_parse.i4_cur_slice_idx = parse_slice_idx;
3413                     break;
3414                 }
3415 
3416             }
3417         }
3418 
3419     }
3420     else
3421     {
3422 #if FRAME_ILF_PAD
3423         if(FRAME_ILF_PAD && 1 == ps_codec->i4_num_cores)
3424         {
3425             if(ps_slice_hdr->i4_abs_pic_order_cnt == 0)
3426             {
3427                 DUMP_PRE_ILF(ps_codec->as_process[0].pu1_cur_pic_luma,
3428                              ps_codec->as_process[0].pu1_cur_pic_chroma,
3429                              ps_sps->i2_pic_width_in_luma_samples,
3430                              ps_sps->i2_pic_height_in_luma_samples,
3431                              ps_codec->i4_strd);
3432 
3433                 DUMP_BS(ps_codec->as_process[0].s_bs_ctxt.pu4_pic_vert_bs,
3434                         ps_codec->as_process[0].s_bs_ctxt.pu4_pic_horz_bs,
3435                         ps_sps->i2_pic_wd_in_ctb * (ctb_size * ctb_size / 8 / 16) * ps_sps->i2_pic_ht_in_ctb,
3436                         (ps_sps->i2_pic_wd_in_ctb + 1) * (ctb_size * ctb_size / 8 / 16) * ps_sps->i2_pic_ht_in_ctb);
3437 
3438                 DUMP_QP(ps_codec->as_process[0].s_bs_ctxt.pu1_pic_qp,
3439                         (ps_sps->i2_pic_height_in_luma_samples * ps_sps->i2_pic_width_in_luma_samples) / (MIN_CU_SIZE * MIN_CU_SIZE));
3440 
3441                 DUMP_QP_CONST_IN_CTB(ps_codec->as_process[0].s_bs_ctxt.pu1_pic_qp_const_in_ctb,
3442                                      (ps_sps->i2_pic_height_in_luma_samples * ps_sps->i2_pic_width_in_luma_samples) / (MIN_CTB_SIZE * MIN_CTB_SIZE) / 8);
3443 
3444                 DUMP_NO_LOOP_FILTER(ps_codec->as_process[0].pu1_pic_no_loop_filter_flag,
3445                                     (ps_sps->i2_pic_width_in_luma_samples / MIN_CU_SIZE) * (ps_sps->i2_pic_height_in_luma_samples / MIN_CU_SIZE) / 8);
3446 
3447                 DUMP_OFFSETS(ps_slice_hdr->i1_beta_offset_div2,
3448                              ps_slice_hdr->i1_tc_offset_div2,
3449                              ps_pps->i1_pic_cb_qp_offset,
3450                              ps_pps->i1_pic_cr_qp_offset);
3451             }
3452             ps_codec->s_parse.s_deblk_ctxt.ps_pps = ps_codec->s_parse.ps_pps;
3453             ps_codec->s_parse.s_deblk_ctxt.ps_sps = ps_codec->s_parse.ps_sps;
3454             ps_codec->s_parse.s_deblk_ctxt.ps_codec = ps_codec;
3455             ps_codec->s_parse.s_deblk_ctxt.ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
3456             ps_codec->s_parse.s_deblk_ctxt.is_chroma_yuv420sp_vu = (ps_codec->e_ref_chroma_fmt == IV_YUV_420SP_VU);
3457 
3458             ps_codec->s_parse.s_sao_ctxt.ps_pps = ps_codec->s_parse.ps_pps;
3459             ps_codec->s_parse.s_sao_ctxt.ps_sps = ps_codec->s_parse.ps_sps;
3460             ps_codec->s_parse.s_sao_ctxt.ps_codec = ps_codec;
3461             ps_codec->s_parse.s_sao_ctxt.ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr;
3462 
3463             ihevcd_ilf_pad_frame(&ps_codec->s_parse.s_deblk_ctxt, &ps_codec->s_parse.s_sao_ctxt);
3464 
3465         }
3466 #endif
3467         ps_codec->s_parse.i4_end_of_frame = 1;
3468     }
3469     return ret;
3470 }
3471 
3472 
3473 
3474 
3475 
3476 
3477 
3478 
3479