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