1 /******************************************************************************
2 *
3 * Copyright (C) 2022 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 * isvce_rc_utils.c
25 *
26 * @brief
27 * Contains get gpp function required by the SVC encoder
28 *
29 * @author
30 * ittiam
31 *
32 * @par List of Functions:
33 * - isvce_get_gpp()
34 * - isvce_rc_utils_init()
35 * - isvce_get_rc_utils_data_size()
36 * - isvce_compute_gpp()
37 * - isvce_get_gpp_function_selector()
38 *
39 * @remarks
40 * None
41 *
42 *******************************************************************************
43 */
44
45 #include "ih264_typedefs.h"
46 #include "ih264_macros.h"
47 #include "isvc_structs.h"
48 #include "isvce_rc_utils.h"
49 #include "isvce_rc_utils_private_defs.h"
50
51 /**
52 *******************************************************************************
53 *
54 * @brief
55 * get gpp function
56 *
57 * @par Description:
58 * computes gradient per pixel value for a given frame
59 *
60 * @param[in] ps_input_buf
61 * pointer to yuv buffer properties
62 *
63 * @returns
64 * calculated gpp value
65 *
66 * @remarks
67 * none
68 *
69 *******************************************************************************
70 */
71
isvce_get_gpp(yuv_buf_props_t * ps_input_buf)72 static DOUBLE isvce_get_gpp(yuv_buf_props_t *ps_input_buf)
73 {
74 UWORD32 i, j;
75
76 DOUBLE d_gpp_y = 0;
77 DOUBLE d_gpp_u = 0;
78 DOUBLE d_gpp_v = 0;
79
80 DOUBLE d_gpp = 0;
81
82 UWORD32 u4_width = ps_input_buf->u4_width;
83 UWORD32 u4_height = ps_input_buf->u4_height;
84
85 UWORD8 *pu1_input_buf = (UWORD8 *) ps_input_buf->as_component_bufs[0].pv_data;
86 WORD32 i4_input_stride = ps_input_buf->as_component_bufs[0].i4_data_stride;
87
88 for(i = 0; i < u4_height - 1; i++)
89 {
90 for(j = 0; j < u4_width - 1; j++)
91 {
92 UWORD8 u1_cur_pix = pu1_input_buf[j];
93 UWORD8 u1_bot_pix = pu1_input_buf[i4_input_stride + j];
94 UWORD8 u1_right_pix = pu1_input_buf[j + 1];
95
96 d_gpp_y += (ABS(u1_cur_pix - u1_bot_pix) + ABS(u1_cur_pix - u1_right_pix));
97 }
98 pu1_input_buf += i4_input_stride;
99 }
100
101 pu1_input_buf = (UWORD8 *) ps_input_buf->as_component_bufs[1].pv_data;
102 i4_input_stride = ps_input_buf->as_component_bufs[1].i4_data_stride;
103
104 for(i = 0; i < (u4_height >> 1) - 1; i++)
105 {
106 for(j = 0; j < u4_width - 2; j += 2)
107 {
108 UWORD8 u1_cur_pix = pu1_input_buf[j];
109 UWORD8 u1_bot_pix = pu1_input_buf[i4_input_stride + j];
110 UWORD8 u1_right_pix = pu1_input_buf[j + 2];
111
112 d_gpp_u += (ABS(u1_cur_pix - u1_bot_pix) + ABS(u1_cur_pix - u1_right_pix));
113
114 u1_cur_pix = pu1_input_buf[j + 1];
115 u1_bot_pix = pu1_input_buf[i4_input_stride + j + 1];
116 u1_right_pix = pu1_input_buf[j + 2 + 1];
117
118 d_gpp_v += (ABS(u1_cur_pix - u1_bot_pix) + ABS(u1_cur_pix - u1_right_pix));
119 }
120 pu1_input_buf += i4_input_stride;
121 }
122
123 d_gpp_y /= (u4_width * u4_height);
124 d_gpp_u /= ((u4_width >> 1) * (u4_height >> 1));
125 d_gpp_v /= ((u4_width >> 1) * (u4_height >> 1));
126
127 d_gpp = (DOUBLE) ((4 * d_gpp_y) + d_gpp_u + d_gpp_v) / 6;
128
129 return d_gpp;
130 }
131
132 /**
133 *******************************************************************************
134 *
135 * @brief
136 * gets the memory size required for compute gpp
137 *
138 * @par Description:
139 * returns the memory required by the rc utils context and state structs
140 * for allocation.
141 *
142 * @returns
143 *
144 * @remarks
145 *
146 *
147 *******************************************************************************
148 */
149
isvce_get_rc_utils_data_size()150 UWORD32 isvce_get_rc_utils_data_size() { return sizeof(svc_rc_utils_state_t); }
151
152 /**
153 *******************************************************************************
154 *
155 * @brief
156 * compute gpp process
157 *
158 * @par Description:
159 * calls the function to compute gpp
160 *
161 * @param[in] ps_svc_rc_utils_ctxt
162 * pointer to svc rc utils context
163 *
164 * @param[in] ps_input_buf
165 * pointer to yuv buffer properties
166 *
167 * @returns
168 * calculated gpp value
169 *
170 * @remarks
171 * none
172 *
173 *******************************************************************************
174 */
175
isvce_compute_gpp(svc_rc_utils_ctxt_t * ps_svc_rc_utils_ctxt,yuv_buf_props_t * ps_input_buf)176 DOUBLE isvce_compute_gpp(svc_rc_utils_ctxt_t *ps_svc_rc_utils_ctxt, yuv_buf_props_t *ps_input_buf)
177 {
178 svc_rc_utils_state_t *ps_rc_utils_state =
179 (svc_rc_utils_state_t *) ps_svc_rc_utils_ctxt->pv_rc_utils_state;
180
181 return ps_rc_utils_state->pf_get_gpp(ps_input_buf);
182 }
183
184 /**
185 *******************************************************************************
186 *
187 * @brief
188 * selects which function to call for get gpp based on e_arch
189 *
190 * @par Description:
191 *
192 * @param[in] ps_rc_utils_state
193 * pointer to svc rc utils state
194 *
195 * @param[in] e_arch
196 * architecure type
197 *
198 * @returns
199 *
200 * @remarks
201 *
202 *******************************************************************************
203 */
204
isvce_get_gpp_function_selector(svc_rc_utils_state_t * ps_rc_utils_state,IV_ARCH_T e_arch)205 static void isvce_get_gpp_function_selector(svc_rc_utils_state_t *ps_rc_utils_state,
206 IV_ARCH_T e_arch)
207 {
208 switch(e_arch)
209 {
210 #if defined(X86)
211 case ARCH_X86_SSE42:
212 {
213 ps_rc_utils_state->pf_get_gpp = isvce_get_gpp_sse42;
214
215 break;
216 }
217 #elif defined(ARMV8)
218 case ARCH_ARM_A53:
219 case ARCH_ARM_A57:
220 case ARCH_ARM_V8_NEON:
221 {
222 ps_rc_utils_state->pf_get_gpp = isvce_get_gpp_neon;
223
224 break;
225 }
226 #elif defined(ARM) && !defined(DISABLE_NEON)
227 case ARCH_ARM_A9Q:
228 case ARCH_ARM_A9A:
229 case ARCH_ARM_A9:
230 case ARCH_ARM_A7:
231 case ARCH_ARM_A5:
232 case ARCH_ARM_A15:
233 {
234 ps_rc_utils_state->pf_get_gpp = isvce_get_gpp_neon;
235
236 break;
237 }
238 #endif
239 default:
240 {
241 ps_rc_utils_state->pf_get_gpp = isvce_get_gpp;
242
243 break;
244 }
245 }
246 }
247
248 /**
249 *******************************************************************************
250 *
251 * @brief
252 * initializes the rc utils context
253 *
254 * @par Description:
255 * initializes the rc utils context
256 *
257 * @param[in] ps_svc_rc_utils_ctxt
258 * pointer to svc rc utils context
259 *
260 * @param[in] ps_mem_rec
261 * pointer to memory allocated to compute gpp process
262 *
263 * @param[in] e_arch
264 * architecure type
265 *
266 * @returns
267 *
268 * @remarks
269 * none
270 *
271 *******************************************************************************
272 */
273
isvce_rc_utils_init(svc_rc_utils_ctxt_t * ps_svc_rc_utils_ctxt,iv_mem_rec_t * ps_mem_rec,IV_ARCH_T e_arch)274 void isvce_rc_utils_init(svc_rc_utils_ctxt_t *ps_svc_rc_utils_ctxt, iv_mem_rec_t *ps_mem_rec,
275 IV_ARCH_T e_arch)
276 {
277 svc_rc_utils_state_t *ps_rc_utils_state;
278
279 UWORD8 *pu1_buf = (UWORD8 *) ps_mem_rec->pv_base;
280
281 ps_rc_utils_state = (svc_rc_utils_state_t *) pu1_buf;
282
283 ps_svc_rc_utils_ctxt->pv_rc_utils_state = ps_rc_utils_state;
284
285 isvce_get_gpp_function_selector(ps_rc_utils_state, e_arch);
286 }
287