1 // Copyright (c) 2012 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 <gtest/gtest.h>
6 #include <sys/param.h>
7
8 extern "C" {
9 #include "cras_fmt_conv.h"
10 #include "cras_types.h"
11 }
12
13 static int mono_channel_layout[CRAS_CH_MAX] =
14 {-1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1};
15 static int stereo_channel_layout[CRAS_CH_MAX] =
16 {0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
17 static int surround_channel_layout[CRAS_CH_MAX] =
18 {0, 1, 2, 3, 4, 5, -1, -1, -1, -1, -1};
19 static int quad_channel_layout[CRAS_CH_MAX] =
20 {0, 1, 2, 3, -1, -1, -1, -1, -1, -1, -1};
21 static int linear_resampler_needed_val;
22 static double linear_resampler_ratio = 1.0;
23 static unsigned int linear_resampler_num_channels;
24 static unsigned int linear_resampler_format_bytes;
25
ResetStub()26 void ResetStub() {
27 linear_resampler_needed_val = 0;
28 linear_resampler_ratio = 1.0;
29 }
30
31 // Like malloc or calloc, but fill the memory with random bytes.
ralloc(size_t size)32 static void *ralloc(size_t size) {
33 unsigned char *buf = (unsigned char *)malloc(size);
34 while (size--)
35 buf[size] = rand() & 0xff;
36 return buf;
37 }
38
swap_channel_layout(int8_t * layout,CRAS_CHANNEL a,CRAS_CHANNEL b)39 static void swap_channel_layout(int8_t *layout,
40 CRAS_CHANNEL a,
41 CRAS_CHANNEL b) {
42 int8_t tmp = layout[a];
43 layout[a] = layout[b];
44 layout[b] = tmp;
45 }
46
TEST(FormatConverterTest,SmallFramesSRCWithLinearResampler)47 TEST(FormatConverterTest, SmallFramesSRCWithLinearResampler) {
48 struct cras_audio_format in_fmt;
49 struct cras_audio_format out_fmt;
50 struct cras_fmt_conv *c;
51 int16_t *in_buf;
52 int16_t *out_buf;
53 unsigned int in_frames = 1;
54 unsigned int out_frames = 2;
55
56 ResetStub();
57 in_fmt.format = out_fmt.format = SND_PCM_FORMAT_S16_LE;
58 in_fmt.num_channels = out_fmt.num_channels = 1;
59 in_fmt.frame_rate = 16000;
60 out_fmt.frame_rate = 48000;
61 linear_resampler_needed_val = 1;
62
63 in_buf = (int16_t *)malloc(10 * 2 * 2);
64 out_buf = (int16_t *)malloc(10 * 2 * 2);
65
66 c = cras_fmt_conv_create(&in_fmt, &out_fmt, 10, 1);
67 EXPECT_NE((void *)NULL, c);
68
69 /* When process on small buffers doing SRC 16KHz -> 48KHz,
70 * speex does the work in two steps:
71 *
72 * (1) 0 -> 2 frames in output
73 * (2) 1 -> 1 frame in output
74 *
75 * Total result is 1 frame consumed in input and generated
76 * 3 frames in output.
77 */
78 out_frames = cras_fmt_conv_convert_frames(c, (uint8_t *)in_buf,
79 (uint8_t *)out_buf,
80 &in_frames,
81 out_frames);
82 EXPECT_EQ(2, out_frames);
83 EXPECT_EQ(0, in_frames);
84
85 in_frames = 1;
86 out_frames = 2;
87 out_frames = cras_fmt_conv_convert_frames(c, (uint8_t *)in_buf,
88 (uint8_t *)out_buf,
89 &in_frames,
90 out_frames);
91 EXPECT_EQ(1, out_frames);
92 EXPECT_EQ(1, in_frames);
93
94 cras_fmt_conv_destroy(&c);
95 free(in_buf);
96 free(out_buf);
97 }
98
99 // Only support LE, BE should fail.
TEST(FormatConverterTest,InvalidParamsOnlyLE)100 TEST(FormatConverterTest, InvalidParamsOnlyLE) {
101 struct cras_audio_format in_fmt;
102 struct cras_audio_format out_fmt;
103 struct cras_fmt_conv *c;
104
105 ResetStub();
106 in_fmt.format = out_fmt.format = SND_PCM_FORMAT_S32_BE;
107 in_fmt.num_channels = out_fmt.num_channels = 2;
108 c = cras_fmt_conv_create(&in_fmt, &out_fmt, 4096, 0);
109 EXPECT_EQ(NULL, c);
110 }
111
112 // Test Mono to Stereo mix.
TEST(FormatConverterTest,MonoToStereo)113 TEST(FormatConverterTest, MonoToStereo) {
114 struct cras_fmt_conv *c;
115 struct cras_audio_format in_fmt;
116 struct cras_audio_format out_fmt;
117
118 size_t out_frames;
119 int16_t *in_buff;
120 int16_t *out_buff;
121 const size_t buf_size = 4096;
122 unsigned int in_buf_size = 4096;
123
124 ResetStub();
125 in_fmt.format = SND_PCM_FORMAT_S16_LE;
126 out_fmt.format = SND_PCM_FORMAT_S16_LE;
127 in_fmt.num_channels = 1;
128 out_fmt.num_channels = 2;
129 in_fmt.frame_rate = 48000;
130 out_fmt.frame_rate = 48000;
131
132 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
133 ASSERT_NE(c, (void *)NULL);
134
135 out_frames = cras_fmt_conv_out_frames_to_in(c, buf_size);
136 EXPECT_EQ(buf_size, out_frames);
137
138 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
139 EXPECT_EQ(buf_size, out_frames);
140
141 in_buff = (int16_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
142 out_buff = (int16_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&out_fmt));
143 out_frames = cras_fmt_conv_convert_frames(c,
144 (uint8_t *)in_buff,
145 (uint8_t *)out_buff,
146 &in_buf_size,
147 buf_size);
148 EXPECT_EQ(buf_size, out_frames);
149 for (size_t i = 0; i < buf_size; i++) {
150 if (in_buff[i] != out_buff[i*2] ||
151 in_buff[i] != out_buff[i*2 + 1]) {
152 EXPECT_TRUE(false);
153 break;
154 }
155 }
156
157 cras_fmt_conv_destroy(&c);
158 free(in_buff);
159 free(out_buff);
160 }
161
162 // Test Stereo to Mono mix.
TEST(FormatConverterTest,StereoToMono)163 TEST(FormatConverterTest, StereoToMono) {
164 struct cras_fmt_conv *c;
165 struct cras_audio_format in_fmt;
166 struct cras_audio_format out_fmt;
167
168 size_t out_frames;
169 int16_t *in_buff;
170 int16_t *out_buff;
171 unsigned int i;
172 const size_t buf_size = 4096;
173 unsigned int in_buf_size = 4096;
174
175 ResetStub();
176 in_fmt.format = SND_PCM_FORMAT_S16_LE;
177 out_fmt.format = SND_PCM_FORMAT_S16_LE;
178 in_fmt.num_channels = 2;
179 out_fmt.num_channels = 1;
180 in_fmt.frame_rate = 48000;
181 out_fmt.frame_rate = 48000;
182
183 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
184 ASSERT_NE(c, (void *)NULL);
185
186 out_frames = cras_fmt_conv_out_frames_to_in(c, buf_size);
187 EXPECT_EQ(buf_size, out_frames);
188
189 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
190 EXPECT_EQ(buf_size, out_frames);
191
192 in_buff = (int16_t *)malloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
193 out_buff = (int16_t *)malloc(buf_size * cras_get_format_bytes(&out_fmt));
194 for (i = 0; i < buf_size; i++) {
195 in_buff[i * 2] = 13450;
196 in_buff[i * 2 + 1] = -13449;
197 }
198 out_frames = cras_fmt_conv_convert_frames(c,
199 (uint8_t *)in_buff,
200 (uint8_t *)out_buff,
201 &in_buf_size,
202 buf_size);
203 EXPECT_EQ(buf_size, out_frames);
204 for (i = 0; i < buf_size; i++) {
205 EXPECT_EQ(1, out_buff[i]);
206 }
207
208 cras_fmt_conv_destroy(&c);
209 free(in_buff);
210 free(out_buff);
211 }
212
213 // Test Stereo to Mono mix 24 and 32 bit.
TEST(FormatConverterTest,StereoToMono24bit)214 TEST(FormatConverterTest, StereoToMono24bit) {
215 struct cras_fmt_conv *c;
216 struct cras_audio_format in_fmt;
217 struct cras_audio_format out_fmt;
218
219 size_t out_frames;
220 int32_t *in_buff;
221 int32_t *out_buff;
222 unsigned int i;
223 const size_t buf_size = 100;
224 unsigned int in_buf_size = 100;
225 unsigned int test;
226
227 for (test = 0; test < 2; test++) {
228 ResetStub();
229 if (test == 0) {
230 in_fmt.format = SND_PCM_FORMAT_S24_LE;
231 out_fmt.format = SND_PCM_FORMAT_S24_LE;
232 } else {
233 in_fmt.format = SND_PCM_FORMAT_S32_LE;
234 out_fmt.format = SND_PCM_FORMAT_S32_LE;
235 }
236 in_fmt.num_channels = 2;
237 out_fmt.num_channels = 1;
238 in_fmt.frame_rate = 48000;
239 out_fmt.frame_rate = 48000;
240
241 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
242 ASSERT_NE(c, (void *)NULL);
243
244 out_frames = cras_fmt_conv_out_frames_to_in(c, buf_size);
245 EXPECT_EQ(buf_size, out_frames);
246
247 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
248 EXPECT_EQ(buf_size, out_frames);
249
250 in_buff = (int32_t *)malloc(buf_size * cras_get_format_bytes(&in_fmt));
251 out_buff = (int32_t *)malloc(buf_size * cras_get_format_bytes(&out_fmt));
252 // TODO(dgreid) - s/0x10000/1/ once it stays full bits the whole way.
253 for (i = 0; i < buf_size; i++) {
254 in_buff[i * 2] = 13450 << 16;
255 in_buff[i * 2 + 1] = -in_buff[i * 2] + 0x10000;
256 }
257 out_frames = cras_fmt_conv_convert_frames(c,
258 (uint8_t *)in_buff,
259 (uint8_t *)out_buff,
260 &in_buf_size,
261 buf_size);
262 EXPECT_EQ(buf_size, out_frames);
263 for (i = 0; i < buf_size; i++) {
264 EXPECT_EQ(0x10000, out_buff[i]);
265 }
266
267 cras_fmt_conv_destroy(&c);
268 free(in_buff);
269 free(out_buff);
270 }
271 }
272
273 // Test 5.1 to Stereo mix.
TEST(FormatConverterTest,SurroundToStereo)274 TEST(FormatConverterTest, SurroundToStereo) {
275 struct cras_fmt_conv *c;
276 struct cras_audio_format in_fmt;
277 struct cras_audio_format out_fmt;
278
279 size_t out_frames;
280 int16_t *in_buff;
281 int16_t *out_buff;
282 unsigned int i;
283 const size_t buf_size = 4096;
284 unsigned int in_buf_size = 4096;
285
286 ResetStub();
287 in_fmt.format = SND_PCM_FORMAT_S16_LE;
288 out_fmt.format = SND_PCM_FORMAT_S16_LE;
289 in_fmt.num_channels = 6;
290 out_fmt.num_channels = 2;
291 in_fmt.frame_rate = 48000;
292 out_fmt.frame_rate = 48000;
293 for (i = 0; i < CRAS_CH_MAX; i++)
294 in_fmt.channel_layout[i] = surround_channel_layout[i];
295
296 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
297 ASSERT_NE(c, (void *)NULL);
298
299 out_frames = cras_fmt_conv_out_frames_to_in(c, buf_size);
300 EXPECT_EQ(buf_size, out_frames);
301
302 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
303 EXPECT_EQ(buf_size, out_frames);
304
305 in_buff = (int16_t *)malloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
306
307 /* Swap channel to FL = 13450, RL = -100.
308 * Assert right channel is silent.
309 */
310 for (i = 0; i < buf_size; i++) {
311 in_buff[i * 6] = 13450;
312 in_buff[i * 6 + 1] = 0;
313 in_buff[i * 6 + 2] = -100;
314 in_buff[i * 6 + 3] = 0;
315 in_buff[i * 6 + 4] = 0;
316 in_buff[i * 6 + 5] = 0;
317 }
318 out_buff = (int16_t *)malloc(buf_size * 2 * cras_get_format_bytes(&out_fmt));
319 out_frames = cras_fmt_conv_convert_frames(c,
320 (uint8_t *)in_buff,
321 (uint8_t *)out_buff,
322 &in_buf_size,
323 buf_size);
324 EXPECT_EQ(buf_size, out_frames);
325 for (i = 0; i < buf_size; i++)
326 EXPECT_LT(0, out_buff[i * 2]);
327 cras_fmt_conv_destroy(&c);
328
329 /* Swap channel to FR = 13450, RR = -100.
330 * Assert left channel is silent.
331 */
332 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_FL, CRAS_CH_FR);
333 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_RL, CRAS_CH_RR);
334 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
335 out_frames = cras_fmt_conv_convert_frames(c,
336 (uint8_t *)in_buff,
337 (uint8_t *)out_buff,
338 &in_buf_size,
339 buf_size);
340 EXPECT_EQ(buf_size, out_frames);
341 for (i = 0; i < buf_size; i++)
342 EXPECT_LT(0, out_buff[i * 2 + 1]);
343 cras_fmt_conv_destroy(&c);
344
345 /* Swap channel to FC = 13450, LFE = -100.
346 * Assert output left and right has equal magnitude.
347 */
348 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_FR, CRAS_CH_FC);
349 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_RR, CRAS_CH_LFE);
350 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
351 out_frames = cras_fmt_conv_convert_frames(c,
352 (uint8_t *)in_buff,
353 (uint8_t *)out_buff,
354 &in_buf_size,
355 buf_size);
356 EXPECT_EQ(buf_size, out_frames);
357 for (i = 0; i < buf_size; i++) {
358 EXPECT_NE(0, out_buff[i * 2]);
359 EXPECT_EQ(out_buff[i * 2], out_buff[i * 2 + 1]);
360 }
361 cras_fmt_conv_destroy(&c);
362
363 /* Swap channel to FR = 13450, FL = -100.
364 * Assert output left is positive and right is negative. */
365 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_LFE, CRAS_CH_FR);
366 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_FC, CRAS_CH_FL);
367 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
368 out_frames = cras_fmt_conv_convert_frames(c,
369 (uint8_t *)in_buff,
370 (uint8_t *)out_buff,
371 &in_buf_size,
372 buf_size);
373 EXPECT_EQ(buf_size, out_frames);
374 for (i = 0; i < buf_size; i++) {
375 EXPECT_LT(0, out_buff[i * 2]);
376 EXPECT_GT(0, out_buff[i * 2 + 1]);
377 }
378
379 cras_fmt_conv_destroy(&c);
380 free(in_buff);
381 free(out_buff);
382 }
383
384 // Test Quad to Stereo mix.
TEST(FormatConverterTest,QuadToStereo)385 TEST(FormatConverterTest, QuadToStereo) {
386 struct cras_fmt_conv *c;
387 struct cras_audio_format in_fmt;
388 struct cras_audio_format out_fmt;
389
390 size_t out_frames;
391 int16_t *in_buff;
392 int16_t *out_buff;
393 unsigned int i;
394 const size_t buf_size = 4096;
395 unsigned int in_buf_size = 4096;
396
397 ResetStub();
398 in_fmt.format = SND_PCM_FORMAT_S16_LE;
399 out_fmt.format = SND_PCM_FORMAT_S16_LE;
400 in_fmt.num_channels = 4;
401 out_fmt.num_channels = 2;
402 in_fmt.frame_rate = 48000;
403 out_fmt.frame_rate = 48000;
404 for (i = 0; i < CRAS_CH_MAX; i++)
405 in_fmt.channel_layout[i] = quad_channel_layout[i];
406
407 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
408 ASSERT_NE(c, (void *)NULL);
409
410 out_frames = cras_fmt_conv_out_frames_to_in(c, buf_size);
411 EXPECT_EQ(buf_size, out_frames);
412
413 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
414 EXPECT_EQ(buf_size, out_frames);
415
416 in_buff = (int16_t *)malloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
417
418 /*
419 * Set left channel positive, right channel negative, assert values are
420 * copied and scaled as expected.
421 */
422 for (i = 0; i < buf_size; i++) {
423 in_buff[i * 4] = 800;
424 in_buff[i * 4 + 1] = -800;
425 in_buff[i * 4 + 2] = 80;
426 in_buff[i * 4 + 3] = -80;
427 }
428 out_buff = (int16_t *)malloc(buf_size * 2 * cras_get_format_bytes(&out_fmt));
429
430 out_frames = cras_fmt_conv_convert_frames(c,
431 (uint8_t *)in_buff,
432 (uint8_t *)out_buff,
433 &in_buf_size,
434 buf_size);
435 EXPECT_EQ(buf_size, out_frames);
436 for (i = 0; i < buf_size; i++) {
437 EXPECT_EQ(820, out_buff[i * 2]);
438 EXPECT_EQ(-820, out_buff[i * 2 + 1]);
439 }
440 cras_fmt_conv_destroy(&c);
441
442 /*
443 * Swap left and right channels, check channel map is respected.
444 */
445 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_FL, CRAS_CH_FR);
446 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_RL, CRAS_CH_RR);
447 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
448 out_frames = cras_fmt_conv_convert_frames(c,
449 (uint8_t *)in_buff,
450 (uint8_t *)out_buff,
451 &in_buf_size,
452 buf_size);
453 EXPECT_EQ(buf_size, out_frames);
454 for (i = 0; i < buf_size; i++) {
455 EXPECT_EQ(-820, out_buff[i * 2]);
456 EXPECT_EQ(820, out_buff[i * 2 + 1]);
457 }
458 cras_fmt_conv_destroy(&c);
459
460 /*
461 * Swap front and rear, check channel map is respected.
462 */
463 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_FR, CRAS_CH_RR);
464 swap_channel_layout(in_fmt.channel_layout, CRAS_CH_FL, CRAS_CH_RL);
465 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
466 out_frames = cras_fmt_conv_convert_frames(c,
467 (uint8_t *)in_buff,
468 (uint8_t *)out_buff,
469 &in_buf_size,
470 buf_size);
471 EXPECT_EQ(buf_size, out_frames);
472 for (i = 0; i < buf_size; i++) {
473 EXPECT_EQ(-280, out_buff[i * 2]);
474 EXPECT_EQ(280, out_buff[i * 2 + 1]);
475 }
476 cras_fmt_conv_destroy(&c);
477
478 /*
479 * Empty channel map, check default behavior is applied.
480 */
481 for (i = 0; i < CRAS_CH_MAX; i++)
482 in_fmt.channel_layout[i] = -1;
483 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
484 out_frames = cras_fmt_conv_convert_frames(c,
485 (uint8_t *)in_buff,
486 (uint8_t *)out_buff,
487 &in_buf_size,
488 buf_size);
489 EXPECT_EQ(buf_size, out_frames);
490 for (i = 0; i < buf_size; i++) {
491 EXPECT_EQ(820, out_buff[i * 2]);
492 EXPECT_EQ(-820, out_buff[i * 2 + 1]);
493 }
494 cras_fmt_conv_destroy(&c);
495
496 free(in_buff);
497 free(out_buff);
498 }
499
500 // Test 2 to 1 SRC.
TEST(FormatConverterTest,Convert2To1)501 TEST(FormatConverterTest, Convert2To1) {
502 struct cras_fmt_conv *c;
503 struct cras_audio_format in_fmt;
504 struct cras_audio_format out_fmt;
505
506 size_t out_frames;
507 int16_t *in_buff;
508 int16_t *out_buff;
509 const size_t buf_size = 4096;
510 unsigned int in_buf_size = 4096;
511
512 ResetStub();
513 in_fmt.format = out_fmt.format = SND_PCM_FORMAT_S16_LE;
514 in_fmt.num_channels = out_fmt.num_channels = 2;
515 in_fmt.frame_rate = 96000;
516 out_fmt.frame_rate = 48000;
517
518 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
519 ASSERT_NE(c, (void *)NULL);
520
521 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
522 EXPECT_EQ(buf_size/2, out_frames);
523
524 in_buff = (int16_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
525 out_buff = (int16_t *)ralloc(buf_size/2 * cras_get_format_bytes(&out_fmt));
526 out_frames = cras_fmt_conv_convert_frames(c,
527 (uint8_t *)in_buff,
528 (uint8_t *)out_buff,
529 &in_buf_size,
530 buf_size / 2);
531 cras_fmt_conv_destroy(&c);
532 free(in_buff);
533 free(out_buff);
534 }
535
536 // Test 1 to 2 SRC.
TEST(FormatConverterTest,Convert1To2)537 TEST(FormatConverterTest, Convert1To2) {
538 struct cras_fmt_conv *c;
539 struct cras_audio_format in_fmt;
540 struct cras_audio_format out_fmt;
541 size_t out_frames;
542 int16_t *in_buff;
543 int16_t *out_buff;
544 const size_t buf_size = 4096;
545 unsigned int in_buf_size = 4096;
546
547 ResetStub();
548 in_fmt.format = out_fmt.format = SND_PCM_FORMAT_S16_LE;
549 in_fmt.num_channels = out_fmt.num_channels = 2;
550 in_fmt.frame_rate = 22050;
551 out_fmt.frame_rate = 44100;
552
553 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
554 ASSERT_NE(c, (void *)NULL);
555
556 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
557 EXPECT_EQ(buf_size*2, out_frames);
558
559 in_buff = (int16_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
560 out_buff = (int16_t *)ralloc(buf_size*2 * cras_get_format_bytes(&out_fmt));
561 out_frames = cras_fmt_conv_convert_frames(c,
562 (uint8_t *)in_buff,
563 (uint8_t *)out_buff,
564 &in_buf_size,
565 buf_size * 2);
566 cras_fmt_conv_destroy(&c);
567 free(in_buff);
568 free(out_buff);
569 }
570
571 // Test 1 to 2 SRC with mono to stereo conversion.
TEST(FormatConverterTest,Convert1To2MonoToStereo)572 TEST(FormatConverterTest, Convert1To2MonoToStereo) {
573 struct cras_fmt_conv *c;
574 struct cras_audio_format in_fmt;
575 struct cras_audio_format out_fmt;
576 size_t out_frames;
577 int16_t *in_buff;
578 int16_t *out_buff;
579 const size_t buf_size = 4096;
580 unsigned int in_buf_size = 4096;
581
582 ResetStub();
583 in_fmt.format = out_fmt.format = SND_PCM_FORMAT_S16_LE;
584 in_fmt.num_channels = 1;
585 out_fmt.num_channels = 2;
586 in_fmt.frame_rate = 22050;
587 out_fmt.frame_rate = 44100;
588
589 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
590 ASSERT_NE(c, (void *)NULL);
591
592 out_frames = cras_fmt_conv_out_frames_to_in(c, buf_size);
593 EXPECT_EQ(buf_size / 2, out_frames);
594
595 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
596 EXPECT_EQ(buf_size * 2, out_frames);
597
598 in_buff = (int16_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
599 out_buff = (int16_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&out_fmt));
600 out_frames = cras_fmt_conv_convert_frames(c,
601 (uint8_t *)in_buff,
602 (uint8_t *)out_buff,
603 &in_buf_size,
604 buf_size * 2);
605 cras_fmt_conv_destroy(&c);
606 free(in_buff);
607 free(out_buff);
608 }
609
610 // Test 32 to 16 bit conversion.
TEST(FormatConverterTest,ConvertS32LEToS16LE)611 TEST(FormatConverterTest, ConvertS32LEToS16LE) {
612 struct cras_fmt_conv *c;
613 struct cras_audio_format in_fmt;
614 struct cras_audio_format out_fmt;
615
616 size_t out_frames;
617 int32_t *in_buff;
618 int16_t *out_buff;
619 const size_t buf_size = 4096;
620 unsigned int in_buf_size = 4096;
621
622 ResetStub();
623 in_fmt.format = SND_PCM_FORMAT_S32_LE;
624 out_fmt.format = SND_PCM_FORMAT_S16_LE;
625 in_fmt.num_channels = out_fmt.num_channels = 2;
626 in_fmt.frame_rate = 48000;
627 out_fmt.frame_rate = 48000;
628
629 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
630 ASSERT_NE(c, (void *)NULL);
631
632 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
633 EXPECT_EQ(buf_size, out_frames);
634
635 in_buff = (int32_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
636 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
637 out_frames = cras_fmt_conv_convert_frames(c,
638 (uint8_t *)in_buff,
639 (uint8_t *)out_buff,
640 &in_buf_size,
641 buf_size);
642 EXPECT_EQ(buf_size, out_frames);
643 for (unsigned int i = 0; i < buf_size; i++)
644 EXPECT_EQ((int16_t)(in_buff[i] >> 16), out_buff[i]);
645
646 cras_fmt_conv_destroy(&c);
647 free(in_buff);
648 free(out_buff);
649 }
650
651 // Test 24 to 16 bit conversion.
TEST(FormatConverterTest,ConvertS24LEToS16LE)652 TEST(FormatConverterTest, ConvertS24LEToS16LE) {
653 struct cras_fmt_conv *c;
654 struct cras_audio_format in_fmt;
655 struct cras_audio_format out_fmt;
656
657 size_t out_frames;
658 int32_t *in_buff;
659 int16_t *out_buff;
660 const size_t buf_size = 4096;
661 unsigned int in_buf_size = 4096;
662
663 ResetStub();
664 in_fmt.format = SND_PCM_FORMAT_S24_LE;
665 out_fmt.format = SND_PCM_FORMAT_S16_LE;
666 in_fmt.num_channels = out_fmt.num_channels = 2;
667 in_fmt.frame_rate = 48000;
668 out_fmt.frame_rate = 48000;
669
670 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
671 ASSERT_NE(c, (void *)NULL);
672
673 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
674 EXPECT_EQ(buf_size, out_frames);
675
676 in_buff = (int32_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
677 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
678 out_frames = cras_fmt_conv_convert_frames(c,
679 (uint8_t *)in_buff,
680 (uint8_t *)out_buff,
681 &in_buf_size,
682 buf_size);
683 EXPECT_EQ(buf_size, out_frames);
684 for (unsigned int i = 0; i < buf_size; i++)
685 EXPECT_EQ((int16_t)(in_buff[i] >> 8), out_buff[i]);
686
687 cras_fmt_conv_destroy(&c);
688 free(in_buff);
689 free(out_buff);
690 }
691
692 // Test 8 to 16 bit conversion.
TEST(FormatConverterTest,ConvertU8LEToS16LE)693 TEST(FormatConverterTest, ConvertU8LEToS16LE) {
694 struct cras_fmt_conv *c;
695 struct cras_audio_format in_fmt;
696 struct cras_audio_format out_fmt;
697
698 size_t out_frames;
699 uint8_t *in_buff;
700 int16_t *out_buff;
701 const size_t buf_size = 4096;
702 unsigned int in_buf_size = 4096;
703
704 ResetStub();
705 in_fmt.format = SND_PCM_FORMAT_U8;
706 out_fmt.format = SND_PCM_FORMAT_S16_LE;
707 in_fmt.num_channels = 2;
708 out_fmt.num_channels = 2;
709 in_fmt.frame_rate = 48000;
710 out_fmt.frame_rate = 48000;
711
712 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
713 ASSERT_NE(c, (void *)NULL);
714
715 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
716 EXPECT_EQ(buf_size, out_frames);
717
718 in_buff = (uint8_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
719 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
720 out_frames = cras_fmt_conv_convert_frames(c,
721 (uint8_t *)in_buff,
722 (uint8_t *)out_buff,
723 &in_buf_size,
724 buf_size);
725 EXPECT_EQ(buf_size, out_frames);
726 for (unsigned int i = 0; i < buf_size; i++)
727 EXPECT_EQ((int16_t)((uint16_t)((int16_t)(in_buff[i]) - 128) << 8),
728 out_buff[i]);
729
730 cras_fmt_conv_destroy(&c);
731 free(in_buff);
732 free(out_buff);
733 }
734
735 // Test 16 to 32 bit conversion.
TEST(FormatConverterTest,ConvertS16LEToS32LE)736 TEST(FormatConverterTest, ConvertS16LEToS32LE) {
737 struct cras_fmt_conv *c;
738 struct cras_audio_format in_fmt;
739 struct cras_audio_format out_fmt;
740
741 size_t out_frames;
742 int16_t *in_buff;
743 int32_t *out_buff;
744 const size_t buf_size = 4096;
745 unsigned int in_buf_size = 4096;
746
747 ResetStub();
748 in_fmt.format = SND_PCM_FORMAT_S16_LE;
749 out_fmt.format = SND_PCM_FORMAT_S32_LE;
750 in_fmt.num_channels = out_fmt.num_channels = 2;
751 in_fmt.frame_rate = 48000;
752 out_fmt.frame_rate = 48000;
753
754 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
755 ASSERT_NE(c, (void *)NULL);
756
757 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
758 EXPECT_EQ(buf_size, out_frames);
759
760 in_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
761 out_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
762 out_frames = cras_fmt_conv_convert_frames(c,
763 (uint8_t *)in_buff,
764 (uint8_t *)out_buff,
765 &in_buf_size,
766 buf_size);
767 EXPECT_EQ(buf_size, out_frames);
768 for (unsigned int i = 0; i < buf_size; i++)
769 EXPECT_EQ((int32_t)((uint32_t)(int32_t)in_buff[i] << 16), out_buff[i]);
770
771 cras_fmt_conv_destroy(&c);
772 free(in_buff);
773 free(out_buff);
774 }
775
776 // Test 16 to 24 bit conversion.
TEST(FormatConverterTest,ConvertS16LEToS24LE)777 TEST(FormatConverterTest, ConvertS16LEToS24LE) {
778 struct cras_fmt_conv *c;
779 struct cras_audio_format in_fmt;
780 struct cras_audio_format out_fmt;
781
782 size_t out_frames;
783 int16_t *in_buff;
784 int32_t *out_buff;
785 const size_t buf_size = 4096;
786 unsigned int in_buf_size = 4096;
787
788 ResetStub();
789 in_fmt.format = SND_PCM_FORMAT_S16_LE;
790 out_fmt.format = SND_PCM_FORMAT_S24_LE;
791 in_fmt.num_channels = out_fmt.num_channels = 2;
792 in_fmt.frame_rate = 48000;
793 out_fmt.frame_rate = 48000;
794
795 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
796 ASSERT_NE(c, (void *)NULL);
797
798 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
799 EXPECT_EQ(buf_size, out_frames);
800
801 in_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
802 out_buff = (int32_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&out_fmt));
803 out_frames = cras_fmt_conv_convert_frames(c,
804 (uint8_t *)in_buff,
805 (uint8_t *)out_buff,
806 &in_buf_size,
807 buf_size);
808 EXPECT_EQ(buf_size, out_frames);
809 for (unsigned int i = 0; i < buf_size; i++)
810 EXPECT_EQ((int32_t)((uint32_t)(int32_t)in_buff[i] << 8), out_buff[i]);
811
812 cras_fmt_conv_destroy(&c);
813 free(in_buff);
814 free(out_buff);
815 }
816
817 // Test 16 to 8 bit conversion.
TEST(FormatConverterTest,ConvertS16LEToU8)818 TEST(FormatConverterTest, ConvertS16LEToU8) {
819 struct cras_fmt_conv *c;
820 struct cras_audio_format in_fmt;
821 struct cras_audio_format out_fmt;
822
823 size_t out_frames;
824 int16_t *in_buff;
825 uint8_t *out_buff;
826 const size_t buf_size = 4096;
827 unsigned int in_buf_size = 4096;
828
829 ResetStub();
830 in_fmt.format = SND_PCM_FORMAT_S16_LE;
831 out_fmt.format = SND_PCM_FORMAT_U8;
832 in_fmt.num_channels = 2;
833 out_fmt.num_channels = 2;
834 in_fmt.frame_rate = 48000;
835 out_fmt.frame_rate = 48000;
836
837 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
838 ASSERT_NE(c, (void *)NULL);
839
840 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
841 EXPECT_EQ(buf_size, out_frames);
842
843 in_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
844 out_buff = (uint8_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
845 out_frames = cras_fmt_conv_convert_frames(c,
846 (uint8_t *)in_buff,
847 (uint8_t *)out_buff,
848 &in_buf_size,
849 buf_size);
850 EXPECT_EQ(buf_size, out_frames);
851 for (unsigned int i = 0; i < buf_size; i++)
852 EXPECT_EQ((in_buff[i] >> 8) + 128, out_buff[i]);
853
854 cras_fmt_conv_destroy(&c);
855 free(in_buff);
856 free(out_buff);
857 }
858
859 // Test 32 bit 5.1 to 16 bit stereo conversion.
TEST(FormatConverterTest,ConvertS32LEToS16LEDownmix51ToStereo)860 TEST(FormatConverterTest, ConvertS32LEToS16LEDownmix51ToStereo) {
861 struct cras_fmt_conv *c;
862 struct cras_audio_format in_fmt;
863 struct cras_audio_format out_fmt;
864
865 size_t out_frames;
866 int32_t *in_buff;
867 int16_t *out_buff;
868 const size_t buf_size = 4096;
869 unsigned int in_buf_size = 4096;
870 int i;
871
872 ResetStub();
873 in_fmt.format = SND_PCM_FORMAT_S32_LE;
874 out_fmt.format = SND_PCM_FORMAT_S16_LE;
875 in_fmt.num_channels = 6;
876 out_fmt.num_channels = 2;
877 in_fmt.frame_rate = 48000;
878 out_fmt.frame_rate = 48000;
879 for (i = 0; i < CRAS_CH_MAX; i++)
880 in_fmt.channel_layout[i] = surround_channel_layout[i];
881
882 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
883 ASSERT_NE(c, (void *)NULL);
884
885 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
886 EXPECT_EQ(buf_size, out_frames);
887
888 in_buff = (int32_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&in_fmt));
889 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
890 out_frames = cras_fmt_conv_convert_frames(c,
891 (uint8_t *)in_buff,
892 (uint8_t *)out_buff,
893 &in_buf_size,
894 buf_size);
895 EXPECT_EQ(buf_size, out_frames);
896
897 cras_fmt_conv_destroy(&c);
898 free(in_buff);
899 free(out_buff);
900 }
901
902 // Test 16 bit stereo to 5.1 conversion.
TEST(FormatConverterTest,ConvertS16LEToS16LEStereoTo51)903 TEST(FormatConverterTest, ConvertS16LEToS16LEStereoTo51) {
904 struct cras_fmt_conv *c;
905 struct cras_audio_format in_fmt;
906 struct cras_audio_format out_fmt;
907
908 size_t out_frames;
909 int16_t *in_buff;
910 int16_t *out_buff;
911 const size_t buf_size = 4096;
912 unsigned int in_buf_size = 4096;
913 int i;
914
915 ResetStub();
916 in_fmt.format = SND_PCM_FORMAT_S16_LE;
917 out_fmt.format = SND_PCM_FORMAT_S16_LE;
918 in_fmt.num_channels = 2;
919 out_fmt.num_channels = 6;
920 in_fmt.frame_rate = 48000;
921 out_fmt.frame_rate = 48000;
922 for (i = 0; i < CRAS_CH_MAX; i++)
923 out_fmt.channel_layout[i] = surround_channel_layout[i];
924
925 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
926 ASSERT_NE(c, (void *)NULL);
927
928 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
929 EXPECT_EQ(buf_size, out_frames);
930
931 in_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
932 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
933 out_frames = cras_fmt_conv_convert_frames(c,
934 (uint8_t *)in_buff,
935 (uint8_t *)out_buff,
936 &in_buf_size,
937 buf_size);
938 EXPECT_EQ(buf_size, out_frames);
939 for (unsigned int i = 0; i < buf_size; i++) {
940 /* Check mono be converted to CRAS_CH_FL and CRAS_CH_FR */
941 EXPECT_EQ(in_buff[2 * i], out_buff[6 * i]);
942 EXPECT_EQ(in_buff[2 * i + 1], out_buff[6 * i + 1]);
943 EXPECT_EQ(0, out_buff[6 * i + 2]);
944 EXPECT_EQ(0, out_buff[6 * i + 3]);
945 EXPECT_EQ(0, out_buff[6 * i + 4]);
946 EXPECT_EQ(0, out_buff[6 * i + 5]);
947 }
948
949 cras_fmt_conv_destroy(&c);
950 free(in_buff);
951 free(out_buff);
952 }
953
954 // Test 16 bit mono to 5.1 conversion.
TEST(FormatConverterTest,ConvertS16LEToS16LEMonoTo51)955 TEST(FormatConverterTest, ConvertS16LEToS16LEMonoTo51) {
956 struct cras_fmt_conv *c;
957 struct cras_audio_format in_fmt;
958 struct cras_audio_format out_fmt;
959
960 size_t out_frames;
961 int16_t *in_buff;
962 int16_t *out_buff;
963 const size_t buf_size = 4096;
964 unsigned int in_buf_size = 4096;
965 int i;
966
967 ResetStub();
968 in_fmt.format = SND_PCM_FORMAT_S16_LE;
969 out_fmt.format = SND_PCM_FORMAT_S16_LE;
970 in_fmt.num_channels = 1;
971 out_fmt.num_channels = 6;
972 in_fmt.frame_rate = 48000;
973 out_fmt.frame_rate = 48000;
974 for (i = 0; i < CRAS_CH_MAX; i++)
975 out_fmt.channel_layout[i] = surround_channel_layout[i];
976
977 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
978 ASSERT_NE(c, (void *)NULL);
979
980 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
981 EXPECT_EQ(buf_size, out_frames);
982
983 in_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
984 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
985 out_frames = cras_fmt_conv_convert_frames(c,
986 (uint8_t *)in_buff,
987 (uint8_t *)out_buff,
988 &in_buf_size,
989 buf_size);
990 EXPECT_EQ(buf_size, out_frames);
991 for (unsigned int i = 0; i < buf_size; i++) {
992 /* Check mono be converted to CRAS_CH_FC */
993 EXPECT_EQ(in_buff[i], out_buff[6 * i + 4]);
994 EXPECT_EQ(0, out_buff[6 * i + 0]);
995 EXPECT_EQ(0, out_buff[6 * i + 1]);
996 EXPECT_EQ(0, out_buff[6 * i + 2]);
997 EXPECT_EQ(0, out_buff[6 * i + 3]);
998 EXPECT_EQ(0, out_buff[6 * i + 5]);
999 }
1000
1001 cras_fmt_conv_destroy(&c);
1002 free(in_buff);
1003 free(out_buff);
1004 }
1005
1006
1007 // Test 16 bit stereo to quad conversion.
TEST(FormatConverterTest,ConvertS16LEToS16LEStereoToQuad)1008 TEST(FormatConverterTest, ConvertS16LEToS16LEStereoToQuad) {
1009 struct cras_fmt_conv *c;
1010 struct cras_audio_format in_fmt;
1011 struct cras_audio_format out_fmt;
1012
1013 size_t out_frames;
1014 int16_t *in_buff;
1015 int16_t *out_buff;
1016 const size_t buf_size = 4096;
1017 unsigned int in_buf_size = 4096;
1018
1019 ResetStub();
1020 in_fmt.format = SND_PCM_FORMAT_S16_LE;
1021 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1022 in_fmt.num_channels = 2;
1023 out_fmt.num_channels = 4;
1024 in_fmt.frame_rate = 48000;
1025 out_fmt.frame_rate = 48000;
1026 for (unsigned int i = 0; i < CRAS_CH_MAX; i++)
1027 out_fmt.channel_layout[i] = quad_channel_layout[i];
1028
1029 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
1030 ASSERT_NE(c, (void *)NULL);
1031
1032 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1033 EXPECT_EQ(buf_size, out_frames);
1034
1035 in_buff = (int16_t *)malloc(buf_size * cras_get_format_bytes(&in_fmt));
1036 for (unsigned int i = 0; i < in_buf_size; i++) {
1037 in_buff[i * 2] = 40;
1038 in_buff[i * 2 + 1] = 80;
1039 }
1040
1041 out_buff = (int16_t *)malloc(buf_size * cras_get_format_bytes(&out_fmt));
1042 out_frames = cras_fmt_conv_convert_frames(c,
1043 (uint8_t *)in_buff,
1044 (uint8_t *)out_buff,
1045 &in_buf_size,
1046 buf_size);
1047 EXPECT_EQ(buf_size, out_frames);
1048 for (unsigned int i = 0; i < buf_size; i++) {
1049 EXPECT_EQ(40, out_buff[4 * i]);
1050 EXPECT_EQ(80, out_buff[4 * i + 1]);
1051 EXPECT_EQ(40, out_buff[4 * i + 2]);
1052 EXPECT_EQ(80, out_buff[4 * i + 3]);
1053 }
1054 cras_fmt_conv_destroy(&c);
1055
1056 // Swap channels and check channel layout is respected.
1057 swap_channel_layout(out_fmt.channel_layout, CRAS_CH_FL, CRAS_CH_RR);
1058 swap_channel_layout(out_fmt.channel_layout, CRAS_CH_RL, CRAS_CH_FR);
1059 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
1060 out_frames = cras_fmt_conv_convert_frames(c,
1061 (uint8_t *)in_buff,
1062 (uint8_t *)out_buff,
1063 &in_buf_size,
1064 buf_size);
1065 EXPECT_EQ(buf_size, out_frames);
1066 for (unsigned int i = 0; i < buf_size; i++) {
1067 EXPECT_EQ(80, out_buff[4 * i]);
1068 EXPECT_EQ(40, out_buff[4 * i + 1]);
1069 EXPECT_EQ(80, out_buff[4 * i + 2]);
1070 EXPECT_EQ(40, out_buff[4 * i + 3]);
1071 }
1072
1073 cras_fmt_conv_destroy(&c);
1074 free(in_buff);
1075 free(out_buff);
1076 }
1077
1078 // Test 32 bit 5.1 to 16 bit stereo conversion with SRC 1 to 2.
TEST(FormatConverterTest,ConvertS32LEToS16LEDownmix51ToStereo48To96)1079 TEST(FormatConverterTest, ConvertS32LEToS16LEDownmix51ToStereo48To96) {
1080 struct cras_fmt_conv *c;
1081 struct cras_audio_format in_fmt;
1082 struct cras_audio_format out_fmt;
1083
1084 size_t out_frames;
1085 int32_t *in_buff;
1086 int16_t *out_buff;
1087 const size_t buf_size = 4096;
1088 unsigned int in_buf_size = 4096;
1089 int i;
1090
1091 ResetStub();
1092 in_fmt.format = SND_PCM_FORMAT_S32_LE;
1093 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1094 in_fmt.num_channels = 6;
1095 out_fmt.num_channels = 2;
1096 in_fmt.frame_rate = 48000;
1097 out_fmt.frame_rate = 96000;
1098 for (i = 0; i < CRAS_CH_MAX; i++)
1099 in_fmt.channel_layout[i] = surround_channel_layout[i];
1100
1101 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
1102 ASSERT_NE(c, (void *)NULL);
1103
1104 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1105 EXPECT_EQ(buf_size * 2, out_frames);
1106
1107 in_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
1108 out_buff = (int16_t *)ralloc(buf_size * 2 * cras_get_format_bytes(&out_fmt));
1109 out_frames = cras_fmt_conv_convert_frames(c,
1110 (uint8_t *)in_buff,
1111 (uint8_t *)out_buff,
1112 &in_buf_size,
1113 buf_size * 2);
1114 EXPECT_EQ(buf_size * 2, out_frames);
1115
1116 cras_fmt_conv_destroy(&c);
1117 free(in_buff);
1118 free(out_buff);
1119 }
1120
1121 // Test 32 bit 5.1 to 16 bit stereo conversion with SRC 2 to 1.
TEST(FormatConverterTest,ConvertS32LEToS16LEDownmix51ToStereo96To48)1122 TEST(FormatConverterTest, ConvertS32LEToS16LEDownmix51ToStereo96To48) {
1123 struct cras_fmt_conv *c;
1124 struct cras_audio_format in_fmt;
1125 struct cras_audio_format out_fmt;
1126
1127 size_t out_frames;
1128 int32_t *in_buff;
1129 int16_t *out_buff;
1130 const size_t buf_size = 4096;
1131 unsigned int in_buf_size = 4096;
1132 int i;
1133
1134 ResetStub();
1135 in_fmt.format = SND_PCM_FORMAT_S32_LE;
1136 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1137 in_fmt.num_channels = 6;
1138 out_fmt.num_channels = 2;
1139 in_fmt.frame_rate = 96000;
1140 out_fmt.frame_rate = 48000;
1141 for (i = 0; i < CRAS_CH_MAX; i++)
1142 in_fmt.channel_layout[i] = surround_channel_layout[i];
1143
1144 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
1145 ASSERT_NE(c, (void *)NULL);
1146
1147 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1148 EXPECT_EQ(buf_size / 2, out_frames);
1149
1150 in_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
1151 out_buff = (int16_t *)ralloc(buf_size / 2 * cras_get_format_bytes(&out_fmt));
1152 out_frames = cras_fmt_conv_convert_frames(c,
1153 (uint8_t *)in_buff,
1154 (uint8_t *)out_buff,
1155 &in_buf_size,
1156 buf_size / 2);
1157 EXPECT_EQ(buf_size / 2, out_frames);
1158
1159 cras_fmt_conv_destroy(&c);
1160 free(in_buff);
1161 free(out_buff);
1162 }
1163
1164 // Test 32 bit 5.1 to 16 bit stereo conversion with SRC 48 to 44.1.
TEST(FormatConverterTest,ConvertS32LEToS16LEDownmix51ToStereo48To441)1165 TEST(FormatConverterTest, ConvertS32LEToS16LEDownmix51ToStereo48To441) {
1166 struct cras_fmt_conv *c;
1167 struct cras_audio_format in_fmt;
1168 struct cras_audio_format out_fmt;
1169
1170 size_t out_frames;
1171 size_t ret_frames;
1172 int32_t *in_buff;
1173 int16_t *out_buff;
1174 const size_t buf_size = 4096;
1175 unsigned int in_buf_size = 4096;
1176 int i;
1177
1178 ResetStub();
1179 in_fmt.format = SND_PCM_FORMAT_S32_LE;
1180 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1181 in_fmt.num_channels = 6;
1182 out_fmt.num_channels = 2;
1183 in_fmt.frame_rate = 48000;
1184 out_fmt.frame_rate = 44100;
1185 for (i = 0; i < CRAS_CH_MAX; i++)
1186 in_fmt.channel_layout[i] = surround_channel_layout[i];
1187
1188 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
1189 ASSERT_NE(c, (void *)NULL);
1190
1191 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1192 EXPECT_LT(out_frames, buf_size);
1193
1194 in_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
1195 out_buff = (int16_t *)ralloc(out_frames * cras_get_format_bytes(&out_fmt));
1196 ret_frames = cras_fmt_conv_convert_frames(c,
1197 (uint8_t *)in_buff,
1198 (uint8_t *)out_buff,
1199 &in_buf_size,
1200 out_frames);
1201 EXPECT_EQ(out_frames, ret_frames);
1202
1203 cras_fmt_conv_destroy(&c);
1204 free(in_buff);
1205 free(out_buff);
1206 }
1207
1208 // Test 32 bit 5.1 to 16 bit stereo conversion with SRC 441 to 48.
TEST(FormatConverterTest,ConvertS32LEToS16LEDownmix51ToStereo441To48)1209 TEST(FormatConverterTest, ConvertS32LEToS16LEDownmix51ToStereo441To48) {
1210 struct cras_fmt_conv *c;
1211 struct cras_audio_format in_fmt;
1212 struct cras_audio_format out_fmt;
1213
1214 size_t out_frames;
1215 size_t ret_frames;
1216 int32_t *in_buff;
1217 int16_t *out_buff;
1218 const size_t buf_size = 4096;
1219 unsigned int in_buf_size = 4096;
1220 int i;
1221
1222 ResetStub();
1223 in_fmt.format = SND_PCM_FORMAT_S32_LE;
1224 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1225 in_fmt.num_channels = 6;
1226 out_fmt.num_channels = 2;
1227 in_fmt.frame_rate = 44100;
1228 out_fmt.frame_rate = 48000;
1229 for (i = 0; i < CRAS_CH_MAX; i++)
1230 in_fmt.channel_layout[i] = surround_channel_layout[i];
1231
1232 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
1233 ASSERT_NE(c, (void *)NULL);
1234
1235 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1236 EXPECT_GT(out_frames, buf_size);
1237
1238 in_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
1239 out_buff = (int16_t *)ralloc((out_frames - 1) *
1240 cras_get_format_bytes(&out_fmt));
1241 ret_frames = cras_fmt_conv_convert_frames(c,
1242 (uint8_t *)in_buff,
1243 (uint8_t *)out_buff,
1244 &in_buf_size,
1245 out_frames - 1);
1246 EXPECT_EQ(out_frames - 1, ret_frames);
1247
1248 cras_fmt_conv_destroy(&c);
1249 free(in_buff);
1250 free(out_buff);
1251 }
1252
1253 // Test Invalid buffer length just truncates.
TEST(FormatConverterTest,ConvertS32LEToS16LEDownmix51ToStereo96To48Short)1254 TEST(FormatConverterTest, ConvertS32LEToS16LEDownmix51ToStereo96To48Short) {
1255 struct cras_fmt_conv *c;
1256 struct cras_audio_format in_fmt;
1257 struct cras_audio_format out_fmt;
1258
1259 size_t out_frames;
1260 size_t ret_frames;
1261 int32_t *in_buff;
1262 int16_t *out_buff;
1263 const size_t buf_size = 4096;
1264 unsigned int in_buf_size = 4096;
1265 int i;
1266
1267 ResetStub();
1268 in_fmt.format = SND_PCM_FORMAT_S32_LE;
1269 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1270 in_fmt.num_channels = 6;
1271 out_fmt.num_channels = 2;
1272 in_fmt.frame_rate = 96000;
1273 out_fmt.frame_rate = 48000;
1274 for (i = 0; i < CRAS_CH_MAX; i++)
1275 in_fmt.channel_layout[i] = surround_channel_layout[i];
1276
1277 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size, 0);
1278 ASSERT_NE(c, (void *)NULL);
1279
1280 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1281 EXPECT_EQ(buf_size / 2, out_frames);
1282
1283 in_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
1284 out_buff = (int16_t *)ralloc((out_frames - 2) *
1285 cras_get_format_bytes(&out_fmt));
1286 ret_frames = cras_fmt_conv_convert_frames(c,
1287 (uint8_t *)in_buff,
1288 (uint8_t *)out_buff,
1289 &in_buf_size,
1290 out_frames - 2);
1291 EXPECT_EQ(out_frames - 2, ret_frames);
1292
1293 cras_fmt_conv_destroy(&c);
1294 free(in_buff);
1295 free(out_buff);
1296 }
1297
1298 // Test format convert pre linear resample and then follows SRC from 96 to 48.
TEST(FormatConverterTest,Convert96to48PreLinearResample)1299 TEST(FormatConverterTest, Convert96to48PreLinearResample) {
1300 struct cras_fmt_conv *c;
1301 struct cras_audio_format in_fmt;
1302 struct cras_audio_format out_fmt;
1303
1304 size_t out_frames;
1305 int32_t *in_buff;
1306 int16_t *out_buff;
1307 const size_t buf_size = 4096;
1308 unsigned int in_buf_size = 4096;
1309 unsigned int expected_fr;
1310 int i;
1311
1312 ResetStub();
1313 in_fmt.format = SND_PCM_FORMAT_S16_LE;
1314 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1315 in_fmt.num_channels = 2;
1316 out_fmt.num_channels = 2;
1317 in_fmt.frame_rate = 96000;
1318 out_fmt.frame_rate = 48000;
1319 for (i = 0; i < CRAS_CH_MAX; i++) {
1320 in_fmt.channel_layout[i] = surround_channel_layout[i];
1321 out_fmt.channel_layout[i] = surround_channel_layout[i];
1322 }
1323
1324 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size * 2, 1);
1325 ASSERT_NE(c, (void *)NULL);
1326
1327 linear_resampler_needed_val = 1;
1328 linear_resampler_ratio = 1.01;
1329 expected_fr = buf_size / 2 * linear_resampler_ratio;
1330 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1331 EXPECT_EQ(expected_fr, out_frames);
1332
1333 in_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
1334 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
1335 out_frames = cras_fmt_conv_convert_frames(c,
1336 (uint8_t *)in_buff,
1337 (uint8_t *)out_buff,
1338 &in_buf_size,
1339 out_frames);
1340 EXPECT_EQ(expected_fr, out_frames);
1341
1342 cras_fmt_conv_destroy(&c);
1343 free(in_buff);
1344 free(out_buff);
1345 }
1346
1347 // Test format convert SRC from 96 to 48 and then post linear resample.
TEST(FormatConverterTest,Convert96to48PostLinearResample)1348 TEST(FormatConverterTest, Convert96to48PostLinearResample) {
1349 struct cras_fmt_conv *c;
1350 struct cras_audio_format in_fmt;
1351 struct cras_audio_format out_fmt;
1352
1353 size_t out_frames;
1354 int32_t *in_buff;
1355 int16_t *out_buff;
1356 const size_t buf_size = 4096;
1357 unsigned int in_buf_size = 4096;
1358 unsigned int expected_fr;
1359 int i;
1360
1361 ResetStub();
1362 in_fmt.format = SND_PCM_FORMAT_S16_LE;
1363 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1364 in_fmt.num_channels = 2;
1365 out_fmt.num_channels = 2;
1366 in_fmt.frame_rate = 96000;
1367 out_fmt.frame_rate = 48000;
1368 for (i = 0; i < CRAS_CH_MAX; i++) {
1369 in_fmt.channel_layout[i] = surround_channel_layout[i];
1370 out_fmt.channel_layout[i] = surround_channel_layout[i];
1371 }
1372
1373 c = cras_fmt_conv_create(&in_fmt, &out_fmt, buf_size * 2, 0);
1374 ASSERT_NE(c, (void *)NULL);
1375
1376 linear_resampler_needed_val = 1;
1377 linear_resampler_ratio = 0.99;
1378 expected_fr = buf_size / 2 * linear_resampler_ratio;
1379 out_frames = cras_fmt_conv_in_frames_to_out(c, buf_size);
1380 EXPECT_EQ(expected_fr, out_frames);
1381
1382 in_buff = (int32_t *)ralloc(buf_size * cras_get_format_bytes(&in_fmt));
1383 out_buff = (int16_t *)ralloc(buf_size * cras_get_format_bytes(&out_fmt));
1384 out_frames = cras_fmt_conv_convert_frames(c,
1385 (uint8_t *)in_buff,
1386 (uint8_t *)out_buff,
1387 &in_buf_size,
1388 buf_size);
1389 EXPECT_EQ(expected_fr, out_frames);
1390
1391 cras_fmt_conv_destroy(&c);
1392 free(in_buff);
1393 free(out_buff);
1394 }
1395
1396 // Test format converter created in config_format_converter
TEST(FormatConverterTest,ConfigConverter)1397 TEST(FormatConverterTest, ConfigConverter) {
1398 int i;
1399 struct cras_fmt_conv *c = NULL;
1400 struct cras_audio_format in_fmt;
1401 struct cras_audio_format out_fmt;
1402
1403 ResetStub();
1404 in_fmt.format = SND_PCM_FORMAT_S16_LE;
1405 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1406 in_fmt.num_channels = 1;
1407 out_fmt.num_channels = 2;
1408 in_fmt.frame_rate = 96000;
1409 out_fmt.frame_rate = 48000;
1410 for (i = 0; i < CRAS_CH_MAX; i++) {
1411 in_fmt.channel_layout[i] = mono_channel_layout[i];
1412 out_fmt.channel_layout[i] = stereo_channel_layout[i];
1413 }
1414
1415 config_format_converter(&c, CRAS_STREAM_OUTPUT, &in_fmt, &out_fmt, 4096);
1416 ASSERT_NE(c, (void *)NULL);
1417
1418 cras_fmt_conv_destroy(&c);
1419 }
1420
1421 // Test format converter not created when in/out format conversion is not
1422 // needed.
TEST(FormatConverterTest,ConfigConverterNoNeed)1423 TEST(FormatConverterTest, ConfigConverterNoNeed) {
1424 int i;
1425 struct cras_fmt_conv *c = NULL;
1426 struct cras_audio_format in_fmt;
1427 struct cras_audio_format out_fmt;
1428
1429 ResetStub();
1430 in_fmt.format = SND_PCM_FORMAT_S16_LE;
1431 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1432 in_fmt.num_channels = 2;
1433 out_fmt.num_channels = 2;
1434 in_fmt.frame_rate = 48000;
1435 out_fmt.frame_rate = 48000;
1436 for (i = 0; i < CRAS_CH_MAX; i++) {
1437 in_fmt.channel_layout[i] = stereo_channel_layout[i];
1438 out_fmt.channel_layout[i] = stereo_channel_layout[i];
1439 }
1440
1441 config_format_converter(&c, CRAS_STREAM_OUTPUT, &in_fmt, &out_fmt, 4096);
1442 EXPECT_NE(c, (void *)NULL);
1443 EXPECT_EQ(0, cras_fmt_conversion_needed(c));
1444 cras_fmt_conv_destroy(&c);
1445 }
1446
1447 // Test format converter not created for input when in/out format differs
1448 // at channel count or layout.
TEST(FormatConverterTest,ConfigConverterNoNeedForInput)1449 TEST(FormatConverterTest, ConfigConverterNoNeedForInput) {
1450 static int kmic_channel_layout[CRAS_CH_MAX] =
1451 {0, 1, -1, -1, 2, -1, -1, -1, -1, -1, -1};
1452 int i;
1453 struct cras_fmt_conv *c = NULL;
1454 struct cras_audio_format in_fmt;
1455 struct cras_audio_format out_fmt;
1456
1457 ResetStub();
1458 in_fmt.format = SND_PCM_FORMAT_S16_LE;
1459 out_fmt.format = SND_PCM_FORMAT_S16_LE;
1460 in_fmt.num_channels = 2;
1461 out_fmt.num_channels = 3;
1462 in_fmt.frame_rate = 48000;
1463 out_fmt.frame_rate = 48000;
1464 for (i = 0; i < CRAS_CH_MAX; i++) {
1465 in_fmt.channel_layout[i] = stereo_channel_layout[i];
1466 out_fmt.channel_layout[i] = kmic_channel_layout[i];
1467 }
1468
1469 config_format_converter(&c, CRAS_STREAM_INPUT, &in_fmt, &out_fmt, 4096);
1470 EXPECT_NE(c, (void *)NULL);
1471 EXPECT_EQ(0, cras_fmt_conversion_needed(c));
1472 cras_fmt_conv_destroy(&c);
1473 }
1474
TEST(ChannelRemixTest,ChannelRemixAppliedOrNot)1475 TEST(ChannelRemixTest, ChannelRemixAppliedOrNot) {
1476 float coeff[4] = {0.5, 0.5, 0.26, 0.73};
1477 struct cras_fmt_conv *conv;
1478 struct cras_audio_format fmt;
1479 int16_t *buf, *res;
1480 unsigned i;
1481
1482 fmt.num_channels = 2;
1483 conv = cras_channel_remix_conv_create(2, coeff);
1484
1485 buf = (int16_t *)ralloc(50 * 4);
1486 res = (int16_t *)malloc(50 * 4);
1487
1488 for (i = 0; i < 100; i += 2) {
1489 res[i] = coeff[0] * buf[i];
1490 res[i] += coeff[1] * buf[i + 1];
1491 res[i + 1] = coeff[2] * buf[i];
1492 res[i + 1] += coeff[3] * buf[i + 1];
1493 }
1494
1495 cras_channel_remix_convert(conv, &fmt, (uint8_t *)buf, 50);
1496 for (i = 0; i < 100; i++)
1497 EXPECT_EQ(res[i], buf[i]);
1498
1499 /* If num_channels not match, remix conversion will not apply. */
1500 fmt.num_channels = 6;
1501 cras_channel_remix_convert(conv, &fmt, (uint8_t *)buf, 50);
1502 for (i = 0; i < 100; i++)
1503 EXPECT_EQ(res[i], buf[i]);
1504
1505 cras_fmt_conv_destroy(&conv);
1506 free(buf);
1507 free(res);
1508 }
1509
main(int argc,char ** argv)1510 int main(int argc, char **argv) {
1511 ::testing::InitGoogleTest(&argc, argv);
1512 return RUN_ALL_TESTS();
1513 }
1514
1515 extern "C" {
cras_channel_conv_matrix_alloc(size_t in_ch,size_t out_ch)1516 float** cras_channel_conv_matrix_alloc(size_t in_ch, size_t out_ch)
1517 {
1518 int i;
1519 float** conv_mtx;
1520 conv_mtx = (float **)calloc(CRAS_CH_MAX, sizeof(*conv_mtx));
1521 for (i = 0; i < CRAS_CH_MAX; i++)
1522 conv_mtx[i] = (float *)calloc(CRAS_CH_MAX, sizeof(*conv_mtx[i]));
1523 return conv_mtx;
1524 }
cras_channel_conv_matrix_destroy(float ** mtx,size_t out_ch)1525 void cras_channel_conv_matrix_destroy(float **mtx, size_t out_ch)
1526 {
1527 int i;
1528 for (i = 0; i < CRAS_CH_MAX; i++)
1529 free(mtx[i]);
1530 free(mtx);
1531 }
cras_channel_conv_matrix_create(const struct cras_audio_format * in,const struct cras_audio_format * out)1532 float **cras_channel_conv_matrix_create(const struct cras_audio_format *in,
1533 const struct cras_audio_format *out)
1534 {
1535 return cras_channel_conv_matrix_alloc(in->num_channels,
1536 out->num_channels);
1537 }
linear_resampler_create(unsigned int num_channels,unsigned int format_bytes,float src_rate,float dst_rate)1538 struct linear_resampler *linear_resampler_create(unsigned int num_channels,
1539 unsigned int format_bytes,
1540 float src_rate,
1541 float dst_rate)
1542 {
1543 linear_resampler_format_bytes = format_bytes;
1544 linear_resampler_num_channels = num_channels;
1545 return reinterpret_cast<struct linear_resampler*>(0x33);;
1546 }
1547
linear_resampler_needed(struct linear_resampler * lr)1548 int linear_resampler_needed(struct linear_resampler *lr)
1549 {
1550 return linear_resampler_needed_val;
1551 }
1552
linear_resampler_set_rates(struct linear_resampler * lr,unsigned int from,unsigned int to)1553 void linear_resampler_set_rates(struct linear_resampler *lr,
1554 unsigned int from,
1555 unsigned int to)
1556 {
1557 }
1558
linear_resampler_out_frames_to_in(struct linear_resampler * lr,unsigned int frames)1559 unsigned int linear_resampler_out_frames_to_in(struct linear_resampler *lr,
1560 unsigned int frames)
1561 {
1562 return (double)frames / linear_resampler_ratio;
1563 }
1564
1565 /* Converts the frames count from input rate to output rate. */
linear_resampler_in_frames_to_out(struct linear_resampler * lr,unsigned int frames)1566 unsigned int linear_resampler_in_frames_to_out(struct linear_resampler *lr,
1567 unsigned int frames)
1568 {
1569 return (double)frames * linear_resampler_ratio;
1570 }
1571
linear_resampler_resample(struct linear_resampler * lr,uint8_t * src,unsigned int * src_frames,uint8_t * dst,unsigned dst_frames)1572 unsigned int linear_resampler_resample(struct linear_resampler *lr,
1573 uint8_t *src,
1574 unsigned int *src_frames,
1575 uint8_t *dst,
1576 unsigned dst_frames)
1577 {
1578 unsigned int resampled_fr = *src_frames * linear_resampler_ratio;
1579
1580 if (resampled_fr > dst_frames) {
1581 resampled_fr = dst_frames;
1582 *src_frames = dst_frames / linear_resampler_ratio;
1583 }
1584 unsigned int resampled_bytes = resampled_fr * linear_resampler_format_bytes *
1585 linear_resampler_num_channels;
1586 for(size_t i = 0; i < resampled_bytes; i++)
1587 dst[i] = (uint8_t)rand() & 0xff;
1588
1589 return resampled_fr;
1590 }
1591
linear_resampler_destroy(struct linear_resampler * lr)1592 void linear_resampler_destroy(struct linear_resampler *lr)
1593 {
1594 }
1595 } // extern "C"
1596