blob: 38cdf2b1727049c32af66cab81d35012c0397582 [file] [log] [blame]
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +00001/*
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.org12dc1a32013-08-05 16:22:53 +000013#include <string.h>
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000014
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 Lemurc20978e2017-07-06 19:44:34 +020018#include "webrtc/rtc_base/checks.h"
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000019
20namespace webrtc {
Tommif4fc0ff2016-05-26 22:40:09 +020021namespace {
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.
Tommi90edc652016-05-26 23:48:16 +020026// TODO(tommi): Re-enable when we've figured out what the problem is.
27// http://crbug.com/615050
Tommif4fc0ff2016-05-26 22:40:09 +020028void CheckValidInitParams(int src_sample_rate_hz, int dst_sample_rate_hz,
29 size_t num_channels) {
Tommi90edc652016-05-26 23:48:16 +020030// The below checks are temporarily disabled on WEBRTC_WIN due to problems
31// with clang debug builds.
kwiberg5377bc72016-10-04 13:46:56 -070032#if !defined(WEBRTC_WIN) && defined(__clang__)
Tommif4fc0ff2016-05-26 22:40:09 +020033 RTC_DCHECK_GT(src_sample_rate_hz, 0);
34 RTC_DCHECK_GT(dst_sample_rate_hz, 0);
kwibergaf476c72016-11-28 15:21:39 -080035 RTC_DCHECK_GT(num_channels, 0);
36 RTC_DCHECK_LE(num_channels, 2);
Tommi0ad72ea2016-05-26 23:07:40 +020037#endif
Tommif4fc0ff2016-05-26 22:40:09 +020038}
39
Tommic47f0092016-05-26 22:55:35 +020040void CheckExpectedBufferSizes(size_t src_length,
41 size_t dst_capacity,
42 size_t num_channels,
43 int src_sample_rate,
Tommif4fc0ff2016-05-26 22:40:09 +020044 int dst_sample_rate) {
Tommi90edc652016-05-26 23:48:16 +020045// 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
kwiberg5377bc72016-10-04 13:46:56 -070049#if !defined(WEBRTC_WIN) && defined(__clang__)
Tommif4fc0ff2016-05-26 22:40:09 +020050 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;
kwiberg5377bc72016-10-04 13:46:56 -070052 RTC_DCHECK_EQ(src_length, src_size_10ms);
53 RTC_DCHECK_GE(dst_capacity, dst_size_10ms);
Tommi0ad72ea2016-05-26 23:07:40 +020054#endif
Tommif4fc0ff2016-05-26 22:40:09 +020055}
oprypin67fdb802017-03-09 06:25:06 -080056} // namespace
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000057
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000058template <typename T>
59PushResampler<T>::PushResampler()
andrew@webrtc.org31628aa2013-10-22 12:50:00 +000060 : src_sample_rate_hz_(0),
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000061 dst_sample_rate_hz_(0),
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000062 num_channels_(0) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000063}
64
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000065template <typename T>
66PushResampler<T>::~PushResampler() {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000067}
68
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000069template <typename T>
70int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
71 int dst_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080072 size_t num_channels) {
Tommif4fc0ff2016-05-26 22:40:09 +020073 CheckValidInitParams(src_sample_rate_hz, dst_sample_rate_hz, num_channels);
74
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000075 if (src_sample_rate_hz == src_sample_rate_hz_ &&
76 dst_sample_rate_hz == dst_sample_rate_hz_ &&
Tommif4fc0ff2016-05-26 22:40:09 +020077 num_channels == num_channels_) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000078 // No-op if settings haven't changed.
79 return 0;
Tommif4fc0ff2016-05-26 22:40:09 +020080 }
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000081
Tommif4fc0ff2016-05-26 22:40:09 +020082 if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 ||
83 num_channels > 2) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000084 return -1;
Tommif4fc0ff2016-05-26 22:40:09 +020085 }
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000086
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 Kastingdce40cf2015-08-24 14:52:23 -070091 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.org50b2efe2013-04-29 17:27:29 +000095 sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono,
96 dst_size_10ms_mono));
97 if (num_channels_ == 2) {
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000098 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.org50b2efe2013-04-29 17:27:29 +0000102 sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono,
103 dst_size_10ms_mono));
104 }
105
106 return 0;
107}
108
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000109template <typename T>
Peter Kastingdce40cf2015-08-24 14:52:23 -0700110int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst,
111 size_t dst_capacity) {
Tommic47f0092016-05-26 22:55:35 +0200112 CheckExpectedBufferSizes(src_length, dst_capacity, num_channels_,
113 src_sample_rate_hz_, dst_sample_rate_hz_);
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000114
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000115 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.orgf5a33f12014-04-19 00:32:07 +0000118 memcpy(dst, src, src_length * sizeof(T));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 return static_cast<int>(src_length);
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000120 }
121 if (num_channels_ == 2) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700122 const size_t src_length_mono = src_length / num_channels_;
123 const size_t dst_capacity_mono = dst_capacity / num_channels_;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000124 T* deinterleaved[] = {src_left_.get(), src_right_.get()};
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000125 Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
126
Peter Kastingdce40cf2015-08-24 14:52:23 -0700127 size_t dst_length_mono =
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000128 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 Kastingdce40cf2015-08-24 14:52:23 -0700136 return static_cast<int>(dst_length_mono * num_channels_);
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000137 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 return static_cast<int>(
139 sinc_resampler_->Resample(src, src_length, dst, dst_capacity));
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000140 }
141}
142
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000143// Explictly generate required instantiations.
144template class PushResampler<int16_t>;
145template class PushResampler<float>;
146
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000147} // namespace webrtc