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