Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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_TAG "MPEG4Extractor" |
| 18 | #include <utils/Log.h> |
| 19 | |
Andreas Huber | 66326a5 | 2009-10-23 09:55:10 -0700 | [diff] [blame] | 20 | #include "include/MPEG4Extractor.h" |
Andreas Huber | 89e69da | 2009-10-13 10:22:55 -0700 | [diff] [blame] | 21 | #include "include/SampleTable.h" |
Gloria Wang | 7a1e3e8 | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 22 | #include "include/ESDS.h" |
Gloria Wang | 965d08b | 2011-05-11 11:24:09 -0700 | [diff] [blame] | 23 | #include "timedtext/TimedTextPlayer.h" |
Andreas Huber | 89e69da | 2009-10-13 10:22:55 -0700 | [diff] [blame] | 24 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 25 | #include <arpa/inet.h> |
| 26 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 32 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 33 | #include <media/stagefright/foundation/AMessage.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 34 | #include <media/stagefright/DataSource.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 35 | #include <media/stagefright/MediaBuffer.h> |
| 36 | #include <media/stagefright/MediaBufferGroup.h> |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 37 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 38 | #include <media/stagefright/MediaSource.h> |
| 39 | #include <media/stagefright/MetaData.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 40 | #include <media/stagefright/Utils.h> |
| 41 | #include <utils/String8.h> |
| 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | class MPEG4Source : public MediaSource { |
| 46 | public: |
| 47 | // Caller retains ownership of both "dataSource" and "sampleTable". |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 48 | MPEG4Source(const sp<MetaData> &format, |
| 49 | const sp<DataSource> &dataSource, |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 50 | int32_t timeScale, |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 51 | const sp<SampleTable> &sampleTable); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 52 | |
| 53 | virtual status_t start(MetaData *params = NULL); |
| 54 | virtual status_t stop(); |
| 55 | |
| 56 | virtual sp<MetaData> getFormat(); |
| 57 | |
| 58 | virtual status_t read( |
| 59 | MediaBuffer **buffer, const ReadOptions *options = NULL); |
| 60 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 61 | protected: |
| 62 | virtual ~MPEG4Source(); |
| 63 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 64 | private: |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 65 | Mutex mLock; |
| 66 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 67 | sp<MetaData> mFormat; |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 68 | sp<DataSource> mDataSource; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 69 | int32_t mTimescale; |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 70 | sp<SampleTable> mSampleTable; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 71 | uint32_t mCurrentSampleIndex; |
| 72 | |
| 73 | bool mIsAVC; |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 74 | size_t mNALLengthSize; |
| 75 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 76 | bool mStarted; |
| 77 | |
| 78 | MediaBufferGroup *mGroup; |
| 79 | |
| 80 | MediaBuffer *mBuffer; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 81 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 82 | bool mWantsNALFragments; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 83 | |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 84 | uint8_t *mSrcBuffer; |
| 85 | |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 86 | size_t parseNALSize(const uint8_t *data) const; |
| 87 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 88 | MPEG4Source(const MPEG4Source &); |
| 89 | MPEG4Source &operator=(const MPEG4Source &); |
| 90 | }; |
| 91 | |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 92 | // This custom data source wraps an existing one and satisfies requests |
| 93 | // falling entirely within a cached range from the cache while forwarding |
| 94 | // all remaining requests to the wrapped datasource. |
| 95 | // This is used to cache the full sampletable metadata for a single track, |
| 96 | // possibly wrapping multiple times to cover all tracks, i.e. |
| 97 | // Each MPEG4DataSource caches the sampletable metadata for a single track. |
| 98 | |
| 99 | struct MPEG4DataSource : public DataSource { |
| 100 | MPEG4DataSource(const sp<DataSource> &source); |
| 101 | |
| 102 | virtual status_t initCheck() const; |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 103 | virtual ssize_t readAt(off64_t offset, void *data, size_t size); |
| 104 | virtual status_t getSize(off64_t *size); |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 105 | virtual uint32_t flags(); |
| 106 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 107 | status_t setCachedRange(off64_t offset, size_t size); |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 108 | |
| 109 | protected: |
| 110 | virtual ~MPEG4DataSource(); |
| 111 | |
| 112 | private: |
| 113 | Mutex mLock; |
| 114 | |
| 115 | sp<DataSource> mSource; |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 116 | off64_t mCachedOffset; |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 117 | size_t mCachedSize; |
| 118 | uint8_t *mCache; |
| 119 | |
| 120 | void clearCache(); |
| 121 | |
| 122 | MPEG4DataSource(const MPEG4DataSource &); |
| 123 | MPEG4DataSource &operator=(const MPEG4DataSource &); |
| 124 | }; |
| 125 | |
| 126 | MPEG4DataSource::MPEG4DataSource(const sp<DataSource> &source) |
| 127 | : mSource(source), |
| 128 | mCachedOffset(0), |
| 129 | mCachedSize(0), |
| 130 | mCache(NULL) { |
| 131 | } |
| 132 | |
| 133 | MPEG4DataSource::~MPEG4DataSource() { |
| 134 | clearCache(); |
| 135 | } |
| 136 | |
| 137 | void MPEG4DataSource::clearCache() { |
| 138 | if (mCache) { |
| 139 | free(mCache); |
| 140 | mCache = NULL; |
| 141 | } |
| 142 | |
| 143 | mCachedOffset = 0; |
| 144 | mCachedSize = 0; |
| 145 | } |
| 146 | |
| 147 | status_t MPEG4DataSource::initCheck() const { |
| 148 | return mSource->initCheck(); |
| 149 | } |
| 150 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 151 | ssize_t MPEG4DataSource::readAt(off64_t offset, void *data, size_t size) { |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 152 | Mutex::Autolock autoLock(mLock); |
| 153 | |
| 154 | if (offset >= mCachedOffset |
| 155 | && offset + size <= mCachedOffset + mCachedSize) { |
| 156 | memcpy(data, &mCache[offset - mCachedOffset], size); |
| 157 | return size; |
| 158 | } |
| 159 | |
| 160 | return mSource->readAt(offset, data, size); |
| 161 | } |
| 162 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 163 | status_t MPEG4DataSource::getSize(off64_t *size) { |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 164 | return mSource->getSize(size); |
| 165 | } |
| 166 | |
| 167 | uint32_t MPEG4DataSource::flags() { |
| 168 | return mSource->flags(); |
| 169 | } |
| 170 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 171 | status_t MPEG4DataSource::setCachedRange(off64_t offset, size_t size) { |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 172 | Mutex::Autolock autoLock(mLock); |
| 173 | |
| 174 | clearCache(); |
| 175 | |
| 176 | mCache = (uint8_t *)malloc(size); |
| 177 | |
| 178 | if (mCache == NULL) { |
| 179 | return -ENOMEM; |
| 180 | } |
| 181 | |
| 182 | mCachedOffset = offset; |
| 183 | mCachedSize = size; |
| 184 | |
| 185 | ssize_t err = mSource->readAt(mCachedOffset, mCache, mCachedSize); |
| 186 | |
| 187 | if (err < (ssize_t)size) { |
| 188 | clearCache(); |
| 189 | |
| 190 | return ERROR_IO; |
| 191 | } |
| 192 | |
| 193 | return OK; |
| 194 | } |
| 195 | |
| 196 | //////////////////////////////////////////////////////////////////////////////// |
| 197 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 198 | static void hexdump(const void *_data, size_t size) { |
| 199 | const uint8_t *data = (const uint8_t *)_data; |
| 200 | size_t offset = 0; |
| 201 | while (offset < size) { |
| 202 | printf("0x%04x ", offset); |
| 203 | |
| 204 | size_t n = size - offset; |
| 205 | if (n > 16) { |
| 206 | n = 16; |
| 207 | } |
| 208 | |
| 209 | for (size_t i = 0; i < 16; ++i) { |
| 210 | if (i == 8) { |
| 211 | printf(" "); |
| 212 | } |
| 213 | |
| 214 | if (offset + i < size) { |
| 215 | printf("%02x ", data[offset + i]); |
| 216 | } else { |
| 217 | printf(" "); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | printf(" "); |
| 222 | |
| 223 | for (size_t i = 0; i < n; ++i) { |
| 224 | if (isprint(data[offset + i])) { |
| 225 | printf("%c", data[offset + i]); |
| 226 | } else { |
| 227 | printf("."); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | printf("\n"); |
| 232 | |
| 233 | offset += 16; |
| 234 | } |
| 235 | } |
| 236 | |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 237 | static const char *FourCC2MIME(uint32_t fourcc) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 238 | switch (fourcc) { |
| 239 | case FOURCC('m', 'p', '4', 'a'): |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 240 | return MEDIA_MIMETYPE_AUDIO_AAC; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 241 | |
| 242 | case FOURCC('s', 'a', 'm', 'r'): |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 243 | return MEDIA_MIMETYPE_AUDIO_AMR_NB; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 244 | |
Andreas Huber | 371e243 | 2009-09-09 16:32:59 -0700 | [diff] [blame] | 245 | case FOURCC('s', 'a', 'w', 'b'): |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 246 | return MEDIA_MIMETYPE_AUDIO_AMR_WB; |
Andreas Huber | 371e243 | 2009-09-09 16:32:59 -0700 | [diff] [blame] | 247 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 248 | case FOURCC('m', 'p', '4', 'v'): |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 249 | return MEDIA_MIMETYPE_VIDEO_MPEG4; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 250 | |
| 251 | case FOURCC('s', '2', '6', '3'): |
James Dong | e071b05 | 2010-12-15 15:49:46 -0800 | [diff] [blame] | 252 | case FOURCC('h', '2', '6', '3'): |
| 253 | case FOURCC('H', '2', '6', '3'): |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 254 | return MEDIA_MIMETYPE_VIDEO_H263; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 255 | |
| 256 | case FOURCC('a', 'v', 'c', '1'): |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 257 | return MEDIA_MIMETYPE_VIDEO_AVC; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 258 | |
| 259 | default: |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 260 | CHECK(!"should not be here."); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 261 | return NULL; |
| 262 | } |
| 263 | } |
| 264 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 265 | MPEG4Extractor::MPEG4Extractor(const sp<DataSource> &source) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 266 | : mDataSource(source), |
Andreas Huber | bdba1f2 | 2011-03-03 13:48:41 -0800 | [diff] [blame] | 267 | mInitCheck(NO_INIT), |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 268 | mHasVideo(false), |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 269 | mFirstTrack(NULL), |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 270 | mLastTrack(NULL), |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 271 | mFileMetaData(new MetaData), |
| 272 | mFirstSINF(NULL), |
| 273 | mIsDrm(false) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | MPEG4Extractor::~MPEG4Extractor() { |
| 277 | Track *track = mFirstTrack; |
| 278 | while (track) { |
| 279 | Track *next = track->next; |
| 280 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 281 | delete track; |
| 282 | track = next; |
| 283 | } |
| 284 | mFirstTrack = mLastTrack = NULL; |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 285 | |
| 286 | SINF *sinf = mFirstSINF; |
| 287 | while (sinf) { |
| 288 | SINF *next = sinf->next; |
| 289 | delete sinf->IPMPData; |
| 290 | delete sinf; |
| 291 | sinf = next; |
| 292 | } |
| 293 | mFirstSINF = NULL; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 296 | sp<MetaData> MPEG4Extractor::getMetaData() { |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 297 | status_t err; |
| 298 | if ((err = readMetaData()) != OK) { |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 299 | return new MetaData; |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 302 | return mFileMetaData; |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 305 | size_t MPEG4Extractor::countTracks() { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 306 | status_t err; |
| 307 | if ((err = readMetaData()) != OK) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 308 | return 0; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 311 | size_t n = 0; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 312 | Track *track = mFirstTrack; |
| 313 | while (track) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 314 | ++n; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 315 | track = track->next; |
| 316 | } |
| 317 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 318 | return n; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Andreas Huber | 7e04dcf | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 321 | sp<MetaData> MPEG4Extractor::getTrackMetaData( |
| 322 | size_t index, uint32_t flags) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 323 | status_t err; |
| 324 | if ((err = readMetaData()) != OK) { |
| 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | Track *track = mFirstTrack; |
| 329 | while (index > 0) { |
| 330 | if (track == NULL) { |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | track = track->next; |
| 335 | --index; |
| 336 | } |
| 337 | |
Andreas Huber | 371e243 | 2009-09-09 16:32:59 -0700 | [diff] [blame] | 338 | if (track == NULL) { |
| 339 | return NULL; |
| 340 | } |
| 341 | |
Andreas Huber | 7e04dcf | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 342 | if ((flags & kIncludeExtensiveMetaData) |
| 343 | && !track->includes_expensive_metadata) { |
| 344 | track->includes_expensive_metadata = true; |
| 345 | |
| 346 | const char *mime; |
| 347 | CHECK(track->meta->findCString(kKeyMIMEType, &mime)); |
| 348 | if (!strncasecmp("video/", mime, 6)) { |
| 349 | uint32_t sampleIndex; |
| 350 | uint32_t sampleTime; |
| 351 | if (track->sampleTable->findThumbnailSample(&sampleIndex) == OK |
Andreas Huber | c57b679 | 2010-01-19 10:39:21 -0800 | [diff] [blame] | 352 | && track->sampleTable->getMetaDataForSample( |
| 353 | sampleIndex, NULL /* offset */, NULL /* size */, |
| 354 | &sampleTime) == OK) { |
Andreas Huber | 7e04dcf | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 355 | track->meta->setInt64( |
| 356 | kKeyThumbnailTime, |
| 357 | ((int64_t)sampleTime * 1000000) / track->timescale); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 362 | return track->meta; |
| 363 | } |
| 364 | |
| 365 | status_t MPEG4Extractor::readMetaData() { |
Andreas Huber | bdba1f2 | 2011-03-03 13:48:41 -0800 | [diff] [blame] | 366 | if (mInitCheck != NO_INIT) { |
| 367 | return mInitCheck; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 368 | } |
| 369 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 370 | off64_t offset = 0; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 371 | status_t err; |
| 372 | while ((err = parseChunk(&offset, 0)) == OK) { |
| 373 | } |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 374 | |
Andreas Huber | bdba1f2 | 2011-03-03 13:48:41 -0800 | [diff] [blame] | 375 | if (mInitCheck == OK) { |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 376 | if (mHasVideo) { |
| 377 | mFileMetaData->setCString(kKeyMIMEType, "video/mp4"); |
| 378 | } else { |
| 379 | mFileMetaData->setCString(kKeyMIMEType, "audio/mp4"); |
| 380 | } |
| 381 | |
Andreas Huber | 4678a6d | 2011-04-15 11:52:29 -0700 | [diff] [blame] | 382 | mInitCheck = OK; |
Andreas Huber | bdba1f2 | 2011-03-03 13:48:41 -0800 | [diff] [blame] | 383 | } else { |
| 384 | mInitCheck = err; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Andreas Huber | bdba1f2 | 2011-03-03 13:48:41 -0800 | [diff] [blame] | 387 | CHECK_NE(err, (status_t)NO_INIT); |
| 388 | return mInitCheck; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 391 | char* MPEG4Extractor::getDrmTrackInfo(size_t trackID, int *len) { |
| 392 | if (mFirstSINF == NULL) { |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | SINF *sinf = mFirstSINF; |
| 397 | while (sinf && (trackID != sinf->trackID)) { |
| 398 | sinf = sinf->next; |
| 399 | } |
| 400 | |
| 401 | if (sinf == NULL) { |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | *len = sinf->len; |
| 406 | return sinf->IPMPData; |
| 407 | } |
| 408 | |
| 409 | // Reads an encoded integer 7 bits at a time until it encounters the high bit clear. |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 410 | int32_t readSize(off64_t offset, |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 411 | const sp<DataSource> DataSource, uint8_t *numOfBytes) { |
| 412 | uint32_t size = 0; |
| 413 | uint8_t data; |
| 414 | bool moreData = true; |
| 415 | *numOfBytes = 0; |
| 416 | |
| 417 | while (moreData) { |
| 418 | if (DataSource->readAt(offset, &data, 1) < 1) { |
| 419 | return -1; |
| 420 | } |
| 421 | offset ++; |
| 422 | moreData = (data >= 128) ? true : false; |
| 423 | size = (size << 7) | (data & 0x7f); // Take last 7 bits |
| 424 | (*numOfBytes) ++; |
| 425 | } |
| 426 | |
| 427 | return size; |
| 428 | } |
| 429 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 430 | status_t MPEG4Extractor::parseDrmSINF(off64_t *offset, off64_t data_offset) { |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 431 | uint8_t updateIdTag; |
| 432 | if (mDataSource->readAt(data_offset, &updateIdTag, 1) < 1) { |
| 433 | return ERROR_IO; |
| 434 | } |
| 435 | data_offset ++; |
| 436 | |
| 437 | if (0x01/*OBJECT_DESCRIPTOR_UPDATE_ID_TAG*/ != updateIdTag) { |
| 438 | return ERROR_MALFORMED; |
| 439 | } |
| 440 | |
| 441 | uint8_t numOfBytes; |
| 442 | int32_t size = readSize(data_offset, mDataSource, &numOfBytes); |
| 443 | if (size < 0) { |
| 444 | return ERROR_IO; |
| 445 | } |
| 446 | int32_t classSize = size; |
| 447 | data_offset += numOfBytes; |
| 448 | |
| 449 | while(size >= 11 ) { |
| 450 | uint8_t descriptorTag; |
| 451 | if (mDataSource->readAt(data_offset, &descriptorTag, 1) < 1) { |
| 452 | return ERROR_IO; |
| 453 | } |
| 454 | data_offset ++; |
| 455 | |
| 456 | if (0x11/*OBJECT_DESCRIPTOR_ID_TAG*/ != descriptorTag) { |
| 457 | return ERROR_MALFORMED; |
| 458 | } |
| 459 | |
| 460 | uint8_t buffer[8]; |
| 461 | //ObjectDescriptorID and ObjectDescriptor url flag |
| 462 | if (mDataSource->readAt(data_offset, buffer, 2) < 2) { |
| 463 | return ERROR_IO; |
| 464 | } |
| 465 | data_offset += 2; |
| 466 | |
| 467 | if ((buffer[1] >> 5) & 0x0001) { //url flag is set |
| 468 | return ERROR_MALFORMED; |
| 469 | } |
| 470 | |
| 471 | if (mDataSource->readAt(data_offset, buffer, 8) < 8) { |
| 472 | return ERROR_IO; |
| 473 | } |
| 474 | data_offset += 8; |
| 475 | |
| 476 | if ((0x0F/*ES_ID_REF_TAG*/ != buffer[1]) |
| 477 | || ( 0x0A/*IPMP_DESCRIPTOR_POINTER_ID_TAG*/ != buffer[5])) { |
| 478 | return ERROR_MALFORMED; |
| 479 | } |
| 480 | |
| 481 | SINF *sinf = new SINF; |
| 482 | sinf->trackID = U16_AT(&buffer[3]); |
| 483 | sinf->IPMPDescriptorID = buffer[7]; |
| 484 | sinf->next = mFirstSINF; |
| 485 | mFirstSINF = sinf; |
| 486 | |
| 487 | size -= (8 + 2 + 1); |
| 488 | } |
| 489 | |
| 490 | if (size != 0) { |
| 491 | return ERROR_MALFORMED; |
| 492 | } |
| 493 | |
| 494 | if (mDataSource->readAt(data_offset, &updateIdTag, 1) < 1) { |
| 495 | return ERROR_IO; |
| 496 | } |
| 497 | data_offset ++; |
| 498 | |
| 499 | if(0x05/*IPMP_DESCRIPTOR_UPDATE_ID_TAG*/ != updateIdTag) { |
| 500 | return ERROR_MALFORMED; |
| 501 | } |
| 502 | |
| 503 | size = readSize(data_offset, mDataSource, &numOfBytes); |
| 504 | if (size < 0) { |
| 505 | return ERROR_IO; |
| 506 | } |
| 507 | classSize = size; |
| 508 | data_offset += numOfBytes; |
| 509 | |
| 510 | while (size > 0) { |
| 511 | uint8_t tag; |
| 512 | int32_t dataLen; |
| 513 | if (mDataSource->readAt(data_offset, &tag, 1) < 1) { |
| 514 | return ERROR_IO; |
| 515 | } |
| 516 | data_offset ++; |
| 517 | |
| 518 | if (0x0B/*IPMP_DESCRIPTOR_ID_TAG*/ == tag) { |
| 519 | uint8_t id; |
| 520 | dataLen = readSize(data_offset, mDataSource, &numOfBytes); |
| 521 | if (dataLen < 0) { |
| 522 | return ERROR_IO; |
| 523 | } else if (dataLen < 4) { |
| 524 | return ERROR_MALFORMED; |
| 525 | } |
| 526 | data_offset += numOfBytes; |
| 527 | |
| 528 | if (mDataSource->readAt(data_offset, &id, 1) < 1) { |
| 529 | return ERROR_IO; |
| 530 | } |
| 531 | data_offset ++; |
| 532 | |
| 533 | SINF *sinf = mFirstSINF; |
| 534 | while (sinf && (sinf->IPMPDescriptorID != id)) { |
| 535 | sinf = sinf->next; |
| 536 | } |
| 537 | if (sinf == NULL) { |
| 538 | return ERROR_MALFORMED; |
| 539 | } |
| 540 | sinf->len = dataLen - 3; |
| 541 | sinf->IPMPData = new char[sinf->len]; |
| 542 | |
| 543 | if (mDataSource->readAt(data_offset + 2, sinf->IPMPData, sinf->len) < sinf->len) { |
| 544 | return ERROR_IO; |
| 545 | } |
| 546 | data_offset += sinf->len; |
| 547 | |
| 548 | size -= (dataLen + numOfBytes + 1); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (size != 0) { |
| 553 | return ERROR_MALFORMED; |
| 554 | } |
| 555 | |
| 556 | return UNKNOWN_ERROR; // Return a dummy error. |
| 557 | } |
| 558 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 559 | static void MakeFourCCString(uint32_t x, char *s) { |
| 560 | s[0] = x >> 24; |
| 561 | s[1] = (x >> 16) & 0xff; |
| 562 | s[2] = (x >> 8) & 0xff; |
| 563 | s[3] = x & 0xff; |
| 564 | s[4] = '\0'; |
| 565 | } |
| 566 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 567 | struct PathAdder { |
| 568 | PathAdder(Vector<uint32_t> *path, uint32_t chunkType) |
| 569 | : mPath(path) { |
| 570 | mPath->push(chunkType); |
| 571 | } |
| 572 | |
| 573 | ~PathAdder() { |
| 574 | mPath->pop(); |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | Vector<uint32_t> *mPath; |
| 579 | |
| 580 | PathAdder(const PathAdder &); |
| 581 | PathAdder &operator=(const PathAdder &); |
| 582 | }; |
| 583 | |
| 584 | static bool underMetaDataPath(const Vector<uint32_t> &path) { |
| 585 | return path.size() >= 5 |
| 586 | && path[0] == FOURCC('m', 'o', 'o', 'v') |
| 587 | && path[1] == FOURCC('u', 'd', 't', 'a') |
| 588 | && path[2] == FOURCC('m', 'e', 't', 'a') |
| 589 | && path[3] == FOURCC('i', 'l', 's', 't'); |
| 590 | } |
| 591 | |
| 592 | // Given a time in seconds since Jan 1 1904, produce a human-readable string. |
| 593 | static void convertTimeToDate(int64_t time_1904, String8 *s) { |
| 594 | time_t time_1970 = time_1904 - (((66 * 365 + 17) * 24) * 3600); |
| 595 | |
| 596 | char tmp[32]; |
| 597 | strftime(tmp, sizeof(tmp), "%Y%m%dT%H%M%S.000Z", gmtime(&time_1970)); |
| 598 | |
| 599 | s->setTo(tmp); |
| 600 | } |
| 601 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 602 | status_t MPEG4Extractor::parseChunk(off64_t *offset, int depth) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 603 | uint32_t hdr[2]; |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 604 | if (mDataSource->readAt(*offset, hdr, 8) < 8) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 605 | return ERROR_IO; |
| 606 | } |
| 607 | uint64_t chunk_size = ntohl(hdr[0]); |
| 608 | uint32_t chunk_type = ntohl(hdr[1]); |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 609 | off64_t data_offset = *offset + 8; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 610 | |
| 611 | if (chunk_size == 1) { |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 612 | if (mDataSource->readAt(*offset + 8, &chunk_size, 8) < 8) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 613 | return ERROR_IO; |
| 614 | } |
| 615 | chunk_size = ntoh64(chunk_size); |
| 616 | data_offset += 8; |
Andreas Huber | 71b1a4e | 2010-06-24 12:16:25 -0700 | [diff] [blame] | 617 | |
| 618 | if (chunk_size < 16) { |
| 619 | // The smallest valid chunk is 16 bytes long in this case. |
| 620 | return ERROR_MALFORMED; |
| 621 | } |
| 622 | } else if (chunk_size < 8) { |
| 623 | // The smallest valid chunk is 8 bytes long. |
| 624 | return ERROR_MALFORMED; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | char chunk[5]; |
| 628 | MakeFourCCString(chunk_type, chunk); |
| 629 | |
| 630 | #if 0 |
| 631 | static const char kWhitespace[] = " "; |
| 632 | const char *indent = &kWhitespace[sizeof(kWhitespace) - 1 - 2 * depth]; |
| 633 | printf("%sfound chunk '%s' of size %lld\n", indent, chunk, chunk_size); |
| 634 | |
| 635 | char buffer[256]; |
Andreas Huber | ea5ea10 | 2010-02-25 15:59:24 -0800 | [diff] [blame] | 636 | size_t n = chunk_size; |
| 637 | if (n > sizeof(buffer)) { |
| 638 | n = sizeof(buffer); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 639 | } |
Andreas Huber | ea5ea10 | 2010-02-25 15:59:24 -0800 | [diff] [blame] | 640 | if (mDataSource->readAt(*offset, buffer, n) |
| 641 | < (ssize_t)n) { |
| 642 | return ERROR_IO; |
| 643 | } |
| 644 | |
| 645 | hexdump(buffer, n); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 646 | #endif |
| 647 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 648 | PathAdder autoAdder(&mPath, chunk_type); |
| 649 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 650 | off64_t chunk_data_size = *offset + chunk_size - data_offset; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 651 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 652 | if (chunk_type != FOURCC('c', 'p', 'r', 't') |
James Dong | 515e855 | 2011-05-25 15:02:50 -0700 | [diff] [blame] | 653 | && chunk_type != FOURCC('c', 'o', 'v', 'r') |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 654 | && mPath.size() == 5 && underMetaDataPath(mPath)) { |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 655 | off64_t stop_offset = *offset + chunk_size; |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 656 | *offset = data_offset; |
| 657 | while (*offset < stop_offset) { |
| 658 | status_t err = parseChunk(offset, depth + 1); |
| 659 | if (err != OK) { |
| 660 | return err; |
| 661 | } |
| 662 | } |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 663 | |
| 664 | if (*offset != stop_offset) { |
| 665 | return ERROR_MALFORMED; |
| 666 | } |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 667 | |
| 668 | return OK; |
| 669 | } |
| 670 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 671 | switch(chunk_type) { |
| 672 | case FOURCC('m', 'o', 'o', 'v'): |
| 673 | case FOURCC('t', 'r', 'a', 'k'): |
| 674 | case FOURCC('m', 'd', 'i', 'a'): |
| 675 | case FOURCC('m', 'i', 'n', 'f'): |
| 676 | case FOURCC('d', 'i', 'n', 'f'): |
| 677 | case FOURCC('s', 't', 'b', 'l'): |
| 678 | case FOURCC('m', 'v', 'e', 'x'): |
| 679 | case FOURCC('m', 'o', 'o', 'f'): |
| 680 | case FOURCC('t', 'r', 'a', 'f'): |
| 681 | case FOURCC('m', 'f', 'r', 'a'): |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 682 | case FOURCC('u', 'd', 't', 'a'): |
| 683 | case FOURCC('i', 'l', 's', 't'): |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 684 | { |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 685 | if (chunk_type == FOURCC('s', 't', 'b', 'l')) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 686 | ALOGV("sampleTable chunk is %d bytes long.", (size_t)chunk_size); |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 687 | |
Andreas Huber | b9d3038 | 2010-10-15 08:38:49 -0700 | [diff] [blame] | 688 | if (mDataSource->flags() |
| 689 | & (DataSource::kWantsPrefetching |
| 690 | | DataSource::kIsCachingDataSource)) { |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 691 | sp<MPEG4DataSource> cachedSource = |
| 692 | new MPEG4DataSource(mDataSource); |
| 693 | |
| 694 | if (cachedSource->setCachedRange(*offset, chunk_size) == OK) { |
| 695 | mDataSource = cachedSource; |
| 696 | } |
| 697 | } |
Andreas Huber | 1edbcb2 | 2010-05-14 15:28:51 -0700 | [diff] [blame] | 698 | |
| 699 | mLastTrack->sampleTable = new SampleTable(mDataSource); |
Andreas Huber | 50a9976 | 2010-01-28 14:27:37 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Andreas Huber | 6f3a0c2 | 2010-03-12 16:15:53 -0800 | [diff] [blame] | 702 | bool isTrack = false; |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 703 | if (chunk_type == FOURCC('t', 'r', 'a', 'k')) { |
Andreas Huber | 6f3a0c2 | 2010-03-12 16:15:53 -0800 | [diff] [blame] | 704 | isTrack = true; |
| 705 | |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 706 | Track *track = new Track; |
| 707 | track->next = NULL; |
| 708 | if (mLastTrack) { |
| 709 | mLastTrack->next = track; |
| 710 | } else { |
| 711 | mFirstTrack = track; |
| 712 | } |
| 713 | mLastTrack = track; |
| 714 | |
| 715 | track->meta = new MetaData; |
| 716 | track->includes_expensive_metadata = false; |
Andreas Huber | 6f3a0c2 | 2010-03-12 16:15:53 -0800 | [diff] [blame] | 717 | track->skipTrack = false; |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 718 | track->timescale = 0; |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 719 | track->meta->setCString(kKeyMIMEType, "application/octet-stream"); |
| 720 | } |
| 721 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 722 | off64_t stop_offset = *offset + chunk_size; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 723 | *offset = data_offset; |
| 724 | while (*offset < stop_offset) { |
| 725 | status_t err = parseChunk(offset, depth + 1); |
| 726 | if (err != OK) { |
| 727 | return err; |
| 728 | } |
| 729 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 730 | |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 731 | if (*offset != stop_offset) { |
| 732 | return ERROR_MALFORMED; |
| 733 | } |
| 734 | |
Andreas Huber | 6f3a0c2 | 2010-03-12 16:15:53 -0800 | [diff] [blame] | 735 | if (isTrack) { |
| 736 | if (mLastTrack->skipTrack) { |
| 737 | Track *cur = mFirstTrack; |
| 738 | |
| 739 | if (cur == mLastTrack) { |
| 740 | delete cur; |
| 741 | mFirstTrack = mLastTrack = NULL; |
| 742 | } else { |
| 743 | while (cur && cur->next != mLastTrack) { |
| 744 | cur = cur->next; |
| 745 | } |
| 746 | cur->next = NULL; |
| 747 | delete mLastTrack; |
| 748 | mLastTrack = cur; |
| 749 | } |
| 750 | |
| 751 | return OK; |
| 752 | } |
| 753 | |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 754 | status_t err = verifyTrack(mLastTrack); |
| 755 | |
| 756 | if (err != OK) { |
| 757 | return err; |
| 758 | } |
| 759 | } else if (chunk_type == FOURCC('m', 'o', 'o', 'v')) { |
Andreas Huber | bdba1f2 | 2011-03-03 13:48:41 -0800 | [diff] [blame] | 760 | mInitCheck = OK; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 761 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 762 | if (!mIsDrm) { |
| 763 | return UNKNOWN_ERROR; // Return a dummy error. |
| 764 | } else { |
| 765 | return OK; |
| 766 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 767 | } |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | case FOURCC('t', 'k', 'h', 'd'): |
| 772 | { |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 773 | status_t err; |
| 774 | if ((err = parseTrackHeader(data_offset, chunk_data_size)) != OK) { |
| 775 | return err; |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 776 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 777 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 778 | *offset += chunk_size; |
| 779 | break; |
| 780 | } |
| 781 | |
| 782 | case FOURCC('m', 'd', 'h', 'd'): |
| 783 | { |
| 784 | if (chunk_data_size < 4) { |
| 785 | return ERROR_MALFORMED; |
| 786 | } |
| 787 | |
| 788 | uint8_t version; |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 789 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 790 | data_offset, &version, sizeof(version)) |
| 791 | < (ssize_t)sizeof(version)) { |
| 792 | return ERROR_IO; |
| 793 | } |
| 794 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 795 | off64_t timescale_offset; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 796 | |
| 797 | if (version == 1) { |
| 798 | timescale_offset = data_offset + 4 + 16; |
| 799 | } else if (version == 0) { |
| 800 | timescale_offset = data_offset + 4 + 8; |
| 801 | } else { |
| 802 | return ERROR_IO; |
| 803 | } |
| 804 | |
| 805 | uint32_t timescale; |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 806 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 807 | timescale_offset, ×cale, sizeof(timescale)) |
| 808 | < (ssize_t)sizeof(timescale)) { |
| 809 | return ERROR_IO; |
| 810 | } |
| 811 | |
| 812 | mLastTrack->timescale = ntohl(timescale); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 813 | |
| 814 | int64_t duration; |
| 815 | if (version == 1) { |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 816 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 817 | timescale_offset + 4, &duration, sizeof(duration)) |
| 818 | < (ssize_t)sizeof(duration)) { |
| 819 | return ERROR_IO; |
| 820 | } |
| 821 | duration = ntoh64(duration); |
| 822 | } else { |
| 823 | int32_t duration32; |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 824 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 825 | timescale_offset + 4, &duration32, sizeof(duration32)) |
| 826 | < (ssize_t)sizeof(duration32)) { |
| 827 | return ERROR_IO; |
| 828 | } |
| 829 | duration = ntohl(duration32); |
| 830 | } |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 831 | mLastTrack->meta->setInt64( |
| 832 | kKeyDuration, (duration * 1000000) / mLastTrack->timescale); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 833 | |
Gloria Wang | 7a1e3e8 | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 834 | uint8_t lang[2]; |
| 835 | off64_t lang_offset; |
| 836 | if (version == 1) { |
| 837 | lang_offset = timescale_offset + 4 + 8; |
| 838 | } else if (version == 0) { |
| 839 | lang_offset = timescale_offset + 4 + 4; |
| 840 | } else { |
| 841 | return ERROR_IO; |
| 842 | } |
| 843 | |
| 844 | if (mDataSource->readAt(lang_offset, &lang, sizeof(lang)) |
| 845 | < (ssize_t)sizeof(lang)) { |
| 846 | return ERROR_IO; |
| 847 | } |
| 848 | |
| 849 | // To get the ISO-639-2/T three character language code |
| 850 | // 1 bit pad followed by 3 5-bits characters. Each character |
| 851 | // is packed as the difference between its ASCII value and 0x60. |
| 852 | char lang_code[4]; |
| 853 | lang_code[0] = ((lang[0] >> 2) & 0x1f) + 0x60; |
| 854 | lang_code[1] = ((lang[0] & 0x3) << 3 | (lang[1] >> 5)) + 0x60; |
| 855 | lang_code[2] = (lang[1] & 0x1f) + 0x60; |
| 856 | lang_code[3] = '\0'; |
| 857 | |
| 858 | mLastTrack->meta->setCString( |
| 859 | kKeyMediaLanguage, lang_code); |
| 860 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 861 | *offset += chunk_size; |
| 862 | break; |
| 863 | } |
| 864 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 865 | case FOURCC('s', 't', 's', 'd'): |
| 866 | { |
| 867 | if (chunk_data_size < 8) { |
| 868 | return ERROR_MALFORMED; |
| 869 | } |
| 870 | |
| 871 | uint8_t buffer[8]; |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 872 | if (chunk_data_size < (off64_t)sizeof(buffer)) { |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 873 | return ERROR_MALFORMED; |
| 874 | } |
| 875 | |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 876 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 877 | data_offset, buffer, 8) < 8) { |
| 878 | return ERROR_IO; |
| 879 | } |
| 880 | |
| 881 | if (U32_AT(buffer) != 0) { |
| 882 | // Should be version 0, flags 0. |
| 883 | return ERROR_MALFORMED; |
| 884 | } |
| 885 | |
| 886 | uint32_t entry_count = U32_AT(&buffer[4]); |
| 887 | |
| 888 | if (entry_count > 1) { |
Gloria Wang | 3b573f7 | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 889 | // For 3GPP timed text, there could be multiple tx3g boxes contain |
| 890 | // multiple text display formats. These formats will be used to |
| 891 | // display the timed text. |
| 892 | const char *mime; |
| 893 | CHECK(mLastTrack->meta->findCString(kKeyMIMEType, &mime)); |
| 894 | if (strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) { |
| 895 | // For now we only support a single type of media per track. |
| 896 | mLastTrack->skipTrack = true; |
| 897 | *offset += chunk_size; |
| 898 | break; |
| 899 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 900 | } |
| 901 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 902 | off64_t stop_offset = *offset + chunk_size; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 903 | *offset = data_offset + 8; |
| 904 | for (uint32_t i = 0; i < entry_count; ++i) { |
| 905 | status_t err = parseChunk(offset, depth + 1); |
| 906 | if (err != OK) { |
| 907 | return err; |
| 908 | } |
| 909 | } |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 910 | |
| 911 | if (*offset != stop_offset) { |
| 912 | return ERROR_MALFORMED; |
| 913 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 914 | break; |
| 915 | } |
| 916 | |
| 917 | case FOURCC('m', 'p', '4', 'a'): |
| 918 | case FOURCC('s', 'a', 'm', 'r'): |
Andreas Huber | 371e243 | 2009-09-09 16:32:59 -0700 | [diff] [blame] | 919 | case FOURCC('s', 'a', 'w', 'b'): |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 920 | { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 921 | uint8_t buffer[8 + 20]; |
| 922 | if (chunk_data_size < (ssize_t)sizeof(buffer)) { |
| 923 | // Basic AudioSampleEntry size. |
| 924 | return ERROR_MALFORMED; |
| 925 | } |
| 926 | |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 927 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 928 | data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { |
| 929 | return ERROR_IO; |
| 930 | } |
| 931 | |
| 932 | uint16_t data_ref_index = U16_AT(&buffer[6]); |
| 933 | uint16_t num_channels = U16_AT(&buffer[16]); |
| 934 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 935 | uint16_t sample_size = U16_AT(&buffer[18]); |
| 936 | uint32_t sample_rate = U32_AT(&buffer[24]) >> 16; |
| 937 | |
Andreas Huber | 81f82c3 | 2010-04-12 16:05:57 -0700 | [diff] [blame] | 938 | if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, |
| 939 | FourCC2MIME(chunk_type))) { |
| 940 | // AMR NB audio is always mono, 8kHz |
| 941 | num_channels = 1; |
| 942 | sample_rate = 8000; |
| 943 | } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, |
| 944 | FourCC2MIME(chunk_type))) { |
| 945 | // AMR WB audio is always mono, 16kHz |
| 946 | num_channels = 1; |
| 947 | sample_rate = 16000; |
| 948 | } |
| 949 | |
| 950 | #if 0 |
| 951 | printf("*** coding='%s' %d channels, size %d, rate %d\n", |
| 952 | chunk, num_channels, sample_size, sample_rate); |
| 953 | #endif |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 954 | |
| 955 | mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); |
| 956 | mLastTrack->meta->setInt32(kKeyChannelCount, num_channels); |
| 957 | mLastTrack->meta->setInt32(kKeySampleRate, sample_rate); |
| 958 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 959 | off64_t stop_offset = *offset + chunk_size; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 960 | *offset = data_offset + sizeof(buffer); |
| 961 | while (*offset < stop_offset) { |
| 962 | status_t err = parseChunk(offset, depth + 1); |
| 963 | if (err != OK) { |
| 964 | return err; |
| 965 | } |
| 966 | } |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 967 | |
| 968 | if (*offset != stop_offset) { |
| 969 | return ERROR_MALFORMED; |
| 970 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | |
| 974 | case FOURCC('m', 'p', '4', 'v'): |
| 975 | case FOURCC('s', '2', '6', '3'): |
James Dong | e071b05 | 2010-12-15 15:49:46 -0800 | [diff] [blame] | 976 | case FOURCC('H', '2', '6', '3'): |
| 977 | case FOURCC('h', '2', '6', '3'): |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 978 | case FOURCC('a', 'v', 'c', '1'): |
| 979 | { |
Andreas Huber | 7be6407 | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 980 | mHasVideo = true; |
| 981 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 982 | uint8_t buffer[78]; |
| 983 | if (chunk_data_size < (ssize_t)sizeof(buffer)) { |
| 984 | // Basic VideoSampleEntry size. |
| 985 | return ERROR_MALFORMED; |
| 986 | } |
| 987 | |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 988 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 989 | data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { |
| 990 | return ERROR_IO; |
| 991 | } |
| 992 | |
| 993 | uint16_t data_ref_index = U16_AT(&buffer[6]); |
| 994 | uint16_t width = U16_AT(&buffer[6 + 18]); |
| 995 | uint16_t height = U16_AT(&buffer[6 + 20]); |
| 996 | |
James Dong | a625623 | 2010-12-15 17:32:45 -0800 | [diff] [blame] | 997 | // The video sample is not stand-compliant if it has invalid dimension. |
| 998 | // Use some default width and height value, and |
| 999 | // let the decoder figure out the actual width and height (and thus |
| 1000 | // be prepared for INFO_FOMRAT_CHANGED event). |
| 1001 | if (width == 0) width = 352; |
| 1002 | if (height == 0) height = 288; |
| 1003 | |
Andreas Huber | 134ee6a | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 1004 | // printf("*** coding='%s' width=%d height=%d\n", |
| 1005 | // chunk, width, height); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1006 | |
| 1007 | mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); |
| 1008 | mLastTrack->meta->setInt32(kKeyWidth, width); |
| 1009 | mLastTrack->meta->setInt32(kKeyHeight, height); |
| 1010 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1011 | off64_t stop_offset = *offset + chunk_size; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1012 | *offset = data_offset + sizeof(buffer); |
| 1013 | while (*offset < stop_offset) { |
| 1014 | status_t err = parseChunk(offset, depth + 1); |
| 1015 | if (err != OK) { |
| 1016 | return err; |
| 1017 | } |
| 1018 | } |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 1019 | |
| 1020 | if (*offset != stop_offset) { |
| 1021 | return ERROR_MALFORMED; |
| 1022 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1023 | break; |
| 1024 | } |
| 1025 | |
| 1026 | case FOURCC('s', 't', 'c', 'o'): |
| 1027 | case FOURCC('c', 'o', '6', '4'): |
| 1028 | { |
| 1029 | status_t err = |
| 1030 | mLastTrack->sampleTable->setChunkOffsetParams( |
| 1031 | chunk_type, data_offset, chunk_data_size); |
| 1032 | |
| 1033 | if (err != OK) { |
| 1034 | return err; |
| 1035 | } |
| 1036 | |
| 1037 | *offset += chunk_size; |
| 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | case FOURCC('s', 't', 's', 'c'): |
| 1042 | { |
| 1043 | status_t err = |
| 1044 | mLastTrack->sampleTable->setSampleToChunkParams( |
| 1045 | data_offset, chunk_data_size); |
| 1046 | |
| 1047 | if (err != OK) { |
| 1048 | return err; |
| 1049 | } |
| 1050 | |
| 1051 | *offset += chunk_size; |
| 1052 | break; |
| 1053 | } |
| 1054 | |
| 1055 | case FOURCC('s', 't', 's', 'z'): |
| 1056 | case FOURCC('s', 't', 'z', '2'): |
| 1057 | { |
| 1058 | status_t err = |
| 1059 | mLastTrack->sampleTable->setSampleSizeParams( |
| 1060 | chunk_type, data_offset, chunk_data_size); |
| 1061 | |
| 1062 | if (err != OK) { |
| 1063 | return err; |
| 1064 | } |
| 1065 | |
Andreas Huber | 738c431 | 2009-11-23 14:03:32 -0800 | [diff] [blame] | 1066 | size_t max_size; |
Andreas Huber | 49110ce | 2010-09-09 10:10:15 -0700 | [diff] [blame] | 1067 | err = mLastTrack->sampleTable->getMaxSampleSize(&max_size); |
| 1068 | |
| 1069 | if (err != OK) { |
| 1070 | return err; |
| 1071 | } |
Andreas Huber | 738c431 | 2009-11-23 14:03:32 -0800 | [diff] [blame] | 1072 | |
| 1073 | // Assume that a given buffer only contains at most 10 fragments, |
| 1074 | // each fragment originally prefixed with a 2 byte length will |
| 1075 | // have a 4 byte header (0x00 0x00 0x00 0x01) after conversion, |
| 1076 | // and thus will grow by 2 bytes per fragment. |
| 1077 | mLastTrack->meta->setInt32(kKeyMaxInputSize, max_size + 10 * 2); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1078 | *offset += chunk_size; |
James Dong | 113a954 | 2011-01-19 00:13:55 -0800 | [diff] [blame] | 1079 | |
| 1080 | // Calculate average frame rate. |
| 1081 | const char *mime; |
| 1082 | CHECK(mLastTrack->meta->findCString(kKeyMIMEType, &mime)); |
| 1083 | if (!strncasecmp("video/", mime, 6)) { |
| 1084 | size_t nSamples = mLastTrack->sampleTable->countSamples(); |
| 1085 | int64_t durationUs; |
| 1086 | if (mLastTrack->meta->findInt64(kKeyDuration, &durationUs)) { |
| 1087 | if (durationUs > 0) { |
| 1088 | int32_t frameRate = (nSamples * 1000000LL + |
| 1089 | (durationUs >> 1)) / durationUs; |
| 1090 | mLastTrack->meta->setInt32(kKeyFrameRate, frameRate); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | case FOURCC('s', 't', 't', 's'): |
| 1099 | { |
| 1100 | status_t err = |
| 1101 | mLastTrack->sampleTable->setTimeToSampleParams( |
| 1102 | data_offset, chunk_data_size); |
| 1103 | |
| 1104 | if (err != OK) { |
| 1105 | return err; |
| 1106 | } |
| 1107 | |
| 1108 | *offset += chunk_size; |
| 1109 | break; |
| 1110 | } |
| 1111 | |
Andreas Huber | 4931bb5 | 2011-02-03 13:18:16 -0800 | [diff] [blame] | 1112 | case FOURCC('c', 't', 't', 's'): |
| 1113 | { |
| 1114 | status_t err = |
| 1115 | mLastTrack->sampleTable->setCompositionTimeToSampleParams( |
| 1116 | data_offset, chunk_data_size); |
| 1117 | |
| 1118 | if (err != OK) { |
| 1119 | return err; |
| 1120 | } |
| 1121 | |
| 1122 | *offset += chunk_size; |
| 1123 | break; |
| 1124 | } |
| 1125 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1126 | case FOURCC('s', 't', 's', 's'): |
| 1127 | { |
| 1128 | status_t err = |
| 1129 | mLastTrack->sampleTable->setSyncSampleParams( |
| 1130 | data_offset, chunk_data_size); |
| 1131 | |
| 1132 | if (err != OK) { |
| 1133 | return err; |
| 1134 | } |
| 1135 | |
| 1136 | *offset += chunk_size; |
| 1137 | break; |
| 1138 | } |
| 1139 | |
James Dong | 4948818 | 2011-11-09 00:48:56 -0800 | [diff] [blame] | 1140 | // @xyz |
| 1141 | case FOURCC('\xA9', 'x', 'y', 'z'): |
| 1142 | { |
| 1143 | // Best case the total data length inside "@xyz" box |
| 1144 | // would be 8, for instance "@xyz" + "\x00\x04\x15\xc7" + "0+0/", |
| 1145 | // where "\x00\x04" is the text string length with value = 4, |
| 1146 | // "\0x15\xc7" is the language code = en, and "0+0" is a |
| 1147 | // location (string) value with longitude = 0 and latitude = 0. |
| 1148 | if (chunk_data_size < 8) { |
| 1149 | return ERROR_MALFORMED; |
| 1150 | } |
| 1151 | |
| 1152 | // Worst case the location string length would be 18, |
| 1153 | // for instance +90.0000-180.0000, without the trailing "/" and |
| 1154 | // the string length + language code. |
| 1155 | char buffer[18]; |
| 1156 | |
| 1157 | // Substracting 5 from the data size is because the text string length + |
| 1158 | // language code takes 4 bytes, and the trailing slash "/" takes 1 byte. |
| 1159 | off64_t location_length = chunk_data_size - 5; |
| 1160 | if (location_length >= (off64_t) sizeof(buffer)) { |
| 1161 | return ERROR_MALFORMED; |
| 1162 | } |
| 1163 | |
| 1164 | if (mDataSource->readAt( |
| 1165 | data_offset + 4, buffer, location_length) < location_length) { |
| 1166 | return ERROR_IO; |
| 1167 | } |
| 1168 | |
| 1169 | buffer[location_length] = '\0'; |
| 1170 | mFileMetaData->setCString(kKeyLocation, buffer); |
| 1171 | *offset += chunk_size; |
| 1172 | break; |
| 1173 | } |
| 1174 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1175 | case FOURCC('e', 's', 'd', 's'): |
| 1176 | { |
| 1177 | if (chunk_data_size < 4) { |
| 1178 | return ERROR_MALFORMED; |
| 1179 | } |
| 1180 | |
| 1181 | uint8_t buffer[256]; |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1182 | if (chunk_data_size > (off64_t)sizeof(buffer)) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1183 | return ERROR_BUFFER_TOO_SMALL; |
| 1184 | } |
| 1185 | |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 1186 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1187 | data_offset, buffer, chunk_data_size) < chunk_data_size) { |
| 1188 | return ERROR_IO; |
| 1189 | } |
| 1190 | |
| 1191 | if (U32_AT(buffer) != 0) { |
| 1192 | // Should be version 0, flags 0. |
| 1193 | return ERROR_MALFORMED; |
| 1194 | } |
| 1195 | |
| 1196 | mLastTrack->meta->setData( |
| 1197 | kKeyESDS, kTypeESDS, &buffer[4], chunk_data_size - 4); |
| 1198 | |
Andreas Huber | ba7c7ee | 2010-01-29 14:46:59 -0800 | [diff] [blame] | 1199 | if (mPath.size() >= 2 |
| 1200 | && mPath[mPath.size() - 2] == FOURCC('m', 'p', '4', 'a')) { |
| 1201 | // Information from the ESDS must be relied on for proper |
| 1202 | // setup of sample rate and channel count for MPEG4 Audio. |
| 1203 | // The generic header appears to only contain generic |
| 1204 | // information... |
| 1205 | |
| 1206 | status_t err = updateAudioTrackInfoFromESDS_MPEG4Audio( |
| 1207 | &buffer[4], chunk_data_size - 4); |
| 1208 | |
| 1209 | if (err != OK) { |
| 1210 | return err; |
| 1211 | } |
| 1212 | } |
| 1213 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1214 | *offset += chunk_size; |
| 1215 | break; |
| 1216 | } |
| 1217 | |
| 1218 | case FOURCC('a', 'v', 'c', 'C'): |
| 1219 | { |
| 1220 | char buffer[256]; |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1221 | if (chunk_data_size > (off64_t)sizeof(buffer)) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1222 | return ERROR_BUFFER_TOO_SMALL; |
| 1223 | } |
| 1224 | |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 1225 | if (mDataSource->readAt( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1226 | data_offset, buffer, chunk_data_size) < chunk_data_size) { |
| 1227 | return ERROR_IO; |
| 1228 | } |
| 1229 | |
| 1230 | mLastTrack->meta->setData( |
| 1231 | kKeyAVCC, kTypeAVCC, buffer, chunk_data_size); |
| 1232 | |
| 1233 | *offset += chunk_size; |
| 1234 | break; |
| 1235 | } |
| 1236 | |
James Dong | 244ba87 | 2011-02-11 15:21:45 -0800 | [diff] [blame] | 1237 | case FOURCC('d', '2', '6', '3'): |
| 1238 | { |
James Dong | 17e89b5 | 2011-02-23 17:07:49 -0800 | [diff] [blame] | 1239 | /* |
| 1240 | * d263 contains a fixed 7 bytes part: |
| 1241 | * vendor - 4 bytes |
| 1242 | * version - 1 byte |
| 1243 | * level - 1 byte |
| 1244 | * profile - 1 byte |
| 1245 | * optionally, "d263" box itself may contain a 16-byte |
| 1246 | * bit rate box (bitr) |
| 1247 | * average bit rate - 4 bytes |
| 1248 | * max bit rate - 4 bytes |
| 1249 | */ |
| 1250 | char buffer[23]; |
| 1251 | if (chunk_data_size != 7 && |
| 1252 | chunk_data_size != 23) { |
James Dong | 244ba87 | 2011-02-11 15:21:45 -0800 | [diff] [blame] | 1253 | LOGE("Incorrect D263 box size %lld", chunk_data_size); |
| 1254 | return ERROR_MALFORMED; |
| 1255 | } |
| 1256 | |
| 1257 | if (mDataSource->readAt( |
| 1258 | data_offset, buffer, chunk_data_size) < chunk_data_size) { |
| 1259 | return ERROR_IO; |
| 1260 | } |
| 1261 | |
| 1262 | mLastTrack->meta->setData(kKeyD263, kTypeD263, buffer, chunk_data_size); |
| 1263 | |
| 1264 | *offset += chunk_size; |
| 1265 | break; |
| 1266 | } |
| 1267 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1268 | case FOURCC('m', 'e', 't', 'a'): |
| 1269 | { |
| 1270 | uint8_t buffer[4]; |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1271 | if (chunk_data_size < (off64_t)sizeof(buffer)) { |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 1272 | return ERROR_MALFORMED; |
| 1273 | } |
| 1274 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1275 | if (mDataSource->readAt( |
| 1276 | data_offset, buffer, 4) < 4) { |
| 1277 | return ERROR_IO; |
| 1278 | } |
| 1279 | |
| 1280 | if (U32_AT(buffer) != 0) { |
| 1281 | // Should be version 0, flags 0. |
Andreas Huber | ea5ea10 | 2010-02-25 15:59:24 -0800 | [diff] [blame] | 1282 | |
| 1283 | // If it's not, let's assume this is one of those |
| 1284 | // apparently malformed chunks that don't have flags |
| 1285 | // and completely different semantics than what's |
| 1286 | // in the MPEG4 specs and skip it. |
| 1287 | *offset += chunk_size; |
| 1288 | return OK; |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1291 | off64_t stop_offset = *offset + chunk_size; |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1292 | *offset = data_offset + sizeof(buffer); |
| 1293 | while (*offset < stop_offset) { |
| 1294 | status_t err = parseChunk(offset, depth + 1); |
| 1295 | if (err != OK) { |
| 1296 | return err; |
| 1297 | } |
| 1298 | } |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 1299 | |
| 1300 | if (*offset != stop_offset) { |
| 1301 | return ERROR_MALFORMED; |
| 1302 | } |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | case FOURCC('d', 'a', 't', 'a'): |
| 1307 | { |
| 1308 | if (mPath.size() == 6 && underMetaDataPath(mPath)) { |
| 1309 | status_t err = parseMetaData(data_offset, chunk_data_size); |
| 1310 | |
| 1311 | if (err != OK) { |
| 1312 | return err; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | *offset += chunk_size; |
| 1317 | break; |
| 1318 | } |
| 1319 | |
| 1320 | case FOURCC('m', 'v', 'h', 'd'): |
| 1321 | { |
| 1322 | if (chunk_data_size < 12) { |
| 1323 | return ERROR_MALFORMED; |
| 1324 | } |
| 1325 | |
| 1326 | uint8_t header[12]; |
| 1327 | if (mDataSource->readAt( |
| 1328 | data_offset, header, sizeof(header)) |
| 1329 | < (ssize_t)sizeof(header)) { |
| 1330 | return ERROR_IO; |
| 1331 | } |
| 1332 | |
| 1333 | int64_t creationTime; |
| 1334 | if (header[0] == 1) { |
| 1335 | creationTime = U64_AT(&header[4]); |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 1336 | } else if (header[0] != 0) { |
| 1337 | return ERROR_MALFORMED; |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1338 | } else { |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1339 | creationTime = U32_AT(&header[4]); |
| 1340 | } |
| 1341 | |
| 1342 | String8 s; |
| 1343 | convertTimeToDate(creationTime, &s); |
| 1344 | |
| 1345 | mFileMetaData->setCString(kKeyDate, s.string()); |
| 1346 | |
| 1347 | *offset += chunk_size; |
| 1348 | break; |
| 1349 | } |
| 1350 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 1351 | case FOURCC('m', 'd', 'a', 't'): |
| 1352 | { |
| 1353 | if (!mIsDrm) { |
| 1354 | *offset += chunk_size; |
| 1355 | break; |
| 1356 | } |
| 1357 | |
| 1358 | if (chunk_size < 8) { |
| 1359 | return ERROR_MALFORMED; |
| 1360 | } |
| 1361 | |
| 1362 | return parseDrmSINF(offset, data_offset); |
| 1363 | } |
| 1364 | |
Gloria Wang | 3b573f7 | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 1365 | case FOURCC('h', 'd', 'l', 'r'): |
| 1366 | { |
| 1367 | uint32_t buffer; |
| 1368 | if (mDataSource->readAt( |
| 1369 | data_offset + 8, &buffer, 4) < 4) { |
| 1370 | return ERROR_IO; |
| 1371 | } |
| 1372 | |
| 1373 | uint32_t type = ntohl(buffer); |
| 1374 | // For the 3GPP file format, the handler-type within the 'hdlr' box |
Insun Kang | e26dd87 | 2011-11-10 16:11:26 +0900 | [diff] [blame] | 1375 | // shall be 'text'. We also want to support 'sbtl' handler type |
| 1376 | // for a practical reason as various MPEG4 containers use it. |
| 1377 | if (type == FOURCC('t', 'e', 'x', 't') || type == FOURCC('s', 'b', 't', 'l')) { |
Gloria Wang | 3b573f7 | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 1378 | mLastTrack->meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_TEXT_3GPP); |
| 1379 | } |
| 1380 | |
| 1381 | *offset += chunk_size; |
| 1382 | break; |
| 1383 | } |
| 1384 | |
Gloria Wang | 7a1e3e8 | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 1385 | case FOURCC('t', 'x', '3', 'g'): |
| 1386 | { |
Gloria Wang | 3b573f7 | 2011-05-31 16:08:47 -0700 | [diff] [blame] | 1387 | uint32_t type; |
| 1388 | const void *data; |
| 1389 | size_t size = 0; |
| 1390 | if (!mLastTrack->meta->findData( |
| 1391 | kKeyTextFormatData, &type, &data, &size)) { |
| 1392 | size = 0; |
| 1393 | } |
| 1394 | |
| 1395 | uint8_t *buffer = new uint8_t[size + chunk_size]; |
| 1396 | |
| 1397 | if (size > 0) { |
| 1398 | memcpy(buffer, data, size); |
| 1399 | } |
| 1400 | |
| 1401 | if ((size_t)(mDataSource->readAt(*offset, buffer + size, chunk_size)) |
| 1402 | < chunk_size) { |
| 1403 | delete[] buffer; |
| 1404 | buffer = NULL; |
| 1405 | |
| 1406 | return ERROR_IO; |
| 1407 | } |
| 1408 | |
| 1409 | mLastTrack->meta->setData( |
| 1410 | kKeyTextFormatData, 0, buffer, size + chunk_size); |
| 1411 | |
| 1412 | delete[] buffer; |
Gloria Wang | 7a1e3e8 | 2011-05-03 15:59:03 -0700 | [diff] [blame] | 1413 | |
| 1414 | *offset += chunk_size; |
| 1415 | break; |
| 1416 | } |
| 1417 | |
James Dong | 515e855 | 2011-05-25 15:02:50 -0700 | [diff] [blame] | 1418 | case FOURCC('c', 'o', 'v', 'r'): |
| 1419 | { |
| 1420 | if (mFileMetaData != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1421 | ALOGV("chunk_data_size = %lld and data_offset = %lld", |
James Dong | 515e855 | 2011-05-25 15:02:50 -0700 | [diff] [blame] | 1422 | chunk_data_size, data_offset); |
| 1423 | uint8_t *buffer = new uint8_t[chunk_data_size + 1]; |
| 1424 | if (mDataSource->readAt( |
| 1425 | data_offset, buffer, chunk_data_size) != (ssize_t)chunk_data_size) { |
| 1426 | delete[] buffer; |
| 1427 | buffer = NULL; |
| 1428 | |
| 1429 | return ERROR_IO; |
| 1430 | } |
| 1431 | const int kSkipBytesOfDataBox = 16; |
| 1432 | mFileMetaData->setData( |
| 1433 | kKeyAlbumArt, MetaData::TYPE_NONE, |
| 1434 | buffer + kSkipBytesOfDataBox, chunk_data_size - kSkipBytesOfDataBox); |
| 1435 | } |
| 1436 | |
| 1437 | *offset += chunk_size; |
| 1438 | break; |
| 1439 | } |
| 1440 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1441 | default: |
| 1442 | { |
| 1443 | *offset += chunk_size; |
| 1444 | break; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | return OK; |
| 1449 | } |
| 1450 | |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1451 | status_t MPEG4Extractor::parseTrackHeader( |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1452 | off64_t data_offset, off64_t data_size) { |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1453 | if (data_size < 4) { |
| 1454 | return ERROR_MALFORMED; |
| 1455 | } |
| 1456 | |
| 1457 | uint8_t version; |
| 1458 | if (mDataSource->readAt(data_offset, &version, 1) < 1) { |
| 1459 | return ERROR_IO; |
| 1460 | } |
| 1461 | |
| 1462 | size_t dynSize = (version == 1) ? 36 : 24; |
| 1463 | |
| 1464 | uint8_t buffer[36 + 60]; |
| 1465 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1466 | if (data_size != (off64_t)dynSize + 60) { |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1467 | return ERROR_MALFORMED; |
| 1468 | } |
| 1469 | |
| 1470 | if (mDataSource->readAt( |
| 1471 | data_offset, buffer, data_size) < (ssize_t)data_size) { |
| 1472 | return ERROR_IO; |
| 1473 | } |
| 1474 | |
| 1475 | uint64_t ctime, mtime, duration; |
| 1476 | int32_t id; |
| 1477 | |
| 1478 | if (version == 1) { |
| 1479 | ctime = U64_AT(&buffer[4]); |
| 1480 | mtime = U64_AT(&buffer[12]); |
| 1481 | id = U32_AT(&buffer[20]); |
| 1482 | duration = U64_AT(&buffer[28]); |
| 1483 | } else { |
| 1484 | CHECK_EQ((unsigned)version, 0u); |
| 1485 | |
| 1486 | ctime = U32_AT(&buffer[4]); |
| 1487 | mtime = U32_AT(&buffer[8]); |
| 1488 | id = U32_AT(&buffer[12]); |
| 1489 | duration = U32_AT(&buffer[20]); |
| 1490 | } |
| 1491 | |
| 1492 | mLastTrack->meta->setInt32(kKeyTrackID, id); |
| 1493 | |
| 1494 | size_t matrixOffset = dynSize + 16; |
| 1495 | int32_t a00 = U32_AT(&buffer[matrixOffset]); |
| 1496 | int32_t a01 = U32_AT(&buffer[matrixOffset + 4]); |
| 1497 | int32_t dx = U32_AT(&buffer[matrixOffset + 8]); |
| 1498 | int32_t a10 = U32_AT(&buffer[matrixOffset + 12]); |
| 1499 | int32_t a11 = U32_AT(&buffer[matrixOffset + 16]); |
| 1500 | int32_t dy = U32_AT(&buffer[matrixOffset + 20]); |
| 1501 | |
| 1502 | #if 0 |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1503 | ALOGI("x' = %.2f * x + %.2f * y + %.2f", |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1504 | a00 / 65536.0f, a01 / 65536.0f, dx / 65536.0f); |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1505 | ALOGI("y' = %.2f * x + %.2f * y + %.2f", |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1506 | a10 / 65536.0f, a11 / 65536.0f, dy / 65536.0f); |
| 1507 | #endif |
| 1508 | |
| 1509 | uint32_t rotationDegrees; |
| 1510 | |
| 1511 | static const int32_t kFixedOne = 0x10000; |
| 1512 | if (a00 == kFixedOne && a01 == 0 && a10 == 0 && a11 == kFixedOne) { |
| 1513 | // Identity, no rotation |
| 1514 | rotationDegrees = 0; |
| 1515 | } else if (a00 == 0 && a01 == kFixedOne && a10 == -kFixedOne && a11 == 0) { |
| 1516 | rotationDegrees = 90; |
| 1517 | } else if (a00 == 0 && a01 == -kFixedOne && a10 == kFixedOne && a11 == 0) { |
| 1518 | rotationDegrees = 270; |
| 1519 | } else if (a00 == -kFixedOne && a01 == 0 && a10 == 0 && a11 == -kFixedOne) { |
| 1520 | rotationDegrees = 180; |
| 1521 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame^] | 1522 | ALOGW("We only support 0,90,180,270 degree rotation matrices"); |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1523 | rotationDegrees = 0; |
| 1524 | } |
| 1525 | |
| 1526 | if (rotationDegrees != 0) { |
| 1527 | mLastTrack->meta->setInt32(kKeyRotation, rotationDegrees); |
| 1528 | } |
| 1529 | |
James Dong | b45c01c | 2011-01-16 11:30:13 -0800 | [diff] [blame] | 1530 | // Handle presentation display size, which could be different |
| 1531 | // from the image size indicated by kKeyWidth and kKeyHeight. |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1532 | uint32_t width = U32_AT(&buffer[dynSize + 52]); |
| 1533 | uint32_t height = U32_AT(&buffer[dynSize + 56]); |
James Dong | b45c01c | 2011-01-16 11:30:13 -0800 | [diff] [blame] | 1534 | mLastTrack->meta->setInt32(kKeyDisplayWidth, width >> 16); |
| 1535 | mLastTrack->meta->setInt32(kKeyDisplayHeight, height >> 16); |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1536 | |
| 1537 | return OK; |
| 1538 | } |
| 1539 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 1540 | status_t MPEG4Extractor::parseMetaData(off64_t offset, size_t size) { |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1541 | if (size < 4) { |
| 1542 | return ERROR_MALFORMED; |
| 1543 | } |
| 1544 | |
| 1545 | uint8_t *buffer = new uint8_t[size + 1]; |
| 1546 | if (mDataSource->readAt( |
| 1547 | offset, buffer, size) != (ssize_t)size) { |
| 1548 | delete[] buffer; |
| 1549 | buffer = NULL; |
| 1550 | |
| 1551 | return ERROR_IO; |
| 1552 | } |
| 1553 | |
| 1554 | uint32_t flags = U32_AT(buffer); |
| 1555 | |
| 1556 | uint32_t metadataKey = 0; |
| 1557 | switch (mPath[4]) { |
| 1558 | case FOURCC(0xa9, 'a', 'l', 'b'): |
| 1559 | { |
| 1560 | metadataKey = kKeyAlbum; |
| 1561 | break; |
| 1562 | } |
| 1563 | case FOURCC(0xa9, 'A', 'R', 'T'): |
| 1564 | { |
| 1565 | metadataKey = kKeyArtist; |
| 1566 | break; |
| 1567 | } |
Marco Nelissen | 66ac4df | 2010-02-11 13:31:44 -0800 | [diff] [blame] | 1568 | case FOURCC('a', 'A', 'R', 'T'): |
| 1569 | { |
| 1570 | metadataKey = kKeyAlbumArtist; |
| 1571 | break; |
| 1572 | } |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1573 | case FOURCC(0xa9, 'd', 'a', 'y'): |
| 1574 | { |
| 1575 | metadataKey = kKeyYear; |
| 1576 | break; |
| 1577 | } |
| 1578 | case FOURCC(0xa9, 'n', 'a', 'm'): |
| 1579 | { |
| 1580 | metadataKey = kKeyTitle; |
| 1581 | break; |
| 1582 | } |
| 1583 | case FOURCC(0xa9, 'w', 'r', 't'): |
| 1584 | { |
| 1585 | metadataKey = kKeyWriter; |
| 1586 | break; |
| 1587 | } |
| 1588 | case FOURCC('c', 'o', 'v', 'r'): |
| 1589 | { |
| 1590 | metadataKey = kKeyAlbumArt; |
| 1591 | break; |
| 1592 | } |
| 1593 | case FOURCC('g', 'n', 'r', 'e'): |
| 1594 | { |
| 1595 | metadataKey = kKeyGenre; |
| 1596 | break; |
| 1597 | } |
Marco Nelissen | 26bae54 | 2010-02-09 14:02:16 -0800 | [diff] [blame] | 1598 | case FOURCC(0xa9, 'g', 'e', 'n'): |
| 1599 | { |
| 1600 | metadataKey = kKeyGenre; |
| 1601 | break; |
| 1602 | } |
Marco Nelissen | 11f8109 | 2011-01-06 11:12:17 -0800 | [diff] [blame] | 1603 | case FOURCC('c', 'p', 'i', 'l'): |
| 1604 | { |
| 1605 | if (size == 9 && flags == 21) { |
| 1606 | char tmp[16]; |
| 1607 | sprintf(tmp, "%d", |
| 1608 | (int)buffer[size - 1]); |
| 1609 | |
| 1610 | mFileMetaData->setCString(kKeyCompilation, tmp); |
| 1611 | } |
| 1612 | break; |
| 1613 | } |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1614 | case FOURCC('t', 'r', 'k', 'n'): |
| 1615 | { |
| 1616 | if (size == 16 && flags == 0) { |
| 1617 | char tmp[16]; |
| 1618 | sprintf(tmp, "%d/%d", |
| 1619 | (int)buffer[size - 5], (int)buffer[size - 3]); |
| 1620 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1621 | mFileMetaData->setCString(kKeyCDTrackNumber, tmp); |
| 1622 | } |
| 1623 | break; |
| 1624 | } |
Marco Nelissen | 26bae54 | 2010-02-09 14:02:16 -0800 | [diff] [blame] | 1625 | case FOURCC('d', 'i', 's', 'k'): |
| 1626 | { |
| 1627 | if (size == 14 && flags == 0) { |
| 1628 | char tmp[16]; |
| 1629 | sprintf(tmp, "%d/%d", |
| 1630 | (int)buffer[size - 3], (int)buffer[size - 1]); |
| 1631 | |
| 1632 | mFileMetaData->setCString(kKeyDiscNumber, tmp); |
| 1633 | } |
| 1634 | break; |
| 1635 | } |
| 1636 | |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1637 | default: |
| 1638 | break; |
| 1639 | } |
| 1640 | |
| 1641 | if (size >= 8 && metadataKey) { |
| 1642 | if (metadataKey == kKeyAlbumArt) { |
| 1643 | mFileMetaData->setData( |
| 1644 | kKeyAlbumArt, MetaData::TYPE_NONE, |
| 1645 | buffer + 8, size - 8); |
| 1646 | } else if (metadataKey == kKeyGenre) { |
| 1647 | if (flags == 0) { |
Marco Nelissen | 26bae54 | 2010-02-09 14:02:16 -0800 | [diff] [blame] | 1648 | // uint8_t genre code, iTunes genre codes are |
| 1649 | // the standard id3 codes, except they start |
| 1650 | // at 1 instead of 0 (e.g. Pop is 14, not 13) |
| 1651 | // We use standard id3 numbering, so subtract 1. |
| 1652 | int genrecode = (int)buffer[size - 1]; |
| 1653 | genrecode--; |
| 1654 | if (genrecode < 0) { |
| 1655 | genrecode = 255; // reserved for 'unknown genre' |
| 1656 | } |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1657 | char genre[10]; |
Marco Nelissen | 26bae54 | 2010-02-09 14:02:16 -0800 | [diff] [blame] | 1658 | sprintf(genre, "%d", genrecode); |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1659 | |
| 1660 | mFileMetaData->setCString(metadataKey, genre); |
Marco Nelissen | 26bae54 | 2010-02-09 14:02:16 -0800 | [diff] [blame] | 1661 | } else if (flags == 1) { |
| 1662 | // custom genre string |
| 1663 | buffer[size] = '\0'; |
| 1664 | |
| 1665 | mFileMetaData->setCString( |
| 1666 | metadataKey, (const char *)buffer + 8); |
Andreas Huber | 72b8c5a | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 1667 | } |
| 1668 | } else { |
| 1669 | buffer[size] = '\0'; |
| 1670 | |
| 1671 | mFileMetaData->setCString( |
| 1672 | metadataKey, (const char *)buffer + 8); |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | delete[] buffer; |
| 1677 | buffer = NULL; |
| 1678 | |
| 1679 | return OK; |
| 1680 | } |
| 1681 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1682 | sp<MediaSource> MPEG4Extractor::getTrack(size_t index) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1683 | status_t err; |
| 1684 | if ((err = readMetaData()) != OK) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1685 | return NULL; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | Track *track = mFirstTrack; |
| 1689 | while (index > 0) { |
| 1690 | if (track == NULL) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1691 | return NULL; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | track = track->next; |
| 1695 | --index; |
| 1696 | } |
| 1697 | |
Andreas Huber | 371e243 | 2009-09-09 16:32:59 -0700 | [diff] [blame] | 1698 | if (track == NULL) { |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1702 | return new MPEG4Source( |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 1703 | track->meta, mDataSource, track->timescale, track->sampleTable); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 1706 | // static |
| 1707 | status_t MPEG4Extractor::verifyTrack(Track *track) { |
| 1708 | const char *mime; |
| 1709 | CHECK(track->meta->findCString(kKeyMIMEType, &mime)); |
| 1710 | |
| 1711 | uint32_t type; |
| 1712 | const void *data; |
| 1713 | size_t size; |
| 1714 | if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) { |
| 1715 | if (!track->meta->findData(kKeyAVCC, &type, &data, &size) |
| 1716 | || type != kTypeAVCC) { |
| 1717 | return ERROR_MALFORMED; |
| 1718 | } |
| 1719 | } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) |
| 1720 | || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { |
| 1721 | if (!track->meta->findData(kKeyESDS, &type, &data, &size) |
| 1722 | || type != kTypeESDS) { |
| 1723 | return ERROR_MALFORMED; |
| 1724 | } |
| 1725 | } |
| 1726 | |
Andreas Huber | 169c286 | 2011-08-17 13:03:51 -0700 | [diff] [blame] | 1727 | if (!track->sampleTable->isValid()) { |
| 1728 | // Make sure we have all the metadata we need. |
| 1729 | return ERROR_MALFORMED; |
| 1730 | } |
| 1731 | |
Andreas Huber | bd5d93f | 2010-02-23 10:12:02 -0800 | [diff] [blame] | 1732 | return OK; |
| 1733 | } |
| 1734 | |
Andreas Huber | ba7c7ee | 2010-01-29 14:46:59 -0800 | [diff] [blame] | 1735 | status_t MPEG4Extractor::updateAudioTrackInfoFromESDS_MPEG4Audio( |
| 1736 | const void *esds_data, size_t esds_size) { |
| 1737 | ESDS esds(esds_data, esds_size); |
Andreas Huber | 81f82c3 | 2010-04-12 16:05:57 -0700 | [diff] [blame] | 1738 | |
| 1739 | uint8_t objectTypeIndication; |
| 1740 | if (esds.getObjectTypeIndication(&objectTypeIndication) != OK) { |
| 1741 | return ERROR_MALFORMED; |
| 1742 | } |
| 1743 | |
| 1744 | if (objectTypeIndication == 0xe1) { |
| 1745 | // This isn't MPEG4 audio at all, it's QCELP 14k... |
| 1746 | mLastTrack->meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_QCELP); |
| 1747 | return OK; |
| 1748 | } |
| 1749 | |
James Dong | 633b4c8 | 2011-02-24 21:16:54 -0800 | [diff] [blame] | 1750 | if (objectTypeIndication == 0x6b) { |
| 1751 | // The media subtype is MP3 audio |
| 1752 | // Our software MP3 audio decoder may not be able to handle |
| 1753 | // packetized MP3 audio; for now, lets just return ERROR_UNSUPPORTED |
| 1754 | LOGE("MP3 track in MP4/3GPP file is not supported"); |
| 1755 | return ERROR_UNSUPPORTED; |
| 1756 | } |
| 1757 | |
Andreas Huber | ba7c7ee | 2010-01-29 14:46:59 -0800 | [diff] [blame] | 1758 | const uint8_t *csd; |
| 1759 | size_t csd_size; |
| 1760 | if (esds.getCodecSpecificInfo( |
| 1761 | (const void **)&csd, &csd_size) != OK) { |
| 1762 | return ERROR_MALFORMED; |
| 1763 | } |
| 1764 | |
| 1765 | #if 0 |
| 1766 | printf("ESD of size %d\n", csd_size); |
| 1767 | hexdump(csd, csd_size); |
| 1768 | #endif |
| 1769 | |
Andreas Huber | a65ccbf | 2010-08-18 09:58:30 -0700 | [diff] [blame] | 1770 | if (csd_size == 0) { |
| 1771 | // There's no further information, i.e. no codec specific data |
| 1772 | // Let's assume that the information provided in the mpeg4 headers |
| 1773 | // is accurate and hope for the best. |
| 1774 | |
| 1775 | return OK; |
| 1776 | } |
| 1777 | |
Andreas Huber | ba7c7ee | 2010-01-29 14:46:59 -0800 | [diff] [blame] | 1778 | if (csd_size < 2) { |
| 1779 | return ERROR_MALFORMED; |
| 1780 | } |
| 1781 | |
| 1782 | uint32_t objectType = csd[0] >> 3; |
| 1783 | |
| 1784 | if (objectType == 31) { |
| 1785 | return ERROR_UNSUPPORTED; |
| 1786 | } |
| 1787 | |
| 1788 | uint32_t freqIndex = (csd[0] & 7) << 1 | (csd[1] >> 7); |
| 1789 | int32_t sampleRate = 0; |
| 1790 | int32_t numChannels = 0; |
| 1791 | if (freqIndex == 15) { |
| 1792 | if (csd_size < 5) { |
| 1793 | return ERROR_MALFORMED; |
| 1794 | } |
| 1795 | |
| 1796 | sampleRate = (csd[1] & 0x7f) << 17 |
| 1797 | | csd[2] << 9 |
| 1798 | | csd[3] << 1 |
| 1799 | | (csd[4] >> 7); |
| 1800 | |
| 1801 | numChannels = (csd[4] >> 3) & 15; |
| 1802 | } else { |
Andreas Huber | 3c3ddfa | 2010-07-20 09:21:17 -0700 | [diff] [blame] | 1803 | static uint32_t kSamplingRate[] = { |
| 1804 | 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, |
| 1805 | 16000, 12000, 11025, 8000, 7350 |
| 1806 | }; |
Andreas Huber | ba7c7ee | 2010-01-29 14:46:59 -0800 | [diff] [blame] | 1807 | |
| 1808 | if (freqIndex == 13 || freqIndex == 14) { |
| 1809 | return ERROR_MALFORMED; |
| 1810 | } |
| 1811 | |
| 1812 | sampleRate = kSamplingRate[freqIndex]; |
| 1813 | numChannels = (csd[1] >> 3) & 15; |
| 1814 | } |
| 1815 | |
| 1816 | if (numChannels == 0) { |
| 1817 | return ERROR_UNSUPPORTED; |
| 1818 | } |
| 1819 | |
| 1820 | int32_t prevSampleRate; |
| 1821 | CHECK(mLastTrack->meta->findInt32(kKeySampleRate, &prevSampleRate)); |
| 1822 | |
| 1823 | if (prevSampleRate != sampleRate) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1824 | ALOGV("mpeg4 audio sample rate different from previous setting. " |
Andreas Huber | ba7c7ee | 2010-01-29 14:46:59 -0800 | [diff] [blame] | 1825 | "was: %d, now: %d", prevSampleRate, sampleRate); |
| 1826 | } |
| 1827 | |
| 1828 | mLastTrack->meta->setInt32(kKeySampleRate, sampleRate); |
| 1829 | |
| 1830 | int32_t prevChannelCount; |
| 1831 | CHECK(mLastTrack->meta->findInt32(kKeyChannelCount, &prevChannelCount)); |
| 1832 | |
| 1833 | if (prevChannelCount != numChannels) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1834 | ALOGV("mpeg4 audio channel count different from previous setting. " |
Andreas Huber | ba7c7ee | 2010-01-29 14:46:59 -0800 | [diff] [blame] | 1835 | "was: %d, now: %d", prevChannelCount, numChannels); |
| 1836 | } |
| 1837 | |
| 1838 | mLastTrack->meta->setInt32(kKeyChannelCount, numChannels); |
| 1839 | |
| 1840 | return OK; |
| 1841 | } |
| 1842 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1843 | //////////////////////////////////////////////////////////////////////////////// |
| 1844 | |
| 1845 | MPEG4Source::MPEG4Source( |
| 1846 | const sp<MetaData> &format, |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1847 | const sp<DataSource> &dataSource, |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 1848 | int32_t timeScale, |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1849 | const sp<SampleTable> &sampleTable) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1850 | : mFormat(format), |
| 1851 | mDataSource(dataSource), |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 1852 | mTimescale(timeScale), |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1853 | mSampleTable(sampleTable), |
| 1854 | mCurrentSampleIndex(0), |
| 1855 | mIsAVC(false), |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 1856 | mNALLengthSize(0), |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1857 | mStarted(false), |
| 1858 | mGroup(NULL), |
| 1859 | mBuffer(NULL), |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1860 | mWantsNALFragments(false), |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 1861 | mSrcBuffer(NULL) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1862 | const char *mime; |
| 1863 | bool success = mFormat->findCString(kKeyMIMEType, &mime); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1864 | CHECK(success); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1865 | |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1866 | mIsAVC = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC); |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 1867 | |
| 1868 | if (mIsAVC) { |
| 1869 | uint32_t type; |
| 1870 | const void *data; |
| 1871 | size_t size; |
| 1872 | CHECK(format->findData(kKeyAVCC, &type, &data, &size)); |
| 1873 | |
| 1874 | const uint8_t *ptr = (const uint8_t *)data; |
| 1875 | |
| 1876 | CHECK(size >= 7); |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 1877 | CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 1878 | |
| 1879 | // The number of bytes used to encode the length of a NAL unit. |
| 1880 | mNALLengthSize = 1 + (ptr[4] & 3); |
| 1881 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | MPEG4Source::~MPEG4Source() { |
| 1885 | if (mStarted) { |
| 1886 | stop(); |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | status_t MPEG4Source::start(MetaData *params) { |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 1891 | Mutex::Autolock autoLock(mLock); |
| 1892 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1893 | CHECK(!mStarted); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1894 | |
| 1895 | int32_t val; |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1896 | if (params && params->findInt32(kKeyWantsNALFragments, &val) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1897 | && val != 0) { |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1898 | mWantsNALFragments = true; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1899 | } else { |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1900 | mWantsNALFragments = false; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | mGroup = new MediaBufferGroup; |
| 1904 | |
Andreas Huber | 738c431 | 2009-11-23 14:03:32 -0800 | [diff] [blame] | 1905 | int32_t max_size; |
| 1906 | CHECK(mFormat->findInt32(kKeyMaxInputSize, &max_size)); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1907 | |
Andreas Huber | 738c431 | 2009-11-23 14:03:32 -0800 | [diff] [blame] | 1908 | mGroup->add_buffer(new MediaBuffer(max_size)); |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 1909 | |
| 1910 | mSrcBuffer = new uint8_t[max_size]; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1911 | |
| 1912 | mStarted = true; |
| 1913 | |
| 1914 | return OK; |
| 1915 | } |
| 1916 | |
| 1917 | status_t MPEG4Source::stop() { |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 1918 | Mutex::Autolock autoLock(mLock); |
| 1919 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1920 | CHECK(mStarted); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1921 | |
| 1922 | if (mBuffer != NULL) { |
| 1923 | mBuffer->release(); |
| 1924 | mBuffer = NULL; |
| 1925 | } |
| 1926 | |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 1927 | delete[] mSrcBuffer; |
| 1928 | mSrcBuffer = NULL; |
| 1929 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1930 | delete mGroup; |
| 1931 | mGroup = NULL; |
| 1932 | |
| 1933 | mStarted = false; |
| 1934 | mCurrentSampleIndex = 0; |
| 1935 | |
| 1936 | return OK; |
| 1937 | } |
| 1938 | |
| 1939 | sp<MetaData> MPEG4Source::getFormat() { |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 1940 | Mutex::Autolock autoLock(mLock); |
| 1941 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1942 | return mFormat; |
| 1943 | } |
| 1944 | |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 1945 | size_t MPEG4Source::parseNALSize(const uint8_t *data) const { |
| 1946 | switch (mNALLengthSize) { |
| 1947 | case 1: |
| 1948 | return *data; |
| 1949 | case 2: |
| 1950 | return U16_AT(data); |
| 1951 | case 3: |
| 1952 | return ((size_t)data[0] << 16) | U16_AT(&data[1]); |
| 1953 | case 4: |
| 1954 | return U32_AT(data); |
| 1955 | } |
| 1956 | |
| 1957 | // This cannot happen, mNALLengthSize springs to life by adding 1 to |
| 1958 | // a 2-bit integer. |
| 1959 | CHECK(!"Should not be here."); |
| 1960 | |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1964 | status_t MPEG4Source::read( |
| 1965 | MediaBuffer **out, const ReadOptions *options) { |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 1966 | Mutex::Autolock autoLock(mLock); |
| 1967 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 1968 | CHECK(mStarted); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1969 | |
| 1970 | *out = NULL; |
| 1971 | |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 1972 | int64_t targetSampleTimeUs = -1; |
| 1973 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1974 | int64_t seekTimeUs; |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 1975 | ReadOptions::SeekMode mode; |
| 1976 | if (options && options->getSeekTo(&seekTimeUs, &mode)) { |
| 1977 | uint32_t findFlags = 0; |
| 1978 | switch (mode) { |
| 1979 | case ReadOptions::SEEK_PREVIOUS_SYNC: |
| 1980 | findFlags = SampleTable::kFlagBefore; |
| 1981 | break; |
| 1982 | case ReadOptions::SEEK_NEXT_SYNC: |
| 1983 | findFlags = SampleTable::kFlagAfter; |
| 1984 | break; |
| 1985 | case ReadOptions::SEEK_CLOSEST_SYNC: |
| 1986 | case ReadOptions::SEEK_CLOSEST: |
| 1987 | findFlags = SampleTable::kFlagClosest; |
| 1988 | break; |
| 1989 | default: |
| 1990 | CHECK(!"Should not be here."); |
| 1991 | break; |
| 1992 | } |
| 1993 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1994 | uint32_t sampleIndex; |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 1995 | status_t err = mSampleTable->findSampleAtTime( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1996 | seekTimeUs * mTimescale / 1000000, |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 1997 | &sampleIndex, findFlags); |
| 1998 | |
| 1999 | if (mode == ReadOptions::SEEK_CLOSEST) { |
| 2000 | // We found the closest sample already, now we want the sync |
| 2001 | // sample preceding it (or the sample itself of course), even |
| 2002 | // if the subsequent sync sample is closer. |
| 2003 | findFlags = SampleTable::kFlagBefore; |
| 2004 | } |
| 2005 | |
| 2006 | uint32_t syncSampleIndex; |
| 2007 | if (err == OK) { |
| 2008 | err = mSampleTable->findSyncSampleNear( |
| 2009 | sampleIndex, &syncSampleIndex, findFlags); |
| 2010 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2011 | |
Andreas Huber | 788a07f | 2011-10-06 09:07:12 -0700 | [diff] [blame] | 2012 | uint32_t sampleTime; |
| 2013 | if (err == OK) { |
| 2014 | err = mSampleTable->getMetaDataForSample( |
| 2015 | sampleIndex, NULL, NULL, &sampleTime); |
| 2016 | } |
| 2017 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2018 | if (err != OK) { |
Andreas Huber | 5295c0c | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 2019 | if (err == ERROR_OUT_OF_RANGE) { |
| 2020 | // An attempt to seek past the end of the stream would |
| 2021 | // normally cause this ERROR_OUT_OF_RANGE error. Propagating |
| 2022 | // this all the way to the MediaPlayer would cause abnormal |
| 2023 | // termination. Legacy behaviour appears to be to behave as if |
| 2024 | // we had seeked to the end of stream, ending normally. |
| 2025 | err = ERROR_END_OF_STREAM; |
| 2026 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2027 | return err; |
| 2028 | } |
| 2029 | |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2030 | if (mode == ReadOptions::SEEK_CLOSEST) { |
| 2031 | targetSampleTimeUs = (sampleTime * 1000000ll) / mTimescale; |
| 2032 | } |
| 2033 | |
| 2034 | #if 0 |
| 2035 | uint32_t syncSampleTime; |
| 2036 | CHECK_EQ(OK, mSampleTable->getMetaDataForSample( |
| 2037 | syncSampleIndex, NULL, NULL, &syncSampleTime)); |
| 2038 | |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2039 | ALOGI("seek to time %lld us => sample at time %lld us, " |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2040 | "sync sample at time %lld us", |
| 2041 | seekTimeUs, |
| 2042 | sampleTime * 1000000ll / mTimescale, |
| 2043 | syncSampleTime * 1000000ll / mTimescale); |
| 2044 | #endif |
| 2045 | |
| 2046 | mCurrentSampleIndex = syncSampleIndex; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2047 | if (mBuffer != NULL) { |
| 2048 | mBuffer->release(); |
| 2049 | mBuffer = NULL; |
| 2050 | } |
| 2051 | |
| 2052 | // fall through |
| 2053 | } |
| 2054 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 2055 | off64_t offset; |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 2056 | size_t size; |
Andreas Huber | 4678a6d | 2011-04-15 11:52:29 -0700 | [diff] [blame] | 2057 | uint32_t cts; |
Andreas Huber | 8bf59e7 | 2010-08-06 14:13:10 -0700 | [diff] [blame] | 2058 | bool isSyncSample; |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2059 | bool newBuffer = false; |
| 2060 | if (mBuffer == NULL) { |
| 2061 | newBuffer = true; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2062 | |
Andreas Huber | c57b679 | 2010-01-19 10:39:21 -0800 | [diff] [blame] | 2063 | status_t err = |
| 2064 | mSampleTable->getMetaDataForSample( |
Andreas Huber | 4678a6d | 2011-04-15 11:52:29 -0700 | [diff] [blame] | 2065 | mCurrentSampleIndex, &offset, &size, &cts, &isSyncSample); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2066 | |
| 2067 | if (err != OK) { |
| 2068 | return err; |
| 2069 | } |
| 2070 | |
| 2071 | err = mGroup->acquire_buffer(&mBuffer); |
Andreas Huber | c57b679 | 2010-01-19 10:39:21 -0800 | [diff] [blame] | 2072 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2073 | if (err != OK) { |
Andreas Huber | ba529e4 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 2074 | CHECK(mBuffer == NULL); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2075 | return err; |
| 2076 | } |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 2077 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2078 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2079 | if (!mIsAVC || mWantsNALFragments) { |
| 2080 | if (newBuffer) { |
| 2081 | ssize_t num_bytes_read = |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 2082 | mDataSource->readAt(offset, (uint8_t *)mBuffer->data(), size); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2083 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2084 | if (num_bytes_read < (ssize_t)size) { |
| 2085 | mBuffer->release(); |
| 2086 | mBuffer = NULL; |
| 2087 | |
| 2088 | return ERROR_IO; |
| 2089 | } |
| 2090 | |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 2091 | CHECK(mBuffer != NULL); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2092 | mBuffer->set_range(0, size); |
| 2093 | mBuffer->meta_data()->clear(); |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 2094 | mBuffer->meta_data()->setInt64( |
Andreas Huber | 4678a6d | 2011-04-15 11:52:29 -0700 | [diff] [blame] | 2095 | kKeyTime, ((int64_t)cts * 1000000) / mTimescale); |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2096 | |
| 2097 | if (targetSampleTimeUs >= 0) { |
| 2098 | mBuffer->meta_data()->setInt64( |
| 2099 | kKeyTargetTime, targetSampleTimeUs); |
| 2100 | } |
| 2101 | |
Andreas Huber | 8bf59e7 | 2010-08-06 14:13:10 -0700 | [diff] [blame] | 2102 | if (isSyncSample) { |
| 2103 | mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); |
| 2104 | } |
| 2105 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2106 | ++mCurrentSampleIndex; |
| 2107 | } |
| 2108 | |
| 2109 | if (!mIsAVC) { |
| 2110 | *out = mBuffer; |
| 2111 | mBuffer = NULL; |
| 2112 | |
| 2113 | return OK; |
| 2114 | } |
| 2115 | |
| 2116 | // Each NAL unit is split up into its constituent fragments and |
| 2117 | // each one of them returned in its own buffer. |
| 2118 | |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 2119 | CHECK(mBuffer->range_length() >= mNALLengthSize); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2120 | |
| 2121 | const uint8_t *src = |
| 2122 | (const uint8_t *)mBuffer->data() + mBuffer->range_offset(); |
| 2123 | |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 2124 | size_t nal_size = parseNALSize(src); |
Andreas Huber | e8a0849 | 2010-01-14 14:13:15 -0800 | [diff] [blame] | 2125 | if (mBuffer->range_length() < mNALLengthSize + nal_size) { |
| 2126 | LOGE("incomplete NAL unit."); |
| 2127 | |
| 2128 | mBuffer->release(); |
| 2129 | mBuffer = NULL; |
| 2130 | |
| 2131 | return ERROR_MALFORMED; |
| 2132 | } |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2133 | |
| 2134 | MediaBuffer *clone = mBuffer->clone(); |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 2135 | CHECK(clone != NULL); |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 2136 | clone->set_range(mBuffer->range_offset() + mNALLengthSize, nal_size); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2137 | |
Andreas Huber | 7b3396a | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 2138 | CHECK(mBuffer != NULL); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2139 | mBuffer->set_range( |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 2140 | mBuffer->range_offset() + mNALLengthSize + nal_size, |
| 2141 | mBuffer->range_length() - mNALLengthSize - nal_size); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2142 | |
| 2143 | if (mBuffer->range_length() == 0) { |
| 2144 | mBuffer->release(); |
| 2145 | mBuffer = NULL; |
| 2146 | } |
| 2147 | |
| 2148 | *out = clone; |
| 2149 | |
| 2150 | return OK; |
| 2151 | } else { |
| 2152 | // Whole NAL units are returned but each fragment is prefixed by |
| 2153 | // the start code (0x00 00 00 01). |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 2154 | ssize_t num_bytes_read = 0; |
| 2155 | int32_t drm = 0; |
| 2156 | bool usesDRM = (mFormat->findInt32(kKeyIsDRM, &drm) && drm != 0); |
| 2157 | if (usesDRM) { |
| 2158 | num_bytes_read = |
| 2159 | mDataSource->readAt(offset, (uint8_t*)mBuffer->data(), size); |
| 2160 | } else { |
| 2161 | num_bytes_read = mDataSource->readAt(offset, mSrcBuffer, size); |
| 2162 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2163 | |
| 2164 | if (num_bytes_read < (ssize_t)size) { |
| 2165 | mBuffer->release(); |
| 2166 | mBuffer = NULL; |
| 2167 | |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2168 | return ERROR_IO; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 2171 | if (usesDRM) { |
| 2172 | CHECK(mBuffer != NULL); |
| 2173 | mBuffer->set_range(0, size); |
Andreas Huber | 6064017 | 2010-01-12 16:30:44 -0800 | [diff] [blame] | 2174 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 2175 | } else { |
| 2176 | uint8_t *dstData = (uint8_t *)mBuffer->data(); |
| 2177 | size_t srcOffset = 0; |
| 2178 | size_t dstOffset = 0; |
Andreas Huber | e8a0849 | 2010-01-14 14:13:15 -0800 | [diff] [blame] | 2179 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 2180 | while (srcOffset < size) { |
James Dong | 34fe252 | 2011-04-22 13:21:03 -0700 | [diff] [blame] | 2181 | bool isMalFormed = (srcOffset + mNALLengthSize > size); |
| 2182 | size_t nalLength = 0; |
| 2183 | if (!isMalFormed) { |
| 2184 | nalLength = parseNALSize(&mSrcBuffer[srcOffset]); |
| 2185 | srcOffset += mNALLengthSize; |
| 2186 | isMalFormed = srcOffset + nalLength > size; |
| 2187 | } |
Andreas Huber | e8a0849 | 2010-01-14 14:13:15 -0800 | [diff] [blame] | 2188 | |
James Dong | 34fe252 | 2011-04-22 13:21:03 -0700 | [diff] [blame] | 2189 | if (isMalFormed) { |
| 2190 | LOGE("Video is malformed"); |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 2191 | mBuffer->release(); |
| 2192 | mBuffer = NULL; |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 2193 | return ERROR_MALFORMED; |
| 2194 | } |
| 2195 | |
| 2196 | if (nalLength == 0) { |
| 2197 | continue; |
| 2198 | } |
| 2199 | |
| 2200 | CHECK(dstOffset + 4 <= mBuffer->size()); |
| 2201 | |
| 2202 | dstData[dstOffset++] = 0; |
| 2203 | dstData[dstOffset++] = 0; |
| 2204 | dstData[dstOffset++] = 0; |
| 2205 | dstData[dstOffset++] = 1; |
| 2206 | memcpy(&dstData[dstOffset], &mSrcBuffer[srcOffset], nalLength); |
| 2207 | srcOffset += nalLength; |
| 2208 | dstOffset += nalLength; |
Andreas Huber | e8a0849 | 2010-01-14 14:13:15 -0800 | [diff] [blame] | 2209 | } |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 2210 | CHECK_EQ(srcOffset, size); |
| 2211 | CHECK(mBuffer != NULL); |
| 2212 | mBuffer->set_range(0, dstOffset); |
Andreas Huber | dbc0344 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2215 | mBuffer->meta_data()->clear(); |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 2216 | mBuffer->meta_data()->setInt64( |
Andreas Huber | 4678a6d | 2011-04-15 11:52:29 -0700 | [diff] [blame] | 2217 | kKeyTime, ((int64_t)cts * 1000000) / mTimescale); |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2218 | |
| 2219 | if (targetSampleTimeUs >= 0) { |
| 2220 | mBuffer->meta_data()->setInt64( |
| 2221 | kKeyTargetTime, targetSampleTimeUs); |
| 2222 | } |
| 2223 | |
Andreas Huber | 8bf59e7 | 2010-08-06 14:13:10 -0700 | [diff] [blame] | 2224 | if (isSyncSample) { |
| 2225 | mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); |
| 2226 | } |
| 2227 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2228 | ++mCurrentSampleIndex; |
| 2229 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2230 | *out = mBuffer; |
| 2231 | mBuffer = NULL; |
| 2232 | |
| 2233 | return OK; |
| 2234 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Andreas Huber | bdba1f2 | 2011-03-03 13:48:41 -0800 | [diff] [blame] | 2237 | MPEG4Extractor::Track *MPEG4Extractor::findTrackByMimePrefix( |
| 2238 | const char *mimePrefix) { |
| 2239 | for (Track *track = mFirstTrack; track != NULL; track = track->next) { |
| 2240 | const char *mime; |
| 2241 | if (track->meta != NULL |
| 2242 | && track->meta->findCString(kKeyMIMEType, &mime) |
| 2243 | && !strncasecmp(mime, mimePrefix, strlen(mimePrefix))) { |
| 2244 | return track; |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2251 | static bool LegacySniffMPEG4( |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2252 | const sp<DataSource> &source, String8 *mimeType, float *confidence) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2253 | uint8_t header[8]; |
| 2254 | |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 2255 | ssize_t n = source->readAt(4, header, sizeof(header)); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2256 | if (n < (ssize_t)sizeof(header)) { |
| 2257 | return false; |
| 2258 | } |
| 2259 | |
| 2260 | if (!memcmp(header, "ftyp3gp", 7) || !memcmp(header, "ftypmp42", 8) |
James Dong | b9dbba9 | 2010-04-13 15:06:41 -0700 | [diff] [blame] | 2261 | || !memcmp(header, "ftyp3gr6", 8) || !memcmp(header, "ftyp3gs6", 8) |
| 2262 | || !memcmp(header, "ftyp3ge6", 8) || !memcmp(header, "ftyp3gg6", 8) |
Andreas Huber | 0dba737 | 2009-11-03 16:00:58 -0800 | [diff] [blame] | 2263 | || !memcmp(header, "ftypisom", 8) || !memcmp(header, "ftypM4V ", 8) |
Andreas Huber | ea5ea10 | 2010-02-25 15:59:24 -0800 | [diff] [blame] | 2264 | || !memcmp(header, "ftypM4A ", 8) || !memcmp(header, "ftypf4v ", 8) |
Andreas Huber | 983b449 | 2010-04-01 11:42:50 -0700 | [diff] [blame] | 2265 | || !memcmp(header, "ftypkddi", 8) || !memcmp(header, "ftypM4VP", 8)) { |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 2266 | *mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4; |
Andreas Huber | 5a1c352 | 2010-08-25 11:09:41 -0700 | [diff] [blame] | 2267 | *confidence = 0.4; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2268 | |
| 2269 | return true; |
| 2270 | } |
| 2271 | |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2275 | static bool isCompatibleBrand(uint32_t fourcc) { |
| 2276 | static const uint32_t kCompatibleBrands[] = { |
| 2277 | FOURCC('i', 's', 'o', 'm'), |
| 2278 | FOURCC('i', 's', 'o', '2'), |
| 2279 | FOURCC('a', 'v', 'c', '1'), |
| 2280 | FOURCC('3', 'g', 'p', '4'), |
| 2281 | FOURCC('m', 'p', '4', '1'), |
| 2282 | FOURCC('m', 'p', '4', '2'), |
James Dong | 4cb2c4d | 2011-04-07 17:57:04 -0700 | [diff] [blame] | 2283 | |
| 2284 | // Won't promise that the following file types can be played. |
| 2285 | // Just give these file types a chance. |
| 2286 | FOURCC('q', 't', ' ', ' '), // Apple's QuickTime |
| 2287 | FOURCC('M', 'S', 'N', 'V'), // Sony's PSP |
Andreas Huber | 58fb391 | 2011-05-17 10:12:34 -0700 | [diff] [blame] | 2288 | |
| 2289 | FOURCC('3', 'g', '2', 'a'), // 3GPP2 |
| 2290 | FOURCC('3', 'g', '2', 'b'), |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2291 | }; |
| 2292 | |
| 2293 | for (size_t i = 0; |
| 2294 | i < sizeof(kCompatibleBrands) / sizeof(kCompatibleBrands[0]); |
| 2295 | ++i) { |
| 2296 | if (kCompatibleBrands[i] == fourcc) { |
| 2297 | return true; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | return false; |
| 2302 | } |
| 2303 | |
| 2304 | // Attempt to actually parse the 'ftyp' atom and determine if a suitable |
| 2305 | // compatible brand is present. |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2306 | // Also try to identify where this file's metadata ends |
| 2307 | // (end of the 'moov' atom) and report it to the caller as part of |
| 2308 | // the metadata. |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2309 | static bool BetterSniffMPEG4( |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2310 | const sp<DataSource> &source, String8 *mimeType, float *confidence, |
| 2311 | sp<AMessage> *meta) { |
| 2312 | // We scan up to 128 bytes to identify this file as an MP4. |
| 2313 | static const off64_t kMaxScanOffset = 128ll; |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2314 | |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2315 | off64_t offset = 0ll; |
| 2316 | bool foundGoodFileType = false; |
| 2317 | off64_t moovAtomEndOffset = -1ll; |
| 2318 | bool done = false; |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2319 | |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2320 | while (!done && offset < kMaxScanOffset) { |
| 2321 | uint32_t hdr[2]; |
| 2322 | if (source->readAt(offset, hdr, 8) < 8) { |
| 2323 | return false; |
| 2324 | } |
| 2325 | |
| 2326 | uint64_t chunkSize = ntohl(hdr[0]); |
| 2327 | uint32_t chunkType = ntohl(hdr[1]); |
| 2328 | off64_t chunkDataOffset = offset + 8; |
| 2329 | |
| 2330 | if (chunkSize == 1) { |
| 2331 | if (source->readAt(offset + 8, &chunkSize, 8) < 8) { |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2332 | return false; |
| 2333 | } |
| 2334 | |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2335 | chunkSize = ntoh64(chunkSize); |
| 2336 | chunkDataOffset += 8; |
| 2337 | |
| 2338 | if (chunkSize < 16) { |
| 2339 | // The smallest valid chunk is 16 bytes long in this case. |
| 2340 | return false; |
| 2341 | } |
| 2342 | } else if (chunkSize < 8) { |
| 2343 | // The smallest valid chunk is 8 bytes long. |
| 2344 | return false; |
| 2345 | } |
| 2346 | |
| 2347 | off64_t chunkDataSize = offset + chunkSize - chunkDataOffset; |
| 2348 | |
| 2349 | switch (chunkType) { |
| 2350 | case FOURCC('f', 't', 'y', 'p'): |
| 2351 | { |
| 2352 | if (chunkDataSize < 8) { |
| 2353 | return false; |
| 2354 | } |
| 2355 | |
| 2356 | uint32_t numCompatibleBrands = (chunkDataSize - 8) / 4; |
| 2357 | for (size_t i = 0; i < numCompatibleBrands + 2; ++i) { |
| 2358 | if (i == 1) { |
| 2359 | // Skip this index, it refers to the minorVersion, |
| 2360 | // not a brand. |
| 2361 | continue; |
| 2362 | } |
| 2363 | |
| 2364 | uint32_t brand; |
| 2365 | if (source->readAt( |
| 2366 | chunkDataOffset + 4 * i, &brand, 4) < 4) { |
| 2367 | return false; |
| 2368 | } |
| 2369 | |
| 2370 | brand = ntohl(brand); |
| 2371 | |
| 2372 | if (isCompatibleBrand(brand)) { |
| 2373 | foundGoodFileType = true; |
| 2374 | break; |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | if (!foundGoodFileType) { |
| 2379 | return false; |
| 2380 | } |
| 2381 | |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2382 | break; |
| 2383 | } |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2384 | |
| 2385 | case FOURCC('m', 'o', 'o', 'v'): |
| 2386 | { |
| 2387 | moovAtomEndOffset = offset + chunkSize; |
| 2388 | |
| 2389 | done = true; |
| 2390 | break; |
| 2391 | } |
| 2392 | |
| 2393 | default: |
| 2394 | break; |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2395 | } |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2396 | |
| 2397 | offset += chunkSize; |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2400 | if (!foundGoodFileType) { |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2401 | return false; |
| 2402 | } |
| 2403 | |
| 2404 | *mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4; |
Andreas Huber | 5a1c352 | 2010-08-25 11:09:41 -0700 | [diff] [blame] | 2405 | *confidence = 0.4f; |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2406 | |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2407 | if (moovAtomEndOffset >= 0) { |
| 2408 | *meta = new AMessage; |
| 2409 | (*meta)->setInt64("meta-data-size", moovAtomEndOffset); |
| 2410 | |
Andreas Huber | 456caf3 | 2011-12-05 13:30:52 -0800 | [diff] [blame] | 2411 | ALOGV("found metadata size: %lld", moovAtomEndOffset); |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2414 | return true; |
| 2415 | } |
| 2416 | |
| 2417 | bool SniffMPEG4( |
Andreas Huber | 5a1c352 | 2010-08-25 11:09:41 -0700 | [diff] [blame] | 2418 | const sp<DataSource> &source, String8 *mimeType, float *confidence, |
Andreas Huber | a0f2bf5 | 2011-12-05 11:34:43 -0800 | [diff] [blame] | 2419 | sp<AMessage> *meta) { |
| 2420 | if (BetterSniffMPEG4(source, mimeType, confidence, meta)) { |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2421 | return true; |
| 2422 | } |
| 2423 | |
| 2424 | if (LegacySniffMPEG4(source, mimeType, confidence)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame^] | 2425 | ALOGW("Identified supported mpeg4 through LegacySniffMPEG4."); |
Andreas Huber | 1b52bd2 | 2010-05-27 09:59:54 -0700 | [diff] [blame] | 2426 | return true; |
| 2427 | } |
| 2428 | |
| 2429 | return false; |
| 2430 | } |
| 2431 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2432 | } // namespace android |
| 2433 | |