blob: 6d0c99107cdced3adbedc87d5da8958dc909af95 [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
magjed712338e2017-05-11 05:11:57 -070021class PlanarYuvBuffer;
22
23// Base class for frame buffers of different types of pixel format and storage.
24// The tag in type() indicates how the data is represented, and each type is
25// implemented as a subclass. To access the pixel data, call the appropriate
26// GetXXX() function, where XXX represents the type. There is also a function
27// ToI420() that returns a frame buffer in I420 format, converting from the
28// underlying representation if necessary. I420 is the most widely accepted
29// format and serves as a fallback for video sinks that can only handle I420,
30// e.g. the internal WebRTC software encoders. A special enum value 'kNative' is
31// provided for external clients to implement their own frame buffer
32// representations, e.g. as textures. The external client can produce such
33// native frame buffers from custom video sources, and then cast it back to the
34// correct subclass in custom video sinks. The purpose of this is to improve
35// performance by providing an optimized path without intermediate conversions.
36// Frame metadata such as rotation and timestamp are stored in
37// webrtc::VideoFrame, and not here.
nisseaf916892017-01-10 07:44:26 -080038class VideoFrameBuffer : public rtc::RefCountInterface {
39 public:
magjed712338e2017-05-11 05:11:57 -070040 // New frame buffer types will be added conservatively when there is an
41 // opportunity to optimize the path between some pair of video source and
42 // video sink.
43 enum class Type {
44 kNative,
45 kI420,
46 kI444,
47 };
48
49 // This function specifies in what pixel format the data is stored in.
50 virtual Type type() const;
51
nisseaf916892017-01-10 07:44:26 -080052 // The resolution of the frame in pixels. For formats where some planes are
53 // subsampled, this is the highest-resolution plane.
54 virtual int width() const = 0;
55 virtual int height() const = 0;
56
magjed712338e2017-05-11 05:11:57 -070057 // Returns a memory-backed frame buffer in I420 format. If the pixel data is
58 // in another format, a conversion will take place. All implementations must
59 // provide a fallback to I420 for compatibility with e.g. the internal WebRTC
60 // software encoders.
61 virtual rtc::scoped_refptr<PlanarYuvBuffer> ToI420();
62
63 // These functions should only be called if type() is of the correct type.
64 // Calling with a different type will result in a crash.
65 rtc::scoped_refptr<PlanarYuvBuffer> GetI420();
66 rtc::scoped_refptr<PlanarYuvBuffer> GetI444();
67
68 // Deprecated - use ToI420() first instead.
nisseaf916892017-01-10 07:44:26 -080069 // Returns pointer to the pixel data for a given plane. The memory is owned by
70 // the VideoFrameBuffer object and must not be freed by the caller.
magjed712338e2017-05-11 05:11:57 -070071 virtual const uint8_t* DataY() const;
72 virtual const uint8_t* DataU() const;
73 virtual const uint8_t* DataV() const;
nisseaf916892017-01-10 07:44:26 -080074 // Returns the number of bytes between successive rows for a given plane.
magjed712338e2017-05-11 05:11:57 -070075 virtual int StrideY() const;
76 virtual int StrideU() const;
77 virtual int StrideV() const;
nisseaf916892017-01-10 07:44:26 -080078
magjed712338e2017-05-11 05:11:57 -070079 // Deprecated - use type() to determine if the stored data is kNative, and
80 // then cast into the appropriate type.
nisseaf916892017-01-10 07:44:26 -080081 // Return the handle of the underlying video frame. This is used when the
82 // frame is backed by a texture.
magjed712338e2017-05-11 05:11:57 -070083 virtual void* native_handle() const;
nisseaf916892017-01-10 07:44:26 -080084
magjed712338e2017-05-11 05:11:57 -070085 // Deprecated - use ToI420() instead.
86 virtual rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer();
nisseaf916892017-01-10 07:44:26 -080087
88 protected:
89 ~VideoFrameBuffer() override {}
90};
91
magjed712338e2017-05-11 05:11:57 -070092// This interface represents Type::kI420 and Type::kI444.
93class PlanarYuvBuffer : public VideoFrameBuffer {
94 public:
95 int ChromaWidth() const;
96 int ChromaHeight() const;
97
98 // Returns pointer to the pixel data for a given plane. The memory is owned by
99 // the VideoFrameBuffer object and must not be freed by the caller.
100 const uint8_t* DataY() const override = 0;
101 const uint8_t* DataU() const override = 0;
102 const uint8_t* DataV() const override = 0;
103
104 // Returns the number of bytes between successive rows for a given plane.
105 int StrideY() const override = 0;
106 int StrideU() const override = 0;
107 int StrideV() const override = 0;
108
109 rtc::scoped_refptr<PlanarYuvBuffer> ToI420() override;
110
111 protected:
112 ~PlanarYuvBuffer() override {}
113};
114
nisseaf916892017-01-10 07:44:26 -0800115} // namespace webrtc
116
117#endif // WEBRTC_API_VIDEO_VIDEO_FRAME_BUFFER_H_