-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathslice.go
362 lines (340 loc) · 10.3 KB
/
slice.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package avc
import (
"bytes"
"errors"
"fmt"
"math"
"github.com/Eyevinn/mp4ff/bits"
)
// Errors for parsing and handling AVC slices
var (
ErrNoSliceHeader = errors.New("No slice header")
ErrInvalidSliceType = errors.New("Invalid slice type")
ErrTooFewBytesToParse = errors.New("Too few bytes to parse symbol")
)
// SliceType - AVC slice type
type SliceType uint
func (s SliceType) String() string {
switch s {
case SLICE_I:
return "I"
case SLICE_P:
return "P"
case SLICE_B:
return "B"
case SLICE_SI:
return "SI"
case SLICE_SP:
return "SP"
default:
return ""
}
}
// AVC slice types
const (
SLICE_P = SliceType(0)
SLICE_B = SliceType(1)
SLICE_I = SliceType(2)
SLICE_SP = SliceType(3)
SLICE_SI = SliceType(4)
)
// GetSliceTypeFromNALU - parse slice header to get slice type in interval 0 to 4
func GetSliceTypeFromNALU(data []byte) (sliceType SliceType, err error) {
if len(data) <= 1 {
err = ErrTooFewBytesToParse
return
}
naluType := GetNaluType(data[0])
switch naluType {
case 1, 2, 5, 19:
// slice_layer_without_partitioning_rbsp
// slice_data_partition_a_layer_rbsp
default:
err = ErrNoSliceHeader
return
}
r := bits.NewEBSPReader(bytes.NewReader((data[1:])))
// first_mb_in_slice
_ = r.ReadExpGolomb()
sliceType = SliceType(r.ReadExpGolomb())
if r.AccError() != nil {
err = r.AccError()
}
if sliceType > 9 {
err = ErrInvalidSliceType
return
}
if sliceType >= 5 {
sliceType -= 5 // The same type is repeated twice to tell if all slices in picture are the same
}
return
}
type SliceHeader struct {
SliceType SliceType
FirstMBInSlice uint32
PicParamID uint32
SeqParamID uint32
ColorPlaneID uint32
FrameNum uint32
IDRPicID uint32
PicOrderCntLsb uint32
DeltaPicOrderCntBottom int32
DeltaPicOrderCnt [2]int32
RedundantPicCnt uint32
NumRefIdxL0ActiveMinus1 uint32
NumRefIdxL1ActiveMinus1 uint32
ModificationOfPicNumsIDC uint32
AbsDiffPicNumMinus1 uint32
LongTermPicNum uint32
AbsDiffViewIdxMinus1 uint32
LumaLog2WeightDenom uint32
ChromaLog2WeightDenom uint32
DifferenceOfPicNumsMinus1 uint32
LongTermFramIdx uint32
MaxLongTermFrameIdxPlus1 uint32
CabacInitIDC uint32
SliceQPDelta int32
SliceQSDelta int32
DisableDeblockingFilterIDC uint32
SliceAlphaC0OffsetDiv2 int32
SliceBetaOffsetDiv2 int32
SliceGroupChangeCycle uint32
Size uint32
FieldPicFlag bool
BottomFieldFlag bool
DirectSpatialMvPredFlag bool
NumRefIdxActiveOverrideFlag bool
RefPicListModificationL0Flag bool
RefPicListModificationL1Flag bool
NoOutputOfPriorPicsFlag bool
LongTermReferenceFlag bool
SPForSwitchFlag bool
AdaptiveRefPicMarkingModeFlag bool
}
// ParseSliceHeader parses AVC slice header following the syntax in ISO/IEC 14496-10 section 7.3.3
func ParseSliceHeader(nalu []byte, spsMap map[uint32]*SPS, ppsMap map[uint32]*PPS) (*SliceHeader, error) {
sh := SliceHeader{}
buf := bytes.NewBuffer(nalu)
r := bits.NewEBSPReader(buf)
nalHdr := r.Read(8)
naluType := GetNaluType(byte(nalHdr))
switch naluType {
case 1, 2, 5, 19:
// slice_layer_without_partitioning_rbsp
// slice_data_partition_a_layer_rbsp
default:
err := ErrNoSliceHeader
return nil, err
}
nalRefIDC := (nalHdr >> 5) & 0x3
sh.FirstMBInSlice = uint32(r.ReadExpGolomb())
sh.SliceType = SliceType(r.ReadExpGolomb())
sh.PicParamID = uint32(r.ReadExpGolomb())
pps, ok := ppsMap[sh.PicParamID]
if !ok {
return nil, fmt.Errorf("pps ID %d unknown", sh.PicParamID)
}
spsID := pps.PicParameterSetID
sps, ok := spsMap[uint32(spsID)]
if !ok {
return nil, fmt.Errorf("sps ID %d unknown", spsID)
}
if sps.SeparateColourPlaneFlag {
sh.ColorPlaneID = uint32(r.Read(2))
}
sh.FrameNum = uint32(r.Read(int(sps.Log2MaxFrameNumMinus4 + 4)))
if !sps.FrameMbsOnlyFlag {
sh.FieldPicFlag = r.ReadFlag()
if sh.FieldPicFlag {
sh.BottomFieldFlag = r.ReadFlag()
}
}
if naluType == NALU_IDR {
sh.IDRPicID = uint32(r.ReadExpGolomb())
}
if sps.PicOrderCntType == 0 {
sh.PicOrderCntLsb = uint32(r.Read(int(sps.Log2MaxPicOrderCntLsbMinus4 + 4)))
if pps.BottomFieldPicOrderInFramePresentFlag && !sh.FieldPicFlag {
sh.DeltaPicOrderCntBottom = int32(r.ReadSignedGolomb())
}
} else if sps.PicOrderCntType == 1 && !sps.DeltaPicOrderAlwaysZeroFlag {
sh.DeltaPicOrderCnt[0] = int32(r.ReadSignedGolomb())
if pps.BottomFieldPicOrderInFramePresentFlag && !sh.FieldPicFlag {
sh.DeltaPicOrderCnt[1] = int32(r.ReadSignedGolomb())
}
}
if pps.RedundantPicCntPresentFlag {
sh.RedundantPicCnt = uint32(r.ReadExpGolomb())
}
sliceType := SliceType(sh.SliceType % 5)
if sliceType == SLICE_B {
sh.DirectSpatialMvPredFlag = r.ReadFlag()
}
switch sliceType {
case SLICE_P, SLICE_SP, SLICE_B:
sh.NumRefIdxActiveOverrideFlag = r.ReadFlag()
if sh.NumRefIdxActiveOverrideFlag {
sh.NumRefIdxL0ActiveMinus1 = uint32(r.ReadExpGolomb())
if sliceType == SLICE_B {
sh.NumRefIdxL1ActiveMinus1 = uint32(r.ReadExpGolomb())
}
} else {
sh.NumRefIdxL0ActiveMinus1 = uint32(pps.NumRefIdxI0DefaultActiveMinus1)
sh.NumRefIdxL1ActiveMinus1 = uint32(pps.NumRefIdxI1DefaultActiveMinus1)
}
}
// ref_pic_list_modification (nal unit type != 20 or 21) Section G.3.3.3.1.1
if sliceType != SLICE_I && sliceType != SLICE_SI {
sh.RefPicListModificationL0Flag = r.ReadFlag()
if sh.RefPicListModificationL0Flag {
refPicListL0Loop:
for {
sh.ModificationOfPicNumsIDC = uint32(r.ReadExpGolomb())
switch sh.ModificationOfPicNumsIDC {
case 0, 1:
sh.AbsDiffPicNumMinus1 = uint32(r.ReadExpGolomb())
case 2:
sh.LongTermPicNum = uint32(r.ReadExpGolomb())
case 4, 5:
sh.AbsDiffViewIdxMinus1 = uint32(r.ReadExpGolomb())
case 3:
break refPicListL0Loop
}
if r.AccError() != nil {
break refPicListL0Loop
}
}
}
}
if sliceType == SLICE_B {
sh.RefPicListModificationL1Flag = r.ReadFlag()
if sh.RefPicListModificationL1Flag {
refPicListL1Loop:
for {
sh.ModificationOfPicNumsIDC = uint32(r.ReadExpGolomb())
switch sh.ModificationOfPicNumsIDC {
case 0, 1:
sh.AbsDiffPicNumMinus1 = uint32(r.ReadExpGolomb())
case 2:
sh.LongTermPicNum = uint32(r.ReadExpGolomb())
case 4, 5:
sh.AbsDiffViewIdxMinus1 = uint32(r.ReadExpGolomb())
case 3:
break refPicListL1Loop
}
if r.AccError() != nil {
break refPicListL1Loop
}
}
}
}
// end ref_pic_list_modification
if pps.WeightedPredFlag && (sliceType == SLICE_P || sliceType == SLICE_SP) ||
(pps.WeightedBipredIDC == 1 && sliceType == SLICE_B) {
// pred_weight_table, section 7.3.3.2
sh.LumaLog2WeightDenom = uint32(r.ReadExpGolomb())
if sps.ChromaArrayType() != 0 { // chroma_idc != 0 in Bento4
sh.ChromaLog2WeightDenom = uint32(r.ReadExpGolomb())
}
for i := uint32(0); i <= sh.NumRefIdxL0ActiveMinus1; i++ {
lumaWeightL0Flag := r.ReadFlag()
if lumaWeightL0Flag {
// Just parse, don't store this
_ = r.ReadExpGolomb() // luma_weight_l0[i] = SignedGolomb()
_ = r.ReadExpGolomb() // luma_offset_l0[i] = SignedGolomb()
}
if sps.ChromaArrayType() != 0 {
chromaWeightL0Flag := r.ReadFlag()
if chromaWeightL0Flag {
for j := 0; j < 2; j++ {
// Just parse, don't store this
_ = r.ReadExpGolomb() // chroma_weight_l0[i][j] = SignedGolomb()
_ = r.ReadExpGolomb() // chroma_offset_l0[i][j] = SignedGolomb()
}
}
}
}
if sliceType == SLICE_B {
for i := uint32(0); i <= sh.NumRefIdxL1ActiveMinus1; i++ {
lumaWeightL1Flag := r.ReadFlag()
if lumaWeightL1Flag {
// Just parse, don't store this
_ = r.ReadExpGolomb() // luma_weight_l1[i] = SignedGolomb()
_ = r.ReadExpGolomb() // luma_offset_l1[i] = SignedGolomb()
}
if sps.ChromaArrayType() != 0 {
chromaWeightL1Flag := r.ReadFlag()
if chromaWeightL1Flag {
// Just parse, don't store this
for j := 0; j < 2; j++ {
_ = r.ReadSignedGolomb() // chroma_weight_l1[i][j] = SignedGolomb()
_ = r.ReadSignedGolomb() // chroma_offset_l1[i][j] = SignedGolomb()
}
}
}
}
}
// end pred_weight_table
}
if nalRefIDC != 0 {
// dec_ref_pic_marking
if naluType == NALU_IDR {
sh.NoOutputOfPriorPicsFlag = r.ReadFlag()
sh.LongTermReferenceFlag = r.ReadFlag()
} else {
sh.AdaptiveRefPicMarkingModeFlag = r.ReadFlag()
if sh.AdaptiveRefPicMarkingModeFlag {
adaptiveRefPicLoop:
for {
memoryManagementControlOperation := r.ReadExpGolomb()
switch memoryManagementControlOperation {
case 1, 3:
sh.DifferenceOfPicNumsMinus1 = uint32(r.ReadExpGolomb())
case 2:
sh.LongTermPicNum = uint32(r.ReadExpGolomb())
}
switch memoryManagementControlOperation {
case 3, 6:
sh.LongTermFramIdx = uint32(r.ReadExpGolomb())
case 4:
sh.MaxLongTermFrameIdxPlus1 = uint32(r.ReadExpGolomb())
case 0:
break adaptiveRefPicLoop
}
if r.AccError() != nil {
break adaptiveRefPicLoop
}
}
}
}
// end dec_ref_pic_marking
}
if pps.EntropyCodingModeFlag && sliceType != SLICE_I && sliceType != SLICE_SI {
sh.CabacInitIDC = uint32(r.ReadExpGolomb())
}
sh.SliceQPDelta = int32(r.ReadSignedGolomb())
if sliceType == SLICE_SP || sliceType == SLICE_SI {
if sliceType == SLICE_SP {
sh.SPForSwitchFlag = r.ReadFlag()
}
sh.SliceQSDelta = int32(r.ReadSignedGolomb())
}
if pps.DeblockingFilterControlPresentFlag {
sh.DisableDeblockingFilterIDC = uint32(r.ReadExpGolomb())
if sh.DisableDeblockingFilterIDC != 1 {
sh.SliceAlphaC0OffsetDiv2 = int32(r.ReadSignedGolomb())
sh.SliceBetaOffsetDiv2 = int32(r.ReadSignedGolomb())
}
}
if pps.NumSliceGroupsMinus1 > 0 &&
pps.SliceGroupMapType >= 3 &&
pps.SliceGroupMapType <= 5 {
picSizeInMapUnits := pps.PicSizeInMapUnitsMinus1 + 1
sliceGroupChangeRate := pps.SliceGroupChangeRateMinus1 + 1
nrBits := int(math.Ceil(math.Log2(float64(picSizeInMapUnits/sliceGroupChangeRate + 1))))
sh.SliceGroupChangeCycle = uint32(r.Read(nrBits))
}
// compute the size in bytes. The last byte may not be fully parsed
sh.Size = uint32(r.NrBytesRead())
return &sh, nil
}