Skip to content

Commit

Permalink
Merge pull request opencv#13677 from mshabunin:video_plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Feb 26, 2019
2 parents 8bde6ae + 9702987 commit 4188e8a
Show file tree
Hide file tree
Showing 64 changed files with 2,278 additions and 1,476 deletions.
4 changes: 4 additions & 0 deletions modules/core/include/opencv2/core/private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,10 @@ Passed subdirectories are used in LIFO order.
*/
CV_EXPORTS void addDataSearchSubDirectory(const cv::String& subdir);

/** @brief Return location of OpenCV libraries or current executable
*/
CV_EXPORTS std::string getBinLocation();

//! @}

} // namespace utils
Expand Down
3 changes: 3 additions & 0 deletions modules/core/include/opencv2/core/utils/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ CV_EXPORTS cv::String canonical(const cv::String& path);
/** Join path components */
CV_EXPORTS cv::String join(const cv::String& base, const cv::String& path);

/** Get parent directory */
CV_EXPORTS cv::String getParent(const cv::String &path);

/**
* Generate a list of all files that match the globbing pattern.
*
Expand Down
7 changes: 6 additions & 1 deletion modules/core/src/utils/datafile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ static cv::String getModuleLocation(const void* addr)
return cv::String();
}

std::string getBinLocation()
{
return getModuleLocation((void*)getModuleLocation); // use code addr, doesn't work with static linkage!
}

cv::String findDataFile(const cv::String& relative_path,
const char* configuration_parameter,
const std::vector<String>* search_paths,
Expand Down Expand Up @@ -287,7 +292,7 @@ cv::String findDataFile(const cv::String& relative_path,
}
}

cv::String module_path = getModuleLocation((void*)getModuleLocation); // use code addr, doesn't work with static linkage!
cv::String module_path = getBinLocation();
CV_LOG_DEBUG(NULL, "Detected module path: '" << module_path << '\'');

if (!has_tested_build_directory &&
Expand Down
8 changes: 8 additions & 0 deletions modules/core/src/utils/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ cv::String join(const cv::String& base, const cv::String& path)
return result;
}

CV_EXPORTS cv::String getParent(const cv::String &path)
{
std::string::size_type loc = path.find_last_of("/\\");
if (loc == std::string::npos)
return std::string();
return std::string(path, 0, loc);
}

#if OPENCV_HAVE_FILESYSTEM_SUPPORT

cv::String canonical(const cv::String& path)
Expand Down
34 changes: 29 additions & 5 deletions modules/videoio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
set(VIDEOIO_PLUGIN_LIST "" CACHE STRING "List of videoio backends to be compiled as plugins (ffmpeg, gstreamer)")
set(VIDEOIO_ENABLE_PLUGINS "ON" CACHE BOOL "Allow building videoio plugin support")
set(VIDEOIO_ENABLE_STRICT_PLUGIN_CHECK "ON" CACHE BOOL "Make sure OpenCV version is the same in plugin and host code")
mark_as_advanced(VIDEOIO_PLUGIN_LIST VIDEOIO_ENABLE_PLUGINS VIDEOIO_ENABLE_STRICT_PLUGIN_CHECK)

ocv_add_module(videoio opencv_imgproc opencv_imgcodecs WRAP java python)

set(videoio_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/precomp.hpp)
Expand All @@ -9,6 +14,7 @@ set(videoio_srcs
"${CMAKE_CURRENT_LIST_DIR}/src/cap_images.cpp"
"${CMAKE_CURRENT_LIST_DIR}/src/cap_mjpeg_encoder.cpp"
"${CMAKE_CURRENT_LIST_DIR}/src/cap_mjpeg_decoder.cpp"
"${CMAKE_CURRENT_LIST_DIR}/src/backend.cpp"
"${CMAKE_CURRENT_LIST_DIR}/src/container_avi.cpp")

file(GLOB videoio_ext_hdrs
Expand Down Expand Up @@ -86,9 +92,15 @@ if(TARGET ocv.3rdparty.dc1394_2)
list(APPEND tgts ocv.3rdparty.dc1394_2)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/cmake/plugin.cmake)

if(TARGET ocv.3rdparty.gstreamer)
list(APPEND videoio_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_gstreamer.cpp)
list(APPEND tgts ocv.3rdparty.gstreamer)
if("gstreamer" IN_LIST VIDEOIO_PLUGIN_LIST)
ocv_create_builtin_videoio_plugin("opencv_videoio_gstreamer" ocv.3rdparty.gstreamer "cap_gstreamer.cpp")
else()
list(APPEND videoio_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_gstreamer.cpp)
list(APPEND tgts ocv.3rdparty.gstreamer)
endif()
endif()

if(TARGET ocv.3rdparty.v4l)
Expand All @@ -107,9 +119,13 @@ if(TARGET ocv.3rdparty.ximea)
endif()

if(TARGET ocv.3rdparty.ffmpeg)
list(APPEND videoio_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/cap_ffmpeg_impl.hpp)
list(APPEND videoio_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_ffmpeg.cpp)
list(APPEND tgts ocv.3rdparty.ffmpeg)
if("ffmpeg" IN_LIST VIDEOIO_PLUGIN_LIST)
ocv_create_builtin_videoio_plugin("opencv_videoio_ffmpeg" ocv.3rdparty.ffmpeg "cap_ffmpeg.cpp")
else()
list(APPEND videoio_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/cap_ffmpeg_impl.hpp)
list(APPEND videoio_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_ffmpeg.cpp)
list(APPEND tgts ocv.3rdparty.ffmpeg)
endif()
endif()

if(TARGET ocv.3rdparty.pvapi)
Expand Down Expand Up @@ -155,6 +171,14 @@ ocv_create_module()
ocv_add_accuracy_tests(${tgts})
ocv_add_perf_tests(${tgts})

if(VIDEOIO_ENABLE_PLUGINS)
ocv_target_compile_definitions(${the_module} PRIVATE ENABLE_PLUGINS)
endif()

if(VIDEOIO_ENABLE_STRICT_PLUGIN_CHECK)
ocv_target_compile_definitions(${the_module} PRIVATE STRICT_PLUGIN_CHECK)
endif()

ocv_target_link_libraries(${the_module} LINK_PRIVATE ${tgts})

# copy FFmpeg dll to the output folder
Expand Down
5 changes: 2 additions & 3 deletions modules/videoio/cmake/detect_ffmpeg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ if(NOT HAVE_FFMPEG AND PKG_CONFIG_FOUND)
if(FFMPEG_libavresample_FOUND)
list(APPEND FFMPEG_LIBRARIES ${FFMPEG_libavresample_LIBRARIES})
endif()

# rewrite libraries to absolute paths
foreach(lib ${FFMPEG_LIBRARIES})
find_library(FFMPEG_ABSOLUTE_${lib} "${lib}" PATHS "${FFMPEG_lib${lib}_LIBDIR}" NO_DEFAULT_PATH)
find_library(FFMPEG_ABSOLUTE_${lib} "${lib}" PATHS "${FFMPEG_lib${lib}_LIBDIR}" "${FFMPEG_LIBRARY_DIRS}" NO_DEFAULT_PATH)
if(FFMPEG_ABSOLUTE_${lib})
list(APPEND ffmpeg_abs_libs "${FFMPEG_ABSOLUTE_${lib}}")
else()
Expand All @@ -49,7 +48,7 @@ endif()

#==================================

if(HAVE_FFMPEG AND NOT HAVE_FFMPEG_WRAPPER)
if(HAVE_FFMPEG AND NOT HAVE_FFMPEG_WRAPPER AND NOT OPENCV_FFMPEG_SKIP_BUILD_CHECK)
try_compile(__VALID_FFMPEG
"${OpenCV_BINARY_DIR}"
"${OpenCV_SOURCE_DIR}/cmake/checks/ffmpeg_test.cpp"
Expand Down
2 changes: 2 additions & 0 deletions modules/videoio/cmake/init.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function(ocv_add_external_target name inc link def)
endif()
endfunction()

include(FindPkgConfig)

add_backend("ffmpeg" WITH_FFMPEG)
add_backend("gstreamer" WITH_GSTREAMER)
add_backend("v4l" WITH_V4L)
Expand Down
99 changes: 99 additions & 0 deletions modules/videoio/cmake/plugin.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#=============================================
# build with OpenCV

function(ocv_create_builtin_videoio_plugin name target videoio_src_file)

if(NOT TARGET ${target})
message(FATAL_ERROR "${target} does not exist!")
endif()
if(NOT OpenCV_SOURCE_DIR)
message(FATAL_ERROR "OpenCV_SOURCE_DIR must be set to build the plugin!")
endif()

add_library(${name} MODULE
"${CMAKE_CURRENT_LIST_DIR}/src/${videoio_src_file}"
"${CMAKE_CURRENT_LIST_DIR}/src/plugin_api.cpp")
target_include_directories(${name} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_compile_definitions(${name} PRIVATE BUILD_PLUGIN)
target_link_libraries(${name} PRIVATE ${target})

foreach(mod opencv_videoio opencv_core opencv_imgproc opencv_imgcodecs)
target_link_libraries(${name} PRIVATE ${mod})
target_include_directories(${name} PRIVATE "${OPENCV_MODULE_${mod}_LOCATION}/include")
endforeach()

set_target_properties(${name} PROPERTIES
CXX_STANDARD 11
CXX_VISIBILITY_PRESET hidden
)
install(TARGETS ${name} LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT plugins OPTIONAL)

endfunction()

#=============================================
# standalone build

function(ocv_create_videoio_plugin default_name target target_desc videoio_src_file)

set(OPENCV_PLUGIN_NAME ${default_name} CACHE STRING "")
set(OPENCV_PLUGIN_DESTINATION "" CACHE PATH "")
project(${OPENCV_PLUGIN_NAME} LANGUAGES CXX)

set(BUILD_SHARED_LIBS ON CACHE BOOL "")
if(NOT BUILD_SHARED_LIBS)
message(FATAL_ERROR "Static plugin build does not make sense")
endif()

if(NOT OpenCV_SOURCE_DIR)
message(FATAL_ERROR "OpenCV_SOURCE_DIR must be set to build the plugin!")
endif()

include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/init.cmake")

if(NOT TARGET ${target})
message(FATAL_ERROR "${target_desc} was not found!")
endif()

set(modules_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../..")
set(videoio_ROOT "${modules_ROOT}/videoio")
set(core_ROOT "${modules_ROOT}/core")
set(imgproc_ROOT "${modules_ROOT}/imgproc")
set(imgcodecs_ROOT "${modules_ROOT}/imgcodecs")

add_library(${OPENCV_PLUGIN_NAME} MODULE "${videoio_ROOT}/src/${videoio_src_file}" "${videoio_ROOT}/src/plugin_api.cpp")
target_include_directories(${OPENCV_PLUGIN_NAME} PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}"
"${videoio_ROOT}/src"
"${videoio_ROOT}/include"
"${core_ROOT}/include"
"${imgproc_ROOT}/include"
"${imgcodecs_ROOT}/include"
)
target_compile_definitions(${OPENCV_PLUGIN_NAME} PRIVATE BUILD_PLUGIN)

# Fixes for build
target_compile_definitions(${OPENCV_PLUGIN_NAME} PRIVATE __OPENCV_BUILD)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/cvconfig.h" "#pragma once")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/cv_cpu_config.h" "#pragma once")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/opencv2/opencv_modules.hpp" "#pragma once")

target_link_libraries(${OPENCV_PLUGIN_NAME} PRIVATE ${target})
set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES
CXX_STANDARD 11
CXX_VISIBILITY_PRESET hidden
)

# Hack for Windows
if(WIN32)
find_package(OpenCV REQUIRED core imgproc videoio)
target_link_libraries(${OPENCV_PLUGIN_NAME} ${OpenCV_LIBS})
endif()

if(OPENCV_PLUGIN_DESTINATION)
set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${OPENCV_PLUGIN_DESTINATION}")
message(STATUS "Output destination: ${OPENCV_PLUGIN_DESTINATION}")
endif()

message(STATUS "Library name: ${OPENCV_PLUGIN_NAME}")

endfunction()
10 changes: 10 additions & 0 deletions modules/videoio/include/opencv2/videoio/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ CV_EXPORTS_W std::vector<VideoCaptureAPIs> getStreamBackends();
/** @brief Returns list of available backends which works via `cv::VideoWriter()` */
CV_EXPORTS_W std::vector<VideoCaptureAPIs> getWriterBackends();

enum Capability
{
Read,
Write,
ReadWrite
};

/** @brief Returns true if backend is available */
CV_EXPORTS bool hasBackend(VideoCaptureAPIs api, Capability cap = ReadWrite);

//! @}
}} // namespace

Expand Down
81 changes: 81 additions & 0 deletions modules/videoio/misc/build_plugins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

set -e

if [ -z $1 ] ; then
echo "<script> <destination directory>"
exit 1
fi

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
OCV="$( cd "${DIR}/../../.." >/dev/null 2>&1 && pwd )"
mkdir -p "${1}" # Docker creates non-existed mounts with 'root' owner, lets ensure that dir exists under the current user to avoid "Permission denied" problem
DST="$( cd "$1" >/dev/null 2>&1 && pwd )"
CFG=$2

do_build()
{
TAG=$1
D=$2
F=$3
shift 3
docker build \
--build-arg http_proxy \
--build-arg https_proxy \
$@ \
-t $TAG \
-f "${D}/${F}" \
"${D}"
}

do_run()
{
TAG=$1
shift 1
docker run \
-it \
--rm \
-v "${OCV}":/opencv:ro \
-v "${DST}":/dst \
-e CFG=$CFG \
--user $(id -u):$(id -g) \
$TAG \
$@
}

build_gstreamer()
{
TAG=opencv_gstreamer_builder
do_build $TAG "${DIR}/plugin_gstreamer" Dockerfile
do_run $TAG /opencv/modules/videoio/misc/plugin_gstreamer/build.sh /dst $CFG
}

build_ffmpeg_ubuntu()
{
VER=$1
TAG=opencv_ffmpeg_ubuntu_builder:${VER}
do_build $TAG "${DIR}/plugin_ffmpeg" Dockerfile-ubuntu --build-arg VER=${VER}
do_run $TAG /opencv/modules/videoio/misc/plugin_ffmpeg/build-ubuntu.sh /dst ${VER} ${CFG}
}

build_ffmpeg()
{
VER=$1
TAG=opencv_ffmpeg_builder:${VER}
ARCHIVE="${DIR}/plugin_ffmpeg/ffmpeg-${VER}.tar.xz"
if [ ! -f "${ARCHIVE}" ] ; then
wget https://www.ffmpeg.org/releases/ffmpeg-${VER}.tar.xz -O "${ARCHIVE}"
fi
do_build $TAG "${DIR}/plugin_ffmpeg" Dockerfile-ffmpeg --build-arg VER=${VER}
do_run $TAG /opencv/modules/videoio/misc/plugin_ffmpeg/build-standalone.sh /dst ${VER} ${CFG}
}

echo "OpenCV: ${OCV}"
echo "Destination: ${DST}"

build_gstreamer
build_ffmpeg_ubuntu 18.04
build_ffmpeg_ubuntu 16.04
build_ffmpeg 4.1
build_ffmpeg 3.4.5
build_ffmpeg 2.8.15
1 change: 1 addition & 0 deletions modules/videoio/misc/plugin_ffmpeg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.tar.xz
13 changes: 13 additions & 0 deletions modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.5)

set(OpenCV_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../..")
set(WITH_FFMPEG ON)
set(OPENCV_FFMPEG_SKIP_BUILD_CHECK ON)
include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/plugin.cmake")
ocv_create_videoio_plugin("opencv_videoio_ffmpeg" "ocv.3rdparty.ffmpeg" "FFmpeg" "cap_ffmpeg.cpp")

message(STATUS "FFMPEG_libavcodec_VERSION=${FFMPEG_libavcodec_VERSION}")
message(STATUS "FFMPEG_libavformat_VERSION=${FFMPEG_libavformat_VERSION}")
message(STATUS "FFMPEG_libavutil_VERSION=${FFMPEG_libavutil_VERSION}")
message(STATUS "FFMPEG_libswscale_VERSION=${FFMPEG_libswscale_VERSION}")
message(STATUS "FFMPEG_libavresample_VERSION=${FFMPEG_libavresample_VERSION}")
Loading

0 comments on commit 4188e8a

Please sign in to comment.