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"
7 #include "base/command_line.h"
8 #include "base/files/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"
25 #include "ui/gfx/x/x11_types.h"
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"
33 #error The VideoEncodeAcceleratorUnittest is not supported on this platform.
36 using media::VideoEncodeAccelerator;
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
90 // - |requested_subsequent_framerate| framerate to switch to in the middle
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 // Environment to store test stream data for all test cases.
96 class VideoEncodeAcceleratorTestEnvironment;
97 VideoEncodeAcceleratorTestEnvironment* g_env;
102 aligned_buffer_size(0),
103 requested_bitrate(0),
104 requested_framerate(0),
105 requested_subsequent_bitrate(0),
106 requested_subsequent_framerate(0) {}
109 gfx::Size visible_size;
110 gfx::Size coded_size;
111 unsigned int num_frames;
113 // Original unaligned input file name provided as an argument to the test.
114 // And the file must be an I420 (YUV planar) raw stream.
115 std::string in_filename;
117 // A temporary file used to prepare aligned input buffers of |in_filename|.
118 // The file makes sure starting address of YUV planes are 64 byte-aligned.
119 base::FilePath aligned_in_file;
121 // The memory mapping of |aligned_in_file|
122 base::MemoryMappedFile mapped_aligned_in_file;
124 // Byte size of a frame of |aligned_in_file|.
125 size_t aligned_buffer_size;
127 // Byte size for each aligned plane of a frame
128 std::vector<size_t> aligned_plane_size;
130 std::string out_filename;
131 media::VideoCodecProfile requested_profile;
132 unsigned int requested_bitrate;
133 unsigned int requested_framerate;
134 unsigned int requested_subsequent_bitrate;
135 unsigned int requested_subsequent_framerate;
138 inline static size_t Align64Bytes(size_t value) {
139 return (value + 63) & ~63;
142 // Write |data| of |size| bytes at |offset| bytes into |file|.
143 static bool WriteFile(base::File* file,
147 size_t written_bytes = 0;
148 while (written_bytes < size) {
149 int bytes = file->Write(offset + written_bytes,
150 reinterpret_cast<const char*>(data + written_bytes),
151 size - written_bytes);
154 written_bytes += bytes;
159 // ARM performs CPU cache management with CPU cache line granularity. We thus
160 // need to ensure our buffers are CPU cache line-aligned (64 byte-aligned).
161 // Otherwise newer kernels will refuse to accept them, and on older kernels
162 // we'll be treating ourselves to random corruption.
163 // Since we are just mapping and passing chunks of the input file directly to
164 // the VEA as input frames to avoid copying large chunks of raw data on each
165 // frame and thus affecting performance measurements, we have to prepare a
166 // temporary file with all planes aligned to 64-byte boundaries beforehand.
167 static void CreateAlignedInputStreamFile(const gfx::Size& coded_size,
168 TestStream* test_stream) {
169 // Test case may have many encoders and memory should be prepared once.
170 if (test_stream->coded_size == coded_size &&
171 test_stream->mapped_aligned_in_file.IsValid())
174 // All encoders in multiple encoder test reuse the same test_stream, make
175 // sure they requested the same coded_size
176 ASSERT_TRUE(!test_stream->mapped_aligned_in_file.IsValid() ||
177 coded_size == test_stream->coded_size);
178 test_stream->coded_size = coded_size;
180 size_t num_planes = media::VideoFrame::NumPlanes(kInputFormat);
181 std::vector<size_t> padding_sizes(num_planes);
182 std::vector<size_t> coded_bpl(num_planes);
183 std::vector<size_t> visible_bpl(num_planes);
184 std::vector<size_t> visible_plane_rows(num_planes);
186 // Calculate padding in bytes to be added after each plane required to keep
187 // starting addresses of all planes at a 64 byte boudnary. This padding will
188 // be added after each plane when copying to the temporary file.
189 // At the same time we also need to take into account coded_size requested by
190 // the VEA; each row of visible_bpl bytes in the original file needs to be
191 // copied into a row of coded_bpl bytes in the aligned file.
192 for (size_t i = 0; i < num_planes; i++) {
194 media::VideoFrame::PlaneAllocationSize(kInputFormat, i, coded_size);
195 test_stream->aligned_plane_size.push_back(Align64Bytes(size));
196 test_stream->aligned_buffer_size += test_stream->aligned_plane_size.back();
199 media::VideoFrame::RowBytes(i, kInputFormat, coded_size.width());
200 visible_bpl[i] = media::VideoFrame::RowBytes(
201 i, kInputFormat, test_stream->visible_size.width());
202 visible_plane_rows[i] = media::VideoFrame::Rows(
203 i, kInputFormat, test_stream->visible_size.height());
204 size_t padding_rows =
205 media::VideoFrame::Rows(i, kInputFormat, coded_size.height()) -
206 visible_plane_rows[i];
207 padding_sizes[i] = padding_rows * coded_bpl[i] + Align64Bytes(size) - size;
210 base::MemoryMappedFile src_file;
211 CHECK(src_file.Initialize(base::FilePath(test_stream->in_filename)));
212 CHECK(base::CreateTemporaryFile(&test_stream->aligned_in_file));
214 size_t visible_buffer_size = media::VideoFrame::AllocationSize(
215 kInputFormat, test_stream->visible_size);
216 CHECK_EQ(src_file.length() % visible_buffer_size, 0U)
217 << "Stream byte size is not a product of calculated frame byte size";
219 test_stream->num_frames = src_file.length() / visible_buffer_size;
220 uint32 flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
221 base::File::FLAG_READ;
223 // Create a temporary file with coded_size length.
224 base::File dest_file(test_stream->aligned_in_file, flags);
225 CHECK_GT(test_stream->aligned_buffer_size, 0UL);
226 dest_file.SetLength(test_stream->aligned_buffer_size *
227 test_stream->num_frames);
229 const uint8* src = src_file.data();
230 off_t dest_offset = 0;
231 for (size_t frame = 0; frame < test_stream->num_frames; frame++) {
232 for (size_t i = 0; i < num_planes; i++) {
233 // Assert that each plane of frame starts at 64 byte boundary.
234 ASSERT_EQ(dest_offset & 63, 0)
235 << "Planes of frame should be mapped at a 64 byte boundary";
236 for (size_t j = 0; j < visible_plane_rows[i]; j++) {
237 CHECK(WriteFile(&dest_file, dest_offset, src, visible_bpl[i]));
238 src += visible_bpl[i];
239 dest_offset += coded_bpl[i];
241 dest_offset += padding_sizes[i];
244 CHECK(test_stream->mapped_aligned_in_file.Initialize(dest_file.Pass()));
245 // Assert that memory mapped of file starts at 64 byte boundary. So each
246 // plane of frames also start at 64 byte boundary.
248 reinterpret_cast<off_t>(test_stream->mapped_aligned_in_file.data()) & 63,
250 << "File should be mapped at a 64 byte boundary";
252 CHECK_EQ(test_stream->mapped_aligned_in_file.length() %
253 test_stream->aligned_buffer_size,
255 << "Stream byte size is not a product of calculated frame byte size";
256 CHECK_GT(test_stream->num_frames, 0UL);
257 CHECK_LE(test_stream->num_frames, kMaxFrameNum);
260 // Parse |data| into its constituent parts, set the various output fields
261 // accordingly, read in video stream, and store them to |test_streams|.
262 static void ParseAndReadTestStreamData(const base::FilePath::StringType& data,
263 ScopedVector<TestStream>* test_streams) {
264 // Split the string to individual test stream data.
265 std::vector<base::FilePath::StringType> test_streams_data;
266 base::SplitString(data, ';', &test_streams_data);
267 CHECK_GE(test_streams_data.size(), 1U) << data;
269 // Parse each test stream data and read the input file.
270 for (size_t index = 0; index < test_streams_data.size(); ++index) {
271 std::vector<base::FilePath::StringType> fields;
272 base::SplitString(test_streams_data[index], ':', &fields);
273 CHECK_GE(fields.size(), 4U) << data;
274 CHECK_LE(fields.size(), 9U) << data;
275 TestStream* test_stream = new TestStream();
277 test_stream->in_filename = fields[0];
279 CHECK(base::StringToInt(fields[1], &width));
280 CHECK(base::StringToInt(fields[2], &height));
281 test_stream->visible_size = gfx::Size(width, height);
282 CHECK(!test_stream->visible_size.IsEmpty());
284 CHECK(base::StringToInt(fields[3], &profile));
285 CHECK_GT(profile, media::VIDEO_CODEC_PROFILE_UNKNOWN);
286 CHECK_LE(profile, media::VIDEO_CODEC_PROFILE_MAX);
287 test_stream->requested_profile =
288 static_cast<media::VideoCodecProfile>(profile);
290 if (fields.size() >= 5 && !fields[4].empty())
291 test_stream->out_filename = fields[4];
293 if (fields.size() >= 6 && !fields[5].empty())
294 CHECK(base::StringToUint(fields[5], &test_stream->requested_bitrate));
296 if (fields.size() >= 7 && !fields[6].empty())
297 CHECK(base::StringToUint(fields[6], &test_stream->requested_framerate));
299 if (fields.size() >= 8 && !fields[7].empty()) {
300 CHECK(base::StringToUint(fields[7],
301 &test_stream->requested_subsequent_bitrate));
304 if (fields.size() >= 9 && !fields[8].empty()) {
305 CHECK(base::StringToUint(fields[8],
306 &test_stream->requested_subsequent_framerate));
308 test_streams->push_back(test_stream);
321 // Performs basic, codec-specific sanity checks on the stream buffers passed
322 // to ProcessStreamBuffer(): whether we've seen keyframes before non-keyframes,
323 // correct sequences of H.264 NALUs (SPS before PPS and before slices), etc.
324 // Calls given FrameFoundCallback when a complete frame is found while
326 class StreamValidator {
328 // To be called when a complete frame is found while processing a stream
329 // buffer, passing true if the frame is a keyframe. Returns false if we
330 // are not interested in more frames and further processing should be aborted.
331 typedef base::Callback<bool(bool)> FrameFoundCallback;
333 virtual ~StreamValidator() {}
335 // Provide a StreamValidator instance for the given |profile|.
336 static scoped_ptr<StreamValidator> Create(media::VideoCodecProfile profile,
337 const FrameFoundCallback& frame_cb);
339 // Process and verify contents of a bitstream buffer.
340 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) = 0;
343 explicit StreamValidator(const FrameFoundCallback& frame_cb)
344 : frame_cb_(frame_cb) {}
346 FrameFoundCallback frame_cb_;
349 class H264Validator : public StreamValidator {
351 explicit H264Validator(const FrameFoundCallback& frame_cb)
352 : StreamValidator(frame_cb),
357 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE;
360 // Set to true when encoder provides us with the corresponding NALU type.
365 media::H264Parser h264_parser_;
368 void H264Validator::ProcessStreamBuffer(const uint8* stream, size_t size) {
369 h264_parser_.SetStream(stream, size);
372 media::H264NALU nalu;
373 media::H264Parser::Result result;
375 result = h264_parser_.AdvanceToNextNALU(&nalu);
376 if (result == media::H264Parser::kEOStream)
379 ASSERT_EQ(media::H264Parser::kOk, result);
381 bool keyframe = false;
383 switch (nalu.nal_unit_type) {
384 case media::H264NALU::kIDRSlice:
385 ASSERT_TRUE(seen_sps_);
386 ASSERT_TRUE(seen_pps_);
389 case media::H264NALU::kNonIDRSlice: {
390 ASSERT_TRUE(seen_idr_);
392 media::H264SliceHeader shdr;
393 ASSERT_EQ(media::H264Parser::kOk,
394 h264_parser_.ParseSliceHeader(nalu, &shdr));
395 keyframe = shdr.IsISlice() || shdr.IsSISlice();
397 if (!frame_cb_.Run(keyframe))
402 case media::H264NALU::kSPS: {
404 ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParseSPS(&sps_id));
409 case media::H264NALU::kPPS: {
410 ASSERT_TRUE(seen_sps_);
412 ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParsePPS(&pps_id));
423 class VP8Validator : public StreamValidator {
425 explicit VP8Validator(const FrameFoundCallback& frame_cb)
426 : StreamValidator(frame_cb),
427 seen_keyframe_(false) {}
429 virtual void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE;
432 // Have we already got a keyframe in the stream?
436 void VP8Validator::ProcessStreamBuffer(const uint8* stream, size_t size) {
437 bool keyframe = !(stream[0] & 0x01);
439 seen_keyframe_ = true;
441 EXPECT_TRUE(seen_keyframe_);
443 frame_cb_.Run(keyframe);
444 // TODO(posciak): We could be getting more frames in the buffer, but there is
445 // no simple way to detect this. We'd need to parse the frames and go through
446 // partition numbers/sizes. For now assume one frame per buffer.
450 scoped_ptr<StreamValidator> StreamValidator::Create(
451 media::VideoCodecProfile profile,
452 const FrameFoundCallback& frame_cb) {
453 scoped_ptr<StreamValidator> validator;
455 if (profile >= media::H264PROFILE_MIN &&
456 profile <= media::H264PROFILE_MAX) {
457 validator.reset(new H264Validator(frame_cb));
458 } else if (profile >= media::VP8PROFILE_MIN &&
459 profile <= media::VP8PROFILE_MAX) {
460 validator.reset(new VP8Validator(frame_cb));
462 LOG(FATAL) << "Unsupported profile: " << profile;
465 return validator.Pass();
468 class VEAClient : public VideoEncodeAccelerator::Client {
470 VEAClient(TestStream* test_stream,
471 ClientStateNotification<ClientState>* note,
473 unsigned int keyframe_period,
476 bool mid_stream_bitrate_switch,
477 bool mid_stream_framerate_switch);
478 virtual ~VEAClient();
479 void CreateEncoder();
480 void DestroyEncoder();
482 // Return the number of encoded frames per second.
483 double frames_per_second();
485 // VideoDecodeAccelerator::Client implementation.
486 virtual void RequireBitstreamBuffers(unsigned int input_count,
487 const gfx::Size& input_coded_size,
488 size_t output_buffer_size) OVERRIDE;
489 virtual void BitstreamBufferReady(int32 bitstream_buffer_id,
491 bool key_frame) OVERRIDE;
492 virtual void NotifyError(VideoEncodeAccelerator::Error error) OVERRIDE;
495 bool has_encoder() { return encoder_.get(); }
497 void SetState(ClientState new_state);
499 // Set current stream parameters to given |bitrate| at |framerate|.
500 void SetStreamParameters(unsigned int bitrate, unsigned int framerate);
502 // Called when encoder is done with a VideoFrame.
503 void InputNoLongerNeededCallback(int32 input_id);
505 // Ensure encoder has at least as many inputs as it asked for
506 // via RequireBitstreamBuffers().
507 void FeedEncoderWithInputs();
509 // Provide the encoder with a new output buffer.
510 void FeedEncoderWithOutput(base::SharedMemory* shm);
512 // Called on finding a complete frame (with |keyframe| set to true for
513 // keyframes) in the stream, to perform codec-independent, per-frame checks
514 // and accounting. Returns false once we have collected all frames we needed.
515 bool HandleEncodedFrame(bool keyframe);
517 // Verify that stream bitrate has been close to current_requested_bitrate_,
518 // assuming current_framerate_ since the last time VerifyStreamProperties()
519 // was called. Fail the test if |force_bitrate_| is true and the bitrate
520 // is not within kBitrateTolerance.
521 void VerifyStreamProperties();
523 // Test codec performance, failing the test if we are currently running
524 // the performance test.
527 // Prepare and return a frame wrapping the data at |position| bytes in
528 // the input stream, ready to be sent to encoder.
529 scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position);
531 // Update the parameters according to |mid_stream_bitrate_switch| and
532 // |mid_stream_framerate_switch|.
533 void UpdateTestStreamData(bool mid_stream_bitrate_switch,
534 bool mid_stream_framerate_switch);
537 scoped_ptr<VideoEncodeAccelerator> encoder_;
539 TestStream* test_stream_;
540 // Used to notify another thread about the state. VEAClient does not own this.
541 ClientStateNotification<ClientState>* note_;
543 // Ids assigned to VideoFrames (start at 1 for easy comparison with
544 // num_encoded_frames_).
545 std::set<int32> inputs_at_client_;
546 int32 next_input_id_;
548 // Ids for output BitstreamBuffers.
549 typedef std::map<int32, base::SharedMemory*> IdToSHM;
550 ScopedVector<base::SharedMemory> output_shms_;
551 IdToSHM output_buffers_at_client_;
552 int32 next_output_buffer_id_;
554 // Current offset into input stream.
555 off_t pos_in_input_stream_;
556 gfx::Size input_coded_size_;
557 // Requested by encoder.
558 unsigned int num_required_input_buffers_;
559 size_t output_buffer_size_;
561 // Number of frames to encode. This may differ from the number of frames in
562 // stream if we need more frames for bitrate tests.
563 unsigned int num_frames_to_encode_;
565 // Number of encoded frames we've got from the encoder thus far.
566 unsigned int num_encoded_frames_;
568 // Frames since last bitrate verification.
569 unsigned int num_frames_since_last_check_;
571 // True if received a keyframe while processing current bitstream buffer.
572 bool seen_keyframe_in_this_buffer_;
574 // True if we are to save the encoded stream to a file.
577 // Request a keyframe every keyframe_period_ frames.
578 const unsigned int keyframe_period_;
580 // Frame number for which we requested a keyframe.
581 unsigned int keyframe_requested_at_;
583 // True if we are asking encoder for a particular bitrate.
586 // Current requested bitrate.
587 unsigned int current_requested_bitrate_;
589 // Current expected framerate.
590 unsigned int current_framerate_;
592 // Byte size of the encoded stream (for bitrate calculation) since last
593 // time we checked bitrate.
594 size_t encoded_stream_size_since_last_check_;
596 // If true, verify performance at the end of the test.
599 scoped_ptr<StreamValidator> validator_;
601 // The time when the encoding started.
602 base::TimeTicks encode_start_time_;
604 // The time when the last encoded frame is ready.
605 base::TimeTicks last_frame_ready_time_;
607 // All methods of this class should be run on the same thread.
608 base::ThreadChecker thread_checker_;
610 // Requested bitrate in bits per second.
611 unsigned int requested_bitrate_;
613 // Requested initial framerate.
614 unsigned int requested_framerate_;
616 // Bitrate to switch to in the middle of the stream.
617 unsigned int requested_subsequent_bitrate_;
619 // Framerate to switch to in the middle of the stream.
620 unsigned int requested_subsequent_framerate_;
623 VEAClient::VEAClient(TestStream* test_stream,
624 ClientStateNotification<ClientState>* note,
626 unsigned int keyframe_period,
629 bool mid_stream_bitrate_switch,
630 bool mid_stream_framerate_switch)
631 : state_(CS_CREATED),
632 test_stream_(test_stream),
635 next_output_buffer_id_(0),
636 pos_in_input_stream_(0),
637 num_required_input_buffers_(0),
638 output_buffer_size_(0),
639 num_frames_to_encode_(0),
640 num_encoded_frames_(0),
641 num_frames_since_last_check_(0),
642 seen_keyframe_in_this_buffer_(false),
643 save_to_file_(save_to_file),
644 keyframe_period_(keyframe_period),
645 keyframe_requested_at_(kMaxFrameNum),
646 force_bitrate_(force_bitrate),
647 current_requested_bitrate_(0),
648 current_framerate_(0),
649 encoded_stream_size_since_last_check_(0),
650 test_perf_(test_perf),
651 requested_bitrate_(0),
652 requested_framerate_(0),
653 requested_subsequent_bitrate_(0),
654 requested_subsequent_framerate_(0) {
655 if (keyframe_period_)
656 CHECK_LT(kMaxKeyframeDelay, keyframe_period_);
658 validator_ = StreamValidator::Create(
659 test_stream_->requested_profile,
660 base::Bind(&VEAClient::HandleEncodedFrame, base::Unretained(this)));
662 CHECK(validator_.get());
665 CHECK(!test_stream_->out_filename.empty());
666 base::FilePath out_filename(test_stream_->out_filename);
667 // This creates or truncates out_filename.
668 // Without it, AppendToFile() will not work.
669 EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0));
672 // Initialize the parameters of the test streams.
673 UpdateTestStreamData(mid_stream_bitrate_switch, mid_stream_framerate_switch);
675 thread_checker_.DetachFromThread();
678 VEAClient::~VEAClient() { CHECK(!has_encoder()); }
680 void VEAClient::CreateEncoder() {
681 DCHECK(thread_checker_.CalledOnValidThread());
682 CHECK(!has_encoder());
684 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
685 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder);
686 encoder_.reset(new V4L2VideoEncodeAccelerator(device.Pass()));
687 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
688 encoder_.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay()));
691 SetState(CS_ENCODER_SET);
693 DVLOG(1) << "Profile: " << test_stream_->requested_profile
694 << ", initial bitrate: " << requested_bitrate_;
695 if (!encoder_->Initialize(kInputFormat,
696 test_stream_->visible_size,
697 test_stream_->requested_profile,
700 DLOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed";
705 SetStreamParameters(requested_bitrate_, requested_framerate_);
706 SetState(CS_INITIALIZED);
709 void VEAClient::DestroyEncoder() {
710 DCHECK(thread_checker_.CalledOnValidThread());
716 void VEAClient::UpdateTestStreamData(bool mid_stream_bitrate_switch,
717 bool mid_stream_framerate_switch) {
718 // Use defaults for bitrate/framerate if they are not provided.
719 if (test_stream_->requested_bitrate == 0)
720 requested_bitrate_ = kDefaultBitrate;
722 requested_bitrate_ = test_stream_->requested_bitrate;
724 if (test_stream_->requested_framerate == 0)
725 requested_framerate_ = kDefaultFramerate;
727 requested_framerate_ = test_stream_->requested_framerate;
729 // If bitrate/framerate switch is requested, use the subsequent values if
730 // provided, or, if not, calculate them from their initial values using
731 // the default ratios.
732 // Otherwise, if a switch is not requested, keep the initial values.
733 if (mid_stream_bitrate_switch) {
734 if (test_stream_->requested_subsequent_bitrate == 0)
735 requested_subsequent_bitrate_ =
736 requested_bitrate_ * kDefaultSubsequentBitrateRatio;
738 requested_subsequent_bitrate_ =
739 test_stream_->requested_subsequent_bitrate;
741 requested_subsequent_bitrate_ = requested_bitrate_;
743 if (requested_subsequent_bitrate_ == 0)
744 requested_subsequent_bitrate_ = 1;
746 if (mid_stream_framerate_switch) {
747 if (test_stream_->requested_subsequent_framerate == 0)
748 requested_subsequent_framerate_ =
749 requested_framerate_ * kDefaultSubsequentFramerateRatio;
751 requested_subsequent_framerate_ =
752 test_stream_->requested_subsequent_framerate;
754 requested_subsequent_framerate_ = requested_framerate_;
756 if (requested_subsequent_framerate_ == 0)
757 requested_subsequent_framerate_ = 1;
760 double VEAClient::frames_per_second() {
761 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_;
762 return num_encoded_frames_ / duration.InSecondsF();
765 void VEAClient::RequireBitstreamBuffers(unsigned int input_count,
766 const gfx::Size& input_coded_size,
767 size_t output_size) {
768 DCHECK(thread_checker_.CalledOnValidThread());
769 ASSERT_EQ(state_, CS_INITIALIZED);
770 SetState(CS_ENCODING);
772 CreateAlignedInputStreamFile(input_coded_size, test_stream_);
774 // We may need to loop over the stream more than once if more frames than
775 // provided is required for bitrate tests.
776 if (force_bitrate_ && test_stream_->num_frames < kMinFramesForBitrateTests) {
777 DVLOG(1) << "Stream too short for bitrate test ("
778 << test_stream_->num_frames << " frames), will loop it to reach "
779 << kMinFramesForBitrateTests << " frames";
780 num_frames_to_encode_ = kMinFramesForBitrateTests;
782 num_frames_to_encode_ = test_stream_->num_frames;
785 input_coded_size_ = input_coded_size;
786 num_required_input_buffers_ = input_count;
787 ASSERT_GT(num_required_input_buffers_, 0UL);
789 output_buffer_size_ = output_size;
790 ASSERT_GT(output_buffer_size_, 0UL);
792 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) {
793 base::SharedMemory* shm = new base::SharedMemory();
794 CHECK(shm->CreateAndMapAnonymous(output_buffer_size_));
795 output_shms_.push_back(shm);
796 FeedEncoderWithOutput(shm);
799 encode_start_time_ = base::TimeTicks::Now();
800 FeedEncoderWithInputs();
803 void VEAClient::BitstreamBufferReady(int32 bitstream_buffer_id,
806 DCHECK(thread_checker_.CalledOnValidThread());
807 ASSERT_LE(payload_size, output_buffer_size_);
809 IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id);
810 ASSERT_NE(it, output_buffers_at_client_.end());
811 base::SharedMemory* shm = it->second;
812 output_buffers_at_client_.erase(it);
814 if (state_ == CS_FINISHED)
817 encoded_stream_size_since_last_check_ += payload_size;
819 const uint8* stream_ptr = static_cast<const uint8*>(shm->memory());
820 if (payload_size > 0)
821 validator_->ProcessStreamBuffer(stream_ptr, payload_size);
823 EXPECT_EQ(key_frame, seen_keyframe_in_this_buffer_);
824 seen_keyframe_in_this_buffer_ = false;
827 int size = base::checked_cast<int>(payload_size);
828 EXPECT_EQ(base::AppendToFile(
829 base::FilePath::FromUTF8Unsafe(test_stream_->out_filename),
830 static_cast<char*>(shm->memory()),
835 FeedEncoderWithOutput(shm);
838 void VEAClient::NotifyError(VideoEncodeAccelerator::Error error) {
839 DCHECK(thread_checker_.CalledOnValidThread());
843 void VEAClient::SetState(ClientState new_state) {
844 DVLOG(4) << "Changing state " << state_ << "->" << new_state;
845 note_->Notify(new_state);
849 void VEAClient::SetStreamParameters(unsigned int bitrate,
850 unsigned int framerate) {
851 current_requested_bitrate_ = bitrate;
852 current_framerate_ = framerate;
853 CHECK_GT(current_requested_bitrate_, 0UL);
854 CHECK_GT(current_framerate_, 0UL);
855 encoder_->RequestEncodingParametersChange(current_requested_bitrate_,
857 DVLOG(1) << "Switched parameters to " << current_requested_bitrate_
858 << " bps @ " << current_framerate_ << " FPS";
861 void VEAClient::InputNoLongerNeededCallback(int32 input_id) {
862 std::set<int32>::iterator it = inputs_at_client_.find(input_id);
863 ASSERT_NE(it, inputs_at_client_.end());
864 inputs_at_client_.erase(it);
865 FeedEncoderWithInputs();
868 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) {
869 CHECK_LE(position + test_stream_->aligned_buffer_size,
870 test_stream_->mapped_aligned_in_file.length());
872 uint8* frame_data_y = const_cast<uint8*>(
873 test_stream_->mapped_aligned_in_file.data() + position);
874 uint8* frame_data_u = frame_data_y + test_stream_->aligned_plane_size[0];
875 uint8* frame_data_v = frame_data_u + test_stream_->aligned_plane_size[1];
877 CHECK_GT(current_framerate_, 0U);
878 scoped_refptr<media::VideoFrame> frame =
879 media::VideoFrame::WrapExternalYuvData(
882 gfx::Rect(test_stream_->visible_size),
883 test_stream_->visible_size,
884 input_coded_size_.width(),
885 input_coded_size_.width() / 2,
886 input_coded_size_.width() / 2,
890 base::TimeDelta().FromMilliseconds(
891 next_input_id_ * base::Time::kMillisecondsPerSecond /
893 media::BindToCurrentLoop(
894 base::Bind(&VEAClient::InputNoLongerNeededCallback,
895 base::Unretained(this),
898 CHECK(inputs_at_client_.insert(next_input_id_).second);
904 void VEAClient::FeedEncoderWithInputs() {
908 if (state_ != CS_ENCODING)
911 while (inputs_at_client_.size() <
912 num_required_input_buffers_ + kNumExtraInputFrames) {
914 test_stream_->mapped_aligned_in_file.length() - pos_in_input_stream_;
915 if (bytes_left < test_stream_->aligned_buffer_size) {
916 DCHECK_EQ(bytes_left, 0UL);
917 // Rewind if at the end of stream and we are still encoding.
918 // This is to flush the encoder with additional frames from the beginning
919 // of the stream, or if the stream is shorter that the number of frames
920 // we require for bitrate tests.
921 pos_in_input_stream_ = 0;
925 bool force_keyframe = false;
926 if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) {
927 keyframe_requested_at_ = next_input_id_;
928 force_keyframe = true;
931 scoped_refptr<media::VideoFrame> video_frame =
932 PrepareInputFrame(pos_in_input_stream_);
933 pos_in_input_stream_ += test_stream_->aligned_buffer_size;
935 encoder_->Encode(video_frame, force_keyframe);
939 void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) {
943 if (state_ != CS_ENCODING)
946 base::SharedMemoryHandle dup_handle;
947 CHECK(shm->ShareToProcess(base::Process::Current().handle(), &dup_handle));
949 media::BitstreamBuffer bitstream_buffer(
950 next_output_buffer_id_++, dup_handle, output_buffer_size_);
951 CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(),
953 encoder_->UseOutputBitstreamBuffer(bitstream_buffer);
956 bool VEAClient::HandleEncodedFrame(bool keyframe) {
957 // This would be a bug in the test, which should not ignore false
958 // return value from this method.
959 CHECK_LE(num_encoded_frames_, num_frames_to_encode_);
961 ++num_encoded_frames_;
962 ++num_frames_since_last_check_;
964 last_frame_ready_time_ = base::TimeTicks::Now();
966 // Got keyframe, reset keyframe detection regardless of whether we
967 // got a frame in time or not.
968 keyframe_requested_at_ = kMaxFrameNum;
969 seen_keyframe_in_this_buffer_ = true;
972 // Because the keyframe behavior requirements are loose, we give
973 // the encoder more freedom here. It could either deliver a keyframe
974 // immediately after we requested it, which could be for a frame number
975 // before the one we requested it for (if the keyframe request
976 // is asynchronous, i.e. not bound to any concrete frame, and because
977 // the pipeline can be deeper than one frame), at that frame, or after.
978 // So the only constraints we put here is that we get a keyframe not
979 // earlier than we requested one (in time), and not later than
980 // kMaxKeyframeDelay frames after the frame, for which we requested
981 // it, comes back encoded.
982 EXPECT_LE(num_encoded_frames_, keyframe_requested_at_ + kMaxKeyframeDelay);
984 if (num_encoded_frames_ == num_frames_to_encode_ / 2) {
985 VerifyStreamProperties();
986 if (requested_subsequent_bitrate_ != current_requested_bitrate_ ||
987 requested_subsequent_framerate_ != current_framerate_) {
988 SetStreamParameters(requested_subsequent_bitrate_,
989 requested_subsequent_framerate_);
991 } else if (num_encoded_frames_ == num_frames_to_encode_) {
993 VerifyStreamProperties();
994 SetState(CS_FINISHED);
1001 void VEAClient::VerifyPerf() {
1002 double measured_fps = frames_per_second();
1003 LOG(INFO) << "Measured encoder FPS: " << measured_fps;
1005 EXPECT_GE(measured_fps, kMinPerfFPS);
1008 void VEAClient::VerifyStreamProperties() {
1009 CHECK_GT(num_frames_since_last_check_, 0UL);
1010 CHECK_GT(encoded_stream_size_since_last_check_, 0UL);
1011 unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 *
1012 current_framerate_ / num_frames_since_last_check_;
1013 DVLOG(1) << "Current chunk's bitrate: " << bitrate
1014 << " (expected: " << current_requested_bitrate_
1015 << " @ " << current_framerate_ << " FPS,"
1016 << " num frames in chunk: " << num_frames_since_last_check_;
1018 num_frames_since_last_check_ = 0;
1019 encoded_stream_size_since_last_check_ = 0;
1021 if (force_bitrate_) {
1022 EXPECT_NEAR(bitrate,
1023 current_requested_bitrate_,
1024 kBitrateTolerance * current_requested_bitrate_);
1028 // Setup test stream data and delete temporary aligned files at the beginning
1029 // and end of unittest. We only need to setup once for all test cases.
1030 class VideoEncodeAcceleratorTestEnvironment : public ::testing::Environment {
1032 VideoEncodeAcceleratorTestEnvironment(
1033 scoped_ptr<base::FilePath::StringType> data) {
1034 test_stream_data_ = data.Pass();
1037 virtual void SetUp() {
1038 ParseAndReadTestStreamData(*test_stream_data_, &test_streams_);
1041 virtual void TearDown() {
1042 for (size_t i = 0; i < test_streams_.size(); i++) {
1043 base::DeleteFile(test_streams_[i]->aligned_in_file, false);
1047 ScopedVector<TestStream> test_streams_;
1050 scoped_ptr<base::FilePath::StringType> test_stream_data_;
1054 // - Number of concurrent encoders.
1055 // - If true, save output to file (provided an output filename was supplied).
1056 // - Force a keyframe every n frames.
1057 // - Force bitrate; the actual required value is provided as a property
1058 // of the input stream, because it depends on stream type/resolution/etc.
1059 // - If true, measure performance.
1060 // - If true, switch bitrate mid-stream.
1061 // - If true, switch framerate mid-stream.
1062 class VideoEncodeAcceleratorTest
1063 : public ::testing::TestWithParam<
1064 Tuple7<int, bool, int, bool, bool, bool, bool> > {};
1066 TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) {
1067 const size_t num_concurrent_encoders = GetParam().a;
1068 const bool save_to_file = GetParam().b;
1069 const unsigned int keyframe_period = GetParam().c;
1070 const bool force_bitrate = GetParam().d;
1071 const bool test_perf = GetParam().e;
1072 const bool mid_stream_bitrate_switch = GetParam().f;
1073 const bool mid_stream_framerate_switch = GetParam().g;
1075 ScopedVector<ClientStateNotification<ClientState> > notes;
1076 ScopedVector<VEAClient> clients;
1077 base::Thread encoder_thread("EncoderThread");
1078 ASSERT_TRUE(encoder_thread.Start());
1080 // Create all encoders.
1081 for (size_t i = 0; i < num_concurrent_encoders; i++) {
1082 size_t test_stream_index = i % g_env->test_streams_.size();
1083 // Disregard save_to_file if we didn't get an output filename.
1084 bool encoder_save_to_file =
1086 !g_env->test_streams_[test_stream_index]->out_filename.empty());
1088 notes.push_back(new ClientStateNotification<ClientState>());
1089 clients.push_back(new VEAClient(g_env->test_streams_[test_stream_index],
1091 encoder_save_to_file,
1095 mid_stream_bitrate_switch,
1096 mid_stream_framerate_switch));
1098 encoder_thread.message_loop()->PostTask(
1100 base::Bind(&VEAClient::CreateEncoder,
1101 base::Unretained(clients.back())));
1104 // All encoders must pass through states in this order.
1105 enum ClientState state_transitions[] = {CS_ENCODER_SET, CS_INITIALIZED,
1106 CS_ENCODING, CS_FINISHED};
1108 // Wait for all encoders to go through all states and finish.
1109 // Do this by waiting for all encoders to advance to state n before checking
1110 // state n+1, to verify that they are able to operate concurrently.
1111 // It also simulates the real-world usage better, as the main thread, on which
1112 // encoders are created/destroyed, is a single GPU Process ChildThread.
1113 // Moreover, we can't have proper multithreading on X11, so this could cause
1114 // hard to debug issues there, if there were multiple "ChildThreads".
1115 for (size_t state_no = 0; state_no < arraysize(state_transitions); ++state_no)
1116 for (size_t i = 0; i < num_concurrent_encoders; i++)
1117 ASSERT_EQ(notes[i]->Wait(), state_transitions[state_no]);
1119 for (size_t i = 0; i < num_concurrent_encoders; ++i) {
1120 encoder_thread.message_loop()->PostTask(
1122 base::Bind(&VEAClient::DestroyEncoder, base::Unretained(clients[i])));
1125 // This ensures all tasks have finished.
1126 encoder_thread.Stop();
1129 INSTANTIATE_TEST_CASE_P(
1131 VideoEncodeAcceleratorTest,
1132 ::testing::Values(MakeTuple(1, true, 0, false, false, false, false)));
1134 INSTANTIATE_TEST_CASE_P(
1136 VideoEncodeAcceleratorTest,
1137 ::testing::Values(MakeTuple(1, false, 0, false, true, false, false)));
1139 INSTANTIATE_TEST_CASE_P(
1141 VideoEncodeAcceleratorTest,
1142 ::testing::Values(MakeTuple(1, false, 10, false, false, false, false)));
1144 INSTANTIATE_TEST_CASE_P(
1146 VideoEncodeAcceleratorTest,
1147 ::testing::Values(MakeTuple(1, false, 0, true, false, false, false)));
1149 INSTANTIATE_TEST_CASE_P(
1150 MidStreamParamSwitchBitrate,
1151 VideoEncodeAcceleratorTest,
1152 ::testing::Values(MakeTuple(1, false, 0, true, false, true, false)));
1154 INSTANTIATE_TEST_CASE_P(
1155 MidStreamParamSwitchFPS,
1156 VideoEncodeAcceleratorTest,
1157 ::testing::Values(MakeTuple(1, false, 0, true, false, false, true)));
1159 INSTANTIATE_TEST_CASE_P(
1160 MidStreamParamSwitchBitrateAndFPS,
1161 VideoEncodeAcceleratorTest,
1162 ::testing::Values(MakeTuple(1, false, 0, true, false, true, true)));
1164 INSTANTIATE_TEST_CASE_P(
1166 VideoEncodeAcceleratorTest,
1167 ::testing::Values(MakeTuple(3, false, 0, false, false, false, false),
1168 MakeTuple(3, false, 0, true, false, true, true)));
1170 // TODO(posciak): more tests:
1171 // - async FeedEncoderWithOutput
1172 // - out-of-order return of outputs to encoder
1173 // - multiple encoders + decoders
1174 // - mid-stream encoder_->Destroy()
1177 } // namespace content
1179 int main(int argc, char** argv) {
1180 testing::InitGoogleTest(&argc, argv); // Removes gtest-specific args.
1181 base::CommandLine::Init(argc, argv);
1183 base::ShadowingAtExitManager at_exit_manager;
1184 scoped_ptr<base::FilePath::StringType> test_stream_data(
1185 new base::FilePath::StringType(
1186 media::GetTestDataFilePath(content::g_default_in_filename).value() +
1187 content::g_default_in_parameters));
1189 // Needed to enable DVLOG through --vmodule.
1190 logging::LoggingSettings settings;
1191 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
1192 CHECK(logging::InitLogging(settings));
1194 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
1197 base::CommandLine::SwitchMap switches = cmd_line->GetSwitches();
1198 for (base::CommandLine::SwitchMap::const_iterator it = switches.begin();
1199 it != switches.end();
1201 if (it->first == "test_stream_data") {
1202 test_stream_data->assign(it->second.c_str());
1205 if (it->first == "v" || it->first == "vmodule")
1207 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second;
1211 reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>(
1212 testing::AddGlobalTestEnvironment(
1213 new content::VideoEncodeAcceleratorTestEnvironment(
1214 test_stream_data.Pass())));
1216 return RUN_ALL_TESTS();