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