audio: add af_lavrresample, remove old resampling filters
[mplayer.git] / TOOLS / psnr-video.sh
blobd06ed0d880c9df3bde5fe08737c77fd6abf5b8c0
1 #!/bin/bash
2 # Helper script to ease comparing the PSNR between two video files
3 # Copyleft 2005 by Matthias Wieser
4 # Copyleft 2005 by Ivo
5 # This file comes under GPL, see http://www.gnu.org/copyleft/gpl.html for more
6 # information on its licensing.
8 warning_frame_number () {
9 echo "Files have differing numbers of frames!"
10 echo "$FILE1 has `ls -1 ${TEMPDIR}/FILE1/*ppm | wc -l` frames,"
11 echo "$FILE2 has `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` frames."
12 echo "Processing the first `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` frames."
16 TEMPDIR="/tmp/psnr_video"
17 WORKDIR=`pwd`
18 OUTFILE=psnr.dat
19 ERRFILE=errorsum.del
21 exit=0
22 if [[ `which pnmpsnr 2> /dev/null` = "" ]]
23 then
24 echo
25 echo "To use this script you have to install the program \"pnmpsnr\" which is"
26 echo " included in the netpbm package."
27 echo
28 exit=1
31 if [[ `which bc 2> /dev/null` = "" ]]
32 then
33 echo
34 echo "To use this script you have to install the GNU command line calculator \"bc\"."
35 echo
36 exit=1
39 if [ $# -le 1 ]; then
40 echo
41 echo "Usage: `basename $0` <file1> <file2> [<frames>] [<options1>] [<options2>]"
42 echo
43 echo " <file1> and <file2> are the files for which the PSNR should be calculated."
44 echo " [<frames>] is the number of frames to process, starting from frame 1."
45 echo " [<options1>] are additional MPlayer options for <file1>."
46 echo " [<options2>] are additional MPlayer options for <file2>."
47 echo
48 echo " Be aware that `basename $0` needs a lot of temporary space inside /tmp/."
49 echo
50 echo "Example:"
51 echo " ./`basename $0` ./orig.avi ./test.avi 250 \"\" \"-vf pp=ac\""
52 echo
54 exit=1
57 if [ "$exit" -eq 1 ]; then
58 exit 1
61 FILE1=$1
62 FILE2=$2
64 LastFrame=-1
65 if [ $# -ge 3 ]; then
66 LastFrame=$3
67 echo
68 echo "Will process $LastFrame frames."
71 if [ $# -ge 4 ]; then
72 FILE1_Options=$4
73 echo "MPlayer options for ${FILE1}: $FILE1_Options"
76 if [ $# -ge 5 ]; then
77 FILE2_Options=$5
78 echo "MPlayer options for ${FILE2}: $FILE2_Options"
82 mkdir -p ${TEMPDIR}/FILE1
83 mkdir -p ${TEMPDIR}/FILE2
85 ### File 1
86 echo
87 echo "############## $FILE1 #################"
89 cd ${TEMPDIR}/FILE1
91 rm -f *ppm
92 rm -f *del
94 if [ $LastFrame -ge 0 ]; then
95 mplayer $FILE1_Options -frames $LastFrame -nosound -vo pnm ${WORKDIR}/$FILE1 > /dev/null
96 else
97 mplayer $FILE1_Options -nosound -vo pnm ${WORKDIR}/$FILE1 > /dev/null
100 ### File 2
101 echo
102 echo "############## $FILE2 #################"
104 cd ${TEMPDIR}/FILE2
106 rm -f *ppm
108 if [ $LastFrame -ge 0 ]; then
109 mplayer $FILE2_Options -frames $LastFrame -nosound -vo pnm ${WORKDIR}/$FILE2 > /dev/null
110 else
111 mplayer $FILE2_Options -nosound -vo pnm ${WORKDIR}/$FILE2 > /dev/null
115 ### PSNR
117 echo
118 echo "############## PSNR Calculation #################"
120 if [[ `ls -1 ${TEMPDIR}/FILE1/*ppm | wc -l` = `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` ]]
121 then
122 echo
123 else
124 warning_frame_number
125 echo
129 cd ${TEMPDIR}/FILE2
130 #rm ../$OUTFILE
131 echo "File;Y;Cb;Cr" > ../$OUTFILE
132 echo "0" > $ERRFILE
134 for FILE in `ls -1 *.ppm`
136 echo $FILE
137 echo -n "$FILE" >> ../$OUTFILE
138 echo -n ";" >> ../$OUTFILE
140 YCBCR=`pnmpsnr ../FILE1/$FILE $FILE 2>&1 | tail -n 3 | cut -f 3 -d ':' | \
141 ( read Y X; read CB X; read CR X; echo "$Y;$CB;$CR;")`
142 Y=`echo $YCBCR | cut -f 1 -d ';'`
143 CB=`echo $YCBCR | cut -f 2 -d ';'`
144 CR=`echo $YCBCR | cut -f 3 -d ';'`
145 echo $YCBCR >> ../$OUTFILE
147 ALL=`echo "(-10)*l((e(-$Y/10*l(10))+e(-$CB/10*l(10))/4+e(-$CR/10*l(10))/4)/1.5)/l(10)" | bc -l`
148 echo "$ALL" >> ../$OUTFILE
149 ERROR=`echo "scale=30; (e(-1*$Y/10*l(10))+e(-1*$CB/10*l(10))/4+e(-1*$CR/10*l(10))/4)/1.5" | bc -l`
150 ERRORSUM=`cat $ERRFILE`
151 echo `echo "scale=30; $ERROR + $ERRORSUM" | bc -l` > $ERRFILE
153 i=$(($i+1))
154 if [[ $i = $LastFrame ]]
155 then
156 break
158 done
160 ERRORSUM=`cat $ERRFILE`
161 PSNR=`echo "-10*l($ERRORSUM/$i)/l(10)" | bc -l`
162 echo "PSNR:;$PSNR" >> ../$OUTFILE
164 cd ..
165 mv $OUTFILE ${WORKDIR}/
167 if [[ `ls -1 ${TEMPDIR}/FILE1/*ppm | wc -l` = `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` ]]
168 then
169 echo
170 else
171 warning_frame_number
172 echo
175 cd ..
176 rm -r ${TEMPDIR}
178 echo "Created ${WORKDIR}/$OUTFILE"
179 echo