com.google.android.exoplayer2.audio.Ac4Util Java Examples

The following examples show how to use com.google.android.exoplayer2.audio.Ac4Util. 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: Ac4Reader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new reader for AC-4 elementary streams.
 *
 * @param language Track language.
 */
public Ac4Reader(String language) {
  headerScratchBits = new ParsableBitArray(new byte[Ac4Util.HEADER_SIZE_FOR_PARSER]);
  headerScratchBytes = new ParsableByteArray(headerScratchBits.data);
  state = STATE_FINDING_SYNC;
  bytesRead = 0;
  lastByteWasAC = false;
  hasCRC = false;
  this.language = language;
}
 
Example #2
Source File: Ac4Reader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public void consume(ParsableByteArray data) {
  while (data.bytesLeft() > 0) {
    switch (state) {
      case STATE_FINDING_SYNC:
        if (skipToNextSync(data)) {
          state = STATE_READING_HEADER;
          headerScratchBytes.data[0] = (byte) 0xAC;
          headerScratchBytes.data[1] = (byte) (hasCRC ? 0x41 : 0x40);
          bytesRead = 2;
        }
        break;
      case STATE_READING_HEADER:
        if (continueRead(data, headerScratchBytes.data, Ac4Util.HEADER_SIZE_FOR_PARSER)) {
          parseHeader();
          headerScratchBytes.setPosition(0);
          output.sampleData(headerScratchBytes, Ac4Util.HEADER_SIZE_FOR_PARSER);
          state = STATE_READING_SAMPLE;
        }
        break;
      case STATE_READING_SAMPLE:
        int bytesToRead = Math.min(data.bytesLeft(), sampleSize - bytesRead);
        output.sampleData(data, bytesToRead);
        bytesRead += bytesToRead;
        if (bytesRead == sampleSize) {
          output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
          timeUs += sampleDurationUs;
          state = STATE_FINDING_SYNC;
        }
        break;
      default:
        break;
    }
  }
}
 
Example #3
Source File: Ac4Reader.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/** Parses the sample header. */
@SuppressWarnings("ReferenceEquality")
private void parseHeader() {
  headerScratchBits.setPosition(0);
  SyncFrameInfo frameInfo = Ac4Util.parseAc4SyncframeInfo(headerScratchBits);
  if (format == null
      || frameInfo.channelCount != format.channelCount
      || frameInfo.sampleRate != format.sampleRate
      || !MimeTypes.AUDIO_AC4.equals(format.sampleMimeType)) {
    format =
        Format.createAudioSampleFormat(
            trackFormatId,
            MimeTypes.AUDIO_AC4,
            /* codecs= */ null,
            /* bitrate= */ Format.NO_VALUE,
            /* maxInputSize= */ Format.NO_VALUE,
            frameInfo.channelCount,
            frameInfo.sampleRate,
            /* initializationData= */ null,
            /* drmInitData= */ null,
            /* selectionFlags= */ 0,
            language);
    output.format(format);
  }
  sampleSize = frameInfo.frameSize;
  // In this class a sample is an AC-4 sync frame, but Format#sampleRate specifies the number of
  // PCM audio samples per second.
  sampleDurationUs = C.MICROS_PER_SECOND * frameInfo.sampleCount / format.sampleRate;
}
 
Example #4
Source File: Ac4Reader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new reader for AC-4 elementary streams.
 *
 * @param language Track language.
 */
public Ac4Reader(String language) {
  headerScratchBits = new ParsableBitArray(new byte[Ac4Util.HEADER_SIZE_FOR_PARSER]);
  headerScratchBytes = new ParsableByteArray(headerScratchBits.data);
  state = STATE_FINDING_SYNC;
  bytesRead = 0;
  lastByteWasAC = false;
  hasCRC = false;
  this.language = language;
}
 
Example #5
Source File: Ac4Reader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void consume(ParsableByteArray data) {
  while (data.bytesLeft() > 0) {
    switch (state) {
      case STATE_FINDING_SYNC:
        if (skipToNextSync(data)) {
          state = STATE_READING_HEADER;
          headerScratchBytes.data[0] = (byte) 0xAC;
          headerScratchBytes.data[1] = (byte) (hasCRC ? 0x41 : 0x40);
          bytesRead = 2;
        }
        break;
      case STATE_READING_HEADER:
        if (continueRead(data, headerScratchBytes.data, Ac4Util.HEADER_SIZE_FOR_PARSER)) {
          parseHeader();
          headerScratchBytes.setPosition(0);
          output.sampleData(headerScratchBytes, Ac4Util.HEADER_SIZE_FOR_PARSER);
          state = STATE_READING_SAMPLE;
        }
        break;
      case STATE_READING_SAMPLE:
        int bytesToRead = Math.min(data.bytesLeft(), sampleSize - bytesRead);
        output.sampleData(data, bytesToRead);
        bytesRead += bytesToRead;
        if (bytesRead == sampleSize) {
          output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
          timeUs += sampleDurationUs;
          state = STATE_FINDING_SYNC;
        }
        break;
      default:
        break;
    }
  }
}
 
Example #6
Source File: Ac4Reader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/** Parses the sample header. */
@SuppressWarnings("ReferenceEquality")
private void parseHeader() {
  headerScratchBits.setPosition(0);
  SyncFrameInfo frameInfo = Ac4Util.parseAc4SyncframeInfo(headerScratchBits);
  if (format == null
      || frameInfo.channelCount != format.channelCount
      || frameInfo.sampleRate != format.sampleRate
      || !MimeTypes.AUDIO_AC4.equals(format.sampleMimeType)) {
    format =
        Format.createAudioSampleFormat(
            trackFormatId,
            MimeTypes.AUDIO_AC4,
            /* codecs= */ null,
            /* bitrate= */ Format.NO_VALUE,
            /* maxInputSize= */ Format.NO_VALUE,
            frameInfo.channelCount,
            frameInfo.sampleRate,
            /* initializationData= */ null,
            /* drmInitData= */ null,
            /* selectionFlags= */ 0,
            language);
    output.format(format);
  }
  sampleSize = frameInfo.frameSize;
  // In this class a sample is an AC-4 sync frame, but Format#sampleRate specifies the number of
  // PCM audio samples per second.
  sampleDurationUs = C.MICROS_PER_SECOND * frameInfo.sampleCount / format.sampleRate;
}
 
Example #7
Source File: Ac4Reader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new reader for AC-4 elementary streams.
 *
 * @param language Track language.
 */
public Ac4Reader(String language) {
  headerScratchBits = new ParsableBitArray(new byte[Ac4Util.HEADER_SIZE_FOR_PARSER]);
  headerScratchBytes = new ParsableByteArray(headerScratchBits.data);
  state = STATE_FINDING_SYNC;
  bytesRead = 0;
  lastByteWasAC = false;
  hasCRC = false;
  this.language = language;
}
 
Example #8
Source File: Ac4Reader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void consume(ParsableByteArray data) {
  while (data.bytesLeft() > 0) {
    switch (state) {
      case STATE_FINDING_SYNC:
        if (skipToNextSync(data)) {
          state = STATE_READING_HEADER;
          headerScratchBytes.data[0] = (byte) 0xAC;
          headerScratchBytes.data[1] = (byte) (hasCRC ? 0x41 : 0x40);
          bytesRead = 2;
        }
        break;
      case STATE_READING_HEADER:
        if (continueRead(data, headerScratchBytes.data, Ac4Util.HEADER_SIZE_FOR_PARSER)) {
          parseHeader();
          headerScratchBytes.setPosition(0);
          output.sampleData(headerScratchBytes, Ac4Util.HEADER_SIZE_FOR_PARSER);
          state = STATE_READING_SAMPLE;
        }
        break;
      case STATE_READING_SAMPLE:
        int bytesToRead = Math.min(data.bytesLeft(), sampleSize - bytesRead);
        output.sampleData(data, bytesToRead);
        bytesRead += bytesToRead;
        if (bytesRead == sampleSize) {
          output.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
          timeUs += sampleDurationUs;
          state = STATE_FINDING_SYNC;
        }
        break;
      default:
        break;
    }
  }
}
 
Example #9
Source File: Ac4Reader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/** Parses the sample header. */
@SuppressWarnings("ReferenceEquality")
private void parseHeader() {
  headerScratchBits.setPosition(0);
  SyncFrameInfo frameInfo = Ac4Util.parseAc4SyncframeInfo(headerScratchBits);
  if (format == null
      || frameInfo.channelCount != format.channelCount
      || frameInfo.sampleRate != format.sampleRate
      || !MimeTypes.AUDIO_AC4.equals(format.sampleMimeType)) {
    format =
        Format.createAudioSampleFormat(
            trackFormatId,
            MimeTypes.AUDIO_AC4,
            /* codecs= */ null,
            /* bitrate= */ Format.NO_VALUE,
            /* maxInputSize= */ Format.NO_VALUE,
            frameInfo.channelCount,
            frameInfo.sampleRate,
            /* initializationData= */ null,
            /* drmInitData= */ null,
            /* selectionFlags= */ 0,
            language);
    output.format(format);
  }
  sampleSize = frameInfo.frameSize;
  // In this class a sample is an AC-4 sync frame, but Format#sampleRate specifies the number of
  // PCM audio samples per second.
  sampleDurationUs = C.MICROS_PER_SECOND * frameInfo.sampleCount / format.sampleRate;
}
 
Example #10
Source File: Ac4Extractor.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
  // Skip any ID3 headers.
  ParsableByteArray scratch = new ParsableByteArray(ID3_HEADER_LENGTH);
  int startPosition = 0;
  while (true) {
    input.peekFully(scratch.data, /* offset= */ 0, ID3_HEADER_LENGTH);
    scratch.setPosition(0);
    if (scratch.readUnsignedInt24() != ID3_TAG) {
      break;
    }
    scratch.skipBytes(3); // version, flags
    int length = scratch.readSynchSafeInt();
    startPosition += 10 + length;
    input.advancePeekPosition(length);
  }
  input.resetPeekPosition();
  input.advancePeekPosition(startPosition);

  int headerPosition = startPosition;
  int validFramesCount = 0;
  while (true) {
    input.peekFully(scratch.data, /* offset= */ 0, /* length= */ FRAME_HEADER_SIZE);
    scratch.setPosition(0);
    int syncBytes = scratch.readUnsignedShort();
    if (syncBytes != AC40_SYNCWORD && syncBytes != AC41_SYNCWORD) {
      validFramesCount = 0;
      input.resetPeekPosition();
      if (++headerPosition - startPosition >= MAX_SNIFF_BYTES) {
        return false;
      }
      input.advancePeekPosition(headerPosition);
    } else {
      if (++validFramesCount >= 4) {
        return true;
      }
      int frameSize = Ac4Util.parseAc4SyncframeSize(scratch.data, syncBytes);
      if (frameSize == C.LENGTH_UNSET) {
        return false;
      }
      input.advancePeekPosition(frameSize - FRAME_HEADER_SIZE);
    }
  }
}
 
Example #11
Source File: Ac4Extractor.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
  // Skip any ID3 headers.
  ParsableByteArray scratch = new ParsableByteArray(10);
  int startPosition = 0;
  while (true) {
    input.peekFully(scratch.data, /* offset= */ 0, /* length= */ 10);
    scratch.setPosition(0);
    if (scratch.readUnsignedInt24() != ID3_TAG) {
      break;
    }
    scratch.skipBytes(3); // version, flags
    int length = scratch.readSynchSafeInt();
    startPosition += 10 + length;
    input.advancePeekPosition(length);
  }
  input.resetPeekPosition();
  input.advancePeekPosition(startPosition);

  int headerPosition = startPosition;
  int validFramesCount = 0;
  while (true) {
    input.peekFully(scratch.data, /* offset= */ 0, /* length= */ FRAME_HEADER_SIZE);
    scratch.setPosition(0);
    int syncBytes = scratch.readUnsignedShort();
    if (syncBytes != AC40_SYNCWORD && syncBytes != AC41_SYNCWORD) {
      validFramesCount = 0;
      input.resetPeekPosition();
      if (++headerPosition - startPosition >= MAX_SNIFF_BYTES) {
        return false;
      }
      input.advancePeekPosition(headerPosition);
    } else {
      if (++validFramesCount >= 4) {
        return true;
      }
      int frameSize = Ac4Util.parseAc4SyncframeSize(scratch.data, syncBytes);
      if (frameSize == C.LENGTH_UNSET) {
        return false;
      }
      input.advancePeekPosition(frameSize - FRAME_HEADER_SIZE);
    }
  }
}
 
Example #12
Source File: Ac4Extractor.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
  // Skip any ID3 headers.
  ParsableByteArray scratch = new ParsableByteArray(10);
  int startPosition = 0;
  while (true) {
    input.peekFully(scratch.data, /* offset= */ 0, /* length= */ 10);
    scratch.setPosition(0);
    if (scratch.readUnsignedInt24() != ID3_TAG) {
      break;
    }
    scratch.skipBytes(3); // version, flags
    int length = scratch.readSynchSafeInt();
    startPosition += 10 + length;
    input.advancePeekPosition(length);
  }
  input.resetPeekPosition();
  input.advancePeekPosition(startPosition);

  int headerPosition = startPosition;
  int validFramesCount = 0;
  while (true) {
    input.peekFully(scratch.data, /* offset= */ 0, /* length= */ FRAME_HEADER_SIZE);
    scratch.setPosition(0);
    int syncBytes = scratch.readUnsignedShort();
    if (syncBytes != AC40_SYNCWORD && syncBytes != AC41_SYNCWORD) {
      validFramesCount = 0;
      input.resetPeekPosition();
      if (++headerPosition - startPosition >= MAX_SNIFF_BYTES) {
        return false;
      }
      input.advancePeekPosition(headerPosition);
    } else {
      if (++validFramesCount >= 4) {
        return true;
      }
      int frameSize = Ac4Util.parseAc4SyncframeSize(scratch.data, syncBytes);
      if (frameSize == C.LENGTH_UNSET) {
        return false;
      }
      input.advancePeekPosition(frameSize - FRAME_HEADER_SIZE);
    }
  }
}