blob: 3e12c75a45d13c6fc32ca42e816ad9e39f428f63 [file] [log] [blame]
nisseaf916892017-01-10 07:44:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_VIDEO_VIDEO_FRAME_BUFFER_H_
12#define API_VIDEO_VIDEO_FRAME_BUFFER_H_
nisseaf916892017-01-10 07:44:26 -080013
14#include <stdint.h>
15
Evan Shrubsoleb556b082020-10-08 14:56:45 +020016#include "api/array_view.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010017#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/ref_count.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020019#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080020
21namespace webrtc {
22
magjedeaf4a1e2017-05-30 01:21:59 -070023class I420BufferInterface;
Emircan Uysaler574eaa42017-11-09 12:33:24 -080024class I420ABufferInterface;
magjedeaf4a1e2017-05-30 01:21:59 -070025class I444BufferInterface;
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070026class I010BufferInterface;
Evan Shrubsole84995432020-09-09 16:14:19 +020027class NV12BufferInterface;
magjed712338e2017-05-11 05:11:57 -070028
29// Base class for frame buffers of different types of pixel format and storage.
30// The tag in type() indicates how the data is represented, and each type is
31// implemented as a subclass. To access the pixel data, call the appropriate
32// GetXXX() function, where XXX represents the type. There is also a function
33// ToI420() that returns a frame buffer in I420 format, converting from the
34// underlying representation if necessary. I420 is the most widely accepted
35// format and serves as a fallback for video sinks that can only handle I420,
36// e.g. the internal WebRTC software encoders. A special enum value 'kNative' is
37// provided for external clients to implement their own frame buffer
38// representations, e.g. as textures. The external client can produce such
39// native frame buffers from custom video sources, and then cast it back to the
40// correct subclass in custom video sinks. The purpose of this is to improve
41// performance by providing an optimized path without intermediate conversions.
42// Frame metadata such as rotation and timestamp are stored in
43// webrtc::VideoFrame, and not here.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020044class RTC_EXPORT VideoFrameBuffer : public rtc::RefCountInterface {
nisseaf916892017-01-10 07:44:26 -080045 public:
magjed712338e2017-05-11 05:11:57 -070046 // New frame buffer types will be added conservatively when there is an
47 // opportunity to optimize the path between some pair of video source and
48 // video sink.
Byoungchan Leef740c252021-07-24 06:16:20 +090049 // GENERATED_JAVA_ENUM_PACKAGE: org.webrtc
50 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: VideoFrameBufferType
magjed712338e2017-05-11 05:11:57 -070051 enum class Type {
52 kNative,
53 kI420,
Emircan Uysaler574eaa42017-11-09 12:33:24 -080054 kI420A,
magjed712338e2017-05-11 05:11:57 -070055 kI444,
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070056 kI010,
Evan Shrubsole84995432020-09-09 16:14:19 +020057 kNV12,
magjed712338e2017-05-11 05:11:57 -070058 };
59
60 // This function specifies in what pixel format the data is stored in.
Magnus Jedvert224e6592017-06-30 12:14:42 +000061 virtual Type type() const = 0;
magjed712338e2017-05-11 05:11:57 -070062
nisseaf916892017-01-10 07:44:26 -080063 // The resolution of the frame in pixels. For formats where some planes are
64 // subsampled, this is the highest-resolution plane.
65 virtual int width() const = 0;
66 virtual int height() const = 0;
67
magjed712338e2017-05-11 05:11:57 -070068 // Returns a memory-backed frame buffer in I420 format. If the pixel data is
69 // in another format, a conversion will take place. All implementations must
70 // provide a fallback to I420 for compatibility with e.g. the internal WebRTC
71 // software encoders.
Magnus Jedvert224e6592017-06-30 12:14:42 +000072 virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0;
magjed712338e2017-05-11 05:11:57 -070073
Ilya Nikolaevskiyade0dc92019-04-29 11:25:50 +020074 // GetI420() methods should return I420 buffer if conversion is trivial, i.e
75 // no change for binary data is needed. Otherwise these methods should return
76 // nullptr. One example of buffer with that property is
77 // WebrtcVideoFrameAdapter in Chrome - it's I420 buffer backed by a shared
78 // memory buffer. Therefore it must have type kNative. Yet, ToI420()
79 // doesn't affect binary data at all. Another example is any I420A buffer.
Evan Shrubsoleb556b082020-10-08 14:56:45 +020080 // TODO(https://crbug.com/webrtc/12021): Make this method non-virtual and
81 // behave as the other GetXXX methods below.
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +020082 virtual const I420BufferInterface* GetI420() const;
Ilya Nikolaevskiyade0dc92019-04-29 11:25:50 +020083
Ilya Nikolaevskiy38e9b062020-10-08 14:36:33 +000084 // A format specific scale function. Default implementation works by
85 // converting to I420. But more efficient implementations may override it,
86 // especially for kNative.
Artem Titov0e61fdd2021-07-25 21:50:14 +020087 // First, the image is cropped to `crop_width` and `crop_height` and then
88 // scaled to `scaled_width` and `scaled_height`.
Ilya Nikolaevskiy38e9b062020-10-08 14:36:33 +000089 virtual rtc::scoped_refptr<VideoFrameBuffer> CropAndScale(int offset_x,
90 int offset_y,
91 int crop_width,
92 int crop_height,
93 int scaled_width,
94 int scaled_height);
95
96 // Alias for common use case.
97 rtc::scoped_refptr<VideoFrameBuffer> Scale(int scaled_width,
98 int scaled_height) {
99 return CropAndScale(0, 0, width(), height(), scaled_width, scaled_height);
100 }
101
Ilya Nikolaevskiyade0dc92019-04-29 11:25:50 +0200102 // These functions should only be called if type() is of the correct type.
103 // Calling with a different type will result in a crash.
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800104 const I420ABufferInterface* GetI420A() const;
magjed3f075492017-06-01 10:02:26 -0700105 const I444BufferInterface* GetI444() const;
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700106 const I010BufferInterface* GetI010() const;
Evan Shrubsole84995432020-09-09 16:14:19 +0200107 const NV12BufferInterface* GetNV12() const;
magjed712338e2017-05-11 05:11:57 -0700108
Evan Shrubsoleb556b082020-10-08 14:56:45 +0200109 // From a kNative frame, returns a VideoFrameBuffer with a pixel format in
110 // the list of types that is in the main memory with a pixel perfect
111 // conversion for encoding with a software encoder. Returns nullptr if the
112 // frame type is not supported, mapping is not possible, or if the kNative
113 // frame has not implemented this method. Only callable if type() is kNative.
114 virtual rtc::scoped_refptr<VideoFrameBuffer> GetMappedFrameBuffer(
115 rtc::ArrayView<Type> types);
116
nisseaf916892017-01-10 07:44:26 -0800117 protected:
118 ~VideoFrameBuffer() override {}
119};
120
Evan Shrubsoleb556b082020-10-08 14:56:45 +0200121// Update when VideoFrameBuffer::Type is updated.
122const char* VideoFrameBufferTypeToString(VideoFrameBuffer::Type type);
123
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700124// This interface represents planar formats.
magjed712338e2017-05-11 05:11:57 -0700125class PlanarYuvBuffer : public VideoFrameBuffer {
126 public:
magjedeaf4a1e2017-05-30 01:21:59 -0700127 virtual int ChromaWidth() const = 0;
128 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 05:11:57 -0700129
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700130 // Returns the number of steps(in terms of Data*() return type) between
131 // successive rows for a given plane.
Magnus Jedvert224e6592017-06-30 12:14:42 +0000132 virtual int StrideY() const = 0;
133 virtual int StrideU() const = 0;
134 virtual int StrideV() const = 0;
magjed712338e2017-05-11 05:11:57 -0700135
magjed712338e2017-05-11 05:11:57 -0700136 protected:
137 ~PlanarYuvBuffer() override {}
138};
139
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700140// This interface represents 8-bit color depth formats: Type::kI420,
141// Type::kI420A and Type::kI444.
142class PlanarYuv8Buffer : public PlanarYuvBuffer {
143 public:
144 // Returns pointer to the pixel data for a given plane. The memory is owned by
145 // the VideoFrameBuffer object and must not be freed by the caller.
146 virtual const uint8_t* DataY() const = 0;
147 virtual const uint8_t* DataU() const = 0;
148 virtual const uint8_t* DataV() const = 0;
149
150 protected:
151 ~PlanarYuv8Buffer() override {}
152};
153
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200154class RTC_EXPORT I420BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 01:21:59 -0700155 public:
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800156 Type type() const override;
magjedeaf4a1e2017-05-30 01:21:59 -0700157
158 int ChromaWidth() const final;
159 int ChromaHeight() const final;
160
161 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +0200162 const I420BufferInterface* GetI420() const final;
magjedeaf4a1e2017-05-30 01:21:59 -0700163
164 protected:
165 ~I420BufferInterface() override {}
166};
167
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200168class RTC_EXPORT I420ABufferInterface : public I420BufferInterface {
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800169 public:
170 Type type() const final;
171 virtual const uint8_t* DataA() const = 0;
172 virtual int StrideA() const = 0;
173
174 protected:
175 ~I420ABufferInterface() override {}
176};
177
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700178class I444BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 01:21:59 -0700179 public:
180 Type type() const final;
181
182 int ChromaWidth() const final;
183 int ChromaHeight() const final;
184
magjedeaf4a1e2017-05-30 01:21:59 -0700185 protected:
186 ~I444BufferInterface() override {}
187};
188
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700189// This interface represents 8-bit to 16-bit color depth formats: Type::kI010.
190class PlanarYuv16BBuffer : public PlanarYuvBuffer {
191 public:
192 // Returns pointer to the pixel data for a given plane. The memory is owned by
193 // the VideoFrameBuffer object and must not be freed by the caller.
194 virtual const uint16_t* DataY() const = 0;
195 virtual const uint16_t* DataU() const = 0;
196 virtual const uint16_t* DataV() const = 0;
197
198 protected:
199 ~PlanarYuv16BBuffer() override {}
200};
201
202// Represents Type::kI010, allocates 16 bits per pixel and fills 10 least
203// significant bits with color information.
204class I010BufferInterface : public PlanarYuv16BBuffer {
205 public:
206 Type type() const override;
207
208 int ChromaWidth() const final;
209 int ChromaHeight() const final;
210
211 protected:
212 ~I010BufferInterface() override {}
213};
214
Evan Shrubsole84995432020-09-09 16:14:19 +0200215class BiplanarYuvBuffer : public VideoFrameBuffer {
216 public:
217 virtual int ChromaWidth() const = 0;
218 virtual int ChromaHeight() const = 0;
219
220 // Returns the number of steps(in terms of Data*() return type) between
221 // successive rows for a given plane.
222 virtual int StrideY() const = 0;
223 virtual int StrideUV() const = 0;
224
225 protected:
226 ~BiplanarYuvBuffer() override {}
227};
228
229class BiplanarYuv8Buffer : public BiplanarYuvBuffer {
230 public:
231 virtual const uint8_t* DataY() const = 0;
232 virtual const uint8_t* DataUV() const = 0;
233
234 protected:
235 ~BiplanarYuv8Buffer() override {}
236};
237
238// Represents Type::kNV12. NV12 is full resolution Y and half-resolution
239// interleved UV.
Evan Shrubsole0d1b0442020-10-07 18:09:56 +0200240class RTC_EXPORT NV12BufferInterface : public BiplanarYuv8Buffer {
Evan Shrubsole84995432020-09-09 16:14:19 +0200241 public:
242 Type type() const override;
243
244 int ChromaWidth() const final;
245 int ChromaHeight() const final;
246
Henrik Boströmf4129762021-03-22 10:22:54 +0100247 rtc::scoped_refptr<VideoFrameBuffer> CropAndScale(int offset_x,
248 int offset_y,
249 int crop_width,
250 int crop_height,
251 int scaled_width,
252 int scaled_height) override;
253
Evan Shrubsole84995432020-09-09 16:14:19 +0200254 protected:
255 ~NV12BufferInterface() override {}
256};
257
nisseaf916892017-01-10 07:44:26 -0800258} // namespace webrtc
259
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200260#endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_