Phil Burk | d55dea3 | 2020-03-04 11:54:59 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #include <array> |
| 18 | #include <climits> |
| 19 | #include <math.h> |
| 20 | #include <memory> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include <audio_utils/spdif/SPDIFEncoder.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | class MySPDIFEncoder : public SPDIFEncoder { |
| 30 | public: |
| 31 | |
| 32 | explicit MySPDIFEncoder(audio_format_t format) |
| 33 | : SPDIFEncoder(format) |
| 34 | { |
| 35 | } |
| 36 | // Defaults to AC3 format. Was in original API. |
| 37 | MySPDIFEncoder() = default; |
| 38 | |
| 39 | ssize_t writeOutput( const void* /* buffer */, size_t numBytes ) override { |
| 40 | mOutputSizeBytes = numBytes; |
| 41 | return numBytes; |
| 42 | } |
| 43 | |
| 44 | FrameScanner *getFramer() const { return mFramer; } |
| 45 | size_t getByteCursor() const { return mByteCursor; } |
| 46 | size_t getPayloadBytesPending() const { return mPayloadBytesPending; } |
| 47 | size_t getBurstBufferSizeBytes() const { return mBurstBufferSizeBytes; } |
| 48 | |
| 49 | size_t mOutputSizeBytes = 0; |
| 50 | }; |
| 51 | |
| 52 | // This is the beginning of the file voice1-48k-64kbps-15s.ac3 |
| 53 | static const uint8_t sVoice1ch48k_AC3[] = { |
| 54 | 0x0b, 0x77, 0x44, 0xcd, 0x08, 0x40, 0x2f, 0x84, 0x29, 0xca, 0x6e, 0x44, 0xa4, 0xfd, 0xce, 0xf7, |
| 55 | 0xc9, 0x9f, 0x3e, 0x74, 0xfa, 0x01, 0x0a, 0xda, 0xb3, 0x3e, 0xb0, 0x95, 0xf2, 0x5a, 0xef, 0x9e |
| 56 | }; |
| 57 | |
| 58 | // This is the beginning of the file channelcheck_48k6ch.eac3 |
| 59 | static const uint8_t sChannel6ch48k_EAC3[] = { |
| 60 | 0x0b, 0x77, 0x01, 0xbf, 0x3f, 0x85, 0x7f, 0xe8, 0x1e, 0x40, 0x82, 0x10, 0x00, 0x00, 0x00, 0x01, |
| 61 | 0x00, 0x00, 0x00, 0x03, 0xfc, 0x60, 0x80, 0x7e, 0x59, 0x00, 0xfc, 0xf3, 0xcf, 0x01, 0xf9, 0xe7 |
| 62 | }; |
| 63 | |
| 64 | static const uint8_t sZeros[32] = { 0 }; |
| 65 | |
| 66 | static constexpr int kBytesPerOutputFrame = 2 * sizeof(int16_t); // stereo |
| 67 | |
| 68 | TEST(audio_utils_spdif, SupportedFormats) |
| 69 | { |
| 70 | ASSERT_FALSE(SPDIFEncoder::isFormatSupported(AUDIO_FORMAT_PCM_FLOAT)); |
| 71 | ASSERT_FALSE(SPDIFEncoder::isFormatSupported(AUDIO_FORMAT_PCM_16_BIT)); |
| 72 | ASSERT_FALSE(SPDIFEncoder::isFormatSupported(AUDIO_FORMAT_MP3)); |
| 73 | |
| 74 | ASSERT_TRUE(SPDIFEncoder::isFormatSupported(AUDIO_FORMAT_AC3)); |
| 75 | ASSERT_TRUE(SPDIFEncoder::isFormatSupported(AUDIO_FORMAT_E_AC3)); |
| 76 | ASSERT_TRUE(SPDIFEncoder::isFormatSupported(AUDIO_FORMAT_DTS)); |
| 77 | ASSERT_TRUE(SPDIFEncoder::isFormatSupported(AUDIO_FORMAT_DTS_HD)); |
| 78 | } |
| 79 | |
| 80 | TEST(audio_utils_spdif, ScanAC3) |
| 81 | { |
| 82 | MySPDIFEncoder encoder(AUDIO_FORMAT_AC3); |
| 83 | FrameScanner *scanner = encoder.getFramer(); |
| 84 | // It should recognize the valid AC3 header. |
| 85 | int i = 0; |
| 86 | while (i < 5) { |
| 87 | ASSERT_FALSE(scanner->scan(sVoice1ch48k_AC3[i++])); |
| 88 | } |
| 89 | ASSERT_TRUE(scanner->scan(sVoice1ch48k_AC3[i++])); |
| 90 | ASSERT_FALSE(scanner->scan(sVoice1ch48k_AC3[i++])); |
| 91 | } |
| 92 | |
| 93 | TEST(audio_utils_spdif, WriteAC3) |
| 94 | { |
| 95 | MySPDIFEncoder encoder(AUDIO_FORMAT_AC3); |
| 96 | encoder.write(sVoice1ch48k_AC3, sizeof(sVoice1ch48k_AC3)); |
| 97 | ASSERT_EQ(48000, encoder.getFramer()->getSampleRate()); |
| 98 | ASSERT_EQ(kBytesPerOutputFrame, encoder.getBytesPerOutputFrame()); |
| 99 | ASSERT_EQ(1, encoder.getRateMultiplier()); |
| 100 | |
| 101 | // Check to make sure that the pending bytes calculation did not overflow. |
| 102 | size_t burstBufferSizeBytes = encoder.getBurstBufferSizeBytes(); // allocated maximum size |
| 103 | size_t pendingBytes = encoder.getPayloadBytesPending(); |
| 104 | ASSERT_GE(burstBufferSizeBytes, pendingBytes); |
| 105 | |
| 106 | // Write some fake compressed audio to force an output data burst. |
| 107 | for (int i = 0; i < 7; i++) { |
| 108 | auto result = encoder.write(sZeros, sizeof(sZeros)); |
| 109 | ASSERT_EQ(sizeof(sZeros), result); |
| 110 | } |
| 111 | // This value is calculated in SPDIFEncoder::sendZeroPad() |
| 112 | // size_t burstSize = mFramer->getSampleFramesPerSyncFrame() * sizeof(uint16_t) |
| 113 | // * SPDIF_ENCODED_CHANNEL_COUNT; |
| 114 | // If it changes then there is probably a regression. |
| 115 | const int kExpectedBurstSize = 6144; |
| 116 | ASSERT_EQ(kExpectedBurstSize, encoder.mOutputSizeBytes); |
| 117 | } |
| 118 | |
| 119 | TEST(audio_utils_spdif, ValidEAC3) |
| 120 | { |
| 121 | MySPDIFEncoder encoder(AUDIO_FORMAT_E_AC3); |
| 122 | auto result = encoder.write(sChannel6ch48k_EAC3, sizeof(sChannel6ch48k_EAC3)); |
| 123 | ASSERT_EQ(sizeof(sChannel6ch48k_EAC3), result); |
| 124 | ASSERT_EQ(4, encoder.getRateMultiplier()); // EAC3_RATE_MULTIPLIER |
| 125 | ASSERT_EQ(48000, encoder.getFramer()->getSampleRate()); |
| 126 | ASSERT_EQ(kBytesPerOutputFrame, encoder.getBytesPerOutputFrame()); |
| 127 | |
| 128 | // Check to make sure that the pending bytes calculation did not overflow. |
| 129 | size_t bufferSize = encoder.getBurstBufferSizeBytes(); |
| 130 | size_t pendingBytes = encoder.getPayloadBytesPending(); |
| 131 | ASSERT_GE(bufferSize, pendingBytes); |
| 132 | } |
| 133 | |
| 134 | TEST(audio_utils_spdif, InvalidLengthEAC3) |
| 135 | { |
| 136 | MySPDIFEncoder encoder(AUDIO_FORMAT_E_AC3); |
| 137 | // Mangle a valid header and try to force a numeric overflow. |
| 138 | uint8_t mangled[sizeof(sChannel6ch48k_EAC3)] = {0}; |
| 139 | memcpy(mangled, sChannel6ch48k_EAC3, sizeof(sChannel6ch48k_EAC3)); |
| 140 | |
| 141 | // force frmsiz to zero! |
| 142 | mangled[2] = mangled[2] & 0xF8; |
| 143 | mangled[3] = 0; |
| 144 | auto result = encoder.write(mangled, sizeof(mangled)); |
| 145 | ASSERT_EQ(sizeof(mangled), result); |
| 146 | |
| 147 | // Check to make sure that the pending bytes calculation did not overflow. |
| 148 | size_t bufferSize = encoder.getBurstBufferSizeBytes(); |
| 149 | size_t pendingBytes = encoder.getPayloadBytesPending(); |
| 150 | ASSERT_GE(bufferSize, pendingBytes); |
| 151 | |
| 152 | } |