Commit e6bafec4 by andrewlewis Committed by Oliver Woodman

Deduplicate ID3 header constants

PiperOrigin-RevId: 259479785
parent 3a53543a
...@@ -53,7 +53,7 @@ public final class Id3Peeker { ...@@ -53,7 +53,7 @@ public final class Id3Peeker {
Metadata metadata = null; Metadata metadata = null;
while (true) { while (true) {
try { try {
input.peekFully(scratch.data, 0, Id3Decoder.ID3_HEADER_LENGTH); input.peekFully(scratch.data, /* offset= */ 0, Id3Decoder.ID3_HEADER_LENGTH);
} catch (EOFException e) { } catch (EOFException e) {
// If input has less than ID3_HEADER_LENGTH, ignore the rest. // If input has less than ID3_HEADER_LENGTH, ignore the rest.
break; break;
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package com.google.android.exoplayer2.extractor.ts; package com.google.android.exoplayer2.extractor.ts;
import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR; import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR;
import static com.google.android.exoplayer2.metadata.id3.Id3Decoder.ID3_HEADER_LENGTH;
import static com.google.android.exoplayer2.metadata.id3.Id3Decoder.ID3_TAG;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.audio.Ac3Util; import com.google.android.exoplayer2.audio.Ac3Util;
...@@ -44,7 +46,6 @@ public final class Ac3Extractor implements Extractor { ...@@ -44,7 +46,6 @@ public final class Ac3Extractor implements Extractor {
private static final int MAX_SNIFF_BYTES = 8 * 1024; private static final int MAX_SNIFF_BYTES = 8 * 1024;
private static final int AC3_SYNC_WORD = 0x0B77; private static final int AC3_SYNC_WORD = 0x0B77;
private static final int MAX_SYNC_FRAME_SIZE = 2786; private static final int MAX_SYNC_FRAME_SIZE = 2786;
private static final int ID3_TAG = 0x00494433;
private final Ac3Reader reader; private final Ac3Reader reader;
private final ParsableByteArray sampleData; private final ParsableByteArray sampleData;
...@@ -62,10 +63,10 @@ public final class Ac3Extractor implements Extractor { ...@@ -62,10 +63,10 @@ public final class Ac3Extractor implements Extractor {
@Override @Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException { public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
// Skip any ID3 headers. // Skip any ID3 headers.
ParsableByteArray scratch = new ParsableByteArray(10); ParsableByteArray scratch = new ParsableByteArray(ID3_HEADER_LENGTH);
int startPosition = 0; int startPosition = 0;
while (true) { while (true) {
input.peekFully(scratch.data, 0, 10); input.peekFully(scratch.data, /* offset= */ 0, ID3_HEADER_LENGTH);
scratch.setPosition(0); scratch.setPosition(0);
if (scratch.readUnsignedInt24() != ID3_TAG) { if (scratch.readUnsignedInt24() != ID3_TAG) {
break; break;
......
...@@ -18,6 +18,8 @@ package com.google.android.exoplayer2.extractor.ts; ...@@ -18,6 +18,8 @@ package com.google.android.exoplayer2.extractor.ts;
import static com.google.android.exoplayer2.audio.Ac4Util.AC40_SYNCWORD; import static com.google.android.exoplayer2.audio.Ac4Util.AC40_SYNCWORD;
import static com.google.android.exoplayer2.audio.Ac4Util.AC41_SYNCWORD; import static com.google.android.exoplayer2.audio.Ac4Util.AC41_SYNCWORD;
import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR; import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR;
import static com.google.android.exoplayer2.metadata.id3.Id3Decoder.ID3_HEADER_LENGTH;
import static com.google.android.exoplayer2.metadata.id3.Id3Decoder.ID3_TAG;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.audio.Ac4Util; import com.google.android.exoplayer2.audio.Ac4Util;
...@@ -52,8 +54,6 @@ public final class Ac4Extractor implements Extractor { ...@@ -52,8 +54,6 @@ public final class Ac4Extractor implements Extractor {
/** The size of the frame header, in bytes. */ /** The size of the frame header, in bytes. */
private static final int FRAME_HEADER_SIZE = 7; private static final int FRAME_HEADER_SIZE = 7;
private static final int ID3_TAG = 0x00494433;
private final Ac4Reader reader; private final Ac4Reader reader;
private final ParsableByteArray sampleData; private final ParsableByteArray sampleData;
...@@ -70,10 +70,10 @@ public final class Ac4Extractor implements Extractor { ...@@ -70,10 +70,10 @@ public final class Ac4Extractor implements Extractor {
@Override @Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException { public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
// Skip any ID3 headers. // Skip any ID3 headers.
ParsableByteArray scratch = new ParsableByteArray(10); ParsableByteArray scratch = new ParsableByteArray(ID3_HEADER_LENGTH);
int startPosition = 0; int startPosition = 0;
while (true) { while (true) {
input.peekFully(scratch.data, /* offset= */ 0, /* length= */ 10); input.peekFully(scratch.data, /* offset= */ 0, ID3_HEADER_LENGTH);
scratch.setPosition(0); scratch.setPosition(0);
if (scratch.readUnsignedInt24() != ID3_TAG) { if (scratch.readUnsignedInt24() != ID3_TAG) {
break; break;
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package com.google.android.exoplayer2.extractor.ts; package com.google.android.exoplayer2.extractor.ts;
import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR; import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR;
import static com.google.android.exoplayer2.metadata.id3.Id3Decoder.ID3_HEADER_LENGTH;
import static com.google.android.exoplayer2.metadata.id3.Id3Decoder.ID3_TAG;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -65,7 +67,6 @@ public final class AdtsExtractor implements Extractor { ...@@ -65,7 +67,6 @@ public final class AdtsExtractor implements Extractor {
public static final int FLAG_ENABLE_CONSTANT_BITRATE_SEEKING = 1; public static final int FLAG_ENABLE_CONSTANT_BITRATE_SEEKING = 1;
private static final int MAX_PACKET_SIZE = 2 * 1024; private static final int MAX_PACKET_SIZE = 2 * 1024;
private static final int ID3_TAG = 0x00494433;
/** /**
* The maximum number of bytes to search when sniffing, excluding the header, before giving up. * The maximum number of bytes to search when sniffing, excluding the header, before giving up.
* Frame sizes are represented by 13-bit fields, so expect a valid frame in the first 8192 bytes. * Frame sizes are represented by 13-bit fields, so expect a valid frame in the first 8192 bytes.
...@@ -109,7 +110,8 @@ public final class AdtsExtractor implements Extractor { ...@@ -109,7 +110,8 @@ public final class AdtsExtractor implements Extractor {
packetBuffer = new ParsableByteArray(MAX_PACKET_SIZE); packetBuffer = new ParsableByteArray(MAX_PACKET_SIZE);
averageFrameSize = C.LENGTH_UNSET; averageFrameSize = C.LENGTH_UNSET;
firstFramePosition = C.POSITION_UNSET; firstFramePosition = C.POSITION_UNSET;
scratch = new ParsableByteArray(10); // Allocate scratch space for an ID3 header. The same buffer is also used to read 4 byte values.
scratch = new ParsableByteArray(ID3_HEADER_LENGTH);
scratchBits = new ParsableBitArray(scratch.data); scratchBits = new ParsableBitArray(scratch.data);
} }
...@@ -209,7 +211,7 @@ public final class AdtsExtractor implements Extractor { ...@@ -209,7 +211,7 @@ public final class AdtsExtractor implements Extractor {
private int peekId3Header(ExtractorInput input) throws IOException, InterruptedException { private int peekId3Header(ExtractorInput input) throws IOException, InterruptedException {
int firstFramePosition = 0; int firstFramePosition = 0;
while (true) { while (true) {
input.peekFully(scratch.data, 0, 10); input.peekFully(scratch.data, /* offset= */ 0, ID3_HEADER_LENGTH);
scratch.setPosition(0); scratch.setPosition(0);
if (scratch.readUnsignedInt24() != ID3_TAG) { if (scratch.readUnsignedInt24() != ID3_TAG) {
break; break;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.extractor.ts; package com.google.android.exoplayer2.extractor.ts;
import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR; import static com.google.android.exoplayer2.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR;
import static com.google.android.exoplayer2.metadata.id3.Id3Decoder.ID3_HEADER_LENGTH;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
...@@ -33,8 +34,6 @@ public final class Id3Reader implements ElementaryStreamReader { ...@@ -33,8 +34,6 @@ public final class Id3Reader implements ElementaryStreamReader {
private static final String TAG = "Id3Reader"; private static final String TAG = "Id3Reader";
private static final int ID3_HEADER_SIZE = 10;
private final ParsableByteArray id3Header; private final ParsableByteArray id3Header;
private TrackOutput output; private TrackOutput output;
...@@ -48,7 +47,7 @@ public final class Id3Reader implements ElementaryStreamReader { ...@@ -48,7 +47,7 @@ public final class Id3Reader implements ElementaryStreamReader {
private int sampleBytesRead; private int sampleBytesRead;
public Id3Reader() { public Id3Reader() {
id3Header = new ParsableByteArray(ID3_HEADER_SIZE); id3Header = new ParsableByteArray(ID3_HEADER_LENGTH);
} }
@Override @Override
...@@ -81,12 +80,12 @@ public final class Id3Reader implements ElementaryStreamReader { ...@@ -81,12 +80,12 @@ public final class Id3Reader implements ElementaryStreamReader {
return; return;
} }
int bytesAvailable = data.bytesLeft(); int bytesAvailable = data.bytesLeft();
if (sampleBytesRead < ID3_HEADER_SIZE) { if (sampleBytesRead < ID3_HEADER_LENGTH) {
// We're still reading the ID3 header. // We're still reading the ID3 header.
int headerBytesAvailable = Math.min(bytesAvailable, ID3_HEADER_SIZE - sampleBytesRead); int headerBytesAvailable = Math.min(bytesAvailable, ID3_HEADER_LENGTH - sampleBytesRead);
System.arraycopy(data.data, data.getPosition(), id3Header.data, sampleBytesRead, System.arraycopy(data.data, data.getPosition(), id3Header.data, sampleBytesRead,
headerBytesAvailable); headerBytesAvailable);
if (sampleBytesRead + headerBytesAvailable == ID3_HEADER_SIZE) { if (sampleBytesRead + headerBytesAvailable == ID3_HEADER_LENGTH) {
// We've finished reading the ID3 header. Extract the sample size. // We've finished reading the ID3 header. Extract the sample size.
id3Header.setPosition(0); id3Header.setPosition(0);
if ('I' != id3Header.readUnsignedByte() || 'D' != id3Header.readUnsignedByte() if ('I' != id3Header.readUnsignedByte() || 'D' != id3Header.readUnsignedByte()
...@@ -96,7 +95,7 @@ public final class Id3Reader implements ElementaryStreamReader { ...@@ -96,7 +95,7 @@ public final class Id3Reader implements ElementaryStreamReader {
return; return;
} }
id3Header.skipBytes(3); // version (2) + flags (1) id3Header.skipBytes(3); // version (2) + flags (1)
sampleSize = ID3_HEADER_SIZE + id3Header.readSynchSafeInt(); sampleSize = ID3_HEADER_LENGTH + id3Header.readSynchSafeInt();
} }
} }
// Write data to the output. // Write data to the output.
......
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