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 and http://www.fourcc.org/yuv.php
48 // Logged to UMA, so never reuse values. Leave gaps if necessary.
50 UNKNOWN
= 0, // Unknown or unspecified format value.
51 YV12
= 1, // 12bpp YVU planar 1x1 Y, 2x2 VU samples.
52 I420
= 2, // 12bpp YUV planar 1x1 Y, 2x2 UV samples, a.k.a. YU12.
53 YV16
= 3, // 16bpp YVU planar 1x1 Y, 2x1 VU samples.
54 YV12A
= 4, // 20bpp YUVA planar 1x1 Y, 2x2 VU, 1x1 A samples.
55 YV24
= 5, // 24bpp YUV planar, no subsampling.
56 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
57 NV12
= 6, // 12bpp with Y plane followed by a 2x2 interleaved UV plane.
59 ARGB
= 7, // 32bpp ARGB, 1 plane.
60 XRGB
= 8, // 24bpp XRGB, 1 plane.
61 // Please update UMA histogram enumeration when adding new formats here.
62 FORMAT_MAX
= XRGB
, // Must always be equal to largest entry logged.
65 // Color space or color range used for the pixels, in general this is left
66 // unspecified, meaning Rec601 (SD) is assumed.
67 // Logged to UMA, so never reuse values. Leave gaps if necessary.
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. Differentiates between directly accessible
76 // |data_| and pixels that are only indirectly accessible and not via mappable
78 // Note that VideoFrames of any StorageType can also have Texture backing,
79 // with "classical" GPU Driver-only textures identified as STORAGE_OPAQUE.
82 STORAGE_OPAQUE
= 1, // We don't know how VideoFrame's pixels are stored.
83 STORAGE_UNOWNED_MEMORY
= 2, // External, non owned data pointers.
84 STORAGE_OWNED_MEMORY
= 3, // VideoFrame has allocated its own data buffer.
85 STORAGE_SHMEM
= 4, // Pixels are backed by Shared Memory.
87 // TODO(mcasas): Consider turning this type into STORAGE_NATIVE or another
88 // meaningful name and handle it appropriately in all cases.
89 STORAGE_DMABUFS
= 5, // Each plane is stored into a DmaBuf.
91 #if defined(VIDEO_HOLE)
92 // Indicates protected media that needs to be directly rendered to hw. It
93 // is, in principle, platform independent, see http://crbug.com/323157 and
94 // https://groups.google.com/a/google.com/d/topic/chrome-gpu/eIM1RwarUmk/discussion
98 #if defined(VIDEO_HOLE)
99 STORAGE_LAST
= STORAGE_HOLE
,
100 #elif defined(OS_LINUX)
101 STORAGE_LAST
= STORAGE_DMABUFS
,
103 STORAGE_LAST
= STORAGE_SHMEM
107 // CB to be called on the mailbox backing this frame when the frame is
109 typedef base::Callback
<void(uint32
)> ReleaseMailboxCB
;
111 // Interface representing client operations on a SyncPoint, i.e. insert one in
112 // the GPU Command Buffer and wait for it.
113 class SyncPointClient
{
116 virtual uint32
InsertSyncPoint() = 0;
117 virtual void WaitSyncPoint(uint32 sync_point
) = 0;
120 virtual ~SyncPointClient() {}
122 DISALLOW_COPY_AND_ASSIGN(SyncPointClient
);
125 // Returns the name of a Format as a string.
126 static std::string
FormatToString(Format format
);
128 // Returns true if |format| is a YUV format. This includes (multi)planar
129 // and/or (partially) interleaved formats.
130 static bool IsYuvPlanar(Format format
);
132 // Call prior to CreateFrame to ensure validity of frame configuration. Called
133 // automatically by VideoDecoderConfig::IsValidConfig().
134 // TODO(scherkus): VideoDecoderConfig shouldn't call this method
135 static bool IsValidConfig(Format format
,
136 StorageType storage_type
,
137 const gfx::Size
& coded_size
,
138 const gfx::Rect
& visible_rect
,
139 const gfx::Size
& natural_size
);
141 // Creates a new YUV frame in system memory with given parameters (|format|
142 // must be YUV). Buffers for the frame are allocated but not initialized. The
143 // caller most not make assumptions about the actual underlying size(s), but
144 // check the returned VideoFrame instead.
145 // TODO(mcasas): implement the RGB version of this factory method.
146 static scoped_refptr
<VideoFrame
> CreateFrame(
148 const gfx::Size
& coded_size
,
149 const gfx::Rect
& visible_rect
,
150 const gfx::Size
& natural_size
,
151 base::TimeDelta timestamp
);
153 // Wraps a native texture of the given parameters with a VideoFrame.
154 // The backing of the VideoFrame is held in the mailbox held by
155 // |mailbox_holder|, and |mailbox_holder_release_cb| will be called with
156 // a syncpoint as the argument when the VideoFrame is to be destroyed.
157 static scoped_refptr
<VideoFrame
> WrapNativeTexture(
159 const gpu::MailboxHolder
& mailbox_holder
,
160 const ReleaseMailboxCB
& mailbox_holder_release_cb
,
161 const gfx::Size
& coded_size
,
162 const gfx::Rect
& visible_rect
,
163 const gfx::Size
& natural_size
,
164 base::TimeDelta timestamp
);
166 // Wraps a set of native textures representing YUV data with a VideoFrame.
167 // |mailbox_holders_release_cb| will be called with a syncpoint as the
168 // argument when the VideoFrame is to be destroyed.
169 static scoped_refptr
<VideoFrame
> WrapYUV420NativeTextures(
170 const gpu::MailboxHolder
& y_mailbox_holder
,
171 const gpu::MailboxHolder
& u_mailbox_holder
,
172 const gpu::MailboxHolder
& v_mailbox_holder
,
173 const ReleaseMailboxCB
& mailbox_holders_release_cb
,
174 const gfx::Size
& coded_size
,
175 const gfx::Rect
& visible_rect
,
176 const gfx::Size
& natural_size
,
177 base::TimeDelta timestamp
);
179 // Wraps packed image data residing in a memory buffer with a VideoFrame.
180 // The image data resides in |data| and is assumed to be packed tightly in a
181 // buffer of logical dimensions |coded_size| with the appropriate bit depth
182 // and plane count as given by |format|. Returns NULL on failure.
183 static scoped_refptr
<VideoFrame
> WrapExternalData(
185 const gfx::Size
& coded_size
,
186 const gfx::Rect
& visible_rect
,
187 const gfx::Size
& natural_size
,
190 base::TimeDelta timestamp
);
192 // Same as WrapExternalData() with SharedMemoryHandle and its offset.
193 static scoped_refptr
<VideoFrame
> WrapExternalSharedMemory(
195 const gfx::Size
& coded_size
,
196 const gfx::Rect
& visible_rect
,
197 const gfx::Size
& natural_size
,
200 base::SharedMemoryHandle handle
,
201 size_t shared_memory_offset
,
202 base::TimeDelta timestamp
);
204 // Wraps external YUV data of the given parameters with a VideoFrame.
205 // The returned VideoFrame does not own the data passed in.
206 static scoped_refptr
<VideoFrame
> WrapExternalYuvData(
208 const gfx::Size
& coded_size
,
209 const gfx::Rect
& visible_rect
,
210 const gfx::Size
& natural_size
,
217 base::TimeDelta timestamp
);
219 #if defined(OS_LINUX)
220 // Wraps provided dmabufs
221 // (https://www.kernel.org/doc/Documentation/dma-buf-sharing.txt) with a
222 // VideoFrame. The dmabuf fds are dup()ed on creation, so that the VideoFrame
223 // retains a reference to them, and are automatically close()d on destruction,
224 // dropping the reference. The caller may safely close() its reference after
225 // calling WrapExternalDmabufs().
226 // The image data is only accessible via dmabuf fds, which are usually passed
227 // directly to a hardware device and/or to another process, or can also be
228 // mapped via mmap() for CPU access.
229 // Returns NULL on failure.
230 static scoped_refptr
<VideoFrame
> WrapExternalDmabufs(
232 const gfx::Size
& coded_size
,
233 const gfx::Rect
& visible_rect
,
234 const gfx::Size
& natural_size
,
235 const std::vector
<int>& dmabuf_fds
,
236 base::TimeDelta timestamp
);
239 #if defined(OS_MACOSX)
240 // Wraps a provided CVPixelBuffer with a VideoFrame. The pixel buffer is
241 // retained for the lifetime of the VideoFrame and released upon destruction.
242 // The image data is only accessible via the pixel buffer, which could be
243 // backed by an IOSurface from another process. All the attributes of the
244 // VideoFrame are derived from the pixel buffer, with the exception of the
245 // timestamp. If information is missing or is incompatible (for example, a
246 // pixel format that has no VideoFrame match), NULL is returned.
247 // http://crbug.com/401308
248 static scoped_refptr
<VideoFrame
> WrapCVPixelBuffer(
249 CVPixelBufferRef cv_pixel_buffer
,
250 base::TimeDelta timestamp
);
253 // Wraps |frame|. |visible_rect| must be a sub rect within
254 // frame->visible_rect().
255 static scoped_refptr
<VideoFrame
> WrapVideoFrame(
256 const scoped_refptr
<VideoFrame
>& frame
,
257 const gfx::Rect
& visible_rect
,
258 const gfx::Size
& natural_size
);
260 // Creates a frame which indicates end-of-stream.
261 static scoped_refptr
<VideoFrame
> CreateEOSFrame();
263 // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v).
264 static scoped_refptr
<VideoFrame
> CreateColorFrame(
265 const gfx::Size
& size
,
266 uint8 y
, uint8 u
, uint8 v
,
267 base::TimeDelta timestamp
);
269 // Allocates YV12 frame based on |size|, and sets its data to the YUV
270 // equivalent of RGB(0,0,0).
271 static scoped_refptr
<VideoFrame
> CreateBlackFrame(const gfx::Size
& size
);
273 // Allocates YV12A frame based on |size|, and sets its data to the YUVA
274 // equivalent of RGBA(0,0,0,0).
275 static scoped_refptr
<VideoFrame
> CreateTransparentFrame(
276 const gfx::Size
& size
);
278 #if defined(VIDEO_HOLE)
279 // Allocates a hole frame.
280 static scoped_refptr
<VideoFrame
> CreateHoleFrame(const gfx::Size
& size
);
281 #endif // defined(VIDEO_HOLE)
283 static size_t NumPlanes(Format format
);
285 // Returns the required allocation size for a (tightly packed) frame of the
286 // given coded size and format.
287 static size_t AllocationSize(Format format
, const gfx::Size
& coded_size
);
289 // Returns the plane gfx::Size (in bytes) for a plane of the given coded size
291 static gfx::Size
PlaneSize(Format format
,
293 const gfx::Size
& coded_size
);
295 // Returns horizontal bits per pixel for given |plane| and |format|.
296 static int PlaneHorizontalBitsPerPixel(Format format
, size_t plane
);
298 // Returns bits per pixel for given |plane| and |format|.
299 static int PlaneBitsPerPixel(Format format
, size_t plane
);
301 // Returns the number of bytes per row for the given plane, format, and width.
302 // The width may be aligned to format requirements.
303 static size_t RowBytes(size_t plane
, Format format
, int width
);
305 // Returns the number of rows for the given plane, format, and height.
306 // The height may be aligned to format requirements.
307 static size_t Rows(size_t plane
, Format format
, int height
);
309 // Returns the number of columns for the given plane, format, and width.
310 // The width may be aligned to format requirements.
311 static size_t Columns(size_t plane
, Format format
, int width
);
313 // Used to keep a running hash of seen frames. Expects an initialized MD5
314 // context. Calls MD5Update with the context and the contents of the frame.
315 static void HashFrameForTesting(base::MD5Context
* context
,
316 const scoped_refptr
<VideoFrame
>& frame
);
318 // Returns true if |frame| is accessible and mapped in the VideoFrame memory
319 // space. If false, clients should refrain from accessing data(),
320 // visible_data() etc.
321 bool IsMappable() const;
323 // Returns true if |frame| has textures with any StorageType and should not be
324 // accessed via data(), visible_data() etc.
325 bool HasTextures() const;
327 Format
format() const { return format_
; }
328 StorageType
storage_type() const { return storage_type_
; }
330 const gfx::Size
& coded_size() const { return coded_size_
; }
331 const gfx::Rect
& visible_rect() const { return visible_rect_
; }
332 const gfx::Size
& natural_size() const { return natural_size_
; }
334 int stride(size_t plane
) const;
336 // Returns the number of bytes per row and number of rows for a given plane.
338 // As opposed to stride(), row_bytes() refers to the bytes representing
339 // frame data scanlines (coded_size.width() pixels, without stride padding).
340 int row_bytes(size_t plane
) const;
341 int rows(size_t plane
) const;
343 // Returns pointer to the buffer for a given plane, if this is an
344 // IsMappable() frame type. The memory is owned by VideoFrame object and must
345 // not be freed by the caller.
346 const uint8
* data(size_t plane
) const;
347 uint8
* data(size_t plane
);
349 // Returns pointer to the data in the visible region of the frame, for
350 // IsMappable() storage types. The returned pointer is offsetted into the
351 // plane buffer specified by visible_rect().origin(). Memory is owned by
352 // VideoFrame object and must not be freed by the caller.
353 const uint8
* visible_data(size_t plane
) const;
354 uint8
* visible_data(size_t plane
);
356 // Returns a mailbox holder for a given texture.
357 // Only valid to call if this is a NATIVE_TEXTURE frame. Before using the
358 // mailbox, the caller must wait for the included sync point.
359 const gpu::MailboxHolder
& mailbox_holder(size_t texture_index
) const;
361 // Returns the shared-memory handle, if present
362 base::SharedMemoryHandle
shared_memory_handle() const;
364 // Returns the offset into the shared memory where the frame data begins.
365 size_t shared_memory_offset() const;
367 #if defined(OS_LINUX)
368 // Returns backing DmaBuf file descriptor for given |plane|, if present, or
370 // TODO(mcasas): Rename to DmabufFd() to comply with Style Guide.
371 int dmabuf_fd(size_t plane
) const;
373 // Duplicates internally the |fds_in|, overwriting the current ones. Returns
374 // false if something goes wrong, and leaves all internal fds closed.
375 bool DuplicateFileDescriptors(const std::vector
<int>& fds_in
);
378 void AddSharedMemoryHandle(base::SharedMemoryHandle handle
);
380 #if defined(OS_MACOSX)
381 // Returns the backing CVPixelBuffer, if present.
382 // TODO(mcasas): Rename to CvPixelBuffer() to comply with Style Guide.
383 CVPixelBufferRef
cv_pixel_buffer() const;
386 // Adds a callback to be run when the VideoFrame is about to be destroyed.
387 // The callback may be run from ANY THREAD, and so it is up to the client to
388 // ensure thread safety. Although read-only access to the members of this
389 // VideoFrame is permitted while the callback executes (including
390 // VideoFrameMetadata), clients should not assume the data pointers are
392 void AddDestructionObserver(const base::Closure
& callback
);
394 // Returns a dictionary of optional metadata. This contains information
395 // associated with the frame that downstream clients might use for frame-level
396 // logging, quality/performance optimizations, signaling, etc.
398 // TODO(miu): Move some of the "extra" members of VideoFrame (below) into
399 // here as a later clean-up step.
400 const VideoFrameMetadata
* metadata() const { return &metadata_
; }
401 VideoFrameMetadata
* metadata() { return &metadata_
; }
403 base::TimeDelta
timestamp() const { return timestamp_
; }
404 void set_timestamp(base::TimeDelta timestamp
) {
405 timestamp_
= timestamp
;
408 // It uses |client| to insert a new sync point and potentially waits on a
409 // older sync point. The final sync point will be used to release this
411 // This method is thread safe. Both blink and compositor threads can call it.
412 void UpdateReleaseSyncPoint(SyncPointClient
* client
);
415 friend class base::RefCountedThreadSafe
<VideoFrame
>;
417 static scoped_refptr
<VideoFrame
> WrapExternalStorage(
419 StorageType storage_type
,
420 const gfx::Size
& coded_size
,
421 const gfx::Rect
& visible_rect
,
422 const gfx::Size
& natural_size
,
425 base::TimeDelta timestamp
,
426 base::SharedMemoryHandle handle
,
429 // Clients must use the static factory/wrapping methods to create a new frame.
430 VideoFrame(Format format
,
431 StorageType storage_type
,
432 const gfx::Size
& coded_size
,
433 const gfx::Rect
& visible_rect
,
434 const gfx::Size
& natural_size
,
435 base::TimeDelta timestamp
);
436 VideoFrame(Format format
,
437 StorageType storage_type
,
438 const gfx::Size
& coded_size
,
439 const gfx::Rect
& visible_rect
,
440 const gfx::Size
& natural_size
,
441 base::TimeDelta timestamp
,
442 base::SharedMemoryHandle handle
,
443 size_t shared_memory_offset
);
444 VideoFrame(Format format
,
445 StorageType storage_type
,
446 const gfx::Size
& coded_size
,
447 const gfx::Rect
& visible_rect
,
448 const gfx::Size
& natural_size
,
449 const gpu::MailboxHolder(&mailbox_holders
)[kMaxPlanes
],
450 const ReleaseMailboxCB
& mailbox_holder_release_cb
,
451 base::TimeDelta timestamp
);
452 virtual ~VideoFrame();
457 const Format format_
;
459 // Storage type for the different planes.
460 StorageType storage_type_
; // TODO(mcasas): make const
462 // Width and height of the video frame, in pixels. This must include pixel
463 // data for the whole image; i.e. for YUV formats with subsampled chroma
464 // planes, in the case that the visible portion of the image does not line up
465 // on a sample boundary, |coded_size_| must be rounded up appropriately and
466 // the pixel data provided for the odd pixels.
467 const gfx::Size coded_size_
;
469 // Width, height, and offsets of the visible portion of the video frame. Must
470 // be a subrect of |coded_size_|. Can be odd with respect to the sample
471 // boundaries, e.g. for formats with subsampled chroma.
472 const gfx::Rect visible_rect_
;
474 // Width and height of the visible portion of the video frame
475 // (|visible_rect_.size()|) with aspect ratio taken into account.
476 const gfx::Size natural_size_
;
478 // Array of strides for each plane, typically greater or equal to the width
479 // of the surface divided by the horizontal sampling period. Note that
480 // strides can be negative.
481 int32 strides_
[kMaxPlanes
];
483 // Array of data pointers to each plane.
484 // TODO(mcasas): we don't know on ctor if we own |data_| or not. After
485 // refactoring VideoFrame, change to scoped_ptr<uint8, AlignedFreeDeleter>.
486 uint8
* data_
[kMaxPlanes
];
488 // Native texture mailboxes, if this is a IsTexture() frame.
489 gpu::MailboxHolder mailbox_holders_
[kMaxPlanes
];
490 ReleaseMailboxCB mailbox_holders_release_cb_
;
492 // Shared memory handle and associated offset inside it, if this frame is
493 // a STORAGE_SHMEM one.
494 base::SharedMemoryHandle shared_memory_handle_
;
495 size_t shared_memory_offset_
;
497 #if defined(OS_LINUX)
498 // Dmabufs for each plane. If set, this frame has DmaBuf backing in some way.
499 base::ScopedFD dmabuf_fds_
[kMaxPlanes
];
502 #if defined(OS_MACOSX)
503 // CVPixelBuffer, if this frame is wrapping one.
504 base::ScopedCFTypeRef
<CVPixelBufferRef
> cv_pixel_buffer_
;
507 std::vector
<base::Closure
> done_callbacks_
;
509 base::TimeDelta timestamp_
;
511 base::Lock release_sync_point_lock_
;
512 uint32 release_sync_point_
;
514 VideoFrameMetadata metadata_
;
516 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame
);
521 #endif // MEDIA_BASE_VIDEO_FRAME_H_