SUMMARY="Audio and video recording, conversion, and streaming library"
DESCRIPTION="FFmpeg is a complete, cross-platform solution to record, convert \
and stream audio and video. It includes libavcodec - the leading audio/video \
codec library."
HOMEPAGE="http://www.ffmpeg.org"
LICENSE="GNU LGPL v2.1
	GNU GPL v2"
COPYRIGHT="2000-2003 Fabrice Bellard
	2003-2014 the FFmpeg developers"
SOURCE_URI="http://www.ffmpeg.org/releases/ffmpeg-0.11.5.tar.bz2"
CHECKSUM_SHA256="9492991f44d6757080f457c0aef83e68884dbfb925666806c7f7ea0389e5b8c0"
REVISION="2"
SOURCE_DIR="ffmpeg-$portVersion"
# WARNING: although this can be built with gcc2, the resulting binaries will
# crash when decoding h263 or h264 videos in MediaPlayer. So gcc2 is stuck with
# ffmpeg version 0.10. Do not unmark this as broken until the runtime issues are
# actually fixed.
ARCHITECTURES="!x86_gcc2 ?x86 x86_64"
SECONDARY_ARCHITECTURES="!x86_gcc2 !x86"

if [ $effectiveTargetArchitecture == x86_gcc2 ]; then
	PATCHES="ffmpeg-0.11.5-gcc2.patch"
else
	PATCHES="ffmpeg-0.11.5-gcc4.patch"
fi

PROVIDES="
	ffmpeg_legacy$secondaryArchSuffix = $portVersion compat >= 0.11
	lib:libavutil$secondaryArchSuffix = 51.54.100 compat >= 51
	lib:libavcodec$secondaryArchSuffix = 54.23.100 compat >= 54
	lib:libavformat$secondaryArchSuffix = 54.6.100 compat >= 54
	lib:libavdevice$secondaryArchSuffix = 54.0.100 compat >= 54
	lib:libavfilter$secondaryArchSuffix = 2.77.100 compat >= 2
	lib:libswscale$secondaryArchSuffix = 2.1.100 compat >= 2
	lib:libswresample$secondaryArchSuffix = 0.15.100 compat >= 0
	"


REQUIRES="
	haiku$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libogg$secondaryArchSuffix
	lib:libspeex$secondaryArchSuffix
	lib:libtheoradec$secondaryArchSuffix
	lib:libtheoraenc$secondaryArchSuffix
	lib:libvorbis$secondaryArchSuffix
	lib:libvorbisenc$secondaryArchSuffix
	lib:libvpx$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	ffmpeg_legacy${secondaryArchSuffix}_devel = $portVersion compat >= 0.11
	devel:libavutil$secondaryArchSuffix = 51.54.100 compat >= 51
	devel:libavcodec$secondaryArchSuffix = 54.23.100 compat >= 54
	devel:libavformat$secondaryArchSuffix = 54.6.100 compat >= 54
	devel:libavdevice$secondaryArchSuffix = 54.0.100 compat >= 54
	devel:libavfilter$secondaryArchSuffix = 2.77.100 compat >= 2
	devel:libswscale$secondaryArchSuffix = 2.1.100 compat >= 2
	devel:libswresample$secondaryArchSuffix = 0.15.100 compat >= 0
	"
REQUIRES_devel="
	ffmpeg_legacy$secondaryArchSuffix == $portVersion
	"

BUILD_REQUIRES="
	devel:libbz2$secondaryArchSuffix
	devel:libogg$secondaryArchSuffix
	devel:libspeex$secondaryArchSuffix
	devel:libtheora$secondaryArchSuffix
	devel:libvorbis$secondaryArchSuffix
	devel:libvpx$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:awk
	cmd:make
	cmd:perl
	cmd:pkg_config$secondaryArchSuffix
	cmd:texi2html
	cmd:yasm
	"

GLOBAL_WRITABLE_FILES="
	settings/ffserver.conf
	"

PATCH()
{
	# patch hard-coded config file path
	sed -i "s,/etc/ffserver.conf,$sysconfDir/ffserver.conf," \
		ffserver.c \
		doc/ffserver.texi

	# patch hard-coded paths to perl
	sed -i "s,/usr/bin/perl,$portPackageLinksDir/cmd~perl/bin/perl," \
		Doxyfile \
		doc/texi2pod.pl
}

BUILD()
{
	# not an autotools configure
	./configure \
		--prefix=$prefix \
		--bindir=$prefix/bin \
		--datadir=$dataDir/$portName \
		--incdir=$includeDir \
		--libdir=$libDir \
		--shlibdir=$libDir \
		--mandir=$manDir \
		--disable-debug \
		--enable-shared \
		--enable-libvorbis \
		--enable-libspeex \
		--enable-libtheora \
		--enable-libvpx \
		--disable-amd3dnow --disable-optimizations --disable-stripping \
		--disable-decoder=rv30 --disable-decoder=rv40 --disable-decoder=svq3 \
		--disable-decoder=vp8 --disable-decoder=h264 --disable-parser=h264 \
		--disable-decoder=h263

	if [ "$effectiveTargetArchitecture" == "x86_64" ]; then
		pic=-fPIC
	else
		pic=-fno-PIC
	fi
	make $jobArgs CFLAGS="$pic -std=gnu9x -g"
}

INSTALL()
{
	make install
	if [ "$effectiveTargetArchitecture" == "x86_gcc2" ]; then
		rm -rf $prefix/bin
	fi

	prepareInstalledDevelLibs \
		libavcodec \
		libavdevice \
		libavfilter \
		libavformat \
		libavutil \
		libswresample \
		libswscale
	fixPkgconfig

	# include the documented ffserver.conf
	mkdir -p $docDir
	cp doc/ffserver.conf $docDir

	# devel package
	packageEntries devel \
		$developDir

	rm -rf $prefix/bin
}
