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 | |
| 11 | #ifndef WEBRTC_API_VIDEO_VIDEO_FRAME_BUFFER_H_ |
| 12 | #define WEBRTC_API_VIDEO_VIDEO_FRAME_BUFFER_H_ |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include "webrtc/base/refcount.h" |
| 17 | #include "webrtc/base/scoped_ref_ptr.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 21 | class I420BufferInterface; |
| 22 | class I444BufferInterface; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 23 | |
| 24 | // Base class for frame buffers of different types of pixel format and storage. |
| 25 | // The tag in type() indicates how the data is represented, and each type is |
| 26 | // implemented as a subclass. To access the pixel data, call the appropriate |
| 27 | // GetXXX() function, where XXX represents the type. There is also a function |
| 28 | // ToI420() that returns a frame buffer in I420 format, converting from the |
| 29 | // underlying representation if necessary. I420 is the most widely accepted |
| 30 | // format and serves as a fallback for video sinks that can only handle I420, |
| 31 | // e.g. the internal WebRTC software encoders. A special enum value 'kNative' is |
| 32 | // provided for external clients to implement their own frame buffer |
| 33 | // representations, e.g. as textures. The external client can produce such |
| 34 | // native frame buffers from custom video sources, and then cast it back to the |
| 35 | // correct subclass in custom video sinks. The purpose of this is to improve |
| 36 | // performance by providing an optimized path without intermediate conversions. |
| 37 | // Frame metadata such as rotation and timestamp are stored in |
| 38 | // webrtc::VideoFrame, and not here. |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 39 | class VideoFrameBuffer : public rtc::RefCountInterface { |
| 40 | public: |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 41 | // New frame buffer types will be added conservatively when there is an |
| 42 | // opportunity to optimize the path between some pair of video source and |
| 43 | // video sink. |
| 44 | enum class Type { |
| 45 | kNative, |
| 46 | kI420, |
| 47 | kI444, |
| 48 | }; |
| 49 | |
| 50 | // This function specifies in what pixel format the data is stored in. |
| 51 | virtual Type type() const; |
| 52 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 53 | // The resolution of the frame in pixels. For formats where some planes are |
| 54 | // subsampled, this is the highest-resolution plane. |
| 55 | virtual int width() const = 0; |
| 56 | virtual int height() const = 0; |
| 57 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 58 | // Returns a memory-backed frame buffer in I420 format. If the pixel data is |
| 59 | // in another format, a conversion will take place. All implementations must |
| 60 | // provide a fallback to I420 for compatibility with e.g. the internal WebRTC |
| 61 | // software encoders. |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 62 | virtual rtc::scoped_refptr<I420BufferInterface> ToI420(); |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 63 | |
| 64 | // These functions should only be called if type() is of the correct type. |
| 65 | // Calling with a different type will result in a crash. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame^] | 66 | // TODO(magjed): Return raw pointers for GetI420 once deprecated interface is |
| 67 | // removed. |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 68 | rtc::scoped_refptr<I420BufferInterface> GetI420(); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame^] | 69 | rtc::scoped_refptr<const I420BufferInterface> GetI420() const; |
| 70 | I444BufferInterface* GetI444(); |
| 71 | const I444BufferInterface* GetI444() const; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 72 | |
| 73 | // Deprecated - use ToI420() first instead. |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 74 | // Returns pointer to the pixel data for a given plane. The memory is owned by |
| 75 | // the VideoFrameBuffer object and must not be freed by the caller. |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 76 | virtual const uint8_t* DataY() const; |
| 77 | virtual const uint8_t* DataU() const; |
| 78 | virtual const uint8_t* DataV() const; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 79 | // Returns the number of bytes between successive rows for a given plane. |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 80 | virtual int StrideY() const; |
| 81 | virtual int StrideU() const; |
| 82 | virtual int StrideV() const; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 83 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 84 | // Deprecated - use type() to determine if the stored data is kNative, and |
| 85 | // then cast into the appropriate type. |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 86 | // Return the handle of the underlying video frame. This is used when the |
| 87 | // frame is backed by a texture. |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 88 | virtual void* native_handle() const; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 89 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 90 | // Deprecated - use ToI420() instead. |
| 91 | virtual rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer(); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 92 | |
| 93 | protected: |
| 94 | ~VideoFrameBuffer() override {} |
| 95 | }; |
| 96 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 97 | // This interface represents Type::kI420 and Type::kI444. |
| 98 | class PlanarYuvBuffer : public VideoFrameBuffer { |
| 99 | public: |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 100 | virtual int ChromaWidth() const = 0; |
| 101 | virtual int ChromaHeight() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 102 | |
| 103 | // Returns pointer to the pixel data for a given plane. The memory is owned by |
| 104 | // the VideoFrameBuffer object and must not be freed by the caller. |
| 105 | const uint8_t* DataY() const override = 0; |
| 106 | const uint8_t* DataU() const override = 0; |
| 107 | const uint8_t* DataV() const override = 0; |
| 108 | |
| 109 | // Returns the number of bytes between successive rows for a given plane. |
| 110 | int StrideY() const override = 0; |
| 111 | int StrideU() const override = 0; |
| 112 | int StrideV() const override = 0; |
| 113 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 114 | protected: |
| 115 | ~PlanarYuvBuffer() override {} |
| 116 | }; |
| 117 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 118 | class I420BufferInterface : public PlanarYuvBuffer { |
| 119 | public: |
| 120 | Type type() const final; |
| 121 | |
| 122 | int ChromaWidth() const final; |
| 123 | int ChromaHeight() const final; |
| 124 | |
| 125 | rtc::scoped_refptr<I420BufferInterface> ToI420() final; |
| 126 | |
| 127 | protected: |
| 128 | ~I420BufferInterface() override {} |
| 129 | }; |
| 130 | |
| 131 | class I444BufferInterface : public PlanarYuvBuffer { |
| 132 | public: |
| 133 | Type type() const final; |
| 134 | |
| 135 | int ChromaWidth() const final; |
| 136 | int ChromaHeight() const final; |
| 137 | |
| 138 | rtc::scoped_refptr<I420BufferInterface> ToI420() final; |
| 139 | |
| 140 | protected: |
| 141 | ~I444BufferInterface() override {} |
| 142 | }; |
| 143 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 144 | } // namespace webrtc |
| 145 | |
| 146 | #endif // WEBRTC_API_VIDEO_VIDEO_FRAME_BUFFER_H_ |