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

The following examples show how to use com.google.android.exoplayer2.util.ParsableByteArray#readUnsignedByte() . 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: DolbyVisionConfig.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses Dolby Vision configuration data.
 *
 * @param data A {@link ParsableByteArray}, whose position is set to the start of the Dolby Vision
 *     configuration data to parse.
 * @return The {@link DolbyVisionConfig} corresponding to the configuration, or {@code null} if
 *     the configuration isn't supported.
 */
@Nullable
public static DolbyVisionConfig parse(ParsableByteArray data) {
  data.skipBytes(2); // dv_version_major, dv_version_minor
  int profileData = data.readUnsignedByte();
  int dvProfile = (profileData >> 1);
  int dvLevel = ((profileData & 0x1) << 5) | ((data.readUnsignedByte() >> 3) & 0x1F);
  String codecsPrefix;
  if (dvProfile == 4 || dvProfile == 5) {
    codecsPrefix = "dvhe";
  } else if (dvProfile == 8) {
    codecsPrefix = "hev1";
  } else if (dvProfile == 9) {
    codecsPrefix = "avc3";
  } else {
    return null;
  }
  String codecs = codecsPrefix + ".0" + dvProfile + ".0" + dvLevel;
  return new DolbyVisionConfig(dvProfile, dvLevel, codecs);
}
 
Example 2
Source File: DtsReader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Locates the next SYNC value in the buffer, advancing the position to the byte that immediately
 * follows it. If SYNC was not located, the position is advanced to the limit.
 *
 * @param pesBuffer The buffer whose position should be advanced.
 * @return Whether SYNC was found.
 */
private boolean skipToNextSync(ParsableByteArray pesBuffer) {
  while (pesBuffer.bytesLeft() > 0) {
    syncBytes <<= 8;
    syncBytes |= pesBuffer.readUnsignedByte();
    if (DtsUtil.isSyncWord(syncBytes)) {
      headerScratchBytes.data[0] = (byte) ((syncBytes >> 24) & 0xFF);
      headerScratchBytes.data[1] = (byte) ((syncBytes >> 16) & 0xFF);
      headerScratchBytes.data[2] = (byte) ((syncBytes >> 8) & 0xFF);
      headerScratchBytes.data[3] = (byte) (syncBytes & 0xFF);
      bytesRead = 4;
      syncBytes = 0;
      return true;
    }
  }
  return false;
}
 
Example 3
Source File: AtomParsers.java    From K-Sonic with MIT License 6 votes vote down vote up
private static TrackEncryptionBox parseSchiFromParent(ParsableByteArray parent, int position,
    int size) {
  int childPosition = position + Atom.HEADER_SIZE;
  while (childPosition - position < size) {
    parent.setPosition(childPosition);
    int childAtomSize = parent.readInt();
    int childAtomType = parent.readInt();
    if (childAtomType == Atom.TYPE_tenc) {
      parent.skipBytes(6);
      boolean defaultIsEncrypted = parent.readUnsignedByte() == 1;
      int defaultInitVectorSize = parent.readUnsignedByte();
      byte[] defaultKeyId = new byte[16];
      parent.readBytes(defaultKeyId, 0, defaultKeyId.length);
      return new TrackEncryptionBox(defaultIsEncrypted, defaultInitVectorSize, defaultKeyId);
    }
    childPosition += childAtomSize;
  }
  return null;
}
 
Example 4
Source File: Ac4Reader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Locates the next syncword, advancing the position to the byte that immediately follows it. If a
 * syncword was not located, the position is advanced to the limit.
 *
 * @param pesBuffer The buffer whose position should be advanced.
 * @return Whether a syncword position was found.
 */
private boolean skipToNextSync(ParsableByteArray pesBuffer) {
  while (pesBuffer.bytesLeft() > 0) {
    if (!lastByteWasAC) {
      lastByteWasAC = (pesBuffer.readUnsignedByte() == 0xAC);
      continue;
    }
    int secondByte = pesBuffer.readUnsignedByte();
    lastByteWasAC = secondByte == 0xAC;
    if (secondByte == 0x40 || secondByte == 0x41) {
      hasCRC = secondByte == 0x41;
      return true;
    }
  }
  return false;
}
 
Example 5
Source File: Ac3Util.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the AC-3 format given {@code data} containing the AC3SpecificBox according to ETSI TS
 * 102 366 Annex F. The reading position of {@code data} will be modified.
 *
 * @param data The AC3SpecificBox to parse.
 * @param trackId The track identifier to set on the format.
 * @param language The language to set on the format.
 * @param drmInitData {@link DrmInitData} to be included in the format.
 * @return The AC-3 format parsed from data in the header.
 */
public static Format parseAc3AnnexFFormat(
    ParsableByteArray data, String trackId, String language, DrmInitData drmInitData) {
  int fscod = (data.readUnsignedByte() & 0xC0) >> 6;
  int sampleRate = SAMPLE_RATE_BY_FSCOD[fscod];
  int nextByte = data.readUnsignedByte();
  int channelCount = CHANNEL_COUNT_BY_ACMOD[(nextByte & 0x38) >> 3];
  if ((nextByte & 0x04) != 0) { // lfeon
    channelCount++;
  }
  return Format.createAudioSampleFormat(
      trackId,
      MimeTypes.AUDIO_AC3,
      /* codecs= */ null,
      Format.NO_VALUE,
      Format.NO_VALUE,
      channelCount,
      sampleRate,
      /* initializationData= */ null,
      drmInitData,
      /* selectionFlags= */ 0,
      language);
}
 
Example 6
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 7
Source File: DvbSubtitleReader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private boolean checkNextByte(ParsableByteArray data, int expectedValue) {
  if (data.bytesLeft() == 0) {
    return false;
  }
  if (data.readUnsignedByte() != expectedValue) {
    writingSample = false;
  }
  bytesToCheck--;
  return writingSample;
}
 
Example 8
Source File: VideoTagPayloadReader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected boolean parseHeader(ParsableByteArray data) throws UnsupportedFormatException {
  int header = data.readUnsignedByte();
  int frameType = (header >> 4) & 0x0F;
  int videoCodec = (header & 0x0F);
  // Support just H.264 encoded content.
  if (videoCodec != VIDEO_CODEC_AVC) {
    throw new UnsupportedFormatException("Video format not supported: " + videoCodec);
  }
  this.frameType = frameType;
  return (frameType != VIDEO_FRAME_VIDEO_INFO);
}
 
Example 9
Source File: CssParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
static String parseNextToken(ParsableByteArray input, StringBuilder stringBuilder) {
  skipWhitespaceAndComments(input);
  if (input.bytesLeft() == 0) {
    return null;
  }
  String identifier = parseIdentifier(input, stringBuilder);
  if (!"".equals(identifier)) {
    return identifier;
  }
  // We found a delimiter.
  return "" + (char) input.readUnsignedByte();
}
 
Example 10
Source File: PgsDecoder.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Nullable
private static Cue readNextSection(ParsableByteArray buffer, CueBuilder cueBuilder) {
  int limit = buffer.limit();
  int sectionType = buffer.readUnsignedByte();
  int sectionLength = buffer.readUnsignedShort();

  int nextSectionPosition = buffer.getPosition() + sectionLength;
  if (nextSectionPosition > limit) {
    buffer.setPosition(limit);
    return null;
  }

  Cue cue = null;
  switch (sectionType) {
    case SECTION_TYPE_PALETTE:
      cueBuilder.parsePaletteSection(buffer, sectionLength);
      break;
    case SECTION_TYPE_BITMAP_PICTURE:
      cueBuilder.parseBitmapSection(buffer, sectionLength);
      break;
    case SECTION_TYPE_IDENTIFIER:
      cueBuilder.parseIdentifierSection(buffer, sectionLength);
      break;
    case SECTION_TYPE_END:
      cue = cueBuilder.build();
      cueBuilder.reset();
      break;
    default:
      break;
  }

  buffer.setPosition(nextSectionPosition);
  return cue;
}
 
Example 11
Source File: TsUtil.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the PCR value read from a given TS packet.
 *
 * @param packetBuffer The buffer that holds the packet.
 * @param startOfPacket The starting position of the packet in the buffer.
 * @param pcrPid The PID for valid packets that contain PCR values.
 * @return The PCR value read from the packet, if its PID is equal to {@code pcrPid} and it
 *     contains a valid PCR value. Returns {@link C#TIME_UNSET} otherwise.
 */
public static long readPcrFromPacket(
    ParsableByteArray packetBuffer, int startOfPacket, int pcrPid) {
  packetBuffer.setPosition(startOfPacket);
  if (packetBuffer.bytesLeft() < 5) {
    // Header = 4 bytes, adaptationFieldLength = 1 byte.
    return C.TIME_UNSET;
  }
  // Note: See ISO/IEC 13818-1, section 2.4.3.2 for details of the header format.
  int tsPacketHeader = packetBuffer.readInt();
  if ((tsPacketHeader & 0x800000) != 0) {
    // transport_error_indicator != 0 means there are uncorrectable errors in this packet.
    return C.TIME_UNSET;
  }
  int pid = (tsPacketHeader & 0x1FFF00) >> 8;
  if (pid != pcrPid) {
    return C.TIME_UNSET;
  }
  boolean adaptationFieldExists = (tsPacketHeader & 0x20) != 0;
  if (!adaptationFieldExists) {
    return C.TIME_UNSET;
  }

  int adaptationFieldLength = packetBuffer.readUnsignedByte();
  if (adaptationFieldLength >= 7 && packetBuffer.bytesLeft() >= 7) {
    int flags = packetBuffer.readUnsignedByte();
    boolean pcrFlagSet = (flags & 0x10) == 0x10;
    if (pcrFlagSet) {
      byte[] pcrBytes = new byte[6];
      packetBuffer.readBytes(pcrBytes, /* offset= */ 0, pcrBytes.length);
      return readPcrValueFromPcrBytes(pcrBytes);
    }
  }
  return C.TIME_UNSET;
}
 
Example 12
Source File: AtomParsers.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses the size of an expandable class, as specified by ISO 14496-1 subsection 8.3.3.
 */
private static int parseExpandableClassSize(ParsableByteArray data) {
  int currentByte = data.readUnsignedByte();
  int size = currentByte & 0x7F;
  while ((currentByte & 0x80) == 0x80) {
    currentByte = data.readUnsignedByte();
    size = (size << 7) | (currentByte & 0x7F);
  }
  return size;
}
 
Example 13
Source File: FlacReader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private int getFlacFrameBlockSize(ParsableByteArray packet) {
  int blockSizeCode = (packet.data[2] & 0xFF) >> 4;
  switch (blockSizeCode) {
    case 1:
      return 192;
    case 2:
    case 3:
    case 4:
    case 5:
      return 576 << (blockSizeCode - 2);
    case 6:
    case 7:
      // skip the sample number
      packet.skipBytes(FRAME_HEADER_SAMPLE_NUMBER_OFFSET);
      packet.readUtf8EncodedLong();
      int value = blockSizeCode == 6 ? packet.readUnsignedByte() : packet.readUnsignedShort();
      packet.setPosition(0);
      return value + 1;
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
      return 256 << (blockSizeCode - 8);
    default:
      return -1;
  }
}
 
Example 14
Source File: TsExtractor.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public void consume(ParsableByteArray sectionData) {
  int tableId = sectionData.readUnsignedByte();
  if (tableId != 0x00 /* program_association_section */) {
    // See ISO/IEC 13818-1, section 2.4.4.4 for more information on table id assignment.
    return;
  }
  // section_syntax_indicator(1), '0'(1), reserved(2), section_length(12),
  // transport_stream_id (16), reserved (2), version_number (5), current_next_indicator (1),
  // section_number (8), last_section_number (8)
  sectionData.skipBytes(7);

  int programCount = sectionData.bytesLeft() / 4;
  for (int i = 0; i < programCount; i++) {
    sectionData.readBytes(patScratch, 4);
    int programNumber = patScratch.readBits(16);
    patScratch.skipBits(3); // reserved (3)
    if (programNumber == 0) {
      patScratch.skipBits(13); // network_PID (13)
    } else {
      int pid = patScratch.readBits(13);
      tsPayloadReaders.put(pid, new SectionReader(new PmtReader(pid)));
      remainingPmts++;
    }
  }
  if (mode != MODE_HLS) {
    tsPayloadReaders.remove(TS_PAT_PID);
  }
}
 
Example 15
Source File: Tx3gDecoder.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void applyStyleRecord(ParsableByteArray parsableByteArray,
    SpannableStringBuilder cueText) throws SubtitleDecoderException {
  assertTrue(parsableByteArray.bytesLeft() >= SIZE_STYLE_RECORD);
  int start = parsableByteArray.readUnsignedShort();
  int end = parsableByteArray.readUnsignedShort();
  parsableByteArray.skipBytes(2); // font identifier
  int fontFace = parsableByteArray.readUnsignedByte();
  parsableByteArray.skipBytes(1); // font size
  int colorRgba = parsableByteArray.readInt();
  attachFontFace(cueText, fontFace, defaultFontFace, start, end, SPAN_PRIORITY_HIGH);
  attachColor(cueText, colorRgba, defaultColorRgba, start, end, SPAN_PRIORITY_HIGH);
}
 
Example 16
Source File: AtomParsers.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static TrackEncryptionBox parseSchiFromParent(ParsableByteArray parent, int position,
    int size, String schemeType) {
  int childPosition = position + Atom.HEADER_SIZE;
  while (childPosition - position < size) {
    parent.setPosition(childPosition);
    int childAtomSize = parent.readInt();
    int childAtomType = parent.readInt();
    if (childAtomType == Atom.TYPE_tenc) {
      int fullAtom = parent.readInt();
      int version = Atom.parseFullAtomVersion(fullAtom);
      parent.skipBytes(1); // reserved = 0.
      int defaultCryptByteBlock = 0;
      int defaultSkipByteBlock = 0;
      if (version == 0) {
        parent.skipBytes(1); // reserved = 0.
      } else /* version 1 or greater */ {
        int patternByte = parent.readUnsignedByte();
        defaultCryptByteBlock = (patternByte & 0xF0) >> 4;
        defaultSkipByteBlock = patternByte & 0x0F;
      }
      boolean defaultIsProtected = parent.readUnsignedByte() == 1;
      int defaultPerSampleIvSize = parent.readUnsignedByte();
      byte[] defaultKeyId = new byte[16];
      parent.readBytes(defaultKeyId, 0, defaultKeyId.length);
      byte[] constantIv = null;
      if (defaultIsProtected && defaultPerSampleIvSize == 0) {
        int constantIvSize = parent.readUnsignedByte();
        constantIv = new byte[constantIvSize];
        parent.readBytes(constantIv, 0, constantIvSize);
      }
      return new TrackEncryptionBox(defaultIsProtected, schemeType, defaultPerSampleIvSize,
          defaultKeyId, defaultCryptByteBlock, defaultSkipByteBlock, constantIv);
    }
    childPosition += childAtomSize;
  }
  return null;
}
 
Example 17
Source File: Ac3Util.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Returns the AC-3 format given {@code data} containing the AC3SpecificBox according to
 * ETSI TS 102 366 Annex F. The reading position of {@code data} will be modified.
 *
 * @param data The AC3SpecificBox to parse.
 * @param trackId The track identifier to set on the format, or null.
 * @param language The language to set on the format.
 * @param drmInitData {@link DrmInitData} to be included in the format.
 * @return The AC-3 format parsed from data in the header.
 */
public static Format parseAc3AnnexFFormat(ParsableByteArray data, String trackId,
    String language, DrmInitData drmInitData) {
  int fscod = (data.readUnsignedByte() & 0xC0) >> 6;
  int sampleRate = SAMPLE_RATE_BY_FSCOD[fscod];
  int nextByte = data.readUnsignedByte();
  int channelCount = CHANNEL_COUNT_BY_ACMOD[(nextByte & 0x38) >> 3];
  if ((nextByte & 0x04) != 0) { // lfeon
    channelCount++;
  }
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_AC3, null, Format.NO_VALUE,
      Format.NO_VALUE, channelCount, sampleRate, null, drmInitData, 0, language);
}
 
Example 18
Source File: ScriptTagPayloadReader.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private static int readAmfType(ParsableByteArray data) {
  return data.readUnsignedByte();
}
 
Example 19
Source File: AtomParsers.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Returns codec-specific initialization data contained in an esds box.
 */
private static Pair<String, byte[]> parseEsdsFromParent(ParsableByteArray parent, int position) {
  parent.setPosition(position + Atom.HEADER_SIZE + 4);
  // Start of the ES_Descriptor (defined in 14496-1)
  parent.skipBytes(1); // ES_Descriptor tag
  parseExpandableClassSize(parent);
  parent.skipBytes(2); // ES_ID

  int flags = parent.readUnsignedByte();
  if ((flags & 0x80 /* streamDependenceFlag */) != 0) {
    parent.skipBytes(2);
  }
  if ((flags & 0x40 /* URL_Flag */) != 0) {
    parent.skipBytes(parent.readUnsignedShort());
  }
  if ((flags & 0x20 /* OCRstreamFlag */) != 0) {
    parent.skipBytes(2);
  }

  // Start of the DecoderConfigDescriptor (defined in 14496-1)
  parent.skipBytes(1); // DecoderConfigDescriptor tag
  parseExpandableClassSize(parent);

  // Set the MIME type based on the object type indication (14496-1 table 5).
  int objectTypeIndication = parent.readUnsignedByte();
  String mimeType = getMimeTypeFromMp4ObjectType(objectTypeIndication);
  if (MimeTypes.AUDIO_MPEG.equals(mimeType)
      || MimeTypes.AUDIO_DTS.equals(mimeType)
      || MimeTypes.AUDIO_DTS_HD.equals(mimeType)) {
    return Pair.create(mimeType, null);
  }

  parent.skipBytes(12);

  // Start of the DecoderSpecificInfo.
  parent.skipBytes(1); // DecoderSpecificInfo tag
  int initializationDataSize = parseExpandableClassSize(parent);
  byte[] initializationData = new byte[initializationDataSize];
  parent.readBytes(initializationData, 0, initializationDataSize);
  return Pair.create(mimeType, initializationData);
}
 
Example 20
Source File: ScriptTagPayloadReader.java    From MediaSDK with Apache License 2.0 2 votes vote down vote up
/**
 * Read a boolean from an AMF encoded buffer.
 *
 * @param data The buffer from which to read.
 * @return The value read from the buffer.
 */
private static Boolean readAmfBoolean(ParsableByteArray data) {
  return data.readUnsignedByte() == 1;
}