-
Notifications
You must be signed in to change notification settings - Fork 202
/
av1.go
172 lines (160 loc) · 4.63 KB
/
av1.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
package track
import (
"time"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpav1"
"github.com/bluenviron/mediacommon/pkg/codecs/av1"
"go.uber.org/zap"
"m7s.live/engine/v4/codec"
. "m7s.live/engine/v4/common"
"m7s.live/engine/v4/log"
"m7s.live/engine/v4/util"
)
var _ SpesificTrack = (*AV1)(nil)
type AV1 struct {
Video
decoder rtpav1.Decoder
encoder rtpav1.Encoder
seqHeader av1.SequenceHeader
seenFrameHeader bool
refFrameType map[byte]byte
}
func NewAV1(puber IPuber, stuff ...any) (vt *AV1) {
vt = &AV1{}
vt.Video.CodecID = codec.CodecID_AV1
vt.SetStuff("av1", byte(96), uint32(90000), vt, stuff, puber)
if vt.BytesPool == nil {
vt.BytesPool = make(util.BytesPool, 17)
}
vt.nalulenSize = 0
vt.dtsEst = util.NewDTSEstimator()
vt.decoder.Init()
vt.encoder.Init()
vt.encoder.PayloadType = vt.PayloadType
vt.ParamaterSets = [][]byte{nil, {0, 0, 0}}
return
}
func (vt *AV1) WriteSequenceHead(head []byte) (err error) {
vt.Video.WriteSequenceHead(head)
var info codec.AV1CodecConfigurationRecord
info.Unmarshal(head[5:])
vt.seqHeader.Unmarshal(info.ConfigOBUs)
vt.ParamaterSets = [][]byte{info.ConfigOBUs, {info.SeqLevelIdx0, info.SeqProfile, info.SeqTier0}}
return
}
func (vt *AV1) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
defer func() {
err := recover()
if err != nil {
vt.Error("WriteRTPFrame panic", zap.Any("err", err))
vt.Publisher.Stop(zap.Any("err", err))
}
}()
if vt.lastSeq != vt.lastSeq2+1 && vt.lastSeq2 != 0 {
vt.lostFlag = true
vt.Warn("lost rtp packet", zap.Uint16("lastSeq", vt.lastSeq), zap.Uint16("lastSeq2", vt.lastSeq2))
}
frame := &rtpItem.Value
rv := vt.Value
rv.RTP.Push(rtpItem)
obus, err := vt.decoder.Decode(frame.Packet)
for _, obu := range obus {
var obuHeader av1.OBUHeader
obuHeader.Unmarshal(obu)
switch obuHeader.Type {
case av1.OBUTypeSequenceHeader:
rtmpHead := []byte{0b1001_0000 | byte(codec.PacketTypeMPEG2TSSequenceStart), 0, 0, 0, 0}
util.BigEndian.PutUint32(rtmpHead[1:], codec.FourCC_AV1_32)
// TODO: 生成 head
rtmpHead = append(rtmpHead, obu...)
vt.Video.WriteSequenceHead(rtmpHead)
vt.ParamaterSets[0] = obu
default:
rv.AUList.Push(vt.BytesPool.GetShell(obu))
}
}
if err == nil {
vt.generateTimestamp(frame.Timestamp)
vt.Flush()
}
}
func (vt *AV1) writeAVCCFrame(ts uint32, r *util.BLLReader, frame *util.BLL) (err error) {
vt.Value.PTS = time.Duration(ts) * 90
vt.Value.DTS = time.Duration(ts) * 90
var obuHeader av1.OBUHeader
for r.CanRead() {
offset := r.GetOffset()
b, _ := r.ReadByte()
obuHeader.Unmarshal([]byte{b})
if log.Trace {
vt.Trace("obu", zap.Any("type", obuHeader.Type), zap.Bool("iframe", vt.Value.IFrame))
}
obuSize, _, _ := r.LEB128Unmarshal()
end := r.GetOffset()
size := end - offset + int(obuSize)
r = frame.NewReader()
r.Skip(offset)
obu := r.ReadN(size)
switch obuHeader.Type {
case codec.AV1_OBU_SEQUENCE_HEADER:
// vt.seqHeader.Unmarshal(util.ConcatBuffers(obu))
// vt.seenFrameHeader = false
// vt.AppendAuBytes(obu...)
case codec.AV1_OBU_FRAME:
// if !vt.seenFrameHeader {
// if vt.seqHeader.ReducedStillPictureHeader {
// vt.Value.IFrame = true
// vt.seenFrameHeader = true
// } else {
// showframe := obu[0][0] >> 7
// if showframe != 0 {
// frame_to_show_map_idx := (obu[0][0] >> 4) & 0b0111
// vt.Value.IFrame = vt.refFrameType[frame_to_show_map_idx] == 0
// } else {
// vt.Value.IFrame = (obu[0][0])&0b0110_0000 == 0
// }
// vt.seenFrameHeader = showframe == 0
// }
// }
// vt.AppendAuBytes(obu...)
case codec.AV1_OBU_TEMPORAL_DELIMITER:
case codec.AV1_OBU_FRAME_HEADER:
}
vt.AppendAuBytes(obu...)
}
return
}
func (vt *AV1) CompleteAVCC(rv *AVFrame) {
mem := vt.BytesPool.Get(5)
b := mem.Value
if rv.IFrame {
b[0] = 0b1001_0000 | byte(codec.PacketTypeCodedFrames)
} else {
b[0] = 0b1010_0000 | byte(codec.PacketTypeCodedFrames)
}
util.BigEndian.PutUint32(b[1:], codec.FourCC_AV1_32)
// println(rv.PTS < rv.DTS, "\t", rv.PTS, "\t", rv.DTS, "\t", rv.PTS-rv.DTS)
// 写入CTS
rv.AVCC.Push(mem)
rv.AUList.Range(func(au *util.BLL) bool {
au.Range(func(slice util.Buffer) bool {
rv.AVCC.Push(vt.BytesPool.GetShell(slice))
return true
})
return true
})
}
// RTP格式补完
func (vt *AV1) CompleteRTP(value *AVFrame) {
obus := vt.Value.AUList.ToBuffers()
// if vt.Value.IFrame {
// obus = append(net.Buffers{vt.ParamaterSets[0]}, obus...)
// }
rtps, err := vt.encoder.Encode(obus)
if err != nil {
vt.Error("AV1 encoder encode error", zap.Error(err))
return
}
for _, rtp := range rtps {
vt.Value.RTP.PushValue(RTPFrame{Packet: rtp})
}
}