#!/bin/sh

#
# Script to support automated testing of mencoder. The parameters are:
#
#	$1	codec name
#	$2	block bit rate to be used to compute the optimal bitrate (see below)
#	$3	codec options under testing
#	$4	movie filename
#
#
codec=$1
bbr=$2
copt=$3
fileout=$4

#
# The script must be run from the directory containing only the frames to be used for testing
# The images are in 'fmt' format (fmt can be jpg, png)
#
fmt=jpg

#
# Set the mplayer environment variables (change for your configuration)
#
LD_LIBRARY_PATH=/local/usr/lib/lib; export LD_LIBRARY_PATH
PATH=${PATH}:/local/usr/bin; export PATH

#
# read the image dimensions
#
for i in *.$fmt
do
  img=$i
  break
done

width=`identify $img | awk '{print $3}' | awk -Fx '{print $1}'`
height=`identify $img | awk '{print $3}' | awk -Fx '{print $2}'`

#
# compute the optimal bitrate 
#	optimal_bitrate = block_bit_rate * 25 * width * height / 256
#
obr=`expr $width \* $height \* $bbr \* 25 / 256`

#
# clean temporary files that can interfere with the compression phase
#
rm -f divx2pass.log frameno.avi

#
# compress
#
mencoder -ovc lavc -lavcopts vcodec=$codec:vpass=1:vbitrate=$obr:$copt:psnr -mf type=$fmt:w=$width:h=$height:fps=10 -nosound -o /dev/null mf://\*.$fmt >/tmp/testing-mencoder-out1 2>/dev/null
mencoder -ovc lavc -lavcopts vcodec=$codec:vpass=2:vbitrate=$obr:$copt:psnr -mf type=$fmt:w=$width:h=$height:fps=10 -nosound -o $fileout  mf://\*.$fmt >/tmp/testing-mencoder-out2 2>/dev/null

#
# cleanup
#
rm -f divx2pass.log

#
# report
#
echo "==============================================="
echo "Codec:            " $codec
echo "Block Bit Rate:   " $bbr
echo "Optimal Bit Rate: " $obr
echo "options:          " $copt
echo "File:             " $fileout
echo "Pass 1:"
echo -n "                   "
grep "Video stream: " /tmp/testing-mencoder-out1
echo -n "                   "
grep "PSNR: " /tmp/testing-mencoder-out1
echo "Pass 2:"
echo -n "                   "
grep "Video stream: " /tmp/testing-mencoder-out2
echo -n "                   "
grep "PSNR: " /tmp/testing-mencoder-out2
echo "==============================================="

rm -f /tmp/testing-mencoder-out1 /tmp/testing-mencoder-out2




