From da4ac6b7eff2e8869567e4faaff73312f9e1ef57 Mon Sep 17 00:00:00 2001 From: Amir Hassan Date: Fri, 11 Nov 2022 23:28:02 +0100 Subject: [PATCH] Merge pull request #22706 from kallaballa:libavdevice_for_ffmpeg_v4l2 Introduce libavdevice to make v4l2 available to the ffmpeg backend * introduce libavdevice to make v4l2 available to the ffmpeg backend * downgrade the min required libavdevice version to 53.2.0 * make libavdevice optional * create OCV_OPTION OPENCV_FFMPEG_ENABLE_LIBAVDEVICE and add definition through ocv_add_external_target * move OCV_OPTION 'OPENCV_FFMPEG_ENABLE_LIBAVDEVICE' to detect_ffmpeg.cmake --- CMakeLists.txt | 3 +++ modules/videoio/cmake/detect_ffmpeg.cmake | 19 ++++++++++++++++++- .../videoio/misc/plugin_ffmpeg/CMakeLists.txt | 3 +++ modules/videoio/src/cap_ffmpeg_impl.hpp | 7 +++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aba65ba23f..d293210014 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1444,6 +1444,9 @@ if(WITH_FFMPEG OR HAVE_FFMPEG) status(" avutil:" FFMPEG_libavutil_VERSION THEN "YES (${FFMPEG_libavutil_VERSION})" ELSE NO) status(" swscale:" FFMPEG_libswscale_VERSION THEN "YES (${FFMPEG_libswscale_VERSION})" ELSE NO) status(" avresample:" FFMPEG_libavresample_VERSION THEN "YES (${FFMPEG_libavresample_VERSION})" ELSE NO) + if(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE) + status(" avdevice:" FFMPEG_libavdevice_VERSION THEN "YES (${FFMPEG_libavdevice_VERSION})" ELSE NO) + endif() endif() if(WITH_GSTREAMER OR HAVE_GSTREAMER) diff --git a/modules/videoio/cmake/detect_ffmpeg.cmake b/modules/videoio/cmake/detect_ffmpeg.cmake index c33eaf221b..aa669f36b0 100644 --- a/modules/videoio/cmake/detect_ffmpeg.cmake +++ b/modules/videoio/cmake/detect_ffmpeg.cmake @@ -1,4 +1,7 @@ # --- FFMPEG --- +OCV_OPTION(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE "Include FFMPEG/libavdevice library support." OFF + VISIBLE_IF WITH_FFMPEG) + if(NOT HAVE_FFMPEG AND OPENCV_FFMPEG_USE_FIND_PACKAGE) if(OPENCV_FFMPEG_USE_FIND_PACKAGE STREQUAL "1" OR OPENCV_FFMPEG_USE_FIND_PACKAGE STREQUAL "ON") set(OPENCV_FFMPEG_USE_FIND_PACKAGE "FFMPEG") @@ -29,6 +32,13 @@ if(NOT HAVE_FFMPEG AND PKG_CONFIG_FOUND) list(APPEND FFMPEG_LIBRARIES ${FFMPEG_libavresample_LIBRARIES}) list(APPEND _used_ffmpeg_libraries libavresample) endif() + if(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE) + ocv_check_modules(FFMPEG_libavdevice libavdevice) # optional + if(FFMPEG_libavdevice_FOUND) + list(APPEND FFMPEG_LIBRARIES ${FFMPEG_libavdevice_LIBRARIES}) + list(APPEND _used_ffmpeg_libraries libavdevice) + endif() + endif() set(HAVE_FFMPEG TRUE) else() set(_missing_ffmpeg_libraries "") @@ -51,6 +61,7 @@ if(HAVE_FFMPEG AND NOT HAVE_FFMPEG_WRAPPER) set(_min_libavutil_version 52.3.0) set(_min_libswscale_version 2.1.1) set(_min_libavresample_version 1.0.1) + set(_min_libavdevice_version 53.2.0) foreach(ffmpeg_lib ${_used_ffmpeg_libraries}) if(FFMPEG_${ffmpeg_lib}_VERSION VERSION_LESS _min_${ffmpeg_lib}_version) message(STATUS "FFMPEG is disabled. Can't find suitable ${ffmpeg_lib} library" @@ -67,6 +78,7 @@ if(HAVE_FFMPEG AND NOT HAVE_FFMPEG_WRAPPER) unset(_min_libavutil_version) unset(_min_libswscale_version) unset(_min_libavresample_version) + unset(_min_libavdevice_version) endif() #================================== @@ -93,7 +105,12 @@ unset(_used_ffmpeg_libraries) if(HAVE_FFMPEG_WRAPPER) ocv_add_external_target(ffmpeg "" "" "HAVE_FFMPEG_WRAPPER") elseif(HAVE_FFMPEG) - ocv_add_external_target(ffmpeg "${FFMPEG_INCLUDE_DIRS}" "${FFMPEG_LIBRARIES}" "HAVE_FFMPEG") + if(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE AND FFMPEG_libavdevice_FOUND) + set(HAVE_FFMPEG_LIBAVDEVICE TRUE) + ocv_add_external_target(ffmpeg "${FFMPEG_INCLUDE_DIRS}" "${FFMPEG_LIBRARIES}" "HAVE_FFMPEG;HAVE_FFMPEG_LIBAVDEVICE") + else() + ocv_add_external_target(ffmpeg "${FFMPEG_INCLUDE_DIRS}" "${FFMPEG_LIBRARIES}" "HAVE_FFMPEG") + endif() set(__builtin_defines "") set(__builtin_include_dirs "") set(__builtin_libs "") diff --git a/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt b/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt index ebe388a886..204a425b17 100644 --- a/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt +++ b/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt @@ -16,3 +16,6 @@ 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}") +if(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE) + message(STATUS "FFMPEG_libavdevice_VERSION=${FFMPEG_libavdevice_VERSION}") +endif() diff --git a/modules/videoio/src/cap_ffmpeg_impl.hpp b/modules/videoio/src/cap_ffmpeg_impl.hpp index cf289f6bf3..600bcee407 100644 --- a/modules/videoio/src/cap_ffmpeg_impl.hpp +++ b/modules/videoio/src/cap_ffmpeg_impl.hpp @@ -95,6 +95,9 @@ extern "C" { #include #include +#ifdef HAVE_FFMPEG_LIBAVDEVICE +#include +#endif // https://github.com/FFmpeg/FFmpeg/blob/b6af56c034759b81985f8ea094e41cbd5f7fecfb/doc/APIchanges#L602-L605 #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(58, 9, 100) @@ -625,6 +628,10 @@ struct CvCapture_FFMPEG void CvCapture_FFMPEG::init() { +#ifdef HAVE_FFMPEG_LIBAVDEVICE + //libavdevice is available, so let's register all input and output devices (e.g v4l2) + avdevice_register_all(); +#endif ic = 0; video_stream = -1; video_st = 0;