VBRISeeker.cpp revision 9e50385f34676d8fcb3b473ec4c9f5c388a4c887
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
30namespace android {
31
32static uint32_t U24_AT(const uint8_t *ptr) {
33    return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
34}
35
36// static
37sp<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    seeker->mDurationUs = durationUs;
97
98    off64_t offset = post_id3_pos;
99    for (size_t i = 0; i < numEntries; ++i) {
100        uint32_t numBytes;
101        switch (entrySize) {
102            case 1: numBytes = buffer[i]; break;
103            case 2: numBytes = U16_AT(buffer + 2 * i); break;
104            case 3: numBytes = U24_AT(buffer + 3 * i); break;
105            default:
106            {
107                CHECK_EQ(entrySize, 4u);
108                numBytes = U32_AT(buffer + 4 * i); break;
109            }
110        }
111
112        numBytes *= scale;
113
114        seeker->mSegments.push(numBytes);
115
116        ALOGV("entry #%d: %d offset 0x%08lx", i, numBytes, offset);
117        offset += numBytes;
118    }
119
120    delete[] buffer;
121    buffer = NULL;
122
123    ALOGI("Found VBRI header.");
124
125    return seeker;
126}
127
128VBRISeeker::VBRISeeker()
129    : mDurationUs(-1) {
130}
131
132bool VBRISeeker::getDuration(int64_t *durationUs) {
133    if (mDurationUs < 0) {
134        return false;
135    }
136
137    *durationUs = mDurationUs;
138
139    return true;
140}
141
142bool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
143    if (mDurationUs < 0) {
144        return false;
145    }
146
147    int64_t segmentDurationUs = mDurationUs / mSegments.size();
148
149    int64_t nowUs = 0;
150    *pos = mBasePos;
151    size_t segmentIndex = 0;
152    while (segmentIndex < mSegments.size() && nowUs < *timeUs) {
153        nowUs += segmentDurationUs;
154        *pos += mSegments.itemAt(segmentIndex++);
155    }
156
157    ALOGV("getOffsetForTime %lld us => 0x%08lx", *timeUs, *pos);
158
159    *timeUs = nowUs;
160
161    return true;
162}
163
164}  // namespace android
165
166