1/*
2 * Copyright (C) 2003 - 2016 Sony Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ldac.h"
18
19/***************************************************************************************************
20    Allocate Memory
21***************************************************************************************************/
22static LDAC_RESULT alloc_encode_ldac(
23SFINFO *p_sfinfo)
24{
25    LDAC_RESULT result = LDAC_S_OK;
26    CFG *p_cfg = &p_sfinfo->cfg;
27    int ich;
28    int nchs = p_cfg->ch;
29    int nbks = gaa_block_setting_ldac[p_cfg->chconfig_id][1];
30
31    /* Allocate AC */
32    for (ich = 0; ich < nchs; ich++) {
33        p_sfinfo->ap_ac[ich] = (AC *)calloc_ldac(p_sfinfo, 1, sizeof(AC));
34        if (p_sfinfo->ap_ac[ich] != (AC *)NULL) {
35            p_sfinfo->ap_ac[ich]->p_acsub = (ACSUB *)calloc_ldac(p_sfinfo, 1, sizeof(ACSUB));
36            if (p_sfinfo->ap_ac[ich]->p_acsub == (ACSUB *)NULL) {
37                result = LDAC_E_FAIL;
38                break;
39            }
40        }
41        else {
42            result = LDAC_E_FAIL;
43            break;
44        }
45    }
46
47    if (result != LDAC_S_OK) {
48        return result;
49    }
50
51    /* Allocate AB */
52    p_sfinfo->p_ab = (AB *)calloc_ldac(p_sfinfo, nbks, sizeof(AB));
53    if (p_sfinfo->p_ab == (AB *)NULL) {
54        result = LDAC_E_FAIL;
55    }
56
57    return result;
58}
59
60/***************************************************************************************************
61    Initialize Memory
62***************************************************************************************************/
63DECLFUNC LDAC_RESULT init_encode_ldac(
64SFINFO *p_sfinfo)
65{
66    LDAC_RESULT result = LDAC_S_OK;
67    CFG *p_cfg = &p_sfinfo->cfg;
68    AB *p_ab;
69    int ibk, ich;
70    int blk_type, blk_nchs;
71    int ch_offset = 0;
72    int chconfig_id = p_cfg->chconfig_id;
73    int nbks = gaa_block_setting_ldac[chconfig_id][1];
74
75    if (alloc_encode_ldac(p_sfinfo) == LDAC_E_FAIL) {
76        p_sfinfo->error_code = LDAC_ERR_ALLOC_MEMORY;
77        return LDAC_E_FAIL;
78    }
79
80    p_sfinfo->error_code = LDAC_ERR_NONE;
81    p_cfg->frame_status = LDAC_FRMSTAT_LEV_0;
82
83    /* Set AB information */
84    p_ab = p_sfinfo->p_ab;
85    for (ibk = 0; ibk < nbks; ibk++){
86        p_ab->blk_type = blk_type = gaa_block_setting_ldac[chconfig_id][ibk+2];
87        p_ab->blk_nchs = blk_nchs = get_block_nchs_ldac(blk_type);
88        p_ab->p_smplrate_id = &p_cfg->smplrate_id;
89        p_ab->p_error_code = &p_sfinfo->error_code;
90
91        /* Set AC Information */
92        for (ich = 0; ich < blk_nchs; ich++) {
93            p_ab->ap_ac[ich] = p_sfinfo->ap_ac[ch_offset++];
94            p_ab->ap_ac[ich]->p_ab = p_ab;
95            p_ab->ap_ac[ich]->ich = ich;
96            p_ab->ap_ac[ich]->frmana_cnt = 0;
97        }
98
99        p_ab++;
100    }
101
102    calc_initial_bits_ldac(p_sfinfo);
103
104    return result;
105}
106
107/***************************************************************************************************
108    Calculate Initial Bits
109***************************************************************************************************/
110DECLFUNC void calc_initial_bits_ldac(
111SFINFO *p_sfinfo)
112{
113    CFG *p_cfg = &p_sfinfo->cfg;
114    AB *p_ab = p_sfinfo->p_ab;
115    int ibk;
116    int blk_type;
117    int nbits_ab, nbits_ac;
118    int chconfig_id = p_cfg->chconfig_id;
119    int nbks = gaa_block_setting_ldac[chconfig_id][1];
120
121    nbits_ac = p_cfg->frame_length * LDAC_BYTESIZE / p_cfg->ch;
122
123    for (ibk = 0; ibk < nbks; ibk++){
124        blk_type = gaa_block_setting_ldac[chconfig_id][ibk+2];
125
126        if (blk_type == LDAC_BLKID_STEREO){
127            nbits_ab = (nbits_ac * 2 / LDAC_BYTESIZE) * LDAC_BYTESIZE;
128        }
129        else{
130            nbits_ab = (nbits_ac / LDAC_BYTESIZE) * LDAC_BYTESIZE;
131        }
132        p_ab->nbits_ab = nbits_ab;
133
134        p_ab++;
135    }
136
137    return;
138}
139
140/***************************************************************************************************
141    Free Memory
142***************************************************************************************************/
143DECLFUNC void free_encode_ldac(
144SFINFO *p_sfinfo)
145{
146    int ich;
147    int nchs = p_sfinfo->cfg.ch;
148
149    /* Free AB */
150    if (p_sfinfo->p_ab != (AB *)NULL) {
151        free(p_sfinfo->p_ab);
152        p_sfinfo->p_ab = (AB *)NULL;
153    }
154
155    /* Free AC */
156    for (ich = 0; ich < nchs; ich++) {
157        if (p_sfinfo->ap_ac[ich] != (AC *)NULL) {
158            if (p_sfinfo->ap_ac[ich]->p_acsub != (ACSUB *)NULL) {
159                free(p_sfinfo->ap_ac[ich]->p_acsub);
160                p_sfinfo->ap_ac[ich]->p_acsub = (ACSUB *)NULL;
161            }
162            free(p_sfinfo->ap_ac[ich]);
163            p_sfinfo->ap_ac[ich] = (AC *)NULL;
164        }
165    }
166
167    return;
168}
169
170/***************************************************************************************************
171    Encode Audio Block
172***************************************************************************************************/
173static int encode_audio_block_ldac(
174AB *p_ab)
175{
176    AC *p_ac;
177    int ich;
178    int nchs = p_ab->blk_nchs;
179
180    for (ich = 0; ich < nchs; ich++) {
181        p_ac = p_ab->ap_ac[ich];
182
183        norm_spectrum_ldac(p_ac);
184    }
185
186    if (!alloc_bits_ldac(p_ab)) {
187        return LDAC_FALSE;
188    }
189
190    for (ich = 0; ich < nchs; ich++) {
191        p_ac = p_ab->ap_ac[ich];
192
193        quant_spectrum_ldac(p_ac);
194
195        quant_residual_ldac(p_ac);
196    }
197
198    return LDAC_TRUE;
199}
200
201/***************************************************************************************************
202    Encode
203***************************************************************************************************/
204DECLFUNC int encode_ldac(
205SFINFO *p_sfinfo,
206int nbands,
207int grad_mode,
208int grad_qu_l,
209int grad_qu_h,
210int grad_os_l,
211int grad_os_h,
212int abc_status)
213{
214    AB *p_ab = p_sfinfo->p_ab;
215    int ibk;
216    int nbks = gaa_block_setting_ldac[p_sfinfo->cfg.chconfig_id][1];
217
218    for (ibk = 0; ibk < nbks; ibk++){
219        p_ab->nbands = nbands;
220        p_ab->nqus = ga_nqus_ldac[nbands];
221        p_ab->grad_mode = grad_mode;
222        p_ab->grad_qu_l = grad_qu_l;
223        p_ab->grad_qu_h = grad_qu_h;
224        p_ab->grad_os_l = grad_os_l;
225        p_ab->grad_os_h = grad_os_h;
226        p_ab->abc_status = abc_status;
227
228        if (!encode_audio_block_ldac(p_ab)) {
229            return LDAC_ERR_NON_FATAL_ENCODE;
230        }
231
232        p_ab++;
233    }
234
235    return LDAC_ERR_NONE;
236}
237
238