From 704538355c24be072226d0c13b6f12d5dc70d3e9 Mon Sep 17 00:00:00 2001 From: guoqiang Date: Mon, 6 May 2024 16:43:18 +0800 Subject: [PATCH] =?UTF-8?q?[feat]:=20=E5=A2=9E=E5=8A=A0=E4=BB=8EAnnexb?= =?UTF-8?q?=E4=B8=AD=E8=A7=A3=E6=9E=90vps=E3=80=81sps=E3=80=81pps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/hevc/hevc.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/pkg/hevc/hevc.go b/pkg/hevc/hevc.go index 8d0d89cc..e9d5c836 100644 --- a/pkg/hevc/hevc.go +++ b/pkg/hevc/hevc.go @@ -251,7 +251,44 @@ func ParseVpsSpsPpsFromSeqHeaderWithoutMalloc(payload []byte) (vps, sps, pps []b return nil, nil, nil, nazaerrors.Wrap(base.ErrHevc) } - return parseVpsSpsPpsFromRecord(payload) + vps, sps, pps, err = parseVpsSpsPpsFromRecord(payload) + if err != nil { + vps, sps, pps, err = parseVpsSpsPpsAnnexbFromRecord(payload) + } + + return +} + +func parseVpsSpsPpsAnnexbFromRecord(payload []byte) (vps, sps, pps []byte, err error) { + for i := 0; i < len(payload)-4; { + start := bytes.Index(payload[i:], NaluStartCode4) + if start == -1 { + break + } + i += start + var end int + if endIdx := bytes.Index(payload[i+4:], NaluStartCode4); endIdx != -1 { + end = endIdx + 4 + } else { + end = len(payload) - i + } + nal := payload[i+4 : i+end] + typ := ParseNaluType(nal[0]) + switch typ { + case NaluTypeVps: + vps = append(vps, nal...) + case NaluTypeSps: + sps = append(sps, nal...) + case NaluTypePps: + pps = append(pps, nal...) + } + i += end + } + + if len(vps) == 0 || len(sps) == 0 || len(pps) == 0 { + return nil, nil, nil, nazaerrors.Wrap(base.ErrHevc) + } + return } func parseVpsSpsPpsFromRecord(payload []byte) (vps, sps, pps []byte, err error) {