kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_H_ |
| 12 | #define WEBRTC_COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_H_ |
| 13 | |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
| 16 | #include <memory> |
| 17 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 18 | #include "webrtc/base/callback.h" |
| 19 | #include "webrtc/base/refcount.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 20 | #include "webrtc/base/scoped_ref_ptr.h" |
nisse | d507472 | 2016-08-30 08:45:44 -0700 | [diff] [blame] | 21 | #include "webrtc/common_video/rotation.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 22 | #include "webrtc/system_wrappers/include/aligned_malloc.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 26 | // Interface of a simple frame buffer containing pixel data. This interface does |
| 27 | // not contain any frame metadata such as rotation, timestamp, pixel_width, etc. |
| 28 | class VideoFrameBuffer : public rtc::RefCountInterface { |
| 29 | public: |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 30 | // The resolution of the frame in pixels. For formats where some planes are |
| 31 | // subsampled, this is the highest-resolution plane. |
| 32 | virtual int width() const = 0; |
| 33 | virtual int height() const = 0; |
| 34 | |
| 35 | // Returns pointer to the pixel data for a given plane. The memory is owned by |
| 36 | // the VideoFrameBuffer object and must not be freed by the caller. |
nisse | 1e6bbe4 | 2016-06-21 03:59:23 -0700 | [diff] [blame] | 37 | virtual const uint8_t* DataY() const = 0; |
| 38 | virtual const uint8_t* DataU() const = 0; |
| 39 | virtual const uint8_t* DataV() const = 0; |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 40 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 41 | // Returns the number of bytes between successive rows for a given plane. |
nisse | 1e6bbe4 | 2016-06-21 03:59:23 -0700 | [diff] [blame] | 42 | virtual int StrideY() const = 0; |
| 43 | virtual int StrideU() const = 0; |
| 44 | virtual int StrideV() const = 0; |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 45 | |
| 46 | // Return the handle of the underlying video frame. This is used when the |
| 47 | // frame is backed by a texture. |
| 48 | virtual void* native_handle() const = 0; |
| 49 | |
| 50 | // Returns a new memory-backed frame buffer converted from this buffer's |
| 51 | // native handle. |
| 52 | virtual rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() = 0; |
| 53 | |
| 54 | protected: |
| 55 | virtual ~VideoFrameBuffer(); |
| 56 | }; |
| 57 | |
| 58 | // Plain I420 buffer in standard memory. |
| 59 | class I420Buffer : public VideoFrameBuffer { |
| 60 | public: |
| 61 | I420Buffer(int width, int height); |
| 62 | I420Buffer(int width, int height, int stride_y, int stride_u, int stride_v); |
nisse | efec590 | 2016-06-09 00:31:39 -0700 | [diff] [blame] | 63 | |
nisse | ac62bd4 | 2016-06-20 03:38:52 -0700 | [diff] [blame] | 64 | static rtc::scoped_refptr<I420Buffer> Create(int width, int height); |
| 65 | static rtc::scoped_refptr<I420Buffer> Create(int width, |
| 66 | int height, |
| 67 | int stride_y, |
| 68 | int stride_u, |
| 69 | int stride_v); |
| 70 | |
nisse | efec590 | 2016-06-09 00:31:39 -0700 | [diff] [blame] | 71 | // Sets all three planes to all zeros. Used to work around for |
| 72 | // quirks in memory checkers |
| 73 | // (https://bugs.chromium.org/p/libyuv/issues/detail?id=377) and |
| 74 | // ffmpeg (http://crbug.com/390941). |
| 75 | // TODO(nisse): Should be deleted if/when those issues are resolved |
| 76 | // in a better way. |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 77 | void InitializeData(); |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 78 | |
nisse | efec590 | 2016-06-09 00:31:39 -0700 | [diff] [blame] | 79 | // Sets the frame buffer to all black. |
| 80 | void SetToBlack(); |
| 81 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 82 | int width() const override; |
| 83 | int height() const override; |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 84 | const uint8_t* DataY() const override; |
| 85 | const uint8_t* DataU() const override; |
| 86 | const uint8_t* DataV() const override; |
nisse | d591e3f | 2016-05-26 06:49:55 -0700 | [diff] [blame] | 87 | |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 88 | uint8_t* MutableDataY(); |
| 89 | uint8_t* MutableDataU(); |
| 90 | uint8_t* MutableDataV(); |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 91 | int StrideY() const override; |
| 92 | int StrideU() const override; |
| 93 | int StrideV() const override; |
| 94 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 95 | void* native_handle() const override; |
| 96 | rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() override; |
| 97 | |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 98 | // Create a new buffer and copy the pixel data. |
nisse | e3fe4a7 | 2016-11-10 08:44:38 -0800 | [diff] [blame] | 99 | static rtc::scoped_refptr<I420Buffer> Copy(const VideoFrameBuffer& buffer); |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 100 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 101 | // Scale the cropped area of |src| to the size of |this| buffer, and |
| 102 | // write the result into |this|. |
nisse | e3fe4a7 | 2016-11-10 08:44:38 -0800 | [diff] [blame] | 103 | void CropAndScaleFrom(const VideoFrameBuffer& src, |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 104 | int offset_x, |
| 105 | int offset_y, |
| 106 | int crop_width, |
| 107 | int crop_height); |
| 108 | |
| 109 | // The common case of a center crop, when needed to adjust the |
| 110 | // aspect ratio without distorting the image. |
nisse | e3fe4a7 | 2016-11-10 08:44:38 -0800 | [diff] [blame] | 111 | void CropAndScaleFrom(const VideoFrameBuffer& src); |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 112 | |
| 113 | // Scale all of |src| to the size of |this| buffer, with no cropping. |
nisse | e3fe4a7 | 2016-11-10 08:44:38 -0800 | [diff] [blame] | 114 | void ScaleFrom(const VideoFrameBuffer& src); |
| 115 | |
| 116 | // Deprecated methods, using smart pointer references. |
| 117 | // TODO(nisse): Delete once downstream applications are updated. |
| 118 | static rtc::scoped_refptr<I420Buffer> Copy( |
| 119 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 120 | return Copy(*buffer); |
| 121 | } |
| 122 | void CropAndScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src, |
| 123 | int offset_x, |
| 124 | int offset_y, |
| 125 | int crop_width, |
| 126 | int crop_height) { |
| 127 | CropAndScaleFrom(*src, offset_x, offset_y, crop_width, crop_height); |
| 128 | } |
| 129 | void CropAndScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
| 130 | CropAndScaleFrom(*src); |
| 131 | } |
| 132 | void ScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
| 133 | ScaleFrom(*src); |
| 134 | } |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 135 | |
nisse | d507472 | 2016-08-30 08:45:44 -0700 | [diff] [blame] | 136 | // Returns a rotated versions of |src|. Native buffers are not |
| 137 | // supported. The reason this function doesn't return an I420Buffer, |
| 138 | // is that it returns |src| unchanged in case |rotation| is zero. |
| 139 | static rtc::scoped_refptr<VideoFrameBuffer> Rotate( |
nisse | e3fe4a7 | 2016-11-10 08:44:38 -0800 | [diff] [blame] | 140 | rtc::scoped_refptr<VideoFrameBuffer> src, |
nisse | d507472 | 2016-08-30 08:45:44 -0700 | [diff] [blame] | 141 | VideoRotation rotation); |
| 142 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 143 | protected: |
| 144 | ~I420Buffer() override; |
| 145 | |
| 146 | private: |
| 147 | const int width_; |
| 148 | const int height_; |
| 149 | const int stride_y_; |
| 150 | const int stride_u_; |
| 151 | const int stride_v_; |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 152 | const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_; |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | // Base class for native-handle buffer is a wrapper around a |native_handle|. |
| 156 | // This is used for convenience as most native-handle implementations can share |
| 157 | // many VideoFrame implementations, but need to implement a few others (such |
| 158 | // as their own destructors or conversion methods back to software I420). |
| 159 | class NativeHandleBuffer : public VideoFrameBuffer { |
| 160 | public: |
| 161 | NativeHandleBuffer(void* native_handle, int width, int height); |
| 162 | |
| 163 | int width() const override; |
| 164 | int height() const override; |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 165 | const uint8_t* DataY() const override; |
| 166 | const uint8_t* DataU() const override; |
| 167 | const uint8_t* DataV() const override; |
| 168 | int StrideY() const override; |
| 169 | int StrideU() const override; |
| 170 | int StrideV() const override; |
| 171 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 172 | void* native_handle() const override; |
| 173 | |
| 174 | protected: |
| 175 | void* native_handle_; |
| 176 | const int width_; |
| 177 | const int height_; |
| 178 | }; |
| 179 | |
| 180 | class WrappedI420Buffer : public webrtc::VideoFrameBuffer { |
| 181 | public: |
| 182 | WrappedI420Buffer(int width, |
| 183 | int height, |
| 184 | const uint8_t* y_plane, |
| 185 | int y_stride, |
| 186 | const uint8_t* u_plane, |
| 187 | int u_stride, |
| 188 | const uint8_t* v_plane, |
| 189 | int v_stride, |
| 190 | const rtc::Callback0<void>& no_longer_used); |
| 191 | int width() const override; |
| 192 | int height() const override; |
| 193 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 194 | const uint8_t* DataY() const override; |
| 195 | const uint8_t* DataU() const override; |
| 196 | const uint8_t* DataV() const override; |
| 197 | int StrideY() const override; |
| 198 | int StrideU() const override; |
| 199 | int StrideV() const override; |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 200 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 201 | void* native_handle() const override; |
| 202 | |
| 203 | rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() override; |
| 204 | |
| 205 | private: |
| 206 | friend class rtc::RefCountedObject<WrappedI420Buffer>; |
| 207 | ~WrappedI420Buffer() override; |
| 208 | |
| 209 | const int width_; |
| 210 | const int height_; |
| 211 | const uint8_t* const y_plane_; |
| 212 | const uint8_t* const u_plane_; |
| 213 | const uint8_t* const v_plane_; |
| 214 | const int y_stride_; |
| 215 | const int u_stride_; |
| 216 | const int v_stride_; |
| 217 | rtc::Callback0<void> no_longer_used_cb_; |
| 218 | }; |
| 219 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 220 | } // namespace webrtc |
| 221 | |
| 222 | #endif // WEBRTC_COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_H_ |