2 # - Try to find the required ffmpeg components(default: AVFORMAT, AVUTIL, AVCODEC)
4 # Once done this will define
5 # FFMPEG_FOUND - System has the all required components.
6 # FFMPEG_INCLUDE_DIRS - Include directory necessary for using the required components headers.
7 # FFMPEG_LIBRARIES - Link these to use the required ffmpeg components.
8 # FFMPEG_DEFINITIONS - Compiler switches required for using the required ffmpeg components.
10 # For each of the components it will additionaly set.
18 # the following variables will be defined
19 # <component>_FOUND - System has <component>
20 # <component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
21 # <component>_LIBRARIES - Link these to use <component>
22 # <component>_DEFINITIONS - Compiler switches required for using <component>
23 # <component>_VERSION - The components version
25 # Copyright (c) 2006, Matthias Kretz, <kretz@kde.org>
26 # Copyright (c) 2008, Alexander Neundorf, <neundorf@kde.org>
27 # Copyright (c) 2011, Michael Jansen, <kde@michael-jansen.biz>
29 # Redistribution and use is allowed according to the terms of the BSD license.
31 include(FindPackageHandleStandardArgs)
33 if(NOT FFmpeg_FIND_COMPONENTS)
34 set(FFmpeg_FIND_COMPONENTS AVFORMAT AVCODEC AVUTIL)
38 ### Macro: set_component_found
40 # Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
42 macro(set_component_found _component)
43 if(${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
44 # message(STATUS " - ${_component} found.")
45 set(${_component}_FOUND TRUE)
47 # message(STATUS " - ${_component} not found.")
52 ### Macro: find_component
54 # Checks for the given component by invoking pkgconfig and then looking up the libraries and
55 # include directories.
57 macro(find_component _component _pkgconfig _library _header)
59 # use pkg-config to get the directories and then use these values
60 # in the FIND_PATH() and FIND_LIBRARY() calls
61 find_package(PkgConfig)
63 pkg_check_modules(PC_${_component} ${_pkgconfig})
67 find_path(${_component}_INCLUDE_DIRS ${_header}
70 ${PC_LIB${_component}_INCLUDEDIR}
71 ${PC_LIB${_component}_INCLUDE_DIRS}
76 find_library(${_component}_LIBRARIES NAMES ${_library}
79 ${PC_LIB${_component}_LIBDIR}
80 ${PC_LIB${_component}_LIBRARY_DIRS}
83 STRING(REGEX REPLACE "/.*" "/version.h" _ver_header ${_header})
84 if(EXISTS "${${_component}_INCLUDE_DIRS}/${_ver_header}")
85 file(STRINGS "${${_component}_INCLUDE_DIRS}/${_ver_header}" version_str REGEX "^#define[\t ]+LIB${_component}_VERSION_M.*")
87 string(REGEX REPLACE "^.*LIB${_component}_VERSION_MAJOR[\t ]+([0-9]*).*$" "\\1" version_maj "${version_str}")
88 string(REGEX REPLACE "^.*LIB${_component}_VERSION_MINOR[\t ]+([0-9]*).*$" "\\1" version_min "${version_str}")
89 string(REGEX REPLACE "^.*LIB${_component}_VERSION_MICRO[\t ]+([0-9]*).*$" "\\1" version_mic "${version_str}")
92 set(${_component}_VERSION "${version_maj}.${version_min}.${version_mic}" CACHE STRING "The ${_component} version number.")
96 endif(EXISTS "${${_component}_INCLUDE_DIRS}/${_ver_header}")
97 set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
98 set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
100 set_component_found(${_component})
103 ${_component}_INCLUDE_DIRS
104 ${_component}_LIBRARIES
105 ${_component}_DEFINITIONS
106 ${_component}_VERSION)
110 set(FFMPEGSDK $ENV{FFMPEG_HOME})
112 set(FFMPEGSDK_INC "${FFMPEGSDK}/include")
113 set(FFMPEGSDK_LIB "${FFMPEGSDK}/lib")
116 # Check for all possible components.
117 find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
118 find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
119 find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
120 find_component(AVUTIL libavutil avutil libavutil/avutil.h)
121 find_component(SWSCALE libswscale swscale libswscale/swscale.h)
122 find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
123 find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
125 # Check if the required components were found and add their stuff to the FFMPEG_* vars.
126 foreach(_component ${FFmpeg_FIND_COMPONENTS})
127 if(${_component}_FOUND)
128 # message(STATUS "Required component ${_component} present.")
129 set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARIES})
130 set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
131 list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
133 # message(STATUS "Required component ${_component} missing.")
137 # Add libz if it exists (needed for static ffmpeg builds)
138 find_library(_FFmpeg_HAVE_LIBZ NAMES z)
139 if(_FFmpeg_HAVE_LIBZ)
140 set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${_FFmpeg_HAVE_LIBZ})
143 # Build the include path and library list with duplicates removed.
144 if(FFMPEG_INCLUDE_DIRS)
145 list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
149 list(REMOVE_DUPLICATES FFMPEG_LIBRARIES)
153 set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
154 set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
155 set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
157 mark_as_advanced(FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARIES FFMPEG_DEFINITIONS)
159 # Now set the noncached _FOUND vars for the components.
160 foreach(_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWRESAMPLE SWSCALE)
161 set_component_found(${_component})
164 # Compile the list of required vars
165 set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)
166 foreach(_component ${FFmpeg_FIND_COMPONENTS})
167 list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
170 # Give a nice error message if some of the required vars are missing.
171 find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})