aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-02-13 11:52:40 +0100
committerOnur Gündüz <ogunduz@gmail.com>2026-02-13 11:52:40 +0100
commitb77eda22996be44ecb9c63f665c0acf1d6786821 (patch)
treeaee54821006521001ddb718e140c9ea87a907d78
parentf4b35bb7f4475db77fa6969eb42964824f616fb0 (diff)
downloadmp4-b77eda22996be44ecb9c63f665c0acf1d6786821.tar.gz
mp4-b77eda22996be44ecb9c63f665c0acf1d6786821.zip
Add segment type and index boxes, and update CttsIter for version handling
-rw-r--r--box.go11
-rw-r--r--cmd/mp4dump/main.go2
-rw-r--r--iter.go42
3 files changed, 44 insertions, 11 deletions
diff --git a/box.go b/box.go
index 6185728..0c836b0 100644
--- a/box.go
+++ b/box.go
@@ -11,6 +11,7 @@ func (t BoxType) String() string {
// Known box types.
var (
TypeFtyp = BoxType{'f', 't', 'y', 'p'}
+ TypeStyp = BoxType{'s', 't', 'y', 'p'} // Segment type box (used in fragmented MP4)
TypeMoov = BoxType{'m', 'o', 'o', 'v'}
TypeMvhd = BoxType{'m', 'v', 'h', 'd'}
TypeTrak = BoxType{'t', 'r', 'a', 'k'}
@@ -51,6 +52,7 @@ var (
TypeSubs = BoxType{'s', 'u', 'b', 's'}
TypeSaiz = BoxType{'s', 'a', 'i', 'z'}
TypeSaio = BoxType{'s', 'a', 'i', 'o'}
+ // Fragment movie boxes
TypeMvex = BoxType{'m', 'v', 'e', 'x'}
TypeMehd = BoxType{'m', 'e', 'h', 'd'}
TypeTrex = BoxType{'t', 'r', 'e', 'x'}
@@ -61,13 +63,20 @@ var (
TypeTfhd = BoxType{'t', 'f', 'h', 'd'}
TypeTfdt = BoxType{'t', 'f', 'd', 't'}
TypeTrun = BoxType{'t', 'r', 'u', 'n'}
+ TypeSidx = BoxType{'s', 'i', 'd', 'x'} // Segment index box
+ TypeEmsg = BoxType{'e', 'm', 's', 'g'} // Event message box
+ // Metadata boxes
TypeMeta = BoxType{'m', 'e', 't', 'a'}
TypeUdta = BoxType{'u', 'd', 't', 'a'}
+ // Data boxes
TypeMdat = BoxType{'m', 'd', 'a', 't'}
TypeFree = BoxType{'f', 'r', 'e', 'e'}
TypeSkip = BoxType{'s', 'k', 'i', 'p'}
+ // Sample entry boxes
TypeAvc1 = BoxType{'a', 'v', 'c', '1'}
TypeAvcC = BoxType{'a', 'v', 'c', 'C'}
+ TypeBtrt = BoxType{'b', 't', 'r', 't'} // MPEG-4 Bit rate box
+ TypePasp = BoxType{'p', 'a', 's', 'p'} // Pixel aspect ratio box
TypeMp4a = BoxType{'m', 'p', '4', 'a'}
TypeEsds = BoxType{'e', 's', 'd', 's'}
)
@@ -82,7 +91,7 @@ func IsFullBox(t BoxType) bool {
TypeMeta, TypeEsds, TypeMehd, TypeTrex,
TypeMfhd, TypeTfhd, TypeTfdt, TypeTrun,
TypeSbgp, TypeSgpd, TypeSaiz, TypeSaio,
- TypeCslg, TypeSdtp:
+ TypeCslg, TypeSdtp, TypeSidx, TypeEmsg:
return true
}
return false
diff --git a/cmd/mp4dump/main.go b/cmd/mp4dump/main.go
index 0d42d18..ee7a577 100644
--- a/cmd/mp4dump/main.go
+++ b/cmd/mp4dump/main.go
@@ -209,7 +209,7 @@ func printBoxInfo(r *mp4.Reader) {
fmt.Printf(" entries=%d", it.Count())
case mp4.TypeCtts:
- it := mp4.NewCttsIter(r.Data())
+ it := mp4.NewCttsIter(r.Data(), r.Version())
fmt.Printf(" entries=%d", it.Count())
case mp4.TypeStsc:
diff --git a/iter.go b/iter.go
index 8fb4697..2ec0e1f 100644
--- a/iter.go
+++ b/iter.go
@@ -133,24 +133,29 @@ func (it *SttsIter) Next() (SttsEntry, bool) {
// CttsEntry is a composition offset entry.
type CttsEntry struct {
Count uint32
- Offset int32
+ Offset int32 // Signed offset (version 1), or unsigned treated as signed (version 0)
}
// CttsIter iterates over ctts entries.
type CttsIter struct {
- buf []byte
- count uint32
- index uint32
+ buf []byte
+ count uint32
+ index uint32
+ version uint8
}
// NewCttsIter creates an iterator from ctts box data.
-func NewCttsIter(data []byte) CttsIter {
+// version should be 0 or 1 from the ctts box version field.
+// Version 0: offsets are uint32 (but interpreted as composition time offset)
+// Version 1: offsets are int32 (signed composition time offset)
+func NewCttsIter(data []byte, version uint8) CttsIter {
if len(data) < 4 {
return CttsIter{}
}
return CttsIter{
- buf: data,
- count: be.Uint32(data[0:4]),
+ buf: data,
+ count: be.Uint32(data[0:4]),
+ version: version,
}
}
@@ -167,8 +172,16 @@ func (it *CttsIter) Next() (CttsEntry, bool) {
return CttsEntry{}, false
}
e := CttsEntry{
- Count: be.Uint32(it.buf[offset:]),
- Offset: int32(be.Uint32(it.buf[offset+4:])),
+ Count: be.Uint32(it.buf[offset:]),
+ }
+ // Version 0: uint32 offset (but typically small positive values)
+ // Version 1: int32 offset (can be negative)
+ if it.version == 0 {
+ // In version 0, the value is uint32 but should be interpreted as offset
+ e.Offset = int32(be.Uint32(it.buf[offset+4:]))
+ } else {
+ // In version 1, the value is explicitly signed
+ e.Offset = int32(be.Uint32(it.buf[offset+4:]))
}
it.index++
return e, true
@@ -300,6 +313,17 @@ const (
TrunSampleCompositionTimeOffsetPresent = 0x000800
)
+// Tfhd flags (Track Fragment Header Box).
+const (
+ TfhdBaseDataOffsetPresent = 0x000001
+ TfhdSampleDescriptionIndexPresent = 0x000002
+ TfhdDefaultSampleDurationPresent = 0x000008
+ TfhdDefaultSampleSizePresent = 0x000010
+ TfhdDefaultSampleFlagsPresent = 0x000020
+ TfhdDurationIsEmpty = 0x010000
+ TfhdDefaultBaseIsMoof = 0x020000
+)
+
// TrunIter iterates over trun entries.
type TrunIter struct {
buf []byte