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/DataSourceBase.h>
23 #include <media/MediaTrack.h>
24 #include <media/stagefright/foundation/ABuffer.h>
25 #include <media/stagefright/foundation/AMessage.h>
26 #include <media/stagefright/foundation/ADebug.h>
27 #include <media/stagefright/MediaBufferGroup.h>
28 #include <media/stagefright/MediaDefs.h>
29 #include <media/stagefright/MediaErrors.h>
30 #include <media/stagefright/MetaData.h>
31 #include <media/stagefright/MetaDataUtils.h>
32 #include <utils/String8.h>
33
34 namespace android {
35
36 class AACSource : public MediaTrack {
37 public:
38 AACSource(
39 DataSourceBase *source,
40 MetaDataBase &meta,
41 const Vector<uint64_t> &offset_vector,
42 int64_t frame_duration_us);
43
44 virtual status_t start(MetaDataBase *params = NULL);
45 virtual status_t stop();
46
47 virtual status_t getFormat(MetaDataBase&);
48
49 virtual status_t read(
50 MediaBufferBase **buffer, const ReadOptions *options = NULL);
51
52 protected:
53 virtual ~AACSource();
54
55 private:
56 static const size_t kMaxFrameSize;
57 DataSourceBase *mDataSource;
58 MetaDataBase mMeta;
59
60 off64_t mOffset;
61 int64_t mCurrentTimeUs;
62 bool mStarted;
63 MediaBufferGroup *mGroup;
64
65 Vector<uint64_t> mOffsetVector;
66 int64_t mFrameDurationUs;
67
68 AACSource(const AACSource &);
69 AACSource &operator=(const AACSource &);
70 };
71
72 ////////////////////////////////////////////////////////////////////////////////
73
74 // Returns the sample rate based on the sampling frequency index
get_sample_rate(const uint8_t sf_index)75 uint32_t get_sample_rate(const uint8_t sf_index)
76 {
77 static const uint32_t sample_rates[] =
78 {
79 96000, 88200, 64000, 48000, 44100, 32000,
80 24000, 22050, 16000, 12000, 11025, 8000
81 };
82
83 if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
84 return sample_rates[sf_index];
85 }
86
87 return 0;
88 }
89
90 // Returns the frame length in bytes as described in an ADTS header starting at the given offset,
91 // or 0 if the size can't be read due to an error in the header or a read failure.
92 // The returned value is the AAC frame size with the ADTS header length (regardless of
93 // the presence of the CRC).
94 // If headerSize is non-NULL, it will be used to return the size of the header of this ADTS frame.
getAdtsFrameLength(DataSourceBase * source,off64_t offset,size_t * headerSize)95 static size_t getAdtsFrameLength(DataSourceBase *source, off64_t offset, size_t* headerSize) {
96
97 const size_t kAdtsHeaderLengthNoCrc = 7;
98 const size_t kAdtsHeaderLengthWithCrc = 9;
99
100 size_t frameSize = 0;
101
102 uint8_t syncword[2];
103 if (source->readAt(offset, &syncword, 2) != 2) {
104 return 0;
105 }
106 if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
107 return 0;
108 }
109
110 uint8_t protectionAbsent;
111 if (source->readAt(offset + 1, &protectionAbsent, 1) < 1) {
112 return 0;
113 }
114 protectionAbsent &= 0x1;
115
116 uint8_t header[3];
117 if (source->readAt(offset + 3, &header, 3) < 3) {
118 return 0;
119 }
120
121 frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
122
123 // protectionAbsent is 0 if there is CRC
124 size_t headSize = protectionAbsent ? kAdtsHeaderLengthNoCrc : kAdtsHeaderLengthWithCrc;
125 if (headSize > frameSize) {
126 return 0;
127 }
128 if (headerSize != NULL) {
129 *headerSize = headSize;
130 }
131
132 return frameSize;
133 }
134
AACExtractor(DataSourceBase * source,off64_t offset)135 AACExtractor::AACExtractor(
136 DataSourceBase *source, off64_t offset)
137 : mDataSource(source),
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 MakeAACCodecSpecificData(mMeta, profile, sf_index, channel);
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 mMeta.setInt64(kKeyDuration, duration);
178 }
179
180 mInitCheck = OK;
181 }
182
~AACExtractor()183 AACExtractor::~AACExtractor() {
184 }
185
getMetaData(MetaDataBase & meta)186 status_t AACExtractor::getMetaData(MetaDataBase &meta) {
187 meta.clear();
188 if (mInitCheck == OK) {
189 meta.setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
190 }
191
192 return OK;
193 }
194
countTracks()195 size_t AACExtractor::countTracks() {
196 return mInitCheck == OK ? 1 : 0;
197 }
198
getTrack(size_t index)199 MediaTrack *AACExtractor::getTrack(size_t index) {
200 if (mInitCheck != OK || index != 0) {
201 return NULL;
202 }
203
204 return new AACSource(mDataSource, mMeta, mOffsetVector, mFrameDurationUs);
205 }
206
getTrackMetaData(MetaDataBase & meta,size_t index,uint32_t)207 status_t AACExtractor::getTrackMetaData(MetaDataBase &meta, size_t index, uint32_t /* flags */) {
208 if (mInitCheck != OK || index != 0) {
209 return UNKNOWN_ERROR;
210 }
211
212 meta = mMeta;
213 return OK;
214 }
215
216 ////////////////////////////////////////////////////////////////////////////////
217
218 // 8192 = 2^13, 13bit AAC frame size (in bytes)
219 const size_t AACSource::kMaxFrameSize = 8192;
220
AACSource(DataSourceBase * source,MetaDataBase & meta,const Vector<uint64_t> & offset_vector,int64_t frame_duration_us)221 AACSource::AACSource(
222 DataSourceBase *source,
223 MetaDataBase &meta,
224 const Vector<uint64_t> &offset_vector,
225 int64_t frame_duration_us)
226 : mDataSource(source),
227 mMeta(meta),
228 mOffset(0),
229 mCurrentTimeUs(0),
230 mStarted(false),
231 mGroup(NULL),
232 mOffsetVector(offset_vector),
233 mFrameDurationUs(frame_duration_us) {
234 }
235
~AACSource()236 AACSource::~AACSource() {
237 if (mStarted) {
238 stop();
239 }
240 }
241
start(MetaDataBase *)242 status_t AACSource::start(MetaDataBase * /* params */) {
243 CHECK(!mStarted);
244
245 if (mOffsetVector.empty()) {
246 mOffset = 0;
247 } else {
248 mOffset = mOffsetVector.itemAt(0);
249 }
250
251 mCurrentTimeUs = 0;
252 mGroup = new MediaBufferGroup;
253 mGroup->add_buffer(MediaBufferBase::Create(kMaxFrameSize));
254 mStarted = true;
255
256 return OK;
257 }
258
stop()259 status_t AACSource::stop() {
260 CHECK(mStarted);
261
262 delete mGroup;
263 mGroup = NULL;
264
265 mStarted = false;
266 return OK;
267 }
268
getFormat(MetaDataBase & meta)269 status_t AACSource::getFormat(MetaDataBase &meta) {
270 meta = mMeta;
271 return OK;
272 }
273
read(MediaBufferBase ** out,const ReadOptions * options)274 status_t AACSource::read(
275 MediaBufferBase **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 = seekTimeUs / mFrameDurationUs;
283 if (seekFrame < 0 || seekFrame >= (int64_t)mOffsetVector.size()) {
284 android_errorWriteLog(0x534e4554, "70239507");
285 return ERROR_MALFORMED;
286 }
287 mCurrentTimeUs = seekFrame * mFrameDurationUs;
288
289 mOffset = mOffsetVector.itemAt(seekFrame);
290 }
291 }
292
293 size_t frameSize, frameSizeWithoutHeader, headerSize;
294 if ((frameSize = getAdtsFrameLength(mDataSource, mOffset, &headerSize)) == 0) {
295 return ERROR_END_OF_STREAM;
296 }
297
298 MediaBufferBase *buffer;
299 status_t err = mGroup->acquire_buffer(&buffer);
300 if (err != OK) {
301 return err;
302 }
303
304 frameSizeWithoutHeader = frameSize - headerSize;
305 if (mDataSource->readAt(mOffset + headerSize, buffer->data(),
306 frameSizeWithoutHeader) != (ssize_t)frameSizeWithoutHeader) {
307 buffer->release();
308 buffer = NULL;
309
310 return ERROR_IO;
311 }
312
313 buffer->set_range(0, frameSizeWithoutHeader);
314 buffer->meta_data().setInt64(kKeyTime, mCurrentTimeUs);
315 buffer->meta_data().setInt32(kKeyIsSyncFrame, 1);
316
317 mOffset += frameSize;
318 mCurrentTimeUs += mFrameDurationUs;
319
320 *out = buffer;
321 return OK;
322 }
323
324 ////////////////////////////////////////////////////////////////////////////////
325
CreateExtractor(DataSourceBase * source,void * meta)326 static MediaExtractor* CreateExtractor(
327 DataSourceBase *source,
328 void *meta) {
329 off64_t offset = *static_cast<off64_t*>(meta);
330 return new AACExtractor(source, offset);
331 }
332
Sniff(DataSourceBase * source,float * confidence,void ** meta,MediaExtractor::FreeMetaFunc * freeMeta)333 static MediaExtractor::CreatorFunc Sniff(
334 DataSourceBase *source, float *confidence, void **meta,
335 MediaExtractor::FreeMetaFunc *freeMeta) {
336 off64_t pos = 0;
337
338 for (;;) {
339 uint8_t id3header[10];
340 if (source->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 (source->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
387 extern "C" {
388 // This is the only symbol that needs to be exported
389 __attribute__ ((visibility ("default")))
GETEXTRACTORDEF()390 MediaExtractor::ExtractorDef GETEXTRACTORDEF() {
391 return {
392 MediaExtractor::EXTRACTORDEF_VERSION,
393 UUID("4fd80eae-03d2-4d72-9eb9-48fa6bb54613"),
394 1, // version
395 "AAC Extractor",
396 Sniff
397 };
398 }
399
400 } // extern "C"
401
402 } // namespace android
403