blob: c8d02a153f5f9ae7cafc3959a0b39b85b2cbd5d1 [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;
22class I444BufferInterface;
magjed712338e2017-05-11 05:11:57 -070023
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.
nisseaf916892017-01-10 07:44:26 -080039class VideoFrameBuffer : public rtc::RefCountInterface {
40 public:
magjed712338e2017-05-11 05:11:57 -070041 // 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 Jedvert224e6592017-06-30 12:14:42 +000051 virtual Type type() const = 0;
magjed712338e2017-05-11 05:11:57 -070052
nisseaf916892017-01-10 07:44:26 -080053 // 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
magjed712338e2017-05-11 05:11:57 -070058 // 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 Jedvert224e6592017-06-30 12:14:42 +000062 virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0;
magjed712338e2017-05-11 05:11:57 -070063
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.
magjed3f075492017-06-01 10:02:26 -070066 // TODO(magjed): Return raw pointers for GetI420 once deprecated interface is
67 // removed.
magjedeaf4a1e2017-05-30 01:21:59 -070068 rtc::scoped_refptr<I420BufferInterface> GetI420();
magjed3f075492017-06-01 10:02:26 -070069 rtc::scoped_refptr<const I420BufferInterface> GetI420() const;
70 I444BufferInterface* GetI444();
71 const I444BufferInterface* GetI444() const;
magjed712338e2017-05-11 05:11:57 -070072
nisseaf916892017-01-10 07:44:26 -080073 protected:
74 ~VideoFrameBuffer() override {}
75};
76
magjed712338e2017-05-11 05:11:57 -070077// This interface represents Type::kI420 and Type::kI444.
78class PlanarYuvBuffer : public VideoFrameBuffer {
79 public:
magjedeaf4a1e2017-05-30 01:21:59 -070080 virtual int ChromaWidth() const = 0;
81 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 05:11:57 -070082
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 Jedvert224e6592017-06-30 12:14:42 +000085 virtual const uint8_t* DataY() const = 0;
86 virtual const uint8_t* DataU() const = 0;
87 virtual const uint8_t* DataV() const = 0;
magjed712338e2017-05-11 05:11:57 -070088
89 // Returns the number of bytes between successive rows for a given plane.
Magnus Jedvert224e6592017-06-30 12:14:42 +000090 virtual int StrideY() const = 0;
91 virtual int StrideU() const = 0;
92 virtual int StrideV() const = 0;
magjed712338e2017-05-11 05:11:57 -070093
magjed712338e2017-05-11 05:11:57 -070094 protected:
95 ~PlanarYuvBuffer() override {}
96};
97
magjedeaf4a1e2017-05-30 01:21:59 -070098class 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
111class 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
nisseaf916892017-01-10 07:44:26 -0800124} // namespace webrtc
125
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200126#endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_