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 "base/at_exit.h" | 5 #include "base/at_exit.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/memory_mapped_file.h" | 9 #include "base/files/memory_mapped_file.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | 28 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
| 29 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" | 29 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" |
| 30 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) | 30 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) |
| 31 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" | 31 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" |
| 32 #else | 32 #else |
| 33 #error The VideoEncodeAcceleratorUnittest is not supported on this platform. | 33 #error The VideoEncodeAcceleratorUnittest is not supported on this platform. |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #define ALIGN_64_BYTES(x) (((x) + 63) & ~63) | |
| 37 | |
| 36 using media::VideoEncodeAccelerator; | 38 using media::VideoEncodeAccelerator; |
| 37 | 39 |
| 38 namespace content { | 40 namespace content { |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 41 const media::VideoFrame::Format kInputFormat = media::VideoFrame::I420; | 43 const media::VideoFrame::Format kInputFormat = media::VideoFrame::I420; |
| 42 | 44 |
| 43 // Arbitrarily chosen to add some depth to the pipeline. | 45 // Arbitrarily chosen to add some depth to the pipeline. |
| 44 const unsigned int kNumOutputBuffers = 4; | 46 const unsigned int kNumOutputBuffers = 4; |
| 45 const unsigned int kNumExtraInputFrames = 4; | 47 const unsigned int kNumExtraInputFrames = 4; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 // stream. | 91 // stream. |
| 90 // - |requested_subsequent_framerate| framerate to switch to in the middle | 92 // - |requested_subsequent_framerate| framerate to switch to in the middle |
| 91 // of the stream. | 93 // of the stream. |
| 92 // Bitrate is only forced for tests that test bitrate. | 94 // Bitrate is only forced for tests that test bitrate. |
| 93 const char* g_default_in_filename = "bear_320x192_40frames.yuv"; | 95 const char* g_default_in_filename = "bear_320x192_40frames.yuv"; |
| 94 const char* g_default_in_parameters = ":320:192:1:out.h264:200000"; | 96 const char* g_default_in_parameters = ":320:192:1:out.h264:200000"; |
| 95 base::FilePath::StringType* g_test_stream_data; | 97 base::FilePath::StringType* g_test_stream_data; |
| 96 | 98 |
| 97 struct TestStream { | 99 struct TestStream { |
| 98 TestStream() | 100 TestStream() |
| 99 : requested_bitrate(0), | 101 : default_requested_bitrate(0), |
| 100 requested_framerate(0), | 102 default_requested_framerate(0), |
| 101 requested_subsequent_bitrate(0), | 103 default_requested_subsequent_bitrate(0), |
| 102 requested_subsequent_framerate(0) {} | 104 default_requested_subsequent_framerate(0) {} |
| 103 ~TestStream() {} | 105 ~TestStream() {} |
| 104 | 106 |
| 105 gfx::Size size; | 107 gfx::Size size; |
| 108 | |
| 109 // Input file name and the file must be an I420 (YUV planar) raw stream. | |
| 110 std::string in_filename; | |
| 111 | |
| 112 // The memory mapped of |temp_file| | |
| 106 base::MemoryMappedFile input_file; | 113 base::MemoryMappedFile input_file; |
| 114 | |
| 115 // A temporary file used to prepare input buffers. | |
| 116 base::FilePath temp_file; | |
| 117 | |
| 118 std::string out_filename; | |
| 107 media::VideoCodecProfile requested_profile; | 119 media::VideoCodecProfile requested_profile; |
| 108 std::string out_filename; | 120 unsigned int default_requested_bitrate; |
| 109 unsigned int requested_bitrate; | 121 unsigned int default_requested_framerate; |
| 110 unsigned int requested_framerate; | 122 unsigned int default_requested_subsequent_bitrate; |
|
wuchengli
2014/09/04 08:24:40
I don't think the name needs to be changed. The na
henryhsu
2014/09/04 08:54:56
Done.
| |
| 111 unsigned int requested_subsequent_bitrate; | 123 unsigned int default_requested_subsequent_framerate; |
| 112 unsigned int requested_subsequent_framerate; | |
| 113 }; | 124 }; |
| 125 TestStream *g_test_streams; | |
| 126 size_t g_test_streams_size; | |
| 127 | |
| 128 static bool WriteFile(base::File *file, | |
| 129 const off_t offset, | |
| 130 const char* data, | |
| 131 size_t size) { | |
| 132 size_t written_bytes = 0; | |
| 133 while (written_bytes < size) { | |
| 134 int bytes = file->Write( | |
| 135 offset + written_bytes, data + written_bytes, size - written_bytes); | |
| 136 if (bytes <= 0) return false; | |
| 137 written_bytes += bytes; | |
| 138 } | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 // ARM performs CPU cache management with CPU cache line granularity. We thus | |
| 143 // need to ensure our buffers are CPU cache line-aligned (64 byte-aligned). | |
| 144 // Otherwise newer kernels will refuse to accept them, and on older kernels | |
| 145 // we'll be treating ourselves to random corruption. | |
| 146 // Since we are just mmapping and passing chunks of the input file, to ensure | |
| 147 // alignment, if the starting virtual addresses of YUV planes of the frames | |
| 148 // in it were not 64 byte-aligned, we'd have to prepare a memory with 64 | |
| 149 // byte-aligned starting address and make sure the addresses of YUV planes of | |
| 150 // each frame are 64 byte-aligned before sending to the encoder. | |
| 151 // Now we test resolutions different from coded size and prepare chunks before | |
| 152 // encoding to avoid performance impact. | |
| 153 // YUV data are copied to file directly. Use |visible_size| and |coded_size| to | |
| 154 // write YUV data from |in_filename| to |input_file|. Also calculate the byte | |
| 155 // size of an input frame and set it to |coded_buffer_size|. |temp_file| is used | |
| 156 // to prepare input buffers and will be deleted after test finished. | |
| 157 static void PrepareInputBuffers(const gfx::Size& visible_size, | |
| 158 const gfx::Size& coded_size, | |
| 159 const std::string in_filename, | |
| 160 base::MemoryMappedFile* input_file, | |
| 161 base::FilePath* temp_file, | |
| 162 size_t* coded_buffer_size) { | |
| 163 size_t input_num_planes = media::VideoFrame::NumPlanes(kInputFormat); | |
| 164 std::vector<size_t> padding_sizes(input_num_planes); | |
| 165 std::vector<size_t> coded_bpl(input_num_planes); | |
| 166 std::vector<size_t> visible_bpl(input_num_planes); | |
| 167 std::vector<size_t> visible_plane_rows(input_num_planes); | |
| 168 | |
| 169 // YUV plane starting address should be 64 bytes alignment. Calculate padding | |
| 170 // size for each plane, and frame allocation size for coded size. Also store | |
| 171 // bytes per line information of coded size and visible size. | |
| 172 *coded_buffer_size = 0; | |
| 173 for (off_t i = 0; i < input_num_planes; i++) { | |
| 174 size_t size = | |
| 175 media::VideoFrame::PlaneAllocationSize(kInputFormat, i, coded_size); | |
| 176 size_t padding_bytes = ALIGN_64_BYTES(size) - size; | |
| 177 *coded_buffer_size += ALIGN_64_BYTES(size); | |
| 178 | |
| 179 coded_bpl[i] = | |
| 180 media::VideoFrame::RowBytes(i, kInputFormat, coded_size.width()); | |
| 181 visible_bpl[i] = | |
| 182 media::VideoFrame::RowBytes(i, kInputFormat, visible_size.width()); | |
| 183 visible_plane_rows[i] = | |
| 184 media::VideoFrame::Rows(i, kInputFormat, visible_size.height()); | |
| 185 size_t padding_rows = | |
| 186 media::VideoFrame::Rows(i, kInputFormat, coded_size.height()) - | |
| 187 visible_plane_rows[i]; | |
| 188 padding_sizes[i] = padding_rows * coded_bpl[i] + padding_bytes; | |
| 189 } | |
| 190 | |
| 191 // Test case may have many encoders and memory should be prepared once. | |
| 192 if (input_file->IsValid()) | |
| 193 return; | |
| 194 | |
| 195 base::MemoryMappedFile src_file; | |
| 196 CHECK(base::CreateTemporaryFile(temp_file)); | |
| 197 CHECK(src_file.Initialize(base::FilePath(in_filename))); | |
| 198 | |
| 199 size_t visible_buffer_size = | |
| 200 media::VideoFrame::AllocationSize(kInputFormat, visible_size); | |
| 201 size_t num_frames = src_file.length() / visible_buffer_size; | |
| 202 uint32 flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | | |
| 203 base::File::FLAG_READ; | |
| 204 | |
| 205 // Create a temporary file with coded_size length. | |
| 206 base::File dest_file(*temp_file, flags); | |
| 207 dest_file.SetLength(*coded_buffer_size * num_frames); | |
| 208 | |
| 209 off_t src_offset = 0, dest_offset = 0; | |
| 210 while (src_offset < static_cast<off_t>(src_file.length())) { | |
| 211 for (off_t i = 0; i < input_num_planes; i++) { | |
| 212 #if defined(ARCH_CPU_ARMEL) | |
| 213 // Assert that each plane of frame starts at 64-byte boundary. | |
| 214 const uint8* ptr = input_file->data() + dest_offset; | |
| 215 ASSERT_EQ(reinterpret_cast<off_t>(ptr) & 63, 0) | |
| 216 << "Planes of frame should be mapped at a 64 byte boundary"; | |
| 217 #endif | |
| 218 for (off_t j = 0; j < visible_plane_rows[i]; j++) { | |
| 219 const char* src = | |
| 220 reinterpret_cast<const char*>(src_file.data() + src_offset); | |
| 221 CHECK(WriteFile(&dest_file, dest_offset, src, visible_bpl[i])); | |
| 222 src_offset += visible_bpl[i]; | |
| 223 dest_offset += coded_bpl[i]; | |
| 224 } | |
| 225 dest_offset += padding_sizes[i]; | |
| 226 } | |
| 227 } | |
| 228 CHECK(input_file->Initialize(dest_file.Pass())); | |
| 229 } | |
| 114 | 230 |
| 115 // Parse |data| into its constituent parts, set the various output fields | 231 // Parse |data| into its constituent parts, set the various output fields |
| 116 // accordingly, read in video stream, and store them to |test_streams|. | 232 // accordingly, read in video stream, and store them to |test_streams|. Also |
| 233 // store the size of |test_streams| to |test_streams_size|. | |
| 117 static void ParseAndReadTestStreamData(const base::FilePath::StringType& data, | 234 static void ParseAndReadTestStreamData(const base::FilePath::StringType& data, |
| 118 ScopedVector<TestStream>* test_streams) { | 235 TestStream** test_streams, |
| 236 size_t* test_streams_size) { | |
| 119 // Split the string to individual test stream data. | 237 // Split the string to individual test stream data. |
| 120 std::vector<base::FilePath::StringType> test_streams_data; | 238 std::vector<base::FilePath::StringType> test_streams_data; |
| 121 base::SplitString(data, ';', &test_streams_data); | 239 base::SplitString(data, ';', &test_streams_data); |
| 122 CHECK_GE(test_streams_data.size(), 1U) << data; | 240 CHECK_GE(test_streams_data.size(), 1U) << data; |
| 123 | 241 |
| 242 *test_streams_size = test_streams_data.size(); | |
| 243 *test_streams = new TestStream[*test_streams_size]; | |
| 124 // Parse each test stream data and read the input file. | 244 // Parse each test stream data and read the input file. |
| 125 for (size_t index = 0; index < test_streams_data.size(); ++index) { | 245 for (size_t index = 0; index < *test_streams_size; ++index) { |
| 126 std::vector<base::FilePath::StringType> fields; | 246 std::vector<base::FilePath::StringType> fields; |
| 127 base::SplitString(test_streams_data[index], ':', &fields); | 247 base::SplitString(test_streams_data[index], ':', &fields); |
| 128 CHECK_GE(fields.size(), 4U) << data; | 248 CHECK_GE(fields.size(), 4U) << data; |
| 129 CHECK_LE(fields.size(), 9U) << data; | 249 CHECK_LE(fields.size(), 9U) << data; |
| 130 TestStream* test_stream = new TestStream(); | 250 TestStream* test_stream = &*test_streams[index]; |
| 131 | 251 |
| 132 base::FilePath::StringType filename = fields[0]; | 252 test_stream->in_filename = fields[0]; |
| 133 int width, height; | 253 int width, height; |
| 134 CHECK(base::StringToInt(fields[1], &width)); | 254 CHECK(base::StringToInt(fields[1], &width)); |
| 135 CHECK(base::StringToInt(fields[2], &height)); | 255 CHECK(base::StringToInt(fields[2], &height)); |
| 136 test_stream->size = gfx::Size(width, height); | 256 test_stream->size = gfx::Size(width, height); |
| 137 CHECK(!test_stream->size.IsEmpty()); | 257 CHECK(!test_stream->size.IsEmpty()); |
| 138 int profile; | 258 int profile; |
| 139 CHECK(base::StringToInt(fields[3], &profile)); | 259 CHECK(base::StringToInt(fields[3], &profile)); |
| 140 CHECK_GT(profile, media::VIDEO_CODEC_PROFILE_UNKNOWN); | 260 CHECK_GT(profile, media::VIDEO_CODEC_PROFILE_UNKNOWN); |
| 141 CHECK_LE(profile, media::VIDEO_CODEC_PROFILE_MAX); | 261 CHECK_LE(profile, media::VIDEO_CODEC_PROFILE_MAX); |
| 142 test_stream->requested_profile = | 262 test_stream->requested_profile = |
| 143 static_cast<media::VideoCodecProfile>(profile); | 263 static_cast<media::VideoCodecProfile>(profile); |
| 144 | 264 |
| 145 if (fields.size() >= 5 && !fields[4].empty()) | 265 if (fields.size() >= 5 && !fields[4].empty()) |
| 146 test_stream->out_filename = fields[4]; | 266 test_stream->out_filename = fields[4]; |
| 147 | 267 |
| 148 if (fields.size() >= 6 && !fields[5].empty()) | 268 if (fields.size() >= 6 && !fields[5].empty()) |
| 149 CHECK(base::StringToUint(fields[5], &test_stream->requested_bitrate)); | 269 CHECK(base::StringToUint( |
| 270 fields[5], &test_stream->default_requested_bitrate)); | |
| 150 | 271 |
| 151 if (fields.size() >= 7 && !fields[6].empty()) | 272 if (fields.size() >= 7 && !fields[6].empty()) |
| 152 CHECK(base::StringToUint(fields[6], &test_stream->requested_framerate)); | 273 CHECK(base::StringToUint( |
| 274 fields[6], &test_stream->default_requested_framerate)); | |
| 153 | 275 |
| 154 if (fields.size() >= 8 && !fields[7].empty()) { | 276 if (fields.size() >= 8 && !fields[7].empty()) { |
| 155 CHECK(base::StringToUint(fields[7], | 277 CHECK(base::StringToUint( |
| 156 &test_stream->requested_subsequent_bitrate)); | 278 fields[7], &test_stream->default_requested_subsequent_bitrate)); |
| 157 } | 279 } |
| 158 | 280 |
| 159 if (fields.size() >= 9 && !fields[8].empty()) { | 281 if (fields.size() >= 9 && !fields[8].empty()) { |
| 160 CHECK(base::StringToUint(fields[8], | 282 CHECK(base::StringToUint( |
| 161 &test_stream->requested_subsequent_framerate)); | 283 fields[8], &test_stream->default_requested_subsequent_framerate)); |
| 162 } | 284 } |
| 163 | |
| 164 CHECK(test_stream->input_file.Initialize(base::FilePath(filename))); | |
| 165 test_streams->push_back(test_stream); | |
| 166 } | 285 } |
| 167 } | 286 } |
| 168 | 287 |
| 169 // Set default parameters of |test_streams| and update the parameters according | |
| 170 // to |mid_stream_bitrate_switch| and |mid_stream_framerate_switch|. | |
| 171 static void UpdateTestStreamData(bool mid_stream_bitrate_switch, | |
| 172 bool mid_stream_framerate_switch, | |
| 173 ScopedVector<TestStream>* test_streams) { | |
| 174 for (size_t i = 0; i < test_streams->size(); i++) { | |
| 175 TestStream* test_stream = (*test_streams)[i]; | |
| 176 // Use defaults for bitrate/framerate if they are not provided. | |
| 177 if (test_stream->requested_bitrate == 0) | |
| 178 test_stream->requested_bitrate = kDefaultBitrate; | |
| 179 | |
| 180 if (test_stream->requested_framerate == 0) | |
| 181 test_stream->requested_framerate = kDefaultFramerate; | |
| 182 | |
| 183 // If bitrate/framerate switch is requested, use the subsequent values if | |
| 184 // provided, or, if not, calculate them from their initial values using | |
| 185 // the default ratios. | |
| 186 // Otherwise, if a switch is not requested, keep the initial values. | |
| 187 if (mid_stream_bitrate_switch) { | |
| 188 if (test_stream->requested_subsequent_bitrate == 0) { | |
| 189 test_stream->requested_subsequent_bitrate = | |
| 190 test_stream->requested_bitrate * kDefaultSubsequentBitrateRatio; | |
| 191 } | |
| 192 } else { | |
| 193 test_stream->requested_subsequent_bitrate = | |
| 194 test_stream->requested_bitrate; | |
| 195 } | |
| 196 if (test_stream->requested_subsequent_bitrate == 0) | |
| 197 test_stream->requested_subsequent_bitrate = 1; | |
| 198 | |
| 199 if (mid_stream_framerate_switch) { | |
| 200 if (test_stream->requested_subsequent_framerate == 0) { | |
| 201 test_stream->requested_subsequent_framerate = | |
| 202 test_stream->requested_framerate * kDefaultSubsequentFramerateRatio; | |
| 203 } | |
| 204 } else { | |
| 205 test_stream->requested_subsequent_framerate = | |
| 206 test_stream->requested_framerate; | |
| 207 } | |
| 208 if (test_stream->requested_subsequent_framerate == 0) | |
| 209 test_stream->requested_subsequent_framerate = 1; | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 enum ClientState { | 288 enum ClientState { |
| 214 CS_CREATED, | 289 CS_CREATED, |
| 215 CS_ENCODER_SET, | 290 CS_ENCODER_SET, |
| 216 CS_INITIALIZED, | 291 CS_INITIALIZED, |
| 217 CS_ENCODING, | 292 CS_ENCODING, |
| 218 CS_FINISHED, | 293 CS_FINISHED, |
| 219 CS_ERROR, | 294 CS_ERROR, |
| 220 }; | 295 }; |
| 221 | 296 |
| 222 // Performs basic, codec-specific sanity checks on the stream buffers passed | 297 // Performs basic, codec-specific sanity checks on the stream buffers passed |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 validator.reset(new VP8Validator(frame_cb)); | 436 validator.reset(new VP8Validator(frame_cb)); |
| 362 } else { | 437 } else { |
| 363 LOG(FATAL) << "Unsupported profile: " << profile; | 438 LOG(FATAL) << "Unsupported profile: " << profile; |
| 364 } | 439 } |
| 365 | 440 |
| 366 return validator.Pass(); | 441 return validator.Pass(); |
| 367 } | 442 } |
| 368 | 443 |
| 369 class VEAClient : public VideoEncodeAccelerator::Client { | 444 class VEAClient : public VideoEncodeAccelerator::Client { |
| 370 public: | 445 public: |
| 371 VEAClient(const TestStream& test_stream, | 446 VEAClient(TestStream& test_stream, |
| 372 ClientStateNotification<ClientState>* note, | 447 ClientStateNotification<ClientState>* note, |
| 373 bool save_to_file, | 448 bool save_to_file, |
| 374 unsigned int keyframe_period, | 449 unsigned int keyframe_period, |
| 375 bool force_bitrate, | 450 bool force_bitrate, |
| 376 bool test_perf); | 451 bool test_perf, |
| 452 bool mid_stream_bitrate_switch, | |
| 453 bool mid_stream_framerate_switch); | |
| 377 virtual ~VEAClient(); | 454 virtual ~VEAClient(); |
| 378 void CreateEncoder(); | 455 void CreateEncoder(); |
| 379 void DestroyEncoder(); | 456 void DestroyEncoder(); |
| 380 | 457 |
| 381 // Return the number of encoded frames per second. | 458 // Return the number of encoded frames per second. |
| 382 double frames_per_second(); | 459 double frames_per_second(); |
| 383 | 460 |
| 384 // VideoDecodeAccelerator::Client implementation. | 461 // VideoDecodeAccelerator::Client implementation. |
| 385 virtual void RequireBitstreamBuffers(unsigned int input_count, | 462 virtual void RequireBitstreamBuffers(unsigned int input_count, |
| 386 const gfx::Size& input_coded_size, | 463 const gfx::Size& input_coded_size, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 420 void VerifyStreamProperties(); | 497 void VerifyStreamProperties(); |
| 421 | 498 |
| 422 // Test codec performance, failing the test if we are currently running | 499 // Test codec performance, failing the test if we are currently running |
| 423 // the performance test. | 500 // the performance test. |
| 424 void VerifyPerf(); | 501 void VerifyPerf(); |
| 425 | 502 |
| 426 // Prepare and return a frame wrapping the data at |position| bytes in | 503 // Prepare and return a frame wrapping the data at |position| bytes in |
| 427 // the input stream, ready to be sent to encoder. | 504 // the input stream, ready to be sent to encoder. |
| 428 scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position); | 505 scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position); |
| 429 | 506 |
| 507 // Update the parameters according to |mid_stream_bitrate_switch| and | |
| 508 // |mid_stream_framerate_switch|. | |
| 509 void UpdateTestStreamData(bool mid_stream_bitrate_switch, | |
| 510 bool mid_stream_framerate_switch); | |
| 511 | |
| 430 ClientState state_; | 512 ClientState state_; |
| 431 scoped_ptr<VideoEncodeAccelerator> encoder_; | 513 scoped_ptr<VideoEncodeAccelerator> encoder_; |
| 432 | 514 |
| 433 const TestStream& test_stream_; | 515 TestStream& test_stream_; |
| 434 // Used to notify another thread about the state. VEAClient does not own this. | 516 // Used to notify another thread about the state. VEAClient does not own this. |
| 435 ClientStateNotification<ClientState>* note_; | 517 ClientStateNotification<ClientState>* note_; |
| 436 | 518 |
| 437 // Ids assigned to VideoFrames (start at 1 for easy comparison with | 519 // Ids assigned to VideoFrames (start at 1 for easy comparison with |
| 438 // num_encoded_frames_). | 520 // num_encoded_frames_). |
| 439 std::set<int32> inputs_at_client_; | 521 std::set<int32> inputs_at_client_; |
| 440 int32 next_input_id_; | 522 int32 next_input_id_; |
| 441 | 523 |
| 442 // Ids for output BitstreamBuffers. | 524 // Ids for output BitstreamBuffers. |
| 443 typedef std::map<int32, base::SharedMemory*> IdToSHM; | 525 typedef std::map<int32, base::SharedMemory*> IdToSHM; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 scoped_ptr<StreamValidator> validator_; | 580 scoped_ptr<StreamValidator> validator_; |
| 499 | 581 |
| 500 // The time when the encoding started. | 582 // The time when the encoding started. |
| 501 base::TimeTicks encode_start_time_; | 583 base::TimeTicks encode_start_time_; |
| 502 | 584 |
| 503 // The time when the last encoded frame is ready. | 585 // The time when the last encoded frame is ready. |
| 504 base::TimeTicks last_frame_ready_time_; | 586 base::TimeTicks last_frame_ready_time_; |
| 505 | 587 |
| 506 // All methods of this class should be run on the same thread. | 588 // All methods of this class should be run on the same thread. |
| 507 base::ThreadChecker thread_checker_; | 589 base::ThreadChecker thread_checker_; |
| 590 | |
| 591 // Requested bitrate in bits per second. | |
| 592 unsigned int requested_bitrate_; | |
| 593 | |
| 594 // Requested initial framerate. | |
| 595 unsigned int requested_framerate_; | |
| 596 | |
| 597 // Bitrate to switch to in the middle of the stream. | |
| 598 unsigned int requested_subsequent_bitrate_; | |
| 599 | |
| 600 // Framerate to switch to in the middle of the stream. | |
| 601 unsigned int requested_subsequent_framerate_; | |
| 508 }; | 602 }; |
| 509 | 603 |
| 510 VEAClient::VEAClient(const TestStream& test_stream, | 604 VEAClient::VEAClient(TestStream& test_stream, |
| 511 ClientStateNotification<ClientState>* note, | 605 ClientStateNotification<ClientState>* note, |
| 512 bool save_to_file, | 606 bool save_to_file, |
| 513 unsigned int keyframe_period, | 607 unsigned int keyframe_period, |
| 514 bool force_bitrate, | 608 bool force_bitrate, |
| 515 bool test_perf) | 609 bool test_perf, |
| 610 bool mid_stream_bitrate_switch, | |
| 611 bool mid_stream_framerate_switch) | |
| 516 : state_(CS_CREATED), | 612 : state_(CS_CREATED), |
| 517 test_stream_(test_stream), | 613 test_stream_(test_stream), |
| 518 note_(note), | 614 note_(note), |
| 519 next_input_id_(1), | 615 next_input_id_(1), |
| 520 next_output_buffer_id_(0), | 616 next_output_buffer_id_(0), |
| 521 pos_in_input_stream_(0), | 617 pos_in_input_stream_(0), |
| 522 input_buffer_size_(0), | 618 input_buffer_size_(0), |
| 523 num_required_input_buffers_(0), | 619 num_required_input_buffers_(0), |
| 524 output_buffer_size_(0), | 620 output_buffer_size_(0), |
| 525 num_frames_in_stream_(0), | 621 num_frames_in_stream_(0), |
| 526 num_frames_to_encode_(0), | 622 num_frames_to_encode_(0), |
| 527 num_encoded_frames_(0), | 623 num_encoded_frames_(0), |
| 528 num_frames_since_last_check_(0), | 624 num_frames_since_last_check_(0), |
| 529 seen_keyframe_in_this_buffer_(false), | 625 seen_keyframe_in_this_buffer_(false), |
| 530 save_to_file_(save_to_file), | 626 save_to_file_(save_to_file), |
| 531 keyframe_period_(keyframe_period), | 627 keyframe_period_(keyframe_period), |
| 532 keyframe_requested_at_(kMaxFrameNum), | 628 keyframe_requested_at_(kMaxFrameNum), |
| 533 force_bitrate_(force_bitrate), | 629 force_bitrate_(force_bitrate), |
| 534 current_requested_bitrate_(0), | 630 current_requested_bitrate_(0), |
| 535 current_framerate_(0), | 631 current_framerate_(0), |
| 536 encoded_stream_size_since_last_check_(0), | 632 encoded_stream_size_since_last_check_(0), |
| 537 test_perf_(test_perf) { | 633 test_perf_(test_perf), |
| 634 requested_bitrate_(0), | |
| 635 requested_framerate_(0), | |
| 636 requested_subsequent_bitrate_(0), | |
| 637 requested_subsequent_framerate_(0) { | |
| 538 if (keyframe_period_) | 638 if (keyframe_period_) |
| 539 CHECK_LT(kMaxKeyframeDelay, keyframe_period_); | 639 CHECK_LT(kMaxKeyframeDelay, keyframe_period_); |
| 540 | 640 |
| 541 validator_ = StreamValidator::Create( | 641 validator_ = StreamValidator::Create( |
| 542 test_stream_.requested_profile, | 642 test_stream_.requested_profile, |
| 543 base::Bind(&VEAClient::HandleEncodedFrame, base::Unretained(this))); | 643 base::Bind(&VEAClient::HandleEncodedFrame, base::Unretained(this))); |
| 544 | 644 |
| 545 CHECK(validator_.get()); | 645 CHECK(validator_.get()); |
| 546 | 646 |
| 547 if (save_to_file_) { | 647 if (save_to_file_) { |
| 548 CHECK(!test_stream_.out_filename.empty()); | 648 CHECK(!test_stream_.out_filename.empty()); |
| 549 base::FilePath out_filename(test_stream_.out_filename); | 649 base::FilePath out_filename(test_stream_.out_filename); |
| 550 // This creates or truncates out_filename. | 650 // This creates or truncates out_filename. |
| 551 // Without it, AppendToFile() will not work. | 651 // Without it, AppendToFile() will not work. |
| 552 EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0)); | 652 EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0)); |
| 553 } | 653 } |
| 554 | 654 |
| 555 input_buffer_size_ = | 655 // Initialize the test streams. |
|
wuchengli
2014/09/04 08:24:40
Initialize the parameters of the test streams.
henryhsu
2014/09/04 08:54:56
Done.
| |
| 556 media::VideoFrame::AllocationSize(kInputFormat, test_stream.size); | 656 UpdateTestStreamData(mid_stream_bitrate_switch, mid_stream_framerate_switch); |
| 557 CHECK_GT(input_buffer_size_, 0UL); | |
| 558 | |
| 559 // Calculate the number of frames in the input stream by dividing its length | |
| 560 // in bytes by frame size in bytes. | |
| 561 CHECK_EQ(test_stream_.input_file.length() % input_buffer_size_, 0U) | |
| 562 << "Stream byte size is not a product of calculated frame byte size"; | |
| 563 num_frames_in_stream_ = test_stream_.input_file.length() / input_buffer_size_; | |
| 564 CHECK_GT(num_frames_in_stream_, 0UL); | |
| 565 CHECK_LE(num_frames_in_stream_, kMaxFrameNum); | |
| 566 | |
| 567 // We may need to loop over the stream more than once if more frames than | |
| 568 // provided is required for bitrate tests. | |
| 569 if (force_bitrate_ && num_frames_in_stream_ < kMinFramesForBitrateTests) { | |
| 570 DVLOG(1) << "Stream too short for bitrate test (" << num_frames_in_stream_ | |
| 571 << " frames), will loop it to reach " << kMinFramesForBitrateTests | |
| 572 << " frames"; | |
| 573 num_frames_to_encode_ = kMinFramesForBitrateTests; | |
| 574 } else { | |
| 575 num_frames_to_encode_ = num_frames_in_stream_; | |
| 576 } | |
| 577 | 657 |
| 578 thread_checker_.DetachFromThread(); | 658 thread_checker_.DetachFromThread(); |
| 579 } | 659 } |
| 580 | 660 |
| 581 VEAClient::~VEAClient() { CHECK(!has_encoder()); } | 661 VEAClient::~VEAClient() { CHECK(!has_encoder()); } |
| 582 | 662 |
| 583 void VEAClient::CreateEncoder() { | 663 void VEAClient::CreateEncoder() { |
| 584 DCHECK(thread_checker_.CalledOnValidThread()); | 664 DCHECK(thread_checker_.CalledOnValidThread()); |
| 585 CHECK(!has_encoder()); | 665 CHECK(!has_encoder()); |
| 586 | 666 |
| 587 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | 667 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
| 588 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); | 668 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); |
| 589 encoder_.reset(new V4L2VideoEncodeAccelerator(device.Pass())); | 669 encoder_.reset(new V4L2VideoEncodeAccelerator(device.Pass())); |
| 590 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) | 670 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) |
| 591 encoder_.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay())); | 671 encoder_.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay())); |
| 592 #endif | 672 #endif |
| 593 | 673 |
| 594 SetState(CS_ENCODER_SET); | 674 SetState(CS_ENCODER_SET); |
| 595 | 675 |
| 596 DVLOG(1) << "Profile: " << test_stream_.requested_profile | 676 DVLOG(1) << "Profile: " << test_stream_.requested_profile |
| 597 << ", initial bitrate: " << test_stream_.requested_bitrate; | 677 << ", initial bitrate: " << requested_bitrate_; |
| 598 if (!encoder_->Initialize(kInputFormat, | 678 if (!encoder_->Initialize(kInputFormat, |
| 599 test_stream_.size, | 679 test_stream_.size, |
| 600 test_stream_.requested_profile, | 680 test_stream_.requested_profile, |
| 601 test_stream_.requested_bitrate, | 681 requested_bitrate_, |
| 602 this)) { | 682 this)) { |
| 603 DLOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed"; | 683 DLOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed"; |
| 604 SetState(CS_ERROR); | 684 SetState(CS_ERROR); |
| 605 return; | 685 return; |
| 606 } | 686 } |
| 607 | 687 |
| 608 SetStreamParameters(test_stream_.requested_bitrate, | 688 SetStreamParameters(requested_bitrate_, requested_framerate_); |
| 609 test_stream_.requested_framerate); | |
| 610 SetState(CS_INITIALIZED); | 689 SetState(CS_INITIALIZED); |
| 611 } | 690 } |
| 612 | 691 |
| 613 void VEAClient::DestroyEncoder() { | 692 void VEAClient::DestroyEncoder() { |
| 614 DCHECK(thread_checker_.CalledOnValidThread()); | 693 DCHECK(thread_checker_.CalledOnValidThread()); |
| 615 if (!has_encoder()) | 694 if (!has_encoder()) |
| 616 return; | 695 return; |
| 617 encoder_.reset(); | 696 encoder_.reset(); |
| 618 } | 697 } |
| 619 | 698 |
| 699 void VEAClient::UpdateTestStreamData(bool mid_stream_bitrate_switch, | |
| 700 bool mid_stream_framerate_switch) { | |
| 701 // Use defaults for bitrate/framerate if they are not provided. | |
| 702 if (test_stream_.default_requested_bitrate == 0) | |
| 703 requested_bitrate_ = kDefaultBitrate; | |
| 704 else | |
| 705 requested_bitrate_ = test_stream_.default_requested_bitrate; | |
| 706 | |
| 707 if (test_stream_.default_requested_framerate == 0) | |
| 708 requested_framerate_ = kDefaultFramerate; | |
| 709 else | |
| 710 requested_framerate_ = test_stream_.default_requested_framerate; | |
| 711 | |
| 712 // If bitrate/framerate switch is requested, use the subsequent values if | |
| 713 // provided, or, if not, calculate them from their initial values using | |
| 714 // the default ratios. | |
| 715 // Otherwise, if a switch is not requested, keep the initial values. | |
| 716 if (mid_stream_bitrate_switch) { | |
| 717 if (test_stream_.default_requested_subsequent_bitrate == 0) | |
| 718 requested_subsequent_bitrate_ = | |
| 719 requested_bitrate_ * kDefaultSubsequentBitrateRatio; | |
| 720 else | |
| 721 requested_subsequent_bitrate_ = | |
| 722 test_stream_.default_requested_subsequent_bitrate; | |
| 723 } else { | |
| 724 requested_subsequent_bitrate_ = requested_bitrate_; | |
| 725 } | |
| 726 if (requested_subsequent_bitrate_ == 0) | |
| 727 requested_subsequent_bitrate_ = 1; | |
| 728 | |
| 729 if (mid_stream_framerate_switch) { | |
| 730 if (test_stream_.default_requested_subsequent_framerate == 0) | |
| 731 requested_subsequent_framerate_ = | |
| 732 requested_framerate_ * kDefaultSubsequentFramerateRatio; | |
| 733 else | |
| 734 requested_subsequent_framerate_ = | |
| 735 test_stream_.default_requested_subsequent_framerate; | |
| 736 } else { | |
| 737 requested_subsequent_framerate_ = requested_framerate_; | |
| 738 } | |
| 739 if (requested_subsequent_framerate_ == 0) | |
| 740 requested_subsequent_framerate_ = 1; | |
| 741 } | |
| 742 | |
| 620 double VEAClient::frames_per_second() { | 743 double VEAClient::frames_per_second() { |
| 621 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_; | 744 base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_; |
| 622 return num_encoded_frames_ / duration.InSecondsF(); | 745 return num_encoded_frames_ / duration.InSecondsF(); |
| 623 } | 746 } |
| 624 | 747 |
| 625 void VEAClient::RequireBitstreamBuffers(unsigned int input_count, | 748 void VEAClient::RequireBitstreamBuffers(unsigned int input_count, |
| 626 const gfx::Size& input_coded_size, | 749 const gfx::Size& input_coded_size, |
| 627 size_t output_size) { | 750 size_t output_size) { |
| 628 DCHECK(thread_checker_.CalledOnValidThread()); | 751 DCHECK(thread_checker_.CalledOnValidThread()); |
| 629 ASSERT_EQ(state_, CS_INITIALIZED); | 752 ASSERT_EQ(state_, CS_INITIALIZED); |
| 630 SetState(CS_ENCODING); | 753 SetState(CS_ENCODING); |
| 631 | 754 |
| 632 // TODO(posciak): For now we only support input streams that meet encoder | 755 PrepareInputBuffers(test_stream_.size, |
| 633 // size requirements exactly (i.e. coded size == visible size), so that we | 756 input_coded_size, |
| 634 // can simply mmap the stream file and feed the encoder directly with chunks | 757 test_stream_.in_filename, |
| 635 // of that, instead of memcpying from mmapped file into a separate set of | 758 &test_stream_.input_file, |
| 636 // input buffers that would meet the coded size and alignment requirements. | 759 &test_stream_.temp_file, |
| 637 // If/when this is changed, the ARM-specific alignment check below should be | 760 &input_buffer_size_); |
| 638 // redone as well. | 761 CHECK_GT(input_buffer_size_, 0UL); |
| 762 | |
| 763 // Calculate the number of frames in the input stream by dividing its length | |
| 764 // in bytes by frame size in bytes. | |
| 765 CHECK_EQ(test_stream_.input_file.length() % input_buffer_size_, 0U) | |
| 766 << "Stream byte size is not a product of calculated frame byte size"; | |
| 767 num_frames_in_stream_ = test_stream_.input_file.length() / input_buffer_size_; | |
| 768 CHECK_GT(num_frames_in_stream_, 0UL); | |
| 769 CHECK_LE(num_frames_in_stream_, kMaxFrameNum); | |
| 770 | |
| 771 // We may need to loop over the stream more than once if more frames than | |
| 772 // provided is required for bitrate tests. | |
| 773 if (force_bitrate_ && num_frames_in_stream_ < kMinFramesForBitrateTests) { | |
| 774 DVLOG(1) << "Stream too short for bitrate test (" << num_frames_in_stream_ | |
| 775 << " frames), will loop it to reach " << kMinFramesForBitrateTests | |
| 776 << " frames"; | |
| 777 num_frames_to_encode_ = kMinFramesForBitrateTests; | |
| 778 } else { | |
| 779 num_frames_to_encode_ = num_frames_in_stream_; | |
| 780 } | |
| 781 | |
| 639 input_coded_size_ = input_coded_size; | 782 input_coded_size_ = input_coded_size; |
| 640 ASSERT_EQ(input_coded_size_, test_stream_.size); | |
| 641 #if defined(ARCH_CPU_ARMEL) | |
| 642 // ARM performs CPU cache management with CPU cache line granularity. We thus | |
| 643 // need to ensure our buffers are CPU cache line-aligned (64 byte-aligned). | |
| 644 // Otherwise newer kernels will refuse to accept them, and on older kernels | |
| 645 // we'll be treating ourselves to random corruption. | |
| 646 // Since we are just mmapping and passing chunks of the input file, to ensure | |
| 647 // alignment, if the starting virtual addresses of the frames in it were not | |
| 648 // 64 byte-aligned, we'd have to use a separate set of input buffers and copy | |
| 649 // the frames into them before sending to the encoder. It would have been an | |
| 650 // overkill here though, because, for now at least, we only test resolutions | |
| 651 // that result in proper alignment, and it would have also interfered with | |
| 652 // performance testing. So just assert that the frame size is a multiple of | |
| 653 // 64 bytes. This ensures all frames start at 64-byte boundary, because | |
| 654 // MemoryMappedFile should be mmapp()ed at virtual page start as well. | |
| 655 ASSERT_EQ(input_buffer_size_ & 63, 0u) | |
| 656 << "Frame size has to be a multiple of 64 bytes"; | |
| 657 ASSERT_EQ(reinterpret_cast<off_t>(test_stream_.input_file.data()) & 63, 0) | |
| 658 << "Mapped file should be mapped at a 64 byte boundary"; | |
| 659 #endif | |
| 660 | |
| 661 num_required_input_buffers_ = input_count; | 783 num_required_input_buffers_ = input_count; |
| 662 ASSERT_GT(num_required_input_buffers_, 0UL); | 784 ASSERT_GT(num_required_input_buffers_, 0UL); |
| 663 | 785 |
| 664 output_buffer_size_ = output_size; | 786 output_buffer_size_ = output_size; |
| 665 ASSERT_GT(output_buffer_size_, 0UL); | 787 ASSERT_GT(output_buffer_size_, 0UL); |
| 666 | 788 |
| 667 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) { | 789 for (unsigned int i = 0; i < kNumOutputBuffers; ++i) { |
| 668 base::SharedMemory* shm = new base::SharedMemory(); | 790 base::SharedMemory* shm = new base::SharedMemory(); |
| 669 CHECK(shm->CreateAndMapAnonymous(output_buffer_size_)); | 791 CHECK(shm->CreateAndMapAnonymous(output_buffer_size_)); |
| 670 output_shms_.push_back(shm); | 792 output_shms_.push_back(shm); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 736 void VEAClient::InputNoLongerNeededCallback(int32 input_id) { | 858 void VEAClient::InputNoLongerNeededCallback(int32 input_id) { |
| 737 std::set<int32>::iterator it = inputs_at_client_.find(input_id); | 859 std::set<int32>::iterator it = inputs_at_client_.find(input_id); |
| 738 ASSERT_NE(it, inputs_at_client_.end()); | 860 ASSERT_NE(it, inputs_at_client_.end()); |
| 739 inputs_at_client_.erase(it); | 861 inputs_at_client_.erase(it); |
| 740 FeedEncoderWithInputs(); | 862 FeedEncoderWithInputs(); |
| 741 } | 863 } |
| 742 | 864 |
| 743 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { | 865 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
| 744 CHECK_LE(position + input_buffer_size_, test_stream_.input_file.length()); | 866 CHECK_LE(position + input_buffer_size_, test_stream_.input_file.length()); |
| 745 | 867 |
| 746 uint8* frame_data = | 868 uint8* frame_data_y = |
| 747 const_cast<uint8*>(test_stream_.input_file.data() + position); | 869 const_cast<uint8*>(test_stream_.input_file.data() + position); |
| 870 uint8* frame_data_u = | |
| 871 frame_data_y + ALIGN_64_BYTES(media::VideoFrame::PlaneAllocationSize( | |
| 872 kInputFormat, 0, input_coded_size_)); | |
| 873 uint8* frame_data_v = | |
| 874 frame_data_u + ALIGN_64_BYTES(media::VideoFrame::PlaneAllocationSize( | |
| 875 kInputFormat, 1, input_coded_size_)); | |
| 748 | 876 |
| 749 CHECK_GT(current_framerate_, 0U); | 877 CHECK_GT(current_framerate_, 0U); |
| 750 scoped_refptr<media::VideoFrame> frame = | 878 scoped_refptr<media::VideoFrame> frame = |
| 751 media::VideoFrame::WrapExternalYuvData( | 879 media::VideoFrame::WrapExternalYuvData( |
| 752 kInputFormat, | 880 kInputFormat, |
| 753 input_coded_size_, | 881 input_coded_size_, |
| 754 gfx::Rect(test_stream_.size), | 882 gfx::Rect(test_stream_.size), |
| 755 test_stream_.size, | 883 test_stream_.size, |
| 756 input_coded_size_.width(), | 884 input_coded_size_.width(), |
| 757 input_coded_size_.width() / 2, | 885 input_coded_size_.width() / 2, |
| 758 input_coded_size_.width() / 2, | 886 input_coded_size_.width() / 2, |
| 759 frame_data, | 887 frame_data_y, |
| 760 frame_data + input_coded_size_.GetArea(), | 888 frame_data_u, |
| 761 frame_data + (input_coded_size_.GetArea() * 5 / 4), | 889 frame_data_v, |
| 762 base::TimeDelta().FromMilliseconds( | 890 base::TimeDelta().FromMilliseconds( |
| 763 next_input_id_ * base::Time::kMillisecondsPerSecond / | 891 next_input_id_ * base::Time::kMillisecondsPerSecond / |
| 764 current_framerate_), | 892 current_framerate_), |
| 765 media::BindToCurrentLoop( | 893 media::BindToCurrentLoop( |
| 766 base::Bind(&VEAClient::InputNoLongerNeededCallback, | 894 base::Bind(&VEAClient::InputNoLongerNeededCallback, |
| 767 base::Unretained(this), | 895 base::Unretained(this), |
| 768 next_input_id_))); | 896 next_input_id_))); |
| 769 | 897 |
| 770 CHECK(inputs_at_client_.insert(next_input_id_).second); | 898 CHECK(inputs_at_client_.insert(next_input_id_).second); |
| 771 ++next_input_id_; | 899 ++next_input_id_; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 847 // is asynchronous, i.e. not bound to any concrete frame, and because | 975 // is asynchronous, i.e. not bound to any concrete frame, and because |
| 848 // the pipeline can be deeper than one frame), at that frame, or after. | 976 // the pipeline can be deeper than one frame), at that frame, or after. |
| 849 // So the only constraints we put here is that we get a keyframe not | 977 // So the only constraints we put here is that we get a keyframe not |
| 850 // earlier than we requested one (in time), and not later than | 978 // earlier than we requested one (in time), and not later than |
| 851 // kMaxKeyframeDelay frames after the frame, for which we requested | 979 // kMaxKeyframeDelay frames after the frame, for which we requested |
| 852 // it, comes back encoded. | 980 // it, comes back encoded. |
| 853 EXPECT_LE(num_encoded_frames_, keyframe_requested_at_ + kMaxKeyframeDelay); | 981 EXPECT_LE(num_encoded_frames_, keyframe_requested_at_ + kMaxKeyframeDelay); |
| 854 | 982 |
| 855 if (num_encoded_frames_ == num_frames_to_encode_ / 2) { | 983 if (num_encoded_frames_ == num_frames_to_encode_ / 2) { |
| 856 VerifyStreamProperties(); | 984 VerifyStreamProperties(); |
| 857 if (test_stream_.requested_subsequent_bitrate != | 985 if (requested_subsequent_bitrate_ != current_requested_bitrate_ || |
| 858 current_requested_bitrate_ || | 986 requested_subsequent_framerate_ != current_framerate_) { |
| 859 test_stream_.requested_subsequent_framerate != current_framerate_) { | 987 SetStreamParameters(requested_subsequent_bitrate_, |
| 860 SetStreamParameters(test_stream_.requested_subsequent_bitrate, | 988 requested_subsequent_framerate_); |
| 861 test_stream_.requested_subsequent_framerate); | |
| 862 } | 989 } |
| 863 } else if (num_encoded_frames_ == num_frames_to_encode_) { | 990 } else if (num_encoded_frames_ == num_frames_to_encode_) { |
| 864 VerifyPerf(); | 991 VerifyPerf(); |
| 865 VerifyStreamProperties(); | 992 VerifyStreamProperties(); |
| 866 SetState(CS_FINISHED); | 993 SetState(CS_FINISHED); |
| 867 return false; | 994 return false; |
| 868 } | 995 } |
| 869 | 996 |
| 870 return true; | 997 return true; |
| 871 } | 998 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 890 num_frames_since_last_check_ = 0; | 1017 num_frames_since_last_check_ = 0; |
| 891 encoded_stream_size_since_last_check_ = 0; | 1018 encoded_stream_size_since_last_check_ = 0; |
| 892 | 1019 |
| 893 if (force_bitrate_) { | 1020 if (force_bitrate_) { |
| 894 EXPECT_NEAR(bitrate, | 1021 EXPECT_NEAR(bitrate, |
| 895 current_requested_bitrate_, | 1022 current_requested_bitrate_, |
| 896 kBitrateTolerance * current_requested_bitrate_); | 1023 kBitrateTolerance * current_requested_bitrate_); |
| 897 } | 1024 } |
| 898 } | 1025 } |
| 899 | 1026 |
| 1027 class VideoEncodeAcceleratorTestEnvironment : public ::testing::Environment { | |
| 1028 public: | |
| 1029 virtual void TearDown() { | |
| 1030 for (size_t i = 0; i < g_test_streams_size; i++) | |
| 1031 base::DeleteFile(g_test_streams[i].temp_file, false); | |
| 1032 delete[] g_test_streams; | |
|
wuchengli
2014/09/04 08:24:40
nit: set g_test_streams to NULL after deleting.
henryhsu
2014/09/04 08:54:56
Done.
| |
| 1033 } | |
| 1034 }; | |
| 1035 | |
| 900 // Test parameters: | 1036 // Test parameters: |
| 901 // - Number of concurrent encoders. | 1037 // - Number of concurrent encoders. |
| 902 // - If true, save output to file (provided an output filename was supplied). | 1038 // - If true, save output to file (provided an output filename was supplied). |
| 903 // - Force a keyframe every n frames. | 1039 // - Force a keyframe every n frames. |
| 904 // - Force bitrate; the actual required value is provided as a property | 1040 // - Force bitrate; the actual required value is provided as a property |
| 905 // of the input stream, because it depends on stream type/resolution/etc. | 1041 // of the input stream, because it depends on stream type/resolution/etc. |
| 906 // - If true, measure performance. | 1042 // - If true, measure performance. |
| 907 // - If true, switch bitrate mid-stream. | 1043 // - If true, switch bitrate mid-stream. |
| 908 // - If true, switch framerate mid-stream. | 1044 // - If true, switch framerate mid-stream. |
| 909 class VideoEncodeAcceleratorTest | 1045 class VideoEncodeAcceleratorTest |
| 910 : public ::testing::TestWithParam< | 1046 : public ::testing::TestWithParam< |
| 911 Tuple7<int, bool, int, bool, bool, bool, bool> > {}; | 1047 Tuple7<int, bool, int, bool, bool, bool, bool> > {}; |
| 912 | 1048 |
| 913 TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) { | 1049 TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) { |
| 914 const size_t num_concurrent_encoders = GetParam().a; | 1050 const size_t num_concurrent_encoders = GetParam().a; |
| 915 const bool save_to_file = GetParam().b; | 1051 const bool save_to_file = GetParam().b; |
| 916 const unsigned int keyframe_period = GetParam().c; | 1052 const unsigned int keyframe_period = GetParam().c; |
| 917 const bool force_bitrate = GetParam().d; | 1053 const bool force_bitrate = GetParam().d; |
| 918 const bool test_perf = GetParam().e; | 1054 const bool test_perf = GetParam().e; |
| 919 const bool mid_stream_bitrate_switch = GetParam().f; | 1055 const bool mid_stream_bitrate_switch = GetParam().f; |
| 920 const bool mid_stream_framerate_switch = GetParam().g; | 1056 const bool mid_stream_framerate_switch = GetParam().g; |
| 921 | 1057 |
| 922 // Initialize the test streams. | |
| 923 ScopedVector<TestStream> test_streams; | |
| 924 ParseAndReadTestStreamData(*g_test_stream_data, &test_streams); | |
| 925 UpdateTestStreamData( | |
| 926 mid_stream_bitrate_switch, mid_stream_framerate_switch, &test_streams); | |
| 927 | |
| 928 ScopedVector<ClientStateNotification<ClientState> > notes; | 1058 ScopedVector<ClientStateNotification<ClientState> > notes; |
| 929 ScopedVector<VEAClient> clients; | 1059 ScopedVector<VEAClient> clients; |
| 930 base::Thread encoder_thread("EncoderThread"); | 1060 base::Thread encoder_thread("EncoderThread"); |
| 931 ASSERT_TRUE(encoder_thread.Start()); | 1061 ASSERT_TRUE(encoder_thread.Start()); |
| 932 | 1062 |
| 933 // Create all encoders. | 1063 // Create all encoders. |
| 934 for (size_t i = 0; i < num_concurrent_encoders; i++) { | 1064 for (size_t i = 0; i < num_concurrent_encoders; i++) { |
| 935 size_t test_stream_index = i % test_streams.size(); | 1065 size_t test_stream_index = i % g_test_streams_size; |
| 936 // Disregard save_to_file if we didn't get an output filename. | 1066 // Disregard save_to_file if we didn't get an output filename. |
| 937 bool encoder_save_to_file = | 1067 bool encoder_save_to_file = |
| 938 (save_to_file && | 1068 (save_to_file && |
| 939 !test_streams[test_stream_index]->out_filename.empty()); | 1069 !g_test_streams[test_stream_index].out_filename.empty()); |
| 940 | 1070 |
| 941 notes.push_back(new ClientStateNotification<ClientState>()); | 1071 notes.push_back(new ClientStateNotification<ClientState>()); |
| 942 clients.push_back(new VEAClient(*test_streams[test_stream_index], | 1072 clients.push_back(new VEAClient(g_test_streams[test_stream_index], |
| 943 notes.back(), | 1073 notes.back(), |
| 944 encoder_save_to_file, | 1074 encoder_save_to_file, |
| 945 keyframe_period, | 1075 keyframe_period, |
| 946 force_bitrate, | 1076 force_bitrate, |
| 947 test_perf)); | 1077 test_perf, |
| 1078 mid_stream_bitrate_switch, | |
| 1079 mid_stream_framerate_switch)); | |
| 948 | 1080 |
| 949 encoder_thread.message_loop()->PostTask( | 1081 encoder_thread.message_loop()->PostTask( |
| 950 FROM_HERE, | 1082 FROM_HERE, |
| 951 base::Bind(&VEAClient::CreateEncoder, | 1083 base::Bind(&VEAClient::CreateEncoder, |
| 952 base::Unretained(clients.back()))); | 1084 base::Unretained(clients.back()))); |
| 953 } | 1085 } |
| 954 | 1086 |
| 955 // All encoders must pass through states in this order. | 1087 // All encoders must pass through states in this order. |
| 956 enum ClientState state_transitions[] = {CS_ENCODER_SET, CS_INITIALIZED, | 1088 enum ClientState state_transitions[] = {CS_ENCODER_SET, CS_INITIALIZED, |
| 957 CS_ENCODING, CS_FINISHED}; | 1089 CS_ENCODING, CS_FINISHED}; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1021 // TODO(posciak): more tests: | 1153 // TODO(posciak): more tests: |
| 1022 // - async FeedEncoderWithOutput | 1154 // - async FeedEncoderWithOutput |
| 1023 // - out-of-order return of outputs to encoder | 1155 // - out-of-order return of outputs to encoder |
| 1024 // - multiple encoders + decoders | 1156 // - multiple encoders + decoders |
| 1025 // - mid-stream encoder_->Destroy() | 1157 // - mid-stream encoder_->Destroy() |
| 1026 | 1158 |
| 1027 } // namespace | 1159 } // namespace |
| 1028 } // namespace content | 1160 } // namespace content |
| 1029 | 1161 |
| 1030 int main(int argc, char** argv) { | 1162 int main(int argc, char** argv) { |
| 1163 testing::AddGlobalTestEnvironment( | |
| 1164 new content::VideoEncodeAcceleratorTestEnvironment); | |
| 1031 testing::InitGoogleTest(&argc, argv); // Removes gtest-specific args. | 1165 testing::InitGoogleTest(&argc, argv); // Removes gtest-specific args. |
| 1032 base::CommandLine::Init(argc, argv); | 1166 base::CommandLine::Init(argc, argv); |
| 1033 | 1167 |
| 1034 base::ShadowingAtExitManager at_exit_manager; | 1168 base::ShadowingAtExitManager at_exit_manager; |
| 1035 scoped_ptr<base::FilePath::StringType> test_stream_data( | 1169 scoped_ptr<base::FilePath::StringType> test_stream_data( |
| 1036 new base::FilePath::StringType( | 1170 new base::FilePath::StringType( |
| 1037 media::GetTestDataFilePath(content::g_default_in_filename).value() + | 1171 media::GetTestDataFilePath(content::g_default_in_filename).value() + |
| 1038 content::g_default_in_parameters)); | 1172 content::g_default_in_parameters)); |
| 1039 content::g_test_stream_data = test_stream_data.get(); | 1173 content::g_test_stream_data = test_stream_data.get(); |
| 1040 | 1174 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1051 it != switches.end(); | 1185 it != switches.end(); |
| 1052 ++it) { | 1186 ++it) { |
| 1053 if (it->first == "test_stream_data") { | 1187 if (it->first == "test_stream_data") { |
| 1054 test_stream_data->assign(it->second.c_str()); | 1188 test_stream_data->assign(it->second.c_str()); |
| 1055 continue; | 1189 continue; |
| 1056 } | 1190 } |
| 1057 if (it->first == "v" || it->first == "vmodule") | 1191 if (it->first == "v" || it->first == "vmodule") |
| 1058 continue; | 1192 continue; |
| 1059 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; | 1193 LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second; |
| 1060 } | 1194 } |
| 1061 | 1195 content::ParseAndReadTestStreamData(*content::g_test_stream_data, |
| 1196 &content::g_test_streams, | |
| 1197 &content::g_test_streams_size); | |
| 1062 return RUN_ALL_TESTS(); | 1198 return RUN_ALL_TESTS(); |
| 1063 } | 1199 } |
| OLD | NEW |