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 "gpu/command_buffer/common/mailbox_holder.h"
15 #include "media/base/buffers.h"
16 #include "media/base/video_frame_metadata.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/size.h"
20 #if defined(OS_MACOSX)
21 #include <CoreVideo/CVPixelBuffer.h>
22 #include "base/mac/scoped_cftyperef.h"
27 class MEDIA_EXPORT VideoFrame
: public base::RefCountedThreadSafe
<VideoFrame
> {
30 kFrameSizeAlignment
= 16,
31 kFrameSizePadding
= 16,
32 kFrameAddressAlignment
= 32
46 // Pixel formats roughly based on FOURCC labels, see:
47 // http://www.fourcc.org/rgb.php
48 // http://www.fourcc.org/yuv.php
49 // Logged to UMA, so never reuse values. Leave gaps if necessary.
51 UNKNOWN
= 0, // Unknown or unspecified format value.
52 YV12
= 1, // 12bpp YVU planar 1x1 Y, 2x2 VU samples.
53 I420
= 2, // 12bpp YUV planar 1x1 Y, 2x2 UV samples, a.k.a. YU12.
54 YV16
= 3, // 16bpp YVU planar 1x1 Y, 2x1 VU samples.
55 YV12A
= 4, // 20bpp YUVA planar 1x1 Y, 2x2 VU, 1x1 A samples.
56 YV24
= 5, // 24bpp YUV planar, no subsampling.
57 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
58 NV12
= 6, // 12bpp with Y plane followed by a 2x2 interleaved UV plane.
60 ARGB
= 7, // 32bpp ARGB, 1 plane.
61 XRGB
= 8, // 24bpp XRGB, 1 plane.
62 // Please update UMA histogram enumeration when adding new formats here.
63 FORMAT_MAX
= XRGB
, // Must always be equal to largest entry logged.
66 // Color space or color range used for the pixels, in general this is left
67 // unspecified, meaning Rec601 (SD) is assumed.
69 COLOR_SPACE_UNSPECIFIED
= 0, // In general this is Rec601.
70 COLOR_SPACE_JPEG
= 1, // JPEG color range.
71 COLOR_SPACE_HD_REC709
= 2, // Rec709 "HD" color space.
72 COLOR_SPACE_MAX
= COLOR_SPACE_HD_REC709
,
75 // Defines the pixel storage type. STORAGE_{OWNED, UNOWNED}_MEMORY and
76 // STORAGE_SHMEM have in common that are mappable, i.e. can be accessed from
77 // the VideoFrame memory space, whereas the rest, in principle, can't.
80 STORAGE_UNOWNED_MEMORY
= 1, // External, non owned data pointers.
81 STORAGE_OWNED_MEMORY
= 2, // VideoFrame has allocated its own data buffer.
82 STORAGE_SHMEM
= 3, // Pixels are backed by Shared Memory.
84 STORAGE_DMABUFS
= 4, // Each plane is stored into a DmaBuf.
86 #if defined(VIDEO_HOLE)
90 // Pixels are stored in textures, one per plane, referred by MailboxHolders.
92 STORAGE_MAX
= STORAGE_TEXTURE
, // Always equal to the last StorageType.
95 // CB to be called on the mailbox backing this frame when the frame is
97 typedef base::Callback
<void(uint32
)> ReleaseMailboxCB
;
99 // Returns the name of a Format as a string.
100 static std::string
FormatToString(Format format
);
102 // Returns true if |format| is a YUV non interleaved format.
103 static bool IsYuvPlanar(Format format
);
105 // Returns true if |storage_type| is mappable in the VideoFrame memory space.
106 static bool IsMappable(StorageType storage_type
);
108 // Returns true if |plane| is a valid plane number for the given format.
109 static bool IsValidPlane(size_t plane
, VideoFrame::Format format
);
111 // Call prior to CreateFrame to ensure validity of frame configuration. Called
112 // automatically by VideoDecoderConfig::IsValidConfig().
113 // TODO(scherkus): VideoDecoderConfig shouldn't call this method
114 static bool IsValidConfig(Format format
,
115 StorageType storage_type
,
116 const gfx::Size
& coded_size
,
117 const gfx::Rect
& visible_rect
,
118 const gfx::Size
& natural_size
);
120 // Creates a new YUV frame in system memory with given parameters (|format|
121 // must be YUV). Buffers for the frame are allocated but not initialized. The
122 // caller most not make assumptions about the actual underlying size(s), but
123 // check the returned VideoFrame instead.
124 // TODO(mcasas): implement the RGB version of this factory method.
125 static scoped_refptr
<VideoFrame
> CreateFrame(
127 const gfx::Size
& coded_size
,
128 const gfx::Rect
& visible_rect
,
129 const gfx::Size
& natural_size
,
130 base::TimeDelta timestamp
);
132 // Wraps a native texture of the given parameters with a VideoFrame.
133 // The backing of the VideoFrame is held in the mailbox held by
134 // |mailbox_holder|, and |mailbox_holder_release_cb| will be called with
135 // a syncpoint as the argument when the VideoFrame is to be destroyed.
136 static scoped_refptr
<VideoFrame
> WrapNativeTexture(
138 const gpu::MailboxHolder
& mailbox_holder
,
139 const ReleaseMailboxCB
& mailbox_holder_release_cb
,
140 const gfx::Size
& coded_size
,
141 const gfx::Rect
& visible_rect
,
142 const gfx::Size
& natural_size
,
143 base::TimeDelta timestamp
);
145 // Wraps a set of native textures representing YUV data with a VideoFrame.
146 // |mailbox_holders_release_cb| will be called with a syncpoint as the
147 // argument when the VideoFrame is to be destroyed.
148 static scoped_refptr
<VideoFrame
> WrapYUV420NativeTextures(
149 const gpu::MailboxHolder
& y_mailbox_holder
,
150 const gpu::MailboxHolder
& u_mailbox_holder
,
151 const gpu::MailboxHolder
& v_mailbox_holder
,
152 const ReleaseMailboxCB
& mailbox_holders_release_cb
,
153 const gfx::Size
& coded_size
,
154 const gfx::Rect
& visible_rect
,
155 const gfx::Size
& natural_size
,
156 base::TimeDelta timestamp
);
158 // Wraps packed image data residing in a memory buffer with a VideoFrame.
159 // The image data resides in |data| and is assumed to be packed tightly in a
160 // buffer of logical dimensions |coded_size| with the appropriate bit depth
161 // and plane count as given by |format|. Returns NULL on failure.
162 static scoped_refptr
<VideoFrame
> WrapExternalData(
164 const gfx::Size
& coded_size
,
165 const gfx::Rect
& visible_rect
,
166 const gfx::Size
& natural_size
,
169 base::TimeDelta timestamp
);
171 // Same as WrapExternalData() with SharedMemoryHandle and its offset.
172 static scoped_refptr
<VideoFrame
> WrapExternalSharedMemory(
174 const gfx::Size
& coded_size
,
175 const gfx::Rect
& visible_rect
,
176 const gfx::Size
& natural_size
,
179 base::SharedMemoryHandle handle
,
180 size_t shared_memory_offset
,
181 base::TimeDelta timestamp
);
183 // Wraps external YUV data of the given parameters with a VideoFrame.
184 // The returned VideoFrame does not own the data passed in.
185 static scoped_refptr
<VideoFrame
> WrapExternalYuvData(
187 const gfx::Size
& coded_size
,
188 const gfx::Rect
& visible_rect
,
189 const gfx::Size
& natural_size
,
196 base::TimeDelta timestamp
);
198 #if defined(OS_LINUX)
199 // Wraps provided dmabufs
200 // (https://www.kernel.org/doc/Documentation/dma-buf-sharing.txt) with a
201 // VideoFrame. The dmabuf fds are dup()ed on creation, so that the VideoFrame
202 // retains a reference to them, and are automatically close()d on destruction,
203 // dropping the reference. The caller may safely close() its reference after
204 // calling WrapExternalDmabufs().
205 // The image data is only accessible via dmabuf fds, which are usually passed
206 // directly to a hardware device and/or to another process, or can also be
207 // mapped via mmap() for CPU access.
208 // Returns NULL on failure.
209 static scoped_refptr
<VideoFrame
> WrapExternalDmabufs(
211 const gfx::Size
& coded_size
,
212 const gfx::Rect
& visible_rect
,
213 const gfx::Size
& natural_size
,
214 const std::vector
<int> dmabuf_fds
,
215 base::TimeDelta timestamp
);
218 #if defined(OS_MACOSX)
219 // Wraps a provided CVPixelBuffer with a VideoFrame. The pixel buffer is
220 // retained for the lifetime of the VideoFrame and released upon destruction.
221 // The image data is only accessible via the pixel buffer, which could be
222 // backed by an IOSurface from another process. All the attributes of the
223 // VideoFrame are derived from the pixel buffer, with the exception of the
224 // timestamp. If information is missing or is incompatible (for example, a
225 // pixel format that has no VideoFrame match), NULL is returned.
226 // http://crbug.com/401308
227 static scoped_refptr
<VideoFrame
> WrapCVPixelBuffer(
228 CVPixelBufferRef cv_pixel_buffer
,
229 base::TimeDelta timestamp
);
232 // Wraps |frame|. |visible_rect| must be a sub rect within
233 // frame->visible_rect().
234 static scoped_refptr
<VideoFrame
> WrapVideoFrame(
235 const scoped_refptr
<VideoFrame
>& frame
,
236 const gfx::Rect
& visible_rect
,
237 const gfx::Size
& natural_size
);
239 // Creates a frame which indicates end-of-stream.
240 static scoped_refptr
<VideoFrame
> CreateEOSFrame();
242 // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v).
243 static scoped_refptr
<VideoFrame
> CreateColorFrame(
244 const gfx::Size
& size
,
245 uint8 y
, uint8 u
, uint8 v
,
246 base::TimeDelta timestamp
);
248 // Allocates YV12 frame based on |size|, and sets its data to the YUV
249 // equivalent of RGB(0,0,0).
250 static scoped_refptr
<VideoFrame
> CreateBlackFrame(const gfx::Size
& size
);
252 // Allocates YV12A frame based on |size|, and sets its data to the YUVA
253 // equivalent of RGBA(0,0,0,0).
254 static scoped_refptr
<VideoFrame
> CreateTransparentFrame(
255 const gfx::Size
& size
);
257 #if defined(VIDEO_HOLE)
258 // Allocates a hole frame.
259 static scoped_refptr
<VideoFrame
> CreateHoleFrame(const gfx::Size
& size
);
260 #endif // defined(VIDEO_HOLE)
262 static size_t NumPlanes(Format format
);
264 // Returns the required allocation size for a (tightly packed) frame of the
265 // given coded size and format.
266 static size_t AllocationSize(Format format
, const gfx::Size
& coded_size
);
268 // Returns the plane size (in bytes) for a plane of the given coded size and
270 static gfx::Size
PlaneSize(Format format
,
272 const gfx::Size
& coded_size
);
274 // Returns the required allocation size for a (tightly packed) plane of the
275 // given coded size and format.
276 static size_t PlaneAllocationSize(Format format
,
278 const gfx::Size
& coded_size
);
280 // Returns horizontal bits per pixel for given |plane| and |format|.
281 static int PlaneHorizontalBitsPerPixel(Format format
, size_t plane
);
283 // Returns bits per pixel for given |plane| and |format|.
284 static int PlaneBitsPerPixel(Format format
, size_t plane
);
286 // Returns the number of bytes per row for the given plane, format, and width.
287 // The width may be aligned to format requirements.
288 static size_t RowBytes(size_t plane
, Format format
, int width
);
290 // Returns the number of rows for the given plane, format, and height.
291 // The height may be aligned to format requirements.
292 static size_t Rows(size_t plane
, Format format
, int height
);
294 // Returns the number of columns for the given plane, format, and width.
295 // The width may be aligned to format requirements.
296 static size_t Columns(size_t plane
, Format format
, int width
);
298 Format
format() const { return format_
; }
300 StorageType
storage_type() const { return storage_type_
; }
302 const gfx::Size
& coded_size() const { return coded_size_
; }
303 const gfx::Rect
& visible_rect() const { return visible_rect_
; }
304 const gfx::Size
& natural_size() const { return natural_size_
; }
306 int stride(size_t plane
) const;
308 // Returns the number of bytes per row and number of rows for a given plane.
310 // As opposed to stride(), row_bytes() refers to the bytes representing
311 // frame data scanlines (coded_size.width() pixels, without stride padding).
312 int row_bytes(size_t plane
) const;
313 int rows(size_t plane
) const;
315 // Returns pointer to the buffer for a given plane, if this is an IsMappable()
316 // frame type. The memory is owned by VideoFrame object and must not be freed
318 const uint8
* data(size_t plane
) const;
319 uint8
* data(size_t plane
);
321 // Returns pointer to the data in the visible region of the frame, for
322 // IsMappable() storage types. The returned pointer is offsetted into the
323 // plane buffer specified by visible_rect().origin(). Memory is owned by
324 // VideoFrame object and must not be freed by the caller.
325 const uint8
* visible_data(size_t plane
) const;
326 uint8
* visible_data(size_t plane
);
328 // Returns a mailbox holder for a given texture.
329 // Only valid to call if this is a NATIVE_TEXTURE frame. Before using the
330 // mailbox, the caller must wait for the included sync point.
331 const gpu::MailboxHolder
& mailbox_holder(size_t texture_index
) const;
333 // Returns the shared-memory handle, if present
334 base::SharedMemoryHandle
shared_memory_handle() const;
336 // Returns the offset into the shared memory where the frame data begins.
337 size_t shared_memory_offset() const;
339 // Adds a callback to be run when the VideoFrame is about to be destroyed.
340 // The callback may be run from ANY THREAD, and so it is up to the client to
341 // ensure thread safety. Although read-only access to the members of this
342 // VideoFrame is permitted while the callback executes (including
343 // VideoFrameMetadata), clients should not assume the data pointers are
345 void AddDestructionObserver(const base::Closure
& callback
);
347 // Returns a dictionary of optional metadata. This contains information
348 // associated with the frame that downstream clients might use for frame-level
349 // logging, quality/performance optimizations, signaling, etc.
351 // TODO(miu): Move some of the "extra" members of VideoFrame (below) into
352 // here as a later clean-up step.
353 const VideoFrameMetadata
* metadata() const { return &metadata_
; }
354 VideoFrameMetadata
* metadata() { return &metadata_
; }
356 #if defined(OS_LINUX)
357 // Returns backing dmabuf file descriptor for given |plane|, if present.
358 int dmabuf_fd(size_t plane
) const;
361 #if defined(OS_MACOSX)
362 // Returns the backing CVPixelBuffer, if present.
363 CVPixelBufferRef
cv_pixel_buffer() const;
366 base::TimeDelta
timestamp() const { return timestamp_
; }
367 void set_timestamp(base::TimeDelta timestamp
) {
368 timestamp_
= timestamp
;
371 class SyncPointClient
{
374 virtual uint32
InsertSyncPoint() = 0;
375 virtual void WaitSyncPoint(uint32 sync_point
) = 0;
378 virtual ~SyncPointClient() {}
380 DISALLOW_COPY_AND_ASSIGN(SyncPointClient
);
382 // It uses |client| to insert a new sync point and potentially waits on a
383 // older sync point. The final sync point will be used to release this
385 // This method is thread safe. Both blink and compositor threads can call it.
386 void UpdateReleaseSyncPoint(SyncPointClient
* client
);
388 // Used to keep a running hash of seen frames. Expects an initialized MD5
389 // context. Calls MD5Update with the context and the contents of the frame.
390 void HashFrameForTesting(base::MD5Context
* context
);
393 friend class base::RefCountedThreadSafe
<VideoFrame
>;
395 // Clients must use the static CreateFrame() method to create a new frame.
396 VideoFrame(Format format
,
397 StorageType storage_type
,
398 const gfx::Size
& coded_size
,
399 const gfx::Rect
& visible_rect
,
400 const gfx::Size
& natural_size
,
401 base::TimeDelta timestamp
);
402 VideoFrame(Format format
,
403 StorageType storage_type
,
404 const gfx::Size
& coded_size
,
405 const gfx::Rect
& visible_rect
,
406 const gfx::Size
& natural_size
,
407 base::TimeDelta timestamp
,
408 base::SharedMemoryHandle handle
,
409 size_t shared_memory_offset
);
410 VideoFrame(Format format
,
411 StorageType storage_type
,
412 const gfx::Size
& coded_size
,
413 const gfx::Rect
& visible_rect
,
414 const gfx::Size
& natural_size
,
415 const gpu::MailboxHolder(&mailbox_holders
)[kMaxPlanes
],
416 base::TimeDelta timestamp
);
417 virtual ~VideoFrame();
419 static scoped_refptr
<VideoFrame
> WrapExternalStorage(
421 StorageType storage_type
,
422 const gfx::Size
& coded_size
,
423 const gfx::Rect
& visible_rect
,
424 const gfx::Size
& natural_size
,
427 base::TimeDelta timestamp
,
428 base::SharedMemoryHandle handle
,
434 const Format format_
;
436 // Storage type for the different planes.
437 const StorageType storage_type_
;
439 // Width and height of the video frame, in pixels. This must include pixel
440 // data for the whole image; i.e. for YUV formats with subsampled chroma
441 // planes, in the case that the visible portion of the image does not line up
442 // on a sample boundary, |coded_size_| must be rounded up appropriately and
443 // the pixel data provided for the odd pixels.
444 const gfx::Size coded_size_
;
446 // Width, height, and offsets of the visible portion of the video frame. Must
447 // be a subrect of |coded_size_|. Can be odd with respect to the sample
448 // boundaries, e.g. for formats with subsampled chroma.
449 const gfx::Rect visible_rect_
;
451 // Width and height of the visible portion of the video frame
452 // (|visible_rect_.size()|) with aspect ratio taken into account.
453 const gfx::Size natural_size_
;
455 // Array of strides for each plane, typically greater or equal to the width
456 // of the surface divided by the horizontal sampling period. Note that
457 // strides can be negative.
458 int32 strides_
[kMaxPlanes
];
460 // Array of data pointers to each plane.
461 // TODO(mcasas): we don't know on ctor if we own |data_| or not. After
462 // refactoring VideoFrame, change to scoped_ptr<uint8, AlignedFreeDeleter>.
463 uint8
* data_
[kMaxPlanes
];
465 // Native texture mailboxes, if this is a STORAGE_TEXTURE frame.
466 gpu::MailboxHolder mailbox_holders_
[kMaxPlanes
];
467 ReleaseMailboxCB mailbox_holders_release_cb_
;
469 // Shared memory handle and associated offset inside it, if this frame was
470 // constructed as STORAGE_SHMEM.
471 base::SharedMemoryHandle shared_memory_handle_
;
472 size_t shared_memory_offset_
;
474 #if defined(OS_LINUX)
475 // Dmabufs for each plane, if this frame is wrapping memory
476 // acquired via dmabuf.
477 base::ScopedFD dmabuf_fds_
[kMaxPlanes
];
480 #if defined(OS_MACOSX)
481 // CVPixelBuffer, if this frame is wrapping one.
482 base::ScopedCFTypeRef
<CVPixelBufferRef
> cv_pixel_buffer_
;
485 std::vector
<base::Closure
> done_callbacks_
;
487 base::TimeDelta timestamp_
;
489 base::Lock release_sync_point_lock_
;
490 uint32 release_sync_point_
;
492 VideoFrameMetadata metadata_
;
494 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame
);
499 #endif // MEDIA_BASE_VIDEO_FRAME_H_