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