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

avi: Handle stream sample size #776

Merged
merged 1 commit into from
Oct 7, 2023
Merged
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
28 changes: 22 additions & 6 deletions format/riff/avi.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package riff

// TODO:
// mp3 mappig, seem there can be many frames in one sample and they span samples?
// mp3 mappig, samples can span?
// hevc mapping?
// DV handler https://learn.microsoft.com/en-us/windows/win32/directshow/dv-data-in-the-avi-file-format
// palette change
Expand Down Expand Up @@ -120,6 +120,7 @@ type aviStream struct {
hasFormat bool
format *decode.Group
formatInArg any
sampleSize uint64
indexes []ranges.Range
ixSamples []ranges.Range
}
Expand Down Expand Up @@ -327,7 +328,7 @@ func aviDecode(d *decode.D) any {
d.FieldU32("length")
d.FieldU32("suggested_buffer_size")
d.FieldU32("quality")
d.FieldU32("sample_size")
sampleSize := d.FieldU32("sample_size")
d.FieldStruct("frame", func(d *decode.D) {
d.FieldU16("left")
d.FieldU16("top")
Expand All @@ -338,6 +339,7 @@ func aviDecode(d *decode.D) any {
if stream, ok := path.topData().(*aviStream); ok {
stream.typ = typ
stream.handler = handler
stream.sampleSize = sampleSize
}

return false, nil
Expand Down Expand Up @@ -557,14 +559,28 @@ func aviDecode(d *decode.D) any {
})
}

// TODO: handle zero length samples differently?
// TODO: palette change
decodeSample := func(d *decode.D, sr ranges.Range) {
d.RangeFn(sr.Start, sr.Len, func(d *decode.D) {
if sr.Len > 0 && ai.DecodeSamples && stream.hasFormat {
d.FieldFormat("sample", stream.format, stream.formatInArg)
} else {
if sr.Len == 0 {
d.FieldRawLen("sample", d.BitsLeft())
return
}

subSampleSize := int64(stream.sampleSize) * 8
// TODO: <= 4*8 heuristics to not create separate pcm samples
if subSampleSize == 0 || subSampleSize <= 4*8 {
subSampleSize = sr.Len
}

for d.BitsLeft() > 0 {
d.FramedFn(subSampleSize, func(d *decode.D) {
if ai.DecodeSamples && stream.hasFormat {
d.FieldFormat("sample", stream.format, stream.formatInArg)
} else {
d.FieldRawLen("sample", d.BitsLeft())
}
})
}
})
}
Expand Down