blob: 52a491fdd9217029eace8d153766e43252f73eea [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
braveyao@webrtc.orgd7131432012-03-29 10:39:442 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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 10:18:3111#include "audio/remix_resample.h"
niklase@google.com470e71d2011-07-07 08:21:2512
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "audio/utility/audio_frame_operations.h"
14#include "common_audio/resampler/include/push_resampler.h"
15#include "common_audio/signal_processing/include/signal_processing_library.h"
Mirko Bonadei71207422017-09-15 11:58:0916#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "modules/include/module_common_types.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
niklase@google.com470e71d2011-07-07 08:21:2520
andrew@webrtc.org40ee3d02014-04-03 21:56:0121namespace webrtc {
22namespace voe {
niklase@google.com470e71d2011-07-07 08:21:2523
andrew@webrtc.org40ee3d02014-04-03 21:56:0124void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:0725 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:0126 AudioFrame* dst_frame) {
yujo36b1a5f2017-06-12 19:45:3227 RemixAndResample(src_frame.data(), src_frame.samples_per_channel_,
Alejandro Luebscdfe20b2015-09-23 19:49:1228 src_frame.num_channels_, src_frame.sample_rate_hz_,
29 resampler, dst_frame);
30 dst_frame->timestamp_ = src_frame.timestamp_;
31 dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
32 dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
33}
34
35void RemixAndResample(const int16_t* src_data,
36 size_t samples_per_channel,
Peter Kasting69558702016-01-13 00:26:3537 size_t num_channels,
Alejandro Luebscdfe20b2015-09-23 19:49:1238 int sample_rate_hz,
39 PushResampler<int16_t>* resampler,
40 AudioFrame* dst_frame) {
41 const int16_t* audio_ptr = src_data;
Peter Kasting69558702016-01-13 00:26:3542 size_t audio_ptr_num_channels = num_channels;
henrik.lundinde5ff8e2017-07-07 12:29:4743 int16_t downmixed_audio[AudioFrame::kMaxDataSizeSamples];
niklase@google.com470e71d2011-07-07 08:21:2544
andrew@webrtc.org40ee3d02014-04-03 21:56:0145 // Downmix before resampling.
jens.nielsen228c2682017-03-01 13:11:2246 if (num_channels > dst_frame->num_channels_) {
47 RTC_DCHECK(num_channels == 2 || num_channels == 4)
48 << "num_channels: " << num_channels;
49 RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2)
50 << "dst_frame->num_channels_: " << dst_frame->num_channels_;
51
52 AudioFrameOperations::DownmixChannels(
53 src_data, num_channels, samples_per_channel, dst_frame->num_channels_,
henrik.lundinde5ff8e2017-07-07 12:29:4754 downmixed_audio);
55 audio_ptr = downmixed_audio;
jens.nielsen228c2682017-03-01 13:11:2256 audio_ptr_num_channels = dst_frame->num_channels_;
andrew@webrtc.org40ee3d02014-04-03 21:56:0157 }
braveyao@webrtc.orgd7131432012-03-29 10:39:4458
Alejandro Luebscdfe20b2015-09-23 19:49:1259 if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
andrew@webrtc.org40ee3d02014-04-03 21:56:0160 audio_ptr_num_channels) == -1) {
Tommi54e1c6a2016-05-26 20:03:0561 FATAL() << "InitializeIfNeeded failed: sample_rate_hz = " << sample_rate_hz
62 << ", dst_frame->sample_rate_hz_ = " << dst_frame->sample_rate_hz_
63 << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:0164 }
65
yujo36b1a5f2017-06-12 19:45:3266 // TODO(yujo): for muted input frames, don't resample. Either 1) allow
67 // resampler to return output length without doing the resample, so we know
68 // how much to zero here; or 2) make resampler accept a hint that the input is
69 // zeroed.
Alejandro Luebscdfe20b2015-09-23 19:49:1270 const size_t src_length = samples_per_channel * audio_ptr_num_channels;
yujo36b1a5f2017-06-12 19:45:3271 int out_length = resampler->Resample(audio_ptr, src_length,
72 dst_frame->mutable_data(),
andrew@webrtc.org40ee3d02014-04-03 21:56:0173 AudioFrame::kMaxDataSizeSamples);
74 if (out_length == -1) {
Tommi54e1c6a2016-05-26 20:03:0575 FATAL() << "Resample failed: audio_ptr = " << audio_ptr
76 << ", src_length = " << src_length
yujo36b1a5f2017-06-12 19:45:3277 << ", dst_frame->mutable_data() = " << dst_frame->mutable_data();
andrew@webrtc.org40ee3d02014-04-03 21:56:0178 }
Peter Kasting69558702016-01-13 00:26:3579 dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:0180
81 // Upmix after resampling.
Alejandro Luebscdfe20b2015-09-23 19:49:1282 if (num_channels == 1 && dst_frame->num_channels_ == 2) {
andrew@webrtc.org40ee3d02014-04-03 21:56:0183 // The audio in dst_frame really is mono at this point; MonoToStereo will
84 // set this back to stereo.
85 dst_frame->num_channels_ = 1;
86 AudioFrameOperations::MonoToStereo(dst_frame);
87 }
niklase@google.com470e71d2011-07-07 08:21:2588}
89
pbos@webrtc.orgd900e8b2013-07-03 15:12:2690} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:2691} // namespace webrtc