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 <inttypes.h> | 5 #include <inttypes.h> |
6 #include <stddef.h> | 6 #include <stddef.h> |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 856 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
867 } | 867 } |
868 } | 868 } |
869 } | 869 } |
870 | 870 |
871 // Divide the difference by the size of frame. | 871 // Divide the difference by the size of frame. |
872 difference /= VideoFrame::AllocationSize(kInputFormat, visible_size); | 872 difference /= VideoFrame::AllocationSize(kInputFormat, visible_size); |
873 EXPECT_TRUE(difference <= kDecodeSimilarityThreshold) | 873 EXPECT_TRUE(difference <= kDecodeSimilarityThreshold) |
874 << "difference = " << difference << " > decode similarity threshold"; | 874 << "difference = " << difference << " > decode similarity threshold"; |
875 } | 875 } |
876 | 876 |
877 class VEAClient : public VideoEncodeAccelerator::Client { | 877 // Base class for VEAClient's |
878 class VEAClientBase : public VideoEncodeAccelerator::Client { | |
879 public: | |
880 void NotifyError(VideoEncodeAccelerator::Error error) override { | |
881 DCHECK(thread_checker_.CalledOnValidThread()); | |
882 SetState(CS_ERROR); | |
883 } | |
884 | |
885 protected: | |
886 VEAClientBase(ClientStateNotification<ClientState>* note) | |
887 : note_(note), next_output_buffer_id_(0) {} | |
888 | |
889 bool has_encoder() { return encoder_.get(); } | |
890 | |
891 virtual void SetState(ClientState new_state) = 0; | |
892 | |
893 std::unique_ptr<VideoEncodeAccelerator> encoder_; | |
894 | |
895 // Used to notify another thread about the state. VEAClient does not own this. | |
wuchengli
2016/11/28 04:54:42
s/VEAClient/VEAClientBase/
hywu1
2016/11/29 05:47:06
Done.
| |
896 ClientStateNotification<ClientState>* note_; | |
897 | |
898 // All methods of this class should be run on the same thread. | |
899 base::ThreadChecker thread_checker_; | |
900 | |
901 ScopedVector<base::SharedMemory> output_shms_; | |
902 int32_t next_output_buffer_id_; | |
903 }; | |
904 | |
905 class VEAClient : public VEAClientBase { | |
878 public: | 906 public: |
879 VEAClient(TestStream* test_stream, | 907 VEAClient(TestStream* test_stream, |
880 ClientStateNotification<ClientState>* note, | 908 ClientStateNotification<ClientState>* note, |
881 bool save_to_file, | 909 bool save_to_file, |
882 unsigned int keyframe_period, | 910 unsigned int keyframe_period, |
883 bool force_bitrate, | 911 bool force_bitrate, |
884 bool test_perf, | 912 bool test_perf, |
885 bool mid_stream_bitrate_switch, | 913 bool mid_stream_bitrate_switch, |
886 bool mid_stream_framerate_switch, | 914 bool mid_stream_framerate_switch, |
887 bool verify_output, | 915 bool verify_output, |
888 bool verify_output_timestamp); | 916 bool verify_output_timestamp); |
889 ~VEAClient() override; | 917 ~VEAClient() override; |
890 void CreateEncoder(); | 918 void CreateEncoder(); |
891 void DestroyEncoder(); | 919 void DestroyEncoder(); |
892 | 920 |
893 void TryToSetupEncodeOnSeperateThread(); | 921 void TryToSetupEncodeOnSeperateThread(); |
894 void DestroyEncodeOnSeperateThread(); | 922 void DestroyEncodeOnSeperateThread(); |
895 | 923 |
896 // VideoDecodeAccelerator::Client implementation. | 924 // VideoDecodeAccelerator::Client implementation. |
897 void RequireBitstreamBuffers(unsigned int input_count, | 925 void RequireBitstreamBuffers(unsigned int input_count, |
898 const gfx::Size& input_coded_size, | 926 const gfx::Size& input_coded_size, |
899 size_t output_buffer_size) override; | 927 size_t output_buffer_size) override; |
900 void BitstreamBufferReady(int32_t bitstream_buffer_id, | 928 void BitstreamBufferReady(int32_t bitstream_buffer_id, |
901 size_t payload_size, | 929 size_t payload_size, |
902 bool key_frame, | 930 bool key_frame, |
903 base::TimeDelta timestamp) override; | 931 base::TimeDelta timestamp) override; |
904 void NotifyError(VideoEncodeAccelerator::Error error) override; | |
905 | 932 |
906 private: | 933 private: |
907 void BitstreamBufferReadyOnMainThread(int32_t bitstream_buffer_id, | 934 void BitstreamBufferReadyOnMainThread(int32_t bitstream_buffer_id, |
908 size_t payload_size, | 935 size_t payload_size, |
909 bool key_frame, | 936 bool key_frame, |
910 base::TimeDelta timestamp); | 937 base::TimeDelta timestamp); |
911 bool has_encoder() { return encoder_.get(); } | |
912 | 938 |
913 // Return the number of encoded frames per second. | 939 // Return the number of encoded frames per second. |
914 double frames_per_second(); | 940 double frames_per_second(); |
915 | 941 |
916 void SetState(ClientState new_state); | 942 void SetState(ClientState new_state) override; |
917 | 943 |
918 // Set current stream parameters to given |bitrate| at |framerate|. | 944 // Set current stream parameters to given |bitrate| at |framerate|. |
919 void SetStreamParameters(unsigned int bitrate, unsigned int framerate); | 945 void SetStreamParameters(unsigned int bitrate, unsigned int framerate); |
920 | 946 |
921 // Called when encoder is done with a VideoFrame. | 947 // Called when encoder is done with a VideoFrame. |
922 void InputNoLongerNeededCallback(int32_t input_id); | 948 void InputNoLongerNeededCallback(int32_t input_id); |
923 | 949 |
924 // Feed the encoder with one input frame. | 950 // Feed the encoder with one input frame. |
925 void FeedEncoderWithOneInput(); | 951 void FeedEncoderWithOneInput(); |
926 | 952 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
971 // Called when the quality validator has decoded all the frames. | 997 // Called when the quality validator has decoded all the frames. |
972 void DecodeCompleted(); | 998 void DecodeCompleted(); |
973 | 999 |
974 // Called when the quality validator fails to decode a frame. | 1000 // Called when the quality validator fails to decode a frame. |
975 void DecodeFailed(); | 1001 void DecodeFailed(); |
976 | 1002 |
977 // Verify that the output timestamp matches input timestamp. | 1003 // Verify that the output timestamp matches input timestamp. |
978 void VerifyOutputTimestamp(base::TimeDelta timestamp); | 1004 void VerifyOutputTimestamp(base::TimeDelta timestamp); |
979 | 1005 |
980 ClientState state_; | 1006 ClientState state_; |
981 std::unique_ptr<VideoEncodeAccelerator> encoder_; | |
982 | 1007 |
983 TestStream* test_stream_; | 1008 TestStream* test_stream_; |
984 | 1009 |
985 // Used to notify another thread about the state. VEAClient does not own this. | |
986 ClientStateNotification<ClientState>* note_; | |
987 | |
988 // Ids assigned to VideoFrames. | 1010 // Ids assigned to VideoFrames. |
989 std::set<int32_t> inputs_at_client_; | 1011 std::set<int32_t> inputs_at_client_; |
990 int32_t next_input_id_; | 1012 int32_t next_input_id_; |
991 | 1013 |
992 // Encode start time of all encoded frames. The position in the vector is the | 1014 // Encode start time of all encoded frames. The position in the vector is the |
993 // frame input id. | 1015 // frame input id. |
994 std::vector<base::TimeTicks> encode_start_time_; | 1016 std::vector<base::TimeTicks> encode_start_time_; |
995 // The encode latencies of all encoded frames. We define encode latency as the | 1017 // The encode latencies of all encoded frames. We define encode latency as the |
996 // time delay from input of each VideoFrame (VEA::Encode()) to output of the | 1018 // time delay from input of each VideoFrame (VEA::Encode()) to output of the |
997 // corresponding BitstreamBuffer (VEA::Client::BitstreamBufferReady()). | 1019 // corresponding BitstreamBuffer (VEA::Client::BitstreamBufferReady()). |
998 std::vector<base::TimeDelta> encode_latencies_; | 1020 std::vector<base::TimeDelta> encode_latencies_; |
999 | 1021 |
1000 // Ids for output BitstreamBuffers. | 1022 // Ids for output BitstreamBuffers. |
1001 typedef std::map<int32_t, base::SharedMemory*> IdToSHM; | 1023 typedef std::map<int32_t, base::SharedMemory*> IdToSHM; |
1002 ScopedVector<base::SharedMemory> output_shms_; | |
1003 IdToSHM output_buffers_at_client_; | 1024 IdToSHM output_buffers_at_client_; |
1004 int32_t next_output_buffer_id_; | |
1005 | 1025 |
1006 // Current offset into input stream. | 1026 // Current offset into input stream. |
1007 off_t pos_in_input_stream_; | 1027 off_t pos_in_input_stream_; |
1008 gfx::Size input_coded_size_; | 1028 gfx::Size input_coded_size_; |
1009 // Requested by encoder. | 1029 // Requested by encoder. |
1010 unsigned int num_required_input_buffers_; | 1030 unsigned int num_required_input_buffers_; |
1011 size_t output_buffer_size_; | 1031 size_t output_buffer_size_; |
1012 | 1032 |
1013 // Number of frames to encode. This may differ from the number of frames in | 1033 // Number of frames to encode. This may differ from the number of frames in |
1014 // stream if we need more frames for bitrate tests. | 1034 // stream if we need more frames for bitrate tests. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1062 | 1082 |
1063 // Used to validate the encoded frame quality. | 1083 // Used to validate the encoded frame quality. |
1064 std::unique_ptr<VideoFrameQualityValidator> quality_validator_; | 1084 std::unique_ptr<VideoFrameQualityValidator> quality_validator_; |
1065 | 1085 |
1066 // The time when the first frame is submitted for encode. | 1086 // The time when the first frame is submitted for encode. |
1067 base::TimeTicks first_frame_start_time_; | 1087 base::TimeTicks first_frame_start_time_; |
1068 | 1088 |
1069 // The time when the last encoded frame is ready. | 1089 // The time when the last encoded frame is ready. |
1070 base::TimeTicks last_frame_ready_time_; | 1090 base::TimeTicks last_frame_ready_time_; |
1071 | 1091 |
1072 // All methods of this class should be run on the same thread. | |
1073 base::ThreadChecker thread_checker_; | |
1074 | |
1075 // Requested bitrate in bits per second. | 1092 // Requested bitrate in bits per second. |
1076 unsigned int requested_bitrate_; | 1093 unsigned int requested_bitrate_; |
1077 | 1094 |
1078 // Requested initial framerate. | 1095 // Requested initial framerate. |
1079 unsigned int requested_framerate_; | 1096 unsigned int requested_framerate_; |
1080 | 1097 |
1081 // Bitrate to switch to in the middle of the stream. | 1098 // Bitrate to switch to in the middle of the stream. |
1082 unsigned int requested_subsequent_bitrate_; | 1099 unsigned int requested_subsequent_bitrate_; |
1083 | 1100 |
1084 // Framerate to switch to in the middle of the stream. | 1101 // Framerate to switch to in the middle of the stream. |
(...skipping 30 matching lines...) Expand all Loading... | |
1115 VEAClient::VEAClient(TestStream* test_stream, | 1132 VEAClient::VEAClient(TestStream* test_stream, |
1116 ClientStateNotification<ClientState>* note, | 1133 ClientStateNotification<ClientState>* note, |
1117 bool save_to_file, | 1134 bool save_to_file, |
1118 unsigned int keyframe_period, | 1135 unsigned int keyframe_period, |
1119 bool force_bitrate, | 1136 bool force_bitrate, |
1120 bool test_perf, | 1137 bool test_perf, |
1121 bool mid_stream_bitrate_switch, | 1138 bool mid_stream_bitrate_switch, |
1122 bool mid_stream_framerate_switch, | 1139 bool mid_stream_framerate_switch, |
1123 bool verify_output, | 1140 bool verify_output, |
1124 bool verify_output_timestamp) | 1141 bool verify_output_timestamp) |
1125 : state_(CS_CREATED), | 1142 : VEAClientBase(note), |
1143 state_(CS_CREATED), | |
1126 test_stream_(test_stream), | 1144 test_stream_(test_stream), |
1127 note_(note), | |
1128 next_input_id_(0), | 1145 next_input_id_(0), |
1129 next_output_buffer_id_(0), | |
1130 pos_in_input_stream_(0), | 1146 pos_in_input_stream_(0), |
1131 num_required_input_buffers_(0), | 1147 num_required_input_buffers_(0), |
1132 output_buffer_size_(0), | 1148 output_buffer_size_(0), |
1133 num_frames_to_encode_(0), | 1149 num_frames_to_encode_(0), |
1134 num_encoded_frames_(0), | 1150 num_encoded_frames_(0), |
1135 num_frames_since_last_check_(0), | 1151 num_frames_since_last_check_(0), |
1136 seen_keyframe_in_this_buffer_(false), | 1152 seen_keyframe_in_this_buffer_(false), |
1137 save_to_file_(save_to_file), | 1153 save_to_file_(save_to_file), |
1138 keyframe_period_(keyframe_period), | 1154 keyframe_period_(keyframe_period), |
1139 num_keyframes_requested_(0), | 1155 num_keyframes_requested_(0), |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1397 size_t payload_size, | 1413 size_t payload_size, |
1398 bool key_frame, | 1414 bool key_frame, |
1399 base::TimeDelta timestamp) { | 1415 base::TimeDelta timestamp) { |
1400 ASSERT_TRUE(encode_task_runner_->BelongsToCurrentThread()); | 1416 ASSERT_TRUE(encode_task_runner_->BelongsToCurrentThread()); |
1401 main_thread_task_runner_->PostTask( | 1417 main_thread_task_runner_->PostTask( |
1402 FROM_HERE, base::Bind(&VEAClient::BitstreamBufferReadyOnMainThread, | 1418 FROM_HERE, base::Bind(&VEAClient::BitstreamBufferReadyOnMainThread, |
1403 base::Unretained(this), bitstream_buffer_id, | 1419 base::Unretained(this), bitstream_buffer_id, |
1404 payload_size, key_frame, timestamp)); | 1420 payload_size, key_frame, timestamp)); |
1405 } | 1421 } |
1406 | 1422 |
1407 void VEAClient::NotifyError(VideoEncodeAccelerator::Error error) { | |
1408 DCHECK(thread_checker_.CalledOnValidThread()); | |
1409 SetState(CS_ERROR); | |
1410 } | |
1411 | |
1412 void VEAClient::BitstreamBufferReadyOnMainThread(int32_t bitstream_buffer_id, | 1423 void VEAClient::BitstreamBufferReadyOnMainThread(int32_t bitstream_buffer_id, |
1413 size_t payload_size, | 1424 size_t payload_size, |
1414 bool key_frame, | 1425 bool key_frame, |
1415 base::TimeDelta timestamp) { | 1426 base::TimeDelta timestamp) { |
1416 DCHECK(thread_checker_.CalledOnValidThread()); | 1427 DCHECK(thread_checker_.CalledOnValidThread()); |
1417 | 1428 |
1418 ASSERT_LE(payload_size, output_buffer_size_); | 1429 ASSERT_LE(payload_size, output_buffer_size_); |
1419 | 1430 |
1420 IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id); | 1431 IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id); |
1421 ASSERT_NE(it, output_buffers_at_client_.end()); | 1432 ASSERT_NE(it, output_buffers_at_client_.end()); |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1752 IvfFrameHeader header = {}; | 1763 IvfFrameHeader header = {}; |
1753 | 1764 |
1754 header.frame_size = static_cast<uint32_t>(frame_size); | 1765 header.frame_size = static_cast<uint32_t>(frame_size); |
1755 header.timestamp = frame_index; | 1766 header.timestamp = frame_index; |
1756 header.ByteSwap(); | 1767 header.ByteSwap(); |
1757 EXPECT_TRUE(base::AppendToFile( | 1768 EXPECT_TRUE(base::AppendToFile( |
1758 base::FilePath::FromUTF8Unsafe(test_stream_->out_filename), | 1769 base::FilePath::FromUTF8Unsafe(test_stream_->out_filename), |
1759 reinterpret_cast<char*>(&header), sizeof(header))); | 1770 reinterpret_cast<char*>(&header), sizeof(header))); |
1760 } | 1771 } |
1761 | 1772 |
1762 // This client is only used to make sure the encoder does not return an encoded | 1773 // Base class for simple VEAClient's |
1763 // frame before getting any input. | 1774 class SimpleVEAClientBase : public VEAClientBase { |
1764 class VEANoInputClient : public VideoEncodeAccelerator::Client { | |
1765 public: | 1775 public: |
1766 explicit VEANoInputClient(ClientStateNotification<ClientState>* note); | |
1767 ~VEANoInputClient() override; | |
1768 void CreateEncoder(); | 1776 void CreateEncoder(); |
1769 void DestroyEncoder(); | 1777 void DestroyEncoder(); |
1770 | 1778 |
1771 // VideoDecodeAccelerator::Client implementation. | 1779 // VideoDecodeAccelerator::Client implementation. |
1772 void RequireBitstreamBuffers(unsigned int input_count, | 1780 void RequireBitstreamBuffers(unsigned int input_count, |
1773 const gfx::Size& input_coded_size, | 1781 const gfx::Size& input_coded_size, |
1774 size_t output_buffer_size) override; | 1782 size_t output_buffer_size) override; |
1775 void BitstreamBufferReady(int32_t bitstream_buffer_id, | |
1776 size_t payload_size, | |
1777 bool key_frame, | |
1778 base::TimeDelta timestamp) override; | |
1779 void NotifyError(VideoEncodeAccelerator::Error error) override; | |
1780 | 1783 |
1781 private: | 1784 protected: |
1782 bool has_encoder() { return encoder_.get(); } | 1785 SimpleVEAClientBase(ClientStateNotification<ClientState>* note, |
1786 const int width, | |
1787 const int height); | |
1783 | 1788 |
1784 void SetState(ClientState new_state); | 1789 void SetState(ClientState new_state) override; |
1785 | 1790 |
1786 // Provide the encoder with a new output buffer. | 1791 // Provide the encoder with a new output buffer. |
1787 void FeedEncoderWithOutput(base::SharedMemory* shm, size_t output_size); | 1792 void FeedEncoderWithOutput(base::SharedMemory* shm, size_t output_size); |
1788 | 1793 |
1789 std::unique_ptr<VideoEncodeAccelerator> encoder_; | 1794 const int default_width_; |
1790 | 1795 const int default_height_; |
wuchengli
2016/11/28 04:54:42
s/default_width_/width_/ and s/default_height_/hei
hywu1
2016/11/29 05:47:06
Done.
| |
1791 // Used to notify another thread about the state. VEAClient does not own this. | 1796 const int default_bitrate_; |
1792 ClientStateNotification<ClientState>* note_; | 1797 const int default_fps_; |
1793 | |
1794 // All methods of this class should be run on the same thread. | |
1795 base::ThreadChecker thread_checker_; | |
1796 | |
1797 // Ids for output BitstreamBuffers. | |
1798 ScopedVector<base::SharedMemory> output_shms_; | |
1799 int32_t next_output_buffer_id_; | |
1800 | |
1801 // The timer used to monitor the encoder doesn't return an output buffer in | |
1802 // a period of time. | |
1803 std::unique_ptr<base::Timer> timer_; | |
1804 }; | 1798 }; |
1805 | 1799 |
1806 VEANoInputClient::VEANoInputClient(ClientStateNotification<ClientState>* note) | 1800 SimpleVEAClientBase::SimpleVEAClientBase( |
1807 : note_(note), next_output_buffer_id_(0) { | 1801 ClientStateNotification<ClientState>* note, |
1802 const int width, | |
1803 const int height) | |
1804 : VEAClientBase(note), | |
1805 default_width_(width), | |
1806 default_height_(height), | |
1807 default_bitrate_(200000), | |
1808 default_fps_(30) { | |
1808 thread_checker_.DetachFromThread(); | 1809 thread_checker_.DetachFromThread(); |
1809 } | 1810 } |
1810 | 1811 |
1811 VEANoInputClient::~VEANoInputClient() { | 1812 void SimpleVEAClientBase::CreateEncoder() { |
1812 LOG_ASSERT(!has_encoder()); | |
1813 } | |
1814 | |
1815 void VEANoInputClient::CreateEncoder() { | |
1816 DCHECK(thread_checker_.CalledOnValidThread()); | 1813 DCHECK(thread_checker_.CalledOnValidThread()); |
1817 LOG_ASSERT(!has_encoder()); | 1814 LOG_ASSERT(!has_encoder()); |
1818 LOG_ASSERT(g_env->test_streams_.size()); | 1815 LOG_ASSERT(g_env->test_streams_.size()); |
1819 | 1816 |
1820 const int kDefaultWidth = 320; | |
1821 const int kDefaultHeight = 240; | |
1822 const int kDefaultBitrate = 200000; | |
1823 const int kDefaultFps = 30; | |
1824 | |
1825 std::unique_ptr<VideoEncodeAccelerator> encoders[] = { | 1817 std::unique_ptr<VideoEncodeAccelerator> encoders[] = { |
1826 CreateFakeVEA(), CreateV4L2VEA(), CreateVaapiVEA(), CreateVTVEA(), | 1818 CreateFakeVEA(), CreateV4L2VEA(), CreateVaapiVEA(), CreateVTVEA(), |
1827 CreateMFVEA()}; | 1819 CreateMFVEA()}; |
1828 | 1820 |
1829 // The test case is no input. Use default visible size, bitrate, and fps. | 1821 gfx::Size visible_size(default_width_, default_height_); |
1830 gfx::Size visible_size(kDefaultWidth, kDefaultHeight); | |
1831 for (auto& encoder : encoders) { | 1822 for (auto& encoder : encoders) { |
1832 if (!encoder) | 1823 if (!encoder) |
1833 continue; | 1824 continue; |
1834 encoder_ = std::move(encoder); | 1825 encoder_ = std::move(encoder); |
1835 if (encoder_->Initialize(kInputFormat, visible_size, | 1826 if (encoder_->Initialize(kInputFormat, visible_size, |
1836 g_env->test_streams_[0]->requested_profile, | 1827 g_env->test_streams_[0]->requested_profile, |
1837 kDefaultBitrate, this)) { | 1828 default_bitrate_, this)) { |
1838 encoder_->RequestEncodingParametersChange(kDefaultBitrate, kDefaultFps); | 1829 encoder_->RequestEncodingParametersChange(default_bitrate_, default_fps_); |
1839 SetState(CS_INITIALIZED); | 1830 SetState(CS_INITIALIZED); |
1840 return; | 1831 return; |
1841 } | 1832 } |
1842 } | 1833 } |
1843 encoder_.reset(); | 1834 encoder_.reset(); |
1844 LOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed"; | 1835 LOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed"; |
1845 SetState(CS_ERROR); | 1836 SetState(CS_ERROR); |
1846 } | 1837 } |
1847 | 1838 |
1848 void VEANoInputClient::DestroyEncoder() { | 1839 void SimpleVEAClientBase::DestroyEncoder() { |
1849 DCHECK(thread_checker_.CalledOnValidThread()); | 1840 DCHECK(thread_checker_.CalledOnValidThread()); |
1850 if (!has_encoder()) | 1841 if (!has_encoder()) |
1851 return; | 1842 return; |
1852 // Clear the objects that should be destroyed on the same thread as creation. | 1843 // Clear the objects that should be destroyed on the same thread as creation. |
1853 encoder_.reset(); | 1844 encoder_.reset(); |
1854 timer_.reset(); | |
1855 } | 1845 } |
1856 | 1846 |
1857 void VEANoInputClient::RequireBitstreamBuffers( | 1847 void SimpleVEAClientBase::SetState(ClientState new_state) { |
1848 DVLOG(4) << "Changing state to " << new_state; | |
1849 note_->Notify(new_state); | |
1850 } | |
1851 | |
1852 void SimpleVEAClientBase::RequireBitstreamBuffers( | |
1858 unsigned int input_count, | 1853 unsigned int input_count, |
1859 const gfx::Size& input_coded_size, | 1854 const gfx::Size& input_coded_size, |
1860 size_t output_size) { | 1855 size_t output_size) { |
1861 DCHECK(thread_checker_.CalledOnValidThread()); | 1856 DCHECK(thread_checker_.CalledOnValidThread()); |
1862 SetState(CS_ENCODING); | 1857 SetState(CS_ENCODING); |
1863 ASSERT_GT(output_size, 0UL); | 1858 ASSERT_GT(output_size, 0UL); |
1864 | 1859 |
1865 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) { | 1860 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) { |
1866 base::SharedMemory* shm = new base::SharedMemory(); | 1861 base::SharedMemory* shm = new base::SharedMemory(); |
1867 LOG_ASSERT(shm->CreateAndMapAnonymous(output_size)); | 1862 LOG_ASSERT(shm->CreateAndMapAnonymous(output_size)); |
1868 output_shms_.push_back(shm); | 1863 output_shms_.push_back(shm); |
1869 FeedEncoderWithOutput(shm, output_size); | 1864 FeedEncoderWithOutput(shm, output_size); |
1870 } | 1865 } |
1866 } | |
1867 | |
1868 void SimpleVEAClientBase::FeedEncoderWithOutput(base::SharedMemory* shm, | |
1869 size_t output_size) { | |
1870 if (!has_encoder()) | |
1871 return; | |
1872 | |
1873 base::SharedMemoryHandle dup_handle; | |
1874 LOG_ASSERT(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle)); | |
1875 | |
1876 BitstreamBuffer bitstream_buffer(next_output_buffer_id_++, dup_handle, | |
1877 output_size); | |
1878 encoder_->UseOutputBitstreamBuffer(bitstream_buffer); | |
1879 } | |
1880 | |
1881 // This client is only used to make sure the encoder does not return an encoded | |
1882 // frame before getting any input. | |
1883 class VEANoInputClient : public SimpleVEAClientBase { | |
1884 public: | |
1885 explicit VEANoInputClient(ClientStateNotification<ClientState>* note); | |
1886 ~VEANoInputClient() override; | |
1887 void DestroyEncoder(); | |
1888 | |
1889 // VideoDecodeAccelerator::Client implementation. | |
1890 void RequireBitstreamBuffers(unsigned int input_count, | |
1891 const gfx::Size& input_coded_size, | |
1892 size_t output_buffer_size) override; | |
1893 void BitstreamBufferReady(int32_t bitstream_buffer_id, | |
1894 size_t payload_size, | |
1895 bool key_frame, | |
1896 base::TimeDelta timestamp) override; | |
1897 | |
1898 private: | |
1899 // The timer used to monitor the encoder doesn't return an output buffer in | |
1900 // a period of time. | |
1901 std::unique_ptr<base::Timer> timer_; | |
1902 }; | |
1903 | |
1904 VEANoInputClient::VEANoInputClient(ClientStateNotification<ClientState>* note) | |
1905 : SimpleVEAClientBase(note, 320, 240) {} | |
1906 | |
1907 VEANoInputClient::~VEANoInputClient() { | |
1908 LOG_ASSERT(!has_encoder()); | |
1909 } | |
1910 | |
1911 void VEANoInputClient::DestroyEncoder() { | |
1912 SimpleVEAClientBase::DestroyEncoder(); | |
1913 // Clear the objects that should be destroyed on the same thread as creation. | |
1914 encoder_.reset(); | |
1915 } | |
1916 | |
1917 void VEANoInputClient::RequireBitstreamBuffers( | |
1918 unsigned int input_count, | |
1919 const gfx::Size& input_coded_size, | |
1920 size_t output_size) { | |
1921 SimpleVEAClientBase::RequireBitstreamBuffers(input_count, input_coded_size, | |
1922 output_size); | |
1923 | |
1871 // Timer is used to make sure there is no output frame in 100ms. | 1924 // Timer is used to make sure there is no output frame in 100ms. |
1872 timer_.reset(new base::Timer(FROM_HERE, | 1925 timer_.reset(new base::Timer(FROM_HERE, |
1873 base::TimeDelta::FromMilliseconds(100), | 1926 base::TimeDelta::FromMilliseconds(100), |
1874 base::Bind(&VEANoInputClient::SetState, | 1927 base::Bind(&VEANoInputClient::SetState, |
1875 base::Unretained(this), CS_FINISHED), | 1928 base::Unretained(this), CS_FINISHED), |
1876 false)); | 1929 false)); |
1877 timer_->Reset(); | 1930 timer_->Reset(); |
1878 } | 1931 } |
1879 | 1932 |
1880 void VEANoInputClient::BitstreamBufferReady(int32_t bitstream_buffer_id, | 1933 void VEANoInputClient::BitstreamBufferReady(int32_t bitstream_buffer_id, |
1881 size_t payload_size, | 1934 size_t payload_size, |
1882 bool key_frame, | 1935 bool key_frame, |
1883 base::TimeDelta timestamp) { | 1936 base::TimeDelta timestamp) { |
1884 DCHECK(thread_checker_.CalledOnValidThread()); | 1937 DCHECK(thread_checker_.CalledOnValidThread()); |
1885 SetState(CS_ERROR); | 1938 SetState(CS_ERROR); |
1886 } | 1939 } |
1887 | 1940 |
1888 void VEANoInputClient::NotifyError(VideoEncodeAccelerator::Error error) { | 1941 /* This client is only used to test input frame with the size of U and V planes |
wuchengli
2016/11/28 04:54:42
Use // comment style to be consistent with the res
hywu1
2016/11/29 05:47:06
Done.
| |
1889 DCHECK(thread_checker_.CalledOnValidThread()); | 1942 unaligned to cache line. |
1890 SetState(CS_ERROR); | 1943 To have both width and height divisible by 16 but not 32 will make the size |
1944 of U/V plane (width * height / 4) unaligned to 128-byte cache line. */ | |
1945 class VEACacheLineUnalignedInputClient : public SimpleVEAClientBase { | |
1946 public: | |
1947 explicit VEACacheLineUnalignedInputClient( | |
1948 ClientStateNotification<ClientState>* note); | |
1949 ~VEACacheLineUnalignedInputClient() override; | |
1950 | |
1951 // VideoDecodeAccelerator::Client implementation. | |
1952 void RequireBitstreamBuffers(unsigned int input_count, | |
1953 const gfx::Size& input_coded_size, | |
1954 size_t output_buffer_size) override; | |
1955 void BitstreamBufferReady(int32_t bitstream_buffer_id, | |
1956 size_t payload_size, | |
1957 bool key_frame, | |
1958 base::TimeDelta timestamp) override; | |
1959 | |
1960 private: | |
1961 // Feed the encoder with one input frame. | |
1962 void FeedEncoderWithOneInput(const gfx::Size& input_coded_size); | |
1963 }; | |
1964 | |
1965 VEACacheLineUnalignedInputClient::VEACacheLineUnalignedInputClient( | |
1966 ClientStateNotification<ClientState>* note) | |
1967 : SimpleVEAClientBase(note, 368, 368) { | |
1968 } // 368 is divisible by 16 but not 32 | |
1969 | |
1970 VEACacheLineUnalignedInputClient::~VEACacheLineUnalignedInputClient() { | |
1971 LOG_ASSERT(!has_encoder()); | |
1891 } | 1972 } |
1892 | 1973 |
1893 void VEANoInputClient::SetState(ClientState new_state) { | 1974 void VEACacheLineUnalignedInputClient::RequireBitstreamBuffers( |
1894 DVLOG(4) << "Changing state to " << new_state; | 1975 unsigned int input_count, |
1895 note_->Notify(new_state); | 1976 const gfx::Size& input_coded_size, |
1977 size_t output_size) { | |
1978 SimpleVEAClientBase::RequireBitstreamBuffers(input_count, input_coded_size, | |
1979 output_size); | |
1980 | |
1981 FeedEncoderWithOneInput(input_coded_size); | |
1896 } | 1982 } |
1897 | 1983 |
1898 void VEANoInputClient::FeedEncoderWithOutput(base::SharedMemory* shm, | 1984 void VEACacheLineUnalignedInputClient::BitstreamBufferReady( |
1899 size_t output_size) { | 1985 int32_t bitstream_buffer_id, |
1986 size_t payload_size, | |
1987 bool key_frame, | |
1988 base::TimeDelta timestamp) { | |
1989 DCHECK(thread_checker_.CalledOnValidThread()); | |
1990 // It's enough to encode just one frame | |
wuchengli
2016/11/28 04:54:42
// It's enough to encode just one frame. If plane
hywu1
2016/11/29 05:47:06
Done.
| |
1991 SetState(CS_FINISHED); | |
1992 } | |
1993 | |
1994 void VEACacheLineUnalignedInputClient::FeedEncoderWithOneInput( | |
1995 const gfx::Size& input_coded_size) { | |
1900 if (!has_encoder()) | 1996 if (!has_encoder()) |
1901 return; | 1997 return; |
1902 | 1998 |
1903 base::SharedMemoryHandle dup_handle; | 1999 std::vector<char, AlignedAllocator<char, kPlatformBufferAlignment>> |
1904 LOG_ASSERT(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle)); | 2000 aligned_in_file_data( |
2001 VideoFrame::AllocationSize(PIXEL_FORMAT_I420, input_coded_size)); | |
wuchengli
2016/11/28 04:54:42
s/PIXEL_FORMAT_I420/kInputFormat/. Same for line 2
hywu1
2016/11/29 05:47:06
Done.
| |
1905 | 2002 |
1906 BitstreamBuffer bitstream_buffer(next_output_buffer_id_++, dup_handle, | 2003 uint8_t* frame_data_y = reinterpret_cast<uint8_t*>(&aligned_in_file_data[0]); |
1907 output_size); | 2004 uint8_t* frame_data_u = |
wuchengli
2016/11/28 04:54:42
The starting address of u and v planes need to be
hywu1
2016/11/29 05:47:06
Done.
| |
1908 encoder_->UseOutputBitstreamBuffer(bitstream_buffer); | 2005 frame_data_y + |
2006 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, 0, input_coded_size).GetArea(); | |
2007 uint8_t* frame_data_v = | |
2008 frame_data_u + | |
2009 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, 1, input_coded_size).GetArea(); | |
2010 | |
2011 scoped_refptr<VideoFrame> video_frame = VideoFrame::WrapExternalYuvData( | |
2012 PIXEL_FORMAT_I420, input_coded_size, gfx::Rect(input_coded_size), | |
2013 input_coded_size, input_coded_size.width(), input_coded_size.width() / 2, | |
2014 input_coded_size.width() / 2, frame_data_y, frame_data_u, frame_data_v, | |
2015 base::TimeDelta().FromMilliseconds(base::Time::kMillisecondsPerSecond / | |
2016 default_fps_)); | |
2017 | |
2018 encoder_->Encode(video_frame, false); | |
1909 } | 2019 } |
1910 | 2020 |
1911 // Test parameters: | 2021 // Test parameters: |
1912 // - Number of concurrent encoders. The value takes effect when there is only | 2022 // - Number of concurrent encoders. The value takes effect when there is only |
1913 // one input stream; otherwise, one encoder per input stream will be | 2023 // one input stream; otherwise, one encoder per input stream will be |
1914 // instantiated. | 2024 // instantiated. |
1915 // - If true, save output to file (provided an output filename was supplied). | 2025 // - If true, save output to file (provided an output filename was supplied). |
1916 // - Force a keyframe every n frames. | 2026 // - Force a keyframe every n frames. |
1917 // - Force bitrate; the actual required value is provided as a property | 2027 // - Force bitrate; the actual required value is provided as a property |
1918 // of the input stream, because it depends on stream type/resolution/etc. | 2028 // of the input stream, because it depends on stream type/resolution/etc. |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2058 | 2168 |
2059 encoder_thread.task_runner()->PostTask( | 2169 encoder_thread.task_runner()->PostTask( |
2060 FROM_HERE, base::Bind(&VEANoInputClient::CreateEncoder, | 2170 FROM_HERE, base::Bind(&VEANoInputClient::CreateEncoder, |
2061 base::Unretained(client.get()))); | 2171 base::Unretained(client.get()))); |
2062 | 2172 |
2063 // Encoder must pass through states in this order. | 2173 // Encoder must pass through states in this order. |
2064 enum ClientState state_transitions[] = {CS_INITIALIZED, CS_ENCODING, | 2174 enum ClientState state_transitions[] = {CS_INITIALIZED, CS_ENCODING, |
2065 CS_FINISHED}; | 2175 CS_FINISHED}; |
2066 | 2176 |
2067 for (const auto& state : state_transitions) { | 2177 for (const auto& state : state_transitions) { |
2068 ASSERT_EQ(state, note->Wait()); | 2178 ASSERT_EQ(state, note->Wait()); |
wuchengli
2016/11/28 04:54:42
s/ASSERT_EQ/EXPECT_EQ/ so |client| will be always
hywu1
2016/11/29 05:47:06
Done.
| |
2069 } | 2179 } |
2070 | 2180 |
2071 encoder_thread.task_runner()->PostTask( | 2181 encoder_thread.task_runner()->PostTask( |
2072 FROM_HERE, base::Bind(&VEANoInputClient::DestroyEncoder, | 2182 FROM_HERE, base::Bind(&VEANoInputClient::DestroyEncoder, |
2073 base::Unretained(client.get()))); | 2183 base::Unretained(client.get()))); |
2074 | 2184 |
2075 // This ensures all tasks have finished. | 2185 // This ensures all tasks have finished. |
2076 encoder_thread.Stop(); | 2186 encoder_thread.Stop(); |
2077 } | 2187 } |
2078 | 2188 |
2189 TEST(VEACacheLineUnalignedInputTest, CheckOutput) { | |
wuchengli
2016/11/28 04:54:42
Combine TEST(VEANoInputTest, CheckOutput) and TEST
hywu1
2016/11/29 05:47:06
Done.
| |
2190 std::unique_ptr<ClientStateNotification<ClientState>> note( | |
2191 new ClientStateNotification<ClientState>()); | |
2192 std::unique_ptr<VEACacheLineUnalignedInputClient> client( | |
2193 new VEACacheLineUnalignedInputClient(note.get())); | |
2194 base::Thread encoder_thread("EncoderThread"); | |
2195 ASSERT_TRUE(encoder_thread.Start()); | |
2196 | |
2197 encoder_thread.task_runner()->PostTask( | |
2198 FROM_HERE, base::Bind(&VEACacheLineUnalignedInputClient::CreateEncoder, | |
2199 base::Unretained(client.get()))); | |
2200 | |
2201 // Encoder must pass through states in this order. | |
2202 enum ClientState state_transitions[] = {CS_INITIALIZED, CS_ENCODING, | |
2203 CS_FINISHED}; | |
2204 | |
2205 ClientState expected_state, wait_state; | |
2206 for (const auto& state : state_transitions) { | |
2207 expected_state = state; | |
2208 wait_state = note->Wait(); | |
wuchengli
2016/11/28 04:54:42
Use EXPECT_EQ(state, note->Wait());
hywu1
2016/11/29 05:47:06
Done.
| |
2209 if (expected_state != wait_state) { | |
2210 break; | |
2211 } | |
2212 } | |
2213 | |
2214 encoder_thread.task_runner()->PostTask( | |
2215 FROM_HERE, base::Bind(&VEACacheLineUnalignedInputClient::DestroyEncoder, | |
2216 base::Unretained(client.get()))); | |
2217 | |
2218 // This ensures all tasks have finished. | |
2219 encoder_thread.Stop(); | |
2220 | |
2221 ASSERT_EQ(expected_state, wait_state); | |
2222 } | |
2223 | |
2079 #elif defined(OS_MACOSX) || defined(OS_WIN) | 2224 #elif defined(OS_MACOSX) || defined(OS_WIN) |
2080 INSTANTIATE_TEST_CASE_P( | 2225 INSTANTIATE_TEST_CASE_P( |
2081 SimpleEncode, | 2226 SimpleEncode, |
2082 VideoEncodeAcceleratorTest, | 2227 VideoEncodeAcceleratorTest, |
2083 ::testing::Values( | 2228 ::testing::Values( |
2084 std::make_tuple(1, true, 0, false, false, false, false, false, false), | 2229 std::make_tuple(1, true, 0, false, false, false, false, false, false), |
2085 std::make_tuple(1, true, 0, false, false, false, false, true, false))); | 2230 std::make_tuple(1, true, 0, false, false, false, false, true, false))); |
2086 | 2231 |
2087 INSTANTIATE_TEST_CASE_P( | 2232 INSTANTIATE_TEST_CASE_P( |
2088 EncoderPerf, | 2233 EncoderPerf, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2199 | 2344 |
2200 media::g_env = | 2345 media::g_env = |
2201 reinterpret_cast<media::VideoEncodeAcceleratorTestEnvironment*>( | 2346 reinterpret_cast<media::VideoEncodeAcceleratorTestEnvironment*>( |
2202 testing::AddGlobalTestEnvironment( | 2347 testing::AddGlobalTestEnvironment( |
2203 new media::VideoEncodeAcceleratorTestEnvironment( | 2348 new media::VideoEncodeAcceleratorTestEnvironment( |
2204 std::move(test_stream_data), log_path, run_at_fps, | 2349 std::move(test_stream_data), log_path, run_at_fps, |
2205 needs_encode_latency, verify_all_output))); | 2350 needs_encode_latency, verify_all_output))); |
2206 | 2351 |
2207 return RUN_ALL_TESTS(); | 2352 return RUN_ALL_TESTS(); |
2208 } | 2353 } |
OLD | NEW |