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