niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/sanitizer.h" |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 15 | |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 16 | // TODO(Bjornv): Change the function parameter order to WebRTC code style. |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 17 | // C version of WebRtcSpl_DownsampleFast() for generic platforms. |
| 18 | int WebRtcSpl_DownsampleFastC(const int16_t* data_in, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 19 | size_t data_in_length, |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 20 | int16_t* data_out, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 21 | size_t data_out_length, |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 22 | const int16_t* __restrict coefficients, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 23 | size_t coefficients_length, |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 24 | int factor, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 25 | size_t delay) { |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 26 | int16_t* const original_data_out = data_out; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 27 | size_t i = 0; |
| 28 | size_t j = 0; |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 29 | int32_t out_s32 = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 30 | size_t endpos = delay + factor * (data_out_length - 1) + 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 32 | // Return error if any of the running conditions doesn't meet. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 33 | if (data_out_length == 0 || coefficients_length == 0 |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 34 | || data_in_length < endpos) { |
| 35 | return -1; |
| 36 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 38 | rtc_MsanCheckInitialized(coefficients, sizeof(coefficients[0]), |
| 39 | coefficients_length); |
| 40 | |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 41 | for (i = delay; i < endpos; i += factor) { |
| 42 | out_s32 = 2048; // Round value, 0.5 in Q12. |
| 43 | |
| 44 | for (j = 0; j < coefficients_length; j++) { |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 45 | rtc_MsanCheckInitialized(&data_in[i - j], sizeof(data_in[0]), 1); |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 46 | out_s32 += coefficients[j] * data_in[i - j]; // Q12. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 49 | out_s32 >>= 12; // Q0. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 51 | // Saturate and store the output. |
| 52 | *data_out++ = WebRtcSpl_SatW32ToW16(out_s32); |
| 53 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 55 | RTC_DCHECK_EQ(original_data_out + data_out_length, data_out); |
| 56 | rtc_MsanCheckInitialized(original_data_out, sizeof(original_data_out[0]), |
| 57 | data_out_length); |
| 58 | |
kma@webrtc.org | 551fcc0 | 2012-02-07 18:03:11 +0000 | [diff] [blame] | 59 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | } |