format_tests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_format_tests"
  18. #include <log/log.h>
  19. #include <audio_utils/format.h>
  20. #include <gtest/gtest.h>
  21. /** returns true if the format is a common source or destination format.
  22. memcpy_by_audio_format() allows interchange between any PCM format and the
  23. "common" PCM 16 bit and PCM float formats. */
  24. static bool is_common_format(audio_format_t format) {
  25. return format == AUDIO_FORMAT_PCM_16_BIT || format == AUDIO_FORMAT_PCM_FLOAT;
  26. }
  27. // Initialize PCM 16 bit ramp for basic data sanity check (generated from PCM 8 bit data).
  28. // TODO: consider creating fillPseudoRandomValue().
  29. template<size_t size>
  30. static void fillRamp(int16_t(&buffer)[size])
  31. {
  32. // Create PCM 16 bit data based on PCM 8 bit format because PCM 8 bit is convertible
  33. // to all other audio formats without loss; hence, round trip conversion preserves equality.
  34. uint8_t bytes[size];
  35. for (size_t i = 0; i < size; ++i) {
  36. bytes[i] = i;
  37. }
  38. // convert to PCM 16 bit
  39. memcpy_by_audio_format(
  40. buffer, AUDIO_FORMAT_PCM_16_BIT,
  41. bytes, AUDIO_FORMAT_PCM_8_BIT, size);
  42. uint8_t check[size];
  43. memcpy_by_audio_format(
  44. check, AUDIO_FORMAT_PCM_8_BIT,
  45. buffer, AUDIO_FORMAT_PCM_16_BIT, size);
  46. EXPECT_EQ(0, memcmp(check, bytes, size));
  47. }
  48. class FormatTest : public testing::TestWithParam<std::tuple<audio_format_t, audio_format_t>>
  49. {
  50. };
  51. TEST_P(FormatTest, memcpy_by_audio_format)
  52. {
  53. // fetch parameters
  54. const auto param = GetParam();
  55. const audio_format_t src_encoding = std::get<0>(param);
  56. const audio_format_t dst_encoding = std::get<1>(param);
  57. // either source or destination (or both) need to be a common format
  58. if (!is_common_format(src_encoding) && !is_common_format(dst_encoding)) {
  59. printf("skip conversion src:%#x dst:%#x\n", src_encoding, dst_encoding);
  60. return;
  61. }
  62. constexpr size_t SAMPLES = UINT8_MAX;
  63. constexpr audio_format_t orig_encoding = AUDIO_FORMAT_PCM_16_BIT;
  64. int16_t orig_data[SAMPLES];
  65. fillRamp(orig_data);
  66. // data buffer for in-place conversion (uint32_t is maximum sample size of 4 bytes)
  67. uint32_t data[SAMPLES];
  68. // check buffer is used to compare out-of-place vs in-place conversion.
  69. uint32_t check[SAMPLES];
  70. printf("trying conversion src:%#x dst:%#x\n", src_encoding, dst_encoding);
  71. fflush(stdout);
  72. // Copy original data to data buffer at src_encoding.
  73. memcpy_by_audio_format(
  74. data, src_encoding,
  75. orig_data, orig_encoding, SAMPLES);
  76. // Convert from src encoding to dst encoding.
  77. memcpy_by_audio_format(
  78. check, dst_encoding,
  79. data, src_encoding, SAMPLES);
  80. // Check in-place is same as out-of-place conversion.
  81. memcpy_by_audio_format(
  82. data, dst_encoding,
  83. data, src_encoding, SAMPLES);
  84. EXPECT_EQ(0, memcmp(check, data, SAMPLES * audio_bytes_per_sample(dst_encoding)));
  85. // Go back to the original data encoding for comparison.
  86. memcpy_by_audio_format(
  87. data, orig_encoding,
  88. data, dst_encoding, SAMPLES);
  89. // Raw byte compare at the original encoding must succeed - our conversions
  90. // must be lossless for PCM 8 bit representation which orig_data was constructed from.
  91. EXPECT_EQ(0,
  92. memcmp(data, orig_data, SAMPLES * audio_bytes_per_sample(orig_encoding)));
  93. }
  94. INSTANTIATE_TEST_CASE_P(FormatVariations, FormatTest, ::testing::Combine(
  95. ::testing::Values(
  96. AUDIO_FORMAT_PCM_8_BIT,
  97. AUDIO_FORMAT_PCM_16_BIT,
  98. AUDIO_FORMAT_PCM_FLOAT,
  99. AUDIO_FORMAT_PCM_24_BIT_PACKED,
  100. AUDIO_FORMAT_PCM_32_BIT,
  101. AUDIO_FORMAT_PCM_8_24_BIT
  102. ),
  103. ::testing::Values(
  104. AUDIO_FORMAT_PCM_8_BIT,
  105. AUDIO_FORMAT_PCM_16_BIT,
  106. AUDIO_FORMAT_PCM_FLOAT,
  107. AUDIO_FORMAT_PCM_24_BIT_PACKED,
  108. AUDIO_FORMAT_PCM_32_BIT,
  109. AUDIO_FORMAT_PCM_8_24_BIT
  110. )));