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