Line data Source code
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 :
13 : #include <string.h>
14 :
15 : #include "webrtc/base/checks.h"
16 : #include "webrtc/common_audio/include/audio_util.h"
17 : #include "webrtc/common_audio/resampler/push_sinc_resampler.h"
18 :
19 : namespace webrtc {
20 : namespace {
21 : // These checks were factored out into a non-templatized function
22 : // due to problems with clang on Windows in debug builds.
23 : // For some reason having the DCHECKs inline in the template code
24 : // caused the compiler to generate code that threw off the linker.
25 : // TODO(tommi): Re-enable when we've figured out what the problem is.
26 : // http://crbug.com/615050
27 0 : void CheckValidInitParams(int src_sample_rate_hz, int dst_sample_rate_hz,
28 : size_t num_channels) {
29 : // The below checks are temporarily disabled on WEBRTC_WIN due to problems
30 : // with clang debug builds.
31 : #if !defined(WEBRTC_WIN) && defined(__clang__)
32 : RTC_DCHECK_GT(src_sample_rate_hz, 0);
33 : RTC_DCHECK_GT(dst_sample_rate_hz, 0);
34 : RTC_DCHECK_GT(num_channels, 0);
35 : RTC_DCHECK_LE(num_channels, 2);
36 : #endif
37 0 : }
38 :
39 0 : void CheckExpectedBufferSizes(size_t src_length,
40 : size_t dst_capacity,
41 : size_t num_channels,
42 : int src_sample_rate,
43 : int dst_sample_rate) {
44 : // The below checks are temporarily disabled on WEBRTC_WIN due to problems
45 : // with clang debug builds.
46 : // TODO(tommi): Re-enable when we've figured out what the problem is.
47 : // http://crbug.com/615050
48 : #if !defined(WEBRTC_WIN) && defined(__clang__)
49 : const size_t src_size_10ms = src_sample_rate * num_channels / 100;
50 : const size_t dst_size_10ms = dst_sample_rate * num_channels / 100;
51 : RTC_DCHECK_EQ(src_length, src_size_10ms);
52 : RTC_DCHECK_GE(dst_capacity, dst_size_10ms);
53 : #endif
54 0 : }
55 : }
56 :
57 : template <typename T>
58 0 : PushResampler<T>::PushResampler()
59 : : src_sample_rate_hz_(0),
60 : dst_sample_rate_hz_(0),
61 0 : num_channels_(0) {
62 0 : }
63 :
64 : template <typename T>
65 0 : PushResampler<T>::~PushResampler() {
66 0 : }
67 :
68 : template <typename T>
69 0 : int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
70 : int dst_sample_rate_hz,
71 : size_t num_channels) {
72 0 : CheckValidInitParams(src_sample_rate_hz, dst_sample_rate_hz, num_channels);
73 :
74 0 : if (src_sample_rate_hz == src_sample_rate_hz_ &&
75 0 : dst_sample_rate_hz == dst_sample_rate_hz_ &&
76 0 : num_channels == num_channels_) {
77 : // No-op if settings haven't changed.
78 0 : return 0;
79 : }
80 :
81 0 : if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 ||
82 : num_channels > 2) {
83 0 : return -1;
84 : }
85 :
86 0 : src_sample_rate_hz_ = src_sample_rate_hz;
87 0 : dst_sample_rate_hz_ = dst_sample_rate_hz;
88 0 : num_channels_ = num_channels;
89 :
90 : const size_t src_size_10ms_mono =
91 0 : static_cast<size_t>(src_sample_rate_hz / 100);
92 : const size_t dst_size_10ms_mono =
93 0 : static_cast<size_t>(dst_sample_rate_hz / 100);
94 0 : sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono,
95 : dst_size_10ms_mono));
96 0 : if (num_channels_ == 2) {
97 0 : src_left_.reset(new T[src_size_10ms_mono]);
98 0 : src_right_.reset(new T[src_size_10ms_mono]);
99 0 : dst_left_.reset(new T[dst_size_10ms_mono]);
100 0 : dst_right_.reset(new T[dst_size_10ms_mono]);
101 0 : sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono,
102 : dst_size_10ms_mono));
103 : }
104 :
105 0 : return 0;
106 : }
107 :
108 : template <typename T>
109 0 : int PushResampler<T>::Resample(const T* src, size_t src_length, T* dst,
110 : size_t dst_capacity) {
111 0 : CheckExpectedBufferSizes(src_length, dst_capacity, num_channels_,
112 : src_sample_rate_hz_, dst_sample_rate_hz_);
113 :
114 0 : 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.
117 0 : memcpy(dst, src, src_length * sizeof(T));
118 0 : return static_cast<int>(src_length);
119 : }
120 0 : if (num_channels_ == 2) {
121 0 : const size_t src_length_mono = src_length / num_channels_;
122 0 : const size_t dst_capacity_mono = dst_capacity / num_channels_;
123 0 : T* deinterleaved[] = {src_left_.get(), src_right_.get()};
124 0 : Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
125 :
126 : size_t dst_length_mono =
127 : sinc_resampler_->Resample(src_left_.get(), src_length_mono,
128 0 : dst_left_.get(), dst_capacity_mono);
129 0 : sinc_resampler_right_->Resample(src_right_.get(), src_length_mono,
130 : dst_right_.get(), dst_capacity_mono);
131 :
132 0 : deinterleaved[0] = dst_left_.get();
133 0 : deinterleaved[1] = dst_right_.get();
134 0 : Interleave(deinterleaved, dst_length_mono, num_channels_, dst);
135 0 : return static_cast<int>(dst_length_mono * num_channels_);
136 : } else {
137 0 : return static_cast<int>(
138 0 : sinc_resampler_->Resample(src, src_length, dst, dst_capacity));
139 : }
140 : }
141 :
142 : // Explictly generate required instantiations.
143 : template class PushResampler<int16_t>;
144 : template class PushResampler<float>;
145 :
146 : } // namespace webrtc
|