3136c7542bd5667be47701b7a5f0f34f8d81d65f
[platform/upstream/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/stream_decoder.h"
35
36 typedef struct {
37         FLAC__bool got_data;
38         FLAC__uint64 total_samples;
39         unsigned channels;
40         unsigned bits_per_sample;
41         FLAC__bool quiet;
42         FLAC__bool ignore_errors;
43         FLAC__bool error_occurred;
44 } DecoderClientData;
45
46 static FLAC__bool stop_signal_ = false;
47
48 static void our_sigint_handler_(int signal)
49 {
50         (void)signal;
51         printf("(caught SIGINT) ");
52         fflush(stdout);
53         stop_signal_ = true;
54 }
55
56 static FLAC__bool die_(const char *msg)
57 {
58         printf("ERROR: %s\n", msg);
59         return false;
60 }
61
62 static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
63 {
64         FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
65
66         if(msg)
67                 printf("FAILED, %s", msg);
68         else
69                 printf("FAILED");
70
71         printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamDecoderStateString[state]);
72
73         return false;
74 }
75
76 static off_t get_filesize_(const char *srcpath)
77 {
78         struct stat srcstat;
79
80         if(0 == stat(srcpath, &srcstat))
81                 return srcstat.st_size;
82         else
83                 return -1;
84 }
85
86 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
87 {
88         DecoderClientData *dcd = (DecoderClientData*)client_data;
89
90         (void)decoder, (void)buffer;
91
92         if(0 == dcd) {
93                 printf("ERROR: client_data in write callback is NULL\n");
94                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
95         }
96
97         if(dcd->error_occurred)
98                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
99
100         if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
101                 if (!dcd->quiet)
102                         printf("frame@%uf(%u)... ", frame->header.number.frame_number, frame->header.blocksize);
103         }
104         else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER) {
105                 if (!dcd->quiet)
106 #ifdef _MSC_VER
107                         printf("frame@%I64u(%u)... ", frame->header.number.sample_number, frame->header.blocksize);
108 #else
109                         printf("frame@%llu(%u)... ", (unsigned long long)frame->header.number.sample_number, frame->header.blocksize);
110 #endif
111         }
112         else {
113                 FLAC__ASSERT(0);
114                 dcd->error_occurred = true;
115                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
116         }
117         fflush(stdout);
118
119         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
120 }
121
122 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
123 {
124         DecoderClientData *dcd = (DecoderClientData*)client_data;
125
126         (void)decoder;
127
128         if(0 == dcd) {
129                 printf("ERROR: client_data in metadata callback is NULL\n");
130                 return;
131         }
132
133         if(dcd->error_occurred)
134                 return;
135
136         if (!dcd->got_data && metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
137                 dcd->got_data = true;
138                 dcd->total_samples = metadata->data.stream_info.total_samples;
139                 dcd->channels = metadata->data.stream_info.channels;
140                 dcd->bits_per_sample = metadata->data.stream_info.bits_per_sample;
141         }
142 }
143
144 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
145 {
146         DecoderClientData *dcd = (DecoderClientData*)client_data;
147
148         (void)decoder;
149
150         if(0 == dcd) {
151                 printf("ERROR: client_data in error callback is NULL\n");
152                 return;
153         }
154
155         if(!dcd->ignore_errors) {
156                 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
157                 dcd->error_occurred = true;
158         }
159 }
160
161 /* read mode:
162  * 0 - no read after seek
163  * 1 - read 2 frames
164  * 2 - read until end
165  */
166 static FLAC__bool seek_barrage(FLAC__bool is_ogg, const char *filename, off_t filesize, unsigned count, FLAC__int64 total_samples, unsigned read_mode)
167 {
168         FLAC__StreamDecoder *decoder;
169         DecoderClientData decoder_client_data;
170         unsigned i;
171         long int n;
172
173         decoder_client_data.got_data = false;
174         decoder_client_data.total_samples = 0;
175         decoder_client_data.quiet = false;
176         decoder_client_data.ignore_errors = false;
177         decoder_client_data.error_occurred = false;
178
179         printf("\n+++ seek test: FLAC__StreamDecoder (%s FLAC, read_mode=%u)\n\n", is_ogg? "Ogg":"native", read_mode);
180
181         decoder = FLAC__stream_decoder_new();
182         if(0 == decoder)
183                 return die_("FLAC__stream_decoder_new() FAILED, returned NULL\n");
184
185         if(is_ogg) {
186                 if(FLAC__stream_decoder_init_ogg_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
187                         return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
188         }
189         else {
190                 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &decoder_client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
191                         return die_s_("FLAC__stream_decoder_init_file() FAILED", decoder);
192         }
193
194         if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
195                 return die_s_("FLAC__stream_decoder_process_until_end_of_metadata() FAILED", decoder);
196
197         if(!is_ogg) { /* not necessary to do this for Ogg because of its seeking method */
198         /* process until end of stream to make sure we can still seek in that state */
199                 decoder_client_data.quiet = true;
200                 if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
201                         return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
202                 decoder_client_data.quiet = false;
203
204                 printf("stream decoder state is %s\n", FLAC__stream_decoder_get_resolved_state_string(decoder));
205                 if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM)
206                         return die_s_("expected FLAC__STREAM_DECODER_END_OF_STREAM", decoder);
207         }
208
209 #ifdef _MSC_VER
210         printf("file's total_samples is %I64u\n", decoder_client_data.total_samples);
211 #else
212         printf("file's total_samples is %llu\n", (unsigned long long)decoder_client_data.total_samples);
213 #endif
214 #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
215         if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) {
216                 printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX);
217                 return false;
218         }
219 #endif
220         n = (long int)decoder_client_data.total_samples;
221
222         if(n == 0 && total_samples >= 0)
223                 n = (long int)total_samples;
224
225         /* if we don't have a total samples count, just guess based on the file size */
226         /* @@@ for is_ogg we should get it from last page's granulepos */
227         if(n == 0) {
228                 /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */
229                 n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample);
230 #if !defined _MSC_VER && !defined __MINGW32__
231                 if(n > RAND_MAX)
232                         n = RAND_MAX;
233 #endif
234         }
235
236         printf("Begin seek barrage, count=%u\n", count);
237
238         for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) {
239                 FLAC__uint64 pos;
240
241                 /* for the first 10, seek to the first 10 samples */
242                 if (n >= 10 && i < 10) {
243                         pos = i;
244                 }
245                 /* for the second 10, seek to the last 10 samples */
246                 else if (n >= 10 && i < 20) {
247                         pos = n - 1 - (i-10);
248                 }
249                 /* for the third 10, seek past the end and make sure we fail properly as expected */
250                 else if (i < 30) {
251                         pos = n + (i-20);
252                 }
253                 else {
254 #if !defined _MSC_VER && !defined __MINGW32__
255                         pos = (FLAC__uint64)(random() % n);
256 #else
257                         /* RAND_MAX is only 32767 in my MSVC */
258                         pos = (FLAC__uint64)((rand()<<15|rand()) % n);
259 #endif
260                 }
261
262 #ifdef _MSC_VER
263                 printf("seek(%I64u)... ", pos);
264 #else
265                 printf("seek(%llu)... ", (unsigned long long)pos);
266 #endif
267                 fflush(stdout);
268                 if(!FLAC__stream_decoder_seek_absolute(decoder, pos)) {
269                         if(pos >= (FLAC__uint64)n)
270                                 printf("seek past end failed as expected... ");
271                         else if(decoder_client_data.total_samples == 0 && total_samples <= 0)
272                                 printf("seek failed, assuming it was past EOF... ");
273                         else
274                                 return die_s_("FLAC__stream_decoder_seek_absolute() FAILED", decoder);
275                         if(!FLAC__stream_decoder_flush(decoder))
276                                 return die_s_("FLAC__stream_decoder_flush() FAILED", decoder);
277                 }
278                 else if(read_mode == 1) {
279                         printf("decode_frame... ");
280                         fflush(stdout);
281                         if(!FLAC__stream_decoder_process_single(decoder))
282                                 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
283
284                         printf("decode_frame... ");
285                         fflush(stdout);
286                         if(!FLAC__stream_decoder_process_single(decoder))
287                                 return die_s_("FLAC__stream_decoder_process_single() FAILED", decoder);
288                 }
289                 else if(read_mode == 2) {
290                         printf("decode_all... ");
291                         fflush(stdout);
292                         decoder_client_data.quiet = true;
293                         if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
294                                 return die_s_("FLAC__stream_decoder_process_until_end_of_stream() FAILED", decoder);
295                         decoder_client_data.quiet = false;
296                 }
297
298                 printf("OK\n");
299                 fflush(stdout);
300         }
301
302         if(FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
303                 if(!FLAC__stream_decoder_finish(decoder))
304                         return die_s_("FLAC__stream_decoder_finish() FAILED", decoder);
305         }
306
307         FLAC__stream_decoder_delete(decoder);
308         printf("\nPASSED!\n");
309
310         return true;
311 }
312
313 #ifdef _MSC_VER
314 /* There's no strtoull() in MSVC6 so we just write a specialized one */
315 static FLAC__uint64 local__strtoull(const char *src)
316 {
317         FLAC__uint64 ret = 0;
318         int c;
319         FLAC__ASSERT(0 != src);
320         while(0 != (c = *src++)) {
321                 c -= '0';
322                 if(c >= 0 && c <= 9)
323                         ret = (ret * 10) + c;
324                 else
325                         break;
326         }
327         return ret;
328 }
329 #endif
330
331 int main(int argc, char *argv[])
332 {
333         const char *filename;
334         unsigned count = 0, read_mode;
335         FLAC__int64 samples = -1;
336         off_t filesize;
337
338         static const char * const usage = "usage: test_seeking file.flac [#seeks] [#samples-in-file.flac]\n";
339
340         if (argc < 2 || argc > 4) {
341                 fprintf(stderr, usage);
342                 return 1;
343         }
344
345         filename = argv[1];
346
347         if (argc > 2)
348                 count = strtoul(argv[2], 0, 10);
349         if (argc > 3)
350 #ifdef _MSC_VER
351                 samples = local__strtoull(argv[3]);
352 #else
353                 samples = strtoull(argv[3], 0, 10);
354 #endif
355
356         if (count < 30)
357                 fprintf(stderr, "WARNING: random seeks don't kick in until after 30 preprogrammed ones\n");
358
359 #if !defined _MSC_VER && !defined __MINGW32__
360         {
361                 struct timeval tv;
362
363                 if (gettimeofday(&tv, 0) < 0) {
364                         fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
365                         tv.tv_usec = 4321;
366                 }
367                 srandom(tv.tv_usec);
368         }
369 #else
370         srand((unsigned)time(0));
371 #endif
372
373         filesize = get_filesize_(filename);
374         if (filesize < 0) {
375                 fprintf(stderr, "ERROR: can't determine filesize for %s\n", filename);
376                 return 1;
377         }
378
379         (void) signal(SIGINT, our_sigint_handler_);
380
381         for (read_mode = 0; read_mode <= 2; read_mode++) {
382                 FLAC__bool ok;
383                 if (strlen(filename) > 4 && 0 == strcmp(filename+strlen(filename)-4, ".ogg")) {
384 #if FLAC__HAS_OGG
385                         ok = seek_barrage(/*is_ogg=*/true, filename, filesize, count, samples, read_mode);
386 #else
387                         fprintf(stderr, "ERROR: Ogg FLAC not supported\n");
388                         ok = false;
389 #endif
390                 }
391                 else {
392                         ok = seek_barrage(/*is_ogg=*/false, filename, filesize, count, samples, read_mode);
393                 }
394                 if (!ok)
395                         return 2;
396         }
397
398         return 0;
399 }