Merged in fixed-len-binarization (pull request #233)

codec/h264/h264dec: added fixedLenBinarization and testing

Approved-by: Alan Noble <anoble@gmail.com>
This commit is contained in:
Saxon Milton 2019-08-19 23:39:01 +00:00
commit 0e3bdf6932
2 changed files with 103 additions and 0 deletions

View File

@ -1,6 +1,35 @@
/*
DESCRIPTION
cabac.go provides utilities for context-adaptive binary artihmetic decoding
for the parsing of H.264 syntax structure fields.
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
Bruce McMoran <mcmoranbjr@gmail.com>
Shawn Smith <shawnpsmith@gmail.com>
LICENSE
Copyright (C) 2019 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
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package h264dec
import (
"math"
"bitbucket.org/ausocean/av/codec/h264/h264dec/bits"
"github.com/pkg/errors"
)
@ -830,3 +859,22 @@ func suffix(v, uCoff, k int, signedValFlag bool) []int {
return s
}
// Error used by fixedLenBinariztion.
var errNegativeValue = errors.New("cannot get fixed length binarization of negative value")
// fixedLenBinarization returns the fixed-length (FL) binarization of the syntax
// element v, given cMax to determine bin length, as specified by section 9.3.2.4
// of the specifications.
func fixedLenBinarization(v, cMax int) ([]int, error) {
if v < 0 {
return nil, errNegativeValue
}
l := int(math.Ceil(math.Log2(float64(cMax + 1))))
r := make([]int, l)
for i := l - 1; i >= 0; i-- {
r[i] = v % 2
v = v / 2
}
return r, nil
}

View File

@ -1,3 +1,28 @@
/*
DESCRIPTION
cabac_test.go provides testing for functionality found in cabac.go.
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
Shawn Smith <shawnpsmith@gmail.com>
LICENSE
Copyright (C) 2019 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
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package h264dec
import (
@ -215,6 +240,36 @@ func TestUnaryBinarization(t *testing.T) {
}
}
func TestFixedLengthBinarization(t *testing.T) {
tests := []struct {
v int
cMax int
want []int
err error
}{
{v: 0, cMax: 7, want: []int{0, 0, 0}},
{v: 1, cMax: 7, want: []int{0, 0, 1}},
{v: 2, cMax: 7, want: []int{0, 1, 0}},
{v: 3, cMax: 7, want: []int{0, 1, 1}},
{v: 4, cMax: 7, want: []int{1, 0, 0}},
{v: 5, cMax: 7, want: []int{1, 0, 1}},
{v: 6, cMax: 7, want: []int{1, 1, 0}},
{v: 7, cMax: 7, want: []int{1, 1, 1}},
{v: -1, cMax: 7, want: nil, err: errNegativeValue},
}
for i, test := range tests {
got, err := fixedLenBinarization(test.v, test.cMax)
if err != test.err {
t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v\n", 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