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 MojoVideoFramePoolDelegate::~MojoVideoFramePoolDelegate() {} | |
20 | |
21 scoped_refptr<VideoFrame> | |
22 MojoVideoFramePoolDelegate::CreateZeroInitializedFrame( | |
23 VideoPixelFormat format, | |
24 const gfx::Size& coded_size, | |
25 const gfx::Rect& visible_rect, | |
26 const gfx::Size& natural_size, | |
27 base::TimeDelta timestamp) { | |
28 return MojoSharedBufferVideoFrame::CreateYUV(format, natural_size, timestamp); | |
29 } | |
30 | |
31 scoped_refptr<VideoFrame> MojoVideoFramePoolDelegate::WrapVideoFrame( | |
32 const scoped_refptr<VideoFrame>& frame) { | |
33 DCHECK(frame->storage_type() == VideoFrame::STORAGE_MOJO_SHARED_BUFFER); | |
34 | |
35 return MojoSharedBufferVideoFrame::WrapMojoSharedBufferVideoFrame( | |
36 static_cast<media::MojoSharedBufferVideoFrame*>(frame.get())); | |
37 } | |
38 | |
39 // static | |
40 scoped_refptr<MojoSharedBufferVideoFrame> | |
41 MojoSharedBufferVideoFrame::WrapMojoSharedBufferVideoFrame( | |
42 const scoped_refptr<MojoSharedBufferVideoFrame>& frame) { | |
43 mojo::ScopedSharedBufferHandle duplicated_handle = frame->Handle().Clone(); | |
44 CHECK(duplicated_handle.is_valid()); | |
45 | |
46 return MojoSharedBufferVideoFrame::Create( | |
47 frame->format(), frame->coded_size(), frame->visible_rect(), | |
48 frame->natural_size(), std::move(duplicated_handle), frame->MappedSize(), | |
49 frame->PlaneOffset(media::VideoFrame::kYPlane), | |
50 frame->PlaneOffset(media::VideoFrame::kUPlane), | |
51 frame->PlaneOffset(media::VideoFrame::kVPlane), | |
52 frame->stride(media::VideoFrame::kYPlane), | |
53 frame->stride(media::VideoFrame::kUPlane), | |
54 frame->stride(media::VideoFrame::kVPlane), frame->timestamp()); | |
55 } | |
56 | |
57 // static | |
58 scoped_refptr<MojoSharedBufferVideoFrame> MojoSharedBufferVideoFrame::CreateYUV( | |
59 const VideoPixelFormat format, | |
60 const gfx::Size& dimensions, | |
61 base::TimeDelta timestamp) { | |
62 if (!IsYuvPlanar(format)) { | |
63 NOTIMPLEMENTED(); | |
64 return nullptr; | |
65 } | |
66 | |
67 const gfx::Rect visible_rect(dimensions); | |
68 | |
69 // Since we're allocating memory for the new frame, pad the requested | |
70 // size if necessary so that the requested size does line up on sample | |
71 // boundaries. See related discussion in VideoFrame::CreateFrameInternal(). | |
72 const gfx::Size coded_size = DetermineAlignedSize(format, dimensions); | |
73 if (!IsValidConfig(format, STORAGE_MOJO_SHARED_BUFFER, coded_size, | |
74 visible_rect, dimensions)) { | |
75 LOG(DFATAL) << __FUNCTION__ << " Invalid config. " | |
sandersd (OOO until July 31)
2016/12/15 00:38:02
Nit: Prefer __func__ to __FUNCTION__.
| |
76 << ConfigToString(format, STORAGE_MOJO_SHARED_BUFFER, | |
77 dimensions, visible_rect, dimensions); | |
78 return nullptr; | |
79 } | |
80 | |
81 size_t data_size = 0; | |
82 int32_t strides[kMaxPlanes]; | |
83 size_t offsets[kMaxPlanes]; | |
84 for (size_t plane = 0; plane < NumPlanes(format); ++plane) { | |
85 // The *2 in alignment for height is because some formats (e.g. h264) allow | |
86 // interlaced coding, and then the size needs to be a multiple of two | |
87 // macroblocks (vertically). See | |
88 // libavcodec/utils.c:avcodec_align_dimensions2(). | |
89 const size_t height = | |
90 RoundUp(VideoFrame::Rows(plane, format, coded_size.height()), | |
91 kFrameSizeAlignment * 2); | |
92 | |
93 size_t stride = | |
94 RoundUp(VideoFrame::RowBytes(plane, format, coded_size.width()), | |
95 kFrameSizeAlignment); | |
96 DCHECK_LE(stride, | |
97 static_cast<uint32_t>(std::numeric_limits<int32_t>::max())); | |
98 strides[plane] = static_cast<int32_t>(stride); | |
99 offsets[plane] = data_size; | |
100 data_size += height * strides[plane]; | |
101 } | |
102 | |
103 // The extra line of UV being allocated is because h264 chroma MC | |
104 // overreads by one line in some cases, see libavcodec/utils.c: | |
105 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: | |
106 // put_h264_chroma_mc4_ssse3(). | |
107 DCHECK(IsValidPlane(kUPlane, format)); | |
108 data_size += strides[kUPlane] + kFrameSizePadding; | |
109 | |
110 // Allocate a shared memory buffer big enough to hold the desired frame. | |
111 // const size_t allocation_size = VideoFrame::AllocationSize(format, | |
112 // coded_size); | |
113 mojo::ScopedSharedBufferHandle handle = | |
114 mojo::SharedBufferHandle::Create(data_size); | |
115 if (!handle.is_valid()) | |
116 return nullptr; | |
117 | |
118 return Create(format, coded_size, visible_rect, dimensions, std::move(handle), | |
119 data_size, offsets[kYPlane], offsets[kUPlane], offsets[kVPlane], | |
120 strides[kYPlane], strides[kUPlane], strides[kVPlane], | |
121 timestamp); | |
122 } | |
123 | |
14 // static | 124 // static |
15 scoped_refptr<MojoSharedBufferVideoFrame> | 125 scoped_refptr<MojoSharedBufferVideoFrame> |
16 MojoSharedBufferVideoFrame::CreateDefaultI420(const gfx::Size& dimensions, | 126 MojoSharedBufferVideoFrame::CreateDefaultI420(const gfx::Size& dimensions, |
17 base::TimeDelta timestamp) { | 127 base::TimeDelta timestamp) { |
18 const VideoPixelFormat format = PIXEL_FORMAT_I420; | 128 const VideoPixelFormat format = PIXEL_FORMAT_I420; |
19 const gfx::Rect visible_rect(dimensions); | 129 const gfx::Rect visible_rect(dimensions); |
20 | 130 |
21 // Since we're allocating memory for the new frame, pad the requested | 131 // 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 | 132 // size if necessary so that the requested size does line up on sample |
23 // boundaries. See related discussion in VideoFrame::CreateFrameInternal(). | 133 // boundaries. See related discussion in VideoFrame::CreateFrameInternal(). |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 | 255 |
146 const mojo::SharedBufferHandle& MojoSharedBufferVideoFrame::Handle() const { | 256 const mojo::SharedBufferHandle& MojoSharedBufferVideoFrame::Handle() const { |
147 return shared_buffer_handle_.get(); | 257 return shared_buffer_handle_.get(); |
148 } | 258 } |
149 | 259 |
150 size_t MojoSharedBufferVideoFrame::MappedSize() const { | 260 size_t MojoSharedBufferVideoFrame::MappedSize() const { |
151 return shared_buffer_size_; | 261 return shared_buffer_size_; |
152 } | 262 } |
153 | 263 |
154 } // namespace media | 264 } // namespace media |
OLD | NEW |