Chromium Code Reviews| Index: media/mojo/common/mojo_video_frame.cc |
| diff --git a/media/mojo/common/mojo_video_frame.cc b/media/mojo/common/mojo_video_frame.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84f7853b7e52af73cf0b88de9644ff788e841038 |
| --- /dev/null |
| +++ b/media/mojo/common/mojo_video_frame.cc |
| @@ -0,0 +1,148 @@ |
| +// Copyright 2016 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/mojo/common/mojo_video_frame.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "build/build_config.h" |
|
mcasas
2016/02/25 15:12:35
Needed?
jrummell
2016/02/25 21:51:47
Nope. Removed.
|
| + |
| +namespace media { |
| + |
| +// static |
| +scoped_refptr<MojoVideoFrame> MojoVideoFrame::Create( |
| + const gfx::Size& dimensions, |
| + base::TimeDelta timestamp) { |
| + const StorageType storage_type = STORAGE_MOJO_SHARED_MEMORY; |
| + const VideoPixelFormat format = PIXEL_FORMAT_I420; |
| + gfx::Rect visible_rect(dimensions); |
| + if (!IsValidConfig(format, storage_type, dimensions, visible_rect, |
| + dimensions)) { |
| + LOG(DFATAL) << __FUNCTION__ << " Invalid config." |
|
mcasas
2016/02/25 15:12:35
nit: space before closing |"|
jrummell
2016/02/25 21:51:47
Done.
|
| + << ConfigToString(format, storage_type, dimensions, |
| + visible_rect, dimensions); |
| + return nullptr; |
| + } |
| + |
| + // Allocate a shared memory buffer big enough to hold the desired frame. |
| + size_t requested_size = VideoFrame::AllocationSize(format, dimensions); |
| + mojo::ScopedSharedBufferHandle handle; |
| + MojoResult result = |
|
mcasas
2016/02/25 15:12:35
nit: const ?
jrummell
2016/02/25 21:51:48
Done.
|
| + mojo::CreateSharedBuffer(nullptr, requested_size, &handle); |
| + if (result != MOJO_RESULT_OK) |
| + return nullptr; |
| + |
| + // Now allocate the frame and initialize it. |
| + scoped_refptr<MojoVideoFrame> frame(new MojoVideoFrame( |
| + format, storage_type, dimensions, visible_rect, dimensions, |
| + std::move(handle), requested_size, timestamp)); |
| + if (!frame->Init(dimensions.width(), dimensions.width() / 2, |
| + dimensions.width() / 2, 0 /* y_offset */, |
| + dimensions.GetArea(), dimensions.GetArea() * 5 / 4)) |
| + return nullptr; |
| + |
| + return frame; |
| +} |
| + |
| +// static |
| +scoped_refptr<MojoVideoFrame> MojoVideoFrame::Create( |
| + VideoPixelFormat format, |
| + const gfx::Size& coded_size, |
| + const gfx::Rect& visible_rect, |
| + const gfx::Size& natural_size, |
| + mojo::SharedBufferHandle handle, |
| + size_t data_size, |
| + size_t y_offset, |
| + size_t u_offset, |
| + size_t v_offset, |
| + int32_t y_stride, |
| + int32_t u_stride, |
| + int32_t v_stride, |
| + base::TimeDelta timestamp) { |
| + const StorageType storage_type = STORAGE_MOJO_SHARED_MEMORY; |
|
mcasas
2016/02/25 15:12:35
Since |storage_type| is always the same in
this fi
jrummell
2016/02/25 21:51:48
Done.
|
| + if (!IsValidConfig(format, storage_type, coded_size, visible_rect, |
| + natural_size)) { |
| + LOG(DFATAL) << __FUNCTION__ << " Invalid config." |
| + << ConfigToString(format, storage_type, coded_size, |
| + visible_rect, natural_size); |
| + return nullptr; |
| + } |
| + |
| + // Make a copy of the shared memory handle. |
| + mojo::ScopedSharedBufferHandle duplicated_handle; |
| + MojoResult result = |
| + mojo::DuplicateBuffer(handle, nullptr, &duplicated_handle); |
| + if (result != MOJO_RESULT_OK) |
| + return nullptr; |
| + |
| + // Now allocate the frame and initialize it. |
| + scoped_refptr<MojoVideoFrame> frame(new MojoVideoFrame( |
| + format, storage_type, coded_size, visible_rect, natural_size, |
| + std::move(duplicated_handle), data_size, timestamp)); |
| + if (!frame->Init(y_stride, u_stride, v_stride, y_offset, u_offset, v_offset)) |
| + return nullptr; |
| + |
| + return frame; |
| +} |
| + |
| +MojoVideoFrame::MojoVideoFrame(VideoPixelFormat format, |
| + StorageType storage_type, |
|
mcasas
2016/02/25 15:12:35
This parameter is not needed since is always
the
jrummell
2016/02/25 21:51:48
Done.
|
| + const gfx::Size& coded_size, |
| + const gfx::Rect& visible_rect, |
| + const gfx::Size& natural_size, |
|
mcasas
2016/02/25 15:12:35
IIRC the only case of |natural_size| being
differe
jrummell
2016/02/25 21:51:48
I talked with xhwang@. Apparently both Netflix and
|
| + mojo::ScopedSharedBufferHandle handle, |
| + size_t mapped_size, |
| + base::TimeDelta timestamp) |
| + : VideoFrame(format, |
| + storage_type, |
| + coded_size, |
| + visible_rect, |
| + natural_size, |
| + timestamp), |
| + shared_buffer_handle_(std::move(handle)), |
| + shared_buffer_size_(mapped_size) { |
| + DCHECK(shared_buffer_handle_.is_valid()); |
| +} |
| + |
| +bool MojoVideoFrame::Init(int32_t y_stride, |
| + int32_t u_stride, |
| + int32_t v_stride, |
| + size_t y_offset, |
| + size_t u_offset, |
| + size_t v_offset) { |
| + uint8_t* frame_data = nullptr; |
| + MojoResult result = mojo::MapBuffer( |
|
mcasas
2016/02/25 15:12:35
const.
jrummell
2016/02/25 21:51:47
Done.
|
| + shared_buffer_handle_.get(), 0 /* offset */, shared_buffer_size_, |
| + reinterpret_cast<void**>(&frame_data), MOJO_MAP_BUFFER_FLAG_NONE); |
| + if (result != MOJO_RESULT_OK || !frame_data) |
| + return false; |
| + |
| + set_stride(kYPlane, y_stride); |
| + set_stride(kUPlane, u_stride); |
| + set_stride(kVPlane, v_stride); |
| + offsets_[kYPlane] = y_offset; |
| + offsets_[kUPlane] = u_offset; |
| + offsets_[kVPlane] = v_offset; |
| + set_data(kYPlane, frame_data + y_offset); |
| + set_data(kUPlane, frame_data + u_offset); |
| + set_data(kVPlane, frame_data + v_offset); |
| + return true; |
| +} |
| + |
| +MojoVideoFrame::~MojoVideoFrame() {} |
| + |
| +mojo::SharedBufferHandle MojoVideoFrame::handle() const { |
| + return shared_buffer_handle_.get(); |
| +} |
| + |
| +size_t MojoVideoFrame::mapped_size() const { |
| + return shared_buffer_size_; |
| +} |
| + |
| +size_t MojoVideoFrame::plane_offset(size_t plane) const { |
| + DCHECK(IsValidPlane(plane, format())); |
| + return offsets_[plane]; |
| +} |
| + |
| +} // namespace media |