blob: 13005afc8a57b5370e3fce5bd88717e51e970f66 [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.
magjedeaf4a1e2017-05-30 01:21:59 -070066 rtc::scoped_refptr<I420BufferInterface> GetI420();
67 rtc::scoped_refptr<I444BufferInterface> GetI444();
magjed712338e2017-05-11 05:11:57 -070068
69 // Deprecated - use ToI420() first instead.
nisseaf916892017-01-10 07:44:26 -080070 // Returns pointer to the pixel data for a given plane. The memory is owned by
71 // the VideoFrameBuffer object and must not be freed by the caller.
magjed712338e2017-05-11 05:11:57 -070072 virtual const uint8_t* DataY() const;
73 virtual const uint8_t* DataU() const;
74 virtual const uint8_t* DataV() const;
nisseaf916892017-01-10 07:44:26 -080075 // Returns the number of bytes between successive rows for a given plane.
magjed712338e2017-05-11 05:11:57 -070076 virtual int StrideY() const;
77 virtual int StrideU() const;
78 virtual int StrideV() const;
nisseaf916892017-01-10 07:44:26 -080079
magjed712338e2017-05-11 05:11:57 -070080 // Deprecated - use type() to determine if the stored data is kNative, and
81 // then cast into the appropriate type.
nisseaf916892017-01-10 07:44:26 -080082 // Return the handle of the underlying video frame. This is used when the
83 // frame is backed by a texture.
magjed712338e2017-05-11 05:11:57 -070084 virtual void* native_handle() const;
nisseaf916892017-01-10 07:44:26 -080085
magjed712338e2017-05-11 05:11:57 -070086 // Deprecated - use ToI420() instead.
87 virtual rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer();
nisseaf916892017-01-10 07:44:26 -080088
89 protected:
90 ~VideoFrameBuffer() override {}
91};
92
magjed712338e2017-05-11 05:11:57 -070093// This interface represents Type::kI420 and Type::kI444.
94class PlanarYuvBuffer : public VideoFrameBuffer {
95 public:
magjedeaf4a1e2017-05-30 01:21:59 -070096 virtual int ChromaWidth() const = 0;
97 virtual int ChromaHeight() const = 0;
magjed712338e2017-05-11 05:11:57 -070098
99 // Returns pointer to the pixel data for a given plane. The memory is owned by
100 // the VideoFrameBuffer object and must not be freed by the caller.
101 const uint8_t* DataY() const override = 0;
102 const uint8_t* DataU() const override = 0;
103 const uint8_t* DataV() const override = 0;
104
105 // Returns the number of bytes between successive rows for a given plane.
106 int StrideY() const override = 0;
107 int StrideU() const override = 0;
108 int StrideV() const override = 0;
109
magjed712338e2017-05-11 05:11:57 -0700110 protected:
111 ~PlanarYuvBuffer() override {}
112};
113
magjedeaf4a1e2017-05-30 01:21:59 -0700114class I420BufferInterface : public PlanarYuvBuffer {
115 public:
116 Type type() const final;
117
118 int ChromaWidth() const final;
119 int ChromaHeight() const final;
120
121 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
122
123 protected:
124 ~I420BufferInterface() override {}
125};
126
127class I444BufferInterface : public PlanarYuvBuffer {
128 public:
129 Type type() const final;
130
131 int ChromaWidth() const final;
132 int ChromaHeight() const final;
133
134 rtc::scoped_refptr<I420BufferInterface> ToI420() final;
135
136 protected:
137 ~I444BufferInterface() override {}
138};
139
nisseaf916892017-01-10 07:44:26 -0800140} // namespace webrtc
141
142#endif // WEBRTC_API_VIDEO_VIDEO_FRAME_BUFFER_H_