Coverage Report

Created: 2026-01-17 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_ipe_pass.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_ipe_pass.c
24
*
25
* \brief
26
*    This file contains interface functions of Intra Prediction Estimation
27
*    module
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
52
/* User include files */
53
#include "ihevc_typedefs.h"
54
#include "itt_video_api.h"
55
#include "ihevce_api.h"
56
57
#include "rc_cntrl_param.h"
58
#include "rc_frame_info_collector.h"
59
#include "rc_look_ahead_params.h"
60
61
#include "ihevc_debug.h"
62
#include "ihevc_defs.h"
63
#include "ihevc_structs.h"
64
#include "ihevc_platform_macros.h"
65
#include "ihevc_deblk.h"
66
#include "ihevc_itrans_recon.h"
67
#include "ihevc_chroma_itrans_recon.h"
68
#include "ihevc_chroma_intra_pred.h"
69
#include "ihevc_intra_pred.h"
70
#include "ihevc_inter_pred.h"
71
#include "ihevc_mem_fns.h"
72
#include "ihevc_padding.h"
73
#include "ihevc_weighted_pred.h"
74
#include "ihevc_sao.h"
75
#include "ihevc_resi_trans.h"
76
#include "ihevc_quant_iquant_ssd.h"
77
#include "ihevc_cabac_tables.h"
78
#include "ihevc_quant_tables.h"
79
80
#include "ihevce_defs.h"
81
#include "ihevce_hle_interface.h"
82
#include "ihevce_lap_enc_structs.h"
83
#include "ihevce_multi_thrd_structs.h"
84
#include "ihevce_multi_thrd_funcs.h"
85
#include "ihevce_me_common_defs.h"
86
#include "ihevce_had_satd.h"
87
#include "ihevce_error_codes.h"
88
#include "ihevce_bitstream.h"
89
#include "ihevce_cabac.h"
90
#include "ihevce_rdoq_macros.h"
91
#include "ihevce_function_selector.h"
92
#include "ihevce_enc_structs.h"
93
#include "ihevce_entropy_structs.h"
94
#include "ihevce_cmn_utils_instr_set_router.h"
95
#include "ihevce_enc_loop_structs.h"
96
#include "ihevce_inter_pred.h"
97
#include "ihevc_weighted_pred.h"
98
#include "ihevce_ipe_instr_set_router.h"
99
#include "ihevce_ipe_structs.h"
100
#include "ihevce_ipe_pass.h"
101
#include "ihevce_decomp_pre_intra_structs.h"
102
#include "ihevce_decomp_pre_intra_pass.h"
103
#include "ihevce_recur_bracketing.h"
104
#include "ihevce_nbr_avail.h"
105
#include "ihevce_global_tables.h"
106
#include "ihevc_resi_trans.h"
107
108
#include "cast_types.h"
109
#include "osal.h"
110
#include "osal_defaults.h"
111
112
/*****************************************************************************/
113
/* Global Tables                                                             */
114
/*****************************************************************************/
115
116
/**
117
******************************************************************************
118
* @brief  Look up table for choosing the appropriate function for
119
*         Intra prediction
120
*
121
* @remarks Same look up table enums are used for luma & chroma but each
122
*          have seperate functions implemented
123
******************************************************************************
124
*/
125
WORD32 g_i4_ipe_funcs[MAX_NUM_IP_MODES] = {
126
    IPE_FUNC_MODE_0, /* Mode 0 */
127
    IPE_FUNC_MODE_1, /* Mode 1 */
128
    IPE_FUNC_MODE_2, /* Mode 2 */
129
    IPE_FUNC_MODE_3TO9, /* Mode 3 */
130
    IPE_FUNC_MODE_3TO9, /* Mode 4 */
131
    IPE_FUNC_MODE_3TO9, /* Mode 5 */
132
    IPE_FUNC_MODE_3TO9, /* Mode 6 */
133
    IPE_FUNC_MODE_3TO9, /* Mode 7 */
134
    IPE_FUNC_MODE_3TO9, /* Mode 8 */
135
    IPE_FUNC_MODE_3TO9, /* Mode 9 */
136
    IPE_FUNC_MODE_10, /* Mode 10 */
137
    IPE_FUNC_MODE_11TO17, /* Mode 11 */
138
    IPE_FUNC_MODE_11TO17, /* Mode 12 */
139
    IPE_FUNC_MODE_11TO17, /* Mode 13 */
140
    IPE_FUNC_MODE_11TO17, /* Mode 14 */
141
    IPE_FUNC_MODE_11TO17, /* Mode 15 */
142
    IPE_FUNC_MODE_11TO17, /* Mode 16 */
143
    IPE_FUNC_MODE_11TO17, /* Mode 17 */
144
    IPE_FUNC_MODE_18_34, /* Mode 18 */
145
    IPE_FUNC_MODE_19TO25, /* Mode 19 */
146
    IPE_FUNC_MODE_19TO25, /* Mode 20 */
147
    IPE_FUNC_MODE_19TO25, /* Mode 21 */
148
    IPE_FUNC_MODE_19TO25, /* Mode 22 */
149
    IPE_FUNC_MODE_19TO25, /* Mode 23 */
150
    IPE_FUNC_MODE_19TO25, /* Mode 24 */
151
    IPE_FUNC_MODE_19TO25, /* Mode 25 */
152
    IPE_FUNC_MODE_26, /* Mode 26 */
153
    IPE_FUNC_MODE_27TO33, /* Mode 27 */
154
    IPE_FUNC_MODE_27TO33, /* Mode 26 */
155
    IPE_FUNC_MODE_27TO33, /* Mode 29 */
156
    IPE_FUNC_MODE_27TO33, /* Mode 30 */
157
    IPE_FUNC_MODE_27TO33, /* Mode 31 */
158
    IPE_FUNC_MODE_27TO33, /* Mode 32 */
159
    IPE_FUNC_MODE_27TO33, /* Mode 33 */
160
    IPE_FUNC_MODE_18_34, /* Mode 34 */
161
};
162
163
/**
164
******************************************************************************
165
* @brief  Look up table for deciding whether to use original samples or
166
*         filtered reference samples for Intra prediction
167
*
168
* @remarks This table has the flags for transform size of 8, 16 and 32
169
*          Input is log2nT - 3 and intra prediction mode
170
******************************************************************************
171
*/
172
UWORD8 gau1_ipe_filter_flag[3][MAX_NUM_IP_MODES] = {
173
    { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174
      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
175
    { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,
176
      1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
177
    { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
178
      1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 }
179
};
180
181
/*****************************************************************************/
182
/* Function Definitions                                                      */
183
/*****************************************************************************/
184
185
/*!
186
******************************************************************************
187
* \if Function name : ihevce_ipe_recompute_lambda_from_min_8x8_act_in_ctb \endif
188
*
189
* \brief
190
*    This function recomputes lambda using min 8x8 act in CTB
191
*
192
* \author
193
*    Ittiam
194
*
195
* \return
196
*    Nothing
197
*
198
******************************************************************************
199
*/
200
void ihevce_ipe_recompute_lambda_from_min_8x8_act_in_ctb(
201
    ihevce_ipe_ctxt_t *ps_ctxt, ihevce_ed_ctb_l1_t *ps_ed_ctb_l1)
202
10.1k
{
203
10.1k
    WORD32 i4_cu_qp = 0;
204
10.1k
#if MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON
205
10.1k
    WORD32 i4_activity;
206
10.1k
#endif
207
10.1k
    WORD32 i4_qscale;
208
10.1k
    WORD32 i4_curr_satd;
209
10.1k
    long double ld_avg_satd;
210
211
#if LAMDA_BASED_ON_QUANT
212
    i4_curr_satd = ps_ed_ctb_l1->i4_32x32_satd[0][2];
213
    i8_avg_satd = ps_ctxt->i8_curr_frame_32x32_avg_act[2];
214
#else
215
10.1k
    i4_curr_satd = ps_ed_ctb_l1->i4_32x32_satd[0][3];
216
10.1k
    ld_avg_satd = 2.0 + ps_ctxt->ld_curr_frame_16x16_log_avg[0];
217
10.1k
#endif
218
219
10.1k
    if(ps_ctxt->i4_l0ipe_qp_mod)
220
10.1k
    {
221
10.1k
#if MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON
222
10.1k
        i4_cu_qp = ihevce_cu_level_qp_mod(
223
10.1k
            ps_ctxt->i4_qscale,
224
10.1k
            i4_curr_satd,
225
10.1k
            ld_avg_satd,
226
10.1k
            ps_ctxt->f_strength,
227
10.1k
            &i4_activity,
228
10.1k
            &i4_qscale,
229
10.1k
            ps_ctxt->ps_rc_quant_ctxt);
230
10.1k
#endif
231
10.1k
    }
232
10.1k
    ihevce_get_ipe_ol_cu_lambda_prms(ps_ctxt, i4_cu_qp);
233
10.1k
}
234
/*!
235
******************************************************************************
236
* \if Function name : ihevce_ipe_pass_satd \endif
237
*
238
* \brief
239
*    This function calcuates the SATD for a given size and returns the value
240
*
241
* \date
242
*    18/09/2012
243
*
244
* \author
245
*    Ittiam
246
*
247
* \return
248
*
249
* List of Functions
250
*
251
******************************************************************************
252
*/
253
UWORD32 ihevce_ipe_pass_satd(WORD16 *pi2_coeff, WORD32 coeff_stride, WORD32 trans_size)
254
5.19M
{
255
5.19M
    WORD32 i, j, satd;
256
257
5.19M
    satd = 0;
258
259
    /* run a loop and find the satd by doing ABS */
260
25.9M
    for(i = 0; i < trans_size; i++)
261
20.7M
    {
262
103M
        for(j = 0; j < trans_size; j++)
263
83.0M
        {
264
83.0M
            satd += abs(*pi2_coeff++);
265
83.0M
        }
266
        /* row level update */
267
20.7M
        pi2_coeff += coeff_stride - trans_size;
268
20.7M
    }
269
270
5.19M
    {
271
5.19M
        WORD32 transform_shift;
272
5.19M
        WORD32 log2_trans_size;
273
274
5.19M
        GETRANGE(log2_trans_size, trans_size);
275
5.19M
        log2_trans_size -= 1;
276
5.19M
        transform_shift = MAX_TR_DYNAMIC_RANGE - BIT_DEPTH - log2_trans_size;
277
5.19M
        satd >>= transform_shift;
278
5.19M
    }
279
280
5.19M
    return (satd);
281
5.19M
}
282
283
/*!
284
******************************************************************************
285
* \if Function name : ihevce_ipe_get_num_mem_recs \endif
286
*
287
* \brief
288
*    Number of memory records are returned for IPE module
289
*
290
*
291
* \return
292
*    None
293
*
294
* \author
295
*  Ittiam
296
*
297
*****************************************************************************
298
*/
299
WORD32 ihevce_ipe_get_num_mem_recs(void)
300
682
{
301
682
    return (NUM_IPE_MEM_RECS);
302
682
}
303
304
/*!
305
******************************************************************************
306
* \if Function name : ihevce_ipe_get_mem_recs \endif
307
*
308
* \brief
309
*    Memory requirements are returned for IPE.
310
*
311
* \param[in,out]  ps_mem_tab : pointer to memory descriptors table
312
* \param[in] ps_init_prms : Create time static parameters
313
* \param[in] i4_num_proc_thrds : Number of processing threads for this module
314
* \param[in] i4_mem_space : memspace in whihc memory request should be done
315
*
316
* \return
317
*    None
318
*
319
* \author
320
*  Ittiam
321
*
322
*****************************************************************************
323
*/
324
WORD32
325
    ihevce_ipe_get_mem_recs(iv_mem_rec_t *ps_mem_tab, WORD32 i4_num_proc_thrds, WORD32 i4_mem_space)
326
341
{
327
    /* memories should be requested assuming worst case requirememnts */
328
329
    /* Module context structure */
330
341
    ps_mem_tab[IPE_CTXT].i4_mem_size = sizeof(ihevce_ipe_master_ctxt_t);
331
332
341
    ps_mem_tab[IPE_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
333
334
341
    ps_mem_tab[IPE_CTXT].i4_mem_alignment = 8;
335
336
    /* Threads ctxt structure */
337
341
    ps_mem_tab[IPE_THRDS_CTXT].i4_mem_size = i4_num_proc_thrds * sizeof(ihevce_ipe_ctxt_t);
338
339
341
    ps_mem_tab[IPE_THRDS_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
340
341
341
    ps_mem_tab[IPE_THRDS_CTXT].i4_mem_alignment = 32;
342
343
341
    return (NUM_IPE_MEM_RECS);
344
341
}
345
346
/*!
347
******************************************************************************
348
* \if Function name : ihevce_ipe_init \endif
349
*
350
* \brief
351
*    Intialization for IPE context state structure .
352
*
353
* \param[in] ps_mem_tab : pointer to memory descriptors table
354
* \param[in] ps_init_prms : Create time static parameters
355
*
356
* \return
357
*    None
358
*
359
* \author
360
*  Ittiam
361
*
362
*****************************************************************************
363
*/
364
void *ihevce_ipe_init(
365
    iv_mem_rec_t *ps_mem_tab,
366
    ihevce_static_cfg_params_t *ps_init_prms,
367
    WORD32 i4_num_proc_thrds,
368
    WORD32 i4_ref_id,
369
    func_selector_t *ps_func_selector,
370
    rc_quant_t *ps_rc_quant_ctxt,
371
    WORD32 i4_resolution_id,
372
    UWORD8 u1_is_popcnt_available)
373
341
{
374
341
    WORD32 i4_thrds;
375
341
    UWORD32 u4_width, u4_ctb_in_a_row;
376
    //  WORD32 i4_ctr;
377
341
    ihevce_ipe_master_ctxt_t *ps_master_ctxt;
378
341
    ihevce_ipe_ctxt_t *ps_ctxt;
379
380
    /* IPE master state structure */
381
341
    ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)ps_mem_tab[IPE_CTXT].pv_base;
382
383
341
    ps_master_ctxt->i4_num_proc_thrds = i4_num_proc_thrds;
384
385
341
    ps_ctxt = (ihevce_ipe_ctxt_t *)ps_mem_tab[IPE_THRDS_CTXT].pv_base;
386
387
341
    ps_ctxt->ps_rc_quant_ctxt = ps_rc_quant_ctxt;
388
389
    /*width of the input YUV to be encoded. */
390
341
    u4_width = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width;
391
    /*making the width a multiple of CTB size*/
392
341
    u4_width += SET_CTB_ALIGN(
393
341
        ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width, MAX_CTB_SIZE);
394
395
341
    u4_ctb_in_a_row = (u4_width / MAX_CTB_SIZE);
396
397
    /* perform all one initialisation here */
398
682
    for(i4_thrds = 0; i4_thrds < ps_master_ctxt->i4_num_proc_thrds; i4_thrds++)
399
341
    {
400
341
        ps_master_ctxt->aps_ipe_thrd_ctxt[i4_thrds] = ps_ctxt;
401
402
        /* initialise the CU and TU sizes */
403
341
        ps_ctxt->u1_ctb_size = (1 << ps_init_prms->s_config_prms.i4_max_log2_cu_size);
404
341
        ps_ctxt->u1_min_cu_size = (1 << ps_init_prms->s_config_prms.i4_min_log2_cu_size);
405
341
        ps_ctxt->u1_min_tu_size = (1 << ps_init_prms->s_config_prms.i4_min_log2_tu_size);
406
407
        /** Register the function selector pointer*/
408
341
        ps_ctxt->ps_func_selector = ps_func_selector;
409
410
        /* Initiailize the encoder quality preset           */
411
        /* IPE algorithm is controlled based on this preset */
412
341
        ps_ctxt->i4_quality_preset =
413
341
            ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset;
414
415
341
        if(ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P7)
416
36
        {
417
36
            ps_ctxt->i4_quality_preset = IHEVCE_QUALITY_P6;
418
36
        }
419
420
        /* initialise all the pointer to start of arrays */
421
341
        ps_ctxt->ps_ipe_cu_tree = &ps_ctxt->as_ipe_cu_tree[0];
422
423
        /* initialize QP */
424
341
        ps_ctxt->i1_QP =
425
341
            ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].ai4_frame_qp[i4_ref_id];
426
341
        ps_ctxt->u1_num_b_frames =
427
341
            (1 << ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers) - 1;
428
429
341
        ps_ctxt->b_sad_type = IPE_SAD_TYPE;
430
341
        ps_ctxt->u1_ipe_step_size = IPE_STEP_SIZE;
431
432
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_0] =
433
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_planar_fptr;
434
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_1] =
435
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_dc_fptr;
436
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_2] =
437
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode2_fptr;
438
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_3TO9] =
439
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_3_to_9_fptr;
440
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_10] =
441
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_horz_fptr;
442
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_11TO17] =
443
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_11_to_17_fptr;
444
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_18_34] =
445
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_18_34_fptr;
446
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_19TO25] =
447
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_19_to_25_fptr;
448
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_26] =
449
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_ver_fptr;
450
341
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_27TO33] =
451
341
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_27_to_33_fptr;
452
453
        /* nbr parameters initialization */
454
        /* perform all one initialisation here */
455
456
341
        ps_ctxt->i4_nbr_map_strd = MAX_PU_IN_CTB_ROW + 1 + 8;
457
458
341
        ps_ctxt->pu1_ctb_nbr_map = ps_ctxt->au1_nbr_ctb_map[0];
459
460
        /* move the pointer to 1,2 location */
461
341
        ps_ctxt->pu1_ctb_nbr_map += ps_ctxt->i4_nbr_map_strd;
462
341
        ps_ctxt->pu1_ctb_nbr_map++;
463
341
        ps_ctxt->i4_l0ipe_qp_mod = ps_init_prms->s_config_prms.i4_cu_level_rc & 1;
464
341
        ps_ctxt->i4_pass = ps_init_prms->s_pass_prms.i4_pass;
465
341
        if(ps_init_prms->s_coding_tools_prms.i4_use_default_sc_mtx == 0)
466
205
        {
467
            /* initialise the scale & rescale matricies */
468
205
            ps_ctxt->api2_scal_mat[0] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
469
205
            ps_ctxt->api2_scal_mat[1] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
470
205
            ps_ctxt->api2_scal_mat[2] = (WORD16 *)&gi2_flat_scale_mat_8x8[0];
471
205
            ps_ctxt->api2_scal_mat[3] = (WORD16 *)&gi2_flat_scale_mat_16x16[0];
472
205
            ps_ctxt->api2_scal_mat[4] = (WORD16 *)&gi2_flat_scale_mat_32x32[0];
473
            /*init for inter matrix*/
474
205
            ps_ctxt->api2_scal_mat[5] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
475
205
            ps_ctxt->api2_scal_mat[6] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
476
205
            ps_ctxt->api2_scal_mat[7] = (WORD16 *)&gi2_flat_scale_mat_8x8[0];
477
205
            ps_ctxt->api2_scal_mat[8] = (WORD16 *)&gi2_flat_scale_mat_16x16[0];
478
205
            ps_ctxt->api2_scal_mat[9] = (WORD16 *)&gi2_flat_scale_mat_32x32[0];
479
480
            /*init for rescale matrix*/
481
205
            ps_ctxt->api2_rescal_mat[0] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
482
205
            ps_ctxt->api2_rescal_mat[1] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
483
205
            ps_ctxt->api2_rescal_mat[2] = (WORD16 *)&gi2_flat_rescale_mat_8x8[0];
484
205
            ps_ctxt->api2_rescal_mat[3] = (WORD16 *)&gi2_flat_rescale_mat_16x16[0];
485
205
            ps_ctxt->api2_rescal_mat[4] = (WORD16 *)&gi2_flat_rescale_mat_32x32[0];
486
            /*init for rescale inter matrix*/
487
205
            ps_ctxt->api2_rescal_mat[5] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
488
205
            ps_ctxt->api2_rescal_mat[6] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
489
205
            ps_ctxt->api2_rescal_mat[7] = (WORD16 *)&gi2_flat_rescale_mat_8x8[0];
490
205
            ps_ctxt->api2_rescal_mat[8] = (WORD16 *)&gi2_flat_rescale_mat_16x16[0];
491
205
            ps_ctxt->api2_rescal_mat[9] = (WORD16 *)&gi2_flat_rescale_mat_32x32[0];
492
205
        }
493
136
        else if(ps_init_prms->s_coding_tools_prms.i4_use_default_sc_mtx == 1)
494
136
        {
495
            /* initialise the scale & rescale matricies */
496
136
            ps_ctxt->api2_scal_mat[0] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
497
136
            ps_ctxt->api2_scal_mat[1] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
498
136
            ps_ctxt->api2_scal_mat[2] = (WORD16 *)&gi2_intra_default_scale_mat_8x8[0];
499
136
            ps_ctxt->api2_scal_mat[3] = (WORD16 *)&gi2_intra_default_scale_mat_16x16[0];
500
136
            ps_ctxt->api2_scal_mat[4] = (WORD16 *)&gi2_intra_default_scale_mat_32x32[0];
501
            /*init for inter matrix*/
502
136
            ps_ctxt->api2_scal_mat[5] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
503
136
            ps_ctxt->api2_scal_mat[6] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
504
136
            ps_ctxt->api2_scal_mat[7] = (WORD16 *)&gi2_inter_default_scale_mat_8x8[0];
505
136
            ps_ctxt->api2_scal_mat[8] = (WORD16 *)&gi2_inter_default_scale_mat_16x16[0];
506
136
            ps_ctxt->api2_scal_mat[9] = (WORD16 *)&gi2_inter_default_scale_mat_32x32[0];
507
508
            /*init for rescale matrix*/
509
136
            ps_ctxt->api2_rescal_mat[0] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
510
136
            ps_ctxt->api2_rescal_mat[1] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
511
136
            ps_ctxt->api2_rescal_mat[2] = (WORD16 *)&gi2_intra_default_rescale_mat_8x8[0];
512
136
            ps_ctxt->api2_rescal_mat[3] = (WORD16 *)&gi2_intra_default_rescale_mat_16x16[0];
513
136
            ps_ctxt->api2_rescal_mat[4] = (WORD16 *)&gi2_intra_default_rescale_mat_32x32[0];
514
            /*init for rescale inter matrix*/
515
136
            ps_ctxt->api2_rescal_mat[5] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
516
136
            ps_ctxt->api2_rescal_mat[6] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
517
136
            ps_ctxt->api2_rescal_mat[7] = (WORD16 *)&gi2_inter_default_rescale_mat_8x8[0];
518
136
            ps_ctxt->api2_rescal_mat[8] = (WORD16 *)&gi2_inter_default_rescale_mat_16x16[0];
519
136
            ps_ctxt->api2_rescal_mat[9] = (WORD16 *)&gi2_inter_default_rescale_mat_32x32[0];
520
136
        }
521
0
        else
522
0
        {
523
0
            ASSERT(0);
524
0
        }
525
526
341
        ps_ctxt->u1_bit_depth = ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth;
527
528
        /**
529
        * Initialize the intra prediction modes map for the CTB to INTRA_DC
530
        **/
531
341
        {
532
341
            WORD32 row, col;
533
6.13k
            for(row = 0; row < (MAX_TU_ROW_IN_CTB + 1); row++)
534
104k
                for(col = 0; col < (MAX_TU_COL_IN_CTB + 1); col++)
535
98.5k
                    ps_ctxt->au1_ctb_mode_map[row][col] = INTRA_DC;
536
341
        }
537
538
341
        ihevce_cmn_utils_instr_set_router(
539
341
            &ps_ctxt->s_cmn_opt_func, u1_is_popcnt_available, ps_init_prms->e_arch_type);
540
541
341
        ihevce_ipe_instr_set_router(
542
341
            &ps_ctxt->s_ipe_optimised_function_list, ps_init_prms->e_arch_type);
543
544
        /* increment the thread ctxt pointer */
545
341
        ps_ctxt++;
546
341
    }
547
548
    /* return the handle to caller */
549
341
    return ((void *)ps_master_ctxt);
550
341
}
551
/*!
552
******************************************************************************
553
* \if Function name : ihevce_ipe_get_frame_intra_satd_cost \endif
554
*
555
* \brief
556
*    Function to export frame-level accumalated SATD .
557
*
558
* \param[in] pv_ctxt : pointer to IPE module
559
*
560
* \return
561
*    None
562
*
563
* \author
564
*  Ittiam
565
*
566
*****************************************************************************
567
*/
568
LWORD64 ihevce_ipe_get_frame_intra_satd_cost(
569
    void *pv_ctxt,
570
    LWORD64 *pi8_frame_satd_by_qpmod,
571
    LWORD64 *pi8_frame_acc_mode_bits_cost,
572
    LWORD64 *pi8_frame_acc_activity_factor,
573
    LWORD64 *pi8_frame_l0_acc_satd)
574
8.93k
{
575
8.93k
    WORD32 i4_thrds;
576
577
8.93k
    ihevce_ipe_master_ctxt_t *ps_master_ctxt;
578
8.93k
    ihevce_ipe_ctxt_t *ps_ctxt;
579
8.93k
    LWORD64 i8_frame_acc_satd_cost = 0;
580
8.93k
    LWORD64 i8_frame_acc_satd = 0;
581
8.93k
    LWORD64 i8_frame_satd_by_qpmod = 0;
582
8.93k
    LWORD64 i8_frame_acc_mode_bits_cost = 0;
583
8.93k
    LWORD64 i8_frame_acc_activity_factor = 0;
584
    /* IPE master state structure */
585
8.93k
    ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)pv_ctxt;
586
587
    /* perform all one initialisation here */
588
17.8k
    for(i4_thrds = 0; i4_thrds < ps_master_ctxt->i4_num_proc_thrds; i4_thrds++)
589
8.93k
    {
590
8.93k
        ps_ctxt = ps_master_ctxt->aps_ipe_thrd_ctxt[i4_thrds];
591
592
8.93k
        i8_frame_acc_satd_cost += ps_ctxt->i8_frame_acc_satd_cost;
593
8.93k
        i8_frame_satd_by_qpmod += (ps_ctxt->i8_frame_acc_satd_by_modqp_q10 >> SATD_BY_ACT_Q_FAC);
594
8.93k
        i8_frame_acc_mode_bits_cost += ps_ctxt->i8_frame_acc_mode_bits_cost;
595
596
8.93k
        i8_frame_acc_activity_factor += ps_ctxt->i8_frame_acc_act_factor;
597
598
8.93k
        i8_frame_acc_satd += ps_ctxt->i8_frame_acc_satd;
599
8.93k
    }
600
8.93k
    *pi8_frame_satd_by_qpmod = i8_frame_satd_by_qpmod;
601
602
8.93k
    *pi8_frame_acc_mode_bits_cost = i8_frame_acc_mode_bits_cost;
603
604
8.93k
    *pi8_frame_acc_activity_factor = i8_frame_acc_activity_factor;
605
606
8.93k
    *pi8_frame_l0_acc_satd = i8_frame_acc_satd;
607
608
8.93k
    return (i8_frame_acc_satd_cost);
609
8.93k
}
610
611
/**
612
*******************************************************************************
613
* \if Function name : ihevce_intra_pred_ref_filtering \endif
614
*
615
* \brief
616
*    Intra prediction interpolation filter for ref_filtering for Encoder
617
*
618
* \par Description:
619
*    Reference DC filtering for neighboring samples dependent  on TU size and
620
*    mode  Refer to section 8.4.4.2.3 in the standard
621
*
622
* \param[in] pu1_src pointer to the source
623
* \param[out] pu1_dst pointer to the destination
624
* \param[in] nt integer Transform Block size
625
*
626
* \returns
627
*  none
628
*
629
* \author
630
*  Ittiam
631
*
632
*******************************************************************************
633
*/
634
635
#if IHEVCE_INTRA_REF_FILTERING == C
636
void ihevce_intra_pred_ref_filtering(UWORD8 *pu1_src, WORD32 nt, UWORD8 *pu1_dst)
637
3.42M
{
638
3.42M
    WORD32 i; /* Generic indexing variable */
639
3.42M
    WORD32 four_nt = 4 * nt;
640
641
    /* Extremities Untouched*/
642
3.42M
    pu1_dst[0] = pu1_src[0];
643
3.42M
    pu1_dst[4 * nt] = pu1_src[4 * nt];
644
    /* Perform bilinear filtering of Reference Samples */
645
106M
    for(i = 0; i < (four_nt - 1); i++)
646
103M
    {
647
103M
        pu1_dst[i + 1] = (pu1_src[i] + 2 * pu1_src[i + 1] + pu1_src[i + 2] + 2) >> 2;
648
103M
    }
649
3.42M
}
650
#endif
651
652
/*!
653
******************************************************************************
654
* \if Function name : ihevce_ipe_process_ctb \endif
655
*
656
* \brief
657
*    CTB level IPE function
658
*
659
* \param[in] pv_ctxt : pointer to IPE module
660
* \param[in] ps_frm_ctb_prms : CTB characteristics parameters
661
* \param[in] ps_curr_src  : pointer to input yuv buffer (row buffer)
662
* \param[out] ps_ctb_out : pointer to CTB analyse output structure (row buffer)
663
* \param[out] ps_row_cu : pointer to CU analyse output structure (row buffer)
664
*
665
* \return
666
*    None
667
*
668
* Note : This function will receive CTB pointers which may point to
669
* blocks of CTB size or smaller (at the right and bottom edges of the picture)
670
* This function recursively creates smaller square partitions and passes them
671
* on for intra processing estimation
672
*
673
* \author
674
*  Ittiam
675
*
676
*****************************************************************************
677
*/
678
void ihevce_ipe_process_ctb(
679
    ihevce_ipe_ctxt_t *ps_ctxt,
680
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
681
    iv_enc_yuv_buf_t *ps_curr_src,
682
    ihevce_ipe_cu_tree_t *ps_curr_ctb_node,
683
    ipe_l0_ctb_analyse_for_me_t *ps_l0_ipe_out_ctb,
684
    ctb_analyse_t *ps_ctb_out,
685
    //cu_analyse_t      *ps_row_cu,
686
    ihevce_ed_blk_t *ps_ed_l1_ctb,
687
    ihevce_ed_blk_t *ps_ed_l2_ctb,
688
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1)
689
34.3k
{
690
    /* reset the map buffer to 0*/
691
34.3k
    memset(
692
34.3k
        &ps_ctxt->au1_nbr_ctb_map[0][0],
693
34.3k
        0,
694
34.3k
        (MAX_PU_IN_CTB_ROW + 1 + 8) * (MAX_PU_IN_CTB_ROW + 1 + 8));
695
696
    /* set the CTB neighbour availability flags */
697
34.3k
    ihevce_set_ctb_nbr(
698
34.3k
        &ps_ctxt->s_ctb_nbr_avail_flags,
699
34.3k
        ps_ctxt->pu1_ctb_nbr_map,
700
34.3k
        ps_ctxt->i4_nbr_map_strd,
701
34.3k
        ps_ctxt->u2_ctb_num_in_row,
702
34.3k
        ps_ctxt->u2_ctb_row_num,
703
34.3k
        ps_frm_ctb_prms);
704
705
    /* IPE cu and mode decision */
706
34.3k
    ihevce_bracketing_analysis(
707
34.3k
        ps_ctxt,
708
34.3k
        ps_curr_ctb_node,
709
34.3k
        ps_curr_src,
710
34.3k
        ps_ctb_out,
711
        //ps_row_cu,
712
34.3k
        ps_ed_l1_ctb,
713
34.3k
        ps_ed_l2_ctb,
714
34.3k
        ps_ed_ctb_l1,
715
34.3k
        ps_l0_ipe_out_ctb);
716
717
34.3k
    return;
718
34.3k
}
719
720
/*!
721
******************************************************************************
722
* \if Function name : ihevce_ipe_process_row \endif
723
*
724
* \brief
725
*    Row level IPE function
726
*
727
* \param[in] pv_ctxt : pointer to IPE module
728
* \param[in] ps_frm_ctb_prms : CTB characteristics parameters
729
* \param[in] ps_curr_src  : pointer to input yuv buffer (row buffer)
730
* \param[out] ps_ctb_out : pointer to CTB analyse output structure (row buffer)
731
* \param[out] ps_cu_out : pointer to CU analyse output structure (row buffer)
732
*\param[out]  pi4_num_ctbs_cur_row  : pointer to store the number of ctbs processed in current row
733
*\param[in]  pi4_num_ctbs_top_row  : pointer to check the number of ctbs processed in top row
734
*
735
* \return
736
*    None
737
*
738
* Note : Currently the frame level calculations done assumes that
739
*        framewidth of the input are excat multiple of ctbsize
740
*
741
* \author
742
*  Ittiam
743
*
744
*****************************************************************************
745
*/
746
void ihevce_ipe_process_row(
747
    ihevce_ipe_ctxt_t *ps_ctxt,
748
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
749
    iv_enc_yuv_buf_t *ps_curr_src,
750
    ipe_l0_ctb_analyse_for_me_t *ps_ipe_ctb_out_row,
751
    ctb_analyse_t *ps_ctb_out,
752
    //cu_analyse_t   *ps_row_cu,
753
    ihevce_ed_blk_t *ps_ed_l1_row,
754
    ihevce_ed_blk_t *ps_ed_l2_row,
755
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1_row,
756
    WORD32 blk_inc_ctb_l1,
757
    WORD32 blk_inc_ctb_l2)
758
14.3k
{
759
    /* local variables */
760
14.3k
    UWORD16 ctb_ctr;
761
14.3k
    iv_enc_yuv_buf_t s_curr_src_bufs;
762
14.3k
    ipe_l0_ctb_analyse_for_me_t *ps_l0_ipe_out_ctb;
763
14.3k
    UWORD16 u2_pic_wdt;
764
14.3k
    UWORD16 u2_pic_hgt;
765
14.3k
    ihevce_ed_blk_t *ps_ed_l1_ctb;
766
14.3k
    ihevce_ed_blk_t *ps_ed_l2_ctb;
767
14.3k
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1;
768
769
14.3k
    UWORD8 u1_ctb_size;
770
771
14.3k
    u2_pic_wdt = ps_frm_ctb_prms->i4_cu_aligned_pic_wd;
772
14.3k
    u2_pic_hgt = ps_frm_ctb_prms->i4_cu_aligned_pic_ht;
773
774
14.3k
    u1_ctb_size = ps_ctxt->u1_ctb_size;
775
776
    /* ----------------------------------------------------- */
777
    /* store the stride and dimensions of source             */
778
    /* buffer pointers will be over written at every CTB row */
779
    /* ----------------------------------------------------- */
780
14.3k
    memcpy(&s_curr_src_bufs, ps_curr_src, sizeof(iv_enc_yuv_buf_t));
781
14.3k
    ps_l0_ipe_out_ctb = ps_ipe_ctb_out_row;
782
783
    /* --------- Loop over all the CTBs in a row --------------- */
784
48.6k
    for(ctb_ctr = 0; ctb_ctr < ps_frm_ctb_prms->i4_num_ctbs_horz; ctb_ctr++)
785
34.3k
    {
786
        //UWORD8            num_cus_in_ctb;
787
788
34.3k
        UWORD8 *pu1_tmp;
789
790
        /* Create pointer to ctb node */
791
34.3k
        ihevce_ipe_cu_tree_t *ps_ctb_node;
792
793
34.3k
        WORD32 nbr_flags;
794
795
34.3k
        WORD32 row;
796
        /* luma src */
797
34.3k
        pu1_tmp = (UWORD8 *)ps_curr_src->pv_y_buf;
798
34.3k
        pu1_tmp += (ctb_ctr * ps_frm_ctb_prms->i4_ctb_size);
799
800
34.3k
        s_curr_src_bufs.pv_y_buf = pu1_tmp;
801
802
        /* Cb & CR pixel interleaved src */
803
34.3k
        pu1_tmp = (UWORD8 *)ps_curr_src->pv_u_buf;
804
34.3k
        pu1_tmp += (ctb_ctr * (ps_frm_ctb_prms->i4_ctb_size >> 1));
805
806
34.3k
        s_curr_src_bufs.pv_u_buf = pu1_tmp;
807
808
        /* Store the number of current ctb within row in the context */
809
34.3k
        ps_ctxt->u2_ctb_num_in_row = ctb_ctr;
810
811
        /* Initialize number of coding units in ctb to 0 */
812
34.3k
        ps_ctb_out->u1_num_cus_in_ctb = 0;
813
        /* Initialize split flag to 0 - No partition  */
814
34.3k
        ps_ctb_out->u4_cu_split_flags = 0;
815
        /* store the cu pointer for current ctb out */
816
        //ps_ctb_out->ps_coding_units_in_ctb = ps_row_cu;
817
818
        /* Initialize the CTB parameters at the root node level */
819
34.3k
        ps_ctb_node = ps_ctxt->ps_ipe_cu_tree;
820
34.3k
        ps_ctb_node->ps_parent = NULL;
821
34.3k
        ps_ctb_node->u1_depth = 0;
822
34.3k
        ps_ctb_node->u1_cu_size = u1_ctb_size;
823
34.3k
        ps_ctb_node->u2_x0 = 0;
824
34.3k
        ps_ctb_node->u2_y0 = 0;
825
826
34.3k
        ps_ctb_node->u2_orig_x = ctb_ctr * ps_ctb_node->u1_cu_size;
827
34.3k
        ps_ctb_node->u2_orig_y = ps_ctxt->u2_ctb_row_num * ps_ctb_node->u1_cu_size;
828
829
34.3k
        ps_ctb_node->u1_width = u1_ctb_size;
830
34.3k
        ps_ctb_node->u1_height = u1_ctb_size;
831
34.3k
#if !(PIC_ALIGN_CTB_SIZE)
832
34.3k
        if(ps_ctxt->u2_ctb_num_in_row == (ps_frm_ctb_prms->i4_num_ctbs_horz - 1))
833
14.3k
        {
834
14.3k
            ps_ctb_node->u1_width = u2_pic_wdt - (ps_ctxt->u2_ctb_num_in_row) * (u1_ctb_size);
835
14.3k
        }
836
34.3k
        if(ps_ctxt->u2_ctb_row_num == (ps_frm_ctb_prms->i4_num_ctbs_vert - 1))
837
19.1k
        {
838
19.1k
            ps_ctb_node->u1_height = u2_pic_hgt - (ps_ctxt->u2_ctb_row_num) * (u1_ctb_size);
839
19.1k
        }
840
34.3k
#endif
841
842
34.3k
        switch(ps_ctb_node->u1_cu_size)
843
34.3k
        {
844
34.3k
        case 64:
845
34.3k
            ps_ctb_node->u1_log2_nt = 6;
846
34.3k
            ps_ctb_node->u1_part_flag_pos = 0;
847
34.3k
            break;
848
0
        case 32:
849
0
            ps_ctb_node->u1_log2_nt = 5;
850
0
            ps_ctb_node->u1_part_flag_pos = 4;
851
0
            break;
852
0
        case 16:
853
0
            ps_ctb_node->u1_log2_nt = 4;
854
0
            ps_ctb_node->u1_part_flag_pos = 8;
855
0
            break;
856
34.3k
        }
857
858
        /* Set neighbor flags for the CTB */
859
34.3k
        nbr_flags = 0;
860
861
34.3k
        if(ps_ctxt->u2_ctb_num_in_row != 0)
862
20.0k
        {
863
20.0k
            nbr_flags |= LEFT_FLAG; /* Set Left Flag if not in first column */
864
20.0k
            ps_ctb_node->u1_num_left_avail = ((u2_pic_hgt - ps_ctb_node->u2_orig_y) >= u1_ctb_size)
865
20.0k
                                                 ? u1_ctb_size
866
20.0k
                                                 : u2_pic_hgt - ps_ctb_node->u2_orig_y;
867
20.0k
        }
868
14.3k
        else
869
14.3k
        {
870
14.3k
            ps_ctb_node->u1_num_left_avail = 0;
871
14.3k
        }
872
873
34.3k
        if((ps_ctxt->u2_ctb_num_in_row != 0) && (ps_ctxt->u2_ctb_row_num != 0))
874
9.82k
            nbr_flags |= TOP_LEFT_FLAG; /* Set Top-Left Flag if not in first row or first column */
875
876
34.3k
        if(ps_ctxt->u2_ctb_row_num != 0)
877
15.2k
        {
878
15.2k
            nbr_flags |= TOP_FLAG; /* Set Top Flag if not in first row */
879
15.2k
            ps_ctb_node->u1_num_top_avail = ((u2_pic_wdt - ps_ctb_node->u2_orig_x) >= u1_ctb_size)
880
15.2k
                                                ? u1_ctb_size
881
15.2k
                                                : u2_pic_wdt - ps_ctb_node->u2_orig_x;
882
15.2k
        }
883
19.1k
        else
884
19.1k
        {
885
19.1k
            ps_ctb_node->u1_num_top_avail = 0;
886
19.1k
        }
887
888
34.3k
        if(ps_ctxt->u2_ctb_row_num != 0)
889
15.2k
        {
890
15.2k
            if(ps_ctxt->u2_ctb_num_in_row == (ps_frm_ctb_prms->i4_num_ctbs_horz - 1))
891
5.42k
                ps_ctb_node->u1_num_top_right_avail = 0;
892
9.82k
            else
893
9.82k
            {
894
9.82k
                ps_ctb_node->u1_num_top_right_avail =
895
9.82k
                    ((u2_pic_wdt - ps_ctb_node->u2_orig_x - u1_ctb_size) >= u1_ctb_size)
896
9.82k
                        ? u1_ctb_size
897
9.82k
                        : u2_pic_wdt - ps_ctb_node->u2_orig_x - u1_ctb_size;
898
9.82k
                nbr_flags |=
899
9.82k
                    TOP_RIGHT_FLAG; /* Set Top-Right Flag if not in first row or last column*/
900
9.82k
            }
901
15.2k
        }
902
19.1k
        else
903
19.1k
        {
904
19.1k
            ps_ctb_node->u1_num_top_right_avail = 0;
905
19.1k
        }
906
907
34.3k
        ps_ctb_node->u1_num_bottom_left_avail = 0;
908
909
34.3k
        ps_ctb_node->i4_nbr_flag = nbr_flags;
910
911
        /**
912
        * Update CTB Mode Map
913
        * In case this is first CTB in a row, set left most column to INTRA_DC (NA)
914
        * else copy last column to first column
915
        **/
916
34.3k
        if(ctb_ctr == 0)
917
14.3k
        {
918
257k
            for(row = 0; row < (MAX_TU_ROW_IN_CTB + 1); row++)
919
243k
            {
920
243k
                ps_ctxt->au1_ctb_mode_map[row][0] = INTRA_DC;
921
243k
            }
922
14.3k
        }
923
20.0k
        else
924
20.0k
        {
925
360k
            for(row = 0; row < (MAX_TU_ROW_IN_CTB + 1); row++)
926
340k
            {
927
340k
                ps_ctxt->au1_ctb_mode_map[row][0] =
928
340k
                    ps_ctxt->au1_ctb_mode_map[row][MAX_TU_COL_IN_CTB];
929
340k
            }
930
20.0k
        }
931
932
        /* --------- IPE call at CTB level ------------------ */
933
934
        /* IPE CTB function is expected to Decide on the CUs sizes  */
935
        /* and populate the best intra prediction modes and TX flags*/
936
        /* Interface of this CTb level function is kept open */
937
938
34.3k
        ps_ed_l1_ctb = ps_ed_l1_row + ctb_ctr * blk_inc_ctb_l1;
939
34.3k
        ps_ed_l2_ctb = ps_ed_l2_row + ctb_ctr * blk_inc_ctb_l2;
940
34.3k
        ps_ed_ctb_l1 = ps_ed_ctb_l1_row + ctb_ctr;
941
942
34.3k
        if(ps_ctxt->u1_use_lambda_derived_from_min_8x8_act_in_ctb)
943
10.1k
        {
944
10.1k
            ihevce_ipe_recompute_lambda_from_min_8x8_act_in_ctb(ps_ctxt, ps_ed_ctb_l1);
945
10.1k
        }
946
947
34.3k
        ihevce_ipe_process_ctb(
948
34.3k
            ps_ctxt,
949
34.3k
            ps_frm_ctb_prms,
950
34.3k
            &s_curr_src_bufs,
951
34.3k
            ps_ctb_node,
952
34.3k
            ps_l0_ipe_out_ctb,
953
34.3k
            ps_ctb_out,
954
            //ps_row_cu,
955
34.3k
            ps_ed_l1_ctb,
956
34.3k
            ps_ed_l2_ctb,
957
34.3k
            ps_ed_ctb_l1);
958
959
        /* -------------- ctb level updates ----------------- */
960
961
34.3k
        ps_l0_ipe_out_ctb++;
962
        //num_cus_in_ctb = ps_ctb_out->u1_num_cus_in_ctb;
963
964
        //ps_row_cu += num_cus_in_ctb;
965
966
34.3k
        ps_ctb_out++;
967
34.3k
    }
968
14.3k
    return;
969
14.3k
}
970
971
/*!
972
******************************************************************************
973
* \if Function name : ihevce_ipe_process \endif
974
*
975
* \brief
976
*    Frame level IPE function
977
*
978
* \param[in] pv_ctxt : pointer to IPE module
979
* \param[in] ps_frm_ctb_prms : CTB characteristics parameters
980
* \param[in] ps_inp  : pointer to input yuv buffer (frame buffer)
981
* \param[out] ps_ctb_out : pointer to CTB analyse output structure (frame buffer)
982
* \param[out] ps_cu_out : pointer to CU analyse output structure (frame buffer)
983
*
984
* \return
985
*    None
986
*
987
* Note : Currently the frame level calculations done assumes that
988
*        framewidth of the input are excat multiple of ctbsize
989
*
990
* \author
991
*  Ittiam
992
*
993
*****************************************************************************
994
*/
995
void ihevce_ipe_process(
996
    void *pv_ctxt,
997
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
998
    frm_lambda_ctxt_t *ps_frm_lamda,
999
    ihevce_lap_enc_buf_t *ps_curr_inp,
1000
    pre_enc_L0_ipe_encloop_ctxt_t *ps_L0_IPE_curr_out_pre_enc,
1001
    ctb_analyse_t *ps_ctb_out,
1002
    //cu_analyse_t               *ps_cu_out,
1003
    ipe_l0_ctb_analyse_for_me_t *ps_ipe_ctb_out,
1004
    void *pv_multi_thrd_ctxt,
1005
    WORD32 slice_type,
1006
    ihevce_ed_blk_t *ps_ed_pic_l1,
1007
    ihevce_ed_blk_t *ps_ed_pic_l2,
1008
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1_pic,
1009
    WORD32 thrd_id,
1010
    WORD32 i4_ping_pong)
1011
8.90k
{
1012
    /* local variables */
1013
8.90k
    ihevce_ipe_master_ctxt_t *ps_master_ctxt;
1014
8.90k
    iv_enc_yuv_buf_t *ps_inp = &ps_curr_inp->s_lap_out.s_input_buf;
1015
8.90k
    ihevce_ipe_ctxt_t *ps_ctxt;
1016
8.90k
    iv_enc_yuv_buf_t s_curr_src_bufs;
1017
8.90k
    WORD32 end_of_frame;
1018
1019
8.90k
    ihevce_ed_blk_t *ps_ed_l1_row;
1020
8.90k
    ihevce_ed_blk_t *ps_ed_l2_row;
1021
8.90k
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1_row;
1022
8.90k
    WORD32 blk_inc_ctb_l1 = 0;
1023
8.90k
    WORD32 blk_inc_ctb_l2 = 0;
1024
1025
    /* Layer 1 pre intra analysis related initilization.
1026
    * Compute no of 8x8 blks in the ctb which which is
1027
    * same as no of 4x4 blks in the ctb in layer 1 */
1028
8.90k
    blk_inc_ctb_l1 = ps_frm_ctb_prms->i4_ctb_size >> 3;
1029
8.90k
    blk_inc_ctb_l1 = blk_inc_ctb_l1 * blk_inc_ctb_l1;
1030
1031
    /* Layer 2 pre intra analysis related initilization.
1032
    * Compute no of 16x16 blks in the ctb which which is
1033
    * same as no of 8x8 blks in the ctb in layer 2 */
1034
8.90k
    blk_inc_ctb_l2 = ps_frm_ctb_prms->i4_ctb_size >> 4;
1035
8.90k
    blk_inc_ctb_l2 = blk_inc_ctb_l2 * blk_inc_ctb_l2;
1036
1037
    /* ----------------------------------------------------- */
1038
    /* store the stride and dimensions of source             */
1039
    /* buffer pointers will be over written at every CTB row */
1040
    /* ----------------------------------------------------- */
1041
8.90k
    memcpy(&s_curr_src_bufs, ps_inp, sizeof(iv_enc_yuv_buf_t));
1042
1043
8.90k
    ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)pv_ctxt;
1044
8.90k
    ps_ctxt = ps_master_ctxt->aps_ipe_thrd_ctxt[thrd_id];
1045
8.90k
    end_of_frame = 0;
1046
1047
8.90k
    if(ISLICE == slice_type)
1048
2.56k
    {
1049
2.56k
        ps_ctxt->b_sad_type = IPE_SAD_TYPE;
1050
2.56k
        ps_ctxt->i4_ol_satd_lambda = ps_frm_lamda->i4_ol_satd_lambda_qf;
1051
2.56k
        ps_ctxt->i4_ol_sad_lambda = ps_frm_lamda->i4_ol_sad_lambda_qf;
1052
2.56k
    }
1053
6.33k
    else
1054
6.33k
    {
1055
6.33k
        ps_ctxt->b_sad_type = IPE_SAD_TYPE; /* SAD */
1056
6.33k
        ps_ctxt->i4_ol_satd_lambda = ps_frm_lamda->i4_ol_satd_lambda_qf;
1057
6.33k
        ps_ctxt->i4_ol_sad_lambda = ps_frm_lamda->i4_ol_sad_lambda_qf;
1058
6.33k
    }
1059
1060
8.90k
    ihevce_populate_ipe_ol_cu_lambda_prms(
1061
8.90k
        (void *)ps_ctxt,
1062
8.90k
        ps_frm_lamda,
1063
8.90k
        slice_type,
1064
8.90k
        ps_curr_inp->s_lap_out.i4_temporal_lyr_id,
1065
8.90k
        IPE_LAMBDA_TYPE);
1066
1067
    /* register the slice type in the ctxt */
1068
8.90k
    ps_ctxt->i4_slice_type = slice_type;
1069
1070
    /** Frame-levelSATD cost accumalator init to 0 */
1071
8.90k
    ps_ctxt->i8_frame_acc_satd_cost = 0;
1072
1073
    /** Frame-levelSATD accumalator init to 0 */
1074
8.90k
    ps_ctxt->i8_frame_acc_satd = 0;
1075
1076
    /** Frame-level Activity factor accumalator init to 1 */
1077
8.90k
    ps_ctxt->i8_frame_acc_act_factor = 1;
1078
1079
    /** Frame-levelMode Bits cost accumalator init to 0 */
1080
8.90k
    ps_ctxt->i8_frame_acc_mode_bits_cost = 0;
1081
1082
    /** Frame -level SATD/qp acc init to 0*/
1083
8.90k
    ps_ctxt->i8_frame_acc_satd_by_modqp_q10 = 0;
1084
1085
    /* ------------ Loop over all the CTB rows --------------- */
1086
32.1k
    while(0 == end_of_frame)
1087
23.2k
    {
1088
23.2k
        UWORD8 *pu1_tmp;
1089
23.2k
        WORD32 vert_ctr;
1090
        //cu_analyse_t *ps_row_cu;
1091
23.2k
        ctb_analyse_t *ps_ctb_out_row;
1092
23.2k
        job_queue_t *ps_job;
1093
23.2k
        ipe_l0_ctb_analyse_for_me_t *ps_ipe_ctb_out_row;
1094
1095
        /* Get the current row from the job queue */
1096
23.2k
        ps_job = (job_queue_t *)ihevce_pre_enc_grp_get_next_job(
1097
23.2k
            pv_multi_thrd_ctxt, IPE_JOB_LYR0, 1, i4_ping_pong);
1098
1099
        /* If all rows are done, set the end of process flag to 1, */
1100
        /* and the current row to -1 */
1101
23.2k
        if(NULL == ps_job)
1102
8.90k
        {
1103
8.90k
            vert_ctr = -1;
1104
8.90k
            end_of_frame = 1;
1105
8.90k
        }
1106
14.3k
        else
1107
14.3k
        {
1108
14.3k
            ASSERT(IPE_JOB_LYR0 == ps_job->i4_pre_enc_task_type);
1109
1110
            /* Obtain the current row's details from the job */
1111
14.3k
            vert_ctr = ps_job->s_job_info.s_ipe_job_info.i4_ctb_row_no;
1112
            //DBG_PRINTF("IPE PASS : Thread id %d, Vert Ctr %d\n",thrd_id,vert_ctr);
1113
1114
            /* Update the ipe context with current row number */
1115
14.3k
            ps_ctxt->u2_ctb_row_num = vert_ctr;
1116
1117
            /* derive the current ctb row pointers */
1118
1119
            /* luma src */
1120
14.3k
            pu1_tmp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_y_buf;
1121
14.3k
            pu1_tmp += (vert_ctr * ps_frm_ctb_prms->i4_ctb_size * ps_inp->i4_y_strd);
1122
1123
14.3k
            s_curr_src_bufs.pv_y_buf = pu1_tmp;
1124
1125
            /* Cb & CR pixel interleaved src */
1126
14.3k
            pu1_tmp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_u_buf;
1127
14.3k
            pu1_tmp += (vert_ctr * (ps_frm_ctb_prms->i4_ctb_size >> 1) * ps_inp->i4_uv_strd);
1128
1129
14.3k
            s_curr_src_bufs.pv_u_buf = pu1_tmp;
1130
1131
            /* row intra analyse cost buffer */
1132
14.3k
            ps_ipe_ctb_out_row = ps_ipe_ctb_out + vert_ctr * ps_frm_ctb_prms->i4_num_ctbs_horz;
1133
1134
            /* row ctb out structure */
1135
14.3k
            ps_ctb_out_row = ps_ctb_out + vert_ctr * ps_frm_ctb_prms->i4_num_ctbs_horz;
1136
1137
            /* call the row level processing function */
1138
14.3k
            ps_ed_l1_row =
1139
14.3k
                ps_ed_pic_l1 + ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l1 * vert_ctr;
1140
14.3k
            ps_ed_l2_row =
1141
14.3k
                ps_ed_pic_l2 + ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l2 * vert_ctr;
1142
14.3k
            ps_ed_ctb_l1_row = ps_ed_ctb_l1_pic + ps_frm_ctb_prms->i4_num_ctbs_horz * vert_ctr;
1143
14.3k
            ihevce_ipe_process_row(
1144
14.3k
                ps_ctxt,
1145
14.3k
                ps_frm_ctb_prms,
1146
14.3k
                &s_curr_src_bufs,
1147
14.3k
                ps_ipe_ctb_out_row,
1148
14.3k
                ps_ctb_out_row,
1149
                //ps_row_cu,
1150
14.3k
                ps_ed_l1_row,
1151
14.3k
                ps_ed_l2_row,
1152
14.3k
                ps_ed_ctb_l1_row,
1153
14.3k
                blk_inc_ctb_l1,
1154
14.3k
                blk_inc_ctb_l2);
1155
1156
14.3k
            memset(
1157
14.3k
                ps_ed_l1_row,
1158
14.3k
                0,
1159
14.3k
                ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l1 * sizeof(ihevce_ed_blk_t));
1160
14.3k
            memset(
1161
14.3k
                ps_ed_l2_row,
1162
14.3k
                0,
1163
14.3k
                ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l2 * sizeof(ihevce_ed_blk_t));
1164
1165
            /* set the output dependency */
1166
14.3k
            ihevce_pre_enc_grp_job_set_out_dep(pv_multi_thrd_ctxt, ps_job, i4_ping_pong);
1167
14.3k
        }
1168
23.2k
    }
1169
1170
    /* EIID: Print stat regarding how many 16x16 blocks are skipped in the frame, valid for single thread only */
1171
    //DBG_PRINTF("num_16x16_analyze_skipped: %d\n",ps_ctxt->u4_num_16x16_skips_at_L0_IPE);
1172
1173
8.90k
    return;
1174
8.90k
}
1175
1176
/*!
1177
******************************************************************************
1178
* \if Function name : ihevce_get_frame_lambda_prms \endif
1179
*
1180
* \brief
1181
*    Function whihc calculates the Lambda params for current picture
1182
*
1183
* \param[in] ps_enc_ctxt : encoder ctxt pointer
1184
* \param[in] ps_cur_pic_ctxt : current pic ctxt
1185
* \param[in] i4_cur_frame_qp : current pic QP
1186
* \param[in] first_field : is first field flag
1187
* \param[in] i4_temporal_lyr_id : Current picture layer id
1188
*
1189
* \return
1190
*    None
1191
*
1192
* \author
1193
*  Ittiam
1194
*
1195
*****************************************************************************
1196
*/
1197
void ihevce_get_ipe_ol_cu_lambda_prms(void *pv_ctxt, WORD32 i4_cur_cu_qp)
1198
10.1k
{
1199
10.1k
    ihevce_ipe_ctxt_t *ps_ctxt = (ihevce_ipe_ctxt_t *)pv_ctxt;
1200
    //WORD32 chroma_qp = gau1_ihevc_chroma_qp_scale[i4_cur_cu_qp];
1201
1202
    /* Store the params for IPE pass */
1203
10.1k
    ps_ctxt->i4_ol_satd_lambda = ps_ctxt->i4_ol_satd_lambda_qf_array[i4_cur_cu_qp];
1204
10.1k
    ps_ctxt->i4_ol_sad_lambda = ps_ctxt->i4_ol_sad_lambda_qf_array[i4_cur_cu_qp];
1205
10.1k
}
1206
1207
/*!
1208
******************************************************************************
1209
* \if Function name : ihevce_get_frame_lambda_prms \endif
1210
*
1211
* \brief
1212
*    Function whihc calculates the Lambda params for current picture
1213
*
1214
* \param[in] ps_enc_ctxt : encoder ctxt pointer
1215
* \param[in] ps_cur_pic_ctxt : current pic ctxt
1216
* \param[in] i4_cur_frame_qp : current pic QP
1217
* \param[in] first_field : is first field flag
1218
* \param[in] i4_temporal_lyr_id : Current picture layer id
1219
*
1220
* \return
1221
*    None
1222
*
1223
* \author
1224
*  Ittiam
1225
*
1226
*****************************************************************************
1227
*/
1228
void ihevce_populate_ipe_ol_cu_lambda_prms(
1229
    void *pv_ctxt,
1230
    frm_lambda_ctxt_t *ps_frm_lamda,
1231
    WORD32 i4_slice_type,
1232
    WORD32 i4_temporal_lyr_id,
1233
    WORD32 i4_lambda_type)
1234
8.90k
{
1235
8.90k
    WORD32 i4_curr_cu_qp;
1236
8.90k
    double lambda_modifier;
1237
8.90k
    double lambda_uv_modifier;
1238
8.90k
    double lambda;
1239
8.90k
    double lambda_uv;
1240
1241
8.90k
    ihevce_ipe_ctxt_t *ps_ctxt = (ihevce_ipe_ctxt_t *)pv_ctxt;
1242
1243
8.90k
    WORD32 i4_qp_bd_offset = 6 * (ps_ctxt->u1_bit_depth - 8);
1244
1245
8.90k
    for(i4_curr_cu_qp =
1246
8.90k
            ps_ctxt->ps_rc_quant_ctxt->i2_min_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset;
1247
462k
        i4_curr_cu_qp <= ps_ctxt->ps_rc_quant_ctxt->i2_max_qp;
1248
454k
        i4_curr_cu_qp++)
1249
454k
    {
1250
454k
        WORD32 chroma_qp = i4_curr_cu_qp;
1251
1252
454k
        if((BSLICE == i4_slice_type) && (i4_temporal_lyr_id))
1253
48.5k
        {
1254
48.5k
            lambda_modifier = ps_frm_lamda->lambda_modifier *
1255
48.5k
                              CLIP3((((double)(i4_curr_cu_qp - 12)) / 6.0), 2.00, 4.00);
1256
48.5k
            lambda_uv_modifier = ps_frm_lamda->lambda_uv_modifier *
1257
48.5k
                                 CLIP3((((double)(chroma_qp - 12)) / 6.0), 2.00, 4.00);
1258
48.5k
        }
1259
405k
        else
1260
405k
        {
1261
405k
            lambda_modifier = ps_frm_lamda->lambda_modifier;
1262
405k
            lambda_uv_modifier = ps_frm_lamda->lambda_uv_modifier;
1263
405k
        }
1264
454k
        if(ps_ctxt->i4_use_const_lamda_modifier)
1265
0
        {
1266
0
            if(ISLICE == i4_slice_type)
1267
0
            {
1268
0
                lambda_modifier = ps_ctxt->f_i_pic_lamda_modifier;
1269
0
                lambda_uv_modifier = ps_ctxt->f_i_pic_lamda_modifier;
1270
0
            }
1271
0
            else
1272
0
            {
1273
0
                lambda_modifier = CONST_LAMDA_MOD_VAL;
1274
0
                lambda_uv_modifier = CONST_LAMDA_MOD_VAL;
1275
0
            }
1276
0
        }
1277
1278
454k
        switch(i4_lambda_type)
1279
454k
        {
1280
454k
        case 0:
1281
454k
        {
1282
454k
            i4_qp_bd_offset = 0;
1283
1284
454k
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
1285
1286
454k
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
1287
1288
454k
            lambda *= lambda_modifier;
1289
454k
            lambda_uv *= lambda_uv_modifier;
1290
454k
            if(ps_ctxt->i4_use_const_lamda_modifier)
1291
0
            {
1292
0
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
1293
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1294
1295
0
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
1296
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1297
0
            }
1298
454k
            else
1299
454k
            {
1300
454k
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
1301
454k
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
1302
1303
454k
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
1304
454k
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
1305
454k
            }
1306
1307
454k
            ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
1308
454k
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp];
1309
1310
454k
            ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
1311
454k
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp];
1312
1313
454k
            break;
1314
0
        }
1315
0
        case 1:
1316
0
        {
1317
0
            ASSERT(0); /* should not enter the path for IPE*/
1318
0
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
1319
1320
0
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
1321
1322
0
            lambda *= lambda_modifier;
1323
0
            lambda_uv *= lambda_uv_modifier;
1324
0
            if(ps_ctxt->i4_use_const_lamda_modifier)
1325
0
            {
1326
0
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
1327
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1328
1329
0
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
1330
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1331
0
            }
1332
0
            else
1333
0
            {
1334
0
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
1335
0
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
1336
1337
0
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
1338
0
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
1339
0
            }
1340
1341
0
            ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
1342
0
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp];
1343
1344
0
            ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
1345
0
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp];
1346
1347
0
            break;
1348
0
        }
1349
0
        case 2:
1350
0
        {
1351
0
            ASSERT(0); /* should not enter the path for IPE*/
1352
0
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
1353
1354
0
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
1355
1356
0
            lambda *= lambda_modifier;
1357
0
            lambda_uv *= lambda_uv_modifier;
1358
0
            if(ps_ctxt->i4_use_const_lamda_modifier)
1359
0
            {
1360
0
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
1361
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1362
1363
0
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
1364
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1365
0
            }
1366
0
            else
1367
0
            {
1368
0
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
1369
0
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
1370
1371
0
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
1372
0
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
1373
0
            }
1374
0
            i4_qp_bd_offset = 0;
1375
1376
0
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
1377
1378
0
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
1379
1380
0
            lambda *= lambda_modifier;
1381
0
            lambda_uv *= lambda_uv_modifier;
1382
0
            if(ps_ctxt->i4_use_const_lamda_modifier)
1383
0
            {
1384
0
                ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
1385
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1386
1387
0
                ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
1388
0
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
1389
0
            }
1390
0
            else
1391
0
            {
1392
0
                ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
1393
0
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
1394
1395
0
                ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
1396
0
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
1397
0
            }
1398
0
            break;
1399
0
        }
1400
0
        default:
1401
0
        {
1402
            /* Intended to be a barren wasteland! */
1403
0
            ASSERT(0);
1404
0
        }
1405
454k
        }
1406
454k
    }
1407
8.90k
}
1408
1409
#define ME_COST_THRSHOLD 7
1410
/*!
1411
******************************************************************************
1412
* \if Function name : ihevce_get_frame_lambda_prms \endif
1413
*
1414
* \brief
1415
*    Function whihc calculates the Lambda params for current picture
1416
*
1417
* \param[in] ps_enc_ctxt : encoder ctxt pointer
1418
* \param[in] ps_cur_pic_ctxt : current pic ctxt
1419
* \param[in] i4_cur_frame_qp : current pic QP
1420
* \param[in] first_field : is first field flag
1421
* \param[in] i4_temporal_lyr_id : Current picture layer id
1422
*
1423
* \return
1424
*    None
1425
*
1426
* \author
1427
*  Ittiam
1428
*
1429
*****************************************************************************
1430
*/
1431
void ihevce_populate_ipe_frame_init(
1432
    void *pv_ctxt,
1433
    ihevce_static_cfg_params_t *ps_stat_prms,
1434
    WORD32 i4_curr_frm_qp,
1435
    WORD32 i4_slice_type,
1436
    WORD32 i4_thrd_id,
1437
    pre_enc_me_ctxt_t *ps_curr_out,
1438
    WORD8 i1_cu_qp_delta_enabled_flag,
1439
    rc_quant_t *ps_rc_quant_ctxt,
1440
    WORD32 i4_quality_preset,
1441
    WORD32 i4_temporal_lyr_id,
1442
    ihevce_lap_output_params_t *ps_lap_out)
1443
8.90k
{
1444
8.90k
    ihevce_ipe_master_ctxt_t *ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)pv_ctxt;
1445
8.90k
    WORD32 i4_i;
1446
8.90k
    WORD32 ai4_mod_factor_num[2];
1447
1448
8.90k
    ihevce_ipe_ctxt_t *ps_ctxt = ps_master_ctxt->aps_ipe_thrd_ctxt[i4_thrd_id];
1449
8.90k
    ps_ctxt->i4_hevc_qp = i4_curr_frm_qp;
1450
8.90k
    ps_ctxt->i4_quality_preset = i4_quality_preset;
1451
8.90k
    ps_ctxt->i4_temporal_lyr_id = i4_temporal_lyr_id;
1452
8.90k
    ps_ctxt->ps_rc_quant_ctxt = ps_rc_quant_ctxt;
1453
8.90k
    ps_ctxt->i4_qscale =
1454
8.90k
        ps_ctxt->ps_rc_quant_ctxt
1455
8.90k
            ->pi4_qp_to_qscale[i4_curr_frm_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
1456
1457
8.90k
    ps_ctxt->i4_frm_qp = i4_curr_frm_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset;
1458
8.90k
    ps_ctxt->i4_slice_type = i4_slice_type;  //EIID
1459
8.90k
    ps_ctxt->i4_temporal_layer = ps_lap_out->i4_temporal_lyr_id;
1460
8.90k
    ps_ctxt->i4_is_ref_pic = ps_lap_out->i4_is_ref_pic;
1461
8.90k
    ps_ctxt->u4_num_16x16_skips_at_L0_IPE = 0;
1462
8.90k
    ps_ctxt->i4_use_const_lamda_modifier = USE_CONSTANT_LAMBDA_MODIFIER;
1463
8.90k
    ps_ctxt->i4_use_const_lamda_modifier =
1464
8.90k
        ps_ctxt->i4_use_const_lamda_modifier ||
1465
8.90k
        ((ps_stat_prms->s_coding_tools_prms.i4_vqet &
1466
8.90k
          (1 << BITPOS_IN_VQ_TOGGLE_FOR_CONTROL_TOGGLER)) &&
1467
0
         ((ps_stat_prms->s_coding_tools_prms.i4_vqet &
1468
0
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_NOISE_PRESERVATION)) ||
1469
0
          (ps_stat_prms->s_coding_tools_prms.i4_vqet &
1470
0
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_1)) ||
1471
0
          (ps_stat_prms->s_coding_tools_prms.i4_vqet &
1472
0
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_2)) ||
1473
0
          (ps_stat_prms->s_coding_tools_prms.i4_vqet &
1474
0
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_3))));
1475
8.90k
    {
1476
8.90k
        ps_ctxt->f_i_pic_lamda_modifier = ps_lap_out->f_i_pic_lamda_modifier;
1477
8.90k
    }
1478
8.90k
#if POW_OPT
1479
26.7k
    for(i4_i = 0; i4_i < 2; i4_i++)
1480
17.8k
    {
1481
17.8k
        ps_ctxt->ld_curr_frame_8x8_log_avg[i4_i] = ps_curr_out->ld_curr_frame_8x8_log_avg[i4_i];
1482
17.8k
        ps_ctxt->ld_curr_frame_16x16_log_avg[i4_i] = ps_curr_out->ld_curr_frame_16x16_log_avg[i4_i];
1483
17.8k
        ps_ctxt->ld_curr_frame_32x32_log_avg[i4_i] = ps_curr_out->ld_curr_frame_32x32_log_avg[i4_i];
1484
17.8k
    }
1485
1486
8.90k
    ps_ctxt->ld_curr_frame_16x16_log_avg[2] = ps_curr_out->ld_curr_frame_16x16_log_avg[2];
1487
8.90k
    ps_ctxt->ld_curr_frame_32x32_log_avg[2] = ps_curr_out->ld_curr_frame_32x32_log_avg[2];
1488
8.90k
    ps_ctxt->i8_curr_frame_avg_mean_act = ps_curr_out->i8_curr_frame_avg_mean_act;
1489
#else
1490
    for(i4_i = 0; i4_i < 2; i4_i++)
1491
    {
1492
        ps_ctxt->i8_curr_frame_8x8_avg_act[i4_i] = ps_curr_out->i8_curr_frame_8x8_avg_act[i4_i];
1493
        ps_ctxt->i8_curr_frame_16x16_avg_act[i4_i] = ps_curr_out->i8_curr_frame_16x16_avg_act[i4_i];
1494
        ps_ctxt->i8_curr_frame_32x32_avg_act[i4_i] = ps_curr_out->i8_curr_frame_32x32_avg_act[i4_i];
1495
    }
1496
1497
    ps_ctxt->i8_curr_frame_16x16_avg_act[2] = ps_curr_out->i8_curr_frame_16x16_avg_act[2];
1498
    ps_ctxt->i8_curr_frame_32x32_avg_act[2] = ps_curr_out->i8_curr_frame_32x32_avg_act[2];
1499
#endif
1500
1501
8.90k
    ps_ctxt->pi2_trans_out =
1502
8.90k
        (WORD16 *)&ps_ctxt->au1_pred_samples[0];  //overlaying trans coeff memory with pred_samples
1503
8.90k
    ps_ctxt->pi2_trans_tmp = (WORD16 *)&ps_ctxt->au1_pred_samples[2048];
1504
1505
    /*Mod factor NUM */
1506
8.90k
    ps_ctxt->ai4_mod_factor_derived_by_variance[0] =
1507
8.90k
        ps_curr_out->ai4_mod_factor_derived_by_variance[0];
1508
8.90k
    ps_ctxt->ai4_mod_factor_derived_by_variance[1] =
1509
8.90k
        ps_curr_out->ai4_mod_factor_derived_by_variance[1];
1510
1511
8.90k
    ps_ctxt->f_strength = ps_curr_out->f_strength;
1512
1513
8.90k
    if(ps_stat_prms->s_coding_tools_prms.i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_CONTROL_TOGGLER))
1514
0
    {
1515
0
        if(ps_stat_prms->s_coding_tools_prms.i4_vqet &
1516
0
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_NOISE_PRESERVATION))
1517
0
        {
1518
0
            ps_ctxt->i4_enable_noise_detection = 1;
1519
0
        }
1520
0
        else
1521
0
        {
1522
0
            ps_ctxt->i4_enable_noise_detection = 0;
1523
0
        }
1524
0
    }
1525
8.90k
    else
1526
8.90k
    {
1527
8.90k
        ps_ctxt->i4_enable_noise_detection = 0;
1528
8.90k
    }
1529
1530
8.90k
    {
1531
8.90k
        if(ISLICE == ps_ctxt->i4_slice_type)
1532
2.56k
        {
1533
2.56k
            ai4_mod_factor_num[0] = INTRA_QP_MOD_FACTOR_NUM;  //16;
1534
2.56k
            ai4_mod_factor_num[1] = INTRA_QP_MOD_FACTOR_NUM;  //16;
1535
2.56k
        }
1536
6.33k
        else
1537
6.33k
        {
1538
6.33k
            ai4_mod_factor_num[0] = INTER_QP_MOD_FACTOR_NUM;  //4;
1539
6.33k
            ai4_mod_factor_num[1] = INTER_QP_MOD_FACTOR_NUM;  //4;
1540
6.33k
        }
1541
1542
#if ENABLE_QP_MOD_BASED_ON_SPATIAL_VARIANCE
1543
        for(i4_i = 0; i4_i < 2; i4_i++)
1544
        {
1545
            WORD32 mod_factor_num_val =
1546
                ps_ctxt->ai4_mod_factor_derived_by_variance[i4_i] * QP_MOD_FACTOR_DEN;
1547
1548
            ai4_mod_factor_num[i4_i] = CLIP3(mod_factor_num_val, 1, ai4_mod_factor_num[i4_i]);
1549
            ps_ctxt->ai4_mod_factor_derived_by_variance[i4_i] = ai4_mod_factor_num[i4_i];
1550
        }
1551
#else
1552
26.7k
        for(i4_i = 0; i4_i < 2; i4_i++)
1553
17.8k
        {
1554
17.8k
            ps_ctxt->ai4_mod_factor_derived_by_variance[i4_i] = ai4_mod_factor_num[i4_i];
1555
17.8k
        }
1556
8.90k
#endif
1557
8.90k
    }
1558
1559
8.90k
    ps_ctxt->u1_use_lambda_derived_from_min_8x8_act_in_ctb = MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON &&
1560
8.90k
                                                             i1_cu_qp_delta_enabled_flag;
1561
1562
8.90k
    ps_ctxt->u1_use_satd = 1;
1563
8.90k
    ps_ctxt->u1_level_1_refine_on = 1;
1564
8.90k
    ps_ctxt->u1_disable_child_cu_decide = 0;
1565
1566
8.90k
#if !OLD_XTREME_SPEED
1567
8.90k
    if(((ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P5) ||
1568
6.21k
        (ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P6)) &&
1569
3.78k
       (ps_ctxt->i4_slice_type != ISLICE))
1570
3.13k
    {
1571
3.13k
        ps_ctxt->u1_use_satd = 0;
1572
3.13k
        ps_ctxt->u1_level_1_refine_on = 1;
1573
3.13k
        ps_ctxt->u1_disable_child_cu_decide = 0;
1574
3.13k
    }
1575
1576
8.90k
#endif
1577
1578
8.90k
    if((ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P4) && (ps_ctxt->i4_slice_type != ISLICE))
1579
805
        ps_ctxt->u1_use_satd = 0;
1580
8.90k
    if(ps_ctxt->i4_quality_preset > IHEVCE_QUALITY_P3)
1581
6.48k
        ps_ctxt->u1_use_satd = 0;
1582
8.90k
}