-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathvvcdecoderconfigurationrecord.go
More file actions
404 lines (343 loc) · 12.9 KB
/
Copy pathvvcdecoderconfigurationrecord.go
File metadata and controls
404 lines (343 loc) · 12.9 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package vvc
import (
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
/*
PTL represents profile-tier-level information (VvcPTLRecord) Section 11.2.4.1.2
aligned(8) class VvcPTLRecord(num_sublayers) {
bit(2) reserved = 0;
unsigned int(6) num_bytes_constraint_info;
unsigned int(7) general_profile_idc;
unsigned int(1) general_tier_flag;
unsigned int(8) general_level_idc;
unsigned int(1) ptl_frame_only_constraint_flag;
unsigned int(1) ptl_multi_layer_enabled_flag;
unsigned int(8*num_bytes_constraint_info - 2) general_constraint_info;
for (i=num_sublayers - 2; i >= 0; i--)
unsigned int(1) ptl_sublayer_level_present_flag[i];
for (j=num_sublayers; j<=8 && num_sublayers > 1; j++)
bit(1) ptl_reserved_zero_bit = 0;
for (i=num_sublayers-2; i >= 0; i--)
if (ptl_sublayer_level_present_flag[i])
unsigned int(8) sublayer_level_idc[i];
unsigned int(8) ptl_num_sub_profiles;
for (j=0; j < ptl_num_sub_profiles; j++)
unsigned int(32) general_sub_profile_idc[j];
}
*/
type PTL struct {
NumBytesConstraintInfo uint8
GeneralProfileIDC uint8
GeneralTierFlag bool
GeneralLevelIDC uint8
PtlFrameOnlyConstraintFlag bool
PtlMultiLayerEnabledFlag bool
GeneralConstraintInfo []byte
PtlSublayerLevelPresentFlag []bool
SublayerLevelIDC []uint8
PtlNumSubProfiles uint8
GeneralSubProfileIDC []uint32
}
/*
DecConfRec represents VVC decoder configuration record, 11.2.4.2.2
aligned(8) class VvcDecoderConfigurationRecord {
bit(5) reserved = '11111'b;
unsigned int(2) LengthSizeMinusOne;
unsigned int(1) ptl_present_flag;
if (ptl_present_flag) {
unsigned int(9) ols_idx;
unsigned int(3) num_sublayers;
unsigned int(2) constant_frame_rate;
unsigned int(2) chroma_format_idc;
unsigned int(3) bit_depth_minus8;
bit(5) reserved = '11111'b;
VvcPTLRecord(num_sublayers) native_ptl;
unsigned_int(16) max_picture_width;
unsigned_int(16) max_picture_height;
unsigned int(16) avg_frame_rate;
}
unsigned int(8) num_of_arrays;
for (j=0; j < num_of_arrays; j++) {
unsigned int(1) array_completeness;
bit(2) reserved = 0;
unsigned int(5) NAL_unit_type;
if (NAL_unit_type != DCI_NUT && NAL_unit_type != OPI_NUT)
unsigned int(16) num_nalus;
for (i=0; i< num_nalus; i++) {
unsigned int(16) nal_unit_length;
bit(8*nal_unit_length) nal_unit;
}
}
}
*/
type DecConfRec struct {
LengthSizeMinusOne uint8
PtlPresentFlag bool
OlsIdx uint16
NumSublayers uint8
ConstantFrameRate uint8
ChromaFormatIDC uint8
BitDepthMinus8 uint8
NativePTL PTL
MaxPictureWidth uint16
MaxPictureHeight uint16
AvgFrameRate uint16
NaluArrays []NaluArray
}
// Size returns the size of the decoder configuration record
func (d *DecConfRec) Size() int {
size := 1 // reserved + lengthSizeMinusOne + ptlPresentFlag
if d.PtlPresentFlag {
size += 2 // olsIdx + numSublayers + constantFrameRate + chromaFormatIDC
size += 1 // bitDepthMinus8 + reserved
// PTL fields (VvcPTLRecord)
size += 1 // num_bytes_constraint_info
size += 1 // general_profile_idc + general_tier_flag
size += 1 // general_level_idc
size += int(d.NativePTL.NumBytesConstraintInfo) // constraint info (includes frame_only/multi_layer flags)
// Sublayer flags and levels
numSublayers := int(d.NumSublayers)
if numSublayers > 1 {
// The spec requires exactly 8 bits total for flags + reserved bits = 1 byte
size += 1
// Sublayer level IDCs (only for present ones)
for _, present := range d.NativePTL.PtlSublayerLevelPresentFlag {
if present {
size += 1
}
}
}
size += 1 // ptl_num_sub_profiles
size += int(d.NativePTL.PtlNumSubProfiles) * 4 // general_sub_profile_idc array
size += 6 // maxPictureWidth + maxPictureHeight + avgFrameRate
}
size += 1 // numOfArrays
for _, array := range d.NaluArrays {
size += 1 // arrayCompleteness + reserved + NALUnitType
if array.NaluType != NALU_DCI && array.NaluType != NALU_OPI {
size += 2 // numNalus
}
for _, nalu := range array.Nalus {
size += 2 + len(nalu) // naluLength + nalu
}
}
return size
}
// Encode writes the decoder configuration record to w
func (d *DecConfRec) Encode(w io.Writer) error {
sw := bits.NewFixedSliceWriter(d.Size())
err := d.EncodeSW(sw)
if err != nil {
return err
}
_, err = w.Write(sw.Bytes())
return err
}
// EncodeSW writes the decoder configuration record to sw
func (d *DecConfRec) EncodeSW(sw bits.SliceWriter) error {
// First byte: reserved (5 bits) + lengthSizeMinusOne (2 bits) + ptlPresentFlag (1 bit)
firstByte := byte(0xF8) | (d.LengthSizeMinusOne << 1)
if d.PtlPresentFlag {
firstByte |= 0x01
}
sw.WriteUint8(firstByte)
if d.PtlPresentFlag {
// olsIdx (9 bits) + numSublayers (3 bits) + constantFrameRate (2 bits) + chromaFormatIDC (2 bits)
sw.WriteUint16((d.OlsIdx << 7) | (uint16(d.NumSublayers) << 4) | (uint16(d.ConstantFrameRate) << 2) | uint16(d.ChromaFormatIDC))
// bitDepthMinus8 (3 bits) + reserved (5 bits)
sw.WriteUint8((d.BitDepthMinus8 << 5) | 0x1F)
// PTL fields - VvcPTLRecord structure
// First byte: reserved (2 bits) + num_bytes_constraint_info (6 bits)
sw.WriteUint8((d.NativePTL.NumBytesConstraintInfo & 0x3F))
// Second byte: general_profile_idc (7 bits) + general_tier_flag (1 bit)
sw.WriteUint8((d.NativePTL.GeneralProfileIDC << 1) | boolToUint8(d.NativePTL.GeneralTierFlag))
// general_level_idc (8 bits)
sw.WriteUint8(d.NativePTL.GeneralLevelIDC)
// ptl_frame_only_constraint_flag (1 bit) + ptl_multi_layer_enabled_flag (1 bit) + constraint info
flagsByte := boolToUint8(d.NativePTL.PtlFrameOnlyConstraintFlag)<<7 | boolToUint8(d.NativePTL.PtlMultiLayerEnabledFlag)<<6
if len(d.NativePTL.GeneralConstraintInfo) > 0 {
flagsByte |= d.NativePTL.GeneralConstraintInfo[0] & 0x3F
sw.WriteUint8(flagsByte)
if len(d.NativePTL.GeneralConstraintInfo) > 1 {
sw.WriteBytes(d.NativePTL.GeneralConstraintInfo[1:])
}
} else {
sw.WriteUint8(flagsByte)
}
// Handle sublayer level present flags
numSublayers := int(d.NumSublayers)
if numSublayers > 1 {
// The spec requires writing exactly 8 bits total:
// - (numSublayers - 1) bits for ptl_sublayer_level_present_flag
// - (9 - numSublayers) bits for ptl_reserved_zero_bit
// This always totals to 8 bits when numSublayers > 1
flagByte := uint8(0)
// Write sublayer level present flags (from MSB to LSB)
for i := numSublayers - 2; i >= 0; i-- {
bitPos := (numSublayers - 2) - i
if i < len(d.NativePTL.PtlSublayerLevelPresentFlag) && d.NativePTL.PtlSublayerLevelPresentFlag[i] {
flagByte |= (0x80 >> bitPos)
}
}
// The remaining bits are reserved zero bits (already 0)
sw.WriteUint8(flagByte)
// Write sublayer level IDCs
for i := numSublayers - 2; i >= 0; i-- {
if i < len(d.NativePTL.PtlSublayerLevelPresentFlag) && d.NativePTL.PtlSublayerLevelPresentFlag[i] {
if i < len(d.NativePTL.SublayerLevelIDC) {
sw.WriteUint8(d.NativePTL.SublayerLevelIDC[i])
} else {
sw.WriteUint8(0) // Default value
}
}
}
}
// ptl_num_sub_profiles
sw.WriteUint8(d.NativePTL.PtlNumSubProfiles)
// general_sub_profile_idc
for _, subProfile := range d.NativePTL.GeneralSubProfileIDC {
sw.WriteUint32(subProfile)
}
// maxPictureWidth, maxPictureHeight, avgFrameRate
sw.WriteUint16(d.MaxPictureWidth)
sw.WriteUint16(d.MaxPictureHeight)
sw.WriteUint16(d.AvgFrameRate)
}
// numOfArrays
sw.WriteUint8(uint8(len(d.NaluArrays)))
// NAL unit arrays
for _, array := range d.NaluArrays {
// arrayCompleteness (1 bit) + reserved (2 bits) + NALUnitType (5 bits)
arrayByte := uint8(array.NaluType) & 0x1F
if array.Complete {
arrayByte |= 0x80
}
sw.WriteUint8(arrayByte)
// NALU_DCI and NALU_OPI do not have numNalus field but default to 1 NALU
if array.NaluType != NALU_DCI && array.NaluType != NALU_OPI {
sw.WriteUint16(uint16(len(array.Nalus)))
}
for _, nalu := range array.Nalus {
sw.WriteUint16(uint16(len(nalu)))
sw.WriteBytes(nalu)
}
}
return sw.AccError()
}
// DecodeVVCDecConfRec decodes a VVC decoder configuration record
func DecodeVVCDecConfRec(data []byte) (DecConfRec, error) {
sr := bits.NewFixedSliceReader(data)
d := DecConfRec{}
// First byte: reserved (5 bits) + lengthSizeMinusOne (2 bits) + ptlPresentFlag (1 bit)
firstByte := sr.ReadUint8()
d.LengthSizeMinusOne = (firstByte >> 1) & 0x03
d.PtlPresentFlag = (firstByte & 0x01) != 0
if d.PtlPresentFlag {
// olsIdx (9 bits) + numSublayers (3 bits) + constantFrameRate (2 bits) + chromaFormatIDC (2 bits)
combined := sr.ReadUint16()
d.OlsIdx = combined >> 7
d.NumSublayers = uint8((combined >> 4) & 0x07)
d.ConstantFrameRate = uint8((combined >> 2) & 0x03)
d.ChromaFormatIDC = uint8(combined & 0x03)
// bitDepthMinus8 (3 bits) + reserved (5 bits)
bitDepthByte := sr.ReadUint8()
d.BitDepthMinus8 = bitDepthByte >> 5
// PTL fields - VvcPTLRecord structure
// First byte: reserved (2 bits) + num_bytes_constraint_info (6 bits)
ptlFirstByte := sr.ReadUint8()
d.NativePTL.NumBytesConstraintInfo = ptlFirstByte & 0x3F
// Validate num_bytes_constraint_info - must be > 0 according to VVC spec
if d.NativePTL.NumBytesConstraintInfo == 0 {
return d, fmt.Errorf("invalid VVC PTL: num_bytes_constraint_info must be > 0")
}
// Second byte: general_profile_idc (7 bits) + general_tier_flag (1 bit)
ptlSecondByte := sr.ReadUint8()
d.NativePTL.GeneralProfileIDC = ptlSecondByte >> 1
d.NativePTL.GeneralTierFlag = (ptlSecondByte & 0x01) != 0
// general_level_idc (8 bits)
d.NativePTL.GeneralLevelIDC = sr.ReadUint8()
// ptl_frame_only_constraint_flag + ptl_multi_layer_enabled_flag + general_constraint_info
// These flags are always present, followed by (8*num_bytes_constraint_info - 2) bits of constraint info
constraintInfoBytes := int(d.NativePTL.NumBytesConstraintInfo)
firstConstraintByte := sr.ReadUint8()
d.NativePTL.PtlFrameOnlyConstraintFlag = (firstConstraintByte & 0x80) != 0
d.NativePTL.PtlMultiLayerEnabledFlag = (firstConstraintByte & 0x40) != 0
// Read remaining constraint info bytes
d.NativePTL.GeneralConstraintInfo = make([]byte, constraintInfoBytes)
d.NativePTL.GeneralConstraintInfo[0] = firstConstraintByte & 0x3F // Mask to get the last 6 bits
if constraintInfoBytes > 1 {
remainingConstraintBytes := sr.ReadBytes(constraintInfoBytes - 1)
copy(d.NativePTL.GeneralConstraintInfo[1:], remainingConstraintBytes)
}
// Handle sublayer level present flags
numSublayers := int(d.NumSublayers)
if numSublayers > 1 {
d.NativePTL.PtlSublayerLevelPresentFlag = make([]bool, numSublayers-1)
// The spec requires reading exactly 8 bits total:
// - (numSublayers - 1) bits for ptl_sublayer_level_present_flag
// - (9 - numSublayers) bits for ptl_reserved_zero_bit
// This always totals to 8 bits when numSublayers > 1
flagByte := sr.ReadUint8()
// Read sublayer level present flags (from MSB to LSB)
for i := numSublayers - 2; i >= 0; i-- {
bitPos := (numSublayers - 2) - i
d.NativePTL.PtlSublayerLevelPresentFlag[i] = (flagByte & (0x80 >> bitPos)) != 0
}
// The remaining bits in flagByte are reserved zero bits (already consumed)
// Read sublayer level IDCs
d.NativePTL.SublayerLevelIDC = make([]uint8, numSublayers-1)
for i := numSublayers - 2; i >= 0; i-- {
if len(d.NativePTL.PtlSublayerLevelPresentFlag) > i && d.NativePTL.PtlSublayerLevelPresentFlag[i] {
d.NativePTL.SublayerLevelIDC[i] = sr.ReadUint8()
}
}
}
// ptl_num_sub_profiles
d.NativePTL.PtlNumSubProfiles = sr.ReadUint8()
// general_sub_profile_idc
if d.NativePTL.PtlNumSubProfiles > 0 {
d.NativePTL.GeneralSubProfileIDC = make([]uint32, d.NativePTL.PtlNumSubProfiles)
for i := 0; i < int(d.NativePTL.PtlNumSubProfiles); i++ {
d.NativePTL.GeneralSubProfileIDC[i] = sr.ReadUint32()
}
}
// maxPictureWidth, maxPictureHeight, avgFrameRate
d.MaxPictureWidth = sr.ReadUint16()
d.MaxPictureHeight = sr.ReadUint16()
d.AvgFrameRate = sr.ReadUint16()
}
// numOfArrays
numOfArrays := sr.ReadUint8()
// NAL unit arrays
for i := 0; i < int(numOfArrays); i++ {
// arrayCompleteness (1 bit) + reserved (2 bits) + NALUnitType (5 bits)
arrayByte := sr.ReadUint8()
array := NaluArray{
Complete: (arrayByte & 0x80) != 0,
NaluType: NaluType(arrayByte & 0x1F),
}
numNalus := uint16(1) // Default to 1 NALU for DCI and OPI
if array.NaluType != NALU_DCI && array.NaluType != NALU_OPI {
numNalus = sr.ReadUint16()
}
array.Nalus = make([][]byte, numNalus)
for j := 0; j < int(numNalus); j++ {
naluLength := sr.ReadUint16()
array.Nalus[j] = sr.ReadBytes(int(naluLength))
}
d.NaluArrays = append(d.NaluArrays, array)
}
if err := sr.AccError(); err != nil {
return d, err
}
return d, nil
}
// Helper function to convert bool to uint8
func boolToUint8(b bool) uint8 {
if b {
return 1
}
return 0
}