blob: 3c4d2a0a9f1d192ae9de2292cb807cba969c7c40 [file] [log] [blame]
magjed@webrtc.org398a7842015-03-05 14:03:081/*
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 */
perkjd0271782016-10-20 07:24:0110#include "webrtc/common_video/include/video_frame_buffer.h"
11
12#include <string.h>
magjed@webrtc.org398a7842015-03-05 14:03:0813
Niels Möller34d0c322016-06-13 11:06:0114#include <algorithm>
15
Henrik Kjellanderbc324102017-07-01 14:48:1516#include "webrtc/base/checks.h"
17#include "webrtc/base/keep_ref_until_done.h"
nisse5ed79722016-03-30 06:44:1918#include "libyuv/convert.h"
fbarchard8bd39242016-07-09 00:33:2019#include "libyuv/planar_functions.h"
Niels Möller34d0c322016-06-13 11:06:0120#include "libyuv/scale.h"
magjed@webrtc.org398a7842015-03-05 14:03:0821
magjed@webrtc.org398a7842015-03-05 14:03:0822namespace webrtc {
23
Magnus Jedverta5090c12015-08-25 21:22:0824WrappedI420Buffer::WrappedI420Buffer(int width,
Perf92c6f82015-04-02 10:30:5125 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 Jedverta5090c12015-08-25 21:22:0833 : 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) {
Perf92c6f82015-04-02 10:30:5142}
43
44WrappedI420Buffer::~WrappedI420Buffer() {
45 no_longer_used_cb_();
46}
47
Perf92c6f82015-04-02 10:30:5148int WrappedI420Buffer::width() const {
49 return width_;
50}
51
52int WrappedI420Buffer::height() const {
53 return height_;
54}
55
nisse1e4f6062016-04-18 12:34:4056const 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_;
Perf92c6f82015-04-02 10:30:5164}
65
nisse1e4f6062016-04-18 12:34:4066int 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_;
Perf92c6f82015-04-02 10:30:5174}
75
yuweih7c9d6a22017-06-23 03:28:0676// 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
99 int width() const override { return width_; }
100
101 int height() const override { return height_; }
102
103 const uint8_t* DataY() const override { return y_plane_; }
104
105 const uint8_t* DataU() const override { return u_plane_; }
106
107 const uint8_t* DataV() const override { return v_plane_; }
108
109 int StrideY() const override { return y_stride_; }
110
111 int StrideU() const override { return u_stride_; }
112
113 int StrideV() const override { return v_stride_; }
114
115 private:
116 friend class rtc::RefCountedObject<WrappedYuvBuffer>;
117
118 ~WrappedYuvBuffer() override { no_longer_used_cb_(); }
119
120 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
131rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer(
132 int width,
133 int height,
134 const uint8_t* y_plane,
135 int y_stride,
136 const uint8_t* u_plane,
137 int u_stride,
138 const uint8_t* v_plane,
139 int v_stride,
140 const rtc::Callback0<void>& no_longer_used) {
141 return rtc::scoped_refptr<I420BufferInterface>(
142 new rtc::RefCountedObject<WrappedYuvBuffer<I420BufferInterface>>(
143 width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
144 v_stride, no_longer_used));
145}
146
147rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
148 int width,
149 int height,
150 const uint8_t* y_plane,
151 int y_stride,
152 const uint8_t* u_plane,
153 int u_stride,
154 const uint8_t* v_plane,
155 int v_stride,
156 const rtc::Callback0<void>& no_longer_used) {
157 return rtc::scoped_refptr<I444BufferInterface>(
158 new rtc::RefCountedObject<WrappedYuvBuffer<I444BufferInterface>>(
159 width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
160 v_stride, no_longer_used));
161}
162
163rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer(
164 VideoFrameBuffer::Type type,
165 int width,
166 int height,
167 const uint8_t* y_plane,
168 int y_stride,
169 const uint8_t* u_plane,
170 int u_stride,
171 const uint8_t* v_plane,
172 int v_stride,
173 const rtc::Callback0<void>& no_longer_used) {
174 switch (type) {
175 case VideoFrameBuffer::Type::kI420:
176 return WrapI420Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
177 v_plane, v_stride, no_longer_used);
178 case VideoFrameBuffer::Type::kI444:
179 return WrapI444Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
180 v_plane, v_stride, no_longer_used);
181 default:
182 FATAL() << "Unexpected frame buffer type.";
183 return nullptr;
184 }
185}
186
magjed@webrtc.org398a7842015-03-05 14:03:08187} // namespace webrtc