#ifndef MEDIA_BASE_VIDEO_FRAME_H_
#define MEDIA_BASE_VIDEO_FRAME_H_
+#include <vector>
+
#include "base/callback.h"
#include "base/md5.h"
#include "base/memory/shared_memory.h"
-#include "gpu/command_buffer/common/mailbox.h"
+#include "base/synchronization/lock.h"
#include "media/base/buffers.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
+#if defined(OS_MACOSX)
+#include <CoreVideo/CVPixelBuffer.h>
+#include "base/mac/scoped_cftyperef.h"
+#endif
+
class SkBitmap;
+namespace gpu {
+struct MailboxHolder;
+} // namespace gpu
+
namespace media {
class MEDIA_EXPORT VideoFrame : public base::RefCountedThreadSafe<VideoFrame> {
kYPlane = 0,
kUPlane = 1,
+ kUVPlane = kUPlane,
kVPlane = 2,
kAPlane = 3,
};
#endif // defined(VIDEO_HOLE)
NATIVE_TEXTURE = 6, // Native texture. Pixel-format agnostic.
YV12J = 7, // JPEG color range version of YV12
- HISTOGRAM_MAX, // Must always be greatest.
+ NV12 = 8, // 12bpp 1x1 Y plane followed by an interleaved 2x2 UV plane.
+ YV24 = 9, // 24bpp YUV planar, no subsampling.
+ FORMAT_MAX = YV24, // Must always be equal to largest entry logged.
};
// Returns the name of a Format as a string.
static std::string FormatToString(Format format);
- // This class calls the TextureNoLongerNeededCallback when this class is
- // destroyed. Users can query the current sync point associated with this
- // mailbox with sync_point(), and should call Resync() with a new sync point
- // to ensure the mailbox remains valid for the issued commands.
- // valid for the issued commands.
- class MEDIA_EXPORT MailboxHolder {
- public:
- typedef base::Callback<void(uint32 sync_point)>
- TextureNoLongerNeededCallback;
-
- MailboxHolder(const gpu::Mailbox& mailbox,
- unsigned sync_point,
- const TextureNoLongerNeededCallback& release_callback);
- ~MailboxHolder();
-
- const gpu::Mailbox& mailbox() const { return mailbox_; }
- unsigned sync_point() const { return sync_point_; }
-
- void Resync(unsigned sync_point) { sync_point_ = sync_point; }
-
- private:
-
- gpu::Mailbox mailbox_;
- unsigned sync_point_;
- TextureNoLongerNeededCallback release_callback_;
- };
-
-
// Creates a new frame in system memory with given parameters. Buffers for
// the frame are allocated but not initialized.
- // |coded_size| is the width and height of the frame data in pixels.
- // |visible_rect| is the visible portion of |coded_size|, after cropping (if
- // any) is applied.
- // |natural_size| is the width and height of the frame when the frame's aspect
- // ratio is applied to |visible_rect|.
static scoped_refptr<VideoFrame> CreateFrame(
Format format,
const gfx::Size& coded_size,
// |const SkBitmap&| parameter.
typedef base::Callback<void(const SkBitmap&)> ReadPixelsCB;
- // Wraps a native texture of the given parameters with a VideoFrame. When the
- // frame is destroyed |no_longer_needed_cb.Run()| will be called.
- // |coded_size| is the width and height of the frame data in pixels.
- // |visible_rect| is the visible portion of |coded_size|, after cropping (if
- // any) is applied.
- // |natural_size| is the width and height of the frame when the frame's aspect
- // ratio is applied to |visible_rect|.
+ // CB to be called on the mailbox backing this frame when the frame is
+ // destroyed.
+ typedef base::Callback<void(uint32)> ReleaseMailboxCB;
+ // Wraps a native texture of the given parameters with a VideoFrame. The
+ // backing of the VideoFrame is held in the mailbox held by |mailbox_holder|,
+ // and |mailbox_holder_release_cb| will be called with |mailbox_holder| as the
+ // argument when the VideoFrame is to be destroyed.
// |read_pixels_cb| may be used to do (slow!) readbacks from the
// texture to main memory.
static scoped_refptr<VideoFrame> WrapNativeTexture(
- scoped_ptr<MailboxHolder> mailbox_holder,
- uint32 texture_target,
+ scoped_ptr<gpu::MailboxHolder> mailbox_holder,
+ const ReleaseMailboxCB& mailbox_holder_release_cb,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
base::TimeDelta timestamp,
- const ReadPixelsCB& read_pixels_cb,
- const base::Closure& no_longer_needed_cb);
+ const ReadPixelsCB& read_pixels_cb);
+#if !defined(MEDIA_FOR_CAST_IOS)
// Read pixels from the native texture backing |*this| and write
// them to |pixels| as BGRA. |pixels| must point to a buffer at
- // least as large as 4*visible_rect().width()*visible_rect().height().
+ // least as large as 4 * visible_rect().size().GetArea().
void ReadPixelsFromNativeTexture(const SkBitmap& pixels);
+#endif
// Wraps packed image data residing in a memory buffer with a VideoFrame.
// The image data resides in |data| and is assumed to be packed tightly in a
// and plane count as given by |format|. The shared memory handle of the
// backing allocation, if present, can be passed in with |handle|. When the
// frame is destroyed, |no_longer_needed_cb.Run()| will be called.
+ // Returns NULL on failure.
static scoped_refptr<VideoFrame> WrapExternalPackedMemory(
Format format,
const gfx::Size& coded_size,
base::TimeDelta timestamp,
const base::Closure& no_longer_needed_cb);
+#if defined(OS_POSIX)
+ // Wraps provided dmabufs
+ // (https://www.kernel.org/doc/Documentation/dma-buf-sharing.txt) with a
+ // VideoFrame. The dmabuf fds are dup()ed on creation, so that the VideoFrame
+ // retains a reference to them, and are automatically close()d on destruction,
+ // dropping the reference. The caller may safely close() its reference after
+ // calling WrapExternalDmabufs().
+ // The image data is only accessible via dmabuf fds, which are usually passed
+ // directly to a hardware device and/or to another process, or can also be
+ // mapped via mmap() for CPU access.
+ // When the frame is destroyed, |no_longer_needed_cb.Run()| will be called.
+ // Returns NULL on failure.
+ static scoped_refptr<VideoFrame> WrapExternalDmabufs(
+ Format format,
+ const gfx::Size& coded_size,
+ const gfx::Rect& visible_rect,
+ const gfx::Size& natural_size,
+ const std::vector<int> dmabuf_fds,
+ base::TimeDelta timestamp,
+ const base::Closure& no_longer_needed_cb);
+#endif
+
+#if defined(OS_MACOSX)
+ // Wraps a provided CVPixelBuffer with a VideoFrame. The pixel buffer is
+ // retained for the lifetime of the VideoFrame and released upon destruction.
+ // The image data is only accessible via the pixel buffer, which could be
+ // backed by an IOSurface from another process. All the attributes of the
+ // VideoFrame are derived from the pixel buffer, with the exception of the
+ // timestamp. If information is missing or is incompatible (for example, a
+ // pixel format that has no VideoFrame match), NULL is returned.
+ // http://crbug.com/401308
+ static scoped_refptr<VideoFrame> WrapCVPixelBuffer(
+ CVPixelBufferRef cv_pixel_buffer,
+ base::TimeDelta timestamp);
+#endif
+
// Wraps external YUV data of the given parameters with a VideoFrame.
// The returned VideoFrame does not own the data passed in. When the frame
// is destroyed |no_longer_needed_cb.Run()| will be called.
const base::Closure& no_longer_needed_cb);
// Wraps |frame| and calls |no_longer_needed_cb| when the wrapper VideoFrame
- // gets destroyed.
+ // gets destroyed. |visible_rect| must be a sub rect within
+ // frame->visible_rect().
static scoped_refptr<VideoFrame> WrapVideoFrame(
const scoped_refptr<VideoFrame>& frame,
+ const gfx::Rect& visible_rect,
+ const gfx::Size& natural_size,
const base::Closure& no_longer_needed_cb);
// Creates a frame which indicates end-of-stream.
// equivalent of RGB(0,0,0).
static scoped_refptr<VideoFrame> CreateBlackFrame(const gfx::Size& size);
+ // Allocates YV12A frame based on |size|, and sets its data to the YUVA
+ // equivalent of RGBA(0,0,0,0).
+ static scoped_refptr<VideoFrame> CreateTransparentFrame(
+ const gfx::Size& size);
+
#if defined(VIDEO_HOLE)
// Allocates a hole frame.
static scoped_refptr<VideoFrame> CreateHoleFrame(const gfx::Size& size);
// given coded size and format.
static size_t AllocationSize(Format format, const gfx::Size& coded_size);
+ // Returns the plane size for a plane of the given coded size and format.
+ static gfx::Size PlaneSize(Format format,
+ size_t plane,
+ const gfx::Size& coded_size);
+
// Returns the required allocation size for a (tightly packed) plane of the
// given coded size and format.
static size_t PlaneAllocationSize(Format format,
size_t plane,
const gfx::Size& coded_size);
+ // Returns horizontal bits per pixel for given |plane| and |format|.
+ static int PlaneHorizontalBitsPerPixel(Format format, size_t plane);
+
+ // Returns the number of bytes per row for the given plane, format, and width.
+ // The width may be aligned to format requirements.
+ static size_t RowBytes(size_t plane, Format format, int width);
+
+ // Returns the number of rows for the given plane, format, and height.
+ // The height may be aligned to format requirements.
+ static size_t Rows(size_t plane, Format format, int height);
+
Format format() const { return format_; }
const gfx::Size& coded_size() const { return coded_size_; }
// VideoFrame object and must not be freed by the caller.
uint8* data(size_t plane) const;
- // Returns the mailbox of the native texture wrapped by this frame. Only
- // valid to call if this is a NATIVE_TEXTURE frame. Before using the
+ // Returns the mailbox holder of the native texture wrapped by this frame.
+ // Only valid to call if this is a NATIVE_TEXTURE frame. Before using the
// mailbox, the caller must wait for the included sync point.
- MailboxHolder* texture_mailbox() const;
-
- // Returns the texture target. Only valid for NATIVE_TEXTURE frames.
- uint32 texture_target() const;
+ const gpu::MailboxHolder* mailbox_holder() const;
// Returns the shared-memory handle, if present
base::SharedMemoryHandle shared_memory_handle() const;
+#if defined(OS_POSIX)
+ // Returns backing dmabuf file descriptor for given |plane|, if present.
+ int dmabuf_fd(size_t plane) const;
+#endif
+
+#if defined(OS_MACOSX)
+ // Returns the backing CVPixelBuffer, if present.
+ CVPixelBufferRef cv_pixel_buffer() const;
+#endif
+
// Returns true if this VideoFrame represents the end of the stream.
bool end_of_stream() const { return end_of_stream_; }
- base::TimeDelta GetTimestamp() const {
+ base::TimeDelta timestamp() const {
return timestamp_;
}
- void SetTimestamp(const base::TimeDelta& timestamp) {
+ void set_timestamp(const base::TimeDelta& timestamp) {
timestamp_ = timestamp;
}
+ class SyncPointClient {
+ public:
+ SyncPointClient() {}
+ virtual uint32 InsertSyncPoint() = 0;
+ virtual void WaitSyncPoint(uint32 sync_point) = 0;
+
+ protected:
+ virtual ~SyncPointClient() {}
+
+ DISALLOW_COPY_AND_ASSIGN(SyncPointClient);
+ };
+ // It uses |client| to insert a new sync point and potentially waits on a
+ // older sync point. The final sync point will be used to release this
+ // VideoFrame.
+ // This method is thread safe. Both blink and compositor threads can call it.
+ void UpdateReleaseSyncPoint(SyncPointClient* client);
+
// Used to keep a running hash of seen frames. Expects an initialized MD5
// context. Calls MD5Update with the context and the contents of the frame.
void HashFrameForTesting(base::MD5Context* context);
private:
friend class base::RefCountedThreadSafe<VideoFrame>;
+
+ // Returns true if |plane| is a valid plane number for the given format. This
+ // can be used to DCHECK() plane parameters.
+ static bool IsValidPlane(size_t plane, VideoFrame::Format format);
+
// Clients must use the static CreateFrame() method to create a new frame.
VideoFrame(Format format,
const gfx::Size& coded_size,
const gfx::Rect& visible_rect,
const gfx::Size& natural_size,
+ scoped_ptr<gpu::MailboxHolder> mailbox_holder,
base::TimeDelta timestamp,
bool end_of_stream);
virtual ~VideoFrame();
void AllocateYUV();
- // Used to DCHECK() plane parameters.
- bool IsValidPlane(size_t plane) const;
-
// Frame format.
- Format format_;
+ const Format format_;
- // Width and height of the video frame.
- gfx::Size coded_size_;
+ // Width and height of the video frame, in pixels. This must include pixel
+ // data for the whole image; i.e. for YUV formats with subsampled chroma
+ // planes, in the case that the visible portion of the image does not line up
+ // on a sample boundary, |coded_size_| must be rounded up appropriately and
+ // the pixel data provided for the odd pixels.
+ const gfx::Size coded_size_;
- // Width, height, and offsets of the visible portion of the video frame.
- gfx::Rect visible_rect_;
+ // Width, height, and offsets of the visible portion of the video frame. Must
+ // be a subrect of |coded_size_|. Can be odd with respect to the sample
+ // boundaries, e.g. for formats with subsampled chroma.
+ const gfx::Rect visible_rect_;
- // Width and height of the visible portion of the video frame with aspect
- // ratio taken into account.
- gfx::Size natural_size_;
+ // Width and height of the visible portion of the video frame
+ // (|visible_rect_.size()|) with aspect ratio taken into account.
+ const gfx::Size natural_size_;
// Array of strides for each plane, typically greater or equal to the width
// of the surface divided by the horizontal sampling period. Note that
uint8* data_[kMaxPlanes];
// Native texture mailbox, if this is a NATIVE_TEXTURE frame.
- scoped_ptr<MailboxHolder> texture_mailbox_holder_;
- uint32 texture_target_;
+ const scoped_ptr<gpu::MailboxHolder> mailbox_holder_;
+ ReleaseMailboxCB mailbox_holder_release_cb_;
ReadPixelsCB read_pixels_cb_;
// Shared memory handle, if this frame was allocated from shared memory.
base::SharedMemoryHandle shared_memory_handle_;
+#if defined(OS_POSIX)
+ // Dmabufs for each plane, if this frame is wrapping memory
+ // acquired via dmabuf.
+ base::ScopedFD dmabuf_fds_[kMaxPlanes];
+#endif
+
+#if defined(OS_MACOSX)
+ // CVPixelBuffer, if this frame is wrapping one.
+ base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer_;
+#endif
+
base::Closure no_longer_needed_cb_;
base::TimeDelta timestamp_;
+ base::Lock release_sync_point_lock_;
+ uint32 release_sync_point_;
+
const bool end_of_stream_;
DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame);