Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videoio/src/cap_mfx_common.hpp
16354 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html
4
5
#ifndef MFXHELPER_H
6
#define MFXHELPER_H
7
8
#include "opencv2/core.hpp"
9
10
#include <iostream>
11
#include <fstream>
12
#include <sstream>
13
14
#include <mfxcommon.h>
15
#include <mfxstructures.h>
16
#include <mfxvideo++.h>
17
#include <mfxvp8.h>
18
#include <mfxjpeg.h>
19
#include <mfxplugin++.h>
20
21
// //
22
// Debug helpers //
23
// //
24
25
#if 0
26
# define DBG(i) i
27
#else
28
# define DBG(i)
29
#endif
30
31
#if 1
32
# define MSG(i) i
33
#else
34
# define MSG(i)
35
#endif
36
37
template <typename T>
38
struct HexWrap {
39
HexWrap(T val_) : val(val_) {}
40
T val;
41
};
42
43
template <typename T>
44
inline std::ostream & operator<<(std::ostream &out, const HexWrap<T> &wrap) {
45
std::ios_base::fmtflags flags = out.flags(std::ios::hex | std::ios::showbase);
46
out << wrap.val;
47
out.flags(flags);
48
return out;
49
}
50
51
template <typename T>
52
inline ::HexWrap<T> asHex(const T & val) {
53
return ::HexWrap<T>(val);
54
}
55
56
struct FourCC
57
{
58
FourCC(uint val) : val32(val) {}
59
FourCC(char a, char b, char c, char d) { val8[0] = a; val8[1] = b; val8[2] = c; val8[3] = d; }
60
union {
61
uint val32;
62
int vali32;
63
uchar val8[4];
64
};
65
};
66
67
inline std::ostream & operator<<(std::ostream &out, FourCC cc) {
68
for (size_t i = 0; i < 4; out << cc.val8[i++]) {}
69
out << " (" << asHex(cc.val32) << ")";
70
return out;
71
}
72
73
inline std::string mfxStatusToString(mfxStatus s) {
74
switch (s)
75
{
76
case MFX_ERR_NONE: return "MFX_ERR_NONE";
77
case MFX_ERR_UNKNOWN: return "MFX_ERR_UNKNOWN";
78
case MFX_ERR_NULL_PTR: return "MFX_ERR_NULL_PTR";
79
case MFX_ERR_UNSUPPORTED: return "MFX_ERR_UNSUPPORTED";
80
case MFX_ERR_MEMORY_ALLOC: return "MFX_ERR_MEMORY_ALLOC";
81
case MFX_ERR_NOT_ENOUGH_BUFFER: return "MFX_ERR_NOT_ENOUGH_BUFFER";
82
case MFX_ERR_INVALID_HANDLE: return "MFX_ERR_INVALID_HANDLE";
83
case MFX_ERR_LOCK_MEMORY: return "MFX_ERR_LOCK_MEMORY";
84
case MFX_ERR_NOT_INITIALIZED: return "MFX_ERR_NOT_INITIALIZED";
85
case MFX_ERR_NOT_FOUND: return "MFX_ERR_NOT_FOUND";
86
case MFX_ERR_MORE_DATA: return "MFX_ERR_MORE_DATA";
87
case MFX_ERR_MORE_SURFACE: return "MFX_ERR_MORE_SURFACE";
88
case MFX_ERR_ABORTED: return "MFX_ERR_ABORTED";
89
case MFX_ERR_DEVICE_LOST: return "MFX_ERR_DEVICE_LOST";
90
case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM: return "MFX_ERR_INCOMPATIBLE_VIDEO_PARAM";
91
case MFX_ERR_INVALID_VIDEO_PARAM: return "MFX_ERR_INVALID_VIDEO_PARAM";
92
case MFX_ERR_UNDEFINED_BEHAVIOR: return "MFX_ERR_UNDEFINED_BEHAVIOR";
93
case MFX_ERR_DEVICE_FAILED: return "MFX_ERR_DEVICE_FAILED";
94
case MFX_ERR_MORE_BITSTREAM: return "MFX_ERR_MORE_BITSTREAM";
95
case MFX_ERR_INCOMPATIBLE_AUDIO_PARAM: return "MFX_ERR_INCOMPATIBLE_AUDIO_PARAM";
96
case MFX_ERR_INVALID_AUDIO_PARAM: return "MFX_ERR_INVALID_AUDIO_PARAM";
97
case MFX_ERR_GPU_HANG: return "MFX_ERR_GPU_HANG";
98
case MFX_ERR_REALLOC_SURFACE: return "MFX_ERR_REALLOC_SURFACE";
99
case MFX_WRN_IN_EXECUTION: return "MFX_WRN_IN_EXECUTION";
100
case MFX_WRN_DEVICE_BUSY: return "MFX_WRN_DEVICE_BUSY";
101
case MFX_WRN_VIDEO_PARAM_CHANGED: return "MFX_WRN_VIDEO_PARAM_CHANGED";
102
case MFX_WRN_PARTIAL_ACCELERATION: return "MFX_WRN_PARTIAL_ACCELERATION";
103
case MFX_WRN_INCOMPATIBLE_VIDEO_PARAM: return "MFX_WRN_INCOMPATIBLE_VIDEO_PARAM";
104
case MFX_WRN_VALUE_NOT_CHANGED: return "MFX_WRN_VALUE_NOT_CHANGED";
105
case MFX_WRN_OUT_OF_RANGE: return "MFX_WRN_OUT_OF_RANGE";
106
case MFX_WRN_FILTER_SKIPPED: return "MFX_WRN_FILTER_SKIPPED";
107
case MFX_WRN_INCOMPATIBLE_AUDIO_PARAM: return "MFX_WRN_INCOMPATIBLE_AUDIO_PARAM";
108
default: return "<Invalid mfxStatus>";
109
}
110
}
111
112
inline std::ostream & operator<<(std::ostream &out, mfxStatus s) {
113
out << mfxStatusToString(s) << " (" << (int)s << ")"; return out;
114
}
115
116
inline std::ostream & operator<<(std::ostream &out, const mfxInfoMFX &info) {
117
out << "InfoMFX:" << std::endl
118
<< "| Codec: " << FourCC(info.CodecId) << " / " << info.CodecProfile << " / " << info.CodecLevel << std::endl
119
<< "| DecodedOrder: " << info.DecodedOrder << std::endl
120
<< "| TimeStampCalc: " << info.TimeStampCalc << std::endl
121
;
122
return out;
123
}
124
125
inline std::ostream & operator<<(std::ostream & out, const mfxFrameInfo & info) {
126
out << "FrameInfo: " << std::endl
127
<< "| FourCC: " << FourCC(info.FourCC) << std::endl
128
<< "| Size: " << info.Width << "x" << info.Height << std::endl
129
<< "| ROI: " << "(" << info.CropX << ";" << info.CropY << ") " << info.CropW << "x" << info.CropH << std::endl
130
<< "| BitDepth(L/C): " << info.BitDepthLuma << " / " << info.BitDepthChroma << std::endl
131
<< "| Shift: " << info.Shift << std::endl
132
<< "| TemporalID: " << info.FrameId.TemporalId << std::endl
133
<< "| FrameRate: " << info.FrameRateExtN << "/" << info.FrameRateExtD << std::endl
134
<< "| AspectRatio: " << info.AspectRatioW << "x" << info.AspectRatioH << std::endl
135
<< "| PicStruct: " << info.PicStruct << std::endl
136
<< "| ChromaFormat: " << info.ChromaFormat << std::endl
137
;
138
return out;
139
}
140
141
inline std::ostream & operator<<(std::ostream &out, const mfxFrameData &data) {
142
out << "FrameData:" << std::endl
143
<< "| NumExtParam: " << data.NumExtParam << std::endl
144
<< "| MemType: " << data.MemType << std::endl
145
<< "| PitchHigh: " << data.PitchHigh << std::endl
146
<< "| TimeStamp: " << data.TimeStamp << std::endl
147
<< "| FrameOrder: " << data.FrameOrder << std::endl
148
<< "| Locked: " << data.Locked << std::endl
149
<< "| Pitch: " << data.PitchHigh << ", " << data.PitchLow << std::endl
150
<< "| Y: " << (void*)data.Y << std::endl
151
<< "| U: " << (void*)data.U << std::endl
152
<< "| V: " << (void*)data.V << std::endl
153
;
154
return out;
155
}
156
157
//==================================================================================================
158
159
template <typename T>
160
inline void cleanup(T * &ptr)
161
{
162
if (ptr)
163
{
164
delete ptr;
165
ptr = 0;
166
}
167
}
168
169
//==================================================================================================
170
171
class Plugin
172
{
173
public:
174
static Plugin * loadEncoderPlugin(MFXVideoSession &session, mfxU32 codecId)
175
{
176
static const mfxPluginUID hevc_enc_uid = { 0x6f, 0xad, 0xc7, 0x91, 0xa0, 0xc2, 0xeb, 0x47, 0x9a, 0xb6, 0xdc, 0xd5, 0xea, 0x9d, 0xa3, 0x47 };
177
if (codecId == MFX_CODEC_HEVC)
178
return new Plugin(session, hevc_enc_uid);
179
return 0;
180
}
181
static Plugin * loadDecoderPlugin(MFXVideoSession &session, mfxU32 codecId)
182
{
183
static const mfxPluginUID hevc_dec_uid = { 0x33, 0xa6, 0x1c, 0x0b, 0x4c, 0x27, 0x45, 0x4c, 0xa8, 0xd8, 0x5d, 0xde, 0x75, 0x7c, 0x6f, 0x8e };
184
if (codecId == MFX_CODEC_HEVC)
185
return new Plugin(session, hevc_dec_uid);
186
return 0;
187
}
188
~Plugin()
189
{
190
if (isGood())
191
MFXVideoUSER_UnLoad(session, &uid);
192
}
193
bool isGood() const { return res >= MFX_ERR_NONE; }
194
private:
195
MFXVideoSession &session;
196
mfxPluginUID uid;
197
mfxStatus res;
198
private:
199
Plugin(MFXVideoSession &_session, mfxPluginUID _uid) : session(_session), uid(_uid)
200
{
201
res = MFXVideoUSER_Load(session, &uid, 1);
202
}
203
Plugin(const Plugin &);
204
Plugin &operator=(const Plugin &);
205
};
206
207
//==================================================================================================
208
209
class ReadBitstream
210
{
211
public:
212
ReadBitstream(const char * filename, size_t maxSize = 10 * 1024 * 1024);
213
~ReadBitstream();
214
bool isOpened() const;
215
bool isDone() const;
216
bool read();
217
private:
218
ReadBitstream(const ReadBitstream &);
219
ReadBitstream &operator=(const ReadBitstream &);
220
public:
221
std::fstream input;
222
mfxBitstream stream;
223
bool drain;
224
};
225
226
//==================================================================================================
227
228
class WriteBitstream
229
{
230
public:
231
WriteBitstream(const char * filename, size_t maxSize);
232
~WriteBitstream();
233
bool write();
234
bool isOpened() const;
235
private:
236
WriteBitstream(const WriteBitstream &);
237
WriteBitstream &operator=(const WriteBitstream &);
238
public:
239
std::fstream output;
240
mfxBitstream stream;
241
};
242
243
//==================================================================================================
244
245
class SurfacePool
246
{
247
public:
248
SurfacePool(ushort width_, ushort height_, ushort count, const mfxFrameInfo & frameInfo, uchar bpp = 12);
249
~SurfacePool();
250
mfxFrameSurface1 *getFreeSurface();
251
252
template <typename T>
253
static SurfacePool * create(T * instance, mfxVideoParam &params)
254
{
255
CV_Assert(instance);
256
mfxFrameAllocRequest request;
257
memset(&request, 0, sizeof(request));
258
mfxStatus res = instance->QueryIOSurf(&params, &request);
259
DBG(std::cout << "MFX QueryIOSurf: " << res << std::endl);
260
if (res < MFX_ERR_NONE)
261
return 0;
262
return new SurfacePool(request.Info.Width,
263
request.Info.Height,
264
request.NumFrameSuggested,
265
params.mfx.FrameInfo);
266
}
267
private:
268
SurfacePool(const SurfacePool &);
269
SurfacePool &operator=(const SurfacePool &);
270
public:
271
size_t width, height;
272
size_t oneSize;
273
cv::AutoBuffer<uchar, 0> buffers;
274
std::vector<mfxFrameSurface1> surfaces;
275
};
276
277
//==================================================================================================
278
279
class DeviceHandler {
280
public:
281
virtual ~DeviceHandler() {}
282
bool init(MFXVideoSession &session);
283
protected:
284
virtual bool initDeviceSession(MFXVideoSession &session) = 0;
285
};
286
287
288
// Linux specific
289
#ifdef __linux__
290
291
#include <unistd.h>
292
#include <va/va_drm.h>
293
294
class VAHandle : public DeviceHandler {
295
public:
296
VAHandle();
297
~VAHandle();
298
private:
299
VAHandle(const VAHandle &);
300
VAHandle &operator=(const VAHandle &);
301
virtual bool initDeviceSession(MFXVideoSession &session);
302
private:
303
VADisplay display;
304
int file;
305
};
306
307
#endif // __linux__
308
309
// Windows specific
310
#ifdef _WIN32
311
312
#include <Windows.h>
313
inline void sleep(unsigned long sec) { Sleep(1000 * sec); }
314
315
class DXHandle : public DeviceHandler {
316
public:
317
DXHandle() {}
318
~DXHandle() {}
319
private:
320
DXHandle(const DXHandle &);
321
DXHandle &operator=(const DXHandle &);
322
virtual bool initDeviceSession(MFXVideoSession &) { return true; }
323
};
324
325
#endif // _WIN32
326
327
DeviceHandler * createDeviceHandler();
328
329
#endif // MFXHELPER_H
330
331