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