#!/bin/bash 
btime_=`grep btime /proc/stat | awk '{print $2}'`
utime_=`awk '{print $1}' /proc/uptime`
btime_utime_=`echo "${btime_?} + ${utime_?}" | bc -l`

if [ x"$3" == x"" ] ; then
	echo "$0 <command> <left_video> <right_video> <output_file>"
   echo "$0 <command>  <doublefisheye_video> <output_file>"
	exit 1
fi

HLS="n"
MP4="n"
EQ="n"
case "${1}" in
   hls)
      HLS="y"
      echo "hls mode"
      ;;
   mp4)
      MP4="y"
      echo "mp4 mode"
      ;;
   all)
      HLS="y"
      MP4="y"
      echo "Allsizes mode"
      ;;
   equi)
      echo "Fisheye to Equirectangolar mode"
      EQ="y"
      ;;
	equimp4)
		echo "Fisheye to Equirectangular mp4 mode"
		EQ="y"
		MP4="y"
		;;
	equihls)
		echo "Fisheye to Equirectangular HLS mode"
	   EQ="y"
		HLS="y"
		;;
   equiall)
      echo "Fisheye to Equirectangular all size mode"
      EQ="y"
      MP4="y"
      HLS="y"
      ;;
   *)
      echo "Error. Command has to be hls, mp4, all, equi, equihls, equimp4 or equiall"
		echo $1
      exit 1

esac



scales="5760x2880 5120x2560 4096x2048 3840x1920 2880x1440"
degree="180"
yaw="-90"
#VENC="libx264" # can be libx264 or h264_nvenc ( nvidia only after https://github.com/davidramiro/ffmpeg-nvenc-debian )
VENC="h264_nvenc"
HWACC="-hwaccel cuda -hwaccel_output_format cuda"
#HWACC=""
YES="-y"

PRESET="slower"
if [ "$VENC" == "h264_nvenc" ] ; then
   PRESET="slow"
fi

if [ x"$4" != x"" ] ; then
	left=$2
	right=$3
   inf=$2.mp4
   outf=$4
   ViENC="libx264"
   ffmpeg $YES  -copyts \
       -i $left  \
       -i $right  \
       -filter_complex "pad=in_w*2:in_h, overlay=main_w/2:0" -vcodec $ViENC -crf 18 \
       -x264-params mvrange=511 -maxrate 80M -bufsize 25M -ac 2 \
       -pix_fmt yuv420p -c:a aac -b:a 160k -movflags faststart \
       -preset $PRESET $inf
else
   inf=$2
   outf=$3
fi


if [ x"$EQ" == x"y" ] ; then

   ffmpeg $YES -i $inf -vf v360=dfisheye:equirect:ih_fov=$degree:iv_fov=$degree:yaw=${yaw} -vcodec libx264 \
     -crf 18 -x264-params mvrange=511 -maxrate 80M -bufsize 25M -ac 2 \
     -pix_fmt yuv420p -c:a aac -b:a 160k -movflags faststart \
     -preset $PRESET FullScale_${outf}
	  spatialmedia -i -s left-right -m equirectangular FullScale_${outf} FullScale_injected_${outf}
	inf=FullScale_${outf}

fi

num=0
scalestring=""
vmaps=""
amaps=""
tees=""
for ftype in mp4 hls
do
   case $ftype in
      mp4)
         if [ $MP4 != "y" ] ; then
            continue
         fi
         ;;
      hls)
         if [ $HLS != "y" ] ; then
            continue
         fi
    esac
   for scale in $scales
   do
      let "num++"
      if [ x"$scale" == "5760x2880" ] ; then
         sc=""
      else
         sc=",scale=$scale"
      fi
      size=${scale/x/:}
      width=`echo $scale |  awk -F 'x' '{print $1}'`
      ViENC="$VENC"
      if [ "$width" -gt "4097" ] ; then
         ViENC="libx264"
      fi

      splits="$splits[v$num]"
      if [ x"$scalestring" != x"" ] ; then
         scalestring="$scalestring;"
      fi
      scalestring="$scalestring [v${num}]scale=$size[v${num}out]" 
      vmaps="$vmaps -map [v${num}out] -c:v:$((num-1)) $ViENC"
      if [ "$ViENC" == "libx264" ] ; then
         vmaps="$vmaps -crf 18 -x264-params mvrange=511"
      elif [ "$ViENC" == "h264_nvenc" ] ; then
         vmaps="$vmaps -preset:v p7 -tune:v:$((num-1)) hq -rc:v:$((num-1)) vbr -cq:v:$((num-1)) 19 -profile:v high"
      fi
      vmaps="$vmaps -b:v:$((num-1)) 0 -maxrate 80M -bufsize 25M -pix_fmt yuv420p"
      vmaps="$vmaps -preset $PRESET"
      amaps="$amaps -map a:0 -c:a:$((num-1)) aac -b:a:$((num-1)) 160k -ac 2 "

      if [ x"$tees" != x"" ] ; then
         tees="$tees|"
      fi
      tees="$tees [select=\'v:$((num-1)),a:$((num-1))\':f=mp4:movflags=+faststart]${scale}_${outf}"


	#ffmpeg $YES -i $inf -vf v360=dfisheye:equirect:ih_fov=$degree:iv_fov=$degree:yaw=${yaw}${sc} -vcodec $ViENC \
   #	-crf 18 -x264-params mvrange=511 -maxrate 80M -bufsize 25M -ac 2 \
   #	-pix_fmt yuv420p -c:a aac -b:a 160k -movflags faststart \
   #	-preset $PRESET ${scale}_${outf}
   #spatialmedia -i -s left-right -m equirectangular ${scale}_${outf} ${scale}_injected_${outf}
   done
done

if [ X"$scalestring" != X"" ] ; then
   ffmpeg $YES -i $inf -filter_complex "[0:v]split=${num}${splits};$scalestring" $vmaps $amaps -f tee "$tees"
fi




# scale=3840x2560
# Oculus: 1920*1832 per eye.
# 1920*2 = 3840 like 4k.
