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

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

Issue 2525563005: VEA unittest: add a test case for cache line-alignment (Closed)
Patch Set: VEA unittest: add a test case for cache line-alignment Created 4 years 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 <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
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 all VEA Clients in this file
878 class VEAClientBase : public VideoEncodeAccelerator::Client {
879 public:
880 ~VEAClientBase() override { LOG_ASSERT(!has_encoder()); }
881 void NotifyError(VideoEncodeAccelerator::Error error) override {
882 DCHECK(thread_checker_.CalledOnValidThread());
883 SetState(CS_ERROR);
884 }
885
886 protected:
887 VEAClientBase(ClientStateNotification<ClientState>* note)
888 : note_(note), next_output_buffer_id_(0) {}
889
890 bool has_encoder() { return encoder_.get(); }
891
892 virtual void SetState(ClientState new_state) = 0;
893
894 std::unique_ptr<VideoEncodeAccelerator> encoder_;
895
896 // Used to notify another thread about the state. VEAClientBase does not own
897 // this.
898 ClientStateNotification<ClientState>* note_;
899
900 // All methods of this class should be run on the same thread.
901 base::ThreadChecker thread_checker_;
902
903 ScopedVector<base::SharedMemory> output_shms_;
904 int32_t next_output_buffer_id_;
905 };
906
907 class VEAClient : public VEAClientBase {
878 public: 908 public:
879 VEAClient(TestStream* test_stream, 909 VEAClient(TestStream* test_stream,
880 ClientStateNotification<ClientState>* note, 910 ClientStateNotification<ClientState>* note,
881 bool save_to_file, 911 bool save_to_file,
882 unsigned int keyframe_period, 912 unsigned int keyframe_period,
883 bool force_bitrate, 913 bool force_bitrate,
884 bool test_perf, 914 bool test_perf,
885 bool mid_stream_bitrate_switch, 915 bool mid_stream_bitrate_switch,
886 bool mid_stream_framerate_switch, 916 bool mid_stream_framerate_switch,
887 bool verify_output, 917 bool verify_output,
888 bool verify_output_timestamp); 918 bool verify_output_timestamp);
889 ~VEAClient() override;
890 void CreateEncoder(); 919 void CreateEncoder();
891 void DestroyEncoder(); 920 void DestroyEncoder();
892 921
893 void TryToSetupEncodeOnSeperateThread(); 922 void TryToSetupEncodeOnSeperateThread();
894 void DestroyEncodeOnSeperateThread(); 923 void DestroyEncodeOnSeperateThread();
895 924
896 // VideoDecodeAccelerator::Client implementation. 925 // VideoDecodeAccelerator::Client implementation.
897 void RequireBitstreamBuffers(unsigned int input_count, 926 void RequireBitstreamBuffers(unsigned int input_count,
898 const gfx::Size& input_coded_size, 927 const gfx::Size& input_coded_size,
899 size_t output_buffer_size) override; 928 size_t output_buffer_size) override;
900 void BitstreamBufferReady(int32_t bitstream_buffer_id, 929 void BitstreamBufferReady(int32_t bitstream_buffer_id,
901 size_t payload_size, 930 size_t payload_size,
902 bool key_frame, 931 bool key_frame,
903 base::TimeDelta timestamp) override; 932 base::TimeDelta timestamp) override;
904 void NotifyError(VideoEncodeAccelerator::Error error) override;
905 933
906 private: 934 private:
907 void BitstreamBufferReadyOnMainThread(int32_t bitstream_buffer_id, 935 void BitstreamBufferReadyOnMainThread(int32_t bitstream_buffer_id,
908 size_t payload_size, 936 size_t payload_size,
909 bool key_frame, 937 bool key_frame,
910 base::TimeDelta timestamp); 938 base::TimeDelta timestamp);
911 bool has_encoder() { return encoder_.get(); }
912 939
913 // Return the number of encoded frames per second. 940 // Return the number of encoded frames per second.
914 double frames_per_second(); 941 double frames_per_second();
915 942
916 void SetState(ClientState new_state); 943 void SetState(ClientState new_state) override;
917 944
918 // Set current stream parameters to given |bitrate| at |framerate|. 945 // Set current stream parameters to given |bitrate| at |framerate|.
919 void SetStreamParameters(unsigned int bitrate, unsigned int framerate); 946 void SetStreamParameters(unsigned int bitrate, unsigned int framerate);
920 947
921 // Called when encoder is done with a VideoFrame. 948 // Called when encoder is done with a VideoFrame.
922 void InputNoLongerNeededCallback(int32_t input_id); 949 void InputNoLongerNeededCallback(int32_t input_id);
923 950
924 // Feed the encoder with one input frame. 951 // Feed the encoder with one input frame.
925 void FeedEncoderWithOneInput(); 952 void FeedEncoderWithOneInput();
926 953
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 // Called when the quality validator has decoded all the frames. 998 // Called when the quality validator has decoded all the frames.
972 void DecodeCompleted(); 999 void DecodeCompleted();
973 1000
974 // Called when the quality validator fails to decode a frame. 1001 // Called when the quality validator fails to decode a frame.
975 void DecodeFailed(); 1002 void DecodeFailed();
976 1003
977 // Verify that the output timestamp matches input timestamp. 1004 // Verify that the output timestamp matches input timestamp.
978 void VerifyOutputTimestamp(base::TimeDelta timestamp); 1005 void VerifyOutputTimestamp(base::TimeDelta timestamp);
979 1006
980 ClientState state_; 1007 ClientState state_;
981 std::unique_ptr<VideoEncodeAccelerator> encoder_;
982 1008
983 TestStream* test_stream_; 1009 TestStream* test_stream_;
984 1010
985 // Used to notify another thread about the state. VEAClient does not own this.
986 ClientStateNotification<ClientState>* note_;
987
988 // Ids assigned to VideoFrames. 1011 // Ids assigned to VideoFrames.
989 std::set<int32_t> inputs_at_client_; 1012 std::set<int32_t> inputs_at_client_;
990 int32_t next_input_id_; 1013 int32_t next_input_id_;
991 1014
992 // Encode start time of all encoded frames. The position in the vector is the 1015 // Encode start time of all encoded frames. The position in the vector is the
993 // frame input id. 1016 // frame input id.
994 std::vector<base::TimeTicks> encode_start_time_; 1017 std::vector<base::TimeTicks> encode_start_time_;
995 // The encode latencies of all encoded frames. We define encode latency as the 1018 // 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 1019 // time delay from input of each VideoFrame (VEA::Encode()) to output of the
997 // corresponding BitstreamBuffer (VEA::Client::BitstreamBufferReady()). 1020 // corresponding BitstreamBuffer (VEA::Client::BitstreamBufferReady()).
998 std::vector<base::TimeDelta> encode_latencies_; 1021 std::vector<base::TimeDelta> encode_latencies_;
999 1022
1000 // Ids for output BitstreamBuffers. 1023 // Ids for output BitstreamBuffers.
1001 typedef std::map<int32_t, base::SharedMemory*> IdToSHM; 1024 typedef std::map<int32_t, base::SharedMemory*> IdToSHM;
1002 ScopedVector<base::SharedMemory> output_shms_;
1003 IdToSHM output_buffers_at_client_; 1025 IdToSHM output_buffers_at_client_;
1004 int32_t next_output_buffer_id_;
1005 1026
1006 // Current offset into input stream. 1027 // Current offset into input stream.
1007 off_t pos_in_input_stream_; 1028 off_t pos_in_input_stream_;
1008 gfx::Size input_coded_size_; 1029 gfx::Size input_coded_size_;
1009 // Requested by encoder. 1030 // Requested by encoder.
1010 unsigned int num_required_input_buffers_; 1031 unsigned int num_required_input_buffers_;
1011 size_t output_buffer_size_; 1032 size_t output_buffer_size_;
1012 1033
1013 // Number of frames to encode. This may differ from the number of frames in 1034 // Number of frames to encode. This may differ from the number of frames in
1014 // stream if we need more frames for bitrate tests. 1035 // stream if we need more frames for bitrate tests.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1062 1083
1063 // Used to validate the encoded frame quality. 1084 // Used to validate the encoded frame quality.
1064 std::unique_ptr<VideoFrameQualityValidator> quality_validator_; 1085 std::unique_ptr<VideoFrameQualityValidator> quality_validator_;
1065 1086
1066 // The time when the first frame is submitted for encode. 1087 // The time when the first frame is submitted for encode.
1067 base::TimeTicks first_frame_start_time_; 1088 base::TimeTicks first_frame_start_time_;
1068 1089
1069 // The time when the last encoded frame is ready. 1090 // The time when the last encoded frame is ready.
1070 base::TimeTicks last_frame_ready_time_; 1091 base::TimeTicks last_frame_ready_time_;
1071 1092
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. 1093 // Requested bitrate in bits per second.
1076 unsigned int requested_bitrate_; 1094 unsigned int requested_bitrate_;
1077 1095
1078 // Requested initial framerate. 1096 // Requested initial framerate.
1079 unsigned int requested_framerate_; 1097 unsigned int requested_framerate_;
1080 1098
1081 // Bitrate to switch to in the middle of the stream. 1099 // Bitrate to switch to in the middle of the stream.
1082 unsigned int requested_subsequent_bitrate_; 1100 unsigned int requested_subsequent_bitrate_;
1083 1101
1084 // Framerate to switch to in the middle of the stream. 1102 // Framerate to switch to in the middle of the stream.
(...skipping 30 matching lines...) Expand all
1115 VEAClient::VEAClient(TestStream* test_stream, 1133 VEAClient::VEAClient(TestStream* test_stream,
1116 ClientStateNotification<ClientState>* note, 1134 ClientStateNotification<ClientState>* note,
1117 bool save_to_file, 1135 bool save_to_file,
1118 unsigned int keyframe_period, 1136 unsigned int keyframe_period,
1119 bool force_bitrate, 1137 bool force_bitrate,
1120 bool test_perf, 1138 bool test_perf,
1121 bool mid_stream_bitrate_switch, 1139 bool mid_stream_bitrate_switch,
1122 bool mid_stream_framerate_switch, 1140 bool mid_stream_framerate_switch,
1123 bool verify_output, 1141 bool verify_output,
1124 bool verify_output_timestamp) 1142 bool verify_output_timestamp)
1125 : state_(CS_CREATED), 1143 : VEAClientBase(note),
1144 state_(CS_CREATED),
1126 test_stream_(test_stream), 1145 test_stream_(test_stream),
1127 note_(note),
1128 next_input_id_(0), 1146 next_input_id_(0),
1129 next_output_buffer_id_(0),
1130 pos_in_input_stream_(0), 1147 pos_in_input_stream_(0),
1131 num_required_input_buffers_(0), 1148 num_required_input_buffers_(0),
1132 output_buffer_size_(0), 1149 output_buffer_size_(0),
1133 num_frames_to_encode_(0), 1150 num_frames_to_encode_(0),
1134 num_encoded_frames_(0), 1151 num_encoded_frames_(0),
1135 num_frames_since_last_check_(0), 1152 num_frames_since_last_check_(0),
1136 seen_keyframe_in_this_buffer_(false), 1153 seen_keyframe_in_this_buffer_(false),
1137 save_to_file_(save_to_file), 1154 save_to_file_(save_to_file),
1138 keyframe_period_(keyframe_period), 1155 keyframe_period_(keyframe_period),
1139 num_keyframes_requested_(0), 1156 num_keyframes_requested_(0),
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 // Without it, AppendToFile() will not work. 1190 // Without it, AppendToFile() will not work.
1174 EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0)); 1191 EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0));
1175 } 1192 }
1176 1193
1177 // Initialize the parameters of the test streams. 1194 // Initialize the parameters of the test streams.
1178 UpdateTestStreamData(mid_stream_bitrate_switch, mid_stream_framerate_switch); 1195 UpdateTestStreamData(mid_stream_bitrate_switch, mid_stream_framerate_switch);
1179 1196
1180 thread_checker_.DetachFromThread(); 1197 thread_checker_.DetachFromThread();
1181 } 1198 }
1182 1199
1183 VEAClient::~VEAClient() {
1184 LOG_ASSERT(!has_encoder());
1185 }
1186
1187 void VEAClient::CreateEncoder() { 1200 void VEAClient::CreateEncoder() {
1188 DCHECK(thread_checker_.CalledOnValidThread()); 1201 DCHECK(thread_checker_.CalledOnValidThread());
1189 LOG_ASSERT(!has_encoder()); 1202 LOG_ASSERT(!has_encoder());
1190 1203
1191 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 1204 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get();
1192 encode_task_runner_ = main_thread_task_runner_; 1205 encode_task_runner_ = main_thread_task_runner_;
1193 1206
1194 std::unique_ptr<VideoEncodeAccelerator> encoders[] = { 1207 std::unique_ptr<VideoEncodeAccelerator> encoders[] = {
1195 CreateFakeVEA(), CreateV4L2VEA(), CreateVaapiVEA(), CreateVTVEA(), 1208 CreateFakeVEA(), CreateV4L2VEA(), CreateVaapiVEA(), CreateVTVEA(),
1196 CreateMFVEA()}; 1209 CreateMFVEA()};
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
1397 size_t payload_size, 1410 size_t payload_size,
1398 bool key_frame, 1411 bool key_frame,
1399 base::TimeDelta timestamp) { 1412 base::TimeDelta timestamp) {
1400 ASSERT_TRUE(encode_task_runner_->BelongsToCurrentThread()); 1413 ASSERT_TRUE(encode_task_runner_->BelongsToCurrentThread());
1401 main_thread_task_runner_->PostTask( 1414 main_thread_task_runner_->PostTask(
1402 FROM_HERE, base::Bind(&VEAClient::BitstreamBufferReadyOnMainThread, 1415 FROM_HERE, base::Bind(&VEAClient::BitstreamBufferReadyOnMainThread,
1403 base::Unretained(this), bitstream_buffer_id, 1416 base::Unretained(this), bitstream_buffer_id,
1404 payload_size, key_frame, timestamp)); 1417 payload_size, key_frame, timestamp));
1405 } 1418 }
1406 1419
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, 1420 void VEAClient::BitstreamBufferReadyOnMainThread(int32_t bitstream_buffer_id,
1413 size_t payload_size, 1421 size_t payload_size,
1414 bool key_frame, 1422 bool key_frame,
1415 base::TimeDelta timestamp) { 1423 base::TimeDelta timestamp) {
1416 DCHECK(thread_checker_.CalledOnValidThread()); 1424 DCHECK(thread_checker_.CalledOnValidThread());
1417 1425
1418 ASSERT_LE(payload_size, output_buffer_size_); 1426 ASSERT_LE(payload_size, output_buffer_size_);
1419 1427
1420 IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id); 1428 IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id);
1421 ASSERT_NE(it, output_buffers_at_client_.end()); 1429 ASSERT_NE(it, output_buffers_at_client_.end());
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
1752 IvfFrameHeader header = {}; 1760 IvfFrameHeader header = {};
1753 1761
1754 header.frame_size = static_cast<uint32_t>(frame_size); 1762 header.frame_size = static_cast<uint32_t>(frame_size);
1755 header.timestamp = frame_index; 1763 header.timestamp = frame_index;
1756 header.ByteSwap(); 1764 header.ByteSwap();
1757 EXPECT_TRUE(base::AppendToFile( 1765 EXPECT_TRUE(base::AppendToFile(
1758 base::FilePath::FromUTF8Unsafe(test_stream_->out_filename), 1766 base::FilePath::FromUTF8Unsafe(test_stream_->out_filename),
1759 reinterpret_cast<char*>(&header), sizeof(header))); 1767 reinterpret_cast<char*>(&header), sizeof(header)));
1760 } 1768 }
1761 1769
1762 // This client is only used to make sure the encoder does not return an encoded 1770 // Base class for simple VEA Clients
1763 // frame before getting any input. 1771 class SimpleVEAClientBase : public VEAClientBase {
1764 class VEANoInputClient : public VideoEncodeAccelerator::Client {
1765 public: 1772 public:
1766 explicit VEANoInputClient(ClientStateNotification<ClientState>* note);
1767 ~VEANoInputClient() override;
1768 void CreateEncoder(); 1773 void CreateEncoder();
1769 void DestroyEncoder(); 1774 void DestroyEncoder();
1770 1775
1771 // VideoDecodeAccelerator::Client implementation. 1776 // VideoDecodeAccelerator::Client implementation.
1772 void RequireBitstreamBuffers(unsigned int input_count, 1777 void RequireBitstreamBuffers(unsigned int input_count,
1773 const gfx::Size& input_coded_size, 1778 const gfx::Size& input_coded_size,
1774 size_t output_buffer_size) override; 1779 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 1780
1781 private: 1781 protected:
1782 bool has_encoder() { return encoder_.get(); } 1782 SimpleVEAClientBase(ClientStateNotification<ClientState>* note,
1783 const int width,
1784 const int height);
1783 1785
1784 void SetState(ClientState new_state); 1786 void SetState(ClientState new_state) override;
1785 1787
1786 // Provide the encoder with a new output buffer. 1788 // Provide the encoder with a new output buffer.
1787 void FeedEncoderWithOutput(base::SharedMemory* shm, size_t output_size); 1789 void FeedEncoderWithOutput(base::SharedMemory* shm, size_t output_size);
1788 1790
1789 std::unique_ptr<VideoEncodeAccelerator> encoder_; 1791 const int width_;
1790 1792 const int height_;
1791 // Used to notify another thread about the state. VEAClient does not own this. 1793 const int bitrate_;
1792 ClientStateNotification<ClientState>* note_; 1794 const int 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 }; 1795 };
1805 1796
1806 VEANoInputClient::VEANoInputClient(ClientStateNotification<ClientState>* note) 1797 SimpleVEAClientBase::SimpleVEAClientBase(
1807 : note_(note), next_output_buffer_id_(0) { 1798 ClientStateNotification<ClientState>* note,
1799 const int width,
1800 const int height)
1801 : VEAClientBase(note),
1802 width_(width),
1803 height_(height),
1804 bitrate_(200000),
1805 fps_(30) {
1808 thread_checker_.DetachFromThread(); 1806 thread_checker_.DetachFromThread();
1809 } 1807 }
1810 1808
1811 VEANoInputClient::~VEANoInputClient() { 1809 void SimpleVEAClientBase::CreateEncoder() {
1812 LOG_ASSERT(!has_encoder());
1813 }
1814
1815 void VEANoInputClient::CreateEncoder() {
1816 DCHECK(thread_checker_.CalledOnValidThread()); 1810 DCHECK(thread_checker_.CalledOnValidThread());
1817 LOG_ASSERT(!has_encoder()); 1811 LOG_ASSERT(!has_encoder());
1818 LOG_ASSERT(g_env->test_streams_.size()); 1812 LOG_ASSERT(g_env->test_streams_.size());
1819 1813
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[] = { 1814 std::unique_ptr<VideoEncodeAccelerator> encoders[] = {
1826 CreateFakeVEA(), CreateV4L2VEA(), CreateVaapiVEA(), CreateVTVEA(), 1815 CreateFakeVEA(), CreateV4L2VEA(), CreateVaapiVEA(), CreateVTVEA(),
1827 CreateMFVEA()}; 1816 CreateMFVEA()};
1828 1817
1829 // The test case is no input. Use default visible size, bitrate, and fps. 1818 gfx::Size visible_size(width_, height_);
1830 gfx::Size visible_size(kDefaultWidth, kDefaultHeight);
1831 for (auto& encoder : encoders) { 1819 for (auto& encoder : encoders) {
1832 if (!encoder) 1820 if (!encoder)
1833 continue; 1821 continue;
1834 encoder_ = std::move(encoder); 1822 encoder_ = std::move(encoder);
1835 if (encoder_->Initialize(kInputFormat, visible_size, 1823 if (encoder_->Initialize(kInputFormat, visible_size,
1836 g_env->test_streams_[0]->requested_profile, 1824 g_env->test_streams_[0]->requested_profile,
1837 kDefaultBitrate, this)) { 1825 bitrate_, this)) {
1838 encoder_->RequestEncodingParametersChange(kDefaultBitrate, kDefaultFps); 1826 encoder_->RequestEncodingParametersChange(bitrate_, fps_);
1839 SetState(CS_INITIALIZED); 1827 SetState(CS_INITIALIZED);
1840 return; 1828 return;
1841 } 1829 }
1842 } 1830 }
1843 encoder_.reset(); 1831 encoder_.reset();
1844 LOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed"; 1832 LOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed";
1845 SetState(CS_ERROR); 1833 SetState(CS_ERROR);
1846 } 1834 }
1847 1835
1848 void VEANoInputClient::DestroyEncoder() { 1836 void SimpleVEAClientBase::DestroyEncoder() {
1849 DCHECK(thread_checker_.CalledOnValidThread()); 1837 DCHECK(thread_checker_.CalledOnValidThread());
1850 if (!has_encoder()) 1838 if (!has_encoder())
1851 return; 1839 return;
1852 // Clear the objects that should be destroyed on the same thread as creation. 1840 // Clear the objects that should be destroyed on the same thread as creation.
1853 encoder_.reset(); 1841 encoder_.reset();
1854 timer_.reset();
1855 } 1842 }
1856 1843
1857 void VEANoInputClient::RequireBitstreamBuffers( 1844 void SimpleVEAClientBase::SetState(ClientState new_state) {
1845 DVLOG(4) << "Changing state to " << new_state;
1846 note_->Notify(new_state);
1847 }
1848
1849 void SimpleVEAClientBase::RequireBitstreamBuffers(
1858 unsigned int input_count, 1850 unsigned int input_count,
1859 const gfx::Size& input_coded_size, 1851 const gfx::Size& input_coded_size,
1860 size_t output_size) { 1852 size_t output_size) {
1861 DCHECK(thread_checker_.CalledOnValidThread()); 1853 DCHECK(thread_checker_.CalledOnValidThread());
1862 SetState(CS_ENCODING); 1854 SetState(CS_ENCODING);
1863 ASSERT_GT(output_size, 0UL); 1855 ASSERT_GT(output_size, 0UL);
1864 1856
1865 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) { 1857 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) {
1866 base::SharedMemory* shm = new base::SharedMemory(); 1858 base::SharedMemory* shm = new base::SharedMemory();
1867 LOG_ASSERT(shm->CreateAndMapAnonymous(output_size)); 1859 LOG_ASSERT(shm->CreateAndMapAnonymous(output_size));
1868 output_shms_.push_back(shm); 1860 output_shms_.push_back(shm);
1869 FeedEncoderWithOutput(shm, output_size); 1861 FeedEncoderWithOutput(shm, output_size);
1870 } 1862 }
1863 }
1864
1865 void SimpleVEAClientBase::FeedEncoderWithOutput(base::SharedMemory* shm,
1866 size_t output_size) {
1867 if (!has_encoder())
1868 return;
1869
1870 base::SharedMemoryHandle dup_handle;
1871 LOG_ASSERT(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle));
1872
1873 BitstreamBuffer bitstream_buffer(next_output_buffer_id_++, dup_handle,
1874 output_size);
1875 encoder_->UseOutputBitstreamBuffer(bitstream_buffer);
1876 }
1877
1878 // This client is only used to make sure the encoder does not return an encoded
1879 // frame before getting any input.
1880 class VEANoInputClient : public SimpleVEAClientBase {
1881 public:
1882 explicit VEANoInputClient(ClientStateNotification<ClientState>* note);
1883 void DestroyEncoder();
1884
1885 // VideoDecodeAccelerator::Client implementation.
1886 void RequireBitstreamBuffers(unsigned int input_count,
1887 const gfx::Size& input_coded_size,
1888 size_t output_buffer_size) override;
1889 void BitstreamBufferReady(int32_t bitstream_buffer_id,
1890 size_t payload_size,
1891 bool key_frame,
1892 base::TimeDelta timestamp) override;
1893
1894 private:
1895 // The timer used to monitor the encoder doesn't return an output buffer in
1896 // a period of time.
1897 std::unique_ptr<base::Timer> timer_;
1898 };
1899
1900 VEANoInputClient::VEANoInputClient(ClientStateNotification<ClientState>* note)
1901 : SimpleVEAClientBase(note, 320, 240) {}
1902
1903 void VEANoInputClient::DestroyEncoder() {
1904 SimpleVEAClientBase::DestroyEncoder();
1905 // Clear the objects that should be destroyed on the same thread as creation.
1906 timer_.reset();
1907 }
1908
1909 void VEANoInputClient::RequireBitstreamBuffers(
1910 unsigned int input_count,
1911 const gfx::Size& input_coded_size,
1912 size_t output_size) {
1913 SimpleVEAClientBase::RequireBitstreamBuffers(input_count, input_coded_size,
1914 output_size);
1915
1871 // Timer is used to make sure there is no output frame in 100ms. 1916 // Timer is used to make sure there is no output frame in 100ms.
1872 timer_.reset(new base::Timer(FROM_HERE, 1917 timer_.reset(new base::Timer(FROM_HERE,
1873 base::TimeDelta::FromMilliseconds(100), 1918 base::TimeDelta::FromMilliseconds(100),
1874 base::Bind(&VEANoInputClient::SetState, 1919 base::Bind(&VEANoInputClient::SetState,
1875 base::Unretained(this), CS_FINISHED), 1920 base::Unretained(this), CS_FINISHED),
1876 false)); 1921 false));
1877 timer_->Reset(); 1922 timer_->Reset();
1878 } 1923 }
1879 1924
1880 void VEANoInputClient::BitstreamBufferReady(int32_t bitstream_buffer_id, 1925 void VEANoInputClient::BitstreamBufferReady(int32_t bitstream_buffer_id,
1881 size_t payload_size, 1926 size_t payload_size,
1882 bool key_frame, 1927 bool key_frame,
1883 base::TimeDelta timestamp) { 1928 base::TimeDelta timestamp) {
1884 DCHECK(thread_checker_.CalledOnValidThread()); 1929 DCHECK(thread_checker_.CalledOnValidThread());
1885 SetState(CS_ERROR); 1930 SetState(CS_ERROR);
1886 } 1931 }
1887 1932
1888 void VEANoInputClient::NotifyError(VideoEncodeAccelerator::Error error) { 1933 // This client is only used to test input frame with the size of U and V planes
1889 DCHECK(thread_checker_.CalledOnValidThread()); 1934 // unaligned to cache line.
1890 SetState(CS_ERROR); 1935 // To have both width and height divisible by 16 but not 32 will make the size
1936 // of U/V plane (width * height / 4) unaligned to 128-byte cache line.
1937 class VEACacheLineUnalignedInputClient : public SimpleVEAClientBase {
1938 public:
1939 explicit VEACacheLineUnalignedInputClient(
1940 ClientStateNotification<ClientState>* note);
1941
1942 // VideoDecodeAccelerator::Client implementation.
1943 void RequireBitstreamBuffers(unsigned int input_count,
1944 const gfx::Size& input_coded_size,
1945 size_t output_buffer_size) override;
1946 void BitstreamBufferReady(int32_t bitstream_buffer_id,
1947 size_t payload_size,
1948 bool key_frame,
1949 base::TimeDelta timestamp) override;
1950
1951 private:
1952 // Feed the encoder with one input frame.
1953 void FeedEncoderWithOneInput(const gfx::Size& input_coded_size);
1954 };
1955
1956 VEACacheLineUnalignedInputClient::VEACacheLineUnalignedInputClient(
1957 ClientStateNotification<ClientState>* note)
1958 : SimpleVEAClientBase(note, 368, 368) {
1959 } // 368 is divisible by 16 but not 32
1960
1961 void VEACacheLineUnalignedInputClient::RequireBitstreamBuffers(
1962 unsigned int input_count,
1963 const gfx::Size& input_coded_size,
1964 size_t output_size) {
1965 SimpleVEAClientBase::RequireBitstreamBuffers(input_count, input_coded_size,
1966 output_size);
1967
1968 FeedEncoderWithOneInput(input_coded_size);
1891 } 1969 }
1892 1970
1893 void VEANoInputClient::SetState(ClientState new_state) { 1971 void VEACacheLineUnalignedInputClient::BitstreamBufferReady(
1894 DVLOG(4) << "Changing state to " << new_state; 1972 int32_t bitstream_buffer_id,
1895 note_->Notify(new_state); 1973 size_t payload_size,
1974 bool key_frame,
1975 base::TimeDelta timestamp) {
1976 DCHECK(thread_checker_.CalledOnValidThread());
1977 // It's enough to encode just one frame. If plane size is not aligned,
1978 // VideoEncodeAccelerator::Encode will fail.
1979 SetState(CS_FINISHED);
1896 } 1980 }
1897 1981
1898 void VEANoInputClient::FeedEncoderWithOutput(base::SharedMemory* shm, 1982 void VEACacheLineUnalignedInputClient::FeedEncoderWithOneInput(
1899 size_t output_size) { 1983 const gfx::Size& input_coded_size) {
1900 if (!has_encoder()) 1984 if (!has_encoder())
1901 return; 1985 return;
1902 1986
1903 base::SharedMemoryHandle dup_handle; 1987 std::vector<char, AlignedAllocator<char, kPlatformBufferAlignment>>
1904 LOG_ASSERT(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle)); 1988 aligned_data_y, aligned_data_u, aligned_data_v;
1989 aligned_data_y.resize(
1990 VideoFrame::PlaneSize(kInputFormat, 0, input_coded_size).GetArea());
1991 aligned_data_u.resize(
1992 VideoFrame::PlaneSize(kInputFormat, 1, input_coded_size).GetArea());
1993 aligned_data_v.resize(
1994 VideoFrame::PlaneSize(kInputFormat, 2, input_coded_size).GetArea());
1995 uint8_t* frame_data_y = reinterpret_cast<uint8_t*>(&aligned_data_y[0]);
1996 uint8_t* frame_data_u = reinterpret_cast<uint8_t*>(&aligned_data_u[0]);
1997 uint8_t* frame_data_v = reinterpret_cast<uint8_t*>(&aligned_data_v[0]);
1905 1998
1906 BitstreamBuffer bitstream_buffer(next_output_buffer_id_++, dup_handle, 1999 scoped_refptr<VideoFrame> video_frame = VideoFrame::WrapExternalYuvData(
1907 output_size); 2000 kInputFormat, input_coded_size, gfx::Rect(input_coded_size),
1908 encoder_->UseOutputBitstreamBuffer(bitstream_buffer); 2001 input_coded_size, input_coded_size.width(), input_coded_size.width() / 2,
2002 input_coded_size.width() / 2, frame_data_y, frame_data_u, frame_data_v,
2003 base::TimeDelta().FromMilliseconds(base::Time::kMillisecondsPerSecond /
2004 fps_));
2005
2006 encoder_->Encode(video_frame, false);
1909 } 2007 }
1910 2008
1911 // Test parameters: 2009 // Test parameters:
1912 // - Number of concurrent encoders. The value takes effect when there is only 2010 // - Number of concurrent encoders. The value takes effect when there is only
1913 // one input stream; otherwise, one encoder per input stream will be 2011 // one input stream; otherwise, one encoder per input stream will be
1914 // instantiated. 2012 // instantiated.
1915 // - If true, save output to file (provided an output filename was supplied). 2013 // - If true, save output to file (provided an output filename was supplied).
1916 // - Force a keyframe every n frames. 2014 // - Force a keyframe every n frames.
1917 // - Force bitrate; the actual required value is provided as a property 2015 // - Force bitrate; the actual required value is provided as a property
1918 // of the input stream, because it depends on stream type/resolution/etc. 2016 // of the input stream, because it depends on stream type/resolution/etc.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1984 for (size_t i = 0; i < num_concurrent_encoders; ++i) { 2082 for (size_t i = 0; i < num_concurrent_encoders; ++i) {
1985 encoder_thread.task_runner()->PostTask( 2083 encoder_thread.task_runner()->PostTask(
1986 FROM_HERE, 2084 FROM_HERE,
1987 base::Bind(&VEAClient::DestroyEncoder, base::Unretained(clients[i]))); 2085 base::Bind(&VEAClient::DestroyEncoder, base::Unretained(clients[i])));
1988 } 2086 }
1989 2087
1990 // This ensures all tasks have finished. 2088 // This ensures all tasks have finished.
1991 encoder_thread.Stop(); 2089 encoder_thread.Stop();
1992 } 2090 }
1993 2091
2092 // Test parameters:
2093 // - Test type
2094 // 0: No input test
2095 // 1: Cache line-unaligned test
2096 class VideoEncodeAcceleratorSimpleTest : public ::testing::TestWithParam<int> {
2097 };
2098
2099 template <class TestClient>
2100 void SimpleTestFunc() {
2101 std::unique_ptr<ClientStateNotification<ClientState>> note(
2102 new ClientStateNotification<ClientState>());
2103 std::unique_ptr<TestClient> client(new TestClient(note.get()));
2104 base::Thread encoder_thread("EncoderThread");
2105 ASSERT_TRUE(encoder_thread.Start());
2106
2107 encoder_thread.task_runner()->PostTask(
2108 FROM_HERE,
2109 base::Bind(&TestClient::CreateEncoder, base::Unretained(client.get())));
2110
2111 // Encoder must pass through states in this order.
2112 enum ClientState state_transitions[] = {CS_INITIALIZED, CS_ENCODING,
2113 CS_FINISHED};
2114
2115 for (const auto& state : state_transitions) {
2116 EXPECT_EQ(state, note->Wait());
2117 }
2118
2119 encoder_thread.task_runner()->PostTask(
2120 FROM_HERE,
2121 base::Bind(&TestClient::DestroyEncoder, base::Unretained(client.get())));
2122
2123 // This ensures all tasks have finished.
2124 encoder_thread.Stop();
2125 }
2126
2127 TEST_P(VideoEncodeAcceleratorSimpleTest, TestSimpleEncode) {
2128 const int test_type = GetParam();
2129 ASSERT_LT(test_type, 2) << "Invalid test type=" << test_type;
2130
2131 if (test_type == 0)
2132 SimpleTestFunc<VEANoInputClient>();
2133 else if (test_type == 1)
2134 SimpleTestFunc<VEACacheLineUnalignedInputClient>();
2135 }
2136
1994 #if defined(OS_CHROMEOS) 2137 #if defined(OS_CHROMEOS)
1995 INSTANTIATE_TEST_CASE_P( 2138 INSTANTIATE_TEST_CASE_P(
1996 SimpleEncode, 2139 SimpleEncode,
1997 VideoEncodeAcceleratorTest, 2140 VideoEncodeAcceleratorTest,
1998 ::testing::Values( 2141 ::testing::Values(
1999 std::make_tuple(1, true, 0, false, false, false, false, false, false), 2142 std::make_tuple(1, true, 0, false, false, false, false, false, false),
2000 std::make_tuple(1, true, 0, false, false, false, false, true, false))); 2143 std::make_tuple(1, true, 0, false, false, false, false, true, false)));
2001 2144
2002 INSTANTIATE_TEST_CASE_P( 2145 INSTANTIATE_TEST_CASE_P(
2003 EncoderPerf, 2146 EncoderPerf,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2042 std::make_tuple(3, false, 0, false, false, false, false, false, false), 2185 std::make_tuple(3, false, 0, false, false, false, false, false, false),
2043 std::make_tuple(3, false, 0, true, false, false, true, false, false), 2186 std::make_tuple(3, false, 0, true, false, false, true, false, false),
2044 std::make_tuple(3, false, 0, true, false, true, false, false, false))); 2187 std::make_tuple(3, false, 0, true, false, true, false, false, false)));
2045 2188
2046 INSTANTIATE_TEST_CASE_P( 2189 INSTANTIATE_TEST_CASE_P(
2047 VerifyTimestamp, 2190 VerifyTimestamp,
2048 VideoEncodeAcceleratorTest, 2191 VideoEncodeAcceleratorTest,
2049 ::testing::Values( 2192 ::testing::Values(
2050 std::make_tuple(1, false, 0, false, false, false, false, false, true))); 2193 std::make_tuple(1, false, 0, false, false, false, false, false, true)));
2051 2194
2052 TEST(VEANoInputTest, CheckOutput) { 2195 INSTANTIATE_TEST_CASE_P(NoInputTest,
2053 std::unique_ptr<ClientStateNotification<ClientState>> note( 2196 VideoEncodeAcceleratorSimpleTest,
2054 new ClientStateNotification<ClientState>()); 2197 ::testing::Values(0));
2055 std::unique_ptr<VEANoInputClient> client(new VEANoInputClient(note.get()));
2056 base::Thread encoder_thread("EncoderThread");
2057 ASSERT_TRUE(encoder_thread.Start());
2058 2198
2059 encoder_thread.task_runner()->PostTask( 2199 INSTANTIATE_TEST_CASE_P(CacheLineUnalignedInputTest,
2060 FROM_HERE, base::Bind(&VEANoInputClient::CreateEncoder, 2200 VideoEncodeAcceleratorSimpleTest,
2061 base::Unretained(client.get()))); 2201 ::testing::Values(1));
2062
2063 // Encoder must pass through states in this order.
2064 enum ClientState state_transitions[] = {CS_INITIALIZED, CS_ENCODING,
2065 CS_FINISHED};
2066
2067 for (const auto& state : state_transitions) {
2068 ASSERT_EQ(state, note->Wait());
2069 }
2070
2071 encoder_thread.task_runner()->PostTask(
2072 FROM_HERE, base::Bind(&VEANoInputClient::DestroyEncoder,
2073 base::Unretained(client.get())));
2074
2075 // This ensures all tasks have finished.
2076 encoder_thread.Stop();
2077 }
2078 2202
2079 #elif defined(OS_MACOSX) || defined(OS_WIN) 2203 #elif defined(OS_MACOSX) || defined(OS_WIN)
2080 INSTANTIATE_TEST_CASE_P( 2204 INSTANTIATE_TEST_CASE_P(
2081 SimpleEncode, 2205 SimpleEncode,
2082 VideoEncodeAcceleratorTest, 2206 VideoEncodeAcceleratorTest,
2083 ::testing::Values( 2207 ::testing::Values(
2084 std::make_tuple(1, true, 0, false, false, false, false, false, false), 2208 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))); 2209 std::make_tuple(1, true, 0, false, false, false, false, true, false)));
2086 2210
2087 INSTANTIATE_TEST_CASE_P( 2211 INSTANTIATE_TEST_CASE_P(
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
2199 2323
2200 media::g_env = 2324 media::g_env =
2201 reinterpret_cast<media::VideoEncodeAcceleratorTestEnvironment*>( 2325 reinterpret_cast<media::VideoEncodeAcceleratorTestEnvironment*>(
2202 testing::AddGlobalTestEnvironment( 2326 testing::AddGlobalTestEnvironment(
2203 new media::VideoEncodeAcceleratorTestEnvironment( 2327 new media::VideoEncodeAcceleratorTestEnvironment(
2204 std::move(test_stream_data), log_path, run_at_fps, 2328 std::move(test_stream_data), log_path, run_at_fps,
2205 needs_encode_latency, verify_all_output))); 2329 needs_encode_latency, verify_all_output)));
2206 2330
2207 return RUN_ALL_TESTS(); 2331 return RUN_ALL_TESTS();
2208 } 2332 }
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