blob: bd9ea8b1e37bb20ec3d2767beb5fab28090122a4 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#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 13:58:09 +020016#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/include/module_common_types.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000022namespace webrtc {
23namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000025void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000026 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000027 AudioFrame* dst_frame) {
yujo36b1a5f2017-06-12 12:45:32 -070028 RemixAndResample(src_frame.data(), src_frame.samples_per_channel_,
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070029 src_frame.num_channels_, src_frame.sample_rate_hz_,
30 resampler, dst_frame);
31 dst_frame->timestamp_ = src_frame.timestamp_;
32 dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
33 dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
34}
35
36void RemixAndResample(const int16_t* src_data,
37 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -080038 size_t num_channels,
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070039 int sample_rate_hz,
40 PushResampler<int16_t>* resampler,
41 AudioFrame* dst_frame) {
42 const int16_t* audio_ptr = src_data;
Peter Kasting69558702016-01-12 16:26:35 -080043 size_t audio_ptr_num_channels = num_channels;
henrik.lundinde5ff8e2017-07-07 05:29:47 -070044 int16_t downmixed_audio[AudioFrame::kMaxDataSizeSamples];
niklase@google.com470e71d2011-07-07 08:21:25 +000045
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000046 // Downmix before resampling.
jens.nielsen228c2682017-03-01 05:11:22 -080047 if (num_channels > dst_frame->num_channels_) {
48 RTC_DCHECK(num_channels == 2 || num_channels == 4)
49 << "num_channels: " << num_channels;
50 RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2)
51 << "dst_frame->num_channels_: " << dst_frame->num_channels_;
52
53 AudioFrameOperations::DownmixChannels(
54 src_data, num_channels, samples_per_channel, dst_frame->num_channels_,
henrik.lundinde5ff8e2017-07-07 05:29:47 -070055 downmixed_audio);
56 audio_ptr = downmixed_audio;
jens.nielsen228c2682017-03-01 05:11:22 -080057 audio_ptr_num_channels = dst_frame->num_channels_;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000058 }
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000059
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070060 if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000061 audio_ptr_num_channels) == -1) {
Tommi54e1c6a2016-05-26 22:03:05 +020062 FATAL() << "InitializeIfNeeded failed: sample_rate_hz = " << sample_rate_hz
63 << ", dst_frame->sample_rate_hz_ = " << dst_frame->sample_rate_hz_
64 << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000065 }
66
yujo36b1a5f2017-06-12 12:45:32 -070067 // TODO(yujo): for muted input frames, don't resample. Either 1) allow
68 // resampler to return output length without doing the resample, so we know
69 // how much to zero here; or 2) make resampler accept a hint that the input is
70 // zeroed.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070071 const size_t src_length = samples_per_channel * audio_ptr_num_channels;
yujo36b1a5f2017-06-12 12:45:32 -070072 int out_length = resampler->Resample(audio_ptr, src_length,
73 dst_frame->mutable_data(),
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000074 AudioFrame::kMaxDataSizeSamples);
75 if (out_length == -1) {
Tommi54e1c6a2016-05-26 22:03:05 +020076 FATAL() << "Resample failed: audio_ptr = " << audio_ptr
77 << ", src_length = " << src_length
yujo36b1a5f2017-06-12 12:45:32 -070078 << ", dst_frame->mutable_data() = " << dst_frame->mutable_data();
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000079 }
Peter Kasting69558702016-01-12 16:26:35 -080080 dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000081
82 // Upmix after resampling.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070083 if (num_channels == 1 && dst_frame->num_channels_ == 2) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000084 // The audio in dst_frame really is mono at this point; MonoToStereo will
85 // set this back to stereo.
86 dst_frame->num_channels_ = 1;
87 AudioFrameOperations::MonoToStereo(dst_frame);
88 }
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000091void MixWithSat(int16_t target[],
Peter Kasting69558702016-01-12 16:26:35 -080092 size_t target_channel,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000093 const int16_t source[],
Peter Kasting69558702016-01-12 16:26:35 -080094 size_t source_channel,
Peter Kastingdce40cf2015-08-24 14:52:23 -070095 size_t source_len) {
kwibergaf476c72016-11-28 15:21:39 -080096 RTC_DCHECK_GE(target_channel, 1);
97 RTC_DCHECK_LE(target_channel, 2);
98 RTC_DCHECK_GE(source_channel, 1);
99 RTC_DCHECK_LE(source_channel, 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000101 if (target_channel == 2 && source_channel == 1) {
102 // Convert source from mono to stereo.
103 int32_t left = 0;
104 int32_t right = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700105 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000106 left = source[i] + target[i * 2];
107 right = source[i] + target[i * 2 + 1];
108 target[i * 2] = WebRtcSpl_SatW32ToW16(left);
109 target[i * 2 + 1] = WebRtcSpl_SatW32ToW16(right);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000111 } else if (target_channel == 1 && source_channel == 2) {
112 // Convert source from stereo to mono.
113 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700114 for (size_t i = 0; i < source_len / 2; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000115 temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i];
116 target[i] = WebRtcSpl_SatW32ToW16(temp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000118 } else {
119 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700120 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000121 temp = source[i] + target[i];
122 target[i] = WebRtcSpl_SatW32ToW16(temp);
123 }
124 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000127} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000128} // namespace webrtc