File size: 7,713 Bytes
dffe123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html

#include "cap_mfx_common.hpp"

// Linux specific
#ifdef __linux__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif

using namespace std;
using namespace cv;

#ifndef HAVE_ONEVPL
static mfxIMPL getImpl()
{
    static const size_t res = utils::getConfigurationParameterSizeT("OPENCV_VIDEOIO_MFX_IMPL", MFX_IMPL_AUTO_ANY);
    return (mfxIMPL)res;
}
#endif

static size_t getExtraSurfaceNum()
{
    static const size_t res = cv::utils::getConfigurationParameterSizeT("OPENCV_VIDEOIO_MFX_EXTRA_SURFACE_NUM", 1);
    return res;
}

static size_t getPoolTimeoutSec()
{
    static const size_t res = utils::getConfigurationParameterSizeT("OPENCV_VIDEOIO_MFX_POOL_TIMEOUT", 1);
    return res;
}

#ifdef HAVE_ONEVPL
// oneVPL loader singleton (HW implementation only)
static mfxLoader setupVPLLoader()
{
    mfxLoader instance = MFXLoad();
    mfxConfig cfg = MFXCreateConfig(instance);
    mfxVariant impl;
    impl.Type = MFX_VARIANT_TYPE_U32;
    impl.Data.U32 = MFX_IMPL_TYPE_HARDWARE;
    MFXSetConfigFilterProperty(cfg, (const mfxU8*)"mfxImplDescription.Impl", impl);
    DBG(cerr << "MFX Load: " << instance << endl);
    return instance;
}

mfxLoader getVPLLoaderInstance()
{
    static mfxLoader instance = setupVPLLoader();
    return instance;
}
#endif

//==================================================================================================

bool DeviceHandler::init(MFXVideoSession_WRAP &session)
{
    mfxStatus res = MFX_ERR_NONE;
    mfxVersion ver = { {19, 1} };

#ifdef HAVE_ONEVPL
    res = session.CreateSession();
    DBG(cout << "MFX CreateSession: " << res << endl);
#else
    mfxIMPL impl = getImpl();

    res = session.Init(impl, &ver);
    DBG(cout << "MFX SessionInit: " << res << endl);

    res = session.QueryIMPL(&impl);
    DBG(cout << "MFX QueryIMPL: " << res << " => " << asHex(impl) << endl);
#endif

    res = session.QueryVersion(&ver);
    DBG(cout << "MFX QueryVersion: " << res << " => " << ver.Major << "." << ver.Minor << endl);

    if (res != MFX_ERR_NONE)
        return false;

    return initDeviceSession(session);
}

//==================================================================================================

#ifdef __linux__

VAHandle::VAHandle() {
    // TODO: provide a way of modifying this path
    const string filename = "/dev/dri/renderD128";
    file = open(filename.c_str(), O_RDWR);
    if (file < 0)
        CV_Error(Error::StsError, "Can't open file: " + filename);
    display = vaGetDisplayDRM(file);
}

VAHandle::~VAHandle() {
    if (display) {
        vaTerminate(display);
    }
    if (file >= 0) {
        close(file);
    }
}

bool VAHandle::initDeviceSession(MFXVideoSession_WRAP &session) {
    int majorVer = 0, minorVer = 0;
    VAStatus va_res = vaInitialize(display, &majorVer, &minorVer);
    DBG(cout << "vaInitialize: " << va_res << endl << majorVer << '.' << minorVer << endl);
    if (va_res == VA_STATUS_SUCCESS) {
        mfxStatus mfx_res = session.SetHandle(static_cast<mfxHandleType>(MFX_HANDLE_VA_DISPLAY), display);
        DBG(cout << "MFX SetHandle: " << mfx_res << endl);
        if (mfx_res == MFX_ERR_NONE) {
            return true;
        }
    }
    return false;
}

#endif // __linux__

DeviceHandler * createDeviceHandler()
{
#if defined __linux__
    return new VAHandle();
#elif defined _WIN32
    return new DXHandle();
#else
    return 0;
#endif
}

//==================================================================================================

SurfacePool::SurfacePool(ushort width_, ushort height_, ushort count, const mfxFrameInfo &frameInfo, uchar bpp)
    : width(alignSize(width_, 32)),
      height(alignSize(height_, 32)),
      oneSize(width * height * bpp / 8),
      buffers(count * oneSize),
      surfaces(count)
{
    for(int i = 0; i < count; ++i)
    {
        mfxFrameSurface1 &surface = surfaces[i];
        uint8_t * dataPtr = buffers.data() + oneSize * i;
        memset(&surface, 0, sizeof(mfxFrameSurface1));
        surface.Info = frameInfo;
        surface.Data.Y = dataPtr;
        surface.Data.UV = dataPtr + width * height;
        surface.Data.PitchLow = width & 0xFFFF;
        surface.Data.PitchHigh = (width >> 16) & 0xFFFF;
        DBG(cout << "allocate surface " << (void*)&surface << ", Y = " << (void*)dataPtr << " (" << width << "x" << height << ")" << endl);
    }
    DBG(cout << "Allocated: " << endl
         << "- surface data: " << buffers.size() << " bytes" << endl
         << "- surface headers: " << surfaces.size() * sizeof(mfxFrameSurface1) << " bytes" << endl);
}

SurfacePool::~SurfacePool()
{
}

SurfacePool * SurfacePool::_create(const mfxFrameAllocRequest &request, const mfxVideoParam &params)
{
    return new SurfacePool(request.Info.Width,
                           request.Info.Height,
                           saturate_cast<ushort>((size_t)request.NumFrameSuggested + getExtraSurfaceNum()),
                           params.mfx.FrameInfo);
}

mfxFrameSurface1 *SurfacePool::getFreeSurface()
{
    const int64 start = cv::getTickCount();
    do
    {
        for(std::vector<mfxFrameSurface1>::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
            if (!i->Data.Locked)
                return &(*i);
        sleep_ms(10);
    }
    while((cv::getTickCount() - start) / cv::getTickFrequency() < getPoolTimeoutSec()); // seconds
    DBG(cout << "No free surface!" << std::endl);
    return 0;
}

//==================================================================================================

ReadBitstream::ReadBitstream(const char *filename, size_t maxSize) : drain(false)
{
    input.open(filename, std::ios::in | std::ios::binary);
    DBG(cout << "Open " << filename << " -> " << input.is_open() << std::endl);
    memset(&stream, 0, sizeof(stream));
    stream.MaxLength = (mfxU32)maxSize;
    stream.Data = new mfxU8[stream.MaxLength];
    CV_Assert(stream.Data);
}

ReadBitstream::~ReadBitstream()
{
    delete[] stream.Data;
}

bool ReadBitstream::isOpened() const
{
    return input.is_open();
}

bool ReadBitstream::isDone() const
{
    return input.eof();
}

bool ReadBitstream::read()
{
    memmove(stream.Data, stream.Data + stream.DataOffset, stream.DataLength);
    stream.DataOffset = 0;
    input.read((char*)(stream.Data + stream.DataLength), stream.MaxLength - stream.DataLength);
    if (input.eof() || input.good())
    {
        mfxU32 bytesRead = (mfxU32)input.gcount();
        if (bytesRead > 0)
        {
            stream.DataLength += bytesRead;
            DBG(cout << "read " << bytesRead << " bytes" << endl);
            return true;
        }
    }
    return false;
}

//==================================================================================================

WriteBitstream::WriteBitstream(const char * filename, size_t maxSize)
{
    output.open(filename, std::ios::out | std::ios::binary);
    DBG(cout << "BS Open " << filename << " -> " << output.is_open() << std::endl);
    memset(&stream, 0, sizeof(stream));
    stream.MaxLength = (mfxU32)maxSize;
    stream.Data = new mfxU8[stream.MaxLength];
    DBG(cout << "BS Allocate " << maxSize << " bytes (" << ((float)maxSize / (1 << 20)) << " Mb)" << endl);
    CV_Assert(stream.Data);
}

WriteBitstream::~WriteBitstream()
{
    delete[] stream.Data;
}

bool WriteBitstream::write()
{
    output.write((char*)(stream.Data + stream.DataOffset), stream.DataLength);
    stream.DataLength = 0;
    return output.good();
}

bool WriteBitstream::isOpened() const
{
    return output.is_open();
}