Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <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 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 unsigned int keyframe_period, | 563 unsigned int keyframe_period, |
| 562 bool force_bitrate, | 564 bool force_bitrate, |
| 563 bool test_perf, | 565 bool test_perf, |
| 564 bool mid_stream_bitrate_switch, | 566 bool mid_stream_bitrate_switch, |
| 565 bool mid_stream_framerate_switch, | 567 bool mid_stream_framerate_switch, |
| 566 bool run_at_fps); | 568 bool run_at_fps); |
| 567 ~VEAClient() override; | 569 ~VEAClient() override; |
| 568 void CreateEncoder(); | 570 void CreateEncoder(); |
| 569 void DestroyEncoder(); | 571 void DestroyEncoder(); |
| 570 | 572 |
| 571 // Return the number of encoded frames per second. | 573 // Returns the number of encoded frames per second. |
| 572 double frames_per_second(); | 574 double frames_per_second(); |
| 573 | 575 |
| 576 // Returns the median encode delay for all the encoded frames. | |
| 577 base::TimeDelta median_encode_latency(); | |
| 578 | |
| 574 // VideoDecodeAccelerator::Client implementation. | 579 // VideoDecodeAccelerator::Client implementation. |
| 575 void RequireBitstreamBuffers(unsigned int input_count, | 580 void RequireBitstreamBuffers(unsigned int input_count, |
| 576 const gfx::Size& input_coded_size, | 581 const gfx::Size& input_coded_size, |
| 577 size_t output_buffer_size) override; | 582 size_t output_buffer_size) override; |
| 578 void BitstreamBufferReady(int32 bitstream_buffer_id, | 583 void BitstreamBufferReady(int32 bitstream_buffer_id, |
| 579 size_t payload_size, | 584 size_t payload_size, |
| 580 bool key_frame) override; | 585 bool key_frame) override; |
| 581 void NotifyError(VideoEncodeAccelerator::Error error) override; | 586 void NotifyError(VideoEncodeAccelerator::Error error) override; |
| 582 | 587 |
| 583 private: | 588 private: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 615 // Test codec performance, failing the test if we are currently running | 620 // Test codec performance, failing the test if we are currently running |
| 616 // the performance test. | 621 // the performance test. |
| 617 void VerifyPerf(); | 622 void VerifyPerf(); |
| 618 | 623 |
| 619 // Write IVF file header to test_stream_->out_filename. | 624 // Write IVF file header to test_stream_->out_filename. |
| 620 void WriteIvfFileHeader(); | 625 void WriteIvfFileHeader(); |
| 621 | 626 |
| 622 // Write an IVF frame header to test_stream_->out_filename. | 627 // Write an IVF frame header to test_stream_->out_filename. |
| 623 void WriteIvfFrameHeader(int frame_index, size_t frame_size); | 628 void WriteIvfFrameHeader(int frame_index, size_t frame_size); |
| 624 | 629 |
| 625 // Prepare and return a frame wrapping the data at |position| bytes in | 630 // Prepare and return a frame wrapping the data at |position| bytes in the |
| 626 // the input stream, ready to be sent to encoder. | 631 // input stream, ready to be sent to encoder. Returns a pair of video frame |
| 627 scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position); | 632 // and the input frame id. |
| 633 std::pair<scoped_refptr<media::VideoFrame>, int32> PrepareInputFrame( | |
| 634 off_t position); | |
| 628 | 635 |
| 629 // Update the parameters according to |mid_stream_bitrate_switch| and | 636 // Update the parameters according to |mid_stream_bitrate_switch| and |
| 630 // |mid_stream_framerate_switch|. | 637 // |mid_stream_framerate_switch|. |
| 631 void UpdateTestStreamData(bool mid_stream_bitrate_switch, | 638 void UpdateTestStreamData(bool mid_stream_bitrate_switch, |
| 632 bool mid_stream_framerate_switch); | 639 bool mid_stream_framerate_switch); |
| 633 | 640 |
| 634 // Callback function of the |input_timer_|. | 641 // Callback function of the |input_timer_|. |
| 635 void OnInputTimer(); | 642 void OnInputTimer(); |
| 636 | 643 |
| 637 ClientState state_; | 644 ClientState state_; |
| 638 scoped_ptr<VideoEncodeAccelerator> encoder_; | 645 scoped_ptr<VideoEncodeAccelerator> encoder_; |
| 639 | 646 |
| 640 TestStream* test_stream_; | 647 TestStream* test_stream_; |
| 641 | 648 |
| 642 // Used to notify another thread about the state. VEAClient does not own this. | 649 // Used to notify another thread about the state. VEAClient does not own this. |
| 643 ClientStateNotification<ClientState>* note_; | 650 ClientStateNotification<ClientState>* note_; |
| 644 | 651 |
| 645 // Ids assigned to VideoFrames (start at 1 for easy comparison with | 652 // Ids assigned to VideoFrames. |
| 646 // num_encoded_frames_). | |
|
Justin Chuang
2015/04/30 10:36:56
The original comments is wrong. So remove it. (It
| |
| 647 std::set<int32> inputs_at_client_; | 653 std::set<int32> inputs_at_client_; |
| 648 int32 next_input_id_; | 654 int32 next_input_id_; |
| 649 | 655 |
| 656 typedef std::map<int32, base::TimeTicks> IdToTicks; | |
| 657 // A map from input frame id to the encode start time of the frame. | |
| 658 IdToTicks encode_start_time_per_frame_; | |
| 659 // The encode latencies of all encoded frames. | |
| 660 std::vector<base::TimeDelta> encode_latencies_; | |
| 661 | |
| 650 // Ids for output BitstreamBuffers. | 662 // Ids for output BitstreamBuffers. |
| 651 typedef std::map<int32, base::SharedMemory*> IdToSHM; | 663 typedef std::map<int32, base::SharedMemory*> IdToSHM; |
| 652 ScopedVector<base::SharedMemory> output_shms_; | 664 ScopedVector<base::SharedMemory> output_shms_; |
| 653 IdToSHM output_buffers_at_client_; | 665 IdToSHM output_buffers_at_client_; |
| 654 int32 next_output_buffer_id_; | 666 int32 next_output_buffer_id_; |
| 655 | 667 |
| 656 // Current offset into input stream. | 668 // Current offset into input stream. |
| 657 off_t pos_in_input_stream_; | 669 off_t pos_in_input_stream_; |
| 658 gfx::Size input_coded_size_; | 670 gfx::Size input_coded_size_; |
| 659 // Requested by encoder. | 671 // Requested by encoder. |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 908 } | 920 } |
| 909 if (requested_subsequent_framerate_ == 0) | 921 if (requested_subsequent_framerate_ == 0) |
| 910 requested_subsequent_framerate_ = 1; | 922 requested_subsequent_framerate_ = 1; |
| 911 } | 923 } |
| 912 | 924 |
| 913 double VEAClient::frames_per_second() { | 925 double VEAClient::frames_per_second() { |
| 914 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_; | 926 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_; |
| 915 return num_encoded_frames_ / duration.InSecondsF(); | 927 return num_encoded_frames_ / duration.InSecondsF(); |
| 916 } | 928 } |
| 917 | 929 |
| 930 base::TimeDelta VEAClient::median_encode_latency() { | |
| 931 if (encode_latencies_.size() == 0) | |
| 932 return base::TimeDelta(); | |
| 933 std::sort(encode_latencies_.begin(), encode_latencies_.end()); | |
| 934 int index = encode_latencies_.size() / 2; | |
| 935 if (encode_latencies_.size() % 2 != 0) | |
| 936 return encode_latencies_[index]; | |
| 937 return (encode_latencies_[index] + encode_latencies_[index - 1]) / 2; | |
| 938 } | |
| 939 | |
| 918 void VEAClient::RequireBitstreamBuffers(unsigned int input_count, | 940 void VEAClient::RequireBitstreamBuffers(unsigned int input_count, |
| 919 const gfx::Size& input_coded_size, | 941 const gfx::Size& input_coded_size, |
| 920 size_t output_size) { | 942 size_t output_size) { |
| 921 DCHECK(thread_checker_.CalledOnValidThread()); | 943 DCHECK(thread_checker_.CalledOnValidThread()); |
| 922 ASSERT_EQ(state_, CS_INITIALIZED); | 944 ASSERT_EQ(state_, CS_INITIALIZED); |
| 923 SetState(CS_ENCODING); | 945 SetState(CS_ENCODING); |
| 924 | 946 |
| 925 CreateAlignedInputStreamFile(input_coded_size, test_stream_); | 947 CreateAlignedInputStreamFile(input_coded_size, test_stream_); |
| 926 | 948 |
| 927 num_frames_to_encode_ = test_stream_->num_frames; | 949 num_frames_to_encode_ = test_stream_->num_frames; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1031 } | 1053 } |
| 1032 | 1054 |
| 1033 void VEAClient::InputNoLongerNeededCallback(int32 input_id) { | 1055 void VEAClient::InputNoLongerNeededCallback(int32 input_id) { |
| 1034 std::set<int32>::iterator it = inputs_at_client_.find(input_id); | 1056 std::set<int32>::iterator it = inputs_at_client_.find(input_id); |
| 1035 ASSERT_NE(it, inputs_at_client_.end()); | 1057 ASSERT_NE(it, inputs_at_client_.end()); |
| 1036 inputs_at_client_.erase(it); | 1058 inputs_at_client_.erase(it); |
| 1037 if (!run_at_fps_) | 1059 if (!run_at_fps_) |
| 1038 FeedEncoderWithOneInput(); | 1060 FeedEncoderWithOneInput(); |
| 1039 } | 1061 } |
| 1040 | 1062 |
| 1041 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { | 1063 std::pair<scoped_refptr<media::VideoFrame>, int32> VEAClient::PrepareInputFrame( |
| 1064 off_t position) { | |
| 1042 CHECK_LE(position + test_stream_->aligned_buffer_size, | 1065 CHECK_LE(position + test_stream_->aligned_buffer_size, |
| 1043 test_stream_->mapped_aligned_in_file.length()); | 1066 test_stream_->mapped_aligned_in_file.length()); |
| 1044 | 1067 |
| 1045 uint8* frame_data_y = const_cast<uint8*>( | 1068 uint8* frame_data_y = const_cast<uint8*>( |
| 1046 test_stream_->mapped_aligned_in_file.data() + position); | 1069 test_stream_->mapped_aligned_in_file.data() + position); |
| 1047 uint8* frame_data_u = frame_data_y + test_stream_->aligned_plane_size[0]; | 1070 uint8* frame_data_u = frame_data_y + test_stream_->aligned_plane_size[0]; |
| 1048 uint8* frame_data_v = frame_data_u + test_stream_->aligned_plane_size[1]; | 1071 uint8* frame_data_v = frame_data_u + test_stream_->aligned_plane_size[1]; |
| 1049 | 1072 |
| 1050 CHECK_GT(current_framerate_, 0U); | 1073 CHECK_GT(current_framerate_, 0U); |
| 1051 scoped_refptr<media::VideoFrame> frame = | 1074 scoped_refptr<media::VideoFrame> frame = |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1062 frame_data_v, | 1085 frame_data_v, |
| 1063 base::TimeDelta().FromMilliseconds( | 1086 base::TimeDelta().FromMilliseconds( |
| 1064 next_input_id_ * base::Time::kMillisecondsPerSecond / | 1087 next_input_id_ * base::Time::kMillisecondsPerSecond / |
| 1065 current_framerate_), | 1088 current_framerate_), |
| 1066 media::BindToCurrentLoop( | 1089 media::BindToCurrentLoop( |
| 1067 base::Bind(&VEAClient::InputNoLongerNeededCallback, | 1090 base::Bind(&VEAClient::InputNoLongerNeededCallback, |
| 1068 base::Unretained(this), | 1091 base::Unretained(this), |
| 1069 next_input_id_))); | 1092 next_input_id_))); |
| 1070 | 1093 |
| 1071 CHECK(inputs_at_client_.insert(next_input_id_).second); | 1094 CHECK(inputs_at_client_.insert(next_input_id_).second); |
| 1072 ++next_input_id_; | |
| 1073 | 1095 |
| 1074 return frame; | 1096 return std::make_pair(frame, next_input_id_++); |
| 1075 } | 1097 } |
| 1076 | 1098 |
| 1077 void VEAClient::OnInputTimer() { | 1099 void VEAClient::OnInputTimer() { |
| 1078 if (!has_encoder() || state_ != CS_ENCODING) | 1100 if (!has_encoder() || state_ != CS_ENCODING) |
| 1079 input_timer_.reset(); | 1101 input_timer_.reset(); |
| 1080 else if (inputs_at_client_.size() < | 1102 else if (inputs_at_client_.size() < |
| 1081 num_required_input_buffers_ + kNumExtraInputFrames) | 1103 num_required_input_buffers_ + kNumExtraInputFrames) |
| 1082 FeedEncoderWithOneInput(); | 1104 FeedEncoderWithOneInput(); |
| 1083 else | 1105 else |
| 1084 DVLOG(1) << "Dropping input frame"; | 1106 DVLOG(1) << "Dropping input frame"; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 1098 // we require for bitrate tests. | 1120 // we require for bitrate tests. |
| 1099 pos_in_input_stream_ = 0; | 1121 pos_in_input_stream_ = 0; |
| 1100 } | 1122 } |
| 1101 | 1123 |
| 1102 bool force_keyframe = false; | 1124 bool force_keyframe = false; |
| 1103 if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) { | 1125 if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) { |
| 1104 force_keyframe = true; | 1126 force_keyframe = true; |
| 1105 ++num_keyframes_requested_; | 1127 ++num_keyframes_requested_; |
| 1106 } | 1128 } |
| 1107 | 1129 |
| 1108 scoped_refptr<media::VideoFrame> video_frame = | 1130 auto ret = PrepareInputFrame(pos_in_input_stream_); |
| 1109 PrepareInputFrame(pos_in_input_stream_); | |
| 1110 pos_in_input_stream_ += test_stream_->aligned_buffer_size; | 1131 pos_in_input_stream_ += test_stream_->aligned_buffer_size; |
| 1111 | 1132 |
| 1112 encoder_->Encode(video_frame, force_keyframe); | 1133 CHECK(encode_start_time_per_frame_.insert( |
| 1134 std::make_pair(ret.second, base::TimeTicks::Now())).second); | |
| 1135 encoder_->Encode(ret.first, force_keyframe); | |
| 1113 } | 1136 } |
| 1114 | 1137 |
| 1115 void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) { | 1138 void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) { |
| 1116 if (!has_encoder()) | 1139 if (!has_encoder()) |
| 1117 return; | 1140 return; |
| 1118 | 1141 |
| 1119 if (state_ != CS_ENCODING) | 1142 if (state_ != CS_ENCODING) |
| 1120 return; | 1143 return; |
| 1121 | 1144 |
| 1122 base::SharedMemoryHandle dup_handle; | 1145 base::SharedMemoryHandle dup_handle; |
| 1123 CHECK(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle)); | 1146 CHECK(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle)); |
| 1124 | 1147 |
| 1125 media::BitstreamBuffer bitstream_buffer( | 1148 media::BitstreamBuffer bitstream_buffer( |
| 1126 next_output_buffer_id_++, dup_handle, output_buffer_size_); | 1149 next_output_buffer_id_++, dup_handle, output_buffer_size_); |
| 1127 CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(), | 1150 CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(), |
| 1128 shm)).second); | 1151 shm)).second); |
| 1129 encoder_->UseOutputBitstreamBuffer(bitstream_buffer); | 1152 encoder_->UseOutputBitstreamBuffer(bitstream_buffer); |
| 1130 } | 1153 } |
| 1131 | 1154 |
| 1132 bool VEAClient::HandleEncodedFrame(bool keyframe) { | 1155 bool VEAClient::HandleEncodedFrame(bool keyframe) { |
| 1133 // This would be a bug in the test, which should not ignore false | 1156 // This would be a bug in the test, which should not ignore false |
| 1134 // return value from this method. | 1157 // return value from this method. |
| 1135 CHECK_LE(num_encoded_frames_, num_frames_to_encode_); | 1158 CHECK_LE(num_encoded_frames_, num_frames_to_encode_); |
| 1136 | 1159 |
| 1160 last_frame_ready_time_ = base::TimeTicks::Now(); | |
| 1161 | |
| 1162 // Record encode latency of this frame. | |
| 1163 IdToTicks::iterator it = | |
| 1164 encode_start_time_per_frame_.find(num_encoded_frames_); | |
| 1165 CHECK_NE(it, encode_start_time_per_frame_.end()); | |
| 1166 encode_latencies_.push_back(last_frame_ready_time_ - it->second); | |
| 1167 encode_start_time_per_frame_.erase(it); | |
| 1168 | |
| 1137 ++num_encoded_frames_; | 1169 ++num_encoded_frames_; |
| 1138 ++num_frames_since_last_check_; | 1170 ++num_frames_since_last_check_; |
| 1139 | 1171 |
| 1140 last_frame_ready_time_ = base::TimeTicks::Now(); | |
| 1141 | |
| 1142 // Because the keyframe behavior requirements are loose, we give | 1172 // Because the keyframe behavior requirements are loose, we give |
| 1143 // the encoder more freedom here. It could either deliver a keyframe | 1173 // the encoder more freedom here. It could either deliver a keyframe |
| 1144 // immediately after we requested it, which could be for a frame number | 1174 // immediately after we requested it, which could be for a frame number |
| 1145 // before the one we requested it for (if the keyframe request | 1175 // before the one we requested it for (if the keyframe request |
| 1146 // is asynchronous, i.e. not bound to any concrete frame, and because | 1176 // is asynchronous, i.e. not bound to any concrete frame, and because |
| 1147 // the pipeline can be deeper than one frame), at that frame, or after. | 1177 // the pipeline can be deeper than one frame), at that frame, or after. |
| 1148 // So the only constraints we put here is that we get a keyframe not | 1178 // So the only constraints we put here is that we get a keyframe not |
| 1149 // earlier than we requested one (in time), and not later than | 1179 // earlier than we requested one (in time), and not later than |
| 1150 // kMaxKeyframeDelay frames after the frame, for which we requested | 1180 // kMaxKeyframeDelay frames after the frame, for which we requested |
| 1151 // it, comes back encoded. | 1181 // it, comes back encoded. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1182 return true; | 1212 return true; |
| 1183 } | 1213 } |
| 1184 | 1214 |
| 1185 void VEAClient::VerifyPerf() { | 1215 void VEAClient::VerifyPerf() { |
| 1186 double measured_fps = frames_per_second(); | 1216 double measured_fps = frames_per_second(); |
| 1187 LOG(INFO) << "Measured encoder FPS: " << measured_fps; | 1217 LOG(INFO) << "Measured encoder FPS: " << measured_fps; |
| 1188 g_env->LogToFile("Measured encoder FPS", | 1218 g_env->LogToFile("Measured encoder FPS", |
| 1189 base::StringPrintf("%.3f", measured_fps)); | 1219 base::StringPrintf("%.3f", measured_fps)); |
| 1190 if (test_perf_) | 1220 if (test_perf_) |
| 1191 EXPECT_GE(measured_fps, kMinPerfFPS); | 1221 EXPECT_GE(measured_fps, kMinPerfFPS); |
| 1222 | |
| 1223 base::TimeDelta median_latency = median_encode_latency(); | |
| 1224 LOG(INFO) << "Median encode latency: " << median_latency.InMicroseconds() | |
| 1225 << " us"; | |
| 1226 g_env->LogToFile( | |
| 1227 "Median encode latency", | |
| 1228 base::StringPrintf("%" PRId64 " us", median_latency.InMicroseconds())); | |
| 1192 } | 1229 } |
| 1193 | 1230 |
| 1194 void VEAClient::VerifyStreamProperties() { | 1231 void VEAClient::VerifyStreamProperties() { |
| 1195 CHECK_GT(num_frames_since_last_check_, 0UL); | 1232 CHECK_GT(num_frames_since_last_check_, 0UL); |
| 1196 CHECK_GT(encoded_stream_size_since_last_check_, 0UL); | 1233 CHECK_GT(encoded_stream_size_since_last_check_, 0UL); |
| 1197 unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 * | 1234 unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 * |
| 1198 current_framerate_ / num_frames_since_last_check_; | 1235 current_framerate_ / num_frames_since_last_check_; |
| 1199 DVLOG(1) << "Current chunk's bitrate: " << bitrate | 1236 DVLOG(1) << "Current chunk's bitrate: " << bitrate |
| 1200 << " (expected: " << current_requested_bitrate_ | 1237 << " (expected: " << current_requested_bitrate_ |
| 1201 << " @ " << current_framerate_ << " FPS," | 1238 << " @ " << current_framerate_ << " FPS," |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1441 } | 1478 } |
| 1442 | 1479 |
| 1443 content::g_env = | 1480 content::g_env = |
| 1444 reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>( | 1481 reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>( |
| 1445 testing::AddGlobalTestEnvironment( | 1482 testing::AddGlobalTestEnvironment( |
| 1446 new content::VideoEncodeAcceleratorTestEnvironment( | 1483 new content::VideoEncodeAcceleratorTestEnvironment( |
| 1447 test_stream_data.Pass(), log_path, run_at_fps))); | 1484 test_stream_data.Pass(), log_path, run_at_fps))); |
| 1448 | 1485 |
| 1449 return RUN_ALL_TESTS(); | 1486 return RUN_ALL_TESTS(); |
| 1450 } | 1487 } |
| OLD | NEW |