# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Jakub Skrzypnik <j.skrzypnik@openmailbox.org>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=ffmpeg4
pkgver=4.4.5
pkgrel=0
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
depends_dev="!ffmpeg-dev"
makedepends="
	alsa-lib-dev
	aom-dev
	bzip2-dev
	coreutils
	gnutls-dev
	imlib2-dev
	lame-dev
	libass-dev
	libdrm-dev
	libsrt-dev
	libssh-dev
	libtheora-dev
	libva-dev
	libvdpau-dev
	libvorbis-dev
	libvpx-dev
	libwebp-dev
	libxfixes-dev
	nasm
	opus-dev
	perl-dev
	pulseaudio-dev
	sdl2-dev
	soxr-dev
	v4l-utils-dev
	x264-dev
	x265-dev
	xvidcore-dev
	zlib-dev
	dav1d-dev
	vidstab-dev
	vulkan-loader-dev
	"
checkdepends="rsync"
subpackages="
	$pkgname-dev
	$pkgname-libavcodec
	$pkgname-libavdevice
	$pkgname-libavfilter
	$pkgname-libavformat
	$pkgname-libavutil
	$pkgname-libpostproc
	$pkgname-libswresample
	$pkgname-libswscale
	"
source="https://ffmpeg.org/releases/ffmpeg-$pkgver.tar.xz
	flto-auto.patch
	0001-libavutil-clean-up-unused-FF_SYMVER-macro.patch
	0001-ffbuild-libversion.sh-add-shebang.patch
	32bit.patch
	ffmpeg-v4l2-configure.patch
	gcc14-posix-ioctl.patch
	binutils-2.43-arm.patch
	"
builddir="$srcdir/ffmpeg-$pkgver"

# secfixes:
#   4.4.1-r0:
#     - CVE-2020-20446
#     - CVE-2020-20453
#     - CVE-2020-22015
#     - CVE-2020-22019
#     - CVE-2020-22021
#     - CVE-2020-22037
#     - CVE-2021-38114
#     - CVE-2021-38171
#     - CVE-2021-38291
#   4.4-r1:
#     - CVE-2021-33815
#   4.4-r0:
#     - CVE-2020-20450
#     - CVE-2020-21041
#     - CVE-2020-22038
#     - CVE-2020-22042
#     - CVE-2020-24020
#     - CVE-2021-30123
#   4.3.2-r0:
#     - CVE-2020-35964
#     - 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

# This is a huge library of audio/video codecs; performance matters,
# compiling it with -Os doesn't make sense.
export CFLAGS=${CFLAGS/-Os/-O2}
export CPPFLAGS=${CPPFLAGS/-Os/-O2}
export CXXFLAGS=${CXXFLAGS/-Os/-O2}

build() {
	export CFLAGS="$CFLAGS -Wno-incompatible-pointer-types"
	export CXXFLAGS="$CXXFLAGS -Wno-incompatible-pointer-types"

	local _dbg="--disable-debug"
	local _asm=""
	[ -n "$DEBUG" ] && _dbg="--enable-debug"

	case "$CARCH" in
	x86) _asm="--disable-asm" ;;
	esac

	./configure \
		--prefix=/usr \
		--enable-avresample \
		--enable-avfilter \
		--enable-gnutls \
		--enable-gpl \
		--enable-libass \
		--enable-libmp3lame \
		--enable-libpulse \
		--enable-libvorbis \
		--enable-libvpx \
		--enable-libxvid \
		--enable-libx264 \
		--enable-libx265 \
		--enable-libtheora \
		--enable-libv4l2 \
		--enable-libdav1d \
		--enable-lto \
		--enable-postproc \
		--enable-pic \
		--enable-pthreads \
		--enable-shared \
		--enable-libxcb \
		--enable-libsrt \
		--enable-libssh \
		--enable-libvidstab \
		--disable-stripping \
		--disable-static \
		--disable-librtmp \
		--enable-libaom \
		--enable-libopus \
		--enable-libsoxr \
		--enable-libwebp \
		--enable-vaapi \
		--enable-vdpau \
		--enable-vulkan \
		--enable-libdrm \
		$_asm $_dbg
	make
}

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

package() {
	make DESTDIR="$pkgdir" install

	# remove files conflicting with main ffmpeg package
	rm -rf "$pkgdir"/usr/bin
	rm -rf "$pkgdir"/usr/share
}

libavcodec() {
	pkgdesc="$pkgdesc (libavcodec library)"

	amove usr/lib/libavcodec.so.*
}

libavdevice() {
	pkgdesc="$pkgdesc (libavdevice library)"

	amove usr/lib/libavdevice.so.*
}

libavfilter() {
	pkgdesc="$pkgdesc (libavfilter library)"

	amove usr/lib/libavfilter.so.*
}

libavformat() {
	pkgdesc="$pkgdesc (libavformat library)"

	amove usr/lib/libavformat.so.*
}

libavutil() {
	pkgdesc="$pkgdesc (libavutil library)"

	amove usr/lib/libavutil.so.*
}

libpostproc() {
	pkgdesc="$pkgdesc (libpostproc library)"

	amove usr/lib/libpostproc.so.*
}

libswresample() {
	pkgdesc="$pkgdesc (libswresample library)"

	amove usr/lib/libswresample.so.*
}

libswscale() {
	pkgdesc="$pkgdesc (libswscale library)"

	amove usr/lib/libswscale.so.*
}

sha512sums="
70df4e63ef507a7ec76da34438142499139769728fd5130d9cf48d56c110ec82c3d6a7e6d1622da03c70167fa861d901d016bbe52c21d2b284b8a0d9f30811dc  ffmpeg-4.4.5.tar.xz
47dda03ef3fddac1f79998bd07044dd907f9ac311737071546cb5cf6b6c51e29dd39cc4458bb07cab049e8ed497358c1e03a4fab07c6c66d7cfeb782ab752e63  flto-auto.patch
1047a23eda51b576ac200d5106a1cd318d1d5291643b3a69e025c0a7b6f3dbc9f6eb0e1e6faa231b7e38c8dd4e49a54f7431f87a93664da35825cc2e9e8aedf4  0001-libavutil-clean-up-unused-FF_SYMVER-macro.patch
38443b570cf32b2ba8ffa9ecc5480425c7da9f93f9773fbb3f9ec5f603b369b6225f4329a1b1f34d0ef30b2c9730f25ff9cff31315ed0a89a600df8e53bd54bf  0001-ffbuild-libversion.sh-add-shebang.patch
c8efe229b66575c375d74352fbab20780a70a8e600974db1ccdc45822ad7b975825738bc971ef9c226f01cdf2829a76586fe2863c0cb732238536427d056d8ae  32bit.patch
9c258e6cca24f090458b9309f5bc3b61460fbc1a5e0397177d0d3a0c42408d7a7d2df4bc68a2cfe283ff3b4653900d3a9a9181bfc464ced48741a7a9e9c980a5  ffmpeg-v4l2-configure.patch
f0a08b14839358db7ec152e8faa677438e42fcd201aed1da994aecd2464865a5d95d433d139bf0da816c60c6c3ee2bde2651a20f2232b727830b47d9cd2448de  gcc14-posix-ioctl.patch
08b83e64f88f22a340dc3f85f8670ca55a07a546ad29dcc740182e8fa82a39534ca253b2ca1e17abaa374e906e1970b7f8ec75a980f90b873321494410232a53  binutils-2.43-arm.patch
"
