1 /*
2 * Copyright (C) 2011 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 USE_LOG SLAndroidLogLevel_Debug
18
19 #include "sles_allinclusive.h"
20 #include "android/include/AacBqToPcmCbRenderer.h"
21 #include "android/channels.h"
22 #include <media/stagefright/foundation/ADebug.h>
23
24 namespace android {
25
26 // ADTS header size is 7, but frame size information ends on byte 6 (when counting from byte 1)
27 #define ADTS_HEADER_SIZE_UP_TO_FRAMESIZE 6
28
29 /**
30 * Returns the size of an AAC ADTS frame.
31 * Note that if the returned value + offset > size, it means that a partial frame starts at that
32 * offset, but this function will still return the size of the full frame.
33 * @param data pointer to the compressed audio data
34 * @param offset offset in bytes relative to data of where the frame is supposed to start
35 * @param size the size in bytes of the data block starting at data
36 * @return the size in bytes of the AAC ADTS frame starting at the given offset of the given
37 * memory address, 0 if the frame couldn't be parsed.
38 */
getAdtsFrameSize(const uint8_t * data,off64_t offset,size_t size)39 static size_t getAdtsFrameSize(const uint8_t *data, off64_t offset, size_t size) {
40 size_t frameSize = 0;
41
42 if (!(offset + ADTS_HEADER_SIZE_UP_TO_FRAMESIZE < (off64_t) size)) {
43 SL_LOGE("AacBqToPcmCbRenderer::getAdtsFrameSize() returns 0 (can't read syncword or header)"
44 );
45 return 0;
46 }
47
48 const uint8_t *syncword = data + offset;
49 if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
50 SL_LOGE("AacBqToPcmCbRenderer::getAdtsFrameSize() returns 0 (wrong syncword)");
51 return 0;
52 }
53
54 const uint8_t protectionAbsent = data[offset+1] & 0x1;
55
56 const uint8_t* header = data + offset + 3;
57 frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
58 // the frame size read already contains the size of the header, so no need to add it here
59
60 // protectionAbsent is 0 if there is CRC
61 static const size_t kAdtsHeaderLengthNoCrc = 7;
62 static const size_t kAdtsHeaderLengthWithCrc = 9;
63 size_t headSize = protectionAbsent ? kAdtsHeaderLengthNoCrc : kAdtsHeaderLengthWithCrc;
64 if (headSize > frameSize) {
65 SL_LOGE("AacBqToPcmCbRenderer::getAdtsFrameSize() returns 0 (frameSize %zu < headSize %zu)",
66 frameSize, headSize);
67 return 0;
68 }
69
70 SL_LOGV("AacBqToPcmCbRenderer::getAdtsFrameSize() returns %u", frameSize);
71
72 return frameSize;
73 }
74
75 /**
76 * Returns whether a block of memory starts and ends on AAC ADTS frame boundaries
77 * @param data pointer to the compressed audio data
78 * @param size the size in bytes of the data block to validate
79 * @return SL_RESULT_SUCCESS if there is AAC ADTS data, and it starts and ends on frame boundaries,
80 * or an appropriate error code otherwise:
81 * SL_RESULT_PARAMETER_INVALID if not possible to attempt validation of even one frame
82 * SL_RESULT_CONTENT_CORRUPTED if the frame contents are otherwise invalid
83 */
validateBufferStartEndOnFrameBoundaries(void * data,size_t size)84 SLresult AacBqToPcmCbRenderer::validateBufferStartEndOnFrameBoundaries(void* data, size_t size)
85 {
86 off64_t offset = 0;
87 size_t frameSize = 0;
88
89 if ((NULL == data) || (size == 0)) {
90 SL_LOGE("No ADTS to validate");
91 return SL_RESULT_PARAMETER_INVALID;
92 }
93
94 while (offset < (off64_t) size) {
95 if ((frameSize = getAdtsFrameSize((uint8_t *)data, offset, size)) == 0) {
96 SL_LOGE("found ADTS frame of size 0 at offset %lld", (long long) offset);
97 return SL_RESULT_CONTENT_CORRUPTED;
98 }
99 //SL_LOGV("last good offset %llu", offset);
100 offset += frameSize;
101 if (offset > (off64_t) size) {
102 SL_LOGE("found incomplete ADTS frame at end of data");
103 return SL_RESULT_CONTENT_CORRUPTED;
104 }
105 }
106 if (offset != (off64_t) size) {
107 SL_LOGE("ADTS parsing error: reached end of incomplete frame");
108 }
109 assert(offset == (off64_t) size);
110 return SL_RESULT_SUCCESS;
111 }
112
113 //--------------------------------------------------------------------------------------------------
AacBqToPcmCbRenderer(const AudioPlayback_Parameters * params,IAndroidBufferQueue * androidBufferQueue)114 AacBqToPcmCbRenderer::AacBqToPcmCbRenderer(const AudioPlayback_Parameters* params,
115 IAndroidBufferQueue *androidBufferQueue) :
116 AudioToCbRenderer(params),
117 mBqSource(new BufferQueueSource(androidBufferQueue))
118 {
119 SL_LOGD("AacBqToPcmCbRenderer::AacBqToPcmCbRenderer()");
120 }
121
122
~AacBqToPcmCbRenderer()123 AacBqToPcmCbRenderer::~AacBqToPcmCbRenderer() {
124 SL_LOGD("AacBqToPcmCbRenderer::~AacBqToPcmCbRenderer()");
125
126 }
127
128
129 //--------------------------------------------------
130 // Event handlers
onPrepare()131 void AacBqToPcmCbRenderer::onPrepare() {
132 SL_LOGD("AacBqToPcmCbRenderer::onPrepare()");
133 Mutex::Autolock _l(mBufferSourceLock);
134
135 // Initialize the PCM format info with the known parameters before the start of the decode
136 {
137 android::Mutex::Autolock autoLock(mPcmFormatLock);
138 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16;
139 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16;
140 //FIXME not true on all platforms
141 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN;
142 // initialization with the default values: they will be replaced by the actual values
143 // once the decoder has figured them out
144 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = UNKNOWN_NUMCHANNELS;
145 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = UNKNOWN_SAMPLERATE;
146 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = UNKNOWN_CHANNELMASK;
147 }
148
149 sp<MediaExtractor> extractor = new AacAdtsExtractor(mBqSource);
150
151 // only decoding a single track of data
152 const size_t kTrackToDecode = 0;
153
154 sp<MediaSource> source = extractor->getTrack(kTrackToDecode);
155 if (source == 0) {
156 SL_LOGE("AacBqToPcmCbRenderer::onPrepare: error getting source from extractor");
157 notifyPrepared(ERROR_UNSUPPORTED);
158 return;
159 }
160 sp<MetaData> meta = extractor->getTrackMetaData(kTrackToDecode);
161
162 // the audio content is not raw PCM, so we need a decoder
163 OMXClient client;
164 CHECK_EQ(client.connect(), (status_t)OK);
165
166 source = OMXCodec::Create(
167 client.interface(), meta, false /* createEncoder */,
168 source);
169
170 if (source == NULL) {
171 SL_LOGE("AacBqToPcmCbRenderer::onPrepare: Could not instantiate decoder.");
172 notifyPrepared(ERROR_UNSUPPORTED);
173 return;
174 }
175
176 meta = source->getFormat();
177
178 SL_LOGD("AacBqToPcmCbRenderer::onPrepare() after instantiating decoder");
179
180 if (source->start() != OK) {
181 SL_LOGE("AacBqToPcmCbRenderer::onPrepare() Failed to start source/decoder.");
182 notifyPrepared(MEDIA_ERROR_BASE);
183 return;
184 }
185
186 //---------------------------------
187 int32_t channelCount;
188 CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
189 int32_t sr;
190 CHECK(meta->findInt32(kKeySampleRate, &sr));
191 // FIXME similar to AudioSfDecoder::onPrepare()
192
193 // already "good to go" (compare to AudioSfDecoder::onPrepare)
194 mCacheStatus = kStatusHigh;
195 mCacheFill = 1000;
196 notifyStatus();
197 notifyCacheFill();
198
199 {
200 android::Mutex::Autolock autoLock(mPcmFormatLock);
201 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = sr;
202 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = channelCount;
203 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] =
204 channelCountToMask(channelCount);
205 }
206 SL_LOGV("AacBqToPcmCbRenderer::onPrepare() channel count=%d SR=%d",
207 channelCount, sr);
208
209 //---------------------------------
210 // The data source, and audio source (a decoder) are ready to be used
211 mDataSource = mBqSource;
212 mAudioSource = source;
213 mAudioSourceStarted = true;
214
215 //-------------------------------------
216 // signal successful completion of prepare
217 mStateFlags |= kFlagPrepared;
218
219 // skipping past AudioToCbRenderer and AudioSfDecoder
220 GenericPlayer::onPrepare();
221
222 SL_LOGD("AacBqToPcmCbRenderer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
223 }
224
225 } // namespace android
226