fate: convert codec-regression.sh to makefile rules
[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 FLAGS="-flags +bitexact -sws_flags +accurate_rnd+bitexact"
115 DEC_OPTS="-threads $threads -idct simple $FLAGS"
116 ENC_OPTS="-threads 1        -idct simple -dct fastint"
117
118 enc_dec(){
119     src_fmt=$1
120     srcfile=$2
121     enc_fmt=$3
122     enc_opt=$4
123     dec_fmt=$5
124     dec_opt=$6
125     encfile="${outdir}/${test}.${enc_fmt}"
126     decfile="${outdir}/${test}.out.${dec_fmt}"
127     cleanfiles="$cleanfiles $decfile"
128     test "$7" = -keep || cleanfiles="$cleanfiles $encfile"
129     tsrcfile=$(target_path $srcfile)
130     tencfile=$(target_path $encfile)
131     tdecfile=$(target_path $decfile)
132     avconv -f $src_fmt $DEC_OPTS -i $tsrcfile $ENC_OPTS $enc_opt $FLAGS \
133         -f $enc_fmt -y $tencfile || return
134     do_md5sum $encfile
135     echo $(wc -c $encfile)
136     avconv $DEC_OPTS -i $tencfile $ENC_OPTS $dec_opt $FLAGS \
137         -f $dec_fmt -y $tdecfile || return
138     do_md5sum $decfile
139     tests/tiny_psnr $srcfile $decfile $cmp_unit $cmp_shift
140 }
141
142 regtest(){
143     t="${test#$2-}"
144     ref=${base}/ref/$2/$t
145     ${base}/${1}-regression.sh $t $2 $3 "$target_exec" "$target_path" "$threads" "$thread_type" "$cpuflags"
146 }
147
148 lavftest(){
149     regtest lavf lavf tests/vsynth1
150 }
151
152 lavfitest(){
153     cleanfiles="tests/data/lavfi/${test#lavfi-}.nut"
154     regtest lavfi lavfi tests/vsynth1
155 }
156
157 seektest(){
158     t="${test#seek-}"
159     ref=${base}/ref/seek/$t
160     case $t in
161         image_*) file="tests/data/images/${t#image_}/%02d.${t#image_}" ;;
162         *)       file=$(echo $t | tr _ '?')
163                  for d in fate/acodec- fate/vsynth2- lavf/; do
164                      test -f tests/data/$d$file && break
165                  done
166                  file=$(echo tests/data/$d$file)
167                  ;;
168     esac
169     run libavformat/seek-test $target_path/$file
170 }
171
172 mkdir -p "$outdir"
173
174 exec 3>&2
175 eval $command >"$outfile" 2>$errfile
176 err=$?
177
178 if [ $err -gt 128 ]; then
179     sig=$(kill -l $err 2>/dev/null)
180     test "${sig}" = "${sig%[!A-Za-z]*}" || unset sig
181 fi
182
183 if test -e "$ref" || test $cmp = "oneline" ; then
184     case $cmp in
185         diff)   diff -u -b "$ref" "$outfile"            >$cmpfile ;;
186         oneoff) oneoff     "$ref" "$outfile"            >$cmpfile ;;
187         stddev) stddev     "$ref" "$outfile"            >$cmpfile ;;
188         oneline)oneline    "$ref" "$outfile"            >$cmpfile ;;
189         null)   cat               "$outfile"            >$cmpfile ;;
190     esac
191     cmperr=$?
192     test $err = 0 && err=$cmperr
193     test $err = 0 || cat $cmpfile
194 else
195     echo "reference file '$ref' not found"
196     err=1
197 fi
198
199 echo "${test}:${sig:-$err}:$($base64 <$cmpfile):$($base64 <$errfile)" >$repfile
200
201 test $err = 0 && rm -f $outfile $errfile $cmpfile $cleanfiles
202 exit $err