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