main.c 14.5 KB
Newer Older
1
/* test_seeking - Seeking tester for libFLAC
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2004,2005,2006,2007,2008,2009  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

23
24
25
#if HAVE_INTTYPES_H
#include <inttypes.h>
#endif
Josh Coalson's avatar
Josh Coalson committed
26
27
28
29
30
31
32
33
34
#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
35
#include <sys/stat.h> /* for stat() */
Josh Coalson's avatar
Josh Coalson committed
36
#include "FLAC/assert.h"
37
#include "FLAC/metadata.h"
38
#include "FLAC/stream_decoder.h"
Josh Coalson's avatar
Josh Coalson committed
39
40

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

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;
}

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

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

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

	return false;
}

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
static unsigned local_rand_(void)
{
#if !defined _MSC_VER && !defined __MINGW32__
#define RNDFUNC random
#else
#define RNDFUNC rand
#endif
	/* every RAND_MAX I've ever seen is 2^15-1 or 2^31-1, so a little hackery here: */
	if (RAND_MAX > 32767)
		return RNDFUNC();
	else /* usually MSVC, some solaris */
		return (RNDFUNC()<<15) | RNDFUNC();
#undef RNDFUNC
}

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

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

106
107
108
static FLAC__bool read_pcm_(FLAC__int32 *pcm[], const char *rawfilename, const char *flacfilename)
{
	FILE *f;
Josh Coalson's avatar
Josh Coalson committed
109
	unsigned channels = 0, bps = 0, samples, i, j;
110
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

	off_t rawfilesize = get_filesize_(rawfilename);
	if (rawfilesize < 0) {
		fprintf(stderr, "ERROR: can't determine filesize for %s\n", rawfilename);
		return false;
	}
	/* get sample format from flac file; would just use FLAC__metadata_get_streaminfo() except it doesn't work for Ogg FLAC yet */
	{
#if 0
		FLAC__StreamMetadata streaminfo;
		if(!FLAC__metadata_get_streaminfo(flacfilename, &streaminfo)) {
			printf("ERROR: getting STREAMINFO from %s\n", flacfilename);
			return false;
		}
		channels = streaminfo.data.stream_info.channels;
		bps = streaminfo.data.stream_info.bits_per_sample;
#else
		FLAC__bool ok = true;
		FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
		FLAC__Metadata_Iterator *it = 0;
		ok = ok && chain && (FLAC__metadata_chain_read(chain, flacfilename) || FLAC__metadata_chain_read_ogg(chain, flacfilename));
		ok = ok && (it = FLAC__metadata_iterator_new());
		if(ok) FLAC__metadata_iterator_init(it, chain);
		ok = ok && (FLAC__metadata_iterator_get_block(it)->type == FLAC__METADATA_TYPE_STREAMINFO);
		ok = ok && (channels = FLAC__metadata_iterator_get_block(it)->data.stream_info.channels);
		ok = ok && (bps = FLAC__metadata_iterator_get_block(it)->data.stream_info.bits_per_sample);
		if(it) FLAC__metadata_iterator_delete(it);
		if(chain) FLAC__metadata_chain_delete(chain);
		if(!ok) {
			printf("ERROR: getting STREAMINFO from %s\n", flacfilename);
			return false;
		}
#endif
	}
	if(channels > 2) {
		printf("ERROR: PCM verification requires 1 or 2 channels, got %u\n", channels);
		return false;
	}
	if(bps != 8 && bps != 16) {
		printf("ERROR: PCM verification requires 8 or 16 bps, got %u\n", bps);
		return false;
	}
	samples = rawfilesize / channels / (bps>>3);
	if (samples > 10000000) {
		fprintf(stderr, "ERROR: %s is too big\n", rawfilename);
		return false;
	}
	for(i = 0; i < channels; i++) {
		if(0 == (pcm[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32)*samples))) {
			printf("ERROR: allocating space for PCM samples\n");
			return false;
		}
	}
Josh Coalson's avatar
Josh Coalson committed
163
	if(0 == (f = fopen(rawfilename, "rb"))) {
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
		printf("ERROR: opening %s for reading\n", rawfilename);
		return false;
	}
	/* assumes signed big-endian data */
	if(bps == 8) {
		signed char c;
		for(i = 0; i < samples; i++) {
			for(j = 0; j < channels; j++) {
				fread(&c, 1, 1, f);
				pcm[j][i] = c;
			}
		}
	}
	else { /* bps == 16 */
		unsigned char c[2];
		for(i = 0; i < samples; i++) {
			for(j = 0; j < channels; j++) {
				fread(&c, 1, 2, f);
				pcm[j][i] = ((int)((signed char)c[0])) << 8 | (int)c[1];
			}
		}
	}
Josh Coalson's avatar
Josh Coalson committed
186
	fclose(f);
187
188
189
	return true;
}

190
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
191
{
192
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
193
194
195
196
197
198
199
200
201
202
203

	(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;

204
205
	FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); /* decoder guarantees this */
	if (!dcd->quiet)
206
		printf("frame@%" PRIu64 "(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
Josh Coalson's avatar
Josh Coalson committed
207
208
	fflush(stdout);

209
210
211
212
213
214
215
216
217
218
219
	/* check against PCM data if we have it */
	if (dcd->pcm) {
		unsigned c, i, j;
		for (c = 0; c < frame->header.channels; c++)
			for (i = (unsigned)frame->header.number.sample_number, j = 0; j < frame->header.blocksize; i++, j++)
				if (buffer[c][j] != dcd->pcm[c][i]) {
					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]);
					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
				}
	}

Josh Coalson's avatar
Josh Coalson committed
220
221
222
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

223
static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
224
{
225
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
226
227
228
229
230
231
232
233
234
235
236

	(void)decoder;

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

	if(dcd->error_occurred)
		return;

237
238
	if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
		dcd->got_data = true;
Josh Coalson's avatar
Josh Coalson committed
239
		dcd->total_samples = metadata->data.stream_info.total_samples;
240
241
242
		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
243
244
}

245
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
246
{
247
	DecoderClientData *dcd = (DecoderClientData*)client_data;
Josh Coalson's avatar
Josh Coalson committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261

	(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;
	}
}

262
263
264
265
266
/* read mode:
 * 0 - no read after seek
 * 1 - read 2 frames
 * 2 - read until end
 */
267
static FLAC__bool seek_barrage(FLAC__bool is_ogg, const char *filename, off_t filesize, unsigned count, FLAC__int64 total_samples, unsigned read_mode, FLAC__int32 **pcm)
Josh Coalson's avatar
Josh Coalson committed
268
{
269
270
	FLAC__StreamDecoder *decoder;
	DecoderClientData decoder_client_data;
Josh Coalson's avatar
Josh Coalson committed
271
272
273
	unsigned i;
	long int n;

274
	decoder_client_data.pcm = pcm;
275
	decoder_client_data.got_data = false;
Josh Coalson's avatar
Josh Coalson committed
276
	decoder_client_data.total_samples = 0;
277
	decoder_client_data.quiet = false;
Josh Coalson's avatar
Josh Coalson committed
278
279
280
	decoder_client_data.ignore_errors = false;
	decoder_client_data.error_occurred = false;

281
	printf("\n+++ seek test: FLAC__StreamDecoder (%s FLAC, read_mode=%u)\n\n", is_ogg? "Ogg":"native", read_mode);
Josh Coalson's avatar
Josh Coalson committed
282

283
	decoder = FLAC__stream_decoder_new();
Josh Coalson's avatar
Josh Coalson committed
284
	if(0 == decoder)
285
		return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
Josh Coalson's avatar
Josh Coalson committed
286

287
288
289
290
291
292
293
294
	if(is_ogg) {
		if(FLAC__stream_decoder_init_ogg_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);
	}
	else {
		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
295

296
297
	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
298

299
	if(!is_ogg) { /* not necessary to do this for Ogg because of its seeking method */
300
	/* process until end of stream to make sure we can still seek in that state */
301
302
303
304
305
306
307
308
309
		decoder_client_data.quiet = true;
		if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
			return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
		decoder_client_data.quiet = false;

		printf("stream decoder state is %s\n", FLAC__stream_decoder_get_resolved_state_string(decoder));
		if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM)
			return die_s_("expected FLAC__STREAM_DECODER_END_OF_STREAM", decoder);
	}
310

311
	printf("file's total_samples is %" PRIu64 "\n", decoder_client_data.total_samples);
Josh Coalson's avatar
Josh Coalson committed
312
313
	n = (long int)decoder_client_data.total_samples;

314
315
316
	if(n == 0 && total_samples >= 0)
		n = (long int)total_samples;

317
	/* if we don't have a total samples count, just guess based on the file size */
318
	/* @@@ for is_ogg we should get it from last page's granulepos */
319
320
321
322
323
	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
324
325
326
327
328
	printf("Begin seek barrage, count=%u\n", count);

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

329
330
331
332
333
334
335
336
		/* 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);
		}
337
338
339
340
		/* for the third 10, seek past the end and make sure we fail properly as expected */
		else if (i < 30) {
			pos = n + (i-20);
		}
341
		else {
342
			pos = (FLAC__uint64)(local_rand_() % n);
343
		}
Josh Coalson's avatar
Josh Coalson committed
344

345
		printf("#%u:seek(%" PRIu64 ")... ", i, pos);
Josh Coalson's avatar
Josh Coalson committed
346
		fflush(stdout);
347
		if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
348
349
350
			if(pos >= (FLAC__uint64)n)
				printf("seek past end failed as expected... ");
			else if(decoder_client_data.total_samples == 0 && total_samples <= 0)
351
352
				printf("seek failed, assuming it was past EOF... ");
			else
353
				return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
354
355
			if(!FLAC__stream_decoder_flush(decoder))
				return die_s_("FLAC__stream_decoder_flush() FAILED", decoder);
356
		}
357
		else if(read_mode == 1) {
358
359
			printf("decode_frame... ");
			fflush(stdout);
360
361
			if(!FLAC__stream_decoder_process_single(decoder))
				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
362
363
364

			printf("decode_frame... ");
			fflush(stdout);
365
366
			if(!FLAC__stream_decoder_process_single(decoder))
				return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
367
		}
368
369
370
371
372
373
374
375
		else if(read_mode == 2) {
			printf("decode_all... ");
			fflush(stdout);
			decoder_client_data.quiet = true;
			if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
				return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
			decoder_client_data.quiet = false;
		}
Josh Coalson's avatar
Josh Coalson committed
376
377
378
379

		printf("OK\n");
		fflush(stdout);
	}
380
	stop_signal_ = false;
Josh Coalson's avatar
Josh Coalson committed
381

382
383
384
	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);
385
386
	}

Josh Coalson's avatar
Josh Coalson committed
387
	FLAC__stream_decoder_delete(decoder);
Josh Coalson's avatar
Josh Coalson committed
388
389
390
391
392
	printf("\nPASSED!\n");

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#ifdef _MSC_VER
/* There's no strtoull() in MSVC6 so we just write a specialized one */
static FLAC__uint64 local__strtoull(const char *src)
{
	FLAC__uint64 ret = 0;
	int c;
	FLAC__ASSERT(0 != src);
	while(0 != (c = *src++)) {
		c -= '0';
		if(c >= 0 && c <= 9)
			ret = (ret * 10) + c;
		else
			break;
	}
	return ret;
}
#endif

Josh Coalson's avatar
Josh Coalson committed
411
412
int main(int argc, char *argv[])
{
413
	const char *flacfilename, *rawfilename = 0;
414
	unsigned count = 0, read_mode;
415
	FLAC__int64 samples = -1;
416
417
	off_t flacfilesize;
	FLAC__int32 *pcm[2] = { 0, 0 };
Josh Coalson's avatar
Josh Coalson committed
418
	FLAC__bool ok = true;
Josh Coalson's avatar
Josh Coalson committed
419

420
	static const char * const usage = "usage: test_seeking file.flac [#seeks] [#samples-in-file.flac] [file.raw]\n";
Josh Coalson's avatar
Josh Coalson committed
421

422
	if (argc < 2 || argc > 5) {
Josh Coalson's avatar
Josh Coalson committed
423
424
425
426
		fprintf(stderr, usage);
		return 1;
	}

427
	flacfilename = argv[1];
Josh Coalson's avatar
Josh Coalson committed
428
429
430

	if (argc > 2)
		count = strtoul(argv[2], 0, 10);
431
	if (argc > 3)
Josh Coalson's avatar
Josh Coalson committed
432
#ifdef _MSC_VER
Josh Coalson's avatar
Josh Coalson committed
433
		samples = local__strtoull(argv[3]);
Josh Coalson's avatar
Josh Coalson committed
434
#else
435
		samples = strtoull(argv[3], 0, 10);
Josh Coalson's avatar
Josh Coalson committed
436
#endif
437
438
	if (argc > 4)
		rawfilename = argv[4];
Josh Coalson's avatar
Josh Coalson committed
439

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

Josh Coalson's avatar
Josh Coalson committed
443
444
445
446
#if !defined _MSC_VER && !defined __MINGW32__
	{
		struct timeval tv;

447
		if (gettimeofday(&tv, 0) < 0) {
Josh Coalson's avatar
Josh Coalson committed
448
449
450
451
452
453
			fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
			tv.tv_usec = 4321;
		}
		srandom(tv.tv_usec);
	}
#else
Josh Coalson's avatar
Josh Coalson committed
454
	srand((unsigned)time(0));
Josh Coalson's avatar
Josh Coalson committed
455
456
#endif

457
458
459
	flacfilesize = get_filesize_(flacfilename);
	if (flacfilesize < 0) {
		fprintf(stderr, "ERROR: can't determine filesize for %s\n", flacfilename);
460
461
462
		return 1;
	}

Josh Coalson's avatar
Josh Coalson committed
463
464
465
	if (rawfilename && !read_pcm_(pcm, rawfilename, flacfilename)) {
		free(pcm[0]);
		free(pcm[1]);
466
		return 1;
Josh Coalson's avatar
Josh Coalson committed
467
	}
468

Josh Coalson's avatar
Josh Coalson committed
469
470
	(void) signal(SIGINT, our_sigint_handler_);

Josh Coalson's avatar
Josh Coalson committed
471
	for (read_mode = 0; ok && read_mode <= 2; read_mode++) {
472
473
474
		/* no need to do "decode all" read_mode if PCM checking is available */
		if (rawfilename && read_mode > 1)
			continue;
475
		if (strlen(flacfilename) > 4 && (0 == strcmp(flacfilename+strlen(flacfilename)-4, ".oga") || 0 == strcmp(flacfilename+strlen(flacfilename)-4, ".ogg"))) {
476
#if FLAC__HAS_OGG
477
			ok = seek_barrage(/*is_ogg=*/true, flacfilename, flacfilesize, count, samples, read_mode, rawfilename? pcm : 0);
478
479
480
481
482
483
#else
			fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
			ok = false;
#endif
		}
		else {
484
			ok = seek_barrage(/*is_ogg=*/false, flacfilename, flacfilesize, count, samples, read_mode, rawfilename? pcm : 0);
485
		}
Josh Coalson's avatar
Josh Coalson committed
486
	}
487

Josh Coalson's avatar
Josh Coalson committed
488
489
490
491
	free(pcm[0]);
	free(pcm[1]);

	return ok? 0 : 2;
Josh Coalson's avatar
Josh Coalson committed
492
}