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 LOG_NDEBUG 0
18 #define LOG_TAG "AACExtractor"
19 #include <utils/Log.h>
20 
21 #include "AACExtractor.h"
22 #include <media/MediaExtractorPluginApi.h>
23 #include <media/stagefright/foundation/ABuffer.h>
24 #include <media/stagefright/foundation/AMessage.h>
25 #include <media/stagefright/foundation/ADebug.h>
26 #include <media/stagefright/MediaBufferGroup.h>
27 #include <media/stagefright/MediaDefs.h>
28 #include <media/stagefright/MediaErrors.h>
29 #include <media/stagefright/MetaDataUtils.h>
30 #include <utils/String8.h>
31 
32 namespace android {
33 
34 class AACSource : public MediaTrackHelper {
35 public:
36     AACSource(
37             DataSourceHelper *source,
38             AMediaFormat *meta,
39             const Vector<uint64_t> &offset_vector,
40             int64_t frame_duration_us);
41 
42     virtual media_status_t start();
43     virtual media_status_t stop();
44 
45     virtual media_status_t getFormat(AMediaFormat*);
46 
47     virtual media_status_t read(
48             MediaBufferHelper **buffer, const ReadOptions *options = NULL);
49 
50 protected:
51     virtual ~AACSource();
52 
53 private:
54     static const size_t kMaxFrameSize;
55     DataSourceHelper *mDataSource;
56     AMediaFormat *mMeta;
57 
58     off64_t mOffset;
59     int64_t mCurrentTimeUs;
60     bool mStarted;
61 
62     Vector<uint64_t> mOffsetVector;
63     int64_t mFrameDurationUs;
64 
65     AACSource(const AACSource &);
66     AACSource &operator=(const AACSource &);
67 };
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 
71 // Returns the sample rate based on the sampling frequency index
get_sample_rate(const uint8_t sf_index)72 uint32_t get_sample_rate(const uint8_t sf_index)
73 {
74     static const uint32_t sample_rates[] =
75     {
76         96000, 88200, 64000, 48000, 44100, 32000,
77         24000, 22050, 16000, 12000, 11025, 8000
78     };
79 
80     if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
81         return sample_rates[sf_index];
82     }
83 
84     return 0;
85 }
86 
87 // Returns the frame length in bytes as described in an ADTS header starting at the given offset,
88 //     or 0 if the size can't be read due to an error in the header or a read failure.
89 // The returned value is the AAC frame size with the ADTS header length (regardless of
90 //     the presence of the CRC).
91 // If headerSize is non-NULL, it will be used to return the size of the header of this ADTS frame.
getAdtsFrameLength(DataSourceHelper * source,off64_t offset,size_t * headerSize)92 static size_t getAdtsFrameLength(DataSourceHelper *source, off64_t offset, size_t* headerSize) {
93 
94     const size_t kAdtsHeaderLengthNoCrc = 7;
95     const size_t kAdtsHeaderLengthWithCrc = 9;
96 
97     size_t frameSize = 0;
98 
99     uint8_t syncword[2];
100     if (source->readAt(offset, &syncword, 2) != 2) {
101         return 0;
102     }
103     if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
104         return 0;
105     }
106 
107     uint8_t protectionAbsent;
108     if (source->readAt(offset + 1, &protectionAbsent, 1) < 1) {
109         return 0;
110     }
111     protectionAbsent &= 0x1;
112 
113     uint8_t header[3];
114     if (source->readAt(offset + 3, &header, 3) < 3) {
115         return 0;
116     }
117 
118     frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
119 
120     // protectionAbsent is 0 if there is CRC
121     size_t headSize = protectionAbsent ? kAdtsHeaderLengthNoCrc : kAdtsHeaderLengthWithCrc;
122     if (headSize > frameSize) {
123         return 0;
124     }
125     if (headerSize != NULL) {
126         *headerSize = headSize;
127     }
128 
129     return frameSize;
130 }
131 
AACExtractor(DataSourceHelper * source,off64_t offset)132 AACExtractor::AACExtractor(
133         DataSourceHelper *source, off64_t offset)
134     : mDataSource(source),
135       mMeta(nullptr),
136       mInitCheck(NO_INIT),
137       mFrameDurationUs(0) {
138 
139     uint8_t profile, sf_index, channel, header[2];
140     if (mDataSource->readAt(offset + 2, &header, 2) < 2) {
141         return;
142     }
143 
144     profile = (header[0] >> 6) & 0x3;
145     sf_index = (header[0] >> 2) & 0xf;
146     uint32_t sr = get_sample_rate(sf_index);
147     if (sr == 0) {
148         return;
149     }
150     channel = (header[0] & 0x1) << 2 | (header[1] >> 6);
151 
152     mMeta = AMediaFormat_new();
153     MakeAACCodecSpecificData(mMeta, profile, sf_index, channel);
154     AMediaFormat_setInt32(mMeta, AMEDIAFORMAT_KEY_AAC_PROFILE, profile + 1);
155 
156     off64_t streamSize, numFrames = 0;
157     size_t frameSize = 0;
158     int64_t duration = 0;
159 
160     if (mDataSource->getSize(&streamSize) == OK) {
161          while (offset < streamSize) {
162             if ((frameSize = getAdtsFrameLength(source, offset, NULL)) == 0) {
163                 ALOGW("prematured AAC stream (%lld vs %lld)",
164                         (long long)offset, (long long)streamSize);
165                 break;
166             }
167 
168             mOffsetVector.push(offset);
169 
170             offset += frameSize;
171             numFrames ++;
172         }
173 
174         // Round up and get the duration
175         mFrameDurationUs = (1024 * 1000000ll + (sr - 1)) / sr;
176         duration = numFrames * mFrameDurationUs;
177         AMediaFormat_setInt64(mMeta, AMEDIAFORMAT_KEY_DURATION, duration);
178     }
179 
180     mInitCheck = OK;
181 }
182 
~AACExtractor()183 AACExtractor::~AACExtractor() {
184     mOffsetVector.clear();
185     delete mDataSource;
186     if (mMeta != nullptr) {
187         AMediaFormat_delete(mMeta);
188     }
189 }
190 
getMetaData(AMediaFormat * meta)191 media_status_t AACExtractor::getMetaData(AMediaFormat *meta) {
192     AMediaFormat_clear(meta);
193     if (mInitCheck == OK) {
194         AMediaFormat_setString(meta, AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
195     }
196 
197     return AMEDIA_OK;
198 }
199 
countTracks()200 size_t AACExtractor::countTracks() {
201     return mInitCheck == OK ? 1 : 0;
202 }
203 
getTrack(size_t index)204 MediaTrackHelper *AACExtractor::getTrack(size_t index) {
205     if (mInitCheck != OK || index != 0) {
206         return NULL;
207     }
208 
209     return new AACSource(mDataSource, mMeta, mOffsetVector, mFrameDurationUs);
210 }
211 
getTrackMetaData(AMediaFormat * meta,size_t index,uint32_t)212 media_status_t AACExtractor::getTrackMetaData(AMediaFormat *meta, size_t index, uint32_t /* flags */) {
213     if (mInitCheck != OK || index != 0) {
214         return AMEDIA_ERROR_UNKNOWN;
215     }
216 
217     return AMediaFormat_copy(meta, mMeta);
218 }
219 
220 ////////////////////////////////////////////////////////////////////////////////
221 
222 // 8192 = 2^13, 13bit AAC frame size (in bytes)
223 const size_t AACSource::kMaxFrameSize = 8192;
224 
AACSource(DataSourceHelper * source,AMediaFormat * meta,const Vector<uint64_t> & offset_vector,int64_t frame_duration_us)225 AACSource::AACSource(
226         DataSourceHelper *source,
227         AMediaFormat *meta,
228         const Vector<uint64_t> &offset_vector,
229         int64_t frame_duration_us)
230     : mDataSource(source),
231       mMeta(meta),
232       mOffset(0),
233       mCurrentTimeUs(0),
234       mStarted(false),
235       mOffsetVector(offset_vector),
236       mFrameDurationUs(frame_duration_us) {
237 }
238 
~AACSource()239 AACSource::~AACSource() {
240     if (mStarted) {
241         stop();
242     }
243 }
244 
start()245 media_status_t AACSource::start() {
246     CHECK(!mStarted);
247 
248     if (mOffsetVector.empty()) {
249         mOffset = 0;
250     } else {
251         mOffset = mOffsetVector.itemAt(0);
252     }
253 
254     mCurrentTimeUs = 0;
255     mBufferGroup->add_buffer(kMaxFrameSize);
256     mStarted = true;
257 
258     return AMEDIA_OK;
259 }
260 
stop()261 media_status_t AACSource::stop() {
262     CHECK(mStarted);
263 
264     mStarted = false;
265     return AMEDIA_OK;
266 }
267 
getFormat(AMediaFormat * meta)268 media_status_t AACSource::getFormat(AMediaFormat *meta) {
269     return AMediaFormat_copy(meta, mMeta);
270 }
271 
read(MediaBufferHelper ** out,const ReadOptions * options)272 media_status_t AACSource::read(
273         MediaBufferHelper **out, const ReadOptions *options) {
274     *out = NULL;
275 
276     int64_t seekTimeUs;
277     ReadOptions::SeekMode mode;
278     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
279         if (mFrameDurationUs > 0) {
280             int64_t seekFrame = seekTimeUs / mFrameDurationUs;
281             if (seekFrame < 0 || seekFrame >= (int64_t)mOffsetVector.size()) {
282                 android_errorWriteLog(0x534e4554, "70239507");
283                 return AMEDIA_ERROR_MALFORMED;
284             }
285             mCurrentTimeUs = seekFrame * mFrameDurationUs;
286 
287             mOffset = mOffsetVector.itemAt(seekFrame);
288         }
289     }
290 
291     size_t frameSize, frameSizeWithoutHeader, headerSize;
292     if ((frameSize = getAdtsFrameLength(mDataSource, mOffset, &headerSize)) == 0) {
293         return AMEDIA_ERROR_END_OF_STREAM;
294     }
295 
296     MediaBufferHelper *buffer;
297     status_t err = mBufferGroup->acquire_buffer(&buffer);
298     if (err != OK) {
299         return AMEDIA_ERROR_UNKNOWN;
300     }
301 
302     frameSizeWithoutHeader = frameSize - headerSize;
303     if (mDataSource->readAt(mOffset + headerSize, buffer->data(),
304                 frameSizeWithoutHeader) != (ssize_t)frameSizeWithoutHeader) {
305         buffer->release();
306         buffer = NULL;
307 
308         return AMEDIA_ERROR_IO;
309     }
310 
311     buffer->set_range(0, frameSizeWithoutHeader);
312     AMediaFormat *meta = buffer->meta_data();
313     AMediaFormat_setInt64(meta, AMEDIAFORMAT_KEY_TIME_US, mCurrentTimeUs);
314     AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_IS_SYNC_FRAME, 1);
315 
316     mOffset += frameSize;
317     mCurrentTimeUs += mFrameDurationUs;
318 
319     *out = buffer;
320     return AMEDIA_OK;
321 }
322 
323 ////////////////////////////////////////////////////////////////////////////////
324 
CreateExtractor(CDataSource * source,void * meta)325 static CMediaExtractor* CreateExtractor(
326         CDataSource *source,
327         void *meta) {
328     off64_t offset = *static_cast<off64_t*>(meta);
329     return wrap(new AACExtractor(new DataSourceHelper(source), offset));
330 }
331 
Sniff(CDataSource * source,float * confidence,void ** meta,FreeMetaFunc * freeMeta)332 static CreatorFunc Sniff(
333         CDataSource *source, float *confidence, void **meta,
334         FreeMetaFunc *freeMeta) {
335     off64_t pos = 0;
336 
337     DataSourceHelper helper(source);
338     for (;;) {
339         uint8_t id3header[10];
340         if (helper.readAt(pos, id3header, sizeof(id3header))
341                 < (ssize_t)sizeof(id3header)) {
342             return NULL;
343         }
344 
345         if (memcmp("ID3", id3header, 3)) {
346             break;
347         }
348 
349         // Skip the ID3v2 header.
350 
351         size_t len =
352             ((id3header[6] & 0x7f) << 21)
353             | ((id3header[7] & 0x7f) << 14)
354             | ((id3header[8] & 0x7f) << 7)
355             | (id3header[9] & 0x7f);
356 
357         len += 10;
358 
359         pos += len;
360 
361         ALOGV("skipped ID3 tag, new starting offset is %lld (0x%016llx)",
362                 (long long)pos, (long long)pos);
363     }
364 
365     uint8_t header[2];
366 
367     if (helper.readAt(pos, &header, 2) != 2) {
368         return NULL;
369     }
370 
371     // ADTS syncword
372     if ((header[0] == 0xff) && ((header[1] & 0xf6) == 0xf0)) {
373         *confidence = 0.2;
374 
375         off64_t *offPtr = (off64_t*) malloc(sizeof(off64_t));
376         *offPtr = pos;
377         *meta = offPtr;
378         *freeMeta = ::free;
379 
380         return CreateExtractor;
381     }
382 
383     return NULL;
384 }
385 
386 static const char *extensions[] = {
387     "aac",
388     NULL
389 };
390 
391 extern "C" {
392 // This is the only symbol that needs to be exported
393 __attribute__ ((visibility ("default")))
GETEXTRACTORDEF()394 ExtractorDef GETEXTRACTORDEF() {
395     return {
396         EXTRACTORDEF_VERSION,
397         UUID("4fd80eae-03d2-4d72-9eb9-48fa6bb54613"),
398         1, // version
399         "AAC Extractor",
400         { .v3 = {Sniff, extensions} },
401     };
402 }
403 
404 } // extern "C"
405 
406 } // namespace android
407