Skip to content

Refactor audio sample calculation for EAC3#3276

Open
minlinbroadcom wants to merge 3 commits into
androidx:releasefrom
minlinbroadcom:patch-1
Open

Refactor audio sample calculation for EAC3#3276
minlinbroadcom wants to merge 3 commits into
androidx:releasefrom
minlinbroadcom:patch-1

Conversation

@minlinbroadcom

Copy link
Copy Markdown

Calculate the number of audio samples based on frame size and number of frames for EAC3.

Calculate the number of audio samples based on frame size and number of frames for EAC3.
@google-cla

google-cla Bot commented Jun 16, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@FongMi

FongMi commented Jun 21, 2026

Copy link
Copy Markdown

For #3276, the sample count calculation can overcount when an EC-3 sample contains multiple syncframes for different substreams.

In Ac3Util.java, the new code derives numFrames from buffer.remaining() / firstFrameBytes and returns numFrames * samplesPerFrame. However, in ISO BMFF, an E-AC-3 sample may contain multiple syncframes for different substreams or shorter block frames, and those syncframes do not necessarily represent consecutive presentation time. Per ETSI TS 102 366 Annex F, the EC-3 sample duration is still six audio blocks, i.e. 1536 PCM samples.

For example, a sample with two substreams and three audio blocks per syncframe may contain four syncframes. This implementation would return 4 * 768 = 3072, but the correct sample duration is still 1536. I think this should either be reverted or implemented as MP4/EC-3-specific logic, rather than changing a utility method whose contract is to parse one syncframe and relying on buffer.remaining().

Enhanced the parseAc3SyncframeAudioSampleCount method to handle EC-3 access units and calculate total audio blocks from independent substreams.
@FongMi

FongMi commented Jun 23, 2026

Copy link
Copy Markdown

Thanks for the update. This fixes the dependent-substream overcount, but I think the duration can still be overcounted when the EC-3 bitstream contains multiple independent substreams.

The current code adds audio blocks for every syncframe whose strmtyp is not TYPE1. That means it also adds blocks from independent substreams with non-zero substreamid. Per ETSI TS 102 366 Annex F.2, an AC-3/E-AC-3 sample still represents six audio blocks / 1536 PCM samples; additional independent substreams are concurrent services/substreams, not additional presentation time. Annex F.6 also defines num_ind_sub and associated services, so summing all independent substreams can make the sample duration too large.

I think this should count only one presentation timeline, for example independent substream 0, or otherwise keep the MP4 EC-3 sample duration fixed at 1536 samples. It would also be good to add tests for an access unit with dependent substreams and one with multiple independent substreams.

Only accumulates audio blocks when strmtyp == TYPE0 && substreamid == 0 — the primary independent substream
@FongMi

FongMi commented Jun 24, 2026

Copy link
Copy Markdown

FongMi@5e43a05

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants