video_frame.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#ifndef MEDIA_BASE_VIDEO_FRAME_H_
6#define MEDIA_BASE_VIDEO_FRAME_H_
7
8#include "base/callback.h"
9#include "base/md5.h"
10#include "base/shared_memory.h"
11#include "gpu/command_buffer/common/mailbox.h"
12#include "media/base/buffers.h"
13#include "ui/gfx/rect.h"
14#include "ui/gfx/size.h"
15
16class SkBitmap;
17
18namespace media {
19
20class MEDIA_EXPORT VideoFrame : public base::RefCountedThreadSafe<VideoFrame> {
21 public:
22  enum {
23    kFrameSizeAlignment = 16,
24    kFrameSizePadding = 16,
25    kFrameAddressAlignment = 32
26  };
27
28  enum {
29    kMaxPlanes = 4,
30
31    kRGBPlane = 0,
32
33    kYPlane = 0,
34    kUPlane = 1,
35    kVPlane = 2,
36    kAPlane = 3,
37  };
38
39  // Surface formats roughly based on FOURCC labels, see:
40  // http://www.fourcc.org/rgb.php
41  // http://www.fourcc.org/yuv.php
42  enum Format {
43    INVALID = 0,  // Invalid format value.  Used for error reporting.
44    RGB32 = 4,  // 32bpp RGB packed with extra byte 8:8:8
45    YV12 = 6,  // 12bpp YVU planar 1x1 Y, 2x2 VU samples
46    YV16 = 7,  // 16bpp YVU planar 1x1 Y, 2x1 VU samples
47    EMPTY = 9,  // An empty frame.
48    I420 = 11,  // 12bpp YVU planar 1x1 Y, 2x2 UV samples.
49    NATIVE_TEXTURE = 12,  // Native texture.  Pixel-format agnostic.
50#if defined(GOOGLE_TV)
51    HOLE = 13,  // Hole frame.
52#endif
53    YV12A = 14,  // 20bpp YUVA planar 1x1 Y, 2x2 VU, 1x1 A samples.
54  };
55
56  // This class calls the TextureNoLongerNeededCallback when the last reference
57  // on the class is destroyed. The VideoFrame holds a reference to the mailbox
58  // but anyone else who queries the mailbox should also hold a reference while
59  // it is uses the mailbox, to ensure it remains valid. When finished with the
60  // mailbox, call Return() with a new sync point, to ensure the mailbox remains
61  // valid for the issued commands.
62  class MEDIA_EXPORT MailboxHolder
63      : public base::RefCountedThreadSafe<MailboxHolder> {
64   public:
65    typedef base::Callback<void(uint32 sync_point)>
66        TextureNoLongerNeededCallback;
67
68    MailboxHolder(const gpu::Mailbox& mailbox,
69                  unsigned sync_point,
70                  const TextureNoLongerNeededCallback& release_callback);
71
72    const gpu::Mailbox& mailbox() const { return mailbox_; }
73    unsigned sync_point() const { return sync_point_; }
74
75    void Return(unsigned sync_point) { sync_point_ = sync_point; }
76
77   private:
78    friend class base::RefCountedThreadSafe<MailboxHolder>;
79    ~MailboxHolder();
80
81    gpu::Mailbox mailbox_;
82    unsigned sync_point_;
83    TextureNoLongerNeededCallback release_callback_;
84  };
85
86
87  // Creates a new frame in system memory with given parameters. Buffers for
88  // the frame are allocated but not initialized.
89  // |coded_size| is the width and height of the frame data in pixels.
90  // |visible_rect| is the visible portion of |coded_size|, after cropping (if
91  // any) is applied.
92  // |natural_size| is the width and height of the frame when the frame's aspect
93  // ratio is applied to |visible_rect|.
94  static scoped_refptr<VideoFrame> CreateFrame(
95      Format format,
96      const gfx::Size& coded_size,
97      const gfx::Rect& visible_rect,
98      const gfx::Size& natural_size,
99      base::TimeDelta timestamp);
100
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, const gfx::Size& coded_size,
105                            const gfx::Rect& visible_rect,
106                            const gfx::Size& natural_size);
107
108  // CB to write pixels from the texture backing this frame into the
109  // |const SkBitmap&| parameter.
110  typedef base::Callback<void(const SkBitmap&)> ReadPixelsCB;
111
112  // Wraps a native texture of the given parameters with a VideoFrame.  When the
113  // frame is destroyed |no_longer_needed_cb.Run()| will be called.
114  // |coded_size| is the width and height of the frame data in pixels.
115  // |visible_rect| is the visible portion of |coded_size|, after cropping (if
116  // any) is applied.
117  // |natural_size| is the width and height of the frame when the frame's aspect
118  // ratio is applied to |visible_rect|.
119
120  // |read_pixels_cb| may be used to do (slow!) readbacks from the
121  // texture to main memory.
122  static scoped_refptr<VideoFrame> WrapNativeTexture(
123      const scoped_refptr<MailboxHolder>& mailbox_holder,
124      uint32 texture_target,
125      const gfx::Size& coded_size,
126      const gfx::Rect& visible_rect,
127      const gfx::Size& natural_size,
128      base::TimeDelta timestamp,
129      const ReadPixelsCB& read_pixels_cb,
130      const base::Closure& no_longer_needed_cb);
131
132  // Read pixels from the native texture backing |*this| and write
133  // them to |pixels| as BGRA.  |pixels| must point to a buffer at
134  // least as large as 4*visible_rect().width()*visible_rect().height().
135  void ReadPixelsFromNativeTexture(const SkBitmap& pixels);
136
137  // Wraps external YUV data of the given parameters with a VideoFrame.
138  // The returned VideoFrame does not own the data passed in. When the frame
139  // is destroyed |no_longer_needed_cb.Run()| will be called.
140  static scoped_refptr<VideoFrame> WrapExternalYuvData(
141      Format format,
142      const gfx::Size& coded_size,
143      const gfx::Rect& visible_rect,
144      const gfx::Size& natural_size,
145      int32 y_stride,
146      int32 u_stride,
147      int32 v_stride,
148      uint8* y_data,
149      uint8* u_data,
150      uint8* v_data,
151      base::TimeDelta timestamp,
152      base::SharedMemoryHandle shm_handle,  // may be NULLHandle()
153      const base::Closure& no_longer_needed_cb);
154
155  // Creates a frame with format equals to VideoFrame::EMPTY, width, height,
156  // and timestamp are all 0.
157  static scoped_refptr<VideoFrame> CreateEmptyFrame();
158
159  // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v).
160  static scoped_refptr<VideoFrame> CreateColorFrame(
161      const gfx::Size& size,
162      uint8 y, uint8 u, uint8 v,
163      base::TimeDelta timestamp);
164
165  // Allocates YV12 frame based on |size|, and sets its data to the YUV
166  // equivalent of RGB(0,0,0).
167  static scoped_refptr<VideoFrame> CreateBlackFrame(const gfx::Size& size);
168
169#if defined(GOOGLE_TV)
170  // Allocates a hole frame.
171  static scoped_refptr<VideoFrame> CreateHoleFrame(const gfx::Size& size);
172#endif
173
174  static size_t NumPlanes(Format format);
175
176  Format format() const { return format_; }
177
178  const gfx::Size& coded_size() const { return coded_size_; }
179  const gfx::Rect& visible_rect() const { return visible_rect_; }
180  const gfx::Size& natural_size() const { return natural_size_; }
181
182  int stride(size_t plane) const;
183
184  // Returns the number of bytes per row and number of rows for a given plane.
185  //
186  // As opposed to stride(), row_bytes() refers to the bytes representing
187  // frame data scanlines (coded_size.width() pixels, without stride padding).
188  int row_bytes(size_t plane) const;
189  int rows(size_t plane) const;
190
191  // Returns pointer to the buffer for a given plane. The memory is owned by
192  // VideoFrame object and must not be freed by the caller.
193  uint8* data(size_t plane) const;
194
195  // Returns the mailbox of the native texture wrapped by this frame. Only
196  // valid to call if this is a NATIVE_TEXTURE frame. Before using the
197  // mailbox, the caller must wait for the included sync point.
198  const scoped_refptr<MailboxHolder>& texture_mailbox() const;
199
200  // Returns the texture target. Only valid for NATIVE_TEXTURE frames.
201  uint32 texture_target() const;
202
203  // Returns the shared-memory handle, if present
204  base::SharedMemoryHandle shared_memory_handle() const;
205
206  // Returns true if this VideoFrame represents the end of the stream.
207  bool IsEndOfStream() const;
208
209  base::TimeDelta GetTimestamp() const {
210    return timestamp_;
211  }
212  void SetTimestamp(const base::TimeDelta& timestamp) {
213    timestamp_ = timestamp;
214  }
215
216  // Used to keep a running hash of seen frames.  Expects an initialized MD5
217  // context.  Calls MD5Update with the context and the contents of the frame.
218  void HashFrameForTesting(base::MD5Context* context);
219
220 private:
221  friend class base::RefCountedThreadSafe<VideoFrame>;
222  // Clients must use the static CreateFrame() method to create a new frame.
223  VideoFrame(Format format,
224             const gfx::Size& coded_size,
225             const gfx::Rect& visible_rect,
226             const gfx::Size& natural_size,
227             base::TimeDelta timestamp);
228  virtual ~VideoFrame();
229
230  // Used internally by CreateFrame().
231  void AllocateRGB(size_t bytes_per_pixel);
232  void AllocateYUV();
233
234  // Used to DCHECK() plane parameters.
235  bool IsValidPlane(size_t plane) const;
236
237  // Frame format.
238  Format format_;
239
240  // Width and height of the video frame.
241  gfx::Size coded_size_;
242
243  // Width, height, and offsets of the visible portion of the video frame.
244  gfx::Rect visible_rect_;
245
246  // Width and height of the visible portion of the video frame with aspect
247  // ratio taken into account.
248  gfx::Size natural_size_;
249
250  // Array of strides for each plane, typically greater or equal to the width
251  // of the surface divided by the horizontal sampling period.  Note that
252  // strides can be negative.
253  int32 strides_[kMaxPlanes];
254
255  // Array of data pointers to each plane.
256  uint8* data_[kMaxPlanes];
257
258  // Native texture mailbox, if this is a NATIVE_TEXTURE frame.
259  scoped_refptr<MailboxHolder> texture_mailbox_holder_;
260  uint32 texture_target_;
261  ReadPixelsCB read_pixels_cb_;
262
263  // Shared memory handle, if this frame was allocated from shared memory.
264  base::SharedMemoryHandle shared_memory_handle_;
265
266  base::Closure no_longer_needed_cb_;
267
268  base::TimeDelta timestamp_;
269
270  DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame);
271};
272
273}  // namespace media
274
275#endif  // MEDIA_BASE_VIDEO_FRAME_H_
276