Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/mojo/common/mojo_video_frame.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 | |
| 10 namespace media { | |
| 11 | |
| 12 // static | |
| 13 scoped_refptr<MojoSharedBufferVideoFrame> | |
| 14 MojoSharedBufferVideoFrame::CreateDefaultI420(const gfx::Size& dimensions, | |
| 15 base::TimeDelta timestamp) { | |
| 16 const VideoPixelFormat format = PIXEL_FORMAT_I420; | |
| 17 const gfx::Rect visible_rect(dimensions); | |
| 18 | |
| 19 // Since we're allocating memory for the new frame, pad the requested | |
| 20 // size if necessary so that the requested size does line up on sample | |
| 21 // boundaries. See related discussion in VideoFrame::CreateFrameInternal(). | |
| 22 const gfx::Size coded_size = DetermineAlignedSize(format, dimensions); | |
| 23 if (!IsValidConfig(format, STORAGE_MOJO_SHARED_BUFFER, coded_size, | |
| 24 visible_rect, dimensions)) { | |
| 25 LOG(DFATAL) << __FUNCTION__ << " Invalid config. " | |
| 26 << ConfigToString(format, STORAGE_MOJO_SHARED_BUFFER, | |
| 27 dimensions, visible_rect, dimensions); | |
| 28 return nullptr; | |
| 29 } | |
| 30 | |
| 31 // Allocate a shared memory buffer big enough to hold the desired frame. | |
| 32 const size_t requested_size = VideoFrame::AllocationSize(format, coded_size); | |
| 33 mojo::ScopedSharedBufferHandle handle; | |
| 34 const MojoResult result = | |
| 35 mojo::CreateSharedBuffer(nullptr, requested_size, &handle); | |
| 36 if (result != MOJO_RESULT_OK) | |
| 37 return nullptr; | |
| 38 | |
| 39 // Create and initialize the frame. As this is I420 format, the U and V | |
| 40 // planes have samples for each 2x2 block. The memory is laid out as follows: | |
| 41 // - Yplane, full size (each element represents a 1x1 block) | |
| 42 // - Uplane, quarter size (each element represents a 2x2 block) | |
| 43 // - Vplane, quarter size (each element represents a 2x2 block) | |
| 44 DCHECK((coded_size.width() % 2 == 0) && (coded_size.height() % 2 == 0)); | |
| 45 return Create(format, coded_size, visible_rect, dimensions, std::move(handle), | |
| 46 requested_size, 0 /* y_offset */, coded_size.GetArea(), | |
| 47 coded_size.GetArea() * 5 / 4, coded_size.width(), | |
| 48 coded_size.width() / 2, coded_size.width() / 2, timestamp); | |
| 49 } | |
| 50 | |
| 51 // static | |
| 52 scoped_refptr<MojoSharedBufferVideoFrame> MojoSharedBufferVideoFrame::Create( | |
| 53 VideoPixelFormat format, | |
| 54 const gfx::Size& coded_size, | |
| 55 const gfx::Rect& visible_rect, | |
| 56 const gfx::Size& natural_size, | |
| 57 mojo::ScopedSharedBufferHandle handle, | |
| 58 size_t data_size, | |
| 59 size_t y_offset, | |
| 60 size_t u_offset, | |
| 61 size_t v_offset, | |
| 62 int32_t y_stride, | |
| 63 int32_t u_stride, | |
| 64 int32_t v_stride, | |
| 65 base::TimeDelta timestamp) { | |
| 66 if (!IsValidConfig(format, STORAGE_MOJO_SHARED_BUFFER, coded_size, | |
| 67 visible_rect, natural_size)) { | |
| 68 LOG(DFATAL) << __FUNCTION__ << " Invalid config. " | |
| 69 << ConfigToString(format, STORAGE_MOJO_SHARED_BUFFER, | |
| 70 coded_size, visible_rect, natural_size); | |
| 71 return nullptr; | |
| 72 } | |
| 73 | |
| 74 // Now allocate the frame and initialize it. | |
| 75 scoped_refptr<MojoSharedBufferVideoFrame> frame( | |
| 76 new MojoSharedBufferVideoFrame(format, coded_size, visible_rect, | |
| 77 natural_size, std::move(handle), data_size, | |
| 78 timestamp)); | |
| 79 if (!frame->Init(y_stride, u_stride, v_stride, y_offset, u_offset, v_offset)) | |
| 80 return nullptr; | |
| 81 | |
| 82 return frame; | |
| 83 } | |
| 84 | |
| 85 MojoSharedBufferVideoFrame::MojoSharedBufferVideoFrame( | |
| 86 VideoPixelFormat format, | |
| 87 const gfx::Size& coded_size, | |
| 88 const gfx::Rect& visible_rect, | |
| 89 const gfx::Size& natural_size, | |
| 90 mojo::ScopedSharedBufferHandle handle, | |
| 91 size_t mapped_size, | |
| 92 base::TimeDelta timestamp) | |
| 93 : VideoFrame(format, | |
| 94 STORAGE_MOJO_SHARED_BUFFER, | |
| 95 coded_size, | |
| 96 visible_rect, | |
| 97 natural_size, | |
| 98 timestamp), | |
| 99 shared_buffer_handle_(std::move(handle)), | |
| 100 shared_buffer_size_(mapped_size), | |
| 101 buffer_data_(nullptr) { | |
| 102 DCHECK(shared_buffer_handle_.is_valid()); | |
| 103 } | |
| 104 | |
| 105 bool MojoSharedBufferVideoFrame::Init(int32_t y_stride, | |
| 106 int32_t u_stride, | |
| 107 int32_t v_stride, | |
| 108 size_t y_offset, | |
| 109 size_t u_offset, | |
| 110 size_t v_offset) { | |
| 111 DCHECK(!buffer_data_); | |
| 112 const MojoResult result = mojo::MapBuffer( | |
| 113 shared_buffer_handle_.get(), 0 /* offset */, shared_buffer_size_, | |
| 114 &buffer_data_, MOJO_MAP_BUFFER_FLAG_NONE); | |
| 115 if (result != MOJO_RESULT_OK || !buffer_data_) | |
| 116 return false; | |
| 117 | |
| 118 set_stride(kYPlane, y_stride); | |
| 119 set_stride(kUPlane, u_stride); | |
| 120 set_stride(kVPlane, v_stride); | |
| 121 offsets_[kYPlane] = y_offset; | |
| 122 offsets_[kUPlane] = u_offset; | |
| 123 offsets_[kVPlane] = v_offset; | |
| 124 set_data(kYPlane, reinterpret_cast<uint8_t*>(buffer_data_) + y_offset); | |
|
xhwang
2016/03/04 00:55:34
Can we declare |buffer_data_| as uint8_t* then we
jrummell
2016/03/04 02:19:06
MapBuffer() and UnmapBuffer() want void*, so have
xhwang
2016/03/04 04:32:27
Non-void pointers can be converted to void* implic
jrummell
2016/03/04 19:13:38
MapBuffer takes void**, so it still needs a cast.
| |
| 125 set_data(kUPlane, reinterpret_cast<uint8_t*>(buffer_data_) + u_offset); | |
| 126 set_data(kVPlane, reinterpret_cast<uint8_t*>(buffer_data_) + v_offset); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 MojoSharedBufferVideoFrame::~MojoSharedBufferVideoFrame() { | |
| 131 // As MapBuffer() was called, we need to have a matching call to unmap it. | |
| 132 const MojoResult result = mojo::UnmapBuffer(buffer_data_); | |
| 133 MOJO_ALLOW_UNUSED_LOCAL(result); | |
| 134 DCHECK_EQ(result, MOJO_RESULT_OK); | |
| 135 } | |
| 136 | |
| 137 size_t MojoSharedBufferVideoFrame::plane_offset(size_t plane) const { | |
| 138 DCHECK(IsValidPlane(plane, format())); | |
| 139 return offsets_[plane]; | |
| 140 } | |
| 141 | |
| 142 } // namespace media | |
| OLD | NEW |