/src/libhevc/encoder/ihevce_cabac_cu_pu.c
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Copyright (C) 2018 The Android Open Source Project |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at: |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | * |
17 | | ***************************************************************************** |
18 | | * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore |
19 | | */ |
20 | | /** |
21 | | ****************************************************************************** |
22 | | * @file ihevce_cabac_cu_pu.c |
23 | | * |
24 | | * @brief |
25 | | * This file contains function definitions for cabac entropy coding of CU |
26 | | * and PU structures in HEVC syntax |
27 | | * |
28 | | * @author |
29 | | * ittiam |
30 | | * |
31 | | * @List of Functions |
32 | | * ihevce_cabac_encode_intra_pu() |
33 | | * ihevce_cabac_encode_skip_flag() |
34 | | * ihevce_cabac_encode_part_mode() |
35 | | * ihevce_cabac_encode_merge_idx() |
36 | | * ihevce_cabac_encode_inter_pred_idc() |
37 | | * ihevce_cabac_encode_refidx() |
38 | | * ihevce_cabac_encode_mvd() |
39 | | * ihevce_cabac_encode_inter_pu() |
40 | | * ihevce_cabac_encode_coding_unit() |
41 | | * ihevce_cabac_encode_sao() |
42 | | * ihevce_encode_coding_quadtree() |
43 | | * ihevce_encode_slice_data() |
44 | | * |
45 | | ****************************************************************************** |
46 | | */ |
47 | | |
48 | | /*****************************************************************************/ |
49 | | /* File Includes */ |
50 | | /*****************************************************************************/ |
51 | | /* System include files */ |
52 | | #include <stdio.h> |
53 | | #include <string.h> |
54 | | #include <stdlib.h> |
55 | | #include <assert.h> |
56 | | #include <stdarg.h> |
57 | | #include <math.h> |
58 | | |
59 | | /* User include files */ |
60 | | #include "ihevc_typedefs.h" |
61 | | #include "itt_video_api.h" |
62 | | #include "ihevce_api.h" |
63 | | |
64 | | #include "rc_cntrl_param.h" |
65 | | #include "rc_frame_info_collector.h" |
66 | | #include "rc_look_ahead_params.h" |
67 | | |
68 | | #include "ihevc_defs.h" |
69 | | #include "ihevc_structs.h" |
70 | | #include "ihevc_platform_macros.h" |
71 | | #include "ihevc_deblk.h" |
72 | | #include "ihevc_itrans_recon.h" |
73 | | #include "ihevc_chroma_itrans_recon.h" |
74 | | #include "ihevc_chroma_intra_pred.h" |
75 | | #include "ihevc_intra_pred.h" |
76 | | #include "ihevc_inter_pred.h" |
77 | | #include "ihevc_mem_fns.h" |
78 | | #include "ihevc_padding.h" |
79 | | #include "ihevc_weighted_pred.h" |
80 | | #include "ihevc_sao.h" |
81 | | #include "ihevc_resi_trans.h" |
82 | | #include "ihevc_quant_iquant_ssd.h" |
83 | | #include "ihevc_cabac_tables.h" |
84 | | |
85 | | #include "ihevce_defs.h" |
86 | | #include "ihevce_lap_enc_structs.h" |
87 | | #include "ihevce_multi_thrd_structs.h" |
88 | | #include "ihevce_me_common_defs.h" |
89 | | #include "ihevce_had_satd.h" |
90 | | #include "ihevce_error_codes.h" |
91 | | #include "ihevce_bitstream.h" |
92 | | #include "ihevce_cabac.h" |
93 | | #include "ihevce_rdoq_macros.h" |
94 | | #include "ihevce_function_selector.h" |
95 | | #include "ihevce_enc_structs.h" |
96 | | #include "ihevce_entropy_structs.h" |
97 | | #include "ihevce_cmn_utils_instr_set_router.h" |
98 | | #include "ihevce_enc_loop_structs.h" |
99 | | #include "ihevce_trace.h" |
100 | | |
101 | | #define TEST_CABAC_BITESTIMATE 0 |
102 | | |
103 | | // clang-format off |
104 | | /** |
105 | | ****************************************************************************** |
106 | | * @brief LUT for binarization of inter partmode bins for cu size > mincu size |
107 | | * as per Table9-34 of spec |
108 | | * |
109 | | * @input : amp_enable flag and part_mode |
110 | | * |
111 | | * @output : packed bins and count of bins as per following bit packed format |
112 | | * Bins : (bits3-bit0) first bin starts from bit3 |
113 | | * Bins Count: (bits7-bit4) |
114 | | * 0xFF in the following table is invalid entry |
115 | | * |
116 | | * @remarks See Table 9-34 of HEVC spec for Binarization of part_mode |
117 | | ******************************************************************************* |
118 | | */ |
119 | | #define INVALID 0xFF |
120 | | const UWORD8 gu1_hevce_inter_part_mode_bins[2][8] = { |
121 | | |
122 | | /* cusize > minCUsize, no amp */ |
123 | | { 0x18, 0x24, 0x20, INVALID, INVALID, INVALID, INVALID, INVALID, }, |
124 | | |
125 | | /* cusize > minCUsize, amp enable, minCUsize > 8 (irrelevant) */ |
126 | | { 0x18, 0x36, 0x32, INVALID, 0x44, 0x45, 0x40, 0x41, }, |
127 | | |
128 | | }; |
129 | | |
130 | | /** |
131 | | ****************************************************************************** |
132 | | * @brief LUT for binarization of inter partmode bins for cu size = mincu size |
133 | | * as per Table9-34 of spec |
134 | | * |
135 | | * @input : mincusize==8 flag and part_mode |
136 | | * |
137 | | * @output : packed bins and count of bins as per following bit packed format |
138 | | * Bins : (bits3-bit0) first bin starts from bit3 |
139 | | * Bins Count: (bits7-bit4) |
140 | | * 0xFF in the following table is invalid entry |
141 | | * |
142 | | * @remarks See Table 9-34 of HEVC spec for Binarization of part_mode |
143 | | ******************************************************************************* |
144 | | */ |
145 | | const UWORD8 gu1_hevce_inter_part_mode_bins_mincu[2][4] = { |
146 | | |
147 | | /* cusize == minCUsize, minCUsize > 8 */ |
148 | | { 0x18, 0x24, 0x32, 0x30, }, |
149 | | |
150 | | /* cusize == minCUsize, minCUsize = 8 */ |
151 | | { 0x18, 0x24, 0x20, INVALID }, |
152 | | |
153 | | }; |
154 | | // clang-format on |
155 | | |
156 | | /*****************************************************************************/ |
157 | | /* Function Definitions */ |
158 | | /*****************************************************************************/ |
159 | | /** |
160 | | ****************************************************************************** |
161 | | * |
162 | | * @brief Entropy encoding of luma and chroma intra pred modes |
163 | | * |
164 | | * @par Description |
165 | | * Encodes prev_intra_ped_mode, mpm_idx and rem_intra_pred_mode for each |
166 | | * luma partition and chrom intra pred of cu as per section:7.3.9.1 |
167 | | * |
168 | | * Binzarization, context model as per Table 9-32 for luma |
169 | | * Binzarization, context model as per Table 9-35, section 9.3.2.8 for chroma |
170 | | * |
171 | | * @param[inout] ps_entropy_ctxt |
172 | | * pointer to entropy context (handle) |
173 | | * |
174 | | * @param[in] part_mode |
175 | | * indicates whether the mode is 2Nx2N or NxN luma parition |
176 | | * |
177 | | * @param[in] ps_enc_cu |
178 | | * pointer to the intra cu whose luma and chroma pred modes are encoded |
179 | | * |
180 | | * @return success or failure error code |
181 | | * |
182 | | ****************************************************************************** |
183 | | */ |
184 | | WORD32 ihevce_cabac_encode_intra_pu( |
185 | | entropy_context_t *ps_entropy_ctxt, WORD32 part_mode, cu_enc_loop_out_t *ps_enc_cu) |
186 | 6.33M | { |
187 | 6.33M | WORD32 error = IHEVCE_SUCCESS; |
188 | 6.33M | cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt; |
189 | 6.33M | intra_prev_rem_flags_t *ps_prev_mpm_rem_flags = &ps_enc_cu->as_prev_rem[0]; |
190 | 6.33M | WORD32 i, num_parts; |
191 | | |
192 | | /* intra can only be 2Nx2N partition or a NxN partition */ |
193 | 6.33M | num_parts = (PART_NxN == part_mode) ? 4 : 1; |
194 | | |
195 | 6.33M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
196 | 738k | { |
197 | 738k | WORD32 cu_size = ps_enc_cu->b4_cu_size << 3; |
198 | | |
199 | | /*PIC_INFO : INTRA CU in frame*/ |
200 | 738k | ps_entropy_ctxt->ps_pic_level_info->i8_total_intra_cu++; |
201 | 738k | ps_entropy_ctxt->ps_pic_level_info->i8_total_pu += num_parts; |
202 | 738k | ps_entropy_ctxt->ps_pic_level_info->i8_total_intra_pu += num_parts; |
203 | | /*PIC_INFO : Total CU in frame based on cu size */ |
204 | | |
205 | 738k | if(PART_2Nx2N == part_mode) |
206 | 562k | { |
207 | | // clang-format off |
208 | 562k | if(cu_size == 64) |
209 | 8.21k | ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_intra_pu[3]++; |
210 | 554k | else |
211 | 554k | ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_intra_pu[cu_size >> 4]++; |
212 | | // clang-format on |
213 | 562k | } |
214 | 176k | else if(PART_NxN == part_mode) |
215 | 176k | { |
216 | 176k | ps_entropy_ctxt->ps_pic_level_info->i8_total_nxn_intra_pu++; |
217 | 176k | } |
218 | 738k | } |
219 | | /* encode prev intra pred mode flags : context model based */ |
220 | 14.3M | for(i = 0; i < num_parts; i++) |
221 | 7.96M | { |
222 | 7.96M | WORD32 prev_intra_pred_flag = ps_prev_mpm_rem_flags[i].b1_prev_intra_luma_pred_flag; |
223 | 7.96M | error |= |
224 | 7.96M | ihevce_cabac_encode_bin(ps_cabac, prev_intra_pred_flag, IHEVC_CAB_INTRA_LUMA_PRED_FLAG); |
225 | 7.96M | AEV_TRACE("prev_intra_pred_luma_flag", prev_intra_pred_flag, ps_cabac->u4_range); |
226 | 7.96M | } |
227 | | |
228 | | /* encode mpm_idx or rem_intra_pred_mode bypass bins */ |
229 | 14.3M | for(i = 0; i < num_parts; i++) |
230 | 7.96M | { |
231 | 7.96M | if(ps_prev_mpm_rem_flags[i].b1_prev_intra_luma_pred_flag) |
232 | 6.25M | { |
233 | 6.25M | WORD32 mpm_idx = ps_prev_mpm_rem_flags[i].b2_mpm_idx; |
234 | | |
235 | | /* tunary bins for cmax = 2 */ |
236 | 6.25M | WORD32 num_bins = mpm_idx ? 2 : 1; |
237 | 6.25M | UWORD32 bins = mpm_idx ? ((1 << 1) | (mpm_idx - 1)) : 0; |
238 | | |
239 | 6.25M | ASSERT(mpm_idx < 3); |
240 | | |
241 | 6.25M | error |= ihevce_cabac_encode_bypass_bins(ps_cabac, bins, num_bins); |
242 | 6.25M | AEV_TRACE("mpm_idx", mpm_idx, ps_cabac->u4_range); |
243 | 6.25M | } |
244 | 1.70M | else |
245 | 1.70M | { |
246 | 1.70M | WORD32 rem_intra_pred_mode = ps_prev_mpm_rem_flags[i].b5_rem_intra_pred_mode; |
247 | 1.70M | error |= ihevce_cabac_encode_bypass_bins(ps_cabac, rem_intra_pred_mode, 5); |
248 | 1.70M | AEV_TRACE("rem_intra_luma_pred_mode", rem_intra_pred_mode, ps_cabac->u4_range); |
249 | 1.70M | } |
250 | 7.96M | } |
251 | | |
252 | | /************************************************************************/ |
253 | | /* encode the chroma intra prediction mode as per Table 9-35 */ |
254 | | /* First bin is context model based prefix : 0 if chroma_mode==4 else 1 */ |
255 | | /* If chroma pred mode is not 4, suffix bins are coded as bypass bins */ |
256 | | /************************************************************************/ |
257 | 6.33M | { |
258 | 6.33M | WORD32 chroma_pred_mode = ps_enc_cu->b3_chroma_intra_pred_mode; |
259 | 6.33M | WORD32 prefix_bin = (chroma_pred_mode == 4) ? 0 : 1; |
260 | | |
261 | | /* encode prefix bin */ |
262 | 6.33M | error |= ihevce_cabac_encode_bin(ps_cabac, prefix_bin, IHEVC_CAB_CHROMA_PRED_MODE); |
263 | | |
264 | | /* encode suffix bins */ |
265 | 6.33M | if(prefix_bin) |
266 | 313k | { |
267 | 313k | error |= ihevce_cabac_encode_bypass_bins(ps_cabac, chroma_pred_mode, 2); |
268 | 313k | } |
269 | 6.33M | AEV_TRACE("intra_chroma_pred_mode", chroma_pred_mode, ps_cabac->u4_range); |
270 | 6.33M | } |
271 | | |
272 | 6.33M | return (error); |
273 | 6.33M | } |
274 | | |
275 | | /** |
276 | | ****************************************************************************** |
277 | | * |
278 | | * @brief Entropy encoding of skip flag (Coding Unit syntax) |
279 | | * |
280 | | * @par Description |
281 | | * context increment for skip flag is derived based on left and top skip flag |
282 | | * as per section 9.3.3.1.1, Table 9-38 |
283 | | * |
284 | | * @param[inout] ps_entropy_ctxt |
285 | | * pointer to entropy context (handle) |
286 | | * |
287 | | * @param[in] ps_enc_cu |
288 | | * pointer to inter cu whose skip flag is to be coded |
289 | | * |
290 | | * @param[in] top_avail |
291 | | * top availabilty flag for current cu (boolean) |
292 | | * |
293 | | * @param[in] left_avail |
294 | | * left availabilty flag for current cu (boolean) |
295 | | * |
296 | | * @return success or failure error code |
297 | | * |
298 | | ****************************************************************************** |
299 | | */ |
300 | | WORD32 ihevce_cabac_encode_skip_flag( |
301 | | entropy_context_t *ps_entropy_ctxt, |
302 | | cu_enc_loop_out_t *ps_enc_cu, |
303 | | WORD32 top_avail, |
304 | | WORD32 left_avail) |
305 | | |
306 | 5.92M | { |
307 | 5.92M | WORD32 error = IHEVCE_SUCCESS; |
308 | 5.92M | WORD32 skip_flag = ps_enc_cu->b1_skip_flag; |
309 | 5.92M | cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt; |
310 | | |
311 | | /* CU top left co-ordinates w.r.t ctb */ |
312 | 5.92M | WORD32 cu_x0 = ps_enc_cu->b3_cu_pos_x << 3; |
313 | 5.92M | WORD32 cu_y0 = ps_enc_cu->b3_cu_pos_y << 3; |
314 | | |
315 | | /* CU size in pels */ |
316 | 5.92M | WORD32 cu_size = ps_enc_cu->b4_cu_size << 3; |
317 | | |
318 | | /* CU x co-ordinate w.r.t frame start */ |
319 | 5.92M | WORD32 ctb_x0_frm = (ps_entropy_ctxt->i4_ctb_x << ps_entropy_ctxt->i1_log2_ctb_size); |
320 | | |
321 | 5.92M | WORD32 cu_x0_frm = cu_x0 + ctb_x0_frm; |
322 | | |
323 | | /* bit postion from where top skip flag is extracted; 1bit per 8 pel */ |
324 | 5.92M | WORD32 x_pos = ((cu_x0_frm >> 3) & 0x7); |
325 | | |
326 | | /* bit postion from where left skip flag is extracted; 1bit per 8 pel */ |
327 | 5.92M | WORD32 y_pos = ((cu_y0 >> 3) & 0x7); |
328 | | |
329 | | /* top and left skip flags computed based on nbr availability */ |
330 | 5.92M | UWORD8 *pu1_top_skip_flags = ps_entropy_ctxt->pu1_skip_cu_top + (cu_x0_frm >> 6); |
331 | 5.92M | UWORD32 u4_skip_left_flags = ps_entropy_ctxt->u4_skip_cu_left; |
332 | | |
333 | | /* context incerements based on top and left neigbours */ |
334 | 5.92M | UWORD32 ctxt_inc = 0; |
335 | | |
336 | 5.92M | if(top_avail) |
337 | 4.60M | { |
338 | 4.60M | WORD32 val; |
339 | 4.60M | EXTRACT_BIT(val, pu1_top_skip_flags[0], x_pos); |
340 | 4.60M | ctxt_inc += val; |
341 | 4.60M | } |
342 | | |
343 | 5.92M | if(left_avail) |
344 | 4.57M | { |
345 | 4.57M | WORD32 val; |
346 | 4.57M | EXTRACT_BIT(val, u4_skip_left_flags, y_pos); |
347 | 4.57M | ctxt_inc += val; |
348 | 4.57M | } |
349 | | |
350 | 5.92M | if(CABAC_MODE_COMPUTE_BITS == ps_cabac->e_cabac_op_mode) |
351 | 5.23M | { |
352 | | //ASSERT(ctxt_inc == ps_entropy_ctxt->i4_num_nbr_skip_cus); |
353 | 5.23M | ctxt_inc = ps_entropy_ctxt->i4_num_nbr_skip_cus; |
354 | 5.23M | ASSERT(ctxt_inc < 3); |
355 | 5.23M | ASSERT((WORD32)ctxt_inc <= (top_avail + left_avail)); |
356 | 5.23M | } |
357 | | |
358 | | /* encode the skip flag */ |
359 | 5.92M | error |= ihevce_cabac_encode_bin(ps_cabac, skip_flag, (IHEVC_CAB_SKIP_FLAG + ctxt_inc)); |
360 | | |
361 | 5.92M | AEV_TRACE("cu_skip_flag", skip_flag, ps_cabac->u4_range); |
362 | | |
363 | 5.92M | if(CABAC_MODE_ENCODE_BITS == ps_cabac->e_cabac_op_mode) |
364 | 688k | { |
365 | | /* update top and left skip flags only in encode mode */ |
366 | 688k | if(skip_flag) |
367 | 361k | { |
368 | 361k | SET_BITS(pu1_top_skip_flags[0], x_pos, (cu_size >> 3)); |
369 | 361k | SET_BITS(u4_skip_left_flags, y_pos, (cu_size >> 3)); |
370 | 361k | } |
371 | 326k | else |
372 | 326k | { |
373 | 326k | CLEAR_BITS(pu1_top_skip_flags[0], x_pos, (cu_size >> 3)); |
374 | 326k | CLEAR_BITS(u4_skip_left_flags, y_pos, (cu_size >> 3)); |
375 | 326k | } |
376 | | |
377 | 688k | ps_entropy_ctxt->u4_skip_cu_left = u4_skip_left_flags; |
378 | 688k | } |
379 | | |
380 | 5.92M | return (error); |
381 | 5.92M | } |
382 | | |
383 | | /** |
384 | | ****************************************************************************** |
385 | | * |
386 | | * @brief Entropy encoding of partition mode (Coding Unit syntax) |
387 | | * |
388 | | * @par Description |
389 | | * Binarization process and context modelling of partition mode is done as per |
390 | | * section 9.3.2.6 (Table 9-34) and se |
391 | | * |
392 | | * @param[inout] ps_cabac |
393 | | * pointer to cabac encoding context (handle) |
394 | | * |
395 | | * @param[in] intra |
396 | | * boolean indicating if current cu is intra cu |
397 | | * |
398 | | * @param[in] is_mincu |
399 | | * boolean indicating if current cu size is equal to mincu |
400 | | * |
401 | | * @param[in] amp_enabled |
402 | | * flag to indicate if AMP(Assymetric motion partition) is enabled at sps level |
403 | | * |
404 | | * @param[in] cu_eq_8 |
405 | | * boolean indicating if current cu size is equal to 8 |
406 | | * |
407 | | * @param[in] part_mode |
408 | | * partition mode of current CU |
409 | | * |
410 | | * @return success or failure error code |
411 | | * |
412 | | ****************************************************************************** |
413 | | */ |
414 | | WORD32 ihevce_cabac_encode_part_mode( |
415 | | cab_ctxt_t *ps_cabac, |
416 | | WORD32 intra, |
417 | | WORD32 is_mincu, |
418 | | WORD32 amp_enabled, |
419 | | WORD32 cu_eq_8, |
420 | | WORD32 part_mode) |
421 | 4.45M | { |
422 | | /* Binarization depends on intra/inter, is_mincu, amp flag, cbsize == 8 */ |
423 | 4.45M | WORD32 bins; |
424 | 4.45M | WORD32 bin_count, i; |
425 | 4.45M | WORD32 error = IHEVCE_SUCCESS; |
426 | | |
427 | 4.45M | (void)is_mincu; |
428 | 4.45M | (void)amp_enabled; |
429 | 4.45M | (void)cu_eq_8; |
430 | 4.45M | if(intra) |
431 | 3.01M | { |
432 | | /* sanity checks for intra part mode */ |
433 | 3.01M | ASSERT(is_mincu); |
434 | 3.01M | ASSERT((part_mode == SIZE_NxN) || (part_mode == SIZE_2Nx2N)); |
435 | | |
436 | 3.01M | bins = (part_mode == SIZE_2Nx2N) ? 1 : 0; |
437 | 3.01M | error |= ihevce_cabac_encode_bin(ps_cabac, bins, IHEVC_CAB_PART_MODE); |
438 | 3.01M | } |
439 | 1.44M | else |
440 | 1.44M | { |
441 | | /* sanity checks for inter part mode....Too many but good to have */ |
442 | 1.44M | ASSERT((amp_enabled == 0) || (amp_enabled == 1)); |
443 | 1.44M | ASSERT((is_mincu == 0) || (is_mincu == 1)); |
444 | 1.44M | ASSERT((cu_eq_8 == 0) || (cu_eq_8 == 1)); |
445 | 1.44M | ASSERT((part_mode <= SIZE_nRx2N) && (part_mode >= SIZE_2Nx2N)); |
446 | 1.44M | if(!amp_enabled) |
447 | 1.44M | ASSERT(part_mode <= SIZE_NxN); |
448 | 1.44M | if(!is_mincu) |
449 | 1.44M | ASSERT(part_mode != SIZE_NxN); |
450 | 1.44M | if(is_mincu) |
451 | 1.44M | ASSERT(part_mode <= SIZE_NxN); |
452 | 1.44M | if(cu_eq_8) |
453 | 1.44M | ASSERT(part_mode < SIZE_NxN); |
454 | 1.44M | if(cu_eq_8) |
455 | 1.44M | ASSERT(is_mincu); |
456 | | |
457 | | /* look up table for bins and number of bins for inter pred mode */ |
458 | 1.44M | if(!is_mincu) |
459 | 1.00M | { |
460 | 1.00M | bins = gu1_hevce_inter_part_mode_bins[amp_enabled][part_mode]; |
461 | 1.00M | } |
462 | 437k | else |
463 | 437k | { |
464 | 437k | bins = gu1_hevce_inter_part_mode_bins_mincu[cu_eq_8][part_mode]; |
465 | 437k | } |
466 | | |
467 | 1.44M | bin_count = (bins >> 4) & 0xF; |
468 | | |
469 | | /* Encode the context model based bins, max of 3 */ |
470 | 3.83M | for(i = 0; i < MIN(bin_count, 3); i++) |
471 | 2.38M | { |
472 | | //TODO: HM-8.0-dev uses 0 context increment for bin2 (i===2) when amp is enabled |
473 | 2.38M | WORD32 ctxt_inc = IHEVC_CAB_PART_MODE + i; |
474 | 2.38M | WORD32 bin = (bins >> (3 - i)) & 0x1; |
475 | 2.38M | error |= ihevce_cabac_encode_bin(ps_cabac, bin, ctxt_inc); |
476 | 2.38M | } |
477 | | |
478 | | /* Encode the last bin as bypass bin for amp partitions */ |
479 | 1.44M | if(bin_count == 4) |
480 | 397k | { |
481 | 397k | error |= ihevce_cabac_encode_bypass_bin(ps_cabac, (bins & 0x1)); |
482 | 397k | } |
483 | 1.44M | } |
484 | 4.45M | AEV_TRACE("part_mode", part_mode, ps_cabac->u4_range); |
485 | 4.45M | return (error); |
486 | 4.45M | } |
487 | | |
488 | | /** |
489 | | ****************************************************************************** |
490 | | * |
491 | | * @brief Entropy encoding of merge_idx of inter prediction unit as per sec |
492 | | * as per sec 9.3.2 Table9-32. (tunary binarization) |
493 | | * |
494 | | * @par Description |
495 | | * trunacted unary binarization is done based on max merge candidates |
496 | | * First bin is context modelled bin and the rest are coded as bypass |
497 | | * |
498 | | * @param[inout] ps_cabac |
499 | | * pointer to cabac encoding context (handle) |
500 | | * |
501 | | * @param[in] merge_idx |
502 | | * merge idx of the pu to be encoded; |
503 | | * |
504 | | * @param[in] max_merge_cand |
505 | | * maximum merge candidates signalled in the slice header* |
506 | | * |
507 | | * @return success or failure error code |
508 | | * |
509 | | ****************************************************************************** |
510 | | */ |
511 | | WORD32 ihevce_cabac_encode_merge_idx(cab_ctxt_t *ps_cabac, WORD32 merge_idx, WORD32 max_merge_cand) |
512 | 2.99M | { |
513 | 2.99M | WORD32 ret = IHEVCE_SUCCESS; |
514 | 2.99M | WORD32 ctxt_inc = IHEVC_CAB_MERGE_IDX_EXT; |
515 | | |
516 | | /* sanity checks */ |
517 | 2.99M | ASSERT((merge_idx >= 0) && (merge_idx < max_merge_cand)); |
518 | | |
519 | | /* encode the merge idx only if required */ |
520 | 2.99M | if(max_merge_cand > 1) |
521 | 2.99M | { |
522 | | /* encode the context modelled first bin */ |
523 | 2.99M | ret |= ihevce_cabac_encode_bin(ps_cabac, (merge_idx > 0), ctxt_inc); |
524 | | |
525 | | /* encode the remaining bins as bypass tunary */ |
526 | 2.99M | if((max_merge_cand > 2) && (merge_idx > 0)) |
527 | 64.3k | { |
528 | 64.3k | ret |= |
529 | 64.3k | ihevce_cabac_encode_tunary_bypass(ps_cabac, (merge_idx - 1), (max_merge_cand - 2)); |
530 | 64.3k | } |
531 | | |
532 | 2.99M | AEV_TRACE("merge_idx", merge_idx, ps_cabac->u4_range); |
533 | 2.99M | } |
534 | | |
535 | 2.99M | return (ret); |
536 | 2.99M | } |
537 | | |
538 | | /** |
539 | | ****************************************************************************** |
540 | | * |
541 | | * @brief Entropy encoding of inter_pred_idc for prediction unit of B slice as |
542 | | * per sec 9.3.2.9 Table9-36 |
543 | | * |
544 | | * @par Description |
545 | | * Max of two context modelled bins coded for pu size > 8x4 or 4x8 |
546 | | * one context modelled bin coded for pu size = 8x4 or 4x8; bipred not allowed |
547 | | * for 8x4 or 4x8. |
548 | | * |
549 | | * @param[inout] ps_cabac |
550 | | * pointer to cabac encoding context (handle) |
551 | | * |
552 | | * @param[in] inter_pred_idc |
553 | | * inter pred mode to be encoded; shall be PRED_L0 or PRED_L1 or PRED_BI |
554 | | * |
555 | | * @param[in] cu_depth |
556 | | * depth of the cu to which current pu belongs (required for context increment) |
557 | | * |
558 | | * @param[in] pu_w_plus_pu_h |
559 | | * required to check if pu_w_plus_pu_h is 12 (8x4PU or 4x8PU) |
560 | | * |
561 | | * @return success or failure error code |
562 | | * |
563 | | ****************************************************************************** |
564 | | */ |
565 | | WORD32 ihevce_cabac_encode_inter_pred_idc( |
566 | | cab_ctxt_t *ps_cabac, WORD32 inter_pred_idc, WORD32 cu_depth, WORD32 pu_w_plus_pu_h) |
567 | 296k | { |
568 | 296k | WORD32 ret = IHEVCE_SUCCESS; |
569 | 296k | WORD32 ctxt_inc; |
570 | | |
571 | 296k | ASSERT(inter_pred_idc <= PRED_BI); |
572 | | |
573 | | /* check if PU is 8x4/4x8 */ |
574 | 296k | if(pu_w_plus_pu_h == 12) |
575 | 0 | { |
576 | | /* case of 8x4 or 4x8 where bi_pred is not allowed */ |
577 | 0 | ASSERT((inter_pred_idc == PRED_L0) || (inter_pred_idc == PRED_L1)); |
578 | |
|
579 | 0 | ctxt_inc = IHEVC_CAB_INTER_PRED_IDC + 4; |
580 | 0 | ret |= ihevce_cabac_encode_bin(ps_cabac, inter_pred_idc, ctxt_inc); |
581 | 0 | } |
582 | 296k | else |
583 | 296k | { |
584 | | /* larger PUs can be encoded as bi_pred/l0/l1 inter_pred_idc */ |
585 | 296k | WORD32 is_bipred = (inter_pred_idc == PRED_BI); |
586 | | |
587 | 296k | ctxt_inc = IHEVC_CAB_INTER_PRED_IDC + cu_depth; |
588 | 296k | ret |= ihevce_cabac_encode_bin(ps_cabac, is_bipred, ctxt_inc); |
589 | | |
590 | 296k | if(!is_bipred) |
591 | 263k | { |
592 | 263k | ctxt_inc = IHEVC_CAB_INTER_PRED_IDC + 4; |
593 | 263k | ret |= ihevce_cabac_encode_bin(ps_cabac, inter_pred_idc, ctxt_inc); |
594 | 263k | } |
595 | 296k | } |
596 | | |
597 | 296k | AEV_TRACE("inter_pred_idc", inter_pred_idc, ps_cabac->u4_range); |
598 | | |
599 | 296k | return (ret); |
600 | 296k | } |
601 | | |
602 | | /** |
603 | | ****************************************************************************** |
604 | | * |
605 | | * @brief Entropy encoding of refidx for prediction unit; Binarization done as |
606 | | * tunary code as per sec 9.3.2 Table9-32 |
607 | | * |
608 | | * @par Description |
609 | | * First two bins are context modelled while the rest are coded as bypass |
610 | | * |
611 | | * @param[inout] ps_cabac |
612 | | * pointer to cabac encoding context (handle) |
613 | | * |
614 | | * @param[in] ref_idx |
615 | | * ref idx of partition unit |
616 | | * |
617 | | * @param[in] active_refs |
618 | | * max number of active references signalled in slice header |
619 | | * |
620 | | * @return success or failure error code |
621 | | * |
622 | | ****************************************************************************** |
623 | | */ |
624 | | WORD32 ihevce_cabac_encode_refidx(cab_ctxt_t *ps_cabac, WORD32 ref_idx, WORD32 active_refs) |
625 | 1.05M | { |
626 | | /************************************************************/ |
627 | | /* encode ref_idx as tunary binarization Table 9-32 */ |
628 | | /* First 2 bin use context model and rest coded as bypass */ |
629 | | /************************************************************/ |
630 | 1.05M | WORD32 ret = IHEVCE_SUCCESS; |
631 | 1.05M | WORD32 ctxt_inc = IHEVC_CAB_INTER_REF_IDX; |
632 | | |
633 | | /* sanity checks */ |
634 | 1.05M | ASSERT((ref_idx >= 0) && (ref_idx < active_refs)); |
635 | | |
636 | | /* encode the ref idx only if required */ |
637 | 1.05M | if(active_refs > 1) |
638 | 628k | { |
639 | | /* encode the context modelled first bin */ |
640 | 628k | ret |= ihevce_cabac_encode_bin(ps_cabac, (ref_idx > 0), ctxt_inc); |
641 | | |
642 | 628k | if((active_refs > 2) && (ref_idx > 0)) |
643 | 85.3k | { |
644 | | /* encode the context modelled second bin */ |
645 | 85.3k | ctxt_inc++; |
646 | 85.3k | ret |= ihevce_cabac_encode_bin(ps_cabac, (ref_idx > 1), ctxt_inc); |
647 | 85.3k | } |
648 | | |
649 | 628k | if((active_refs > 3) && (ref_idx > 1)) |
650 | 35.8k | { |
651 | | /* encode remaining bypass bins */ |
652 | 35.8k | ret |= ihevce_cabac_encode_tunary_bypass(ps_cabac, (ref_idx - 2), (active_refs - 3)); |
653 | 35.8k | } |
654 | | |
655 | 628k | AEV_TRACE("ref_idx", ref_idx, ps_cabac->u4_range); |
656 | 628k | } |
657 | | |
658 | 1.05M | return (ret); |
659 | 1.05M | } |
660 | | |
661 | | /** |
662 | | ****************************************************************************** |
663 | | * |
664 | | * @brief Entropy encoding of mvd for inter pu as per section 7.3.10.2 |
665 | | * |
666 | | * @par Description |
667 | | * syntax coded as per section 7.3.10.2 for mvdx and mvdy |
668 | | * context modeling of abs_mvd_greater0 abs_mvd_greater1 done as per Table 9-32 |
669 | | * binazrization of abs_mvd_minus2 is done as done as EG1 code section 9.3.2.4 |
670 | | * |
671 | | * @param[inout] ps_cabac |
672 | | * pointer to cabac encoding context (handle) |
673 | | * |
674 | | * @param[in] ps_mvd |
675 | | * pointer to mvd struct containing mvdx and mvdy |
676 | | * |
677 | | * @return success or failure error code |
678 | | * |
679 | | ****************************************************************************** |
680 | | */ |
681 | | WORD32 ihevce_cabac_encode_mvd(cab_ctxt_t *ps_cabac, mv_t *ps_mvd) |
682 | 1.05M | { |
683 | 1.05M | WORD32 ret = IHEVCE_SUCCESS; |
684 | 1.05M | WORD32 mvd_x = ps_mvd->i2_mvx; |
685 | 1.05M | WORD32 mvd_y = ps_mvd->i2_mvy; |
686 | | |
687 | 1.05M | WORD32 abs_mvd_x = ABS(mvd_x); |
688 | 1.05M | WORD32 abs_mvd_y = ABS(mvd_y); |
689 | | |
690 | 1.05M | WORD32 abs_mvd_x_gt0 = abs_mvd_x > 0; |
691 | 1.05M | WORD32 abs_mvd_y_gt0 = abs_mvd_y > 0; |
692 | | |
693 | 1.05M | WORD32 abs_mvd_x_gt1 = abs_mvd_x > 1; |
694 | 1.05M | WORD32 abs_mvd_y_gt1 = abs_mvd_y > 1; |
695 | | |
696 | 1.05M | WORD32 ctxt_inc = IHEVC_CAB_MVD_GRT0; |
697 | | |
698 | | /* encode absmvd_x > 0 */ |
699 | 1.05M | ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_x_gt0, ctxt_inc); |
700 | 1.05M | AEV_TRACE("abs_mvd_greater0_flag[0]", abs_mvd_x_gt0, ps_cabac->u4_range); |
701 | | |
702 | | /* encode absmvd_y > 0 */ |
703 | 1.05M | ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_y_gt0, ctxt_inc); |
704 | 1.05M | AEV_TRACE("abs_mvd_greater0_flag[1]", abs_mvd_y_gt0, ps_cabac->u4_range); |
705 | | |
706 | 1.05M | ctxt_inc = IHEVC_CAB_MVD_GRT1; |
707 | | |
708 | | /* encode abs_mvd_x > 1 iff (abs_mvd_x > 0) */ |
709 | 1.05M | if(abs_mvd_x_gt0) |
710 | 597k | { |
711 | 597k | ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_x_gt1, ctxt_inc); |
712 | 597k | AEV_TRACE("abs_mvd_greater1_flag[0]", abs_mvd_x_gt1, ps_cabac->u4_range); |
713 | 597k | } |
714 | | |
715 | | /* encode abs_mvd_y > 1 iff (abs_mvd_y > 0) */ |
716 | 1.05M | if(abs_mvd_y_gt0) |
717 | 593k | { |
718 | 593k | ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_y_gt1, ctxt_inc); |
719 | 593k | AEV_TRACE("abs_mvd_greater1_flag[1]", abs_mvd_y_gt1, ps_cabac->u4_range); |
720 | 593k | } |
721 | | |
722 | | /* encode abs_mvd_x - 2 iff (abs_mvd_x > 1) */ |
723 | 1.05M | if(abs_mvd_x_gt1) |
724 | 523k | { |
725 | 523k | ret |= ihevce_cabac_encode_egk(ps_cabac, (abs_mvd_x - 2), 1); |
726 | 523k | AEV_TRACE("abs_mvd_minus2[0]", (abs_mvd_x - 2), ps_cabac->u4_range); |
727 | 523k | } |
728 | | |
729 | | /* encode mvd_x sign iff (abs_mvd_x > 0) */ |
730 | 1.05M | if(abs_mvd_x_gt0) |
731 | 597k | { |
732 | 597k | ret |= ihevce_cabac_encode_bypass_bin(ps_cabac, (mvd_x < 0)); |
733 | 597k | AEV_TRACE("mvd_sign_flag[0]", (mvd_x < 0), ps_cabac->u4_range); |
734 | 597k | } |
735 | | |
736 | | /* encode abs_mvd_y - 2 iff (abs_mvd_y > 1) */ |
737 | 1.05M | if(abs_mvd_y_gt1) |
738 | 513k | { |
739 | 513k | ret |= ihevce_cabac_encode_egk(ps_cabac, (abs_mvd_y - 2), 1); |
740 | 513k | AEV_TRACE("abs_mvd_minus2[1]", (abs_mvd_y - 2), ps_cabac->u4_range); |
741 | 513k | } |
742 | | |
743 | | /* encode mvd_y sign iff (abs_mvd_y > 0) */ |
744 | 1.05M | if(abs_mvd_y_gt0) |
745 | 593k | { |
746 | 593k | ret |= ihevce_cabac_encode_bypass_bin(ps_cabac, (mvd_y < 0)); |
747 | 593k | AEV_TRACE("mvd_sign_flag[1]", (mvd_y < 0), ps_cabac->u4_range); |
748 | 593k | } |
749 | | |
750 | 1.05M | return ret; |
751 | 1.05M | } |
752 | | |
753 | | /** |
754 | | ****************************************************************************** |
755 | | * |
756 | | * @brief Entropy encoding of all syntax elements of inter PUs in a CU |
757 | | * |
758 | | * @par Description |
759 | | * syntax coded as per section 7.3.10.1 for inter prediction unit |
760 | | * |
761 | | * @param[inout] ps_entropy_ctxt |
762 | | * pointer to entropy context (handle) |
763 | | * |
764 | | * @param[in] ps_enc_cu |
765 | | * pointer to current cu whose inter prediction units are to be encoded |
766 | | * |
767 | | * @param[in] cu_depth |
768 | | * depth of the the current cu in coding tree |
769 | | * |
770 | | * @return success or failure error code |
771 | | * |
772 | | ****************************************************************************** |
773 | | */ |
774 | | WORD32 ihevce_cabac_encode_inter_pu( |
775 | | entropy_context_t *ps_entropy_ctxt, cu_enc_loop_out_t *ps_enc_cu, WORD32 cu_depth) |
776 | 3.54M | { |
777 | 3.54M | WORD32 ret = IHEVCE_SUCCESS; |
778 | | |
779 | 3.54M | slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr; |
780 | 3.54M | cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt; |
781 | 3.54M | pu_t *ps_pu = ps_enc_cu->ps_pu; |
782 | | |
783 | 3.54M | WORD32 merge_idx = ps_pu->b3_merge_idx; |
784 | 3.54M | WORD32 max_merge_cand = ps_slice_hdr->i1_max_num_merge_cand; |
785 | 3.54M | WORD32 ctxt_inc; |
786 | | |
787 | 3.54M | if(ps_enc_cu->b1_skip_flag) |
788 | 2.10M | { |
789 | 2.10M | WORD32 cu_size = ps_enc_cu->b4_cu_size << 3; |
790 | | /*PIC_INFO : SKIP CU in frame*/ |
791 | 2.10M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
792 | 361k | { |
793 | 361k | ps_entropy_ctxt->ps_pic_level_info->i8_total_skip_cu++; |
794 | 361k | ps_entropy_ctxt->ps_pic_level_info->i8_total_pu++; |
795 | 361k | if(cu_size == 64) |
796 | 4.86k | ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[3]++; |
797 | 357k | else |
798 | 357k | ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[cu_size >> 4]++; |
799 | 361k | } |
800 | | /* encode the merge idx for skip cu and return */ |
801 | 2.10M | ret |= ihevce_cabac_encode_merge_idx(ps_cabac, merge_idx, max_merge_cand); |
802 | 2.10M | } |
803 | 1.44M | else |
804 | 1.44M | { |
805 | | /* MODE_INTER */ |
806 | 1.44M | WORD32 part_mode = ps_enc_cu->b3_part_mode; |
807 | 1.44M | WORD32 num_parts, i; |
808 | | |
809 | 1.44M | num_parts = (part_mode == SIZE_2Nx2N) ? 1 : ((part_mode == SIZE_NxN) ? 4 : 2); |
810 | | |
811 | | /*PIC_INFO : INTER CU in frame*/ |
812 | 1.44M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
813 | 158k | { |
814 | 158k | WORD32 cu_size = ps_enc_cu->b4_cu_size << 3; |
815 | 158k | ps_entropy_ctxt->ps_pic_level_info->i8_total_inter_cu++; |
816 | 158k | ps_entropy_ctxt->ps_pic_level_info->i8_total_pu += num_parts; |
817 | | |
818 | | // clang-format off |
819 | 158k | if(PART_2Nx2N == part_mode) |
820 | 133k | { |
821 | 133k | if(cu_size == 64) |
822 | 5.43k | ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[3]++; |
823 | 127k | else |
824 | 127k | ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[cu_size >> 4]++; |
825 | 133k | } |
826 | 25.4k | else if((PART_2NxN == part_mode) || (PART_Nx2N == part_mode)) |
827 | 4.59k | { |
828 | 4.59k | if(cu_size == 64) |
829 | 126 | ps_entropy_ctxt->ps_pic_level_info->i8_total_smp_inter_pu[3]++; |
830 | 4.46k | else |
831 | 4.46k | ps_entropy_ctxt->ps_pic_level_info->i8_total_smp_inter_pu[cu_size >> 4]++; |
832 | 4.59k | } |
833 | 20.8k | else if((PART_2NxnU == part_mode) || (PART_2NxnD == part_mode) || |
834 | 4.85k | (PART_nLx2N == part_mode) || (PART_nRx2N == part_mode)) |
835 | 20.8k | { |
836 | 20.8k | ps_entropy_ctxt->ps_pic_level_info->i8_total_amp_inter_pu[cu_size >> 5]++; |
837 | 20.8k | } |
838 | 0 | else |
839 | 0 | { |
840 | 0 | ps_entropy_ctxt->ps_pic_level_info->i8_total_nxn_inter_pu[cu_size >> 5]++; |
841 | 0 | } |
842 | | // clang-format on |
843 | 158k | } |
844 | | |
845 | | /* encode each pu partition */ |
846 | 3.35M | for(i = 0; i < num_parts; i++) |
847 | 1.91M | { |
848 | | /* encode the merge flag context modelled bin */ |
849 | 1.91M | WORD32 merge_flag; |
850 | 1.91M | UWORD32 u4_bits_estimated_merge_flag = 0; |
851 | 1.91M | ps_pu = ps_enc_cu->ps_pu + i; |
852 | | |
853 | | /* encode the merge flag context modelled bin */ |
854 | 1.91M | merge_flag = ps_pu->b1_merge_flag; |
855 | 1.91M | u4_bits_estimated_merge_flag = ps_cabac->u4_bits_estimated_q12; |
856 | 1.91M | ctxt_inc = IHEVC_CAB_MERGE_FLAG_EXT; |
857 | 1.91M | ret |= ihevce_cabac_encode_bin(ps_cabac, merge_flag, ctxt_inc); |
858 | | |
859 | 1.91M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
860 | 184k | { |
861 | | // clang-format off |
862 | | /*PIC INFO : Populate merge flag */ |
863 | 184k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_merge_flag = |
864 | 184k | (ps_cabac->u4_bits_estimated_q12 - |
865 | 184k | u4_bits_estimated_merge_flag); |
866 | | // clang-format on |
867 | 184k | } |
868 | 1.91M | AEV_TRACE("merge_flag", merge_flag, ps_cabac->u4_range); |
869 | | |
870 | 1.91M | if(merge_flag) |
871 | 897k | { |
872 | 897k | merge_idx = ps_pu->b3_merge_idx; |
873 | 897k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
874 | 102k | ps_entropy_ctxt->ps_pic_level_info->i8_total_merge_pu++; |
875 | | /* encode the merge idx for the pu */ |
876 | 897k | ret |= ihevce_cabac_encode_merge_idx(ps_cabac, merge_idx, max_merge_cand); |
877 | 897k | } |
878 | 1.01M | else |
879 | 1.01M | { |
880 | | /* encode the inter_pred_idc, ref_idx and mvd */ |
881 | 1.01M | WORD32 inter_pred_idc = ps_pu->b2_pred_mode; |
882 | 1.01M | WORD32 ref_l0_active = ps_slice_hdr->i1_num_ref_idx_l0_active; |
883 | 1.01M | WORD32 ref_l1_active = ps_slice_hdr->i1_num_ref_idx_l1_active; |
884 | | |
885 | | /*PIC_INFO : L0 L1 BI ro r1.. in frame*/ |
886 | 1.01M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
887 | 81.4k | { |
888 | 81.4k | ps_entropy_ctxt->ps_pic_level_info->i8_total_non_skipped_inter_pu++; |
889 | | // clang-format off |
890 | 81.4k | if(inter_pred_idc == PRED_L0) |
891 | 73.9k | { |
892 | 73.9k | ps_entropy_ctxt->ps_pic_level_info->i8_total_L0_mode++; |
893 | 73.9k | ps_entropy_ctxt->ps_pic_level_info->i8_total_L0_ref_idx[ps_pu->mv.i1_l0_ref_idx]++; |
894 | 73.9k | } |
895 | 7.42k | else if(inter_pred_idc == PRED_L1) |
896 | 4.57k | { |
897 | 4.57k | ps_entropy_ctxt->ps_pic_level_info->i8_total_L1_mode++; |
898 | 4.57k | ps_entropy_ctxt->ps_pic_level_info->i8_total_L1_ref_idx[ps_pu->mv.i1_l1_ref_idx]++; |
899 | 4.57k | } |
900 | 2.84k | else if(inter_pred_idc == PRED_BI) |
901 | 2.84k | { |
902 | 2.84k | ps_entropy_ctxt->ps_pic_level_info->i8_total_BI_mode++; |
903 | 2.84k | if(inter_pred_idc != PRED_L1) |
904 | 2.84k | ps_entropy_ctxt->ps_pic_level_info->i8_total_L0_ref_idx[ps_pu->mv.i1_l0_ref_idx]++; |
905 | 2.84k | if(inter_pred_idc != PRED_L0) |
906 | 2.84k | ps_entropy_ctxt->ps_pic_level_info->i8_total_L1_ref_idx[ps_pu->mv.i1_l1_ref_idx]++; |
907 | 2.84k | } |
908 | | // clang-format on |
909 | 81.4k | } |
910 | 1.01M | if(ps_slice_hdr->i1_slice_type == BSLICE) |
911 | 296k | { |
912 | | /* Encode inter_pred_idc as per sec 9.3.2.9 Table9-36 */ |
913 | 296k | WORD32 pu_w_plus_pu_h; |
914 | 296k | WORD32 inter_pred_idc = ps_pu->b2_pred_mode; |
915 | | |
916 | | /* required to check if w+h==12 case */ |
917 | 296k | pu_w_plus_pu_h = ((ps_pu->b4_wd + 1) << 2) + ((ps_pu->b4_ht + 1) << 2); |
918 | | |
919 | 296k | ret |= ihevce_cabac_encode_inter_pred_idc( |
920 | 296k | ps_cabac, inter_pred_idc, cu_depth, pu_w_plus_pu_h); |
921 | 296k | } |
922 | 721k | else |
923 | 721k | { |
924 | 721k | ASSERT(inter_pred_idc == 0); |
925 | 721k | } |
926 | | |
927 | | /* Decode ref idx and mvd for L0 (PRED_L0 or PRED_BI) */ |
928 | 1.01M | if(inter_pred_idc != PRED_L1) |
929 | 929k | { |
930 | 929k | UWORD32 u4_bits_estimated_prev_mvd_ref_id; |
931 | | /* encode L0 ref_idx */ |
932 | 929k | WORD32 ref_idx_l0 = ps_pu->mv.i1_l0_ref_idx; |
933 | | |
934 | | /*PIC INFO : Populate Ref Indx L0 Bits*/ |
935 | 929k | u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12; |
936 | 929k | ret |= ihevce_cabac_encode_refidx(ps_cabac, ref_idx_l0, ref_l0_active); |
937 | | |
938 | 929k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
939 | 76.8k | { |
940 | | // clang-format off |
941 | 76.8k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_ref_id += |
942 | 76.8k | (ps_cabac->u4_bits_estimated_q12 - |
943 | 76.8k | u4_bits_estimated_prev_mvd_ref_id); |
944 | | // clang-format on |
945 | 76.8k | } |
946 | | /* Encode the mvd for L0 */ |
947 | | /*PIC INFO : Populate MVD Bits*/ |
948 | 929k | u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12; |
949 | | |
950 | 929k | ret |= ihevce_cabac_encode_mvd(ps_cabac, &ps_pu->mv.s_l0_mv); |
951 | | |
952 | 929k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
953 | 76.8k | { // clang-format off |
954 | 76.8k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_mvd += |
955 | 76.8k | (ps_cabac->u4_bits_estimated_q12 - |
956 | 76.8k | u4_bits_estimated_prev_mvd_ref_id); |
957 | | // clang-format on |
958 | 76.8k | } |
959 | | |
960 | | /* Encode the mvp_l0_flag */ |
961 | 929k | ctxt_inc = IHEVC_CAB_MVP_L0L1; |
962 | 929k | ret |= ihevce_cabac_encode_bin(ps_cabac, ps_pu->b1_l0_mvp_idx, ctxt_inc); |
963 | | |
964 | 929k | AEV_TRACE("mvp_l0/l1_flag", ps_pu->b1_l0_mvp_idx, ps_cabac->u4_range); |
965 | 929k | } |
966 | | |
967 | | /* Encode ref idx and MVD for L1 (PRED_L1 or PRED_BI) */ |
968 | 1.01M | if(inter_pred_idc != PRED_L0) |
969 | 122k | { |
970 | | /* encode L1 ref_idx */ |
971 | 122k | WORD32 ref_idx_l1 = ps_pu->mv.i1_l1_ref_idx; |
972 | | |
973 | 122k | UWORD32 u4_bits_estimated_prev_mvd_ref_id; |
974 | | /*PIC INFO : Populate Ref Indx L1 Bits*/ |
975 | 122k | u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12; |
976 | | |
977 | 122k | ret |= ihevce_cabac_encode_refidx(ps_cabac, ref_idx_l1, ref_l1_active); |
978 | | |
979 | 122k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
980 | 7.42k | { // clang-format off |
981 | 7.42k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_ref_id += |
982 | 7.42k | (ps_cabac->u4_bits_estimated_q12 - |
983 | 7.42k | u4_bits_estimated_prev_mvd_ref_id); |
984 | 7.42k | } // clang-format on |
985 | | |
986 | | /* Check for zero mvd in case of bi_pred */ |
987 | 122k | if(ps_slice_hdr->i1_mvd_l1_zero_flag && inter_pred_idc == PRED_BI) |
988 | 0 | { |
989 | 0 | ASSERT(ps_pu->mv.s_l1_mv.i2_mvx == 0); |
990 | 0 | ASSERT(ps_pu->mv.s_l1_mv.i2_mvy == 0); |
991 | 0 | } |
992 | 122k | else |
993 | 122k | { |
994 | | /* Encode the mvd for L1 */ |
995 | | /*PIC INFO : Populate MVD Bits*/ |
996 | 122k | u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12; |
997 | | |
998 | | /* Encode the mvd for L1 */ |
999 | 122k | ret |= ihevce_cabac_encode_mvd(ps_cabac, &ps_pu->mv.s_l1_mv); |
1000 | | |
1001 | 122k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1002 | 7.42k | { |
1003 | 7.42k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_mvd += |
1004 | 7.42k | (ps_cabac->u4_bits_estimated_q12 - |
1005 | 7.42k | u4_bits_estimated_prev_mvd_ref_id); |
1006 | 7.42k | } |
1007 | 122k | } |
1008 | | |
1009 | | /* Encode the mvp_l1_flag */ |
1010 | 122k | ctxt_inc = IHEVC_CAB_MVP_L0L1; |
1011 | 122k | ret |= ihevce_cabac_encode_bin(ps_cabac, ps_pu->b1_l1_mvp_idx, ctxt_inc); |
1012 | | |
1013 | 122k | AEV_TRACE("mvp_l0/l1_flag", ps_pu->b1_l1_mvp_idx, ps_cabac->u4_range); |
1014 | 122k | } |
1015 | 1.01M | } |
1016 | 1.91M | } |
1017 | 1.44M | } |
1018 | | |
1019 | 3.54M | return ret; |
1020 | 3.54M | } |
1021 | | |
1022 | | /** |
1023 | | ****************************************************************************** |
1024 | | * |
1025 | | * @brief Entropy encoding of coding unit (Coding Unit syntax) |
1026 | | * |
1027 | | * @par Description |
1028 | | * Entropy encode of coding unit (Coding Unit syntax) as per section:7.3.9.1 |
1029 | | * General Coding unit syntax |
1030 | | * |
1031 | | * @param[inout] ps_entropy_ctxt |
1032 | | * pointer to entropy context (handle) |
1033 | | * |
1034 | | * @param[in] ps_enc_cu |
1035 | | * pointer to current cu whose entropy encode is done |
1036 | | * |
1037 | | * @param[in] cu_depth |
1038 | | * depth of the the current cu in coding tree |
1039 | | * |
1040 | | * @param[in] top_avail |
1041 | | * top availabilty flag for current cu (boolean) |
1042 | | * |
1043 | | * @param[in] left_avail |
1044 | | * left availabilty flag for current cu (boolean) |
1045 | | * |
1046 | | * @return success or failure error code |
1047 | | * |
1048 | | ****************************************************************************** |
1049 | | */ |
1050 | | WORD32 ihevce_cabac_encode_coding_unit( |
1051 | | entropy_context_t *ps_entropy_ctxt, |
1052 | | cu_enc_loop_out_t *ps_enc_cu, |
1053 | | WORD32 cu_depth, |
1054 | | WORD32 top_avail, |
1055 | | WORD32 left_avail) |
1056 | 9.88M | { |
1057 | 9.88M | WORD32 ret = IHEVCE_SUCCESS; |
1058 | 9.88M | sps_t *ps_sps = ps_entropy_ctxt->ps_sps; |
1059 | 9.88M | pps_t *ps_pps = ps_entropy_ctxt->ps_pps; |
1060 | 9.88M | slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr; |
1061 | | |
1062 | 9.88M | WORD32 skip_flag = 0; |
1063 | 9.88M | WORD32 no_res_flag = 0; |
1064 | | |
1065 | | /* CU top left co-ordinates w.r.t ctb */ |
1066 | 9.88M | WORD32 cu_x0 = ps_enc_cu->b3_cu_pos_x << 3; |
1067 | 9.88M | WORD32 cu_y0 = ps_enc_cu->b3_cu_pos_y << 3; |
1068 | | |
1069 | | /* CU size in pels */ |
1070 | 9.88M | WORD32 cu_size = ps_enc_cu->b4_cu_size << 3; |
1071 | 9.88M | WORD32 log2_cb_size; |
1072 | | |
1073 | 9.88M | cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt; |
1074 | | |
1075 | 9.88M | UWORD32 u4_header_bits_temp = ps_cabac->u4_bits_estimated_q12; |
1076 | | |
1077 | 9.88M | (void)cu_depth; |
1078 | 9.88M | (void)top_avail; |
1079 | 9.88M | (void)left_avail; |
1080 | | /* Sanity checks */ |
1081 | 9.88M | ASSERT((cu_x0 + cu_size) <= (1 << ps_entropy_ctxt->i1_log2_ctb_size)); |
1082 | 9.88M | ASSERT((cu_y0 + cu_size) <= (1 << ps_entropy_ctxt->i1_log2_ctb_size)); |
1083 | | |
1084 | | /* code tq bypass flag */ |
1085 | 9.88M | ASSERT(ps_pps->i1_transquant_bypass_enable_flag == 0); |
1086 | | |
1087 | | /* log2_cb_size based on cu size */ |
1088 | 9.88M | GETRANGE(log2_cb_size, cu_size); |
1089 | 9.88M | log2_cb_size -= 1; |
1090 | | |
1091 | 9.88M | if(ps_pps->i1_transquant_bypass_enable_flag) |
1092 | 0 | { |
1093 | 0 | ihevce_cabac_encode_bin( |
1094 | 0 | ps_cabac, ps_enc_cu->b1_tq_bypass_flag, IHEVC_CAB_CU_TQ_BYPASS_FLAG); |
1095 | |
|
1096 | 0 | AEV_TRACE("cu_transquant_bypass_flag", ps_enc_cu->b1_tq_bypass_flag, ps_cabac->u4_range); |
1097 | 0 | } |
1098 | | /* code the skip flag for inter slices */ |
1099 | 9.88M | if(ps_slice_hdr->i1_slice_type != ISLICE) |
1100 | 5.92M | { |
1101 | 5.92M | skip_flag = ps_enc_cu->b1_skip_flag; |
1102 | | |
1103 | 5.92M | ret |= ihevce_cabac_encode_skip_flag(ps_entropy_ctxt, ps_enc_cu, top_avail, left_avail); |
1104 | 5.92M | } |
1105 | | /*PIC_INFO : Total CU in frame based on cu size */ |
1106 | 9.88M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1107 | 1.25M | { |
1108 | | // clang-format off |
1109 | 1.25M | if(cu_size == 64) |
1110 | 20.1k | ps_entropy_ctxt->ps_pic_level_info->i8_total_cu_based_on_size[3]++; |
1111 | 1.23M | else |
1112 | 1.23M | ps_entropy_ctxt->ps_pic_level_info->i8_total_cu_based_on_size[cu_size >> 4]++; |
1113 | | // clang-format on |
1114 | 1.25M | } |
1115 | 9.88M | if(skip_flag) |
1116 | 2.10M | { |
1117 | | /* encode merge idx for the skip cu */ |
1118 | 2.10M | ret |= ihevce_cabac_encode_inter_pu(ps_entropy_ctxt, ps_enc_cu, cu_depth); |
1119 | | |
1120 | 2.10M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1121 | 361k | { |
1122 | | /*PIC INFO: Populated non-coded TUs in CU*/ |
1123 | 361k | ps_entropy_ctxt->ps_pic_level_info->i8_total_non_coded_tu += |
1124 | 361k | ps_enc_cu->u2_num_tus_in_cu; |
1125 | | // clang-format off |
1126 | 361k | if(cu_size == 64) |
1127 | 4.86k | ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] += |
1128 | 4.86k | ps_enc_cu->u2_num_tus_in_cu; |
1129 | 357k | else if(cu_size == 32) |
1130 | 142k | ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] += |
1131 | 142k | ps_enc_cu->u2_num_tus_in_cu; |
1132 | 214k | else |
1133 | 214k | ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[cu_size >> 3] += |
1134 | 214k | ps_enc_cu->u2_num_tus_in_cu; |
1135 | | // clang-format on |
1136 | | |
1137 | | /*PIC INFO: Populate cu header bits*/ |
1138 | 361k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cu_hdr_bits += |
1139 | 361k | (ps_cabac->u4_bits_estimated_q12 - u4_header_bits_temp); |
1140 | 361k | } |
1141 | 2.10M | } |
1142 | 7.78M | else |
1143 | 7.78M | { |
1144 | 7.78M | WORD32 pred_mode = PRED_MODE_INTRA; |
1145 | 7.78M | WORD32 part_mode = ps_enc_cu->b3_part_mode; |
1146 | 7.78M | WORD32 pcm_flag = ps_enc_cu->b1_pcm_flag; |
1147 | 7.78M | WORD32 is_mincu; |
1148 | 7.78M | WORD32 is_intra; |
1149 | | |
1150 | 7.78M | is_mincu = (cu_size == (1 << ps_sps->i1_log2_min_coding_block_size)); |
1151 | | /* encode pred mode flag for inter slice */ |
1152 | 7.78M | if(ps_slice_hdr->i1_slice_type != ISLICE) |
1153 | 3.81M | { |
1154 | 3.81M | pred_mode = ps_enc_cu->b1_pred_mode_flag; |
1155 | | |
1156 | 3.81M | ret |= ihevce_cabac_encode_bin(ps_cabac, pred_mode, IHEVC_CAB_PRED_MODE); |
1157 | | |
1158 | 3.81M | AEV_TRACE("pred_mode_flag", pred_mode, ps_cabac->u4_range); |
1159 | 3.81M | } |
1160 | 7.78M | is_intra = (PRED_MODE_INTRA == pred_mode); |
1161 | | |
1162 | | /* encode partition mode for inter pred or smallest intra pred cu */ |
1163 | 7.78M | if((!is_intra) || is_mincu) |
1164 | 4.45M | { |
1165 | 4.45M | WORD32 amp_enabled = ps_sps->i1_amp_enabled_flag; |
1166 | 4.45M | WORD32 cusize_8 = (cu_size == 8); |
1167 | | |
1168 | 4.45M | ret |= ihevce_cabac_encode_part_mode( |
1169 | 4.45M | ps_cabac, is_intra, is_mincu, amp_enabled, cusize_8, part_mode); |
1170 | 4.45M | } |
1171 | 3.32M | else |
1172 | 3.32M | { |
1173 | 3.32M | ASSERT(part_mode == SIZE_2Nx2N); |
1174 | 3.32M | } |
1175 | | |
1176 | | /* encode intra / inter pu modes of the current CU */ |
1177 | 7.78M | if(is_intra) |
1178 | 6.33M | { |
1179 | | /* NOTE: I_PCM not supported in encoder */ |
1180 | 6.33M | ASSERT(0 == pcm_flag); |
1181 | 6.33M | ASSERT(0 == ps_sps->i1_pcm_enabled_flag); |
1182 | | |
1183 | 6.33M | ret |= ihevce_cabac_encode_intra_pu(ps_entropy_ctxt, part_mode, ps_enc_cu); |
1184 | 6.33M | } |
1185 | 1.44M | else |
1186 | 1.44M | { |
1187 | 1.44M | ret |= ihevce_cabac_encode_inter_pu(ps_entropy_ctxt, ps_enc_cu, cu_depth); |
1188 | 1.44M | } |
1189 | | /* encode no residue syntax flag and transform tree conditionally */ |
1190 | 7.78M | if(!pcm_flag) |
1191 | 7.78M | { |
1192 | 7.78M | pu_t *ps_pu = &ps_enc_cu->ps_pu[0]; |
1193 | 7.78M | WORD32 merge_cu; |
1194 | | /* Encode residue syntax flag for inter cus not merged as 2Nx2N */ |
1195 | 7.78M | if(!is_intra) |
1196 | 1.44M | merge_cu = (part_mode == PART_2Nx2N) && ps_pu->b1_merge_flag; |
1197 | | |
1198 | 7.78M | if(!is_intra && !merge_cu) |
1199 | 1.24M | { |
1200 | 1.24M | no_res_flag = ps_enc_cu->b1_no_residual_syntax_flag; |
1201 | | |
1202 | 1.24M | #if 1 /* HACK FOR COMPLIANCE WITH HM REFERENCE DECODER */ |
1203 | | /*********************************************************/ |
1204 | | /* currently the HM decoder expects qtroot cbf instead of */ |
1205 | | /* no_residue_flag which has opposite meaning */ |
1206 | | /* This will be fixed once the software / spec is fixed */ |
1207 | | /*********************************************************/ |
1208 | 1.24M | ret |= ihevce_cabac_encode_bin(ps_cabac, !no_res_flag, IHEVC_CAB_NORES_IDX); |
1209 | | |
1210 | 1.24M | AEV_TRACE("no_residual_syntax_flag (HACKY)", !no_res_flag, ps_cabac->u4_range); |
1211 | | #else |
1212 | | ret |= ihevce_cabac_encode_bin(ps_cabac, no_res_flag, IHEVC_CAB_NORES_IDX); |
1213 | | |
1214 | | AEV_TRACE("no_residual_syntax_flag", no_res_flag, ps_cabac->u4_range); |
1215 | | #endif |
1216 | 1.24M | } |
1217 | | /*initialize header bits*/ |
1218 | 7.78M | ps_cabac->u4_header_bits_estimated_q12 = ps_cabac->u4_bits_estimated_q12; |
1219 | | |
1220 | 7.78M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1221 | 897k | { // clang-format off |
1222 | | /*PIC INFO: Populate cu header bits*/ |
1223 | 897k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cu_hdr_bits += |
1224 | 897k | (ps_cabac->u4_bits_estimated_q12 - u4_header_bits_temp); |
1225 | 897k | } // clang-format on |
1226 | | |
1227 | 7.78M | ps_cabac->u4_true_tu_split_flag_q12 = 0; |
1228 | | /* encode transform tree if no_residue_flag is 0 */ |
1229 | 7.78M | if(!no_res_flag) |
1230 | 6.94M | { |
1231 | 6.94M | ps_entropy_ctxt->i4_tu_idx = 0; |
1232 | | |
1233 | 6.94M | ret |= ihevce_encode_transform_tree( |
1234 | 6.94M | ps_entropy_ctxt, cu_x0, cu_y0, log2_cb_size, 0, 0, ps_enc_cu); |
1235 | 6.94M | } |
1236 | 831k | else |
1237 | 831k | { |
1238 | 831k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1239 | 31.2k | { |
1240 | | /*PIC INFO: Populated non-coded TUs in CU*/ |
1241 | 31.2k | ps_entropy_ctxt->ps_pic_level_info->i8_total_non_coded_tu += |
1242 | 31.2k | ps_enc_cu->u2_num_tus_in_cu; |
1243 | | // clang-format off |
1244 | 31.2k | if(cu_size == 64) |
1245 | 1.78k | ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] += |
1246 | 1.78k | ps_enc_cu->u2_num_tus_in_cu; |
1247 | 29.5k | else if(cu_size == 32) |
1248 | 16.8k | ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] += |
1249 | 16.8k | ps_enc_cu->u2_num_tus_in_cu; |
1250 | 12.6k | else |
1251 | 12.6k | ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[cu_size >> 3] += |
1252 | 12.6k | ps_enc_cu->u2_num_tus_in_cu; |
1253 | | // clang-format on |
1254 | 31.2k | } |
1255 | 831k | } |
1256 | 7.78M | ps_cabac->u4_cbf_bits_q12 = ps_cabac->u4_bits_estimated_q12 - |
1257 | 7.78M | ps_cabac->u4_header_bits_estimated_q12 - |
1258 | 7.78M | ps_cabac->u4_true_tu_split_flag_q12; |
1259 | 7.78M | } |
1260 | 7.78M | } |
1261 | | |
1262 | | /*duplicate the qp values for 8x8 CU array to maintain neighbour qp*/ |
1263 | 9.88M | if(CABAC_MODE_ENCODE_BITS == ps_entropy_ctxt->s_cabac_ctxt.e_cabac_op_mode) |
1264 | 1.25M | { |
1265 | 1.25M | WORD32 i, j; |
1266 | 1.25M | WORD32 cur_cu_offset, cur_qp, qp_left, qp_top; |
1267 | 1.25M | WORD32 is_last_blk_in_qg; |
1268 | | /* CU x co-ordinate w.r.t frame start */ |
1269 | 1.25M | WORD32 ctb_x0_frm = (ps_entropy_ctxt->i4_ctb_x << ps_entropy_ctxt->i1_log2_ctb_size); |
1270 | | |
1271 | 1.25M | WORD32 cu_x0_frm = cu_x0 + ctb_x0_frm; |
1272 | | |
1273 | | /* CU y co-ordinate w.r.t frame start */ |
1274 | 1.25M | WORD32 ctb_y0_frm = (ps_entropy_ctxt->i4_ctb_y << ps_entropy_ctxt->i1_log2_ctb_size); |
1275 | | |
1276 | 1.25M | WORD32 cu_y0_frm = cu_y0 + ctb_y0_frm; |
1277 | | |
1278 | 1.25M | WORD32 pic_width = ps_sps->i2_pic_width_in_luma_samples; |
1279 | 1.25M | WORD32 pic_height = ps_sps->i2_pic_height_in_luma_samples; |
1280 | | |
1281 | | /* Added code for handling the QP neighbour population depending |
1282 | | on the diff_cu_qp_delta_depth: Lokesh */ |
1283 | | /* is_last_blk_in_qg variables is to find if the coding block is the last CU in the Quantization group |
1284 | | 3 - i1_diff_cu_qp_delta_depth is done as the cu_pos_x and cu_pos_y are in terms of 8x8 positions in the CTB: Lokesh*/ |
1285 | 1.25M | WORD32 log2_min_cu_qp_delta_size = |
1286 | 1.25M | ps_entropy_ctxt->i1_log2_ctb_size - ps_entropy_ctxt->ps_pps->i1_diff_cu_qp_delta_depth; |
1287 | 1.25M | UWORD32 min_cu_qp_delta_size = 1 << log2_min_cu_qp_delta_size; |
1288 | | |
1289 | 1.25M | WORD32 block_addr_align = 15 << (log2_min_cu_qp_delta_size - 3); |
1290 | | |
1291 | 1.25M | ps_entropy_ctxt->i4_qg_pos_x = ps_enc_cu->b3_cu_pos_x & block_addr_align; |
1292 | 1.25M | ps_entropy_ctxt->i4_qg_pos_y = ps_enc_cu->b3_cu_pos_y & block_addr_align; |
1293 | | |
1294 | | /* Condition for detecting last cu in a qp group. */ |
1295 | | /* Case 1: Current cu position + size exceed or meets the next qp group start location */ |
1296 | | /* Case 2: Current cu position + size hits the incomplete ctb boundary in atleast one */ |
1297 | | /* direction and the qp grp limit in other direction */ |
1298 | | |
1299 | | /* case 1 */ |
1300 | 1.25M | is_last_blk_in_qg = |
1301 | 1.25M | ((cu_x0 + cu_size) >= |
1302 | 1.25M | ((ps_entropy_ctxt->i4_qg_pos_x << 3) + (WORD32)min_cu_qp_delta_size) && |
1303 | 1.25M | (cu_y0 + cu_size) >= |
1304 | 1.25M | ((ps_entropy_ctxt->i4_qg_pos_y << 3) + (WORD32)min_cu_qp_delta_size)); |
1305 | | |
1306 | | /* case 2 : x direction incomplete ctb */ |
1307 | 1.25M | if((cu_x0_frm + cu_size) >= pic_width) |
1308 | 238k | { |
1309 | 238k | is_last_blk_in_qg |= |
1310 | 238k | ((cu_y0 + cu_size) >= |
1311 | 238k | ((ps_entropy_ctxt->i4_qg_pos_y << 3) + (WORD32)min_cu_qp_delta_size)); |
1312 | 238k | } |
1313 | | |
1314 | | /* case 2 : y direction incomplete ctb */ |
1315 | 1.25M | if((cu_y0_frm + cu_size) >= pic_height) |
1316 | 241k | { |
1317 | 241k | is_last_blk_in_qg |= |
1318 | 241k | ((cu_x0 + cu_size) >= |
1319 | 241k | ((ps_entropy_ctxt->i4_qg_pos_x << 3) + (WORD32)min_cu_qp_delta_size)); |
1320 | 241k | } |
1321 | | |
1322 | 1.25M | cur_cu_offset = ps_enc_cu->b3_cu_pos_x + (ps_enc_cu->b3_cu_pos_y * 8); |
1323 | | |
1324 | 1.25M | if((ps_entropy_ctxt->i4_is_cu_cbf_zero || no_res_flag || skip_flag) && |
1325 | 695k | ((ps_entropy_ctxt->i1_encode_qp_delta))) |
1326 | 220k | { |
1327 | 220k | { // clang-format off |
1328 | | /*it should remember average of qp_top and qp_left*/ |
1329 | 220k | if(ps_entropy_ctxt->i4_qg_pos_x > 0) |
1330 | 160k | { |
1331 | 160k | qp_left = |
1332 | 160k | ps_entropy_ctxt->ai4_8x8_cu_qp[(ps_entropy_ctxt->i4_qg_pos_x - 1) + |
1333 | 160k | (ps_entropy_ctxt->i4_qg_pos_y * 8)]; |
1334 | 160k | } |
1335 | 220k | if(ps_entropy_ctxt->i4_qg_pos_y > 0) |
1336 | 167k | { |
1337 | 167k | qp_top = |
1338 | 167k | ps_entropy_ctxt->ai4_8x8_cu_qp[ps_entropy_ctxt->i4_qg_pos_x + |
1339 | 167k | (ps_entropy_ctxt->i4_qg_pos_y - 1) * |
1340 | 167k | 8]; |
1341 | 167k | } // clang-format on |
1342 | 220k | if(ps_entropy_ctxt->i4_qg_pos_x == 0) |
1343 | 60.3k | { |
1344 | | /*previous coded Qp*/ |
1345 | 60.3k | qp_left = ps_entropy_ctxt->i1_cur_qp; |
1346 | 60.3k | } |
1347 | 220k | if(ps_entropy_ctxt->i4_qg_pos_y == 0) |
1348 | 53.8k | { |
1349 | | /*previous coded Qp*/ |
1350 | 53.8k | qp_top = ps_entropy_ctxt->i1_cur_qp; |
1351 | 53.8k | } |
1352 | 220k | cur_qp = (qp_top + qp_left + 1) >> 1; |
1353 | | /*In case of skip or zero cbf CU the previous qp used has to be updated*/ |
1354 | 220k | if(is_last_blk_in_qg) |
1355 | 220k | ps_entropy_ctxt->i1_cur_qp = cur_qp; |
1356 | 220k | } |
1357 | 220k | } |
1358 | 1.03M | else |
1359 | 1.03M | { |
1360 | 1.03M | cur_qp = (WORD32)ps_enc_cu->ps_enc_tu->s_tu.b7_qp; |
1361 | 1.03M | } |
1362 | | |
1363 | 1.25M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1364 | 1.25M | { |
1365 | 1.25M | WORD32 temp = 0; |
1366 | | /*PIC_INFO: Accumalate average qp, min qp and max qp*/ |
1367 | 1.25M | ps_entropy_ctxt->ps_pic_level_info->i8_total_qp += cur_qp; |
1368 | 1.25M | if(cu_size == 64) |
1369 | 20.1k | temp = 6; |
1370 | 1.23M | else if(cu_size == 32) |
1371 | 282k | temp = 4; |
1372 | 956k | else if(cu_size == 16) |
1373 | 355k | temp = 2; |
1374 | 600k | else if(cu_size == 8) |
1375 | 600k | temp = 0; |
1376 | | |
1377 | 1.25M | ps_entropy_ctxt->ps_pic_level_info->i8_total_qp_min_cu += (cur_qp * (1 << temp)); |
1378 | 1.25M | if(cur_qp < ps_entropy_ctxt->ps_pic_level_info->i4_min_qp) |
1379 | 100k | ps_entropy_ctxt->ps_pic_level_info->i4_min_qp = cur_qp; |
1380 | 1.25M | if(cur_qp > ps_entropy_ctxt->ps_pic_level_info->i4_max_qp) |
1381 | 95.3k | ps_entropy_ctxt->ps_pic_level_info->i4_max_qp = cur_qp; |
1382 | 1.25M | } |
1383 | | |
1384 | 3.86M | for(i = 0; i < (WORD32)ps_enc_cu->b4_cu_size; i++) |
1385 | 2.60M | { |
1386 | 10.4M | for(j = 0; j < (WORD32)ps_enc_cu->b4_cu_size; j++) |
1387 | 7.83M | { |
1388 | 7.83M | ps_entropy_ctxt->ai4_8x8_cu_qp[cur_cu_offset + (i * 8) + j] = cur_qp; |
1389 | 7.83M | } |
1390 | 2.60M | } |
1391 | 1.25M | ps_entropy_ctxt->i4_is_cu_cbf_zero = 1; |
1392 | 1.25M | } |
1393 | | |
1394 | 9.88M | return ret; |
1395 | 9.88M | } |
1396 | | |
1397 | | /** |
1398 | | ****************************************************************************** |
1399 | | * |
1400 | | * @brief Entropy encoding of SAO related syntax elements as per sec 7.3.8.3 |
1401 | | * |
1402 | | * @par Description |
1403 | | * Encoding of sao related syntax elements at ctb level. |
1404 | | * |
1405 | | * @param[inout] ps_entropy_ctxt |
1406 | | * pointer to entropy context (handle) |
1407 | | * |
1408 | | * @param[in] ps_ctb_enc_loop_out |
1409 | | * pointer to ctb level output structure from enc loop |
1410 | | * |
1411 | | * @return success or failure error code |
1412 | | * |
1413 | | ****************************************************************************** |
1414 | | */ |
1415 | | WORD32 ihevce_cabac_encode_sao( |
1416 | | entropy_context_t *ps_entropy_ctxt, ctb_enc_loop_out_t *ps_ctb_enc_loop_out) |
1417 | 526k | { |
1418 | 526k | WORD32 error = IHEVCE_SUCCESS; |
1419 | 526k | sao_enc_t *ps_sao; |
1420 | 526k | nbr_avail_flags_t *ps_ctb_nbr_avail_flags; |
1421 | 526k | slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr; |
1422 | 526k | cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt; |
1423 | | |
1424 | 526k | UWORD8 u1_left_avail, u1_top_avail; |
1425 | | |
1426 | 526k | ps_ctb_nbr_avail_flags = &ps_ctb_enc_loop_out->s_ctb_nbr_avail_flags; |
1427 | | |
1428 | 526k | ps_sao = &ps_ctb_enc_loop_out->s_sao; |
1429 | | |
1430 | 526k | ASSERT(ps_sao->b1_sao_merge_left_flag < 2); |
1431 | | |
1432 | 526k | u1_left_avail = ps_ctb_nbr_avail_flags->u1_left_avail; |
1433 | 526k | u1_top_avail = ps_ctb_nbr_avail_flags->u1_top_avail; |
1434 | | |
1435 | 526k | if(u1_left_avail == 1) |
1436 | 147k | { |
1437 | | /*Encode the sao_merge_left_flag as FL as per table 9-32*/ |
1438 | 147k | error |= |
1439 | 147k | ihevce_cabac_encode_bin(ps_cabac, ps_sao->b1_sao_merge_left_flag, IHEVC_CAB_SAO_MERGE); |
1440 | | |
1441 | 147k | AEV_TRACE("sao_merge_flag", ps_sao->b1_sao_merge_left_flag, ps_cabac->u4_range); |
1442 | 147k | } |
1443 | | |
1444 | 526k | if((u1_top_avail == 1) && (!ps_sao->b1_sao_merge_left_flag)) |
1445 | 125k | { |
1446 | | /*Encode the sao_merge_up_flag as FL as per table 9-32*/ |
1447 | 125k | error |= |
1448 | 125k | ihevce_cabac_encode_bin(ps_cabac, ps_sao->b1_sao_merge_up_flag, IHEVC_CAB_SAO_MERGE); |
1449 | | |
1450 | 125k | AEV_TRACE("sao_merge_flag", ps_sao->b1_sao_merge_up_flag, ps_cabac->u4_range); |
1451 | 125k | } |
1452 | | |
1453 | 526k | if((!ps_sao->b1_sao_merge_left_flag) && (!ps_sao->b1_sao_merge_up_flag)) |
1454 | 476k | { |
1455 | 476k | WORD32 c_idx; |
1456 | 476k | WORD32 sao_type_idx = ps_sao->b3_y_type_idx; |
1457 | | |
1458 | | /*Run a loop for y,cb and cr to encode the type idx for luma and chroma*/ |
1459 | 1.90M | for(c_idx = 0; c_idx < 3; c_idx++) |
1460 | 1.42M | { |
1461 | 1.42M | if((ps_slice_hdr->i1_slice_sao_luma_flag && c_idx == 0) || |
1462 | 952k | (ps_slice_hdr->i1_slice_sao_chroma_flag && c_idx > 0)) |
1463 | 1.42M | { |
1464 | 1.42M | WORD32 ctxt_bin; |
1465 | | |
1466 | | /**************************************************************************/ |
1467 | | /* encode the sao_type_idx as per Table 9-33 */ |
1468 | | /* First bin is context model based prefix : 1 if sao_type_idx > 0 else 0 */ |
1469 | | /* Second bin is coded as bypass bin if sao_type_ide > 0 */ |
1470 | | /**************************************************************************/ |
1471 | | |
1472 | 1.42M | if(c_idx < 2) |
1473 | 952k | { |
1474 | 952k | WORD32 sao_type_idx_temp; |
1475 | | |
1476 | 952k | ASSERT(ps_sao->b3_cb_type_idx == ps_sao->b3_cr_type_idx); |
1477 | | |
1478 | 952k | sao_type_idx = c_idx ? ps_sao->b3_cb_type_idx : ps_sao->b3_y_type_idx; |
1479 | | |
1480 | 952k | ctxt_bin = sao_type_idx ? 1 : 0; |
1481 | | |
1482 | 952k | if(sao_type_idx > 1) |
1483 | 461k | { |
1484 | 461k | sao_type_idx_temp = 2; |
1485 | 461k | } |
1486 | 491k | else |
1487 | 491k | { |
1488 | 491k | sao_type_idx_temp = sao_type_idx; |
1489 | 491k | } |
1490 | | |
1491 | 952k | ASSERT(sao_type_idx_temp < 3); |
1492 | | |
1493 | | /*Encode the first bin as context bin as per table 9-37*/ |
1494 | 952k | error |= ihevce_cabac_encode_bin(ps_cabac, ctxt_bin, IHEVC_CAB_SAO_TYPE); |
1495 | | |
1496 | 952k | if(sao_type_idx_temp) |
1497 | 461k | { |
1498 | | /*Binarisation for sao_type_idx is TR(truncated rice) process as per |
1499 | | * table 9-32 with cMax=2 and cRiceParam=0 |
1500 | | */ |
1501 | | |
1502 | | /* Encode the second bin as bypass bin as per below table*/ |
1503 | | /* |
1504 | | |Symbol | Prefix |Prefix length |Prefix bins| |
1505 | | | 0 | 0 | 1 | 0 | |
1506 | | | 1 | 1 | 2 | 10 | |
1507 | | | 2 | 2 | 2 | 11 | |
1508 | | |
1509 | | Since cRiceParam=0, there is no suffix code |
1510 | | */ |
1511 | | |
1512 | 461k | error |= ihevce_cabac_encode_bypass_bin(ps_cabac, sao_type_idx_temp - 1); |
1513 | 461k | } |
1514 | 952k | AEV_TRACE("sao_type_idx", sao_type_idx_temp, ps_cabac->u4_range); |
1515 | 952k | } |
1516 | | |
1517 | 1.42M | if(sao_type_idx != 0) |
1518 | 551k | { |
1519 | 551k | WORD32 i; |
1520 | 551k | UWORD8 u1_bit_depth = ps_entropy_ctxt->ps_sps->i1_bit_depth_luma_minus8 + 8; |
1521 | 551k | WORD8 *sao_offset; |
1522 | 551k | WORD32 sao_band_position; |
1523 | 551k | WORD32 c_max = (1 << (MIN(u1_bit_depth, 10) - 5)) - |
1524 | 551k | 1; //( 1 << (MIN(BIT_DEPTH, 10) - 5)) - 1; |
1525 | | |
1526 | 551k | if(c_idx == 0) |
1527 | 371k | { |
1528 | | //sao_offset[0] = ps_sao->b4_y_offset_1; |
1529 | | //sao_offset[1] = ps_sao->b4_y_offset_2; |
1530 | | //sao_offset[2] = ps_sao->b4_y_offset_3; |
1531 | | //sao_offset[3] = ps_sao->b4_y_offset_4; |
1532 | 371k | sao_offset = &ps_sao->u1_y_offset[1]; |
1533 | 371k | sao_band_position = ps_sao->b5_y_band_pos; |
1534 | 371k | } |
1535 | 180k | else if(c_idx == 1) |
1536 | 90.0k | { |
1537 | | //sao_offset[0] = ps_sao->b4_cb_offset_1; |
1538 | | //sao_offset[1] = ps_sao->b4_cb_offset_2; |
1539 | | //sao_offset[2] = ps_sao->b4_cb_offset_3; |
1540 | | //sao_offset[3] = ps_sao->b4_cb_offset_4; |
1541 | 90.0k | sao_offset = &ps_sao->u1_cb_offset[1]; |
1542 | 90.0k | sao_band_position = ps_sao->b5_cb_band_pos; |
1543 | 90.0k | } |
1544 | 90.0k | else |
1545 | 90.0k | { |
1546 | | //sao_offset[0] = ps_sao->b4_cr_offset_1; |
1547 | | //sao_offset[1] = ps_sao->b4_cr_offset_2; |
1548 | | //sao_offset[2] = ps_sao->b4_cr_offset_3; |
1549 | | //sao_offset[3] = ps_sao->b4_cr_offset_4; |
1550 | 90.0k | sao_offset = &ps_sao->u1_cr_offset[1]; |
1551 | 90.0k | sao_band_position = ps_sao->b5_cr_band_pos; |
1552 | 90.0k | } |
1553 | | |
1554 | 2.75M | for(i = 0; i < 4; i++) |
1555 | 2.20M | { |
1556 | | /*Encode the sao offset value as tunary bypass*/ |
1557 | 2.20M | error |= |
1558 | 2.20M | ihevce_cabac_encode_tunary_bypass(ps_cabac, abs(sao_offset[i]), c_max); |
1559 | | |
1560 | 2.20M | AEV_TRACE("sao_offset_abs", abs(sao_offset[i]), ps_cabac->u4_range); |
1561 | 2.20M | } |
1562 | | |
1563 | | /*Band offset case*/ |
1564 | 551k | if(sao_type_idx == 1) |
1565 | 0 | { |
1566 | 0 | for(i = 0; i < 4; i++) |
1567 | 0 | { |
1568 | 0 | if(sao_offset[i] != 0) |
1569 | 0 | { |
1570 | | /*Encode the sao offset sign as FL as per table 9-32*/ |
1571 | 0 | error |= ihevce_cabac_encode_bypass_bin( |
1572 | 0 | ps_cabac, |
1573 | 0 | (abs(sao_offset[i]) + sao_offset[i] == 0)); //, |
1574 | | //IHEVC_CAB_SAO_MERGE |
1575 | | //); |
1576 | |
|
1577 | 0 | AEV_TRACE( |
1578 | 0 | "sao_offset_sign", |
1579 | 0 | (abs(sao_offset[i]) + sao_offset[i] == 0), |
1580 | 0 | ps_cabac->u4_range); |
1581 | 0 | } |
1582 | 0 | } |
1583 | | |
1584 | | /*Encode the sao band position as FL as per table 9-32*/ |
1585 | 0 | error |= ihevce_cabac_encode_bypass_bins(ps_cabac, sao_band_position, 5); |
1586 | 0 | AEV_TRACE("sao_band_position", sao_band_position, ps_cabac->u4_range); |
1587 | 0 | } |
1588 | 551k | else |
1589 | 551k | { |
1590 | | /*Encode the sao edge offset class for luma and chroma as FL as per table 9-32*/ |
1591 | 551k | if(c_idx == 0) |
1592 | 371k | { |
1593 | 371k | error |= ihevce_cabac_encode_bypass_bins( |
1594 | 371k | ps_cabac, (ps_sao->b3_y_type_idx - 2), 2); |
1595 | 371k | AEV_TRACE( |
1596 | 371k | "sao_eo_class", (ps_sao->b3_y_type_idx - 2), ps_cabac->u4_range); |
1597 | 371k | } |
1598 | | |
1599 | 551k | if(c_idx == 1) |
1600 | 90.0k | { |
1601 | 90.0k | ASSERT(ps_sao->b3_cb_type_idx == ps_sao->b3_cr_type_idx); |
1602 | 90.0k | error |= ihevce_cabac_encode_bypass_bins( |
1603 | 90.0k | ps_cabac, (ps_sao->b3_cb_type_idx - 2), 2); |
1604 | 90.0k | AEV_TRACE( |
1605 | 90.0k | "sao_eo_class", (ps_sao->b3_cb_type_idx - 2), ps_cabac->u4_range); |
1606 | 90.0k | } |
1607 | 551k | } |
1608 | 551k | } |
1609 | 1.42M | } |
1610 | 1.42M | } |
1611 | 476k | } |
1612 | | |
1613 | 526k | return (error); |
1614 | 526k | } |
1615 | | |
1616 | | /** |
1617 | | ****************************************************************************** |
1618 | | * |
1619 | | * @brief Encodes a coding quad tree (QuadTree syntax) as per section 7.3.8 |
1620 | | * |
1621 | | * @par Description |
1622 | | * Entropy encode of coding quad tree based on cu split flags of ctb as per |
1623 | | * section:7.3.8 |
1624 | | * |
1625 | | * @param[inout] ps_entropy_ctxt |
1626 | | * pointer to entropy context (handle) |
1627 | | * |
1628 | | * @param[in] x0_frm |
1629 | | * x co-ordinate of current cu node of coding tree |
1630 | | * |
1631 | | * @param[in] y0_frm |
1632 | | * y co-ordinate of current cu node of coding tree |
1633 | | * |
1634 | | * @param[in] log2_cb_size |
1635 | | * current cu node block size |
1636 | | * |
1637 | | * @param[in] ct_depth |
1638 | | * depth of current cu node w.r.t ctb |
1639 | | * |
1640 | | * @param[in] ps_ctb |
1641 | | * pointer to current ctb structure |
1642 | | * |
1643 | | * @return success or failure error code |
1644 | | * |
1645 | | ****************************************************************************** |
1646 | | */ |
1647 | | WORD32 ihevce_encode_coding_quadtree( |
1648 | | entropy_context_t *ps_entropy_ctxt, |
1649 | | WORD32 x0_frm, |
1650 | | WORD32 y0_frm, |
1651 | | WORD32 log2_cb_size, |
1652 | | WORD32 ct_depth, |
1653 | | ctb_enc_loop_out_t *ps_ctb, |
1654 | | ihevce_tile_params_t *ps_tile_params) |
1655 | 1.68M | { |
1656 | 1.68M | WORD32 ret = IHEVCE_SUCCESS; |
1657 | 1.68M | sps_t *ps_sps = ps_entropy_ctxt->ps_sps; |
1658 | 1.68M | pps_t *ps_pps = ps_entropy_ctxt->ps_pps; |
1659 | 1.68M | WORD32 split_cu_flag; |
1660 | 1.68M | WORD32 cu_idx = ps_entropy_ctxt->i4_cu_idx; |
1661 | 1.68M | cu_enc_loop_out_t *ps_enc_cu = ps_ctb->ps_enc_cu + cu_idx; |
1662 | | |
1663 | | /* CU size in pels */ |
1664 | 1.68M | WORD32 cu_size = ps_enc_cu->b4_cu_size << 3; |
1665 | | |
1666 | 1.68M | WORD32 pic_width = ps_tile_params->i4_curr_tile_width; |
1667 | 1.68M | WORD32 pic_height = ps_tile_params->i4_curr_tile_height; |
1668 | | |
1669 | 1.68M | WORD32 log2_min_cb_size = ps_sps->i1_log2_min_coding_block_size; |
1670 | 1.68M | WORD32 ctb_size = (1 << (log2_cb_size + ct_depth)); |
1671 | 1.68M | cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt; |
1672 | | |
1673 | | /* top row cu depth stored for frm_width (1byte per mincusize=8) */ |
1674 | 1.68M | UWORD8 *pu1_cu_depth_top = ps_entropy_ctxt->pu1_cu_depth_top; |
1675 | | |
1676 | | /* left cu depth stored for one ctb column (1byte per mincusize=8) */ |
1677 | 1.68M | UWORD8 *pu1_cu_depth_left = &ps_entropy_ctxt->au1_cu_depth_left[0]; |
1678 | | |
1679 | | /* calculation of top and left nbr availability */ |
1680 | 1.68M | WORD32 top_avail; |
1681 | 1.68M | WORD32 left_avail; |
1682 | | |
1683 | | /* top and left cu within ctb or outside ctb boundary */ |
1684 | 1.68M | left_avail = (x0_frm & (ctb_size - 1)) ? 1 : ps_ctb->s_ctb_nbr_avail_flags.u1_left_avail; |
1685 | 1.68M | top_avail = (y0_frm & (ctb_size - 1)) ? 1 : ps_ctb->s_ctb_nbr_avail_flags.u1_top_avail; |
1686 | | |
1687 | | /* Sanity checks */ |
1688 | 1.68M | ASSERT(ct_depth <= 3); |
1689 | 1.68M | ASSERT((cu_idx >= 0) && (cu_idx < ps_ctb->u1_num_cus_in_ctb)); |
1690 | 1.68M | ASSERT(cu_size >= (1 << log2_min_cb_size)); |
1691 | 1.68M | ASSERT(((ps_enc_cu->b3_cu_pos_x << 3) + cu_size) <= (UWORD32)ctb_size); |
1692 | 1.68M | ASSERT(((ps_enc_cu->b3_cu_pos_y << 3) + cu_size) <= (UWORD32)ctb_size); |
1693 | | |
1694 | | /* Encode cu split flags based on following conditions; See section 7.3.8*/ |
1695 | 1.68M | if(((x0_frm + (1 << log2_cb_size)) <= pic_width) && |
1696 | 1.64M | ((y0_frm + (1 << log2_cb_size)) <= pic_height) && (log2_cb_size > log2_min_cb_size) && |
1697 | 1.00M | (ps_entropy_ctxt->i1_ctb_num_pcm_blks == 0)) |
1698 | 1.00M | { |
1699 | | /* encode the split cu flag */ |
1700 | 1.00M | WORD32 ctxt_inc = IHEVC_CAB_SPLIT_CU_FLAG; |
1701 | 1.00M | UWORD32 u4_bits_estimated_prev; |
1702 | | /* Context increment for skip flag as per Table 9-38 */ |
1703 | 1.00M | if(top_avail) |
1704 | 553k | { |
1705 | 553k | ctxt_inc += (pu1_cu_depth_top[x0_frm >> 3] > ct_depth); |
1706 | 553k | } |
1707 | | |
1708 | 1.00M | if(left_avail) |
1709 | 562k | { |
1710 | 562k | ctxt_inc += (pu1_cu_depth_left[(y0_frm >> 3) & 0x7] > ct_depth); |
1711 | 562k | } |
1712 | | |
1713 | | /* split if actual cu size is smaller than target cu size */ |
1714 | 1.00M | split_cu_flag = cu_size < (1 << log2_cb_size); |
1715 | 1.00M | u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12; |
1716 | 1.00M | ret |= ihevce_cabac_encode_bin(ps_cabac, split_cu_flag, ctxt_inc); |
1717 | | |
1718 | 1.00M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1719 | 1.00M | { // clang-format off |
1720 | | /*PIC INFO : populate cu split flag*/ |
1721 | 1.00M | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_split_cu_flag += |
1722 | 1.00M | (ps_cabac->u4_bits_estimated_q12 - u4_bits_estimated_prev); |
1723 | 1.00M | } // clang-format on |
1724 | | |
1725 | 1.00M | AEV_TRACE("split_cu_flag", split_cu_flag, ps_cabac->u4_range); |
1726 | 1.00M | if(split_cu_flag == 0) |
1727 | 658k | { |
1728 | 658k | AEV_TRACE("split_cu_flag : X0", (x0_frm >> 6) << 6, ps_cabac->u4_range); |
1729 | 658k | AEV_TRACE("split_cu_flag : Y0", (y0_frm >> 6) << 6, ps_cabac->u4_range); |
1730 | 658k | } |
1731 | 1.00M | } |
1732 | 681k | else |
1733 | 681k | { |
1734 | | /*********************************************************************/ |
1735 | | /* split cu is implicitly derived as 1 in frame/slice boundary case */ |
1736 | | /* else split cu is implicitly derived as 0 if mincu size is reached */ |
1737 | | /*********************************************************************/ |
1738 | 681k | if(log2_cb_size > ps_sps->i1_log2_min_coding_block_size) |
1739 | 80.4k | split_cu_flag = 1; |
1740 | 600k | else |
1741 | 600k | split_cu_flag = 0; |
1742 | 681k | } |
1743 | | |
1744 | | /************************************************************************/ |
1745 | | /* Reset qp delata coded flag appropriately so as to signal qp rightly */ |
1746 | | /* during transform coding */ |
1747 | | /************************************************************************/ |
1748 | 1.68M | if((ps_pps->i1_cu_qp_delta_enabled_flag) && (ct_depth <= (ps_pps->i1_diff_cu_qp_delta_depth))) |
1749 | | |
1750 | 550k | { |
1751 | 550k | ps_entropy_ctxt->i1_encode_qp_delta = 1; |
1752 | 550k | } |
1753 | | /*else |
1754 | | { |
1755 | | ps_entropy_ctxt->i1_encode_qp_delta = 0; |
1756 | | }*/ |
1757 | | |
1758 | 1.68M | if(split_cu_flag) |
1759 | 424k | { |
1760 | | /* recurse quad tree till a leaf node is reached */ |
1761 | 424k | WORD32 x1_frm = x0_frm + ((1 << log2_cb_size) >> 1); |
1762 | 424k | WORD32 y1_frm = y0_frm + ((1 << log2_cb_size) >> 1); |
1763 | | |
1764 | | /* node0 of quad tree */ |
1765 | 424k | ret |= ihevce_encode_coding_quadtree( |
1766 | 424k | ps_entropy_ctxt, x0_frm, y0_frm, log2_cb_size - 1, ct_depth + 1, ps_ctb, ps_tile_params); |
1767 | | |
1768 | 424k | if(x1_frm < pic_width) |
1769 | 384k | { /* node1 of quad tree */ |
1770 | 384k | ret |= ihevce_encode_coding_quadtree( |
1771 | 384k | ps_entropy_ctxt, |
1772 | 384k | x1_frm, |
1773 | 384k | y0_frm, |
1774 | 384k | log2_cb_size - 1, |
1775 | 384k | ct_depth + 1, |
1776 | 384k | ps_ctb, |
1777 | 384k | ps_tile_params); |
1778 | 384k | } |
1779 | | |
1780 | 424k | if(y1_frm < pic_height) |
1781 | 391k | { |
1782 | | /* node2 of quad tree */ |
1783 | 391k | ret |= ihevce_encode_coding_quadtree( |
1784 | 391k | ps_entropy_ctxt, |
1785 | 391k | x0_frm, |
1786 | 391k | y1_frm, |
1787 | 391k | log2_cb_size - 1, |
1788 | 391k | ct_depth + 1, |
1789 | 391k | ps_ctb, |
1790 | 391k | ps_tile_params); |
1791 | 391k | } |
1792 | | |
1793 | 424k | if((x1_frm < pic_width) && (y1_frm < pic_height)) |
1794 | 352k | { |
1795 | | /* node3 of quad tree */ |
1796 | 352k | ret |= ihevce_encode_coding_quadtree( |
1797 | 352k | ps_entropy_ctxt, |
1798 | 352k | x1_frm, |
1799 | 352k | y1_frm, |
1800 | 352k | log2_cb_size - 1, |
1801 | 352k | ct_depth + 1, |
1802 | 352k | ps_ctb, |
1803 | 352k | ps_tile_params); |
1804 | 352k | } |
1805 | 424k | } |
1806 | 1.25M | else |
1807 | 1.25M | { |
1808 | | /* leaf node is reached! Encode the CU */ |
1809 | 1.25M | WORD32 i; |
1810 | | |
1811 | | /* sanity checks */ |
1812 | 1.25M | ASSERT(ps_entropy_ctxt->i1_ctb_num_pcm_blks == 0); |
1813 | | |
1814 | 1.25M | if(ps_entropy_ctxt->i1_ctb_num_pcm_blks == 0) |
1815 | 1.25M | { |
1816 | 1.25M | UWORD32 u4_bits_eztimated = ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12; |
1817 | | /* Encode a non-PCM CU */ |
1818 | | /*PCM INFO: populate total TUs*/ |
1819 | 1.25M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1820 | 1.25M | { |
1821 | 1.25M | ps_entropy_ctxt->ps_pic_level_info->i8_total_tu += ps_enc_cu->u2_num_tus_in_cu; |
1822 | 1.25M | } |
1823 | | |
1824 | 1.25M | ret |= ihevce_cabac_encode_coding_unit( |
1825 | 1.25M | ps_entropy_ctxt, ps_enc_cu, ct_depth, top_avail, left_avail); |
1826 | | |
1827 | 1.25M | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
1828 | 1.25M | { |
1829 | | // clang-format off |
1830 | 1.25M | if(PRED_MODE_INTRA == ps_enc_cu->b1_pred_mode_flag) |
1831 | 738k | { |
1832 | 738k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_intra += |
1833 | 738k | (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 - |
1834 | 738k | u4_bits_eztimated); |
1835 | 738k | } |
1836 | 520k | else |
1837 | 520k | { |
1838 | 520k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_inter += |
1839 | 520k | (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 - |
1840 | 520k | u4_bits_eztimated); |
1841 | 520k | } |
1842 | | // clang-format on |
1843 | 1.25M | } |
1844 | 1.25M | } |
1845 | 0 | else |
1846 | 0 | { //TODO: //PCM not supported in this encoder |
1847 | 0 | } |
1848 | | |
1849 | | /* update cu_idx, left and top arrays for cudepth after encoding cu */ |
1850 | 1.25M | ps_entropy_ctxt->i4_cu_idx++; |
1851 | 3.86M | for(i = 0; i < (cu_size >> 3); i++) |
1852 | 2.60M | { |
1853 | 2.60M | pu1_cu_depth_top[(x0_frm >> 3) + i] = ct_depth; |
1854 | 2.60M | pu1_cu_depth_left[((y0_frm >> 3) & 0x7) + i] = ct_depth; |
1855 | 2.60M | } |
1856 | 1.25M | } |
1857 | | |
1858 | 1.68M | return ret; |
1859 | 1.68M | } |
1860 | | |
1861 | | /** |
1862 | | ****************************************************************************** |
1863 | | * |
1864 | | * @brief Encodes slice data (General Slice syntax) as per section 7.3.6.1 |
1865 | | * |
1866 | | * @par Description |
1867 | | * Entropy encode of all ctbs in a slice as per section 7.3.6.1 |
1868 | | * |
1869 | | * @param[inout] ps_entropy_ctxt |
1870 | | * pointer to entropy context (handle) |
1871 | | * |
1872 | | * @return success or failure error code |
1873 | | * |
1874 | | ****************************************************************************** |
1875 | | */ |
1876 | | WORD32 ihevce_encode_slice_data( |
1877 | | entropy_context_t *ps_entropy_ctxt, |
1878 | | ihevce_tile_params_t *ps_tile_params, |
1879 | | WORD32 *pi4_end_of_slice_flag) |
1880 | 94.8k | { |
1881 | 94.8k | WORD32 ret = IHEVCE_SUCCESS; |
1882 | 94.8k | WORD32 end_of_slice_seg_flag = 0; |
1883 | 94.8k | sps_t *ps_sps = ps_entropy_ctxt->ps_sps; |
1884 | 94.8k | pps_t *ps_pps = ps_entropy_ctxt->ps_pps; |
1885 | 94.8k | slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr; |
1886 | | |
1887 | 94.8k | cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt; |
1888 | | |
1889 | | /* State of previous CTB before it's terminate bin is encoded */ |
1890 | 94.8k | cab_ctxt_t s_cabac_prev_ctb; |
1891 | | |
1892 | | /* State after current CTB's encoding is complete but before |
1893 | | the termintate bin encoding */ |
1894 | 94.8k | cab_ctxt_t s_cabac_after_ctb; |
1895 | | |
1896 | | /* Storing the last 4 bytes before adding terminate bin |
1897 | | as these 4 bytes might get corrupted while encoding terminate bin */ |
1898 | 94.8k | UWORD32 u4_prev_ctb_temp, u4_cur_ctb_temp; |
1899 | 94.8k | WORD8 i1_last_cu_qp = 0; |
1900 | 94.8k | bitstrm_t *ps_bit_strm = &ps_entropy_ctxt->s_bit_strm; |
1901 | | |
1902 | 94.8k | WORD32 log2_ctb_size, ctb_size; |
1903 | | //WORD32 pic_width = ps_sps->i2_pic_width_in_luma_samples; |
1904 | | //WORD32 pic_height = ps_sps->i2_pic_height_in_luma_samples; |
1905 | 94.8k | WORD32 pic_width = ps_tile_params->i4_curr_tile_width; |
1906 | 94.8k | WORD32 pic_height = ps_tile_params->i4_curr_tile_height; |
1907 | 94.8k | WORD32 num_ctb_in_row; |
1908 | | |
1909 | 94.8k | WORD32 i4_curr_ctb_x, i4_curr_ctb_y; |
1910 | 94.8k | UWORD32 u4_slice_seg_hdr_size = (UWORD32)ps_entropy_ctxt->i4_slice_seg_len; |
1911 | 94.8k | UWORD32 u4_slice_start_offset = ps_bit_strm->u4_strm_buf_offset - u4_slice_seg_hdr_size; |
1912 | | |
1913 | 94.8k | WORD32 ctb_slice_address = ps_slice_hdr->i2_slice_address; |
1914 | 94.8k | WORD32 slice_qp = ps_slice_hdr->i1_slice_qp_delta + ps_pps->i1_pic_init_qp; |
1915 | 94.8k | WORD32 cabac_init_idc; |
1916 | 94.8k | WORD32 x0_frm, y0_frm; |
1917 | 94.8k | ctb_enc_loop_out_t *ps_first_ctb; // Points to first CTB of ctb-row |
1918 | 94.8k | ctb_enc_loop_out_t *ps_ctb; |
1919 | 94.8k | WORD32 ctb_ctr = 0; //count ctb encoded in a ctb-row |
1920 | | |
1921 | 94.8k | ihevce_sys_api_t *ps_sys_api = (ihevce_sys_api_t *)ps_entropy_ctxt->pv_sys_api; |
1922 | | |
1923 | | /* Structure to backup pic info in case we need to revert back to pervious |
1924 | | CTB when i4_slice_segment_mode is 2 */ |
1925 | 94.8k | s_pic_level_acc_info_t s_pic_level_info_backup; // info before |
1926 | | |
1927 | | /* Initialize the CTB size from sps parameters */ |
1928 | 94.8k | log2_ctb_size = |
1929 | 94.8k | ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size; |
1930 | | |
1931 | 94.8k | ctb_size = (1 << log2_ctb_size); |
1932 | | |
1933 | | /* sanity checks */ |
1934 | 94.8k | ASSERT((log2_ctb_size >= 3) && (log2_ctb_size <= 6)); |
1935 | | |
1936 | 94.8k | ps_entropy_ctxt->i1_log2_ctb_size = (WORD8)log2_ctb_size; |
1937 | | |
1938 | | /* Initiallise before starting slice. For single slice case both |
1939 | | x and y will be set to zero */ |
1940 | 94.8k | ps_entropy_ctxt->i4_ctb_x = ps_entropy_ctxt->i4_next_slice_seg_x; |
1941 | 94.8k | ps_entropy_ctxt->i4_ctb_y = ps_entropy_ctxt->i4_next_slice_seg_y; |
1942 | 94.8k | num_ctb_in_row = (ps_sps->i2_pic_width_in_luma_samples + ctb_size - 1) >> log2_ctb_size; |
1943 | | |
1944 | | /* initialize the cabac init idc based on slice type */ |
1945 | 94.8k | if(ps_slice_hdr->i1_slice_type == ISLICE) |
1946 | 31.0k | { |
1947 | 31.0k | cabac_init_idc = 0; |
1948 | 31.0k | } |
1949 | 63.8k | else if(ps_slice_hdr->i1_slice_type == PSLICE) |
1950 | 52.4k | { |
1951 | 52.4k | cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 2 : 1; |
1952 | 52.4k | } |
1953 | 11.3k | else |
1954 | 11.3k | { |
1955 | 11.3k | cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 1 : 2; |
1956 | 11.3k | } |
1957 | 94.8k | ps_cabac->i1_entropy_coding_sync_enabled_flag = ps_pps->i1_entropy_coding_sync_enabled_flag; |
1958 | | |
1959 | | /* Dependent slices should be ON only when slice segment mode is enabled */ |
1960 | 94.8k | if(ps_slice_hdr->i1_dependent_slice_flag == 1) |
1961 | 0 | { |
1962 | 0 | ASSERT( |
1963 | 0 | (ps_entropy_ctxt->i4_slice_segment_mode == 1) || |
1964 | 0 | (ps_entropy_ctxt->i4_slice_segment_mode == 2)); |
1965 | 0 | } |
1966 | | |
1967 | | /* initialize the cabac engine. For dependent slice segments |
1968 | | cabac context models will not be reset */ |
1969 | 94.8k | if(ps_slice_hdr->i1_dependent_slice_flag == 1) |
1970 | 0 | { |
1971 | 0 | ret = ihevce_cabac_reset(ps_cabac, ps_bit_strm, CABAC_MODE_ENCODE_BITS); |
1972 | 0 | } |
1973 | 94.8k | else |
1974 | 94.8k | { |
1975 | 94.8k | ret = ihevce_cabac_init( |
1976 | 94.8k | ps_cabac, |
1977 | 94.8k | ps_bit_strm, |
1978 | 94.8k | CLIP3(slice_qp, 0, IHEVC_MAX_QP), |
1979 | 94.8k | cabac_init_idc, |
1980 | 94.8k | CABAC_MODE_ENCODE_BITS); |
1981 | | |
1982 | | /* initialize qp to slice start qp */ |
1983 | 94.8k | ps_entropy_ctxt->i1_cur_qp = slice_qp; |
1984 | 94.8k | } |
1985 | | |
1986 | | /* initialize slice x and y offset in pels w.r.t top left conrner */ |
1987 | 94.8k | x0_frm = ps_entropy_ctxt->i4_ctb_x << log2_ctb_size; |
1988 | 94.8k | y0_frm = ps_entropy_ctxt->i4_ctb_y << log2_ctb_size; |
1989 | | |
1990 | | /* Pointing ctb structure to the correct CTB in frame based on |
1991 | | slice address */ |
1992 | 94.8k | ps_first_ctb = ps_entropy_ctxt->ps_frm_ctb + ctb_slice_address; |
1993 | 94.8k | ps_ctb = ps_first_ctb - 1; |
1994 | | |
1995 | | //ps_entropy_ctxt->i4_ctb_slice_x = 0; |
1996 | | //ps_entropy_ctxt->i4_ctb_slice_y = 0; |
1997 | | |
1998 | | /* Setting to NULL to detect if first CTB of slice itself |
1999 | | exceeds the i4_slice_segment_max_length. Will be used only if |
2000 | | i4_slice_segment_mode is non-zero */ |
2001 | 94.8k | s_cabac_prev_ctb.pu1_strm_buffer = NULL; |
2002 | | |
2003 | 94.8k | do |
2004 | 131k | { |
2005 | 131k | UWORD8 au1_cu_depth_top[8] = { 0 }, au1_cu_depth_left[8] = { 0 }; |
2006 | 131k | UWORD8 u1_skip_cu_top = 0; |
2007 | 131k | UWORD32 u4_skip_cu_left = 0; |
2008 | | |
2009 | | /* By default assume that slice-segment is going to end after |
2010 | | current CTB */ |
2011 | 131k | end_of_slice_seg_flag = 1; |
2012 | | |
2013 | 131k | i4_curr_ctb_x = ps_entropy_ctxt->i4_ctb_x; |
2014 | 131k | i4_curr_ctb_y = ps_entropy_ctxt->i4_ctb_y; |
2015 | | |
2016 | 131k | if(1 == ps_tile_params->i4_tiles_enabled_flag) |
2017 | 0 | { |
2018 | 0 | ps_ctb = ps_first_ctb + ctb_ctr; |
2019 | 0 | } |
2020 | 131k | else |
2021 | 131k | { |
2022 | 131k | ps_ctb++; |
2023 | 131k | } |
2024 | | |
2025 | | /* Store some parameters. Will be used if current CTB's encoding |
2026 | | has to be reverted in the event of overflow beyond i4_slice_segment_max_length */ |
2027 | 131k | if(2 == ps_entropy_ctxt->i4_slice_segment_mode) |
2028 | 0 | { |
2029 | | /* Store CU depths flag */ |
2030 | 0 | memcpy(au1_cu_depth_top, &ps_entropy_ctxt->pu1_cu_depth_top[i4_curr_ctb_x * 8], 8); |
2031 | 0 | memcpy(au1_cu_depth_left, ps_entropy_ctxt->au1_cu_depth_left, 8); |
2032 | | |
2033 | | /* Store CU skip flags */ |
2034 | 0 | u1_skip_cu_top = *(ps_entropy_ctxt->pu1_skip_cu_top + i4_curr_ctb_x); |
2035 | 0 | u4_skip_cu_left = ps_entropy_ctxt->u4_skip_cu_left; |
2036 | | |
2037 | | /* Backup current state of pic info */ |
2038 | 0 | s_pic_level_info_backup = *(ps_entropy_ctxt->ps_pic_level_info); |
2039 | 0 | } |
2040 | | |
2041 | | /* Section:7.3.7 Coding tree unit syntax */ |
2042 | | /* coding_tree_unit() inlined here */ |
2043 | 131k | ps_entropy_ctxt->i1_ctb_num_pcm_blks = 0; |
2044 | | |
2045 | | /* Simple Neigbour avail calculation */ |
2046 | 131k | ps_ctb->s_ctb_nbr_avail_flags.u1_left_avail = (x0_frm > 0); |
2047 | 131k | ps_ctb->s_ctb_nbr_avail_flags.u1_top_avail = (y0_frm > 0); |
2048 | | |
2049 | 131k | ps_entropy_ctxt->i4_cu_idx = 0; |
2050 | | |
2051 | | /* Encode SAO syntax as per section 7.3.8.3 */ |
2052 | 131k | if(ps_sps->i1_sample_adaptive_offset_enabled_flag) |
2053 | 70.3k | { |
2054 | 70.3k | if((ps_slice_hdr->i1_slice_sao_luma_flag) || (ps_slice_hdr->i1_slice_sao_chroma_flag)) |
2055 | 70.3k | { |
2056 | | /*PIC INFO: SAO encode biys*/ |
2057 | 70.3k | UWORD32 u4_bits_estimated_prev = |
2058 | 70.3k | ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12; |
2059 | | |
2060 | 70.3k | ret |= ihevce_cabac_encode_sao(ps_entropy_ctxt, ps_ctb); |
2061 | | |
2062 | 70.3k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
2063 | 70.3k | { |
2064 | 70.3k | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_sao += |
2065 | 70.3k | (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 - |
2066 | 70.3k | u4_bits_estimated_prev); |
2067 | 70.3k | } |
2068 | 70.3k | } |
2069 | 70.3k | } |
2070 | | |
2071 | 131k | ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 = 0; |
2072 | | |
2073 | 131k | if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS) |
2074 | 131k | { |
2075 | | /*PIC_INFO: Update total no.of CUS*/ |
2076 | 131k | ps_entropy_ctxt->ps_pic_level_info->i8_total_cu += ps_ctb->u1_num_cus_in_ctb; |
2077 | 131k | } |
2078 | | /* call recursive coding tree structure to encode all cus in ctb */ |
2079 | 131k | ret |= ihevce_encode_coding_quadtree( |
2080 | 131k | ps_entropy_ctxt, x0_frm, y0_frm, log2_ctb_size, 0, ps_ctb, ps_tile_params); |
2081 | | |
2082 | | /* post ctb encode increments */ |
2083 | 131k | ctb_ctr++; |
2084 | 131k | x0_frm += ctb_size; |
2085 | 131k | ps_entropy_ctxt->i4_ctb_x++; |
2086 | | //ps_entropy_ctxt->i4_ctb_slice_x++; |
2087 | | |
2088 | 131k | if(ps_pps->i1_entropy_coding_sync_enabled_flag && ps_entropy_ctxt->i4_ctb_x == 2) |
2089 | 635 | { |
2090 | | /*backup cabac context at end of second CTB(top right neighbour for start of bottom row)*/ |
2091 | 635 | ihevce_cabac_ctxt_backup(ps_cabac); |
2092 | 635 | } |
2093 | | |
2094 | | /* end of row check using x0_frm offset */ |
2095 | 131k | if(x0_frm >= pic_width) |
2096 | 104k | { |
2097 | 104k | ctb_ctr = 0; |
2098 | 104k | ps_first_ctb += num_ctb_in_row; |
2099 | 104k | x0_frm = 0; |
2100 | 104k | y0_frm += ctb_size; |
2101 | | |
2102 | 104k | ps_entropy_ctxt->i4_ctb_x = 0; |
2103 | 104k | ps_entropy_ctxt->i4_ctb_y++; |
2104 | | //ps_entropy_ctxt->i4_ctb_slice_y++; |
2105 | 104k | } |
2106 | | |
2107 | | /* Detect end of slice. Which would mean end-of-slice-segment too */ |
2108 | 131k | *pi4_end_of_slice_flag = (y0_frm >= pic_height); |
2109 | | |
2110 | 131k | if(0 == ps_entropy_ctxt->i4_slice_segment_mode) |
2111 | 131k | { |
2112 | | /* If slice ends then so does slice segment */ |
2113 | 131k | end_of_slice_seg_flag = *pi4_end_of_slice_flag; |
2114 | | |
2115 | | /* encode terminate bin */ |
2116 | 131k | ret |= ihevce_cabac_encode_terminate(ps_cabac, end_of_slice_seg_flag, 0); |
2117 | 131k | } |
2118 | 0 | else if(1 == ps_entropy_ctxt->i4_slice_segment_mode) |
2119 | 0 | { |
2120 | 0 | ps_entropy_ctxt->i4_slice_seg_len++; |
2121 | 0 | if((ps_entropy_ctxt->i4_slice_seg_len) >= ps_entropy_ctxt->i4_slice_segment_max_length) |
2122 | 0 | { |
2123 | | /* Store the address of CTB from where next slice segment will start */ |
2124 | 0 | ps_entropy_ctxt->i4_next_slice_seg_x = ps_entropy_ctxt->i4_ctb_x; |
2125 | 0 | ps_entropy_ctxt->i4_next_slice_seg_y = ps_entropy_ctxt->i4_ctb_y; |
2126 | 0 | } |
2127 | 0 | else |
2128 | 0 | { |
2129 | | /* If slice ends then so does slice segment */ |
2130 | 0 | end_of_slice_seg_flag = *pi4_end_of_slice_flag; |
2131 | 0 | } |
2132 | | |
2133 | | /* encode terminate bin */ |
2134 | 0 | ret |= ihevce_cabac_encode_terminate(ps_cabac, end_of_slice_seg_flag, 0); |
2135 | 0 | } |
2136 | 0 | else if(2 == ps_entropy_ctxt->i4_slice_segment_mode) |
2137 | 0 | { |
2138 | | //WORD32 i4_slice_seg_len_prev = i4_slice_seg_len; |
2139 | | |
2140 | | /* Store some parameters. Will be used to revert back to this state if |
2141 | | i4_slice_segment_max_length is not exceeded after encoding end-of-slice */ |
2142 | 0 | s_cabac_after_ctb = *ps_cabac; |
2143 | 0 | u4_cur_ctb_temp = |
2144 | 0 | *((UWORD32 *)(ps_cabac->pu1_strm_buffer + ps_cabac->u4_strm_buf_offset - 4)); |
2145 | | |
2146 | | /* encode terminate bin. For dependent slices, always simulate |
2147 | | end-of-slice to check if i4_slice_segment_max_length is surpassed */ |
2148 | 0 | ret |= ihevce_cabac_encode_terminate(ps_cabac, 1, 0); |
2149 | | |
2150 | | //i4_slice_seg_len_prev = i4_slice_seg_len; |
2151 | 0 | ps_entropy_ctxt->i4_slice_seg_len = |
2152 | 0 | (WORD32)(ps_cabac->u4_strm_buf_offset - u4_slice_start_offset); |
2153 | | |
2154 | | //ps_entropy_ctxt->i4_slice_seg_len = i4_slice_seg_len; //No need to update it. |
2155 | |
|
2156 | 0 | if(ps_entropy_ctxt->i4_slice_seg_len > ps_entropy_ctxt->i4_slice_segment_max_length) |
2157 | 0 | { |
2158 | 0 | if(s_cabac_prev_ctb.pu1_strm_buffer == NULL) |
2159 | 0 | { |
2160 | | /* Bytes in a single CTB has exceeded the i4_slice_segment_max_length |
2161 | | set by the user. Close the slice-segment and print a warning */ |
2162 | | |
2163 | | /* Store the address of CTB from where next slice segment will start */ |
2164 | 0 | ps_entropy_ctxt->i4_next_slice_seg_x = ps_entropy_ctxt->i4_ctb_x; |
2165 | 0 | ps_entropy_ctxt->i4_next_slice_seg_y = ps_entropy_ctxt->i4_ctb_y; |
2166 | |
|
2167 | 0 | ps_sys_api->ihevce_printf( |
2168 | 0 | ps_sys_api->pv_cb_handle, |
2169 | 0 | "IHEVCE_WARNING: CTB(%2d, %2d) encoded using %d bytes; " |
2170 | 0 | "this exceeds max slice segment size %d as requested " |
2171 | 0 | "by the user\n", |
2172 | 0 | i4_curr_ctb_x, |
2173 | 0 | i4_curr_ctb_y, |
2174 | 0 | ps_entropy_ctxt->i4_slice_seg_len, |
2175 | 0 | ps_entropy_ctxt->i4_slice_segment_max_length); |
2176 | 0 | } |
2177 | 0 | else /* Revert back to previous CTB's state and close current slice */ |
2178 | 0 | { |
2179 | 0 | *ps_cabac = s_cabac_prev_ctb; |
2180 | 0 | *((UWORD32 *)(ps_cabac->pu1_strm_buffer + ps_cabac->u4_strm_buf_offset - 4)) = |
2181 | 0 | u4_prev_ctb_temp; |
2182 | |
|
2183 | 0 | memcpy( |
2184 | 0 | &ps_entropy_ctxt->pu1_cu_depth_top[i4_curr_ctb_x * 8], au1_cu_depth_top, 8); |
2185 | 0 | memcpy(ps_entropy_ctxt->au1_cu_depth_left, au1_cu_depth_left, 8); |
2186 | |
|
2187 | 0 | *(ps_entropy_ctxt->pu1_skip_cu_top + i4_curr_ctb_x) = u1_skip_cu_top; |
2188 | 0 | ps_entropy_ctxt->u4_skip_cu_left = u4_skip_cu_left; |
2189 | |
|
2190 | 0 | ps_entropy_ctxt->i1_cur_qp = i1_last_cu_qp; |
2191 | | |
2192 | | /* Restore pic info */ |
2193 | 0 | *(ps_entropy_ctxt->ps_pic_level_info) = s_pic_level_info_backup; |
2194 | | |
2195 | | /* encode terminate bin with end-of-slice */ |
2196 | 0 | ret |= ihevce_cabac_encode_terminate(ps_cabac, 1, 0); |
2197 | | |
2198 | | /* Store the address of CTB from where next slice segment will start */ |
2199 | 0 | ps_entropy_ctxt->i4_next_slice_seg_x = i4_curr_ctb_x; |
2200 | 0 | ps_entropy_ctxt->i4_next_slice_seg_y = i4_curr_ctb_y; |
2201 | | |
2202 | | /* As we are reverted back to the previous CTB, force end of slice to zero */ |
2203 | 0 | *pi4_end_of_slice_flag = 0; |
2204 | 0 | } |
2205 | 0 | } |
2206 | 0 | else if(0 == *pi4_end_of_slice_flag) |
2207 | 0 | { |
2208 | | /* As this is not the end of slice, therefore revert back |
2209 | | the end-of-slice encoding and then add terminate bit */ |
2210 | | |
2211 | | /* Signal that this is not slice segment end */ |
2212 | 0 | end_of_slice_seg_flag = 0; |
2213 | |
|
2214 | 0 | *ps_cabac = s_cabac_after_ctb; |
2215 | 0 | *((UWORD32 *)(ps_cabac->pu1_strm_buffer + ps_cabac->u4_strm_buf_offset - 4)) = |
2216 | 0 | u4_cur_ctb_temp; |
2217 | | |
2218 | | /* encode terminate bin */ |
2219 | 0 | ret |= ihevce_cabac_encode_terminate(ps_cabac, 0, 0); |
2220 | 0 | } |
2221 | | |
2222 | | /* Update variables storing previous CTB's state in order to be |
2223 | | able to revert to previous CTB's state */ |
2224 | 0 | s_cabac_prev_ctb = s_cabac_after_ctb; |
2225 | 0 | u4_prev_ctb_temp = u4_cur_ctb_temp; |
2226 | |
|
2227 | 0 | i1_last_cu_qp = ps_entropy_ctxt->i1_cur_qp; |
2228 | 0 | } |
2229 | 0 | else //No other slice segment mode supported |
2230 | 0 | { |
2231 | 0 | ASSERT(0); |
2232 | 0 | } |
2233 | | |
2234 | 131k | AEV_TRACE("end_of_slice_flag", end_of_slice_seg_flag, ps_cabac->u4_range); |
2235 | | |
2236 | 131k | if((0 == ps_entropy_ctxt->i4_ctb_x) && (!end_of_slice_seg_flag) && |
2237 | 9.64k | (ps_pps->i1_entropy_coding_sync_enabled_flag)) |
2238 | 4.90k | { |
2239 | | /* initialize qp to slice start qp */ |
2240 | 4.90k | ps_entropy_ctxt->i1_cur_qp = slice_qp; |
2241 | | |
2242 | | /* flush and align to byte bounary for entropy sync every row */ |
2243 | 4.90k | ret |= ihevce_cabac_encode_terminate(ps_cabac, 1, 1); |
2244 | | |
2245 | | /*This will be entered only during row end, tap bits generated in that row to cal entry point offset*/ |
2246 | | /*add error check to make sure row count doesnt exceed the size of array allocated*/ |
2247 | 4.90k | ASSERT(ps_entropy_ctxt->i4_ctb_y < MAX_NUM_CTB_ROWS_FRM); |
2248 | 4.90k | ps_slice_hdr->pu4_entry_point_offset[ps_entropy_ctxt->i4_ctb_y] = |
2249 | 4.90k | ps_cabac->u4_strm_buf_offset; |
2250 | | |
2251 | | /*init the cabac context with top right neighbour*/ |
2252 | 4.90k | ret |= ihevce_cabac_ctxt_row_init(ps_cabac); |
2253 | 4.90k | } |
2254 | | |
2255 | 131k | } while(!end_of_slice_seg_flag); |
2256 | | |
2257 | 94.8k | if(end_of_slice_seg_flag && ps_pps->i1_entropy_coding_sync_enabled_flag) |
2258 | 2.48k | { |
2259 | 2.48k | ps_slice_hdr->pu4_entry_point_offset[ps_entropy_ctxt->i4_ctb_y] = |
2260 | 2.48k | ps_cabac->u4_strm_buf_offset; |
2261 | 2.48k | } |
2262 | | |
2263 | 94.8k | return ret; |
2264 | 94.8k | } |