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/*
13 * This file contains the resampling functions between 48, 44, 32 and 24 kHz.
14 * The description headers can be found in signal_processing_library.h
15 *
16 */
17
18#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
19
20// interpolation coefficients
21static const int16_t kCoefficients48To32[2][8] = {
22        {778, -2050, 1087, 23285, 12903, -3783, 441, 222},
23        {222, 441, -3783, 12903, 23285, 1087, -2050, 778}
24};
25
26static const int16_t kCoefficients32To24[3][8] = {
27        {767, -2362, 2434, 24406, 10620, -3838, 721, 90},
28        {386, -381, -2646, 19062, 19062, -2646, -381, 386},
29        {90, 721, -3838, 10620, 24406, 2434, -2362, 767}
30};
31
32static const int16_t kCoefficients44To32[4][9] = {
33        {117, -669, 2245, -6183, 26267, 13529, -3245, 845, -138},
34        {-101, 612, -2283, 8532, 29790, -5138, 1789, -524, 91},
35        {50, -292, 1016, -3064, 32010, 3933, -1147, 315, -53},
36        {-156, 974, -3863, 18603, 21691, -6246, 2353, -712, 126}
37};
38
39//   Resampling ratio: 2/3
40// input:  int32_t (normalized, not saturated) :: size 3 * K
41// output: int32_t (shifted 15 positions to the left, + offset 16384) :: size 2 * K
42//      K: number of blocks
43
44void WebRtcSpl_Resample48khzTo32khz(const int32_t *In, int32_t *Out, size_t K)
45{
46    /////////////////////////////////////////////////////////////
47    // Filter operation:
48    //
49    // Perform resampling (3 input samples -> 2 output samples);
50    // process in sub blocks of size 3 samples.
51    int32_t tmp;
52    size_t m;
53
54    for (m = 0; m < K; m++)
55    {
56        tmp = 1 << 14;
57        tmp += kCoefficients48To32[0][0] * In[0];
58        tmp += kCoefficients48To32[0][1] * In[1];
59        tmp += kCoefficients48To32[0][2] * In[2];
60        tmp += kCoefficients48To32[0][3] * In[3];
61        tmp += kCoefficients48To32[0][4] * In[4];
62        tmp += kCoefficients48To32[0][5] * In[5];
63        tmp += kCoefficients48To32[0][6] * In[6];
64        tmp += kCoefficients48To32[0][7] * In[7];
65        Out[0] = tmp;
66
67        tmp = 1 << 14;
68        tmp += kCoefficients48To32[1][0] * In[1];
69        tmp += kCoefficients48To32[1][1] * In[2];
70        tmp += kCoefficients48To32[1][2] * In[3];
71        tmp += kCoefficients48To32[1][3] * In[4];
72        tmp += kCoefficients48To32[1][4] * In[5];
73        tmp += kCoefficients48To32[1][5] * In[6];
74        tmp += kCoefficients48To32[1][6] * In[7];
75        tmp += kCoefficients48To32[1][7] * In[8];
76        Out[1] = tmp;
77
78        // update pointers
79        In += 3;
80        Out += 2;
81    }
82}
83
84//   Resampling ratio: 3/4
85// input:  int32_t (normalized, not saturated) :: size 4 * K
86// output: int32_t (shifted 15 positions to the left, + offset 16384) :: size 3 * K
87//      K: number of blocks
88
89void WebRtcSpl_Resample32khzTo24khz(const int32_t *In, int32_t *Out, size_t K)
90{
91    /////////////////////////////////////////////////////////////
92    // Filter operation:
93    //
94    // Perform resampling (4 input samples -> 3 output samples);
95    // process in sub blocks of size 4 samples.
96    size_t m;
97    int32_t tmp;
98
99    for (m = 0; m < K; m++)
100    {
101        tmp = 1 << 14;
102        tmp += kCoefficients32To24[0][0] * In[0];
103        tmp += kCoefficients32To24[0][1] * In[1];
104        tmp += kCoefficients32To24[0][2] * In[2];
105        tmp += kCoefficients32To24[0][3] * In[3];
106        tmp += kCoefficients32To24[0][4] * In[4];
107        tmp += kCoefficients32To24[0][5] * In[5];
108        tmp += kCoefficients32To24[0][6] * In[6];
109        tmp += kCoefficients32To24[0][7] * In[7];
110        Out[0] = tmp;
111
112        tmp = 1 << 14;
113        tmp += kCoefficients32To24[1][0] * In[1];
114        tmp += kCoefficients32To24[1][1] * In[2];
115        tmp += kCoefficients32To24[1][2] * In[3];
116        tmp += kCoefficients32To24[1][3] * In[4];
117        tmp += kCoefficients32To24[1][4] * In[5];
118        tmp += kCoefficients32To24[1][5] * In[6];
119        tmp += kCoefficients32To24[1][6] * In[7];
120        tmp += kCoefficients32To24[1][7] * In[8];
121        Out[1] = tmp;
122
123        tmp = 1 << 14;
124        tmp += kCoefficients32To24[2][0] * In[2];
125        tmp += kCoefficients32To24[2][1] * In[3];
126        tmp += kCoefficients32To24[2][2] * In[4];
127        tmp += kCoefficients32To24[2][3] * In[5];
128        tmp += kCoefficients32To24[2][4] * In[6];
129        tmp += kCoefficients32To24[2][5] * In[7];
130        tmp += kCoefficients32To24[2][6] * In[8];
131        tmp += kCoefficients32To24[2][7] * In[9];
132        Out[2] = tmp;
133
134        // update pointers
135        In += 4;
136        Out += 3;
137    }
138}
139
140//
141// fractional resampling filters
142//   Fout = 11/16 * Fin
143//   Fout =  8/11 * Fin
144//
145
146// compute two inner-products and store them to output array
147static void WebRtcSpl_ResampDotProduct(const int32_t *in1, const int32_t *in2,
148                                       const int16_t *coef_ptr, int32_t *out1,
149                                       int32_t *out2)
150{
151    int32_t tmp1 = 16384;
152    int32_t tmp2 = 16384;
153    int16_t coef;
154
155    coef = coef_ptr[0];
156    tmp1 += coef * in1[0];
157    tmp2 += coef * in2[-0];
158
159    coef = coef_ptr[1];
160    tmp1 += coef * in1[1];
161    tmp2 += coef * in2[-1];
162
163    coef = coef_ptr[2];
164    tmp1 += coef * in1[2];
165    tmp2 += coef * in2[-2];
166
167    coef = coef_ptr[3];
168    tmp1 += coef * in1[3];
169    tmp2 += coef * in2[-3];
170
171    coef = coef_ptr[4];
172    tmp1 += coef * in1[4];
173    tmp2 += coef * in2[-4];
174
175    coef = coef_ptr[5];
176    tmp1 += coef * in1[5];
177    tmp2 += coef * in2[-5];
178
179    coef = coef_ptr[6];
180    tmp1 += coef * in1[6];
181    tmp2 += coef * in2[-6];
182
183    coef = coef_ptr[7];
184    tmp1 += coef * in1[7];
185    tmp2 += coef * in2[-7];
186
187    coef = coef_ptr[8];
188    *out1 = tmp1 + coef * in1[8];
189    *out2 = tmp2 + coef * in2[-8];
190}
191
192//   Resampling ratio: 8/11
193// input:  int32_t (normalized, not saturated) :: size 11 * K
194// output: int32_t (shifted 15 positions to the left, + offset 16384) :: size  8 * K
195//      K: number of blocks
196
197void WebRtcSpl_Resample44khzTo32khz(const int32_t *In, int32_t *Out, size_t K)
198{
199    /////////////////////////////////////////////////////////////
200    // Filter operation:
201    //
202    // Perform resampling (11 input samples -> 8 output samples);
203    // process in sub blocks of size 11 samples.
204    int32_t tmp;
205    size_t m;
206
207    for (m = 0; m < K; m++)
208    {
209        tmp = 1 << 14;
210
211        // first output sample
212        Out[0] = ((int32_t)In[3] << 15) + tmp;
213
214        // sum and accumulate filter coefficients and input samples
215        tmp += kCoefficients44To32[3][0] * In[5];
216        tmp += kCoefficients44To32[3][1] * In[6];
217        tmp += kCoefficients44To32[3][2] * In[7];
218        tmp += kCoefficients44To32[3][3] * In[8];
219        tmp += kCoefficients44To32[3][4] * In[9];
220        tmp += kCoefficients44To32[3][5] * In[10];
221        tmp += kCoefficients44To32[3][6] * In[11];
222        tmp += kCoefficients44To32[3][7] * In[12];
223        tmp += kCoefficients44To32[3][8] * In[13];
224        Out[4] = tmp;
225
226        // sum and accumulate filter coefficients and input samples
227        WebRtcSpl_ResampDotProduct(&In[0], &In[17], kCoefficients44To32[0], &Out[1], &Out[7]);
228
229        // sum and accumulate filter coefficients and input samples
230        WebRtcSpl_ResampDotProduct(&In[2], &In[15], kCoefficients44To32[1], &Out[2], &Out[6]);
231
232        // sum and accumulate filter coefficients and input samples
233        WebRtcSpl_ResampDotProduct(&In[3], &In[14], kCoefficients44To32[2], &Out[3], &Out[5]);
234
235        // update pointers
236        In += 11;
237        Out += 8;
238    }
239}
240