From 838d5dd5d68afafbf3f1b27dae96188bec3131bf Mon Sep 17 00:00:00 2001 From: Saxon Date: Wed, 28 Aug 2019 10:09:27 +0930 Subject: [PATCH] codec/h264/h264dec: added level_prefix parsing process and test --- codec/h264/h264dec/cavlc.go | 46 ++++++++++++++++++++++++++ codec/h264/h264dec/cavlc_test.go | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 codec/h264/h264dec/cavlc.go create mode 100644 codec/h264/h264dec/cavlc_test.go diff --git a/codec/h264/h264dec/cavlc.go b/codec/h264/h264dec/cavlc.go new file mode 100644 index 00000000..c477a490 --- /dev/null +++ b/codec/h264/h264dec/cavlc.go @@ -0,0 +1,46 @@ +/* +DESCRIPTION + cavlc.go provides utilities for context-adaptive variable-length coding + for the parsing of H.264 syntax structure fields. + +AUTHORS + Saxon A. Nelson-Milton + +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 ( + "fmt" + + "bitbucket.org/ausocean/av/codec/h264/h264dec/bits" +) + +// parseLevelPrefix parses the level_prefix variable as specified by the process +// outlined in section 9.2.2.1 in the specifications. +func parseLevelPrefix(br *bits.BitReader) (int, error) { + zeros := -1 + for b := 0; b != 1; zeros++ { + _b, err := br.ReadBits(1) + if err != nil { + return -1, fmt.Errorf("could not read bit, failed with error: %v", err) + } + b = int(_b) + } + return zeros, nil +} diff --git a/codec/h264/h264dec/cavlc_test.go b/codec/h264/h264dec/cavlc_test.go new file mode 100644 index 00000000..a6740858 --- /dev/null +++ b/codec/h264/h264dec/cavlc_test.go @@ -0,0 +1,55 @@ +/* +DESCRIPTION + cavlc_test.go provides testing for functionality in cavlc.go. + +AUTHORS + Saxon A. Nelson-Milton + +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 ( + "bytes" + "testing" + + "bitbucket.org/ausocean/av/codec/h264/h264dec/bits" +) + +func TestParseLevelPrefix(t *testing.T) { + tests := []struct { + in string + want int + }{ + {in: "00001", want: 4}, + {in: "0000001", want: 6}, + {in: "1", want: 0}, + } + + for i, test := range tests { + s, _ := binToSlice(test.in) + l, err := parseLevelPrefix(bits.NewBitReader(bytes.NewReader(s))) + if err != nil { + t.Errorf("did not expect error: %v, for test %d", err, i) + } + + if l != test.want { + t.Errorf("did not get expected result for test %d\nGot: %d\nWant: %d\n", i, l, test.want) + } + } +}