forked from h4tr3d/avcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavutils.cpp
264 lines (230 loc) · 5.46 KB
/
avutils.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#ifdef _WIN32
# include <windows.h>
#endif
#include <signal.h>
#include "av.h"
#include "avutils.h"
#include "packet.h"
#include "frame.h"
using namespace std;
//
// Functions
//
namespace av {
void set_logging_level(int32_t level)
{
if (level < AV_LOG_PANIC)
av_log_set_level(AV_LOG_QUIET);
else if (level < AV_LOG_FATAL)
av_log_set_level(AV_LOG_PANIC);
else if (level < AV_LOG_ERROR)
av_log_set_level(AV_LOG_FATAL);
else if (level < AV_LOG_WARNING)
av_log_set_level(AV_LOG_ERROR);
else if (level < AV_LOG_INFO)
av_log_set_level(AV_LOG_WARNING);
else if (level < AV_LOG_VERBOSE)
av_log_set_level(AV_LOG_INFO);
else if (level < AV_LOG_DEBUG)
av_log_set_level(AV_LOG_VERBOSE);
else
av_log_set_level(AV_LOG_DEBUG);
}
void set_logging_level(const string &level)
{
if (level == "quiet")
av_log_set_level(AV_LOG_QUIET);
else if (level == "panic")
av_log_set_level(AV_LOG_PANIC);
else if (level == "fatal")
av_log_set_level(AV_LOG_FATAL);
else if (level == "error")
av_log_set_level(AV_LOG_ERROR);
else if (level == "warning")
av_log_set_level(AV_LOG_WARNING);
else if (level == "info")
av_log_set_level(AV_LOG_INFO);
else if (level == "verbose")
av_log_set_level(AV_LOG_VERBOSE);
else if (level == "debug")
av_log_set_level(AV_LOG_DEBUG);
else
{
try
{
set_logging_level(lexical_cast<int32_t>(level));
}
catch (...)
{}
}
}
void dumpBinaryBuffer(uint8_t *buffer, int buffer_size, int width)
{
bool needNewLine = false;
for (int i = 0; i < buffer_size; ++i)
{
needNewLine = true;
printf("%.2X", buffer[i]);
if ((i + 1) % width == 0 && i != 0)
{
printf("\n");
needNewLine = false;
}
else
{
printf(" ");
}
}
if (needNewLine)
{
printf("\n");
}
}
#if LIBAVCODEC_VERSION_MAJOR < 58 // FFmpeg 4.0
#if !defined(__MINGW32__) || defined(_GLIBCXX_HAS_GTHREADS)
static int avcpp_lockmgr_cb(void **ctx, enum AVLockOp op)
{
if (!ctx)
return 1;
std::mutex *mutex = static_cast<std::mutex*>(*ctx);
int ret = 0;
switch (op)
{
case AV_LOCK_CREATE:
mutex = new std::mutex();
*ctx = mutex;
ret = !mutex;
break;
case AV_LOCK_OBTAIN:
if (mutex)
mutex->lock();
break;
case AV_LOCK_RELEASE:
if (mutex)
mutex->unlock();
break;
case AV_LOCK_DESTROY:
delete mutex;
*ctx = 0;
break;
}
return ret;
}
#elif _WIN32
// MinGW with Win32 thread model
static int avcpp_lockmgr_cb(void **ctx, enum AVLockOp op)
{
if (!ctx)
return 1;
CRITICAL_SECTION *sec = static_cast<CRITICAL_SECTION*>(*ctx);
int ret = 0;
switch (op)
{
case AV_LOCK_CREATE:
sec = new CRITICAL_SECTION;
InitializeCriticalSection(sec);
*ctx = sec;
break;
case AV_LOCK_OBTAIN:
if (sec)
EnterCriticalSection(sec);
break;
case AV_LOCK_RELEASE:
if (sec)
LeaveCriticalSection(sec);
break;
case AV_LOCK_DESTROY:
if (ctx) {
DeleteCriticalSection(sec);
delete sec;
}
*ctx = nullptr;
break;
}
return ret;
}
#else
# error "Unknown Threading model"
#endif
#endif
void init()
{
#if LIBAVFORMAT_VERSION_MAJOR < 58 // FFmpeg 4.0
av_register_all();
#endif
avformat_network_init();
#if LIBAVCODEC_VERSION_MAJOR < 58 // FFmpeg 4.0
avcodec_register_all();
#endif
#if LIBAVFILTER_VERSION_MAJOR < 7 // FFmpeg 4.0
avfilter_register_all();
#endif
avdevice_register_all();
#if LIBAVCODEC_VERSION_MAJOR < 58 // FFmpeg 4.0
av_lockmgr_register(avcpp_lockmgr_cb);
#endif
set_logging_level(AV_LOG_ERROR);
// Ignore sigpipe by default
#ifdef __unix__
signal(SIGPIPE, SIG_IGN);
#endif
}
string error2string(int error)
{
char errorBuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(error, errorBuf, AV_ERROR_MAX_STRING_SIZE);
return string(errorBuf);
}
namespace v1 {
bool AvDeleter::operator()(SwsContext *&swsContext)
{
sws_freeContext(swsContext);
swsContext = nullptr;
return true;
}
bool AvDeleter::operator()(AVCodecContext *&codecContext)
{
#if LIBAVCODEC_VERSION_MAJOR >= 58
avcodec_free_context(&codecContext);
#else
avcodec_close(codecContext);
av_free(codecContext);
codecContext = nullptr;
#endif
return true;
}
bool AvDeleter::operator()(AVOutputFormat *&format)
{
// Only set format to zero, it can'be freed by user
format = 0;
return true;
}
bool AvDeleter::operator()(AVFormatContext *&formatContext)
{
avformat_free_context(formatContext);
formatContext = nullptr;
return true;
}
bool AvDeleter::operator()(AVFrame *&frame)
{
av_frame_free(&frame);
return true;
}
bool AvDeleter::operator()(AVPacket *&packet)
{
av_packet_free(&packet);
return true;
}
bool AvDeleter::operator()(AVDictionary *&dictionary)
{
av_dict_free(&dictionary);
dictionary = nullptr;
return true;
}
bool AvDeleter::operator ()(AVFilterInOut *&filterInOut)
{
avfilter_inout_free(&filterInOut);
return true;
}
} // ::v1
} // ::av