Commit bdc7633f by Oliver Woodman

Cleanup in TsExtractor.

parent 84307eff
...@@ -235,12 +235,15 @@ public final class TsExtractor implements Extractor { ...@@ -235,12 +235,15 @@ public final class TsExtractor implements Extractor {
*/ */
private class PmtReader extends TsPayloadReader { private class PmtReader extends TsPayloadReader {
private final ParsableByteArray data;
private final ParsableBitArray pmtScratch; private final ParsableBitArray pmtScratch;
private final ParsableByteArray sectionData;
private int sectionLength;
private int sectionBytesRead;
public PmtReader() { public PmtReader() {
data = new ParsableByteArray();
pmtScratch = new ParsableBitArray(new byte[5]); pmtScratch = new ParsableBitArray(new byte[5]);
sectionData = new ParsableByteArray();
} }
@Override @Override
...@@ -249,47 +252,47 @@ public final class TsExtractor implements Extractor { ...@@ -249,47 +252,47 @@ public final class TsExtractor implements Extractor {
} }
@Override @Override
public void consume(ParsableByteArray dataPart, boolean payloadUnitStartIndicator, public void consume(ParsableByteArray data, boolean payloadUnitStartIndicator,
ExtractorOutput output) { ExtractorOutput output) {
if (payloadUnitStartIndicator) { if (payloadUnitStartIndicator) {
// Skip pointer. // Skip pointer.
int pointerField = dataPart.readUnsignedByte(); int pointerField = data.readUnsignedByte();
dataPart.skipBytes(pointerField); data.skipBytes(pointerField);
// Note: see ISO/IEC 13818-1, section 2.4.4.8 for detailed information on the format of // Note: see ISO/IEC 13818-1, section 2.4.4.8 for detailed information on the format of
// the header. // the header.
dataPart.readBytes(pmtScratch, 3); data.readBytes(pmtScratch, 3);
pmtScratch.skipBits(12); // table_id (8), section_syntax_indicator (1), '0' (1), reserved (2) pmtScratch.skipBits(12); // table_id (8), section_syntax_indicator (1), 0 (1), reserved (2)
int sectionLength = pmtScratch.readBits(12); sectionLength = pmtScratch.readBits(12);
if (data.capacity() == sectionLength) { if (sectionData.capacity() < sectionLength) {
data.reset(); sectionData.reset(new byte[sectionLength], sectionLength);
} else { } else {
data.reset(new byte[sectionLength], 0); sectionData.reset();
sectionData.setLimit(sectionLength);
} }
} }
// read part of the section from single TS packet (dataPart) int bytesToRead = Math.min(data.bytesLeft(), sectionLength - sectionBytesRead);
int partLength = Math.min(dataPart.bytesLeft(), data.capacity()-data.limit()); data.readBytes(sectionData.data, sectionBytesRead, bytesToRead);
dataPart.readBytes(data.data, data.limit(), partLength); sectionBytesRead += bytesToRead;
data.setLimit(data.limit()+partLength); if (sectionBytesRead < sectionLength) {
if (data.limit() != data.capacity()) { // section is not complete yet // Not yet fully read.
return; return;
} }
int sectionLength = data.capacity();
// program_number (16), reserved (2), version_number (5), current_next_indicator (1), // program_number (16), reserved (2), version_number (5), current_next_indicator (1),
// section_number (8), last_section_number (8), reserved (3), PCR_PID (13) // section_number (8), last_section_number (8), reserved (3), PCR_PID (13)
// Skip the rest of the PMT header. // Skip the rest of the PMT header.
data.skipBytes(7); sectionData.skipBytes(7);
data.readBytes(pmtScratch, 2); // Read program_info_length.
sectionData.readBytes(pmtScratch, 2);
pmtScratch.skipBits(4); pmtScratch.skipBits(4);
int programInfoLength = pmtScratch.readBits(12); int programInfoLength = pmtScratch.readBits(12);
// Skip the descriptors. // Skip the descriptors.
data.skipBytes(programInfoLength); sectionData.skipBytes(programInfoLength);
if (id3Reader == null) { if (id3Reader == null) {
// Setup an ID3 track regardless of whether there's a corresponding entry, in case one // Setup an ID3 track regardless of whether there's a corresponding entry, in case one
...@@ -300,7 +303,7 @@ public final class TsExtractor implements Extractor { ...@@ -300,7 +303,7 @@ public final class TsExtractor implements Extractor {
int remainingEntriesLength = sectionLength - 9 /* Length of fields before descriptors */ int remainingEntriesLength = sectionLength - 9 /* Length of fields before descriptors */
- programInfoLength - 4 /* CRC length */; - programInfoLength - 4 /* CRC length */;
while (remainingEntriesLength > 0) { while (remainingEntriesLength > 0) {
data.readBytes(pmtScratch, 5); sectionData.readBytes(pmtScratch, 5);
int streamType = pmtScratch.readBits(8); int streamType = pmtScratch.readBits(8);
pmtScratch.skipBits(3); // reserved pmtScratch.skipBits(3); // reserved
int elementaryPid = pmtScratch.readBits(13); int elementaryPid = pmtScratch.readBits(13);
...@@ -308,9 +311,9 @@ public final class TsExtractor implements Extractor { ...@@ -308,9 +311,9 @@ public final class TsExtractor implements Extractor {
int esInfoLength = pmtScratch.readBits(12); // ES_info_length int esInfoLength = pmtScratch.readBits(12); // ES_info_length
if (streamType == 0x06) { if (streamType == 0x06) {
// Read descriptors in PES packets containing private data. // Read descriptors in PES packets containing private data.
streamType = readPrivateDataStreamType(data, esInfoLength); streamType = readPrivateDataStreamType(sectionData, esInfoLength);
} else { } else {
data.skipBytes(esInfoLength); sectionData.skipBytes(esInfoLength);
} }
remainingEntriesLength -= esInfoLength + 5; remainingEntriesLength -= esInfoLength + 5;
if (streamTypes.get(streamType)) { if (streamTypes.get(streamType)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment