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) {
|
2021-09-07 15:05:48 +03:00
|
|
|
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-09-07 15:05:48 +03:00
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 00:42:41 +03:00
|
|
|
func annexBDecode(d *decode.D, _ interface{}, format []*decode.Format) interface{} {
|
2021-08-31 20:32:34 +03:00
|
|
|
currentOffset, currentPrefixLen, err := annexBFindStartCode(d)
|
|
|
|
// TODO: really restrict to 0?
|
|
|
|
if err != nil || currentOffset != 0 {
|
2021-11-16 19:11:26 +03:00
|
|
|
d.Error("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() {
|
2021-11-05 17:04:26 +03:00
|
|
|
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
|
|
|
|
}
|