#!/bin/sh
#
def_interlace=1
def_rescale=1
def_subq=1

help(){
    cat <<END
Usage: $0 [options] filename.anything
Options:
    -i n      n=0 prog, n=1 top, n=2 bot            ($def_interlace)
    -s n      n=0 no change, n=1 720p30             ($def_rescale)
    -q n      subpixed refinement quality
              n=1 lowest, ..., n=7 highest          ($def_subq)
    -h        Print this help message
END
    exit 0
}

interlace=$def_interlace
rescale=$def_rescale
subq=$def_subq

while getopts s:i:q:h name "$@"
do
    case $name in
i)
    interlace=$OPTARG ;;
s)
    rescale=$OPTARG ;;
q)
    subq=$OPTARG ;;
*)
    help ;;
    esac
done
let shiftind=$OPTIND-1
shift $shiftind
if test "$#" != "1"
then
    help
fi

outfile=$1
base=`echo $outfile | sed "s/\.[^.]*$//"`

case $interlace in
0)
    iflag_m=NOT_INTERLACED
	xint_f="nointerlaced"
	scale_f="-vf scale=1280:720"
	int_f="" ;;
1)
    iflag_m=INTERLACED_TOP_FIRST
	xint_f="interlaced"
	xint_g="--interlaced"
	scale_f="-vf yadif=0:0,scale=1280:720"
	int_f="-field-dominance 0" ;;
*)
	iflag_m=INTERLACED_BOTTOM_FIRST
	xint_f="interlaced"
	xint_g="--interlaced"
	scale_f="-vf yadif=0:1,scale=1280:720"
	int_f="-field-dominance 1" ;;
esac

case $rescale in
1)
	xint_f="nointerlaced" ;;
*)
	scale_f="" ;;
esac

echo "Encoding yuv4mpeg on /dev/stdin to $base.avi"
echo x264 --pass 1 --bitrate 10000 --stats ".stats" --level 4.1 --keyint 14 --min-keyint 2 --ref 2 --mixed-refs --bframes 2 --weightb --direct auto --deblock -1:-1 --subme 5 --partitions p8x8,b8x8,i4x4,i8x8 --8x8dct $xint_g --ipratio 1.1 --pbratio 1.1 --vbv-bufsize 14475 --vbv-maxrate 24000 --qcomp 0.5 --merange 12 --threads auto --progress --no-psnr --no-ssim --output $base.264 /dev/stdin --mvrange 511 --aud


encopts="bitrate=10000:$xint_f:threads=2:level_idc=41:bframes=2"
encopts=$encopts":frameref=3:nob_pyramid:8x8dct:mixed_refs:bime:brdo"
encopts=$encopts":subq=$subq:trellis=2:aq_mode=1:cqm=flat"

#y4mtoyuv | head -c 256
#cat |
#y4mtoyuv |
#x264 --fps 30000/1001 --sar 1:1 --pass 1 --bitrate 8000 --stats ".stats" --level 4.1 --keyint 14 --min-keyint 2 --ref 2 --mixed-refs --bframes 2 --weightb --direct auto --deblock -1:-1 --subme 5 --partitions p8x8,b8x8,i4x4,i8x8 --8x8dct $xint_g --ipratio 1.1 --pbratio 1.1 --vbv-bufsize 14475 --vbv-maxrate 24000 --qcomp 0.5 --merange 12 --threads auto --progress --no-psnr --no-ssim --output $base.264 /dev/stdin 1440x1080 --mvrange 511 --aud
yuvcorrect -T $iflag_m 2>/dev/null |
yuvdeinterlace |
yuvcorrect -T PROGRESSIVE |
y4mscaler -O size=1280x720 |
y4mtoyuv |
x264 --fps 30000/1001 --sar 1:1 --pass 1 --bitrate 8000 --stats ".stats" --level 4.1 --keyint 14 --min-keyint 2 --ref 2 --mixed-refs --bframes 2 --weightb --direct auto --deblock -1:-1 --subme 5 --partitions p8x8,b8x8,i4x4,i8x8 --8x8dct $xint_g --ipratio 1.1 --pbratio 1.1 --vbv-bufsize 14475 --vbv-maxrate 24000 --qcomp 0.5 --merange 12 --threads 1 --progress --no-psnr --no-ssim --output $base.264 /dev/stdin 1280x720 --mvrange 511 --aud

#mencoder /dev/stdin -nosound $int_f \
#	-aspect 16:9 $scale_f \
#    -ovc x264 -x264encopts $encopts \
#    -of rawvideo -o $base.264
