Skip to content

Commit

Permalink
Merge pull request #621 from TheCycoONE/libavresample
Browse files Browse the repository at this point in the history
Support playing movies with libav and fix ffmpeg audio stutter
  • Loading branch information
Lego3 committed Feb 4, 2015
2 parents bbbba7b + 5710f1c commit 19e2ad1
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 57 deletions.
145 changes: 145 additions & 0 deletions CMake/FindLibAV.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# vim: ts=2 sw=2
# - Try to find the required libav components(default: AVFORMAT, AVUTIL, AVCODEC)
#
# Once done this will define
# LIBAV_FOUND - System has the all required components.
# LIBAV_INCLUDE_DIRS - Include directory necessary for using the required components headers.
# LIBAV_LIBRARIES - Link these to use the required libav components.
# LIBAV_DEFINITIONS - Compiler switches required for using the required libav components.
#
# For each of the components it will additionally set.
# - AVCODEC
# - AVDEVICE
# - AVFILTER
# - AVFORMAT
# - AVRESAMPLE
# - AVUTIL
# - SWSCALE
# the following variables will be defined
# <component>_FOUND - System has <component>
# <component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
# <component>_LIBRARIES - Link these to use <component>
# <component>_DEFINITIONS - Compiler switches required for using <component>
# <component>_VERSION - The components version
#
# Copyright (c) 2006, Matthias Kretz, <kretz@kde.org>
# Copyright (c) 2008, Alexander Neundorf, <neundorf@kde.org>
# Copyright (c) 2011, Michael Jansen, <kde@michael-jansen.biz>
# Copyright (c) 2013,2015 Stephen Baker <baker.stephen.e@gmail.com>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

include(FindPackageHandleStandardArgs)

# The default components were taken from a survey over other FindLIBAV.cmake files
if (NOT LibAV_FIND_COMPONENTS)
set(LibAV_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL)
endif ()

#
### Macro: set_component_found
#
# Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
#
macro(set_component_found _component )
if (${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
# message(STATUS " - ${_component} found.")
set(${_component}_FOUND TRUE)
else ()
# message(STATUS " - ${_component} not found.")
endif ()
endmacro()

#
### Macro: find_component
#
# Checks for the given component by invoking pkgconfig and then looking up the libraries and
# include directories.
#
macro(find_component _component _pkgconfig _library _header)

if (NOT WIN32)
# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(PC_${_component} ${_pkgconfig})
endif ()
endif (NOT WIN32)

find_path(${_component}_INCLUDE_DIRS ${_header}
HINTS
${PC_LIB${_component}_INCLUDEDIR}
${PC_LIB${_component}_INCLUDE_DIRS}
PATH_SUFFIXES
libav
)

find_library(${_component}_LIBRARIES NAMES ${_library}
HINTS
${PC_LIB${_component}_LIBDIR}
${PC_LIB${_component}_LIBRARY_DIRS}
)

set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")

set_component_found(${_component})

endmacro()


# Check for cached results. If there are skip the costly part.
if (NOT LIBAV_LIBRARIES)

# Check for all possible component.
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
find_component(AVRESAMPLE libavresample avresample libavresample/avresample.h)
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
find_component(SWSCALE libswscale swscale libswscale/swscale.h)

# Check if the required components were found and add their stuff to the LIBAV_* vars.
foreach (_component ${LibAV_FIND_COMPONENTS})
if (${_component}_FOUND)
# message(STATUS "Required component ${_component} present.")
set(LIBAV_LIBRARIES ${LIBAV_LIBRARIES} ${${_component}_LIBRARIES})
set(LIBAV_DEFINITIONS ${LIBAV_DEFINITIONS} ${${_component}_DEFINITIONS})
list(APPEND LIBAV_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
else ()
# message(STATUS "Required component ${_component} missing.")
endif ()
endforeach ()

# Build the include path with duplicates removed.
if (LIBAV_INCLUDE_DIRS)
list(REMOVE_DUPLICATES LIBAV_INCLUDE_DIRS)
endif ()

# cache the vars.
set(LIBAV_INCLUDE_DIRS ${LIBAV_INCLUDE_DIRS} CACHE STRING "The LibAV include directories." FORCE)
set(LIBAV_LIBRARIES ${LIBAV_LIBRARIES} CACHE STRING "The LibAV libraries." FORCE)
set(LIBAV_DEFINITIONS ${LIBAV_DEFINITIONS} CACHE STRING "The LibAV cflags." FORCE)

mark_as_advanced(LIBAV_INCLUDE_DIRS
LIBAV_LIBRARIES
LIBAV_DEFINITIONS)

endif ()

# Now set the noncached _FOUND vars for the components.
foreach (_component AVCODEC AVDEVICE AVFILTER AVFORMAT AVRESAMPLE AVUTIL SWSCALE)
set_component_found(${_component})
endforeach ()

# Compile the list of required vars
set(_LibAV_REQUIRED_VARS LIBAV_LIBRARIES LIBAV_INCLUDE_DIRS)
foreach (_component ${LibAV_FIND_COMPONENTS})
list(APPEND _LibAV_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
endforeach ()

# Give a nice error message if some of the required vars are missing.
find_package_handle_standard_args(LibAV DEFAULT_MSG ${_LibAV_REQUIRED_VARS})
15 changes: 13 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# - BUILD_ANIMVIEWER
# - BUILD_MAPEDITOR
# - WITH_LUAJIT : Whether to use LuaJIT 2 instead of Lua51 (default is LuaJIT 2)
# - WITH_LIBAV : Whether to use LibAV (as opposed to FFMEPG) when building movies

PROJECT(CorsixTH_Top_Level)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
Expand All @@ -36,6 +37,7 @@ OPTION(WITH_AUDIO "Activate Sound" ON) # enabled by default
OPTION(WITH_MOVIES "Activate in game movies" ON)
OPTION(WITH_FREETYPE2 "Enhanced Font Support" ON)
OPTION(WITH_LUAJIT "Use LuaJIT instead of Lua" ON)
OPTION(WITH_LIBAV "Use LibAV instead of FFmpeg" OFF)
OPTION(BUILD_ANIMVIEWER "Build the animation viewer as part of the build process" OFF)
OPTION(BUILD_MAPEDITOR "Build the map editor as part of the build process" OFF)

Expand All @@ -49,14 +51,23 @@ ENDIF(WITH_AUDIO)

IF(WITH_MOVIES)
IF(WITH_AUDIO)
SET(CORSIX_TH_USE_FFMPEG ON)
MESSAGE("Note: FFMPEG video is enabled (default)")
IF(WITH_LIBAV)
SET(CORSIX_TH_USE_FFMPEG OFF)
SET(CORSIX_TH_USE_LIBAV ON)
MESSAGE("Note: LibAV video is enabled")
ELSE()
SET(CORSIX_TH_USE_FFMPEG ON)
SET(CORSIX_TH_USE_LIBAV OFF)
MESSAGE("Note: FFMPEG video is enabled (default)")
ENDIF(WITH_LIBAV)
ELSE()
SET(CORSIX_TH_USE_FFMPEG OFF)
SET(CORSIX_TH_USE_LIBAV OFF)
MESSAGE("Note: FFMPEG video disabled since it requires SDL audio.")
ENDIF(WITH_AUDIO)
ELSE()
SET(CORSIX_TH_USE_FFMPEG OFF)
SET(CORSIX_TH_USE_LIBAV OFF)
MESSAGE("Note: FFMPEG video is disabled")
ENDIF(WITH_MOVIES)

Expand Down
16 changes: 15 additions & 1 deletion CorsixTH/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ message( STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}" )

# Find FFMPEG
IF(CORSIX_TH_USE_FFMPEG)
FIND_PACKAGE(FFmpeg COMPONENTS AVFORMAT AVCODEC AVUTIL SWSCALE SWRESAMPLE REQUIRED)
FIND_PACKAGE(FFmpeg COMPONENTS AVFORMAT AVCODEC AVUTIL SWSCALE SWRESAMPLE REQUIRED)
IF(FFMPEG_FOUND)
TARGET_LINK_LIBRARIES(CorsixTH ${FFMPEG_LIBRARIES})
INCLUDE_DIRECTORIES(${FFMPEG_INCLUDE_DIRS})
Expand All @@ -150,6 +150,20 @@ IF(CORSIX_TH_USE_FFMPEG)
ENDIF(FFMPEG_FOUND)
ENDIF(CORSIX_TH_USE_FFMPEG)

IF(CORSIX_TH_USE_LIBAV)
FIND_PACKAGE(LibAV COMPONENTS AVFORMAT AVCODEC AVRESAMPLE AVUTIL SWSCALE REQUIRED)
IF(LIBAV_FOUND)
TARGET_LINK_LIBRARIES(CorsixTH ${LIBAV_LIBRARIES})
INCLUDE_DIRECTORIES(${LIBAV_INCLUDE_DIRS})
IF(APPLE)
TARGET_LINK_LIBRARIES(CorsixTH libz.dylib)
ENDIF()
message(" LibAV found")
ELSE(LIBAV_FOUND)
message("Error: LibAV library not found, even though it was selected to be included")
ENDIF(LIBAV_FOUND)
ENDIF(CORSIX_TH_USE_LIBAV)

# Find Freetype2
IF(CORSIX_TH_USE_FREETYPE2)
FIND_PACKAGE(Freetype REQUIRED)
Expand Down
10 changes: 6 additions & 4 deletions CorsixTH/Src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ SOFTWARE.
#cmakedefine CORSIX_TH_USE_SDL_MIXER

/** Movie options **/
// FFMPEG is used for in game movies. If this library is not present on your
// system, then you can comment out the next line and the game will not have
// movies.
// FFMPEG or LibAV are used for in game movies.
// If this library is not present on your system, then you can comment out the
// next line and the game will not have movies.
#cmakedefine CORSIX_TH_USE_FFMPEG

#ifndef CORSIX_TH_USE_FFMPEG
#cmakedefine CORSIX_TH_USE_LIBAV
#endif
/** Font options **/
// FreeType2 can be used for font support beyond the CP437 bitmap fonts which
// come with Theme Hospital. It must be used if translations like Russian or
Expand Down

0 comments on commit 19e2ad1

Please sign in to comment.