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 resampling functions between 48 kHz and nb/wb.
14 * The description header can be found in signal_processing_library.h
15 *
16 */
17
18#include <string.h>
19#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
20#include "webrtc/common_audio/signal_processing/resample_by_2_internal.h"
21
22////////////////////////////
23///// 48 kHz -> 16 kHz /////
24////////////////////////////
25
26// 48 -> 16 resampler
27void WebRtcSpl_Resample48khzTo16khz(const int16_t* in, int16_t* out,
28                                    WebRtcSpl_State48khzTo16khz* state, int32_t* tmpmem)
29{
30    ///// 48 --> 48(LP) /////
31    // int16_t  in[480]
32    // int32_t out[480]
33    /////
34    WebRtcSpl_LPBy2ShortToInt(in, 480, tmpmem + 16, state->S_48_48);
35
36    ///// 48 --> 32 /////
37    // int32_t  in[480]
38    // int32_t out[320]
39    /////
40    // copy state to and from input array
41    memcpy(tmpmem + 8, state->S_48_32, 8 * sizeof(int32_t));
42    memcpy(state->S_48_32, tmpmem + 488, 8 * sizeof(int32_t));
43    WebRtcSpl_Resample48khzTo32khz(tmpmem + 8, tmpmem, 160);
44
45    ///// 32 --> 16 /////
46    // int32_t  in[320]
47    // int16_t out[160]
48    /////
49    WebRtcSpl_DownBy2IntToShort(tmpmem, 320, out, state->S_32_16);
50}
51
52// initialize state of 48 -> 16 resampler
53void WebRtcSpl_ResetResample48khzTo16khz(WebRtcSpl_State48khzTo16khz* state)
54{
55    memset(state->S_48_48, 0, 16 * sizeof(int32_t));
56    memset(state->S_48_32, 0, 8 * sizeof(int32_t));
57    memset(state->S_32_16, 0, 8 * sizeof(int32_t));
58}
59
60////////////////////////////
61///// 16 kHz -> 48 kHz /////
62////////////////////////////
63
64// 16 -> 48 resampler
65void WebRtcSpl_Resample16khzTo48khz(const int16_t* in, int16_t* out,
66                                    WebRtcSpl_State16khzTo48khz* state, int32_t* tmpmem)
67{
68    ///// 16 --> 32 /////
69    // int16_t  in[160]
70    // int32_t out[320]
71    /////
72    WebRtcSpl_UpBy2ShortToInt(in, 160, tmpmem + 16, state->S_16_32);
73
74    ///// 32 --> 24 /////
75    // int32_t  in[320]
76    // int32_t out[240]
77    // copy state to and from input array
78    /////
79    memcpy(tmpmem + 8, state->S_32_24, 8 * sizeof(int32_t));
80    memcpy(state->S_32_24, tmpmem + 328, 8 * sizeof(int32_t));
81    WebRtcSpl_Resample32khzTo24khz(tmpmem + 8, tmpmem, 80);
82
83    ///// 24 --> 48 /////
84    // int32_t  in[240]
85    // int16_t out[480]
86    /////
87    WebRtcSpl_UpBy2IntToShort(tmpmem, 240, out, state->S_24_48);
88}
89
90// initialize state of 16 -> 48 resampler
91void WebRtcSpl_ResetResample16khzTo48khz(WebRtcSpl_State16khzTo48khz* state)
92{
93    memset(state->S_16_32, 0, 8 * sizeof(int32_t));
94    memset(state->S_32_24, 0, 8 * sizeof(int32_t));
95    memset(state->S_24_48, 0, 8 * sizeof(int32_t));
96}
97
98////////////////////////////
99///// 48 kHz ->  8 kHz /////
100////////////////////////////
101
102// 48 -> 8 resampler
103void WebRtcSpl_Resample48khzTo8khz(const int16_t* in, int16_t* out,
104                                   WebRtcSpl_State48khzTo8khz* state, int32_t* tmpmem)
105{
106    ///// 48 --> 24 /////
107    // int16_t  in[480]
108    // int32_t out[240]
109    /////
110    WebRtcSpl_DownBy2ShortToInt(in, 480, tmpmem + 256, state->S_48_24);
111
112    ///// 24 --> 24(LP) /////
113    // int32_t  in[240]
114    // int32_t out[240]
115    /////
116    WebRtcSpl_LPBy2IntToInt(tmpmem + 256, 240, tmpmem + 16, state->S_24_24);
117
118    ///// 24 --> 16 /////
119    // int32_t  in[240]
120    // int32_t out[160]
121    /////
122    // copy state to and from input array
123    memcpy(tmpmem + 8, state->S_24_16, 8 * sizeof(int32_t));
124    memcpy(state->S_24_16, tmpmem + 248, 8 * sizeof(int32_t));
125    WebRtcSpl_Resample48khzTo32khz(tmpmem + 8, tmpmem, 80);
126
127    ///// 16 --> 8 /////
128    // int32_t  in[160]
129    // int16_t out[80]
130    /////
131    WebRtcSpl_DownBy2IntToShort(tmpmem, 160, out, state->S_16_8);
132}
133
134// initialize state of 48 -> 8 resampler
135void WebRtcSpl_ResetResample48khzTo8khz(WebRtcSpl_State48khzTo8khz* state)
136{
137    memset(state->S_48_24, 0, 8 * sizeof(int32_t));
138    memset(state->S_24_24, 0, 16 * sizeof(int32_t));
139    memset(state->S_24_16, 0, 8 * sizeof(int32_t));
140    memset(state->S_16_8, 0, 8 * sizeof(int32_t));
141}
142
143////////////////////////////
144/////  8 kHz -> 48 kHz /////
145////////////////////////////
146
147// 8 -> 48 resampler
148void WebRtcSpl_Resample8khzTo48khz(const int16_t* in, int16_t* out,
149                                   WebRtcSpl_State8khzTo48khz* state, int32_t* tmpmem)
150{
151    ///// 8 --> 16 /////
152    // int16_t  in[80]
153    // int32_t out[160]
154    /////
155    WebRtcSpl_UpBy2ShortToInt(in, 80, tmpmem + 264, state->S_8_16);
156
157    ///// 16 --> 12 /////
158    // int32_t  in[160]
159    // int32_t out[120]
160    /////
161    // copy state to and from input array
162    memcpy(tmpmem + 256, state->S_16_12, 8 * sizeof(int32_t));
163    memcpy(state->S_16_12, tmpmem + 416, 8 * sizeof(int32_t));
164    WebRtcSpl_Resample32khzTo24khz(tmpmem + 256, tmpmem + 240, 40);
165
166    ///// 12 --> 24 /////
167    // int32_t  in[120]
168    // int16_t out[240]
169    /////
170    WebRtcSpl_UpBy2IntToInt(tmpmem + 240, 120, tmpmem, state->S_12_24);
171
172    ///// 24 --> 48 /////
173    // int32_t  in[240]
174    // int16_t out[480]
175    /////
176    WebRtcSpl_UpBy2IntToShort(tmpmem, 240, out, state->S_24_48);
177}
178
179// initialize state of 8 -> 48 resampler
180void WebRtcSpl_ResetResample8khzTo48khz(WebRtcSpl_State8khzTo48khz* state)
181{
182    memset(state->S_8_16, 0, 8 * sizeof(int32_t));
183    memset(state->S_16_12, 0, 8 * sizeof(int32_t));
184    memset(state->S_12_24, 0, 8 * sizeof(int32_t));
185    memset(state->S_24_48, 0, 8 * sizeof(int32_t));
186}
187