1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_format_tests"
19 #include <log/log.h>
20
21 #include <audio_utils/format.h>
22 #include <gtest/gtest.h>
23
24 /** returns true if the format is a common source or destination format.
25 memcpy_by_audio_format() allows interchange between any PCM format and the
26 "common" PCM 16 bit and PCM float formats. */
is_common_src_format(audio_format_t format)27 static bool is_common_src_format(audio_format_t format) {
28 return format == AUDIO_FORMAT_PCM_16_BIT || format == AUDIO_FORMAT_PCM_FLOAT;
29 }
30
is_common_dst_format(audio_format_t format)31 static bool is_common_dst_format(audio_format_t format) {
32 return format == AUDIO_FORMAT_PCM_8_BIT // Allowed for HAL to AudioRecord conversion.
33 || format == AUDIO_FORMAT_PCM_16_BIT
34 || format == AUDIO_FORMAT_PCM_FLOAT;
35 }
36
37 // Initialize PCM 16 bit ramp for basic data validation (generated from PCM 8 bit data).
38 // TODO: consider creating fillPseudoRandomValue().
39 template<size_t size>
fillRamp(int16_t (& buffer)[size])40 static void fillRamp(int16_t(&buffer)[size])
41 {
42 // Create PCM 16 bit data based on PCM 8 bit format because PCM 8 bit is convertible
43 // to all other audio formats without loss; hence, round trip conversion preserves equality.
44 uint8_t bytes[size];
45 for (size_t i = 0; i < size; ++i) {
46 bytes[i] = i;
47 }
48 // convert to PCM 16 bit
49 memcpy_by_audio_format(
50 buffer, AUDIO_FORMAT_PCM_16_BIT,
51 bytes, AUDIO_FORMAT_PCM_8_BIT, size);
52
53 uint8_t check[size];
54 memcpy_by_audio_format(
55 check, AUDIO_FORMAT_PCM_8_BIT,
56 buffer, AUDIO_FORMAT_PCM_16_BIT, size);
57 EXPECT_EQ(0, memcmp(check, bytes, size));
58 }
59
60 class FormatTest : public testing::TestWithParam<std::tuple<audio_format_t, audio_format_t>>
61 {
62 };
63
TEST_P(FormatTest,memcpy_by_audio_format)64 TEST_P(FormatTest, memcpy_by_audio_format)
65 {
66 // fetch parameters
67 const auto param = GetParam();
68 const audio_format_t src_encoding = std::get<0>(param);
69 const audio_format_t dst_encoding = std::get<1>(param);
70
71 // either source or destination (or both) need to be a common format
72 if (!is_common_src_format(src_encoding) && !is_common_dst_format(dst_encoding)) {
73 printf("skip conversion src:%#x dst:%#x\n", src_encoding, dst_encoding);
74 return;
75 }
76
77 constexpr size_t SAMPLES = UINT8_MAX;
78 constexpr audio_format_t orig_encoding = AUDIO_FORMAT_PCM_16_BIT;
79 int16_t orig_data[SAMPLES];
80
81 fillRamp(orig_data);
82
83 // data buffer for in-place conversion (uint32_t is maximum sample size of 4 bytes)
84 uint32_t data[SAMPLES];
85 // check buffer is used to compare out-of-place vs in-place conversion.
86 uint32_t check[SAMPLES];
87
88 printf("trying conversion src:%#x dst:%#x\n", src_encoding, dst_encoding);
89 fflush(stdout);
90 // Copy original data to data buffer at src_encoding.
91 memcpy_by_audio_format(
92 data, src_encoding,
93 orig_data, orig_encoding, SAMPLES);
94
95 // Convert from src encoding to dst encoding.
96 memcpy_by_audio_format(
97 check, dst_encoding,
98 data, src_encoding, SAMPLES);
99
100 // Check in-place is same as out-of-place conversion.
101 memcpy_by_audio_format(
102 data, dst_encoding,
103 data, src_encoding, SAMPLES);
104 EXPECT_EQ(0, memcmp(check, data, SAMPLES * audio_bytes_per_sample(dst_encoding)));
105
106 // Go back to the original data encoding for comparison.
107 memcpy_by_audio_format(
108 data, orig_encoding,
109 data, dst_encoding, SAMPLES);
110
111 // Raw byte compare at the original encoding must succeed - our conversions
112 // must be lossless for PCM 8 bit representation which orig_data was constructed from.
113 EXPECT_EQ(0,
114 memcmp(data, orig_data, SAMPLES * audio_bytes_per_sample(orig_encoding)));
115 }
116
117 INSTANTIATE_TEST_CASE_P(FormatVariations, FormatTest, ::testing::Combine(
118 ::testing::Values(
119 AUDIO_FORMAT_PCM_8_BIT,
120 AUDIO_FORMAT_PCM_16_BIT,
121 AUDIO_FORMAT_PCM_FLOAT,
122 AUDIO_FORMAT_PCM_24_BIT_PACKED,
123 AUDIO_FORMAT_PCM_32_BIT,
124 AUDIO_FORMAT_PCM_8_24_BIT
125 ),
126 ::testing::Values(
127 AUDIO_FORMAT_PCM_8_BIT,
128 AUDIO_FORMAT_PCM_16_BIT,
129 AUDIO_FORMAT_PCM_FLOAT,
130 AUDIO_FORMAT_PCM_24_BIT_PACKED,
131 AUDIO_FORMAT_PCM_32_BIT,
132 AUDIO_FORMAT_PCM_8_24_BIT
133 )));
134