Skip to content

Commit

Permalink
h264: support AVCC with empty NALUs
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Sep 24, 2023
1 parent 7fae03f commit f2ff49e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
33 changes: 16 additions & 17 deletions pkg/codecs/h264/avcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

// AVCCUnmarshal decodes an access unit from the AVCC stream format.
// This can also return nil in case the AVCC unit doesn't contain any NALU.
// Specification: ?
func AVCCUnmarshal(buf []byte) ([][]byte, error) {
bl := len(buf)
Expand All @@ -21,28 +22,26 @@ func AVCCUnmarshal(buf []byte) ([][]byte, error) {
l := int(uint32(buf[pos])<<24 | uint32(buf[pos+1])<<16 | uint32(buf[pos+2])<<8 | uint32(buf[pos+3]))
pos += 4

if l == 0 {
return nil, fmt.Errorf("invalid NALU")
}
if l != 0 {
if (auSize + l) > MaxAccessUnitSize {
return nil, fmt.Errorf("access unit size (%d) is too big, maximum is %d", auSize+l, MaxAccessUnitSize)
}

if (auSize + l) > MaxAccessUnitSize {
return nil, fmt.Errorf("access unit size (%d) is too big, maximum is %d", auSize+l, MaxAccessUnitSize)
}
if (naluCount + 1) > MaxNALUsPerAccessUnit {
return nil, fmt.Errorf("NALU count (%d) exceeds maximum allowed (%d)",
len(ret)+1, MaxNALUsPerAccessUnit)
}

if (naluCount + 1) > MaxNALUsPerAccessUnit {
return nil, fmt.Errorf("NALU count (%d) exceeds maximum allowed (%d)",
len(ret)+1, MaxNALUsPerAccessUnit)
}
if (bl - pos) < l {
return nil, fmt.Errorf("invalid length")
}

if (bl - pos) < l {
return nil, fmt.Errorf("invalid length")
ret = append(ret, buf[pos:pos+l])
auSize += l
naluCount++
pos += l
}

ret = append(ret, buf[pos:pos+l])
auSize += l
naluCount++
pos += l

if (bl - pos) == 0 {
break
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/codecs/h264/avcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ func TestAVCCUnmarshal(t *testing.T) {
}
}

// issue mediamtx/2375
func TestAVCCUnmarshalEmpty(t *testing.T) {
caenc := []byte{
0x0, 0x0, 0x0, 0x0,
}
cadec := [][]byte(nil)

dec, err := AVCCUnmarshal(caenc)
require.NoError(t, err)
require.Equal(t, cadec, dec)

caenc = []byte{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x1, 0x2, 0x3,
}
cadec = [][]byte{
{1, 2, 3},
}

dec, err = AVCCUnmarshal(caenc)
require.NoError(t, err)
require.Equal(t, cadec, dec)
}

func TestAVCCMarshal(t *testing.T) {
for _, ca := range casesAVCC {
t.Run(ca.name, func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/formats/fmp4/part_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (ps PartSample) GetH26x() ([][]byte, error) {
return nil, err
}

if au == nil {
return nil, fmt.Errorf("no valid NALUs found")
}

return au, nil
}

Expand Down

0 comments on commit f2ff49e

Please sign in to comment.