blob: 1e8169ac57ca387da467d36a24a5e8af44d440ea [file] [log] [blame]
nisseaf916892017-01-10 15:44:261/*
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 Bonadei92ea95e2017-09-15 04:47:3111#ifndef API_VIDEO_VIDEO_FRAME_BUFFER_H_
12#define API_VIDEO_VIDEO_FRAME_BUFFER_H_
nisseaf916892017-01-10 15:44:2613
14#include <stdint.h>
15
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/refcount.h"
17#include "rtc_base/scoped_ref_ptr.h"
nisseaf916892017-01-10 15:44:2618
19namespace webrtc {
20
magjedeaf4a1e2017-05-30 08:21:5921class I420BufferInterface;
Emircan Uysaler574eaa42017-11-09 20:33:2422class I420ABufferInterface;
magjedeaf4a1e2017-05-30 08:21:5923class I444BufferInterface;
Emircan Uysaler901e0ff2018-06-26 19:22:3824class I010BufferInterface;
magjed712338e2017-05-11 12:11:5725
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.
nisseaf916892017-01-10 15:44:2641class VideoFrameBuffer : public rtc::RefCountInterface {
42 public:
magjed712338e2017-05-11 12:11:5743 // 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 Uysaler574eaa42017-11-09 20:33:2449 kI420A,
magjed712338e2017-05-11 12:11:5750 kI444,
Emircan Uysaler901e0ff2018-06-26 19:22:3851 kI010,
magjed712338e2017-05-11 12:11:5752 };
53
54 // This function specifies in what pixel format the data is stored in.
Magnus Jedvert224e6592017-06-30 12:14:4255 virtual Type type() const = 0;
magjed712338e2017-05-11 12:11:5756
nisseaf916892017-01-10 15:44:2657 // 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
magjed712338e2017-05-11 12:11:5762 // 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 Jedvert224e6592017-06-30 12:14:4266 virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0;
magjed712338e2017-05-11 12:11:5767
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.
magjed3f075492017-06-01 17:02:2670 // TODO(magjed): Return raw pointers for GetI420 once deprecated interface is
71 // removed.
magjedeaf4a1e2017-05-30 08:21:5972 rtc::scoped_refptr<I420BufferInterface> GetI420();
magjed3f075492017-06-01 17:02:2673 rtc::scoped_refptr<const I420BufferInterface> GetI420() const;
Emircan Uysaler574eaa42017-11-09 20:33:2474 I420ABufferInterface* GetI420A();
75 const I420ABufferInterface* GetI420A() const;
magjed3f075492017-06-01 17:02:2676 I444BufferInterface* GetI444();
77 const I444BufferInterface* GetI444() const;
Emircan Uysaler901e0ff2018-06-26 19:22:3878 I010BufferInterface* GetI010();
79 const I010BufferInterface* GetI010() const;
magjed712338e2017-05-11 12:11:5780
nisseaf916892017-01-10 15:44:2681 protected:
82 ~VideoFrameBuffer() override {}
83};
84
Emircan Uysaler901e0ff2018-06-26 19:22:3885// This interface represents planar formats.
magjed712338e2017-05-11 12:11:5786class PlanarYuvBuffer : public VideoFrameBuffer {
87 public:
magjedeaf4a1e2017-05-30 08:21:5988 virtual int ChromaWidth() const = 0;
89 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 12:11:5790
Emircan Uysaler901e0ff2018-06-26 19:22:3891 // Returns the number of steps(in terms of Data*() return type) between
92 // successive rows for a given plane.
Magnus Jedvert224e6592017-06-30 12:14:4293 virtual int StrideY() const = 0;
94 virtual int StrideU() const = 0;
95 virtual int StrideV() const = 0;
magjed712338e2017-05-11 12:11:5796
magjed712338e2017-05-11 12:11:5797 protected:
98 ~PlanarYuvBuffer() override {}
99};
100
Emircan Uysaler901e0ff2018-06-26 19:22:38101// This interface represents 8-bit color depth formats: Type::kI420,
102// Type::kI420A and Type::kI444.
103class 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
115class I420BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 08:21:59116 public:
Emircan Uysaler574eaa42017-11-09 20:33:24117 Type type() const override;
magjedeaf4a1e2017-05-30 08:21:59118
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 Uysaler574eaa42017-11-09 20:33:24128class 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 Uysaler901e0ff2018-06-26 19:22:38138class I444BufferInterface : public PlanarYuv8Buffer {
magjedeaf4a1e2017-05-30 08:21:59139 public:
140 Type type() const final;
141
142 int ChromaWidth() const final;
143 int ChromaHeight() const final;
144
magjedeaf4a1e2017-05-30 08:21:59145 protected:
146 ~I444BufferInterface() override {}
147};
148
Emircan Uysaler901e0ff2018-06-26 19:22:38149// This interface represents 8-bit to 16-bit color depth formats: Type::kI010.
150class 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.
164class 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
nisseaf916892017-01-10 15:44:26175} // namespace webrtc
176
Mirko Bonadei92ea95e2017-09-15 04:47:31177#endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_