mirror of https://bitbucket.org/ausocean/av.git
63 lines
951 B
Go
63 lines
951 B
Go
/*
|
|
DESCRIPTION
|
|
helpers.go provides general helper utilities.
|
|
|
|
AUTHORS
|
|
Saxon Nelson-Milton <saxon@ausocean.org>, The Australian Ocean Laboratory (AusOcean)
|
|
*/
|
|
package h264dec
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
func maxi(a, b int) int {
|
|
if a > b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func mini(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func absi(i int) int {
|
|
return int(math.Abs(float64(i)))
|
|
}
|