2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "VBRISeeker"
22 #include <utils/Log.h>
24 #include "include/VBRISeeker.h"
26 #include "include/avc_utils.h"
27 #include "include/MP3Extractor.h"
29 #include <media/stagefright/foundation/ADebug.h>
30 #include <media/stagefright/DataSource.h>
31 #include <media/stagefright/Utils.h>
35 static uint32_t U24_AT(const uint8_t *ptr) {
36 return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
40 sp<VBRISeeker> VBRISeeker::CreateFromSource(
41 const sp<DataSource> &source, off64_t post_id3_pos) {
42 off64_t pos = post_id3_pos;
45 ssize_t n = source->readAt(pos, header, sizeof(header));
46 if (n < (ssize_t)sizeof(header)) {
50 uint32_t tmp = U32_AT(&header[0]);
53 if (!GetMPEGAudioFrameSize(tmp, &frameSize, &sampleRate)) {
57 // VBRI header follows 32 bytes after the header _ends_.
58 pos += sizeof(header) + 32;
60 uint8_t vbriHeader[26];
61 n = source->readAt(pos, vbriHeader, sizeof(vbriHeader));
62 if (n < (ssize_t)sizeof(vbriHeader)) {
66 if (memcmp(vbriHeader, "VBRI", 4)) {
70 size_t numFrames = U32_AT(&vbriHeader[14]);
73 numFrames * 1000000ll * (sampleRate >= 32000 ? 1152 : 576) / sampleRate;
75 ALOGV("duration = %.2f secs", durationUs / 1E6);
77 size_t numEntries = U16_AT(&vbriHeader[18]);
78 size_t entrySize = U16_AT(&vbriHeader[22]);
79 size_t scale = U16_AT(&vbriHeader[20]);
81 ALOGV("%zu entries, scale=%zu, size_per_entry=%zu",
87 ALOGE("invalid VBRI entry size: %zu", entrySize);
91 sp<VBRISeeker> seeker = new (std::nothrow) VBRISeeker;
93 ALOGW("Couldn't allocate VBRISeeker");
97 size_t totalEntrySize = numEntries * entrySize;
98 uint8_t *buffer = new (std::nothrow) uint8_t[totalEntrySize];
100 ALOGW("Couldn't allocate %zu bytes", totalEntrySize);
104 n = source->readAt(pos + sizeof(vbriHeader), buffer, totalEntrySize);
105 if (n < (ssize_t)totalEntrySize) {
112 seeker->mBasePos = post_id3_pos + frameSize;
113 // only update mDurationUs if the calculated duration is valid (non zero)
114 // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
115 // return false when called, to indicate that this vbri tag does not have the
116 // requested information
118 seeker->mDurationUs = durationUs;
121 off64_t offset = post_id3_pos;
122 for (size_t i = 0; i < numEntries; ++i) {
125 case 1: numBytes = buffer[i]; break;
126 case 2: numBytes = U16_AT(buffer + 2 * i); break;
127 case 3: numBytes = U24_AT(buffer + 3 * i); break;
130 CHECK_EQ(entrySize, 4u);
131 numBytes = U32_AT(buffer + 4 * i); break;
137 seeker->mSegments.push(numBytes);
139 ALOGV("entry #%zu: %u offset 0x%016llx", i, numBytes, offset);
146 ALOGI("Found VBRI header.");
151 VBRISeeker::VBRISeeker()
155 bool VBRISeeker::getDuration(int64_t *durationUs) {
156 if (mDurationUs < 0) {
160 *durationUs = mDurationUs;
165 bool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
166 if (mDurationUs < 0) {
170 int64_t segmentDurationUs = mDurationUs / mSegments.size();
174 size_t segmentIndex = 0;
175 while (segmentIndex < mSegments.size() && nowUs < *timeUs) {
176 nowUs += segmentDurationUs;
177 *pos += mSegments.itemAt(segmentIndex++);
180 ALOGV("getOffsetForTime %" PRId64 " us => 0x%016llx", *timeUs, *pos);
187 } // namespace android