blob: 80fdc58a49955cadacbacd75e4691d2c117afdd0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
kma@webrtc.org551fcc02012-02-07 18:03:11 +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 "common_audio/signal_processing/include/signal_processing_library.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
14#include "rtc_base/sanitizer.h"
kwibergac554ee2016-09-02 00:39:33 -070015
kma@webrtc.org551fcc02012-02-07 18:03:11 +000016// TODO(Bjornv): Change the function parameter order to WebRTC code style.
kma@webrtc.org0221b782012-09-08 00:09:26 +000017// C version of WebRtcSpl_DownsampleFast() for generic platforms.
18int WebRtcSpl_DownsampleFastC(const int16_t* data_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -070019 size_t data_in_length,
kma@webrtc.org0221b782012-09-08 00:09:26 +000020 int16_t* data_out,
Peter Kastingdce40cf2015-08-24 14:52:23 -070021 size_t data_out_length,
kma@webrtc.org0221b782012-09-08 00:09:26 +000022 const int16_t* __restrict coefficients,
Peter Kastingdce40cf2015-08-24 14:52:23 -070023 size_t coefficients_length,
kma@webrtc.org0221b782012-09-08 00:09:26 +000024 int factor,
Peter Kastingdce40cf2015-08-24 14:52:23 -070025 size_t delay) {
kwibergac554ee2016-09-02 00:39:33 -070026 int16_t* const original_data_out = data_out;
Peter Kastingdce40cf2015-08-24 14:52:23 -070027 size_t i = 0;
28 size_t j = 0;
kma@webrtc.org551fcc02012-02-07 18:03:11 +000029 int32_t out_s32 = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -070030 size_t endpos = delay + factor * (data_out_length - 1) + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000031
kma@webrtc.org551fcc02012-02-07 18:03:11 +000032 // Return error if any of the running conditions doesn't meet.
Peter Kastingdce40cf2015-08-24 14:52:23 -070033 if (data_out_length == 0 || coefficients_length == 0
kma@webrtc.org551fcc02012-02-07 18:03:11 +000034 || data_in_length < endpos) {
35 return -1;
36 }
niklase@google.com470e71d2011-07-07 08:21:25 +000037
kwibergac554ee2016-09-02 00:39:33 -070038 rtc_MsanCheckInitialized(coefficients, sizeof(coefficients[0]),
39 coefficients_length);
40
kma@webrtc.org551fcc02012-02-07 18:03:11 +000041 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++) {
Artem Titov13fb0ef2018-04-19 09:58:17 +020045 // Negative overflow is permitted here, because this is
46 // auto-regressive filters, and the state for each batch run is
47 // stored in the "negative" positions of the output vector.
48 rtc_MsanCheckInitialized(&data_in[(ptrdiff_t) i - (ptrdiff_t) j],
49 sizeof(data_in[0]), 1);
50 // out_s32 is in Q12 domain.
51 out_s32 += coefficients[j] * data_in[(ptrdiff_t) i - (ptrdiff_t) j];
niklase@google.com470e71d2011-07-07 08:21:25 +000052 }
53
kma@webrtc.org551fcc02012-02-07 18:03:11 +000054 out_s32 >>= 12; // Q0.
niklase@google.com470e71d2011-07-07 08:21:25 +000055
kma@webrtc.org551fcc02012-02-07 18:03:11 +000056 // Saturate and store the output.
57 *data_out++ = WebRtcSpl_SatW32ToW16(out_s32);
58 }
niklase@google.com470e71d2011-07-07 08:21:25 +000059
kwibergac554ee2016-09-02 00:39:33 -070060 RTC_DCHECK_EQ(original_data_out + data_out_length, data_out);
61 rtc_MsanCheckInitialized(original_data_out, sizeof(original_data_out[0]),
62 data_out_length);
63
kma@webrtc.org551fcc02012-02-07 18:03:11 +000064 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000065}