• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SimpleDecodingSource.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] = SL_ANDROID_UNKNOWN_CHANNELMASK;
147     }
148 
149     sp<AacAdtsExtractor> 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 
161     // the audio content is not raw PCM, so we need a decoder
162     source = SimpleDecodingSource::Create(source);
163 
164     if (source == NULL) {
165         SL_LOGE("AacBqToPcmCbRenderer::onPrepare: Could not instantiate decoder.");
166         notifyPrepared(ERROR_UNSUPPORTED);
167         return;
168     }
169 
170     sp<MetaData> meta = source->getFormat();
171 
172     SL_LOGD("AacBqToPcmCbRenderer::onPrepare() after instantiating decoder");
173 
174     if (source->start() != OK) {
175         SL_LOGE("AacBqToPcmCbRenderer::onPrepare() Failed to start source/decoder.");
176         notifyPrepared(MEDIA_ERROR_BASE);
177         return;
178     }
179 
180     //---------------------------------
181     int32_t channelCount;
182     CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
183     int32_t sr;
184     CHECK(meta->findInt32(kKeySampleRate, &sr));
185     // FIXME similar to AudioSfDecoder::onPrepare()
186 
187     // already "good to go" (compare to AudioSfDecoder::onPrepare)
188     mCacheStatus = kStatusHigh;
189     mCacheFill = 1000;
190     notifyStatus();
191     notifyCacheFill();
192 
193     {
194         android::Mutex::Autolock autoLock(mPcmFormatLock);
195         mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = sr;
196         mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = channelCount;
197         mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] =
198                 sles_channel_out_mask_from_count(channelCount);
199     }
200     SL_LOGV("AacBqToPcmCbRenderer::onPrepare() channel count=%d SR=%d",
201             channelCount, sr);
202 
203     //---------------------------------
204     // The data source, and audio source (a decoder) are ready to be used
205     mDataSource = mBqSource;
206     mAudioSource = source;
207     mAudioSourceStarted = true;
208 
209     //-------------------------------------
210     // signal successful completion of prepare
211     mStateFlags |= kFlagPrepared;
212 
213     // skipping past AudioToCbRenderer and AudioSfDecoder
214     GenericPlayer::onPrepare();
215 
216     SL_LOGD("AacBqToPcmCbRenderer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
217 }
218 
219 } // namespace android
220