mirror of https://bitbucket.org/ausocean/av.git
Merged in unary-binarization (pull request #232)
codec/h264/h264dec: added unary binarization processes Approved-by: Alan Noble <anoble@gmail.com>
This commit is contained in:
commit
d36fe190b0
|
@ -733,3 +733,100 @@ func CtxIdx(binIdx, maxBinIdxCtx, ctxIdxOffset int) int {
|
||||||
|
|
||||||
return ctxIdx
|
return ctxIdx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error used by unaryBinarization.
|
||||||
|
var errNegativeSyntaxVal = errors.New("cannot get unary binarization of negative value")
|
||||||
|
|
||||||
|
// unaryBinarization returns the unary binarization of a syntax element having
|
||||||
|
// value v, as specified by setion 9.3.2.1 in the specifications.
|
||||||
|
func unaryBinarization(v int) ([]int, error) {
|
||||||
|
if v < 0 {
|
||||||
|
return nil, errNegativeSyntaxVal
|
||||||
|
}
|
||||||
|
r := make([]int, v+1)
|
||||||
|
for i := 0; i <= v; i++ {
|
||||||
|
if i < v {
|
||||||
|
r[i] = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error used by truncUnaryBinarization.
|
||||||
|
var errInvalidSyntaxVal = errors.New("syntax value cannot be greater than cMax")
|
||||||
|
|
||||||
|
// truncUnaryBinarization returns the truncated unary binarization of a syntax
|
||||||
|
// element v given a cMax as specified in section 9.3.2.2 of the specifications.
|
||||||
|
func truncUnaryBinarization(v, cMax int) ([]int, error) {
|
||||||
|
if v < 0 {
|
||||||
|
return nil, errNegativeSyntaxVal
|
||||||
|
}
|
||||||
|
|
||||||
|
if v > cMax {
|
||||||
|
return nil, errInvalidSyntaxVal
|
||||||
|
}
|
||||||
|
|
||||||
|
if v == cMax {
|
||||||
|
b, _ := unaryBinarization(v)
|
||||||
|
return b[:len(b)-1], nil
|
||||||
|
}
|
||||||
|
return unaryBinarization(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error used by unaryExpGolombBinarization.
|
||||||
|
var errInvalidUCoff = errors.New("uCoff cannot be less than or equal to zero")
|
||||||
|
|
||||||
|
// unaryExpGolombBinarization returns the concatendated unary/k-th order
|
||||||
|
// Exp-Golomb (UEGk) binarization of a syntax element using the process defined
|
||||||
|
// in section 9.3.2.3 of the specifications.
|
||||||
|
func unaryExpGolombBinarization(v, uCoff, k int, signedValFlag bool) ([]int, error) {
|
||||||
|
if uCoff <= 0 {
|
||||||
|
return nil, errInvalidUCoff
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix, err := truncUnaryBinarization(mini(uCoff, absi(v)), uCoff)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return append(prefix, suffix(v, uCoff, k, signedValFlag)...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// suffix returns the suffix part of a unary k-th Exp-Golomb Binarization
|
||||||
|
// using the the algorithm as described by pseudo code 9-6 in section 9.3.2.3.
|
||||||
|
// TODO: could probably reduce allocations.
|
||||||
|
func suffix(v, uCoff, k int, signedValFlag bool) []int {
|
||||||
|
var s []int
|
||||||
|
|
||||||
|
if absi(v) >= uCoff {
|
||||||
|
sufS := absi(v) - uCoff
|
||||||
|
var stop bool
|
||||||
|
|
||||||
|
for {
|
||||||
|
if sufS >= (1 << uint(k)) {
|
||||||
|
s = append(s, 1)
|
||||||
|
sufS = sufS - (1 << uint(k))
|
||||||
|
k++
|
||||||
|
} else {
|
||||||
|
s = append(s, 0)
|
||||||
|
for k = k - 1; k >= 0; k-- {
|
||||||
|
s = append(s, (sufS>>uint(k))&1)
|
||||||
|
}
|
||||||
|
stop = true
|
||||||
|
}
|
||||||
|
if stop {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if signedValFlag && v != 0 {
|
||||||
|
if v > 0 {
|
||||||
|
s = append(s, 0)
|
||||||
|
} else {
|
||||||
|
s = append(s, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
|
@ -186,3 +186,114 @@ func TestCtxIdx(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnaryBinarization(t *testing.T) {
|
||||||
|
// Test data has been extracted from table 9-35 of the specifications.
|
||||||
|
tests := []struct {
|
||||||
|
in int
|
||||||
|
want []int
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{in: 0, want: []int{0}, err: nil},
|
||||||
|
{in: 1, want: []int{1, 0}, err: nil},
|
||||||
|
{in: 2, want: []int{1, 1, 0}, err: nil},
|
||||||
|
{in: 3, want: []int{1, 1, 1, 0}, err: nil},
|
||||||
|
{in: 4, want: []int{1, 1, 1, 1, 0}, err: nil},
|
||||||
|
{in: 5, want: []int{1, 1, 1, 1, 1, 0}, err: nil},
|
||||||
|
{in: -3, want: nil, err: errNegativeSyntaxVal},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
got, err := unaryBinarization(test.in)
|
||||||
|
if err != test.err {
|
||||||
|
t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v", i, err, test.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(test.want, got) {
|
||||||
|
t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTruncUnaryBinarization(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v int
|
||||||
|
cMax int
|
||||||
|
want []int
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{v: 0, cMax: 10, want: []int{0}, err: nil},
|
||||||
|
{v: 1, cMax: 10, want: []int{1, 0}, err: nil},
|
||||||
|
{v: 2, cMax: 10, want: []int{1, 1, 0}, err: nil},
|
||||||
|
{v: 0, cMax: 0, want: []int{}, err: nil},
|
||||||
|
{v: 4, cMax: 4, want: []int{1, 1, 1, 1}, err: nil},
|
||||||
|
{v: 1, cMax: 10, want: []int{1, 0}, err: nil},
|
||||||
|
{v: 2, cMax: 10, want: []int{1, 1, 0}, err: nil},
|
||||||
|
{v: -3, cMax: 10, want: nil, err: errNegativeSyntaxVal},
|
||||||
|
{v: 5, cMax: 4, want: nil, err: errInvalidSyntaxVal},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
got, err := truncUnaryBinarization(test.v, test.cMax)
|
||||||
|
if err != test.err {
|
||||||
|
t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v", i, err, test.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(test.want, got) {
|
||||||
|
t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUEGkSuffix(t *testing.T) {
|
||||||
|
// Data from https://patents.google.com/patent/US20070092150
|
||||||
|
tests := []struct {
|
||||||
|
v, uCoff, k int
|
||||||
|
signedValFlag bool
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
0: {v: 14, uCoff: 14, want: []int{0}},
|
||||||
|
1: {v: 15, uCoff: 14, want: []int{1, 0, 0}},
|
||||||
|
2: {v: 16, uCoff: 14, want: []int{1, 0, 1}},
|
||||||
|
3: {v: 17, uCoff: 14, want: []int{1, 1, 0, 0, 0}},
|
||||||
|
4: {v: 18, uCoff: 14, want: []int{1, 1, 0, 0, 1}},
|
||||||
|
5: {v: 19, uCoff: 14, want: []int{1, 1, 0, 1, 0}},
|
||||||
|
6: {v: 20, uCoff: 14, want: []int{1, 1, 0, 1, 1}},
|
||||||
|
7: {v: 21, uCoff: 14, want: []int{1, 1, 1, 0, 0, 0, 0}},
|
||||||
|
8: {v: 22, uCoff: 14, want: []int{1, 1, 1, 0, 0, 0, 1}},
|
||||||
|
9: {v: 23, uCoff: 14, want: []int{1, 1, 1, 0, 0, 1, 0}},
|
||||||
|
10: {v: 24, uCoff: 14, want: []int{1, 1, 1, 0, 0, 1, 1}},
|
||||||
|
11: {v: 25, uCoff: 14, want: []int{1, 1, 1, 0, 1, 0, 0}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
got := suffix(test.v, test.uCoff, test.k, test.signedValFlag)
|
||||||
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
|
t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnaryExpGolombBinarization(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v, uCoff, k int
|
||||||
|
signedValFlag bool
|
||||||
|
want []int
|
||||||
|
}{
|
||||||
|
0: {v: 7, uCoff: 14, want: []int{1, 1, 1, 1, 1, 1, 1, 0}},
|
||||||
|
1: {v: 17, uCoff: 14, want: []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}},
|
||||||
|
2: {v: 15, uCoff: 14, signedValFlag: true, want: []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}},
|
||||||
|
3: {v: -15, uCoff: 14, signedValFlag: true, want: []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
got, err := unaryExpGolombBinarization(test.v, test.uCoff, test.k, test.signedValFlag)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("did not expect error %v for test %d", err, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
|
t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,9 @@ AUTHORS
|
||||||
*/
|
*/
|
||||||
package h264dec
|
package h264dec
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
// binToSlice is a helper function to convert a string of binary into a
|
// binToSlice is a helper function to convert a string of binary into a
|
||||||
// corresponding byte slice, e.g. "0100 0001 1000 1100" => {0x41,0x8c}.
|
// corresponding byte slice, e.g. "0100 0001 1000 1100" => {0x41,0x8c}.
|
||||||
|
@ -39,3 +41,24 @@ func binToSlice(s string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
return bytes, nil
|
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(a int) int {
|
||||||
|
if a < 0 {
|
||||||
|
return -a
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue