Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 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" |
Mark Salyzyn | a5750e0 | 2014-06-18 16:34:45 -0700 | [diff] [blame] | 19 | |
| 20 | #include <inttypes.h> |
| 21 | |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | |
Marco Nelissen | 7522617 | 2016-11-16 14:10:52 -0800 | [diff] [blame] | 24 | #include "VBRISeeker.h" |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 25 | |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 26 | #include <media/stagefright/foundation/avc_utils.h> |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 27 | |
| 28 | #include <media/stagefright/foundation/ADebug.h> |
Dongwon Kang | 6076128 | 2017-10-09 11:16:48 -0700 | [diff] [blame] | 29 | #include <media/stagefright/foundation/ByteUtils.h> |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 30 | #include <media/DataSourceBase.h> |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | static uint32_t U24_AT(const uint8_t *ptr) { |
| 35 | return ptr[0] << 16 | ptr[1] << 8 | ptr[2]; |
| 36 | } |
| 37 | |
| 38 | // static |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame^] | 39 | VBRISeeker *VBRISeeker::CreateFromSource( |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 40 | DataSourceBase *source, off64_t post_id3_pos) { |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 41 | off64_t pos = post_id3_pos; |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 42 | |
| 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 Huber | 386d609 | 2011-05-19 08:37:39 -0700 | [diff] [blame] | 52 | if (!GetMPEGAudioFrameSize(tmp, &frameSize, &sampleRate)) { |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 53 | 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 Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 74 | ALOGV("duration = %.2f secs", durationUs / 1E6); |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 75 | |
| 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 Salyzyn | a5750e0 | 2014-06-18 16:34:45 -0700 | [diff] [blame] | 80 | ALOGV("%zu entries, scale=%zu, size_per_entry=%zu", |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 81 | numEntries, |
| 82 | scale, |
| 83 | entrySize); |
| 84 | |
Marco Nelissen | 7fdd364 | 2016-11-11 09:20:00 -0800 | [diff] [blame] | 85 | if (entrySize > 4) { |
| 86 | ALOGE("invalid VBRI entry size: %zu", entrySize); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame^] | 90 | VBRISeeker *seeker = new (std::nothrow) VBRISeeker; |
Marco Nelissen | 7fdd364 | 2016-11-11 09:20:00 -0800 | [diff] [blame] | 91 | if (seeker == NULL) { |
| 92 | ALOGW("Couldn't allocate VBRISeeker"); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 96 | size_t totalEntrySize = numEntries * entrySize; |
Marco Nelissen | 7fdd364 | 2016-11-11 09:20:00 -0800 | [diff] [blame] | 97 | uint8_t *buffer = new (std::nothrow) uint8_t[totalEntrySize]; |
| 98 | if (!buffer) { |
| 99 | ALOGW("Couldn't allocate %zu bytes", totalEntrySize); |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame^] | 100 | delete seeker; |
Marco Nelissen | 7fdd364 | 2016-11-11 09:20:00 -0800 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 103 | |
| 104 | n = source->readAt(pos + sizeof(vbriHeader), buffer, totalEntrySize); |
| 105 | if (n < (ssize_t)totalEntrySize) { |
| 106 | delete[] buffer; |
| 107 | buffer = NULL; |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame^] | 108 | delete seeker; |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
Marco Nelissen | 9e50385 | 2012-03-16 07:56:42 -0700 | [diff] [blame] | 112 | seeker->mBasePos = post_id3_pos + frameSize; |
Marco Nelissen | 5fd7d3a | 2012-06-13 08:51:00 -0700 | [diff] [blame] | 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 |
| 117 | if (durationUs) { |
| 118 | seeker->mDurationUs = durationUs; |
| 119 | } |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 120 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 121 | off64_t offset = post_id3_pos; |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 122 | for (size_t i = 0; i < numEntries; ++i) { |
| 123 | uint32_t numBytes; |
| 124 | switch (entrySize) { |
| 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; |
| 128 | default: |
| 129 | { |
| 130 | CHECK_EQ(entrySize, 4u); |
| 131 | numBytes = U32_AT(buffer + 4 * i); break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | numBytes *= scale; |
| 136 | |
| 137 | seeker->mSegments.push(numBytes); |
| 138 | |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 139 | ALOGV("entry #%zu: %u offset %#016llx", i, numBytes, (long long)offset); |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 140 | offset += numBytes; |
| 141 | } |
| 142 | |
| 143 | delete[] buffer; |
| 144 | buffer = NULL; |
| 145 | |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 146 | ALOGI("Found VBRI header."); |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 147 | |
| 148 | return seeker; |
| 149 | } |
| 150 | |
| 151 | VBRISeeker::VBRISeeker() |
| 152 | : mDurationUs(-1) { |
| 153 | } |
| 154 | |
| 155 | bool VBRISeeker::getDuration(int64_t *durationUs) { |
| 156 | if (mDurationUs < 0) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | *durationUs = mDurationUs; |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 165 | bool VBRISeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) { |
Wei Jia | e206ba0 | 2015-10-02 16:40:49 -0700 | [diff] [blame] | 166 | if (mDurationUs < 0 || mSegments.size() == 0) { |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
| 170 | int64_t segmentDurationUs = mDurationUs / mSegments.size(); |
| 171 | |
| 172 | int64_t nowUs = 0; |
| 173 | *pos = mBasePos; |
| 174 | size_t segmentIndex = 0; |
| 175 | while (segmentIndex < mSegments.size() && nowUs < *timeUs) { |
| 176 | nowUs += segmentDurationUs; |
| 177 | *pos += mSegments.itemAt(segmentIndex++); |
| 178 | } |
| 179 | |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 180 | ALOGV("getOffsetForTime %lld us => 0x%016llx", (long long)*timeUs, (long long)*pos); |
Andreas Huber | 4456da5 | 2010-11-09 08:57:45 -0800 | [diff] [blame] | 181 | |
| 182 | *timeUs = nowUs; |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | } // namespace android |
| 188 | |