Skip to content

Commit

Permalink
h264: improve performance of SPS parser (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Oct 20, 2024
1 parent 8883ba8 commit 058cf2b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pkg/codecs/h264/emulation_prevention.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func EmulationPreventionRemove(nalu []byte) []byte {
for i := 2; i < l; i++ {
if nalu[i-2] == 0 && nalu[i-1] == 0 && nalu[i] == 3 {
n--
i += 2
}
}

Expand All @@ -25,6 +26,7 @@ func EmulationPreventionRemove(nalu []byte) []byte {
if nalu[i-2] == 0 && nalu[i-1] == 0 && nalu[i] == 3 {
pos += copy(ret[pos:], nalu[start:i])
start = i + 1
i += 2
}
}

Expand Down
15 changes: 7 additions & 8 deletions pkg/codecs/h264/sps.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,25 @@ func (h *SPS_HRD) unmarshal(buf []byte, pos *int) error {
h.BitRateScale = uint8(bits.ReadBitsUnsafe(buf, pos, 4))
h.CpbSizeScale = uint8(bits.ReadBitsUnsafe(buf, pos, 4))

h.BitRateValueMinus1 = make([]uint32, h.CpbCntMinus1+1)
h.CpbSizeValueMinus1 = make([]uint32, h.CpbCntMinus1+1)
h.CbrFlag = make([]bool, h.CpbCntMinus1+1)

for i := uint32(0); i <= h.CpbCntMinus1; i++ {
var v uint32
v, err = bits.ReadGolombUnsigned(buf, pos)
h.BitRateValueMinus1[i], err = bits.ReadGolombUnsigned(buf, pos)
if err != nil {
return err
}
h.BitRateValueMinus1 = append(h.BitRateValueMinus1, v)

v, err = bits.ReadGolombUnsigned(buf, pos)
h.CpbSizeValueMinus1[i], err = bits.ReadGolombUnsigned(buf, pos)
if err != nil {
return err
}
h.CpbSizeValueMinus1 = append(h.CpbSizeValueMinus1, v)

var vb bool
vb, err = bits.ReadFlag(buf, pos)
h.CbrFlag[i], err = bits.ReadFlag(buf, pos)
if err != nil {
return err
}
h.CbrFlag = append(h.CbrFlag, vb)
}

err = bits.HasSpace(buf, *pos, 5+5+5+5)
Expand Down

0 comments on commit 058cf2b

Please sign in to comment.