1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 /*
12 * WebRtcIsacfix_kTransform.c
13 *
14 * Transform functions
15 *
16 */
17
18 #include "modules/audio_coding/codecs/isac/fix/source/codec.h"
19 #include "modules/audio_coding/codecs/isac/fix/source/fft.h"
20 #include "modules/audio_coding/codecs/isac/fix/source/settings.h"
21 #include "modules/third_party/fft/fft.h"
22
23 /* Tables are defined in transform_tables.c file or ARM assembly files. */
24 /* Cosine table 1 in Q14 */
25 extern const int16_t WebRtcIsacfix_kCosTab1[FRAMESAMPLES/2];
26 /* Sine table 1 in Q14 */
27 extern const int16_t WebRtcIsacfix_kSinTab1[FRAMESAMPLES/2];
28 /* Sine table 2 in Q14 */
29 extern const int16_t WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4];
30
WebRtcIsacfix_Time2SpecC(int16_t * inre1Q9,int16_t * inre2Q9,int16_t * outreQ7,int16_t * outimQ7)31 void WebRtcIsacfix_Time2SpecC(int16_t *inre1Q9,
32 int16_t *inre2Q9,
33 int16_t *outreQ7,
34 int16_t *outimQ7)
35 {
36
37 int k;
38 int32_t tmpreQ16[FRAMESAMPLES/2], tmpimQ16[FRAMESAMPLES/2];
39 int16_t tmp1rQ14, tmp1iQ14;
40 int32_t xrQ16, xiQ16, yrQ16, yiQ16;
41 int32_t v1Q16, v2Q16;
42 int16_t factQ19, sh;
43
44 /* Multiply with complex exponentials and combine into one complex vector */
45 factQ19 = 16921; // 0.5/sqrt(240) in Q19 is round(.5/sqrt(240)*(2^19)) = 16921
46 for (k = 0; k < FRAMESAMPLES/2; k++) {
47 tmp1rQ14 = WebRtcIsacfix_kCosTab1[k];
48 tmp1iQ14 = WebRtcIsacfix_kSinTab1[k];
49 xrQ16 = (tmp1rQ14 * inre1Q9[k] + tmp1iQ14 * inre2Q9[k]) >> 7;
50 xiQ16 = (tmp1rQ14 * inre2Q9[k] - tmp1iQ14 * inre1Q9[k]) >> 7;
51 // Q-domains below: (Q16*Q19>>16)>>3 = Q16
52 tmpreQ16[k] = (WEBRTC_SPL_MUL_16_32_RSFT16(factQ19, xrQ16) + 4) >> 3;
53 tmpimQ16[k] = (WEBRTC_SPL_MUL_16_32_RSFT16(factQ19, xiQ16) + 4) >> 3;
54 }
55
56
57 xrQ16 = WebRtcSpl_MaxAbsValueW32(tmpreQ16, FRAMESAMPLES/2);
58 yrQ16 = WebRtcSpl_MaxAbsValueW32(tmpimQ16, FRAMESAMPLES/2);
59 if (yrQ16>xrQ16) {
60 xrQ16 = yrQ16;
61 }
62
63 sh = WebRtcSpl_NormW32(xrQ16);
64 sh = sh-24; //if sh becomes >=0, then we should shift sh steps to the left, and the domain will become Q(16+sh)
65 //if sh becomes <0, then we should shift -sh steps to the right, and the domain will become Q(16+sh)
66
67 //"Fastest" vectors
68 if (sh>=0) {
69 for (k=0; k<FRAMESAMPLES/2; k++) {
70 inre1Q9[k] = (int16_t)(tmpreQ16[k] << sh); // Q(16+sh)
71 inre2Q9[k] = (int16_t)(tmpimQ16[k] << sh); // Q(16+sh)
72 }
73 } else {
74 int32_t round = 1 << (-sh - 1);
75 for (k=0; k<FRAMESAMPLES/2; k++) {
76 inre1Q9[k] = (int16_t)((tmpreQ16[k] + round) >> -sh); // Q(16+sh)
77 inre2Q9[k] = (int16_t)((tmpimQ16[k] + round) >> -sh); // Q(16+sh)
78 }
79 }
80
81 /* Get DFT */
82 WebRtcIsacfix_FftRadix16Fastest(inre1Q9, inre2Q9, -1); // real call
83
84 //"Fastest" vectors
85 if (sh>=0) {
86 for (k=0; k<FRAMESAMPLES/2; k++) {
87 tmpreQ16[k] = inre1Q9[k] >> sh; // Q(16+sh) -> Q16
88 tmpimQ16[k] = inre2Q9[k] >> sh; // Q(16+sh) -> Q16
89 }
90 } else {
91 for (k=0; k<FRAMESAMPLES/2; k++) {
92 tmpreQ16[k] = inre1Q9[k] << -sh; // Q(16+sh) -> Q16
93 tmpimQ16[k] = inre2Q9[k] << -sh; // Q(16+sh) -> Q16
94 }
95 }
96
97
98 /* Use symmetry to separate into two complex vectors and center frames in time around zero */
99 for (k = 0; k < FRAMESAMPLES/4; k++) {
100 xrQ16 = tmpreQ16[k] + tmpreQ16[FRAMESAMPLES/2 - 1 - k];
101 yiQ16 = -tmpreQ16[k] + tmpreQ16[FRAMESAMPLES/2 - 1 - k];
102 xiQ16 = tmpimQ16[k] - tmpimQ16[FRAMESAMPLES/2 - 1 - k];
103 yrQ16 = tmpimQ16[k] + tmpimQ16[FRAMESAMPLES/2 - 1 - k];
104 tmp1rQ14 = -WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4 - 1 - k];
105 tmp1iQ14 = WebRtcIsacfix_kSinTab2[k];
106 v1Q16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, xrQ16) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, xiQ16);
107 v2Q16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, xrQ16) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, xiQ16);
108 outreQ7[k] = (int16_t)(v1Q16 >> 9);
109 outimQ7[k] = (int16_t)(v2Q16 >> 9);
110 v1Q16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, yrQ16) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, yiQ16);
111 v2Q16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, yrQ16) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, yiQ16);
112 // CalcLrIntQ(v1Q16, 9);
113 outreQ7[FRAMESAMPLES / 2 - 1 - k] = (int16_t)(v1Q16 >> 9);
114 // CalcLrIntQ(v2Q16, 9);
115 outimQ7[FRAMESAMPLES / 2 - 1 - k] = (int16_t)(v2Q16 >> 9);
116
117 }
118 }
119
120
WebRtcIsacfix_Spec2TimeC(int16_t * inreQ7,int16_t * inimQ7,int32_t * outre1Q16,int32_t * outre2Q16)121 void WebRtcIsacfix_Spec2TimeC(int16_t *inreQ7, int16_t *inimQ7, int32_t *outre1Q16, int32_t *outre2Q16)
122 {
123
124 int k;
125 int16_t tmp1rQ14, tmp1iQ14;
126 int32_t xrQ16, xiQ16, yrQ16, yiQ16;
127 int32_t tmpInRe, tmpInIm, tmpInRe2, tmpInIm2;
128 int16_t factQ11;
129 int16_t sh;
130
131 for (k = 0; k < FRAMESAMPLES/4; k++) {
132 /* Move zero in time to beginning of frames */
133 tmp1rQ14 = -WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4 - 1 - k];
134 tmp1iQ14 = WebRtcIsacfix_kSinTab2[k];
135
136 tmpInRe = inreQ7[k] * (1 << 9); // Q7 -> Q16
137 tmpInIm = inimQ7[k] * (1 << 9); // Q7 -> Q16
138 tmpInRe2 = inreQ7[FRAMESAMPLES / 2 - 1 - k] * (1 << 9); // Q7 -> Q16
139 tmpInIm2 = inimQ7[FRAMESAMPLES / 2 - 1 - k] * (1 << 9); // Q7 -> Q16
140
141 xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInRe) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInIm);
142 xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInIm) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInRe);
143 yrQ16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInIm2) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInRe2);
144 yiQ16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInRe2) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInIm2);
145
146 /* Combine into one vector, z = x + j * y */
147 outre1Q16[k] = xrQ16 - yiQ16;
148 outre1Q16[FRAMESAMPLES/2 - 1 - k] = xrQ16 + yiQ16;
149 outre2Q16[k] = xiQ16 + yrQ16;
150 outre2Q16[FRAMESAMPLES/2 - 1 - k] = -xiQ16 + yrQ16;
151 }
152
153 /* Get IDFT */
154 tmpInRe = WebRtcSpl_MaxAbsValueW32(outre1Q16, 240);
155 tmpInIm = WebRtcSpl_MaxAbsValueW32(outre2Q16, 240);
156 if (tmpInIm>tmpInRe) {
157 tmpInRe = tmpInIm;
158 }
159
160 sh = WebRtcSpl_NormW32(tmpInRe);
161 sh = sh-24; //if sh becomes >=0, then we should shift sh steps to the left, and the domain will become Q(16+sh)
162 //if sh becomes <0, then we should shift -sh steps to the right, and the domain will become Q(16+sh)
163
164 //"Fastest" vectors
165 if (sh>=0) {
166 for (k=0; k<240; k++) {
167 inreQ7[k] = (int16_t)(outre1Q16[k] << sh); // Q(16+sh)
168 inimQ7[k] = (int16_t)(outre2Q16[k] << sh); // Q(16+sh)
169 }
170 } else {
171 int32_t round = 1 << (-sh - 1);
172 for (k=0; k<240; k++) {
173 inreQ7[k] = (int16_t)((outre1Q16[k] + round) >> -sh); // Q(16+sh)
174 inimQ7[k] = (int16_t)((outre2Q16[k] + round) >> -sh); // Q(16+sh)
175 }
176 }
177
178 WebRtcIsacfix_FftRadix16Fastest(inreQ7, inimQ7, 1); // real call
179
180 //"Fastest" vectors
181 if (sh>=0) {
182 for (k=0; k<240; k++) {
183 outre1Q16[k] = inreQ7[k] >> sh; // Q(16+sh) -> Q16
184 outre2Q16[k] = inimQ7[k] >> sh; // Q(16+sh) -> Q16
185 }
186 } else {
187 for (k=0; k<240; k++) {
188 outre1Q16[k] = inreQ7[k] * (1 << -sh); // Q(16+sh) -> Q16
189 outre2Q16[k] = inimQ7[k] * (1 << -sh); // Q(16+sh) -> Q16
190 }
191 }
192
193 /* Divide through by the normalizing constant: */
194 /* scale all values with 1/240, i.e. with 273 in Q16 */
195 /* 273/65536 ~= 0.0041656 */
196 /* 1/240 ~= 0.0041666 */
197 for (k=0; k<240; k++) {
198 outre1Q16[k] = WEBRTC_SPL_MUL_16_32_RSFT16(273, outre1Q16[k]);
199 outre2Q16[k] = WEBRTC_SPL_MUL_16_32_RSFT16(273, outre2Q16[k]);
200 }
201
202 /* Demodulate and separate */
203 factQ11 = 31727; // sqrt(240) in Q11 is round(15.49193338482967*2048) = 31727
204 for (k = 0; k < FRAMESAMPLES/2; k++) {
205 tmp1rQ14 = WebRtcIsacfix_kCosTab1[k];
206 tmp1iQ14 = WebRtcIsacfix_kSinTab1[k];
207 xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, outre1Q16[k]) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, outre2Q16[k]);
208 xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, outre2Q16[k]) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, outre1Q16[k]);
209 xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT11(factQ11, xrQ16);
210 xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT11(factQ11, xiQ16);
211 outre2Q16[k] = xiQ16;
212 outre1Q16[k] = xrQ16;
213 }
214 }
215