blob: 3b8db14bf96568b51f4b66b7d21074b220c803a8 [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.
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +020074 virtual const I420BufferInterface* GetI420() const;
Ilya Nikolaevskiyade0dc92019-04-29 11:25:50 +020075
76 // These functions should only be called if type() is of the correct type.
77 // Calling with a different type will result in a crash.
Emircan Uysaler574eaa42017-11-09 12:33:24 -080078 const I420ABufferInterface* GetI420A() const;
magjed3f075492017-06-01 10:02:26 -070079 const I444BufferInterface* GetI444() const;
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070080 const I010BufferInterface* GetI010() const;
magjed712338e2017-05-11 05:11:57 -070081
nisseaf916892017-01-10 07:44:26 -080082 protected:
83 ~VideoFrameBuffer() override {}
84};
85
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070086// This interface represents planar formats.
magjed712338e2017-05-11 05:11:57 -070087class PlanarYuvBuffer : public VideoFrameBuffer {
88 public:
magjedeaf4a1e2017-05-30 01:21:59 -070089 virtual int ChromaWidth() const = 0;
90 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 05:11:57 -070091
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070092 // Returns the number of steps(in terms of Data*() return type) between
93 // successive rows for a given plane.
Magnus Jedvert224e6592017-06-30 12:14:42 +000094 virtual int StrideY() const = 0;
95 virtual int StrideU() const = 0;
96 virtual int StrideV() const = 0;
magjed712338e2017-05-11 05:11:57 -070097
magjed712338e2017-05-11 05:11:57 -070098 protected:
99 ~PlanarYuvBuffer() override {}
100};
101
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700102// This interface represents 8-bit color depth formats: Type::kI420,
103// Type::kI420A and Type::kI444.
104class PlanarYuv8Buffer : public PlanarYuvBuffer {
105 public:
106 // Returns pointer to the pixel data for a given plane. The memory is owned by
107 // the VideoFrameBuffer object and must not be freed by the caller.
108 virtual const uint8_t* DataY() const = 0;
109 virtual const uint8_t* DataU() const = 0;
110 virtual const uint8_t* DataV() const = 0;
111
112 protected:
113 ~PlanarYuv8Buffer() override {}
114};
115
116class I420BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 01:21:59 -0700117 public:
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800118 Type type() const override;
magjedeaf4a1e2017-05-30 01:21:59 -0700119
120 int ChromaWidth() const final;
121 int ChromaHeight() const final;
122
123 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +0200124 const I420BufferInterface* GetI420() const final;
magjedeaf4a1e2017-05-30 01:21:59 -0700125
126 protected:
127 ~I420BufferInterface() override {}
128};
129
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800130class I420ABufferInterface : public I420BufferInterface {
131 public:
132 Type type() const final;
133 virtual const uint8_t* DataA() const = 0;
134 virtual int StrideA() const = 0;
135
136 protected:
137 ~I420ABufferInterface() override {}
138};
139
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700140class I444BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 01:21:59 -0700141 public:
142 Type type() const final;
143
144 int ChromaWidth() const final;
145 int ChromaHeight() const final;
146
magjedeaf4a1e2017-05-30 01:21:59 -0700147 protected:
148 ~I444BufferInterface() override {}
149};
150
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700151// This interface represents 8-bit to 16-bit color depth formats: Type::kI010.
152class PlanarYuv16BBuffer : public PlanarYuvBuffer {
153 public:
154 // Returns pointer to the pixel data for a given plane. The memory is owned by
155 // the VideoFrameBuffer object and must not be freed by the caller.
156 virtual const uint16_t* DataY() const = 0;
157 virtual const uint16_t* DataU() const = 0;
158 virtual const uint16_t* DataV() const = 0;
159
160 protected:
161 ~PlanarYuv16BBuffer() override {}
162};
163
164// Represents Type::kI010, allocates 16 bits per pixel and fills 10 least
165// significant bits with color information.
166class I010BufferInterface : public PlanarYuv16BBuffer {
167 public:
168 Type type() const override;
169
170 int ChromaWidth() const final;
171 int ChromaHeight() const final;
172
173 protected:
174 ~I010BufferInterface() override {}
175};
176
nisseaf916892017-01-10 07:44:26 -0800177} // namespace webrtc
178
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200179#endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_