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