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 | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 16 | #include "rtc_base/refcount.h" |
| 17 | #include "rtc_base/scoped_ref_ptr.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; |
| 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. |
Magnus Jedvert | 224e659 | 2017-06-30 12:14:42 +0000 | [diff] [blame] | 51 | virtual Type type() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 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. |
Magnus Jedvert | 224e659 | 2017-06-30 12:14:42 +0000 | [diff] [blame] | 62 | virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0; |
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 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 73 | protected: |
| 74 | ~VideoFrameBuffer() override {} |
| 75 | }; |
| 76 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 77 | // This interface represents Type::kI420 and Type::kI444. |
| 78 | class PlanarYuvBuffer : public VideoFrameBuffer { |
| 79 | public: |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 80 | virtual int ChromaWidth() const = 0; |
| 81 | virtual int ChromaHeight() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 82 | |
| 83 | // Returns pointer to the pixel data for a given plane. The memory is owned by |
| 84 | // the VideoFrameBuffer object and must not be freed by the caller. |
Magnus Jedvert | 224e659 | 2017-06-30 12:14:42 +0000 | [diff] [blame] | 85 | virtual const uint8_t* DataY() const = 0; |
| 86 | virtual const uint8_t* DataU() const = 0; |
| 87 | virtual const uint8_t* DataV() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 88 | |
| 89 | // Returns the number of bytes between successive rows for a given plane. |
Magnus Jedvert | 224e659 | 2017-06-30 12:14:42 +0000 | [diff] [blame] | 90 | virtual int StrideY() const = 0; |
| 91 | virtual int StrideU() const = 0; |
| 92 | virtual int StrideV() const = 0; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 93 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 94 | protected: |
| 95 | ~PlanarYuvBuffer() override {} |
| 96 | }; |
| 97 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 98 | class I420BufferInterface : public PlanarYuvBuffer { |
| 99 | public: |
| 100 | Type type() const final; |
| 101 | |
| 102 | int ChromaWidth() const final; |
| 103 | int ChromaHeight() const final; |
| 104 | |
| 105 | rtc::scoped_refptr<I420BufferInterface> ToI420() final; |
| 106 | |
| 107 | protected: |
| 108 | ~I420BufferInterface() override {} |
| 109 | }; |
| 110 | |
| 111 | class I444BufferInterface : public PlanarYuvBuffer { |
| 112 | public: |
| 113 | Type type() const final; |
| 114 | |
| 115 | int ChromaWidth() const final; |
| 116 | int ChromaHeight() const final; |
| 117 | |
| 118 | rtc::scoped_refptr<I420BufferInterface> ToI420() final; |
| 119 | |
| 120 | protected: |
| 121 | ~I444BufferInterface() override {} |
| 122 | }; |
| 123 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 124 | } // namespace webrtc |
| 125 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 126 | #endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_ |