Home | History | Annotate | Download | only in libstagefright
      1 /*
      2  * Copyright (C) 2009 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 "MP3Extractor"
     19 #include <utils/Log.h>
     20 
     21 #include "include/MP3Extractor.h"
     22 
     23 #include "include/avc_utils.h"
     24 #include "include/ID3.h"
     25 #include "include/VBRISeeker.h"
     26 #include "include/XINGSeeker.h"
     27 
     28 #include <media/stagefright/foundation/ADebug.h>
     29 #include <media/stagefright/foundation/AMessage.h>
     30 #include <media/stagefright/DataSource.h>
     31 #include <media/stagefright/MediaBuffer.h>
     32 #include <media/stagefright/MediaBufferGroup.h>
     33 #include <media/stagefright/MediaDefs.h>
     34 #include <media/stagefright/MediaErrors.h>
     35 #include <media/stagefright/MediaSource.h>
     36 #include <media/stagefright/MetaData.h>
     37 #include <media/stagefright/Utils.h>
     38 #include <utils/String8.h>
     39 
     40 namespace android {
     41 
     42 // Everything must match except for
     43 // protection, bitrate, padding, private bits, mode, mode extension,
     44 // copyright bit, original bit and emphasis.
     45 // Yes ... there are things that must indeed match...
     46 static const uint32_t kMask = 0xfffe0c00;
     47 
     48 static bool Resync(
     49         const sp<DataSource> &source, uint32_t match_header,
     50         off64_t *inout_pos, off64_t *post_id3_pos, uint32_t *out_header) {
     51     if (post_id3_pos != NULL) {
     52         *post_id3_pos = 0;
     53     }
     54 
     55     if (*inout_pos == 0) {
     56         // Skip an optional ID3 header if syncing at the very beginning
     57         // of the datasource.
     58 
     59         for (;;) {
     60             uint8_t id3header[10];
     61             if (source->readAt(*inout_pos, id3header, sizeof(id3header))
     62                     < (ssize_t)sizeof(id3header)) {
     63                 // If we can't even read these 10 bytes, we might as well bail
     64                 // out, even if there _were_ 10 bytes of valid mp3 audio data...
     65                 return false;
     66             }
     67 
     68             if (memcmp("ID3", id3header, 3)) {
     69                 break;
     70             }
     71 
     72             // Skip the ID3v2 header.
     73 
     74             size_t len =
     75                 ((id3header[6] & 0x7f) << 21)
     76                 | ((id3header[7] & 0x7f) << 14)
     77                 | ((id3header[8] & 0x7f) << 7)
     78                 | (id3header[9] & 0x7f);
     79 
     80             len += 10;
     81 
     82             *inout_pos += len;
     83 
     84             ALOGV("skipped ID3 tag, new starting offset is %lld (0x%016llx)",
     85                  *inout_pos, *inout_pos);
     86         }
     87 
     88         if (post_id3_pos != NULL) {
     89             *post_id3_pos = *inout_pos;
     90         }
     91     }
     92 
     93     off64_t pos = *inout_pos;
     94     bool valid = false;
     95 
     96     const size_t kMaxReadBytes = 1024;
     97     const size_t kMaxBytesChecked = 128 * 1024;
     98     uint8_t buf[kMaxReadBytes];
     99     ssize_t bytesToRead = kMaxReadBytes;
    100     ssize_t totalBytesRead = 0;
    101     ssize_t remainingBytes = 0;
    102     bool reachEOS = false;
    103     uint8_t *tmp = buf;
    104 
    105     do {
    106         if (pos >= *inout_pos + kMaxBytesChecked) {
    107             // Don't scan forever.
    108             ALOGV("giving up at offset %lld", pos);
    109             break;
    110         }
    111 
    112         if (remainingBytes < 4) {
    113             if (reachEOS) {
    114                 break;
    115             } else {
    116                 memcpy(buf, tmp, remainingBytes);
    117                 bytesToRead = kMaxReadBytes - remainingBytes;
    118 
    119                 /*
    120                  * The next read position should start from the end of
    121                  * the last buffer, and thus should include the remaining
    122                  * bytes in the buffer.
    123                  */
    124                 totalBytesRead = source->readAt(pos + remainingBytes,
    125                                                 buf + remainingBytes,
    126                                                 bytesToRead);
    127                 if (totalBytesRead <= 0) {
    128                     break;
    129                 }
    130                 reachEOS = (totalBytesRead != bytesToRead);
    131                 totalBytesRead += remainingBytes;
    132                 remainingBytes = totalBytesRead;
    133                 tmp = buf;
    134                 continue;
    135             }
    136         }
    137 
    138         uint32_t header = U32_AT(tmp);
    139 
    140         if (match_header != 0 && (header & kMask) != (match_header & kMask)) {
    141             ++pos;
    142             ++tmp;
    143             --remainingBytes;
    144             continue;
    145         }
    146 
    147         size_t frame_size;
    148         int sample_rate, num_channels, bitrate;
    149         if (!GetMPEGAudioFrameSize(
    150                     header, &frame_size,
    151                     &sample_rate, &num_channels, &bitrate)) {
    152             ++pos;
    153             ++tmp;
    154             --remainingBytes;
    155             continue;
    156         }
    157 
    158         ALOGV("found possible 1st frame at %lld (header = 0x%08x)", pos, header);
    159 
    160         // We found what looks like a valid frame,
    161         // now find its successors.
    162 
    163         off64_t test_pos = pos + frame_size;
    164 
    165         valid = true;
    166         for (int j = 0; j < 3; ++j) {
    167             uint8_t tmp[4];
    168             if (source->readAt(test_pos, tmp, 4) < 4) {
    169                 valid = false;
    170                 break;
    171             }
    172 
    173             uint32_t test_header = U32_AT(tmp);
    174 
    175             ALOGV("subsequent header is %08x", test_header);
    176 
    177             if ((test_header & kMask) != (header & kMask)) {
    178                 valid = false;
    179                 break;
    180             }
    181 
    182             size_t test_frame_size;
    183             if (!GetMPEGAudioFrameSize(
    184                         test_header, &test_frame_size)) {
    185                 valid = false;
    186                 break;
    187             }
    188 
    189             ALOGV("found subsequent frame #%d at %lld", j + 2, test_pos);
    190 
    191             test_pos += test_frame_size;
    192         }
    193 
    194         if (valid) {
    195             *inout_pos = pos;
    196 
    197             if (out_header != NULL) {
    198                 *out_header = header;
    199             }
    200         } else {
    201             ALOGV("no dice, no valid sequence of frames found.");
    202         }
    203 
    204         ++pos;
    205         ++tmp;
    206         --remainingBytes;
    207     } while (!valid);
    208 
    209     return valid;
    210 }
    211 
    212 class MP3Source : public MediaSource {
    213 public:
    214     MP3Source(
    215             const sp<MetaData> &meta, const sp<DataSource> &source,
    216             off64_t first_frame_pos, uint32_t fixed_header,
    217             const sp<MP3Seeker> &seeker);
    218 
    219     virtual status_t start(MetaData *params = NULL);
    220     virtual status_t stop();
    221 
    222     virtual sp<MetaData> getFormat();
    223 
    224     virtual status_t read(
    225             MediaBuffer **buffer, const ReadOptions *options = NULL);
    226 
    227 protected:
    228     virtual ~MP3Source();
    229 
    230 private:
    231     static const size_t kMaxFrameSize;
    232     sp<MetaData> mMeta;
    233     sp<DataSource> mDataSource;
    234     off64_t mFirstFramePos;
    235     uint32_t mFixedHeader;
    236     off64_t mCurrentPos;
    237     int64_t mCurrentTimeUs;
    238     bool mStarted;
    239     sp<MP3Seeker> mSeeker;
    240     MediaBufferGroup *mGroup;
    241 
    242     int64_t mBasisTimeUs;
    243     int64_t mSamplesRead;
    244 
    245     MP3Source(const MP3Source &);
    246     MP3Source &operator=(const MP3Source &);
    247 };
    248 
    249 MP3Extractor::MP3Extractor(
    250         const sp<DataSource> &source, const sp<AMessage> &meta)
    251     : mInitCheck(NO_INIT),
    252       mDataSource(source),
    253       mFirstFramePos(-1),
    254       mFixedHeader(0) {
    255     off64_t pos = 0;
    256     off64_t post_id3_pos;
    257     uint32_t header;
    258     bool success;
    259 
    260     int64_t meta_offset;
    261     uint32_t meta_header;
    262     int64_t meta_post_id3_offset;
    263     if (meta != NULL
    264             && meta->findInt64("offset", &meta_offset)
    265             && meta->findInt32("header", (int32_t *)&meta_header)
    266             && meta->findInt64("post-id3-offset", &meta_post_id3_offset)) {
    267         // The sniffer has already done all the hard work for us, simply
    268         // accept its judgement.
    269         pos = (off64_t)meta_offset;
    270         header = meta_header;
    271         post_id3_pos = (off64_t)meta_post_id3_offset;
    272 
    273         success = true;
    274     } else {
    275         success = Resync(mDataSource, 0, &pos, &post_id3_pos, &header);
    276     }
    277 
    278     if (!success) {
    279         // mInitCheck will remain NO_INIT
    280         return;
    281     }
    282 
    283     mFirstFramePos = pos;
    284     mFixedHeader = header;
    285 
    286     size_t frame_size;
    287     int sample_rate;
    288     int num_channels;
    289     int bitrate;
    290     GetMPEGAudioFrameSize(
    291             header, &frame_size, &sample_rate, &num_channels, &bitrate);
    292 
    293     unsigned layer = 4 - ((header >> 17) & 3);
    294 
    295     mMeta = new MetaData;
    296 
    297     switch (layer) {
    298         case 1:
    299             mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
    300             break;
    301         case 2:
    302             mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
    303             break;
    304         case 3:
    305             mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
    306             break;
    307         default:
    308             TRESPASS();
    309     }
    310 
    311     mMeta->setInt32(kKeySampleRate, sample_rate);
    312     mMeta->setInt32(kKeyBitRate, bitrate * 1000);
    313     mMeta->setInt32(kKeyChannelCount, num_channels);
    314 
    315     sp<XINGSeeker> seeker = XINGSeeker::CreateFromSource(mDataSource, mFirstFramePos);
    316 
    317     if (seeker == NULL) {
    318         mSeeker = VBRISeeker::CreateFromSource(mDataSource, post_id3_pos);
    319     } else {
    320         mSeeker = seeker;
    321         int encd = seeker->getEncoderDelay();
    322         int encp = seeker->getEncoderPadding();
    323         if (encd != 0 || encp != 0) {
    324             mMeta->setInt32(kKeyEncoderDelay, encd);
    325             mMeta->setInt32(kKeyEncoderPadding, encp);
    326         }
    327     }
    328 
    329     if (mSeeker != NULL) {
    330         // While it is safe to send the XING/VBRI frame to the decoder, this will
    331         // result in an extra 1152 samples being output. The real first frame to
    332         // decode is after the XING/VBRI frame, so skip there.
    333         mFirstFramePos += frame_size;
    334     }
    335 
    336     int64_t durationUs;
    337 
    338     if (mSeeker == NULL || !mSeeker->getDuration(&durationUs)) {
    339         off64_t fileSize;
    340         if (mDataSource->getSize(&fileSize) == OK) {
    341             durationUs = 8000LL * (fileSize - mFirstFramePos) / bitrate;
    342         } else {
    343             durationUs = -1;
    344         }
    345     }
    346 
    347     if (durationUs >= 0) {
    348         mMeta->setInt64(kKeyDuration, durationUs);
    349     }
    350 
    351     mInitCheck = OK;
    352 
    353     // Get iTunes-style gapless info if present.
    354     // When getting the id3 tag, skip the V1 tags to prevent the source cache
    355     // from being iterated to the end of the file.
    356     ID3 id3(mDataSource, true);
    357     if (id3.isValid()) {
    358         ID3::Iterator *com = new ID3::Iterator(id3, "COM");
    359         if (com->done()) {
    360             delete com;
    361             com = new ID3::Iterator(id3, "COMM");
    362         }
    363         while(!com->done()) {
    364             String8 commentdesc;
    365             String8 commentvalue;
    366             com->getString(&commentdesc, &commentvalue);
    367             const char * desc = commentdesc.string();
    368             const char * value = commentvalue.string();
    369 
    370             // first 3 characters are the language, which we don't care about
    371             if(strlen(desc) > 3 && strcmp(desc + 3, "iTunSMPB") == 0) {
    372 
    373                 int32_t delay, padding;
    374                 if (sscanf(value, " %*x %x %x %*x", &delay, &padding) == 2) {
    375                     mMeta->setInt32(kKeyEncoderDelay, delay);
    376                     mMeta->setInt32(kKeyEncoderPadding, padding);
    377                 }
    378                 break;
    379             }
    380             com->next();
    381         }
    382         delete com;
    383         com = NULL;
    384     }
    385 }
    386 
    387 size_t MP3Extractor::countTracks() {
    388     return mInitCheck != OK ? 0 : 1;
    389 }
    390 
    391 sp<MediaSource> MP3Extractor::getTrack(size_t index) {
    392     if (mInitCheck != OK || index != 0) {
    393         return NULL;
    394     }
    395 
    396     return new MP3Source(
    397             mMeta, mDataSource, mFirstFramePos, mFixedHeader,
    398             mSeeker);
    399 }
    400 
    401 sp<MetaData> MP3Extractor::getTrackMetaData(
    402         size_t index, uint32_t /* flags */) {
    403     if (mInitCheck != OK || index != 0) {
    404         return NULL;
    405     }
    406 
    407     return mMeta;
    408 }
    409 
    410 ////////////////////////////////////////////////////////////////////////////////
    411 
    412 // The theoretical maximum frame size for an MPEG audio stream should occur
    413 // while playing a Layer 2, MPEGv2.5 audio stream at 160kbps (with padding).
    414 // The size of this frame should be...
    415 // ((1152 samples/frame * 160000 bits/sec) /
    416 //  (8000 samples/sec * 8 bits/byte)) + 1 padding byte/frame = 2881 bytes/frame.
    417 // Set our max frame size to the nearest power of 2 above this size (aka, 4kB)
    418 const size_t MP3Source::kMaxFrameSize = (1 << 12); /* 4096 bytes */
    419 MP3Source::MP3Source(
    420         const sp<MetaData> &meta, const sp<DataSource> &source,
    421         off64_t first_frame_pos, uint32_t fixed_header,
    422         const sp<MP3Seeker> &seeker)
    423     : mMeta(meta),
    424       mDataSource(source),
    425       mFirstFramePos(first_frame_pos),
    426       mFixedHeader(fixed_header),
    427       mCurrentPos(0),
    428       mCurrentTimeUs(0),
    429       mStarted(false),
    430       mSeeker(seeker),
    431       mGroup(NULL),
    432       mBasisTimeUs(0),
    433       mSamplesRead(0) {
    434 }
    435 
    436 MP3Source::~MP3Source() {
    437     if (mStarted) {
    438         stop();
    439     }
    440 }
    441 
    442 status_t MP3Source::start(MetaData *) {
    443     CHECK(!mStarted);
    444 
    445     mGroup = new MediaBufferGroup;
    446 
    447     mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
    448 
    449     mCurrentPos = mFirstFramePos;
    450     mCurrentTimeUs = 0;
    451 
    452     mBasisTimeUs = mCurrentTimeUs;
    453     mSamplesRead = 0;
    454 
    455     mStarted = true;
    456 
    457     return OK;
    458 }
    459 
    460 status_t MP3Source::stop() {
    461     CHECK(mStarted);
    462 
    463     delete mGroup;
    464     mGroup = NULL;
    465 
    466     mStarted = false;
    467 
    468     return OK;
    469 }
    470 
    471 sp<MetaData> MP3Source::getFormat() {
    472     return mMeta;
    473 }
    474 
    475 status_t MP3Source::read(
    476         MediaBuffer **out, const ReadOptions *options) {
    477     *out = NULL;
    478 
    479     int64_t seekTimeUs;
    480     ReadOptions::SeekMode mode;
    481     bool seekCBR = false;
    482 
    483     if (options != NULL && options->getSeekTo(&seekTimeUs, &mode)) {
    484         int64_t actualSeekTimeUs = seekTimeUs;
    485         if (mSeeker == NULL
    486                 || !mSeeker->getOffsetForTime(&actualSeekTimeUs, &mCurrentPos)) {
    487             int32_t bitrate;
    488             if (!mMeta->findInt32(kKeyBitRate, &bitrate)) {
    489                 // bitrate is in bits/sec.
    490                 ALOGI("no bitrate");
    491 
    492                 return ERROR_UNSUPPORTED;
    493             }
    494 
    495             mCurrentTimeUs = seekTimeUs;
    496             mCurrentPos = mFirstFramePos + seekTimeUs * bitrate / 8000000;
    497             seekCBR = true;
    498         } else {
    499             mCurrentTimeUs = actualSeekTimeUs;
    500         }
    501 
    502         mBasisTimeUs = mCurrentTimeUs;
    503         mSamplesRead = 0;
    504     }
    505 
    506     MediaBuffer *buffer;
    507     status_t err = mGroup->acquire_buffer(&buffer);
    508     if (err != OK) {
    509         return err;
    510     }
    511 
    512     size_t frame_size;
    513     int bitrate;
    514     int num_samples;
    515     int sample_rate;
    516     for (;;) {
    517         ssize_t n = mDataSource->readAt(mCurrentPos, buffer->data(), 4);
    518         if (n < 4) {
    519             buffer->release();
    520             buffer = NULL;
    521 
    522             return ERROR_END_OF_STREAM;
    523         }
    524 
    525         uint32_t header = U32_AT((const uint8_t *)buffer->data());
    526 
    527         if ((header & kMask) == (mFixedHeader & kMask)
    528             && GetMPEGAudioFrameSize(
    529                 header, &frame_size, &sample_rate, NULL,
    530                 &bitrate, &num_samples)) {
    531 
    532             // re-calculate mCurrentTimeUs because we might have called Resync()
    533             if (seekCBR) {
    534                 mCurrentTimeUs = (mCurrentPos - mFirstFramePos) * 8000 / bitrate;
    535                 mBasisTimeUs = mCurrentTimeUs;
    536             }
    537 
    538             break;
    539         }
    540 
    541         // Lost sync.
    542         ALOGV("lost sync! header = 0x%08x, old header = 0x%08x\n", header, mFixedHeader);
    543 
    544         off64_t pos = mCurrentPos;
    545         if (!Resync(mDataSource, mFixedHeader, &pos, NULL, NULL)) {
    546             ALOGE("Unable to resync. Signalling end of stream.");
    547 
    548             buffer->release();
    549             buffer = NULL;
    550 
    551             return ERROR_END_OF_STREAM;
    552         }
    553 
    554         mCurrentPos = pos;
    555 
    556         // Try again with the new position.
    557     }
    558 
    559     CHECK(frame_size <= buffer->size());
    560 
    561     ssize_t n = mDataSource->readAt(mCurrentPos, buffer->data(), frame_size);
    562     if (n < (ssize_t)frame_size) {
    563         buffer->release();
    564         buffer = NULL;
    565 
    566         return ERROR_END_OF_STREAM;
    567     }
    568 
    569     buffer->set_range(0, frame_size);
    570 
    571     buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
    572     buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
    573 
    574     mCurrentPos += frame_size;
    575 
    576     mSamplesRead += num_samples;
    577     mCurrentTimeUs = mBasisTimeUs + ((mSamplesRead * 1000000) / sample_rate);
    578 
    579     *out = buffer;
    580 
    581     return OK;
    582 }
    583 
    584 sp<MetaData> MP3Extractor::getMetaData() {
    585     sp<MetaData> meta = new MetaData;
    586 
    587     if (mInitCheck != OK) {
    588         return meta;
    589     }
    590 
    591     meta->setCString(kKeyMIMEType, "audio/mpeg");
    592 
    593     ID3 id3(mDataSource);
    594 
    595     if (!id3.isValid()) {
    596         return meta;
    597     }
    598 
    599     struct Map {
    600         int key;
    601         const char *tag1;
    602         const char *tag2;
    603     };
    604     static const Map kMap[] = {
    605         { kKeyAlbum, "TALB", "TAL" },
    606         { kKeyArtist, "TPE1", "TP1" },
    607         { kKeyAlbumArtist, "TPE2", "TP2" },
    608         { kKeyComposer, "TCOM", "TCM" },
    609         { kKeyGenre, "TCON", "TCO" },
    610         { kKeyTitle, "TIT2", "TT2" },
    611         { kKeyYear, "TYE", "TYER" },
    612         { kKeyAuthor, "TXT", "TEXT" },
    613         { kKeyCDTrackNumber, "TRK", "TRCK" },
    614         { kKeyDiscNumber, "TPA", "TPOS" },
    615         { kKeyCompilation, "TCP", "TCMP" },
    616     };
    617     static const size_t kNumMapEntries = sizeof(kMap) / sizeof(kMap[0]);
    618 
    619     for (size_t i = 0; i < kNumMapEntries; ++i) {
    620         ID3::Iterator *it = new ID3::Iterator(id3, kMap[i].tag1);
    621         if (it->done()) {
    622             delete it;
    623             it = new ID3::Iterator(id3, kMap[i].tag2);
    624         }
    625 
    626         if (it->done()) {
    627             delete it;
    628             continue;
    629         }
    630 
    631         String8 s;
    632         it->getString(&s);
    633         delete it;
    634 
    635         meta->setCString(kMap[i].key, s);
    636     }
    637 
    638     size_t dataSize;
    639     String8 mime;
    640     const void *data = id3.getAlbumArt(&dataSize, &mime);
    641 
    642     if (data) {
    643         meta->setData(kKeyAlbumArt, MetaData::TYPE_NONE, data, dataSize);
    644         meta->setCString(kKeyAlbumArtMIME, mime.string());
    645     }
    646 
    647     return meta;
    648 }
    649 
    650 bool SniffMP3(
    651         const sp<DataSource> &source, String8 *mimeType,
    652         float *confidence, sp<AMessage> *meta) {
    653     off64_t pos = 0;
    654     off64_t post_id3_pos;
    655     uint32_t header;
    656     if (!Resync(source, 0, &pos, &post_id3_pos, &header)) {
    657         return false;
    658     }
    659 
    660     *meta = new AMessage;
    661     (*meta)->setInt64("offset", pos);
    662     (*meta)->setInt32("header", header);
    663     (*meta)->setInt64("post-id3-offset", post_id3_pos);
    664 
    665     *mimeType = MEDIA_MIMETYPE_AUDIO_MPEG;
    666     *confidence = 0.2f;
    667 
    668     return true;
    669 }
    670 
    671 }  // namespace android
    672