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