video_frame.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "media/base/buffers.h"
11#include "ui/gfx/rect.h"
12#include "ui/gfx/size.h"
13
14namespace media {
15
16class MEDIA_EXPORT VideoFrame : public base::RefCountedThreadSafe<VideoFrame> {
17 public:
18  enum {
19    kMaxPlanes = 3,
20
21    kRGBPlane = 0,
22
23    kYPlane = 0,
24    kUPlane = 1,
25    kVPlane = 2,
26  };
27
28  // Surface formats roughly based on FOURCC labels, see:
29  // http://www.fourcc.org/rgb.php
30  // http://www.fourcc.org/yuv.php
31  // Keep in sync with WebKit::WebVideoFrame!
32  enum Format {
33    INVALID = 0,  // Invalid format value.  Used for error reporting.
34    RGB32 = 4,  // 32bpp RGB packed with extra byte 8:8:8
35    YV12 = 6,  // 12bpp YVU planar 1x1 Y, 2x2 VU samples
36    YV16 = 7,  // 16bpp YVU planar 1x1 Y, 2x1 VU samples
37    EMPTY = 9,  // An empty frame.
38    I420 = 11,  // 12bpp YVU planar 1x1 Y, 2x2 UV samples.
39    NATIVE_TEXTURE = 12,  // Native texture.  Pixel-format agnostic.
40  };
41
42  // Creates a new frame in system memory with given parameters. Buffers for
43  // the frame are allocated but not initialized.
44  // |coded_size| is the width and height of the frame data in pixels.
45  // |visible_rect| is the visible portion of |coded_size|, after cropping (if
46  // any) is applied.
47  // |natural_size| is the width and height of the frame when the frame's aspect
48  // ratio is applied to |visible_rect|.
49  static scoped_refptr<VideoFrame> CreateFrame(
50      Format format,
51      const gfx::Size& coded_size,
52      const gfx::Rect& visible_rect,
53      const gfx::Size& natural_size,
54      base::TimeDelta timestamp);
55
56  // Call prior to CreateFrame to ensure validity of frame configuration. Called
57  // automatically by VideoDecoderConfig::IsValidConfig().
58  // TODO(scherkus): VideoDecoderConfig shouldn't call this method
59  static bool IsValidConfig(Format format, const gfx::Size& coded_size,
60                            const gfx::Rect& visible_rect,
61                            const gfx::Size& natural_size);
62
63  // CB to write pixels from the texture backing this frame into the
64  // |void*| parameter.
65  typedef base::Callback<void(void*)> ReadPixelsCB;
66
67  // Wraps a native texture of the given parameters with a VideoFrame.  When the
68  // frame is destroyed |no_longer_needed.Run()| will be called.
69  // |coded_size| is the width and height of the frame data in pixels.
70  // |visible_rect| is the visible portion of |coded_size|, after cropping (if
71  // any) is applied.
72  // |natural_size| is the width and height of the frame when the frame's aspect
73  // ratio is applied to |visible_rect|.
74  // |read_pixels_cb| may be used to do (slow!) readbacks from the
75  // texture to main memory.
76  static scoped_refptr<VideoFrame> WrapNativeTexture(
77      uint32 texture_id,
78      uint32 texture_target,
79      const gfx::Size& coded_size,
80      const gfx::Rect& visible_rect,
81      const gfx::Size& natural_size,
82      base::TimeDelta timestamp,
83      const ReadPixelsCB& read_pixels_cb,
84      const base::Closure& no_longer_needed);
85
86  // Read pixels from the native texture backing |*this| and write
87  // them to |*pixels| as BGRA.  |pixels| must point to a buffer at
88  // least as large as 4*visible_rect().width()*visible_rect().height().
89  void ReadPixelsFromNativeTexture(void* pixels);
90
91  // Creates a frame with format equals to VideoFrame::EMPTY, width, height,
92  // and timestamp are all 0.
93  static scoped_refptr<VideoFrame> CreateEmptyFrame();
94
95  // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v).
96  static scoped_refptr<VideoFrame> CreateColorFrame(
97      const gfx::Size& size,
98      uint8 y, uint8 u, uint8 v,
99      base::TimeDelta timestamp);
100
101  // Allocates YV12 frame based on |size|, and sets its data to the YUV
102  // equivalent of RGB(0,0,0).
103  static scoped_refptr<VideoFrame> CreateBlackFrame(const gfx::Size& size);
104
105  Format format() const { return format_; }
106
107  const gfx::Size& coded_size() const { return coded_size_; }
108  const gfx::Rect& visible_rect() const { return visible_rect_; }
109  const gfx::Size& natural_size() const { return natural_size_; }
110
111  int stride(size_t plane) const;
112
113  // Returns the number of bytes per row and number of rows for a given plane.
114  //
115  // As opposed to stride(), row_bytes() refers to the bytes representing
116  // frame data scanlines (coded_size.width() pixels, without stride padding).
117  int row_bytes(size_t plane) const;
118  int rows(size_t plane) const;
119
120  // Returns pointer to the buffer for a given plane. The memory is owned by
121  // VideoFrame object and must not be freed by the caller.
122  uint8* data(size_t plane) const;
123
124  // Returns the ID of the native texture wrapped by this frame.  Only valid to
125  // call if this is a NATIVE_TEXTURE frame.
126  uint32 texture_id() const;
127
128  // Returns the texture target. Only valid for NATIVE_TEXTURE frames.
129  uint32 texture_target() const;
130
131  // Returns true if this VideoFrame represents the end of the stream.
132  bool IsEndOfStream() const;
133
134  base::TimeDelta GetTimestamp() const {
135    return timestamp_;
136  }
137  void SetTimestamp(const base::TimeDelta& timestamp) {
138    timestamp_ = timestamp;
139  }
140
141  // Used to keep a running hash of seen frames.  Expects an initialized MD5
142  // context.  Calls MD5Update with the context and the contents of the frame.
143  void HashFrameForTesting(base::MD5Context* context);
144
145 private:
146  friend class base::RefCountedThreadSafe<VideoFrame>;
147  // Clients must use the static CreateFrame() method to create a new frame.
148  VideoFrame(Format format,
149             const gfx::Size& coded_size,
150             const gfx::Rect& visible_rect,
151             const gfx::Size& natural_size,
152             base::TimeDelta timestamp);
153  virtual ~VideoFrame();
154
155  // Used internally by CreateFrame().
156  void AllocateRGB(size_t bytes_per_pixel);
157  void AllocateYUV();
158
159  // Used to DCHECK() plane parameters.
160  bool IsValidPlane(size_t plane) const;
161
162  // Frame format.
163  Format format_;
164
165  // Width and height of the video frame.
166  gfx::Size coded_size_;
167
168  // Width, height, and offsets of the visible portion of the video frame.
169  gfx::Rect visible_rect_;
170
171  // Width and height of the visible portion of the video frame with aspect
172  // ratio taken into account.
173  gfx::Size natural_size_;
174
175  // Array of strides for each plane, typically greater or equal to the width
176  // of the surface divided by the horizontal sampling period.  Note that
177  // strides can be negative.
178  int32 strides_[kMaxPlanes];
179
180  // Array of data pointers to each plane.
181  uint8* data_[kMaxPlanes];
182
183  // Native texture ID, if this is a NATIVE_TEXTURE frame.
184  uint32 texture_id_;
185  uint32 texture_target_;
186  ReadPixelsCB read_pixels_cb_;
187  base::Closure texture_no_longer_needed_;
188
189  base::TimeDelta timestamp_;
190
191  DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame);
192};
193
194}  // namespace media
195
196#endif  // MEDIA_BASE_VIDEO_FRAME_H_
197