# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Jakub Skrzypnik <j.skrzypnik@openmailbox.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=jellyfin-ffmpeg
pkgver=4.3.2
pkgrel=1
pkgdesc="Complete and free Internet live audio and video broadcasting solution for Linux/Unix"
url="https://ffmpeg.org/"
arch="all"
license="GPL-2.0-or-later AND LGPL-2.1-or-later"
options="!check" # tests/data/hls-lists.append.m3u8 fails
subpackages="$pkgname-libs $pkgname-doc $pkgname-dev"
makedepends="
	git
	amf-headers
	alsa-lib-dev
	coreutils
	bzip2-dev
	gnutls-dev
	imlib2-dev
	lame-dev
	libass-dev
	libsrt-dev
	libssh-dev
	libtheora-dev
	libva-dev
	libvdpau-dev
	libvorbis-dev
	libvpx-dev
	libxfixes-dev
	libdav1d
	libdrm-dev
	libwebp-dev
	libx11-dev
	libxext-dev
	libxml2-dev
	opus-dev
	perl-dev
	sdl2-dev
	soxr-dev
	v4l-utils-dev
	x264-dev
	x265-dev
	xvidcore-dev
	yasm
	zlib-dev
	xz-dev
	dav1d-dev
	vidstab-dev
	vulkan-loader-dev
	fribidi-dev
	gmp-dev
	libbluray-dev
	freetype
	ffnvcodec-headers
	opencl-headers
	libmfx
	zimg-dev
	zvbi-dev
	"

checkdepends="rsync"
source="${pkgname}-${pkgver}.tar.gz::https://github.com/jellyfin/jellyfin-ffmpeg/archive/refs/tags/v$pkgver-1.tar.gz
	0001-libavutil-clean-up-unused-FF_SYMVER-macro.patch
	3e098cca6e51db0f19928c12d0348deaa17137b3.patch
	"
builddir="${srcdir}/${pkgname}-${pkgver}-1"
provides="ffmpeg"

# secfixes:
#   4.3.1-r4:
#     - CVE-2020-35965
#   4.3.1-r0:
#     - CVE-2020-14212
#   4.3-r0:
#     - CVE-2020-13904
#     - CVE-2020-12284
#   4.2.1-r0:
#     - CVE-2019-15942
#   4.2-r0:
#     - CVE-2019-17539
#     - CVE-2019-17542
#   4.1.4-r0:
#     - CVE-2019-12730
#   4.1.3-r0:
#     - CVE-2019-9718
#     - CVE-2019-9721
#     - CVE-2019-11338
#     - CVE-2019-11339
#   4.1.1-r0:
#     - CVE-2019-1000016
#   4.1-r0:
#     - CVE-2018-13305
#     - CVE-2018-15822
#   4.0.2-r0:
#     - CVE-2018-13301
#     - CVE-2018-13303
#     - CVE-2018-13304
#     - CVE-2018-1999010
#     - CVE-2018-1999011
#     - CVE-2018-1999012
#     - CVE-2018-1999013
#     - CVE-2018-1999014
#     - CVE-2018-1999015
#   4.0.1-r0:
#     - CVE-2018-12459
#     - CVE-2018-12460
#   4.0.0-r0:
#     - CVE-2018-6912
#     - CVE-2018-7757
#     - CVE-2018-9841
#   3.4.4-r0:
#     - CVE-2018-14395
#   3.4.3-r0:
#     - CVE-2018-7557
#     - CVE-2018-7751
#     - CVE-2018-10001
#     - CVE-2018-12458
#     - CVE-2018-13300
#     - CVE-2018-13302
#     - CVE-2018-14394
#   3.3.4-r0:
#     - CVE-2017-14054
#     - CVE-2017-14055
#     - CVE-2017-14056
#     - CVE-2017-14057
#     - CVE-2017-14058
#     - CVE-2017-14059
#     - CVE-2017-14169
#     - CVE-2017-14170
#     - CVE-2017-14171
#     - CVE-2017-14222
#     - CVE-2017-14223
#     - CVE-2017-14225

# add support for AV1 codec for all archies except armhf and armv7
# as aom is not available on them
_aom="";

case "$CARCH" in
	x86|x86_64|aarch64|ppc64le|s390x )
		_aom="--enable-libaom"; makedepends="$makedepends aom-dev" ;;
esac

build() {
	cd "${builddir}"
	local _dbg="--disable-debug"
	local _asm=""
	[ -n "$DEBUG" ] && _dbg="--enable-debug"

	case "$CARCH" in
	x86) _asm="--disable-asm" ;;
	mips*) _asm="--disable-asm --disable-inline-asm --disable-mipsdsp --disable-mipsdspr2 --disable-msa --disable-msa2 --disable-mipsfpu --disable-mips64r6 --disable-mips64r2" ;;
	esac

	./configure \
	--prefix="/usr" \
        --extra-version=Jellyfin \
        --disable-ffplay \
        --disable-shared \
        --disable-libxcb \
        --disable-sdl2 \
        --disable-xlib \
        --enable-gpl \
        --enable-version3 \
        --enable-static \
        --enable-libfontconfig \
        --enable-fontconfig \
        --enable-gmp \
        --enable-gnutls \
        --enable-libass \
        --enable-libbluray \
        --enable-libdrm \
        --enable-libfreetype \
        --enable-libfribidi \
        --enable-libmp3lame \
        --enable-libopus \
        --enable-libtheora \
        --enable-libvorbis \
        --enable-libdav1d \
        --enable-libwebp \
        --enable-libvpx \
        --enable-libx264 \
        --enable-libx265 \
        --enable-vaapi \
        --enable-amf \
        --enable-cuvid \
        --enable-nvenc \
        --enable-nvdec \
        --enable-ffnvcodec \
 	--enable-libzvbi \
        --enable-libzimg \
	$_asm $_aom $_dbg

        #--disable-doc \
#        --enable-cuda-llvm \
#        --enable-cuda \
#        --enable-libmfx \
#       --enable-opencl \

	make
	${CC:-gcc} -o tools/qt-faststart $CFLAGS tools/qt-faststart.c
	make doc/ffmpeg.1 doc/ffplay.1
}

# https://ffmpeg.org/fate.html
check() {
	cd "${builddir}"
	./configure \
		--samples=fate-suite/
	make fate-rsync
	make fate-list
	make fate
}

package() {
	cd "${builddir}"
	make DESTDIR="$pkgdir" install install-man
	install -D -m755 tools/qt-faststart "$pkgdir/usr/bin/qt-faststart"
#	strip --strip-debug "$pkgdir"/usr/lib/*.a
}

libs() {
	pkgdesc="Libraries for ffmpeg"
	replaces="ffmpeg"
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr
}

sha512sums="
64e1052c45145e27726e43d4fe49c9a92058e55562d34fd3b3adf54d3506e6bd680f016b748828215e1bfc8ce19aa85b6f7e4eb05fafe21479118a4ad528a81f  ffmpeg-4.3.1.tar.xz
1047a23eda51b576ac200d5106a1cd318d1d5291643b3a69e025c0a7b6f3dbc9f6eb0e1e6faa231b7e38c8dd4e49a54f7431f87a93664da35825cc2e9e8aedf4  0001-libavutil-clean-up-unused-FF_SYMVER-macro.patch
7151e98829c215619b82e27fdff98b9a0d6a778f499170f3688e111a8bf7b2cc8895f09aa49bcb812ba5b5f06dd0243ebc79c31af246420f7d0869859b4a0241  3e098cca6e51db0f19928c12d0348deaa17137b3.patch
acf4b34feaa1c57f621d5a8f56967e6d77ebe1d6288d94b853b513d6b2339debbaa38063ec11900258f31753cf24fef81bd60225149af45c03bfddf0b231f881  7c59e1b0f285cd7c7b35fcd71f49c5fd52cf9315.patch
ab5006a99af6e0402e1a2bc13a76f55b13144fcd7b71124fe3f82989d03bf1e1c306b66da7ce63662b6dfbdbb3edebef2cf280c08ac793fc3b983bd65c0f0ad5  CVE-2020-35965.patch
"
sha512sums="8102a249db3d03519e3a68990934acdc373f68cdf921ed4d092f43adf402815f4b092e75bf2a1c5116d679ddf2c72d43d5c65d274eed65ca66421f166bb84633  jellyfin-ffmpeg-4.3.2.tar.gz
1047a23eda51b576ac200d5106a1cd318d1d5291643b3a69e025c0a7b6f3dbc9f6eb0e1e6faa231b7e38c8dd4e49a54f7431f87a93664da35825cc2e9e8aedf4  0001-libavutil-clean-up-unused-FF_SYMVER-macro.patch
7151e98829c215619b82e27fdff98b9a0d6a778f499170f3688e111a8bf7b2cc8895f09aa49bcb812ba5b5f06dd0243ebc79c31af246420f7d0869859b4a0241  3e098cca6e51db0f19928c12d0348deaa17137b3.patch"
