-
Notifications
You must be signed in to change notification settings - Fork 84
/
sei.go
74 lines (68 loc) · 2.15 KB
/
sei.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package hevc
import (
"bytes"
"errors"
"fmt"
"github.com/Eyevinn/mp4ff/sei"
)
var (
ErrNotSEINalu = errors.New("not an SEI NAL unit")
)
// ParseSEINalu - parse SEI NAL unit (incl header) and return messages given SPS.
// Returns sei.ErrRbspTrailingBitsMissing if the NALU is missing the trailing bits.
func ParseSEINalu(nalu []byte, sps *SPS) ([]sei.SEIMessage, error) {
switch GetNaluType(nalu[0]) {
case NALU_SEI_PREFIX, NALU_SEI_SUFFIX:
default:
return nil, ErrNotSEINalu
}
seiBytes := nalu[2:] // Skip NALU header
buf := bytes.NewReader(seiBytes)
seiDatas, err := sei.ExtractSEIData(buf)
missingRbspTrailingBits := false
if err != nil {
if errors.Is(err, sei.ErrRbspTrailingBitsMissing) {
missingRbspTrailingBits = true
} else {
return nil, fmt.Errorf("extracting SEI data: %w", err)
}
}
seiMsgs := make([]sei.SEIMessage, 0, len(seiDatas))
var seiMsg sei.SEIMessage
for _, seiData := range seiDatas {
switch {
case seiData.Type() == sei.SEIPicTimingType && sps != nil && sps.VUI != nil:
htp := fillHEVCPicTimingParams(sps)
seiMsg, err = sei.DecodePicTimingHevcSEI(&seiData, htp)
default:
seiMsg, err = sei.DecodeSEIMessage(&seiData, sei.HEVC)
}
if err != nil {
return nil, fmt.Errorf("sei decode: %w", err)
}
seiMsgs = append(seiMsgs, seiMsg)
}
if missingRbspTrailingBits {
return seiMsgs, sei.ErrRbspTrailingBitsMissing
}
return seiMsgs, nil
}
func fillHEVCPicTimingParams(sps *SPS) sei.HEVCPicTimingParams {
hpt := sei.HEVCPicTimingParams{}
if sps.VUI == nil {
return hpt
}
hpt.FrameFieldInfoPresentFlag = sps.VUI.FrameFieldInfoPresentFlag
hrd := sps.VUI.HrdParameters
if hrd == nil {
return hpt
}
hpt.CpbDpbDelaysPresentFlag = hrd.CpbDpbDelaysPresentFlag()
hpt.SubPicHrdParamsPresentFlag = hrd.SubPicHrdParamsPresentFlag
hpt.SubPicCpbParamsInPicTimingSeiFlag = hrd.SubPicCpbParamsInPicTimingSeiFlag
hpt.AuCbpRemovalDelayLengthMinus1 = hrd.AuCpbRemovalDelayLengthMinus1
hpt.DpbOutputDelayLengthMinus1 = hrd.DpbOutputDelayLengthMinus1
hpt.DpbOutputDelayDuLengthMinus1 = hrd.DpbOutputDelayDuLengthMinus1
hpt.DuCpbRemovalDelayIncrementLengthMinus1 = hrd.DuCpbRemovalDelayIncrementLengthMinus1
return hpt
}