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