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