Java Code Examples for com.google.android.exoplayer2.Format#createAudioSampleFormat()

The following examples show how to use com.google.android.exoplayer2.Format#createAudioSampleFormat() . 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: FlacExtractor.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private void outputFormat(FlacStreamInfo streamInfo) {
  Format mediaFormat =
      Format.createAudioSampleFormat(
          /* id= */ null,
          MimeTypes.AUDIO_RAW,
          /* codecs= */ null,
          streamInfo.bitRate(),
          streamInfo.maxDecodedFrameSize(),
          streamInfo.channels,
          streamInfo.sampleRate,
          getPcmEncoding(streamInfo.bitsPerSample),
          /* encoderDelay= */ 0,
          /* encoderPadding= */ 0,
          /* initializationData= */ null,
          /* drmInitData= */ null,
          /* selectionFlags= */ 0,
          /* language= */ null,
          isId3MetadataDisabled ? null : id3Metadata);
  trackOutput.format(mediaFormat);
}
 
Example 2
Source File: Ac4Util.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the AC-4 format given {@code data} containing the AC4SpecificBox according to ETSI TS
 * 103 190-1 Annex E. The reading position of {@code data} will be modified.
 *
 * @param data The AC4SpecificBox 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-4 format parsed from data in the header.
 */
public static Format parseAc4AnnexEFormat(
    ParsableByteArray data, String trackId, String language, DrmInitData drmInitData) {
  data.skipBytes(1); // ac4_dsi_version, bitstream_version[0:5]
  int sampleRate = ((data.readUnsignedByte() & 0x20) >> 5 == 1) ? 48000 : 44100;
  return Format.createAudioSampleFormat(
      trackId,
      MimeTypes.AUDIO_AC4,
      /* codecs= */ null,
      /* bitrate= */ Format.NO_VALUE,
      /* maxInputSize= */ Format.NO_VALUE,
      CHANNEL_COUNT_2,
      sampleRate,
      /* initializationData= */ null,
      drmInitData,
      /* selectionFlags= */ 0,
      language);
}
 
Example 3
Source File: VorbisReader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected boolean readHeaders(ParsableByteArray packet, long position, SetupData setupData)
    throws IOException, InterruptedException {
  if (vorbisSetup != null) {
    return false;
  }

  vorbisSetup = readSetupHeaders(packet);
  if (vorbisSetup == null) {
    return true;
  }

  ArrayList<byte[]> codecInitialisationData = new ArrayList<>();
  codecInitialisationData.add(vorbisSetup.idHeader.data);
  codecInitialisationData.add(vorbisSetup.setupHeaderData);

  setupData.format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_VORBIS, null,
      this.vorbisSetup.idHeader.bitrateNominal, Format.NO_VALUE,
      this.vorbisSetup.idHeader.channels, (int) this.vorbisSetup.idHeader.sampleRate,
      codecInitialisationData, null, 0, null);
  return true;
}
 
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 the EC3SpecificBox according to
 * ETSI TS 102 366 Annex F. The reading position of {@code data} will be modified.
 *
 * @param data The EC3SpecificBox 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 E-AC-3 format parsed from data in the header.
 */
public static Format parseEAc3AnnexFFormat(ParsableByteArray data, String trackId,
    String language, DrmInitData drmInitData) {
  data.skipBytes(2); // data_rate, num_ind_sub

  // Read only the first substream.
  // TODO: Read later substreams?
  int fscod = (data.readUnsignedByte() & 0xC0) >> 6;
  int sampleRate = SAMPLE_RATE_BY_FSCOD[fscod];
  int nextByte = data.readUnsignedByte();
  int channelCount = CHANNEL_COUNT_BY_ACMOD[(nextByte & 0x0E) >> 1];
  if ((nextByte & 0x01) != 0) { // lfeon
    channelCount++;
  }
  return Format.createAudioSampleFormat(trackId, MimeTypes.AUDIO_E_AC3, null, Format.NO_VALUE,
      Format.NO_VALUE, channelCount, sampleRate, null, drmInitData, 0, language);
}
 
Example 5
Source File: VorbisReader.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean readHeaders(ParsableByteArray packet, long position, SetupData setupData)
    throws IOException, InterruptedException {
  if (vorbisSetup != null) {
    return false;
  }

  vorbisSetup = readSetupHeaders(packet);
  if (vorbisSetup == null) {
    return true;
  }

  ArrayList<byte[]> codecInitialisationData = new ArrayList<>();
  codecInitialisationData.add(vorbisSetup.idHeader.data);
  codecInitialisationData.add(vorbisSetup.setupHeaderData);

  setupData.format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_VORBIS, null,
      this.vorbisSetup.idHeader.bitrateNominal, Format.NO_VALUE,
      this.vorbisSetup.idHeader.channels, (int) this.vorbisSetup.idHeader.sampleRate,
      codecInitialisationData, null, 0, null);
  return true;
}
 
Example 6
Source File: FfmpegAudioRenderer.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Format getOutputFormat() {
  Assertions.checkNotNull(decoder);
  int channelCount = decoder.getChannelCount();
  int sampleRate = decoder.getSampleRate();
  @C.PcmEncoding int encoding = decoder.getEncoding();
  return Format.createAudioSampleFormat(
      /* id= */ null,
      MimeTypes.AUDIO_RAW,
      /* codecs= */ null,
      Format.NO_VALUE,
      Format.NO_VALUE,
      channelCount,
      sampleRate,
      encoding,
      Collections.emptyList(),
      /* drmInitData= */ null,
      /* selectionFlags= */ 0,
      /* language= */ null);
}
 
Example 7
Source File: VorbisReader.java    From K-Sonic with MIT License 6 votes vote down vote up
@Override
protected boolean readHeaders(ParsableByteArray packet, long position, SetupData setupData)
    throws IOException, InterruptedException {
  if (vorbisSetup != null) {
    return false;
  }

  vorbisSetup = readSetupHeaders(packet);
  if (vorbisSetup == null) {
    return true;
  }

  ArrayList<byte[]> codecInitialisationData = new ArrayList<>();
  codecInitialisationData.add(vorbisSetup.idHeader.data);
  codecInitialisationData.add(vorbisSetup.setupHeaderData);

  setupData.format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_VORBIS, null,
      this.vorbisSetup.idHeader.bitrateNominal, OggPageHeader.MAX_PAGE_PAYLOAD,
      this.vorbisSetup.idHeader.channels, (int) this.vorbisSetup.idHeader.sampleRate,
      codecInitialisationData, null, 0, null);
  return true;
}
 
Example 8
Source File: FfmpegAudioRenderer.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Format getOutputFormat() {
  Assertions.checkNotNull(decoder);
  int channelCount = decoder.getChannelCount();
  int sampleRate = decoder.getSampleRate();
  @C.PcmEncoding int encoding = decoder.getEncoding();
  return Format.createAudioSampleFormat(
      /* id= */ null,
      MimeTypes.AUDIO_RAW,
      /* codecs= */ null,
      Format.NO_VALUE,
      Format.NO_VALUE,
      channelCount,
      sampleRate,
      encoding,
      Collections.emptyList(),
      /* drmInitData= */ null,
      /* selectionFlags= */ 0,
      /* language= */ null);
}
 
Example 9
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 10
Source File: WavExtractor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  if (wavHeader == null) {
    wavHeader = WavHeaderReader.peek(input);
    if (wavHeader == null) {
      // Should only happen if the media wasn't sniffed.
      throw new ParserException("Unsupported or unrecognized wav header.");
    }
    Format format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, null,
        wavHeader.getBitrate(), MAX_INPUT_SIZE, wavHeader.getNumChannels(),
        wavHeader.getSampleRateHz(), wavHeader.getEncoding(), null, null, 0, null);
    trackOutput.format(format);
    bytesPerFrame = wavHeader.getBytesPerFrame();
  }

  if (!wavHeader.hasDataBounds()) {
    WavHeaderReader.skipToData(input, wavHeader);
    extractorOutput.seekMap(wavHeader);
  }

  int bytesAppended = trackOutput.sampleData(input, MAX_INPUT_SIZE - pendingBytes, true);
  if (bytesAppended != RESULT_END_OF_INPUT) {
    pendingBytes += bytesAppended;
  }

  // Samples must consist of a whole number of frames.
  int pendingFrames = pendingBytes / bytesPerFrame;
  if (pendingFrames > 0) {
    long timeUs = wavHeader.getTimeUs(input.getPosition() - pendingBytes);
    int size = pendingFrames * bytesPerFrame;
    pendingBytes -= size;
    trackOutput.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, size, pendingBytes, null);
  }

  return bytesAppended == RESULT_END_OF_INPUT ? RESULT_END_OF_INPUT : RESULT_CONTINUE;
}
 
Example 11
Source File: FfmpegAudioRenderer.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Format getOutputFormat() {
  int channelCount = decoder.getChannelCount();
  int sampleRate = decoder.getSampleRate();
  return Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, null, Format.NO_VALUE,
      Format.NO_VALUE, channelCount, sampleRate, C.ENCODING_PCM_16BIT, null, null, 0, null);
}
 
Example 12
Source File: MpegAudioReader.java    From TelePlus-Android with GNU General Public License v2.0 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 13
Source File: AudioFormatFixture.java    From no-player with Apache License 2.0 5 votes vote down vote up
Format build() {
    return Format.createAudioSampleFormat(
            id,
            sampleMimeType,
            codecs,
            bitrate,
            maxInputSize,
            channelCount,
            sampleRate,
            initializationData,
            drmInitData,
            selectionFlags,
            language
    );
}
 
Example 14
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 15
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 16
Source File: LibopusAudioRenderer.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected Format getOutputFormat() {
  return Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, null, Format.NO_VALUE,
      Format.NO_VALUE, decoder.getChannelCount(), decoder.getSampleRate(), C.ENCODING_PCM_16BIT,
      null, null, 0, null);
}
 
Example 17
Source File: WavExtractor.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(ExtractorInput input, PositionHolder seekPosition)
    throws IOException, InterruptedException {
  if (wavHeader == null) {
    wavHeader = WavHeaderReader.peek(input);
    if (wavHeader == null) {
      // Should only happen if the media wasn't sniffed.
      throw new ParserException("Unsupported or unrecognized wav header.");
    }
    Format format = Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, null,
        wavHeader.getBitrate(), MAX_INPUT_SIZE, wavHeader.getNumChannels(),
        wavHeader.getSampleRateHz(), wavHeader.getEncoding(), null, null, 0, null);
    trackOutput.format(format);
    bytesPerFrame = wavHeader.getBytesPerFrame();
  }

  if (!wavHeader.hasDataBounds()) {
    WavHeaderReader.skipToData(input, wavHeader);
    extractorOutput.seekMap(wavHeader);
  } else if (input.getPosition() == 0) {
    input.skipFully(wavHeader.getDataStartPosition());
  }

  long dataEndPosition = wavHeader.getDataEndPosition();
  Assertions.checkState(dataEndPosition != C.POSITION_UNSET);

  long bytesLeft = dataEndPosition - input.getPosition();
  if (bytesLeft <= 0) {
    return Extractor.RESULT_END_OF_INPUT;
  }

  int maxBytesToRead = (int) Math.min(MAX_INPUT_SIZE - pendingBytes, bytesLeft);
  int bytesAppended = trackOutput.sampleData(input, maxBytesToRead, true);
  if (bytesAppended != RESULT_END_OF_INPUT) {
    pendingBytes += bytesAppended;
  }

  // Samples must consist of a whole number of frames.
  int pendingFrames = pendingBytes / bytesPerFrame;
  if (pendingFrames > 0) {
    long timeUs = wavHeader.getTimeUs(input.getPosition() - pendingBytes);
    int size = pendingFrames * bytesPerFrame;
    pendingBytes -= size;
    trackOutput.sampleMetadata(timeUs, C.BUFFER_FLAG_KEY_FRAME, size, pendingBytes, null);
  }

  return bytesAppended == RESULT_END_OF_INPUT ? RESULT_END_OF_INPUT : RESULT_CONTINUE;
}
 
Example 18
Source File: AdtsReader.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses the sample header.
 */
private void parseAdtsHeader() throws ParserException {
  adtsScratch.setPosition(0);

  if (!hasOutputFormat) {
    int audioObjectType = adtsScratch.readBits(2) + 1;
    if (audioObjectType != 2) {
      // The stream indicates AAC-Main (1), AAC-SSR (3) or AAC-LTP (4). When the stream indicates
      // AAC-Main it's more likely that the stream contains HE-AAC (5), which cannot be
      // represented correctly in the 2 bit audio_object_type field in the ADTS header. In
      // practice when the stream indicates AAC-SSR or AAC-LTP it more commonly contains AAC-LC or
      // HE-AAC. Since most Android devices don't support AAC-Main, AAC-SSR or AAC-LTP, and since
      // indicating AAC-LC works for HE-AAC streams, we pretend that we're dealing with AAC-LC and
      // hope for the best. In practice this often works.
      // See: https://github.com/google/ExoPlayer/issues/774
      // See: https://github.com/google/ExoPlayer/issues/1383
      Log.w(TAG, "Detected audio object type: " + audioObjectType + ", but assuming AAC LC.");
      audioObjectType = 2;
    }

    int sampleRateIndex = adtsScratch.readBits(4);
    adtsScratch.skipBits(1);
    int channelConfig = adtsScratch.readBits(3);

    byte[] audioSpecificConfig = CodecSpecificDataUtil.buildAacAudioSpecificConfig(
        audioObjectType, sampleRateIndex, channelConfig);
    Pair<Integer, Integer> audioParams = CodecSpecificDataUtil.parseAacAudioSpecificConfig(
        audioSpecificConfig);

    Format format = Format.createAudioSampleFormat(formatId, MimeTypes.AUDIO_AAC, null,
        Format.NO_VALUE, Format.NO_VALUE, audioParams.second, audioParams.first,
        Collections.singletonList(audioSpecificConfig), null, 0, language);
    // In this class a sample is an access unit, but the MediaFormat sample rate specifies the
    // number of PCM audio samples per second.
    sampleDurationUs = (C.MICROS_PER_SECOND * 1024) / format.sampleRate;
    output.format(format);
    hasOutputFormat = true;
  } else {
    adtsScratch.skipBits(10);
  }

  adtsScratch.skipBits(4);
  int sampleSize = adtsScratch.readBits(13) - 2 /* the sync word */ - HEADER_SIZE;
  if (hasCrc) {
    sampleSize -= CRC_SIZE;
  }

  setReadingSampleState(output, sampleDurationUs, 0, sampleSize);
}
 
Example 19
Source File: SimpleDecoderAudioRenderer.java    From TelePlus-Android with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the format of audio buffers output by the decoder. Will not be called until the first
 * output buffer has been dequeued, so the decoder may use input data to determine the format.
 * <p>
 * The default implementation returns a 16-bit PCM format with the same channel count and sample
 * rate as the input.
 */
protected Format getOutputFormat() {
  return Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, null, Format.NO_VALUE,
      Format.NO_VALUE, inputFormat.channelCount, inputFormat.sampleRate, C.ENCODING_PCM_16BIT,
      null, null, 0, null);
}
 
Example 20
Source File: SimpleDecoderAudioRenderer.java    From MediaSDK with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the format of audio buffers output by the decoder. Will not be called until the first
 * output buffer has been dequeued, so the decoder may use input data to determine the format.
 * <p>
 * The default implementation returns a 16-bit PCM format with the same channel count and sample
 * rate as the input.
 */
protected Format getOutputFormat() {
  return Format.createAudioSampleFormat(null, MimeTypes.AUDIO_RAW, null, Format.NO_VALUE,
      Format.NO_VALUE, inputFormat.channelCount, inputFormat.sampleRate, C.ENCODING_PCM_16BIT,
      null, null, 0, null);
}