Documentation
¶
Overview ¶
Package box parses ISO Base Media File Format boxes for the AAC demuxer.
Index ¶
- func FindChild(payload []byte, typ FourCC) (body []byte, found bool, err error)
- func ParseAudioSampleEntry(payload []byte) (channels uint16, sampleRate uint32, childOffset int, err error)
- func ParseCo64(payload []byte) ([]int64, error)
- func ParseEsds(payload []byte) (asc []byte, objectType byte, err error)
- func ParseMdhd(payload []byte) (timescale uint32, duration uint64, err error)
- func ParseMvhd(payload []byte) (timescale uint32, duration uint64, err error)
- func ParseStco(payload []byte) ([]int64, error)
- func ParseStsz(payload []byte) (constantSize, count uint32, sizes []uint32, err error)
- func ParseTfdt(payload []byte) (baseMediaDecodeTime uint64, err error)
- func ParseTkhd(payload []byte) (trackID uint32, err error)
- func ReadTags(source io.ReaderAt, size, selectedTrack int64) (tagdata.Result, error)
- func WalkChildren(payload []byte, fn func(typ FourCC, body []byte) error) error
- type ElstEntry
- type FileBox
- type FourCC
- type Header
- type StscEntry
- type SttsRun
- type Tfhd
- type Trex
- type Trun
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindChild ¶
FindChild returns the body of the first child box of type typ packed in payload. It walks payload to the end rather than stopping at the match, so every child is framing-checked exactly as WalkChildren would check it (a malformed box after the match is still reported), and a malformed-framing error takes precedence over a miss. found is false when no such child is present. The returned body aliases payload; it is not copied.
This is the shared form of the hand-rolled "walk the children, act on one FourCC" idiom. It intentionally returns the first match: its callers each expect a single instance of their target box, and completing the walk keeps the container's remaining children validated. It is not a fit for a box that legitimately repeats (an mvex carrying one trex per track, say); walk those directly.
func ParseAudioSampleEntry ¶
func ParseAudioSampleEntry(payload []byte) (channels uint16, sampleRate uint32, childOffset int, err error)
ParseAudioSampleEntry reads the fixed AudioSampleEntry fields from an mp4a box body (the bytes after the 8-byte box header) and returns the channel count, the integer sample rate (the 16.16 samplerate field truncated to Hz), and the offset within payload where the child boxes (esds and any siblings) begin. It honors the QuickTime version 1 and version 2 sound entries, which insert extra fixed fields before the child boxes.
func ParseCo64 ¶
ParseCo64 decodes the co64 (64-bit chunk offset) FullBox body into absolute file offsets. The entry count is bounded by the body length before allocation.
func ParseEsds ¶
ParseEsds extracts the AudioSpecificConfig and the DecoderConfigDescriptor objectTypeIndication from an esds box body (the bytes after the 8-byte box header). It walks ES_Descriptor(0x03) > DecoderConfigDescriptor(0x04) > DecoderSpecificInfo(0x05), tolerating the optional ES_Descriptor dependency, URL, and OCR fields and any sibling descriptors (for example the mandatory SLConfigDescriptor). The returned asc aliases payload; the caller copies it.
func ParseMdhd ¶
ParseMdhd decodes the timescale and duration from an mdhd (media header) FullBox body, honoring the version-0 and version-1 field widths.
func ParseMvhd ¶
ParseMvhd decodes the timescale and duration from an mvhd (movie header) FullBox body, honoring the version-0 and version-1 field widths.
func ParseStco ¶
ParseStco decodes the stco (32-bit chunk offset) FullBox body into absolute file offsets. The entry count is bounded by the body length before allocation.
func ParseStsz ¶
ParseStsz decodes the stsz (sample size) FullBox body. When sample_size is nonzero it is the constant size shared by all count samples and sizes is nil. When sample_size is zero a per-sample size list of length count follows and is bounded by the body length before allocation.
func ParseTfdt ¶
ParseTfdt decodes the baseMediaDecodeTime from a tfdt (track fragment decode time) FullBox body, honoring both the version-0 (32-bit) and version-1 (64-bit) widths.
func ParseTkhd ¶
ParseTkhd decodes the track_ID from a tkhd (track header) FullBox body, honoring the version-0 (32-bit time fields) and version-1 (64-bit time fields) layouts. The demuxer uses it to bind a track's movie fragments to its sample entry: a moof's traf carries the track_ID, not the codec, so this is what tells an audio traf apart from a video one.
func ReadTags ¶
ReadTags reads file metadata and metadata on the selected audio track. selectedTrack is the offset of its trak box, or zero when unknown. Artwork refers to the original file bytes.
func WalkChildren ¶
WalkChildren iterates the child boxes packed in payload, calling fn with each child's type and body (the bytes after that child's header). A size==0 child consumes the rest of payload. It stops and returns the first error from fn or the first malformed header, and returns nil once payload is fully consumed. Every child advances the cursor by at least eight bytes, so a hostile table cannot spin: ParseHeader rejects any smaller size.
Types ¶
type ElstEntry ¶
type ElstEntry struct {
// SegmentDuration is the length of the edit in the movie timescale. Zero is
// literal on a plain file; the caller decides what it means on a fragmented one.
SegmentDuration uint64
// MediaTime is the media tick the edit starts at, in the media timescale. A
// value of -1 marks an empty edit (a dwell of silence).
MediaTime int64
// MediaRate is the playback rate as the 16.16 fixed-point value
// media_rate_integer<<16 | media_rate_fraction, so 1.0 is 0x00010000.
MediaRate int32
}
ElstEntry is one edit of an elst (edit list) box.
func ParseElst ¶
ParseElst decodes every entry of an elst (edit list) FullBox body, honoring the version-0 (32-bit) and version-1 (64-bit) layouts. The entry count is checked against the bytes present before the slice is allocated, so the result is proportional to the body. A count of zero returns nil. The caller resolves the list; entries are returned as-is, with media_time -1 marking an empty edit.
type FourCC ¶
type FourCC [4]byte
FourCC is a four-character box or brand code, stored as raw bytes.
func NewFourCC ¶
NewFourCC builds a FourCC from s. s should be exactly four bytes; a shorter string is right-padded with NUL and a longer one is truncated to four bytes.
type Header ¶
type Header struct {
Type FourCC
HeaderLen int64
Total int64 // total box length including header; 0 when ToEnd
ToEnd bool
}
Header describes a parsed ISO-BMFF box header: its four-character type, the header length in bytes (8 for the 32-bit form, 16 for the 64-bit largesize form), and the total box length including the header. ToEnd reports the size==0 form, whose total length is "everything to the end of the enclosing stream or buffer" and is resolved by the caller (valid only for the last box).
func ParseHeader ¶
ParseHeader decodes a box header from the front of b. It handles the 32-bit size form, the 64-bit largesize form (size==1), and the extends-to-EOF form (size==0). It rejects a truncated header, a size in 2..7 (smaller than the 8-byte header), and a largesize below 16. It does NOT validate Total against the caller's available bytes: the caller knows whether b is a whole stream or a single container payload and bounds-checks Total itself.
type StscEntry ¶
StscEntry is one run of the sample-to-chunk table: every chunk numbered at or after FirstChunk (until the next entry's FirstChunk) holds SamplesPerChunk samples described by SampleDescriptionIndex.
type SttsRun ¶
SttsRun gives the length and sample duration of a decoding-time run.
func ParseSttsRuns ¶
ParseSttsRuns decodes the stts FullBox body into its raw (sample_count, sample_delta) entries, in file order, zero counts included; the caller normalizes them. The entry count is bounded by the body length before the allocation, so the slice stays input-proportional (8 bytes per entry on both sides).
type Tfhd ¶
type Tfhd struct {
TrackID uint32
HasBaseDataOffset bool
BaseDataOffset uint64
DefaultBaseIsMoof bool
HasDefaultSampleDuration bool
DefaultSampleDuration uint32
HasDefaultSampleSize bool
DefaultSampleSize uint32
HasDefaultSampleFlags bool
DefaultSampleFlags uint32
DurationIsEmpty bool
}
Tfhd holds the decoded track fragment header (tfhd) fields the demuxer needs. Each optional field carries a Has* companion because 0 is a legal value that must be distinguished from an absent field: an absent default falls through to the trex default, a present zero does not.
func ParseTfhd ¶
ParseTfhd decodes a tfhd (track fragment header) FullBox body. It reads the mandatory track_ID and then the flag-gated optional fields in their ISO order: base_data_offset(8, 0x000001), sample_description_index(4, 0x000002), default_sample_duration(4, 0x000008), default_sample_size(4, 0x000010), default_sample_flags(4, 0x000020). sample_description_index is skipped: a single-track fragmented file has one sample entry. Every optional field is bounds-checked against the remaining body before it is read.
type Trex ¶
type Trex struct {
TrackID uint32
DefaultSampleDescriptionIndex uint32
DefaultSampleDuration uint32
DefaultSampleSize uint32
DefaultSampleFlags uint32
}
Trex holds the decoded track extends (trex) defaults, which a movie fragment inherits when its tfhd or trun does not override them.
type Trun ¶
type Trun struct {
SampleCount uint32
HasDataOffset bool
DataOffset int32
HasSampleDuration bool
HasSampleSize bool
HasSampleFlags bool
HasCompositionTime bool
// contains filtered or unexported fields
}
Trun holds one decoded track fragment run (trun). The Has* flags report which per-sample fields the box actually carried, so the caller knows whether to read a sample's size and duration from the accessors or from the tfhd/trex defaults. When no per-sample field is present, SampleCount alone describes the run.
The per-sample records are not decoded into a slice. A run in a CMAF fragment routinely declares hundreds of samples, of which the demuxer reads only the size and duration, so Trun retains the record bytes of the payload it was parsed from and each accessor decodes one field on demand. That keeps ParseTrun allocation-free however many samples a run declares.
Retaining those bytes means a Trun aliases the buffer it was parsed from and must not outlive it. The demuxer parses and consumes each run inside the walk over the moof body it was read from, which is what makes this safe. Anything that keeps a Trun past that walk, or hands one to a later phase, has to copy the fields it needs first, or it pins the whole fragment body.
func ParseTrun ¶
ParseTrun decodes a trun (track fragment run) FullBox body. The flags select the optional fields after sample_count (data_offset, first_sample_flags) and the per-sample record layout (duration, size, flags, composition_time_offset), each 4 bytes. first_sample_flags is skipped: it describes only the first sample's sync state, which does not affect extraction of audio.
The per-record width times sample_count is bounded against the remaining body before the records are retained. Parsing a run allocates nothing on the success path however many samples it declares, so that bound is not an allocation guard: it is what makes every accessor index safe, since it proves the retained slice holds a whole record for every sample below sample_count. Removing it would not save an allocation, it would let a hostile sample_count read past the box.
func (*Trun) SampleCompositionOffset ¶
SampleCompositionOffset returns sample i's composition time offset in media-timescale ticks, or 0 when the run carries none. A version 0 run encodes it unsigned and a version 1 run signed, which this reads faithfully.
func (*Trun) SampleDuration ¶
SampleDuration returns sample i's duration in media-timescale ticks, or 0 when the run carries no per-sample durations.
func (*Trun) SampleFlags ¶
SampleFlags returns sample i's flags word, or 0 when the run carries no per-sample flags.
Nothing in this module reads it: audio access units are all sync samples, and internal/box cannot be imported from outside the module, so there is no other caller to serve. It exists because the record layout has to account for the field's width regardless, which makes decoding it free, and because reading every field back is what lets TestParseTrunAllFieldsPresent pin the offset of each one. The same goes for SampleCompositionOffset.
func (*Trun) SampleSize ¶
SampleSize returns sample i's byte length, or 0 when the run carries no per-sample sizes.