Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading