add flac__file_get_filesize()
[flac.git] / src / flac / decode.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000,2001  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 defined _WIN32 && !defined __CYGWIN__
20 /* where MSVC puts unlink() */
21 # include <io.h>
22 #else
23 # include <unistd.h>
24 #endif
25 #include <stdio.h> /* for FILE */
26 #include <string.h> /* for strcmp() */
27 #include "FLAC/all.h"
28 #include "decode.h"
29
30 typedef struct {
31         FILE *fout;
32         bool abort_flag;
33         bool analysis_mode;
34         analysis_options aopts;
35         bool test_only;
36         bool is_wave_out;
37         bool is_big_endian;
38         bool is_unsigned_samples;
39         uint64 total_samples;
40         unsigned bps;
41         unsigned channels;
42         unsigned sample_rate;
43         bool verbose;
44         uint64 skip;
45         bool skip_count_too_high;
46         uint64 samples_processed;
47         unsigned frame_counter;
48 } stream_info_struct;
49
50 static FLAC__FileDecoder *decoder;
51 static bool is_big_endian_host;
52
53 /* local routines */
54 static bool init(const char *infile, stream_info_struct *stream_info);
55 static bool write_little_endian_uint16(FILE *f, uint16 val);
56 static bool write_little_endian_uint32(FILE *f, uint32 val);
57 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
58 static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
59 static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
60 static void print_stats(const stream_info_struct *stream_info);
61
62 int flac__decode_wav(const char *infile, const char *outfile, bool analysis_mode, analysis_options aopts, bool verbose, uint64 skip)
63 {
64         bool md5_failure = false;
65         stream_info_struct stream_info;
66
67         decoder = 0;
68         stream_info.abort_flag = false;
69         stream_info.analysis_mode = analysis_mode;
70         stream_info.aopts = aopts;
71         stream_info.test_only = (outfile == 0);
72         stream_info.is_wave_out = true;
73         stream_info.verbose = verbose;
74         stream_info.skip = skip;
75         stream_info.skip_count_too_high = false;
76         stream_info.samples_processed = 0;
77         stream_info.frame_counter = 0;
78         stream_info.fout = 0; /* initialized with an open file later if necessary */
79
80         FLAC__ASSERT(!(stream_info.test_only && stream_info.analysis_mode));
81
82         if(!stream_info.test_only) {
83                 if(0 == strcmp(outfile, "-")) {
84                         stream_info.fout = stdout;
85                 }
86                 else {
87                         if(0 == (stream_info.fout = fopen(outfile, "wb"))) {
88                                 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
89                                 return false;
90                         }
91                 }
92         }
93
94         if(analysis_mode)
95                 flac__analyze_init(aopts);
96
97         if(!init(infile, &stream_info))
98                 goto wav_abort_;
99
100         if(skip > 0) {
101                 if(!FLAC__file_decoder_process_metadata(decoder)) {
102                         fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
103                         goto wav_abort_;
104                 }
105                 if(stream_info.skip_count_too_high) {
106                         fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", infile);
107                         goto wav_abort_;
108                 }
109                 if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
110                         fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
111                         goto wav_abort_;
112                 }
113                 if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
114                         if(verbose) fprintf(stderr, "\n");
115                         fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
116                         goto wav_abort_;
117                 }
118                 if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
119                         if(verbose) fprintf(stderr, "\n");
120                         fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
121                         goto wav_abort_;
122                 }
123         }
124         else {
125                 if(!FLAC__file_decoder_process_whole_file(decoder)) {
126                         if(verbose) fprintf(stderr, "\n");
127                         fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
128                         goto wav_abort_;
129                 }
130                 if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
131                         if(verbose) fprintf(stderr, "\n");
132                         fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
133                         goto wav_abort_;
134                 }
135         }
136
137         if(decoder) {
138                 if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
139                         md5_failure = !FLAC__file_decoder_finish(decoder);
140                 print_stats(&stream_info);
141                 FLAC__file_decoder_free_instance(decoder);
142         }
143         if(0 != stream_info.fout && stream_info.fout != stdout)
144                 fclose(stream_info.fout);
145         if(verbose)
146                 fprintf(stderr, "\n");
147         if(analysis_mode)
148                 flac__analyze_finish(aopts);
149         if(md5_failure) {
150                 fprintf(stderr, "%s: WARNING, MD5 signature mismatch\n", infile);
151                 return 1;
152         }
153         else {
154                 if(stream_info.test_only)
155                         fprintf(stderr, "%s: ok\n", infile);
156         }
157         return 0;
158 wav_abort_:
159         if(decoder) {
160                 if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
161                         FLAC__file_decoder_finish(decoder);
162                 FLAC__file_decoder_free_instance(decoder);
163         }
164         if(0 != stream_info.fout && stream_info.fout != stdout) {
165                 fclose(stream_info.fout);
166                 unlink(outfile);
167         }
168         if(analysis_mode)
169                 flac__analyze_finish(aopts);
170         return 1;
171 }
172
173 int flac__decode_raw(const char *infile, const char *outfile, bool analysis_mode, analysis_options aopts, bool verbose, uint64 skip, bool is_big_endian, bool is_unsigned_samples)
174 {
175         bool md5_failure = false;
176         stream_info_struct stream_info;
177
178         decoder = 0;
179         stream_info.abort_flag = false;
180         stream_info.analysis_mode = analysis_mode;
181         stream_info.aopts = aopts;
182         stream_info.test_only = (outfile == 0);
183         stream_info.is_wave_out = false;
184         stream_info.is_big_endian = is_big_endian;
185         stream_info.is_unsigned_samples = is_unsigned_samples;
186         stream_info.verbose = verbose;
187         stream_info.skip = skip;
188         stream_info.skip_count_too_high = false;
189         stream_info.samples_processed = 0;
190         stream_info.frame_counter = 0;
191         stream_info.fout = 0; /* initialized with an open file later if necessary */
192
193         FLAC__ASSERT(!(stream_info.test_only && stream_info.analysis_mode));
194
195         if(!stream_info.test_only) {
196                 if(0 == strcmp(outfile, "-")) {
197                         stream_info.fout = stdout;
198                 }
199                 else {
200                         if(0 == (stream_info.fout = fopen(outfile, "wb"))) {
201                                 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
202                                 return false;
203                         }
204                 }
205         }
206
207         if(analysis_mode)
208                 flac__analyze_init(aopts);
209
210         if(!init(infile, &stream_info))
211                 goto raw_abort_;
212
213         if(skip > 0) {
214                 if(!FLAC__file_decoder_process_metadata(decoder)) {
215                         fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
216                         goto raw_abort_;
217                 }
218                 if(stream_info.skip_count_too_high) {
219                         fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", infile);
220                         goto raw_abort_;
221                 }
222                 if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
223                         fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
224                         goto raw_abort_;
225                 }
226                 if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
227                         if(verbose) fprintf(stderr, "\n");
228                         fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
229                         goto raw_abort_;
230                 }
231                 if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
232                         if(verbose) fprintf(stderr, "\n");
233                         fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
234                         goto raw_abort_;
235                 }
236         }
237         else {
238                 if(!FLAC__file_decoder_process_whole_file(decoder)) {
239                         if(verbose) fprintf(stderr, "\n");
240                         fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
241                         goto raw_abort_;
242                 }
243                 if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
244                         if(verbose) fprintf(stderr, "\n");
245                         fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
246                         goto raw_abort_;
247                 }
248         }
249
250         if(decoder) {
251                 if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
252                         md5_failure = !FLAC__file_decoder_finish(decoder);
253                 print_stats(&stream_info);
254                 FLAC__file_decoder_free_instance(decoder);
255         }
256         if(0 != stream_info.fout && stream_info.fout != stdout)
257                 fclose(stream_info.fout);
258         if(verbose)
259                 fprintf(stderr, "\n");
260         if(analysis_mode)
261                 flac__analyze_finish(aopts);
262         if(md5_failure) {
263                 fprintf(stderr, "%s: WARNING, MD5 signature mismatch\n", infile);
264                 return 1;
265         }
266         else {
267                 if(stream_info.test_only)
268                         fprintf(stderr, "%s: ok\n", infile);
269         }
270         return 0;
271 raw_abort_:
272         if(decoder) {
273                 if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
274                         FLAC__file_decoder_finish(decoder);
275                 FLAC__file_decoder_free_instance(decoder);
276         }
277         if(0 != stream_info.fout && stream_info.fout != stdout) {
278                 fclose(stream_info.fout);
279                 unlink(outfile);
280         }
281         if(analysis_mode)
282                 flac__analyze_finish(aopts);
283         return 1;
284 }
285
286 bool init(const char *infile, stream_info_struct *stream_info)
287 {
288         uint32 test = 1;
289
290         is_big_endian_host = (*((byte*)(&test)))? false : true;
291
292         decoder = FLAC__file_decoder_get_new_instance();
293         if(0 == decoder) {
294                 fprintf(stderr, "ERROR creating the decoder instance\n");
295                 return false;
296         }
297         decoder->check_md5 = true;
298
299         if(FLAC__file_decoder_init(decoder, infile, write_callback, metadata_callback, error_callback, stream_info) != FLAC__FILE_DECODER_OK) {
300                 fprintf(stderr, "ERROR initializing decoder, state=%d:%s\n", decoder->state, FLAC__FileDecoderStateString[decoder->state]);
301                 return false;
302         }
303
304         return true;
305 }
306
307 bool write_little_endian_uint16(FILE *f, uint16 val)
308 {
309         byte *b = (byte*)(&val);
310         if(is_big_endian_host) {
311                 byte tmp;
312                 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
313         }
314         return fwrite(b, 1, 2, f) == 2;
315 }
316
317 bool write_little_endian_uint32(FILE *f, uint32 val)
318 {
319         byte *b = (byte*)(&val);
320         if(is_big_endian_host) {
321                 byte tmp;
322                 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
323                 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
324         }
325         return fwrite(b, 1, 4, f) == 4;
326 }
327
328 FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
329 {
330         stream_info_struct *stream_info = (stream_info_struct *)client_data;
331         FILE *fout = stream_info->fout;
332         unsigned bps = stream_info->bps, channels = stream_info->channels;
333         bool is_big_endian = (stream_info->is_wave_out? false : stream_info->is_big_endian);
334         bool is_unsigned_samples = (stream_info->is_wave_out? bps<=8 : stream_info->is_unsigned_samples);
335         unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
336         static int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(int32)]; /* WATCHOUT: can be up to 2 megs */
337         /* WATCHOUT: we say 'sizeof(int32)' above instead of '(FLAC__MAX_BITS_PER_SAMPLE+7)/8' because we have to use an array int32 even for 24 bps */
338         uint8  *u8buffer  = (uint8  *)s8buffer;
339         int16  *s16buffer = (int16  *)s8buffer;
340         uint16 *u16buffer = (uint16 *)s8buffer;
341         int32  *s32buffer = (int32  *)s8buffer;
342         uint32 *u32buffer = (uint32 *)s8buffer;
343
344         (void)decoder;
345
346         if(stream_info->abort_flag)
347                 return FLAC__STREAM_DECODER_WRITE_ABORT;
348
349         stream_info->samples_processed += wide_samples;
350         stream_info->frame_counter++;
351
352         if(stream_info->verbose && !(stream_info->frame_counter & 0x7f))
353                 print_stats(stream_info);
354
355         if(stream_info->analysis_mode) {
356                 flac__analyze_frame(frame, stream_info->frame_counter-1, stream_info->aopts, fout);
357         }
358         else if(!stream_info->test_only) {
359                 if(bps == 8) {
360                         if(is_unsigned_samples) {
361                                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
362                                         for(channel = 0; channel < channels; channel++, sample++)
363                                                 u8buffer[sample] = buffer[channel][wide_sample] + 0x80;
364                         }
365                         else {
366                                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
367                                         for(channel = 0; channel < channels; channel++, sample++)
368                                                 s8buffer[sample] = buffer[channel][wide_sample];
369                         }
370                         if(fwrite(u8buffer, 1, sample, fout) != sample)
371                                 return FLAC__STREAM_DECODER_WRITE_ABORT;
372                 }
373                 else if(bps == 16) {
374                         if(is_unsigned_samples) {
375                                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
376                                         for(channel = 0; channel < channels; channel++, sample++)
377                                                 u16buffer[sample] = buffer[channel][wide_sample] + 0x8000;
378                         }
379                         else {
380                                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
381                                         for(channel = 0; channel < channels; channel++, sample++)
382                                                 s16buffer[sample] = buffer[channel][wide_sample];
383                         }
384                         if(is_big_endian != is_big_endian_host) {
385                                 unsigned char tmp;
386                                 const unsigned bytes = sample * 2;
387                                 for(byte = 0; byte < bytes; byte += 2) {
388                                         tmp = u8buffer[byte];
389                                         u8buffer[byte] = u8buffer[byte+1];
390                                         u8buffer[byte+1] = tmp;
391                                 }
392                         }
393                         if(fwrite(u16buffer, 2, sample, fout) != sample)
394                                 return FLAC__STREAM_DECODER_WRITE_ABORT;
395                 }
396                 else if(bps == 24) {
397                         if(is_unsigned_samples) {
398                                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
399                                         for(channel = 0; channel < channels; channel++, sample++)
400                                                 u32buffer[sample] = buffer[channel][wide_sample] + 0x800000;
401                         }
402                         else {
403                                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
404                                         for(channel = 0; channel < channels; channel++, sample++)
405                                                 s32buffer[sample] = buffer[channel][wide_sample];
406                         }
407                         if(is_big_endian != is_big_endian_host) {
408                                 unsigned char tmp;
409                                 const unsigned bytes = sample * 4;
410                                 for(byte = 0; byte < bytes; byte += 4) {
411                                         tmp = u8buffer[byte];
412                                         u8buffer[byte] = u8buffer[byte+3];
413                                         u8buffer[byte+3] = tmp;
414                                         tmp = u8buffer[byte+1];
415                                         u8buffer[byte+1] = u8buffer[byte+2];
416                                         u8buffer[byte+2] = tmp;
417                                 }
418                         }
419                         if(is_big_endian) {
420                                 unsigned lbyte;
421                                 const unsigned bytes = sample * 4;
422                                 for(lbyte = byte = 0; byte < bytes; ) {
423                                         byte++;
424                                         u8buffer[lbyte++] = u8buffer[byte++];
425                                         u8buffer[lbyte++] = u8buffer[byte++];
426                                         u8buffer[lbyte++] = u8buffer[byte++];
427                                 }
428                         }
429                         else {
430                                 unsigned lbyte;
431                                 const unsigned bytes = sample * 4;
432                                 for(lbyte = byte = 0; byte < bytes; ) {
433                                         u8buffer[lbyte++] = u8buffer[byte++];
434                                         u8buffer[lbyte++] = u8buffer[byte++];
435                                         u8buffer[lbyte++] = u8buffer[byte++];
436                                         byte++;
437                                 }
438                         }
439                         if(fwrite(u8buffer, 3, sample, fout) != sample)
440                                 return FLAC__STREAM_DECODER_WRITE_ABORT;
441                 }
442                 else {
443                         FLAC__ASSERT(0);
444                 }
445         }
446         return FLAC__STREAM_DECODER_WRITE_CONTINUE;
447 }
448
449 void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
450 {
451         stream_info_struct *stream_info = (stream_info_struct *)client_data;
452         (void)decoder;
453         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
454                 /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
455                 if(metadata->data.stream_info.total_samples > 0 && stream_info->skip >= metadata->data.stream_info.total_samples) {
456                         stream_info->total_samples = 0;
457                         stream_info->skip_count_too_high = true;
458                 }
459                 else
460                         stream_info->total_samples = metadata->data.stream_info.total_samples - stream_info->skip;
461                 stream_info->bps = metadata->data.stream_info.bits_per_sample;
462                 stream_info->channels = metadata->data.stream_info.channels;
463                 stream_info->sample_rate = metadata->data.stream_info.sample_rate;
464
465                 if(stream_info->bps != 8 && stream_info->bps != 16 && stream_info->bps != 24) {
466                         fprintf(stderr, "ERROR: bits per sample is not 8/16/24\n");
467                         stream_info->abort_flag = true;
468                         return;
469                 }
470
471                 /* write the WAVE headers if necessary */
472                 if(!stream_info->analysis_mode && !stream_info->test_only && stream_info->is_wave_out) {
473                         uint64 data_size = stream_info->total_samples * stream_info->channels * ((stream_info->bps+7)/8);
474                         if(data_size >= 0xFFFFFFDC) {
475                                 fprintf(stderr, "ERROR: stream is too big for a wave file\n");
476                                 stream_info->abort_flag = true;
477                                 return;
478                         }
479                         if(fwrite("RIFF", 1, 4, stream_info->fout) != 4) stream_info->abort_flag = true;
480                         if(!write_little_endian_uint32(stream_info->fout, (uint32)(data_size+36))) stream_info->abort_flag = true; /* filesize-8 */
481                         if(fwrite("WAVEfmt ", 1, 8, stream_info->fout) != 8) stream_info->abort_flag = true;
482                         if(fwrite("\020\000\000\000", 1, 4, stream_info->fout) != 4) stream_info->abort_flag = true; /* chunk size = 16 */
483                         if(fwrite("\001\000", 1, 2, stream_info->fout) != 2) stream_info->abort_flag = true; /* compression code == 1 */
484                         if(!write_little_endian_uint16(stream_info->fout, (uint16)(stream_info->channels))) stream_info->abort_flag = true;
485                         if(!write_little_endian_uint32(stream_info->fout, stream_info->sample_rate)) stream_info->abort_flag = true;
486                         if(!write_little_endian_uint32(stream_info->fout, stream_info->sample_rate * stream_info->channels * ((stream_info->bps+7) / 8))) stream_info->abort_flag = true; /* @@@ or is it (sample_rate*channels*bps) / 8 ??? */
487                         if(!write_little_endian_uint16(stream_info->fout, (uint16)(stream_info->channels * ((stream_info->bps+7) / 8)))) stream_info->abort_flag = true; /* block align */
488                         if(!write_little_endian_uint16(stream_info->fout, (uint16)(stream_info->bps))) stream_info->abort_flag = true; /* bits per sample */
489                         if(fwrite("data", 1, 4, stream_info->fout) != 4) stream_info->abort_flag = true;
490                         if(!write_little_endian_uint32(stream_info->fout, (uint32)data_size)) stream_info->abort_flag = true; /* data size */
491                 }
492         }
493 }
494
495 void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
496 {
497         stream_info_struct *stream_info = (stream_info_struct *)client_data;
498         (void)decoder;
499         fprintf(stderr, "*** Got error code %d:%s\n", status, FLAC__StreamDecoderErrorStatusString[status]);
500         stream_info->abort_flag = true;
501 }
502
503 void print_stats(const stream_info_struct *stream_info)
504 {
505         if(stream_info->verbose) {
506                 if(stream_info->total_samples > 0) {
507                         fprintf(stderr, "\r%s %u of %u samples, %6.2f%% complete",
508                                 stream_info->test_only? "tested" : stream_info->analysis_mode? "analyzed" : "wrote",
509                                 (unsigned)stream_info->samples_processed,
510                                 (unsigned)stream_info->total_samples,
511 #ifdef _MSC_VER
512                                 /* with VC++ you have to spoon feed it the casting */
513                                 (double)(int64)stream_info->samples_processed / (double)(int64)stream_info->total_samples * 100.0
514 #else
515                                 (double)stream_info->samples_processed / (double)stream_info->total_samples * 100.0
516 #endif
517                         );
518                 }
519                 else {
520                         fprintf(stderr, "\r%s %u of ? samples, ?%% complete",
521                                 stream_info->test_only? "tested" : stream_info->analysis_mode? "analyzed" : "wrote",
522                                 (unsigned)stream_info->samples_processed
523                         );
524                 }
525         }
526 }