1 // Copyright (c) 2012 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.
5 #include "media/base/video_frame.h"
8 #include "base/callback_helpers.h"
9 #include "base/format_macros.h"
10 #include "base/memory/aligned_memory.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/stringprintf.h"
13 #include "gpu/command_buffer/common/mailbox_holder.h"
14 #include "media/base/buffers.h"
15 #include "media/base/yuv_convert.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 using base::MD5DigestToBase16
;
22 // Helper function that initializes a YV12 frame with white and black scan
23 // lines based on the |white_to_black| parameter. If 0, then the entire
24 // frame will be black, if 1 then the entire frame will be white.
25 void InitializeYV12Frame(VideoFrame
* frame
, double white_to_black
) {
26 EXPECT_EQ(VideoFrame::YV12
, frame
->format());
27 int first_black_row
= static_cast<int>(frame
->coded_size().height() *
29 uint8
* y_plane
= frame
->data(VideoFrame::kYPlane
);
30 for (int row
= 0; row
< frame
->coded_size().height(); ++row
) {
31 int color
= (row
< first_black_row
) ? 0xFF : 0x00;
32 memset(y_plane
, color
, frame
->stride(VideoFrame::kYPlane
));
33 y_plane
+= frame
->stride(VideoFrame::kYPlane
);
35 uint8
* u_plane
= frame
->data(VideoFrame::kUPlane
);
36 uint8
* v_plane
= frame
->data(VideoFrame::kVPlane
);
37 for (int row
= 0; row
< frame
->coded_size().height(); row
+= 2) {
38 memset(u_plane
, 0x80, frame
->stride(VideoFrame::kUPlane
));
39 memset(v_plane
, 0x80, frame
->stride(VideoFrame::kVPlane
));
40 u_plane
+= frame
->stride(VideoFrame::kUPlane
);
41 v_plane
+= frame
->stride(VideoFrame::kVPlane
);
45 // Given a |yv12_frame| this method converts the YV12 frame to RGBA and
46 // makes sure that all the pixels of the RBG frame equal |expect_rgb_color|.
47 void ExpectFrameColor(media::VideoFrame
* yv12_frame
, uint32 expect_rgb_color
) {
48 ASSERT_EQ(VideoFrame::YV12
, yv12_frame
->format());
49 ASSERT_EQ(yv12_frame
->stride(VideoFrame::kUPlane
),
50 yv12_frame
->stride(VideoFrame::kVPlane
));
52 yv12_frame
->coded_size().width() & (VideoFrame::kFrameSizeAlignment
- 1),
55 yv12_frame
->coded_size().height() & (VideoFrame::kFrameSizeAlignment
- 1),
58 size_t bytes_per_row
= yv12_frame
->coded_size().width() * 4u;
59 uint8
* rgb_data
= reinterpret_cast<uint8
*>(
60 base::AlignedAlloc(bytes_per_row
* yv12_frame
->coded_size().height() +
61 VideoFrame::kFrameSizePadding
,
62 VideoFrame::kFrameAddressAlignment
));
64 media::ConvertYUVToRGB32(yv12_frame
->data(VideoFrame::kYPlane
),
65 yv12_frame
->data(VideoFrame::kUPlane
),
66 yv12_frame
->data(VideoFrame::kVPlane
),
68 yv12_frame
->coded_size().width(),
69 yv12_frame
->coded_size().height(),
70 yv12_frame
->stride(VideoFrame::kYPlane
),
71 yv12_frame
->stride(VideoFrame::kUPlane
),
75 for (int row
= 0; row
< yv12_frame
->coded_size().height(); ++row
) {
76 uint32
* rgb_row_data
= reinterpret_cast<uint32
*>(
77 rgb_data
+ (bytes_per_row
* row
));
78 for (int col
= 0; col
< yv12_frame
->coded_size().width(); ++col
) {
79 SCOPED_TRACE(base::StringPrintf("Checking (%d, %d)", row
, col
));
80 EXPECT_EQ(expect_rgb_color
, rgb_row_data
[col
]);
84 base::AlignedFree(rgb_data
);
87 // Fill each plane to its reported extents and verify accessors report non
88 // zero values. Additionally, for the first plane verify the rows and
89 // row_bytes values are correct.
90 void ExpectFrameExtents(VideoFrame::Format format
, const char* expected_hash
) {
91 const unsigned char kFillByte
= 0x80;
92 const int kWidth
= 61;
93 const int kHeight
= 31;
94 const base::TimeDelta kTimestamp
= base::TimeDelta::FromMicroseconds(1337);
96 gfx::Size
size(kWidth
, kHeight
);
97 scoped_refptr
<VideoFrame
> frame
= VideoFrame::CreateFrame(
98 format
, size
, gfx::Rect(size
), size
, kTimestamp
);
99 ASSERT_TRUE(frame
.get());
101 int planes
= VideoFrame::NumPlanes(format
);
102 for (int plane
= 0; plane
< planes
; plane
++) {
103 SCOPED_TRACE(base::StringPrintf("Checking plane %d", plane
));
104 EXPECT_TRUE(frame
->data(plane
));
105 EXPECT_TRUE(frame
->stride(plane
));
106 EXPECT_TRUE(frame
->rows(plane
));
107 EXPECT_TRUE(frame
->row_bytes(plane
));
109 memset(frame
->data(plane
), kFillByte
,
110 frame
->stride(plane
) * frame
->rows(plane
));
113 base::MD5Context context
;
114 base::MD5Init(&context
);
115 frame
->HashFrameForTesting(&context
);
116 base::MD5Digest digest
;
117 base::MD5Final(&digest
, &context
);
118 EXPECT_EQ(MD5DigestToBase16(digest
), expected_hash
);
121 TEST(VideoFrame
, CreateFrame
) {
122 const int kWidth
= 64;
123 const int kHeight
= 48;
124 const base::TimeDelta kTimestamp
= base::TimeDelta::FromMicroseconds(1337);
126 // Create a YV12 Video Frame.
127 gfx::Size
size(kWidth
, kHeight
);
128 scoped_refptr
<media::VideoFrame
> frame
=
129 VideoFrame::CreateFrame(media::VideoFrame::YV12
, size
, gfx::Rect(size
),
131 ASSERT_TRUE(frame
.get());
133 // Test VideoFrame implementation.
134 EXPECT_EQ(media::VideoFrame::YV12
, frame
->format());
137 InitializeYV12Frame(frame
.get(), 0.0f
);
138 ExpectFrameColor(frame
.get(), 0xFF000000);
140 base::MD5Digest digest
;
141 base::MD5Context context
;
142 base::MD5Init(&context
);
143 frame
->HashFrameForTesting(&context
);
144 base::MD5Final(&digest
, &context
);
145 EXPECT_EQ(MD5DigestToBase16(digest
), "9065c841d9fca49186ef8b4ef547e79b");
148 InitializeYV12Frame(frame
.get(), 1.0f
);
149 ExpectFrameColor(frame
.get(), 0xFFFFFFFF);
151 base::MD5Init(&context
);
152 frame
->HashFrameForTesting(&context
);
153 base::MD5Final(&digest
, &context
);
154 EXPECT_EQ(MD5DigestToBase16(digest
), "911991d51438ad2e1a40ed5f6fc7c796");
156 // Test an empty frame.
157 frame
= VideoFrame::CreateEOSFrame();
159 frame
->metadata()->IsTrue(VideoFrameMetadata::END_OF_STREAM
));
162 TEST(VideoFrame
, CreateBlackFrame
) {
163 const int kWidth
= 2;
164 const int kHeight
= 2;
165 const uint8 kExpectedYRow
[] = { 0, 0 };
166 const uint8 kExpectedUVRow
[] = { 128 };
168 scoped_refptr
<media::VideoFrame
> frame
=
169 VideoFrame::CreateBlackFrame(gfx::Size(kWidth
, kHeight
));
170 ASSERT_TRUE(frame
.get());
172 // Test basic properties.
173 EXPECT_EQ(0, frame
->timestamp().InMicroseconds());
175 frame
->metadata()->IsTrue(VideoFrameMetadata::END_OF_STREAM
));
177 // Test |frame| properties.
178 EXPECT_EQ(VideoFrame::YV12
, frame
->format());
179 EXPECT_EQ(kWidth
, frame
->coded_size().width());
180 EXPECT_EQ(kHeight
, frame
->coded_size().height());
182 // Test frames themselves.
183 uint8
* y_plane
= frame
->data(VideoFrame::kYPlane
);
184 for (int y
= 0; y
< frame
->coded_size().height(); ++y
) {
185 EXPECT_EQ(0, memcmp(kExpectedYRow
, y_plane
, arraysize(kExpectedYRow
)));
186 y_plane
+= frame
->stride(VideoFrame::kYPlane
);
189 uint8
* u_plane
= frame
->data(VideoFrame::kUPlane
);
190 uint8
* v_plane
= frame
->data(VideoFrame::kVPlane
);
191 for (int y
= 0; y
< frame
->coded_size().height() / 2; ++y
) {
192 EXPECT_EQ(0, memcmp(kExpectedUVRow
, u_plane
, arraysize(kExpectedUVRow
)));
193 EXPECT_EQ(0, memcmp(kExpectedUVRow
, v_plane
, arraysize(kExpectedUVRow
)));
194 u_plane
+= frame
->stride(VideoFrame::kUPlane
);
195 v_plane
+= frame
->stride(VideoFrame::kVPlane
);
199 static void FrameNoLongerNeededCallback(
200 const scoped_refptr
<media::VideoFrame
>& frame
,
205 TEST(VideoFrame
, WrapVideoFrame
) {
206 const int kWidth
= 4;
207 const int kHeight
= 4;
208 scoped_refptr
<media::VideoFrame
> frame
;
209 bool done_callback_was_run
= false;
211 scoped_refptr
<media::VideoFrame
> wrapped_frame
=
212 VideoFrame::CreateBlackFrame(gfx::Size(kWidth
, kHeight
));
213 ASSERT_TRUE(wrapped_frame
.get());
215 gfx::Rect
visible_rect(1, 1, 1, 1);
216 gfx::Size natural_size
= visible_rect
.size();
217 frame
= media::VideoFrame::WrapVideoFrame(
218 wrapped_frame
, visible_rect
, natural_size
);
219 frame
->AddDestructionObserver(
220 base::Bind(&FrameNoLongerNeededCallback
, wrapped_frame
,
221 &done_callback_was_run
));
222 EXPECT_EQ(wrapped_frame
->coded_size(), frame
->coded_size());
223 EXPECT_EQ(wrapped_frame
->data(media::VideoFrame::kYPlane
),
224 frame
->data(media::VideoFrame::kYPlane
));
225 EXPECT_NE(wrapped_frame
->visible_rect(), frame
->visible_rect());
226 EXPECT_EQ(visible_rect
, frame
->visible_rect());
227 EXPECT_NE(wrapped_frame
->natural_size(), frame
->natural_size());
228 EXPECT_EQ(natural_size
, frame
->natural_size());
231 EXPECT_FALSE(done_callback_was_run
);
233 EXPECT_TRUE(done_callback_was_run
);
236 // Ensure each frame is properly sized and allocated. Will trigger OOB reads
237 // and writes as well as incorrect frame hashes otherwise.
238 TEST(VideoFrame
, CheckFrameExtents
) {
239 // Each call consists of a VideoFrame::Format and the expected hash of all
240 // planes if filled with kFillByte (defined in ExpectFrameExtents).
241 ExpectFrameExtents(VideoFrame::YV12
, "8e5d54cb23cd0edca111dd35ffb6ff05");
242 ExpectFrameExtents(VideoFrame::YV16
, "cce408a044b212db42a10dfec304b3ef");
245 static void TextureCallback(uint32
* called_sync_point
,
246 uint32 release_sync_point
) {
247 *called_sync_point
= release_sync_point
;
250 // Verify the gpu::MailboxHolder::ReleaseCallback is called when VideoFrame is
251 // destroyed with the default release sync point.
252 TEST(VideoFrame
, TextureNoLongerNeededCallbackIsCalled
) {
253 uint32 called_sync_point
= 1;
256 scoped_refptr
<VideoFrame
> frame
= VideoFrame::WrapNativeTexture(
258 gpu::MailboxHolder(gpu::Mailbox(), 5, 0 /* sync_point */),
259 base::Bind(&TextureCallback
, &called_sync_point
),
260 gfx::Size(10, 10), // coded_size
261 gfx::Rect(10, 10), // visible_rect
262 gfx::Size(10, 10), // natural_size
263 base::TimeDelta()); // timestamp
264 EXPECT_EQ(VideoFrame::STORAGE_TEXTURE
, frame
->storage_type());
265 EXPECT_EQ(VideoFrame::ARGB
, frame
->format());
267 // Nobody set a sync point to |frame|, so |frame| set |called_sync_point| to 0
269 EXPECT_EQ(0u, called_sync_point
);
274 class SyncPointClientImpl
: public VideoFrame::SyncPointClient
{
276 explicit SyncPointClientImpl(uint32 sync_point
) : sync_point_(sync_point
) {}
277 ~SyncPointClientImpl() override
{}
278 uint32
InsertSyncPoint() override
{ return sync_point_
; }
279 void WaitSyncPoint(uint32 sync_point
) override
{}
287 // Verify the gpu::MailboxHolder::ReleaseCallback is called when VideoFrame is
288 // destroyed with the release sync point, which was updated by clients.
289 // (i.e. the compositor, webgl).
291 TexturesNoLongerNeededCallbackAfterTakingAndReleasingMailboxes
) {
292 const int kPlanesNum
= 3;
293 gpu::Mailbox mailbox
[kPlanesNum
];
294 for (int i
= 0; i
< kPlanesNum
; ++i
) {
295 mailbox
[i
].name
[0] = 50 + 1;
298 uint32 sync_point
= 7;
300 uint32 release_sync_point
= 111;
301 uint32 called_sync_point
= 0;
303 scoped_refptr
<VideoFrame
> frame
= VideoFrame::WrapYUV420NativeTextures(
304 gpu::MailboxHolder(mailbox
[VideoFrame::kYPlane
], target
, sync_point
),
305 gpu::MailboxHolder(mailbox
[VideoFrame::kUPlane
], target
, sync_point
),
306 gpu::MailboxHolder(mailbox
[VideoFrame::kVPlane
], target
, sync_point
),
307 base::Bind(&TextureCallback
, &called_sync_point
),
308 gfx::Size(10, 10), // coded_size
309 gfx::Rect(10, 10), // visible_rect
310 gfx::Size(10, 10), // natural_size
311 base::TimeDelta()); // timestamp
313 EXPECT_EQ(VideoFrame::STORAGE_TEXTURE
, frame
->storage_type());
314 EXPECT_EQ(VideoFrame::I420
, frame
->format());
315 EXPECT_EQ(3u, VideoFrame::NumPlanes(frame
->format()));
316 for (size_t i
= 0; i
< VideoFrame::NumPlanes(frame
->format());
318 const gpu::MailboxHolder
& mailbox_holder
= frame
->mailbox_holder(i
);
319 EXPECT_EQ(mailbox
[i
].name
[0], mailbox_holder
.mailbox
.name
[0]);
320 EXPECT_EQ(target
, mailbox_holder
.texture_target
);
321 EXPECT_EQ(sync_point
, mailbox_holder
.sync_point
);
324 SyncPointClientImpl
client(release_sync_point
);
325 frame
->UpdateReleaseSyncPoint(&client
);
326 EXPECT_EQ(sync_point
,
327 frame
->mailbox_holder(VideoFrame::kYPlane
).sync_point
);
329 EXPECT_EQ(release_sync_point
, called_sync_point
);
332 TEST(VideoFrame
, ZeroInitialized
) {
333 const int kWidth
= 64;
334 const int kHeight
= 48;
335 const base::TimeDelta kTimestamp
= base::TimeDelta::FromMicroseconds(1337);
337 gfx::Size
size(kWidth
, kHeight
);
338 scoped_refptr
<media::VideoFrame
> frame
= VideoFrame::CreateFrame(
339 media::VideoFrame::YV12
, size
, gfx::Rect(size
), size
, kTimestamp
);
341 for (size_t i
= 0; i
< VideoFrame::NumPlanes(frame
->format()); ++i
)
342 EXPECT_EQ(0, frame
->data(i
)[0]);
345 TEST(VideoFrameMetadata
, SetAndThenGetAllKeysForAllTypes
) {
346 VideoFrameMetadata metadata
;
348 for (int i
= 0; i
< VideoFrameMetadata::NUM_KEYS
; ++i
) {
349 const VideoFrameMetadata::Key key
= static_cast<VideoFrameMetadata::Key
>(i
);
351 EXPECT_FALSE(metadata
.HasKey(key
));
352 metadata
.SetBoolean(key
, true);
353 EXPECT_TRUE(metadata
.HasKey(key
));
354 bool bool_value
= false;
355 EXPECT_TRUE(metadata
.GetBoolean(key
, &bool_value
));
356 EXPECT_EQ(true, bool_value
);
359 EXPECT_FALSE(metadata
.HasKey(key
));
360 metadata
.SetInteger(key
, i
);
361 EXPECT_TRUE(metadata
.HasKey(key
));
362 int int_value
= -999;
363 EXPECT_TRUE(metadata
.GetInteger(key
, &int_value
));
364 EXPECT_EQ(i
, int_value
);
367 EXPECT_FALSE(metadata
.HasKey(key
));
368 metadata
.SetDouble(key
, 3.14 * i
);
369 EXPECT_TRUE(metadata
.HasKey(key
));
370 double double_value
= -999.99;
371 EXPECT_TRUE(metadata
.GetDouble(key
, &double_value
));
372 EXPECT_EQ(3.14 * i
, double_value
);
375 EXPECT_FALSE(metadata
.HasKey(key
));
376 metadata
.SetString(key
, base::StringPrintf("\xfe%d\xff", i
));
377 EXPECT_TRUE(metadata
.HasKey(key
));
378 std::string string_value
;
379 EXPECT_TRUE(metadata
.GetString(key
, &string_value
));
380 EXPECT_EQ(base::StringPrintf("\xfe%d\xff", i
), string_value
);
383 EXPECT_FALSE(metadata
.HasKey(key
));
384 metadata
.SetTimeDelta(key
, base::TimeDelta::FromInternalValue(42 + i
));
385 EXPECT_TRUE(metadata
.HasKey(key
));
386 base::TimeDelta delta_value
;
387 EXPECT_TRUE(metadata
.GetTimeDelta(key
, &delta_value
));
388 EXPECT_EQ(base::TimeDelta::FromInternalValue(42 + i
), delta_value
);
391 EXPECT_FALSE(metadata
.HasKey(key
));
392 metadata
.SetTimeTicks(key
, base::TimeTicks::FromInternalValue(~(0LL) + i
));
393 EXPECT_TRUE(metadata
.HasKey(key
));
394 base::TimeTicks ticks_value
;
395 EXPECT_TRUE(metadata
.GetTimeTicks(key
, &ticks_value
));
396 EXPECT_EQ(base::TimeTicks::FromInternalValue(~(0LL) + i
), ticks_value
);
399 EXPECT_FALSE(metadata
.HasKey(key
));
400 metadata
.SetValue(key
, base::Value::CreateNullValue());
401 EXPECT_TRUE(metadata
.HasKey(key
));
402 const base::Value
* const null_value
= metadata
.GetValue(key
);
403 EXPECT_TRUE(null_value
);
404 EXPECT_EQ(base::Value::TYPE_NULL
, null_value
->GetType());
409 TEST(VideoFrameMetadata
, PassMetadataViaIntermediary
) {
410 VideoFrameMetadata expected
;
411 for (int i
= 0; i
< VideoFrameMetadata::NUM_KEYS
; ++i
) {
412 const VideoFrameMetadata::Key key
= static_cast<VideoFrameMetadata::Key
>(i
);
413 expected
.SetInteger(key
, i
);
416 base::DictionaryValue tmp
;
417 expected
.MergeInternalValuesInto(&tmp
);
418 EXPECT_EQ(static_cast<size_t>(VideoFrameMetadata::NUM_KEYS
), tmp
.size());
420 VideoFrameMetadata result
;
421 result
.MergeInternalValuesFrom(tmp
);
423 for (int i
= 0; i
< VideoFrameMetadata::NUM_KEYS
; ++i
) {
424 const VideoFrameMetadata::Key key
= static_cast<VideoFrameMetadata::Key
>(i
);
426 EXPECT_TRUE(result
.GetInteger(key
, &value
));