-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffclasses.h
316 lines (236 loc) · 6.87 KB
/
ffclasses.h
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#ifndef CLOSECV_H
#define CLOSECV_H
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavformat/avio.h"
#include "libavutil/pixdesc.h"
#include "libavutil/hwcontext.h"
#include "libavutil/opt.h"
#include "libavutil/avassert.h"
#include "libavutil/imgutils.h"
#include "libavutil/motion_vector.h"
#include "libavutil/frame.h"
#include <libavutil/timestamp.h>
}
#include <QString>
#include <thread>
#include <mutex>
#include <queue>
#include <QDebug>
#include <condition_variable>
#include "global.h"
namespace ff {
class Image : public AVPicture
{
public:
Image(){}
Image (const Image &image) {
std::unique_lock<std::recursive_mutex> un1(mainMtx);
clone(image.data[0], image.linesize[0], image.height, image.width, image.format);
}
Image(unsigned char *data, int linesize, int height, int width, enum AVPixelFormat format){
std::unique_lock<std::recursive_mutex> un1(mainMtx);
clone(data, linesize, height, width, format);
}
Image(int height, int width, enum AVPixelFormat format = AV_PIX_FMT_RGB24) {
std::unique_lock<std::recursive_mutex> un1(mainMtx);
allocateMemory(height, width, format);
}
~Image() {
freeMemory();
}
bool getIsInvalidSize() {
return isInvalidSize;
}
void allocateMemory(int height, int width, enum AVPixelFormat format = AV_PIX_FMT_RGB24) {
std::unique_lock<std::recursive_mutex> un1(mainMtx);
if (isAllocateMemory == true)
freeMemory();
this->height = height;
this->width = width;
if (height <= 0 || width <= 0) {
isInvalidSize = 1;
return;
} else
isInvalidSize = 0;
avpicture_alloc(this, format, width, height);
if (format == AV_PIX_FMT_RGB24)
channels = 3;
else
channels = 4;
this->format = format;
isAllocateMemory = true;
}
void freeMemory() {
std::unique_lock<std::recursive_mutex> un1(mainMtx);
if (isAllocateMemory == true) {
avpicture_free(this);
isAllocateMemory = false;
}
}
void clone(unsigned char *data, int linesize, int height, int width, enum AVPixelFormat format = AV_PIX_FMT_RGB24){
std::unique_lock<std::recursive_mutex> un1(mainMtx);
allocateMemory(height, width, format);
this->linesize[0] = linesize;
for (int j = 0; j < channels * height * width; j++) {
this->data[0][j] = data[j];
}
}
void operator = (const Image &image) {
std::unique_lock<std::recursive_mutex> un1(mainMtx);
clone(image.data[0], image.linesize[0], image.height, image.width, image.format);
}
int height;
int width;
int channels;
enum AVPixelFormat format;
bool isAllocateMemory = false;
bool isInvalidSize = true;
private:
std::recursive_mutex mainMtx;
};
struct Size
{
Size(int width = 0, int height = 0) {
this->width = width;
this->height = height;
}
int width = 0;
int height = 0;
bool isValid() {
return bool(width * height);
}
};
struct VecRgb {
VecRgb(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0) {
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
}
unsigned char r() {
return rgb[0];
}
unsigned char g() {
return rgb[1];
}
unsigned char b() {
return rgb[2];
}
unsigned char operator[](int index){
return rgb[index];
}
private:
unsigned char rgb[3];
};
class VideoGraber
{
public:
VideoGraber() {
std::unique_lock<std::recursive_mutex> uni(mainMtx);
isOpen = false;
}
VideoGraber(const std::string &url) {
std::unique_lock<std::recursive_mutex> uni(mainMtx);
isOpen = false;
open(url);
}
~VideoGraber(){
release();
}
bool open(const std::string &linkVideoDevice);
void operator >> (Image &image);
void rewind(long timeStampMcSec, bool highAccuracy = 0);
long getCurrentMcSec();
long getDuractionMcSec();
long getFirstTimestamp();
int mcSec2FrameNum(long);
long frameNum2McSec(int);
double getFps();
bool isOpened();
int frameHeight();
int frameWidth();
int streamID();
double getTimeBaseAVstream() {
return timeBaseAVstream;
}
QDateTime getTimestampStart() {
return timestampStart;
}
long get2PreTs(long curTs);
long get2NextTs(long curTs);
void release();
protected:
int num = 0;
std::recursive_mutex mainMtx;
static std::mutex sharedOpenMtx;
static std::mutex sharedReleaseMtx;
void grabStream();
AVFormatContext *formatCtx;
AVStream *stream;
AVCodecContext *codecCtx;
AVPacket packet;
SwsContext *swsCtx;
Image image;
int streamNum;
std::string linkVideoDevice;
AVFrame *frame;
std::atomic<bool> isOpen;
double fps;
long maxFrameIndex; // Порой может быть некорректен
double timeBaseAVstream;
QDateTime timestampStart;
std::vector<long> frameNumbers;
};
class FastEncoder : public QObject
{
Q_OBJECT
public:
FastEncoder() {
std::unique_lock<std::recursive_mutex> uni(mainMtx);
isOpenIn = false;
isOpenOut = false;
stopSignal = false;
}
~FastEncoder() {
release();
}
void start(const std::string &inputFile, const std::string &outputFile, const QDateTime startTime);
void release();
void stop();
bool isOpened() {
std::unique_lock<std::recursive_mutex> uni(mainMtx);
return isOpenOut * isOpenIn;
}
protected:
AVFormatContext *formatCtx;
AVPacket packet;
int videoStream;
AVStream *in_stream;
AVFormatContext *outFormatCtx;
AVStream *out_stream;
QDateTime startTime;
std::recursive_mutex mainMtx;
std::mutex stopMtx;
std::recursive_mutex grabMtx;
static std::mutex sharedOpenMtx;
std::atomic<bool> isOpenIn;
std::atomic<bool> isOpenOut;
std::atomic<bool> stopSignal;
std::string linkVideoDevice;
int64_t pacDuraction; // Расчётная величина на случай если кодер не даёт такой информации
bool connectOutputFile(const std::string &outputFile);
bool open(const std::string &linkVideoDevice);
void releaseInput();
signals:
void fileCreated(QString);
};
void mergeVideo(const std::string directory, const std::string &outputFile);
void mergeVideo(const std::queue<std::string> &linkVideoDevices,
const std::string &outputFile);
void trimVideo(const std::string &linkVideoDevice, const std::string &saveDirectory, bool isOriginalTimestamp,
long firstFrameMcS = LONG_MIN, long secondFrameMcs = LONG_MIN);
}
#endif // CLOSECV_H