fix compatibility with Annex-B encoded H264 SPS/PPS (#402) (#426)

This commit is contained in:
Alessandro Ros
2023-09-18 22:24:19 +02:00
committed by GitHub
parent a29348e65f
commit 747de35cbc
3 changed files with 43 additions and 9 deletions

View File

@@ -746,6 +746,35 @@ var casesFormat = []struct {
"packetization-mode": "1", "packetization-mode": "1",
}, },
}, },
{
"video h264 annexb",
"video",
96,
"H264/90000",
map[string]string{
"sprop-parameter-sets": "AAAAAWdNAB6NjUBaHtCAAAOEAACvyAI=,AAAAAWjuOIA=",
"packetization-mode": "1",
"profile-level-id": "4DE028",
},
&H264{
PayloadTyp: 96,
SPS: []byte{
0x67, 0x4d, 0x00, 0x1e, 0x8d, 0x8d, 0x40, 0x5a,
0x1e, 0xd0, 0x80, 0x00, 0x03, 0x84, 0x00, 0x00,
0xaf, 0xc8, 0x02,
},
PPS: []byte{
0x68, 0xee, 0x38, 0x80,
},
PacketizationMode: 1,
},
"H264/90000",
map[string]string{
"packetization-mode": "1",
"profile-level-id": "4D001E",
"sprop-parameter-sets": "Z00AHo2NQFoe0IAAA4QAAK/IAg==,aO44gA==",
},
},
{ {
"video h265", "video h265",
"video", "video",

View File

@@ -1,6 +1,7 @@
package format package format
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
@@ -33,24 +34,28 @@ func (f *H264) unmarshal(ctx *unmarshalContext) error {
case "sprop-parameter-sets": case "sprop-parameter-sets":
tmp := strings.Split(val, ",") tmp := strings.Split(val, ",")
if len(tmp) >= 2 { if len(tmp) >= 2 {
sps, err := base64.StdEncoding.DecodeString(tmp[0]) var err error
f.SPS, err = base64.StdEncoding.DecodeString(tmp[0])
if err != nil { if err != nil {
return fmt.Errorf("invalid sprop-parameter-sets (%v)", val) return fmt.Errorf("invalid sprop-parameter-sets (%v)", val)
} }
pps, err := base64.StdEncoding.DecodeString(tmp[1]) // some cameras ship parameters with Annex-B prefix
f.SPS = bytes.TrimPrefix(f.SPS, []byte{0, 0, 0, 1})
f.PPS, err = base64.StdEncoding.DecodeString(tmp[1])
if err != nil { if err != nil {
return fmt.Errorf("invalid sprop-parameter-sets (%v)", val) return fmt.Errorf("invalid sprop-parameter-sets (%v)", val)
} }
// some cameras ship parameters with Annex-B prefix
f.PPS = bytes.TrimPrefix(f.PPS, []byte{0, 0, 0, 1})
var spsp h264.SPS var spsp h264.SPS
err = spsp.Unmarshal(sps) err = spsp.Unmarshal(f.SPS)
if err != nil { if err != nil {
return fmt.Errorf("invalid SPS: %v", err) return fmt.Errorf("invalid SPS: %v", err)
} }
f.SPS = sps
f.PPS = pps
} }
case "packetization-mode": case "packetization-mode":

View File

@@ -37,7 +37,7 @@ func (f *H265) unmarshal(ctx *unmarshalContext) error {
return fmt.Errorf("invalid sprop-vps (%v)", ctx.fmtp) return fmt.Errorf("invalid sprop-vps (%v)", ctx.fmtp)
} }
// some cameras ships parameters with Annex-B prefix // some cameras ship parameters with Annex-B prefix
f.VPS = bytes.TrimPrefix(f.VPS, []byte{0, 0, 0, 1}) f.VPS = bytes.TrimPrefix(f.VPS, []byte{0, 0, 0, 1})
case "sprop-sps": case "sprop-sps":
@@ -47,7 +47,7 @@ func (f *H265) unmarshal(ctx *unmarshalContext) error {
return fmt.Errorf("invalid sprop-sps (%v)", ctx.fmtp) return fmt.Errorf("invalid sprop-sps (%v)", ctx.fmtp)
} }
// some cameras ships parameters with Annex-B prefix // some cameras ship parameters with Annex-B prefix
f.SPS = bytes.TrimPrefix(f.SPS, []byte{0, 0, 0, 1}) f.SPS = bytes.TrimPrefix(f.SPS, []byte{0, 0, 0, 1})
var spsp h265.SPS var spsp h265.SPS
@@ -63,7 +63,7 @@ func (f *H265) unmarshal(ctx *unmarshalContext) error {
return fmt.Errorf("invalid sprop-pps (%v)", ctx.fmtp) return fmt.Errorf("invalid sprop-pps (%v)", ctx.fmtp)
} }
// some cameras ships parameters with Annex-B prefix // some cameras ship parameters with Annex-B prefix
f.PPS = bytes.TrimPrefix(f.PPS, []byte{0, 0, 0, 1}) f.PPS = bytes.TrimPrefix(f.PPS, []byte{0, 0, 0, 1})
var ppsp h265.PPS var ppsp h265.PPS