video_frame.h revision 3240926e260ce088908e02ac07a6cf7b0c0cbf44
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/memory/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 // Returns the name of a Format as a string. 57 static std::string FormatToString(Format format); 58 59 // This class calls the TextureNoLongerNeededCallback when the last reference 60 // on the class is destroyed. The VideoFrame holds a reference to the mailbox 61 // but anyone else who queries the mailbox should also hold a reference while 62 // it is uses the mailbox, to ensure it remains valid. When finished with the 63 // mailbox, call Return() with a new sync point, to ensure the mailbox remains 64 // valid for the issued commands. 65 class MEDIA_EXPORT MailboxHolder 66 : public base::RefCountedThreadSafe<MailboxHolder> { 67 public: 68 typedef base::Callback<void(uint32 sync_point)> 69 TextureNoLongerNeededCallback; 70 71 MailboxHolder(const gpu::Mailbox& mailbox, 72 unsigned sync_point, 73 const TextureNoLongerNeededCallback& release_callback); 74 75 const gpu::Mailbox& mailbox() const { return mailbox_; } 76 unsigned sync_point() const { return sync_point_; } 77 78 void Return(unsigned sync_point) { sync_point_ = sync_point; } 79 80 private: 81 friend class base::RefCountedThreadSafe<MailboxHolder>; 82 ~MailboxHolder(); 83 84 gpu::Mailbox mailbox_; 85 unsigned sync_point_; 86 TextureNoLongerNeededCallback release_callback_; 87 }; 88 89 90 // Creates a new frame in system memory with given parameters. Buffers for 91 // the frame are allocated but not initialized. 92 // |coded_size| is the width and height of the frame data in pixels. 93 // |visible_rect| is the visible portion of |coded_size|, after cropping (if 94 // any) is applied. 95 // |natural_size| is the width and height of the frame when the frame's aspect 96 // ratio is applied to |visible_rect|. 97 static scoped_refptr<VideoFrame> CreateFrame( 98 Format format, 99 const gfx::Size& coded_size, 100 const gfx::Rect& visible_rect, 101 const gfx::Size& natural_size, 102 base::TimeDelta timestamp); 103 104 // Call prior to CreateFrame to ensure validity of frame configuration. Called 105 // automatically by VideoDecoderConfig::IsValidConfig(). 106 // TODO(scherkus): VideoDecoderConfig shouldn't call this method 107 static bool IsValidConfig(Format format, const gfx::Size& coded_size, 108 const gfx::Rect& visible_rect, 109 const gfx::Size& natural_size); 110 111 // CB to write pixels from the texture backing this frame into the 112 // |const SkBitmap&| parameter. 113 typedef base::Callback<void(const SkBitmap&)> ReadPixelsCB; 114 115 // Wraps a native texture of the given parameters with a VideoFrame. When the 116 // frame is destroyed |no_longer_needed_cb.Run()| will be called. 117 // |coded_size| is the width and height of the frame data in pixels. 118 // |visible_rect| is the visible portion of |coded_size|, after cropping (if 119 // any) is applied. 120 // |natural_size| is the width and height of the frame when the frame's aspect 121 // ratio is applied to |visible_rect|. 122 123 // |read_pixels_cb| may be used to do (slow!) readbacks from the 124 // texture to main memory. 125 static scoped_refptr<VideoFrame> WrapNativeTexture( 126 const scoped_refptr<MailboxHolder>& mailbox_holder, 127 uint32 texture_target, 128 const gfx::Size& coded_size, 129 const gfx::Rect& visible_rect, 130 const gfx::Size& natural_size, 131 base::TimeDelta timestamp, 132 const ReadPixelsCB& read_pixels_cb, 133 const base::Closure& no_longer_needed_cb); 134 135 // Read pixels from the native texture backing |*this| and write 136 // them to |pixels| as BGRA. |pixels| must point to a buffer at 137 // least as large as 4*visible_rect().width()*visible_rect().height(). 138 void ReadPixelsFromNativeTexture(const SkBitmap& pixels); 139 140 // Wraps external YUV data of the given parameters with a VideoFrame. 141 // The returned VideoFrame does not own the data passed in. When the frame 142 // is destroyed |no_longer_needed_cb.Run()| will be called. 143 static scoped_refptr<VideoFrame> WrapExternalYuvData( 144 Format format, 145 const gfx::Size& coded_size, 146 const gfx::Rect& visible_rect, 147 const gfx::Size& natural_size, 148 int32 y_stride, 149 int32 u_stride, 150 int32 v_stride, 151 uint8* y_data, 152 uint8* u_data, 153 uint8* v_data, 154 base::TimeDelta timestamp, 155 base::SharedMemoryHandle shm_handle, // may be NULLHandle() 156 const base::Closure& no_longer_needed_cb); 157 158 // Creates a frame with format equals to VideoFrame::EMPTY, width, height, 159 // and timestamp are all 0. 160 static scoped_refptr<VideoFrame> CreateEmptyFrame(); 161 162 // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v). 163 static scoped_refptr<VideoFrame> CreateColorFrame( 164 const gfx::Size& size, 165 uint8 y, uint8 u, uint8 v, 166 base::TimeDelta timestamp); 167 168 // Allocates YV12 frame based on |size|, and sets its data to the YUV 169 // equivalent of RGB(0,0,0). 170 static scoped_refptr<VideoFrame> CreateBlackFrame(const gfx::Size& size); 171 172#if defined(GOOGLE_TV) 173 // Allocates a hole frame. 174 static scoped_refptr<VideoFrame> CreateHoleFrame(const gfx::Size& size); 175#endif 176 177 static size_t NumPlanes(Format format); 178 179 Format format() const { return format_; } 180 181 const gfx::Size& coded_size() const { return coded_size_; } 182 const gfx::Rect& visible_rect() const { return visible_rect_; } 183 const gfx::Size& natural_size() const { return natural_size_; } 184 185 int stride(size_t plane) const; 186 187 // Returns the number of bytes per row and number of rows for a given plane. 188 // 189 // As opposed to stride(), row_bytes() refers to the bytes representing 190 // frame data scanlines (coded_size.width() pixels, without stride padding). 191 int row_bytes(size_t plane) const; 192 int rows(size_t plane) const; 193 194 // Returns pointer to the buffer for a given plane. The memory is owned by 195 // VideoFrame object and must not be freed by the caller. 196 uint8* data(size_t plane) const; 197 198 // Returns the mailbox of the native texture wrapped by this frame. Only 199 // valid to call if this is a NATIVE_TEXTURE frame. Before using the 200 // mailbox, the caller must wait for the included sync point. 201 const scoped_refptr<MailboxHolder>& texture_mailbox() const; 202 203 // Returns the texture target. Only valid for NATIVE_TEXTURE frames. 204 uint32 texture_target() const; 205 206 // Returns the shared-memory handle, if present 207 base::SharedMemoryHandle shared_memory_handle() const; 208 209 // Returns true if this VideoFrame represents the end of the stream. 210 bool IsEndOfStream() const; 211 212 base::TimeDelta GetTimestamp() const { 213 return timestamp_; 214 } 215 void SetTimestamp(const base::TimeDelta& timestamp) { 216 timestamp_ = timestamp; 217 } 218 219 // Used to keep a running hash of seen frames. Expects an initialized MD5 220 // context. Calls MD5Update with the context and the contents of the frame. 221 void HashFrameForTesting(base::MD5Context* context); 222 223 private: 224 friend class base::RefCountedThreadSafe<VideoFrame>; 225 // Clients must use the static CreateFrame() method to create a new frame. 226 VideoFrame(Format format, 227 const gfx::Size& coded_size, 228 const gfx::Rect& visible_rect, 229 const gfx::Size& natural_size, 230 base::TimeDelta timestamp); 231 virtual ~VideoFrame(); 232 233 // Used internally by CreateFrame(). 234 void AllocateRGB(size_t bytes_per_pixel); 235 void AllocateYUV(); 236 237 // Used to DCHECK() plane parameters. 238 bool IsValidPlane(size_t plane) const; 239 240 // Frame format. 241 Format format_; 242 243 // Width and height of the video frame. 244 gfx::Size coded_size_; 245 246 // Width, height, and offsets of the visible portion of the video frame. 247 gfx::Rect visible_rect_; 248 249 // Width and height of the visible portion of the video frame with aspect 250 // ratio taken into account. 251 gfx::Size natural_size_; 252 253 // Array of strides for each plane, typically greater or equal to the width 254 // of the surface divided by the horizontal sampling period. Note that 255 // strides can be negative. 256 int32 strides_[kMaxPlanes]; 257 258 // Array of data pointers to each plane. 259 uint8* data_[kMaxPlanes]; 260 261 // Native texture mailbox, if this is a NATIVE_TEXTURE frame. 262 scoped_refptr<MailboxHolder> texture_mailbox_holder_; 263 uint32 texture_target_; 264 ReadPixelsCB read_pixels_cb_; 265 266 // Shared memory handle, if this frame was allocated from shared memory. 267 base::SharedMemoryHandle shared_memory_handle_; 268 269 base::Closure no_longer_needed_cb_; 270 271 base::TimeDelta timestamp_; 272 273 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame); 274}; 275 276} // namespace media 277 278#endif // MEDIA_BASE_VIDEO_FRAME_H_ 279