#!/bin/sh

set -e

##
## defaults
##
INPUT=../tmp/omni/show0/earth.yuv
RATIO=4:3

##
## generic functions
##
# shellcheck disable=SC2048,SC2059
echo_n() {
 printf -- "$*"
}

HOST=${1:-morla}
if [ "$HOST" = "$(hostname --short)" ]; then
	IP=127.0.0.1
else
	IP=$(host "$HOST" | grep -Po 'address \K\S+')
fi

FIRSTPORT=${2:-5002} # even number - next 3 ports used too

# TODO: resolve this or resize based on it
HEIGHT=384

videosize() { height=$1; width_or_ratio=$2;
 case $width_or_ratio in
  *:*) num=${width_or_ratio%:*}; den=${width_or_ratio#*:}; width=$((height*num/den));;
  *) width=$width_or_ratio;;
 esac
 echo_n $((height*width))
}

BITS=$(videosize "$HEIGHT" "$RATIO")

# * bitrates and bits in parens based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
#   + bits rounded up to include nearby modulo 16 formats
#   + best (i.e. high-modulo) 16:9 heights: 216 288 360 432 576 720
#   + best (i.e. high-modulo) 4:3 heights: 288 312 384 480 624 816
# * speeds tuned to just below 100% cpu usage for each combination on a multi-core computer
# TODO: Externalize speeds to site-specific configfile
if [ $BITS -le 110592 ]; then # 234p → 97344
 VBITRATE=145000
 SPEED_VP8=2
elif [ $BITS -le 150528 ]; then # 270p → 129600
 VBITRATE=365000
 SPEED_VP8=2
elif [ $BITS -le 196608 ]; then # 360p → 172800
 VBITRATE=730000
 SPEED_VP8=3
elif [ $BITS -le 331776 ]; then # 432p → 331776
 VBITRATE=1100000
 SPEED_VP8=4
elif [ $BITS -le 589824 ]; then # 540p → 518400
 VBITRATE=2000000
 SPEED_VP8=5
elif [ $BITS -le 921600 ]; then # 720p → 921600
 VBITRATE=3000000
 SPEED_VP8=15
fi

encode_vp8() { bitrate=$1; speed=$2;
 echo_n "-codec:v vp8 -quality realtime -deadline 1000000 -cpu-used $speed \
 -b:v $bitrate -minrate $bitrate -maxrate $bitrate \
 -undershoot-pct 95 -bufsize $((6000*bitrate/1000)) -rc_init_occupancy $((4000*bitrate/1000)) \
 -max-intra-rate 0 \
 -qmin 4 -qmax 56"
}

# based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
# + Add 1s latency (deadline)
# + Use same RTP payload types as GStreamer
ffmpeg -hide_banner -threads auto -re \
 -stream_loop -1 -i "$INPUT" \
 -vf format=pix_fmts=yuv420p \
 -map '0:v' \
 $(encode_vp8 "$VBITRATE" "$SPEED_VP8") \
 -f rtp -payload_type 100 "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1200"