mirror of https://bitbucket.org/ausocean/av.git
98 lines
2.0 KiB
Go
98 lines
2.0 KiB
Go
|
/*
|
||
|
DESCRIPTION
|
||
|
helpers.go provides general helper utilities.
|
||
|
|
||
|
AUTHORS
|
||
|
Saxon Nelson-Milton <saxon@ausocean.org>, The Australian Ocean Laboratory (AusOcean)
|
||
|
|
||
|
LICENSE
|
||
|
rtp.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
|
||
|
|
||
|
It is free software: you can redistribute it and/or modify them
|
||
|
under the terms of the GNU General Public License as published by the
|
||
|
Free Software Foundation, either version 3 of the License, or (at your
|
||
|
option) any later version.
|
||
|
|
||
|
It is distributed in the hope that it will be useful, but WITHOUT
|
||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||
|
for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||
|
*/
|
||
|
|
||
|
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 i, 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 || i == (len(s)-1) {
|
||
|
bytes = append(bytes, cur)
|
||
|
cur = 0
|
||
|
a = 0x80
|
||
|
}
|
||
|
}
|
||
|
return bytes, nil
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
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(a int) int {
|
||
|
if a < 0 {
|
||
|
return -a
|
||
|
}
|
||
|
return a
|
||
|
}
|