1 // Copyright (c) 2012 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.
5 #ifndef MEDIA_BASE_VIDEO_FRAME_H_
6 #define MEDIA_BASE_VIDEO_FRAME_H_
10 #include "base/callback.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/synchronization/lock.h"
14 #include "media/base/buffers.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/size.h"
18 #if defined(OS_MACOSX)
19 #include <CoreVideo/CVPixelBuffer.h>
20 #include "base/mac/scoped_cftyperef.h"
29 class MEDIA_EXPORT VideoFrame
: public base::RefCountedThreadSafe
<VideoFrame
> {
32 kFrameSizeAlignment
= 16,
33 kFrameSizePadding
= 16,
34 kFrameAddressAlignment
= 32
48 // Surface formats roughly based on FOURCC labels, see:
49 // http://www.fourcc.org/rgb.php
50 // http://www.fourcc.org/yuv.php
51 // Logged to UMA, so never reuse values.
53 UNKNOWN
= 0, // Unknown format value.
54 YV12
= 1, // 12bpp YVU planar 1x1 Y, 2x2 VU samples
55 YV16
= 2, // 16bpp YVU planar 1x1 Y, 2x1 VU samples
56 I420
= 3, // 12bpp YVU planar 1x1 Y, 2x2 UV samples.
57 YV12A
= 4, // 20bpp YUVA planar 1x1 Y, 2x2 VU, 1x1 A samples.
58 #if defined(VIDEO_HOLE)
59 HOLE
= 5, // Hole frame.
60 #endif // defined(VIDEO_HOLE)
61 NATIVE_TEXTURE
= 6, // Native texture. Pixel-format agnostic.
62 YV12J
= 7, // JPEG color range version of YV12
63 NV12
= 8, // 12bpp 1x1 Y plane followed by an interleaved 2x2 UV plane.
64 YV24
= 9, // 24bpp YUV planar, no subsampling.
65 ARGB
= 10, // 32bpp ARGB, 1 plane.
66 YV12HD
= 11, // Rec709 "HD" color space version of YV12
67 // Please update UMA histogram enumeration when adding new formats here.
68 FORMAT_MAX
= YV12HD
, // Must always be equal to largest entry logged.
71 // Returns the name of a Format as a string.
72 static std::string
FormatToString(Format format
);
74 // Creates a new frame in system memory with given parameters. Buffers for
75 // the frame are allocated but not initialized.
76 static scoped_refptr
<VideoFrame
> CreateFrame(
78 const gfx::Size
& coded_size
,
79 const gfx::Rect
& visible_rect
,
80 const gfx::Size
& natural_size
,
81 base::TimeDelta timestamp
);
83 // Returns true if |plane| is a valid plane number for the given format. This
84 // can be used to DCHECK() plane parameters.
85 static bool IsValidPlane(size_t plane
, VideoFrame::Format format
);
87 // Call prior to CreateFrame to ensure validity of frame configuration. Called
88 // automatically by VideoDecoderConfig::IsValidConfig().
89 // TODO(scherkus): VideoDecoderConfig shouldn't call this method
90 static bool IsValidConfig(Format format
, const gfx::Size
& coded_size
,
91 const gfx::Rect
& visible_rect
,
92 const gfx::Size
& natural_size
);
94 // CB to be called on the mailbox backing this frame when the frame is
96 typedef base::Callback
<void(uint32
)> ReleaseMailboxCB
;
98 // Wraps a native texture of the given parameters with a VideoFrame. The
99 // backing of the VideoFrame is held in the mailbox held by |mailbox_holder|,
100 // and |mailbox_holder_release_cb| will be called with |mailbox_holder| as the
101 // argument when the VideoFrame is to be destroyed.
102 // |read_pixels_cb| may be used to do (slow!) readbacks from the
103 // texture to main memory.
104 static scoped_refptr
<VideoFrame
> WrapNativeTexture(
105 scoped_ptr
<gpu::MailboxHolder
> mailbox_holder
,
106 const ReleaseMailboxCB
& mailbox_holder_release_cb
,
107 const gfx::Size
& coded_size
,
108 const gfx::Rect
& visible_rect
,
109 const gfx::Size
& natural_size
,
110 base::TimeDelta timestamp
,
113 // Wraps packed image data residing in a memory buffer with a VideoFrame.
114 // The image data resides in |data| and is assumed to be packed tightly in a
115 // buffer of logical dimensions |coded_size| with the appropriate bit depth
116 // and plane count as given by |format|. The shared memory handle of the
117 // backing allocation, if present, can be passed in with |handle|. When the
118 // frame is destroyed, |no_longer_needed_cb.Run()| will be called.
119 // Returns NULL on failure.
120 static scoped_refptr
<VideoFrame
> WrapExternalPackedMemory(
122 const gfx::Size
& coded_size
,
123 const gfx::Rect
& visible_rect
,
124 const gfx::Size
& natural_size
,
127 base::SharedMemoryHandle handle
,
128 size_t shared_memory_offset
,
129 base::TimeDelta timestamp
,
130 const base::Closure
& no_longer_needed_cb
);
132 // Wraps external YUV data of the given parameters with a VideoFrame.
133 // The returned VideoFrame does not own the data passed in. When the frame
134 // is destroyed |no_longer_needed_cb.Run()| will be called.
135 static scoped_refptr
<VideoFrame
> WrapExternalYuvData(
137 const gfx::Size
& coded_size
,
138 const gfx::Rect
& visible_rect
,
139 const gfx::Size
& natural_size
,
146 base::TimeDelta timestamp
,
147 const base::Closure
& no_longer_needed_cb
);
149 #if defined(OS_POSIX)
150 // Wraps provided dmabufs
151 // (https://www.kernel.org/doc/Documentation/dma-buf-sharing.txt) with a
152 // VideoFrame. The dmabuf fds are dup()ed on creation, so that the VideoFrame
153 // retains a reference to them, and are automatically close()d on destruction,
154 // dropping the reference. The caller may safely close() its reference after
155 // calling WrapExternalDmabufs().
156 // The image data is only accessible via dmabuf fds, which are usually passed
157 // directly to a hardware device and/or to another process, or can also be
158 // mapped via mmap() for CPU access.
159 // When the frame is destroyed, |no_longer_needed_cb.Run()| will be called.
160 // Returns NULL on failure.
161 static scoped_refptr
<VideoFrame
> WrapExternalDmabufs(
163 const gfx::Size
& coded_size
,
164 const gfx::Rect
& visible_rect
,
165 const gfx::Size
& natural_size
,
166 const std::vector
<int> dmabuf_fds
,
167 base::TimeDelta timestamp
,
168 const base::Closure
& no_longer_needed_cb
);
171 #if defined(OS_MACOSX)
172 // Wraps a provided CVPixelBuffer with a VideoFrame. The pixel buffer is
173 // retained for the lifetime of the VideoFrame and released upon destruction.
174 // The image data is only accessible via the pixel buffer, which could be
175 // backed by an IOSurface from another process. All the attributes of the
176 // VideoFrame are derived from the pixel buffer, with the exception of the
177 // timestamp. If information is missing or is incompatible (for example, a
178 // pixel format that has no VideoFrame match), NULL is returned.
179 // http://crbug.com/401308
180 static scoped_refptr
<VideoFrame
> WrapCVPixelBuffer(
181 CVPixelBufferRef cv_pixel_buffer
,
182 base::TimeDelta timestamp
);
185 // Wraps |frame| and calls |no_longer_needed_cb| when the wrapper VideoFrame
186 // gets destroyed. |visible_rect| must be a sub rect within
187 // frame->visible_rect().
188 static scoped_refptr
<VideoFrame
> WrapVideoFrame(
189 const scoped_refptr
<VideoFrame
>& frame
,
190 const gfx::Rect
& visible_rect
,
191 const gfx::Size
& natural_size
,
192 const base::Closure
& no_longer_needed_cb
);
194 // Creates a frame which indicates end-of-stream.
195 static scoped_refptr
<VideoFrame
> CreateEOSFrame();
197 // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v).
198 static scoped_refptr
<VideoFrame
> CreateColorFrame(
199 const gfx::Size
& size
,
200 uint8 y
, uint8 u
, uint8 v
,
201 base::TimeDelta timestamp
);
203 // Allocates YV12 frame based on |size|, and sets its data to the YUV
204 // equivalent of RGB(0,0,0).
205 static scoped_refptr
<VideoFrame
> CreateBlackFrame(const gfx::Size
& size
);
207 // Allocates YV12A frame based on |size|, and sets its data to the YUVA
208 // equivalent of RGBA(0,0,0,0).
209 static scoped_refptr
<VideoFrame
> CreateTransparentFrame(
210 const gfx::Size
& size
);
212 #if defined(VIDEO_HOLE)
213 // Allocates a hole frame.
214 static scoped_refptr
<VideoFrame
> CreateHoleFrame(const gfx::Size
& size
);
215 #endif // defined(VIDEO_HOLE)
217 static size_t NumPlanes(Format format
);
219 // Returns the required allocation size for a (tightly packed) frame of the
220 // given coded size and format.
221 static size_t AllocationSize(Format format
, const gfx::Size
& coded_size
);
223 // Returns the plane size (in bytes) for a plane of the given coded size and
225 static gfx::Size
PlaneSize(Format format
,
227 const gfx::Size
& coded_size
);
229 // Returns the required allocation size for a (tightly packed) plane of the
230 // given coded size and format.
231 static size_t PlaneAllocationSize(Format format
,
233 const gfx::Size
& coded_size
);
235 // Returns horizontal bits per pixel for given |plane| and |format|.
236 static int PlaneHorizontalBitsPerPixel(Format format
, size_t plane
);
238 // Returns bits per pixel for given |plane| and |format|.
239 static int PlaneBitsPerPixel(Format format
, size_t plane
);
241 // Returns the number of bytes per row for the given plane, format, and width.
242 // The width may be aligned to format requirements.
243 static size_t RowBytes(size_t plane
, Format format
, int width
);
245 // Returns the number of rows for the given plane, format, and height.
246 // The height may be aligned to format requirements.
247 static size_t Rows(size_t plane
, Format format
, int height
);
249 // Returns the number of columns for the given plane, format, and width.
250 // The width may be aligned to format requirements.
251 static size_t Columns(size_t plane
, Format format
, int width
);
253 Format
format() const { return format_
; }
255 const gfx::Size
& coded_size() const { return coded_size_
; }
256 const gfx::Rect
& visible_rect() const { return visible_rect_
; }
257 const gfx::Size
& natural_size() const { return natural_size_
; }
259 int stride(size_t plane
) const;
261 // Returns the number of bytes per row and number of rows for a given plane.
263 // As opposed to stride(), row_bytes() refers to the bytes representing
264 // frame data scanlines (coded_size.width() pixels, without stride padding).
265 int row_bytes(size_t plane
) const;
266 int rows(size_t plane
) const;
268 // Returns pointer to the buffer for a given plane. The memory is owned by
269 // VideoFrame object and must not be freed by the caller.
270 const uint8
* data(size_t plane
) const;
271 uint8
* data(size_t plane
);
273 // Returns pointer to the data in the visible region of the frame. I.e. the
274 // returned pointer is offsetted into the plane buffer specified by
275 // visible_rect().origin(). Memory is owned by VideoFrame object and must not
276 // be freed by the caller.
277 const uint8
* visible_data(size_t plane
) const;
278 uint8
* visible_data(size_t plane
);
280 // Returns the mailbox holder of the native texture wrapped by this frame.
281 // Only valid to call if this is a NATIVE_TEXTURE frame. Before using the
282 // mailbox, the caller must wait for the included sync point.
283 const gpu::MailboxHolder
* mailbox_holder() const;
285 // Returns the shared-memory handle, if present
286 base::SharedMemoryHandle
shared_memory_handle() const;
288 // Returns the offset into the shared memory where the frame data begins.
289 size_t shared_memory_offset() const;
291 bool allow_overlay() const { return allow_overlay_
; }
293 #if defined(OS_POSIX)
294 // Returns backing dmabuf file descriptor for given |plane|, if present.
295 int dmabuf_fd(size_t plane
) const;
298 #if defined(OS_MACOSX)
299 // Returns the backing CVPixelBuffer, if present.
300 CVPixelBufferRef
cv_pixel_buffer() const;
303 // Returns true if this VideoFrame represents the end of the stream.
304 bool end_of_stream() const { return end_of_stream_
; }
306 base::TimeDelta
timestamp() const {
309 void set_timestamp(const base::TimeDelta
& timestamp
) {
310 timestamp_
= timestamp
;
313 class SyncPointClient
{
316 virtual uint32
InsertSyncPoint() = 0;
317 virtual void WaitSyncPoint(uint32 sync_point
) = 0;
320 virtual ~SyncPointClient() {}
322 DISALLOW_COPY_AND_ASSIGN(SyncPointClient
);
324 // It uses |client| to insert a new sync point and potentially waits on a
325 // older sync point. The final sync point will be used to release this
327 // This method is thread safe. Both blink and compositor threads can call it.
328 void UpdateReleaseSyncPoint(SyncPointClient
* client
);
330 // Used to keep a running hash of seen frames. Expects an initialized MD5
331 // context. Calls MD5Update with the context and the contents of the frame.
332 void HashFrameForTesting(base::MD5Context
* context
);
335 friend class base::RefCountedThreadSafe
<VideoFrame
>;
337 // Clients must use the static CreateFrame() method to create a new frame.
338 VideoFrame(Format format
,
339 const gfx::Size
& coded_size
,
340 const gfx::Rect
& visible_rect
,
341 const gfx::Size
& natural_size
,
342 scoped_ptr
<gpu::MailboxHolder
> mailbox_holder
,
343 base::TimeDelta timestamp
,
345 virtual ~VideoFrame();
350 const Format format_
;
352 // Width and height of the video frame, in pixels. This must include pixel
353 // data for the whole image; i.e. for YUV formats with subsampled chroma
354 // planes, in the case that the visible portion of the image does not line up
355 // on a sample boundary, |coded_size_| must be rounded up appropriately and
356 // the pixel data provided for the odd pixels.
357 const gfx::Size coded_size_
;
359 // Width, height, and offsets of the visible portion of the video frame. Must
360 // be a subrect of |coded_size_|. Can be odd with respect to the sample
361 // boundaries, e.g. for formats with subsampled chroma.
362 const gfx::Rect visible_rect_
;
364 // Width and height of the visible portion of the video frame
365 // (|visible_rect_.size()|) with aspect ratio taken into account.
366 const gfx::Size natural_size_
;
368 // Array of strides for each plane, typically greater or equal to the width
369 // of the surface divided by the horizontal sampling period. Note that
370 // strides can be negative.
371 int32 strides_
[kMaxPlanes
];
373 // Array of data pointers to each plane.
374 uint8
* data_
[kMaxPlanes
];
376 // Native texture mailbox, if this is a NATIVE_TEXTURE frame.
377 const scoped_ptr
<gpu::MailboxHolder
> mailbox_holder_
;
378 ReleaseMailboxCB mailbox_holder_release_cb_
;
380 // Shared memory handle, if this frame was allocated from shared memory.
381 base::SharedMemoryHandle shared_memory_handle_
;
383 // Offset in shared memory buffer.
384 size_t shared_memory_offset_
;
386 #if defined(OS_POSIX)
387 // Dmabufs for each plane, if this frame is wrapping memory
388 // acquired via dmabuf.
389 base::ScopedFD dmabuf_fds_
[kMaxPlanes
];
392 #if defined(OS_MACOSX)
393 // CVPixelBuffer, if this frame is wrapping one.
394 base::ScopedCFTypeRef
<CVPixelBufferRef
> cv_pixel_buffer_
;
397 base::Closure no_longer_needed_cb_
;
399 base::TimeDelta timestamp_
;
401 base::Lock release_sync_point_lock_
;
402 uint32 release_sync_point_
;
404 const bool end_of_stream_
;
408 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame
);
413 #endif // MEDIA_BASE_VIDEO_FRAME_H_