xref: /aosp_15_r20/external/libavc/encoder/ih264e_modify_frm_rate.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 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
24 *  ih264e_modify_frm_rate.c
25 *
26 * @brief
27 *  Handle source frame rate pulldown
28 *
29 * @author
30 *  ittiam
31 *
32 * @par List of Functions:
33 *  - ih264e_pd_frm_rate_get_init_free_memtab
34 *  - ih264e_init_pd_frm_rate
35 *  - ih264e_update_pd_frm_rate
36 *  - ih264e_get_pd_avg_frm_rate
37 *
38 * @remarks
39 *  none
40 *
41 *******************************************************************************
42 */
43 
44 /*****************************************************************************/
45 /* File Includes                                                             */
46 /*****************************************************************************/
47 
48 /* User Include Files */
49 #include "ih264_typedefs.h"
50 #include "iv2.h"
51 #include "ive2.h"
52 
53 #include "ih264_defs.h"
54 #include "ih264_mem_fns.h"
55 #include "ih264_padding.h"
56 #include "ih264_structs.h"
57 #include "ih264_trans_quant_itrans_iquant.h"
58 #include "ih264_inter_pred_filters.h"
59 #include "ih264_intra_pred_filters.h"
60 #include "ih264_deblk_edge_filters.h"
61 #include "ih264_cabac_tables.h"
62 
63 #include "ime_defs.h"
64 #include "ime_distortion_metrics.h"
65 #include "ime_structs.h"
66 
67 #include "irc_mem_req_and_acq.h"
68 #include "irc_cntrl_param.h"
69 #include "irc_frame_info_collector.h"
70 
71 #include "ih264e_error.h"
72 #include "ih264e_defs.h"
73 #include "ih264e_rate_control.h"
74 #include "ih264e_bitstream.h"
75 #include "ih264e_cabac_structs.h"
76 #include "ih264e_structs.h"
77 #include "ih264e_time_stamp.h"
78 #include "ih264e_modify_frm_rate.h"
79 
80 
81 /*****************************************************************************/
82 /* Function Definitions                                                      */
83 /*****************************************************************************/
84 
85 /**
86 *******************************************************************************
87 *
88 * @brief Function to init pd frame rate memtab
89 *
90 * @par Description
91 *  Function to init pull down frame rate memtab
92 *
93 * @param[in] pps_pd_frm_rate
94 *  pull down frame rate context
95 *
96 * @param[in] ps_memtab
97 *  Handle to memtab
98 *
99 * @param[in] e_func_type
100 *  Function type (get memtab/ update memtab)
101 *
102 * @returns  Number of memtabs used
103 *
104 * @remarks  None
105 *
106 *******************************************************************************
107 */
ih264e_pd_frm_rate_get_init_free_memtab(pd_frm_rate_handle * pps_pd_frm_rate,itt_memtab_t * ps_memtab,ITT_FUNC_TYPE_E e_func_type)108 WORD32 ih264e_pd_frm_rate_get_init_free_memtab(pd_frm_rate_handle *pps_pd_frm_rate,
109                                                itt_memtab_t *ps_memtab,
110                                                ITT_FUNC_TYPE_E e_func_type)
111 {
112     WORD32 i4_mem_tab_idx = 0;
113     pd_frm_rate_t s_temp_pd_frm_rate_t;
114 
115     /* Hack for al alloc, during which we dont have any state memory.
116      Dereferencing can cause issues */
117     if (e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
118         (*pps_pd_frm_rate) = &s_temp_pd_frm_rate_t;
119 
120     /* for src rate control state structure */
121     if (e_func_type != GET_NUM_MEMTAB)
122     {
123         fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(pd_frm_rate_t),
124                     ALIGN_128_BYTE, PERSISTENT, DDR);
125         use_or_fill_base(&ps_memtab[0], (void**) pps_pd_frm_rate, e_func_type);
126     }
127     i4_mem_tab_idx++;
128 
129     return (i4_mem_tab_idx);
130 }
131 
132 /**
133 *******************************************************************************
134 *
135 * @brief Initializes the pull down frame rate state structure based on input
136 *  frame rate
137 *
138 * @par Description
139 *  Initializes the pull down frame rate state structure based on input frame rate
140 *
141 * @param[in] ps_pd_frm_rate
142 *  Pull down frame rate context
143 *
144 * @param[in] u4_input_frm_rate
145 *  Input frame rate in frame per 1000sec
146 *
147 * @returns none
148 *
149 * @remarks
150 *
151 *******************************************************************************
152 */
ih264e_init_pd_frm_rate(pd_frm_rate_t * ps_pd_frm_rate,UWORD32 u4_input_frm_rate)153 void ih264e_init_pd_frm_rate(pd_frm_rate_t *ps_pd_frm_rate,
154                              UWORD32 u4_input_frm_rate)
155 {
156     WORD32 i;
157 
158     ps_pd_frm_rate->u4_input_frm_rate = u4_input_frm_rate;
159 
160     for (i = 0; i < (WORD32) (u4_input_frm_rate / 1000); i++)
161     {
162         ps_pd_frm_rate->u4_cur_frm_rate[i] = u4_input_frm_rate;
163     }
164 
165     ps_pd_frm_rate->u4_frm_num = 0;
166 
167     ps_pd_frm_rate->u4_tot_frm_encoded = 0;
168 }
169 
170 /**
171 *******************************************************************************
172 *
173 * @brief Function to update pull down frame rate
174 *
175 * @par   Description
176 *  For each frame a run time frame rate value is sent based on whether a frame
177 *  is skipped or not. If it is skipped for pull down then the current frame
178 *  rate for the pull down period is signaled as 4/5th of the original frame
179 *  rate. Thus when this is averaged the frame rate gradually switches from the
180 *  input frame rate to 4/5th of input frame rate as and when more 3:2 pull
181 *  down patterns are detected
182 *
183 * @param[in] ps_pd_frm_rate
184 *  Pull down frame rate context
185 *
186 * @param[in] u4_input_frm_rate
187 *  Input frame rate in frame per 1000sec
188 *
189 * @returns none
190 *
191 * @remarks
192 *
193 *******************************************************************************
194 */
ih264e_update_pd_frm_rate(pd_frm_rate_t * ps_pd_frm_rate,UWORD32 u4_cur_frm_rate)195 void ih264e_update_pd_frm_rate(pd_frm_rate_t *ps_pd_frm_rate,
196                                UWORD32 u4_cur_frm_rate)
197 {
198     ps_pd_frm_rate->u4_cur_frm_rate[ps_pd_frm_rate->u4_frm_num] = u4_cur_frm_rate;
199 
200     ps_pd_frm_rate->u4_frm_num++;
201 
202     /* Increment the frame number */
203     if (ps_pd_frm_rate->u4_tot_frm_encoded < (ps_pd_frm_rate->u4_input_frm_rate / 1000))
204     {
205         ps_pd_frm_rate->u4_tot_frm_encoded++;
206     }
207 
208     /* Reset frm_num to zero  */
209     if (ps_pd_frm_rate->u4_frm_num >= (ps_pd_frm_rate->u4_input_frm_rate / 1000))
210     {
211         ps_pd_frm_rate->u4_frm_num = 0;
212     }
213 }
214 
215 /**
216 *******************************************************************************
217 *
218 * @brief returns average frame rate in 1 sec duration
219 *
220 * @par Description
221 *  Averages the last N frame in period(1 sec) and then gives that
222 *  as the current frames frame rate. Thus this averages out the sudden
223 *  variation in frame rate
224 *
225 * @param[in] ps_pd_frm_rate
226 *  Handle to pull down frame rate context
227 *
228 * @returns average frame rate
229 *
230 * @remarks
231 *
232 *******************************************************************************
233 */
ih264e_get_pd_avg_frm_rate(pd_frm_rate_t * ps_pd_frm_rate)234 UWORD32 ih264e_get_pd_avg_frm_rate(pd_frm_rate_t *ps_pd_frm_rate)
235 {
236     WORD32 i;
237     WORD32 i4_avg_frm_rate = 0;
238 
239     for (i = 0; i < (WORD32) ps_pd_frm_rate->u4_tot_frm_encoded; i++)
240     {
241         i4_avg_frm_rate += ps_pd_frm_rate->u4_cur_frm_rate[i];
242     }
243 
244     i4_avg_frm_rate = i4_avg_frm_rate / ps_pd_frm_rate->u4_tot_frm_encoded;
245 
246     return i4_avg_frm_rate;
247 }
248