2019-07-31 16:05:09 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
helpers.go provides general helper utilities.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>, The Australian Ocean Laboratory (AusOcean)
|
|
|
|
*/
|
2020-02-26 06:57:19 +03:00
|
|
|
|
2019-07-31 16:05:09 +03:00
|
|
|
package h264dec
|
|
|
|
|
2019-08-11 15:33:25 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
2019-09-06 03:57:57 +03:00
|
|
|
"math"
|
2019-08-11 15:33:25 +03:00
|
|
|
)
|
2019-07-31 16:05:09 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2019-08-04 18:47:19 +03:00
|
|
|
for i, c := range s {
|
2019-07-31 16:05:09 +03:00
|
|
|
switch c {
|
|
|
|
case ' ':
|
|
|
|
continue
|
|
|
|
case '1':
|
|
|
|
cur |= a
|
|
|
|
case '0':
|
|
|
|
default:
|
|
|
|
return nil, errors.New("invalid binary string")
|
|
|
|
}
|
|
|
|
|
|
|
|
a >>= 1
|
2019-08-04 18:47:19 +03:00
|
|
|
if a == 0 || i == (len(s)-1) {
|
2019-07-31 16:05:09 +03:00
|
|
|
bytes = append(bytes, cur)
|
|
|
|
cur = 0
|
|
|
|
a = 0x80
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bytes, nil
|
|
|
|
}
|
2019-08-11 15:33:25 +03:00
|
|
|
|
2019-09-06 03:57:57 +03:00
|
|
|
// binToInt converts a binary string provided as a string and returns as an int.
|
|
|
|
// White spaces are ignored.
|
|
|
|
func binToInt(s string) (int, error) {
|
|
|
|
var sum int
|
|
|
|
var nSpace int
|
|
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
|
|
if s[i] == ' ' {
|
|
|
|
nSpace++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sum += int(math.Pow(2, float64(len(s)-1-i-nSpace))) * int(s[i]-'0')
|
|
|
|
}
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
2019-08-11 15:33:25 +03:00
|
|
|
func maxi(a, b int) int {
|
2019-08-14 18:46:28 +03:00
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
2019-08-11 15:33:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func mini(a, b int) int {
|
2019-08-14 18:49:13 +03:00
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
2019-08-11 15:33:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-14 18:50:25 +03:00
|
|
|
func absi(a int) int {
|
|
|
|
if a < 0 {
|
|
|
|
return -a
|
|
|
|
}
|
|
|
|
return a
|
2019-08-11 15:33:25 +03:00
|
|
|
}
|