codec/h264/h264dec: fixed field types for NalUnit and now using fieldReader in NewNALUnit

This commit is contained in:
Saxon 2019-07-29 13:04:59 +09:30
parent 03813d5a89
commit 23d9f289dd
2 changed files with 20 additions and 35 deletions

View File

@ -10,6 +10,7 @@ AUTHORS
package h264dec package h264dec
import ( import (
"fmt"
"io" "io"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -262,18 +263,18 @@ func NewSVCExtension(br *bits.BitReader) (*SVCExtension, error) {
// Field semantics are defined in section 7.4.1. // Field semantics are defined in section 7.4.1.
type NALUnit struct { type NALUnit struct {
// forbidden_zero_bit, always 0. // forbidden_zero_bit, always 0.
ForbiddenZeroBit int ForbiddenZeroBit uint8
// nal_ref_idc, if not 0 indicates content of NAL contains a sequence parameter // 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, // set, a sequence parameter set extension, a subset sequence parameter set,
// a picture parameter set, a slice of a reference picture, a slice data // 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 // partition of a reference picture, or a prefix NAL preceding a slice of
// a reference picture. // a reference picture.
RefIdc int RefIdc uint8
// nal_unit_type, specifies the type of RBSP data contained in the NAL as // nal_unit_type, specifies the type of RBSP data contained in the NAL as
// defined in Table 7-1. // defined in Table 7-1.
Type int Type uint8
// svc_extension_flag, indicates whether a nal_unit_header_svc_extension() // 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 // (G.7.3.1.1) or nal_unit_header_mvc_extension() (H.7.3.1.1) will follow next
@ -306,28 +307,19 @@ type NALUnit struct {
// syntax structure specified in section 7.3.1, and returns as a new NALUnit. // syntax structure specified in section 7.3.1, and returns as a new NALUnit.
func NewNALUnit(br *bits.BitReader) (*NALUnit, error) { func NewNALUnit(br *bits.BitReader) (*NALUnit, error) {
n := &NALUnit{} n := &NALUnit{}
r := newFieldReader(br)
err := readFields(br, []field{ n.ForbiddenZeroBit = uint8(r.readBits(1))
{&n.ForbiddenZeroBit, "ForbiddenZeroBit", 1}, n.RefIdc = uint8(r.readBits(2))
{&n.RefIdc, "NalRefIdc", 2}, n.Type = uint8(r.readBits(5))
{&n.Type, "NalUnitType", 5},
})
if err != nil {
return nil, err
}
// TODO: use consts for the NAL types here // TODO: use consts for the NAL types here
var err error
if n.Type == 14 || n.Type == 20 || n.Type == 21 { if n.Type == 14 || n.Type == 20 || n.Type == 21 {
if n.Type != 21 { if n.Type != 21 {
n.SVCExtensionFlag, err = br.ReadBool() n.SVCExtensionFlag = r.readBits(1) == 1
if err != nil {
return nil, errors.Wrap(err, "could not read SVCExtensionFlag")
}
} else { } else {
n.AVC3DExtensionFlag, err = br.ReadBool() n.AVC3DExtensionFlag = r.readBits(1) == 1
if err != nil {
return nil, errors.Wrap(err, "could not read AVC3DExtensionFlag")
}
} }
if n.SVCExtensionFlag { if n.SVCExtensionFlag {
n.SVCExtension, err = NewSVCExtension(br) n.SVCExtension, err = NewSVCExtension(br)
@ -360,27 +352,20 @@ func NewNALUnit(br *bits.BitReader) (*NALUnit, error) {
if next3Bytes == 0x000003 { if next3Bytes == 0x000003 {
for j := 0; j < 2; j++ { for j := 0; j < 2; j++ {
rbspByte, err := br.ReadBits(8) rbspByte := byte(r.readBits(8))
if err != nil {
return nil, errors.Wrap(err, "could not read rbspByte")
}
n.RBSP = append(n.RBSP, byte(rbspByte)) n.RBSP = append(n.RBSP, byte(rbspByte))
} }
// Read Emulation prevention three byte. // Read Emulation prevention three byte.
eptByte, err := br.ReadBits(8) n.EmulationPreventionThreeByte = byte(r.readBits(8))
if err != nil {
return nil, errors.Wrap(err, "could not read eptByte")
}
n.EmulationPreventionThreeByte = byte(eptByte)
} else { } else {
b, err := br.ReadBits(8) n.RBSP = append(n.RBSP, byte(r.readBits(8)))
if err != nil {
return nil, errors.Wrap(err, "could not read RBSP byte")
}
n.RBSP = append(n.RBSP, byte(b))
} }
} }
if r.err() != nil {
return nil, fmt.Errorf("fieldReader error: %v", r.err())
}
return n, nil return n, nil
} }

View File

@ -949,7 +949,7 @@ func NewSliceContext(videoStream *VideoStream, nalUnit *NALUnit, rbsp []byte, sh
var err error var err error
sps := videoStream.SPS sps := videoStream.SPS
pps := videoStream.PPS pps := videoStream.PPS
logger.Printf("debug: %s RBSP %d bytes %d bits == \n", NALUnitType[nalUnit.Type], len(rbsp), len(rbsp)*8) logger.Printf("debug: %s RBSP %d bytes %d bits == \n", NALUnitType[int(nalUnit.Type)], len(rbsp), len(rbsp)*8)
logger.Printf("debug: \t%#v\n", rbsp[0:8]) logger.Printf("debug: \t%#v\n", rbsp[0:8])
var idrPic bool var idrPic bool
if nalUnit.Type == 5 { if nalUnit.Type == 5 {
@ -974,7 +974,7 @@ func NewSliceContext(videoStream *VideoStream, nalUnit *NALUnit, rbsp []byte, sh
} }
sliceType := sliceTypeMap[header.SliceType] sliceType := sliceTypeMap[header.SliceType]
logger.Printf("debug: %s (%s) slice of %d bytes\n", NALUnitType[nalUnit.Type], sliceType, len(rbsp)) logger.Printf("debug: %s (%s) slice of %d bytes\n", NALUnitType[int(nalUnit.Type)], sliceType, len(rbsp))
header.PPSID, err = readUe(br) header.PPSID, err = readUe(br)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not parse PPSID") return nil, errors.Wrap(err, "could not parse PPSID")