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