-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFFHelper.hh
123 lines (102 loc) · 2.26 KB
/
FFHelper.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once
#include <mutex>
extern "C" {
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
}
namespace LiveRTSP {
class FFHelper {
public:
static void Init() {
static std::once_flag once;
std::call_once(once, []{
avdevice_register_all();
avformat_network_init();
});
}
};
// https://docs.microsoft.com/en-us/cpp/cpp/move-constructors-and-move-assignment-operators-cpp?view=msvc-170
struct FFFrame {
FFFrame() :
frame(av_frame_alloc()),
pts(AV_NOPTS_VALUE),
width(0),
height(0),
format(AV_PIX_FMT_NONE)
{
}
FFFrame(AVFrame *input_frame, double pts, int width, int height, int format) :
frame(av_frame_alloc()),
pts(pts),
width(width),
height(height),
format(format)
{
if (input_frame)
av_frame_move_ref(frame, input_frame);
}
~FFFrame()
{
av_frame_free(&frame);
}
FFFrame(const FFFrame&) = delete;
FFFrame& operator=(const FFFrame&) = delete;
FFFrame(FFFrame &&rhs)
{
*this = std::move(rhs);
}
FFFrame& operator=(FFFrame&& rhs) noexcept
{
if (this != &rhs) {
pts = rhs.pts;
width = rhs.width;
height = rhs.height;
format = rhs.format;
av_frame_unref(frame);
av_frame_move_ref(frame, rhs.frame);
}
return *this;
}
void unref() {
av_frame_unref(frame);
}
AVFrame *frame;
double pts;
int width;
int height;
int format;
};
struct FFPacket {
FFPacket() :
pkt(av_packet_alloc())
{}
FFPacket(AVPacket *ipkt) :
pkt(av_packet_alloc())
{
if (pkt) av_packet_move_ref(pkt, ipkt);
}
~FFPacket()
{
av_packet_free(&pkt);
}
FFPacket(FFPacket &&rhs)
{
*this = std::move(rhs);
}
FFPacket& operator=(FFPacket&& rhs) noexcept
{
if (this != &rhs) {
av_packet_unref(pkt);
av_packet_move_ref(pkt, rhs.pkt);
}
return *this;
}
void unref()
{
av_packet_unref(pkt);
}
AVPacket *pkt;
};
}