From 9f47b22a84b82dc09579852238d45a14f5815b81 Mon Sep 17 00:00:00 2001 From: Saxon Date: Sat, 27 Jul 2019 13:59:12 +0930 Subject: [PATCH] codec/h264/h264dec: moved fieldReader to parse.go and wrote methods for reading other descriptor types like ue and te. --- codec/h264/h264dec/parse.go | 92 +++++++++++++++++++++++++++++++++++++ codec/h264/h264dec/read.go | 40 ---------------- 2 files changed, 92 insertions(+), 40 deletions(-) diff --git a/codec/h264/h264dec/parse.go b/codec/h264/h264dec/parse.go index 86baaece..e6fba287 100644 --- a/codec/h264/h264dec/parse.go +++ b/codec/h264/h264dec/parse.go @@ -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. // diff --git a/codec/h264/h264dec/read.go b/codec/h264/h264dec/read.go index f0a65500..92a12ed9 100644 --- a/codec/h264/h264dec/read.go +++ b/codec/h264/h264dec/read.go @@ -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 -}