blob: fa711d85e1b368873ccf69b8c9e3b094ec6948da [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
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
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.
51 virtual Type type() const;
52
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.
magjedeaf4a1e2017-05-30 01:21:59 -070062 virtual rtc::scoped_refptr<I420BufferInterface> ToI420();
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
73 // Deprecated - use ToI420() first instead.
nisseaf916892017-01-10 07:44:26 -080074 // Returns pointer to the pixel data for a given plane. The memory is owned by
75 // the VideoFrameBuffer object and must not be freed by the caller.
magjed712338e2017-05-11 05:11:57 -070076 virtual const uint8_t* DataY() const;
77 virtual const uint8_t* DataU() const;
78 virtual const uint8_t* DataV() const;
nisseaf916892017-01-10 07:44:26 -080079 // Returns the number of bytes between successive rows for a given plane.
magjed712338e2017-05-11 05:11:57 -070080 virtual int StrideY() const;
81 virtual int StrideU() const;
82 virtual int StrideV() const;
nisseaf916892017-01-10 07:44:26 -080083
magjed712338e2017-05-11 05:11:57 -070084 // Deprecated - use type() to determine if the stored data is kNative, and
85 // then cast into the appropriate type.
nisseaf916892017-01-10 07:44:26 -080086 // Return the handle of the underlying video frame. This is used when the
87 // frame is backed by a texture.
magjed712338e2017-05-11 05:11:57 -070088 virtual void* native_handle() const;
nisseaf916892017-01-10 07:44:26 -080089
magjed712338e2017-05-11 05:11:57 -070090 // Deprecated - use ToI420() instead.
91 virtual rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer();
nisseaf916892017-01-10 07:44:26 -080092
93 protected:
94 ~VideoFrameBuffer() override {}
95};
96
magjed712338e2017-05-11 05:11:57 -070097// This interface represents Type::kI420 and Type::kI444.
98class PlanarYuvBuffer : public VideoFrameBuffer {
99 public:
magjedeaf4a1e2017-05-30 01:21:59 -0700100 virtual int ChromaWidth() const = 0;
101 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 05:11:57 -0700102
103 // Returns pointer to the pixel data for a given plane. The memory is owned by
104 // the VideoFrameBuffer object and must not be freed by the caller.
105 const uint8_t* DataY() const override = 0;
106 const uint8_t* DataU() const override = 0;
107 const uint8_t* DataV() const override = 0;
108
109 // Returns the number of bytes between successive rows for a given plane.
110 int StrideY() const override = 0;
111 int StrideU() const override = 0;
112 int StrideV() const override = 0;
113
magjed712338e2017-05-11 05:11:57 -0700114 protected:
115 ~PlanarYuvBuffer() override {}
116};
117
magjedeaf4a1e2017-05-30 01:21:59 -0700118class I420BufferInterface : public PlanarYuvBuffer {
119 public:
120 Type type() const final;
121
122 int ChromaWidth() const final;
123 int ChromaHeight() const final;
124
125 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
126
127 protected:
128 ~I420BufferInterface() override {}
129};
130
131class I444BufferInterface : public PlanarYuvBuffer {
132 public:
133 Type type() const final;
134
135 int ChromaWidth() const final;
136 int ChromaHeight() const final;
137
138 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
139
140 protected:
141 ~I444BufferInterface() override {}
142};
143
nisseaf916892017-01-10 07:44:26 -0800144} // namespace webrtc
145
146#endif // WEBRTC_API_VIDEO_VIDEO_FRAME_BUFFER_H_