xref: /aosp_15_r20/external/libavc/encoder/irc_vbr_str_prms.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1*495ae853SAndroid Build Coastguard Worker /******************************************************************************
2*495ae853SAndroid Build Coastguard Worker  *
3*495ae853SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
4*495ae853SAndroid Build Coastguard Worker  *
5*495ae853SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*495ae853SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*495ae853SAndroid Build Coastguard Worker  * You may obtain a copy of the License at:
8*495ae853SAndroid Build Coastguard Worker  *
9*495ae853SAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
10*495ae853SAndroid Build Coastguard Worker  *
11*495ae853SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*495ae853SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*495ae853SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*495ae853SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*495ae853SAndroid Build Coastguard Worker  * limitations under the License.
16*495ae853SAndroid Build Coastguard Worker  *
17*495ae853SAndroid Build Coastguard Worker  *****************************************************************************
18*495ae853SAndroid Build Coastguard Worker  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19*495ae853SAndroid Build Coastguard Worker */
20*495ae853SAndroid Build Coastguard Worker 
21*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
22*495ae853SAndroid Build Coastguard Worker /* Includes */
23*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
24*495ae853SAndroid Build Coastguard Worker 
25*495ae853SAndroid Build Coastguard Worker /* System include files */
26*495ae853SAndroid Build Coastguard Worker #include <stdio.h>
27*495ae853SAndroid Build Coastguard Worker 
28*495ae853SAndroid Build Coastguard Worker /* User include files */
29*495ae853SAndroid Build Coastguard Worker #include "irc_datatypes.h"
30*495ae853SAndroid Build Coastguard Worker #include "irc_cntrl_param.h"
31*495ae853SAndroid Build Coastguard Worker #include "irc_vbr_str_prms.h"
32*495ae853SAndroid Build Coastguard Worker 
33*495ae853SAndroid Build Coastguard Worker /******************************************************************************
34*495ae853SAndroid Build Coastguard Worker  Function Name   : irc_init_vbv_str_prms
35*495ae853SAndroid Build Coastguard Worker  Description     : Initializes and calculates the number of I frame and P frames
36*495ae853SAndroid Build Coastguard Worker                    in the delay period
37*495ae853SAndroid Build Coastguard Worker  Return Values   : void
38*495ae853SAndroid Build Coastguard Worker  *****************************************************************************/
irc_init_vbv_str_prms(vbr_str_prms_t * p_vbr_str_prms,UWORD32 u4_intra_frm_interval,UWORD32 u4_src_ticks,UWORD32 u4_tgt_ticks,UWORD32 u4_frms_in_delay_period)39*495ae853SAndroid Build Coastguard Worker void irc_init_vbv_str_prms(vbr_str_prms_t *p_vbr_str_prms,
40*495ae853SAndroid Build Coastguard Worker                            UWORD32 u4_intra_frm_interval,
41*495ae853SAndroid Build Coastguard Worker                            UWORD32 u4_src_ticks,
42*495ae853SAndroid Build Coastguard Worker                            UWORD32 u4_tgt_ticks,
43*495ae853SAndroid Build Coastguard Worker                            UWORD32 u4_frms_in_delay_period)
44*495ae853SAndroid Build Coastguard Worker {
45*495ae853SAndroid Build Coastguard Worker 
46*495ae853SAndroid Build Coastguard Worker     UWORD32 i4_num_i_frms_in_delay_per, i4_num_p_frms_in_delay_per;
47*495ae853SAndroid Build Coastguard Worker 
48*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_frms_in_delay_prd = u4_frms_in_delay_period;
49*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_src_ticks = u4_src_ticks;
50*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_tgt_ticks = u4_tgt_ticks;
51*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_intra_frame_int = u4_intra_frm_interval;
52*495ae853SAndroid Build Coastguard Worker 
53*495ae853SAndroid Build Coastguard Worker     /*
54*495ae853SAndroid Build Coastguard Worker      * Finding the number of I frames and P frames in delay period. This
55*495ae853SAndroid Build Coastguard Worker      * value along with the drain rates for the corresponding picture types will
56*495ae853SAndroid Build Coastguard Worker      * be used to calculate the buffer sizes
57*495ae853SAndroid Build Coastguard Worker      */
58*495ae853SAndroid Build Coastguard Worker     i4_num_i_frms_in_delay_per = ((u4_frms_in_delay_period * u4_src_ticks)
59*495ae853SAndroid Build Coastguard Worker                     / (u4_intra_frm_interval * u4_tgt_ticks));
60*495ae853SAndroid Build Coastguard Worker 
61*495ae853SAndroid Build Coastguard Worker     /* Ceiling the above result*/
62*495ae853SAndroid Build Coastguard Worker     if((i4_num_i_frms_in_delay_per * u4_intra_frm_interval * u4_tgt_ticks)
63*495ae853SAndroid Build Coastguard Worker                     < (u4_frms_in_delay_period * u4_src_ticks))
64*495ae853SAndroid Build Coastguard Worker     {
65*495ae853SAndroid Build Coastguard Worker         i4_num_i_frms_in_delay_per++;
66*495ae853SAndroid Build Coastguard Worker 
67*495ae853SAndroid Build Coastguard Worker     }
68*495ae853SAndroid Build Coastguard Worker     i4_num_p_frms_in_delay_per = u4_frms_in_delay_period
69*495ae853SAndroid Build Coastguard Worker                     - i4_num_i_frms_in_delay_per;
70*495ae853SAndroid Build Coastguard Worker 
71*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_num_pics_in_delay_prd[I_PIC] =
72*495ae853SAndroid Build Coastguard Worker                     i4_num_i_frms_in_delay_per;
73*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_num_pics_in_delay_prd[P_PIC] =
74*495ae853SAndroid Build Coastguard Worker                     i4_num_p_frms_in_delay_per;
75*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_intra_prd_pos_in_tgt_ticks = (u4_intra_frm_interval
76*495ae853SAndroid Build Coastguard Worker                     * (p_vbr_str_prms->u4_num_pics_in_delay_prd[I_PIC]))
77*495ae853SAndroid Build Coastguard Worker                     * u4_tgt_ticks;
78*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_pic_num = 0;
79*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_cur_pos_in_src_ticks = 0;
80*495ae853SAndroid Build Coastguard Worker }
81*495ae853SAndroid Build Coastguard Worker 
irc_get_vsp_num_pics_in_dly_prd(vbr_str_prms_t * p_vbr_str_prms,UWORD32 * pu4_num_pics_in_delay_prd)82*495ae853SAndroid Build Coastguard Worker WORD32 irc_get_vsp_num_pics_in_dly_prd(vbr_str_prms_t *p_vbr_str_prms,
83*495ae853SAndroid Build Coastguard Worker                                        UWORD32 *pu4_num_pics_in_delay_prd)
84*495ae853SAndroid Build Coastguard Worker {
85*495ae853SAndroid Build Coastguard Worker     pu4_num_pics_in_delay_prd[I_PIC] =
86*495ae853SAndroid Build Coastguard Worker                     p_vbr_str_prms->u4_num_pics_in_delay_prd[I_PIC];
87*495ae853SAndroid Build Coastguard Worker     pu4_num_pics_in_delay_prd[P_PIC] =
88*495ae853SAndroid Build Coastguard Worker                     p_vbr_str_prms->u4_num_pics_in_delay_prd[P_PIC];
89*495ae853SAndroid Build Coastguard Worker     return (p_vbr_str_prms->u4_frms_in_delay_prd);
90*495ae853SAndroid Build Coastguard Worker }
91*495ae853SAndroid Build Coastguard Worker 
92*495ae853SAndroid Build Coastguard Worker /******************************************************************************
93*495ae853SAndroid Build Coastguard Worker  Function Name   : irc_update_vbr_str_prms
94*495ae853SAndroid Build Coastguard Worker  Description     : update the number of I frames and P/B frames in the delay period
95*495ae853SAndroid Build Coastguard Worker                    for buffer size calculations
96*495ae853SAndroid Build Coastguard Worker  *****************************************************************************/
irc_update_vbr_str_prms(vbr_str_prms_t * p_vbr_str_prms,picture_type_e e_pic_type)97*495ae853SAndroid Build Coastguard Worker void irc_update_vbr_str_prms(vbr_str_prms_t *p_vbr_str_prms,
98*495ae853SAndroid Build Coastguard Worker                              picture_type_e e_pic_type)
99*495ae853SAndroid Build Coastguard Worker {
100*495ae853SAndroid Build Coastguard Worker     /*
101*495ae853SAndroid Build Coastguard Worker      * Updating the number of I frames and P frames after encoding every
102*495ae853SAndroid Build Coastguard Worker      * picture. These values along with the drain rates for the corresponding
103*495ae853SAndroid Build Coastguard Worker      * picture  types will be used to calculate the CBR buffer size every frame
104*495ae853SAndroid Build Coastguard Worker      */
105*495ae853SAndroid Build Coastguard Worker 
106*495ae853SAndroid Build Coastguard Worker     if(e_pic_type == I_PIC)
107*495ae853SAndroid Build Coastguard Worker     {
108*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_num_pics_in_delay_prd[I_PIC]--;
109*495ae853SAndroid Build Coastguard Worker     }
110*495ae853SAndroid Build Coastguard Worker     else
111*495ae853SAndroid Build Coastguard Worker     {
112*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_num_pics_in_delay_prd[P_PIC]--;
113*495ae853SAndroid Build Coastguard Worker     }
114*495ae853SAndroid Build Coastguard Worker 
115*495ae853SAndroid Build Coastguard Worker     /* If the next I frame falls within the delay period, we need to increment
116*495ae853SAndroid Build Coastguard Worker      * the number of I frames in the period, else increment the number of P
117*495ae853SAndroid Build Coastguard Worker      * frames
118*495ae853SAndroid Build Coastguard Worker      */
119*495ae853SAndroid Build Coastguard Worker     if((p_vbr_str_prms->u4_cur_pos_in_src_ticks
120*495ae853SAndroid Build Coastguard Worker                     + (p_vbr_str_prms->u4_frms_in_delay_prd
121*495ae853SAndroid Build Coastguard Worker                                     * p_vbr_str_prms->u4_src_ticks))
122*495ae853SAndroid Build Coastguard Worker                     >= p_vbr_str_prms->u4_intra_prd_pos_in_tgt_ticks)
123*495ae853SAndroid Build Coastguard Worker     {
124*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_intra_prd_pos_in_tgt_ticks -=
125*495ae853SAndroid Build Coastguard Worker                         p_vbr_str_prms->u4_cur_pos_in_src_ticks;
126*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_intra_prd_pos_in_tgt_ticks +=
127*495ae853SAndroid Build Coastguard Worker                         p_vbr_str_prms->u4_intra_frame_int
128*495ae853SAndroid Build Coastguard Worker                                         * p_vbr_str_prms->u4_tgt_ticks;
129*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_num_pics_in_delay_prd[I_PIC]++;
130*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_pic_num = 0;
131*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_cur_pos_in_src_ticks = 0;
132*495ae853SAndroid Build Coastguard Worker     }
133*495ae853SAndroid Build Coastguard Worker     else
134*495ae853SAndroid Build Coastguard Worker     {
135*495ae853SAndroid Build Coastguard Worker         p_vbr_str_prms->u4_num_pics_in_delay_prd[P_PIC]++;
136*495ae853SAndroid Build Coastguard Worker     }
137*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_pic_num++;
138*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_cur_pos_in_src_ticks += p_vbr_str_prms->u4_src_ticks;
139*495ae853SAndroid Build Coastguard Worker }
140*495ae853SAndroid Build Coastguard Worker 
irc_get_vsp_src_tgt_ticks(vbr_str_prms_t * p_vbr_str_prms,UWORD32 * pu4_src_ticks,UWORD32 * pu4_tgt_ticks)141*495ae853SAndroid Build Coastguard Worker void irc_get_vsp_src_tgt_ticks(vbr_str_prms_t *p_vbr_str_prms,
142*495ae853SAndroid Build Coastguard Worker                                UWORD32 *pu4_src_ticks,
143*495ae853SAndroid Build Coastguard Worker                                UWORD32 *pu4_tgt_ticks)
144*495ae853SAndroid Build Coastguard Worker {
145*495ae853SAndroid Build Coastguard Worker     pu4_src_ticks[0] = p_vbr_str_prms->u4_src_ticks;
146*495ae853SAndroid Build Coastguard Worker     pu4_tgt_ticks[0] = p_vbr_str_prms->u4_tgt_ticks;
147*495ae853SAndroid Build Coastguard Worker }
148*495ae853SAndroid Build Coastguard Worker 
149*495ae853SAndroid Build Coastguard Worker /*******************************************************************************
150*495ae853SAndroid Build Coastguard Worker  Function Name   : change_vbr_str_prms
151*495ae853SAndroid Build Coastguard Worker  Description     : Takes in changes of Intra frame interval, source and target
152*495ae853SAndroid Build Coastguard Worker                    ticks and recalculates the position of the  next I frame
153*495ae853SAndroid Build Coastguard Worker  ******************************************************************************/
irc_change_vsp_ifi(vbr_str_prms_t * p_vbr_str_prms,UWORD32 u4_intra_frame_int)154*495ae853SAndroid Build Coastguard Worker void irc_change_vsp_ifi(vbr_str_prms_t *p_vbr_str_prms,
155*495ae853SAndroid Build Coastguard Worker                         UWORD32 u4_intra_frame_int)
156*495ae853SAndroid Build Coastguard Worker {
157*495ae853SAndroid Build Coastguard Worker     irc_init_vbv_str_prms(p_vbr_str_prms, u4_intra_frame_int,
158*495ae853SAndroid Build Coastguard Worker                           p_vbr_str_prms->u4_src_ticks,
159*495ae853SAndroid Build Coastguard Worker                           p_vbr_str_prms->u4_tgt_ticks,
160*495ae853SAndroid Build Coastguard Worker                           p_vbr_str_prms->u4_frms_in_delay_prd);
161*495ae853SAndroid Build Coastguard Worker }
162*495ae853SAndroid Build Coastguard Worker 
irc_change_vsp_tgt_ticks(vbr_str_prms_t * p_vbr_str_prms,UWORD32 u4_tgt_ticks)163*495ae853SAndroid Build Coastguard Worker void irc_change_vsp_tgt_ticks(vbr_str_prms_t *p_vbr_str_prms,
164*495ae853SAndroid Build Coastguard Worker                               UWORD32 u4_tgt_ticks)
165*495ae853SAndroid Build Coastguard Worker {
166*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_rem_intra_per_scaled;
167*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_prev_tgt_ticks = p_vbr_str_prms->u4_tgt_ticks;
168*495ae853SAndroid Build Coastguard Worker 
169*495ae853SAndroid Build Coastguard Worker     /*
170*495ae853SAndroid Build Coastguard Worker      * If the target frame rate is changed, recalculate the position of the next
171*495ae853SAndroid Build Coastguard Worker      * I frame based on the new target frame rate
172*495ae853SAndroid Build Coastguard Worker      * LIMITATIONS :
173*495ae853SAndroid Build Coastguard Worker      * Currently no support is available for dynamic change in source frame rate
174*495ae853SAndroid Build Coastguard Worker      */
175*495ae853SAndroid Build Coastguard Worker 
176*495ae853SAndroid Build Coastguard Worker     u4_rem_intra_per_scaled = ((p_vbr_str_prms->u4_intra_prd_pos_in_tgt_ticks
177*495ae853SAndroid Build Coastguard Worker                     - p_vbr_str_prms->u4_cur_pos_in_src_ticks)
178*495ae853SAndroid Build Coastguard Worker                     / u4_prev_tgt_ticks) * u4_tgt_ticks;
179*495ae853SAndroid Build Coastguard Worker 
180*495ae853SAndroid Build Coastguard Worker     p_vbr_str_prms->u4_intra_prd_pos_in_tgt_ticks = u4_rem_intra_per_scaled
181*495ae853SAndroid Build Coastguard Worker                     + p_vbr_str_prms->u4_cur_pos_in_src_ticks;
182*495ae853SAndroid Build Coastguard Worker 
183*495ae853SAndroid Build Coastguard Worker }
184*495ae853SAndroid Build Coastguard Worker 
irc_change_vsp_src_ticks(vbr_str_prms_t * p_vbr_str_prms,UWORD32 u4_src_ticks)185*495ae853SAndroid Build Coastguard Worker void irc_change_vsp_src_ticks(vbr_str_prms_t *p_vbr_str_prms,
186*495ae853SAndroid Build Coastguard Worker                               UWORD32 u4_src_ticks)
187*495ae853SAndroid Build Coastguard Worker {
188*495ae853SAndroid Build Coastguard Worker     irc_init_vbv_str_prms(p_vbr_str_prms, p_vbr_str_prms->u4_intra_frame_int,
189*495ae853SAndroid Build Coastguard Worker                           u4_src_ticks, p_vbr_str_prms->u4_tgt_ticks,
190*495ae853SAndroid Build Coastguard Worker                           p_vbr_str_prms->u4_frms_in_delay_prd);
191*495ae853SAndroid Build Coastguard Worker }
192*495ae853SAndroid Build Coastguard Worker 
irc_change_vsp_fidp(vbr_str_prms_t * p_vbr_str_prms,UWORD32 u4_frms_in_delay_period)193*495ae853SAndroid Build Coastguard Worker void irc_change_vsp_fidp(vbr_str_prms_t *p_vbr_str_prms,
194*495ae853SAndroid Build Coastguard Worker                          UWORD32 u4_frms_in_delay_period)
195*495ae853SAndroid Build Coastguard Worker {
196*495ae853SAndroid Build Coastguard Worker     irc_init_vbv_str_prms(p_vbr_str_prms, p_vbr_str_prms->u4_intra_frame_int,
197*495ae853SAndroid Build Coastguard Worker                           p_vbr_str_prms->u4_src_ticks,
198*495ae853SAndroid Build Coastguard Worker                           p_vbr_str_prms->u4_tgt_ticks,
199*495ae853SAndroid Build Coastguard Worker                           u4_frms_in_delay_period);
200*495ae853SAndroid Build Coastguard Worker }
201