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