PonyPlayer
frame.hpp
浏览该文件的文档.
1//
2// Created by ColorsWind on 2022/5/7.
3//
4#pragma once
5
6#include "helper.hpp"
7#include "ponyplayer.h"
8#include <mutex>
9#include <functional>
10#include <queue>
11
12INCLUDE_FFMPEG_BEGIN
13#include <libavformat/avformat.h>
15
16typedef std::function<void(AVFrame *)> FrameFreeFunc;
17
19 AVFrame *m_frame;
20 std::atomic<int> m_refCount;
21 const double m_pts;
22 const bool m_isValid;
23
24 friend class VideoFrameRef;
25public:
26 static inline std::atomic<int> totalCount = 0;
27
28 VideoFrame(AVFrame *frame, double pts, const bool isValid)
29 : m_frame(frame), m_refCount(1), m_pts(pts), m_isValid(isValid) {
30 if (frame) ++totalCount;
31 }
32
34 if (m_frame) {
35 --totalCount;
36 av_frame_free(&m_frame);
37 }
38 }
39
40 void unref() {
41 if (--m_refCount == 0) {
42 delete this;
43 }
44 }
45};
46
48private:
49 VideoFrame *m_videoFrame;
50public:
51 VideoFrameRef(AVFrame *frame, bool isValid, double pts) {
52 m_videoFrame = new VideoFrame(frame, pts, isValid);
53 }
54
55 VideoFrameRef() : VideoFrameRef(nullptr, false, std::numeric_limits<double>::quiet_NaN()) {}
56
57 VideoFrameRef(VideoFrameRef &&rhs) noexcept: m_videoFrame(rhs.m_videoFrame) {
58 rhs.m_videoFrame = nullptr;
59 }
60
61 VideoFrameRef(const VideoFrameRef &rhs) : m_videoFrame(rhs.m_videoFrame) {
62 ++rhs.m_videoFrame->m_refCount;
63 }
64
66 if (this->m_videoFrame) { this->m_videoFrame->unref(); }
67 this->m_videoFrame = rhs.m_videoFrame;
68 rhs.m_videoFrame = nullptr;
69 return *this;
70 }
71
72 VideoFrameRef &operator=(const VideoFrameRef &rhs) noexcept {
73 if (&rhs != this) {
74 if (this->m_videoFrame) { this->m_videoFrame->unref(); }
75 this->m_videoFrame = rhs.m_videoFrame;
76 ++m_videoFrame->m_refCount;
77 }
78 return *this;
79 }
80
81
83 if (m_videoFrame) { m_videoFrame->unref(); }
84 }
85
86 bool operator==(const VideoFrameRef &frame) const {
87 return this->m_videoFrame == frame.m_videoFrame;
88 }
89
90 bool operator!=(const VideoFrameRef &frame) const {
91 return !this->operator==(frame);
92 }
93
97 [[nodiscard]] bool isValid() const {
98 return m_videoFrame && m_videoFrame->m_isValid;
99 }
100
101
102 [[nodiscard]] double getPTS() const {
103 return m_videoFrame->m_pts;
104 }
105
106 [[nodiscard]] std::byte *getY() const {
107 return !m_videoFrame->m_frame ? nullptr : reinterpret_cast<std::byte *>(m_videoFrame->m_frame->data[0]);
108 }
109
110 [[nodiscard]] std::byte *getU() const {
111 return !m_videoFrame->m_frame ? nullptr : reinterpret_cast<std::byte *>(m_videoFrame->m_frame->data[1]);
112 }
113
114 [[nodiscard]] std::byte *getV() const {
115 return !m_videoFrame->m_frame ? nullptr : reinterpret_cast<std::byte *>(m_videoFrame->m_frame->data[2]);
116 }
117
118 [[nodiscard]] int getLineSize() const {
119 return !m_videoFrame->m_frame ? 0 : m_videoFrame->m_frame->linesize[0];
120 }
121
122 [[nodiscard]] int getWidth() const {
123 return !m_videoFrame->m_frame ? 0 : m_videoFrame->m_frame->width;
124 }
125
126 [[nodiscard]] int getHeight() const {
127 return !m_videoFrame->m_frame ? 0 : m_videoFrame->m_frame->height;
128 }
129
130 [[nodiscard]] bool isSameSize(const VideoFrameRef &frame) const {
131 return (this->isValid()
132 && frame.isValid()
133 && this->getWidth() == frame.getWidth()
134 && this->getHeight() == frame.getHeight()
135 && this->getLineSize() == frame.getLineSize())
136 || (!this->isValid() && !frame.isValid());
137 }
138
139 [[nodiscard]] bool isSameSize(int width, int height) const {
140 return this->isValid() && this->getWidth() == width && this->getHeight() == height;
141 }
142
143};
144
146private:
147 std::byte *m_data;
148 int m_len;
149 double m_pts;
150public:
151 AudioFrame() : m_data(nullptr), m_len(0), m_pts(std::numeric_limits<double>::quiet_NaN()) {}
152
153 AudioFrame(std::byte *data, int len, double pts) : m_data(data), m_len(len), m_pts(pts) {}
154
155 bool isValid() {
156 return m_data;
157 }
158
159 [[nodiscard]] std::byte *getSampleData() const {
160 return m_data;
161 }
162
163 [[nodiscard]] int getDataLen() const {
164 return m_len;
165 }
166
167 [[nodiscard]] double getPTS() const {
168 return m_pts;
169 }
170};
171
Definition: frame.hpp:145
int getDataLen() const
Definition: frame.hpp:163
AudioFrame()
Definition: frame.hpp:151
AudioFrame(std::byte *data, int len, double pts)
Definition: frame.hpp:153
std::byte * getSampleData() const
Definition: frame.hpp:159
bool isValid()
Definition: frame.hpp:155
double getPTS() const
Definition: frame.hpp:167
Definition: frame.hpp:18
void unref()
Definition: frame.hpp:40
VideoFrame(AVFrame *frame, double pts, const bool isValid)
Definition: frame.hpp:28
static std::atomic< int > totalCount
Definition: frame.hpp:26
~VideoFrame()
Definition: frame.hpp:33
Definition: frame.hpp:47
std::byte * getV() const
Definition: frame.hpp:114
VideoFrameRef & operator=(const VideoFrameRef &rhs) noexcept
Definition: frame.hpp:72
VideoFrameRef & operator=(VideoFrameRef &&rhs) noexcept
Definition: frame.hpp:65
bool operator==(const VideoFrameRef &frame) const
Definition: frame.hpp:86
VideoFrameRef()
Definition: frame.hpp:55
bool isValid() const
Definition: frame.hpp:97
~VideoFrameRef()
Definition: frame.hpp:82
bool isSameSize(int width, int height) const
Definition: frame.hpp:139
VideoFrameRef(VideoFrameRef &&rhs) noexcept
Definition: frame.hpp:57
bool isSameSize(const VideoFrameRef &frame) const
Definition: frame.hpp:130
double getPTS() const
Definition: frame.hpp:102
int getWidth() const
Definition: frame.hpp:122
int getLineSize() const
Definition: frame.hpp:118
VideoFrameRef(AVFrame *frame, bool isValid, double pts)
Definition: frame.hpp:51
std::byte * getU() const
Definition: frame.hpp:110
bool operator!=(const VideoFrameRef &frame) const
Definition: frame.hpp:90
std::byte * getY() const
Definition: frame.hpp:106
VideoFrameRef(const VideoFrameRef &rhs)
Definition: frame.hpp:61
int getHeight() const
Definition: frame.hpp:126
INCLUDE_FFMPEG_BEGIN INCLUDE_FFMPEG_END typedef std::function< void(AVFrame *)> FrameFreeFunc
Definition: frame.hpp:16
#define INCLUDE_FFMPEG_END
Definition: ponyplayer.h:26