blob: 104f95d3afe8cb85b27ed8ba31dbccc487236603 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_DYADIC_DECIMATOR_H_
12#define MODULES_AUDIO_PROCESSING_TRANSIENT_DYADIC_DECIMATOR_H_
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013
14#include <cstdlib>
15
Mirko Bonadei71207422017-09-15 13:58:09 +020016#include "typedefs.h" // NOLINT(build/include)
pbos@webrtc.org788acd12014-12-15 09:41:24 +000017
18// Provides a set of static methods to perform dyadic decimations.
19
20namespace webrtc {
21
22// Returns the proper length of the output buffer that you should use for the
23// given |in_length| and decimation |odd_sequence|.
24// Return -1 on error.
25inline size_t GetOutLengthToDyadicDecimate(size_t in_length,
26 bool odd_sequence) {
27 size_t out_length = in_length / 2;
28
29 if (in_length % 2 == 1 && !odd_sequence) {
30 ++out_length;
31 }
32
33 return out_length;
34}
35
36// Performs a dyadic decimation: removes every odd/even member of a sequence
37// halving its overall length.
38// Arguments:
39// in: array of |in_length|.
40// odd_sequence: If false, the odd members will be removed (1, 3, 5, ...);
41// if true, the even members will be removed (0, 2, 4, ...).
42// out: array of |out_length|. |out_length| must be large enough to
43// hold the decimated output. The necessary length can be provided by
44// GetOutLengthToDyadicDecimate().
45// Must be previously allocated.
46// Returns the number of output samples, -1 on error.
47template<typename T>
48static size_t DyadicDecimate(const T* in,
49 size_t in_length,
50 bool odd_sequence,
51 T* out,
52 size_t out_length) {
53 size_t half_length = GetOutLengthToDyadicDecimate(in_length, odd_sequence);
54
55 if (!in || !out || in_length <= 0 || out_length < half_length) {
56 return 0;
57 }
58
59 size_t output_samples = 0;
60 size_t index_adjustment = odd_sequence ? 1 : 0;
61 for (output_samples = 0; output_samples < half_length; ++output_samples) {
62 out[output_samples] = in[output_samples * 2 + index_adjustment];
63 }
64
65 return output_samples;
66}
67
68} // namespace webrtc
69
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020070#endif // MODULES_AUDIO_PROCESSING_TRANSIENT_DYADIC_DECIMATOR_H_