2019-07-19 14:21:48 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
nalunit.go provides structures for a NAL unit as well as it's extensions.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>, The Australian Ocean Laboratory (AusOcean)
|
|
|
|
mrmod <mcmoranbjr@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2019-07-19 08:50:39 +03:00
|
|
|
package h264dec
|
2019-07-18 07:32:42 +03:00
|
|
|
|
|
|
|
import (
|
2019-07-29 06:34:59 +03:00
|
|
|
"fmt"
|
2019-07-19 13:50:33 +03:00
|
|
|
"io"
|
|
|
|
|
2019-07-18 07:32:42 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2019-07-19 13:53:30 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/h264/h264dec/bits"
|
2019-07-19 13:50:33 +03:00
|
|
|
)
|
2019-07-18 07:32:42 +03:00
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// MVCExtension describes a NAL unit header multiview video coding extension, as
|
|
|
|
// defined in section H.7.3.1.1 of the specifications.
|
2019-07-27 05:56:30 +03:00
|
|
|
// Semantics of fields are specified in section H.7.4.1.1.
|
2019-07-19 11:34:51 +03:00
|
|
|
type MVCExtension struct {
|
2019-07-27 05:56:30 +03:00
|
|
|
// non_idr_flag, if true indicates that access unit is not IDR.
|
|
|
|
NonIdrFlag bool
|
|
|
|
|
|
|
|
// priority_id, indicates priority of NAL unit. A lower value => higher priority.
|
2019-07-29 07:11:40 +03:00
|
|
|
PriorityID uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// view_id, specifies a view identifier for the unit. Units with identical
|
|
|
|
// view_id are in the same view.
|
2019-07-29 07:11:40 +03:00
|
|
|
ViewID uint32
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// temporal_id, temporal identifier for the unit.
|
2019-07-29 07:11:40 +03:00
|
|
|
TemporalID uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// anchor_pic_flag, if true access unit is an anchor access unit.
|
|
|
|
AnchorPicFlag bool
|
|
|
|
|
|
|
|
// inter_view_flag, if false current view component not used for inter-view
|
|
|
|
// prediction elsewhere in access unit.
|
|
|
|
InterViewFlag bool
|
|
|
|
|
|
|
|
// reserved_one_bit, always 1 (ignored by decoders)
|
2019-07-29 07:11:40 +03:00
|
|
|
ReservedOneBit uint8
|
2019-07-19 11:34:51 +03:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// NewMVCExtension parses a NAL unit header multiview video coding extension
|
|
|
|
// from br following the syntax structure specified in section H.7.3.1.1, and
|
|
|
|
// returns as a new MVCExtension.
|
2019-07-19 11:34:51 +03:00
|
|
|
func NewMVCExtension(br *bits.BitReader) (*MVCExtension, error) {
|
|
|
|
e := &MVCExtension{}
|
2019-07-29 07:11:40 +03:00
|
|
|
r := newFieldReader(br)
|
2019-07-19 11:34:51 +03:00
|
|
|
|
2019-07-29 07:11:40 +03:00
|
|
|
e.NonIdrFlag = r.readBits(1) == 1
|
|
|
|
e.PriorityID = uint8(r.readBits(6))
|
|
|
|
e.ViewID = uint32(r.readBits(10))
|
|
|
|
e.TemporalID = uint8(r.readBits(3))
|
|
|
|
e.AnchorPicFlag = r.readBits(1) == 1
|
|
|
|
e.InterViewFlag = r.readBits(1) == 1
|
|
|
|
e.ReservedOneBit = uint8(r.readBits(1))
|
2019-07-19 11:34:51 +03:00
|
|
|
|
2019-07-29 07:11:40 +03:00
|
|
|
if r.err() != nil {
|
|
|
|
return nil, fmt.Errorf("error from fieldReader: %v", r.err())
|
2019-07-19 11:34:51 +03:00
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// ThreeDAVCExtension describes a NAL unit header 3D advanced video coding
|
|
|
|
// extension, as defined in section J.7.3.1.1 of the specifications.
|
2019-07-27 05:56:30 +03:00
|
|
|
// For field semantics see section J.7.4.1.1.
|
2019-07-19 11:20:45 +03:00
|
|
|
type ThreeDAVCExtension struct {
|
2019-07-27 05:56:30 +03:00
|
|
|
// view_idx, specifies the order index for the NAL i.e. view_id = view_id[view_idx].
|
2019-07-29 07:11:40 +03:00
|
|
|
ViewIdx uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// dpeth_flag, if true indicates NAL part of a depth view component, otherwise
|
|
|
|
// a texture view component.
|
|
|
|
DepthFlag bool
|
|
|
|
|
|
|
|
// non_idr_flag, if true indicates that access unit is not IDR.
|
|
|
|
NonIdrFlag bool
|
|
|
|
|
|
|
|
// temporal_id, temporal identifier for the unit.
|
2019-07-29 07:11:40 +03:00
|
|
|
TemporalID uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// anchor_pic_flag, if true access unit is an anchor access unit.
|
2019-07-19 11:20:45 +03:00
|
|
|
AnchorPicFlag bool
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// inter_view_flag, if false current view component not used for inter-view
|
|
|
|
// prediction elsewhere in access unit.
|
2019-07-19 11:20:45 +03:00
|
|
|
InterViewFlag bool
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// NewThreeDAVCExtension parses a NAL unit header 3D advanced video coding
|
|
|
|
// extension from br following the syntax structure specified in section
|
|
|
|
// J.7.3.1.1, and returns as a new ThreeDAVCExtension.
|
2019-07-19 11:20:45 +03:00
|
|
|
func NewThreeDAVCExtension(br *bits.BitReader) (*ThreeDAVCExtension, error) {
|
|
|
|
e := &ThreeDAVCExtension{}
|
2019-07-29 07:11:40 +03:00
|
|
|
r := newFieldReader(br)
|
2019-07-19 11:20:45 +03:00
|
|
|
|
2019-07-29 07:11:40 +03:00
|
|
|
e.ViewIdx = uint8(r.readBits(8))
|
|
|
|
e.DepthFlag = r.readBits(1) == 1
|
|
|
|
e.NonIdrFlag = r.readBits(1) == 1
|
|
|
|
e.TemporalID = uint8(r.readBits(3))
|
|
|
|
e.AnchorPicFlag = r.readBits(1) == 1
|
|
|
|
e.InterViewFlag = r.readBits(1) == 1
|
2019-07-19 11:20:45 +03:00
|
|
|
|
2019-07-29 07:11:40 +03:00
|
|
|
if r.err() != nil {
|
|
|
|
return nil, fmt.Errorf("error from fieldReader: %v", r.err())
|
2019-07-19 11:20:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// SVCExtension describes a NAL unit header scalable video coding extension, as
|
|
|
|
// defined in section G.7.3.1.1 of the specifications.
|
2019-07-27 05:56:30 +03:00
|
|
|
// For field semantics see section G.7.4.1.1.
|
2019-07-19 11:07:54 +03:00
|
|
|
type SVCExtension struct {
|
2019-07-27 05:56:30 +03:00
|
|
|
// idr_flag, if true the current coded picture is an IDR picture when
|
|
|
|
// dependency_id == max(dependency_id) in the coded picture.
|
|
|
|
IdrFlag bool
|
|
|
|
|
|
|
|
// priority_id, specifies priority identifier for unit.
|
2019-07-29 07:11:40 +03:00
|
|
|
PriorityID uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// no_inter_layer_pred_flag, if true inter-layer prediction can't be used for
|
|
|
|
// decoding slice.
|
2019-07-19 11:07:54 +03:00
|
|
|
NoInterLayerPredFlag bool
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// dependency_id, specifies a dependency identifier for the NAL.
|
2019-07-29 07:11:40 +03:00
|
|
|
DependencyID uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// quality_id, specifies a quality identifier for the NAL.
|
2019-07-29 07:11:40 +03:00
|
|
|
QualityID uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// temporal_id, specifiesa temporal identifier for the NAL.
|
2019-07-29 07:11:40 +03:00
|
|
|
TemporalID uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// use_ref_base_pic_flag, if true indicates reference base pictures and
|
|
|
|
// decoded pictures are used as references for inter prediction.
|
|
|
|
UseRefBasePicFlag bool
|
|
|
|
|
|
|
|
// discardable_flag, if true, indicates current NAL is not used for decoding
|
|
|
|
// dependency representations that are part of the current coded picture or
|
|
|
|
// any subsequent coded picture in decoding order and have a greater
|
|
|
|
// dependency_id value than current NAL.
|
|
|
|
DiscardableFlag bool
|
|
|
|
|
|
|
|
// output_flag, affects the decoded picture output and removal processes as
|
|
|
|
// specified in Annex C.
|
|
|
|
OutputFlag bool
|
|
|
|
|
|
|
|
// reserved_three_2bits, equal to 3. Decoders ignore.
|
2019-07-29 07:11:40 +03:00
|
|
|
ReservedThree2Bits uint8
|
2019-07-19 11:07:54 +03:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// NewSVCExtension parses a NAL unit header scalable video coding extension from
|
|
|
|
// br following the syntax structure specified in section G.7.3.1.1, and returns
|
|
|
|
// as a new SVCExtension.
|
2019-07-19 11:07:54 +03:00
|
|
|
func NewSVCExtension(br *bits.BitReader) (*SVCExtension, error) {
|
|
|
|
e := &SVCExtension{}
|
2019-07-29 07:11:40 +03:00
|
|
|
r := newFieldReader(br)
|
|
|
|
|
|
|
|
e.IdrFlag = r.readBits(1) == 1
|
|
|
|
e.PriorityID = uint8(r.readBits(6))
|
|
|
|
e.NoInterLayerPredFlag = r.readBits(1) == 1
|
|
|
|
e.DependencyID = uint8(r.readBits(3))
|
|
|
|
e.QualityID = uint8(r.readBits(4))
|
|
|
|
e.TemporalID = uint8(r.readBits(3))
|
|
|
|
e.UseRefBasePicFlag = r.readBits(1) == 1
|
|
|
|
e.DiscardableFlag = r.readBits(1) == 1
|
|
|
|
e.OutputFlag = r.readBits(1) == 1
|
|
|
|
e.ReservedThree2Bits = uint8(r.readBits(2))
|
|
|
|
|
|
|
|
if r.err() != nil {
|
|
|
|
return nil, fmt.Errorf("error from fieldReader: %v", r.err())
|
2019-07-19 11:07:54 +03:00
|
|
|
}
|
|
|
|
return e, nil
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// NALUnit describes a network abstraction layer unit, as defined in section
|
|
|
|
// 7.3.1 of the specifications.
|
2019-07-27 05:56:30 +03:00
|
|
|
// Field semantics are defined in section 7.4.1.
|
2019-07-19 13:50:33 +03:00
|
|
|
type NALUnit struct {
|
2019-07-27 05:56:30 +03:00
|
|
|
// forbidden_zero_bit, always 0.
|
2019-07-29 06:34:59 +03:00
|
|
|
ForbiddenZeroBit uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// nal_ref_idc, if not 0 indicates content of NAL contains a sequence parameter
|
|
|
|
// set, a sequence parameter set extension, a subset sequence parameter set,
|
|
|
|
// a picture parameter set, a slice of a reference picture, a slice data
|
|
|
|
// partition of a reference picture, or a prefix NAL preceding a slice of
|
|
|
|
// a reference picture.
|
2019-07-29 06:34:59 +03:00
|
|
|
RefIdc uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// nal_unit_type, specifies the type of RBSP data contained in the NAL as
|
|
|
|
// defined in Table 7-1.
|
2019-07-29 06:34:59 +03:00
|
|
|
Type uint8
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// svc_extension_flag, indicates whether a nal_unit_header_svc_extension()
|
|
|
|
// (G.7.3.1.1) or nal_unit_header_mvc_extension() (H.7.3.1.1) will follow next
|
|
|
|
// in the syntax structure.
|
|
|
|
SVCExtensionFlag bool
|
|
|
|
|
|
|
|
// avc_3d_extension_flag, for nal_unit_type = 21, indicates that a
|
|
|
|
// nal_unit_header_mvc_extension() (H.7.3.1.1) or
|
|
|
|
// nal_unit_header_3davc_extension() (J.7.3.1.1) will follow next in syntax
|
|
|
|
// structure.
|
|
|
|
AVC3DExtensionFlag bool
|
|
|
|
|
|
|
|
// nal_unit_header_svc_extension() as defined in section G.7.3.1.1.
|
|
|
|
SVCExtension *SVCExtension
|
|
|
|
|
|
|
|
// nal_unit_header_3davc_extension() as defined in section J.7.3.1.1
|
|
|
|
ThreeDAVCExtension *ThreeDAVCExtension
|
|
|
|
|
|
|
|
// nal_unit_header_mvc_extension() as defined in section H.7.3.1.1).
|
|
|
|
MVCExtension *MVCExtension
|
|
|
|
|
|
|
|
// emulation_prevention_three_byte, equal to 0x03 and is discarded by decoder.
|
2019-07-19 13:50:33 +03:00
|
|
|
EmulationPreventionThreeByte byte
|
2019-07-27 05:56:30 +03:00
|
|
|
|
|
|
|
// rbsp_byte, the raw byte sequence payload data for the NAL.
|
|
|
|
RBSP []byte
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:21:48 +03:00
|
|
|
// NewNALUnit parses a network abstraction layer unit from br following the
|
|
|
|
// syntax structure specified in section 7.3.1, and returns as a new NALUnit.
|
2019-07-19 13:50:33 +03:00
|
|
|
func NewNALUnit(br *bits.BitReader) (*NALUnit, error) {
|
|
|
|
n := &NALUnit{}
|
2019-07-29 06:34:59 +03:00
|
|
|
r := newFieldReader(br)
|
2019-07-18 07:32:42 +03:00
|
|
|
|
2019-07-29 06:34:59 +03:00
|
|
|
n.ForbiddenZeroBit = uint8(r.readBits(1))
|
|
|
|
n.RefIdc = uint8(r.readBits(2))
|
|
|
|
n.Type = uint8(r.readBits(5))
|
2019-07-18 07:32:42 +03:00
|
|
|
|
2019-07-29 06:34:59 +03:00
|
|
|
var err error
|
2019-07-29 07:38:59 +03:00
|
|
|
if n.Type == naluTypePrefixNALU || n.Type == naluTypeSliceLayerExtRBSP || n.Type == naluTypeSliceLayerExtRBSP2 {
|
|
|
|
if n.Type != naluTypeSliceLayerExtRBSP2 {
|
2019-07-29 06:34:59 +03:00
|
|
|
n.SVCExtensionFlag = r.readBits(1) == 1
|
2019-07-18 07:32:42 +03:00
|
|
|
} else {
|
2019-07-29 06:34:59 +03:00
|
|
|
n.AVC3DExtensionFlag = r.readBits(1) == 1
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|
2019-07-19 13:50:33 +03:00
|
|
|
if n.SVCExtensionFlag {
|
|
|
|
n.SVCExtension, err = NewSVCExtension(br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not parse SVCExtension")
|
|
|
|
}
|
|
|
|
} else if n.AVC3DExtensionFlag {
|
|
|
|
n.ThreeDAVCExtension, err = NewThreeDAVCExtension(br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not parse ThreeDAVCExtension")
|
|
|
|
}
|
2019-07-18 07:32:42 +03:00
|
|
|
} else {
|
2019-07-19 13:50:33 +03:00
|
|
|
n.MVCExtension, err = NewMVCExtension(br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not parse MVCExtension")
|
|
|
|
}
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:50:33 +03:00
|
|
|
for moreRBSPData(br) {
|
2019-07-31 19:58:29 +03:00
|
|
|
fmt.Println("here")
|
2019-07-18 07:32:42 +03:00
|
|
|
next3Bytes, err := br.PeekBits(24)
|
2019-07-21 14:52:55 +03:00
|
|
|
|
|
|
|
// If PeekBits cannot get 3 bytes, but there still might be 2 bytes left in
|
|
|
|
// the source, we will get an io.EOF; we wish to ignore this and continue.
|
|
|
|
// The call to moreRBSPData will determine when we have reached the end of
|
|
|
|
// the NAL unit.
|
2019-07-19 13:50:33 +03:00
|
|
|
if err != nil && errors.Cause(err) != io.EOF {
|
2019-07-19 14:28:01 +03:00
|
|
|
return nil, errors.Wrap(err, "could not Peek next 3 bytes")
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|
2019-07-19 13:50:33 +03:00
|
|
|
|
|
|
|
if next3Bytes == 0x000003 {
|
2019-07-18 07:32:42 +03:00
|
|
|
for j := 0; j < 2; j++ {
|
2019-07-29 06:34:59 +03:00
|
|
|
rbspByte := byte(r.readBits(8))
|
2019-07-19 14:28:01 +03:00
|
|
|
n.RBSP = append(n.RBSP, byte(rbspByte))
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read Emulation prevention three byte.
|
2019-07-29 06:34:59 +03:00
|
|
|
n.EmulationPreventionThreeByte = byte(r.readBits(8))
|
2019-07-18 07:32:42 +03:00
|
|
|
} else {
|
2019-07-29 06:34:59 +03:00
|
|
|
n.RBSP = append(n.RBSP, byte(r.readBits(8)))
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 06:34:59 +03:00
|
|
|
if r.err() != nil {
|
|
|
|
return nil, fmt.Errorf("fieldReader error: %v", r.err())
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:50:33 +03:00
|
|
|
return n, nil
|
2019-07-18 07:32:42 +03:00
|
|
|
}
|