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 * recon.c
25 *
26 * @brief
27 * Contains functions necessary for managing recon 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 Write recon buffers to a file
72 **************************************************************************
73 */
write_recon(FILE * fp,iv_raw_buf_t * ps_raw_buf)74 IV_STATUS_T write_recon(FILE *fp, iv_raw_buf_t *ps_raw_buf)
75 {
76 WORD32 num_comp = 2;
77 WORD32 comp_idx;
78
79 if(IV_YUV_420P == ps_raw_buf->e_color_fmt)
80 num_comp = 3;
81
82 for(comp_idx = 0; comp_idx < num_comp; comp_idx++)
83 {
84 WORD32 wd = ps_raw_buf->au4_wd[comp_idx];
85 WORD32 ht = ps_raw_buf->au4_ht[comp_idx];
86 UWORD8 *pu1_buf = ps_raw_buf->apv_bufs[comp_idx];
87 WORD32 i;
88
89 for(i = 0; i < ht; i++)
90 {
91 WORD32 bytes = fwrite(pu1_buf, sizeof(UWORD8), wd, fp);
92 if(bytes != wd)
93 {
94 return (IV_FAIL);
95 }
96 pu1_buf += wd;
97 }
98 }
99 fflush(fp);
100 return IV_SUCCESS;
101 }
102
103 /**
104 **************************************************************************
105 * @brief Allocate space for recon buffers
106 **************************************************************************
107 */
allocate_recon(app_ctxt_t * ps_app_ctxt)108 void allocate_recon(app_ctxt_t *ps_app_ctxt)
109 {
110 WORD32 num_bufs = DEFAULT_NUM_RECON_BUFS;
111 /* Size of buffer for YUV420 */
112 WORD32 luma_size = ps_app_ctxt->u4_max_wd * ps_app_ctxt->u4_max_ht;
113 WORD32 chroma_size = (luma_size) / 4;
114 WORD32 pic_size = luma_size + chroma_size * 2;
115 WORD32 i;
116
117 for(i = 0; i < num_bufs; i++)
118 {
119 UWORD8 *pu1_buf = (UWORD8 *)ih264a_aligned_malloc(16, pic_size);
120
121 if(NULL == pu1_buf)
122 {
123 CHAR ac_error[STRLENGTH];
124 sprintf(ac_error, "Allocation failed for recon buffer of size %d\n",
125 pic_size);
126 codec_exit(ac_error);
127 }
128 ps_app_ctxt->as_recon_buf[i].pu1_buf = pu1_buf;
129 ps_app_ctxt->as_recon_buf[i].u4_buf_size = pic_size;
130 ps_app_ctxt->as_recon_buf[i].u4_is_free = 1;
131 }
132 if(ps_app_ctxt->u4_psnr_enable)
133 {
134 UWORD8 *pu1_buf = (UWORD8 *)ih264a_aligned_malloc(16, pic_size);
135
136 if(NULL == pu1_buf)
137 {
138 CHAR ac_error[STRLENGTH];
139 sprintf(ac_error, "Allocation failed for recon buffer of size %d\n",
140 pic_size);
141 codec_exit(ac_error);
142 }
143 ps_app_ctxt->pu1_psnr_buf = pu1_buf;
144 ps_app_ctxt->u4_psnr_buf_size = pic_size;
145 }
146 }
147
148 /**
149 **************************************************************************
150 * @brief free recon buffers
151 **************************************************************************
152 */
free_recon(app_ctxt_t * ps_app_ctxt)153 void free_recon(app_ctxt_t *ps_app_ctxt)
154 {
155 WORD32 num_bufs = DEFAULT_NUM_RECON_BUFS;
156 WORD32 i;
157
158 for(i = 0; i < num_bufs; i++)
159 {
160 ih264a_aligned_free(ps_app_ctxt->as_recon_buf[i].pu1_buf);
161 }
162 if(ps_app_ctxt->u4_psnr_enable)
163 {
164 ih264a_aligned_free(ps_app_ctxt->pu1_psnr_buf);
165 }
166 }
167
168 /**
169 **************************************************************************
170 * @brief initialize raw buffer descriptor
171 * All the pointers and dimensions are initialized here to support change in
172 * resolution from the application
173 **************************************************************************
174 */
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)175 void init_raw_buf_descr(app_ctxt_t *ps_app_ctxt,
176 iv_raw_buf_t *ps_raw_buf,
177 UWORD8 *pu1_buf,
178 IV_COLOR_FORMAT_T e_color_fmt)
179 {
180 WORD32 luma_size = ps_app_ctxt->u4_max_wd * ps_app_ctxt->u4_max_ht;
181 WORD32 chroma_size = (luma_size) / 4;
182
183 ps_raw_buf->u4_size = sizeof(iv_raw_buf_t);
184 ps_raw_buf->e_color_fmt = e_color_fmt;
185
186 ps_raw_buf->apv_bufs[0] = pu1_buf;
187 pu1_buf += luma_size;
188 ps_raw_buf->apv_bufs[1] = pu1_buf;
189 pu1_buf += chroma_size;
190 ps_raw_buf->apv_bufs[2] = NULL;
191 if(IV_YUV_420P == e_color_fmt)
192 {
193 ps_raw_buf->apv_bufs[2] = pu1_buf;
194 }
195
196 ps_raw_buf->au4_wd[0] = ps_app_ctxt->u4_wd;
197 ps_raw_buf->au4_ht[0] = ps_app_ctxt->u4_ht;
198 ps_raw_buf->au4_strd[0] = ps_app_ctxt->u4_wd;
199 /* Initialize for 420SP */
200 {
201 ps_raw_buf->au4_wd[1] = ps_app_ctxt->u4_wd;
202 ps_raw_buf->au4_wd[2] = 0;
203
204 ps_raw_buf->au4_ht[1] = ps_app_ctxt->u4_ht / 2;
205 ps_raw_buf->au4_ht[2] = 0;
206
207 ps_raw_buf->au4_strd[1] = ps_app_ctxt->u4_wd;
208 ps_raw_buf->au4_strd[2] = 0;
209 }
210 if(IV_YUV_420P == e_color_fmt)
211 {
212 ps_raw_buf->au4_wd[1] = ps_app_ctxt->u4_wd / 2;
213 ps_raw_buf->au4_wd[2] = ps_app_ctxt->u4_wd / 2;
214
215 ps_raw_buf->au4_ht[1] = ps_app_ctxt->u4_ht / 2;
216 ps_raw_buf->au4_ht[2] = ps_app_ctxt->u4_ht / 2;
217
218 ps_raw_buf->au4_strd[1] = ps_app_ctxt->u4_wd / 2;
219 ps_raw_buf->au4_strd[2] = ps_app_ctxt->u4_wd / 2;
220 }
221 /* If stride is not initialized, then use width as stride */
222 if(0 == ps_raw_buf->au4_strd[0])
223 {
224 ps_raw_buf->au4_strd[0] = ps_raw_buf->au4_wd[0];
225 ps_raw_buf->au4_strd[1] = ps_raw_buf->au4_wd[1];
226 ps_raw_buf->au4_strd[2] = ps_raw_buf->au4_wd[2];
227 }
228 }
229
230