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 * input.c
25 *
26 * @brief
27 * Contains functions necessary for managing input buffers
28 *
29 * @author
30 * ittiam
31 *
32 * @remarks
33 * none
34 *
35 *******************************************************************************
36 */
37
38 /*****************************************************************************/
39 /* File Includes */
40 /*****************************************************************************/
41
42 /* System include files */
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <assert.h>
46 #include <string.h>
47
48 /* User include files */
49 #include "ih264_typedefs.h"
50 #include "iv2.h"
51 #include "ive2.h"
52 #include "ih264e.h"
53 #include "app.h"
54
55 /*****************************************************************************/
56 /* Constant Macros */
57 /*****************************************************************************/
58
59
60 /*****************************************************************************/
61 /* Macros */
62 /*****************************************************************************/
63
64
65 /*****************************************************************************/
66 /* Function Definitions */
67 /*****************************************************************************/
68
69 /**
70 **************************************************************************
71 * @brief read pic level metadata from a file
72 **************************************************************************
73 */
read_pic_info(app_ctxt_t * ps_app_ctxt,void * pv_pic_info)74 IV_STATUS_T read_pic_info(app_ctxt_t *ps_app_ctxt, void *pv_pic_info)
75 {
76 IV_STATUS_T ret = IV_SUCCESS;
77 WORD32 size, bytes;
78
79 switch(ps_app_ctxt->u4_pic_info_type)
80 {
81 case 1:
82 size = sizeof(ih264e_pic_info1_t);
83 ps_app_ctxt->u4_pic_info_size = sizeof(ih264e_pic_info1_t);
84 break;
85 case 2:
86 size = sizeof(ih264e_pic_info2_t);
87 ps_app_ctxt->u4_pic_info_size = sizeof(ih264e_pic_info2_t);
88 break;
89 default:
90 size = 0;
91 break;
92 }
93
94 bytes = fread(pv_pic_info, 1, size, ps_app_ctxt->fp_pic_info);
95 if(bytes != size)
96 ret = IV_FAIL;
97
98 return ret;
99 }
100
101 /**
102 **************************************************************************
103 * @brief read mb level metadata from a file
104 **************************************************************************
105 */
read_mb_info(app_ctxt_t * ps_app_ctxt,void * pv_mb_info)106 IV_STATUS_T read_mb_info(app_ctxt_t *ps_app_ctxt, void *pv_mb_info)
107 {
108 IV_STATUS_T ret = IV_SUCCESS;
109 WORD32 num_mbs;
110 WORD32 size;
111 WORD32 bytes;
112
113 num_mbs = ALIGN16(ps_app_ctxt->u4_wd) * ALIGN16(ps_app_ctxt->u4_ht);
114 num_mbs /= 256;
115
116 switch(ps_app_ctxt->u4_mb_info_type)
117 {
118 case 1:
119 size = sizeof(ih264e_mb_info1_t) * num_mbs;
120 ps_app_ctxt->u4_mb_info_size = sizeof(ih264e_mb_info1_t);
121 break;
122 case 2:
123 size = sizeof(ih264e_mb_info2_t) * num_mbs;
124 ps_app_ctxt->u4_mb_info_size = sizeof(ih264e_mb_info2_t);
125 break;
126 case 3:
127 size = sizeof(ih264e_mb_info3_t) * num_mbs;
128 ps_app_ctxt->u4_mb_info_size = sizeof(ih264e_mb_info3_t);
129 break;
130 case 4:
131 size = sizeof(ih264e_mb_info4_t) * num_mbs;
132 ps_app_ctxt->u4_mb_info_size = sizeof(ih264e_mb_info4_t);
133 break;
134 default:
135 size = 0;
136 break;
137 }
138
139 bytes = fread(pv_mb_info, 1, size, ps_app_ctxt->fp_mb_info);
140 if(bytes != size)
141 ret = IV_FAIL;
142
143 return ret;
144 }
145
146 /**
147 **************************************************************************
148 * @brief read input from a file
149 **************************************************************************
150 */
read_input(FILE * fp,iv_raw_buf_t * ps_raw_buf)151 IV_STATUS_T read_input(FILE *fp, iv_raw_buf_t *ps_raw_buf)
152 {
153 WORD32 i;
154
155 if(IV_YUV_422ILE == ps_raw_buf->e_color_fmt)
156 {
157 WORD32 wd = ps_raw_buf->au4_wd[0];
158 WORD32 ht = ps_raw_buf->au4_ht[0];
159 WORD32 strd = ps_raw_buf->au4_strd[0];
160 UWORD8 *pu1_buf = ps_raw_buf->apv_bufs[0];
161
162 for(i = 0; i < ht; i++)
163 {
164 WORD32 bytes = fread(pu1_buf, sizeof(UWORD8), wd, fp);
165
166 if(bytes != wd)
167 {
168 return (IV_FAIL);
169 }
170 pu1_buf += strd;
171 }
172 }
173 else
174 {
175 WORD32 num_comp = 2;
176 WORD32 comp_idx;
177
178 if(IV_YUV_420P == ps_raw_buf->e_color_fmt)
179 num_comp = 3;
180
181 for(comp_idx = 0; comp_idx < num_comp; comp_idx++)
182 {
183 WORD32 wd = ps_raw_buf->au4_wd[comp_idx];
184 WORD32 ht = ps_raw_buf->au4_ht[comp_idx];
185 WORD32 strd = ps_raw_buf->au4_strd[comp_idx];
186 UWORD8 *pu1_buf = ps_raw_buf->apv_bufs[comp_idx];
187
188 for(i = 0; i < ht; i++)
189 {
190 WORD32 bytes = fread(pu1_buf, sizeof(UWORD8), wd, fp);
191
192 if(bytes != wd)
193 {
194 return (IV_FAIL);
195 }
196 pu1_buf += strd;
197 }
198 }
199 }
200 return IV_SUCCESS;
201 }
202
203 /**
204 **************************************************************************
205 * @brief write input to a file
206 **************************************************************************
207 */
dump_input(FILE * fp,iv_raw_buf_t * ps_raw_buf)208 IV_STATUS_T dump_input(FILE *fp, iv_raw_buf_t *ps_raw_buf)
209 {
210 WORD32 i;
211
212 if(IV_YUV_422ILE == ps_raw_buf->e_color_fmt)
213 {
214 WORD32 wd = ps_raw_buf->au4_wd[0];
215 WORD32 ht = ps_raw_buf->au4_ht[0];
216 WORD32 strd = ps_raw_buf->au4_strd[0];
217 UWORD8 *pu1_buf = ps_raw_buf->apv_bufs[0];
218
219 for(i = 0; i < ht; i++)
220 {
221 WORD32 bytes = fwrite(pu1_buf, sizeof(UWORD8), wd, fp);
222
223 if(bytes != wd)
224 {
225 return (IV_FAIL);
226 }
227 pu1_buf += strd;
228 }
229 }
230 else
231 {
232 WORD32 num_comp = 2;
233 WORD32 comp_idx;
234
235 if(IV_YUV_420P == ps_raw_buf->e_color_fmt)
236 num_comp = 3;
237
238 for(comp_idx = 0; comp_idx < num_comp; comp_idx++)
239 {
240 WORD32 wd = ps_raw_buf->au4_wd[comp_idx];
241 WORD32 ht = ps_raw_buf->au4_ht[comp_idx];
242 WORD32 strd = ps_raw_buf->au4_strd[comp_idx];
243 UWORD8 *pu1_buf = ps_raw_buf->apv_bufs[comp_idx];
244
245 for(i = 0; i < ht; i++)
246 {
247 WORD32 bytes = fwrite(pu1_buf, sizeof(UWORD8), wd, fp);
248
249 if(bytes != wd)
250 {
251 return (IV_FAIL);
252 }
253 pu1_buf += strd;
254 }
255 }
256 }
257 return IV_SUCCESS;
258 }
259
260 /**
261 **************************************************************************
262 * @brief allocate input buffers
263 **************************************************************************
264 */
allocate_input(app_ctxt_t * ps_app_ctxt)265 void allocate_input(app_ctxt_t *ps_app_ctxt)
266 {
267 WORD32 num_bufs;
268 WORD32 luma_size = ps_app_ctxt->u4_wd * ps_app_ctxt->u4_ht;
269 WORD32 chroma_size = luma_size >> 1;
270 WORD32 pic_size = luma_size + chroma_size * 2;
271 WORD32 num_mbs;
272 WORD32 i;
273 ih264e_ctl_getbufinfo_op_t *ps_get_buf_info_op = &ps_app_ctxt->s_get_buf_info_op;
274
275 num_bufs = MAX(DEFAULT_NUM_INPUT_BUFS,
276 ps_get_buf_info_op->s_ive_op.u4_min_inp_bufs);
277 num_bufs = MIN(DEFAULT_MAX_INPUT_BUFS, num_bufs);
278
279 num_mbs = ALIGN16(ps_app_ctxt->u4_max_wd) * ALIGN16(ps_app_ctxt->u4_max_ht);
280 num_mbs /= 256;
281
282 /* Memset the input buffer array to set is_free to 0 */
283 memset(ps_app_ctxt->as_input_buf, 0,
284 sizeof(input_buf_t) * DEFAULT_MAX_INPUT_BUFS);
285
286 for(i = 0; i < num_bufs; i++)
287 {
288 UWORD8 *pu1_buf = (UWORD8 *)ih264a_aligned_malloc(16, pic_size);
289 if(NULL == pu1_buf)
290 {
291 CHAR ac_error[STRLENGTH];
292 sprintf(ac_error, "Allocation failed for input buffer of size %d\n",
293 pic_size);
294 codec_exit(ac_error);
295 }
296 ps_app_ctxt->as_input_buf[i].pu1_buf = pu1_buf;
297
298 pu1_buf = (UWORD8 *)ih264a_aligned_malloc(
299 16, num_mbs * sizeof(ih264e_mb_info_t));
300 if(NULL == pu1_buf)
301 {
302 CHAR ac_error[STRLENGTH];
303 sprintf(ac_error,
304 "Allocation failed for mb info buffer of size %d\n",
305 (WORD32)(num_mbs * sizeof(ih264e_mb_info_t)));
306 codec_exit(ac_error);
307 }
308 ps_app_ctxt->as_input_buf[i].pv_mb_info = pu1_buf;
309 pu1_buf = (UWORD8 *)ih264a_aligned_malloc(16,
310 sizeof(ih264e_pic_info2_t));
311 if(NULL == pu1_buf)
312 {
313 CHAR ac_error[STRLENGTH];
314 sprintf(ac_error,
315 "Allocation failed for pic info buffer of size %d\n",
316 (WORD32)sizeof(ih264e_pic_info2_t));
317 codec_exit(ac_error);
318 }
319 ps_app_ctxt->as_input_buf[i].pv_pic_info = pu1_buf;
320 ps_app_ctxt->as_input_buf[i].u4_buf_size = pic_size;
321 ps_app_ctxt->as_input_buf[i].u4_is_free = 1;
322 }
323 }
324
325 /**
326 **************************************************************************
327 * @brief free input buffers
328 **************************************************************************
329 */
free_input(app_ctxt_t * ps_app_ctxt)330 void free_input(app_ctxt_t *ps_app_ctxt)
331 {
332 WORD32 num_bufs;
333 WORD32 i;
334
335 num_bufs = MAX(DEFAULT_NUM_INPUT_BUFS,
336 ps_app_ctxt->s_get_buf_info_op.s_ive_op.u4_min_inp_bufs);
337 num_bufs = MIN(DEFAULT_MAX_INPUT_BUFS, num_bufs);
338
339 for(i = 0; i < num_bufs; i++)
340 {
341 ih264a_aligned_free(ps_app_ctxt->as_input_buf[i].pu1_buf);
342 ih264a_aligned_free(ps_app_ctxt->as_input_buf[i].pv_mb_info);
343 ih264a_aligned_free(ps_app_ctxt->as_input_buf[i].pv_pic_info);
344 }
345 }
346
347