Skip to content

Commit 4f0106c

Browse files
James DongAndroid (Google) Code Review
authored andcommitted
Merge "Finish up B frame support in MPEG4Writer"
2 parents 468a971 + 9975229 commit 4f0106c

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

media/libstagefright/MPEG4Writer.cpp

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
#include <pthread.h>
2424
#include <sys/prctl.h>
2525

26+
#include <media/stagefright/foundation/ADebug.h>
2627
#include <media/stagefright/MPEG4Writer.h>
2728
#include <media/stagefright/MediaBuffer.h>
2829
#include <media/stagefright/MetaData.h>
29-
#include <media/stagefright/MediaDebug.h>
3030
#include <media/stagefright/MediaDefs.h>
3131
#include <media/stagefright/MediaErrors.h>
3232
#include <media/stagefright/MediaSource.h>
@@ -141,7 +141,7 @@ class MPEG4Writer::Track {
141141
: sampleCount(count), sampleDuration(timescaledDur) {}
142142

143143
uint32_t sampleCount;
144-
int32_t sampleDuration; // time scale based
144+
uint32_t sampleDuration; // time scale based
145145
};
146146
size_t mNumCttsTableEntries;
147147
List<CttsTableEntry> mCttsTableEntries;
@@ -478,7 +478,7 @@ status_t MPEG4Writer::start(MetaData *param) {
478478
!param->findInt32(kKeyTimeScale, &mTimeScale)) {
479479
mTimeScale = 1000;
480480
}
481-
CHECK(mTimeScale > 0);
481+
CHECK_GT(mTimeScale, 0);
482482
ALOGV("movie time scale: %d", mTimeScale);
483483

484484
mStreamableFile = true;
@@ -497,7 +497,7 @@ status_t MPEG4Writer::start(MetaData *param) {
497497
}
498498
mEstimatedMoovBoxSize = estimateMoovBoxSize(bitRate);
499499
}
500-
CHECK(mEstimatedMoovBoxSize >= 8);
500+
CHECK_GE(mEstimatedMoovBoxSize, 8);
501501
lseek64(mFd, mFreeBoxOffset, SEEK_SET);
502502
writeInt32(mEstimatedMoovBoxSize);
503503
write("free", 4);
@@ -691,7 +691,7 @@ status_t MPEG4Writer::reset() {
691691

692692
mWriteMoovBoxToMemory = false;
693693
if (mStreamableFile) {
694-
CHECK(mMoovBoxBufferOffset + 8 <= mEstimatedMoovBoxSize);
694+
CHECK_LE(mMoovBoxBufferOffset + 8, mEstimatedMoovBoxSize);
695695

696696
// Moov box
697697
lseek64(mFd, mFreeBoxOffset, SEEK_SET);
@@ -863,7 +863,7 @@ off64_t MPEG4Writer::addLengthPrefixedSample_l(MediaBuffer *buffer) {
863863

864864
mOffset += length + 4;
865865
} else {
866-
CHECK(length < 65536);
866+
CHECK_LT(length, 65536);
867867

868868
uint8_t x = length >> 8;
869869
::write(mFd, &x, 1);
@@ -1092,7 +1092,7 @@ bool MPEG4Writer::reachedEOS() {
10921092

10931093
void MPEG4Writer::setStartTimestampUs(int64_t timeUs) {
10941094
ALOGI("setStartTimestampUs: %lld", timeUs);
1095-
CHECK(timeUs >= 0);
1095+
CHECK_GE(timeUs, 0ll);
10961096
Mutex::Autolock autoLock(mLock);
10971097
if (mStartTimestampUs < 0 || mStartTimestampUs > timeUs) {
10981098
mStartTimestampUs = timeUs;
@@ -1222,7 +1222,7 @@ void MPEG4Writer::Track::setTimeScale() {
12221222
mTimeScale = timeScale;
12231223
}
12241224

1225-
CHECK(mTimeScale > 0);
1225+
CHECK_GT(mTimeScale, 0);
12261226
}
12271227

12281228
void MPEG4Writer::Track::getCodecSpecificDataFromInputFormatIfPossible() {
@@ -1303,7 +1303,7 @@ void MPEG4Writer::bufferChunk(const Chunk& chunk) {
13031303
}
13041304
}
13051305

1306-
CHECK("Received a chunk for a unknown track" == 0);
1306+
CHECK(!"Received a chunk for a unknown track");
13071307
}
13081308

13091309
void MPEG4Writer::writeChunkToFile(Chunk* chunk) {
@@ -1846,7 +1846,7 @@ status_t MPEG4Writer::Track::threadEntry() {
18461846
int64_t cttsOffsetTimeUs = 0;
18471847
int64_t currCttsOffsetTimeTicks = 0; // Timescale based ticks
18481848
int64_t lastCttsOffsetTimeTicks = -1; // Timescale based ticks
1849-
int32_t cttsSampleCount = 1; // Sample count in the current ctts table entry
1849+
int32_t cttsSampleCount = 0; // Sample count in the current ctts table entry
18501850

18511851
if (mIsAudio) {
18521852
prctl(PR_SET_NAME, (unsigned long)"AudioTrackEncoding", 0, 0, 0);
@@ -1889,7 +1889,7 @@ status_t MPEG4Writer::Track::threadEntry() {
18891889
(const uint8_t *)buffer->data()
18901890
+ buffer->range_offset(),
18911891
buffer->range_length());
1892-
CHECK_EQ(OK, err);
1892+
CHECK_EQ((status_t)OK, err);
18931893
} else if (mIsMPEG4) {
18941894
mCodecSpecificDataSize = buffer->range_length();
18951895
mCodecSpecificData = malloc(mCodecSpecificDataSize);
@@ -1955,15 +1955,15 @@ status_t MPEG4Writer::Track::threadEntry() {
19551955

19561956
if (mResumed) {
19571957
int64_t durExcludingEarlierPausesUs = timestampUs - previousPausedDurationUs;
1958-
CHECK(durExcludingEarlierPausesUs >= 0);
1958+
CHECK_GE(durExcludingEarlierPausesUs, 0ll);
19591959
int64_t pausedDurationUs = durExcludingEarlierPausesUs - mTrackDurationUs;
1960-
CHECK(pausedDurationUs >= lastDurationUs);
1960+
CHECK_GE(pausedDurationUs, lastDurationUs);
19611961
previousPausedDurationUs += pausedDurationUs - lastDurationUs;
19621962
mResumed = false;
19631963
}
19641964

19651965
timestampUs -= previousPausedDurationUs;
1966-
CHECK(timestampUs >= 0);
1966+
CHECK_GE(timestampUs, 0ll);
19671967
if (!mIsAudio) {
19681968
/*
19691969
* Composition time: timestampUs
@@ -1975,24 +1975,31 @@ status_t MPEG4Writer::Track::threadEntry() {
19751975
decodingTimeUs -= previousPausedDurationUs;
19761976
cttsOffsetTimeUs =
19771977
timestampUs + kMaxCttsOffsetTimeUs - decodingTimeUs;
1978-
CHECK(cttsOffsetTimeUs >= 0);
1978+
CHECK_GE(cttsOffsetTimeUs, 0ll);
19791979
timestampUs = decodingTimeUs;
19801980
ALOGV("decoding time: %lld and ctts offset time: %lld",
19811981
timestampUs, cttsOffsetTimeUs);
19821982

19831983
// Update ctts box table if necessary
19841984
currCttsOffsetTimeTicks =
19851985
(cttsOffsetTimeUs * mTimeScale + 500000LL) / 1000000LL;
1986-
CHECK(currCttsOffsetTimeTicks <= 0x7FFFFFFFLL);
1987-
#if 0
1988-
// FIXME:
1989-
// Optimize to reduce the number of ctts table entries.
1990-
// Also, make sure that the very first ctts table entry contains
1991-
// only a single sample.
1992-
#else
1993-
addOneCttsTableEntry(1, currCttsOffsetTimeTicks);
1994-
#endif
1995-
lastCttsOffsetTimeTicks = currCttsOffsetTimeTicks;
1986+
CHECK_LE(currCttsOffsetTimeTicks, 0x0FFFFFFFFLL);
1987+
if (mNumSamples == 0) {
1988+
// Force the first ctts table entry to have one single entry
1989+
// so that we can do adjustment for the initial track start
1990+
// time offset easily in writeCttsBox().
1991+
lastCttsOffsetTimeTicks = currCttsOffsetTimeTicks;
1992+
addOneCttsTableEntry(1, currCttsOffsetTimeTicks);
1993+
cttsSampleCount = 0; // No sample in ctts box is pending
1994+
} else {
1995+
if (currCttsOffsetTimeTicks != lastCttsOffsetTimeTicks) {
1996+
addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks);
1997+
lastCttsOffsetTimeTicks = currCttsOffsetTimeTicks;
1998+
cttsSampleCount = 1; // One sample in ctts box is pending
1999+
} else {
2000+
++cttsSampleCount;
2001+
}
2002+
}
19962003

19972004
// Update ctts time offset range
19982005
if (mNumSamples == 0) {
@@ -2014,7 +2021,7 @@ status_t MPEG4Writer::Track::threadEntry() {
20142021
}
20152022
}
20162023

2017-
CHECK(timestampUs >= 0);
2024+
CHECK_GE(timestampUs, 0ll);
20182025
ALOGV("%s media time stamp: %lld and previous paused duration %lld",
20192026
mIsAudio? "Audio": "Video", timestampUs, previousPausedDurationUs);
20202027
if (timestampUs > mTrackDurationUs) {
@@ -2029,7 +2036,7 @@ status_t MPEG4Writer::Track::threadEntry() {
20292036
currDurationTicks =
20302037
((timestampUs * mTimeScale + 500000LL) / 1000000LL -
20312038
(lastTimestampUs * mTimeScale + 500000LL) / 1000000LL);
2032-
CHECK(currDurationTicks >= 0);
2039+
CHECK_GE(currDurationTicks, 0ll);
20332040

20342041
mSampleSizes.push_back(sampleSize);
20352042
++mNumSamples;
@@ -2127,7 +2134,6 @@ status_t MPEG4Writer::Track::threadEntry() {
21272134
lastDurationTicks = 0;
21282135
} else {
21292136
++sampleCount; // Count for the last sample
2130-
++cttsSampleCount;
21312137
}
21322138

21332139
if (mNumSamples <= 2) {
@@ -2139,6 +2145,14 @@ status_t MPEG4Writer::Track::threadEntry() {
21392145
addOneSttsTableEntry(sampleCount, lastDurationTicks);
21402146
}
21412147

2148+
// The last ctts box may not have been written yet, and this
2149+
// is to make sure that we write out the last ctts box.
2150+
if (currCttsOffsetTimeTicks == lastCttsOffsetTimeTicks) {
2151+
if (cttsSampleCount > 0) {
2152+
addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks);
2153+
}
2154+
}
2155+
21422156
mTrackDurationUs += lastDurationUs;
21432157
mReachedEOS = true;
21442158

@@ -2404,7 +2418,7 @@ void MPEG4Writer::Track::writeVideoFourCCBox() {
24042418
mOwner->writeInt16(0x18); // depth
24052419
mOwner->writeInt16(-1); // predefined
24062420

2407-
CHECK(23 + mCodecSpecificDataSize < 128);
2421+
CHECK_LT(23 + mCodecSpecificDataSize, 128);
24082422

24092423
if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
24102424
writeMp4vEsdsBox();
@@ -2463,10 +2477,10 @@ void MPEG4Writer::Track::writeAudioFourCCBox() {
24632477
void MPEG4Writer::Track::writeMp4aEsdsBox() {
24642478
mOwner->beginBox("esds");
24652479
CHECK(mCodecSpecificData);
2466-
CHECK(mCodecSpecificDataSize > 0);
2480+
CHECK_GT(mCodecSpecificDataSize, 0);
24672481

24682482
// Make sure all sizes encode to a single byte.
2469-
CHECK(mCodecSpecificDataSize + 23 < 128);
2483+
CHECK_LT(mCodecSpecificDataSize + 23, 128);
24702484

24712485
mOwner->writeInt32(0); // version=0, flags=0
24722486
mOwner->writeInt8(0x03); // ES_DescrTag
@@ -2500,7 +2514,7 @@ void MPEG4Writer::Track::writeMp4aEsdsBox() {
25002514

25012515
void MPEG4Writer::Track::writeMp4vEsdsBox() {
25022516
CHECK(mCodecSpecificData);
2503-
CHECK(mCodecSpecificDataSize > 0);
2517+
CHECK_GT(mCodecSpecificDataSize, 0);
25042518
mOwner->beginBox("esds");
25052519

25062520
mOwner->writeInt32(0); // version=0, flags=0
@@ -2660,7 +2674,7 @@ void MPEG4Writer::Track::writeDinfBox() {
26602674

26612675
void MPEG4Writer::Track::writeAvccBox() {
26622676
CHECK(mCodecSpecificData);
2663-
CHECK(mCodecSpecificDataSize >= 5);
2677+
CHECK_GE(mCodecSpecificDataSize, 5);
26642678

26652679
// Patch avcc's lengthSize field to match the number
26662680
// of bytes we use to indicate the size of a nal unit.
@@ -2692,7 +2706,7 @@ int32_t MPEG4Writer::Track::getStartTimeOffsetScaledTime() const {
26922706
int64_t trackStartTimeOffsetUs = 0;
26932707
int64_t moovStartTimeUs = mOwner->getStartTimestampUs();
26942708
if (mStartTimestampUs != moovStartTimeUs) {
2695-
CHECK(mStartTimestampUs > moovStartTimeUs);
2709+
CHECK_GT(mStartTimestampUs, moovStartTimeUs);
26962710
trackStartTimeOffsetUs = mStartTimestampUs - moovStartTimeUs;
26972711
}
26982712
return (trackStartTimeOffsetUs * mTimeScale + 500000LL) / 1000000LL;
@@ -2715,7 +2729,7 @@ void MPEG4Writer::Track::writeSttsBox() {
27152729
mOwner->writeInt32(it->sampleDuration);
27162730
totalCount += it->sampleCount;
27172731
}
2718-
CHECK(totalCount == mNumSamples);
2732+
CHECK_EQ(totalCount, mNumSamples);
27192733
mOwner->endBox(); // stts
27202734
}
27212735

@@ -2758,7 +2772,7 @@ void MPEG4Writer::Track::writeCttsBox() {
27582772
mOwner->writeInt32(it->sampleDuration - mMinCttsOffsetTimeUs);
27592773
totalCount += it->sampleCount;
27602774
}
2761-
CHECK(totalCount == mNumSamples);
2775+
CHECK_EQ(totalCount, mNumSamples);
27622776
mOwner->endBox(); // ctts
27632777
}
27642778

0 commit comments

Comments
 (0)