MPEG4Extractor.cpp revision dfb8eee5bd1359d8042c1fd2f5143b416c09dfdd
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 20#include "include/MPEG4Extractor.h" 21#include "include/SampleTable.h" 22 23#include <arpa/inet.h> 24 25#include <ctype.h> 26#include <stdint.h> 27#include <stdlib.h> 28#include <string.h> 29 30#include <media/stagefright/DataSource.h> 31#include "include/ESDS.h" 32#include <media/stagefright/MediaBuffer.h> 33#include <media/stagefright/MediaBufferGroup.h> 34#include <media/stagefright/MediaDebug.h> 35#include <media/stagefright/MediaDefs.h> 36#include <media/stagefright/MediaSource.h> 37#include <media/stagefright/MetaData.h> 38#include <media/stagefright/Utils.h> 39#include <utils/String8.h> 40 41namespace android { 42 43class MPEG4Source : public MediaSource { 44public: 45 // Caller retains ownership of both "dataSource" and "sampleTable". 46 MPEG4Source(const sp<MetaData> &format, 47 const sp<DataSource> &dataSource, 48 int32_t timeScale, 49 const sp<SampleTable> &sampleTable); 50 51 virtual status_t start(MetaData *params = NULL); 52 virtual status_t stop(); 53 54 virtual sp<MetaData> getFormat(); 55 56 virtual status_t read( 57 MediaBuffer **buffer, const ReadOptions *options = NULL); 58 59protected: 60 virtual ~MPEG4Source(); 61 62private: 63 Mutex mLock; 64 65 sp<MetaData> mFormat; 66 sp<DataSource> mDataSource; 67 int32_t mTimescale; 68 sp<SampleTable> mSampleTable; 69 uint32_t mCurrentSampleIndex; 70 71 bool mIsAVC; 72 size_t mNALLengthSize; 73 74 bool mStarted; 75 76 MediaBufferGroup *mGroup; 77 78 MediaBuffer *mBuffer; 79 80 bool mWantsNALFragments; 81 82 uint8_t *mSrcBuffer; 83 84 size_t parseNALSize(const uint8_t *data) const; 85 86 MPEG4Source(const MPEG4Source &); 87 MPEG4Source &operator=(const MPEG4Source &); 88}; 89 90// This custom data source wraps an existing one and satisfies requests 91// falling entirely within a cached range from the cache while forwarding 92// all remaining requests to the wrapped datasource. 93// This is used to cache the full sampletable metadata for a single track, 94// possibly wrapping multiple times to cover all tracks, i.e. 95// Each MPEG4DataSource caches the sampletable metadata for a single track. 96 97struct MPEG4DataSource : public DataSource { 98 MPEG4DataSource(const sp<DataSource> &source); 99 100 virtual status_t initCheck() const; 101 virtual ssize_t readAt(off_t offset, void *data, size_t size); 102 virtual status_t getSize(off_t *size); 103 virtual uint32_t flags(); 104 105 status_t setCachedRange(off_t offset, size_t size); 106 107protected: 108 virtual ~MPEG4DataSource(); 109 110private: 111 Mutex mLock; 112 113 sp<DataSource> mSource; 114 off_t mCachedOffset; 115 size_t mCachedSize; 116 uint8_t *mCache; 117 118 void clearCache(); 119 120 MPEG4DataSource(const MPEG4DataSource &); 121 MPEG4DataSource &operator=(const MPEG4DataSource &); 122}; 123 124MPEG4DataSource::MPEG4DataSource(const sp<DataSource> &source) 125 : mSource(source), 126 mCachedOffset(0), 127 mCachedSize(0), 128 mCache(NULL) { 129} 130 131MPEG4DataSource::~MPEG4DataSource() { 132 clearCache(); 133} 134 135void MPEG4DataSource::clearCache() { 136 if (mCache) { 137 free(mCache); 138 mCache = NULL; 139 } 140 141 mCachedOffset = 0; 142 mCachedSize = 0; 143} 144 145status_t MPEG4DataSource::initCheck() const { 146 return mSource->initCheck(); 147} 148 149ssize_t MPEG4DataSource::readAt(off_t offset, void *data, size_t size) { 150 Mutex::Autolock autoLock(mLock); 151 152 if (offset >= mCachedOffset 153 && offset + size <= mCachedOffset + mCachedSize) { 154 memcpy(data, &mCache[offset - mCachedOffset], size); 155 return size; 156 } 157 158 return mSource->readAt(offset, data, size); 159} 160 161status_t MPEG4DataSource::getSize(off_t *size) { 162 return mSource->getSize(size); 163} 164 165uint32_t MPEG4DataSource::flags() { 166 return mSource->flags(); 167} 168 169status_t MPEG4DataSource::setCachedRange(off_t offset, size_t size) { 170 Mutex::Autolock autoLock(mLock); 171 172 clearCache(); 173 174 mCache = (uint8_t *)malloc(size); 175 176 if (mCache == NULL) { 177 return -ENOMEM; 178 } 179 180 mCachedOffset = offset; 181 mCachedSize = size; 182 183 ssize_t err = mSource->readAt(mCachedOffset, mCache, mCachedSize); 184 185 if (err < (ssize_t)size) { 186 clearCache(); 187 188 return ERROR_IO; 189 } 190 191 return OK; 192} 193 194//////////////////////////////////////////////////////////////////////////////// 195 196static void hexdump(const void *_data, size_t size) { 197 const uint8_t *data = (const uint8_t *)_data; 198 size_t offset = 0; 199 while (offset < size) { 200 printf("0x%04x ", offset); 201 202 size_t n = size - offset; 203 if (n > 16) { 204 n = 16; 205 } 206 207 for (size_t i = 0; i < 16; ++i) { 208 if (i == 8) { 209 printf(" "); 210 } 211 212 if (offset + i < size) { 213 printf("%02x ", data[offset + i]); 214 } else { 215 printf(" "); 216 } 217 } 218 219 printf(" "); 220 221 for (size_t i = 0; i < n; ++i) { 222 if (isprint(data[offset + i])) { 223 printf("%c", data[offset + i]); 224 } else { 225 printf("."); 226 } 227 } 228 229 printf("\n"); 230 231 offset += 16; 232 } 233} 234 235static const char *FourCC2MIME(uint32_t fourcc) { 236 switch (fourcc) { 237 case FOURCC('m', 'p', '4', 'a'): 238 return MEDIA_MIMETYPE_AUDIO_AAC; 239 240 case FOURCC('s', 'a', 'm', 'r'): 241 return MEDIA_MIMETYPE_AUDIO_AMR_NB; 242 243 case FOURCC('s', 'a', 'w', 'b'): 244 return MEDIA_MIMETYPE_AUDIO_AMR_WB; 245 246 case FOURCC('m', 'p', '4', 'v'): 247 return MEDIA_MIMETYPE_VIDEO_MPEG4; 248 249 case FOURCC('s', '2', '6', '3'): 250 return MEDIA_MIMETYPE_VIDEO_H263; 251 252 case FOURCC('a', 'v', 'c', '1'): 253 return MEDIA_MIMETYPE_VIDEO_AVC; 254 255 default: 256 CHECK(!"should not be here."); 257 return NULL; 258 } 259} 260 261MPEG4Extractor::MPEG4Extractor(const sp<DataSource> &source) 262 : mDataSource(source), 263 mHaveMetadata(false), 264 mHasVideo(false), 265 mFirstTrack(NULL), 266 mLastTrack(NULL), 267 mFileMetaData(new MetaData) { 268} 269 270MPEG4Extractor::~MPEG4Extractor() { 271 Track *track = mFirstTrack; 272 while (track) { 273 Track *next = track->next; 274 275 delete track; 276 track = next; 277 } 278 mFirstTrack = mLastTrack = NULL; 279} 280 281sp<MetaData> MPEG4Extractor::getMetaData() { 282 status_t err; 283 if ((err = readMetaData()) != OK) { 284 return new MetaData; 285 } 286 287 return mFileMetaData; 288} 289 290size_t MPEG4Extractor::countTracks() { 291 status_t err; 292 if ((err = readMetaData()) != OK) { 293 return 0; 294 } 295 296 size_t n = 0; 297 Track *track = mFirstTrack; 298 while (track) { 299 ++n; 300 track = track->next; 301 } 302 303 return n; 304} 305 306sp<MetaData> MPEG4Extractor::getTrackMetaData( 307 size_t index, uint32_t flags) { 308 status_t err; 309 if ((err = readMetaData()) != OK) { 310 return NULL; 311 } 312 313 Track *track = mFirstTrack; 314 while (index > 0) { 315 if (track == NULL) { 316 return NULL; 317 } 318 319 track = track->next; 320 --index; 321 } 322 323 if (track == NULL) { 324 return NULL; 325 } 326 327 if ((flags & kIncludeExtensiveMetaData) 328 && !track->includes_expensive_metadata) { 329 track->includes_expensive_metadata = true; 330 331 const char *mime; 332 CHECK(track->meta->findCString(kKeyMIMEType, &mime)); 333 if (!strncasecmp("video/", mime, 6)) { 334 uint32_t sampleIndex; 335 uint32_t sampleTime; 336 if (track->sampleTable->findThumbnailSample(&sampleIndex) == OK 337 && track->sampleTable->getMetaDataForSample( 338 sampleIndex, NULL /* offset */, NULL /* size */, 339 &sampleTime) == OK) { 340 track->meta->setInt64( 341 kKeyThumbnailTime, 342 ((int64_t)sampleTime * 1000000) / track->timescale); 343 } 344 } 345 } 346 347 return track->meta; 348} 349 350status_t MPEG4Extractor::readMetaData() { 351 if (mHaveMetadata) { 352 return OK; 353 } 354 355 off_t offset = 0; 356 status_t err; 357 while ((err = parseChunk(&offset, 0)) == OK) { 358 } 359 360 if (mHaveMetadata) { 361 if (mHasVideo) { 362 mFileMetaData->setCString(kKeyMIMEType, "video/mp4"); 363 } else { 364 mFileMetaData->setCString(kKeyMIMEType, "audio/mp4"); 365 } 366 367 return OK; 368 } 369 370 return err; 371} 372 373static void MakeFourCCString(uint32_t x, char *s) { 374 s[0] = x >> 24; 375 s[1] = (x >> 16) & 0xff; 376 s[2] = (x >> 8) & 0xff; 377 s[3] = x & 0xff; 378 s[4] = '\0'; 379} 380 381struct PathAdder { 382 PathAdder(Vector<uint32_t> *path, uint32_t chunkType) 383 : mPath(path) { 384 mPath->push(chunkType); 385 } 386 387 ~PathAdder() { 388 mPath->pop(); 389 } 390 391private: 392 Vector<uint32_t> *mPath; 393 394 PathAdder(const PathAdder &); 395 PathAdder &operator=(const PathAdder &); 396}; 397 398static bool underMetaDataPath(const Vector<uint32_t> &path) { 399 return path.size() >= 5 400 && path[0] == FOURCC('m', 'o', 'o', 'v') 401 && path[1] == FOURCC('u', 'd', 't', 'a') 402 && path[2] == FOURCC('m', 'e', 't', 'a') 403 && path[3] == FOURCC('i', 'l', 's', 't'); 404} 405 406// Given a time in seconds since Jan 1 1904, produce a human-readable string. 407static void convertTimeToDate(int64_t time_1904, String8 *s) { 408 time_t time_1970 = time_1904 - (((66 * 365 + 17) * 24) * 3600); 409 410 char tmp[32]; 411 strftime(tmp, sizeof(tmp), "%Y%m%dT%H%M%S.000Z", gmtime(&time_1970)); 412 413 s->setTo(tmp); 414} 415 416status_t MPEG4Extractor::parseChunk(off_t *offset, int depth) { 417 uint32_t hdr[2]; 418 if (mDataSource->readAt(*offset, hdr, 8) < 8) { 419 return ERROR_IO; 420 } 421 uint64_t chunk_size = ntohl(hdr[0]); 422 uint32_t chunk_type = ntohl(hdr[1]); 423 off_t data_offset = *offset + 8; 424 425 if (chunk_size == 1) { 426 if (mDataSource->readAt(*offset + 8, &chunk_size, 8) < 8) { 427 return ERROR_IO; 428 } 429 chunk_size = ntoh64(chunk_size); 430 data_offset += 8; 431 } 432 433 char chunk[5]; 434 MakeFourCCString(chunk_type, chunk); 435 436#if 0 437 static const char kWhitespace[] = " "; 438 const char *indent = &kWhitespace[sizeof(kWhitespace) - 1 - 2 * depth]; 439 printf("%sfound chunk '%s' of size %lld\n", indent, chunk, chunk_size); 440 441 char buffer[256]; 442 size_t n = chunk_size; 443 if (n > sizeof(buffer)) { 444 n = sizeof(buffer); 445 } 446 if (mDataSource->readAt(*offset, buffer, n) 447 < (ssize_t)n) { 448 return ERROR_IO; 449 } 450 451 hexdump(buffer, n); 452#endif 453 454 PathAdder autoAdder(&mPath, chunk_type); 455 456 off_t chunk_data_size = *offset + chunk_size - data_offset; 457 458 if (chunk_type != FOURCC('c', 'p', 'r', 't') 459 && mPath.size() == 5 && underMetaDataPath(mPath)) { 460 off_t stop_offset = *offset + chunk_size; 461 *offset = data_offset; 462 while (*offset < stop_offset) { 463 status_t err = parseChunk(offset, depth + 1); 464 if (err != OK) { 465 return err; 466 } 467 } 468 469 if (*offset != stop_offset) { 470 return ERROR_MALFORMED; 471 } 472 473 return OK; 474 } 475 476 switch(chunk_type) { 477 case FOURCC('m', 'o', 'o', 'v'): 478 case FOURCC('t', 'r', 'a', 'k'): 479 case FOURCC('m', 'd', 'i', 'a'): 480 case FOURCC('m', 'i', 'n', 'f'): 481 case FOURCC('d', 'i', 'n', 'f'): 482 case FOURCC('s', 't', 'b', 'l'): 483 case FOURCC('m', 'v', 'e', 'x'): 484 case FOURCC('m', 'o', 'o', 'f'): 485 case FOURCC('t', 'r', 'a', 'f'): 486 case FOURCC('m', 'f', 'r', 'a'): 487 case FOURCC('s', 'k', 'i' ,'p'): 488 case FOURCC('u', 'd', 't', 'a'): 489 case FOURCC('i', 'l', 's', 't'): 490 { 491 if (chunk_type == FOURCC('s', 't', 'b', 'l')) { 492 LOGV("sampleTable chunk is %d bytes long.", (size_t)chunk_size); 493 494 if (mDataSource->flags() & DataSource::kWantsPrefetching) { 495 sp<MPEG4DataSource> cachedSource = 496 new MPEG4DataSource(mDataSource); 497 498 if (cachedSource->setCachedRange(*offset, chunk_size) == OK) { 499 mDataSource = cachedSource; 500 } 501 } 502 } 503 504 if (chunk_type == FOURCC('t', 'r', 'a', 'k')) { 505 Track *track = new Track; 506 track->next = NULL; 507 if (mLastTrack) { 508 mLastTrack->next = track; 509 } else { 510 mFirstTrack = track; 511 } 512 mLastTrack = track; 513 514 track->meta = new MetaData; 515 track->includes_expensive_metadata = false; 516 track->timescale = 0; 517 track->sampleTable = new SampleTable(mDataSource); 518 track->meta->setCString(kKeyMIMEType, "application/octet-stream"); 519 } 520 521 off_t stop_offset = *offset + chunk_size; 522 *offset = data_offset; 523 while (*offset < stop_offset) { 524 status_t err = parseChunk(offset, depth + 1); 525 if (err != OK) { 526 return err; 527 } 528 } 529 530 if (*offset != stop_offset) { 531 return ERROR_MALFORMED; 532 } 533 534 if (chunk_type == FOURCC('t', 'r', 'a', 'k')) { 535 status_t err = verifyTrack(mLastTrack); 536 537 if (err != OK) { 538 return err; 539 } 540 } else if (chunk_type == FOURCC('m', 'o', 'o', 'v')) { 541 mHaveMetadata = true; 542 543 return UNKNOWN_ERROR; // Return a dummy error. 544 } 545 break; 546 } 547 548 case FOURCC('t', 'k', 'h', 'd'): 549 { 550 if (chunk_data_size < 4) { 551 return ERROR_MALFORMED; 552 } 553 554 uint8_t version; 555 if (mDataSource->readAt(data_offset, &version, 1) < 1) { 556 return ERROR_IO; 557 } 558 559 uint64_t ctime, mtime, duration; 560 int32_t id; 561 uint32_t width, height; 562 563 if (version == 1) { 564 if (chunk_data_size != 36 + 60) { 565 return ERROR_MALFORMED; 566 } 567 568 uint8_t buffer[36 + 60]; 569 if (mDataSource->readAt( 570 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 571 return ERROR_IO; 572 } 573 574 ctime = U64_AT(&buffer[4]); 575 mtime = U64_AT(&buffer[12]); 576 id = U32_AT(&buffer[20]); 577 duration = U64_AT(&buffer[28]); 578 width = U32_AT(&buffer[88]); 579 height = U32_AT(&buffer[92]); 580 } else if (version == 0) { 581 if (chunk_data_size != 24 + 60) { 582 return ERROR_MALFORMED; 583 } 584 585 uint8_t buffer[24 + 60]; 586 if (mDataSource->readAt( 587 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 588 return ERROR_IO; 589 } 590 ctime = U32_AT(&buffer[4]); 591 mtime = U32_AT(&buffer[8]); 592 id = U32_AT(&buffer[12]); 593 duration = U32_AT(&buffer[20]); 594 width = U32_AT(&buffer[76]); 595 height = U32_AT(&buffer[80]); 596 } 597 598 *offset += chunk_size; 599 break; 600 } 601 602 case FOURCC('m', 'd', 'h', 'd'): 603 { 604 if (chunk_data_size < 4) { 605 return ERROR_MALFORMED; 606 } 607 608 uint8_t version; 609 if (mDataSource->readAt( 610 data_offset, &version, sizeof(version)) 611 < (ssize_t)sizeof(version)) { 612 return ERROR_IO; 613 } 614 615 off_t timescale_offset; 616 617 if (version == 1) { 618 timescale_offset = data_offset + 4 + 16; 619 } else if (version == 0) { 620 timescale_offset = data_offset + 4 + 8; 621 } else { 622 return ERROR_IO; 623 } 624 625 uint32_t timescale; 626 if (mDataSource->readAt( 627 timescale_offset, ×cale, sizeof(timescale)) 628 < (ssize_t)sizeof(timescale)) { 629 return ERROR_IO; 630 } 631 632 mLastTrack->timescale = ntohl(timescale); 633 634 int64_t duration; 635 if (version == 1) { 636 if (mDataSource->readAt( 637 timescale_offset + 4, &duration, sizeof(duration)) 638 < (ssize_t)sizeof(duration)) { 639 return ERROR_IO; 640 } 641 duration = ntoh64(duration); 642 } else { 643 int32_t duration32; 644 if (mDataSource->readAt( 645 timescale_offset + 4, &duration32, sizeof(duration32)) 646 < (ssize_t)sizeof(duration32)) { 647 return ERROR_IO; 648 } 649 duration = ntohl(duration32); 650 } 651 mLastTrack->meta->setInt64( 652 kKeyDuration, (duration * 1000000) / mLastTrack->timescale); 653 654 *offset += chunk_size; 655 break; 656 } 657 658 case FOURCC('h', 'd', 'l', 'r'): 659 { 660 if (chunk_data_size < 25) { 661 return ERROR_MALFORMED; 662 } 663 664 uint8_t buffer[24]; 665 if (mDataSource->readAt(data_offset, buffer, 24) < 24) { 666 return ERROR_IO; 667 } 668 669 if (U32_AT(buffer) != 0) { 670 // Should be version 0, flags 0. 671 return ERROR_MALFORMED; 672 } 673 674 if (U32_AT(&buffer[4]) != 0) { 675 // pre_defined should be 0. 676 return ERROR_MALFORMED; 677 } 678 679 mHandlerType = U32_AT(&buffer[8]); 680 *offset += chunk_size; 681 break; 682 } 683 684 case FOURCC('s', 't', 's', 'd'): 685 { 686 if (chunk_data_size < 8) { 687 return ERROR_MALFORMED; 688 } 689 690 uint8_t buffer[8]; 691 if (chunk_data_size < (off_t)sizeof(buffer)) { 692 return ERROR_MALFORMED; 693 } 694 695 if (mDataSource->readAt( 696 data_offset, buffer, 8) < 8) { 697 return ERROR_IO; 698 } 699 700 if (U32_AT(buffer) != 0) { 701 // Should be version 0, flags 0. 702 return ERROR_MALFORMED; 703 } 704 705 uint32_t entry_count = U32_AT(&buffer[4]); 706 707 if (entry_count > 1) { 708 // For now we only support a single type of media per track. 709 return ERROR_UNSUPPORTED; 710 } 711 712 off_t stop_offset = *offset + chunk_size; 713 *offset = data_offset + 8; 714 for (uint32_t i = 0; i < entry_count; ++i) { 715 status_t err = parseChunk(offset, depth + 1); 716 if (err != OK) { 717 return err; 718 } 719 } 720 721 if (*offset != stop_offset) { 722 return ERROR_MALFORMED; 723 } 724 break; 725 } 726 727 case FOURCC('m', 'p', '4', 'a'): 728 case FOURCC('s', 'a', 'm', 'r'): 729 case FOURCC('s', 'a', 'w', 'b'): 730 { 731 if (mHandlerType != FOURCC('s', 'o', 'u', 'n')) { 732 return ERROR_MALFORMED; 733 } 734 735 uint8_t buffer[8 + 20]; 736 if (chunk_data_size < (ssize_t)sizeof(buffer)) { 737 // Basic AudioSampleEntry size. 738 return ERROR_MALFORMED; 739 } 740 741 if (mDataSource->readAt( 742 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 743 return ERROR_IO; 744 } 745 746 uint16_t data_ref_index = U16_AT(&buffer[6]); 747 uint16_t num_channels = U16_AT(&buffer[16]); 748 749 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, 750 FourCC2MIME(chunk_type)) 751 || !strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, 752 FourCC2MIME(chunk_type))) { 753 // AMR audio is always mono. 754 num_channels = 1; 755 } 756 757 uint16_t sample_size = U16_AT(&buffer[18]); 758 uint32_t sample_rate = U32_AT(&buffer[24]) >> 16; 759 760 // printf("*** coding='%s' %d channels, size %d, rate %d\n", 761 // chunk, num_channels, sample_size, sample_rate); 762 763 mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); 764 mLastTrack->meta->setInt32(kKeyChannelCount, num_channels); 765 mLastTrack->meta->setInt32(kKeySampleRate, sample_rate); 766 767 off_t stop_offset = *offset + chunk_size; 768 *offset = data_offset + sizeof(buffer); 769 while (*offset < stop_offset) { 770 status_t err = parseChunk(offset, depth + 1); 771 if (err != OK) { 772 return err; 773 } 774 } 775 776 if (*offset != stop_offset) { 777 return ERROR_MALFORMED; 778 } 779 break; 780 } 781 782 case FOURCC('m', 'p', '4', 'v'): 783 case FOURCC('s', '2', '6', '3'): 784 case FOURCC('a', 'v', 'c', '1'): 785 { 786 mHasVideo = true; 787 788 if (mHandlerType != FOURCC('v', 'i', 'd', 'e')) { 789 return ERROR_MALFORMED; 790 } 791 792 uint8_t buffer[78]; 793 if (chunk_data_size < (ssize_t)sizeof(buffer)) { 794 // Basic VideoSampleEntry size. 795 return ERROR_MALFORMED; 796 } 797 798 if (mDataSource->readAt( 799 data_offset, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer)) { 800 return ERROR_IO; 801 } 802 803 uint16_t data_ref_index = U16_AT(&buffer[6]); 804 uint16_t width = U16_AT(&buffer[6 + 18]); 805 uint16_t height = U16_AT(&buffer[6 + 20]); 806 807 // printf("*** coding='%s' width=%d height=%d\n", 808 // chunk, width, height); 809 810 mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type)); 811 mLastTrack->meta->setInt32(kKeyWidth, width); 812 mLastTrack->meta->setInt32(kKeyHeight, height); 813 814 off_t stop_offset = *offset + chunk_size; 815 *offset = data_offset + sizeof(buffer); 816 while (*offset < stop_offset) { 817 status_t err = parseChunk(offset, depth + 1); 818 if (err != OK) { 819 return err; 820 } 821 } 822 823 if (*offset != stop_offset) { 824 return ERROR_MALFORMED; 825 } 826 break; 827 } 828 829 case FOURCC('s', 't', 'c', 'o'): 830 case FOURCC('c', 'o', '6', '4'): 831 { 832 status_t err = 833 mLastTrack->sampleTable->setChunkOffsetParams( 834 chunk_type, data_offset, chunk_data_size); 835 836 if (err != OK) { 837 return err; 838 } 839 840 *offset += chunk_size; 841 break; 842 } 843 844 case FOURCC('s', 't', 's', 'c'): 845 { 846 status_t err = 847 mLastTrack->sampleTable->setSampleToChunkParams( 848 data_offset, chunk_data_size); 849 850 if (err != OK) { 851 return err; 852 } 853 854 *offset += chunk_size; 855 break; 856 } 857 858 case FOURCC('s', 't', 's', 'z'): 859 case FOURCC('s', 't', 'z', '2'): 860 { 861 status_t err = 862 mLastTrack->sampleTable->setSampleSizeParams( 863 chunk_type, data_offset, chunk_data_size); 864 865 if (err != OK) { 866 return err; 867 } 868 869 size_t max_size; 870 CHECK_EQ(mLastTrack->sampleTable->getMaxSampleSize(&max_size), OK); 871 872 // Assume that a given buffer only contains at most 10 fragments, 873 // each fragment originally prefixed with a 2 byte length will 874 // have a 4 byte header (0x00 0x00 0x00 0x01) after conversion, 875 // and thus will grow by 2 bytes per fragment. 876 mLastTrack->meta->setInt32(kKeyMaxInputSize, max_size + 10 * 2); 877 878 *offset += chunk_size; 879 break; 880 } 881 882 case FOURCC('s', 't', 't', 's'): 883 { 884 status_t err = 885 mLastTrack->sampleTable->setTimeToSampleParams( 886 data_offset, chunk_data_size); 887 888 if (err != OK) { 889 return err; 890 } 891 892 *offset += chunk_size; 893 break; 894 } 895 896 case FOURCC('s', 't', 's', 's'): 897 { 898 status_t err = 899 mLastTrack->sampleTable->setSyncSampleParams( 900 data_offset, chunk_data_size); 901 902 if (err != OK) { 903 return err; 904 } 905 906 *offset += chunk_size; 907 break; 908 } 909 910 case FOURCC('e', 's', 'd', 's'): 911 { 912 if (chunk_data_size < 4) { 913 return ERROR_MALFORMED; 914 } 915 916 uint8_t buffer[256]; 917 if (chunk_data_size > (off_t)sizeof(buffer)) { 918 return ERROR_BUFFER_TOO_SMALL; 919 } 920 921 if (mDataSource->readAt( 922 data_offset, buffer, chunk_data_size) < chunk_data_size) { 923 return ERROR_IO; 924 } 925 926 if (U32_AT(buffer) != 0) { 927 // Should be version 0, flags 0. 928 return ERROR_MALFORMED; 929 } 930 931 mLastTrack->meta->setData( 932 kKeyESDS, kTypeESDS, &buffer[4], chunk_data_size - 4); 933 934 if (mPath.size() >= 2 935 && mPath[mPath.size() - 2] == FOURCC('m', 'p', '4', 'a')) { 936 // Information from the ESDS must be relied on for proper 937 // setup of sample rate and channel count for MPEG4 Audio. 938 // The generic header appears to only contain generic 939 // information... 940 941 status_t err = updateAudioTrackInfoFromESDS_MPEG4Audio( 942 &buffer[4], chunk_data_size - 4); 943 944 if (err != OK) { 945 return err; 946 } 947 } 948 949 *offset += chunk_size; 950 break; 951 } 952 953 case FOURCC('a', 'v', 'c', 'C'): 954 { 955 char buffer[256]; 956 if (chunk_data_size > (off_t)sizeof(buffer)) { 957 return ERROR_BUFFER_TOO_SMALL; 958 } 959 960 if (mDataSource->readAt( 961 data_offset, buffer, chunk_data_size) < chunk_data_size) { 962 return ERROR_IO; 963 } 964 965 mLastTrack->meta->setData( 966 kKeyAVCC, kTypeAVCC, buffer, chunk_data_size); 967 968 *offset += chunk_size; 969 break; 970 } 971 972 case FOURCC('m', 'e', 't', 'a'): 973 { 974 uint8_t buffer[4]; 975 if (chunk_data_size < (off_t)sizeof(buffer)) { 976 return ERROR_MALFORMED; 977 } 978 979 if (mDataSource->readAt( 980 data_offset, buffer, 4) < 4) { 981 return ERROR_IO; 982 } 983 984 if (U32_AT(buffer) != 0) { 985 // Should be version 0, flags 0. 986 987 // If it's not, let's assume this is one of those 988 // apparently malformed chunks that don't have flags 989 // and completely different semantics than what's 990 // in the MPEG4 specs and skip it. 991 *offset += chunk_size; 992 return OK; 993 } 994 995 off_t stop_offset = *offset + chunk_size; 996 *offset = data_offset + sizeof(buffer); 997 while (*offset < stop_offset) { 998 status_t err = parseChunk(offset, depth + 1); 999 if (err != OK) { 1000 return err; 1001 } 1002 } 1003 1004 if (*offset != stop_offset) { 1005 return ERROR_MALFORMED; 1006 } 1007 break; 1008 } 1009 1010 case FOURCC('d', 'a', 't', 'a'): 1011 { 1012 if (mPath.size() == 6 && underMetaDataPath(mPath)) { 1013 status_t err = parseMetaData(data_offset, chunk_data_size); 1014 1015 if (err != OK) { 1016 return err; 1017 } 1018 } 1019 1020 *offset += chunk_size; 1021 break; 1022 } 1023 1024 case FOURCC('m', 'v', 'h', 'd'): 1025 { 1026 if (chunk_data_size < 12) { 1027 return ERROR_MALFORMED; 1028 } 1029 1030 uint8_t header[12]; 1031 if (mDataSource->readAt( 1032 data_offset, header, sizeof(header)) 1033 < (ssize_t)sizeof(header)) { 1034 return ERROR_IO; 1035 } 1036 1037 int64_t creationTime; 1038 if (header[0] == 1) { 1039 creationTime = U64_AT(&header[4]); 1040 } else if (header[0] != 0) { 1041 return ERROR_MALFORMED; 1042 } else { 1043 creationTime = U32_AT(&header[4]); 1044 } 1045 1046 String8 s; 1047 convertTimeToDate(creationTime, &s); 1048 1049 mFileMetaData->setCString(kKeyDate, s.string()); 1050 1051 *offset += chunk_size; 1052 break; 1053 } 1054 1055 default: 1056 { 1057 *offset += chunk_size; 1058 break; 1059 } 1060 } 1061 1062 return OK; 1063} 1064 1065status_t MPEG4Extractor::parseMetaData(off_t offset, size_t size) { 1066 if (size < 4) { 1067 return ERROR_MALFORMED; 1068 } 1069 1070 uint8_t *buffer = new uint8_t[size + 1]; 1071 if (mDataSource->readAt( 1072 offset, buffer, size) != (ssize_t)size) { 1073 delete[] buffer; 1074 buffer = NULL; 1075 1076 return ERROR_IO; 1077 } 1078 1079 uint32_t flags = U32_AT(buffer); 1080 1081 uint32_t metadataKey = 0; 1082 switch (mPath[4]) { 1083 case FOURCC(0xa9, 'a', 'l', 'b'): 1084 { 1085 metadataKey = kKeyAlbum; 1086 break; 1087 } 1088 case FOURCC(0xa9, 'A', 'R', 'T'): 1089 { 1090 metadataKey = kKeyArtist; 1091 break; 1092 } 1093 case FOURCC('a', 'A', 'R', 'T'): 1094 { 1095 metadataKey = kKeyAlbumArtist; 1096 break; 1097 } 1098 case FOURCC(0xa9, 'd', 'a', 'y'): 1099 { 1100 metadataKey = kKeyYear; 1101 break; 1102 } 1103 case FOURCC(0xa9, 'n', 'a', 'm'): 1104 { 1105 metadataKey = kKeyTitle; 1106 break; 1107 } 1108 case FOURCC(0xa9, 'w', 'r', 't'): 1109 { 1110 metadataKey = kKeyWriter; 1111 break; 1112 } 1113 case FOURCC('c', 'o', 'v', 'r'): 1114 { 1115 metadataKey = kKeyAlbumArt; 1116 break; 1117 } 1118 case FOURCC('g', 'n', 'r', 'e'): 1119 { 1120 metadataKey = kKeyGenre; 1121 break; 1122 } 1123 case FOURCC(0xa9, 'g', 'e', 'n'): 1124 { 1125 metadataKey = kKeyGenre; 1126 break; 1127 } 1128 case FOURCC('t', 'r', 'k', 'n'): 1129 { 1130 if (size == 16 && flags == 0) { 1131 char tmp[16]; 1132 sprintf(tmp, "%d/%d", 1133 (int)buffer[size - 5], (int)buffer[size - 3]); 1134 1135 mFileMetaData->setCString(kKeyCDTrackNumber, tmp); 1136 } 1137 break; 1138 } 1139 case FOURCC('d', 'i', 's', 'k'): 1140 { 1141 if (size == 14 && flags == 0) { 1142 char tmp[16]; 1143 sprintf(tmp, "%d/%d", 1144 (int)buffer[size - 3], (int)buffer[size - 1]); 1145 1146 mFileMetaData->setCString(kKeyDiscNumber, tmp); 1147 } 1148 break; 1149 } 1150 1151 default: 1152 break; 1153 } 1154 1155 if (size >= 8 && metadataKey) { 1156 if (metadataKey == kKeyAlbumArt) { 1157 mFileMetaData->setData( 1158 kKeyAlbumArt, MetaData::TYPE_NONE, 1159 buffer + 8, size - 8); 1160 } else if (metadataKey == kKeyGenre) { 1161 if (flags == 0) { 1162 // uint8_t genre code, iTunes genre codes are 1163 // the standard id3 codes, except they start 1164 // at 1 instead of 0 (e.g. Pop is 14, not 13) 1165 // We use standard id3 numbering, so subtract 1. 1166 int genrecode = (int)buffer[size - 1]; 1167 genrecode--; 1168 if (genrecode < 0) { 1169 genrecode = 255; // reserved for 'unknown genre' 1170 } 1171 char genre[10]; 1172 sprintf(genre, "%d", genrecode); 1173 1174 mFileMetaData->setCString(metadataKey, genre); 1175 } else if (flags == 1) { 1176 // custom genre string 1177 buffer[size] = '\0'; 1178 1179 mFileMetaData->setCString( 1180 metadataKey, (const char *)buffer + 8); 1181 } 1182 } else { 1183 buffer[size] = '\0'; 1184 1185 mFileMetaData->setCString( 1186 metadataKey, (const char *)buffer + 8); 1187 } 1188 } 1189 1190 delete[] buffer; 1191 buffer = NULL; 1192 1193 return OK; 1194} 1195 1196sp<MediaSource> MPEG4Extractor::getTrack(size_t index) { 1197 status_t err; 1198 if ((err = readMetaData()) != OK) { 1199 return NULL; 1200 } 1201 1202 Track *track = mFirstTrack; 1203 while (index > 0) { 1204 if (track == NULL) { 1205 return NULL; 1206 } 1207 1208 track = track->next; 1209 --index; 1210 } 1211 1212 if (track == NULL) { 1213 return NULL; 1214 } 1215 1216 return new MPEG4Source( 1217 track->meta, mDataSource, track->timescale, track->sampleTable); 1218} 1219 1220// static 1221status_t MPEG4Extractor::verifyTrack(Track *track) { 1222 const char *mime; 1223 CHECK(track->meta->findCString(kKeyMIMEType, &mime)); 1224 1225 uint32_t type; 1226 const void *data; 1227 size_t size; 1228 if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) { 1229 if (!track->meta->findData(kKeyAVCC, &type, &data, &size) 1230 || type != kTypeAVCC) { 1231 return ERROR_MALFORMED; 1232 } 1233 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) 1234 || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { 1235 if (!track->meta->findData(kKeyESDS, &type, &data, &size) 1236 || type != kTypeESDS) { 1237 return ERROR_MALFORMED; 1238 } 1239 } 1240 1241 return OK; 1242} 1243 1244status_t MPEG4Extractor::updateAudioTrackInfoFromESDS_MPEG4Audio( 1245 const void *esds_data, size_t esds_size) { 1246 ESDS esds(esds_data, esds_size); 1247 const uint8_t *csd; 1248 size_t csd_size; 1249 if (esds.getCodecSpecificInfo( 1250 (const void **)&csd, &csd_size) != OK) { 1251 return ERROR_MALFORMED; 1252 } 1253 1254#if 0 1255 printf("ESD of size %d\n", csd_size); 1256 hexdump(csd, csd_size); 1257#endif 1258 1259 if (csd_size < 2) { 1260 return ERROR_MALFORMED; 1261 } 1262 1263 uint32_t objectType = csd[0] >> 3; 1264 1265 if (objectType == 31) { 1266 return ERROR_UNSUPPORTED; 1267 } 1268 1269 uint32_t freqIndex = (csd[0] & 7) << 1 | (csd[1] >> 7); 1270 int32_t sampleRate = 0; 1271 int32_t numChannels = 0; 1272 if (freqIndex == 15) { 1273 if (csd_size < 5) { 1274 return ERROR_MALFORMED; 1275 } 1276 1277 sampleRate = (csd[1] & 0x7f) << 17 1278 | csd[2] << 9 1279 | csd[3] << 1 1280 | (csd[4] >> 7); 1281 1282 numChannels = (csd[4] >> 3) & 15; 1283 } else { 1284 static uint32_t kSamplingRate[] = { 1285 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 1286 16000, 12000, 11025, 8000, 7350 1287 }; 1288 1289 if (freqIndex == 13 || freqIndex == 14) { 1290 return ERROR_MALFORMED; 1291 } 1292 1293 sampleRate = kSamplingRate[freqIndex]; 1294 numChannels = (csd[1] >> 3) & 15; 1295 } 1296 1297 if (numChannels == 0) { 1298 return ERROR_UNSUPPORTED; 1299 } 1300 1301 int32_t prevSampleRate; 1302 CHECK(mLastTrack->meta->findInt32(kKeySampleRate, &prevSampleRate)); 1303 1304 if (prevSampleRate != sampleRate) { 1305 LOGV("mpeg4 audio sample rate different from previous setting. " 1306 "was: %d, now: %d", prevSampleRate, sampleRate); 1307 } 1308 1309 mLastTrack->meta->setInt32(kKeySampleRate, sampleRate); 1310 1311 int32_t prevChannelCount; 1312 CHECK(mLastTrack->meta->findInt32(kKeyChannelCount, &prevChannelCount)); 1313 1314 if (prevChannelCount != numChannels) { 1315 LOGV("mpeg4 audio channel count different from previous setting. " 1316 "was: %d, now: %d", prevChannelCount, numChannels); 1317 } 1318 1319 mLastTrack->meta->setInt32(kKeyChannelCount, numChannels); 1320 1321 return OK; 1322} 1323 1324//////////////////////////////////////////////////////////////////////////////// 1325 1326MPEG4Source::MPEG4Source( 1327 const sp<MetaData> &format, 1328 const sp<DataSource> &dataSource, 1329 int32_t timeScale, 1330 const sp<SampleTable> &sampleTable) 1331 : mFormat(format), 1332 mDataSource(dataSource), 1333 mTimescale(timeScale), 1334 mSampleTable(sampleTable), 1335 mCurrentSampleIndex(0), 1336 mIsAVC(false), 1337 mNALLengthSize(0), 1338 mStarted(false), 1339 mGroup(NULL), 1340 mBuffer(NULL), 1341 mWantsNALFragments(false), 1342 mSrcBuffer(NULL) { 1343 const char *mime; 1344 bool success = mFormat->findCString(kKeyMIMEType, &mime); 1345 CHECK(success); 1346 1347 mIsAVC = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC); 1348 1349 if (mIsAVC) { 1350 uint32_t type; 1351 const void *data; 1352 size_t size; 1353 CHECK(format->findData(kKeyAVCC, &type, &data, &size)); 1354 1355 const uint8_t *ptr = (const uint8_t *)data; 1356 1357 CHECK(size >= 7); 1358 CHECK_EQ(ptr[0], 1); // configurationVersion == 1 1359 1360 // The number of bytes used to encode the length of a NAL unit. 1361 mNALLengthSize = 1 + (ptr[4] & 3); 1362 } 1363} 1364 1365MPEG4Source::~MPEG4Source() { 1366 if (mStarted) { 1367 stop(); 1368 } 1369} 1370 1371status_t MPEG4Source::start(MetaData *params) { 1372 Mutex::Autolock autoLock(mLock); 1373 1374 CHECK(!mStarted); 1375 1376 int32_t val; 1377 if (params && params->findInt32(kKeyWantsNALFragments, &val) 1378 && val != 0) { 1379 mWantsNALFragments = true; 1380 } else { 1381 mWantsNALFragments = false; 1382 } 1383 1384 mGroup = new MediaBufferGroup; 1385 1386 int32_t max_size; 1387 CHECK(mFormat->findInt32(kKeyMaxInputSize, &max_size)); 1388 1389 mGroup->add_buffer(new MediaBuffer(max_size)); 1390 1391 mSrcBuffer = new uint8_t[max_size]; 1392 1393 mStarted = true; 1394 1395 return OK; 1396} 1397 1398status_t MPEG4Source::stop() { 1399 Mutex::Autolock autoLock(mLock); 1400 1401 CHECK(mStarted); 1402 1403 if (mBuffer != NULL) { 1404 mBuffer->release(); 1405 mBuffer = NULL; 1406 } 1407 1408 delete[] mSrcBuffer; 1409 mSrcBuffer = NULL; 1410 1411 delete mGroup; 1412 mGroup = NULL; 1413 1414 mStarted = false; 1415 mCurrentSampleIndex = 0; 1416 1417 return OK; 1418} 1419 1420sp<MetaData> MPEG4Source::getFormat() { 1421 Mutex::Autolock autoLock(mLock); 1422 1423 return mFormat; 1424} 1425 1426size_t MPEG4Source::parseNALSize(const uint8_t *data) const { 1427 switch (mNALLengthSize) { 1428 case 1: 1429 return *data; 1430 case 2: 1431 return U16_AT(data); 1432 case 3: 1433 return ((size_t)data[0] << 16) | U16_AT(&data[1]); 1434 case 4: 1435 return U32_AT(data); 1436 } 1437 1438 // This cannot happen, mNALLengthSize springs to life by adding 1 to 1439 // a 2-bit integer. 1440 CHECK(!"Should not be here."); 1441 1442 return 0; 1443} 1444 1445status_t MPEG4Source::read( 1446 MediaBuffer **out, const ReadOptions *options) { 1447 Mutex::Autolock autoLock(mLock); 1448 1449 CHECK(mStarted); 1450 1451 *out = NULL; 1452 1453 int64_t seekTimeUs; 1454 if (options && options->getSeekTo(&seekTimeUs)) { 1455 uint32_t sampleIndex; 1456 status_t err = mSampleTable->findClosestSample( 1457 seekTimeUs * mTimescale / 1000000, 1458 &sampleIndex, SampleTable::kSyncSample_Flag); 1459 1460 if (err != OK) { 1461 if (err == ERROR_OUT_OF_RANGE) { 1462 // An attempt to seek past the end of the stream would 1463 // normally cause this ERROR_OUT_OF_RANGE error. Propagating 1464 // this all the way to the MediaPlayer would cause abnormal 1465 // termination. Legacy behaviour appears to be to behave as if 1466 // we had seeked to the end of stream, ending normally. 1467 err = ERROR_END_OF_STREAM; 1468 } 1469 return err; 1470 } 1471 1472 mCurrentSampleIndex = sampleIndex; 1473 if (mBuffer != NULL) { 1474 mBuffer->release(); 1475 mBuffer = NULL; 1476 } 1477 1478 // fall through 1479 } 1480 1481 off_t offset; 1482 size_t size; 1483 uint32_t dts; 1484 bool newBuffer = false; 1485 if (mBuffer == NULL) { 1486 newBuffer = true; 1487 1488 status_t err = 1489 mSampleTable->getMetaDataForSample( 1490 mCurrentSampleIndex, &offset, &size, &dts); 1491 1492 if (err != OK) { 1493 return err; 1494 } 1495 1496 err = mGroup->acquire_buffer(&mBuffer); 1497 1498 if (err != OK) { 1499 CHECK_EQ(mBuffer, NULL); 1500 return err; 1501 } 1502 } 1503 1504 if (!mIsAVC || mWantsNALFragments) { 1505 if (newBuffer) { 1506 ssize_t num_bytes_read = 1507 mDataSource->readAt(offset, (uint8_t *)mBuffer->data(), size); 1508 1509 if (num_bytes_read < (ssize_t)size) { 1510 mBuffer->release(); 1511 mBuffer = NULL; 1512 1513 return ERROR_IO; 1514 } 1515 1516 CHECK(mBuffer != NULL); 1517 mBuffer->set_range(0, size); 1518 mBuffer->meta_data()->clear(); 1519 mBuffer->meta_data()->setInt64( 1520 kKeyTime, ((int64_t)dts * 1000000) / mTimescale); 1521 ++mCurrentSampleIndex; 1522 } 1523 1524 if (!mIsAVC) { 1525 *out = mBuffer; 1526 mBuffer = NULL; 1527 1528 return OK; 1529 } 1530 1531 // Each NAL unit is split up into its constituent fragments and 1532 // each one of them returned in its own buffer. 1533 1534 CHECK(mBuffer->range_length() >= mNALLengthSize); 1535 1536 const uint8_t *src = 1537 (const uint8_t *)mBuffer->data() + mBuffer->range_offset(); 1538 1539 size_t nal_size = parseNALSize(src); 1540 if (mBuffer->range_length() < mNALLengthSize + nal_size) { 1541 LOGE("incomplete NAL unit."); 1542 1543 mBuffer->release(); 1544 mBuffer = NULL; 1545 1546 return ERROR_MALFORMED; 1547 } 1548 1549 MediaBuffer *clone = mBuffer->clone(); 1550 CHECK(clone != NULL); 1551 clone->set_range(mBuffer->range_offset() + mNALLengthSize, nal_size); 1552 1553 CHECK(mBuffer != NULL); 1554 mBuffer->set_range( 1555 mBuffer->range_offset() + mNALLengthSize + nal_size, 1556 mBuffer->range_length() - mNALLengthSize - nal_size); 1557 1558 if (mBuffer->range_length() == 0) { 1559 mBuffer->release(); 1560 mBuffer = NULL; 1561 } 1562 1563 *out = clone; 1564 1565 return OK; 1566 } else { 1567 // Whole NAL units are returned but each fragment is prefixed by 1568 // the start code (0x00 00 00 01). 1569 1570 ssize_t num_bytes_read = 1571 mDataSource->readAt(offset, mSrcBuffer, size); 1572 1573 if (num_bytes_read < (ssize_t)size) { 1574 mBuffer->release(); 1575 mBuffer = NULL; 1576 1577 return ERROR_IO; 1578 } 1579 1580 uint8_t *dstData = (uint8_t *)mBuffer->data(); 1581 size_t srcOffset = 0; 1582 size_t dstOffset = 0; 1583 1584 while (srcOffset < size) { 1585 CHECK(srcOffset + mNALLengthSize <= size); 1586 size_t nalLength = parseNALSize(&mSrcBuffer[srcOffset]); 1587 srcOffset += mNALLengthSize; 1588 1589 if (srcOffset + nalLength > size) { 1590 mBuffer->release(); 1591 mBuffer = NULL; 1592 1593 return ERROR_MALFORMED; 1594 } 1595 1596 if (nalLength == 0) { 1597 continue; 1598 } 1599 1600 CHECK(dstOffset + 4 <= mBuffer->size()); 1601 1602 dstData[dstOffset++] = 0; 1603 dstData[dstOffset++] = 0; 1604 dstData[dstOffset++] = 0; 1605 dstData[dstOffset++] = 1; 1606 memcpy(&dstData[dstOffset], &mSrcBuffer[srcOffset], nalLength); 1607 srcOffset += nalLength; 1608 dstOffset += nalLength; 1609 } 1610 CHECK_EQ(srcOffset, size); 1611 1612 CHECK(mBuffer != NULL); 1613 mBuffer->set_range(0, dstOffset); 1614 mBuffer->meta_data()->clear(); 1615 mBuffer->meta_data()->setInt64( 1616 kKeyTime, ((int64_t)dts * 1000000) / mTimescale); 1617 ++mCurrentSampleIndex; 1618 1619 *out = mBuffer; 1620 mBuffer = NULL; 1621 1622 return OK; 1623 } 1624} 1625 1626bool SniffMPEG4( 1627 const sp<DataSource> &source, String8 *mimeType, float *confidence) { 1628 uint8_t header[8]; 1629 1630 ssize_t n = source->readAt(4, header, sizeof(header)); 1631 if (n < (ssize_t)sizeof(header)) { 1632 return false; 1633 } 1634 1635 if (!memcmp(header, "ftyp3gp", 7) || !memcmp(header, "ftypmp42", 8) 1636 || !memcmp(header, "ftypisom", 8) || !memcmp(header, "ftypM4V ", 8) 1637 || !memcmp(header, "ftypM4A ", 8) || !memcmp(header, "ftypf4v ", 8) 1638 || !memcmp(header, "ftypkddi", 8)) { 1639 *mimeType = MEDIA_MIMETYPE_CONTAINER_MPEG4; 1640 *confidence = 0.1; 1641 1642 return true; 1643 } 1644 1645 return false; 1646} 1647 1648} // namespace android 1649 1650