Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: content/common/gpu/media/video_encode_accelerator_unittest.cc

Issue 1117853002: vea_unittest: Calculate per-frame encode latency (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Pawel's comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm>
6
5 #include "base/at_exit.h" 7 #include "base/at_exit.h"
6 #include "base/bind.h" 8 #include "base/bind.h"
7 #include "base/command_line.h" 9 #include "base/command_line.h"
8 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
9 #include "base/files/memory_mapped_file.h" 11 #include "base/files/memory_mapped_file.h"
10 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
11 #include "base/numerics/safe_conversions.h" 13 #include "base/numerics/safe_conversions.h"
12 #include "base/process/process_handle.h" 14 #include "base/process/process_handle.h"
13 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h" 16 #include "base/strings/string_split.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 // Tolerance factor for how encoded bitrate can differ from requested bitrate. 64 // Tolerance factor for how encoded bitrate can differ from requested bitrate.
63 const double kBitrateTolerance = 0.1; 65 const double kBitrateTolerance = 0.1;
64 // Minimum required FPS throughput for the basic performance test. 66 // Minimum required FPS throughput for the basic performance test.
65 const uint32 kMinPerfFPS = 30; 67 const uint32 kMinPerfFPS = 30;
66 // Minimum (arbitrary) number of frames required to enforce bitrate requirements 68 // Minimum (arbitrary) number of frames required to enforce bitrate requirements
67 // over. Streams shorter than this may be too short to realistically require 69 // 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. 70 // 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 71 // 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. 72 // to reach at least this number of frames before calculating final bitrate.
71 const unsigned int kMinFramesForBitrateTests = 300; 73 const unsigned int kMinFramesForBitrateTests = 300;
74 // The percentiles to measure for encode latency.
75 static unsigned int kLoggedLatencyPercentiles[] = {50, 75, 95};
72 76
73 // The syntax of multiple test streams is: 77 // The syntax of multiple test streams is:
74 // test-stream1;test-stream2;test-stream3 78 // test-stream1;test-stream2;test-stream3
75 // The syntax of each test stream is: 79 // The syntax of each test stream is:
76 // "in_filename:width:height:out_filename:requested_bitrate:requested_framerate 80 // "in_filename:width:height:out_filename:requested_bitrate:requested_framerate
77 // :requested_subsequent_bitrate:requested_subsequent_framerate" 81 // :requested_subsequent_bitrate:requested_subsequent_framerate"
78 // - |in_filename| must be an I420 (YUV planar) raw stream 82 // - |in_filename| must be an I420 (YUV planar) raw stream
79 // (see http://www.fourcc.org/yuv.php#IYUV). 83 // (see http://www.fourcc.org/yuv.php#IYUV).
80 // - |width| and |height| are in pixels. 84 // - |width| and |height| are in pixels.
81 // - |profile| to encode into (values of media::VideoCodecProfile). 85 // - |profile| to encode into (values of media::VideoCodecProfile).
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 int bytes = file->Write(offset + written_bytes, 186 int bytes = file->Write(offset + written_bytes,
183 reinterpret_cast<const char*>(data + written_bytes), 187 reinterpret_cast<const char*>(data + written_bytes),
184 size - written_bytes); 188 size - written_bytes);
185 if (bytes <= 0) 189 if (bytes <= 0)
186 return false; 190 return false;
187 written_bytes += bytes; 191 written_bytes += bytes;
188 } 192 }
189 return true; 193 return true;
190 } 194 }
191 195
196 // Return the |percentile| from a sorted vector.
197 static base::TimeDelta Percentile(
198 const std::vector<base::TimeDelta>& sorted_values,
199 unsigned int percentile) {
200 size_t size = sorted_values.size();
201 CHECK_GT(size, 0);
202 CHECK_LE(percentile, 100);
203 // Use Nearest Rank method in http://en.wikipedia.org/wiki/Percentile.
204 int index =
205 std::max(static_cast<int>(ceil(0.01f * percentile * size)) - 1, 0);
206 return sorted_values[index];
207 }
208
192 static bool IsH264(media::VideoCodecProfile profile) { 209 static bool IsH264(media::VideoCodecProfile profile) {
193 return profile >= media::H264PROFILE_MIN && profile <= media::H264PROFILE_MAX; 210 return profile >= media::H264PROFILE_MIN && profile <= media::H264PROFILE_MAX;
194 } 211 }
195 212
196 static bool IsVP8(media::VideoCodecProfile profile) { 213 static bool IsVP8(media::VideoCodecProfile profile) {
197 return profile >= media::VP8PROFILE_MIN && profile <= media::VP8PROFILE_MAX; 214 return profile >= media::VP8PROFILE_MIN && profile <= media::VP8PROFILE_MAX;
198 } 215 }
199 216
200 // ARM performs CPU cache management with CPU cache line granularity. We thus 217 // ARM performs CPU cache management with CPU cache line granularity. We thus
201 // need to ensure our buffers are CPU cache line-aligned (64 byte-aligned). 218 // need to ensure our buffers are CPU cache line-aligned (64 byte-aligned).
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 ParseAndReadTestStreamData(*test_stream_data_, &test_streams_); 391 ParseAndReadTestStreamData(*test_stream_data_, &test_streams_);
375 } 392 }
376 393
377 virtual void TearDown() { 394 virtual void TearDown() {
378 for (size_t i = 0; i < test_streams_.size(); i++) { 395 for (size_t i = 0; i < test_streams_.size(); i++) {
379 base::DeleteFile(test_streams_[i]->aligned_in_file, false); 396 base::DeleteFile(test_streams_[i]->aligned_in_file, false);
380 } 397 }
381 log_file_.reset(); 398 log_file_.reset();
382 } 399 }
383 400
384 // Log one entry of machine-readable data to file. 401 // Log one entry of machine-readable data to file and LOG(INFO).
385 // The log has one data entry per line in the format of "<key>: <value>". 402 // The log has one data entry per line in the format of "<key>: <value>".
403 // Note that Chrome OS video_VEAPerf autotest parses the output key and value
404 // pairs. Be sure to keep the autotest in sync.
386 void LogToFile(const std::string& key, const std::string& value) { 405 void LogToFile(const std::string& key, const std::string& value) {
406 std::string s = base::StringPrintf("%s: %s\n", key.c_str(), value.c_str());
407 LOG(INFO) << s;
387 if (log_file_) { 408 if (log_file_) {
388 std::string s =
389 base::StringPrintf("%s: %s\n", key.c_str(), value.c_str());
390 log_file_->WriteAtCurrentPos(s.data(), s.length()); 409 log_file_->WriteAtCurrentPos(s.data(), s.length());
391 } 410 }
392 } 411 }
393 412
394 ScopedVector<TestStream> test_streams_; 413 ScopedVector<TestStream> test_streams_;
395 bool run_at_fps_; 414 bool run_at_fps_;
396 415
397 private: 416 private:
398 scoped_ptr<base::FilePath::StringType> test_stream_data_; 417 scoped_ptr<base::FilePath::StringType> test_stream_data_;
399 base::FilePath log_path_; 418 base::FilePath log_path_;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 unsigned int keyframe_period, 576 unsigned int keyframe_period,
558 bool force_bitrate, 577 bool force_bitrate,
559 bool test_perf, 578 bool test_perf,
560 bool mid_stream_bitrate_switch, 579 bool mid_stream_bitrate_switch,
561 bool mid_stream_framerate_switch, 580 bool mid_stream_framerate_switch,
562 bool run_at_fps); 581 bool run_at_fps);
563 ~VEAClient() override; 582 ~VEAClient() override;
564 void CreateEncoder(); 583 void CreateEncoder();
565 void DestroyEncoder(); 584 void DestroyEncoder();
566 585
567 // Return the number of encoded frames per second.
568 double frames_per_second();
569
570 // VideoDecodeAccelerator::Client implementation. 586 // VideoDecodeAccelerator::Client implementation.
571 void RequireBitstreamBuffers(unsigned int input_count, 587 void RequireBitstreamBuffers(unsigned int input_count,
572 const gfx::Size& input_coded_size, 588 const gfx::Size& input_coded_size,
573 size_t output_buffer_size) override; 589 size_t output_buffer_size) override;
574 void BitstreamBufferReady(int32 bitstream_buffer_id, 590 void BitstreamBufferReady(int32 bitstream_buffer_id,
575 size_t payload_size, 591 size_t payload_size,
576 bool key_frame) override; 592 bool key_frame) override;
577 void NotifyError(VideoEncodeAccelerator::Error error) override; 593 void NotifyError(VideoEncodeAccelerator::Error error) override;
578 594
579 private: 595 private:
580 bool has_encoder() { return encoder_.get(); } 596 bool has_encoder() { return encoder_.get(); }
581 597
598 // Encode latency can only be measured with run_at_fps_. Otherwise, we get
599 // skewed results since it may queue too many frames at once with the same
600 // encode start time.
601 bool needs_encode_latency() { return run_at_fps_; }
602
603 // Return the number of encoded frames per second.
604 double frames_per_second();
605
582 scoped_ptr<media::VideoEncodeAccelerator> CreateFakeVEA(); 606 scoped_ptr<media::VideoEncodeAccelerator> CreateFakeVEA();
583 scoped_ptr<media::VideoEncodeAccelerator> CreateV4L2VEA(); 607 scoped_ptr<media::VideoEncodeAccelerator> CreateV4L2VEA();
584 scoped_ptr<media::VideoEncodeAccelerator> CreateVaapiVEA(); 608 scoped_ptr<media::VideoEncodeAccelerator> CreateVaapiVEA();
585 609
586 void SetState(ClientState new_state); 610 void SetState(ClientState new_state);
587 611
588 // Set current stream parameters to given |bitrate| at |framerate|. 612 // Set current stream parameters to given |bitrate| at |framerate|.
589 void SetStreamParameters(unsigned int bitrate, unsigned int framerate); 613 void SetStreamParameters(unsigned int bitrate, unsigned int framerate);
590 614
591 // Called when encoder is done with a VideoFrame. 615 // Called when encoder is done with a VideoFrame.
592 void InputNoLongerNeededCallback(int32 input_id); 616 void InputNoLongerNeededCallback(int32 input_id);
593 617
594 // Feed the encoder with one input frame. 618 // Feed the encoder with one input frame.
595 void FeedEncoderWithOneInput(); 619 void FeedEncoderWithOneInput();
596 620
597 // Provide the encoder with a new output buffer. 621 // Provide the encoder with a new output buffer.
598 void FeedEncoderWithOutput(base::SharedMemory* shm); 622 void FeedEncoderWithOutput(base::SharedMemory* shm);
599 623
600 // Called on finding a complete frame (with |keyframe| set to true for 624 // Called on finding a complete frame (with |keyframe| set to true for
601 // keyframes) in the stream, to perform codec-independent, per-frame checks 625 // keyframes) in the stream, to perform codec-independent, per-frame checks
602 // and accounting. Returns false once we have collected all frames we needed. 626 // and accounting. Returns false once we have collected all frames we needed.
603 bool HandleEncodedFrame(bool keyframe); 627 bool HandleEncodedFrame(bool keyframe);
604 628
629 // Verify the minimum FPS requirement.
630 void VerifyMinFPS();
631
605 // Verify that stream bitrate has been close to current_requested_bitrate_, 632 // Verify that stream bitrate has been close to current_requested_bitrate_,
606 // assuming current_framerate_ since the last time VerifyStreamProperties() 633 // assuming current_framerate_ since the last time VerifyStreamProperties()
607 // was called. Fail the test if |force_bitrate_| is true and the bitrate 634 // was called. Fail the test if |force_bitrate_| is true and the bitrate
608 // is not within kBitrateTolerance. 635 // is not within kBitrateTolerance.
609 void VerifyStreamProperties(); 636 void VerifyStreamProperties();
610 637
611 // Test codec performance, failing the test if we are currently running 638 // Log the performance data.
612 // the performance test. 639 void LogPerf();
613 void VerifyPerf();
614 640
615 // Write IVF file header to test_stream_->out_filename. 641 // Write IVF file header to test_stream_->out_filename.
616 void WriteIvfFileHeader(); 642 void WriteIvfFileHeader();
617 643
618 // Write an IVF frame header to test_stream_->out_filename. 644 // Write an IVF frame header to test_stream_->out_filename.
619 void WriteIvfFrameHeader(int frame_index, size_t frame_size); 645 void WriteIvfFrameHeader(int frame_index, size_t frame_size);
620 646
621 // Prepare and return a frame wrapping the data at |position| bytes in 647 // Prepare and return a frame wrapping the data at |position| bytes in the
622 // the input stream, ready to be sent to encoder. 648 // input stream, ready to be sent to encoder.
623 scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position); 649 // The input frame id is returned in |input_id|.
650 scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position,
651 int32* input_id);
624 652
625 // Update the parameters according to |mid_stream_bitrate_switch| and 653 // Update the parameters according to |mid_stream_bitrate_switch| and
626 // |mid_stream_framerate_switch|. 654 // |mid_stream_framerate_switch|.
627 void UpdateTestStreamData(bool mid_stream_bitrate_switch, 655 void UpdateTestStreamData(bool mid_stream_bitrate_switch,
628 bool mid_stream_framerate_switch); 656 bool mid_stream_framerate_switch);
629 657
630 // Callback function of the |input_timer_|. 658 // Callback function of the |input_timer_|.
631 void OnInputTimer(); 659 void OnInputTimer();
632 660
633 ClientState state_; 661 ClientState state_;
634 scoped_ptr<VideoEncodeAccelerator> encoder_; 662 scoped_ptr<VideoEncodeAccelerator> encoder_;
635 663
636 TestStream* test_stream_; 664 TestStream* test_stream_;
637 665
638 // Used to notify another thread about the state. VEAClient does not own this. 666 // Used to notify another thread about the state. VEAClient does not own this.
639 ClientStateNotification<ClientState>* note_; 667 ClientStateNotification<ClientState>* note_;
640 668
641 // Ids assigned to VideoFrames (start at 1 for easy comparison with 669 // Ids assigned to VideoFrames.
642 // num_encoded_frames_).
643 std::set<int32> inputs_at_client_; 670 std::set<int32> inputs_at_client_;
644 int32 next_input_id_; 671 int32 next_input_id_;
645 672
673 // Encode start time of all encoded frames. The position in the vector is the
674 // frame input id.
675 std::vector<base::TimeTicks> encode_start_time_;
676 // The encode latencies of all encoded frames. We define encode latency as the
677 // time delay from input of each VideoFrame (VEA::Encode()) to output of the
678 // corresponding BitstreamBuffer (VEA::Client::BitstreamBufferReady()).
679 std::vector<base::TimeDelta> encode_latencies_;
680
646 // Ids for output BitstreamBuffers. 681 // Ids for output BitstreamBuffers.
647 typedef std::map<int32, base::SharedMemory*> IdToSHM; 682 typedef std::map<int32, base::SharedMemory*> IdToSHM;
648 ScopedVector<base::SharedMemory> output_shms_; 683 ScopedVector<base::SharedMemory> output_shms_;
649 IdToSHM output_buffers_at_client_; 684 IdToSHM output_buffers_at_client_;
650 int32 next_output_buffer_id_; 685 int32 next_output_buffer_id_;
651 686
652 // Current offset into input stream. 687 // Current offset into input stream.
653 off_t pos_in_input_stream_; 688 off_t pos_in_input_stream_;
654 gfx::Size input_coded_size_; 689 gfx::Size input_coded_size_;
655 // Requested by encoder. 690 // Requested by encoder.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 727
693 // Byte size of the encoded stream (for bitrate calculation) since last 728 // Byte size of the encoded stream (for bitrate calculation) since last
694 // time we checked bitrate. 729 // time we checked bitrate.
695 size_t encoded_stream_size_since_last_check_; 730 size_t encoded_stream_size_since_last_check_;
696 731
697 // If true, verify performance at the end of the test. 732 // If true, verify performance at the end of the test.
698 bool test_perf_; 733 bool test_perf_;
699 734
700 scoped_ptr<StreamValidator> validator_; 735 scoped_ptr<StreamValidator> validator_;
701 736
702 // The time when the encoding started.
703 base::TimeTicks encode_start_time_;
704
705 // The time when the last encoded frame is ready. 737 // The time when the last encoded frame is ready.
706 base::TimeTicks last_frame_ready_time_; 738 base::TimeTicks last_frame_ready_time_;
707 739
708 // All methods of this class should be run on the same thread. 740 // All methods of this class should be run on the same thread.
709 base::ThreadChecker thread_checker_; 741 base::ThreadChecker thread_checker_;
710 742
711 // Requested bitrate in bits per second. 743 // Requested bitrate in bits per second.
712 unsigned int requested_bitrate_; 744 unsigned int requested_bitrate_;
713 745
714 // Requested initial framerate. 746 // Requested initial framerate.
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 requested_subsequent_framerate_ = 932 requested_subsequent_framerate_ =
901 test_stream_->requested_subsequent_framerate; 933 test_stream_->requested_subsequent_framerate;
902 } else { 934 } else {
903 requested_subsequent_framerate_ = requested_framerate_; 935 requested_subsequent_framerate_ = requested_framerate_;
904 } 936 }
905 if (requested_subsequent_framerate_ == 0) 937 if (requested_subsequent_framerate_ == 0)
906 requested_subsequent_framerate_ = 1; 938 requested_subsequent_framerate_ = 1;
907 } 939 }
908 940
909 double VEAClient::frames_per_second() { 941 double VEAClient::frames_per_second() {
910 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_; 942 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_[0];
911 return num_encoded_frames_ / duration.InSecondsF(); 943 return num_encoded_frames_ / duration.InSecondsF();
912 } 944 }
913 945
914 void VEAClient::RequireBitstreamBuffers(unsigned int input_count, 946 void VEAClient::RequireBitstreamBuffers(unsigned int input_count,
915 const gfx::Size& input_coded_size, 947 const gfx::Size& input_coded_size,
916 size_t output_size) { 948 size_t output_size) {
917 DCHECK(thread_checker_.CalledOnValidThread()); 949 DCHECK(thread_checker_.CalledOnValidThread());
918 ASSERT_EQ(state_, CS_INITIALIZED); 950 ASSERT_EQ(state_, CS_INITIALIZED);
919 SetState(CS_ENCODING); 951 SetState(CS_ENCODING);
920 952
921 CreateAlignedInputStreamFile(input_coded_size, test_stream_); 953 CreateAlignedInputStreamFile(input_coded_size, test_stream_);
922 954
923 num_frames_to_encode_ = test_stream_->num_frames; 955 num_frames_to_encode_ = test_stream_->num_frames;
924 if (g_num_frames_to_encode > 0) 956 if (g_num_frames_to_encode > 0)
925 num_frames_to_encode_ = g_num_frames_to_encode; 957 num_frames_to_encode_ = g_num_frames_to_encode;
926 958
959 // Speed up vector insertion.
960 encode_start_time_.reserve(num_frames_to_encode_);
961 if (needs_encode_latency())
962 encode_latencies_.reserve(num_frames_to_encode_);
963
927 // We may need to loop over the stream more than once if more frames than 964 // We may need to loop over the stream more than once if more frames than
928 // provided is required for bitrate tests. 965 // provided is required for bitrate tests.
929 if (force_bitrate_ && num_frames_to_encode_ < kMinFramesForBitrateTests) { 966 if (force_bitrate_ && num_frames_to_encode_ < kMinFramesForBitrateTests) {
930 DVLOG(1) << "Stream too short for bitrate test (" 967 DVLOG(1) << "Stream too short for bitrate test ("
931 << test_stream_->num_frames << " frames), will loop it to reach " 968 << test_stream_->num_frames << " frames), will loop it to reach "
932 << kMinFramesForBitrateTests << " frames"; 969 << kMinFramesForBitrateTests << " frames";
933 num_frames_to_encode_ = kMinFramesForBitrateTests; 970 num_frames_to_encode_ = kMinFramesForBitrateTests;
934 } 971 }
935 if (save_to_file_ && IsVP8(test_stream_->requested_profile)) 972 if (save_to_file_ && IsVP8(test_stream_->requested_profile))
936 WriteIvfFileHeader(); 973 WriteIvfFileHeader();
937 974
938 input_coded_size_ = input_coded_size; 975 input_coded_size_ = input_coded_size;
939 num_required_input_buffers_ = input_count; 976 num_required_input_buffers_ = input_count;
940 ASSERT_GT(num_required_input_buffers_, 0UL); 977 ASSERT_GT(num_required_input_buffers_, 0UL);
941 978
942 output_buffer_size_ = output_size; 979 output_buffer_size_ = output_size;
943 ASSERT_GT(output_buffer_size_, 0UL); 980 ASSERT_GT(output_buffer_size_, 0UL);
944 981
945 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) { 982 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) {
946 base::SharedMemory* shm = new base::SharedMemory(); 983 base::SharedMemory* shm = new base::SharedMemory();
947 CHECK(shm->CreateAndMapAnonymous(output_buffer_size_)); 984 CHECK(shm->CreateAndMapAnonymous(output_buffer_size_));
948 output_shms_.push_back(shm); 985 output_shms_.push_back(shm);
949 FeedEncoderWithOutput(shm); 986 FeedEncoderWithOutput(shm);
950 } 987 }
951 988
952 encode_start_time_ = base::TimeTicks::Now();
953 if (run_at_fps_) { 989 if (run_at_fps_) {
954 input_timer_.reset(new base::RepeatingTimer<VEAClient>()); 990 input_timer_.reset(new base::RepeatingTimer<VEAClient>());
955 input_timer_->Start( 991 input_timer_->Start(
956 FROM_HERE, base::TimeDelta::FromSeconds(1) / current_framerate_, 992 FROM_HERE, base::TimeDelta::FromSeconds(1) / current_framerate_,
957 base::Bind(&VEAClient::OnInputTimer, base::Unretained(this))); 993 base::Bind(&VEAClient::OnInputTimer, base::Unretained(this)));
958 } else { 994 } else {
959 while (inputs_at_client_.size() < 995 while (inputs_at_client_.size() <
960 num_required_input_buffers_ + kNumExtraInputFrames) 996 num_required_input_buffers_ + kNumExtraInputFrames)
961 FeedEncoderWithOneInput(); 997 FeedEncoderWithOneInput();
962 } 998 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1027 } 1063 }
1028 1064
1029 void VEAClient::InputNoLongerNeededCallback(int32 input_id) { 1065 void VEAClient::InputNoLongerNeededCallback(int32 input_id) {
1030 std::set<int32>::iterator it = inputs_at_client_.find(input_id); 1066 std::set<int32>::iterator it = inputs_at_client_.find(input_id);
1031 ASSERT_NE(it, inputs_at_client_.end()); 1067 ASSERT_NE(it, inputs_at_client_.end());
1032 inputs_at_client_.erase(it); 1068 inputs_at_client_.erase(it);
1033 if (!run_at_fps_) 1069 if (!run_at_fps_)
1034 FeedEncoderWithOneInput(); 1070 FeedEncoderWithOneInput();
1035 } 1071 }
1036 1072
1037 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { 1073 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position,
1074 int32* input_id) {
1038 CHECK_LE(position + test_stream_->aligned_buffer_size, 1075 CHECK_LE(position + test_stream_->aligned_buffer_size,
1039 test_stream_->mapped_aligned_in_file.length()); 1076 test_stream_->mapped_aligned_in_file.length());
1040 1077
1041 uint8* frame_data_y = const_cast<uint8*>( 1078 uint8* frame_data_y = const_cast<uint8*>(
1042 test_stream_->mapped_aligned_in_file.data() + position); 1079 test_stream_->mapped_aligned_in_file.data() + position);
1043 uint8* frame_data_u = frame_data_y + test_stream_->aligned_plane_size[0]; 1080 uint8* frame_data_u = frame_data_y + test_stream_->aligned_plane_size[0];
1044 uint8* frame_data_v = frame_data_u + test_stream_->aligned_plane_size[1]; 1081 uint8* frame_data_v = frame_data_u + test_stream_->aligned_plane_size[1];
1045 1082
1046 CHECK_GT(current_framerate_, 0U); 1083 CHECK_GT(current_framerate_, 0U);
1047 scoped_refptr<media::VideoFrame> frame = 1084 scoped_refptr<media::VideoFrame> frame =
(...skipping 10 matching lines...) Expand all
1058 frame_data_v, 1095 frame_data_v,
1059 base::TimeDelta().FromMilliseconds( 1096 base::TimeDelta().FromMilliseconds(
1060 next_input_id_ * base::Time::kMillisecondsPerSecond / 1097 next_input_id_ * base::Time::kMillisecondsPerSecond /
1061 current_framerate_), 1098 current_framerate_),
1062 media::BindToCurrentLoop( 1099 media::BindToCurrentLoop(
1063 base::Bind(&VEAClient::InputNoLongerNeededCallback, 1100 base::Bind(&VEAClient::InputNoLongerNeededCallback,
1064 base::Unretained(this), 1101 base::Unretained(this),
1065 next_input_id_))); 1102 next_input_id_)));
1066 1103
1067 CHECK(inputs_at_client_.insert(next_input_id_).second); 1104 CHECK(inputs_at_client_.insert(next_input_id_).second);
1068 ++next_input_id_;
1069 1105
1106 *input_id = next_input_id_++;
1070 return frame; 1107 return frame;
1071 } 1108 }
1072 1109
1073 void VEAClient::OnInputTimer() { 1110 void VEAClient::OnInputTimer() {
1074 if (!has_encoder() || state_ != CS_ENCODING) 1111 if (!has_encoder() || state_ != CS_ENCODING)
1075 input_timer_.reset(); 1112 input_timer_.reset();
1076 else if (inputs_at_client_.size() < 1113 else if (inputs_at_client_.size() <
1077 num_required_input_buffers_ + kNumExtraInputFrames) 1114 num_required_input_buffers_ + kNumExtraInputFrames)
1078 FeedEncoderWithOneInput(); 1115 FeedEncoderWithOneInput();
1079 else 1116 else
1080 DVLOG(1) << "Dropping input frame"; 1117 DVLOG(1) << "Dropping input frame";
1081 } 1118 }
1082 1119
1083 void VEAClient::FeedEncoderWithOneInput() { 1120 void VEAClient::FeedEncoderWithOneInput() {
1084 if (!has_encoder() || state_ != CS_ENCODING) 1121 if (!has_encoder() || state_ != CS_ENCODING)
1085 return; 1122 return;
1086 1123
1087 size_t bytes_left = 1124 size_t bytes_left =
1088 test_stream_->mapped_aligned_in_file.length() - pos_in_input_stream_; 1125 test_stream_->mapped_aligned_in_file.length() - pos_in_input_stream_;
1089 if (bytes_left < test_stream_->aligned_buffer_size) { 1126 if (bytes_left < test_stream_->aligned_buffer_size) {
1090 DCHECK_EQ(bytes_left, 0UL); 1127 DCHECK_EQ(bytes_left, 0UL);
1091 // Rewind if at the end of stream and we are still encoding. 1128 // Rewind if at the end of stream and we are still encoding.
1092 // This is to flush the encoder with additional frames from the beginning 1129 // This is to flush the encoder with additional frames from the beginning
1093 // of the stream, or if the stream is shorter that the number of frames 1130 // of the stream, or if the stream is shorter that the number of frames
1094 // we require for bitrate tests. 1131 // we require for bitrate tests.
1095 pos_in_input_stream_ = 0; 1132 pos_in_input_stream_ = 0;
1096 } 1133 }
1097 1134
1135 int32 input_id;
1136 scoped_refptr<media::VideoFrame> video_frame =
1137 PrepareInputFrame(pos_in_input_stream_, &input_id);
1138 pos_in_input_stream_ += test_stream_->aligned_buffer_size;
1139
1098 bool force_keyframe = false; 1140 bool force_keyframe = false;
1099 if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) { 1141 if (keyframe_period_ && input_id % keyframe_period_ == 0) {
1100 force_keyframe = true; 1142 force_keyframe = true;
1101 ++num_keyframes_requested_; 1143 ++num_keyframes_requested_;
1102 } 1144 }
1103 1145
1104 scoped_refptr<media::VideoFrame> video_frame = 1146 CHECK_EQ(input_id, static_cast<int32>(encode_start_time_.size()));
1105 PrepareInputFrame(pos_in_input_stream_); 1147 encode_start_time_.push_back(base::TimeTicks::Now());
1106 pos_in_input_stream_ += test_stream_->aligned_buffer_size;
1107
1108 encoder_->Encode(video_frame, force_keyframe); 1148 encoder_->Encode(video_frame, force_keyframe);
1109 } 1149 }
1110 1150
1111 void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) { 1151 void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) {
1112 if (!has_encoder()) 1152 if (!has_encoder())
1113 return; 1153 return;
1114 1154
1115 if (state_ != CS_ENCODING) 1155 if (state_ != CS_ENCODING)
1116 return; 1156 return;
1117 1157
1118 base::SharedMemoryHandle dup_handle; 1158 base::SharedMemoryHandle dup_handle;
1119 CHECK(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle)); 1159 CHECK(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle));
1120 1160
1121 media::BitstreamBuffer bitstream_buffer( 1161 media::BitstreamBuffer bitstream_buffer(
1122 next_output_buffer_id_++, dup_handle, output_buffer_size_); 1162 next_output_buffer_id_++, dup_handle, output_buffer_size_);
1123 CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(), 1163 CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(),
1124 shm)).second); 1164 shm)).second);
1125 encoder_->UseOutputBitstreamBuffer(bitstream_buffer); 1165 encoder_->UseOutputBitstreamBuffer(bitstream_buffer);
1126 } 1166 }
1127 1167
1128 bool VEAClient::HandleEncodedFrame(bool keyframe) { 1168 bool VEAClient::HandleEncodedFrame(bool keyframe) {
1129 // This would be a bug in the test, which should not ignore false 1169 // This would be a bug in the test, which should not ignore false
1130 // return value from this method. 1170 // return value from this method.
1131 CHECK_LE(num_encoded_frames_, num_frames_to_encode_); 1171 CHECK_LE(num_encoded_frames_, num_frames_to_encode_);
1132 1172
1173 last_frame_ready_time_ = base::TimeTicks::Now();
1174
1175 if (needs_encode_latency()) {
1176 base::TimeTicks start_time = encode_start_time_[num_encoded_frames_];
1177 CHECK(!start_time.is_null());
1178 encode_latencies_.push_back(last_frame_ready_time_ - start_time);
1179 }
1180
1133 ++num_encoded_frames_; 1181 ++num_encoded_frames_;
1134 ++num_frames_since_last_check_; 1182 ++num_frames_since_last_check_;
1135 1183
1136 last_frame_ready_time_ = base::TimeTicks::Now();
1137
1138 // Because the keyframe behavior requirements are loose, we give 1184 // Because the keyframe behavior requirements are loose, we give
1139 // the encoder more freedom here. It could either deliver a keyframe 1185 // the encoder more freedom here. It could either deliver a keyframe
1140 // immediately after we requested it, which could be for a frame number 1186 // immediately after we requested it, which could be for a frame number
1141 // before the one we requested it for (if the keyframe request 1187 // before the one we requested it for (if the keyframe request
1142 // is asynchronous, i.e. not bound to any concrete frame, and because 1188 // is asynchronous, i.e. not bound to any concrete frame, and because
1143 // the pipeline can be deeper than one frame), at that frame, or after. 1189 // the pipeline can be deeper than one frame), at that frame, or after.
1144 // So the only constraints we put here is that we get a keyframe not 1190 // So the only constraints we put here is that we get a keyframe not
1145 // earlier than we requested one (in time), and not later than 1191 // earlier than we requested one (in time), and not later than
1146 // kMaxKeyframeDelay frames after the frame, for which we requested 1192 // kMaxKeyframeDelay frames after the frame, for which we requested
1147 // it, comes back encoded. 1193 // it, comes back encoded.
(...skipping 14 matching lines...) Expand all
1162 if (requested_subsequent_bitrate_ != current_requested_bitrate_ || 1208 if (requested_subsequent_bitrate_ != current_requested_bitrate_ ||
1163 requested_subsequent_framerate_ != current_framerate_) { 1209 requested_subsequent_framerate_ != current_framerate_) {
1164 SetStreamParameters(requested_subsequent_bitrate_, 1210 SetStreamParameters(requested_subsequent_bitrate_,
1165 requested_subsequent_framerate_); 1211 requested_subsequent_framerate_);
1166 if (run_at_fps_ && input_timer_) 1212 if (run_at_fps_ && input_timer_)
1167 input_timer_->Start( 1213 input_timer_->Start(
1168 FROM_HERE, base::TimeDelta::FromSeconds(1) / current_framerate_, 1214 FROM_HERE, base::TimeDelta::FromSeconds(1) / current_framerate_,
1169 base::Bind(&VEAClient::OnInputTimer, base::Unretained(this))); 1215 base::Bind(&VEAClient::OnInputTimer, base::Unretained(this)));
1170 } 1216 }
1171 } else if (num_encoded_frames_ == num_frames_to_encode_) { 1217 } else if (num_encoded_frames_ == num_frames_to_encode_) {
1172 VerifyPerf(); 1218 LogPerf();
1219 VerifyMinFPS();
1173 VerifyStreamProperties(); 1220 VerifyStreamProperties();
1174 SetState(CS_FINISHED); 1221 SetState(CS_FINISHED);
1175 return false; 1222 return false;
1176 } 1223 }
1177 1224
1178 return true; 1225 return true;
1179 } 1226 }
1180 1227
1181 void VEAClient::VerifyPerf() { 1228 void VEAClient::LogPerf() {
1182 double measured_fps = frames_per_second(); 1229 // Log measured FPS.
1183 LOG(INFO) << "Measured encoder FPS: " << measured_fps;
1184 g_env->LogToFile("Measured encoder FPS", 1230 g_env->LogToFile("Measured encoder FPS",
1185 base::StringPrintf("%.3f", measured_fps)); 1231 base::StringPrintf("%.3f", frames_per_second()));
1232
1233 // Log encode latencies.
1234 if (needs_encode_latency()) {
1235 std::sort(encode_latencies_.begin(), encode_latencies_.end());
1236 for (unsigned int percentile : kLoggedLatencyPercentiles) {
Pawel Osciak 2015/05/13 03:21:18 s/unsigned int/const auto&/ ?
Justin Chuang 2015/05/13 13:59:35 Done. I changed to unsigned int because Percentile
1237 base::TimeDelta latency = Percentile(encode_latencies_, percentile);
1238 g_env->LogToFile(
1239 base::StringPrintf("Encode latency for the %dth percentile",
1240 percentile),
1241 base::StringPrintf("%" PRId64 " us", latency.InMicroseconds()));
1242 }
1243 }
1244 }
1245
1246 void VEAClient::VerifyMinFPS() {
1186 if (test_perf_) 1247 if (test_perf_)
1187 EXPECT_GE(measured_fps, kMinPerfFPS); 1248 EXPECT_GE(frames_per_second(), kMinPerfFPS);
1188 } 1249 }
1189 1250
1190 void VEAClient::VerifyStreamProperties() { 1251 void VEAClient::VerifyStreamProperties() {
1191 CHECK_GT(num_frames_since_last_check_, 0UL); 1252 CHECK_GT(num_frames_since_last_check_, 0UL);
1192 CHECK_GT(encoded_stream_size_since_last_check_, 0UL); 1253 CHECK_GT(encoded_stream_size_since_last_check_, 0UL);
1193 unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 * 1254 unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 *
1194 current_framerate_ / num_frames_since_last_check_; 1255 current_framerate_ / num_frames_since_last_check_;
1195 DVLOG(1) << "Current chunk's bitrate: " << bitrate 1256 DVLOG(1) << "Current chunk's bitrate: " << bitrate
1196 << " (expected: " << current_requested_bitrate_ 1257 << " (expected: " << current_requested_bitrate_
1197 << " @ " << current_framerate_ << " FPS," 1258 << " @ " << current_framerate_ << " FPS,"
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
1432 } 1493 }
1433 1494
1434 content::g_env = 1495 content::g_env =
1435 reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>( 1496 reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>(
1436 testing::AddGlobalTestEnvironment( 1497 testing::AddGlobalTestEnvironment(
1437 new content::VideoEncodeAcceleratorTestEnvironment( 1498 new content::VideoEncodeAcceleratorTestEnvironment(
1438 test_stream_data.Pass(), log_path, run_at_fps))); 1499 test_stream_data.Pass(), log_path, run_at_fps)));
1439 1500
1440 return RUN_ALL_TESTS(); 1501 return RUN_ALL_TESTS();
1441 } 1502 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698