Commit a8656ea2 authored by Alexey Ershov's avatar Alexey Ershov

implemented core support & sample

fixed whitespaces
fixed issues
fixed issue
fixed module statement issues
fixed access mode
added initialization check
fixed warning
parent 09b9b0fb
......@@ -205,6 +205,7 @@ OCV_OPTION(WITH_OPENCLAMDBLAS "Include AMD OpenCL BLAS library support" ON
OCV_OPTION(WITH_DIRECTX "Include DirectX support" ON IF (WIN32 AND NOT WINRT) )
OCV_OPTION(WITH_INTELPERC "Include Intel Perceptual Computing support" OFF IF (WIN32 AND NOT WINRT) )
OCV_OPTION(WITH_IPP_A "Include Intel IPP_A support" OFF IF (MSVC OR X86 OR X86_64) )
OCV_OPTION(WITH_VAAPI "Include VA-API support" OFF IF (UNIX AND NOT ANDROID) )
OCV_OPTION(WITH_GDAL "Include GDAL Support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) )
OCV_OPTION(WITH_GPHOTO2 "Include gPhoto2 library support" ON IF (UNIX AND NOT ANDROID) )
......@@ -1065,6 +1066,10 @@ if(DEFINED WITH_IPP_A)
status(" Use IPP Async:" HAVE_IPP_A THEN "YES" ELSE NO)
endif(DEFINED WITH_IPP_A)
if(DEFINED WITH_VAAPI)
status(" Use Intel VA-API:" HAVE_VAAPI THEN "YES (MSDK: ${VAAPI_MSDK_ROOT} OpenCL: ${VAAPI_IOCL_ROOT})" ELSE NO)
endif(DEFINED WITH_VAAPI)
status(" Use Eigen:" HAVE_EIGEN THEN "YES (ver ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})" ELSE NO)
status(" Use Cuda:" HAVE_CUDA THEN "YES (ver ${CUDA_VERSION_STRING})" ELSE NO)
status(" Use OpenCL:" HAVE_OPENCL THEN YES ELSE NO)
......
......@@ -317,3 +317,11 @@ ocv_clear_vars(HAVE_GPHOTO2)
if(WITH_GPHOTO2)
CHECK_MODULE(libgphoto2 HAVE_GPHOTO2)
endif(WITH_GPHOTO2)
# --- VA-API ---
if(WITH_VAAPI)
include("${OpenCV_SOURCE_DIR}/cmake/OpenCVFindVAAPI.cmake")
if(VAAPI_IOCL_INCLUDE_DIR)
ocv_include_directories(${VAAPI_IOCL_INCLUDE_DIR})
endif()
endif(WITH_VAAPI)
# Main variables:
# VAAPI_MSDK_INCLUDE_DIR and VAAPI_IOCL_INCLUDE_DIR to use VAAPI
# HAVE_VAAPI for conditional compilation OpenCV with/without VAAPI
# VAAPI_MSDK_ROOT - root of Intel MSDK installation
# VAAPI_IOCL_ROOT - root of Intel OCL installation
if(UNIX AND NOT ANDROID)
if($ENV{VAAPI_MSDK_ROOT})
set(VAAPI_MSDK_ROOT $ENV{VAAPI_MSDK_ROOT})
else()
set(VAAPI_MSDK_ROOT "/opt/intel/mediasdk")
endif()
if($ENV{VAAPI_IOCL_ROOT})
set(VAAPI_IOCL_ROOT $ENV{VAAPI_IOCL_ROOT})
else()
set(VAAPI_IOCL_ROOT "/opt/intel/opencl")
endif()
find_path(
VAAPI_MSDK_INCLUDE_DIR
NAMES mfxdefs.h
PATHS ${VAAPI_MSDK_ROOT}
PATH_SUFFIXES include
DOC "Path to Intel MSDK headers")
find_path(
VAAPI_IOCL_INCLUDE_DIR
NAMES CL/va_ext.h
PATHS ${VAAPI_IOCL_ROOT}
PATH_SUFFIXES include
DOC "Path to Intel OpenCL headers")
endif()
if(VAAPI_MSDK_INCLUDE_DIR AND VAAPI_IOCL_INCLUDE_DIR)
set(HAVE_VAAPI TRUE)
set(VAAPI_EXTRA_LIBS "-lva" "-lva-drm")
else()
set(HAVE_VAAPI FALSE)
message(WARNING "Intel MSDK & OpenCL installation is not found.")
endif()
mark_as_advanced(FORCE VAAPI_MSDK_INCLUDE_DIR VAAPI_IOCL_INCLUDE_DIR)
......@@ -188,3 +188,6 @@
/* gPhoto2 library */
#cmakedefine HAVE_GPHOTO2
/* Intel VA-API */
#cmakedefine HAVE_VAAPI
// 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.
// Copyright (C) 2015, Itseez, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
#ifndef __OPENCV_CORE_VAAPI_HPP__
#define __OPENCV_CORE_VAAPI_HPP__
#ifndef __cplusplus
# error vaapi.hpp header must be compiled as C++
#endif
#include "opencv2/core.hpp"
#include "ocl.hpp"
#if defined(HAVE_VAAPI)
# include "va/va.h"
#else // HAVE_VAAPI
# if !defined(_VA_H_)
typedef void* VADisplay;
typedef unsigned int VASurfaceID;
# endif // !_VA_H_
#endif // HAVE_VAAPI
namespace cv { namespace vaapi {
/** @addtogroup core_vaapi
This section describes CL-VA (VA-API) interoperability.
To enable CL-VA interoperability support, configure OpenCV using CMake with WITH_VAAPI=ON . Currently VA-API is
supported on Linux only. You should also install Intel Media Server Studio (MSS) to use this feature. You may
have to specify the path(s) to MSS components for cmake in environment variables: VAAPI_MSDK_ROOT for Media SDK
(default is "/opt/intel/mediasdk"), and VAAPI_IOCL_ROOT for Intel OpenCL (default is "/opt/intel/opencl").
To use VA-API interoperability you should first create VADisplay (libva), and then call initializeContextFromVA()
function to create OpenCL context and set up interoperability.
*/
//! @{
/////////////////// CL-VA Interoperability Functions ///////////////////
namespace ocl {
using namespace cv::ocl;
// TODO static functions in the Context class
/** @brief Creates OpenCL context from VA.
@param display - VADisplay for which CL interop should be established.
@return Returns reference to OpenCL Context
*/
CV_EXPORTS Context& initializeContextFromVA(VADisplay display);
} // namespace cv::vaapi::ocl
/** @brief Converts InputArray to VASurfaceID object.
@param src - source InputArray.
@param surface - destination VASurfaceID object.
@param size - size of image represented by VASurfaceID object.
*/
CV_EXPORTS void convertToVASurface(InputArray src, VASurfaceID surface, Size size);
/** @brief Converts VASurfaceID object to OutputArray.
@param surface - source VASurfaceID object.
@param size - size of image represented by VASurfaceID object.
@param dst - destination OutputArray.
*/
CV_EXPORTS void convertFromVASurface(VASurfaceID surface, Size size, OutputArray dst);
//! @}
}} // namespace cv::vaapi
#endif /* __OPENCV_CORE_VAAPI_HPP__ */
......@@ -50,6 +50,7 @@
#include "opencv2/core/core_c.h"
#include "opencv2/core/cuda.hpp"
#include "opencv2/core/opengl.hpp"
#include "opencv2/core/vaapi.hpp"
#include "opencv2/core/private.hpp"
#include "opencv2/core/private.cuda.hpp"
......
This diff is collapsed.
......@@ -22,6 +22,10 @@ if((NOT ANDROID) AND HAVE_OPENGL)
add_subdirectory(opengl)
endif()
if(UNIX AND NOT ANDROID AND HAVE_VAAPI)
add_subdirectory(vaapi)
endif()
if(ANDROID AND BUILD_ANDROID_EXAMPLES)
add_subdirectory(android)
endif()
......
SET(OPENCV_VAAPI_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_imgcodecs opencv_videoio opencv_highgui)
ocv_check_dependencies(${OPENCV_VAAPI_SAMPLES_REQUIRED_DEPS})
if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
set(project "vaapi")
string(TOUPPER "${project}" project_upper)
project("${project}_samples")
ocv_include_modules_recurse(${OPENCV_VAAPI_SAMPLES_REQUIRED_DEPS})
# ---------------------------------------------
# Define executable targets
# ---------------------------------------------
MACRO(OPENCV_DEFINE_VAAPI_EXAMPLE name srcs)
set(the_target "example_${project}_${name}")
add_executable(${the_target} ${srcs})
ocv_target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${OPENCV_VAAPI_SAMPLES_REQUIRED_DEPS} ${VAAPI_EXTRA_LIBS})
set_target_properties(${the_target} PROPERTIES
OUTPUT_NAME "${project}-example-${name}"
PROJECT_LABEL "(EXAMPLE_${project_upper}) ${name}")
if(ENABLE_SOLUTION_FOLDERS)
set_target_properties(${the_target} PROPERTIES FOLDER "samples//${project}")
endif()
ENDMACRO()
file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
foreach(sample_filename ${all_samples})
get_filename_component(sample ${sample_filename} NAME_WE)
file(GLOB sample_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${sample}.*)
OPENCV_DEFINE_VAAPI_EXAMPLE(${sample} ${sample_srcs})
endforeach()
endif()
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <va/va.h>
#include <va/va_drm.h>
#define VAAPI_PCI_DIR "/sys/bus/pci/devices"
#define VAAPI_DRI_DIR "/dev/dri/"
#define VAAPI_PCI_DISPLAY_CONTROLLER_CLASS 0x03
namespace va {
static unsigned readId(const char* devName, const char* idName);
static int findAdapter(unsigned desiredVendorId);
bool openDisplay();
void closeDisplay();
int drmfd = -1;
VADisplay display = NULL;
bool initialized = false;
class Directory
{
typedef int (*fsort)(const struct dirent**, const struct dirent**);
public:
Directory(const char* path)
{
dirEntries = 0;
numEntries = scandir(path, &dirEntries, filterFunc, (fsort)alphasort);
}
~Directory()
{
if (numEntries && dirEntries)
{
for (int i = 0; i < numEntries; ++i)
free(dirEntries[i]);
free(dirEntries);
}
}
int count() const
{
return numEntries;
}
const struct dirent* operator[](int index) const
{
return ((dirEntries != 0) && (index >= 0) && (index < numEntries)) ? dirEntries[index] : 0;
}
protected:
static int filterFunc(const struct dirent* dir)
{
if (!dir) return 0;
if (!strcmp(dir->d_name, ".")) return 0;
if (!strcmp(dir->d_name, "..")) return 0;
return 1;
}
private:
int numEntries;
struct dirent** dirEntries;
};
static unsigned readId(const char* devName, const char* idName)
{
long int id = 0;
char fileName[256];
snprintf(fileName, sizeof(fileName), "%s/%s/%s", VAAPI_PCI_DIR, devName, idName);
FILE* file = fopen(fileName, "r");
if (file)
{
char str[16] = "";
if (fgets(str, sizeof(str), file))
id = strtol(str, NULL, 16);
fclose(file);
}
return (unsigned)id;
}
static int findAdapter(unsigned desiredVendorId)
{
int adapterIndex = -1;
int numAdapters = 0;
Directory dir(VAAPI_PCI_DIR);
for (int i = 0; i < dir.count(); ++i)
{
const char* name = dir[i]->d_name;
unsigned classId = readId(name, "class");
if ((classId >> 16) == VAAPI_PCI_DISPLAY_CONTROLLER_CLASS)
{
unsigned vendorId = readId(name, "vendor");
if (vendorId == desiredVendorId)
{
adapterIndex = numAdapters;
break;
}
++numAdapters;
}
}
return adapterIndex;
}
class NodeInfo
{
enum { NUM_NODES = 2 };
public:
NodeInfo(int adapterIndex)
{
const char* names[NUM_NODES] = { "renderD", "card" };
int numbers[NUM_NODES];
numbers[0] = adapterIndex+128;
numbers[1] = adapterIndex;
for (int i = 0; i < NUM_NODES; ++i)
{
int sz = sizeof(VAAPI_DRI_DIR) + strlen(names[i]) + 3;
paths[i] = new char [sz];
snprintf(paths[i], sz, "%s%s%d", VAAPI_DRI_DIR, names[i], numbers[i]);
}
}
~NodeInfo()
{
for (int i = 0; i < NUM_NODES; ++i)
{
delete paths[i];
paths[i] = 0;
}
}
int count() const
{
return NUM_NODES;
}
const char* path(int index) const
{
return ((index >= 0) && (index < NUM_NODES)) ? paths[index] : 0;
}
private:
char* paths[NUM_NODES];
};
bool openDisplay()
{
if (!initialized)
{
const unsigned IntelVendorID = 0x8086;
drmfd = -1;
display = 0;
int adapterIndex = findAdapter(IntelVendorID);
if (adapterIndex >= 0)
{
NodeInfo nodes(adapterIndex);
for (int i = 0; i < nodes.count(); ++i)
{
drmfd = open(nodes.path(i), O_RDWR);
if (drmfd >= 0)
{
display = vaGetDisplayDRM(drmfd);
if (display)
{
int majorVersion = 0, minorVersion = 0;
if (vaInitialize(display, &majorVersion, &minorVersion) == VA_STATUS_SUCCESS)
{
initialized = true;
return true;
}
display = 0;
}
close(drmfd);
drmfd = -1;
}
}
}
if (adapterIndex < 0)
return false; // Can't find Intel display adapter
if ((drmfd < 0) || !display)
return false; // Can't load VA display
}
return true;
}
void closeDisplay()
{
if (initialized)
{
if (display)
vaTerminate(display);
if (drmfd >= 0)
close(drmfd);
display = 0;
drmfd = -1;
initialized = false;
}
}
} // namespace va
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment