blob: 6ac5a83e523c2d23865d145300887a67879ba829 [file] [log] [blame]
Andreas Huber1bd994982010-11-09 08:57:45 -08001/*
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
Andreas Hubereb2f9c12011-05-19 08:37:39 -070023#include "include/avc_utils.h"
Andreas Huber1bd994982010-11-09 08:57:45 -080024#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(
James Dongb1262a82010-11-16 14:04:54 -080038 const sp<DataSource> &source, off64_t post_id3_pos) {
39 off64_t pos = post_id3_pos;
Andreas Huber1bd994982010-11-09 08:57:45 -080040
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;
Andreas Hubereb2f9c12011-05-19 08:37:39 -070050 if (!GetMPEGAudioFrameSize(tmp, &frameSize, &sampleRate)) {
Andreas Huber1bd994982010-11-09 08:57:45 -080051 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
Steve Block71f2cf12011-10-20 11:56:00 +010072 ALOGV("duration = %.2f secs", durationUs / 1E6);
Andreas Huber1bd994982010-11-09 08:57:45 -080073
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
Steve Block71f2cf12011-10-20 11:56:00 +010078 ALOGV("%d entries, scale=%d, size_per_entry=%d",
Andreas Huber1bd994982010-11-09 08:57:45 -080079 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;
96 seeker->mDurationUs = durationUs;
97
James Dongb1262a82010-11-16 14:04:54 -080098 off64_t offset = post_id3_pos;
Andreas Huber1bd994982010-11-09 08:57:45 -080099 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
Steve Block71f2cf12011-10-20 11:56:00 +0100116 ALOGV("entry #%d: %d offset 0x%08lx", i, numBytes, offset);
Andreas Huber1bd994982010-11-09 08:57:45 -0800117 offset += numBytes;
118 }
119
120 delete[] buffer;
121 buffer = NULL;
122
Steve Block6215d3f2012-01-04 20:05:49 +0000123 ALOGI("Found VBRI header.");
Andreas Huber1bd994982010-11-09 08:57:45 -0800124
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
James Dongb1262a82010-11-16 14:04:54 -0800142bool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
Andreas Huber1bd994982010-11-09 08:57:45 -0800143 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
Steve Block71f2cf12011-10-20 11:56:00 +0100157 ALOGV("getOffsetForTime %lld us => 0x%08lx", *timeUs, *pos);
Andreas Huber1bd994982010-11-09 08:57:45 -0800158
159 *timeUs = nowUs;
160
161 return true;
162}
163
164} // namespace android
165