blob: e656edd7e60b447b2dd7b3b11111f23110d0bba5 [file] [log] [blame]
nisseaf916892017-01-10 07:44:26 -08001/*
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 06:47:31 +020011#ifndef API_VIDEO_VIDEO_FRAME_BUFFER_H_
12#define API_VIDEO_VIDEO_FRAME_BUFFER_H_
nisseaf916892017-01-10 07:44:26 -080013
14#include <stdint.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/refcount.h"
17#include "rtc_base/scoped_ref_ptr.h"
nisseaf916892017-01-10 07:44:26 -080018
19namespace webrtc {
20
magjedeaf4a1e2017-05-30 01:21:59 -070021class I420BufferInterface;
Emircan Uysaler574eaa42017-11-09 12:33:24 -080022class I420ABufferInterface;
magjedeaf4a1e2017-05-30 01:21:59 -070023class I444BufferInterface;
magjed712338e2017-05-11 05:11:57 -070024
25// Base class for frame buffers of different types of pixel format and storage.
26// The tag in type() indicates how the data is represented, and each type is
27// implemented as a subclass. To access the pixel data, call the appropriate
28// GetXXX() function, where XXX represents the type. There is also a function
29// ToI420() that returns a frame buffer in I420 format, converting from the
30// underlying representation if necessary. I420 is the most widely accepted
31// format and serves as a fallback for video sinks that can only handle I420,
32// e.g. the internal WebRTC software encoders. A special enum value 'kNative' is
33// provided for external clients to implement their own frame buffer
34// representations, e.g. as textures. The external client can produce such
35// native frame buffers from custom video sources, and then cast it back to the
36// correct subclass in custom video sinks. The purpose of this is to improve
37// performance by providing an optimized path without intermediate conversions.
38// Frame metadata such as rotation and timestamp are stored in
39// webrtc::VideoFrame, and not here.
nisseaf916892017-01-10 07:44:26 -080040class VideoFrameBuffer : public rtc::RefCountInterface {
41 public:
magjed712338e2017-05-11 05:11:57 -070042 // New frame buffer types will be added conservatively when there is an
43 // opportunity to optimize the path between some pair of video source and
44 // video sink.
45 enum class Type {
46 kNative,
47 kI420,
Emircan Uysaler574eaa42017-11-09 12:33:24 -080048 kI420A,
magjed712338e2017-05-11 05:11:57 -070049 kI444,
50 };
51
52 // This function specifies in what pixel format the data is stored in.
Magnus Jedvert224e6592017-06-30 12:14:42 +000053 virtual Type type() const = 0;
magjed712338e2017-05-11 05:11:57 -070054
nisseaf916892017-01-10 07:44:26 -080055 // The resolution of the frame in pixels. For formats where some planes are
56 // subsampled, this is the highest-resolution plane.
57 virtual int width() const = 0;
58 virtual int height() const = 0;
59
magjed712338e2017-05-11 05:11:57 -070060 // Returns a memory-backed frame buffer in I420 format. If the pixel data is
61 // in another format, a conversion will take place. All implementations must
62 // provide a fallback to I420 for compatibility with e.g. the internal WebRTC
63 // software encoders.
Magnus Jedvert224e6592017-06-30 12:14:42 +000064 virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0;
magjed712338e2017-05-11 05:11:57 -070065
66 // These functions should only be called if type() is of the correct type.
67 // Calling with a different type will result in a crash.
magjed3f075492017-06-01 10:02:26 -070068 // TODO(magjed): Return raw pointers for GetI420 once deprecated interface is
69 // removed.
magjedeaf4a1e2017-05-30 01:21:59 -070070 rtc::scoped_refptr<I420BufferInterface> GetI420();
magjed3f075492017-06-01 10:02:26 -070071 rtc::scoped_refptr<const I420BufferInterface> GetI420() const;
Emircan Uysaler574eaa42017-11-09 12:33:24 -080072 I420ABufferInterface* GetI420A();
73 const I420ABufferInterface* GetI420A() const;
magjed3f075492017-06-01 10:02:26 -070074 I444BufferInterface* GetI444();
75 const I444BufferInterface* GetI444() const;
magjed712338e2017-05-11 05:11:57 -070076
nisseaf916892017-01-10 07:44:26 -080077 protected:
78 ~VideoFrameBuffer() override {}
79};
80
magjed712338e2017-05-11 05:11:57 -070081// This interface represents Type::kI420 and Type::kI444.
82class PlanarYuvBuffer : public VideoFrameBuffer {
83 public:
magjedeaf4a1e2017-05-30 01:21:59 -070084 virtual int ChromaWidth() const = 0;
85 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 05:11:57 -070086
87 // Returns pointer to the pixel data for a given plane. The memory is owned by
88 // the VideoFrameBuffer object and must not be freed by the caller.
Magnus Jedvert224e6592017-06-30 12:14:42 +000089 virtual const uint8_t* DataY() const = 0;
90 virtual const uint8_t* DataU() const = 0;
91 virtual const uint8_t* DataV() const = 0;
magjed712338e2017-05-11 05:11:57 -070092
93 // Returns the number of bytes between successive rows for a given plane.
Magnus Jedvert224e6592017-06-30 12:14:42 +000094 virtual int StrideY() const = 0;
95 virtual int StrideU() const = 0;
96 virtual int StrideV() const = 0;
magjed712338e2017-05-11 05:11:57 -070097
magjed712338e2017-05-11 05:11:57 -070098 protected:
99 ~PlanarYuvBuffer() override {}
100};
101
magjedeaf4a1e2017-05-30 01:21:59 -0700102class I420BufferInterface : public PlanarYuvBuffer {
103 public:
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800104 Type type() const override;
magjedeaf4a1e2017-05-30 01:21:59 -0700105
106 int ChromaWidth() const final;
107 int ChromaHeight() const final;
108
109 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
110
111 protected:
112 ~I420BufferInterface() override {}
113};
114
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800115class I420ABufferInterface : public I420BufferInterface {
116 public:
117 Type type() const final;
118 virtual const uint8_t* DataA() const = 0;
119 virtual int StrideA() const = 0;
120
121 protected:
122 ~I420ABufferInterface() override {}
123};
124
magjedeaf4a1e2017-05-30 01:21:59 -0700125class I444BufferInterface : public PlanarYuvBuffer {
126 public:
127 Type type() const final;
128
129 int ChromaWidth() const final;
130 int ChromaHeight() const final;
131
132 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
133
134 protected:
135 ~I444BufferInterface() override {}
136};
137
nisseaf916892017-01-10 07:44:26 -0800138} // namespace webrtc
139
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200140#endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_