Skip to content

Commit df2d886

Browse files
committed
fix: handle trailing bytes in avc1 box (issue #444)
Proper fix for a bigger context with boxes after the avc1 box. New test content added.
1 parent 80edc2d commit df2d886

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed
3.22 KB
Binary file not shown.

mp4/visualsampleentry.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ func DecodeVisualSampleEntrySR(hdr BoxHeader, startPos uint64, sr bits.SliceRead
131131
sr.SkipBytes(2) // Skip depth
132132
sr.ReadUint16() // pre_defined == -1
133133

134-
// Now there may be clap and pasp boxes
134+
// Now there may be clap, pasp, btrt and other boxes
135135
// 14496-15 5.4.2.1.2 avcC should be inside avc1, avc3 box
136136
pos := startPos + 86 // Size of all previous data
137137
endPos := startPos + uint64(hdr.Hdrlen) + uint64(hdr.payloadLen())
138138
for pos < endPos {
139-
if sr.NrRemainingBytes() < boxHeaderSize {
139+
remainingBytes := endPos - pos
140+
if remainingBytes < boxHeaderSize {
140141
// This should not happen, but was reported in issue #444
141-
b.TrailingBytes = sr.ReadBytes(sr.NrRemainingBytes())
142+
b.TrailingBytes = sr.ReadBytes(int(remainingBytes))
142143
break
143144
}
144145
box, err := DecodeBoxSR(pos, sr)

mp4/visualsampleentry_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mp4_test
22

33
import (
44
"encoding/hex"
5+
"os"
56
"testing"
67

78
"github.com/Eyevinn/mp4ff/bits"
@@ -61,28 +62,21 @@ func TestVisualSampleEntryBoxVP9(t *testing.T) {
6162
}
6263

6364
func TestAvc1WithTrailingBytes(t *testing.T) {
64-
avc1Hex := "0000008b6176633100000000000000010000000000000000000000000000000002800168004800000048000000000000000100" +
65-
"000000000000000000000000000000000000000000000000000000000000000018ffff00000031617663430164001effe100196764001ea" +
66-
"cd940a02ff9610000030001000003003c8f162d9601000568ebecb22c00000000"
67-
avc1Raw, err := hex.DecodeString(avc1Hex)
65+
minfWithTrailingAvc1Bytes, err := os.ReadFile("testdata/minf_with_trailing_avc1_bytes.bin")
6866
if err != nil {
69-
t.Error(err)
67+
t.Fatal(err)
7068
}
71-
sr := bits.NewFixedSliceReader(avc1Raw)
69+
sr := bits.NewFixedSliceReader(minfWithTrailingAvc1Bytes)
7270
// Decode the box
7371
box, err := mp4.DecodeBoxSR(0, sr)
7472
if err != nil {
7573
t.Error(err)
7674
}
7775
// Check the box type
78-
if box.Type() != "avc1" {
79-
t.Errorf("expected box type avc1, got %s", box.Type())
80-
}
81-
// Check the box size
82-
if box.Size() != uint64(len(avc1Raw)) {
83-
t.Errorf("expected box size %d, got %d", len(avc1Raw), box.Size())
76+
if box.Type() != "minf" {
77+
t.Errorf("expected box type minf, got %s", box.Type())
8478
}
85-
avc1 := box.(*mp4.VisualSampleEntryBox)
79+
avc1 := box.(*mp4.MinfBox).Stbl.Stsd.Children[0].(*mp4.VisualSampleEntryBox)
8680
if len(avc1.TrailingBytes) != 4 {
8781
t.Errorf("expected 4 trailing bytes, got %d", len(avc1.TrailingBytes))
8882
}

0 commit comments

Comments
 (0)