blob: 485ec80c56f500ac5a9d99725fb1f237e20a7082 [file] [log] [blame]
andrew@webrtc.orgaada86b2014-10-27 18:18:171/*
2 * Copyright (c) 2014 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 Bonadei92ea95e2017-09-15 04:47:3111#include "common_audio/audio_converter.h"
andrew@webrtc.org2c29c2e2015-02-11 01:09:5012
13#include <cstring>
kwiberg4a206a92016-03-31 17:24:2614#include <memory>
kwiberg0eb15ed2015-12-17 11:04:1515#include <utility>
kwiberg4a206a92016-03-31 17:24:2616#include <vector>
andrew@webrtc.org2c29c2e2015-02-11 01:09:5017
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "common_audio/channel_buffer.h"
19#include "common_audio/resampler/push_sinc_resampler.h"
20#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 09:42:2621#include "rtc_base/numerics/safe_conversions.h"
andrew@webrtc.org2c29c2e2015-02-11 01:09:5022
andrew@webrtc.orgaada86b2014-10-27 18:18:1723namespace webrtc {
andrew@webrtc.orgaada86b2014-10-27 18:18:1724
andrew@webrtc.org2c29c2e2015-02-11 01:09:5025class CopyConverter : public AudioConverter {
26 public:
Yves Gerey665174f2018-06-19 13:03:0527 CopyConverter(size_t src_channels,
28 size_t src_frames,
29 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 21:52:2330 size_t dst_frames)
andrew@webrtc.org2c29c2e2015-02-11 01:09:5031 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {}
Nico Weber22f99252019-02-20 15:13:1632 ~CopyConverter() override {}
andrew@webrtc.org2c29c2e2015-02-11 01:09:5033
Yves Gerey665174f2018-06-19 13:03:0534 void Convert(const float* const* src,
35 size_t src_size,
36 float* const* dst,
andrew@webrtc.org2c29c2e2015-02-11 01:09:5037 size_t dst_capacity) override {
38 CheckSizes(src_size, dst_capacity);
39 if (src != dst) {
Peter Kasting69558702016-01-13 00:26:3540 for (size_t i = 0; i < src_channels(); ++i)
andrew@webrtc.org2c29c2e2015-02-11 01:09:5041 std::memcpy(dst[i], src[i], dst_frames() * sizeof(*dst[i]));
42 }
andrew@webrtc.orgaada86b2014-10-27 18:18:1743 }
andrew@webrtc.org2c29c2e2015-02-11 01:09:5044};
45
46class UpmixConverter : public AudioConverter {
47 public:
Yves Gerey665174f2018-06-19 13:03:0548 UpmixConverter(size_t src_channels,
49 size_t src_frames,
50 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 21:52:2351 size_t dst_frames)
andrew@webrtc.org2c29c2e2015-02-11 01:09:5052 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {}
Nico Weber22f99252019-02-20 15:13:1653 ~UpmixConverter() override {}
andrew@webrtc.org2c29c2e2015-02-11 01:09:5054
Yves Gerey665174f2018-06-19 13:03:0555 void Convert(const float* const* src,
56 size_t src_size,
57 float* const* dst,
andrew@webrtc.org2c29c2e2015-02-11 01:09:5058 size_t dst_capacity) override {
59 CheckSizes(src_size, dst_capacity);
Peter Kastingdce40cf2015-08-24 21:52:2360 for (size_t i = 0; i < dst_frames(); ++i) {
andrew@webrtc.org2c29c2e2015-02-11 01:09:5061 const float value = src[0][i];
Peter Kasting69558702016-01-13 00:26:3562 for (size_t j = 0; j < dst_channels(); ++j)
andrew@webrtc.org2c29c2e2015-02-11 01:09:5063 dst[j][i] = value;
64 }
65 }
66};
67
68class DownmixConverter : public AudioConverter {
69 public:
Yves Gerey665174f2018-06-19 13:03:0570 DownmixConverter(size_t src_channels,
71 size_t src_frames,
72 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 21:52:2373 size_t dst_frames)
Yves Gerey665174f2018-06-19 13:03:0574 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {}
Nico Weber22f99252019-02-20 15:13:1675 ~DownmixConverter() override {}
andrew@webrtc.org2c29c2e2015-02-11 01:09:5076
Yves Gerey665174f2018-06-19 13:03:0577 void Convert(const float* const* src,
78 size_t src_size,
79 float* const* dst,
andrew@webrtc.org2c29c2e2015-02-11 01:09:5080 size_t dst_capacity) override {
81 CheckSizes(src_size, dst_capacity);
82 float* dst_mono = dst[0];
Peter Kastingdce40cf2015-08-24 21:52:2383 for (size_t i = 0; i < src_frames(); ++i) {
andrew@webrtc.org2c29c2e2015-02-11 01:09:5084 float sum = 0;
Peter Kasting69558702016-01-13 00:26:3585 for (size_t j = 0; j < src_channels(); ++j)
andrew@webrtc.org2c29c2e2015-02-11 01:09:5086 sum += src[j][i];
87 dst_mono[i] = sum / src_channels();
88 }
89 }
90};
91
92class ResampleConverter : public AudioConverter {
93 public:
Yves Gerey665174f2018-06-19 13:03:0594 ResampleConverter(size_t src_channels,
95 size_t src_frames,
96 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 21:52:2397 size_t dst_frames)
andrew@webrtc.org2c29c2e2015-02-11 01:09:5098 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {
99 resamplers_.reserve(src_channels);
Peter Kasting69558702016-01-13 00:26:35100 for (size_t i = 0; i < src_channels; ++i)
kwiberg4a206a92016-03-31 17:24:26101 resamplers_.push_back(std::unique_ptr<PushSincResampler>(
102 new PushSincResampler(src_frames, dst_frames)));
andrew@webrtc.org2c29c2e2015-02-11 01:09:50103 }
Nico Weber22f99252019-02-20 15:13:16104 ~ResampleConverter() override {}
andrew@webrtc.org2c29c2e2015-02-11 01:09:50105
Yves Gerey665174f2018-06-19 13:03:05106 void Convert(const float* const* src,
107 size_t src_size,
108 float* const* dst,
andrew@webrtc.org2c29c2e2015-02-11 01:09:50109 size_t dst_capacity) override {
110 CheckSizes(src_size, dst_capacity);
111 for (size_t i = 0; i < resamplers_.size(); ++i)
112 resamplers_[i]->Resample(src[i], src_frames(), dst[i], dst_frames());
113 }
114
115 private:
kwiberg4a206a92016-03-31 17:24:26116 std::vector<std::unique_ptr<PushSincResampler>> resamplers_;
andrew@webrtc.org2c29c2e2015-02-11 01:09:50117};
118
119// Apply a vector of converters in serial, in the order given. At least two
120// converters must be provided.
121class CompositionConverter : public AudioConverter {
122 public:
oprypin67fdb802017-03-09 14:25:06123 explicit CompositionConverter(
Yves Gerey665174f2018-06-19 13:03:05124 std::vector<std::unique_ptr<AudioConverter>> converters)
kwiberg0eb15ed2015-12-17 11:04:15125 : converters_(std::move(converters)) {
kwibergaf476c72016-11-28 23:21:39126 RTC_CHECK_GE(converters_.size(), 2);
andrew@webrtc.org2c29c2e2015-02-11 01:09:50127 // We need an intermediate buffer after every converter.
128 for (auto it = converters_.begin(); it != converters_.end() - 1; ++it)
kwiberg4a206a92016-03-31 17:24:26129 buffers_.push_back(
130 std::unique_ptr<ChannelBuffer<float>>(new ChannelBuffer<float>(
131 (*it)->dst_frames(), (*it)->dst_channels())));
andrew@webrtc.org2c29c2e2015-02-11 01:09:50132 }
Nico Weber22f99252019-02-20 15:13:16133 ~CompositionConverter() override {}
andrew@webrtc.org2c29c2e2015-02-11 01:09:50134
Yves Gerey665174f2018-06-19 13:03:05135 void Convert(const float* const* src,
136 size_t src_size,
137 float* const* dst,
andrew@webrtc.org2c29c2e2015-02-11 01:09:50138 size_t dst_capacity) override {
139 converters_.front()->Convert(src, src_size, buffers_.front()->channels(),
140 buffers_.front()->size());
141 for (size_t i = 2; i < converters_.size(); ++i) {
kwiberg4a206a92016-03-31 17:24:26142 auto& src_buffer = buffers_[i - 2];
143 auto& dst_buffer = buffers_[i - 1];
Yves Gerey665174f2018-06-19 13:03:05144 converters_[i]->Convert(src_buffer->channels(), src_buffer->size(),
145 dst_buffer->channels(), dst_buffer->size());
andrew@webrtc.org2c29c2e2015-02-11 01:09:50146 }
147 converters_.back()->Convert(buffers_.back()->channels(),
148 buffers_.back()->size(), dst, dst_capacity);
149 }
150
151 private:
kwiberg4a206a92016-03-31 17:24:26152 std::vector<std::unique_ptr<AudioConverter>> converters_;
153 std::vector<std::unique_ptr<ChannelBuffer<float>>> buffers_;
andrew@webrtc.org2c29c2e2015-02-11 01:09:50154};
155
kwibergc2b785d2016-02-24 13:22:32156std::unique_ptr<AudioConverter> AudioConverter::Create(size_t src_channels,
Peter Kastingdce40cf2015-08-24 21:52:23157 size_t src_frames,
Peter Kasting69558702016-01-13 00:26:35158 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 21:52:23159 size_t dst_frames) {
kwibergc2b785d2016-02-24 13:22:32160 std::unique_ptr<AudioConverter> sp;
andrew@webrtc.org2c29c2e2015-02-11 01:09:50161 if (src_channels > dst_channels) {
162 if (src_frames != dst_frames) {
kwiberg4a206a92016-03-31 17:24:26163 std::vector<std::unique_ptr<AudioConverter>> converters;
164 converters.push_back(std::unique_ptr<AudioConverter>(new DownmixConverter(
165 src_channels, src_frames, dst_channels, src_frames)));
166 converters.push_back(
167 std::unique_ptr<AudioConverter>(new ResampleConverter(
168 dst_channels, src_frames, dst_channels, dst_frames)));
kwiberg0eb15ed2015-12-17 11:04:15169 sp.reset(new CompositionConverter(std::move(converters)));
andrew@webrtc.org2c29c2e2015-02-11 01:09:50170 } else {
171 sp.reset(new DownmixConverter(src_channels, src_frames, dst_channels,
172 dst_frames));
173 }
174 } else if (src_channels < dst_channels) {
175 if (src_frames != dst_frames) {
kwiberg4a206a92016-03-31 17:24:26176 std::vector<std::unique_ptr<AudioConverter>> converters;
177 converters.push_back(
178 std::unique_ptr<AudioConverter>(new ResampleConverter(
179 src_channels, src_frames, src_channels, dst_frames)));
180 converters.push_back(std::unique_ptr<AudioConverter>(new UpmixConverter(
181 src_channels, dst_frames, dst_channels, dst_frames)));
kwiberg0eb15ed2015-12-17 11:04:15182 sp.reset(new CompositionConverter(std::move(converters)));
andrew@webrtc.org2c29c2e2015-02-11 01:09:50183 } else {
184 sp.reset(new UpmixConverter(src_channels, src_frames, dst_channels,
185 dst_frames));
186 }
187 } else if (src_frames != dst_frames) {
188 sp.reset(new ResampleConverter(src_channels, src_frames, dst_channels,
189 dst_frames));
190 } else {
Yves Gerey665174f2018-06-19 13:03:05191 sp.reset(
192 new CopyConverter(src_channels, src_frames, dst_channels, dst_frames));
andrew@webrtc.org2c29c2e2015-02-11 01:09:50193 }
194
kwiberg0eb15ed2015-12-17 11:04:15195 return sp;
andrew@webrtc.orgaada86b2014-10-27 18:18:17196}
197
andrew@webrtc.org2c29c2e2015-02-11 01:09:50198// For CompositionConverter.
199AudioConverter::AudioConverter()
Yves Gerey665174f2018-06-19 13:03:05200 : src_channels_(0), src_frames_(0), dst_channels_(0), dst_frames_(0) {}
andrew@webrtc.orgaada86b2014-10-27 18:18:17201
Yves Gerey665174f2018-06-19 13:03:05202AudioConverter::AudioConverter(size_t src_channels,
203 size_t src_frames,
204 size_t dst_channels,
205 size_t dst_frames)
andrew@webrtc.org58049362014-11-03 21:32:14206 : src_channels_(src_channels),
207 src_frames_(src_frames),
208 dst_channels_(dst_channels),
209 dst_frames_(dst_frames) {
henrikg91d6ede2015-09-17 07:24:34210 RTC_CHECK(dst_channels == src_channels || dst_channels == 1 ||
211 src_channels == 1);
andrew@webrtc.orgaada86b2014-10-27 18:18:17212}
213
andrew@webrtc.org2c29c2e2015-02-11 01:09:50214void AudioConverter::CheckSizes(size_t src_size, size_t dst_capacity) const {
henrikg91d6ede2015-09-17 07:24:34215 RTC_CHECK_EQ(src_size, src_channels() * src_frames());
216 RTC_CHECK_GE(dst_capacity, dst_channels() * dst_frames());
andrew@webrtc.orgaada86b2014-10-27 18:18:17217}
218
219} // namespace webrtc