Skip to content

Commit

Permalink
h265: improve DTS extractor performance (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Nov 17, 2023
1 parent 151a3cb commit dd5555b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
19 changes: 12 additions & 7 deletions pkg/codecs/h265/dts_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
)

const (
maxBytesToGetPOC = 12
)

func getPTSDTSDiff(buf []byte, sps *SPS, pps *PPS) (uint32, error) {
if len(buf) < 12 {
return 0, fmt.Errorf("not enough bits")
}
typ := NALUType((buf[0] >> 1) & 0b111111)

buf = h264.EmulationPreventionRemove(buf[:12])
buf = buf[1:]
lb := len(buf)

typ := NALUType((buf[0] >> 1) & 0b111111)
if lb > maxBytesToGetPOC {
lb = maxBytesToGetPOC
}

buf = buf[2:]
pos := 0
buf = h264.EmulationPreventionRemove(buf[:lb])
pos := 8

firstSliceSegmentInPicFlag, err := bits.ReadFlag(buf, &pos)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/codecs/h265/pps.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (p *PPS) Unmarshal(buf []byte) error {
return fmt.Errorf("not a PPS")
}

buf = h264.EmulationPreventionRemove(buf[2:])
pos := 0
buf = h264.EmulationPreventionRemove(buf[1:])
pos := 8

var err error
p.ID, err = bits.ReadGolombUnsigned(buf, &pos)
Expand Down
4 changes: 2 additions & 2 deletions pkg/codecs/h265/sps.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ func (s *SPS) Unmarshal(buf []byte) error {
return fmt.Errorf("not a SPS")
}

buf = h264.EmulationPreventionRemove(buf[2:])
pos := 0
buf = h264.EmulationPreventionRemove(buf[1:])
pos := 8

err := bits.HasSpace(buf, pos, 8)
if err != nil {
Expand Down

0 comments on commit dd5555b

Please sign in to comment.