Coverage Report

Created: 2026-01-09 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_frame_process_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_frame_process_utils.c
24
*
25
* \brief
26
*    This file contains definitions of top level functions related to frame
27
*    processing
28
*
29
* \date
30
*    18/09/2012
31
*
32
* \author
33
*    Ittiam
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_defs.h"
62
#include "ihevc_debug.h"
63
#include "ihevc_macros.h"
64
#include "ihevc_structs.h"
65
#include "ihevc_platform_macros.h"
66
#include "ihevc_deblk.h"
67
#include "ihevc_itrans_recon.h"
68
#include "ihevc_chroma_itrans_recon.h"
69
#include "ihevc_chroma_intra_pred.h"
70
#include "ihevc_intra_pred.h"
71
#include "ihevc_inter_pred.h"
72
#include "ihevc_mem_fns.h"
73
#include "ihevc_padding.h"
74
#include "ihevc_weighted_pred.h"
75
#include "ihevc_sao.h"
76
#include "ihevc_resi_trans.h"
77
#include "ihevc_quant_iquant_ssd.h"
78
#include "ihevc_cabac_tables.h"
79
#include "ihevc_common_tables.h"
80
81
#include "ihevce_defs.h"
82
#include "ihevce_hle_interface.h"
83
#include "ihevce_hle_q_func.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_checks.h"
90
#include "ihevce_error_codes.h"
91
#include "ihevce_bitstream.h"
92
#include "ihevce_cabac.h"
93
#include "ihevce_function_selector.h"
94
#include "ihevce_enc_structs.h"
95
#include "ihevce_global_tables.h"
96
#include "ihevce_rc_enc_structs.h"
97
#include "ihevce_rc_interface.h"
98
#include "ihevce_frame_process_utils.h"
99
100
#include "cast_types.h"
101
#include "osal.h"
102
#include "osal_defaults.h"
103
104
/*****************************************************************************/
105
/* Globals                                                                   */
106
/*****************************************************************************/
107
108
/************** Version Number string *******************/
109
UWORD8 gau1_version_string[] = "i265-v4.13-218 Build ";
110
111
/*****************************************************************************/
112
/* Function Definitions                                                      */
113
/*****************************************************************************/
114
115
/*!
116
******************************************************************************
117
*
118
* @brief
119
*    API to return frame qp in constant qp mode based on init I frame qp,
120
*    slice type and current temporal layer.
121
*
122
*      I picture is given the same qp as the init qp configure in static params
123
*      P picture is set equal to I frame qp + 1
124
*      B picture is set equal to P frame qp + temporal layer
125
*
126
* @param[in] static_params_frame_qp
127
*   frame level qp set for I frames in create time params
128
*
129
* @param[in] slice_type
130
*   slice type for current frame (I/P/B)
131
*
132
* @param[in] temporal_id
133
*   temoporal layer ID of the current frame. This is associalted with B frame.
134
*   temporal layer ID. I and P frames have temporal_id set to 0.
135
*
136
* @param[in] min_qp
137
*   minimum qp to be allocated for this frame.
138
*
139
* @param[in] max_qp
140
*   maximum qp to be allocated for this frame
141
*
142
* @return
143
*    current frame qp
144
*
145
* @author
146
*  Ittiam
147
*
148
* @remarks
149
*  This is right place to plug in frame level RC call for current frame qp
150
*  allocation later when RC support is added
151
*
152
*****************************************************************************
153
*/
154
WORD32 ihevce_get_cur_frame_qp(
155
    WORD32 static_params_frame_qp,
156
    WORD32 slice_type,
157
    WORD32 temporal_id,
158
    WORD32 min_qp,
159
    WORD32 max_qp,
160
    rc_quant_t *ps_rc_quant_ctxt)
161
6.71k
{
162
6.71k
    WORD32 i4_curr_qp = static_params_frame_qp;
163
164
    /* sanity checks */
165
6.71k
    ASSERT(max_qp >= min_qp);
166
6.71k
    ASSERT((min_qp >= ps_rc_quant_ctxt->i2_min_qp) && (min_qp <= ps_rc_quant_ctxt->i2_max_qp));
167
6.71k
    ASSERT(
168
6.71k
        (static_params_frame_qp >= ps_rc_quant_ctxt->i2_min_qp) &&
169
6.71k
        (static_params_frame_qp <= ps_rc_quant_ctxt->i2_max_qp));
170
6.71k
    if(ISLICE == slice_type)
171
1.33k
    {
172
        /* I frame qp is same as init qp in static params   */
173
1.33k
        i4_curr_qp = static_params_frame_qp;
174
1.33k
    }
175
5.37k
    else if(PSLICE == slice_type)
176
3.36k
    {
177
        /* P frame qp is I frame qp + 1                     */
178
3.36k
        i4_curr_qp = static_params_frame_qp + 1;
179
3.36k
    }
180
2.01k
    else if(BSLICE == slice_type)
181
2.01k
    {
182
        /* B frame qp is I frame qp + 1 + temporal layer id */
183
2.01k
        i4_curr_qp = static_params_frame_qp + temporal_id + 1;
184
2.01k
    }
185
0
    else
186
0
    {
187
        /* illegal slice type */
188
0
        ASSERT(0);
189
0
    }
190
191
6.71k
    i4_curr_qp = CLIP3(i4_curr_qp, min_qp, max_qp);
192
193
6.71k
    return (i4_curr_qp);
194
6.71k
}
195
196
/*!
197
******************************************************************************
198
* \if Function name : calc_block_ssim \endif
199
*
200
* \brief
201
*    Calc Block SSIM
202
*
203
* \return
204
*    None
205
*
206
* \author
207
*  Ittiam
208
*****************************************************************************
209
*/
210
unsigned int calc_block_ssim(
211
    unsigned char *pu1_ref,
212
    unsigned char *pu1_tst,
213
    unsigned char *pu1_win,
214
    WORD32 i4_horz_jump,
215
    unsigned short u2_ref_stride,
216
    unsigned short u2_tst_stride,
217
    unsigned char u1_win_size,
218
    unsigned char u1_win_q_shift)
219
0
{
220
0
    unsigned int u4_wtd_ref_mean, u4_wtd_tst_mean, u4_wtd_ref_sq, u4_wtd_tst_sq, u4_wtd_ref_tst;
221
0
    unsigned int u4_wtd_ref_mean_sq, u4_wtd_tst_mean_sq, u4_wtd_ref_tst_mean_prod;
222
0
    unsigned char u1_wt, u1_ref_smpl, u1_tst_smpl;
223
0
    unsigned short u2_wtd_ref_smpl, u2_wtd_tst_smpl, u2_win_q_rounding;
224
0
    int i4_row, i4_col;
225
226
0
    u4_wtd_ref_mean = 0;
227
0
    u4_wtd_tst_mean = 0;
228
0
    u4_wtd_ref_sq = 0;
229
0
    u4_wtd_tst_sq = 0;
230
0
    u4_wtd_ref_tst = 0;
231
232
0
    for(i4_row = 0; i4_row < u1_win_size; i4_row++)
233
0
    {
234
0
        for(i4_col = 0; i4_col < u1_win_size; i4_col++)
235
0
        {
236
0
            u1_wt = *pu1_win++;
237
0
            u1_ref_smpl = pu1_ref[i4_col * i4_horz_jump];
238
0
            u1_tst_smpl = pu1_tst[i4_col * i4_horz_jump];
239
240
0
            u2_wtd_ref_smpl = u1_wt * u1_ref_smpl;
241
0
            u2_wtd_tst_smpl = u1_wt * u1_tst_smpl;
242
243
0
            u4_wtd_ref_mean += u2_wtd_ref_smpl;
244
0
            u4_wtd_tst_mean += u2_wtd_tst_smpl;
245
246
0
            u4_wtd_ref_sq += u2_wtd_ref_smpl * u1_ref_smpl;
247
0
            u4_wtd_tst_sq += u2_wtd_tst_smpl * u1_tst_smpl;
248
0
            u4_wtd_ref_tst += u2_wtd_ref_smpl * u1_tst_smpl;
249
0
        }
250
0
        pu1_ref += u2_ref_stride;
251
0
        pu1_tst += u2_tst_stride;
252
0
    }
253
254
0
    {
255
0
        unsigned int u4_num, u4_den, u4_term1;
256
257
0
        u2_win_q_rounding = (1 << u1_win_q_shift) >> 1;
258
0
        u4_wtd_ref_mean += (u2_win_q_rounding >> 8);
259
0
        u4_wtd_tst_mean += (u2_win_q_rounding >> 8);
260
261
        /* Keep the mean terms within 16-bits before squaring */
262
0
        u4_wtd_ref_mean >>= (u1_win_q_shift - 8);
263
0
        u4_wtd_tst_mean >>= (u1_win_q_shift - 8);
264
265
        /* Bring down the square of sum terms to same Q format as the sum of square terms */
266
0
        u4_wtd_ref_mean_sq = (u4_wtd_ref_mean * u4_wtd_ref_mean + 16) >> (16 - u1_win_q_shift);
267
0
        u4_wtd_tst_mean_sq = (u4_wtd_tst_mean * u4_wtd_tst_mean + 16) >> (16 - u1_win_q_shift);
268
0
        u4_wtd_ref_tst_mean_prod = (u4_wtd_ref_mean * u4_wtd_tst_mean + 16) >>
269
0
                                   (16 - u1_win_q_shift);
270
271
        /* Compute self and cross variances */
272
0
        if(u4_wtd_ref_sq > u4_wtd_ref_mean_sq)
273
0
            u4_wtd_ref_sq -= u4_wtd_ref_mean_sq;
274
0
        else
275
0
            u4_wtd_ref_sq = 0;
276
277
0
        if(u4_wtd_tst_sq > u4_wtd_tst_mean_sq)
278
0
            u4_wtd_tst_sq -= u4_wtd_tst_mean_sq;
279
0
        else
280
0
            u4_wtd_tst_sq = 0;
281
282
0
        if(u4_wtd_ref_tst > u4_wtd_ref_tst_mean_prod)
283
0
            u4_wtd_ref_tst -= u4_wtd_ref_tst_mean_prod;
284
0
        else
285
0
            u4_wtd_ref_tst = 0;
286
287
        /* Keep the numerator in Q12 format before division */
288
0
        u4_num = ((u4_wtd_ref_tst_mean_prod << 1) + C1) << (12 - u1_win_q_shift);
289
0
        u4_den = ((u4_wtd_ref_mean_sq + u4_wtd_tst_mean_sq) + C1 + u2_win_q_rounding) >>
290
0
                 u1_win_q_shift;
291
0
        u4_term1 = (u4_num) / u4_den;
292
293
0
        u4_num = (u4_wtd_ref_tst << 1) + C2;
294
0
        u4_den = (u4_wtd_ref_sq + u4_wtd_tst_sq) + C2;
295
        /* If numerator takes less than 20-bits, product would not overflow; so no need to normalize */
296
0
        if(u4_num < 1048576)
297
0
        {
298
0
            return ((u4_num * u4_term1) / u4_den);
299
0
        }
300
301
        /* While the above should be done really with getRange calculation, for simplicity,
302
        the other cases go through a less accurate calculation */
303
0
        u4_num = (u4_num + u2_win_q_rounding) >> u1_win_q_shift;
304
0
        u4_den = (u4_den + u2_win_q_rounding) >> u1_win_q_shift;
305
306
        /* What is returned is SSIM in 1Q12 */
307
0
        return ((u4_term1 * u4_num) / u4_den);
308
0
    }
309
0
}
310
311
#ifndef DISABLE_SEI
312
/*!
313
******************************************************************************
314
* \if Function name : ihevce_fill_sei_payload \endif
315
*
316
* \brief
317
*    Fills SEI Payload
318
*
319
* \param[in]    ps_enc_ctxt
320
* Encoder Context
321
*
322
* \param[in]    ps_curr_inp
323
* Current Input pointer
324
*
325
* \param[in]    ps_curr_out
326
* Current Output pointer
327
*
328
* \return
329
*    None
330
*
331
* \author
332
*  Ittiam
333
*
334
*****************************************************************************
335
*/
336
void ihevce_fill_sei_payload(
337
    enc_ctxt_t *ps_enc_ctxt,
338
    ihevce_lap_enc_buf_t *ps_curr_inp,
339
    frm_proc_ent_cod_ctxt_t *ps_curr_out)
340
0
{
341
0
    UWORD32 *pu4_length, i4_cmd_len;
342
0
    UWORD32 *pu4_tag, i4_pic_type;
343
0
    UWORD8 *pu1_user_data;
344
345
0
    pu4_tag = ((UWORD32 *)(ps_curr_inp->s_input_buf.pv_synch_ctrl_bufs));
346
0
    ps_curr_out->u4_num_sei_payload = 0;
347
0
    i4_pic_type = ps_curr_inp->s_lap_out.i4_pic_type;
348
0
    (void)ps_enc_ctxt;
349
0
    while(1)
350
0
    {
351
0
        if(((*pu4_tag) & IHEVCE_COMMANDS_TAG_MASK) == IHEVCE_SYNCH_API_END_TAG)
352
0
            break;
353
354
0
        pu4_length = pu4_tag + 1;
355
0
        pu1_user_data = (UWORD8 *)(pu4_length + 1);
356
0
        i4_cmd_len = *pu4_length;
357
358
0
        if((*pu4_tag & IHEVCE_COMMANDS_TAG_MASK) == IHEVCE_SYNCH_API_REG_KEYFRAME_SEI_TAG)
359
0
        {
360
0
            if(i4_pic_type == IV_IDR_FRAME)
361
0
            {
362
0
                memcpy(
363
0
                    (void *)((ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload]
364
0
                                  .pu1_sei_payload)),
365
0
                    (void *)pu1_user_data,
366
0
                    i4_cmd_len);
367
0
                ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_length =
368
0
                    (i4_cmd_len);
369
0
                ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_type =
370
0
                    ((*pu4_tag & IHEVCE_PAYLOAD_TYPE_MASK) >> IHEVCE_PAYLOAD_TYPE_SHIFT);
371
0
                ps_curr_out->u4_num_sei_payload++;
372
0
            }
373
0
        }
374
0
        else if((*pu4_tag & IHEVCE_COMMANDS_TAG_MASK) == IHEVCE_SYNCH_API_REG_ALLFRAME_SEI_TAG)
375
0
        {
376
0
            memcpy(
377
0
                (void *)((
378
0
                    ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].pu1_sei_payload)),
379
0
                (void *)pu1_user_data,
380
0
                i4_cmd_len);
381
0
            ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_length =
382
0
                (i4_cmd_len);
383
0
            ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_type =
384
0
                ((*pu4_tag & IHEVCE_PAYLOAD_TYPE_MASK) >> IHEVCE_PAYLOAD_TYPE_SHIFT);
385
0
            ps_curr_out->u4_num_sei_payload++;
386
0
        }
387
388
        //The formula (((x-1)>>2)+1) gives us the ceiling of (x mod 4). Hence this will take the pointer to the next address boundary divisible by 4.
389
        //And then we add 2 bytes for the tag and the payload length.
390
0
        if(i4_cmd_len)
391
0
            pu4_tag += (((i4_cmd_len - 1) >> 2) + 1 + 2);
392
0
        else
393
0
            pu4_tag += 2;
394
0
    }
395
0
}
396
#endif
397
398
/*!
399
******************************************************************************
400
* \if Function name : ihevce_dyn_bitrate \endif
401
*
402
* \brief
403
*    Call back function to be called for changing the bitrate
404
*
405
*
406
* \return
407
*    None
408
*
409
* \author
410
*  Ittiam
411
*
412
*****************************************************************************
413
*/
414
void ihevce_dyn_bitrate(void *pv_hle_ctxt, void *pv_dyn_bitrate_prms)
415
271
{
416
271
    ihevce_hle_ctxt_t *ps_hle_ctxt = (ihevce_hle_ctxt_t *)pv_hle_ctxt;
417
271
    ihevce_dyn_config_prms_t *ps_dyn_bitrate_prms = (ihevce_dyn_config_prms_t *)pv_dyn_bitrate_prms;
418
271
    enc_ctxt_t *ps_enc_ctxt =
419
271
        (enc_ctxt_t *)ps_hle_ctxt->apv_enc_hdl[ps_dyn_bitrate_prms->i4_tgt_res_id];
420
271
    ihevce_static_cfg_params_t *ps_static_cfg_params = ps_hle_ctxt->ps_static_cfg_prms;
421
422
271
    if(ps_enc_ctxt->ps_stat_prms->i4_log_dump_level > 0)
423
0
    {
424
0
        ps_static_cfg_params->s_sys_api.ihevce_printf(
425
0
            ps_static_cfg_params->s_sys_api.pv_cb_handle,
426
0
            "\n Average Bitrate changed to %d",
427
0
            ps_dyn_bitrate_prms->i4_new_tgt_bitrate);
428
0
        ps_static_cfg_params->s_sys_api.ihevce_printf(
429
0
            ps_static_cfg_params->s_sys_api.pv_cb_handle,
430
0
            "\n Peak    Bitrate changed to %d",
431
0
            ps_dyn_bitrate_prms->i4_new_peak_bitrate);
432
0
    }
433
434
435
    /* acquire mutex lock for rate control calls */
436
271
    osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
437
438
271
    ihevce_rc_register_dyn_change_bitrate(
439
271
        ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[ps_dyn_bitrate_prms->i4_tgt_br_id],
440
271
        (LWORD64)ps_dyn_bitrate_prms->i4_new_tgt_bitrate,
441
271
        (LWORD64)ps_dyn_bitrate_prms->i4_new_peak_bitrate);
442
443
    /*unlock rate control context*/
444
271
    osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
445
271
    return;
446
271
}
447
448
/*!
449
******************************************************************************
450
* \if Function name : ihevce_validate_encoder_parameters \endif
451
*
452
* \brief
453
*    Call back function to be called for changing the bitrate
454
*
455
* \return
456
*    None
457
*
458
* \author
459
*  Ittiam
460
*****************************************************************************
461
*/
462
WORD32 ihevce_validate_encoder_parameters(ihevce_static_cfg_params_t *ps_static_cfg_prms)
463
0
{
464
0
    return (ihevce_hle_validate_static_params(ps_static_cfg_prms));
465
0
}
466
467
/*!
468
******************************************************************************
469
* \if Function name : ihevce_get_encoder_version \endif
470
*
471
* \brief
472
*    Call back function to be called for changing the bitrate
473
*
474
* \return
475
*    None
476
*
477
* \author
478
*  Ittiam
479
*****************************************************************************
480
*/
481
const char *ihevce_get_encoder_version()
482
319
{
483
319
    return ((const char *)gau1_version_string);
484
319
}