Merge pull request #22706 from kallaballa:libavdevice_for_ffmpeg_v4l2
authorAmir Hassan <amir@viel-zu.org>
Fri, 11 Nov 2022 22:28:02 +0000 (23:28 +0100)
committerGitHub <noreply@github.com>
Fri, 11 Nov 2022 22:28:02 +0000 (22:28 +0000)
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
modules/videoio/cmake/detect_ffmpeg.cmake
modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt
modules/videoio/src/cap_ffmpeg_impl.hpp

index aba65ba..d293210 100644 (file)
@@ -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)
index c33eaf2..aa669f3 100644 (file)
@@ -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 "")
index ebe388a..204a425 100644 (file)
@@ -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()
index cf289f6..600bcee 100644 (file)
@@ -95,6 +95,9 @@ extern "C" {
 
 #include <libavcodec/avcodec.h>
 #include <libswscale/swscale.h>
+#ifdef HAVE_FFMPEG_LIBAVDEVICE
+#include <libavdevice/avdevice.h>
+#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;