#!/bin/sh
# ffpip 006 von ice32
#+yadif=2:-1:0,mcdeint=2:1:1
#-f:v field:0+yadif=0:1:0 -sws_flags fast_bilinear \

function ffplay() {
    if [[ "$osd_res" == 0 ]]; then
        x_scale=4*"$zoom"
        y_scale=3*"$zoom"/1.3
        x_offset=1280-"$x_scale"-"$x_pos"
    fi

    if [[ "$ist_hd" == 0 ]]; then
	ffmpeg -nostdin -loglevel panic -threads 0 -err_detect ignore_err \
	-fflags +genpts+igndts+ignidx -re -vsync drop -ss 00:00:05 \
	-i http://"$ip_box":"$port"/id="$channel":loop=-1:50:0 -streamid 0:0 \
	-slices 8 -map 0:v -vf scale="$x_scale":"$y_scale":force_original_aspect_ratio=disable \
	-an -sn -dn \
	-pix_fmt bgra -yoffset "$y_pos" -xoffset "$x_offset" -r 0.5 \
	-f fbdev /dev/fb0 &
    else
        ffmpeg -nostdin -loglevel panic -threads 2 -err_detect ignore_err -fflags igndts \
        -i http://"$ip_box":"$port"/id="$channel":loop=-1:50:0 -streamid 0:0 \
        -map 0:v -vf scale="$x_scale":"$y_scale":force_original_aspect_ratio=disable \
        -f:v fps=50+idet+field=1 -sws_flags fast_bilinear \
        -vsync -1 -an -sn -dn \
        -pix_fmt bgra -yoffset "$y_pos" -xoffset "$x_offset" -r 1 \
        -f fbdev /dev/fb0 &
    fi
}

function ffkill() {
    kill -TERM "$ffmpid"
}

function no_webtv() {
    chkid=$(echo "$1" | grep -c "^ffffffff")
    if [[ "$chkid" == 1 ]]; then
	text "$webtv"
	exit 1
    fi
}

function text() {
    msgbox popup="FFpip ""$1" timeout=3 hide=1 refresh=1
}

function ffnow() {
    ffget=$(ps aux | grep id= | cut -d'=' -f2 | awk '{print $1}')
#    kerne=$(ps aux | grep ffmpeg | cut -d's' -f3 | awk '{print $1}')
}

ffmpid=$(pidof ffmpeg)
osd_res=$(pzapit -osd)
id_holen=$(pzapit -gi)
ist_hd=$(echo $id_holen | grep -c HD)
channel=$(echo $id_holen | awk '{print $1}')
ip_box=$(ifconfig |grep -m 1 'inet addr'| cut -d':' -f2| awk '{print $1}')
port=31339

zoom=90

if [[ "$ist_hd" == 1 ]]; then
    zoom=60
fi

x_scale=6*"$zoom"
y_scale=4.5*"$zoom"/1.3
x_pos=15
y_pos=15
x_offset=1920-"$x_scale"-"$x_pos"
x_zap="$x_offset"

schon="läuft schon..."
starten="wird gestartet..."
tausch="tausche Bild..."
ende="wird beendet..."
webtv="spielt kein webtv..."
links="Bild nach links..."
rechts="Bild nach rechts..."

msgbox size=22 title="FFpip Auswahl" select=" Starten , tauschen, Beenden , links , rechts" default=2 cyclic=0 order=3 msg="" refresh=1

case "$?" in
    1)   if [ -n "$ffmpid" ]; then
	     text "$schon"
	     return
	 fi
	 no_webtv $channel
	 text "$starten"
         ffplay
         ;;
    2)   ffnow
         no_webtv $channel
         ffkill
         ffplay
         text "$tausch"
         pzapit -zi "$ffget"
	 ;;
    3)	 ffkill
    	 text "$ende"
         ;;
    4)	 ffnow
	 ffkill
	 channel="$ffget"
	 x_offset=15
	 ffplay
	 text "$links"
	 ;;
    5)	 ffnow
	 ffkill
	 channel="$ffget"
	 x_offset="$x_zap"
	 ffplay
	 text "$rechts"
	 ;;
esac

exit 0





