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"
10 #include "base/callback_helpers.h"
11 #include "base/logging.h"
12 #include "base/memory/aligned_memory.h"
13 #include "base/strings/string_piece.h"
14 #include "gpu/command_buffer/common/mailbox_holder.h"
15 #include "media/base/limits.h"
16 #include "media/base/video_util.h"
18 #if !defined(MEDIA_FOR_CAST_IOS)
19 #include "third_party/skia/include/core/SkBitmap.h"
24 static inline size_t RoundUp(size_t value, size_t alignment) {
25 // Check that |alignment| is a power of 2.
26 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
27 return ((value + (alignment - 1)) & ~(alignment - 1));
30 // Rounds up |coded_size| if necessary for |format|.
31 static gfx::Size AdjustCodedSize(VideoFrame::Format format,
32 const gfx::Size& coded_size) {
33 gfx::Size new_coded_size(coded_size);
35 case VideoFrame::YV12:
36 case VideoFrame::YV12A:
37 case VideoFrame::I420:
38 case VideoFrame::YV12J:
39 new_coded_size.set_height(RoundUp(new_coded_size.height(), 2));
41 case VideoFrame::YV16:
42 new_coded_size.set_width(RoundUp(new_coded_size.width(), 2));
47 return new_coded_size;
51 scoped_refptr<VideoFrame> VideoFrame::CreateFrame(
52 VideoFrame::Format format,
53 const gfx::Size& coded_size,
54 const gfx::Rect& visible_rect,
55 const gfx::Size& natural_size,
56 base::TimeDelta timestamp) {
57 DCHECK(format != VideoFrame::UNKNOWN &&
58 format != VideoFrame::NV12 &&
59 format != VideoFrame::NATIVE_TEXTURE);
60 #if defined(VIDEO_HOLE)
61 DCHECK(format != VideoFrame::HOLE);
62 #endif // defined(VIDEO_HOLE)
64 // Since we're creating a new YUV frame (and allocating memory for it
65 // ourselves), we can pad the requested |coded_size| if necessary if the
66 // request does not line up on sample boundaries.
67 gfx::Size new_coded_size = AdjustCodedSize(format, coded_size);
68 DCHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size));
70 scoped_refptr<VideoFrame> frame(
71 new VideoFrame(format,
75 scoped_ptr<gpu::MailboxHolder>(),
83 std::string VideoFrame::FormatToString(VideoFrame::Format format) {
85 case VideoFrame::UNKNOWN:
87 case VideoFrame::YV12:
89 case VideoFrame::YV16:
91 case VideoFrame::I420:
93 case VideoFrame::NATIVE_TEXTURE:
94 return "NATIVE_TEXTURE";
95 #if defined(VIDEO_HOLE)
96 case VideoFrame::HOLE:
98 #endif // defined(VIDEO_HOLE)
99 case VideoFrame::YV12A:
101 case VideoFrame::YV12J:
103 case VideoFrame::NV12:
105 case VideoFrame::YV24:
108 NOTREACHED() << "Invalid videoframe format provided: " << format;
113 bool VideoFrame::IsValidConfig(VideoFrame::Format format,
114 const gfx::Size& coded_size,
115 const gfx::Rect& visible_rect,
116 const gfx::Size& natural_size) {
117 // Check maximum limits for all formats.
118 if (coded_size.GetArea() > limits::kMaxCanvas ||
119 coded_size.width() > limits::kMaxDimension ||
120 coded_size.height() > limits::kMaxDimension ||
121 visible_rect.x() < 0 || visible_rect.y() < 0 ||
122 visible_rect.right() > coded_size.width() ||
123 visible_rect.bottom() > coded_size.height() ||
124 natural_size.GetArea() > limits::kMaxCanvas ||
125 natural_size.width() > limits::kMaxDimension ||
126 natural_size.height() > limits::kMaxDimension)
129 // Check format-specific width/height requirements.
131 case VideoFrame::UNKNOWN:
132 return (coded_size.IsEmpty() && visible_rect.IsEmpty() &&
133 natural_size.IsEmpty());
134 case VideoFrame::YV24:
136 case VideoFrame::YV12:
137 case VideoFrame::YV12J:
138 case VideoFrame::I420:
139 case VideoFrame::YV12A:
140 case VideoFrame::NV12:
141 // Subsampled YUV formats have width/height requirements.
142 if (static_cast<size_t>(coded_size.height()) <
143 RoundUp(visible_rect.bottom(), 2))
146 case VideoFrame::YV16:
147 if (static_cast<size_t>(coded_size.width()) <
148 RoundUp(visible_rect.right(), 2))
151 case VideoFrame::NATIVE_TEXTURE:
152 #if defined(VIDEO_HOLE)
153 case VideoFrame::HOLE:
154 #endif // defined(VIDEO_HOLE)
155 // NATIVE_TEXTURE and HOLE have no software-allocated buffers and are
156 // allowed to skip the below check and be empty.
160 // Check that software-allocated buffer formats are not empty.
161 return (!coded_size.IsEmpty() && !visible_rect.IsEmpty() &&
162 !natural_size.IsEmpty());
166 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture(
167 scoped_ptr<gpu::MailboxHolder> mailbox_holder,
168 const ReleaseMailboxCB& mailbox_holder_release_cb,
169 const gfx::Size& coded_size,
170 const gfx::Rect& visible_rect,
171 const gfx::Size& natural_size,
172 base::TimeDelta timestamp,
173 const ReadPixelsCB& read_pixels_cb) {
174 scoped_refptr<VideoFrame> frame(new VideoFrame(NATIVE_TEXTURE,
178 mailbox_holder.Pass(),
181 frame->mailbox_holder_release_cb_ = mailbox_holder_release_cb;
182 frame->read_pixels_cb_ = read_pixels_cb;
187 #if !defined(MEDIA_FOR_CAST_IOS)
188 void VideoFrame::ReadPixelsFromNativeTexture(const SkBitmap& pixels) {
189 DCHECK_EQ(format_, NATIVE_TEXTURE);
190 if (!read_pixels_cb_.is_null())
191 read_pixels_cb_.Run(pixels);
196 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory(
198 const gfx::Size& coded_size,
199 const gfx::Rect& visible_rect,
200 const gfx::Size& natural_size,
203 base::SharedMemoryHandle handle,
204 base::TimeDelta timestamp,
205 const base::Closure& no_longer_needed_cb) {
206 gfx::Size new_coded_size = AdjustCodedSize(format, coded_size);
208 if (!IsValidConfig(format, new_coded_size, visible_rect, natural_size))
210 if (data_size < AllocationSize(format, new_coded_size))
214 case VideoFrame::I420: {
215 scoped_refptr<VideoFrame> frame(
216 new VideoFrame(format,
220 scoped_ptr<gpu::MailboxHolder>(),
223 frame->shared_memory_handle_ = handle;
224 frame->strides_[kYPlane] = new_coded_size.width();
225 frame->strides_[kUPlane] = new_coded_size.width() / 2;
226 frame->strides_[kVPlane] = new_coded_size.width() / 2;
227 frame->data_[kYPlane] = data;
228 frame->data_[kUPlane] = data + new_coded_size.GetArea();
229 frame->data_[kVPlane] = data + (new_coded_size.GetArea() * 5 / 4);
230 frame->no_longer_needed_cb_ = no_longer_needed_cb;
239 #if defined(OS_POSIX)
241 scoped_refptr<VideoFrame> VideoFrame::WrapExternalDmabufs(
243 const gfx::Size& coded_size,
244 const gfx::Rect& visible_rect,
245 const gfx::Size& natural_size,
246 const std::vector<int> dmabuf_fds,
247 base::TimeDelta timestamp,
248 const base::Closure& no_longer_needed_cb) {
249 if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
252 if (dmabuf_fds.size() != NumPlanes(format)) {
253 LOG(FATAL) << "Not enough dmabuf fds provided!";
257 scoped_refptr<VideoFrame> frame(
258 new VideoFrame(format,
262 scoped_ptr<gpu::MailboxHolder>(),
266 for (size_t i = 0; i < dmabuf_fds.size(); ++i) {
267 int duped_fd = HANDLE_EINTR(dup(dmabuf_fds[i]));
268 if (duped_fd == -1) {
269 // The already-duped in previous iterations fds will be closed when
270 // the partially-created frame drops out of scope here.
271 DLOG(ERROR) << "Failed duplicating a dmabuf fd";
275 frame->dmabuf_fds_[i].reset(duped_fd);
276 // Data is accessible only via fds.
277 frame->data_[i] = NULL;
278 frame->strides_[i] = 0;
281 frame->no_longer_needed_cb_ = no_longer_needed_cb;
286 #if defined(OS_MACOSX)
288 scoped_refptr<VideoFrame> VideoFrame::WrapCVPixelBuffer(
289 CVPixelBufferRef cv_pixel_buffer,
290 base::TimeDelta timestamp) {
291 DCHECK(cv_pixel_buffer);
292 DCHECK(CFGetTypeID(cv_pixel_buffer) == CVPixelBufferGetTypeID());
294 OSType cv_format = CVPixelBufferGetPixelFormatType(cv_pixel_buffer);
296 // There are very few compatible CV pixel formats, so just check each.
297 if (cv_format == kCVPixelFormatType_420YpCbCr8Planar) {
298 format = Format::I420;
299 } else if (cv_format == kCVPixelFormatType_444YpCbCr8) {
300 format = Format::YV24;
301 } else if (cv_format == '420v') {
302 // TODO(jfroy): Use kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange when the
303 // minimum OS X and iOS SDKs permits it.
304 format = Format::NV12;
306 DLOG(ERROR) << "CVPixelBuffer format not supported: " << cv_format;
310 gfx::Size coded_size(CVImageBufferGetEncodedSize(cv_pixel_buffer));
311 gfx::Rect visible_rect(CVImageBufferGetCleanRect(cv_pixel_buffer));
312 gfx::Size natural_size(CVImageBufferGetDisplaySize(cv_pixel_buffer));
314 if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
317 scoped_refptr<VideoFrame> frame(
318 new VideoFrame(format,
322 scoped_ptr<gpu::MailboxHolder>(),
326 frame->cv_pixel_buffer_.reset(cv_pixel_buffer, base::scoped_policy::RETAIN);
332 scoped_refptr<VideoFrame> VideoFrame::WrapExternalYuvData(
334 const gfx::Size& coded_size,
335 const gfx::Rect& visible_rect,
336 const gfx::Size& natural_size,
343 base::TimeDelta timestamp,
344 const base::Closure& no_longer_needed_cb) {
345 gfx::Size new_coded_size = AdjustCodedSize(format, coded_size);
346 CHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size));
348 scoped_refptr<VideoFrame> frame(
349 new VideoFrame(format,
353 scoped_ptr<gpu::MailboxHolder>(),
356 frame->strides_[kYPlane] = y_stride;
357 frame->strides_[kUPlane] = u_stride;
358 frame->strides_[kVPlane] = v_stride;
359 frame->data_[kYPlane] = y_data;
360 frame->data_[kUPlane] = u_data;
361 frame->data_[kVPlane] = v_data;
362 frame->no_longer_needed_cb_ = no_longer_needed_cb;
367 scoped_refptr<VideoFrame> VideoFrame::WrapVideoFrame(
368 const scoped_refptr<VideoFrame>& frame,
369 const gfx::Rect& visible_rect,
370 const gfx::Size& natural_size,
371 const base::Closure& no_longer_needed_cb) {
372 // NATIVE_TEXTURE frames need mailbox info propagated, and there's no support
373 // for that here yet, see http://crbug/362521.
374 CHECK_NE(frame->format(), NATIVE_TEXTURE);
376 DCHECK(frame->visible_rect().Contains(visible_rect));
377 scoped_refptr<VideoFrame> wrapped_frame(
378 new VideoFrame(frame->format(),
382 scoped_ptr<gpu::MailboxHolder>(),
384 frame->end_of_stream()));
386 for (size_t i = 0; i < NumPlanes(frame->format()); ++i) {
387 wrapped_frame->strides_[i] = frame->stride(i);
388 wrapped_frame->data_[i] = frame->data(i);
391 wrapped_frame->no_longer_needed_cb_ = no_longer_needed_cb;
392 return wrapped_frame;
396 scoped_refptr<VideoFrame> VideoFrame::CreateEOSFrame() {
397 return new VideoFrame(VideoFrame::UNKNOWN,
401 scoped_ptr<gpu::MailboxHolder>(),
407 scoped_refptr<VideoFrame> VideoFrame::CreateColorFrame(
408 const gfx::Size& size,
409 uint8 y, uint8 u, uint8 v,
410 base::TimeDelta timestamp) {
411 scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame(
412 VideoFrame::YV12, size, gfx::Rect(size), size, timestamp);
413 FillYUV(frame.get(), y, u, v);
418 scoped_refptr<VideoFrame> VideoFrame::CreateBlackFrame(const gfx::Size& size) {
419 const uint8 kBlackY = 0x00;
420 const uint8 kBlackUV = 0x80;
421 const base::TimeDelta kZero;
422 return CreateColorFrame(size, kBlackY, kBlackUV, kBlackUV, kZero);
426 scoped_refptr<VideoFrame> VideoFrame::CreateTransparentFrame(
427 const gfx::Size& size) {
428 const uint8 kBlackY = 0x00;
429 const uint8 kBlackUV = 0x00;
430 const uint8 kTransparentA = 0x00;
431 const base::TimeDelta kZero;
432 scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame(
433 VideoFrame::YV12A, size, gfx::Rect(size), size, kZero);
434 FillYUVA(frame.get(), kBlackY, kBlackUV, kBlackUV, kTransparentA);
438 #if defined(VIDEO_HOLE)
439 // This block and other blocks wrapped around #if defined(VIDEO_HOLE) is not
440 // maintained by the general compositor team. Please contact the following
443 // wonsik@chromium.org
444 // ycheo@chromium.org
447 scoped_refptr<VideoFrame> VideoFrame::CreateHoleFrame(
448 const gfx::Size& size) {
449 DCHECK(IsValidConfig(VideoFrame::HOLE, size, gfx::Rect(size), size));
450 scoped_refptr<VideoFrame> frame(
451 new VideoFrame(VideoFrame::HOLE,
455 scoped_ptr<gpu::MailboxHolder>(),
460 #endif // defined(VIDEO_HOLE)
463 size_t VideoFrame::NumPlanes(Format format) {
465 case VideoFrame::NATIVE_TEXTURE:
466 #if defined(VIDEO_HOLE)
467 case VideoFrame::HOLE:
468 #endif // defined(VIDEO_HOLE)
470 case VideoFrame::NV12:
472 case VideoFrame::YV12:
473 case VideoFrame::YV16:
474 case VideoFrame::I420:
475 case VideoFrame::YV12J:
476 case VideoFrame::YV24:
478 case VideoFrame::YV12A:
480 case VideoFrame::UNKNOWN:
483 NOTREACHED() << "Unsupported video frame format: " << format;
489 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) {
491 for (size_t i = 0; i < NumPlanes(format); ++i)
492 total += PlaneAllocationSize(format, i, coded_size);
497 gfx::Size VideoFrame::PlaneSize(Format format,
499 const gfx::Size& coded_size) {
500 // Align to multiple-of-two size overall. This ensures that non-subsampled
501 // planes can be addressed by pixel with the same scaling as the subsampled
503 const int width = RoundUp(coded_size.width(), 2);
504 const int height = RoundUp(coded_size.height(), 2);
506 case VideoFrame::YV24:
508 case VideoFrame::kYPlane:
509 case VideoFrame::kUPlane:
510 case VideoFrame::kVPlane:
511 return gfx::Size(width, height);
516 case VideoFrame::YV12:
517 case VideoFrame::YV12J:
518 case VideoFrame::I420:
520 case VideoFrame::kYPlane:
521 return gfx::Size(width, height);
522 case VideoFrame::kUPlane:
523 case VideoFrame::kVPlane:
524 return gfx::Size(width / 2, height / 2);
529 case VideoFrame::YV12A:
531 case VideoFrame::kYPlane:
532 case VideoFrame::kAPlane:
533 return gfx::Size(width, height);
534 case VideoFrame::kUPlane:
535 case VideoFrame::kVPlane:
536 return gfx::Size(width / 2, height / 2);
541 case VideoFrame::YV16:
543 case VideoFrame::kYPlane:
544 return gfx::Size(width, height);
545 case VideoFrame::kUPlane:
546 case VideoFrame::kVPlane:
547 return gfx::Size(width / 2, height);
552 case VideoFrame::NV12:
554 case VideoFrame::kYPlane:
555 return gfx::Size(width, height);
556 case VideoFrame::kUVPlane:
557 return gfx::Size(width, height / 2);
562 case VideoFrame::UNKNOWN:
563 case VideoFrame::NATIVE_TEXTURE:
564 #if defined(VIDEO_HOLE)
565 case VideoFrame::HOLE:
566 #endif // defined(VIDEO_HOLE)
569 NOTREACHED() << "Unsupported video frame format/plane: "
570 << format << "/" << plane;
574 size_t VideoFrame::PlaneAllocationSize(Format format,
576 const gfx::Size& coded_size) {
577 // VideoFrame formats are (so far) all YUV and 1 byte per sample.
578 return PlaneSize(format, plane, coded_size).GetArea();
582 int VideoFrame::PlaneHorizontalBitsPerPixel(Format format, size_t plane) {
584 case VideoFrame::YV24:
594 case VideoFrame::YV12:
595 case VideoFrame::YV16:
596 case VideoFrame::I420:
597 case VideoFrame::YV12J:
608 case VideoFrame::YV12A:
620 case VideoFrame::NV12:
630 case VideoFrame::UNKNOWN:
631 #if defined(VIDEO_HOLE)
632 case VideoFrame::HOLE:
633 #endif // defined(VIDEO_HOLE)
634 case VideoFrame::NATIVE_TEXTURE:
637 NOTREACHED() << "Unsupported video frame format/plane: "
638 << format << "/" << plane;
642 // Release data allocated by AllocateYUV().
643 static void ReleaseData(uint8* data) {
645 base::AlignedFree(data);
648 void VideoFrame::AllocateYUV() {
649 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 ||
650 format_ == VideoFrame::YV12A || format_ == VideoFrame::I420 ||
651 format_ == VideoFrame::YV12J || format_ == VideoFrame::YV24);
652 // Align Y rows at least at 16 byte boundaries. The stride for both
653 // YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for
654 // U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in
655 // the case of YV12 the strides are identical for the same width surface, but
656 // the number of bytes allocated for YV12 is 1/2 the amount for U & V as
657 // YV16. We also round the height of the surface allocated to be an even
658 // number to avoid any potential of faulting by code that attempts to access
659 // the Y values of the final row, but assumes that the last row of U & V
660 // applies to a full two rows of Y. YV12A is the same as YV12, but with an
661 // additional alpha plane that has the same size and alignment as the Y plane.
662 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane),
663 kFrameSizeAlignment);
664 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane),
665 kFrameSizeAlignment);
667 // The *2 here is because some formats (e.g. h264) allow interlaced coding,
668 // and then the size needs to be a multiple of two macroblocks (vertically).
669 // See libavcodec/utils.c:avcodec_align_dimensions2().
670 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2);
672 (format_ == VideoFrame::YV12 || format_ == VideoFrame::YV12A ||
673 format_ == VideoFrame::I420)
676 size_t y_bytes = y_height * y_stride;
677 size_t uv_bytes = uv_height * uv_stride;
678 size_t a_bytes = format_ == VideoFrame::YV12A ? y_bytes : 0;
680 // The extra line of UV being allocated is because h264 chroma MC
681 // overreads by one line in some cases, see libavcodec/utils.c:
682 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm:
683 // put_h264_chroma_mc4_ssse3().
684 const size_t data_size =
685 y_bytes + (uv_bytes * 2 + uv_stride) + a_bytes + kFrameSizePadding;
686 uint8* data = reinterpret_cast<uint8*>(
687 base::AlignedAlloc(data_size, kFrameAddressAlignment));
688 // FFmpeg expects the initialize allocation to be zero-initialized. Failure
689 // to do so can lead to unitialized value usage. See http://crbug.com/390941
690 memset(data, 0, data_size);
691 no_longer_needed_cb_ = base::Bind(&ReleaseData, data);
692 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0);
693 data_[VideoFrame::kYPlane] = data;
694 data_[VideoFrame::kUPlane] = data + y_bytes;
695 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes;
696 strides_[VideoFrame::kYPlane] = y_stride;
697 strides_[VideoFrame::kUPlane] = uv_stride;
698 strides_[VideoFrame::kVPlane] = uv_stride;
699 if (format_ == YV12A) {
700 data_[VideoFrame::kAPlane] = data + y_bytes + (2 * uv_bytes);
701 strides_[VideoFrame::kAPlane] = y_stride;
705 VideoFrame::VideoFrame(VideoFrame::Format format,
706 const gfx::Size& coded_size,
707 const gfx::Rect& visible_rect,
708 const gfx::Size& natural_size,
709 scoped_ptr<gpu::MailboxHolder> mailbox_holder,
710 base::TimeDelta timestamp,
713 coded_size_(coded_size),
714 visible_rect_(visible_rect),
715 natural_size_(natural_size),
716 mailbox_holder_(mailbox_holder.Pass()),
717 shared_memory_handle_(base::SharedMemory::NULLHandle()),
718 timestamp_(timestamp),
719 release_sync_point_(0),
720 end_of_stream_(end_of_stream) {
721 DCHECK(IsValidConfig(format_, coded_size_, visible_rect_, natural_size_));
723 memset(&strides_, 0, sizeof(strides_));
724 memset(&data_, 0, sizeof(data_));
727 VideoFrame::~VideoFrame() {
728 if (!mailbox_holder_release_cb_.is_null()) {
729 uint32 release_sync_point;
731 // To ensure that changes to |release_sync_point_| are visible on this
732 // thread (imply a memory barrier).
733 base::AutoLock locker(release_sync_point_lock_);
734 release_sync_point = release_sync_point_;
736 base::ResetAndReturn(&mailbox_holder_release_cb_).Run(release_sync_point);
738 if (!no_longer_needed_cb_.is_null())
739 base::ResetAndReturn(&no_longer_needed_cb_).Run();
743 bool VideoFrame::IsValidPlane(size_t plane, VideoFrame::Format format) {
744 return (plane < NumPlanes(format));
747 int VideoFrame::stride(size_t plane) const {
748 DCHECK(IsValidPlane(plane, format_));
749 return strides_[plane];
753 size_t VideoFrame::RowBytes(size_t plane, VideoFrame::Format format,
755 DCHECK(IsValidPlane(plane, format));
757 case VideoFrame::YV24:
767 case VideoFrame::YV12:
768 case VideoFrame::YV16:
769 case VideoFrame::I420:
770 case VideoFrame::YV12J:
776 return RoundUp(width, 2) / 2;
781 case VideoFrame::YV12A:
788 return RoundUp(width, 2) / 2;
793 case VideoFrame::NV12:
802 case VideoFrame::UNKNOWN:
803 #if defined(VIDEO_HOLE)
804 case VideoFrame::HOLE:
805 #endif // defined(VIDEO_HOLE)
806 case VideoFrame::NATIVE_TEXTURE:
809 NOTREACHED() << "Unsupported video frame format/plane: " << format << "/"
814 int VideoFrame::row_bytes(size_t plane) const {
815 return RowBytes(plane, format_, coded_size_.width());
819 size_t VideoFrame::Rows(size_t plane, VideoFrame::Format format, int height) {
820 DCHECK(IsValidPlane(plane, format));
822 case VideoFrame::YV24:
823 case VideoFrame::YV16:
833 case VideoFrame::YV12:
834 case VideoFrame::YV12J:
835 case VideoFrame::I420:
841 return RoundUp(height, 2) / 2;
846 case VideoFrame::YV12A:
853 return RoundUp(height, 2) / 2;
858 case VideoFrame::NV12:
863 return RoundUp(height, 2) / 2;
868 case VideoFrame::UNKNOWN:
869 #if defined(VIDEO_HOLE)
870 case VideoFrame::HOLE:
871 #endif // defined(VIDEO_HOLE)
872 case VideoFrame::NATIVE_TEXTURE:
875 NOTREACHED() << "Unsupported video frame format/plane: " << format << "/"
880 int VideoFrame::rows(size_t plane) const {
881 return Rows(plane, format_, coded_size_.height());
884 uint8* VideoFrame::data(size_t plane) const {
885 DCHECK(IsValidPlane(plane, format_));
889 const gpu::MailboxHolder* VideoFrame::mailbox_holder() const {
890 DCHECK_EQ(format_, NATIVE_TEXTURE);
891 return mailbox_holder_.get();
894 base::SharedMemoryHandle VideoFrame::shared_memory_handle() const {
895 return shared_memory_handle_;
898 void VideoFrame::UpdateReleaseSyncPoint(SyncPointClient* client) {
899 DCHECK_EQ(format_, NATIVE_TEXTURE);
900 base::AutoLock locker(release_sync_point_lock_);
901 // Must wait on the previous sync point before inserting a new sync point so
902 // that |mailbox_holder_release_cb_| guarantees the previous sync point
903 // occurred when it waits on |release_sync_point_|.
904 if (release_sync_point_)
905 client->WaitSyncPoint(release_sync_point_);
906 release_sync_point_ = client->InsertSyncPoint();
909 #if defined(OS_POSIX)
910 int VideoFrame::dmabuf_fd(size_t plane) const {
911 return dmabuf_fds_[plane].get();
915 #if defined(OS_MACOSX)
916 CVPixelBufferRef VideoFrame::cv_pixel_buffer() const {
917 return cv_pixel_buffer_.get();
921 void VideoFrame::HashFrameForTesting(base::MD5Context* context) {
922 for (int plane = 0; plane < kMaxPlanes; ++plane) {
923 if (!IsValidPlane(plane, format_))
925 for (int row = 0; row < rows(plane); ++row) {
926 base::MD5Update(context, base::StringPiece(
927 reinterpret_cast<char*>(data(plane) + stride(plane) * row),