detect_ffmpeg.cmake 4.9 KB

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