blob: aa3a4bac2aa2bc23447ce28f57662cd96b3ed725 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
niklase@google.com470e71d2011-07-07 08:21:25 +000011/*
12 * A wrapper for resampling a numerous amount of sampling combinations.
13 */
14
15#include <stdlib.h>
16#include <string.h>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/resampler/include/resampler.h"
19#include "common_audio/signal_processing/include/signal_processing_library.h"
Sam Zackrisson9da7c742017-10-30 10:05:10 +010020#include "rtc_base/logging.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24Resampler::Resampler()
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070025 : state1_(nullptr),
26 state2_(nullptr),
27 state3_(nullptr),
28 in_buffer_(nullptr),
29 out_buffer_(nullptr),
30 in_buffer_size_(0),
31 out_buffer_size_(0),
32 in_buffer_size_max_(0),
33 out_buffer_size_max_(0),
34 my_in_frequency_khz_(0),
35 my_out_frequency_khz_(0),
36 my_mode_(kResamplerMode1To1),
37 num_channels_(0),
38 slave_left_(nullptr),
Yves Gerey665174f2018-06-19 15:03:05 +020039 slave_right_(nullptr) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000040
Peter Kasting69558702016-01-12 16:26:35 -080041Resampler::Resampler(int inFreq, int outFreq, size_t num_channels)
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070042 : Resampler() {
43 Reset(inFreq, outFreq, num_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
oprypin67fdb802017-03-09 06:25:06 -080046Resampler::~Resampler() {
47 if (state1_) {
48 free(state1_);
49 }
50 if (state2_) {
51 free(state2_);
52 }
53 if (state3_) {
54 free(state3_);
55 }
56 if (in_buffer_) {
57 free(in_buffer_);
58 }
59 if (out_buffer_) {
60 free(out_buffer_);
61 }
62 if (slave_left_) {
63 delete slave_left_;
64 }
65 if (slave_right_) {
66 delete slave_right_;
67 }
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
oprypin67fdb802017-03-09 06:25:06 -080070int Resampler::ResetIfNeeded(int inFreq, int outFreq, size_t num_channels) {
71 int tmpInFreq_kHz = inFreq / 1000;
72 int tmpOutFreq_kHz = outFreq / 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +000073
Yves Gerey665174f2018-06-19 15:03:05 +020074 if ((tmpInFreq_kHz != my_in_frequency_khz_) ||
75 (tmpOutFreq_kHz != my_out_frequency_khz_) ||
76 (num_channels != num_channels_)) {
oprypin67fdb802017-03-09 06:25:06 -080077 return Reset(inFreq, outFreq, num_channels);
78 } else {
79 return 0;
80 }
niklase@google.com470e71d2011-07-07 08:21:25 +000081}
82
oprypin67fdb802017-03-09 06:25:06 -080083int Resampler::Reset(int inFreq, int outFreq, size_t num_channels) {
84 if (num_channels != 1 && num_channels != 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010085 RTC_LOG(LS_WARNING)
Sam Zackrisson9da7c742017-10-30 10:05:10 +010086 << "Reset() called with unsupported channel count, num_channels = "
87 << num_channels;
88 return -1;
oprypin67fdb802017-03-09 06:25:06 -080089 }
Sam Zackrisson9da7c742017-10-30 10:05:10 +010090 ResamplerMode mode;
91 if (ComputeResamplerMode(inFreq, outFreq, &mode) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010092 RTC_LOG(LS_WARNING)
93 << "Reset() called with unsupported sample rates, inFreq = " << inFreq
94 << ", outFreq = " << outFreq;
Sam Zackrisson9da7c742017-10-30 10:05:10 +010095 return -1;
96 }
97 // Reinitialize internal state for the frequencies and sample rates.
oprypin67fdb802017-03-09 06:25:06 -080098 num_channels_ = num_channels;
Sam Zackrisson9da7c742017-10-30 10:05:10 +010099 my_mode_ = mode;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
oprypin67fdb802017-03-09 06:25:06 -0800101 if (state1_) {
102 free(state1_);
103 state1_ = nullptr;
104 }
105 if (state2_) {
106 free(state2_);
107 state2_ = nullptr;
108 }
109 if (state3_) {
110 free(state3_);
111 state3_ = nullptr;
112 }
113 if (in_buffer_) {
114 free(in_buffer_);
115 in_buffer_ = nullptr;
116 }
117 if (out_buffer_) {
118 free(out_buffer_);
119 out_buffer_ = nullptr;
120 }
121 if (slave_left_) {
122 delete slave_left_;
123 slave_left_ = nullptr;
124 }
125 if (slave_right_) {
126 delete slave_right_;
127 slave_right_ = nullptr;
128 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
oprypin67fdb802017-03-09 06:25:06 -0800130 in_buffer_size_ = 0;
131 out_buffer_size_ = 0;
132 in_buffer_size_max_ = 0;
133 out_buffer_size_max_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
oprypin67fdb802017-03-09 06:25:06 -0800135 // We need to track what domain we're in.
136 my_in_frequency_khz_ = inFreq / 1000;
137 my_out_frequency_khz_ = outFreq / 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
oprypin67fdb802017-03-09 06:25:06 -0800139 if (num_channels_ == 2) {
140 // Create two mono resamplers.
141 slave_left_ = new Resampler(inFreq, outFreq, 1);
142 slave_right_ = new Resampler(inFreq, outFreq, 1);
143 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
Sam Zackrisson9da7c742017-10-30 10:05:10 +0100145 // Now create the states we need.
oprypin67fdb802017-03-09 06:25:06 -0800146 switch (my_mode_) {
147 case kResamplerMode1To1:
148 // No state needed;
149 break;
150 case kResamplerMode1To2:
151 state1_ = malloc(8 * sizeof(int32_t));
152 memset(state1_, 0, 8 * sizeof(int32_t));
153 break;
154 case kResamplerMode1To3:
155 state1_ = malloc(sizeof(WebRtcSpl_State16khzTo48khz));
156 WebRtcSpl_ResetResample16khzTo48khz(
157 static_cast<WebRtcSpl_State16khzTo48khz*>(state1_));
158 break;
159 case kResamplerMode1To4:
160 // 1:2
161 state1_ = malloc(8 * sizeof(int32_t));
162 memset(state1_, 0, 8 * sizeof(int32_t));
163 // 2:4
164 state2_ = malloc(8 * sizeof(int32_t));
165 memset(state2_, 0, 8 * sizeof(int32_t));
166 break;
167 case kResamplerMode1To6:
168 // 1:2
169 state1_ = malloc(8 * sizeof(int32_t));
170 memset(state1_, 0, 8 * sizeof(int32_t));
171 // 2:6
172 state2_ = malloc(sizeof(WebRtcSpl_State16khzTo48khz));
173 WebRtcSpl_ResetResample16khzTo48khz(
174 static_cast<WebRtcSpl_State16khzTo48khz*>(state2_));
175 break;
176 case kResamplerMode1To12:
177 // 1:2
178 state1_ = malloc(8 * sizeof(int32_t));
179 memset(state1_, 0, 8 * sizeof(int32_t));
180 // 2:4
181 state2_ = malloc(8 * sizeof(int32_t));
182 memset(state2_, 0, 8 * sizeof(int32_t));
183 // 4:12
184 state3_ = malloc(sizeof(WebRtcSpl_State16khzTo48khz));
185 WebRtcSpl_ResetResample16khzTo48khz(
186 static_cast<WebRtcSpl_State16khzTo48khz*>(state3_));
187 break;
188 case kResamplerMode2To3:
189 // 2:6
190 state1_ = malloc(sizeof(WebRtcSpl_State16khzTo48khz));
191 WebRtcSpl_ResetResample16khzTo48khz(
Yves Gerey665174f2018-06-19 15:03:05 +0200192 static_cast<WebRtcSpl_State16khzTo48khz*>(state1_));
oprypin67fdb802017-03-09 06:25:06 -0800193 // 6:3
194 state2_ = malloc(8 * sizeof(int32_t));
195 memset(state2_, 0, 8 * sizeof(int32_t));
196 break;
197 case kResamplerMode2To11:
198 state1_ = malloc(8 * sizeof(int32_t));
199 memset(state1_, 0, 8 * sizeof(int32_t));
200
201 state2_ = malloc(sizeof(WebRtcSpl_State8khzTo22khz));
202 WebRtcSpl_ResetResample8khzTo22khz(
203 static_cast<WebRtcSpl_State8khzTo22khz*>(state2_));
204 break;
205 case kResamplerMode4To11:
206 state1_ = malloc(sizeof(WebRtcSpl_State8khzTo22khz));
207 WebRtcSpl_ResetResample8khzTo22khz(
208 static_cast<WebRtcSpl_State8khzTo22khz*>(state1_));
209 break;
210 case kResamplerMode8To11:
211 state1_ = malloc(sizeof(WebRtcSpl_State16khzTo22khz));
212 WebRtcSpl_ResetResample16khzTo22khz(
213 static_cast<WebRtcSpl_State16khzTo22khz*>(state1_));
214 break;
215 case kResamplerMode11To16:
216 state1_ = malloc(8 * sizeof(int32_t));
217 memset(state1_, 0, 8 * sizeof(int32_t));
218
219 state2_ = malloc(sizeof(WebRtcSpl_State22khzTo16khz));
220 WebRtcSpl_ResetResample22khzTo16khz(
221 static_cast<WebRtcSpl_State22khzTo16khz*>(state2_));
222 break;
223 case kResamplerMode11To32:
224 // 11 -> 22
225 state1_ = malloc(8 * sizeof(int32_t));
226 memset(state1_, 0, 8 * sizeof(int32_t));
227
228 // 22 -> 16
229 state2_ = malloc(sizeof(WebRtcSpl_State22khzTo16khz));
230 WebRtcSpl_ResetResample22khzTo16khz(
231 static_cast<WebRtcSpl_State22khzTo16khz*>(state2_));
232
233 // 16 -> 32
234 state3_ = malloc(8 * sizeof(int32_t));
235 memset(state3_, 0, 8 * sizeof(int32_t));
236
237 break;
238 case kResamplerMode2To1:
239 state1_ = malloc(8 * sizeof(int32_t));
240 memset(state1_, 0, 8 * sizeof(int32_t));
241 break;
242 case kResamplerMode3To1:
243 state1_ = malloc(sizeof(WebRtcSpl_State48khzTo16khz));
244 WebRtcSpl_ResetResample48khzTo16khz(
245 static_cast<WebRtcSpl_State48khzTo16khz*>(state1_));
246 break;
247 case kResamplerMode4To1:
248 // 4:2
249 state1_ = malloc(8 * sizeof(int32_t));
250 memset(state1_, 0, 8 * sizeof(int32_t));
251 // 2:1
252 state2_ = malloc(8 * sizeof(int32_t));
253 memset(state2_, 0, 8 * sizeof(int32_t));
254 break;
255 case kResamplerMode6To1:
256 // 6:2
257 state1_ = malloc(sizeof(WebRtcSpl_State48khzTo16khz));
258 WebRtcSpl_ResetResample48khzTo16khz(
259 static_cast<WebRtcSpl_State48khzTo16khz*>(state1_));
260 // 2:1
261 state2_ = malloc(8 * sizeof(int32_t));
262 memset(state2_, 0, 8 * sizeof(int32_t));
263 break;
264 case kResamplerMode12To1:
265 // 12:4
266 state1_ = malloc(sizeof(WebRtcSpl_State48khzTo16khz));
267 WebRtcSpl_ResetResample48khzTo16khz(
268 static_cast<WebRtcSpl_State48khzTo16khz*>(state1_));
269 // 4:2
270 state2_ = malloc(8 * sizeof(int32_t));
271 memset(state2_, 0, 8 * sizeof(int32_t));
272 // 2:1
273 state3_ = malloc(8 * sizeof(int32_t));
274 memset(state3_, 0, 8 * sizeof(int32_t));
275 break;
276 case kResamplerMode3To2:
277 // 3:6
278 state1_ = malloc(8 * sizeof(int32_t));
279 memset(state1_, 0, 8 * sizeof(int32_t));
280 // 6:2
281 state2_ = malloc(sizeof(WebRtcSpl_State48khzTo16khz));
282 WebRtcSpl_ResetResample48khzTo16khz(
283 static_cast<WebRtcSpl_State48khzTo16khz*>(state2_));
284 break;
285 case kResamplerMode11To2:
286 state1_ = malloc(sizeof(WebRtcSpl_State22khzTo8khz));
287 WebRtcSpl_ResetResample22khzTo8khz(
288 static_cast<WebRtcSpl_State22khzTo8khz*>(state1_));
289
290 state2_ = malloc(8 * sizeof(int32_t));
291 memset(state2_, 0, 8 * sizeof(int32_t));
292
293 break;
294 case kResamplerMode11To4:
295 state1_ = malloc(sizeof(WebRtcSpl_State22khzTo8khz));
296 WebRtcSpl_ResetResample22khzTo8khz(
297 static_cast<WebRtcSpl_State22khzTo8khz*>(state1_));
298 break;
299 case kResamplerMode11To8:
300 state1_ = malloc(sizeof(WebRtcSpl_State22khzTo16khz));
301 WebRtcSpl_ResetResample22khzTo16khz(
302 static_cast<WebRtcSpl_State22khzTo16khz*>(state1_));
303 break;
304 }
305
306 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000307}
308
Sam Zackrisson9da7c742017-10-30 10:05:10 +0100309int Resampler::ComputeResamplerMode(int in_freq_hz,
310 int out_freq_hz,
311 ResamplerMode* mode) {
312 // Start with a math exercise, Euclid's algorithm to find the gcd:
313 int a = in_freq_hz;
314 int b = out_freq_hz;
315 int c = a % b;
316 while (c != 0) {
317 a = b;
318 b = c;
319 c = a % b;
320 }
321 // b is now the gcd;
322
323 // Scale with GCD
324 const int reduced_in_freq = in_freq_hz / b;
325 const int reduced_out_freq = out_freq_hz / b;
326
327 if (reduced_in_freq == reduced_out_freq) {
328 *mode = kResamplerMode1To1;
329 } else if (reduced_in_freq == 1) {
330 switch (reduced_out_freq) {
331 case 2:
332 *mode = kResamplerMode1To2;
333 break;
334 case 3:
335 *mode = kResamplerMode1To3;
336 break;
337 case 4:
338 *mode = kResamplerMode1To4;
339 break;
340 case 6:
341 *mode = kResamplerMode1To6;
342 break;
343 case 12:
344 *mode = kResamplerMode1To12;
345 break;
346 default:
347 return -1;
348 }
349 } else if (reduced_out_freq == 1) {
350 switch (reduced_in_freq) {
351 case 2:
352 *mode = kResamplerMode2To1;
353 break;
354 case 3:
355 *mode = kResamplerMode3To1;
356 break;
357 case 4:
358 *mode = kResamplerMode4To1;
359 break;
360 case 6:
361 *mode = kResamplerMode6To1;
362 break;
363 case 12:
364 *mode = kResamplerMode12To1;
365 break;
366 default:
367 return -1;
368 }
369 } else if ((reduced_in_freq == 2) && (reduced_out_freq == 3)) {
370 *mode = kResamplerMode2To3;
371 } else if ((reduced_in_freq == 2) && (reduced_out_freq == 11)) {
372 *mode = kResamplerMode2To11;
373 } else if ((reduced_in_freq == 4) && (reduced_out_freq == 11)) {
374 *mode = kResamplerMode4To11;
375 } else if ((reduced_in_freq == 8) && (reduced_out_freq == 11)) {
376 *mode = kResamplerMode8To11;
377 } else if ((reduced_in_freq == 3) && (reduced_out_freq == 2)) {
378 *mode = kResamplerMode3To2;
379 } else if ((reduced_in_freq == 11) && (reduced_out_freq == 2)) {
380 *mode = kResamplerMode11To2;
381 } else if ((reduced_in_freq == 11) && (reduced_out_freq == 4)) {
382 *mode = kResamplerMode11To4;
383 } else if ((reduced_in_freq == 11) && (reduced_out_freq == 16)) {
384 *mode = kResamplerMode11To16;
385 } else if ((reduced_in_freq == 11) && (reduced_out_freq == 32)) {
386 *mode = kResamplerMode11To32;
387 } else if ((reduced_in_freq == 11) && (reduced_out_freq == 8)) {
388 *mode = kResamplerMode11To8;
389 } else {
390 return -1;
391 }
392 return 0;
393}
394
niklase@google.com470e71d2011-07-07 08:21:25 +0000395// Synchronous resampling, all output samples are written to samplesOut
Yves Gerey665174f2018-06-19 15:03:05 +0200396int Resampler::Push(const int16_t* samplesIn,
397 size_t lengthIn,
398 int16_t* samplesOut,
399 size_t maxLen,
400 size_t& outLen) {
oprypin67fdb802017-03-09 06:25:06 -0800401 if (num_channels_ == 2) {
402 // Split up the signal and call the slave object for each channel
403 int16_t* left =
404 static_cast<int16_t*>(malloc(lengthIn * sizeof(int16_t) / 2));
405 int16_t* right =
406 static_cast<int16_t*>(malloc(lengthIn * sizeof(int16_t) / 2));
407 int16_t* out_left =
408 static_cast<int16_t*>(malloc(maxLen / 2 * sizeof(int16_t)));
409 int16_t* out_right =
410 static_cast<int16_t*>(malloc(maxLen / 2 * sizeof(int16_t)));
411 int res = 0;
412 for (size_t i = 0; i < lengthIn; i += 2) {
413 left[i >> 1] = samplesIn[i];
414 right[i >> 1] = samplesIn[i + 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000415 }
416
oprypin67fdb802017-03-09 06:25:06 -0800417 // It's OK to overwrite the local parameter, since it's just a copy
418 lengthIn = lengthIn / 2;
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
oprypin67fdb802017-03-09 06:25:06 -0800420 size_t actualOutLen_left = 0;
421 size_t actualOutLen_right = 0;
422 // Do resampling for right channel
423 res |= slave_left_->Push(left, lengthIn, out_left, maxLen / 2,
424 actualOutLen_left);
425 res |= slave_right_->Push(right, lengthIn, out_right, maxLen / 2,
426 actualOutLen_right);
427 if (res || (actualOutLen_left != actualOutLen_right)) {
428 free(left);
429 free(right);
430 free(out_left);
431 free(out_right);
432 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000433 }
oprypin67fdb802017-03-09 06:25:06 -0800434
435 // Reassemble the signal
436 for (size_t i = 0; i < actualOutLen_left; i++) {
437 samplesOut[i * 2] = out_left[i];
438 samplesOut[i * 2 + 1] = out_right[i];
439 }
440 outLen = 2 * actualOutLen_left;
441
442 free(left);
443 free(right);
444 free(out_left);
445 free(out_right);
446
niklase@google.com470e71d2011-07-07 08:21:25 +0000447 return 0;
oprypin67fdb802017-03-09 06:25:06 -0800448 }
449
450 // Containers for temp samples
451 int16_t* tmp;
452 int16_t* tmp_2;
453 // tmp data for resampling routines
454 int32_t* tmp_mem;
455
456 switch (my_mode_) {
457 case kResamplerMode1To1:
458 memcpy(samplesOut, samplesIn, lengthIn * sizeof(int16_t));
459 outLen = lengthIn;
460 break;
461 case kResamplerMode1To2:
462 if (maxLen < (lengthIn * 2)) {
463 return -1;
464 }
465 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, samplesOut,
466 static_cast<int32_t*>(state1_));
467 outLen = lengthIn * 2;
468 return 0;
469 case kResamplerMode1To3:
470
471 // We can only handle blocks of 160 samples
472 // Can be fixed, but I don't think it's needed
473 if ((lengthIn % 160) != 0) {
474 return -1;
475 }
476 if (maxLen < (lengthIn * 3)) {
477 return -1;
478 }
479 tmp_mem = static_cast<int32_t*>(malloc(336 * sizeof(int32_t)));
480
481 for (size_t i = 0; i < lengthIn; i += 160) {
482 WebRtcSpl_Resample16khzTo48khz(
483 samplesIn + i, samplesOut + i * 3,
484 static_cast<WebRtcSpl_State16khzTo48khz*>(state1_), tmp_mem);
485 }
486 outLen = lengthIn * 3;
487 free(tmp_mem);
488 return 0;
489 case kResamplerMode1To4:
490 if (maxLen < (lengthIn * 4)) {
491 return -1;
492 }
493
494 tmp = static_cast<int16_t*>(malloc(sizeof(int16_t) * 2 * lengthIn));
495 // 1:2
496 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, tmp,
497 static_cast<int32_t*>(state1_));
498 // 2:4
499 WebRtcSpl_UpsampleBy2(tmp, lengthIn * 2, samplesOut,
500 static_cast<int32_t*>(state2_));
501 outLen = lengthIn * 4;
502 free(tmp);
503 return 0;
504 case kResamplerMode1To6:
505 // We can only handle blocks of 80 samples
506 // Can be fixed, but I don't think it's needed
507 if ((lengthIn % 80) != 0) {
508 return -1;
509 }
510 if (maxLen < (lengthIn * 6)) {
511 return -1;
512 }
513
514 // 1:2
515
516 tmp_mem = static_cast<int32_t*>(malloc(336 * sizeof(int32_t)));
517 tmp = static_cast<int16_t*>(malloc(sizeof(int16_t) * 2 * lengthIn));
518
519 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, tmp,
520 static_cast<int32_t*>(state1_));
521 outLen = lengthIn * 2;
522
523 for (size_t i = 0; i < outLen; i += 160) {
524 WebRtcSpl_Resample16khzTo48khz(
525 tmp + i, samplesOut + i * 3,
526 static_cast<WebRtcSpl_State16khzTo48khz*>(state2_), tmp_mem);
527 }
528 outLen = outLen * 3;
529 free(tmp_mem);
530 free(tmp);
531
532 return 0;
533 case kResamplerMode1To12:
534 // We can only handle blocks of 40 samples
535 // Can be fixed, but I don't think it's needed
536 if ((lengthIn % 40) != 0) {
537 return -1;
538 }
539 if (maxLen < (lengthIn * 12)) {
540 return -1;
541 }
542
543 tmp_mem = static_cast<int32_t*>(malloc(336 * sizeof(int32_t)));
544 tmp = static_cast<int16_t*>(malloc(sizeof(int16_t) * 4 * lengthIn));
545 // 1:2
546 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, samplesOut,
547 static_cast<int32_t*>(state1_));
548 outLen = lengthIn * 2;
549 // 2:4
550 WebRtcSpl_UpsampleBy2(samplesOut, outLen, tmp,
551 static_cast<int32_t*>(state2_));
552 outLen = outLen * 2;
553 // 4:12
554 for (size_t i = 0; i < outLen; i += 160) {
555 // WebRtcSpl_Resample16khzTo48khz() takes a block of 160 samples
556 // as input and outputs a resampled block of 480 samples. The
557 // data is now actually in 32 kHz sampling rate, despite the
558 // function name, and with a resampling factor of three becomes
559 // 96 kHz.
560 WebRtcSpl_Resample16khzTo48khz(
561 tmp + i, samplesOut + i * 3,
562 static_cast<WebRtcSpl_State16khzTo48khz*>(state3_), tmp_mem);
563 }
564 outLen = outLen * 3;
565 free(tmp_mem);
566 free(tmp);
567
568 return 0;
569 case kResamplerMode2To3:
570 if (maxLen < (lengthIn * 3 / 2)) {
571 return -1;
572 }
573 // 2:6
574 // We can only handle blocks of 160 samples
575 // Can be fixed, but I don't think it's needed
576 if ((lengthIn % 160) != 0) {
577 return -1;
578 }
Yves Gerey665174f2018-06-19 15:03:05 +0200579 tmp = static_cast<int16_t*>(malloc(sizeof(int16_t) * lengthIn * 3));
oprypin67fdb802017-03-09 06:25:06 -0800580 tmp_mem = static_cast<int32_t*>(malloc(336 * sizeof(int32_t)));
581 for (size_t i = 0; i < lengthIn; i += 160) {
582 WebRtcSpl_Resample16khzTo48khz(
583 samplesIn + i, tmp + i * 3,
584 static_cast<WebRtcSpl_State16khzTo48khz*>(state1_), tmp_mem);
585 }
586 lengthIn = lengthIn * 3;
587 // 6:3
588 WebRtcSpl_DownsampleBy2(tmp, lengthIn, samplesOut,
589 static_cast<int32_t*>(state2_));
590 outLen = lengthIn / 2;
591 free(tmp);
592 free(tmp_mem);
593 return 0;
594 case kResamplerMode2To11:
595
596 // We can only handle blocks of 80 samples
597 // Can be fixed, but I don't think it's needed
598 if ((lengthIn % 80) != 0) {
599 return -1;
600 }
601 if (maxLen < ((lengthIn * 11) / 2)) {
602 return -1;
603 }
604 tmp = static_cast<int16_t*>(malloc(sizeof(int16_t) * 2 * lengthIn));
605 // 1:2
606 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, tmp,
607 static_cast<int32_t*>(state1_));
608 lengthIn *= 2;
609
610 tmp_mem = static_cast<int32_t*>(malloc(98 * sizeof(int32_t)));
611
612 for (size_t i = 0; i < lengthIn; i += 80) {
613 WebRtcSpl_Resample8khzTo22khz(
614 tmp + i, samplesOut + (i * 11) / 4,
615 static_cast<WebRtcSpl_State8khzTo22khz*>(state2_), tmp_mem);
616 }
617 outLen = (lengthIn * 11) / 4;
618 free(tmp_mem);
619 free(tmp);
620 return 0;
621 case kResamplerMode4To11:
622
623 // We can only handle blocks of 80 samples
624 // Can be fixed, but I don't think it's needed
625 if ((lengthIn % 80) != 0) {
626 return -1;
627 }
628 if (maxLen < ((lengthIn * 11) / 4)) {
629 return -1;
630 }
631 tmp_mem = static_cast<int32_t*>(malloc(98 * sizeof(int32_t)));
632
633 for (size_t i = 0; i < lengthIn; i += 80) {
634 WebRtcSpl_Resample8khzTo22khz(
635 samplesIn + i, samplesOut + (i * 11) / 4,
636 static_cast<WebRtcSpl_State8khzTo22khz*>(state1_), tmp_mem);
637 }
638 outLen = (lengthIn * 11) / 4;
639 free(tmp_mem);
640 return 0;
641 case kResamplerMode8To11:
642 // We can only handle blocks of 160 samples
643 // Can be fixed, but I don't think it's needed
644 if ((lengthIn % 160) != 0) {
645 return -1;
646 }
647 if (maxLen < ((lengthIn * 11) / 8)) {
648 return -1;
649 }
650 tmp_mem = static_cast<int32_t*>(malloc(88 * sizeof(int32_t)));
651
652 for (size_t i = 0; i < lengthIn; i += 160) {
653 WebRtcSpl_Resample16khzTo22khz(
654 samplesIn + i, samplesOut + (i * 11) / 8,
655 static_cast<WebRtcSpl_State16khzTo22khz*>(state1_), tmp_mem);
656 }
657 outLen = (lengthIn * 11) / 8;
658 free(tmp_mem);
659 return 0;
660
661 case kResamplerMode11To16:
662 // We can only handle blocks of 110 samples
663 if ((lengthIn % 110) != 0) {
664 return -1;
665 }
666 if (maxLen < ((lengthIn * 16) / 11)) {
667 return -1;
668 }
669
670 tmp_mem = static_cast<int32_t*>(malloc(104 * sizeof(int32_t)));
671 tmp = static_cast<int16_t*>(malloc((sizeof(int16_t) * lengthIn * 2)));
672
673 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, tmp,
674 static_cast<int32_t*>(state1_));
675
676 for (size_t i = 0; i < (lengthIn * 2); i += 220) {
677 WebRtcSpl_Resample22khzTo16khz(
678 tmp + i, samplesOut + (i / 220) * 160,
679 static_cast<WebRtcSpl_State22khzTo16khz*>(state2_), tmp_mem);
680 }
681
682 outLen = (lengthIn * 16) / 11;
683
684 free(tmp_mem);
685 free(tmp);
686 return 0;
687
688 case kResamplerMode11To32:
689
690 // We can only handle blocks of 110 samples
691 if ((lengthIn % 110) != 0) {
692 return -1;
693 }
694 if (maxLen < ((lengthIn * 32) / 11)) {
695 return -1;
696 }
697
698 tmp_mem = static_cast<int32_t*>(malloc(104 * sizeof(int32_t)));
699 tmp = static_cast<int16_t*>(malloc((sizeof(int16_t) * lengthIn * 2)));
700
701 // 11 -> 22 kHz in samplesOut
702 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, samplesOut,
703 static_cast<int32_t*>(state1_));
704
705 // 22 -> 16 in tmp
706 for (size_t i = 0; i < (lengthIn * 2); i += 220) {
707 WebRtcSpl_Resample22khzTo16khz(
708 samplesOut + i, tmp + (i / 220) * 160,
709 static_cast<WebRtcSpl_State22khzTo16khz*>(state2_), tmp_mem);
710 }
711
712 // 16 -> 32 in samplesOut
713 WebRtcSpl_UpsampleBy2(tmp, (lengthIn * 16) / 11, samplesOut,
714 static_cast<int32_t*>(state3_));
715
716 outLen = (lengthIn * 32) / 11;
717
718 free(tmp_mem);
719 free(tmp);
720 return 0;
721
722 case kResamplerMode2To1:
723 if (maxLen < (lengthIn / 2)) {
724 return -1;
725 }
726 WebRtcSpl_DownsampleBy2(samplesIn, lengthIn, samplesOut,
727 static_cast<int32_t*>(state1_));
728 outLen = lengthIn / 2;
729 return 0;
730 case kResamplerMode3To1:
731 // We can only handle blocks of 480 samples
732 // Can be fixed, but I don't think it's needed
733 if ((lengthIn % 480) != 0) {
734 return -1;
735 }
736 if (maxLen < (lengthIn / 3)) {
737 return -1;
738 }
739 tmp_mem = static_cast<int32_t*>(malloc(496 * sizeof(int32_t)));
740
741 for (size_t i = 0; i < lengthIn; i += 480) {
742 WebRtcSpl_Resample48khzTo16khz(
743 samplesIn + i, samplesOut + i / 3,
744 static_cast<WebRtcSpl_State48khzTo16khz*>(state1_), tmp_mem);
745 }
746 outLen = lengthIn / 3;
747 free(tmp_mem);
748 return 0;
749 case kResamplerMode4To1:
750 if (maxLen < (lengthIn / 4)) {
751 return -1;
752 }
753 tmp = static_cast<int16_t*>(malloc(sizeof(int16_t) * lengthIn / 2));
754 // 4:2
755 WebRtcSpl_DownsampleBy2(samplesIn, lengthIn, tmp,
756 static_cast<int32_t*>(state1_));
757 // 2:1
758 WebRtcSpl_DownsampleBy2(tmp, lengthIn / 2, samplesOut,
759 static_cast<int32_t*>(state2_));
760 outLen = lengthIn / 4;
761 free(tmp);
762 return 0;
763
764 case kResamplerMode6To1:
765 // We can only handle blocks of 480 samples
766 // Can be fixed, but I don't think it's needed
767 if ((lengthIn % 480) != 0) {
768 return -1;
769 }
770 if (maxLen < (lengthIn / 6)) {
771 return -1;
772 }
773
774 tmp_mem = static_cast<int32_t*>(malloc(496 * sizeof(int32_t)));
775 tmp = static_cast<int16_t*>(malloc((sizeof(int16_t) * lengthIn) / 3));
776
777 for (size_t i = 0; i < lengthIn; i += 480) {
778 WebRtcSpl_Resample48khzTo16khz(
779 samplesIn + i, tmp + i / 3,
780 static_cast<WebRtcSpl_State48khzTo16khz*>(state1_), tmp_mem);
781 }
782 outLen = lengthIn / 3;
783 free(tmp_mem);
784 WebRtcSpl_DownsampleBy2(tmp, outLen, samplesOut,
785 static_cast<int32_t*>(state2_));
786 free(tmp);
787 outLen = outLen / 2;
788 return 0;
789 case kResamplerMode12To1:
790 // We can only handle blocks of 480 samples
791 // Can be fixed, but I don't think it's needed
792 if ((lengthIn % 480) != 0) {
793 return -1;
794 }
795 if (maxLen < (lengthIn / 12)) {
796 return -1;
797 }
798
799 tmp_mem = static_cast<int32_t*>(malloc(496 * sizeof(int32_t)));
800 tmp = static_cast<int16_t*>(malloc((sizeof(int16_t) * lengthIn) / 3));
801 tmp_2 = static_cast<int16_t*>(malloc((sizeof(int16_t) * lengthIn) / 6));
802 // 12:4
803 for (size_t i = 0; i < lengthIn; i += 480) {
804 // WebRtcSpl_Resample48khzTo16khz() takes a block of 480 samples
805 // as input and outputs a resampled block of 160 samples. The
806 // data is now actually in 96 kHz sampling rate, despite the
807 // function name, and with a resampling factor of 1/3 becomes
808 // 32 kHz.
809 WebRtcSpl_Resample48khzTo16khz(
810 samplesIn + i, tmp + i / 3,
811 static_cast<WebRtcSpl_State48khzTo16khz*>(state1_), tmp_mem);
812 }
813 outLen = lengthIn / 3;
814 free(tmp_mem);
815 // 4:2
816 WebRtcSpl_DownsampleBy2(tmp, outLen, tmp_2,
817 static_cast<int32_t*>(state2_));
818 outLen = outLen / 2;
819 free(tmp);
820 // 2:1
821 WebRtcSpl_DownsampleBy2(tmp_2, outLen, samplesOut,
822 static_cast<int32_t*>(state3_));
823 free(tmp_2);
824 outLen = outLen / 2;
825 return 0;
826 case kResamplerMode3To2:
827 if (maxLen < (lengthIn * 2 / 3)) {
828 return -1;
829 }
830 // 3:6
Yves Gerey665174f2018-06-19 15:03:05 +0200831 tmp = static_cast<int16_t*>(malloc(sizeof(int16_t) * lengthIn * 2));
oprypin67fdb802017-03-09 06:25:06 -0800832 WebRtcSpl_UpsampleBy2(samplesIn, lengthIn, tmp,
833 static_cast<int32_t*>(state1_));
834 lengthIn *= 2;
835 // 6:2
836 // We can only handle blocks of 480 samples
837 // Can be fixed, but I don't think it's needed
838 if ((lengthIn % 480) != 0) {
839 free(tmp);
840 return -1;
841 }
842 tmp_mem = static_cast<int32_t*>(malloc(496 * sizeof(int32_t)));
843 for (size_t i = 0; i < lengthIn; i += 480) {
844 WebRtcSpl_Resample48khzTo16khz(
845 tmp + i, samplesOut + i / 3,
846 static_cast<WebRtcSpl_State48khzTo16khz*>(state2_), tmp_mem);
847 }
848 outLen = lengthIn / 3;
849 free(tmp);
850 free(tmp_mem);
851 return 0;
852 case kResamplerMode11To2:
853 // We can only handle blocks of 220 samples
854 // Can be fixed, but I don't think it's needed
855 if ((lengthIn % 220) != 0) {
856 return -1;
857 }
858 if (maxLen < ((lengthIn * 2) / 11)) {
859 return -1;
860 }
861 tmp_mem = static_cast<int32_t*>(malloc(126 * sizeof(int32_t)));
Yves Gerey665174f2018-06-19 15:03:05 +0200862 tmp =
863 static_cast<int16_t*>(malloc((lengthIn * 4) / 11 * sizeof(int16_t)));
oprypin67fdb802017-03-09 06:25:06 -0800864
865 for (size_t i = 0; i < lengthIn; i += 220) {
866 WebRtcSpl_Resample22khzTo8khz(
867 samplesIn + i, tmp + (i * 4) / 11,
868 static_cast<WebRtcSpl_State22khzTo8khz*>(state1_), tmp_mem);
869 }
870 lengthIn = (lengthIn * 4) / 11;
871
872 WebRtcSpl_DownsampleBy2(tmp, lengthIn, samplesOut,
873 static_cast<int32_t*>(state2_));
874 outLen = lengthIn / 2;
875
876 free(tmp_mem);
877 free(tmp);
878 return 0;
879 case kResamplerMode11To4:
880 // We can only handle blocks of 220 samples
881 // Can be fixed, but I don't think it's needed
882 if ((lengthIn % 220) != 0) {
883 return -1;
884 }
885 if (maxLen < ((lengthIn * 4) / 11)) {
886 return -1;
887 }
888 tmp_mem = static_cast<int32_t*>(malloc(126 * sizeof(int32_t)));
889
890 for (size_t i = 0; i < lengthIn; i += 220) {
891 WebRtcSpl_Resample22khzTo8khz(
892 samplesIn + i, samplesOut + (i * 4) / 11,
893 static_cast<WebRtcSpl_State22khzTo8khz*>(state1_), tmp_mem);
894 }
895 outLen = (lengthIn * 4) / 11;
896 free(tmp_mem);
897 return 0;
898 case kResamplerMode11To8:
899 // We can only handle blocks of 160 samples
900 // Can be fixed, but I don't think it's needed
901 if ((lengthIn % 220) != 0) {
902 return -1;
903 }
904 if (maxLen < ((lengthIn * 8) / 11)) {
905 return -1;
906 }
907 tmp_mem = static_cast<int32_t*>(malloc(104 * sizeof(int32_t)));
908
909 for (size_t i = 0; i < lengthIn; i += 220) {
910 WebRtcSpl_Resample22khzTo16khz(
911 samplesIn + i, samplesOut + (i * 8) / 11,
912 static_cast<WebRtcSpl_State22khzTo16khz*>(state1_), tmp_mem);
913 }
914 outLen = (lengthIn * 8) / 11;
915 free(tmp_mem);
916 return 0;
917 break;
918 }
919 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000920}
921
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000922} // namespace webrtc