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    Subfunction: Get Scale Factor Index
21***************************************************************************************************/
22__inline static int get_scale_factor_id_ldac(
23INT32 val)
24{
25    int i;
26    int id, step;
27
28    if (ga_sf_ldac[0] > val) {
29        return 0;
30    }
31
32    id = LDAC_NIDSF >> 1;
33    step = LDAC_NIDSF >> 2;
34    for (i = 0; i < LDAC_IDSFBITS-1; i++) {
35        if (ga_sf_ldac[id] > val) {
36            id -= step;
37        }
38        else {
39            id += step;
40        }
41        step >>= 1;
42    }
43
44    if ((ga_sf_ldac[id] <= val) && (id < LDAC_NIDSF-1)) {
45        id++;
46    }
47
48    return id;
49}
50
51/***************************************************************************************************
52    Normalize Spectrum
53***************************************************************************************************/
54static INT32 sa_val_ldac[LDAC_MAXNSPS] = { /* Q31 */
55    0xa0000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
56    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
57};
58
59DECLFUNC void norm_spectrum_ldac(
60AC *p_ac)
61{
62    int iqu, isp;
63    int lsp, hsp;
64    int nqus = p_ac->p_ab->nqus;
65    int idsf;
66    INT32 maxspec, tmp;
67    INT32 *p_spec = p_ac->p_acsub->a_spec;
68
69    for (iqu = 0; iqu < nqus; iqu++) {
70        lsp = ga_isp_ldac[iqu];
71        hsp = ga_isp_ldac[iqu+1];
72
73        maxspec = abs(p_spec[lsp]);
74        for (isp = lsp+1; isp < hsp; isp++) {
75            tmp = abs(p_spec[isp]);
76            if (maxspec < tmp) {
77                maxspec = tmp;
78            }
79        }
80        idsf = get_scale_factor_id_ldac(maxspec);
81
82        if (idsf > 0) {
83            for (isp = lsp; isp < hsp; isp++) {
84                p_spec[isp] = sftrnd_ldac(p_spec[isp], idsf-LDAC_Q_NORM);
85            }
86        }
87        else {
88            for (isp = lsp; isp < hsp; isp++) {
89                p_spec[isp] = sa_val_ldac[isp-lsp];
90            }
91        }
92
93        p_ac->a_idsf[iqu] = idsf;
94    }
95
96    return;
97}
98
99/***************************************************************************************************
100    Subfunction: Quantize Spectrum Core
101***************************************************************************************************/
102__inline static void quant_spectrum_core_ldac(
103AC *p_ac,
104int iqu)
105{
106    int i;
107    int isp = ga_isp_ldac[iqu];
108    int nsps = ga_nsps_ldac[iqu];
109    int *p_qspec = p_ac->a_qspec+isp;
110    INT32 qf = ga_qf_ldac[p_ac->a_idwl1[iqu]];
111    INT32 *p_nspec = p_ac->p_acsub->a_spec+isp;
112
113    for (i = 0; i < nsps; i++) {
114        /* Q00 <- Q31 * Q16 */
115        p_qspec[i] = mul_rsftrnd_ldac(p_nspec[i], qf, LDAC_Q_QUANT1);
116    }
117
118    return;
119}
120
121/***************************************************************************************************
122    Quantize Spectrum
123***************************************************************************************************/
124DECLFUNC void quant_spectrum_ldac(
125AC *p_ac)
126{
127    int iqu;
128    int nqus = p_ac->p_ab->nqus;
129
130    for (iqu = 0; iqu < nqus; iqu++) {
131        quant_spectrum_core_ldac(p_ac, iqu);
132    }
133
134    return;
135}
136
137/***************************************************************************************************
138    Subfunction: Quantize Residual Spectrum Core
139***************************************************************************************************/
140__inline static void quant_residual_core_ldac(
141AC *p_ac,
142int iqu)
143{
144    int i;
145    int isp = ga_isp_ldac[iqu];
146    int nsps = ga_nsps_ldac[iqu];
147    int *p_qspec = p_ac->a_qspec+isp;
148    int *p_rspec = p_ac->a_rspec+isp;
149    INT32 ldqspec, rnspec;
150    INT32 iqf = ga_iqf_ldac[LDAC_MAXIDWL1];
151    INT32 rqf = ga_qf_ldac[p_ac->a_idwl2[iqu]];
152    INT32 irsf = ga_irsf_ldac[LDAC_MAXIDWL1];
153    INT32 *p_nspec = p_ac->p_acsub->a_spec+isp;
154
155    for (i = 0; i < nsps; i++) {
156        /* Q31 <- Q00 * Q31 */
157        ldqspec = mul_lsftrnd_ldac(p_qspec[i], iqf, LDAC_Q_QUANT2);
158        /* Q31 <- (Q31 - Q31) * Q15 */
159        rnspec = mul_rsftrnd_ldac(p_nspec[i]-ldqspec, irsf, LDAC_Q_QUANT3);
160        /* Q00 <- Q31 * Q16 */
161        p_rspec[i] = mul_rsftrnd_ldac(rnspec, rqf, LDAC_Q_QUANT4);
162    }
163
164    return;
165}
166
167/***************************************************************************************************
168    Quantize Residual Spectrum
169***************************************************************************************************/
170DECLFUNC void quant_residual_ldac(
171AC *p_ac)
172{
173    int iqu;
174    int nqus = p_ac->p_ab->nqus;
175    int	*p_idwl2 = p_ac->a_idwl2;
176
177    for (iqu = 0; iqu < nqus; iqu++) {
178        if (p_idwl2[iqu] > 0) {
179            quant_residual_core_ldac(p_ac, iqu);
180        }
181    }
182
183    return;
184}
185
186