forked from bluenviron/gohlslib
/
marshal.go
188 lines (162 loc) · 4.12 KB
/
marshal.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Package codecparams contains utilities to deal with codec parameters.
package codecparams
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/bluenviron/mediacommon/pkg/codecs/av1"
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/IgorDeforges/gohlslib/pkg/codecs"
)
func leadingZeros(v int, size int) string {
out := strconv.FormatInt(int64(v), 10)
if len(out) >= size {
return out
}
out2 := ""
for i := 0; i < (size - len(out)); i++ {
out2 += "0"
}
return out2 + out
}
func av1EncodeTier(v bool) string {
if v {
return "H"
}
return "M"
}
func av1EncodeBool(v bool) string {
if v {
return "1"
}
return "0"
}
func h265EncodeProfileSpace(v uint8) string {
if v >= 1 && v <= 3 {
return string('A' + (v - 1))
}
return ""
}
func h265EncodeCompatibilityFlag(v [32]bool) string {
var o uint32
for i, b := range v {
if b {
o |= 1 << i
}
}
return fmt.Sprintf("%x", o)
}
func h265EncodeGeneralTierFlag(v uint8) string {
if v > 0 {
return "H"
}
return "L"
}
func h265EncodeGeneralConstraintIndicatorFlags(v *h265.SPS_ProfileTierLevel) string {
var ret []string
var o1 uint8
if v.GeneralProgressiveSourceFlag {
o1 |= 1 << 7
}
if v.GeneralInterlacedSourceFlag {
o1 |= 1 << 6
}
if v.GeneralNonPackedConstraintFlag {
o1 |= 1 << 5
}
if v.GeneralFrameOnlyConstraintFlag {
o1 |= 1 << 4
}
if v.GeneralMax12bitConstraintFlag {
o1 |= 1 << 3
}
if v.GeneralMax10bitConstraintFlag {
o1 |= 1 << 2
}
if v.GeneralMax8bitConstraintFlag {
o1 |= 1 << 1
}
if v.GeneralMax422ChromeConstraintFlag {
o1 |= 1 << 0
}
ret = append(ret, fmt.Sprintf("%x", o1))
var o2 uint8
if v.GeneralMax420ChromaConstraintFlag {
o2 |= 1 << 7
}
if v.GeneralMaxMonochromeConstraintFlag {
o2 |= 1 << 6
}
if v.GeneralIntraConstraintFlag {
o2 |= 1 << 5
}
if v.GeneralOnePictureOnlyConstraintFlag {
o2 |= 1 << 4
}
if v.GeneralLowerBitRateConstraintFlag {
o2 |= 1 << 3
}
if v.GeneralMax14BitConstraintFlag {
o2 |= 1 << 2
}
if o2 != 0 {
ret = append(ret, fmt.Sprintf("%x", o2))
}
return strings.Join(ret, ".")
}
// Marshal generates codec parameters of given tracks.
func Marshal(codec codecs.Codec) string {
switch codec := codec.(type) {
case *codecs.AV1:
var sh av1.SequenceHeader
err := sh.Unmarshal(codec.SequenceHeader)
if err == nil {
v := "av01." +
strconv.FormatInt(int64(sh.SeqProfile), 10) + "." +
leadingZeros(int(sh.SeqLevelIdx[0]), 2) +
av1EncodeTier(sh.SeqTier[0]) + "." +
leadingZeros(sh.ColorConfig.BitDepth, 2) + "." +
av1EncodeBool(sh.ColorConfig.MonoChrome) + "." +
av1EncodeBool(sh.ColorConfig.SubsamplingX) +
av1EncodeBool(sh.ColorConfig.SubsamplingY) +
strconv.FormatInt(int64(sh.ColorConfig.ChromaSamplePosition), 10) + "."
if sh.ColorConfig.ColorDescriptionPresentFlag {
v += leadingZeros(int(sh.ColorConfig.ColorPrimaries), 2) + "." +
leadingZeros(int(sh.ColorConfig.TransferCharacteristics), 2) + "." +
leadingZeros(int(sh.ColorConfig.MatrixCoefficients), 2) + "." +
av1EncodeBool(sh.ColorConfig.ColorRange)
} else {
v += "01.01.01.0"
}
return v
}
case *codecs.VP9:
return "vp09." +
leadingZeros(int(codec.Profile), 2) + "." +
"10." + // level
leadingZeros(int(codec.BitDepth), 2)
case *codecs.H265:
var sps h265.SPS
err := sps.Unmarshal(codec.SPS)
if err == nil {
return "hvc1." +
h265EncodeProfileSpace(sps.ProfileTierLevel.GeneralProfileSpace) +
strconv.FormatInt(int64(sps.ProfileTierLevel.GeneralProfileIdc), 10) + "." +
h265EncodeCompatibilityFlag(sps.ProfileTierLevel.GeneralProfileCompatibilityFlag) + "." +
h265EncodeGeneralTierFlag(sps.ProfileTierLevel.GeneralTierFlag) +
strconv.FormatInt(int64(sps.ProfileTierLevel.GeneralLevelIdc), 10) + "." +
h265EncodeGeneralConstraintIndicatorFlags(&sps.ProfileTierLevel)
}
case *codecs.H264:
if len(codec.SPS) >= 4 {
return "avc1." + hex.EncodeToString(codec.SPS[1:4])
}
case *codecs.Opus:
return "opus"
case *codecs.MPEG4Audio:
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/codecs_parameter
return "mp4a.40." + strconv.FormatInt(int64(codec.Config.Type), 10)
}
return ""
}