MPEG4Writer.cpp revision 3c0131f02b6f008321608044c53bccce2ac5f6dd
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_NDEBUG 0 18#define LOG_TAG "MPEG4Writer" 19#include <utils/Log.h> 20 21#include <arpa/inet.h> 22 23#include <ctype.h> 24#include <pthread.h> 25 26#include <media/stagefright/MPEG4Writer.h> 27#include <media/stagefright/MediaBuffer.h> 28#include <media/stagefright/MetaData.h> 29#include <media/stagefright/MediaDebug.h> 30#include <media/stagefright/MediaDefs.h> 31#include <media/stagefright/MediaErrors.h> 32#include <media/stagefright/MediaSource.h> 33#include <media/stagefright/Utils.h> 34#include <media/mediarecorder.h> 35 36namespace android { 37 38class MPEG4Writer::Track { 39public: 40 Track(MPEG4Writer *owner, const sp<MediaSource> &source); 41 ~Track(); 42 43 status_t start(); 44 void stop(); 45 bool reachedEOS(); 46 47 int64_t getDurationUs() const; 48 int64_t getEstimatedTrackSizeBytes() const; 49 void writeTrackHeader(int32_t trackID); 50 51private: 52 MPEG4Writer *mOwner; 53 sp<MetaData> mMeta; 54 sp<MediaSource> mSource; 55 volatile bool mDone; 56 int64_t mMaxTimeStampUs; 57 int64_t mEstimatedTrackSizeBytes; 58 59 pthread_t mThread; 60 61 struct SampleInfo { 62 size_t size; 63 int64_t timestamp; 64 }; 65 List<SampleInfo> mSampleInfos; 66 bool mSamplesHaveSameSize; 67 68 List<MediaBuffer *> mChunkSamples; 69 List<off_t> mChunkOffsets; 70 71 struct StscTableEntry { 72 73 StscTableEntry(uint32_t chunk, uint32_t samples, uint32_t id) 74 : firstChunk(chunk), 75 samplesPerChunk(samples), 76 sampleDescriptionId(id) {} 77 78 uint32_t firstChunk; 79 uint32_t samplesPerChunk; 80 uint32_t sampleDescriptionId; 81 }; 82 List<StscTableEntry> mStscTableEntries; 83 84 List<int32_t> mStssTableEntries; 85 86 struct SttsTableEntry { 87 88 SttsTableEntry(uint32_t count, uint32_t duration) 89 : sampleCount(count), sampleDuration(duration) {} 90 91 uint32_t sampleCount; 92 uint32_t sampleDuration; 93 }; 94 List<SttsTableEntry> mSttsTableEntries; 95 96 void *mCodecSpecificData; 97 size_t mCodecSpecificDataSize; 98 bool mGotAllCodecSpecificData; 99 100 bool mReachedEOS; 101 int64_t mStartTimestampUs; 102 103 static void *ThreadWrapper(void *me); 104 void threadEntry(); 105 106 status_t makeAVCCodecSpecificData( 107 const uint8_t *data, size_t size); 108 void writeOneChunk(bool isAvc); 109 110 Track(const Track &); 111 Track &operator=(const Track &); 112}; 113 114#define USE_NALLEN_FOUR 1 115 116MPEG4Writer::MPEG4Writer(const char *filename) 117 : mFile(fopen(filename, "wb")), 118 mOffset(0), 119 mMdatOffset(0), 120 mEstimatedMoovBoxSize(0), 121 mInterleaveDurationUs(500000) { 122 CHECK(mFile != NULL); 123} 124 125MPEG4Writer::MPEG4Writer(int fd) 126 : mFile(fdopen(fd, "wb")), 127 mOffset(0), 128 mMdatOffset(0), 129 mEstimatedMoovBoxSize(0), 130 mInterleaveDurationUs(500000) { 131 CHECK(mFile != NULL); 132} 133 134MPEG4Writer::~MPEG4Writer() { 135 stop(); 136 137 for (List<Track *>::iterator it = mTracks.begin(); 138 it != mTracks.end(); ++it) { 139 delete *it; 140 } 141 mTracks.clear(); 142} 143 144status_t MPEG4Writer::addSource(const sp<MediaSource> &source) { 145 Track *track = new Track(this, source); 146 mTracks.push_back(track); 147 148 return OK; 149} 150 151status_t MPEG4Writer::start() { 152 if (mFile == NULL) { 153 return UNKNOWN_ERROR; 154 } 155 156 mStartTimestampUs = 0; 157 mStreamableFile = true; 158 mWriteMoovBoxToMemory = false; 159 mMoovBoxBuffer = NULL; 160 mMoovBoxBufferOffset = 0; 161 162 beginBox("ftyp"); 163 writeFourcc("isom"); 164 writeInt32(0); 165 writeFourcc("isom"); 166 endBox(); 167 168 mFreeBoxOffset = mOffset; 169 170 if (mEstimatedMoovBoxSize == 0) { 171 // XXX: Estimate the moov box size 172 // based on max file size or duration limit 173 mEstimatedMoovBoxSize = 0x0F00; 174 } 175 CHECK(mEstimatedMoovBoxSize >= 8); 176 fseeko(mFile, mFreeBoxOffset, SEEK_SET); 177 writeInt32(mEstimatedMoovBoxSize); 178 write("free", 4); 179 180 mMdatOffset = mFreeBoxOffset + mEstimatedMoovBoxSize; 181 mOffset = mMdatOffset; 182 fseeko(mFile, mMdatOffset, SEEK_SET); 183 write("\x00\x00\x00\x01mdat????????", 16); 184 for (List<Track *>::iterator it = mTracks.begin(); 185 it != mTracks.end(); ++it) { 186 status_t err = (*it)->start(); 187 188 if (err != OK) { 189 for (List<Track *>::iterator it2 = mTracks.begin(); 190 it2 != it; ++it2) { 191 (*it2)->stop(); 192 } 193 194 return err; 195 } 196 } 197 198 return OK; 199} 200 201void MPEG4Writer::stop() { 202 if (mFile == NULL) { 203 return; 204 } 205 206 int64_t max_duration = 0; 207 for (List<Track *>::iterator it = mTracks.begin(); 208 it != mTracks.end(); ++it) { 209 (*it)->stop(); 210 211 int64_t duration = (*it)->getDurationUs(); 212 if (duration > max_duration) { 213 max_duration = duration; 214 } 215 } 216 217 218 // Fix up the size of the 'mdat' chunk. 219 fseeko(mFile, mMdatOffset + 8, SEEK_SET); 220 int64_t size = mOffset - mMdatOffset; 221 size = hton64(size); 222 fwrite(&size, 1, 8, mFile); 223 fseeko(mFile, mOffset, SEEK_SET); 224 225 time_t now = time(NULL); 226 const off_t moovOffset = mOffset; 227 mWriteMoovBoxToMemory = true; 228 mMoovBoxBuffer = (uint8_t *) malloc(mEstimatedMoovBoxSize); 229 mMoovBoxBufferOffset = 0; 230 CHECK(mMoovBoxBuffer != NULL); 231 232 beginBox("moov"); 233 234 beginBox("mvhd"); 235 writeInt32(0); // version=0, flags=0 236 writeInt32(now); // creation time 237 writeInt32(now); // modification time 238 writeInt32(1000); // timescale 239 writeInt32(max_duration / 1000); 240 writeInt32(0x10000); // rate 241 writeInt16(0x100); // volume 242 writeInt16(0); // reserved 243 writeInt32(0); // reserved 244 writeInt32(0); // reserved 245 writeInt32(0x10000); // matrix 246 writeInt32(0); 247 writeInt32(0); 248 writeInt32(0); 249 writeInt32(0x10000); 250 writeInt32(0); 251 writeInt32(0); 252 writeInt32(0); 253 writeInt32(0x40000000); 254 writeInt32(0); // predefined 255 writeInt32(0); // predefined 256 writeInt32(0); // predefined 257 writeInt32(0); // predefined 258 writeInt32(0); // predefined 259 writeInt32(0); // predefined 260 writeInt32(mTracks.size() + 1); // nextTrackID 261 endBox(); // mvhd 262 263 int32_t id = 1; 264 for (List<Track *>::iterator it = mTracks.begin(); 265 it != mTracks.end(); ++it, ++id) { 266 (*it)->writeTrackHeader(id); 267 } 268 endBox(); // moov 269 270 mWriteMoovBoxToMemory = false; 271 if (mStreamableFile) { 272 CHECK(mMoovBoxBufferOffset + 8 <= mEstimatedMoovBoxSize); 273 274 // Moov box 275 fseeko(mFile, mFreeBoxOffset, SEEK_SET); 276 mOffset = mFreeBoxOffset; 277 write(mMoovBoxBuffer, 1, mMoovBoxBufferOffset, mFile); 278 279 // Free box 280 mFreeBoxOffset = mStreamableFile? mOffset: mFreeBoxOffset; 281 fseeko(mFile, mFreeBoxOffset, SEEK_SET); 282 writeInt32(mEstimatedMoovBoxSize - mMoovBoxBufferOffset); 283 write("free", 4); 284 285 // Free temp memory 286 free(mMoovBoxBuffer); 287 mMoovBoxBuffer = NULL; 288 mMoovBoxBufferOffset = 0; 289 } 290 291 CHECK(mBoxes.empty()); 292 293 fflush(mFile); 294 fclose(mFile); 295 mFile = NULL; 296} 297 298status_t MPEG4Writer::setInterleaveDuration(uint32_t durationUs) { 299 mInterleaveDurationUs = durationUs; 300 return OK; 301} 302 303void MPEG4Writer::lock() { 304 mLock.lock(); 305} 306 307void MPEG4Writer::unlock() { 308 mLock.unlock(); 309} 310 311off_t MPEG4Writer::addSample_l(MediaBuffer *buffer) { 312 off_t old_offset = mOffset; 313 314 fwrite((const uint8_t *)buffer->data() + buffer->range_offset(), 315 1, buffer->range_length(), mFile); 316 317 mOffset += buffer->range_length(); 318 319 return old_offset; 320} 321 322static void StripStartcode(MediaBuffer *buffer) { 323 if (buffer->range_length() < 4) { 324 return; 325 } 326 327 const uint8_t *ptr = 328 (const uint8_t *)buffer->data() + buffer->range_offset(); 329 330 if (!memcmp(ptr, "\x00\x00\x00\x01", 4)) { 331 buffer->set_range( 332 buffer->range_offset() + 4, buffer->range_length() - 4); 333 } 334} 335 336off_t MPEG4Writer::addLengthPrefixedSample_l(MediaBuffer *buffer) { 337 StripStartcode(buffer); 338 339 off_t old_offset = mOffset; 340 341 size_t length = buffer->range_length(); 342 343#if USE_NALLEN_FOUR 344 uint8_t x = length >> 24; 345 fwrite(&x, 1, 1, mFile); 346 x = (length >> 16) & 0xff; 347 fwrite(&x, 1, 1, mFile); 348 x = (length >> 8) & 0xff; 349 fwrite(&x, 1, 1, mFile); 350 x = length & 0xff; 351 fwrite(&x, 1, 1, mFile); 352#else 353 CHECK(length < 65536); 354 355 uint8_t x = length >> 8; 356 fwrite(&x, 1, 1, mFile); 357 x = length & 0xff; 358 fwrite(&x, 1, 1, mFile); 359#endif 360 361 fwrite((const uint8_t *)buffer->data() + buffer->range_offset(), 362 1, length, mFile); 363 364#if USE_NALLEN_FOUR 365 mOffset += length + 4; 366#else 367 mOffset += length + 2; 368#endif 369 370 return old_offset; 371} 372 373size_t MPEG4Writer::write( 374 const void *ptr, size_t size, size_t nmemb, FILE *stream) { 375 376 const size_t bytes = size * nmemb; 377 if (mWriteMoovBoxToMemory) { 378 if (8 + mMoovBoxBufferOffset + bytes > mEstimatedMoovBoxSize) { 379 for (List<off_t>::iterator it = mBoxes.begin(); 380 it != mBoxes.end(); ++it) { 381 (*it) += mOffset; 382 } 383 fseeko(mFile, mOffset, SEEK_SET); 384 fwrite(mMoovBoxBuffer, 1, mMoovBoxBufferOffset, stream); 385 fwrite(ptr, size, nmemb, stream); 386 mOffset += (bytes + mMoovBoxBufferOffset); 387 free(mMoovBoxBuffer); 388 mMoovBoxBuffer = NULL; 389 mMoovBoxBufferOffset = 0; 390 mWriteMoovBoxToMemory = false; 391 mStreamableFile = false; 392 } else { 393 memcpy(mMoovBoxBuffer + mMoovBoxBufferOffset, ptr, bytes); 394 mMoovBoxBufferOffset += bytes; 395 } 396 } else { 397 fwrite(ptr, size, nmemb, stream); 398 mOffset += bytes; 399 } 400 return bytes; 401} 402 403void MPEG4Writer::beginBox(const char *fourcc) { 404 CHECK_EQ(strlen(fourcc), 4); 405 406 mBoxes.push_back(mWriteMoovBoxToMemory? 407 mMoovBoxBufferOffset: mOffset); 408 409 writeInt32(0); 410 writeFourcc(fourcc); 411} 412 413void MPEG4Writer::endBox() { 414 CHECK(!mBoxes.empty()); 415 416 off_t offset = *--mBoxes.end(); 417 mBoxes.erase(--mBoxes.end()); 418 419 if (mWriteMoovBoxToMemory) { 420 int32_t x = htonl(mMoovBoxBufferOffset - offset); 421 memcpy(mMoovBoxBuffer + offset, &x, 4); 422 } else { 423 fseeko(mFile, offset, SEEK_SET); 424 writeInt32(mOffset - offset); 425 mOffset -= 4; 426 fseeko(mFile, mOffset, SEEK_SET); 427 } 428} 429 430void MPEG4Writer::writeInt8(int8_t x) { 431 write(&x, 1, 1, mFile); 432} 433 434void MPEG4Writer::writeInt16(int16_t x) { 435 x = htons(x); 436 write(&x, 1, 2, mFile); 437} 438 439void MPEG4Writer::writeInt32(int32_t x) { 440 x = htonl(x); 441 write(&x, 1, 4, mFile); 442} 443 444void MPEG4Writer::writeInt64(int64_t x) { 445 x = hton64(x); 446 write(&x, 1, 8, mFile); 447} 448 449void MPEG4Writer::writeCString(const char *s) { 450 size_t n = strlen(s); 451 write(s, 1, n + 1, mFile); 452} 453 454void MPEG4Writer::writeFourcc(const char *s) { 455 CHECK_EQ(strlen(s), 4); 456 write(s, 1, 4, mFile); 457} 458 459void MPEG4Writer::write(const void *data, size_t size) { 460 write(data, 1, size, mFile); 461} 462 463bool MPEG4Writer::exceedsFileSizeLimit() { 464 // No limit 465 if (mMaxFileSizeLimitBytes == 0) { 466 return false; 467 } 468 469 int64_t nTotalBytesEstimate = mEstimatedMoovBoxSize; 470 for (List<Track *>::iterator it = mTracks.begin(); 471 it != mTracks.end(); ++it) { 472 nTotalBytesEstimate += (*it)->getEstimatedTrackSizeBytes(); 473 } 474 return (nTotalBytesEstimate >= mMaxFileSizeLimitBytes); 475} 476 477bool MPEG4Writer::exceedsFileDurationLimit() { 478 // No limit 479 if (mMaxFileDurationLimitUs == 0) { 480 return false; 481 } 482 483 for (List<Track *>::iterator it = mTracks.begin(); 484 it != mTracks.end(); ++it) { 485 if ((*it)->getDurationUs() >= mMaxFileDurationLimitUs) { 486 return true; 487 } 488 } 489 return false; 490} 491 492bool MPEG4Writer::reachedEOS() { 493 bool allDone = true; 494 for (List<Track *>::iterator it = mTracks.begin(); 495 it != mTracks.end(); ++it) { 496 if (!(*it)->reachedEOS()) { 497 allDone = false; 498 break; 499 } 500 } 501 502 return allDone; 503} 504 505void MPEG4Writer::setStartTimestamp(int64_t timeUs) { 506 LOGI("setStartTimestamp: %lld", timeUs); 507 Mutex::Autolock autoLock(mLock); 508 if (mStartTimestampUs != 0) { 509 return; // Sorry, too late 510 } 511 mStartTimestampUs = timeUs; 512} 513 514int64_t MPEG4Writer::getStartTimestamp() { 515 LOGI("getStartTimestamp: %lld", mStartTimestampUs); 516 Mutex::Autolock autoLock(mLock); 517 return mStartTimestampUs; 518} 519 520//////////////////////////////////////////////////////////////////////////////// 521 522MPEG4Writer::Track::Track( 523 MPEG4Writer *owner, const sp<MediaSource> &source) 524 : mOwner(owner), 525 mMeta(source->getFormat()), 526 mSource(source), 527 mDone(false), 528 mMaxTimeStampUs(0), 529 mSamplesHaveSameSize(true), 530 mCodecSpecificData(NULL), 531 mCodecSpecificDataSize(0), 532 mGotAllCodecSpecificData(false), 533 mReachedEOS(false) { 534} 535 536MPEG4Writer::Track::~Track() { 537 stop(); 538 539 if (mCodecSpecificData != NULL) { 540 free(mCodecSpecificData); 541 mCodecSpecificData = NULL; 542 } 543} 544 545status_t MPEG4Writer::Track::start() { 546 status_t err = mSource->start(); 547 548 if (err != OK) { 549 mDone = mReachedEOS = true; 550 return err; 551 } 552 553 pthread_attr_t attr; 554 pthread_attr_init(&attr); 555 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 556 557 mDone = false; 558 mMaxTimeStampUs = 0; 559 mReachedEOS = false; 560 561 pthread_create(&mThread, &attr, ThreadWrapper, this); 562 pthread_attr_destroy(&attr); 563 564 return OK; 565} 566 567void MPEG4Writer::Track::stop() { 568 if (mDone) { 569 return; 570 } 571 572 mDone = true; 573 574 void *dummy; 575 pthread_join(mThread, &dummy); 576 577 mSource->stop(); 578} 579 580bool MPEG4Writer::Track::reachedEOS() { 581 return mReachedEOS; 582} 583 584// static 585void *MPEG4Writer::Track::ThreadWrapper(void *me) { 586 Track *track = static_cast<Track *>(me); 587 588 track->threadEntry(); 589 590 return NULL; 591} 592 593#include <ctype.h> 594static void hexdump(const void *_data, size_t size) { 595 const uint8_t *data = (const uint8_t *)_data; 596 size_t offset = 0; 597 while (offset < size) { 598 printf("0x%04x ", offset); 599 600 size_t n = size - offset; 601 if (n > 16) { 602 n = 16; 603 } 604 605 for (size_t i = 0; i < 16; ++i) { 606 if (i == 8) { 607 printf(" "); 608 } 609 610 if (offset + i < size) { 611 printf("%02x ", data[offset + i]); 612 } else { 613 printf(" "); 614 } 615 } 616 617 printf(" "); 618 619 for (size_t i = 0; i < n; ++i) { 620 if (isprint(data[offset + i])) { 621 printf("%c", data[offset + i]); 622 } else { 623 printf("."); 624 } 625 } 626 627 printf("\n"); 628 629 offset += 16; 630 } 631} 632 633 634status_t MPEG4Writer::Track::makeAVCCodecSpecificData( 635 const uint8_t *data, size_t size) { 636 // hexdump(data, size); 637 638 if (mCodecSpecificData != NULL) { 639 LOGE("Already have codec specific data"); 640 return ERROR_MALFORMED; 641 } 642 643 if (size < 4 || memcmp("\x00\x00\x00\x01", data, 4)) { 644 LOGE("Must start with a start code"); 645 return ERROR_MALFORMED; 646 } 647 648 size_t picParamOffset = 4; 649 while (picParamOffset + 3 < size 650 && memcmp("\x00\x00\x00\x01", &data[picParamOffset], 4)) { 651 ++picParamOffset; 652 } 653 654 if (picParamOffset + 3 >= size) { 655 LOGE("Could not find start-code for pictureParameterSet"); 656 return ERROR_MALFORMED; 657 } 658 659 size_t seqParamSetLength = picParamOffset - 4; 660 size_t picParamSetLength = size - picParamOffset - 4; 661 662 mCodecSpecificDataSize = 663 6 + 1 + seqParamSetLength + 2 + picParamSetLength + 2; 664 665 mCodecSpecificData = malloc(mCodecSpecificDataSize); 666 uint8_t *header = (uint8_t *)mCodecSpecificData; 667 header[0] = 1; 668 header[1] = 0x42; // profile 669 header[2] = 0x80; 670 header[3] = 0x1e; // level 671 672#if USE_NALLEN_FOUR 673 header[4] = 0xfc | 3; // length size == 4 bytes 674#else 675 header[4] = 0xfc | 1; // length size == 2 bytes 676#endif 677 678 header[5] = 0xe0 | 1; 679 header[6] = seqParamSetLength >> 8; 680 header[7] = seqParamSetLength & 0xff; 681 memcpy(&header[8], &data[4], seqParamSetLength); 682 header += 8 + seqParamSetLength; 683 header[0] = 1; 684 header[1] = picParamSetLength >> 8; 685 header[2] = picParamSetLength & 0xff; 686 memcpy(&header[3], &data[picParamOffset + 4], picParamSetLength); 687 688 return OK; 689} 690 691void MPEG4Writer::Track::threadEntry() { 692 sp<MetaData> meta = mSource->getFormat(); 693 const char *mime; 694 meta->findCString(kKeyMIMEType, &mime); 695 bool is_mpeg4 = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) || 696 !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC); 697 bool is_avc = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC); 698 int32_t count = 0; 699 const int64_t interleaveDurationUs = mOwner->interleaveDuration(); 700 int64_t chunkTimestampUs = 0; 701 int32_t nChunks = 0; 702 int32_t nZeroLengthFrames = 0; 703 int64_t lastTimestamp = 0; // Timestamp of the previous sample 704 int64_t lastDuration = 0; // Time spacing between the previous two samples 705 int32_t sampleCount = 1; // Sample count in the current stts table entry 706 uint32_t previousSampleSize = 0; // Size of the previous sample 707 708 mEstimatedTrackSizeBytes = 0; 709 MediaBuffer *buffer; 710 while (!mDone && mSource->read(&buffer) == OK) { 711 if (buffer->range_length() == 0) { 712 buffer->release(); 713 buffer = NULL; 714 ++nZeroLengthFrames; 715 continue; 716 } 717 718 ++count; 719 720 int32_t isCodecConfig; 721 if (buffer->meta_data()->findInt32(kKeyIsCodecConfig, &isCodecConfig) 722 && isCodecConfig) { 723 CHECK(!mGotAllCodecSpecificData); 724 725 if (is_avc) { 726 status_t err = makeAVCCodecSpecificData( 727 (const uint8_t *)buffer->data() 728 + buffer->range_offset(), 729 buffer->range_length()); 730 CHECK_EQ(OK, err); 731 } else if (is_mpeg4) { 732 mCodecSpecificDataSize = buffer->range_length(); 733 mCodecSpecificData = malloc(mCodecSpecificDataSize); 734 memcpy(mCodecSpecificData, 735 (const uint8_t *)buffer->data() 736 + buffer->range_offset(), 737 buffer->range_length()); 738 } 739 740 buffer->release(); 741 buffer = NULL; 742 743 mGotAllCodecSpecificData = true; 744 continue; 745 } else if (!mGotAllCodecSpecificData && 746 count == 1 && is_mpeg4 && mCodecSpecificData == NULL) { 747 // The TI mpeg4 encoder does not properly set the 748 // codec-specific-data flag. 749 750 const uint8_t *data = 751 (const uint8_t *)buffer->data() + buffer->range_offset(); 752 753 const size_t size = buffer->range_length(); 754 755 size_t offset = 0; 756 while (offset + 3 < size) { 757 if (data[offset] == 0x00 && data[offset + 1] == 0x00 758 && data[offset + 2] == 0x01 && data[offset + 3] == 0xb6) { 759 break; 760 } 761 762 ++offset; 763 } 764 765 // CHECK(offset + 3 < size); 766 if (offset + 3 >= size) { 767 // XXX assume the entire first chunk of data is the codec specific 768 // data. 769 offset = size; 770 } 771 772 mCodecSpecificDataSize = offset; 773 mCodecSpecificData = malloc(offset); 774 memcpy(mCodecSpecificData, data, offset); 775 776 buffer->set_range(buffer->range_offset() + offset, size - offset); 777 778 if (size == offset) { 779 buffer->release(); 780 buffer = NULL; 781 782 continue; 783 } 784 785 mGotAllCodecSpecificData = true; 786 } else if (!mGotAllCodecSpecificData && is_avc && count < 3) { 787 // The TI video encoder does not flag codec specific data 788 // as such and also splits up SPS and PPS across two buffers. 789 790 const uint8_t *data = 791 (const uint8_t *)buffer->data() + buffer->range_offset(); 792 793 size_t size = buffer->range_length(); 794 795 CHECK(count == 2 || mCodecSpecificData == NULL); 796 797 size_t offset = mCodecSpecificDataSize; 798 mCodecSpecificDataSize += size + 4; 799 mCodecSpecificData = 800 realloc(mCodecSpecificData, mCodecSpecificDataSize); 801 802 memcpy((uint8_t *)mCodecSpecificData + offset, 803 "\x00\x00\x00\x01", 4); 804 805 memcpy((uint8_t *)mCodecSpecificData + offset + 4, data, size); 806 807 buffer->release(); 808 buffer = NULL; 809 810 if (count == 2) { 811 void *tmp = mCodecSpecificData; 812 size = mCodecSpecificDataSize; 813 mCodecSpecificData = NULL; 814 mCodecSpecificDataSize = 0; 815 816 status_t err = makeAVCCodecSpecificData( 817 (const uint8_t *)tmp, size); 818 free(tmp); 819 tmp = NULL; 820 CHECK_EQ(OK, err); 821 822 mGotAllCodecSpecificData = true; 823 } 824 825 continue; 826 } 827 828 SampleInfo info; 829 info.size = is_avc 830#if USE_NALLEN_FOUR 831 ? buffer->range_length() + 4 832#else 833 ? buffer->range_length() + 2 834#endif 835 : buffer->range_length(); 836 837 // Max file size or duration handling 838 mEstimatedTrackSizeBytes += info.size; 839 if (mOwner->exceedsFileSizeLimit()) { 840 buffer->release(); 841 buffer = NULL; 842 mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0); 843 break; 844 } 845 if (mOwner->exceedsFileDurationLimit()) { 846 buffer->release(); 847 buffer = NULL; 848 mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0); 849 break; 850 } 851 852 bool is_audio = !strncasecmp(mime, "audio/", 6); 853 854 int64_t timestampUs; 855 CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); 856 if (mSampleInfos.empty()) { 857 mOwner->setStartTimestamp(timestampUs); 858 mStartTimestampUs = (timestampUs - mOwner->getStartTimestamp()); 859 } 860 861 if (timestampUs > mMaxTimeStampUs) { 862 mMaxTimeStampUs = timestampUs; 863 } 864 865 // Our timestamp is in ms. 866 info.timestamp = (timestampUs + 500) / 1000; 867 mSampleInfos.push_back(info); 868 if (mSampleInfos.size() > 2) { 869 if (lastDuration != info.timestamp - lastTimestamp) { 870 SttsTableEntry sttsEntry(sampleCount, lastDuration); 871 mSttsTableEntries.push_back(sttsEntry); 872 sampleCount = 1; 873 } else { 874 ++sampleCount; 875 } 876 } 877 if (mSamplesHaveSameSize) { 878 if (mSampleInfos.size() >= 2 && previousSampleSize != info.size) { 879 mSamplesHaveSameSize = false; 880 } 881 previousSampleSize = info.size; 882 } 883 lastDuration = info.timestamp - lastTimestamp; 884 lastTimestamp = info.timestamp; 885 886//////////////////////////////////////////////////////////////////////////////// 887 // Make a deep copy of the MediaBuffer less Metadata 888 MediaBuffer *copy = new MediaBuffer(buffer->range_length()); 889 memcpy(copy->data(), (uint8_t *)buffer->data() + buffer->range_offset(), 890 buffer->range_length()); 891 copy->set_range(0, buffer->range_length()); 892 893 mChunkSamples.push_back(copy); 894 if (interleaveDurationUs == 0) { 895 StscTableEntry stscEntry(++nChunks, 1, 1); 896 mStscTableEntries.push_back(stscEntry); 897 writeOneChunk(is_avc); 898 } else { 899 if (chunkTimestampUs == 0) { 900 chunkTimestampUs = timestampUs; 901 } else { 902 if (timestampUs - chunkTimestampUs > interleaveDurationUs) { 903 ++nChunks; 904 if (nChunks == 1 || // First chunk 905 (--(mStscTableEntries.end()))->samplesPerChunk != 906 mChunkSamples.size()) { 907 StscTableEntry stscEntry(nChunks, 908 mChunkSamples.size(), 1); 909 mStscTableEntries.push_back(stscEntry); 910 } 911 writeOneChunk(is_avc); 912 chunkTimestampUs = timestampUs; 913 } 914 } 915 } 916 917 int32_t isSync = false; 918 if (buffer->meta_data()->findInt32(kKeyIsSyncFrame, &isSync) && 919 isSync != 0) { 920 mStssTableEntries.push_back(mSampleInfos.size()); 921 } 922 923 buffer->release(); 924 buffer = NULL; 925 } 926 927 CHECK(!mSampleInfos.empty()); 928 929 // Last chunk 930 if (!mChunkSamples.empty()) { 931 ++nChunks; 932 StscTableEntry stscEntry(nChunks, mChunkSamples.size(), 1); 933 mStscTableEntries.push_back(stscEntry); 934 writeOneChunk(is_avc); 935 } 936 937 // We don't really know how long the last frame lasts, since 938 // there is no frame time after it, just repeat the previous 939 // frame's duration. 940 if (mSampleInfos.size() == 1) { 941 lastDuration = 0; // A single sample's duration 942 } else { 943 ++sampleCount; // Count for the last sample 944 } 945 SttsTableEntry sttsEntry(sampleCount, lastDuration); 946 mSttsTableEntries.push_back(sttsEntry); 947 mReachedEOS = true; 948 LOGI("Received total/0-length (%d/%d) buffers and encoded %d frames", 949 count, nZeroLengthFrames, mSampleInfos.size()); 950} 951 952void MPEG4Writer::Track::writeOneChunk(bool isAvc) { 953 mOwner->lock(); 954 for (List<MediaBuffer *>::iterator it = mChunkSamples.begin(); 955 it != mChunkSamples.end(); ++it) { 956 off_t offset = isAvc? mOwner->addLengthPrefixedSample_l(*it) 957 : mOwner->addSample_l(*it); 958 if (it == mChunkSamples.begin()) { 959 mChunkOffsets.push_back(offset); 960 } 961 } 962 mOwner->unlock(); 963 while (!mChunkSamples.empty()) { 964 List<MediaBuffer *>::iterator it = mChunkSamples.begin(); 965 (*it)->release(); 966 (*it) = NULL; 967 mChunkSamples.erase(it); 968 } 969 mChunkSamples.clear(); 970} 971 972int64_t MPEG4Writer::Track::getDurationUs() const { 973 return mMaxTimeStampUs; 974} 975 976int64_t MPEG4Writer::Track::getEstimatedTrackSizeBytes() const { 977 return mEstimatedTrackSizeBytes; 978} 979 980void MPEG4Writer::Track::writeTrackHeader(int32_t trackID) { 981 const char *mime; 982 bool success = mMeta->findCString(kKeyMIMEType, &mime); 983 CHECK(success); 984 985 bool is_audio = !strncasecmp(mime, "audio/", 6); 986 987 time_t now = time(NULL); 988 989 mOwner->beginBox("trak"); 990 991 mOwner->beginBox("tkhd"); 992 mOwner->writeInt32(0); // version=0, flags=0 993 mOwner->writeInt32(now); // creation time 994 mOwner->writeInt32(now); // modification time 995 mOwner->writeInt32(trackID); 996 mOwner->writeInt32(0); // reserved 997 mOwner->writeInt32(getDurationUs() / 1000); 998 mOwner->writeInt32(0); // reserved 999 mOwner->writeInt32(0); // reserved 1000 mOwner->writeInt16(0); // layer 1001 mOwner->writeInt16(0); // alternate group 1002 mOwner->writeInt16(is_audio ? 0x100 : 0); // volume 1003 mOwner->writeInt16(0); // reserved 1004 1005 mOwner->writeInt32(0x10000); // matrix 1006 mOwner->writeInt32(0); 1007 mOwner->writeInt32(0); 1008 mOwner->writeInt32(0); 1009 mOwner->writeInt32(0x10000); 1010 mOwner->writeInt32(0); 1011 mOwner->writeInt32(0); 1012 mOwner->writeInt32(0); 1013 mOwner->writeInt32(0x40000000); 1014 1015 if (is_audio) { 1016 mOwner->writeInt32(0); 1017 mOwner->writeInt32(0); 1018 } else { 1019 int32_t width, height; 1020 bool success = mMeta->findInt32(kKeyWidth, &width); 1021 success = success && mMeta->findInt32(kKeyHeight, &height); 1022 CHECK(success); 1023 1024 mOwner->writeInt32(width << 16); // 32-bit fixed-point value 1025 mOwner->writeInt32(height << 16); // 32-bit fixed-point value 1026 } 1027 mOwner->endBox(); // tkhd 1028 1029 if (mStartTimestampUs != 0) { 1030 mOwner->beginBox("edts"); 1031 mOwner->writeInt32(0); // version=0, flags=0 1032 mOwner->beginBox("elst"); 1033 mOwner->writeInt32(0); // version=0, flags=0 1034 mOwner->writeInt32(1); // a single entry 1035 mOwner->writeInt32(mStartTimestampUs / 1000); // edit duration 1036 mOwner->writeInt32(0); // edit media starting time 1037 mOwner->writeInt32(1); // x1 rate 1038 mOwner->endBox(); 1039 mOwner->endBox(); 1040 } 1041 1042 mOwner->beginBox("mdia"); 1043 1044 mOwner->beginBox("mdhd"); 1045 mOwner->writeInt32(0); // version=0, flags=0 1046 mOwner->writeInt32(now); // creation time 1047 mOwner->writeInt32(now); // modification time 1048 mOwner->writeInt32(1000); // timescale 1049 mOwner->writeInt32(getDurationUs() / 1000); 1050 mOwner->writeInt16(0); // language code XXX 1051 mOwner->writeInt16(0); // predefined 1052 mOwner->endBox(); 1053 1054 mOwner->beginBox("hdlr"); 1055 mOwner->writeInt32(0); // version=0, flags=0 1056 mOwner->writeInt32(0); // component type: should be mhlr 1057 mOwner->writeFourcc(is_audio ? "soun" : "vide"); // component subtype 1058 mOwner->writeInt32(0); // reserved 1059 mOwner->writeInt32(0); // reserved 1060 mOwner->writeInt32(0); // reserved 1061 mOwner->writeCString("SoundHandler"); // name 1062 mOwner->endBox(); 1063 1064 mOwner->beginBox("minf"); 1065 if (is_audio) { 1066 mOwner->beginBox("smhd"); 1067 mOwner->writeInt32(0); // version=0, flags=0 1068 mOwner->writeInt16(0); // balance 1069 mOwner->writeInt16(0); // reserved 1070 mOwner->endBox(); 1071 } else { 1072 mOwner->beginBox("vmhd"); 1073 mOwner->writeInt32(0x00000001); // version=0, flags=1 1074 mOwner->writeInt16(0); // graphics mode 1075 mOwner->writeInt16(0); // opcolor 1076 mOwner->writeInt16(0); 1077 mOwner->writeInt16(0); 1078 mOwner->endBox(); 1079 } 1080 1081 mOwner->beginBox("dinf"); 1082 mOwner->beginBox("dref"); 1083 mOwner->writeInt32(0); // version=0, flags=0 1084 mOwner->writeInt32(1); 1085 mOwner->beginBox("url "); 1086 mOwner->writeInt32(1); // version=0, flags=1 1087 mOwner->endBox(); // url 1088 mOwner->endBox(); // dref 1089 mOwner->endBox(); // dinf 1090 1091 mOwner->endBox(); // minf 1092 1093 mOwner->beginBox("stbl"); 1094 1095 mOwner->beginBox("stsd"); 1096 mOwner->writeInt32(0); // version=0, flags=0 1097 mOwner->writeInt32(1); // entry count 1098 if (is_audio) { 1099 const char *fourcc = NULL; 1100 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) { 1101 fourcc = "samr"; 1102 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) { 1103 fourcc = "sawb"; 1104 } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) { 1105 fourcc = "mp4a"; 1106 } else { 1107 LOGE("Unknown mime type '%s'.", mime); 1108 CHECK(!"should not be here, unknown mime type."); 1109 } 1110 1111 mOwner->beginBox(fourcc); // audio format 1112 mOwner->writeInt32(0); // reserved 1113 mOwner->writeInt16(0); // reserved 1114 mOwner->writeInt16(0x1); // data ref index 1115 mOwner->writeInt32(0); // reserved 1116 mOwner->writeInt32(0); // reserved 1117 int32_t nChannels; 1118 CHECK_EQ(true, mMeta->findInt32(kKeyChannelCount, &nChannels)); 1119 mOwner->writeInt16(nChannels); // channel count 1120 mOwner->writeInt16(16); // sample size 1121 mOwner->writeInt16(0); // predefined 1122 mOwner->writeInt16(0); // reserved 1123 1124 int32_t samplerate; 1125 bool success = mMeta->findInt32(kKeySampleRate, &samplerate); 1126 CHECK(success); 1127 1128 mOwner->writeInt32(samplerate << 16); 1129 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) { 1130 mOwner->beginBox("esds"); 1131 1132 mOwner->writeInt32(0); // version=0, flags=0 1133 mOwner->writeInt8(0x03); // ES_DescrTag 1134 mOwner->writeInt8(23 + mCodecSpecificDataSize); 1135 mOwner->writeInt16(0x0000);// ES_ID 1136 mOwner->writeInt8(0x00); 1137 1138 mOwner->writeInt8(0x04); // DecoderConfigDescrTag 1139 mOwner->writeInt8(15 + mCodecSpecificDataSize); 1140 mOwner->writeInt8(0x40); // objectTypeIndication ISO/IEC 14492-2 1141 mOwner->writeInt8(0x15); // streamType AudioStream 1142 1143 mOwner->writeInt16(0x03); // XXX 1144 mOwner->writeInt8(0x00); // buffer size 24-bit 1145 mOwner->writeInt32(96000); // max bit rate 1146 mOwner->writeInt32(96000); // avg bit rate 1147 1148 mOwner->writeInt8(0x05); // DecoderSpecificInfoTag 1149 mOwner->writeInt8(mCodecSpecificDataSize); 1150 mOwner->write(mCodecSpecificData, mCodecSpecificDataSize); 1151 1152 static const uint8_t kData2[] = { 1153 0x06, // SLConfigDescriptorTag 1154 0x01, 1155 0x02 1156 }; 1157 mOwner->write(kData2, sizeof(kData2)); 1158 1159 mOwner->endBox(); // esds 1160 } 1161 mOwner->endBox(); 1162 } else { 1163 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 1164 mOwner->beginBox("mp4v"); 1165 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 1166 mOwner->beginBox("s263"); 1167 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 1168 mOwner->beginBox("avc1"); 1169 } else { 1170 LOGE("Unknown mime type '%s'.", mime); 1171 CHECK(!"should not be here, unknown mime type."); 1172 } 1173 1174 mOwner->writeInt32(0); // reserved 1175 mOwner->writeInt16(0); // reserved 1176 mOwner->writeInt16(0); // data ref index 1177 mOwner->writeInt16(0); // predefined 1178 mOwner->writeInt16(0); // reserved 1179 mOwner->writeInt32(0); // predefined 1180 mOwner->writeInt32(0); // predefined 1181 mOwner->writeInt32(0); // predefined 1182 1183 int32_t width, height; 1184 bool success = mMeta->findInt32(kKeyWidth, &width); 1185 success = success && mMeta->findInt32(kKeyHeight, &height); 1186 CHECK(success); 1187 1188 mOwner->writeInt16(width); 1189 mOwner->writeInt16(height); 1190 mOwner->writeInt32(0x480000); // horiz resolution 1191 mOwner->writeInt32(0x480000); // vert resolution 1192 mOwner->writeInt32(0); // reserved 1193 mOwner->writeInt16(1); // frame count 1194 mOwner->write(" ", 32); 1195 mOwner->writeInt16(0x18); // depth 1196 mOwner->writeInt16(-1); // predefined 1197 1198 CHECK(23 + mCodecSpecificDataSize < 128); 1199 1200 if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 1201 mOwner->beginBox("esds"); 1202 1203 mOwner->writeInt32(0); // version=0, flags=0 1204 1205 mOwner->writeInt8(0x03); // ES_DescrTag 1206 mOwner->writeInt8(23 + mCodecSpecificDataSize); 1207 mOwner->writeInt16(0x0000); // ES_ID 1208 mOwner->writeInt8(0x1f); 1209 1210 mOwner->writeInt8(0x04); // DecoderConfigDescrTag 1211 mOwner->writeInt8(15 + mCodecSpecificDataSize); 1212 mOwner->writeInt8(0x20); // objectTypeIndication ISO/IEC 14492-2 1213 mOwner->writeInt8(0x11); // streamType VisualStream 1214 1215 static const uint8_t kData[] = { 1216 0x01, 0x77, 0x00, 1217 0x00, 0x03, 0xe8, 0x00, 1218 0x00, 0x03, 0xe8, 0x00 1219 }; 1220 mOwner->write(kData, sizeof(kData)); 1221 1222 mOwner->writeInt8(0x05); // DecoderSpecificInfoTag 1223 1224 mOwner->writeInt8(mCodecSpecificDataSize); 1225 mOwner->write(mCodecSpecificData, mCodecSpecificDataSize); 1226 1227 static const uint8_t kData2[] = { 1228 0x06, // SLConfigDescriptorTag 1229 0x01, 1230 0x02 1231 }; 1232 mOwner->write(kData2, sizeof(kData2)); 1233 1234 mOwner->endBox(); // esds 1235 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 1236 mOwner->beginBox("d263"); 1237 1238 mOwner->writeInt32(0); // vendor 1239 mOwner->writeInt8(0); // decoder version 1240 mOwner->writeInt8(10); // level: 10 1241 mOwner->writeInt8(0); // profile: 0 1242 1243 mOwner->endBox(); // d263 1244 } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 1245 mOwner->beginBox("avcC"); 1246 mOwner->write(mCodecSpecificData, mCodecSpecificDataSize); 1247 mOwner->endBox(); // avcC 1248 } 1249 1250 mOwner->endBox(); // mp4v, s263 or avc1 1251 } 1252 mOwner->endBox(); // stsd 1253 1254 mOwner->beginBox("stts"); 1255 mOwner->writeInt32(0); // version=0, flags=0 1256 mOwner->writeInt32(mSttsTableEntries.size()); 1257 for (List<SttsTableEntry>::iterator it = mSttsTableEntries.begin(); 1258 it != mSttsTableEntries.end(); ++it) { 1259 mOwner->writeInt32(it->sampleCount); 1260 mOwner->writeInt32(it->sampleDuration); 1261 } 1262 mOwner->endBox(); // stts 1263 1264 if (!is_audio) { 1265 mOwner->beginBox("stss"); 1266 mOwner->writeInt32(0); // version=0, flags=0 1267 mOwner->writeInt32(mStssTableEntries.size()); // number of sync frames 1268 for (List<int32_t>::iterator it = mStssTableEntries.begin(); 1269 it != mStssTableEntries.end(); ++it) { 1270 mOwner->writeInt32(*it); 1271 } 1272 mOwner->endBox(); // stss 1273 } 1274 1275 mOwner->beginBox("stsz"); 1276 mOwner->writeInt32(0); // version=0, flags=0 1277 if (mSamplesHaveSameSize) { 1278 List<SampleInfo>::iterator it = mSampleInfos.begin(); 1279 mOwner->writeInt32(it->size); // default sample size 1280 } else { 1281 mOwner->writeInt32(0); 1282 } 1283 mOwner->writeInt32(mSampleInfos.size()); 1284 if (!mSamplesHaveSameSize) { 1285 for (List<SampleInfo>::iterator it = mSampleInfos.begin(); 1286 it != mSampleInfos.end(); ++it) { 1287 mOwner->writeInt32((*it).size); 1288 } 1289 } 1290 mOwner->endBox(); // stsz 1291 1292 mOwner->beginBox("stsc"); 1293 mOwner->writeInt32(0); // version=0, flags=0 1294 mOwner->writeInt32(mStscTableEntries.size()); 1295 for (List<StscTableEntry>::iterator it = mStscTableEntries.begin(); 1296 it != mStscTableEntries.end(); ++it) { 1297 mOwner->writeInt32(it->firstChunk); 1298 mOwner->writeInt32(it->samplesPerChunk); 1299 mOwner->writeInt32(it->sampleDescriptionId); 1300 } 1301 mOwner->endBox(); // stsc 1302 1303 mOwner->beginBox("co64"); 1304 mOwner->writeInt32(0); // version=0, flags=0 1305 mOwner->writeInt32(mChunkOffsets.size()); 1306 for (List<off_t>::iterator it = mChunkOffsets.begin(); 1307 it != mChunkOffsets.end(); ++it) { 1308 mOwner->writeInt64((*it)); 1309 } 1310 mOwner->endBox(); // co64 1311 1312 mOwner->endBox(); // stbl 1313 mOwner->endBox(); // mdia 1314 mOwner->endBox(); // trak 1315} 1316 1317} // namespace android 1318