#!/bin/bash

#######################################################################
# HD-Videobench: A Benchmark for High Definition Digital Video Coding
# http://people.ac.upc.edu/alvarez/hdvideobench
#######################################################################
# Author: Mauricio Alvarez: alvarez@ac.upc.edu
# 15/03/2007. 
# Licence: GPL.
# You have to define the <base-dir> directory in defines.inc
#######################################################################

if [ -e config.inc ]; then
	source config.inc
        if [ ! -d $HDBENCH_DIR ]; then
        	echo "Error: HDBENCH_DIR not defined"
		echo "Please define HDBENCH_DIR in file defines.inc"
		exit 1
        fi		
else
	echo "Error: defines.inc not found"
	echo "Please define the base directories in a file defines.inc"
	exit 1
fi

#######################################
# Encoding/Decoding one video sequence
#######################################

mpeg2_decode() {
    ${HDBENCH_PREFIX}/bin/mplayer $1 -vc mpeg12 -nosound -vo null -benchmark
}

mpeg4_decode() {
    ${HDBENCH_PREFIX}/bin/mplayer $1 -vc xvid -nosound -vo null -benchmark
}

h264_decode(){
    ${HDBENCH_PREFIX}/bin/mplayer $1 -vc ffh264 -nosound -vo null -benchmark
}

mpeg2_encode() {
    time ${HDBENCH_PREFIX}/bin/mencoder $1 -o $2		\
      -demuxer rawvideo -rawvideo fps=25:format=i420:w=$3:h=$4	\
      -ofps 25 -ovc lavc -lavcopts vcodec=mpeg2video:vqscale=$6:vmax_b_frames=2:subq=8:psnr
}

mpeg4_encode() {
    time ${HDBENCH_PREFIX}/bin/mencoder $1 -o $2			\
      -demuxer rawvideo -rawvideo fps=25:format=i420:w=$3:h=$4  	\
      -ofps 25 -ovc xvid -xvidencopts fixed_quant=$6:max_bframes=2:qpel:psnr
}

h264_encode() {
    time ${HDBENCH_PREFIX}/bin/x264 --bframes 2 --no-b-adapt --b-bias=0 --ref 16 --analyse all \
      --weightb --me hex --merange 24 --subme 7 --8x8dct --fps 25 --frames 101 --progress \
      -o $2 $1 $3x$4 --threads=$5 --qp=$6
}

#h264_encode_mencoder() {
#    ${HDBENCH_PREFIX}/bin/mencoder $1					\
#      -demuxer rawvideo -rawvideo fps=25:format=i420:w=$3:h=$4	\
#      -o /usr/tmp/out/$2 				\
#      -ofps 25 -ovc x264enc -x264encopts #qp=26:frameref=16:bframes=2:nob_adapt:b_bias=0:weight_b:partitions=all:8x8dct:me=hex:me_range=24:subq=7
#}

#######################################################
# Encoding/Decoding multiple video sequences
#######################################################

###################
# Decoding
###################
mpeg2_decode_all() {
    for video in $input_videos; do
        for resol in $resolutions; do
            video_name=${resol}_${video}_mpeg2.avi
            input_video="${HDBENCH_SEQS}/mpeg2/${video_name}"
            if [ -e $input_video ] ; then
                echo "mpeg2_decode $input_video"             
                mpeg2_decode $input_video
            else
                echo "file not found $input_video"
            fi
        done
    done
}

mpeg4_decode_all() {
    for video in $input_videos; do
        for resol in $resolutions; do
            video_name=${resol}_${video}_mpeg4.avi
            input_video="${HDBENCH_SEQS}/mpeg4/${video_name}"
            if [ -e $input_video ] ; then             
                echo "mpeg4_decode $input_video"
                mpeg4_decode $input_video
            else
                echo "file not found $input_video"
            fi
        done
    done
}

h264_decode_all() {
    for video in $input_videos; do
        for resol in $resolutions; do
            video_name=${resol}_${video}.h264
            input_video="${HDBENCH_SEQS}/h264/${video_name}"
            if [ -e $input_video ] ; then
                echo "h264_decode $input_video"
                h264_decode $input_video
            else
                echo "file not found $input_video"
            fi
        done
    done
}


###################
# Encoding
###################
mpeg2_encode_all() {
    for resol in $resolutions; do
        if [ $resol = "576p25" ] ; then
            width=720
            height=576
        elif [ $resol = "720p25" ] ; then
            width=1280
            height=720
        elif [ $resol = "1088p25" ] ; then
            width=1920
            height=1088
        else
            echo "resolution not recognized"
            exit 1
        fi
        for video in $input_videos; do
            video_name=${resol}_${video}
            yuv_input="${HDBENCH_SEQS}/yuv/${video_name}.yuv"
            if [ -e $yuv_input ] ; then 
                for slices in $SLICES; do
                    for qp in $MPEG_QP; do 
                        mpeg2_output="/usr/tmp/out/${video_name}.${slices}s.qp${qp}.mpeg2.avi"
                        echo "mpeg2_encode $yuv_input $mpeg2_output $width $height $slices $qp"
	                mpeg2_encode $yuv_input $mpeg2_output $width $height $slices $qp
	            done
                done
            else
                echo "file not found $yuv_input"
            fi
        done
    done
}

mpeg4_encode_all() {
    for resol in $resolutions; do
        if [ $resol = "576p25" ] ; then
            width=720
            height=576
        elif [ $resol = "720p25" ] ; then
            width=1280
            height=720
        elif [ $resol = "1088p25" ] ; then
            width=1920
            height=1088
        else
            echo "resolution not recognized"
            exit 1
        fi
        for video in $input_videos; do
            video_name=${resol}_${video}
            yuv_input="${HDBENCH_SEQS}/yuv/${video_name}.yuv"
            if [ -e $yuv_input ] ; then 
                for slices in $SLICES; do
                    for qp in $MPEG_QP; do 
                        mpeg4_output="/usr/tmp/out/${video_name}.${slices}s.qp${qp}.mpeg4.avi"
                        echo "mpeg4_encode $yuv_input $mpeg4_output $width $height $slices $qp"
	                mpeg4_encode $yuv_input $mpeg4_output $width $height $slices $qp
	            done
                done
            else
                echo "file not found $yuv_input"
            fi
        done
    done
}



h264_encode_all() {
    for resol in $resolutions; do
        if [ $resol = "576p25" ] ; then
            width=720
            height=576
        elif [ $resol = "720p25" ] ; then
            width=1280
            height=720
        elif [ $resol = "1088p25" ] ; then
            width=1920
            height=1088
        else
            echo "resolution not recognized"
            exit 1
        fi
        for video in $input_videos; do
            video_name=${resol}_${video}
            yuv_input="${HDBENCH_SEQS}/yuv/${video_name}.yuv"
            if [ -e $yuv_input ] ; then 
                for slices in $SLICES; do
                    for qp in $H264_QP; do 
                        h264_output="/usr/tmp/out/${video_name}.${slices}s.qp${qp}.h264"
                        echo "h264_encode $yuv_input $h264_output $width $height $slices $qp"
	                h264_encode $yuv_input $h264_output $width $height $slices $qp        
	            done
                done
            else
                echo "file not found $yuv_input"
            fi
        done
    done
}

###########
#decoding #
###########
if [ $decoding -eq 1 ] ; then
    i="0"
    while [ $i -lt $runs ]
    do
        echo "************ HD-VideoBench decoding run: $i"
        mpeg2_decode_all
        mpeg4_decode_all
        h264_decode_all
        i=$[$i+1]
    done
fi

###########
#encoding #
###########
if [ $encoding -eq 1 ] ; then
    i="0"
    while [ $i -lt $runs ]
    do
        echo "************ HD-VideoBench encoding run: $i"
        mpeg2_encode_all
        mpeg4_encode_all
        h264_encode_all
        i=$[$i+1]
    done
fi


