xref: /aosp_15_r20/external/libavc/examples/svcenc/recon.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 
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <string.h>
31 /* User include files */
32 
33 #include "ih264_typedefs.h"
34 #include "iv2.h"
35 #include "ive2.h"
36 #include "isvce.h"
37 #include "app.h"
38 
39 /*****************************************************************************/
40 /* Constant Macros                                                           */
41 /*****************************************************************************/
42 
43 /*****************************************************************************/
44 /*  Macros                                                                   */
45 /*****************************************************************************/
46 
47 /*****************************************************************************/
48 /*  Function Declarations                                                    */
49 /*****************************************************************************/
50 
write_recon(FILE * fp,iv_raw_buf_t * ps_raw_buf)51 IV_STATUS_T write_recon(FILE *fp, iv_raw_buf_t *ps_raw_buf)
52 {
53     WORD32 bytes;
54     WORD32 wd, ht;
55     UWORD8 *pu1_buf;
56     WORD32 i;
57     WORD32 comp;
58     WORD32 num_comp;
59 
60     num_comp = 2;
61     if(IV_YUV_420P == ps_raw_buf->e_color_fmt) num_comp = 3;
62 
63     for(comp = 0; comp < num_comp; comp++)
64     {
65         wd = ps_raw_buf->au4_wd[comp];
66         ht = ps_raw_buf->au4_ht[comp];
67         pu1_buf = ps_raw_buf->apv_bufs[comp];
68         for(i = 0; i < ht; i++)
69         {
70             bytes = (WORD32) fwrite(pu1_buf, sizeof(UWORD8), wd, fp);
71             if(bytes != wd)
72             {
73                 return (IV_FAIL);
74             }
75             pu1_buf += wd;
76         }
77     }
78 
79     fflush(fp);
80     return IV_SUCCESS;
81 }
82 
allocate_recon(app_ctxt_t * ps_app_ctxt)83 void allocate_recon(app_ctxt_t *ps_app_ctxt)
84 {
85     WORD32 num_bufs;
86     WORD32 pic_size;
87     WORD32 luma_size;
88     WORD32 chroma_size;
89     WORD32 i;
90     UWORD8 *pu1_buf;
91 
92     num_bufs = DEFAULT_NUM_RECON_BUFS;
93 
94     assert(ps_app_ctxt->s_get_buf_info_op.u4_rec_comp_cnt == 3);
95 
96     /* Size of buffer for YUV420/420SP */
97     luma_size = ps_app_ctxt->s_get_buf_info_op.au4_min_rec_buf_size[0];
98     chroma_size = ps_app_ctxt->s_get_buf_info_op.au4_min_rec_buf_size[1] +
99                   ps_app_ctxt->s_get_buf_info_op.au4_min_rec_buf_size[2];
100     pic_size = luma_size + chroma_size;
101 
102     for(i = 0; i < num_bufs; i++)
103     {
104         pu1_buf = (UWORD8 *) isvca_aligned_malloc(16, pic_size);
105 
106         if(NULL == pu1_buf)
107         {
108             CHAR ac_error[STRLENGTH];
109             snprintf(ac_error, sizeof(ac_error) - 1,
110                      "Allocation failed for recon buffer of size %d\n", pic_size);
111             codec_exit(ac_error);
112         }
113 
114         ps_app_ctxt->as_recon_buf[i].pu1_buf = pu1_buf;
115         ps_app_ctxt->as_recon_buf[i].u4_buf_size = pic_size;
116         ps_app_ctxt->as_recon_buf[i].u4_is_free = 1;
117     }
118 
119     if(ps_app_ctxt->u4_psnr_enable)
120     {
121         pu1_buf = (UWORD8 *) isvca_aligned_malloc(16, pic_size);
122 
123         if(NULL == pu1_buf)
124         {
125             CHAR ac_error[STRLENGTH];
126             snprintf(ac_error, sizeof(ac_error) - 1,
127                      "Allocation failed for recon buffer of size %d\n", pic_size);
128             codec_exit(ac_error);
129         }
130 
131         ps_app_ctxt->pu1_psnr_buf = pu1_buf;
132         ps_app_ctxt->u4_psnr_buf_size = pic_size;
133     }
134 }
135 
free_recon(app_ctxt_t * ps_app_ctxt)136 void free_recon(app_ctxt_t *ps_app_ctxt)
137 {
138     WORD32 num_bufs;
139     WORD32 i;
140 
141     num_bufs = DEFAULT_NUM_RECON_BUFS;
142 
143     for(i = 0; i < num_bufs; i++)
144     {
145         isvca_aligned_free(ps_app_ctxt->as_recon_buf[i].pu1_buf);
146     }
147 
148     if(ps_app_ctxt->u4_psnr_enable)
149     {
150         isvca_aligned_free(ps_app_ctxt->pu1_psnr_buf);
151     }
152 }
153 
init_raw_buf_descr(app_ctxt_t * ps_app_ctxt,iv_raw_buf_t * ps_raw_buf,UWORD8 * pu1_buf,IV_COLOR_FORMAT_T e_color_fmt)154 void init_raw_buf_descr(app_ctxt_t *ps_app_ctxt, iv_raw_buf_t *ps_raw_buf, UWORD8 *pu1_buf,
155                         IV_COLOR_FORMAT_T e_color_fmt)
156 {
157     WORD32 luma_size;
158     WORD32 au4_chroma_sizes[2];
159 
160     assert(IV_YUV_420P == e_color_fmt);
161 
162     luma_size = ps_app_ctxt->s_get_buf_info_op.au4_min_rec_buf_size[0];
163     au4_chroma_sizes[0] = ps_app_ctxt->s_get_buf_info_op.au4_min_rec_buf_size[1];
164     au4_chroma_sizes[1] = ps_app_ctxt->s_get_buf_info_op.au4_min_rec_buf_size[2];
165 
166     ps_raw_buf->apv_bufs[0] = pu1_buf;
167     pu1_buf += luma_size;
168 
169     ps_raw_buf->apv_bufs[1] = pu1_buf;
170     pu1_buf += au4_chroma_sizes[0];
171 
172     ps_raw_buf->apv_bufs[2] = NULL;
173     if(IV_YUV_420P == e_color_fmt)
174     {
175         ps_raw_buf->apv_bufs[2] = pu1_buf;
176     }
177 
178     ps_raw_buf->e_color_fmt = e_color_fmt;
179     ps_raw_buf->au4_wd[0] = ps_app_ctxt->u4_enc_wd;
180     ps_raw_buf->au4_ht[0] = ps_app_ctxt->u4_enc_ht;
181     ps_raw_buf->au4_strd[0] = ps_app_ctxt->u4_enc_wd;
182 
183     /* Initialize for 420SP */
184     {
185         ps_raw_buf->au4_wd[1] = ps_app_ctxt->u4_enc_wd;
186         ps_raw_buf->au4_wd[2] = 0;
187 
188         ps_raw_buf->au4_ht[1] = ps_app_ctxt->u4_enc_ht / 2;
189         ps_raw_buf->au4_ht[2] = 0;
190 
191         ps_raw_buf->au4_strd[1] = ps_app_ctxt->u4_enc_wd;
192         ps_raw_buf->au4_strd[2] = 0;
193     }
194 
195     if(IV_YUV_420P == e_color_fmt)
196     {
197         ps_raw_buf->au4_wd[1] = ps_app_ctxt->u4_enc_wd / 2;
198         ps_raw_buf->au4_wd[2] = ps_app_ctxt->u4_enc_wd / 2;
199 
200         ps_raw_buf->au4_ht[1] = ps_app_ctxt->u4_enc_ht / 2;
201         ps_raw_buf->au4_ht[2] = ps_app_ctxt->u4_enc_ht / 2;
202 
203         ps_raw_buf->au4_strd[1] = ps_app_ctxt->u4_enc_wd / 2;
204         ps_raw_buf->au4_strd[2] = ps_app_ctxt->u4_enc_wd / 2;
205     }
206     /* If stride is not initialized, then use width as stride */
207     if(0 == ps_raw_buf->au4_strd[0])
208     {
209         ps_raw_buf->au4_strd[0] = ps_raw_buf->au4_wd[0];
210         ps_raw_buf->au4_strd[1] = ps_raw_buf->au4_wd[1];
211         ps_raw_buf->au4_strd[2] = ps_raw_buf->au4_wd[2];
212     }
213 
214     ps_raw_buf->u4_size = sizeof(iv_raw_buf_t);
215 }
216