Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 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 "include/AACExtractor.h" |
| 22 | #include "include/avc_utils.h" |
| 23 | |
| 24 | #include <media/stagefright/foundation/ABuffer.h> |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 25 | #include <media/stagefright/foundation/AMessage.h> |
James Dong | f1d5aa1 | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 26 | #include <media/stagefright/foundation/ADebug.h> |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 27 | #include <media/stagefright/DataSource.h> |
| 28 | #include <media/stagefright/MediaBufferGroup.h> |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 29 | #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 | |
| 35 | namespace android { |
| 36 | |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 37 | class AACSource : public MediaSource { |
| 38 | public: |
| 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 | |
| 52 | protected: |
| 53 | virtual ~AACSource(); |
| 54 | |
| 55 | private: |
| 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 |
| 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 | |
Jean-Michel Trivi | bf4c48b | 2011-08-29 14:06:51 -0700 | [diff] [blame] | 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. |
| 95 | static 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 Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 100 | size_t frameSize = 0; |
| 101 | |
| 102 | uint8_t syncword[2]; |
Gloria Wang | fdb04b6 | 2011-08-03 15:06:35 -0700 | [diff] [blame] | 103 | if (source->readAt(offset, &syncword, 2) != 2) { |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 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; |
Jean-Michel Trivi | bf4c48b | 2011-08-29 14:06:51 -0700 | [diff] [blame] | 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 | } |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 131 | |
| 132 | return frameSize; |
| 133 | } |
| 134 | |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 135 | AACExtractor::AACExtractor( |
| 136 | const sp<DataSource> &source, const sp<AMessage> &_meta) |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 137 | : mDataSource(source), |
| 138 | mInitCheck(NO_INIT), |
| 139 | mFrameDurationUs(0) { |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 140 | 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 Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 152 | int64_t offset; |
| 153 | CHECK(meta->findInt64("offset", &offset)); |
| 154 | |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 155 | uint8_t profile, sf_index, channel, header[2]; |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 156 | if (mDataSource->readAt(offset + 2, &header, 2) < 2) { |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 157 | 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 Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 170 | 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 Trivi | bf4c48b | 2011-08-29 14:06:51 -0700 | [diff] [blame] | 176 | if ((frameSize = getAdtsFrameLength(source, offset, NULL)) == 0) { |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 177 | 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 | |
| 195 | AACExtractor::~AACExtractor() { |
| 196 | } |
| 197 | |
| 198 | sp<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 | |
| 210 | size_t AACExtractor::countTracks() { |
| 211 | return mInitCheck == OK ? 1 : 0; |
| 212 | } |
| 213 | |
| 214 | sp<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 Huber | 84333e0 | 2014-02-07 15:36:10 -0800 | [diff] [blame] | 222 | sp<MetaData> AACExtractor::getTrackMetaData(size_t index, uint32_t /* flags */) { |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 223 | 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) |
| 233 | const size_t AACSource::kMaxFrameSize = 8192; |
| 234 | |
| 235 | AACSource::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 | |
| 249 | AACSource::~AACSource() { |
| 250 | if (mStarted) { |
| 251 | stop(); |
| 252 | } |
| 253 | } |
| 254 | |
Andreas Huber | 84333e0 | 2014-02-07 15:36:10 -0800 | [diff] [blame] | 255 | status_t AACSource::start(MetaData * /* params */) { |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 256 | CHECK(!mStarted); |
| 257 | |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 258 | if (mOffsetVector.empty()) { |
| 259 | mOffset = 0; |
| 260 | } else { |
| 261 | mOffset = mOffsetVector.itemAt(0); |
| 262 | } |
| 263 | |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 264 | mCurrentTimeUs = 0; |
| 265 | mGroup = new MediaBufferGroup; |
| 266 | mGroup->add_buffer(new MediaBuffer(kMaxFrameSize)); |
| 267 | mStarted = true; |
| 268 | |
| 269 | return OK; |
| 270 | } |
| 271 | |
| 272 | status_t AACSource::stop() { |
| 273 | CHECK(mStarted); |
| 274 | |
| 275 | delete mGroup; |
| 276 | mGroup = NULL; |
| 277 | |
| 278 | mStarted = false; |
| 279 | return OK; |
| 280 | } |
| 281 | |
| 282 | sp<MetaData> AACSource::getFormat() { |
| 283 | return mMeta; |
| 284 | } |
| 285 | |
| 286 | status_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 Shih | 8b0f0da | 2018-01-11 14:38:23 -0800 | [diff] [blame] | 295 | if (seekFrame < 0 || seekFrame >= (int64_t)mOffsetVector.size()) { |
| 296 | android_errorWriteLog(0x534e4554, "70239507"); |
| 297 | return ERROR_MALFORMED; |
| 298 | } |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 299 | mCurrentTimeUs = seekFrame * mFrameDurationUs; |
| 300 | |
| 301 | mOffset = mOffsetVector.itemAt(seekFrame); |
| 302 | } |
| 303 | } |
| 304 | |
Jean-Michel Trivi | bf4c48b | 2011-08-29 14:06:51 -0700 | [diff] [blame] | 305 | size_t frameSize, frameSizeWithoutHeader, headerSize; |
| 306 | if ((frameSize = getAdtsFrameLength(mDataSource, mOffset, &headerSize)) == 0) { |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 307 | 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 Trivi | bf4c48b | 2011-08-29 14:06:51 -0700 | [diff] [blame] | 316 | frameSizeWithoutHeader = frameSize - headerSize; |
| 317 | if (mDataSource->readAt(mOffset + headerSize, buffer->data(), |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 318 | 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 | |
| 338 | bool SniffAAC( |
| 339 | const sp<DataSource> &source, String8 *mimeType, float *confidence, |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 340 | 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 Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 367 | (long long)pos, (long long)pos); |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 370 | uint8_t header[2]; |
| 371 | |
Andreas Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 372 | if (source->readAt(pos, &header, 2) != 2) { |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 373 | 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 Huber | 14da736 | 2011-11-01 11:01:01 -0700 | [diff] [blame] | 380 | |
| 381 | *meta = new AMessage; |
| 382 | (*meta)->setInt64("offset", pos); |
| 383 | |
Gloria Wang | 50c44c7 | 2011-02-02 14:12:49 -0800 | [diff] [blame] | 384 | return true; |
| 385 | } |
| 386 | |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | } // namespace android |