#!/bin/bash
set -e

w=1920; h=1024;  # 16:9
#w=1920; h=1200;  # 16:10
#w=3440; h=1440;  # ed's ultrawide
#w=4096; h=2160;  # 4k cinema (dci 4k)
#w=3840; h=2160;  # 4k tv

fps=24
fps=50
fps=60
#fps=1

# zoom level
z=1

c1=ffbb00 #44deg
c2=bb00ff #284deg

c1=ffee00
c2=9900ff

printf "$(echo $c1 | sed -r 's/(..)/\\x\1/g')" > /dev/shm/c1
printf "$(echo $c2 | sed -r 's/(..)/\\x\1/g')" > /dev/shm/c2

while true; do
cat /dev/shm/c{,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}{1,2}
done | head -c $((3*w)) > /dev/shm/r1

while true; do
cat /dev/shm/c{,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}{2,1}
done | head -c $((3*w)) > /dev/shm/r2

f1() {
while true; do cat /dev/shm/r{,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}{1,2}
done | head -c $((3*w*h))
}

f2() {
while true; do cat /dev/shm/r{,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}{2,1}
done | head -c $((3*w*h))
}

use='-pix_fmt rgb24 -vcodec rawvideo -f nut -'
#use='-pix_fmt rgb24 -re -f sdl -window_fullscreen 1 -'

box="drawbox=x=$((w/4)):y=$((h/4)):w=$((w/2)):h=$((h/2)):color=d0a7b5:t=fill"

{ f1; f2; } |
ffmpeg -hide_banner -v warning -f rawvideo -s ${w}x${h} -r $fps -pix_fmt rgb24 -i - \
  -pix_fmt yuv444p -f yuv4mpegpipe - > /dev/shm/f12.y4m

(head -n 1 /dev/shm/f12.y4m; while true; do
  tail -qn +2 /dev/shm/f12.y4m{,,,,,,,,,,,,,,,,,,,}; done) |
ffmpeg -hide_banner -v warning -f yuv4mpegpipe -r $fps -i - \
  -vf crop=$((w/z)):$((h/z)),scale=$w:$h:flags=neighbor,"$box",setpts="N/($fps*TB)" $use |
mpv -fs --video-unscaled=yes -vo gpu -
#cat

stty echo
