Skip to content

Commit 16aaa10

Browse files
authored
pmp4: improve compatibility (bluenviron/mediamtx#5423) (#298)
1 parent c6b64a9 commit 16aaa10

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

pkg/formats/pmp4/presentation.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,19 @@ func (p *Presentation) Unmarshal(r io.ReadSeeker) error {
292292
stsc := box.(*amp4.Stsc)
293293

294294
if len(stsc.Entries) != 0 {
295-
prevFirstChunk := uint32(0)
296-
i := 0
295+
sampleCount := 0
297296

298-
for _, entry := range stsc.Entries {
299-
chunkCount := entry.FirstChunk - prevFirstChunk
297+
for i, entry := range stsc.Entries {
298+
var chunkCount uint32
299+
if i != (len(stsc.Entries) - 1) {
300+
chunkCount = stsc.Entries[i+1].FirstChunk - entry.FirstChunk
301+
} else {
302+
remaining := len(curTrack.Samples) - sampleCount
303+
if (remaining % int(entry.SamplesPerChunk)) != 0 {
304+
return nil, fmt.Errorf("invalid stsc")
305+
}
306+
chunkCount = uint32(remaining / int(entry.SamplesPerChunk))
307+
}
300308

301309
if (len(curChunks) + int(chunkCount)) > maxChunks {
302310
return nil, fmt.Errorf("invalid stsc")
@@ -307,32 +315,15 @@ func (p *Presentation) Unmarshal(r io.ReadSeeker) error {
307315
}
308316

309317
for range chunkCount {
310-
if (i + int(entry.SamplesPerChunk)) > len(curTrack.Samples) {
318+
if (sampleCount + int(entry.SamplesPerChunk)) > len(curTrack.Samples) {
311319
return nil, fmt.Errorf("invalid stsc")
312320
}
313321

314322
curChunks = append(curChunks, &chunk{
315323
sampleCount: int(entry.SamplesPerChunk),
316324
})
317325

318-
i += int(entry.SamplesPerChunk)
319-
}
320-
prevFirstChunk = entry.FirstChunk
321-
}
322-
323-
if i != len(curTrack.Samples) {
324-
remaining := len(curTrack.Samples) - i
325-
lastEntry := stsc.Entries[len(stsc.Entries)-1]
326-
327-
if (remaining % int(lastEntry.SamplesPerChunk)) != 0 {
328-
return nil, fmt.Errorf("invalid stsc")
329-
}
330-
331-
count := remaining / int(lastEntry.SamplesPerChunk)
332-
for range count {
333-
curChunks = append(curChunks, &chunk{
334-
sampleCount: int(lastEntry.SamplesPerChunk),
335-
})
326+
sampleCount += int(entry.SamplesPerChunk)
336327
}
337328
}
338329
}
@@ -400,7 +391,7 @@ func (p *Presentation) Unmarshal(r io.ReadSeeker) error {
400391
}
401392
}
402393

403-
case "sdtp":
394+
case "sdtp", "sgpd", "sbgp":
404395
if state != waitingSampleProps {
405396
return nil, fmt.Errorf("unexpected box '%v'", h.BoxInfo.Type)
406397
}

0 commit comments

Comments
 (0)