1 // Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <limits.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <gtest/gtest.h>
9
10 extern "C" {
11 #include "linear_resampler.h"
12 }
13
14 #define BUF_SIZE 2048
15
16 static uint8_t in_buf[BUF_SIZE];
17 static uint8_t out_buf[BUF_SIZE];
18
TEST(LinearResampler,ReampleToSlightlyLargerRate)19 TEST(LinearResampler, ReampleToSlightlyLargerRate) {
20 int i, rc;
21 unsigned int count;
22 unsigned int in_offset = 0;
23 unsigned int out_offset = 0;
24 struct linear_resampler *lr;
25
26
27 memset(in_buf, 0, BUF_SIZE);
28 memset(out_buf, 0, BUF_SIZE);
29 for (i = 0; i < 100; i++) {
30 *((int16_t *)(in_buf + i * 4)) = i * 10;
31 *((int16_t *)(in_buf + i * 4 + 2)) = i * 20;
32 }
33
34 lr = linear_resampler_create(2, 4, 48000, 48001);
35
36 count = 20;
37 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
38 out_buf + 4 * out_offset, 50);
39 EXPECT_EQ(20, rc);
40 EXPECT_EQ(20, count);
41
42 in_offset += count;
43 out_offset += rc;
44 count = 20;
45 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
46 out_buf + 4 * out_offset, 15);
47 EXPECT_EQ(15, rc);
48 EXPECT_EQ(15, count);
49
50 /* Assert linear interpotation result. */
51 for (i = 0; i < 34; i++) {
52 EXPECT_GE(*(int16_t *)(in_buf + 4 * i),
53 *(int16_t *)(out_buf + 4 * i));
54 EXPECT_LE(*(int16_t *)(in_buf + 4 * i),
55 *(int16_t *)(out_buf + 4 * (i + 1)));
56 }
57 linear_resampler_destroy(lr);
58 }
59
TEST(LinearResampler,ResampleIntegerFractionToLarger)60 TEST(LinearResampler, ResampleIntegerFractionToLarger) {
61 int i, rc;
62 unsigned int count;
63 unsigned int in_offset = 0;
64 unsigned int out_offset = 0;
65 struct linear_resampler *lr;
66
67 memset(in_buf, 0, BUF_SIZE);
68 memset(out_buf, 0, BUF_SIZE);
69 for (i = 0; i < 100; i++) {
70 *((int16_t *)(in_buf + i * 4)) = SHRT_MAX - i;
71 *((int16_t *)(in_buf + i * 4 + 2)) = SHRT_MAX - i * 10;
72 }
73
74 /* Rate 10 -> 11 */
75 lr = linear_resampler_create(2, 4, 10, 11);
76
77 count = 5;
78 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
79 out_buf + 4 * out_offset, 10);
80 EXPECT_EQ(5, rc);
81 EXPECT_EQ(5, count);
82
83 in_offset += count;
84 out_offset += rc;
85 count = 6;
86 /* Assert source rate + 1 frames resample to destination rate + 1
87 * frames. */
88 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
89 out_buf + 4 * out_offset, 10);
90 EXPECT_EQ(7, rc);
91 EXPECT_EQ(6, count);
92
93 in_offset += count;
94 out_offset += rc;
95 count = 89;
96 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
97 out_buf + 4 * out_offset, 100);
98 EXPECT_EQ(97, rc);
99 EXPECT_EQ(89, count);
100
101 /* Assert linear interpotation result. */
102 for (i = 0; i < 90; i++) {
103 EXPECT_LE(*(int16_t *)(in_buf + 4 * i),
104 *(int16_t *)(out_buf + 4 * i));
105 EXPECT_LE(*(int16_t *)(in_buf + 4 * i + 2),
106 *(int16_t *)(out_buf + 4 * i + 2));
107 }
108 linear_resampler_destroy(lr);
109 }
110
TEST(LinearResampler,ResampleIntegerFractionToLess)111 TEST(LinearResampler, ResampleIntegerFractionToLess) {
112 int i, rc;
113 unsigned int count;
114 unsigned int in_offset = 0;
115 unsigned int out_offset = 0;
116 struct linear_resampler *lr;
117
118 memset(in_buf, 0, BUF_SIZE);
119 memset(out_buf, 0, BUF_SIZE);
120 for (i = 0; i < 100; i++) {
121 *((int16_t *)(in_buf + i * 4)) = SHRT_MIN + i * 10;
122 *((int16_t *)(in_buf + i * 4 + 2)) = SHRT_MIN + i * 20;
123 }
124
125 /* Rate 10 -> 9 */
126 lr = linear_resampler_create(2, 4, 10, 9);
127
128 count = 6;
129 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
130 out_buf + 4 * out_offset, 6);
131 EXPECT_EQ(5, rc);
132 EXPECT_EQ(6, count);
133
134 in_offset += count;
135 out_offset += rc;
136 count = 4;
137
138 /* Assert source rate frames resample to destination rate frames. */
139 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
140 out_buf + 4 * out_offset, 4);
141 EXPECT_EQ(4, rc);
142 EXPECT_EQ(4, count);
143
144 in_offset += count;
145 out_offset += rc;
146 count = 90;
147 rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
148 out_buf + 4 * out_offset, 90);
149
150 /* Assert linear interpotation result. */
151 for (i = 0; i < 90; i++) {
152 EXPECT_LE(*(int16_t *)(in_buf + 4 * i),
153 *(int16_t *)(out_buf + 4 * i));
154 EXPECT_LE(*(int16_t *)(in_buf + 4 * i + 2),
155 *(int16_t *)(out_buf + 4 * i + 2));
156 }
157 linear_resampler_destroy(lr);
158 }
159
TEST(LinearResampler,ResampleIntegerNoSrcBuffer)160 TEST(LinearResampler, ResampleIntegerNoSrcBuffer) {
161 int rc;
162 unsigned int count;
163 struct linear_resampler *lr;
164
165 memset(in_buf, 0, BUF_SIZE);
166 memset(out_buf, 0, BUF_SIZE);
167
168 /* Rate 10 -> 9 */
169 lr = linear_resampler_create(2, 4, 10, 9);
170
171 count = 0;
172 rc = linear_resampler_resample(lr, in_buf, &count,
173 out_buf, BUF_SIZE);
174 EXPECT_EQ(0, rc);
175 EXPECT_EQ(0, count);
176 linear_resampler_destroy(lr);
177 }
178
TEST(LinearResampler,ResampleIntegerNoDstBuffer)179 TEST(LinearResampler, ResampleIntegerNoDstBuffer) {
180 int rc;
181 unsigned int count;
182 struct linear_resampler *lr;
183
184 memset(in_buf, 0, BUF_SIZE);
185 memset(out_buf, 0, BUF_SIZE);
186
187 /* Rate 10 -> 9 */
188 lr = linear_resampler_create(2, 4, 10, 9);
189
190 count = BUF_SIZE;
191 rc = linear_resampler_resample(lr, in_buf, &count,
192 out_buf, 0);
193 EXPECT_EQ(0, rc);
194 EXPECT_EQ(0, count);
195 linear_resampler_destroy(lr);
196 }
197
198 extern "C" {
199
cras_mix_add_scale_stride(int fmt,uint8_t * dst,uint8_t * src,unsigned int count,unsigned int dst_stride,unsigned int src_stride,float scaler)200 void cras_mix_add_scale_stride(int fmt, uint8_t *dst, uint8_t *src,
201 unsigned int count, unsigned int dst_stride,
202 unsigned int src_stride, float scaler)
203 {
204 unsigned int i;
205
206 for (i = 0; i < count; i++) {
207 int32_t sum;
208 sum = *(int16_t *)dst + *(int16_t *)src * scaler;
209 if (sum > INT16_MAX)
210 sum = INT16_MAX;
211 else if (sum < INT16_MIN)
212 sum = INT16_MIN;
213 *(int16_t*)dst = sum;
214 dst += dst_stride;
215 src += src_stride;
216 }
217 }
218
219 } // extern "C"
220
main(int argc,char ** argv)221 int main(int argc, char **argv) {
222 ::testing::InitGoogleTest(&argc, argv);
223 return RUN_ALL_TESTS();
224 }
225