Skip to content

Commit 1ba29cc

Browse files
committed
Merge pull request opencv#9834 from mshabunin:mediasdk-win-support
2 parents df5b222 + 83655ba commit 1ba29cc

File tree

6 files changed

+93
-38
lines changed

6 files changed

+93
-38
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ OCV_OPTION(WITH_INTELPERC "Include Intel Perceptual Computing support" OFF
245245
OCV_OPTION(WITH_MATLAB "Include Matlab support" ON IF (NOT ANDROID AND NOT IOS AND NOT WINRT))
246246
OCV_OPTION(WITH_VA "Include VA support" OFF IF (UNIX AND NOT ANDROID) )
247247
OCV_OPTION(WITH_VA_INTEL "Include Intel VA-API/OpenCL support" OFF IF (UNIX AND NOT ANDROID) )
248-
OCV_OPTION(WITH_MFX "Include Intel Media SDK support" OFF IF (UNIX AND NOT ANDROID) )
248+
OCV_OPTION(WITH_MFX "Include Intel Media SDK support" OFF IF ((UNIX AND NOT ANDROID) OR (WIN32 AND NOT WINRT AND NOT MINGW)) )
249249
OCV_OPTION(WITH_GDAL "Include GDAL Support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) )
250250
OCV_OPTION(WITH_GPHOTO2 "Include gPhoto2 library support" ON IF (UNIX AND NOT ANDROID) )
251251
OCV_OPTION(WITH_LAPACK "Include Lapack library support" ON IF (NOT ANDROID AND NOT IOS) )

cmake/OpenCVDetectMediaSDK.cmake

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
set(root "$ENV{MFX_HOME}")
1+
set(HAVE_MFX 0)
22

3-
find_path(MFX_INCLUDE mfxdefs.h PATHS "${root}/include" NO_DEFAULT_PATH)
3+
if (UNIX)
4+
set(root "$ENV{MFX_HOME}")
5+
elseif(WIN32)
6+
set(root "$ENV{INTELMEDIASDKROOT}")
7+
endif()
48

59
# TODO: ICC? MINGW? ARM? IOS?
610
if(WIN32)
@@ -15,24 +19,41 @@ else()
1519
# ???
1620
endif()
1721

18-
find_library(MFX_LIBRARY mfx PATHS "${root}/lib/${arch}" NO_DEFAULT_PATH)
19-
find_library(MFX_VA_LIBRARY va)
20-
find_library(MFX_VA_DRM_LIBRARY va-drm)
22+
find_path(MFX_INCLUDE mfxdefs.h PATHS "${root}/include" NO_DEFAULT_PATH)
23+
message(STATUS "MFX_INCLUDE: ${MFX_INCLUDE} (${root}/include)")
24+
find_library(MFX_LIBRARY NAMES mfx PATHS "${root}/lib/${arch}" NO_DEFAULT_PATH)
25+
if(MSVC)
26+
if(MSVC14)
27+
find_library(MFX_LIBRARY NAMES libmfx_vs2015.lib PATHS "${root}/lib/${arch}" NO_DEFAULT_PATH)
28+
else()
29+
find_library(MFX_LIBRARY NAMES libmfx.lib PATHS "${root}/lib/${arch}" NO_DEFAULT_PATH)
30+
endif()
31+
endif()
32+
33+
if(NOT MFX_INCLUDE OR NOT MFX_LIBRARY)
34+
return()
35+
endif()
2136

22-
if(MFX_INCLUDE AND MFX_LIBRARY AND MFX_VA_LIBRARY AND MFX_VA_DRM_LIBRARY)
37+
set(deps)
38+
39+
if (UNIX)
40+
find_library(MFX_VA_LIBRARY va)
41+
find_library(MFX_VA_DRM_LIBRARY va-drm)
42+
if (NOT MFX_VA_LIBRARY OR NOT MFX_VA_DRM_LIBRARY)
43+
return()
44+
endif()
2345
add_library(mfx-va UNKNOWN IMPORTED)
2446
set_target_properties(mfx-va PROPERTIES IMPORTED_LOCATION "${MFX_VA_LIBRARY}")
25-
2647
add_library(mfx-va-drm UNKNOWN IMPORTED)
2748
set_target_properties(mfx-va-drm PROPERTIES IMPORTED_LOCATION "${MFX_VA_DRM_LIBRARY}")
28-
29-
add_library(mfx UNKNOWN IMPORTED)
30-
set_target_properties(mfx PROPERTIES
31-
IMPORTED_LOCATION "${MFX_LIBRARY}"
32-
INTERFACE_INCLUDE_DIRECTORIES "${MFX_INCLUDE}"
33-
INTERFACE_LINK_LIBRARIES "mfx-va;mfx-va-drm;-Wl,--exclude-libs=libmfx"
34-
)
35-
set(HAVE_MFX 1)
36-
else()
37-
set(HAVE_MFX 0)
49+
list(APPEND deps mfx-va mfx-va-drm "-Wl,--exclude-libs=libmfx")
3850
endif()
51+
52+
add_library(mfx UNKNOWN IMPORTED)
53+
set_target_properties(mfx PROPERTIES
54+
IMPORTED_LOCATION "${MFX_LIBRARY}"
55+
INTERFACE_INCLUDE_DIRECTORIES "${MFX_INCLUDE}"
56+
INTERFACE_LINK_LIBRARIES "${deps}"
57+
)
58+
59+
set(HAVE_MFX 1)

modules/videoio/src/cap_mfx_common.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include "cap_mfx_common.hpp"
66

77
// Linux specific
8+
#ifdef __linux__
89
#include <unistd.h>
910
#include <sys/types.h>
1011
#include <sys/stat.h>
1112
#include <fcntl.h>
13+
#endif
1214

1315
using namespace std;
1416
using namespace cv;
@@ -36,6 +38,8 @@ bool DeviceHandler::init(MFXVideoSession &session)
3638

3739
//==================================================================================================
3840

41+
#ifdef __linux__
42+
3943
VAHandle::VAHandle() {
4044
// TODO: provide a way of modifying this path
4145
const string filename = "/dev/dri/card0";
@@ -68,6 +72,19 @@ bool VAHandle::initDeviceSession(MFXVideoSession &session) {
6872
return false;
6973
}
7074

75+
#endif // __linux__
76+
77+
DeviceHandler * createDeviceHandler()
78+
{
79+
#if defined __linux__
80+
return new VAHandle();
81+
#elif defined _WIN32
82+
return new DXHandle();
83+
#else
84+
return 0;
85+
#endif
86+
}
87+
7188
//==================================================================================================
7289

7390
SurfacePool::SurfacePool(ushort width_, ushort height_, ushort count, const mfxFrameInfo &frameInfo, uchar bpp)
@@ -85,7 +102,8 @@ SurfacePool::SurfacePool(ushort width_, ushort height_, ushort count, const mfxF
85102
surface.Info = frameInfo;
86103
surface.Data.Y = dataPtr;
87104
surface.Data.UV = dataPtr + width * height;
88-
surface.Data.Pitch = width;
105+
surface.Data.PitchLow = width & 0xFFFF;
106+
surface.Data.PitchHigh = (width >> 16) & 0xFFFF;
89107
DBG(cout << "allocate surface " << (void*)&surface << ", Y = " << (void*)dataPtr << " (" << width << "x" << height << ")" << endl);
90108
}
91109
DBG(cout << "Allocated: " << endl
@@ -112,7 +130,7 @@ ReadBitstream::ReadBitstream(const char *filename, size_t maxSize) : drain(false
112130
input.open(filename, std::ios::in | std::ios::binary);
113131
DBG(cout << "Open " << filename << " -> " << input.is_open() << std::endl);
114132
memset(&stream, 0, sizeof(stream));
115-
stream.MaxLength = maxSize;
133+
stream.MaxLength = (mfxU32)maxSize;
116134
stream.Data = new mfxU8[stream.MaxLength];
117135
CV_Assert(stream.Data);
118136
}
@@ -139,7 +157,7 @@ bool ReadBitstream::read()
139157
input.read((char*)(stream.Data + stream.DataLength), stream.MaxLength - stream.DataLength);
140158
if (input.eof() || input.good())
141159
{
142-
mfxU32 bytesRead = input.gcount();
160+
mfxU32 bytesRead = (mfxU32)input.gcount();
143161
if (bytesRead > 0)
144162
{
145163
stream.DataLength += bytesRead;
@@ -157,7 +175,7 @@ WriteBitstream::WriteBitstream(const char * filename, size_t maxSize)
157175
output.open(filename, std::ios::out | std::ios::binary);
158176
DBG(cout << "BS Open " << filename << " -> " << output.is_open() << std::endl);
159177
memset(&stream, 0, sizeof(stream));
160-
stream.MaxLength = maxSize;
178+
stream.MaxLength = (mfxU32)maxSize;
161179
stream.Data = new mfxU8[stream.MaxLength];
162180
DBG(cout << "BS Allocate " << maxSize << " bytes (" << ((float)maxSize / (1 << 20)) << " Mb)" << endl);
163181
CV_Assert(stream.Data);

modules/videoio/src/cap_mfx_common.hpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ inline void cleanup(T * &ptr)
168168

169169
//==================================================================================================
170170

171-
struct Plugin
171+
class Plugin
172172
{
173173
public:
174174
static Plugin * loadEncoderPlugin(MFXVideoSession &session, mfxU32 codecId)
@@ -206,7 +206,7 @@ struct Plugin
206206

207207
//==================================================================================================
208208

209-
struct ReadBitstream
209+
class ReadBitstream
210210
{
211211
public:
212212
ReadBitstream(const char * filename, size_t maxSize = 10 * 1024 * 1024);
@@ -225,7 +225,7 @@ struct ReadBitstream
225225

226226
//==================================================================================================
227227

228-
struct WriteBitstream
228+
class WriteBitstream
229229
{
230230
public:
231231
WriteBitstream(const char * filename, size_t maxSize);
@@ -268,7 +268,7 @@ class SurfacePool
268268
SurfacePool(const SurfacePool &);
269269
SurfacePool &operator=(const SurfacePool &);
270270
public:
271-
ushort width, height;
271+
size_t width, height;
272272
size_t oneSize;
273273
cv::AutoBuffer<uchar, 0> buffers;
274274
std::vector<mfxFrameSurface1> surfaces;
@@ -286,6 +286,7 @@ class DeviceHandler {
286286

287287

288288
// Linux specific
289+
#ifdef __linux__
289290

290291
#include <va/va_drm.h>
291292

@@ -302,7 +303,26 @@ class VAHandle : public DeviceHandler {
302303
int file;
303304
};
304305

305-
// TODO: Windows specific
306+
#endif // __linux__
306307

308+
// Windows specific
309+
#ifdef _WIN32
310+
311+
#include <Windows.h>
312+
inline void sleep(unsigned long sec) { Sleep(1000 * sec); }
313+
314+
class DXHandle : public DeviceHandler {
315+
public:
316+
DXHandle() {}
317+
~DXHandle() {}
318+
private:
319+
DXHandle(const DXHandle &);
320+
DXHandle &operator=(const DXHandle &);
321+
virtual bool initDeviceSession(MFXVideoSession &) { return true; }
322+
};
323+
324+
#endif // _WIN32
325+
326+
DeviceHandler * createDeviceHandler();
307327

308328
#endif // MFXHELPER_H

modules/videoio/src/cap_mfx_reader.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ VideoCapture_IntelMFX::VideoCapture_IntelMFX(const cv::String &filename)
3838
mfxStatus res = MFX_ERR_NONE;
3939

4040
// Init device and session
41-
42-
deviceHandler = new VAHandle();
41+
deviceHandler = createDeviceHandler();
4342
session = new MFXVideoSession();
4443
if (!deviceHandler->init(*session))
4544
{
@@ -227,7 +226,6 @@ bool VideoCapture_IntelMFX::grabFrame()
227226
MSG(cerr << "MFX: Bad status: " << res << endl);
228227
return false;
229228
}
230-
return false;
231229
}
232230
}
233231

modules/videoio/src/cap_mfx_writer.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ VideoWriter_IntelMFX::VideoWriter_IntelMFX(const String &filename, int _fourcc,
4040
}
4141

4242
// Init device and session
43-
44-
deviceHandler = new VAHandle();
43+
deviceHandler = createDeviceHandler();
4544
session = new MFXVideoSession();
4645
if (!deviceHandler->init(*session))
4746
{
@@ -71,7 +70,7 @@ VideoWriter_IntelMFX::VideoWriter_IntelMFX(const String &filename, int _fourcc,
7170
memset(&params, 0, sizeof(params));
7271
params.mfx.CodecId = codecId;
7372
params.mfx.TargetUsage = MFX_TARGETUSAGE_BALANCED;
74-
params.mfx.TargetKbps = frameSize.area() * fps / 500; // TODO: set in options
73+
params.mfx.TargetKbps = (mfxU16)cvRound(frameSize.area() * fps / 500); // TODO: set in options
7574
params.mfx.RateControlMethod = MFX_RATECONTROL_VBR;
7675
params.mfx.FrameInfo.FrameRateExtN = cvRound(fps * 1000);
7776
params.mfx.FrameInfo.FrameRateExtD = 1000;
@@ -80,10 +79,10 @@ VideoWriter_IntelMFX::VideoWriter_IntelMFX(const String &filename, int _fourcc,
8079
params.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
8180
params.mfx.FrameInfo.CropX = 0;
8281
params.mfx.FrameInfo.CropY = 0;
83-
params.mfx.FrameInfo.CropW = frameSize.width;
84-
params.mfx.FrameInfo.CropH = frameSize.height;
85-
params.mfx.FrameInfo.Width = alignSize(frameSize.width, 32);
86-
params.mfx.FrameInfo.Height = alignSize(frameSize.height, 32);
82+
params.mfx.FrameInfo.CropW = (mfxU16)frameSize.width;
83+
params.mfx.FrameInfo.CropH = (mfxU16)frameSize.height;
84+
params.mfx.FrameInfo.Width = (mfxU16)alignSize(frameSize.width, 32);
85+
params.mfx.FrameInfo.Height = (mfxU16)alignSize(frameSize.height, 32);
8786
params.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
8887
res = encoder->Query(&params, &params);
8988
DBG(cout << "MFX Query: " << res << endl << params.mfx << params.mfx.FrameInfo);
@@ -263,7 +262,6 @@ bool VideoWriter_IntelMFX::write_one(cv::InputArray bgr)
263262
MSG(cerr << "MFX: Bad status: " << res << endl);
264263
return false;
265264
}
266-
return true;
267265
}
268266
}
269267

0 commit comments

Comments
 (0)