Java Code Examples for com.google.android.exoplayer2.util.ParsableByteArray#readLittleEndianUnsignedInt()

The following examples show how to use com.google.android.exoplayer2.util.ParsableByteArray#readLittleEndianUnsignedInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VorbisUtil.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a vorbis identification header from {@code headerData}.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-630004.2.2">Vorbis
 *     spec/Identification header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisIdHeader} with meta data.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static VorbisIdHeader readVorbisIdentificationHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x01, headerData, false);

  long version = headerData.readLittleEndianUnsignedInt();
  int channels = headerData.readUnsignedByte();
  long sampleRate = headerData.readLittleEndianUnsignedInt();
  int bitrateMax = headerData.readLittleEndianInt();
  int bitrateNominal = headerData.readLittleEndianInt();
  int bitrateMin = headerData.readLittleEndianInt();

  int blockSize = headerData.readUnsignedByte();
  int blockSize0 = (int) Math.pow(2, blockSize & 0x0F);
  int blockSize1 = (int) Math.pow(2, (blockSize & 0xF0) >> 4);

  boolean framingFlag = (headerData.readUnsignedByte() & 0x01) > 0;
  // raw data of vorbis setup header has to be passed to decoder as CSD buffer #1
  byte[] data = Arrays.copyOf(headerData.data, headerData.limit());

  return new VorbisIdHeader(version, channels, sampleRate, bitrateMax, bitrateNominal, bitrateMin,
      blockSize0, blockSize1, framingFlag, data);
}
 
Example 2
Source File: VorbisUtil.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads a vorbis identification header from {@code headerData}.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-630004.2.2">Vorbis
 *     spec/Identification header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisUtil.VorbisIdHeader} with meta data.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static VorbisIdHeader readVorbisIdentificationHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x01, headerData, false);

  long version = headerData.readLittleEndianUnsignedInt();
  int channels = headerData.readUnsignedByte();
  long sampleRate = headerData.readLittleEndianUnsignedInt();
  int bitrateMax = headerData.readLittleEndianInt();
  int bitrateNominal = headerData.readLittleEndianInt();
  int bitrateMin = headerData.readLittleEndianInt();

  int blockSize = headerData.readUnsignedByte();
  int blockSize0 = (int) Math.pow(2, blockSize & 0x0F);
  int blockSize1 = (int) Math.pow(2, (blockSize & 0xF0) >> 4);

  boolean framingFlag = (headerData.readUnsignedByte() & 0x01) > 0;
  // raw data of vorbis setup header has to be passed to decoder as CSD buffer #1
  byte[] data = Arrays.copyOf(headerData.data, headerData.limit());

  return new VorbisIdHeader(version, channels, sampleRate, bitrateMax, bitrateNominal, bitrateMin,
      blockSize0, blockSize1, framingFlag, data);
}
 
Example 3
Source File: VorbisUtil.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads a vorbis identification header from {@code headerData}.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-630004.2.2">Vorbis
 *     spec/Identification header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisUtil.VorbisIdHeader} with meta data.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static VorbisIdHeader readVorbisIdentificationHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x01, headerData, false);

  long version = headerData.readLittleEndianUnsignedInt();
  int channels = headerData.readUnsignedByte();
  long sampleRate = headerData.readLittleEndianUnsignedInt();
  int bitrateMax = headerData.readLittleEndianInt();
  int bitrateNominal = headerData.readLittleEndianInt();
  int bitrateMin = headerData.readLittleEndianInt();

  int blockSize = headerData.readUnsignedByte();
  int blockSize0 = (int) Math.pow(2, blockSize & 0x0F);
  int blockSize1 = (int) Math.pow(2, (blockSize & 0xF0) >> 4);

  boolean framingFlag = (headerData.readUnsignedByte() & 0x01) > 0;
  // raw data of vorbis setup header has to be passed to decoder as CSD buffer #1
  byte[] data = Arrays.copyOf(headerData.data, headerData.limit());

  return new VorbisIdHeader(version, channels, sampleRate, bitrateMax, bitrateNominal, bitrateMin,
      blockSize0, blockSize1, framingFlag, data);
}
 
Example 4
Source File: VorbisUtil.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads a vorbis identification header from {@code headerData}.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-630004.2.2">Vorbis
 *     spec/Identification header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisUtil.VorbisIdHeader} with meta data.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static VorbisIdHeader readVorbisIdentificationHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x01, headerData, false);

  long version = headerData.readLittleEndianUnsignedInt();
  int channels = headerData.readUnsignedByte();
  long sampleRate = headerData.readLittleEndianUnsignedInt();
  int bitrateMax = headerData.readLittleEndianInt();
  int bitrateNominal = headerData.readLittleEndianInt();
  int bitrateMin = headerData.readLittleEndianInt();

  int blockSize = headerData.readUnsignedByte();
  int blockSize0 = (int) Math.pow(2, blockSize & 0x0F);
  int blockSize1 = (int) Math.pow(2, (blockSize & 0xF0) >> 4);

  boolean framingFlag = (headerData.readUnsignedByte() & 0x01) > 0;
  // raw data of vorbis setup header has to be passed to decoder as CSD buffer #1
  byte[] data = Arrays.copyOf(headerData.data, headerData.limit());

  return new VorbisIdHeader(version, channels, sampleRate, bitrateMax, bitrateNominal, bitrateMin,
      blockSize0, blockSize1, framingFlag, data);
}
 
Example 5
Source File: MatroskaExtractor.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds initialization data for a {@link Format} from FourCC codec private data.
 *
 * @return The codec mime type and initialization data. If the compression type is not supported
 *     then the mime type is set to {@link MimeTypes#VIDEO_UNKNOWN} and the initialization data
 *     is {@code null}.
 * @throws ParserException If the initialization data could not be built.
 */
private static Pair<String, List<byte[]>> parseFourCcPrivate(ParsableByteArray buffer)
    throws ParserException {
  try {
    buffer.skipBytes(16); // size(4), width(4), height(4), planes(2), bitcount(2).
    long compression = buffer.readLittleEndianUnsignedInt();
    if (compression == FOURCC_COMPRESSION_DIVX) {
      return new Pair<>(MimeTypes.VIDEO_DIVX, null);
    } else if (compression == FOURCC_COMPRESSION_H263) {
      return new Pair<>(MimeTypes.VIDEO_H263, null);
    } else if (compression == FOURCC_COMPRESSION_VC1) {
      // Search for the initialization data from the end of the BITMAPINFOHEADER. The last 20
      // bytes of which are: sizeImage(4), xPel/m (4), yPel/m (4), clrUsed(4), clrImportant(4).
      int startOffset = buffer.getPosition() + 20;
      byte[] bufferData = buffer.data;
      for (int offset = startOffset; offset < bufferData.length - 4; offset++) {
        if (bufferData[offset] == 0x00
            && bufferData[offset + 1] == 0x00
            && bufferData[offset + 2] == 0x01
            && bufferData[offset + 3] == 0x0F) {
          // We've found the initialization data.
          byte[] initializationData = Arrays.copyOfRange(bufferData, offset, bufferData.length);
          return new Pair<>(MimeTypes.VIDEO_VC1, Collections.singletonList(initializationData));
        }
      }
      throw new ParserException("Failed to find FourCC VC1 initialization data");
    }
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new ParserException("Error parsing FourCC private data");
  }

  Log.w(TAG, "Unknown FourCC. Setting mimeType to " + MimeTypes.VIDEO_UNKNOWN);
  return new Pair<>(MimeTypes.VIDEO_UNKNOWN, null);
}
 
Example 6
Source File: WavHeaderReader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Peeks and returns a {@link ChunkHeader}.
 *
 * @param input Input stream to peek the chunk header from.
 * @param scratch Buffer for temporary use.
 * @throws IOException If peeking from the input fails.
 * @throws InterruptedException If interrupted while peeking from input.
 * @return A new {@code ChunkHeader} peeked from {@code input}.
 */
public static ChunkHeader peek(ExtractorInput input, ParsableByteArray scratch)
    throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, SIZE_IN_BYTES);
  scratch.setPosition(0);

  int id = scratch.readInt();
  long size = scratch.readLittleEndianUnsignedInt();

  return new ChunkHeader(id, size);
}
 
Example 7
Source File: VorbisUtil.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads a vorbis comment header.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3">
 *     Vorbis spec/Comment header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisUtil.CommentHeader} with all the comments.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static CommentHeader readVorbisCommentHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x03, headerData, false);
  int length = 7;

  int len = (int) headerData.readLittleEndianUnsignedInt();
  length += 4;
  String vendor = headerData.readString(len);
  length += vendor.length();

  long commentListLen = headerData.readLittleEndianUnsignedInt();
  String[] comments = new String[(int) commentListLen];
  length += 4;
  for (int i = 0; i < commentListLen; i++) {
    len = (int) headerData.readLittleEndianUnsignedInt();
    length += 4;
    comments[i] = headerData.readString(len);
    length += comments[i].length();
  }
  if ((headerData.readUnsignedByte() & 0x01) == 0) {
    throw new ParserException("framing bit expected to be set");
  }
  length += 1;
  return new CommentHeader(vendor, comments, length);
}
 
Example 8
Source File: WavHeaderReader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Peeks and returns a {@link ChunkHeader}.
 *
 * @param input Input stream to peek the chunk header from.
 * @param scratch Buffer for temporary use.
 * @throws IOException If peeking from the input fails.
 * @throws InterruptedException If interrupted while peeking from input.
 * @return A new {@code ChunkHeader} peeked from {@code input}.
 */
public static ChunkHeader peek(ExtractorInput input, ParsableByteArray scratch)
    throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, SIZE_IN_BYTES);
  scratch.setPosition(0);

  int id = scratch.readInt();
  long size = scratch.readLittleEndianUnsignedInt();

  return new ChunkHeader(id, size);
}
 
Example 9
Source File: MatroskaExtractor.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Builds initialization data for a {@link Format} from FourCC codec private data.
 * <p>
 * VC1 is the only supported compression type.
 *
 * @return The initialization data for the {@link Format}, or null if the compression type is
 *     not VC1.
 * @throws ParserException If the initialization data could not be built.
 */
private static List<byte[]> parseFourCcVc1Private(ParsableByteArray buffer)
    throws ParserException {
  try {
    buffer.skipBytes(16); // size(4), width(4), height(4), planes(2), bitcount(2).
    long compression = buffer.readLittleEndianUnsignedInt();
    if (compression != FOURCC_COMPRESSION_VC1) {
      return null;
    }

    // Search for the initialization data from the end of the BITMAPINFOHEADER. The last 20
    // bytes of which are: sizeImage(4), xPel/m (4), yPel/m (4), clrUsed(4), clrImportant(4).
    int startOffset = buffer.getPosition() + 20;
    byte[] bufferData = buffer.data;
    for (int offset = startOffset; offset < bufferData.length - 4; offset++) {
      if (bufferData[offset] == 0x00 && bufferData[offset + 1] == 0x00
          && bufferData[offset + 2] == 0x01 && bufferData[offset + 3] == 0x0F) {
        // We've found the initialization data.
        byte[] initializationData = Arrays.copyOfRange(bufferData, offset, bufferData.length);
        return Collections.singletonList(initializationData);
      }
    }

    throw new ParserException("Failed to find FourCC VC1 initialization data");
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new ParserException("Error parsing FourCC VC1 codec private");
  }
}
 
Example 10
Source File: VorbisUtil.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Reads a vorbis comment header.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3">
 *     Vorbis spec/Comment header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisUtil.CommentHeader} with all the comments.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static CommentHeader readVorbisCommentHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x03, headerData, false);
  int length = 7;

  int len = (int) headerData.readLittleEndianUnsignedInt();
  length += 4;
  String vendor = headerData.readString(len);
  length += vendor.length();

  long commentListLen = headerData.readLittleEndianUnsignedInt();
  String[] comments = new String[(int) commentListLen];
  length += 4;
  for (int i = 0; i < commentListLen; i++) {
    len = (int) headerData.readLittleEndianUnsignedInt();
    length += 4;
    comments[i] = headerData.readString(len);
    length += comments[i].length();
  }
  if ((headerData.readUnsignedByte() & 0x01) == 0) {
    throw new ParserException("framing bit expected to be set");
  }
  length += 1;
  return new CommentHeader(vendor, comments, length);
}
 
Example 11
Source File: WavHeaderReader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Peeks and returns a {@link ChunkHeader}.
 *
 * @param input Input stream to peek the chunk header from.
 * @param scratch Buffer for temporary use.
 * @throws IOException If peeking from the input fails.
 * @throws InterruptedException If interrupted while peeking from input.
 * @return A new {@code ChunkHeader} peeked from {@code input}.
 */
public static ChunkHeader peek(ExtractorInput input, ParsableByteArray scratch)
    throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, SIZE_IN_BYTES);
  scratch.setPosition(0);

  int id = scratch.readInt();
  long size = scratch.readLittleEndianUnsignedInt();

  return new ChunkHeader(id, size);
}
 
Example 12
Source File: VorbisUtil.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads a vorbis comment header.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3">
 *     Vorbis spec/Comment header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisUtil.CommentHeader} with all the comments.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static CommentHeader readVorbisCommentHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x03, headerData, false);
  int length = 7;

  int len = (int) headerData.readLittleEndianUnsignedInt();
  length += 4;
  String vendor = headerData.readString(len);
  length += vendor.length();

  long commentListLen = headerData.readLittleEndianUnsignedInt();
  String[] comments = new String[(int) commentListLen];
  length += 4;
  for (int i = 0; i < commentListLen; i++) {
    len = (int) headerData.readLittleEndianUnsignedInt();
    length += 4;
    comments[i] = headerData.readString(len);
    length += comments[i].length();
  }
  if ((headerData.readUnsignedByte() & 0x01) == 0) {
    throw new ParserException("framing bit expected to be set");
  }
  length += 1;
  return new CommentHeader(vendor, comments, length);
}
 
Example 13
Source File: MatroskaExtractor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds initialization data for a {@link Format} from FourCC codec private data.
 *
 * <p>VC1 and H263 are the only supported compression types.
 *
 * @return The codec mime type and initialization data. If the compression type is not supported
 *     then the mime type is set to {@link MimeTypes#VIDEO_UNKNOWN} and the initialization data
 *     is {@code null}.
 * @throws ParserException If the initialization data could not be built.
 */
private static Pair<String, List<byte[]>> parseFourCcPrivate(ParsableByteArray buffer)
    throws ParserException {
  try {
    buffer.skipBytes(16); // size(4), width(4), height(4), planes(2), bitcount(2).
    long compression = buffer.readLittleEndianUnsignedInt();
    if (compression == FOURCC_COMPRESSION_DIVX) {
      return new Pair<>(MimeTypes.VIDEO_H263, null);
    } else if (compression == FOURCC_COMPRESSION_VC1) {
      // Search for the initialization data from the end of the BITMAPINFOHEADER. The last 20
      // bytes of which are: sizeImage(4), xPel/m (4), yPel/m (4), clrUsed(4), clrImportant(4).
      int startOffset = buffer.getPosition() + 20;
      byte[] bufferData = buffer.data;
      for (int offset = startOffset; offset < bufferData.length - 4; offset++) {
        if (bufferData[offset] == 0x00
            && bufferData[offset + 1] == 0x00
            && bufferData[offset + 2] == 0x01
            && bufferData[offset + 3] == 0x0F) {
          // We've found the initialization data.
          byte[] initializationData = Arrays.copyOfRange(bufferData, offset, bufferData.length);
          return new Pair<>(MimeTypes.VIDEO_VC1, Collections.singletonList(initializationData));
        }
      }
      throw new ParserException("Failed to find FourCC VC1 initialization data");
    }
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new ParserException("Error parsing FourCC private data");
  }

  Log.w(TAG, "Unknown FourCC. Setting mimeType to " + MimeTypes.VIDEO_UNKNOWN);
  return new Pair<>(MimeTypes.VIDEO_UNKNOWN, null);
}
 
Example 14
Source File: VorbisUtil.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads a vorbis comment header.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3">
 *     Vorbis spec/Comment header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link VorbisUtil.CommentHeader} with all the comments.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static CommentHeader readVorbisCommentHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x03, headerData, false);
  int length = 7;

  int len = (int) headerData.readLittleEndianUnsignedInt();
  length += 4;
  String vendor = headerData.readString(len);
  length += vendor.length();

  long commentListLen = headerData.readLittleEndianUnsignedInt();
  String[] comments = new String[(int) commentListLen];
  length += 4;
  for (int i = 0; i < commentListLen; i++) {
    len = (int) headerData.readLittleEndianUnsignedInt();
    length += 4;
    comments[i] = headerData.readString(len);
    length += comments[i].length();
  }
  if ((headerData.readUnsignedByte() & 0x01) == 0) {
    throw new ParserException("framing bit expected to be set");
  }
  length += 1;
  return new CommentHeader(vendor, comments, length);
}
 
Example 15
Source File: WavHeaderReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Peeks and returns a {@link ChunkHeader}.
 *
 * @param input Input stream to peek the chunk header from.
 * @param scratch Buffer for temporary use.
 * @throws IOException If peeking from the input fails.
 * @throws InterruptedException If interrupted while peeking from input.
 * @return A new {@code ChunkHeader} peeked from {@code input}.
 */
public static ChunkHeader peek(ExtractorInput input, ParsableByteArray scratch)
    throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, SIZE_IN_BYTES);
  scratch.setPosition(0);

  int id = scratch.readInt();
  long size = scratch.readLittleEndianUnsignedInt();

  return new ChunkHeader(id, size);
}
 
Example 16
Source File: MatroskaExtractor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds initialization data for a {@link Format} from FourCC codec private data.
 *
 * <p>VC1 and H263 are the only supported compression types.
 *
 * @return The codec mime type and initialization data. If the compression type is not supported
 *     then the mime type is set to {@link MimeTypes#VIDEO_UNKNOWN} and the initialization data
 *     is {@code null}.
 * @throws ParserException If the initialization data could not be built.
 */
private static Pair<String, List<byte[]>> parseFourCcPrivate(ParsableByteArray buffer)
    throws ParserException {
  try {
    buffer.skipBytes(16); // size(4), width(4), height(4), planes(2), bitcount(2).
    long compression = buffer.readLittleEndianUnsignedInt();
    if (compression == FOURCC_COMPRESSION_DIVX) {
      return new Pair<>(MimeTypes.VIDEO_H263, null);
    } else if (compression == FOURCC_COMPRESSION_VC1) {
      // Search for the initialization data from the end of the BITMAPINFOHEADER. The last 20
      // bytes of which are: sizeImage(4), xPel/m (4), yPel/m (4), clrUsed(4), clrImportant(4).
      int startOffset = buffer.getPosition() + 20;
      byte[] bufferData = buffer.data;
      for (int offset = startOffset; offset < bufferData.length - 4; offset++) {
        if (bufferData[offset] == 0x00
            && bufferData[offset + 1] == 0x00
            && bufferData[offset + 2] == 0x01
            && bufferData[offset + 3] == 0x0F) {
          // We've found the initialization data.
          byte[] initializationData = Arrays.copyOfRange(bufferData, offset, bufferData.length);
          return new Pair<>(MimeTypes.VIDEO_VC1, Collections.singletonList(initializationData));
        }
      }
      throw new ParserException("Failed to find FourCC VC1 initialization data");
    }
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new ParserException("Error parsing FourCC private data");
  }

  Log.w(TAG, "Unknown FourCC. Setting mimeType to " + MimeTypes.VIDEO_UNKNOWN);
  return new Pair<>(MimeTypes.VIDEO_UNKNOWN, null);
}
 
Example 17
Source File: MatroskaExtractor.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds initialization data for a {@link Format} from FourCC codec private data.
 *
 * @return The codec mime type and initialization data. If the compression type is not supported
 *     then the mime type is set to {@link MimeTypes#VIDEO_UNKNOWN} and the initialization data
 *     is {@code null}.
 * @throws ParserException If the initialization data could not be built.
 */
private static Pair<String, List<byte[]>> parseFourCcPrivate(ParsableByteArray buffer)
    throws ParserException {
  try {
    buffer.skipBytes(16); // size(4), width(4), height(4), planes(2), bitcount(2).
    long compression = buffer.readLittleEndianUnsignedInt();
    if (compression == FOURCC_COMPRESSION_DIVX) {
      return new Pair<>(MimeTypes.VIDEO_DIVX, null);
    } else if (compression == FOURCC_COMPRESSION_H263) {
      return new Pair<>(MimeTypes.VIDEO_H263, null);
    } else if (compression == FOURCC_COMPRESSION_VC1) {
      // Search for the initialization data from the end of the BITMAPINFOHEADER. The last 20
      // bytes of which are: sizeImage(4), xPel/m (4), yPel/m (4), clrUsed(4), clrImportant(4).
      int startOffset = buffer.getPosition() + 20;
      byte[] bufferData = buffer.data;
      for (int offset = startOffset; offset < bufferData.length - 4; offset++) {
        if (bufferData[offset] == 0x00
            && bufferData[offset + 1] == 0x00
            && bufferData[offset + 2] == 0x01
            && bufferData[offset + 3] == 0x0F) {
          // We've found the initialization data.
          byte[] initializationData = Arrays.copyOfRange(bufferData, offset, bufferData.length);
          return new Pair<>(MimeTypes.VIDEO_VC1, Collections.singletonList(initializationData));
        }
      }
      throw new ParserException("Failed to find FourCC VC1 initialization data");
    }
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new ParserException("Error parsing FourCC private data");
  }

  Log.w(TAG, "Unknown FourCC. Setting mimeType to " + MimeTypes.VIDEO_UNKNOWN);
  return new Pair<>(MimeTypes.VIDEO_UNKNOWN, null);
}
 
Example 18
Source File: WavHeaderReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Peeks and returns a {@link ChunkHeader}.
 *
 * @param input Input stream to peek the chunk header from.
 * @param scratch Buffer for temporary use.
 * @throws IOException If peeking from the input fails.
 * @throws InterruptedException If interrupted while peeking from input.
 * @return A new {@code ChunkHeader} peeked from {@code input}.
 */
public static ChunkHeader peek(ExtractorInput input, ParsableByteArray scratch)
    throws IOException, InterruptedException {
  input.peekFully(scratch.data, 0, SIZE_IN_BYTES);
  scratch.setPosition(0);

  int id = scratch.readInt();
  long size = scratch.readLittleEndianUnsignedInt();

  return new ChunkHeader(id, size);
}
 
Example 19
Source File: MatroskaExtractor.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Builds initialization data for a {@link Format} from FourCC codec private data.
 *
 * @return The codec mime type and initialization data. If the compression type is not supported
 *     then the mime type is set to {@link MimeTypes#VIDEO_UNKNOWN} and the initialization data
 *     is {@code null}.
 * @throws ParserException If the initialization data could not be built.
 */
private static Pair<String, List<byte[]>> parseFourCcPrivate(ParsableByteArray buffer)
    throws ParserException {
  try {
    buffer.skipBytes(16); // size(4), width(4), height(4), planes(2), bitcount(2).
    long compression = buffer.readLittleEndianUnsignedInt();
    if (compression == FOURCC_COMPRESSION_DIVX) {
      return new Pair<>(MimeTypes.VIDEO_DIVX, null);
    } else if (compression == FOURCC_COMPRESSION_H263) {
      return new Pair<>(MimeTypes.VIDEO_H263, null);
    } else if (compression == FOURCC_COMPRESSION_VC1) {
      // Search for the initialization data from the end of the BITMAPINFOHEADER. The last 20
      // bytes of which are: sizeImage(4), xPel/m (4), yPel/m (4), clrUsed(4), clrImportant(4).
      int startOffset = buffer.getPosition() + 20;
      byte[] bufferData = buffer.data;
      for (int offset = startOffset; offset < bufferData.length - 4; offset++) {
        if (bufferData[offset] == 0x00
            && bufferData[offset + 1] == 0x00
            && bufferData[offset + 2] == 0x01
            && bufferData[offset + 3] == 0x0F) {
          // We've found the initialization data.
          byte[] initializationData = Arrays.copyOfRange(bufferData, offset, bufferData.length);
          return new Pair<>(MimeTypes.VIDEO_VC1, Collections.singletonList(initializationData));
        }
      }
      throw new ParserException("Failed to find FourCC VC1 initialization data");
    }
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new ParserException("Error parsing FourCC private data");
  }

  Log.w(TAG, "Unknown FourCC. Setting mimeType to " + MimeTypes.VIDEO_UNKNOWN);
  return new Pair<>(MimeTypes.VIDEO_UNKNOWN, null);
}
 
Example 20
Source File: VorbisUtil.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a vorbis comment header.
 *
 * @see <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3">
 *     Vorbis spec/Comment header</a>
 * @param headerData a {@link ParsableByteArray} wrapping the header data.
 * @return a {@link CommentHeader} with all the comments.
 * @throws ParserException thrown if invalid capture pattern is detected.
 */
public static CommentHeader readVorbisCommentHeader(ParsableByteArray headerData)
    throws ParserException {

  verifyVorbisHeaderCapturePattern(0x03, headerData, false);
  int length = 7;

  int len = (int) headerData.readLittleEndianUnsignedInt();
  length += 4;
  String vendor = headerData.readString(len);
  length += vendor.length();

  long commentListLen = headerData.readLittleEndianUnsignedInt();
  String[] comments = new String[(int) commentListLen];
  length += 4;
  for (int i = 0; i < commentListLen; i++) {
    len = (int) headerData.readLittleEndianUnsignedInt();
    length += 4;
    comments[i] = headerData.readString(len);
    length += comments[i].length();
  }
  if ((headerData.readUnsignedByte() & 0x01) == 0) {
    throw new ParserException("framing bit expected to be set");
  }
  length += 1;
  return new CommentHeader(vendor, comments, length);
}