From ecdaab175b098b4f86acab29d867bf36dfa3e3dd Mon Sep 17 00:00:00 2001 From: Saxon Date: Wed, 11 Sep 2019 09:47:37 +0930 Subject: [PATCH] codec/h264/parse.go: skip over access unit delimiters when getting nalType --- codec/h264/h264dec/frame.go | 2 +- codec/h264/parse.go | 39 +++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/codec/h264/h264dec/frame.go b/codec/h264/h264dec/frame.go index ca85982b..1904a09d 100644 --- a/codec/h264/h264dec/frame.go +++ b/codec/h264/h264dec/frame.go @@ -11,7 +11,7 @@ const ( naluTypeSEI NALTypeSPS naluTypePPS - naluTypeAccessUnitDelimiter + NALTypeAccessUnitDelimiter naluTypeEndOfSequence naluTypeEndOfStream naluTypeFillerData diff --git a/codec/h264/parse.go b/codec/h264/parse.go index 9465fb1c..accf68cf 100644 --- a/codec/h264/parse.go +++ b/codec/h264/parse.go @@ -26,34 +26,43 @@ LICENSE package h264 -import "errors" +import ( + "errors" + + "bitbucket.org/ausocean/av/codec/h264/h264dec" +) var errNotEnoughBytes = errors.New("not enough bytes to read") // NALType returns the NAL type of the given NAL unit bytes. The given NAL unit // may be in byte stream or packet format. +// NB: access unit delimiters are skipped. func NALType(n []byte) (int, error) { sc := frameScanner{buf: n} - b, ok := sc.readByte() - if !ok { - return 0, errNotEnoughBytes - } - for i := 1; b == 0x00 && i != 4; i++ { - b, ok = sc.readByte() + for { + b, ok := sc.readByte() if !ok { return 0, errNotEnoughBytes } - if b != 0x01 || (i != 2 && i != 3) { - continue - } + for i := 1; b == 0x00 && i != 4; i++ { + b, ok = sc.readByte() + if !ok { + return 0, errNotEnoughBytes + } + if b != 0x01 || (i != 2 && i != 3) { + continue + } - b, ok = sc.readByte() - if !ok { - return 0, errNotEnoughBytes + b, ok = sc.readByte() + if !ok { + 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 {