From 48955967e039b751e6972d4bfb55aa483b921cf4 Mon Sep 17 00:00:00 2001 From: Saxon Date: Wed, 31 Jul 2019 22:53:26 +0930 Subject: [PATCH 1/8] codec/h264/h264dec/nalunit_test.go: added TestNewMVCExtension --- codec/h264/h264dec/nalunit_test.go | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 codec/h264/h264dec/nalunit_test.go diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go new file mode 100644 index 00000000..2519fafa --- /dev/null +++ b/codec/h264/h264dec/nalunit_test.go @@ -0,0 +1,52 @@ +package h264dec + +import ( + "bytes" + "reflect" + "testing" + + "bitbucket.org/ausocean/av/codec/h264/h264dec/bits" +) + +func TestNewMVCExtension(t *testing.T) { + tests := []struct { + in string + want MVCExtension + err error + }{ + { + in: "0" + // u(1) non_idr_flag = false + "00 0010" + // u(6) priority_id = 2 + "00 0001 1000" + // u(10) view_id = 24 + "100" + // u(3) temporal_id = 4 + "1" + // u(1) anchor_pic_flag = true + "0" + // u(1) inter_view_flag = false + "1", // u(1) reserved_one_bit = 1 + want: MVCExtension{ + NonIdrFlag: false, + PriorityID: 2, + ViewID: 24, + TemporalID: 4, + AnchorPicFlag: true, + InterViewFlag: false, + ReservedOneBit: 1, + }, + }, + } + + for i, test := range tests { + inBytes, err := binToSlice(test.in) + if err != nil { + t.Fatalf("did not expect error %v from binToSlice for test %d", err, i) + } + + got, err := NewMVCExtension(bits.NewBitReader(bytes.NewReader(inBytes))) + 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(*got, test.want) { + t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, *got, test.want) + } + } +} From 96377f8788411c589cc469ee9611bc399e023559 Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 1 Aug 2019 00:15:07 +0930 Subject: [PATCH 2/8] codec/h264/h264dec/nalunit_test.go: added TestNewThreeDAVCExtension --- codec/h264/h264dec/nalunit_test.go | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go index 2519fafa..f3828880 100644 --- a/codec/h264/h264dec/nalunit_test.go +++ b/codec/h264/h264dec/nalunit_test.go @@ -50,3 +50,44 @@ func TestNewMVCExtension(t *testing.T) { } } } + +func TestNewThreeDAVCExtension(t *testing.T) { + tests := []struct { + in string + want ThreeDAVCExtension + err error + }{ + { + in: "0001 0000" + // u(8) view_idx = 16 + "1" + // u(1) depth_flag = true + "0" + // u(1) non_idr_flag = false + "010" + // u(1) temporal_id = 2 + "1" + // u(1) anchor_pic_flag = true + "1", // u(1) inter_view_flag = true + want: ThreeDAVCExtension{ + ViewIdx: 16, + DepthFlag: true, + NonIdrFlag: false, + TemporalID: 2, + AnchorPicFlag: true, + InterViewFlag: true, + }, + }, + } + + for i, test := range tests { + inBytes, err := binToSlice(test.in) + if err != nil { + t.Fatalf("did not expect error %v from binToSlice for test %d", err, i) + } + + got, err := NewThreeDAVCExtension(bits.NewBitReader(bytes.NewReader(inBytes))) + 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(*got, test.want) { + t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, *got, test.want) + } + } +} From aab3473abfba0f178e4beeab373c803df99c12ff Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 1 Aug 2019 00:32:54 +0930 Subject: [PATCH 3/8] codec/h264/h264dec: fixed bug in tests --- codec/h264/h264dec/nalunit_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go index f3828880..930fd2b3 100644 --- a/codec/h264/h264dec/nalunit_test.go +++ b/codec/h264/h264dec/nalunit_test.go @@ -21,7 +21,8 @@ func TestNewMVCExtension(t *testing.T) { "100" + // u(3) temporal_id = 4 "1" + // u(1) anchor_pic_flag = true "0" + // u(1) inter_view_flag = false - "1", // u(1) reserved_one_bit = 1 + "1" + // u(1) reserved_one_bit = 1 + "0 00000000", // Some padding want: MVCExtension{ NonIdrFlag: false, PriorityID: 2, @@ -63,7 +64,8 @@ func TestNewThreeDAVCExtension(t *testing.T) { "0" + // u(1) non_idr_flag = false "010" + // u(1) temporal_id = 2 "1" + // u(1) anchor_pic_flag = true - "1", // u(1) inter_view_flag = true + "1" + // u(1) inter_view_flag = true + "000", // Some padding want: ThreeDAVCExtension{ ViewIdx: 16, DepthFlag: true, From d5776c4496d84decba68bf0a73eb12b887a9c3ff Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 1 Aug 2019 00:57:38 +0930 Subject: [PATCH 4/8] codec/h264/h264dec: added TestNewSVCExtension --- codec/h264/h264dec/nalunit_test.go | 52 +++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go index 930fd2b3..b294760e 100644 --- a/codec/h264/h264dec/nalunit_test.go +++ b/codec/h264/h264dec/nalunit_test.go @@ -22,7 +22,7 @@ func TestNewMVCExtension(t *testing.T) { "1" + // u(1) anchor_pic_flag = true "0" + // u(1) inter_view_flag = false "1" + // u(1) reserved_one_bit = 1 - "0 00000000", // Some padding + "0", // Some padding want: MVCExtension{ NonIdrFlag: false, PriorityID: 2, @@ -93,3 +93,53 @@ func TestNewThreeDAVCExtension(t *testing.T) { } } } + +func TestSVCExtension(t *testing.T) { + tests := []struct { + in string + want SVCExtension + err error + }{ + { + in: "0" + // u(1) idr_flag = false + "10 0000" + // u(6) priority_id = 32 + "0" + // u(1) no_inter_layer_pred_flag = false + "001" + // u(3) dependency_id = 1 + "1000" + // u(4) quality_id = 8 + "010" + // u(3) temporal_id = 2 + "1" + // u(1) use_ref_base_pic_flag = true + "0" + // u(1) discardable_flag = false + "0" + // u(1) output_flag = false + "11" + // ReservedThree2Bits + "0", // padding + want: SVCExtension{ + IdrFlag: false, + PriorityID: 32, + NoInterLayerPredFlag: false, + DependencyID: 1, + QualityID: 8, + TemporalID: 2, + UseRefBasePicFlag: true, + DiscardableFlag: false, + OutputFlag: false, + ReservedThree2Bits: 3, + }, + }, + } + + for i, test := range tests { + inBytes, err := binToSlice(test.in) + if err != nil { + t.Fatalf("did not expect error %v from binToSlice for test %d", err, i) + } + + got, err := NewSVCExtension(bits.NewBitReader(bytes.NewReader(inBytes))) + 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(*got, test.want) { + t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, *got, test.want) + } + } +} From 5185a70bbef844f3834e256badd3c9958c7f1eb4 Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 1 Aug 2019 02:28:29 +0930 Subject: [PATCH 5/8] codec/h264/h264dec: added TestNewNALUnit but broken, need to fix --- codec/h264/h264dec/nalunit.go | 1 + codec/h264/h264dec/nalunit_test.go | 113 +++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/codec/h264/h264dec/nalunit.go b/codec/h264/h264dec/nalunit.go index f1535855..dd0248a1 100644 --- a/codec/h264/h264dec/nalunit.go +++ b/codec/h264/h264dec/nalunit.go @@ -260,6 +260,7 @@ func NewNALUnit(br *bits.BitReader) (*NALUnit, error) { } for moreRBSPData(br) { + fmt.Println("here") next3Bytes, err := br.PeekBits(24) // If PeekBits cannot get 3 bytes, but there still might be 2 bytes left in diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go index b294760e..419c6c5f 100644 --- a/codec/h264/h264dec/nalunit_test.go +++ b/codec/h264/h264dec/nalunit_test.go @@ -143,3 +143,116 @@ func TestSVCExtension(t *testing.T) { } } } + +func TestNewNALUnit(t *testing.T) { + tests := []struct { + in string + want NALUnit + err error + }{ + { + in: "0" + // f(1) forbidden_zero_bit = 0 + "01" + // u(2) nal_ref_idc = 1 + "0 1110" + // u(5) nal_unit_type = 14 + "1" + // u(1) svc_extension_flag = true + + // svc extension + "0" + // u(1) idr_flag = false + "10 0000" + // u(6) priority_id = 32 + "0" + // u(1) no_inter_layer_pred_flag = false + "001" + // u(3) dependency_id = 1 + "1000" + // u(4) quality_id = 8 + "010" + // u(3) temporal_id = 2 + "1" + // u(1) use_ref_base_pic_flag = true + "0" + // u(1) discardable_flag = false + "0" + // u(1) output_flag = false + "11" + // ReservedThree2Bits + + // rbsp bytes + "0000 0001" + + "0000 0010" + + "0000 0100" + + "0000 1000" + + "1000 0000", + + want: NALUnit{ + ForbiddenZeroBit: 0, + RefIdc: 1, + Type: 14, + SVCExtensionFlag: true, + SVCExtension: &SVCExtension{ + IdrFlag: false, + PriorityID: 32, + NoInterLayerPredFlag: false, + DependencyID: 1, + QualityID: 8, + TemporalID: 2, + UseRefBasePicFlag: true, + DiscardableFlag: false, + OutputFlag: false, + ReservedThree2Bits: 3, + }, + + RBSP: []byte{ + 0x01, + 0x02, + 0x04, + 0x08, + 0x80, + }, + }, + }, + } + + for i, test := range tests { + inBytes, err := binToSlice(test.in) + if err != nil { + t.Fatalf("did not expect error %v from binToSlice for test %d", err, i) + } + + got, err := NewNALUnit(bits.NewBitReader(bytes.NewReader(inBytes))) + if err != test.err { + t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v\n", i, err, test.err) + } + + if !nalEqual(*got, test.want) { + t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, *got, test.want) + } + } +} + +func nalEqual(a, b NALUnit) bool { + aCopy := a + bCopy := b + for _, n := range [](*NALUnit){&aCopy, &bCopy} { + n.SVCExtension = nil + n.MVCExtension = nil + n.ThreeDAVCExtension = nil + } + + if !reflect.DeepEqual(aCopy, bCopy) { + return false + } + + if (a.SVCExtension == nil || b.SVCExtension == nil) && + (a.SVCExtension != b.SVCExtension) { + return false + } + + if (a.MVCExtension == nil || b.MVCExtension == nil) && + (a.MVCExtension != b.MVCExtension) { + return false + } + + if (a.ThreeDAVCExtension == nil || b.ThreeDAVCExtension == nil) && + (a.ThreeDAVCExtension != b.ThreeDAVCExtension) { + return false + } + + if !reflect.DeepEqual(a.SVCExtension, b.SVCExtension) || + !reflect.DeepEqual(a.MVCExtension, b.MVCExtension) || + !reflect.DeepEqual(a.ThreeDAVCExtension, b.ThreeDAVCExtension) { + return false + } + return true +} From 311c44f55ce105915dd1c16eb5b12b657b6c761e Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 1 Aug 2019 02:37:34 +0930 Subject: [PATCH 6/8] codec/h264/h264dec: fixed problem with test --- codec/h264/h264dec/nalunit.go | 3 +-- codec/h264/h264dec/nalunit_test.go | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/codec/h264/h264dec/nalunit.go b/codec/h264/h264dec/nalunit.go index dd0248a1..e8ae3e3f 100644 --- a/codec/h264/h264dec/nalunit.go +++ b/codec/h264/h264dec/nalunit.go @@ -260,14 +260,13 @@ func NewNALUnit(br *bits.BitReader) (*NALUnit, error) { } for moreRBSPData(br) { - fmt.Println("here") next3Bytes, err := br.PeekBits(24) // If PeekBits cannot get 3 bytes, but there still might be 2 bytes left in // the source, we will get an io.EOF; we wish to ignore this and continue. // The call to moreRBSPData will determine when we have reached the end of // the NAL unit. - if err != nil && errors.Cause(err) != io.EOF { + if err != nil && errors.Cause(err) != io.ErrUnexpectedEOF { return nil, errors.Wrap(err, "could not Peek next 3 bytes") } diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go index 419c6c5f..0099b8ba 100644 --- a/codec/h264/h264dec/nalunit_test.go +++ b/codec/h264/h264dec/nalunit_test.go @@ -173,7 +173,7 @@ func TestNewNALUnit(t *testing.T) { "0000 0010" + "0000 0100" + "0000 1000" + - "1000 0000", + "1000 0000", // trailing bits want: NALUnit{ ForbiddenZeroBit: 0, @@ -198,7 +198,6 @@ func TestNewNALUnit(t *testing.T) { 0x02, 0x04, 0x08, - 0x80, }, }, }, @@ -221,6 +220,7 @@ func TestNewNALUnit(t *testing.T) { } } +// nalEqual returns true if two NALUnits are equal. func nalEqual(a, b NALUnit) bool { aCopy := a bCopy := b From 344d37cd29169e9ddea77a644118f31f462e125e Mon Sep 17 00:00:00 2001 From: Saxon Date: Thu, 1 Aug 2019 02:41:20 +0930 Subject: [PATCH 7/8] codec/h264/h264dec/nalunit_test.go: added file header --- codec/h264/h264dec/nalunit_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go index 0099b8ba..d9925fdc 100644 --- a/codec/h264/h264dec/nalunit_test.go +++ b/codec/h264/h264dec/nalunit_test.go @@ -1,3 +1,10 @@ +/* +DESCRIPTION + nalunit_test.go provides testing for NAL unit parsing utilities in nalunit.go. + +AUTHORS + Saxon Nelson-Milton , The Australian Ocean Laboratory (AusOcean) +*/ package h264dec import ( From dab94f6ae2cfb06128540b54f26325a6f96bdaf7 Mon Sep 17 00:00:00 2001 From: Saxon Date: Tue, 20 Aug 2019 11:39:12 +0930 Subject: [PATCH 8/8] codec/h264/h264dec: addressing PR feedback. Updated comment. Put a space between file header and package declaration. Not dereferencing things --- codec/h264/h264dec/nalunit.go | 6 +-- codec/h264/h264dec/nalunit_test.go | 63 +++++++++++++++++++----------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/codec/h264/h264dec/nalunit.go b/codec/h264/h264dec/nalunit.go index e8ae3e3f..9e3360ed 100644 --- a/codec/h264/h264dec/nalunit.go +++ b/codec/h264/h264dec/nalunit.go @@ -263,9 +263,9 @@ func NewNALUnit(br *bits.BitReader) (*NALUnit, error) { next3Bytes, err := br.PeekBits(24) // If PeekBits cannot get 3 bytes, but there still might be 2 bytes left in - // the source, we will get an io.EOF; we wish to ignore this and continue. - // The call to moreRBSPData will determine when we have reached the end of - // the NAL unit. + // the source, we will get an io.ErrUnexpectedEOF; we wish to ignore this + // and continue. The call to moreRBSPData will determine when we have + // reached the end of the NAL unit. if err != nil && errors.Cause(err) != io.ErrUnexpectedEOF { return nil, errors.Wrap(err, "could not Peek next 3 bytes") } diff --git a/codec/h264/h264dec/nalunit_test.go b/codec/h264/h264dec/nalunit_test.go index d9925fdc..afa2cdf1 100644 --- a/codec/h264/h264dec/nalunit_test.go +++ b/codec/h264/h264dec/nalunit_test.go @@ -1,10 +1,27 @@ /* DESCRIPTION - nalunit_test.go provides testing for NAL unit parsing utilities in nalunit.go. + nalunit_test.go provides testing for functionality in nalunit.go. AUTHORS - Saxon Nelson-Milton , The Australian Ocean Laboratory (AusOcean) + Saxon A. Nelson-Milton + +LICENSE + Copyright (C) 2017-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 ( @@ -18,7 +35,7 @@ import ( func TestNewMVCExtension(t *testing.T) { tests := []struct { in string - want MVCExtension + want *MVCExtension err error }{ { @@ -30,7 +47,7 @@ func TestNewMVCExtension(t *testing.T) { "0" + // u(1) inter_view_flag = false "1" + // u(1) reserved_one_bit = 1 "0", // Some padding - want: MVCExtension{ + want: &MVCExtension{ NonIdrFlag: false, PriorityID: 2, ViewID: 24, @@ -53,7 +70,7 @@ func TestNewMVCExtension(t *testing.T) { t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v\n", i, err, test.err) } - if !reflect.DeepEqual(*got, test.want) { + 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) } } @@ -62,7 +79,7 @@ func TestNewMVCExtension(t *testing.T) { func TestNewThreeDAVCExtension(t *testing.T) { tests := []struct { in string - want ThreeDAVCExtension + want *ThreeDAVCExtension err error }{ { @@ -73,7 +90,7 @@ func TestNewThreeDAVCExtension(t *testing.T) { "1" + // u(1) anchor_pic_flag = true "1" + // u(1) inter_view_flag = true "000", // Some padding - want: ThreeDAVCExtension{ + want: &ThreeDAVCExtension{ ViewIdx: 16, DepthFlag: true, NonIdrFlag: false, @@ -95,7 +112,7 @@ func TestNewThreeDAVCExtension(t *testing.T) { t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v\n", i, err, test.err) } - if !reflect.DeepEqual(*got, test.want) { + 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) } } @@ -104,7 +121,7 @@ func TestNewThreeDAVCExtension(t *testing.T) { func TestSVCExtension(t *testing.T) { tests := []struct { in string - want SVCExtension + want *SVCExtension err error }{ { @@ -119,7 +136,7 @@ func TestSVCExtension(t *testing.T) { "0" + // u(1) output_flag = false "11" + // ReservedThree2Bits "0", // padding - want: SVCExtension{ + want: &SVCExtension{ IdrFlag: false, PriorityID: 32, NoInterLayerPredFlag: false, @@ -145,7 +162,7 @@ func TestSVCExtension(t *testing.T) { t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v\n", i, err, test.err) } - if !reflect.DeepEqual(*got, test.want) { + 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) } } @@ -154,7 +171,7 @@ func TestSVCExtension(t *testing.T) { func TestNewNALUnit(t *testing.T) { tests := []struct { in string - want NALUnit + want *NALUnit err error }{ { @@ -182,7 +199,7 @@ func TestNewNALUnit(t *testing.T) { "0000 1000" + "1000 0000", // trailing bits - want: NALUnit{ + want: &NALUnit{ ForbiddenZeroBit: 0, RefIdc: 1, Type: 14, @@ -221,21 +238,16 @@ func TestNewNALUnit(t *testing.T) { t.Errorf("did not get expected error for test %d\nGot: %v\nWant: %v\n", i, err, test.err) } - if !nalEqual(*got, test.want) { + if !nalEqual(got, test.want) { t.Errorf("did not get expected result for test %d\nGot: %v\nWant: %v\n", i, *got, test.want) } } } // nalEqual returns true if two NALUnits are equal. -func nalEqual(a, b NALUnit) bool { - aCopy := a - bCopy := b - for _, n := range [](*NALUnit){&aCopy, &bCopy} { - n.SVCExtension = nil - n.MVCExtension = nil - n.ThreeDAVCExtension = nil - } +func nalEqual(a, b *NALUnit) bool { + aCopy := nalWithoutExtensions(*a) + bCopy := nalWithoutExtensions(*b) if !reflect.DeepEqual(aCopy, bCopy) { return false @@ -263,3 +275,10 @@ func nalEqual(a, b NALUnit) bool { } return true } + +func nalWithoutExtensions(n NALUnit) NALUnit { + n.SVCExtension = nil + n.MVCExtension = nil + n.ThreeDAVCExtension = nil + return n +}