Skip to content

mp4write: resolve container timing, HE-AAC rate, and gapless bugs - #179

Merged
fabiangreffrath merged 1 commit into
knik0:masterfrom
nschimme:add-edts-elst
Aug 28, 2026
Merged

mp4write: resolve container timing, HE-AAC rate, and gapless bugs#179
fabiangreffrath merged 1 commit into
knik0:masterfrom
nschimme:add-edts-elst

Conversation

@nschimme

@nschimme nschimme commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

This PR resolves several critical MP4 container bug fixes and specification compliance issues in mp4write and encode_engine.c.

It addresses an HE-AAC sample rate declaration bug that caused double-speed playback, replaces fixed-duration guesses in stts with dynamic run-length tracking (fixing ffmpeg demuxer rejection on short files), adds native edit lists (edts/elst) for gapless playback, and fixes an underlying gapless padding calculation.


Key Changes

  • HE-AAC Core Sample Rate Declaration:

    • Updated mp4_set_format() to register HE-AAC containers at the core (pre-SBR) rate (sample_rate / 2), matching standard player conventions for implicit SBR signaling.
    • Introduced rate_conv_t to scale frame durations from cumulative sample positions and align gapless priming/padding metadata without per-frame rounding drift.
  • Dynamic stts Duration Tracking:

    • Replaced the single uniform-duration entry assumption in stts with a dynamic run-length (count, delta) array (g_mp4.durs).
    • Accurately tracks partial trailing frames and flush-drain frames, eliminating duration mismatches between stts and mdhd that previously caused ffmpeg to reject short files ("missing mandatory atoms").
  • Gapless Edit Lists (edts/elst) & Padding Calculation:

    • Added edts/elst atom generation when encoder delay is present so ISO-compliant demuxers trim priming samples correctly.
    • Fixed total_output_samples in encode_engine.c to calculate frame totals using frame_size instead of priming (encoder delay), preventing invalid padding values on HE-AAC/SBR streams.

Testing & Verification

  1. HE-AAC Playback: Encoded HE-AAC streams into MP4 and verified correct pitch and playback speed across QuickTime, VLC, and ffmpeg.
  2. Short Audio Streams: Encoded short audio clips (<2,000 samples) and verified ffmpeg -i parses the file without mandatory atom or timing mismatch errors.
  3. Gapless Trimming: Verified presence of elst atoms using mp4box -info / ffprobe to ensure sample-accurate trimming at track boundaries.

@nschimme
nschimme marked this pull request as draft August 27, 2026 02:34
@nschimme
nschimme force-pushed the add-edts-elst branch 2 times, most recently from c7a5565 to 7b8ce63 Compare August 27, 2026 04:01
@nschimme

Copy link
Copy Markdown
Contributor Author

@fabiangreffrath hold off on a release please, I might need to adjust the ABI contract to fix a flushing bug exposed by gapless media playback. This is also why I submitted some PRs to faad2 as part of my investigation.

@nschimme nschimme changed the title mp4write: add edts/elst edit list, fix gapless padding calc mp4write: resolve container timing, HE-AAC rate, and gapless bugs Aug 27, 2026
@nschimme
nschimme marked this pull request as ready for review August 27, 2026 18:17
@nschimme

Copy link
Copy Markdown
Contributor Author

Whew, the ABI change wasn't necessary and I was able to resolve it in #180 :-)

@nschimme
nschimme marked this pull request as draft August 27, 2026 19:11
- Declare HE-AAC container at core sample rate for implicit SBR.
- Track real per-frame durations in stts via run-length table instead of guessing.
- Add edts/elst edit list atoms for accurate gapless audio trimming.
- Fix total output sample calculation (use frame_size instead of priming).
@nschimme
nschimme marked this pull request as ready for review August 27, 2026 20:20
@nschimme

Copy link
Copy Markdown
Contributor Author

@fabiangreffrath I fixed the bugs, this is ready for merge now

@nschimme

Copy link
Copy Markdown
Contributor Author

@fabiangreffrath
fabiangreffrath merged commit c58e4e8 into knik0:master Aug 28, 2026
7 checks passed
@nschimme
nschimme deleted the add-edts-elst branch August 28, 2026 15:25
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