1 /* test_seeking - Seeking tester for libFLAC
2 * Copyright (C) 2004-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
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.
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.
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.
28 #if defined _MSC_VER || defined __MINGW32__
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"
42 FLAC__uint64 total_samples;
44 unsigned bits_per_sample;
46 FLAC__bool ignore_errors;
47 FLAC__bool error_occurred;
50 static FLAC__bool stop_signal_ = false;
52 static void our_sigint_handler_(int signum)
55 printf("(caught SIGINT) ");
60 static FLAC__bool die_(const char *msg)
62 printf("ERROR: %s\n", msg);
66 static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
68 FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
71 printf("FAILED, %s", msg);
75 printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamDecoderStateString[state]);
80 static unsigned local_rand_(void)
82 #if !defined _MSC_VER && !defined __MINGW32__
83 #define RNDFUNC random
87 /* every RAND_MAX I've ever seen is 2^15-1 or 2^31-1, so a little hackery here: */
90 else /* usually MSVC, some solaris */
91 return (RNDFUNC()<<15) | RNDFUNC();
95 static FLAC__off_t get_filesize_(const char *srcpath)
97 struct flac_stat_s srcstat;
99 if(0 == flac_stat(srcpath, &srcstat))
100 return srcstat.st_size;
105 static FLAC__bool read_pcm_(FLAC__int32 *pcm[], const char *rawfilename, const char *flacfilename)
108 unsigned channels = 0, bps = 0, samples, i, j;
110 FLAC__off_t rawfilesize = get_filesize_(rawfilename);
111 if (rawfilesize < 0) {
112 fprintf(stderr, "ERROR: can't determine filesize for %s\n", rawfilename);
115 /* get sample format from flac file; would just use FLAC__metadata_get_streaminfo() except it doesn't work for Ogg FLAC yet */
118 FLAC__StreamMetadata streaminfo;
119 if(!FLAC__metadata_get_streaminfo(flacfilename, &streaminfo)) {
120 printf("ERROR: getting STREAMINFO from %s\n", flacfilename);
123 channels = streaminfo.data.stream_info.channels;
124 bps = streaminfo.data.stream_info.bits_per_sample;
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);
138 printf("ERROR: getting STREAMINFO from %s\n", flacfilename);
144 printf("ERROR: PCM verification requires 1 or 2 channels, got %u\n", channels);
147 if(bps != 8 && bps != 16) {
148 printf("ERROR: PCM verification requires 8 or 16 bps, got %u\n", bps);
151 samples = rawfilesize / channels / (bps>>3);
152 if (samples > 10000000) {
153 fprintf(stderr, "ERROR: %s is too big\n", rawfilename);
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");
162 if(0 == (f = flac_fopen(rawfilename, "rb"))) {
163 printf("ERROR: opening %s for reading\n", rawfilename);
166 /* assumes signed big-endian data */
169 for(i = 0; i < samples; i++) {
170 for(j = 0; j < channels; j++) {
171 if (fread(&c, 1, 1, f) == 1)
176 else { /* bps == 16 */
178 for(i = 0; i < samples; i++) {
179 for(j = 0; j < channels; j++) {
180 if (fread(&c, 1, 2, f) == 2)
181 pcm[j][i] = ((int)((signed char)c[0])) << 8 | (int)c[1];
189 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
191 DecoderClientData *dcd = (DecoderClientData*)client_data;
193 (void)decoder, (void)buffer;
196 printf("ERROR: client_data in write callback is NULL\n");
197 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
200 if(dcd->error_occurred)
201 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
203 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); /* decoder guarantees this */
205 printf("frame@%" PRIu64 "(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
208 /* check against PCM data if we have it */
211 for (c = 0; c < frame->header.channels; c++)
212 for (i = (unsigned)frame->header.number.sample_number, j = 0; j < frame->header.blocksize; i++, j++)
213 if (buffer[c][j] != dcd->pcm[c][i]) {
214 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]);
215 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
219 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
222 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
224 DecoderClientData *dcd = (DecoderClientData*)client_data;
229 printf("ERROR: client_data in metadata callback is NULL\n");
233 if(dcd->error_occurred)
236 if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
237 dcd->got_data = true;
238 dcd->total_samples = metadata->data.stream_info.total_samples;
239 dcd->channels = metadata->data.stream_info.channels;
240 dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
244 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
246 DecoderClientData *dcd = (DecoderClientData*)client_data;
251 printf("ERROR: client_data in error callback is NULL\n");
255 if(!dcd->ignore_errors) {
256 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
257 dcd->error_occurred = true;
262 * 0 - no read after seek
266 static FLAC__bool seek_barrage(FLAC__bool is_ogg, const char *filename, FLAC__off_t filesize, unsigned count, FLAC__int64 total_samples, unsigned read_mode, FLAC__int32 **pcm)
268 FLAC__StreamDecoder *decoder;
269 DecoderClientData decoder_client_data;
273 decoder_client_data.pcm = pcm;
274 decoder_client_data.got_data = false;
275 decoder_client_data.total_samples = 0;
276 decoder_client_data.quiet = false;
277 decoder_client_data.ignore_errors = false;
278 decoder_client_data.error_occurred = false;
280 printf("\n+++ seek test: FLAC__StreamDecoder (%s FLAC, read_mode=%u)\n\n", is_ogg? "Ogg":"native", read_mode);
282 decoder = FLAC__stream_decoder_new();
284 return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
287 if(FLAC__stream_decoder_init_ogg_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
288 return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
291 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
292 return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
295 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
296 return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
298 if(!is_ogg) { /* not necessary to do this for Ogg because of its seeking method */
299 /* process until end of stream to make sure we can still seek in that state */
300 decoder_client_data.quiet = true;
301 if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
302 return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
303 decoder_client_data.quiet = false;
305 printf("stream decoder state is %s\n", FLAC__stream_decoder_get_resolved_state_string(decoder));
306 if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM)
307 return die_s_("expected FLAC__STREAM_DECODER_END_OF_STREAM", decoder);
310 printf("file's total_samples is %" PRIu64 "\n", decoder_client_data.total_samples);
311 n = (long int)decoder_client_data.total_samples;
313 if(n == 0 && total_samples >= 0)
314 n = (long int)total_samples;
316 /* if we don't have a total samples count, just guess based on the file size */
317 /* @@@ for is_ogg we should get it from last page's granulepos */
319 /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
320 n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
323 printf("Begin seek barrage, count=%u\n", count);
325 for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
328 /* for the first 10, seek to the first 10 samples */
329 if (n >= 10 && i < 10) {
332 /* for the second 10, seek to the last 10 samples */
333 else if (n >= 10 && i < 20) {
334 pos = n - 1 - (i-10);
336 /* for the third 10, seek past the end and make sure we fail properly as expected */
341 pos = (FLAC__uint64)(local_rand_() % n);
344 printf("#%u:seek(%" PRIu64 ")... ", i, pos);
346 if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
347 if(pos >= (FLAC__uint64)n)
348 printf("seek past end failed as expected... ");
349 else if(decoder_client_data.total_samples == 0 && total_samples <= 0)
350 printf("seek failed, assuming it was past EOF... ");
352 return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
353 if(!FLAC__stream_decoder_flush(decoder))
354 return die_s_("FLAC__stream_decoder_flush() FAILED", decoder);
356 else if(read_mode == 1) {
357 printf("decode_frame... ");
359 if(!FLAC__stream_decoder_process_single(decoder))
360 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
362 printf("decode_frame... ");
364 if(!FLAC__stream_decoder_process_single(decoder))
365 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
367 else if(read_mode == 2) {
368 printf("decode_all... ");
370 decoder_client_data.quiet = true;
371 if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
372 return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
373 decoder_client_data.quiet = false;
379 stop_signal_ = false;
381 if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
382 if(!FLAC__stream_decoder_finish(decoder))
383 return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
386 FLAC__stream_decoder_delete(decoder);
387 printf("\nPASSED!\n");
393 /* There's no strtoull() in MSVC6 so we just write a specialized one */
394 static FLAC__uint64 local__strtoull(const char *src)
396 FLAC__uint64 ret = 0;
398 FLAC__ASSERT(0 != src);
399 while(0 != (c = *src++)) {
402 ret = (ret * 10) + c;
410 int main(int argc, char *argv[])
412 const char *flacfilename, *rawfilename = 0;
413 unsigned count = 0, read_mode;
414 FLAC__int64 samples = -1;
415 FLAC__off_t flacfilesize;
416 FLAC__int32 *pcm[2] = { 0, 0 };
417 FLAC__bool ok = true;
419 static const char * const usage = "usage: test_seeking file.flac [#seeks] [#samples-in-file.flac] [file.raw]\n";
421 if (argc < 2 || argc > 5) {
422 fputs(usage, stderr);
426 flacfilename = argv[1];
429 count = strtoul(argv[2], 0, 10);
432 samples = local__strtoull(argv[3]);
434 samples = strtoull(argv[3], 0, 10);
437 rawfilename = argv[4];
440 fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
442 #if !defined _MSC_VER && !defined __MINGW32__
446 if (gettimeofday(&tv, 0) < 0) {
447 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
453 srand((unsigned)time(0));
456 flacfilesize = get_filesize_(flacfilename);
457 if (flacfilesize < 0) {
458 fprintf(stderr, "ERROR: can't determine filesize for %s\n", flacfilename);
462 if (rawfilename && !read_pcm_(pcm, rawfilename, flacfilename)) {
468 (void) signal(SIGINT, our_sigint_handler_);
470 for (read_mode = 0; ok && read_mode <= 2; read_mode++) {
471 /* no need to do "decode all" read_mode if PCM checking is available */
472 if (rawfilename && read_mode > 1)
474 if (strlen(flacfilename) > 4 && (0 == strcmp(flacfilename+strlen(flacfilename)-4, ".oga") || 0 == strcmp(flacfilename+strlen(flacfilename)-4, ".ogg"))) {
476 ok = seek_barrage(/*is_ogg=*/true, flacfilename, flacfilesize, count, samples, read_mode, rawfilename? pcm : 0);
478 fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
483 ok = seek_barrage(/*is_ogg=*/false, flacfilename, flacfilesize, count, samples, read_mode, rawfilename? pcm : 0);