Remove obsolete for_web_contents parameter in FontRenderParamsQuery.
[chromium-blink-merge.git] / media / base / video_frame.h
blobcebed0af11a47aea5537b92346d97cebae1ef05f
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_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/md5.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/synchronization/lock.h"
14 #include "media/base/buffers.h"
15 #include "media/base/video_frame_metadata.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/geometry/size.h"
19 #if defined(OS_MACOSX)
20 #include <CoreVideo/CVPixelBuffer.h>
21 #include "base/mac/scoped_cftyperef.h"
22 #endif
24 namespace gpu {
25 struct MailboxHolder;
26 } // namespace gpu
28 namespace media {
30 class MEDIA_EXPORT VideoFrame : public base::RefCountedThreadSafe<VideoFrame> {
31 public:
32 enum {
33 kFrameSizeAlignment = 16,
34 kFrameSizePadding = 16,
35 kFrameAddressAlignment = 32
38 enum {
39 kMaxPlanes = 4,
41 kYPlane = 0,
42 kARGBPlane = kYPlane,
43 kUPlane = 1,
44 kUVPlane = kUPlane,
45 kVPlane = 2,
46 kAPlane = 3,
49 // Surface formats roughly based on FOURCC labels, see:
50 // http://www.fourcc.org/rgb.php
51 // http://www.fourcc.org/yuv.php
52 // Logged to UMA, so never reuse values.
53 enum Format {
54 UNKNOWN = 0, // Unknown format value.
55 YV12 = 1, // 12bpp YVU planar 1x1 Y, 2x2 VU samples
56 YV16 = 2, // 16bpp YVU planar 1x1 Y, 2x1 VU samples
57 I420 = 3, // 12bpp YVU planar 1x1 Y, 2x2 UV samples.
58 YV12A = 4, // 20bpp YUVA planar 1x1 Y, 2x2 VU, 1x1 A samples.
59 #if defined(VIDEO_HOLE)
60 HOLE = 5, // Hole frame.
61 #endif // defined(VIDEO_HOLE)
62 NATIVE_TEXTURE = 6, // Native texture. Pixel-format agnostic.
63 YV12J = 7, // JPEG color range version of YV12
64 NV12 = 8, // 12bpp 1x1 Y plane followed by an interleaved 2x2 UV plane.
65 YV24 = 9, // 24bpp YUV planar, no subsampling.
66 ARGB = 10, // 32bpp ARGB, 1 plane.
67 YV12HD = 11, // Rec709 "HD" color space version of YV12
68 // Please update UMA histogram enumeration when adding new formats here.
69 FORMAT_MAX = YV12HD, // Must always be equal to largest entry logged.
72 // Returns the name of a Format as a string.
73 static std::string FormatToString(Format format);
75 // Creates a new frame in system memory with given parameters. Buffers for
76 // the frame are allocated but not initialized.
77 static scoped_refptr<VideoFrame> CreateFrame(
78 Format format,
79 const gfx::Size& coded_size,
80 const gfx::Rect& visible_rect,
81 const gfx::Size& natural_size,
82 base::TimeDelta timestamp);
84 // Returns true if |plane| is a valid plane number for the given format. This
85 // can be used to DCHECK() plane parameters.
86 static bool IsValidPlane(size_t plane, VideoFrame::Format format);
88 // Call prior to CreateFrame to ensure validity of frame configuration. Called
89 // automatically by VideoDecoderConfig::IsValidConfig().
90 // TODO(scherkus): VideoDecoderConfig shouldn't call this method
91 static bool IsValidConfig(Format format, const gfx::Size& coded_size,
92 const gfx::Rect& visible_rect,
93 const gfx::Size& natural_size);
95 // CB to be called on the mailbox backing this frame when the frame is
96 // destroyed.
97 typedef base::Callback<void(uint32)> ReleaseMailboxCB;
99 // Wraps a native texture of the given parameters with a VideoFrame. The
100 // backing of the VideoFrame is held in the mailbox held by |mailbox_holder|,
101 // and |mailbox_holder_release_cb| will be called with |mailbox_holder| as the
102 // argument when the VideoFrame is to be destroyed.
103 // |read_pixels_cb| may be used to do (slow!) readbacks from the
104 // texture to main memory.
105 static scoped_refptr<VideoFrame> WrapNativeTexture(
106 scoped_ptr<gpu::MailboxHolder> mailbox_holder,
107 const ReleaseMailboxCB& mailbox_holder_release_cb,
108 const gfx::Size& coded_size,
109 const gfx::Rect& visible_rect,
110 const gfx::Size& natural_size,
111 base::TimeDelta timestamp,
112 bool allow_overlay);
114 // Wraps packed image data residing in a memory buffer with a VideoFrame.
115 // The image data resides in |data| and is assumed to be packed tightly in a
116 // buffer of logical dimensions |coded_size| with the appropriate bit depth
117 // and plane count as given by |format|. The shared memory handle of the
118 // backing allocation, if present, can be passed in with |handle|. When the
119 // frame is destroyed, |no_longer_needed_cb.Run()| will be called.
120 // Returns NULL on failure.
121 static scoped_refptr<VideoFrame> WrapExternalPackedMemory(
122 Format format,
123 const gfx::Size& coded_size,
124 const gfx::Rect& visible_rect,
125 const gfx::Size& natural_size,
126 uint8* data,
127 size_t data_size,
128 base::SharedMemoryHandle handle,
129 size_t shared_memory_offset,
130 base::TimeDelta timestamp,
131 const base::Closure& no_longer_needed_cb);
133 // Wraps external YUV data of the given parameters with a VideoFrame.
134 // The returned VideoFrame does not own the data passed in. When the frame
135 // is destroyed |no_longer_needed_cb.Run()| will be called.
136 static scoped_refptr<VideoFrame> WrapExternalYuvData(
137 Format format,
138 const gfx::Size& coded_size,
139 const gfx::Rect& visible_rect,
140 const gfx::Size& natural_size,
141 int32 y_stride,
142 int32 u_stride,
143 int32 v_stride,
144 uint8* y_data,
145 uint8* u_data,
146 uint8* v_data,
147 base::TimeDelta timestamp,
148 const base::Closure& no_longer_needed_cb);
150 #if defined(OS_POSIX)
151 // Wraps provided dmabufs
152 // (https://www.kernel.org/doc/Documentation/dma-buf-sharing.txt) with a
153 // VideoFrame. The dmabuf fds are dup()ed on creation, so that the VideoFrame
154 // retains a reference to them, and are automatically close()d on destruction,
155 // dropping the reference. The caller may safely close() its reference after
156 // calling WrapExternalDmabufs().
157 // The image data is only accessible via dmabuf fds, which are usually passed
158 // directly to a hardware device and/or to another process, or can also be
159 // mapped via mmap() for CPU access.
160 // When the frame is destroyed, |no_longer_needed_cb.Run()| will be called.
161 // Returns NULL on failure.
162 static scoped_refptr<VideoFrame> WrapExternalDmabufs(
163 Format format,
164 const gfx::Size& coded_size,
165 const gfx::Rect& visible_rect,
166 const gfx::Size& natural_size,
167 const std::vector<int> dmabuf_fds,
168 base::TimeDelta timestamp,
169 const base::Closure& no_longer_needed_cb);
170 #endif
172 #if defined(OS_MACOSX)
173 // Wraps a provided CVPixelBuffer with a VideoFrame. The pixel buffer is
174 // retained for the lifetime of the VideoFrame and released upon destruction.
175 // The image data is only accessible via the pixel buffer, which could be
176 // backed by an IOSurface from another process. All the attributes of the
177 // VideoFrame are derived from the pixel buffer, with the exception of the
178 // timestamp. If information is missing or is incompatible (for example, a
179 // pixel format that has no VideoFrame match), NULL is returned.
180 // http://crbug.com/401308
181 static scoped_refptr<VideoFrame> WrapCVPixelBuffer(
182 CVPixelBufferRef cv_pixel_buffer,
183 base::TimeDelta timestamp);
184 #endif
186 // Wraps |frame| and calls |no_longer_needed_cb| when the wrapper VideoFrame
187 // gets destroyed. |visible_rect| must be a sub rect within
188 // frame->visible_rect().
189 static scoped_refptr<VideoFrame> WrapVideoFrame(
190 const scoped_refptr<VideoFrame>& frame,
191 const gfx::Rect& visible_rect,
192 const gfx::Size& natural_size,
193 const base::Closure& no_longer_needed_cb);
195 // Creates a frame which indicates end-of-stream.
196 static scoped_refptr<VideoFrame> CreateEOSFrame();
198 // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v).
199 static scoped_refptr<VideoFrame> CreateColorFrame(
200 const gfx::Size& size,
201 uint8 y, uint8 u, uint8 v,
202 base::TimeDelta timestamp);
204 // Allocates YV12 frame based on |size|, and sets its data to the YUV
205 // equivalent of RGB(0,0,0).
206 static scoped_refptr<VideoFrame> CreateBlackFrame(const gfx::Size& size);
208 // Allocates YV12A frame based on |size|, and sets its data to the YUVA
209 // equivalent of RGBA(0,0,0,0).
210 static scoped_refptr<VideoFrame> CreateTransparentFrame(
211 const gfx::Size& size);
213 #if defined(VIDEO_HOLE)
214 // Allocates a hole frame.
215 static scoped_refptr<VideoFrame> CreateHoleFrame(const gfx::Size& size);
216 #endif // defined(VIDEO_HOLE)
218 static size_t NumPlanes(Format format);
220 // Returns the required allocation size for a (tightly packed) frame of the
221 // given coded size and format.
222 static size_t AllocationSize(Format format, const gfx::Size& coded_size);
224 // Returns the plane size (in bytes) for a plane of the given coded size and
225 // format.
226 static gfx::Size PlaneSize(Format format,
227 size_t plane,
228 const gfx::Size& coded_size);
230 // Returns the required allocation size for a (tightly packed) plane of the
231 // given coded size and format.
232 static size_t PlaneAllocationSize(Format format,
233 size_t plane,
234 const gfx::Size& coded_size);
236 // Returns horizontal bits per pixel for given |plane| and |format|.
237 static int PlaneHorizontalBitsPerPixel(Format format, size_t plane);
239 // Returns bits per pixel for given |plane| and |format|.
240 static int PlaneBitsPerPixel(Format format, size_t plane);
242 // Returns the number of bytes per row for the given plane, format, and width.
243 // The width may be aligned to format requirements.
244 static size_t RowBytes(size_t plane, Format format, int width);
246 // Returns the number of rows for the given plane, format, and height.
247 // The height may be aligned to format requirements.
248 static size_t Rows(size_t plane, Format format, int height);
250 // Returns the number of columns for the given plane, format, and width.
251 // The width may be aligned to format requirements.
252 static size_t Columns(size_t plane, Format format, int width);
254 Format format() const { return format_; }
256 const gfx::Size& coded_size() const { return coded_size_; }
257 const gfx::Rect& visible_rect() const { return visible_rect_; }
258 const gfx::Size& natural_size() const { return natural_size_; }
260 int stride(size_t plane) const;
262 // Returns the number of bytes per row and number of rows for a given plane.
264 // As opposed to stride(), row_bytes() refers to the bytes representing
265 // frame data scanlines (coded_size.width() pixels, without stride padding).
266 int row_bytes(size_t plane) const;
267 int rows(size_t plane) const;
269 // Returns pointer to the buffer for a given plane. The memory is owned by
270 // VideoFrame object and must not be freed by the caller.
271 const uint8* data(size_t plane) const;
272 uint8* data(size_t plane);
274 // Returns pointer to the data in the visible region of the frame. I.e. the
275 // returned pointer is offsetted into the plane buffer specified by
276 // visible_rect().origin(). Memory is owned by VideoFrame object and must not
277 // be freed by the caller.
278 const uint8* visible_data(size_t plane) const;
279 uint8* visible_data(size_t plane);
281 // Returns the mailbox holder of the native texture wrapped by this frame.
282 // Only valid to call if this is a NATIVE_TEXTURE frame. Before using the
283 // mailbox, the caller must wait for the included sync point.
284 const gpu::MailboxHolder* mailbox_holder() const;
286 // Returns the shared-memory handle, if present
287 base::SharedMemoryHandle shared_memory_handle() const;
289 // Returns the offset into the shared memory where the frame data begins.
290 size_t shared_memory_offset() const;
292 // Returns a dictionary of optional metadata. This contains information
293 // associated with the frame that downstream clients might use for frame-level
294 // logging, quality/performance optimizations, signaling, etc.
296 // TODO(miu): Move some of the "extra" members of VideoFrame (below) into
297 // here as a later clean-up step.
298 const VideoFrameMetadata* metadata() const { return &metadata_; }
299 VideoFrameMetadata* metadata() { return &metadata_; }
301 bool allow_overlay() const { return allow_overlay_; }
303 #if defined(OS_POSIX)
304 // Returns backing dmabuf file descriptor for given |plane|, if present.
305 int dmabuf_fd(size_t plane) const;
306 #endif
308 #if defined(OS_MACOSX)
309 // Returns the backing CVPixelBuffer, if present.
310 CVPixelBufferRef cv_pixel_buffer() const;
311 #endif
313 // Returns true if this VideoFrame represents the end of the stream.
314 bool end_of_stream() const { return end_of_stream_; }
316 base::TimeDelta timestamp() const {
317 return timestamp_;
319 void set_timestamp(const base::TimeDelta& timestamp) {
320 timestamp_ = timestamp;
323 class SyncPointClient {
324 public:
325 SyncPointClient() {}
326 virtual uint32 InsertSyncPoint() = 0;
327 virtual void WaitSyncPoint(uint32 sync_point) = 0;
329 protected:
330 virtual ~SyncPointClient() {}
332 DISALLOW_COPY_AND_ASSIGN(SyncPointClient);
334 // It uses |client| to insert a new sync point and potentially waits on a
335 // older sync point. The final sync point will be used to release this
336 // VideoFrame.
337 // This method is thread safe. Both blink and compositor threads can call it.
338 void UpdateReleaseSyncPoint(SyncPointClient* client);
340 // Used to keep a running hash of seen frames. Expects an initialized MD5
341 // context. Calls MD5Update with the context and the contents of the frame.
342 void HashFrameForTesting(base::MD5Context* context);
344 private:
345 friend class base::RefCountedThreadSafe<VideoFrame>;
347 // Clients must use the static CreateFrame() method to create a new frame.
348 VideoFrame(Format format,
349 const gfx::Size& coded_size,
350 const gfx::Rect& visible_rect,
351 const gfx::Size& natural_size,
352 scoped_ptr<gpu::MailboxHolder> mailbox_holder,
353 base::TimeDelta timestamp,
354 bool end_of_stream);
355 virtual ~VideoFrame();
357 void AllocateYUV();
359 // Frame format.
360 const Format format_;
362 // Width and height of the video frame, in pixels. This must include pixel
363 // data for the whole image; i.e. for YUV formats with subsampled chroma
364 // planes, in the case that the visible portion of the image does not line up
365 // on a sample boundary, |coded_size_| must be rounded up appropriately and
366 // the pixel data provided for the odd pixels.
367 const gfx::Size coded_size_;
369 // Width, height, and offsets of the visible portion of the video frame. Must
370 // be a subrect of |coded_size_|. Can be odd with respect to the sample
371 // boundaries, e.g. for formats with subsampled chroma.
372 const gfx::Rect visible_rect_;
374 // Width and height of the visible portion of the video frame
375 // (|visible_rect_.size()|) with aspect ratio taken into account.
376 const gfx::Size natural_size_;
378 // Array of strides for each plane, typically greater or equal to the width
379 // of the surface divided by the horizontal sampling period. Note that
380 // strides can be negative.
381 int32 strides_[kMaxPlanes];
383 // Array of data pointers to each plane.
384 uint8* data_[kMaxPlanes];
386 // Native texture mailbox, if this is a NATIVE_TEXTURE frame.
387 const scoped_ptr<gpu::MailboxHolder> mailbox_holder_;
388 ReleaseMailboxCB mailbox_holder_release_cb_;
390 // Shared memory handle, if this frame was allocated from shared memory.
391 base::SharedMemoryHandle shared_memory_handle_;
393 // Offset in shared memory buffer.
394 size_t shared_memory_offset_;
396 #if defined(OS_POSIX)
397 // Dmabufs for each plane, if this frame is wrapping memory
398 // acquired via dmabuf.
399 base::ScopedFD dmabuf_fds_[kMaxPlanes];
400 #endif
402 #if defined(OS_MACOSX)
403 // CVPixelBuffer, if this frame is wrapping one.
404 base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer_;
405 #endif
407 base::Closure no_longer_needed_cb_;
409 base::TimeDelta timestamp_;
411 base::Lock release_sync_point_lock_;
412 uint32 release_sync_point_;
414 const bool end_of_stream_;
416 VideoFrameMetadata metadata_;
418 bool allow_overlay_;
420 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame);
423 } // namespace media
425 #endif // MEDIA_BASE_VIDEO_FRAME_H_