#make fprofiled

INF="The.Matrix.Reloaded.2003.720p.HDDVD.DTS.x264-ESiR.clip.mkv"
IN="$INF -vf crop=1280:528 -ss 85 -frames 10000"
cache_file x264.8 x264.10 $INF

run() {
    LOG="x264.prof.${1}bit.$2.0"
    echo "x264 $3" > $LOG
    ./x264.$1 --version | egrep -v 'libav|libswscale' >> $LOG
    perl -pi -e 's/gcc: \S+/$& fprofiled/' $LOG
    rm -f fifo.y4m
    mkfifo fifo.y4m
    sudo opcontrol --deinit
    sudo opcontrol --reset
    sudo opcontrol --start $EVENT
    mplayer $IN -vo yuv4mpeg:file=fifo.y4m -nosound -benchmark -nocache &
    ./x264.$1 fifo.y4m $3 --no-progress -o /dev/null 2>&1 | egrep -v 'no version information' >> $LOG
    sudo opcontrol --stop
    sudo opcontrol --dump
    opreport -l ./x264.$1 >> $LOG
}

runs() {
    for b in 8 10 ; do
        run $b p0 "--crf 20 --preset ultrafast"
        run $b p1 "--crf 20 --preset superfast"
        run $b p2 "--crf 20 --preset veryfast"
        run $b p3 "--crf 20 --preset faster"
        run $b p4 "--crf 20 --preset fast"
        run $b p5 "--crf 20 --preset medium"
        run $b p6 "--crf 20 --preset slow"
        run $b p7 "--crf 20 --preset slower"
        run $b p8 "--crf 20 --preset veryslow"
        run $b p9 "--crf 20 --preset placebo"
        run $b lossless "--qp 0 --preset fast"
    done
    run 8 baseline "--crf 20 --preset medium --profile baseline"
    rm -f fifo.y4m
    for i in x264.prof.*.0 ; do (grep -v '^(' $i | head -4; echo) > ${i%\.0}.1 ; merge_oprof_x264.pl $i >> ${i%\.0}.1 ; done
}

EVENT="--event CPU_CLK_UNHALTED:100000"
runs
sudo opcontrol --reset
