nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_VIDEO_VIDEO_FRAME_BUFFER_H_ |
| 12 | #define API_VIDEO_VIDEO_FRAME_BUFFER_H_ |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 16 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "rtc_base/ref_count.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 21 | class I420BufferInterface; |
Emircan Uysaler | 574eaa4 | 2017-11-09 12:33:24 -0800 | [diff] [blame] | 22 | class I420ABufferInterface; |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 23 | class I444BufferInterface; |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 24 | class I010BufferInterface; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 25 | |
| 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. |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 41 | class VideoFrameBuffer : public rtc::RefCountInterface { |
| 42 | public: |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 43 | // 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 Uysaler | 574eaa4 | 2017-11-09 12:33:24 -0800 | [diff] [blame] | 49 | kI420A, |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 50 | kI444, |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 51 | kI010, |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // This function specifies in what pixel format the data is stored in. |
Magnus Jedvert | 224e659 | 2017-06-30 12:14:42 +0000 | [diff] [blame] | 55 | virtual Type type() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 56 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 57 | // 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 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 62 | // 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 Jedvert | 224e659 | 2017-06-30 12:14:42 +0000 | [diff] [blame] | 66 | virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 67 | |
Ilya Nikolaevskiy | ade0dc9 | 2019-04-29 11:25:50 +0200 | [diff] [blame] | 68 | // 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 Nikolaevskiy | a8507e3 | 2019-05-03 11:39:26 +0200 | [diff] [blame^] | 74 | virtual const I420BufferInterface* GetI420() const; |
Ilya Nikolaevskiy | ade0dc9 | 2019-04-29 11:25:50 +0200 | [diff] [blame] | 75 | |
| 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 Uysaler | 574eaa4 | 2017-11-09 12:33:24 -0800 | [diff] [blame] | 78 | const I420ABufferInterface* GetI420A() const; |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 79 | const I444BufferInterface* GetI444() const; |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 80 | const I010BufferInterface* GetI010() const; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 81 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 82 | protected: |
| 83 | ~VideoFrameBuffer() override {} |
| 84 | }; |
| 85 | |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 86 | // This interface represents planar formats. |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 87 | class PlanarYuvBuffer : public VideoFrameBuffer { |
| 88 | public: |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 89 | virtual int ChromaWidth() const = 0; |
| 90 | virtual int ChromaHeight() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 91 | |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 92 | // Returns the number of steps(in terms of Data*() return type) between |
| 93 | // successive rows for a given plane. |
Magnus Jedvert | 224e659 | 2017-06-30 12:14:42 +0000 | [diff] [blame] | 94 | virtual int StrideY() const = 0; |
| 95 | virtual int StrideU() const = 0; |
| 96 | virtual int StrideV() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 97 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 98 | protected: |
| 99 | ~PlanarYuvBuffer() override {} |
| 100 | }; |
| 101 | |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 102 | // This interface represents 8-bit color depth formats: Type::kI420, |
| 103 | // Type::kI420A and Type::kI444. |
| 104 | class 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 | |
| 116 | class I420BufferInterface : public PlanarYuv8Buffer { |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 117 | public: |
Emircan Uysaler | 574eaa4 | 2017-11-09 12:33:24 -0800 | [diff] [blame] | 118 | Type type() const override; |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 119 | |
| 120 | int ChromaWidth() const final; |
| 121 | int ChromaHeight() const final; |
| 122 | |
| 123 | rtc::scoped_refptr<I420BufferInterface> ToI420() final; |
Ilya Nikolaevskiy | a8507e3 | 2019-05-03 11:39:26 +0200 | [diff] [blame^] | 124 | const I420BufferInterface* GetI420() const final; |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 125 | |
| 126 | protected: |
| 127 | ~I420BufferInterface() override {} |
| 128 | }; |
| 129 | |
Emircan Uysaler | 574eaa4 | 2017-11-09 12:33:24 -0800 | [diff] [blame] | 130 | class 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 Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 140 | class I444BufferInterface : public PlanarYuv8Buffer { |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 141 | public: |
| 142 | Type type() const final; |
| 143 | |
| 144 | int ChromaWidth() const final; |
| 145 | int ChromaHeight() const final; |
| 146 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 147 | protected: |
| 148 | ~I444BufferInterface() override {} |
| 149 | }; |
| 150 | |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 151 | // This interface represents 8-bit to 16-bit color depth formats: Type::kI010. |
| 152 | class 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. |
| 166 | class 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 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 177 | } // namespace webrtc |
| 178 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 179 | #endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_ |