Coverage Report

Created: 2026-03-20 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
1.22M
{
187
1.22M
    WORD32 error = IHEVCE_SUCCESS;
188
1.22M
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
189
1.22M
    intra_prev_rem_flags_t *ps_prev_mpm_rem_flags = &ps_enc_cu->as_prev_rem[0];
190
1.22M
    WORD32 i, num_parts;
191
192
    /* intra can only be 2Nx2N partition or a NxN partition     */
193
1.22M
    num_parts = (PART_NxN == part_mode) ? 4 : 1;
194
195
1.22M
    if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
196
227k
    {
197
227k
        WORD32 cu_size = ps_enc_cu->b4_cu_size << 3;
198
199
        /*PIC_INFO : INTRA CU in frame*/
200
227k
        ps_entropy_ctxt->ps_pic_level_info->i8_total_intra_cu++;
201
227k
        ps_entropy_ctxt->ps_pic_level_info->i8_total_pu += num_parts;
202
227k
        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
227k
        if(PART_2Nx2N == part_mode)
206
137k
        {
207
            // clang-format off
208
137k
            if(cu_size == 64)
209
1.64k
                ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_intra_pu[3]++;
210
136k
            else
211
136k
                ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_intra_pu[cu_size >> 4]++;
212
            // clang-format on
213
137k
        }
214
89.4k
        else if(PART_NxN == part_mode)
215
89.4k
        {
216
89.4k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_nxn_intra_pu++;
217
89.4k
        }
218
227k
    }
219
    /* encode prev intra pred mode flags  : context model based */
220
3.19M
    for(i = 0; i < num_parts; i++)
221
1.96M
    {
222
1.96M
        WORD32 prev_intra_pred_flag = ps_prev_mpm_rem_flags[i].b1_prev_intra_luma_pred_flag;
223
1.96M
        error |=
224
1.96M
            ihevce_cabac_encode_bin(ps_cabac, prev_intra_pred_flag, IHEVC_CAB_INTRA_LUMA_PRED_FLAG);
225
1.96M
        AEV_TRACE("prev_intra_pred_luma_flag", prev_intra_pred_flag, ps_cabac->u4_range);
226
1.96M
    }
227
228
    /* encode mpm_idx or rem_intra_pred_mode bypass bins */
229
3.19M
    for(i = 0; i < num_parts; i++)
230
1.96M
    {
231
1.96M
        if(ps_prev_mpm_rem_flags[i].b1_prev_intra_luma_pred_flag)
232
1.35M
        {
233
1.35M
            WORD32 mpm_idx = ps_prev_mpm_rem_flags[i].b2_mpm_idx;
234
235
            /* tunary bins for cmax = 2 */
236
1.35M
            WORD32 num_bins = mpm_idx ? 2 : 1;
237
1.35M
            UWORD32 bins = mpm_idx ? ((1 << 1) | (mpm_idx - 1)) : 0;
238
239
1.35M
            ASSERT(mpm_idx < 3);
240
241
1.35M
            error |= ihevce_cabac_encode_bypass_bins(ps_cabac, bins, num_bins);
242
1.35M
            AEV_TRACE("mpm_idx", mpm_idx, ps_cabac->u4_range);
243
1.35M
        }
244
615k
        else
245
615k
        {
246
615k
            WORD32 rem_intra_pred_mode = ps_prev_mpm_rem_flags[i].b5_rem_intra_pred_mode;
247
615k
            error |= ihevce_cabac_encode_bypass_bins(ps_cabac, rem_intra_pred_mode, 5);
248
615k
            AEV_TRACE("rem_intra_luma_pred_mode", rem_intra_pred_mode, ps_cabac->u4_range);
249
615k
        }
250
1.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
1.22M
    {
258
1.22M
        WORD32 chroma_pred_mode = ps_enc_cu->b3_chroma_intra_pred_mode;
259
1.22M
        WORD32 prefix_bin = (chroma_pred_mode == 4) ? 0 : 1;
260
261
        /* encode prefix bin */
262
1.22M
        error |= ihevce_cabac_encode_bin(ps_cabac, prefix_bin, IHEVC_CAB_CHROMA_PRED_MODE);
263
264
        /* encode suffix bins */
265
1.22M
        if(prefix_bin)
266
131k
        {
267
131k
            error |= ihevce_cabac_encode_bypass_bins(ps_cabac, chroma_pred_mode, 2);
268
131k
        }
269
1.22M
        AEV_TRACE("intra_chroma_pred_mode", chroma_pred_mode, ps_cabac->u4_range);
270
1.22M
    }
271
272
1.22M
    return (error);
273
1.22M
}
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
652k
{
307
652k
    WORD32 error = IHEVCE_SUCCESS;
308
652k
    WORD32 skip_flag = ps_enc_cu->b1_skip_flag;
309
652k
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
310
311
    /* CU top left co-ordinates w.r.t ctb */
312
652k
    WORD32 cu_x0 = ps_enc_cu->b3_cu_pos_x << 3;
313
652k
    WORD32 cu_y0 = ps_enc_cu->b3_cu_pos_y << 3;
314
315
    /* CU size in pels */
316
652k
    WORD32 cu_size = ps_enc_cu->b4_cu_size << 3;
317
318
    /* CU x co-ordinate w.r.t frame start           */
319
652k
    WORD32 ctb_x0_frm = (ps_entropy_ctxt->i4_ctb_x << ps_entropy_ctxt->i1_log2_ctb_size);
320
321
652k
    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
652k
    WORD32 x_pos = ((cu_x0_frm >> 3) & 0x7);
325
326
    /* bit postion from where left skip flag is extracted; 1bit per 8 pel  */
327
652k
    WORD32 y_pos = ((cu_y0 >> 3) & 0x7);
328
329
    /* top and left skip flags computed based on nbr availability */
330
652k
    UWORD8 *pu1_top_skip_flags = ps_entropy_ctxt->pu1_skip_cu_top + (cu_x0_frm >> 6);
331
652k
    UWORD32 u4_skip_left_flags = ps_entropy_ctxt->u4_skip_cu_left;
332
333
    /* context incerements based on top and left neigbours */
334
652k
    UWORD32 ctxt_inc = 0;
335
336
652k
    if(top_avail)
337
579k
    {
338
579k
        WORD32 val;
339
579k
        EXTRACT_BIT(val, pu1_top_skip_flags[0], x_pos);
340
579k
        ctxt_inc += val;
341
579k
    }
342
343
652k
    if(left_avail)
344
591k
    {
345
591k
        WORD32 val;
346
591k
        EXTRACT_BIT(val, u4_skip_left_flags, y_pos);
347
591k
        ctxt_inc += val;
348
591k
    }
349
350
652k
    if(CABAC_MODE_COMPUTE_BITS == ps_cabac->e_cabac_op_mode)
351
529k
    {
352
        //ASSERT(ctxt_inc == ps_entropy_ctxt->i4_num_nbr_skip_cus);
353
529k
        ctxt_inc = ps_entropy_ctxt->i4_num_nbr_skip_cus;
354
529k
        ASSERT(ctxt_inc < 3);
355
529k
        ASSERT((WORD32)ctxt_inc <= (top_avail + left_avail));
356
529k
    }
357
358
    /* encode the skip flag */
359
652k
    error |= ihevce_cabac_encode_bin(ps_cabac, skip_flag, (IHEVC_CAB_SKIP_FLAG + ctxt_inc));
360
361
652k
    AEV_TRACE("cu_skip_flag", skip_flag, ps_cabac->u4_range);
362
363
652k
    if(CABAC_MODE_ENCODE_BITS == ps_cabac->e_cabac_op_mode)
364
123k
    {
365
        /* update top and left skip flags only in encode mode */
366
123k
        if(skip_flag)
367
21.5k
        {
368
21.5k
            SET_BITS(pu1_top_skip_flags[0], x_pos, (cu_size >> 3));
369
21.5k
            SET_BITS(u4_skip_left_flags, y_pos, (cu_size >> 3));
370
21.5k
        }
371
101k
        else
372
101k
        {
373
101k
            CLEAR_BITS(pu1_top_skip_flags[0], x_pos, (cu_size >> 3));
374
101k
            CLEAR_BITS(u4_skip_left_flags, y_pos, (cu_size >> 3));
375
101k
        }
376
377
123k
        ps_entropy_ctxt->u4_skip_cu_left = u4_skip_left_flags;
378
123k
    }
379
380
652k
    return (error);
381
652k
}
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
1.15M
{
422
    /* Binarization depends on intra/inter, is_mincu, amp flag, cbsize == 8 */
423
1.15M
    WORD32 bins;
424
1.15M
    WORD32 bin_count, i;
425
1.15M
    WORD32 error = IHEVCE_SUCCESS;
426
427
1.15M
    (void)is_mincu;
428
1.15M
    (void)amp_enabled;
429
1.15M
    (void)cu_eq_8;
430
1.15M
    if(intra)
431
981k
    {
432
        /* sanity checks for intra part mode */
433
981k
        ASSERT(is_mincu);
434
981k
        ASSERT((part_mode == SIZE_NxN) || (part_mode == SIZE_2Nx2N));
435
436
981k
        bins = (part_mode == SIZE_2Nx2N) ? 1 : 0;
437
981k
        error |= ihevce_cabac_encode_bin(ps_cabac, bins, IHEVC_CAB_PART_MODE);
438
981k
    }
439
169k
    else
440
169k
    {
441
        /* sanity checks for inter part mode....Too many but good to have  */
442
169k
        ASSERT((amp_enabled == 0) || (amp_enabled == 1));
443
169k
        ASSERT((is_mincu == 0) || (is_mincu == 1));
444
169k
        ASSERT((cu_eq_8 == 0) || (cu_eq_8 == 1));
445
169k
        ASSERT((part_mode <= SIZE_nRx2N) && (part_mode >= SIZE_2Nx2N));
446
169k
        if(!amp_enabled)
447
169k
            ASSERT(part_mode <= SIZE_NxN);
448
169k
        if(!is_mincu)
449
169k
            ASSERT(part_mode != SIZE_NxN);
450
169k
        if(is_mincu)
451
169k
            ASSERT(part_mode <= SIZE_NxN);
452
169k
        if(cu_eq_8)
453
169k
            ASSERT(part_mode < SIZE_NxN);
454
169k
        if(cu_eq_8)
455
169k
            ASSERT(is_mincu);
456
457
        /* look up table for bins and number of bins for inter pred mode    */
458
169k
        if(!is_mincu)
459
92.5k
        {
460
92.5k
            bins = gu1_hevce_inter_part_mode_bins[amp_enabled][part_mode];
461
92.5k
        }
462
77.3k
        else
463
77.3k
        {
464
77.3k
            bins = gu1_hevce_inter_part_mode_bins_mincu[cu_eq_8][part_mode];
465
77.3k
        }
466
467
169k
        bin_count = (bins >> 4) & 0xF;
468
469
        /* Encode the context model based bins, max of 3 */
470
413k
        for(i = 0; i < MIN(bin_count, 3); i++)
471
243k
        {
472
            //TODO: HM-8.0-dev uses 0 context increment for bin2 (i===2) when amp is enabled
473
243k
            WORD32 ctxt_inc = IHEVC_CAB_PART_MODE + i;
474
243k
            WORD32 bin = (bins >> (3 - i)) & 0x1;
475
243k
            error |= ihevce_cabac_encode_bin(ps_cabac, bin, ctxt_inc);
476
243k
        }
477
478
        /* Encode the last bin as bypass bin for amp partitions */
479
169k
        if(bin_count == 4)
480
23.8k
        {
481
23.8k
            error |= ihevce_cabac_encode_bypass_bin(ps_cabac, (bins & 0x1));
482
23.8k
        }
483
169k
    }
484
1.15M
    AEV_TRACE("part_mode", part_mode, ps_cabac->u4_range);
485
1.15M
    return (error);
486
1.15M
}
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
199k
{
513
199k
    WORD32 ret = IHEVCE_SUCCESS;
514
199k
    WORD32 ctxt_inc = IHEVC_CAB_MERGE_IDX_EXT;
515
516
    /* sanity checks */
517
199k
    ASSERT((merge_idx >= 0) && (merge_idx < max_merge_cand));
518
519
    /* encode the merge idx only if required */
520
199k
    if(max_merge_cand > 1)
521
199k
    {
522
        /* encode the context modelled first bin */
523
199k
        ret |= ihevce_cabac_encode_bin(ps_cabac, (merge_idx > 0), ctxt_inc);
524
525
        /* encode the remaining bins as bypass tunary */
526
199k
        if((max_merge_cand > 2) && (merge_idx > 0))
527
8.86k
        {
528
8.86k
            ret |=
529
8.86k
                ihevce_cabac_encode_tunary_bypass(ps_cabac, (merge_idx - 1), (max_merge_cand - 2));
530
8.86k
        }
531
532
199k
        AEV_TRACE("merge_idx", merge_idx, ps_cabac->u4_range);
533
199k
    }
534
535
199k
    return (ret);
536
199k
}
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
11.0k
{
568
11.0k
    WORD32 ret = IHEVCE_SUCCESS;
569
11.0k
    WORD32 ctxt_inc;
570
571
11.0k
    ASSERT(inter_pred_idc <= PRED_BI);
572
573
    /* check if PU is 8x4/4x8  */
574
11.0k
    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
11.0k
    else
583
11.0k
    {
584
        /* larger PUs can be encoded as bi_pred/l0/l1 inter_pred_idc */
585
11.0k
        WORD32 is_bipred = (inter_pred_idc == PRED_BI);
586
587
11.0k
        ctxt_inc = IHEVC_CAB_INTER_PRED_IDC + cu_depth;
588
11.0k
        ret |= ihevce_cabac_encode_bin(ps_cabac, is_bipred, ctxt_inc);
589
590
11.0k
        if(!is_bipred)
591
9.20k
        {
592
9.20k
            ctxt_inc = IHEVC_CAB_INTER_PRED_IDC + 4;
593
9.20k
            ret |= ihevce_cabac_encode_bin(ps_cabac, inter_pred_idc, ctxt_inc);
594
9.20k
        }
595
11.0k
    }
596
597
11.0k
    AEV_TRACE("inter_pred_idc", inter_pred_idc, ps_cabac->u4_range);
598
599
11.0k
    return (ret);
600
11.0k
}
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
117k
{
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
117k
    WORD32 ret = IHEVCE_SUCCESS;
631
117k
    WORD32 ctxt_inc = IHEVC_CAB_INTER_REF_IDX;
632
633
    /* sanity checks */
634
117k
    ASSERT((ref_idx >= 0) && (ref_idx < active_refs));
635
636
    /* encode the ref idx only if required */
637
117k
    if(active_refs > 1)
638
57.5k
    {
639
        /* encode the context modelled first bin */
640
57.5k
        ret |= ihevce_cabac_encode_bin(ps_cabac, (ref_idx > 0), ctxt_inc);
641
642
57.5k
        if((active_refs > 2) && (ref_idx > 0))
643
9.17k
        {
644
            /* encode the context modelled second bin */
645
9.17k
            ctxt_inc++;
646
9.17k
            ret |= ihevce_cabac_encode_bin(ps_cabac, (ref_idx > 1), ctxt_inc);
647
9.17k
        }
648
649
57.5k
        if((active_refs > 3) && (ref_idx > 1))
650
3.42k
        {
651
            /* encode remaining bypass bins */
652
3.42k
            ret |= ihevce_cabac_encode_tunary_bypass(ps_cabac, (ref_idx - 2), (active_refs - 3));
653
3.42k
        }
654
655
57.5k
        AEV_TRACE("ref_idx", ref_idx, ps_cabac->u4_range);
656
57.5k
    }
657
658
117k
    return (ret);
659
117k
}
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
117k
{
683
117k
    WORD32 ret = IHEVCE_SUCCESS;
684
117k
    WORD32 mvd_x = ps_mvd->i2_mvx;
685
117k
    WORD32 mvd_y = ps_mvd->i2_mvy;
686
687
117k
    WORD32 abs_mvd_x = ABS(mvd_x);
688
117k
    WORD32 abs_mvd_y = ABS(mvd_y);
689
690
117k
    WORD32 abs_mvd_x_gt0 = abs_mvd_x > 0;
691
117k
    WORD32 abs_mvd_y_gt0 = abs_mvd_y > 0;
692
693
117k
    WORD32 abs_mvd_x_gt1 = abs_mvd_x > 1;
694
117k
    WORD32 abs_mvd_y_gt1 = abs_mvd_y > 1;
695
696
117k
    WORD32 ctxt_inc = IHEVC_CAB_MVD_GRT0;
697
698
    /* encode absmvd_x > 0 */
699
117k
    ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_x_gt0, ctxt_inc);
700
117k
    AEV_TRACE("abs_mvd_greater0_flag[0]", abs_mvd_x_gt0, ps_cabac->u4_range);
701
702
    /* encode absmvd_y > 0 */
703
117k
    ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_y_gt0, ctxt_inc);
704
117k
    AEV_TRACE("abs_mvd_greater0_flag[1]", abs_mvd_y_gt0, ps_cabac->u4_range);
705
706
117k
    ctxt_inc = IHEVC_CAB_MVD_GRT1;
707
708
    /* encode abs_mvd_x > 1 iff (abs_mvd_x > 0) */
709
117k
    if(abs_mvd_x_gt0)
710
90.3k
    {
711
90.3k
        ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_x_gt1, ctxt_inc);
712
90.3k
        AEV_TRACE("abs_mvd_greater1_flag[0]", abs_mvd_x_gt1, ps_cabac->u4_range);
713
90.3k
    }
714
715
    /* encode abs_mvd_y > 1 iff (abs_mvd_y > 0) */
716
117k
    if(abs_mvd_y_gt0)
717
90.9k
    {
718
90.9k
        ret |= ihevce_cabac_encode_bin(ps_cabac, abs_mvd_y_gt1, ctxt_inc);
719
90.9k
        AEV_TRACE("abs_mvd_greater1_flag[1]", abs_mvd_y_gt1, ps_cabac->u4_range);
720
90.9k
    }
721
722
    /* encode abs_mvd_x - 2 iff (abs_mvd_x > 1) */
723
117k
    if(abs_mvd_x_gt1)
724
86.0k
    {
725
86.0k
        ret |= ihevce_cabac_encode_egk(ps_cabac, (abs_mvd_x - 2), 1);
726
86.0k
        AEV_TRACE("abs_mvd_minus2[0]", (abs_mvd_x - 2), ps_cabac->u4_range);
727
86.0k
    }
728
729
    /* encode mvd_x sign iff (abs_mvd_x > 0) */
730
117k
    if(abs_mvd_x_gt0)
731
90.3k
    {
732
90.3k
        ret |= ihevce_cabac_encode_bypass_bin(ps_cabac, (mvd_x < 0));
733
90.3k
        AEV_TRACE("mvd_sign_flag[0]", (mvd_x < 0), ps_cabac->u4_range);
734
90.3k
    }
735
736
    /* encode abs_mvd_y - 2 iff (abs_mvd_y > 1) */
737
117k
    if(abs_mvd_y_gt1)
738
87.1k
    {
739
87.1k
        ret |= ihevce_cabac_encode_egk(ps_cabac, (abs_mvd_y - 2), 1);
740
87.1k
        AEV_TRACE("abs_mvd_minus2[1]", (abs_mvd_y - 2), ps_cabac->u4_range);
741
87.1k
    }
742
743
    /* encode mvd_y sign iff (abs_mvd_y > 0) */
744
117k
    if(abs_mvd_y_gt0)
745
90.9k
    {
746
90.9k
        ret |= ihevce_cabac_encode_bypass_bin(ps_cabac, (mvd_y < 0));
747
90.9k
        AEV_TRACE("mvd_sign_flag[1]", (mvd_y < 0), ps_cabac->u4_range);
748
90.9k
    }
749
750
117k
    return ret;
751
117k
}
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
278k
{
777
278k
    WORD32 ret = IHEVCE_SUCCESS;
778
779
278k
    slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr;
780
278k
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
781
278k
    pu_t *ps_pu = ps_enc_cu->ps_pu;
782
783
278k
    WORD32 merge_idx = ps_pu->b3_merge_idx;
784
278k
    WORD32 max_merge_cand = ps_slice_hdr->i1_max_num_merge_cand;
785
278k
    WORD32 ctxt_inc;
786
787
278k
    if(ps_enc_cu->b1_skip_flag)
788
108k
    {
789
108k
        WORD32 cu_size = ps_enc_cu->b4_cu_size << 3;
790
        /*PIC_INFO : SKIP CU in frame*/
791
108k
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
792
21.5k
        {
793
21.5k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_skip_cu++;
794
21.5k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_pu++;
795
21.5k
            if(cu_size == 64)
796
72
                ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[3]++;
797
21.4k
            else
798
21.4k
                ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[cu_size >> 4]++;
799
21.5k
        }
800
        /* encode the merge idx for skip cu and return */
801
108k
        ret |= ihevce_cabac_encode_merge_idx(ps_cabac, merge_idx, max_merge_cand);
802
108k
    }
803
169k
    else
804
169k
    {
805
        /* MODE_INTER */
806
169k
        WORD32 part_mode = ps_enc_cu->b3_part_mode;
807
169k
        WORD32 num_parts, i;
808
809
169k
        num_parts = (part_mode == SIZE_2Nx2N) ? 1 : ((part_mode == SIZE_NxN) ? 4 : 2);
810
811
        /*PIC_INFO : INTER CU in frame*/
812
169k
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
813
32.4k
        {
814
32.4k
            WORD32 cu_size = ps_enc_cu->b4_cu_size << 3;
815
32.4k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_inter_cu++;
816
32.4k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_pu += num_parts;
817
818
            // clang-format off
819
32.4k
            if(PART_2Nx2N == part_mode)
820
29.5k
            {
821
29.5k
                if(cu_size == 64)
822
162
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[3]++;
823
29.4k
                else
824
29.4k
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_2nx2n_inter_pu[cu_size >> 4]++;
825
29.5k
            }
826
2.91k
            else if((PART_2NxN == part_mode) || (PART_Nx2N == part_mode))
827
1.07k
            {
828
1.07k
                if(cu_size == 64)
829
39
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_smp_inter_pu[3]++;
830
1.03k
                else
831
1.03k
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_smp_inter_pu[cu_size >> 4]++;
832
1.07k
            }
833
1.83k
            else if((PART_2NxnU == part_mode) || (PART_2NxnD == part_mode) ||
834
270
                    (PART_nLx2N == part_mode) || (PART_nRx2N == part_mode))
835
1.83k
            {
836
1.83k
                ps_entropy_ctxt->ps_pic_level_info->i8_total_amp_inter_pu[cu_size >> 5]++;
837
1.83k
            }
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
32.4k
        }
844
845
        /* encode each pu partition */
846
376k
        for(i = 0; i < num_parts; i++)
847
206k
        {
848
            /* encode the merge flag context modelled bin */
849
206k
            WORD32 merge_flag;
850
206k
            UWORD32 u4_bits_estimated_merge_flag = 0;
851
206k
            ps_pu = ps_enc_cu->ps_pu + i;
852
853
            /* encode the merge flag context modelled bin */
854
206k
            merge_flag = ps_pu->b1_merge_flag;
855
206k
            u4_bits_estimated_merge_flag = ps_cabac->u4_bits_estimated_q12;
856
206k
            ctxt_inc = IHEVC_CAB_MERGE_FLAG_EXT;
857
206k
            ret |= ihevce_cabac_encode_bin(ps_cabac, merge_flag, ctxt_inc);
858
859
206k
            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
860
35.4k
            {
861
                // clang-format off
862
                /*PIC INFO : Populate merge flag */
863
35.4k
                ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_merge_flag =
864
35.4k
                    (ps_cabac->u4_bits_estimated_q12 -
865
35.4k
                        u4_bits_estimated_merge_flag);
866
                // clang-format on
867
35.4k
            }
868
206k
            AEV_TRACE("merge_flag", merge_flag, ps_cabac->u4_range);
869
870
206k
            if(merge_flag)
871
91.0k
            {
872
91.0k
                merge_idx = ps_pu->b3_merge_idx;
873
91.0k
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
874
23.0k
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_merge_pu++;
875
                /* encode the merge idx for the pu */
876
91.0k
                ret |= ihevce_cabac_encode_merge_idx(ps_cabac, merge_idx, max_merge_cand);
877
91.0k
            }
878
115k
            else
879
115k
            {
880
                /* encode the inter_pred_idc, ref_idx and mvd */
881
115k
                WORD32 inter_pred_idc = ps_pu->b2_pred_mode;
882
115k
                WORD32 ref_l0_active = ps_slice_hdr->i1_num_ref_idx_l0_active;
883
115k
                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
115k
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
887
12.3k
                {
888
12.3k
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_non_skipped_inter_pu++;
889
                    // clang-format off
890
12.3k
                    if(inter_pred_idc == PRED_L0)
891
11.7k
                    {
892
11.7k
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_L0_mode++;
893
11.7k
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_L0_ref_idx[ps_pu->mv.i1_l0_ref_idx]++;
894
11.7k
                    }
895
554
                    else if(inter_pred_idc == PRED_L1)
896
326
                    {
897
326
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_L1_mode++;
898
326
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_L1_ref_idx[ps_pu->mv.i1_l1_ref_idx]++;
899
326
                    }
900
228
                    else if(inter_pred_idc == PRED_BI)
901
228
                    {
902
228
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_BI_mode++;
903
228
                        if(inter_pred_idc != PRED_L1)
904
228
                            ps_entropy_ctxt->ps_pic_level_info->i8_total_L0_ref_idx[ps_pu->mv.i1_l0_ref_idx]++;
905
228
                        if(inter_pred_idc != PRED_L0)
906
228
                            ps_entropy_ctxt->ps_pic_level_info->i8_total_L1_ref_idx[ps_pu->mv.i1_l1_ref_idx]++;
907
228
                    }
908
                    // clang-format on
909
12.3k
                }
910
115k
                if(ps_slice_hdr->i1_slice_type == BSLICE)
911
11.0k
                {
912
                    /* Encode inter_pred_idc as per sec 9.3.2.9 Table9-36 */
913
11.0k
                    WORD32 pu_w_plus_pu_h;
914
11.0k
                    WORD32 inter_pred_idc = ps_pu->b2_pred_mode;
915
916
                    /* required to check if w+h==12 case */
917
11.0k
                    pu_w_plus_pu_h = ((ps_pu->b4_wd + 1) << 2) + ((ps_pu->b4_ht + 1) << 2);
918
919
11.0k
                    ret |= ihevce_cabac_encode_inter_pred_idc(
920
11.0k
                        ps_cabac, inter_pred_idc, cu_depth, pu_w_plus_pu_h);
921
11.0k
                }
922
104k
                else
923
104k
                {
924
104k
                    ASSERT(inter_pred_idc == 0);
925
104k
                }
926
927
                /* Decode ref idx and mvd  for L0 (PRED_L0 or PRED_BI) */
928
115k
                if(inter_pred_idc != PRED_L1)
929
112k
                {
930
112k
                    UWORD32 u4_bits_estimated_prev_mvd_ref_id;
931
                    /* encode L0 ref_idx  */
932
112k
                    WORD32 ref_idx_l0 = ps_pu->mv.i1_l0_ref_idx;
933
934
                    /*PIC INFO : Populate Ref Indx L0 Bits*/
935
112k
                    u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12;
936
112k
                    ret |= ihevce_cabac_encode_refidx(ps_cabac, ref_idx_l0, ref_l0_active);
937
938
112k
                    if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
939
12.0k
                    {
940
                        // clang-format off
941
12.0k
                        ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_ref_id +=
942
12.0k
                            (ps_cabac->u4_bits_estimated_q12 -
943
12.0k
                                u4_bits_estimated_prev_mvd_ref_id);
944
                        // clang-format on
945
12.0k
                    }
946
                    /* Encode the mvd for L0 */
947
                    /*PIC INFO : Populate MVD Bits*/
948
112k
                    u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12;
949
950
112k
                    ret |= ihevce_cabac_encode_mvd(ps_cabac, &ps_pu->mv.s_l0_mv);
951
952
112k
                    if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
953
12.0k
                    {  // clang-format off
954
12.0k
                        ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_mvd +=
955
12.0k
                            (ps_cabac->u4_bits_estimated_q12 -
956
12.0k
                                u4_bits_estimated_prev_mvd_ref_id);
957
                        // clang-format on
958
12.0k
                    }
959
960
                    /* Encode the mvp_l0_flag */
961
112k
                    ctxt_inc = IHEVC_CAB_MVP_L0L1;
962
112k
                    ret |= ihevce_cabac_encode_bin(ps_cabac, ps_pu->b1_l0_mvp_idx, ctxt_inc);
963
964
112k
                    AEV_TRACE("mvp_l0/l1_flag", ps_pu->b1_l0_mvp_idx, ps_cabac->u4_range);
965
112k
                }
966
967
                /* Encode ref idx and MVD for L1  (PRED_L1 or PRED_BI) */
968
115k
                if(inter_pred_idc != PRED_L0)
969
5.60k
                {
970
                    /* encode L1 ref_idx  */
971
5.60k
                    WORD32 ref_idx_l1 = ps_pu->mv.i1_l1_ref_idx;
972
973
5.60k
                    UWORD32 u4_bits_estimated_prev_mvd_ref_id;
974
                    /*PIC INFO : Populate Ref Indx L1 Bits*/
975
5.60k
                    u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12;
976
977
5.60k
                    ret |= ihevce_cabac_encode_refidx(ps_cabac, ref_idx_l1, ref_l1_active);
978
979
5.60k
                    if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
980
554
                    {  // clang-format off
981
554
                        ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_ref_id +=
982
554
                            (ps_cabac->u4_bits_estimated_q12 -
983
554
                                u4_bits_estimated_prev_mvd_ref_id);
984
554
                    }  // clang-format on
985
986
                    /* Check for zero mvd in case of bi_pred */
987
5.60k
                    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
5.60k
                    else
993
5.60k
                    {
994
                        /* Encode the mvd for L1 */
995
                        /*PIC INFO : Populate MVD Bits*/
996
5.60k
                        u4_bits_estimated_prev_mvd_ref_id = ps_cabac->u4_bits_estimated_q12;
997
998
                        /* Encode the mvd for L1 */
999
5.60k
                        ret |= ihevce_cabac_encode_mvd(ps_cabac, &ps_pu->mv.s_l1_mv);
1000
1001
5.60k
                        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1002
554
                        {
1003
554
                            ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_mvd +=
1004
554
                                (ps_cabac->u4_bits_estimated_q12 -
1005
554
                                 u4_bits_estimated_prev_mvd_ref_id);
1006
554
                        }
1007
5.60k
                    }
1008
1009
                    /* Encode the mvp_l1_flag */
1010
5.60k
                    ctxt_inc = IHEVC_CAB_MVP_L0L1;
1011
5.60k
                    ret |= ihevce_cabac_encode_bin(ps_cabac, ps_pu->b1_l1_mvp_idx, ctxt_inc);
1012
1013
5.60k
                    AEV_TRACE("mvp_l0/l1_flag", ps_pu->b1_l1_mvp_idx, ps_cabac->u4_range);
1014
5.60k
                }
1015
115k
            }
1016
206k
        }
1017
169k
    }
1018
1019
278k
    return ret;
1020
278k
}
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
1.50M
{
1057
1.50M
    WORD32 ret = IHEVCE_SUCCESS;
1058
1.50M
    sps_t *ps_sps = ps_entropy_ctxt->ps_sps;
1059
1.50M
    pps_t *ps_pps = ps_entropy_ctxt->ps_pps;
1060
1.50M
    slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr;
1061
1062
1.50M
    WORD32 skip_flag = 0;
1063
1.50M
    WORD32 no_res_flag = 0;
1064
1065
    /* CU top left co-ordinates w.r.t ctb */
1066
1.50M
    WORD32 cu_x0 = ps_enc_cu->b3_cu_pos_x << 3;
1067
1.50M
    WORD32 cu_y0 = ps_enc_cu->b3_cu_pos_y << 3;
1068
1069
    /* CU size in pels */
1070
1.50M
    WORD32 cu_size = ps_enc_cu->b4_cu_size << 3;
1071
1.50M
    WORD32 log2_cb_size;
1072
1073
1.50M
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
1074
1075
1.50M
    UWORD32 u4_header_bits_temp = ps_cabac->u4_bits_estimated_q12;
1076
1077
1.50M
    (void)cu_depth;
1078
1.50M
    (void)top_avail;
1079
1.50M
    (void)left_avail;
1080
    /* Sanity checks */
1081
1.50M
    ASSERT((cu_x0 + cu_size) <= (1 << ps_entropy_ctxt->i1_log2_ctb_size));
1082
1.50M
    ASSERT((cu_y0 + cu_size) <= (1 << ps_entropy_ctxt->i1_log2_ctb_size));
1083
1084
    /* code tq bypass flag */
1085
1.50M
    ASSERT(ps_pps->i1_transquant_bypass_enable_flag == 0);
1086
1087
    /* log2_cb_size based on cu size */
1088
1.50M
    GETRANGE(log2_cb_size, cu_size);
1089
1.50M
    log2_cb_size -= 1;
1090
1091
1.50M
    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
1.50M
    if(ps_slice_hdr->i1_slice_type != ISLICE)
1100
652k
    {
1101
652k
        skip_flag = ps_enc_cu->b1_skip_flag;
1102
1103
652k
        ret |= ihevce_cabac_encode_skip_flag(ps_entropy_ctxt, ps_enc_cu, top_avail, left_avail);
1104
652k
    }
1105
    /*PIC_INFO : Total CU in frame based on cu size */
1106
1.50M
    if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1107
281k
    {
1108
        // clang-format off
1109
281k
        if(cu_size == 64)
1110
2.00k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_cu_based_on_size[3]++;
1111
279k
        else
1112
279k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_cu_based_on_size[cu_size >> 4]++;
1113
        // clang-format on
1114
281k
    }
1115
1.50M
    if(skip_flag)
1116
108k
    {
1117
        /* encode merge idx for the skip cu */
1118
108k
        ret |= ihevce_cabac_encode_inter_pu(ps_entropy_ctxt, ps_enc_cu, cu_depth);
1119
1120
108k
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1121
21.5k
        {
1122
            /*PIC INFO: Populated non-coded TUs in CU*/
1123
21.5k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_non_coded_tu +=
1124
21.5k
                ps_enc_cu->u2_num_tus_in_cu;
1125
            // clang-format off
1126
21.5k
            if(cu_size == 64)
1127
72
                ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] +=
1128
72
                    ps_enc_cu->u2_num_tus_in_cu;
1129
21.4k
            else if(cu_size == 32)
1130
4.13k
                ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] +=
1131
4.13k
                    ps_enc_cu->u2_num_tus_in_cu;
1132
17.3k
            else
1133
17.3k
                ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[cu_size >> 3] +=
1134
17.3k
                    ps_enc_cu->u2_num_tus_in_cu;
1135
            // clang-format on
1136
1137
            /*PIC INFO: Populate cu header bits*/
1138
21.5k
            ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cu_hdr_bits +=
1139
21.5k
                (ps_cabac->u4_bits_estimated_q12 - u4_header_bits_temp);
1140
21.5k
        }
1141
108k
    }
1142
1.39M
    else
1143
1.39M
    {
1144
1.39M
        WORD32 pred_mode = PRED_MODE_INTRA;
1145
1.39M
        WORD32 part_mode = ps_enc_cu->b3_part_mode;
1146
1.39M
        WORD32 pcm_flag = ps_enc_cu->b1_pcm_flag;
1147
1.39M
        WORD32 is_mincu;
1148
1.39M
        WORD32 is_intra;
1149
1150
1.39M
        is_mincu = (cu_size == (1 << ps_sps->i1_log2_min_coding_block_size));
1151
        /* encode pred mode flag for inter slice */
1152
1.39M
        if(ps_slice_hdr->i1_slice_type != ISLICE)
1153
543k
        {
1154
543k
            pred_mode = ps_enc_cu->b1_pred_mode_flag;
1155
1156
543k
            ret |= ihevce_cabac_encode_bin(ps_cabac, pred_mode, IHEVC_CAB_PRED_MODE);
1157
1158
543k
            AEV_TRACE("pred_mode_flag", pred_mode, ps_cabac->u4_range);
1159
543k
        }
1160
1.39M
        is_intra = (PRED_MODE_INTRA == pred_mode);
1161
1162
        /* encode partition mode for inter pred or smallest intra pred cu */
1163
1.39M
        if((!is_intra) || is_mincu)
1164
1.15M
        {
1165
1.15M
            WORD32 amp_enabled = ps_sps->i1_amp_enabled_flag;
1166
1.15M
            WORD32 cusize_8 = (cu_size == 8);
1167
1168
1.15M
            ret |= ihevce_cabac_encode_part_mode(
1169
1.15M
                ps_cabac, is_intra, is_mincu, amp_enabled, cusize_8, part_mode);
1170
1.15M
        }
1171
244k
        else
1172
244k
        {
1173
244k
            ASSERT(part_mode == SIZE_2Nx2N);
1174
244k
        }
1175
1176
        /* encode intra / inter pu modes of the current CU */
1177
1.39M
        if(is_intra)
1178
1.22M
        {
1179
            /* NOTE: I_PCM not supported in encoder */
1180
1.22M
            ASSERT(0 == pcm_flag);
1181
1.22M
            ASSERT(0 == ps_sps->i1_pcm_enabled_flag);
1182
1183
1.22M
            ret |= ihevce_cabac_encode_intra_pu(ps_entropy_ctxt, part_mode, ps_enc_cu);
1184
1.22M
        }
1185
169k
        else
1186
169k
        {
1187
169k
            ret |= ihevce_cabac_encode_inter_pu(ps_entropy_ctxt, ps_enc_cu, cu_depth);
1188
169k
        }
1189
        /* encode no residue syntax flag and transform tree conditionally */
1190
1.39M
        if(!pcm_flag)
1191
1.39M
        {
1192
1.39M
            pu_t *ps_pu = &ps_enc_cu->ps_pu[0];
1193
1.39M
            WORD32 merge_cu;
1194
            /* Encode residue syntax flag for inter cus not merged as 2Nx2N */
1195
1.39M
            if(!is_intra)
1196
169k
                merge_cu = (part_mode == PART_2Nx2N) && ps_pu->b1_merge_flag;
1197
1198
1.39M
            if(!is_intra && !merge_cu)
1199
114k
            {
1200
114k
                no_res_flag = ps_enc_cu->b1_no_residual_syntax_flag;
1201
1202
114k
#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
114k
                ret |= ihevce_cabac_encode_bin(ps_cabac, !no_res_flag, IHEVC_CAB_NORES_IDX);
1209
1210
114k
                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
114k
            }
1217
            /*initialize header bits*/
1218
1.39M
            ps_cabac->u4_header_bits_estimated_q12 = ps_cabac->u4_bits_estimated_q12;
1219
1220
1.39M
            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1221
259k
            {  // clang-format off
1222
                /*PIC INFO: Populate cu header bits*/
1223
259k
                ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_cu_hdr_bits +=
1224
259k
                    (ps_cabac->u4_bits_estimated_q12 - u4_header_bits_temp);
1225
259k
            }  // clang-format on
1226
1227
1.39M
            ps_cabac->u4_true_tu_split_flag_q12 = 0;
1228
            /* encode transform tree if no_residue_flag is 0 */
1229
1.39M
            if(!no_res_flag)
1230
1.36M
            {
1231
1.36M
                ps_entropy_ctxt->i4_tu_idx = 0;
1232
1233
1.36M
                ret |= ihevce_encode_transform_tree(
1234
1.36M
                    ps_entropy_ctxt, cu_x0, cu_y0, log2_cb_size, 0, 0, ps_enc_cu);
1235
1.36M
            }
1236
34.9k
            else
1237
34.9k
            {
1238
34.9k
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1239
1.54k
                {
1240
                    /*PIC INFO: Populated non-coded TUs in CU*/
1241
1.54k
                    ps_entropy_ctxt->ps_pic_level_info->i8_total_non_coded_tu +=
1242
1.54k
                        ps_enc_cu->u2_num_tus_in_cu;
1243
                    // clang-format off
1244
1.54k
                    if(cu_size == 64)
1245
100
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] +=
1246
100
                            ps_enc_cu->u2_num_tus_in_cu;
1247
1.44k
                    else if(cu_size == 32)
1248
746
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[3] +=
1249
746
                            ps_enc_cu->u2_num_tus_in_cu;
1250
694
                    else
1251
694
                        ps_entropy_ctxt->ps_pic_level_info->i8_total_tu_based_on_size[cu_size >> 3] +=
1252
694
                            ps_enc_cu->u2_num_tus_in_cu;
1253
                    // clang-format on
1254
1.54k
                }
1255
34.9k
            }
1256
1.39M
            ps_cabac->u4_cbf_bits_q12 = ps_cabac->u4_bits_estimated_q12 -
1257
1.39M
                                        ps_cabac->u4_header_bits_estimated_q12 -
1258
1.39M
                                        ps_cabac->u4_true_tu_split_flag_q12;
1259
1.39M
        }
1260
1.39M
    }
1261
1262
    /*duplicate the qp values for 8x8 CU array to maintain neighbour qp*/
1263
1.50M
    if(CABAC_MODE_ENCODE_BITS == ps_entropy_ctxt->s_cabac_ctxt.e_cabac_op_mode)
1264
281k
    {
1265
281k
        WORD32 i, j;
1266
281k
        WORD32 cur_cu_offset, cur_qp, qp_left, qp_top;
1267
281k
        WORD32 is_last_blk_in_qg;
1268
        /* CU x co-ordinate w.r.t frame start           */
1269
281k
        WORD32 ctb_x0_frm = (ps_entropy_ctxt->i4_ctb_x << ps_entropy_ctxt->i1_log2_ctb_size);
1270
1271
281k
        WORD32 cu_x0_frm = cu_x0 + ctb_x0_frm;
1272
1273
        /* CU y co-ordinate w.r.t frame start           */
1274
281k
        WORD32 ctb_y0_frm = (ps_entropy_ctxt->i4_ctb_y << ps_entropy_ctxt->i1_log2_ctb_size);
1275
1276
281k
        WORD32 cu_y0_frm = cu_y0 + ctb_y0_frm;
1277
1278
281k
        WORD32 pic_width = ps_sps->i2_pic_width_in_luma_samples;
1279
281k
        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
281k
        WORD32 log2_min_cu_qp_delta_size =
1286
281k
            ps_entropy_ctxt->i1_log2_ctb_size - ps_entropy_ctxt->ps_pps->i1_diff_cu_qp_delta_depth;
1287
281k
        UWORD32 min_cu_qp_delta_size = 1 << log2_min_cu_qp_delta_size;
1288
1289
281k
        WORD32 block_addr_align = 15 << (log2_min_cu_qp_delta_size - 3);
1290
1291
281k
        ps_entropy_ctxt->i4_qg_pos_x = ps_enc_cu->b3_cu_pos_x & block_addr_align;
1292
281k
        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
281k
        is_last_blk_in_qg =
1301
281k
            ((cu_x0 + cu_size) >=
1302
281k
                 ((ps_entropy_ctxt->i4_qg_pos_x << 3) + (WORD32)min_cu_qp_delta_size) &&
1303
281k
             (cu_y0 + cu_size) >=
1304
281k
                 ((ps_entropy_ctxt->i4_qg_pos_y << 3) + (WORD32)min_cu_qp_delta_size));
1305
1306
        /* case 2 : x direction incomplete ctb */
1307
281k
        if((cu_x0_frm + cu_size) >= pic_width)
1308
18.8k
        {
1309
18.8k
            is_last_blk_in_qg |=
1310
18.8k
                ((cu_y0 + cu_size) >=
1311
18.8k
                 ((ps_entropy_ctxt->i4_qg_pos_y << 3) + (WORD32)min_cu_qp_delta_size));
1312
18.8k
        }
1313
1314
        /* case 2 : y direction incomplete ctb */
1315
281k
        if((cu_y0_frm + cu_size) >= pic_height)
1316
20.5k
        {
1317
20.5k
            is_last_blk_in_qg |=
1318
20.5k
                ((cu_x0 + cu_size) >=
1319
20.5k
                 ((ps_entropy_ctxt->i4_qg_pos_x << 3) + (WORD32)min_cu_qp_delta_size));
1320
20.5k
        }
1321
1322
281k
        cur_cu_offset = ps_enc_cu->b3_cu_pos_x + (ps_enc_cu->b3_cu_pos_y * 8);
1323
1324
281k
        if((ps_entropy_ctxt->i4_is_cu_cbf_zero || no_res_flag || skip_flag) &&
1325
60.4k
           ((ps_entropy_ctxt->i1_encode_qp_delta)))
1326
14.7k
        {
1327
14.7k
            {  // clang-format off
1328
                /*it should remember average of qp_top and qp_left*/
1329
14.7k
                if(ps_entropy_ctxt->i4_qg_pos_x > 0)
1330
11.4k
                {
1331
11.4k
                    qp_left =
1332
11.4k
                        ps_entropy_ctxt->ai4_8x8_cu_qp[(ps_entropy_ctxt->i4_qg_pos_x - 1) +
1333
11.4k
                                            (ps_entropy_ctxt->i4_qg_pos_y * 8)];
1334
11.4k
                }
1335
14.7k
                if(ps_entropy_ctxt->i4_qg_pos_y > 0)
1336
11.0k
                {
1337
11.0k
                    qp_top =
1338
11.0k
                        ps_entropy_ctxt->ai4_8x8_cu_qp[ps_entropy_ctxt->i4_qg_pos_x +
1339
11.0k
                                            (ps_entropy_ctxt->i4_qg_pos_y - 1) *
1340
11.0k
                                                8];
1341
11.0k
                }  // clang-format on
1342
14.7k
                if(ps_entropy_ctxt->i4_qg_pos_x == 0)
1343
3.27k
                {
1344
                    /*previous coded Qp*/
1345
3.27k
                    qp_left = ps_entropy_ctxt->i1_cur_qp;
1346
3.27k
                }
1347
14.7k
                if(ps_entropy_ctxt->i4_qg_pos_y == 0)
1348
3.64k
                {
1349
                    /*previous coded Qp*/
1350
3.64k
                    qp_top = ps_entropy_ctxt->i1_cur_qp;
1351
3.64k
                }
1352
14.7k
                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
14.7k
                if(is_last_blk_in_qg)
1355
14.7k
                    ps_entropy_ctxt->i1_cur_qp = cur_qp;
1356
14.7k
            }
1357
14.7k
        }
1358
266k
        else
1359
266k
        {
1360
266k
            cur_qp = (WORD32)ps_enc_cu->ps_enc_tu->s_tu.b7_qp;
1361
266k
        }
1362
1363
281k
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1364
281k
        {
1365
281k
            WORD32 temp = 0;
1366
            /*PIC_INFO: Accumalate average qp, min qp and max qp*/
1367
281k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_qp += cur_qp;
1368
281k
            if(cu_size == 64)
1369
2.00k
                temp = 6;
1370
279k
            else if(cu_size == 32)
1371
12.7k
                temp = 4;
1372
266k
            else if(cu_size == 16)
1373
47.6k
                temp = 2;
1374
218k
            else if(cu_size == 8)
1375
218k
                temp = 0;
1376
1377
281k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_qp_min_cu += (cur_qp * (1 << temp));
1378
281k
            if(cur_qp < ps_entropy_ctxt->ps_pic_level_info->i4_min_qp)
1379
2.94k
                ps_entropy_ctxt->ps_pic_level_info->i4_min_qp = cur_qp;
1380
281k
            if(cur_qp > ps_entropy_ctxt->ps_pic_level_info->i4_max_qp)
1381
2.42k
                ps_entropy_ctxt->ps_pic_level_info->i4_max_qp = cur_qp;
1382
281k
        }
1383
1384
662k
        for(i = 0; i < (WORD32)ps_enc_cu->b4_cu_size; i++)
1385
381k
        {
1386
1.12M
            for(j = 0; j < (WORD32)ps_enc_cu->b4_cu_size; j++)
1387
741k
            {
1388
741k
                ps_entropy_ctxt->ai4_8x8_cu_qp[cur_cu_offset + (i * 8) + j] = cur_qp;
1389
741k
            }
1390
381k
        }
1391
281k
        ps_entropy_ctxt->i4_is_cu_cbf_zero = 1;
1392
281k
    }
1393
1394
1.50M
    return ret;
1395
1.50M
}
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
62.4k
{
1418
62.4k
    WORD32 error = IHEVCE_SUCCESS;
1419
62.4k
    sao_enc_t *ps_sao;
1420
62.4k
    nbr_avail_flags_t *ps_ctb_nbr_avail_flags;
1421
62.4k
    slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr;
1422
62.4k
    cab_ctxt_t *ps_cabac = &ps_entropy_ctxt->s_cabac_ctxt;
1423
1424
62.4k
    UWORD8 u1_left_avail, u1_top_avail;
1425
1426
62.4k
    ps_ctb_nbr_avail_flags = &ps_ctb_enc_loop_out->s_ctb_nbr_avail_flags;
1427
1428
62.4k
    ps_sao = &ps_ctb_enc_loop_out->s_sao;
1429
1430
62.4k
    ASSERT(ps_sao->b1_sao_merge_left_flag < 2);
1431
1432
62.4k
    u1_left_avail = ps_ctb_nbr_avail_flags->u1_left_avail;
1433
62.4k
    u1_top_avail = ps_ctb_nbr_avail_flags->u1_top_avail;
1434
1435
62.4k
    if(u1_left_avail == 1)
1436
41.7k
    {
1437
        /*Encode the sao_merge_left_flag as FL as per table 9-32*/
1438
41.7k
        error |=
1439
41.7k
            ihevce_cabac_encode_bin(ps_cabac, ps_sao->b1_sao_merge_left_flag, IHEVC_CAB_SAO_MERGE);
1440
1441
41.7k
        AEV_TRACE("sao_merge_flag", ps_sao->b1_sao_merge_left_flag, ps_cabac->u4_range);
1442
41.7k
    }
1443
1444
62.4k
    if((u1_top_avail == 1) && (!ps_sao->b1_sao_merge_left_flag))
1445
31.7k
    {
1446
        /*Encode the sao_merge_up_flag as FL as per table 9-32*/
1447
31.7k
        error |=
1448
31.7k
            ihevce_cabac_encode_bin(ps_cabac, ps_sao->b1_sao_merge_up_flag, IHEVC_CAB_SAO_MERGE);
1449
1450
31.7k
        AEV_TRACE("sao_merge_flag", ps_sao->b1_sao_merge_up_flag, ps_cabac->u4_range);
1451
31.7k
    }
1452
1453
62.4k
    if((!ps_sao->b1_sao_merge_left_flag) && (!ps_sao->b1_sao_merge_up_flag))
1454
49.2k
    {
1455
49.2k
        WORD32 c_idx;
1456
49.2k
        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
196k
        for(c_idx = 0; c_idx < 3; c_idx++)
1460
147k
        {
1461
147k
            if((ps_slice_hdr->i1_slice_sao_luma_flag && c_idx == 0) ||
1462
98.4k
               (ps_slice_hdr->i1_slice_sao_chroma_flag && c_idx > 0))
1463
147k
            {
1464
147k
                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
147k
                if(c_idx < 2)
1473
98.4k
                {
1474
98.4k
                    WORD32 sao_type_idx_temp;
1475
1476
98.4k
                    ASSERT(ps_sao->b3_cb_type_idx == ps_sao->b3_cr_type_idx);
1477
1478
98.4k
                    sao_type_idx = c_idx ? ps_sao->b3_cb_type_idx : ps_sao->b3_y_type_idx;
1479
1480
98.4k
                    ctxt_bin = sao_type_idx ? 1 : 0;
1481
1482
98.4k
                    if(sao_type_idx > 1)
1483
48.4k
                    {
1484
48.4k
                        sao_type_idx_temp = 2;
1485
48.4k
                    }
1486
49.9k
                    else
1487
49.9k
                    {
1488
49.9k
                        sao_type_idx_temp = sao_type_idx;
1489
49.9k
                    }
1490
1491
98.4k
                    ASSERT(sao_type_idx_temp < 3);
1492
1493
                    /*Encode the first bin as context bin as per table 9-37*/
1494
98.4k
                    error |= ihevce_cabac_encode_bin(ps_cabac, ctxt_bin, IHEVC_CAB_SAO_TYPE);
1495
1496
98.4k
                    if(sao_type_idx_temp)
1497
48.4k
                    {
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
48.4k
                        error |= ihevce_cabac_encode_bypass_bin(ps_cabac, sao_type_idx_temp - 1);
1513
48.4k
                    }
1514
98.4k
                    AEV_TRACE("sao_type_idx", sao_type_idx_temp, ps_cabac->u4_range);
1515
98.4k
                }
1516
1517
147k
                if(sao_type_idx != 0)
1518
57.4k
                {
1519
57.4k
                    WORD32 i;
1520
57.4k
                    UWORD8 u1_bit_depth = ps_entropy_ctxt->ps_sps->i1_bit_depth_luma_minus8 + 8;
1521
57.4k
                    WORD8 *sao_offset;
1522
57.4k
                    WORD32 sao_band_position;
1523
57.4k
                    WORD32 c_max = (1 << (MIN(u1_bit_depth, 10) - 5)) -
1524
57.4k
                                   1;  //( 1 << (MIN(BIT_DEPTH, 10) - 5)) - 1;
1525
1526
57.4k
                    if(c_idx == 0)
1527
39.4k
                    {
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
39.4k
                        sao_offset = &ps_sao->u1_y_offset[1];
1533
39.4k
                        sao_band_position = ps_sao->b5_y_band_pos;
1534
39.4k
                    }
1535
17.9k
                    else if(c_idx == 1)
1536
8.97k
                    {
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
8.97k
                        sao_offset = &ps_sao->u1_cb_offset[1];
1542
8.97k
                        sao_band_position = ps_sao->b5_cb_band_pos;
1543
8.97k
                    }
1544
8.97k
                    else
1545
8.97k
                    {
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
8.97k
                        sao_offset = &ps_sao->u1_cr_offset[1];
1551
8.97k
                        sao_band_position = ps_sao->b5_cr_band_pos;
1552
8.97k
                    }
1553
1554
287k
                    for(i = 0; i < 4; i++)
1555
229k
                    {
1556
                        /*Encode the sao offset value as tunary bypass*/
1557
229k
                        error |=
1558
229k
                            ihevce_cabac_encode_tunary_bypass(ps_cabac, abs(sao_offset[i]), c_max);
1559
1560
229k
                        AEV_TRACE("sao_offset_abs", abs(sao_offset[i]), ps_cabac->u4_range);
1561
229k
                    }
1562
1563
                    /*Band offset case*/
1564
57.4k
                    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
57.4k
                    else
1589
57.4k
                    {
1590
                        /*Encode the sao edge offset class for luma and chroma as FL as per table 9-32*/
1591
57.4k
                        if(c_idx == 0)
1592
39.4k
                        {
1593
39.4k
                            error |= ihevce_cabac_encode_bypass_bins(
1594
39.4k
                                ps_cabac, (ps_sao->b3_y_type_idx - 2), 2);
1595
39.4k
                            AEV_TRACE(
1596
39.4k
                                "sao_eo_class", (ps_sao->b3_y_type_idx - 2), ps_cabac->u4_range);
1597
39.4k
                        }
1598
1599
57.4k
                        if(c_idx == 1)
1600
8.97k
                        {
1601
8.97k
                            ASSERT(ps_sao->b3_cb_type_idx == ps_sao->b3_cr_type_idx);
1602
8.97k
                            error |= ihevce_cabac_encode_bypass_bins(
1603
8.97k
                                ps_cabac, (ps_sao->b3_cb_type_idx - 2), 2);
1604
8.97k
                            AEV_TRACE(
1605
8.97k
                                "sao_eo_class", (ps_sao->b3_cb_type_idx - 2), ps_cabac->u4_range);
1606
8.97k
                        }
1607
57.4k
                    }
1608
57.4k
                }
1609
147k
            }
1610
147k
        }
1611
49.2k
    }
1612
1613
62.4k
    return (error);
1614
62.4k
}
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
379k
{
1656
379k
    WORD32 ret = IHEVCE_SUCCESS;
1657
379k
    sps_t *ps_sps = ps_entropy_ctxt->ps_sps;
1658
379k
    pps_t *ps_pps = ps_entropy_ctxt->ps_pps;
1659
379k
    WORD32 split_cu_flag;
1660
379k
    WORD32 cu_idx = ps_entropy_ctxt->i4_cu_idx;
1661
379k
    cu_enc_loop_out_t *ps_enc_cu = ps_ctb->ps_enc_cu + cu_idx;
1662
1663
    /* CU size in pels */
1664
379k
    WORD32 cu_size = ps_enc_cu->b4_cu_size << 3;
1665
1666
379k
    WORD32 pic_width = ps_tile_params->i4_curr_tile_width;
1667
379k
    WORD32 pic_height = ps_tile_params->i4_curr_tile_height;
1668
1669
379k
    WORD32 log2_min_cb_size = ps_sps->i1_log2_min_coding_block_size;
1670
379k
    WORD32 ctb_size = (1 << (log2_cb_size + ct_depth));
1671
379k
    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
379k
    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
379k
    UWORD8 *pu1_cu_depth_left = &ps_entropy_ctxt->au1_cu_depth_left[0];
1678
1679
    /* calculation of top and left nbr availability */
1680
379k
    WORD32 top_avail;
1681
379k
    WORD32 left_avail;
1682
1683
    /* top and left cu within ctb  or outside ctb boundary */
1684
379k
    left_avail = (x0_frm & (ctb_size - 1)) ? 1 : ps_ctb->s_ctb_nbr_avail_flags.u1_left_avail;
1685
379k
    top_avail = (y0_frm & (ctb_size - 1)) ? 1 : ps_ctb->s_ctb_nbr_avail_flags.u1_top_avail;
1686
1687
    /* Sanity checks */
1688
379k
    ASSERT(ct_depth <= 3);
1689
379k
    ASSERT((cu_idx >= 0) && (cu_idx < ps_ctb->u1_num_cus_in_ctb));
1690
379k
    ASSERT(cu_size >= (1 << log2_min_cb_size));
1691
379k
    ASSERT(((ps_enc_cu->b3_cu_pos_x << 3) + cu_size) <= (UWORD32)ctb_size);
1692
379k
    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
379k
    if(((x0_frm + (1 << log2_cb_size)) <= pic_width) &&
1696
371k
       ((y0_frm + (1 << log2_cb_size)) <= pic_height) && (log2_cb_size > log2_min_cb_size) &&
1697
145k
       (ps_entropy_ctxt->i1_ctb_num_pcm_blks == 0))
1698
145k
    {
1699
        /* encode the split cu flag */
1700
145k
        WORD32 ctxt_inc = IHEVC_CAB_SPLIT_CU_FLAG;
1701
145k
        UWORD32 u4_bits_estimated_prev;
1702
        /* Context increment for skip flag as per Table 9-38 */
1703
145k
        if(top_avail)
1704
115k
        {
1705
115k
            ctxt_inc += (pu1_cu_depth_top[x0_frm >> 3] > ct_depth);
1706
115k
        }
1707
1708
145k
        if(left_avail)
1709
123k
        {
1710
123k
            ctxt_inc += (pu1_cu_depth_left[(y0_frm >> 3) & 0x7] > ct_depth);
1711
123k
        }
1712
1713
        /* split if actual cu size is smaller than target cu size */
1714
145k
        split_cu_flag = cu_size < (1 << log2_cb_size);
1715
145k
        u4_bits_estimated_prev = ps_cabac->u4_bits_estimated_q12;
1716
145k
        ret |= ihevce_cabac_encode_bin(ps_cabac, split_cu_flag, ctxt_inc);
1717
1718
145k
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1719
145k
        {  // clang-format off
1720
            /*PIC INFO : populate cu split flag*/
1721
145k
            ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_split_cu_flag +=
1722
145k
                (ps_cabac->u4_bits_estimated_q12 - u4_bits_estimated_prev);
1723
145k
        }  // clang-format on
1724
1725
145k
        AEV_TRACE("split_cu_flag", split_cu_flag, ps_cabac->u4_range);
1726
145k
        if(split_cu_flag == 0)
1727
62.4k
        {
1728
62.4k
            AEV_TRACE("split_cu_flag : X0", (x0_frm >> 6) << 6, ps_cabac->u4_range);
1729
62.4k
            AEV_TRACE("split_cu_flag : Y0", (y0_frm >> 6) << 6, ps_cabac->u4_range);
1730
62.4k
        }
1731
145k
    }
1732
233k
    else
1733
233k
    {
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
233k
        if(log2_cb_size > ps_sps->i1_log2_min_coding_block_size)
1739
14.9k
            split_cu_flag = 1;
1740
218k
        else
1741
218k
            split_cu_flag = 0;
1742
233k
    }
1743
1744
    /************************************************************************/
1745
    /* Reset qp delata coded flag appropriately so as to signal qp rightly  */
1746
    /* during transform coding                                              */
1747
    /************************************************************************/
1748
379k
    if((ps_pps->i1_cu_qp_delta_enabled_flag) && (ct_depth <= (ps_pps->i1_diff_cu_qp_delta_depth)))
1749
1750
111k
    {
1751
111k
        ps_entropy_ctxt->i1_encode_qp_delta = 1;
1752
111k
    }
1753
    /*else
1754
    {
1755
        ps_entropy_ctxt->i1_encode_qp_delta = 0;
1756
    }*/
1757
1758
379k
    if(split_cu_flag)
1759
98.3k
    {
1760
        /* recurse quad tree till a leaf node is reached */
1761
98.3k
        WORD32 x1_frm = x0_frm + ((1 << log2_cb_size) >> 1);
1762
98.3k
        WORD32 y1_frm = y0_frm + ((1 << log2_cb_size) >> 1);
1763
1764
        /* node0 of quad tree */
1765
98.3k
        ret |= ihevce_encode_coding_quadtree(
1766
98.3k
            ps_entropy_ctxt, x0_frm, y0_frm, log2_cb_size - 1, ct_depth + 1, ps_ctb, ps_tile_params);
1767
1768
98.3k
        if(x1_frm < pic_width)
1769
91.3k
        { /* node1 of quad tree */
1770
91.3k
            ret |= ihevce_encode_coding_quadtree(
1771
91.3k
                ps_entropy_ctxt,
1772
91.3k
                x1_frm,
1773
91.3k
                y0_frm,
1774
91.3k
                log2_cb_size - 1,
1775
91.3k
                ct_depth + 1,
1776
91.3k
                ps_ctb,
1777
91.3k
                ps_tile_params);
1778
91.3k
        }
1779
1780
98.3k
        if(y1_frm < pic_height)
1781
91.1k
        {
1782
            /* node2 of quad tree */
1783
91.1k
            ret |= ihevce_encode_coding_quadtree(
1784
91.1k
                ps_entropy_ctxt,
1785
91.1k
                x0_frm,
1786
91.1k
                y1_frm,
1787
91.1k
                log2_cb_size - 1,
1788
91.1k
                ct_depth + 1,
1789
91.1k
                ps_ctb,
1790
91.1k
                ps_tile_params);
1791
91.1k
        }
1792
1793
98.3k
        if((x1_frm < pic_width) && (y1_frm < pic_height))
1794
85.0k
        {
1795
            /* node3 of quad tree */
1796
85.0k
            ret |= ihevce_encode_coding_quadtree(
1797
85.0k
                ps_entropy_ctxt,
1798
85.0k
                x1_frm,
1799
85.0k
                y1_frm,
1800
85.0k
                log2_cb_size - 1,
1801
85.0k
                ct_depth + 1,
1802
85.0k
                ps_ctb,
1803
85.0k
                ps_tile_params);
1804
85.0k
        }
1805
98.3k
    }
1806
281k
    else
1807
281k
    {
1808
        /* leaf node is reached! Encode the CU */
1809
281k
        WORD32 i;
1810
1811
        /* sanity checks */
1812
281k
        ASSERT(ps_entropy_ctxt->i1_ctb_num_pcm_blks == 0);
1813
1814
281k
        if(ps_entropy_ctxt->i1_ctb_num_pcm_blks == 0)
1815
281k
        {
1816
281k
            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
281k
            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1820
281k
            {
1821
281k
                ps_entropy_ctxt->ps_pic_level_info->i8_total_tu += ps_enc_cu->u2_num_tus_in_cu;
1822
281k
            }
1823
1824
281k
            ret |= ihevce_cabac_encode_coding_unit(
1825
281k
                ps_entropy_ctxt, ps_enc_cu, ct_depth, top_avail, left_avail);
1826
1827
281k
            if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
1828
281k
            {
1829
                // clang-format off
1830
281k
                if(PRED_MODE_INTRA == ps_enc_cu->b1_pred_mode_flag)
1831
227k
                {
1832
227k
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_intra +=
1833
227k
                        (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 -
1834
227k
                            u4_bits_eztimated);
1835
227k
                }
1836
54.0k
                else
1837
54.0k
                {
1838
54.0k
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_inter +=
1839
54.0k
                        (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 -
1840
54.0k
                            u4_bits_eztimated);
1841
54.0k
                }
1842
                // clang-format on
1843
281k
            }
1844
281k
        }
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
281k
        ps_entropy_ctxt->i4_cu_idx++;
1851
662k
        for(i = 0; i < (cu_size >> 3); i++)
1852
381k
        {
1853
381k
            pu1_cu_depth_top[(x0_frm >> 3) + i] = ct_depth;
1854
381k
            pu1_cu_depth_left[((y0_frm >> 3) & 0x7) + i] = ct_depth;
1855
381k
        }
1856
281k
    }
1857
1858
379k
    return ret;
1859
379k
}
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
2.25k
{
1881
2.25k
    WORD32 ret = IHEVCE_SUCCESS;
1882
2.25k
    WORD32 end_of_slice_seg_flag = 0;
1883
2.25k
    sps_t *ps_sps = ps_entropy_ctxt->ps_sps;
1884
2.25k
    pps_t *ps_pps = ps_entropy_ctxt->ps_pps;
1885
2.25k
    slice_header_t *ps_slice_hdr = ps_entropy_ctxt->ps_slice_hdr;
1886
1887
2.25k
    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
2.25k
    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
2.25k
    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
2.25k
    UWORD32 u4_prev_ctb_temp, u4_cur_ctb_temp;
1899
2.25k
    WORD8 i1_last_cu_qp = 0;
1900
2.25k
    bitstrm_t *ps_bit_strm = &ps_entropy_ctxt->s_bit_strm;
1901
1902
2.25k
    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
2.25k
    WORD32 pic_width = ps_tile_params->i4_curr_tile_width;
1906
2.25k
    WORD32 pic_height = ps_tile_params->i4_curr_tile_height;
1907
2.25k
    WORD32 num_ctb_in_row;
1908
1909
2.25k
    WORD32 i4_curr_ctb_x, i4_curr_ctb_y;
1910
2.25k
    UWORD32 u4_slice_seg_hdr_size = (UWORD32)ps_entropy_ctxt->i4_slice_seg_len;
1911
2.25k
    UWORD32 u4_slice_start_offset = ps_bit_strm->u4_strm_buf_offset - u4_slice_seg_hdr_size;
1912
1913
2.25k
    WORD32 ctb_slice_address = ps_slice_hdr->i2_slice_address;
1914
2.25k
    WORD32 slice_qp = ps_slice_hdr->i1_slice_qp_delta + ps_pps->i1_pic_init_qp;
1915
2.25k
    WORD32 cabac_init_idc;
1916
2.25k
    WORD32 x0_frm, y0_frm;
1917
2.25k
    ctb_enc_loop_out_t *ps_first_ctb;  // Points to first CTB of ctb-row
1918
2.25k
    ctb_enc_loop_out_t *ps_ctb;
1919
2.25k
    WORD32 ctb_ctr = 0;  //count ctb encoded in a ctb-row
1920
1921
2.25k
    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
2.25k
    s_pic_level_acc_info_t s_pic_level_info_backup;  // info before
1926
1927
    /* Initialize the CTB size from sps parameters */
1928
2.25k
    log2_ctb_size =
1929
2.25k
        ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1930
1931
2.25k
    ctb_size = (1 << log2_ctb_size);
1932
1933
    /* sanity checks */
1934
2.25k
    ASSERT((log2_ctb_size >= 3) && (log2_ctb_size <= 6));
1935
1936
2.25k
    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
2.25k
    ps_entropy_ctxt->i4_ctb_x = ps_entropy_ctxt->i4_next_slice_seg_x;
1941
2.25k
    ps_entropy_ctxt->i4_ctb_y = ps_entropy_ctxt->i4_next_slice_seg_y;
1942
2.25k
    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
2.25k
    if(ps_slice_hdr->i1_slice_type == ISLICE)
1946
500
    {
1947
500
        cabac_init_idc = 0;
1948
500
    }
1949
1.75k
    else if(ps_slice_hdr->i1_slice_type == PSLICE)
1950
1.71k
    {
1951
1.71k
        cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 2 : 1;
1952
1.71k
    }
1953
38
    else
1954
38
    {
1955
38
        cabac_init_idc = ps_slice_hdr->i1_cabac_init_flag ? 1 : 2;
1956
38
    }
1957
2.25k
    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
2.25k
    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
2.25k
    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
2.25k
    else
1974
2.25k
    {
1975
2.25k
        ret = ihevce_cabac_init(
1976
2.25k
            ps_cabac,
1977
2.25k
            ps_bit_strm,
1978
2.25k
            CLIP3(slice_qp, 0, IHEVC_MAX_QP),
1979
2.25k
            cabac_init_idc,
1980
2.25k
            CABAC_MODE_ENCODE_BITS);
1981
1982
        /* initialize qp to slice start qp */
1983
2.25k
        ps_entropy_ctxt->i1_cur_qp = slice_qp;
1984
2.25k
    }
1985
1986
    /* initialize slice x and y offset in pels w.r.t top left conrner */
1987
2.25k
    x0_frm = ps_entropy_ctxt->i4_ctb_x << log2_ctb_size;
1988
2.25k
    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
2.25k
    ps_first_ctb = ps_entropy_ctxt->ps_frm_ctb + ctb_slice_address;
1993
2.25k
    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
2.25k
    s_cabac_prev_ctb.pu1_strm_buffer = NULL;
2002
2003
2.25k
    do
2004
13.7k
    {
2005
13.7k
        UWORD8 au1_cu_depth_top[8] = { 0 }, au1_cu_depth_left[8] = { 0 };
2006
13.7k
        UWORD8 u1_skip_cu_top = 0;
2007
13.7k
        UWORD32 u4_skip_cu_left = 0;
2008
2009
        /* By default assume that slice-segment is going to end after
2010
        current CTB */
2011
13.7k
        end_of_slice_seg_flag = 1;
2012
2013
13.7k
        i4_curr_ctb_x = ps_entropy_ctxt->i4_ctb_x;
2014
13.7k
        i4_curr_ctb_y = ps_entropy_ctxt->i4_ctb_y;
2015
2016
13.7k
        if(1 == ps_tile_params->i4_tiles_enabled_flag)
2017
0
        {
2018
0
            ps_ctb = ps_first_ctb + ctb_ctr;
2019
0
        }
2020
13.7k
        else
2021
13.7k
        {
2022
13.7k
            ps_ctb++;
2023
13.7k
        }
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
13.7k
        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
13.7k
        ps_entropy_ctxt->i1_ctb_num_pcm_blks = 0;
2044
2045
        /* Simple Neigbour avail calculation */
2046
13.7k
        ps_ctb->s_ctb_nbr_avail_flags.u1_left_avail = (x0_frm > 0);
2047
13.7k
        ps_ctb->s_ctb_nbr_avail_flags.u1_top_avail = (y0_frm > 0);
2048
2049
13.7k
        ps_entropy_ctxt->i4_cu_idx = 0;
2050
2051
        /* Encode SAO syntax as per section 7.3.8.3 */
2052
13.7k
        if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
2053
7.62k
        {
2054
7.62k
            if((ps_slice_hdr->i1_slice_sao_luma_flag) || (ps_slice_hdr->i1_slice_sao_chroma_flag))
2055
7.62k
            {
2056
                /*PIC INFO: SAO encode biys*/
2057
7.62k
                UWORD32 u4_bits_estimated_prev =
2058
7.62k
                    ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12;
2059
2060
7.62k
                ret |= ihevce_cabac_encode_sao(ps_entropy_ctxt, ps_ctb);
2061
2062
7.62k
                if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
2063
7.62k
                {
2064
7.62k
                    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_sao +=
2065
7.62k
                        (ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 -
2066
7.62k
                         u4_bits_estimated_prev);
2067
7.62k
                }
2068
7.62k
            }
2069
7.62k
        }
2070
2071
13.7k
        ps_entropy_ctxt->s_cabac_ctxt.u4_bits_estimated_q12 = 0;
2072
2073
13.7k
        if(ps_cabac->e_cabac_op_mode == CABAC_MODE_ENCODE_BITS)
2074
13.7k
        {
2075
            /*PIC_INFO: Update total no.of CUS*/
2076
13.7k
            ps_entropy_ctxt->ps_pic_level_info->i8_total_cu += ps_ctb->u1_num_cus_in_ctb;
2077
13.7k
        }
2078
        /* call recursive coding tree structure to encode all cus in ctb */
2079
13.7k
        ret |= ihevce_encode_coding_quadtree(
2080
13.7k
            ps_entropy_ctxt, x0_frm, y0_frm, log2_ctb_size, 0, ps_ctb, ps_tile_params);
2081
2082
        /* post ctb encode increments */
2083
13.7k
        ctb_ctr++;
2084
13.7k
        x0_frm += ctb_size;
2085
13.7k
        ps_entropy_ctxt->i4_ctb_x++;
2086
        //ps_entropy_ctxt->i4_ctb_slice_x++;
2087
2088
13.7k
        if(ps_pps->i1_entropy_coding_sync_enabled_flag && ps_entropy_ctxt->i4_ctb_x == 2)
2089
393
        {
2090
            /*backup cabac context at end of second CTB(top right neighbour for start of bottom row)*/
2091
393
            ihevce_cabac_ctxt_backup(ps_cabac);
2092
393
        }
2093
2094
        /* end of row check using x0_frm offset */
2095
13.7k
        if(x0_frm >= pic_width)
2096
4.62k
        {
2097
4.62k
            ctb_ctr = 0;
2098
4.62k
            ps_first_ctb += num_ctb_in_row;
2099
4.62k
            x0_frm = 0;
2100
4.62k
            y0_frm += ctb_size;
2101
2102
4.62k
            ps_entropy_ctxt->i4_ctb_x = 0;
2103
4.62k
            ps_entropy_ctxt->i4_ctb_y++;
2104
            //ps_entropy_ctxt->i4_ctb_slice_y++;
2105
4.62k
        }
2106
2107
        /* Detect end of slice. Which would mean end-of-slice-segment too */
2108
13.7k
        *pi4_end_of_slice_flag = (y0_frm >= pic_height);
2109
2110
13.7k
        if(0 == ps_entropy_ctxt->i4_slice_segment_mode)
2111
13.7k
        {
2112
            /* If slice ends then so does slice segment  */
2113
13.7k
            end_of_slice_seg_flag = *pi4_end_of_slice_flag;
2114
2115
            /*  encode terminate bin */
2116
13.7k
            ret |= ihevce_cabac_encode_terminate(ps_cabac, end_of_slice_seg_flag, 0);
2117
13.7k
        }
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
13.7k
        AEV_TRACE("end_of_slice_flag", end_of_slice_seg_flag, ps_cabac->u4_range);
2235
2236
13.7k
        if((0 == ps_entropy_ctxt->i4_ctb_x) && (!end_of_slice_seg_flag) &&
2237
2.37k
           (ps_pps->i1_entropy_coding_sync_enabled_flag))
2238
744
        {
2239
            /* initialize qp to slice start qp */
2240
744
            ps_entropy_ctxt->i1_cur_qp = slice_qp;
2241
2242
            /* flush and align to byte bounary for entropy sync every row */
2243
744
            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
744
            ASSERT(ps_entropy_ctxt->i4_ctb_y < MAX_NUM_CTB_ROWS_FRM);
2248
744
            ps_slice_hdr->pu4_entry_point_offset[ps_entropy_ctxt->i4_ctb_y] =
2249
744
                ps_cabac->u4_strm_buf_offset;
2250
2251
            /*init the cabac context with top right neighbour*/
2252
744
            ret |= ihevce_cabac_ctxt_row_init(ps_cabac);
2253
744
        }
2254
2255
13.7k
    } while(!end_of_slice_seg_flag);
2256
2257
2.25k
    if(end_of_slice_seg_flag && ps_pps->i1_entropy_coding_sync_enabled_flag)
2258
292
    {
2259
292
        ps_slice_hdr->pu4_entry_point_offset[ps_entropy_ctxt->i4_ctb_y] =
2260
292
            ps_cabac->u4_strm_buf_offset;
2261
292
    }
2262
2263
2.25k
    return ret;
2264
2.25k
}