andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | #include "webrtc/common_audio/resampler/include/push_resampler.h" |
| 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <string.h> |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 14 | |
| 15 | #include "webrtc/common_audio/include/audio_util.h" |
| 16 | #include "webrtc/common_audio/resampler/include/resampler.h" |
| 17 | #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 18 | #include "webrtc/rtc_base/checks.h" |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 21 | namespace { |
| 22 | // These checks were factored out into a non-templatized function |
| 23 | // due to problems with clang on Windows in debug builds. |
| 24 | // For some reason having the DCHECKs inline in the template code |
| 25 | // caused the compiler to generate code that threw off the linker. |
Tommi | 90edc65 | 2016-05-26 23:48:16 +0200 | [diff] [blame] | 26 | // TODO(tommi): Re-enable when we've figured out what the problem is. |
| 27 | // http://crbug.com/615050 |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 28 | void CheckValidInitParams(int src_sample_rate_hz, int dst_sample_rate_hz, |
| 29 | size_t num_channels) { |
Tommi | 90edc65 | 2016-05-26 23:48:16 +0200 | [diff] [blame] | 30 | // The below checks are temporarily disabled on WEBRTC_WIN due to problems |
| 31 | // with clang debug builds. |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 32 | #if !defined(WEBRTC_WIN) && defined(__clang__) |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 33 | RTC_DCHECK_GT(src_sample_rate_hz, 0); |
| 34 | RTC_DCHECK_GT(dst_sample_rate_hz, 0); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 35 | RTC_DCHECK_GT(num_channels, 0); |
| 36 | RTC_DCHECK_LE(num_channels, 2); |
Tommi | 0ad72ea | 2016-05-26 23:07:40 +0200 | [diff] [blame] | 37 | #endif |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Tommi | c47f009 | 2016-05-26 22:55:35 +0200 | [diff] [blame] | 40 | void CheckExpectedBufferSizes(size_t src_length, |
| 41 | size_t dst_capacity, |
| 42 | size_t num_channels, |
| 43 | int src_sample_rate, |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 44 | int dst_sample_rate) { |
Tommi | 90edc65 | 2016-05-26 23:48:16 +0200 | [diff] [blame] | 45 | // The below checks are temporarily disabled on WEBRTC_WIN due to problems |
| 46 | // with clang debug builds. |
| 47 | // TODO(tommi): Re-enable when we've figured out what the problem is. |
| 48 | // http://crbug.com/615050 |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 49 | #if !defined(WEBRTC_WIN) && defined(__clang__) |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 50 | const size_t src_size_10ms = src_sample_rate * num_channels / 100; |
| 51 | const size_t dst_size_10ms = dst_sample_rate * num_channels / 100; |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 52 | RTC_DCHECK_EQ(src_length, src_size_10ms); |
| 53 | RTC_DCHECK_GE(dst_capacity, dst_size_10ms); |
Tommi | 0ad72ea | 2016-05-26 23:07:40 +0200 | [diff] [blame] | 54 | #endif |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 55 | } |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 56 | } // namespace |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 57 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 58 | template <typename T> |
| 59 | PushResampler<T>::PushResampler() |
andrew@webrtc.org | 31628aa | 2013-10-22 12:50:00 +0000 | [diff] [blame] | 60 | : src_sample_rate_hz_(0), |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 61 | dst_sample_rate_hz_(0), |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 62 | num_channels_(0) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 63 | } |
| 64 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 65 | template <typename T> |
| 66 | PushResampler<T>::~PushResampler() { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 67 | } |
| 68 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 69 | template <typename T> |
| 70 | int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz, |
| 71 | int dst_sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 72 | size_t num_channels) { |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 73 | CheckValidInitParams(src_sample_rate_hz, dst_sample_rate_hz, num_channels); |
| 74 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 75 | if (src_sample_rate_hz == src_sample_rate_hz_ && |
| 76 | dst_sample_rate_hz == dst_sample_rate_hz_ && |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 77 | num_channels == num_channels_) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 78 | // No-op if settings haven't changed. |
| 79 | return 0; |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 80 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 81 | |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 82 | if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 || |
| 83 | num_channels > 2) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 84 | return -1; |
Tommi | f4fc0ff | 2016-05-26 22:40:09 +0200 | [diff] [blame] | 85 | } |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 86 | |
| 87 | src_sample_rate_hz_ = src_sample_rate_hz; |
| 88 | dst_sample_rate_hz_ = dst_sample_rate_hz; |
| 89 | num_channels_ = num_channels; |
| 90 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 91 | const size_t src_size_10ms_mono = |
| 92 | static_cast<size_t>(src_sample_rate_hz / 100); |
| 93 | const size_t dst_size_10ms_mono = |
| 94 | static_cast<size_t>(dst_sample_rate_hz / 100); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 95 | sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono, |
| 96 | dst_size_10ms_mono)); |
| 97 | if (num_channels_ == 2) { |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 98 | src_left_.reset(new T[src_size_10ms_mono]); |
| 99 | src_right_.reset(new T[src_size_10ms_mono]); |
| 100 | dst_left_.reset(new T[dst_size_10ms_mono]); |
| 101 | dst_right_.reset(new T[dst_size_10ms_mono]); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 102 | sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono, |
| 103 | dst_size_10ms_mono)); |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 109 | template <typename T> |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 110 | int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst, |
| 111 | size_t dst_capacity) { |
Tommi | c47f009 | 2016-05-26 22:55:35 +0200 | [diff] [blame] | 112 | CheckExpectedBufferSizes(src_length, dst_capacity, num_channels_, |
| 113 | src_sample_rate_hz_, dst_sample_rate_hz_); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 114 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 115 | if (src_sample_rate_hz_ == dst_sample_rate_hz_) { |
| 116 | // The old resampler provides this memcpy facility in the case of matching |
| 117 | // sample rates, so reproduce it here for the sinc resampler. |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 118 | memcpy(dst, src, src_length * sizeof(T)); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 119 | return static_cast<int>(src_length); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 120 | } |
| 121 | if (num_channels_ == 2) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 122 | const size_t src_length_mono = src_length / num_channels_; |
| 123 | const size_t dst_capacity_mono = dst_capacity / num_channels_; |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 124 | T* deinterleaved[] = {src_left_.get(), src_right_.get()}; |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 125 | Deinterleave(src, src_length_mono, num_channels_, deinterleaved); |
| 126 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 127 | size_t dst_length_mono = |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 128 | sinc_resampler_->Resample(src_left_.get(), src_length_mono, |
| 129 | dst_left_.get(), dst_capacity_mono); |
| 130 | sinc_resampler_right_->Resample(src_right_.get(), src_length_mono, |
| 131 | dst_right_.get(), dst_capacity_mono); |
| 132 | |
| 133 | deinterleaved[0] = dst_left_.get(); |
| 134 | deinterleaved[1] = dst_right_.get(); |
| 135 | Interleave(deinterleaved, dst_length_mono, num_channels_, dst); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 136 | return static_cast<int>(dst_length_mono * num_channels_); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 137 | } else { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 138 | return static_cast<int>( |
| 139 | sinc_resampler_->Resample(src, src_length, dst, dst_capacity)); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 143 | // Explictly generate required instantiations. |
| 144 | template class PushResampler<int16_t>; |
| 145 | template class PushResampler<float>; |
| 146 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 147 | } // namespace webrtc |