Skip to content

Commit

Permalink
fix compatibility with Annex-B encoded H265 SPS/PPS (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Sep 18, 2023
1 parent a29348e commit b8f8275
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
29 changes: 29 additions & 0 deletions pkg/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,35 @@ var casesFormat = []struct {
"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",
Expand Down
17 changes: 11 additions & 6 deletions pkg/format/h264.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package format

import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
Expand Down Expand Up @@ -33,24 +34,28 @@ func (f *H264) unmarshal(ctx *unmarshalContext) error {
case "sprop-parameter-sets":
tmp := strings.Split(val, ",")
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 {
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 {
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
err = spsp.Unmarshal(sps)
err = spsp.Unmarshal(f.SPS)
if err != nil {
return fmt.Errorf("invalid SPS: %v", err)
}

f.SPS = sps
f.PPS = pps
}

case "packetization-mode":
Expand Down
6 changes: 3 additions & 3 deletions pkg/format/h265.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (f *H265) unmarshal(ctx *unmarshalContext) error {
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})

case "sprop-sps":
Expand All @@ -47,7 +47,7 @@ func (f *H265) unmarshal(ctx *unmarshalContext) error {
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})

var spsp h265.SPS
Expand All @@ -63,7 +63,7 @@ func (f *H265) unmarshal(ctx *unmarshalContext) error {
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})

var ppsp h265.PPS
Expand Down

0 comments on commit b8f8275

Please sign in to comment.