Skip to content

Commit f5f5834

Browse files
authored
improve pmp4 and fmp4 parser robustness (#299)
1 parent 16aaa10 commit f5f5834

File tree

4 files changed

+153
-57
lines changed

4 files changed

+153
-57
lines changed

internal/mp4/codec_boxes.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ type CodecBoxesReader struct {
170170
// ReadCodecBoxes reads codec-related boxes.
171171
func (r *CodecBoxesReader) Read(h *amp4.ReadHandle) (any, error) {
172172
if len(h.Path) < 7 {
173+
if r.state != waitingAdditional {
174+
return nil, fmt.Errorf("codec information not found")
175+
}
176+
173177
return nil, ErrReadEnded
174178
}
175179

@@ -568,14 +572,6 @@ func (r *CodecBoxesReader) Read(h *amp4.ReadHandle) (any, error) {
568572
ChannelCount: r.channelCount,
569573
}
570574
r.state = waitingAdditional
571-
572-
case "pasp", "colr", "fiel", "chrm", "btrt":
573-
if r.state != waitingAdditional {
574-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
575-
}
576-
577-
default:
578-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
579575
}
580576

581577
return nil, nil

pkg/formats/fmp4/init.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ func (i *Init) Unmarshal(r io.ReadSeeker) error {
7777

7878
state = waitingMoov
7979

80-
case "free":
81-
if state == waitingFtyp {
82-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
83-
}
84-
8580
case "moov":
8681
if state != waitingMoov {
8782
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
@@ -106,11 +101,6 @@ func (i *Init) Unmarshal(r io.ReadSeeker) error {
106101
state = waitingTrak
107102
return h.Expand()
108103

109-
case "mvex", "stts", "stsc", "stsz", "stco":
110-
if state != waitingTrak {
111-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
112-
}
113-
114104
case "trak":
115105
if state != waitingTrak {
116106
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
@@ -160,11 +150,6 @@ func (i *Init) Unmarshal(r io.ReadSeeker) error {
160150
curTrack.TimeScale = mdhd.Timescale
161151
state = waitingStsd
162152

163-
case "hdlr", "vmhd", "smhd", "nmhd", "dinf":
164-
if state != waitingStsd {
165-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
166-
}
167-
168153
case "minf", "stbl":
169154
if state != waitingStsd {
170155
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
@@ -180,12 +165,6 @@ func (i *Init) Unmarshal(r io.ReadSeeker) error {
180165
codecBoxesReader = &imp4.CodecBoxesReader{}
181166
state = readingCodec
182167
return h.Expand()
183-
184-
case "moof", "mdat":
185-
return nil, nil
186-
187-
default:
188-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
189168
}
190169

191170
return nil, nil

pkg/formats/pmp4/presentation.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ func (p *Presentation) Unmarshal(r io.ReadSeeker) error {
8989

9090
state = waitingMoov
9191

92-
case "free":
93-
if state == waitingFtyp {
94-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
95-
}
96-
9792
case "moov":
9893
if state != waitingMoov {
9994
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
@@ -110,11 +105,6 @@ func (p *Presentation) Unmarshal(r io.ReadSeeker) error {
110105
state = waitingTrak
111106
return h.Expand()
112107

113-
case "udta":
114-
if state != waitingTrak && state != waitingSampleProps {
115-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
116-
}
117-
118108
case "trak":
119109
if state != waitingTrak && state != waitingSampleProps {
120110
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
@@ -181,11 +171,6 @@ func (p *Presentation) Unmarshal(r io.ReadSeeker) error {
181171
trackDuration = mdhd.DurationV0
182172
state = waitingStsd
183173

184-
case "hdlr", "vmhd", "smhd", "nmhd", "dinf":
185-
if state != waitingStsd {
186-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
187-
}
188-
189174
case "minf", "stbl":
190175
if state != waitingStsd {
191176
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
@@ -390,19 +375,6 @@ func (p *Presentation) Unmarshal(r io.ReadSeeker) error {
390375
i++
391376
}
392377
}
393-
394-
case "sdtp", "sgpd", "sbgp":
395-
if state != waitingSampleProps {
396-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
397-
}
398-
399-
case "mdat":
400-
if state != waitingTrak && state != waitingSampleProps && state != waitingMoov {
401-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
402-
}
403-
404-
default:
405-
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
406378
}
407379

408380
return nil, nil

pkg/formats/pmp4/presentation_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,155 @@ func TestPresentationUnmarshalOnly(t *testing.T) {
21502150
}},
21512151
},
21522152
},
2153+
{
2154+
"uuid",
2155+
[]byte{
2156+
0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70,
2157+
0x69, 0x73, 0x6f, 0x6d, 0x00, 0x00, 0x02, 0x00,
2158+
0x69, 0x73, 0x6f, 0x6d, 0x69, 0x73, 0x6f, 0x32,
2159+
0x61, 0x76, 0x63, 0x31, 0x6d, 0x70, 0x34, 0x31,
2160+
0x00, 0x00, 0x00, 0x39, 0x75, 0x75, 0x69, 0x64,
2161+
0x6b, 0x68, 0x40, 0xf2, 0x5f, 0x24, 0x4f, 0xc5,
2162+
0xba, 0x39, 0xa5, 0x1b, 0xcf, 0x03, 0x23, 0x38,
2163+
0x53, 0x6f, 0x6d, 0x65, 0x20, 0x61, 0x64, 0x64,
2164+
0x69, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x20,
2165+
0x55, 0x55, 0x49, 0x44, 0x20, 0x64, 0x61, 0x74,
2166+
0x61, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
2167+
0x74, 0x00, 0x00, 0x00, 0x10, 0x66, 0x72, 0x65,
2168+
0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2169+
0x00, 0x00, 0x00, 0x00, 0x3a, 0x6d, 0x64, 0x61,
2170+
0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2171+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2172+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2173+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2174+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2175+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2176+
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xd4, 0x6d,
2177+
0x6f, 0x6f, 0x76, 0x00, 0x00, 0x00, 0x6c, 0x6d,
2178+
0x76, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00,
2179+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2180+
0x00, 0x03, 0xe8, 0x00, 0x00, 0x03, 0xe8, 0x00,
2181+
0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
2182+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2183+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2184+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2185+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2186+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
2187+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2188+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2189+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2190+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
2191+
0x00, 0x02, 0x60, 0x74, 0x72, 0x61, 0x6b, 0x00,
2192+
0x00, 0x00, 0x5c, 0x74, 0x6b, 0x68, 0x64, 0x00,
2193+
0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
2194+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2195+
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x00,
2196+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2197+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2198+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2199+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2200+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2201+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
2202+
0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x02,
2203+
0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x65,
2204+
0x64, 0x74, 0x73, 0x00, 0x00, 0x00, 0x1c, 0x65,
2205+
0x6c, 0x73, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00,
2206+
0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0xe8, 0x00,
2207+
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
2208+
0x00, 0x01, 0xd8, 0x6d, 0x64, 0x69, 0x61, 0x00,
2209+
0x00, 0x00, 0x20, 0x6d, 0x64, 0x68, 0x64, 0x00,
2210+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2211+
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x00,
2212+
0x00, 0x03, 0xe8, 0x55, 0xc4, 0x00, 0x00, 0x00,
2213+
0x00, 0x00, 0x2d, 0x68, 0x64, 0x6c, 0x72, 0x00,
2214+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76,
2215+
0x69, 0x64, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00,
2216+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56,
2217+
0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64,
2218+
0x6c, 0x65, 0x72, 0x00, 0x00, 0x00, 0x01, 0x83,
2219+
0x6d, 0x69, 0x6e, 0x66, 0x00, 0x00, 0x00, 0x14,
2220+
0x76, 0x6d, 0x68, 0x64, 0x00, 0x00, 0x00, 0x01,
2221+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2222+
0x00, 0x00, 0x00, 0x24, 0x64, 0x69, 0x6e, 0x66,
2223+
0x00, 0x00, 0x00, 0x1c, 0x64, 0x72, 0x65, 0x66,
2224+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
2225+
0x00, 0x00, 0x00, 0x0c, 0x75, 0x72, 0x6c, 0x20,
2226+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x43,
2227+
0x73, 0x74, 0x62, 0x6c, 0x00, 0x00, 0x00, 0xaf,
2228+
0x73, 0x74, 0x73, 0x64, 0x00, 0x00, 0x00, 0x00,
2229+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x9f,
2230+
0x61, 0x76, 0x63, 0x31, 0x00, 0x00, 0x00, 0x00,
2231+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
2232+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2233+
0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x02, 0xd0,
2234+
0x00, 0x48, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00,
2235+
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
2236+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2237+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2238+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2239+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18,
2240+
0xff, 0xff, 0x00, 0x00, 0x00, 0x35, 0x61, 0x76,
2241+
0x63, 0x43, 0x01, 0x64, 0x00, 0x1f, 0xff, 0xe1,
2242+
0x00, 0x1c, 0x67, 0x64, 0x00, 0x1f, 0xac, 0xd9,
2243+
0x40, 0x50, 0x05, 0xbb, 0x01, 0x6a, 0x02, 0x02,
2244+
0x02, 0x80, 0x00, 0x00, 0x03, 0x00, 0x80, 0x00,
2245+
0x00, 0x1e, 0x07, 0x8c, 0x18, 0xcb, 0x01, 0x00,
2246+
0x06, 0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0, 0x00,
2247+
0x00, 0x00, 0x14, 0x62, 0x74, 0x72, 0x74, 0x00,
2248+
0x00, 0x00, 0x00, 0x00, 0x0f, 0x42, 0x40, 0x00,
2249+
0x0c, 0x35, 0x00, 0x00, 0x00, 0x00, 0x18, 0x73,
2250+
0x74, 0x74, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
2251+
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
2252+
0x00, 0x03, 0xe8, 0x00, 0x00, 0x00, 0x14, 0x73,
2253+
0x74, 0x73, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
2254+
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
2255+
0x00, 0x00, 0x18, 0x63, 0x74, 0x74, 0x73, 0x00,
2256+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2257+
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
2258+
0x00, 0x00, 0x1c, 0x73, 0x74, 0x73, 0x63, 0x00,
2259+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2260+
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
2261+
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x73,
2262+
0x74, 0x73, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00,
2263+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2264+
0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x14, 0x73,
2265+
0x74, 0x63, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00,
2266+
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x71, 0x00,
2267+
0x00, 0x00, 0x08, 0x75, 0x64, 0x74, 0x61, 0x00,
2268+
0x00, 0x00, 0x41, 0x6d, 0x65, 0x74, 0x61, 0x00,
2269+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x68,
2270+
0x64, 0x6c, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
2271+
0x00, 0x00, 0x00, 0x6d, 0x64, 0x69, 0x72, 0x00,
2272+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2273+
0x00, 0x00, 0x00, 0x56, 0x69, 0x64, 0x65, 0x6f,
2274+
0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00,
2275+
0x00, 0x00, 0x00, 0x08, 0x69, 0x6c, 0x73, 0x74,
2276+
},
2277+
Presentation{
2278+
Tracks: []*Track{{
2279+
ID: 1,
2280+
TimeScale: 1000,
2281+
Codec: &codecs.H264{
2282+
SPS: []byte{
2283+
0x67, 0x64, 0x00, 0x1f, 0xac, 0xd9, 0x40, 0x50,
2284+
0x05, 0xbb, 0x01, 0x6a, 0x02, 0x02, 0x02, 0x80,
2285+
0x00, 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x1e,
2286+
0x07, 0x8c, 0x18, 0xcb,
2287+
},
2288+
PPS: []byte{0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0},
2289+
},
2290+
Samples: []*Sample{
2291+
{
2292+
Duration: 1000,
2293+
PayloadSize: 50,
2294+
GetPayload: func() ([]byte, error) {
2295+
return make([]byte, 50), nil
2296+
},
2297+
},
2298+
},
2299+
}},
2300+
},
2301+
},
21532302
} {
21542303
t.Run(ca.name, func(t *testing.T) {
21552304
var p Presentation

0 commit comments

Comments
 (0)