Path: blob/master/modules/videoio/src/cap_mfx_common.cpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html34#include "cap_mfx_common.hpp"56// Linux specific7#ifdef __linux__8#include <sys/types.h>9#include <sys/stat.h>10#include <fcntl.h>11#endif1213using namespace std;14using namespace cv;1516bool DeviceHandler::init(MFXVideoSession &session)17{18mfxStatus res = MFX_ERR_NONE;19mfxIMPL impl = MFX_IMPL_AUTO;20mfxVersion ver = { {19, 1} };2122res = session.Init(impl, &ver);23DBG(cout << "MFX SessionInit: " << res << endl);2425res = session.QueryIMPL(&impl);26DBG(cout << "MFX QueryIMPL: " << res << " => " << asHex(impl) << endl);2728res = session.QueryVersion(&ver);29DBG(cout << "MFX QueryVersion: " << res << " => " << ver.Major << "." << ver.Minor << endl);3031if (res != MFX_ERR_NONE)32return false;3334return initDeviceSession(session);35}3637//==================================================================================================3839#ifdef __linux__4041VAHandle::VAHandle() {42// TODO: provide a way of modifying this path43const string filename = "/dev/dri/renderD128";44file = open(filename.c_str(), O_RDWR);45if (file < 0)46CV_Error(Error::StsError, "Can't open file: " + filename);47display = vaGetDisplayDRM(file);48}4950VAHandle::~VAHandle() {51if (display) {52vaTerminate(display);53}54if (file >= 0) {55close(file);56}57}5859bool VAHandle::initDeviceSession(MFXVideoSession &session) {60int majorVer = 0, minorVer = 0;61VAStatus va_res = vaInitialize(display, &majorVer, &minorVer);62DBG(cout << "vaInitialize: " << va_res << endl << majorVer << '.' << minorVer << endl);63if (va_res == VA_STATUS_SUCCESS) {64mfxStatus mfx_res = session.SetHandle(static_cast<mfxHandleType>(MFX_HANDLE_VA_DISPLAY), display);65DBG(cout << "MFX SetHandle: " << mfx_res << endl);66if (mfx_res == MFX_ERR_NONE) {67return true;68}69}70return false;71}7273#endif // __linux__7475DeviceHandler * createDeviceHandler()76{77#if defined __linux__78return new VAHandle();79#elif defined _WIN3280return new DXHandle();81#else82return 0;83#endif84}8586//==================================================================================================8788SurfacePool::SurfacePool(ushort width_, ushort height_, ushort count, const mfxFrameInfo &frameInfo, uchar bpp)89: width(alignSize(width_, 32)),90height(alignSize(height_, 32)),91oneSize(width * height * bpp / 8),92buffers(count * oneSize),93surfaces(count)94{95for(int i = 0; i < count; ++i)96{97mfxFrameSurface1 &surface = surfaces[i];98uint8_t * dataPtr = buffers.data() + oneSize * i;99memset(&surface, 0, sizeof(mfxFrameSurface1));100surface.Info = frameInfo;101surface.Data.Y = dataPtr;102surface.Data.UV = dataPtr + width * height;103surface.Data.PitchLow = width & 0xFFFF;104surface.Data.PitchHigh = (width >> 16) & 0xFFFF;105DBG(cout << "allocate surface " << (void*)&surface << ", Y = " << (void*)dataPtr << " (" << width << "x" << height << ")" << endl);106}107DBG(cout << "Allocated: " << endl108<< "- surface data: " << buffers.size() << " bytes" << endl109<< "- surface headers: " << surfaces.size() * sizeof(mfxFrameSurface1) << " bytes" << endl);110}111112SurfacePool::~SurfacePool()113{114}115116mfxFrameSurface1 *SurfacePool::getFreeSurface()117{118for(std::vector<mfxFrameSurface1>::iterator i = surfaces.begin(); i != surfaces.end(); ++i)119if (!i->Data.Locked)120return &(*i);121return 0;122}123124//==================================================================================================125126ReadBitstream::ReadBitstream(const char *filename, size_t maxSize) : drain(false)127{128input.open(filename, std::ios::in | std::ios::binary);129DBG(cout << "Open " << filename << " -> " << input.is_open() << std::endl);130memset(&stream, 0, sizeof(stream));131stream.MaxLength = (mfxU32)maxSize;132stream.Data = new mfxU8[stream.MaxLength];133CV_Assert(stream.Data);134}135136ReadBitstream::~ReadBitstream()137{138delete[] stream.Data;139}140141bool ReadBitstream::isOpened() const142{143return input.is_open();144}145146bool ReadBitstream::isDone() const147{148return input.eof();149}150151bool ReadBitstream::read()152{153memmove(stream.Data, stream.Data + stream.DataOffset, stream.DataLength);154stream.DataOffset = 0;155input.read((char*)(stream.Data + stream.DataLength), stream.MaxLength - stream.DataLength);156if (input.eof() || input.good())157{158mfxU32 bytesRead = (mfxU32)input.gcount();159if (bytesRead > 0)160{161stream.DataLength += bytesRead;162DBG(cout << "read " << bytesRead << " bytes" << endl);163return true;164}165}166return false;167}168169//==================================================================================================170171WriteBitstream::WriteBitstream(const char * filename, size_t maxSize)172{173output.open(filename, std::ios::out | std::ios::binary);174DBG(cout << "BS Open " << filename << " -> " << output.is_open() << std::endl);175memset(&stream, 0, sizeof(stream));176stream.MaxLength = (mfxU32)maxSize;177stream.Data = new mfxU8[stream.MaxLength];178DBG(cout << "BS Allocate " << maxSize << " bytes (" << ((float)maxSize / (1 << 20)) << " Mb)" << endl);179CV_Assert(stream.Data);180}181182WriteBitstream::~WriteBitstream()183{184delete[] stream.Data;185}186187bool WriteBitstream::write()188{189output.write((char*)(stream.Data + stream.DataOffset), stream.DataLength);190stream.DataLength = 0;191return output.good();192}193194bool WriteBitstream::isOpened() const195{196return output.is_open();197}198199//==================================================================================================200201202