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