]> git.sesse.net Git - ffmpeg/blob - tests/fate-run.sh
Merge commit '618d02c1fa9e74d490cace64a7d15762656b521c'
[ffmpeg] / 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 target_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 gen=${16:-no}
26 hwaccel=${17:-none}
27 report_type=${18:-standard}
28 keep=${19:-0}
29
30 outdir="tests/data/fate"
31 outfile="${outdir}/${test}"
32 errfile="${outdir}/${test}.err"
33 cmpfile="${outdir}/${test}.diff"
34 repfile="${outdir}/${test}.rep"
35
36 target_path(){
37     test ${1} = ${1#/} && p=${target_path}/
38     echo ${p}${1}
39 }
40
41 # $1=value1, $2=value2, $3=threshold
42 # prints 0 if absolute difference between value1 and value2 is <= threshold
43 compare(){
44     awk "BEGIN { v = $1 - $2; printf ((v < 0 ? -v : v) > $3) }"
45 }
46
47 do_tiny_psnr(){
48     psnr=$(tests/tiny_psnr "$1" "$2" $cmp_unit $cmp_shift 0) || return 1
49     val=$(expr "$psnr" : ".*$3: *\([0-9.]*\)")
50     size1=$(expr "$psnr" : '.*bytes: *\([0-9]*\)')
51     size2=$(expr "$psnr" : '.*bytes:[ 0-9]*/ *\([0-9]*\)')
52     val_cmp=$(compare $val $cmp_target $fuzz)
53     size_cmp=$(compare $size1 $size2 $size_tolerance)
54     if [ "$val_cmp" != 0 ] || [ "$size_cmp" != 0 ]; then
55         echo "$psnr"
56         if [ "$val_cmp" != 0 ]; then
57             echo "$3: |$val - $cmp_target| >= $fuzz"
58         fi
59         if [ "$size_cmp" != 0 ]; then
60             echo "size: |$size1 - $size2| >= $size_tolerance"
61         fi
62         return 1
63     fi
64 }
65
66 oneoff(){
67     do_tiny_psnr "$1" "$2" MAXDIFF
68 }
69
70 stddev(){
71     do_tiny_psnr "$1" "$2" stddev
72 }
73
74 oneline(){
75     printf '%s\n' "$1" | diff -u -b - "$2"
76 }
77
78 run(){
79     test "${V:-0}" -gt 0 && echo "$target_exec" $target_path/"$@" >&3
80     $target_exec $target_path/"$@"
81 }
82
83 runecho(){
84     test "${V:-0}" -gt 0 && echo "$target_exec" $target_path/"$@" >&3
85     $target_exec $target_path/"$@" >&3
86 }
87
88 probefmt(){
89     run ffprobe${PROGSUF} -show_entries format=format_name -print_format default=nw=1:nk=1 -v 0 "$@"
90 }
91
92 probetags(){
93     run ffprobe${PROGSUF} -show_entries format_tags -v 0 "$@"
94 }
95
96 runlocal(){
97     test "${V:-0}" -gt 0 && echo ${base}/"$@" ${base} >&3
98     ${base}/"$@" ${base}
99 }
100
101 probeframes(){
102     run ffprobe${PROGSUF} -show_frames -v 0 "$@"
103 }
104
105 probechapters(){
106     run ffprobe${PROGSUF} -show_chapters -v 0 "$@"
107 }
108
109 probegaplessinfo(){
110     filename="$1"
111     shift
112     run ffprobe${PROGSUF} -bitexact -select_streams a -show_entries format=start_time,duration:stream=index,start_pts,duration_ts -v 0 "$filename" "$@"
113     pktfile1="${outdir}/${test}.pkts"
114     framefile1="${outdir}/${test}.frames"
115     cleanfiles="$cleanfiles $pktfile1 $framefile1"
116     run ffprobe${PROGSUF} -bitexact -select_streams a -of compact -count_packets -show_entries packet=pts,dts,duration,flags:stream=nb_read_packets -v 0 "$filename" "$@" > "$pktfile1"
117     head -n 8 "$pktfile1"
118     tail -n 9 "$pktfile1"
119     run ffprobe${PROGSUF} -bitexact -select_streams a -of compact -count_frames -show_entries frame=pkt_pts,pkt_dts,best_effort_timestamp,pkt_duration,nb_samples:stream=nb_read_frames -v 0 "$filename" "$@" > "$framefile1"
120     head -n 8 "$framefile1"
121     tail -n 9 "$framefile1"
122 }
123
124 ffmpeg(){
125     dec_opts="-hwaccel $hwaccel -threads $threads -thread_type $thread_type"
126     ffmpeg_args="-nostdin -nostats -cpuflags $cpuflags"
127     for arg in $@; do
128         [ x${arg} = x-i ] && ffmpeg_args="${ffmpeg_args} ${dec_opts}"
129         ffmpeg_args="${ffmpeg_args} ${arg}"
130     done
131     run ffmpeg${PROGSUF} ${ffmpeg_args}
132 }
133
134 framecrc(){
135     ffmpeg "$@" -bitexact -f framecrc -
136 }
137
138 ffmetadata(){
139     ffmpeg "$@" -bitexact -f ffmetadata -
140 }
141
142 framemd5(){
143     ffmpeg "$@" -bitexact -f framemd5 -
144 }
145
146 crc(){
147     ffmpeg "$@" -f crc -
148 }
149
150 md5pipe(){
151     ffmpeg "$@" md5:
152 }
153
154 md5(){
155     encfile="${outdir}/${test}.out"
156     cleanfiles="$cleanfiles $encfile"
157     ffmpeg "$@" $encfile
158     do_md5sum $encfile | awk '{print $1}'
159 }
160
161 pcm(){
162     ffmpeg "$@" -vn -f s16le -
163 }
164
165 fmtstdout(){
166     fmt=$1
167     shift 1
168     ffmpeg -bitexact "$@" -f $fmt -
169 }
170
171 enc_dec_pcm(){
172     out_fmt=$1
173     dec_fmt=$2
174     pcm_fmt=$3
175     src_file=$(target_path $4)
176     shift 4
177     encfile="${outdir}/${test}.${out_fmt}"
178     cleanfiles=$encfile
179     encfile=$(target_path ${encfile})
180     ffmpeg -i $src_file "$@" -f $out_fmt -y ${encfile} || return
181     ffmpeg -bitexact -i ${encfile} -c:a pcm_${pcm_fmt} -fflags +bitexact -f ${dec_fmt} -
182 }
183
184 FLAGS="-flags +bitexact -sws_flags +accurate_rnd+bitexact -fflags +bitexact"
185 DEC_OPTS="-threads $threads -idct simple $FLAGS"
186 ENC_OPTS="-threads 1        -idct simple -dct fastint"
187
188 enc_dec(){
189     src_fmt=$1
190     srcfile=$2
191     enc_fmt=$3
192     enc_opt=$4
193     dec_fmt=$5
194     dec_opt=$6
195     encfile="${outdir}/${test}.${enc_fmt}"
196     decfile="${outdir}/${test}.out.${dec_fmt}"
197     cleanfiles="$cleanfiles $decfile"
198     test "$7" = -keep || cleanfiles="$cleanfiles $encfile"
199     tsrcfile=$(target_path $srcfile)
200     tencfile=$(target_path $encfile)
201     tdecfile=$(target_path $decfile)
202     ffmpeg -f $src_fmt $DEC_OPTS -i $tsrcfile $ENC_OPTS $enc_opt $FLAGS \
203         -f $enc_fmt -y $tencfile || return
204     do_md5sum $encfile
205     echo $(wc -c $encfile)
206     ffmpeg $8 $DEC_OPTS -i $tencfile $ENC_OPTS $dec_opt $FLAGS \
207         -f $dec_fmt -y $tdecfile || return
208     do_md5sum $decfile
209     tests/tiny_psnr $srcfile $decfile $cmp_unit $cmp_shift
210 }
211
212 transcode(){
213     src_fmt=$1
214     srcfile=$2
215     enc_fmt=$3
216     enc_opt=$4
217     final_decode=$5
218     encfile="${outdir}/${test}.${enc_fmt}"
219     test "$7" = -keep || cleanfiles="$cleanfiles $encfile"
220     tsrcfile=$(target_path $srcfile)
221     tencfile=$(target_path $encfile)
222     ffmpeg -f $src_fmt $DEC_OPTS -i $tsrcfile $ENC_OPTS $enc_opt $FLAGS \
223         -f $enc_fmt -y $tencfile || return
224     do_md5sum $encfile
225     echo $(wc -c $encfile)
226     ffmpeg $DEC_OPTS -i $encfile $ENC_OPTS $FLAGS $final_decode \
227         -f framecrc - || return
228 }
229
230 stream_remux(){
231     src_fmt=$1
232     srcfile=$2
233     enc_fmt=$3
234     stream_maps=$4
235     final_decode=$5
236     encfile="${outdir}/${test}.${enc_fmt}"
237     test "$7" = -keep || cleanfiles="$cleanfiles $encfile"
238     tsrcfile=$(target_path $srcfile)
239     tencfile=$(target_path $encfile)
240     ffmpeg -f $src_fmt -i $tsrcfile $stream_maps -codec copy $FLAGS \
241         -f $enc_fmt -y $tencfile || return
242     ffmpeg $DEC_OPTS -i $encfile $ENC_OPTS $FLAGS $final_decode \
243         -f framecrc - || return
244 }
245
246 # FIXME: There is a certain duplication between the avconv-related helper
247 # functions above and below that should be refactored.
248 ffmpeg2="$target_exec ${target_path}/ffmpeg"
249 raw_src="${target_path}/tests/vsynth1/%02d.pgm"
250 pcm_src="${target_path}/tests/data/asynth1.sw"
251 crcfile="tests/data/$test.lavf.crc"
252 target_crcfile="${target_path}/$crcfile"
253
254 [ "${V-0}" -gt 0 ] && echov=echov || echov=:
255
256 echov(){
257     echo "$@" >&3
258 }
259
260 AVCONV_OPTS="-nostdin -nostats -y -cpuflags $cpuflags -threads $threads"
261 DEC_OPTS="-flags +bitexact -idct simple -sws_flags +accurate_rnd+bitexact -fflags +bitexact"
262 ENC_OPTS="$DEC_OPTS -threads $threads -dct fastint"
263
264 run_avconv(){
265     $echov $ffmpeg2 $AVCONV_OPTS $*
266     $ffmpeg2 $AVCONV_OPTS $*
267 }
268
269 do_avconv(){
270     f="$1"
271     shift
272     set -- $* ${target_path}/$f
273     run_avconv $*
274     do_md5sum $f
275     echo $(wc -c $f)
276 }
277
278 do_avconv_crc(){
279     f="$1"
280     shift
281     run_avconv $* -f crc "$target_crcfile"
282     echo "$f $(cat $crcfile)"
283 }
284
285 lavf_audio(){
286     t="${test#lavf-}"
287     outdir="tests/data/lavf"
288     file=${outdir}/lavf.$t
289     do_avconv $file $DEC_OPTS $1 -ar 44100 -f s16le -i $pcm_src "$ENC_OPTS -metadata title=lavftest" -t 1 -qscale 10 $2
290     do_avconv_crc $file $DEC_OPTS $3 -i $target_path/$file
291 }
292
293 lavf_container(){
294     t="${test#lavf-}"
295     outdir="tests/data/lavf"
296     file=${outdir}/lavf.$t
297     do_avconv $file $DEC_OPTS -f image2 -c:v pgmyuv -i $raw_src $DEC_OPTS -ar 44100 -f s16le $1 -i $pcm_src "$ENC_OPTS -metadata title=lavftest" -b:a 64k -t 1 -qscale:v 10 $2
298     test $3 = "disable_crc" ||
299         do_avconv_crc $file $DEC_OPTS -i $target_path/$file $3
300 }
301
302 lavf_container_attach() {          lavf_container "" "$1 -attach ${raw_src%/*}/00.pgm -metadata:s:t mimetype=image/x-portable-greymap"; }
303 lavf_container_timecode_nodrop() { lavf_container "" "$1 -timecode 02:56:14:13"; }
304 lavf_container_timecode_drop()   { lavf_container "" "$1 -timecode 02:56:14.13 -r 30000/1001"; }
305
306 lavf_container_timecode()
307 {
308     lavf_container_timecode_nodrop "$@"
309     lavf_container_timecode_drop "$@"
310     lavf_container "" "$1"
311 }
312
313 lavf_image(){
314     t="${test#lavf-}"
315     outdir="tests/data/images/$t"
316     mkdir -p "$outdir"
317     file=${outdir}/%02d.$t
318     run_avconv $DEC_OPTS $1 -f image2 -c:v pgmyuv -i $raw_src "$ENC_OPTS -metadata title=lavftest" $2 -frames 13 -y -qscale 10 $target_path/$file
319     do_md5sum ${outdir}/02.$t
320     do_avconv_crc $file $DEC_OPTS $2 -i $target_path/$file
321     echo $(wc -c ${outdir}/02.$t)
322 }
323
324 lavf_image2pipe(){
325     t="${test#lavf-}"
326     t="${t%pipe}"
327     outdir="tests/data/lavf"
328     file=${outdir}/${t}pipe.$t
329     do_avconv $file $DEC_OPTS -f image2 -c:v pgmyuv -i $raw_src -f image2pipe "$ENC_OPTS -metadata title=lavftest" -t 1 -qscale 10
330     do_avconv_crc $file $DEC_OPTS -f image2pipe -i $target_path/$file
331 }
332
333 lavffatetest(){
334     t="${test#lavf-fate-}"
335     ref=${base}/ref/lavf-fate/$t
336     ${base}/lavf-regression.sh $t lavf-fate tests/vsynth1 "$target_exec" "$target_path" "$threads" "$thread_type" "$cpuflags" "$target_samples"
337 }
338
339 lavftest(){
340     t="${test#lavf-}"
341     ref=${base}/ref/lavf/$t
342     ${base}/lavf-regression.sh $t lavf tests/vsynth1 "$target_exec" "$target_path" "$threads" "$thread_type" "$cpuflags" "$target_samples"
343 }
344
345 refcmp_metadata(){
346     refcmp=$1
347     pixfmt=$2
348     fuzz=${3:-0.001}
349     ffmpeg $FLAGS $ENC_OPTS \
350         -lavfi "testsrc2=size=300x200:rate=1:duration=5,format=${pixfmt},split[ref][tmp];[tmp]avgblur=4[enc];[enc][ref]${refcmp},metadata=print:file=-" \
351         -f null /dev/null | awk -v ref=${ref} -v fuzz=${fuzz} -f ${base}/refcmp-metadata.awk -
352 }
353
354 pixfmt_conversion(){
355     conversion="${test#pixfmt-}"
356     outdir="tests/data/pixfmt"
357     raw_dst="$outdir/$conversion.out.yuv"
358     file=${outdir}/${conversion}.yuv
359     run_avconv $DEC_OPTS -r 1 -f image2 -c:v pgmyuv -i $raw_src \
360                $ENC_OPTS -f rawvideo -t 1 -s 352x288 -pix_fmt $conversion $target_path/$raw_dst
361     do_avconv $file $DEC_OPTS -f rawvideo -s 352x288 -pix_fmt $conversion -i $target_path/$raw_dst \
362               $ENC_OPTS -f rawvideo -s 352x288 -pix_fmt yuv444p
363 }
364
365 video_filter(){
366     filters=$1
367     shift
368     label=${test#filter-}
369     raw_src="${target_path}/tests/vsynth1/%02d.pgm"
370     printf '%-20s' $label
371     ffmpeg $DEC_OPTS -f image2 -vcodec pgmyuv -i $raw_src \
372         $FLAGS $ENC_OPTS -vf "$filters" -vcodec rawvideo -frames:v 5 $* -f nut md5:
373 }
374
375 pixfmts(){
376     filter=${test#filter-pixfmts-}
377     filter=${filter%_*}
378     filter_args=$1
379     prefilter_chain=$2
380     nframes=${3:-1}
381
382     showfiltfmts="$target_exec $target_path/libavfilter/tests/filtfmts"
383     scale_exclude_fmts=${outfile}_scale_exclude_fmts
384     scale_in_fmts=${outfile}_scale_in_fmts
385     scale_out_fmts=${outfile}_scale_out_fmts
386     in_fmts=${outfile}_in_fmts
387
388     # exclude pixel formats which are not supported as input
389     $showfiltfmts scale | awk -F '[ \r]' '/^INPUT/{ fmt=substr($3, 5); print fmt }' | sort >$scale_in_fmts
390     $showfiltfmts scale | awk -F '[ \r]' '/^OUTPUT/{ fmt=substr($3, 5); print fmt }' | sort >$scale_out_fmts
391     comm -12 $scale_in_fmts $scale_out_fmts >$scale_exclude_fmts
392
393     $showfiltfmts $filter | awk -F '[ \r]' '/^INPUT/{ fmt=substr($3, 5); print fmt }' | sort >$in_fmts
394     pix_fmts=$(comm -12 $scale_exclude_fmts $in_fmts)
395
396     outertest=$test
397     for pix_fmt in $pix_fmts; do
398         test=$pix_fmt
399         video_filter "${prefilter_chain}format=$pix_fmt,$filter=$filter_args" -pix_fmt $pix_fmt -frames:v $nframes
400     done
401
402     rm $in_fmts $scale_in_fmts $scale_out_fmts $scale_exclude_fmts
403     test=$outertest
404 }
405
406 gapless(){
407     sample=$(target_path $1)
408     extra_args=$2
409
410     decfile1="${outdir}/${test}.out-1"
411     decfile2="${outdir}/${test}.out-2"
412     decfile3="${outdir}/${test}.out-3"
413     cleanfiles="$cleanfiles $decfile1 $decfile2 $decfile3"
414
415     # test packet data
416     ffmpeg $extra_args -i "$sample" -bitexact -c:a copy -f framecrc -y $decfile1
417     do_md5sum $decfile1
418     # test decoded (and cut) data
419     ffmpeg $extra_args -i "$sample" -bitexact -f wav md5:
420     # the same as above again, with seeking to the start
421     ffmpeg $extra_args -ss 0 -seek_timestamp 1 -i "$sample" -bitexact -c:a copy -f framecrc -y $decfile2
422     do_md5sum $decfile2
423     ffmpeg $extra_args -ss 0 -seek_timestamp 1 -i "$sample" -bitexact -f wav md5:
424     # test packet data, with seeking to a specific position
425     ffmpeg $extra_args -ss 5 -seek_timestamp 1 -i "$sample" -bitexact -c:a copy -f framecrc -y $decfile3
426     do_md5sum $decfile3
427 }
428
429 gaplessenc(){
430     sample=$(target_path $1)
431     format=$2
432     codec=$3
433
434     file1="${outdir}/${test}.out-1"
435     cleanfiles="$cleanfiles $file1"
436
437     # test data after reencoding
438     ffmpeg -i "$sample" -bitexact -map 0:a -c:a $codec -f $format -y "$file1"
439     probegaplessinfo "$file1"
440 }
441
442 audio_match(){
443     sample=$(target_path $1)
444     trefile=$(target_path $2)
445     extra_args=$3
446
447     decfile="${outdir}/${test}.wav"
448     cleanfiles="$cleanfiles $decfile"
449
450     ffmpeg -i "$sample" -bitexact $extra_args -y $decfile
451     tests/audiomatch $decfile $trefile
452 }
453
454 concat(){
455     template=$1
456     sample=$2
457     mode=$3
458     extra_args=$4
459
460     concatfile="${outdir}/${test}.ffconcat"
461     packetfile="${outdir}/${test}.ffprobe"
462     cleanfiles="$concatfile $packetfile"
463
464     awk "{gsub(/%SRCFILE%/, \"$sample\"); print}" $template > $concatfile
465
466     if [ "$mode" = "md5" ]; then
467         run ffprobe${PROGSUF} -bitexact -show_streams -show_packets -v 0 -fflags keepside -safe 0 $extra_args $concatfile | tr -d '\r' > $packetfile
468         do_md5sum $packetfile
469     else
470         run ffprobe${PROGSUF} -bitexact -show_streams -show_packets -v 0 -of compact=p=0:nk=1 -fflags keepside -safe 0 $extra_args $concatfile
471     fi
472 }
473
474 null(){
475     :
476 }
477
478 # Disable globbing: command arguments may contain globbing characters and
479 # must be kept verbatim
480 set -f
481
482 exec 3>&2
483 eval $command >"$outfile" 2>$errfile
484 err=$?
485
486 if [ $err -gt 128 ]; then
487     sig=$(kill -l $err 2>/dev/null)
488     test "${sig}" = "${sig%[!A-Za-z]*}" || unset sig
489 fi
490
491 if test -e "$ref" || test $cmp = "oneline" || test $cmp = "null" || test $cmp = "grep" ; then
492     case $cmp in
493         diff)   diff -u -b "$ref" "$outfile"            >$cmpfile ;;
494         rawdiff)diff -u    "$ref" "$outfile"            >$cmpfile ;;
495         oneoff) oneoff     "$ref" "$outfile"            >$cmpfile ;;
496         stddev) stddev     "$ref" "$outfile"            >$cmpfile ;;
497         oneline)oneline    "$ref" "$outfile"            >$cmpfile ;;
498         grep)   grep       "$ref" "$errfile"            >$cmpfile ;;
499         null)   cat               "$outfile"            >$cmpfile ;;
500     esac
501     cmperr=$?
502     test $err = 0 && err=$cmperr
503     if [ "$report_type" = "ignore" ]; then
504         test $err = 0 || echo "IGNORE\t${test}" && err=0 && unset sig
505     else
506         test $err = 0 || cat $cmpfile
507     fi
508 else
509     echo "reference file '$ref' not found"
510     err=1
511 fi
512
513 if [ $err -eq 0 ] && test $report_type = "standard" ; then
514     unset cmpo erro
515 else
516     cmpo="$($base64 <$cmpfile)"
517     erro="$($base64 <$errfile)"
518 fi
519 echo "${test}:${sig:-$err}:$cmpo:$erro" >$repfile
520
521 if test $err != 0 && test $gen != "no" ; then
522     echo "GEN     $ref"
523     cp -f "$outfile" "$ref"
524     err=$?
525 fi
526
527 if test $err = 0; then
528     if test $keep = 0; then
529         rm -f $outfile $errfile $cmpfile $cleanfiles
530     fi
531 elif test $gen = "no"; then
532     echo "Test $test failed. Look at $errfile for details."
533     test "${V:-0}" -gt 0 && cat $errfile
534 else
535     echo "Updating reference failed, possibly no output file was generated."
536 fi
537 exit $err