14456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber/*
24456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * Copyright (C) 2010 The Android Open Source Project
34456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber *
44456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
54456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * you may not use this file except in compliance with the License.
64456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * You may obtain a copy of the License at
74456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber *
84456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
94456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber *
104456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * Unless required by applicable law or agreed to in writing, software
114456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
124456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * See the License for the specific language governing permissions and
144456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber * limitations under the License.
154456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber */
164456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
174456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber//#define LOG_NDEBUG 0
184456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#define LOG_TAG "VBRISeeker"
19a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn
20a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn#include <inttypes.h>
21a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn
224456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <utils/Log.h>
234456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
244456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include "include/VBRISeeker.h"
254456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
26386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber#include "include/avc_utils.h"
274456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include "include/MP3Extractor.h"
284456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
294456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <media/stagefright/foundation/ADebug.h>
304456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <media/stagefright/DataSource.h>
314456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <media/stagefright/Utils.h>
324456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
334456da54bcd206ed1f518c69cc959ca65a179c83Andreas Hubernamespace android {
344456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
354456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huberstatic uint32_t U24_AT(const uint8_t *ptr) {
364456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
374456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
384456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
394456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber// static
404456da54bcd206ed1f518c69cc959ca65a179c83Andreas Hubersp<VBRISeeker> VBRISeeker::CreateFromSource(
41c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong        const sp<DataSource> &source, off64_t post_id3_pos) {
42c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    off64_t pos = post_id3_pos;
434456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
444456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint8_t header[4];
454456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    ssize_t n = source->readAt(pos, header, sizeof(header));
464456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (n < (ssize_t)sizeof(header)) {
474456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
484456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
494456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
504456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint32_t tmp = U32_AT(&header[0]);
514456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t frameSize;
524456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int sampleRate;
53386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    if (!GetMPEGAudioFrameSize(tmp, &frameSize, &sampleRate)) {
544456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
554456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
564456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
574456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    // VBRI header follows 32 bytes after the header _ends_.
584456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    pos += sizeof(header) + 32;
594456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
604456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint8_t vbriHeader[26];
614456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    n = source->readAt(pos, vbriHeader, sizeof(vbriHeader));
624456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (n < (ssize_t)sizeof(vbriHeader)) {
634456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
644456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
654456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
664456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (memcmp(vbriHeader, "VBRI", 4)) {
674456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
684456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
694456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
704456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t numFrames = U32_AT(&vbriHeader[14]);
714456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
724456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int64_t durationUs =
734456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        numFrames * 1000000ll * (sampleRate >= 32000 ? 1152 : 576) / sampleRate;
744456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
753856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("duration = %.2f secs", durationUs / 1E6);
764456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
774456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t numEntries = U16_AT(&vbriHeader[18]);
784456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t entrySize = U16_AT(&vbriHeader[22]);
794456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t scale = U16_AT(&vbriHeader[20]);
804456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
81a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn    ALOGV("%zu entries, scale=%zu, size_per_entry=%zu",
824456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber         numEntries,
834456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber         scale,
844456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber         entrySize);
854456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
864456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t totalEntrySize = numEntries * entrySize;
874456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint8_t *buffer = new uint8_t[totalEntrySize];
884456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
894456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    n = source->readAt(pos + sizeof(vbriHeader), buffer, totalEntrySize);
904456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (n < (ssize_t)totalEntrySize) {
914456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        delete[] buffer;
924456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        buffer = NULL;
934456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
944456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
954456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
964456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
974456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    sp<VBRISeeker> seeker = new VBRISeeker;
989e50385f34676d8fcb3b473ec4c9f5c388a4c887Marco Nelissen    seeker->mBasePos = post_id3_pos + frameSize;
995fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // only update mDurationUs if the calculated duration is valid (non zero)
1005fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
1015fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // return false when called, to indicate that this vbri tag does not have the
1025fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // requested information
1035fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    if (durationUs) {
1045fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen        seeker->mDurationUs = durationUs;
1055fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    }
1064456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
107c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    off64_t offset = post_id3_pos;
1084456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    for (size_t i = 0; i < numEntries; ++i) {
1094456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        uint32_t numBytes;
1104456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        switch (entrySize) {
1114456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            case 1: numBytes = buffer[i]; break;
1124456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            case 2: numBytes = U16_AT(buffer + 2 * i); break;
1134456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            case 3: numBytes = U24_AT(buffer + 3 * i); break;
1144456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            default:
1154456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            {
1164456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber                CHECK_EQ(entrySize, 4u);
1174456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber                numBytes = U32_AT(buffer + 4 * i); break;
1184456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            }
1194456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        }
1204456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1214456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        numBytes *= scale;
1224456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1234456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        seeker->mSegments.push(numBytes);
1244456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
125ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnar        ALOGV("entry #%zu: %u offset %#016llx", i, numBytes, (long long)offset);
1264456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        offset += numBytes;
1274456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1284456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1294456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    delete[] buffer;
1304456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    buffer = NULL;
1314456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
132df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block    ALOGI("Found VBRI header.");
1334456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1344456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return seeker;
1354456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1364456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1374456da54bcd206ed1f518c69cc959ca65a179c83Andreas HuberVBRISeeker::VBRISeeker()
1384456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    : mDurationUs(-1) {
1394456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1404456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1414456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huberbool VBRISeeker::getDuration(int64_t *durationUs) {
1424456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (mDurationUs < 0) {
1434456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return false;
1444456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1454456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1464456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    *durationUs = mDurationUs;
1474456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1484456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return true;
1494456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1504456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
151c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dongbool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
152e206ba0720bad1e29912a0ec359f451672c0bc95Wei Jia    if (mDurationUs < 0 || mSegments.size() == 0) {
1534456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return false;
1544456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1554456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1564456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int64_t segmentDurationUs = mDurationUs / mSegments.size();
1574456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1584456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int64_t nowUs = 0;
1594456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    *pos = mBasePos;
1604456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t segmentIndex = 0;
1614456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    while (segmentIndex < mSegments.size() && nowUs < *timeUs) {
1624456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        nowUs += segmentDurationUs;
1634456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        *pos += mSegments.itemAt(segmentIndex++);
1644456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1654456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
166ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnar    ALOGV("getOffsetForTime %lld us => 0x%016llx", (long long)*timeUs, (long long)*pos);
1674456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1684456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    *timeUs = nowUs;
1694456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1704456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return true;
1714456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1724456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1734456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}  // namespace android
1744456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
175