Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / media / base / video_frame.h
blobdd3fcc0aac308a18499dbf8fac26513df3ad8f26
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 "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"
23 #endif
25 namespace media {
27 class MEDIA_EXPORT VideoFrame : public base::RefCountedThreadSafe<VideoFrame> {
28 public:
29 enum {
30 kFrameSizeAlignment = 16,
31 kFrameSizePadding = 16,
32 kFrameAddressAlignment = 32
35 enum {
36 kMaxPlanes = 4,
38 kYPlane = 0,
39 kARGBPlane = kYPlane,
40 kUPlane = 1,
41 kUVPlane = kUPlane,
42 kVPlane = 2,
43 kAPlane = 3,
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.
50 enum Format {
51 UNKNOWN = 0, // Unknown format value.
52 YV12 = 1, // 12bpp YVU planar 1x1 Y, 2x2 VU samples.
53 I420 = 2, // 12bpp YVU 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 NV12 = 6, // 12bpp 1x1 Y plane followed by an interleaved 2x2 UV plane.
58 ARGB = 7, // 32bpp ARGB, 1 plane.
59 #if defined(VIDEO_HOLE)
60 HOLE = 8, // Hole frame.
61 #endif
62 NATIVE_TEXTURE = 9, // Native texture. Pixel-format agnostic.
63 // Please update UMA histogram enumeration when adding new formats here.
64 // Must always be equal to largest entry logged.
65 FORMAT_MAX = NATIVE_TEXTURE,
68 // Defines the internal format and the number of the textures in the mailbox
69 // holders.
70 enum TextureFormat {
71 TEXTURE_RGBA, // One RGBA texture.
72 TEXTURE_RGB, // One RGB texture.
73 TEXTURE_YUV_420, // 3 RED textures one per channel. UV are 2x2 subsampled.
76 // Color space or color range used for the pixels, in general this is left
77 // unspecified, meaning SD is assumed.
78 enum ColorSpace {
79 COLOR_SPACE_UNSPECIFIED = 0, // In general this is Rec601.
80 COLOR_SPACE_JPEG = 1, // JPEG color range.
81 COLOR_SPACE_HD_REC709 = 2, // Rec709 "HD" color space.
82 COLOR_SPACE_MAX = COLOR_SPACE_HD_REC709,
85 // Returns the name of a Format as a string.
86 static std::string FormatToString(Format format);
88 // Creates a new frame in system memory with given parameters. Buffers for
89 // the frame are allocated but not initialized.
90 static scoped_refptr<VideoFrame> CreateFrame(
91 Format format,
92 const gfx::Size& coded_size,
93 const gfx::Rect& visible_rect,
94 const gfx::Size& natural_size,
95 base::TimeDelta timestamp);
97 // Returns true if |plane| is a valid plane number for the given format. This
98 // can be used to DCHECK() plane parameters.
99 static bool IsValidPlane(size_t plane, VideoFrame::Format format);
101 // Call prior to CreateFrame to ensure validity of frame configuration. Called
102 // automatically by VideoDecoderConfig::IsValidConfig().
103 // TODO(scherkus): VideoDecoderConfig shouldn't call this method
104 static bool IsValidConfig(Format format,
105 const gfx::Size& coded_size,
106 const gfx::Rect& visible_rect,
107 const gfx::Size& natural_size);
109 // CB to be called on the mailbox backing this frame when the frame is
110 // destroyed.
111 typedef base::Callback<void(uint32)> ReleaseMailboxCB;
113 // Wraps a native texture of the given parameters with a VideoFrame.
114 // The backing of the VideoFrame is held in the mailbox held by
115 // |mailbox_holder|, and |mailbox_holder_release_cb| will be called with
116 // a syncpoint as the argument when the VideoFrame is to be destroyed.
117 static scoped_refptr<VideoFrame> WrapNativeTexture(
118 const gpu::MailboxHolder& mailbox_holder,
119 const ReleaseMailboxCB& mailbox_holder_release_cb,
120 const gfx::Size& coded_size,
121 const gfx::Rect& visible_rect,
122 const gfx::Size& natural_size,
123 base::TimeDelta timestamp,
124 bool allow_overlay,
125 bool has_alpha);
127 // Wraps a set of native textures representing YUV data with a VideoFrame.
128 // |mailbox_holders_release_cb| will be called with a syncpoint as the
129 // argument when the VideoFrame is to be destroyed.
130 static scoped_refptr<VideoFrame> WrapYUV420NativeTextures(
131 const gpu::MailboxHolder& y_mailbox_holder,
132 const gpu::MailboxHolder& u_mailbox_holder,
133 const gpu::MailboxHolder& v_mailbox_holder,
134 const ReleaseMailboxCB& mailbox_holders_release_cb,
135 const gfx::Size& coded_size,
136 const gfx::Rect& visible_rect,
137 const gfx::Size& natural_size,
138 base::TimeDelta timestamp,
139 bool allow_overlay);
141 // Wraps packed image data residing in a memory buffer with a VideoFrame.
142 // The image data resides in |data| and is assumed to be packed tightly in a
143 // buffer of logical dimensions |coded_size| with the appropriate bit depth
144 // and plane count as given by |format|. The shared memory handle of the
145 // backing allocation, if present, can be passed in with |handle|.
146 // Returns NULL on failure.
147 static scoped_refptr<VideoFrame> WrapExternalPackedMemory(
148 Format format,
149 const gfx::Size& coded_size,
150 const gfx::Rect& visible_rect,
151 const gfx::Size& natural_size,
152 uint8* data,
153 size_t data_size,
154 base::SharedMemoryHandle handle,
155 size_t shared_memory_offset,
156 base::TimeDelta timestamp);
158 // Wraps external YUV data of the given parameters with a VideoFrame.
159 // The returned VideoFrame does not own the data passed in.
160 static scoped_refptr<VideoFrame> WrapExternalYuvData(
161 Format format,
162 const gfx::Size& coded_size,
163 const gfx::Rect& visible_rect,
164 const gfx::Size& natural_size,
165 int32 y_stride,
166 int32 u_stride,
167 int32 v_stride,
168 uint8* y_data,
169 uint8* u_data,
170 uint8* v_data,
171 base::TimeDelta timestamp);
173 #if defined(OS_POSIX)
174 // Wraps provided dmabufs
175 // (https://www.kernel.org/doc/Documentation/dma-buf-sharing.txt) with a
176 // VideoFrame. The dmabuf fds are dup()ed on creation, so that the VideoFrame
177 // retains a reference to them, and are automatically close()d on destruction,
178 // dropping the reference. The caller may safely close() its reference after
179 // calling WrapExternalDmabufs().
180 // The image data is only accessible via dmabuf fds, which are usually passed
181 // directly to a hardware device and/or to another process, or can also be
182 // mapped via mmap() for CPU access.
183 // Returns NULL on failure.
184 static scoped_refptr<VideoFrame> WrapExternalDmabufs(
185 Format format,
186 const gfx::Size& coded_size,
187 const gfx::Rect& visible_rect,
188 const gfx::Size& natural_size,
189 const std::vector<int> dmabuf_fds,
190 base::TimeDelta timestamp);
191 #endif
193 #if defined(OS_MACOSX)
194 // Wraps a provided CVPixelBuffer with a VideoFrame. The pixel buffer is
195 // retained for the lifetime of the VideoFrame and released upon destruction.
196 // The image data is only accessible via the pixel buffer, which could be
197 // backed by an IOSurface from another process. All the attributes of the
198 // VideoFrame are derived from the pixel buffer, with the exception of the
199 // timestamp. If information is missing or is incompatible (for example, a
200 // pixel format that has no VideoFrame match), NULL is returned.
201 // http://crbug.com/401308
202 static scoped_refptr<VideoFrame> WrapCVPixelBuffer(
203 CVPixelBufferRef cv_pixel_buffer,
204 base::TimeDelta timestamp);
205 #endif
207 // Wraps |frame|. |visible_rect| must be a sub rect within
208 // frame->visible_rect().
209 static scoped_refptr<VideoFrame> WrapVideoFrame(
210 const scoped_refptr<VideoFrame>& frame,
211 const gfx::Rect& visible_rect,
212 const gfx::Size& natural_size);
214 // Creates a frame which indicates end-of-stream.
215 static scoped_refptr<VideoFrame> CreateEOSFrame();
217 // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v).
218 static scoped_refptr<VideoFrame> CreateColorFrame(
219 const gfx::Size& size,
220 uint8 y, uint8 u, uint8 v,
221 base::TimeDelta timestamp);
223 // Allocates YV12 frame based on |size|, and sets its data to the YUV
224 // equivalent of RGB(0,0,0).
225 static scoped_refptr<VideoFrame> CreateBlackFrame(const gfx::Size& size);
227 // Allocates YV12A frame based on |size|, and sets its data to the YUVA
228 // equivalent of RGBA(0,0,0,0).
229 static scoped_refptr<VideoFrame> CreateTransparentFrame(
230 const gfx::Size& size);
232 #if defined(VIDEO_HOLE)
233 // Allocates a hole frame.
234 static scoped_refptr<VideoFrame> CreateHoleFrame(const gfx::Size& size);
235 #endif // defined(VIDEO_HOLE)
237 static size_t NumPlanes(Format format);
239 static size_t NumTextures(TextureFormat texture_format);
241 // Returns the required allocation size for a (tightly packed) frame of the
242 // given coded size and format.
243 static size_t AllocationSize(Format format, const gfx::Size& coded_size);
245 // Returns the plane size (in bytes) for a plane of the given coded size and
246 // format.
247 static gfx::Size PlaneSize(Format format,
248 size_t plane,
249 const gfx::Size& coded_size);
251 // Returns the required allocation size for a (tightly packed) plane of the
252 // given coded size and format.
253 static size_t PlaneAllocationSize(Format format,
254 size_t plane,
255 const gfx::Size& coded_size);
257 // Returns horizontal bits per pixel for given |plane| and |format|.
258 static int PlaneHorizontalBitsPerPixel(Format format, size_t plane);
260 // Returns bits per pixel for given |plane| and |format|.
261 static int PlaneBitsPerPixel(Format format, size_t plane);
263 // Returns the number of bytes per row for the given plane, format, and width.
264 // The width may be aligned to format requirements.
265 static size_t RowBytes(size_t plane, Format format, int width);
267 // Returns the number of rows for the given plane, format, and height.
268 // The height may be aligned to format requirements.
269 static size_t Rows(size_t plane, Format format, int height);
271 // Returns the number of columns for the given plane, format, and width.
272 // The width may be aligned to format requirements.
273 static size_t Columns(size_t plane, Format format, int width);
275 Format format() const { return format_; }
277 TextureFormat texture_format() const { return texture_format_; }
279 const gfx::Size& coded_size() const { return coded_size_; }
280 const gfx::Rect& visible_rect() const { return visible_rect_; }
281 const gfx::Size& natural_size() const { return natural_size_; }
283 int stride(size_t plane) const;
285 // Returns the number of bytes per row and number of rows for a given plane.
287 // As opposed to stride(), row_bytes() refers to the bytes representing
288 // frame data scanlines (coded_size.width() pixels, without stride padding).
289 int row_bytes(size_t plane) const;
290 int rows(size_t plane) const;
292 // Returns pointer to the buffer for a given plane. The memory is owned by
293 // VideoFrame object and must not be freed by the caller.
294 const uint8* data(size_t plane) const;
295 uint8* data(size_t plane);
297 // Returns pointer to the data in the visible region of the frame. I.e. the
298 // returned pointer is offsetted into the plane buffer specified by
299 // visible_rect().origin(). Memory is owned by VideoFrame object and must not
300 // be freed by the caller.
301 const uint8* visible_data(size_t plane) const;
302 uint8* visible_data(size_t plane);
304 // Returns a mailbox holder for a given texture.
305 // Only valid to call if this is a NATIVE_TEXTURE frame. Before using the
306 // mailbox, the caller must wait for the included sync point.
307 const gpu::MailboxHolder& mailbox_holder(size_t texture) const;
309 // Returns the shared-memory handle, if present
310 base::SharedMemoryHandle shared_memory_handle() const;
312 // Returns the offset into the shared memory where the frame data begins.
313 size_t shared_memory_offset() const;
315 // Adds a callback to be run when the VideoFrame is about to be destroyed.
316 // The callback may be run from ANY THREAD, and so it is up to the client to
317 // ensure thread safety. Although read-only access to the members of this
318 // VideoFrame is permitted while the callback executes (including
319 // VideoFrameMetadata), clients should not assume the data pointers are
320 // valid.
321 void AddDestructionObserver(const base::Closure& callback);
323 // Returns a dictionary of optional metadata. This contains information
324 // associated with the frame that downstream clients might use for frame-level
325 // logging, quality/performance optimizations, signaling, etc.
327 // TODO(miu): Move some of the "extra" members of VideoFrame (below) into
328 // here as a later clean-up step.
329 const VideoFrameMetadata* metadata() const { return &metadata_; }
330 VideoFrameMetadata* metadata() { return &metadata_; }
332 bool allow_overlay() const { return allow_overlay_; }
334 #if defined(OS_POSIX)
335 // Returns backing dmabuf file descriptor for given |plane|, if present.
336 int dmabuf_fd(size_t plane) const;
337 #endif
339 #if defined(OS_MACOSX)
340 // Returns the backing CVPixelBuffer, if present.
341 CVPixelBufferRef cv_pixel_buffer() const;
342 #endif
344 // Returns true if this VideoFrame represents the end of the stream.
345 bool end_of_stream() const { return end_of_stream_; }
347 base::TimeDelta timestamp() const {
348 return timestamp_;
350 void set_timestamp(const base::TimeDelta& timestamp) {
351 timestamp_ = timestamp;
354 class SyncPointClient {
355 public:
356 SyncPointClient() {}
357 virtual uint32 InsertSyncPoint() = 0;
358 virtual void WaitSyncPoint(uint32 sync_point) = 0;
360 protected:
361 virtual ~SyncPointClient() {}
363 DISALLOW_COPY_AND_ASSIGN(SyncPointClient);
365 // It uses |client| to insert a new sync point and potentially waits on a
366 // older sync point. The final sync point will be used to release this
367 // VideoFrame.
368 // This method is thread safe. Both blink and compositor threads can call it.
369 void UpdateReleaseSyncPoint(SyncPointClient* client);
371 // Used to keep a running hash of seen frames. Expects an initialized MD5
372 // context. Calls MD5Update with the context and the contents of the frame.
373 void HashFrameForTesting(base::MD5Context* context);
375 private:
376 friend class base::RefCountedThreadSafe<VideoFrame>;
378 // Clients must use the static CreateFrame() method to create a new frame.
379 VideoFrame(Format format,
380 const gfx::Size& coded_size,
381 const gfx::Rect& visible_rect,
382 const gfx::Size& natural_size,
383 const gpu::MailboxHolder(&mailbox_holders)[kMaxPlanes],
384 TextureFormat texture_format,
385 base::TimeDelta timestamp,
386 bool end_of_stream);
387 virtual ~VideoFrame();
389 void AllocateYUV();
391 // Frame format.
392 const Format format_;
394 // Format of the native textures associated with this frame.
395 const TextureFormat texture_format_;
397 // Width and height of the video frame, in pixels. This must include pixel
398 // data for the whole image; i.e. for YUV formats with subsampled chroma
399 // planes, in the case that the visible portion of the image does not line up
400 // on a sample boundary, |coded_size_| must be rounded up appropriately and
401 // the pixel data provided for the odd pixels.
402 const gfx::Size coded_size_;
404 // Width, height, and offsets of the visible portion of the video frame. Must
405 // be a subrect of |coded_size_|. Can be odd with respect to the sample
406 // boundaries, e.g. for formats with subsampled chroma.
407 const gfx::Rect visible_rect_;
409 // Width and height of the visible portion of the video frame
410 // (|visible_rect_.size()|) with aspect ratio taken into account.
411 const gfx::Size natural_size_;
413 // Array of strides for each plane, typically greater or equal to the width
414 // of the surface divided by the horizontal sampling period. Note that
415 // strides can be negative.
416 int32 strides_[kMaxPlanes];
418 // Array of data pointers to each plane.
419 uint8* data_[kMaxPlanes];
421 // Native texture mailboxes, if this is a NATIVE_TEXTURE frame.
422 gpu::MailboxHolder mailbox_holders_[kMaxPlanes];
423 ReleaseMailboxCB mailbox_holders_release_cb_;
425 // Shared memory handle, if this frame was allocated from shared memory.
426 base::SharedMemoryHandle shared_memory_handle_;
428 // Offset in shared memory buffer.
429 size_t shared_memory_offset_;
431 #if defined(OS_POSIX)
432 // Dmabufs for each plane, if this frame is wrapping memory
433 // acquired via dmabuf.
434 base::ScopedFD dmabuf_fds_[kMaxPlanes];
435 #endif
437 #if defined(OS_MACOSX)
438 // CVPixelBuffer, if this frame is wrapping one.
439 base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer_;
440 #endif
442 std::vector<base::Closure> done_callbacks_;
444 base::TimeDelta timestamp_;
446 base::Lock release_sync_point_lock_;
447 uint32 release_sync_point_;
449 const bool end_of_stream_;
451 VideoFrameMetadata metadata_;
453 bool allow_overlay_;
455 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame);
458 } // namespace media
460 #endif // MEDIA_BASE_VIDEO_FRAME_H_