Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for MP4 files with CO64 atom instead of STCO #102

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion format/mp4/demuxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (self *Demuxer) probe() (err error) {
}

var moov *mp4io.Movie
var mdat *mp4io.Dummy
var atoms []mp4io.Atom

if atoms, err = mp4io.ReadFileAtoms(self.r); err != nil {
Expand All @@ -62,6 +63,8 @@ func (self *Demuxer) probe() (err error) {
for _, atom := range atoms {
if atom.Tag() == mp4io.MOOV {
moov = atom.(*mp4io.Movie)
} else if atom.Tag() == mp4io.MDAT {
mdat = atom.(*mp4io.Dummy)
}
}

Expand All @@ -85,6 +88,10 @@ func (self *Demuxer) probe() (err error) {
return
}

if stream.sample.ChunkOffset == nil {
stream.restoreChunkOffsets(mdat)
}

if avc1 := atrack.GetAVC1Conf(); avc1 != nil {
if stream.CodecData, err = h264parser.NewCodecDataFromAVCDecoderConfRecord(avc1.Data); err != nil {
return
Expand All @@ -102,6 +109,38 @@ func (self *Demuxer) probe() (err error) {
return
}

func (self *Stream) restoreChunkOffsets(mdat *mp4io.Dummy) {
var dataOffset int64
if mdat.Size == 1 {
dataOffset = int64(mdat.Offset + 16) // account for extended size field
} else {
dataOffset = int64(mdat.Offset + 8)
}

offset := dataOffset
chunkGroupIndex := 0
chunkOffsets := []int64{offset}
chunkIndex := uint32(1)
sampleIndexInChunk := uint32(0)
for _, sampleSize := range self.sample.SampleSize.Entries {
offset += int64(sampleSize)
sampleIndexInChunk++
if sampleIndexInChunk >= self.sample.SampleToChunk.Entries[chunkGroupIndex].SamplesPerChunk {
chunkOffsets = append(chunkOffsets, offset)
chunkIndex++
sampleIndexInChunk = 0
}
if chunkGroupIndex+1 < len(self.sample.SampleToChunk.Entries) &&
chunkIndex == self.sample.SampleToChunk.Entries[chunkGroupIndex+1].FirstChunk {
chunkGroupIndex++
}
}

self.sample.ChunkOffset = &mp4io.ChunkOffset{
Entries: chunkOffsets,
}
}

func (self *Stream) setSampleIndex(index int) (err error) {
found := false
start := 0
Expand Down Expand Up @@ -371,7 +410,7 @@ func (self *Stream) readPacket() (pkt av.Packet, err error) {
sampleSize = self.sample.SampleSize.Entries[self.sampleIndex]
}

sampleOffset := int64(chunkOffset) + self.sampleOffsetInChunk
sampleOffset := chunkOffset + self.sampleOffsetInChunk
pkt.Data = make([]byte, sampleSize)
if err = self.demuxer.readat(sampleOffset, pkt.Data); err != nil {
return
Expand Down
Loading