blob: 9c8955cbd74db6b581e26a48a1b15f850bea43dd [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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "common_video/include/video_frame_buffer.h"
perkj0489e492016-10-20 00:24:01 -070011
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
nisse7cc9cc02016-03-29 23:44:19 -070016#include "libyuv/convert.h"
fbarchardd1f83cf2016-07-08 17:33:20 -070017#include "libyuv/planar_functions.h"
Niels Möller718a7632016-06-13 13:06:01 +020018#include "libyuv/scale.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/keep_ref_until_done.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000021
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000022namespace webrtc {
23
Magnus Jedvertc464f502015-08-25 23:22:08 +020024WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +020025 int height,
26 const uint8_t* y_plane,
27 int y_stride,
28 const uint8_t* u_plane,
29 int u_stride,
30 const uint8_t* v_plane,
31 int v_stride,
32 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +020033 : width_(width),
34 height_(height),
35 y_plane_(y_plane),
36 u_plane_(u_plane),
37 v_plane_(v_plane),
38 y_stride_(y_stride),
39 u_stride_(u_stride),
40 v_stride_(v_stride),
41 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +020042}
43
44WrappedI420Buffer::~WrappedI420Buffer() {
45 no_longer_used_cb_();
46}
47
Per33544192015-04-02 12:30:51 +020048int WrappedI420Buffer::width() const {
49 return width_;
50}
51
52int WrappedI420Buffer::height() const {
53 return height_;
54}
55
nisse06176e42016-04-18 05:34:40 -070056const uint8_t* WrappedI420Buffer::DataY() const {
57 return y_plane_;
58}
59const uint8_t* WrappedI420Buffer::DataU() const {
60 return u_plane_;
61}
62const uint8_t* WrappedI420Buffer::DataV() const {
63 return v_plane_;
Per33544192015-04-02 12:30:51 +020064}
65
nisse06176e42016-04-18 05:34:40 -070066int WrappedI420Buffer::StrideY() const {
67 return y_stride_;
68}
69int WrappedI420Buffer::StrideU() const {
70 return u_stride_;
71}
72int WrappedI420Buffer::StrideV() const {
73 return v_stride_;
Per33544192015-04-02 12:30:51 +020074}
75
yuweih23cc4682017-06-22 20:28:06 -070076// Template to implement a wrapped buffer for a I4??BufferInterface.
77template <typename Base>
78class WrappedYuvBuffer : public Base {
79 public:
80 WrappedYuvBuffer(int width,
81 int height,
82 const uint8_t* y_plane,
83 int y_stride,
84 const uint8_t* u_plane,
85 int u_stride,
86 const uint8_t* v_plane,
87 int v_stride,
88 const rtc::Callback0<void>& no_longer_used)
89 : width_(width),
90 height_(height),
91 y_plane_(y_plane),
92 u_plane_(u_plane),
93 v_plane_(v_plane),
94 y_stride_(y_stride),
95 u_stride_(u_stride),
96 v_stride_(v_stride),
97 no_longer_used_cb_(no_longer_used) {}
98
Emircan Uysaler574eaa42017-11-09 12:33:24 -080099 ~WrappedYuvBuffer() override { no_longer_used_cb_(); }
100
yuweih23cc4682017-06-22 20:28:06 -0700101 int width() const override { return width_; }
102
103 int height() const override { return height_; }
104
105 const uint8_t* DataY() const override { return y_plane_; }
106
107 const uint8_t* DataU() const override { return u_plane_; }
108
109 const uint8_t* DataV() const override { return v_plane_; }
110
111 int StrideY() const override { return y_stride_; }
112
113 int StrideU() const override { return u_stride_; }
114
115 int StrideV() const override { return v_stride_; }
116
117 private:
118 friend class rtc::RefCountedObject<WrappedYuvBuffer>;
119
yuweih23cc4682017-06-22 20:28:06 -0700120 const int width_;
121 const int height_;
122 const uint8_t* const y_plane_;
123 const uint8_t* const u_plane_;
124 const uint8_t* const v_plane_;
125 const int y_stride_;
126 const int u_stride_;
127 const int v_stride_;
128 rtc::Callback0<void> no_longer_used_cb_;
129};
130
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800131// Template to implement a wrapped buffer for a I4??BufferInterface.
132template <typename BaseWithA>
133class WrappedYuvaBuffer : public WrappedYuvBuffer<BaseWithA> {
134 public:
135 WrappedYuvaBuffer(int width,
136 int height,
137 const uint8_t* y_plane,
138 int y_stride,
139 const uint8_t* u_plane,
140 int u_stride,
141 const uint8_t* v_plane,
142 int v_stride,
143 const uint8_t* a_plane,
144 int a_stride,
145 const rtc::Callback0<void>& no_longer_used)
146 : WrappedYuvBuffer<BaseWithA>(width,
147 height,
148 y_plane,
149 y_stride,
150 u_plane,
151 u_stride,
152 v_plane,
153 v_stride,
154 no_longer_used),
155 a_plane_(a_plane),
156 a_stride_(a_stride) {}
157
158 const uint8_t* DataA() const override { return a_plane_; }
159 int StrideA() const override { return a_stride_; }
160
161 private:
162 const uint8_t* const a_plane_;
163 const int a_stride_;
164};
165
yuweih23cc4682017-06-22 20:28:06 -0700166rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer(
167 int width,
168 int height,
169 const uint8_t* y_plane,
170 int y_stride,
171 const uint8_t* u_plane,
172 int u_stride,
173 const uint8_t* v_plane,
174 int v_stride,
175 const rtc::Callback0<void>& no_longer_used) {
176 return rtc::scoped_refptr<I420BufferInterface>(
177 new rtc::RefCountedObject<WrappedYuvBuffer<I420BufferInterface>>(
178 width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
179 v_stride, no_longer_used));
180}
181
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800182rtc::scoped_refptr<I420ABufferInterface> WrapI420ABuffer(
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 uint8_t* a_plane,
192 int a_stride,
193 const rtc::Callback0<void>& no_longer_used) {
194 return rtc::scoped_refptr<I420ABufferInterface>(
195 new rtc::RefCountedObject<WrappedYuvaBuffer<I420ABufferInterface>>(
196 width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
197 v_stride, a_plane, a_stride, no_longer_used));
198}
199
yuweih23cc4682017-06-22 20:28:06 -0700200rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
201 int width,
202 int height,
203 const uint8_t* y_plane,
204 int y_stride,
205 const uint8_t* u_plane,
206 int u_stride,
207 const uint8_t* v_plane,
208 int v_stride,
209 const rtc::Callback0<void>& no_longer_used) {
210 return rtc::scoped_refptr<I444BufferInterface>(
211 new rtc::RefCountedObject<WrappedYuvBuffer<I444BufferInterface>>(
212 width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
213 v_stride, no_longer_used));
214}
215
216rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer(
217 VideoFrameBuffer::Type type,
218 int width,
219 int height,
220 const uint8_t* y_plane,
221 int y_stride,
222 const uint8_t* u_plane,
223 int u_stride,
224 const uint8_t* v_plane,
225 int v_stride,
226 const rtc::Callback0<void>& no_longer_used) {
227 switch (type) {
228 case VideoFrameBuffer::Type::kI420:
229 return WrapI420Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
230 v_plane, v_stride, no_longer_used);
231 case VideoFrameBuffer::Type::kI444:
232 return WrapI444Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
233 v_plane, v_stride, no_longer_used);
234 default:
235 FATAL() << "Unexpected frame buffer type.";
236 return nullptr;
237 }
238}
239
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000240} // namespace webrtc