1 /* 2 * Copyright (C) 2017 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_channels_tests" 19 20 #include <math.h> 21 #include <vector> 22 23 #include <gtest/gtest.h> 24 #include <log/log.h> 25 26 #include <audio_utils/channels.h> 27 28 // TODO: Make a common include file for helper functions. 29 30 template<typename T> 31 void checkMonotone(const T *ary, size_t size) 32 { 33 for (size_t i = 1; i < size; ++i) { 34 EXPECT_LT(ary[i-1], ary[i]); 35 } 36 } 37 38 template<typename T> 39 void checkUnsignedMonotoneOrZero(const T *ary, size_t size) 40 { 41 if (size == 0) return; 42 43 T least = ary[0]; 44 for (size_t i = 1; i < size; ++i) { 45 if (ary[i]) { 46 EXPECT_LT(least, ary[i]); 47 least = ary[i]; 48 } 49 } 50 } 51 52 template<typename T> 53 void expectEq(const T &c1, const T &c2) { 54 EXPECT_EQ(c1.size(), c2.size()); 55 EXPECT_EQ(0, memcmp(c1.data(), c2.data(), sizeof(c1[0]) * std::min(c1.size(), c2.size()))); 56 } 57 58 TEST(audio_utils_channels, adjust_channels) { 59 constexpr size_t size = 65536; 60 std::vector<uint16_t> u16ref(size); 61 std::vector<uint16_t> u16expand(size * 2); 62 std::vector<uint16_t> u16ary(size); 63 64 // reference buffer is monotonic. 65 for (size_t i = 0; i < u16ref.size(); ++i) { 66 u16ref[i] = i; 67 } 68 69 // expand channels from stereo to quad. 70 adjust_channels( 71 u16ref.data() /*in_buff*/, 72 2 /*in_channels*/, 73 u16expand.data() /*out_buff*/, 74 4 /*out_channels*/, 75 sizeof(u16ref[0]) /*sample_size_in_bytes*/, 76 sizeof(u16ref[0]) * u16ref.size() /*num_in_bytes*/); 77 78 // expanded buffer must increase (or be zero). 79 checkUnsignedMonotoneOrZero(u16expand.data(), u16expand.size()); 80 81 // contract channels back to stereo. 82 adjust_channels( 83 u16expand.data() /*in_buff*/, 84 4 /*in_channels*/, 85 u16ary.data() /*out_buff*/, 86 2 /*out_channels*/, 87 sizeof(u16expand[0]) /*sample_size_in_bytes*/, 88 sizeof(u16expand[0]) * u16expand.size() /*num_in_bytes*/); 89 90 // contracted array must be identical to original. 91 expectEq(u16ary, u16ref); 92 } 93 94 TEST(audio_utils_channels, adjust_selected_channels) { 95 constexpr size_t size = 65536; 96 std::vector<uint16_t> u16ref(size); 97 std::vector<uint16_t> u16contract(size / 2); 98 std::vector<uint16_t> u16ary(size); 99 100 // reference buffer is monotonic. 101 for (size_t i = 0; i < u16ref.size(); ++i) { 102 u16ref[i] = i; 103 } 104 105 // contract from quad to stereo. 106 adjust_selected_channels( 107 u16ref.data() /*in_buff*/, 108 4 /*in_channels*/, 109 u16contract.data() /*out_buff*/, 110 2 /*out_channels*/, 111 sizeof(u16ref[0]) /*sample_size_in_bytes*/, 112 sizeof(u16ref[0]) * u16ref.size() /*num_in_bytes*/); 113 114 // contracted buffer must increase. 115 checkMonotone(u16contract.data(), u16contract.size()); 116 117 // initialize channels 3 and 4 of final comparison array. 118 for (size_t i = 0; i < u16ary.size() / 4; ++i) { 119 u16ary[i * 4 + 2] = u16ref[i * 4 + 2]; 120 u16ary[i * 4 + 3] = u16ref[i * 4 + 3]; 121 } 122 123 // expand stereo into channels 1 and 2 of quad comparison array. 124 adjust_selected_channels( 125 u16contract.data() /*in_buff*/, 126 2 /*in_channels*/, 127 u16ary.data() /*out_buff*/, 128 4 /*out_channels*/, 129 sizeof(u16contract[0]) /*sample_size_in_bytes*/, 130 sizeof(u16contract[0]) * u16contract.size() /*num_in_bytes*/); 131 132 // comparison array must be identical to original. 133 expectEq(u16ary, u16ref); 134 } 135 136 137