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