OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "content/common/gpu/media/h264_parser.h" | 11 #include "content/common/gpu/media/h264_parser.h" |
12 | 12 |
13 using content::H264Parser; | 13 using content::H264Parser; |
14 using content::H264NALU; | 14 using content::H264NALU; |
15 | 15 |
16 const FilePath::CharType* test_stream_filename = | 16 const FilePath::CharType* test_stream_filename = |
17 FILE_PATH_LITERAL("content/common/gpu/testdata/test-25fps.h264"); | 17 FILE_PATH_LITERAL("content/common/gpu/testdata/test-25fps.h264"); |
18 // Number of NALUs in the stream to be parsed. | 18 // Number of NALUs in the stream to be parsed. |
19 int num_nalus = 759; | 19 int num_nalus = 759; |
20 | 20 |
21 TEST(H264ParserTest, StreamFileParsing) { | 21 TEST(H264ParserTest, StreamFileParsing) { |
Ami GONE FROM CHROMIUM
2012/07/30 23:32:32
Is there any reason for this to still exist??
xiaomings
2012/07/30 23:59:34
I will keep the original one and remove the one to
| |
22 FilePath fp(test_stream_filename); | 22 FilePath fp(test_stream_filename); |
23 file_util::MemoryMappedFile stream; | 23 file_util::MemoryMappedFile stream; |
24 CHECK(stream.Initialize(fp)) << "Couldn't open stream file: " | 24 CHECK(stream.Initialize(fp)) << "Couldn't open stream file: " |
25 << test_stream_filename; | 25 << test_stream_filename; |
26 DVLOG(1) << "Parsing file: " << test_stream_filename; | 26 DVLOG(1) << "Parsing file: " << test_stream_filename; |
27 | 27 |
28 H264Parser parser; | 28 H264Parser parser; |
29 parser.SetStream(stream.data(), stream.length()); | 29 parser.SetStream(stream.data(), stream.length()); |
30 | 30 |
31 // Parse until the end of stream/unsupported stream/error in stream is found. | 31 // Parse until the end of stream/unsupported stream/error in stream is found. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 break; | 65 break; |
66 | 66 |
67 default: | 67 default: |
68 // Skip unsupported NALU. | 68 // Skip unsupported NALU. |
69 DVLOG(4) << "Skipping unsupported NALU"; | 69 DVLOG(4) << "Skipping unsupported NALU"; |
70 break; | 70 break; |
71 } | 71 } |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 TEST(H264ParserTest, MultiStreamFileParsing) { | |
76 const FilePath::CharType* test_stream_dir = | |
77 FILE_PATH_LITERAL("content/common/gpu/testdata/"); | |
78 file_util::FileEnumerator files(FilePath(test_stream_dir), false, | |
79 file_util::FileEnumerator::FILES, | |
80 FILE_PATH_LITERAL("*.h264")); | |
81 | |
82 for (FilePath file = files.Next(); !file.empty(); file = files.Next()) { | |
83 file_util::MemoryMappedFile stream; | |
84 CHECK(stream.Initialize(file)) << "Couldn't open stream file: " | |
85 << file.value(); | |
86 DVLOG(1) << "Parsing file: " << file.value(); | |
87 | |
88 H264Parser parser; | |
89 parser.SetStream(stream.data(), stream.length()); | |
90 | |
91 // Parse until the end of stream/unsupported stream/error in stream is | |
92 // found. | |
93 while (true) { | |
94 content::H264SliceHeader shdr; | |
95 content::H264SEIMessage sei_msg; | |
96 H264NALU nalu; | |
97 H264Parser::Result res = parser.AdvanceToNextNALU(&nalu); | |
98 if (res == H264Parser::kEOStream) | |
99 break; | |
100 ASSERT_EQ(res, H264Parser::kOk); | |
101 | |
102 int id; | |
103 switch (nalu.nal_unit_type) { | |
104 case H264NALU::kIDRSlice: | |
105 case H264NALU::kNonIDRSlice: | |
106 ASSERT_EQ(parser.ParseSliceHeader(nalu, &shdr), H264Parser::kOk); | |
107 break; | |
108 | |
109 case H264NALU::kSPS: | |
110 ASSERT_EQ(parser.ParseSPS(&id), H264Parser::kOk); | |
111 break; | |
112 | |
113 case H264NALU::kPPS: | |
114 ASSERT_EQ(parser.ParsePPS(&id), H264Parser::kOk); | |
115 break; | |
116 | |
117 case H264NALU::kSEIMessage: | |
118 ASSERT_EQ(parser.ParseSEI(&sei_msg), H264Parser::kOk); | |
119 break; | |
120 | |
121 default: | |
122 // Skip unsupported NALU. | |
123 DVLOG(4) << "Skipping unsupported NALU"; | |
124 break; | |
125 } | |
126 } | |
127 } | |
128 } | |
129 | |
130 class H264BitReaderTest : public ::testing::Test { | |
131 public: | |
132 H264BitReaderTest() {} | |
133 ~H264BitReaderTest() {} | |
Ami GONE FROM CHROMIUM
2012/07/30 23:32:32
These two aren't adding value.
In fact I don't ac
xiaomings
2012/07/30 23:59:34
Removed.
On 2012/07/30 23:32:32, Ami Fischman wro
| |
134 | |
135 bool Initialize(const uint8* data, off_t size) { | |
136 return reader_.Initialize(data, size); | |
137 } | |
138 | |
139 bool ReadBits(int num_bits, int *out) { | |
140 return reader_.ReadBits(num_bits, out); | |
141 } | |
142 | |
143 off_t NumBitsLeft() { | |
144 return reader_.NumBitsLeft(); | |
145 } | |
146 | |
147 bool HasMoreRBSPData() { | |
148 return reader_.HasMoreRBSPData(); | |
149 } | |
150 | |
151 private: | |
152 H264Parser::H264BitReader reader_; | |
153 }; | |
154 | |
155 TEST_F(H264BitReaderTest, ReadStreamWithoutEscapeAndTrailingZeroBytes) { | |
156 const unsigned char rbsp[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xa0}; | |
157 int dummy = 0; | |
158 | |
159 EXPECT_TRUE(Initialize(rbsp, sizeof(rbsp))); | |
160 | |
161 EXPECT_TRUE(ReadBits(1, &dummy)); | |
162 EXPECT_EQ(dummy, 0x00); | |
163 EXPECT_EQ(NumBitsLeft(), 47); | |
164 EXPECT_TRUE(HasMoreRBSPData()); | |
165 | |
166 EXPECT_TRUE(ReadBits(8, &dummy)); | |
167 EXPECT_EQ(dummy, 0x02); | |
168 EXPECT_EQ(NumBitsLeft(), 39); | |
169 EXPECT_TRUE(HasMoreRBSPData()); | |
170 | |
171 EXPECT_TRUE(ReadBits(31, &dummy)); | |
172 EXPECT_EQ(dummy, 0x23456789); | |
173 EXPECT_EQ(NumBitsLeft(), 8); | |
174 EXPECT_TRUE(HasMoreRBSPData()); | |
175 | |
176 EXPECT_TRUE(ReadBits(1, &dummy)); | |
177 EXPECT_EQ(dummy, 1); | |
178 EXPECT_EQ(NumBitsLeft(), 7); | |
179 EXPECT_TRUE(HasMoreRBSPData()); | |
180 | |
181 EXPECT_TRUE(ReadBits(1, &dummy)); | |
182 EXPECT_EQ(dummy, 0); | |
183 EXPECT_EQ(NumBitsLeft(), 6); | |
184 EXPECT_FALSE(HasMoreRBSPData()); | |
185 } | |
186 | |
187 TEST_F(H264BitReaderTest, EmptyStream) { | |
188 const unsigned char rbsp[] = {0x80, 0x00, 0x00}; | |
189 | |
190 EXPECT_FALSE(Initialize(rbsp, 0)); | |
191 EXPECT_FALSE(Initialize(rbsp, 1)); | |
192 EXPECT_FALSE(Initialize(rbsp, sizeof(rbsp))); | |
193 } | |
194 | |
195 TEST_F(H264BitReaderTest, SingleByteStream) { | |
196 const unsigned char rbsp[] = {0x18}; | |
197 int dummy = 0; | |
198 | |
199 EXPECT_TRUE(Initialize(rbsp, sizeof(rbsp))); | |
200 EXPECT_EQ(NumBitsLeft(), 8); | |
201 EXPECT_TRUE(HasMoreRBSPData()); | |
202 | |
203 EXPECT_TRUE(ReadBits(4, &dummy)); | |
204 EXPECT_EQ(dummy, 0x01); | |
205 EXPECT_EQ(NumBitsLeft(), 4); | |
206 EXPECT_FALSE(HasMoreRBSPData()); | |
207 } | |
208 | |
209 TEST_F(H264BitReaderTest, StopBitOccupyFullByte) { | |
210 const unsigned char rbsp[] = {0xab, 0x80}; | |
211 int dummy = 0; | |
212 | |
213 EXPECT_TRUE(Initialize(rbsp, sizeof(rbsp))); | |
214 EXPECT_EQ(NumBitsLeft(), 16); | |
215 EXPECT_TRUE(HasMoreRBSPData()); | |
216 | |
217 EXPECT_TRUE(ReadBits(8, &dummy)); | |
218 EXPECT_EQ(dummy, 0xab); | |
219 EXPECT_EQ(NumBitsLeft(), 8); | |
220 EXPECT_FALSE(HasMoreRBSPData()); | |
221 } | |
222 | |
223 TEST_F(H264BitReaderTest, ReadFailure) { | |
224 const unsigned char rbsp[] = {0x18}; | |
225 int dummy = 0; | |
226 | |
227 EXPECT_TRUE(Initialize(rbsp, 1)); | |
228 EXPECT_FALSE(ReadBits(5, &dummy)); | |
229 } | |
230 | |
231 TEST_F(H264BitReaderTest, MalformedStream) { | |
232 const unsigned char rbsp[] = {0x00, 0x00, 0x03, 0x00, 0x00}; | |
233 | |
234 EXPECT_FALSE(Initialize(rbsp, 1)); | |
235 EXPECT_FALSE(Initialize(rbsp, sizeof(rbsp))); | |
236 } | |
237 | |
238 TEST_F(H264BitReaderTest, EscapeSequence) { | |
239 const unsigned char rbsp[] = | |
240 {0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x03}; | |
241 int dummy = 0; | |
242 | |
243 EXPECT_TRUE(Initialize(rbsp, sizeof(rbsp))); | |
244 | |
245 EXPECT_TRUE(ReadBits(8, &dummy)); | |
246 EXPECT_EQ(dummy, 0x00); | |
247 EXPECT_EQ(NumBitsLeft(), 80); | |
248 EXPECT_TRUE(HasMoreRBSPData()); | |
249 | |
250 EXPECT_TRUE(ReadBits(24, &dummy)); | |
251 EXPECT_EQ(dummy, 0x00); | |
252 EXPECT_EQ(NumBitsLeft(), 48); | |
253 EXPECT_TRUE(HasMoreRBSPData()); | |
254 | |
255 EXPECT_TRUE(ReadBits(15, &dummy)); | |
256 EXPECT_EQ(dummy, 0x01); | |
257 EXPECT_EQ(NumBitsLeft(), 25); | |
258 EXPECT_FALSE(HasMoreRBSPData()); | |
259 } | |
260 | |
261 TEST_F(H264BitReaderTest, NonEscapeFollowedByStopBit) { | |
262 const unsigned char rbsp[] = {0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00}; | |
263 int dummy = 0; | |
264 | |
265 EXPECT_TRUE(Initialize(rbsp, sizeof(rbsp))); | |
266 EXPECT_TRUE(ReadBits(23, &dummy)); | |
267 EXPECT_EQ(dummy, 0x03); | |
268 EXPECT_EQ(NumBitsLeft(), 33); | |
269 EXPECT_FALSE(HasMoreRBSPData()); | |
270 } | |
271 | |
272 TEST_F(H264BitReaderTest, TrailingZero) { | |
273 const unsigned char rbsp[] = {0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00}; | |
274 int dummy = 0; | |
275 | |
276 EXPECT_TRUE(Initialize(rbsp, sizeof(rbsp))); | |
277 EXPECT_TRUE(ReadBits(8, &dummy)); | |
278 EXPECT_EQ(dummy, 0x01); | |
279 EXPECT_TRUE(HasMoreRBSPData()); | |
280 EXPECT_TRUE(ReadBits(15, &dummy)); | |
281 EXPECT_EQ(dummy, 0x01); | |
282 EXPECT_FALSE(HasMoreRBSPData()); | |
283 } | |
284 | |
75 int main(int argc, char **argv) { | 285 int main(int argc, char **argv) { |
76 ::testing::InitGoogleTest(&argc, argv); | 286 ::testing::InitGoogleTest(&argc, argv); |
77 CommandLine::Init(argc, argv); | 287 CommandLine::Init(argc, argv); |
78 | 288 |
79 const CommandLine::SwitchMap& switches = | 289 const CommandLine::SwitchMap& switches = |
80 CommandLine::ForCurrentProcess()->GetSwitches(); | 290 CommandLine::ForCurrentProcess()->GetSwitches(); |
81 for (CommandLine::SwitchMap::const_iterator it = switches.begin(); | 291 for (CommandLine::SwitchMap::const_iterator it = switches.begin(); |
82 it != switches.end(); ++it) { | 292 it != switches.end(); ++it) { |
83 if (it->first == "test_stream") { | 293 if (it->first == "test_stream") { |
84 test_stream_filename = it->second.c_str(); | 294 test_stream_filename = it->second.c_str(); |
85 } else if (it->first == "num_nalus") { | 295 } else if (it->first == "num_nalus") { |
86 CHECK(base::StringToInt(it->second, &num_nalus)); | 296 CHECK(base::StringToInt(it->second, &num_nalus)); |
87 } else { | 297 } else { |
88 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; | 298 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; |
89 } | 299 } |
90 } | 300 } |
91 | 301 |
92 return RUN_ALL_TESTS(); | 302 return RUN_ALL_TESTS(); |
93 } | 303 } |
94 | |
OLD | NEW |