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