From 404e6493b609d533e3583c8b300563b65554074f Mon Sep 17 00:00:00 2001 From: Saxon Date: Sat, 27 Jul 2019 13:35:05 +0930 Subject: [PATCH] codec/h264/h26dec/read.go: added fieldReader type to provide sticky error and specific methods for bool and int reading --- codec/h264/h264dec/read.go | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/codec/h264/h264dec/read.go b/codec/h264/h264dec/read.go index 92a12ed9..f0a65500 100644 --- a/codec/h264/h264dec/read.go +++ b/codec/h264/h264dec/read.go @@ -228,3 +228,43 @@ 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 +}