Index: content/common/gpu/media/video_encode_accelerator_unittest.cc |
diff --git a/content/common/gpu/media/video_encode_accelerator_unittest.cc b/content/common/gpu/media/video_encode_accelerator_unittest.cc |
index 9d1e3b69dc7c9edccf56b2fab6021bc0fb20f23b..759ccdf9ceb552c7baec576ee0b8328bd614b702 100644 |
--- a/content/common/gpu/media/video_encode_accelerator_unittest.cc |
+++ b/content/common/gpu/media/video_encode_accelerator_unittest.cc |
@@ -33,6 +33,8 @@ |
#error The VideoEncodeAcceleratorUnittest is not supported on this platform. |
#endif |
+#define ALIGN_64_BYTES(x) (((x) + 63) & ~63) |
+ |
using media::VideoEncodeAccelerator; |
namespace content { |
@@ -103,8 +105,10 @@ struct TestStream { |
~TestStream() {} |
gfx::Size size; |
+ std::string in_filename; |
base::MemoryMappedFile input_file; |
media::VideoCodecProfile requested_profile; |
+ base::FilePath temp_file; |
std::string out_filename; |
unsigned int requested_bitrate; |
unsigned int requested_framerate; |
@@ -112,6 +116,84 @@ struct TestStream { |
unsigned int requested_subsequent_framerate; |
}; |
+static bool WriteFile(base::File* file, |
+ const char* data, |
+ size_t size, |
+ off_t *offset) { |
+ size_t write_bytes = 0; |
+ while (write_bytes < size) { |
+ int bytes = file->Write(*offset, data + write_bytes, size - write_bytes); |
+ if (!bytes) return false; |
+ write_bytes += bytes; |
+ *offset += bytes; |
+ } |
+ return true; |
+} |
+ |
wuchengli
2014/08/26 09:53:24
Add comment to explain what this function does.
henryhsu
2014/08/27 02:59:06
Done.
|
+static bool PrepareAlignedTempFile(TestStream* test_stream, |
wuchengli
2014/08/26 09:53:24
Output parameter should be after input.
henryhsu
2014/08/27 02:59:06
Done.
|
+ const gfx::Size& coded_size, |
+ size_t* input_buffer_size) { |
+ off_t input_planes_count = media::VideoFrame::NumPlanes(kInputFormat); |
wuchengli
2014/08/26 09:53:24
s/input_planes_count/input_num_planes/ to be consi
henryhsu
2014/08/27 02:59:06
Done.
|
+ size_t *padding_size = new size_t[input_planes_count]; |
wuchengli
2014/08/26 09:53:24
s/size_t */size_t* /
henryhsu
2014/08/27 02:59:06
Done.
|
+ |
wuchengli
2014/08/26 09:53:24
Add comments for important parts of this function.
henryhsu
2014/08/27 02:59:05
Done.
|
+ *input_buffer_size = 0; |
+ for (off_t i = 0; i < input_planes_count; i++) { |
+ size_t size = media::VideoFrame::PlaneAllocationSize( |
+ kInputFormat, i, coded_size); |
+ padding_size[i] = ALIGN_64_BYTES(size) - size; |
+ *input_buffer_size += ALIGN_64_BYTES(size); |
+ } |
+ |
+ if (!test_stream->input_file.IsValid()) { |
+ base::MemoryMappedFile input_file; |
+ CHECK(base::CreateTemporaryFile(&test_stream->temp_file)); |
+ CHECK(input_file.Initialize(base::FilePath(test_stream->in_filename))); |
+ uint32 flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; |
+ base::File file(base::FilePath(test_stream->temp_file), flags); |
+ |
+ off_t position = 0, offset = 0; |
+ char *dummy_buffer = new char[coded_size.width()]; |
+ while (position < static_cast<off_t>(input_file.length())) { |
+ for (off_t i = 0; i < input_planes_count; i++) { |
+ size_t coded_bpl = |
+ media::VideoFrame::RowBytes(i, kInputFormat, coded_size.width()); |
+ size_t visible_bpl = |
+ media::VideoFrame::RowBytes(i, kInputFormat, |
+ test_stream->size.width()); |
+ size_t padding_bpl = coded_bpl - visible_bpl; |
+ off_t rows = media::VideoFrame::Rows(i, kInputFormat, |
+ test_stream->size.height()); |
+ for (off_t j = 0; j < rows; j++) { |
+ char *buffer = reinterpret_cast<char*>( |
+ const_cast<uint8*>(input_file.data() + position)); |
+ CHECK(WriteFile(&file, buffer, visible_bpl, &offset)); |
+ CHECK(WriteFile(&file, dummy_buffer, padding_bpl, &offset)); |
+ position += visible_bpl; |
+ } |
+ off_t padding_rows = |
+ media::VideoFrame::Rows(i, kInputFormat, coded_size.height()) - |
+ rows; |
+ for (off_t j = 0; j < padding_rows; j++) { |
+ CHECK(WriteFile(&file, dummy_buffer, coded_bpl, &offset)); |
+ } |
wuchengli
2014/08/26 09:53:24
Remove parenthesis to be consistent with the next
henryhsu
2014/08/27 02:59:05
Done.
|
+ if (padding_size[i]) |
+ CHECK(WriteFile(&file, dummy_buffer, padding_size[i], &offset)); |
+ } |
+ } |
+ file.Close(); |
+ CHECK(test_stream->input_file.Initialize(test_stream->temp_file)); |
+ delete[] dummy_buffer; |
+ } |
+ delete[] padding_size; |
+ return true; |
+} |
+ |
+static void RemoveTempFile(ScopedVector<TestStream>* test_streams) { |
wuchengli
2014/08/26 09:53:25
s/RemoveTempFile/RemoveTempFiles/.
If a function
henryhsu
2014/08/27 02:59:05
Done.
|
+ for (size_t i = 0; i < test_streams->size(); i++) { |
+ base::DeleteFile((*test_streams)[i]->temp_file, false); |
+ } |
+} |
+ |
// Parse |data| into its constituent parts, set the various output fields |
// accordingly, read in video stream, and store them to |test_streams|. |
static void ParseAndReadTestStreamData(const base::FilePath::StringType& data, |
@@ -129,7 +211,7 @@ static void ParseAndReadTestStreamData(const base::FilePath::StringType& data, |
CHECK_LE(fields.size(), 9U) << data; |
TestStream* test_stream = new TestStream(); |
- base::FilePath::StringType filename = fields[0]; |
+ test_stream->in_filename = fields[0]; |
int width, height; |
CHECK(base::StringToInt(fields[1], &width)); |
CHECK(base::StringToInt(fields[2], &height)); |
@@ -161,7 +243,6 @@ static void ParseAndReadTestStreamData(const base::FilePath::StringType& data, |
&test_stream->requested_subsequent_framerate)); |
} |
- CHECK(test_stream->input_file.Initialize(base::FilePath(filename))); |
test_streams->push_back(test_stream); |
} |
} |
@@ -552,29 +633,6 @@ VEAClient::VEAClient(const TestStream& test_stream, |
EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0)); |
} |
- input_buffer_size_ = |
- media::VideoFrame::AllocationSize(kInputFormat, test_stream.size); |
- CHECK_GT(input_buffer_size_, 0UL); |
- |
- // Calculate the number of frames in the input stream by dividing its length |
- // in bytes by frame size in bytes. |
- CHECK_EQ(test_stream_.input_file.length() % input_buffer_size_, 0U) |
- << "Stream byte size is not a product of calculated frame byte size"; |
- num_frames_in_stream_ = test_stream_.input_file.length() / input_buffer_size_; |
- CHECK_GT(num_frames_in_stream_, 0UL); |
- CHECK_LE(num_frames_in_stream_, kMaxFrameNum); |
- |
- // We may need to loop over the stream more than once if more frames than |
- // provided is required for bitrate tests. |
- if (force_bitrate_ && num_frames_in_stream_ < kMinFramesForBitrateTests) { |
- DVLOG(1) << "Stream too short for bitrate test (" << num_frames_in_stream_ |
- << " frames), will loop it to reach " << kMinFramesForBitrateTests |
- << " frames"; |
- num_frames_to_encode_ = kMinFramesForBitrateTests; |
- } else { |
- num_frames_to_encode_ = num_frames_in_stream_; |
- } |
- |
thread_checker_.DetachFromThread(); |
} |
@@ -629,6 +687,30 @@ void VEAClient::RequireBitstreamBuffers(unsigned int input_count, |
ASSERT_EQ(state_, CS_INITIALIZED); |
SetState(CS_ENCODING); |
+ PrepareAlignedTempFile(const_cast<TestStream*>(&test_stream_), |
+ input_coded_size, |
+ &input_buffer_size_); |
+ CHECK_GT(input_buffer_size_, 0UL); |
+ |
+ // Calculate the number of frames in the input stream by dividing its length |
+ // in bytes by frame size in bytes. |
+ CHECK_EQ(test_stream_.input_file.length() % input_buffer_size_, 0U) |
+ << "Stream byte size is not a product of calculated frame byte size"; |
+ num_frames_in_stream_ = test_stream_.input_file.length() / input_buffer_size_; |
+ CHECK_GT(num_frames_in_stream_, 0UL); |
+ CHECK_LE(num_frames_in_stream_, kMaxFrameNum); |
+ |
+ // We may need to loop over the stream more than once if more frames than |
+ // provided is required for bitrate tests. |
+ if (force_bitrate_ && num_frames_in_stream_ < kMinFramesForBitrateTests) { |
+ DVLOG(1) << "Stream too short for bitrate test (" << num_frames_in_stream_ |
+ << " frames), will loop it to reach " << kMinFramesForBitrateTests |
+ << " frames"; |
+ num_frames_to_encode_ = kMinFramesForBitrateTests; |
+ } else { |
+ num_frames_to_encode_ = num_frames_in_stream_; |
+ } |
+ |
// TODO(posciak): For now we only support input streams that meet encoder |
wuchengli
2014/08/26 09:53:24
remove the comments
henryhsu
2014/08/27 02:59:06
Done.
|
// size requirements exactly (i.e. coded size == visible size), so that we |
// can simply mmap the stream file and feed the encoder directly with chunks |
@@ -637,7 +719,6 @@ void VEAClient::RequireBitstreamBuffers(unsigned int input_count, |
// If/when this is changed, the ARM-specific alignment check below should be |
wuchengli
2014/08/26 09:53:25
Do we need to redo the below check?
henryhsu
2014/08/27 02:59:05
I think the below check is enabled when platform i
|
// redone as well. |
input_coded_size_ = input_coded_size; |
- ASSERT_EQ(input_coded_size_, test_stream_.size); |
#if defined(ARCH_CPU_ARMEL) |
// ARM performs CPU cache management with CPU cache line granularity. We thus |
// need to ensure our buffers are CPU cache line-aligned (64 byte-aligned). |
@@ -743,8 +824,14 @@ void VEAClient::InputNoLongerNeededCallback(int32 input_id) { |
scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
CHECK_LE(position + input_buffer_size_, test_stream_.input_file.length()); |
- uint8* frame_data = |
+ uint8* frame_data_y = |
const_cast<uint8*>(test_stream_.input_file.data() + position); |
+ uint8* frame_data_u = frame_data_y + |
+ ALIGN_64_BYTES(media::VideoFrame::PlaneAllocationSize( |
+ kInputFormat, 0, input_coded_size_)); |
+ uint8* frame_data_v = frame_data_u + |
+ ALIGN_64_BYTES(media::VideoFrame::PlaneAllocationSize( |
+ kInputFormat, 1, input_coded_size_)); |
CHECK_GT(current_framerate_, 0U); |
scoped_refptr<media::VideoFrame> frame = |
@@ -756,9 +843,9 @@ scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) { |
input_coded_size_.width(), |
input_coded_size_.width() / 2, |
input_coded_size_.width() / 2, |
- frame_data, |
- frame_data + input_coded_size_.GetArea(), |
- frame_data + (input_coded_size_.GetArea() * 5 / 4), |
+ frame_data_y, |
+ frame_data_u, |
+ frame_data_v, |
base::TimeDelta().FromMilliseconds( |
next_input_id_ * base::Time::kMillisecondsPerSecond / |
current_framerate_), |
@@ -973,6 +1060,7 @@ TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) { |
base::Bind(&VEAClient::DestroyEncoder, base::Unretained(clients[i]))); |
} |
+ RemoveTempFile(&test_streams); |
// This ensures all tasks have finished. |
encoder_thread.Stop(); |
} |