From 547e9f22ae0a84860cf9cf2cb2fb6c5c8706798f Mon Sep 17 00:00:00 2001 From: Saxon Date: Wed, 31 Jul 2019 22:35:09 +0930 Subject: [PATCH] codec/h264/h264dec/helpers.go: added helpers.go file and binToSlice func for converting binary string to a []byte --- codec/h264/h264dec/helpers.go | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 codec/h264/h264dec/helpers.go diff --git a/codec/h264/h264dec/helpers.go b/codec/h264/h264dec/helpers.go new file mode 100644 index 00000000..22e9a5eb --- /dev/null +++ b/codec/h264/h264dec/helpers.go @@ -0,0 +1,41 @@ +/* +DESCRIPTION + helpers.go provides general helper utilities. + +AUTHORS + Saxon Nelson-Milton , The Australian Ocean Laboratory (AusOcean) +*/ +package h264dec + +import "errors" + +// binToSlice is a helper function to convert a string of binary into a +// corresponding byte slice, e.g. "0100 0001 1000 1100" => {0x41,0x8c}. +// Spaces in the string are ignored. +func binToSlice(s string) ([]byte, error) { + var ( + a byte = 0x80 + cur byte + bytes []byte + ) + + for _, c := range s { + switch c { + case ' ': + continue + case '1': + cur |= a + case '0': + default: + return nil, errors.New("invalid binary string") + } + + a >>= 1 + if a == 0 { + bytes = append(bytes, cur) + cur = 0 + a = 0x80 + } + } + return bytes, nil +}