niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 Solenberg | a8b7c7f | 2018-01-17 11:18:31 +0100 | [diff] [blame] | 11 | #include "audio/remix_resample.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 13 | #include "api/audio/audio_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "audio/utility/audio_frame_operations.h" |
| 15 | #include "common_audio/resampler/include/push_resampler.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | namespace voe { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 21 | void RemixAndResample(const AudioFrame& src_frame, |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 22 | PushResampler<int16_t>* resampler, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 23 | AudioFrame* dst_frame) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 24 | RemixAndResample(src_frame.data(), src_frame.samples_per_channel_, |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 25 | 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_; |
Alessio Bazzica | 8f319a3 | 2019-07-24 16:47:02 +0000 | [diff] [blame] | 30 | dst_frame->packet_infos_ = src_frame.packet_infos_; |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void RemixAndResample(const int16_t* src_data, |
| 34 | size_t samples_per_channel, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 35 | size_t num_channels, |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 36 | int sample_rate_hz, |
| 37 | PushResampler<int16_t>* resampler, |
| 38 | AudioFrame* dst_frame) { |
| 39 | const int16_t* audio_ptr = src_data; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 40 | size_t audio_ptr_num_channels = num_channels; |
henrik.lundin | de5ff8e | 2017-07-07 05:29:47 -0700 | [diff] [blame] | 41 | int16_t downmixed_audio[AudioFrame::kMaxDataSizeSamples]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 43 | // Downmix before resampling. |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 44 | 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.lundin | de5ff8e | 2017-07-07 05:29:47 -0700 | [diff] [blame] | 52 | downmixed_audio); |
| 53 | audio_ptr = downmixed_audio; |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 54 | audio_ptr_num_channels = dst_frame->num_channels_; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 55 | } |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 +0000 | [diff] [blame] | 56 | |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 57 | if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 58 | audio_ptr_num_channels) == -1) { |
Tommi | 54e1c6a | 2016-05-26 22:03:05 +0200 | [diff] [blame] | 59 | 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.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 64 | // 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 Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 68 | const size_t src_length = samples_per_channel * audio_ptr_num_channels; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 69 | int out_length = |
| 70 | resampler->Resample(audio_ptr, src_length, dst_frame->mutable_data(), |
| 71 | AudioFrame::kMaxDataSizeSamples); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 72 | if (out_length == -1) { |
Tommi | 54e1c6a | 2016-05-26 22:03:05 +0200 | [diff] [blame] | 73 | FATAL() << "Resample failed: audio_ptr = " << audio_ptr |
| 74 | << ", src_length = " << src_length |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 75 | << ", dst_frame->mutable_data() = " << dst_frame->mutable_data(); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 76 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 77 | dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 78 | |
| 79 | // Upmix after resampling. |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 80 | if (num_channels == 1 && dst_frame->num_channels_ == 2) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 81 | // 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 Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 84 | AudioFrameOperations::UpmixChannels(2, dst_frame); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 85 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 88 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 89 | } // namespace webrtc |