|
1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "resampler_sinc_conv_one_to_three_int16.h" |
|
21 #include "resampler_clip.h" |
|
22 #include "resampler_sinc_conv_one_to_three_tables_standard.h" |
|
23 |
|
24 #include <string.h> |
|
25 |
|
26 #include "resampler_sinc_conv_one_to_three_int16.inl" |
|
27 |
|
28 static const int LC_ZERO_CROSSINGS = RESAMPLER_ONE_TO_THREE_ZERO_CROSSINGS_STANDARD; |
|
29 static const int LC_MOD3[6] = { 0, 1, 2, 0, 1, 2 }; |
|
30 |
|
31 |
|
32 RESAMPLER_SincConvOneToThreeInt16::RESAMPLER_SincConvOneToThreeInt16(int channelCount) : |
|
33 m_memBuffers(0), |
|
34 m_scratchBuffer(0), |
|
35 m_filter1(RESAMPLER_ONE_TO_THREE_FILTER1_STANDARD), |
|
36 m_filter2(RESAMPLER_ONE_TO_THREE_FILTER2_STANDARD), |
|
37 m_channelCount(channelCount), |
|
38 m_blockSize(0), |
|
39 m_channelEnabled(0), |
|
40 m_inputSamples(0), |
|
41 m_state(2) |
|
42 { |
|
43 } |
|
44 |
|
45 |
|
46 RESAMPLER_SincConvOneToThreeInt16::~RESAMPLER_SincConvOneToThreeInt16() |
|
47 { |
|
48 DeInit(); |
|
49 } |
|
50 |
|
51 |
|
52 bool RESAMPLER_SincConvOneToThreeInt16::InitInputDriven() |
|
53 { |
|
54 return Init(); |
|
55 } |
|
56 |
|
57 |
|
58 bool RESAMPLER_SincConvOneToThreeInt16::InitOutputDriven() |
|
59 { |
|
60 return Init(); |
|
61 } |
|
62 |
|
63 |
|
64 /* |
|
65 * This function must be called before using the converter. |
|
66 * This function reserves memory for ring buffers and |
|
67 * prepares the ring buffer indexing. |
|
68 */ |
|
69 bool RESAMPLER_SincConvOneToThreeInt16::Init() |
|
70 { |
|
71 int i(0); |
|
72 |
|
73 m_memBuffers = new int16 *[m_channelCount]; |
|
74 if (!m_memBuffers) |
|
75 { |
|
76 return false; |
|
77 } |
|
78 |
|
79 for (i = 0; i < m_channelCount; i++) |
|
80 { |
|
81 m_memBuffers[i] = 0; |
|
82 } |
|
83 |
|
84 m_channelEnabled = new bool[m_channelCount]; |
|
85 if (!m_channelEnabled) |
|
86 { |
|
87 DeInit(); |
|
88 return false; |
|
89 } |
|
90 for (i = 0; i < m_channelCount; i++) |
|
91 { |
|
92 m_channelEnabled[i] = true; |
|
93 } |
|
94 |
|
95 for (i = 0; i < m_channelCount; i++) |
|
96 { |
|
97 m_memBuffers[i] = new int16[LC_ZERO_CROSSINGS * 2]; |
|
98 if (!m_memBuffers[i]) |
|
99 { |
|
100 DeInit(); |
|
101 return false; |
|
102 } |
|
103 memset(m_memBuffers[i], 0, sizeof(int16) * (LC_ZERO_CROSSINGS * 2)); |
|
104 } |
|
105 |
|
106 return true; |
|
107 } |
|
108 |
|
109 |
|
110 void RESAMPLER_SincConvOneToThreeInt16::DeInit() |
|
111 { |
|
112 if (m_channelCount) |
|
113 { |
|
114 for (int i = 0; i < m_channelCount; i++) |
|
115 { |
|
116 delete [] m_memBuffers[i]; |
|
117 } |
|
118 delete [] m_memBuffers; |
|
119 delete [] m_channelEnabled; |
|
120 } |
|
121 } |
|
122 |
|
123 |
|
124 void RESAMPLER_SincConvOneToThreeInt16::EnableChannel(int channel) |
|
125 { |
|
126 m_channelEnabled[channel] = true; |
|
127 } |
|
128 |
|
129 |
|
130 void RESAMPLER_SincConvOneToThreeInt16::DisableChannel(int channel) |
|
131 { |
|
132 m_channelEnabled[channel] = false; |
|
133 } |
|
134 |
|
135 |
|
136 int RESAMPLER_SincConvOneToThreeInt16::MaxOutputSampleCount(int inSamples) const |
|
137 { |
|
138 return 3*inSamples; |
|
139 } |
|
140 |
|
141 |
|
142 int RESAMPLER_SincConvOneToThreeInt16::ProcessFromInput(int16 *outputBuffers[], |
|
143 int16 *inputBuffers[], |
|
144 int inSamples) |
|
145 { |
|
146 int outSamples(3 * inSamples); |
|
147 m_inputSamples = inSamples; |
|
148 return ProcessToOutput(outputBuffers, inputBuffers, outSamples); |
|
149 } |
|
150 |
|
151 |
|
152 size_t |
|
153 RESAMPLER_SincConvOneToThreeInt16::ScratchMemoryNeedOutputDriven(int maxOutputBlockSize) const |
|
154 { |
|
155 return ScratchMemoryNeedInputDriven((maxOutputBlockSize + 2) / 3); |
|
156 } |
|
157 |
|
158 |
|
159 void |
|
160 RESAMPLER_SincConvOneToThreeInt16::SetScratchBufferOutputDriven(char *buffer) |
|
161 { |
|
162 m_scratchBuffer = (int16 *)buffer; |
|
163 } |
|
164 |
|
165 size_t |
|
166 RESAMPLER_SincConvOneToThreeInt16::ScratchMemoryNeedInputDriven(int maxInputBlockSize) const |
|
167 { |
|
168 return sizeof(int16) * (LC_ZERO_CROSSINGS * 2 + maxInputBlockSize); |
|
169 } |
|
170 |
|
171 |
|
172 void |
|
173 RESAMPLER_SincConvOneToThreeInt16::SetScratchBufferInputDriven(char *buffer) |
|
174 { |
|
175 m_scratchBuffer = (int16 *)buffer; |
|
176 } |
|
177 |
|
178 |
|
179 int RESAMPLER_SincConvOneToThreeInt16::MaxInputSampleCount(int outSamples) const |
|
180 { |
|
181 return (outSamples + 2) / 3; |
|
182 } |
|
183 |
|
184 |
|
185 /* |
|
186 * This function returns the value, which is given as |
|
187 * a parameter to the nextSamplesOut() function. This |
|
188 * function must be called every time before calling |
|
189 * nextSamplesOut. |
|
190 */ |
|
191 int RESAMPLER_SincConvOneToThreeInt16::InSamplesNeeded(int outSamples) |
|
192 { |
|
193 m_inputSamples = (outSamples + LC_MOD3[4 - m_state]) / 3; |
|
194 |
|
195 return m_inputSamples; |
|
196 } |
|
197 |
|
198 |
|
199 int RESAMPLER_SincConvOneToThreeInt16::ProcessToOutput(int16 *outputBuffers[], |
|
200 int16 *inputBuffers[], |
|
201 int outSamples) |
|
202 { |
|
203 static const int FILTER_LENGTH = 2 * LC_ZERO_CROSSINGS; |
|
204 int i, j, k; |
|
205 int bufReadHead1 = LC_ZERO_CROSSINGS + 1 - (m_state != 2 ? 1 : 0); |
|
206 int bufReadHead2 = LC_ZERO_CROSSINGS; |
|
207 int bufReadHead3 = LC_ZERO_CROSSINGS - (m_state == 1 ? 1 : 0); |
|
208 |
|
209 for (i = 0; i < m_channelCount; i++) |
|
210 { |
|
211 if (!m_channelEnabled[i]) |
|
212 { |
|
213 break; |
|
214 } |
|
215 |
|
216 int16 *tempBuf = m_scratchBuffer; |
|
217 int16 *outBuf = outputBuffers[i]; |
|
218 |
|
219 memcpy(m_scratchBuffer, m_memBuffers[i], FILTER_LENGTH * sizeof(int16)); |
|
220 |
|
221 memcpy(tempBuf + FILTER_LENGTH, |
|
222 inputBuffers[i], |
|
223 m_inputSamples * sizeof(int16)); |
|
224 |
|
225 // copy samples into every third index of output buffer. |
|
226 tempBuf = m_scratchBuffer + bufReadHead1; |
|
227 for (j = 0, k = m_state; k < outSamples; j++, k+=3) |
|
228 { |
|
229 outBuf[k] = tempBuf[j]; |
|
230 } |
|
231 |
|
232 // Do band limited interpolation and set the result into |
|
233 // every third index in the output buffer. |
|
234 tempBuf = m_scratchBuffer + bufReadHead2; |
|
235 for (j = 0, k = LC_MOD3[m_state+1]; k < outSamples; j++, k += 3) |
|
236 { |
|
237 int32 newSample(0); |
|
238 // Note that the filters are reversed |
|
239 newSample = |
|
240 RESAMPLER_SincConvOneToThreeFilterInt16(tempBuf + j + 1, |
|
241 m_filter2, |
|
242 m_filter1, |
|
243 LC_ZERO_CROSSINGS); |
|
244 // round and shift down |
|
245 outBuf[k] = (int16)RESAMPLER_Clip16((newSample + 16384) >> 15); |
|
246 } |
|
247 |
|
248 // Do band limited interpolation and set the result into |
|
249 // every third index in the output buffer. |
|
250 tempBuf = m_scratchBuffer + bufReadHead3; |
|
251 for (j = 0, k = LC_MOD3[m_state+2]; k < outSamples; j++, k += 3) |
|
252 { |
|
253 int32 newSample(0); |
|
254 newSample = |
|
255 RESAMPLER_SincConvOneToThreeFilterInt16(tempBuf + j + 1, |
|
256 m_filter1, |
|
257 m_filter2, |
|
258 LC_ZERO_CROSSINGS); |
|
259 // round and shift down |
|
260 outBuf[k] = (int16)RESAMPLER_Clip16((newSample + 16384) >> 15); |
|
261 } |
|
262 |
|
263 // Copy the newest samples to the beginning of the buffer |
|
264 memcpy(m_memBuffers[i], |
|
265 m_scratchBuffer + m_inputSamples, |
|
266 FILTER_LENGTH * sizeof(int16)); |
|
267 } |
|
268 |
|
269 // Update state according to amount of output samples. |
|
270 m_state = LC_MOD3[m_state + (3 - (outSamples % 3))]; |
|
271 |
|
272 return outSamples; |
|
273 } |
|
274 |
|
275 |