Skip to content

Commit

Permalink
Merge pull request #69 from edgeware/co64
Browse files Browse the repository at this point in the history
feat: add co64 box
  • Loading branch information
tobbee authored Mar 19, 2021
2 parents c20d74c + e265e6f commit c734943
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cmd/mp4ff-nallister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ func parseProgressiveMp4(f *mp4.File, maxNrSamples int, codec string) error {
if err != nil {
return err
}
offset := int64(stbl.Stco.ChunkOffset[chunkNr-1])
var offset int64
if stbl.Stco != nil {
offset = int64(stbl.Stco.ChunkOffset[chunkNr-1])
} else if stbl.Co64 != nil {
offset = int64(stbl.Co64.ChunkOffset[chunkNr-1])
}
for sNr := sampleNrAtChunkStart; sNr < sampleNr; sNr++ {
offset += int64(stbl.Stsz.SampleSize[sNr-1])
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/mp4ff-wvttlister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ func parseProgressiveMp4(f *mp4.File, trackID uint32, maxNrSamples int) error {
if err != nil {
return err
}
offset := int64(stbl.Stco.ChunkOffset[chunkNr-1])
var offset int64
if stbl.Stco != nil {
offset = int64(stbl.Stco.ChunkOffset[chunkNr-1])
} else if stbl.Co64 != nil {
offset = int64(stbl.Co64.ChunkOffset[chunkNr-1])
}
for sNr := sampleNrAtChunkStart; sNr < sampleNr; sNr++ {
offset += int64(stbl.Stsz.SampleSize[sNr-1])
}
Expand Down
8 changes: 7 additions & 1 deletion examples/segmenter/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ func (s *Segmenter) GetSamplesUntilTime(mp4f *mp4.File, tr *Track, startSampleNr
if err != nil {
return nil
}
offset := int64(stbl.Stco.ChunkOffset[chunkNr-1])
var offset int64
if stbl.Stco != nil {
offset = int64(stbl.Stco.ChunkOffset[chunkNr-1])
} else if stbl.Co64 != nil {
offset = int64(stbl.Co64.ChunkOffset[chunkNr-1])
}

for sNr := sampleNrAtChunkStart; sNr < sampleNr; sNr++ {
offset += int64(stbl.Stsz.SampleSize[sNr-1])
}
Expand Down
1 change: 1 addition & 0 deletions mp4/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
"btrt": DecodeBtrt,
"cdat": DecodeCdat,
"clap": DecodeClap,
"co64": DecodeCo64,
"ctim": DecodeCtim,
"ctts": DecodeCtts,
"dinf": DecodeDinf,
Expand Down
77 changes: 77 additions & 0 deletions mp4/co64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package mp4

import (
"io"
"io/ioutil"
)

// Co64Box - Chunk Large Offset Box
//
// Contained in : Sample Table box (stbl)
//
// 64-bit version of StcoBox
type Co64Box struct {
Version byte
Flags uint32
ChunkOffset []uint64
}

// DecodeStco - box-specific decode
func DecodeCo64(hdr *boxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
sr := NewSliceReader(data)
versionAndFlags := sr.ReadUint32()
nrEntries := sr.ReadUint32()
b := &Co64Box{
Version: byte(versionAndFlags >> 24),
Flags: versionAndFlags & flagsMask,
ChunkOffset: make([]uint64, nrEntries),
}

for i := uint32(0); i < nrEntries; i++ {
b.ChunkOffset[i] = sr.ReadUint64()
}
return b, nil
}

// Type - box-specific type
func (b *Co64Box) Type() string {
return "co64"
}

// Size - box-specific size
func (b *Co64Box) Size() uint64 {
return uint64(boxHeaderSize + 8 + len(b.ChunkOffset)*8)
}

// Encode - box-specific encode
func (b *Co64Box) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
buf := makebuf(b)
sw := NewSliceWriter(buf)
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
sw.WriteUint32(uint32(len(b.ChunkOffset)))
for i := range b.ChunkOffset {
sw.WriteUint64(b.ChunkOffset[i])
}
_, err = w.Write(buf)
return err
}

func (b *Co64Box) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
level := getInfoLevel(b, specificBoxLevels)
if level >= 1 {
for i := range b.ChunkOffset {
bd.write(" - entry[%d]: chunkOffset=%d", i+1, b.ChunkOffset[i])
}
}
return bd.err
}
15 changes: 15 additions & 0 deletions mp4/co64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mp4

import (
"testing"
)

func TestEncDecCo64(t *testing.T) {

b := &Co64Box{
Version: 0,
Flags: 2, // Just in test
ChunkOffset: []uint64{1234, 8908080},
}
boxDiffAfterEncodeAndDecode(t, b)
}
3 changes: 3 additions & 0 deletions mp4/stbl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type StblBox struct {
Stsz *StszBox
Stss *StssBox
Stco *StcoBox
Co64 *Co64Box
Sdtp *SdtpBox
Sbgp *SbgpBox // The first
Sbgps []*SbgpBox // All
Expand Down Expand Up @@ -53,6 +54,8 @@ func (s *StblBox) AddChild(box Box) {
s.Stss = box.(*StssBox)
case "stco":
s.Stco = box.(*StcoBox)
case "co64":
s.Co64 = box.(*Co64Box)
case "sbgp":
if s.Sbgp == nil {
s.Sbgp = box.(*SbgpBox)
Expand Down

0 comments on commit c734943

Please sign in to comment.