Skip to content

Commit

Permalink
Parse and set bitrates in Ac3Reader
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 492003800
  • Loading branch information
rohitjoins authored and icbaker committed Dec 12, 2022
1 parent 2652d0e commit 5f73984
Show file tree
Hide file tree
Showing 27 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,24 @@ public static final class SyncFrameInfo {
public final int frameSize;
/** Number of audio samples in the frame. */
public final int sampleCount;
/** The bitrate of audio samples. */
public final int bitrate;

private SyncFrameInfo(
@Nullable String mimeType,
@StreamType int streamType,
int channelCount,
int sampleRate,
int frameSize,
int sampleCount) {
int sampleCount,
int bitrate) {
this.mimeType = mimeType;
this.streamType = streamType;
this.channelCount = channelCount;
this.sampleRate = sampleRate;
this.frameSize = frameSize;
this.sampleCount = sampleCount;
this.bitrate = bitrate;
}
}

Expand Down Expand Up @@ -259,6 +263,7 @@ public static SyncFrameInfo parseAc3SyncframeInfo(ParsableBitArray data) {
int sampleCount;
boolean lfeon;
int channelCount;
int bitrate;
if (isEac3) {
// Subsection E.1.2.
data.skipBits(16); // syncword
Expand Down Expand Up @@ -291,6 +296,7 @@ public static SyncFrameInfo parseAc3SyncframeInfo(ParsableBitArray data) {
sampleRate = SAMPLE_RATE_BY_FSCOD[fscod];
}
sampleCount = AUDIO_SAMPLES_PER_AUDIO_BLOCK * audioBlocks;
bitrate = calculateEac3Bitrate(frameSize, sampleRate, audioBlocks);
acmod = data.readBits(3);
lfeon = data.readBit();
channelCount = CHANNEL_COUNT_BY_ACMOD[acmod] + (lfeon ? 1 : 0);
Expand Down Expand Up @@ -446,6 +452,7 @@ public static SyncFrameInfo parseAc3SyncframeInfo(ParsableBitArray data) {
mimeType = null;
}
int frmsizecod = data.readBits(6);
bitrate = BITRATE_BY_HALF_FRMSIZECOD[frmsizecod / 2] * 1000;
frameSize = getAc3SyncframeSize(fscod, frmsizecod);
data.skipBits(5 + 3); // bsid, bsmod
acmod = data.readBits(3);
Expand All @@ -465,7 +472,7 @@ public static SyncFrameInfo parseAc3SyncframeInfo(ParsableBitArray data) {
channelCount = CHANNEL_COUNT_BY_ACMOD[acmod] + (lfeon ? 1 : 0);
}
return new SyncFrameInfo(
mimeType, streamType, channelCount, sampleRate, frameSize, sampleCount);
mimeType, streamType, channelCount, sampleRate, frameSize, sampleCount, bitrate);
}

/**
Expand Down Expand Up @@ -587,5 +594,15 @@ private static int getAc3SyncframeSize(int fscod, int frmsizecod) {
}
}

/**
* Derived from the formula defined in F.6.2.2 to calculate data_rate for the (E-)AC3 bitstream.
* Note: The formula is based on frmsiz read from the spec. We already do some modifications to it
* when deriving frameSize from the read value. The formula used here is adapted to accommodate
* that modification.
*/
private static int calculateEac3Bitrate(int frameSize, int sampleRate, int audioBlocks) {
return (frameSize * sampleRate) / (audioBlocks * 32);
}

private Ac3Util() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.TrackIdGenerator;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableBitArray;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util;
Expand Down Expand Up @@ -207,14 +208,19 @@ private void parseHeader() {
|| frameInfo.channelCount != format.channelCount
|| frameInfo.sampleRate != format.sampleRate
|| !Util.areEqual(frameInfo.mimeType, format.sampleMimeType)) {
format =
Format.Builder formatBuilder =
new Format.Builder()
.setId(formatId)
.setSampleMimeType(frameInfo.mimeType)
.setChannelCount(frameInfo.channelCount)
.setSampleRate(frameInfo.sampleRate)
.setLanguage(language)
.build();
.setPeakBitrate(frameInfo.bitrate);
// AC3 has constant bitrate, so averageBitrate = peakBitrate
if (MimeTypes.AUDIO_AC3.equals(frameInfo.mimeType)) {
formatBuilder.setAverageBitrate(frameInfo.bitrate);
}
format = formatBuilder.build();
output.format(format);
}
sampleSize = frameInfo.frameSize;
Expand Down
2 changes: 2 additions & 0 deletions testdata/src/test/assets/extractordumps/ts/sample.ac3.0.dump
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ track 0:
total output bytes = 13281
sample count = 8
format 0:
averageBitrate = 384000
peakBitrate = 384000
id = 0
sampleMimeType = audio/ac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ track 0:
total output bytes = 13281
sample count = 8
format 0:
averageBitrate = 384000
peakBitrate = 384000
id = 0
sampleMimeType = audio/ac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 0:
total output bytes = 216000
sample count = 54
format 0:
peakBitrate = 6000000
id = 0
sampleMimeType = audio/eac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 0:
total output bytes = 216000
sample count = 54
format 0:
peakBitrate = 6000000
id = 0
sampleMimeType = audio/eac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ track 189:
total output bytes = 1252
sample count = 3
format 0:
averageBitrate = 96000
peakBitrate = 96000
id = 189
sampleMimeType = audio/ac3
channelCount = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ track 189:
total output bytes = 1252
sample count = 3
format 0:
averageBitrate = 96000
peakBitrate = 96000
id = 189
sampleMimeType = audio/ac3
channelCount = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ track 1900:
total output bytes = 13281
sample count = 8
format 0:
averageBitrate = 384000
peakBitrate = 384000
id = 1/1900
sampleMimeType = audio/ac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ track 1900:
total output bytes = 10209
sample count = 6
format 0:
averageBitrate = 384000
peakBitrate = 384000
id = 1/1900
sampleMimeType = audio/ac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ track 1900:
total output bytes = 7137
sample count = 4
format 0:
averageBitrate = 384000
peakBitrate = 384000
id = 1/1900
sampleMimeType = audio/ac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ track 1900:
total output bytes = 0
sample count = 0
format 0:
averageBitrate = 384000
peakBitrate = 384000
id = 1/1900
sampleMimeType = audio/ac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ track 1900:
total output bytes = 13281
sample count = 8
format 0:
averageBitrate = 384000
peakBitrate = 384000
id = 1/1900
sampleMimeType = audio/ac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 330:
total output bytes = 9928
sample count = 19
format 0:
peakBitrate = 128000
id = 1031/330
sampleMimeType = audio/eac3
channelCount = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 330:
total output bytes = 9928
sample count = 19
format 0:
peakBitrate = 128000
id = 1031/330
sampleMimeType = audio/eac3
channelCount = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 216000
sample count = 54
format 0:
peakBitrate = 6000000
id = 1/1900
sampleMimeType = audio/eac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 168000
sample count = 42
format 0:
peakBitrate = 6000000
id = 1/1900
sampleMimeType = audio/eac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 96000
sample count = 24
format 0:
peakBitrate = 6000000
id = 1/1900
sampleMimeType = audio/eac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 0
sample count = 0
format 0:
peakBitrate = 6000000
id = 1/1900
sampleMimeType = audio/eac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 1900:
total output bytes = 216000
sample count = 54
format 0:
peakBitrate = 6000000
id = 1/1900
sampleMimeType = audio/eac3
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 0:
total output bytes = 163840
sample count = 64
format 0:
peakBitrate = 640000
id = 0
sampleMimeType = audio/eac3-joc
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 0:
total output bytes = 163840
sample count = 64
format 0:
peakBitrate = 640000
id = 0
sampleMimeType = audio/eac3-joc
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 163840
sample count = 64
format 0:
peakBitrate = 640000
id = 1/1900
sampleMimeType = audio/eac3-joc
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 112640
sample count = 44
format 0:
peakBitrate = 640000
id = 1/1900
sampleMimeType = audio/eac3-joc
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 56320
sample count = 22
format 0:
peakBitrate = 640000
id = 1/1900
sampleMimeType = audio/eac3-joc
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ track 1900:
total output bytes = 5120
sample count = 2
format 0:
peakBitrate = 640000
id = 1/1900
sampleMimeType = audio/eac3-joc
channelCount = 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ track 1900:
total output bytes = 163840
sample count = 64
format 0:
peakBitrate = 640000
id = 1/1900
sampleMimeType = audio/eac3-joc
channelCount = 6
Expand Down

0 comments on commit 5f73984

Please sign in to comment.