1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/video_frame.h"
#include <algorithm>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/aligned_memory.h"
#include "base/strings/string_piece.h"
#include "gpu/command_buffer/common/mailbox_holder.h"
#include "media/base/limits.h"
#include "media/base/video_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace media {
static inline size_t RoundUp(size_t value, size_t alignment) {
// Check that |alignment| is a power of 2.
DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
return ((value + (alignment - 1)) & ~(alignment - 1));
}
// static
scoped_refptr<VideoFrame> VideoFrame::CreateFrame(
VideoFrame::Format format,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
base::TimeDelta timestamp) {
// Since we're creating a new YUV frame (and allocating memory for it
// ourselves), we can pad the requested |coded_size| if necessary if the
// request does not line up on sample boundaries.
gfx::Size new_coded_size(coded_size);
switch (format) {
case VideoFrame::YV12:
case VideoFrame::YV12A:
case VideoFrame::I420:
case VideoFrame::YV12J:
new_coded_size.set_height((new_coded_size.height() + 1) / 2 * 2);
// Fallthrough.
case VideoFrame::YV16:
new_coded_size.set_width((new_coded_size.width() + 1) / 2 * 2);
break;
default:
LOG(FATAL) << "Only YUV formats supported: " << format;
return NULL;
}
DCHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size));
scoped_refptr<VideoFrame> frame(
new VideoFrame(format,
new_coded_size,
visible_rect,
natural_size,
scoped_ptr<gpu::MailboxHolder>(),
timestamp,
false));
frame->AllocateYUV();
return frame;
}
// static
std::string VideoFrame::FormatToString(VideoFrame::Format format) {
switch (format) {
case VideoFrame::UNKNOWN:
return "UNKNOWN";
case VideoFrame::YV12:
return "YV12";
case VideoFrame::YV16:
return "YV16";
case VideoFrame::I420:
return "I420";
case VideoFrame::NATIVE_TEXTURE:
return "NATIVE_TEXTURE";
#if defined(VIDEO_HOLE)
case VideoFrame::HOLE:
return "HOLE";
#endif // defined(VIDEO_HOLE)
case VideoFrame::YV12A:
return "YV12A";
case VideoFrame::YV12J:
return "YV12J";
case VideoFrame::NV12:
return "NV12";
}
NOTREACHED() << "Invalid videoframe format provided: " << format;
return "";
}
// static
bool VideoFrame::IsValidConfig(VideoFrame::Format format,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size) {
// Check maximum limits for all formats.
if (coded_size.GetArea() > limits::kMaxCanvas ||
coded_size.width() > limits::kMaxDimension ||
coded_size.height() > limits::kMaxDimension ||
visible_rect.x() < 0 || visible_rect.y() < 0 ||
visible_rect.right() > coded_size.width() ||
visible_rect.bottom() > coded_size.height() ||
natural_size.GetArea() > limits::kMaxCanvas ||
natural_size.width() > limits::kMaxDimension ||
natural_size.height() > limits::kMaxDimension)
return false;
// Check format-specific width/height requirements.
switch (format) {
case VideoFrame::UNKNOWN:
return (coded_size.IsEmpty() && visible_rect.IsEmpty() &&
natural_size.IsEmpty());
case VideoFrame::YV12:
case VideoFrame::YV12J:
case VideoFrame::I420:
case VideoFrame::YV12A:
case VideoFrame::NV12:
// YUV formats have width/height requirements due to chroma subsampling.
if (static_cast<size_t>(coded_size.height()) <
RoundUp(visible_rect.bottom(), 2))
return false;
// Fallthrough.
case VideoFrame::YV16:
if (static_cast<size_t>(coded_size.width()) <
RoundUp(visible_rect.right(), 2))
return false;
break;
case VideoFrame::NATIVE_TEXTURE:
#if defined(VIDEO_HOLE)
case VideoFrame::HOLE:
#endif // defined(VIDEO_HOLE)
// NATIVE_TEXTURE and HOLE have no software-allocated buffers and are
// allowed to skip the below check and be empty.
return true;
}
// Check that software-allocated buffer formats are not empty.
return (!coded_size.IsEmpty() && !visible_rect.IsEmpty() &&
!natural_size.IsEmpty());
}
// static
scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture(
scoped_ptr<gpu::MailboxHolder> mailbox_holder,
const ReleaseMailboxCB& mailbox_holder_release_cb,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
base::TimeDelta timestamp,
const ReadPixelsCB& read_pixels_cb) {
scoped_refptr<VideoFrame> frame(new VideoFrame(NATIVE_TEXTURE,
coded_size,
visible_rect,
natural_size,
mailbox_holder.Pass(),
timestamp,
false));
frame->mailbox_holder_release_cb_ = mailbox_holder_release_cb;
frame->read_pixels_cb_ = read_pixels_cb;
return frame;
}
void VideoFrame::ReadPixelsFromNativeTexture(const SkBitmap& pixels) {
DCHECK_EQ(format_, NATIVE_TEXTURE);
if (!read_pixels_cb_.is_null())
read_pixels_cb_.Run(pixels);
}
// static
scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory(
Format format,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
uint8* data,
size_t data_size,
base::SharedMemoryHandle handle,
base::TimeDelta timestamp,
const base::Closure& no_longer_needed_cb) {
if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
return NULL;
if (data_size < AllocationSize(format, coded_size))
return NULL;
switch (format) {
case I420: {
scoped_refptr<VideoFrame> frame(
new VideoFrame(format,
coded_size,
visible_rect,
natural_size,
scoped_ptr<gpu::MailboxHolder>(),
timestamp,
false));
frame->shared_memory_handle_ = handle;
frame->strides_[kYPlane] = coded_size.width();
frame->strides_[kUPlane] = coded_size.width() / 2;
frame->strides_[kVPlane] = coded_size.width() / 2;
frame->data_[kYPlane] = data;
frame->data_[kUPlane] = data + coded_size.GetArea();
frame->data_[kVPlane] = data + (coded_size.GetArea() * 5 / 4);
frame->no_longer_needed_cb_ = no_longer_needed_cb;
return frame;
}
default:
NOTIMPLEMENTED();
return NULL;
}
}
#if defined(OS_POSIX)
// static
scoped_refptr<VideoFrame> VideoFrame::WrapExternalDmabufs(
Format format,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
const std::vector<int> dmabuf_fds,
base::TimeDelta timestamp,
const base::Closure& no_longer_needed_cb) {
if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
return NULL;
if (dmabuf_fds.size() != NumPlanes(format)) {
LOG(FATAL) << "Not enough dmabuf fds provided!";
return NULL;
}
scoped_refptr<VideoFrame> frame(
new VideoFrame(format,
coded_size,
visible_rect,
natural_size,
scoped_ptr<gpu::MailboxHolder>(),
timestamp,
false));
for (size_t i = 0; i < dmabuf_fds.size(); ++i) {
int duped_fd = HANDLE_EINTR(dup(dmabuf_fds[i]));
if (duped_fd == -1) {
// The already-duped in previous iterations fds will be closed when
// the partially-created frame drops out of scope here.
DLOG(ERROR) << "Failed duplicating a dmabuf fd";
return NULL;
}
frame->dmabuf_fds_[i].reset(duped_fd);
// Data is accessible only via fds.
frame->data_[i] = NULL;
frame->strides_[i] = 0;
}
frame->no_longer_needed_cb_ = no_longer_needed_cb;
return frame;
}
#endif
// static
scoped_refptr<VideoFrame> VideoFrame::WrapExternalYuvData(
Format format,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
int32 y_stride,
int32 u_stride,
int32 v_stride,
uint8* y_data,
uint8* u_data,
uint8* v_data,
base::TimeDelta timestamp,
const base::Closure& no_longer_needed_cb) {
if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
return NULL;
scoped_refptr<VideoFrame> frame(
new VideoFrame(format,
coded_size,
visible_rect,
natural_size,
scoped_ptr<gpu::MailboxHolder>(),
timestamp,
false));
frame->strides_[kYPlane] = y_stride;
frame->strides_[kUPlane] = u_stride;
frame->strides_[kVPlane] = v_stride;
frame->data_[kYPlane] = y_data;
frame->data_[kUPlane] = u_data;
frame->data_[kVPlane] = v_data;
frame->no_longer_needed_cb_ = no_longer_needed_cb;
return frame;
}
// static
scoped_refptr<VideoFrame> VideoFrame::WrapVideoFrame(
const scoped_refptr<VideoFrame>& frame,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
const base::Closure& no_longer_needed_cb) {
// NATIVE_TEXTURE frames need mailbox info propagated, and there's no support
// for that here yet, see http://crbug/362521.
CHECK(frame->format() != NATIVE_TEXTURE);
DCHECK(frame->visible_rect().Contains(visible_rect));
scoped_refptr<VideoFrame> wrapped_frame(
new VideoFrame(frame->format(),
frame->coded_size(),
visible_rect,
natural_size,
scoped_ptr<gpu::MailboxHolder>(),
frame->timestamp(),
frame->end_of_stream()));
for (size_t i = 0; i < NumPlanes(frame->format()); ++i) {
wrapped_frame->strides_[i] = frame->stride(i);
wrapped_frame->data_[i] = frame->data(i);
}
wrapped_frame->no_longer_needed_cb_ = no_longer_needed_cb;
return wrapped_frame;
}
// static
scoped_refptr<VideoFrame> VideoFrame::CreateEOSFrame() {
return new VideoFrame(VideoFrame::UNKNOWN,
gfx::Size(),
gfx::Rect(),
gfx::Size(),
scoped_ptr<gpu::MailboxHolder>(),
kNoTimestamp(),
true);
}
// static
scoped_refptr<VideoFrame> VideoFrame::CreateColorFrame(
const gfx::Size& size,
uint8 y, uint8 u, uint8 v,
base::TimeDelta timestamp) {
scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame(
VideoFrame::YV12, size, gfx::Rect(size), size, timestamp);
FillYUV(frame.get(), y, u, v);
return frame;
}
// static
scoped_refptr<VideoFrame> VideoFrame::CreateBlackFrame(const gfx::Size& size) {
const uint8 kBlackY = 0x00;
const uint8 kBlackUV = 0x80;
const base::TimeDelta kZero;
return CreateColorFrame(size, kBlackY, kBlackUV, kBlackUV, kZero);
}
#if defined(VIDEO_HOLE)
// This block and other blocks wrapped around #if defined(VIDEO_HOLE) is not
// maintained by the general compositor team. Please contact the following
// people instead:
//
// wonsik@chromium.org
// ycheo@chromium.org
// static
scoped_refptr<VideoFrame> VideoFrame::CreateHoleFrame(
const gfx::Size& size) {
DCHECK(IsValidConfig(VideoFrame::HOLE, size, gfx::Rect(size), size));
scoped_refptr<VideoFrame> frame(
new VideoFrame(VideoFrame::HOLE,
size,
gfx::Rect(size),
size,
scoped_ptr<gpu::MailboxHolder>(),
base::TimeDelta(),
false));
return frame;
}
#endif // defined(VIDEO_HOLE)
// static
size_t VideoFrame::NumPlanes(Format format) {
switch (format) {
case VideoFrame::NATIVE_TEXTURE:
#if defined(VIDEO_HOLE)
case VideoFrame::HOLE:
#endif // defined(VIDEO_HOLE)
return 0;
case VideoFrame::NV12:
return 2;
case VideoFrame::YV12:
case VideoFrame::YV16:
case VideoFrame::I420:
case VideoFrame::YV12J:
return 3;
case VideoFrame::YV12A:
return 4;
case VideoFrame::UNKNOWN:
break;
}
NOTREACHED() << "Unsupported video frame format: " << format;
return 0;
}
// static
size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) {
size_t total = 0;
for (size_t i = 0; i < NumPlanes(format); ++i)
total += PlaneAllocationSize(format, i, coded_size);
return total;
}
// static
gfx::Size VideoFrame::PlaneSize(Format format,
size_t plane,
const gfx::Size& coded_size) {
const int width = RoundUp(coded_size.width(), 2);
const int height = RoundUp(coded_size.height(), 2);
switch (format) {
case VideoFrame::YV12:
case VideoFrame::YV12J:
case VideoFrame::I420: {
switch (plane) {
case VideoFrame::kYPlane:
return gfx::Size(width, height);
case VideoFrame::kUPlane:
case VideoFrame::kVPlane:
return gfx::Size(width / 2, height / 2);
default:
break;
}
}
case VideoFrame::YV12A: {
switch (plane) {
case VideoFrame::kYPlane:
case VideoFrame::kAPlane:
return gfx::Size(width, height);
case VideoFrame::kUPlane:
case VideoFrame::kVPlane:
return gfx::Size(width / 2, height / 2);
default:
break;
}
}
case VideoFrame::YV16: {
switch (plane) {
case VideoFrame::kYPlane:
return gfx::Size(width, height);
case VideoFrame::kUPlane:
case VideoFrame::kVPlane:
return gfx::Size(width / 2, height);
default:
break;
}
}
case VideoFrame::NV12: {
switch (plane) {
case VideoFrame::kYPlane:
return gfx::Size(width, height);
case VideoFrame::kUVPlane:
return gfx::Size(width, height / 2);
default:
break;
}
}
case VideoFrame::UNKNOWN:
case VideoFrame::NATIVE_TEXTURE:
#if defined(VIDEO_HOLE)
case VideoFrame::HOLE:
#endif // defined(VIDEO_HOLE)
break;
}
NOTREACHED() << "Unsupported video frame format/plane: "
<< format << "/" << plane;
return gfx::Size();
}
size_t VideoFrame::PlaneAllocationSize(Format format,
size_t plane,
const gfx::Size& coded_size) {
// VideoFrame formats are (so far) all YUV and 1 byte per sample.
return PlaneSize(format, plane, coded_size).GetArea();
}
// static
int VideoFrame::PlaneHorizontalBitsPerPixel(Format format, size_t plane) {
switch (format) {
case VideoFrame::YV12A:
if (plane == kAPlane)
return 8;
// fallthrough
case VideoFrame::YV12:
case VideoFrame::YV16:
case VideoFrame::I420:
case VideoFrame::YV12J: {
switch (plane) {
case kYPlane:
return 8;
case kUPlane:
case kVPlane:
return 2;
default:
break;
}
}
case VideoFrame::NV12: {
switch (plane) {
case kYPlane:
return 8;
case kUVPlane:
return 4;
default:
break;
}
}
default:
break;
}
NOTREACHED() << "Unsupported video frame format: " << format;
return 0;
}
// Release data allocated by AllocateYUV().
static void ReleaseData(uint8* data) {
DCHECK(data);
base::AlignedFree(data);
}
void VideoFrame::AllocateYUV() {
DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 ||
format_ == VideoFrame::YV12A || format_ == VideoFrame::I420 ||
format_ == VideoFrame::YV12J);
// Align Y rows at least at 16 byte boundaries. The stride for both
// YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for
// U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in
// the case of YV12 the strides are identical for the same width surface, but
// the number of bytes allocated for YV12 is 1/2 the amount for U & V as
// YV16. We also round the height of the surface allocated to be an even
// number to avoid any potential of faulting by code that attempts to access
// the Y values of the final row, but assumes that the last row of U & V
// applies to a full two rows of Y. YV12A is the same as YV12, but with an
// additional alpha plane that has the same size and alignment as the Y plane.
size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane),
kFrameSizeAlignment);
size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane),
kFrameSizeAlignment);
// The *2 here is because some formats (e.g. h264) allow interlaced coding,
// and then the size needs to be a multiple of two macroblocks (vertically).
// See libavcodec/utils.c:avcodec_align_dimensions2().
size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2);
size_t uv_height =
(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV12A ||
format_ == VideoFrame::I420)
? y_height / 2
: y_height;
size_t y_bytes = y_height * y_stride;
size_t uv_bytes = uv_height * uv_stride;
size_t a_bytes = format_ == VideoFrame::YV12A ? y_bytes : 0;
// The extra line of UV being allocated is because h264 chroma MC
// overreads by one line in some cases, see libavcodec/utils.c:
// avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm:
// put_h264_chroma_mc4_ssse3().
uint8* data = reinterpret_cast<uint8*>(
base::AlignedAlloc(
y_bytes + (uv_bytes * 2 + uv_stride) + a_bytes + kFrameSizePadding,
kFrameAddressAlignment));
no_longer_needed_cb_ = base::Bind(&ReleaseData, data);
COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0);
data_[VideoFrame::kYPlane] = data;
data_[VideoFrame::kUPlane] = data + y_bytes;
data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes;
strides_[VideoFrame::kYPlane] = y_stride;
strides_[VideoFrame::kUPlane] = uv_stride;
strides_[VideoFrame::kVPlane] = uv_stride;
if (format_ == YV12A) {
data_[VideoFrame::kAPlane] = data + y_bytes + (2 * uv_bytes);
strides_[VideoFrame::kAPlane] = y_stride;
}
}
VideoFrame::VideoFrame(VideoFrame::Format format,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
scoped_ptr<gpu::MailboxHolder> mailbox_holder,
base::TimeDelta timestamp,
bool end_of_stream)
: format_(format),
coded_size_(coded_size),
visible_rect_(visible_rect),
natural_size_(natural_size),
mailbox_holder_(mailbox_holder.Pass()),
shared_memory_handle_(base::SharedMemory::NULLHandle()),
timestamp_(timestamp),
end_of_stream_(end_of_stream) {
DCHECK(IsValidConfig(format_, coded_size_, visible_rect_, natural_size_));
memset(&strides_, 0, sizeof(strides_));
memset(&data_, 0, sizeof(data_));
}
VideoFrame::~VideoFrame() {
if (!mailbox_holder_release_cb_.is_null()) {
std::vector<uint32> release_sync_points;
{
base::AutoLock locker(release_sync_point_lock_);
release_sync_points_.swap(release_sync_points);
}
base::ResetAndReturn(&mailbox_holder_release_cb_).Run(release_sync_points);
}
if (!no_longer_needed_cb_.is_null())
base::ResetAndReturn(&no_longer_needed_cb_).Run();
}
bool VideoFrame::IsValidPlane(size_t plane) const {
return (plane < NumPlanes(format_));
}
int VideoFrame::stride(size_t plane) const {
DCHECK(IsValidPlane(plane));
return strides_[plane];
}
int VideoFrame::row_bytes(size_t plane) const {
DCHECK(IsValidPlane(plane));
int width = coded_size_.width();
switch (format_) {
// Planar, 8bpp.
case YV12A:
if (plane == kAPlane)
return width;
// Fallthrough.
case YV12:
case YV16:
case I420:
case YV12J:
if (plane == kYPlane)
return width;
else if (plane <= kVPlane)
return RoundUp(width, 2) / 2;
break;
case NV12:
if (plane <= kUVPlane)
return width;
break;
default:
break;
}
// Intentionally leave out non-production formats.
NOTREACHED() << "Unsupported video frame format: " << format_;
return 0;
}
int VideoFrame::rows(size_t plane) const {
DCHECK(IsValidPlane(plane));
int height = coded_size_.height();
switch (format_) {
case YV16:
return height;
case YV12A:
if (plane == kAPlane)
return height;
// Fallthrough.
case YV12:
case YV12J:
case I420:
if (plane == kYPlane)
return height;
else if (plane <= kVPlane)
return RoundUp(height, 2) / 2;
break;
case NV12:
if (plane == kYPlane)
return height;
else if (plane == kUVPlane)
return RoundUp(height, 2) / 2;
break;
default:
break;
}
// Intentionally leave out non-production formats.
NOTREACHED() << "Unsupported video frame format: " << format_;
return 0;
}
uint8* VideoFrame::data(size_t plane) const {
DCHECK(IsValidPlane(plane));
return data_[plane];
}
const gpu::MailboxHolder* VideoFrame::mailbox_holder() const {
DCHECK_EQ(format_, NATIVE_TEXTURE);
return mailbox_holder_.get();
}
base::SharedMemoryHandle VideoFrame::shared_memory_handle() const {
return shared_memory_handle_;
}
void VideoFrame::AppendReleaseSyncPoint(uint32 sync_point) {
DCHECK_EQ(format_, NATIVE_TEXTURE);
if (!sync_point)
return;
base::AutoLock locker(release_sync_point_lock_);
release_sync_points_.push_back(sync_point);
}
#if defined(OS_POSIX)
int VideoFrame::dmabuf_fd(size_t plane) const {
return dmabuf_fds_[plane].get();
}
#endif
void VideoFrame::HashFrameForTesting(base::MD5Context* context) {
for (int plane = 0; plane < kMaxPlanes; ++plane) {
if (!IsValidPlane(plane))
break;
for (int row = 0; row < rows(plane); ++row) {
base::MD5Update(context, base::StringPiece(
reinterpret_cast<char*>(data(plane) + stride(plane) * row),
row_bytes(plane)));
}
}
}
} // namespace media
|