2013-04-29 17:27:29 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "common_audio/resampler/include/push_resampler.h"
|
2013-04-29 17:27:29 +00:00
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <stdint.h>
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <string.h>
|
2013-04-29 17:27:29 +00:00
|
|
|
|
2019-09-17 17:06:18 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "common_audio/include/audio_util.h"
|
|
|
|
|
#include "common_audio/resampler/push_sinc_resampler.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
2013-04-29 17:27:29 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2016-05-26 22:40:09 +02:00
|
|
|
namespace {
|
|
|
|
|
// These checks were factored out into a non-templatized function
|
|
|
|
|
// due to problems with clang on Windows in debug builds.
|
|
|
|
|
// For some reason having the DCHECKs inline in the template code
|
|
|
|
|
// caused the compiler to generate code that threw off the linker.
|
2016-05-26 23:48:16 +02:00
|
|
|
// TODO(tommi): Re-enable when we've figured out what the problem is.
|
|
|
|
|
// http://crbug.com/615050
|
2016-05-26 22:40:09 +02:00
|
|
|
void CheckValidInitParams(int src_sample_rate_hz,
|
|
|
|
|
int dst_sample_rate_hz,
|
|
|
|
|
size_t num_channels) {
|
2016-05-26 23:48:16 +02:00
|
|
|
// The below checks are temporarily disabled on WEBRTC_WIN due to problems
|
|
|
|
|
// with clang debug builds.
|
2016-10-04 13:46:56 -07:00
|
|
|
#if !defined(WEBRTC_WIN) && defined(__clang__)
|
2016-05-26 22:40:09 +02:00
|
|
|
RTC_DCHECK_GT(src_sample_rate_hz, 0);
|
|
|
|
|
RTC_DCHECK_GT(dst_sample_rate_hz, 0);
|
2016-11-28 15:21:39 -08:00
|
|
|
RTC_DCHECK_GT(num_channels, 0);
|
2016-05-26 23:07:40 +02:00
|
|
|
#endif
|
2016-05-26 22:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-26 22:55:35 +02:00
|
|
|
void CheckExpectedBufferSizes(size_t src_length,
|
|
|
|
|
size_t dst_capacity,
|
|
|
|
|
size_t num_channels,
|
|
|
|
|
int src_sample_rate,
|
2016-05-26 22:40:09 +02:00
|
|
|
int dst_sample_rate) {
|
2016-05-26 23:48:16 +02:00
|
|
|
// The below checks are temporarily disabled on WEBRTC_WIN due to problems
|
|
|
|
|
// with clang debug builds.
|
|
|
|
|
// TODO(tommi): Re-enable when we've figured out what the problem is.
|
|
|
|
|
// http://crbug.com/615050
|
2016-10-04 13:46:56 -07:00
|
|
|
#if !defined(WEBRTC_WIN) && defined(__clang__)
|
2016-05-26 22:40:09 +02:00
|
|
|
const size_t src_size_10ms = src_sample_rate * num_channels / 100;
|
|
|
|
|
const size_t dst_size_10ms = dst_sample_rate * num_channels / 100;
|
2016-10-04 13:46:56 -07:00
|
|
|
RTC_DCHECK_EQ(src_length, src_size_10ms);
|
|
|
|
|
RTC_DCHECK_GE(dst_capacity, dst_size_10ms);
|
2016-05-26 23:07:40 +02:00
|
|
|
#endif
|
2016-05-26 22:40:09 +02:00
|
|
|
}
|
2017-03-09 06:25:06 -08:00
|
|
|
} // namespace
|
2013-04-29 17:27:29 +00:00
|
|
|
|
2014-04-19 00:32:07 +00:00
|
|
|
template <typename T>
|
|
|
|
|
PushResampler<T>::PushResampler()
|
2013-10-22 12:50:00 +00:00
|
|
|
: src_sample_rate_hz_(0), dst_sample_rate_hz_(0), num_channels_(0) {}
|
2013-04-29 17:27:29 +00:00
|
|
|
|
2014-04-19 00:32:07 +00:00
|
|
|
template <typename T>
|
|
|
|
|
PushResampler<T>::~PushResampler() {}
|
2013-04-29 17:27:29 +00:00
|
|
|
|
2014-04-19 00:32:07 +00:00
|
|
|
template <typename T>
|
|
|
|
|
int PushResampler<T>::InitializeIfNeeded(int src_sample_rate_hz,
|
|
|
|
|
int dst_sample_rate_hz,
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
size_t num_channels) {
|
2016-05-26 22:40:09 +02:00
|
|
|
CheckValidInitParams(src_sample_rate_hz, dst_sample_rate_hz, num_channels);
|
|
|
|
|
|
2013-04-29 17:27:29 +00:00
|
|
|
if (src_sample_rate_hz == src_sample_rate_hz_ &&
|
|
|
|
|
dst_sample_rate_hz == dst_sample_rate_hz_ &&
|
2016-05-26 22:40:09 +02:00
|
|
|
num_channels == num_channels_) {
|
2013-04-29 17:27:29 +00:00
|
|
|
// No-op if settings haven't changed.
|
|
|
|
|
return 0;
|
2016-05-26 22:40:09 +02:00
|
|
|
}
|
2013-04-29 17:27:29 +00:00
|
|
|
|
2018-10-02 14:09:46 +02:00
|
|
|
if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0) {
|
2013-04-29 17:27:29 +00:00
|
|
|
return -1;
|
2016-05-26 22:40:09 +02:00
|
|
|
}
|
2013-04-29 17:27:29 +00:00
|
|
|
|
|
|
|
|
src_sample_rate_hz_ = src_sample_rate_hz;
|
|
|
|
|
dst_sample_rate_hz_ = dst_sample_rate_hz;
|
|
|
|
|
num_channels_ = num_channels;
|
|
|
|
|
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
const size_t src_size_10ms_mono =
|
|
|
|
|
static_cast<size_t>(src_sample_rate_hz / 100);
|
|
|
|
|
const size_t dst_size_10ms_mono =
|
|
|
|
|
static_cast<size_t>(dst_sample_rate_hz / 100);
|
2018-10-02 14:09:46 +02:00
|
|
|
channel_resamplers_.clear();
|
|
|
|
|
for (size_t i = 0; i < num_channels; ++i) {
|
|
|
|
|
channel_resamplers_.push_back(ChannelResampler());
|
|
|
|
|
auto channel_resampler = channel_resamplers_.rbegin();
|
2019-09-17 17:06:18 +02:00
|
|
|
channel_resampler->resampler = std::make_unique<PushSincResampler>(
|
2018-10-02 14:09:46 +02:00
|
|
|
src_size_10ms_mono, dst_size_10ms_mono);
|
|
|
|
|
channel_resampler->source.resize(src_size_10ms_mono);
|
|
|
|
|
channel_resampler->destination.resize(dst_size_10ms_mono);
|
2013-04-29 17:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 10:57:13 +01:00
|
|
|
channel_data_array_.resize(num_channels_);
|
|
|
|
|
|
2013-04-29 17:27:29 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-19 00:32:07 +00:00
|
|
|
template <typename T>
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
int PushResampler<T>::Resample(const T* src,
|
|
|
|
|
size_t src_length,
|
|
|
|
|
T* dst,
|
|
|
|
|
size_t dst_capacity) {
|
2016-05-26 22:55:35 +02:00
|
|
|
CheckExpectedBufferSizes(src_length, dst_capacity, num_channels_,
|
|
|
|
|
src_sample_rate_hz_, dst_sample_rate_hz_);
|
2013-04-29 17:27:29 +00:00
|
|
|
|
|
|
|
|
if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
|
|
|
|
|
// The old resampler provides this memcpy facility in the case of matching
|
|
|
|
|
// sample rates, so reproduce it here for the sinc resampler.
|
2014-04-19 00:32:07 +00:00
|
|
|
memcpy(dst, src, src_length * sizeof(T));
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
return static_cast<int>(src_length);
|
2013-04-29 17:27:29 +00:00
|
|
|
}
|
2018-10-02 14:09:46 +02:00
|
|
|
|
|
|
|
|
const size_t src_length_mono = src_length / num_channels_;
|
|
|
|
|
const size_t dst_capacity_mono = dst_capacity / num_channels_;
|
|
|
|
|
|
2019-12-11 10:57:13 +01:00
|
|
|
for (size_t ch = 0; ch < num_channels_; ++ch) {
|
|
|
|
|
channel_data_array_[ch] = channel_resamplers_[ch].source.data();
|
2013-04-29 17:27:29 +00:00
|
|
|
}
|
2018-10-02 14:09:46 +02:00
|
|
|
|
2019-12-11 10:57:13 +01:00
|
|
|
Deinterleave(src, src_length_mono, num_channels_, channel_data_array_.data());
|
2018-10-02 14:09:46 +02:00
|
|
|
|
|
|
|
|
size_t dst_length_mono = 0;
|
|
|
|
|
|
|
|
|
|
for (auto& resampler : channel_resamplers_) {
|
|
|
|
|
dst_length_mono = resampler.resampler->Resample(
|
|
|
|
|
resampler.source.data(), src_length_mono, resampler.destination.data(),
|
|
|
|
|
dst_capacity_mono);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 10:57:13 +01:00
|
|
|
for (size_t ch = 0; ch < num_channels_; ++ch) {
|
|
|
|
|
channel_data_array_[ch] = channel_resamplers_[ch].destination.data();
|
2018-10-02 14:09:46 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 10:57:13 +01:00
|
|
|
Interleave(channel_data_array_.data(), dst_length_mono, num_channels_, dst);
|
2018-10-02 14:09:46 +02:00
|
|
|
return static_cast<int>(dst_length_mono * num_channels_);
|
2013-04-29 17:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-19 00:32:07 +00:00
|
|
|
// Explictly generate required instantiations.
|
|
|
|
|
template class PushResampler<int16_t>;
|
|
|
|
|
template class PushResampler<float>;
|
|
|
|
|
|
2013-04-29 17:27:29 +00:00
|
|
|
} // namespace webrtc
|