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