|
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_two_int16.h" |
|
21 #include "resampler_clip.h" |
|
22 #include "resampler_sinc_conv_one_to_two_tables_standard.h" |
|
23 |
|
24 #include <string.h> |
|
25 |
|
26 #include "resampler_sinc_conv_one_to_two_int16.inl" |
|
27 |
|
28 static const int LC_ZERO_CROSSINGS = RESAMPLER_ONE_TO_TWO_ZERO_CROSSINGS_STANDARD; /* Needs to be divisible by two */ |
|
29 |
|
30 |
|
31 RESAMPLER_SincConvOneToTwoInt16::RESAMPLER_SincConvOneToTwoInt16(int channelCount) : |
|
32 m_memBuffers(0), |
|
33 m_scratchBuffer(0), |
|
34 m_filter(RESAMPLER_ONE_TO_TWO_FILTERS_STANDARD), |
|
35 m_channelCount(channelCount), |
|
36 m_blockSize(0), |
|
37 m_channelEnabled(0), |
|
38 m_state(0) |
|
39 { |
|
40 } |
|
41 |
|
42 |
|
43 RESAMPLER_SincConvOneToTwoInt16::~RESAMPLER_SincConvOneToTwoInt16() |
|
44 { |
|
45 DeInit(); |
|
46 } |
|
47 |
|
48 |
|
49 bool RESAMPLER_SincConvOneToTwoInt16::InitInputDriven() |
|
50 { |
|
51 return Init(); |
|
52 } |
|
53 |
|
54 |
|
55 bool RESAMPLER_SincConvOneToTwoInt16::InitOutputDriven() |
|
56 { |
|
57 return Init(); |
|
58 } |
|
59 |
|
60 |
|
61 bool RESAMPLER_SincConvOneToTwoInt16::Init() |
|
62 { |
|
63 int i(0); |
|
64 |
|
65 m_memBuffers = new int16 *[m_channelCount]; |
|
66 if (!m_memBuffers) |
|
67 { |
|
68 return false; |
|
69 } |
|
70 |
|
71 for (i = 0; i < m_channelCount; i++) |
|
72 { |
|
73 m_memBuffers[i] = 0; |
|
74 } |
|
75 |
|
76 m_channelCount = m_channelCount; |
|
77 |
|
78 m_channelEnabled = new bool[m_channelCount]; |
|
79 if (!m_channelEnabled) |
|
80 { |
|
81 DeInit(); |
|
82 return false; |
|
83 } |
|
84 for (i = 0; i < m_channelCount; i++) |
|
85 { |
|
86 m_channelEnabled[i] = true; |
|
87 } |
|
88 |
|
89 for (i = 0; i < m_channelCount; i++) |
|
90 { |
|
91 m_memBuffers[i] = new int16[LC_ZERO_CROSSINGS * 2]; |
|
92 if (!m_memBuffers[i]) |
|
93 { |
|
94 DeInit(); |
|
95 return false; |
|
96 } |
|
97 memset(m_memBuffers[i], 0, sizeof(int16) * (LC_ZERO_CROSSINGS * 2)); |
|
98 } |
|
99 |
|
100 return true; |
|
101 } |
|
102 |
|
103 |
|
104 void RESAMPLER_SincConvOneToTwoInt16::DeInit() |
|
105 { |
|
106 if (m_channelCount) |
|
107 { |
|
108 for (int i = 0; i < m_channelCount; i++) |
|
109 { |
|
110 delete [] m_memBuffers[i]; |
|
111 } |
|
112 delete [] m_memBuffers; |
|
113 delete [] m_channelEnabled; |
|
114 } |
|
115 } |
|
116 |
|
117 |
|
118 void RESAMPLER_SincConvOneToTwoInt16::EnableChannel(int channel) |
|
119 { |
|
120 m_channelEnabled[channel] = true; |
|
121 } |
|
122 |
|
123 |
|
124 void RESAMPLER_SincConvOneToTwoInt16::DisableChannel(int channel) |
|
125 { |
|
126 m_channelEnabled[channel] = false; |
|
127 } |
|
128 |
|
129 |
|
130 size_t |
|
131 RESAMPLER_SincConvOneToTwoInt16::ScratchMemoryNeedInputDriven(int maxInputBlockSize) const |
|
132 { |
|
133 return sizeof(int16) * (LC_ZERO_CROSSINGS * 2 + maxInputBlockSize); |
|
134 } |
|
135 |
|
136 |
|
137 void |
|
138 RESAMPLER_SincConvOneToTwoInt16::SetScratchBufferInputDriven(char *buffer) |
|
139 { |
|
140 m_scratchBuffer = (int16 *)buffer; |
|
141 } |
|
142 |
|
143 |
|
144 int RESAMPLER_SincConvOneToTwoInt16::MaxOutputSampleCount(int inSamples) const |
|
145 { |
|
146 return 2 * inSamples; |
|
147 } |
|
148 |
|
149 |
|
150 int RESAMPLER_SincConvOneToTwoInt16::ProcessFromInput(int16 *outputBuffers[], |
|
151 int16 *inputBuffers[], |
|
152 int inSamples) |
|
153 { |
|
154 int outSamples(2 * inSamples + m_state); |
|
155 return ProcessToOutput(outputBuffers, inputBuffers, outSamples); |
|
156 } |
|
157 |
|
158 |
|
159 size_t |
|
160 RESAMPLER_SincConvOneToTwoInt16::ScratchMemoryNeedOutputDriven(int maxOutputBlockSize) const |
|
161 { |
|
162 return ScratchMemoryNeedInputDriven((maxOutputBlockSize + 1) >> 1); |
|
163 } |
|
164 |
|
165 |
|
166 void |
|
167 RESAMPLER_SincConvOneToTwoInt16::SetScratchBufferOutputDriven(char *buffer) |
|
168 { |
|
169 m_scratchBuffer = (int16 *)buffer; |
|
170 } |
|
171 |
|
172 |
|
173 int RESAMPLER_SincConvOneToTwoInt16::MaxInputSampleCount(int outSamples) const |
|
174 { |
|
175 return (outSamples + 1) >> 1; |
|
176 } |
|
177 |
|
178 |
|
179 int RESAMPLER_SincConvOneToTwoInt16::InSamplesNeeded(int outSamples) |
|
180 { |
|
181 int inSamples( (outSamples >> 1) + (outSamples & 1) * (1 - m_state) ); |
|
182 |
|
183 return inSamples; |
|
184 } |
|
185 |
|
186 |
|
187 int RESAMPLER_SincConvOneToTwoInt16::ProcessToOutput(int16 *outputBuffers[], |
|
188 int16 *inputBuffers[], |
|
189 int outSamples) |
|
190 { |
|
191 static const int FILTER_LENGTH = 2 * LC_ZERO_CROSSINGS; |
|
192 |
|
193 int i, j, k; |
|
194 int oldState = m_state; |
|
195 int newState = (oldState + outSamples) & 1; |
|
196 |
|
197 for (i = 0; i < m_channelCount; i++) |
|
198 { |
|
199 if (!m_channelEnabled[i]) |
|
200 { |
|
201 break; |
|
202 } |
|
203 |
|
204 int16 *tempBuf = m_scratchBuffer; |
|
205 const int16 *inBuf = inputBuffers[i]; |
|
206 int16 *outBuf = outputBuffers[i]; |
|
207 |
|
208 memcpy(m_scratchBuffer, m_memBuffers[i], FILTER_LENGTH * sizeof(int16)); |
|
209 |
|
210 // Read samples into the memory buffer and |
|
211 // copy samples into every second index of output buffer. |
|
212 for (j = 0, k = oldState; k < outSamples; j++, k+=2) |
|
213 { |
|
214 int16 inputSample(inBuf[j]); |
|
215 tempBuf[j + FILTER_LENGTH - 1 + oldState] = inputSample; |
|
216 outBuf[k] = tempBuf[j + LC_ZERO_CROSSINGS - 1 + oldState]; |
|
217 } |
|
218 int inSamples = j; |
|
219 |
|
220 // Do band-limited interpolation and set the result into |
|
221 // every second index in the output buffer. |
|
222 #ifdef RESAMPLER_SINC_CONV_ONE_TO_TWO_DUAL_FILTERING |
|
223 for (j = 0, k = 1 - oldState; k < outSamples; j+=2, k+=4) |
|
224 { |
|
225 int newSample1(0); |
|
226 int newSample2(0); |
|
227 |
|
228 RESAMPLER_SincConvOneToTwoFilterDualInt16(newSample1, |
|
229 newSample2, |
|
230 tempBuf + LC_ZERO_CROSSINGS + j, |
|
231 m_filter, |
|
232 LC_ZERO_CROSSINGS); |
|
233 // Round, shift down, and clip to 16 bits |
|
234 outBuf[k] = (int16)RESAMPLER_Clip16((newSample1 + 16384) >> 15); |
|
235 if (k + 2 < outSamples) |
|
236 { |
|
237 outBuf[k+2] = (int16)RESAMPLER_Clip16((newSample2 + 16384) >> 15); |
|
238 } |
|
239 } |
|
240 #else |
|
241 for (j = 0, k = 1 - oldState; k < outSamples; j++, k+=2) |
|
242 { |
|
243 int32 newSample = |
|
244 RESAMPLER_SincConvOneToTwoFilterInt16(tempBuf + LC_ZERO_CROSSINGS + j, |
|
245 m_filter, |
|
246 LC_ZERO_CROSSINGS); |
|
247 // Round, shift down, and clip to 16 bits |
|
248 outBuf[k] = (int16)RESAMPLER_Clip16((newSample + 16384) >> 15); |
|
249 } |
|
250 #endif |
|
251 |
|
252 // Copy the newest samples to the beginning of the buffer |
|
253 memcpy(m_memBuffers[i], |
|
254 tempBuf + inSamples + oldState - newState, |
|
255 (FILTER_LENGTH - 1 + newState) * sizeof(int16)); |
|
256 } |
|
257 |
|
258 // Update state according to even or odd amount of output samples. |
|
259 m_state = newState; |
|
260 |
|
261 return outSamples; |
|
262 } |