2013-09-12 18:30:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 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 "modules/audio_coding/acm2/acm_resampler.h"
|
2013-09-12 18:30:26 +00:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2024-05-24 16:43:55 +02:00
|
|
|
#include "api/audio/audio_frame.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2013-09-12 18:30:26 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2013-10-06 04:47:28 +00:00
|
|
|
namespace acm2 {
|
|
|
|
|
|
Consolidate audio conversion from Channel and TransmitMixer.
Replace the two versions with a single DownConvertToCodecFormat. As
mentioned in comments, this could be further consolidated with
RemixAndResample but we should write a full audio converter class in
that case.
Along the way:
- Fix the bug present in Channel::Demultiplex with mono input and a
stereo codec.
- Remove the 32 kHz max from the OnDataAvailable path. This avoids a
48 -> 32 -> 48 conversion when VoE is passed 48 kHz audio; instead we
get a straight pass-through to ACM. The 32 kHz conversion is still
needed in the RecordedDataIsAvailable path until APM natively supports
48 kHz.
- Merge resampler improvements from ACM1 to ACM2. This allows ACM to
handle 44.1 kHz audio passed to VoE and was originally done here:
https://webrtc-codereview.appspot.com/1590004
- Reuse the RemixAndResample unit tests for DownConvertToCodecFormat.
- Remove unused functions from utility.cc.
BUG=3155,3000,b/12867572
TESTED=voe_cmd_test using both the OnDataAvailable and
RecordedDataIsAvailable paths, with a captured audio format of all
combinations of {44.1,48} kHz and {1,2} channels, running through all
codecs, and finally using both ACM1 and ACM2.
R=henrika@webrtc.org, turaj@webrtc.org, xians@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/11019005
git-svn-id: http://webrtc.googlecode.com/svn/trunk@5843 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-04-03 21:56:01 +00:00
|
|
|
ACMResampler::ACMResampler() {}
|
2013-09-12 18:30:26 +00:00
|
|
|
|
|
|
|
|
ACMResampler::~ACMResampler() {}
|
|
|
|
|
|
|
|
|
|
int ACMResampler::Resample10Msec(const int16_t* in_audio,
|
|
|
|
|
int in_freq_hz,
|
|
|
|
|
int out_freq_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_audio_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
|
|
|
size_t out_capacity_samples,
|
2013-09-12 18:30:26 +00:00
|
|
|
int16_t* out_audio) {
|
2024-05-24 16:43:55 +02:00
|
|
|
InterleavedView<const int16_t> src(
|
|
|
|
|
in_audio, SampleRateToDefaultChannelSize(in_freq_hz), num_audio_channels);
|
|
|
|
|
InterleavedView<int16_t> dst(out_audio,
|
|
|
|
|
SampleRateToDefaultChannelSize(out_freq_hz),
|
|
|
|
|
num_audio_channels);
|
|
|
|
|
RTC_DCHECK_GE(out_capacity_samples, dst.size());
|
2013-09-12 18:30:26 +00:00
|
|
|
if (in_freq_hz == out_freq_hz) {
|
2024-05-24 16:43:55 +02:00
|
|
|
if (out_capacity_samples < src.data().size()) {
|
2021-11-15 16:57:07 +01:00
|
|
|
RTC_DCHECK_NOTREACHED();
|
2014-04-24 19:05:33 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2024-05-24 16:43:55 +02:00
|
|
|
CopySamples(dst, src);
|
|
|
|
|
RTC_DCHECK_EQ(dst.samples_per_channel(), src.samples_per_channel());
|
|
|
|
|
return static_cast<int>(dst.samples_per_channel());
|
2013-09-12 18:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-24 16:43:55 +02:00
|
|
|
int out_length = resampler_.Resample(src, dst);
|
Consolidate audio conversion from Channel and TransmitMixer.
Replace the two versions with a single DownConvertToCodecFormat. As
mentioned in comments, this could be further consolidated with
RemixAndResample but we should write a full audio converter class in
that case.
Along the way:
- Fix the bug present in Channel::Demultiplex with mono input and a
stereo codec.
- Remove the 32 kHz max from the OnDataAvailable path. This avoids a
48 -> 32 -> 48 conversion when VoE is passed 48 kHz audio; instead we
get a straight pass-through to ACM. The 32 kHz conversion is still
needed in the RecordedDataIsAvailable path until APM natively supports
48 kHz.
- Merge resampler improvements from ACM1 to ACM2. This allows ACM to
handle 44.1 kHz audio passed to VoE and was originally done here:
https://webrtc-codereview.appspot.com/1590004
- Reuse the RemixAndResample unit tests for DownConvertToCodecFormat.
- Remove unused functions from utility.cc.
BUG=3155,3000,b/12867572
TESTED=voe_cmd_test using both the OnDataAvailable and
RecordedDataIsAvailable paths, with a captured audio format of all
combinations of {44.1,48} kHz and {1,2} channels, running through all
codecs, and finally using both ACM1 and ACM2.
R=henrika@webrtc.org, turaj@webrtc.org, xians@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/11019005
git-svn-id: http://webrtc.googlecode.com/svn/trunk@5843 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-04-03 21:56:01 +00:00
|
|
|
if (out_length == -1) {
|
2024-05-24 16:43:55 +02:00
|
|
|
RTC_LOG(LS_ERROR) << "Resample(" << in_audio << ", " << src.data().size()
|
|
|
|
|
<< ", " << out_audio << ", " << out_capacity_samples
|
2017-11-09 11:09:25 +01:00
|
|
|
<< ") failed.";
|
2013-09-12 18:30:26 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2024-05-24 16:43:55 +02:00
|
|
|
RTC_DCHECK_EQ(out_length, dst.size());
|
|
|
|
|
RTC_DCHECK_EQ(out_length / num_audio_channels, dst.samples_per_channel());
|
|
|
|
|
return static_cast<int>(dst.samples_per_channel());
|
2013-09-12 18:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-06 11:53:14 +00:00
|
|
|
ResamplerHelper::ResamplerHelper() {
|
|
|
|
|
ClearSamples(last_audio_buffer_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ResamplerHelper::MaybeResample(int desired_sample_rate_hz,
|
|
|
|
|
AudioFrame* audio_frame) {
|
|
|
|
|
const int current_sample_rate_hz = audio_frame->sample_rate_hz_;
|
|
|
|
|
RTC_DCHECK_NE(current_sample_rate_hz, 0);
|
|
|
|
|
|
|
|
|
|
// Update if resampling is required.
|
|
|
|
|
const bool need_resampling =
|
|
|
|
|
(desired_sample_rate_hz != -1) &&
|
|
|
|
|
(current_sample_rate_hz != desired_sample_rate_hz);
|
|
|
|
|
|
|
|
|
|
if (need_resampling && !resampled_last_output_frame_) {
|
|
|
|
|
// Prime the resampler with the last frame.
|
|
|
|
|
int16_t temp_output[AudioFrame::kMaxDataSizeSamples];
|
|
|
|
|
int samples_per_channel_int = resampler_.Resample10Msec(
|
|
|
|
|
last_audio_buffer_.data(), current_sample_rate_hz,
|
|
|
|
|
desired_sample_rate_hz, audio_frame->num_channels_,
|
|
|
|
|
AudioFrame::kMaxDataSizeSamples, temp_output);
|
|
|
|
|
if (samples_per_channel_int < 0) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "AcmReceiver::GetAudio - "
|
|
|
|
|
"Resampling last_audio_buffer_ failed.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(bugs.webrtc.org/3923) Glitches in the output may appear if the output
|
|
|
|
|
// rate from NetEq changes.
|
|
|
|
|
if (need_resampling) {
|
|
|
|
|
// TODO(yujo): handle this more efficiently for muted frames.
|
|
|
|
|
int samples_per_channel_int = resampler_.Resample10Msec(
|
|
|
|
|
audio_frame->data(), current_sample_rate_hz, desired_sample_rate_hz,
|
|
|
|
|
audio_frame->num_channels_, AudioFrame::kMaxDataSizeSamples,
|
|
|
|
|
audio_frame->mutable_data());
|
|
|
|
|
if (samples_per_channel_int < 0) {
|
|
|
|
|
RTC_LOG(LS_ERROR)
|
|
|
|
|
<< "AcmReceiver::GetAudio - Resampling audio_buffer_ failed.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
audio_frame->samples_per_channel_ =
|
|
|
|
|
static_cast<size_t>(samples_per_channel_int);
|
|
|
|
|
audio_frame->sample_rate_hz_ = desired_sample_rate_hz;
|
|
|
|
|
RTC_DCHECK_EQ(
|
|
|
|
|
audio_frame->sample_rate_hz_,
|
|
|
|
|
rtc::dchecked_cast<int>(audio_frame->samples_per_channel_ * 100));
|
|
|
|
|
resampled_last_output_frame_ = true;
|
|
|
|
|
} else {
|
|
|
|
|
resampled_last_output_frame_ = false;
|
|
|
|
|
// We might end up here ONLY if codec is changed.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store current audio in `last_audio_buffer_` for next time.
|
|
|
|
|
// TODO: b/335805780 - Use CopySamples().
|
|
|
|
|
memcpy(last_audio_buffer_.data(), audio_frame->data(),
|
|
|
|
|
sizeof(int16_t) * audio_frame->samples_per_channel_ *
|
|
|
|
|
audio_frame->num_channels_);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-06 04:47:28 +00:00
|
|
|
} // namespace acm2
|
2013-09-12 18:30:26 +00:00
|
|
|
} // namespace webrtc
|