Coverage Report

Created: 2026-12-01 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_enc_loop_utils.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
******************************************************************************
23
* \file ihevce_enc_loop_utils.c
24
*
25
* \brief
26
*    This file contains utility functions of Encode loop
27
*
28
* \date
29
*    18/09/2012
30
*
31
* \author
32
*    Ittiam
33
*
34
*
35
* List of Functions
36
*
37
*
38
******************************************************************************
39
*/
40
41
/*****************************************************************************/
42
/* File Includes                                                             */
43
/*****************************************************************************/
44
/* System include files */
45
#include <stdio.h>
46
#include <string.h>
47
#include <stdlib.h>
48
#include <assert.h>
49
#include <stdarg.h>
50
#include <math.h>
51
#include <limits.h>
52
53
/* User include files */
54
#include "ihevc_typedefs.h"
55
#include "itt_video_api.h"
56
#include "ihevce_api.h"
57
58
#include "rc_cntrl_param.h"
59
#include "rc_frame_info_collector.h"
60
#include "rc_look_ahead_params.h"
61
62
#include "ihevc_defs.h"
63
#include "ihevc_macros.h"
64
#include "ihevc_debug.h"
65
#include "ihevc_structs.h"
66
#include "ihevc_platform_macros.h"
67
#include "ihevc_deblk.h"
68
#include "ihevc_itrans_recon.h"
69
#include "ihevc_chroma_itrans_recon.h"
70
#include "ihevc_chroma_intra_pred.h"
71
#include "ihevc_intra_pred.h"
72
#include "ihevc_inter_pred.h"
73
#include "ihevc_mem_fns.h"
74
#include "ihevc_padding.h"
75
#include "ihevc_weighted_pred.h"
76
#include "ihevc_sao.h"
77
#include "ihevc_resi_trans.h"
78
#include "ihevc_quant_iquant_ssd.h"
79
#include "ihevc_cabac_tables.h"
80
#include "ihevc_common_tables.h"
81
82
#include "ihevce_defs.h"
83
#include "ihevce_hle_interface.h"
84
#include "ihevce_lap_enc_structs.h"
85
#include "ihevce_multi_thrd_structs.h"
86
#include "ihevce_multi_thrd_funcs.h"
87
#include "ihevce_me_common_defs.h"
88
#include "ihevce_had_satd.h"
89
#include "ihevce_error_codes.h"
90
#include "ihevce_bitstream.h"
91
#include "ihevce_cabac.h"
92
#include "ihevce_rdoq_macros.h"
93
#include "ihevce_function_selector.h"
94
#include "ihevce_enc_structs.h"
95
#include "ihevce_entropy_structs.h"
96
#include "ihevce_cmn_utils_instr_set_router.h"
97
#include "ihevce_ipe_instr_set_router.h"
98
#include "ihevce_decomp_pre_intra_structs.h"
99
#include "ihevce_decomp_pre_intra_pass.h"
100
#include "ihevce_enc_loop_structs.h"
101
#include "ihevce_nbr_avail.h"
102
#include "ihevce_enc_loop_utils.h"
103
#include "ihevce_sub_pic_rc.h"
104
#include "ihevce_global_tables.h"
105
#include "ihevce_bs_compute_ctb.h"
106
#include "ihevce_cabac_rdo.h"
107
#include "ihevce_deblk.h"
108
#include "ihevce_frame_process.h"
109
#include "ihevce_rc_enc_structs.h"
110
#include "hme_datatype.h"
111
#include "hme_interface.h"
112
#include "hme_common_defs.h"
113
#include "hme_defs.h"
114
#include "hme_common_utils.h"
115
#include "ihevce_me_instr_set_router.h"
116
#include "ihevce_enc_subpel_gen.h"
117
#include "ihevce_inter_pred.h"
118
#include "ihevce_mv_pred.h"
119
#include "ihevce_mv_pred_merge.h"
120
#include "ihevce_enc_loop_inter_mode_sifter.h"
121
#include "ihevce_enc_cu_recursion.h"
122
#include "ihevce_enc_loop_pass.h"
123
#include "ihevce_common_utils.h"
124
#include "ihevce_dep_mngr_interface.h"
125
#include "ihevce_sao.h"
126
#include "ihevce_tile_interface.h"
127
#include "ihevce_profile.h"
128
#include "ihevce_stasino_helpers.h"
129
#include "ihevce_tu_tree_selector.h"
130
131
/*****************************************************************************/
132
/* Globals                                                                   */
133
/*****************************************************************************/
134
135
extern UWORD16 gau2_ihevce_cabac_bin_to_bits[64 * 2];
136
extern const UWORD8 gu1_hevce_scan4x4[3][16];
137
extern const UWORD8 gu1_hevce_sigcoeff_ctxtinc[4][16];
138
extern const UWORD8 gu1_hevce_sigcoeff_ctxtinc_tr4[16];
139
extern const UWORD8 gu1_hevce_sigcoeff_ctxtinc_00[16];
140
141
/*****************************************************************************/
142
/* Constant Macros                                                           */
143
/*****************************************************************************/
144
#define ENABLE_ZERO_CBF 1
145
#define DISABLE_RDOQ_INTRA 0
146
147
/*****************************************************************************/
148
/* Function Definitions                                                      */
149
/*****************************************************************************/
150
void *ihevce_tu_tree_update(
151
    tu_prms_t *ps_tu_prms,
152
    WORD32 *pnum_tu_in_cu,
153
    WORD32 depth,
154
    WORD32 tu_split_flag,
155
    WORD32 tu_early_cbf,
156
    WORD32 i4_x_off,
157
    WORD32 i4_y_off)
158
1.96M
{
159
    //WORD32 tu_split_flag = p_tu_split_flag[0];
160
1.96M
    WORD32 p_tu_split_flag[4];
161
1.96M
    WORD32 p_tu_early_cbf[4];
162
163
1.96M
    WORD32 tu_size = ps_tu_prms->u1_tu_size;
164
165
1.96M
    if(((tu_size >> depth) >= 16) && (tu_split_flag & 0x1))
166
157k
    {
167
157k
        if((tu_size >> depth) == 32)
168
38.4k
        {
169
            /* Get the individual TU split flags */
170
38.4k
            p_tu_split_flag[0] = (tu_split_flag >> 16) & 0x1F;
171
38.4k
            p_tu_split_flag[1] = (tu_split_flag >> 11) & 0x1F;
172
38.4k
            p_tu_split_flag[2] = (tu_split_flag >> 6) & 0x1F;
173
38.4k
            p_tu_split_flag[3] = (tu_split_flag >> 1) & 0x1F;
174
175
            /* Get the early CBF flags */
176
38.4k
            p_tu_early_cbf[0] = (tu_early_cbf >> 16) & 0x1F;
177
38.4k
            p_tu_early_cbf[1] = (tu_early_cbf >> 11) & 0x1F;
178
38.4k
            p_tu_early_cbf[2] = (tu_early_cbf >> 6) & 0x1F;
179
38.4k
            p_tu_early_cbf[3] = (tu_early_cbf >> 1) & 0x1F;
180
38.4k
        }
181
119k
        else
182
119k
        {
183
            /* Get the individual TU split flags */
184
119k
            p_tu_split_flag[0] = ((tu_split_flag >> 4) & 0x1);
185
119k
            p_tu_split_flag[1] = ((tu_split_flag >> 3) & 0x1);
186
119k
            p_tu_split_flag[2] = ((tu_split_flag >> 2) & 0x1);
187
119k
            p_tu_split_flag[3] = ((tu_split_flag >> 1) & 0x1);
188
189
            /* Get the early CBF flags */
190
119k
            p_tu_early_cbf[0] = ((tu_early_cbf >> 4) & 0x1);
191
119k
            p_tu_early_cbf[1] = ((tu_early_cbf >> 3) & 0x1);
192
119k
            p_tu_early_cbf[2] = ((tu_early_cbf >> 2) & 0x1);
193
119k
            p_tu_early_cbf[3] = ((tu_early_cbf >> 1) & 0x1);
194
119k
        }
195
196
157k
        ps_tu_prms = (tu_prms_t *)ihevce_tu_tree_update(
197
157k
            ps_tu_prms,
198
157k
            pnum_tu_in_cu,
199
157k
            depth + 1,
200
157k
            p_tu_split_flag[0],
201
157k
            p_tu_early_cbf[0],
202
157k
            i4_x_off,
203
157k
            i4_y_off);
204
205
157k
        ps_tu_prms = (tu_prms_t *)ihevce_tu_tree_update(
206
157k
            ps_tu_prms,
207
157k
            pnum_tu_in_cu,
208
157k
            depth + 1,
209
157k
            p_tu_split_flag[1],
210
157k
            p_tu_early_cbf[1],
211
157k
            (i4_x_off + (tu_size >> (depth + 1))),
212
157k
            i4_y_off);
213
214
157k
        ps_tu_prms = (tu_prms_t *)ihevce_tu_tree_update(
215
157k
            ps_tu_prms,
216
157k
            pnum_tu_in_cu,
217
157k
            depth + 1,
218
157k
            p_tu_split_flag[2],
219
157k
            p_tu_early_cbf[2],
220
157k
            i4_x_off,
221
157k
            (i4_y_off + (tu_size >> (depth + 1))));
222
223
157k
        ps_tu_prms = (tu_prms_t *)ihevce_tu_tree_update(
224
157k
            ps_tu_prms,
225
157k
            pnum_tu_in_cu,
226
157k
            depth + 1,
227
157k
            p_tu_split_flag[3],
228
157k
            p_tu_early_cbf[3],
229
157k
            (i4_x_off + (tu_size >> (depth + 1))),
230
157k
            (i4_y_off + (tu_size >> (depth + 1))));
231
157k
    }
232
1.80M
    else
233
1.80M
    {
234
1.80M
        if(tu_split_flag & 0x1)
235
179k
        {
236
            /* This piece of code will be entered for the 8x8, if it is split
237
            Update the 4 child TU's accordingly. */
238
239
179k
            (*pnum_tu_in_cu) += 4;
240
241
            /* TL TU update */
242
179k
            ps_tu_prms->u1_tu_size = tu_size >> (depth + 1);
243
244
179k
            ps_tu_prms->u1_x_off = i4_x_off;
245
246
179k
            ps_tu_prms->u1_y_off = i4_y_off;
247
248
            /* Early CBF is not done for 4x4 transforms */
249
179k
            ps_tu_prms->i4_early_cbf = 1;
250
251
179k
            ps_tu_prms++;
252
253
            /* TR TU update */
254
179k
            ps_tu_prms->u1_tu_size = tu_size >> (depth + 1);
255
256
179k
            ps_tu_prms->u1_x_off = i4_x_off + (tu_size >> (depth + 1));
257
258
179k
            ps_tu_prms->u1_y_off = i4_y_off;
259
260
            /* Early CBF is not done for 4x4 transforms */
261
179k
            ps_tu_prms->i4_early_cbf = 1;
262
263
179k
            ps_tu_prms++;
264
265
            /* BL TU update */
266
179k
            ps_tu_prms->u1_tu_size = tu_size >> (depth + 1);
267
268
179k
            ps_tu_prms->u1_x_off = i4_x_off;
269
270
179k
            ps_tu_prms->u1_y_off = i4_y_off + (tu_size >> (depth + 1));
271
272
            /* Early CBF is not done for 4x4 transforms */
273
179k
            ps_tu_prms->i4_early_cbf = 1;
274
275
179k
            ps_tu_prms++;
276
277
            /* BR TU update */
278
179k
            ps_tu_prms->u1_tu_size = tu_size >> (depth + 1);
279
280
179k
            ps_tu_prms->u1_x_off = i4_x_off + (tu_size >> (depth + 1));
281
282
179k
            ps_tu_prms->u1_y_off = i4_y_off + (tu_size >> (depth + 1));
283
284
            /* Early CBF is not done for 4x4 transforms */
285
179k
            ps_tu_prms->i4_early_cbf = 1;
286
179k
        }
287
1.62M
        else
288
1.62M
        {
289
            /* Update the TU params */
290
1.62M
            ps_tu_prms->u1_tu_size = tu_size >> depth;
291
292
1.62M
            ps_tu_prms->u1_x_off = i4_x_off;
293
294
1.62M
            ps_tu_prms->u1_y_off = i4_y_off;
295
296
1.62M
            (*pnum_tu_in_cu)++;
297
298
            /* Early CBF update for current TU */
299
1.62M
            ps_tu_prms->i4_early_cbf = tu_early_cbf & 0x1;
300
1.62M
        }
301
1.80M
        if((*pnum_tu_in_cu) < MAX_TU_IN_CTB)
302
1.80M
        {
303
1.80M
            ps_tu_prms++;
304
305
1.80M
            ps_tu_prms->u1_tu_size = tu_size;
306
1.80M
        }
307
1.80M
    }
308
309
1.96M
    return ps_tu_prms;
310
1.96M
}
311
312
/*!
313
******************************************************************************
314
* \if Function name : ihevce_compute_quant_rel_param \endif
315
*
316
* \brief
317
*    This function updates quantization related parameters like qp_mod_6 etc in
318
*       context according to new qp
319
*
320
* \date
321
*    08/01/2013
322
*
323
* \author
324
*    Ittiam
325
*
326
* \return
327
*
328
* List of Functions
329
*
330
*
331
******************************************************************************
332
*/
333
void ihevce_compute_quant_rel_param(ihevce_enc_loop_ctxt_t *ps_ctxt, WORD8 i1_cu_qp)
334
8.77M
{
335
8.77M
    WORD32 i4_div_factor;
336
337
8.77M
    ps_ctxt->i4_chrm_cu_qp =
338
8.77M
        (ps_ctxt->u1_chroma_array_type == 2)
339
8.77M
            ? MIN(i1_cu_qp + ps_ctxt->i4_chroma_qp_offset, 51)
340
8.77M
            : gai1_ihevc_chroma_qp_scale[i1_cu_qp + ps_ctxt->i4_chroma_qp_offset + MAX_QP_BD_OFFSET];
341
8.77M
    ps_ctxt->i4_cu_qp_div6 = (i1_cu_qp + (6 * (ps_ctxt->u1_bit_depth - 8))) / 6;
342
8.77M
    i4_div_factor = (i1_cu_qp + 3) / 6;
343
8.77M
    i4_div_factor = CLIP3(i4_div_factor, 3, 6);
344
8.77M
    ps_ctxt->i4_cu_qp_mod6 = (i1_cu_qp + (6 * (ps_ctxt->u1_bit_depth - 8))) % 6;
345
8.77M
    ps_ctxt->i4_chrm_cu_qp_div6 = (ps_ctxt->i4_chrm_cu_qp + (6 * (ps_ctxt->u1_bit_depth - 8))) / 6;
346
8.77M
    ps_ctxt->i4_chrm_cu_qp_mod6 = (ps_ctxt->i4_chrm_cu_qp + (6 * (ps_ctxt->u1_bit_depth - 8))) % 6;
347
348
8.77M
#define INTER_RND_QP_BY_6
349
8.77M
#ifdef INTER_RND_QP_BY_6
350
    /* quant factor without RDOQ is 1/6th of shift for inter : like in H264 */
351
8.77M
    {
352
8.77M
        ps_ctxt->i4_quant_rnd_factor[PRED_MODE_INTER] =
353
8.77M
            (WORD32)(((1 << QUANT_ROUND_FACTOR_Q) / (float)6) + 0.5f);
354
8.77M
    }
355
#else
356
    /* quant factor without RDOQ is 1/6th of shift for inter : like in H264 */
357
    ps_ctxt->i4_quant_rnd_factor[PRED_MODE_INTER] = (1 << QUANT_ROUND_FACTOR_Q) / 3;
358
#endif
359
360
8.77M
    if(ISLICE == ps_ctxt->i1_slice_type)
361
3.26M
    {
362
        /* quant factor without RDOQ is 1/3rd of shift for intra : like in H264 */
363
3.26M
        ps_ctxt->i4_quant_rnd_factor[PRED_MODE_INTRA] =
364
3.26M
            (WORD32)(((1 << QUANT_ROUND_FACTOR_Q) / (float)3) + 0.5f);
365
3.26M
    }
366
5.50M
    else
367
5.50M
    {
368
5.50M
        if(0) /*TRAQO_EXT_ENABLE_ONE_THIRD_RND*/
369
0
        {
370
            /* quant factor without RDOQ is 1/3rd of shift for intra : like in H264 */
371
0
            ps_ctxt->i4_quant_rnd_factor[PRED_MODE_INTRA] =
372
0
                (WORD32)(((1 << QUANT_ROUND_FACTOR_Q) / (float)3) + 0.5f);
373
0
        }
374
5.50M
        else
375
5.50M
        {
376
            /* quant factor without RDOQ is 1/6th of shift for intra in inter pic */
377
5.50M
            ps_ctxt->i4_quant_rnd_factor[PRED_MODE_INTRA] =
378
5.50M
                ps_ctxt->i4_quant_rnd_factor[PRED_MODE_INTER];
379
            /* (1 << QUANT_ROUND_FACTOR_Q) / 6; */
380
5.50M
        }
381
5.50M
    }
382
8.77M
}
383
384
/*!
385
******************************************************************************
386
* \if Function name : ihevce_populate_cl_cu_lambda_prms \endif
387
*
388
* \brief
389
*    Function whihc calculates the Lambda params for current picture
390
*
391
* \param[in] ps_enc_ctxt : encoder ctxt pointer
392
* \param[in] ps_cur_pic_ctxt : current pic ctxt
393
* \param[in] i4_cur_frame_qp : current pic QP
394
* \param[in] first_field : is first field flag
395
* \param[in] i4_temporal_lyr_id : Current picture layer id
396
*
397
* \return
398
*    None
399
*
400
* \author
401
*  Ittiam
402
*
403
*****************************************************************************
404
*/
405
void ihevce_populate_cl_cu_lambda_prms(
406
    ihevce_enc_loop_ctxt_t *ps_ctxt,
407
    frm_lambda_ctxt_t *ps_frm_lamda,
408
    WORD32 i4_slice_type,
409
    WORD32 i4_temporal_lyr_id,
410
    WORD32 i4_lambda_type)
411
144k
{
412
144k
    WORD32 i4_curr_cu_qp, i4_curr_cu_qp_offset;
413
144k
    double lambda_modifier;
414
144k
    double lambda_uv_modifier;
415
144k
    double lambda;
416
144k
    double lambda_uv;
417
418
144k
    WORD32 i4_qp_bdoffset = 6 * (ps_ctxt->u1_bit_depth - 8);
419
420
    /*Populate lamda modifier */
421
144k
    ps_ctxt->i4_lamda_modifier = ps_frm_lamda->lambda_modifier;
422
144k
    ps_ctxt->i4_uv_lamda_modifier = ps_frm_lamda->lambda_uv_modifier;
423
144k
    ps_ctxt->i4_temporal_layer_id = i4_temporal_lyr_id;
424
425
144k
    for(i4_curr_cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_min_qp;
426
7.49M
        i4_curr_cu_qp <= ps_ctxt->ps_rc_quant_ctxt->i2_max_qp;
427
7.34M
        i4_curr_cu_qp++)
428
7.34M
    {
429
7.34M
        WORD32 chroma_qp = (ps_ctxt->i4_chroma_format == IV_YUV_422SP_UV)
430
7.34M
                               ? MIN(i4_curr_cu_qp, 51)
431
7.34M
                               : gai1_ihevc_chroma_qp_scale[i4_curr_cu_qp + MAX_QP_BD_OFFSET];
432
433
7.34M
        i4_curr_cu_qp_offset = i4_curr_cu_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset;
434
435
7.34M
        lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bdoffset - 12)) / 3.0));
436
7.34M
        lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bdoffset - 12)) / 3.0));
437
438
7.34M
        if((BSLICE == i4_slice_type) && (i4_temporal_lyr_id))
439
1.52M
        {
440
1.52M
            lambda_modifier = ps_frm_lamda->lambda_modifier *
441
1.52M
                              CLIP3((((double)(i4_curr_cu_qp - 12)) / 6.0), 2.00, 4.00);
442
1.52M
            lambda_uv_modifier = ps_frm_lamda->lambda_uv_modifier *
443
1.52M
                                 CLIP3((((double)(chroma_qp - 12)) / 6.0), 2.00, 4.00);
444
1.52M
        }
445
5.82M
        else
446
5.82M
        {
447
5.82M
            lambda_modifier = ps_frm_lamda->lambda_modifier;
448
5.82M
            lambda_uv_modifier = ps_frm_lamda->lambda_uv_modifier;
449
5.82M
        }
450
7.34M
        if(ps_ctxt->i4_use_const_lamda_modifier)
451
0
        {
452
0
            if(ISLICE == ps_ctxt->i1_slice_type)
453
0
            {
454
0
                lambda_modifier = ps_ctxt->f_i_pic_lamda_modifier;
455
0
                lambda_uv_modifier = ps_ctxt->f_i_pic_lamda_modifier;
456
0
            }
457
0
            else
458
0
            {
459
0
                lambda_modifier = CONST_LAMDA_MOD_VAL;
460
0
                lambda_uv_modifier = CONST_LAMDA_MOD_VAL;
461
0
            }
462
0
        }
463
7.34M
        switch(i4_lambda_type)
464
7.34M
        {
465
0
        case 0:
466
0
        {
467
0
            i4_qp_bdoffset = 0;
468
469
0
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bdoffset - 12)) / 3.0));
470
0
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bdoffset - 12)) / 3.0));
471
472
0
            lambda *= lambda_modifier;
473
0
            lambda_uv *= lambda_uv_modifier;
474
475
0
            ps_ctxt->au4_chroma_cost_weighing_factor_array[i4_curr_cu_qp_offset] =
476
0
                (UWORD32)((lambda / lambda_uv) * (1 << CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT));
477
478
0
            ps_ctxt->i8_cl_ssd_lambda_qf_array[i4_curr_cu_qp_offset] =
479
0
                (LWORD64)(lambda * (1 << LAMBDA_Q_SHIFT));
480
481
0
            ps_ctxt->i8_cl_ssd_lambda_chroma_qf_array[i4_curr_cu_qp_offset] =
482
0
                (LWORD64)(lambda_uv * (1 << LAMBDA_Q_SHIFT));
483
0
            if(ps_ctxt->i4_use_const_lamda_modifier)
484
0
            {
485
0
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset] =
486
0
                    (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
487
0
            }
488
0
            else
489
0
            {
490
0
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset] =
491
0
                    (WORD32)(sqrt(lambda * 1.9) * (1 << LAMBDA_Q_SHIFT));
492
0
            }
493
494
0
            ps_ctxt->i4_sad_lamda_array[i4_curr_cu_qp_offset] =
495
0
                (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
496
497
0
            ps_ctxt->i8_cl_ssd_type2_lambda_qf_array[i4_curr_cu_qp_offset] =
498
0
                ps_ctxt->i8_cl_ssd_lambda_qf_array[i4_curr_cu_qp_offset];
499
500
0
            ps_ctxt->i8_cl_ssd_type2_lambda_chroma_qf_array[i4_curr_cu_qp_offset] =
501
0
                ps_ctxt->i8_cl_ssd_lambda_chroma_qf_array[i4_curr_cu_qp_offset];
502
503
0
            ps_ctxt->i4_satd_type2_lamda_array[i4_curr_cu_qp_offset] =
504
0
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset];
505
506
0
            ps_ctxt->i4_sad_type2_lamda_array[i4_curr_cu_qp_offset] =
507
0
                ps_ctxt->i4_sad_lamda_array[i4_curr_cu_qp_offset];
508
509
0
            break;
510
0
        }
511
0
        case 1:
512
0
        {
513
0
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bdoffset - 12)) / 3.0));
514
0
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bdoffset - 12)) / 3.0));
515
516
0
            lambda *= lambda_modifier;
517
0
            lambda_uv *= lambda_uv_modifier;
518
519
0
            ps_ctxt->au4_chroma_cost_weighing_factor_array[i4_curr_cu_qp_offset] =
520
0
                (UWORD32)((lambda / lambda_uv) * (1 << CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT));
521
522
0
            ps_ctxt->i8_cl_ssd_lambda_qf_array[i4_curr_cu_qp_offset] =
523
0
                (LWORD64)(lambda * (1 << LAMBDA_Q_SHIFT));
524
525
0
            ps_ctxt->i8_cl_ssd_lambda_chroma_qf_array[i4_curr_cu_qp_offset] =
526
0
                (LWORD64)(lambda_uv * (1 << LAMBDA_Q_SHIFT));
527
0
            if(ps_ctxt->i4_use_const_lamda_modifier)
528
0
            {
529
0
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset] =
530
0
                    (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
531
0
            }
532
0
            else
533
0
            {
534
0
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset] =
535
0
                    (WORD32)(sqrt(lambda * 1.9) * (1 << LAMBDA_Q_SHIFT));
536
0
            }
537
0
            ps_ctxt->i4_sad_lamda_array[i4_curr_cu_qp_offset] =
538
0
                (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
539
540
0
            ps_ctxt->i8_cl_ssd_type2_lambda_qf_array[i4_curr_cu_qp_offset] =
541
0
                ps_ctxt->i8_cl_ssd_lambda_qf_array[i4_curr_cu_qp_offset];
542
543
0
            ps_ctxt->i8_cl_ssd_type2_lambda_chroma_qf_array[i4_curr_cu_qp_offset] =
544
0
                ps_ctxt->i8_cl_ssd_lambda_chroma_qf_array[i4_curr_cu_qp_offset];
545
546
0
            ps_ctxt->i4_satd_type2_lamda_array[i4_curr_cu_qp_offset] =
547
0
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset];
548
549
0
            ps_ctxt->i4_sad_type2_lamda_array[i4_curr_cu_qp_offset] =
550
0
                ps_ctxt->i4_sad_lamda_array[i4_curr_cu_qp_offset];
551
552
0
            break;
553
0
        }
554
7.34M
        case 2:
555
7.34M
        {
556
7.34M
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bdoffset - 12)) / 3.0));
557
7.34M
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bdoffset - 12)) / 3.0));
558
559
7.34M
            lambda *= lambda_modifier;
560
7.34M
            lambda_uv *= lambda_uv_modifier;
561
562
7.34M
            ps_ctxt->au4_chroma_cost_weighing_factor_array[i4_curr_cu_qp_offset] =
563
7.34M
                (UWORD32)((lambda / lambda_uv) * (1 << CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT));
564
565
7.34M
            ps_ctxt->i8_cl_ssd_lambda_qf_array[i4_curr_cu_qp_offset] =
566
7.34M
                (LWORD64)(lambda * (1 << LAMBDA_Q_SHIFT));
567
568
7.34M
            ps_ctxt->i8_cl_ssd_lambda_chroma_qf_array[i4_curr_cu_qp_offset] =
569
7.34M
                (LWORD64)(lambda_uv * (1 << LAMBDA_Q_SHIFT));
570
571
7.34M
            if(ps_ctxt->i4_use_const_lamda_modifier)
572
0
            {
573
0
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset] =
574
0
                    (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
575
0
            }
576
7.34M
            else
577
7.34M
            {
578
7.34M
                ps_ctxt->i4_satd_lamda_array[i4_curr_cu_qp_offset] =
579
7.34M
                    (WORD32)(sqrt(lambda * 1.9) * (1 << LAMBDA_Q_SHIFT));
580
7.34M
            }
581
7.34M
            ps_ctxt->i4_sad_lamda_array[i4_curr_cu_qp_offset] =
582
7.34M
                (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
583
584
            /* lambda corresponding to 8- bit, for metrics based on 8- bit ( Example 8bit SAD in encloop)*/
585
7.34M
            lambda = pow(2.0, (((double)(i4_curr_cu_qp - 12)) / 3.0));
586
7.34M
            lambda_uv = pow(2.0, (((double)(chroma_qp - 12)) / 3.0));
587
588
7.34M
            lambda *= lambda_modifier;
589
7.34M
            lambda_uv *= lambda_uv_modifier;
590
591
7.34M
            ps_ctxt->au4_chroma_cost_weighing_factor_array[i4_curr_cu_qp_offset] =
592
7.34M
                (UWORD32)((lambda / lambda_uv) * (1 << CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT));
593
594
7.34M
            ps_ctxt->i8_cl_ssd_type2_lambda_qf_array[i4_curr_cu_qp_offset] =
595
7.34M
                (LWORD64)(lambda * (1 << LAMBDA_Q_SHIFT));
596
597
7.34M
            ps_ctxt->i8_cl_ssd_type2_lambda_chroma_qf_array[i4_curr_cu_qp_offset] =
598
7.34M
                (LWORD64)(lambda_uv * (1 << LAMBDA_Q_SHIFT));
599
7.34M
            if(ps_ctxt->i4_use_const_lamda_modifier)
600
0
            {
601
0
                ps_ctxt->i4_satd_type2_lamda_array[i4_curr_cu_qp_offset] =
602
0
                    (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
603
0
            }
604
7.34M
            else
605
7.34M
            {
606
7.34M
                ps_ctxt->i4_satd_type2_lamda_array[i4_curr_cu_qp_offset] =
607
7.34M
                    (WORD32)(sqrt(lambda * 1.9) * (1 << LAMBDA_Q_SHIFT));
608
7.34M
            }
609
610
7.34M
            ps_ctxt->i4_sad_type2_lamda_array[i4_curr_cu_qp_offset] =
611
7.34M
                (WORD32)(sqrt(lambda) * (1 << LAMBDA_Q_SHIFT));
612
613
7.34M
            break;
614
0
        }
615
0
        default:
616
0
        {
617
            /* Intended to be a barren wasteland! */
618
0
            ASSERT(0);
619
0
        }
620
7.34M
        }
621
7.34M
    }
622
144k
}
623
624
/*!
625
******************************************************************************
626
* \if Function name : ihevce_get_cl_cu_lambda_prms \endif
627
*
628
* \brief
629
*    Function whihc calculates the Lambda params for current picture
630
*
631
* \param[in] ps_enc_ctxt : encoder ctxt pointer
632
* \param[in] ps_cur_pic_ctxt : current pic ctxt
633
* \param[in] i4_cur_frame_qp : current pic QP
634
* \param[in] first_field : is first field flag
635
* \param[in] i4_temporal_lyr_id : Current picture layer id
636
*
637
* \return
638
*    None
639
*
640
* \author
641
*  Ittiam
642
*
643
*****************************************************************************
644
*/
645
void ihevce_get_cl_cu_lambda_prms(ihevce_enc_loop_ctxt_t *ps_ctxt, WORD32 i4_cur_cu_qp)
646
8.77M
{
647
8.77M
    WORD32 chroma_qp = (ps_ctxt->u1_chroma_array_type == 2)
648
8.77M
                           ? MIN(i4_cur_cu_qp + ps_ctxt->i4_chroma_qp_offset, 51)
649
8.77M
                           : gai1_ihevc_chroma_qp_scale
650
8.77M
                                 [i4_cur_cu_qp + ps_ctxt->i4_chroma_qp_offset + MAX_QP_BD_OFFSET];
651
652
    /* closed loop ssd lambda is same as final lambda */
653
8.77M
    ps_ctxt->i8_cl_ssd_lambda_qf =
654
8.77M
        ps_ctxt->i8_cl_ssd_lambda_qf_array[i4_cur_cu_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
655
8.77M
    ps_ctxt->i8_cl_ssd_lambda_chroma_qf =
656
8.77M
        ps_ctxt
657
8.77M
            ->i8_cl_ssd_lambda_chroma_qf_array[chroma_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
658
8.77M
    ps_ctxt->u4_chroma_cost_weighing_factor =
659
8.77M
        ps_ctxt->au4_chroma_cost_weighing_factor_array
660
8.77M
            [chroma_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
661
    /* --- Initialized the lambda for SATD computations --- */
662
    /* --- 0.95 is the multiplication factor as per HM --- */
663
    /* --- 1.9 is the multiplication factor for Hadamard Transform --- */
664
8.77M
    ps_ctxt->i4_satd_lamda =
665
8.77M
        ps_ctxt->i4_satd_lamda_array[i4_cur_cu_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
666
8.77M
    ps_ctxt->i4_sad_lamda =
667
8.77M
        ps_ctxt->i4_sad_type2_lamda_array[i4_cur_cu_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
668
8.77M
}
669
670
/*!
671
******************************************************************************
672
* \if Function name : ihevce_update_pred_qp \endif
673
*
674
* \brief
675
*    Computes pred qp for the given CU
676
*
677
* \param[in]
678
*
679
* \return
680
*
681
*
682
* \author
683
*  Ittiam
684
*
685
*****************************************************************************
686
*/
687
void ihevce_update_pred_qp(ihevce_enc_loop_ctxt_t *ps_ctxt, WORD32 cu_pos_x, WORD32 cu_pos_y)
688
2.99M
{
689
2.99M
    WORD32 i4_pred_qp = 0x7FFFFFFF;
690
2.99M
    WORD32 i4_top, i4_left;
691
2.99M
    if(cu_pos_x == 0 && cu_pos_y == 0) /*CTB start*/
692
349k
    {
693
349k
        i4_pred_qp = ps_ctxt->i4_prev_QP;
694
349k
    }
695
2.64M
    else
696
2.64M
    {
697
2.64M
        if(cu_pos_y == 0) /*CTB boundary*/
698
595k
        {
699
595k
            i4_top = ps_ctxt->i4_prev_QP;
700
595k
        }
701
2.04M
        else /*within CTB*/
702
2.04M
        {
703
2.04M
            i4_top = ps_ctxt->ai4_qp_qg[(cu_pos_y - 1) * 8 + (cu_pos_x)];
704
2.04M
        }
705
2.64M
        if(cu_pos_x == 0) /*CTB boundary*/
706
607k
        {
707
607k
            i4_left = ps_ctxt->i4_prev_QP;
708
607k
        }
709
2.03M
        else /*within CTB*/
710
2.03M
        {
711
2.03M
            i4_left = ps_ctxt->ai4_qp_qg[(cu_pos_y)*8 + (cu_pos_x - 1)];
712
2.03M
        }
713
2.64M
        i4_pred_qp = (i4_left + i4_top + 1) >> 1;
714
2.64M
    }
715
2.99M
    ps_ctxt->i4_pred_qp = i4_pred_qp;
716
2.99M
    return;
717
2.99M
}
718
/*!
719
******************************************************************************
720
* \if Function name : ihevce_compute_cu_level_QP \endif
721
*
722
* \brief
723
*    Computes cu level QP with Traqo,Spatial Mod and In-frame RC
724
*
725
* \param[in]
726
*
727
* \return
728
*
729
*
730
* \author
731
*  Ittiam
732
*
733
*****************************************************************************
734
*/
735
void ihevce_compute_cu_level_QP(
736
    ihevce_enc_loop_ctxt_t *ps_ctxt,
737
    WORD32 i4_activity_for_qp,
738
    WORD32 i4_activity_for_lamda,
739
    WORD32 i4_reduce_qp)
740
7.65M
{
741
    /*modify quant related param in ctxt based on current cu qp*/
742
7.65M
    WORD32 i4_input_QP = ps_ctxt->i4_frame_mod_qp;
743
7.65M
    WORD32 cu_qp = i4_input_QP + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset;
744
745
7.65M
    WORD32 i4_max_qp_allowed;
746
7.65M
    WORD32 i4_min_qp_allowed;
747
7.65M
    WORD32 i4_pred_qp;
748
749
7.65M
    i4_pred_qp = ps_ctxt->i4_pred_qp;
750
751
7.65M
    if(ps_ctxt->i4_sub_pic_level_rc)
752
0
    {
753
0
        i4_max_qp_allowed = (i4_pred_qp + (25 + (ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset / 2)));
754
0
        i4_min_qp_allowed = (i4_pred_qp - (26 + (ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset / 2)));
755
0
    }
756
7.65M
    else
757
7.65M
    {
758
7.65M
        i4_max_qp_allowed = (i4_input_QP + (7 + (ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset / 4)));
759
7.65M
        i4_min_qp_allowed = (i4_input_QP - (18 + (ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset / 4)));
760
7.65M
    }
761
7.65M
    if((ps_ctxt->i1_slice_type == BSLICE) && (ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P6))
762
0
        return;
763
764
#if LAMDA_BASED_ON_QUANT
765
    i4_activity_for_lamda = i4_activity_for_qp;
766
#endif
767
768
7.65M
    if(i4_activity_for_qp != -1)
769
7.65M
    {
770
7.65M
        cu_qp = (ps_ctxt->ps_rc_quant_ctxt
771
7.65M
                     ->pi4_qp_to_qscale[i4_input_QP + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset]);
772
7.65M
        if(ps_ctxt->i4_qp_mod)
773
7.65M
        {
774
            /*Recompute the Qp as per enc thread's frame level Qp*/
775
7.65M
            ASSERT(i4_activity_for_qp > 0);
776
7.65M
            cu_qp = ((cu_qp * i4_activity_for_qp) + (1 << (QP_LEVEL_MOD_ACT_FACTOR - 1))) >>
777
7.65M
                    QP_LEVEL_MOD_ACT_FACTOR;
778
7.65M
        }
779
780
        // To avoid access of uninitialised Qscale to qp conversion table
781
7.65M
        if(cu_qp > ps_ctxt->ps_rc_quant_ctxt->i2_max_qscale)
782
415k
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_max_qscale;
783
7.24M
        else if(cu_qp < ps_ctxt->ps_rc_quant_ctxt->i2_min_qscale)
784
0
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_min_qscale;
785
786
7.65M
        cu_qp = ps_ctxt->ps_rc_quant_ctxt->pi4_qscale_to_qp[cu_qp];
787
788
7.65M
        if((1 == i4_reduce_qp) && (cu_qp > 1))
789
0
            cu_qp--;
790
791
        /*CLIP the delta to obey standard allowed QP variation of (-26 + offset/2) to (25 + offset/2)*/
792
7.65M
        if(cu_qp > i4_max_qp_allowed)
793
0
            cu_qp = i4_max_qp_allowed;
794
7.65M
        else if(cu_qp < i4_min_qp_allowed)
795
0
            cu_qp = i4_min_qp_allowed;
796
797
        /* CLIP to maintain Qp between user configured and min and max Qp values*/
798
7.65M
        if(cu_qp > ps_ctxt->ps_rc_quant_ctxt->i2_max_qp)
799
0
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_max_qp;
800
7.65M
        else if(cu_qp < ps_ctxt->ps_rc_quant_ctxt->i2_min_qp)
801
493k
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_min_qp;
802
803
        /*cu qp must be populated in cu_analyse_t struct*/
804
7.65M
        ps_ctxt->i4_cu_qp = cu_qp;
805
        /*recompute quant related param at every cu level*/
806
7.65M
        ihevce_compute_quant_rel_param(ps_ctxt, cu_qp);
807
7.65M
    }
808
809
    /*Decoupling qp and lamda calculation */
810
7.65M
    if(i4_activity_for_lamda != -1)
811
7.65M
    {
812
7.65M
        cu_qp = (ps_ctxt->ps_rc_quant_ctxt
813
7.65M
                     ->pi4_qp_to_qscale[i4_input_QP + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset]);
814
815
7.65M
        if(ps_ctxt->i4_qp_mod)
816
7.65M
        {
817
7.65M
#if MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON
818
            /*Recompute the Qp as per enc thread's frame level Qp*/
819
7.65M
            ASSERT(i4_activity_for_lamda > 0);
820
7.65M
            cu_qp = ((cu_qp * i4_activity_for_lamda) + (1 << (QP_LEVEL_MOD_ACT_FACTOR - 1))) >>
821
7.65M
                    QP_LEVEL_MOD_ACT_FACTOR;
822
7.65M
#endif
823
7.65M
        }
824
7.65M
        if(cu_qp > ps_ctxt->ps_rc_quant_ctxt->i2_max_qscale)
825
210k
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_max_qscale;
826
7.44M
        else if(cu_qp < ps_ctxt->ps_rc_quant_ctxt->i2_min_qscale)
827
0
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_min_qscale;
828
829
7.65M
        cu_qp = ps_ctxt->ps_rc_quant_ctxt->pi4_qscale_to_qp[cu_qp];
830
831
        /*CLIP the delta to obey standard allowed QP variation of (-26 + offset/2) to (25 + offset/2)*/
832
7.65M
        if(cu_qp > i4_max_qp_allowed)
833
0
            cu_qp = i4_max_qp_allowed;
834
7.65M
        else if(cu_qp < i4_min_qp_allowed)
835
0
            cu_qp = i4_min_qp_allowed;
836
837
        /* CLIP to maintain Qp between user configured and min and max Qp values*/
838
7.65M
        if(cu_qp > ps_ctxt->ps_rc_quant_ctxt->i2_max_qp)
839
0
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_max_qp;
840
7.65M
        else if(cu_qp < ps_ctxt->ps_rc_quant_ctxt->i2_min_qp)
841
1.08M
            cu_qp = ps_ctxt->ps_rc_quant_ctxt->i2_min_qp;
842
        /* get frame level lambda params */
843
7.65M
        ihevce_get_cl_cu_lambda_prms(
844
7.65M
            ps_ctxt, MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON ? cu_qp : ps_ctxt->i4_frame_qp);
845
7.65M
    }
846
7.65M
}
847
848
void ihevce_update_cu_level_qp_lamda(
849
    ihevce_enc_loop_ctxt_t *ps_ctxt, cu_analyse_t *ps_cu_analyse, WORD32 trans_size, WORD32 is_intra)
850
7.65M
{
851
7.65M
    WORD32 i4_act_counter = 0, i4_act_counter_lamda = 0;
852
853
7.65M
    if(ps_cu_analyse->u1_cu_size == 64)
854
158k
    {
855
158k
        ASSERT((trans_size == 32) || (trans_size == 16) || (trans_size == 8) || (trans_size == 4));
856
158k
        i4_act_counter = (trans_size == 16) + 2 * ((trans_size == 8) || (trans_size == 4));
857
158k
        i4_act_counter_lamda = 3;
858
158k
    }
859
7.49M
    else if(ps_cu_analyse->u1_cu_size == 32)
860
1.15M
    {
861
1.15M
        ASSERT((trans_size == 32) || (trans_size == 16) || (trans_size == 8) || (trans_size == 4));
862
1.15M
        i4_act_counter = (trans_size == 16) + 2 * ((trans_size == 8) || (trans_size == 4));
863
1.15M
        i4_act_counter_lamda = 0;
864
1.15M
    }
865
6.34M
    else if(ps_cu_analyse->u1_cu_size == 16)
866
3.21M
    {
867
3.21M
        ASSERT((trans_size == 16) || (trans_size == 8) || (trans_size == 4));
868
3.21M
        i4_act_counter = (trans_size == 8) || (trans_size == 4);
869
3.21M
        i4_act_counter_lamda = 0;
870
3.21M
    }
871
3.12M
    else if(ps_cu_analyse->u1_cu_size == 8)
872
3.12M
    {
873
3.12M
        ASSERT((trans_size == 8) || (trans_size == 4));
874
3.12M
        i4_act_counter = 1;
875
3.12M
        i4_act_counter_lamda = 0;
876
3.12M
    }
877
0
    else
878
0
    {
879
0
        ASSERT(0);
880
0
    }
881
882
7.65M
    if(ps_ctxt->i4_use_ctb_level_lamda)
883
0
    {
884
0
        ihevce_compute_cu_level_QP(
885
0
            ps_ctxt, ps_cu_analyse->i4_act_factor[i4_act_counter][is_intra], -1, 0);
886
0
    }
887
7.65M
    else
888
7.65M
    {
889
7.65M
        ihevce_compute_cu_level_QP(
890
7.65M
            ps_ctxt,
891
7.65M
            ps_cu_analyse->i4_act_factor[i4_act_counter][is_intra],
892
7.65M
            ps_cu_analyse->i4_act_factor[i4_act_counter_lamda][is_intra],
893
7.65M
            0);
894
7.65M
    }
895
896
7.65M
    ps_cu_analyse->i1_cu_qp = ps_ctxt->i4_cu_qp;
897
7.65M
}
898
899
/**
900
*******************************************************************************
901
* \if Function name : ihevce_scan_coeffs \endif
902
*
903
* @brief * Computes the coeff buffer for a coded TU for entropy coding
904
*
905
* @par   Description
906
* Computes the coeff buffer for a coded TU for entropy coding
907
*
908
* \param[in] pi2_quan_coeffs Quantized coefficient context
909
*
910
* \param[in] scan_idx Scan index specifying the scan order
911
*
912
* \param[in] trans_size Transform unit size
913
*
914
* \param[inout] pu1_out_data output coeff buffer for a coded TU for entropy coding
915
*
916
* \param[in] pu1_csbf_buf csb flag buffer
917
*
918
* @returns num_bytes
919
* Number of bytes written to pu1_out_data
920
*
921
* @remarks
922
*
923
* \author
924
*  Ittiam
925
*
926
*******************************************************************************
927
*/
928
929
WORD32 ihevce_scan_coeffs(
930
    WORD16 *pi2_quant_coeffs,
931
    WORD32 *pi4_subBlock2csbfId_map,
932
    WORD32 scan_idx,
933
    WORD32 trans_size,
934
    UWORD8 *pu1_out_data,
935
    UWORD8 *pu1_csbf_buf,
936
    WORD32 i4_csbf_stride)
937
24.2M
{
938
24.2M
    WORD32 i, trans_unit_idx, num_gt1_flag;
939
24.2M
    UWORD16 u2_csbf0flags;
940
24.2M
    WORD32 num_bytes = 0;
941
24.2M
    UWORD8 *pu1_trans_table;
942
24.2M
    UWORD8 *pu1_csb_table;
943
24.2M
    WORD32 shift_value, mask_value;
944
24.2M
    UWORD16 u2_sig_coeff_abs_gt0_flags = 0, u2_sig_coeff_abs_gt1_flags = 0;
945
24.2M
    UWORD16 u2_sign_flags;
946
24.2M
    UWORD16 u2_abs_coeff_remaining[16];
947
24.2M
    WORD32 blk_row, blk_col;
948
949
24.2M
    UWORD8 *pu1_out_data_header;
950
24.2M
    UWORD16 *pu2_out_data_coeff;
951
952
24.2M
    WORD32 x_pos, y_pos;
953
24.2M
    WORD32 quant_coeff;
954
955
24.2M
    WORD32 num_gt0_flag;
956
24.2M
    (void)i4_csbf_stride;
957
24.2M
    pu1_out_data_header = pu1_out_data;
958
    /* Need only last 3 bits, rest are reserved for debugging and making */
959
    /* WORD alignment */
960
24.2M
    u2_csbf0flags = 0xBAD0;
961
962
    /* Select proper order for your transform unit and csb based on scan_idx*/
963
    /* and the trans_size */
964
965
    /* scan order inside a csb */
966
24.2M
    pu1_csb_table = (UWORD8 *)&(g_u1_scan_table_4x4[scan_idx][0]);
967
    /* GETRANGE will give the log_2 of trans_size to shift_value */
968
24.2M
    GETRANGE(shift_value, trans_size);
969
24.2M
    shift_value = shift_value - 3; /* for finding. row no. from scan index */
970
24.2M
    mask_value = (trans_size / 4) - 1; /*for finding the col. no. from scan index*/
971
24.2M
    switch(trans_size)
972
24.2M
    {
973
517k
    case 32:
974
517k
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_8x8[scan_idx][0]);
975
517k
        break;
976
1.77M
    case 16:
977
1.77M
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_4x4[scan_idx][0]);
978
1.77M
        break;
979
4.36M
    case 8:
980
4.36M
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_2x2[scan_idx][0]);
981
4.36M
        break;
982
17.6M
    case 4:
983
17.6M
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_1x1[0]);
984
17.6M
        break;
985
0
    default:
986
0
        DBG_PRINTF("Invalid Trans Size\n");
987
0
        return -1;
988
0
        break;
989
24.2M
    }
990
991
    /*go through each csb in the scan order for first non-zero coded sub-block*/
992
52.7M
    for(trans_unit_idx = (trans_size * trans_size / 16) - 1; trans_unit_idx >= 0; trans_unit_idx--)
993
52.7M
    {
994
        /* check for the first csb flag in our scan order */
995
52.7M
        if(pu1_csbf_buf[pi4_subBlock2csbfId_map[pu1_trans_table[trans_unit_idx]]])
996
24.2M
        {
997
24.2M
            UWORD8 u1_last_x, u1_last_y;
998
            /* row of csb */
999
24.2M
            blk_row = pu1_trans_table[trans_unit_idx] >> shift_value;
1000
            /* col of csb */
1001
24.2M
            blk_col = pu1_trans_table[trans_unit_idx] & mask_value;
1002
1003
            /*check for the 1st non-0 values inside the csb in our scan order*/
1004
98.1M
            for(i = 15; i >= 0; i--)
1005
98.1M
            {
1006
98.1M
                x_pos = (pu1_csb_table[i] & 0x3) + blk_col * 4;
1007
98.1M
                y_pos = (pu1_csb_table[i] >> 2) + blk_row * 4;
1008
1009
98.1M
                quant_coeff = pi2_quant_coeffs[x_pos + (y_pos * trans_size)];
1010
1011
98.1M
                if(quant_coeff != 0)
1012
24.2M
                    break;
1013
98.1M
            }
1014
1015
24.2M
            ASSERT(i >= 0);
1016
1017
24.2M
            u1_last_x = x_pos;
1018
24.2M
            u1_last_y = y_pos;
1019
1020
            /* storing last_x and last_y */
1021
24.2M
            *pu1_out_data_header = u1_last_x;
1022
24.2M
            pu1_out_data_header++;
1023
24.2M
            num_bytes++;
1024
24.2M
            *pu1_out_data_header = u1_last_y;
1025
24.2M
            pu1_out_data_header++;
1026
24.2M
            num_bytes++;
1027
1028
            /* storing the scan order */
1029
24.2M
            *pu1_out_data_header = scan_idx;
1030
24.2M
            pu1_out_data_header++;
1031
24.2M
            num_bytes++;
1032
            /* storing last_sub_block pos. in scan order count */
1033
24.2M
            *pu1_out_data_header = trans_unit_idx;
1034
24.2M
            pu1_out_data_header++;
1035
24.2M
            num_bytes++;
1036
1037
            /*stored the first 4 bytes, now all are word16. So word16 pointer*/
1038
24.2M
            pu2_out_data_coeff = (UWORD16 *)pu1_out_data_header;
1039
1040
            /* u2_csbf0flags word */
1041
24.2M
            u2_csbf0flags = 0xBAD0 | 1; /*since right&bottom csbf is 0*/
1042
            /* storing u2_csbf0flags word */
1043
24.2M
            *pu2_out_data_coeff = u2_csbf0flags;
1044
24.2M
            pu2_out_data_coeff++;
1045
24.2M
            num_bytes += 2;
1046
1047
24.2M
            num_gt0_flag = 1;
1048
24.2M
            num_gt1_flag = 0;
1049
24.2M
            u2_sign_flags = 0;
1050
1051
            /* set the i th bit of u2_sig_coeff_abs_gt0_flags */
1052
24.2M
            u2_sig_coeff_abs_gt0_flags = u2_sig_coeff_abs_gt0_flags | (1 << i);
1053
24.2M
            if(abs(quant_coeff) > 1)
1054
13.3M
            {
1055
                /* set the i th bit of u2_sig_coeff_abs_gt1_flags */
1056
13.3M
                u2_sig_coeff_abs_gt1_flags = u2_sig_coeff_abs_gt1_flags | (1 << i);
1057
                /* update u2_abs_coeff_remaining */
1058
13.3M
                u2_abs_coeff_remaining[num_gt1_flag] = (UWORD16)abs(quant_coeff) - 1;
1059
1060
13.3M
                num_gt1_flag++;
1061
13.3M
            }
1062
1063
24.2M
            if(quant_coeff < 0)
1064
12.6M
            {
1065
                /* set the i th bit of u2_sign_flags */
1066
12.6M
                u2_sign_flags = u2_sign_flags | (1 << i);
1067
12.6M
            }
1068
1069
            /* Test remaining elements in our scan order */
1070
            /* Can optimize further by CLZ macro */
1071
314M
            for(i = i - 1; i >= 0; i--)
1072
290M
            {
1073
290M
                x_pos = (pu1_csb_table[i] & 0x3) + blk_col * 4;
1074
290M
                y_pos = (pu1_csb_table[i] >> 2) + blk_row * 4;
1075
1076
290M
                quant_coeff = pi2_quant_coeffs[x_pos + (y_pos * trans_size)];
1077
1078
290M
                if(quant_coeff != 0)
1079
240M
                {
1080
                    /* set the i th bit of u2_sig_coeff_abs_gt0_flags */
1081
240M
                    u2_sig_coeff_abs_gt0_flags |= (1 << i);
1082
1083
240M
                    if((abs(quant_coeff) > 1) || (num_gt0_flag >= MAX_GT_ONE))
1084
203M
                    {
1085
                        /* set the i th bit of u2_sig_coeff_abs_gt1_flags */
1086
203M
                        u2_sig_coeff_abs_gt1_flags |= (1 << i);
1087
1088
                        /* update u2_abs_coeff_remaining */
1089
203M
                        u2_abs_coeff_remaining[num_gt1_flag] = (UWORD16)abs(quant_coeff) - 1;
1090
1091
203M
                        num_gt1_flag++; /*n0. of Ones in sig_coeff_abs_gt1_flag*/
1092
203M
                    }
1093
1094
240M
                    if(quant_coeff < 0)
1095
121M
                    {
1096
                        /* set the i th bit of u2_sign_flags */
1097
121M
                        u2_sign_flags |= (1 << i);
1098
121M
                    }
1099
1100
240M
                    num_gt0_flag++;
1101
240M
                }
1102
290M
            }
1103
1104
            /* storing u2_sig_coeff_abs_gt0_flags 2 bytes */
1105
24.2M
            *pu2_out_data_coeff = u2_sig_coeff_abs_gt0_flags;
1106
24.2M
            pu2_out_data_coeff++;
1107
24.2M
            num_bytes += 2;
1108
            /* storing u2_sig_coeff_abs_gt1_flags 2 bytes */
1109
24.2M
            *pu2_out_data_coeff = u2_sig_coeff_abs_gt1_flags;
1110
24.2M
            pu2_out_data_coeff++;
1111
24.2M
            num_bytes += 2;
1112
            /* storing u2_sign_flags 2 bytes */
1113
24.2M
            *pu2_out_data_coeff = u2_sign_flags;
1114
24.2M
            pu2_out_data_coeff++;
1115
24.2M
            num_bytes += 2;
1116
1117
            /* Store the u2_abs_coeff_remaining[] */
1118
241M
            for(i = 0; i < num_gt1_flag; i++)
1119
216M
            {
1120
                /* storing u2_abs_coeff_remaining[i] 2 bytes */
1121
216M
                *pu2_out_data_coeff = u2_abs_coeff_remaining[i];
1122
216M
                pu2_out_data_coeff++;
1123
216M
                num_bytes += 2;
1124
216M
            }
1125
1126
24.2M
            break; /*We just need this loop for finding 1st non-zero csb only*/
1127
24.2M
        }
1128
52.7M
    }
1129
1130
    /* go through remaining csb in the scan order */
1131
68.1M
    for(trans_unit_idx = trans_unit_idx - 1; trans_unit_idx >= 0; trans_unit_idx--)
1132
43.8M
    {
1133
43.8M
        blk_row = pu1_trans_table[trans_unit_idx] >> shift_value; /*row of csb*/
1134
43.8M
        blk_col = pu1_trans_table[trans_unit_idx] & mask_value; /*col of csb*/
1135
1136
        /* u2_csbf0flags word */
1137
43.8M
        u2_csbf0flags = 0xBAD0 | /* assuming csbf_buf has only 0 or 1 values */
1138
43.8M
                        (pu1_csbf_buf[pi4_subBlock2csbfId_map[pu1_trans_table[trans_unit_idx]]]);
1139
1140
        /********************************************************************/
1141
        /* Minor hack: As per HEVC spec csbf in not signalled in stream for */
1142
        /* block0, instead sig coeff map is directly signalled. This is     */
1143
        /* taken care by forcing csbf for block0 to be 1 even if it is 0    */
1144
        /********************************************************************/
1145
43.8M
        if(0 == trans_unit_idx)
1146
5.56M
        {
1147
5.56M
            u2_csbf0flags |= 1;
1148
5.56M
        }
1149
1150
43.8M
        if((blk_col + 1 < trans_size / 4)) /* checking right boundary */
1151
36.9M
        {
1152
36.9M
            if(pu1_csbf_buf[pi4_subBlock2csbfId_map[blk_row * trans_size / 4 + blk_col + 1]])
1153
29.5M
            {
1154
                /* set the 2nd bit of u2_csbf0flags for right csbf */
1155
29.5M
                u2_csbf0flags = u2_csbf0flags | (1 << 1);
1156
29.5M
            }
1157
36.9M
        }
1158
43.8M
        if((blk_row + 1 < trans_size / 4)) /* checking bottom oundary */
1159
36.1M
        {
1160
36.1M
            if(pu1_csbf_buf[pi4_subBlock2csbfId_map[(blk_row + 1) * trans_size / 4 + blk_col]])
1161
30.0M
            {
1162
                /* set the 3rd bit of u2_csbf0flags  for bottom csbf */
1163
30.0M
                u2_csbf0flags = u2_csbf0flags | (1 << 2);
1164
30.0M
            }
1165
36.1M
        }
1166
1167
        /* storing u2_csbf0flags word */
1168
43.8M
        *pu2_out_data_coeff = u2_csbf0flags;
1169
43.8M
        pu2_out_data_coeff++;
1170
43.8M
        num_bytes += 2;
1171
1172
        /* check for the csb flag in our scan order */
1173
43.8M
        if(u2_csbf0flags & 0x1)
1174
38.5M
        {
1175
38.5M
            u2_sig_coeff_abs_gt0_flags = 0;
1176
38.5M
            u2_sig_coeff_abs_gt1_flags = 0;
1177
38.5M
            u2_sign_flags = 0;
1178
1179
38.5M
            num_gt0_flag = 0;
1180
38.5M
            num_gt1_flag = 0;
1181
            /* check for the non-0 values inside the csb in our scan order */
1182
            /* Can optimize further by CLZ macro */
1183
655M
            for(i = 15; i >= 0; i--)
1184
617M
            {
1185
617M
                x_pos = (pu1_csb_table[i] & 0x3) + blk_col * 4;
1186
617M
                y_pos = (pu1_csb_table[i] >> 2) + blk_row * 4;
1187
1188
617M
                quant_coeff = pi2_quant_coeffs[x_pos + (y_pos * trans_size)];
1189
1190
617M
                if(quant_coeff != 0)
1191
490M
                {
1192
                    /* set the i th bit of u2_sig_coeff_abs_gt0_flags */
1193
490M
                    u2_sig_coeff_abs_gt0_flags |= (1 << i);
1194
1195
490M
                    if((abs(quant_coeff) > 1) || (num_gt0_flag >= MAX_GT_ONE))
1196
410M
                    {
1197
                        /* set the i th bit of u2_sig_coeff_abs_gt1_flags */
1198
410M
                        u2_sig_coeff_abs_gt1_flags |= (1 << i);
1199
1200
                        /* update u2_abs_coeff_remaining */
1201
410M
                        u2_abs_coeff_remaining[num_gt1_flag] = (UWORD16)abs(quant_coeff) - 1;
1202
1203
410M
                        num_gt1_flag++;
1204
410M
                    }
1205
1206
490M
                    if(quant_coeff < 0)
1207
253M
                    {
1208
                        /* set the i th bit of u2_sign_flags */
1209
253M
                        u2_sign_flags = u2_sign_flags | (1 << i);
1210
253M
                    }
1211
1212
490M
                    num_gt0_flag++;
1213
490M
                }
1214
617M
            }
1215
1216
            /* storing u2_sig_coeff_abs_gt0_flags 2 bytes */
1217
38.5M
            *pu2_out_data_coeff = u2_sig_coeff_abs_gt0_flags;
1218
38.5M
            pu2_out_data_coeff++;
1219
38.5M
            num_bytes += 2;
1220
1221
            /* storing u2_sig_coeff_abs_gt1_flags 2 bytes */
1222
38.5M
            *pu2_out_data_coeff = u2_sig_coeff_abs_gt1_flags;
1223
38.5M
            pu2_out_data_coeff++;
1224
38.5M
            num_bytes += 2;
1225
1226
            /* storing u2_sign_flags 2 bytes */
1227
38.5M
            *pu2_out_data_coeff = u2_sign_flags;
1228
38.5M
            pu2_out_data_coeff++;
1229
38.5M
            num_bytes += 2;
1230
1231
            /* Store the u2_abs_coeff_remaining[] */
1232
448M
            for(i = 0; i < num_gt1_flag; i++)
1233
410M
            {
1234
                /* storing u2_abs_coeff_remaining[i] 2 bytes */
1235
410M
                *pu2_out_data_coeff = u2_abs_coeff_remaining[i];
1236
410M
                pu2_out_data_coeff++;
1237
410M
                num_bytes += 2;
1238
410M
            }
1239
38.5M
        }
1240
43.8M
    }
1241
1242
24.2M
    return num_bytes; /* Return the number of bytes written to out_data */
1243
24.2M
}
1244
1245
/**
1246
*******************************************************************************
1247
* \if Function name : ihevce_populate_intra_pred_mode \endif
1248
*
1249
* \brief * populates intra pred modes,b2_mpm_idx,b1_prev_intra_luma_pred_flag &
1250
* b5_rem_intra_pred_mode for a CU based on nieghbouring CUs,
1251
*
1252
* \par   Description
1253
* Computes the b1_prev_intra_luma_pred_flag, b2_mpm_idx & b5_rem_intra_pred_mode
1254
* for a CU
1255
*
1256
* \param[in] top_intra_mode Top intra mode
1257
* \param[in] left_intra_mode Left intra mode
1258
* \param[in] available_top Top availability flag
1259
* \param[in] available_left Left availability flag
1260
* \param[in] cu_pos_y CU 'y' position
1261
* \param[in] ps_cand_mode_list pointer to populate candidate list
1262
*
1263
* \returns none
1264
*
1265
* \author
1266
*  Ittiam
1267
*
1268
*******************************************************************************
1269
*/
1270
1271
void ihevce_populate_intra_pred_mode(
1272
    WORD32 top_intra_mode,
1273
    WORD32 left_intra_mode,
1274
    WORD32 available_top,
1275
    WORD32 available_left,
1276
    WORD32 cu_pos_y,
1277
    WORD32 *ps_cand_mode_list)
1278
2.09M
{
1279
    /* local variables */
1280
2.09M
    WORD32 cand_intra_pred_mode_left, cand_intra_pred_mode_top;
1281
1282
    /* Calculate cand_intra_pred_mode_N as per sec. 8.4.2 in JCTVC-J1003_d7 */
1283
    /* N = top */
1284
2.09M
    if(0 == available_top)
1285
224k
    {
1286
224k
        cand_intra_pred_mode_top = INTRA_DC;
1287
224k
    }
1288
    /* for neighbour != INTRA, setting DC is done outside */
1289
1.87M
    else if(0 == cu_pos_y) /* It's on the CTB boundary */
1290
93.8k
    {
1291
93.8k
        cand_intra_pred_mode_top = INTRA_DC;
1292
93.8k
    }
1293
1.77M
    else
1294
1.77M
    {
1295
1.77M
        cand_intra_pred_mode_top = top_intra_mode;
1296
1.77M
    }
1297
1298
    /* N = left */
1299
2.09M
    if(0 == available_left)
1300
234k
    {
1301
234k
        cand_intra_pred_mode_left = INTRA_DC;
1302
234k
    }
1303
    /* for neighbour != INTRA, setting DC is done outside */
1304
1.86M
    else
1305
1.86M
    {
1306
1.86M
        cand_intra_pred_mode_left = left_intra_mode;
1307
1.86M
    }
1308
1309
    /* Calculate cand_mode_list as per sec. 8.4.2 in JCTVC-J1003_d7 */
1310
2.09M
    if(cand_intra_pred_mode_left == cand_intra_pred_mode_top)
1311
712k
    {
1312
712k
        if(cand_intra_pred_mode_left < 2)
1313
469k
        {
1314
469k
            ps_cand_mode_list[0] = INTRA_PLANAR;
1315
469k
            ps_cand_mode_list[1] = INTRA_DC;
1316
469k
            ps_cand_mode_list[2] = INTRA_ANGULAR(26); /* angular 26 = Vertical */
1317
469k
        }
1318
242k
        else
1319
242k
        {
1320
242k
            ps_cand_mode_list[0] = cand_intra_pred_mode_left;
1321
242k
            ps_cand_mode_list[1] = 2 + ((cand_intra_pred_mode_left + 29) % 32);
1322
242k
            ps_cand_mode_list[2] = 2 + ((cand_intra_pred_mode_left - 2 + 1) % 32);
1323
242k
        }
1324
712k
    }
1325
1.38M
    else
1326
1.38M
    {
1327
1.38M
        ps_cand_mode_list[0] = cand_intra_pred_mode_left;
1328
1.38M
        ps_cand_mode_list[1] = cand_intra_pred_mode_top;
1329
1330
1.38M
        if((cand_intra_pred_mode_left != INTRA_PLANAR) &&
1331
1.07M
           (cand_intra_pred_mode_top != INTRA_PLANAR))
1332
766k
        {
1333
766k
            ps_cand_mode_list[2] = INTRA_PLANAR;
1334
766k
        }
1335
617k
        else if((cand_intra_pred_mode_left != INTRA_DC) && (cand_intra_pred_mode_top != INTRA_DC))
1336
232k
        {
1337
232k
            ps_cand_mode_list[2] = INTRA_DC;
1338
232k
        }
1339
384k
        else
1340
384k
        {
1341
384k
            ps_cand_mode_list[2] = INTRA_ANGULAR(26);
1342
384k
        }
1343
1.38M
    }
1344
2.09M
}
1345
/**
1346
*******************************************************************************
1347
* \if Function name : ihevce_intra_pred_mode_signaling \endif
1348
*
1349
* \brief * Computes the b1_prev_intra_luma_pred_flag, b2_mpm_idx &
1350
* b5_rem_intra_pred_mode for a CU
1351
*
1352
* \par   Description
1353
* Computes the b1_prev_intra_luma_pred_flag, b2_mpm_idx & b5_rem_intra_pred_mode
1354
* for a CU
1355
*
1356
* \param[in] ps_nbr_top Top neighbour context
1357
* \param[in] ps_nbr_left Left neighbour context
1358
* \param[in] available_top Top availability flag
1359
* \param[in] available_left Left availability flag
1360
* \param[in] cu_pos_y CU 'y' position
1361
* \param[in] luma_intra_pred_mode_current the intra_pred_mode of current block
1362
* \param[inout] ps_intra_pred_mode_current
1363
* Pointer to structure having b1_prev_intra_luma_pred_flag, b2_mpm_idx and
1364
* b5_rem_intra_pred_mode
1365
*
1366
* \returns none
1367
*
1368
* \author
1369
*  Ittiam
1370
*
1371
*******************************************************************************
1372
*/
1373
1374
void ihevce_intra_pred_mode_signaling(
1375
    WORD32 top_intra_mode,
1376
    WORD32 left_intra_mode,
1377
    WORD32 available_top,
1378
    WORD32 available_left,
1379
    WORD32 cu_pos_y,
1380
    WORD32 luma_intra_pred_mode_current,
1381
    intra_prev_rem_flags_t *ps_intra_pred_mode_current)
1382
29.3M
{
1383
    /* local variables */
1384
29.3M
    WORD32 cand_intra_pred_mode_left, cand_intra_pred_mode_top;
1385
29.3M
    WORD32 cand_mode_list[3];
1386
1387
29.3M
    ps_intra_pred_mode_current->b1_prev_intra_luma_pred_flag = 0;
1388
29.3M
    ps_intra_pred_mode_current->b2_mpm_idx = 0;  // for safety purpose
1389
29.3M
    ps_intra_pred_mode_current->b5_rem_intra_pred_mode = 0;
1390
1391
    /* Calculate cand_intra_pred_mode_N as per sec. 8.4.2 in JCTVC-J1003_d7 */
1392
    /* N = top */
1393
29.3M
    if(0 == available_top)
1394
3.64M
    {
1395
3.64M
        cand_intra_pred_mode_top = INTRA_DC;
1396
3.64M
    }
1397
    /* for neighbour != INTRA, setting DC is done outside */
1398
25.7M
    else if(0 == cu_pos_y) /* It's on the CTB boundary */
1399
1.69M
    {
1400
1.69M
        cand_intra_pred_mode_top = INTRA_DC;
1401
1.69M
    }
1402
24.0M
    else
1403
24.0M
    {
1404
24.0M
        cand_intra_pred_mode_top = top_intra_mode;
1405
24.0M
    }
1406
1407
    /* N = left */
1408
29.3M
    if(0 == available_left)
1409
3.32M
    {
1410
3.32M
        cand_intra_pred_mode_left = INTRA_DC;
1411
3.32M
    }
1412
    /* for neighbour != INTRA, setting DC is done outside */
1413
26.0M
    else
1414
26.0M
    {
1415
26.0M
        cand_intra_pred_mode_left = left_intra_mode;
1416
26.0M
    }
1417
1418
    /* Calculate cand_mode_list as per sec. 8.4.2 in JCTVC-J1003_d7 */
1419
29.3M
    if(cand_intra_pred_mode_left == cand_intra_pred_mode_top)
1420
13.5M
    {
1421
13.5M
        if(cand_intra_pred_mode_left < 2)
1422
10.1M
        {
1423
10.1M
            cand_mode_list[0] = INTRA_PLANAR;
1424
10.1M
            cand_mode_list[1] = INTRA_DC;
1425
10.1M
            cand_mode_list[2] = INTRA_ANGULAR(26); /* angular 26 = Vertical */
1426
10.1M
        }
1427
3.38M
        else
1428
3.38M
        {
1429
3.38M
            cand_mode_list[0] = cand_intra_pred_mode_left;
1430
3.38M
            cand_mode_list[1] = 2 + ((cand_intra_pred_mode_left + 29) % 32);
1431
3.38M
            cand_mode_list[2] = 2 + ((cand_intra_pred_mode_left - 2 + 1) % 32);
1432
3.38M
        }
1433
13.5M
    }
1434
15.8M
    else
1435
15.8M
    {
1436
15.8M
        cand_mode_list[0] = cand_intra_pred_mode_left;
1437
15.8M
        cand_mode_list[1] = cand_intra_pred_mode_top;
1438
1439
15.8M
        if((cand_intra_pred_mode_left != INTRA_PLANAR) &&
1440
11.3M
           (cand_intra_pred_mode_top != INTRA_PLANAR))
1441
7.70M
        {
1442
7.70M
            cand_mode_list[2] = INTRA_PLANAR;
1443
7.70M
        }
1444
8.12M
        else if((cand_intra_pred_mode_left != INTRA_DC) && (cand_intra_pred_mode_top != INTRA_DC))
1445
2.74M
        {
1446
2.74M
            cand_mode_list[2] = INTRA_DC;
1447
2.74M
        }
1448
5.37M
        else
1449
5.37M
        {
1450
5.37M
            cand_mode_list[2] = INTRA_ANGULAR(26);
1451
5.37M
        }
1452
15.8M
    }
1453
1454
    /* Signal Generation */
1455
1456
    /* Flag & mpm_index generation */
1457
29.3M
    if(cand_mode_list[0] == luma_intra_pred_mode_current)
1458
9.17M
    {
1459
9.17M
        ps_intra_pred_mode_current->b1_prev_intra_luma_pred_flag = 1;
1460
9.17M
        ps_intra_pred_mode_current->b2_mpm_idx = 0;
1461
9.17M
    }
1462
20.1M
    else if(cand_mode_list[1] == luma_intra_pred_mode_current)
1463
7.68M
    {
1464
7.68M
        ps_intra_pred_mode_current->b1_prev_intra_luma_pred_flag = 1;
1465
7.68M
        ps_intra_pred_mode_current->b2_mpm_idx = 1;
1466
7.68M
    }
1467
12.4M
    else if(cand_mode_list[2] == luma_intra_pred_mode_current)
1468
4.23M
    {
1469
4.23M
        ps_intra_pred_mode_current->b1_prev_intra_luma_pred_flag = 1;
1470
4.23M
        ps_intra_pred_mode_current->b2_mpm_idx = 2;
1471
4.23M
    }
1472
    /* Flag & b5_rem_intra_pred_mode generation */
1473
8.26M
    else
1474
8.26M
    {
1475
8.26M
        WORD32 rem_mode;
1476
1477
8.26M
        ps_intra_pred_mode_current->b1_prev_intra_luma_pred_flag = 0;
1478
1479
        /* sorting cand_mode_list */
1480
8.26M
        if(cand_mode_list[0] > cand_mode_list[1])
1481
3.80M
        {
1482
3.80M
            SWAP(cand_mode_list[0], cand_mode_list[1]);
1483
3.80M
        }
1484
8.26M
        if(cand_mode_list[0] > cand_mode_list[2])
1485
3.31M
        {
1486
3.31M
            SWAP(cand_mode_list[0], cand_mode_list[2]);
1487
3.31M
        }
1488
8.26M
        if(cand_mode_list[1] > cand_mode_list[2])
1489
4.31M
        {
1490
4.31M
            SWAP(cand_mode_list[1], cand_mode_list[2]);
1491
4.31M
        }
1492
1493
8.26M
        rem_mode = luma_intra_pred_mode_current;
1494
1495
8.26M
        if((rem_mode) >= cand_mode_list[2])
1496
2.78M
        {
1497
2.78M
            (rem_mode)--;
1498
2.78M
        }
1499
8.26M
        if((rem_mode) >= cand_mode_list[1])
1500
6.82M
        {
1501
6.82M
            (rem_mode)--;
1502
6.82M
        }
1503
8.26M
        if((rem_mode) >= cand_mode_list[0])
1504
7.52M
        {
1505
7.52M
            (rem_mode)--;
1506
7.52M
        }
1507
8.26M
        ps_intra_pred_mode_current->b5_rem_intra_pred_mode = rem_mode;
1508
8.26M
    }
1509
29.3M
}
1510
1511
void ihevce_quant_rounding_factor_gen(
1512
    WORD32 i4_trans_size,
1513
    WORD32 is_luma,
1514
    rdopt_entropy_ctxt_t *ps_rdopt_entropy_ctxt,
1515
    WORD32 *pi4_quant_round_0_1,
1516
    WORD32 *pi4_quant_round_1_2,
1517
    double i4_lamda_modifier,
1518
    UWORD8 i4_is_tu_level_quant_rounding)
1519
9.08M
{
1520
    //WORD32 i4_scan_idx = ps_ctxt->i4_scan_idx;
1521
9.08M
    UWORD8 *pu1_ctxt_model;
1522
9.08M
    WORD32 scan_pos;
1523
9.08M
    WORD32 sig_coeff_base_ctxt; /* cabac context for sig coeff flag    */
1524
9.08M
    WORD32 abs_gt1_base_ctxt;
1525
9.08M
    WORD32 log2_tr_size, i;
1526
9.08M
    UWORD16 u4_bits_estimated_r0, u4_bits_estimated_r1, u4_bits_estimated_r2;
1527
9.08M
    UWORD16 u4_bits_estimated_r1_temp;
1528
9.08M
    WORD32 j = 0;
1529
9.08M
    WORD32 k = 0;
1530
9.08M
    WORD32 temp2;
1531
1532
9.08M
    double i4_lamda_mod = i4_lamda_modifier * pow(2.0, (-8.0 / 3.0));
1533
9.08M
    LWORD64 lamda_mod = (LWORD64)(i4_lamda_mod * (1 << LAMDA_Q_SHIFT_FACT));
1534
    /* transform size to log2transform size */
1535
9.08M
    GETRANGE(log2_tr_size, i4_trans_size);
1536
9.08M
    log2_tr_size -= 1;
1537
1538
9.08M
    if(1 == i4_is_tu_level_quant_rounding)
1539
0
    {
1540
0
        entropy_context_t *ps_cur_tu_entropy;
1541
0
        cab_ctxt_t *ps_cabac;
1542
0
        WORD32 curr_buf_idx = ps_rdopt_entropy_ctxt->i4_curr_buf_idx;
1543
0
        ps_cur_tu_entropy = &ps_rdopt_entropy_ctxt->as_cu_entropy_ctxt[curr_buf_idx];
1544
1545
0
        ps_cabac = &ps_cur_tu_entropy->s_cabac_ctxt;
1546
1547
0
        pu1_ctxt_model = &ps_cabac->au1_ctxt_models[0];
1548
0
    }
1549
9.08M
    else
1550
9.08M
    {
1551
9.08M
        pu1_ctxt_model = &ps_rdopt_entropy_ctxt->au1_init_cabac_ctxt_states[0];
1552
9.08M
    }
1553
    /*If transform size is 4x4, then only one sub-block*/
1554
9.08M
    if(is_luma)
1555
5.56M
    {
1556
5.56M
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG;
1557
5.56M
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG;
1558
1559
5.56M
        if(3 == log2_tr_size)
1560
2.03M
        {
1561
            /* 8x8 transform size */
1562
            /* Assuming diagnol scan idx for now */
1563
2.03M
            sig_coeff_base_ctxt += 9;
1564
2.03M
        }
1565
3.52M
        else if(3 < log2_tr_size)
1566
1.49M
        {
1567
            /* larger transform sizes */
1568
1.49M
            sig_coeff_base_ctxt += 21;
1569
1.49M
        }
1570
5.56M
    }
1571
3.52M
    else
1572
3.52M
    {
1573
        /* chroma context initializations */
1574
3.52M
        sig_coeff_base_ctxt = IHEVC_CAB_COEFF_FLAG + 27;
1575
3.52M
        abs_gt1_base_ctxt = IHEVC_CAB_COEFABS_GRTR1_FLAG + 16;
1576
1577
3.52M
        if(3 == log2_tr_size)
1578
1.15M
        {
1579
            /* 8x8 transform size */
1580
1.15M
            sig_coeff_base_ctxt += 9;
1581
1.15M
        }
1582
2.37M
        else if(3 < log2_tr_size)
1583
342k
        {
1584
            /* larger transform sizes */
1585
342k
            sig_coeff_base_ctxt += 12;
1586
342k
        }
1587
3.52M
    }
1588
1589
    /*Transform size of 4x4 will have only a single CSB */
1590
    /* derive the context inc as per section 9.3.3.1.4 */
1591
1592
9.08M
    if(2 == log2_tr_size)
1593
4.06M
    {
1594
4.06M
        UWORD8 sig_ctxinc;
1595
4.06M
        WORD32 state_mps;
1596
4.06M
        WORD32 gt1_ctxt = 0;
1597
4.06M
        WORD32 ctxt_set = 0;
1598
4.06M
        WORD32 ctxt_idx = 0;
1599
1600
        /* context set based on luma subblock pos */
1601
1602
        /* Encodet the abs level gt1 bins */
1603
        /* Currently calculating trade off between mps(2) and mps(1)*/
1604
        /* The estimation has to be further done for mps(11) and mps(111)*/
1605
        /*ctxt_set = 0 as transform 4x4 has only one csb with DC */
1606
        /* gt1_ctxt = 0 for the co-ef value to be 2 */
1607
1608
4.06M
        ctxt_set = gt1_ctxt = 0;
1609
4.06M
        ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
1610
1611
4.06M
        state_mps = pu1_ctxt_model[ctxt_idx];
1612
1613
4.06M
        u4_bits_estimated_r2 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 1];
1614
1615
4.06M
        u4_bits_estimated_r1_temp = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1616
1617
4.06M
        QUANT_ROUND_FACTOR(temp2, u4_bits_estimated_r2, u4_bits_estimated_r1_temp, lamda_mod);
1618
69.1M
        for(scan_pos = 0; scan_pos < 16; scan_pos++)
1619
65.0M
        {
1620
65.0M
            *(pi4_quant_round_1_2 + scan_pos) = temp2;
1621
65.0M
        }
1622
1623
69.1M
        for(scan_pos = 0; scan_pos < 16; scan_pos++)
1624
65.0M
        {
1625
            //UWORD8 nbr_csbf = 1;
1626
            /* derive the x,y pos */
1627
65.0M
            UWORD8 y_pos_x_pos = scan_pos;  //gu1_hevce_scan4x4[i4_scan_idx][scan_pos];
1628
1629
            /* 4x4 transform size increment uses lookup */
1630
65.0M
            sig_ctxinc = gu1_hevce_sigcoeff_ctxtinc_tr4[y_pos_x_pos];
1631
1632
            /*Get the mps state based on ctxt modes */
1633
65.0M
            state_mps = pu1_ctxt_model[sig_ctxinc + sig_coeff_base_ctxt];
1634
1635
            /* Bits taken to encode sig co-ef flag as 0 */
1636
65.0M
            u4_bits_estimated_r0 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1637
1638
            /* Bits taken to encode sig co-ef flag as 1, also account for sign bit worst case */
1639
            //
1640
65.0M
            u4_bits_estimated_r1 =
1641
65.0M
                (gau2_ihevce_cabac_bin_to_bits[state_mps ^ 1] + ROUND_Q12(1.000000000));
1642
1643
            /* Bits taken to encode sig co-ef flag as 1, also account for sign bit worst case */
1644
65.0M
            u4_bits_estimated_r1 += u4_bits_estimated_r1_temp;
1645
1646
65.0M
            QUANT_ROUND_FACTOR(temp2, u4_bits_estimated_r1, u4_bits_estimated_r0, lamda_mod);
1647
65.0M
            *(pi4_quant_round_0_1 + scan_pos) = temp2;
1648
65.0M
        }
1649
4.06M
    }
1650
5.02M
    else
1651
5.02M
    {
1652
5.02M
        UWORD8 *pu1_hevce_sigcoeff_ctxtinc;
1653
5.02M
        WORD32 is_nbr_csb_state_mps;
1654
1655
5.02M
        WORD32 state_mps;
1656
5.02M
        WORD32 gt1_ctxt = 0;
1657
5.02M
        WORD32 ctxt_set = 0;
1658
5.02M
        WORD32 ctxt_idx;
1659
        /*1to2 rounding factor is same for all sub blocks except for sub-block = 0*/
1660
        /*Hence will write all the sub-block with i >=1 coeff, and then overwrite for i = 0*/
1661
1662
        /*ctxt_set = 0 DC subblock, the previous state did not have 2
1663
        ctxt_set = 1 DC subblock, the previous state did have >= 2
1664
        ctxt_set = 2 AC subblock, the previous state did not have 2
1665
        ctxt_set = 3 AC subblock, the previous state did have >= 2*/
1666
5.02M
        i = 1;
1667
5.02M
        ctxt_set = (i && is_luma) ? 2 : 0;
1668
1669
5.02M
        ctxt_set++;
1670
1671
        /*0th position indicates the probability of 2 */
1672
        /*1th position indicates the probability of 1 */
1673
        /*2th position indicates the probability of 11 */
1674
        /*3th position indicates the probability of 111 */
1675
1676
5.02M
        gt1_ctxt = 0;
1677
5.02M
        ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
1678
1679
5.02M
        state_mps = pu1_ctxt_model[ctxt_idx];
1680
1681
5.02M
        u4_bits_estimated_r2 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 1];
1682
1683
5.02M
        u4_bits_estimated_r1 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1684
5.02M
        QUANT_ROUND_FACTOR(temp2, u4_bits_estimated_r2, u4_bits_estimated_r1, lamda_mod);
1685
1686
941M
        for(scan_pos = 0; scan_pos < (16 * (i4_trans_size * i4_trans_size >> 4)); scan_pos++)
1687
936M
        {
1688
936M
            *(pi4_quant_round_1_2 + scan_pos) = temp2;
1689
936M
        }
1690
1691
5.02M
        i = 0;
1692
5.02M
        ctxt_set = (i && is_luma) ? 2 : 0;
1693
5.02M
        ctxt_set++;
1694
1695
        /*0th position indicates the probability of 2 */
1696
        /*1th position indicates the probability of 1 */
1697
        /*2th position indicates the probability of 11 */
1698
        /*3th position indicates the probability of 111 */
1699
1700
5.02M
        gt1_ctxt = 0;
1701
5.02M
        ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
1702
1703
5.02M
        state_mps = pu1_ctxt_model[ctxt_idx];
1704
1705
5.02M
        u4_bits_estimated_r2 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 1];
1706
1707
5.02M
        u4_bits_estimated_r1 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1708
5.02M
        QUANT_ROUND_FACTOR(temp2, u4_bits_estimated_r2, u4_bits_estimated_r1, lamda_mod);
1709
1710
85.3M
        for(scan_pos = 0; scan_pos < 16; scan_pos++)
1711
80.3M
        {
1712
80.3M
            *(pi4_quant_round_1_2 + ((scan_pos % 4) + ((scan_pos >> 2) * i4_trans_size))) = temp2;
1713
80.3M
        }
1714
1715
5.02M
        {
1716
5.02M
            WORD32 ctxt_idx;
1717
1718
5.02M
            WORD32 nbr_csbf_0, nbr_csbf_1;
1719
5.02M
            WORD32 state_mps_0, state_mps_1;
1720
5.02M
            ctxt_idx = IHEVC_CAB_CODED_SUBLK_IDX;
1721
5.02M
            ctxt_idx += is_luma ? 0 : 2;
1722
1723
            /* ctxt based on right / bottom avail csbf, section 9.3.3.1.3 */
1724
            /* if neibhor not available, ctxt idx = 0*/
1725
5.02M
            nbr_csbf_0 = 0;
1726
5.02M
            ctxt_idx += nbr_csbf_0 ? 1 : 0;
1727
5.02M
            state_mps_0 = pu1_ctxt_model[ctxt_idx];
1728
1729
5.02M
            nbr_csbf_1 = 1;
1730
5.02M
            ctxt_idx += nbr_csbf_1 ? 1 : 0;
1731
5.02M
            state_mps_1 = pu1_ctxt_model[ctxt_idx];
1732
1733
5.02M
            is_nbr_csb_state_mps = ((state_mps_0 % 2) == 1) && ((state_mps_1 % 2) == 1);
1734
5.02M
        }
1735
1736
5.02M
        if(1 == is_nbr_csb_state_mps)
1737
899k
        {
1738
11.9M
            for(i = 0; i < (i4_trans_size * i4_trans_size >> 4); i++)
1739
11.0M
            {
1740
11.0M
                UWORD8 sig_ctxinc;
1741
11.0M
                WORD32 state_mps;
1742
11.0M
                WORD32 gt1_ctxt = 0;
1743
11.0M
                WORD32 ctxt_set = 0;
1744
1745
11.0M
                WORD32 ctxt_idx;
1746
1747
                /*Check if the cabac states had previous nbr available */
1748
1749
11.0M
                if(i == 0)
1750
899k
                    pu1_hevce_sigcoeff_ctxtinc = (UWORD8 *)&gu1_hevce_sigcoeff_ctxtinc[3][0];
1751
10.1M
                else if(i < (i4_trans_size >> 2))
1752
1.83M
                    pu1_hevce_sigcoeff_ctxtinc = (UWORD8 *)&gu1_hevce_sigcoeff_ctxtinc[1][0];
1753
8.26M
                else if((i % (i4_trans_size >> 2)) == 0)
1754
1.83M
                    pu1_hevce_sigcoeff_ctxtinc = (UWORD8 *)&gu1_hevce_sigcoeff_ctxtinc[2][0];
1755
6.43M
                else
1756
6.43M
                    pu1_hevce_sigcoeff_ctxtinc = (UWORD8 *)&gu1_hevce_sigcoeff_ctxtinc[0][0];
1757
1758
11.0M
                if(((i % (i4_trans_size >> 2)) == 0) && (i != 0))
1759
1.83M
                    k++;
1760
1761
11.0M
                j = ((i4_trans_size * 4) * k) + ((i % (i4_trans_size >> 2)) * 4);
1762
                /*ctxt_set = 0 DC subblock, the previous state did not have 2
1763
                ctxt_set = 1 DC subblock, the previous state did have >= 2
1764
                ctxt_set = 2 AC subblock, the previous state did not have 2
1765
                ctxt_set = 3 AC subblock, the previous state did have >= 2*/
1766
1767
11.0M
                ctxt_set = (i && is_luma) ? 2 : 0;
1768
1769
                /* gt1_ctxt = 1 for the co-ef value to be 1 */
1770
11.0M
                gt1_ctxt = 0;
1771
11.0M
                ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
1772
1773
11.0M
                state_mps = pu1_ctxt_model[ctxt_idx];
1774
1775
                /* Bits taken to encode sig co-ef flag as 1, also account for sign bit worst case */
1776
11.0M
                u4_bits_estimated_r1_temp = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1777
1778
187M
                for(scan_pos = 0; scan_pos < 16; scan_pos++)
1779
176M
                {
1780
176M
                    UWORD8 y_pos_x_pos;
1781
1782
176M
                    if(scan_pos || i)
1783
175M
                    {
1784
175M
                        y_pos_x_pos = scan_pos;  // gu1_hevce_scan4x4[i4_scan_idx][scan_pos];
1785
                        /* ctxt for AC coeff depends on curpos and neigbour csbf */
1786
175M
                        sig_ctxinc = pu1_hevce_sigcoeff_ctxtinc[y_pos_x_pos];
1787
1788
                        /* based on luma subblock pos */
1789
175M
                        sig_ctxinc += (i && is_luma) ? 3 : 0;
1790
1791
175M
                        sig_ctxinc += sig_coeff_base_ctxt;
1792
175M
                    }
1793
899k
                    else
1794
899k
                    {
1795
                        /*MAM : both scan pos and i 0 impies the DC coef of 1st block only */
1796
                        /* DC coeff has fixed context for luma and chroma */
1797
899k
                        sig_ctxinc = is_luma ? IHEVC_CAB_COEFF_FLAG : IHEVC_CAB_COEFF_FLAG + 27;
1798
899k
                    }
1799
1800
                    /*Get the mps state based on ctxt modes */
1801
176M
                    state_mps = pu1_ctxt_model[sig_ctxinc];
1802
1803
                    /* Bits taken to encode sig co-ef flag as 0 */
1804
176M
                    u4_bits_estimated_r0 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1805
1806
176M
                    u4_bits_estimated_r1 =
1807
176M
                        (gau2_ihevce_cabac_bin_to_bits[state_mps ^ 1] + ROUND_Q12(1.000000000));
1808
1809
                    /* Bits taken to encode sig co-ef flag as 1, also account for sign bit worst case */
1810
176M
                    u4_bits_estimated_r1 += u4_bits_estimated_r1_temp;
1811
176M
                    {
1812
176M
                        QUANT_ROUND_FACTOR(
1813
176M
                            temp2, u4_bits_estimated_r1, u4_bits_estimated_r0, lamda_mod);
1814
176M
                        *(pi4_quant_round_0_1 +
1815
176M
                          ((scan_pos % 4) + ((scan_pos >> 2) * i4_trans_size)) + j) = temp2;
1816
176M
                    }
1817
176M
                }
1818
11.0M
            }
1819
899k
        }
1820
4.12M
        else
1821
4.12M
        {
1822
            /*If Both nbr csbfs are 0, then all the coef in sub-blocks will have same value except for 1st subblock,
1823
            Hence will write the same value to all sub block, and overwrite for the 1st one */
1824
4.12M
            i = 1;
1825
4.12M
            {
1826
4.12M
                UWORD8 sig_ctxinc;
1827
4.12M
                UWORD8 y_pos_x_pos;
1828
4.12M
                WORD32 quant_rounding_0_1;
1829
1830
4.12M
                pu1_hevce_sigcoeff_ctxtinc = (UWORD8 *)&gu1_hevce_sigcoeff_ctxtinc_00[0];
1831
1832
4.12M
                scan_pos = 0;
1833
4.12M
                y_pos_x_pos = scan_pos;  // gu1_hevce_scan4x4[i4_scan_idx][scan_pos];
1834
                /* ctxt for AC coeff depends on curpos and neigbour csbf */
1835
4.12M
                sig_ctxinc = pu1_hevce_sigcoeff_ctxtinc[y_pos_x_pos];
1836
1837
                /* based on luma subblock pos */
1838
4.12M
                sig_ctxinc += (is_luma) ? 3 : 0;
1839
1840
4.12M
                sig_ctxinc += sig_coeff_base_ctxt;
1841
1842
                /*Get the mps state based on ctxt modes */
1843
4.12M
                state_mps = pu1_ctxt_model[sig_ctxinc];
1844
1845
                /* Bits taken to encode sig co-ef flag as 0 */
1846
4.12M
                u4_bits_estimated_r0 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1847
1848
4.12M
                u4_bits_estimated_r1 =
1849
4.12M
                    (gau2_ihevce_cabac_bin_to_bits[state_mps ^ 1] + ROUND_Q12(1.000000000));
1850
1851
                /*ctxt_set = 0 DC subblock, the previous state did not have 2
1852
                ctxt_set = 1 DC subblock, the previous state did have >= 2
1853
                ctxt_set = 2 AC subblock, the previous state did not have 2
1854
                ctxt_set = 3 AC subblock, the previous state did have >= 2*/
1855
1856
4.12M
                ctxt_set = (i && is_luma) ? 2 : 0;
1857
1858
                /* gt1_ctxt = 1 for the co-ef value to be 1 */
1859
4.12M
                gt1_ctxt = 0;
1860
4.12M
                ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
1861
1862
4.12M
                state_mps = pu1_ctxt_model[ctxt_idx];
1863
1864
                /* Bits taken to encode sig co-ef flag as 1, also account for sign bit worst case */
1865
4.12M
                u4_bits_estimated_r1 += gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1866
1867
4.12M
                QUANT_ROUND_FACTOR(
1868
4.12M
                    quant_rounding_0_1, u4_bits_estimated_r1, u4_bits_estimated_r0, lamda_mod);
1869
1870
764M
                for(scan_pos = 0; scan_pos < (16 * (i4_trans_size * i4_trans_size >> 4));
1871
760M
                    scan_pos++)
1872
760M
                {
1873
760M
                    *(pi4_quant_round_0_1 + scan_pos) = quant_rounding_0_1;
1874
760M
                }
1875
4.12M
            }
1876
1877
            /*First Subblock*/
1878
4.12M
            i = 0;
1879
1880
4.12M
            {
1881
4.12M
                UWORD8 sig_ctxinc;
1882
4.12M
                WORD32 state_mps;
1883
4.12M
                WORD32 gt1_ctxt = 0;
1884
4.12M
                WORD32 ctxt_set = 0;
1885
1886
4.12M
                WORD32 ctxt_idx;
1887
1888
                /*Check if the cabac states had previous nbr available */
1889
1890
4.12M
                {
1891
4.12M
                    pu1_hevce_sigcoeff_ctxtinc = (UWORD8 *)&gu1_hevce_sigcoeff_ctxtinc[0][0];
1892
1893
                    /*ctxt_set = 0 DC subblock, the previous state did not have 2
1894
                    ctxt_set = 1 DC subblock, the previous state did have >= 2
1895
                    ctxt_set = 2 AC subblock, the previous state did not have 2
1896
                    ctxt_set = 3 AC subblock, the previous state did have >= 2*/
1897
4.12M
                    ctxt_set = (i && is_luma) ? 2 : 0;
1898
1899
                    /* gt1_ctxt = 1 for the co-ef value to be 1 */
1900
4.12M
                    gt1_ctxt = 0;
1901
4.12M
                    ctxt_idx = (ctxt_set * 4) + abs_gt1_base_ctxt + gt1_ctxt;
1902
1903
4.12M
                    state_mps = pu1_ctxt_model[ctxt_idx];
1904
1905
                    /* Bits taken to encode sig co-ef flag as 1, also account for sign bit worst case */
1906
4.12M
                    u4_bits_estimated_r1_temp = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1907
1908
70.0M
                    for(scan_pos = 0; scan_pos < 16; scan_pos++)
1909
65.9M
                    {
1910
65.9M
                        UWORD8 y_pos_x_pos;
1911
1912
65.9M
                        if(scan_pos)
1913
61.8M
                        {
1914
61.8M
                            y_pos_x_pos = scan_pos;  // gu1_hevce_scan4x4[i4_scan_idx][scan_pos];
1915
                            /* ctxt for AC coeff depends on curpos and neigbour csbf */
1916
61.8M
                            sig_ctxinc = pu1_hevce_sigcoeff_ctxtinc[y_pos_x_pos];
1917
1918
                            /* based on luma subblock pos */
1919
61.8M
                            sig_ctxinc += (i && is_luma) ? 3 : 0;
1920
1921
61.8M
                            sig_ctxinc += sig_coeff_base_ctxt;
1922
61.8M
                        }
1923
4.12M
                        else
1924
4.12M
                        {
1925
                            /*MAM : both scan pos and i 0 impies the DC coef of 1st block only */
1926
                            /* DC coeff has fixed context for luma and chroma */
1927
4.12M
                            sig_ctxinc = is_luma ? IHEVC_CAB_COEFF_FLAG : IHEVC_CAB_COEFF_FLAG + 27;
1928
4.12M
                        }
1929
1930
                        /*Get the mps state based on ctxt modes */
1931
65.9M
                        state_mps = pu1_ctxt_model[sig_ctxinc];
1932
1933
                        /* Bits taken to encode sig co-ef flag as 0 */
1934
65.9M
                        u4_bits_estimated_r0 = gau2_ihevce_cabac_bin_to_bits[state_mps ^ 0];
1935
1936
65.9M
                        u4_bits_estimated_r1 =
1937
65.9M
                            (gau2_ihevce_cabac_bin_to_bits[state_mps ^ 1] + ROUND_Q12(1.000000000));
1938
1939
                        /* Bits taken to encode sig co-ef flag as 1, also account for sign bit worst case */
1940
65.9M
                        u4_bits_estimated_r1 += u4_bits_estimated_r1_temp;
1941
65.9M
                        {
1942
65.9M
                            QUANT_ROUND_FACTOR(
1943
65.9M
                                temp2, u4_bits_estimated_r1, u4_bits_estimated_r0, lamda_mod);
1944
65.9M
                            *(pi4_quant_round_0_1 +
1945
65.9M
                              ((scan_pos % 4) + ((scan_pos >> 2) * i4_trans_size))) = temp2;
1946
65.9M
                        }
1947
65.9M
                    }
1948
4.12M
                }
1949
4.12M
            }
1950
4.12M
        }
1951
5.02M
    }
1952
9.08M
    return;
1953
9.08M
}
1954
1955
/*!
1956
******************************************************************************
1957
* \if Function name : ihevce_t_q_iq_ssd_scan_fxn \endif
1958
*
1959
* \brief
1960
*    Transform unit level (Luma) enc_loop function
1961
*
1962
* \param[in] ps_ctxt    enc_loop module ctxt pointer
1963
* \param[in] pu1_pred   pointer to predicted data buffer
1964
* \param[in] pred_strd  predicted buffer stride
1965
* \param[in] pu1_src    pointer to source data buffer
1966
* \param[in] src_strd   source buffer stride
1967
* \param[in] pi2_deq_data   pointer to store iq data
1968
* \param[in] deq_data_strd  iq data buffer stride
1969
* \param[out] pu1_ecd_data pointer coeff output buffer (input to ent cod)
1970
* \param[out] pu1_csbf_buf  pointer to store the csbf for all 4x4 in a current
1971
*                           block
1972
* \param[out] csbf_strd  csbf buffer stride
1973
* \param[in] trans_size transform size (4, 8, 16,32)
1974
* \param[in] packed_pred_mode   0:Inter 1:Intra 2:Skip
1975
* \param[out] pi4_cost      pointer to store the cost
1976
* \param[out] pi4_coeff_off pointer to store the number of bytes produced in
1977
*                           coeff buffer
1978
* \param[out] pu4_tu_bits   pointer to store the best TU bits required encode
1979
the current TU in RDopt Mode
1980
* \param[out] pu4_blk_sad   pointer to store the block sad for RC
1981
* \param[out] pi4_zero_col  pointer to store the zero_col info for the TU
1982
* \param[out] pi4_zero_row  pointer to store the zero_row info for the TU
1983
* \param[in]  i4_perform_rdoq Indicates if RDOQ should be performed or not
1984
* \param[in]  i4_perform_sbh Indicates if SBH should be performed or not
1985
*
1986
* \return
1987
*    CBF of the current block
1988
*
1989
* \author
1990
*  Ittiam
1991
*
1992
*****************************************************************************
1993
*/
1994
1995
WORD32 ihevce_t_q_iq_ssd_scan_fxn(
1996
    ihevce_enc_loop_ctxt_t *ps_ctxt,
1997
    UWORD8 *pu1_pred,
1998
    WORD32 pred_strd,
1999
    UWORD8 *pu1_src,
2000
    WORD32 src_strd,
2001
    WORD16 *pi2_deq_data,
2002
    WORD32 deq_data_strd,
2003
    UWORD8 *pu1_recon,
2004
    WORD32 i4_recon_stride,
2005
    UWORD8 *pu1_ecd_data,
2006
    UWORD8 *pu1_csbf_buf,
2007
    WORD32 csbf_strd,
2008
    WORD32 trans_size,
2009
    WORD32 packed_pred_mode,
2010
    LWORD64 *pi8_cost,
2011
    WORD32 *pi4_coeff_off,
2012
    WORD32 *pi4_tu_bits,
2013
    UWORD32 *pu4_blk_sad,
2014
    WORD32 *pi4_zero_col,
2015
    WORD32 *pi4_zero_row,
2016
    UWORD8 *pu1_is_recon_available,
2017
    WORD32 i4_perform_rdoq,
2018
    WORD32 i4_perform_sbh,
2019
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
2020
    WORD32 i4_alpha_stim_multiplier,
2021
    UWORD8 u1_is_cu_noisy,
2022
#endif
2023
    SSD_TYPE_T e_ssd_type,
2024
    WORD32 early_cbf)
2025
38.0M
{
2026
38.0M
    WORD32 cbf = 0;
2027
38.0M
    WORD32 trans_idx;
2028
38.0M
    WORD32 quant_scale_mat_offset;
2029
38.0M
    WORD32 *pi4_trans_scratch;
2030
38.0M
    WORD16 *pi2_trans_values;
2031
38.0M
    WORD16 *pi2_quant_coeffs;
2032
38.0M
    WORD32 *pi4_subBlock2csbfId_map = NULL;
2033
2034
#if PROHIBIT_INTRA_QUANT_ROUNDING_FACTOR_TO_DROP_BELOW_1BY3
2035
    WORD32 ai4_quant_rounding_factors[3][MAX_TU_SIZE * MAX_TU_SIZE], i;
2036
#endif
2037
2038
38.0M
    rdoq_sbh_ctxt_t *ps_rdoq_sbh_ctxt = &ps_ctxt->s_rdoq_sbh_ctxt;
2039
2040
38.0M
    WORD32 i4_perform_zcbf = (ENABLE_INTER_ZCU_COST && (PRED_MODE_INTRA != packed_pred_mode)) ||
2041
29.3M
                             (ps_ctxt->i4_zcbf_rdo_level == ZCBF_ENABLE);
2042
38.0M
    WORD32 i4_perform_coeff_level_rdoq = (ps_ctxt->i4_quant_rounding_level != FIXED_QUANT_ROUNDING);
2043
38.0M
    WORD8 intra_flag = 0;
2044
38.0M
    ASSERT(csbf_strd == MAX_TU_IN_CTB_ROW);
2045
2046
38.0M
    *pi4_tu_bits = 0;
2047
38.0M
    *pi4_coeff_off = 0;
2048
38.0M
    *pi4_zero_col = 0;
2049
38.0M
    *pi4_zero_row = 0;
2050
38.0M
    pu1_is_recon_available[0] = 0;
2051
2052
38.0M
    if((PRED_MODE_SKIP == packed_pred_mode) || (0 == early_cbf))
2053
1.85M
    {
2054
1.85M
        if(e_ssd_type != NULL_TYPE)
2055
1.85M
        {
2056
            /* SSD cost is stored to the pointer */
2057
1.85M
            pi8_cost[0] =
2058
2059
1.85M
                ps_ctxt->s_cmn_opt_func.pf_ssd_and_sad_calculator(
2060
1.85M
                    pu1_pred, pred_strd, pu1_src, src_strd, trans_size, pu4_blk_sad);
2061
2062
1.85M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
2063
1.85M
            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
2064
0
            {
2065
0
                pi8_cost[0] = ihevce_inject_stim_into_distortion(
2066
0
                    pu1_src,
2067
0
                    src_strd,
2068
0
                    pu1_pred,
2069
0
                    pred_strd,
2070
0
                    pi8_cost[0],
2071
0
                    !ps_ctxt->u1_is_refPic ? ALPHA_FOR_ZERO_CODING_DECISIONS
2072
0
                                           : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
2073
0
                                              (double)ALPHA_FOR_ZERO_CODING_DECISIONS) /
2074
0
                                                 100.0,
2075
0
                    trans_size,
2076
0
                    0,
2077
0
                    ps_ctxt->u1_enable_psyRDOPT,
2078
0
                    NULL_PLANE);
2079
0
            }
2080
1.85M
#endif
2081
2082
            /* copy pred to recon for skip mode */
2083
1.85M
            if(SPATIAL_DOMAIN_SSD == e_ssd_type)
2084
743k
            {
2085
743k
                ps_ctxt->s_cmn_opt_func.pf_copy_2d(
2086
743k
                    pu1_recon, i4_recon_stride, pu1_pred, pred_strd, trans_size, trans_size);
2087
743k
                pu1_is_recon_available[0] = 1;
2088
743k
            }
2089
1.11M
            else
2090
1.11M
            {
2091
1.11M
                pu1_is_recon_available[0] = 0;
2092
1.11M
            }
2093
2094
1.85M
#if ENABLE_INTER_ZCU_COST
2095
1.85M
            ps_ctxt->i8_cu_not_coded_cost += pi8_cost[0];
2096
1.85M
#endif
2097
1.85M
        }
2098
0
        else
2099
0
        {
2100
0
            pi8_cost[0] = UINT_MAX;
2101
0
        }
2102
2103
        /* cbf is returned as 0 */
2104
1.85M
        return (0);
2105
1.85M
    }
2106
2107
    /* derive context variables */
2108
36.1M
    pi4_trans_scratch = (WORD32 *)&ps_ctxt->ai2_scratch[0];
2109
36.1M
    pi2_quant_coeffs = &ps_ctxt->ai2_scratch[0];
2110
36.1M
    pi2_trans_values = &ps_ctxt->ai2_scratch[0] + (MAX_TRANS_SIZE * 2);
2111
2112
    /* translate the transform size to index for 4x4 and 8x8 */
2113
36.1M
    trans_idx = trans_size >> 2;
2114
2115
36.1M
    if(PRED_MODE_INTRA == packed_pred_mode)
2116
29.3M
    {
2117
29.3M
        quant_scale_mat_offset = 0;
2118
29.3M
        intra_flag = 1;
2119
#if PROHIBIT_INTRA_QUANT_ROUNDING_FACTOR_TO_DROP_BELOW_1BY3
2120
        ai4_quant_rounding_factors[0][0] =
2121
            MAX(ps_ctxt->i4_quant_rnd_factor[intra_flag], (1 << QUANT_ROUND_FACTOR_Q) / 3);
2122
2123
        for(i = 0; i < trans_size * trans_size; i++)
2124
        {
2125
            ai4_quant_rounding_factors[1][i] =
2126
                MAX(ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3][i],
2127
                    (1 << QUANT_ROUND_FACTOR_Q) / 3);
2128
            ai4_quant_rounding_factors[2][i] =
2129
                MAX(ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3][i],
2130
                    (1 << QUANT_ROUND_FACTOR_Q) / 3);
2131
        }
2132
#endif
2133
29.3M
    }
2134
6.80M
    else
2135
6.80M
    {
2136
6.80M
        quant_scale_mat_offset = NUM_TRANS_TYPES;
2137
6.80M
    }
2138
    /* for intra 4x4 DST transform should be used */
2139
36.1M
    if((1 == trans_idx) && (1 == intra_flag))
2140
15.0M
    {
2141
15.0M
        trans_idx = 0;
2142
15.0M
    }
2143
    /* for 16x16 cases */
2144
21.1M
    else if(16 == trans_size)
2145
6.94M
    {
2146
6.94M
        trans_idx = 3;
2147
6.94M
    }
2148
    /* for 32x32 cases */
2149
14.1M
    else if(32 == trans_size)
2150
2.54M
    {
2151
2.54M
        trans_idx = 4;
2152
2.54M
    }
2153
2154
36.1M
    switch(trans_size)
2155
36.1M
    {
2156
17.1M
    case 4:
2157
17.1M
    {
2158
17.1M
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map4x4TU;
2159
2160
17.1M
        break;
2161
0
    }
2162
9.48M
    case 8:
2163
9.48M
    {
2164
9.48M
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map8x8TU;
2165
2166
9.48M
        break;
2167
0
    }
2168
6.94M
    case 16:
2169
6.94M
    {
2170
6.94M
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map16x16TU;
2171
2172
6.94M
        break;
2173
0
    }
2174
2.54M
    case 32:
2175
2.54M
    {
2176
2.54M
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map32x32TU;
2177
2178
2.54M
        break;
2179
0
    }
2180
36.1M
    }
2181
2182
    /* Do not call the FT and Quant functions if early_cbf is 0 */
2183
36.1M
    if(1 == early_cbf)
2184
36.1M
    {
2185
        /* ---------- call residue and transform block ------- */
2186
36.1M
        *pu4_blk_sad = ps_ctxt->apf_resd_trns[trans_idx](
2187
36.1M
            pu1_src,
2188
36.1M
            pu1_pred,
2189
36.1M
            pi4_trans_scratch,
2190
36.1M
            pi2_trans_values,
2191
36.1M
            src_strd,
2192
36.1M
            pred_strd,
2193
36.1M
            trans_size,
2194
36.1M
            NULL_PLANE);
2195
2196
36.1M
        cbf = ps_ctxt->apf_quant_iquant_ssd
2197
36.1M
                  [i4_perform_coeff_level_rdoq + (e_ssd_type != FREQUENCY_DOMAIN_SSD) * 2](
2198
36.1M
                      pi2_trans_values,
2199
36.1M
                      ps_ctxt->api2_rescal_mat[trans_idx + quant_scale_mat_offset],
2200
36.1M
                      pi2_quant_coeffs,
2201
36.1M
                      pi2_deq_data,
2202
36.1M
                      trans_size,
2203
36.1M
                      ps_ctxt->i4_cu_qp_div6,
2204
36.1M
                      ps_ctxt->i4_cu_qp_mod6,
2205
36.1M
#if !PROHIBIT_INTRA_QUANT_ROUNDING_FACTOR_TO_DROP_BELOW_1BY3
2206
36.1M
                      ps_ctxt->i4_quant_rnd_factor[intra_flag],
2207
36.1M
                      ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3],
2208
36.1M
                      ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3],
2209
#else
2210
                      intra_flag ? ai4_quant_rounding_factors[0][0]
2211
                                 : ps_ctxt->i4_quant_rnd_factor[intra_flag],
2212
                      intra_flag ? ai4_quant_rounding_factors[1]
2213
                                 : ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3],
2214
                      intra_flag ? ai4_quant_rounding_factors[2]
2215
                                 : ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3],
2216
#endif
2217
36.1M
                      trans_size,
2218
36.1M
                      trans_size,
2219
36.1M
                      deq_data_strd,
2220
36.1M
                      pu1_csbf_buf,
2221
36.1M
                      csbf_strd,
2222
36.1M
                      pi4_zero_col,
2223
36.1M
                      pi4_zero_row,
2224
36.1M
                      ps_ctxt->api2_scal_mat[trans_idx + quant_scale_mat_offset],
2225
36.1M
                      pi8_cost);
2226
2227
36.1M
        if(e_ssd_type != FREQUENCY_DOMAIN_SSD)
2228
14.9M
        {
2229
14.9M
            pi8_cost[0] = UINT_MAX;
2230
14.9M
        }
2231
36.1M
    }
2232
2233
36.1M
    if(0 != cbf)
2234
12.7M
    {
2235
12.7M
        if(i4_perform_sbh || i4_perform_rdoq)
2236
9.58M
        {
2237
9.58M
            ps_rdoq_sbh_ctxt->i4_iq_data_strd = deq_data_strd;
2238
9.58M
            ps_rdoq_sbh_ctxt->i4_q_data_strd = trans_size;
2239
9.58M
            ps_rdoq_sbh_ctxt->pi4_subBlock2csbfId_map = pi4_subBlock2csbfId_map;
2240
2241
9.58M
            ps_rdoq_sbh_ctxt->i4_qp_div = ps_ctxt->i4_cu_qp_div6;
2242
9.58M
            ps_rdoq_sbh_ctxt->i2_qp_rem = ps_ctxt->i4_cu_qp_mod6;
2243
9.58M
            ps_rdoq_sbh_ctxt->i4_scan_idx = ps_ctxt->i4_scan_idx;
2244
9.58M
            ps_rdoq_sbh_ctxt->i8_ssd_cost = *pi8_cost;
2245
9.58M
            ps_rdoq_sbh_ctxt->i4_trans_size = trans_size;
2246
2247
9.58M
            ps_rdoq_sbh_ctxt->pi2_dequant_coeff =
2248
9.58M
                ps_ctxt->api2_scal_mat[trans_idx + quant_scale_mat_offset];
2249
9.58M
            ps_rdoq_sbh_ctxt->pi2_iquant_coeffs = pi2_deq_data;
2250
9.58M
            ps_rdoq_sbh_ctxt->pi2_quant_coeffs = pi2_quant_coeffs;
2251
9.58M
            ps_rdoq_sbh_ctxt->pi2_trans_values = pi2_trans_values;
2252
9.58M
            ps_rdoq_sbh_ctxt->pu1_csbf_buf = pu1_csbf_buf;
2253
2254
            /* ------- call coeffs scan function ------- */
2255
9.58M
            if((!i4_perform_rdoq))
2256
5.05M
            {
2257
5.05M
                ihevce_sign_data_hiding(ps_rdoq_sbh_ctxt);
2258
2259
5.05M
                pi8_cost[0] = ps_rdoq_sbh_ctxt->i8_ssd_cost;
2260
5.05M
            }
2261
9.58M
        }
2262
2263
12.7M
        *pi4_coeff_off = ps_ctxt->s_cmn_opt_func.pf_scan_coeffs(
2264
12.7M
            pi2_quant_coeffs,
2265
12.7M
            pi4_subBlock2csbfId_map,
2266
12.7M
            ps_ctxt->i4_scan_idx,
2267
12.7M
            trans_size,
2268
12.7M
            pu1_ecd_data,
2269
12.7M
            pu1_csbf_buf,
2270
12.7M
            csbf_strd);
2271
12.7M
    }
2272
36.1M
    *pi8_cost >>= ga_trans_shift[trans_idx];
2273
2274
36.1M
#if RDOPT_ZERO_CBF_ENABLE
2275
    /* compare null cbf cost with encode tu rd-cost */
2276
36.1M
    if(cbf != 0)
2277
12.7M
    {
2278
12.7M
        WORD32 tu_bits;
2279
12.7M
        LWORD64 tu_rd_cost;
2280
2281
12.7M
        LWORD64 zero_cbf_cost = 0;
2282
2283
        /*Populating the feilds of rdoq_ctxt structure*/
2284
12.7M
        if(i4_perform_rdoq)
2285
4.53M
        {
2286
            /* transform size to log2transform size */
2287
4.53M
            GETRANGE(ps_rdoq_sbh_ctxt->i4_log2_trans_size, trans_size);
2288
4.53M
            ps_rdoq_sbh_ctxt->i4_log2_trans_size -= 1;
2289
4.53M
            ps_rdoq_sbh_ctxt->i8_cl_ssd_lambda_qf = ps_ctxt->i8_cl_ssd_lambda_qf;
2290
4.53M
            ps_rdoq_sbh_ctxt->i4_is_luma = 1;
2291
4.53M
            ps_rdoq_sbh_ctxt->i4_shift_val_ssd_in_td = ga_trans_shift[trans_idx];
2292
4.53M
            ps_rdoq_sbh_ctxt->i4_round_val_ssd_in_td =
2293
4.53M
                (1 << ps_rdoq_sbh_ctxt->i4_shift_val_ssd_in_td) / 2;
2294
4.53M
            ps_rdoq_sbh_ctxt->i1_tu_is_coded = 0;
2295
4.53M
            ps_rdoq_sbh_ctxt->pi4_zero_col = pi4_zero_col;
2296
4.53M
            ps_rdoq_sbh_ctxt->pi4_zero_row = pi4_zero_row;
2297
4.53M
        }
2298
8.18M
        else if(i4_perform_zcbf)
2299
1.46M
        {
2300
1.46M
            zero_cbf_cost =
2301
2302
1.46M
                ps_ctxt->s_cmn_opt_func.pf_ssd_calculator(
2303
1.46M
                    pu1_src, pu1_pred, src_strd, pred_strd, trans_size, trans_size, NULL_PLANE);
2304
1.46M
        }
2305
2306
        /************************************************************************/
2307
        /* call the entropy rdo encode to get the bit estimate for current tu   */
2308
        /* note that tu includes only residual coding bits and does not include */
2309
        /* tu split, cbf and qp delta encoding bits for a TU                    */
2310
        /************************************************************************/
2311
12.7M
        if(i4_perform_rdoq)
2312
4.53M
        {
2313
4.53M
            tu_bits = ihevce_entropy_rdo_encode_tu_rdoq(
2314
4.53M
                &ps_ctxt->s_rdopt_entropy_ctxt,
2315
4.53M
                (pu1_ecd_data),
2316
4.53M
                trans_size,
2317
4.53M
                1,
2318
4.53M
                ps_rdoq_sbh_ctxt,
2319
4.53M
                pi8_cost,
2320
4.53M
                &zero_cbf_cost,
2321
4.53M
                0);
2322
2323
4.53M
            if(ps_rdoq_sbh_ctxt->i1_tu_is_coded == 0)
2324
156k
            {
2325
156k
                cbf = 0;
2326
156k
                *pi4_coeff_off = 0;
2327
156k
            }
2328
2329
4.53M
            if((i4_perform_sbh) && (0 != cbf))
2330
4.37M
            {
2331
4.37M
                ps_rdoq_sbh_ctxt->i8_ssd_cost = *pi8_cost;
2332
4.37M
                ihevce_sign_data_hiding(ps_rdoq_sbh_ctxt);
2333
4.37M
                *pi8_cost = ps_rdoq_sbh_ctxt->i8_ssd_cost;
2334
4.37M
            }
2335
2336
            /*Add round value before normalizing*/
2337
4.53M
            *pi8_cost += ps_rdoq_sbh_ctxt->i4_round_val_ssd_in_td;
2338
4.53M
            *pi8_cost >>= ga_trans_shift[trans_idx];
2339
2340
4.53M
            if(ps_rdoq_sbh_ctxt->i1_tu_is_coded == 1)
2341
4.37M
            {
2342
4.37M
                pi2_quant_coeffs = &ps_ctxt->ai2_scratch[0];
2343
4.37M
                *pi4_coeff_off = ps_ctxt->s_cmn_opt_func.pf_scan_coeffs(
2344
4.37M
                    pi2_quant_coeffs,
2345
4.37M
                    pi4_subBlock2csbfId_map,
2346
4.37M
                    ps_ctxt->i4_scan_idx,
2347
4.37M
                    trans_size,
2348
4.37M
                    pu1_ecd_data,
2349
4.37M
                    pu1_csbf_buf,
2350
4.37M
                    csbf_strd);
2351
4.37M
            }
2352
4.53M
        }
2353
8.18M
        else
2354
8.18M
        {
2355
8.18M
            tu_bits = ihevce_entropy_rdo_encode_tu(
2356
8.18M
                &ps_ctxt->s_rdopt_entropy_ctxt, pu1_ecd_data, trans_size, 1, i4_perform_sbh);
2357
8.18M
        }
2358
2359
12.7M
        *pi4_tu_bits = tu_bits;
2360
2361
12.7M
        if(e_ssd_type == SPATIAL_DOMAIN_SSD)
2362
3.27M
        {
2363
3.27M
            *pi8_cost = ihevce_it_recon_ssd(
2364
3.27M
                ps_ctxt,
2365
3.27M
                pu1_src,
2366
3.27M
                src_strd,
2367
3.27M
                pu1_pred,
2368
3.27M
                pred_strd,
2369
3.27M
                pi2_deq_data,
2370
3.27M
                deq_data_strd,
2371
3.27M
                pu1_recon,
2372
3.27M
                i4_recon_stride,
2373
3.27M
                pu1_ecd_data,
2374
3.27M
                trans_size,
2375
3.27M
                packed_pred_mode,
2376
3.27M
                cbf,
2377
3.27M
                *pi4_zero_col,
2378
3.27M
                *pi4_zero_row,
2379
3.27M
                NULL_PLANE);
2380
2381
3.27M
            pu1_is_recon_available[0] = 1;
2382
3.27M
        }
2383
2384
12.7M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
2385
12.7M
        if(u1_is_cu_noisy && (e_ssd_type == SPATIAL_DOMAIN_SSD) && i4_alpha_stim_multiplier)
2386
0
        {
2387
0
            pi8_cost[0] = ihevce_inject_stim_into_distortion(
2388
0
                pu1_src,
2389
0
                src_strd,
2390
0
                pu1_recon,
2391
0
                i4_recon_stride,
2392
0
                pi8_cost[0],
2393
0
                i4_alpha_stim_multiplier,
2394
0
                trans_size,
2395
0
                0,
2396
0
                ps_ctxt->u1_enable_psyRDOPT,
2397
0
                NULL_PLANE);
2398
0
        }
2399
12.7M
        else if(u1_is_cu_noisy && (e_ssd_type == FREQUENCY_DOMAIN_SSD) && i4_alpha_stim_multiplier)
2400
0
        {
2401
0
            pi8_cost[0] = ihevce_inject_stim_into_distortion(
2402
0
                pu1_src,
2403
0
                src_strd,
2404
0
                pu1_pred,
2405
0
                pred_strd,
2406
0
                pi8_cost[0],
2407
0
                i4_alpha_stim_multiplier,
2408
0
                trans_size,
2409
0
                0,
2410
0
                ps_ctxt->u1_enable_psyRDOPT,
2411
0
                NULL_PLANE);
2412
0
        }
2413
12.7M
#endif
2414
2415
        /* add the SSD cost to bits estimate given by ECD */
2416
12.7M
        tu_rd_cost = *pi8_cost + COMPUTE_RATE_COST_CLIP30(
2417
12.7M
                                     tu_bits, ps_ctxt->i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
2418
2419
12.7M
        if(i4_perform_zcbf)
2420
2.32M
        {
2421
2.32M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
2422
2.32M
            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
2423
0
            {
2424
0
                zero_cbf_cost = ihevce_inject_stim_into_distortion(
2425
0
                    pu1_src,
2426
0
                    src_strd,
2427
0
                    pu1_pred,
2428
0
                    pred_strd,
2429
0
                    zero_cbf_cost,
2430
0
                    !ps_ctxt->u1_is_refPic ? ALPHA_FOR_ZERO_CODING_DECISIONS
2431
0
                                           : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
2432
0
                                              (double)ALPHA_FOR_ZERO_CODING_DECISIONS) /
2433
0
                                                 100.0,
2434
0
                    trans_size,
2435
0
                    0,
2436
0
                    ps_ctxt->u1_enable_psyRDOPT,
2437
0
                    NULL_PLANE);
2438
0
            }
2439
2.32M
#endif
2440
2441
            /* force the tu as zero cbf if zero_cbf_cost is lower */
2442
2.32M
            if(zero_cbf_cost < tu_rd_cost)
2443
87.0k
            {
2444
                /* num bytes is set to 0 */
2445
87.0k
                *pi4_coeff_off = 0;
2446
2447
                /* cbf is returned as 0 */
2448
87.0k
                cbf = 0;
2449
2450
                /* cost is returned as 0 cbf cost */
2451
87.0k
                *pi8_cost = zero_cbf_cost;
2452
2453
                /* TU bits is set to 0 */
2454
87.0k
                *pi4_tu_bits = 0;
2455
87.0k
                pu1_is_recon_available[0] = 0;
2456
2457
87.0k
                if(SPATIAL_DOMAIN_SSD == e_ssd_type)
2458
8.64k
                {
2459
                    /* copy pred to recon for zcbf mode */
2460
2461
8.64k
                    ps_ctxt->s_cmn_opt_func.pf_copy_2d(
2462
8.64k
                        pu1_recon, i4_recon_stride, pu1_pred, pred_strd, trans_size, trans_size);
2463
2464
8.64k
                    pu1_is_recon_available[0] = 1;
2465
8.64k
                }
2466
87.0k
            }
2467
            /* accumulate cu not coded cost with zcbf cost */
2468
2.32M
#if ENABLE_INTER_ZCU_COST
2469
2.32M
            ps_ctxt->i8_cu_not_coded_cost += zero_cbf_cost;
2470
2.32M
#endif
2471
2.32M
        }
2472
12.7M
    }
2473
23.4M
    else
2474
23.4M
    {
2475
        /* cbf = 0, accumulate cu not coded cost */
2476
23.4M
        if(e_ssd_type == SPATIAL_DOMAIN_SSD)
2477
11.6M
        {
2478
11.6M
            *pi8_cost = ihevce_it_recon_ssd(
2479
11.6M
                ps_ctxt,
2480
11.6M
                pu1_src,
2481
11.6M
                src_strd,
2482
11.6M
                pu1_pred,
2483
11.6M
                pred_strd,
2484
11.6M
                pi2_deq_data,
2485
11.6M
                deq_data_strd,
2486
11.6M
                pu1_recon,
2487
11.6M
                i4_recon_stride,
2488
11.6M
                pu1_ecd_data,
2489
11.6M
                trans_size,
2490
11.6M
                packed_pred_mode,
2491
11.6M
                cbf,
2492
11.6M
                *pi4_zero_col,
2493
11.6M
                *pi4_zero_row,
2494
11.6M
                NULL_PLANE);
2495
2496
11.6M
            pu1_is_recon_available[0] = 1;
2497
11.6M
        }
2498
2499
23.4M
#if ENABLE_INTER_ZCU_COST
2500
23.4M
        {
2501
23.4M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
2502
23.4M
            if(u1_is_cu_noisy && (e_ssd_type == SPATIAL_DOMAIN_SSD) && i4_alpha_stim_multiplier)
2503
0
            {
2504
0
                pi8_cost[0] = ihevce_inject_stim_into_distortion(
2505
0
                    pu1_src,
2506
0
                    src_strd,
2507
0
                    pu1_recon,
2508
0
                    i4_recon_stride,
2509
0
                    pi8_cost[0],
2510
0
                    !ps_ctxt->u1_is_refPic ? ALPHA_FOR_ZERO_CODING_DECISIONS
2511
0
                                           : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
2512
0
                                              (double)ALPHA_FOR_ZERO_CODING_DECISIONS) /
2513
0
                                                 100.0,
2514
0
                    trans_size,
2515
0
                    0,
2516
0
                    ps_ctxt->u1_enable_psyRDOPT,
2517
0
                    NULL_PLANE);
2518
0
            }
2519
23.4M
            else if(u1_is_cu_noisy && (e_ssd_type == FREQUENCY_DOMAIN_SSD) && i4_alpha_stim_multiplier)
2520
0
            {
2521
0
                pi8_cost[0] = ihevce_inject_stim_into_distortion(
2522
0
                    pu1_src,
2523
0
                    src_strd,
2524
0
                    pu1_pred,
2525
0
                    pred_strd,
2526
0
                    pi8_cost[0],
2527
0
                    !ps_ctxt->u1_is_refPic ? ALPHA_FOR_ZERO_CODING_DECISIONS
2528
0
                                           : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
2529
0
                                              (double)ALPHA_FOR_ZERO_CODING_DECISIONS) /
2530
0
                                                 100.0,
2531
0
                    trans_size,
2532
0
                    0,
2533
0
                    ps_ctxt->u1_enable_psyRDOPT,
2534
0
                    NULL_PLANE);
2535
0
            }
2536
23.4M
#endif
2537
2538
23.4M
            ps_ctxt->i8_cu_not_coded_cost += *pi8_cost;
2539
23.4M
        }
2540
23.4M
#endif /* ENABLE_INTER_ZCU_COST */
2541
23.4M
    }
2542
36.1M
#endif
2543
2544
36.1M
    return (cbf);
2545
36.1M
}
2546
2547
/*!
2548
******************************************************************************
2549
* \if Function name : ihevce_it_recon_fxn \endif
2550
*
2551
* \brief
2552
*    Transform unit level (Luma) IT Recon function
2553
*
2554
* \param[in] ps_ctxt        enc_loop module ctxt pointer
2555
* \param[in] pi2_deq_data   pointer to iq data
2556
* \param[in] deq_data_strd  iq data buffer stride
2557
* \param[in] pu1_pred       pointer to predicted data buffer
2558
* \param[in] pred_strd      predicted buffer stride
2559
* \param[in] pu1_recon      pointer to recon buffer
2560
* \param[in] recon_strd     recon buffer stride
2561
* \param[out] pu1_ecd_data  pointer coeff output buffer (input to ent cod)
2562
* \param[in] trans_size     transform size (4, 8, 16,32)
2563
* \param[in] packed_pred_mode   0:Inter 1:Intra 2:Skip
2564
* \param[in] cbf            CBF of the current block
2565
* \param[in] zero_cols      zero_cols of the current block
2566
* \param[in] zero_rows      zero_rows of the current block
2567
*
2568
* \return
2569
*
2570
* \author
2571
*  Ittiam
2572
*
2573
*****************************************************************************
2574
*/
2575
2576
void ihevce_it_recon_fxn(
2577
    ihevce_enc_loop_ctxt_t *ps_ctxt,
2578
    WORD16 *pi2_deq_data,
2579
    WORD32 deq_dat_strd,
2580
    UWORD8 *pu1_pred,
2581
    WORD32 pred_strd,
2582
    UWORD8 *pu1_recon,
2583
    WORD32 recon_strd,
2584
    UWORD8 *pu1_ecd_data,
2585
    WORD32 trans_size,
2586
    WORD32 packed_pred_mode,
2587
    WORD32 cbf,
2588
    WORD32 zero_cols,
2589
    WORD32 zero_rows)
2590
23.5M
{
2591
23.5M
    WORD32 dc_add_flag = 0;
2592
23.5M
    WORD32 trans_idx;
2593
2594
    /* translate the transform size to index for 4x4 and 8x8 */
2595
23.5M
    trans_idx = trans_size >> 2;
2596
2597
    /* if SKIP mode needs to be evaluated the pred is copied to recon */
2598
23.5M
    if(PRED_MODE_SKIP == packed_pred_mode)
2599
292k
    {
2600
292k
        UWORD8 *pu1_curr_recon, *pu1_curr_pred;
2601
2602
292k
        pu1_curr_pred = pu1_pred;
2603
292k
        pu1_curr_recon = pu1_recon;
2604
2605
        /* 2D copy of data */
2606
2607
292k
        ps_ctxt->s_cmn_opt_func.pf_2d_square_copy(
2608
292k
            pu1_curr_recon, recon_strd, pu1_curr_pred, pred_strd, trans_size, sizeof(UWORD8));
2609
2610
292k
        return;
2611
292k
    }
2612
2613
    /* for intra 4x4 DST transform should be used */
2614
23.2M
    if((1 == trans_idx) && (PRED_MODE_INTRA == packed_pred_mode))
2615
8.14M
    {
2616
8.14M
        trans_idx = 0;
2617
8.14M
    }
2618
    /* for 16x16 cases */
2619
15.0M
    else if(16 == trans_size)
2620
5.02M
    {
2621
5.02M
        trans_idx = 3;
2622
5.02M
    }
2623
    /* for 32x32 cases */
2624
10.0M
    else if(32 == trans_size)
2625
1.87M
    {
2626
1.87M
        trans_idx = 4;
2627
1.87M
    }
2628
2629
    /*if (lastx == 0 && lasty == 0) , ie only 1 coefficient */
2630
23.2M
    if((0 == pu1_ecd_data[0]) && (0 == pu1_ecd_data[1]))
2631
3.92M
    {
2632
3.92M
        dc_add_flag = 1;
2633
3.92M
    }
2634
2635
23.2M
    if(0 == cbf)
2636
17.3M
    {
2637
        /* buffer copy */
2638
17.3M
        ps_ctxt->s_cmn_opt_func.pf_2d_square_copy(
2639
17.3M
            pu1_recon, recon_strd, pu1_pred, pred_strd, trans_size, 1);
2640
17.3M
    }
2641
5.84M
    else if((1 == dc_add_flag) && (0 != trans_idx))
2642
172k
    {
2643
        /* dc add */
2644
172k
        ps_ctxt->s_cmn_opt_func.pf_itrans_recon_dc(
2645
172k
            pu1_pred,
2646
172k
            pred_strd,
2647
172k
            pu1_recon,
2648
172k
            recon_strd,
2649
172k
            trans_size,
2650
172k
            pi2_deq_data[0],
2651
172k
            NULL_PLANE /* luma */
2652
172k
        );
2653
172k
    }
2654
5.66M
    else
2655
5.66M
    {
2656
5.66M
        ps_ctxt->apf_it_recon[trans_idx](
2657
5.66M
            pi2_deq_data,
2658
5.66M
            &ps_ctxt->ai2_scratch[0],
2659
5.66M
            pu1_pred,
2660
5.66M
            pu1_recon,
2661
5.66M
            deq_dat_strd,
2662
5.66M
            pred_strd,
2663
5.66M
            recon_strd,
2664
5.66M
            zero_cols,
2665
5.66M
            zero_rows);
2666
5.66M
    }
2667
23.2M
}
2668
2669
/*!
2670
******************************************************************************
2671
* \if Function name : ihevce_chroma_it_recon_fxn \endif
2672
*
2673
* \brief
2674
*    Transform unit level (Chroma) IT Recon function
2675
*
2676
* \param[in] ps_ctxt        enc_loop module ctxt pointer
2677
* \param[in] pi2_deq_data   pointer to iq data
2678
* \param[in] deq_data_strd  iq data buffer stride
2679
* \param[in] pu1_pred       pointer to predicted data buffer
2680
* \param[in] pred_strd      predicted buffer stride
2681
* \param[in] pu1_recon      pointer to recon buffer
2682
* \param[in] recon_strd     recon buffer stride
2683
* \param[out] pu1_ecd_data  pointer coeff output buffer (input to ent cod)
2684
* \param[in] trans_size     transform size (4, 8, 16)
2685
* \param[in] cbf            CBF of the current block
2686
* \param[in] zero_cols      zero_cols of the current block
2687
* \param[in] zero_rows      zero_rows of the current block
2688
*
2689
* \return
2690
*
2691
* \author
2692
*  Ittiam
2693
*
2694
*****************************************************************************
2695
*/
2696
2697
void ihevce_chroma_it_recon_fxn(
2698
    ihevce_enc_loop_ctxt_t *ps_ctxt,
2699
    WORD16 *pi2_deq_data,
2700
    WORD32 deq_dat_strd,
2701
    UWORD8 *pu1_pred,
2702
    WORD32 pred_strd,
2703
    UWORD8 *pu1_recon,
2704
    WORD32 recon_strd,
2705
    UWORD8 *pu1_ecd_data,
2706
    WORD32 trans_size,
2707
    WORD32 cbf,
2708
    WORD32 zero_cols,
2709
    WORD32 zero_rows,
2710
    CHROMA_PLANE_ID_T e_chroma_plane)
2711
29.1M
{
2712
29.1M
    WORD32 trans_idx;
2713
2714
29.1M
    ASSERT((e_chroma_plane == U_PLANE) || (e_chroma_plane == V_PLANE));
2715
2716
    /* since 2x2 transform is not allowed for chroma*/
2717
29.1M
    if(2 == trans_size)
2718
0
    {
2719
0
        trans_size = 4;
2720
0
    }
2721
2722
    /* translate the transform size to index */
2723
29.1M
    trans_idx = trans_size >> 2;
2724
2725
    /* for 16x16 cases */
2726
29.1M
    if(16 == trans_size)
2727
4.00M
    {
2728
4.00M
        trans_idx = 3;
2729
4.00M
    }
2730
2731
29.1M
    if(0 == cbf)
2732
25.7M
    {
2733
        /* buffer copy */
2734
25.7M
        ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
2735
25.7M
            pu1_pred, pred_strd, pu1_recon, recon_strd, trans_size, trans_size, e_chroma_plane);
2736
25.7M
    }
2737
3.43M
    else if((0 == pu1_ecd_data[0]) && (0 == pu1_ecd_data[1]))
2738
287k
    {
2739
        /* dc add */
2740
287k
        ps_ctxt->s_cmn_opt_func.pf_itrans_recon_dc(
2741
287k
            pu1_pred,
2742
287k
            pred_strd,
2743
287k
            pu1_recon,
2744
287k
            recon_strd,
2745
287k
            trans_size,
2746
287k
            pi2_deq_data[0],
2747
287k
            e_chroma_plane /* chroma plane */
2748
287k
        );
2749
287k
    }
2750
3.14M
    else
2751
3.14M
    {
2752
3.14M
        ps_ctxt->apf_chrm_it_recon[trans_idx - 1](
2753
3.14M
            pi2_deq_data,
2754
3.14M
            &ps_ctxt->ai2_scratch[0],
2755
3.14M
            pu1_pred + (WORD32)e_chroma_plane,
2756
3.14M
            pu1_recon + (WORD32)e_chroma_plane,
2757
3.14M
            deq_dat_strd,
2758
3.14M
            pred_strd,
2759
3.14M
            recon_strd,
2760
3.14M
            zero_cols,
2761
3.14M
            zero_rows);
2762
3.14M
    }
2763
29.1M
}
2764
2765
/**
2766
*******************************************************************************
2767
* \if Function name : ihevce_mpm_idx_based_filter_RDOPT_cand \endif
2768
*
2769
* \brief * Filters the RDOPT candidates based on mpm_idx
2770
*
2771
* \par   Description
2772
* Computes the b1_prev_intra_luma_pred_flag, b2_mpm_idx & b5_rem_intra_pred_mode
2773
* for a CU
2774
*
2775
* \param[in] ps_ctxt : ptr to enc loop context
2776
* \param[in] ps_cu_analyse : ptr to CU analyse structure
2777
* \param[in] ps_top_nbr_4x4 top 4x4 neighbour pointer
2778
* \param[in] ps_left_nbr_4x4 left 4x4 neighbour pointer
2779
* \param[in] pu1_luma_mode luma mode
2780
*
2781
* \returns none
2782
*
2783
* \author
2784
*  Ittiam
2785
*
2786
*******************************************************************************
2787
*/
2788
2789
void ihevce_mpm_idx_based_filter_RDOPT_cand(
2790
    ihevce_enc_loop_ctxt_t *ps_ctxt,
2791
    cu_analyse_t *ps_cu_analyse,
2792
    nbr_4x4_t *ps_left_nbr_4x4,
2793
    nbr_4x4_t *ps_top_nbr_4x4,
2794
    UWORD8 *pu1_luma_mode,
2795
    UWORD8 *pu1_eval_mark)
2796
418k
{
2797
418k
    WORD32 cu_pos_x;
2798
418k
    WORD32 cu_pos_y;
2799
418k
    nbr_avail_flags_t s_nbr;
2800
418k
    WORD32 trans_size;
2801
418k
    WORD32 au4_cand_mode_list[3];
2802
418k
    WORD32 nbr_flags;
2803
418k
    UWORD8 *pu1_intra_luma_modes;
2804
418k
    WORD32 rdopt_cand_ctr = 0;
2805
418k
    UWORD8 *pu1_luma_eval_mark;
2806
2807
418k
    cu_pos_x = ps_cu_analyse->b3_cu_pos_x << 1;
2808
418k
    cu_pos_y = ps_cu_analyse->b3_cu_pos_y << 1;
2809
418k
    trans_size = ps_cu_analyse->u1_cu_size;
2810
2811
    /* get the neighbour availability flags */
2812
418k
    nbr_flags = ihevce_get_nbr_intra(
2813
418k
        &s_nbr,
2814
418k
        ps_ctxt->pu1_ctb_nbr_map,
2815
418k
        ps_ctxt->i4_nbr_map_strd,
2816
418k
        cu_pos_x,
2817
418k
        cu_pos_y,
2818
418k
        trans_size >> 2);
2819
418k
    (void)nbr_flags;
2820
    /*Call the fun to populate luma intra pred mode fro TU=CU and use the same list fro
2821
    *TU=CU/2 also since the modes are same in both the cases.
2822
    */
2823
418k
    ihevce_populate_intra_pred_mode(
2824
418k
        ps_top_nbr_4x4->b6_luma_intra_mode,
2825
418k
        ps_left_nbr_4x4->b6_luma_intra_mode,
2826
418k
        s_nbr.u1_top_avail,
2827
418k
        s_nbr.u1_left_avail,
2828
418k
        cu_pos_y,
2829
418k
        &au4_cand_mode_list[0]);
2830
2831
    /*Loop through all the RDOPT candidates of TU=CU and TU=CU/2 and check if the current RDOPT
2832
    *cand is present in a4_cand_mode_list, If yes set eval flag to 1 else set it to zero
2833
    */
2834
2835
418k
    pu1_intra_luma_modes = pu1_luma_mode;
2836
418k
    pu1_luma_eval_mark = pu1_eval_mark;
2837
2838
1.54M
    while(pu1_intra_luma_modes[rdopt_cand_ctr] != 255)
2839
1.12M
    {
2840
1.12M
        WORD32 i;
2841
1.12M
        WORD32 found_flag = 0;
2842
2843
        /*1st candidate of TU=CU list and TU=CU/2 list must go through RDOPT stage
2844
        *irrespective of whether the cand is present in the mpm idx list or not
2845
        */
2846
1.12M
        if(rdopt_cand_ctr == 0)
2847
387k
        {
2848
387k
            rdopt_cand_ctr++;
2849
387k
            continue;
2850
387k
        }
2851
2852
2.24M
        for(i = 0; i < 3; i++)
2853
1.85M
        {
2854
1.85M
            if(pu1_intra_luma_modes[rdopt_cand_ctr] == au4_cand_mode_list[i])
2855
349k
            {
2856
349k
                found_flag = 1;
2857
349k
                break;
2858
349k
            }
2859
1.85M
        }
2860
2861
735k
        if(found_flag == 0)
2862
385k
        {
2863
385k
            pu1_luma_eval_mark[rdopt_cand_ctr] = 0;
2864
385k
        }
2865
2866
735k
        rdopt_cand_ctr++;
2867
735k
    }
2868
418k
}
2869
2870
/*!
2871
******************************************************************************
2872
* \if Function name : ihevce_intra_rdopt_cu_ntu \endif
2873
*
2874
* \brief
2875
*    Intra Coding unit funtion for RD opt mode
2876
*
2877
* \param[in] ps_ctxt    enc_loop module ctxt pointer
2878
* \param[in] ps_chrm_cu_buf_prms pointer to chroma buffer pointers structure
2879
* \param[in] pu1_luma_mode : pointer to luma mode
2880
* \param[in] ps_cu_analyse  pointer to cu analyse pointer
2881
* \param[in] pu1_src    pointer to source data buffer
2882
* \param[in] src_strd   source buffer stride
2883
* \param[in] pu1_cu_left pointer to left recon data buffer
2884
* \param[in] pu1_cu_top  pointer to top recon data buffer
2885
* \param[in] pu1_cu_top_left pointer to top left recon data buffer
2886
* \param[in] ps_left_nbr_4x4 : left 4x4 neighbour pointer
2887
* \param[in] ps_top_nbr_4x4 : top 4x4 neighbour pointer
2888
* \param[in] nbr_4x4_left_strd left nbr4x4 stride
2889
* \param[in] cu_left_stride left recon buffer stride
2890
* \param[in] curr_buf_idx RD opt buffer index for current usage
2891
* \param[in] func_proc_mode : function procesing mode @sa TU_SIZE_WRT_CU_T
2892
*
2893
* \return
2894
*    RDopt cost
2895
*
2896
* \author
2897
*  Ittiam
2898
*
2899
*****************************************************************************
2900
*/
2901
LWORD64 ihevce_intra_rdopt_cu_ntu(
2902
    ihevce_enc_loop_ctxt_t *ps_ctxt,
2903
    enc_loop_cu_prms_t *ps_cu_prms,
2904
    void *pv_pred_org,
2905
    WORD32 pred_strd_org,
2906
    enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
2907
    UWORD8 *pu1_luma_mode,
2908
    cu_analyse_t *ps_cu_analyse,
2909
    void *pv_curr_src,
2910
    void *pv_cu_left,
2911
    void *pv_cu_top,
2912
    void *pv_cu_top_left,
2913
    nbr_4x4_t *ps_left_nbr_4x4,
2914
    nbr_4x4_t *ps_top_nbr_4x4,
2915
    WORD32 nbr_4x4_left_strd,
2916
    WORD32 cu_left_stride,
2917
    WORD32 curr_buf_idx,
2918
    WORD32 func_proc_mode,
2919
    WORD32 i4_alpha_stim_multiplier)
2920
10.1M
{
2921
10.1M
    enc_loop_cu_final_prms_t *ps_final_prms;
2922
10.1M
    nbr_avail_flags_t s_nbr;
2923
10.1M
    nbr_4x4_t *ps_nbr_4x4;
2924
10.1M
    nbr_4x4_t *ps_tmp_lt_4x4;
2925
10.1M
    recon_datastore_t *ps_recon_datastore;
2926
2927
10.1M
    ihevc_intra_pred_luma_ref_substitution_ft *ihevc_intra_pred_luma_ref_substitution_fptr;
2928
2929
10.1M
    UWORD32 *pu4_nbr_flags;
2930
10.1M
    UWORD8 *pu1_intra_pred_mode;
2931
10.1M
    WORD32 cu_pos_x;
2932
10.1M
    WORD32 cu_pos_y;
2933
10.1M
    WORD32 trans_size = 0;
2934
10.1M
    UWORD8 *pu1_left;
2935
10.1M
    UWORD8 *pu1_top;
2936
10.1M
    UWORD8 *pu1_top_left;
2937
10.1M
    UWORD8 *pu1_recon;
2938
10.1M
    UWORD8 *pu1_csbf_buf;
2939
10.1M
    UWORD8 *pu1_ecd_data;
2940
10.1M
    WORD16 *pi2_deq_data;
2941
10.1M
    WORD32 deq_data_strd;
2942
10.1M
    LWORD64 total_rdopt_cost;
2943
10.1M
    WORD32 ctr;
2944
10.1M
    WORD32 left_strd;
2945
10.1M
    WORD32 i4_recon_stride;
2946
10.1M
    WORD32 csbf_strd;
2947
10.1M
    WORD32 ecd_data_bytes_cons;
2948
10.1M
    WORD32 num_4x4_in_tu;
2949
10.1M
    WORD32 num_4x4_in_cu;
2950
10.1M
    WORD32 chrm_present_flag;
2951
10.1M
    WORD32 tx_size;
2952
10.1M
    WORD32 cu_bits;
2953
10.1M
    WORD32 num_cu_parts = 0;
2954
10.1M
    WORD32 num_cands = 0;
2955
10.1M
    WORD32 cu_pos_x_8pelunits;
2956
10.1M
    WORD32 cu_pos_y_8pelunits;
2957
10.1M
    WORD32 i4_perform_rdoq;
2958
10.1M
    WORD32 i4_perform_sbh;
2959
10.1M
    UWORD8 u1_compute_spatial_ssd;
2960
10.1M
    UWORD8 u1_compute_recon;
2961
10.1M
    UWORD8 au1_intra_nxn_rdopt_ctxt_models[2][IHEVC_CAB_CTXT_END];
2962
2963
10.1M
    UWORD16 u2_num_tus_in_cu = 0;
2964
10.1M
    WORD32 is_sub_pu_in_hq = 0;
2965
    /* Get the RDOPT cost of the best CU mode for early_exit */
2966
10.1M
    LWORD64 prev_best_rdopt_cost = ps_ctxt->as_cu_prms[!curr_buf_idx].i8_best_rdopt_cost;
2967
    /* cabac context of prev intra luma pred flag */
2968
10.1M
    UWORD8 u1_prev_flag_cabac_ctxt =
2969
10.1M
        ps_ctxt->au1_rdopt_init_ctxt_models[IHEVC_CAB_INTRA_LUMA_PRED_FLAG];
2970
10.1M
    WORD32 src_strd = ps_cu_prms->i4_luma_src_stride;
2971
2972
10.1M
    UWORD8 u1_is_cu_noisy = ps_cu_prms->u1_is_cu_noisy && !DISABLE_INTRA_WHEN_NOISY;
2973
2974
10.1M
    total_rdopt_cost = 0;
2975
10.1M
    ps_final_prms = &ps_ctxt->as_cu_prms[curr_buf_idx];
2976
10.1M
    ps_recon_datastore = &ps_final_prms->s_recon_datastore;
2977
10.1M
    i4_recon_stride = ps_final_prms->s_recon_datastore.i4_lumaRecon_stride;
2978
10.1M
    csbf_strd = ps_ctxt->i4_cu_csbf_strd;
2979
10.1M
    pu1_csbf_buf = &ps_ctxt->au1_cu_csbf[0];
2980
10.1M
    pu1_ecd_data = &ps_final_prms->pu1_cu_coeffs[0];
2981
10.1M
    pi2_deq_data = &ps_final_prms->pi2_cu_deq_coeffs[0];
2982
10.1M
    deq_data_strd = ps_cu_analyse->u1_cu_size; /* deq_data stride is cu size */
2983
10.1M
    ps_nbr_4x4 = &ps_ctxt->as_cu_nbr[curr_buf_idx][0];
2984
10.1M
    ps_tmp_lt_4x4 = ps_left_nbr_4x4;
2985
10.1M
    pu4_nbr_flags = &ps_final_prms->au4_nbr_flags[0];
2986
10.1M
    pu1_intra_pred_mode = &ps_final_prms->au1_intra_pred_mode[0];
2987
10.1M
    cu_pos_x = ps_cu_analyse->b3_cu_pos_x;
2988
10.1M
    cu_pos_y = ps_cu_analyse->b3_cu_pos_y;
2989
10.1M
    cu_pos_x_8pelunits = cu_pos_x;
2990
10.1M
    cu_pos_y_8pelunits = cu_pos_y;
2991
2992
    /* reset cu not coded cost */
2993
10.1M
    ps_ctxt->i8_cu_not_coded_cost = 0;
2994
2995
    /* based on the Processng mode */
2996
10.1M
    if(TU_EQ_CU == func_proc_mode)
2997
7.09M
    {
2998
7.09M
        ps_final_prms->u1_part_mode = SIZE_2Nx2N;
2999
7.09M
        trans_size = ps_cu_analyse->u1_cu_size;
3000
7.09M
        num_cu_parts = 1;
3001
7.09M
        num_cands = 1;
3002
7.09M
        u2_num_tus_in_cu = 1;
3003
7.09M
    }
3004
3.09M
    else if(TU_EQ_CU_DIV2 == func_proc_mode)
3005
2.46M
    {
3006
2.46M
        ps_final_prms->u1_part_mode = SIZE_2Nx2N;
3007
2.46M
        trans_size = ps_cu_analyse->u1_cu_size >> 1;
3008
2.46M
        num_cu_parts = 4;
3009
2.46M
        num_cands = 1;
3010
2.46M
        u2_num_tus_in_cu = 4;
3011
2.46M
    }
3012
626k
    else if(TU_EQ_SUBCU == func_proc_mode)
3013
626k
    {
3014
626k
        ps_final_prms->u1_part_mode = SIZE_NxN;
3015
626k
        trans_size = ps_cu_analyse->u1_cu_size >> 1;
3016
626k
        num_cu_parts = 4;
3017
        /*In HQ for TU = SUBPU, all 35 modes used for RDOPT instead of 3 modes */
3018
626k
        if(IHEVCE_QUALITY_P3 > ps_ctxt->i4_quality_preset)
3019
424k
        {
3020
424k
            if(ps_ctxt->i1_slice_type != BSLICE)
3021
355k
            {
3022
355k
                num_cands = (4 * MAX_INTRA_CU_CANDIDATES) + 2;
3023
355k
            }
3024
69.4k
            else
3025
69.4k
            {
3026
69.4k
                num_cands = (2 * MAX_INTRA_CU_CANDIDATES);
3027
69.4k
            }
3028
424k
        }
3029
201k
        else
3030
201k
        {
3031
201k
            num_cands = MAX_INTRA_CU_CANDIDATES;
3032
201k
        }
3033
626k
        u2_num_tus_in_cu = 4;
3034
626k
    }
3035
0
    else
3036
0
    {
3037
        /* should not enter here */
3038
0
        ASSERT(0);
3039
0
    }
3040
3041
10.1M
    if(ps_ctxt->i1_cu_qp_delta_enable)
3042
4.15M
    {
3043
4.15M
        ihevce_update_cu_level_qp_lamda(ps_ctxt, ps_cu_analyse, trans_size, 1);
3044
4.15M
    }
3045
3046
10.1M
    if(u1_is_cu_noisy && !ps_ctxt->u1_enable_psyRDOPT)
3047
0
    {
3048
0
        ps_ctxt->i8_cl_ssd_lambda_qf =
3049
0
            ((float)ps_ctxt->i8_cl_ssd_lambda_qf * (100.0f - RDOPT_LAMBDA_DISCOUNT_WHEN_NOISY) /
3050
0
             100.0f);
3051
0
        ps_ctxt->i8_cl_ssd_lambda_chroma_qf =
3052
0
            ((float)ps_ctxt->i8_cl_ssd_lambda_chroma_qf *
3053
0
             (100.0f - RDOPT_LAMBDA_DISCOUNT_WHEN_NOISY) / 100.0f);
3054
0
    }
3055
3056
10.1M
    u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_QP_WHERE_SPATIAL_SSD_ENABLED) &&
3057
6.19M
                             (ps_ctxt->i4_quality_preset < IHEVCE_QUALITY_P3) &&
3058
4.61M
                             CONVERT_SSDS_TO_SPATIAL_DOMAIN;
3059
3060
10.1M
    if(u1_is_cu_noisy || ps_ctxt->u1_enable_psyRDOPT)
3061
0
    {
3062
0
        u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_HEVC_QP) &&
3063
0
                                 CONVERT_SSDS_TO_SPATIAL_DOMAIN;
3064
0
    }
3065
3066
    /* populate the neigbours */
3067
10.1M
    pu1_left = (UWORD8 *)pv_cu_left;
3068
10.1M
    pu1_top = (UWORD8 *)pv_cu_top;
3069
10.1M
    pu1_top_left = (UWORD8 *)pv_cu_top_left;
3070
10.1M
    left_strd = cu_left_stride;
3071
10.1M
    num_4x4_in_tu = (trans_size >> 2);
3072
10.1M
    num_4x4_in_cu = (ps_cu_analyse->u1_cu_size >> 2);
3073
10.1M
    chrm_present_flag = 1;
3074
10.1M
    ecd_data_bytes_cons = 0;
3075
10.1M
    cu_bits = 0;
3076
3077
    /* get the 4x4 level postion of current cu */
3078
10.1M
    cu_pos_x = cu_pos_x << 1;
3079
10.1M
    cu_pos_y = cu_pos_y << 1;
3080
3081
    /* pouplate cu level params knowing that current is intra */
3082
10.1M
    ps_final_prms->u1_skip_flag = 0;
3083
10.1M
    ps_final_prms->u1_intra_flag = PRED_MODE_INTRA;
3084
10.1M
    ps_final_prms->u2_num_pus_in_cu = 1;
3085
    /*init the is_cu_coded flag*/
3086
10.1M
    ps_final_prms->u1_is_cu_coded = 0;
3087
10.1M
    ps_final_prms->u4_cu_sad = 0;
3088
3089
10.1M
    ps_final_prms->as_pu_enc_loop[0].b1_intra_flag = PRED_MODE_INTRA;
3090
10.1M
    ps_final_prms->as_pu_enc_loop[0].b4_wd = (trans_size >> 1) - 1;
3091
10.1M
    ps_final_prms->as_pu_enc_loop[0].b4_ht = (trans_size >> 1) - 1;
3092
10.1M
    ps_final_prms->as_pu_enc_loop[0].b4_pos_x = cu_pos_x;
3093
10.1M
    ps_final_prms->as_pu_enc_loop[0].b4_pos_y = cu_pos_y;
3094
10.1M
    ps_final_prms->as_pu_enc_loop[0].b1_merge_flag = 0;
3095
3096
10.1M
    ps_final_prms->as_col_pu_enc_loop[0].b1_intra_flag = 1;
3097
3098
    /*copy qp directly as intra cant be skip*/
3099
10.1M
    ps_nbr_4x4->b8_qp = ps_ctxt->i4_cu_qp;
3100
10.1M
    ps_nbr_4x4->mv.s_l0_mv.i2_mvx = 0;
3101
10.1M
    ps_nbr_4x4->mv.s_l0_mv.i2_mvy = 0;
3102
10.1M
    ps_nbr_4x4->mv.s_l1_mv.i2_mvx = 0;
3103
10.1M
    ps_nbr_4x4->mv.s_l1_mv.i2_mvy = 0;
3104
10.1M
    ps_nbr_4x4->mv.i1_l0_ref_pic_buf_id = -1;
3105
10.1M
    ps_nbr_4x4->mv.i1_l1_ref_pic_buf_id = -1;
3106
10.1M
    ps_nbr_4x4->mv.i1_l0_ref_idx = -1;
3107
10.1M
    ps_nbr_4x4->mv.i1_l1_ref_idx = -1;
3108
3109
    /* RDOPT copy States :  TU init (best until prev TU) to current */
3110
10.1M
    memcpy(
3111
10.1M
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
3112
10.1M
             .s_cabac_ctxt.au1_ctxt_models[0],
3113
10.1M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0],
3114
10.1M
        IHEVC_CAB_COEFFX_PREFIX);
3115
3116
    /* RDOPT copy States :update to init state if 0 cbf */
3117
10.1M
    memcpy(
3118
10.1M
        &au1_intra_nxn_rdopt_ctxt_models[0][0],
3119
10.1M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0],
3120
10.1M
        IHEVC_CAB_COEFFX_PREFIX);
3121
10.1M
    memcpy(
3122
10.1M
        &au1_intra_nxn_rdopt_ctxt_models[1][0],
3123
10.1M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0],
3124
10.1M
        IHEVC_CAB_COEFFX_PREFIX);
3125
3126
    /* loop for all partitions in CU  blocks */
3127
28.3M
    for(ctr = 0; ctr < num_cu_parts; ctr++)
3128
19.1M
    {
3129
19.1M
        UWORD8 *pu1_curr_mode;
3130
19.1M
        WORD32 cand_ctr;
3131
19.1M
        WORD32 nbr_flags;
3132
3133
        /* for NxN case to track the best mode       */
3134
        /* for other cases zeroth index will be used */
3135
19.1M
        intra_prev_rem_flags_t as_intra_prev_rem[2];
3136
19.1M
        LWORD64 ai8_cand_rdopt_cost[2];
3137
19.1M
        UWORD32 au4_tu_sad[2];
3138
19.1M
        WORD32 ai4_tu_bits[2];
3139
19.1M
        WORD32 ai4_cbf[2];
3140
19.1M
        WORD32 ai4_curr_bytes[2];
3141
19.1M
        WORD32 ai4_zero_col[2];
3142
19.1M
        WORD32 ai4_zero_row[2];
3143
        /* To store the pred, coeff and dequant for TU_EQ_SUBCU case (since mul.
3144
        cand. are there) ping-pong buffer to store the best and current */
3145
19.1M
        UWORD8 au1_cur_pred_data[2][MIN_TU_SIZE * MIN_TU_SIZE];
3146
19.1M
        UWORD8 au1_intra_coeffs[2][MAX_SCAN_COEFFS_BYTES_4x4];
3147
19.1M
        WORD16 ai2_intra_deq_coeffs[2][MIN_TU_SIZE * MIN_TU_SIZE];
3148
        /* Context models stored for RDopt store and restore purpose */
3149
3150
19.1M
        UWORD8 au1_recon_availability[2];
3151
3152
19.1M
        WORD32 best_cand_idx = 0;
3153
19.1M
        LWORD64 best_cand_cost = MAX_COST_64;
3154
        /* counters to toggle b/w best and current */
3155
19.1M
        WORD32 best_intra_buf_idx = 1;
3156
19.1M
        WORD32 curr_intra_buf_idx = 0;
3157
3158
        /* copy the mode pointer to be used in inner loop */
3159
19.1M
        pu1_curr_mode = pu1_luma_mode;
3160
3161
        /* get the neighbour availability flags */
3162
19.1M
        nbr_flags = ihevce_get_nbr_intra(
3163
19.1M
            &s_nbr,
3164
19.1M
            ps_ctxt->pu1_ctb_nbr_map,
3165
19.1M
            ps_ctxt->i4_nbr_map_strd,
3166
19.1M
            cu_pos_x,
3167
19.1M
            cu_pos_y,
3168
19.1M
            num_4x4_in_tu);
3169
3170
        /* copy the nbr flags for chroma reuse */
3171
19.1M
        if(4 != trans_size)
3172
14.3M
        {
3173
14.3M
            *pu4_nbr_flags = nbr_flags;
3174
14.3M
        }
3175
4.81M
        else if(1 == chrm_present_flag)
3176
1.23M
        {
3177
            /* compute the avail flags assuming luma trans is 8x8 */
3178
            /* get the neighbour availability flags */
3179
1.23M
            *pu4_nbr_flags = ihevce_get_nbr_intra_mxn_tu(
3180
1.23M
                ps_ctxt->pu1_ctb_nbr_map,
3181
1.23M
                ps_ctxt->i4_nbr_map_strd,
3182
1.23M
                cu_pos_x,
3183
1.23M
                cu_pos_y,
3184
1.23M
                (num_4x4_in_tu << 1),
3185
1.23M
                (num_4x4_in_tu << 1));
3186
1.23M
        }
3187
3188
19.1M
        u1_compute_recon = !u1_compute_spatial_ssd && ((num_cu_parts > 1) && (ctr < 3));
3189
3190
19.1M
        if(!ctr && (u1_compute_spatial_ssd || u1_compute_recon))
3191
6.68M
        {
3192
6.68M
            ps_recon_datastore->u1_is_lumaRecon_available = 1;
3193
6.68M
        }
3194
12.4M
        else if(!ctr)
3195
3.50M
        {
3196
3.50M
            ps_recon_datastore->u1_is_lumaRecon_available = 0;
3197
3.50M
        }
3198
3199
19.1M
        ihevc_intra_pred_luma_ref_substitution_fptr =
3200
19.1M
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_ref_substitution_fptr;
3201
3202
        /* call reference array substitution */
3203
19.1M
        ihevc_intra_pred_luma_ref_substitution_fptr(
3204
19.1M
            pu1_top_left,
3205
19.1M
            pu1_top,
3206
19.1M
            pu1_left,
3207
19.1M
            left_strd,
3208
19.1M
            trans_size,
3209
19.1M
            nbr_flags,
3210
19.1M
            (UWORD8 *)ps_ctxt->pv_ref_sub_out,
3211
19.1M
            1);
3212
3213
        /* Intra Mode gating based on MPM cand list and encoder quality preset */
3214
19.1M
        if((ps_ctxt->i1_slice_type != ISLICE) && (TU_EQ_SUBCU == func_proc_mode) &&
3215
1.01M
           (ps_ctxt->i4_quality_preset >= IHEVCE_QUALITY_P3))
3216
195k
        {
3217
195k
            ihevce_mpm_idx_based_filter_RDOPT_cand(
3218
195k
                ps_ctxt,
3219
195k
                ps_cu_analyse,
3220
195k
                ps_left_nbr_4x4,
3221
195k
                ps_top_nbr_4x4,
3222
195k
                pu1_luma_mode,
3223
195k
                &ps_cu_analyse->s_cu_intra_cand.au1_nxn_eval_mark[ctr][0]);
3224
195k
        }
3225
3226
19.1M
        if((TU_EQ_SUBCU == func_proc_mode) && (ps_ctxt->i4_quality_preset < IHEVCE_QUALITY_P3) &&
3227
1.67M
           (ps_cu_analyse->s_cu_intra_cand.au1_num_modes_added[ctr] >= MAX_INTRA_CU_CANDIDATES))
3228
1.67M
        {
3229
1.67M
            WORD32 ai4_mpm_mode_list[3];
3230
1.67M
            WORD32 i;
3231
3232
1.67M
            WORD32 i4_curr_index = ps_cu_analyse->s_cu_intra_cand.au1_num_modes_added[ctr];
3233
3234
1.67M
            ihevce_populate_intra_pred_mode(
3235
1.67M
                ps_top_nbr_4x4->b6_luma_intra_mode,
3236
1.67M
                ps_tmp_lt_4x4->b6_luma_intra_mode,
3237
1.67M
                s_nbr.u1_top_avail,
3238
1.67M
                s_nbr.u1_left_avail,
3239
1.67M
                cu_pos_y,
3240
1.67M
                &ai4_mpm_mode_list[0]);
3241
3242
6.71M
            for(i = 0; i < 3; i++)
3243
5.03M
            {
3244
5.03M
                if(ps_cu_analyse->s_cu_intra_cand
3245
5.03M
                       .au1_intra_luma_mode_nxn_hash[ctr][ai4_mpm_mode_list[i]] == 0)
3246
1.02M
                {
3247
1.02M
                    ASSERT(ai4_mpm_mode_list[i] < 35);
3248
3249
1.02M
                    ps_cu_analyse->s_cu_intra_cand
3250
1.02M
                        .au1_intra_luma_mode_nxn_hash[ctr][ai4_mpm_mode_list[i]] = 1;
3251
1.02M
                    pu1_luma_mode[i4_curr_index] = ai4_mpm_mode_list[i];
3252
1.02M
                    ps_cu_analyse->s_cu_intra_cand.au1_num_modes_added[ctr]++;
3253
1.02M
                    i4_curr_index++;
3254
1.02M
                }
3255
5.03M
            }
3256
3257
1.67M
            pu1_luma_mode[i4_curr_index] = 255;
3258
1.67M
        }
3259
3260
        /* loop over candidates for each partition */
3261
48.7M
        for(cand_ctr = 0; cand_ctr < num_cands; cand_ctr++)
3262
31.4M
        {
3263
31.4M
            WORD32 curr_pred_mode;
3264
31.4M
            WORD32 bits = 0;
3265
31.4M
            LWORD64 curr_cost;
3266
31.4M
            WORD32 luma_pred_func_idx;
3267
31.4M
            UWORD8 *pu1_curr_ecd_data;
3268
31.4M
            WORD16 *pi2_curr_deq_data;
3269
31.4M
            WORD32 curr_deq_data_strd;
3270
31.4M
            WORD32 pred_strd;
3271
31.4M
            UWORD8 *pu1_pred;
3272
3273
            /* if NXN case the recon and ecd data is stored in temp buffers */
3274
31.4M
            if(TU_EQ_SUBCU == func_proc_mode)
3275
14.7M
            {
3276
14.7M
                pu1_pred = &au1_cur_pred_data[curr_intra_buf_idx][0];
3277
14.7M
                pred_strd = trans_size;
3278
14.7M
                pu1_curr_ecd_data = &au1_intra_coeffs[curr_intra_buf_idx][0];
3279
14.7M
                pi2_curr_deq_data = &ai2_intra_deq_coeffs[curr_intra_buf_idx][0];
3280
14.7M
                curr_deq_data_strd = trans_size;
3281
3282
14.7M
                ASSERT(trans_size == MIN_TU_SIZE);
3283
14.7M
            }
3284
16.6M
            else
3285
16.6M
            {
3286
16.6M
                pu1_pred = (UWORD8 *)pv_pred_org;
3287
16.6M
                pred_strd = pred_strd_org;
3288
16.6M
                pu1_curr_ecd_data = pu1_ecd_data;
3289
16.6M
                pi2_curr_deq_data = pi2_deq_data;
3290
16.6M
                curr_deq_data_strd = deq_data_strd;
3291
16.6M
            }
3292
3293
31.4M
            pu1_recon = ((UWORD8 *)ps_recon_datastore->apv_luma_recon_bufs[curr_intra_buf_idx]) +
3294
31.4M
                        (ctr & 1) * trans_size + (ctr > 1) * trans_size * i4_recon_stride;
3295
3296
31.4M
            if(is_sub_pu_in_hq == 1)
3297
0
            {
3298
0
                curr_pred_mode = cand_ctr;
3299
0
            }
3300
31.4M
            else
3301
31.4M
            {
3302
31.4M
                curr_pred_mode = pu1_curr_mode[cand_ctr];
3303
31.4M
            }
3304
3305
            /* If the candidate mode is 255, then break */
3306
31.4M
            if(255 == curr_pred_mode)
3307
1.84M
            {
3308
1.84M
                break;
3309
1.84M
            }
3310
29.5M
            else if(250 == curr_pred_mode)
3311
0
            {
3312
0
                continue;
3313
0
            }
3314
3315
            /* check if this mode needs to be evaluated or not. For 2nx2n cases, this   */
3316
            /* function will be called once per candidate, so this check has been done  */
3317
            /* outside this function call. For NxN case, this function will be called   */
3318
            /* only once, and all the candidates will be evaluated here.                */
3319
29.5M
            if(ps_ctxt->i4_quality_preset >= IHEVCE_QUALITY_P3)
3320
7.93M
            {
3321
7.93M
                if((TU_EQ_SUBCU == func_proc_mode) &&
3322
2.20M
                   (0 == ps_cu_analyse->s_cu_intra_cand.au1_nxn_eval_mark[ctr][cand_ctr]))
3323
224k
                {
3324
224k
                    continue;
3325
224k
                }
3326
7.93M
            }
3327
3328
            /* call reference filtering */
3329
29.3M
            ps_ctxt->ps_func_selector->ihevc_intra_pred_ref_filtering_fptr(
3330
29.3M
                (UWORD8 *)ps_ctxt->pv_ref_sub_out,
3331
29.3M
                trans_size,
3332
29.3M
                (UWORD8 *)ps_ctxt->pv_ref_filt_out,
3333
29.3M
                curr_pred_mode,
3334
29.3M
                ps_ctxt->i1_strong_intra_smoothing_enable_flag);
3335
3336
            /* use the look up to get the function idx */
3337
29.3M
            luma_pred_func_idx = g_i4_ip_funcs[curr_pred_mode];
3338
3339
            /* call the intra prediction function */
3340
29.3M
            ps_ctxt->apf_lum_ip[luma_pred_func_idx](
3341
29.3M
                (UWORD8 *)ps_ctxt->pv_ref_filt_out,
3342
29.3M
                1,
3343
29.3M
                pu1_pred,
3344
29.3M
                pred_strd,
3345
29.3M
                trans_size,
3346
29.3M
                curr_pred_mode);
3347
3348
            /* populate the coeffs scan idx */
3349
29.3M
            ps_ctxt->i4_scan_idx = SCAN_DIAG_UPRIGHT;
3350
3351
            /* for luma 4x4 and 8x8 transforms based on intra pred mode scan is choosen*/
3352
29.3M
            if(trans_size < 16)
3353
22.4M
            {
3354
                /* for modes from 22 upto 30 horizontal scan is used */
3355
22.4M
                if((curr_pred_mode > 21) && (curr_pred_mode < 31))
3356
5.96M
                {
3357
5.96M
                    ps_ctxt->i4_scan_idx = SCAN_HORZ;
3358
5.96M
                }
3359
                /* for modes from 6 upto 14 horizontal scan is used */
3360
16.5M
                else if((curr_pred_mode > 5) && (curr_pred_mode < 15))
3361
5.21M
                {
3362
5.21M
                    ps_ctxt->i4_scan_idx = SCAN_VERT;
3363
5.21M
                }
3364
22.4M
            }
3365
3366
            /* RDOPT copy States :  TU init (best until prev TU) to current */
3367
29.3M
            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
3368
29.3M
                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
3369
29.3M
                        .s_cabac_ctxt.au1_ctxt_models[0] +
3370
29.3M
                    IHEVC_CAB_COEFFX_PREFIX,
3371
29.3M
                &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
3372
29.3M
                IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
3373
3374
29.3M
            i4_perform_rdoq = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_rdoq;
3375
29.3M
            i4_perform_sbh = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_sbh;
3376
3377
#if DISABLE_RDOQ_INTRA
3378
            i4_perform_rdoq = 0;
3379
#endif
3380
3381
            /*2 Multi- dimensinal array based on trans size  of rounding factor to be added here */
3382
            /* arrays are for rounding factor corr. to 0-1 decision and 1-2 decision */
3383
            /* Currently the complete array will contain only single value*/
3384
            /*The rounding factor is calculated with the formula
3385
            Deadzone val = (((R1 - R0) * (2^(-8/3)) * lamMod) + 1)/2
3386
            rounding factor = (1 - DeadZone Val)
3387
3388
            Assumption: Cabac states of All the sub-blocks in the TU are considered independent
3389
            */
3390
29.3M
            if((ps_ctxt->i4_quant_rounding_level != FIXED_QUANT_ROUNDING))
3391
21.6M
            {
3392
21.6M
                if((ps_ctxt->i4_quant_rounding_level == TU_LEVEL_QUANT_ROUNDING) && (ctr != 0))
3393
0
                {
3394
0
                    double i4_lamda_modifier;
3395
3396
0
                    if((BSLICE == ps_ctxt->i1_slice_type) && (ps_ctxt->i4_temporal_layer_id))
3397
0
                    {
3398
0
                        i4_lamda_modifier =
3399
0
                            ps_ctxt->i4_lamda_modifier *
3400
0
                            CLIP3((((double)(ps_ctxt->i4_cu_qp - 12)) / 6.0), 2.00, 4.00);
3401
0
                    }
3402
0
                    else
3403
0
                    {
3404
0
                        i4_lamda_modifier = ps_ctxt->i4_lamda_modifier;
3405
0
                    }
3406
0
                    if(ps_ctxt->i4_use_const_lamda_modifier)
3407
0
                    {
3408
0
                        if(ISLICE == ps_ctxt->i1_slice_type)
3409
0
                        {
3410
0
                            i4_lamda_modifier = ps_ctxt->f_i_pic_lamda_modifier;
3411
0
                        }
3412
0
                        else
3413
0
                        {
3414
0
                            i4_lamda_modifier = CONST_LAMDA_MOD_VAL;
3415
0
                        }
3416
0
                    }
3417
3418
0
                    ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3] =
3419
0
                        &ps_ctxt->i4_quant_round_tu[0][0];
3420
0
                    ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3] =
3421
0
                        &ps_ctxt->i4_quant_round_tu[1][0];
3422
3423
0
                    memset(
3424
0
                        ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3],
3425
0
                        0,
3426
0
                        trans_size * trans_size * sizeof(WORD32));
3427
0
                    memset(
3428
0
                        ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3],
3429
0
                        0,
3430
0
                        trans_size * trans_size * sizeof(WORD32));
3431
3432
0
                    ihevce_quant_rounding_factor_gen(
3433
0
                        trans_size,
3434
0
                        1,
3435
0
                        &ps_ctxt->s_rdopt_entropy_ctxt,
3436
0
                        ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3],
3437
0
                        ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3],
3438
0
                        i4_lamda_modifier,
3439
0
                        1);
3440
0
                }
3441
21.6M
                else
3442
21.6M
                {
3443
21.6M
                    ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3] =
3444
21.6M
                        ps_ctxt->pi4_quant_round_factor_cu_ctb_0_1[trans_size >> 3];
3445
21.6M
                    ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3] =
3446
21.6M
                        ps_ctxt->pi4_quant_round_factor_cu_ctb_1_2[trans_size >> 3];
3447
21.6M
                }
3448
21.6M
            }
3449
3450
            /* call T Q IT IQ and recon function */
3451
29.3M
            ai4_cbf[curr_intra_buf_idx] = ihevce_t_q_iq_ssd_scan_fxn(
3452
29.3M
                ps_ctxt,
3453
29.3M
                pu1_pred,
3454
29.3M
                pred_strd,
3455
29.3M
                (UWORD8 *)pv_curr_src,
3456
29.3M
                src_strd,
3457
29.3M
                pi2_curr_deq_data,
3458
29.3M
                curr_deq_data_strd,
3459
29.3M
                pu1_recon,
3460
29.3M
                i4_recon_stride,
3461
29.3M
                pu1_curr_ecd_data,
3462
29.3M
                pu1_csbf_buf,
3463
29.3M
                csbf_strd,
3464
29.3M
                trans_size,
3465
29.3M
                PRED_MODE_INTRA,
3466
29.3M
                &ai8_cand_rdopt_cost[curr_intra_buf_idx],
3467
29.3M
                &ai4_curr_bytes[curr_intra_buf_idx],
3468
29.3M
                &ai4_tu_bits[curr_intra_buf_idx],
3469
29.3M
                &au4_tu_sad[curr_intra_buf_idx],
3470
29.3M
                &ai4_zero_col[curr_intra_buf_idx],
3471
29.3M
                &ai4_zero_row[curr_intra_buf_idx],
3472
29.3M
                &au1_recon_availability[curr_intra_buf_idx],
3473
29.3M
                i4_perform_rdoq,
3474
29.3M
                i4_perform_sbh,
3475
29.3M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
3476
29.3M
                i4_alpha_stim_multiplier,
3477
29.3M
                u1_is_cu_noisy,
3478
29.3M
#endif
3479
29.3M
                u1_compute_spatial_ssd ? SPATIAL_DOMAIN_SSD : FREQUENCY_DOMAIN_SSD,
3480
29.3M
                1 /*early_cbf */
3481
29.3M
            );
3482
3483
#if COMPUTE_NOISE_TERM_AT_THE_TU_LEVEL && !USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
3484
            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
3485
            {
3486
#if !USE_RECON_TO_EVALUATE_STIM_IN_RDOPT
3487
                ai8_cand_rdopt_cost[curr_intra_buf_idx] = ihevce_inject_stim_into_distortion(
3488
                    pv_curr_src,
3489
                    src_strd,
3490
                    pu1_pred,
3491
                    pred_strd,
3492
                    ai8_cand_rdopt_cost[curr_intra_buf_idx],
3493
                    i4_alpha_stim_multiplier,
3494
                    trans_size,
3495
                    0,
3496
                    ps_ctxt->u1_enable_psyRDOPT,
3497
                    NULL_PLANE);
3498
#else
3499
                if(u1_compute_spatial_ssd && au1_recon_availability[curr_intra_buf_idx])
3500
                {
3501
                    ai8_cand_rdopt_cost[curr_intra_buf_idx] = ihevce_inject_stim_into_distortion(
3502
                        pv_curr_src,
3503
                        src_strd,
3504
                        pu1_recon,
3505
                        i4_recon_stride,
3506
                        ai8_cand_rdopt_cost[curr_intra_buf_idx],
3507
                        i4_alpha_stim_multiplier,
3508
                        trans_size,
3509
                        0,
3510
                        ps_ctxt->u1_enable_psyRDOPT,
3511
                        NULL_PLANE);
3512
                }
3513
                else
3514
                {
3515
                    ai8_cand_rdopt_cost[curr_intra_buf_idx] = ihevce_inject_stim_into_distortion(
3516
                        pv_curr_src,
3517
                        src_strd,
3518
                        pu1_pred,
3519
                        pred_strd,
3520
                        ai8_cand_rdopt_cost[curr_intra_buf_idx],
3521
                        i4_alpha_stim_multiplier,
3522
                        trans_size,
3523
                        0,
3524
                        ps_ctxt->u1_enable_psyRDOPT,
3525
                        NULL_PLANE);
3526
                }
3527
#endif
3528
            }
3529
#endif
3530
3531
29.3M
            if(TU_EQ_SUBCU == func_proc_mode)
3532
12.7M
            {
3533
12.7M
                ASSERT(ai4_curr_bytes[curr_intra_buf_idx] < MAX_SCAN_COEFFS_BYTES_4x4);
3534
12.7M
            }
3535
3536
            /* based on CBF/No CBF copy the corresponding state */
3537
29.3M
            if(0 == ai4_cbf[curr_intra_buf_idx])
3538
19.1M
            {
3539
                /* RDOPT copy States :update to init state if 0 cbf */
3540
19.1M
                COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
3541
19.1M
                    &au1_intra_nxn_rdopt_ctxt_models[curr_intra_buf_idx][0] +
3542
19.1M
                        IHEVC_CAB_COEFFX_PREFIX,
3543
19.1M
                    &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
3544
19.1M
                    IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
3545
19.1M
            }
3546
10.2M
            else
3547
10.2M
            {
3548
                /* RDOPT copy States :update to new state only if CBF is non zero */
3549
10.2M
                COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
3550
10.2M
                    &au1_intra_nxn_rdopt_ctxt_models[curr_intra_buf_idx][0] +
3551
10.2M
                        IHEVC_CAB_COEFFX_PREFIX,
3552
10.2M
                    &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
3553
10.2M
                            .s_cabac_ctxt.au1_ctxt_models[0] +
3554
10.2M
                        IHEVC_CAB_COEFFX_PREFIX,
3555
10.2M
                    IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
3556
10.2M
            }
3557
3558
            /* call the function which perform intra mode prediction */
3559
29.3M
            ihevce_intra_pred_mode_signaling(
3560
29.3M
                ps_top_nbr_4x4->b6_luma_intra_mode,
3561
29.3M
                ps_tmp_lt_4x4->b6_luma_intra_mode,
3562
29.3M
                s_nbr.u1_top_avail,
3563
29.3M
                s_nbr.u1_left_avail,
3564
29.3M
                cu_pos_y,
3565
29.3M
                curr_pred_mode,
3566
29.3M
                &as_intra_prev_rem[curr_intra_buf_idx]);
3567
            /******************************************************************/
3568
            /* PREV INTRA LUMA FLAG, MPM MODE and REM INTRA MODE bits for I_NxN
3569
            The bits for these are evaluated for every RDO mode of current subcu
3570
            as they can significantly contribute to RDO cost.  Note that these
3571
            bits are not accounted for here (ai8_cand_rdopt_cost) as they
3572
            are accounted for in encode_cu call later */
3573
3574
            /******************************************************************/
3575
            /* PREV INTRA LUMA FLAG, MPM MODE and REM INTRA MODE bits for I_NxN
3576
            The bits for these are evaluated for every RDO mode of current subcu
3577
            as they can significantly contribute to RDO cost.  Note that these
3578
            bits are not accounted for here (ai8_cand_rdopt_cost) as they
3579
            are accounted for in encode_cu call later */
3580
3581
            /* Estimate bits to encode prev rem flag  for NXN mode */
3582
29.3M
            {
3583
29.3M
                WORD32 bits_frac = gau2_ihevce_cabac_bin_to_bits
3584
29.3M
                    [u1_prev_flag_cabac_ctxt ^
3585
29.3M
                     as_intra_prev_rem[curr_intra_buf_idx].b1_prev_intra_luma_pred_flag];
3586
3587
                /* rounding the fractional bits to nearest integer */
3588
29.3M
                bits = ((bits_frac + (1 << (CABAC_FRAC_BITS_Q - 1))) >> CABAC_FRAC_BITS_Q);
3589
29.3M
            }
3590
3591
            /* based on prev flag all the mpmidx bits and rem bits */
3592
29.3M
            if(1 == as_intra_prev_rem[curr_intra_buf_idx].b1_prev_intra_luma_pred_flag)
3593
21.0M
            {
3594
                /* mpm_idx */
3595
21.0M
                bits += as_intra_prev_rem[curr_intra_buf_idx].b2_mpm_idx ? 2 : 1;
3596
21.0M
            }
3597
8.26M
            else
3598
8.26M
            {
3599
                /* rem intra mode */
3600
8.26M
                bits += 5;
3601
8.26M
            }
3602
3603
29.3M
            bits += ai4_tu_bits[curr_intra_buf_idx];
3604
3605
            /* compute the total cost for current candidate */
3606
29.3M
            curr_cost = ai8_cand_rdopt_cost[curr_intra_buf_idx];
3607
3608
            /* get the final ssd cost */
3609
29.3M
            curr_cost +=
3610
29.3M
                COMPUTE_RATE_COST_CLIP30(bits, ps_ctxt->i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
3611
3612
            /* check of the best candidate cost */
3613
29.3M
            if(curr_cost < best_cand_cost)
3614
20.6M
            {
3615
20.6M
                best_cand_cost = curr_cost;
3616
20.6M
                best_cand_idx = cand_ctr;
3617
20.6M
                best_intra_buf_idx = curr_intra_buf_idx;
3618
20.6M
                curr_intra_buf_idx = !curr_intra_buf_idx;
3619
20.6M
            }
3620
29.3M
        }
3621
3622
        /***************    For TU_EQ_SUBCU case    *****************/
3623
        /* Copy the pred for best cand. to the final pred array     */
3624
        /* Copy the iq-coeff for best cand. to the final array      */
3625
        /* copy the best coeffs data to final buffer                */
3626
19.1M
        if(TU_EQ_SUBCU == func_proc_mode)
3627
2.48M
        {
3628
            /* Copy the pred for best cand. to the final pred array */
3629
3630
2.48M
            ps_ctxt->s_cmn_opt_func.pf_copy_2d(
3631
2.48M
                (UWORD8 *)pv_pred_org,
3632
2.48M
                pred_strd_org,
3633
2.48M
                &au1_cur_pred_data[best_intra_buf_idx][0],
3634
2.48M
                trans_size,
3635
2.48M
                trans_size,
3636
2.48M
                trans_size);
3637
3638
            /* Copy the deq-coeff for best cand. to the final array */
3639
3640
2.48M
            ps_ctxt->s_cmn_opt_func.pf_copy_2d(
3641
2.48M
                (UWORD8 *)pi2_deq_data,
3642
2.48M
                deq_data_strd << 1,
3643
2.48M
                (UWORD8 *)&ai2_intra_deq_coeffs[best_intra_buf_idx][0],
3644
2.48M
                trans_size << 1,
3645
2.48M
                trans_size << 1,
3646
2.48M
                trans_size);
3647
            /* copy the coeffs to final cu ecd bytes buffer */
3648
2.48M
            memcpy(
3649
2.48M
                pu1_ecd_data,
3650
2.48M
                &au1_intra_coeffs[best_intra_buf_idx][0],
3651
2.48M
                ai4_curr_bytes[best_intra_buf_idx]);
3652
3653
2.48M
            pu1_recon = ((UWORD8 *)ps_recon_datastore->apv_luma_recon_bufs[best_intra_buf_idx]) +
3654
2.48M
                        (ctr & 1) * trans_size + (ctr > 1) * trans_size * i4_recon_stride;
3655
2.48M
        }
3656
3657
        /*----------   Calculate Recon for the best INTRA mode     ---------*/
3658
        /* TU_EQ_CU case : No need for recon, otherwise recon is required   */
3659
        /* Compute recon only for the best mode for TU_EQ_SUBCU case        */
3660
19.1M
        if(u1_compute_recon)
3661
6.10M
        {
3662
6.10M
            ihevce_it_recon_fxn(
3663
6.10M
                ps_ctxt,
3664
6.10M
                pi2_deq_data,
3665
6.10M
                deq_data_strd,
3666
6.10M
                (UWORD8 *)pv_pred_org,
3667
6.10M
                pred_strd_org,
3668
6.10M
                pu1_recon,
3669
6.10M
                i4_recon_stride,
3670
6.10M
                pu1_ecd_data,
3671
6.10M
                trans_size,
3672
6.10M
                PRED_MODE_INTRA,
3673
6.10M
                ai4_cbf[best_intra_buf_idx],
3674
6.10M
                ai4_zero_col[best_intra_buf_idx],
3675
6.10M
                ai4_zero_row[best_intra_buf_idx]);
3676
3677
6.10M
            ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr] = best_intra_buf_idx;
3678
6.10M
        }
3679
13.0M
        else if(u1_compute_spatial_ssd && au1_recon_availability[best_intra_buf_idx])
3680
7.56M
        {
3681
7.56M
            ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr] = best_intra_buf_idx;
3682
7.56M
        }
3683
5.45M
        else
3684
5.45M
        {
3685
5.45M
            ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr] = UCHAR_MAX;
3686
5.45M
        }
3687
3688
        /* RDOPT copy States :update to best modes state */
3689
19.1M
        COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
3690
19.1M
            &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
3691
19.1M
            &au1_intra_nxn_rdopt_ctxt_models[best_intra_buf_idx][0] + IHEVC_CAB_COEFFX_PREFIX,
3692
19.1M
            IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
3693
3694
        /* copy the prev,mpm_idx and rem modes from best cand */
3695
19.1M
        ps_final_prms->as_intra_prev_rem[ctr] = as_intra_prev_rem[best_intra_buf_idx];
3696
3697
        /* update the cabac context of prev intra pred mode flag */
3698
19.1M
        u1_prev_flag_cabac_ctxt = gau1_ihevc_next_state
3699
19.1M
            [(u1_prev_flag_cabac_ctxt << 1) |
3700
19.1M
             as_intra_prev_rem[best_intra_buf_idx].b1_prev_intra_luma_pred_flag];
3701
3702
        /* accumulate the TU bits into cu bits */
3703
19.1M
        cu_bits += ai4_tu_bits[best_intra_buf_idx];
3704
3705
        /* copy the intra pred mode for chroma reuse */
3706
19.1M
        if(is_sub_pu_in_hq == 0)
3707
19.1M
        {
3708
19.1M
            *pu1_intra_pred_mode = pu1_curr_mode[best_cand_idx];
3709
19.1M
        }
3710
0
        else
3711
0
        {
3712
0
            *pu1_intra_pred_mode = best_cand_idx;
3713
0
        }
3714
3715
        /* Store luma mode as chroma mode. If chroma prcs happens, and
3716
        if a diff. mode wins, it should update this!! */
3717
19.1M
        if(1 == chrm_present_flag)
3718
15.5M
        {
3719
15.5M
            if(is_sub_pu_in_hq == 0)
3720
15.5M
            {
3721
15.5M
                ps_final_prms->u1_chroma_intra_pred_actual_mode =
3722
15.5M
                    ((ps_ctxt->u1_chroma_array_type == 2)
3723
15.5M
                         ? gau1_chroma422_intra_angle_mapping[pu1_curr_mode[best_cand_idx]]
3724
15.5M
                         : pu1_curr_mode[best_cand_idx]);
3725
15.5M
            }
3726
0
            else
3727
0
            {
3728
0
                ps_final_prms->u1_chroma_intra_pred_actual_mode =
3729
0
                    ((ps_ctxt->u1_chroma_array_type == 2)
3730
0
                         ? gau1_chroma422_intra_angle_mapping[best_cand_idx]
3731
0
                         : best_cand_idx);
3732
0
            }
3733
3734
15.5M
            ps_final_prms->u1_chroma_intra_pred_mode = 4;
3735
15.5M
        }
3736
3737
        /*remember the cbf flag to replicate qp for 4x4 neighbour*/
3738
19.1M
        ps_final_prms->u1_is_cu_coded |= ai4_cbf[best_intra_buf_idx];
3739
3740
        /*accumulate ssd over all TU of intra CU*/
3741
19.1M
        ps_final_prms->u4_cu_sad += au4_tu_sad[best_intra_buf_idx];
3742
3743
        /* update the bytes */
3744
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].i4_luma_coeff_offset = ecd_data_bytes_cons;
3745
19.1M
        ps_final_prms->as_tu_enc_loop_temp_prms[ctr].i2_luma_bytes_consumed =
3746
19.1M
            ai4_curr_bytes[best_intra_buf_idx];
3747
        /* update the zero_row and col info for the final mode */
3748
19.1M
        ps_final_prms->as_tu_enc_loop_temp_prms[ctr].u4_luma_zero_col =
3749
19.1M
            ai4_zero_col[best_intra_buf_idx];
3750
19.1M
        ps_final_prms->as_tu_enc_loop_temp_prms[ctr].u4_luma_zero_row =
3751
19.1M
            ai4_zero_row[best_intra_buf_idx];
3752
3753
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].i4_luma_coeff_offset = ecd_data_bytes_cons;
3754
3755
        /* update the total bytes cons */
3756
19.1M
        ecd_data_bytes_cons += ai4_curr_bytes[best_intra_buf_idx];
3757
19.1M
        pu1_ecd_data += ai4_curr_bytes[best_intra_buf_idx];
3758
3759
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_y_cbf = ai4_cbf[best_intra_buf_idx];
3760
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf = 0;
3761
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf = 0;
3762
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf_subtu1 = 0;
3763
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf_subtu1 = 0;
3764
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b3_chroma_intra_mode_idx = chrm_present_flag;
3765
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b7_qp = ps_ctxt->i4_cu_qp;
3766
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_first_tu_in_cu = 0;
3767
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_transquant_bypass = 0;
3768
19.1M
        GETRANGE(tx_size, trans_size);
3769
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b3_size = tx_size - 3;
3770
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_x = cu_pos_x;
3771
19.1M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_y = cu_pos_y;
3772
3773
        /* repiclate the nbr 4x4 structure for all 4x4 blocks current TU */
3774
19.1M
        ps_nbr_4x4->b1_skip_flag = 0;
3775
19.1M
        ps_nbr_4x4->b1_intra_flag = 1;
3776
19.1M
        ps_nbr_4x4->b1_pred_l0_flag = 0;
3777
19.1M
        ps_nbr_4x4->b1_pred_l1_flag = 0;
3778
3779
19.1M
        if(is_sub_pu_in_hq == 0)
3780
19.1M
        {
3781
19.1M
            ps_nbr_4x4->b6_luma_intra_mode = pu1_curr_mode[best_cand_idx];
3782
19.1M
        }
3783
0
        else
3784
0
        {
3785
0
            ps_nbr_4x4->b6_luma_intra_mode = best_cand_idx;
3786
0
        }
3787
3788
19.1M
        ps_nbr_4x4->b1_y_cbf = ai4_cbf[best_intra_buf_idx];
3789
3790
        /* since tu size can be less than cusize, replication is done with strd */
3791
19.1M
        {
3792
19.1M
            WORD32 i, j;
3793
19.1M
            nbr_4x4_t *ps_tmp_4x4;
3794
3795
19.1M
            ps_tmp_4x4 = ps_nbr_4x4;
3796
3797
73.6M
            for(i = 0; i < num_4x4_in_tu; i++)
3798
54.4M
            {
3799
286M
                for(j = 0; j < num_4x4_in_tu; j++)
3800
232M
                {
3801
232M
                    ps_tmp_4x4[j] = *ps_nbr_4x4;
3802
232M
                }
3803
                /* row level update*/
3804
54.4M
                ps_tmp_4x4 += num_4x4_in_cu;
3805
54.4M
            }
3806
19.1M
        }
3807
3808
19.1M
        if(TU_EQ_SUBCU == func_proc_mode)
3809
2.48M
        {
3810
2.48M
            pu1_luma_mode += ((MAX_INTRA_CU_CANDIDATES * 4) + 2 + 1);
3811
2.48M
        }
3812
3813
19.1M
        if((num_cu_parts > 1) && (ctr < 3))
3814
9.10M
        {
3815
            /* set the neighbour map to 1 */
3816
9.10M
            ihevce_set_nbr_map(
3817
9.10M
                ps_ctxt->pu1_ctb_nbr_map,
3818
9.10M
                ps_ctxt->i4_nbr_map_strd,
3819
9.10M
                cu_pos_x,
3820
9.10M
                cu_pos_y,
3821
9.10M
                trans_size >> 2,
3822
9.10M
                1);
3823
3824
            /* block level updates block number (1 & 3 )*/
3825
9.10M
            pv_curr_src = (UWORD8 *)pv_curr_src + trans_size;
3826
9.10M
            pv_pred_org = (UWORD8 *)pv_pred_org + trans_size;
3827
9.10M
            pi2_deq_data += trans_size;
3828
3829
9.10M
            switch(ctr)
3830
9.10M
            {
3831
3.09M
            case 0:
3832
3.09M
            {
3833
3.09M
                pu1_left = pu1_recon + trans_size - 1;
3834
3.09M
                pu1_top += trans_size;
3835
3.09M
                pu1_top_left = pu1_top - 1;
3836
3.09M
                left_strd = i4_recon_stride;
3837
3838
3.09M
                break;
3839
0
            }
3840
3.02M
            case 1:
3841
3.02M
            {
3842
3.02M
                ASSERT(
3843
3.02M
                    (ps_recon_datastore->au1_bufId_with_winning_LumaRecon[0] == 0) ||
3844
3.02M
                    (ps_recon_datastore->au1_bufId_with_winning_LumaRecon[0] == 1));
3845
3846
                /* Since the 'lumaRefSubstitution' function expects both Top and */
3847
                /* TopRight recon pixels to be present in the same buffer */
3848
3.02M
                if(ps_recon_datastore->au1_bufId_with_winning_LumaRecon[0] !=
3849
3.02M
                   ps_recon_datastore->au1_bufId_with_winning_LumaRecon[1])
3850
175k
                {
3851
175k
                    UWORD8 *pu1_src =
3852
175k
                        ((UWORD8 *)ps_recon_datastore->apv_luma_recon_bufs
3853
175k
                             [ps_recon_datastore->au1_bufId_with_winning_LumaRecon[1]]) +
3854
175k
                        trans_size;
3855
175k
                    UWORD8 *pu1_dst =
3856
175k
                        ((UWORD8 *)ps_recon_datastore->apv_luma_recon_bufs
3857
175k
                             [ps_recon_datastore->au1_bufId_with_winning_LumaRecon[0]]) +
3858
175k
                        trans_size;
3859
3860
175k
                    ps_ctxt->s_cmn_opt_func.pf_copy_2d(
3861
175k
                        pu1_dst, i4_recon_stride, pu1_src, i4_recon_stride, trans_size, trans_size);
3862
3863
175k
                    ps_recon_datastore->au1_bufId_with_winning_LumaRecon[1] =
3864
175k
                        ps_recon_datastore->au1_bufId_with_winning_LumaRecon[0];
3865
175k
                }
3866
3867
3.02M
                pu1_left = (UWORD8 *)pv_cu_left + trans_size * cu_left_stride;
3868
3.02M
                pu1_top = ((UWORD8 *)ps_recon_datastore->apv_luma_recon_bufs
3869
3.02M
                               [ps_recon_datastore->au1_bufId_with_winning_LumaRecon[0]]) +
3870
3.02M
                          (trans_size - 1) * i4_recon_stride;
3871
3.02M
                pu1_top_left = pu1_left - cu_left_stride;
3872
3.02M
                left_strd = cu_left_stride;
3873
3874
3.02M
                break;
3875
0
            }
3876
2.98M
            case 2:
3877
2.98M
            {
3878
2.98M
                ASSERT(
3879
2.98M
                    (ps_recon_datastore->au1_bufId_with_winning_LumaRecon[1] == 0) ||
3880
2.98M
                    (ps_recon_datastore->au1_bufId_with_winning_LumaRecon[1] == 1));
3881
3882
2.98M
                pu1_left = pu1_recon + trans_size - 1;
3883
2.98M
                pu1_top = ((UWORD8 *)ps_recon_datastore->apv_luma_recon_bufs
3884
2.98M
                               [ps_recon_datastore->au1_bufId_with_winning_LumaRecon[1]]) +
3885
2.98M
                          (trans_size - 1) * i4_recon_stride + trans_size;
3886
2.98M
                pu1_top_left = pu1_top - 1;
3887
2.98M
                left_strd = i4_recon_stride;
3888
3889
2.98M
                break;
3890
0
            }
3891
9.10M
            }
3892
3893
9.10M
            pu1_csbf_buf += num_4x4_in_tu;
3894
9.10M
            cu_pos_x += num_4x4_in_tu;
3895
9.10M
            ps_nbr_4x4 += num_4x4_in_tu;
3896
9.10M
            ps_top_nbr_4x4 += num_4x4_in_tu;
3897
9.10M
            ps_tmp_lt_4x4 = ps_nbr_4x4 - 1;
3898
3899
9.10M
            pu1_intra_pred_mode++;
3900
3901
            /* after 2 blocks increment the pointers to bottom blocks */
3902
9.10M
            if(1 == ctr)
3903
3.02M
            {
3904
3.02M
                pv_curr_src = (UWORD8 *)pv_curr_src - (trans_size << 1);
3905
3.02M
                pv_curr_src = (UWORD8 *)pv_curr_src + (trans_size * src_strd);
3906
3907
3.02M
                pv_pred_org = (UWORD8 *)pv_pred_org - (trans_size << 1);
3908
3.02M
                pv_pred_org = (UWORD8 *)pv_pred_org + (trans_size * pred_strd_org);
3909
3.02M
                pi2_deq_data -= (trans_size << 1);
3910
3.02M
                pi2_deq_data += (trans_size * deq_data_strd);
3911
3912
3.02M
                pu1_csbf_buf -= (num_4x4_in_tu << 1);
3913
3.02M
                pu1_csbf_buf += (num_4x4_in_tu * csbf_strd);
3914
3915
3.02M
                ps_nbr_4x4 -= (num_4x4_in_tu << 1);
3916
3.02M
                ps_nbr_4x4 += (num_4x4_in_tu * num_4x4_in_cu);
3917
3.02M
                ps_top_nbr_4x4 = ps_nbr_4x4 - num_4x4_in_cu;
3918
3.02M
                ps_tmp_lt_4x4 = ps_left_nbr_4x4 + (num_4x4_in_tu * nbr_4x4_left_strd);
3919
3920
                /* decrement pos x to start */
3921
3.02M
                cu_pos_x -= (num_4x4_in_tu << 1);
3922
3.02M
                cu_pos_y += num_4x4_in_tu;
3923
3.02M
            }
3924
9.10M
        }
3925
3926
19.1M
#if RDOPT_ENABLE
3927
        /* compute the RDOPT cost for the current TU */
3928
19.1M
        ai8_cand_rdopt_cost[best_intra_buf_idx] += COMPUTE_RATE_COST_CLIP30(
3929
19.1M
            ai4_tu_bits[best_intra_buf_idx], ps_ctxt->i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
3930
19.1M
#endif
3931
3932
        /* accumulate the costs */
3933
19.1M
        total_rdopt_cost += ai8_cand_rdopt_cost[best_intra_buf_idx];
3934
3935
19.1M
        if(ps_ctxt->i4_bitrate_instance_num || ps_ctxt->i4_num_bitrates == 1)
3936
19.1M
        {
3937
            /* Early exit : If the current running cost exceeds
3938
            the prev. best mode cost, break */
3939
19.1M
            if(total_rdopt_cost > prev_best_rdopt_cost)
3940
991k
            {
3941
991k
                return (total_rdopt_cost);
3942
991k
            }
3943
19.1M
        }
3944
3945
        /* if transfrom size is 4x4 then only first luma 4x4 will have chroma*/
3946
18.1M
        chrm_present_flag = (4 != trans_size) ? 1 : INTRA_PRED_CHROMA_IDX_NONE;
3947
3948
18.1M
        pu4_nbr_flags++;
3949
18.1M
    }
3950
    /* Modify the cost function for this CU. */
3951
    /* loop in for 8x8 blocks */
3952
9.19M
    if(ps_ctxt->u1_enable_psyRDOPT)
3953
0
    {
3954
0
        UWORD8 *pu1_recon_cu;
3955
0
        WORD32 recon_stride;
3956
0
        WORD32 curr_pos_x;
3957
0
        WORD32 curr_pos_y;
3958
0
        WORD32 start_index;
3959
0
        WORD32 num_horz_cu_in_ctb;
3960
0
        WORD32 cu_size;
3961
0
        WORD32 had_block_size;
3962
3963
        /* tODO: sreenivasa ctb size has to be used appropriately */
3964
0
        had_block_size = 8;
3965
0
        cu_size = ps_cu_analyse->u1_cu_size; /* todo */
3966
0
        num_horz_cu_in_ctb = 64 / had_block_size;
3967
3968
0
        curr_pos_x = ps_cu_analyse->b3_cu_pos_x << 3; /* pel units */
3969
0
        curr_pos_y = ps_cu_analyse->b3_cu_pos_y << 3; /* pel units */
3970
0
        recon_stride = ps_final_prms->s_recon_datastore.i4_lumaRecon_stride;
3971
0
        pu1_recon_cu =
3972
0
            ((UWORD8 *)ps_final_prms->s_recon_datastore
3973
0
                 .apv_luma_recon_bufs[ps_recon_datastore->au1_bufId_with_winning_LumaRecon[0]]);
3974
        /* + \  curr_pos_x + curr_pos_y * recon_stride; */
3975
3976
        /* start index to index the source satd of curr cu int he current ctb*/
3977
0
        start_index =
3978
0
            (curr_pos_x / had_block_size) + (curr_pos_y / had_block_size) * num_horz_cu_in_ctb;
3979
3980
0
        {
3981
0
            total_rdopt_cost += ihevce_psy_rd_cost(
3982
0
                ps_ctxt->ai4_source_satd_8x8,
3983
0
                pu1_recon_cu,
3984
0
                recon_stride,
3985
0
                1,  //
3986
0
                cu_size,
3987
0
                0,  // pic type
3988
0
                0,  //layer id
3989
0
                ps_ctxt->i4_satd_lamda,  // lambda
3990
0
                start_index,
3991
0
                ps_ctxt->u1_is_input_data_hbd,
3992
0
                ps_ctxt->u4_psy_strength,
3993
0
                &ps_ctxt->s_cmn_opt_func
3994
3995
0
            );  // 8 bit
3996
0
        }
3997
0
    }
3998
3999
#if !FORCE_INTRA_TU_DEPTH_TO_0  //RATIONALISE_NUM_RDO_MODES_IN_PQ_AND_HQ
4000
9.19M
    if(TU_EQ_SUBCU == func_proc_mode)
4001
604k
    {
4002
604k
        UWORD8 au1_tu_eq_cu_div2_modes[4];
4003
604k
        UWORD8 au1_freq_of_mode[4];
4004
4005
604k
        WORD32 i4_num_clusters = ihevce_find_num_clusters_of_identical_points_1D(
4006
604k
            ps_final_prms->au1_intra_pred_mode, au1_tu_eq_cu_div2_modes, au1_freq_of_mode, 4);
4007
4008
604k
        if(1 == i4_num_clusters)
4009
146k
        {
4010
146k
            ps_final_prms->u2_num_pus_in_cu = 1;
4011
146k
            ps_final_prms->u1_part_mode = SIZE_2Nx2N;
4012
146k
        }
4013
604k
    }
4014
9.19M
#endif
4015
4016
    /* store the num TUs*/
4017
9.19M
    ps_final_prms->u2_num_tus_in_cu = u2_num_tus_in_cu;
4018
4019
    /* update the bytes consumed */
4020
9.19M
    ps_final_prms->i4_num_bytes_ecd_data = ecd_data_bytes_cons;
4021
4022
    /* store the current cu size to final prms */
4023
9.19M
    ps_final_prms->u1_cu_size = ps_cu_analyse->u1_cu_size;
4024
4025
    /* cu bits will be having luma residual bits till this point    */
4026
    /* if zero_cbf eval is disabled then cu bits will be zero       */
4027
9.19M
    ps_final_prms->u4_cu_luma_res_bits = cu_bits;
4028
4029
    /* ------------- Chroma processing -------------- */
4030
    /* Chroma rdopt eval for each luma candidate only for HIGH QUALITY/MEDIUM SPEDD preset*/
4031
9.19M
    if(1 == ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt)
4032
7.71M
    {
4033
7.71M
        LWORD64 chrm_rdopt_cost;
4034
7.71M
        WORD32 chrm_rdopt_tu_bits;
4035
4036
        /* Store the current RDOPT cost to enable early exit in chrom_prcs */
4037
7.71M
        ps_ctxt->as_cu_prms[curr_buf_idx].i8_curr_rdopt_cost = total_rdopt_cost;
4038
4039
7.71M
        chrm_rdopt_cost = ihevce_chroma_cu_prcs_rdopt(
4040
7.71M
            ps_ctxt,
4041
7.71M
            curr_buf_idx,
4042
7.71M
            func_proc_mode,
4043
7.71M
            ps_chrm_cu_buf_prms->pu1_curr_src,
4044
7.71M
            ps_chrm_cu_buf_prms->i4_chrm_src_stride,
4045
7.71M
            ps_chrm_cu_buf_prms->pu1_cu_left,
4046
7.71M
            ps_chrm_cu_buf_prms->pu1_cu_top,
4047
7.71M
            ps_chrm_cu_buf_prms->pu1_cu_top_left,
4048
7.71M
            ps_chrm_cu_buf_prms->i4_cu_left_stride,
4049
7.71M
            cu_pos_x_8pelunits,
4050
7.71M
            cu_pos_y_8pelunits,
4051
7.71M
            &chrm_rdopt_tu_bits,
4052
7.71M
            i4_alpha_stim_multiplier,
4053
7.71M
            u1_is_cu_noisy);
4054
4055
7.71M
#if WEIGH_CHROMA_COST
4056
7.71M
        chrm_rdopt_cost = (LWORD64)(
4057
7.71M
            (chrm_rdopt_cost * ps_ctxt->u4_chroma_cost_weighing_factor +
4058
7.71M
             (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
4059
7.71M
            CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
4060
7.71M
#endif
4061
4062
7.71M
#if CHROMA_RDOPT_ENABLE
4063
7.71M
        total_rdopt_cost += chrm_rdopt_cost;
4064
7.71M
#endif
4065
7.71M
        cu_bits += chrm_rdopt_tu_bits;
4066
4067
        /* cu bits for chroma residual if chroma rdopt is on       */
4068
        /* if zero_cbf eval is disabled then cu bits will be zero  */
4069
7.71M
        ps_final_prms->u4_cu_chroma_res_bits = chrm_rdopt_tu_bits;
4070
4071
7.71M
        if(ps_ctxt->i4_bitrate_instance_num || ps_ctxt->i4_num_bitrates == 1)
4072
7.71M
        {
4073
            /* Early exit : If the current running cost exceeds
4074
            the prev. best mode cost, break */
4075
7.71M
            if(total_rdopt_cost > prev_best_rdopt_cost)
4076
757k
            {
4077
757k
                return (total_rdopt_cost);
4078
757k
            }
4079
7.71M
        }
4080
7.71M
    }
4081
1.48M
    else
4082
1.48M
    {}
4083
4084
    /* RDOPT copy States :  Best after all luma TUs to current */
4085
8.44M
    COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
4086
8.44M
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
4087
8.44M
                .s_cabac_ctxt.au1_ctxt_models[0] +
4088
8.44M
            IHEVC_CAB_COEFFX_PREFIX,
4089
8.44M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
4090
8.44M
        IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
4091
4092
    /* get the neighbour availability flags for current cu  */
4093
8.44M
    ihevce_get_only_nbr_flag(
4094
8.44M
        &s_nbr,
4095
8.44M
        ps_ctxt->pu1_ctb_nbr_map,
4096
8.44M
        ps_ctxt->i4_nbr_map_strd,
4097
8.44M
        (cu_pos_x_8pelunits << 1),
4098
8.44M
        (cu_pos_y_8pelunits << 1),
4099
8.44M
        (trans_size << 1),
4100
8.44M
        (trans_size << 1));
4101
4102
    /* call the entropy rdo encode to get the bit estimate for current cu */
4103
    /*if ZERO_CBF eval is enabled then this function will return only CU header bits */
4104
8.44M
    {
4105
        /*cbf_bits will account for both texture and cbf bits when zero cbf eval flag is 0*/
4106
8.44M
        WORD32 cbf_bits, header_bits;
4107
4108
8.44M
        header_bits = ihevce_entropy_rdo_encode_cu(
4109
8.44M
            &ps_ctxt->s_rdopt_entropy_ctxt,
4110
8.44M
            ps_final_prms,
4111
8.44M
            cu_pos_x_8pelunits,
4112
8.44M
            cu_pos_y_8pelunits,
4113
8.44M
            ps_cu_analyse->u1_cu_size,
4114
8.44M
            s_nbr.u1_top_avail,
4115
8.44M
            s_nbr.u1_left_avail,
4116
8.44M
            &ps_final_prms->pu1_cu_coeffs[0],
4117
8.44M
            &cbf_bits);
4118
4119
8.44M
        cu_bits += header_bits;
4120
4121
        /* cbf bits are excluded from header bits, instead considered as texture bits */
4122
        /* incase if zero cbf eval is disabled then texture bits gets added here */
4123
8.44M
        ps_final_prms->u4_cu_hdr_bits = (header_bits - cbf_bits);
4124
8.44M
        ps_final_prms->u4_cu_cbf_bits = cbf_bits;
4125
4126
8.44M
#if RDOPT_ENABLE
4127
        /* add the cost of coding the cu bits */
4128
8.44M
        total_rdopt_cost +=
4129
8.44M
            COMPUTE_RATE_COST_CLIP30(header_bits, ps_ctxt->i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
4130
8.44M
#endif
4131
8.44M
    }
4132
8.44M
    return (total_rdopt_cost);
4133
9.19M
}
4134
/*!
4135
******************************************************************************
4136
* \if Function name : ihevce_inter_rdopt_cu_ntu \endif
4137
*
4138
* \brief
4139
*    Inter Coding unit funtion whic perfomr the TQ IT IQ recon for luma
4140
*
4141
* \param[in] ps_ctxt       enc_loop module ctxt pointer
4142
* \param[in] ps_inter_cand pointer to inter candidate structure
4143
* \param[in] pu1_src       pointer to source data buffer
4144
* \param[in] cu_size       Current CU size
4145
* \param[in] cu_pos_x      cu position x w.r.t to ctb
4146
* \param[in] cu_pos_y      cu position y w.r.t to ctb
4147
* \param[in] src_strd      source buffer stride
4148
* \param[in] curr_buf_idx  buffer index for current output storage
4149
* \param[in] ps_chrm_cu_buf_prms pointer to chroma buffer pointers structure
4150
*
4151
* \return
4152
*    Rdopt cost
4153
*
4154
* \author
4155
*  Ittiam
4156
*
4157
*****************************************************************************
4158
*/
4159
LWORD64 ihevce_inter_rdopt_cu_ntu(
4160
    ihevce_enc_loop_ctxt_t *ps_ctxt,
4161
    enc_loop_cu_prms_t *ps_cu_prms,
4162
    void *pv_src,
4163
    WORD32 cu_size,
4164
    WORD32 cu_pos_x,
4165
    WORD32 cu_pos_y,
4166
    WORD32 curr_buf_idx,
4167
    enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
4168
    cu_inter_cand_t *ps_inter_cand,
4169
    cu_analyse_t *ps_cu_analyse,
4170
    WORD32 i4_alpha_stim_multiplier)
4171
1.19M
{
4172
1.19M
    enc_loop_cu_final_prms_t *ps_final_prms;
4173
1.19M
    nbr_4x4_t *ps_nbr_4x4;
4174
1.19M
    tu_prms_t s_tu_prms[64 * 4];
4175
1.19M
    tu_prms_t *ps_tu_prms;
4176
4177
1.19M
    WORD32 i4_perform_rdoq;
4178
1.19M
    WORD32 i4_perform_sbh;
4179
1.19M
    WORD32 ai4_tu_split_flags[4];
4180
1.19M
    WORD32 ai4_tu_early_cbf[4];
4181
1.19M
    WORD32 num_split_flags = 1;
4182
1.19M
    WORD32 i;
4183
1.19M
    UWORD8 u1_tu_size;
4184
1.19M
    UWORD8 *pu1_pred;
4185
1.19M
    UWORD8 *pu1_ecd_data;
4186
1.19M
    WORD16 *pi2_deq_data;
4187
1.19M
    UWORD8 *pu1_csbf_buf;
4188
1.19M
    UWORD8 *pu1_tu_sz_sft;
4189
1.19M
    UWORD8 *pu1_tu_posx;
4190
1.19M
    UWORD8 *pu1_tu_posy;
4191
1.19M
    LWORD64 total_rdopt_cost;
4192
1.19M
    WORD32 ctr;
4193
1.19M
    WORD32 chrm_ctr;
4194
1.19M
    WORD32 num_tu_in_cu = 0;
4195
1.19M
    WORD32 pred_stride;
4196
1.19M
    WORD32 recon_stride;
4197
1.19M
    WORD32 trans_size = ps_cu_analyse->u1_cu_size;
4198
1.19M
    WORD32 csbf_strd;
4199
1.19M
    WORD32 chrm_present_flag;
4200
1.19M
    WORD32 ecd_data_bytes_cons;
4201
1.19M
    WORD32 num_4x4_in_cu;
4202
1.19M
    WORD32 num_4x4_in_tu;
4203
1.19M
    WORD32 recon_func_mode;
4204
1.19M
    WORD32 cu_bits;
4205
1.19M
    UWORD8 u1_compute_spatial_ssd;
4206
4207
    /* min_trans_size is initialized to some huge number than usual TU sizes */
4208
1.19M
    WORD32 i4_min_trans_size = 256;
4209
    /* Get the RDOPT cost of the best CU mode for early_exit */
4210
1.19M
    LWORD64 prev_best_rdopt_cost = ps_ctxt->as_cu_prms[!curr_buf_idx].i8_best_rdopt_cost;
4211
1.19M
    WORD32 src_strd = ps_cu_prms->i4_luma_src_stride;
4212
4213
    /* model for no residue syntax qt root cbf flag */
4214
1.19M
    UWORD8 u1_qtroot_cbf_cabac_model = ps_ctxt->au1_rdopt_init_ctxt_models[IHEVC_CAB_NORES_IDX];
4215
4216
    /* backup copy of cabac states for restoration if zero cu reside rdo wins later */
4217
1.19M
    UWORD8 au1_rdopt_init_ctxt_models[IHEVC_CAB_CTXT_END];
4218
4219
    /* for skip cases tables are not reqquired */
4220
1.19M
    UWORD8 u1_skip_tu_sz_sft = 0;
4221
1.19M
    UWORD8 u1_skip_tu_posx = 0;
4222
1.19M
    UWORD8 u1_skip_tu_posy = 0;
4223
1.19M
    UWORD8 u1_is_cu_noisy = ps_cu_prms->u1_is_cu_noisy;
4224
4225
    /* get the pointers based on curbuf idx */
4226
1.19M
    ps_final_prms = &ps_ctxt->as_cu_prms[curr_buf_idx];
4227
1.19M
    ps_nbr_4x4 = &ps_ctxt->as_cu_nbr[curr_buf_idx][0];
4228
1.19M
    pu1_ecd_data = &ps_final_prms->pu1_cu_coeffs[0];
4229
1.19M
    pi2_deq_data = &ps_final_prms->pi2_cu_deq_coeffs[0];
4230
1.19M
    csbf_strd = ps_ctxt->i4_cu_csbf_strd;
4231
1.19M
    pu1_csbf_buf = &ps_ctxt->au1_cu_csbf[0];
4232
4233
1.19M
    pred_stride = ps_inter_cand->i4_pred_data_stride;
4234
1.19M
    recon_stride = cu_size;
4235
1.19M
    pu1_pred = ps_inter_cand->pu1_pred_data;
4236
1.19M
    chrm_ctr = 0;
4237
1.19M
    ecd_data_bytes_cons = 0;
4238
1.19M
    total_rdopt_cost = 0;
4239
1.19M
    num_4x4_in_cu = cu_size >> 2;
4240
1.19M
    recon_func_mode = PRED_MODE_INTER;
4241
1.19M
    cu_bits = 0;
4242
4243
    /* get the 4x4 level postion of current cu */
4244
1.19M
    cu_pos_x = cu_pos_x << 1;
4245
1.19M
    cu_pos_y = cu_pos_y << 1;
4246
4247
    /* default value for cu coded flag */
4248
1.19M
    ps_final_prms->u1_is_cu_coded = 0;
4249
4250
    /*init of ssd of CU accuumulated over all TU*/
4251
1.19M
    ps_final_prms->u4_cu_sad = 0;
4252
4253
    /* populate the coeffs scan idx */
4254
1.19M
    ps_ctxt->i4_scan_idx = SCAN_DIAG_UPRIGHT;
4255
4256
1.19M
#if ENABLE_INTER_ZCU_COST
4257
    /* reset cu not coded cost */
4258
1.19M
    ps_ctxt->i8_cu_not_coded_cost = 0;
4259
4260
    /* backup copy of cabac states for restoration if zero cu reside rdo wins later */
4261
1.19M
    memcpy(au1_rdopt_init_ctxt_models, &ps_ctxt->au1_rdopt_init_ctxt_models[0], IHEVC_CAB_CTXT_END);
4262
1.19M
#endif
4263
4264
1.19M
    if(ps_cu_analyse->u1_cu_size == 64)
4265
48.0k
    {
4266
48.0k
        num_split_flags = 4;
4267
48.0k
        u1_tu_size = 32;
4268
48.0k
    }
4269
1.14M
    else
4270
1.14M
    {
4271
1.14M
        num_split_flags = 1;
4272
1.14M
        u1_tu_size = ps_cu_analyse->u1_cu_size;
4273
1.14M
    }
4274
4275
    /* ckeck for skip mode */
4276
1.19M
    if(1 == ps_final_prms->u1_skip_flag)
4277
396k
    {
4278
396k
        if(64 == cu_size)
4279
16.5k
        {
4280
            /* TU = CU/2 is set but no trnaform is evaluated  */
4281
16.5k
            num_tu_in_cu = 4;
4282
16.5k
            pu1_tu_sz_sft = &gau1_inter_tu_shft_amt[0];
4283
16.5k
            pu1_tu_posx = &gau1_inter_tu_posx_scl_amt[0];
4284
16.5k
            pu1_tu_posy = &gau1_inter_tu_posy_scl_amt[0];
4285
16.5k
        }
4286
380k
        else
4287
380k
        {
4288
            /* TU = CU is set but no trnaform is evaluated  */
4289
380k
            num_tu_in_cu = 1;
4290
380k
            pu1_tu_sz_sft = &u1_skip_tu_sz_sft;
4291
380k
            pu1_tu_posx = &u1_skip_tu_posx;
4292
380k
            pu1_tu_posy = &u1_skip_tu_posy;
4293
380k
        }
4294
4295
396k
        recon_func_mode = PRED_MODE_SKIP;
4296
396k
    }
4297
    /* check for PU part mode being AMP or No AMP */
4298
793k
    else if(ps_final_prms->u1_part_mode < SIZE_2NxnU)
4299
711k
    {
4300
711k
        if((SIZE_2Nx2N == ps_final_prms->u1_part_mode) && (cu_size < 64))
4301
681k
        {
4302
            /* TU= CU is evaluated 2Nx2N inter case */
4303
681k
            num_tu_in_cu = 1;
4304
681k
            pu1_tu_sz_sft = &u1_skip_tu_sz_sft;
4305
681k
            pu1_tu_posx = &u1_skip_tu_posx;
4306
681k
            pu1_tu_posy = &u1_skip_tu_posy;
4307
681k
        }
4308
30.0k
        else
4309
30.0k
        {
4310
            /* currently TU= CU/2 is evaluated for all inter case */
4311
30.0k
            num_tu_in_cu = 4;
4312
30.0k
            pu1_tu_sz_sft = &gau1_inter_tu_shft_amt[0];
4313
30.0k
            pu1_tu_posx = &gau1_inter_tu_posx_scl_amt[0];
4314
30.0k
            pu1_tu_posy = &gau1_inter_tu_posy_scl_amt[0];
4315
30.0k
        }
4316
711k
    }
4317
81.6k
    else
4318
81.6k
    {
4319
        /* for AMP cases one level of TU recurssion is done */
4320
        /* based on oreintation of the partitions           */
4321
81.6k
        num_tu_in_cu = 10;
4322
81.6k
        pu1_tu_sz_sft = &gau1_inter_tu_shft_amt_amp[ps_final_prms->u1_part_mode - 4][0];
4323
81.6k
        pu1_tu_posx = &gau1_inter_tu_posx_scl_amt_amp[ps_final_prms->u1_part_mode - 4][0];
4324
81.6k
        pu1_tu_posy = &gau1_inter_tu_posy_scl_amt_amp[ps_final_prms->u1_part_mode - 4][0];
4325
81.6k
    }
4326
4327
1.19M
    ps_tu_prms = &s_tu_prms[0];
4328
1.19M
    num_tu_in_cu = 0;
4329
4330
2.52M
    for(i = 0; i < num_split_flags; i++)
4331
1.33M
    {
4332
1.33M
        WORD32 i4_x_off = 0, i4_y_off = 0;
4333
4334
1.33M
        if(i == 1 || i == 3)
4335
96.0k
        {
4336
96.0k
            i4_x_off = 32;
4337
96.0k
        }
4338
4339
1.33M
        if(i == 2 || i == 3)
4340
96.0k
        {
4341
96.0k
            i4_y_off = 32;
4342
96.0k
        }
4343
4344
1.33M
        if(1 == ps_final_prms->u1_skip_flag)
4345
446k
        {
4346
446k
            ai4_tu_split_flags[0] = 0;
4347
446k
            ps_inter_cand->ai4_tu_split_flag[i] = 0;
4348
4349
446k
            ai4_tu_early_cbf[0] = 0;
4350
446k
        }
4351
887k
        else
4352
887k
        {
4353
887k
            ai4_tu_split_flags[0] = ps_inter_cand->ai4_tu_split_flag[i];
4354
887k
            ai4_tu_early_cbf[0] = ps_inter_cand->ai4_tu_early_cbf[i];
4355
887k
        }
4356
4357
1.33M
        ps_tu_prms->u1_tu_size = u1_tu_size;
4358
4359
1.33M
        ps_tu_prms = (tu_prms_t *)ihevce_tu_tree_update(
4360
1.33M
            ps_tu_prms,
4361
1.33M
            &num_tu_in_cu,
4362
1.33M
            0,
4363
1.33M
            ai4_tu_split_flags[0],
4364
1.33M
            ai4_tu_early_cbf[0],
4365
1.33M
            i4_x_off,
4366
1.33M
            i4_y_off);
4367
1.33M
    }
4368
4369
    /* loop for all tu blocks in current cu */
4370
1.19M
    ps_tu_prms = &s_tu_prms[0];
4371
3.53M
    for(ctr = 0; ctr < num_tu_in_cu; ctr++)
4372
2.34M
    {
4373
2.34M
        trans_size = ps_tu_prms->u1_tu_size;
4374
4375
2.34M
        if(i4_min_trans_size > trans_size)
4376
1.19M
        {
4377
1.19M
            i4_min_trans_size = trans_size;
4378
1.19M
        }
4379
2.34M
        ps_tu_prms++;
4380
2.34M
    }
4381
4382
1.19M
    if(ps_ctxt->i1_cu_qp_delta_enable)
4383
370k
    {
4384
370k
        ihevce_update_cu_level_qp_lamda(ps_ctxt, ps_cu_analyse, i4_min_trans_size, 0);
4385
370k
    }
4386
4387
1.19M
    if(u1_is_cu_noisy && !ps_ctxt->u1_enable_psyRDOPT)
4388
0
    {
4389
0
        ps_ctxt->i8_cl_ssd_lambda_qf =
4390
0
            ((float)ps_ctxt->i8_cl_ssd_lambda_qf * (100.0f - RDOPT_LAMBDA_DISCOUNT_WHEN_NOISY) /
4391
0
             100.0f);
4392
0
        ps_ctxt->i8_cl_ssd_lambda_chroma_qf =
4393
0
            ((float)ps_ctxt->i8_cl_ssd_lambda_chroma_qf *
4394
0
             (100.0f - RDOPT_LAMBDA_DISCOUNT_WHEN_NOISY) / 100.0f);
4395
0
    }
4396
4397
1.19M
    u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_QP_WHERE_SPATIAL_SSD_ENABLED) &&
4398
882k
                             (ps_ctxt->i4_quality_preset < IHEVCE_QUALITY_P3) &&
4399
157k
                             CONVERT_SSDS_TO_SPATIAL_DOMAIN;
4400
4401
1.19M
    if(u1_is_cu_noisy || ps_ctxt->u1_enable_psyRDOPT)
4402
0
    {
4403
0
        u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_HEVC_QP) &&
4404
0
                                 CONVERT_SSDS_TO_SPATIAL_DOMAIN;
4405
0
    }
4406
4407
1.19M
    if(!u1_compute_spatial_ssd)
4408
1.03M
    {
4409
1.03M
        ps_final_prms->s_recon_datastore.u1_is_lumaRecon_available = 0;
4410
1.03M
        ps_final_prms->s_recon_datastore.au1_is_chromaRecon_available[0] = 0;
4411
1.03M
    }
4412
157k
    else
4413
157k
    {
4414
157k
        ps_final_prms->s_recon_datastore.u1_is_lumaRecon_available = 1;
4415
157k
    }
4416
4417
1.19M
    ps_tu_prms = &s_tu_prms[0];
4418
4419
1.19M
    ASSERT(num_tu_in_cu <= 256);
4420
4421
    /* RDOPT copy States :  TU init (best until prev TU) to current */
4422
1.19M
    memcpy(
4423
1.19M
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
4424
1.19M
             .s_cabac_ctxt.au1_ctxt_models[0],
4425
1.19M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0],
4426
1.19M
        IHEVC_CAB_COEFFX_PREFIX);
4427
4428
3.39M
    for(ctr = 0; ctr < num_tu_in_cu; ctr++)
4429
2.32M
    {
4430
2.32M
        WORD32 curr_bytes;
4431
2.32M
        WORD32 tx_size;
4432
2.32M
        WORD32 cbf, zero_col, zero_row;
4433
2.32M
        LWORD64 rdopt_cost;
4434
2.32M
        UWORD8 u1_is_recon_available;
4435
4436
2.32M
        WORD32 curr_pos_x;
4437
2.32M
        WORD32 curr_pos_y;
4438
2.32M
        nbr_4x4_t *ps_cur_nbr_4x4;
4439
2.32M
        UWORD8 *pu1_cur_pred;
4440
2.32M
        UWORD8 *pu1_cur_src;
4441
2.32M
        UWORD8 *pu1_cur_recon;
4442
2.32M
        WORD16 *pi2_cur_deq_data;
4443
2.32M
        UWORD32 u4_tu_sad;
4444
2.32M
        WORD32 tu_bits;
4445
4446
2.32M
        WORD32 i4_recon_stride = ps_final_prms->s_recon_datastore.i4_lumaRecon_stride;
4447
4448
2.32M
        trans_size = ps_tu_prms->u1_tu_size;
4449
        /* get the current pos x and pos y in pixels */
4450
2.32M
        curr_pos_x = ps_tu_prms->u1_x_off;  //((cu_size >> 2) * pu1_tu_posx[ctr]);
4451
2.32M
        curr_pos_y = ps_tu_prms->u1_y_off;  //((cu_size >> 2) * pu1_tu_posy[ctr]);
4452
4453
2.32M
        num_4x4_in_tu = trans_size >> 2;
4454
4455
#if FORCE_8x8_TFR
4456
        if(cu_size == 64)
4457
        {
4458
            curr_pos_x = ((cu_size >> 3) * pu1_tu_posx[ctr]);
4459
            curr_pos_y = ((cu_size >> 3) * pu1_tu_posy[ctr]);
4460
        }
4461
#endif
4462
4463
        /* increment the pointers to start of current TU  */
4464
2.32M
        pu1_cur_src = ((UWORD8 *)pv_src + curr_pos_x);
4465
2.32M
        pu1_cur_src += (curr_pos_y * src_strd);
4466
2.32M
        pu1_cur_pred = (pu1_pred + curr_pos_x);
4467
2.32M
        pu1_cur_pred += (curr_pos_y * pred_stride);
4468
2.32M
        pi2_cur_deq_data = pi2_deq_data + curr_pos_x;
4469
2.32M
        pi2_cur_deq_data += (curr_pos_y * cu_size);
4470
2.32M
        pu1_cur_recon = ((UWORD8 *)ps_final_prms->s_recon_datastore.apv_luma_recon_bufs[0]) +
4471
2.32M
                        curr_pos_x + curr_pos_y * i4_recon_stride;
4472
4473
2.32M
        ps_cur_nbr_4x4 = (ps_nbr_4x4 + (curr_pos_x >> 2));
4474
2.32M
        ps_cur_nbr_4x4 += ((curr_pos_y >> 2) * num_4x4_in_cu);
4475
4476
        /* RDOPT copy States :  TU init (best until prev TU) to current */
4477
2.32M
        COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
4478
2.32M
            &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
4479
2.32M
                    .s_cabac_ctxt.au1_ctxt_models[0] +
4480
2.32M
                IHEVC_CAB_COEFFX_PREFIX,
4481
2.32M
            &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
4482
2.32M
            IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
4483
4484
2.32M
        i4_perform_rdoq = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_rdoq;
4485
2.32M
        i4_perform_sbh = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_sbh;
4486
4487
        /*2 Multi- dimensinal array based on trans size  of rounding factor to be added here */
4488
        /* arrays are for rounding factor corr. to 0-1 decision and 1-2 decision */
4489
        /* Currently the complete array will contain only single value*/
4490
        /*The rounding factor is calculated with the formula
4491
        Deadzone val = (((R1 - R0) * (2^(-8/3)) * lamMod) + 1)/2
4492
        rounding factor = (1 - DeadZone Val)
4493
4494
        Assumption: Cabac states of All the sub-blocks in the TU are considered independent
4495
        */
4496
2.32M
        if((ps_ctxt->i4_quant_rounding_level == TU_LEVEL_QUANT_ROUNDING) && (ctr != 0))
4497
0
        {
4498
0
            double i4_lamda_modifier;
4499
4500
0
            if((BSLICE == ps_ctxt->i1_slice_type) && (ps_ctxt->i4_temporal_layer_id))
4501
0
            {
4502
0
                i4_lamda_modifier = ps_ctxt->i4_lamda_modifier *
4503
0
                                    CLIP3((((double)(ps_ctxt->i4_cu_qp - 12)) / 6.0), 2.00, 4.00);
4504
0
            }
4505
0
            else
4506
0
            {
4507
0
                i4_lamda_modifier = ps_ctxt->i4_lamda_modifier;
4508
0
            }
4509
0
            if(ps_ctxt->i4_use_const_lamda_modifier)
4510
0
            {
4511
0
                if(ISLICE == ps_ctxt->i1_slice_type)
4512
0
                {
4513
0
                    i4_lamda_modifier = ps_ctxt->f_i_pic_lamda_modifier;
4514
0
                }
4515
0
                else
4516
0
                {
4517
0
                    i4_lamda_modifier = CONST_LAMDA_MOD_VAL;
4518
0
                }
4519
0
            }
4520
0
            ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3] =
4521
0
                &ps_ctxt->i4_quant_round_tu[0][0];
4522
0
            ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3] =
4523
0
                &ps_ctxt->i4_quant_round_tu[1][0];
4524
4525
0
            memset(
4526
0
                ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3],
4527
0
                0,
4528
0
                trans_size * trans_size * sizeof(WORD32));
4529
0
            memset(
4530
0
                ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3],
4531
0
                0,
4532
0
                trans_size * trans_size * sizeof(WORD32));
4533
4534
0
            ihevce_quant_rounding_factor_gen(
4535
0
                trans_size,
4536
0
                1,
4537
0
                &ps_ctxt->s_rdopt_entropy_ctxt,
4538
0
                ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3],
4539
0
                ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3],
4540
0
                i4_lamda_modifier,
4541
0
                1);
4542
0
        }
4543
2.32M
        else
4544
2.32M
        {
4545
2.32M
            ps_ctxt->pi4_quant_round_factor_tu_0_1[trans_size >> 3] =
4546
2.32M
                ps_ctxt->pi4_quant_round_factor_cu_ctb_0_1[trans_size >> 3];
4547
2.32M
            ps_ctxt->pi4_quant_round_factor_tu_1_2[trans_size >> 3] =
4548
2.32M
                ps_ctxt->pi4_quant_round_factor_cu_ctb_1_2[trans_size >> 3];
4549
2.32M
        }
4550
4551
        /* call T Q IT IQ and recon function */
4552
2.32M
        cbf = ihevce_t_q_iq_ssd_scan_fxn(
4553
2.32M
            ps_ctxt,
4554
2.32M
            pu1_cur_pred,
4555
2.32M
            pred_stride,
4556
2.32M
            pu1_cur_src,
4557
2.32M
            src_strd,
4558
2.32M
            pi2_cur_deq_data,
4559
2.32M
            cu_size,
4560
2.32M
            pu1_cur_recon,
4561
2.32M
            i4_recon_stride,
4562
2.32M
            pu1_ecd_data,
4563
2.32M
            pu1_csbf_buf,
4564
2.32M
            csbf_strd,
4565
2.32M
            trans_size,
4566
2.32M
            recon_func_mode,
4567
2.32M
            &rdopt_cost,
4568
2.32M
            &curr_bytes,
4569
2.32M
            &tu_bits,
4570
2.32M
            &u4_tu_sad,
4571
2.32M
            &zero_col,
4572
2.32M
            &zero_row,
4573
2.32M
            &u1_is_recon_available,
4574
2.32M
            i4_perform_rdoq,
4575
2.32M
            i4_perform_sbh,
4576
2.32M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
4577
2.32M
            i4_alpha_stim_multiplier,
4578
2.32M
            u1_is_cu_noisy,
4579
2.32M
#endif
4580
2.32M
            u1_compute_spatial_ssd ? SPATIAL_DOMAIN_SSD : FREQUENCY_DOMAIN_SSD,
4581
2.32M
            ps_ctxt->u1_use_early_cbf_data ? ps_tu_prms->i4_early_cbf : 1);
4582
4583
#if COMPUTE_NOISE_TERM_AT_THE_TU_LEVEL && !USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
4584
        if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
4585
        {
4586
#if !USE_RECON_TO_EVALUATE_STIM_IN_RDOPT
4587
            rdopt_cost = ihevce_inject_stim_into_distortion(
4588
                pu1_cur_src,
4589
                src_strd,
4590
                pu1_cur_pred,
4591
                pred_stride,
4592
                rdopt_cost,
4593
                i4_alpha_stim_multiplier,
4594
                trans_size,
4595
                0,
4596
                ps_ctxt->u1_enable_psyRDOPT,
4597
                NULL_PLANE);
4598
#else
4599
            if(u1_compute_spatial_ssd && u1_is_recon_available)
4600
            {
4601
                rdopt_cost = ihevce_inject_stim_into_distortion(
4602
                    pu1_cur_src,
4603
                    src_strd,
4604
                    pu1_cur_recon,
4605
                    i4_recon_stride,
4606
                    rdopt_cost,
4607
                    i4_alpha_stim_multiplier,
4608
                    trans_size,
4609
                    0,
4610
                    NULL_PLANE);
4611
            }
4612
            else
4613
            {
4614
                rdopt_cost = ihevce_inject_stim_into_distortion(
4615
                    pu1_cur_src,
4616
                    src_strd,
4617
                    pu1_cur_pred,
4618
                    pred_stride,
4619
                    rdopt_cost,
4620
                    i4_alpha_stim_multiplier,
4621
                    trans_size,
4622
                    0,
4623
                    ps_ctxt->u1_enable_psyRDOPT,
4624
                    NULL_PLANE);
4625
            }
4626
#endif
4627
        }
4628
#endif
4629
4630
2.32M
        if(u1_compute_spatial_ssd && u1_is_recon_available)
4631
257k
        {
4632
257k
            ps_final_prms->s_recon_datastore.au1_bufId_with_winning_LumaRecon[ctr] = 0;
4633
257k
        }
4634
2.06M
        else
4635
2.06M
        {
4636
2.06M
            ps_final_prms->s_recon_datastore.au1_bufId_with_winning_LumaRecon[ctr] = UCHAR_MAX;
4637
2.06M
        }
4638
4639
        /* accumulate the TU sad into cu sad */
4640
2.32M
        ps_final_prms->u4_cu_sad += u4_tu_sad;
4641
4642
        /* accumulate the TU bits into cu bits */
4643
2.32M
        cu_bits += tu_bits;
4644
4645
        /* inter cu is coded if any of the tu is coded in it */
4646
2.32M
        ps_final_prms->u1_is_cu_coded |= cbf;
4647
4648
        /* call the entropy function to get the bits */
4649
        /* add that to rd opt cost(SSD)              */
4650
4651
        /* update the bytes */
4652
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].i4_luma_coeff_offset = ecd_data_bytes_cons;
4653
2.32M
        ps_final_prms->as_tu_enc_loop_temp_prms[ctr].i2_luma_bytes_consumed = curr_bytes;
4654
        /* update the zero_row and col info for the final mode */
4655
2.32M
        ps_final_prms->as_tu_enc_loop_temp_prms[ctr].u4_luma_zero_col = zero_col;
4656
2.32M
        ps_final_prms->as_tu_enc_loop_temp_prms[ctr].u4_luma_zero_row = zero_row;
4657
4658
        /* update the bytes */
4659
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].i4_luma_coeff_offset = ecd_data_bytes_cons;
4660
4661
        /* update the total bytes cons */
4662
2.32M
        ecd_data_bytes_cons += curr_bytes;
4663
2.32M
        pu1_ecd_data += curr_bytes;
4664
4665
        /* RDOPT copy States :  New updated after curr TU to TU init */
4666
2.32M
        if(0 != cbf)
4667
421k
        {
4668
            /* update to new state only if CBF is non zero */
4669
421k
            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
4670
421k
                &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
4671
421k
                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
4672
421k
                        .s_cabac_ctxt.au1_ctxt_models[0] +
4673
421k
                    IHEVC_CAB_COEFFX_PREFIX,
4674
421k
                IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
4675
421k
        }
4676
4677
        /* by default chroma present is set to 1*/
4678
2.32M
        chrm_present_flag = 1;
4679
2.32M
        if(4 == trans_size)
4680
714k
        {
4681
            /* if tusize is 4x4 then only first luma 4x4 will have chroma*/
4682
714k
            if(0 != chrm_ctr)
4683
535k
            {
4684
535k
                chrm_present_flag = INTRA_PRED_CHROMA_IDX_NONE;
4685
535k
            }
4686
4687
            /* increment the chrm ctr unconditionally */
4688
714k
            chrm_ctr++;
4689
4690
            /* after ctr reached 4 reset it */
4691
714k
            if(4 == chrm_ctr)
4692
178k
            {
4693
178k
                chrm_ctr = 0;
4694
178k
            }
4695
714k
        }
4696
4697
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_y_cbf = cbf;
4698
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf = 0;
4699
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf = 0;
4700
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf_subtu1 = 0;
4701
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf_subtu1 = 0;
4702
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b3_chroma_intra_mode_idx = chrm_present_flag;
4703
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b7_qp = ps_ctxt->i4_cu_qp;
4704
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_first_tu_in_cu = 0;
4705
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_transquant_bypass = 0;
4706
2.32M
        GETRANGE(tx_size, trans_size);
4707
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b3_size = tx_size - 3;
4708
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_x = cu_pos_x + (curr_pos_x >> 2);
4709
2.32M
        ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_y = cu_pos_y + (curr_pos_y >> 2);
4710
4711
        /* repiclate the nbr 4x4 structure for all 4x4 blocks current TU */
4712
2.32M
        ps_cur_nbr_4x4->b1_y_cbf = cbf;
4713
        /*copy the cu qp. This will be overwritten by qp calculated based on skip flag at final stage of cu mode decide*/
4714
2.32M
        ps_cur_nbr_4x4->b8_qp = ps_ctxt->i4_cu_qp;
4715
4716
        /* Qp and cbf are stored for the all 4x4 in TU */
4717
2.32M
        {
4718
2.32M
            WORD32 i, j;
4719
2.32M
            nbr_4x4_t *ps_tmp_4x4;
4720
2.32M
            ps_tmp_4x4 = ps_cur_nbr_4x4;
4721
4722
10.5M
            for(i = 0; i < num_4x4_in_tu; i++)
4723
8.22M
            {
4724
54.2M
                for(j = 0; j < num_4x4_in_tu; j++)
4725
46.0M
                {
4726
46.0M
                    ps_tmp_4x4[j].b8_qp = ps_ctxt->i4_cu_qp;
4727
46.0M
                    ps_tmp_4x4[j].b1_y_cbf = cbf;
4728
46.0M
                }
4729
                /* row level update*/
4730
8.22M
                ps_tmp_4x4 += num_4x4_in_cu;
4731
8.22M
            }
4732
2.32M
        }
4733
4734
2.32M
#if RDOPT_ENABLE
4735
        /* compute the rdopt cost */
4736
2.32M
        rdopt_cost +=
4737
2.32M
            COMPUTE_RATE_COST_CLIP30(tu_bits, ps_ctxt->i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
4738
2.32M
#endif
4739
        /* accumulate the costs */
4740
2.32M
        total_rdopt_cost += rdopt_cost;
4741
4742
2.32M
        ps_tu_prms++;
4743
4744
2.32M
        if(ps_ctxt->i4_bitrate_instance_num || ps_ctxt->i4_num_bitrates == 1)
4745
2.32M
        {
4746
            /* Early exit : If the current running cost exceeds
4747
            the prev. best mode cost, break */
4748
2.32M
            if(total_rdopt_cost > prev_best_rdopt_cost)
4749
124k
            {
4750
124k
                return (total_rdopt_cost);
4751
124k
            }
4752
2.32M
        }
4753
2.32M
    }
4754
4755
    /* Modify the cost function for this CU. */
4756
    /* loop in for 8x8 blocks */
4757
1.06M
    if(ps_ctxt->u1_enable_psyRDOPT)
4758
0
    {
4759
0
        UWORD8 *pu1_recon_cu;
4760
0
        WORD32 recon_stride;
4761
0
        WORD32 curr_pos_x;
4762
0
        WORD32 curr_pos_y;
4763
0
        WORD32 start_index;
4764
0
        WORD32 num_horz_cu_in_ctb;
4765
0
        WORD32 had_block_size;
4766
4767
        /* tODO: sreenivasa ctb size has to be used appropriately */
4768
0
        had_block_size = 8;
4769
0
        num_horz_cu_in_ctb = 64 / had_block_size;
4770
4771
0
        curr_pos_x = cu_pos_x << 2; /* pel units */
4772
0
        curr_pos_y = cu_pos_y << 2; /* pel units */
4773
0
        recon_stride = ps_final_prms->s_recon_datastore.i4_lumaRecon_stride;
4774
0
        pu1_recon_cu = ((UWORD8 *)ps_final_prms->s_recon_datastore
4775
0
                            .apv_luma_recon_bufs[0]);  // already pointing to the current CU recon
4776
        //+ \curr_pos_x + curr_pos_y * recon_stride;
4777
4778
        /* start index to index the source satd of curr cu int he current ctb*/
4779
0
        start_index =
4780
0
            (curr_pos_x / had_block_size) + (curr_pos_y / had_block_size) * num_horz_cu_in_ctb;
4781
4782
0
        {
4783
0
            total_rdopt_cost += ihevce_psy_rd_cost(
4784
0
                ps_ctxt->ai4_source_satd_8x8,
4785
0
                pu1_recon_cu,
4786
0
                recon_stride,
4787
0
                1,  //howz stride
4788
0
                cu_size,
4789
0
                0,  // pic type
4790
0
                0,  //layer id
4791
0
                ps_ctxt->i4_satd_lamda,  // lambda
4792
0
                start_index,
4793
0
                ps_ctxt->u1_is_input_data_hbd,
4794
0
                ps_ctxt->u4_psy_strength,
4795
0
                &ps_ctxt->s_cmn_opt_func);  // 8 bit
4796
0
        }
4797
0
    }
4798
4799
    /* store the num TUs*/
4800
1.06M
    ps_final_prms->u2_num_tus_in_cu = num_tu_in_cu;
4801
4802
    /* update the bytes consumed */
4803
1.06M
    ps_final_prms->i4_num_bytes_ecd_data = ecd_data_bytes_cons;
4804
4805
    /* store the current cu size to final prms */
4806
1.06M
    ps_final_prms->u1_cu_size = cu_size;
4807
4808
    /* cu bits will be having luma residual bits till this point    */
4809
    /* if zero_cbf eval is disabled then cu bits will be zero       */
4810
1.06M
    ps_final_prms->u4_cu_luma_res_bits = cu_bits;
4811
4812
    /* ------------- Chroma processing -------------- */
4813
    /* Chroma rdopt eval for each luma candidate only for HIGH QUALITY/MEDIUM SPEDD preset*/
4814
1.06M
    if(1 == ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt)
4815
512k
    {
4816
512k
        LWORD64 chrm_rdopt_cost;
4817
512k
        WORD32 chrm_rdopt_tu_bits;
4818
4819
        /* Store the current RDOPT cost to enable early exit in chrom_prcs */
4820
512k
        ps_ctxt->as_cu_prms[curr_buf_idx].i8_curr_rdopt_cost = total_rdopt_cost;
4821
4822
512k
        chrm_rdopt_cost = ihevce_chroma_cu_prcs_rdopt(
4823
512k
            ps_ctxt,
4824
512k
            curr_buf_idx,
4825
512k
            0, /* TU mode : Don't care in Inter patrh */
4826
512k
            ps_chrm_cu_buf_prms->pu1_curr_src,
4827
512k
            ps_chrm_cu_buf_prms->i4_chrm_src_stride,
4828
512k
            ps_chrm_cu_buf_prms->pu1_cu_left,
4829
512k
            ps_chrm_cu_buf_prms->pu1_cu_top,
4830
512k
            ps_chrm_cu_buf_prms->pu1_cu_top_left,
4831
512k
            ps_chrm_cu_buf_prms->i4_cu_left_stride,
4832
512k
            (cu_pos_x >> 1),
4833
512k
            (cu_pos_y >> 1),
4834
512k
            &chrm_rdopt_tu_bits,
4835
512k
            i4_alpha_stim_multiplier,
4836
512k
            u1_is_cu_noisy);
4837
4838
512k
#if WEIGH_CHROMA_COST
4839
512k
        chrm_rdopt_cost = (LWORD64)(
4840
512k
            (chrm_rdopt_cost * ps_ctxt->u4_chroma_cost_weighing_factor +
4841
512k
             (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
4842
512k
            CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
4843
512k
#endif
4844
4845
512k
#if CHROMA_RDOPT_ENABLE
4846
512k
        total_rdopt_cost += chrm_rdopt_cost;
4847
512k
#endif
4848
512k
        cu_bits += chrm_rdopt_tu_bits;
4849
4850
        /* during chroma evaluation if skip decision was over written     */
4851
        /* then the current skip candidate is set to a non skip candidate */
4852
512k
        ps_inter_cand->b1_skip_flag = ps_final_prms->u1_skip_flag;
4853
4854
        /* cu bits for chroma residual if chroma rdopt is on       */
4855
        /* if zero_cbf eval is disabled then cu bits will be zero  */
4856
512k
        ps_final_prms->u4_cu_chroma_res_bits = chrm_rdopt_tu_bits;
4857
4858
512k
        if(ps_ctxt->i4_bitrate_instance_num || ps_ctxt->i4_num_bitrates == 1)
4859
512k
        {
4860
            /* Early exit : If the current running cost exceeds
4861
            the prev. best mode cost, break */
4862
512k
            if(total_rdopt_cost > prev_best_rdopt_cost)
4863
35.4k
            {
4864
35.4k
                return (total_rdopt_cost);
4865
35.4k
            }
4866
512k
        }
4867
512k
    }
4868
553k
    else
4869
553k
    {}
4870
4871
1.03M
#if SHRINK_INTER_TUTREE
4872
    /* ------------- Quadtree TU split  optimization ------------  */
4873
1.03M
    if(ps_final_prms->u1_is_cu_coded)
4874
178k
    {
4875
178k
        ps_final_prms->u2_num_tus_in_cu = ihevce_shrink_inter_tu_tree(
4876
178k
            &ps_final_prms->as_tu_enc_loop[0],
4877
178k
            &ps_final_prms->as_tu_enc_loop_temp_prms[0],
4878
178k
            &ps_final_prms->s_recon_datastore,
4879
178k
            num_tu_in_cu,
4880
178k
            (ps_ctxt->u1_chroma_array_type == 2));
4881
178k
    }
4882
1.03M
#endif
4883
4884
    /* RDOPT copy States :  Best after all luma TUs (and chroma,if enabled)to current */
4885
1.03M
    COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
4886
1.03M
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
4887
1.03M
                .s_cabac_ctxt.au1_ctxt_models[0] +
4888
1.03M
            IHEVC_CAB_COEFFX_PREFIX,
4889
1.03M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
4890
1.03M
        IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
4891
4892
    /* -------- Bit estimate for RD opt -------------- */
4893
1.03M
    {
4894
1.03M
        nbr_avail_flags_t s_nbr;
4895
        /*cbf_bits will account for both texture and cbf bits when zero cbf eval flag is 0*/
4896
1.03M
        WORD32 cbf_bits, header_bits;
4897
4898
        /* get the neighbour availability flags for current cu  */
4899
1.03M
        ihevce_get_only_nbr_flag(
4900
1.03M
            &s_nbr,
4901
1.03M
            ps_ctxt->pu1_ctb_nbr_map,
4902
1.03M
            ps_ctxt->i4_nbr_map_strd,
4903
1.03M
            cu_pos_x,
4904
1.03M
            cu_pos_y,
4905
1.03M
            (cu_size >> 2),
4906
1.03M
            (cu_size >> 2));
4907
4908
        /* call the entropy rdo encode to get the bit estimate for current cu */
4909
1.03M
        header_bits = ihevce_entropy_rdo_encode_cu(
4910
1.03M
            &ps_ctxt->s_rdopt_entropy_ctxt,
4911
1.03M
            ps_final_prms,
4912
1.03M
            (cu_pos_x >> 1), /*  back to 8x8 pel units   */
4913
1.03M
            (cu_pos_y >> 1), /*  back to 8x8 pel units   */
4914
1.03M
            cu_size,
4915
1.03M
            ps_ctxt->u1_disable_intra_eval ? !DISABLE_TOP_SYNC && s_nbr.u1_top_avail
4916
1.03M
                                           : s_nbr.u1_top_avail,
4917
1.03M
            s_nbr.u1_left_avail,
4918
1.03M
            &ps_final_prms->pu1_cu_coeffs[0],
4919
1.03M
            &cbf_bits);
4920
4921
1.03M
        cu_bits += header_bits;
4922
4923
        /* cbf bits are excluded from header bits, instead considered as texture bits */
4924
        /* incase if zero cbf eval is disabled then texture bits gets added here */
4925
1.03M
        ps_final_prms->u4_cu_hdr_bits = (header_bits - cbf_bits);
4926
1.03M
        ps_final_prms->u4_cu_cbf_bits = cbf_bits;
4927
4928
1.03M
#if RDOPT_ENABLE
4929
        /* add the cost of coding the header bits */
4930
1.03M
        total_rdopt_cost +=
4931
1.03M
            COMPUTE_RATE_COST_CLIP30(header_bits, ps_ctxt->i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
4932
4933
1.03M
#if ENABLE_INTER_ZCU_COST
4934
        /* If cu is coded, Evaluate not coded cost and check if it improves over coded cost */
4935
1.03M
        if(ps_final_prms->u1_is_cu_coded && (ZCBF_ENABLE == ps_ctxt->i4_zcbf_rdo_level))
4936
178k
        {
4937
178k
            LWORD64 i8_cu_not_coded_cost = ps_ctxt->i8_cu_not_coded_cost;
4938
4939
178k
            WORD32 is_2nx2n_mergecu = (SIZE_2Nx2N == ps_final_prms->u1_part_mode) &&
4940
148k
                                      (1 == ps_final_prms->as_pu_enc_loop[0].b1_merge_flag);
4941
4942
178k
            cab_ctxt_t *ps_cab_ctxt =
4943
178k
                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx].s_cabac_ctxt;
4944
4945
            /* Read header bits generatated after ihevce_entropy_rdo_encode_cu() call  */
4946
178k
            UWORD32 u4_cu_hdr_bits_q12 = ps_cab_ctxt->u4_header_bits_estimated_q12;
4947
4948
            /* account for coding qt_root_cbf = 0 */
4949
            /* First subtract cost for coding as 1 (part of header bits) and then add cost for coding as 0 */
4950
178k
            u4_cu_hdr_bits_q12 += gau2_ihevce_cabac_bin_to_bits[u1_qtroot_cbf_cabac_model ^ 0];
4951
178k
            if(u4_cu_hdr_bits_q12 < gau2_ihevce_cabac_bin_to_bits[u1_qtroot_cbf_cabac_model ^ 1])
4952
95
                u4_cu_hdr_bits_q12 = 0;
4953
178k
            else
4954
178k
                u4_cu_hdr_bits_q12 -= gau2_ihevce_cabac_bin_to_bits[u1_qtroot_cbf_cabac_model ^ 1];
4955
4956
            /* add the cost of coding the header bits */
4957
178k
            i8_cu_not_coded_cost += COMPUTE_RATE_COST_CLIP30(
4958
178k
                u4_cu_hdr_bits_q12 /* ps_final_prms->u4_cu_hdr_bits */,
4959
178k
                ps_ctxt->i8_cl_ssd_lambda_qf,
4960
178k
                (LAMBDA_Q_SHIFT + CABAC_FRAC_BITS_Q));
4961
4962
178k
            if(ps_ctxt->u1_enable_psyRDOPT)
4963
0
            {
4964
0
                i8_cu_not_coded_cost = total_rdopt_cost + 1;
4965
0
            }
4966
4967
            /* Evaluate qtroot cbf rdo; exclude 2Nx2N Merge as skip cu is explicitly evaluated */
4968
178k
            if((i8_cu_not_coded_cost <= total_rdopt_cost) && (!is_2nx2n_mergecu))
4969
3.28k
            {
4970
3.28k
                WORD32 tx_size;
4971
4972
                /* force cu as not coded and update the cost */
4973
3.28k
                ps_final_prms->u1_is_cu_coded = 0;
4974
3.28k
                ps_final_prms->s_recon_datastore.au1_is_chromaRecon_available[0] = 0;
4975
3.28k
                ps_final_prms->s_recon_datastore.u1_is_lumaRecon_available = 0;
4976
4977
3.28k
                total_rdopt_cost = i8_cu_not_coded_cost;
4978
4979
                /* reset num TUs to 1 unless cu size id 64 */
4980
3.28k
                ps_final_prms->u2_num_tus_in_cu = (64 == cu_size) ? 4 : 1;
4981
3.28k
                trans_size = (64 == cu_size) ? 32 : cu_size;
4982
3.28k
                GETRANGE(tx_size, trans_size);
4983
4984
                /* reset the bytes consumed */
4985
3.28k
                ps_final_prms->i4_num_bytes_ecd_data = 0;
4986
4987
                /* reset texture related bits and roll back header bits*/
4988
3.28k
                ps_final_prms->u4_cu_cbf_bits = 0;
4989
3.28k
                ps_final_prms->u4_cu_luma_res_bits = 0;
4990
3.28k
                ps_final_prms->u4_cu_chroma_res_bits = 0;
4991
3.28k
                ps_final_prms->u4_cu_hdr_bits =
4992
3.28k
                    (u4_cu_hdr_bits_q12 + (1 << (CABAC_FRAC_BITS_Q - 1))) >> CABAC_FRAC_BITS_Q;
4993
4994
                /* update cabac model with qtroot cbf = 0 decision */
4995
3.28k
                ps_cab_ctxt->au1_ctxt_models[IHEVC_CAB_NORES_IDX] =
4996
3.28k
                    gau1_ihevc_next_state[u1_qtroot_cbf_cabac_model << 1];
4997
4998
                /* restore untouched cabac models for, tusplit, cbfs, texture etc */
4999
3.28k
                memcpy(
5000
3.28k
                    &ps_cab_ctxt->au1_ctxt_models[IHEVC_CAB_SPLIT_TFM],
5001
3.28k
                    &au1_rdopt_init_ctxt_models[IHEVC_CAB_SPLIT_TFM],
5002
3.28k
                    (IHEVC_CAB_CTXT_END - IHEVC_CAB_SPLIT_TFM));
5003
5004
                /* mark all tus as not coded for final eval */
5005
10.4k
                for(ctr = 0; ctr < ps_final_prms->u2_num_tus_in_cu; ctr++)
5006
7.16k
                {
5007
7.16k
                    WORD32 curr_pos_x = (ctr & 0x1) ? (trans_size >> 2) : 0;
5008
7.16k
                    WORD32 curr_pos_y = (ctr & 0x2) ? (trans_size >> 2) : 0;
5009
5010
7.16k
                    nbr_4x4_t *ps_cur_nbr_4x4 =
5011
7.16k
                        ps_nbr_4x4 + curr_pos_x + (curr_pos_y * num_4x4_in_cu);
5012
5013
7.16k
                    num_4x4_in_tu = trans_size >> 2;
5014
5015
7.16k
                    ps_final_prms->as_tu_enc_loop_temp_prms[ctr].i2_luma_bytes_consumed = 0;
5016
7.16k
                    ps_final_prms->as_tu_enc_loop_temp_prms[ctr].ai2_cb_bytes_consumed[0] = 0;
5017
7.16k
                    ps_final_prms->as_tu_enc_loop_temp_prms[ctr].ai2_cr_bytes_consumed[0] = 0;
5018
5019
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_y_cbf = 0;
5020
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf = 0;
5021
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf = 0;
5022
5023
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf_subtu1 = 0;
5024
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf_subtu1 = 0;
5025
5026
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b3_size = tx_size - 3;
5027
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_x = cu_pos_x + curr_pos_x;
5028
7.16k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_y = cu_pos_y + curr_pos_y;
5029
5030
                    /* reset cbf for the all 4x4 in TU */
5031
7.16k
                    {
5032
7.16k
                        WORD32 i, j;
5033
7.16k
                        nbr_4x4_t *ps_tmp_4x4;
5034
7.16k
                        ps_tmp_4x4 = ps_cur_nbr_4x4;
5035
5036
58.9k
                        for(i = 0; i < num_4x4_in_tu; i++)
5037
51.7k
                        {
5038
446k
                            for(j = 0; j < num_4x4_in_tu; j++)
5039
394k
                            {
5040
394k
                                ps_tmp_4x4[j].b1_y_cbf = 0;
5041
394k
                            }
5042
                            /* row level update*/
5043
51.7k
                            ps_tmp_4x4 += num_4x4_in_cu;
5044
51.7k
                        }
5045
7.16k
                    }
5046
7.16k
                }
5047
3.28k
            }
5048
178k
        }
5049
1.03M
#endif /* ENABLE_INTER_ZCU_COST */
5050
5051
1.03M
#endif /* RDOPT_ENABLE */
5052
1.03M
    }
5053
5054
1.03M
    return (total_rdopt_cost);
5055
1.06M
}
5056
5057
#if ENABLE_RDO_BASED_TU_RECURSION
5058
LWORD64 ihevce_inter_tu_tree_selector_and_rdopt_cost_computer(
5059
    ihevce_enc_loop_ctxt_t *ps_ctxt,
5060
    enc_loop_cu_prms_t *ps_cu_prms,
5061
    void *pv_src,
5062
    WORD32 cu_size,
5063
    WORD32 cu_pos_x,
5064
    WORD32 cu_pos_y,
5065
    WORD32 curr_buf_idx,
5066
    enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
5067
    cu_inter_cand_t *ps_inter_cand,
5068
    cu_analyse_t *ps_cu_analyse,
5069
    WORD32 i4_alpha_stim_multiplier)
5070
3.75M
{
5071
3.75M
    tu_tree_node_t as_tu_nodes[256 + 64 + 16 + 4 + 1];
5072
3.75M
    buffer_data_for_tu_t s_buffer_data_for_tu;
5073
3.75M
    enc_loop_cu_final_prms_t *ps_final_prms;
5074
3.75M
    nbr_4x4_t *ps_nbr_4x4;
5075
5076
3.75M
    WORD32 num_split_flags = 1;
5077
3.75M
    UWORD8 u1_tu_size;
5078
3.75M
    UWORD8 *pu1_pred;
5079
3.75M
    UWORD8 *pu1_ecd_data;
5080
3.75M
    WORD16 *pi2_deq_data;
5081
3.75M
    UWORD8 *pu1_csbf_buf;
5082
3.75M
    UWORD8 *pu1_tu_sz_sft;
5083
3.75M
    UWORD8 *pu1_tu_posx;
5084
3.75M
    UWORD8 *pu1_tu_posy;
5085
3.75M
    LWORD64 total_rdopt_cost;
5086
3.75M
    WORD32 ctr;
5087
3.75M
    WORD32 chrm_ctr;
5088
3.75M
    WORD32 pred_stride;
5089
3.75M
    WORD32 recon_stride;
5090
3.75M
    WORD32 trans_size = ps_cu_analyse->u1_cu_size;
5091
3.75M
    WORD32 csbf_strd;
5092
3.75M
    WORD32 ecd_data_bytes_cons;
5093
3.75M
    WORD32 num_4x4_in_cu;
5094
3.75M
    WORD32 num_4x4_in_tu;
5095
3.75M
    WORD32 recon_func_mode;
5096
3.75M
    WORD32 cu_bits;
5097
3.75M
    UWORD8 u1_compute_spatial_ssd;
5098
    /* backup copy of cabac states for restoration if zero cu reside rdo wins later */
5099
3.75M
    UWORD8 au1_rdopt_init_ctxt_models[IHEVC_CAB_CTXT_END];
5100
5101
3.75M
    WORD32 i4_min_trans_size = 256;
5102
3.75M
    LWORD64 prev_best_rdopt_cost = ps_ctxt->as_cu_prms[!curr_buf_idx].i8_best_rdopt_cost;
5103
3.75M
    WORD32 src_strd = ps_cu_prms->i4_luma_src_stride;
5104
    /* model for no residue syntax qt root cbf flag */
5105
3.75M
    UWORD8 u1_qtroot_cbf_cabac_model = ps_ctxt->au1_rdopt_init_ctxt_models[IHEVC_CAB_NORES_IDX];
5106
3.75M
    UWORD8 u1_skip_tu_sz_sft = 0;
5107
3.75M
    UWORD8 u1_skip_tu_posx = 0;
5108
3.75M
    UWORD8 u1_skip_tu_posy = 0;
5109
3.75M
    UWORD8 u1_is_cu_noisy = ps_cu_prms->u1_is_cu_noisy;
5110
5111
3.75M
    ps_final_prms = &ps_ctxt->as_cu_prms[curr_buf_idx];
5112
3.75M
    ps_nbr_4x4 = &ps_ctxt->as_cu_nbr[curr_buf_idx][0];
5113
3.75M
    pu1_ecd_data = &ps_final_prms->pu1_cu_coeffs[0];
5114
3.75M
    pi2_deq_data = &ps_final_prms->pi2_cu_deq_coeffs[0];
5115
3.75M
    csbf_strd = ps_ctxt->i4_cu_csbf_strd;
5116
3.75M
    pu1_csbf_buf = &ps_ctxt->au1_cu_csbf[0];
5117
3.75M
    pred_stride = ps_inter_cand->i4_pred_data_stride;
5118
3.75M
    recon_stride = cu_size;
5119
3.75M
    pu1_pred = ps_inter_cand->pu1_pred_data;
5120
3.75M
    chrm_ctr = 0;
5121
3.75M
    ecd_data_bytes_cons = 0;
5122
3.75M
    total_rdopt_cost = 0;
5123
3.75M
    num_4x4_in_cu = cu_size >> 2;
5124
3.75M
    recon_func_mode = PRED_MODE_INTER;
5125
3.75M
    cu_bits = 0;
5126
5127
    /* get the 4x4 level postion of current cu */
5128
3.75M
    cu_pos_x = cu_pos_x << 1;
5129
3.75M
    cu_pos_y = cu_pos_y << 1;
5130
5131
3.75M
    ps_final_prms->u1_is_cu_coded = 0;
5132
3.75M
    ps_final_prms->u4_cu_sad = 0;
5133
5134
    /* populate the coeffs scan idx */
5135
3.75M
    ps_ctxt->i4_scan_idx = SCAN_DIAG_UPRIGHT;
5136
5137
3.75M
#if ENABLE_INTER_ZCU_COST
5138
    /* reset cu not coded cost */
5139
3.75M
    ps_ctxt->i8_cu_not_coded_cost = 0;
5140
5141
    /* backup copy of cabac states for restoration if zero cu reside rdo wins later */
5142
3.75M
    memcpy(au1_rdopt_init_ctxt_models, &ps_ctxt->au1_rdopt_init_ctxt_models[0], IHEVC_CAB_CTXT_END);
5143
3.75M
#endif
5144
5145
3.75M
    if(ps_cu_analyse->u1_cu_size == 64)
5146
44.7k
    {
5147
44.7k
        num_split_flags = 4;
5148
44.7k
        u1_tu_size = 32;
5149
44.7k
    }
5150
3.71M
    else
5151
3.71M
    {
5152
3.71M
        num_split_flags = 1;
5153
3.71M
        u1_tu_size = ps_cu_analyse->u1_cu_size;
5154
3.71M
    }
5155
5156
3.75M
    if(1 == ps_final_prms->u1_skip_flag)
5157
1.07M
    {
5158
1.07M
        if(64 == cu_size)
5159
11.7k
        {
5160
            /* TU = CU/2 is set but no trnaform is evaluated  */
5161
11.7k
            pu1_tu_sz_sft = &gau1_inter_tu_shft_amt[0];
5162
11.7k
            pu1_tu_posx = &gau1_inter_tu_posx_scl_amt[0];
5163
11.7k
            pu1_tu_posy = &gau1_inter_tu_posy_scl_amt[0];
5164
11.7k
        }
5165
1.06M
        else
5166
1.06M
        {
5167
            /* TU = CU is set but no trnaform is evaluated  */
5168
1.06M
            pu1_tu_sz_sft = &u1_skip_tu_sz_sft;
5169
1.06M
            pu1_tu_posx = &u1_skip_tu_posx;
5170
1.06M
            pu1_tu_posy = &u1_skip_tu_posy;
5171
1.06M
        }
5172
5173
1.07M
        recon_func_mode = PRED_MODE_SKIP;
5174
1.07M
    }
5175
    /* check for PU part mode being AMP or No AMP */
5176
2.67M
    else if(ps_final_prms->u1_part_mode < SIZE_2NxnU)
5177
2.01M
    {
5178
2.01M
        if((SIZE_2Nx2N == ps_final_prms->u1_part_mode) && (cu_size < 64))
5179
1.89M
        {
5180
            /* TU= CU is evaluated 2Nx2N inter case */
5181
1.89M
            pu1_tu_sz_sft = &u1_skip_tu_sz_sft;
5182
1.89M
            pu1_tu_posx = &u1_skip_tu_posx;
5183
1.89M
            pu1_tu_posy = &u1_skip_tu_posy;
5184
1.89M
        }
5185
115k
        else
5186
115k
        {
5187
            /* currently TU= CU/2 is evaluated for all inter case */
5188
115k
            pu1_tu_sz_sft = &gau1_inter_tu_shft_amt[0];
5189
115k
            pu1_tu_posx = &gau1_inter_tu_posx_scl_amt[0];
5190
115k
            pu1_tu_posy = &gau1_inter_tu_posy_scl_amt[0];
5191
115k
        }
5192
2.01M
    }
5193
666k
    else
5194
666k
    {
5195
        /* for AMP cases one level of TU recurssion is done */
5196
        /* based on oreintation of the partitions           */
5197
666k
        pu1_tu_sz_sft = &gau1_inter_tu_shft_amt_amp[ps_final_prms->u1_part_mode - 4][0];
5198
666k
        pu1_tu_posx = &gau1_inter_tu_posx_scl_amt_amp[ps_final_prms->u1_part_mode - 4][0];
5199
666k
        pu1_tu_posy = &gau1_inter_tu_posy_scl_amt_amp[ps_final_prms->u1_part_mode - 4][0];
5200
666k
    }
5201
5202
3.75M
    i4_min_trans_size = 4;
5203
5204
3.75M
    if(ps_ctxt->i1_cu_qp_delta_enable)
5205
1.49M
    {
5206
1.49M
        ihevce_update_cu_level_qp_lamda(ps_ctxt, ps_cu_analyse, i4_min_trans_size, 0);
5207
1.49M
    }
5208
5209
3.75M
    if(u1_is_cu_noisy && !ps_ctxt->u1_enable_psyRDOPT)
5210
0
    {
5211
0
        ps_ctxt->i8_cl_ssd_lambda_qf =
5212
0
            ((float)ps_ctxt->i8_cl_ssd_lambda_qf * (100.0f - RDOPT_LAMBDA_DISCOUNT_WHEN_NOISY) /
5213
0
             100.0f);
5214
0
        ps_ctxt->i8_cl_ssd_lambda_chroma_qf =
5215
0
            ((float)ps_ctxt->i8_cl_ssd_lambda_chroma_qf *
5216
0
             (100.0f - RDOPT_LAMBDA_DISCOUNT_WHEN_NOISY) / 100.0f);
5217
0
    }
5218
5219
3.75M
    u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_QP_WHERE_SPATIAL_SSD_ENABLED) &&
5220
2.42M
                             (ps_ctxt->i4_quality_preset < IHEVCE_QUALITY_P3) &&
5221
2.42M
                             CONVERT_SSDS_TO_SPATIAL_DOMAIN;
5222
5223
3.75M
    if(u1_is_cu_noisy || ps_ctxt->u1_enable_psyRDOPT)
5224
0
    {
5225
0
        u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_HEVC_QP) &&
5226
0
                                 CONVERT_SSDS_TO_SPATIAL_DOMAIN;
5227
0
    }
5228
5229
3.75M
    if(!u1_compute_spatial_ssd)
5230
1.33M
    {
5231
1.33M
        ps_final_prms->s_recon_datastore.u1_is_lumaRecon_available = 0;
5232
1.33M
        ps_final_prms->s_recon_datastore.au1_is_chromaRecon_available[0] = 0;
5233
1.33M
    }
5234
2.42M
    else
5235
2.42M
    {
5236
2.42M
        ps_final_prms->s_recon_datastore.u1_is_lumaRecon_available = 1;
5237
5238
2.42M
        if(INCLUDE_CHROMA_DURING_TU_RECURSION && (ps_ctxt->i4_quality_preset <= IHEVCE_QUALITY_P0))
5239
0
        {
5240
0
            ps_final_prms->s_recon_datastore.au1_is_chromaRecon_available[0] = 1;
5241
0
        }
5242
2.42M
    }
5243
5244
    /* RDOPT copy States :  TU init (best until prev TU) to current */
5245
3.75M
    memcpy(
5246
3.75M
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
5247
3.75M
             .s_cabac_ctxt.au1_ctxt_models[0],
5248
3.75M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0],
5249
3.75M
        IHEVC_CAB_COEFFX_PREFIX);
5250
5251
3.75M
    ihevce_tu_tree_init(
5252
3.75M
        as_tu_nodes,
5253
3.75M
        cu_size,
5254
3.75M
        (cu_size == 64) ? !ps_inter_cand->b1_skip_flag : 0,
5255
3.75M
        ps_inter_cand->b1_skip_flag ? 0 : ps_ctxt->u1_max_inter_tr_depth,
5256
3.75M
        INCLUDE_CHROMA_DURING_TU_RECURSION && (ps_ctxt->i4_quality_preset <= IHEVCE_QUALITY_P0),
5257
3.75M
        ps_ctxt->u1_chroma_array_type == 2);
5258
5259
3.75M
    if(!ps_inter_cand->b1_skip_flag && (ps_ctxt->i4_quality_preset >= IHEVCE_QUALITY_P3))
5260
0
    {
5261
0
        ihevce_tuSplitArray_to_tuTree_mapper(
5262
0
            as_tu_nodes,
5263
0
            ps_inter_cand->ai4_tu_split_flag,
5264
0
            cu_size,
5265
0
            cu_size,
5266
0
            MAX(MIN_TU_SIZE, (cu_size >> ps_ctxt->u1_max_inter_tr_depth)),
5267
0
            MIN(MAX_TU_SIZE, cu_size),
5268
0
            ps_inter_cand->b1_skip_flag);
5269
0
    }
5270
5271
3.75M
    ASSERT(ihevce_tu_tree_coverage_in_cu(as_tu_nodes) == cu_size * cu_size);
5272
5273
3.75M
#if ENABLE_INTER_ZCU_COST
5274
3.75M
    ps_ctxt->i8_cu_not_coded_cost = 0;
5275
3.75M
#endif
5276
5277
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_luma.pv_src = pv_src;
5278
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_luma.pv_pred = pu1_pred;
5279
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_luma.pv_recon =
5280
3.75M
        ps_final_prms->s_recon_datastore.apv_luma_recon_bufs[0];
5281
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_luma.i4_src_stride = src_strd;
5282
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_luma.i4_pred_stride = pred_stride;
5283
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_luma.i4_recon_stride =
5284
3.75M
        ps_final_prms->s_recon_datastore.i4_lumaRecon_stride;
5285
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.pv_src = ps_chrm_cu_buf_prms->pu1_curr_src;
5286
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.pv_pred =
5287
3.75M
        ps_ctxt->s_cu_me_intra_pred_prms.pu1_pred_data[CU_ME_INTRA_PRED_CHROMA_IDX] +
5288
3.75M
        curr_buf_idx * ((MAX_CTB_SIZE * MAX_CTB_SIZE >> 1) + ((ps_ctxt->u1_chroma_array_type == 2) *
5289
3.75M
                                                              (MAX_CTB_SIZE * MAX_CTB_SIZE >> 1)));
5290
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.pv_recon =
5291
3.75M
        ps_final_prms->s_recon_datastore.apv_chroma_recon_bufs[0];
5292
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.i4_src_stride =
5293
3.75M
        ps_chrm_cu_buf_prms->i4_chrm_src_stride;
5294
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.i4_pred_stride =
5295
3.75M
        ps_ctxt->s_cu_me_intra_pred_prms.ai4_pred_data_stride[CU_ME_INTRA_PRED_CHROMA_IDX];
5296
3.75M
    s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.i4_recon_stride =
5297
3.75M
        ps_final_prms->s_recon_datastore.i4_chromaRecon_stride;
5298
3.75M
    s_buffer_data_for_tu.ps_nbr_data_buf = ps_nbr_4x4;
5299
3.75M
    s_buffer_data_for_tu.pi2_deq_data = pi2_deq_data;
5300
3.75M
    s_buffer_data_for_tu.pi2_deq_data_chroma =
5301
3.75M
        pi2_deq_data + ps_final_prms->i4_chrm_deq_coeff_strt_idx;
5302
3.75M
    s_buffer_data_for_tu.i4_nbr_data_buf_stride = num_4x4_in_cu;
5303
3.75M
    s_buffer_data_for_tu.i4_deq_data_stride = cu_size;
5304
3.75M
    s_buffer_data_for_tu.i4_deq_data_stride_chroma = cu_size;
5305
3.75M
    s_buffer_data_for_tu.ppu1_ecd = &pu1_ecd_data;
5306
5307
3.75M
    if(INCLUDE_CHROMA_DURING_TU_RECURSION && (ps_ctxt->i4_quality_preset <= IHEVCE_QUALITY_P0))
5308
0
    {
5309
0
        UWORD8 i;
5310
5311
0
        UWORD8 *pu1_pred = (UWORD8 *)s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.pv_pred;
5312
5313
0
        for(i = 0; i < (!!ps_inter_cand->b3_part_size) + 1; i++)
5314
0
        {
5315
0
            pu_t *ps_pu;
5316
5317
0
            WORD32 inter_pu_wd;
5318
0
            WORD32 inter_pu_ht;
5319
5320
0
            ps_pu = ps_inter_cand->as_inter_pu + i;
5321
5322
0
            inter_pu_wd = (ps_pu->b4_wd + 1) << 2; /* cb and cr pixel interleaved */
5323
0
            inter_pu_ht = ((ps_pu->b4_ht + 1) << 2) >> 1;
5324
0
            inter_pu_ht <<= (ps_ctxt->u1_chroma_array_type == 2);
5325
0
            ihevce_chroma_inter_pred_pu(
5326
0
                &ps_ctxt->s_mc_ctxt,
5327
0
                ps_pu,
5328
0
                pu1_pred,
5329
0
                s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.i4_pred_stride);
5330
0
            if(!!ps_inter_cand->b3_part_size)
5331
0
            {
5332
                /* 2Nx__ partion case */
5333
0
                if(inter_pu_wd == cu_size)
5334
0
                {
5335
0
                    pu1_pred +=
5336
0
                        (inter_pu_ht *
5337
0
                         s_buffer_data_for_tu.s_src_pred_rec_buf_chroma.i4_pred_stride);
5338
0
                }
5339
5340
                /* __x2N partion case */
5341
0
                if(inter_pu_ht == (cu_size >> !(ps_ctxt->u1_chroma_array_type == 2)))
5342
0
                {
5343
0
                    pu1_pred += inter_pu_wd;
5344
0
                }
5345
0
            }
5346
0
        }
5347
0
    }
5348
5349
#if !ENABLE_TOP_DOWN_TU_RECURSION
5350
    total_rdopt_cost = ihevce_tu_tree_selector(
5351
        ps_ctxt,
5352
        as_tu_nodes,
5353
        &s_buffer_data_for_tu,
5354
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
5355
             .s_cabac_ctxt.au1_ctxt_models[0],
5356
        recon_func_mode,
5357
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
5358
        i4_alpha_stim_multiplier,
5359
        u1_is_cu_noisy,
5360
#endif
5361
        0,
5362
        ps_ctxt->u1_max_inter_tr_depth,
5363
        ps_inter_cand->b3_part_size,
5364
        u1_compute_spatial_ssd);
5365
#else
5366
3.75M
    total_rdopt_cost = ihevce_topDown_tu_tree_selector(
5367
3.75M
        ps_ctxt,
5368
3.75M
        as_tu_nodes,
5369
3.75M
        &s_buffer_data_for_tu,
5370
3.75M
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
5371
3.75M
             .s_cabac_ctxt.au1_ctxt_models[0],
5372
3.75M
        recon_func_mode,
5373
3.75M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
5374
3.75M
        i4_alpha_stim_multiplier,
5375
3.75M
        u1_is_cu_noisy,
5376
3.75M
#endif
5377
3.75M
        0,
5378
3.75M
        ps_ctxt->u1_max_inter_tr_depth,
5379
3.75M
        ps_inter_cand->b3_part_size,
5380
3.75M
        INCLUDE_CHROMA_DURING_TU_RECURSION && (ps_ctxt->i4_quality_preset <= IHEVCE_QUALITY_P0),
5381
3.75M
        u1_compute_spatial_ssd);
5382
3.75M
#endif
5383
5384
3.75M
    ps_final_prms->u2_num_tus_in_cu = 0;
5385
3.75M
    ps_final_prms->u4_cu_luma_res_bits = 0;
5386
3.75M
    ps_final_prms->u4_cu_sad = 0;
5387
3.75M
    total_rdopt_cost = 0;
5388
3.75M
    ecd_data_bytes_cons = 0;
5389
3.75M
    cu_bits = 0;
5390
3.75M
#if ENABLE_INTER_ZCU_COST
5391
3.75M
    ps_ctxt->i8_cu_not_coded_cost = 0;
5392
3.75M
#endif
5393
3.75M
    ps_final_prms->u1_is_cu_coded = 0;
5394
3.75M
    ps_final_prms->u1_cu_size = cu_size;
5395
5396
3.75M
    ihevce_tu_selector_debriefer(
5397
3.75M
        as_tu_nodes,
5398
3.75M
        ps_final_prms,
5399
3.75M
        &total_rdopt_cost,
5400
3.75M
#if ENABLE_INTER_ZCU_COST
5401
3.75M
        &ps_ctxt->i8_cu_not_coded_cost,
5402
3.75M
#endif
5403
3.75M
        &ecd_data_bytes_cons,
5404
3.75M
        &cu_bits,
5405
3.75M
        &ps_final_prms->u2_num_tus_in_cu,
5406
3.75M
        ps_ctxt->i4_cu_qp,
5407
3.75M
        cu_pos_x * 4,
5408
3.75M
        cu_pos_y * 4,
5409
3.75M
        INCLUDE_CHROMA_DURING_TU_RECURSION && (ps_ctxt->i4_quality_preset <= IHEVCE_QUALITY_P0),
5410
3.75M
        (ps_ctxt->u1_chroma_array_type == 2),
5411
3.75M
        POS_TL);
5412
5413
3.75M
    if(!(INCLUDE_CHROMA_DURING_TU_RECURSION && (ps_ctxt->i4_quality_preset <= IHEVCE_QUALITY_P0)))
5414
3.75M
    {
5415
3.75M
        ps_final_prms->i4_chrm_cu_coeff_strt_idx = ecd_data_bytes_cons;
5416
3.75M
    }
5417
5418
    /* Modify the cost function for this CU. */
5419
    /* loop in for 8x8 blocks */
5420
3.75M
    if(ps_ctxt->u1_enable_psyRDOPT)
5421
0
    {
5422
0
        UWORD8 *pu1_recon_cu;
5423
0
        WORD32 recon_stride;
5424
0
        WORD32 curr_pos_x;
5425
0
        WORD32 curr_pos_y;
5426
0
        WORD32 start_index;
5427
0
        WORD32 num_horz_cu_in_ctb;
5428
0
        WORD32 had_block_size;
5429
5430
        /* tODO: sreenivasa ctb size has to be used appropriately */
5431
0
        had_block_size = 8;
5432
0
        num_horz_cu_in_ctb = 64 / had_block_size;
5433
5434
0
        curr_pos_x = cu_pos_x << 2; /* pel units */
5435
0
        curr_pos_y = cu_pos_y << 2; /* pel units */
5436
0
        recon_stride = ps_final_prms->s_recon_datastore.i4_lumaRecon_stride;
5437
0
        pu1_recon_cu = ((UWORD8 *)ps_final_prms->s_recon_datastore
5438
0
                            .apv_luma_recon_bufs[0]);  // already pointing to the current CU recon
5439
        //+ \curr_pos_x + curr_pos_y * recon_stride;
5440
5441
        /* start index to index the source satd of curr cu int he current ctb*/
5442
0
        start_index =
5443
0
            (curr_pos_x / had_block_size) + (curr_pos_y / had_block_size) * num_horz_cu_in_ctb;
5444
5445
0
        {
5446
0
            total_rdopt_cost += ihevce_psy_rd_cost(
5447
0
                ps_ctxt->ai4_source_satd_8x8,
5448
0
                pu1_recon_cu,
5449
0
                recon_stride,
5450
0
                1,  //howz stride
5451
0
                cu_size,
5452
0
                0,  // pic type
5453
0
                0,  //layer id
5454
0
                ps_ctxt->i4_satd_lamda,  // lambda
5455
0
                start_index,
5456
0
                ps_ctxt->u1_is_input_data_hbd,
5457
0
                ps_ctxt->u4_psy_strength,
5458
0
                &ps_ctxt->s_cmn_opt_func);  // 8 bit
5459
0
        }
5460
0
    }
5461
5462
3.75M
    ps_final_prms->u1_chroma_intra_pred_mode = 4;
5463
5464
    /* update the bytes consumed */
5465
3.75M
    ps_final_prms->i4_num_bytes_ecd_data = ecd_data_bytes_cons;
5466
5467
    /* store the current cu size to final prms */
5468
3.75M
    ps_final_prms->u1_cu_size = cu_size;
5469
    /* ------------- Chroma processing -------------- */
5470
    /* Chroma rdopt eval for each luma candidate only for HIGH QUALITY/MEDIUM SPEDD preset*/
5471
3.75M
    if(ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt &&
5472
3.75M
       !(INCLUDE_CHROMA_DURING_TU_RECURSION && (ps_ctxt->i4_quality_preset <= IHEVCE_QUALITY_P0)))
5473
3.75M
    {
5474
3.75M
        LWORD64 chrm_rdopt_cost;
5475
3.75M
        WORD32 chrm_rdopt_tu_bits;
5476
5477
        /* Store the current RDOPT cost to enable early exit in chrom_prcs */
5478
3.75M
        ps_ctxt->as_cu_prms[curr_buf_idx].i8_curr_rdopt_cost = total_rdopt_cost;
5479
5480
3.75M
        chrm_rdopt_cost = ihevce_chroma_cu_prcs_rdopt(
5481
3.75M
            ps_ctxt,
5482
3.75M
            curr_buf_idx,
5483
3.75M
            0, /* TU mode : Don't care in Inter patrh */
5484
3.75M
            ps_chrm_cu_buf_prms->pu1_curr_src,
5485
3.75M
            ps_chrm_cu_buf_prms->i4_chrm_src_stride,
5486
3.75M
            ps_chrm_cu_buf_prms->pu1_cu_left,
5487
3.75M
            ps_chrm_cu_buf_prms->pu1_cu_top,
5488
3.75M
            ps_chrm_cu_buf_prms->pu1_cu_top_left,
5489
3.75M
            ps_chrm_cu_buf_prms->i4_cu_left_stride,
5490
3.75M
            (cu_pos_x >> 1),
5491
3.75M
            (cu_pos_y >> 1),
5492
3.75M
            &chrm_rdopt_tu_bits,
5493
3.75M
            i4_alpha_stim_multiplier,
5494
3.75M
            u1_is_cu_noisy);
5495
5496
3.75M
#if WEIGH_CHROMA_COST
5497
3.75M
        chrm_rdopt_cost = (LWORD64)(
5498
3.75M
            (chrm_rdopt_cost * ps_ctxt->u4_chroma_cost_weighing_factor +
5499
3.75M
             (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
5500
3.75M
            CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
5501
3.75M
#endif
5502
5503
3.75M
#if CHROMA_RDOPT_ENABLE
5504
3.75M
        total_rdopt_cost += chrm_rdopt_cost;
5505
3.75M
#endif
5506
3.75M
        cu_bits += chrm_rdopt_tu_bits;
5507
5508
        /* during chroma evaluation if skip decision was over written     */
5509
        /* then the current skip candidate is set to a non skip candidate */
5510
3.75M
        ps_inter_cand->b1_skip_flag = ps_final_prms->u1_skip_flag;
5511
5512
        /* cu bits for chroma residual if chroma rdopt is on       */
5513
        /* if zero_cbf eval is disabled then cu bits will be zero  */
5514
3.75M
        ps_final_prms->u4_cu_chroma_res_bits = chrm_rdopt_tu_bits;
5515
5516
3.75M
        if(ps_ctxt->i4_bitrate_instance_num || ps_ctxt->i4_num_bitrates == 1)
5517
3.75M
        {
5518
            /* Early exit : If the current running cost exceeds
5519
            the prev. best mode cost, break */
5520
3.75M
            if(total_rdopt_cost > prev_best_rdopt_cost)
5521
451k
            {
5522
451k
                return (total_rdopt_cost);
5523
451k
            }
5524
3.75M
        }
5525
3.75M
    }
5526
0
    else
5527
0
    {}
5528
5529
3.30M
#if SHRINK_INTER_TUTREE
5530
    /* ------------- Quadtree TU split  optimization ------------  */
5531
3.30M
    if(ps_final_prms->u1_is_cu_coded)
5532
387k
    {
5533
387k
        ps_final_prms->u2_num_tus_in_cu = ihevce_shrink_inter_tu_tree(
5534
387k
            &ps_final_prms->as_tu_enc_loop[0],
5535
387k
            &ps_final_prms->as_tu_enc_loop_temp_prms[0],
5536
387k
            &ps_final_prms->s_recon_datastore,
5537
387k
            ps_final_prms->u2_num_tus_in_cu,
5538
387k
            (ps_ctxt->u1_chroma_array_type == 2));
5539
387k
    }
5540
3.30M
#endif
5541
5542
    /* RDOPT copy States :  Best after all luma TUs (and chroma,if enabled)to current */
5543
3.30M
    COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
5544
3.30M
        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx]
5545
3.30M
                .s_cabac_ctxt.au1_ctxt_models[0] +
5546
3.30M
            IHEVC_CAB_COEFFX_PREFIX,
5547
3.30M
        &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
5548
3.30M
        IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
5549
5550
    /* -------- Bit estimate for RD opt -------------- */
5551
3.30M
    {
5552
3.30M
        nbr_avail_flags_t s_nbr;
5553
        /*cbf_bits will account for both texture and cbf bits when zero cbf eval flag is 0*/
5554
3.30M
        WORD32 cbf_bits, header_bits;
5555
5556
        /* get the neighbour availability flags for current cu  */
5557
3.30M
        ihevce_get_only_nbr_flag(
5558
3.30M
            &s_nbr,
5559
3.30M
            ps_ctxt->pu1_ctb_nbr_map,
5560
3.30M
            ps_ctxt->i4_nbr_map_strd,
5561
3.30M
            cu_pos_x,
5562
3.30M
            cu_pos_y,
5563
3.30M
            (cu_size >> 2),
5564
3.30M
            (cu_size >> 2));
5565
5566
        /* call the entropy rdo encode to get the bit estimate for current cu */
5567
3.30M
        header_bits = ihevce_entropy_rdo_encode_cu(
5568
3.30M
            &ps_ctxt->s_rdopt_entropy_ctxt,
5569
3.30M
            ps_final_prms,
5570
3.30M
            (cu_pos_x >> 1), /*  back to 8x8 pel units   */
5571
3.30M
            (cu_pos_y >> 1), /*  back to 8x8 pel units   */
5572
3.30M
            cu_size,
5573
3.30M
            ps_ctxt->u1_disable_intra_eval ? !DISABLE_TOP_SYNC && s_nbr.u1_top_avail
5574
3.30M
                                           : s_nbr.u1_top_avail,
5575
3.30M
            s_nbr.u1_left_avail,
5576
3.30M
            &ps_final_prms->pu1_cu_coeffs[0],
5577
3.30M
            &cbf_bits);
5578
5579
3.30M
        cu_bits += header_bits;
5580
5581
        /* cbf bits are excluded from header bits, instead considered as texture bits */
5582
        /* incase if zero cbf eval is disabled then texture bits gets added here */
5583
3.30M
        ps_final_prms->u4_cu_hdr_bits = (header_bits - cbf_bits);
5584
3.30M
        ps_final_prms->u4_cu_cbf_bits = cbf_bits;
5585
5586
3.30M
#if RDOPT_ENABLE
5587
        /* add the cost of coding the header bits */
5588
3.30M
        total_rdopt_cost +=
5589
3.30M
            COMPUTE_RATE_COST_CLIP30(header_bits, ps_ctxt->i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
5590
5591
3.30M
#if ENABLE_INTER_ZCU_COST
5592
        /* If cu is coded, Evaluate not coded cost and check if it improves over coded cost */
5593
3.30M
        if(ps_final_prms->u1_is_cu_coded && (ZCBF_ENABLE == ps_ctxt->i4_zcbf_rdo_level))
5594
387k
        {
5595
387k
            LWORD64 i8_cu_not_coded_cost = ps_ctxt->i8_cu_not_coded_cost;
5596
5597
387k
            WORD32 is_2nx2n_mergecu = (SIZE_2Nx2N == ps_final_prms->u1_part_mode) &&
5598
219k
                                      (1 == ps_final_prms->as_pu_enc_loop[0].b1_merge_flag);
5599
5600
387k
            cab_ctxt_t *ps_cab_ctxt =
5601
387k
                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[curr_buf_idx].s_cabac_ctxt;
5602
5603
            /* Read header bits generatated after ihevce_entropy_rdo_encode_cu() call  */
5604
387k
            UWORD32 u4_cu_hdr_bits_q12 = ps_cab_ctxt->u4_header_bits_estimated_q12;
5605
5606
            /* account for coding qt_root_cbf = 0 */
5607
            /* First subtract cost for coding as 1 (part of header bits) and then add cost for coding as 0 */
5608
387k
            u4_cu_hdr_bits_q12 += gau2_ihevce_cabac_bin_to_bits[u1_qtroot_cbf_cabac_model ^ 0];
5609
387k
            if(u4_cu_hdr_bits_q12 < gau2_ihevce_cabac_bin_to_bits[u1_qtroot_cbf_cabac_model ^ 1])
5610
2.31k
                u4_cu_hdr_bits_q12 = 0;
5611
384k
            else
5612
384k
                u4_cu_hdr_bits_q12 -= gau2_ihevce_cabac_bin_to_bits[u1_qtroot_cbf_cabac_model ^ 1];
5613
5614
            /* add the cost of coding the header bits */
5615
387k
            i8_cu_not_coded_cost += COMPUTE_RATE_COST_CLIP30(
5616
387k
                u4_cu_hdr_bits_q12 /* ps_final_prms->u4_cu_hdr_bits */,
5617
387k
                ps_ctxt->i8_cl_ssd_lambda_qf,
5618
387k
                (LAMBDA_Q_SHIFT + CABAC_FRAC_BITS_Q));
5619
5620
387k
            if(ps_ctxt->u1_enable_psyRDOPT)
5621
0
            {
5622
0
                i8_cu_not_coded_cost = total_rdopt_cost + 1;
5623
0
            }
5624
5625
            /* Evaluate qtroot cbf rdo; exclude 2Nx2N Merge as skip cu is explicitly evaluated */
5626
387k
            if((i8_cu_not_coded_cost <= total_rdopt_cost) && (!is_2nx2n_mergecu))
5627
5.48k
            {
5628
5.48k
                WORD32 tx_size;
5629
5630
                /* force cu as not coded and update the cost */
5631
5.48k
                ps_final_prms->u1_is_cu_coded = 0;
5632
5.48k
                ps_final_prms->s_recon_datastore.au1_is_chromaRecon_available[0] = 0;
5633
5.48k
                ps_final_prms->s_recon_datastore.u1_is_lumaRecon_available = 0;
5634
5635
5.48k
                total_rdopt_cost = i8_cu_not_coded_cost;
5636
5637
                /* reset num TUs to 1 unless cu size id 64 */
5638
5.48k
                ps_final_prms->u2_num_tus_in_cu = (64 == cu_size) ? 4 : 1;
5639
5.48k
                trans_size = (64 == cu_size) ? 32 : cu_size;
5640
5.48k
                GETRANGE(tx_size, trans_size);
5641
5642
                /* reset the bytes consumed */
5643
5.48k
                ps_final_prms->i4_num_bytes_ecd_data = 0;
5644
5645
                /* reset texture related bits and roll back header bits*/
5646
5.48k
                ps_final_prms->u4_cu_cbf_bits = 0;
5647
5.48k
                ps_final_prms->u4_cu_luma_res_bits = 0;
5648
5.48k
                ps_final_prms->u4_cu_chroma_res_bits = 0;
5649
5.48k
                ps_final_prms->u4_cu_hdr_bits =
5650
5.48k
                    (u4_cu_hdr_bits_q12 + (1 << (CABAC_FRAC_BITS_Q - 1))) >> CABAC_FRAC_BITS_Q;
5651
5652
                /* update cabac model with qtroot cbf = 0 decision */
5653
5.48k
                ps_cab_ctxt->au1_ctxt_models[IHEVC_CAB_NORES_IDX] =
5654
5.48k
                    gau1_ihevc_next_state[u1_qtroot_cbf_cabac_model << 1];
5655
5656
                /* restore untouched cabac models for, tusplit, cbfs, texture etc */
5657
5.48k
                memcpy(
5658
5.48k
                    &ps_cab_ctxt->au1_ctxt_models[IHEVC_CAB_SPLIT_TFM],
5659
5.48k
                    &au1_rdopt_init_ctxt_models[IHEVC_CAB_SPLIT_TFM],
5660
5.48k
                    (IHEVC_CAB_CTXT_END - IHEVC_CAB_SPLIT_TFM));
5661
5662
                /* mark all tus as not coded for final eval */
5663
13.6k
                for(ctr = 0; ctr < ps_final_prms->u2_num_tus_in_cu; ctr++)
5664
8.13k
                {
5665
8.13k
                    WORD32 curr_pos_x = (ctr & 0x1) ? (trans_size >> 2) : 0;
5666
8.13k
                    WORD32 curr_pos_y = (ctr & 0x2) ? (trans_size >> 2) : 0;
5667
5668
8.13k
                    nbr_4x4_t *ps_cur_nbr_4x4 =
5669
8.13k
                        ps_nbr_4x4 + curr_pos_x + (curr_pos_y * num_4x4_in_cu);
5670
5671
8.13k
                    num_4x4_in_tu = trans_size >> 2;
5672
5673
8.13k
                    ps_final_prms->as_tu_enc_loop_temp_prms[ctr].i2_luma_bytes_consumed = 0;
5674
8.13k
                    ps_final_prms->as_tu_enc_loop_temp_prms[ctr].ai2_cb_bytes_consumed[0] = 0;
5675
8.13k
                    ps_final_prms->as_tu_enc_loop_temp_prms[ctr].ai2_cr_bytes_consumed[0] = 0;
5676
5677
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_y_cbf = 0;
5678
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf = 0;
5679
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf = 0;
5680
5681
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cr_cbf_subtu1 = 0;
5682
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b1_cb_cbf_subtu1 = 0;
5683
5684
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b3_size = tx_size - 3;
5685
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_x = cu_pos_x + curr_pos_x;
5686
8.13k
                    ps_final_prms->as_tu_enc_loop[ctr].s_tu.b4_pos_y = cu_pos_y + curr_pos_y;
5687
5688
                    /* reset cbf for the all 4x4 in TU */
5689
8.13k
                    {
5690
8.13k
                        WORD32 i, j;
5691
8.13k
                        nbr_4x4_t *ps_tmp_4x4;
5692
8.13k
                        ps_tmp_4x4 = ps_cur_nbr_4x4;
5693
5694
58.3k
                        for(i = 0; i < num_4x4_in_tu; i++)
5695
50.2k
                        {
5696
404k
                            for(j = 0; j < num_4x4_in_tu; j++)
5697
353k
                            {
5698
353k
                                ps_tmp_4x4[j].b1_y_cbf = 0;
5699
353k
                            }
5700
                            /* row level update*/
5701
50.2k
                            ps_tmp_4x4 += num_4x4_in_cu;
5702
50.2k
                        }
5703
8.13k
                    }
5704
8.13k
                }
5705
5.48k
            }
5706
387k
        }
5707
3.30M
#endif /* ENABLE_INTER_ZCU_COST */
5708
5709
3.30M
#endif /* RDOPT_ENABLE */
5710
3.30M
    }
5711
5712
3.30M
    return (total_rdopt_cost);
5713
3.75M
}
5714
#endif
5715
5716
/*!
5717
******************************************************************************
5718
* \if Function name : ihevce_inter_rdopt_cu_mc_mvp \endif
5719
*
5720
* \brief
5721
*    Inter Coding unit funtion which performs MC and MVP calc for RD opt mode
5722
*
5723
* \param[in] ps_ctxt       enc_loop module ctxt pointer
5724
* \param[in] ps_inter_cand pointer to inter candidate structure
5725
* \param[in] cu_size         Current CU size
5726
* \param[in] cu_pos_x        cu position x w.r.t to ctb
5727
* \param[in] cu_pos_y        cu position y w.r.t to ctb
5728
* \param[in] ps_left_nbr_4x4 Left neighbour 4x4 structure pointer
5729
* \param[in] ps_top_nbr_4x4  top neighbour 4x4 structure pointer
5730
* \param[in] ps_topleft_nbr_4x4  top left neighbour 4x4 structure pointer
5731
* \param[in] nbr_4x4_left_strd  left neighbour 4x4 buffer stride
5732
* \param[in] curr_buf_idx Current Buffer index
5733
*
5734
* \return
5735
*    Rdopt cost
5736
*
5737
* \author
5738
*  Ittiam
5739
*
5740
*****************************************************************************
5741
*/
5742
LWORD64 ihevce_inter_rdopt_cu_mc_mvp(
5743
    ihevce_enc_loop_ctxt_t *ps_ctxt,
5744
    cu_inter_cand_t *ps_inter_cand,
5745
    WORD32 cu_size,
5746
    WORD32 cu_pos_x,
5747
    WORD32 cu_pos_y,
5748
    nbr_4x4_t *ps_left_nbr_4x4,
5749
    nbr_4x4_t *ps_top_nbr_4x4,
5750
    nbr_4x4_t *ps_topleft_nbr_4x4,
5751
    WORD32 nbr_4x4_left_strd,
5752
    WORD32 curr_buf_idx)
5753
4.94M
{
5754
    /* local variables */
5755
4.94M
    enc_loop_cu_final_prms_t *ps_final_prms;
5756
4.94M
    nbr_avail_flags_t s_nbr;
5757
4.94M
    nbr_4x4_t *ps_nbr_4x4;
5758
5759
4.94M
    UWORD8 au1_is_top_used[2][MAX_MVP_LIST_CAND];
5760
4.94M
    UWORD8 *pu1_pred;
5761
4.94M
    WORD32 rdopt_cost;
5762
4.94M
    WORD32 ctr;
5763
4.94M
    WORD32 num_cu_part;
5764
4.94M
    WORD32 inter_pu_wd;
5765
4.94M
    WORD32 inter_pu_ht;
5766
4.94M
    WORD32 pred_stride;
5767
5768
    /* get the pointers based on curbuf idx */
5769
4.94M
    ps_nbr_4x4 = &ps_ctxt->as_cu_nbr[curr_buf_idx][0];
5770
4.94M
    ps_final_prms = &ps_ctxt->as_cu_prms[curr_buf_idx];
5771
4.94M
    pu1_pred = ps_inter_cand->pu1_pred_data;
5772
5773
4.94M
    pred_stride = ps_inter_cand->i4_pred_data_stride;
5774
5775
    /* store the partition mode in final prms */
5776
4.94M
    ps_final_prms->u1_part_mode = ps_inter_cand->b3_part_size;
5777
5778
    /* since encoder does not support NXN part type */
5779
    /* num parts can be either 1 or 2 only          */
5780
4.94M
    ASSERT(SIZE_NxN != ps_inter_cand->b3_part_size);
5781
5782
4.94M
    num_cu_part = (SIZE_2Nx2N != ps_inter_cand->b3_part_size) + 1;
5783
5784
    /* get the 4x4 level position of current cu */
5785
4.94M
    cu_pos_x = cu_pos_x << 1;
5786
4.94M
    cu_pos_y = cu_pos_y << 1;
5787
5788
    /* populate cu level params */
5789
4.94M
    ps_final_prms->u1_intra_flag = PRED_MODE_INTER;
5790
4.94M
    ps_final_prms->u2_num_pus_in_cu = num_cu_part;
5791
5792
    /* run a loop over all the partitons in cu */
5793
10.7M
    for(ctr = 0; ctr < num_cu_part; ctr++)
5794
5.80M
    {
5795
5.80M
        pu_mv_t as_pred_mv[MAX_MVP_LIST_CAND];
5796
5.80M
        pu_t *ps_pu;
5797
5.80M
        WORD32 skip_or_merge_flag;
5798
5.80M
        UWORD8 u1_use_mvp_from_top_row;
5799
5800
5.80M
        ps_pu = &ps_inter_cand->as_inter_pu[ctr];
5801
5802
        /* IF AMP then each partitions can have diff wd ht */
5803
5.80M
        inter_pu_wd = (ps_pu->b4_wd + 1) << 2;
5804
5.80M
        inter_pu_ht = (ps_pu->b4_ht + 1) << 2;
5805
5806
        /* populate reference pic buf id for bs compute */
5807
5808
        /* L0 */
5809
5.80M
        if(-1 != ps_pu->mv.i1_l0_ref_idx)
5810
5.26M
        {
5811
5.26M
            ps_pu->mv.i1_l0_ref_pic_buf_id =
5812
5.26M
                ps_ctxt->s_mv_pred_ctxt.ps_ref_list[0][ps_pu->mv.i1_l0_ref_idx]->i4_buf_id;
5813
5.26M
        }
5814
5815
        /* L1 */
5816
5.80M
        if(-1 != ps_pu->mv.i1_l1_ref_idx)
5817
2.38M
        {
5818
2.38M
            ps_pu->mv.i1_l1_ref_pic_buf_id =
5819
2.38M
                ps_ctxt->s_mv_pred_ctxt.ps_ref_list[1][ps_pu->mv.i1_l1_ref_idx]->i4_buf_id;
5820
2.38M
        }
5821
5822
        /* SKIP or merge check for every part */
5823
5.80M
        skip_or_merge_flag = ps_inter_cand->b1_skip_flag | ps_pu->b1_merge_flag;
5824
5825
        /* ----------- MV Prediction ----------------- */
5826
5.80M
        if(0 == skip_or_merge_flag)
5827
1.50M
        {
5828
            /* get the neighbour availability flags */
5829
1.50M
            ihevce_get_only_nbr_flag(
5830
1.50M
                &s_nbr,
5831
1.50M
                ps_ctxt->pu1_ctb_nbr_map,
5832
1.50M
                ps_ctxt->i4_nbr_map_strd,
5833
1.50M
                cu_pos_x,
5834
1.50M
                cu_pos_y,
5835
1.50M
                inter_pu_wd >> 2,
5836
1.50M
                inter_pu_ht >> 2);
5837
5838
1.50M
            if(ps_ctxt->u1_disable_intra_eval && DISABLE_TOP_SYNC && (ps_pu->b4_pos_y == 0))
5839
0
            {
5840
0
                u1_use_mvp_from_top_row = 0;
5841
0
            }
5842
1.50M
            else
5843
1.50M
            {
5844
1.50M
                u1_use_mvp_from_top_row = 1;
5845
1.50M
            }
5846
5847
1.50M
            if(!u1_use_mvp_from_top_row)
5848
0
            {
5849
0
                if(s_nbr.u1_top_avail || s_nbr.u1_top_lt_avail || s_nbr.u1_top_rt_avail)
5850
0
                {
5851
0
                    if(!s_nbr.u1_left_avail && !s_nbr.u1_bot_lt_avail)
5852
0
                    {
5853
0
                        WORD32 curr_cu_pos_in_row, cu_top_right_offset, cu_top_right_dep_pos;
5854
5855
                        /* Ensure Top Right Sync */
5856
0
                        if(!ps_ctxt->u1_use_top_at_ctb_boundary)
5857
0
                        {
5858
0
                            curr_cu_pos_in_row =
5859
0
                                ps_ctxt->s_mc_ctxt.i4_ctb_frm_pos_x + (cu_pos_x << 2);
5860
5861
0
                            if(ps_ctxt->s_mc_ctxt.i4_ctb_frm_pos_y == 0)
5862
0
                            {
5863
                                /* No wait for 1st row */
5864
0
                                cu_top_right_offset = -(MAX_CTB_SIZE);
5865
0
                                {
5866
0
                                    ihevce_tile_params_t *ps_col_tile_params =
5867
0
                                        ((ihevce_tile_params_t *)ps_ctxt->pv_tile_params_base +
5868
0
                                         ps_ctxt->i4_tile_col_idx);
5869
5870
                                    /* No wait for 1st row */
5871
0
                                    cu_top_right_offset =
5872
0
                                        -(ps_col_tile_params->i4_first_sample_x + (MAX_CTB_SIZE));
5873
0
                                }
5874
0
                                cu_top_right_dep_pos = 0;
5875
0
                            }
5876
0
                            else
5877
0
                            {
5878
0
                                cu_top_right_offset = (cu_size) + 4;
5879
0
                                cu_top_right_dep_pos =
5880
0
                                    (ps_ctxt->s_mc_ctxt.i4_ctb_frm_pos_y >> 6) - 1;
5881
0
                            }
5882
5883
0
                            ihevce_dmgr_chk_row_row_sync(
5884
0
                                ps_ctxt->pv_dep_mngr_enc_loop_cu_top_right,
5885
0
                                curr_cu_pos_in_row,
5886
0
                                cu_top_right_offset,
5887
0
                                cu_top_right_dep_pos,
5888
0
                                ps_ctxt->i4_tile_col_idx, /* Col Tile No. */
5889
0
                                ps_ctxt->thrd_id);
5890
0
                        }
5891
5892
0
                        u1_use_mvp_from_top_row = 1;
5893
0
                    }
5894
0
                    else
5895
0
                    {
5896
0
                        s_nbr.u1_top_avail = 0;
5897
0
                        s_nbr.u1_top_lt_avail = 0;
5898
0
                        s_nbr.u1_top_rt_avail = 0;
5899
0
                    }
5900
0
                }
5901
0
                else
5902
0
                {
5903
0
                    u1_use_mvp_from_top_row = 1;
5904
0
                }
5905
0
            }
5906
            /* Call the MV prediction module to get MVP */
5907
1.50M
            ihevce_mv_pred(
5908
1.50M
                &ps_ctxt->s_mv_pred_ctxt,
5909
1.50M
                ps_top_nbr_4x4,
5910
1.50M
                ps_left_nbr_4x4,
5911
1.50M
                ps_topleft_nbr_4x4,
5912
1.50M
                nbr_4x4_left_strd,
5913
1.50M
                &s_nbr,
5914
1.50M
                NULL, /* colocated MV */
5915
1.50M
                ps_pu,
5916
1.50M
                &as_pred_mv[0],
5917
1.50M
                au1_is_top_used);
5918
1.50M
        }
5919
5920
        /* store the nbr 4x4 structure */
5921
5.80M
        ps_nbr_4x4->b1_skip_flag = ps_inter_cand->b1_skip_flag;
5922
5.80M
        ps_nbr_4x4->b1_intra_flag = 0;
5923
5.80M
        ps_nbr_4x4->b1_pred_l0_flag = 0;
5924
5.80M
        ps_nbr_4x4->b1_pred_l1_flag = 0;
5925
5926
        /* DC is default mode for inter cu, required for intra mode signalling */
5927
5.80M
        ps_nbr_4x4->b6_luma_intra_mode = 1;
5928
5929
        /* copy the motion vectors to neighbour structure */
5930
5.80M
        ps_nbr_4x4->mv = ps_pu->mv;
5931
5932
        /* copy the PU to final out pu */
5933
5.80M
        ps_final_prms->as_pu_enc_loop[ctr] = *ps_pu;
5934
5935
        /* copy the PU to chroma */
5936
5.80M
        ps_final_prms->as_pu_chrm_proc[ctr] = *ps_pu;
5937
5938
        /* store the skip flag to final prms */
5939
5.80M
        ps_final_prms->u1_skip_flag = ps_inter_cand->b1_skip_flag;
5940
5941
        /* MVP index & MVD calc is gated on skip/merge flag */
5942
5.80M
        if(0 == skip_or_merge_flag)
5943
1.50M
        {
5944
            /* calculate the MVDs and popluate the MVP idx for L0 */
5945
1.50M
            if((PRED_BI == ps_pu->b2_pred_mode) || (PRED_L0 == ps_pu->b2_pred_mode))
5946
1.27M
            {
5947
1.27M
                WORD32 idx0_cost, idx1_cost;
5948
5949
                /* calculate the ABS mvd for cand 0 */
5950
1.27M
                idx0_cost = abs(ps_pu->mv.s_l0_mv.i2_mvx - as_pred_mv[0].s_l0_mv.i2_mvx);
5951
1.27M
                idx0_cost += abs(ps_pu->mv.s_l0_mv.i2_mvy - as_pred_mv[0].s_l0_mv.i2_mvy);
5952
5953
                /* calculate the ABS mvd for cand 1 */
5954
1.27M
                if(u1_use_mvp_from_top_row)
5955
1.27M
                {
5956
1.27M
                    idx1_cost = abs(ps_pu->mv.s_l0_mv.i2_mvx - as_pred_mv[1].s_l0_mv.i2_mvx);
5957
1.27M
                    idx1_cost += abs(ps_pu->mv.s_l0_mv.i2_mvy - as_pred_mv[1].s_l0_mv.i2_mvy);
5958
1.27M
                }
5959
0
                else
5960
0
                {
5961
0
                    idx1_cost = INT_MAX;
5962
0
                }
5963
5964
                /* based on the least cost choose the mvp idx */
5965
1.27M
                if(idx0_cost <= idx1_cost)
5966
850k
                {
5967
850k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l0_mv.i2_mvx -=
5968
850k
                        as_pred_mv[0].s_l0_mv.i2_mvx;
5969
850k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l0_mv.i2_mvy -=
5970
850k
                        as_pred_mv[0].s_l0_mv.i2_mvy;
5971
5972
850k
                    ps_final_prms->as_pu_enc_loop[ctr].b1_l0_mvp_idx = 0;
5973
850k
                }
5974
428k
                else
5975
428k
                {
5976
428k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l0_mv.i2_mvx -=
5977
428k
                        as_pred_mv[1].s_l0_mv.i2_mvx;
5978
428k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l0_mv.i2_mvy -=
5979
428k
                        as_pred_mv[1].s_l0_mv.i2_mvy;
5980
5981
428k
                    ps_final_prms->as_pu_enc_loop[ctr].b1_l0_mvp_idx = 1;
5982
428k
                }
5983
5984
                /* set the pred l0 flag for neighbour storage */
5985
1.27M
                ps_nbr_4x4->b1_pred_l0_flag = 1;
5986
1.27M
            }
5987
            /* calculate the MVDs and popluate the MVP idx for L1 */
5988
1.50M
            if((PRED_BI == ps_pu->b2_pred_mode) || (PRED_L1 == ps_pu->b2_pred_mode))
5989
294k
            {
5990
294k
                WORD32 idx0_cost, idx1_cost;
5991
5992
                /* calculate the ABS mvd for cand 0 */
5993
294k
                idx0_cost = abs(ps_pu->mv.s_l1_mv.i2_mvx - as_pred_mv[0].s_l1_mv.i2_mvx);
5994
294k
                idx0_cost += abs(ps_pu->mv.s_l1_mv.i2_mvy - as_pred_mv[0].s_l1_mv.i2_mvy);
5995
5996
                /* calculate the ABS mvd for cand 1 */
5997
294k
                if(u1_use_mvp_from_top_row)
5998
294k
                {
5999
294k
                    idx1_cost = abs(ps_pu->mv.s_l1_mv.i2_mvx - as_pred_mv[1].s_l1_mv.i2_mvx);
6000
294k
                    idx1_cost += abs(ps_pu->mv.s_l1_mv.i2_mvy - as_pred_mv[1].s_l1_mv.i2_mvy);
6001
294k
                }
6002
0
                else
6003
0
                {
6004
0
                    idx1_cost = INT_MAX;
6005
0
                }
6006
6007
                /* based on the least cost choose the mvp idx */
6008
294k
                if(idx0_cost <= idx1_cost)
6009
192k
                {
6010
192k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l1_mv.i2_mvx -=
6011
192k
                        as_pred_mv[0].s_l1_mv.i2_mvx;
6012
192k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l1_mv.i2_mvy -=
6013
192k
                        as_pred_mv[0].s_l1_mv.i2_mvy;
6014
6015
192k
                    ps_final_prms->as_pu_enc_loop[ctr].b1_l1_mvp_idx = 0;
6016
192k
                }
6017
102k
                else
6018
102k
                {
6019
102k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l1_mv.i2_mvx -=
6020
102k
                        as_pred_mv[1].s_l1_mv.i2_mvx;
6021
102k
                    ps_final_prms->as_pu_enc_loop[ctr].mv.s_l1_mv.i2_mvy -=
6022
102k
                        as_pred_mv[1].s_l1_mv.i2_mvy;
6023
6024
102k
                    ps_final_prms->as_pu_enc_loop[ctr].b1_l1_mvp_idx = 1;
6025
102k
                }
6026
6027
                /* set the pred l1 flag for neighbour storage */
6028
294k
                ps_nbr_4x4->b1_pred_l1_flag = 1;
6029
294k
            }
6030
6031
            /* set the merge flag to 0 */
6032
1.50M
            ps_final_prms->as_pu_enc_loop[ctr].b1_merge_flag = 0;
6033
1.50M
            ps_final_prms->as_pu_enc_loop[ctr].b3_merge_idx = 0;
6034
1.50M
        }
6035
4.29M
        else
6036
4.29M
        {
6037
            /* copy the merge index from candidate */
6038
4.29M
            ps_final_prms->as_pu_enc_loop[ctr].b1_merge_flag = ps_pu->b1_merge_flag;
6039
6040
4.29M
            ps_final_prms->as_pu_enc_loop[ctr].b3_merge_idx = ps_pu->b3_merge_idx;
6041
6042
4.29M
            if((PRED_BI == ps_pu->b2_pred_mode) || (PRED_L0 == ps_pu->b2_pred_mode))
6043
3.98M
            {
6044
                /* set the pred l0 flag for neighbour storage */
6045
3.98M
                ps_nbr_4x4->b1_pred_l0_flag = 1;
6046
3.98M
            }
6047
6048
            /* calculate the MVDs and popluate the MVP idx for L1 */
6049
4.29M
            if((PRED_BI == ps_pu->b2_pred_mode) || (PRED_L1 == ps_pu->b2_pred_mode))
6050
1.23M
            {
6051
                /* set the pred l1 flag for neighbour storage */
6052
1.23M
                ps_nbr_4x4->b1_pred_l1_flag = 1;
6053
1.23M
            }
6054
4.29M
        }
6055
6056
        /* RD opt cost computation is part of cu_ntu func hence here it is set to 0 */
6057
5.80M
        rdopt_cost = 0;
6058
6059
        /* copy the MV to colocated Mv structure */
6060
5.80M
        ps_final_prms->as_col_pu_enc_loop[ctr].s_l0_mv = ps_pu->mv.s_l0_mv;
6061
5.80M
        ps_final_prms->as_col_pu_enc_loop[ctr].s_l1_mv = ps_pu->mv.s_l1_mv;
6062
5.80M
        ps_final_prms->as_col_pu_enc_loop[ctr].i1_l0_ref_idx = ps_pu->mv.i1_l0_ref_idx;
6063
5.80M
        ps_final_prms->as_col_pu_enc_loop[ctr].i1_l1_ref_idx = ps_pu->mv.i1_l1_ref_idx;
6064
5.80M
        ps_final_prms->as_col_pu_enc_loop[ctr].b2_pred_mode = ps_pu->b2_pred_mode;
6065
5.80M
        ps_final_prms->as_col_pu_enc_loop[ctr].b1_intra_flag = 0;
6066
6067
        /* replicate neighbour 4x4 strcuture for entire partition */
6068
5.80M
        {
6069
5.80M
            WORD32 i, j;
6070
5.80M
            nbr_4x4_t *ps_tmp_4x4;
6071
6072
5.80M
            ps_tmp_4x4 = ps_nbr_4x4;
6073
6074
27.9M
            for(i = 0; i < (inter_pu_ht >> 2); i++)
6075
22.1M
            {
6076
146M
                for(j = 0; j < (inter_pu_wd >> 2); j++)
6077
123M
                {
6078
123M
                    ps_tmp_4x4[j] = *ps_nbr_4x4;
6079
123M
                }
6080
                /* row level update*/
6081
22.1M
                ps_tmp_4x4 += (cu_size >> 2);
6082
22.1M
            }
6083
5.80M
        }
6084
        /* set the neighbour map to 1 */
6085
5.80M
        ihevce_set_inter_nbr_map(
6086
5.80M
            ps_ctxt->pu1_ctb_nbr_map,
6087
5.80M
            ps_ctxt->i4_nbr_map_strd,
6088
5.80M
            cu_pos_x,
6089
5.80M
            cu_pos_y,
6090
5.80M
            (inter_pu_wd >> 2),
6091
5.80M
            (inter_pu_ht >> 2),
6092
5.80M
            1);
6093
        /* ----------- Motion Compensation for Luma ----------- */
6094
#if !ENABLE_MIXED_INTER_MODE_EVAL
6095
        {
6096
            IV_API_CALL_STATUS_T valid_mv_cand;
6097
6098
            /*If the inter candidate is neither merge cand nor skip cand
6099
            then calculate the mc.*/
6100
            if(0 == skip_or_merge_flag || (ps_ctxt->u1_high_speed_cu_dec_on))
6101
            {
6102
                valid_mv_cand =
6103
                    ihevce_luma_inter_pred_pu(&ps_ctxt->s_mc_ctxt, ps_pu, pu1_pred, pred_stride, 0);
6104
6105
                /* assert if the MC is given a valid mv candidate */
6106
                ASSERT(valid_mv_cand == IV_SUCCESS);
6107
            }
6108
        }
6109
#endif
6110
5.80M
        if((2 == num_cu_part) && (0 == ctr))
6111
860k
        {
6112
            /* 2Nx__ partion case */
6113
860k
            if(inter_pu_wd == cu_size)
6114
703k
            {
6115
703k
                cu_pos_y += (inter_pu_ht >> 2);
6116
703k
                pu1_pred += (inter_pu_ht * pred_stride);
6117
703k
                ps_nbr_4x4 += (inter_pu_ht >> 2) * (cu_size >> 2);
6118
703k
                ps_left_nbr_4x4 += (inter_pu_ht >> 2) * nbr_4x4_left_strd;
6119
703k
                ps_top_nbr_4x4 = ps_nbr_4x4 - (cu_size >> 2);
6120
703k
                ps_topleft_nbr_4x4 = ps_left_nbr_4x4 - nbr_4x4_left_strd;
6121
703k
            }
6122
6123
            /* __x2N partion case */
6124
860k
            if(inter_pu_ht == cu_size)
6125
157k
            {
6126
157k
                cu_pos_x += (inter_pu_wd >> 2);
6127
157k
                pu1_pred += inter_pu_wd;
6128
157k
                ps_nbr_4x4 += (inter_pu_wd >> 2);
6129
157k
                ps_left_nbr_4x4 = ps_nbr_4x4 - 1;
6130
157k
                ps_top_nbr_4x4 += (inter_pu_wd >> 2);
6131
157k
                ps_topleft_nbr_4x4 = ps_top_nbr_4x4 - 1;
6132
157k
                nbr_4x4_left_strd = (cu_size >> 2);
6133
157k
            }
6134
860k
        }
6135
5.80M
    }
6136
6137
4.94M
    return (rdopt_cost);
6138
4.94M
}
6139
6140
/*!
6141
******************************************************************************
6142
* \if Function name : ihevce_intra_chroma_pred_mode_selector \endif
6143
*
6144
* \brief
6145
*    Coding unit processing function for chroma special modes (Non-Luma modes)
6146
*
6147
* \param[in] ps_ctxt       enc_loop module ctxt pointer
6148
* \param[in] ps_chrm_cu_buf_prms    ctxt having chroma related prms
6149
* \param[in] ps_cu_analyse      pointer to cu analyse
6150
* \param[in] rd_opt_curr_idx    index in the array of RDopt params
6151
* \param[in] tu_mode            TU_EQ_CU or other case
6152
*
6153
* \return
6154
*    Stores the best SATD mode, it's RDOPT cost, CABAC state, TU bits
6155
*
6156
* \author
6157
*  Ittiam
6158
*
6159
*****************************************************************************
6160
*/
6161
UWORD8 ihevce_distortion_based_intra_chroma_mode_selector(
6162
    cu_analyse_t *ps_cu_analyse,
6163
    ihevc_intra_pred_chroma_ref_substitution_ft *pf_ref_substitution,
6164
    pf_intra_pred *ppf_chroma_ip,
6165
    pf_res_trans_luma_had_chroma *ppf_resd_trns_had,
6166
    UWORD8 *pu1_src,
6167
    WORD32 i4_src_stride,
6168
    UWORD8 *pu1_pred,
6169
    WORD32 i4_pred_stride,
6170
    UWORD8 *pu1_ctb_nbr_map,
6171
    WORD32 i4_nbr_map_strd,
6172
    UWORD8 *pu1_ref_sub_out,
6173
    WORD32 i4_alpha_stim_multiplier,
6174
    UWORD8 u1_is_cu_noisy,
6175
    UWORD8 u1_trans_size,
6176
    UWORD8 u1_trans_idx,
6177
    UWORD8 u1_num_tus_in_cu,
6178
    UWORD8 u1_num_4x4_luma_blks_in_tu,
6179
    UWORD8 u1_enable_psyRDOPT,
6180
    UWORD8 u1_is_422)
6181
2.39M
{
6182
2.39M
    UWORD8 u1_chrm_mode;
6183
2.39M
    UWORD8 ctr;
6184
2.39M
    WORD32 i4_subtu_idx;
6185
6186
2.39M
    WORD32 i = 0;
6187
2.39M
    UWORD8 u1_chrm_modes[4] = { 0, 1, 10, 26 };
6188
2.39M
    WORD32 i4_satd_had[4] = { 0 };
6189
2.39M
    WORD32 i4_best_satd_had = INT_MAX;
6190
2.39M
    UWORD8 u1_cu_pos_x = (ps_cu_analyse->b3_cu_pos_x << 1);
6191
2.39M
    UWORD8 u1_cu_pos_y = (ps_cu_analyse->b3_cu_pos_y << 1);
6192
2.39M
    WORD32 i4_num_sub_tus = u1_is_422 + 1;
6193
2.39M
    UWORD8 u1_best_chrm_mode = 0;
6194
6195
    /* Get the best satd among all possible modes */
6196
11.9M
    for(i = 0; i < 4; i++)
6197
9.58M
    {
6198
9.58M
        WORD32 left_strd = i4_src_stride;
6199
6200
9.58M
        u1_chrm_mode = (u1_is_422 == 1) ? gau1_chroma422_intra_angle_mapping[u1_chrm_modes[i]]
6201
9.58M
                                        : u1_chrm_modes[i];
6202
6203
        /* loop based on num tus in a cu */
6204
24.0M
        for(ctr = 0; ctr < u1_num_tus_in_cu; ctr++)
6205
14.4M
        {
6206
14.4M
            WORD32 luma_nbr_flags;
6207
14.4M
            WORD32 chrm_pred_func_idx;
6208
6209
14.4M
            WORD32 i4_trans_size_m2 = u1_trans_size << 1;
6210
14.4M
            UWORD8 *pu1_tu_src = pu1_src + ((ctr & 1) * i4_trans_size_m2) +
6211
14.4M
                                 (((ctr > 1) * u1_trans_size * i4_src_stride) << u1_is_422);
6212
14.4M
            UWORD8 *pu1_tu_pred = pu1_pred + ((ctr & 1) * i4_trans_size_m2) +
6213
14.4M
                                  (((ctr > 1) * u1_trans_size * i4_pred_stride) << u1_is_422);
6214
14.4M
            WORD32 i4_curr_tu_pos_x = u1_cu_pos_x + ((ctr & 1) * u1_num_4x4_luma_blks_in_tu);
6215
14.4M
            WORD32 i4_curr_tu_pos_y = u1_cu_pos_y + ((ctr > 1) * u1_num_4x4_luma_blks_in_tu);
6216
6217
14.4M
            luma_nbr_flags = ihevce_get_nbr_intra_mxn_tu(
6218
14.4M
                pu1_ctb_nbr_map,
6219
14.4M
                i4_nbr_map_strd,
6220
14.4M
                i4_curr_tu_pos_x,
6221
14.4M
                i4_curr_tu_pos_y,
6222
14.4M
                u1_num_4x4_luma_blks_in_tu,
6223
14.4M
                u1_num_4x4_luma_blks_in_tu);
6224
6225
28.9M
            for(i4_subtu_idx = 0; i4_subtu_idx < i4_num_sub_tus; i4_subtu_idx++)
6226
14.4M
            {
6227
14.4M
                WORD32 nbr_flags;
6228
6229
14.4M
                UWORD8 *pu1_cur_src =
6230
14.4M
                    pu1_tu_src + ((i4_subtu_idx == 1) * u1_trans_size * i4_src_stride);
6231
14.4M
                UWORD8 *pu1_cur_pred =
6232
14.4M
                    pu1_tu_pred + ((i4_subtu_idx == 1) * u1_trans_size * i4_pred_stride);
6233
14.4M
                UWORD8 *pu1_left = pu1_cur_src - 2;
6234
14.4M
                UWORD8 *pu1_top = pu1_cur_src - i4_src_stride;
6235
14.4M
                UWORD8 *pu1_top_left = pu1_top - 2;
6236
6237
14.4M
                nbr_flags = ihevce_get_intra_chroma_tu_nbr(
6238
14.4M
                    luma_nbr_flags, i4_subtu_idx, u1_trans_size, u1_is_422);
6239
6240
                /* call the chroma reference array substitution */
6241
14.4M
                pf_ref_substitution(
6242
14.4M
                    pu1_top_left,
6243
14.4M
                    pu1_top,
6244
14.4M
                    pu1_left,
6245
14.4M
                    left_strd,
6246
14.4M
                    u1_trans_size,
6247
14.4M
                    nbr_flags,
6248
14.4M
                    pu1_ref_sub_out,
6249
14.4M
                    1,
6250
14.4M
                    CHROMA_FMT_IDC_YUV420);
6251
6252
                /* use the look up to get the function idx */
6253
14.4M
                chrm_pred_func_idx = g_i4_ip_funcs[u1_chrm_mode];
6254
6255
                /* call the intra prediction function */
6256
14.4M
                ppf_chroma_ip[chrm_pred_func_idx](
6257
14.4M
                    pu1_ref_sub_out, 1, pu1_cur_pred, i4_pred_stride, u1_trans_size, u1_chrm_mode);
6258
6259
14.4M
                if(!u1_is_cu_noisy || !i4_alpha_stim_multiplier)
6260
14.4M
                {
6261
                    /* compute Hadamard-transform satd : Cb */
6262
14.4M
                    i4_satd_had[i] += ppf_resd_trns_had[u1_trans_idx - 1](
6263
14.4M
                        pu1_cur_src, i4_src_stride, pu1_cur_pred, i4_pred_stride, NULL, 0);
6264
6265
                    /* compute Hadamard-transform satd : Cr */
6266
14.4M
                    i4_satd_had[i] += ppf_resd_trns_had[u1_trans_idx - 1](
6267
14.4M
                        pu1_cur_src + 1, i4_src_stride, pu1_cur_pred + 1, i4_pred_stride, NULL, 0);
6268
14.4M
                }
6269
0
                else
6270
0
                {
6271
0
                    WORD32 i4_satd;
6272
6273
                    /* compute Hadamard-transform satd : Cb */
6274
0
                    i4_satd = ppf_resd_trns_had[u1_trans_idx - 1](
6275
0
                        pu1_cur_src, i4_src_stride, pu1_cur_pred, i4_pred_stride, NULL, 0);
6276
6277
0
                    i4_satd = ihevce_inject_stim_into_distortion(
6278
0
                        pu1_cur_src,
6279
0
                        i4_src_stride,
6280
0
                        pu1_cur_pred,
6281
0
                        i4_pred_stride,
6282
0
                        i4_satd,
6283
0
                        i4_alpha_stim_multiplier,
6284
0
                        u1_trans_size,
6285
0
                        0,
6286
0
                        u1_enable_psyRDOPT,
6287
0
                        U_PLANE);
6288
6289
0
                    i4_satd_had[i] += i4_satd;
6290
6291
                    /* compute Hadamard-transform satd : Cr */
6292
0
                    i4_satd = ppf_resd_trns_had[u1_trans_idx - 1](
6293
0
                        pu1_cur_src + 1, i4_src_stride, pu1_cur_pred + 1, i4_pred_stride, NULL, 0);
6294
6295
0
                    i4_satd = ihevce_inject_stim_into_distortion(
6296
0
                        pu1_cur_src,
6297
0
                        i4_src_stride,
6298
0
                        pu1_cur_pred,
6299
0
                        i4_pred_stride,
6300
0
                        i4_satd,
6301
0
                        i4_alpha_stim_multiplier,
6302
0
                        u1_trans_size,
6303
0
                        0,
6304
0
                        u1_enable_psyRDOPT,
6305
0
                        V_PLANE);
6306
6307
0
                    i4_satd_had[i] += i4_satd;
6308
0
                }
6309
14.4M
            }
6310
6311
            /* set the neighbour map to 1 */
6312
14.4M
            ihevce_set_nbr_map(
6313
14.4M
                pu1_ctb_nbr_map,
6314
14.4M
                i4_nbr_map_strd,
6315
14.4M
                i4_curr_tu_pos_x,
6316
14.4M
                i4_curr_tu_pos_y,
6317
14.4M
                u1_num_4x4_luma_blks_in_tu,
6318
14.4M
                1);
6319
14.4M
        }
6320
6321
        /* set the neighbour map to 0 */
6322
9.58M
        ihevce_set_nbr_map(
6323
9.58M
            pu1_ctb_nbr_map,
6324
9.58M
            i4_nbr_map_strd,
6325
9.58M
            (ps_cu_analyse->b3_cu_pos_x << 1),
6326
9.58M
            (ps_cu_analyse->b3_cu_pos_y << 1),
6327
9.58M
            (ps_cu_analyse->u1_cu_size >> 2),
6328
9.58M
            0);
6329
6330
        /* Get the least SATD and corresponding mode */
6331
9.58M
        if(i4_best_satd_had > i4_satd_had[i])
6332
2.81M
        {
6333
2.81M
            i4_best_satd_had = i4_satd_had[i];
6334
2.81M
            u1_best_chrm_mode = u1_chrm_mode;
6335
2.81M
        }
6336
9.58M
    }
6337
6338
2.39M
    return u1_best_chrm_mode;
6339
2.39M
}
6340
6341
void ihevce_intra_chroma_pred_mode_selector(
6342
    ihevce_enc_loop_ctxt_t *ps_ctxt,
6343
    enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
6344
    cu_analyse_t *ps_cu_analyse,
6345
    WORD32 rd_opt_curr_idx,
6346
    WORD32 tu_mode,
6347
    WORD32 i4_alpha_stim_multiplier,
6348
    UWORD8 u1_is_cu_noisy)
6349
2.39M
{
6350
2.39M
    chroma_intra_satd_ctxt_t *ps_chr_intra_satd_ctxt;
6351
6352
2.39M
    ihevc_intra_pred_chroma_ref_substitution_ft *ihevc_intra_pred_chroma_ref_substitution_fptr;
6353
6354
2.39M
    UWORD8 *pu1_pred;
6355
2.39M
    WORD32 trans_size;
6356
2.39M
    WORD32 num_tus_in_cu;
6357
2.39M
    WORD32 pred_strd;
6358
2.39M
    WORD32 ctr;
6359
2.39M
    WORD32 i4_subtu_idx;
6360
2.39M
    WORD32 i4_num_sub_tus;
6361
2.39M
    WORD32 trans_idx;
6362
2.39M
    WORD32 scan_idx;
6363
2.39M
    WORD32 num_4x4_luma_in_tu;
6364
2.39M
    WORD32 cu_pos_x;
6365
2.39M
    WORD32 cu_pos_y;
6366
6367
2.39M
    recon_datastore_t *aps_recon_datastore[2] = { &ps_ctxt->as_cu_prms[0].s_recon_datastore,
6368
2.39M
                                                  &ps_ctxt->as_cu_prms[1].s_recon_datastore };
6369
6370
2.39M
    LWORD64 chrm_cod_cost = 0;
6371
2.39M
    WORD32 chrm_tu_bits = 0;
6372
2.39M
    WORD32 best_chrm_mode = DM_CHROMA_IDX;
6373
2.39M
    UWORD8 *pu1_chrm_src = ps_chrm_cu_buf_prms->pu1_curr_src;
6374
2.39M
    WORD32 chrm_src_stride = ps_chrm_cu_buf_prms->i4_chrm_src_stride;
6375
2.39M
    UWORD8 *pu1_cu_left = ps_chrm_cu_buf_prms->pu1_cu_left;
6376
2.39M
    UWORD8 *pu1_cu_top = ps_chrm_cu_buf_prms->pu1_cu_top;
6377
2.39M
    UWORD8 *pu1_cu_top_left = ps_chrm_cu_buf_prms->pu1_cu_top_left;
6378
2.39M
    WORD32 cu_left_stride = ps_chrm_cu_buf_prms->i4_cu_left_stride;
6379
2.39M
    WORD32 cu_size = ps_cu_analyse->u1_cu_size;
6380
2.39M
    WORD32 i4_perform_rdoq = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_rdoq;
6381
2.39M
    WORD32 i4_perform_sbh = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_sbh;
6382
2.39M
    UWORD8 u1_is_422 = (ps_ctxt->u1_chroma_array_type == 2);
6383
6384
2.39M
    ihevc_intra_pred_chroma_ref_substitution_fptr =
6385
2.39M
        ps_ctxt->ps_func_selector->ihevc_intra_pred_chroma_ref_substitution_fptr;
6386
2.39M
    i4_num_sub_tus = (u1_is_422 == 1) + 1;
6387
6388
#if DISABLE_RDOQ_INTRA
6389
    i4_perform_rdoq = 0;
6390
#endif
6391
6392
2.39M
    if(TU_EQ_CU == tu_mode)
6393
1.98M
    {
6394
1.98M
        num_tus_in_cu = 1;
6395
1.98M
        trans_size = cu_size >> 1;
6396
1.98M
        num_4x4_luma_in_tu = trans_size >> 1; /*at luma level*/
6397
1.98M
        ps_chr_intra_satd_ctxt = &ps_ctxt->s_chroma_rdopt_ctxt.as_chr_intra_satd_ctxt[tu_mode];
6398
1.98M
    }
6399
408k
    else
6400
408k
    {
6401
408k
        num_tus_in_cu = 4;
6402
408k
        trans_size = cu_size >> 2;
6403
408k
        num_4x4_luma_in_tu = trans_size >> 1; /*at luma level*/
6404
6405
        /* For 8x8 CU only one TU */
6406
408k
        if(MIN_TU_SIZE > trans_size)
6407
0
        {
6408
0
            trans_size = MIN_TU_SIZE;
6409
0
            num_tus_in_cu = 1;
6410
            /* chroma nbr avail. is derived based on luma.
6411
            for 4x4 chrm use 8x8 luma's size */
6412
0
            num_4x4_luma_in_tu = num_4x4_luma_in_tu << 1;
6413
0
        }
6414
6415
408k
        ps_chr_intra_satd_ctxt = &ps_ctxt->s_chroma_rdopt_ctxt.as_chr_intra_satd_ctxt[tu_mode];
6416
408k
    }
6417
6418
    /* Can't be TU_EQ_SUBCU case */
6419
2.39M
    ASSERT(TU_EQ_SUBCU != tu_mode);
6420
6421
    /* translate the transform size to index */
6422
2.39M
    trans_idx = trans_size >> 2;
6423
6424
2.39M
    pu1_pred = (UWORD8 *)ps_chr_intra_satd_ctxt->pv_pred_data;
6425
6426
2.39M
    pred_strd = ps_chr_intra_satd_ctxt->i4_pred_stride;
6427
6428
    /* for 16x16 cases */
6429
2.39M
    if(16 == trans_size)
6430
342k
    {
6431
342k
        trans_idx = 3;
6432
342k
    }
6433
6434
2.39M
    best_chrm_mode = ihevce_distortion_based_intra_chroma_mode_selector(
6435
2.39M
        ps_cu_analyse,
6436
2.39M
        ihevc_intra_pred_chroma_ref_substitution_fptr,
6437
2.39M
        ps_ctxt->apf_chrm_ip,
6438
2.39M
        ps_ctxt->apf_chrm_resd_trns_had,
6439
2.39M
        pu1_chrm_src,
6440
2.39M
        chrm_src_stride,
6441
2.39M
        pu1_pred,
6442
2.39M
        pred_strd,
6443
2.39M
        ps_ctxt->pu1_ctb_nbr_map,
6444
2.39M
        ps_ctxt->i4_nbr_map_strd,
6445
2.39M
        (UWORD8 *)ps_ctxt->pv_ref_sub_out,
6446
2.39M
        i4_alpha_stim_multiplier,
6447
2.39M
        u1_is_cu_noisy,
6448
2.39M
        trans_size,
6449
2.39M
        trans_idx,
6450
2.39M
        num_tus_in_cu,
6451
2.39M
        num_4x4_luma_in_tu,
6452
2.39M
        ps_ctxt->u1_enable_psyRDOPT,
6453
2.39M
        u1_is_422);
6454
6455
    /* Store the best chroma mode */
6456
2.39M
    ps_chr_intra_satd_ctxt->u1_best_cr_mode = best_chrm_mode;
6457
6458
    /* evaluate RDOPT cost for the Best mode */
6459
2.39M
    {
6460
2.39M
        WORD32 i4_subtu_pos_x;
6461
2.39M
        WORD32 i4_subtu_pos_y;
6462
2.39M
        UWORD8 u1_compute_spatial_ssd;
6463
6464
2.39M
        WORD32 ai4_total_bytes_offset_cb[2] = { 0, 0 };
6465
2.39M
        WORD32 ai4_total_bytes_offset_cr[2] = { 0, 0 };
6466
        /* State for prefix bin of chroma intra pred mode before CU encode */
6467
2.39M
        UWORD8 u1_chroma_intra_mode_prefix_state =
6468
2.39M
            ps_ctxt->au1_rdopt_init_ctxt_models[IHEVC_CAB_CHROMA_PRED_MODE];
6469
2.39M
        WORD32 luma_trans_size = trans_size << 1;
6470
2.39M
        WORD32 calc_recon = 0;
6471
2.39M
        UWORD8 *pu1_left = pu1_cu_left;
6472
2.39M
        UWORD8 *pu1_top = pu1_cu_top;
6473
2.39M
        UWORD8 *pu1_top_left = pu1_cu_top_left;
6474
2.39M
        WORD32 left_strd = cu_left_stride;
6475
6476
2.39M
        if(ps_ctxt->i1_cu_qp_delta_enable)
6477
1.05M
        {
6478
1.05M
            ihevce_update_cu_level_qp_lamda(ps_ctxt, ps_cu_analyse, luma_trans_size, 1);
6479
1.05M
        }
6480
6481
2.39M
        u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_QP_WHERE_SPATIAL_SSD_ENABLED) &&
6482
1.47M
                                 (ps_ctxt->i4_quality_preset < IHEVCE_QUALITY_P3) &&
6483
1.47M
                                 CONVERT_SSDS_TO_SPATIAL_DOMAIN;
6484
6485
2.39M
        if(u1_is_cu_noisy || ps_ctxt->u1_enable_psyRDOPT)
6486
0
        {
6487
0
            u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_HEVC_QP) &&
6488
0
                                     CONVERT_SSDS_TO_SPATIAL_DOMAIN;
6489
0
        }
6490
6491
        /* get the 4x4 level postion of current cu */
6492
2.39M
        cu_pos_x = (ps_cu_analyse->b3_cu_pos_x << 1);
6493
2.39M
        cu_pos_y = (ps_cu_analyse->b3_cu_pos_y << 1);
6494
6495
2.39M
        calc_recon = !u1_compute_spatial_ssd && ((4 == num_tus_in_cu) || (u1_is_422 == 1));
6496
6497
2.39M
        if(calc_recon || u1_compute_spatial_ssd)
6498
1.61M
        {
6499
1.61M
            aps_recon_datastore[0]->au1_is_chromaRecon_available[1 + (num_tus_in_cu > 1)] = 1;
6500
1.61M
            aps_recon_datastore[1]->au1_is_chromaRecon_available[1 + (num_tus_in_cu > 1)] = 1;
6501
1.61M
        }
6502
776k
        else
6503
776k
        {
6504
776k
            aps_recon_datastore[0]->au1_is_chromaRecon_available[1 + (num_tus_in_cu > 1)] = 0;
6505
776k
            aps_recon_datastore[1]->au1_is_chromaRecon_available[1 + (num_tus_in_cu > 1)] = 0;
6506
776k
        }
6507
6508
        /* loop based on num tus in a cu */
6509
6.01M
        for(ctr = 0; ctr < num_tus_in_cu; ctr++)
6510
3.61M
        {
6511
3.61M
            WORD16 *pi2_cur_deq_data_cb;
6512
3.61M
            WORD16 *pi2_cur_deq_data_cr;
6513
6514
3.61M
            WORD32 deq_data_strd = ps_chr_intra_satd_ctxt->i4_iq_buff_stride;
6515
3.61M
            WORD32 luma_nbr_flags = 0;
6516
6517
3.61M
            luma_nbr_flags = ihevce_get_nbr_intra_mxn_tu(
6518
3.61M
                ps_ctxt->pu1_ctb_nbr_map,
6519
3.61M
                ps_ctxt->i4_nbr_map_strd,
6520
3.61M
                (ctr & 1) * (luma_trans_size >> 2) + cu_pos_x,
6521
3.61M
                (ctr > 1) * (luma_trans_size >> 2) + cu_pos_y,
6522
3.61M
                (luma_trans_size >> 2),
6523
3.61M
                (luma_trans_size >> 2));
6524
6525
7.23M
            for(i4_subtu_idx = 0; i4_subtu_idx < i4_num_sub_tus; i4_subtu_idx++)
6526
3.61M
            {
6527
3.61M
                WORD32 cbf, num_bytes;
6528
3.61M
                LWORD64 trans_ssd_u, trans_ssd_v;
6529
3.61M
                UWORD8 u1_is_recon_available;
6530
6531
3.61M
                WORD32 trans_size_m2 = trans_size << 1;
6532
3.61M
                UWORD8 *pu1_cur_src = pu1_chrm_src + ((ctr & 1) * trans_size_m2) +
6533
3.61M
                                      (((ctr > 1) * trans_size * chrm_src_stride) << u1_is_422) +
6534
3.61M
                                      (i4_subtu_idx * trans_size * chrm_src_stride);
6535
3.61M
                UWORD8 *pu1_cur_pred = pu1_pred + ((ctr & 1) * trans_size_m2) +
6536
3.61M
                                       (((ctr > 1) * trans_size * pred_strd) << u1_is_422) +
6537
3.61M
                                       (i4_subtu_idx * trans_size * pred_strd);
6538
3.61M
                WORD32 i4_recon_stride = aps_recon_datastore[0]->i4_chromaRecon_stride;
6539
3.61M
                UWORD8 *pu1_cur_recon = ((UWORD8 *)aps_recon_datastore[0]
6540
3.61M
                                             ->apv_chroma_recon_bufs[1 + (num_tus_in_cu > 1)]) +
6541
3.61M
                                        ((ctr & 1) * trans_size_m2) +
6542
3.61M
                                        (((ctr > 1) * trans_size * i4_recon_stride) << u1_is_422) +
6543
3.61M
                                        (i4_subtu_idx * trans_size * i4_recon_stride);
6544
6545
                /* Use Chroma coeff/iq buf of the cur. intra cand. Not rememb.
6546
                chroma coeff/iq for high quality intra SATD special modes. Will
6547
                be over written by coeff of luma mode in chroma_rdopt call */
6548
3.61M
                UWORD8 *pu1_ecd_data_cb =
6549
3.61M
                    &ps_chr_intra_satd_ctxt->au1_scan_coeff_cb[i4_subtu_idx][0];
6550
3.61M
                UWORD8 *pu1_ecd_data_cr =
6551
3.61M
                    &ps_chr_intra_satd_ctxt->au1_scan_coeff_cr[i4_subtu_idx][0];
6552
6553
3.61M
                WORD32 chrm_pred_func_idx = 0;
6554
3.61M
                LWORD64 curr_cb_cod_cost = 0;
6555
3.61M
                LWORD64 curr_cr_cod_cost = 0;
6556
3.61M
                WORD32 nbr_flags = 0;
6557
6558
3.61M
                i4_subtu_pos_x = (((ctr & 1) * trans_size_m2) >> 2);
6559
3.61M
                i4_subtu_pos_y = (((ctr > 1) * trans_size) >> (!u1_is_422 + 1)) +
6560
3.61M
                                 ((i4_subtu_idx * trans_size) >> 2);
6561
3.61M
                pi2_cur_deq_data_cb = &ps_chr_intra_satd_ctxt->ai2_iq_data_cb[0] +
6562
3.61M
                                      ((ctr & 1) * trans_size) +
6563
3.61M
                                      (((ctr > 1) * trans_size * deq_data_strd) << u1_is_422) +
6564
3.61M
                                      (i4_subtu_idx * trans_size * deq_data_strd);
6565
3.61M
                pi2_cur_deq_data_cr = &ps_chr_intra_satd_ctxt->ai2_iq_data_cr[0] +
6566
3.61M
                                      ((ctr & 1) * trans_size) +
6567
3.61M
                                      (((ctr > 1) * trans_size * deq_data_strd) << u1_is_422) +
6568
3.61M
                                      (i4_subtu_idx * trans_size * deq_data_strd);
6569
6570
                /* left cu boundary */
6571
3.61M
                if(0 == i4_subtu_pos_x)
6572
2.80M
                {
6573
2.80M
                    left_strd = cu_left_stride;
6574
2.80M
                    pu1_left = pu1_cu_left + (i4_subtu_pos_y << 2) * left_strd;
6575
2.80M
                }
6576
816k
                else
6577
816k
                {
6578
816k
                    pu1_left = pu1_cur_recon - 2;
6579
816k
                    left_strd = i4_recon_stride;
6580
816k
                }
6581
6582
                /* top cu boundary */
6583
3.61M
                if(0 == i4_subtu_pos_y)
6584
2.80M
                {
6585
2.80M
                    pu1_top = pu1_cu_top + (i4_subtu_pos_x << 2);
6586
2.80M
                }
6587
816k
                else
6588
816k
                {
6589
816k
                    pu1_top = pu1_cur_recon - i4_recon_stride;
6590
816k
                }
6591
6592
                /* by default top left is set to cu top left */
6593
3.61M
                pu1_top_left = pu1_cu_top_left;
6594
6595
                /* top left based on position */
6596
3.61M
                if((0 != i4_subtu_pos_y) && (0 == i4_subtu_pos_x))
6597
408k
                {
6598
408k
                    pu1_top_left = pu1_left - left_strd;
6599
408k
                }
6600
3.21M
                else if(0 != i4_subtu_pos_x)
6601
816k
                {
6602
816k
                    pu1_top_left = pu1_top - 2;
6603
816k
                }
6604
6605
                /* populate the coeffs scan idx */
6606
3.61M
                scan_idx = SCAN_DIAG_UPRIGHT;
6607
6608
                /* RDOPT copy States :  TU init (best until prev TU) to current */
6609
3.61M
                COPY_CABAC_STATES(
6610
3.61M
                    &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_curr_idx]
6611
3.61M
                         .s_cabac_ctxt.au1_ctxt_models[0],
6612
3.61M
                    &ps_ctxt->au1_rdopt_init_ctxt_models[0],
6613
3.61M
                    IHEVC_CAB_CTXT_END);
6614
6615
                /* for 4x4 transforms based on intra pred mode scan is choosen*/
6616
3.61M
                if(4 == trans_size)
6617
1.83M
                {
6618
                    /* for modes from 22 upto 30 horizontal scan is used */
6619
1.83M
                    if((best_chrm_mode > 21) && (best_chrm_mode < 31))
6620
57.5k
                    {
6621
57.5k
                        scan_idx = SCAN_HORZ;
6622
57.5k
                    }
6623
                    /* for modes from 6 upto 14 horizontal scan is used */
6624
1.77M
                    else if((best_chrm_mode > 5) && (best_chrm_mode < 15))
6625
119k
                    {
6626
119k
                        scan_idx = SCAN_VERT;
6627
119k
                    }
6628
1.83M
                }
6629
6630
3.61M
                nbr_flags = ihevce_get_intra_chroma_tu_nbr(
6631
3.61M
                    luma_nbr_flags, i4_subtu_idx, trans_size, u1_is_422);
6632
6633
                /* call the chroma reference array substitution */
6634
3.61M
                ihevc_intra_pred_chroma_ref_substitution_fptr(
6635
3.61M
                    pu1_top_left,
6636
3.61M
                    pu1_top,
6637
3.61M
                    pu1_left,
6638
3.61M
                    left_strd,
6639
3.61M
                    trans_size,
6640
3.61M
                    nbr_flags,
6641
3.61M
                    (UWORD8 *)ps_ctxt->pv_ref_sub_out,
6642
3.61M
                    1,
6643
3.61M
                    CHROMA_FMT_IDC_YUV420);
6644
6645
                /* use the look up to get the function idx */
6646
3.61M
                chrm_pred_func_idx = g_i4_ip_funcs[best_chrm_mode];
6647
6648
                /* call the intra prediction function */
6649
3.61M
                ps_ctxt->apf_chrm_ip[chrm_pred_func_idx](
6650
3.61M
                    (UWORD8 *)ps_ctxt->pv_ref_sub_out,
6651
3.61M
                    1,
6652
3.61M
                    pu1_cur_pred,
6653
3.61M
                    pred_strd,
6654
3.61M
                    trans_size,
6655
3.61M
                    best_chrm_mode);
6656
6657
                /* UPLANE RDOPT Loop */
6658
3.61M
                {
6659
3.61M
                    WORD32 tu_bits;
6660
6661
3.61M
                    cbf = ihevce_chroma_t_q_iq_ssd_scan_fxn(
6662
3.61M
                        ps_ctxt,
6663
3.61M
                        pu1_cur_pred,
6664
3.61M
                        pred_strd,
6665
3.61M
                        pu1_cur_src,
6666
3.61M
                        chrm_src_stride,
6667
3.61M
                        pi2_cur_deq_data_cb,
6668
3.61M
                        deq_data_strd,
6669
3.61M
                        pu1_cur_recon,
6670
3.61M
                        i4_recon_stride,
6671
3.61M
                        pu1_ecd_data_cb + ai4_total_bytes_offset_cb[i4_subtu_idx],
6672
3.61M
                        ps_ctxt->au1_cu_csbf,
6673
3.61M
                        ps_ctxt->i4_cu_csbf_strd,
6674
3.61M
                        trans_size,
6675
3.61M
                        scan_idx,
6676
3.61M
                        1,
6677
3.61M
                        &num_bytes,
6678
3.61M
                        &tu_bits,
6679
3.61M
                        &ps_chr_intra_satd_ctxt->ai4_zero_col_cb[i4_subtu_idx][ctr],
6680
3.61M
                        &ps_chr_intra_satd_ctxt->ai4_zero_row_cb[i4_subtu_idx][ctr],
6681
3.61M
                        &u1_is_recon_available,
6682
3.61M
                        i4_perform_sbh,
6683
3.61M
                        i4_perform_rdoq,
6684
3.61M
                        &trans_ssd_u,
6685
3.61M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
6686
3.61M
                        i4_alpha_stim_multiplier,
6687
3.61M
                        u1_is_cu_noisy,
6688
3.61M
#endif
6689
3.61M
                        0,
6690
3.61M
                        u1_compute_spatial_ssd ? SPATIAL_DOMAIN_SSD : FREQUENCY_DOMAIN_SSD,
6691
3.61M
                        U_PLANE);
6692
6693
#if !USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS && COMPUTE_NOISE_TERM_AT_THE_TU_LEVEL
6694
                    if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
6695
                    {
6696
#if !USE_RECON_TO_EVALUATE_STIM_IN_RDOPT
6697
                        trans_ssd_u = ihevce_inject_stim_into_distortion(
6698
                            pu1_cur_src,
6699
                            chrm_src_stride,
6700
                            pu1_cur_pred,
6701
                            pred_strd,
6702
                            trans_ssd_u,
6703
                            i4_alpha_stim_multiplier,
6704
                            trans_size,
6705
                            0,
6706
                            ps_ctxt->u1_enable_psyRDOPT,
6707
                            U_PLANE);
6708
#else
6709
                        if(u1_compute_spatial_ssd && u1_is_recon_available)
6710
                        {
6711
                            trans_ssd_u = ihevce_inject_stim_into_distortion(
6712
                                pu1_cur_src,
6713
                                chrm_src_stride,
6714
                                pu1_cur_recon,
6715
                                i4_recon_stride,
6716
                                trans_ssd_u,
6717
                                i4_alpha_stim_multiplier,
6718
                                trans_size,
6719
                                0,
6720
                                ps_ctxt->u1_enable_psyRDOPT,
6721
                                U_PLANE);
6722
                        }
6723
                        else
6724
                        {
6725
                            trans_ssd_u = ihevce_inject_stim_into_distortion(
6726
                                pu1_cur_src,
6727
                                chrm_src_stride,
6728
                                pu1_cur_pred,
6729
                                pred_strd,
6730
                                trans_ssd_u,
6731
                                i4_alpha_stim_multiplier,
6732
                                trans_size,
6733
                                0,
6734
                                ps_ctxt->u1_enable_psyRDOPT,
6735
                                U_PLANE);
6736
                        }
6737
#endif
6738
                    }
6739
#endif
6740
6741
                    /* RDOPT copy States :  New updated after curr TU to TU init */
6742
3.61M
                    if(0 != cbf)
6743
475k
                    {
6744
475k
                        memcpy(
6745
475k
                            &ps_ctxt->au1_rdopt_init_ctxt_models[0],
6746
475k
                            &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_curr_idx]
6747
475k
                                 .s_cabac_ctxt.au1_ctxt_models[0],
6748
475k
                            IHEVC_CAB_CTXT_END);
6749
475k
                    }
6750
                    /* RDOPT copy States :  Restoring back the Cb init state to Cr */
6751
3.14M
                    else
6752
3.14M
                    {
6753
3.14M
                        memcpy(
6754
3.14M
                            &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_curr_idx]
6755
3.14M
                                 .s_cabac_ctxt.au1_ctxt_models[0],
6756
3.14M
                            &ps_ctxt->au1_rdopt_init_ctxt_models[0],
6757
3.14M
                            IHEVC_CAB_CTXT_END);
6758
3.14M
                    }
6759
6760
3.61M
                    if(calc_recon || (!u1_is_recon_available && u1_compute_spatial_ssd))
6761
573k
                    {
6762
573k
                        ihevce_chroma_it_recon_fxn(
6763
573k
                            ps_ctxt,
6764
573k
                            pi2_cur_deq_data_cb,
6765
573k
                            deq_data_strd,
6766
573k
                            pu1_cur_pred,
6767
573k
                            pred_strd,
6768
573k
                            pu1_cur_recon,
6769
573k
                            i4_recon_stride,
6770
573k
                            (pu1_ecd_data_cb + ai4_total_bytes_offset_cb[i4_subtu_idx]),
6771
573k
                            trans_size,
6772
573k
                            cbf,
6773
573k
                            ps_chr_intra_satd_ctxt->ai4_zero_col_cb[i4_subtu_idx][ctr],
6774
573k
                            ps_chr_intra_satd_ctxt->ai4_zero_row_cb[i4_subtu_idx][ctr],
6775
573k
                            U_PLANE);
6776
573k
                    }
6777
6778
3.61M
                    ps_chr_intra_satd_ctxt->au1_cbf_cb[i4_subtu_idx][ctr] = cbf;
6779
3.61M
                    curr_cb_cod_cost =
6780
3.61M
                        trans_ssd_u +
6781
3.61M
                        COMPUTE_RATE_COST_CLIP30(
6782
3.61M
                            tu_bits, ps_ctxt->i8_cl_ssd_lambda_chroma_qf, LAMBDA_Q_SHIFT);
6783
3.61M
                    chrm_tu_bits += tu_bits;
6784
3.61M
                    ai4_total_bytes_offset_cb[i4_subtu_idx] += num_bytes;
6785
3.61M
                    ps_chr_intra_satd_ctxt->ai4_num_bytes_scan_coeff_cb_per_tu[i4_subtu_idx][ctr] =
6786
3.61M
                        num_bytes;
6787
3.61M
                }
6788
6789
                /* VPLANE RDOPT Loop */
6790
3.61M
                {
6791
3.61M
                    WORD32 tu_bits;
6792
6793
3.61M
                    cbf = ihevce_chroma_t_q_iq_ssd_scan_fxn(
6794
3.61M
                        ps_ctxt,
6795
3.61M
                        pu1_cur_pred,
6796
3.61M
                        pred_strd,
6797
3.61M
                        pu1_cur_src,
6798
3.61M
                        chrm_src_stride,
6799
3.61M
                        pi2_cur_deq_data_cr,
6800
3.61M
                        deq_data_strd,
6801
3.61M
                        pu1_cur_recon,
6802
3.61M
                        i4_recon_stride,
6803
3.61M
                        pu1_ecd_data_cr + ai4_total_bytes_offset_cr[i4_subtu_idx],
6804
3.61M
                        ps_ctxt->au1_cu_csbf,
6805
3.61M
                        ps_ctxt->i4_cu_csbf_strd,
6806
3.61M
                        trans_size,
6807
3.61M
                        scan_idx,
6808
3.61M
                        1,
6809
3.61M
                        &num_bytes,
6810
3.61M
                        &tu_bits,
6811
3.61M
                        &ps_chr_intra_satd_ctxt->ai4_zero_col_cr[i4_subtu_idx][ctr],
6812
3.61M
                        &ps_chr_intra_satd_ctxt->ai4_zero_row_cr[i4_subtu_idx][ctr],
6813
3.61M
                        &u1_is_recon_available,
6814
3.61M
                        i4_perform_sbh,
6815
3.61M
                        i4_perform_rdoq,
6816
3.61M
                        &trans_ssd_v,
6817
3.61M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
6818
3.61M
                        i4_alpha_stim_multiplier,
6819
3.61M
                        u1_is_cu_noisy,
6820
3.61M
#endif
6821
3.61M
                        0,
6822
3.61M
                        u1_compute_spatial_ssd ? SPATIAL_DOMAIN_SSD : FREQUENCY_DOMAIN_SSD,
6823
3.61M
                        V_PLANE);
6824
6825
#if !USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS && COMPUTE_NOISE_TERM_AT_THE_TU_LEVEL
6826
                    if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
6827
                    {
6828
#if !USE_RECON_TO_EVALUATE_STIM_IN_RDOPT
6829
                        trans_ssd_v = ihevce_inject_stim_into_distortion(
6830
                            pu1_cur_src,
6831
                            chrm_src_stride,
6832
                            pu1_cur_pred,
6833
                            pred_strd,
6834
                            trans_ssd_v,
6835
                            i4_alpha_stim_multiplier,
6836
                            trans_size,
6837
                            0,
6838
                            ps_ctxt->u1_enable_psyRDOPT,
6839
                            V_PLANE);
6840
#else
6841
                        if(u1_compute_spatial_ssd && u1_is_recon_available)
6842
                        {
6843
                            trans_ssd_v = ihevce_inject_stim_into_distortion(
6844
                                pu1_cur_src,
6845
                                chrm_src_stride,
6846
                                pu1_cur_recon,
6847
                                i4_recon_stride,
6848
                                trans_ssd_v,
6849
                                i4_alpha_stim_multiplier,
6850
                                trans_size,
6851
                                0,
6852
                                ps_ctxt->u1_enable_psyRDOPT,
6853
                                V_PLANE);
6854
                        }
6855
                        else
6856
                        {
6857
                            trans_ssd_v = ihevce_inject_stim_into_distortion(
6858
                                pu1_cur_src,
6859
                                chrm_src_stride,
6860
                                pu1_cur_pred,
6861
                                pred_strd,
6862
                                trans_ssd_v,
6863
                                i4_alpha_stim_multiplier,
6864
                                trans_size,
6865
                                0,
6866
                                ps_ctxt->u1_enable_psyRDOPT,
6867
                                V_PLANE);
6868
                        }
6869
#endif
6870
                    }
6871
#endif
6872
6873
                    /* RDOPT copy States :  New updated after curr TU to TU init */
6874
3.61M
                    if(0 != cbf)
6875
441k
                    {
6876
441k
                        COPY_CABAC_STATES(
6877
441k
                            &ps_ctxt->au1_rdopt_init_ctxt_models[0],
6878
441k
                            &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_curr_idx]
6879
441k
                                 .s_cabac_ctxt.au1_ctxt_models[0],
6880
441k
                            IHEVC_CAB_CTXT_END);
6881
441k
                    }
6882
                    /* RDOPT copy States :  Restoring back the Cb init state to Cr */
6883
3.17M
                    else
6884
3.17M
                    {
6885
3.17M
                        COPY_CABAC_STATES(
6886
3.17M
                            &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_curr_idx]
6887
3.17M
                                 .s_cabac_ctxt.au1_ctxt_models[0],
6888
3.17M
                            &ps_ctxt->au1_rdopt_init_ctxt_models[0],
6889
3.17M
                            IHEVC_CAB_CTXT_END);
6890
3.17M
                    }
6891
6892
3.61M
                    if(calc_recon || (!u1_is_recon_available && u1_compute_spatial_ssd))
6893
573k
                    {
6894
573k
                        ihevce_chroma_it_recon_fxn(
6895
573k
                            ps_ctxt,
6896
573k
                            pi2_cur_deq_data_cr,
6897
573k
                            deq_data_strd,
6898
573k
                            pu1_cur_pred,
6899
573k
                            pred_strd,
6900
573k
                            pu1_cur_recon,
6901
573k
                            i4_recon_stride,
6902
573k
                            (pu1_ecd_data_cr + ai4_total_bytes_offset_cr[i4_subtu_idx]),
6903
573k
                            trans_size,
6904
573k
                            cbf,
6905
573k
                            ps_chr_intra_satd_ctxt->ai4_zero_col_cr[i4_subtu_idx][ctr],
6906
573k
                            ps_chr_intra_satd_ctxt->ai4_zero_row_cr[i4_subtu_idx][ctr],
6907
573k
                            V_PLANE);
6908
573k
                    }
6909
6910
3.61M
                    ps_chr_intra_satd_ctxt->au1_cbf_cr[i4_subtu_idx][ctr] = cbf;
6911
3.61M
                    curr_cr_cod_cost =
6912
3.61M
                        trans_ssd_v +
6913
3.61M
                        COMPUTE_RATE_COST_CLIP30(
6914
3.61M
                            tu_bits, ps_ctxt->i8_cl_ssd_lambda_chroma_qf, LAMBDA_Q_SHIFT);
6915
3.61M
                    chrm_tu_bits += tu_bits;
6916
3.61M
                    ai4_total_bytes_offset_cr[i4_subtu_idx] += num_bytes;
6917
3.61M
                    ps_chr_intra_satd_ctxt->ai4_num_bytes_scan_coeff_cr_per_tu[i4_subtu_idx][ctr] =
6918
3.61M
                        num_bytes;
6919
3.61M
                }
6920
6921
3.61M
                chrm_cod_cost += curr_cb_cod_cost;
6922
3.61M
                chrm_cod_cost += curr_cr_cod_cost;
6923
3.61M
            }
6924
6925
            /* set the neighbour map to 1 */
6926
3.61M
            ihevce_set_nbr_map(
6927
3.61M
                ps_ctxt->pu1_ctb_nbr_map,
6928
3.61M
                ps_ctxt->i4_nbr_map_strd,
6929
3.61M
                (ctr & 1) * (luma_trans_size >> 2) + cu_pos_x,
6930
3.61M
                (ctr > 1) * (luma_trans_size >> 2) + cu_pos_y,
6931
3.61M
                (luma_trans_size >> 2),
6932
3.61M
                1);
6933
3.61M
        }
6934
6935
        /* set the neighbour map to 0 */
6936
2.39M
        ihevce_set_nbr_map(
6937
2.39M
            ps_ctxt->pu1_ctb_nbr_map,
6938
2.39M
            ps_ctxt->i4_nbr_map_strd,
6939
2.39M
            (ps_cu_analyse->b3_cu_pos_x << 1),
6940
2.39M
            (ps_cu_analyse->b3_cu_pos_y << 1),
6941
2.39M
            (ps_cu_analyse->u1_cu_size >> 2),
6942
2.39M
            0);
6943
6944
        /* Account for coding b3_chroma_intra_pred_mode prefix and suffix bins */
6945
        /* This is done by adding the bits for signalling chroma mode (0-3)    */
6946
        /* and subtracting the bits for chroma mode same as luma mode (4)      */
6947
2.39M
#if CHROMA_RDOPT_ENABLE
6948
2.39M
        {
6949
            /* Estimate bits to encode prefix bin as 1 for b3_chroma_intra_pred_mode */
6950
2.39M
            WORD32 bits_frac_1 =
6951
2.39M
                gau2_ihevce_cabac_bin_to_bits[u1_chroma_intra_mode_prefix_state ^ 1];
6952
6953
2.39M
            WORD32 bits_for_mode_0to3 = (2 << CABAC_FRAC_BITS_Q) + bits_frac_1;
6954
6955
            /* Estimate bits to encode prefix bin as 0 for b3_chroma_intra_pred_mode */
6956
2.39M
            WORD32 bits_for_mode4 =
6957
2.39M
                gau2_ihevce_cabac_bin_to_bits[u1_chroma_intra_mode_prefix_state ^ 0];
6958
6959
            /* accumulate into final rd cost for chroma */
6960
2.39M
            ps_chr_intra_satd_ctxt->i8_cost_to_encode_chroma_mode = COMPUTE_RATE_COST_CLIP30(
6961
2.39M
                (bits_for_mode_0to3 - bits_for_mode4),
6962
2.39M
                ps_ctxt->i8_cl_ssd_lambda_chroma_qf,
6963
2.39M
                (LAMBDA_Q_SHIFT + CABAC_FRAC_BITS_Q));
6964
6965
2.39M
            chrm_cod_cost += ps_chr_intra_satd_ctxt->i8_cost_to_encode_chroma_mode;
6966
2.39M
        }
6967
2.39M
#endif
6968
6969
2.39M
        if(ps_ctxt->u1_enable_psyRDOPT)
6970
0
        {
6971
0
            UWORD8 *pu1_recon_cu;
6972
0
            WORD32 recon_stride;
6973
0
            WORD32 curr_pos_x;
6974
0
            WORD32 curr_pos_y;
6975
0
            WORD32 start_index;
6976
0
            WORD32 num_horz_cu_in_ctb;
6977
0
            WORD32 had_block_size;
6978
6979
            /* tODO: sreenivasa ctb size has to be used appropriately */
6980
0
            had_block_size = 8;
6981
0
            num_horz_cu_in_ctb = 2 * 64 / had_block_size;
6982
0
            curr_pos_x = ps_cu_analyse->b3_cu_pos_x << 3; /* pel units */
6983
0
            curr_pos_y = ps_cu_analyse->b3_cu_pos_x << 3; /* pel units */
6984
0
            recon_stride = aps_recon_datastore[0]->i4_chromaRecon_stride;
6985
0
            pu1_recon_cu =
6986
0
                aps_recon_datastore[0]->apv_chroma_recon_bufs[1 + (num_tus_in_cu > 1)];  //
6987
6988
            /* start index to index the source satd of curr cu int he current ctb*/
6989
0
            start_index = 2 * (curr_pos_x / had_block_size) +
6990
0
                          (curr_pos_y / had_block_size) * num_horz_cu_in_ctb;
6991
6992
0
            {
6993
0
                chrm_cod_cost += ihevce_psy_rd_cost_croma(
6994
0
                    ps_ctxt->ai4_source_chroma_satd,
6995
0
                    pu1_recon_cu,
6996
0
                    recon_stride,
6997
0
                    1,  //
6998
0
                    cu_size,
6999
0
                    0,  // pic type
7000
0
                    0,  //layer id
7001
0
                    ps_ctxt->i4_satd_lamda,  // lambda
7002
0
                    start_index,
7003
0
                    ps_ctxt->u1_is_input_data_hbd,  // 8 bit
7004
0
                    ps_ctxt->u1_chroma_array_type,
7005
0
                    &ps_ctxt->s_cmn_opt_func
7006
7007
0
                );  // chroma subsampling 420
7008
0
            }
7009
0
        }
7010
7011
2.39M
        ps_chr_intra_satd_ctxt->i8_chroma_best_rdopt = chrm_cod_cost;
7012
2.39M
        ps_chr_intra_satd_ctxt->i4_chrm_tu_bits = chrm_tu_bits;
7013
7014
2.39M
        memcpy(
7015
2.39M
            &ps_chr_intra_satd_ctxt->au1_chrm_satd_updated_ctxt_models[0],
7016
2.39M
            &ps_ctxt->au1_rdopt_init_ctxt_models[0],
7017
2.39M
            IHEVC_CAB_CTXT_END);
7018
2.39M
    }
7019
2.39M
}
7020
7021
/*!
7022
******************************************************************************
7023
* \if Function name : ihevce_chroma_cu_prcs_rdopt \endif
7024
*
7025
* \brief
7026
*    Coding unit processing function for chroma
7027
*
7028
* \param[in] ps_ctxt    enc_loop module ctxt pointer
7029
* \param[in] rd_opt_curr_idx index in the array of RDopt params
7030
* \param[in] func_proc_mode TU_EQ_CU or other case
7031
* \param[in] pu1_chrm_src  pointer to source data buffer
7032
* \param[in] chrm_src_stride   source buffer stride
7033
* \param[in] pu1_cu_left pointer to left recon data buffer
7034
* \param[in] pu1_cu_top  pointer to top recon data buffer
7035
* \param[in] pu1_cu_top_left pointer to top left recon data buffer
7036
* \param[in] left_stride left recon buffer stride
7037
* \param[out] cu_pos_x position x of current CU in CTB
7038
* \param[out] cu_pos_y position y of current CU in CTB
7039
* \param[out] pi4_chrm_tu_bits pointer to store the totla chroma bits
7040
*
7041
* \return
7042
*    Chroma coding cost (cb adn Cr included)
7043
*
7044
* \author
7045
*  Ittiam
7046
*
7047
*****************************************************************************
7048
*/
7049
LWORD64 ihevce_chroma_cu_prcs_rdopt(
7050
    ihevce_enc_loop_ctxt_t *ps_ctxt,
7051
    WORD32 rd_opt_curr_idx,
7052
    WORD32 func_proc_mode,
7053
    UWORD8 *pu1_chrm_src,
7054
    WORD32 chrm_src_stride,
7055
    UWORD8 *pu1_cu_left,
7056
    UWORD8 *pu1_cu_top,
7057
    UWORD8 *pu1_cu_top_left,
7058
    WORD32 cu_left_stride,
7059
    WORD32 cu_pos_x,
7060
    WORD32 cu_pos_y,
7061
    WORD32 *pi4_chrm_tu_bits,
7062
    WORD32 i4_alpha_stim_multiplier,
7063
    UWORD8 u1_is_cu_noisy)
7064
11.9M
{
7065
11.9M
    tu_enc_loop_out_t *ps_tu;
7066
11.9M
    tu_enc_loop_temp_prms_t *ps_tu_temp_prms;
7067
7068
11.9M
    ihevc_intra_pred_chroma_ref_substitution_ft *ihevc_intra_pred_chroma_ref_substitution_fptr;
7069
7070
11.9M
    UWORD8 *pu1_pred;
7071
11.9M
    UWORD8 *pu1_recon;
7072
11.9M
    WORD32 i4_recon_stride;
7073
11.9M
    WORD32 cu_size, trans_size = 0;
7074
11.9M
    WORD32 pred_strd;
7075
11.9M
    WORD32 ctr, i4_subtu_idx;
7076
11.9M
    WORD32 scan_idx;
7077
11.9M
    WORD32 u1_is_cu_coded_old;
7078
11.9M
    WORD32 init_bytes_offset;
7079
7080
11.9M
    enc_loop_cu_final_prms_t *ps_best_cu_prms = &ps_ctxt->as_cu_prms[rd_opt_curr_idx];
7081
11.9M
    recon_datastore_t *ps_recon_datastore = &ps_best_cu_prms->s_recon_datastore;
7082
7083
11.9M
    WORD32 total_bytes_offset = 0;
7084
11.9M
    LWORD64 chrm_cod_cost = 0;
7085
11.9M
    WORD32 chrm_tu_bits = 0;
7086
11.9M
    WORD32 chrm_pred_mode = DM_CHROMA_IDX, luma_pred_mode = 35;
7087
11.9M
    LWORD64 i8_ssd_cb = 0;
7088
11.9M
    WORD32 i4_bits_cb = 0;
7089
11.9M
    LWORD64 i8_ssd_cr = 0;
7090
11.9M
    WORD32 i4_bits_cr = 0;
7091
11.9M
    UWORD8 u1_is_422 = (ps_ctxt->u1_chroma_array_type == 2);
7092
11.9M
    UWORD8 u1_num_tus =
7093
        /* NumChromaTU's = 1, if TUSize = 4 and CUSize = 8 */
7094
11.9M
        (!ps_best_cu_prms->as_tu_enc_loop[0].s_tu.b3_size && ps_best_cu_prms->u1_intra_flag)
7095
11.9M
            ? 1
7096
11.9M
            : ps_best_cu_prms->u2_num_tus_in_cu;
7097
11.9M
    UWORD8 u1_num_subtus_in_tu = u1_is_422 + 1;
7098
11.9M
    UWORD8 u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_QP_WHERE_SPATIAL_SSD_ENABLED) &&
7099
7.46M
                                    (ps_ctxt->i4_quality_preset < IHEVCE_QUALITY_P3) &&
7100
6.80M
                                    CONVERT_SSDS_TO_SPATIAL_DOMAIN;
7101
    /* Get the RDOPT cost of the best CU mode for early_exit */
7102
11.9M
    LWORD64 prev_best_rdopt_cost = ps_ctxt->as_cu_prms[!rd_opt_curr_idx].i8_best_rdopt_cost;
7103
    /* Get the current running RDOPT (Luma RDOPT) for early_exit */
7104
11.9M
    LWORD64 curr_rdopt_cost = ps_ctxt->as_cu_prms[rd_opt_curr_idx].i8_curr_rdopt_cost;
7105
11.9M
    WORD32 i4_perform_rdoq = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_rdoq;
7106
11.9M
    WORD32 i4_perform_sbh = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_all_cand_sbh;
7107
7108
11.9M
    ihevc_intra_pred_chroma_ref_substitution_fptr =
7109
11.9M
        ps_ctxt->ps_func_selector->ihevc_intra_pred_chroma_ref_substitution_fptr;
7110
7111
11.9M
    if(u1_is_cu_noisy || ps_ctxt->u1_enable_psyRDOPT)
7112
0
    {
7113
0
        u1_compute_spatial_ssd = (ps_ctxt->i4_cu_qp <= MAX_HEVC_QP) &&
7114
0
                                 CONVERT_SSDS_TO_SPATIAL_DOMAIN;
7115
0
    }
7116
7117
    /* Store the init bytes offset from luma */
7118
11.9M
    init_bytes_offset = ps_best_cu_prms->i4_num_bytes_ecd_data;
7119
7120
    /* Unused pred buffer in merge_skip_pred_data_t structure is used as
7121
    Chroma pred storage buf. for final_recon function.
7122
    The buffer is split into two and used as a ping-pong buffer */
7123
11.9M
    pu1_pred = ps_ctxt->s_cu_me_intra_pred_prms.pu1_pred_data[CU_ME_INTRA_PRED_CHROMA_IDX] +
7124
11.9M
               rd_opt_curr_idx * ((MAX_CTB_SIZE * MAX_CTB_SIZE >> 1) +
7125
11.9M
                                  (u1_is_422 * (MAX_CTB_SIZE * MAX_CTB_SIZE >> 1)));
7126
7127
11.9M
    pred_strd = ps_ctxt->s_cu_me_intra_pred_prms.ai4_pred_data_stride[CU_ME_INTRA_PRED_CHROMA_IDX];
7128
7129
11.9M
    pu1_recon = (UWORD8 *)ps_recon_datastore->apv_chroma_recon_bufs[0];
7130
11.9M
    i4_recon_stride = ps_recon_datastore->i4_chromaRecon_stride;
7131
11.9M
    cu_size = ps_best_cu_prms->u1_cu_size;
7132
11.9M
    chrm_tu_bits = 0;
7133
7134
    /* get the first TU pointer */
7135
11.9M
    ps_tu = &ps_best_cu_prms->as_tu_enc_loop[0];
7136
    /* get the first TU enc_loop temp prms pointer */
7137
11.9M
    ps_tu_temp_prms = &ps_best_cu_prms->as_tu_enc_loop_temp_prms[0];
7138
7139
11.9M
    if(PRED_MODE_INTRA == ps_best_cu_prms->u1_intra_flag)
7140
7.71M
    {
7141
        /* Mode signalled by intra prediction for luma */
7142
7.71M
        luma_pred_mode = ps_best_cu_prms->au1_intra_pred_mode[0];
7143
7144
#if DISABLE_RDOQ_INTRA
7145
        i4_perform_rdoq = 0;
7146
#endif
7147
7.71M
    }
7148
7149
4.26M
    else
7150
4.26M
    {
7151
4.26M
        UWORD8 *pu1_pred_org = pu1_pred;
7152
7153
        /* ------ Motion Compensation for Chroma -------- */
7154
9.39M
        for(ctr = 0; ctr < ps_best_cu_prms->u2_num_pus_in_cu; ctr++)
7155
5.12M
        {
7156
5.12M
            pu_t *ps_pu;
7157
5.12M
            WORD32 inter_pu_wd;
7158
5.12M
            WORD32 inter_pu_ht;
7159
7160
5.12M
            ps_pu = &ps_best_cu_prms->as_pu_chrm_proc[ctr];
7161
7162
5.12M
            inter_pu_wd = (ps_pu->b4_wd + 1) << 2; /* cb and cr pixel interleaved */
7163
5.12M
            inter_pu_ht = ((ps_pu->b4_ht + 1) << 2) >> 1;
7164
5.12M
            inter_pu_ht <<= u1_is_422;
7165
7166
5.12M
            ihevce_chroma_inter_pred_pu(&ps_ctxt->s_mc_ctxt, ps_pu, pu1_pred, pred_strd);
7167
7168
5.12M
            if(2 == ps_best_cu_prms->u2_num_pus_in_cu)
7169
1.71M
            {
7170
                /* 2Nx__ partion case */
7171
1.71M
                if(inter_pu_wd == cu_size)
7172
1.40M
                {
7173
1.40M
                    pu1_pred += (inter_pu_ht * pred_strd);
7174
1.40M
                }
7175
7176
                /* __x2N partion case */
7177
1.71M
                if(inter_pu_ht == (cu_size >> (u1_is_422 == 0)))
7178
311k
                {
7179
311k
                    pu1_pred += inter_pu_wd;
7180
311k
                }
7181
1.71M
            }
7182
5.12M
        }
7183
7184
        /* restore the pred pointer to start for transform loop */
7185
4.26M
        pu1_pred = pu1_pred_org;
7186
4.26M
    }
7187
7188
    /* Used to store back only the luma based info. if SATD based chorma
7189
    mode also comes */
7190
11.9M
    u1_is_cu_coded_old = ps_best_cu_prms->u1_is_cu_coded;
7191
7192
    /* evaluate chroma candidates (same as luma) and
7193
    if INTRA & HIGH_QUALITY compare with best SATD mode */
7194
11.9M
    {
7195
11.9M
        WORD32 calc_recon = 0, deq_data_strd;
7196
11.9M
        WORD16 *pi2_deq_data;
7197
11.9M
        UWORD8 *pu1_ecd_data;
7198
11.9M
        UWORD8 u1_is_mode_eq_chroma_satd_mode = 0;
7199
7200
11.9M
        pi2_deq_data = &ps_best_cu_prms->pi2_cu_deq_coeffs[0];
7201
11.9M
        pi2_deq_data += ps_best_cu_prms->i4_chrm_deq_coeff_strt_idx;
7202
11.9M
        deq_data_strd = cu_size;
7203
        /* update ecd buffer for storing coeff. */
7204
11.9M
        pu1_ecd_data = &ps_best_cu_prms->pu1_cu_coeffs[0];
7205
11.9M
        pu1_ecd_data += init_bytes_offset;
7206
        /* store chroma starting index */
7207
11.9M
        ps_best_cu_prms->i4_chrm_cu_coeff_strt_idx = init_bytes_offset;
7208
7209
        /* get the first TU pointer */
7210
11.9M
        ps_tu = &ps_best_cu_prms->as_tu_enc_loop[0];
7211
11.9M
        ps_tu_temp_prms = &ps_best_cu_prms->as_tu_enc_loop_temp_prms[0];
7212
7213
        /* Reset total_bytes_offset for each candidate */
7214
11.9M
        chrm_pred_mode = (u1_is_422 == 1) ? gau1_chroma422_intra_angle_mapping[luma_pred_mode]
7215
11.9M
                                          : luma_pred_mode;
7216
7217
11.9M
        total_bytes_offset = 0;
7218
7219
11.9M
        if(TU_EQ_SUBCU == func_proc_mode)
7220
483k
        {
7221
483k
            func_proc_mode = TU_EQ_CU_DIV2;
7222
483k
        }
7223
7224
        /* For cu_size=8 case, chroma cost will be same for TU_EQ_CU and
7225
        TU_EQ_CU_DIV2 and  TU_EQ_SUBCU case */
7226
11.9M
        if(8 == cu_size)
7227
4.84M
        {
7228
4.84M
            func_proc_mode = TU_EQ_CU;
7229
4.84M
        }
7230
7231
        /* loop based on num tus in a cu */
7232
11.9M
        if(!ps_best_cu_prms->u1_intra_flag || !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_satd ||
7233
6.91M
           (ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_satd &&
7234
6.91M
            (chrm_pred_mode !=
7235
6.91M
             ps_ctxt->s_chroma_rdopt_ctxt.as_chr_intra_satd_ctxt[func_proc_mode].u1_best_cr_mode)))
7236
9.96M
        {
7237
            /* loop based on num tus in a cu */
7238
23.4M
            for(ctr = 0; ctr < u1_num_tus; ctr++)
7239
14.6M
            {
7240
14.6M
                WORD32 num_bytes = 0;
7241
14.6M
                LWORD64 curr_cb_cod_cost = 0;
7242
14.6M
                LWORD64 curr_cr_cod_cost = 0;
7243
14.6M
                WORD32 chrm_pred_func_idx = 0;
7244
14.6M
                UWORD8 u1_is_early_exit_condition_satisfied = 0;
7245
7246
                /* Default cb and cr offset initializatio for b3_chroma_intra_mode_idx=7   */
7247
                /* FIX for TU tree shrinkage caused by ecd data copies in final mode recon */
7248
14.6M
                ps_tu->s_tu.b1_cb_cbf = ps_tu->s_tu.b1_cr_cbf = 0;
7249
14.6M
                ps_tu->s_tu.b1_cb_cbf_subtu1 = ps_tu->s_tu.b1_cr_cbf_subtu1 = 0;
7250
14.6M
                ps_tu->ai4_cb_coeff_offset[0] = total_bytes_offset + init_bytes_offset;
7251
14.6M
                ps_tu->ai4_cr_coeff_offset[0] = total_bytes_offset + init_bytes_offset;
7252
14.6M
                ps_tu->ai4_cb_coeff_offset[1] = total_bytes_offset + init_bytes_offset;
7253
14.6M
                ps_tu->ai4_cr_coeff_offset[1] = total_bytes_offset + init_bytes_offset;
7254
14.6M
                ps_tu_temp_prms->ai2_cb_bytes_consumed[0] = 0;
7255
14.6M
                ps_tu_temp_prms->ai2_cr_bytes_consumed[0] = 0;
7256
14.6M
                ps_tu_temp_prms->ai2_cb_bytes_consumed[1] = 0;
7257
14.6M
                ps_tu_temp_prms->ai2_cr_bytes_consumed[1] = 0;
7258
7259
                /* TU level inits */
7260
                /* check if chroma present flag is set */
7261
14.6M
                if(1 == ps_tu->s_tu.b3_chroma_intra_mode_idx)
7262
13.6M
                {
7263
                    /* RDOPT copy States :  TU init (best until prev TU) to current */
7264
13.6M
                    COPY_CABAC_STATES(
7265
13.6M
                        &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_curr_idx]
7266
13.6M
                             .s_cabac_ctxt.au1_ctxt_models[0],
7267
13.6M
                        &ps_ctxt->au1_rdopt_init_ctxt_models[0],
7268
13.6M
                        IHEVC_CAB_CTXT_END);
7269
7270
                    /* get the current transform size */
7271
13.6M
                    trans_size = ps_tu->s_tu.b3_size;
7272
13.6M
                    trans_size = (1 << (trans_size + 1)); /* in chroma units */
7273
7274
                    /* since 2x2 transform is not allowed for chroma*/
7275
13.6M
                    if(2 == trans_size)
7276
993k
                    {
7277
993k
                        trans_size = 4;
7278
993k
                    }
7279
13.6M
                }
7280
7281
28.1M
                for(i4_subtu_idx = 0; i4_subtu_idx < u1_num_subtus_in_tu; i4_subtu_idx++)
7282
14.6M
                {
7283
14.6M
                    WORD32 cbf;
7284
14.6M
                    UWORD8 u1_is_recon_available;
7285
7286
14.6M
                    WORD32 nbr_flags = 0;
7287
14.6M
                    WORD32 zero_cols = 0;
7288
14.6M
                    WORD32 zero_rows = 0;
7289
7290
                    /* check if chroma present flag is set */
7291
14.6M
                    if(1 == ps_tu->s_tu.b3_chroma_intra_mode_idx)
7292
13.6M
                    {
7293
13.6M
                        UWORD8 *pu1_cur_pred;
7294
13.6M
                        UWORD8 *pu1_cur_recon;
7295
13.6M
                        UWORD8 *pu1_cur_src;
7296
13.6M
                        WORD16 *pi2_cur_deq_data;
7297
13.6M
                        WORD32 curr_pos_x, curr_pos_y;
7298
13.6M
                        LWORD64 trans_ssd_u, trans_ssd_v;
7299
7300
                        /* get the current sub-tu posx and posy w.r.t to cu */
7301
13.6M
                        curr_pos_x = (ps_tu->s_tu.b4_pos_x << 2) - (cu_pos_x << 3);
7302
13.6M
                        curr_pos_y = (ps_tu->s_tu.b4_pos_y << 2) - (cu_pos_y << 3) +
7303
13.6M
                                     (i4_subtu_idx * trans_size);
7304
7305
                        /* 420sp case only vertical height will be half */
7306
13.6M
                        if(u1_is_422 == 0)
7307
13.6M
                        {
7308
13.6M
                            curr_pos_y >>= 1;
7309
13.6M
                        }
7310
7311
                        /* increment the pointers to start of current Sub-TU */
7312
13.6M
                        pu1_cur_recon = (pu1_recon + curr_pos_x);
7313
13.6M
                        pu1_cur_recon += (curr_pos_y * i4_recon_stride);
7314
13.6M
                        pu1_cur_src = (pu1_chrm_src + curr_pos_x);
7315
13.6M
                        pu1_cur_src += (curr_pos_y * chrm_src_stride);
7316
13.6M
                        pu1_cur_pred = (pu1_pred + curr_pos_x);
7317
13.6M
                        pu1_cur_pred += (curr_pos_y * pred_strd);
7318
13.6M
                        pi2_cur_deq_data = pi2_deq_data + curr_pos_x;
7319
13.6M
                        pi2_cur_deq_data += (curr_pos_y * deq_data_strd);
7320
7321
                        /* populate the coeffs scan idx */
7322
13.6M
                        scan_idx = SCAN_DIAG_UPRIGHT;
7323
7324
                        /* perform intra prediction only for Intra case */
7325
13.6M
                        if(PRED_MODE_INTRA == ps_best_cu_prms->u1_intra_flag)
7326
8.40M
                        {
7327
8.40M
                            UWORD8 *pu1_top_left;
7328
8.40M
                            UWORD8 *pu1_top;
7329
8.40M
                            UWORD8 *pu1_left;
7330
8.40M
                            WORD32 left_strd;
7331
7332
8.40M
                            calc_recon = !u1_compute_spatial_ssd &&
7333
3.95M
                                         ((4 == u1_num_tus) || (u1_is_422 == 1)) &&
7334
1.66M
                                         (((u1_num_tus == 1) && (0 == i4_subtu_idx)) ||
7335
1.66M
                                          ((ctr == 3) && (0 == i4_subtu_idx) && (u1_is_422 == 1)) ||
7336
1.66M
                                          ((u1_num_tus == 4) && (ctr < 3)));
7337
7338
                            /* left cu boundary */
7339
8.40M
                            if(0 == curr_pos_x)
7340
6.59M
                            {
7341
6.59M
                                pu1_left = pu1_cu_left + curr_pos_y * cu_left_stride;
7342
6.59M
                                left_strd = cu_left_stride;
7343
6.59M
                            }
7344
1.80M
                            else
7345
1.80M
                            {
7346
1.80M
                                pu1_left = pu1_cur_recon - 2;
7347
1.80M
                                left_strd = i4_recon_stride;
7348
1.80M
                            }
7349
7350
                            /* top cu boundary */
7351
8.40M
                            if(0 == curr_pos_y)
7352
6.61M
                            {
7353
6.61M
                                pu1_top = pu1_cu_top + curr_pos_x;
7354
6.61M
                            }
7355
1.78M
                            else
7356
1.78M
                            {
7357
1.78M
                                pu1_top = pu1_cur_recon - i4_recon_stride;
7358
1.78M
                            }
7359
7360
                            /* by default top left is set to cu top left */
7361
8.40M
                            pu1_top_left = pu1_cu_top_left;
7362
7363
                            /* top left based on position */
7364
8.40M
                            if((0 != curr_pos_y) && (0 == curr_pos_x))
7365
903k
                            {
7366
903k
                                pu1_top_left = pu1_left - cu_left_stride;
7367
903k
                            }
7368
7.49M
                            else if(0 != curr_pos_x)
7369
1.80M
                            {
7370
1.80M
                                pu1_top_left = pu1_top - 2;
7371
1.80M
                            }
7372
7373
                            /* for 4x4 transforms based on intra pred mode scan is choosen*/
7374
8.40M
                            if(4 == trans_size)
7375
4.73M
                            {
7376
                                /* for modes from 22 upto 30 horizontal scan is used */
7377
4.73M
                                if((chrm_pred_mode > 21) && (chrm_pred_mode < 31))
7378
1.70M
                                {
7379
1.70M
                                    scan_idx = SCAN_HORZ;
7380
1.70M
                                }
7381
                                /* for modes from 6 upto 14 horizontal scan is used */
7382
3.03M
                                else if((chrm_pred_mode > 5) && (chrm_pred_mode < 15))
7383
847k
                                {
7384
847k
                                    scan_idx = SCAN_VERT;
7385
847k
                                }
7386
4.73M
                            }
7387
7388
8.40M
                            nbr_flags = ihevce_get_intra_chroma_tu_nbr(
7389
8.40M
                                ps_best_cu_prms->au4_nbr_flags[ctr],
7390
8.40M
                                i4_subtu_idx,
7391
8.40M
                                trans_size,
7392
8.40M
                                u1_is_422);
7393
7394
                            /* call the chroma reference array substitution */
7395
8.40M
                            ihevc_intra_pred_chroma_ref_substitution_fptr(
7396
8.40M
                                pu1_top_left,
7397
8.40M
                                pu1_top,
7398
8.40M
                                pu1_left,
7399
8.40M
                                left_strd,
7400
8.40M
                                trans_size,
7401
8.40M
                                nbr_flags,
7402
8.40M
                                (UWORD8 *)ps_ctxt->pv_ref_sub_out,
7403
8.40M
                                1,
7404
8.40M
                                CHROMA_FMT_IDC_YUV420);
7405
7406
                            /* use the look up to get the function idx */
7407
8.40M
                            chrm_pred_func_idx = g_i4_ip_funcs[chrm_pred_mode];
7408
7409
                            /* call the intra prediction function */
7410
8.40M
                            ps_ctxt->apf_chrm_ip[chrm_pred_func_idx](
7411
8.40M
                                (UWORD8 *)ps_ctxt->pv_ref_sub_out,
7412
8.40M
                                1,
7413
8.40M
                                pu1_cur_pred,
7414
8.40M
                                pred_strd,
7415
8.40M
                                trans_size,
7416
8.40M
                                chrm_pred_mode);
7417
8.40M
                        }
7418
7419
13.6M
                        if(!ctr && !i4_subtu_idx && (u1_compute_spatial_ssd || calc_recon))
7420
5.97M
                        {
7421
5.97M
                            ps_recon_datastore->au1_is_chromaRecon_available[0] =
7422
5.97M
                                !ps_best_cu_prms->u1_skip_flag;
7423
5.97M
                        }
7424
7.70M
                        else if(!ctr && !i4_subtu_idx)
7425
3.98M
                        {
7426
3.98M
                            ps_recon_datastore->au1_is_chromaRecon_available[0] = 0;
7427
3.98M
                        }
7428
                        /************************************************************/
7429
                        /* recon loop is done for all cases including skip cu       */
7430
                        /* This is because skipping chroma reisdual based on luma   */
7431
                        /* skip decision can lead to chroma artifacts               */
7432
                        /************************************************************/
7433
                        /************************************************************/
7434
                        /*In the high quality and medium speed modes, wherein chroma*/
7435
                        /*and luma costs are included in the total cost calculation */
7436
                        /*the cost is just a ssd cost, and not that obtained through*/
7437
                        /*iq_it path                                                */
7438
                        /************************************************************/
7439
13.6M
                        if(ps_best_cu_prms->u1_skip_flag == 0)
7440
12.4M
                        {
7441
12.4M
                            WORD32 tu_bits;
7442
7443
12.4M
                            cbf = ihevce_chroma_t_q_iq_ssd_scan_fxn(
7444
12.4M
                                ps_ctxt,
7445
12.4M
                                pu1_cur_pred,
7446
12.4M
                                pred_strd,
7447
12.4M
                                pu1_cur_src,
7448
12.4M
                                chrm_src_stride,
7449
12.4M
                                pi2_cur_deq_data,
7450
12.4M
                                deq_data_strd,
7451
12.4M
                                pu1_cur_recon,
7452
12.4M
                                i4_recon_stride,
7453
12.4M
                                pu1_ecd_data + total_bytes_offset,
7454
12.4M
                                ps_ctxt->au1_cu_csbf,
7455
12.4M
                                ps_ctxt->i4_cu_csbf_strd,
7456
12.4M
                                trans_size,
7457
12.4M
                                scan_idx,
7458
12.4M
                                PRED_MODE_INTRA == ps_best_cu_prms->u1_intra_flag,
7459
12.4M
                                &num_bytes,
7460
12.4M
                                &tu_bits,
7461
12.4M
                                &zero_cols,
7462
12.4M
                                &zero_rows,
7463
12.4M
                                &u1_is_recon_available,
7464
12.4M
                                i4_perform_sbh,
7465
12.4M
                                i4_perform_rdoq,
7466
12.4M
                                &trans_ssd_u,
7467
12.4M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
7468
12.4M
                                i4_alpha_stim_multiplier,
7469
12.4M
                                u1_is_cu_noisy,
7470
12.4M
#endif
7471
12.4M
                                ps_best_cu_prms->u1_skip_flag,
7472
12.4M
                                u1_compute_spatial_ssd ? SPATIAL_DOMAIN_SSD : FREQUENCY_DOMAIN_SSD,
7473
12.4M
                                U_PLANE);
7474
7475
12.4M
                            if(u1_compute_spatial_ssd && u1_is_recon_available)
7476
6.82M
                            {
7477
6.82M
                                ps_recon_datastore
7478
6.82M
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
7479
6.82M
                                                                        [i4_subtu_idx] = 0;
7480
6.82M
                            }
7481
5.63M
                            else
7482
5.63M
                            {
7483
5.63M
                                ps_recon_datastore
7484
5.63M
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
7485
5.63M
                                                                        [i4_subtu_idx] = UCHAR_MAX;
7486
5.63M
                            }
7487
7488
#if !USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
7489
                            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
7490
                            {
7491
#if !USE_RECON_TO_EVALUATE_STIM_IN_RDOPT
7492
                                trans_ssd_u = ihevce_inject_stim_into_distortion(
7493
                                    pu1_cur_src,
7494
                                    chrm_src_stride,
7495
                                    pu1_cur_pred,
7496
                                    pred_strd,
7497
                                    trans_ssd_u,
7498
                                    i4_alpha_stim_multiplier,
7499
                                    trans_size,
7500
                                    0,
7501
                                    ps_ctxt->u1_enable_psyRDOPT,
7502
                                    U_PLANE);
7503
#else
7504
                                if(u1_compute_spatial_ssd && u1_is_recon_available)
7505
                                {
7506
                                    trans_ssd_u = ihevce_inject_stim_into_distortion(
7507
                                        pu1_cur_src,
7508
                                        chrm_src_stride,
7509
                                        pu1_cur_recon,
7510
                                        i4_recon_stride,
7511
                                        trans_ssd_u,
7512
                                        i4_alpha_stim_multiplier,
7513
                                        trans_size,
7514
                                        0,
7515
                                        ps_ctxt->u1_enable_psyRDOPT,
7516
                                        U_PLANE);
7517
                                }
7518
                                else
7519
                                {
7520
                                    trans_ssd_u = ihevce_inject_stim_into_distortion(
7521
                                        pu1_cur_src,
7522
                                        chrm_src_stride,
7523
                                        pu1_cur_pred,
7524
                                        pred_strd,
7525
                                        trans_ssd_u,
7526
                                        i4_alpha_stim_multiplier,
7527
                                        trans_size,
7528
                                        0,
7529
                                        ps_ctxt->u1_enable_psyRDOPT,
7530
                                        U_PLANE);
7531
                                }
7532
#endif
7533
                            }
7534
#endif
7535
7536
12.4M
                            curr_cb_cod_cost =
7537
12.4M
                                trans_ssd_u +
7538
12.4M
                                COMPUTE_RATE_COST_CLIP30(
7539
12.4M
                                    tu_bits, ps_ctxt->i8_cl_ssd_lambda_chroma_qf, LAMBDA_Q_SHIFT);
7540
7541
12.4M
                            chrm_tu_bits += tu_bits;
7542
12.4M
                            i4_bits_cb += tu_bits;
7543
7544
                            /* RDOPT copy States :  New updated after curr TU to TU init */
7545
12.4M
                            if(0 != cbf)
7546
2.07M
                            {
7547
2.07M
                                COPY_CABAC_STATES(
7548
2.07M
                                    &ps_ctxt->au1_rdopt_init_ctxt_models[0],
7549
2.07M
                                    &ps_ctxt->s_rdopt_entropy_ctxt
7550
2.07M
                                         .as_cu_entropy_ctxt[rd_opt_curr_idx]
7551
2.07M
                                         .s_cabac_ctxt.au1_ctxt_models[0],
7552
2.07M
                                    IHEVC_CAB_CTXT_END);
7553
2.07M
                            }
7554
                            /* RDOPT copy States :  Restoring back the Cb init state to Cr */
7555
10.3M
                            else
7556
10.3M
                            {
7557
10.3M
                                COPY_CABAC_STATES(
7558
10.3M
                                    &ps_ctxt->s_rdopt_entropy_ctxt
7559
10.3M
                                         .as_cu_entropy_ctxt[rd_opt_curr_idx]
7560
10.3M
                                         .s_cabac_ctxt.au1_ctxt_models[0],
7561
10.3M
                                    &ps_ctxt->au1_rdopt_init_ctxt_models[0],
7562
10.3M
                                    IHEVC_CAB_CTXT_END);
7563
10.3M
                            }
7564
7565
                            /* If Intra and TU=CU/2, need recon for next TUs */
7566
12.4M
                            if(calc_recon)
7567
1.27M
                            {
7568
1.27M
                                ihevce_chroma_it_recon_fxn(
7569
1.27M
                                    ps_ctxt,
7570
1.27M
                                    pi2_cur_deq_data,
7571
1.27M
                                    deq_data_strd,
7572
1.27M
                                    pu1_cur_pred,
7573
1.27M
                                    pred_strd,
7574
1.27M
                                    pu1_cur_recon,
7575
1.27M
                                    i4_recon_stride,
7576
1.27M
                                    (pu1_ecd_data + total_bytes_offset),
7577
1.27M
                                    trans_size,
7578
1.27M
                                    cbf,
7579
1.27M
                                    zero_cols,
7580
1.27M
                                    zero_rows,
7581
1.27M
                                    U_PLANE);
7582
7583
1.27M
                                ps_recon_datastore
7584
1.27M
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
7585
1.27M
                                                                        [i4_subtu_idx] = 0;
7586
1.27M
                            }
7587
11.1M
                            else
7588
11.1M
                            {
7589
11.1M
                                ps_recon_datastore
7590
11.1M
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
7591
11.1M
                                                                        [i4_subtu_idx] = UCHAR_MAX;
7592
11.1M
                            }
7593
12.4M
                        }
7594
1.22M
                        else
7595
1.22M
                        {
7596
                            /* num bytes is set to 0 */
7597
1.22M
                            num_bytes = 0;
7598
7599
                            /* cbf is returned as 0 */
7600
1.22M
                            cbf = 0;
7601
7602
1.22M
                            curr_cb_cod_cost = trans_ssd_u =
7603
7604
1.22M
                                ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_ssd_calculator(
7605
1.22M
                                    pu1_cur_pred,
7606
1.22M
                                    pu1_cur_src,
7607
1.22M
                                    pred_strd,
7608
1.22M
                                    chrm_src_stride,
7609
1.22M
                                    trans_size,
7610
1.22M
                                    trans_size,
7611
1.22M
                                    U_PLANE);
7612
7613
1.22M
                            if(u1_compute_spatial_ssd)
7614
734k
                            {
7615
                                /* buffer copy fromp pred to recon */
7616
7617
734k
                                ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
7618
734k
                                    pu1_cur_pred,
7619
734k
                                    pred_strd,
7620
734k
                                    pu1_cur_recon,
7621
734k
                                    i4_recon_stride,
7622
734k
                                    trans_size,
7623
734k
                                    trans_size,
7624
734k
                                    U_PLANE);
7625
7626
734k
                                ps_recon_datastore
7627
734k
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
7628
734k
                                                                        [i4_subtu_idx] = 0;
7629
734k
                            }
7630
7631
1.22M
                            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
7632
0
                            {
7633
0
                                trans_ssd_u = ihevce_inject_stim_into_distortion(
7634
0
                                    pu1_cur_src,
7635
0
                                    chrm_src_stride,
7636
0
                                    pu1_cur_pred,
7637
0
                                    pred_strd,
7638
0
                                    trans_ssd_u,
7639
0
                                    i4_alpha_stim_multiplier,
7640
0
                                    trans_size,
7641
0
                                    0,
7642
0
                                    ps_ctxt->u1_enable_psyRDOPT,
7643
0
                                    U_PLANE);
7644
0
                            }
7645
7646
1.22M
#if ENABLE_INTER_ZCU_COST
7647
#if !WEIGH_CHROMA_COST
7648
                            /* cbf = 0, accumulate cu not coded cost */
7649
                            ps_ctxt->i8_cu_not_coded_cost += curr_cb_cod_cost;
7650
#else
7651
                            /* cbf = 0, accumulate cu not coded cost */
7652
7653
1.22M
                            ps_ctxt->i8_cu_not_coded_cost += (LWORD64)(
7654
1.22M
                                (curr_cb_cod_cost * ps_ctxt->u4_chroma_cost_weighing_factor +
7655
1.22M
                                 (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
7656
1.22M
                                CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
7657
1.22M
#endif
7658
1.22M
#endif
7659
1.22M
                        }
7660
7661
#if !WEIGH_CHROMA_COST
7662
                        curr_rdopt_cost += curr_cb_cod_cost;
7663
#else
7664
13.6M
                        curr_rdopt_cost +=
7665
13.6M
                            ((curr_cb_cod_cost * ps_ctxt->u4_chroma_cost_weighing_factor +
7666
13.6M
                              (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
7667
13.6M
                             CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
7668
13.6M
#endif
7669
13.6M
                        chrm_cod_cost += curr_cb_cod_cost;
7670
13.6M
                        i8_ssd_cb += trans_ssd_u;
7671
7672
13.6M
                        if(ps_ctxt->i4_bitrate_instance_num || ps_ctxt->i4_num_bitrates == 1)
7673
13.6M
                        {
7674
                            /* Early exit : If the current running cost exceeds
7675
                            the prev. best mode cost, break */
7676
13.6M
                            if(curr_rdopt_cost > prev_best_rdopt_cost)
7677
667k
                            {
7678
667k
                                u1_is_early_exit_condition_satisfied = 1;
7679
667k
                                break;
7680
667k
                            }
7681
13.6M
                        }
7682
7683
                        /* inter cu is coded if any of the tu is coded in it */
7684
13.0M
                        ps_best_cu_prms->u1_is_cu_coded |= cbf;
7685
7686
                        /* update CB related params */
7687
13.0M
                        ps_tu->ai4_cb_coeff_offset[i4_subtu_idx] =
7688
13.0M
                            total_bytes_offset + init_bytes_offset;
7689
7690
13.0M
                        if(0 == i4_subtu_idx)
7691
13.0M
                        {
7692
13.0M
                            ps_tu->s_tu.b1_cb_cbf = cbf;
7693
13.0M
                        }
7694
0
                        else
7695
0
                        {
7696
0
                            ps_tu->s_tu.b1_cb_cbf_subtu1 = cbf;
7697
0
                        }
7698
7699
13.0M
                        total_bytes_offset += num_bytes;
7700
7701
13.0M
                        ps_tu_temp_prms->au4_cb_zero_col[i4_subtu_idx] = zero_cols;
7702
13.0M
                        ps_tu_temp_prms->au4_cb_zero_row[i4_subtu_idx] = zero_rows;
7703
13.0M
                        ps_tu_temp_prms->ai2_cb_bytes_consumed[i4_subtu_idx] = num_bytes;
7704
7705
                        /* recon loop is done for non skip cases */
7706
13.0M
                        if(ps_best_cu_prms->u1_skip_flag == 0)
7707
11.9M
                        {
7708
11.9M
                            WORD32 tu_bits;
7709
7710
11.9M
                            cbf = ihevce_chroma_t_q_iq_ssd_scan_fxn(
7711
11.9M
                                ps_ctxt,
7712
11.9M
                                pu1_cur_pred,
7713
11.9M
                                pred_strd,
7714
11.9M
                                pu1_cur_src,
7715
11.9M
                                chrm_src_stride,
7716
11.9M
                                pi2_cur_deq_data + trans_size,
7717
11.9M
                                deq_data_strd,
7718
11.9M
                                pu1_cur_recon,
7719
11.9M
                                i4_recon_stride,
7720
11.9M
                                pu1_ecd_data + total_bytes_offset,
7721
11.9M
                                ps_ctxt->au1_cu_csbf,
7722
11.9M
                                ps_ctxt->i4_cu_csbf_strd,
7723
11.9M
                                trans_size,
7724
11.9M
                                scan_idx,
7725
11.9M
                                PRED_MODE_INTRA == ps_best_cu_prms->u1_intra_flag,
7726
11.9M
                                &num_bytes,
7727
11.9M
                                &tu_bits,
7728
11.9M
                                &zero_cols,
7729
11.9M
                                &zero_rows,
7730
11.9M
                                &u1_is_recon_available,
7731
11.9M
                                i4_perform_sbh,
7732
11.9M
                                i4_perform_rdoq,
7733
11.9M
                                &trans_ssd_v,
7734
11.9M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
7735
11.9M
                                i4_alpha_stim_multiplier,
7736
11.9M
                                u1_is_cu_noisy,
7737
11.9M
#endif
7738
11.9M
                                ps_best_cu_prms->u1_skip_flag,
7739
11.9M
                                u1_compute_spatial_ssd ? SPATIAL_DOMAIN_SSD : FREQUENCY_DOMAIN_SSD,
7740
11.9M
                                V_PLANE);
7741
7742
11.9M
                            if(u1_compute_spatial_ssd && u1_is_recon_available)
7743
6.67M
                            {
7744
6.67M
                                ps_recon_datastore
7745
6.67M
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
7746
6.67M
                                                                        [i4_subtu_idx] = 0;
7747
6.67M
                            }
7748
5.30M
                            else
7749
5.30M
                            {
7750
5.30M
                                ps_recon_datastore
7751
5.30M
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
7752
5.30M
                                                                        [i4_subtu_idx] = UCHAR_MAX;
7753
5.30M
                            }
7754
7755
#if !USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
7756
                            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
7757
                            {
7758
#if !USE_RECON_TO_EVALUATE_STIM_IN_RDOPT
7759
                                trans_ssd_v = ihevce_inject_stim_into_distortion(
7760
                                    pu1_cur_src,
7761
                                    chrm_src_stride,
7762
                                    pu1_cur_pred,
7763
                                    pred_strd,
7764
                                    trans_ssd_v,
7765
                                    i4_alpha_stim_multiplier,
7766
                                    trans_size,
7767
                                    0,
7768
                                    ps_ctxt->u1_enable_psyRDOPT,
7769
                                    V_PLANE);
7770
#else
7771
                                if(u1_compute_spatial_ssd && u1_is_recon_available)
7772
                                {
7773
                                    trans_ssd_v = ihevce_inject_stim_into_distortion(
7774
                                        pu1_cur_src,
7775
                                        chrm_src_stride,
7776
                                        pu1_cur_recon,
7777
                                        i4_recon_stride,
7778
                                        trans_ssd_v,
7779
                                        i4_alpha_stim_multiplier,
7780
                                        trans_size,
7781
                                        0,
7782
                                        ps_ctxt->u1_enable_psyRDOPT,
7783
                                        V_PLANE);
7784
                                }
7785
                                else
7786
                                {
7787
                                    trans_ssd_v = ihevce_inject_stim_into_distortion(
7788
                                        pu1_cur_src,
7789
                                        chrm_src_stride,
7790
                                        pu1_cur_pred,
7791
                                        pred_strd,
7792
                                        trans_ssd_v,
7793
                                        i4_alpha_stim_multiplier,
7794
                                        trans_size,
7795
                                        0,
7796
                                        ps_ctxt->u1_enable_psyRDOPT,
7797
                                        V_PLANE);
7798
                                }
7799
#endif
7800
                            }
7801
#endif
7802
7803
11.9M
                            curr_cr_cod_cost =
7804
11.9M
                                trans_ssd_v +
7805
11.9M
                                COMPUTE_RATE_COST_CLIP30(
7806
11.9M
                                    tu_bits, ps_ctxt->i8_cl_ssd_lambda_chroma_qf, LAMBDA_Q_SHIFT);
7807
11.9M
                            chrm_tu_bits += tu_bits;
7808
11.9M
                            i4_bits_cr += tu_bits;
7809
7810
                            /* RDOPT copy States :  New updated after curr TU to TU init */
7811
11.9M
                            if(0 != cbf)
7812
1.76M
                            {
7813
1.76M
                                COPY_CABAC_STATES(
7814
1.76M
                                    &ps_ctxt->au1_rdopt_init_ctxt_models[0],
7815
1.76M
                                    &ps_ctxt->s_rdopt_entropy_ctxt
7816
1.76M
                                         .as_cu_entropy_ctxt[rd_opt_curr_idx]
7817
1.76M
                                         .s_cabac_ctxt.au1_ctxt_models[0],
7818
1.76M
                                    IHEVC_CAB_CTXT_END);
7819
1.76M
                            }
7820
                            /* RDOPT copy States :  Restoring back the Cb init state to Cr */
7821
10.2M
                            else
7822
10.2M
                            {
7823
10.2M
                                COPY_CABAC_STATES(
7824
10.2M
                                    &ps_ctxt->s_rdopt_entropy_ctxt
7825
10.2M
                                         .as_cu_entropy_ctxt[rd_opt_curr_idx]
7826
10.2M
                                         .s_cabac_ctxt.au1_ctxt_models[0],
7827
10.2M
                                    &ps_ctxt->au1_rdopt_init_ctxt_models[0],
7828
10.2M
                                    IHEVC_CAB_CTXT_END);
7829
10.2M
                            }
7830
7831
                            /* If Intra and TU=CU/2, need recon for next TUs */
7832
11.9M
                            if(calc_recon)
7833
1.24M
                            {
7834
1.24M
                                ihevce_chroma_it_recon_fxn(
7835
1.24M
                                    ps_ctxt,
7836
1.24M
                                    (pi2_cur_deq_data + trans_size),
7837
1.24M
                                    deq_data_strd,
7838
1.24M
                                    pu1_cur_pred,
7839
1.24M
                                    pred_strd,
7840
1.24M
                                    pu1_cur_recon,
7841
1.24M
                                    i4_recon_stride,
7842
1.24M
                                    (pu1_ecd_data + total_bytes_offset),
7843
1.24M
                                    trans_size,
7844
1.24M
                                    cbf,
7845
1.24M
                                    zero_cols,
7846
1.24M
                                    zero_rows,
7847
1.24M
                                    V_PLANE);
7848
7849
1.24M
                                ps_recon_datastore
7850
1.24M
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
7851
1.24M
                                                                        [i4_subtu_idx] = 0;
7852
1.24M
                            }
7853
10.7M
                            else
7854
10.7M
                            {
7855
10.7M
                                ps_recon_datastore
7856
10.7M
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
7857
10.7M
                                                                        [i4_subtu_idx] = UCHAR_MAX;
7858
10.7M
                            }
7859
11.9M
                        }
7860
1.04M
                        else
7861
1.04M
                        {
7862
                            /* num bytes is set to 0 */
7863
1.04M
                            num_bytes = 0;
7864
7865
                            /* cbf is returned as 0 */
7866
1.04M
                            cbf = 0;
7867
7868
1.04M
                            curr_cr_cod_cost = trans_ssd_v =
7869
7870
1.04M
                                ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_ssd_calculator(
7871
1.04M
                                    pu1_cur_pred,
7872
1.04M
                                    pu1_cur_src,
7873
1.04M
                                    pred_strd,
7874
1.04M
                                    chrm_src_stride,
7875
1.04M
                                    trans_size,
7876
1.04M
                                    trans_size,
7877
1.04M
                                    V_PLANE);
7878
7879
1.04M
                            if(u1_compute_spatial_ssd)
7880
628k
                            {
7881
                                /* buffer copy fromp pred to recon */
7882
628k
                                ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
7883
628k
                                    pu1_cur_pred,
7884
628k
                                    pred_strd,
7885
628k
                                    pu1_cur_recon,
7886
628k
                                    i4_recon_stride,
7887
628k
                                    trans_size,
7888
628k
                                    trans_size,
7889
628k
                                    V_PLANE);
7890
7891
628k
                                ps_recon_datastore
7892
628k
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
7893
628k
                                                                        [i4_subtu_idx] = 0;
7894
628k
                            }
7895
7896
1.04M
                            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
7897
0
                            {
7898
0
                                trans_ssd_v = ihevce_inject_stim_into_distortion(
7899
0
                                    pu1_cur_src,
7900
0
                                    chrm_src_stride,
7901
0
                                    pu1_cur_pred,
7902
0
                                    pred_strd,
7903
0
                                    trans_ssd_v,
7904
0
                                    i4_alpha_stim_multiplier,
7905
0
                                    trans_size,
7906
0
                                    0,
7907
0
                                    ps_ctxt->u1_enable_psyRDOPT,
7908
0
                                    V_PLANE);
7909
0
                            }
7910
7911
1.04M
#if ENABLE_INTER_ZCU_COST
7912
#if !WEIGH_CHROMA_COST
7913
                            /* cbf = 0, accumulate cu not coded cost */
7914
                            ps_ctxt->i8_cu_not_coded_cost += curr_cr_cod_cost;
7915
#else
7916
                            /* cbf = 0, accumulate cu not coded cost */
7917
7918
1.04M
                            ps_ctxt->i8_cu_not_coded_cost += (LWORD64)(
7919
1.04M
                                (curr_cr_cod_cost * ps_ctxt->u4_chroma_cost_weighing_factor +
7920
1.04M
                                 (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
7921
1.04M
                                CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
7922
1.04M
#endif
7923
1.04M
#endif
7924
1.04M
                        }
7925
7926
#if !WEIGH_CHROMA_COST
7927
                        curr_rdopt_cost += curr_cr_cod_cost;
7928
#else
7929
13.0M
                        curr_rdopt_cost +=
7930
13.0M
                            ((curr_cr_cod_cost * ps_ctxt->u4_chroma_cost_weighing_factor +
7931
13.0M
                              (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
7932
13.0M
                             CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
7933
13.0M
#endif
7934
7935
13.0M
                        chrm_cod_cost += curr_cr_cod_cost;
7936
13.0M
                        i8_ssd_cr += trans_ssd_v;
7937
7938
13.0M
                        if(ps_ctxt->i4_bitrate_instance_num || ps_ctxt->i4_num_bitrates == 1)
7939
13.0M
                        {
7940
                            /* Early exit : If the current running cost exceeds
7941
                            the prev. best mode cost, break */
7942
13.0M
                            if(curr_rdopt_cost > prev_best_rdopt_cost)
7943
531k
                            {
7944
531k
                                u1_is_early_exit_condition_satisfied = 1;
7945
531k
                                break;
7946
531k
                            }
7947
13.0M
                        }
7948
7949
                        /* inter cu is coded if any of the tu is coded in it */
7950
12.4M
                        ps_best_cu_prms->u1_is_cu_coded |= cbf;
7951
7952
                        /* update CR related params */
7953
12.4M
                        ps_tu->ai4_cr_coeff_offset[i4_subtu_idx] =
7954
12.4M
                            total_bytes_offset + init_bytes_offset;
7955
7956
12.4M
                        if(0 == i4_subtu_idx)
7957
12.4M
                        {
7958
12.4M
                            ps_tu->s_tu.b1_cr_cbf = cbf;
7959
12.4M
                        }
7960
0
                        else
7961
0
                        {
7962
0
                            ps_tu->s_tu.b1_cr_cbf_subtu1 = cbf;
7963
0
                        }
7964
7965
12.4M
                        total_bytes_offset += num_bytes;
7966
7967
12.4M
                        ps_tu_temp_prms->au4_cr_zero_col[i4_subtu_idx] = zero_cols;
7968
12.4M
                        ps_tu_temp_prms->au4_cr_zero_row[i4_subtu_idx] = zero_rows;
7969
12.4M
                        ps_tu_temp_prms->ai2_cr_bytes_consumed[i4_subtu_idx] = num_bytes;
7970
12.4M
                    }
7971
991k
                    else
7972
991k
                    {
7973
991k
                        ps_recon_datastore
7974
991k
                            ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr][i4_subtu_idx] =
7975
991k
                            UCHAR_MAX;
7976
991k
                        ps_recon_datastore
7977
991k
                            ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr][i4_subtu_idx] =
7978
991k
                            UCHAR_MAX;
7979
991k
                    }
7980
14.6M
                }
7981
7982
14.6M
                if(u1_is_early_exit_condition_satisfied)
7983
1.19M
                {
7984
1.19M
                    break;
7985
1.19M
                }
7986
7987
                /* loop increments */
7988
13.4M
                ps_tu++;
7989
13.4M
                ps_tu_temp_prms++;
7990
13.4M
            }
7991
7992
            /* Signal as luma mode. HIGH_QUALITY may update it */
7993
9.96M
            ps_best_cu_prms->u1_chroma_intra_pred_mode = 4;
7994
7995
            /* modify the cost chrm_cod_cost */
7996
9.96M
            if(ps_ctxt->u1_enable_psyRDOPT)
7997
0
            {
7998
0
                UWORD8 *pu1_recon_cu;
7999
0
                WORD32 recon_stride;
8000
0
                WORD32 curr_pos_x;
8001
0
                WORD32 curr_pos_y;
8002
0
                WORD32 start_index;
8003
0
                WORD32 num_horz_cu_in_ctb;
8004
0
                WORD32 had_block_size;
8005
                /* tODO: sreenivasa ctb size has to be used appropriately */
8006
0
                had_block_size = 8;
8007
0
                num_horz_cu_in_ctb = 2 * 64 / had_block_size;
8008
8009
0
                curr_pos_x = cu_pos_x << 3; /* pel units */
8010
0
                curr_pos_y = cu_pos_y << 3; /* pel units */
8011
0
                recon_stride = i4_recon_stride;
8012
0
                pu1_recon_cu = pu1_recon;
8013
8014
                /* start index to index the source satd of curr cu int he current ctb*/
8015
0
                start_index = 2 * (curr_pos_x / had_block_size) +
8016
0
                              (curr_pos_y / had_block_size) * num_horz_cu_in_ctb;
8017
8018
0
                {
8019
0
                    chrm_cod_cost += ihevce_psy_rd_cost_croma(
8020
0
                        ps_ctxt->ai4_source_chroma_satd,
8021
0
                        pu1_recon,
8022
0
                        recon_stride,
8023
0
                        1,  //
8024
0
                        cu_size,
8025
0
                        0,  // pic type
8026
0
                        0,  //layer id
8027
0
                        ps_ctxt->i4_satd_lamda,  // lambda
8028
0
                        start_index,
8029
0
                        ps_ctxt->u1_is_input_data_hbd,  // 8 bit
8030
0
                        ps_ctxt->u1_chroma_array_type,
8031
0
                        &ps_ctxt->s_cmn_opt_func
8032
8033
0
                    );  // chroma subsampling 420
8034
0
                }
8035
0
            }
8036
9.96M
        }
8037
2.01M
        else
8038
2.01M
        {
8039
2.01M
            u1_is_mode_eq_chroma_satd_mode = 1;
8040
2.01M
            chrm_cod_cost = MAX_COST_64;
8041
2.01M
        }
8042
8043
        /* If Intra Block and preset is HIGH QUALITY, then compare with best SATD mode */
8044
11.9M
        if((PRED_MODE_INTRA == ps_best_cu_prms->u1_intra_flag) &&
8045
7.71M
           (1 == ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_satd))
8046
6.91M
        {
8047
6.91M
            if(64 == cu_size)
8048
98.9k
            {
8049
98.9k
                ASSERT(TU_EQ_CU != func_proc_mode);
8050
98.9k
            }
8051
8052
6.91M
            if(ps_ctxt->s_chroma_rdopt_ctxt.as_chr_intra_satd_ctxt[func_proc_mode]
8053
6.91M
                   .i8_chroma_best_rdopt < chrm_cod_cost)
8054
2.50M
            {
8055
2.50M
                UWORD8 *pu1_src;
8056
2.50M
                UWORD8 *pu1_ecd_data_src_cb;
8057
2.50M
                UWORD8 *pu1_ecd_data_src_cr;
8058
8059
2.50M
                chroma_intra_satd_ctxt_t *ps_chr_intra_satd_ctxt =
8060
2.50M
                    &ps_ctxt->s_chroma_rdopt_ctxt.as_chr_intra_satd_ctxt[func_proc_mode];
8061
8062
2.50M
                UWORD8 *pu1_dst = &ps_ctxt->au1_rdopt_init_ctxt_models[0];
8063
2.50M
                WORD32 ai4_ecd_data_cb_offset[2] = { 0, 0 };
8064
2.50M
                WORD32 ai4_ecd_data_cr_offset[2] = { 0, 0 };
8065
8066
2.50M
                pu1_src = &ps_chr_intra_satd_ctxt->au1_chrm_satd_updated_ctxt_models[0];
8067
2.50M
                chrm_cod_cost = ps_chr_intra_satd_ctxt->i8_chroma_best_rdopt;
8068
2.50M
                chrm_pred_mode = ps_chr_intra_satd_ctxt->u1_best_cr_mode;
8069
2.50M
                chrm_tu_bits = ps_chr_intra_satd_ctxt->i4_chrm_tu_bits;
8070
8071
2.50M
                if(u1_is_mode_eq_chroma_satd_mode)
8072
2.01M
                {
8073
2.01M
                    chrm_cod_cost -= ps_chr_intra_satd_ctxt->i8_cost_to_encode_chroma_mode;
8074
2.01M
                }
8075
8076
                /*Resetting total_num_bytes_to 0*/
8077
2.50M
                total_bytes_offset = 0;
8078
8079
                /* Update the CABAC state corresponding to chroma only */
8080
                /* Chroma Cbf */
8081
2.50M
                memcpy(pu1_dst + IHEVC_CAB_CBCR_IDX, pu1_src + IHEVC_CAB_CBCR_IDX, 2);
8082
                /* Chroma transform skip */
8083
2.50M
                memcpy(pu1_dst + IHEVC_CAB_TFM_SKIP12, pu1_src + IHEVC_CAB_TFM_SKIP12, 1);
8084
                /* Chroma last coeff x prefix */
8085
2.50M
                memcpy(
8086
2.50M
                    pu1_dst + IHEVC_CAB_COEFFX_PREFIX + 15,
8087
2.50M
                    pu1_src + IHEVC_CAB_COEFFX_PREFIX + 15,
8088
2.50M
                    3);
8089
                /* Chroma last coeff y prefix */
8090
2.50M
                memcpy(
8091
2.50M
                    pu1_dst + IHEVC_CAB_COEFFY_PREFIX + 15,
8092
2.50M
                    pu1_src + IHEVC_CAB_COEFFY_PREFIX + 15,
8093
2.50M
                    3);
8094
                /* Chroma csbf */
8095
2.50M
                memcpy(
8096
2.50M
                    pu1_dst + IHEVC_CAB_CODED_SUBLK_IDX + 2,
8097
2.50M
                    pu1_src + IHEVC_CAB_CODED_SUBLK_IDX + 2,
8098
2.50M
                    2);
8099
                /* Chroma sig coeff flags */
8100
2.50M
                memcpy(
8101
2.50M
                    pu1_dst + IHEVC_CAB_COEFF_FLAG + 27, pu1_src + IHEVC_CAB_COEFF_FLAG + 27, 15);
8102
                /* Chroma absgt1 flags */
8103
2.50M
                memcpy(
8104
2.50M
                    pu1_dst + IHEVC_CAB_COEFABS_GRTR1_FLAG + 16,
8105
2.50M
                    pu1_src + IHEVC_CAB_COEFABS_GRTR1_FLAG + 16,
8106
2.50M
                    8);
8107
                /* Chroma absgt2 flags */
8108
2.50M
                memcpy(
8109
2.50M
                    pu1_dst + IHEVC_CAB_COEFABS_GRTR2_FLAG + 4,
8110
2.50M
                    pu1_src + IHEVC_CAB_COEFABS_GRTR2_FLAG + 4,
8111
2.50M
                    2);
8112
8113
2.50M
                ps_tu = &ps_best_cu_prms->as_tu_enc_loop[0];
8114
2.50M
                ps_tu_temp_prms = &ps_best_cu_prms->as_tu_enc_loop_temp_prms[0];
8115
8116
                /* update to luma decision as we update chroma in final mode */
8117
2.50M
                ps_best_cu_prms->u1_is_cu_coded = u1_is_cu_coded_old;
8118
8119
6.18M
                for(ctr = 0; ctr < u1_num_tus; ctr++)
8120
3.67M
                {
8121
7.34M
                    for(i4_subtu_idx = 0; i4_subtu_idx < u1_num_subtus_in_tu; i4_subtu_idx++)
8122
3.67M
                    {
8123
3.67M
                        WORD32 cbf;
8124
3.67M
                        WORD32 num_bytes;
8125
8126
3.67M
                        pu1_ecd_data_src_cb =
8127
3.67M
                            &ps_chr_intra_satd_ctxt->au1_scan_coeff_cb[i4_subtu_idx][0];
8128
3.67M
                        pu1_ecd_data_src_cr =
8129
3.67M
                            &ps_chr_intra_satd_ctxt->au1_scan_coeff_cr[i4_subtu_idx][0];
8130
8131
                        /* check if chroma present flag is set */
8132
3.67M
                        if(1 == ps_tu->s_tu.b3_chroma_intra_mode_idx)
8133
3.67M
                        {
8134
3.67M
                            UWORD8 *pu1_cur_pred_dest;
8135
3.67M
                            UWORD8 *pu1_cur_pred_src;
8136
3.67M
                            WORD32 pred_src_strd;
8137
3.67M
                            WORD16 *pi2_cur_deq_data_dest;
8138
3.67M
                            WORD16 *pi2_cur_deq_data_src_cb;
8139
3.67M
                            WORD16 *pi2_cur_deq_data_src_cr;
8140
3.67M
                            WORD32 deq_src_strd;
8141
8142
3.67M
                            WORD32 curr_pos_x, curr_pos_y;
8143
8144
3.67M
                            trans_size = ps_tu->s_tu.b3_size;
8145
3.67M
                            trans_size = (1 << (trans_size + 1)); /* in chroma units */
8146
8147
                            /*Deriving stride values*/
8148
3.67M
                            pred_src_strd = ps_chr_intra_satd_ctxt->i4_pred_stride;
8149
3.67M
                            deq_src_strd = ps_chr_intra_satd_ctxt->i4_iq_buff_stride;
8150
8151
                            /* since 2x2 transform is not allowed for chroma*/
8152
3.67M
                            if(2 == trans_size)
8153
277k
                            {
8154
277k
                                trans_size = 4;
8155
277k
                            }
8156
8157
                            /* get the current tu posx and posy w.r.t to cu */
8158
3.67M
                            curr_pos_x = (ps_tu->s_tu.b4_pos_x << 2) - (cu_pos_x << 3);
8159
3.67M
                            curr_pos_y = (ps_tu->s_tu.b4_pos_y << 2) - (cu_pos_y << 3) +
8160
3.67M
                                         (i4_subtu_idx * trans_size);
8161
8162
                            /* 420sp case only vertical height will be half */
8163
3.67M
                            if(0 == u1_is_422)
8164
3.67M
                            {
8165
3.67M
                                curr_pos_y >>= 1;
8166
3.67M
                            }
8167
8168
                            /* increment the pointers to start of current TU  */
8169
3.67M
                            pu1_cur_pred_src =
8170
3.67M
                                ((UWORD8 *)ps_chr_intra_satd_ctxt->pv_pred_data + curr_pos_x);
8171
3.67M
                            pu1_cur_pred_src += (curr_pos_y * pred_src_strd);
8172
3.67M
                            pu1_cur_pred_dest = (pu1_pred + curr_pos_x);
8173
3.67M
                            pu1_cur_pred_dest += (curr_pos_y * pred_strd);
8174
8175
3.67M
                            pi2_cur_deq_data_src_cb =
8176
3.67M
                                &ps_chr_intra_satd_ctxt->ai2_iq_data_cb[0] + (curr_pos_x >> 1);
8177
3.67M
                            pi2_cur_deq_data_src_cr =
8178
3.67M
                                &ps_chr_intra_satd_ctxt->ai2_iq_data_cr[0] + (curr_pos_x >> 1);
8179
3.67M
                            pi2_cur_deq_data_src_cb += (curr_pos_y * deq_src_strd);
8180
3.67M
                            pi2_cur_deq_data_src_cr += (curr_pos_y * deq_src_strd);
8181
3.67M
                            pi2_cur_deq_data_dest = pi2_deq_data + curr_pos_x;
8182
3.67M
                            pi2_cur_deq_data_dest += (curr_pos_y * deq_data_strd);
8183
8184
                            /*Overwriting deq data with that belonging to the winning special mode
8185
                            (luma mode !=  chroma mode)
8186
                            ihevce_copy_2d takes source and dest arguments as UWORD8 *. We have to
8187
                            correspondingly manipulate to copy WORD16 data*/
8188
8189
3.67M
                            ps_ctxt->s_cmn_opt_func.pf_copy_2d(
8190
3.67M
                                (UWORD8 *)pi2_cur_deq_data_dest,
8191
3.67M
                                (deq_data_strd << 1),
8192
3.67M
                                (UWORD8 *)pi2_cur_deq_data_src_cb,
8193
3.67M
                                (deq_src_strd << 1),
8194
3.67M
                                (trans_size << 1),
8195
3.67M
                                trans_size);
8196
8197
3.67M
                            ps_ctxt->s_cmn_opt_func.pf_copy_2d(
8198
3.67M
                                (UWORD8 *)(pi2_cur_deq_data_dest + trans_size),
8199
3.67M
                                (deq_data_strd << 1),
8200
3.67M
                                (UWORD8 *)pi2_cur_deq_data_src_cr,
8201
3.67M
                                (deq_src_strd << 1),
8202
3.67M
                                (trans_size << 1),
8203
3.67M
                                trans_size);
8204
8205
                            /*Overwriting pred data with that belonging to the winning special mode
8206
                            (luma mode !=  chroma mode)*/
8207
8208
3.67M
                            ps_ctxt->s_cmn_opt_func.pf_copy_2d(
8209
3.67M
                                pu1_cur_pred_dest,
8210
3.67M
                                pred_strd,
8211
3.67M
                                pu1_cur_pred_src,
8212
3.67M
                                pred_src_strd,
8213
3.67M
                                (trans_size << 1),
8214
3.67M
                                trans_size);
8215
8216
3.67M
                            num_bytes = ps_chr_intra_satd_ctxt
8217
3.67M
                                            ->ai4_num_bytes_scan_coeff_cb_per_tu[i4_subtu_idx][ctr];
8218
3.67M
                            cbf = ps_chr_intra_satd_ctxt->au1_cbf_cb[i4_subtu_idx][ctr];
8219
                            /* inter cu is coded if any of the tu is coded in it */
8220
3.67M
                            ps_best_cu_prms->u1_is_cu_coded |= cbf;
8221
8222
                            /* update CB related params */
8223
3.67M
                            ps_tu->ai4_cb_coeff_offset[i4_subtu_idx] =
8224
3.67M
                                total_bytes_offset + init_bytes_offset;
8225
8226
3.67M
                            if(0 == i4_subtu_idx)
8227
3.67M
                            {
8228
3.67M
                                ps_tu->s_tu.b1_cb_cbf = cbf;
8229
3.67M
                            }
8230
0
                            else
8231
0
                            {
8232
0
                                ps_tu->s_tu.b1_cb_cbf_subtu1 = cbf;
8233
0
                            }
8234
8235
                            /*Overwriting the cb ecd data corresponding to the special mode*/
8236
3.67M
                            if(0 != num_bytes)
8237
661k
                            {
8238
661k
                                memcpy(
8239
661k
                                    (pu1_ecd_data + total_bytes_offset),
8240
661k
                                    pu1_ecd_data_src_cb + ai4_ecd_data_cb_offset[i4_subtu_idx],
8241
661k
                                    num_bytes);
8242
661k
                            }
8243
8244
3.67M
                            total_bytes_offset += num_bytes;
8245
3.67M
                            ai4_ecd_data_cb_offset[i4_subtu_idx] += num_bytes;
8246
3.67M
                            ps_tu_temp_prms->ai2_cb_bytes_consumed[i4_subtu_idx] = num_bytes;
8247
8248
3.67M
                            num_bytes = ps_chr_intra_satd_ctxt
8249
3.67M
                                            ->ai4_num_bytes_scan_coeff_cr_per_tu[i4_subtu_idx][ctr];
8250
3.67M
                            cbf = ps_chr_intra_satd_ctxt->au1_cbf_cr[i4_subtu_idx][ctr];
8251
                            /* inter cu is coded if any of the tu is coded in it */
8252
3.67M
                            ps_best_cu_prms->u1_is_cu_coded |= cbf;
8253
8254
                            /*Overwriting the cr ecd data corresponding to the special mode*/
8255
3.67M
                            if(0 != num_bytes)
8256
593k
                            {
8257
593k
                                memcpy(
8258
593k
                                    (pu1_ecd_data + total_bytes_offset),
8259
593k
                                    pu1_ecd_data_src_cr + ai4_ecd_data_cr_offset[i4_subtu_idx],
8260
593k
                                    num_bytes);
8261
593k
                            }
8262
8263
                            /* update CR related params */
8264
3.67M
                            ps_tu->ai4_cr_coeff_offset[i4_subtu_idx] =
8265
3.67M
                                total_bytes_offset + init_bytes_offset;
8266
8267
3.67M
                            if(0 == i4_subtu_idx)
8268
3.67M
                            {
8269
3.67M
                                ps_tu->s_tu.b1_cr_cbf = cbf;
8270
3.67M
                            }
8271
0
                            else
8272
0
                            {
8273
0
                                ps_tu->s_tu.b1_cr_cbf_subtu1 = cbf;
8274
0
                            }
8275
8276
3.67M
                            total_bytes_offset += num_bytes;
8277
3.67M
                            ai4_ecd_data_cr_offset[i4_subtu_idx] += num_bytes;
8278
8279
                            /*Updating zero rows and zero cols*/
8280
3.67M
                            ps_tu_temp_prms->au4_cb_zero_col[i4_subtu_idx] =
8281
3.67M
                                ps_chr_intra_satd_ctxt->ai4_zero_col_cb[i4_subtu_idx][ctr];
8282
3.67M
                            ps_tu_temp_prms->au4_cb_zero_row[i4_subtu_idx] =
8283
3.67M
                                ps_chr_intra_satd_ctxt->ai4_zero_row_cb[i4_subtu_idx][ctr];
8284
3.67M
                            ps_tu_temp_prms->au4_cr_zero_col[i4_subtu_idx] =
8285
3.67M
                                ps_chr_intra_satd_ctxt->ai4_zero_col_cr[i4_subtu_idx][ctr];
8286
3.67M
                            ps_tu_temp_prms->au4_cr_zero_row[i4_subtu_idx] =
8287
3.67M
                                ps_chr_intra_satd_ctxt->ai4_zero_row_cr[i4_subtu_idx][ctr];
8288
8289
3.67M
                            ps_tu_temp_prms->ai2_cr_bytes_consumed[i4_subtu_idx] = num_bytes;
8290
8291
3.67M
                            if((u1_num_tus > 1) &&
8292
1.55M
                               ps_recon_datastore->au1_is_chromaRecon_available[2])
8293
1.55M
                            {
8294
1.55M
                                ps_recon_datastore
8295
1.55M
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
8296
1.55M
                                                                        [i4_subtu_idx] = 2;
8297
1.55M
                                ps_recon_datastore
8298
1.55M
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
8299
1.55M
                                                                        [i4_subtu_idx] = 2;
8300
1.55M
                            }
8301
2.11M
                            else if(
8302
2.11M
                                (1 == u1_num_tus) &&
8303
2.11M
                                ps_recon_datastore->au1_is_chromaRecon_available[1])
8304
1.19M
                            {
8305
1.19M
                                ps_recon_datastore
8306
1.19M
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
8307
1.19M
                                                                        [i4_subtu_idx] = 1;
8308
1.19M
                                ps_recon_datastore
8309
1.19M
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
8310
1.19M
                                                                        [i4_subtu_idx] = 1;
8311
1.19M
                            }
8312
923k
                            else
8313
923k
                            {
8314
923k
                                ps_recon_datastore
8315
923k
                                    ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr]
8316
923k
                                                                        [i4_subtu_idx] = UCHAR_MAX;
8317
923k
                                ps_recon_datastore
8318
923k
                                    ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr]
8319
923k
                                                                        [i4_subtu_idx] = UCHAR_MAX;
8320
923k
                            }
8321
3.67M
                        }
8322
3.67M
                    }
8323
8324
                    /* loop increments */
8325
3.67M
                    ps_tu++;
8326
3.67M
                    ps_tu_temp_prms++;
8327
3.67M
                }
8328
2.50M
            }
8329
8330
6.91M
            if(!u1_is_422)
8331
6.91M
            {
8332
6.91M
                if(chrm_pred_mode == luma_pred_mode)
8333
6.42M
                {
8334
6.42M
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 4;
8335
6.42M
                }
8336
491k
                else if(chrm_pred_mode == 0)
8337
91.8k
                {
8338
91.8k
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 0;
8339
91.8k
                }
8340
399k
                else if(chrm_pred_mode == 1)
8341
129k
                {
8342
129k
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 3;
8343
129k
                }
8344
270k
                else if(chrm_pred_mode == 10)
8345
183k
                {
8346
183k
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 2;
8347
183k
                }
8348
87.2k
                else if(chrm_pred_mode == 26)
8349
87.2k
                {
8350
87.2k
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 1;
8351
87.2k
                }
8352
0
                else
8353
0
                {
8354
0
                    ASSERT(0); /*Should not come here*/
8355
0
                }
8356
6.91M
            }
8357
0
            else
8358
0
            {
8359
0
                if(chrm_pred_mode == gau1_chroma422_intra_angle_mapping[luma_pred_mode])
8360
0
                {
8361
0
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 4;
8362
0
                }
8363
0
                else if(chrm_pred_mode == gau1_chroma422_intra_angle_mapping[0])
8364
0
                {
8365
0
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 0;
8366
0
                }
8367
0
                else if(chrm_pred_mode == gau1_chroma422_intra_angle_mapping[1])
8368
0
                {
8369
0
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 3;
8370
0
                }
8371
0
                else if(chrm_pred_mode == gau1_chroma422_intra_angle_mapping[10])
8372
0
                {
8373
0
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 2;
8374
0
                }
8375
0
                else if(chrm_pred_mode == gau1_chroma422_intra_angle_mapping[26])
8376
0
                {
8377
0
                    ps_best_cu_prms->u1_chroma_intra_pred_mode = 1;
8378
0
                }
8379
0
                else
8380
0
                {
8381
0
                    ASSERT(0); /*Should not come here*/
8382
0
                }
8383
0
            }
8384
6.91M
        }
8385
8386
        /* Store the actual chroma mode */
8387
11.9M
        ps_best_cu_prms->u1_chroma_intra_pred_actual_mode = chrm_pred_mode;
8388
11.9M
    }
8389
8390
    /* update the total bytes produced */
8391
11.9M
    ps_best_cu_prms->i4_num_bytes_ecd_data = total_bytes_offset + init_bytes_offset;
8392
8393
    /* store the final chrm bits accumulated */
8394
11.9M
    *pi4_chrm_tu_bits = chrm_tu_bits;
8395
8396
11.9M
    return (chrm_cod_cost);
8397
11.9M
}
8398
8399
/*!
8400
******************************************************************************
8401
* \if Function name : ihevce_final_rdopt_mode_prcs \endif
8402
*
8403
* \brief
8404
*    Final RDOPT mode process function. Performs Recon computation for the
8405
*    final mode. Re-use or Compute pred, iq-data, coeff based on the flags.
8406
*
8407
* \param[in] pv_ctxt : pointer to enc_loop module
8408
* \param[in] ps_prms : pointer to struct containing requisite parameters
8409
*
8410
* \return
8411
*    None
8412
*
8413
* \author
8414
*  Ittiam
8415
*
8416
*****************************************************************************
8417
*/
8418
void ihevce_final_rdopt_mode_prcs(
8419
    ihevce_enc_loop_ctxt_t *ps_ctxt, final_mode_process_prms_t *ps_prms)
8420
2.99M
{
8421
2.99M
    enc_loop_cu_final_prms_t *ps_best_cu_prms;
8422
2.99M
    tu_enc_loop_out_t *ps_tu_enc_loop;
8423
2.99M
    tu_enc_loop_temp_prms_t *ps_tu_enc_loop_temp_prms;
8424
2.99M
    nbr_avail_flags_t s_nbr;
8425
2.99M
    recon_datastore_t *ps_recon_datastore;
8426
8427
2.99M
    ihevc_intra_pred_luma_ref_substitution_ft *ihevc_intra_pred_luma_ref_substitution_fptr;
8428
2.99M
    ihevc_intra_pred_chroma_ref_substitution_ft *ihevc_intra_pred_chroma_ref_substitution_fptr;
8429
2.99M
    ihevc_intra_pred_ref_filtering_ft *ihevc_intra_pred_ref_filtering_fptr;
8430
8431
2.99M
    WORD32 num_tu_in_cu;
8432
2.99M
    LWORD64 rd_opt_cost;
8433
2.99M
    WORD32 ctr;
8434
2.99M
    WORD32 i4_subtu_idx;
8435
2.99M
    WORD32 cu_size;
8436
2.99M
    WORD32 cu_pos_x, cu_pos_y;
8437
2.99M
    WORD32 chrm_present_flag = 1;
8438
2.99M
    WORD32 num_bytes, total_bytes = 0;
8439
2.99M
    WORD32 chrm_ctr = 0;
8440
2.99M
    WORD32 u1_is_cu_coded;
8441
2.99M
    UWORD8 *pu1_old_ecd_data;
8442
2.99M
    UWORD8 *pu1_chrm_old_ecd_data;
8443
2.99M
    UWORD8 *pu1_cur_pred;
8444
2.99M
    WORD16 *pi2_deq_data;
8445
2.99M
    WORD16 *pi2_chrm_deq_data;
8446
2.99M
    WORD16 *pi2_cur_deq_data;
8447
2.99M
    WORD16 *pi2_cur_deq_data_chrm;
8448
2.99M
    UWORD8 *pu1_cur_luma_recon;
8449
2.99M
    UWORD8 *pu1_cur_chroma_recon;
8450
2.99M
    UWORD8 *pu1_cur_src;
8451
2.99M
    UWORD8 *pu1_cur_src_chrm;
8452
2.99M
    UWORD8 *pu1_cur_pred_chrm;
8453
2.99M
    UWORD8 *pu1_intra_pred_mode;
8454
2.99M
    UWORD32 *pu4_nbr_flags;
8455
2.99M
    LWORD64 i8_ssd;
8456
8457
2.99M
    cu_nbr_prms_t *ps_cu_nbr_prms = ps_prms->ps_cu_nbr_prms;
8458
2.99M
    cu_inter_cand_t *ps_best_inter_cand = ps_prms->ps_best_inter_cand;
8459
2.99M
    enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms = ps_prms->ps_chrm_cu_buf_prms;
8460
8461
2.99M
    WORD32 packed_pred_mode = ps_prms->packed_pred_mode;
8462
2.99M
    WORD32 rd_opt_best_idx = ps_prms->rd_opt_best_idx;
8463
2.99M
    UWORD8 *pu1_src = (UWORD8 *)ps_prms->pv_src;
8464
2.99M
    WORD32 src_strd = ps_prms->src_strd;
8465
2.99M
    UWORD8 *pu1_pred = (UWORD8 *)ps_prms->pv_pred;
8466
2.99M
    WORD32 pred_strd = ps_prms->pred_strd;
8467
2.99M
    UWORD8 *pu1_pred_chrm = (UWORD8 *)ps_prms->pv_pred_chrm;
8468
2.99M
    WORD32 pred_chrm_strd = ps_prms->pred_chrm_strd;
8469
2.99M
    UWORD8 *pu1_final_ecd_data = ps_prms->pu1_final_ecd_data;
8470
2.99M
    UWORD8 *pu1_csbf_buf = ps_prms->pu1_csbf_buf;
8471
2.99M
    WORD32 csbf_strd = ps_prms->csbf_strd;
8472
2.99M
    UWORD8 *pu1_luma_recon = (UWORD8 *)ps_prms->pv_luma_recon;
8473
2.99M
    WORD32 recon_luma_strd = ps_prms->recon_luma_strd;
8474
2.99M
    UWORD8 *pu1_chrm_recon = (UWORD8 *)ps_prms->pv_chrm_recon;
8475
2.99M
    WORD32 recon_chrma_strd = ps_prms->recon_chrma_strd;
8476
2.99M
    UWORD8 u1_cu_pos_x = ps_prms->u1_cu_pos_x;
8477
2.99M
    UWORD8 u1_cu_pos_y = ps_prms->u1_cu_pos_y;
8478
2.99M
    UWORD8 u1_cu_size = ps_prms->u1_cu_size;
8479
2.99M
    WORD8 i1_cu_qp = ps_prms->i1_cu_qp;
8480
2.99M
    UWORD8 u1_is_422 = (ps_ctxt->u1_chroma_array_type == 2);
8481
2.99M
    UWORD8 u1_num_subtus = (u1_is_422 == 1) + 1;
8482
    /* Get the Chroma pointer and parameters */
8483
2.99M
    UWORD8 *pu1_src_chrm = ps_chrm_cu_buf_prms->pu1_curr_src;
8484
2.99M
    WORD32 src_chrm_strd = ps_chrm_cu_buf_prms->i4_chrm_src_stride;
8485
2.99M
    UWORD8 u1_compute_spatial_ssd_luma = 0;
8486
2.99M
    UWORD8 u1_compute_spatial_ssd_chroma = 0;
8487
    /* Get the pointer for function selector */
8488
2.99M
    ihevc_intra_pred_luma_ref_substitution_fptr =
8489
2.99M
        ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_ref_substitution_fptr;
8490
8491
2.99M
    ihevc_intra_pred_ref_filtering_fptr =
8492
2.99M
        ps_ctxt->ps_func_selector->ihevc_intra_pred_ref_filtering_fptr;
8493
8494
2.99M
    ihevc_intra_pred_chroma_ref_substitution_fptr =
8495
2.99M
        ps_ctxt->ps_func_selector->ihevc_intra_pred_chroma_ref_substitution_fptr;
8496
8497
    /* Get the best CU parameters */
8498
2.99M
    ps_best_cu_prms = &ps_ctxt->as_cu_prms[rd_opt_best_idx];
8499
2.99M
    num_tu_in_cu = ps_best_cu_prms->u2_num_tus_in_cu;
8500
2.99M
    cu_size = ps_best_cu_prms->u1_cu_size;
8501
2.99M
    cu_pos_x = u1_cu_pos_x;
8502
2.99M
    cu_pos_y = u1_cu_pos_y;
8503
2.99M
    pu1_intra_pred_mode = &ps_best_cu_prms->au1_intra_pred_mode[0];
8504
2.99M
    pu4_nbr_flags = &ps_best_cu_prms->au4_nbr_flags[0];
8505
2.99M
    ps_recon_datastore = &ps_best_cu_prms->s_recon_datastore;
8506
8507
    /* get the first TU pointer */
8508
2.99M
    ps_tu_enc_loop = &ps_best_cu_prms->as_tu_enc_loop[0];
8509
    /* get the first TU only enc_loop prms pointer */
8510
2.99M
    ps_tu_enc_loop_temp_prms = &ps_best_cu_prms->as_tu_enc_loop_temp_prms[0];
8511
    /*modify quant related param in ctxt based on current cu qp*/
8512
2.99M
    if((ps_ctxt->i1_cu_qp_delta_enable))
8513
1.11M
    {
8514
        /*recompute quant related param at every cu level*/
8515
1.11M
        ihevce_compute_quant_rel_param(ps_ctxt, i1_cu_qp);
8516
8517
        /* get frame level lambda params */
8518
1.11M
        ihevce_get_cl_cu_lambda_prms(
8519
1.11M
            ps_ctxt, MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON ? i1_cu_qp : ps_ctxt->i4_frame_qp);
8520
1.11M
    }
8521
8522
2.99M
    ps_best_cu_prms->i8_cu_ssd = 0;
8523
2.99M
    ps_best_cu_prms->u4_cu_open_intra_sad = 0;
8524
8525
    /* For skip case : Set TU_size = CU_size and make cbf = 0
8526
    so that same TU loop can be used for all modes */
8527
2.99M
    if(PRED_MODE_SKIP == packed_pred_mode)
8528
320k
    {
8529
644k
        for(ctr = 0; ctr < num_tu_in_cu; ctr++)
8530
324k
        {
8531
324k
            ps_tu_enc_loop->s_tu.b1_y_cbf = 0;
8532
8533
324k
            ps_tu_enc_loop_temp_prms->i2_luma_bytes_consumed = 0;
8534
8535
324k
            ps_tu_enc_loop++;
8536
324k
            ps_tu_enc_loop_temp_prms++;
8537
324k
        }
8538
8539
        /* go back to the first TU pointer */
8540
320k
        ps_tu_enc_loop = &ps_best_cu_prms->as_tu_enc_loop[0];
8541
320k
        ps_tu_enc_loop_temp_prms = &ps_best_cu_prms->as_tu_enc_loop_temp_prms[0];
8542
320k
    }
8543
    /**   For inter case, pred calculation is outside the loop     **/
8544
2.99M
    if(PRED_MODE_INTRA != packed_pred_mode)
8545
1.50M
    {
8546
        /**------------- Compute pred data if required --------------**/
8547
1.50M
        if((1 == ps_ctxt->s_cu_final_recon_flags.u1_eval_luma_pred_data))
8548
0
        {
8549
0
            nbr_4x4_t *ps_topleft_nbr_4x4;
8550
0
            nbr_4x4_t *ps_left_nbr_4x4;
8551
0
            nbr_4x4_t *ps_top_nbr_4x4;
8552
0
            WORD32 nbr_4x4_left_strd;
8553
8554
0
            ps_best_inter_cand->pu1_pred_data = pu1_pred;
8555
0
            ps_best_inter_cand->i4_pred_data_stride = pred_strd;
8556
8557
            /* Get the CU nbr information */
8558
0
            ps_topleft_nbr_4x4 = ps_cu_nbr_prms->ps_topleft_nbr_4x4;
8559
0
            ps_left_nbr_4x4 = ps_cu_nbr_prms->ps_left_nbr_4x4;
8560
0
            ps_top_nbr_4x4 = ps_cu_nbr_prms->ps_top_nbr_4x4;
8561
0
            nbr_4x4_left_strd = ps_cu_nbr_prms->nbr_4x4_left_strd;
8562
8563
            /* MVP ,MVD calc and Motion compensation */
8564
0
            rd_opt_cost = ((pf_inter_rdopt_cu_mc_mvp)ps_ctxt->pv_inter_rdopt_cu_mc_mvp)(
8565
0
                ps_ctxt,
8566
0
                ps_best_inter_cand,
8567
0
                u1_cu_size,
8568
0
                cu_pos_x,
8569
0
                cu_pos_y,
8570
0
                ps_left_nbr_4x4,
8571
0
                ps_top_nbr_4x4,
8572
0
                ps_topleft_nbr_4x4,
8573
0
                nbr_4x4_left_strd,
8574
0
                rd_opt_best_idx);
8575
0
        }
8576
8577
        /** ------ Motion Compensation for Chroma -------- **/
8578
1.50M
        if(1 == ps_ctxt->s_cu_final_recon_flags.u1_eval_chroma_pred_data)
8579
335k
        {
8580
335k
            UWORD8 *pu1_cur_pred;
8581
335k
            pu1_cur_pred = pu1_pred_chrm;
8582
8583
            /* run a loop over all the partitons in cu */
8584
674k
            for(ctr = 0; ctr < ps_best_cu_prms->u2_num_pus_in_cu; ctr++)
8585
338k
            {
8586
338k
                pu_t *ps_pu;
8587
338k
                WORD32 inter_pu_wd, inter_pu_ht;
8588
8589
338k
                ps_pu = &ps_best_cu_prms->as_pu_chrm_proc[ctr];
8590
8591
                /* IF AMP then each partitions can have diff wd ht */
8592
338k
                inter_pu_wd = (ps_pu->b4_wd + 1) << 2; /* cb and cr pixel interleaved */
8593
338k
                inter_pu_ht = ((ps_pu->b4_ht + 1) << 2) >> 1;
8594
338k
                inter_pu_ht <<= u1_is_422;
8595
                /* chroma mc func */
8596
338k
                ihevce_chroma_inter_pred_pu(
8597
338k
                    &ps_ctxt->s_mc_ctxt, ps_pu, pu1_cur_pred, pred_chrm_strd);
8598
338k
                if(2 == ps_best_cu_prms->u2_num_pus_in_cu)
8599
6.57k
                {
8600
                    /* 2Nx__ partion case */
8601
6.57k
                    if(inter_pu_wd == ps_best_cu_prms->u1_cu_size)
8602
3.92k
                    {
8603
3.92k
                        pu1_cur_pred += (inter_pu_ht * pred_chrm_strd);
8604
3.92k
                    }
8605
                    /* __x2N partion case */
8606
6.57k
                    if(inter_pu_ht == (ps_best_cu_prms->u1_cu_size >> (u1_is_422 == 0)))
8607
2.64k
                    {
8608
2.64k
                        pu1_cur_pred += inter_pu_wd;
8609
2.64k
                    }
8610
6.57k
                }
8611
338k
            }
8612
335k
        }
8613
1.50M
    }
8614
2.99M
    pi2_deq_data = &ps_best_cu_prms->pi2_cu_deq_coeffs[0];
8615
2.99M
    pi2_chrm_deq_data =
8616
2.99M
        &ps_best_cu_prms->pi2_cu_deq_coeffs[0] + ps_best_cu_prms->i4_chrm_deq_coeff_strt_idx;
8617
2.99M
    pu1_old_ecd_data = &ps_best_cu_prms->pu1_cu_coeffs[0];
8618
2.99M
    pu1_chrm_old_ecd_data =
8619
2.99M
        &ps_best_cu_prms->pu1_cu_coeffs[0] + ps_best_cu_prms->i4_chrm_cu_coeff_strt_idx;
8620
8621
    /* default value for cu coded flag */
8622
2.99M
    u1_is_cu_coded = 0;
8623
8624
    /* If we are re-computing coeff, set sad to 0 and start accumulating */
8625
    /* else use the best cand. sad from RDOPT stage                    */
8626
2.99M
    if(1 == ps_tu_enc_loop_temp_prms->b1_eval_luma_iq_and_coeff_data)
8627
0
    {
8628
        /*init of ssd of CU accuumulated over all TU*/
8629
0
        ps_best_cu_prms->u4_cu_sad = 0;
8630
8631
        /* reset the luma residual bits */
8632
0
        ps_best_cu_prms->u4_cu_luma_res_bits = 0;
8633
0
    }
8634
8635
2.99M
    if(1 == ps_tu_enc_loop_temp_prms->b1_eval_chroma_iq_and_coeff_data)
8636
720k
    {
8637
        /* reset the chroma residual bits */
8638
720k
        ps_best_cu_prms->u4_cu_chroma_res_bits = 0;
8639
720k
    }
8640
8641
2.99M
    if((1 == ps_tu_enc_loop_temp_prms->b1_eval_luma_iq_and_coeff_data) ||
8642
2.99M
       (1 == ps_tu_enc_loop_temp_prms->b1_eval_chroma_iq_and_coeff_data))
8643
720k
    {
8644
        /*Header bits have to be reevaluated if luma and chroma reevaluation is done, as
8645
        the quantized coefficients might be changed.
8646
        We are copying only those states which correspond to the header from the cabac state
8647
        of the previous CU, because the header is going to be recomputed for this condition*/
8648
720k
        ps_ctxt->s_cu_final_recon_flags.u1_eval_header_data = 1;
8649
720k
        memcpy(
8650
720k
            &ps_ctxt->au1_rdopt_init_ctxt_models[0],
8651
720k
            &ps_ctxt->s_rdopt_entropy_ctxt.au1_init_cabac_ctxt_states[0],
8652
720k
            IHEVC_CAB_COEFFX_PREFIX);
8653
8654
720k
        if((1 == ps_tu_enc_loop_temp_prms->b1_eval_luma_iq_and_coeff_data))
8655
0
        {
8656
0
            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
8657
0
                (&ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX),
8658
0
                (&ps_ctxt->s_rdopt_entropy_ctxt.au1_init_cabac_ctxt_states[0] +
8659
0
                 IHEVC_CAB_COEFFX_PREFIX),
8660
0
                (IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX));
8661
0
        }
8662
720k
        else
8663
720k
        {
8664
720k
            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
8665
720k
                (&ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX),
8666
720k
                (&ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
8667
720k
                      .s_cabac_ctxt.au1_ctxt_models[0] +
8668
720k
                 IHEVC_CAB_COEFFX_PREFIX),
8669
720k
                (IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX));
8670
720k
        }
8671
720k
        ps_ctxt->s_rdopt_entropy_ctxt.i4_curr_buf_idx = rd_opt_best_idx;
8672
720k
    }
8673
2.27M
    else
8674
2.27M
    {
8675
2.27M
        ps_ctxt->s_cu_final_recon_flags.u1_eval_header_data = 0;
8676
2.27M
    }
8677
8678
    /* Zero cbf tool is disabled for intra CUs */
8679
2.99M
    if(PRED_MODE_INTRA == packed_pred_mode)
8680
1.48M
    {
8681
#if ENABLE_ZERO_CBF_IN_INTRA
8682
        ps_ctxt->i4_zcbf_rdo_level = ZCBF_ENABLE;
8683
#else
8684
1.48M
        ps_ctxt->i4_zcbf_rdo_level = NO_ZCBF;
8685
1.48M
#endif
8686
1.48M
    }
8687
1.50M
    else
8688
1.50M
    {
8689
#if DISABLE_ZERO_ZBF_IN_INTER
8690
        ps_ctxt->i4_zcbf_rdo_level = NO_ZCBF;
8691
#else
8692
1.50M
        ps_ctxt->i4_zcbf_rdo_level = ZCBF_ENABLE;
8693
1.50M
#endif
8694
1.50M
    }
8695
8696
    /** Loop for all tu blocks in current cu and do reconstruction **/
8697
8.39M
    for(ctr = 0; ctr < num_tu_in_cu; ctr++)
8698
5.40M
    {
8699
5.40M
        tu_t *ps_tu;
8700
5.40M
        WORD32 trans_size, num_4x4_in_tu;
8701
5.40M
        WORD32 cbf, zero_rows, zero_cols;
8702
5.40M
        WORD32 cu_pos_x_in_4x4, cu_pos_y_in_4x4;
8703
5.40M
        WORD32 cu_pos_x_in_pix, cu_pos_y_in_pix;
8704
5.40M
        WORD32 luma_pred_mode, chroma_pred_mode = 0;
8705
5.40M
        UWORD8 au1_is_recon_available[2];
8706
8707
5.40M
        ps_tu = &(ps_tu_enc_loop->s_tu); /* Points to the TU property ctxt */
8708
8709
5.40M
        u1_compute_spatial_ssd_luma = 0;
8710
5.40M
        u1_compute_spatial_ssd_chroma = 0;
8711
8712
5.40M
        trans_size = 1 << (ps_tu->b3_size + 2);
8713
5.40M
        num_4x4_in_tu = (trans_size >> 2);
8714
5.40M
        cu_pos_x_in_4x4 = ps_tu->b4_pos_x;
8715
5.40M
        cu_pos_y_in_4x4 = ps_tu->b4_pos_y;
8716
8717
        /* populate the coeffs scan idx */
8718
5.40M
        ps_ctxt->i4_scan_idx = SCAN_DIAG_UPRIGHT;
8719
8720
        /* get the current pos x and pos y in pixels */
8721
5.40M
        cu_pos_x_in_pix = (cu_pos_x_in_4x4 << 2) - (cu_pos_x << 3);
8722
5.40M
        cu_pos_y_in_pix = (cu_pos_y_in_4x4 << 2) - (cu_pos_y << 3);
8723
8724
        /* Update pointers based on the location */
8725
5.40M
        pu1_cur_src = pu1_src + cu_pos_x_in_pix;
8726
5.40M
        pu1_cur_src += (cu_pos_y_in_pix * src_strd);
8727
5.40M
        pu1_cur_pred = pu1_pred + cu_pos_x_in_pix;
8728
5.40M
        pu1_cur_pred += (cu_pos_y_in_pix * pred_strd);
8729
8730
5.40M
        pu1_cur_luma_recon = pu1_luma_recon + cu_pos_x_in_pix;
8731
5.40M
        pu1_cur_luma_recon += (cu_pos_y_in_pix * recon_luma_strd);
8732
8733
5.40M
        pi2_cur_deq_data = pi2_deq_data + cu_pos_x_in_pix;
8734
5.40M
        pi2_cur_deq_data += cu_pos_y_in_pix * cu_size;
8735
8736
5.40M
        pu1_cur_src_chrm = pu1_src_chrm + cu_pos_x_in_pix;
8737
5.40M
        pu1_cur_src_chrm += ((cu_pos_y_in_pix >> 1) * src_chrm_strd) +
8738
5.40M
                            (u1_is_422 * ((cu_pos_y_in_pix >> 1) * src_chrm_strd));
8739
8740
5.40M
        pu1_cur_pred_chrm = pu1_pred_chrm + cu_pos_x_in_pix;
8741
5.40M
        pu1_cur_pred_chrm += ((cu_pos_y_in_pix >> 1) * pred_chrm_strd) +
8742
5.40M
                             (u1_is_422 * ((cu_pos_y_in_pix >> 1) * pred_chrm_strd));
8743
8744
5.40M
        pu1_cur_chroma_recon = pu1_chrm_recon + cu_pos_x_in_pix;
8745
5.40M
        pu1_cur_chroma_recon += ((cu_pos_y_in_pix >> 1) * recon_chrma_strd) +
8746
5.40M
                                (u1_is_422 * ((cu_pos_y_in_pix >> 1) * recon_chrma_strd));
8747
8748
5.40M
        pi2_cur_deq_data_chrm = pi2_chrm_deq_data + cu_pos_x_in_pix;
8749
5.40M
        pi2_cur_deq_data_chrm +=
8750
5.40M
            ((cu_pos_y_in_pix >> 1) * cu_size) + (u1_is_422 * ((cu_pos_y_in_pix >> 1) * cu_size));
8751
8752
        /* if transfrom size is 4x4 then only first luma 4x4 will have chroma*/
8753
5.40M
        chrm_present_flag = 1; /* by default chroma present is set to 1*/
8754
8755
5.40M
        if(4 == trans_size)
8756
1.93M
        {
8757
            /* if tusize is 4x4 then only first luma 4x4 will have chroma*/
8758
1.93M
            if(0 != chrm_ctr)
8759
1.45M
            {
8760
1.45M
                chrm_present_flag = INTRA_PRED_CHROMA_IDX_NONE;
8761
1.45M
            }
8762
8763
            /* increment the chrm ctr unconditionally */
8764
1.93M
            chrm_ctr++;
8765
            /* after ctr reached 4 reset it */
8766
1.93M
            if(4 == chrm_ctr)
8767
484k
            {
8768
484k
                chrm_ctr = 0;
8769
484k
            }
8770
1.93M
        }
8771
8772
        /**------------- Compute pred data if required --------------**/
8773
5.40M
        if(PRED_MODE_INTRA == packed_pred_mode) /* Inter pred calc. is done outside loop */
8774
2.79M
        {
8775
            /* Get the pred mode for scan idx calculation, even if pred is not required */
8776
2.79M
            luma_pred_mode = *pu1_intra_pred_mode;
8777
8778
2.79M
            if((ps_ctxt->i4_rc_pass == 1) ||
8779
2.79M
               (1 == ps_ctxt->s_cu_final_recon_flags.u1_eval_luma_pred_data))
8780
0
            {
8781
0
                WORD32 nbr_flags;
8782
0
                WORD32 luma_pred_func_idx;
8783
0
                UWORD8 *pu1_left;
8784
0
                UWORD8 *pu1_top;
8785
0
                UWORD8 *pu1_top_left;
8786
0
                WORD32 left_strd;
8787
8788
                /* left cu boundary */
8789
0
                if(0 == cu_pos_x_in_pix)
8790
0
                {
8791
0
                    left_strd = ps_cu_nbr_prms->cu_left_stride;
8792
0
                    pu1_left = ps_cu_nbr_prms->pu1_cu_left + cu_pos_y_in_pix * left_strd;
8793
0
                }
8794
0
                else
8795
0
                {
8796
0
                    pu1_left = pu1_cur_luma_recon - 1;
8797
0
                    left_strd = recon_luma_strd;
8798
0
                }
8799
8800
                /* top cu boundary */
8801
0
                if(0 == cu_pos_y_in_pix)
8802
0
                {
8803
0
                    pu1_top = ps_cu_nbr_prms->pu1_cu_top + cu_pos_x_in_pix;
8804
0
                }
8805
0
                else
8806
0
                {
8807
0
                    pu1_top = pu1_cur_luma_recon - recon_luma_strd;
8808
0
                }
8809
8810
                /* by default top left is set to cu top left */
8811
0
                pu1_top_left = ps_cu_nbr_prms->pu1_cu_top_left;
8812
8813
                /* top left based on position */
8814
0
                if((0 != cu_pos_y_in_pix) && (0 == cu_pos_x_in_pix))
8815
0
                {
8816
0
                    pu1_top_left = pu1_left - left_strd;
8817
0
                }
8818
0
                else if(0 != cu_pos_x_in_pix)
8819
0
                {
8820
0
                    pu1_top_left = pu1_top - 1;
8821
0
                }
8822
8823
                /* get the neighbour availability flags */
8824
0
                nbr_flags = ihevce_get_nbr_intra(
8825
0
                    &s_nbr,
8826
0
                    ps_ctxt->pu1_ctb_nbr_map,
8827
0
                    ps_ctxt->i4_nbr_map_strd,
8828
0
                    cu_pos_x_in_4x4,
8829
0
                    cu_pos_y_in_4x4,
8830
0
                    num_4x4_in_tu);
8831
8832
0
                if(1 == ps_ctxt->s_cu_final_recon_flags.u1_eval_luma_pred_data)
8833
0
                {
8834
                    /* copy the nbr flags for chroma reuse */
8835
0
                    if(4 != trans_size)
8836
0
                    {
8837
0
                        *pu4_nbr_flags = nbr_flags;
8838
0
                    }
8839
0
                    else if(1 == chrm_present_flag)
8840
0
                    {
8841
                        /* compute the avail flags assuming luma trans is 8x8 */
8842
                        /* get the neighbour availability flags */
8843
0
                        *pu4_nbr_flags = ihevce_get_nbr_intra_mxn_tu(
8844
0
                            ps_ctxt->pu1_ctb_nbr_map,
8845
0
                            ps_ctxt->i4_nbr_map_strd,
8846
0
                            cu_pos_x_in_4x4,
8847
0
                            cu_pos_y_in_4x4,
8848
0
                            (num_4x4_in_tu << 1),
8849
0
                            (num_4x4_in_tu << 1));
8850
0
                    }
8851
8852
                    /* call reference array substitution */
8853
0
                    ihevc_intra_pred_luma_ref_substitution_fptr(
8854
0
                        pu1_top_left,
8855
0
                        pu1_top,
8856
0
                        pu1_left,
8857
0
                        left_strd,
8858
0
                        trans_size,
8859
0
                        nbr_flags,
8860
0
                        (UWORD8 *)ps_ctxt->pv_ref_sub_out,
8861
0
                        1);
8862
8863
                    /* call reference filtering */
8864
0
                    ihevc_intra_pred_ref_filtering_fptr(
8865
0
                        (UWORD8 *)ps_ctxt->pv_ref_sub_out,
8866
0
                        trans_size,
8867
0
                        (UWORD8 *)ps_ctxt->pv_ref_filt_out,
8868
0
                        luma_pred_mode,
8869
0
                        ps_ctxt->i1_strong_intra_smoothing_enable_flag);
8870
8871
                    /* use the look up to get the function idx */
8872
0
                    luma_pred_func_idx = g_i4_ip_funcs[luma_pred_mode];
8873
8874
                    /* call the intra prediction function */
8875
0
                    ps_ctxt->apf_lum_ip[luma_pred_func_idx](
8876
0
                        (UWORD8 *)ps_ctxt->pv_ref_filt_out,
8877
0
                        1,
8878
0
                        pu1_cur_pred,
8879
0
                        pred_strd,
8880
0
                        trans_size,
8881
0
                        luma_pred_mode);
8882
0
                }
8883
0
            }
8884
2.79M
            else if(
8885
2.79M
                (1 == chrm_present_flag) &&
8886
1.81M
                (1 == ps_ctxt->s_cu_final_recon_flags.u1_eval_chroma_pred_data))
8887
495k
            {
8888
495k
                WORD32 temp_num_4x4_in_tu = num_4x4_in_tu;
8889
8890
495k
                if(4 == trans_size) /* compute the avail flags assuming luma trans is 8x8 */
8891
98.9k
                {
8892
98.9k
                    temp_num_4x4_in_tu = num_4x4_in_tu << 1;
8893
98.9k
                }
8894
8895
495k
                *pu4_nbr_flags = ihevce_get_nbr_intra_mxn_tu(
8896
495k
                    ps_ctxt->pu1_ctb_nbr_map,
8897
495k
                    ps_ctxt->i4_nbr_map_strd,
8898
495k
                    cu_pos_x_in_4x4,
8899
495k
                    cu_pos_y_in_4x4,
8900
495k
                    temp_num_4x4_in_tu,
8901
495k
                    temp_num_4x4_in_tu);
8902
495k
            }
8903
8904
            /* Get the pred mode for scan idx calculation, even if pred is not required */
8905
2.79M
            chroma_pred_mode = ps_best_cu_prms->u1_chroma_intra_pred_actual_mode;
8906
2.79M
        }
8907
8908
5.40M
        if(1 == ps_tu_enc_loop_temp_prms->b1_eval_luma_iq_and_coeff_data)
8909
0
        {
8910
0
            WORD32 temp_bits;
8911
0
            LWORD64 temp_cost;
8912
0
            UWORD32 u4_tu_sad;
8913
0
            WORD32 perform_sbh, perform_rdoq;
8914
8915
0
            if(PRED_MODE_INTRA == packed_pred_mode)
8916
0
            {
8917
                /* for luma 4x4 and 8x8 transforms based on intra pred mode scan is choosen*/
8918
0
                if(trans_size < 16)
8919
0
                {
8920
                    /* for modes from 22 upto 30 horizontal scan is used */
8921
0
                    if((luma_pred_mode > 21) && (luma_pred_mode < 31))
8922
0
                    {
8923
0
                        ps_ctxt->i4_scan_idx = SCAN_HORZ;
8924
0
                    }
8925
                    /* for modes from 6 upto 14 horizontal scan is used */
8926
0
                    else if((luma_pred_mode > 5) && (luma_pred_mode < 15))
8927
0
                    {
8928
0
                        ps_ctxt->i4_scan_idx = SCAN_VERT;
8929
0
                    }
8930
0
                }
8931
0
            }
8932
8933
            /* RDOPT copy States :  TU init (best until prev TU) to current */
8934
0
            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
8935
0
                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
8936
0
                        .s_cabac_ctxt.au1_ctxt_models[0] +
8937
0
                    IHEVC_CAB_COEFFX_PREFIX,
8938
0
                &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
8939
0
                IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
8940
8941
0
            if(ps_prms->u1_recompute_sbh_and_rdoq)
8942
0
            {
8943
0
                perform_sbh = (ps_ctxt->i4_sbh_level != NO_SBH);
8944
0
                perform_rdoq = (ps_ctxt->i4_rdoq_level != NO_RDOQ);
8945
0
            }
8946
0
            else
8947
0
            {
8948
                /* RDOQ will change the coefficients. If coefficients are changed, we will have to do sbh again*/
8949
0
                perform_sbh = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_best_cand_sbh;
8950
                /* To do SBH we need the quant and iquant data. This would mean we need to do quantization again, which would mean
8951
                we would have to do RDOQ again.*/
8952
0
                perform_rdoq = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_best_cand_rdoq;
8953
0
            }
8954
8955
#if DISABLE_RDOQ_INTRA
8956
            if(PRED_MODE_INTRA == packed_pred_mode)
8957
            {
8958
                perform_rdoq = 0;
8959
            }
8960
#endif
8961
            /*If BEST candidate RDOQ is enabled, Eithe no coef level rdoq or CU level rdoq has to be enabled
8962
            so that all candidates and best candidate are quantized with same rounding factor  */
8963
0
            if(1 == perform_rdoq)
8964
0
            {
8965
0
                ASSERT(ps_ctxt->i4_quant_rounding_level != TU_LEVEL_QUANT_ROUNDING);
8966
0
            }
8967
8968
0
            cbf = ihevce_t_q_iq_ssd_scan_fxn(
8969
0
                ps_ctxt,
8970
0
                pu1_cur_pred,
8971
0
                pred_strd,
8972
0
                pu1_cur_src,
8973
0
                src_strd,
8974
0
                pi2_cur_deq_data,
8975
0
                cu_size, /*deq_data stride is cu_size*/
8976
0
                pu1_cur_luma_recon,
8977
0
                recon_luma_strd,
8978
0
                pu1_final_ecd_data,
8979
0
                pu1_csbf_buf,
8980
0
                csbf_strd,
8981
0
                trans_size,
8982
0
                packed_pred_mode,
8983
0
                &temp_cost,
8984
0
                &num_bytes,
8985
0
                &temp_bits,
8986
0
                &u4_tu_sad,
8987
0
                &zero_cols,
8988
0
                &zero_rows,
8989
0
                &au1_is_recon_available[0],
8990
0
                perform_rdoq,  //(BEST_CAND_RDOQ == ps_ctxt->i4_rdoq_level),
8991
0
                perform_sbh,
8992
0
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
8993
0
                !ps_ctxt->u1_is_refPic ? ALPHA_FOR_NOISE_TERM_IN_RDOPT
8994
0
                                       : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
8995
0
                                          (double)ALPHA_FOR_NOISE_TERM_IN_RDOPT) /
8996
0
                                             100.0,
8997
0
                ps_prms->u1_is_cu_noisy,
8998
0
#endif
8999
0
                u1_compute_spatial_ssd_luma ? SPATIAL_DOMAIN_SSD : FREQUENCY_DOMAIN_SSD,
9000
0
                1 /*early cbf*/
9001
0
            );  //(BEST_CAND_SBH == ps_ctxt->i4_sbh_level));
9002
9003
            /* Accumulate luma residual bits */
9004
0
            ps_best_cu_prms->u4_cu_luma_res_bits += temp_bits;
9005
9006
            /* RDOPT copy States :  New updated after curr TU to TU init */
9007
0
            if(0 != cbf)
9008
0
            {
9009
                /* update to new state only if CBF is non zero */
9010
0
                COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
9011
0
                    &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
9012
0
                    &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
9013
0
                            .s_cabac_ctxt.au1_ctxt_models[0] +
9014
0
                        IHEVC_CAB_COEFFX_PREFIX,
9015
0
                    IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
9016
0
            }
9017
9018
            /* accumulate the TU sad into cu sad */
9019
0
            ps_best_cu_prms->u4_cu_sad += u4_tu_sad;
9020
0
            ps_tu->b1_y_cbf = cbf;
9021
0
            ps_tu_enc_loop_temp_prms->i2_luma_bytes_consumed = num_bytes;
9022
9023
            /* If somebody updates cbf (RDOQ or SBH), update in nbr str. for BS */
9024
0
            if((ps_prms->u1_will_cabac_state_change) && (!ps_prms->u1_is_first_pass))
9025
0
            {
9026
0
                WORD32 num_4x4_in_cu = u1_cu_size >> 2;
9027
0
                nbr_4x4_t *ps_cur_nbr_4x4 = &ps_ctxt->as_cu_nbr[rd_opt_best_idx][0];
9028
0
                ps_cur_nbr_4x4 = (ps_cur_nbr_4x4 + (cu_pos_x_in_pix >> 2));
9029
0
                ps_cur_nbr_4x4 += ((cu_pos_y_in_pix >> 2) * num_4x4_in_cu);
9030
                /* repiclate the nbr 4x4 structure for all 4x4 blocks current TU */
9031
0
                ps_cur_nbr_4x4->b1_y_cbf = cbf;
9032
                /*copy the cu qp. This will be overwritten by qp calculated based on skip flag at final stage of cu mode decide*/
9033
0
                ps_cur_nbr_4x4->b8_qp = ps_ctxt->i4_cu_qp;
9034
                /* Qp and cbf are stored for the all 4x4 in TU */
9035
0
                {
9036
0
                    WORD32 i, j;
9037
0
                    nbr_4x4_t *ps_tmp_4x4;
9038
0
                    ps_tmp_4x4 = ps_cur_nbr_4x4;
9039
9040
0
                    for(i = 0; i < num_4x4_in_tu; i++)
9041
0
                    {
9042
0
                        for(j = 0; j < num_4x4_in_tu; j++)
9043
0
                        {
9044
0
                            ps_tmp_4x4[j].b8_qp = ps_ctxt->i4_cu_qp;
9045
0
                            ps_tmp_4x4[j].b1_y_cbf = cbf;
9046
0
                        }
9047
                        /* row level update*/
9048
0
                        ps_tmp_4x4 += num_4x4_in_cu;
9049
0
                    }
9050
0
                }
9051
0
            }
9052
0
        }
9053
5.40M
        else
9054
5.40M
        {
9055
5.40M
            zero_cols = ps_tu_enc_loop_temp_prms->u4_luma_zero_col;
9056
5.40M
            zero_rows = ps_tu_enc_loop_temp_prms->u4_luma_zero_row;
9057
9058
5.40M
            if(ps_prms->u1_will_cabac_state_change)
9059
5.40M
            {
9060
5.40M
                num_bytes = ps_tu_enc_loop_temp_prms->i2_luma_bytes_consumed;
9061
5.40M
            }
9062
0
            else
9063
0
            {
9064
0
                num_bytes = 0;
9065
0
            }
9066
9067
            /* copy luma ecd data to final buffer */
9068
5.40M
            memcpy(pu1_final_ecd_data, pu1_old_ecd_data, num_bytes);
9069
9070
5.40M
            pu1_old_ecd_data += num_bytes;
9071
9072
5.40M
            au1_is_recon_available[0] = 0;
9073
5.40M
        }
9074
9075
        /**-------- Compute Recon data (Do IT & Recon) : Luma  -----------**/
9076
5.40M
        if(ps_ctxt->s_cu_final_recon_flags.u1_eval_recon_data &&
9077
5.33M
           (!u1_compute_spatial_ssd_luma ||
9078
0
            (!au1_is_recon_available[0] && u1_compute_spatial_ssd_luma)))
9079
5.33M
        {
9080
5.33M
            if(!ps_recon_datastore->u1_is_lumaRecon_available ||
9081
3.24M
               (ps_recon_datastore->u1_is_lumaRecon_available &&
9082
3.24M
                (UCHAR_MAX == ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr])))
9083
2.43M
            {
9084
2.43M
                ihevce_it_recon_fxn(
9085
2.43M
                    ps_ctxt,
9086
2.43M
                    pi2_cur_deq_data,
9087
2.43M
                    cu_size,
9088
2.43M
                    pu1_cur_pred,
9089
2.43M
                    pred_strd,
9090
2.43M
                    pu1_cur_luma_recon,
9091
2.43M
                    recon_luma_strd,
9092
2.43M
                    pu1_final_ecd_data,
9093
2.43M
                    trans_size,
9094
2.43M
                    packed_pred_mode,
9095
2.43M
                    ps_tu->b1_y_cbf,
9096
2.43M
                    zero_cols,
9097
2.43M
                    zero_rows);
9098
2.43M
            }
9099
2.89M
            else if(
9100
2.89M
                ps_recon_datastore->u1_is_lumaRecon_available &&
9101
2.89M
                (UCHAR_MAX != ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr]))
9102
2.89M
            {
9103
2.89M
                UWORD8 *pu1_recon_src =
9104
2.89M
                    ((UWORD8 *)ps_recon_datastore->apv_luma_recon_bufs
9105
2.89M
                         [ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr]]) +
9106
2.89M
                    cu_pos_x_in_pix + cu_pos_y_in_pix * ps_recon_datastore->i4_lumaRecon_stride;
9107
9108
2.89M
                ps_ctxt->s_cmn_opt_func.pf_copy_2d(
9109
2.89M
                    pu1_cur_luma_recon,
9110
2.89M
                    recon_luma_strd,
9111
2.89M
                    pu1_recon_src,
9112
2.89M
                    ps_recon_datastore->i4_lumaRecon_stride,
9113
2.89M
                    trans_size,
9114
2.89M
                    trans_size);
9115
2.89M
            }
9116
5.33M
        }
9117
9118
5.40M
        if(ps_prms->u1_will_cabac_state_change)
9119
5.40M
        {
9120
5.40M
            ps_tu_enc_loop->i4_luma_coeff_offset = total_bytes;
9121
5.40M
        }
9122
9123
5.40M
        pu1_final_ecd_data += num_bytes;
9124
        /* update total bytes consumed */
9125
5.40M
        total_bytes += num_bytes;
9126
9127
5.40M
        u1_is_cu_coded |= ps_tu->b1_y_cbf;
9128
9129
        /***************** Compute T,Q,IQ,IT & Recon for Chroma ********************/
9130
5.40M
        if(1 == chrm_present_flag)
9131
3.95M
        {
9132
3.95M
            pu1_cur_src_chrm = pu1_src_chrm + cu_pos_x_in_pix;
9133
3.95M
            pu1_cur_src_chrm += ((cu_pos_y_in_pix >> 1) * src_chrm_strd) +
9134
3.95M
                                (u1_is_422 * ((cu_pos_y_in_pix >> 1) * src_chrm_strd));
9135
9136
3.95M
            pu1_cur_pred_chrm = pu1_pred_chrm + cu_pos_x_in_pix;
9137
3.95M
            pu1_cur_pred_chrm += ((cu_pos_y_in_pix >> 1) * pred_chrm_strd) +
9138
3.95M
                                 (u1_is_422 * ((cu_pos_y_in_pix >> 1) * pred_chrm_strd));
9139
9140
3.95M
            pu1_cur_chroma_recon = pu1_chrm_recon + cu_pos_x_in_pix;
9141
3.95M
            pu1_cur_chroma_recon += ((cu_pos_y_in_pix >> 1) * recon_chrma_strd) +
9142
3.95M
                                    (u1_is_422 * ((cu_pos_y_in_pix >> 1) * recon_chrma_strd));
9143
9144
3.95M
            pi2_cur_deq_data_chrm = pi2_chrm_deq_data + cu_pos_x_in_pix;
9145
3.95M
            pi2_cur_deq_data_chrm += ((cu_pos_y_in_pix >> 1) * cu_size) +
9146
3.95M
                                     (u1_is_422 * ((cu_pos_y_in_pix >> 1) * cu_size));
9147
9148
3.95M
            if(INCLUDE_CHROMA_DURING_TU_RECURSION &&
9149
0
               (ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P0) &&
9150
0
               (PRED_MODE_INTRA != packed_pred_mode))
9151
0
            {
9152
0
                WORD32 i4_num_bytes;
9153
0
                UWORD8 *pu1_chroma_pred;
9154
0
                UWORD8 *pu1_chroma_recon;
9155
0
                WORD16 *pi2_chroma_deq;
9156
0
                UWORD32 u4_zero_col;
9157
0
                UWORD32 u4_zero_row;
9158
9159
0
                for(i4_subtu_idx = 0; i4_subtu_idx < u1_num_subtus; i4_subtu_idx++)
9160
0
                {
9161
0
                    WORD32 chroma_trans_size = MAX(4, trans_size >> 1);
9162
0
                    WORD32 i4_subtu_pos_x = cu_pos_x_in_pix;
9163
0
                    WORD32 i4_subtu_pos_y = cu_pos_y_in_pix + (i4_subtu_idx * chroma_trans_size);
9164
9165
0
                    if(0 == u1_is_422)
9166
0
                    {
9167
0
                        i4_subtu_pos_y >>= 1;
9168
0
                    }
9169
9170
0
                    pu1_chroma_pred =
9171
0
                        pu1_cur_pred_chrm + (i4_subtu_idx * chroma_trans_size * pred_chrm_strd);
9172
0
                    pu1_chroma_recon = pu1_cur_chroma_recon +
9173
0
                                       (i4_subtu_idx * chroma_trans_size * recon_chrma_strd);
9174
0
                    pi2_chroma_deq =
9175
0
                        pi2_cur_deq_data_chrm + (i4_subtu_idx * chroma_trans_size * cu_size);
9176
9177
0
                    u4_zero_col = ps_tu_enc_loop_temp_prms->au4_cb_zero_col[i4_subtu_idx];
9178
0
                    u4_zero_row = ps_tu_enc_loop_temp_prms->au4_cb_zero_row[i4_subtu_idx];
9179
9180
0
                    if(ps_prms->u1_will_cabac_state_change)
9181
0
                    {
9182
0
                        i4_num_bytes =
9183
0
                            ps_tu_enc_loop_temp_prms->ai2_cb_bytes_consumed[i4_subtu_idx];
9184
0
                    }
9185
0
                    else
9186
0
                    {
9187
0
                        i4_num_bytes = 0;
9188
0
                    }
9189
9190
0
                    memcpy(pu1_final_ecd_data, pu1_old_ecd_data, i4_num_bytes);
9191
9192
0
                    pu1_old_ecd_data += i4_num_bytes;
9193
9194
0
                    au1_is_recon_available[U_PLANE] = 0;
9195
9196
0
                    if(ps_ctxt->s_cu_final_recon_flags.u1_eval_recon_data &&
9197
0
                       (!u1_compute_spatial_ssd_chroma ||
9198
0
                        (!au1_is_recon_available[U_PLANE] && u1_compute_spatial_ssd_chroma)))
9199
0
                    {
9200
0
                        if(!ps_recon_datastore->au1_is_chromaRecon_available[0] ||
9201
0
                           (ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9202
0
                            (UCHAR_MAX ==
9203
0
                             ps_recon_datastore
9204
0
                                 ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr][i4_subtu_idx])))
9205
0
                        {
9206
0
                            ihevce_chroma_it_recon_fxn(
9207
0
                                ps_ctxt,
9208
0
                                pi2_chroma_deq,
9209
0
                                cu_size,
9210
0
                                pu1_chroma_pred,
9211
0
                                pred_chrm_strd,
9212
0
                                pu1_chroma_recon,
9213
0
                                recon_chrma_strd,
9214
0
                                pu1_final_ecd_data,
9215
0
                                chroma_trans_size,
9216
0
                                (i4_subtu_idx == 0) ? ps_tu->b1_cb_cbf : ps_tu->b1_cb_cbf_subtu1,
9217
0
                                u4_zero_col,
9218
0
                                u4_zero_row,
9219
0
                                U_PLANE);
9220
0
                        }
9221
0
                        else if(
9222
0
                            ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9223
0
                            (UCHAR_MAX !=
9224
0
                             ps_recon_datastore
9225
0
                                 ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr][i4_subtu_idx]))
9226
0
                        {
9227
0
                            UWORD8 *pu1_recon_src =
9228
0
                                ((UWORD8 *)ps_recon_datastore->apv_chroma_recon_bufs
9229
0
                                     [ps_recon_datastore->au1_bufId_with_winning_ChromaRecon
9230
0
                                          [U_PLANE][ctr][i4_subtu_idx]]) +
9231
0
                                i4_subtu_pos_x +
9232
0
                                i4_subtu_pos_y * ps_recon_datastore->i4_chromaRecon_stride;
9233
9234
0
                            ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
9235
0
                                pu1_recon_src,
9236
0
                                ps_recon_datastore->i4_lumaRecon_stride,
9237
0
                                pu1_chroma_recon,
9238
0
                                recon_chrma_strd,
9239
0
                                chroma_trans_size,
9240
0
                                chroma_trans_size,
9241
0
                                U_PLANE);
9242
0
                        }
9243
0
                    }
9244
9245
0
                    u1_is_cu_coded |=
9246
0
                        ((1 == i4_subtu_idx) ? ps_tu->b1_cb_cbf_subtu1 : ps_tu->b1_cb_cbf);
9247
9248
0
                    pu1_final_ecd_data += i4_num_bytes;
9249
0
                    total_bytes += i4_num_bytes;
9250
0
                }
9251
9252
0
                for(i4_subtu_idx = 0; i4_subtu_idx < u1_num_subtus; i4_subtu_idx++)
9253
0
                {
9254
0
                    WORD32 chroma_trans_size = MAX(4, trans_size >> 1);
9255
0
                    WORD32 i4_subtu_pos_x = cu_pos_x_in_pix;
9256
0
                    WORD32 i4_subtu_pos_y = cu_pos_y_in_pix + (i4_subtu_idx * chroma_trans_size);
9257
9258
0
                    if(0 == u1_is_422)
9259
0
                    {
9260
0
                        i4_subtu_pos_y >>= 1;
9261
0
                    }
9262
9263
0
                    pu1_chroma_pred =
9264
0
                        pu1_cur_pred_chrm + (i4_subtu_idx * chroma_trans_size * pred_chrm_strd);
9265
0
                    pu1_chroma_recon = pu1_cur_chroma_recon +
9266
0
                                       (i4_subtu_idx * chroma_trans_size * recon_chrma_strd);
9267
0
                    pi2_chroma_deq = pi2_cur_deq_data_chrm +
9268
0
                                     (i4_subtu_idx * chroma_trans_size * cu_size) +
9269
0
                                     chroma_trans_size;
9270
9271
0
                    u4_zero_col = ps_tu_enc_loop_temp_prms->au4_cr_zero_col[i4_subtu_idx];
9272
0
                    u4_zero_row = ps_tu_enc_loop_temp_prms->au4_cr_zero_row[i4_subtu_idx];
9273
9274
0
                    if(ps_prms->u1_will_cabac_state_change)
9275
0
                    {
9276
0
                        i4_num_bytes =
9277
0
                            ps_tu_enc_loop_temp_prms->ai2_cr_bytes_consumed[i4_subtu_idx];
9278
0
                    }
9279
0
                    else
9280
0
                    {
9281
0
                        i4_num_bytes = 0;
9282
0
                    }
9283
9284
0
                    memcpy(pu1_final_ecd_data, pu1_old_ecd_data, i4_num_bytes);
9285
9286
0
                    pu1_old_ecd_data += i4_num_bytes;
9287
9288
0
                    au1_is_recon_available[V_PLANE] = 0;
9289
9290
0
                    if(ps_ctxt->s_cu_final_recon_flags.u1_eval_recon_data &&
9291
0
                       (!u1_compute_spatial_ssd_chroma ||
9292
0
                        (!au1_is_recon_available[V_PLANE] && u1_compute_spatial_ssd_chroma)))
9293
0
                    {
9294
0
                        if(!ps_recon_datastore->au1_is_chromaRecon_available[0] ||
9295
0
                           (ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9296
0
                            (UCHAR_MAX ==
9297
0
                             ps_recon_datastore
9298
0
                                 ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr][i4_subtu_idx])))
9299
0
                        {
9300
0
                            ihevce_chroma_it_recon_fxn(
9301
0
                                ps_ctxt,
9302
0
                                pi2_chroma_deq,
9303
0
                                cu_size,
9304
0
                                pu1_chroma_pred,
9305
0
                                pred_chrm_strd,
9306
0
                                pu1_chroma_recon,
9307
0
                                recon_chrma_strd,
9308
0
                                pu1_final_ecd_data,
9309
0
                                chroma_trans_size,
9310
0
                                (i4_subtu_idx == 0) ? ps_tu->b1_cr_cbf : ps_tu->b1_cr_cbf_subtu1,
9311
0
                                u4_zero_col,
9312
0
                                u4_zero_row,
9313
0
                                V_PLANE);
9314
0
                        }
9315
0
                        else if(
9316
0
                            ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9317
0
                            (UCHAR_MAX !=
9318
0
                             ps_recon_datastore
9319
0
                                 ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr][i4_subtu_idx]))
9320
0
                        {
9321
0
                            UWORD8 *pu1_recon_src =
9322
0
                                ((UWORD8 *)ps_recon_datastore->apv_chroma_recon_bufs
9323
0
                                     [ps_recon_datastore->au1_bufId_with_winning_ChromaRecon
9324
0
                                          [V_PLANE][ctr][i4_subtu_idx]]) +
9325
0
                                i4_subtu_pos_x +
9326
0
                                i4_subtu_pos_y * ps_recon_datastore->i4_chromaRecon_stride;
9327
9328
0
                            ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
9329
0
                                pu1_recon_src,
9330
0
                                ps_recon_datastore->i4_lumaRecon_stride,
9331
0
                                pu1_chroma_recon,
9332
0
                                recon_chrma_strd,
9333
0
                                chroma_trans_size,
9334
0
                                chroma_trans_size,
9335
0
                                V_PLANE);
9336
0
                        }
9337
0
                    }
9338
9339
0
                    u1_is_cu_coded |=
9340
0
                        ((1 == i4_subtu_idx) ? ps_tu->b1_cr_cbf_subtu1 : ps_tu->b1_cr_cbf);
9341
9342
0
                    pu1_final_ecd_data += i4_num_bytes;
9343
0
                    total_bytes += i4_num_bytes;
9344
0
                }
9345
0
            }
9346
3.95M
            else
9347
3.95M
            {
9348
3.95M
                WORD32 cb_zero_col, cb_zero_row, cr_zero_col, cr_zero_row;
9349
9350
7.90M
                for(i4_subtu_idx = 0; i4_subtu_idx < u1_num_subtus; i4_subtu_idx++)
9351
3.95M
                {
9352
3.95M
                    WORD32 cb_cbf, cr_cbf;
9353
3.95M
                    WORD32 cb_num_bytes, cr_num_bytes;
9354
9355
3.95M
                    WORD32 chroma_trans_size = MAX(4, trans_size >> 1);
9356
9357
3.95M
                    WORD32 i4_subtu_pos_x = cu_pos_x_in_pix;
9358
3.95M
                    WORD32 i4_subtu_pos_y = cu_pos_y_in_pix + (i4_subtu_idx * chroma_trans_size);
9359
9360
3.95M
                    if(0 == u1_is_422)
9361
3.95M
                    {
9362
3.95M
                        i4_subtu_pos_y >>= 1;
9363
3.95M
                    }
9364
9365
3.95M
                    pu1_cur_src_chrm += (i4_subtu_idx * chroma_trans_size * src_chrm_strd);
9366
3.95M
                    pu1_cur_pred_chrm += (i4_subtu_idx * chroma_trans_size * pred_chrm_strd);
9367
3.95M
                    pu1_cur_chroma_recon += (i4_subtu_idx * chroma_trans_size * recon_chrma_strd);
9368
3.95M
                    pi2_cur_deq_data_chrm += (i4_subtu_idx * chroma_trans_size * cu_size);
9369
9370
3.95M
                    if((PRED_MODE_INTRA == packed_pred_mode) &&
9371
1.81M
                       (1 == ps_ctxt->s_cu_final_recon_flags.u1_eval_chroma_pred_data))
9372
495k
                    {
9373
495k
                        WORD32 nbr_flags, left_strd_chrm, chrm_pred_func_idx;
9374
495k
                        UWORD8 *pu1_left_chrm;
9375
495k
                        UWORD8 *pu1_top_chrm;
9376
495k
                        UWORD8 *pu1_top_left_chrm;
9377
9378
495k
                        nbr_flags = ihevce_get_intra_chroma_tu_nbr(
9379
495k
                            *pu4_nbr_flags, i4_subtu_idx, chroma_trans_size, u1_is_422);
9380
9381
                        /* left cu boundary */
9382
495k
                        if(0 == i4_subtu_pos_x)
9383
421k
                        {
9384
421k
                            left_strd_chrm = ps_chrm_cu_buf_prms->i4_cu_left_stride;
9385
421k
                            pu1_left_chrm =
9386
421k
                                ps_chrm_cu_buf_prms->pu1_cu_left + i4_subtu_pos_y * left_strd_chrm;
9387
421k
                        }
9388
73.2k
                        else
9389
73.2k
                        {
9390
73.2k
                            pu1_left_chrm = pu1_cur_chroma_recon - 2;
9391
73.2k
                            left_strd_chrm = recon_chrma_strd;
9392
73.2k
                        }
9393
9394
                        /* top cu boundary */
9395
495k
                        if(0 == i4_subtu_pos_y)
9396
421k
                        {
9397
421k
                            pu1_top_chrm = ps_chrm_cu_buf_prms->pu1_cu_top + i4_subtu_pos_x;
9398
421k
                        }
9399
73.2k
                        else
9400
73.2k
                        {
9401
73.2k
                            pu1_top_chrm = pu1_cur_chroma_recon - recon_chrma_strd;
9402
73.2k
                        }
9403
9404
                        /* by default top left is set to cu top left */
9405
495k
                        pu1_top_left_chrm = ps_chrm_cu_buf_prms->pu1_cu_top_left;
9406
9407
                        /* top left based on position */
9408
495k
                        if((0 != i4_subtu_pos_y) && (0 == i4_subtu_pos_x))
9409
36.6k
                        {
9410
36.6k
                            pu1_top_left_chrm = pu1_left_chrm - left_strd_chrm;
9411
36.6k
                        }
9412
458k
                        else if(0 != i4_subtu_pos_x)
9413
73.2k
                        {
9414
73.2k
                            pu1_top_left_chrm = pu1_top_chrm - 2;
9415
73.2k
                        }
9416
9417
                        /* call the chroma reference array substitution */
9418
495k
                        ihevc_intra_pred_chroma_ref_substitution_fptr(
9419
495k
                            pu1_top_left_chrm,
9420
495k
                            pu1_top_chrm,
9421
495k
                            pu1_left_chrm,
9422
495k
                            left_strd_chrm,
9423
495k
                            chroma_trans_size,
9424
495k
                            nbr_flags,
9425
495k
                            (UWORD8 *)ps_ctxt->pv_ref_sub_out,
9426
495k
                            1,
9427
495k
                            CHROMA_FMT_IDC_YUV420);
9428
9429
                        /* use the look up to get the function idx */
9430
495k
                        chrm_pred_func_idx = g_i4_ip_funcs[chroma_pred_mode];
9431
9432
                        /* call the intra prediction function */
9433
495k
                        ps_ctxt->apf_chrm_ip[chrm_pred_func_idx](
9434
495k
                            (UWORD8 *)ps_ctxt->pv_ref_sub_out,
9435
495k
                            1,
9436
495k
                            pu1_cur_pred_chrm,
9437
495k
                            pred_chrm_strd,
9438
495k
                            chroma_trans_size,
9439
495k
                            chroma_pred_mode);
9440
495k
                    }
9441
9442
                    /**---------- Compute iq&coeff data if required : Chroma ------------**/
9443
3.95M
                    if(1 == ps_tu_enc_loop_temp_prms->b1_eval_chroma_iq_and_coeff_data)
9444
1.08M
                    {
9445
1.08M
                        WORD32 perform_sbh, perform_rdoq, temp_bits;
9446
9447
1.08M
                        if(ps_prms->u1_recompute_sbh_and_rdoq)
9448
0
                        {
9449
0
                            perform_sbh = (ps_ctxt->i4_sbh_level != NO_SBH);
9450
0
                            perform_rdoq = (ps_ctxt->i4_rdoq_level != NO_RDOQ);
9451
0
                        }
9452
1.08M
                        else
9453
1.08M
                        {
9454
                            /* RDOQ will change the coefficients. If coefficients are changed, we will have to do sbh again*/
9455
1.08M
                            perform_sbh = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_best_cand_sbh;
9456
                            /* To do SBH we need the quant and iquant data. This would mean we need to do quantization again, which would mean
9457
                        we would have to do RDOQ again.*/
9458
1.08M
                            perform_rdoq = ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_best_cand_rdoq;
9459
1.08M
                        }
9460
9461
                        /* populate the coeffs scan idx */
9462
1.08M
                        ps_ctxt->i4_scan_idx = SCAN_DIAG_UPRIGHT;
9463
9464
1.08M
                        if(PRED_MODE_INTRA == packed_pred_mode)
9465
495k
                        {
9466
                            /* for 4x4 transforms based on intra pred mode scan is choosen*/
9467
495k
                            if(4 == chroma_trans_size)
9468
265k
                            {
9469
                                /* for modes from 22 upto 30 horizontal scan is used */
9470
265k
                                if((chroma_pred_mode > 21) && (chroma_pred_mode < 31))
9471
22.6k
                                {
9472
22.6k
                                    ps_ctxt->i4_scan_idx = SCAN_HORZ;
9473
22.6k
                                }
9474
                                /* for modes from 6 upto 14 horizontal scan is used */
9475
242k
                                else if((chroma_pred_mode > 5) && (chroma_pred_mode < 15))
9476
84.5k
                                {
9477
84.5k
                                    ps_ctxt->i4_scan_idx = SCAN_VERT;
9478
84.5k
                                }
9479
265k
                            }
9480
495k
                        }
9481
9482
#if DISABLE_RDOQ_INTRA
9483
                        if(PRED_MODE_INTRA == packed_pred_mode)
9484
                        {
9485
                            perform_rdoq = 0;
9486
                        }
9487
#endif
9488
9489
                        /* RDOPT copy States :  TU init (best until prev TU) to current */
9490
1.08M
                        COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
9491
1.08M
                            &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
9492
1.08M
                                    .s_cabac_ctxt.au1_ctxt_models[0] +
9493
1.08M
                                IHEVC_CAB_COEFFX_PREFIX,
9494
1.08M
                            &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
9495
1.08M
                            IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
9496
9497
1.08M
                        ASSERT(rd_opt_best_idx == ps_ctxt->s_rdopt_entropy_ctxt.i4_curr_buf_idx);
9498
                        /*If BEST candidate RDOQ is enabled, Eithe no coef level rdoq or CU level rdoq has to be enabled
9499
                    so that all candidates and best candidate are quantized with same rounding factor  */
9500
1.08M
                        if(1 == perform_rdoq)
9501
0
                        {
9502
0
                            ASSERT(ps_ctxt->i4_quant_rounding_level != TU_LEVEL_QUANT_ROUNDING);
9503
0
                        }
9504
9505
1.08M
                        if(!ps_best_cu_prms->u1_skip_flag ||
9506
4.71k
                           !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt)
9507
1.08M
                        {
9508
                            /* Cb */
9509
1.08M
                            cb_cbf = ihevce_chroma_t_q_iq_ssd_scan_fxn(
9510
1.08M
                                ps_ctxt,
9511
1.08M
                                pu1_cur_pred_chrm,
9512
1.08M
                                pred_chrm_strd,
9513
1.08M
                                pu1_cur_src_chrm,
9514
1.08M
                                src_chrm_strd,
9515
1.08M
                                pi2_cur_deq_data_chrm,
9516
1.08M
                                cu_size,
9517
1.08M
                                pu1_chrm_recon,
9518
1.08M
                                recon_chrma_strd,
9519
1.08M
                                pu1_final_ecd_data,
9520
1.08M
                                pu1_csbf_buf,
9521
1.08M
                                csbf_strd,
9522
1.08M
                                chroma_trans_size,
9523
1.08M
                                ps_ctxt->i4_scan_idx,
9524
1.08M
                                (PRED_MODE_INTRA == packed_pred_mode),
9525
1.08M
                                &cb_num_bytes,
9526
1.08M
                                &temp_bits,
9527
1.08M
                                &cb_zero_col,
9528
1.08M
                                &cb_zero_row,
9529
1.08M
                                &au1_is_recon_available[U_PLANE],
9530
1.08M
                                perform_sbh,
9531
1.08M
                                perform_rdoq,
9532
1.08M
                                &i8_ssd,
9533
1.08M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
9534
1.08M
                                !ps_ctxt->u1_is_refPic
9535
1.08M
                                    ? ALPHA_FOR_NOISE_TERM_IN_RDOPT
9536
1.08M
                                    : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
9537
986k
                                       (double)ALPHA_FOR_NOISE_TERM_IN_RDOPT) /
9538
986k
                                          100.0,
9539
1.08M
                                ps_prms->u1_is_cu_noisy,
9540
1.08M
#endif
9541
1.08M
                                ps_best_cu_prms->u1_skip_flag &&
9542
4.71k
                                    ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt,
9543
1.08M
                                u1_compute_spatial_ssd_chroma ? SPATIAL_DOMAIN_SSD
9544
1.08M
                                                              : FREQUENCY_DOMAIN_SSD,
9545
1.08M
                                U_PLANE);
9546
1.08M
                        }
9547
0
                        else
9548
0
                        {
9549
0
                            cb_cbf = 0;
9550
0
                            temp_bits = 0;
9551
0
                            cb_num_bytes = 0;
9552
0
                            au1_is_recon_available[U_PLANE] = 0;
9553
0
                            cb_zero_col = 0;
9554
0
                            cb_zero_row = 0;
9555
0
                        }
9556
9557
                        /* Accumulate chroma residual bits */
9558
1.08M
                        ps_best_cu_prms->u4_cu_chroma_res_bits += temp_bits;
9559
9560
                        /* RDOPT copy States :  New updated after curr TU to TU init */
9561
1.08M
                        if(0 != cb_cbf)
9562
244k
                        {
9563
244k
                            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
9564
244k
                                &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
9565
244k
                                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
9566
244k
                                        .s_cabac_ctxt.au1_ctxt_models[0] +
9567
244k
                                    IHEVC_CAB_COEFFX_PREFIX,
9568
244k
                                IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
9569
244k
                        }
9570
                        /* RDOPT copy States :  Restoring back the Cb init state to Cr */
9571
842k
                        else
9572
842k
                        {
9573
842k
                            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
9574
842k
                                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
9575
842k
                                        .s_cabac_ctxt.au1_ctxt_models[0] +
9576
842k
                                    IHEVC_CAB_COEFFX_PREFIX,
9577
842k
                                &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
9578
842k
                                IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
9579
842k
                        }
9580
9581
1.08M
                        if(!ps_best_cu_prms->u1_skip_flag ||
9582
4.71k
                           !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt)
9583
1.08M
                        {
9584
                            /* Cr */
9585
1.08M
                            cr_cbf = ihevce_chroma_t_q_iq_ssd_scan_fxn(
9586
1.08M
                                ps_ctxt,
9587
1.08M
                                pu1_cur_pred_chrm,
9588
1.08M
                                pred_chrm_strd,
9589
1.08M
                                pu1_cur_src_chrm,
9590
1.08M
                                src_chrm_strd,
9591
1.08M
                                pi2_cur_deq_data_chrm + chroma_trans_size,
9592
1.08M
                                cu_size,
9593
1.08M
                                pu1_chrm_recon,
9594
1.08M
                                recon_chrma_strd,
9595
1.08M
                                pu1_final_ecd_data + cb_num_bytes,
9596
1.08M
                                pu1_csbf_buf,
9597
1.08M
                                csbf_strd,
9598
1.08M
                                chroma_trans_size,
9599
1.08M
                                ps_ctxt->i4_scan_idx,
9600
1.08M
                                (PRED_MODE_INTRA == packed_pred_mode),
9601
1.08M
                                &cr_num_bytes,
9602
1.08M
                                &temp_bits,
9603
1.08M
                                &cr_zero_col,
9604
1.08M
                                &cr_zero_row,
9605
1.08M
                                &au1_is_recon_available[V_PLANE],
9606
1.08M
                                perform_sbh,
9607
1.08M
                                perform_rdoq,
9608
1.08M
                                &i8_ssd,
9609
1.08M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
9610
1.08M
                                !ps_ctxt->u1_is_refPic
9611
1.08M
                                    ? ALPHA_FOR_NOISE_TERM_IN_RDOPT
9612
1.08M
                                    : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
9613
986k
                                       (double)ALPHA_FOR_NOISE_TERM_IN_RDOPT) /
9614
986k
                                          100.0,
9615
1.08M
                                ps_prms->u1_is_cu_noisy,
9616
1.08M
#endif
9617
1.08M
                                ps_best_cu_prms->u1_skip_flag &&
9618
4.71k
                                    ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt,
9619
1.08M
                                u1_compute_spatial_ssd_chroma ? SPATIAL_DOMAIN_SSD
9620
1.08M
                                                              : FREQUENCY_DOMAIN_SSD,
9621
1.08M
                                V_PLANE);
9622
1.08M
                        }
9623
0
                        else
9624
0
                        {
9625
0
                            cr_cbf = 0;
9626
0
                            temp_bits = 0;
9627
0
                            cr_num_bytes = 0;
9628
0
                            au1_is_recon_available[V_PLANE] = 0;
9629
0
                            cr_zero_col = 0;
9630
0
                            cr_zero_row = 0;
9631
0
                        }
9632
9633
                        /* Accumulate chroma residual bits */
9634
1.08M
                        ps_best_cu_prms->u4_cu_chroma_res_bits += temp_bits;
9635
9636
                        /* RDOPT copy States :  New updated after curr TU to TU init */
9637
1.08M
                        if(0 != cr_cbf)
9638
244k
                        {
9639
244k
                            COPY_CABAC_STATES_FRM_CAB_COEFFX_PREFIX(
9640
244k
                                &ps_ctxt->au1_rdopt_init_ctxt_models[0] + IHEVC_CAB_COEFFX_PREFIX,
9641
244k
                                &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
9642
244k
                                        .s_cabac_ctxt.au1_ctxt_models[0] +
9643
244k
                                    IHEVC_CAB_COEFFX_PREFIX,
9644
244k
                                IHEVC_CAB_CTXT_END - IHEVC_CAB_COEFFX_PREFIX);
9645
244k
                        }
9646
9647
1.08M
                        if(0 == i4_subtu_idx)
9648
1.08M
                        {
9649
1.08M
                            ps_tu->b1_cb_cbf = cb_cbf;
9650
1.08M
                            ps_tu->b1_cr_cbf = cr_cbf;
9651
1.08M
                        }
9652
0
                        else
9653
0
                        {
9654
0
                            ps_tu->b1_cb_cbf_subtu1 = cb_cbf;
9655
0
                            ps_tu->b1_cr_cbf_subtu1 = cr_cbf;
9656
0
                        }
9657
1.08M
                    }
9658
2.86M
                    else
9659
2.86M
                    {
9660
2.86M
                        cb_zero_col = ps_tu_enc_loop_temp_prms->au4_cb_zero_col[i4_subtu_idx];
9661
2.86M
                        cb_zero_row = ps_tu_enc_loop_temp_prms->au4_cb_zero_row[i4_subtu_idx];
9662
2.86M
                        cr_zero_col = ps_tu_enc_loop_temp_prms->au4_cr_zero_col[i4_subtu_idx];
9663
2.86M
                        cr_zero_row = ps_tu_enc_loop_temp_prms->au4_cr_zero_row[i4_subtu_idx];
9664
9665
2.86M
                        if(ps_prms->u1_will_cabac_state_change)
9666
2.86M
                        {
9667
2.86M
                            cb_num_bytes =
9668
2.86M
                                ps_tu_enc_loop_temp_prms->ai2_cb_bytes_consumed[i4_subtu_idx];
9669
2.86M
                        }
9670
0
                        else
9671
0
                        {
9672
0
                            cb_num_bytes = 0;
9673
0
                        }
9674
9675
2.86M
                        if(ps_prms->u1_will_cabac_state_change)
9676
2.86M
                        {
9677
2.86M
                            cr_num_bytes =
9678
2.86M
                                ps_tu_enc_loop_temp_prms->ai2_cr_bytes_consumed[i4_subtu_idx];
9679
2.86M
                        }
9680
0
                        else
9681
0
                        {
9682
0
                            cr_num_bytes = 0;
9683
0
                        }
9684
9685
                        /* copy cb ecd data to final buffer */
9686
2.86M
                        memcpy(pu1_final_ecd_data, pu1_chrm_old_ecd_data, cb_num_bytes);
9687
9688
2.86M
                        pu1_chrm_old_ecd_data += cb_num_bytes;
9689
9690
                        /* copy cb ecd data to final buffer */
9691
2.86M
                        memcpy(
9692
2.86M
                            (pu1_final_ecd_data + cb_num_bytes),
9693
2.86M
                            pu1_chrm_old_ecd_data,
9694
2.86M
                            cr_num_bytes);
9695
9696
2.86M
                        pu1_chrm_old_ecd_data += cr_num_bytes;
9697
9698
2.86M
                        au1_is_recon_available[U_PLANE] = 0;
9699
2.86M
                        au1_is_recon_available[V_PLANE] = 0;
9700
2.86M
                    }
9701
9702
                    /**-------- Compute Recon data (Do IT & Recon) : Chroma  -----------**/
9703
3.95M
                    if(ps_ctxt->s_cu_final_recon_flags.u1_eval_recon_data &&
9704
3.89M
                       (!u1_compute_spatial_ssd_chroma ||
9705
0
                        (!au1_is_recon_available[U_PLANE] && u1_compute_spatial_ssd_chroma)))
9706
3.89M
                    {
9707
3.89M
                        if(!ps_recon_datastore->au1_is_chromaRecon_available[0] ||
9708
1.23M
                           (ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9709
1.23M
                            (UCHAR_MAX ==
9710
1.23M
                             ps_recon_datastore
9711
1.23M
                                 ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr][i4_subtu_idx])))
9712
3.72M
                        {
9713
3.72M
                            ihevce_chroma_it_recon_fxn(
9714
3.72M
                                ps_ctxt,
9715
3.72M
                                pi2_cur_deq_data_chrm,
9716
3.72M
                                cu_size,
9717
3.72M
                                pu1_cur_pred_chrm,
9718
3.72M
                                pred_chrm_strd,
9719
3.72M
                                pu1_cur_chroma_recon,
9720
3.72M
                                recon_chrma_strd,
9721
3.72M
                                pu1_final_ecd_data,
9722
3.72M
                                chroma_trans_size,
9723
3.72M
                                (i4_subtu_idx == 0) ? ps_tu->b1_cb_cbf : ps_tu->b1_cb_cbf_subtu1,
9724
3.72M
                                cb_zero_col,
9725
3.72M
                                cb_zero_row,
9726
3.72M
                                U_PLANE);
9727
3.72M
                        }
9728
172k
                        else if(
9729
172k
                            ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9730
172k
                            (UCHAR_MAX !=
9731
172k
                             ps_recon_datastore
9732
172k
                                 ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr][i4_subtu_idx]))
9733
172k
                        {
9734
172k
                            UWORD8 *pu1_recon_src =
9735
172k
                                ((UWORD8 *)ps_recon_datastore->apv_chroma_recon_bufs
9736
172k
                                     [ps_recon_datastore->au1_bufId_with_winning_ChromaRecon
9737
172k
                                          [U_PLANE][ctr][i4_subtu_idx]]) +
9738
172k
                                i4_subtu_pos_x +
9739
172k
                                i4_subtu_pos_y * ps_recon_datastore->i4_chromaRecon_stride;
9740
9741
172k
                            ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
9742
172k
                                pu1_recon_src,
9743
172k
                                ps_recon_datastore->i4_lumaRecon_stride,
9744
172k
                                pu1_cur_chroma_recon,
9745
172k
                                recon_chrma_strd,
9746
172k
                                chroma_trans_size,
9747
172k
                                chroma_trans_size,
9748
172k
                                U_PLANE);
9749
172k
                        }
9750
3.89M
                    }
9751
9752
3.95M
                    u1_is_cu_coded |=
9753
3.95M
                        ((1 == i4_subtu_idx) ? ps_tu->b1_cb_cbf_subtu1 : ps_tu->b1_cb_cbf);
9754
9755
3.95M
                    if(ps_prms->u1_will_cabac_state_change)
9756
3.95M
                    {
9757
3.95M
                        ps_tu_enc_loop->ai4_cb_coeff_offset[i4_subtu_idx] = total_bytes;
9758
3.95M
                    }
9759
9760
3.95M
                    pu1_final_ecd_data += cb_num_bytes;
9761
                    /* update total bytes consumed */
9762
3.95M
                    total_bytes += cb_num_bytes;
9763
9764
3.95M
                    if(ps_ctxt->s_cu_final_recon_flags.u1_eval_recon_data &&
9765
3.89M
                       (!u1_compute_spatial_ssd_chroma ||
9766
0
                        (!au1_is_recon_available[V_PLANE] && u1_compute_spatial_ssd_chroma)))
9767
3.89M
                    {
9768
3.89M
                        if(!ps_recon_datastore->au1_is_chromaRecon_available[0] ||
9769
1.23M
                           (ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9770
1.23M
                            (UCHAR_MAX ==
9771
1.23M
                             ps_recon_datastore
9772
1.23M
                                 ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr][i4_subtu_idx])))
9773
3.72M
                        {
9774
3.72M
                            ihevce_chroma_it_recon_fxn(
9775
3.72M
                                ps_ctxt,
9776
3.72M
                                pi2_cur_deq_data_chrm + chroma_trans_size,
9777
3.72M
                                cu_size,
9778
3.72M
                                pu1_cur_pred_chrm,
9779
3.72M
                                pred_chrm_strd,
9780
3.72M
                                pu1_cur_chroma_recon,
9781
3.72M
                                recon_chrma_strd,
9782
3.72M
                                pu1_final_ecd_data,
9783
3.72M
                                chroma_trans_size,
9784
3.72M
                                (i4_subtu_idx == 0) ? ps_tu->b1_cr_cbf : ps_tu->b1_cr_cbf_subtu1,
9785
3.72M
                                cr_zero_col,
9786
3.72M
                                cr_zero_row,
9787
3.72M
                                V_PLANE);
9788
3.72M
                        }
9789
172k
                        else if(
9790
172k
                            ps_recon_datastore->au1_is_chromaRecon_available[0] &&
9791
172k
                            (UCHAR_MAX !=
9792
172k
                             ps_recon_datastore
9793
172k
                                 ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr][i4_subtu_idx]))
9794
172k
                        {
9795
172k
                            UWORD8 *pu1_recon_src =
9796
172k
                                ((UWORD8 *)ps_recon_datastore->apv_chroma_recon_bufs
9797
172k
                                     [ps_recon_datastore->au1_bufId_with_winning_ChromaRecon
9798
172k
                                          [V_PLANE][ctr][i4_subtu_idx]]) +
9799
172k
                                i4_subtu_pos_x +
9800
172k
                                i4_subtu_pos_y * ps_recon_datastore->i4_chromaRecon_stride;
9801
9802
172k
                            ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
9803
172k
                                pu1_recon_src,
9804
172k
                                ps_recon_datastore->i4_lumaRecon_stride,
9805
172k
                                pu1_cur_chroma_recon,
9806
172k
                                recon_chrma_strd,
9807
172k
                                chroma_trans_size,
9808
172k
                                chroma_trans_size,
9809
172k
                                V_PLANE);
9810
172k
                        }
9811
3.89M
                    }
9812
9813
3.95M
                    u1_is_cu_coded |=
9814
3.95M
                        ((1 == i4_subtu_idx) ? ps_tu->b1_cr_cbf_subtu1 : ps_tu->b1_cr_cbf);
9815
9816
3.95M
                    if(ps_prms->u1_will_cabac_state_change)
9817
3.95M
                    {
9818
3.95M
                        ps_tu_enc_loop->ai4_cr_coeff_offset[i4_subtu_idx] = total_bytes;
9819
3.95M
                    }
9820
9821
3.95M
                    pu1_final_ecd_data += cr_num_bytes;
9822
                    /* update total bytes consumed */
9823
3.95M
                    total_bytes += cr_num_bytes;
9824
3.95M
                }
9825
3.95M
            }
9826
3.95M
        }
9827
1.45M
        else
9828
1.45M
        {
9829
1.45M
            ps_tu_enc_loop->ai4_cb_coeff_offset[0] = total_bytes;
9830
1.45M
            ps_tu_enc_loop->ai4_cr_coeff_offset[0] = total_bytes;
9831
1.45M
            ps_tu_enc_loop->ai4_cb_coeff_offset[1] = total_bytes;
9832
1.45M
            ps_tu_enc_loop->ai4_cr_coeff_offset[1] = total_bytes;
9833
1.45M
            ps_tu->b1_cb_cbf = 0;
9834
1.45M
            ps_tu->b1_cr_cbf = 0;
9835
1.45M
            ps_tu->b1_cb_cbf_subtu1 = 0;
9836
1.45M
            ps_tu->b1_cr_cbf_subtu1 = 0;
9837
1.45M
        }
9838
9839
        /* Update to next TU */
9840
5.40M
        ps_tu_enc_loop++;
9841
5.40M
        ps_tu_enc_loop_temp_prms++;
9842
9843
5.40M
        pu4_nbr_flags++;
9844
5.40M
        pu1_intra_pred_mode++;
9845
9846
        /*Do not set the nbr map for last pu in cu */
9847
5.40M
        if((num_tu_in_cu - 1) != ctr)
9848
2.41M
        {
9849
            /* set the neighbour map to 1 */
9850
2.41M
            ihevce_set_nbr_map(
9851
2.41M
                ps_ctxt->pu1_ctb_nbr_map,
9852
2.41M
                ps_ctxt->i4_nbr_map_strd,
9853
2.41M
                cu_pos_x_in_4x4,
9854
2.41M
                cu_pos_y_in_4x4,
9855
2.41M
                (trans_size >> 2),
9856
2.41M
                1);
9857
2.41M
        }
9858
5.40M
    }
9859
9860
2.99M
    if(ps_prms->u1_will_cabac_state_change)
9861
2.99M
    {
9862
2.99M
        ps_best_cu_prms->u1_is_cu_coded = u1_is_cu_coded;
9863
9864
        /* Modify skip flag, if luma is skipped & Chroma is coded */
9865
2.99M
        if((1 == u1_is_cu_coded) && (PRED_MODE_SKIP == packed_pred_mode))
9866
1.53k
        {
9867
1.53k
            ps_best_cu_prms->u1_skip_flag = 0;
9868
1.53k
        }
9869
2.99M
    }
9870
9871
    /* during chroma evaluation if skip decision was over written     */
9872
    /* then the current skip candidate is set to a non skip candidate */
9873
2.99M
    if(PRED_MODE_INTRA != packed_pred_mode)
9874
1.50M
    {
9875
1.50M
        ps_best_inter_cand->b1_skip_flag = ps_best_cu_prms->u1_skip_flag;
9876
1.50M
    }
9877
9878
    /**------------- Compute header data if required --------------**/
9879
2.99M
    if(1 == ps_ctxt->s_cu_final_recon_flags.u1_eval_header_data)
9880
720k
    {
9881
720k
        WORD32 cbf_bits;
9882
720k
        WORD32 cu_bits;
9883
720k
        WORD32 unit_4x4_size = cu_size >> 2;
9884
9885
        /*Restoring the running reference into the best rdopt_ctxt cabac states which will then
9886
        be copied as the base reference for the next cu
9887
        Assumption : We are ensuring that the u1_eval_header_data flag is set to 1 only if either
9888
        luma and chroma are being reevaluated*/
9889
720k
        COPY_CABAC_STATES(
9890
720k
            &ps_ctxt->s_rdopt_entropy_ctxt.as_cu_entropy_ctxt[rd_opt_best_idx]
9891
720k
                 .s_cabac_ctxt.au1_ctxt_models[0],
9892
720k
            &ps_ctxt->au1_rdopt_init_ctxt_models[0],
9893
720k
            IHEVC_CAB_CTXT_END);
9894
9895
        /* get the neighbour availability flags for current cu  */
9896
720k
        ihevce_get_only_nbr_flag(
9897
720k
            &s_nbr,
9898
720k
            ps_ctxt->pu1_ctb_nbr_map,
9899
720k
            ps_ctxt->i4_nbr_map_strd,
9900
720k
            (cu_pos_x << 1),
9901
720k
            (cu_pos_y << 1),
9902
720k
            unit_4x4_size,
9903
720k
            unit_4x4_size);
9904
9905
720k
        cu_bits = ihevce_entropy_rdo_encode_cu(
9906
720k
            &ps_ctxt->s_rdopt_entropy_ctxt,
9907
720k
            ps_best_cu_prms,
9908
720k
            cu_pos_x,
9909
720k
            cu_pos_y,
9910
720k
            cu_size,
9911
720k
            ps_ctxt->u1_disable_intra_eval ? !DISABLE_TOP_SYNC && s_nbr.u1_top_avail
9912
720k
                                           : s_nbr.u1_top_avail,
9913
720k
            s_nbr.u1_left_avail,
9914
720k
            (pu1_final_ecd_data - total_bytes),
9915
720k
            &cbf_bits);
9916
9917
        /* cbf bits are excluded from header bits, instead considered as texture bits */
9918
720k
        ps_best_cu_prms->u4_cu_hdr_bits = cu_bits - cbf_bits;
9919
720k
        ps_best_cu_prms->u4_cu_cbf_bits = cbf_bits;
9920
720k
    }
9921
9922
2.99M
    if(ps_prms->u1_will_cabac_state_change)
9923
2.99M
    {
9924
2.99M
        ps_best_cu_prms->i4_num_bytes_ecd_data = total_bytes;
9925
2.99M
    }
9926
2.99M
}
9927
9928
/*!
9929
******************************************************************************
9930
* \if Function name : ihevce_set_eval_flags \endif
9931
*
9932
* \brief
9933
*    Function which decides which eval flags have to be set based on present
9934
*    and RDOQ conditions
9935
*
9936
* \param[in] ps_ctxt : encoder ctxt pointer
9937
* \param[in] enc_loop_cu_final_prms_t : pointer to final cu params
9938
*
9939
* \return
9940
*    None
9941
*
9942
* \author
9943
*  Ittiam
9944
*
9945
*****************************************************************************
9946
*/
9947
void ihevce_set_eval_flags(
9948
    ihevce_enc_loop_ctxt_t *ps_ctxt, enc_loop_cu_final_prms_t *ps_enc_loop_bestprms)
9949
2.99M
{
9950
2.99M
    WORD32 count = 0;
9951
9952
2.99M
    ps_ctxt->s_cu_final_recon_flags.u1_eval_luma_pred_data = 0;
9953
9954
2.99M
    ps_ctxt->s_cu_final_recon_flags.u1_eval_chroma_pred_data =
9955
2.99M
        !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt;
9956
9957
2.99M
    if(ps_ctxt->u1_disable_intra_eval && (!(ps_ctxt->i4_deblk_pad_hpel_cur_pic & 0x1)))
9958
32.9k
    {
9959
32.9k
        ps_ctxt->s_cu_final_recon_flags.u1_eval_recon_data = 0;
9960
32.9k
    }
9961
2.95M
    else
9962
2.95M
    {
9963
2.95M
        ps_ctxt->s_cu_final_recon_flags.u1_eval_recon_data = 1;
9964
2.95M
    }
9965
9966
2.99M
    if((1 == ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_best_cand_rdoq) ||
9967
2.99M
       (1 == ps_ctxt->s_rdoq_sbh_ctxt.i4_perform_best_cand_sbh))
9968
0
    {
9969
        /* When rdoq is enabled only for the best candidate, in case of in Intra nTU
9970
        RDOQ might have altered the coeffs of the neighbour CU. As a result, the pred
9971
        for the current CU will change. Therefore, we need to reevaluate the pred data*/
9972
0
        if((ps_enc_loop_bestprms->u2_num_tus_in_cu > 1) &&
9973
0
           (ps_enc_loop_bestprms->u1_intra_flag == 1))
9974
0
        {
9975
0
            ps_ctxt->s_cu_final_recon_flags.u1_eval_luma_pred_data = 1;
9976
0
            ps_ctxt->s_cu_final_recon_flags.u1_eval_chroma_pred_data = 1;
9977
0
        }
9978
0
        if(ps_enc_loop_bestprms->u1_skip_flag == 1)
9979
0
        {
9980
0
            for(count = 0; count < ps_enc_loop_bestprms->u2_num_tus_in_cu; count++)
9981
0
            {
9982
0
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
9983
0
                    .b1_eval_luma_iq_and_coeff_data = 0;
9984
0
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
9985
0
                    .b1_eval_chroma_iq_and_coeff_data = 0;
9986
0
            }
9987
0
        }
9988
0
        else
9989
0
        {
9990
0
            for(count = 0; count < ps_enc_loop_bestprms->u2_num_tus_in_cu; count++)
9991
0
            {
9992
0
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
9993
0
                    .b1_eval_luma_iq_and_coeff_data = 1;
9994
0
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
9995
0
                    .b1_eval_chroma_iq_and_coeff_data = 1;
9996
0
            }
9997
0
        }
9998
0
    }
9999
2.99M
    else
10000
2.99M
    {
10001
2.99M
        switch(ps_ctxt->i4_quality_preset)
10002
2.99M
        {
10003
1.83M
        case IHEVCE_QUALITY_P0:
10004
2.03M
        case IHEVCE_QUALITY_P2:
10005
2.27M
        case IHEVCE_QUALITY_P3:
10006
2.27M
        {
10007
6.15M
            for(count = 0; count < ps_enc_loop_bestprms->u2_num_tus_in_cu; count++)
10008
3.88M
            {
10009
3.88M
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10010
3.88M
                    .b1_eval_luma_iq_and_coeff_data = 0;
10011
3.88M
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10012
3.88M
                    .b1_eval_chroma_iq_and_coeff_data =
10013
3.88M
                    !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt;
10014
3.88M
            }
10015
10016
2.27M
            break;
10017
2.03M
        }
10018
165k
        case IHEVCE_QUALITY_P4:
10019
297k
        case IHEVCE_QUALITY_P5:
10020
297k
        {
10021
993k
            for(count = 0; count < ps_enc_loop_bestprms->u2_num_tus_in_cu; count++)
10022
695k
            {
10023
695k
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10024
695k
                    .b1_eval_luma_iq_and_coeff_data = 0;
10025
695k
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10026
695k
                    .b1_eval_chroma_iq_and_coeff_data =
10027
695k
                    !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt;
10028
695k
            }
10029
10030
297k
            break;
10031
165k
        }
10032
423k
        case IHEVCE_QUALITY_P6:
10033
423k
        {
10034
1.25M
            for(count = 0; count < ps_enc_loop_bestprms->u2_num_tus_in_cu; count++)
10035
829k
            {
10036
829k
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10037
829k
                    .b1_eval_luma_iq_and_coeff_data = 0;
10038
829k
#if !ENABLE_CHROMA_TRACKING_OF_LUMA_CBF_IN_XS25
10039
829k
                ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10040
829k
                    .b1_eval_chroma_iq_and_coeff_data =
10041
829k
                    !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt;
10042
#else
10043
                if((ps_ctxt->i1_slice_type == BSLICE) && (ps_ctxt->i4_temporal_layer_id > 1) &&
10044
                   (ps_enc_loop_bestprms->as_tu_enc_loop[count].s_tu.b3_size >= 2))
10045
                {
10046
                    ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10047
                        .b1_eval_chroma_iq_and_coeff_data =
10048
                        ps_enc_loop_bestprms->as_tu_enc_loop[count].s_tu.b1_y_cbf;
10049
                }
10050
                else
10051
                {
10052
                    ps_enc_loop_bestprms->as_tu_enc_loop_temp_prms[count]
10053
                        .b1_eval_chroma_iq_and_coeff_data =
10054
                        !ps_ctxt->s_chroma_rdopt_ctxt.u1_eval_chrm_rdopt;
10055
                }
10056
#endif
10057
829k
            }
10058
10059
423k
            break;
10060
165k
        }
10061
0
        default:
10062
0
        {
10063
0
            break;
10064
165k
        }
10065
2.99M
        }
10066
2.99M
    }
10067
10068
    /* Not recomputing Luma pred-data and header data for any preset now */
10069
2.99M
    ps_ctxt->s_cu_final_recon_flags.u1_eval_header_data = 1;
10070
2.99M
}
10071
10072
/**
10073
******************************************************************************
10074
*
10075
*  @brief Shrink's TU tree of inter CUs by merging redundnant child nodes
10076
*         (not coded children) into a parent node(not coded).
10077
*
10078
*  @par   Description
10079
*         This is required post RDO evaluation as TU decisions are
10080
*         pre-determined(pre RDO) based on recursive SATD,
10081
*         while the quad children TU's can be skipped during RDO
10082
*
10083
*         The shrink process is applied iteratively till there are no
10084
*         more modes to shrink
10085
*
10086
*  @param[inout]   ps_tu_enc_loop
10087
*       pointer to tu enc loop params of inter cu
10088
*
10089
*  @param[inout]   ps_tu_enc_loop_temp_prms
10090
*       pointer to temp tu enc loop params of inter cu
10091
*
10092
*  @param[in]   num_tu_in_cu
10093
*       number of tus in cu
10094
*
10095
*  @return      modified number of tus in cu
10096
*
10097
******************************************************************************
10098
*/
10099
WORD32 ihevce_shrink_inter_tu_tree(
10100
    tu_enc_loop_out_t *ps_tu_enc_loop,
10101
    tu_enc_loop_temp_prms_t *ps_tu_enc_loop_temp_prms,
10102
    recon_datastore_t *ps_recon_datastore,
10103
    WORD32 num_tu_in_cu,
10104
    UWORD8 u1_is_422)
10105
565k
{
10106
565k
    WORD32 recurse = 1;
10107
565k
    WORD32 ctr;
10108
10109
    /* ------------- Quadtree TU Split Transform flag optimization ------------  */
10110
    /* Post RDO, if all 4 child nodes are not coded the overheads of split TU    */
10111
    /* flags and cbf flags are saved by merging to parent node and marking       */
10112
    /* parent TU as not coded                                                    */
10113
    /*                                                                           */
10114
    /*                               ParentTUSplit=1                             */
10115
    /*                                      |                                    */
10116
    /*       ---------------------------------------------------------           */
10117
    /*       |C0(Not coded) | C1(Not coded) | C2(Not coded) | C3(Not coded)      */
10118
    /*                                     ||                                    */
10119
    /*                                     \/                                    */
10120
    /*                                                                           */
10121
    /*                              ParentTUSplit=0 (Not Coded)                  */
10122
    /*                                                                           */
10123
    /* ------------- Quadtree TU Split Transform flag optimization ------------  */
10124
707k
    while((num_tu_in_cu > 4) && recurse)
10125
141k
    {
10126
141k
        recurse = 0;
10127
10128
        /* Validate inter CU */
10129
        //ASSERT(ps_tu_enc_loop[0].s_tu.s_tu.b1_intra_flag == 0); /*b1_intra_flag no longer a member of tu structure */
10130
10131
        /* loop for all tu blocks in current cu */
10132
962k
        for(ctr = 0; ctr < num_tu_in_cu;)
10133
820k
        {
10134
            /* Get current tu posx, posy and size */
10135
820k
            WORD32 curr_pos_x = ps_tu_enc_loop[ctr].s_tu.b4_pos_x << 2;
10136
820k
            WORD32 curr_pos_y = ps_tu_enc_loop[ctr].s_tu.b4_pos_y << 2;
10137
            /* +1 is for parents size */
10138
820k
            WORD32 parent_tu_size = 1 << (ps_tu_enc_loop[ctr].s_tu.b3_size + 2 + 1);
10139
10140
            /* eval merge if leaf nodes reached i.e all child tus are of same size and first tu pos is same as parent pos */
10141
820k
            WORD32 eval_merge = ((curr_pos_x & (parent_tu_size - 1)) == 0);
10142
820k
            eval_merge &= ((curr_pos_y & (parent_tu_size - 1)) == 0);
10143
10144
            /* As TUs are published in encode order (Z SCAN),                      */
10145
            /* Four consecutive TUS of same size implies we have hit leaf nodes.   */
10146
820k
            if(((ps_tu_enc_loop[ctr].s_tu.b3_size) == (ps_tu_enc_loop[ctr + 1].s_tu.b3_size)) &&
10147
570k
               ((ps_tu_enc_loop[ctr].s_tu.b3_size) == (ps_tu_enc_loop[ctr + 2].s_tu.b3_size)) &&
10148
405k
               ((ps_tu_enc_loop[ctr].s_tu.b3_size) == (ps_tu_enc_loop[ctr + 3].s_tu.b3_size)) &&
10149
286k
               eval_merge)
10150
243k
            {
10151
243k
                WORD32 merge_parent = 1;
10152
10153
                /* If any leaf noded is coded, it cannot be merged to parent */
10154
243k
                if((ps_tu_enc_loop[ctr].s_tu.b1_y_cbf) || (ps_tu_enc_loop[ctr].s_tu.b1_cb_cbf) ||
10155
21.6k
                   (ps_tu_enc_loop[ctr].s_tu.b1_cr_cbf) ||
10156
10157
16.9k
                   (ps_tu_enc_loop[ctr + 1].s_tu.b1_y_cbf) ||
10158
14.5k
                   (ps_tu_enc_loop[ctr + 1].s_tu.b1_cb_cbf) ||
10159
14.4k
                   (ps_tu_enc_loop[ctr + 1].s_tu.b1_cr_cbf) ||
10160
10161
14.4k
                   (ps_tu_enc_loop[ctr + 2].s_tu.b1_y_cbf) ||
10162
10.3k
                   (ps_tu_enc_loop[ctr + 2].s_tu.b1_cb_cbf) ||
10163
10.2k
                   (ps_tu_enc_loop[ctr + 2].s_tu.b1_cr_cbf) ||
10164
10165
10.1k
                   (ps_tu_enc_loop[ctr + 3].s_tu.b1_y_cbf) ||
10166
9.24k
                   (ps_tu_enc_loop[ctr + 3].s_tu.b1_cb_cbf) ||
10167
9.22k
                   (ps_tu_enc_loop[ctr + 3].s_tu.b1_cr_cbf))
10168
234k
                {
10169
234k
                    merge_parent = 0;
10170
234k
                }
10171
10172
243k
                if(u1_is_422)
10173
0
                {
10174
0
                    if((ps_tu_enc_loop[ctr].s_tu.b1_cb_cbf_subtu1) ||
10175
0
                       (ps_tu_enc_loop[ctr].s_tu.b1_cr_cbf_subtu1) ||
10176
10177
0
                       (ps_tu_enc_loop[ctr + 1].s_tu.b1_cb_cbf_subtu1) ||
10178
0
                       (ps_tu_enc_loop[ctr + 1].s_tu.b1_cr_cbf_subtu1) ||
10179
10180
0
                       (ps_tu_enc_loop[ctr + 2].s_tu.b1_cb_cbf_subtu1) ||
10181
0
                       (ps_tu_enc_loop[ctr + 2].s_tu.b1_cr_cbf_subtu1) ||
10182
10183
0
                       (ps_tu_enc_loop[ctr + 3].s_tu.b1_cb_cbf_subtu1) ||
10184
0
                       (ps_tu_enc_loop[ctr + 3].s_tu.b1_cr_cbf_subtu1))
10185
0
                    {
10186
0
                        merge_parent = 0;
10187
0
                    }
10188
0
                }
10189
10190
243k
                if(merge_parent)
10191
9.20k
                {
10192
                    /* Merge all the children (ctr,ctr+1,ctr+2,ctr+3) to parent (ctr) */
10193
10194
9.20k
                    if(ps_recon_datastore->u1_is_lumaRecon_available)
10195
604
                    {
10196
604
                        ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr] = UCHAR_MAX;
10197
10198
604
                        memmove(
10199
604
                            &ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr + 1],
10200
604
                            &ps_recon_datastore->au1_bufId_with_winning_LumaRecon[ctr + 4],
10201
604
                            (num_tu_in_cu - ctr - 4) * sizeof(UWORD8));
10202
604
                    }
10203
10204
9.20k
                    if(ps_recon_datastore->au1_is_chromaRecon_available[0])
10205
604
                    {
10206
604
                        ps_recon_datastore->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr][0] =
10207
604
                            UCHAR_MAX;
10208
604
                        ps_recon_datastore->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr][0] =
10209
604
                            UCHAR_MAX;
10210
10211
604
                        memmove(
10212
604
                            &ps_recon_datastore
10213
604
                                 ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr + 1][0],
10214
604
                            &ps_recon_datastore
10215
604
                                 ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr + 4][0],
10216
604
                            (num_tu_in_cu - ctr - 4) * sizeof(UWORD8));
10217
10218
604
                        memmove(
10219
604
                            &ps_recon_datastore
10220
604
                                 ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr + 1][0],
10221
604
                            &ps_recon_datastore
10222
604
                                 ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr + 4][0],
10223
604
                            (num_tu_in_cu - ctr - 4) * sizeof(UWORD8));
10224
10225
604
                        if(u1_is_422)
10226
0
                        {
10227
0
                            ps_recon_datastore->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr][1] =
10228
0
                                UCHAR_MAX;
10229
0
                            ps_recon_datastore->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr][1] =
10230
0
                                UCHAR_MAX;
10231
10232
0
                            memmove(
10233
0
                                &ps_recon_datastore
10234
0
                                     ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr + 1][1],
10235
0
                                &ps_recon_datastore
10236
0
                                     ->au1_bufId_with_winning_ChromaRecon[U_PLANE][ctr + 4][1],
10237
0
                                (num_tu_in_cu - ctr - 4) * sizeof(UWORD8));
10238
10239
0
                            memmove(
10240
0
                                &ps_recon_datastore
10241
0
                                     ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr + 1][1],
10242
0
                                &ps_recon_datastore
10243
0
                                     ->au1_bufId_with_winning_ChromaRecon[V_PLANE][ctr + 4][1],
10244
0
                                (num_tu_in_cu - ctr - 4) * sizeof(UWORD8));
10245
0
                        }
10246
604
                    }
10247
10248
                    /* Parent node size is one more than that of child */
10249
9.20k
                    ps_tu_enc_loop[ctr].s_tu.b3_size++;
10250
10251
9.20k
                    ctr++;
10252
10253
                    /* move the subsequent TUs to next element */
10254
9.20k
                    ASSERT(num_tu_in_cu >= (ctr + 3));
10255
9.20k
                    memmove(
10256
9.20k
                        (void *)(ps_tu_enc_loop + ctr),
10257
9.20k
                        (void *)(ps_tu_enc_loop + ctr + 3),
10258
9.20k
                        (num_tu_in_cu - ctr - 3) * sizeof(tu_enc_loop_out_t));
10259
10260
                    /* Also memmove the temp TU params */
10261
9.20k
                    memmove(
10262
9.20k
                        (void *)(ps_tu_enc_loop_temp_prms + ctr),
10263
9.20k
                        (void *)(ps_tu_enc_loop_temp_prms + ctr + 3),
10264
9.20k
                        (num_tu_in_cu - ctr - 3) * sizeof(tu_enc_loop_temp_prms_t));
10265
10266
                    /* Number of TUs in CU are now less by 3 */
10267
9.20k
                    num_tu_in_cu -= 3;
10268
10269
                    /* Recurse again as new parent also be can be merged later */
10270
9.20k
                    recurse = 1;
10271
9.20k
                }
10272
234k
                else
10273
234k
                {
10274
                    /* Go to next set of leaf nodes */
10275
234k
                    ctr += 4;
10276
234k
                }
10277
243k
            }
10278
576k
            else
10279
576k
            {
10280
576k
                ctr++;
10281
576k
            }
10282
820k
        }
10283
141k
    }
10284
10285
    /* return the modified num TUs*/
10286
565k
    ASSERT(num_tu_in_cu > 0);
10287
565k
    return (num_tu_in_cu);
10288
565k
}
10289
10290
UWORD8 ihevce_intra_mode_nxn_hash_updater(
10291
    UWORD8 *pu1_mode_array, UWORD8 *pu1_hash_table, UWORD8 u1_num_ipe_modes)
10292
1.42M
{
10293
1.42M
    WORD32 i;
10294
1.42M
    WORD32 i4_mode;
10295
10296
5.68M
    for(i = 0; i < MAX_INTRA_CU_CANDIDATES; i++)
10297
4.26M
    {
10298
4.26M
        if(pu1_mode_array[i] < 35)
10299
4.26M
        {
10300
4.26M
            if(pu1_mode_array[i] != 0)
10301
3.44M
            {
10302
3.44M
                i4_mode = pu1_mode_array[i] - 1;
10303
10304
3.44M
                if(!pu1_hash_table[i4_mode])
10305
1.52M
                {
10306
1.52M
                    pu1_hash_table[i4_mode] = 1;
10307
1.52M
                    pu1_mode_array[u1_num_ipe_modes] = i4_mode;
10308
1.52M
                    u1_num_ipe_modes++;
10309
1.52M
                }
10310
3.44M
            }
10311
10312
4.26M
            if(pu1_mode_array[i] != 34)
10313
4.23M
            {
10314
4.23M
                i4_mode = pu1_mode_array[i] + 1;
10315
10316
4.23M
                if((!pu1_hash_table[i4_mode]))
10317
2.29M
                {
10318
2.29M
                    pu1_hash_table[i4_mode] = 1;
10319
2.29M
                    pu1_mode_array[u1_num_ipe_modes] = i4_mode;
10320
2.29M
                    u1_num_ipe_modes++;
10321
2.29M
                }
10322
4.23M
            }
10323
4.26M
        }
10324
4.26M
    }
10325
10326
1.42M
    if(!pu1_hash_table[INTRA_PLANAR])
10327
453k
    {
10328
453k
        pu1_hash_table[INTRA_PLANAR] = 1;
10329
453k
        pu1_mode_array[u1_num_ipe_modes] = INTRA_PLANAR;
10330
453k
        u1_num_ipe_modes++;
10331
453k
    }
10332
10333
1.42M
    if(!pu1_hash_table[INTRA_DC])
10334
452k
    {
10335
452k
        pu1_hash_table[INTRA_DC] = 1;
10336
452k
        pu1_mode_array[u1_num_ipe_modes] = INTRA_DC;
10337
452k
        u1_num_ipe_modes++;
10338
452k
    }
10339
10340
1.42M
    return u1_num_ipe_modes;
10341
1.42M
}
10342
10343
#if ENABLE_TU_TREE_DETERMINATION_IN_RDOPT
10344
WORD32 ihevce_determine_tu_tree_distribution(
10345
    cu_inter_cand_t *ps_cu_data,
10346
    me_func_selector_t *ps_func_selector,
10347
    WORD16 *pi2_scratch_mem,
10348
    UWORD8 *pu1_inp,
10349
    WORD32 i4_inp_stride,
10350
    WORD32 i4_lambda,
10351
    UWORD8 u1_lambda_q_shift,
10352
    UWORD8 u1_cu_size,
10353
    UWORD8 u1_max_tr_depth)
10354
{
10355
    err_prms_t s_err_prms;
10356
10357
    PF_SAD_FXN_TU_REC pf_err_compute[4];
10358
10359
    WORD32 i4_satd;
10360
10361
    s_err_prms.pi4_sad_grid = &i4_satd;
10362
    s_err_prms.pi4_tu_split_flags = ps_cu_data->ai4_tu_split_flag;
10363
    s_err_prms.pu1_inp = pu1_inp;
10364
    s_err_prms.pu1_ref = ps_cu_data->pu1_pred_data;
10365
    s_err_prms.i4_inp_stride = i4_inp_stride;
10366
    s_err_prms.i4_ref_stride = ps_cu_data->i4_pred_data_stride;
10367
    s_err_prms.pu1_wkg_mem = (UWORD8 *)pi2_scratch_mem;
10368
10369
    if(u1_cu_size == 64)
10370
    {
10371
        s_err_prms.u1_max_tr_depth = MIN(1, u1_max_tr_depth);
10372
    }
10373
    else
10374
    {
10375
        s_err_prms.u1_max_tr_depth = u1_max_tr_depth;
10376
    }
10377
10378
    pf_err_compute[CU_64x64] = hme_evalsatd_pt_pu_64x64_tu_rec;
10379
    pf_err_compute[CU_32x32] = hme_evalsatd_pt_pu_32x32_tu_rec;
10380
    pf_err_compute[CU_16x16] = hme_evalsatd_pt_pu_16x16_tu_rec;
10381
    pf_err_compute[CU_8x8] = hme_evalsatd_pt_pu_8x8_tu_rec;
10382
10383
    i4_satd = pf_err_compute[hme_get_range(u1_cu_size) - 4](
10384
        &s_err_prms, i4_lambda, u1_lambda_q_shift, 0, ps_func_selector);
10385
10386
    if((0 == u1_max_tr_depth) && (ps_cu_data->b3_part_size != 0) && (u1_cu_size != 64))
10387
    {
10388
        ps_cu_data->ai4_tu_split_flag[0] = 1;
10389
    }
10390
10391
    return i4_satd;
10392
}
10393
#endif
10394
10395
void ihevce_populate_nbr_4x4_with_pu_data(
10396
    nbr_4x4_t *ps_nbr_4x4, pu_t *ps_pu, WORD32 i4_nbr_buf_stride)
10397
739k
{
10398
739k
    WORD32 i, j;
10399
10400
739k
    nbr_4x4_t *ps_tmp_4x4 = ps_nbr_4x4;
10401
10402
739k
    WORD32 ht = (ps_pu->b4_ht + 1);
10403
739k
    WORD32 wd = (ps_pu->b4_wd + 1);
10404
10405
739k
    ps_nbr_4x4->b1_intra_flag = 0;
10406
739k
    ps_nbr_4x4->b1_pred_l0_flag = !(ps_pu->b2_pred_mode & 1);
10407
739k
    ps_nbr_4x4->b1_pred_l1_flag = (ps_pu->b2_pred_mode > PRED_L0);
10408
739k
    ps_nbr_4x4->mv = ps_pu->mv;
10409
10410
2.27M
    for(i = 0; i < ht; i++)
10411
1.54M
    {
10412
8.90M
        for(j = 0; j < wd; j++)
10413
7.36M
        {
10414
7.36M
            ps_tmp_4x4[j] = *ps_nbr_4x4;
10415
7.36M
        }
10416
10417
1.54M
        ps_tmp_4x4 += i4_nbr_buf_stride;
10418
1.54M
    }
10419
739k
}
10420
10421
void ihevce_call_luma_inter_pred_rdopt_pass1(
10422
    ihevce_enc_loop_ctxt_t *ps_ctxt, cu_inter_cand_t *ps_inter_cand, WORD32 cu_size)
10423
0
{
10424
0
    pu_t *ps_pu;
10425
0
    UWORD8 *pu1_pred;
10426
0
    WORD32 pred_stride, ctr, num_cu_part, skip_or_merge_flag = 0;
10427
0
    WORD32 inter_pu_wd, inter_pu_ht;
10428
10429
0
    pu1_pred = ps_inter_cand->pu1_pred_data_scr;
10430
0
    pred_stride = ps_inter_cand->i4_pred_data_stride;
10431
0
    num_cu_part = (SIZE_2Nx2N != ps_inter_cand->b3_part_size) + 1;
10432
10433
0
    for(ctr = 0; ctr < num_cu_part; ctr++)
10434
0
    {
10435
0
        ps_pu = &ps_inter_cand->as_inter_pu[ctr];
10436
10437
        /* IF AMP then each partitions can have diff wd ht */
10438
0
        inter_pu_wd = (ps_pu->b4_wd + 1) << 2;
10439
0
        inter_pu_ht = (ps_pu->b4_ht + 1) << 2;
10440
10441
0
        skip_or_merge_flag = ps_inter_cand->b1_skip_flag | ps_pu->b1_merge_flag;
10442
        //if(0 == skip_or_merge_flag)
10443
0
        {
10444
0
            ihevce_luma_inter_pred_pu(&ps_ctxt->s_mc_ctxt, ps_pu, pu1_pred, pred_stride, 1);
10445
0
        }
10446
0
        if((2 == num_cu_part) && (0 == ctr))
10447
0
        {
10448
            /* 2Nx__ partion case */
10449
0
            if(inter_pu_wd == cu_size)
10450
0
            {
10451
0
                pu1_pred += (inter_pu_ht * pred_stride);
10452
0
            }
10453
10454
            /* __x2N partion case */
10455
0
            if(inter_pu_ht == cu_size)
10456
0
            {
10457
0
                pu1_pred += inter_pu_wd;
10458
0
            }
10459
0
        }
10460
0
    }
10461
0
}
10462
10463
LWORD64 ihevce_it_recon_ssd(
10464
    ihevce_enc_loop_ctxt_t *ps_ctxt,
10465
    UWORD8 *pu1_src,
10466
    WORD32 i4_src_strd,
10467
    UWORD8 *pu1_pred,
10468
    WORD32 i4_pred_strd,
10469
    WORD16 *pi2_deq_data,
10470
    WORD32 i4_deq_data_strd,
10471
    UWORD8 *pu1_recon,
10472
    WORD32 i4_recon_stride,
10473
    UWORD8 *pu1_ecd_data,
10474
    UWORD8 u1_trans_size,
10475
    UWORD8 u1_pred_mode,
10476
    WORD32 i4_cbf,
10477
    WORD32 i4_zero_col,
10478
    WORD32 i4_zero_row,
10479
    CHROMA_PLANE_ID_T e_chroma_plane)
10480
33.0M
{
10481
33.0M
    if(NULL_PLANE == e_chroma_plane)
10482
14.9M
    {
10483
14.9M
        ihevce_it_recon_fxn(
10484
14.9M
            ps_ctxt,
10485
14.9M
            pi2_deq_data,
10486
14.9M
            i4_deq_data_strd,
10487
14.9M
            pu1_pred,
10488
14.9M
            i4_pred_strd,
10489
14.9M
            pu1_recon,
10490
14.9M
            i4_recon_stride,
10491
14.9M
            pu1_ecd_data,
10492
14.9M
            u1_trans_size,
10493
14.9M
            u1_pred_mode,
10494
14.9M
            i4_cbf,
10495
14.9M
            i4_zero_col,
10496
14.9M
            i4_zero_row);
10497
10498
14.9M
        return ps_ctxt->s_cmn_opt_func.pf_ssd_calculator(
10499
14.9M
            pu1_recon, pu1_src, i4_recon_stride, i4_src_strd, u1_trans_size, u1_trans_size,
10500
14.9M
            e_chroma_plane);
10501
14.9M
    }
10502
18.0M
    else
10503
18.0M
    {
10504
18.0M
        ihevce_chroma_it_recon_fxn(
10505
18.0M
            ps_ctxt,
10506
18.0M
            pi2_deq_data,
10507
18.0M
            i4_deq_data_strd,
10508
18.0M
            pu1_pred,
10509
18.0M
            i4_pred_strd,
10510
18.0M
            pu1_recon,
10511
18.0M
            i4_recon_stride,
10512
18.0M
            pu1_ecd_data,
10513
18.0M
            u1_trans_size,
10514
18.0M
            i4_cbf,
10515
18.0M
            i4_zero_col,
10516
18.0M
            i4_zero_row,
10517
18.0M
            e_chroma_plane);
10518
10519
18.0M
        return ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_ssd_calculator(
10520
18.0M
            pu1_recon,
10521
18.0M
            pu1_src,
10522
18.0M
            i4_recon_stride,
10523
18.0M
            i4_src_strd,
10524
18.0M
            u1_trans_size,
10525
18.0M
            u1_trans_size,
10526
18.0M
            e_chroma_plane);
10527
18.0M
    }
10528
33.0M
}
10529
10530
/*!
10531
******************************************************************************
10532
* \if Function name : ihevce_t_q_iq_ssd_scan_fxn \endif
10533
*
10534
* \brief
10535
*    Transform unit level (Chroma) enc_loop function
10536
*
10537
* \param[in] ps_ctxt    enc_loop module ctxt pointer
10538
* \param[in] pu1_pred       pointer to predicted data buffer
10539
* \param[in] pred_strd      predicted buffer stride
10540
* \param[in] pu1_src    pointer to source data buffer
10541
* \param[in] src_strd   source buffer stride
10542
* \param[in] pi2_deq_data   pointer to store iq data
10543
* \param[in] deq_data_strd  iq data buffer stride
10544
* \param[out] pu1_ecd_data  pointer coeff output buffer (input to ent cod)
10545
* \param[out] pu1_csbf_buf  pointer to store the csbf for all 4x4 in a current
10546
*                           block
10547
* \param[out] csbf_strd     csbf buffer stride
10548
* \param[in] trans_size     transform size (4, 8, 16)
10549
* \param[in] intra_flag     0:Inter/Skip 1:Intra
10550
* \param[out] pi4_coeff_off pointer to store the number of bytes produced in
10551
*                           coeff buffer
10552
the current TU in RDopt Mode
10553
* \param[out] pi4_zero_col  pointer to store the zero_col info for the TU
10554
* \param[out] pi4_zero_row  pointer to store the zero_row info for the TU
10555
*
10556
* \return
10557
*    CBF of the current block
10558
*
10559
* \author
10560
*  Ittiam
10561
*
10562
*****************************************************************************
10563
*/
10564
WORD32 ihevce_chroma_t_q_iq_ssd_scan_fxn(
10565
    ihevce_enc_loop_ctxt_t *ps_ctxt,
10566
    UWORD8 *pu1_pred,
10567
    WORD32 pred_strd,
10568
    UWORD8 *pu1_src,
10569
    WORD32 src_strd,
10570
    WORD16 *pi2_deq_data,
10571
    WORD32 deq_data_strd,
10572
    UWORD8 *pu1_recon,
10573
    WORD32 i4_recon_stride,
10574
    UWORD8 *pu1_ecd_data,
10575
    UWORD8 *pu1_csbf_buf,
10576
    WORD32 csbf_strd,
10577
    WORD32 trans_size,
10578
    WORD32 i4_scan_idx,
10579
    WORD32 intra_flag,
10580
    WORD32 *pi4_coeff_off,
10581
    WORD32 *pi4_tu_bits,
10582
    WORD32 *pi4_zero_col,
10583
    WORD32 *pi4_zero_row,
10584
    UWORD8 *pu1_is_recon_available,
10585
    WORD32 i4_perform_sbh,
10586
    WORD32 i4_perform_rdoq,
10587
    LWORD64 *pi8_cost,
10588
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
10589
    WORD32 i4_alpha_stim_multiplier,
10590
    UWORD8 u1_is_cu_noisy,
10591
#endif
10592
    UWORD8 u1_is_skip,
10593
    SSD_TYPE_T e_ssd_type,
10594
    CHROMA_PLANE_ID_T e_chroma_plane)
10595
33.8M
{
10596
33.8M
    WORD32 trans_idx, cbf, u4_blk_sad;
10597
33.8M
    WORD16 *pi2_quant_coeffs;
10598
33.8M
    WORD16 *pi2_trans_values;
10599
33.8M
    WORD32 quant_scale_mat_offset;
10600
33.8M
    WORD32 *pi4_trans_scratch;
10601
33.8M
    WORD32 *pi4_subBlock2csbfId_map = NULL;
10602
10603
#if PROHIBIT_INTRA_QUANT_ROUNDING_FACTOR_TO_DROP_BELOW_1BY3
10604
    WORD32 ai4_quant_rounding_factors[3][MAX_TU_SIZE * MAX_TU_SIZE], i;
10605
#endif
10606
10607
33.8M
    rdoq_sbh_ctxt_t *ps_rdoq_sbh_ctxt = &ps_ctxt->s_rdoq_sbh_ctxt;
10608
10609
33.8M
    WORD32 i4_perform_zcbf = (ps_ctxt->i4_zcbf_rdo_level == ZCBF_ENABLE) ||
10610
24.7M
                             (!intra_flag && ENABLE_INTER_ZCU_COST);
10611
33.8M
    WORD32 i4_perform_coeff_level_rdoq =
10612
33.8M
        (ps_ctxt->i4_quant_rounding_level != FIXED_QUANT_ROUNDING) &&
10613
28.4M
        (ps_ctxt->i4_chroma_quant_rounding_level == CHROMA_QUANT_ROUNDING);
10614
10615
33.8M
    ASSERT((e_chroma_plane == U_PLANE) || (e_chroma_plane == V_PLANE));
10616
33.8M
    ASSERT(csbf_strd == MAX_TU_IN_CTB_ROW);
10617
10618
33.8M
    *pi4_coeff_off = 0;
10619
33.8M
    *pi4_tu_bits = 0;
10620
33.8M
    pu1_is_recon_available[0] = 0;
10621
10622
33.8M
    pi4_trans_scratch = (WORD32 *)&ps_ctxt->ai2_scratch[0];
10623
33.8M
    pi2_quant_coeffs = &ps_ctxt->ai2_scratch[0];
10624
33.8M
    pi2_trans_values = &ps_ctxt->ai2_scratch[0] + (MAX_TRANS_SIZE * 2);
10625
10626
33.8M
    if(2 == trans_size)
10627
0
    {
10628
0
        trans_size = 4;
10629
0
    }
10630
10631
    /* translate the transform size to index */
10632
33.8M
    trans_idx = trans_size >> 2;
10633
10634
33.8M
    if(16 == trans_size)
10635
4.47M
    {
10636
4.47M
        trans_idx = 3;
10637
4.47M
    }
10638
10639
33.8M
    if(u1_is_skip)
10640
0
    {
10641
0
        pi8_cost[0] = ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_ssd_calculator(
10642
0
            pu1_pred,
10643
0
            pu1_src,
10644
0
            pred_strd,
10645
0
            src_strd,
10646
0
            trans_size,
10647
0
            trans_size,
10648
0
            e_chroma_plane);
10649
10650
0
        if(e_ssd_type == SPATIAL_DOMAIN_SSD)
10651
0
        {
10652
            /* buffer copy fromp pred to recon */
10653
0
            ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
10654
0
                pu1_pred,
10655
0
                pred_strd,
10656
0
                pu1_recon,
10657
0
                i4_recon_stride,
10658
0
                trans_size,
10659
0
                trans_size,
10660
0
                e_chroma_plane);
10661
10662
0
            pu1_is_recon_available[0] = 1;
10663
0
        }
10664
10665
0
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
10666
0
        if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
10667
0
        {
10668
0
            pi8_cost[0] = ihevce_inject_stim_into_distortion(
10669
0
                pu1_src,
10670
0
                src_strd,
10671
0
                pu1_pred,
10672
0
                pred_strd,
10673
0
                pi8_cost[0],
10674
0
                i4_alpha_stim_multiplier,
10675
0
                trans_size,
10676
0
                0,
10677
0
                ps_ctxt->u1_enable_psyRDOPT,
10678
0
                e_chroma_plane);
10679
0
        }
10680
0
#endif
10681
10682
0
#if ENABLE_INTER_ZCU_COST
10683
#if !WEIGH_CHROMA_COST
10684
        /* cbf = 0, accumulate cu not coded cost */
10685
        ps_ctxt->i8_cu_not_coded_cost += pi8_cost[0];
10686
#else
10687
0
        ps_ctxt->i8_cu_not_coded_cost += (pi8_cost[0] * ps_ctxt->u4_chroma_cost_weighing_factor +
10688
0
                                          (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
10689
0
                                         CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT;
10690
0
#endif
10691
0
#endif
10692
10693
0
        return 0;
10694
0
    }
10695
10696
33.8M
    if(intra_flag == 1)
10697
24.7M
    {
10698
24.7M
        quant_scale_mat_offset = 0;
10699
10700
#if PROHIBIT_INTRA_QUANT_ROUNDING_FACTOR_TO_DROP_BELOW_1BY3
10701
        ai4_quant_rounding_factors[0][0] =
10702
            MAX(ps_ctxt->i4_quant_rnd_factor[intra_flag], (1 << QUANT_ROUND_FACTOR_Q) / 3);
10703
10704
        for(i = 0; i < trans_size * trans_size; i++)
10705
        {
10706
            ai4_quant_rounding_factors[1][i] =
10707
                MAX(ps_ctxt->pi4_quant_round_factor_cr_cu_ctb_0_1[trans_size >> 3][i],
10708
                    (1 << QUANT_ROUND_FACTOR_Q) / 3);
10709
            ai4_quant_rounding_factors[2][i] =
10710
                MAX(ps_ctxt->pi4_quant_round_factor_cr_cu_ctb_1_2[trans_size >> 3][i],
10711
                    (1 << QUANT_ROUND_FACTOR_Q) / 3);
10712
        }
10713
#endif
10714
24.7M
    }
10715
9.12M
    else
10716
9.12M
    {
10717
9.12M
        quant_scale_mat_offset = NUM_TRANS_TYPES;
10718
9.12M
    }
10719
10720
33.8M
    switch(trans_size)
10721
33.8M
    {
10722
17.7M
    case 4:
10723
17.7M
    {
10724
17.7M
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map4x4TU;
10725
10726
17.7M
        break;
10727
0
    }
10728
11.6M
    case 8:
10729
11.6M
    {
10730
11.6M
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map8x8TU;
10731
10732
11.6M
        break;
10733
0
    }
10734
4.47M
    case 16:
10735
4.47M
    {
10736
4.47M
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map16x16TU;
10737
10738
4.47M
        break;
10739
0
    }
10740
0
    case 32:
10741
0
    {
10742
0
        pi4_subBlock2csbfId_map = gai4_subBlock2csbfId_map32x32TU;
10743
10744
0
        break;
10745
0
    }
10746
33.8M
    }
10747
10748
    /* ---------- call residue and transform block ------- */
10749
33.8M
    u4_blk_sad = ps_ctxt->apf_chrm_resd_trns[trans_idx - 1](
10750
33.8M
        pu1_src,
10751
33.8M
        pu1_pred,
10752
33.8M
        pi4_trans_scratch,
10753
33.8M
        pi2_trans_values,
10754
33.8M
        src_strd,
10755
33.8M
        pred_strd,
10756
33.8M
        trans_size,
10757
33.8M
        e_chroma_plane);
10758
33.8M
    (void)u4_blk_sad;
10759
    /* -------- calculate SSD calculation in Transform Domain ------ */
10760
10761
33.8M
    cbf = ps_ctxt->apf_quant_iquant_ssd
10762
33.8M
              [i4_perform_coeff_level_rdoq + (e_ssd_type != FREQUENCY_DOMAIN_SSD) * 2]
10763
10764
33.8M
          (pi2_trans_values,
10765
33.8M
           ps_ctxt->api2_rescal_mat[trans_idx + quant_scale_mat_offset],
10766
33.8M
           pi2_quant_coeffs,
10767
33.8M
           pi2_deq_data,
10768
33.8M
           trans_size,
10769
33.8M
           ps_ctxt->i4_chrm_cu_qp_div6,
10770
33.8M
           ps_ctxt->i4_chrm_cu_qp_mod6,
10771
33.8M
#if !PROHIBIT_INTRA_QUANT_ROUNDING_FACTOR_TO_DROP_BELOW_1BY3
10772
33.8M
           ps_ctxt->i4_quant_rnd_factor[intra_flag],
10773
33.8M
           ps_ctxt->pi4_quant_round_factor_cr_cu_ctb_0_1[trans_size >> 3],
10774
33.8M
           ps_ctxt->pi4_quant_round_factor_cr_cu_ctb_1_2[trans_size >> 3],
10775
#else
10776
           intra_flag ? ai4_quant_rounding_factors[0][0] : ps_ctxt->i4_quant_rnd_factor[intra_flag],
10777
           intra_flag ? ai4_quant_rounding_factors[1]
10778
                      : ps_ctxt->pi4_quant_round_factor_cr_cu_ctb_0_1[trans_size >> 3],
10779
           intra_flag ? ai4_quant_rounding_factors[2]
10780
                      : ps_ctxt->pi4_quant_round_factor_cr_cu_ctb_1_2[trans_size >> 3],
10781
#endif
10782
33.8M
           trans_size,
10783
33.8M
           trans_size,
10784
33.8M
           deq_data_strd,
10785
33.8M
           pu1_csbf_buf,
10786
33.8M
           csbf_strd,
10787
33.8M
           pi4_zero_col,
10788
33.8M
           pi4_zero_row,
10789
33.8M
           ps_ctxt->api2_scal_mat[trans_idx + quant_scale_mat_offset],
10790
33.8M
           pi8_cost);
10791
10792
33.8M
    if(e_ssd_type != FREQUENCY_DOMAIN_SSD)
10793
18.0M
    {
10794
18.0M
        pi8_cost[0] = UINT_MAX;
10795
18.0M
    }
10796
10797
33.8M
    if(0 != cbf)
10798
5.35M
    {
10799
5.35M
        if(i4_perform_sbh || i4_perform_rdoq)
10800
4.14M
        {
10801
4.14M
            ps_rdoq_sbh_ctxt->i4_iq_data_strd = deq_data_strd;
10802
4.14M
            ps_rdoq_sbh_ctxt->i4_q_data_strd = trans_size;
10803
10804
4.14M
            ps_rdoq_sbh_ctxt->i4_qp_div = ps_ctxt->i4_chrm_cu_qp_div6;
10805
4.14M
            ps_rdoq_sbh_ctxt->i2_qp_rem = ps_ctxt->i4_chrm_cu_qp_mod6;
10806
4.14M
            ps_rdoq_sbh_ctxt->i4_scan_idx = i4_scan_idx;
10807
4.14M
            ps_rdoq_sbh_ctxt->i8_ssd_cost = *pi8_cost;
10808
4.14M
            ps_rdoq_sbh_ctxt->i4_trans_size = trans_size;
10809
10810
4.14M
            ps_rdoq_sbh_ctxt->pi2_dequant_coeff =
10811
4.14M
                ps_ctxt->api2_scal_mat[trans_idx + quant_scale_mat_offset];
10812
4.14M
            ps_rdoq_sbh_ctxt->pi2_iquant_coeffs = pi2_deq_data;
10813
4.14M
            ps_rdoq_sbh_ctxt->pi2_quant_coeffs = pi2_quant_coeffs;
10814
4.14M
            ps_rdoq_sbh_ctxt->pi2_trans_values = pi2_trans_values;
10815
4.14M
            ps_rdoq_sbh_ctxt->pu1_csbf_buf = pu1_csbf_buf;
10816
4.14M
            ps_rdoq_sbh_ctxt->pi4_subBlock2csbfId_map = pi4_subBlock2csbfId_map;
10817
10818
4.14M
            if((!i4_perform_rdoq))
10819
2.24M
            {
10820
2.24M
                ihevce_sign_data_hiding(ps_rdoq_sbh_ctxt);
10821
10822
2.24M
                pi8_cost[0] = ps_rdoq_sbh_ctxt->i8_ssd_cost;
10823
2.24M
            }
10824
4.14M
        }
10825
10826
        /* ------- call coeffs scan function ------- */
10827
5.35M
        *pi4_coeff_off = ps_ctxt->s_cmn_opt_func.pf_scan_coeffs(
10828
5.35M
            pi2_quant_coeffs,
10829
5.35M
            pi4_subBlock2csbfId_map,
10830
5.35M
            i4_scan_idx,
10831
5.35M
            trans_size,
10832
5.35M
            pu1_ecd_data,
10833
5.35M
            pu1_csbf_buf,
10834
5.35M
            csbf_strd);
10835
5.35M
    }
10836
10837
    /*  Normalize Cost. Note : trans_idx, not (trans_idx-1) */
10838
33.8M
    pi8_cost[0] >>= ga_trans_shift[trans_idx];
10839
10840
33.8M
#if RDOPT_ZERO_CBF_ENABLE
10841
33.8M
    if((0 != cbf))
10842
5.35M
    {
10843
5.35M
        WORD32 tu_bits;
10844
5.35M
        LWORD64 zero_cbf_cost_u, curr_cb_cod_cost;
10845
10846
5.35M
        zero_cbf_cost_u = 0;
10847
10848
        /*Populating the feilds of rdoq_ctxt structure*/
10849
5.35M
        if(i4_perform_rdoq)
10850
1.90M
        {
10851
            //memset(ps_rdoq_sbh_ctxt,0,sizeof(rdoq_sbh_ctxt_t));
10852
            /* transform size to log2transform size */
10853
1.90M
            GETRANGE(ps_rdoq_sbh_ctxt->i4_log2_trans_size, trans_size);
10854
1.90M
            ps_rdoq_sbh_ctxt->i4_log2_trans_size -= 1;
10855
10856
1.90M
            ps_rdoq_sbh_ctxt->i8_cl_ssd_lambda_qf = ps_ctxt->i8_cl_ssd_lambda_chroma_qf;
10857
1.90M
            ps_rdoq_sbh_ctxt->i4_is_luma = 0;
10858
1.90M
            ps_rdoq_sbh_ctxt->i4_shift_val_ssd_in_td = ga_trans_shift[trans_idx];
10859
1.90M
            ps_rdoq_sbh_ctxt->i4_round_val_ssd_in_td =
10860
1.90M
                (1 << (ps_rdoq_sbh_ctxt->i4_shift_val_ssd_in_td - 1));
10861
1.90M
            ps_rdoq_sbh_ctxt->i1_tu_is_coded = 0;
10862
1.90M
            ps_rdoq_sbh_ctxt->pi4_zero_col = pi4_zero_col;
10863
1.90M
            ps_rdoq_sbh_ctxt->pi4_zero_row = pi4_zero_row;
10864
1.90M
        }
10865
3.45M
        else if(i4_perform_zcbf)
10866
1.11M
        {
10867
            /* cost of zero cbf encoding */
10868
1.11M
            zero_cbf_cost_u =
10869
10870
1.11M
                ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_ssd_calculator(
10871
1.11M
                    pu1_pred,
10872
1.11M
                    pu1_src,
10873
1.11M
                    pred_strd,
10874
1.11M
                    src_strd,
10875
1.11M
                    trans_size,
10876
1.11M
                    trans_size,
10877
1.11M
                    e_chroma_plane);
10878
1.11M
        }
10879
10880
        /************************************************************************/
10881
        /* call the entropy rdo encode to get the bit estimate for current tu   */
10882
        /* note that tu includes only residual coding bits and does not include */
10883
        /* tu split, cbf and qp delta encoding bits for a TU                    */
10884
        /************************************************************************/
10885
5.35M
        if(i4_perform_rdoq)
10886
1.90M
        {
10887
1.90M
            tu_bits = ihevce_entropy_rdo_encode_tu_rdoq(
10888
1.90M
                &ps_ctxt->s_rdopt_entropy_ctxt,
10889
1.90M
                pu1_ecd_data,
10890
1.90M
                trans_size,
10891
1.90M
                0,
10892
1.90M
                ps_rdoq_sbh_ctxt,
10893
1.90M
                pi8_cost,
10894
1.90M
                &zero_cbf_cost_u,
10895
1.90M
                0);
10896
            //Currently, we are not accounting for sign bit in RDOPT bits calculation when RDOQ is turned on
10897
10898
1.90M
            if(ps_rdoq_sbh_ctxt->i1_tu_is_coded == 0)
10899
83.3k
            {
10900
83.3k
                cbf = 0;
10901
10902
                /* num bytes is set to 0 */
10903
83.3k
                *pi4_coeff_off = 0;
10904
83.3k
            }
10905
10906
1.90M
            (*pi4_tu_bits) += tu_bits;
10907
10908
1.90M
            if((i4_perform_sbh) && (0 != cbf))
10909
1.81M
            {
10910
1.81M
                ps_rdoq_sbh_ctxt->i8_ssd_cost = pi8_cost[0];
10911
10912
1.81M
                ihevce_sign_data_hiding(ps_rdoq_sbh_ctxt);
10913
10914
1.81M
                pi8_cost[0] = ps_rdoq_sbh_ctxt->i8_ssd_cost;
10915
1.81M
            }
10916
10917
            /*Add round value before normalizing*/
10918
1.90M
            pi8_cost[0] += ps_rdoq_sbh_ctxt->i4_round_val_ssd_in_td;
10919
1.90M
            pi8_cost[0] >>= ga_trans_shift[trans_idx];
10920
10921
1.90M
            if(ps_rdoq_sbh_ctxt->i1_tu_is_coded == 1)
10922
1.81M
            {
10923
1.81M
                *pi4_coeff_off = ps_ctxt->s_cmn_opt_func.pf_scan_coeffs(
10924
1.81M
                    pi2_quant_coeffs,
10925
1.81M
                    pi4_subBlock2csbfId_map,
10926
1.81M
                    i4_scan_idx,
10927
1.81M
                    trans_size,
10928
1.81M
                    pu1_ecd_data,
10929
1.81M
                    ps_rdoq_sbh_ctxt->pu1_csbf_buf,
10930
1.81M
                    csbf_strd);
10931
1.81M
            }
10932
1.90M
        }
10933
3.45M
        else
10934
3.45M
        {
10935
            /************************************************************************/
10936
            /* call the entropy rdo encode to get the bit estimate for current tu   */
10937
            /* note that tu includes only residual coding bits and does not include */
10938
            /* tu split, cbf and qp delta encoding bits for a TU                    */
10939
            /************************************************************************/
10940
3.45M
            tu_bits = ihevce_entropy_rdo_encode_tu(
10941
3.45M
                &ps_ctxt->s_rdopt_entropy_ctxt, pu1_ecd_data, trans_size, 0, i4_perform_sbh);
10942
10943
3.45M
            (*pi4_tu_bits) += tu_bits;
10944
3.45M
        }
10945
10946
5.35M
        if(e_ssd_type == SPATIAL_DOMAIN_SSD)
10947
1.83M
        {
10948
1.83M
            pi8_cost[0] = ihevce_it_recon_ssd(
10949
1.83M
                ps_ctxt,
10950
1.83M
                pu1_src,
10951
1.83M
                src_strd,
10952
1.83M
                pu1_pred,
10953
1.83M
                pred_strd,
10954
1.83M
                pi2_deq_data,
10955
1.83M
                deq_data_strd,
10956
1.83M
                pu1_recon,
10957
1.83M
                i4_recon_stride,
10958
1.83M
                pu1_ecd_data,
10959
1.83M
                trans_size,
10960
1.83M
                PRED_MODE_INTRA,
10961
1.83M
                cbf,
10962
1.83M
                pi4_zero_col[0],
10963
1.83M
                pi4_zero_row[0],
10964
1.83M
                e_chroma_plane);
10965
10966
1.83M
            pu1_is_recon_available[0] = 1;
10967
1.83M
        }
10968
10969
5.35M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
10970
5.35M
        if(u1_is_cu_noisy && (e_ssd_type == SPATIAL_DOMAIN_SSD) && i4_alpha_stim_multiplier)
10971
0
        {
10972
0
            pi8_cost[0] = ihevce_inject_stim_into_distortion(
10973
0
                pu1_src,
10974
0
                src_strd,
10975
0
                pu1_recon,
10976
0
                i4_recon_stride,
10977
0
                pi8_cost[0],
10978
0
                i4_alpha_stim_multiplier,
10979
0
                trans_size,
10980
0
                0,
10981
0
                ps_ctxt->u1_enable_psyRDOPT,
10982
0
                e_chroma_plane);
10983
0
        }
10984
5.35M
        else if(u1_is_cu_noisy && (e_ssd_type == FREQUENCY_DOMAIN_SSD) && i4_alpha_stim_multiplier)
10985
0
        {
10986
0
            pi8_cost[0] = ihevce_inject_stim_into_distortion(
10987
0
                pu1_src,
10988
0
                src_strd,
10989
0
                pu1_pred,
10990
0
                pred_strd,
10991
0
                pi8_cost[0],
10992
0
                i4_alpha_stim_multiplier,
10993
0
                trans_size,
10994
0
                0,
10995
0
                ps_ctxt->u1_enable_psyRDOPT,
10996
0
                e_chroma_plane);
10997
0
        }
10998
5.35M
#endif
10999
11000
5.35M
        curr_cb_cod_cost = pi8_cost[0];
11001
11002
        /* add the SSD cost to bits estimate given by ECD */
11003
5.35M
        curr_cb_cod_cost +=
11004
5.35M
            COMPUTE_RATE_COST_CLIP30(tu_bits, ps_ctxt->i8_cl_ssd_lambda_chroma_qf, LAMBDA_Q_SHIFT);
11005
11006
5.35M
        if(i4_perform_zcbf)
11007
1.83M
        {
11008
1.83M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
11009
1.83M
            if(u1_is_cu_noisy && i4_alpha_stim_multiplier)
11010
0
            {
11011
0
                zero_cbf_cost_u = ihevce_inject_stim_into_distortion(
11012
0
                    pu1_src,
11013
0
                    src_strd,
11014
0
                    pu1_pred,
11015
0
                    pred_strd,
11016
0
                    zero_cbf_cost_u,
11017
0
                    !ps_ctxt->u1_is_refPic ? ALPHA_FOR_ZERO_CODING_DECISIONS
11018
0
                                           : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
11019
0
                                              (double)ALPHA_FOR_ZERO_CODING_DECISIONS) /
11020
0
                                                 100.0,
11021
0
                    trans_size,
11022
0
                    0,
11023
0
                    ps_ctxt->u1_enable_psyRDOPT,
11024
0
                    e_chroma_plane);
11025
0
            }
11026
1.83M
#endif
11027
            /* force the tu as zero cbf if zero_cbf_cost is lower */
11028
1.83M
            if(zero_cbf_cost_u < curr_cb_cod_cost)
11029
28.3k
            {
11030
28.3k
                *pi4_coeff_off = 0;
11031
28.3k
                cbf = 0;
11032
28.3k
                (*pi4_tu_bits) = 0;
11033
28.3k
                pi8_cost[0] = zero_cbf_cost_u;
11034
11035
28.3k
                pu1_is_recon_available[0] = 0;
11036
11037
28.3k
                if(e_ssd_type == SPATIAL_DOMAIN_SSD)
11038
12.5k
                {
11039
12.5k
                    ps_ctxt->s_cmn_opt_func.pf_chroma_interleave_2d_copy(
11040
12.5k
                        pu1_pred,
11041
12.5k
                        pred_strd,
11042
12.5k
                        pu1_recon,
11043
12.5k
                        i4_recon_stride,
11044
12.5k
                        trans_size,
11045
12.5k
                        trans_size,
11046
12.5k
                        e_chroma_plane);
11047
11048
12.5k
                    pu1_is_recon_available[0] = 1;
11049
12.5k
                }
11050
28.3k
            }
11051
11052
1.83M
#if ENABLE_INTER_ZCU_COST
11053
1.83M
            if(!intra_flag)
11054
1.83M
            {
11055
#if !WEIGH_CHROMA_COST
11056
                ps_ctxt->i8_cu_not_coded_cost += zero_cbf_cost_u;
11057
#else
11058
1.83M
                ps_ctxt->i8_cu_not_coded_cost += (LWORD64)(
11059
1.83M
                    (zero_cbf_cost_u * ps_ctxt->u4_chroma_cost_weighing_factor +
11060
1.83M
                     (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
11061
1.83M
                    CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
11062
1.83M
#endif
11063
1.83M
            }
11064
1.83M
#endif
11065
1.83M
        }
11066
5.35M
    }
11067
28.4M
    else
11068
28.4M
    {
11069
28.4M
        if(e_ssd_type == SPATIAL_DOMAIN_SSD)
11070
16.2M
        {
11071
16.2M
            pi8_cost[0] = ihevce_it_recon_ssd(
11072
16.2M
                ps_ctxt,
11073
16.2M
                pu1_src,
11074
16.2M
                src_strd,
11075
16.2M
                pu1_pred,
11076
16.2M
                pred_strd,
11077
16.2M
                pi2_deq_data,
11078
16.2M
                deq_data_strd,
11079
16.2M
                pu1_recon,
11080
16.2M
                i4_recon_stride,
11081
16.2M
                pu1_ecd_data,
11082
16.2M
                trans_size,
11083
16.2M
                PRED_MODE_INTRA,
11084
16.2M
                cbf,
11085
16.2M
                pi4_zero_col[0],
11086
16.2M
                pi4_zero_row[0],
11087
16.2M
                e_chroma_plane);
11088
11089
16.2M
            pu1_is_recon_available[0] = 1;
11090
16.2M
        }
11091
11092
28.4M
#if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
11093
28.4M
        if(u1_is_cu_noisy && (e_ssd_type == SPATIAL_DOMAIN_SSD) && i4_alpha_stim_multiplier)
11094
0
        {
11095
0
            pi8_cost[0] = ihevce_inject_stim_into_distortion(
11096
0
                pu1_src,
11097
0
                src_strd,
11098
0
                pu1_recon,
11099
0
                i4_recon_stride,
11100
0
                pi8_cost[0],
11101
0
                !ps_ctxt->u1_is_refPic ? ALPHA_FOR_ZERO_CODING_DECISIONS
11102
0
                                       : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
11103
0
                                          (double)ALPHA_FOR_ZERO_CODING_DECISIONS) /
11104
0
                                             100.0,
11105
0
                trans_size,
11106
0
                0,
11107
0
                ps_ctxt->u1_enable_psyRDOPT,
11108
0
                e_chroma_plane);
11109
0
        }
11110
28.4M
        else if(u1_is_cu_noisy && (e_ssd_type == FREQUENCY_DOMAIN_SSD) && i4_alpha_stim_multiplier)
11111
0
        {
11112
0
            pi8_cost[0] = ihevce_inject_stim_into_distortion(
11113
0
                pu1_src,
11114
0
                src_strd,
11115
0
                pu1_pred,
11116
0
                pred_strd,
11117
0
                pi8_cost[0],
11118
0
                !ps_ctxt->u1_is_refPic ? ALPHA_FOR_ZERO_CODING_DECISIONS
11119
0
                                       : ((100 - ALPHA_DISCOUNT_IN_REF_PICS_IN_RDOPT) *
11120
0
                                          (double)ALPHA_FOR_ZERO_CODING_DECISIONS) /
11121
0
                                             100.0,
11122
0
                trans_size,
11123
0
                0,
11124
0
                ps_ctxt->u1_enable_psyRDOPT,
11125
0
                e_chroma_plane);
11126
0
        }
11127
28.4M
#endif
11128
11129
28.4M
#if ENABLE_INTER_ZCU_COST
11130
28.4M
        if(!intra_flag)
11131
7.29M
        {
11132
#if !WEIGH_CHROMA_COST
11133
            /* cbf = 0, accumulate cu not coded cost */
11134
            ps_ctxt->i8_cu_not_coded_cost += pi8_cost[0];
11135
#else
11136
            /* cbf = 0, accumulate cu not coded cost */
11137
11138
7.29M
            ps_ctxt->i8_cu_not_coded_cost += (LWORD64)(
11139
7.29M
                (pi8_cost[0] * ps_ctxt->u4_chroma_cost_weighing_factor +
11140
7.29M
                 (1 << (CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT - 1))) >>
11141
7.29M
                CHROMA_COST_WEIGHING_FACTOR_Q_SHIFT);
11142
7.29M
#endif
11143
7.29M
        }
11144
28.4M
#endif
11145
28.4M
    }
11146
33.8M
#endif /* RDOPT_ZERO_CBF_ENABLE */
11147
11148
33.8M
    return (cbf);
11149
33.8M
}