blob: 9e4687643029fcdf85214c0dbd6decb9d24ab63d [file] [log] [blame]
Gloria Wang50c44c72011-02-02 14:12:49 -08001/*
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 "include/AACExtractor.h"
22#include "include/avc_utils.h"
23
24#include <media/stagefright/foundation/ABuffer.h>
Andreas Huber14da7362011-11-01 11:01:01 -070025#include <media/stagefright/foundation/AMessage.h>
James Dongf1d5aa12012-02-06 23:46:37 -080026#include <media/stagefright/foundation/ADebug.h>
Gloria Wang50c44c72011-02-02 14:12:49 -080027#include <media/stagefright/DataSource.h>
28#include <media/stagefright/MediaBufferGroup.h>
Gloria Wang50c44c72011-02-02 14:12:49 -080029#include <media/stagefright/MediaDefs.h>
30#include <media/stagefright/MediaErrors.h>
31#include <media/stagefright/MediaSource.h>
32#include <media/stagefright/MetaData.h>
33#include <utils/String8.h>
34
35namespace android {
36
Gloria Wang50c44c72011-02-02 14:12:49 -080037class AACSource : public MediaSource {
38public:
39 AACSource(const sp<DataSource> &source,
40 const sp<MetaData> &meta,
41 const Vector<uint64_t> &offset_vector,
42 int64_t frame_duration_us);
43
44 virtual status_t start(MetaData *params = NULL);
45 virtual status_t stop();
46
47 virtual sp<MetaData> getFormat();
48
49 virtual status_t read(
50 MediaBuffer **buffer, const ReadOptions *options = NULL);
51
52protected:
53 virtual ~AACSource();
54
55private:
56 static const size_t kMaxFrameSize;
57 sp<DataSource> mDataSource;
58 sp<MetaData> 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
75uint32_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
Jean-Michel Trivibf4c48b2011-08-29 14:06:51 -070090// 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.
95static size_t getAdtsFrameLength(const sp<DataSource> &source, off64_t offset, size_t* headerSize) {
96
97 const size_t kAdtsHeaderLengthNoCrc = 7;
98 const size_t kAdtsHeaderLengthWithCrc = 9;
99
Gloria Wang50c44c72011-02-02 14:12:49 -0800100 size_t frameSize = 0;
101
102 uint8_t syncword[2];
Gloria Wangfdb04b62011-08-03 15:06:35 -0700103 if (source->readAt(offset, &syncword, 2) != 2) {
Gloria Wang50c44c72011-02-02 14:12:49 -0800104 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;
Jean-Michel Trivibf4c48b2011-08-29 14:06:51 -0700122
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 }
Gloria Wang50c44c72011-02-02 14:12:49 -0800131
132 return frameSize;
133}
134
Andreas Huber14da7362011-11-01 11:01:01 -0700135AACExtractor::AACExtractor(
136 const sp<DataSource> &source, const sp<AMessage> &_meta)
Gloria Wang50c44c72011-02-02 14:12:49 -0800137 : mDataSource(source),
138 mInitCheck(NO_INIT),
139 mFrameDurationUs(0) {
Andreas Huber14da7362011-11-01 11:01:01 -0700140 sp<AMessage> meta = _meta;
141
142 if (meta == NULL) {
143 String8 mimeType;
144 float confidence;
145 sp<AMessage> _meta;
146
147 if (!SniffAAC(mDataSource, &mimeType, &confidence, &meta)) {
148 return;
149 }
Gloria Wang50c44c72011-02-02 14:12:49 -0800150 }
151
Andreas Huber14da7362011-11-01 11:01:01 -0700152 int64_t offset;
153 CHECK(meta->findInt64("offset", &offset));
154
Gloria Wang50c44c72011-02-02 14:12:49 -0800155 uint8_t profile, sf_index, channel, header[2];
Andreas Huber14da7362011-11-01 11:01:01 -0700156 if (mDataSource->readAt(offset + 2, &header, 2) < 2) {
Gloria Wang50c44c72011-02-02 14:12:49 -0800157 return;
158 }
159
160 profile = (header[0] >> 6) & 0x3;
161 sf_index = (header[0] >> 2) & 0xf;
162 uint32_t sr = get_sample_rate(sf_index);
163 if (sr == 0) {
164 return;
165 }
166 channel = (header[0] & 0x1) << 2 | (header[1] >> 6);
167
168 mMeta = MakeAACCodecSpecificData(profile, sf_index, channel);
169
Gloria Wang50c44c72011-02-02 14:12:49 -0800170 off64_t streamSize, numFrames = 0;
171 size_t frameSize = 0;
172 int64_t duration = 0;
173
174 if (mDataSource->getSize(&streamSize) == OK) {
175 while (offset < streamSize) {
Jean-Michel Trivibf4c48b2011-08-29 14:06:51 -0700176 if ((frameSize = getAdtsFrameLength(source, offset, NULL)) == 0) {
Gloria Wang50c44c72011-02-02 14:12:49 -0800177 return;
178 }
179
180 mOffsetVector.push(offset);
181
182 offset += frameSize;
183 numFrames ++;
184 }
185
186 // Round up and get the duration
187 mFrameDurationUs = (1024 * 1000000ll + (sr - 1)) / sr;
188 duration = numFrames * mFrameDurationUs;
189 mMeta->setInt64(kKeyDuration, duration);
190 }
191
192 mInitCheck = OK;
193}
194
195AACExtractor::~AACExtractor() {
196}
197
198sp<MetaData> AACExtractor::getMetaData() {
199 sp<MetaData> meta = new MetaData;
200
201 if (mInitCheck != OK) {
202 return meta;
203 }
204
205 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
206
207 return meta;
208}
209
210size_t AACExtractor::countTracks() {
211 return mInitCheck == OK ? 1 : 0;
212}
213
214sp<MediaSource> AACExtractor::getTrack(size_t index) {
215 if (mInitCheck != OK || index != 0) {
216 return NULL;
217 }
218
219 return new AACSource(mDataSource, mMeta, mOffsetVector, mFrameDurationUs);
220}
221
Andreas Huber84333e02014-02-07 15:36:10 -0800222sp<MetaData> AACExtractor::getTrackMetaData(size_t index, uint32_t /* flags */) {
Gloria Wang50c44c72011-02-02 14:12:49 -0800223 if (mInitCheck != OK || index != 0) {
224 return NULL;
225 }
226
227 return mMeta;
228}
229
230////////////////////////////////////////////////////////////////////////////////
231
232// 8192 = 2^13, 13bit AAC frame size (in bytes)
233const size_t AACSource::kMaxFrameSize = 8192;
234
235AACSource::AACSource(
236 const sp<DataSource> &source, const sp<MetaData> &meta,
237 const Vector<uint64_t> &offset_vector,
238 int64_t frame_duration_us)
239 : mDataSource(source),
240 mMeta(meta),
241 mOffset(0),
242 mCurrentTimeUs(0),
243 mStarted(false),
244 mGroup(NULL),
245 mOffsetVector(offset_vector),
246 mFrameDurationUs(frame_duration_us) {
247}
248
249AACSource::~AACSource() {
250 if (mStarted) {
251 stop();
252 }
253}
254
Andreas Huber84333e02014-02-07 15:36:10 -0800255status_t AACSource::start(MetaData * /* params */) {
Gloria Wang50c44c72011-02-02 14:12:49 -0800256 CHECK(!mStarted);
257
Andreas Huber14da7362011-11-01 11:01:01 -0700258 if (mOffsetVector.empty()) {
259 mOffset = 0;
260 } else {
261 mOffset = mOffsetVector.itemAt(0);
262 }
263
Gloria Wang50c44c72011-02-02 14:12:49 -0800264 mCurrentTimeUs = 0;
265 mGroup = new MediaBufferGroup;
266 mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
267 mStarted = true;
268
269 return OK;
270}
271
272status_t AACSource::stop() {
273 CHECK(mStarted);
274
275 delete mGroup;
276 mGroup = NULL;
277
278 mStarted = false;
279 return OK;
280}
281
282sp<MetaData> AACSource::getFormat() {
283 return mMeta;
284}
285
286status_t AACSource::read(
287 MediaBuffer **out, const ReadOptions *options) {
288 *out = NULL;
289
290 int64_t seekTimeUs;
291 ReadOptions::SeekMode mode;
292 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
293 if (mFrameDurationUs > 0) {
294 int64_t seekFrame = seekTimeUs / mFrameDurationUs;
Robert Shih8b0f0da2018-01-11 14:38:23 -0800295 if (seekFrame < 0 || seekFrame >= (int64_t)mOffsetVector.size()) {
296 android_errorWriteLog(0x534e4554, "70239507");
297 return ERROR_MALFORMED;
298 }
Gloria Wang50c44c72011-02-02 14:12:49 -0800299 mCurrentTimeUs = seekFrame * mFrameDurationUs;
300
301 mOffset = mOffsetVector.itemAt(seekFrame);
302 }
303 }
304
Jean-Michel Trivibf4c48b2011-08-29 14:06:51 -0700305 size_t frameSize, frameSizeWithoutHeader, headerSize;
306 if ((frameSize = getAdtsFrameLength(mDataSource, mOffset, &headerSize)) == 0) {
Gloria Wang50c44c72011-02-02 14:12:49 -0800307 return ERROR_END_OF_STREAM;
308 }
309
310 MediaBuffer *buffer;
311 status_t err = mGroup->acquire_buffer(&buffer);
312 if (err != OK) {
313 return err;
314 }
315
Jean-Michel Trivibf4c48b2011-08-29 14:06:51 -0700316 frameSizeWithoutHeader = frameSize - headerSize;
317 if (mDataSource->readAt(mOffset + headerSize, buffer->data(),
Gloria Wang50c44c72011-02-02 14:12:49 -0800318 frameSizeWithoutHeader) != (ssize_t)frameSizeWithoutHeader) {
319 buffer->release();
320 buffer = NULL;
321
322 return ERROR_IO;
323 }
324
325 buffer->set_range(0, frameSizeWithoutHeader);
326 buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
327 buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
328
329 mOffset += frameSize;
330 mCurrentTimeUs += mFrameDurationUs;
331
332 *out = buffer;
333 return OK;
334}
335
336////////////////////////////////////////////////////////////////////////////////
337
338bool SniffAAC(
339 const sp<DataSource> &source, String8 *mimeType, float *confidence,
Andreas Huber14da7362011-11-01 11:01:01 -0700340 sp<AMessage> *meta) {
341 off64_t pos = 0;
342
343 for (;;) {
344 uint8_t id3header[10];
345 if (source->readAt(pos, id3header, sizeof(id3header))
346 < (ssize_t)sizeof(id3header)) {
347 return false;
348 }
349
350 if (memcmp("ID3", id3header, 3)) {
351 break;
352 }
353
354 // Skip the ID3v2 header.
355
356 size_t len =
357 ((id3header[6] & 0x7f) << 21)
358 | ((id3header[7] & 0x7f) << 14)
359 | ((id3header[8] & 0x7f) << 7)
360 | (id3header[9] & 0x7f);
361
362 len += 10;
363
364 pos += len;
365
366 ALOGV("skipped ID3 tag, new starting offset is %lld (0x%016llx)",
Lajos Molnaree4e1b12015-04-17 13:46:19 -0700367 (long long)pos, (long long)pos);
Andreas Huber14da7362011-11-01 11:01:01 -0700368 }
369
Gloria Wang50c44c72011-02-02 14:12:49 -0800370 uint8_t header[2];
371
Andreas Huber14da7362011-11-01 11:01:01 -0700372 if (source->readAt(pos, &header, 2) != 2) {
Gloria Wang50c44c72011-02-02 14:12:49 -0800373 return false;
374 }
375
376 // ADTS syncword
377 if ((header[0] == 0xff) && ((header[1] & 0xf6) == 0xf0)) {
378 *mimeType = MEDIA_MIMETYPE_AUDIO_AAC_ADTS;
379 *confidence = 0.2;
Andreas Huber14da7362011-11-01 11:01:01 -0700380
381 *meta = new AMessage;
382 (*meta)->setInt64("offset", pos);
383
Gloria Wang50c44c72011-02-02 14:12:49 -0800384 return true;
385 }
386
387 return false;
388}
389
390} // namespace android