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