com.google.android.exoplayer2.util.ParsableBitArray Java Examples

The following examples show how to use com.google.android.exoplayer2.util.ParsableBitArray. 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: DtsUtil.java    From K-Sonic with MIT License 6 votes vote down vote up
/**
 * Returns the DTS format given {@code data} containing the DTS frame according to ETSI TS 102 114
 * subsections 5.3/5.4.
 *
 * @param frame The DTS frame 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 DTS format parsed from data in the header.
 */
public static Format parseDtsFormat(byte[] frame, String trackId, String language,
    DrmInitData drmInitData) {
  ParsableBitArray frameBits = new ParsableBitArray(frame);
  frameBits.skipBits(4 * 8 + 1 + 5 + 1 + 7 + 14); // SYNC, FTYPE, SHORT, CPF, NBLKS, FSIZE
  int amode = frameBits.readBits(6);
  int channelCount = CHANNELS_BY_AMODE[amode];
  int sfreq = frameBits.readBits(4);
  int sampleRate = SAMPLE_RATE_BY_SFREQ[sfreq];
  int rate = frameBits.readBits(5);
  int bitrate = rate >= TWICE_BITRATE_KBPS_BY_RATE.length ? Format.NO_VALUE
      : TWICE_BITRATE_KBPS_BY_RATE[rate] * 1000 / 2;
  frameBits.skipBits(10); // MIX, DYNF, TIMEF, AUXF, HDCD, EXT_AUDIO_ID, EXT_AUDIO, ASPF
  channelCount += frameBits.readBits(2) > 0 ? 1 : 0; // LFF
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_DTS, null, bitrate,
      Format.NO_VALUE, channelCount, sampleRate, null, drmInitData, 0, language);
}
 
Example #2
Source File: DvbParser.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses a page composition segment, as defined by ETSI EN 300 743 7.2.2.
 */
private static PageComposition parsePageComposition(ParsableBitArray data, int length) {
  int timeoutSecs = data.readBits(8);
  int version = data.readBits(4);
  int state = data.readBits(2);
  data.skipBits(2);
  int remainingLength = length - 2;

  SparseArray<PageRegion> regions = new SparseArray<>();
  while (remainingLength > 0) {
    int regionId = data.readBits(8);
    data.skipBits(8); // Skip reserved.
    int regionHorizontalAddress = data.readBits(16);
    int regionVerticalAddress = data.readBits(16);
    remainingLength -= 6;
    regions.put(regionId, new PageRegion(regionHorizontalAddress, regionVerticalAddress));
  }

  return new PageComposition(timeoutSecs, version, state, regions);
}
 
Example #3
Source File: Ac3Util.java    From K-Sonic with MIT License 6 votes vote down vote up
/**
 * Returns the AC-3 format given {@code data} containing a syncframe. The reading position of
 * {@code data} will be modified.
 *
 * @param data The data to parse, positioned at the start of the syncframe.
 * @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 parseAc3SyncframeFormat(ParsableBitArray data, String trackId,
    String language, DrmInitData drmInitData) {
  data.skipBits(16 + 16); // syncword, crc1
  int fscod = data.readBits(2);
  data.skipBits(6 + 5 + 3); // frmsizecod, bsid, bsmod
  int acmod = data.readBits(3);
  if ((acmod & 0x01) != 0 && acmod != 1) {
    data.skipBits(2); // cmixlev
  }
  if ((acmod & 0x04) != 0) {
    data.skipBits(2); // surmixlev
  }
  if (acmod == 2) {
    data.skipBits(2); // dsurmod
  }
  boolean lfeon = data.readBit();
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_AC3, null, Format.NO_VALUE,
      Format.NO_VALUE, CHANNEL_COUNT_BY_ACMOD[acmod] + (lfeon ? 1 : 0),
      SAMPLE_RATE_BY_FSCOD[fscod], null, drmInitData, 0, language);
}
 
Example #4
Source File: Ac3Util.java    From K-Sonic with MIT License 6 votes vote down vote up
/**
 * Returns the E-AC-3 format given {@code data} containing a syncframe. The reading position of
 * {@code data} will be modified.
 *
 * @param data The data to parse, positioned at the start of the syncframe.
 * @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 E-AC-3 format parsed from data in the header.
 */
public static Format parseEac3SyncframeFormat(ParsableBitArray data, String trackId,
    String language, DrmInitData drmInitData) {
  data.skipBits(16 + 2 + 3 + 11); // syncword, strmtype, substreamid, frmsiz
  int sampleRate;
  int fscod = data.readBits(2);
  if (fscod == 3) {
    sampleRate = SAMPLE_RATE_BY_FSCOD2[data.readBits(2)];
  } else {
    data.skipBits(2); // numblkscod
    sampleRate = SAMPLE_RATE_BY_FSCOD[fscod];
  }
  int acmod = data.readBits(3);
  boolean lfeon = data.readBit();
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_E_AC3, null, Format.NO_VALUE,
      Format.NO_VALUE, CHANNEL_COUNT_BY_ACMOD[acmod] + (lfeon ? 1 : 0), sampleRate, null,
      drmInitData, 0, language);
}
 
Example #5
Source File: LatmReader.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private void parseFrameLength(ParsableBitArray data) {
  frameLengthType = data.readBits(3);
  switch (frameLengthType) {
    case 0:
      data.skipBits(8); // latmBufferFullness.
      break;
    case 1:
      data.skipBits(9); // frameLength.
      break;
    case 3:
    case 4:
    case 5:
      data.skipBits(6); // CELPframeLengthTableIndex.
      break;
    case 6:
    case 7:
      data.skipBits(1); // HVXCframeLengthTableIndex.
      break;
    default:
      throw new IllegalStateException();
  }
}
 
Example #6
Source File: LatmReader.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses an AudioMuxElement as defined in 14496-3:2009, Section 1.7.3.1, Table 1.41.
 *
 * @param data A {@link ParsableBitArray} containing the AudioMuxElement's bytes.
 */
private void parseAudioMuxElement(ParsableBitArray data) throws ParserException {
  boolean useSameStreamMux = data.readBit();
  if (!useSameStreamMux) {
    streamMuxRead = true;
    parseStreamMuxConfig(data);
  } else if (!streamMuxRead) {
    return; // Parsing cannot continue without StreamMuxConfig information.
  }

  if (audioMuxVersionA == 0) {
    if (numSubframes != 0) {
      throw new ParserException();
    }
    int muxSlotLengthBytes = parsePayloadLengthInfo(data);
    parsePayloadMux(data, muxSlotLengthBytes);
    if (otherDataPresent) {
      data.skipBits((int) otherDataLenBits);
    }
  } else {
    throw new ParserException(); // Not defined by ISO/IEC 14496-3:2009.
  }
}
 
Example #7
Source File: DtsUtil.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the DTS format given {@code data} containing the DTS frame according to ETSI TS 102 114
 * subsections 5.3/5.4.
 *
 * @param frame The DTS frame 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 DTS format parsed from data in the header.
 */
public static Format parseDtsFormat(
    byte[] frame, String trackId, String language, DrmInitData drmInitData) {
  ParsableBitArray frameBits = getNormalizedFrameHeader(frame);
  frameBits.skipBits(32 + 1 + 5 + 1 + 7 + 14); // SYNC, FTYPE, SHORT, CPF, NBLKS, FSIZE
  int amode = frameBits.readBits(6);
  int channelCount = CHANNELS_BY_AMODE[amode];
  int sfreq = frameBits.readBits(4);
  int sampleRate = SAMPLE_RATE_BY_SFREQ[sfreq];
  int rate = frameBits.readBits(5);
  int bitrate = rate >= TWICE_BITRATE_KBPS_BY_RATE.length ? Format.NO_VALUE
      : TWICE_BITRATE_KBPS_BY_RATE[rate] * 1000 / 2;
  frameBits.skipBits(10); // MIX, DYNF, TIMEF, AUXF, HDCD, EXT_AUDIO_ID, EXT_AUDIO, ASPF
  channelCount += frameBits.readBits(2) > 0 ? 1 : 0; // LFF
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_DTS, null, bitrate,
      Format.NO_VALUE, channelCount, sampleRate, null, drmInitData, 0, language);
}
 
Example #8
Source File: LatmReader.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private void parsePayloadMux(ParsableBitArray data, int muxLengthBytes) {
  // The start of sample data in
  int bitPosition = data.getPosition();
  if ((bitPosition & 0x07) == 0) {
    // Sample data is byte-aligned. We can output it directly.
    sampleDataBuffer.setPosition(bitPosition >> 3);
  } else {
    // Sample data is not byte-aligned and we need align it ourselves before outputting.
    // Byte alignment is needed because LATM framing is not supported by MediaCodec.
    data.readBits(sampleDataBuffer.data, 0, muxLengthBytes * 8);
    sampleDataBuffer.setPosition(0);
  }
  output.sampleData(sampleDataBuffer, muxLengthBytes);
  output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, muxLengthBytes, 0, null);
  timeUs += sampleDurationUs;
}
 
Example #9
Source File: DtsUtil.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the DTS format given {@code data} containing the DTS frame according to ETSI TS 102 114
 * subsections 5.3/5.4.
 *
 * @param frame The DTS frame 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 DTS format parsed from data in the header.
 */
public static Format parseDtsFormat(
    byte[] frame, String trackId, @Nullable String language, @Nullable DrmInitData drmInitData) {
  ParsableBitArray frameBits = getNormalizedFrameHeader(frame);
  frameBits.skipBits(32 + 1 + 5 + 1 + 7 + 14); // SYNC, FTYPE, SHORT, CPF, NBLKS, FSIZE
  int amode = frameBits.readBits(6);
  int channelCount = CHANNELS_BY_AMODE[amode];
  int sfreq = frameBits.readBits(4);
  int sampleRate = SAMPLE_RATE_BY_SFREQ[sfreq];
  int rate = frameBits.readBits(5);
  int bitrate = rate >= TWICE_BITRATE_KBPS_BY_RATE.length ? Format.NO_VALUE
      : TWICE_BITRATE_KBPS_BY_RATE[rate] * 1000 / 2;
  frameBits.skipBits(10); // MIX, DYNF, TIMEF, AUXF, HDCD, EXT_AUDIO_ID, EXT_AUDIO, ASPF
  channelCount += frameBits.readBits(2) > 0 ? 1 : 0; // LFF
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_DTS, null, bitrate,
      Format.NO_VALUE, channelCount, sampleRate, null, drmInitData, 0, language);
}
 
Example #10
Source File: DtsUtil.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the DTS format given {@code data} containing the DTS frame according to ETSI TS 102 114
 * subsections 5.3/5.4.
 *
 * @param frame The DTS frame 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 DTS format parsed from data in the header.
 */
public static Format parseDtsFormat(
    byte[] frame, String trackId, String language, DrmInitData drmInitData) {
  ParsableBitArray frameBits = getNormalizedFrameHeader(frame);
  frameBits.skipBits(32 + 1 + 5 + 1 + 7 + 14); // SYNC, FTYPE, SHORT, CPF, NBLKS, FSIZE
  int amode = frameBits.readBits(6);
  int channelCount = CHANNELS_BY_AMODE[amode];
  int sfreq = frameBits.readBits(4);
  int sampleRate = SAMPLE_RATE_BY_SFREQ[sfreq];
  int rate = frameBits.readBits(5);
  int bitrate = rate >= TWICE_BITRATE_KBPS_BY_RATE.length ? Format.NO_VALUE
      : TWICE_BITRATE_KBPS_BY_RATE[rate] * 1000 / 2;
  frameBits.skipBits(10); // MIX, DYNF, TIMEF, AUXF, HDCD, EXT_AUDIO_ID, EXT_AUDIO, ASPF
  channelCount += frameBits.readBits(2) > 0 ? 1 : 0; // LFF
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_DTS, null, bitrate,
      Format.NO_VALUE, channelCount, sampleRate, null, drmInitData, 0, language);
}
 
Example #11
Source File: LatmReader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private void parsePayloadMux(ParsableBitArray data, int muxLengthBytes) {
  // The start of sample data in
  int bitPosition = data.getPosition();
  if ((bitPosition & 0x07) == 0) {
    // Sample data is byte-aligned. We can output it directly.
    sampleDataBuffer.setPosition(bitPosition >> 3);
  } else {
    // Sample data is not byte-aligned and we need align it ourselves before outputting.
    // Byte alignment is needed because LATM framing is not supported by MediaCodec.
    data.readBits(sampleDataBuffer.data, 0, muxLengthBytes * 8);
    sampleDataBuffer.setPosition(0);
  }
  output.sampleData(sampleDataBuffer, muxLengthBytes);
  output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, muxLengthBytes, 0, null);
  timeUs += sampleDurationUs;
}
 
Example #12
Source File: LatmReader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses an AudioMuxElement as defined in 14496-3:2009, Section 1.7.3.1, Table 1.41.
 *
 * @param data A {@link ParsableBitArray} containing the AudioMuxElement's bytes.
 */
private void parseAudioMuxElement(ParsableBitArray data) throws ParserException {
  boolean useSameStreamMux = data.readBit();
  if (!useSameStreamMux) {
    streamMuxRead = true;
    parseStreamMuxConfig(data);
  } else if (!streamMuxRead) {
    return; // Parsing cannot continue without StreamMuxConfig information.
  }

  if (audioMuxVersionA == 0) {
    if (numSubframes != 0) {
      throw new ParserException();
    }
    int muxSlotLengthBytes = parsePayloadLengthInfo(data);
    parsePayloadMux(data, muxSlotLengthBytes);
    if (otherDataPresent) {
      data.skipBits((int) otherDataLenBits);
    }
  } else {
    throw new ParserException(); // Not defined by ISO/IEC 14496-3:2009.
  }
}
 
Example #13
Source File: DvbParser.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses a page composition segment, as defined by ETSI EN 300 743 7.2.2.
 */
private static PageComposition parsePageComposition(ParsableBitArray data, int length) {
  int timeoutSecs = data.readBits(8);
  int version = data.readBits(4);
  int state = data.readBits(2);
  data.skipBits(2);
  int remainingLength = length - 2;

  SparseArray<PageRegion> regions = new SparseArray<>();
  while (remainingLength > 0) {
    int regionId = data.readBits(8);
    data.skipBits(8); // Skip reserved.
    int regionHorizontalAddress = data.readBits(16);
    int regionVerticalAddress = data.readBits(16);
    remainingLength -= 6;
    regions.put(regionId, new PageRegion(regionHorizontalAddress, regionVerticalAddress));
  }

  return new PageComposition(timeoutSecs, version, state, regions);
}
 
Example #14
Source File: LatmReader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private int parseAudioSpecificConfig(ParsableBitArray data) throws ParserException {
  int bitsLeft = data.bitsLeft();
  Pair<Integer, Integer> config = CodecSpecificDataUtil.parseAacAudioSpecificConfig(data, true);
  sampleRateHz = config.first;
  channelCount = config.second;
  return bitsLeft - data.bitsLeft();
}
 
Example #15
Source File: LatmReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private int parseAudioSpecificConfig(ParsableBitArray data) throws ParserException {
  int bitsLeft = data.bitsLeft();
  Pair<Integer, Integer> config = CodecSpecificDataUtil.parseAacAudioSpecificConfig(data, true);
  sampleRateHz = config.first;
  channelCount = config.second;
  return bitsLeft - data.bitsLeft();
}
 
Example #16
Source File: AdtsExtractor.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new extractor for ADTS bitstreams.
 *
 * @param flags Flags that control the extractor's behavior.
 */
public AdtsExtractor(@Flags int flags) {
  this.flags = flags;
  reader = new AdtsReader(true);
  packetBuffer = new ParsableByteArray(MAX_PACKET_SIZE);
  averageFrameSize = C.LENGTH_UNSET;
  firstFramePosition = C.POSITION_UNSET;
  // 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);
}
 
Example #17
Source File: Ac4Util.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads the number of audio samples represented by the given AC-4 syncframe. The buffer's
 * position is not modified.
 *
 * @param buffer The {@link ByteBuffer} from which to read the syncframe.
 * @return The number of audio samples represented by the syncframe.
 */
public static int parseAc4SyncframeAudioSampleCount(ByteBuffer buffer) {
  byte[] bufferBytes = new byte[HEADER_SIZE_FOR_PARSER];
  int position = buffer.position();
  buffer.get(bufferBytes);
  buffer.position(position);
  return parseAc4SyncframeInfo(new ParsableBitArray(bufferBytes)).sampleCount;
}
 
Example #18
Source File: DvbParser.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Paint an 8-bit/pixel code string, as defined by ETSI EN 300 743 7.2.5.2, to a canvas.
 */
private static int paint8BitPixelCodeString(ParsableBitArray data, int[] clutEntries,
    byte[] clutMapTable, int column, int line, Paint paint, Canvas canvas) {
  boolean endOfPixelCodeString = false;
  do {
    int runLength = 0;
    int clutIndex = 0;
    int peek = data.readBits(8);
    if (peek != 0x00) {
      runLength = 1;
      clutIndex = peek;
    } else {
      if (!data.readBit()) {
        peek = data.readBits(7);
        if (peek != 0x00) {
          runLength = peek;
          clutIndex = 0x00;
        } else {
          endOfPixelCodeString = true;
        }
      } else {
        runLength = data.readBits(7);
        clutIndex = data.readBits(8);
      }
    }

    if (runLength != 0 && paint != null) {
      paint.setColor(clutEntries[clutMapTable != null ? clutMapTable[clutIndex] : clutIndex]);
      canvas.drawRect(column, line, column + runLength, line + 1, paint);
    }
    column += runLength;
  } while (!endOfPixelCodeString);

  return column;
}
 
Example #19
Source File: Ac4Util.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads the number of audio samples represented by the given AC-4 syncframe. The buffer's
 * position is not modified.
 *
 * @param buffer The {@link ByteBuffer} from which to read the syncframe.
 * @return The number of audio samples represented by the syncframe.
 */
public static int parseAc4SyncframeAudioSampleCount(ByteBuffer buffer) {
  byte[] bufferBytes = new byte[HEADER_SIZE_FOR_PARSER];
  int position = buffer.position();
  buffer.get(bufferBytes);
  buffer.position(position);
  return parseAc4SyncframeInfo(new ParsableBitArray(bufferBytes)).sampleCount;
}
 
Example #20
Source File: LatmReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private int parsePayloadLengthInfo(ParsableBitArray data) throws ParserException {
  int muxSlotLengthBytes = 0;
  // Assuming single program and single layer.
  if (frameLengthType == 0) {
    int tmp;
    do {
      tmp = data.readBits(8);
      muxSlotLengthBytes += tmp;
    } while (tmp == 255);
    return muxSlotLengthBytes;
  } else {
    throw new ParserException();
  }
}
 
Example #21
Source File: DvbParser.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses an object data segment, as defined by ETSI EN 300 743 7.2.5.
 *
 * @return The parsed object data.
 */
private static ObjectData parseObjectData(ParsableBitArray data) {
  int objectId = data.readBits(16);
  data.skipBits(4); // Skip object_version_number
  int objectCodingMethod = data.readBits(2);
  boolean nonModifyingColorFlag = data.readBit();
  data.skipBits(1); // Skip reserved.

  byte[] topFieldData = null;
  byte[] bottomFieldData = null;

  if (objectCodingMethod == OBJECT_CODING_STRING) {
    int numberOfCodes = data.readBits(8);
    // TODO: Parse and use character_codes.
    data.skipBits(numberOfCodes * 16); // Skip character_codes.
  } else if (objectCodingMethod == OBJECT_CODING_PIXELS) {
    int topFieldDataLength = data.readBits(16);
    int bottomFieldDataLength = data.readBits(16);
    if (topFieldDataLength > 0) {
      topFieldData = new byte[topFieldDataLength];
      data.readBytes(topFieldData, 0, topFieldDataLength);
    }
    if (bottomFieldDataLength > 0) {
      bottomFieldData = new byte[bottomFieldDataLength];
      data.readBytes(bottomFieldData, 0, bottomFieldDataLength);
    } else {
      bottomFieldData = topFieldData;
    }
  }

  return new ObjectData(objectId, nonModifyingColorFlag, topFieldData, bottomFieldData);
}
 
Example #22
Source File: DvbParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] buildClutMapTable(int length, int bitsPerEntry, ParsableBitArray data) {
  byte[] clutMapTable = new byte[length];
  for (int i = 0; i < length; i++) {
    clutMapTable[i] = (byte) data.readBits(bitsPerEntry);
  }
  return clutMapTable;
}
 
Example #23
Source File: Cea708Decoder.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public Cea708Decoder(int accessibilityChannel, List<byte[]> initializationData) {
  ccData = new ParsableByteArray();
  serviceBlockPacket = new ParsableBitArray();
  selectedServiceNumber = accessibilityChannel == Format.NO_VALUE ? 1 : accessibilityChannel;

  cueBuilders = new CueBuilder[NUM_WINDOWS];
  for (int i = 0; i < NUM_WINDOWS; i++) {
    cueBuilders[i] = new CueBuilder();
  }

  currentCueBuilder = cueBuilders[0];
  resetCueBuilders();
}
 
Example #24
Source File: DvbParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses an object data segment, as defined by ETSI EN 300 743 7.2.5.
 *
 * @return The parsed object data.
 */
private static ObjectData parseObjectData(ParsableBitArray data) {
  int objectId = data.readBits(16);
  data.skipBits(4); // Skip object_version_number
  int objectCodingMethod = data.readBits(2);
  boolean nonModifyingColorFlag = data.readBit();
  data.skipBits(1); // Skip reserved.

  byte[] topFieldData = null;
  byte[] bottomFieldData = null;

  if (objectCodingMethod == OBJECT_CODING_STRING) {
    int numberOfCodes = data.readBits(8);
    // TODO: Parse and use character_codes.
    data.skipBits(numberOfCodes * 16); // Skip character_codes.
  } else if (objectCodingMethod == OBJECT_CODING_PIXELS) {
    int topFieldDataLength = data.readBits(16);
    int bottomFieldDataLength = data.readBits(16);
    if (topFieldDataLength > 0) {
      topFieldData = new byte[topFieldDataLength];
      data.readBytes(topFieldData, 0, topFieldDataLength);
    }
    if (bottomFieldDataLength > 0) {
      bottomFieldData = new byte[bottomFieldDataLength];
      data.readBytes(bottomFieldData, 0, bottomFieldDataLength);
    } else {
      bottomFieldData = topFieldData;
    }
  }

  return new ObjectData(objectId, nonModifyingColorFlag, topFieldData, bottomFieldData);
}
 
Example #25
Source File: DtsUtil.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private static ParsableBitArray getNormalizedFrameHeader(byte[] frameHeader) {
  if (frameHeader[0] == FIRST_BYTE_BE) {
    // The frame is already 16-bit mode, big endian.
    return new ParsableBitArray(frameHeader);
  }
  // Data is not normalized, but we don't want to modify frameHeader.
  frameHeader = Arrays.copyOf(frameHeader, frameHeader.length);
  if (isLittleEndianFrameHeader(frameHeader)) {
    // Change endianness.
    for (int i = 0; i < frameHeader.length - 1; i += 2) {
      byte temp = frameHeader[i];
      frameHeader[i] = frameHeader[i + 1];
      frameHeader[i + 1] = temp;
    }
  }
  ParsableBitArray frameBits = new ParsableBitArray(frameHeader);
  if (frameHeader[0] == (byte) (SYNC_VALUE_14B_BE >> 24)) {
    // Discard the 2 most significant bits of each 16 bit word.
    ParsableBitArray scratchBits = new ParsableBitArray(frameHeader);
    while (scratchBits.bitsLeft() >= 16) {
      scratchBits.skipBits(2);
      frameBits.putInt(scratchBits.readBits(14), 14);
    }
  }
  frameBits.reset(frameHeader);
  return frameBits;
}
 
Example #26
Source File: Ac3Reader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new reader for (E-)AC-3 elementary streams.
 *
 * @param language Track language.
 */
public Ac3Reader(String language) {
  headerScratchBits = new ParsableBitArray(new byte[HEADER_SIZE]);
  headerScratchBytes = new ParsableByteArray(headerScratchBits.data);
  state = STATE_FINDING_SYNC;
  this.language = language;
}
 
Example #27
Source File: AdtsReader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param exposeId3 True if the reader should expose ID3 information.
 * @param language Track language.
 */
public AdtsReader(boolean exposeId3, String language) {
  adtsScratch = new ParsableBitArray(new byte[HEADER_SIZE + CRC_SIZE]);
  id3HeaderBuffer = new ParsableByteArray(Arrays.copyOf(ID3_IDENTIFIER, ID3_HEADER_SIZE));
  setFindingSampleState();
  firstFrameVersion = VERSION_UNSET;
  firstFrameSampleRateIndex = C.INDEX_UNSET;
  sampleDurationUs = C.TIME_UNSET;
  this.exposeId3 = exposeId3;
  this.language = language;
}
 
Example #28
Source File: LatmReader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private int parsePayloadLengthInfo(ParsableBitArray data) throws ParserException {
  int muxSlotLengthBytes = 0;
  // Assuming single program and single layer.
  if (frameLengthType == 0) {
    int tmp;
    do {
      tmp = data.readBits(8);
      muxSlotLengthBytes += tmp;
    } while (tmp == 255);
    return muxSlotLengthBytes;
  } else {
    throw new ParserException();
  }
}
 
Example #29
Source File: Ac3Reader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new reader for (E-)AC-3 elementary streams.
 *
 * @param language Track language.
 */
public Ac3Reader(String language) {
  headerScratchBits = new ParsableBitArray(new byte[HEADER_SIZE]);
  headerScratchBytes = new ParsableByteArray(headerScratchBits.data);
  state = STATE_FINDING_SYNC;
  this.language = language;
}
 
Example #30
Source File: DvbParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a display definition segment, as defined by ETSI EN 300 743 7.2.1.
 */
private static DisplayDefinition parseDisplayDefinition(ParsableBitArray data) {
  data.skipBits(4); // dds_version_number (4).
  boolean displayWindowFlag = data.readBit();
  data.skipBits(3); // Skip reserved.
  int width = data.readBits(16);
  int height = data.readBits(16);

  int horizontalPositionMinimum;
  int horizontalPositionMaximum;
  int verticalPositionMinimum;
  int verticalPositionMaximum;
  if (displayWindowFlag) {
    horizontalPositionMinimum = data.readBits(16);
    horizontalPositionMaximum = data.readBits(16);
    verticalPositionMinimum = data.readBits(16);
    verticalPositionMaximum = data.readBits(16);
  } else {
    horizontalPositionMinimum = 0;
    horizontalPositionMaximum = width;
    verticalPositionMinimum = 0;
    verticalPositionMaximum = height;
  }

  return new DisplayDefinition(width, height, horizontalPositionMinimum,
      horizontalPositionMaximum, verticalPositionMinimum, verticalPositionMaximum);
}