standardize flac__ names
[platform/upstream/flac.git] / src / flac / decode.c
index 0802257..6859d51 100644 (file)
@@ -22,7 +22,6 @@
 #else
 # include <unistd.h>
 #endif
-#include <assert.h> /* for FILE */
 #include <stdio.h> /* for FILE */
 #include <string.h> /* for strcmp() */
 #include "FLAC/all.h"
@@ -43,6 +42,7 @@ typedef struct {
        unsigned sample_rate;
        bool verbose;
        uint64 skip;
+       bool skip_count_too_high;
        uint64 samples_processed;
        unsigned frame_counter;
 } stream_info_struct;
@@ -59,7 +59,7 @@ static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__Stre
 static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
 static void print_stats(const stream_info_struct *stream_info);
 
-int decode_wav(const char *infile, const char *outfile, bool analysis_mode, analysis_options aopts, bool verbose, uint64 skip)
+int flac__decode_wav(const char *infile, const char *outfile, bool analysis_mode, analysis_options aopts, bool verbose, uint64 skip)
 {
        bool md5_failure = false;
        stream_info_struct stream_info;
@@ -72,10 +72,12 @@ int decode_wav(const char *infile, const char *outfile, bool analysis_mode, anal
        stream_info.is_wave_out = true;
        stream_info.verbose = verbose;
        stream_info.skip = skip;
+       stream_info.skip_count_too_high = false;
        stream_info.samples_processed = 0;
        stream_info.frame_counter = 0;
+       stream_info.fout = 0; /* initialized with an open file later if necessary */
 
-       assert(!(stream_info.test_only && stream_info.analysis_mode));
+       FLAC__ASSERT(!(stream_info.test_only && stream_info.analysis_mode));
 
        if(!stream_info.test_only) {
                if(0 == strcmp(outfile, "-")) {
@@ -100,29 +102,33 @@ int decode_wav(const char *infile, const char *outfile, bool analysis_mode, anal
                        fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto wav_abort_;
                }
+               if(stream_info.skip_count_too_high) {
+                       fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", infile);
+                       goto wav_abort_;
+               }
                if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
                        fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto wav_abort_;
                }
                if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto wav_abort_;
                }
                if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto wav_abort_;
                }
        }
        else {
                if(!FLAC__file_decoder_process_whole_file(decoder)) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto wav_abort_;
                }
                if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto wav_abort_;
                }
@@ -134,11 +140,10 @@ int decode_wav(const char *infile, const char *outfile, bool analysis_mode, anal
                print_stats(&stream_info);
                FLAC__file_decoder_free_instance(decoder);
        }
-       if(!stream_info.test_only)
+       if(0 != stream_info.fout && stream_info.fout != stdout)
                fclose(stream_info.fout);
        if(verbose)
-               printf("\n");
-       fflush(stdout);
+               fprintf(stderr, "\n");
        if(analysis_mode)
                analyze_finish(aopts);
        if(md5_failure) {
@@ -147,7 +152,7 @@ int decode_wav(const char *infile, const char *outfile, bool analysis_mode, anal
        }
        else {
                if(stream_info.test_only)
-                       printf("%s: ok\n", infile);
+                       fprintf(stderr, "%s: ok\n", infile);
        }
        return 0;
 wav_abort_:
@@ -156,7 +161,7 @@ wav_abort_:
                        FLAC__file_decoder_finish(decoder);
                FLAC__file_decoder_free_instance(decoder);
        }
-       if(!stream_info.test_only) {
+       if(0 != stream_info.fout && stream_info.fout != stdout) {
                fclose(stream_info.fout);
                unlink(outfile);
        }
@@ -165,7 +170,7 @@ wav_abort_:
        return 1;
 }
 
-int 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)
+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)
 {
        bool md5_failure = false;
        stream_info_struct stream_info;
@@ -180,10 +185,12 @@ int decode_raw(const char *infile, const char *outfile, bool analysis_mode, anal
        stream_info.is_unsigned_samples = is_unsigned_samples;
        stream_info.verbose = verbose;
        stream_info.skip = skip;
+       stream_info.skip_count_too_high = false;
        stream_info.samples_processed = 0;
        stream_info.frame_counter = 0;
+       stream_info.fout = 0; /* initialized with an open file later if necessary */
 
-       assert(!(stream_info.test_only && stream_info.analysis_mode));
+       FLAC__ASSERT(!(stream_info.test_only && stream_info.analysis_mode));
 
        if(!stream_info.test_only) {
                if(0 == strcmp(outfile, "-")) {
@@ -208,29 +215,33 @@ int decode_raw(const char *infile, const char *outfile, bool analysis_mode, anal
                        fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto raw_abort_;
                }
+               if(stream_info.skip_count_too_high) {
+                       fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", infile);
+                       goto raw_abort_;
+               }
                if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
                        fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto raw_abort_;
                }
                if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto raw_abort_;
                }
                if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto raw_abort_;
                }
        }
        else {
                if(!FLAC__file_decoder_process_whole_file(decoder)) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto raw_abort_;
                }
                if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
-                       if(verbose) { printf("\n"); fflush(stdout); }
+                       if(verbose) fprintf(stderr, "\n");
                        fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                        goto raw_abort_;
                }
@@ -242,11 +253,10 @@ int decode_raw(const char *infile, const char *outfile, bool analysis_mode, anal
                print_stats(&stream_info);
                FLAC__file_decoder_free_instance(decoder);
        }
-       if(!stream_info.test_only)
+       if(0 != stream_info.fout && stream_info.fout != stdout)
                fclose(stream_info.fout);
        if(verbose)
-               printf("\n");
-       fflush(stdout);
+               fprintf(stderr, "\n");
        if(analysis_mode)
                analyze_finish(aopts);
        if(md5_failure) {
@@ -255,7 +265,7 @@ int decode_raw(const char *infile, const char *outfile, bool analysis_mode, anal
        }
        else {
                if(stream_info.test_only)
-                       printf("%s: ok\n", infile);
+                       fprintf(stderr, "%s: ok\n", infile);
        }
        return 0;
 raw_abort_:
@@ -264,7 +274,7 @@ raw_abort_:
                        FLAC__file_decoder_finish(decoder);
                FLAC__file_decoder_free_instance(decoder);
        }
-       if(!stream_info.test_only) {
+       if(0 != stream_info.fout && stream_info.fout != stdout) {
                fclose(stream_info.fout);
                unlink(outfile);
        }
@@ -287,7 +297,7 @@ bool init(const char *infile, stream_info_struct *stream_info)
        decoder->check_md5 = true;
 
        if(FLAC__file_decoder_init(decoder, infile, write_callback, metadata_callback, error_callback, stream_info) != FLAC__FILE_DECODER_OK) {
-               fprintf(stderr, "ERROR initializing decoder, state = %d\n", decoder->state);
+               fprintf(stderr, "ERROR initializing decoder, state=%d:%s\n", decoder->state, FLAC__FileDecoderStateString[decoder->state]);
                return false;
        }
 
@@ -339,7 +349,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder,
        stream_info->samples_processed += wide_samples;
        stream_info->frame_counter++;
 
-       if(stream_info->verbose && !(stream_info->frame_counter & 0x1f))
+       if(stream_info->verbose && !(stream_info->frame_counter & 0x7f))
                print_stats(stream_info);
 
        if(stream_info->analysis_mode) {
@@ -430,7 +440,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder,
                                return FLAC__STREAM_DECODER_WRITE_ABORT;
                }
                else {
-                       assert(0);
+                       FLAC__ASSERT(0);
                }
        }
        return FLAC__STREAM_DECODER_WRITE_CONTINUE;
@@ -441,7 +451,13 @@ void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaD
        stream_info_struct *stream_info = (stream_info_struct *)client_data;
        (void)decoder;
        if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
-               stream_info->total_samples = metadata->data.stream_info.total_samples - stream_info->skip;
+               /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
+               if(metadata->data.stream_info.total_samples > 0 && stream_info->skip >= metadata->data.stream_info.total_samples) {
+                       stream_info->total_samples = 0;
+                       stream_info->skip_count_too_high = true;
+               }
+               else
+                       stream_info->total_samples = metadata->data.stream_info.total_samples - stream_info->skip;
                stream_info->bps = metadata->data.stream_info.bits_per_sample;
                stream_info->channels = metadata->data.stream_info.channels;
                stream_info->sample_rate = metadata->data.stream_info.sample_rate;
@@ -487,17 +503,24 @@ void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorSt
 void print_stats(const stream_info_struct *stream_info)
 {
        if(stream_info->verbose) {
-               printf("\r%s %u of %u samples, %6.2f%% complete",
-                       stream_info->test_only? "tested" : stream_info->analysis_mode? "analyzed" : "wrote",
-                       (unsigned)stream_info->samples_processed,
-                       (unsigned)stream_info->total_samples,
+               if(stream_info->total_samples > 0) {
+                       fprintf(stderr, "\r%s %u of %u samples, %6.2f%% complete",
+                               stream_info->test_only? "tested" : stream_info->analysis_mode? "analyzed" : "wrote",
+                               (unsigned)stream_info->samples_processed,
+                               (unsigned)stream_info->total_samples,
 #ifdef _MSC_VER
-                       /* with VC++ you have to spoon feed it the casting */
-                       (double)(int64)stream_info->samples_processed / (double)(int64)stream_info->total_samples * 100.0
+                               /* with VC++ you have to spoon feed it the casting */
+                               (double)(int64)stream_info->samples_processed / (double)(int64)stream_info->total_samples * 100.0
 #else
-                       (double)stream_info->samples_processed / (double)stream_info->total_samples * 100.0
+                               (double)stream_info->samples_processed / (double)stream_info->total_samples * 100.0
 #endif
-               );
-               fflush(stdout);
+                       );
+               }
+               else {
+                       fprintf(stderr, "\r%s %u of ? samples, ?%% complete",
+                               stream_info->test_only? "tested" : stream_info->analysis_mode? "analyzed" : "wrote",
+                               (unsigned)stream_info->samples_processed
+                       );
+               }
        }
 }