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