2020-06-08 03:29:51 +03:00
|
|
|
package mpeg
|
|
|
|
|
|
|
|
// TODO: unescape configurable? merge with AVC_NAL? merge with HEVC?
|
|
|
|
|
|
|
|
import (
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/format"
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/mathex"
|
2022-01-24 23:21:48 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-17 18:46:10 +03:00
|
|
|
var avcSPSFormat decode.Group
|
|
|
|
var avcPPSFormat decode.Group
|
|
|
|
var avcSEIFormat decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
interp.RegisterFormat(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.AVC_NALU,
|
|
|
|
Description: "H.264/AVC Network Access Layer Unit",
|
|
|
|
DecodeFn: avcNALUDecode,
|
|
|
|
Dependencies: []decode.Dependency{
|
2021-11-17 18:46:10 +03:00
|
|
|
{Names: []string{format.AVC_SPS}, Group: &avcSPSFormat},
|
|
|
|
{Names: []string{format.AVC_PPS}, Group: &avcPPSFormat},
|
|
|
|
{Names: []string{format.AVC_SEI}, Group: &avcSEIFormat},
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 14496-10 9.1 Parsing process for Exp-Golomb codes
|
|
|
|
func expGolomb(d *decode.D) uint64 {
|
|
|
|
leadingZeroBits := -1
|
|
|
|
for b := false; !b; leadingZeroBits++ {
|
|
|
|
b = d.Bool()
|
|
|
|
}
|
|
|
|
|
|
|
|
var expN uint64
|
|
|
|
if leadingZeroBits == 0 {
|
|
|
|
expN = 1
|
|
|
|
} else {
|
|
|
|
expN = 2 << (leadingZeroBits - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return expN - 1 + d.U(leadingZeroBits)
|
|
|
|
}
|
|
|
|
|
|
|
|
func uEV(d *decode.D) uint64 { return expGolomb(d) }
|
|
|
|
|
2021-09-15 01:32:00 +03:00
|
|
|
func sEV(d *decode.D) int64 {
|
|
|
|
v := expGolomb(d) + 1
|
2022-08-12 16:27:51 +03:00
|
|
|
return mathex.ZigZag(v) - -int64(v&1)
|
2021-09-15 01:32:00 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
avcNALCodedSliceNonIDR = 1
|
|
|
|
avcNALCodedSlicePartitionA = 2
|
|
|
|
avcNALCodedSlicePartitionB = 3
|
|
|
|
avcNALCodedSlicePartitionC = 4
|
|
|
|
avcNALCodedSliceIDR = 5
|
|
|
|
avcNALSupplementalEnhancementInformation = 6
|
|
|
|
avcNALSequenceParameterSet = 7
|
|
|
|
avcNALPictureParameterSet = 8
|
|
|
|
avcNALCodedSliceAuxWithoutPartition = 19
|
|
|
|
avcNALCodedSliceExtension = 20
|
|
|
|
)
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var avcNALNames = scalar.UToScalar{
|
2022-04-05 14:57:55 +03:00
|
|
|
1: {Sym: "slice", Description: "Coded slice of a non-IDR picture"},
|
|
|
|
2: {Sym: "dpa", Description: "Coded slice data partition A"},
|
|
|
|
3: {Sym: "dpb", Description: "Coded slice data partition B"},
|
|
|
|
4: {Sym: "dpc", Description: "Coded slice data partition C"},
|
|
|
|
5: {Sym: "idr_slice", Description: "Coded slice of an IDR picture"},
|
|
|
|
avcNALSupplementalEnhancementInformation: {Sym: "sei", Description: "Supplemental enhancement information"},
|
|
|
|
avcNALSequenceParameterSet: {Sym: "sps", Description: "Sequence parameter set"},
|
|
|
|
avcNALPictureParameterSet: {Sym: "pps", Description: "Picture parameter set"},
|
|
|
|
9: {Sym: "aud", Description: "Access unit delimiter"},
|
|
|
|
10: {Sym: "eoseq", Description: "End of sequence"},
|
|
|
|
11: {Sym: "eos", Description: "End of stream"},
|
|
|
|
12: {Sym: "filler", Description: "Filler data"},
|
|
|
|
13: {Sym: "sps_ext", Description: "Sequence parameter set extension"},
|
|
|
|
14: {Sym: "prefix", Description: "Prefix NAL unit"},
|
|
|
|
15: {Sym: "sub_sps", Description: "Subset sequence parameter set"},
|
|
|
|
19: {Sym: "aux_slice", Description: "Coded slice of an auxiliary coded picture without partitioning"},
|
|
|
|
20: {Sym: "exten_slice", Description: "Coded slice extension"},
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var sliceNames = scalar.UToSymStr{
|
2022-04-05 14:57:55 +03:00
|
|
|
0: "p",
|
|
|
|
1: "b",
|
|
|
|
2: "i",
|
|
|
|
3: "sp",
|
|
|
|
4: "si",
|
|
|
|
5: "p",
|
|
|
|
6: "b",
|
|
|
|
7: "i",
|
|
|
|
8: "sp",
|
|
|
|
9: "si",
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-07-19 19:33:50 +03:00
|
|
|
func avcNALUDecode(d *decode.D, _ any) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("forbidden_zero_bit")
|
|
|
|
d.FieldU2("nal_ref_idc")
|
2021-12-02 00:48:25 +03:00
|
|
|
nalType := d.FieldU5("nal_unit_type", avcNALNames)
|
2022-07-16 19:39:57 +03:00
|
|
|
unescapedBR := d.NewBitBufFromReader(nalUnescapeReader{Reader: bitio.NewIOReader(d.BitBufRange(d.Pos(), d.BitsLeft()))})
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
switch nalType {
|
|
|
|
case avcNALCodedSliceNonIDR,
|
|
|
|
avcNALCodedSlicePartitionA,
|
|
|
|
avcNALCodedSlicePartitionB,
|
|
|
|
avcNALCodedSlicePartitionC,
|
|
|
|
avcNALCodedSliceIDR,
|
|
|
|
avcNALCodedSliceAuxWithoutPartition,
|
|
|
|
avcNALCodedSliceExtension:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("slice_header", func(d *decode.D) {
|
|
|
|
d.FieldUFn("first_mb_in_slice", uEV)
|
2021-12-02 00:48:25 +03:00
|
|
|
d.FieldUFn("slice_type", uEV, sliceNames)
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldUFn("pic_parameter_set_id", uEV)
|
2020-06-08 03:29:51 +03:00
|
|
|
// TODO: if ( separate_colour_plane_flag from SPS ) colour_plane_id; frame_num
|
|
|
|
})
|
|
|
|
case avcNALSupplementalEnhancementInformation:
|
2022-01-24 23:21:48 +03:00
|
|
|
d.FieldFormatBitBuf("sei", unescapedBR, avcSEIFormat, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
case avcNALSequenceParameterSet:
|
2022-01-24 23:21:48 +03:00
|
|
|
d.FieldFormatBitBuf("sps", unescapedBR, avcSPSFormat, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
case avcNALPictureParameterSet:
|
2022-01-24 23:21:48 +03:00
|
|
|
d.FieldFormatBitBuf("pps", unescapedBR, avcPPSFormat, nil)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("data", d.BitsLeft())
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|