__deps__ := BZIP2 ZLIB FDKAAC LIBDAV1D LIBVPX LAME LIBOPUS LIBSPEEX XZ
ifeq (1,$(FEATURE.qsv))
__deps__ += LIBMFX
endif
ifeq (1,$(FEATURE.vce))
__deps__ += AMF
endif
ifeq (1,$(FEATURE.nvenc))
__deps__ += NVENC
endif

$(eval $(call import.MODULE.defs,FFMPEG,ffmpeg,$(__deps__)))
$(eval $(call import.CONTRIB.defs,FFMPEG))

FFMPEG.FETCH.url    = https://download.handbrake.fr/handbrake/contrib/ffmpeg-4.2.1.tar.bz2
FFMPEG.FETCH.url   += https://ffmpeg.org/releases/ffmpeg-4.2.1.tar.bz2
FFMPEG.FETCH.sha256 = 682a9fa3f6864d7f0dbf224f86b129e337bc60286e0d00dffcd710998d521624

FFMPEG.CONFIGURE.deps  =
FFMPEG.CONFIGURE.host  =
FFMPEG.CONFIGURE.build =
FFMPEG.CONFIGURE.env.LOCAL_PATH = PATH="$(call fn.ABSOLUTE,$(CONTRIB.build/)bin):$(PATH)"
FFMPEG.BUILD.env                = PATH="$(call fn.ABSOLUTE,$(CONTRIB.build/)bin):$(PATH)"

ifneq (,$(filter $(HOST.system),freebsd netbsd))
	FFMPEG.CONFIGURE.env += CFLAGS=-I$(LOCALBASE)/include
	FFMPEG.CONFIGURE.env += LDFLAGS=-L$(LOCALBASE)/lib
endif

FFMPEG.CONFIGURE.extra = \
    --enable-gpl \
    --disable-doc \
    --disable-programs \
    --disable-avdevice \
    --disable-muxers \
    --disable-network \
    --disable-hwaccels \
    --disable-vdpau \
    --disable-encoders \
    --enable-libmp3lame \
    --enable-encoder=aac \
    --enable-encoder=ac3 \
    --enable-encoder=eac3 \
    --enable-encoder=flac \
    --enable-encoder=mpeg2video \
    --enable-encoder=mpeg4 \
    --enable-encoder=libmp3lame \
    --enable-libopus \
    --enable-encoder=libopus \
    --enable-libspeex \
    --disable-encoder=libspeex \
    --enable-decoder=libspeex \
    --enable-libvpx \
    --disable-decoder=libvpx_* \
    --enable-encoder=libvpx_vp8 \
    --enable-encoder=libvpx_vp9 \
    --disable-decoder=*_crystalhd \
    --enable-libdav1d \
    --enable-decoder=libdav1d \
    --cc="$(FFMPEG.GCC.gcc)" \
    --extra-ldflags="$(call fn.ARGS,FFMPEG.GCC,*archs *sysroot *minver ?extra) -L$(call fn.ABSOLUTE,$(CONTRIB.build/)lib)"

ifeq (1-linux,$(FEATURE.qsv)-$(HOST.system))
    FFMPEG.CONFIGURE.extra += --enable-vaapi
    FFMPEG.CONFIGURE.extra += --disable-xlib
else
    FFMPEG.CONFIGURE.extra += --disable-vaapi
endif

ifeq (1,$(FEATURE.fdk_aac))
FFMPEG.CONFIGURE.extra += \
    --enable-nonfree \
    --enable-libfdk-aac \
    --enable-encoder=libfdk_aac
endif

FFMPEG.CONFIGURE.extra += \
    --enable-muxer=matroska \
    --enable-muxer=webm \
    --enable-muxer=mov \
    --enable-muxer=mp4 \
    --enable-muxer=psp \
    --enable-muxer=ipod

ifeq (darwin,$(HOST.system))
    FFMPEG.CONFIGURE.extra += --disable-audiotoolbox --disable-coreimage \
        --enable-encoder=h264_videotoolbox \
        --enable-encoder=hevc_videotoolbox

    ifeq (x86_64,$(HOST.arch))
        FFMPEG.CONFIGURE.extra += --arch=x86_64
    endif
endif

## check against tuple: B-SYSTEM where B is { 0 | 1 } for cross-compiling flag
ifeq (0-cygwin,$(HOST.cross)-$(HOST.system))
    FFMPEG.GCC.args.extra += -fno-common
else ifeq (1-mingw,$(HOST.cross)-$(HOST.system))
    FFMPEG.CONFIGURE.extra += \
        --target-os=mingw32 \
        --arch=$(HOST.machine) \
        --cross-prefix=$(HOST.cross.prefix) \
        --pkg-config=$(PKGCONFIG.exe)
    FFMPEG.GCC.args.extra += -fno-common
endif

ifneq (none,$(FFMPEG.GCC.g))
    FFMPEG.CONFIGURE.extra += --enable-debug
    ifeq (max,$(FFMPEG.GCC.g))
        FFMPEG.CONFIGURE.extra += --extra-cflags="$(call fn.ARGS,FFMPEG.GCC,*archs *sysroot *minver ?extra) -I$(call fn.ABSOLUTE,$(CONTRIB.build/)include) -DDEBUG"
    else
        FFMPEG.CONFIGURE.extra += --extra-cflags="$(call fn.ARGS,FFMPEG.GCC,*archs *sysroot *minver ?extra) -I$(call fn.ABSOLUTE,$(CONTRIB.build/)include) -DNDEBUG"
    endif
else
    FFMPEG.CONFIGURE.extra += --disable-debug
    FFMPEG.CONFIGURE.extra += --extra-cflags="$(call fn.ARGS,FFMPEG.GCC,*archs *sysroot *minver ?extra) -I$(call fn.ABSOLUTE,$(CONTRIB.build/)include) -DNDEBUG"
endif

ifeq (none,$(FFMPEG.GCC.O))
    FFMPEG.CONFIGURE.extra += --disable-optimizations
endif

ifeq (1,$(FEATURE.qsv))
  FFMPEG.CONFIGURE.extra += --enable-libmfx
endif

ifeq (1,$(FEATURE.vce))
  FFMPEG.CONFIGURE.extra += --enable-amf --enable-encoder=h264_amf --enable-encoder=hevc_amf 
endif

ifeq (1,$(FEATURE.nvenc))
FFMPEG.CONFIGURE.extra += \
     --enable-nvenc \
     --enable-encoder=h264_nvenc \
     --enable-encoder=hevc_nvenc
endif

## enable compile verbosity
FFMPEG.BUILD.extra = V=1

## For POSIX.1-2001
ifeq (solaris,$(HOST.system))
    FFMPEG.CONFIGURE.extra += --extra-libs=/usr/lib/values-xpg6.o
endif

## MPC8 Doesn't compile on SPARC
ifeq (solaris-sparc,$(HOST.system)-$(HOST.machine))
    FFMPEG.CONFIGURE.extra += --disable-demuxer=mpc8
endif
