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
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 "ih264e.h"
37#include "app.h"
38
39/*****************************************************************************/
40/* Constant Macros                                                           */
41/*****************************************************************************/
42
43
44/*****************************************************************************/
45/*  Macros                                                                   */
46/*****************************************************************************/
47
48
49/*****************************************************************************/
50/*  Function Declarations                                                    */
51/*****************************************************************************/
52
53IV_STATUS_T write_recon(FILE *fp, iv_raw_buf_t *ps_raw_buf)
54{
55    WORD32 bytes;
56    WORD32 wd, ht;
57    UWORD8 *pu1_buf;
58    WORD32 i;
59    WORD32 comp;
60    WORD32 num_comp;
61
62    num_comp = 2;
63    if(IV_YUV_420P == ps_raw_buf->e_color_fmt)
64        num_comp = 3;
65
66    for(comp = 0; comp < num_comp; comp++)
67    {
68        wd = ps_raw_buf->au4_wd[comp];
69        ht = ps_raw_buf->au4_ht[comp];
70        pu1_buf = ps_raw_buf->apv_bufs[comp];
71        for(i = 0; i < ht; i++)
72        {
73            bytes = fwrite(pu1_buf, sizeof(UWORD8), wd, fp);
74            if(bytes != wd)
75            {
76                return(IV_FAIL);
77            }
78            pu1_buf += wd;
79        }
80    }
81
82    fflush(fp);
83    return IV_SUCCESS;
84}
85void allocate_recon(app_ctxt_t *ps_app_ctxt)
86{
87
88    WORD32 num_bufs;
89    WORD32 pic_size;
90    WORD32 luma_size;
91    WORD32 chroma_size;
92    WORD32 i;
93    UWORD8 *pu1_buf;
94
95    num_bufs = DEFAULT_NUM_RECON_BUFS;
96
97    /* Size of buffer for YUV420/420SP */
98    luma_size = ps_app_ctxt->u4_max_wd * ps_app_ctxt->u4_max_ht;
99    chroma_size = (luma_size) / 4;
100    pic_size = luma_size + chroma_size * 2;
101
102
103    for(i = 0; i < num_bufs; i++)
104    {
105        pu1_buf = (UWORD8 *)ih264a_aligned_malloc(16, pic_size);
106        if(NULL == pu1_buf)
107        {
108            CHAR ac_error[STRLENGTH];
109            sprintf(ac_error, "Allocation failed for recon buffer of size %d\n",
110                    pic_size);
111            codec_exit(ac_error);
112        }
113        ps_app_ctxt->as_recon_buf[i].pu1_buf = pu1_buf;
114        ps_app_ctxt->as_recon_buf[i].u4_buf_size = pic_size;
115        ps_app_ctxt->as_recon_buf[i].u4_is_free = 1;
116    }
117
118    if(ps_app_ctxt->u4_psnr_enable)
119    {
120        pu1_buf = (UWORD8 *)ih264a_aligned_malloc(16, pic_size);
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->pu1_psnr_buf = pu1_buf;
129        ps_app_ctxt->u4_psnr_buf_size = pic_size;
130    }
131    return;
132}
133
134void free_recon(app_ctxt_t *ps_app_ctxt)
135{
136
137    WORD32 num_bufs;
138    WORD32 i;
139
140    num_bufs = DEFAULT_NUM_RECON_BUFS;
141
142    for(i = 0; i < num_bufs; i++)
143    {
144        ih264a_aligned_free(ps_app_ctxt->as_recon_buf[i].pu1_buf);
145    }
146
147    if(ps_app_ctxt->u4_psnr_enable)
148    {
149        ih264a_aligned_free(ps_app_ctxt->pu1_psnr_buf);
150
151    }
152    return;
153}
154
155
156
157void 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)
158{
159    WORD32 luma_size;
160    WORD32 chroma_size;
161
162    /* All the pointers and dimensions are initialized here
163     * to support change in resolution from the application */
164    luma_size = ps_app_ctxt->u4_max_wd * ps_app_ctxt->u4_max_ht;
165    chroma_size = (luma_size) / 4;
166
167    ps_raw_buf->apv_bufs[0] = pu1_buf;
168    pu1_buf += luma_size;
169
170    ps_raw_buf->apv_bufs[1] = pu1_buf;
171    pu1_buf += chroma_size;
172
173    ps_raw_buf->apv_bufs[2] = NULL;
174    if(IV_YUV_420P == e_color_fmt)
175    {
176        ps_raw_buf->apv_bufs[2] = pu1_buf;
177    }
178
179    ps_raw_buf->e_color_fmt = e_color_fmt;
180    ps_raw_buf->au4_wd[0] =  ps_app_ctxt->u4_wd;
181    ps_raw_buf->au4_ht[0] =  ps_app_ctxt->u4_ht;
182    ps_raw_buf->au4_strd[0] =  ps_app_ctxt->u4_wd;
183
184    /* Initialize for 420SP */
185    {
186        ps_raw_buf->au4_wd[1] =  ps_app_ctxt->u4_wd;
187        ps_raw_buf->au4_wd[2] =  0;
188
189        ps_raw_buf->au4_ht[1] =  ps_app_ctxt->u4_ht / 2;
190        ps_raw_buf->au4_ht[2] =  0;
191
192        ps_raw_buf->au4_strd[1] =  ps_app_ctxt->u4_wd;
193        ps_raw_buf->au4_strd[2] =  0;
194    }
195
196    if(IV_YUV_420P == e_color_fmt)
197    {
198        ps_raw_buf->au4_wd[1] =  ps_app_ctxt->u4_wd / 2;
199        ps_raw_buf->au4_wd[2] =  ps_app_ctxt->u4_wd / 2;
200
201        ps_raw_buf->au4_ht[1] =  ps_app_ctxt->u4_ht / 2;
202        ps_raw_buf->au4_ht[2] =  ps_app_ctxt->u4_ht / 2;
203
204        ps_raw_buf->au4_strd[1] =  ps_app_ctxt->u4_wd / 2;
205        ps_raw_buf->au4_strd[2] =  ps_app_ctxt->u4_wd / 2;
206    }
207    /* If stride is not initialized, then use width as stride */
208    if(0 == ps_raw_buf->au4_strd[0])
209    {
210        ps_raw_buf->au4_strd[0] = ps_raw_buf->au4_wd[0];
211        ps_raw_buf->au4_strd[1] = ps_raw_buf->au4_wd[1];
212        ps_raw_buf->au4_strd[2] = ps_raw_buf->au4_wd[2];
213    }
214
215    ps_raw_buf->u4_size = sizeof(iv_raw_buf_t);
216    return;
217}
218
219
220