Skip to content

Implement Main profile and additional ASC support; add SetAACDescriptorFromConfig - #657

Merged
tobbee merged 2 commits into
Eyevinn:masterfrom
montybay:aac-main-and-asc-PR
Nov 3, 2026
Merged

tobbee merged 2 commits into
Eyevinn:masterfrom
montybay:aac-main-and-asc-PR

Conversation

@montybay

Copy link
Copy Markdown
Contributor

Along the same lines as c94c23a, but going the rest of the way to fully parse the ASC and allow re-encoding it into a new stream. This was done to allow Main profile demux/remux.

@tobbee tobbee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Monty! Parsing the full GASpecificConfig and the sync extensions is a real step forward. I ran the branch over every AAC esds in the FFmpeg FATE suite and in the mp4ff test data, decoding each config with both master and this branch:

  • All 30 configs that the branch decodes re-encode to identical bytes. That includes SBR and PS signalled through sync extensions. Master gets those wrong and drops them on Encode; it decodes 138856e5a54880 as plain LC, for example.
  • Keeping frameLengthFlag is valuable too. 960-sample frames are used in DAB+ and DRM, but Encode currently always signals 1024.

There are a few things I'd like changed before merging, though.

1. Channel configuration 0 (PCE) is now an error

Eleven configs that decode on master fail on this branch with channel configuration zero requires an unsupported Program Config Element. For example:

  • al04_44.mp4: 12000504002001e000
  • al_sbr_ps_04_new.mp4: 140006040000000056e5a8
  • al22_chCfg0PCE_44.mp4: 1200050c05200109440003ac042f

For these, master returns the object type, sampling frequency and channel configuration, and callers rely on that. The IAMF parser takes its sample rate from it, for example. Both AAC Main conformance streams in the suite also have a PCE (am00_88.mp4: 0880004400000000, and am05_44.mp4), so the branch doesn't decode them either.

Could you parse the program_config_element (ISO/IEC 14496-3, subpart 4)? The GA flags and sync extensions come after it, so it has to be parsed to reach them anyway. For Encode, either keeping the parsed fields or keeping the raw PCE bits would work. Note that the byte_alignment() before the PCE comment field is relative to the start of the AudioSpecificConfig. If a PCE parser is more than you want to take on now, please at least keep returning the header fields for channel configuration 0 instead of failing.

2. Trailing bits are rejected

The AudioSpecificConfig syntax only looks for a 0x2b7 sync extension when SBR isn't signalled explicitly and at least 16 bits remain. It only looks for 0x548 when sbrPresentFlag is 1 and at least 12 bits remain. Any other trailing bits are ignored. The branch rejects any trailing byte, so these configs decode on master but fail here:

  • 121000: LC followed by a zero byte
  • 2b8a080000: explicit HE-AAC followed by a zero byte
  • 121056e5000000: a sync extension with sbrPresentFlag 0, followed by zero bytes

I'd follow the spec conditions and ignore whatever is left over, including non-zero padding. On the encode side, setting PSSyncExtensionPresent with SBRPresentFlag false writes a 0x548 extension that no decoder reads, so validate could reject that combination.

3. Tests

Coverage of the aac package drops from 90.3% to 72.2%. Could you add table tests for the following?

  • Main
  • frameLengthFlag
  • dependsOnCoreCoder and CoreCoderDelay
  • sync-extension decode/encode roundtrips: SBR on and off, with and without PS
  • PCE
  • the validate error paths
  • CoreSamplesPerFrame and SetAACDescriptorFromConfig

The hex strings above can serve as test vectors.

4. API behaviour changes

  • DecodeAudioSpecificConfig now reads its whole reader, so the reader must end where the config ends. That's fine for the callers in this repo, but it deserves a CHANGELOG line.
  • For an unsupported object type or a bad frequency index, it now returns nil where master returned the partly decoded config. Either behaviour is OK with me, but it should be a deliberate choice: a caller that reads asc.ObjectType after an error will now panic.

5. Small things

  • The doc comment of SetAACDescriptorWithChannelConfig still lists only AAClc, HEAACv1 and HEAACv2.
  • SetAACDescriptorFromConfig accepts the reserved channel configurations 8–10 and 15, which gives a sample entry with channel count 0. The aac.NrChannels(...) == 0 check from SetAACDescriptorWithChannelConfig would fix that.
  • Please add a CHANGELOG entry under Unreleased and use a Conventional Commits title, such as feat(aac): parse full GASpecificConfig and sync extensions, add AAC Main. We rebase-merge, so the commit message lands on master as written.

Finally, out of curiosity: what produces the AAC Main files you're seeing? Main is rare outside conformance streams, so a small sample would make a good test case if you can share one.

@montybay

Copy link
Copy Markdown
Contributor Author

1. Channel configuration 0 (PCE) is now an error

Eleven configs that decode on master fail on this branch with channel configuration zero requires an unsupported Program Config Element. For example:

I wanted to reject something that wasn't known to be handled, rather than having it potentially fail silently. I'm not set up with good test case coverage, so I was a little wary of just implementing it.

  • al04_44.mp4: 12000504002001e000
  • al_sbr_ps_04_new.mp4: 140006040000000056e5a8
  • al22_chCfg0PCE_44.mp4: 1200050c05200109440003ac042f

For these, master returns the object type, sampling frequency and channel configuration, and callers rely on that. The IAMF parser takes its sample rate from it, for example. Both AAC Main conformance streams in the suite also have a PCE (am00_88.mp4: 0880004400000000, and am05_44.mp4), so the branch doesn't decode them either.

Could you parse the program_config_element (ISO/IEC 14496-3, subpart 4)? The GA flags and sync extensions come after it, so it has to be parsed to reach them anyway. For Encode, either keeping the parsed fields or keeping the raw PCE bits would work. Note that the byte_alignment() before the PCE comment field is relative to the start of the AudioSpecificConfig. If a PCE parser is more than you want to take on now, please at least keep returning the header fields for channel configuration 0 instead of failing.

I can certainly try, especially if you have the test samples. Otherwise, I'll go back and preserve your convention of returning what is understood.

2. Trailing bits are rejected

The AudioSpecificConfig syntax only looks for a 0x2b7 sync extension when SBR isn't signalled explicitly and at least 16 bits remain. It only looks for 0x548 when sbrPresentFlag is 1 and at least 12 bits remain. Any other trailing bits are ignored. The branch rejects any trailing byte, so these configs decode on master but fail here:

  • 121000: LC followed by a zero byte
  • 2b8a080000: explicit HE-AAC followed by a zero byte
  • 121056e5000000: a sync extension with sbrPresentFlag 0, followed by zero bytes

AFAIK the trailing bytes aren't part of the ASC, and I'm used to the ASC needing to be clean (or god knows what random proprietary hardware will do). But that's hypothetical, I shouldn't be breaking working cases. I leaned pretty hard on Codex for the tests, and as far as I saw it had added coverage and I hadn't noticed that it altered success criteria.

I'm not an AAC expert by any stretch (I'm on the opposing team ;-) and I need to trace all these things out by hand with the spec in front of me.

I'd follow the spec conditions and ignore whatever is left over, including non-zero padding. On the encode side, setting PSSyncExtensionPresent with SBRPresentFlag false writes a 0x548 extension that no decoder reads, so validate could reject that combination.

OK. My thinking is that with all the extensions and retcons, I needed to be sure what other fields could conditionally follow the ASC and parse those properly, and I've not gotten that far. I'll look a bit deeper at it.

3. Tests

Coverage of the aac package drops from 90.3% to 72.2%. Could you add table tests for the following?

  • Main
  • frameLengthFlag
  • dependsOnCoreCoder and CoreCoderDelay
  • sync-extension decode/encode roundtrips: SBR on and off, with and without PS
  • PCE
  • the validate error paths
  • CoreSamplesPerFrame and SetAACDescriptorFromConfig

The hex strings above can serve as test vectors.

I can do that.

4. API behaviour changes

  • DecodeAudioSpecificConfig now reads its whole reader, so the reader must end where the config ends. That's fine for the callers in this repo, but it deserves a CHANGELOG line.

Completely fair. That's a hard fail on me.

  • For an unsupported object type or a bad frequency index, it now returns nil where master returned the partly decoded config. Either behaviour is OK with me, but it should be a deliberate choice: a caller that reads asc.ObjectType after an error will now panic.

That is even more fair. I should have kept existing behavior. When I started coding, it was just for me, then I never went back and changed it.

5. Small things

  • The doc comment of SetAACDescriptorWithChannelConfig still lists only AAClc, HEAACv1 and HEAACv2.

Will fix.

  • SetAACDescriptorFromConfig accepts the reserved channel configurations 8–10 and 15, which gives a sample entry with channel count 0. The aac.NrChannels(...) == 0 check from SetAACDescriptorWithChannelConfig would fix that.

Will fix. Editing error on my part when I rebased on your version of the SetAACDescriptorWithChannelConfig.

  • Please add a CHANGELOG entry under Unreleased and use a Conventional Commits title, such as feat(aac): parse full GASpecificConfig and sync extensions, add AAC Main. We rebase-merge, so the commit message lands on master as written.

Will fix.

Finally, out of curiosity: what produces the AAC Main files you're seeing? Main is rare outside conformance streams, so a small sample would make a good test case if you can share one.

Right now I have absolutely no use for it! I needed ASC decode/encode to bolt various GCP services onto WebRTC. My thinking didn't go much farther than 'huh, I probably have everything I need now to turn on Main' which used to be more common at least in my memory. If I ever ran into it again, I wanted it to work, not page me when I was asleep. And that's it.

If your verdict is 'yeah, no, not worth the extra surface' I'd completely understand.

@tobbee

tobbee commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the thorough answer, and for wading this far into AAC.

PCE: A real parser would be best. All the samples are public in the FFmpeg FATE suite:

rsync -vrltLW rsync://fate-suite.ffmpeg.org/fate-suite/aac/ fate-suite/aac/

These files have channel configuration 0 with a PCE: al04_44, al04sf_48, al05_44, al06_44, al07_96, al15_44, al17_44, al22_chCfg0PCE_44, al_sbr_cm_48_5.1, al_sbr_ps_04_new, al_sbr_ps_06_new, am00_88 and am05_44. Each one's raw AudioSpecificConfig is in its esds (mp4ff-info prints it as DecConfig), so the hex strings work as unit-test vectors without checking in any files. ffprobe -show_streams gives the channel count and layout to compare against. al15_44 and am05_44 also have a comment field, which exercises the byte alignment.

I understand the worry about silent misparsing. If you'd rather not take on the PCE now, a middle ground would be to decode channel configuration 0 as master does, returning the header fields without an error, but have Encode refuse it. Then nothing gets re-encoded wrongly, and the full parser can come in a later PR.

Trailing bits: The DecoderSpecificInfo length is given in bytes, so the leftovers after the last field are formally part of the config. Decoders just ignore them, so rejecting them would break files that play fine today.

Main: Let's keep it. DecodeADTSHeader already maps profile 0 to object type 1, so it's more consistent for the config code to accept it too, and it's only one more case label.

Tests: The edits to the existing test cases follow from the intended behaviour changes, so they're fine. What's missing is new cases for the items listed in my review.

Fork PRs need my approval before CI runs, so I'll approve the checks after your next push. No rush.

@tobbee
tobbee force-pushed the aac-main-and-asc-PR branch from 896b5f8 to 38f425d Compare November 3, 2026 12:55
tobbee added a commit to montybay/mp4ff that referenced this pull request Nov 3, 2026
…n rules

Completes PR Eyevinn#657 along its review:

- Channel configuration 0 decodes its program_config_element (ISO/IEC 14496-3:2019 Table 4.2) into the new ProgramConfigElement, with the byte alignment before the comment field counted from the start of the AudioSpecificConfig, and Encode writes it back. The FATE suite configs with a PCE, including the AAC Main conformance streams am00_88 and am05_44, decode with the channel counts that ffprobe reports and re-encode byte for byte.
- Sync extensions are only looked for under the conditions of Table 1.19: 0x2b7 if SBR is not signalled explicitly and at least 16 bits are left, then 0x548 if at least 12 bits are left. Unknown sync words, unknown extension object types and other leftover bits are ignored instead of rejected, so configs with trailing zero bytes, such as those of twofields_packet.mp4 and mov_stream_shorter_than_movie.mov, decode again. In the 2019 edition the 0x548 check is outside the sbrPresentFlag condition, so a PS sync extension after an SBR flag of 0 is read and written too.
- Decode errors return the fields decoded so far, as master did for an unsupported object type or frequency index. Escaped object types are reported with their value, such as 39 for ER AAC ELD.
- AudioSpecificConfig.NrChannels gives the channel count from the channel configuration or the PCE. SetAACDescriptorFromConfig uses it and rejects configs without channels, such as the reserved channel configurations 8-10 and 15.
- Tests cover AAC Main, frameLengthFlag, the core coder delay, sync extensions with SBR on and off and with and without PS, PCEs, trailing bits, the validation errors, CoreSamplesPerFrame and SetAACDescriptorFromConfig. Coverage of aac goes from 90.3% on master to 95.6%.

Of the 63 distinct AAC configs in the FATE suite, the 40 that master decodes still decode, and so do am00_88 and am05_44. 40 of these 42 re-encode byte for byte; the other two lose only trailing zero bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EagxH7ENAVwUPJWTRiw1wQ
montybay and others added 2 commits November 3, 2026 13:57
Also add TrakBox.SetAACDescriptorFromConfig to create a sample entry from a full AudioSpecificConfig.
…n rules

Completes PR Eyevinn#657 along its review:

- Channel configuration 0 decodes its program_config_element (ISO/IEC 14496-3:2019 Table 4.2) into the new ProgramConfigElement, with the byte alignment before the comment field counted from the start of the AudioSpecificConfig, and Encode writes it back. The FATE suite configs with a PCE, including the AAC Main conformance streams am00_88 and am05_44, decode with the channel counts that ffprobe reports and re-encode byte for byte.
- Sync extensions are only looked for under the conditions of Table 1.19: 0x2b7 if SBR is not signalled explicitly and at least 16 bits are left, then 0x548 if at least 12 bits are left. Unknown sync words, unknown extension object types and other leftover bits are ignored instead of rejected, so configs with trailing zero bytes, such as those of twofields_packet.mp4 and mov_stream_shorter_than_movie.mov, decode again. In the 2019 edition the 0x548 check is outside the sbrPresentFlag condition, so a PS sync extension after an SBR flag of 0 is read and written too.
- Decode errors return the fields decoded so far, as master did for an unsupported object type or frequency index. Escaped object types are reported with their value, such as 39 for ER AAC ELD.
- AudioSpecificConfig.NrChannels gives the channel count from the channel configuration or the PCE. SetAACDescriptorFromConfig uses it and rejects configs without channels, such as the reserved channel configurations 8-10 and 15.
- Tests cover AAC Main, frameLengthFlag, the core coder delay, sync extensions with SBR on and off and with and without PS, PCEs, trailing bits, the validation errors, CoreSamplesPerFrame and SetAACDescriptorFromConfig. Coverage of aac goes from 90.3% on master to 95.6%.

Of the 63 distinct AAC configs in the FATE suite, the 40 that master decodes still decode, and so do am00_88 and am05_44. 40 of these 42 re-encode byte for byte; the other two lose only trailing zero bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EagxH7ENAVwUPJWTRiw1wQ
@tobbee
tobbee force-pushed the aac-main-and-asc-PR branch from 38f425d to 4f33ac9 Compare November 3, 2026 12:57

@tobbee tobbee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some changes on top of yours, so that I could merge the PR. Thanks for the contribution.

@tobbee
tobbee merged commit 80947f9 into Eyevinn:master Nov 3, 2026
10 checks passed
@montybay

montybay commented Nov 3, 2026 via email

Copy link
Copy Markdown
Contributor Author

@tobbee

tobbee commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

@montybay No problem, I've spent a lot of time (and especially tokens) on fixing all unstable/bad-input corners of mp4ff recently and wanted to make a release so I added what I wanted.

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.

2 participants