fate: allow tests to specify unit size for psnr comparison
[ffmpeg.git] / tests / fate-run.sh
1 #! /bin/sh
2
3 export LC_ALL=C
4
5 base=$(dirname $0)
6 . "${base}/md5.sh"
7
8 base64=tests/base64
9
10 test="${1#fate-}"
11 samples=$2
12 target_exec=$3
13 target_path=$4
14 command=$5
15 cmp=${6:-diff}
16 ref=${7:-"${base}/ref/fate/${test}"}
17 fuzz=${8:-1}
18 threads=${9:-1}
19 thread_type=${10:-frame+slice}
20 cpuflags=${11:-all}
21 cmp_shift=${12:-0}
22 cmp_target=${13:-0}
23 size_tolerance=${14:-0}
24 cmp_unit=${15:-2}
25
26 outdir="tests/data/fate"
27 outfile="${outdir}/${test}"
28 errfile="${outdir}/${test}.err"
29 cmpfile="${outdir}/${test}.diff"
30 repfile="${outdir}/${test}.rep"
31
32 target_path(){
33     test ${1} = ${1#/} && p=${target_path}/
34     echo ${p}${1}
35 }
36
37 # $1=value1, $2=value2, $3=threshold
38 # prints 0 if absolute difference between value1 and value2 is <= threshold
39 compare(){
40     echo "scale=2; v = $1 - $2; if (v < 0) v = -v; if (v > $3) r = 1; r" | bc
41 }
42
43 do_tiny_psnr(){
44     psnr=$(tests/tiny_psnr "$1" "$2" $cmp_unit $cmp_shift 0)
45     val=$(expr "$psnr" : ".*$3: *\([0-9.]*\)")
46     size1=$(expr "$psnr" : '.*bytes: *\([0-9]*\)')
47     size2=$(expr "$psnr" : '.*bytes:[ 0-9]*/ *\([0-9]*\)')
48     val_cmp=$(compare $val $cmp_target $fuzz)
49     size_cmp=$(compare $size1 $size2 $size_tolerance)
50     if [ "$val_cmp" != 0 ] || [ "$size_cmp" != 0 ]; then
51         echo "$psnr"
52         return 1
53     fi
54 }
55
56 oneoff(){
57     do_tiny_psnr "$1" "$2" MAXDIFF
58 }
59
60 stddev(){
61     do_tiny_psnr "$1" "$2" stddev
62 }
63
64 oneline(){
65     printf '%s\n' "$1" | diff -u -b - "$2"
66 }
67
68 run(){
69     test "${V:-0}" -gt 0 && echo "$target_exec" $target_path/"$@" >&3
70     $target_exec $target_path/"$@"
71 }
72
73 probefmt(){
74     run avprobe -show_format_entry format_name -v 0 "$@"
75 }
76
77 avconv(){
78     run avconv -nostats -threads $threads -thread_type $thread_type -cpuflags $cpuflags "$@"
79 }
80
81 framecrc(){
82     avconv "$@" -f framecrc -
83 }
84
85 framemd5(){
86     avconv "$@" -f framemd5 -
87 }
88
89 crc(){
90     avconv "$@" -f crc -
91 }
92
93 md5(){
94     avconv "$@" md5:
95 }
96
97 pcm(){
98     avconv "$@" -vn -f s16le -
99 }
100
101 enc_dec_pcm(){
102     out_fmt=$1
103     dec_fmt=$2
104     pcm_fmt=$3
105     src_file=$(target_path $4)
106     shift 4
107     encfile="${outdir}/${test}.${out_fmt}"
108     cleanfiles=$encfile
109     encfile=$(target_path ${encfile})
110     avconv -i $src_file "$@" -f $out_fmt -y ${encfile} || return
111     avconv -f $out_fmt -i ${encfile} -c:a pcm_${pcm_fmt} -f ${dec_fmt} -
112 }
113
114 regtest(){
115     t="${test#$2-}"
116     ref=${base}/ref/$2/$t
117     ${base}/${1}-regression.sh $t $2 $3 "$target_exec" "$target_path" "$threads" "$thread_type" "$cpuflags"
118 }
119
120 codectest(){
121     regtest codec $1 tests/$1
122 }
123
124 lavftest(){
125     regtest lavf lavf tests/vsynth1
126 }
127
128 lavfitest(){
129     cleanfiles="tests/data/lavfi/${test#lavfi-}.nut"
130     regtest lavfi lavfi tests/vsynth1
131 }
132
133 seektest(){
134     t="${test#seek-}"
135     ref=${base}/ref/seek/$t
136     case $t in
137         image_*) file="tests/data/images/${t#image_}/%02d.${t#image_}" ;;
138         *)       file=$(echo $t | tr _ '?')
139                  for d in acodec vsynth2 lavf; do
140                      test -f tests/data/$d/$file && break
141                  done
142                  file=$(echo tests/data/$d/$file)
143                  ;;
144     esac
145     run libavformat/seek-test $target_path/$file
146 }
147
148 mkdir -p "$outdir"
149
150 exec 3>&2
151 $command > "$outfile" 2>$errfile
152 err=$?
153
154 if [ $err -gt 128 ]; then
155     sig=$(kill -l $err 2>/dev/null)
156     test "${sig}" = "${sig%[!A-Za-z]*}" || unset sig
157 fi
158
159 if test -e "$ref" || test $cmp = "oneline" ; then
160     case $cmp in
161         diff)   diff -u -b "$ref" "$outfile"            >$cmpfile ;;
162         oneoff) oneoff     "$ref" "$outfile"            >$cmpfile ;;
163         stddev) stddev     "$ref" "$outfile"            >$cmpfile ;;
164         oneline)oneline    "$ref" "$outfile"            >$cmpfile ;;
165         null)   cat               "$outfile"            >$cmpfile ;;
166     esac
167     cmperr=$?
168     test $err = 0 && err=$cmperr
169     test $err = 0 || cat $cmpfile
170 else
171     echo "reference file '$ref' not found"
172     err=1
173 fi
174
175 echo "${test}:${sig:-$err}:$($base64 <$cmpfile):$($base64 <$errfile)" >$repfile
176
177 test $err = 0 && rm -f $outfile $errfile $cmpfile $cleanfiles
178 exit $err