# - Try to find FFMPEG
# Once done this will define
#  
#  FFMPEG_FOUND		 - system has FFMPEG
#  FFMPEG_SWSCALE_FOUND	 - FFMPEG also has SWSCALE
#  FFMPEG_INCLUDE_DIRS	 - the include directories
#  FFMPEG_LIBRARIES	 - link these to use FFMPEG
#   

SET( FFMPEG_HEADERS libavformat/avformat.h libavcodec/avcodec.h libavutil/avutil.h )
SET( FFMPEG_SWSCALE_HEADERS libswscale/swscale.h )
SET( FFMPEG_PATH_SUFFIX ffmpeg )
SET( FFMPEG_AVFORMAT_INCLUDE_DIR_TEST TRUE)

#MESSAGE('FFMPEGDIR')
#MESSAGE(${FFMPEGDIR})
#MESSAGE(${FFMPEG_INCLUDE_DIRS})
if( WIN32 )
   SET( FFMPEG_LIBRARY_DIRS $ENV{FFMPEGDIR}\\lib $ENV{EXTERNLIBS}\\ffmpeg\\lib )
   SET( FFMPEG_INCLUDE_DIRS $ENV{FFMPEGDIR}\\include $ENV{EXTERNLIBS}\\ffmpeg\\include)
   
  FIND_LIBRARY(FFMPEG_SWSCALE_LIBRARY NAMES swscale-0 swscale
  HINTS
     $ENV{FFMPEGDIR}
  PATH_SUFFIXES lib64 lib
  PATHS
    $ENV{EXTERNLIBS}/ffmpeg
    /sw
    /opt/local
    /opt/csw
    /opt
    /usr/freeware
    DOC "FFMPEG SWSCALE - Library"
  )
  FIND_LIBRARY(FFMPEG_AVFORMAT_LIBRARY NAMES avformat-52 avformat
  HINTS
     $ENV{FFMPEGDIR}
  PATH_SUFFIXES lib64 lib
  PATHS
    $ENV{EXTERNLIBS}/ffmpeg
    /sw
    /opt/local
    /opt/csw
    /opt
    /usr/freeware
    DOC "FFMPEG AVFORMAT - Library"
  )
  
  FIND_LIBRARY(FFMPEG_AVUTIL_LIBRARY NAMES avutil-49 avutil
  HINTS
     $ENV{FFMPEGDIR}
  PATH_SUFFIXES lib64 lib
  PATHS
    $ENV{EXTERNLIBS}/ffmpeg
    /sw
    /opt/local
    /opt/csw
    /opt
    /usr/freeware
    DOC "FFMPEG AVUTIL - Library"
  )
  
  FIND_LIBRARY(FFMPEG_AVCODEC_LIBRARY NAMES avcodec-52 avcodec-51 avcodec
  HINTS
     $ENV{FFMPEGDIR}
  PATH_SUFFIXES lib64 lib
  PATHS
    $ENV{EXTERNLIBS}/ffmpeg
    /sw
    /opt/local
    /opt/csw
    /opt
    /usr/freeware
    DOC "FFMPEG AVCODEC - Library"
  )
   
   FIND_PATH(FFMPEG_AVFORMAT_INCLUDE_DIR
      NAMES libavformat/avformat.h
      HINTS $ENV{FFMPEGDIR}\\include $ENV{EXTERNLIBS}\\ffmpeg\\include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})
   IF (FFMPEG_AVFORMAT_INCLUDE_DIR_TEST)
		ADD_DEFINITIONS("-DHAVE_FFMPEG_SEPARATE_INCLUDES")
   ENDIF (FFMPEG_AVFORMAT_INCLUDE_DIR_TEST)
   
   FIND_PATH(FFMPEG_AVCODEC_INCLUDE_DIR
      NAMES libavcodec/avcodec.h
      HINTS $ENV{FFMPEGDIR}\\include $ENV{EXTERNLIBS}\\ffmpeg\\include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})

   FIND_PATH(FFMPEG_AVUTIL_INCLUDE_DIR
      NAMES libavutil/avutil.h
      HINTS $ENV{FFMPEGDIR}\\include $ENV{EXTERNLIBS}\\ffmpeg\\include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})

   FIND_PATH(FFMPEG_SWSCALE_INCLUDE_DIR
      NAMES libswscale/swscale.h
      HINTS $ENV{FFMPEGDIR}\\include $ENV{EXTERNLIBS}\\ffmpeg\\include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})
else( WIN32 )
   SET(ENV{PKG_CONFIG_PATH} "$ENV{EXTERNLIBS}/ffmpeg/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}")
   INCLUDE(FindPkgConfig)
   if ( PKG_CONFIG_FOUND )
      pkg_check_modules( PC_FFMPEG_AVFORMAT libavformat>=52 )
      pkg_check_modules( PC_FFMPEG_AVCODEC libavcodec )
      pkg_check_modules( PC_FFMPEG_AVUTIL libavutil )
      pkg_check_modules( PC_FFMPEG_SWSCALE libswscale )
      # pkg_check_modules( PC_FFMPEG_SWRESAMPLE libswresample )
   endif ( PKG_CONFIG_FOUND )
                           
   FIND_PATH(FFMPEG_AVFORMAT_INCLUDE_DIR
      NAMES libavformat/avformat.h
      HINTS ${PC_FFMPEG_AVFORMAT_INCLUDE_DIRS} /usr/include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})
   FIND_LIBRARY(FFMPEG_AVFORMAT_LIBRARY
      NAMES avformat
      HINTS ${PC_FFMPEG_AVFORMAT_LIBRARY_DIRS})
   IF (FFMPEG_AVFORMAT_INCLUDE_DIR)
      ADD_DEFINITIONS("-DHAVE_FFMPEG_SEPARATE_INCLUDES")
   ENDIF (FFMPEG_AVFORMAT_INCLUDE_DIR)

   FIND_PATH(FFMPEG_AVCODEC_INCLUDE_DIR
      NAMES libavcodec/avcodec.h
      HINTS ${PC_FFMPEG_AVCODEC_INCLUDE_DIRS} /usr/include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})
   FIND_LIBRARY(FFMPEG_AVCODEC_LIBRARY
      NAMES avcodec
      HINTS ${PC_FFMPEG_AVCODEC_LIBRARY_DIRS})

   FIND_PATH(FFMPEG_AVUTIL_INCLUDE_DIR
      NAMES libavutil/avutil.h
      HINTS ${PC_FFMPEG_AVUTIL_INCLUDE_DIRS} /usr/include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})
   FIND_LIBRARY(FFMPEG_AVUTIL_LIBRARY
      NAMES avutil
      HINTS ${PC_FFMPEG_AVUTIL_LIBRARY_DIRS})

   FIND_PATH(FFMPEG_SWSCALE_INCLUDE_DIR
      NAMES libswscale/swscale.h
      HINTS ${PC_FFMPEG_SWSCALE_INCLUDE_DIRS} /usr/include
      PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})
   FIND_LIBRARY(FFMPEG_SWSCALE_LIBRARY
      NAMES swscale
      HINTS ${PC_FFMPEG_SWSCALE_LIBRARY_DIRS})

   # FIND_PATH(FFMPEG_SWRESAMPLE_INCLUDE_DIR
      # NAMES libswresample/swresample.h
      # HINTS ${PC_FFMPEG_SWRESAMPLE_INCLUDE_DIRS} /usr/include
      # PATH_SUFFIXES ${FFMPEG_PATH_SUFFIX})
   # FIND_LIBRARY(FFMPEG_SWRESAMPLE_LIBRARY
      # NAMES swresample
      # HINTS ${PC_FFMPEG_SWRESAMPLE_LIBRARY_DIRS})
endif( WIN32 )

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(FFMPEG_SWSCALE DEFAULT_MSG
   FFMPEG_SWSCALE_LIBRARY FFMPEG_SWSCALE_INCLUDE_DIR
   )
# FIND_PACKAGE_HANDLE_STANDARD_ARGS(FFMPEG_SWRESAMPLE DEFAULT_MSG
   # FFMPEG_SWRESAMPLE_LIBRARY FFMPEG_SWRESAMPLE_INCLUDE_DIR
   # )
FIND_PACKAGE_HANDLE_STANDARD_ARGS(FFMPEG DEFAULT_MSG
   FFMPEG_AVFORMAT_LIBRARY FFMPEG_AVFORMAT_INCLUDE_DIR
   FFMPEG_AVCODEC_LIBRARY FFMPEG_AVCODEC_INCLUDE_DIR
   FFMPEG_AVUTIL_LIBRARY FFMPEG_AVUTIL_INCLUDE_DIR
   )

IF(FFMPEG_FOUND)
   SET(FFMPEG_INCLUDE_DIRS ${FFMPEG_AVFORMAT_INCLUDE_DIR} ${FFMPEG_AVCODEC_INCLUDE_DIR} ${FFMPEG_AVUTIL_INCLUDE_DIR})
   SET(FFMPEG_LIBRARIES ${FFMPEG_AVFORMAT_LIBRARY} ${FFMPEG_AVCODEC_LIBRARY} ${FFMPEG_AVUTIL_LIBRARY})
   IF(FFMPEG_SWSCALE_FOUND)
      SET(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} ${FFMPEG_SWSCALE_INCLUDE_DIR})
      SET(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${FFMPEG_SWSCALE_LIBRARY})
   ENDIF(FFMPEG_SWSCALE_FOUND)
   IF(FFMPEG_SWRESAMPLE_FOUND)
      SET(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} ${FFMPEG_SWRESAMPLE_INCLUDE_DIR})
      SET(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${FFMPEG_SWRESAMPLE_LIBRARY})
   ENDIF(FFMPEG_SWRESAMPLE_FOUND)
ENDIF(FFMPEG_FOUND)