codec/h264/h264dec: wrote UnaryBinarization function and TestUnaryBinarization

This commit is contained in:
Saxon 2019-08-11 14:32:10 +09:30
parent 89386ceb2d
commit 336aa73107
2 changed files with 46 additions and 0 deletions

View File

@ -733,3 +733,21 @@ func CtxIdx(binIdx, maxBinIdxCtx, ctxIdxOffset int) int {
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
}

View File

@ -186,3 +186,31 @@ 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)
}
}
}