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