# Template file for 'ffmpeg'
# audacity also needs to be bumped when a new ffmpeg version bumps libavformat's soname!
pkgname=ffmpeg
version=4.3.2
revision=2
short_desc="Decoding, encoding and streaming software"
maintainer="Tuxliban Torvalds <tenshalito@gmail.com>"
license="GPL-3.0-or-later"
homepage="https://www.ffmpeg.org"
changelog="https://raw.githubusercontent.com/FFmpeg/FFmpeg/master/Changelog"
distfiles="${homepage}/releases/ffmpeg-${version}.tar.xz"
checksum=46e4e64f1dd0233cbc0934b9f1c0da676008cad34725113fb7f802cfa84ccddb

hostmakedepends="pkg-config perl yasm"
makedepends="zlib-devel bzip2-devel freetype-devel alsa-lib-devel libXfixes-devel
 libXext-devel libXvMC-devel libxcb-devel faad2-devel lame-devel libtheora-devel
 libvorbis-devel x264-devel xvidcore-devel jack-devel SDL2-devel
 libcdio-paranoia-devel librtmp-devel libmodplug-devel gnutls-devel
 speex-devel celt-devel harfbuzz-devel libass-devel opus-devel ocl-icd-devel
 libbs2b-devel libvidstab-devel $(vopt_if pulseaudio pulseaudio-devel)
 $(vopt_if x265 x265-devel) $(vopt_if vaapi libva-devel) $(vopt_if vdpau libvdpau-devel)
 $(vopt_if v4l2 v4l-utils-devel) $(vopt_if faac faac-devel) $(vopt_if fdk_aac fdk-aac-devel)
 $(vopt_if vpx libvpx-devel) $(vopt_if aom libaom-devel)
 $(vopt_if nvenc nv-codec-headers) $(vopt_if sndio sndio-devel)
 $(vopt_if dav1d libdav1d-devel) $(vopt_if zimg zimg-devel)
 $(vopt_if webp libwebp-devel) $(vopt_if sofa libmysofa-devel)"

build_options="x265 v4l2 vaapi vdpau vpx faac fdk_aac aom nvenc sndio pulseaudio
 dav1d zimg webp sofa"
build_options_default="x265 v4l2 vpx aom sndio pulseaudio dav1d webp"

case "$XBPS_TARGET_MACHINE" in
	i686*|x86_64*) build_options_default+=" vaapi vdpau nvenc";;
	ppc64*) build_options_default+=" vaapi vdpau";;
	mips*) CFLAGS="-mnan=legacy";;
esac

_apply_patch() {
	local args="$1" pname="$(basename $2)"

	if [ ! -f ".${pname}_done" ]; then
		patch -N $args -i $2
		touch .${pname}_done
	fi
}

post_patch() {
	case "$XBPS_TARGET_MACHINE" in
		ppc64le*) ;;
		ppc*) _apply_patch -p1 ${FILESDIR}/altivec.patch ;;
	esac
}

do_configure() {
	# Fix gcc on x86_64-musl only
	if [ "$XBPS_TARGET_MACHINE" = "x86_64-musl" ]; then
		sed -i configure -e "s;_cflags_speed='-O3';_cflags_speed='-O2';"
	fi

	if [ "$CROSS_BUILD" ]; then
		case "$XBPS_TARGET_MACHINE" in
			arm*) _arch="arm";;
			aarch64*) _arch="aarch64";;
			mips*) _arch="mips";;
			ppc64*) _arch="ppc64";;
			ppc*) _arch="ppc";;
			*) _arch="${XBPS_TARGET_MACHINE%%-musl}";;
		esac

		_args+=" --enable-cross-compile
			--sysroot=$XBPS_CROSS_BASE
			--cross-prefix=${XBPS_CROSS_TRIPLET}-
			--target-os=linux --arch=${_arch}"
	fi

	case "$XBPS_TARGET_MACHINE" in
		ppc|ppc-musl) _args+=" --disable-altivec";;
	esac

	./configure --prefix=/usr --disable-debug --enable-gpl --enable-gnutls \
		--disable-stripping \
		--enable-libcdio --enable-version3 --enable-runtime-cpudetect \
		--enable-libmp3lame --enable-libvorbis --enable-libxvid \
		--enable-libx264 $(vopt_enable vpx libvpx) --enable-libtheora \
		--enable-shared --enable-static --enable-libxcb \
		$(vopt_enable pulseaudio libpulse) --enable-libfreetype --enable-libmodplug \
		--enable-libspeex --enable-libcelt --enable-libass \
		--enable-libopus --enable-librtmp --disable-libjack  $(vopt_enable nvenc) \
		$(vopt_if faac '--enable-nonfree --enable-libfaac') \
		$(vopt_if fdk_aac '--enable-nonfree --enable-libfdk-aac') \
		--disable-libopencore_amrnb --disable-libopencore_amrwb \
		--disable-libopenjpeg \
		--enable-postproc --enable-opencl ${_args} \
		$(vopt_if x265 '--enable-libx265' '--disable-libx265') \
		$(vopt_if v4l2 '--enable-libv4l2' '--disable-libv4l2') \
		$(vopt_enable aom libaom) $(vopt_enable vaapi) $(vopt_enable vdpau) \
		--enable-libbs2b --enable-avresample --enable-libvidstab \
		$(vopt_if dav1d '--enable-libdav1d') \
		$(vopt_if zimg '--enable-libzimg') \
		$(vopt_if webp '--enable-libwebp') \
		$(vopt_if sofa '--enable-libmysofa')
}
do_build() {
	make ${makejobs}
	make doc/ff{mpeg,play}.1
}
do_install() {
	make DESTDIR=${DESTDIR} install install-man
}

libavcodec_package() {
	short_desc="FFmpeg codec library"
	pkg_install() {
		vmove "usr/lib/libavcodec.so.*"
	}
}
libavdevice_package() {
	short_desc="FFmpeg device handling library"
	pkg_install() {
		vmove "usr/lib/libavdevice.so.*"
	}
}
libavresample_package() {
	short_desc="FFmpeg audio resampling library"
	pkg_install() {
		vmove "usr/lib/libavresample.so.*"
	}
}
libavformat_package() {
	short_desc="FFmpeg file format library"
	pkg_install() {
		vmove "usr/lib/libavformat.so.*"
	}
}
libavutil_package() {
	short_desc="FFmpeg utility library"
	pkg_install() {
		vmove "usr/lib/libavutil.so.*"
	}
}
libavfilter_package() {
	short_desc="FFmpeg audio/video filter library"
	pkg_install() {
		vmove "usr/lib/libavfilter.so.*"
	}
}
libpostproc_package() {
	short_desc="FFmpeg video postprocessing library"
	pkg_install() {
		vmove "usr/lib/libpostproc.so.*"
	}
}
libswscale_package() {
	short_desc="FFmpeg video scaling library"
	pkg_install() {
		vmove "usr/lib/libswscale.so.*"
	}
}
libswresample_package() {
	short_desc="FFmpeg video resampling library"
	pkg_install() {
		vmove "usr/lib/libswresample.so.*"
	}
}
ffmpeg-devel_package() {
	depends="
		libavcodec>=${version}_${revision}
		libavdevice>=${version}_${revision}
		libavformat>=${version}_${revision}
		libavutil>=${version}_${revision}
		libavfilter>=${version}_${revision}
		libpostproc>=${version}_${revision}
		libswscale>=${version}_${revision}
		libswresample>=${version}_${revision}
		libavresample>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/pkgconfig
		vmove usr/lib/*.a
		vmove usr/lib/*.so
		vmove usr/share/ffmpeg/examples
		vmove usr/share/man/man3
	}
}
