blob: 42cebfe061688de0cde3a214f00b1aa8c5291e25 [file] [log] [blame]
Andreas Huber4456da52010-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"
Mark Salyzyna5750e02014-06-18 16:34:45 -070019
20#include <inttypes.h>
21
Andreas Huber4456da52010-11-09 08:57:45 -080022#include <utils/Log.h>
23
Marco Nelissen75226172016-11-16 14:10:52 -080024#include "VBRISeeker.h"
Andreas Huber4456da52010-11-09 08:57:45 -080025
Marco Nelissen75226172016-11-16 14:10:52 -080026#include "avc_utils.h"
Andreas Huber4456da52010-11-09 08:57:45 -080027
28#include <media/stagefright/foundation/ADebug.h>
Dongwon Kang60761282017-10-09 11:16:48 -070029#include <media/stagefright/foundation/ByteUtils.h>
Andreas Huber4456da52010-11-09 08:57:45 -080030#include <media/stagefright/DataSource.h>
Andreas Huber4456da52010-11-09 08:57:45 -080031
32namespace android {
33
34static uint32_t U24_AT(const uint8_t *ptr) {
35 return ptr[0] << 16 | ptr[1] << 8 | ptr[2];
36}
37
38// static
39sp<VBRISeeker> VBRISeeker::CreateFromSource(
James Dongc7fc37a2010-11-16 14:04:54 -080040 const sp<DataSource> &source, off64_t post_id3_pos) {
41 off64_t pos = post_id3_pos;
Andreas Huber4456da52010-11-09 08:57:45 -080042
43 uint8_t header[4];
44 ssize_t n = source->readAt(pos, header, sizeof(header));
45 if (n < (ssize_t)sizeof(header)) {
46 return NULL;
47 }
48
49 uint32_t tmp = U32_AT(&header[0]);
50 size_t frameSize;
51 int sampleRate;
Andreas Huber386d6092011-05-19 08:37:39 -070052 if (!GetMPEGAudioFrameSize(tmp, &frameSize, &sampleRate)) {
Andreas Huber4456da52010-11-09 08:57:45 -080053 return NULL;
54 }
55
56 // VBRI header follows 32 bytes after the header _ends_.
57 pos += sizeof(header) + 32;
58
59 uint8_t vbriHeader[26];
60 n = source->readAt(pos, vbriHeader, sizeof(vbriHeader));
61 if (n < (ssize_t)sizeof(vbriHeader)) {
62 return NULL;
63 }
64
65 if (memcmp(vbriHeader, "VBRI", 4)) {
66 return NULL;
67 }
68
69 size_t numFrames = U32_AT(&vbriHeader[14]);
70
71 int64_t durationUs =
72 numFrames * 1000000ll * (sampleRate >= 32000 ? 1152 : 576) / sampleRate;
73
Steve Block3856b092011-10-20 11:56:00 +010074 ALOGV("duration = %.2f secs", durationUs / 1E6);
Andreas Huber4456da52010-11-09 08:57:45 -080075
76 size_t numEntries = U16_AT(&vbriHeader[18]);
77 size_t entrySize = U16_AT(&vbriHeader[22]);
78 size_t scale = U16_AT(&vbriHeader[20]);
79
Mark Salyzyna5750e02014-06-18 16:34:45 -070080 ALOGV("%zu entries, scale=%zu, size_per_entry=%zu",
Andreas Huber4456da52010-11-09 08:57:45 -080081 numEntries,
82 scale,
83 entrySize);
84
Marco Nelissen7fdd3642016-11-11 09:20:00 -080085 if (entrySize > 4) {
86 ALOGE("invalid VBRI entry size: %zu", entrySize);
87 return NULL;
88 }
89
90 sp<VBRISeeker> seeker = new (std::nothrow) VBRISeeker;
91 if (seeker == NULL) {
92 ALOGW("Couldn't allocate VBRISeeker");
93 return NULL;
94 }
95
Andreas Huber4456da52010-11-09 08:57:45 -080096 size_t totalEntrySize = numEntries * entrySize;
Marco Nelissen7fdd3642016-11-11 09:20:00 -080097 uint8_t *buffer = new (std::nothrow) uint8_t[totalEntrySize];
98 if (!buffer) {
99 ALOGW("Couldn't allocate %zu bytes", totalEntrySize);
100 return NULL;
101 }
Andreas Huber4456da52010-11-09 08:57:45 -0800102
103 n = source->readAt(pos + sizeof(vbriHeader), buffer, totalEntrySize);
104 if (n < (ssize_t)totalEntrySize) {
105 delete[] buffer;
106 buffer = NULL;
107
108 return NULL;
109 }
110
Marco Nelissen9e503852012-03-16 07:56:42 -0700111 seeker->mBasePos = post_id3_pos + frameSize;
Marco Nelissen5fd7d3a2012-06-13 08:51:00 -0700112 // only update mDurationUs if the calculated duration is valid (non zero)
113 // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
114 // return false when called, to indicate that this vbri tag does not have the
115 // requested information
116 if (durationUs) {
117 seeker->mDurationUs = durationUs;
118 }
Andreas Huber4456da52010-11-09 08:57:45 -0800119
James Dongc7fc37a2010-11-16 14:04:54 -0800120 off64_t offset = post_id3_pos;
Andreas Huber4456da52010-11-09 08:57:45 -0800121 for (size_t i = 0; i < numEntries; ++i) {
122 uint32_t numBytes;
123 switch (entrySize) {
124 case 1: numBytes = buffer[i]; break;
125 case 2: numBytes = U16_AT(buffer + 2 * i); break;
126 case 3: numBytes = U24_AT(buffer + 3 * i); break;
127 default:
128 {
129 CHECK_EQ(entrySize, 4u);
130 numBytes = U32_AT(buffer + 4 * i); break;
131 }
132 }
133
134 numBytes *= scale;
135
136 seeker->mSegments.push(numBytes);
137
Lajos Molnaree4e1b12015-04-17 13:46:19 -0700138 ALOGV("entry #%zu: %u offset %#016llx", i, numBytes, (long long)offset);
Andreas Huber4456da52010-11-09 08:57:45 -0800139 offset += numBytes;
140 }
141
142 delete[] buffer;
143 buffer = NULL;
144
Steve Blockdf64d152012-01-04 20:05:49 +0000145 ALOGI("Found VBRI header.");
Andreas Huber4456da52010-11-09 08:57:45 -0800146
147 return seeker;
148}
149
150VBRISeeker::VBRISeeker()
151 : mDurationUs(-1) {
152}
153
154bool VBRISeeker::getDuration(int64_t *durationUs) {
155 if (mDurationUs < 0) {
156 return false;
157 }
158
159 *durationUs = mDurationUs;
160
161 return true;
162}
163
James Dongc7fc37a2010-11-16 14:04:54 -0800164bool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
Wei Jiae206ba02015-10-02 16:40:49 -0700165 if (mDurationUs < 0 || mSegments.size() == 0) {
Andreas Huber4456da52010-11-09 08:57:45 -0800166 return false;
167 }
168
169 int64_t segmentDurationUs = mDurationUs / mSegments.size();
170
171 int64_t nowUs = 0;
172 *pos = mBasePos;
173 size_t segmentIndex = 0;
174 while (segmentIndex < mSegments.size() && nowUs < *timeUs) {
175 nowUs += segmentDurationUs;
176 *pos += mSegments.itemAt(segmentIndex++);
177 }
178
Lajos Molnaree4e1b12015-04-17 13:46:19 -0700179 ALOGV("getOffsetForTime %lld us => 0x%016llx", (long long)*timeUs, (long long)*pos);
Andreas Huber4456da52010-11-09 08:57:45 -0800180
181 *timeUs = nowUs;
182
183 return true;
184}
185
186} // namespace android
187