OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/video_frame.h" |
| 6 |
| 7 #include "base/format_macros.h" |
| 8 #include "base/string_util.h" |
| 9 #include "media/base/buffers.h" |
| 10 #include "media/base/mock_filters.h" |
| 11 #include "media/base/yuv_convert.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 // Helper function that initializes a YV12 frame with white and black scan |
| 17 // lines based on the |white_to_black| parameter. If 0, then the entire |
| 18 // frame will be black, if 1 then the entire frame will be white. |
| 19 void InitializeYV12Frame(VideoFrame* frame, double white_to_black) { |
| 20 EXPECT_EQ(VideoFrame::YV12, frame->format()); |
| 21 size_t first_black_row = static_cast<size_t>(frame->height() * |
| 22 white_to_black); |
| 23 uint8* y_plane = frame->data(VideoFrame::kYPlane); |
| 24 for (size_t row = 0; row < frame->height(); ++row) { |
| 25 int color = (row < first_black_row) ? 0xFF : 0x00; |
| 26 memset(y_plane, color, frame->width()); |
| 27 y_plane += frame->stride(VideoFrame::kYPlane); |
| 28 } |
| 29 uint8* u_plane = frame->data(VideoFrame::kUPlane); |
| 30 uint8* v_plane = frame->data(VideoFrame::kVPlane); |
| 31 for (size_t row = 0; row < frame->height(); row += 2) { |
| 32 memset(u_plane, 0x80, frame->width() / 2); |
| 33 memset(v_plane, 0x80, frame->width() / 2); |
| 34 u_plane += frame->stride(VideoFrame::kUPlane); |
| 35 v_plane += frame->stride(VideoFrame::kVPlane); |
| 36 } |
| 37 } |
| 38 |
| 39 // Given a |yv12_frame| this method converts the YV12 frame to RGBA and |
| 40 // makes sure that all the pixels of the RBG frame equal |expect_rgb_color|. |
| 41 void ExpectFrameColor(media::VideoFrame* yv12_frame, uint32 expect_rgb_color) { |
| 42 // On linux and mac builds if you directly compare using EXPECT_EQ and use |
| 43 // the VideoFrame::kNumxxxPlanes constants, it generates an error when |
| 44 // linking. These are declared so that we can compare against locals. |
| 45 const size_t expect_yuv_planes = VideoFrame::kNumYUVPlanes; |
| 46 const size_t expect_rgb_planes = VideoFrame::kNumRGBPlanes; |
| 47 |
| 48 ASSERT_EQ(VideoFrame::YV12, yv12_frame->format()); |
| 49 ASSERT_EQ(expect_yuv_planes, yv12_frame->planes()); |
| 50 ASSERT_EQ(yv12_frame->stride(VideoFrame::kUPlane), |
| 51 yv12_frame->stride(VideoFrame::kVPlane)); |
| 52 |
| 53 scoped_refptr<media::VideoFrame> rgb_frame; |
| 54 media::VideoFrame::CreateFrame(VideoFrame::RGBA, |
| 55 yv12_frame->width(), |
| 56 yv12_frame->height(), |
| 57 yv12_frame->GetTimestamp(), |
| 58 yv12_frame->GetDuration(), |
| 59 &rgb_frame); |
| 60 |
| 61 ASSERT_EQ(yv12_frame->width(), rgb_frame->width()); |
| 62 ASSERT_EQ(yv12_frame->height(), rgb_frame->height()); |
| 63 ASSERT_EQ(expect_rgb_planes, rgb_frame->planes()); |
| 64 |
| 65 media::ConvertYUVToRGB32(yv12_frame->data(VideoFrame::kYPlane), |
| 66 yv12_frame->data(VideoFrame::kUPlane), |
| 67 yv12_frame->data(VideoFrame::kVPlane), |
| 68 rgb_frame->data(VideoFrame::kRGBPlane), |
| 69 rgb_frame->width(), |
| 70 rgb_frame->height(), |
| 71 yv12_frame->stride(VideoFrame::kYPlane), |
| 72 yv12_frame->stride(VideoFrame::kUPlane), |
| 73 rgb_frame->stride(VideoFrame::kRGBPlane), |
| 74 media::YV12); |
| 75 |
| 76 for (size_t row = 0; row < rgb_frame->height(); ++row) { |
| 77 uint32* rgb_row_data = reinterpret_cast<uint32*>( |
| 78 rgb_frame->data(VideoFrame::kRGBPlane) + |
| 79 (rgb_frame->stride(VideoFrame::kRGBPlane) * row)); |
| 80 for (size_t col = 0; col < rgb_frame->width(); ++col) { |
| 81 SCOPED_TRACE(StringPrintf("Checking (%" PRIuS ", %" PRIuS ")", |
| 82 row, col)); |
| 83 EXPECT_EQ(expect_rgb_color, rgb_row_data[col]); |
| 84 } |
| 85 } |
| 86 } |
| 87 |
| 88 TEST(VideoFrame, CreateFrame) { |
| 89 const size_t kWidth = 64; |
| 90 const size_t kHeight = 48; |
| 91 const base::TimeDelta kTimestampA = base::TimeDelta::FromMicroseconds(1337); |
| 92 const base::TimeDelta kDurationA = base::TimeDelta::FromMicroseconds(1667); |
| 93 const base::TimeDelta kTimestampB = base::TimeDelta::FromMicroseconds(1234); |
| 94 const base::TimeDelta kDurationB = base::TimeDelta::FromMicroseconds(5678); |
| 95 |
| 96 // Create a YV12 Video Frame. |
| 97 scoped_refptr<media::VideoFrame> frame; |
| 98 VideoFrame::CreateFrame(media::VideoFrame::YV12, kWidth, kHeight, |
| 99 kTimestampA, kDurationA, &frame); |
| 100 ASSERT_TRUE(frame); |
| 101 |
| 102 // Test StreamSample implementation. |
| 103 EXPECT_EQ(kTimestampA.InMicroseconds(), |
| 104 frame->GetTimestamp().InMicroseconds()); |
| 105 EXPECT_EQ(kDurationA.InMicroseconds(), |
| 106 frame->GetDuration().InMicroseconds()); |
| 107 EXPECT_FALSE(frame->IsEndOfStream()); |
| 108 EXPECT_FALSE(frame->IsDiscontinuous()); |
| 109 frame->SetTimestamp(kTimestampB); |
| 110 frame->SetDuration(kDurationB); |
| 111 EXPECT_EQ(kTimestampB.InMicroseconds(), |
| 112 frame->GetTimestamp().InMicroseconds()); |
| 113 EXPECT_EQ(kDurationB.InMicroseconds(), |
| 114 frame->GetDuration().InMicroseconds()); |
| 115 EXPECT_FALSE(frame->IsEndOfStream()); |
| 116 frame->SetDiscontinuous(true); |
| 117 EXPECT_TRUE(frame->IsDiscontinuous()); |
| 118 frame->SetDiscontinuous(false); |
| 119 EXPECT_FALSE(frame->IsDiscontinuous()); |
| 120 |
| 121 // Test VideoFrame implementation. |
| 122 { |
| 123 SCOPED_TRACE(""); |
| 124 InitializeYV12Frame(frame, 0.0f); |
| 125 ExpectFrameColor(frame, 0xFF000000); |
| 126 } |
| 127 { |
| 128 SCOPED_TRACE(""); |
| 129 InitializeYV12Frame(frame, 1.0f); |
| 130 ExpectFrameColor(frame, 0xFFFFFFFF); |
| 131 } |
| 132 |
| 133 // Test an empty frame. |
| 134 VideoFrame::CreateEmptyFrame(&frame); |
| 135 EXPECT_TRUE(frame->IsEndOfStream()); |
| 136 } |
| 137 |
| 138 TEST(VideoFrame, CreateBlackFrame) { |
| 139 const size_t kWidth = 2; |
| 140 const size_t kHeight = 2; |
| 141 const uint8 kExpectedYRow[] = { 0, 0 }; |
| 142 const uint8 kExpectedUVRow[] = { 128 }; |
| 143 |
| 144 scoped_refptr<media::VideoFrame> frame; |
| 145 VideoFrame::CreateBlackFrame(kWidth, kHeight, &frame); |
| 146 ASSERT_TRUE(frame); |
| 147 |
| 148 // Test basic properties. |
| 149 EXPECT_EQ(0, frame->GetTimestamp().InMicroseconds()); |
| 150 EXPECT_EQ(0, frame->GetDuration().InMicroseconds()); |
| 151 EXPECT_FALSE(frame->IsEndOfStream()); |
| 152 |
| 153 // Test |frame| properties. |
| 154 EXPECT_EQ(VideoFrame::YV12, frame->format()); |
| 155 EXPECT_EQ(kWidth, frame->width()); |
| 156 EXPECT_EQ(kHeight, frame->height()); |
| 157 EXPECT_EQ(3u, frame->planes()); |
| 158 |
| 159 // Test frames themselves. |
| 160 uint8* y_plane = frame->data(VideoFrame::kYPlane); |
| 161 for (size_t y = 0; y < frame->height(); ++y) { |
| 162 EXPECT_EQ(0, memcmp(kExpectedYRow, y_plane, arraysize(kExpectedYRow))); |
| 163 y_plane += frame->stride(VideoFrame::kYPlane); |
| 164 } |
| 165 |
| 166 uint8* u_plane = frame->data(VideoFrame::kUPlane); |
| 167 uint8* v_plane = frame->data(VideoFrame::kVPlane); |
| 168 for (size_t y = 0; y < frame->height() / 2; ++y) { |
| 169 EXPECT_EQ(0, memcmp(kExpectedUVRow, u_plane, arraysize(kExpectedUVRow))); |
| 170 EXPECT_EQ(0, memcmp(kExpectedUVRow, v_plane, arraysize(kExpectedUVRow))); |
| 171 u_plane += frame->stride(VideoFrame::kUPlane); |
| 172 v_plane += frame->stride(VideoFrame::kVPlane); |
| 173 } |
| 174 } |
| 175 |
| 176 } // namespace media |
OLD | NEW |