blob: af18e57db7625ff9fbb705b8e6dc2ef67953a566 [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
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/ref_count.h"
nisseaf916892017-01-10 07:44:26 -080018
19namespace webrtc {
20
magjedeaf4a1e2017-05-30 01:21:59 -070021class I420BufferInterface;
Emircan Uysaler574eaa42017-11-09 12:33:24 -080022class I420ABufferInterface;
magjedeaf4a1e2017-05-30 01:21:59 -070023class I444BufferInterface;
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070024class I010BufferInterface;
magjed712338e2017-05-11 05:11:57 -070025
26// Base class for frame buffers of different types of pixel format and storage.
27// The tag in type() indicates how the data is represented, and each type is
28// implemented as a subclass. To access the pixel data, call the appropriate
29// GetXXX() function, where XXX represents the type. There is also a function
30// ToI420() that returns a frame buffer in I420 format, converting from the
31// underlying representation if necessary. I420 is the most widely accepted
32// format and serves as a fallback for video sinks that can only handle I420,
33// e.g. the internal WebRTC software encoders. A special enum value 'kNative' is
34// provided for external clients to implement their own frame buffer
35// representations, e.g. as textures. The external client can produce such
36// native frame buffers from custom video sources, and then cast it back to the
37// correct subclass in custom video sinks. The purpose of this is to improve
38// performance by providing an optimized path without intermediate conversions.
39// Frame metadata such as rotation and timestamp are stored in
40// webrtc::VideoFrame, and not here.
nisseaf916892017-01-10 07:44:26 -080041class VideoFrameBuffer : public rtc::RefCountInterface {
42 public:
magjed712338e2017-05-11 05:11:57 -070043 // New frame buffer types will be added conservatively when there is an
44 // opportunity to optimize the path between some pair of video source and
45 // video sink.
46 enum class Type {
47 kNative,
48 kI420,
Emircan Uysaler574eaa42017-11-09 12:33:24 -080049 kI420A,
magjed712338e2017-05-11 05:11:57 -070050 kI444,
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070051 kI010,
magjed712338e2017-05-11 05:11:57 -070052 };
53
54 // This function specifies in what pixel format the data is stored in.
Magnus Jedvert224e6592017-06-30 12:14:42 +000055 virtual Type type() const = 0;
magjed712338e2017-05-11 05:11:57 -070056
nisseaf916892017-01-10 07:44:26 -080057 // The resolution of the frame in pixels. For formats where some planes are
58 // subsampled, this is the highest-resolution plane.
59 virtual int width() const = 0;
60 virtual int height() const = 0;
61
magjed712338e2017-05-11 05:11:57 -070062 // Returns a memory-backed frame buffer in I420 format. If the pixel data is
63 // in another format, a conversion will take place. All implementations must
64 // provide a fallback to I420 for compatibility with e.g. the internal WebRTC
65 // software encoders.
Magnus Jedvert224e6592017-06-30 12:14:42 +000066 virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0;
magjed712338e2017-05-11 05:11:57 -070067
Ilya Nikolaevskiyade0dc92019-04-29 11:25:50 +020068 // GetI420() methods should return I420 buffer if conversion is trivial, i.e
69 // no change for binary data is needed. Otherwise these methods should return
70 // nullptr. One example of buffer with that property is
71 // WebrtcVideoFrameAdapter in Chrome - it's I420 buffer backed by a shared
72 // memory buffer. Therefore it must have type kNative. Yet, ToI420()
73 // doesn't affect binary data at all. Another example is any I420A buffer.
magjed3f075492017-06-01 10:02:26 -070074 // TODO(magjed): Return raw pointers for GetI420 once deprecated interface is
75 // removed.
Ilya Nikolaevskiyade0dc92019-04-29 11:25:50 +020076 virtual rtc::scoped_refptr<I420BufferInterface> GetI420();
77 virtual rtc::scoped_refptr<const I420BufferInterface> GetI420() const;
78
79 // These functions should only be called if type() is of the correct type.
80 // Calling with a different type will result in a crash.
Emircan Uysaler574eaa42017-11-09 12:33:24 -080081 I420ABufferInterface* GetI420A();
82 const I420ABufferInterface* GetI420A() const;
magjed3f075492017-06-01 10:02:26 -070083 I444BufferInterface* GetI444();
84 const I444BufferInterface* GetI444() const;
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070085 I010BufferInterface* GetI010();
86 const I010BufferInterface* GetI010() const;
magjed712338e2017-05-11 05:11:57 -070087
nisseaf916892017-01-10 07:44:26 -080088 protected:
89 ~VideoFrameBuffer() override {}
90};
91
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070092// This interface represents planar formats.
magjed712338e2017-05-11 05:11:57 -070093class PlanarYuvBuffer : public VideoFrameBuffer {
94 public:
magjedeaf4a1e2017-05-30 01:21:59 -070095 virtual int ChromaWidth() const = 0;
96 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 05:11:57 -070097
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070098 // Returns the number of steps(in terms of Data*() return type) between
99 // successive rows for a given plane.
Magnus Jedvert224e6592017-06-30 12:14:42 +0000100 virtual int StrideY() const = 0;
101 virtual int StrideU() const = 0;
102 virtual int StrideV() const = 0;
magjed712338e2017-05-11 05:11:57 -0700103
magjed712338e2017-05-11 05:11:57 -0700104 protected:
105 ~PlanarYuvBuffer() override {}
106};
107
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700108// This interface represents 8-bit color depth formats: Type::kI420,
109// Type::kI420A and Type::kI444.
110class PlanarYuv8Buffer : public PlanarYuvBuffer {
111 public:
112 // Returns pointer to the pixel data for a given plane. The memory is owned by
113 // the VideoFrameBuffer object and must not be freed by the caller.
114 virtual const uint8_t* DataY() const = 0;
115 virtual const uint8_t* DataU() const = 0;
116 virtual const uint8_t* DataV() const = 0;
117
118 protected:
119 ~PlanarYuv8Buffer() override {}
120};
121
122class I420BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 01:21:59 -0700123 public:
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800124 Type type() const override;
magjedeaf4a1e2017-05-30 01:21:59 -0700125
126 int ChromaWidth() const final;
127 int ChromaHeight() const final;
128
129 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
130
131 protected:
132 ~I420BufferInterface() override {}
133};
134
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800135class I420ABufferInterface : public I420BufferInterface {
136 public:
137 Type type() const final;
138 virtual const uint8_t* DataA() const = 0;
139 virtual int StrideA() const = 0;
140
141 protected:
142 ~I420ABufferInterface() override {}
143};
144
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700145class I444BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 01:21:59 -0700146 public:
147 Type type() const final;
148
149 int ChromaWidth() const final;
150 int ChromaHeight() const final;
151
magjedeaf4a1e2017-05-30 01:21:59 -0700152 protected:
153 ~I444BufferInterface() override {}
154};
155
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700156// This interface represents 8-bit to 16-bit color depth formats: Type::kI010.
157class PlanarYuv16BBuffer : public PlanarYuvBuffer {
158 public:
159 // Returns pointer to the pixel data for a given plane. The memory is owned by
160 // the VideoFrameBuffer object and must not be freed by the caller.
161 virtual const uint16_t* DataY() const = 0;
162 virtual const uint16_t* DataU() const = 0;
163 virtual const uint16_t* DataV() const = 0;
164
165 protected:
166 ~PlanarYuv16BBuffer() override {}
167};
168
169// Represents Type::kI010, allocates 16 bits per pixel and fills 10 least
170// significant bits with color information.
171class I010BufferInterface : public PlanarYuv16BBuffer {
172 public:
173 Type type() const override;
174
175 int ChromaWidth() const final;
176 int ChromaHeight() const final;
177
178 protected:
179 ~I010BufferInterface() override {}
180};
181
nisseaf916892017-01-10 07:44:26 -0800182} // namespace webrtc
183
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200184#endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_