Commit 6424c5b0 authored by Dean Liao's avatar Dean Liao Committed by Commit Bot

media/base: overloads operator==, !=, << for VideoFrameLayout class.

It overloads operator==, !=, <<  for VideoFrameLayout.
Also, it removes VideoFrameLayout.ToString() as operator<<
can replace it.

BUG=b:110815424
TEST=pass media_unittests --gtest_filter=VideoFrameLayout.*

Change-Id: I634846e456a516695a8f792bebf726d1cea80465
Reviewed-on: https://chromium-review.googlesource.com/c/1333016
Commit-Queue: Shuo-Peng Liao <deanliao@google.com>
Reviewed-by: default avatarHirokazu Honda <hiroh@chromium.org>
Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608305}
parent e568dfd5
......@@ -139,21 +139,38 @@ size_t VideoFrameLayout::GetTotalBufferSize() const {
return std::accumulate(buffer_sizes_.begin(), buffer_sizes_.end(), 0u);
}
std::string VideoFrameLayout::ToString() const {
std::ostringstream s;
s << "VideoFrameLayout format: " << VideoPixelFormatToString(format_)
<< ", coded_size: " << coded_size_.ToString()
<< ", num_buffers: " << num_buffers()
<< ", buffer_sizes: " << VectorToString(buffer_sizes_)
<< ", num_planes: " << num_planes()
<< ", planes (stride, offset): " << VectorToString(planes_);
return s.str();
}
std::ostream& operator<<(std::ostream& ostream,
const VideoFrameLayout::Plane& plane) {
ostream << "(" << plane.stride << ", " << plane.offset << ")";
return ostream;
}
bool VideoFrameLayout::Plane::operator==(
const VideoFrameLayout::Plane& rhs) const {
return stride == rhs.stride && offset == rhs.offset;
}
bool VideoFrameLayout::Plane::operator!=(
const VideoFrameLayout::Plane& rhs) const {
return !(*this == rhs);
}
bool VideoFrameLayout::operator==(const VideoFrameLayout& rhs) const {
return format_ == rhs.format_ && coded_size_ == rhs.coded_size_ &&
planes_ == rhs.planes_ && buffer_sizes_ == rhs.buffer_sizes_;
}
bool VideoFrameLayout::operator!=(const VideoFrameLayout& rhs) const {
return !(*this == rhs);
}
std::ostream& operator<<(std::ostream& ostream,
const VideoFrameLayout& layout) {
ostream << "VideoFrameLayout(format: " << layout.format()
<< ", coded_size: " << layout.coded_size().ToString()
<< ", planes (stride, offset): " << VectorToString(layout.planes())
<< ", buffer_sizes: " << VectorToString(layout.buffer_sizes()) << ")";
return ostream;
}
} // namespace media
......@@ -39,9 +39,8 @@ class MEDIA_EXPORT VideoFrameLayout {
Plane() = default;
Plane(int32_t stride, size_t offset) : stride(stride), offset(offset) {}
bool operator==(const Plane& rhs) const {
return stride == rhs.stride && offset == rhs.offset;
}
bool operator==(const Plane& rhs) const;
bool operator!=(const Plane& rhs) const;
// Strides of a plane, typically greater or equal to the
// width of the surface divided by the horizontal sampling period. Note that
......@@ -106,8 +105,8 @@ class MEDIA_EXPORT VideoFrameLayout {
// Returns sum of bytes of all buffers.
size_t GetTotalBufferSize() const;
// Composes VideoFrameLayout as human readable string.
std::string ToString() const;
bool operator==(const VideoFrameLayout& rhs) const;
bool operator!=(const VideoFrameLayout& rhs) const;
// Returns the required memory alignment for buffers.
size_t buffer_addr_align() const {
......@@ -144,9 +143,13 @@ class MEDIA_EXPORT VideoFrameLayout {
};
// Outputs VideoFrameLayout::Plane to stream.
std::ostream& operator<<(std::ostream& ostream,
MEDIA_EXPORT std::ostream& operator<<(std::ostream& ostream,
const VideoFrameLayout::Plane& plane);
// Outputs VideoFrameLayout to stream.
MEDIA_EXPORT std::ostream& operator<<(std::ostream& ostream,
const VideoFrameLayout& layout);
} // namespace media
#endif // MEDIA_BASE_VIDEO_FRAME_LAYOUT_H_
......@@ -7,6 +7,7 @@
#include <stddef.h>
#include <stdint.h>
#include <sstream>
#include <string>
#include <utility>
......@@ -232,11 +233,12 @@ TEST(VideoFrameLayout, ToString) {
PIXEL_FORMAT_I420, coded_size, strides, buffer_sizes);
ASSERT_TRUE(layout.has_value());
EXPECT_EQ(layout->ToString(),
"VideoFrameLayout format: PIXEL_FORMAT_I420, coded_size: 320x180, "
"num_buffers: 3, buffer_sizes: [73728, 18432, 18432], "
"num_planes: 3, "
"planes (stride, offset): [(384, 0), (192, 0), (192, 0)]");
std::ostringstream ostream;
ostream << *layout;
EXPECT_EQ(ostream.str(),
"VideoFrameLayout(format: PIXEL_FORMAT_I420, coded_size: 320x180, "
"planes (stride, offset): [(384, 0), (192, 0), (192, 0)], "
"buffer_sizes: [73728, 18432, 18432])");
}
TEST(VideoFrameLayout, ToStringOneBuffer) {
......@@ -249,10 +251,11 @@ TEST(VideoFrameLayout, ToStringOneBuffer) {
buffer_sizes);
ASSERT_TRUE(layout.has_value());
EXPECT_EQ(layout->ToString(),
"VideoFrameLayout format: PIXEL_FORMAT_NV12, coded_size: 320x180, "
"num_buffers: 1, buffer_sizes: [122880], num_planes: 1, "
"planes (stride, offset): [(384, 100)]");
std::ostringstream ostream;
ostream << *layout;
EXPECT_EQ(ostream.str(),
"VideoFrameLayout(format: PIXEL_FORMAT_NV12, coded_size: 320x180, "
"planes (stride, offset): [(384, 100)], buffer_sizes: [122880])");
}
TEST(VideoFrameLayout, ToStringNoBufferInfo) {
......@@ -260,10 +263,35 @@ TEST(VideoFrameLayout, ToStringNoBufferInfo) {
auto layout = VideoFrameLayout::Create(PIXEL_FORMAT_NV12, coded_size);
ASSERT_TRUE(layout.has_value());
EXPECT_EQ(layout->ToString(),
"VideoFrameLayout format: PIXEL_FORMAT_NV12, coded_size: 320x180, "
"num_buffers: 0, buffer_sizes: [], num_planes: 2, "
"planes (stride, offset): [(0, 0), (0, 0)]");
std::ostringstream ostream;
ostream << *layout;
EXPECT_EQ(ostream.str(),
"VideoFrameLayout(format: PIXEL_FORMAT_NV12, coded_size: 320x180, "
"planes (stride, offset): [(0, 0), (0, 0)], buffer_sizes: [])");
}
TEST(VideoFrameLayout, EqualOperator) {
gfx::Size coded_size = gfx::Size(320, 180);
std::vector<int32_t> strides = {384, 192, 192};
std::vector<size_t> offsets = {0, 100, 200};
std::vector<size_t> buffer_sizes = {73728, 18432, 18432};
auto layout = VideoFrameLayout::CreateWithPlanes(
PIXEL_FORMAT_I420, coded_size, CreatePlanes(strides, offsets),
buffer_sizes);
ASSERT_TRUE(layout.has_value());
auto same_layout = VideoFrameLayout::CreateWithPlanes(
PIXEL_FORMAT_I420, coded_size, CreatePlanes(strides, offsets),
buffer_sizes);
ASSERT_TRUE(same_layout.has_value());
EXPECT_EQ(*layout, *same_layout);
std::vector<size_t> another_buffer_sizes = {73728};
auto different_layout = VideoFrameLayout::CreateWithPlanes(
PIXEL_FORMAT_I420, coded_size, CreatePlanes(strides, offsets),
another_buffer_sizes);
ASSERT_TRUE(different_layout.has_value());
EXPECT_NE(*layout, *different_layout);
}
} // namespace media
......@@ -5,6 +5,7 @@
#include "media/gpu/v4l2/v4l2_device.h"
#include <cstring>
#include <sstream>
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
......@@ -81,11 +82,12 @@ TEST(V4L2DeviceTest, V4L2FormatToVideoFrameLayoutNV12) {
EXPECT_EQ(expected_planes, layout->planes());
EXPECT_EQ(std::vector<size_t>({86400u}), layout->buffer_sizes());
EXPECT_EQ(86400u, layout->GetTotalBufferSize());
EXPECT_EQ(
"VideoFrameLayout format: PIXEL_FORMAT_NV12, coded_size: 300x180, "
"num_buffers: 1, buffer_sizes: [86400], num_planes: 2, "
"planes (stride, offset): [(320, 0), (320, 57600)]",
layout->ToString());
std::ostringstream ostream;
ostream << *layout;
EXPECT_EQ(ostream.str(),
"VideoFrameLayout(format: PIXEL_FORMAT_NV12, coded_size: 300x180, "
"planes (stride, offset): [(320, 0), (320, 57600)], "
"buffer_sizes: [86400])");
}
// Test V4L2FormatToVideoFrameLayout with YUV420 pixelformat, which has one
......@@ -102,11 +104,12 @@ TEST(V4L2DeviceTest, V4L2FormatToVideoFrameLayoutYUV420) {
EXPECT_EQ(expected_planes, layout->planes());
EXPECT_EQ(std::vector<size_t>({86400u}), layout->buffer_sizes());
EXPECT_EQ(86400u, layout->GetTotalBufferSize());
EXPECT_EQ(
"VideoFrameLayout format: PIXEL_FORMAT_I420, coded_size: 300x180, "
"num_buffers: 1, buffer_sizes: [86400], num_planes: 3, "
"planes (stride, offset): [(320, 0), (160, 57600), (160, 72000)]",
layout->ToString());
std::ostringstream ostream;
ostream << *layout;
EXPECT_EQ(ostream.str(),
"VideoFrameLayout(format: PIXEL_FORMAT_I420, coded_size: 300x180, "
"planes (stride, offset): [(320, 0), (160, 57600), (160, 72000)], "
"buffer_sizes: [86400])");
}
// Test V4L2FormatToVideoFrameLayout with single planar v4l2_format.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment