niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [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 | |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 | [diff] [blame] | 11 | #include "webrtc/voice_engine/utility.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | |
aleloi | 6321b49 | 2016-12-05 09:46:09 | [diff] [blame] | 13 | #include "webrtc/audio/utility/audio_frame_operations.h" |
Tommi | 54e1c6a | 2016-05-26 20:03:05 | [diff] [blame] | 14 | #include "webrtc/base/checks.h" |
pbos | ad85622 | 2015-11-27 17:48:36 | [diff] [blame] | 15 | #include "webrtc/base/logging.h" |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 16 | #include "webrtc/common_audio/resampler/include/push_resampler.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 | [diff] [blame] | 17 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 18 | #include "webrtc/common_types.h" |
Henrik Kjellander | ff761fb | 2015-11-04 07:31:52 | [diff] [blame] | 19 | #include "webrtc/modules/include/module_common_types.h" |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 20 | #include "webrtc/voice_engine/voice_engine_defines.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 21 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | namespace voe { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 24 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 25 | void RemixAndResample(const AudioFrame& src_frame, |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 26 | PushResampler<int16_t>* resampler, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 27 | AudioFrame* dst_frame) { |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame^] | 28 | RemixAndResample(src_frame.data(), src_frame.samples_per_channel_, |
Alejandro Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 29 | 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 | |
| 36 | void RemixAndResample(const int16_t* src_data, |
| 37 | size_t samples_per_channel, |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 38 | size_t num_channels, |
Alejandro Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 39 | int sample_rate_hz, |
| 40 | PushResampler<int16_t>* resampler, |
| 41 | AudioFrame* dst_frame) { |
| 42 | const int16_t* audio_ptr = src_data; |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 43 | size_t audio_ptr_num_channels = num_channels; |
henrik.lundin | fb4f8b6 | 2017-03-02 12:10:57 | [diff] [blame] | 44 | int16_t downsmixed_audio[AudioFrame::kMaxDataSizeSamples]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 45 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 46 | // Downmix before resampling. |
jens.nielsen | 228c268 | 2017-03-01 13:11:22 | [diff] [blame] | 47 | 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.lundin | fb4f8b6 | 2017-03-02 12:10:57 | [diff] [blame] | 55 | downsmixed_audio); |
| 56 | audio_ptr = downsmixed_audio; |
jens.nielsen | 228c268 | 2017-03-01 13:11:22 | [diff] [blame] | 57 | audio_ptr_num_channels = dst_frame->num_channels_; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 58 | } |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 | [diff] [blame] | 59 | |
Alejandro Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 60 | if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 61 | audio_ptr_num_channels) == -1) { |
Tommi | 54e1c6a | 2016-05-26 20:03:05 | [diff] [blame] | 62 | 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.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 65 | } |
| 66 | |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame^] | 67 | // 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 Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 71 | const size_t src_length = samples_per_channel * audio_ptr_num_channels; |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame^] | 72 | int out_length = resampler->Resample(audio_ptr, src_length, |
| 73 | dst_frame->mutable_data(), |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 74 | AudioFrame::kMaxDataSizeSamples); |
| 75 | if (out_length == -1) { |
Tommi | 54e1c6a | 2016-05-26 20:03:05 | [diff] [blame] | 76 | FATAL() << "Resample failed: audio_ptr = " << audio_ptr |
| 77 | << ", src_length = " << src_length |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame^] | 78 | << ", dst_frame->mutable_data() = " << dst_frame->mutable_data(); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 79 | } |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 80 | dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 81 | |
| 82 | // Upmix after resampling. |
Alejandro Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 83 | if (num_channels == 1 && dst_frame->num_channels_ == 2) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 84 | // 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.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 89 | } |
| 90 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 91 | void MixWithSat(int16_t target[], |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 92 | size_t target_channel, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 93 | const int16_t source[], |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 94 | size_t source_channel, |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 95 | size_t source_len) { |
kwiberg | af476c7 | 2016-11-28 23:21:39 | [diff] [blame] | 96 | 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.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 100 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 101 | 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 Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 105 | for (size_t i = 0; i < source_len; ++i) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 106 | 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.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 110 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 111 | } else if (target_channel == 1 && source_channel == 2) { |
| 112 | // Convert source from stereo to mono. |
| 113 | int32_t temp = 0; |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 114 | for (size_t i = 0; i < source_len / 2; ++i) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 115 | temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i]; |
| 116 | target[i] = WebRtcSpl_SatW32ToW16(temp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 117 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 118 | } else { |
| 119 | int32_t temp = 0; |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 120 | for (size_t i = 0; i < source_len; ++i) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 121 | temp = source[i] + target[i]; |
| 122 | target[i] = WebRtcSpl_SatW32ToW16(temp); |
| 123 | } |
| 124 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 125 | } |
| 126 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 127 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 128 | } // namespace webrtc |