OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/at_exit.h" | 5 #include "base/at_exit.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
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/files/memory_mapped_file.h" | 9 #include "base/files/memory_mapped_file.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
12 #include "base/process/process.h" | 12 #include "base/process/process.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" |
16 #include "content/common/gpu/media/video_accelerator_unittest_helpers.h" | 17 #include "content/common/gpu/media/video_accelerator_unittest_helpers.h" |
17 #include "media/base/bind_to_current_loop.h" | 18 #include "media/base/bind_to_current_loop.h" |
18 #include "media/base/bitstream_buffer.h" | 19 #include "media/base/bitstream_buffer.h" |
19 #include "media/base/test_data_util.h" | 20 #include "media/base/test_data_util.h" |
20 #include "media/filters/h264_parser.h" | 21 #include "media/filters/h264_parser.h" |
21 #include "media/video/video_encode_accelerator.h" | 22 #include "media/video/video_encode_accelerator.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 #if defined(USE_X11) | |
25 #include "ui/gfx/x/x11_types.h" | |
26 #endif | |
27 | |
28 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | |
29 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" | |
30 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) | |
31 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" | |
32 #else | |
33 #error The VideoEncodeAcceleratorUnittest is not supported on this platform. | |
34 #endif | |
35 | |
36 using media::VideoEncodeAccelerator; | 25 using media::VideoEncodeAccelerator; |
37 | 26 |
38 namespace content { | 27 namespace content { |
39 namespace { | 28 namespace { |
40 | 29 |
41 const media::VideoFrame::Format kInputFormat = media::VideoFrame::I420; | 30 const media::VideoFrame::Format kInputFormat = media::VideoFrame::I420; |
42 | 31 |
43 // Arbitrarily chosen to add some depth to the pipeline. | 32 // Arbitrarily chosen to add some depth to the pipeline. |
44 const unsigned int kNumOutputBuffers = 4; | 33 const unsigned int kNumOutputBuffers = 4; |
45 const unsigned int kNumExtraInputFrames = 4; | 34 const unsigned int kNumExtraInputFrames = 4; |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 }; | 237 }; |
249 | 238 |
250 class H264Validator : public StreamValidator { | 239 class H264Validator : public StreamValidator { |
251 public: | 240 public: |
252 explicit H264Validator(const FrameFoundCallback& frame_cb) | 241 explicit H264Validator(const FrameFoundCallback& frame_cb) |
253 : StreamValidator(frame_cb), | 242 : StreamValidator(frame_cb), |
254 seen_sps_(false), | 243 seen_sps_(false), |
255 seen_pps_(false), | 244 seen_pps_(false), |
256 seen_idr_(false) {} | 245 seen_idr_(false) {} |
257 | 246 |
258 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE; | 247 void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE; |
259 | 248 |
260 private: | 249 private: |
261 // Set to true when encoder provides us with the corresponding NALU type. | 250 // Set to true when encoder provides us with the corresponding NALU type. |
262 bool seen_sps_; | 251 bool seen_sps_; |
263 bool seen_pps_; | 252 bool seen_pps_; |
264 bool seen_idr_; | 253 bool seen_idr_; |
265 | |
266 media::H264Parser h264_parser_; | |
267 }; | 254 }; |
268 | 255 |
269 void H264Validator::ProcessStreamBuffer(const uint8* stream, size_t size) { | 256 void H264Validator::ProcessStreamBuffer(const uint8* stream, size_t size) { |
270 h264_parser_.SetStream(stream, size); | 257 media::H264Parser h264_parser; |
| 258 h264_parser.SetStream(stream, size); |
271 | 259 |
272 while (1) { | 260 while (1) { |
273 media::H264NALU nalu; | 261 media::H264NALU nalu; |
274 media::H264Parser::Result result; | 262 media::H264Parser::Result result; |
275 | 263 |
276 result = h264_parser_.AdvanceToNextNALU(&nalu); | 264 result = h264_parser.AdvanceToNextNALU(&nalu); |
277 if (result == media::H264Parser::kEOStream) | 265 if (result == media::H264Parser::kEOStream) |
278 break; | 266 break; |
279 | 267 |
280 ASSERT_EQ(media::H264Parser::kOk, result); | 268 ASSERT_EQ(result, media::H264Parser::kOk); |
281 | 269 |
282 bool keyframe = false; | 270 bool keyframe = false; |
283 | 271 |
284 switch (nalu.nal_unit_type) { | 272 switch (nalu.nal_unit_type) { |
285 case media::H264NALU::kIDRSlice: | 273 case media::H264NALU::kIDRSlice: |
286 ASSERT_TRUE(seen_sps_); | 274 ASSERT_TRUE(seen_sps_); |
287 ASSERT_TRUE(seen_pps_); | 275 ASSERT_TRUE(seen_pps_); |
288 seen_idr_ = true; | 276 seen_idr_ = keyframe = true; |
289 // fallthrough | 277 // fallthrough |
290 case media::H264NALU::kNonIDRSlice: { | 278 case media::H264NALU::kNonIDRSlice: |
291 ASSERT_TRUE(seen_idr_); | 279 ASSERT_TRUE(seen_idr_); |
292 | |
293 media::H264SliceHeader shdr; | |
294 ASSERT_EQ(media::H264Parser::kOk, | |
295 h264_parser_.ParseSliceHeader(nalu, &shdr)); | |
296 keyframe = shdr.IsISlice() || shdr.IsSISlice(); | |
297 | |
298 if (!frame_cb_.Run(keyframe)) | 280 if (!frame_cb_.Run(keyframe)) |
299 return; | 281 return; |
300 break; | 282 break; |
301 } | |
302 | 283 |
303 case media::H264NALU::kSPS: { | 284 case media::H264NALU::kSPS: |
304 int sps_id; | |
305 ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParseSPS(&sps_id)); | |
306 seen_sps_ = true; | 285 seen_sps_ = true; |
307 break; | 286 break; |
308 } | |
309 | 287 |
310 case media::H264NALU::kPPS: { | 288 case media::H264NALU::kPPS: |
311 ASSERT_TRUE(seen_sps_); | 289 ASSERT_TRUE(seen_sps_); |
312 int pps_id; | |
313 ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParsePPS(&pps_id)); | |
314 seen_pps_ = true; | 290 seen_pps_ = true; |
315 break; | 291 break; |
316 } | |
317 | 292 |
318 default: | 293 default: |
319 break; | 294 break; |
320 } | 295 } |
321 } | 296 } |
322 } | 297 } |
323 | 298 |
324 class VP8Validator : public StreamValidator { | 299 class VP8Validator : public StreamValidator { |
325 public: | 300 public: |
326 explicit VP8Validator(const FrameFoundCallback& frame_cb) | 301 explicit VP8Validator(const FrameFoundCallback& frame_cb) |
327 : StreamValidator(frame_cb), | 302 : StreamValidator(frame_cb), |
328 seen_keyframe_(false) {} | 303 seen_keyframe_(false) {} |
329 | 304 |
330 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE; | 305 void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE; |
331 | 306 |
332 private: | 307 private: |
333 // Have we already got a keyframe in the stream? | 308 // Have we already got a keyframe in the stream? |
334 bool seen_keyframe_; | 309 bool seen_keyframe_; |
335 }; | 310 }; |
336 | 311 |
337 void VP8Validator::ProcessStreamBuffer(const uint8* stream, size_t size) { | 312 void VP8Validator::ProcessStreamBuffer(const uint8* stream, size_t size) { |
338 bool keyframe = !(stream[0] & 0x01); | 313 bool keyframe = !(stream[0] & 0x01); |
339 if (keyframe) | 314 if (keyframe) |
340 seen_keyframe_ = true; | 315 seen_keyframe_ = true; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 bool force_bitrate, | 350 bool force_bitrate, |
376 bool test_perf); | 351 bool test_perf); |
377 virtual ~VEAClient(); | 352 virtual ~VEAClient(); |
378 void CreateEncoder(); | 353 void CreateEncoder(); |
379 void DestroyEncoder(); | 354 void DestroyEncoder(); |
380 | 355 |
381 // Return the number of encoded frames per second. | 356 // Return the number of encoded frames per second. |
382 double frames_per_second(); | 357 double frames_per_second(); |
383 | 358 |
384 // VideoDecodeAccelerator::Client implementation. | 359 // VideoDecodeAccelerator::Client implementation. |
385 virtual void RequireBitstreamBuffers(unsigned int input_count, | 360 void RequireBitstreamBuffers(unsigned int input_count, |
386 const gfx::Size& input_coded_size, | 361 const gfx::Size& input_coded_size, |
387 size_t output_buffer_size) OVERRIDE; | 362 size_t output_buffer_size) OVERRIDE; |
388 virtual void BitstreamBufferReady(int32 bitstream_buffer_id, | 363 void BitstreamBufferReady(int32 bitstream_buffer_id, |
389 size_t payload_size, | 364 size_t payload_size, |
390 bool key_frame) OVERRIDE; | 365 bool key_frame) OVERRIDE; |
391 virtual void NotifyError(VideoEncodeAccelerator::Error error) OVERRIDE; | 366 void NotifyError(VideoEncodeAccelerator::Error error) OVERRIDE; |
392 | 367 |
393 private: | 368 private: |
394 bool has_encoder() { return encoder_.get(); } | 369 bool has_encoder() { return encoder_.get(); } |
395 | 370 |
396 void SetState(ClientState new_state); | 371 void SetState(ClientState new_state); |
397 | 372 |
398 // Set current stream parameters to given |bitrate| at |framerate|. | 373 // Set current stream parameters to given |bitrate| at |framerate|. |
399 void SetStreamParameters(unsigned int bitrate, unsigned int framerate); | 374 void SetStreamParameters(unsigned int bitrate, unsigned int framerate); |
400 | 375 |
401 // Called when encoder is done with a VideoFrame. | 376 // Called when encoder is done with a VideoFrame. |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 | 552 |
578 thread_checker_.DetachFromThread(); | 553 thread_checker_.DetachFromThread(); |
579 } | 554 } |
580 | 555 |
581 VEAClient::~VEAClient() { CHECK(!has_encoder()); } | 556 VEAClient::~VEAClient() { CHECK(!has_encoder()); } |
582 | 557 |
583 void VEAClient::CreateEncoder() { | 558 void VEAClient::CreateEncoder() { |
584 DCHECK(thread_checker_.CalledOnValidThread()); | 559 DCHECK(thread_checker_.CalledOnValidThread()); |
585 CHECK(!has_encoder()); | 560 CHECK(!has_encoder()); |
586 | 561 |
587 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | |
588 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); | 562 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); |
589 encoder_.reset(new V4L2VideoEncodeAccelerator(device.Pass())); | 563 encoder_.reset(new V4L2VideoEncodeAccelerator(device.Pass())); |
590 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) | |
591 encoder_.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay())); | |
592 #endif | |
593 | |
594 SetState(CS_ENCODER_SET); | 564 SetState(CS_ENCODER_SET); |
595 | 565 |
596 DVLOG(1) << "Profile: " << test_stream_.requested_profile | 566 DVLOG(1) << "Profile: " << test_stream_.requested_profile |
597 << ", initial bitrate: " << test_stream_.requested_bitrate; | 567 << ", initial bitrate: " << test_stream_.requested_bitrate; |
598 if (!encoder_->Initialize(kInputFormat, | 568 if (!encoder_->Initialize(kInputFormat, |
599 test_stream_.size, | 569 test_stream_.size, |
600 test_stream_.requested_profile, | 570 test_stream_.requested_profile, |
601 test_stream_.requested_bitrate, | 571 test_stream_.requested_bitrate, |
602 this)) { | 572 this)) { |
603 DLOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed"; | 573 DLOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed"; |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 test_stream_data->assign(it->second.c_str()); | 1024 test_stream_data->assign(it->second.c_str()); |
1055 continue; | 1025 continue; |
1056 } | 1026 } |
1057 if (it->first == "v" || it->first == "vmodule") | 1027 if (it->first == "v" || it->first == "vmodule") |
1058 continue; | 1028 continue; |
1059 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; | 1029 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; |
1060 } | 1030 } |
1061 | 1031 |
1062 return RUN_ALL_TESTS(); | 1032 return RUN_ALL_TESTS(); |
1063 } | 1033 } |
OLD | NEW |