OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 * | |
10 */ | |
11 | |
12 #include <memory> | |
13 | |
14 #include "webrtc/base/arraysize.h" | |
15 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h" | |
16 #include "webrtc/test/gtest.h" | |
17 | |
18 namespace webrtc { | |
19 | |
20 static const uint8_t NALU_TEST_DATA_0[] = {0xAA, 0xBB, 0xCC}; | |
21 static const uint8_t NALU_TEST_DATA_1[] = {0xDE, 0xAD, 0xBE, 0xEF}; | |
22 | |
23 TEST(H264VideoToolboxNaluTest, TestHasVideoFormatDescription) { | |
24 const uint8_t sps_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x27}; | |
25 EXPECT_TRUE(H264AnnexBBufferHasVideoFormatDescription(sps_buffer, | |
26 arraysize(sps_buffer))); | |
27 const uint8_t aud_sps_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x29, 0x10, | |
28 0x00, 0x00, 0x00, 0x01, 0x27, 0xFF}; | |
29 EXPECT_TRUE(H264AnnexBBufferHasVideoFormatDescription( | |
30 aud_sps_buffer, arraysize(aud_sps_buffer))); | |
31 const uint8_t other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x28}; | |
32 EXPECT_FALSE(H264AnnexBBufferHasVideoFormatDescription( | |
33 other_buffer, arraysize(other_buffer))); | |
34 const uint8_t aud_other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x29, | |
35 0x00, 0x00, 0x00, 0x01, 0x28}; | |
36 EXPECT_FALSE(H264AnnexBBufferHasVideoFormatDescription( | |
37 aud_other_buffer, arraysize(aud_other_buffer))); | |
38 } | |
39 | |
40 TEST(H264VideoToolboxNaluTest, TestCreateVideoFormatDescription) { | |
41 const uint8_t sps_pps_buffer[] = { | |
42 // SPS nalu. | |
43 0x00, 0x00, 0x00, 0x01, | |
44 0x27, 0x42, 0x00, 0x1E, 0xAB, 0x40, 0xF0, 0x28, 0xD3, 0x70, 0x20, 0x20, | |
45 0x20, 0x20, | |
46 // PPS nalu. | |
47 0x00, 0x00, 0x00, 0x01, | |
48 0x28, 0xCE, 0x3C, 0x30 | |
49 }; | |
50 CMVideoFormatDescriptionRef description = | |
51 CreateVideoFormatDescription(sps_pps_buffer, arraysize(sps_pps_buffer)); | |
52 EXPECT_TRUE(description); | |
53 if (description) { | |
54 CFRelease(description); | |
55 description = nullptr; | |
56 } | |
57 const uint8_t other_buffer[] = {0x00, 0x00, 0x00, 0x01, 0x28}; | |
58 EXPECT_FALSE(CreateVideoFormatDescription(other_buffer, | |
59 arraysize(other_buffer))); | |
60 } | |
61 | |
62 TEST(AnnexBBufferReaderTest, TestReadEmptyInput) { | |
63 const uint8_t annex_b_test_data[] = {0x00}; | |
64 AnnexBBufferReader reader(annex_b_test_data, 0); | |
65 const uint8_t* nalu = nullptr; | |
66 size_t nalu_length = 0; | |
67 EXPECT_EQ(0u, reader.BytesRemaining()); | |
68 EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length)); | |
69 EXPECT_EQ(nullptr, nalu); | |
70 EXPECT_EQ(0u, nalu_length); | |
71 } | |
72 | |
73 TEST(AnnexBBufferReaderTest, TestReadSingleNalu) { | |
74 const uint8_t annex_b_test_data[] = {0x00, 0x00, 0x00, 0x01, 0xAA}; | |
75 AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data)); | |
76 const uint8_t* nalu = nullptr; | |
77 size_t nalu_length = 0; | |
78 EXPECT_EQ(arraysize(annex_b_test_data), reader.BytesRemaining()); | |
79 EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length)); | |
80 EXPECT_EQ(annex_b_test_data + 4, nalu); | |
81 EXPECT_EQ(1u, nalu_length); | |
82 EXPECT_EQ(0u, reader.BytesRemaining()); | |
83 EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length)); | |
84 EXPECT_EQ(nullptr, nalu); | |
85 EXPECT_EQ(0u, nalu_length); | |
86 } | |
87 | |
88 TEST(AnnexBBufferReaderTest, TestReadSingleNalu3ByteHeader) { | |
89 const uint8_t annex_b_test_data[] = {0x00, 0x00, 0x01, 0xAA}; | |
90 AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data)); | |
91 const uint8_t* nalu = nullptr; | |
92 size_t nalu_length = 0; | |
93 EXPECT_EQ(arraysize(annex_b_test_data), reader.BytesRemaining()); | |
94 EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length)); | |
95 EXPECT_EQ(annex_b_test_data + 3, nalu); | |
96 EXPECT_EQ(1u, nalu_length); | |
97 EXPECT_EQ(0u, reader.BytesRemaining()); | |
98 EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length)); | |
99 EXPECT_EQ(nullptr, nalu); | |
100 EXPECT_EQ(0u, nalu_length); | |
101 } | |
102 | |
103 TEST(AnnexBBufferReaderTest, TestReadMissingNalu) { | |
104 // clang-format off | |
105 const uint8_t annex_b_test_data[] = {0x01, | |
106 0x00, 0x01, | |
107 0x00, 0x00, 0x00, 0xFF}; | |
108 // clang-format on | |
109 AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data)); | |
110 const uint8_t* nalu = nullptr; | |
111 size_t nalu_length = 0; | |
112 EXPECT_EQ(0u, reader.BytesRemaining()); | |
113 EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length)); | |
114 EXPECT_EQ(nullptr, nalu); | |
115 EXPECT_EQ(0u, nalu_length); | |
116 } | |
117 | |
118 TEST(AnnexBBufferReaderTest, TestReadMultipleNalus) { | |
119 // clang-format off | |
120 const uint8_t annex_b_test_data[] = {0x00, 0x00, 0x00, 0x01, 0xFF, | |
121 0x01, | |
122 0x00, 0x01, | |
123 0x00, 0x00, 0x00, 0xFF, | |
124 0x00, 0x00, 0x01, 0xAA, 0xBB}; | |
125 // clang-format on | |
126 AnnexBBufferReader reader(annex_b_test_data, arraysize(annex_b_test_data)); | |
127 const uint8_t* nalu = nullptr; | |
128 size_t nalu_length = 0; | |
129 EXPECT_EQ(arraysize(annex_b_test_data), reader.BytesRemaining()); | |
130 EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length)); | |
131 EXPECT_EQ(annex_b_test_data + 4, nalu); | |
132 EXPECT_EQ(8u, nalu_length); | |
133 EXPECT_EQ(6u, reader.BytesRemaining()); | |
134 EXPECT_TRUE(reader.ReadNalu(&nalu, &nalu_length)); | |
135 EXPECT_EQ(annex_b_test_data + 16, nalu); | |
136 EXPECT_EQ(2u, nalu_length); | |
137 EXPECT_EQ(0u, reader.BytesRemaining()); | |
138 EXPECT_FALSE(reader.ReadNalu(&nalu, &nalu_length)); | |
139 EXPECT_EQ(nullptr, nalu); | |
140 EXPECT_EQ(0u, nalu_length); | |
141 } | |
142 | |
143 TEST(AvccBufferWriterTest, TestEmptyOutputBuffer) { | |
144 const uint8_t expected_buffer[] = {0x00}; | |
145 const size_t buffer_size = 1; | |
146 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | |
147 memset(buffer.get(), 0, buffer_size); | |
148 AvccBufferWriter writer(buffer.get(), 0); | |
149 EXPECT_EQ(0u, writer.BytesRemaining()); | |
150 EXPECT_FALSE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0))); | |
151 EXPECT_EQ(0, | |
152 memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer))); | |
153 } | |
154 | |
155 TEST(AvccBufferWriterTest, TestWriteSingleNalu) { | |
156 const uint8_t expected_buffer[] = { | |
157 0x00, 0x00, 0x00, 0x03, 0xAA, 0xBB, 0xCC, | |
158 }; | |
159 const size_t buffer_size = arraysize(NALU_TEST_DATA_0) + 4; | |
160 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | |
161 AvccBufferWriter writer(buffer.get(), buffer_size); | |
162 EXPECT_EQ(buffer_size, writer.BytesRemaining()); | |
163 EXPECT_TRUE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0))); | |
164 EXPECT_EQ(0u, writer.BytesRemaining()); | |
165 EXPECT_FALSE(writer.WriteNalu(NALU_TEST_DATA_1, arraysize(NALU_TEST_DATA_1))); | |
166 EXPECT_EQ(0, | |
167 memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer))); | |
168 } | |
169 | |
170 TEST(AvccBufferWriterTest, TestWriteMultipleNalus) { | |
171 // clang-format off | |
172 const uint8_t expected_buffer[] = { | |
173 0x00, 0x00, 0x00, 0x03, 0xAA, 0xBB, 0xCC, | |
174 0x00, 0x00, 0x00, 0x04, 0xDE, 0xAD, 0xBE, 0xEF | |
175 }; | |
176 // clang-format on | |
177 const size_t buffer_size = | |
178 arraysize(NALU_TEST_DATA_0) + arraysize(NALU_TEST_DATA_1) + 8; | |
179 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | |
180 AvccBufferWriter writer(buffer.get(), buffer_size); | |
181 EXPECT_EQ(buffer_size, writer.BytesRemaining()); | |
182 EXPECT_TRUE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0))); | |
183 EXPECT_EQ(buffer_size - (arraysize(NALU_TEST_DATA_0) + 4), | |
184 writer.BytesRemaining()); | |
185 EXPECT_TRUE(writer.WriteNalu(NALU_TEST_DATA_1, arraysize(NALU_TEST_DATA_1))); | |
186 EXPECT_EQ(0u, writer.BytesRemaining()); | |
187 EXPECT_EQ(0, | |
188 memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer))); | |
189 } | |
190 | |
191 TEST(AvccBufferWriterTest, TestOverflow) { | |
192 const uint8_t expected_buffer[] = {0x00, 0x00, 0x00}; | |
193 const size_t buffer_size = arraysize(NALU_TEST_DATA_0); | |
194 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | |
195 memset(buffer.get(), 0, buffer_size); | |
196 AvccBufferWriter writer(buffer.get(), buffer_size); | |
197 EXPECT_EQ(buffer_size, writer.BytesRemaining()); | |
198 EXPECT_FALSE(writer.WriteNalu(NALU_TEST_DATA_0, arraysize(NALU_TEST_DATA_0))); | |
199 EXPECT_EQ(buffer_size, writer.BytesRemaining()); | |
200 EXPECT_EQ(0, | |
201 memcmp(expected_buffer, buffer.get(), arraysize(expected_buffer))); | |
202 } | |
203 | |
204 } // namespace webrtc | |
OLD | NEW |