Home | History | Annotate | Download | only in util
      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 #include "sllog.h"
     18 #include <utils/Log.h>
     19 
     20 #include "android/include/AacAdtsExtractor.h"
     21 #include <avc_utils.h>
     22 
     23 
     24 namespace android {
     25 
     26 #define ADTS_HEADER_LENGTH 7
     27 // ADTS header size is 7, but frame size information ends on byte 6 (when counting from byte 1)
     28 #define ADTS_HEADER_SIZE_UP_TO_FRAMESIZE 6
     29 
     30 ////////////////////////////////////////////////////////////////////////////////
     31 
     32 // Returns the sample rate based on the sampling frequency index
     33 static uint32_t get_sample_rate(const uint8_t sf_index)
     34 {
     35     static const uint32_t sample_rates[] =
     36     {
     37         96000, 88200, 64000, 48000, 44100, 32000,
     38         24000, 22050, 16000, 12000, 11025, 8000
     39     };
     40 
     41     if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
     42         return sample_rates[sf_index];
     43     }
     44 
     45     return 0;
     46 }
     47 
     48 static size_t getFrameSize(const sp<DataSource> &source, off64_t offset) {
     49     size_t frameSize = 0;
     50 
     51     uint8_t syncHeader[ADTS_HEADER_SIZE_UP_TO_FRAMESIZE];
     52     const uint8_t *syncword = syncHeader;
     53     const uint8_t *header = syncHeader + 3;
     54 
     55     ssize_t readSize = source->readAt(offset, &syncHeader, ADTS_HEADER_SIZE_UP_TO_FRAMESIZE);
     56     if (readSize == 0) {
     57         // EOS is normal, not an error
     58         SL_LOGV("AacAdtsExtractor::getFrameSize EOS");
     59         return 0;
     60     }
     61     if (readSize != ADTS_HEADER_SIZE_UP_TO_FRAMESIZE) {
     62         SL_LOGE("AacAdtsExtractor:: getFrameSize() returns %d (syncword and header read error)",
     63                 (int) readSize);
     64         return 0;
     65     }
     66 
     67     if ((syncword[0] != 0xff) || ((syncword[1] & 0xf6) != 0xf0)) {
     68         SL_LOGE("AacAdtsExtractor:: getFrameSize() returns 0 (syncword pb)");
     69         return 0;
     70     }
     71 
     72     const uint8_t protectionAbsent = syncword[1] & 0x1;
     73 
     74     frameSize = (header[0] & 0x3) << 11 | header[1] << 3 | header[2] >> 5;
     75     // the frame size read already contains the size of the ADTS header, so no need to add it here
     76 
     77     // protectionAbsent is 0 if there is CRC
     78     static const size_t kAdtsHeaderLengthNoCrc = 7;
     79     static const size_t kAdtsHeaderLengthWithCrc = 9;
     80     size_t headSize = protectionAbsent ? kAdtsHeaderLengthNoCrc : kAdtsHeaderLengthWithCrc;
     81     if (headSize > frameSize) {
     82         SL_LOGE("AacAdtsExtractor:: getFrameSize() returns 0 (frameSize %zu < headSize %zu)",
     83                 frameSize, headSize);
     84         return 0;
     85     }
     86 
     87     //SL_LOGV("AacAdtsExtractor:: getFrameSize() returns %u", frameSize);
     88 
     89     return frameSize;
     90 }
     91 
     92 
     93 AacAdtsExtractor::AacAdtsExtractor(const sp<DataSource> &source)
     94     : mDataSource(source),
     95       mInitCheck(NO_INIT),
     96       mFrameDurationUs(0) {
     97 
     98     // difference with framework's AAC Extractor: we have already validated the data
     99     // upon enqueueing, so no need to sniff the data:
    100     //    String8 mimeType;
    101     //    float confidence;
    102     //    if (!SniffAAC(mDataSource, &mimeType, &confidence, NULL)) {
    103     //        return;
    104     //    }
    105 
    106     uint8_t profile, sf_index, channel, header[2];
    107     ssize_t readSize = mDataSource->readAt(2, &header, 2);
    108     if (readSize != 2) {
    109         SL_LOGE("Unable to determine sample rate");
    110         return;
    111     }
    112 
    113     profile = (header[0] >> 6) & 0x3;
    114     sf_index = (header[0] >> 2) & 0xf;
    115     uint32_t sr = get_sample_rate(sf_index);
    116 
    117     if (sr == 0) {
    118         SL_LOGE("Invalid sample rate");
    119         return;
    120     }
    121     channel = (header[0] & 0x1) << 2 | (header[1] >> 6);
    122 
    123     SL_LOGV("AacAdtsExtractor has found sr=%d channel=%d", sr, channel);
    124 
    125     // Never fails
    126     mMeta = MakeAACCodecSpecificData(profile, sf_index, channel);
    127 
    128     // Round up and get the duration of each frame
    129     mFrameDurationUs = (1024 * 1000000ll + (sr - 1)) / sr;
    130 
    131     off64_t streamSize;
    132     if (mDataSource->getSize(&streamSize) == OK) {
    133         off64_t offset = 0, numFrames = 0;
    134         while (offset < streamSize) {
    135             size_t frameSize;
    136             if ((frameSize = getFrameSize(mDataSource, offset)) == 0) {
    137                 // Usually frameSize == 0 due to EOS is benign (and getFrameSize() doesn't SL_LOGE),
    138                 // but in this case we were told the total size of the data source and so an EOS
    139                 // should not happen.
    140                 SL_LOGE("AacAdtsExtractor() failed querying framesize at offset=%lld",
    141                         (long long) offset);
    142                 return;
    143             }
    144 
    145             offset += frameSize;
    146             if (offset > streamSize) {
    147                 SL_LOGE("AacAdtsExtractor() frame of size %zu at offset=%lld is beyond EOF %lld",
    148                         frameSize, (long long) offset, (long long) streamSize);
    149                 return;
    150             }
    151             numFrames ++;
    152         }
    153 
    154         // Compute total duration
    155         int64_t duration = numFrames * mFrameDurationUs;
    156         mMeta->setInt64(kKeyDuration, duration);
    157     }
    158 
    159     // Any earlier "return" would leave mInitCheck as NO_INIT, causing later methods to fail quickly
    160     mInitCheck = OK;
    161 
    162 }
    163 
    164 
    165 AacAdtsExtractor::~AacAdtsExtractor() {
    166 }
    167 
    168 
    169 sp<MetaData> AacAdtsExtractor::getMetaData() {
    170     sp<MetaData> meta = new MetaData;
    171 
    172     if (mInitCheck != OK) {
    173         return meta;
    174     }
    175 
    176     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC_ADTS);
    177 
    178     return meta;
    179 }
    180 
    181 
    182 size_t AacAdtsExtractor::countTracks() {
    183     return mInitCheck == OK ? 1 : 0;
    184 }
    185 
    186 
    187 sp<IMediaSource> AacAdtsExtractor::getTrack(size_t index) {
    188     if (mInitCheck != OK || index != 0) {
    189         return NULL;
    190     }
    191 
    192     return new AacAdtsSource(mDataSource, mMeta, mFrameDurationUs);
    193 }
    194 
    195 
    196 sp<MetaData> AacAdtsExtractor::getTrackMetaData(size_t index, uint32_t flags) {
    197     if (mInitCheck != OK || index != 0) {
    198         return NULL;
    199     }
    200 
    201     return mMeta;
    202 }
    203 
    204 
    205 ////////////////////////////////////////////////////////////////////////////////
    206 
    207 // 8192 = 2^13, 13bit AAC frame size (in bytes)
    208 const size_t AacAdtsSource::kMaxFrameSize = 8192;
    209 
    210 AacAdtsSource::AacAdtsSource(
    211         const sp<DataSource> &source, const sp<MetaData> &meta,
    212         int64_t frame_duration_us)
    213     : mDataSource(source),
    214       mMeta(meta),
    215       mOffset(0),
    216       mCurrentTimeUs(0),
    217       mStarted(false),
    218       mGroup(NULL),
    219       mFrameDurationUs(frame_duration_us) {
    220 }
    221 
    222 
    223 AacAdtsSource::~AacAdtsSource() {
    224     if (mStarted) {
    225         stop();
    226     }
    227 }
    228 
    229 
    230 status_t AacAdtsSource::start(MetaData *params) {
    231     CHECK(!mStarted);
    232 
    233     mOffset = 0;
    234     mCurrentTimeUs = 0;
    235     mGroup = new MediaBufferGroup;
    236     mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
    237     mStarted = true;
    238 
    239     return OK;
    240 }
    241 
    242 
    243 status_t AacAdtsSource::stop() {
    244     CHECK(mStarted);
    245 
    246     delete mGroup;
    247     mGroup = NULL;
    248 
    249     mStarted = false;
    250     return OK;
    251 }
    252 
    253 
    254 sp<MetaData> AacAdtsSource::getFormat() {
    255     return mMeta;
    256 }
    257 
    258 
    259 status_t AacAdtsSource::read(
    260         MediaBuffer **out, const ReadOptions *options) {
    261     *out = NULL;
    262 
    263     int64_t seekTimeUs;
    264     ReadOptions::SeekMode mode;
    265     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
    266         // difference with framework's AAC Extractor: no seeking
    267         SL_LOGE("Can't seek in AAC ADTS buffer queue");
    268     }
    269 
    270     size_t frameSize, frameSizeWithoutHeader;
    271     SL_LOGV("AacAdtsSource::read() offset=%lld", mOffset);
    272     if ((frameSize = getFrameSize(mDataSource, mOffset)) == 0) {
    273         // EOS is normal, not an error
    274         SL_LOGV("AacAdtsSource::read() returns EOS");
    275         return ERROR_END_OF_STREAM;
    276     }
    277 
    278     MediaBuffer *buffer;
    279     status_t err = mGroup->acquire_buffer(&buffer);
    280     if (err != OK) {
    281         return err;
    282     }
    283 
    284     frameSizeWithoutHeader = frameSize - ADTS_HEADER_LENGTH;
    285     ssize_t readSize = mDataSource->readAt(mOffset + ADTS_HEADER_LENGTH, buffer->data(),
    286             frameSizeWithoutHeader);
    287     //SL_LOGV("AacAdtsSource::read() readAt returned %u bytes", readSize);
    288     if (readSize != (ssize_t)frameSizeWithoutHeader) {
    289         SL_LOGW("AacAdtsSource::read() readSize != frameSizeWithoutHeader");
    290         buffer->release();
    291         buffer = NULL;
    292         return ERROR_IO;
    293     }
    294 
    295     buffer->set_range(0, frameSizeWithoutHeader);
    296     buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
    297     buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
    298 
    299     mOffset += frameSize;
    300     mCurrentTimeUs += mFrameDurationUs;
    301 
    302     *out = buffer;
    303     return OK;
    304 }
    305 
    306 }  // namespace android
    307