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 if (mMeta != nullptr) {
185 AMediaFormat_delete(mMeta);
186 }
187 }
188
getMetaData(AMediaFormat * meta)189 media_status_t AACExtractor::getMetaData(AMediaFormat *meta) {
190 AMediaFormat_clear(meta);
191 if (mInitCheck == OK) {
192 AMediaFormat_setString(meta, AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
193 }
194
195 return AMEDIA_OK;
196 }
197
countTracks()198 size_t AACExtractor::countTracks() {
199 return mInitCheck == OK ? 1 : 0;
200 }
201
getTrack(size_t index)202 MediaTrackHelper *AACExtractor::getTrack(size_t index) {
203 if (mInitCheck != OK || index != 0) {
204 return NULL;
205 }
206
207 return new AACSource(mDataSource, mMeta, mOffsetVector, mFrameDurationUs);
208 }
209
getTrackMetaData(AMediaFormat * meta,size_t index,uint32_t)210 media_status_t AACExtractor::getTrackMetaData(AMediaFormat *meta, size_t index, uint32_t /* flags */) {
211 if (mInitCheck != OK || index != 0) {
212 return AMEDIA_ERROR_UNKNOWN;
213 }
214
215 return AMediaFormat_copy(meta, mMeta);
216 }
217
218 ////////////////////////////////////////////////////////////////////////////////
219
220 // 8192 = 2^13, 13bit AAC frame size (in bytes)
221 const size_t AACSource::kMaxFrameSize = 8192;
222
AACSource(DataSourceHelper * source,AMediaFormat * meta,const Vector<uint64_t> & offset_vector,int64_t frame_duration_us)223 AACSource::AACSource(
224 DataSourceHelper *source,
225 AMediaFormat *meta,
226 const Vector<uint64_t> &offset_vector,
227 int64_t frame_duration_us)
228 : mDataSource(source),
229 mMeta(meta),
230 mOffset(0),
231 mCurrentTimeUs(0),
232 mStarted(false),
233 mOffsetVector(offset_vector),
234 mFrameDurationUs(frame_duration_us) {
235 }
236
~AACSource()237 AACSource::~AACSource() {
238 if (mStarted) {
239 stop();
240 }
241 }
242
start()243 media_status_t AACSource::start() {
244 CHECK(!mStarted);
245
246 if (mOffsetVector.empty()) {
247 mOffset = 0;
248 } else {
249 mOffset = mOffsetVector.itemAt(0);
250 }
251
252 mCurrentTimeUs = 0;
253 mBufferGroup->add_buffer(kMaxFrameSize);
254 mStarted = true;
255
256 return AMEDIA_OK;
257 }
258
stop()259 media_status_t AACSource::stop() {
260 CHECK(mStarted);
261
262 mStarted = false;
263 return AMEDIA_OK;
264 }
265
getFormat(AMediaFormat * meta)266 media_status_t AACSource::getFormat(AMediaFormat *meta) {
267 return AMediaFormat_copy(meta, mMeta);
268 }
269
read(MediaBufferHelper ** out,const ReadOptions * options)270 media_status_t AACSource::read(
271 MediaBufferHelper **out, const ReadOptions *options) {
272 *out = NULL;
273
274 int64_t seekTimeUs;
275 ReadOptions::SeekMode mode;
276 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
277 if (mFrameDurationUs > 0) {
278 int64_t seekFrame = seekTimeUs / mFrameDurationUs;
279 if (seekFrame < 0 || seekFrame >= (int64_t)mOffsetVector.size()) {
280 android_errorWriteLog(0x534e4554, "70239507");
281 return AMEDIA_ERROR_MALFORMED;
282 }
283 mCurrentTimeUs = seekFrame * mFrameDurationUs;
284
285 mOffset = mOffsetVector.itemAt(seekFrame);
286 }
287 }
288
289 size_t frameSize, frameSizeWithoutHeader, headerSize;
290 if ((frameSize = getAdtsFrameLength(mDataSource, mOffset, &headerSize)) == 0) {
291 return AMEDIA_ERROR_END_OF_STREAM;
292 }
293
294 MediaBufferHelper *buffer;
295 status_t err = mBufferGroup->acquire_buffer(&buffer);
296 if (err != OK) {
297 return AMEDIA_ERROR_UNKNOWN;
298 }
299
300 frameSizeWithoutHeader = frameSize - headerSize;
301 if (mDataSource->readAt(mOffset + headerSize, buffer->data(),
302 frameSizeWithoutHeader) != (ssize_t)frameSizeWithoutHeader) {
303 buffer->release();
304 buffer = NULL;
305
306 return AMEDIA_ERROR_IO;
307 }
308
309 buffer->set_range(0, frameSizeWithoutHeader);
310 AMediaFormat *meta = buffer->meta_data();
311 AMediaFormat_setInt64(meta, AMEDIAFORMAT_KEY_TIME_US, mCurrentTimeUs);
312 AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_IS_SYNC_FRAME, 1);
313
314 mOffset += frameSize;
315 mCurrentTimeUs += mFrameDurationUs;
316
317 *out = buffer;
318 return AMEDIA_OK;
319 }
320
321 ////////////////////////////////////////////////////////////////////////////////
322
CreateExtractor(CDataSource * source,void * meta)323 static CMediaExtractor* CreateExtractor(
324 CDataSource *source,
325 void *meta) {
326 off64_t offset = *static_cast<off64_t*>(meta);
327 return wrap(new AACExtractor(new DataSourceHelper(source), offset));
328 }
329
Sniff(CDataSource * source,float * confidence,void ** meta,FreeMetaFunc * freeMeta)330 static CreatorFunc Sniff(
331 CDataSource *source, float *confidence, void **meta,
332 FreeMetaFunc *freeMeta) {
333 off64_t pos = 0;
334
335 DataSourceHelper helper(source);
336 for (;;) {
337 uint8_t id3header[10];
338 if (helper.readAt(pos, id3header, sizeof(id3header))
339 < (ssize_t)sizeof(id3header)) {
340 return NULL;
341 }
342
343 if (memcmp("ID3", id3header, 3)) {
344 break;
345 }
346
347 // Skip the ID3v2 header.
348
349 size_t len =
350 ((id3header[6] & 0x7f) << 21)
351 | ((id3header[7] & 0x7f) << 14)
352 | ((id3header[8] & 0x7f) << 7)
353 | (id3header[9] & 0x7f);
354
355 len += 10;
356
357 pos += len;
358
359 ALOGV("skipped ID3 tag, new starting offset is %lld (0x%016llx)",
360 (long long)pos, (long long)pos);
361 }
362
363 uint8_t header[2];
364
365 if (helper.readAt(pos, &header, 2) != 2) {
366 return NULL;
367 }
368
369 // ADTS syncword
370 if ((header[0] == 0xff) && ((header[1] & 0xf6) == 0xf0)) {
371 *confidence = 0.2;
372
373 off64_t *offPtr = (off64_t*) malloc(sizeof(off64_t));
374 *offPtr = pos;
375 *meta = offPtr;
376 *freeMeta = ::free;
377
378 return CreateExtractor;
379 }
380
381 return NULL;
382 }
383
384 static const char *extensions[] = {
385 "aac",
386 NULL
387 };
388
389 extern "C" {
390 // This is the only symbol that needs to be exported
391 __attribute__ ((visibility ("default")))
GETEXTRACTORDEF()392 ExtractorDef GETEXTRACTORDEF() {
393 return {
394 EXTRACTORDEF_VERSION,
395 UUID("4fd80eae-03d2-4d72-9eb9-48fa6bb54613"),
396 1, // version
397 "AAC Extractor",
398 { .v3 = {Sniff, extensions} },
399 };
400 }
401
402 } // extern "C"
403
404 } // namespace android
405