Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / common / gpu / media / video_encode_accelerator_unittest.cc
blob9d1e3b69dc7c9edccf56b2fab6021bc0fb20f23b
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
3 // found in the LICENSE file.
5 #include "base/at_exit.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/memory_mapped_file.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/process/process.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/time/time.h"
16 #include "content/common/gpu/media/video_accelerator_unittest_helpers.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/bitstream_buffer.h"
19 #include "media/base/test_data_util.h"
20 #include "media/filters/h264_parser.h"
21 #include "media/video/video_encode_accelerator.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 #if defined(USE_X11)
25 #include "ui/gfx/x/x11_types.h"
26 #endif
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
36 using media::VideoEncodeAccelerator;
38 namespace content {
39 namespace {
41 const media::VideoFrame::Format kInputFormat = media::VideoFrame::I420;
43 // Arbitrarily chosen to add some depth to the pipeline.
44 const unsigned int kNumOutputBuffers = 4;
45 const unsigned int kNumExtraInputFrames = 4;
46 // Maximum delay between requesting a keyframe and receiving one, in frames.
47 // Arbitrarily chosen as a reasonable requirement.
48 const unsigned int kMaxKeyframeDelay = 4;
49 // Value to use as max frame number for keyframe detection.
50 const unsigned int kMaxFrameNum =
51 std::numeric_limits<unsigned int>::max() - kMaxKeyframeDelay;
52 // Default initial bitrate.
53 const uint32 kDefaultBitrate = 2000000;
54 // Default ratio of requested_subsequent_bitrate to initial_bitrate
55 // (see test parameters below) if one is not provided.
56 const double kDefaultSubsequentBitrateRatio = 2.0;
57 // Default initial framerate.
58 const uint32 kDefaultFramerate = 30;
59 // Default ratio of requested_subsequent_framerate to initial_framerate
60 // (see test parameters below) if one is not provided.
61 const double kDefaultSubsequentFramerateRatio = 0.1;
62 // Tolerance factor for how encoded bitrate can differ from requested bitrate.
63 const double kBitrateTolerance = 0.1;
64 // Minimum required FPS throughput for the basic performance test.
65 const uint32 kMinPerfFPS = 30;
66 // Minimum (arbitrary) number of frames required to enforce bitrate requirements
67 // over. Streams shorter than this may be too short to realistically require
68 // an encoder to be able to converge to the requested bitrate over.
69 // The input stream will be looped as many times as needed in bitrate tests
70 // to reach at least this number of frames before calculating final bitrate.
71 const unsigned int kMinFramesForBitrateTests = 300;
73 // The syntax of multiple test streams is:
74 // test-stream1;test-stream2;test-stream3
75 // The syntax of each test stream is:
76 // "in_filename:width:height:out_filename:requested_bitrate:requested_framerate
77 // :requested_subsequent_bitrate:requested_subsequent_framerate"
78 // - |in_filename| must be an I420 (YUV planar) raw stream
79 // (see http://www.fourcc.org/yuv.php#IYUV).
80 // - |width| and |height| are in pixels.
81 // - |profile| to encode into (values of media::VideoCodecProfile).
82 // - |out_filename| filename to save the encoded stream to (optional).
83 // Output stream is saved for the simple encode test only.
84 // Further parameters are optional (need to provide preceding positional
85 // parameters if a specific subsequent parameter is required):
86 // - |requested_bitrate| requested bitrate in bits per second.
87 // - |requested_framerate| requested initial framerate.
88 // - |requested_subsequent_bitrate| bitrate to switch to in the middle of the
89 // stream.
90 // - |requested_subsequent_framerate| framerate to switch to in the middle
91 // of the stream.
92 // Bitrate is only forced for tests that test bitrate.
93 const char* g_default_in_filename = "bear_320x192_40frames.yuv";
94 const char* g_default_in_parameters = ":320:192:1:out.h264:200000";
95 base::FilePath::StringType* g_test_stream_data;
97 struct TestStream {
98 TestStream()
99 : requested_bitrate(0),
100 requested_framerate(0),
101 requested_subsequent_bitrate(0),
102 requested_subsequent_framerate(0) {}
103 ~TestStream() {}
105 gfx::Size size;
106 base::MemoryMappedFile input_file;
107 media::VideoCodecProfile requested_profile;
108 std::string out_filename;
109 unsigned int requested_bitrate;
110 unsigned int requested_framerate;
111 unsigned int requested_subsequent_bitrate;
112 unsigned int requested_subsequent_framerate;
115 // Parse |data| into its constituent parts, set the various output fields
116 // accordingly, read in video stream, and store them to |test_streams|.
117 static void ParseAndReadTestStreamData(const base::FilePath::StringType& data,
118 ScopedVector<TestStream>* test_streams) {
119 // Split the string to individual test stream data.
120 std::vector<base::FilePath::StringType> test_streams_data;
121 base::SplitString(data, ';', &test_streams_data);
122 CHECK_GE(test_streams_data.size(), 1U) << data;
124 // Parse each test stream data and read the input file.
125 for (size_t index = 0; index < test_streams_data.size(); ++index) {
126 std::vector<base::FilePath::StringType> fields;
127 base::SplitString(test_streams_data[index], ':', &fields);
128 CHECK_GE(fields.size(), 4U) << data;
129 CHECK_LE(fields.size(), 9U) << data;
130 TestStream* test_stream = new TestStream();
132 base::FilePath::StringType filename = fields[0];
133 int width, height;
134 CHECK(base::StringToInt(fields[1], &width));
135 CHECK(base::StringToInt(fields[2], &height));
136 test_stream->size = gfx::Size(width, height);
137 CHECK(!test_stream->size.IsEmpty());
138 int profile;
139 CHECK(base::StringToInt(fields[3], &profile));
140 CHECK_GT(profile, media::VIDEO_CODEC_PROFILE_UNKNOWN);
141 CHECK_LE(profile, media::VIDEO_CODEC_PROFILE_MAX);
142 test_stream->requested_profile =
143 static_cast<media::VideoCodecProfile>(profile);
145 if (fields.size() >= 5 && !fields[4].empty())
146 test_stream->out_filename = fields[4];
148 if (fields.size() >= 6 && !fields[5].empty())
149 CHECK(base::StringToUint(fields[5], &test_stream->requested_bitrate));
151 if (fields.size() >= 7 && !fields[6].empty())
152 CHECK(base::StringToUint(fields[6], &test_stream->requested_framerate));
154 if (fields.size() >= 8 && !fields[7].empty()) {
155 CHECK(base::StringToUint(fields[7],
156 &test_stream->requested_subsequent_bitrate));
159 if (fields.size() >= 9 && !fields[8].empty()) {
160 CHECK(base::StringToUint(fields[8],
161 &test_stream->requested_subsequent_framerate));
164 CHECK(test_stream->input_file.Initialize(base::FilePath(filename)));
165 test_streams->push_back(test_stream);
169 // Set default parameters of |test_streams| and update the parameters according
170 // to |mid_stream_bitrate_switch| and |mid_stream_framerate_switch|.
171 static void UpdateTestStreamData(bool mid_stream_bitrate_switch,
172 bool mid_stream_framerate_switch,
173 ScopedVector<TestStream>* test_streams) {
174 for (size_t i = 0; i < test_streams->size(); i++) {
175 TestStream* test_stream = (*test_streams)[i];
176 // Use defaults for bitrate/framerate if they are not provided.
177 if (test_stream->requested_bitrate == 0)
178 test_stream->requested_bitrate = kDefaultBitrate;
180 if (test_stream->requested_framerate == 0)
181 test_stream->requested_framerate = kDefaultFramerate;
183 // If bitrate/framerate switch is requested, use the subsequent values if
184 // provided, or, if not, calculate them from their initial values using
185 // the default ratios.
186 // Otherwise, if a switch is not requested, keep the initial values.
187 if (mid_stream_bitrate_switch) {
188 if (test_stream->requested_subsequent_bitrate == 0) {
189 test_stream->requested_subsequent_bitrate =
190 test_stream->requested_bitrate * kDefaultSubsequentBitrateRatio;
192 } else {
193 test_stream->requested_subsequent_bitrate =
194 test_stream->requested_bitrate;
196 if (test_stream->requested_subsequent_bitrate == 0)
197 test_stream->requested_subsequent_bitrate = 1;
199 if (mid_stream_framerate_switch) {
200 if (test_stream->requested_subsequent_framerate == 0) {
201 test_stream->requested_subsequent_framerate =
202 test_stream->requested_framerate * kDefaultSubsequentFramerateRatio;
204 } else {
205 test_stream->requested_subsequent_framerate =
206 test_stream->requested_framerate;
208 if (test_stream->requested_subsequent_framerate == 0)
209 test_stream->requested_subsequent_framerate = 1;
213 enum ClientState {
214 CS_CREATED,
215 CS_ENCODER_SET,
216 CS_INITIALIZED,
217 CS_ENCODING,
218 CS_FINISHED,
219 CS_ERROR,
222 // Performs basic, codec-specific sanity checks on the stream buffers passed
223 // to ProcessStreamBuffer(): whether we've seen keyframes before non-keyframes,
224 // correct sequences of H.264 NALUs (SPS before PPS and before slices), etc.
225 // Calls given FrameFoundCallback when a complete frame is found while
226 // processing.
227 class StreamValidator {
228 public:
229 // To be called when a complete frame is found while processing a stream
230 // buffer, passing true if the frame is a keyframe. Returns false if we
231 // are not interested in more frames and further processing should be aborted.
232 typedef base::Callback<bool(bool)> FrameFoundCallback;
234 virtual ~StreamValidator() {}
236 // Provide a StreamValidator instance for the given |profile|.
237 static scoped_ptr<StreamValidator> Create(media::VideoCodecProfile profile,
238 const FrameFoundCallback& frame_cb);
240 // Process and verify contents of a bitstream buffer.
241 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) = 0;
243 protected:
244 explicit StreamValidator(const FrameFoundCallback& frame_cb)
245 : frame_cb_(frame_cb) {}
247 FrameFoundCallback frame_cb_;
250 class H264Validator : public StreamValidator {
251 public:
252 explicit H264Validator(const FrameFoundCallback& frame_cb)
253 : StreamValidator(frame_cb),
254 seen_sps_(false),
255 seen_pps_(false),
256 seen_idr_(false) {}
258 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE;
260 private:
261 // Set to true when encoder provides us with the corresponding NALU type.
262 bool seen_sps_;
263 bool seen_pps_;
264 bool seen_idr_;
266 media::H264Parser h264_parser_;
269 void H264Validator::ProcessStreamBuffer(const uint8* stream, size_t size) {
270 h264_parser_.SetStream(stream, size);
272 while (1) {
273 media::H264NALU nalu;
274 media::H264Parser::Result result;
276 result = h264_parser_.AdvanceToNextNALU(&nalu);
277 if (result == media::H264Parser::kEOStream)
278 break;
280 ASSERT_EQ(media::H264Parser::kOk, result);
282 bool keyframe = false;
284 switch (nalu.nal_unit_type) {
285 case media::H264NALU::kIDRSlice:
286 ASSERT_TRUE(seen_sps_);
287 ASSERT_TRUE(seen_pps_);
288 seen_idr_ = true;
289 // fallthrough
290 case media::H264NALU::kNonIDRSlice: {
291 ASSERT_TRUE(seen_idr_);
293 media::H264SliceHeader shdr;
294 ASSERT_EQ(media::H264Parser::kOk,
295 h264_parser_.ParseSliceHeader(nalu, &shdr));
296 keyframe = shdr.IsISlice() || shdr.IsSISlice();
298 if (!frame_cb_.Run(keyframe))
299 return;
300 break;
303 case media::H264NALU::kSPS: {
304 int sps_id;
305 ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParseSPS(&sps_id));
306 seen_sps_ = true;
307 break;
310 case media::H264NALU::kPPS: {
311 ASSERT_TRUE(seen_sps_);
312 int pps_id;
313 ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParsePPS(&pps_id));
314 seen_pps_ = true;
315 break;
318 default:
319 break;
324 class VP8Validator : public StreamValidator {
325 public:
326 explicit VP8Validator(const FrameFoundCallback& frame_cb)
327 : StreamValidator(frame_cb),
328 seen_keyframe_(false) {}
330 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE;
332 private:
333 // Have we already got a keyframe in the stream?
334 bool seen_keyframe_;
337 void VP8Validator::ProcessStreamBuffer(const uint8* stream, size_t size) {
338 bool keyframe = !(stream[0] & 0x01);
339 if (keyframe)
340 seen_keyframe_ = true;
342 EXPECT_TRUE(seen_keyframe_);
344 frame_cb_.Run(keyframe);
345 // TODO(posciak): We could be getting more frames in the buffer, but there is
346 // no simple way to detect this. We'd need to parse the frames and go through
347 // partition numbers/sizes. For now assume one frame per buffer.
350 // static
351 scoped_ptr<StreamValidator> StreamValidator::Create(
352 media::VideoCodecProfile profile,
353 const FrameFoundCallback& frame_cb) {
354 scoped_ptr<StreamValidator> validator;
356 if (profile >= media::H264PROFILE_MIN &&
357 profile <= media::H264PROFILE_MAX) {
358 validator.reset(new H264Validator(frame_cb));
359 } else if (profile >= media::VP8PROFILE_MIN &&
360 profile <= media::VP8PROFILE_MAX) {
361 validator.reset(new VP8Validator(frame_cb));
362 } else {
363 LOG(FATAL) << "Unsupported profile: " << profile;
366 return validator.Pass();
369 class VEAClient : public VideoEncodeAccelerator::Client {
370 public:
371 VEAClient(const TestStream& test_stream,
372 ClientStateNotification<ClientState>* note,
373 bool save_to_file,
374 unsigned int keyframe_period,
375 bool force_bitrate,
376 bool test_perf);
377 virtual ~VEAClient();
378 void CreateEncoder();
379 void DestroyEncoder();
381 // Return the number of encoded frames per second.
382 double frames_per_second();
384 // VideoDecodeAccelerator::Client implementation.
385 virtual void RequireBitstreamBuffers(unsigned int input_count,
386 const gfx::Size& input_coded_size,
387 size_t output_buffer_size) OVERRIDE;
388 virtual void BitstreamBufferReady(int32 bitstream_buffer_id,
389 size_t payload_size,
390 bool key_frame) OVERRIDE;
391 virtual void NotifyError(VideoEncodeAccelerator::Error error) OVERRIDE;
393 private:
394 bool has_encoder() { return encoder_.get(); }
396 void SetState(ClientState new_state);
398 // Set current stream parameters to given |bitrate| at |framerate|.
399 void SetStreamParameters(unsigned int bitrate, unsigned int framerate);
401 // Called when encoder is done with a VideoFrame.
402 void InputNoLongerNeededCallback(int32 input_id);
404 // Ensure encoder has at least as many inputs as it asked for
405 // via RequireBitstreamBuffers().
406 void FeedEncoderWithInputs();
408 // Provide the encoder with a new output buffer.
409 void FeedEncoderWithOutput(base::SharedMemory* shm);
411 // Called on finding a complete frame (with |keyframe| set to true for
412 // keyframes) in the stream, to perform codec-independent, per-frame checks
413 // and accounting. Returns false once we have collected all frames we needed.
414 bool HandleEncodedFrame(bool keyframe);
416 // Verify that stream bitrate has been close to current_requested_bitrate_,
417 // assuming current_framerate_ since the last time VerifyStreamProperties()
418 // was called. Fail the test if |force_bitrate_| is true and the bitrate
419 // is not within kBitrateTolerance.
420 void VerifyStreamProperties();
422 // Test codec performance, failing the test if we are currently running
423 // the performance test.
424 void VerifyPerf();
426 // Prepare and return a frame wrapping the data at |position| bytes in
427 // the input stream, ready to be sent to encoder.
428 scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position);
430 ClientState state_;
431 scoped_ptr<VideoEncodeAccelerator> encoder_;
433 const TestStream& test_stream_;
434 // Used to notify another thread about the state. VEAClient does not own this.
435 ClientStateNotification<ClientState>* note_;
437 // Ids assigned to VideoFrames (start at 1 for easy comparison with
438 // num_encoded_frames_).
439 std::set<int32> inputs_at_client_;
440 int32 next_input_id_;
442 // Ids for output BitstreamBuffers.
443 typedef std::map<int32, base::SharedMemory*> IdToSHM;
444 ScopedVector<base::SharedMemory> output_shms_;
445 IdToSHM output_buffers_at_client_;
446 int32 next_output_buffer_id_;
448 // Current offset into input stream.
449 off_t pos_in_input_stream_;
450 // Byte size of an input frame.
451 size_t input_buffer_size_;
452 gfx::Size input_coded_size_;
453 // Requested by encoder.
454 unsigned int num_required_input_buffers_;
455 size_t output_buffer_size_;
457 // Precalculated number of frames in the stream.
458 unsigned int num_frames_in_stream_;
460 // Number of frames to encode. This may differ from num_frames_in_stream_ if
461 // we need more frames for bitrate tests.
462 unsigned int num_frames_to_encode_;
464 // Number of encoded frames we've got from the encoder thus far.
465 unsigned int num_encoded_frames_;
467 // Frames since last bitrate verification.
468 unsigned int num_frames_since_last_check_;
470 // True if received a keyframe while processing current bitstream buffer.
471 bool seen_keyframe_in_this_buffer_;
473 // True if we are to save the encoded stream to a file.
474 bool save_to_file_;
476 // Request a keyframe every keyframe_period_ frames.
477 const unsigned int keyframe_period_;
479 // Frame number for which we requested a keyframe.
480 unsigned int keyframe_requested_at_;
482 // True if we are asking encoder for a particular bitrate.
483 bool force_bitrate_;
485 // Current requested bitrate.
486 unsigned int current_requested_bitrate_;
488 // Current expected framerate.
489 unsigned int current_framerate_;
491 // Byte size of the encoded stream (for bitrate calculation) since last
492 // time we checked bitrate.
493 size_t encoded_stream_size_since_last_check_;
495 // If true, verify performance at the end of the test.
496 bool test_perf_;
498 scoped_ptr<StreamValidator> validator_;
500 // The time when the encoding started.
501 base::TimeTicks encode_start_time_;
503 // The time when the last encoded frame is ready.
504 base::TimeTicks last_frame_ready_time_;
506 // All methods of this class should be run on the same thread.
507 base::ThreadChecker thread_checker_;
510 VEAClient::VEAClient(const TestStream& test_stream,
511 ClientStateNotification<ClientState>* note,
512 bool save_to_file,
513 unsigned int keyframe_period,
514 bool force_bitrate,
515 bool test_perf)
516 : state_(CS_CREATED),
517 test_stream_(test_stream),
518 note_(note),
519 next_input_id_(1),
520 next_output_buffer_id_(0),
521 pos_in_input_stream_(0),
522 input_buffer_size_(0),
523 num_required_input_buffers_(0),
524 output_buffer_size_(0),
525 num_frames_in_stream_(0),
526 num_frames_to_encode_(0),
527 num_encoded_frames_(0),
528 num_frames_since_last_check_(0),
529 seen_keyframe_in_this_buffer_(false),
530 save_to_file_(save_to_file),
531 keyframe_period_(keyframe_period),
532 keyframe_requested_at_(kMaxFrameNum),
533 force_bitrate_(force_bitrate),
534 current_requested_bitrate_(0),
535 current_framerate_(0),
536 encoded_stream_size_since_last_check_(0),
537 test_perf_(test_perf) {
538 if (keyframe_period_)
539 CHECK_LT(kMaxKeyframeDelay, keyframe_period_);
541 validator_ = StreamValidator::Create(
542 test_stream_.requested_profile,
543 base::Bind(&VEAClient::HandleEncodedFrame, base::Unretained(this)));
545 CHECK(validator_.get());
547 if (save_to_file_) {
548 CHECK(!test_stream_.out_filename.empty());
549 base::FilePath out_filename(test_stream_.out_filename);
550 // This creates or truncates out_filename.
551 // Without it, AppendToFile() will not work.
552 EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0));
555 input_buffer_size_ =
556 media::VideoFrame::AllocationSize(kInputFormat, test_stream.size);
557 CHECK_GT(input_buffer_size_, 0UL);
559 // Calculate the number of frames in the input stream by dividing its length
560 // in bytes by frame size in bytes.
561 CHECK_EQ(test_stream_.input_file.length() % input_buffer_size_, 0U)
562 << "Stream byte size is not a product of calculated frame byte size";
563 num_frames_in_stream_ = test_stream_.input_file.length() / input_buffer_size_;
564 CHECK_GT(num_frames_in_stream_, 0UL);
565 CHECK_LE(num_frames_in_stream_, kMaxFrameNum);
567 // We may need to loop over the stream more than once if more frames than
568 // provided is required for bitrate tests.
569 if (force_bitrate_ && num_frames_in_stream_ < kMinFramesForBitrateTests) {
570 DVLOG(1) << "Stream too short for bitrate test (" << num_frames_in_stream_
571 << " frames), will loop it to reach " << kMinFramesForBitrateTests
572 << " frames";
573 num_frames_to_encode_ = kMinFramesForBitrateTests;
574 } else {
575 num_frames_to_encode_ = num_frames_in_stream_;
578 thread_checker_.DetachFromThread();
581 VEAClient::~VEAClient() { CHECK(!has_encoder()); }
583 void VEAClient::CreateEncoder() {
584 DCHECK(thread_checker_.CalledOnValidThread());
585 CHECK(!has_encoder());
587 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
588 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder);
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
594 SetState(CS_ENCODER_SET);
596 DVLOG(1) << "Profile: " << test_stream_.requested_profile
597 << ", initial bitrate: " << test_stream_.requested_bitrate;
598 if (!encoder_->Initialize(kInputFormat,
599 test_stream_.size,
600 test_stream_.requested_profile,
601 test_stream_.requested_bitrate,
602 this)) {
603 DLOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed";
604 SetState(CS_ERROR);
605 return;
608 SetStreamParameters(test_stream_.requested_bitrate,
609 test_stream_.requested_framerate);
610 SetState(CS_INITIALIZED);
613 void VEAClient::DestroyEncoder() {
614 DCHECK(thread_checker_.CalledOnValidThread());
615 if (!has_encoder())
616 return;
617 encoder_.reset();
620 double VEAClient::frames_per_second() {
621 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_;
622 return num_encoded_frames_ / duration.InSecondsF();
625 void VEAClient::RequireBitstreamBuffers(unsigned int input_count,
626 const gfx::Size& input_coded_size,
627 size_t output_size) {
628 DCHECK(thread_checker_.CalledOnValidThread());
629 ASSERT_EQ(state_, CS_INITIALIZED);
630 SetState(CS_ENCODING);
632 // TODO(posciak): For now we only support input streams that meet encoder
633 // size requirements exactly (i.e. coded size == visible size), so that we
634 // can simply mmap the stream file and feed the encoder directly with chunks
635 // of that, instead of memcpying from mmapped file into a separate set of
636 // input buffers that would meet the coded size and alignment requirements.
637 // If/when this is changed, the ARM-specific alignment check below should be
638 // redone as well.
639 input_coded_size_ = input_coded_size;
640 ASSERT_EQ(input_coded_size_, test_stream_.size);
641 #if defined(ARCH_CPU_ARMEL)
642 // ARM performs CPU cache management with CPU cache line granularity. We thus
643 // need to ensure our buffers are CPU cache line-aligned (64 byte-aligned).
644 // Otherwise newer kernels will refuse to accept them, and on older kernels
645 // we'll be treating ourselves to random corruption.
646 // Since we are just mmapping and passing chunks of the input file, to ensure
647 // alignment, if the starting virtual addresses of the frames in it were not
648 // 64 byte-aligned, we'd have to use a separate set of input buffers and copy
649 // the frames into them before sending to the encoder. It would have been an
650 // overkill here though, because, for now at least, we only test resolutions
651 // that result in proper alignment, and it would have also interfered with
652 // performance testing. So just assert that the frame size is a multiple of
653 // 64 bytes. This ensures all frames start at 64-byte boundary, because
654 // MemoryMappedFile should be mmapp()ed at virtual page start as well.
655 ASSERT_EQ(input_buffer_size_ & 63, 0u)
656 << "Frame size has to be a multiple of 64 bytes";
657 ASSERT_EQ(reinterpret_cast<off_t>(test_stream_.input_file.data()) & 63, 0)
658 << "Mapped file should be mapped at a 64 byte boundary";
659 #endif
661 num_required_input_buffers_ = input_count;
662 ASSERT_GT(num_required_input_buffers_, 0UL);
664 output_buffer_size_ = output_size;
665 ASSERT_GT(output_buffer_size_, 0UL);
667 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) {
668 base::SharedMemory* shm = new base::SharedMemory();
669 CHECK(shm->CreateAndMapAnonymous(output_buffer_size_));
670 output_shms_.push_back(shm);
671 FeedEncoderWithOutput(shm);
674 encode_start_time_ = base::TimeTicks::Now();
675 FeedEncoderWithInputs();
678 void VEAClient::BitstreamBufferReady(int32 bitstream_buffer_id,
679 size_t payload_size,
680 bool key_frame) {
681 DCHECK(thread_checker_.CalledOnValidThread());
682 ASSERT_LE(payload_size, output_buffer_size_);
684 IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id);
685 ASSERT_NE(it, output_buffers_at_client_.end());
686 base::SharedMemory* shm = it->second;
687 output_buffers_at_client_.erase(it);
689 if (state_ == CS_FINISHED)
690 return;
692 encoded_stream_size_since_last_check_ += payload_size;
694 const uint8* stream_ptr = static_cast<const uint8*>(shm->memory());
695 if (payload_size > 0)
696 validator_->ProcessStreamBuffer(stream_ptr, payload_size);
698 EXPECT_EQ(key_frame, seen_keyframe_in_this_buffer_);
699 seen_keyframe_in_this_buffer_ = false;
701 if (save_to_file_) {
702 int size = base::checked_cast<int>(payload_size);
703 EXPECT_EQ(base::AppendToFile(
704 base::FilePath::FromUTF8Unsafe(test_stream_.out_filename),
705 static_cast<char*>(shm->memory()),
706 size),
707 size);
710 FeedEncoderWithOutput(shm);
713 void VEAClient::NotifyError(VideoEncodeAccelerator::Error error) {
714 DCHECK(thread_checker_.CalledOnValidThread());
715 SetState(CS_ERROR);
718 void VEAClient::SetState(ClientState new_state) {
719 DVLOG(4) << "Changing state " << state_ << "->" << new_state;
720 note_->Notify(new_state);
721 state_ = new_state;
724 void VEAClient::SetStreamParameters(unsigned int bitrate,
725 unsigned int framerate) {
726 current_requested_bitrate_ = bitrate;
727 current_framerate_ = framerate;
728 CHECK_GT(current_requested_bitrate_, 0UL);
729 CHECK_GT(current_framerate_, 0UL);
730 encoder_->RequestEncodingParametersChange(current_requested_bitrate_,
731 current_framerate_);
732 DVLOG(1) << "Switched parameters to " << current_requested_bitrate_
733 << " bps @ " << current_framerate_ << " FPS";
736 void VEAClient::InputNoLongerNeededCallback(int32 input_id) {
737 std::set<int32>::iterator it = inputs_at_client_.find(input_id);
738 ASSERT_NE(it, inputs_at_client_.end());
739 inputs_at_client_.erase(it);
740 FeedEncoderWithInputs();
743 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) {
744 CHECK_LE(position + input_buffer_size_, test_stream_.input_file.length());
746 uint8* frame_data =
747 const_cast<uint8*>(test_stream_.input_file.data() + position);
749 CHECK_GT(current_framerate_, 0U);
750 scoped_refptr<media::VideoFrame> frame =
751 media::VideoFrame::WrapExternalYuvData(
752 kInputFormat,
753 input_coded_size_,
754 gfx::Rect(test_stream_.size),
755 test_stream_.size,
756 input_coded_size_.width(),
757 input_coded_size_.width() / 2,
758 input_coded_size_.width() / 2,
759 frame_data,
760 frame_data + input_coded_size_.GetArea(),
761 frame_data + (input_coded_size_.GetArea() * 5 / 4),
762 base::TimeDelta().FromMilliseconds(
763 next_input_id_ * base::Time::kMillisecondsPerSecond /
764 current_framerate_),
765 media::BindToCurrentLoop(
766 base::Bind(&VEAClient::InputNoLongerNeededCallback,
767 base::Unretained(this),
768 next_input_id_)));
770 CHECK(inputs_at_client_.insert(next_input_id_).second);
771 ++next_input_id_;
773 return frame;
776 void VEAClient::FeedEncoderWithInputs() {
777 if (!has_encoder())
778 return;
780 if (state_ != CS_ENCODING)
781 return;
783 while (inputs_at_client_.size() <
784 num_required_input_buffers_ + kNumExtraInputFrames) {
785 size_t bytes_left = test_stream_.input_file.length() - pos_in_input_stream_;
786 if (bytes_left < input_buffer_size_) {
787 DCHECK_EQ(bytes_left, 0UL);
788 // Rewind if at the end of stream and we are still encoding.
789 // This is to flush the encoder with additional frames from the beginning
790 // of the stream, or if the stream is shorter that the number of frames
791 // we require for bitrate tests.
792 pos_in_input_stream_ = 0;
793 continue;
796 bool force_keyframe = false;
797 if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) {
798 keyframe_requested_at_ = next_input_id_;
799 force_keyframe = true;
802 scoped_refptr<media::VideoFrame> video_frame =
803 PrepareInputFrame(pos_in_input_stream_);
804 pos_in_input_stream_ += input_buffer_size_;
806 encoder_->Encode(video_frame, force_keyframe);
810 void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) {
811 if (!has_encoder())
812 return;
814 if (state_ != CS_ENCODING)
815 return;
817 base::SharedMemoryHandle dup_handle;
818 CHECK(shm->ShareToProcess(base::Process::Current().handle(), &dup_handle));
820 media::BitstreamBuffer bitstream_buffer(
821 next_output_buffer_id_++, dup_handle, output_buffer_size_);
822 CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(),
823 shm)).second);
824 encoder_->UseOutputBitstreamBuffer(bitstream_buffer);
827 bool VEAClient::HandleEncodedFrame(bool keyframe) {
828 // This would be a bug in the test, which should not ignore false
829 // return value from this method.
830 CHECK_LE(num_encoded_frames_, num_frames_to_encode_);
832 ++num_encoded_frames_;
833 ++num_frames_since_last_check_;
835 last_frame_ready_time_ = base::TimeTicks::Now();
836 if (keyframe) {
837 // Got keyframe, reset keyframe detection regardless of whether we
838 // got a frame in time or not.
839 keyframe_requested_at_ = kMaxFrameNum;
840 seen_keyframe_in_this_buffer_ = true;
843 // Because the keyframe behavior requirements are loose, we give
844 // the encoder more freedom here. It could either deliver a keyframe
845 // immediately after we requested it, which could be for a frame number
846 // before the one we requested it for (if the keyframe request
847 // is asynchronous, i.e. not bound to any concrete frame, and because
848 // the pipeline can be deeper than one frame), at that frame, or after.
849 // So the only constraints we put here is that we get a keyframe not
850 // earlier than we requested one (in time), and not later than
851 // kMaxKeyframeDelay frames after the frame, for which we requested
852 // it, comes back encoded.
853 EXPECT_LE(num_encoded_frames_, keyframe_requested_at_ + kMaxKeyframeDelay);
855 if (num_encoded_frames_ == num_frames_to_encode_ / 2) {
856 VerifyStreamProperties();
857 if (test_stream_.requested_subsequent_bitrate !=
858 current_requested_bitrate_ ||
859 test_stream_.requested_subsequent_framerate != current_framerate_) {
860 SetStreamParameters(test_stream_.requested_subsequent_bitrate,
861 test_stream_.requested_subsequent_framerate);
863 } else if (num_encoded_frames_ == num_frames_to_encode_) {
864 VerifyPerf();
865 VerifyStreamProperties();
866 SetState(CS_FINISHED);
867 return false;
870 return true;
873 void VEAClient::VerifyPerf() {
874 double measured_fps = frames_per_second();
875 LOG(INFO) << "Measured encoder FPS: " << measured_fps;
876 if (test_perf_)
877 EXPECT_GE(measured_fps, kMinPerfFPS);
880 void VEAClient::VerifyStreamProperties() {
881 CHECK_GT(num_frames_since_last_check_, 0UL);
882 CHECK_GT(encoded_stream_size_since_last_check_, 0UL);
883 unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 *
884 current_framerate_ / num_frames_since_last_check_;
885 DVLOG(1) << "Current chunk's bitrate: " << bitrate
886 << " (expected: " << current_requested_bitrate_
887 << " @ " << current_framerate_ << " FPS,"
888 << " num frames in chunk: " << num_frames_since_last_check_;
890 num_frames_since_last_check_ = 0;
891 encoded_stream_size_since_last_check_ = 0;
893 if (force_bitrate_) {
894 EXPECT_NEAR(bitrate,
895 current_requested_bitrate_,
896 kBitrateTolerance * current_requested_bitrate_);
900 // Test parameters:
901 // - Number of concurrent encoders.
902 // - If true, save output to file (provided an output filename was supplied).
903 // - Force a keyframe every n frames.
904 // - Force bitrate; the actual required value is provided as a property
905 // of the input stream, because it depends on stream type/resolution/etc.
906 // - If true, measure performance.
907 // - If true, switch bitrate mid-stream.
908 // - If true, switch framerate mid-stream.
909 class VideoEncodeAcceleratorTest
910 : public ::testing::TestWithParam<
911 Tuple7<int, bool, int, bool, bool, bool, bool> > {};
913 TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) {
914 const size_t num_concurrent_encoders = GetParam().a;
915 const bool save_to_file = GetParam().b;
916 const unsigned int keyframe_period = GetParam().c;
917 const bool force_bitrate = GetParam().d;
918 const bool test_perf = GetParam().e;
919 const bool mid_stream_bitrate_switch = GetParam().f;
920 const bool mid_stream_framerate_switch = GetParam().g;
922 // Initialize the test streams.
923 ScopedVector<TestStream> test_streams;
924 ParseAndReadTestStreamData(*g_test_stream_data, &test_streams);
925 UpdateTestStreamData(
926 mid_stream_bitrate_switch, mid_stream_framerate_switch, &test_streams);
928 ScopedVector<ClientStateNotification<ClientState> > notes;
929 ScopedVector<VEAClient> clients;
930 base::Thread encoder_thread("EncoderThread");
931 ASSERT_TRUE(encoder_thread.Start());
933 // Create all encoders.
934 for (size_t i = 0; i < num_concurrent_encoders; i++) {
935 size_t test_stream_index = i % test_streams.size();
936 // Disregard save_to_file if we didn't get an output filename.
937 bool encoder_save_to_file =
938 (save_to_file &&
939 !test_streams[test_stream_index]->out_filename.empty());
941 notes.push_back(new ClientStateNotification<ClientState>());
942 clients.push_back(new VEAClient(*test_streams[test_stream_index],
943 notes.back(),
944 encoder_save_to_file,
945 keyframe_period,
946 force_bitrate,
947 test_perf));
949 encoder_thread.message_loop()->PostTask(
950 FROM_HERE,
951 base::Bind(&VEAClient::CreateEncoder,
952 base::Unretained(clients.back())));
955 // All encoders must pass through states in this order.
956 enum ClientState state_transitions[] = {CS_ENCODER_SET, CS_INITIALIZED,
957 CS_ENCODING, CS_FINISHED};
959 // Wait for all encoders to go through all states and finish.
960 // Do this by waiting for all encoders to advance to state n before checking
961 // state n+1, to verify that they are able to operate concurrently.
962 // It also simulates the real-world usage better, as the main thread, on which
963 // encoders are created/destroyed, is a single GPU Process ChildThread.
964 // Moreover, we can't have proper multithreading on X11, so this could cause
965 // hard to debug issues there, if there were multiple "ChildThreads".
966 for (size_t state_no = 0; state_no < arraysize(state_transitions); ++state_no)
967 for (size_t i = 0; i < num_concurrent_encoders; i++)
968 ASSERT_EQ(notes[i]->Wait(), state_transitions[state_no]);
970 for (size_t i = 0; i < num_concurrent_encoders; ++i) {
971 encoder_thread.message_loop()->PostTask(
972 FROM_HERE,
973 base::Bind(&VEAClient::DestroyEncoder, base::Unretained(clients[i])));
976 // This ensures all tasks have finished.
977 encoder_thread.Stop();
980 INSTANTIATE_TEST_CASE_P(
981 SimpleEncode,
982 VideoEncodeAcceleratorTest,
983 ::testing::Values(MakeTuple(1, true, 0, false, false, false, false)));
985 INSTANTIATE_TEST_CASE_P(
986 EncoderPerf,
987 VideoEncodeAcceleratorTest,
988 ::testing::Values(MakeTuple(1, false, 0, false, true, false, false)));
990 INSTANTIATE_TEST_CASE_P(
991 ForceKeyframes,
992 VideoEncodeAcceleratorTest,
993 ::testing::Values(MakeTuple(1, false, 10, false, false, false, false)));
995 INSTANTIATE_TEST_CASE_P(
996 ForceBitrate,
997 VideoEncodeAcceleratorTest,
998 ::testing::Values(MakeTuple(1, false, 0, true, false, false, false)));
1000 INSTANTIATE_TEST_CASE_P(
1001 MidStreamParamSwitchBitrate,
1002 VideoEncodeAcceleratorTest,
1003 ::testing::Values(MakeTuple(1, false, 0, true, false, true, false)));
1005 INSTANTIATE_TEST_CASE_P(
1006 MidStreamParamSwitchFPS,
1007 VideoEncodeAcceleratorTest,
1008 ::testing::Values(MakeTuple(1, false, 0, true, false, false, true)));
1010 INSTANTIATE_TEST_CASE_P(
1011 MidStreamParamSwitchBitrateAndFPS,
1012 VideoEncodeAcceleratorTest,
1013 ::testing::Values(MakeTuple(1, false, 0, true, false, true, true)));
1015 INSTANTIATE_TEST_CASE_P(
1016 MultipleEncoders,
1017 VideoEncodeAcceleratorTest,
1018 ::testing::Values(MakeTuple(3, false, 0, false, false, false, false),
1019 MakeTuple(3, false, 0, true, false, true, true)));
1021 // TODO(posciak): more tests:
1022 // - async FeedEncoderWithOutput
1023 // - out-of-order return of outputs to encoder
1024 // - multiple encoders + decoders
1025 // - mid-stream encoder_->Destroy()
1027 } // namespace
1028 } // namespace content
1030 int main(int argc, char** argv) {
1031 testing::InitGoogleTest(&argc, argv); // Removes gtest-specific args.
1032 base::CommandLine::Init(argc, argv);
1034 base::ShadowingAtExitManager at_exit_manager;
1035 scoped_ptr<base::FilePath::StringType> test_stream_data(
1036 new base::FilePath::StringType(
1037 media::GetTestDataFilePath(content::g_default_in_filename).value() +
1038 content::g_default_in_parameters));
1039 content::g_test_stream_data = test_stream_data.get();
1041 // Needed to enable DVLOG through --vmodule.
1042 logging::LoggingSettings settings;
1043 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
1044 CHECK(logging::InitLogging(settings));
1046 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
1047 DCHECK(cmd_line);
1049 base::CommandLine::SwitchMap switches = cmd_line->GetSwitches();
1050 for (base::CommandLine::SwitchMap::const_iterator it = switches.begin();
1051 it != switches.end();
1052 ++it) {
1053 if (it->first == "test_stream_data") {
1054 test_stream_data->assign(it->second.c_str());
1055 continue;
1057 if (it->first == "v" || it->first == "vmodule")
1058 continue;
1059 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second;
1062 return RUN_ALL_TESTS();