xref: /aosp_15_r20/external/flac/src/test_seeking/main.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1*600f14f4SXin Li /* test_seeking - Seeking tester for libFLAC
2*600f14f4SXin Li  * Copyright (C) 2004-2009  Josh Coalson
3*600f14f4SXin Li  * Copyright (C) 2011-2023  Xiph.Org Foundation
4*600f14f4SXin Li  *
5*600f14f4SXin Li  * This program is free software; you can redistribute it and/or
6*600f14f4SXin Li  * modify it under the terms of the GNU General Public License
7*600f14f4SXin Li  * as published by the Free Software Foundation; either version 2
8*600f14f4SXin Li  * of the License, or (at your option) any later version.
9*600f14f4SXin Li  *
10*600f14f4SXin Li  * This program is distributed in the hope that it will be useful,
11*600f14f4SXin Li  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*600f14f4SXin Li  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*600f14f4SXin Li  * GNU General Public License for more details.
14*600f14f4SXin Li  *
15*600f14f4SXin Li  * You should have received a copy of the GNU General Public License along
16*600f14f4SXin Li  * with this program; if not, write to the Free Software Foundation, Inc.,
17*600f14f4SXin Li  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*600f14f4SXin Li  */
19*600f14f4SXin Li 
20*600f14f4SXin Li #ifdef HAVE_CONFIG_H
21*600f14f4SXin Li #  include <config.h>
22*600f14f4SXin Li #endif
23*600f14f4SXin Li 
24*600f14f4SXin Li #include <signal.h>
25*600f14f4SXin Li #include <stdio.h>
26*600f14f4SXin Li #include <stdlib.h>
27*600f14f4SXin Li #include <string.h>
28*600f14f4SXin Li #if defined _MSC_VER || defined __MINGW32__
29*600f14f4SXin Li #include <time.h>
30*600f14f4SXin Li #else
31*600f14f4SXin Li #include <sys/time.h>
32*600f14f4SXin Li #endif
33*600f14f4SXin Li #include <sys/stat.h> /* for stat() */
34*600f14f4SXin Li #include "FLAC/assert.h"
35*600f14f4SXin Li #include "FLAC/metadata.h"
36*600f14f4SXin Li #include "FLAC/stream_decoder.h"
37*600f14f4SXin Li #include "share/compat.h"
38*600f14f4SXin Li 
39*600f14f4SXin Li typedef struct {
40*600f14f4SXin Li 	FLAC__int32 **pcm;
41*600f14f4SXin Li 	FLAC__bool got_data;
42*600f14f4SXin Li 	FLAC__uint64 total_samples;
43*600f14f4SXin Li 	uint32_t channels;
44*600f14f4SXin Li 	uint32_t bits_per_sample;
45*600f14f4SXin Li 	FLAC__bool quiet;
46*600f14f4SXin Li 	FLAC__bool ignore_errors;
47*600f14f4SXin Li 	FLAC__bool error_occurred;
48*600f14f4SXin Li } DecoderClientData;
49*600f14f4SXin Li 
50*600f14f4SXin Li static FLAC__bool stop_signal_ = false;
51*600f14f4SXin Li 
our_sigint_handler_(int signum)52*600f14f4SXin Li static void our_sigint_handler_(int signum)
53*600f14f4SXin Li {
54*600f14f4SXin Li 	(void)signum;
55*600f14f4SXin Li 	printf("(caught SIGINT) ");
56*600f14f4SXin Li 	fflush(stdout);
57*600f14f4SXin Li 	stop_signal_ = true;
58*600f14f4SXin Li }
59*600f14f4SXin Li 
die_(const char * msg)60*600f14f4SXin Li static FLAC__bool die_(const char *msg)
61*600f14f4SXin Li {
62*600f14f4SXin Li 	printf("ERROR: %s\n", msg);
63*600f14f4SXin Li 	return false;
64*600f14f4SXin Li }
65*600f14f4SXin Li 
die_s_(const char * msg,const FLAC__StreamDecoder * decoder)66*600f14f4SXin Li static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
67*600f14f4SXin Li {
68*600f14f4SXin Li 	FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
69*600f14f4SXin Li 
70*600f14f4SXin Li 	if(msg)
71*600f14f4SXin Li 		printf("FAILED, %s", msg);
72*600f14f4SXin Li 	else
73*600f14f4SXin Li 		printf("FAILED");
74*600f14f4SXin Li 
75*600f14f4SXin Li 	printf(", state = %u (%s)\n", (uint32_t)state, FLAC__StreamDecoderStateString[state]);
76*600f14f4SXin Li 
77*600f14f4SXin Li 	return false;
78*600f14f4SXin Li }
79*600f14f4SXin Li 
local_rand_(void)80*600f14f4SXin Li static uint32_t local_rand_(void)
81*600f14f4SXin Li {
82*600f14f4SXin Li #if !defined _MSC_VER && !defined __MINGW32__
83*600f14f4SXin Li #define RNDFUNC random
84*600f14f4SXin Li #else
85*600f14f4SXin Li #define RNDFUNC rand
86*600f14f4SXin Li #endif
87*600f14f4SXin Li 	/* every RAND_MAX I've ever seen is 2^15-1 or 2^31-1, so a little hackery here: */
88*600f14f4SXin Li 	if (RAND_MAX > 32767)
89*600f14f4SXin Li 		return RNDFUNC();
90*600f14f4SXin Li 	else /* usually MSVC, some solaris */
91*600f14f4SXin Li 		return (RNDFUNC()<<15) | RNDFUNC();
92*600f14f4SXin Li #undef RNDFUNC
93*600f14f4SXin Li }
94*600f14f4SXin Li 
get_filesize_(const char * srcpath)95*600f14f4SXin Li static FLAC__off_t get_filesize_(const char *srcpath)
96*600f14f4SXin Li {
97*600f14f4SXin Li 	struct flac_stat_s srcstat;
98*600f14f4SXin Li 
99*600f14f4SXin Li 	if(0 == flac_stat(srcpath, &srcstat))
100*600f14f4SXin Li 		return srcstat.st_size;
101*600f14f4SXin Li 	else
102*600f14f4SXin Li 		return -1;
103*600f14f4SXin Li }
104*600f14f4SXin Li 
read_pcm_(FLAC__int32 * pcm[],const char * rawfilename,const char * flacfilename)105*600f14f4SXin Li static FLAC__bool read_pcm_(FLAC__int32 *pcm[], const char *rawfilename, const char *flacfilename)
106*600f14f4SXin Li {
107*600f14f4SXin Li 	FILE *f;
108*600f14f4SXin Li 	uint32_t channels = 0, bps = 0, samples, i, j;
109*600f14f4SXin Li 
110*600f14f4SXin Li 	FLAC__off_t rawfilesize = get_filesize_(rawfilename);
111*600f14f4SXin Li 	if (rawfilesize < 0) {
112*600f14f4SXin Li 		fprintf(stderr, "ERROR: can't determine filesize for %s\n", rawfilename);
113*600f14f4SXin Li 		return false;
114*600f14f4SXin Li 	}
115*600f14f4SXin Li 	/* get sample format from flac file; would just use FLAC__metadata_get_streaminfo() except it doesn't work for Ogg FLAC yet */
116*600f14f4SXin Li 	{
117*600f14f4SXin Li #if 0
118*600f14f4SXin Li 		FLAC__StreamMetadata streaminfo;
119*600f14f4SXin Li 		if(!FLAC__metadata_get_streaminfo(flacfilename, &streaminfo)) {
120*600f14f4SXin Li 			printf("ERROR: getting STREAMINFO from %s\n", flacfilename);
121*600f14f4SXin Li 			return false;
122*600f14f4SXin Li 		}
123*600f14f4SXin Li 		channels = streaminfo.data.stream_info.channels;
124*600f14f4SXin Li 		bps = streaminfo.data.stream_info.bits_per_sample;
125*600f14f4SXin Li #else
126*600f14f4SXin Li 		FLAC__bool ok = true;
127*600f14f4SXin Li 		FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
128*600f14f4SXin Li 		FLAC__Metadata_Iterator *it = 0;
129*600f14f4SXin Li 		ok = ok && chain && (FLAC__metadata_chain_read(chain, flacfilename) || FLAC__metadata_chain_read_ogg(chain, flacfilename));
130*600f14f4SXin Li 		ok = ok && (it = FLAC__metadata_iterator_new());
131*600f14f4SXin Li 		if(ok) FLAC__metadata_iterator_init(it, chain);
132*600f14f4SXin Li 		ok = ok && (FLAC__metadata_iterator_get_block(it)->type == FLAC__METADATA_TYPE_STREAMINFO);
133*600f14f4SXin Li 		ok = ok && (channels = FLAC__metadata_iterator_get_block(it)->data.stream_info.channels);
134*600f14f4SXin Li 		ok = ok && (bps = FLAC__metadata_iterator_get_block(it)->data.stream_info.bits_per_sample);
135*600f14f4SXin Li 		if(it) FLAC__metadata_iterator_delete(it);
136*600f14f4SXin Li 		if(chain) FLAC__metadata_chain_delete(chain);
137*600f14f4SXin Li 		if(!ok) {
138*600f14f4SXin Li 			printf("ERROR: getting STREAMINFO from %s\n", flacfilename);
139*600f14f4SXin Li 			return false;
140*600f14f4SXin Li 		}
141*600f14f4SXin Li #endif
142*600f14f4SXin Li 	}
143*600f14f4SXin Li 	if(channels > 2) {
144*600f14f4SXin Li 		printf("ERROR: PCM verification requires 1 or 2 channels, got %u\n", channels);
145*600f14f4SXin Li 		return false;
146*600f14f4SXin Li 	}
147*600f14f4SXin Li 	if(bps != 8 && bps != 16) {
148*600f14f4SXin Li 		printf("ERROR: PCM verification requires 8 or 16 bps, got %u\n", bps);
149*600f14f4SXin Li 		return false;
150*600f14f4SXin Li 	}
151*600f14f4SXin Li 	samples = (uint32_t)(rawfilesize / channels / (bps>>3));
152*600f14f4SXin Li 	if (samples > 10000000) {
153*600f14f4SXin Li 		fprintf(stderr, "ERROR: %s is too big\n", rawfilename);
154*600f14f4SXin Li 		return false;
155*600f14f4SXin Li 	}
156*600f14f4SXin Li 	for(i = 0; i < channels; i++) {
157*600f14f4SXin Li 		if(0 == (pcm[i] = malloc(sizeof(FLAC__int32)*samples))) {
158*600f14f4SXin Li 			printf("ERROR: allocating space for PCM samples\n");
159*600f14f4SXin Li 			return false;
160*600f14f4SXin Li 		}
161*600f14f4SXin Li 	}
162*600f14f4SXin Li 	if(0 == (f = flac_fopen(rawfilename, "rb"))) {
163*600f14f4SXin Li 		printf("ERROR: opening %s for reading\n", rawfilename);
164*600f14f4SXin Li 		return false;
165*600f14f4SXin Li 	}
166*600f14f4SXin Li 	/* assumes signed big-endian data */
167*600f14f4SXin Li 	if(bps == 8) {
168*600f14f4SXin Li 		signed char c;
169*600f14f4SXin Li 		for(i = 0; i < samples; i++) {
170*600f14f4SXin Li 			for(j = 0; j < channels; j++) {
171*600f14f4SXin Li 				if (fread(&c, 1, 1, f) == 1)
172*600f14f4SXin Li 					pcm[j][i] = c;
173*600f14f4SXin Li 			}
174*600f14f4SXin Li 		}
175*600f14f4SXin Li 	}
176*600f14f4SXin Li 	else { /* bps == 16 */
177*600f14f4SXin Li 		uint8_t c[2];
178*600f14f4SXin Li 		uint16_t value;
179*600f14f4SXin Li 		for(i = 0; i < samples; i++) {
180*600f14f4SXin Li 			for(j = 0; j < channels; j++) {
181*600f14f4SXin Li 				if (fread(&c, 1, 2, f) == 2) {
182*600f14f4SXin Li 					value = (c[0] << 8) | c[1];
183*600f14f4SXin Li 					pcm[j][i] = value & 0x8000 ? 0xffff0000 | value : value;
184*600f14f4SXin Li 				}
185*600f14f4SXin Li 			}
186*600f14f4SXin Li 		}
187*600f14f4SXin Li 	}
188*600f14f4SXin Li 	fclose(f);
189*600f14f4SXin Li 	return true;
190*600f14f4SXin Li }
191*600f14f4SXin Li 
write_callback_(const FLAC__StreamDecoder * decoder,const FLAC__Frame * frame,const FLAC__int32 * const buffer[],void * client_data)192*600f14f4SXin Li static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
193*600f14f4SXin Li {
194*600f14f4SXin Li 	DecoderClientData *dcd = (DecoderClientData*)client_data;
195*600f14f4SXin Li 
196*600f14f4SXin Li 	(void)decoder, (void)buffer;
197*600f14f4SXin Li 
198*600f14f4SXin Li 	if(0 == dcd) {
199*600f14f4SXin Li 		printf("ERROR: client_data in write callback is NULL\n");
200*600f14f4SXin Li 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
201*600f14f4SXin Li 	}
202*600f14f4SXin Li 
203*600f14f4SXin Li 	if(dcd->error_occurred)
204*600f14f4SXin Li 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
205*600f14f4SXin Li 
206*600f14f4SXin Li 	FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); /* decoder guarantees this */
207*600f14f4SXin Li 	if (!dcd->quiet)
208*600f14f4SXin Li 		printf("frame@%" PRIu64 "(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
209*600f14f4SXin Li 	fflush(stdout);
210*600f14f4SXin Li 
211*600f14f4SXin Li 	/* check against PCM data if we have it */
212*600f14f4SXin Li 	if (dcd->pcm) {
213*600f14f4SXin Li 		uint32_t c, i, j;
214*600f14f4SXin Li 		for (c = 0; c < frame->header.channels; c++)
215*600f14f4SXin Li 			for (i = (uint32_t)frame->header.number.sample_number, j = 0; j < frame->header.blocksize; i++, j++)
216*600f14f4SXin Li 				if (buffer[c][j] != dcd->pcm[c][i]) {
217*600f14f4SXin Li 					printf("ERROR: sample mismatch at sample#%u(%u), channel=%u, expected %d, got %d\n", i, j, c, buffer[c][j], dcd->pcm[c][i]);
218*600f14f4SXin Li 					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
219*600f14f4SXin Li 				}
220*600f14f4SXin Li 	}
221*600f14f4SXin Li 
222*600f14f4SXin Li 	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
223*600f14f4SXin Li }
224*600f14f4SXin Li 
metadata_callback_(const FLAC__StreamDecoder * decoder,const FLAC__StreamMetadata * metadata,void * client_data)225*600f14f4SXin Li static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
226*600f14f4SXin Li {
227*600f14f4SXin Li 	DecoderClientData *dcd = (DecoderClientData*)client_data;
228*600f14f4SXin Li 
229*600f14f4SXin Li 	(void)decoder;
230*600f14f4SXin Li 
231*600f14f4SXin Li 	if(0 == dcd) {
232*600f14f4SXin Li 		printf("ERROR: client_data in metadata callback is NULL\n");
233*600f14f4SXin Li 		return;
234*600f14f4SXin Li 	}
235*600f14f4SXin Li 
236*600f14f4SXin Li 	if(dcd->error_occurred)
237*600f14f4SXin Li 		return;
238*600f14f4SXin Li 
239*600f14f4SXin Li 	if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
240*600f14f4SXin Li 		dcd->got_data = true;
241*600f14f4SXin Li 		dcd->total_samples = metadata->data.stream_info.total_samples;
242*600f14f4SXin Li 		dcd->channels = metadata->data.stream_info.channels;
243*600f14f4SXin Li 		dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
244*600f14f4SXin Li 	}
245*600f14f4SXin Li }
246*600f14f4SXin Li 
error_callback_(const FLAC__StreamDecoder * decoder,FLAC__StreamDecoderErrorStatus status,void * client_data)247*600f14f4SXin Li static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
248*600f14f4SXin Li {
249*600f14f4SXin Li 	DecoderClientData *dcd = (DecoderClientData*)client_data;
250*600f14f4SXin Li 
251*600f14f4SXin Li 	(void)decoder;
252*600f14f4SXin Li 
253*600f14f4SXin Li 	if(0 == dcd) {
254*600f14f4SXin Li 		printf("ERROR: client_data in error callback is NULL\n");
255*600f14f4SXin Li 		return;
256*600f14f4SXin Li 	}
257*600f14f4SXin Li 
258*600f14f4SXin Li 	if(!dcd->ignore_errors) {
259*600f14f4SXin Li 		printf("ERROR: got error callback: err = %u (%s)\n", (uint32_t)status, FLAC__StreamDecoderErrorStatusString[status]);
260*600f14f4SXin Li 		dcd->error_occurred = true;
261*600f14f4SXin Li 	}
262*600f14f4SXin Li }
263*600f14f4SXin Li 
264*600f14f4SXin Li /* read mode:
265*600f14f4SXin Li  * 0 - no read after seek
266*600f14f4SXin Li  * 1 - read 2 frames
267*600f14f4SXin Li  * 2 - read until end
268*600f14f4SXin Li  */
seek_barrage(FLAC__bool is_ogg,const char * filename,FLAC__off_t filesize,uint32_t count,FLAC__int64 total_samples,uint32_t read_mode,FLAC__int32 ** pcm)269*600f14f4SXin Li static FLAC__bool seek_barrage(FLAC__bool is_ogg, const char *filename, FLAC__off_t filesize, uint32_t count, FLAC__int64 total_samples, uint32_t read_mode, FLAC__int32 **pcm)
270*600f14f4SXin Li {
271*600f14f4SXin Li 	FLAC__StreamDecoder *decoder;
272*600f14f4SXin Li 	DecoderClientData decoder_client_data;
273*600f14f4SXin Li 	uint32_t i;
274*600f14f4SXin Li 	long int n;
275*600f14f4SXin Li 
276*600f14f4SXin Li 	decoder_client_data.pcm = pcm;
277*600f14f4SXin Li 	decoder_client_data.got_data = false;
278*600f14f4SXin Li 	decoder_client_data.total_samples = 0;
279*600f14f4SXin Li 	decoder_client_data.quiet = false;
280*600f14f4SXin Li 	decoder_client_data.ignore_errors = false;
281*600f14f4SXin Li 	decoder_client_data.error_occurred = false;
282*600f14f4SXin Li 
283*600f14f4SXin Li 	printf("\n+++ seek test: FLAC__StreamDecoder (%s FLAC, read_mode=%u)\n\n", is_ogg? "Ogg":"native", read_mode);
284*600f14f4SXin Li 
285*600f14f4SXin Li 	decoder = FLAC__stream_decoder_new();
286*600f14f4SXin Li 	if(0 == decoder)
287*600f14f4SXin Li 		return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
288*600f14f4SXin Li 
289*600f14f4SXin Li 	if(is_ogg) {
290*600f14f4SXin Li 		if(FLAC__stream_decoder_init_ogg_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
291*600f14f4SXin Li 			return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
292*600f14f4SXin Li 	}
293*600f14f4SXin Li 	else {
294*600f14f4SXin Li 		if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
295*600f14f4SXin Li 			return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
296*600f14f4SXin Li 	}
297*600f14f4SXin Li 
298*600f14f4SXin Li 	if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
299*600f14f4SXin Li 		return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
300*600f14f4SXin Li 
301*600f14f4SXin Li 	if(!is_ogg) { /* not necessary to do this for Ogg because of its seeking method */
302*600f14f4SXin Li 	/* process until end of stream to make sure we can still seek in that state */
303*600f14f4SXin Li 		decoder_client_data.quiet = true;
304*600f14f4SXin Li 		if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
305*600f14f4SXin Li 			return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
306*600f14f4SXin Li 		decoder_client_data.quiet = false;
307*600f14f4SXin Li 
308*600f14f4SXin Li 		printf("stream decoder state is %s\n", FLAC__stream_decoder_get_resolved_state_string(decoder));
309*600f14f4SXin Li 		if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM)
310*600f14f4SXin Li 			return die_s_("expected FLAC__STREAM_DECODER_END_OF_STREAM", decoder);
311*600f14f4SXin Li 	}
312*600f14f4SXin Li 
313*600f14f4SXin Li 	printf("file's total_samples is %" PRIu64 "\n", decoder_client_data.total_samples);
314*600f14f4SXin Li 	n = (long int)decoder_client_data.total_samples;
315*600f14f4SXin Li 
316*600f14f4SXin Li 	if(n == 0 && total_samples >= 0)
317*600f14f4SXin Li 		n = (long int)total_samples;
318*600f14f4SXin Li 
319*600f14f4SXin Li 	/* if we don't have a total samples count, just guess based on the file size */
320*600f14f4SXin Li 	/* @@@ for is_ogg we should get it from last page's granulepos */
321*600f14f4SXin Li 	if(n == 0) {
322*600f14f4SXin Li 		/* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
323*600f14f4SXin Li 		n = (long int)(9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample));
324*600f14f4SXin Li 	}
325*600f14f4SXin Li 
326*600f14f4SXin Li 	printf("Begin seek barrage, count=%u\n", count);
327*600f14f4SXin Li 
328*600f14f4SXin Li 	for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
329*600f14f4SXin Li 		FLAC__uint64 pos;
330*600f14f4SXin Li 
331*600f14f4SXin Li 		/* for the first 10, seek to the first 10 samples */
332*600f14f4SXin Li 		if (n >= 10 && i < 10) {
333*600f14f4SXin Li 			pos = i;
334*600f14f4SXin Li 		}
335*600f14f4SXin Li 		/* for the second 10, seek to the last 10 samples */
336*600f14f4SXin Li 		else if (n >= 10 && i < 20) {
337*600f14f4SXin Li 			pos = n - 1 - (i-10);
338*600f14f4SXin Li 		}
339*600f14f4SXin Li 		/* for the third 10, seek past the end and make sure we fail properly as expected */
340*600f14f4SXin Li 		else if (i < 30) {
341*600f14f4SXin Li 			pos = n + (i-20);
342*600f14f4SXin Li 		}
343*600f14f4SXin Li 		else {
344*600f14f4SXin Li 			pos = (FLAC__uint64)(local_rand_() % n);
345*600f14f4SXin Li 		}
346*600f14f4SXin Li 
347*600f14f4SXin Li 		printf("#%u:seek(%" PRIu64 ")... ", i, pos);
348*600f14f4SXin Li 		fflush(stdout);
349*600f14f4SXin Li 		if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
350*600f14f4SXin Li 			if(pos >= (FLAC__uint64)n)
351*600f14f4SXin Li 				printf("seek past end failed as expected... ");
352*600f14f4SXin Li 			else if(decoder_client_data.total_samples == 0 && total_samples <= 0)
353*600f14f4SXin Li 				printf("seek failed, assuming it was past EOF... ");
354*600f14f4SXin Li 			else
355*600f14f4SXin Li 				return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
356*600f14f4SXin Li 			if(!FLAC__stream_decoder_flush(decoder))
357*600f14f4SXin Li 				return die_s_("FLAC__stream_decoder_flush() FAILED", decoder);
358*600f14f4SXin Li 		}
359*600f14f4SXin Li 		else if(read_mode == 1) {
360*600f14f4SXin Li 			printf("decode_frame... ");
361*600f14f4SXin Li 			fflush(stdout);
362*600f14f4SXin Li 			if(!FLAC__stream_decoder_process_single(decoder))
363*600f14f4SXin Li 				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
364*600f14f4SXin Li 
365*600f14f4SXin Li 			printf("decode_frame... ");
366*600f14f4SXin Li 			fflush(stdout);
367*600f14f4SXin Li 			if(!FLAC__stream_decoder_process_single(decoder))
368*600f14f4SXin Li 				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
369*600f14f4SXin Li 		}
370*600f14f4SXin Li 		else if(read_mode == 2) {
371*600f14f4SXin Li 			printf("decode_all... ");
372*600f14f4SXin Li 			fflush(stdout);
373*600f14f4SXin Li 			decoder_client_data.quiet = true;
374*600f14f4SXin Li 			if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
375*600f14f4SXin Li 				return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
376*600f14f4SXin Li 			decoder_client_data.quiet = false;
377*600f14f4SXin Li 		}
378*600f14f4SXin Li 
379*600f14f4SXin Li 		printf("OK\n");
380*600f14f4SXin Li 		fflush(stdout);
381*600f14f4SXin Li 	}
382*600f14f4SXin Li 	stop_signal_ = false;
383*600f14f4SXin Li 
384*600f14f4SXin Li 	if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
385*600f14f4SXin Li 		if(!FLAC__stream_decoder_finish(decoder))
386*600f14f4SXin Li 			return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
387*600f14f4SXin Li 	}
388*600f14f4SXin Li 
389*600f14f4SXin Li 	FLAC__stream_decoder_delete(decoder);
390*600f14f4SXin Li 	printf("\nPASSED!\n");
391*600f14f4SXin Li 
392*600f14f4SXin Li 	return true;
393*600f14f4SXin Li }
394*600f14f4SXin Li 
395*600f14f4SXin Li 
main(int argc,char * argv[])396*600f14f4SXin Li int main(int argc, char *argv[])
397*600f14f4SXin Li {
398*600f14f4SXin Li 	const char *flacfilename, *rawfilename = 0;
399*600f14f4SXin Li 	uint32_t count = 0, read_mode;
400*600f14f4SXin Li 	FLAC__int64 samples = -1;
401*600f14f4SXin Li 	FLAC__off_t flacfilesize;
402*600f14f4SXin Li 	FLAC__int32 *pcm[2] = { 0, 0 };
403*600f14f4SXin Li 	FLAC__bool ok = true;
404*600f14f4SXin Li 
405*600f14f4SXin Li 	static const char * const usage = "usage: test_seeking file.flac [#seeks] [#samples-in-file.flac] [file.raw]\n";
406*600f14f4SXin Li 
407*600f14f4SXin Li 	if (argc < 2 || argc > 5) {
408*600f14f4SXin Li 		fputs(usage, stderr);
409*600f14f4SXin Li 		return 1;
410*600f14f4SXin Li 	}
411*600f14f4SXin Li 
412*600f14f4SXin Li 	flacfilename = argv[1];
413*600f14f4SXin Li 
414*600f14f4SXin Li 	if (argc > 2)
415*600f14f4SXin Li 		count = strtoul(argv[2], 0, 10);
416*600f14f4SXin Li 	if (argc > 3)
417*600f14f4SXin Li 		samples = strtoull(argv[3], 0, 10);
418*600f14f4SXin Li 	if (argc > 4)
419*600f14f4SXin Li 		rawfilename = argv[4];
420*600f14f4SXin Li 
421*600f14f4SXin Li 	if (count < 30)
422*600f14f4SXin Li 		fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
423*600f14f4SXin Li 
424*600f14f4SXin Li #if !defined _MSC_VER && !defined __MINGW32__
425*600f14f4SXin Li 	{
426*600f14f4SXin Li 		struct timeval tv;
427*600f14f4SXin Li 
428*600f14f4SXin Li 		if (gettimeofday(&tv, 0) < 0) {
429*600f14f4SXin Li 			fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
430*600f14f4SXin Li 			tv.tv_usec = 4321;
431*600f14f4SXin Li 		}
432*600f14f4SXin Li 		srandom(tv.tv_usec);
433*600f14f4SXin Li 	}
434*600f14f4SXin Li #else
435*600f14f4SXin Li 	srand((uint32_t)time(0));
436*600f14f4SXin Li #endif
437*600f14f4SXin Li 
438*600f14f4SXin Li 	flacfilesize = get_filesize_(flacfilename);
439*600f14f4SXin Li 	if (flacfilesize < 0) {
440*600f14f4SXin Li 		fprintf(stderr, "ERROR: can't determine filesize for %s\n", flacfilename);
441*600f14f4SXin Li 		return 1;
442*600f14f4SXin Li 	}
443*600f14f4SXin Li 
444*600f14f4SXin Li 	if (rawfilename && !read_pcm_(pcm, rawfilename, flacfilename)) {
445*600f14f4SXin Li 		free(pcm[0]);
446*600f14f4SXin Li 		free(pcm[1]);
447*600f14f4SXin Li 		return 1;
448*600f14f4SXin Li 	}
449*600f14f4SXin Li 
450*600f14f4SXin Li 	(void) signal(SIGINT, our_sigint_handler_);
451*600f14f4SXin Li 
452*600f14f4SXin Li 	for (read_mode = 0; ok && read_mode <= 2; read_mode++) {
453*600f14f4SXin Li 		/* no need to do "decode all" read_mode if PCM checking is available */
454*600f14f4SXin Li 		if (rawfilename && read_mode > 1)
455*600f14f4SXin Li 			continue;
456*600f14f4SXin Li 		if (strlen(flacfilename) > 4 && (0 == strcmp(flacfilename+strlen(flacfilename)-4, ".oga") || 0 == strcmp(flacfilename+strlen(flacfilename)-4, ".ogg"))) {
457*600f14f4SXin Li #if FLAC__HAS_OGG
458*600f14f4SXin Li 			ok = seek_barrage(/*is_ogg=*/true, flacfilename, flacfilesize, count, samples, read_mode, rawfilename? pcm : 0);
459*600f14f4SXin Li #else
460*600f14f4SXin Li 			fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
461*600f14f4SXin Li 			ok = false;
462*600f14f4SXin Li #endif
463*600f14f4SXin Li 		}
464*600f14f4SXin Li 		else {
465*600f14f4SXin Li 			ok = seek_barrage(/*is_ogg=*/false, flacfilename, flacfilesize, count, samples, read_mode, rawfilename? pcm : 0);
466*600f14f4SXin Li 		}
467*600f14f4SXin Li 	}
468*600f14f4SXin Li 
469*600f14f4SXin Li 	free(pcm[0]);
470*600f14f4SXin Li 	free(pcm[1]);
471*600f14f4SXin Li 
472*600f14f4SXin Li 	return ok? 0 : 2;
473*600f14f4SXin Li }
474