1 /*
2 * Copyright (C) 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
17
18 #include <utils/logging.h>
19 #include <oboe/Oboe.h>
20
21 #include "AAssetDataSource.h"
22
23 #if !defined(USE_FFMPEG)
24 #error USE_FFMPEG should be defined in app.gradle
25 #endif
26
27 #if USE_FFMPEG==1
28 #include "FFMpegExtractor.h"
29 #else
30 #include "NDKExtractor.h"
31 #endif
32
33
34 constexpr int kMaxCompressionRatio { 12 };
35
newFromCompressedAsset(AAssetManager & assetManager,const char * filename,const AudioProperties targetProperties)36 AAssetDataSource* AAssetDataSource::newFromCompressedAsset(
37 AAssetManager &assetManager,
38 const char *filename,
39 const AudioProperties targetProperties) {
40
41 AAsset *asset = AAssetManager_open(&assetManager, filename, AASSET_MODE_UNKNOWN);
42 if (!asset) {
43 LOGE("Failed to open asset %s", filename);
44 return nullptr;
45 }
46
47 off_t assetSize = AAsset_getLength(asset);
48 LOGD("Opened %s, size %ld", filename, assetSize);
49
50 // Allocate memory to store the decompressed audio. We don't know the exact
51 // size of the decoded data until after decoding so we make an assumption about the
52 // maximum compression ratio and the decoded sample format (float for FFmpeg, int16 for NDK).
53 #if USE_FFMPEG==true
54 const long maximumDataSizeInBytes = kMaxCompressionRatio * assetSize * sizeof(float);
55 auto decodedData = new uint8_t[maximumDataSizeInBytes];
56
57 int64_t bytesDecoded = FFMpegExtractor::decode(asset, decodedData, targetProperties);
58 auto numSamples = bytesDecoded / sizeof(float);
59 #else
60 const long maximumDataSizeInBytes = kMaxCompressionRatio * assetSize * sizeof(int16_t);
61 auto decodedData = new uint8_t[maximumDataSizeInBytes];
62
63 int64_t bytesDecoded = NDKExtractor::decode(asset, decodedData, targetProperties);
64 auto numSamples = bytesDecoded / sizeof(int16_t);
65 #endif
66
67 // Now we know the exact number of samples we can create a float array to hold the audio data
68 auto outputBuffer = std::make_unique<float[]>(numSamples);
69
70 #if USE_FFMPEG==1
71 memcpy(outputBuffer.get(), decodedData, (size_t)bytesDecoded);
72 #else
73 // The NDK decoder can only decode to int16, we need to convert to floats
74 oboe::convertPcm16ToFloat(
75 reinterpret_cast<int16_t*>(decodedData),
76 outputBuffer.get(),
77 bytesDecoded / sizeof(int16_t));
78 #endif
79
80 delete[] decodedData;
81 AAsset_close(asset);
82
83 return new AAssetDataSource(std::move(outputBuffer),
84 numSamples,
85 targetProperties);
86 }