1
1
mirror of https://github.com/wader/fq.git synced 2024-11-28 03:02:55 +03:00
fq/format/mpeg/annexb.go

48 lines
1.0 KiB
Go
Raw Normal View History

2021-08-31 20:32:34 +03:00
package mpeg
import (
"github.com/wader/fq/pkg/decode"
)
func annexBFindStartCode(d *decode.D) (int64, int64, error) {
offset, v, err := d.TryPeekFind(32, 8, -1, func(v uint64) bool {
2021-08-31 20:32:34 +03:00
return annexBDecodeStartCodeLen(v) > 0
})
2021-08-31 20:32:34 +03:00
return offset, annexBDecodeStartCodeLen(v), err
}
func annexBDecodeStartCodeLen(v uint64) int64 {
switch {
case v == 0x00_00_00_01:
return 4 * 8
case v&0xff_ff_ff_00 == 0x00_00_01_00:
return 3 * 8
default:
return 0
}
}
func annexBDecode(d *decode.D, _ any, format decode.Group) any {
2021-08-31 20:32:34 +03:00
currentOffset, currentPrefixLen, err := annexBFindStartCode(d)
// TODO: really restrict to 0?
if err != nil || currentOffset != 0 {
d.Errorf("could not find start code (first)")
2021-08-31 20:32:34 +03:00
}
2021-09-16 17:26:31 +03:00
for d.NotEnd() {
d.FieldRawLen("start_code", currentPrefixLen)
2021-08-31 20:32:34 +03:00
2021-09-16 17:26:31 +03:00
nextOffset, nextPrefixLen, err := annexBFindStartCode(d)
if err != nil {
nextOffset = d.Len() - d.Pos()
}
2021-08-31 20:32:34 +03:00
2021-09-16 17:26:31 +03:00
naluLen := nextOffset
d.FieldFormatLen("nalu", naluLen, format, nil)
2021-08-31 20:32:34 +03:00
2021-09-16 17:26:31 +03:00
currentPrefixLen = nextPrefixLen
}
2021-08-31 20:32:34 +03:00
return nil
}