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

The following examples show how to use com.google.android.exoplayer2.util.ParsableByteArray#readBytes() . 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: MetadataUtil.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Nullable
private static ApicFrame parseCoverArt(ParsableByteArray data) {
  int atomSize = data.readInt();
  int atomType = data.readInt();
  if (atomType == Atom.TYPE_data) {
    int fullVersionInt = data.readInt();
    int flags = Atom.parseFullAtomFlags(fullVersionInt);
    String mimeType = flags == 13 ? "image/jpeg" : flags == 14 ? "image/png" : null;
    if (mimeType == null) {
      Log.w(TAG, "Unrecognized cover art flags: " + flags);
      return null;
    }
    data.skipBytes(4); // empty (4)
    byte[] pictureData = new byte[atomSize - 16];
    data.readBytes(pictureData, 0, pictureData.length);
    return new ApicFrame(
        mimeType,
        /* description= */ null,
        /* pictureType= */ PICTURE_TYPE_FRONT_COVER,
        pictureData);
  }
  Log.w(TAG, "Failed to parse cover art attribute");
  return null;
}
 
Example 2
Source File: MetadataUtil.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an 'mdta' metadata entry starting at the current position in an ilst box.
 *
 * @param ilst The ilst box.
 * @param endPosition The end position of the entry in the ilst box.
 * @param key The mdta metadata entry key for the entry.
 * @return The parsed element, or null if the entry wasn't recognized.
 */
@Nullable
public static MdtaMetadataEntry parseMdtaMetadataEntryFromIlst(
    ParsableByteArray ilst, int endPosition, String key) {
  int atomPosition;
  while ((atomPosition = ilst.getPosition()) < endPosition) {
    int atomSize = ilst.readInt();
    int atomType = ilst.readInt();
    if (atomType == Atom.TYPE_data) {
      int typeIndicator = ilst.readInt();
      int localeIndicator = ilst.readInt();
      int dataSize = atomSize - 16;
      byte[] value = new byte[dataSize];
      ilst.readBytes(value, 0, dataSize);
      return new MdtaMetadataEntry(key, value, localeIndicator, typeIndicator);
    }
    ilst.setPosition(atomPosition + atomSize);
  }
  return null;
}
 
Example 3
Source File: Id3Decoder.java    From K-Sonic with MIT License 6 votes vote down vote up
private static TextInformationFrame decodeTxxxFrame(ParsableByteArray id3Data, int frameSize)
    throws UnsupportedEncodingException {
  int encoding = id3Data.readUnsignedByte();
  String charset = getCharsetName(encoding);

  byte[] data = new byte[frameSize - 1];
  id3Data.readBytes(data, 0, frameSize - 1);

  int descriptionEndIndex = indexOfEos(data, 0, encoding);
  String description = new String(data, 0, descriptionEndIndex, charset);

  String value;
  int valueStartIndex = descriptionEndIndex + delimiterLength(encoding);
  if (valueStartIndex < data.length) {
    int valueEndIndex = indexOfEos(data, valueStartIndex, encoding);
    value = new String(data, valueStartIndex, valueEndIndex - valueStartIndex, charset);
  } else {
    value = "";
  }

  return new TextInformationFrame("TXXX", description, value);
}
 
Example 4
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 5
Source File: Id3Decoder.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private static @Nullable TextInformationFrame decodeTxxxFrame(
    ParsableByteArray id3Data, int frameSize) throws UnsupportedEncodingException {
  if (frameSize < 1) {
    // Frame is malformed.
    return null;
  }

  int encoding = id3Data.readUnsignedByte();
  String charset = getCharsetName(encoding);

  byte[] data = new byte[frameSize - 1];
  id3Data.readBytes(data, 0, frameSize - 1);

  int descriptionEndIndex = indexOfEos(data, 0, encoding);
  String description = new String(data, 0, descriptionEndIndex, charset);

  int valueStartIndex = descriptionEndIndex + delimiterLength(encoding);
  int valueEndIndex = indexOfEos(data, valueStartIndex, encoding);
  String value = decodeStringIfValid(data, valueStartIndex, valueEndIndex, charset);

  return new TextInformationFrame("TXXX", description, value);
}
 
Example 6
Source File: LatmReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void consume(ParsableByteArray data) throws ParserException {
  int bytesToRead;
  while (data.bytesLeft() > 0) {
    switch (state) {
      case STATE_FINDING_SYNC_1:
        if (data.readUnsignedByte() == SYNC_BYTE_FIRST) {
          state = STATE_FINDING_SYNC_2;
        }
        break;
      case STATE_FINDING_SYNC_2:
        int secondByte = data.readUnsignedByte();
        if ((secondByte & SYNC_BYTE_SECOND) == SYNC_BYTE_SECOND) {
          secondHeaderByte = secondByte;
          state = STATE_READING_HEADER;
        } else if (secondByte != SYNC_BYTE_FIRST) {
          state = STATE_FINDING_SYNC_1;
        }
        break;
      case STATE_READING_HEADER:
        sampleSize = ((secondHeaderByte & ~SYNC_BYTE_SECOND) << 8) | data.readUnsignedByte();
        if (sampleSize > sampleDataBuffer.data.length) {
          resetBufferForSize(sampleSize);
        }
        bytesRead = 0;
        state = STATE_READING_SAMPLE;
        break;
      case STATE_READING_SAMPLE:
        bytesToRead = Math.min(data.bytesLeft(), sampleSize - bytesRead);
        data.readBytes(sampleBitArray.data, bytesRead, bytesToRead);
        bytesRead += bytesToRead;
        if (bytesRead == sampleSize) {
          sampleBitArray.setPosition(0);
          parseAudioMuxElement(sampleBitArray);
          state = STATE_FINDING_SYNC_1;
        }
        break;
    }
  }
}
 
Example 7
Source File: PrivateCommand.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
static PrivateCommand parseFromSection(ParsableByteArray sectionData,
    int commandLength, long ptsAdjustment) {
  long identifier = sectionData.readUnsignedInt();
  byte[] privateBytes = new byte[commandLength - 4 /* identifier size */];
  sectionData.readBytes(privateBytes, 0, privateBytes.length);
  return new PrivateCommand(identifier, privateBytes, ptsAdjustment);
}
 
Example 8
Source File: PgsDecoder.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private void parseBitmapSection(ParsableByteArray buffer, int sectionLength) {
  if (sectionLength < 4) {
    return;
  }
  buffer.skipBytes(3); // Id (2 bytes), version (1 byte).
  boolean isBaseSection = (0x80 & buffer.readUnsignedByte()) != 0;
  sectionLength -= 4;

  if (isBaseSection) {
    if (sectionLength < 7) {
      return;
    }
    int totalLength = buffer.readUnsignedInt24();
    if (totalLength < 4) {
      return;
    }
    bitmapWidth = buffer.readUnsignedShort();
    bitmapHeight = buffer.readUnsignedShort();
    bitmapData.reset(totalLength - 4);
    sectionLength -= 7;
  }

  int position = bitmapData.getPosition();
  int limit = bitmapData.limit();
  if (position < limit && sectionLength > 0) {
    int bytesToRead = Math.min(sectionLength, limit - position);
    buffer.readBytes(bitmapData.data, position, bytesToRead);
    bitmapData.setPosition(position + bytesToRead);
  }
}
 
Example 9
Source File: MpegAudioReader.java    From K-Sonic with MIT License 5 votes vote down vote up
/**
 * Attempts to read the remaining two bytes of the frame header.
 * <p>
 * If a frame header is read in full then the state is changed to {@link #STATE_READING_FRAME},
 * the media format is output if this has not previously occurred, the four header bytes are
 * output as sample data, and the position of the source is advanced to the byte that immediately
 * follows the header.
 * <p>
 * If a frame header is read in full but cannot be parsed then the state is changed to
 * {@link #STATE_READING_HEADER}.
 * <p>
 * If a frame header is not read in full then the position of the source is advanced to the limit,
 * and the method should be called again with the next source to continue the read.
 *
 * @param source The source from which to read.
 */
private void readHeaderRemainder(ParsableByteArray source) {
  int bytesToRead = Math.min(source.bytesLeft(), HEADER_SIZE - frameBytesRead);
  source.readBytes(headerScratch.data, frameBytesRead, bytesToRead);
  frameBytesRead += bytesToRead;
  if (frameBytesRead < HEADER_SIZE) {
    // We haven't read the whole header yet.
    return;
  }

  headerScratch.setPosition(0);
  boolean parsedHeader = MpegAudioHeader.populateHeader(headerScratch.readInt(), header);
  if (!parsedHeader) {
    // We thought we'd located a frame header, but we hadn't.
    frameBytesRead = 0;
    state = STATE_READING_HEADER;
    return;
  }

  frameSize = header.frameSize;
  if (!hasOutputFormat) {
    frameDurationUs = (C.MICROS_PER_SECOND * header.samplesPerFrame) / header.sampleRate;
    Format format = Format.createAudioSampleFormat(formatId, header.mimeType, null,
        Format.NO_VALUE, MpegAudioHeader.MAX_FRAME_SIZE_BYTES, header.channels, header.sampleRate,
        null, null, 0, language);
    output.format(format);
    hasOutputFormat = true;
  }

  headerScratch.setPosition(0);
  output.sampleData(headerScratch, HEADER_SIZE);
  state = STATE_READING_FRAME;
}
 
Example 10
Source File: PsshAtomUtil.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a PSSH atom. Version 0 and 1 PSSH atoms are supported.
 *
 * @param atom The atom to parse.
 * @return The parsed PSSH atom. Null if the input is not a valid PSSH atom, or if the PSSH atom
 *     has an unsupported version.
 */
// TODO: Support parsing of the key ids for version 1 PSSH atoms.
private static @Nullable PsshAtom parsePsshAtom(byte[] atom) {
  ParsableByteArray atomData = new ParsableByteArray(atom);
  if (atomData.limit() < Atom.FULL_HEADER_SIZE + 16 /* UUID */ + 4 /* DataSize */) {
    // Data too short.
    return null;
  }
  atomData.setPosition(0);
  int atomSize = atomData.readInt();
  if (atomSize != atomData.bytesLeft() + 4) {
    // Not an atom, or incorrect atom size.
    return null;
  }
  int atomType = atomData.readInt();
  if (atomType != Atom.TYPE_pssh) {
    // Not an atom, or incorrect atom type.
    return null;
  }
  int atomVersion = Atom.parseFullAtomVersion(atomData.readInt());
  if (atomVersion > 1) {
    Log.w(TAG, "Unsupported pssh version: " + atomVersion);
    return null;
  }
  UUID uuid = new UUID(atomData.readLong(), atomData.readLong());
  if (atomVersion == 1) {
    int keyIdCount = atomData.readUnsignedIntToInt();
    atomData.skipBytes(16 * keyIdCount);
  }
  int dataSize = atomData.readUnsignedIntToInt();
  if (dataSize != atomData.bytesLeft()) {
    // Incorrect dataSize.
    return null;
  }
  byte[] data = new byte[dataSize];
  atomData.readBytes(data, 0, dataSize);
  return new PsshAtom(uuid, atomVersion, data);
}
 
Example 11
Source File: AtomParsers.java    From Telegram 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 12
Source File: FragmentedMp4Extractor.java    From K-Sonic with MIT License 4 votes vote down vote up
private static void parseSgpd(ParsableByteArray sbgp, ParsableByteArray sgpd, TrackFragment out)
    throws ParserException {
  sbgp.setPosition(Atom.HEADER_SIZE);
  int sbgpFullAtom = sbgp.readInt();
  if (sbgp.readInt() != SAMPLE_GROUP_TYPE_seig) {
    // Only seig grouping type is supported.
    return;
  }
  if (Atom.parseFullAtomVersion(sbgpFullAtom) == 1) {
    sbgp.skipBytes(4);
  }
  if (sbgp.readInt() != 1) {
    throw new ParserException("Entry count in sbgp != 1 (unsupported).");
  }

  sgpd.setPosition(Atom.HEADER_SIZE);
  int sgpdFullAtom = sgpd.readInt();
  if (sgpd.readInt() != SAMPLE_GROUP_TYPE_seig) {
    // Only seig grouping type is supported.
    return;
  }
  int sgpdVersion = Atom.parseFullAtomVersion(sgpdFullAtom);
  if (sgpdVersion == 1) {
    if (sgpd.readUnsignedInt() == 0) {
      throw new ParserException("Variable length decription in sgpd found (unsupported)");
    }
  } else if (sgpdVersion >= 2) {
    sgpd.skipBytes(4);
  }
  if (sgpd.readUnsignedInt() != 1) {
    throw new ParserException("Entry count in sgpd != 1 (unsupported).");
  }
  // CencSampleEncryptionInformationGroupEntry
  sgpd.skipBytes(2);
  boolean isProtected = sgpd.readUnsignedByte() == 1;
  if (!isProtected) {
    return;
  }
  int initVectorSize = sgpd.readUnsignedByte();
  byte[] keyId = new byte[16];
  sgpd.readBytes(keyId, 0, keyId.length);
  out.definesEncryptionData = true;
  out.trackEncryptionBox = new TrackEncryptionBox(isProtected, initVectorSize, keyId);
}
 
Example 13
Source File: FragmentedMp4Extractor.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private static void parseSgpd(ParsableByteArray sbgp, ParsableByteArray sgpd, String schemeType,
    TrackFragment out) throws ParserException {
  sbgp.setPosition(Atom.HEADER_SIZE);
  int sbgpFullAtom = sbgp.readInt();
  if (sbgp.readInt() != SAMPLE_GROUP_TYPE_seig) {
    // Only seig grouping type is supported.
    return;
  }
  if (Atom.parseFullAtomVersion(sbgpFullAtom) == 1) {
    sbgp.skipBytes(4); // default_length.
  }
  if (sbgp.readInt() != 1) { // entry_count.
    throw new ParserException("Entry count in sbgp != 1 (unsupported).");
  }

  sgpd.setPosition(Atom.HEADER_SIZE);
  int sgpdFullAtom = sgpd.readInt();
  if (sgpd.readInt() != SAMPLE_GROUP_TYPE_seig) {
    // Only seig grouping type is supported.
    return;
  }
  int sgpdVersion = Atom.parseFullAtomVersion(sgpdFullAtom);
  if (sgpdVersion == 1) {
    if (sgpd.readUnsignedInt() == 0) {
      throw new ParserException("Variable length description in sgpd found (unsupported)");
    }
  } else if (sgpdVersion >= 2) {
    sgpd.skipBytes(4); // default_sample_description_index.
  }
  if (sgpd.readUnsignedInt() != 1) { // entry_count.
    throw new ParserException("Entry count in sgpd != 1 (unsupported).");
  }
  // CencSampleEncryptionInformationGroupEntry
  sgpd.skipBytes(1); // reserved = 0.
  int patternByte = sgpd.readUnsignedByte();
  int cryptByteBlock = (patternByte & 0xF0) >> 4;
  int skipByteBlock = patternByte & 0x0F;
  boolean isProtected = sgpd.readUnsignedByte() == 1;
  if (!isProtected) {
    return;
  }
  int perSampleIvSize = sgpd.readUnsignedByte();
  byte[] keyId = new byte[16];
  sgpd.readBytes(keyId, 0, keyId.length);
  byte[] constantIv = null;
  if (isProtected && perSampleIvSize == 0) {
    int constantIvSize = sgpd.readUnsignedByte();
    constantIv = new byte[constantIvSize];
    sgpd.readBytes(constantIv, 0, constantIvSize);
  }
  out.definesEncryptionData = true;
  out.trackEncryptionBox = new TrackEncryptionBox(isProtected, schemeType, perSampleIvSize, keyId,
      cryptByteBlock, skipByteBlock, constantIv);
}
 
Example 14
Source File: TrackFragment.java    From K-Sonic with MIT License 4 votes vote down vote up
/**
 * Fills {@link #sampleEncryptionData} from the provided source.
 *
 * @param source A source from which to read the encryption data.
 */
public void fillEncryptionData(ParsableByteArray source) {
  source.readBytes(sampleEncryptionData.data, 0, sampleEncryptionDataLength);
  sampleEncryptionData.setPosition(0);
  sampleEncryptionDataNeedsFill = false;
}
 
Example 15
Source File: VideoTagPayloadReader.java    From LiveVideoBroadcaster with Apache License 2.0 4 votes vote down vote up
@Override
protected void parsePayload(ParsableByteArray data, long timeUs) throws ParserException {
  int packetType = data.readUnsignedByte();
  int compositionTimeMs = data.readUnsignedInt24();
  timeUs += compositionTimeMs * 1000L;
  // Parse avc sequence header in case this was not done before.
  if (packetType == AVC_PACKET_TYPE_SEQUENCE_HEADER && !hasOutputFormat) {
    ParsableByteArray videoSequence = new ParsableByteArray(new byte[data.bytesLeft()]);
    data.readBytes(videoSequence.data, 0, data.bytesLeft());
    AvcConfig avcConfig = AvcConfig.parse(videoSequence);
    nalUnitLengthFieldLength = avcConfig.nalUnitLengthFieldLength;
    // Construct and output the format.
    Format format = Format.createVideoSampleFormat(null, MimeTypes.VIDEO_H264, null,
        Format.NO_VALUE, Format.NO_VALUE, avcConfig.width, avcConfig.height, Format.NO_VALUE,
        avcConfig.initializationData, Format.NO_VALUE, avcConfig.pixelWidthAspectRatio, null);
    output.format(format);
    hasOutputFormat = true;
  } else if (packetType == AVC_PACKET_TYPE_AVC_NALU) {
    // TODO: Deduplicate with Mp4Extractor.
    // Zero the top three bytes of the array that we'll use to decode nal unit lengths, in case
    // they're only 1 or 2 bytes long.
    byte[] nalLengthData = nalLength.data;
    nalLengthData[0] = 0;
    nalLengthData[1] = 0;
    nalLengthData[2] = 0;
    int nalUnitLengthFieldLengthDiff = 4 - nalUnitLengthFieldLength;
    // NAL units are length delimited, but the decoder requires start code delimited units.
    // Loop until we've written the sample to the track output, replacing length delimiters with
    // start codes as we encounter them.
    int bytesWritten = 0;
    int bytesToWrite;
    while (data.bytesLeft() > 0) {
      // Read the NAL length so that we know where we find the next one.
      data.readBytes(nalLength.data, nalUnitLengthFieldLengthDiff, nalUnitLengthFieldLength);
      nalLength.setPosition(0);
      bytesToWrite = nalLength.readUnsignedIntToInt();

      // Write a start code for the current NAL unit.
      nalStartCode.setPosition(0);
      output.sampleData(nalStartCode, 4);
      bytesWritten += 4;

      // Write the payload of the NAL unit.
      output.sampleData(data, bytesToWrite);
      bytesWritten += bytesToWrite;
    }
    output.sampleMetadata(timeUs, frameType == VIDEO_FRAME_KEYFRAME ? C.BUFFER_FLAG_KEY_FRAME : 0,
        bytesWritten, 0, null);
  }
}
 
Example 16
Source File: SectionReader.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void consume(ParsableByteArray data, @Flags int flags) {
  boolean payloadUnitStartIndicator = (flags & FLAG_PAYLOAD_UNIT_START_INDICATOR) != 0;
  int payloadStartPosition = C.POSITION_UNSET;
  if (payloadUnitStartIndicator) {
    int payloadStartOffset = data.readUnsignedByte();
    payloadStartPosition = data.getPosition() + payloadStartOffset;
  }

  if (waitingForPayloadStart) {
    if (!payloadUnitStartIndicator) {
      return;
    }
    waitingForPayloadStart = false;
    data.setPosition(payloadStartPosition);
    bytesRead = 0;
  }

  while (data.bytesLeft() > 0) {
    if (bytesRead < SECTION_HEADER_LENGTH) {
      // Note: see ISO/IEC 13818-1, section 2.4.4.3 for detailed information on the format of
      // the header.
      if (bytesRead == 0) {
        int tableId = data.readUnsignedByte();
        data.setPosition(data.getPosition() - 1);
        if (tableId == 0xFF /* forbidden value */) {
          // No more sections in this ts packet.
          waitingForPayloadStart = true;
          return;
        }
      }
      int headerBytesToRead = Math.min(data.bytesLeft(), SECTION_HEADER_LENGTH - bytesRead);
      data.readBytes(sectionData.data, bytesRead, headerBytesToRead);
      bytesRead += headerBytesToRead;
      if (bytesRead == SECTION_HEADER_LENGTH) {
        sectionData.reset(SECTION_HEADER_LENGTH);
        sectionData.skipBytes(1); // Skip table id (8).
        int secondHeaderByte = sectionData.readUnsignedByte();
        int thirdHeaderByte = sectionData.readUnsignedByte();
        sectionSyntaxIndicator = (secondHeaderByte & 0x80) != 0;
        totalSectionLength =
            (((secondHeaderByte & 0x0F) << 8) | thirdHeaderByte) + SECTION_HEADER_LENGTH;
        if (sectionData.capacity() < totalSectionLength) {
          // Ensure there is enough space to keep the whole section.
          byte[] bytes = sectionData.data;
          sectionData.reset(
              Math.min(MAX_SECTION_LENGTH, Math.max(totalSectionLength, bytes.length * 2)));
          System.arraycopy(bytes, 0, sectionData.data, 0, SECTION_HEADER_LENGTH);
        }
      }
    } else {
      // Reading the body.
      int bodyBytesToRead = Math.min(data.bytesLeft(), totalSectionLength - bytesRead);
      data.readBytes(sectionData.data, bytesRead, bodyBytesToRead);
      bytesRead += bodyBytesToRead;
      if (bytesRead == totalSectionLength) {
        if (sectionSyntaxIndicator) {
          // This section has common syntax as defined in ISO/IEC 13818-1, section 2.4.4.11.
          if (Util.crc(sectionData.data, 0, totalSectionLength, 0xFFFFFFFF) != 0) {
            // The CRC is invalid so discard the section.
            waitingForPayloadStart = true;
            return;
          }
          sectionData.reset(totalSectionLength - 4); // Exclude the CRC_32 field.
        } else {
          // This is a private section with private defined syntax.
          sectionData.reset(totalSectionLength);
        }
        reader.consume(sectionData);
        bytesRead = 0;
      }
    }
  }
}
 
Example 17
Source File: FragmentedMp4Extractor.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private static void parseSgpd(ParsableByteArray sbgp, ParsableByteArray sgpd, String schemeType,
    TrackFragment out) throws ParserException {
  sbgp.setPosition(Atom.HEADER_SIZE);
  int sbgpFullAtom = sbgp.readInt();
  if (sbgp.readInt() != SAMPLE_GROUP_TYPE_seig) {
    // Only seig grouping type is supported.
    return;
  }
  if (Atom.parseFullAtomVersion(sbgpFullAtom) == 1) {
    sbgp.skipBytes(4); // default_length.
  }
  if (sbgp.readInt() != 1) { // entry_count.
    throw new ParserException("Entry count in sbgp != 1 (unsupported).");
  }

  sgpd.setPosition(Atom.HEADER_SIZE);
  int sgpdFullAtom = sgpd.readInt();
  if (sgpd.readInt() != SAMPLE_GROUP_TYPE_seig) {
    // Only seig grouping type is supported.
    return;
  }
  int sgpdVersion = Atom.parseFullAtomVersion(sgpdFullAtom);
  if (sgpdVersion == 1) {
    if (sgpd.readUnsignedInt() == 0) {
      throw new ParserException("Variable length description in sgpd found (unsupported)");
    }
  } else if (sgpdVersion >= 2) {
    sgpd.skipBytes(4); // default_sample_description_index.
  }
  if (sgpd.readUnsignedInt() != 1) { // entry_count.
    throw new ParserException("Entry count in sgpd != 1 (unsupported).");
  }
  // CencSampleEncryptionInformationGroupEntry
  sgpd.skipBytes(1); // reserved = 0.
  int patternByte = sgpd.readUnsignedByte();
  int cryptByteBlock = (patternByte & 0xF0) >> 4;
  int skipByteBlock = patternByte & 0x0F;
  boolean isProtected = sgpd.readUnsignedByte() == 1;
  if (!isProtected) {
    return;
  }
  int perSampleIvSize = sgpd.readUnsignedByte();
  byte[] keyId = new byte[16];
  sgpd.readBytes(keyId, 0, keyId.length);
  byte[] constantIv = null;
  if (perSampleIvSize == 0) {
    int constantIvSize = sgpd.readUnsignedByte();
    constantIv = new byte[constantIvSize];
    sgpd.readBytes(constantIv, 0, constantIvSize);
  }
  out.definesEncryptionData = true;
  out.trackEncryptionBox = new TrackEncryptionBox(isProtected, schemeType, perSampleIvSize, keyId,
      cryptByteBlock, skipByteBlock, constantIv);
}
 
Example 18
Source File: AdtsReader.java    From Telegram with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Continues a read from the provided {@code source} into a given {@code target}. It's assumed
 * that the data should be written into {@code target} starting from an offset of zero.
 *
 * @param source The source from which to read.
 * @param target The target into which data is to be read.
 * @param targetLength The target length of the read.
 * @return Whether the target length was reached.
 */
private boolean continueRead(ParsableByteArray source, byte[] target, int targetLength) {
  int bytesToRead = Math.min(source.bytesLeft(), targetLength - bytesRead);
  source.readBytes(target, bytesRead, bytesToRead);
  bytesRead += bytesToRead;
  return bytesRead == targetLength;
}
 
Example 19
Source File: AdtsReader.java    From MediaSDK with Apache License 2.0 3 votes vote down vote up
/**
 * Continues a read from the provided {@code source} into a given {@code target}. It's assumed
 * that the data should be written into {@code target} starting from an offset of zero.
 *
 * @param source The source from which to read.
 * @param target The target into which data is to be read.
 * @param targetLength The target length of the read.
 * @return Whether the target length was reached.
 */
private boolean continueRead(ParsableByteArray source, byte[] target, int targetLength) {
  int bytesToRead = Math.min(source.bytesLeft(), targetLength - bytesRead);
  source.readBytes(target, bytesRead, bytesToRead);
  bytesRead += bytesToRead;
  return bytesRead == targetLength;
}
 
Example 20
Source File: Ac4Reader.java    From Telegram with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Continues a read from the provided {@code source} into a given {@code target}. It's assumed
 * that the data should be written into {@code target} starting from an offset of zero.
 *
 * @param source The source from which to read.
 * @param target The target into which data is to be read.
 * @param targetLength The target length of the read.
 * @return Whether the target length was reached.
 */
private boolean continueRead(ParsableByteArray source, byte[] target, int targetLength) {
  int bytesToRead = Math.min(source.bytesLeft(), targetLength - bytesRead);
  source.readBytes(target, bytesRead, bytesToRead);
  bytesRead += bytesToRead;
  return bytesRead == targetLength;
}