diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-30 15:35:01 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-30 15:35:01 +0200 |
| commit | 0f5c3761970cdf122da63a2454dfcce545ab357c (patch) | |
| tree | 3f050071c6b41c900dfba3e346c8216fc1b14593 | |
| parent | 3535c58b2104bc9efe377aadadf6ee8577137c0b (diff) | |
| download | mp4-0f5c3761970cdf122da63a2454dfcce545ab357c.tar.gz mp4-0f5c3761970cdf122da63a2454dfcce545ab357c.zip | |
track: add AV1 support, edit list parsing, and sync flag packing
| -rw-r--r-- | reader.go | 19 | ||||
| -rw-r--r-- | test-data/.gitignore | 1 | ||||
| -rwxr-xr-x | test-data/generate.sh | 22 | ||||
| -rw-r--r-- | track/benchmark_test.go | 97 | ||||
| -rw-r--r-- | track/codec_test.go | 27 | ||||
| -rw-r--r-- | track/track.go | 245 |
6 files changed, 345 insertions, 66 deletions
@@ -312,6 +312,25 @@ func (r *Reader) ReadMehd() (fragmentDuration uint64) { return } +// ReadElst extracts the media time of the first edit list entry, in the media +// timescale. It returns the media time and whether an entry was present. +func (r *Reader) ReadElst() (mediaTime int64, ok bool) { + data := r.Data() + if len(data) < 4 || be.Uint32(data[0:4]) == 0 { + return 0, false + } + if r.version == 1 { + if len(data) < 20 { + return 0, false + } + return int64(be.Uint64(data[12:20])), true + } + if len(data) < 16 { + return 0, false + } + return int64(int32(be.Uint32(data[8:12]))), true +} + // ReadTrex extracts fields from a trex box. // Returns trackId, default sample description index, default sample duration, // default sample size, and default sample flags. diff --git a/test-data/.gitignore b/test-data/.gitignore new file mode 100644 index 0000000..2641667 --- /dev/null +++ b/test-data/.gitignore @@ -0,0 +1 @@ +*.mp4 diff --git a/test-data/generate.sh b/test-data/generate.sh new file mode 100755 index 0000000..7780b0f --- /dev/null +++ b/test-data/generate.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Generates synthetic ISOBMFF files for benchmarking the track parser. Each file +# carries an H.264 video track and an AAC audio track. B-frames produce a ctts +# table and faststart places the moov box before mdat. +set -euo pipefail + +dir="$(cd "$(dirname "$0")" && pwd)" + +gen() { + local dur="$1" name="$2" + ffmpeg -y -loglevel error \ + -f lavfi -i "testsrc=duration=${dur}:size=640x480:rate=30" \ + -f lavfi -i "sine=frequency=440:duration=${dur}:sample_rate=48000" \ + -c:v libx264 -preset veryfast -pix_fmt yuv420p \ + -c:a aac -b:a 128k \ + -movflags +faststart \ + "${dir}/${name}" + echo "wrote ${name}" +} + +gen 10 h264-aac-10s.mp4 +gen 60 h264-aac-60s.mp4 diff --git a/track/benchmark_test.go b/track/benchmark_test.go new file mode 100644 index 0000000..0006e62 --- /dev/null +++ b/track/benchmark_test.go @@ -0,0 +1,97 @@ +package track_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/tetsuo/mp4" + "github.com/tetsuo/mp4/track" +) + +var benchFiles = []string{ + "../test-data/h264-aac-10s.mp4", + "../test-data/h264-aac-60s.mp4", +} + +// loadMoov reads path and returns a copy of its top-level moov box, including +// the box header, which is the input ParseTracks expects. +func loadMoov(b *testing.B, path string) []byte { + b.Helper() + data, err := os.ReadFile(path) + if err != nil { + b.Skipf("test file not available: %v", err) + } + r := mp4.NewReader(data) + for r.Next() { + if r.Type() == mp4.TypeMoov { + raw := r.RawBox() + moov := make([]byte, len(raw)) + copy(moov, raw) + return moov + } + } + b.Skipf("no moov box in %s", path) + return nil +} + +func BenchmarkParseTracks(b *testing.B) { + for _, path := range benchFiles { + moov := loadMoov(b, path) + b.Run(filepath.Base(path), func(b *testing.B) { + b.SetBytes(int64(len(moov))) + b.ReportAllocs() + for b.Loop() { + tracks, _, err := track.ParseTracks(moov) + if err != nil { + b.Fatal(err) + } + if len(tracks) == 0 { + b.Fatal("no tracks parsed") + } + } + }) + } +} + +func BenchmarkParseTracksAndCodec(b *testing.B) { + for _, path := range benchFiles { + moov := loadMoov(b, path) + b.Run(filepath.Base(path), func(b *testing.B) { + b.SetBytes(int64(len(moov))) + b.ReportAllocs() + var sink int + for b.Loop() { + tracks, _, err := track.ParseTracks(moov) + if err != nil { + b.Fatal(err) + } + for _, t := range tracks { + sink += len(t.Codec()) + } + } + _ = sink + }) + } +} + +func BenchmarkCollectTrackSampleStats(b *testing.B) { + for _, path := range benchFiles { + moov := loadMoov(b, path) + tracks, _, err := track.ParseTracks(moov) + if err != nil { + b.Fatal(err) + } + var samples []track.Sample + for _, t := range tracks { + samples = append(samples, t.Samples...) + } + var dst []track.TrackSampleStats + b.Run(filepath.Base(path), func(b *testing.B) { + b.ReportAllocs() + for b.Loop() { + dst = track.CollectTrackSampleStats(dst[:0], tracks, samples) + } + }) + } +} diff --git a/track/codec_test.go b/track/codec_test.go new file mode 100644 index 0000000..182c445 --- /dev/null +++ b/track/codec_test.go @@ -0,0 +1,27 @@ +package track + +import "testing" + +func TestAppendAv1CProfile(t *testing.T) { + tests := []struct { + name string + rec []byte + want string + }{ + // rec[1] = seq_profile<<5 | seq_level_idx + // rec[2] = seq_tier<<7 | high_bitdepth<<6 | twelve_bit<<5 + {"main 8-bit level 8", []byte{0x81, 0x08, 0x00, 0x00}, "av01.0.08M.08"}, + {"main 8-bit level 12", []byte{0x81, 0x0c, 0x00, 0x00}, "av01.0.12M.08"}, + {"high tier 10-bit", []byte{0x81, 0x0c, 0xc0, 0x00}, "av01.0.12H.10"}, + {"profile 2 12-bit", []byte{0x81, 0x48, 0x60, 0x00}, "av01.2.08M.12"}, + } + for _, tt := range tests { + tr := &Track{} + tr.setCodec("av01") + tr.appendAv1CProfile(tt.rec) + got := string(tr.raw.codecBuf[:tr.raw.codecLen]) + if got != tt.want { + t.Errorf("%s: got %q, want %q", tt.name, got, tt.want) + } + } +} diff --git a/track/track.go b/track/track.go index 27ef514..6397268 100644 --- a/track/track.go +++ b/track/track.go @@ -7,11 +7,12 @@ import ( "github.com/tetsuo/mp4" ) -// TrackKind distinguishes video and audio tracks. +// TrackKind classifies a track as video, audio, or unknown. type TrackKind int const ( - TrackVideo TrackKind = iota + TrackUnknown TrackKind = iota + TrackVideo TrackAudio ) @@ -30,6 +31,9 @@ type trackRaw struct { hasVmhd bool hasDinf bool + elstMediaTime int64 // edit list media time (media timescale) + hasElst bool + // Raw sample table data. stszData []byte sttsData []byte @@ -62,12 +66,11 @@ type Track struct { Samples []Sample SampleDescIdx uint32 - codec string // set once after parsing completes - raw trackRaw + raw trackRaw } // Codec returns the MIME codec string (e.g. "avc1.64001e", "mp4a.40.2"). -func (t *Track) Codec() string { return t.codec } +func (t *Track) Codec() string { return string(t.raw.codecBuf[:t.raw.codecLen]) } // StsdRaw returns the raw stsd box data (entire box including header). func (t *Track) StsdRaw() []byte { return t.raw.stsd } @@ -99,6 +102,13 @@ func (t *Track) HasVmhd() bool { return t.raw.hasVmhd } // HasDinf returns true if the track has a dinf box (data information). func (t *Track) HasDinf() bool { return t.raw.hasDinf } +// EditMediaTime returns the media time of the track's edit list and whether the +// track has one. It reproduces the initial composition offset, so the first +// frame is presented at time zero. +func (t *Track) EditMediaTime() (int64, bool) { + return t.raw.elstMediaTime, t.raw.hasElst +} + // FindTrack returns the track with the given ID, or nil. func FindTrack(tracks []*Track, id uint32) *Track { for _, t := range tracks { @@ -131,6 +141,42 @@ func (t *Track) appendAvcCProfile(profile, compat, level byte) { t.raw.codecLen += 6 } +// appendAv1CProfile appends ".P.LLT.DD" to the codec buffer from av1C record +// data: seq_profile, 2-digit seq_level_idx, tier (M or H), and bit depth. +func (t *Track) appendAv1CProfile(d []byte) { + seqProfile := d[1] >> 5 + seqLevelIdx := d[1] & 0x1f + seqTier := d[2] >> 7 + highBitdepth := (d[2] >> 6) & 0x01 + twelveBit := (d[2] >> 5) & 0x01 + + bitDepth := byte(8) + if highBitdepth == 1 { + if seqProfile == 2 && twelveBit == 1 { + bitDepth = 12 + } else { + bitDepth = 10 + } + } + tier := byte('M') + if seqTier == 1 { + tier = 'H' + } + + i := t.raw.codecLen + b := t.raw.codecBuf[:] + b[i+0] = '.' + b[i+1] = '0' + seqProfile + b[i+2] = '.' + b[i+3] = '0' + seqLevelIdx/10 + b[i+4] = '0' + seqLevelIdx%10 + b[i+5] = tier + b[i+6] = '.' + b[i+7] = '0' + bitDepth/10 + b[i+8] = '0' + bitDepth%10 + t.raw.codecLen += 9 +} + // appendEsdsCodec appends ".OTI.audioConfig" to the codec buffer from esds data. func (t *Track) appendEsdsCodec(data []byte) { oti, audioConfig := parseEsds(data) @@ -219,17 +265,27 @@ func skipDescLen(data []byte, ptr, end int) int { return -1 } -// Sample represents a single media sample. +// Sample represents a single media sample. The sync-sample flag is stored in +// the high bit of the size field, which keeps the struct at 32 bytes. Read the +// size and the flag through the Size and IsSync methods. type Sample struct { - TrackID uint32 Offset int64 - Size uint32 - Duration uint32 DTS int64 + TrackID uint32 + size uint32 + Duration uint32 PresentationOffset int32 - IsSync bool } +// syncBit marks a sync sample in the high bit of Sample.size. +const syncBit uint32 = 1 << 31 + +// Size returns the sample size in bytes. +func (s Sample) Size() uint32 { return s.size &^ syncBit } + +// IsSync reports whether the sample is a sync sample (keyframe). +func (s Sample) IsSync() bool { return s.size&syncBit != 0 } + // PTS returns the presentation timestamp. func (s Sample) PTS() int64 { return s.DTS + int64(s.PresentationOffset) @@ -305,6 +361,14 @@ var ( // Returns an error if the moov box is not found, if no playable tracks are // found, or if sample tables cannot be parsed for any track. func ParseTracks(moovBuf []byte) ([]*Track, uint64, error) { + return ParseTracksInto(nil, moovBuf) +} + +// ParseTracksInto behaves like ParseTracks but reuses the Track structs in dst +// and their sample slices, which avoids most allocations when the new file has +// the same shape as the previous one. Pass the slice returned by an earlier +// call as dst. The tracks in dst must not be used after this call. +func ParseTracksInto(dst []*Track, moovBuf []byte) ([]*Track, uint64, error) { mr := mp4.NewReader(moovBuf) if !mr.Next() || mr.Type() != mp4.TypeMoov { return nil, 0, ErrMoovNotFound @@ -312,6 +376,7 @@ func ParseTracks(moovBuf []byte) ([]*Track, uint64, error) { var tracks []*Track var duration uint64 + reused := 0 mr.Enter() for mr.Next() { @@ -320,9 +385,16 @@ func ParseTracks(moovBuf []byte) ([]*Track, uint64, error) { _, dur, _ := mr.ReadMvhd() duration = dur case mp4.TypeTrak: - track := parseTrak(&mr) - if track != nil { - tracks = append(tracks, track) + var t *Track + if reused < len(dst) { + t = dst[reused] + resetTrack(t) + } else { + t = &Track{} + } + reused++ + if parseTrakInto(&mr, t) { + tracks = append(tracks, t) } } } @@ -340,9 +412,17 @@ func ParseTracks(moovBuf []byte) ([]*Track, uint64, error) { return valid, duration, nil } -func parseTrak(mr *mp4.Reader) *Track { - track := &Track{} +// resetTrack clears t for reuse while keeping the backing array of its sample +// slice, so a later parse can refill it without allocating. +func resetTrack(t *Track) { + samples := t.Samples[:0] + *t = Track{} + t.Samples = samples +} +// parseTrakInto fills track from a trak box and reports whether it is a valid, +// playable track. +func parseTrakInto(mr *mp4.Reader, track *Track) bool { mr.Enter() defer mr.Exit() @@ -356,19 +436,30 @@ func parseTrak(mr *mp4.Reader) *Track { track.ID = trackId track.Width = uint16(w >> 16) track.Height = uint16(h >> 16) + case mp4.TypeEdts: + parseEdts(mr, track) case mp4.TypeMdia: parseMdia(mr, track) } } - if track.ID == 0 || track.raw.codecLen == 0 { - return nil - } - - // Finalize codec string - track.codec = string(track.raw.codecBuf[:track.raw.codecLen]) + return track.ID != 0 && track.raw.codecLen != 0 +} - return track +// parseEdts reads the first edit list entry's media time, used to reproduce the +// initial composition offset in the output's init segment. +func parseEdts(mr *mp4.Reader, track *Track) { + mr.Enter() + defer mr.Exit() + for mr.Next() { + if mr.Type() == mp4.TypeElst { + if mt, ok := mr.ReadElst(); ok { + track.raw.elstMediaTime = mt + track.raw.hasElst = true + } + return + } + } } func parseMdia(mr *mp4.Reader, track *Track) { @@ -462,67 +553,81 @@ func parseStsd(mr *mp4.Reader, track *Track, handlerType [4]byte) { } mr.Enter() + defer mr.Exit() mr.Skip(4) if !mr.Next() { - mr.Exit() return } entryType := mr.Type() entryData := mr.Data() - if handlerType == htVide && entryType == mp4.TypeAvc1 { + switch handlerType { + case htVide: track.Kind = TrackVideo - track.setCodec("avc1") - if len(entryData) >= 78 { - v := mp4.ReadVisualSampleEntry(entryData) - track.Width = v.Width - track.Height = v.Height - - mr.Enter() - mr.Skip(v.ChildOffset) - for mr.Next() { - if mr.Type() == mp4.TypeAvcC { - d := mr.Data() - if len(d) >= 4 { - track.appendCodec(".") - track.appendAvcCProfile(d[1], d[2], d[3]) - } - break - } + if len(entryData) < 78 { + track.setCodec(entryType.String()) + return + } + v := mp4.ReadVisualSampleEntry(entryData) + track.Width = v.Width + track.Height = v.Height + switch entryType { + case mp4.TypeAvc1: + track.setCodec("avc1") + if d := childBox(mr, v.ChildOffset, mp4.TypeAvcC); len(d) >= 4 { + track.appendCodec(".") + track.appendAvcCProfile(d[1], d[2], d[3]) + } + case mp4.TypeAv01: + track.setCodec("av01") + if d := childBox(mr, v.ChildOffset, mp4.TypeAv1C); len(d) >= 3 { + track.appendAv1CProfile(d) } - mr.Exit() + default: + track.setCodec(entryType.String()) } - } else if handlerType == htSoun && entryType == mp4.TypeMp4a { + case htSoun: track.Kind = TrackAudio - track.setCodec("mp4a") - if len(entryData) >= 28 { - a := mp4.ReadAudioSampleEntry(entryData) - track.ChannelCount = a.ChannelCount - track.SampleRate = a.SampleRate >> 16 - - mr.Enter() - mr.Skip(a.ChildOffset) - for mr.Next() { - if mr.Type() == mp4.TypeEsds { - track.appendEsdsCodec(mr.Data()) - break + switch entryType { + case mp4.TypeMp4a: + track.setCodec("mp4a") + if len(entryData) >= 28 { + a := mp4.ReadAudioSampleEntry(entryData) + track.ChannelCount = a.ChannelCount + track.SampleRate = a.SampleRate >> 16 + if d := childBox(mr, a.ChildOffset, mp4.TypeEsds); d != nil { + track.appendEsdsCodec(d) } } - mr.Exit() + default: + track.setCodec(entryType.String()) } + default: + track.Kind = TrackUnknown + track.setCodec(entryType.String()) } +} - mr.Exit() +// childBox enters the current sample entry, skips its fixed header of +// childOffset bytes, and returns the data of the first child box of boxType, or +// nil if none is found. +func childBox(mr *mp4.Reader, childOffset int, boxType mp4.BoxType) []byte { + mr.Enter() + defer mr.Exit() + mr.Skip(childOffset) + for mr.Next() { + if mr.Type() == boxType { + return mr.Data() + } + } + return nil } // parseSamples parses sample table data and populates track.Samples. // Returns an error if required sample table data is missing or corrupt. func (t *Track) parseSamples() error { - if t.Samples != nil { - return nil // already parsed - } if t.raw.stszData == nil || t.raw.sttsData == nil || t.raw.stscData == nil { return fmt.Errorf("track %d: %w: missing required sample table data (stsz/stts/stsc)", t.ID, ErrInvalidTrack) } @@ -533,11 +638,16 @@ func (t *Track) parseSamples() error { stszIt := mp4.NewStszIter(t.raw.stszData) numSamples := int(stszIt.Count()) if numSamples == 0 { - t.Samples = []Sample{} + t.Samples = t.Samples[:0] return nil } - samples := make([]Sample, numSamples) + var samples []Sample + if cap(t.Samples) >= numSamples { + samples = t.Samples[:numSamples] + } else { + samples = make([]Sample, numSamples) + } stscIt := mp4.NewStscIter(t.raw.stscData) sttsIt := mp4.NewSttsIter(t.raw.sttsData) @@ -627,14 +737,17 @@ func (t *Track) parseSamples() error { isSync = haveSync && nextSync == uint32(i+1) } + packedSize := size + if isSync { + packedSize |= syncBit + } samples[i] = Sample{ - TrackID: t.ID, Offset: offsetInChunk + chunkOffset, - Size: size, - Duration: curStts.Duration, DTS: dts, + TrackID: t.ID, + size: packedSize, + Duration: curStts.Duration, PresentationOffset: presOff, - IsSync: isSync, } if i+1 >= numSamples { |
