MPEG4Writer.cpp revision f0ce2fb0c7bf3a414279e5aba61105f3d9025c0e
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, &timestampUs));
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    if (mSampleInfos.empty()) {
928        mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_STOP_PREMATURELY, 0);
929    }
930
931    // Last chunk
932    if (!mChunkSamples.empty()) {
933        ++nChunks;
934        StscTableEntry stscEntry(nChunks, mChunkSamples.size(), 1);
935        mStscTableEntries.push_back(stscEntry);
936        writeOneChunk(is_avc);
937    }
938
939    // We don't really know how long the last frame lasts, since
940    // there is no frame time after it, just repeat the previous
941    // frame's duration.
942    if (mSampleInfos.size() == 1) {
943        lastDuration = 0;  // A single sample's duration
944    } else {
945        ++sampleCount;  // Count for the last sample
946    }
947    SttsTableEntry sttsEntry(sampleCount, lastDuration);
948    mSttsTableEntries.push_back(sttsEntry);
949    mReachedEOS = true;
950    LOGI("Received total/0-length (%d/%d) buffers and encoded %d frames",
951            count, nZeroLengthFrames, mSampleInfos.size());
952}
953
954void MPEG4Writer::Track::writeOneChunk(bool isAvc) {
955    mOwner->lock();
956    for (List<MediaBuffer *>::iterator it = mChunkSamples.begin();
957         it != mChunkSamples.end(); ++it) {
958        off_t offset = isAvc? mOwner->addLengthPrefixedSample_l(*it)
959                            : mOwner->addSample_l(*it);
960        if (it == mChunkSamples.begin()) {
961            mChunkOffsets.push_back(offset);
962        }
963    }
964    mOwner->unlock();
965    while (!mChunkSamples.empty()) {
966        List<MediaBuffer *>::iterator it = mChunkSamples.begin();
967        (*it)->release();
968        (*it) = NULL;
969        mChunkSamples.erase(it);
970    }
971    mChunkSamples.clear();
972}
973
974int64_t MPEG4Writer::Track::getDurationUs() const {
975    return mMaxTimeStampUs;
976}
977
978int64_t MPEG4Writer::Track::getEstimatedTrackSizeBytes() const {
979    return mEstimatedTrackSizeBytes;
980}
981
982void MPEG4Writer::Track::writeTrackHeader(int32_t trackID) {
983    const char *mime;
984    bool success = mMeta->findCString(kKeyMIMEType, &mime);
985    CHECK(success);
986
987    bool is_audio = !strncasecmp(mime, "audio/", 6);
988
989    time_t now = time(NULL);
990
991    mOwner->beginBox("trak");
992
993      mOwner->beginBox("tkhd");
994        mOwner->writeInt32(0);             // version=0, flags=0
995        mOwner->writeInt32(now);           // creation time
996        mOwner->writeInt32(now);           // modification time
997        mOwner->writeInt32(trackID);
998        mOwner->writeInt32(0);             // reserved
999        mOwner->writeInt32(getDurationUs() / 1000);
1000        mOwner->writeInt32(0);             // reserved
1001        mOwner->writeInt32(0);             // reserved
1002        mOwner->writeInt16(0);             // layer
1003        mOwner->writeInt16(0);             // alternate group
1004        mOwner->writeInt16(is_audio ? 0x100 : 0);  // volume
1005        mOwner->writeInt16(0);             // reserved
1006
1007        mOwner->writeInt32(0x10000);       // matrix
1008        mOwner->writeInt32(0);
1009        mOwner->writeInt32(0);
1010        mOwner->writeInt32(0);
1011        mOwner->writeInt32(0x10000);
1012        mOwner->writeInt32(0);
1013        mOwner->writeInt32(0);
1014        mOwner->writeInt32(0);
1015        mOwner->writeInt32(0x40000000);
1016
1017        if (is_audio) {
1018            mOwner->writeInt32(0);
1019            mOwner->writeInt32(0);
1020        } else {
1021            int32_t width, height;
1022            bool success = mMeta->findInt32(kKeyWidth, &width);
1023            success = success && mMeta->findInt32(kKeyHeight, &height);
1024            CHECK(success);
1025
1026            mOwner->writeInt32(width << 16);   // 32-bit fixed-point value
1027            mOwner->writeInt32(height << 16);  // 32-bit fixed-point value
1028        }
1029      mOwner->endBox();  // tkhd
1030
1031      if (mStartTimestampUs != 0) {
1032        mOwner->beginBox("edts");
1033          mOwner->writeInt32(0);             // version=0, flags=0
1034          mOwner->beginBox("elst");
1035            mOwner->writeInt32(0);           // version=0, flags=0
1036            mOwner->writeInt32(1);           // a single entry
1037            mOwner->writeInt32(mStartTimestampUs / 1000);  // edit duration
1038            mOwner->writeInt32(0);           // edit media starting time
1039            mOwner->writeInt32(1);           // x1 rate
1040          mOwner->endBox();
1041        mOwner->endBox();
1042      }
1043
1044      mOwner->beginBox("mdia");
1045
1046        mOwner->beginBox("mdhd");
1047          mOwner->writeInt32(0);             // version=0, flags=0
1048          mOwner->writeInt32(now);           // creation time
1049          mOwner->writeInt32(now);           // modification time
1050          mOwner->writeInt32(1000);          // timescale
1051          mOwner->writeInt32(getDurationUs() / 1000);
1052          mOwner->writeInt16(0);             // language code XXX
1053          mOwner->writeInt16(0);             // predefined
1054        mOwner->endBox();
1055
1056        mOwner->beginBox("hdlr");
1057          mOwner->writeInt32(0);             // version=0, flags=0
1058          mOwner->writeInt32(0);             // component type: should be mhlr
1059          mOwner->writeFourcc(is_audio ? "soun" : "vide");  // component subtype
1060          mOwner->writeInt32(0);             // reserved
1061          mOwner->writeInt32(0);             // reserved
1062          mOwner->writeInt32(0);             // reserved
1063          mOwner->writeCString("SoundHandler");          // name
1064        mOwner->endBox();
1065
1066        mOwner->beginBox("minf");
1067          if (is_audio) {
1068              mOwner->beginBox("smhd");
1069              mOwner->writeInt32(0);           // version=0, flags=0
1070              mOwner->writeInt16(0);           // balance
1071              mOwner->writeInt16(0);           // reserved
1072              mOwner->endBox();
1073          } else {
1074              mOwner->beginBox("vmhd");
1075              mOwner->writeInt32(0x00000001);  // version=0, flags=1
1076              mOwner->writeInt16(0);           // graphics mode
1077              mOwner->writeInt16(0);           // opcolor
1078              mOwner->writeInt16(0);
1079              mOwner->writeInt16(0);
1080              mOwner->endBox();
1081          }
1082
1083          mOwner->beginBox("dinf");
1084            mOwner->beginBox("dref");
1085              mOwner->writeInt32(0);  // version=0, flags=0
1086              mOwner->writeInt32(1);
1087              mOwner->beginBox("url ");
1088                mOwner->writeInt32(1);  // version=0, flags=1
1089              mOwner->endBox();  // url
1090            mOwner->endBox();  // dref
1091          mOwner->endBox();  // dinf
1092
1093       mOwner->endBox();  // minf
1094
1095        mOwner->beginBox("stbl");
1096
1097          mOwner->beginBox("stsd");
1098            mOwner->writeInt32(0);               // version=0, flags=0
1099            mOwner->writeInt32(1);               // entry count
1100            if (is_audio) {
1101                const char *fourcc = NULL;
1102                if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
1103                    fourcc = "samr";
1104                } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
1105                    fourcc = "sawb";
1106                } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
1107                    fourcc = "mp4a";
1108                } else {
1109                    LOGE("Unknown mime type '%s'.", mime);
1110                    CHECK(!"should not be here, unknown mime type.");
1111                }
1112
1113                mOwner->beginBox(fourcc);          // audio format
1114                  mOwner->writeInt32(0);           // reserved
1115                  mOwner->writeInt16(0);           // reserved
1116                  mOwner->writeInt16(0x1);         // data ref index
1117                  mOwner->writeInt32(0);           // reserved
1118                  mOwner->writeInt32(0);           // reserved
1119                  int32_t nChannels;
1120                  CHECK_EQ(true, mMeta->findInt32(kKeyChannelCount, &nChannels));
1121                  mOwner->writeInt16(nChannels);   // channel count
1122                  mOwner->writeInt16(16);          // sample size
1123                  mOwner->writeInt16(0);           // predefined
1124                  mOwner->writeInt16(0);           // reserved
1125
1126                  int32_t samplerate;
1127                  bool success = mMeta->findInt32(kKeySampleRate, &samplerate);
1128                  CHECK(success);
1129
1130                  mOwner->writeInt32(samplerate << 16);
1131                  if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
1132                    mOwner->beginBox("esds");
1133
1134                        mOwner->writeInt32(0);     // version=0, flags=0
1135                        mOwner->writeInt8(0x03);   // ES_DescrTag
1136                        mOwner->writeInt8(23 + mCodecSpecificDataSize);
1137                        mOwner->writeInt16(0x0000);// ES_ID
1138                        mOwner->writeInt8(0x00);
1139
1140                        mOwner->writeInt8(0x04);   // DecoderConfigDescrTag
1141                        mOwner->writeInt8(15 + mCodecSpecificDataSize);
1142                        mOwner->writeInt8(0x40);   // objectTypeIndication ISO/IEC 14492-2
1143                        mOwner->writeInt8(0x15);   // streamType AudioStream
1144
1145                        mOwner->writeInt16(0x03);  // XXX
1146                        mOwner->writeInt8(0x00);   // buffer size 24-bit
1147                        mOwner->writeInt32(96000); // max bit rate
1148                        mOwner->writeInt32(96000); // avg bit rate
1149
1150                        mOwner->writeInt8(0x05);   // DecoderSpecificInfoTag
1151                        mOwner->writeInt8(mCodecSpecificDataSize);
1152                        mOwner->write(mCodecSpecificData, mCodecSpecificDataSize);
1153
1154                        static const uint8_t kData2[] = {
1155                            0x06,  // SLConfigDescriptorTag
1156                            0x01,
1157                            0x02
1158                        };
1159                        mOwner->write(kData2, sizeof(kData2));
1160
1161                    mOwner->endBox();  // esds
1162                  }
1163                mOwner->endBox();
1164            } else {
1165                if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
1166                    mOwner->beginBox("mp4v");
1167                } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
1168                    mOwner->beginBox("s263");
1169                } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
1170                    mOwner->beginBox("avc1");
1171                } else {
1172                    LOGE("Unknown mime type '%s'.", mime);
1173                    CHECK(!"should not be here, unknown mime type.");
1174                }
1175
1176                  mOwner->writeInt32(0);           // reserved
1177                  mOwner->writeInt16(0);           // reserved
1178                  mOwner->writeInt16(0);           // data ref index
1179                  mOwner->writeInt16(0);           // predefined
1180                  mOwner->writeInt16(0);           // reserved
1181                  mOwner->writeInt32(0);           // predefined
1182                  mOwner->writeInt32(0);           // predefined
1183                  mOwner->writeInt32(0);           // predefined
1184
1185                  int32_t width, height;
1186                  bool success = mMeta->findInt32(kKeyWidth, &width);
1187                  success = success && mMeta->findInt32(kKeyHeight, &height);
1188                  CHECK(success);
1189
1190                  mOwner->writeInt16(width);
1191                  mOwner->writeInt16(height);
1192                  mOwner->writeInt32(0x480000);    // horiz resolution
1193                  mOwner->writeInt32(0x480000);    // vert resolution
1194                  mOwner->writeInt32(0);           // reserved
1195                  mOwner->writeInt16(1);           // frame count
1196                  mOwner->write("                                ", 32);
1197                  mOwner->writeInt16(0x18);        // depth
1198                  mOwner->writeInt16(-1);          // predefined
1199
1200                  CHECK(23 + mCodecSpecificDataSize < 128);
1201
1202                  if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
1203                      mOwner->beginBox("esds");
1204
1205                        mOwner->writeInt32(0);           // version=0, flags=0
1206
1207                        mOwner->writeInt8(0x03);  // ES_DescrTag
1208                        mOwner->writeInt8(23 + mCodecSpecificDataSize);
1209                        mOwner->writeInt16(0x0000);  // ES_ID
1210                        mOwner->writeInt8(0x1f);
1211
1212                        mOwner->writeInt8(0x04);  // DecoderConfigDescrTag
1213                        mOwner->writeInt8(15 + mCodecSpecificDataSize);
1214                        mOwner->writeInt8(0x20);  // objectTypeIndication ISO/IEC 14492-2
1215                        mOwner->writeInt8(0x11);  // streamType VisualStream
1216
1217                        static const uint8_t kData[] = {
1218                            0x01, 0x77, 0x00,
1219                            0x00, 0x03, 0xe8, 0x00,
1220                            0x00, 0x03, 0xe8, 0x00
1221                        };
1222                        mOwner->write(kData, sizeof(kData));
1223
1224                        mOwner->writeInt8(0x05);  // DecoderSpecificInfoTag
1225
1226                        mOwner->writeInt8(mCodecSpecificDataSize);
1227                        mOwner->write(mCodecSpecificData, mCodecSpecificDataSize);
1228
1229                        static const uint8_t kData2[] = {
1230                            0x06,  // SLConfigDescriptorTag
1231                            0x01,
1232                            0x02
1233                        };
1234                        mOwner->write(kData2, sizeof(kData2));
1235
1236                      mOwner->endBox();  // esds
1237                  } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
1238                      mOwner->beginBox("d263");
1239
1240                          mOwner->writeInt32(0);  // vendor
1241                          mOwner->writeInt8(0);   // decoder version
1242                          mOwner->writeInt8(10);  // level: 10
1243                          mOwner->writeInt8(0);   // profile: 0
1244
1245                      mOwner->endBox();  // d263
1246                  } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
1247                      mOwner->beginBox("avcC");
1248                        mOwner->write(mCodecSpecificData, mCodecSpecificDataSize);
1249                      mOwner->endBox();  // avcC
1250                  }
1251
1252                mOwner->endBox();  // mp4v, s263 or avc1
1253            }
1254          mOwner->endBox();  // stsd
1255
1256          mOwner->beginBox("stts");
1257            mOwner->writeInt32(0);  // version=0, flags=0
1258            mOwner->writeInt32(mSttsTableEntries.size());
1259            for (List<SttsTableEntry>::iterator it = mSttsTableEntries.begin();
1260                 it != mSttsTableEntries.end(); ++it) {
1261                mOwner->writeInt32(it->sampleCount);
1262                mOwner->writeInt32(it->sampleDuration);
1263            }
1264          mOwner->endBox();  // stts
1265
1266          if (!is_audio) {
1267            mOwner->beginBox("stss");
1268              mOwner->writeInt32(0);  // version=0, flags=0
1269              mOwner->writeInt32(mStssTableEntries.size());  // number of sync frames
1270              for (List<int32_t>::iterator it = mStssTableEntries.begin();
1271                   it != mStssTableEntries.end(); ++it) {
1272                  mOwner->writeInt32(*it);
1273              }
1274            mOwner->endBox();  // stss
1275          }
1276
1277          mOwner->beginBox("stsz");
1278            mOwner->writeInt32(0);  // version=0, flags=0
1279            if (mSamplesHaveSameSize) {
1280                List<SampleInfo>::iterator it = mSampleInfos.begin();
1281                mOwner->writeInt32(it->size);  // default sample size
1282            } else {
1283                mOwner->writeInt32(0);
1284            }
1285            mOwner->writeInt32(mSampleInfos.size());
1286            if (!mSamplesHaveSameSize) {
1287                for (List<SampleInfo>::iterator it = mSampleInfos.begin();
1288                     it != mSampleInfos.end(); ++it) {
1289                    mOwner->writeInt32((*it).size);
1290                }
1291            }
1292          mOwner->endBox();  // stsz
1293
1294          mOwner->beginBox("stsc");
1295            mOwner->writeInt32(0);  // version=0, flags=0
1296            mOwner->writeInt32(mStscTableEntries.size());
1297            for (List<StscTableEntry>::iterator it = mStscTableEntries.begin();
1298                 it != mStscTableEntries.end(); ++it) {
1299                mOwner->writeInt32(it->firstChunk);
1300                mOwner->writeInt32(it->samplesPerChunk);
1301                mOwner->writeInt32(it->sampleDescriptionId);
1302            }
1303          mOwner->endBox();  // stsc
1304
1305          mOwner->beginBox("co64");
1306            mOwner->writeInt32(0);  // version=0, flags=0
1307            mOwner->writeInt32(mChunkOffsets.size());
1308            for (List<off_t>::iterator it = mChunkOffsets.begin();
1309                 it != mChunkOffsets.end(); ++it) {
1310                mOwner->writeInt64((*it));
1311            }
1312          mOwner->endBox();  // co64
1313
1314        mOwner->endBox();  // stbl
1315      mOwner->endBox();  // mdia
1316    mOwner->endBox();  // trak
1317}
1318
1319}  // namespace android
1320