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

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

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

Powered by Google App Engine
This is Rietveld 408576698