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"
194456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <utils/Log.h>
204456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
214456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include "include/VBRISeeker.h"
224456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
23386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber#include "include/avc_utils.h"
244456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include "include/MP3Extractor.h"
254456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
264456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <media/stagefright/foundation/ADebug.h>
274456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <media/stagefright/DataSource.h>
284456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber#include <media/stagefright/Utils.h>
294456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
304456da54bcd206ed1f518c69cc959ca65a179c83Andreas Hubernamespace android {
314456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
324456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huberstatic uint32_t U24_AT(const uint8_t *ptr) {
334456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
344456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
354456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
364456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber// static
374456da54bcd206ed1f518c69cc959ca65a179c83Andreas Hubersp<VBRISeeker> VBRISeeker::CreateFromSource(
38c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong        const sp<DataSource> &source, off64_t post_id3_pos) {
39c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    off64_t pos = post_id3_pos;
404456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
414456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint8_t header[4];
424456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    ssize_t n = source->readAt(pos, header, sizeof(header));
434456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (n < (ssize_t)sizeof(header)) {
444456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
454456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
464456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
474456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint32_t tmp = U32_AT(&header[0]);
484456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t frameSize;
494456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int sampleRate;
50386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    if (!GetMPEGAudioFrameSize(tmp, &frameSize, &sampleRate)) {
514456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
524456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
534456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
544456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    // VBRI header follows 32 bytes after the header _ends_.
554456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    pos += sizeof(header) + 32;
564456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
574456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint8_t vbriHeader[26];
584456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    n = source->readAt(pos, vbriHeader, sizeof(vbriHeader));
594456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (n < (ssize_t)sizeof(vbriHeader)) {
604456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
614456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
624456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
634456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (memcmp(vbriHeader, "VBRI", 4)) {
644456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
654456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
664456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
674456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t numFrames = U32_AT(&vbriHeader[14]);
684456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
694456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int64_t durationUs =
704456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        numFrames * 1000000ll * (sampleRate >= 32000 ? 1152 : 576) / sampleRate;
714456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
723856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("duration = %.2f secs", durationUs / 1E6);
734456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
744456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t numEntries = U16_AT(&vbriHeader[18]);
754456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t entrySize = U16_AT(&vbriHeader[22]);
764456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t scale = U16_AT(&vbriHeader[20]);
774456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
783856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d entries, scale=%d, size_per_entry=%d",
794456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber         numEntries,
804456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber         scale,
814456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber         entrySize);
824456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
834456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t totalEntrySize = numEntries * entrySize;
844456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    uint8_t *buffer = new uint8_t[totalEntrySize];
854456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
864456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    n = source->readAt(pos + sizeof(vbriHeader), buffer, totalEntrySize);
874456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (n < (ssize_t)totalEntrySize) {
884456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        delete[] buffer;
894456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        buffer = NULL;
904456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
914456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return NULL;
924456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
934456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
944456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    sp<VBRISeeker> seeker = new VBRISeeker;
959e50385f34676d8fcb3b473ec4c9f5c388a4c887Marco Nelissen    seeker->mBasePos = post_id3_pos + frameSize;
965fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // only update mDurationUs if the calculated duration is valid (non zero)
975fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
985fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // return false when called, to indicate that this vbri tag does not have the
995fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    // requested information
1005fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    if (durationUs) {
1015fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen        seeker->mDurationUs = durationUs;
1025fd7d3a23316ec0d2320fece85201859b52c2aefMarco Nelissen    }
1034456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
104c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    off64_t offset = post_id3_pos;
1054456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    for (size_t i = 0; i < numEntries; ++i) {
1064456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        uint32_t numBytes;
1074456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        switch (entrySize) {
1084456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            case 1: numBytes = buffer[i]; break;
1094456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            case 2: numBytes = U16_AT(buffer + 2 * i); break;
1104456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            case 3: numBytes = U24_AT(buffer + 3 * i); break;
1114456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            default:
1124456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            {
1134456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber                CHECK_EQ(entrySize, 4u);
1144456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber                numBytes = U32_AT(buffer + 4 * i); break;
1154456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber            }
1164456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        }
1174456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1184456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        numBytes *= scale;
1194456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1204456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        seeker->mSegments.push(numBytes);
1214456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1223856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("entry #%d: %d offset 0x%08lx", i, numBytes, offset);
1234456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        offset += numBytes;
1244456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1254456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1264456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    delete[] buffer;
1274456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    buffer = NULL;
1284456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
129df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block    ALOGI("Found VBRI header.");
1304456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1314456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return seeker;
1324456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1334456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1344456da54bcd206ed1f518c69cc959ca65a179c83Andreas HuberVBRISeeker::VBRISeeker()
1354456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    : mDurationUs(-1) {
1364456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1374456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1384456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huberbool VBRISeeker::getDuration(int64_t *durationUs) {
1394456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (mDurationUs < 0) {
1404456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return false;
1414456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1424456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1434456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    *durationUs = mDurationUs;
1444456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1454456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return true;
1464456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1474456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
148c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dongbool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
1494456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    if (mDurationUs < 0) {
1504456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        return false;
1514456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1524456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1534456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int64_t segmentDurationUs = mDurationUs / mSegments.size();
1544456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1554456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    int64_t nowUs = 0;
1564456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    *pos = mBasePos;
1574456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    size_t segmentIndex = 0;
1584456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    while (segmentIndex < mSegments.size() && nowUs < *timeUs) {
1594456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        nowUs += segmentDurationUs;
1604456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber        *pos += mSegments.itemAt(segmentIndex++);
1614456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    }
1624456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1633856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("getOffsetForTime %lld us => 0x%08lx", *timeUs, *pos);
1644456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1654456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    *timeUs = nowUs;
1664456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1674456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber    return true;
1684456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}
1694456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
1704456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber}  // namespace android
1714456da54bcd206ed1f518c69cc959ca65a179c83Andreas Huber
172