VBRISeeker.cpp revision 4456da54bcd206ed1f518c69cc959ca65a179c83
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/MP3Extractor.h" 24 25#include <media/stagefright/foundation/ADebug.h> 26#include <media/stagefright/DataSource.h> 27#include <media/stagefright/Utils.h> 28 29namespace android { 30 31static uint32_t U24_AT(const uint8_t *ptr) { 32 return ptr[0] << 16 | ptr[1] << 8 | ptr[2]; 33} 34 35// static 36sp<VBRISeeker> VBRISeeker::CreateFromSource( 37 const sp<DataSource> &source, off_t post_id3_pos) { 38 off_t pos = post_id3_pos; 39 40 uint8_t header[4]; 41 ssize_t n = source->readAt(pos, header, sizeof(header)); 42 if (n < (ssize_t)sizeof(header)) { 43 return NULL; 44 } 45 46 uint32_t tmp = U32_AT(&header[0]); 47 size_t frameSize; 48 int sampleRate; 49 if (!MP3Extractor::get_mp3_frame_size(tmp, &frameSize, &sampleRate)) { 50 return NULL; 51 } 52 53 // VBRI header follows 32 bytes after the header _ends_. 54 pos += sizeof(header) + 32; 55 56 uint8_t vbriHeader[26]; 57 n = source->readAt(pos, vbriHeader, sizeof(vbriHeader)); 58 if (n < (ssize_t)sizeof(vbriHeader)) { 59 return NULL; 60 } 61 62 if (memcmp(vbriHeader, "VBRI", 4)) { 63 return NULL; 64 } 65 66 size_t numFrames = U32_AT(&vbriHeader[14]); 67 68 int64_t durationUs = 69 numFrames * 1000000ll * (sampleRate >= 32000 ? 1152 : 576) / sampleRate; 70 71 LOGV("duration = %.2f secs", durationUs / 1E6); 72 73 size_t numEntries = U16_AT(&vbriHeader[18]); 74 size_t entrySize = U16_AT(&vbriHeader[22]); 75 size_t scale = U16_AT(&vbriHeader[20]); 76 77 LOGV("%d entries, scale=%d, size_per_entry=%d", 78 numEntries, 79 scale, 80 entrySize); 81 82 size_t totalEntrySize = numEntries * entrySize; 83 uint8_t *buffer = new uint8_t[totalEntrySize]; 84 85 n = source->readAt(pos + sizeof(vbriHeader), buffer, totalEntrySize); 86 if (n < (ssize_t)totalEntrySize) { 87 delete[] buffer; 88 buffer = NULL; 89 90 return NULL; 91 } 92 93 sp<VBRISeeker> seeker = new VBRISeeker; 94 seeker->mBasePos = post_id3_pos; 95 seeker->mDurationUs = durationUs; 96 97 off_t offset = post_id3_pos; 98 for (size_t i = 0; i < numEntries; ++i) { 99 uint32_t numBytes; 100 switch (entrySize) { 101 case 1: numBytes = buffer[i]; break; 102 case 2: numBytes = U16_AT(buffer + 2 * i); break; 103 case 3: numBytes = U24_AT(buffer + 3 * i); break; 104 default: 105 { 106 CHECK_EQ(entrySize, 4u); 107 numBytes = U32_AT(buffer + 4 * i); break; 108 } 109 } 110 111 numBytes *= scale; 112 113 seeker->mSegments.push(numBytes); 114 115 LOGV("entry #%d: %d offset 0x%08lx", i, numBytes, offset); 116 offset += numBytes; 117 } 118 119 delete[] buffer; 120 buffer = NULL; 121 122 LOGI("Found VBRI header."); 123 124 return seeker; 125} 126 127VBRISeeker::VBRISeeker() 128 : mDurationUs(-1) { 129} 130 131bool VBRISeeker::getDuration(int64_t *durationUs) { 132 if (mDurationUs < 0) { 133 return false; 134 } 135 136 *durationUs = mDurationUs; 137 138 return true; 139} 140 141bool VBRISeeker::getOffsetForTime(int64_t *timeUs, off_t *pos) { 142 if (mDurationUs < 0) { 143 return false; 144 } 145 146 int64_t segmentDurationUs = mDurationUs / mSegments.size(); 147 148 int64_t nowUs = 0; 149 *pos = mBasePos; 150 size_t segmentIndex = 0; 151 while (segmentIndex < mSegments.size() && nowUs < *timeUs) { 152 nowUs += segmentDurationUs; 153 *pos += mSegments.itemAt(segmentIndex++); 154 } 155 156 LOGV("getOffsetForTime %lld us => 0x%08lx", *timeUs, *pos); 157 158 *timeUs = nowUs; 159 160 return true; 161} 162 163} // namespace android 164 165