forked from KangLin/RabbitIm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTool.cpp
More file actions
479 lines (434 loc) · 12.7 KB
/
Tool.cpp
File metadata and controls
479 lines (434 loc) · 12.7 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "Tool.h"
#include "Global.h"
CTool::CTool(QObject *parent) :
QObject(parent)
{
}
CTool::~CTool()
{
}
//设置日志的回调函数
void Log(void*, int, const char* fmt, va_list vl)
{
LOG_MODEL_DEBUG("ffmpeg", fmt, vl);
}
int CTool::SetFFmpegLog()
{
//在程序初始化时设置ffmpeg日志的回调函数
av_log_set_callback(Log);
return 0;
}
AVPixelFormat CTool::QVideoFrameFormatToFFMpegPixFormat(const QVideoFrame::PixelFormat format)
{
if(QVideoFrame::Format_RGB32 == format)
return AV_PIX_FMT_RGB32;
else if(QVideoFrame::Format_BGR24 == format)
return AV_PIX_FMT_BGR24;
else if(QVideoFrame::Format_RGB24 == format)
return AV_PIX_FMT_RGB24;
else if(QVideoFrame::Format_YUYV == format)
return AV_PIX_FMT_YUYV422;
else if(QVideoFrame::Format_UYVY == format)
return AV_PIX_FMT_UYVY422;
else if(QVideoFrame::Format_NV21 == format)
return AV_PIX_FMT_NV21;
else
return AV_PIX_FMT_NONE;
}
AVPixelFormat CTool::QXmppVideoFrameFormatToFFMpegPixFormat(const QXmppVideoFrame::PixelFormat format)
{
if(QXmppVideoFrame::Format_RGB32 == format)
return AV_PIX_FMT_RGB32;
else if(QXmppVideoFrame::Format_RGB24 == format)
return AV_PIX_FMT_RGB24;
else if(QXmppVideoFrame::Format_YUYV == format)
return AV_PIX_FMT_YUYV422;
else if(QXmppVideoFrame::Format_UYVY == format)
return AV_PIX_FMT_UYVY422;
else if(QXmppVideoFrame::Format_YUV420P == format)
return AV_PIX_FMT_YUV420P;
else
return AV_PIX_FMT_NONE;
}
//如果转换成功,则调用者使用完 pOutFrame 后,需要调用 avpicture_free(pOutFrame) 释放内存
//成功返回0,不成功返回非0
int CTool::ConvertFormat(/*[in]*/ const QVideoFrame &inFrame,
/*[out]*/AVPicture &outFrame,
/*[in]*/ int nOutWidth,
/*[in]*/ int nOutHeight,
/*[in]*/ AVPixelFormat pixelFormat)
{
int nRet = 0;
AVPicture pic;
nRet = avpicture_fill(&pic, (uint8_t*) inFrame.bits(),
QVideoFrameFormatToFFMpegPixFormat(inFrame.pixelFormat()),
inFrame.width(),
inFrame.height());
if(nRet < 0)
{
LOG_MODEL_DEBUG("Tool", "avpicture_fill fail:%x", nRet);
return nRet;
}
nRet = ConvertFormat(pic, inFrame.width(), inFrame.height(),
QVideoFrameFormatToFFMpegPixFormat(inFrame.pixelFormat()),
outFrame, nOutWidth, nOutHeight, pixelFormat);
return nRet;
}
int CTool::ConvertFormat(/*[in]*/ const QXmppVideoFrame &inFrame,
/*[out]*/AVPicture &outFrame,
/*[in]*/ int nOutWidth,
/*[in]*/ int nOutHeight,
/*[in]*/ AVPixelFormat pixelFormat)
{
int nRet = 0;
AVPicture pic;
nRet = avpicture_fill(&pic, (uint8_t*)inFrame.bits(),
QXmppVideoFrameFormatToFFMpegPixFormat(inFrame.pixelFormat()),
inFrame.width(),
inFrame.height());
if(nRet < 0)
{
LOG_MODEL_ERROR("Tool", "avpicture_fill fail:%x", nRet);
return nRet;
}
nRet = ConvertFormat(pic, inFrame.width(), inFrame.height(),
QXmppVideoFrameFormatToFFMpegPixFormat(inFrame.pixelFormat()),
outFrame, nOutWidth, nOutHeight,
pixelFormat);
return nRet;
}
int CTool::ConvertFormat(/*[in]*/ const AVPicture &inFrame,
/*[in]*/ int nInWidth,
/*[in]*/ int nInHeight,
/*[in]*/ AVPixelFormat inPixelFormat,
/*[out]*/AVPicture &outFrame,
/*[in]*/ int nOutWidth,
/*[in]*/ int nOutHeight,
/*[in]*/ AVPixelFormat outPixelFormat)
{
int nRet = 0;
struct SwsContext* pSwsCtx = NULL;
//分配输出空间
nRet = avpicture_alloc(&outFrame, outPixelFormat, nOutWidth, nOutHeight);
if(nRet)
{
LOG_MODEL_ERROR("Tool", "avpicture_alloc fail:%x", nRet);
return nRet;
}
if(inPixelFormat == outPixelFormat
&& nInWidth == nOutWidth
&& nInHeight == nOutHeight)
{
av_picture_copy(&outFrame, &inFrame, inPixelFormat, nInWidth, nInHeight);
return 0;
}
//设置图像转换上下文
pSwsCtx = sws_getCachedContext (NULL,
nInWidth, //源宽度
nInHeight, //源高度
inPixelFormat, //源格式
nOutWidth, //目标宽度
nOutHeight, //目标高度
outPixelFormat, //目的格式
SWS_FAST_BILINEAR, //转换算法
NULL, NULL, NULL);
if(NULL == pSwsCtx)
{
LOG_MODEL_ERROR("Tool", "sws_getContext false");
avpicture_free(&outFrame);
return -3;
}
//进行图片转换
nRet = sws_scale(pSwsCtx,
inFrame.data, inFrame.linesize,
0, nInHeight,
outFrame.data, outFrame.linesize);
if(nRet < 0)
{
LOG_MODEL_ERROR("Tool", "sws_scale fail:%x", nRet);
avpicture_free(&outFrame);
}
else
{
nRet = 0;
}
sws_freeContext(pSwsCtx);
return nRet;
}
#ifdef RABBITIM_USER_OPENCV
cv::Mat CTool::ImageRotate(cv::Mat & src, const CvPoint &_center, double angle)
{
CvPoint2D32f center;
center.x = float(_center.x);
center.y = float(_center.y);
//计算二维旋转的仿射变换矩阵
cv::Mat M = cv::getRotationMatrix2D(center, angle, 1);
// rotate
cv::Mat dst;
cv::warpAffine(src, dst, M, cvSize(src.cols, src.rows), cv::INTER_LINEAR);
return dst;
}
#endif
void CTool::YUV420spRotate90(uchar *dst, const uchar *src, int srcWidth, int srcHeight)
{
static int nWidth = 0, nHeight = 0;
static int wh = 0;
static int uvHeight = 0;
if(srcWidth != nWidth || srcHeight != nHeight)
{
nWidth = srcWidth;
nHeight = srcHeight;
wh = srcWidth * srcHeight;
uvHeight = srcHeight >> 1;//uvHeight = height / 2
}
//旋转Y
int k = 0;
for(int i = 0; i < srcWidth; i++) {
int nPos = 0;
for(int j = 0; j < srcHeight; j++) {
dst[k] = src[nPos + i];
k++;
nPos += srcWidth;
}
}
for(int i = 0; i < srcWidth; i+=2){
int nPos = wh;
for(int j = 0; j < uvHeight; j++) {
dst[k] = src[nPos + i];
dst[k + 1] = src[nPos + i + 1];
k += 2;
nPos += srcWidth;
}
}
return;
}
void CTool::YUV420spRotateNegative90(uchar *dst, const uchar *src, int srcWidth, int height)
{
static int nWidth = 0, nHeight = 0;
static int wh = 0;
static int uvHeight = 0;
if(srcWidth != nWidth || height != nHeight)
{
nWidth = srcWidth;
nHeight = height;
wh = srcWidth * height;
uvHeight = height >> 1;//uvHeight = height / 2
}
//旋转Y
int k = 0;
for(int i = 0; i < srcWidth; i++){
int nPos = srcWidth - 1;
for(int j = 0; j < height; j++)
{
dst[k] = src[nPos - i];
k++;
nPos += srcWidth;
}
}
for(int i = 0; i < srcWidth; i+=2){
int nPos = wh + srcWidth - 1;
for(int j = 0; j < uvHeight; j++) {
dst[k] = src[nPos - i - 1];
dst[k + 1] = src[nPos - i];
k += 2;
nPos += srcWidth;
}
}
return;
}
void CTool::YUV420spRotate90(uchar *dst, const uchar *src, int srcWidth, int height, int mode)
{
switch (mode) {
case 1:
YUV420spRotate90(dst, src, srcWidth, height);
break;
case -1:
YUV420spRotateNegative90(dst, src, srcWidth, height);
break;
default:
break;
}
return;
}
//以Y轴做镜像
void CTool::YUV420spMirrorY(uchar *dst, const uchar *src, int srcWidth, int srcHeight)
{
//镜像Y
int k = 0;
int nPos = -1;
for(int j = 0; j < srcHeight; j++) {
nPos += srcWidth;
for(int i = 0; i < srcWidth; i++)
{
dst[k] = src[nPos - i];
k++;
}
}
int uvHeight = srcHeight >> 1; // uvHeight = height / 2
for(int j = 0; j < uvHeight; j ++) {
nPos += srcWidth;
for(int i = 0; i < srcWidth; i += 2)
{
dst[k] = src[nPos - i - 1];
dst[k+1] = src[nPos - i];
k+=2;
}
}
}
//以XY轴做镜像
void CTool::YUV420spMirrorXY(uchar *dst, const uchar *src, int width, int srcHeight)
{
static int nWidth = 0, nHeight = 0;
static int wh = 0;
static int nUVPos = 0;
static int uvHeight = 0;
if(width != nWidth || srcHeight != nHeight)
{
nWidth = width;
nHeight = srcHeight;
wh = width * srcHeight;
uvHeight = srcHeight >> 1; //uvHeight = height / 2
nUVPos = wh + uvHeight * width - 1;
}
//镜像Y
int k = 0;
int nPos = wh - 1;
for(int j = 0; j < srcHeight; j++) {
for(int i = 0; i < width; i++)
{
dst[k] = src[nPos - i];
k++;
}
nPos -= width;
}
nPos = nUVPos;
for(int j = 0; j < uvHeight; j ++) {
for(int i = 0; i < width; i += 2)
{
dst[k] = src[nPos - i - 1];
dst[k + 1] = src[nPos - i];
k += 2;
}
nPos -= width;
}
}
//以X轴做镜像
void CTool::YUV420spMirrorX(uchar *dst, const uchar *src, int width, int srcHeight)
{
static int nWidth = 0, nHeight = 0;
static int wh = 0;
static int nUVPos = 0;
static int uvHeight = 0;
if(width != nWidth || srcHeight != nHeight)
{
nWidth = width;
nHeight = srcHeight;
wh = width * srcHeight;
uvHeight = srcHeight >> 1; //uvHeight = height / 2
nUVPos = wh + uvHeight * width;
}
//镜像Y
int k = 0;
int nPos = wh - 1;
for(int j = 0; j < srcHeight; j++) {
nPos -= width;
for(int i = 0; i < width; i++)
{
dst[k] = src[nPos + i];
k++;
}
}
nPos = nUVPos;
for(int j = 0; j < uvHeight; j ++) {
nPos -= width;
for(int i = 0; i < width; i += 2)
{
dst[k] = src[nPos + i];
dst[k+1] = src[nPos + i + 1];
k+=2;
}
}
}
void CTool::YUV420spMirror(uchar *dst, const uchar *src, int srcWidth, int srcHeight, int mode)
{
switch (mode) {
case 0:
return YUV420spMirrorY(dst, src, srcWidth, srcHeight);
break;
case 1:
return YUV420spMirrorX(dst, src, srcWidth, srcHeight);
case -1:
return YUV420spMirrorXY(dst, src, srcWidth, srcHeight);
default:
break;
}
}
//设置窗口中的所有子窗体背景为透明
int CTool::SetAllChildrenTransparent(QWidget *pWin)
{
QObjectList objChildList = pWin->children();
for (int i = 0; i < objChildList.size(); i++)
{
QObject *pObj= objChildList.at(i);
if (pObj->inherits("QWidget"))
{
QWidget *pWidget = (QWidget*)pObj;
pWidget->setStyleSheet("background-color: transparent");
}
}
return 0;
}
//获取字节数的大小文本
QString CTool::GetSizeString(qint64 size, int prec)
{
int index = 0;
QString suffix = "B";
double value = 0.0;
while(size > 0)
{
switch (index) {
case 0:
{
suffix = "B";
value = size % 1024;
break;
}
case 1:
{
suffix = "KB";
value = value / 1024.0 + size % 1024;
break;
}
case 2:
{
suffix = "MB";
value = value / 1024.0 + size % 1024;
break;
}
case 3:
{
suffix = "GB";
value = value / 1024.0 + size % 1024;
break;
}
case 4:
{
suffix = "TB";
value = value / 1024.0 + size % 1024;
break;
}
case 5:
{
suffix = "TB";
value = value + size * 1024;
return QString("%1%2").arg(QString::number(value,'f',prec)).arg(suffix);
break;
}
default:
break;
}
size /= 1024;
index++;
}
return QString("%1%2").arg(QString::number(value,'f',prec)).arg(suffix);
}