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