detect_ffmpeg.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # --- FFMPEG ---
  2. OCV_OPTION(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE "Include FFMPEG/libavdevice library support." ON
  3. VISIBLE_IF WITH_FFMPEG)
  4. if(NOT HAVE_FFMPEG AND OPENCV_FFMPEG_USE_FIND_PACKAGE)
  5. if(OPENCV_FFMPEG_USE_FIND_PACKAGE STREQUAL "1" OR OPENCV_FFMPEG_USE_FIND_PACKAGE STREQUAL "ON")
  6. set(OPENCV_FFMPEG_USE_FIND_PACKAGE "FFMPEG")
  7. endif()
  8. find_package(${OPENCV_FFMPEG_USE_FIND_PACKAGE}) # Required components: AVCODEC AVFORMAT AVUTIL SWSCALE
  9. if(FFMPEG_FOUND OR FFmpeg_FOUND)
  10. set(HAVE_FFMPEG TRUE)
  11. endif()
  12. endif()
  13. if(NOT HAVE_FFMPEG AND WIN32 AND NOT ARM AND NOT AARCH64 AND NOT OPENCV_FFMPEG_SKIP_DOWNLOAD)
  14. include("${OpenCV_SOURCE_DIR}/3rdparty/ffmpeg/ffmpeg.cmake")
  15. download_win_ffmpeg(FFMPEG_CMAKE_SCRIPT)
  16. if(FFMPEG_CMAKE_SCRIPT)
  17. include("${FFMPEG_CMAKE_SCRIPT}")
  18. set(HAVE_FFMPEG TRUE)
  19. set(HAVE_FFMPEG_WRAPPER TRUE)
  20. endif()
  21. endif()
  22. set(_required_ffmpeg_libraries libavcodec libavformat libavutil libswscale)
  23. set(_used_ffmpeg_libraries ${_required_ffmpeg_libraries})
  24. if(NOT HAVE_FFMPEG AND PKG_CONFIG_FOUND)
  25. ocv_check_modules(FFMPEG libavcodec libavformat libavutil libswscale)
  26. if(FFMPEG_FOUND)
  27. if(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE)
  28. ocv_check_modules(FFMPEG_libavdevice libavdevice) # optional
  29. if(FFMPEG_libavdevice_FOUND)
  30. list(APPEND FFMPEG_LIBRARIES ${FFMPEG_libavdevice_LIBRARIES})
  31. list(APPEND _used_ffmpeg_libraries libavdevice)
  32. endif()
  33. endif()
  34. set(HAVE_FFMPEG TRUE)
  35. else()
  36. set(_missing_ffmpeg_libraries "")
  37. foreach (ffmpeg_lib ${_required_ffmpeg_libraries})
  38. if (NOT FFMPEG_${ffmpeg_lib}_FOUND)
  39. list(APPEND _missing_ffmpeg_libraries ${ffmpeg_lib})
  40. endif()
  41. endforeach ()
  42. message(STATUS "FFMPEG is disabled. Required libraries: ${_required_ffmpeg_libraries}."
  43. " Missing libraries: ${_missing_ffmpeg_libraries}")
  44. unset(_missing_ffmpeg_libraries)
  45. endif()
  46. endif()
  47. #=================================
  48. # Versions check.
  49. if(HAVE_FFMPEG AND NOT HAVE_FFMPEG_WRAPPER)
  50. set(_min_libavcodec_version 54.35.0)
  51. set(_min_libavformat_version 54.20.4)
  52. set(_min_libavutil_version 52.3.0)
  53. set(_min_libswscale_version 2.1.1)
  54. set(_min_libavdevice_version 53.2.0)
  55. foreach(ffmpeg_lib ${_used_ffmpeg_libraries})
  56. if(FFMPEG_${ffmpeg_lib}_VERSION VERSION_LESS _min_${ffmpeg_lib}_version)
  57. message(STATUS "FFMPEG is disabled. Can't find suitable ${ffmpeg_lib} library"
  58. " (minimal ${_min_${ffmpeg_lib}_version}, found ${FFMPEG_${ffmpeg_lib}_VERSION}).")
  59. set(HAVE_FFMPEG FALSE)
  60. endif()
  61. endforeach()
  62. if(NOT HAVE_FFMPEG)
  63. message(STATUS "FFMPEG libraries version check failed "
  64. "(minimal libav release 9.20, minimal FFMPEG release 1.1.16).")
  65. endif()
  66. unset(_min_libavcodec_version)
  67. unset(_min_libavformat_version)
  68. unset(_min_libavutil_version)
  69. unset(_min_libswscale_version)
  70. unset(_min_libavdevice_version)
  71. endif()
  72. #==================================
  73. if(HAVE_FFMPEG AND NOT HAVE_FFMPEG_WRAPPER AND NOT OPENCV_FFMPEG_SKIP_BUILD_CHECK)
  74. try_compile(__VALID_FFMPEG
  75. "${OpenCV_BINARY_DIR}"
  76. "${OpenCV_SOURCE_DIR}/cmake/checks/ffmpeg_test.cpp"
  77. CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${FFMPEG_INCLUDE_DIRS}"
  78. "-DLINK_LIBRARIES:STRING=${FFMPEG_LIBRARIES}"
  79. OUTPUT_VARIABLE TRY_OUT
  80. )
  81. if(NOT __VALID_FFMPEG)
  82. # message(FATAL_ERROR "FFMPEG: test check build log:\n${TRY_OUT}")
  83. message(STATUS "WARNING: Can't build ffmpeg test code")
  84. set(HAVE_FFMPEG FALSE)
  85. endif()
  86. endif()
  87. #==================================
  88. unset(_required_ffmpeg_libraries)
  89. unset(_used_ffmpeg_libraries)
  90. if(HAVE_FFMPEG_WRAPPER)
  91. ocv_add_external_target(ffmpeg "" "" "HAVE_FFMPEG_WRAPPER")
  92. elseif(HAVE_FFMPEG)
  93. if(OPENCV_FFMPEG_ENABLE_LIBAVDEVICE AND FFMPEG_libavdevice_FOUND)
  94. set(HAVE_FFMPEG_LIBAVDEVICE TRUE)
  95. ocv_add_external_target(ffmpeg "${FFMPEG_INCLUDE_DIRS}" "${FFMPEG_LIBRARIES}" "HAVE_FFMPEG;HAVE_FFMPEG_LIBAVDEVICE")
  96. else()
  97. ocv_add_external_target(ffmpeg "${FFMPEG_INCLUDE_DIRS}" "${FFMPEG_LIBRARIES}" "HAVE_FFMPEG")
  98. endif()
  99. set(__builtin_defines "")
  100. set(__builtin_include_dirs "")
  101. set(__builtin_libs "")
  102. set(__plugin_defines "")
  103. set(__plugin_include_dirs "")
  104. set(__plugin_libs "")
  105. if(HAVE_OPENCL)
  106. set(__opencl_dirs "")
  107. if(OPENCL_INCLUDE_DIRS)
  108. set(__opencl_dirs "${OPENCL_INCLUDE_DIRS}")
  109. elseif(OPENCL_INCLUDE_DIR)
  110. set(__opencl_dirs "${OPENCL_INCLUDE_DIR}")
  111. else()
  112. set(__opencl_dirs "${OpenCV_SOURCE_DIR}/3rdparty/include/opencl/1.2")
  113. endif()
  114. # extra dependencies for buildin code (OpenCL dir is required for extensions like cl_d3d11.h)
  115. # buildin HAVE_OPENCL is already defined through cvconfig.h
  116. list(APPEND __builtin_include_dirs "${__opencl_dirs}")
  117. # extra dependencies for
  118. list(APPEND __plugin_defines "HAVE_OPENCL")
  119. list(APPEND __plugin_include_dirs "${__opencl_dirs}")
  120. endif()
  121. # TODO: libva, d3d11
  122. if(__builtin_include_dirs OR __builtin_include_defines OR __builtin_include_libs)
  123. ocv_add_external_target(ffmpeg.builtin_deps "${__builtin_include_dirs}" "${__builtin_include_libs}" "${__builtin_defines}")
  124. endif()
  125. if(VIDEOIO_ENABLE_PLUGINS AND __plugin_include_dirs OR __plugin_include_defines OR __plugin_include_libs)
  126. ocv_add_external_target(ffmpeg.plugin_deps "${__plugin_include_dirs}" "${__plugin_include_libs}" "${__plugin_defines}")
  127. endif()
  128. endif()