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