Ruchika Ashtankar | d1a00f8 | 2019-11-18 19:24:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 "Mp3DecoderTest" |
| 19 | |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <audio_utils/sndfile.h> |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #include "mp3reader.h" |
| 26 | #include "pvmp3decoder_api.h" |
| 27 | |
| 28 | #include "Mp3DecoderTestEnvironment.h" |
| 29 | |
| 30 | #define OUTPUT_FILE "/data/local/tmp/mp3Decode.out" |
| 31 | |
| 32 | constexpr int32_t kInputBufferSize = 1024 * 10; |
| 33 | constexpr int32_t kOutputBufferSize = 4608 * 2; |
| 34 | constexpr int32_t kMaxCount = 10; |
| 35 | constexpr int32_t kNumFrameReset = 150; |
| 36 | |
| 37 | static Mp3DecoderTestEnvironment *gEnv = nullptr; |
| 38 | |
| 39 | class Mp3DecoderTest : public ::testing::TestWithParam<string> { |
| 40 | public: |
| 41 | Mp3DecoderTest() : mConfig(nullptr) {} |
| 42 | |
| 43 | ~Mp3DecoderTest() { |
| 44 | if (mConfig) { |
| 45 | delete mConfig; |
| 46 | mConfig = nullptr; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | virtual void SetUp() override { |
| 51 | mConfig = new tPVMP3DecoderExternal{}; |
| 52 | ASSERT_NE(mConfig, nullptr) << "Failed to initialize config. No Memory available"; |
| 53 | mConfig->equalizerType = flat; |
| 54 | mConfig->crcEnabled = false; |
| 55 | } |
| 56 | |
| 57 | tPVMP3DecoderExternal *mConfig; |
| 58 | Mp3Reader mMp3Reader; |
| 59 | |
| 60 | ERROR_CODE DecodeFrames(void *decoderbuf, SNDFILE *outFileHandle, SF_INFO sfInfo, |
| 61 | int32_t frameCount = INT32_MAX); |
| 62 | SNDFILE *openOutputFile(SF_INFO *sfInfo); |
| 63 | }; |
| 64 | |
| 65 | ERROR_CODE Mp3DecoderTest::DecodeFrames(void *decoderBuf, SNDFILE *outFileHandle, SF_INFO sfInfo, |
| 66 | int32_t frameCount) { |
| 67 | uint8_t inputBuf[kInputBufferSize]; |
| 68 | int16_t outputBuf[kOutputBufferSize]; |
| 69 | uint32_t bytesRead; |
| 70 | ERROR_CODE decoderErr; |
| 71 | while (frameCount > 0) { |
| 72 | bool success = mMp3Reader.getFrame(inputBuf, &bytesRead); |
| 73 | if (!success) { |
| 74 | break; |
| 75 | } |
| 76 | mConfig->inputBufferCurrentLength = bytesRead; |
| 77 | mConfig->inputBufferMaxLength = 0; |
| 78 | mConfig->inputBufferUsedLength = 0; |
| 79 | mConfig->pInputBuffer = inputBuf; |
| 80 | mConfig->pOutputBuffer = outputBuf; |
| 81 | mConfig->outputFrameSize = kOutputBufferSize / sizeof(int16_t); |
| 82 | decoderErr = pvmp3_framedecoder(mConfig, decoderBuf); |
| 83 | if (decoderErr != NO_DECODING_ERROR) break; |
| 84 | sf_writef_short(outFileHandle, outputBuf, mConfig->outputFrameSize / sfInfo.channels); |
| 85 | frameCount--; |
| 86 | } |
| 87 | return decoderErr; |
| 88 | } |
| 89 | |
| 90 | SNDFILE *Mp3DecoderTest::openOutputFile(SF_INFO *sfInfo) { |
| 91 | memset(sfInfo, 0, sizeof(SF_INFO)); |
| 92 | sfInfo->channels = mMp3Reader.getNumChannels(); |
| 93 | sfInfo->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; |
| 94 | sfInfo->samplerate = mMp3Reader.getSampleRate(); |
| 95 | SNDFILE *outFileHandle = sf_open(OUTPUT_FILE, SFM_WRITE, sfInfo); |
| 96 | return outFileHandle; |
| 97 | } |
| 98 | |
| 99 | TEST_F(Mp3DecoderTest, MultiCreateMp3DecoderTest) { |
| 100 | size_t memRequirements = pvmp3_decoderMemRequirements(); |
| 101 | ASSERT_NE(memRequirements, 0) << "Failed to get the memory requirement size"; |
| 102 | void *decoderBuf = malloc(memRequirements); |
| 103 | ASSERT_NE(decoderBuf, nullptr) |
| 104 | << "Failed to allocate decoder memory of size " << memRequirements; |
| 105 | for (int count = 0; count < kMaxCount; count++) { |
| 106 | pvmp3_InitDecoder(mConfig, decoderBuf); |
| 107 | ALOGV("Decoder created successfully"); |
| 108 | } |
| 109 | if (decoderBuf) { |
| 110 | free(decoderBuf); |
| 111 | decoderBuf = nullptr; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | TEST_P(Mp3DecoderTest, DecodeTest) { |
| 116 | size_t memRequirements = pvmp3_decoderMemRequirements(); |
| 117 | ASSERT_NE(memRequirements, 0) << "Failed to get the memory requirement size"; |
| 118 | void *decoderBuf = malloc(memRequirements); |
| 119 | ASSERT_NE(decoderBuf, nullptr) |
| 120 | << "Failed to allocate decoder memory of size " << memRequirements; |
| 121 | |
| 122 | pvmp3_InitDecoder(mConfig, decoderBuf); |
| 123 | ALOGV("Decoder created successfully"); |
| 124 | string inputFile = gEnv->getRes() + GetParam(); |
| 125 | bool status = mMp3Reader.init(inputFile.c_str()); |
| 126 | ASSERT_TRUE(status) << "Unable to initialize the mp3Reader"; |
| 127 | |
| 128 | // Open the output file. |
| 129 | SF_INFO sfInfo; |
| 130 | SNDFILE *outFileHandle = openOutputFile(&sfInfo); |
| 131 | ASSERT_NE(outFileHandle, nullptr) << "Error opening output file for writing decoded output"; |
| 132 | |
| 133 | ERROR_CODE decoderErr = DecodeFrames(decoderBuf, outFileHandle, sfInfo); |
| 134 | ASSERT_EQ(decoderErr, NO_DECODING_ERROR) << "Failed to decode the frames"; |
| 135 | ASSERT_EQ(sfInfo.channels, mConfig->num_channels) << "Number of channels does not match"; |
| 136 | ASSERT_EQ(sfInfo.samplerate, mConfig->samplingRate) << "Sample rate does not match"; |
| 137 | |
| 138 | mMp3Reader.close(); |
| 139 | sf_close(outFileHandle); |
| 140 | if (decoderBuf) { |
| 141 | free(decoderBuf); |
| 142 | decoderBuf = nullptr; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | TEST_P(Mp3DecoderTest, ResetDecoderTest) { |
| 147 | size_t memRequirements = pvmp3_decoderMemRequirements(); |
| 148 | ASSERT_NE(memRequirements, 0) << "Failed to get the memory requirement size"; |
| 149 | void *decoderBuf = malloc(memRequirements); |
| 150 | ASSERT_NE(decoderBuf, nullptr) |
| 151 | << "Failed to allocate decoder memory of size " << memRequirements; |
| 152 | |
| 153 | pvmp3_InitDecoder(mConfig, decoderBuf); |
| 154 | ALOGV("Decoder created successfully."); |
| 155 | string inputFile = gEnv->getRes() + GetParam(); |
| 156 | bool status = mMp3Reader.init(inputFile.c_str()); |
| 157 | ASSERT_TRUE(status) << "Unable to initialize the mp3Reader"; |
| 158 | |
| 159 | // Open the output file. |
| 160 | SF_INFO sfInfo; |
| 161 | SNDFILE *outFileHandle = openOutputFile(&sfInfo); |
| 162 | ASSERT_NE(outFileHandle, nullptr) << "Error opening output file for writing decoded output"; |
| 163 | |
| 164 | ERROR_CODE decoderErr; |
| 165 | decoderErr = DecodeFrames(decoderBuf, outFileHandle, sfInfo, kNumFrameReset); |
| 166 | ASSERT_EQ(decoderErr, NO_DECODING_ERROR) << "Failed to decode the frames"; |
| 167 | ASSERT_EQ(sfInfo.channels, mConfig->num_channels) << "Number of channels does not match"; |
| 168 | ASSERT_EQ(sfInfo.samplerate, mConfig->samplingRate) << "Sample rate does not match"; |
| 169 | |
| 170 | pvmp3_resetDecoder(decoderBuf); |
| 171 | // Decode the same file. |
| 172 | decoderErr = DecodeFrames(decoderBuf, outFileHandle, sfInfo); |
| 173 | ASSERT_EQ(decoderErr, NO_DECODING_ERROR) << "Failed to decode the frames"; |
| 174 | ASSERT_EQ(sfInfo.channels, mConfig->num_channels) << "Number of channels does not match"; |
| 175 | ASSERT_EQ(sfInfo.samplerate, mConfig->samplingRate) << "Sample rate does not match"; |
| 176 | |
| 177 | mMp3Reader.close(); |
| 178 | sf_close(outFileHandle); |
| 179 | if (decoderBuf) { |
| 180 | free(decoderBuf); |
| 181 | decoderBuf = nullptr; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | INSTANTIATE_TEST_SUITE_P(Mp3DecoderTestAll, Mp3DecoderTest, |
| 186 | ::testing::Values(("bbb_44100hz_2ch_128kbps_mp3_30sec.mp3"), |
| 187 | ("bbb_44100hz_2ch_128kbps_mp3_5mins.mp3"), |
Harish Mahendrakar | 46b0a6c | 2020-09-12 04:52:01 +0530 | [diff] [blame] | 188 | ("bug_136053885.mp3"), |
Harish Mahendrakar | 91aacb1 | 2020-10-16 01:30:51 +0530 | [diff] [blame] | 189 | ("bbb_2ch_44kHz_lame_crc.mp3"), |
Ruchika Ashtankar | d1a00f8 | 2019-11-18 19:24:10 +0530 | [diff] [blame] | 190 | ("bbb_mp3_stereo_192kbps_48000hz.mp3"))); |
| 191 | |
| 192 | int main(int argc, char **argv) { |
| 193 | gEnv = new Mp3DecoderTestEnvironment(); |
| 194 | ::testing::AddGlobalTestEnvironment(gEnv); |
| 195 | ::testing::InitGoogleTest(&argc, argv); |
| 196 | int status = gEnv->initFromOptions(argc, argv); |
| 197 | if (status == 0) { |
| 198 | status = RUN_ALL_TESTS(); |
| 199 | ALOGV("Test result = %d\n", status); |
| 200 | } |
| 201 | return status; |
| 202 | } |