main.c 14.5 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* test_seeking - Seeking tester for libFLAC and libOggFLAC
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2004,2005,2006  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

Josh Coalson's avatar
Josh Coalson committed
19
20
#if HAVE_CONFIG_H
#  include <config.h>
21
22
#endif

Josh Coalson's avatar
Josh Coalson committed
23
24
25
26
27
28
29
30
31
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined _MSC_VER || defined __MINGW32__
#include <time.h>
#else
#include <sys/time.h>
#endif
32
#include <sys/stat.h> /* for stat() */
Josh Coalson's avatar
Josh Coalson committed
33
#include "FLAC/assert.h"
34
#include "FLAC/stream_decoder.h"
35
#ifdef FLAC__HAS_OGG
36
#include "OggFLAC/stream_decoder.h"
37
#endif
Josh Coalson's avatar
Josh Coalson committed
38
39

typedef struct {
40
	FLAC__bool got_data;
Josh Coalson's avatar
Josh Coalson committed
41
	FLAC__uint64 total_samples;
42
43
	unsigned channels;
	unsigned bits_per_sample;
Josh Coalson's avatar
Josh Coalson committed
44
45
	FLAC__bool ignore_errors;
	FLAC__bool error_occurred;
46
} DecoderClientData;
Josh Coalson's avatar
Josh Coalson committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

static FLAC__bool stop_signal_ = false;

static void our_sigint_handler_(int signal)
{
	(void)signal;
	printf("(caught SIGINT) ");
	fflush(stdout);
	stop_signal_ = true;
}

static FLAC__bool die_(const char *msg)
{
	printf("ERROR: %s\n", msg);
	return false;
}

64
static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
65
{
66
	FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
Josh Coalson's avatar
Josh Coalson committed
67
68
69
70
71
72

	if(msg)
		printf("FAILED, %s", msg);
	else
		printf("FAILED");

73
	printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamDecoderStateString[state]);
Josh Coalson's avatar
Josh Coalson committed
74
75
76
77

	return false;
}

78
#ifdef FLAC__HAS_OGG
79
static FLAC__bool die_os_(const char *msg, const OggFLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
80
{
81
	OggFLAC__StreamDecoderState state = OggFLAC__stream_decoder_get_state(decoder);
Josh Coalson's avatar
Josh Coalson committed
82
83
84
85
86
87

	if(msg)
		printf("FAILED, %s", msg);
	else
		printf("FAILED");

88
89
90
91
	printf(", state = %u (%s)\n", (unsigned)state, OggFLAC__StreamDecoderStateString[state]);
	if(state == OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
		FLAC__StreamDecoderState state_ = OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(decoder);
		printf("      FLAC stream decoder state = %u (%s)\n", (unsigned)state_, FLAC__StreamDecoderStateString[state_]);
Josh Coalson's avatar
Josh Coalson committed
92
93
94
95
	}

	return false;
}
96
#endif
Josh Coalson's avatar
Josh Coalson committed
97

98
99
100
101
102
103
104
105
106
107
static off_t get_filesize_(const char *srcpath)
{
	struct stat srcstat;

	if(0 == stat(srcpath, &srcstat))
		return srcstat.st_size;
	else
		return -1;
}

108
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
Josh Coalson's avatar
Josh Coalson committed
109
{
110
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

	(void)decoder, (void)buffer;

	if(0 == dcd) {
		printf("ERROR: client_data in write callback is NULL\n");
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}

	if(dcd->error_occurred)
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;

	if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)
		printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
	else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
		printf("frame@%llu(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
	else {
		FLAC__ASSERT(0);
		dcd->error_occurred = true;
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}
	fflush(stdout);

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

136
static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
137
{
138
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
139
140
141
142
143
144
145
146
147
148
149

	(void)decoder;

	if(0 == dcd) {
		printf("ERROR: client_data in metadata callback is NULL\n");
		return;
	}

	if(dcd->error_occurred)
		return;

150
151
	if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
		dcd->got_data = true;
Josh Coalson's avatar
Josh Coalson committed
152
		dcd->total_samples = metadata->data.stream_info.total_samples;
153
154
155
		dcd->channels = metadata->data.stream_info.channels;
		dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
	}
Josh Coalson's avatar
Josh Coalson committed
156
157
}

158
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
159
{
160
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174

	(void)decoder;

	if(0 == dcd) {
		printf("ERROR: client_data in error callback is NULL\n");
		return;
	}

	if(!dcd->ignore_errors) {
		printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
		dcd->error_occurred = true;
	}
}

175
static FLAC__bool seek_barrage_native_flac(const char *filename, off_t filesize, unsigned count)
Josh Coalson's avatar
Josh Coalson committed
176
{
177
178
	FLAC__StreamDecoder *decoder;
	DecoderClientData decoder_client_data;
Josh Coalson's avatar
Josh Coalson committed
179
180
181
	unsigned i;
	long int n;

182
	decoder_client_data.got_data = false;
Josh Coalson's avatar
Josh Coalson committed
183
184
185
186
	decoder_client_data.total_samples = 0;
	decoder_client_data.ignore_errors = false;
	decoder_client_data.error_occurred = false;

187
	printf("\n+++ seek test: FLAC__StreamDecoder\n\n");
Josh Coalson's avatar
Josh Coalson committed
188

189
	decoder = FLAC__stream_decoder_new();
Josh Coalson's avatar
Josh Coalson committed
190
	if(0 == decoder)
191
		return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
Josh Coalson's avatar
Josh Coalson committed
192

193
194
	if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
		return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
Josh Coalson's avatar
Josh Coalson committed
195

196
197
	if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
		return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
Josh Coalson's avatar
Josh Coalson committed
198
199

	printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
200
#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
201
202
	if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
		printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
Josh Coalson's avatar
Josh Coalson committed
203
204
		return false;
	}
Josh Coalson's avatar
Josh Coalson committed
205
#endif
Josh Coalson's avatar
Josh Coalson committed
206
207
	n = (long int)decoder_client_data.total_samples;

208
209
210
211
	/* if we don't have a total samples count, just guess based on the file size */
	if(n == 0) {
		/* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
		n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
Josh Coalson's avatar
Josh Coalson committed
212
#if !defined _MSC_VER && !defined __MINGW32__
213
214
		if(n > RAND_MAX)
			n = RAND_MAX;
Josh Coalson's avatar
Josh Coalson committed
215
#endif
216
217
	}

Josh Coalson's avatar
Josh Coalson committed
218
219
220
221
222
	printf("Begin seek barrage, count=%u\n", count);

	for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
		FLAC__uint64 pos;

223
224
225
226
227
228
229
230
		/* for the first 10, seek to the first 10 samples */
		if (n >= 10 && i < 10) {
			pos = i;
		}
		/* for the second 10, seek to the last 10 samples */
		else if (n >= 10 && i < 20) {
			pos = n - 1 - (i-10);
		}
231
232
233
234
		/* for the third 10, seek past the end and make sure we fail properly as expected */
		else if (i < 30) {
			pos = n + (i-20);
		}
235
		else {
Josh Coalson's avatar
Josh Coalson committed
236
#if !defined _MSC_VER && !defined __MINGW32__
Josh Coalson's avatar
Josh Coalson committed
237
			pos = (FLAC__uint64)(random() % n);
Josh Coalson's avatar
Josh Coalson committed
238
239
#else
			/* RAND_MAX is only 32767 in my MSVC */
Josh Coalson's avatar
Josh Coalson committed
240
			pos = (FLAC__uint64)((rand()<<15|rand()) % n);
Josh Coalson's avatar
Josh Coalson committed
241
#endif
242
		}
Josh Coalson's avatar
Josh Coalson committed
243
244
245

		printf("seek(%llu)... ", pos);
		fflush(stdout);
246
		if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
247
			if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
248
				return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
249
250
251
252
			else if(decoder_client_data.total_samples == 0)
				printf("seek failed, assuming it was past EOF... ");
			else
				printf("seek past end failed as expected... ");
Josh Coalson's avatar
Josh Coalson committed
253

254
			/* hack to work around a deficiency in the seek API's behavior */
255
			/* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
256
257
258
			/* @@@@@@ probably no longer true and we can remove this hack */
			if(!FLAC__stream_decoder_finish(decoder))
				return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
259

260
261
			if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
				return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
262

263
264
			if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
				return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
265
266
267
268
		}
		else {
			printf("decode_frame... ");
			fflush(stdout);
269
270
			if(!FLAC__stream_decoder_process_single(decoder))
				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
271
272
273

			printf("decode_frame... ");
			fflush(stdout);
274
275
			if(!FLAC__stream_decoder_process_single(decoder))
				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
276
		}
Josh Coalson's avatar
Josh Coalson committed
277
278
279
280
281

		printf("OK\n");
		fflush(stdout);
	}

282
283
284
	if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
		if(!FLAC__stream_decoder_finish(decoder))
			return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
285
286
	}

Josh Coalson's avatar
Josh Coalson committed
287
288
289
290
291
	printf("\nPASSED!\n");

	return true;
}

292
#ifdef FLAC__HAS_OGG
293
static FLAC__bool seek_barrage_ogg_flac(const char *filename, off_t filesize, unsigned count)
Josh Coalson's avatar
Josh Coalson committed
294
{
295
296
	OggFLAC__StreamDecoder *decoder;
	DecoderClientData decoder_client_data;
Josh Coalson's avatar
Josh Coalson committed
297
298
299
	unsigned i;
	long int n;

300
	decoder_client_data.got_data = false;
Josh Coalson's avatar
Josh Coalson committed
301
302
303
304
	decoder_client_data.total_samples = 0;
	decoder_client_data.ignore_errors = false;
	decoder_client_data.error_occurred = false;

305
	printf("\n+++ seek test: OggFLAC__StreamDecoder\n\n");
Josh Coalson's avatar
Josh Coalson committed
306

307
	decoder = OggFLAC__stream_decoder_new();
Josh Coalson's avatar
Josh Coalson committed
308
	if(0 == decoder)
309
		return die_("OggFLAC__stream_decoder_new() FAILED, returned NULL\n");
Josh Coalson's avatar
Josh Coalson committed
310

311
312
	if(OggFLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
		return die_os_("OggFLAC__stream_decoder_init_file() FAILED", decoder);
Josh Coalson's avatar
Josh Coalson committed
313

314
315
	if(!OggFLAC__stream_decoder_process_until_end_of_metadata(decoder))
		return die_os_("OggFLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
Josh Coalson's avatar
Josh Coalson committed
316
317

	printf("file's total_samples is %llu\n", decoder_client_data.total_samples);
Josh Coalson's avatar
Josh Coalson committed
318
#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
319
320
	if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
		printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
Josh Coalson's avatar
Josh Coalson committed
321
322
		return false;
	}
Josh Coalson's avatar
Josh Coalson committed
323
#endif
Josh Coalson's avatar
Josh Coalson committed
324
325
	n = (long int)decoder_client_data.total_samples;

326
	/* if we don't have a total samples count, just guess based on the file size */
327
	/* @@@ should get it from last page's granulepos */
328
329
330
	if(n == 0) {
		/* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
		n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
Josh Coalson's avatar
Josh Coalson committed
331
#if !defined _MSC_VER && !defined __MINGW32__
332
333
		if(n > RAND_MAX)
			n = RAND_MAX;
Josh Coalson's avatar
Josh Coalson committed
334
#endif
335
336
	}

Josh Coalson's avatar
Josh Coalson committed
337
338
339
340
341
	printf("Begin seek barrage, count=%u\n", count);

	for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
		FLAC__uint64 pos;

342
343
344
345
346
347
348
349
		/* for the first 10, seek to the first 10 samples */
		if (n >= 10 && i < 10) {
			pos = i;
		}
		/* for the second 10, seek to the last 10 samples */
		else if (n >= 10 && i < 20) {
			pos = n - 1 - (i-10);
		}
350
351
352
353
		/* for the third 10, seek past the end and make sure we fail properly as expected */
		else if (i < 30) {
			pos = n + (i-20);
		}
354
		else {
Josh Coalson's avatar
Josh Coalson committed
355
#if !defined _MSC_VER && !defined __MINGW32__
Josh Coalson's avatar
Josh Coalson committed
356
			pos = (FLAC__uint64)(random() % n);
Josh Coalson's avatar
Josh Coalson committed
357
358
#else
			/* RAND_MAX is only 32767 in my MSVC */
Josh Coalson's avatar
Josh Coalson committed
359
			pos = (FLAC__uint64)((rand()<<15|rand()) % n);
Josh Coalson's avatar
Josh Coalson committed
360
#endif
361
		}
Josh Coalson's avatar
Josh Coalson committed
362
363
364

		printf("seek(%llu)... ", pos);
		fflush(stdout);
365
		if(!OggFLAC__stream_decoder_seek_absolute(decoder, pos)) {
366
			if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0)
367
				return die_os_("OggFLAC__stream_decoder_seek_absolute() FAILED", decoder);
368
369
370
371
			else if(decoder_client_data.total_samples == 0)
				printf("seek failed, assuming it was past EOF... ");
			else
				printf("seek past end failed as expected... ");
Josh Coalson's avatar
Josh Coalson committed
372

373
			/* hack to work around a deficiency in the seek API's behavior */
374
			/* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */
375
376
377
			/* @@@@@@ probably no longer true and we can remove this hack */
			if(!OggFLAC__stream_decoder_finish(decoder))
				return die_os_("OggFLAC__stream_decoder_finish() FAILED", decoder);
378

379
380
			if(OggFLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
				return die_os_("OggFLAC__stream_decoder_init_file() FAILED", decoder);
381

382
383
			if(!OggFLAC__stream_decoder_process_until_end_of_metadata(decoder))
				return die_os_("OggFLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
384
385
386
387
		}
		else {
			printf("decode_frame... ");
			fflush(stdout);
388
389
			if(!OggFLAC__stream_decoder_process_single(decoder))
				return die_os_("OggFLAC__stream_decoder_process_single() FAILED", decoder);
390
391
392

			printf("decode_frame... ");
			fflush(stdout);
393
394
			if(!OggFLAC__stream_decoder_process_single(decoder))
				return die_os_("OggFLAC__stream_decoder_process_single() FAILED", decoder);
395
		}
Josh Coalson's avatar
Josh Coalson committed
396
397
398
399
400

		printf("OK\n");
		fflush(stdout);
	}

401
402
403
	if(OggFLAC__stream_decoder_get_state(decoder) != OggFLAC__STREAM_DECODER_UNINITIALIZED) {
		if(!OggFLAC__stream_decoder_finish(decoder))
			return die_os_("OggFLAC__stream_decoder_finish() FAILED", decoder);
404
405
	}

Josh Coalson's avatar
Josh Coalson committed
406
407
408
409
	printf("\nPASSED!\n");

	return true;
}
410
#endif
Josh Coalson's avatar
Josh Coalson committed
411
412
413
414
415

int main(int argc, char *argv[])
{
	const char *filename;
	unsigned count = 0;
416
	off_t filesize;
Josh Coalson's avatar
Josh Coalson committed
417
418
419
420
421
422
423
424
425
426
427
428
429

	static const char * const usage = "usage: test_seeking file.flac [#seeks]\n";

	if (argc < 1 || argc > 3) {
		fprintf(stderr, usage);
		return 1;
	}

	filename = argv[1];

	if (argc > 2)
		count = strtoul(argv[2], 0, 10);

430
431
	if (count < 30)
		fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
432

Josh Coalson's avatar
Josh Coalson committed
433
434
435
436
#if !defined _MSC_VER && !defined __MINGW32__
	{
		struct timeval tv;

437
		if (gettimeofday(&tv, 0) < 0) {
Josh Coalson's avatar
Josh Coalson committed
438
439
440
441
442
443
444
445
446
			fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
			tv.tv_usec = 4321;
		}
		srandom(tv.tv_usec);
	}
#else
	srand(time(0));
#endif

447
448
449
450
451
452
	filesize = get_filesize_(filename);
	if (filesize < 0) {
		fprintf(stderr, "ERROR: can't determine filesize for %s\n", filename);
		return 1;
	}

Josh Coalson's avatar
Josh Coalson committed
453
454
455
456
	(void) signal(SIGINT, our_sigint_handler_);

	{
		FLAC__bool ok;
457
458
		if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg")) {
#ifdef FLAC__HAS_OGG
459
			ok = seek_barrage_ogg_flac(filename, filesize, count);
460
461
462
463
464
465
#else
			fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
			ok = false;
#endif
		}
		else {
466
			ok = seek_barrage_native_flac(filename, filesize, count);
467
		}
Josh Coalson's avatar
Josh Coalson committed
468
469
470
		return ok? 0 : 2;
	}
}