github.com/bluenviron/mediacommon@v1.9.3/pkg/codecs/h264/annexb_test.go (about) 1 package h264 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 var casesAnnexB = []struct { 10 name string 11 encin []byte 12 encout []byte 13 dec [][]byte 14 }{ 15 { 16 "2 zeros", 17 []byte{ 18 0x00, 0x00, 0x01, 0xaa, 0xbb, 0x00, 0x00, 0x01, 19 0xcc, 0xdd, 0x00, 0x00, 0x01, 0xee, 0xff, 20 }, 21 []byte{ 22 0x00, 0x00, 0x00, 0x01, 0xaa, 0xbb, 23 0x00, 0x00, 0x00, 0x01, 0xcc, 0xdd, 24 0x00, 0x00, 0x00, 0x01, 0xee, 0xff, 25 }, 26 [][]byte{ 27 {0xaa, 0xbb}, 28 {0xcc, 0xdd}, 29 {0xee, 0xff}, 30 }, 31 }, 32 { 33 "3 zeros", 34 []byte{ 35 0x00, 0x00, 0x00, 0x01, 0xaa, 0xbb, 36 0x00, 0x00, 0x00, 0x01, 0xcc, 0xdd, 37 0x00, 0x00, 0x00, 0x01, 0xee, 0xff, 38 }, 39 []byte{ 40 0x00, 0x00, 0x00, 0x01, 0xaa, 0xbb, 41 0x00, 0x00, 0x00, 0x01, 0xcc, 0xdd, 42 0x00, 0x00, 0x00, 0x01, 0xee, 0xff, 43 }, 44 [][]byte{ 45 {0xaa, 0xbb}, 46 {0xcc, 0xdd}, 47 {0xee, 0xff}, 48 }, 49 }, 50 { 51 // used by Apple inside HLS test streams 52 "2 or 3 zeros", 53 []byte{ 54 0, 0, 0, 1, 9, 240, 55 0, 0, 0, 1, 39, 66, 224, 21, 169, 24, 60, 23, 252, 184, 3, 80, 96, 16, 107, 108, 43, 94, 247, 192, 64, 56 0, 0, 0, 1, 40, 222, 9, 200, 57 0, 0, 1, 6, 0, 7, 131, 236, 119, 0, 0, 0, 0, 1, 3, 0, 64, 128, 58 0, 0, 1, 6, 5, 17, 3, 135, 244, 78, 205, 10, 75, 220, 161, 148, 58, 195, 212, 155, 23, 31, 0, 128, 59 }, 60 []byte{ 61 0, 0, 0, 1, 9, 240, 62 0, 0, 0, 1, 39, 66, 224, 21, 169, 24, 60, 23, 252, 184, 3, 80, 96, 16, 107, 108, 43, 94, 247, 192, 64, 63 0, 0, 0, 1, 40, 222, 9, 200, 64 0, 0, 0, 1, 6, 0, 7, 131, 236, 119, 0, 0, 0, 0, 1, 3, 0, 64, 128, 65 0, 0, 0, 1, 6, 5, 17, 3, 135, 244, 78, 205, 10, 75, 220, 161, 148, 58, 195, 212, 155, 23, 31, 0, 128, 66 }, 67 [][]byte{ 68 {9, 240}, 69 {39, 66, 224, 21, 169, 24, 60, 23, 252, 184, 3, 80, 96, 16, 107, 108, 43, 94, 247, 192, 64}, 70 {40, 222, 9, 200}, 71 {6, 0, 7, 131, 236, 119, 0, 0, 0, 0, 1, 3, 0, 64, 128}, 72 {6, 5, 17, 3, 135, 244, 78, 205, 10, 75, 220, 161, 148, 58, 195, 212, 155, 23, 31, 0, 128}, 73 }, 74 }, 75 } 76 77 func TestAnnexBUnmarshal(t *testing.T) { 78 for _, ca := range casesAnnexB { 79 t.Run(ca.name, func(t *testing.T) { 80 dec, err := AnnexBUnmarshal(ca.encin) 81 require.NoError(t, err) 82 require.Equal(t, ca.dec, dec) 83 }) 84 } 85 } 86 87 func TestAnnexBMarshal(t *testing.T) { 88 for _, ca := range casesAnnexB { 89 t.Run(ca.name, func(t *testing.T) { 90 enc, err := AnnexBMarshal(ca.dec) 91 require.NoError(t, err) 92 require.Equal(t, ca.encout, enc) 93 }) 94 } 95 } 96 97 func BenchmarkAnnexBUnmarshal(b *testing.B) { 98 for i := 0; i < b.N; i++ { 99 AnnexBUnmarshal([]byte{ //nolint:errcheck 100 0x00, 0x00, 0x00, 0x01, 101 0x01, 0x02, 0x03, 0x04, 102 0x00, 0x00, 0x00, 0x01, 103 0x01, 0x02, 0x03, 0x04, 104 0x00, 0x00, 0x00, 0x01, 105 0x01, 0x02, 0x03, 0x04, 106 0x00, 0x00, 0x00, 0x01, 107 0x01, 0x02, 0x03, 0x04, 108 0x00, 0x00, 0x00, 0x01, 109 0x01, 0x02, 0x03, 0x04, 110 0x00, 0x00, 0x00, 0x01, 111 0x01, 0x02, 0x03, 0x04, 112 0x00, 0x00, 0x00, 0x01, 113 0x01, 0x02, 0x03, 0x04, 114 0x00, 0x00, 0x00, 0x01, 115 0x01, 0x02, 0x03, 0x04, 116 }) 117 } 118 } 119 120 func FuzzAnnexBUnmarshal(f *testing.F) { 121 f.Fuzz(func(_ *testing.T, b []byte) { 122 AnnexBUnmarshal(b) //nolint:errcheck 123 }) 124 }