blob: 7ebff44507a0ae8ec3b049d8c250e0e689675ebc [file] [log] [blame]
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +00001/*
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 */
perkj0489e492016-10-20 00:24:01 -070010#include "webrtc/common_video/include/video_frame_buffer.h"
11
12#include <string.h>
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000013
Niels Möller718a7632016-06-13 13:06:01 +020014#include <algorithm>
15
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000016#include "webrtc/base/checks.h"
perkj14f41442015-11-30 22:15:45 -080017#include "webrtc/base/keep_ref_until_done.h"
nisse7cc9cc02016-03-29 23:44:19 -070018#include "libyuv/convert.h"
fbarchardd1f83cf2016-07-08 17:33:20 -070019#include "libyuv/planar_functions.h"
Niels Möller718a7632016-06-13 13:06:01 +020020#include "libyuv/scale.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000021
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000022namespace webrtc {
23
Peter Boströmeb66e802015-06-05 11:08:03 +020024NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
25 int width,
26 int height)
27 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -070028 RTC_DCHECK(native_handle != nullptr);
29 RTC_DCHECK_GT(width, 0);
30 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000031}
32
magjed712338e2017-05-11 05:11:57 -070033VideoFrameBuffer::Type NativeHandleBuffer::type() const {
34 return Type::kNative;
35}
36
Peter Boströmeb66e802015-06-05 11:08:03 +020037int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000038 return width_;
39}
40
Peter Boströmeb66e802015-06-05 11:08:03 +020041int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000042 return height_;
43}
44
nisse06176e42016-04-18 05:34:40 -070045const uint8_t* NativeHandleBuffer::DataY() const {
46 RTC_NOTREACHED(); // Should not be called.
47 return nullptr;
48}
49const uint8_t* NativeHandleBuffer::DataU() const {
50 RTC_NOTREACHED(); // Should not be called.
51 return nullptr;
52}
53const uint8_t* NativeHandleBuffer::DataV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000054 RTC_NOTREACHED(); // Should not be called.
55 return nullptr;
56}
57
nisse06176e42016-04-18 05:34:40 -070058int NativeHandleBuffer::StrideY() const {
59 RTC_NOTREACHED(); // Should not be called.
60 return 0;
61}
62int NativeHandleBuffer::StrideU() const {
63 RTC_NOTREACHED(); // Should not be called.
64 return 0;
65}
66int NativeHandleBuffer::StrideV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000067 RTC_NOTREACHED(); // Should not be called.
68 return 0;
69}
70
Peter Boströmeb66e802015-06-05 11:08:03 +020071void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000072 return native_handle_;
73}
74
Magnus Jedvertc464f502015-08-25 23:22:08 +020075WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +020076 int height,
77 const uint8_t* y_plane,
78 int y_stride,
79 const uint8_t* u_plane,
80 int u_stride,
81 const uint8_t* v_plane,
82 int v_stride,
83 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +020084 : width_(width),
85 height_(height),
86 y_plane_(y_plane),
87 u_plane_(u_plane),
88 v_plane_(v_plane),
89 y_stride_(y_stride),
90 u_stride_(u_stride),
91 v_stride_(v_stride),
92 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +020093}
94
95WrappedI420Buffer::~WrappedI420Buffer() {
96 no_longer_used_cb_();
97}
98
Per33544192015-04-02 12:30:51 +020099int WrappedI420Buffer::width() const {
100 return width_;
101}
102
103int WrappedI420Buffer::height() const {
104 return height_;
105}
106
nisse06176e42016-04-18 05:34:40 -0700107const uint8_t* WrappedI420Buffer::DataY() const {
108 return y_plane_;
109}
110const uint8_t* WrappedI420Buffer::DataU() const {
111 return u_plane_;
112}
113const uint8_t* WrappedI420Buffer::DataV() const {
114 return v_plane_;
Per33544192015-04-02 12:30:51 +0200115}
116
nisse06176e42016-04-18 05:34:40 -0700117int WrappedI420Buffer::StrideY() const {
118 return y_stride_;
119}
120int WrappedI420Buffer::StrideU() const {
121 return u_stride_;
122}
123int WrappedI420Buffer::StrideV() const {
124 return v_stride_;
Per33544192015-04-02 12:30:51 +0200125}
126
yuweih23cc4682017-06-22 20:28:06 -0700127// Template to implement a wrapped buffer for a I4??BufferInterface.
128template <typename Base>
129class WrappedYuvBuffer : public Base {
130 public:
131 WrappedYuvBuffer(int width,
132 int height,
133 const uint8_t* y_plane,
134 int y_stride,
135 const uint8_t* u_plane,
136 int u_stride,
137 const uint8_t* v_plane,
138 int v_stride,
139 const rtc::Callback0<void>& no_longer_used)
140 : width_(width),
141 height_(height),
142 y_plane_(y_plane),
143 u_plane_(u_plane),
144 v_plane_(v_plane),
145 y_stride_(y_stride),
146 u_stride_(u_stride),
147 v_stride_(v_stride),
148 no_longer_used_cb_(no_longer_used) {}
149
150 int width() const override { return width_; }
151
152 int height() const override { return height_; }
153
154 const uint8_t* DataY() const override { return y_plane_; }
155
156 const uint8_t* DataU() const override { return u_plane_; }
157
158 const uint8_t* DataV() const override { return v_plane_; }
159
160 int StrideY() const override { return y_stride_; }
161
162 int StrideU() const override { return u_stride_; }
163
164 int StrideV() const override { return v_stride_; }
165
166 private:
167 friend class rtc::RefCountedObject<WrappedYuvBuffer>;
168
169 ~WrappedYuvBuffer() override { no_longer_used_cb_(); }
170
171 const int width_;
172 const int height_;
173 const uint8_t* const y_plane_;
174 const uint8_t* const u_plane_;
175 const uint8_t* const v_plane_;
176 const int y_stride_;
177 const int u_stride_;
178 const int v_stride_;
179 rtc::Callback0<void> no_longer_used_cb_;
180};
181
182rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer(
183 int width,
184 int height,
185 const uint8_t* y_plane,
186 int y_stride,
187 const uint8_t* u_plane,
188 int u_stride,
189 const uint8_t* v_plane,
190 int v_stride,
191 const rtc::Callback0<void>& no_longer_used) {
192 return rtc::scoped_refptr<I420BufferInterface>(
193 new rtc::RefCountedObject<WrappedYuvBuffer<I420BufferInterface>>(
194 width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
195 v_stride, no_longer_used));
196}
197
198rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
199 int width,
200 int height,
201 const uint8_t* y_plane,
202 int y_stride,
203 const uint8_t* u_plane,
204 int u_stride,
205 const uint8_t* v_plane,
206 int v_stride,
207 const rtc::Callback0<void>& no_longer_used) {
208 return rtc::scoped_refptr<I444BufferInterface>(
209 new rtc::RefCountedObject<WrappedYuvBuffer<I444BufferInterface>>(
210 width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
211 v_stride, no_longer_used));
212}
213
214rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer(
215 VideoFrameBuffer::Type type,
216 int width,
217 int height,
218 const uint8_t* y_plane,
219 int y_stride,
220 const uint8_t* u_plane,
221 int u_stride,
222 const uint8_t* v_plane,
223 int v_stride,
224 const rtc::Callback0<void>& no_longer_used) {
225 switch (type) {
226 case VideoFrameBuffer::Type::kI420:
227 return WrapI420Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
228 v_plane, v_stride, no_longer_used);
229 case VideoFrameBuffer::Type::kI444:
230 return WrapI444Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
231 v_plane, v_stride, no_longer_used);
232 default:
233 FATAL() << "Unexpected frame buffer type.";
234 return nullptr;
235 }
236}
237
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000238} // namespace webrtc