nisse | af91689 | 2017-01-10 15:44:26 | [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 04:47:31 | [diff] [blame] | 11 | #ifndef API_VIDEO_VIDEO_FRAME_BUFFER_H_ |
| 12 | #define API_VIDEO_VIDEO_FRAME_BUFFER_H_ |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "rtc_base/refcount.h" |
| 17 | #include "rtc_base/scoped_ref_ptr.h" |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 21 | class I420BufferInterface; |
Emircan Uysaler | 574eaa4 | 2017-11-09 20:33:24 | [diff] [blame] | 22 | class I420ABufferInterface; |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 23 | class I444BufferInterface; |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 24 | class I010BufferInterface; |
magjed | 712338e | 2017-05-11 12:11:57 | [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 15:44:26 | [diff] [blame] | 41 | class VideoFrameBuffer : public rtc::RefCountInterface { |
| 42 | public: |
magjed | 712338e | 2017-05-11 12:11:57 | [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 20:33:24 | [diff] [blame] | 49 | kI420A, |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 50 | kI444, |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 51 | kI010, |
magjed | 712338e | 2017-05-11 12:11:57 | [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 | [diff] [blame] | 55 | virtual Type type() const = 0; |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 56 | |
nisse | af91689 | 2017-01-10 15:44:26 | [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 12:11:57 | [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 | [diff] [blame] | 66 | virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0; |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 67 | |
| 68 | // These functions should only be called if type() is of the correct type. |
| 69 | // Calling with a different type will result in a crash. |
magjed | 3f07549 | 2017-06-01 17:02:26 | [diff] [blame] | 70 | // TODO(magjed): Return raw pointers for GetI420 once deprecated interface is |
| 71 | // removed. |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 72 | rtc::scoped_refptr<I420BufferInterface> GetI420(); |
magjed | 3f07549 | 2017-06-01 17:02:26 | [diff] [blame] | 73 | rtc::scoped_refptr<const I420BufferInterface> GetI420() const; |
Emircan Uysaler | 574eaa4 | 2017-11-09 20:33:24 | [diff] [blame] | 74 | I420ABufferInterface* GetI420A(); |
| 75 | const I420ABufferInterface* GetI420A() const; |
magjed | 3f07549 | 2017-06-01 17:02:26 | [diff] [blame] | 76 | I444BufferInterface* GetI444(); |
| 77 | const I444BufferInterface* GetI444() const; |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 78 | I010BufferInterface* GetI010(); |
| 79 | const I010BufferInterface* GetI010() const; |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 80 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 81 | protected: |
| 82 | ~VideoFrameBuffer() override {} |
| 83 | }; |
| 84 | |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 85 | // This interface represents planar formats. |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 86 | class PlanarYuvBuffer : public VideoFrameBuffer { |
| 87 | public: |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 88 | virtual int ChromaWidth() const = 0; |
| 89 | virtual int ChromaHeight() const = 0; |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 90 | |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 91 | // Returns the number of steps(in terms of Data*() return type) between |
| 92 | // successive rows for a given plane. |
Magnus Jedvert | 224e659 | 2017-06-30 12:14:42 | [diff] [blame] | 93 | virtual int StrideY() const = 0; |
| 94 | virtual int StrideU() const = 0; |
| 95 | virtual int StrideV() const = 0; |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 96 | |
magjed | 712338e | 2017-05-11 12:11:57 | [diff] [blame] | 97 | protected: |
| 98 | ~PlanarYuvBuffer() override {} |
| 99 | }; |
| 100 | |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 101 | // This interface represents 8-bit color depth formats: Type::kI420, |
| 102 | // Type::kI420A and Type::kI444. |
| 103 | class PlanarYuv8Buffer : public PlanarYuvBuffer { |
| 104 | public: |
| 105 | // Returns pointer to the pixel data for a given plane. The memory is owned by |
| 106 | // the VideoFrameBuffer object and must not be freed by the caller. |
| 107 | virtual const uint8_t* DataY() const = 0; |
| 108 | virtual const uint8_t* DataU() const = 0; |
| 109 | virtual const uint8_t* DataV() const = 0; |
| 110 | |
| 111 | protected: |
| 112 | ~PlanarYuv8Buffer() override {} |
| 113 | }; |
| 114 | |
| 115 | class I420BufferInterface : public PlanarYuv8Buffer { |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 116 | public: |
Emircan Uysaler | 574eaa4 | 2017-11-09 20:33:24 | [diff] [blame] | 117 | Type type() const override; |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 118 | |
| 119 | int ChromaWidth() const final; |
| 120 | int ChromaHeight() const final; |
| 121 | |
| 122 | rtc::scoped_refptr<I420BufferInterface> ToI420() final; |
| 123 | |
| 124 | protected: |
| 125 | ~I420BufferInterface() override {} |
| 126 | }; |
| 127 | |
Emircan Uysaler | 574eaa4 | 2017-11-09 20:33:24 | [diff] [blame] | 128 | class I420ABufferInterface : public I420BufferInterface { |
| 129 | public: |
| 130 | Type type() const final; |
| 131 | virtual const uint8_t* DataA() const = 0; |
| 132 | virtual int StrideA() const = 0; |
| 133 | |
| 134 | protected: |
| 135 | ~I420ABufferInterface() override {} |
| 136 | }; |
| 137 | |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 138 | class I444BufferInterface : public PlanarYuv8Buffer { |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 139 | public: |
| 140 | Type type() const final; |
| 141 | |
| 142 | int ChromaWidth() const final; |
| 143 | int ChromaHeight() const final; |
| 144 | |
magjed | eaf4a1e | 2017-05-30 08:21:59 | [diff] [blame] | 145 | protected: |
| 146 | ~I444BufferInterface() override {} |
| 147 | }; |
| 148 | |
Emircan Uysaler | 901e0ff | 2018-06-26 19:22:38 | [diff] [blame] | 149 | // This interface represents 8-bit to 16-bit color depth formats: Type::kI010. |
| 150 | class PlanarYuv16BBuffer : public PlanarYuvBuffer { |
| 151 | public: |
| 152 | // Returns pointer to the pixel data for a given plane. The memory is owned by |
| 153 | // the VideoFrameBuffer object and must not be freed by the caller. |
| 154 | virtual const uint16_t* DataY() const = 0; |
| 155 | virtual const uint16_t* DataU() const = 0; |
| 156 | virtual const uint16_t* DataV() const = 0; |
| 157 | |
| 158 | protected: |
| 159 | ~PlanarYuv16BBuffer() override {} |
| 160 | }; |
| 161 | |
| 162 | // Represents Type::kI010, allocates 16 bits per pixel and fills 10 least |
| 163 | // significant bits with color information. |
| 164 | class I010BufferInterface : public PlanarYuv16BBuffer { |
| 165 | public: |
| 166 | Type type() const override; |
| 167 | |
| 168 | int ChromaWidth() const final; |
| 169 | int ChromaHeight() const final; |
| 170 | |
| 171 | protected: |
| 172 | ~I010BufferInterface() override {} |
| 173 | }; |
| 174 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 175 | } // namespace webrtc |
| 176 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 177 | #endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_ |