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