Home | History | Annotate | Download | only in libstagefright
      1 /*
      2  * Copyright (C) 2010 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 "VBRISeeker"
     19 #include <utils/Log.h>
     20 
     21 #include "include/VBRISeeker.h"
     22 
     23 #include "include/avc_utils.h"
     24 #include "include/MP3Extractor.h"
     25 
     26 #include <media/stagefright/foundation/ADebug.h>
     27 #include <media/stagefright/DataSource.h>
     28 #include <media/stagefright/Utils.h>
     29 
     30 namespace android {
     31 
     32 static uint32_t U24_AT(const uint8_t *ptr) {
     33     return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
     34 }
     35 
     36 // static
     37 sp<VBRISeeker> VBRISeeker::CreateFromSource(
     38         const sp<DataSource> &source, off64_t post_id3_pos) {
     39     off64_t pos = post_id3_pos;
     40 
     41     uint8_t header[4];
     42     ssize_t n = source->readAt(pos, header, sizeof(header));
     43     if (n < (ssize_t)sizeof(header)) {
     44         return NULL;
     45     }
     46 
     47     uint32_t tmp = U32_AT(&header[0]);
     48     size_t frameSize;
     49     int sampleRate;
     50     if (!GetMPEGAudioFrameSize(tmp, &frameSize, &sampleRate)) {
     51         return NULL;
     52     }
     53 
     54     // VBRI header follows 32 bytes after the header _ends_.
     55     pos += sizeof(header) + 32;
     56 
     57     uint8_t vbriHeader[26];
     58     n = source->readAt(pos, vbriHeader, sizeof(vbriHeader));
     59     if (n < (ssize_t)sizeof(vbriHeader)) {
     60         return NULL;
     61     }
     62 
     63     if (memcmp(vbriHeader, "VBRI", 4)) {
     64         return NULL;
     65     }
     66 
     67     size_t numFrames = U32_AT(&vbriHeader[14]);
     68 
     69     int64_t durationUs =
     70         numFrames * 1000000ll * (sampleRate >= 32000 ? 1152 : 576) / sampleRate;
     71 
     72     ALOGV("duration = %.2f secs", durationUs / 1E6);
     73 
     74     size_t numEntries = U16_AT(&vbriHeader[18]);
     75     size_t entrySize = U16_AT(&vbriHeader[22]);
     76     size_t scale = U16_AT(&vbriHeader[20]);
     77 
     78     ALOGV("%d entries, scale=%d, size_per_entry=%d",
     79          numEntries,
     80          scale,
     81          entrySize);
     82 
     83     size_t totalEntrySize = numEntries * entrySize;
     84     uint8_t *buffer = new uint8_t[totalEntrySize];
     85 
     86     n = source->readAt(pos + sizeof(vbriHeader), buffer, totalEntrySize);
     87     if (n < (ssize_t)totalEntrySize) {
     88         delete[] buffer;
     89         buffer = NULL;
     90 
     91         return NULL;
     92     }
     93 
     94     sp<VBRISeeker> seeker = new VBRISeeker;
     95     seeker->mBasePos = post_id3_pos + frameSize;
     96     // only update mDurationUs if the calculated duration is valid (non zero)
     97     // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
     98     // return false when called, to indicate that this vbri tag does not have the
     99     // requested information
    100     if (durationUs) {
    101         seeker->mDurationUs = durationUs;
    102     }
    103 
    104     off64_t offset = post_id3_pos;
    105     for (size_t i = 0; i < numEntries; ++i) {
    106         uint32_t numBytes;
    107         switch (entrySize) {
    108             case 1: numBytes = buffer[i]; break;
    109             case 2: numBytes = U16_AT(buffer + 2 * i); break;
    110             case 3: numBytes = U24_AT(buffer + 3 * i); break;
    111             default:
    112             {
    113                 CHECK_EQ(entrySize, 4u);
    114                 numBytes = U32_AT(buffer + 4 * i); break;
    115             }
    116         }
    117 
    118         numBytes *= scale;
    119 
    120         seeker->mSegments.push(numBytes);
    121 
    122         ALOGV("entry #%d: %d offset 0x%08lx", i, numBytes, offset);
    123         offset += numBytes;
    124     }
    125 
    126     delete[] buffer;
    127     buffer = NULL;
    128 
    129     ALOGI("Found VBRI header.");
    130 
    131     return seeker;
    132 }
    133 
    134 VBRISeeker::VBRISeeker()
    135     : mDurationUs(-1) {
    136 }
    137 
    138 bool VBRISeeker::getDuration(int64_t *durationUs) {
    139     if (mDurationUs < 0) {
    140         return false;
    141     }
    142 
    143     *durationUs = mDurationUs;
    144 
    145     return true;
    146 }
    147 
    148 bool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
    149     if (mDurationUs < 0) {
    150         return false;
    151     }
    152 
    153     int64_t segmentDurationUs = mDurationUs / mSegments.size();
    154 
    155     int64_t nowUs = 0;
    156     *pos = mBasePos;
    157     size_t segmentIndex = 0;
    158     while (segmentIndex < mSegments.size() && nowUs < *timeUs) {
    159         nowUs += segmentDurationUs;
    160         *pos += mSegments.itemAt(segmentIndex++);
    161     }
    162 
    163     ALOGV("getOffsetForTime %lld us => 0x%08lx", *timeUs, *pos);
    164 
    165     *timeUs = nowUs;
    166 
    167     return true;
    168 }
    169 
    170 }  // namespace android
    171 
    172