Coverage Report

Created: 2025-07-11 06:43

/src/libhevc/encoder/init_qp.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2018 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
/*!
21
******************************************************************************
22
* \file init_qp.c
23
*
24
* \brief
25
*    This file contain qp initialization functions
26
*
27
* \date
28
*
29
* \author
30
*    ittiam
31
*
32
******************************************************************************
33
*/
34
/*****************************************************************************/
35
/* File Includes                                                             */
36
/*****************************************************************************/
37
/* User include files */
38
#include "ittiam_datatypes.h"
39
#include "rc_cntrl_param.h"
40
#include "var_q_operator.h"
41
#include "mem_req_and_acq.h"
42
#include "rc_common.h"
43
#include "init_qp.h"
44
45
typedef struct init_qp_t
46
{
47
    /* WORD32 ai4_bpp_for_qp[MAX_MPEG2_QP]; */
48
    WORD32 i4_max_qp;
49
    WORD32 i4_num_pels_in_frame;
50
    WORD32 i4_is_hbr;
51
} init_qp_t;
52
53
#define BPP_Q_FACTOR (16)
54
127k
#define QP_FOR_ONE_BPP (3) /*(10)*/
55
56
#if NON_STEADSTATE_CODE
57
WORD32 init_qp_num_fill_use_free_memtab(
58
    init_qp_handle *pps_init_qp, itt_memtab_t *ps_memtab, ITT_FUNC_TYPE_E e_func_type)
59
62.4k
{
60
62.4k
    WORD32 i4_mem_tab_idx = 0;
61
62.4k
    static init_qp_t s_init_qp;
62
63
    /* Hack for al alloc, during which we dont have any state memory.
64
      Dereferencing can cause issues */
65
62.4k
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
66
53.5k
        (*pps_init_qp) = &s_init_qp;
67
68
    /*for src rate control state structure*/
69
62.4k
    if(e_func_type != GET_NUM_MEMTAB)
70
26.7k
    {
71
26.7k
        fill_memtab(
72
26.7k
            &ps_memtab[i4_mem_tab_idx], sizeof(init_qp_t), MEM_TAB_ALIGNMENT, PERSISTENT, DDR);
73
26.7k
        use_or_fill_base(&ps_memtab[0], (void **)pps_init_qp, e_func_type);
74
26.7k
    }
75
62.4k
    i4_mem_tab_idx++;
76
77
62.4k
    return (i4_mem_tab_idx);
78
62.4k
}
79
80
/****************************************************************************
81
Function Name : init_init_qp
82
Description   :
83
Inputs        : ps_init_qp
84
85
Revision History:
86
DD MM YYYY   Author(s)       Changes (Describe the changes made)
87
 *****************************************************************************/
88
void init_init_qp(
89
    init_qp_handle ps_init_qp, WORD32 *pi4_min_max_qp, WORD32 i4_num_pels_in_frame, WORD32 i4_is_hbr)
90
8.92k
{
91
8.92k
    WORD32 i4_max_qp;
92
    /* Finding the max qp among I P and B frame */
93
8.92k
    i4_max_qp = pi4_min_max_qp[1];
94
8.92k
    if(i4_max_qp < pi4_min_max_qp[3])
95
0
        i4_max_qp = pi4_min_max_qp[3];
96
8.92k
    if(i4_max_qp < pi4_min_max_qp[5])
97
0
        i4_max_qp = pi4_min_max_qp[5];
98
99
    /*for(i=0;i<i4_max_qp;i++)
100
    {
101
        ps_init_qp->ai4_bpp_for_qp[i] = (QP_FOR_ONE_BPP*(1<<BPP_Q_FACTOR))/(i+1);
102
    }*/
103
8.92k
    ps_init_qp->i4_max_qp = i4_max_qp;
104
8.92k
    ps_init_qp->i4_num_pels_in_frame = (!i4_num_pels_in_frame) ? 1 : i4_num_pels_in_frame;
105
8.92k
    ps_init_qp->i4_is_hbr = i4_is_hbr;
106
8.92k
}
107
#endif /* #if NON_STEADSTATE_CODE */
108
109
/* To ensure init_qp for high bit rates is low */
110
0
#define QP_FOR_ONE_BPP_HBR (5)
111
112
/****************************************************************************
113
Function Name : get_init_qp_using_pels_bits_per_frame
114
Description   :
115
Inputs        : ps_init_qp
116
117
Revision History:
118
DD MM YYYY   Author(s)       Changes (Describe the changes made)
119
 *****************************************************************************/
120
/* If the remaining pels in frame is zero we would be using the init time pixels for calculating the bits per pixel */
121
WORD32 get_init_qp_using_pels_bits_per_frame(
122
    init_qp_handle ps_init_qp,
123
    picture_type_e e_pic_type,
124
    WORD32 i4_bits_remaining_in_frame,
125
    WORD32 i4_rem_pels_in_frame)
126
127k
{
127
127k
    WORD32 i4_qp;
128
127k
    WORD32 i4_qp_for_one_bpp;
129
130
127k
    if(ps_init_qp->i4_is_hbr)
131
0
    {
132
0
        i4_qp_for_one_bpp = QP_FOR_ONE_BPP_HBR;
133
0
    }
134
127k
    else
135
127k
    {
136
127k
        i4_qp_for_one_bpp = QP_FOR_ONE_BPP;
137
127k
    }
138
139
127k
    if(!i4_rem_pels_in_frame)
140
0
        i4_rem_pels_in_frame = ps_init_qp->i4_num_pels_in_frame;
141
127k
    if(e_pic_type == P_PIC || e_pic_type == P1_PIC)
142
0
        i4_bits_remaining_in_frame = i4_bits_remaining_in_frame * I_TO_P_BIT_RATIO;
143
127k
    if(e_pic_type >= B_PIC && e_pic_type != P1_PIC)
144
0
        i4_bits_remaining_in_frame =
145
0
            i4_bits_remaining_in_frame * (I_TO_P_BIT_RATIO * P_TO_B_BIT_RATIO);
146
147
    /* Assuming a 1 bpp => Qp = 12, So Qp = 1 => 12 bpp. [bpp halves with every doubling of Qp] */
148
    /* x bpp =  i4_bits_remaining_in_frame/i4_rem_pels_in_frame
149
       1 bpp = QP_FOR_ONE_BPP
150
       QP_FOR_X_BPP = QP_FOR_ONE_BPP/(x) = QP_FOR_ONE_BPP*i4_rem_pels_in_frame/i4_bits_remaining_in_frame */
151
127k
    X_PROD_Y_DIV_Z(i4_qp_for_one_bpp, i4_rem_pels_in_frame, i4_bits_remaining_in_frame, i4_qp);
152
153
    /* Scaling the Qp values based on picture type */
154
127k
    if(e_pic_type == P_PIC || e_pic_type == P1_PIC)
155
0
        i4_qp = ((i4_qp * I_TO_P_RATIO) >> K_Q);
156
157
127k
    if(e_pic_type >= B_PIC && e_pic_type != P1_PIC)
158
0
    {
159
0
        if(!ps_init_qp->i4_is_hbr)
160
0
        {
161
0
            i4_qp = ((i4_qp * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q));
162
0
        }
163
0
        else
164
0
        {
165
0
            i4_qp = ((i4_qp * P_TO_B_RATIO_HBR * I_TO_P_RATIO) >> (K_Q + K_Q));
166
0
        }
167
0
    }
168
169
127k
    if(i4_qp > ps_init_qp->i4_max_qp)
170
232
        i4_qp = ps_init_qp->i4_max_qp;
171
127k
    else if(i4_qp == 0)
172
112k
        i4_qp = 1;
173
174
127k
    return i4_qp;
175
127k
}
176
177
#if NON_STEADSTATE_CODE
178
/****************************************************************************
179
Function Name : change_init_qp_max_qp
180
Description   :
181
Inputs        : ps_init_qp
182
183
Revision History:
184
DD MM YYYY   Author(s)       Changes (Describe the changes made)
185
 *****************************************************************************/
186
void change_init_qp_max_qp(init_qp_handle ps_init_qp, WORD32 *pi4_min_max_qp)
187
0
{
188
0
    WORD32 i4_max_qp;
189
    /* Finding the max qp among I P and B frame */
190
0
    i4_max_qp = pi4_min_max_qp[1];
191
0
    if(i4_max_qp < pi4_min_max_qp[3])
192
0
        i4_max_qp = pi4_min_max_qp[3];
193
0
    if(i4_max_qp < pi4_min_max_qp[5])
194
0
        i4_max_qp = pi4_min_max_qp[5];
195
196
0
    ps_init_qp->i4_max_qp = i4_max_qp;
197
0
}
198
#endif /* #if NON_STEADSTATE_CODE */