blob: 3694d34e40a859137de80626d27bfc28eae36102 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010011#include "audio/remix_resample.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020013#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "audio/utility/audio_frame_operations.h"
15#include "common_audio/resampler/include/push_resampler.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000018namespace webrtc {
19namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000020
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000021void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000022 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000023 AudioFrame* dst_frame) {
yujo36b1a5f2017-06-12 12:45:32 -070024 RemixAndResample(src_frame.data(), src_frame.samples_per_channel_,
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070025 src_frame.num_channels_, src_frame.sample_rate_hz_,
26 resampler, dst_frame);
27 dst_frame->timestamp_ = src_frame.timestamp_;
28 dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
29 dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
Chen Xing3e8ef942019-07-01 17:16:32 +020030 dst_frame->packet_infos_ = src_frame.packet_infos_;
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070031}
32
33void RemixAndResample(const int16_t* src_data,
34 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -080035 size_t num_channels,
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070036 int sample_rate_hz,
37 PushResampler<int16_t>* resampler,
38 AudioFrame* dst_frame) {
39 const int16_t* audio_ptr = src_data;
Peter Kasting69558702016-01-12 16:26:35 -080040 size_t audio_ptr_num_channels = num_channels;
henrik.lundinde5ff8e2017-07-07 05:29:47 -070041 int16_t downmixed_audio[AudioFrame::kMaxDataSizeSamples];
niklase@google.com470e71d2011-07-07 08:21:25 +000042
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000043 // Downmix before resampling.
jens.nielsen228c2682017-03-01 05:11:22 -080044 if (num_channels > dst_frame->num_channels_) {
45 RTC_DCHECK(num_channels == 2 || num_channels == 4)
46 << "num_channels: " << num_channels;
47 RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2)
48 << "dst_frame->num_channels_: " << dst_frame->num_channels_;
49
50 AudioFrameOperations::DownmixChannels(
51 src_data, num_channels, samples_per_channel, dst_frame->num_channels_,
henrik.lundinde5ff8e2017-07-07 05:29:47 -070052 downmixed_audio);
53 audio_ptr = downmixed_audio;
jens.nielsen228c2682017-03-01 05:11:22 -080054 audio_ptr_num_channels = dst_frame->num_channels_;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000055 }
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000056
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070057 if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000058 audio_ptr_num_channels) == -1) {
Tommi54e1c6a2016-05-26 22:03:05 +020059 FATAL() << "InitializeIfNeeded failed: sample_rate_hz = " << sample_rate_hz
60 << ", dst_frame->sample_rate_hz_ = " << dst_frame->sample_rate_hz_
61 << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000062 }
63
yujo36b1a5f2017-06-12 12:45:32 -070064 // TODO(yujo): for muted input frames, don't resample. Either 1) allow
65 // resampler to return output length without doing the resample, so we know
66 // how much to zero here; or 2) make resampler accept a hint that the input is
67 // zeroed.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070068 const size_t src_length = samples_per_channel * audio_ptr_num_channels;
Yves Gerey665174f2018-06-19 15:03:05 +020069 int out_length =
70 resampler->Resample(audio_ptr, src_length, dst_frame->mutable_data(),
71 AudioFrame::kMaxDataSizeSamples);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000072 if (out_length == -1) {
Tommi54e1c6a2016-05-26 22:03:05 +020073 FATAL() << "Resample failed: audio_ptr = " << audio_ptr
74 << ", src_length = " << src_length
yujo36b1a5f2017-06-12 12:45:32 -070075 << ", dst_frame->mutable_data() = " << dst_frame->mutable_data();
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000076 }
Peter Kasting69558702016-01-12 16:26:35 -080077 dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000078
79 // Upmix after resampling.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070080 if (num_channels == 1 && dst_frame->num_channels_ == 2) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000081 // The audio in dst_frame really is mono at this point; MonoToStereo will
82 // set this back to stereo.
83 dst_frame->num_channels_ = 1;
Alex Loikob4977de2019-01-28 16:38:38 +010084 AudioFrameOperations::UpmixChannels(2, dst_frame);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000085 }
niklase@google.com470e71d2011-07-07 08:21:25 +000086}
87
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000088} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000089} // namespace webrtc