Skip to content

Commit

Permalink
MatroskaExtractor naming cleanup
Browse files Browse the repository at this point in the history
- Change sampleHasReferenceBlock to a block reading variable, which is
  what it is (the distinction didn't matter previously, but will do so
  when we add lacing support in full blocks because there wont be a 1:1
  relationship any more.
- Move sampleRead to be a reading state variable.
- Stop abbreviating "additional"

Issue: #3026
PiperOrigin-RevId: 284000937
  • Loading branch information
ojw28 committed Dec 6, 2019
1 parent bf495de commit dd60bac
Showing 1 changed file with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public class MatroskaExtractor implements Extractor {
* BlockAddID value for ITU T.35 metadata in a VP9 track. See also
* https://www.webmproject.org/docs/container/.
*/
private static final int BLOCK_ADD_ID_VP9_ITU_T_35 = 4;
private static final int BLOCK_ADDITIONAL_ID_VP9_ITU_T_35 = 4;

private static final int LACING_NONE = 0;
private static final int LACING_XIPH = 1;
Expand Down Expand Up @@ -332,7 +332,7 @@ public class MatroskaExtractor implements Extractor {
private final ParsableByteArray subtitleSample;
private final ParsableByteArray encryptionInitializationVector;
private final ParsableByteArray encryptionSubsampleData;
private final ParsableByteArray blockAddData;
private final ParsableByteArray blockAdditionalData;
private ByteBuffer encryptionSubsampleDataBuffer;

private long segmentContentSize;
Expand Down Expand Up @@ -360,6 +360,9 @@ public class MatroskaExtractor implements Extractor {
private LongArray cueClusterPositions;
private boolean seenClusterPositionForCurrentCuePoint;

// Reading state.
private boolean haveOutputSample;

// Block reading state.
private int blockState;
private long blockTimeUs;
Expand All @@ -371,20 +374,19 @@ public class MatroskaExtractor implements Extractor {
private int blockTrackNumberLength;
@C.BufferFlags
private int blockFlags;
private int blockAddId;
private int blockAdditionalId;
private boolean blockHasReferenceBlock;

// Sample reading state.
private int sampleBytesRead;
private int sampleBytesWritten;
private int sampleCurrentNalBytesRemaining;
private boolean sampleEncodingHandled;
private boolean sampleSignalByteRead;
private boolean sampleInitializationVectorRead;
private boolean samplePartitionCountRead;
private byte sampleSignalByte;
private int samplePartitionCount;
private int sampleCurrentNalBytesRemaining;
private int sampleBytesWritten;
private boolean sampleRead;
private boolean sampleSeenReferenceBlock;
private byte sampleSignalByte;
private boolean sampleInitializationVectorRead;

// Extractor outputs.
private ExtractorOutput extractorOutput;
Expand Down Expand Up @@ -412,7 +414,7 @@ public MatroskaExtractor(@Flags int flags) {
subtitleSample = new ParsableByteArray();
encryptionInitializationVector = new ParsableByteArray(ENCRYPTION_IV_SIZE);
encryptionSubsampleData = new ParsableByteArray();
blockAddData = new ParsableByteArray();
blockAdditionalData = new ParsableByteArray();
}

@Override
Expand Down Expand Up @@ -446,9 +448,9 @@ public final void release() {
@Override
public final int read(ExtractorInput input, PositionHolder seekPosition)
throws IOException, InterruptedException {
sampleRead = false;
haveOutputSample = false;
boolean continueReading = true;
while (continueReading && !sampleRead) {
while (continueReading && !haveOutputSample) {
continueReading = reader.read(input);
if (continueReading && maybeSeekForCues(seekPosition, input.getPosition())) {
return Extractor.RESULT_SEEK;
Expand Down Expand Up @@ -623,7 +625,7 @@ protected void startMasterElement(int id, long contentPosition, long contentSize
}
break;
case ID_BLOCK_GROUP:
sampleSeenReferenceBlock = false;
blockHasReferenceBlock = false;
break;
case ID_CONTENT_ENCODING:
// TODO: check and fail if more than one content encoding is present.
Expand Down Expand Up @@ -681,7 +683,7 @@ protected void endMasterElement(int id) throws ParserException {
return;
}
// If the ReferenceBlock element was not found for this sample, then it is a keyframe.
if (!sampleSeenReferenceBlock) {
if (!blockHasReferenceBlock) {
blockFlags |= C.BUFFER_FLAG_KEY_FRAME;
}
commitSampleToOutput(tracks.get(blockTrackNumber), blockTimeUs);
Expand Down Expand Up @@ -793,7 +795,7 @@ protected void integerElement(int id, long value) throws ParserException {
currentTrack.audioBitDepth = (int) value;
break;
case ID_REFERENCE_BLOCK:
sampleSeenReferenceBlock = true;
blockHasReferenceBlock = true;
break;
case ID_CONTENT_ENCODING_ORDER:
// This extractor only supports one ContentEncoding element and hence the order has to be 0.
Expand Down Expand Up @@ -935,7 +937,7 @@ protected void integerElement(int id, long value) throws ParserException {
}
break;
case ID_BLOCK_ADD_ID:
blockAddId = (int) value;
blockAdditionalId = (int) value;
break;
default:
break;
Expand Down Expand Up @@ -1199,19 +1201,21 @@ protected void binaryElement(int id, int contentSize, ExtractorInput input)
if (blockState != BLOCK_STATE_DATA) {
return;
}
handleBlockAdditionalData(tracks.get(blockTrackNumber), blockAddId, input, contentSize);
handleBlockAdditionalData(
tracks.get(blockTrackNumber), blockAdditionalId, input, contentSize);
break;
default:
throw new ParserException("Unexpected id: " + id);
}
}

protected void handleBlockAdditionalData(
Track track, int blockAddId, ExtractorInput input, int contentSize)
Track track, int blockAdditionalId, ExtractorInput input, int contentSize)
throws IOException, InterruptedException {
if (blockAddId == BLOCK_ADD_ID_VP9_ITU_T_35 && CODEC_ID_VP9.equals(track.codecId)) {
blockAddData.reset(contentSize);
input.readFully(blockAddData.data, 0, contentSize);
if (blockAdditionalId == BLOCK_ADDITIONAL_ID_VP9_ITU_T_35
&& CODEC_ID_VP9.equals(track.codecId)) {
blockAdditionalData.reset(contentSize);
input.readFully(blockAdditionalData.data, 0, contentSize);
} else {
// Unhandled block additional data.
input.skipFully(contentSize);
Expand All @@ -1236,13 +1240,13 @@ private void commitSampleToOutput(Track track, long timeUs) {

if ((blockFlags & C.BUFFER_FLAG_HAS_SUPPLEMENTAL_DATA) != 0) {
// Append supplemental data.
int size = blockAddData.limit();
track.output.sampleData(blockAddData, size);
sampleBytesWritten += size;
int blockAdditionalSize = blockAdditionalData.limit();
track.output.sampleData(blockAdditionalData, blockAdditionalSize);
sampleBytesWritten += blockAdditionalSize;
}
track.output.sampleMetadata(timeUs, blockFlags, sampleBytesWritten, 0, track.cryptoData);
}
sampleRead = true;
haveOutputSample = true;
resetSample();
}

Expand Down Expand Up @@ -1375,7 +1379,7 @@ private void writeSampleData(ExtractorInput input, Track track, int size)

if (track.maxBlockAdditionId > 0) {
blockFlags |= C.BUFFER_FLAG_HAS_SUPPLEMENTAL_DATA;
blockAddData.reset();
blockAdditionalData.reset();
// If there is supplemental data, the structure of the sample data is:
// sample size (4 bytes) || sample data || supplemental data
scratch.reset(/* limit= */ 4);
Expand Down

0 comments on commit dd60bac

Please sign in to comment.