| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/mojo/common/mojo_shared_buffer_video_frame.h" | 5 #include "media/mojo/common/mojo_shared_buffer_video_frame.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 | 11 |
| 12 namespace media { | 12 namespace media { |
| 13 | 13 |
| 14 static inline size_t RoundUp(size_t value, size_t alignment) { |
| 15 // DCHECK(IsPowerOfTwo(alignment)); |
| 16 return ((value + (alignment - 1)) & ~(alignment - 1)); |
| 17 } |
| 18 |
| 19 // static |
| 20 scoped_refptr<MojoSharedBufferVideoFrame> |
| 21 MojoSharedBufferVideoFrame::WrapMojoSharedBufferVideoFrame( |
| 22 const scoped_refptr<MojoSharedBufferVideoFrame>& frame) { |
| 23 mojo::ScopedSharedBufferHandle duplicated_handle = frame->Handle().Clone(); |
| 24 CHECK(duplicated_handle.is_valid()); |
| 25 |
| 26 return MojoSharedBufferVideoFrame::Create( |
| 27 frame->format(), frame->coded_size(), frame->visible_rect(), |
| 28 frame->natural_size(), std::move(duplicated_handle), frame->MappedSize(), |
| 29 frame->PlaneOffset(media::VideoFrame::kYPlane), |
| 30 frame->PlaneOffset(media::VideoFrame::kUPlane), |
| 31 frame->PlaneOffset(media::VideoFrame::kVPlane), |
| 32 frame->stride(media::VideoFrame::kYPlane), |
| 33 frame->stride(media::VideoFrame::kUPlane), |
| 34 frame->stride(media::VideoFrame::kVPlane), frame->timestamp()); |
| 35 } |
| 36 |
| 37 // static |
| 38 scoped_refptr<MojoSharedBufferVideoFrame> MojoSharedBufferVideoFrame::CreateYUV( |
| 39 const VideoPixelFormat format, |
| 40 const gfx::Size& dimensions, |
| 41 base::TimeDelta timestamp) { |
| 42 if (!IsYuvPlanar(format)) { |
| 43 NOTIMPLEMENTED(); |
| 44 return nullptr; |
| 45 } |
| 46 |
| 47 const gfx::Rect visible_rect(dimensions); |
| 48 |
| 49 // Since we're allocating memory for the new frame, pad the requested |
| 50 // size if necessary so that the requested size does line up on sample |
| 51 // boundaries. See related discussion in VideoFrame::CreateFrameInternal(). |
| 52 const gfx::Size coded_size = DetermineAlignedSize(format, dimensions); |
| 53 if (!IsValidConfig(format, STORAGE_MOJO_SHARED_BUFFER, coded_size, |
| 54 visible_rect, dimensions)) { |
| 55 LOG(DFATAL) << __func__ << " Invalid config. " |
| 56 << ConfigToString(format, STORAGE_MOJO_SHARED_BUFFER, |
| 57 dimensions, visible_rect, dimensions); |
| 58 return nullptr; |
| 59 } |
| 60 |
| 61 size_t data_size = 0; |
| 62 int32_t strides[kMaxPlanes]; |
| 63 size_t offsets[kMaxPlanes]; |
| 64 for (size_t plane = 0; plane < NumPlanes(format); ++plane) { |
| 65 // The *2 in alignment for height is because some formats (e.g. h264) allow |
| 66 // interlaced coding, and then the size needs to be a multiple of two |
| 67 // macroblocks (vertically). See |
| 68 // libavcodec/utils.c:avcodec_align_dimensions2(). |
| 69 const size_t height = |
| 70 RoundUp(VideoFrame::Rows(plane, format, coded_size.height()), |
| 71 kFrameSizeAlignment * 2); |
| 72 |
| 73 size_t stride = |
| 74 RoundUp(VideoFrame::RowBytes(plane, format, coded_size.width()), |
| 75 kFrameSizeAlignment); |
| 76 DCHECK_LE(stride, |
| 77 static_cast<uint32_t>(std::numeric_limits<int32_t>::max())); |
| 78 strides[plane] = static_cast<int32_t>(stride); |
| 79 offsets[plane] = data_size; |
| 80 data_size += height * strides[plane]; |
| 81 } |
| 82 |
| 83 // The extra line of UV being allocated is because h264 chroma MC |
| 84 // overreads by one line in some cases, see libavcodec/utils.c: |
| 85 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: |
| 86 // put_h264_chroma_mc4_ssse3(). |
| 87 DCHECK(IsValidPlane(kUPlane, format)); |
| 88 data_size += strides[kUPlane] + kFrameSizePadding; |
| 89 |
| 90 // Allocate a shared memory buffer big enough to hold the desired frame. |
| 91 // const size_t allocation_size = VideoFrame::AllocationSize(format, |
| 92 // coded_size); |
| 93 mojo::ScopedSharedBufferHandle handle = |
| 94 mojo::SharedBufferHandle::Create(data_size); |
| 95 if (!handle.is_valid()) |
| 96 return nullptr; |
| 97 |
| 98 return Create(format, coded_size, visible_rect, dimensions, std::move(handle), |
| 99 data_size, offsets[kYPlane], offsets[kUPlane], offsets[kVPlane], |
| 100 strides[kYPlane], strides[kUPlane], strides[kVPlane], |
| 101 timestamp); |
| 102 } |
| 103 |
| 14 // static | 104 // static |
| 15 scoped_refptr<MojoSharedBufferVideoFrame> | 105 scoped_refptr<MojoSharedBufferVideoFrame> |
| 16 MojoSharedBufferVideoFrame::CreateDefaultI420(const gfx::Size& dimensions, | 106 MojoSharedBufferVideoFrame::CreateDefaultI420(const gfx::Size& dimensions, |
| 17 base::TimeDelta timestamp) { | 107 base::TimeDelta timestamp) { |
| 18 const VideoPixelFormat format = PIXEL_FORMAT_I420; | 108 const VideoPixelFormat format = PIXEL_FORMAT_I420; |
| 19 const gfx::Rect visible_rect(dimensions); | 109 const gfx::Rect visible_rect(dimensions); |
| 20 | 110 |
| 21 // Since we're allocating memory for the new frame, pad the requested | 111 // Since we're allocating memory for the new frame, pad the requested |
| 22 // size if necessary so that the requested size does line up on sample | 112 // size if necessary so that the requested size does line up on sample |
| 23 // boundaries. See related discussion in VideoFrame::CreateFrameInternal(). | 113 // boundaries. See related discussion in VideoFrame::CreateFrameInternal(). |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 235 |
| 146 const mojo::SharedBufferHandle& MojoSharedBufferVideoFrame::Handle() const { | 236 const mojo::SharedBufferHandle& MojoSharedBufferVideoFrame::Handle() const { |
| 147 return shared_buffer_handle_.get(); | 237 return shared_buffer_handle_.get(); |
| 148 } | 238 } |
| 149 | 239 |
| 150 size_t MojoSharedBufferVideoFrame::MappedSize() const { | 240 size_t MojoSharedBufferVideoFrame::MappedSize() const { |
| 151 return shared_buffer_size_; | 241 return shared_buffer_size_; |
| 152 } | 242 } |
| 153 | 243 |
| 154 } // namespace media | 244 } // namespace media |
| OLD | NEW |