blob: 19649cf68fa04d4da17fe4c3fd86c6e1ffc662d9 [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
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.org2386d6d2015-03-05 14:03:08 +0000187} // namespace webrtc