xref: /aosp_15_r20/external/flac/test/test_streams.sh (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1#!/bin/sh -e
2
3#  FLAC - Free Lossless Audio Codec
4#  Copyright (C) 2001-2009  Josh Coalson
5#  Copyright (C) 2011-2023  Xiph.Org Foundation
6#
7#  This file is part the FLAC project.  FLAC is comprised of several
8#  components distributed under different licenses.  The codec libraries
9#  are distributed under Xiph.Org's BSD-like license (see the file
10#  COPYING.Xiph in this distribution).  All other programs, libraries, and
11#  plugins are distributed under the GPL (see COPYING.GPL).  The documentation
12#  is distributed under the Gnu FDL (see COPYING.FDL).  Each file in the
13#  FLAC distribution contains at the top the terms under which it may be
14#  distributed.
15#
16#  Since this particular file is relevant to all components of FLAC,
17#  it may be distributed under the Xiph.Org license, which is the least
18#  restrictive of those mentioned above.  See the file COPYING.Xiph in this
19#  distribution.
20
21. ./common.sh
22
23PATH=../src/flac:$PATH
24PATH=../src/test_streams:$PATH
25PATH=../objs/$BUILD/bin:$PATH
26
27if [ -z "$FLAC__TEST_LEVEL" ] ; then
28	FLAC__TEST_LEVEL=1
29fi
30
31flac${EXE} --help 1>/dev/null 2>/dev/null || die "ERROR can't find flac executable"
32
33run_flac ()
34{
35	if [ "$FLAC__TEST_WITH_VALGRIND" = yes ] ; then
36		echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 flac $*" >>test_streams.valgrind.log
37		valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 flac --no-error-on-compression-fail $* 4>>test_streams.valgrind.log
38	else
39		flac${EXE} --no-error-on-compression-fail $*
40	fi
41}
42
43echo "Generating streams..."
44if [ ! -f wacky1.wav ] ; then
45	test_streams || die "ERROR: missing files"
46fi
47
48#
49# single-file test routines
50#
51
52test_file ()
53{
54	name=$1
55	channels=$2
56	bps=$3
57	encode_options="$4"
58
59	echo $ECHO_N "$name (--channels=$channels --bps=$bps $encode_options): encode..." $ECHO_C
60	cmd="run_flac --verify --silent --force --force-raw-format --endian=little --sign=signed --sample-rate=44100 --bps=$bps --channels=$channels $encode_options --no-padding $name.raw"
61	echo "### ENCODE $name #######################################################" >> ./streams.log
62	echo "###    cmd=$cmd" >> ./streams.log
63	$cmd 2>>./streams.log || die "ERROR during encode of $name"
64
65	echo $ECHO_N "decode..." $ECHO_C
66	cmd="run_flac --silent --force --endian=little --sign=signed --decode --force-raw-format --output-name=$name.cmp $name.flac"
67	echo "### DECODE $name #######################################################" >> ./streams.log
68	echo "###    cmd=$cmd" >> ./streams.log
69	$cmd 2>>./streams.log || die "ERROR during decode of $name"
70
71	ls -1l $name.raw >> ./streams.log
72	ls -1l $name.flac >> ./streams.log
73	ls -1l $name.cmp >> ./streams.log
74
75	echo $ECHO_N "compare..." $ECHO_C
76	cmp $name.raw $name.cmp || die "ERROR during compare of $name"
77
78	echo OK
79}
80
81test_file_piped ()
82{
83	name=$1
84	channels=$2
85	bps=$3
86	encode_options="$4"
87
88	if [ "$(env | grep -ic '^comspec=')" != 0 ] ; then
89		is_win=yes
90	else
91		is_win=no
92	fi
93
94	echo $ECHO_N "$name: encode via pipes..." $ECHO_C
95	if [ $is_win = yes ] ; then
96		cmd="run_flac --verify --silent --force --force-raw-format --endian=little --sign=signed --sample-rate=44100 --bps=$bps --channels=$channels $encode_options --no-padding --stdout $name.raw"
97		echo "### ENCODE $name #######################################################" >> ./streams.log
98		echo "###    cmd=$cmd" >> ./streams.log
99		$cmd 1>$name.flac 2>>./streams.log || die "ERROR during encode of $name"
100	else
101		cmd="run_flac --verify --silent --force --force-raw-format --endian=little --sign=signed --sample-rate=44100 --bps=$bps --channels=$channels $encode_options --no-padding --stdout -"
102		echo "### ENCODE $name #######################################################" >> ./streams.log
103		echo "###    cmd=$cmd" >> ./streams.log
104		$cmd < $name.raw 1>$name.flac 2>>./streams.log || die "ERROR during encode of $name"
105	fi
106	echo $ECHO_N "decode via pipes..." $ECHO_C
107	if [ $is_win = yes ] ; then
108		cmd="run_flac --silent --force --endian=little --sign=signed --decode --force-raw-format --stdout $name.flac"
109		echo "### DECODE $name #######################################################" >> ./streams.log
110		echo "###    cmd=$cmd" >> ./streams.log
111		$cmd 1>$name.cmp 2>>./streams.log || die "ERROR during decode of $name"
112	else
113		cmd="run_flac --silent --force --endian=little --sign=signed --decode --force-raw-format --stdout -"
114		echo "### DECODE $name #######################################################" >> ./streams.log
115		echo "###    cmd=$cmd" >> ./streams.log
116		$cmd < $name.flac 1>$name.cmp 2>>./streams.log || die "ERROR during decode of $name"
117	fi
118	ls -1l $name.raw >> ./streams.log
119	ls -1l $name.flac >> ./streams.log
120	ls -1l $name.cmp >> ./streams.log
121
122	echo $ECHO_N "compare..." $ECHO_C
123	cmp $name.raw $name.cmp || die "ERROR during compare of $name"
124
125	echo OK
126}
127
128test_corrupted_file ()
129{
130	name=$1
131	channels=$2
132	bps=$3
133	encode_options="$4"
134
135	echo $ECHO_N "$name (--channels=$channels --bps=$bps $encode_options): encode..." $ECHO_C
136	cmd="run_flac --verify --silent --no-padding --force --force-raw-format --endian=little --sign=signed --sample-rate=44100 --bps=$bps --channels=$channels $encode_options --no-padding $name.raw"
137	echo "### ENCODE $name #######################################################" >> ./streams.log
138	echo "###    cmd=$cmd" >> ./streams.log
139	$cmd 2>>./streams.log || die "ERROR during encode of $name"
140
141	filesize=$(wc -c < $name.flac)
142	bs=$((filesize/13))
143
144	# Overwrite with 'garbagegarbagegarbage....'
145	yes garbage 2>/dev/null | dd of=$name.flac conv=notrunc bs=$bs seek=1 count=2 2>> ./streams.log
146	# Overwrite with 0x00
147	dd if=/dev/zero of=$name.flac conv=notrunc bs=$bs seek=4 count=2 2>> ./streams.log
148	# Overwrite with 0xFF
149	tr '\0' '\377' < /dev/zero | dd of=$name.flac conv=notrunc bs=$bs seek=7 count=2 2>> ./streams.log
150	# Remove section
151	cp $name.flac $name.tmp.flac
152	dd if=$name.tmp.flac of=$name.flac bs=$bs skip=12 seek=10 2>> ./streams.log
153
154	echo $ECHO_N "decode..." $ECHO_C
155	cmd="run_flac --silent --decode-through-errors --force --endian=little --sign=signed --decode --force-raw-format --output-name=$name.cmp $name.flac"
156	echo "### DECODE $name.corrupt #######################################################" >> ./streams.log
157	echo "###    cmd=$cmd" >> ./streams.log
158	$cmd 2>>./streams.log || die "ERROR during decode of $name"
159
160	ls -1l $name.raw >> ./streams.log
161	ls -1l $name.flac >> ./streams.log
162	ls -1l $name.cmp >> ./streams.log
163
164	echo $ECHO_N "compare..." $ECHO_C
165	if [ "$(wc -c < $name.raw)" -ne "$(wc -c < $name.cmp)" ]; then
166		die "ERROR, length of decoded file not equal to length of original"
167	fi
168
169	echo OK
170}
171
172if [ "$FLAC__TEST_LEVEL" -gt 1 ] ; then
173	max_lpc_order=32
174else
175	max_lpc_order=16
176fi
177
178echo "Testing noise through pipes..."
179test_file_piped noise 1 8 "-0"
180
181echo "Testing small files..."
182test_file test01 1 16 "-0 -l $max_lpc_order --lax -m -e -p"
183test_file test02 2 16 "-0 -l $max_lpc_order --lax -m -e -p"
184test_file test03 1 16 "-0 -l $max_lpc_order --lax -m -e -p"
185test_file test04 2 16 "-0 -l $max_lpc_order --lax -m -e -p"
186
187for bps in 8 16 24 32 ; do
188	echo "Testing $bps-bit full-scale deflection streams..."
189	for b in 01 02 03 04 05 06 07 ; do
190		test_file fsd$bps-$b 1 $bps "-0 -l $max_lpc_order --lax -m -e -p"
191	done
192done
193
194echo "Testing 16-bit wasted-bits-per-sample streams..."
195for b in 01 ; do
196	test_file wbps16-$b 1 16 "-0 -l $max_lpc_order --lax -m -e -p"
197done
198
199for bps in 8 16 24 32; do
200	echo "Testing $bps-bit sine wave streams..."
201	for b in 00 ; do
202		test_file sine${bps}-$b 1 $bps "-0 -l $max_lpc_order --lax -m -e --sample-rate=48000"
203	done
204	for b in 01 ; do
205		test_file sine${bps}-$b 1 $bps "-0 -l $max_lpc_order --lax -m -e --sample-rate=96000"
206	done
207	for b in 02 03 04 ; do
208		test_file sine${bps}-$b 1 $bps "-0 -l $max_lpc_order --lax -m -e"
209	done
210	for b in 10 11 ; do
211		test_file sine${bps}-$b 2 $bps "-0 -l $max_lpc_order --lax -m -e --sample-rate=48000"
212	done
213	for b in 12 ; do
214		test_file sine${bps}-$b 2 $bps "-0 -l $max_lpc_order --lax -m -e --sample-rate=96000"
215	done
216	for b in 13 14 15 16 17 18 19 ; do
217		test_file sine${bps}-$b 2 $bps "-0 -l $max_lpc_order --lax -m -e"
218	done
219done
220
221echo "Testing blocksize variations..."
222for disable in '' '--disable-verbatim-subframes --disable-constant-subframes' '--disable-verbatim-subframes --disable-constant-subframes --disable-fixed-subframes' ; do
223	for blocksize in 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ; do
224		for lpc_order in 0 1 2 3 4 5 7 8 9 15 16 17 31 32 ; do
225			if [ $lpc_order = 0 ] || [ $lpc_order -le $blocksize ] ; then
226				test_file noise8m32 1 8 "-8 -p -e -l $lpc_order --lax --blocksize=$blocksize $disable"
227			fi
228		done
229	done
230done
231
232echo "Testing blocksize variations with subdivide apodization..."
233for blocksize in 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ; do
234	for lpc_order in 0 1 2 3 4 5 7 8 9 15 16 17 31 32 ; do
235		if [ $lpc_order = 0 ] || [ $lpc_order -le $blocksize ] ; then
236			test_file noise8m32 1 8 "-8 -p -e -A \"subdivide_tukey(32)\" -l $lpc_order --lax --blocksize=$blocksize"
237		fi
238	done
239done
240
241echo "Testing some frame header variations..."
242test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax -b $max_lpc_order"
243test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax -b 65535"
244test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax --sample-rate=9"
245test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax --sample-rate=90"
246test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax --sample-rate=90000"
247test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax --sample-rate=9"
248test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax --sample-rate=90"
249test_file sine16-01 1 16 "-0 -l $max_lpc_order -m -e -p --lax --sample-rate=90000"
250
251echo "Testing option variations..."
252for f in 00 01 02 03 04 ; do
253	for disable in '' '--disable-verbatim-subframes --disable-constant-subframes' '--disable-verbatim-subframes --disable-constant-subframes --disable-fixed-subframes' ; do
254		if [ -z "$disable" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ] ; then
255			for opt in 0 1 2 4 5 6 8 ; do
256				for extras in '' '-p' '-e' ; do
257					if [ -z "$extras" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ] ; then
258						test_file sine16-$f 1 16 "-$opt $extras $disable"
259					fi
260				done
261			done
262			if [ "$FLAC__TEST_LEVEL" -gt 1 ] ; then
263				test_file sine16-$f 1 16 "-b 16384 -m -r 8 -l $max_lpc_order --lax -e -p $disable"
264			fi
265		fi
266	done
267done
268
269for f in 10 11 12 13 14 15 16 17 18 19 ; do
270	for disable in '' '--disable-verbatim-subframes --disable-constant-subframes' '--disable-verbatim-subframes --disable-constant-subframes --disable-fixed-subframes' ; do
271		if [ -z "$disable" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ] ; then
272			for opt in 0 1 2 4 5 6 8 ; do
273				for extras in '' '-p' '-e' ; do
274					if [ -z "$extras" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ] ; then
275						test_file sine16-$f 2 16 "-$opt $extras $disable"
276					fi
277				done
278			done
279			if [ "$FLAC__TEST_LEVEL" -gt 1 ] ; then
280				test_file sine16-$f 2 16 "-b 16384 -m -r 8 -l $max_lpc_order --lax -e -p $disable"
281			fi
282		fi
283	done
284done
285
286echo "Testing corruption handling..."
287for bps in 8 16 24 ; do
288	for f in 00 01 02 03 04 10 11 12 13 14 15 16 17 18 19; do
289		for disable in '' '--disable-verbatim-subframes --disable-constant-subframes' '--disable-verbatim-subframes --disable-constant-subframes --disable-fixed-subframes' ; do
290			if [ -z "$disable" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ] ; then
291				for opt in 0 1 2 4 5 6 8 ; do
292					for extras in '' '-p' '-e' ; do
293						if [ -z "$extras" -o "$FLAC__TEST_LEVEL" -gt 0 ] && { [ "$bps" -eq 16 -a "$f" -lt 15 ] || [ "$FLAC__TEST_LEVEL" -gt 1 ]; } ; then
294							if [ "$f" -lt 10 ] ; then
295								test_corrupted_file sine$bps-$f 1 $bps "-$opt $extras $disable"
296							else
297								test_corrupted_file sine$bps-$f 2 $bps "-$opt $extras $disable"
298							fi
299						fi
300					done
301				done
302			fi
303		done
304	done
305done
306
307echo "Testing noise..."
308for disable in '' '--disable-verbatim-subframes --disable-constant-subframes' '--disable-verbatim-subframes --disable-constant-subframes --disable-fixed-subframes' ; do
309	if [ -z "$disable" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ] ; then
310		for channels in 1 2 4 8 ; do
311			if [ $channels -le 2 ] || [ "$FLAC__TEST_LEVEL" -gt 1 ] ; then
312				for bps in 8 16 24 32; do
313					for opt in 0 1 2 3 4 5 6 7 8 ; do
314						for extras in '' '-p' '-e' ; do
315                                                        if { [ -z "$extras" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ]; } && { [ "$extras" != '-p' ] || [ "$opt" -gt 2 ]; } ; then
316								for blocksize in '' '--lax -b 32' '--lax -b 32768' '--lax -b 65535' ; do
317									if [ -z "$blocksize" ] || [ "$FLAC__TEST_LEVEL" -gt 0 ] ; then
318										test_file noise $channels $bps "-$opt $extras $blocksize $disable"
319									fi
320								done
321							fi
322						done
323					done
324					if [ "$FLAC__TEST_LEVEL" -gt 1 ] ; then
325						test_file noise $channels $bps "-b 16384 -m -r 8 -l $max_lpc_order --lax -e -p $disable"
326					fi
327				done
328			fi
329		done
330	fi
331done
332