codec/h264/h264dec: moved fieldReader to parse.go and wrote methods for reading other descriptor types like ue and te.

This commit is contained in:
Saxon 2019-07-27 13:59:12 +09:30
parent 404e6493b6
commit 9f47b22a84
2 changed files with 92 additions and 40 deletions

View File

@ -36,6 +36,98 @@ const (
naMbPartPredMode
)
// fieldReader provides methods for reading bool and int fields from a
// bits.BitReader with a sticky error that may be checked after a series of
// parsing read calls.
type fieldReader struct {
e error
br *bits.BitReader
}
// newFieldReader returns a new fieldReader.
func newFieldReader(br *bits.BitReader) fieldReader {
return fieldReader{br: br}
}
// readBool returns a bool from reading one bit from br. If we have an error
// already, we do not continue with the read.
func (r fieldReader) readBool() bool {
if r.e != nil {
return false
}
var b uint64
b, r.e = r.br.ReadBits(1)
return b == 1
}
// readBitsInt returns an int from reading n bits from br. If we have an error
// already, we do not continue with the read.
func (r fieldReader) readBitsInt(n int) int {
if r.e != nil {
return 0
}
var b uint64
b, r.e = r.br.ReadBits(n)
return int(b)
}
// readUe parses a syntax element of ue(v) descriptor, i.e. an unsigned integer
// Exp-Golomb-coded element using method as specified in section 9.1 of ITU-T
// H.264 and return as an int. The read does not happen if the fieldReader
// has a non-nil error.
func (r fieldReader) readUe() int {
if r.e != nil {
return 0
}
var i int
i, r.e = readUe(r.br)
return i
}
// readTe parses a syntax element of te(v) descriptor i.e, truncated
// Exp-Golomb-coded syntax element using method as specified in section 9.1
// and returns as an int. The read does not happen if the fieldReader
// has a non-nil error.
func (r fieldReader) readTe(x uint) int {
if r.e != nil {
return 0
}
var i int
i, r.e = readTe(r.br, x)
return i
}
// readSe parses a syntax element with descriptor se(v), i.e. a signed integer
// Exp-Golomb-coded syntax element, using the method described in sections
// 9.1 and 9.1.1 and returns as int. The read does not happen if the fieldReader
// has a non-nil error.
func (r fieldReader) readSe() int {
if r.e != nil {
return 0
}
var i int
i, r.e = readSe(r.br)
return i
}
// readMe parses a syntax element of me(v) descriptor, i.e. mapped
// Exp-Golomb-coded element, using methods described in sections 9.1 and 9.1.2
// and returns as int. The read does not happen if the fieldReader has a
// non-nil error.
func (r fieldReader) readMe(chromaArrayType uint, mpm mbPartPredMode) int {
if r.e != nil {
return 0
}
var i uint
i, r.e = readMe(r.br, chromaArrayType, mpm)
return int(i)
}
// err returns the fieldReader's error e.
func (r fieldReader) err() error {
return r.e
}
// readUe parses a syntax element of ue(v) descriptor, i.e. an unsigned integer
// Exp-Golomb-coded element using method as specified in section 9.1 of ITU-T H.264.
//

View File

@ -228,43 +228,3 @@ func readFlags(br *bits.BitReader, flags []flag) error {
}
return nil
}
// fieldReader provides methods for reading bool and int fields from a
// bits.BitReader with a sticky error that may be checked after a series of
// parsing read calls.
type fieldReader struct {
e error
br *bits.BitReader
}
// newFieldReader returns a new fieldReader.
func newFieldReader(br *bits.BitReader) fieldReader {
return fieldReader{br: br}
}
// readBool returns a bool from reading one bit from br. If we have an error
// already, we do not continue with the read.
func (r fieldReader) readBool() bool {
if r.e != nil {
return false
}
var b uint64
b, r.e = r.br.ReadBits(1)
return b == 1
}
// readBitsInt returns an int from reading n bits from br. If we have an error
// already, we do not continue with the read.
func (r fieldReader) readBitsInt(n int) int {
if r.e != nil {
return 0
}
var b uint64
b, r.e = r.br.ReadBits(n)
return int(b)
}
// err returns the fieldReader's error e.
func (r fieldReader) err() error {
return r.e
}