codec/h264/parse.go: skip over access unit delimiters when getting nalType

This commit is contained in:
Saxon 2019-09-11 09:47:37 +09:30
parent 54fb4a551e
commit ecdaab175b
2 changed files with 25 additions and 16 deletions

View File

@ -11,7 +11,7 @@ const (
naluTypeSEI naluTypeSEI
NALTypeSPS NALTypeSPS
naluTypePPS naluTypePPS
naluTypeAccessUnitDelimiter NALTypeAccessUnitDelimiter
naluTypeEndOfSequence naluTypeEndOfSequence
naluTypeEndOfStream naluTypeEndOfStream
naluTypeFillerData naluTypeFillerData

View File

@ -26,34 +26,43 @@ LICENSE
package h264 package h264
import "errors" import (
"errors"
"bitbucket.org/ausocean/av/codec/h264/h264dec"
)
var errNotEnoughBytes = errors.New("not enough bytes to read") var errNotEnoughBytes = errors.New("not enough bytes to read")
// NALType returns the NAL type of the given NAL unit bytes. The given NAL unit // NALType returns the NAL type of the given NAL unit bytes. The given NAL unit
// may be in byte stream or packet format. // may be in byte stream or packet format.
// NB: access unit delimiters are skipped.
func NALType(n []byte) (int, error) { func NALType(n []byte) (int, error) {
sc := frameScanner{buf: n} sc := frameScanner{buf: n}
b, ok := sc.readByte() for {
if !ok { b, ok := sc.readByte()
return 0, errNotEnoughBytes
}
for i := 1; b == 0x00 && i != 4; i++ {
b, ok = sc.readByte()
if !ok { if !ok {
return 0, errNotEnoughBytes return 0, errNotEnoughBytes
} }
if b != 0x01 || (i != 2 && i != 3) { for i := 1; b == 0x00 && i != 4; i++ {
continue b, ok = sc.readByte()
} if !ok {
return 0, errNotEnoughBytes
}
if b != 0x01 || (i != 2 && i != 3) {
continue
}
b, ok = sc.readByte() b, ok = sc.readByte()
if !ok { if !ok {
return 0, errNotEnoughBytes return 0, errNotEnoughBytes
}
nalType := int(b & 0x1f)
if nalType != h264dec.NALTypeAccessUnitDelimiter {
return nalType, nil
}
} }
return int(b & 0x1f), nil
} }
return int(b & 0x1f), nil
} }
type frameScanner struct { type frameScanner struct {