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(PIXEL_FORMAT_YV12
, frame
->format());
27 const int first_black_row
=
28 static_cast<int>(frame
->coded_size().height() * white_to_black
);
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(PIXEL_FORMAT_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(VideoPixelFormat 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 VideoFrame::HashFrameForTesting(&context
, frame
);
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
= VideoFrame::CreateFrame(
129 media::PIXEL_FORMAT_YV12
, size
, gfx::Rect(size
), size
, kTimestamp
);
130 ASSERT_TRUE(frame
.get());
132 // Test VideoFrame implementation.
133 EXPECT_EQ(media::PIXEL_FORMAT_YV12
, frame
->format());
136 InitializeYV12Frame(frame
.get(), 0.0f
);
137 ExpectFrameColor(frame
.get(), 0xFF000000);
139 base::MD5Digest digest
;
140 base::MD5Context context
;
141 base::MD5Init(&context
);
142 VideoFrame::HashFrameForTesting(&context
, frame
);
143 base::MD5Final(&digest
, &context
);
144 EXPECT_EQ(MD5DigestToBase16(digest
), "9065c841d9fca49186ef8b4ef547e79b");
147 InitializeYV12Frame(frame
.get(), 1.0f
);
148 ExpectFrameColor(frame
.get(), 0xFFFFFFFF);
150 base::MD5Init(&context
);
151 VideoFrame::HashFrameForTesting(&context
, frame
);
152 base::MD5Final(&digest
, &context
);
153 EXPECT_EQ(MD5DigestToBase16(digest
), "911991d51438ad2e1a40ed5f6fc7c796");
155 // Test an empty frame.
156 frame
= VideoFrame::CreateEOSFrame();
158 frame
->metadata()->IsTrue(VideoFrameMetadata::END_OF_STREAM
));
161 TEST(VideoFrame
, CreateBlackFrame
) {
162 const int kWidth
= 2;
163 const int kHeight
= 2;
164 const uint8 kExpectedYRow
[] = { 0, 0 };
165 const uint8 kExpectedUVRow
[] = { 128 };
167 scoped_refptr
<media::VideoFrame
> frame
=
168 VideoFrame::CreateBlackFrame(gfx::Size(kWidth
, kHeight
));
169 ASSERT_TRUE(frame
.get());
170 EXPECT_TRUE(frame
->IsMappable());
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(PIXEL_FORMAT_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 Format and the expected hash of all
240 // planes if filled with kFillByte (defined in ExpectFrameExtents).
241 ExpectFrameExtents(PIXEL_FORMAT_YV12
, "8e5d54cb23cd0edca111dd35ffb6ff05");
242 ExpectFrameExtents(PIXEL_FORMAT_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::Generate(), 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(PIXEL_FORMAT_ARGB
, frame
->format());
265 EXPECT_EQ(VideoFrame::STORAGE_OPAQUE
, frame
->storage_type());
266 EXPECT_TRUE(frame
->HasTextures());
268 // Nobody set a sync point to |frame|, so |frame| set |called_sync_point| to 0
270 EXPECT_EQ(0u, called_sync_point
);
275 class SyncPointClientImpl
: public VideoFrame::SyncPointClient
{
277 explicit SyncPointClientImpl(uint32 sync_point
) : sync_point_(sync_point
) {}
278 ~SyncPointClientImpl() override
{}
279 uint32
InsertSyncPoint() override
{ return sync_point_
; }
280 void WaitSyncPoint(uint32 sync_point
) override
{}
288 // Verify the gpu::MailboxHolder::ReleaseCallback is called when VideoFrame is
289 // destroyed with the release sync point, which was updated by clients.
290 // (i.e. the compositor, webgl).
292 TexturesNoLongerNeededCallbackAfterTakingAndReleasingMailboxes
) {
293 const int kPlanesNum
= 3;
294 gpu::Mailbox mailbox
[kPlanesNum
];
295 for (int i
= 0; i
< kPlanesNum
; ++i
) {
296 mailbox
[i
].name
[0] = 50 + 1;
299 uint32 sync_point
= 7;
301 uint32 release_sync_point
= 111;
302 uint32 called_sync_point
= 0;
304 scoped_refptr
<VideoFrame
> frame
= VideoFrame::WrapYUV420NativeTextures(
305 gpu::MailboxHolder(mailbox
[VideoFrame::kYPlane
], target
, sync_point
),
306 gpu::MailboxHolder(mailbox
[VideoFrame::kUPlane
], target
, sync_point
),
307 gpu::MailboxHolder(mailbox
[VideoFrame::kVPlane
], target
, sync_point
),
308 base::Bind(&TextureCallback
, &called_sync_point
),
309 gfx::Size(10, 10), // coded_size
310 gfx::Rect(10, 10), // visible_rect
311 gfx::Size(10, 10), // natural_size
312 base::TimeDelta()); // timestamp
314 EXPECT_EQ(VideoFrame::STORAGE_OPAQUE
, frame
->storage_type());
315 EXPECT_EQ(PIXEL_FORMAT_I420
, frame
->format());
316 EXPECT_EQ(3u, VideoFrame::NumPlanes(frame
->format()));
317 EXPECT_TRUE(frame
->HasTextures());
318 for (size_t i
= 0; i
< VideoFrame::NumPlanes(frame
->format()); ++i
) {
319 const gpu::MailboxHolder
& mailbox_holder
= frame
->mailbox_holder(i
);
320 EXPECT_EQ(mailbox
[i
].name
[0], mailbox_holder
.mailbox
.name
[0]);
321 EXPECT_EQ(target
, mailbox_holder
.texture_target
);
322 EXPECT_EQ(sync_point
, mailbox_holder
.sync_point
);
325 SyncPointClientImpl
client(release_sync_point
);
326 frame
->UpdateReleaseSyncPoint(&client
);
327 EXPECT_EQ(sync_point
,
328 frame
->mailbox_holder(VideoFrame::kYPlane
).sync_point
);
330 EXPECT_EQ(release_sync_point
, called_sync_point
);
333 TEST(VideoFrame
, IsValidConfig_OddCodedSize
) {
334 // Odd sizes are valid for all formats. Odd formats may be internally rounded
335 // in VideoFrame::CreateFrame because VideoFrame owns the allocation and can
336 // pad the requested coded_size to ensure the UV sample boundaries line up
337 // with the Y plane after subsample scaling. See CreateFrame_OddWidth.
338 gfx::Size
odd_size(677, 288);
340 // First choosing a format with sub-sampling for UV.
341 EXPECT_TRUE(VideoFrame::IsValidConfig(
342 PIXEL_FORMAT_I420
, VideoFrame::STORAGE_OWNED_MEMORY
, odd_size
,
343 gfx::Rect(odd_size
), odd_size
));
345 // Next try a format with no sub-sampling for UV.
346 EXPECT_TRUE(VideoFrame::IsValidConfig(
347 PIXEL_FORMAT_YV24
, VideoFrame::STORAGE_OWNED_MEMORY
, odd_size
,
348 gfx::Rect(odd_size
), odd_size
));
351 TEST(VideoFrame
, CreateFrame_OddWidth
) {
352 // Odd sizes are non-standard for YUV formats that subsample the UV, but they
353 // do exist in the wild and should be gracefully handled by VideoFrame in
354 // situations where VideoFrame allocates the YUV memory. See discussion in
355 // crrev.com/1240833003
356 const gfx::Size
odd_size(677, 288);
357 const base::TimeDelta kTimestamp
= base::TimeDelta();
359 // First create a frame that sub-samples UV.
360 scoped_refptr
<VideoFrame
> frame
= VideoFrame::CreateFrame(
361 PIXEL_FORMAT_I420
, odd_size
, gfx::Rect(odd_size
), odd_size
, kTimestamp
);
362 ASSERT_TRUE(frame
.get());
363 // I420 aligns UV to every 2 Y pixels. Hence, 677 should be rounded to 678
364 // which is the nearest value such that width % 2 == 0
365 EXPECT_EQ(678, frame
->coded_size().width());
367 // Next create a frame that does not sub-sample UV.
368 frame
= VideoFrame::CreateFrame(PIXEL_FORMAT_YV24
, odd_size
,
369 gfx::Rect(odd_size
), odd_size
, kTimestamp
);
370 ASSERT_TRUE(frame
.get());
371 // No sub-sampling for YV24 will mean odd width can remain odd since any pixel
372 // in the Y plane has a a corresponding pixel in the UV planes at the same
374 EXPECT_EQ(677, frame
->coded_size().width());
377 TEST(VideoFrameMetadata
, SetAndThenGetAllKeysForAllTypes
) {
378 VideoFrameMetadata metadata
;
380 for (int i
= 0; i
< VideoFrameMetadata::NUM_KEYS
; ++i
) {
381 const VideoFrameMetadata::Key key
= static_cast<VideoFrameMetadata::Key
>(i
);
383 EXPECT_FALSE(metadata
.HasKey(key
));
384 metadata
.SetBoolean(key
, true);
385 EXPECT_TRUE(metadata
.HasKey(key
));
386 bool bool_value
= false;
387 EXPECT_TRUE(metadata
.GetBoolean(key
, &bool_value
));
388 EXPECT_EQ(true, bool_value
);
391 EXPECT_FALSE(metadata
.HasKey(key
));
392 metadata
.SetInteger(key
, i
);
393 EXPECT_TRUE(metadata
.HasKey(key
));
394 int int_value
= -999;
395 EXPECT_TRUE(metadata
.GetInteger(key
, &int_value
));
396 EXPECT_EQ(i
, int_value
);
399 EXPECT_FALSE(metadata
.HasKey(key
));
400 metadata
.SetDouble(key
, 3.14 * i
);
401 EXPECT_TRUE(metadata
.HasKey(key
));
402 double double_value
= -999.99;
403 EXPECT_TRUE(metadata
.GetDouble(key
, &double_value
));
404 EXPECT_EQ(3.14 * i
, double_value
);
407 EXPECT_FALSE(metadata
.HasKey(key
));
408 metadata
.SetString(key
, base::StringPrintf("\xfe%d\xff", i
));
409 EXPECT_TRUE(metadata
.HasKey(key
));
410 std::string string_value
;
411 EXPECT_TRUE(metadata
.GetString(key
, &string_value
));
412 EXPECT_EQ(base::StringPrintf("\xfe%d\xff", i
), string_value
);
415 EXPECT_FALSE(metadata
.HasKey(key
));
416 metadata
.SetTimeDelta(key
, base::TimeDelta::FromInternalValue(42 + i
));
417 EXPECT_TRUE(metadata
.HasKey(key
));
418 base::TimeDelta delta_value
;
419 EXPECT_TRUE(metadata
.GetTimeDelta(key
, &delta_value
));
420 EXPECT_EQ(base::TimeDelta::FromInternalValue(42 + i
), delta_value
);
423 EXPECT_FALSE(metadata
.HasKey(key
));
424 metadata
.SetTimeTicks(key
, base::TimeTicks::FromInternalValue(~(0LL) + i
));
425 EXPECT_TRUE(metadata
.HasKey(key
));
426 base::TimeTicks ticks_value
;
427 EXPECT_TRUE(metadata
.GetTimeTicks(key
, &ticks_value
));
428 EXPECT_EQ(base::TimeTicks::FromInternalValue(~(0LL) + i
), ticks_value
);
431 EXPECT_FALSE(metadata
.HasKey(key
));
432 metadata
.SetValue(key
, base::Value::CreateNullValue());
433 EXPECT_TRUE(metadata
.HasKey(key
));
434 const base::Value
* const null_value
= metadata
.GetValue(key
);
435 EXPECT_TRUE(null_value
);
436 EXPECT_EQ(base::Value::TYPE_NULL
, null_value
->GetType());
441 TEST(VideoFrameMetadata
, PassMetadataViaIntermediary
) {
442 VideoFrameMetadata expected
;
443 for (int i
= 0; i
< VideoFrameMetadata::NUM_KEYS
; ++i
) {
444 const VideoFrameMetadata::Key key
= static_cast<VideoFrameMetadata::Key
>(i
);
445 expected
.SetInteger(key
, i
);
448 base::DictionaryValue tmp
;
449 expected
.MergeInternalValuesInto(&tmp
);
450 EXPECT_EQ(static_cast<size_t>(VideoFrameMetadata::NUM_KEYS
), tmp
.size());
452 VideoFrameMetadata result
;
453 result
.MergeInternalValuesFrom(tmp
);
455 for (int i
= 0; i
< VideoFrameMetadata::NUM_KEYS
; ++i
) {
456 const VideoFrameMetadata::Key key
= static_cast<VideoFrameMetadata::Key
>(i
);
458 EXPECT_TRUE(result
.GetInteger(key
, &value
));