aboutsummaryrefslogtreecommitdiffhomepage
path: root/TOOLS/psnr-video.sh
blob: 95701d45cbefdb0ae1bb34c9e6307b202a50e349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
# Helper script to ease comparing the PSNR between two video files
# Copyleft 2005 by Matthias Wieser
# Copyleft 2005 by Ivo
# This file comes under GPL, see http://www.gnu.org/copyleft/gpl.html for more
# information on its licensing.

warning_frame_number () { 
	echo "Files have differing numbers of frames!"
	echo "$FILE1 has `ls -1 ${TEMPDIR}/FILE1/*ppm | wc -l` frames,"
	echo "$FILE2 has `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` frames."
	echo "Processing the first `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` frames."
} 


TEMPDIR="/tmp/psnr_video"
WORKDIR=`pwd`

exit=0
if [[ `which pnmpsnr 2> /dev/null` = "" ]]
then
	echo
	echo "To use this script you have to install the program \"pnmpsnr\" which is"
	echo " included in the netpbm package."
	echo
	exit=1
fi

if [[ `which bc 2> /dev/null` = "" ]]
then
	echo
	echo "To use this script you have to install the GNU command line calculator \"bc\"."
	echo
	exit=1
fi

if [ $# -le 1 ]; then
   echo
   echo "Usage: `basename $0` <file1> <file2> [<frames>] [<options1>] [<options2>]"
   echo
   echo " <file1> and <file2> are the files for which the PSNR should be calculated."
   echo " [<frames>]          is the number of frames to process, starting from frame 1."
   echo " [<options1>]        are additional MPlayer options for <file1>."
   echo " [<options2>]        are additional MPlayer options for <file2>."
   echo
   echo " Be aware that `basename $0` needs a lot of temporary space inside /tmp/."
   echo
   echo "Example:"
   echo "        ./`basename $0` ./orig.avi ./test.avi 250 \"\" \"-vf pp=ac\""
   echo

   exit=1
fi

if [ "$exit" -eq 1 ]; then
	exit 1
fi

FILE1=$1
FILE2=$2

LastFrame=-1
if [ $# -ge 3 ]; then
	LastFrame=$3
	echo
	echo "Will process $LastFrame frames."
fi

if [ $# -ge 4 ]; then
	FILE1_Options=$4
	echo "MPlayer options for ${FILE1}: $FILE1_Options"
fi

if [ $# -ge 5 ]; then
	FILE2_Options=$5
	echo "Mplayer options for ${FILE2}: $FILE2_Options"
fi


mkdir -p ${TEMPDIR}/FILE1
mkdir -p ${TEMPDIR}/FILE2

###  File 1
echo
echo "############## $FILE1 #################"

cd ${TEMPDIR}/FILE1

rm -f *ppm
rm -f *del

if [ $LastFrame -ge 0 ]; then
	mplayer $FILE1_Options -frames $LastFrame -nosound -vo pnm ${WORKDIR}/$FILE1 >/dev/null
else
	mplayer $FILE1_Options -nosound -vo pnm ${WORKDIR}/$FILE1 > /dev/null
fi

###  File 2
echo
echo "############## $FILE2 #################"

cd ${TEMPDIR}/FILE2

rm *ppm 2> /dev/null

if [ $LastFrame -ge 0 ]; then
	mplayer $FILE2_Options -frames $LastFrame -nosound -vo pnm ${WORKDIR}/$FILE2 >/dev/null
else
	mplayer $FILE2_Options -nosound -vo pnm ${WORKDIR}/$FILE2 >/dev/null
fi


###  PSNR

echo
echo "############## PSNR Calculation #################"

if [[ `ls -1 ${TEMPDIR}/FILE1/*ppm | wc -l` = `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` ]]
then
	echo 
else
	warning_frame_number
	echo
fi


cd ${TEMPDIR}/FILE2
#rm ../psnr.dat
echo "File;Y;Cb;Cr" >../psnr.dat
echo "0" > errorsum.del
i=0
for FILE in `ls -1 *.ppm`
        do
        echo $FILE
                echo -n "$FILE">>../psnr.dat
                echo -n ";">>../psnr.dat

        YCBCR=`pnmpsnr ../FILE1/$FILE $FILE 2>&1 | tail -n 3 | cut -f 3 -d ':' | \
            ( read Y X; read CB X; read CR X; echo "$Y;$CB;$CR;")`
        Y=`echo $YCBCR | cut -f 1 -d ';'`
        CB=`echo $YCBCR | cut -f 2 -d ';'`
        CR=`echo $YCBCR | cut -f 3 -d ';'`
        echo $YCBCR >>../psnr.dat

        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`
        echo "$ALL">>../psnr.dat
        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`
        ERRORSUM=`cat errorsum.del`
        echo `echo "scale=30; $ERROR + $ERRORSUM"|bc -l` > errorsum.del

        i=$(($i+1))
	if [[ $i = $LastFrame ]]
	then
		break
	fi
done

ERRORSUM=`cat errorsum.del`
PSNR=`echo "-10*l($ERRORSUM/$i)/l(10)" | bc -l`
echo "PSNR:;$PSNR">>../psnr.dat

cd ..
mv psnr.dat ${WORKDIR}/

if [[ `ls -1 ${TEMPDIR}/FILE1/*ppm | wc -l` = `ls -1 ${TEMPDIR}/FILE2/*ppm | wc -l` ]]
then
        echo
else
	warning_frame_number
        echo
fi

cd ..
rm -r ${TEMPDIR}

echo "Created ${WORKDIR}/psnr.dat"
echo