Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videoio/src/cap_mfx_common.cpp
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
#include "cap_mfx_common.hpp"
6
7
// Linux specific
8
#ifdef __linux__
9
#include <sys/types.h>
10
#include <sys/stat.h>
11
#include <fcntl.h>
12
#endif
13
14
using namespace std;
15
using namespace cv;
16
17
bool DeviceHandler::init(MFXVideoSession &session)
18
{
19
mfxStatus res = MFX_ERR_NONE;
20
mfxIMPL impl = MFX_IMPL_AUTO;
21
mfxVersion ver = { {19, 1} };
22
23
res = session.Init(impl, &ver);
24
DBG(cout << "MFX SessionInit: " << res << endl);
25
26
res = session.QueryIMPL(&impl);
27
DBG(cout << "MFX QueryIMPL: " << res << " => " << asHex(impl) << endl);
28
29
res = session.QueryVersion(&ver);
30
DBG(cout << "MFX QueryVersion: " << res << " => " << ver.Major << "." << ver.Minor << endl);
31
32
if (res != MFX_ERR_NONE)
33
return false;
34
35
return initDeviceSession(session);
36
}
37
38
//==================================================================================================
39
40
#ifdef __linux__
41
42
VAHandle::VAHandle() {
43
// TODO: provide a way of modifying this path
44
const string filename = "/dev/dri/renderD128";
45
file = open(filename.c_str(), O_RDWR);
46
if (file < 0)
47
CV_Error(Error::StsError, "Can't open file: " + filename);
48
display = vaGetDisplayDRM(file);
49
}
50
51
VAHandle::~VAHandle() {
52
if (display) {
53
vaTerminate(display);
54
}
55
if (file >= 0) {
56
close(file);
57
}
58
}
59
60
bool VAHandle::initDeviceSession(MFXVideoSession &session) {
61
int majorVer = 0, minorVer = 0;
62
VAStatus va_res = vaInitialize(display, &majorVer, &minorVer);
63
DBG(cout << "vaInitialize: " << va_res << endl << majorVer << '.' << minorVer << endl);
64
if (va_res == VA_STATUS_SUCCESS) {
65
mfxStatus mfx_res = session.SetHandle(static_cast<mfxHandleType>(MFX_HANDLE_VA_DISPLAY), display);
66
DBG(cout << "MFX SetHandle: " << mfx_res << endl);
67
if (mfx_res == MFX_ERR_NONE) {
68
return true;
69
}
70
}
71
return false;
72
}
73
74
#endif // __linux__
75
76
DeviceHandler * createDeviceHandler()
77
{
78
#if defined __linux__
79
return new VAHandle();
80
#elif defined _WIN32
81
return new DXHandle();
82
#else
83
return 0;
84
#endif
85
}
86
87
//==================================================================================================
88
89
SurfacePool::SurfacePool(ushort width_, ushort height_, ushort count, const mfxFrameInfo &frameInfo, uchar bpp)
90
: width(alignSize(width_, 32)),
91
height(alignSize(height_, 32)),
92
oneSize(width * height * bpp / 8),
93
buffers(count * oneSize),
94
surfaces(count)
95
{
96
for(int i = 0; i < count; ++i)
97
{
98
mfxFrameSurface1 &surface = surfaces[i];
99
uint8_t * dataPtr = buffers.data() + oneSize * i;
100
memset(&surface, 0, sizeof(mfxFrameSurface1));
101
surface.Info = frameInfo;
102
surface.Data.Y = dataPtr;
103
surface.Data.UV = dataPtr + width * height;
104
surface.Data.PitchLow = width & 0xFFFF;
105
surface.Data.PitchHigh = (width >> 16) & 0xFFFF;
106
DBG(cout << "allocate surface " << (void*)&surface << ", Y = " << (void*)dataPtr << " (" << width << "x" << height << ")" << endl);
107
}
108
DBG(cout << "Allocated: " << endl
109
<< "- surface data: " << buffers.size() << " bytes" << endl
110
<< "- surface headers: " << surfaces.size() * sizeof(mfxFrameSurface1) << " bytes" << endl);
111
}
112
113
SurfacePool::~SurfacePool()
114
{
115
}
116
117
mfxFrameSurface1 *SurfacePool::getFreeSurface()
118
{
119
for(std::vector<mfxFrameSurface1>::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
120
if (!i->Data.Locked)
121
return &(*i);
122
return 0;
123
}
124
125
//==================================================================================================
126
127
ReadBitstream::ReadBitstream(const char *filename, size_t maxSize) : drain(false)
128
{
129
input.open(filename, std::ios::in | std::ios::binary);
130
DBG(cout << "Open " << filename << " -> " << input.is_open() << std::endl);
131
memset(&stream, 0, sizeof(stream));
132
stream.MaxLength = (mfxU32)maxSize;
133
stream.Data = new mfxU8[stream.MaxLength];
134
CV_Assert(stream.Data);
135
}
136
137
ReadBitstream::~ReadBitstream()
138
{
139
delete[] stream.Data;
140
}
141
142
bool ReadBitstream::isOpened() const
143
{
144
return input.is_open();
145
}
146
147
bool ReadBitstream::isDone() const
148
{
149
return input.eof();
150
}
151
152
bool ReadBitstream::read()
153
{
154
memmove(stream.Data, stream.Data + stream.DataOffset, stream.DataLength);
155
stream.DataOffset = 0;
156
input.read((char*)(stream.Data + stream.DataLength), stream.MaxLength - stream.DataLength);
157
if (input.eof() || input.good())
158
{
159
mfxU32 bytesRead = (mfxU32)input.gcount();
160
if (bytesRead > 0)
161
{
162
stream.DataLength += bytesRead;
163
DBG(cout << "read " << bytesRead << " bytes" << endl);
164
return true;
165
}
166
}
167
return false;
168
}
169
170
//==================================================================================================
171
172
WriteBitstream::WriteBitstream(const char * filename, size_t maxSize)
173
{
174
output.open(filename, std::ios::out | std::ios::binary);
175
DBG(cout << "BS Open " << filename << " -> " << output.is_open() << std::endl);
176
memset(&stream, 0, sizeof(stream));
177
stream.MaxLength = (mfxU32)maxSize;
178
stream.Data = new mfxU8[stream.MaxLength];
179
DBG(cout << "BS Allocate " << maxSize << " bytes (" << ((float)maxSize / (1 << 20)) << " Mb)" << endl);
180
CV_Assert(stream.Data);
181
}
182
183
WriteBitstream::~WriteBitstream()
184
{
185
delete[] stream.Data;
186
}
187
188
bool WriteBitstream::write()
189
{
190
output.write((char*)(stream.Data + stream.DataOffset), stream.DataLength);
191
stream.DataLength = 0;
192
return output.good();
193
}
194
195
bool WriteBitstream::isOpened() const
196
{
197
return output.is_open();
198
}
199
200
//==================================================================================================
201
202