Further fix H262 segmentation

Issue: #2891

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=163951910
This commit is contained in:
olly 2017-08-02 03:07:56 -07:00 committed by Oliver Woodman
parent e604daaa09
commit cad25e5a4d
2 changed files with 21 additions and 27 deletions

View File

@ -28,7 +28,7 @@ track 256:
data = length 22, hash CE183139 data = length 22, hash CE183139
sample count = 2 sample count = 2
sample 0: sample 0:
time = 0 time = 33366
flags = 1 flags = 1
data = length 20711, hash 34341E8 data = length 20711, hash 34341E8
sample 1: sample 1:

View File

@ -51,17 +51,17 @@ public final class H262Reader implements ElementaryStreamReader {
// State that should be reset on seek. // State that should be reset on seek.
private final boolean[] prefixFlags; private final boolean[] prefixFlags;
private final CsdBuffer csdBuffer; private final CsdBuffer csdBuffer;
private boolean foundPicture;
private long totalBytesWritten; private long totalBytesWritten;
private boolean startedFirstSample;
// Per packet state that gets reset at the start of each packet. // Per packet state that gets reset at the start of each packet.
private long pesTimeUs; private long pesTimeUs;
private boolean pesPtsUsAvailable;
// Per sample state that gets reset at the start of each frame. // Per sample state that gets reset at the start of each sample.
private boolean isKeyframe;
private long samplePosition; private long samplePosition;
private long sampleTimeUs; private long sampleTimeUs;
private boolean sampleIsKeyframe;
private boolean sampleHasPicture;
public H262Reader() { public H262Reader() {
prefixFlags = new boolean[4]; prefixFlags = new boolean[4];
@ -72,10 +72,8 @@ public final class H262Reader implements ElementaryStreamReader {
public void seek() { public void seek() {
NalUnitUtil.clearPrefixFlags(prefixFlags); NalUnitUtil.clearPrefixFlags(prefixFlags);
csdBuffer.reset(); csdBuffer.reset();
pesPtsUsAvailable = false;
foundPicture = false;
samplePosition = C.POSITION_UNSET;
totalBytesWritten = 0; totalBytesWritten = 0;
startedFirstSample = false;
} }
@Override @Override
@ -87,10 +85,7 @@ public final class H262Reader implements ElementaryStreamReader {
@Override @Override
public void packetStarted(long pesTimeUs, boolean dataAlignmentIndicator) { public void packetStarted(long pesTimeUs, boolean dataAlignmentIndicator) {
pesPtsUsAvailable = pesTimeUs != C.TIME_UNSET; this.pesTimeUs = pesTimeUs;
if (pesPtsUsAvailable) {
this.pesTimeUs = pesTimeUs;
}
} }
@Override @Override
@ -136,27 +131,26 @@ public final class H262Reader implements ElementaryStreamReader {
} }
} }
if (hasOutputFormat if (startCodeValue == START_PICTURE || startCodeValue == START_SEQUENCE_HEADER) {
&& (startCodeValue == START_PICTURE || startCodeValue == START_SEQUENCE_HEADER)) {
int bytesWrittenPastStartCode = limit - startCodeOffset; int bytesWrittenPastStartCode = limit - startCodeOffset;
boolean resetSample = samplePosition == C.POSITION_UNSET; if (startedFirstSample && sampleHasPicture && hasOutputFormat) {
if (foundPicture) { // Output the sample.
@C.BufferFlags int flags = isKeyframe ? C.BUFFER_FLAG_KEY_FRAME : 0; @C.BufferFlags int flags = sampleIsKeyframe ? C.BUFFER_FLAG_KEY_FRAME : 0;
int size = (int) (totalBytesWritten - samplePosition) - bytesWrittenPastStartCode; int size = (int) (totalBytesWritten - samplePosition) - bytesWrittenPastStartCode;
output.sampleMetadata(sampleTimeUs, flags, size, bytesWrittenPastStartCode, null); output.sampleMetadata(sampleTimeUs, flags, size, bytesWrittenPastStartCode, null);
isKeyframe = false;
resetSample = true;
} }
foundPicture = startCodeValue == START_PICTURE; if (!startedFirstSample || sampleHasPicture) {
if (resetSample) { // Start the next sample.
samplePosition = totalBytesWritten - bytesWrittenPastStartCode; samplePosition = totalBytesWritten - bytesWrittenPastStartCode;
sampleTimeUs = (pesPtsUsAvailable ? pesTimeUs : sampleTimeUs + frameDurationUs); sampleTimeUs = pesTimeUs != C.TIME_UNSET ? pesTimeUs
pesPtsUsAvailable = false; : (startedFirstSample ? (sampleTimeUs + frameDurationUs) : 0);
sampleIsKeyframe = false;
pesTimeUs = C.TIME_UNSET;
startedFirstSample = true;
} }
} sampleHasPicture = startCodeValue == START_PICTURE;
} else if (startCodeValue == START_GROUP) {
if (hasOutputFormat && startCodeValue == START_GROUP) { sampleIsKeyframe = true;
isKeyframe = true;
} }
offset = startCodeOffset + 3; offset = startCodeOffset + 3;