#!/bin/sh
#
# script for checking which of the FFmpeg output formats
# are supported (with scaling) by given driver

usage ()
{
	echo "Usage: ${0##*/} <ffmpeg path> <HEVC input video file>"
	echo
	echo "ERROR: $1!"
	exit 1
}

if [ $# -ne 2 ]; then
	usage "wrong number of arguments"
fi

errlog=ffmpeg-error.log
ffmpeg=$1
input=$2

if [ \! -x "$ffmpeg" ]; then
	usage "FFmpeg binary '$ffmpeg' missing or not executable"
fi
if [ \! -f "$input" ]; then
	usage "input video file '$input' missing"
fi
if [ "${input%.h265}" = "${input}" ]; then
	usage "input file '$input' not (*.h265) HEVC video"
fi

formats=$($ffmpeg -pix_fmts | awk '/^.O... [^=]/ {print $2}' | sort 2> $errlog)

vaapi="-hwaccel vaapi -vaapi_device /dev/dri/renderD128 -hwaccel_output_format vaapi"
qsv="-hwaccel qsv -qsv_device /dev/dri/renderD128 -c:v hevc_qsv"

# size for test scale
wd=1920
ht=540

old_x=" "
old_o=" "
vaapi_x=" "
vaapi_o=" "
qsv_x=" "
qsv_o=" "
echo "Testing format:"
for format in $formats; do
	echo "- $format"
	export LIBVA_DRIVER_NAME=i965
	$ffmpeg $vaapi -i $input -vf scale_vaapi=w=$wd:h=$ht:format=$format,hwdownload,format=$format -f null - 2>> $errlog
	if [ $? -eq 0 ]; then
		old_x="$old_x $format "
	else
		# VA-API output without GPU side format conversion
		$ffmpeg $vaapi -i $input -vf scale_vaapi=w=$wd:h=$ht,hwdownload,format=$format -f null - 2>> $errlog
		if [ $? -eq 0 ]; then
			old_o="$old_o $format "
		fi
	fi
	export LIBVA_DRIVER_NAME=iHD
	$ffmpeg $vaapi -i $input -vf scale_vaapi=w=$wd:h=$ht:format=$format,hwdownload,format=$format -f null - 2>> $errlog
	if [ $? -eq 0 ]; then
		vaapi_x="$vaapi_x $format "
	else
		# VA-API output without GPU side format conversion
		$ffmpeg $vaapi -i $input -vf scale_vaapi=w=$wd:h=$ht,hwdownload,format=$format -f null - 2>> $errlog
		if [ $? -eq 0 ]; then
			vaapi_o="$vaapi_o $format "
		fi
	fi
	$ffmpeg $qsv -i $input -vf scale_qsv=w=$wd:h=$ht:format=$format,hwdownload,format=$format -f null - 2>> $errlog
	if [ $? -eq 0 ]; then
		qsv_x="$qsv_x $format "
	else
		# QSV output without GPU side format conversion
		$ffmpeg $qsv -i $input -vf scale_qsv=w=$wd:h=$ht,hwdownload,format=$format -f null - 2>> $errlog
		if [ $? -eq 0 ]; then
			qsv_o="$qsv_o $format "
		fi
	fi
done
echo

printf "\ti965:\tVA-API:\tQSV:\tFormat:\n"
for format in $formats; do
	old='-'
	qsv='-'
	vaapi='-'
	echo $old_x | grep -q -w $format
	if [ $? -eq 0 ]; then
		old='X'
	else
		echo $old_o | grep -q -w $format
		if [ $? -eq 0 ]; then
			old='O'
		fi
	fi
	echo $qsv_x | grep -q -w $format
	if [ $? -eq 0 ]; then
		qsv='X'
	else
		echo $qsv_o | grep -q -w $format
		if [ $? -eq 0 ]; then
			qsv='O'
		fi
	fi
	echo $vaapi_x | grep -q -w $format
	if [ $? -eq 0 ]; then
		vaapi='X'
	else
		echo $vaapi_o | grep -q -w $format
		if [ $? -eq 0 ]; then
			vaapi='O'
		fi
	fi
	printf "\t$old\t$vaapi\t$qsv\t$format\n"
done

echo "X = format conversion before download, O = format conversion only after it, - = no support."
echo
echo "Note: FFmpeg stderr output was logged into $errlog."
