src/libFLAC/stream_encoder.c : Fix typo.
[platform/upstream/flac.git] / src / flac / main.c
index 6cf1f2f..6f24e55 100644 (file)
@@ -1,5 +1,5 @@
 /* flac - Command-line FLAC encoder/decoder
- * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -36,6 +36,7 @@
 #include "FLAC/all.h"
 #include "share/alloc.h"
 #include "share/grabbag.h"
+#include "share/compat.h"
 #include "analyze.h"
 #include "decode.h"
 #include "encode.h"
@@ -43,7 +44,7 @@
 #include "utils.h"
 #include "vorbiscomment.h"
 
-#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
+#if defined _MSC_VER || defined __MINGW32__
 #define FLAC__STRCASECMP stricmp
 #else
 #define FLAC__STRCASECMP strcasecmp
@@ -84,11 +85,6 @@ static const char *get_outfilename(const char *infilename, const char *suffix);
 static void die(const char *message);
 static int conditional_fclose(FILE *f);
 static char *local_strdup(const char *source);
-#ifdef _MSC_VER
-/* There's no strtoll() in MSVC6 so we just write a specialized one */
-static FLAC__int64 local__strtoll(const char *src, char **endptr);
-#endif
-
 
 /*
  * share__getopt format struct; note that for long options with no
@@ -146,8 +142,10 @@ static struct share__option long_options_[] = {
        { "best"                      , share__no_argument, 0, '8' },
        { "fast"                      , share__no_argument, 0, '0' },
        { "verify"                    , share__no_argument, 0, 'V' },
-       { "force-aiff-format"         , share__no_argument, 0, 0 },
        { "force-raw-format"          , share__no_argument, 0, 0 },
+       { "force-aiff-format"         , share__no_argument, 0, 0 },
+       { "force-rf64-format"         , share__no_argument, 0, 0 },
+       { "force-wave64-format"       , share__no_argument, 0, 0 },
        { "lax"                       , share__no_argument, 0, 0 },
        { "replay-gain"               , share__no_argument, 0, 0 },
        { "ignore-chunk-sizes"        , share__no_argument, 0, 0 },
@@ -240,8 +238,10 @@ static struct {
        FLAC__bool has_serial_number; /* true iff --serial-number was used */
        long serial_number; /* this is the Ogg serial number and is unused for native FLAC */
        FLAC__bool force_to_stdout;
-       FLAC__bool force_aiff_format;
        FLAC__bool force_raw_format;
+       FLAC__bool force_aiff_format;
+       FLAC__bool force_rf64_format;
+       FLAC__bool force_wave64_format;
        FLAC__bool delete_input;
        FLAC__bool preserve_modtime;
        FLAC__bool keep_foreign_metadata;
@@ -378,8 +378,8 @@ int do_it(void)
                        if(!FLAC__format_sample_rate_is_valid(option_values.format_sample_rate))
                                return usage_error("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", option_values.format_sample_rate, FLAC__MAX_SAMPLE_RATE);
                }
-               if(option_values.force_raw_format && option_values.force_aiff_format)
-                       return usage_error("ERROR: only one of --force-raw-format and --force-aiff-format allowed\n");
+               if((option_values.force_raw_format?1:0) + (option_values.force_aiff_format?1:0) + (option_values.force_rf64_format?1:0) + (option_values.force_wave64_format?1:0) > 1)
+                       return usage_error("ERROR: only one of --force-raw-format/--force-aiff-format/--force-rf64-format/--force-wave64-format allowed\n");
                if(option_values.mode_decode) {
                        if(!option_values.force_raw_format) {
                                if(option_values.format_is_big_endian >= 0)
@@ -465,15 +465,12 @@ int do_it(void)
                                return usage_error("ERROR: --keep-foreign-metadata is not allowed in test mode\n");
                        if(option_values.analyze)
                                return usage_error("ERROR: --keep-foreign-metadata is not allowed in analyis mode\n");
-                       /*@@@@@@*/
-                       if(option_values.delete_input)
-                               return usage_error("ERROR: using --delete-input-file with --keep-foreign-metadata has been disabled until more testing has been done.\n");
                        flac__utils_printf(stderr, 1, "NOTE: --keep-foreign-metadata is a new feature; make sure to test the output file before deleting the original.\n");
                }
        }
 
        flac__utils_printf(stderr, 2, "\n");
-       flac__utils_printf(stderr, 2, "flac %s, Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson\n", FLAC__VERSION_STRING);
+       flac__utils_printf(stderr, 2, "flac %s, Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson\n", FLAC__VERSION_STRING);
        flac__utils_printf(stderr, 2, "flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
        flac__utils_printf(stderr, 2, "welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
 
@@ -523,12 +520,6 @@ int do_it(void)
                                                flac__utils_printf(stderr, 1, "ERROR: filename too long: %s", option_values.filenames[i]);
                                                return 1;
                                        }
-                                       if(0 == strcmp(option_values.filenames[i], "-")) {
-                                               FLAC__ASSERT(0);
-                                               /* double protection */
-                                               flac__utils_printf(stderr, 1, "internal error\n");
-                                               return 2;
-                                       }
                                        if(0 != (error = grabbag__replaygain_store_to_file_album(outfilename, album_gain, album_peak, option_values.preserve_modtime))) {
                                                flac__utils_printf(stderr, 1, "%s: ERROR writing ReplayGain album tags (%s)\n", outfilename, error);
                                                retval = 1;
@@ -562,8 +553,10 @@ FLAC__bool init_options(void)
        option_values.has_serial_number = false;
        option_values.serial_number = 0;
        option_values.force_to_stdout = false;
-       option_values.force_aiff_format = false;
        option_values.force_raw_format = false;
+       option_values.force_aiff_format = false;
+       option_values.force_rf64_format = false;
+       option_values.force_wave64_format = false;
        option_values.delete_input = false;
        option_values.preserve_modtime = true;
        option_values.keep_foreign_metadata = false;
@@ -641,7 +634,7 @@ int parse_options(int argc, char *argv[])
 
        if(option_values.num_files > 0) {
                unsigned i = 0;
-               if(0 == (option_values.filenames = (char**)malloc(sizeof(char*) * option_values.num_files)))
+               if(0 == (option_values.filenames = malloc(sizeof(char*) * option_values.num_files)))
                        die("out of memory allocating space for file names list");
                while(share__optind < argc)
                        option_values.filenames[i++] = local_strdup(argv[share__optind++]);
@@ -686,13 +679,8 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                        FLAC__ASSERT(0 != option_argument);
                        {
                                char *end;
-#ifdef _MSC_VER
                                FLAC__int64 i;
-                               i = local__strtoll(option_argument, &end);
-#else
-                               long long i;
                                i = strtoll(option_argument, &end, 10);
-#endif
                                if(0 == strlen(option_argument) || *end)
                                        return usage_error("ERROR: --%s must be a number\n", long_option);
                                option_values.format_input_size = (off_t)i;
@@ -757,11 +745,17 @@ int parse_option(int short_option, const char *long_option, const char *option_a
                else if(0 == strcmp(long_option, "no-cued-seekpoints")) {
                        option_values.cued_seekpoints = false;
                }
+               else if(0 == strcmp(long_option, "force-raw-format")) {
+                       option_values.force_raw_format = true;
+               }
                else if(0 == strcmp(long_option, "force-aiff-format")) {
                        option_values.force_aiff_format = true;
                }
-               else if(0 == strcmp(long_option, "force-raw-format")) {
-                       option_values.force_raw_format = true;
+               else if(0 == strcmp(long_option, "force-rf64-format")) {
+                       option_values.force_rf64_format = true;
+               }
+               else if(0 == strcmp(long_option, "force-wave64-format")) {
+                       option_values.force_wave64_format = true;
                }
                else if(0 == strcmp(long_option, "lax")) {
                        option_values.lax = true;
@@ -1144,7 +1138,7 @@ static void usage_header(void)
 {
        printf("===============================================================================\n");
        printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
-       printf("Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson\n");
+       printf("Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson\n");
        printf("\n");
        printf("This program is free software; you can redistribute it and/or\n");
        printf("modify it under the terms of the GNU General Public License\n");
@@ -1271,8 +1265,10 @@ void show_help(void)
        printf("      --sample-rate=#          Sample rate in Hz\n");
        printf("      --sign={signed|unsigned} Sign of samples\n");
        printf("      --input-size=#           Size of the raw input in bytes\n");
-       printf("      --force-aiff-format      Force decoding to AIFF format\n");
        printf("      --force-raw-format       Treat input or output as raw samples\n");
+       printf("      --force-aiff-format      Force decoding to AIFF format\n");
+       printf("      --force-rf64-format      Force decoding to RF64 format\n");
+       printf("      --force-wave64-format    Force decoding to Wave64 format\n");
        printf("negative options:\n");
        printf("      --no-adaptive-mid-side\n");
        printf("      --no-decode-through-errors\n");
@@ -1306,8 +1302,8 @@ void show_explain(void)
        usage_header();
        usage_summary();
        printf("For encoding:\n");
-       printf("  The input file(s) may be a PCM WAVE file, AIFF (or uncompressed AIFF-C)\n");
-       printf("  file, or raw samples.\n");
+       printf("  The input file(s) may be a PCM WAVE or RF64 file, AIFF (or uncompressed\n");
+       printf("  AIFF-C) file, or raw samples.\n");
        printf("  The output file(s) will be in native FLAC or Ogg FLAC format\n");
        printf("For decoding, the reverse is true.\n");
        printf("\n");
@@ -1602,13 +1598,23 @@ void show_explain(void)
        printf("                               the input stream, the encoder will complain\n");
        printf("                               about an unexpected end-of-file.  If the size\n");
        printf("                               given is less, samples will be truncated.\n");
+       printf("      --force-raw-format       Force input (when encoding) or output (when\n");
+       printf("                               decoding) to be treated as raw samples\n");
        printf("      --force-aiff-format      Force the decoder to output AIFF format.  This\n");
        printf("                               option is not needed if the output filename (as\n");
        printf("                               set by -o) ends with .aif or .aiff; this option\n");
        printf("                               has no effect when encoding since input AIFF is\n");
        printf("                               auto-detected.\n");
-       printf("      --force-raw-format       Force input (when encoding) or output (when\n");
-       printf("                               decoding) to be treated as raw samples\n");
+       printf("      --force-rf64-format      Force the decoder to output RF64 format.  This\n");
+       printf("                               option is not needed if the output filename (as\n");
+       printf("                               set by -o) ends with .rf64; this option\n");
+       printf("                               has no effect when encoding since input RF64 is\n");
+       printf("                               auto-detected.\n");
+       printf("      --force-wave64-format    Force the decoder to output Wave64 format.  This\n");
+       printf("                               option is not needed if the output filename (as\n");
+       printf("                               set by -o) ends with .w64; this option\n");
+       printf("                               has no effect when encoding since input Wave64 is\n");
+       printf("                               auto-detected.\n");
        printf("negative options:\n");
        printf("      --no-adaptive-mid-side\n");
        printf("      --no-decode-through-errors\n");
@@ -1639,7 +1645,7 @@ void show_explain(void)
 void format_mistake(const char *infilename, FileFormat wrong, FileFormat right)
 {
        /* WATCHOUT: indexed by FileFormat */
-       static const char * const ff[] = { " raw", " WAVE", "n AIFF", "n AIFF-C", " FLAC", "n Ogg FLAC" };
+       static const char * const ff[] = { " raw", " WAVE", "n RF64", "n AIFF", "n AIFF-C", " FLAC", "n Ogg FLAC" };
        flac__utils_printf(stderr, 1, "WARNING: %s is not a%s file; treating as a%s file\n", infilename, ff[wrong], ff[right]);
 }
 
@@ -1677,6 +1683,10 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                /* first set format based on name */
                if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".wav"))
                        input_format = FORMAT_WAVE;
+               else if(strlen(infilename) >= 5 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-5), ".rf64"))
+                       input_format = FORMAT_RF64;
+               else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".w64"))
+                       input_format = FORMAT_WAVE64;
                else if(strlen(infilename) >= 4 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-4), ".aif"))
                        input_format = FORMAT_AIFF;
                else if(strlen(infilename) >= 5 && 0 == FLAC__STRCASECMP(infilename+(strlen(infilename)-5), ".aiff"))
@@ -1690,6 +1700,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
 
                /* attempt to guess the file type based on the first 12 bytes */
                if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) {
+                       /* all supported non-raw formats have at least 12 bytes of header to read */
                        if(input_format != FORMAT_RAW) {
                                format_mistake(infilename, input_format, FORMAT_RAW);
                                if(option_values.treat_warnings_as_errors) {
@@ -1697,25 +1708,32 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                                        return 1;
                                }
                        }
+                       /* force to raw */
                        input_format = FORMAT_RAW;
                }
                else {
-                       if(!strncmp((const char *)lookahead, "ID3", 3)) {
+                       if(!memcmp(lookahead, "ID3", 3)) {
                                flac__utils_printf(stderr, 1, "ERROR: input file %s has an ID3v2 tag\n", infilename);
+                               conditional_fclose(encode_infile);
                                return 1;
                        }
-                       else if(!strncmp((const char *)lookahead, "RIFF", 4) && !strncmp((const char *)lookahead+8, "WAVE", 4))
+                       else if(!memcmp(lookahead, "RIFF", 4) && !memcmp(lookahead+8, "WAVE", 4))
                                input_format = FORMAT_WAVE;
-                       else if(!strncmp((const char *)lookahead, "FORM", 4) && !strncmp((const char *)lookahead+8, "AIFF", 4))
+                       else if(!memcmp(lookahead, "RF64", 4) && !memcmp(lookahead+8, "WAVE", 4))
+                               input_format = FORMAT_RF64;
+                       else if(!memcmp(lookahead, "riff\x2E\x91\xCF\x11\xD6\xA5\x28\xDB", 12)) /* just check 1st 12 bytes of GUID */
+                               input_format = FORMAT_WAVE64;
+                       else if(!memcmp(lookahead, "FORM", 4) && !memcmp(lookahead+8, "AIFF", 4))
                                input_format = FORMAT_AIFF;
-                       else if(!strncmp((const char *)lookahead, "FORM", 4) && !strncmp((const char *)lookahead+8, "AIFC", 4))
+                       else if(!memcmp(lookahead, "FORM", 4) && !memcmp(lookahead+8, "AIFC", 4))
                                input_format = FORMAT_AIFF_C;
                        else if(!memcmp(lookahead, FLAC__STREAM_SYNC_STRING, sizeof(FLAC__STREAM_SYNC_STRING)))
                                input_format = FORMAT_FLAC;
-                       /* this could be made more accurate by looking at the first packet */
+                       /*@@@ this could be made more accurate by looking at the first packet to make sure it's Ogg FLAC and not, say, Ogg Vorbis.  we do catch such problems later though. */
                        else if(!memcmp(lookahead, "OggS", 4))
                                input_format = FORMAT_OGGFLAC;
                        else {
+                               /* didn't find header of any supported format */
                                if(input_format != FORMAT_RAW) {
                                        format_mistake(infilename, input_format, FORMAT_RAW);
                                        if(option_values.treat_warnings_as_errors) {
@@ -1723,6 +1741,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                                                return 1;
                                        }
                                }
+                               /* force to raw */
                                input_format = FORMAT_RAW;
                        }
                }
@@ -1733,9 +1752,9 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                        conditional_fclose(encode_infile);
                        return usage_error("ERROR: --keep-foreign-metadata cannot be used when encoding from stdin or to stdout\n");
                }
-               if(input_format != FORMAT_WAVE && input_format != FORMAT_AIFF && input_format != FORMAT_AIFF_C) {
+               if(input_format != FORMAT_WAVE && input_format != FORMAT_WAVE64 && input_format != FORMAT_RF64 && input_format != FORMAT_AIFF && input_format != FORMAT_AIFF_C) {
                        conditional_fclose(encode_infile);
-                       return usage_error("ERROR: --keep-foreign-metadata can only be used with WAVE or AIFF input\n");
+                       return usage_error("ERROR: --keep-foreign-metadata can only be used with WAVE, Wave64, RF64, or AIFF input\n");
                }
        }
 
@@ -1801,7 +1820,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
                }
        }
 
-       if(/*@@@@@@why no stdin?*/encode_infile == stdin || option_values.force_to_stdout) {
+       if(option_values.force_to_stdout) {
                if(option_values.replay_gain) {
                        conditional_fclose(encode_infile);
                        return usage_error("ERROR: --replay-gain cannot be used when encoding to stdout\n");
@@ -1868,7 +1887,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
        if(encode_infile != stdin && grabbag__file_are_same(infilename, outfilename)) {
                static const char *tmp_suffix = ".tmp,fl-ac+en'c";
                /*@@@@ still a remote possibility that a file with this filename exists */
-               if(0 == (internal_outfilename = (char *)safe_malloc_add_3op_(strlen(outfilename), /*+*/strlen(tmp_suffix), /*+*/1))) {
+               if(0 == (internal_outfilename = safe_malloc_add_3op_(strlen(outfilename), /*+*/strlen(tmp_suffix), /*+*/1))) {
                        flac__utils_printf(stderr, 1, "ERROR allocating memory for tempfile name\n");
                        conditional_fclose(encode_infile);
                        return 1;
@@ -1889,14 +1908,24 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
        else if(input_format == FORMAT_FLAC || input_format == FORMAT_OGGFLAC) {
                retval = flac__encode_file(encode_infile, infilesize, infilename, internal_outfilename? internal_outfilename : outfilename, lookahead, lookahead_length, encode_options);
        }
-       else if(input_format == FORMAT_WAVE || input_format == FORMAT_AIFF || input_format == FORMAT_AIFF_C) {
+       else if(input_format == FORMAT_WAVE || input_format == FORMAT_WAVE64 || input_format == FORMAT_RF64 || input_format == FORMAT_AIFF || input_format == FORMAT_AIFF_C) {
                encode_options.format_options.iff.foreign_metadata = 0;
 
-               /* read foreign metadata if requested */
+               /* initialize foreign metadata if requested */
                if(option_values.keep_foreign_metadata) {
-                       if(0 == (encode_options.format_options.iff.foreign_metadata = flac__foreign_metadata_new(input_format==FORMAT_WAVE? FOREIGN_BLOCK_TYPE__RIFF : FOREIGN_BLOCK_TYPE__AIFF))) {
+                       encode_options.format_options.iff.foreign_metadata =
+                               flac__foreign_metadata_new(
+                                       input_format==FORMAT_WAVE || input_format==FORMAT_RF64?
+                                               FOREIGN_BLOCK_TYPE__RIFF :
+                                       input_format==FORMAT_WAVE64?
+                                               FOREIGN_BLOCK_TYPE__WAVE64 :
+                                               FOREIGN_BLOCK_TYPE__AIFF
+                               );
+                       if(0 == encode_options.format_options.iff.foreign_metadata) {
                                flac__utils_printf(stderr, 1, "ERROR: creating foreign metadata object\n");
                                conditional_fclose(encode_infile);
+                               if(internal_outfilename != 0)
+                                       free(internal_outfilename);
                                return 1;
                        }
                }
@@ -1965,7 +1994,7 @@ int decode_file(const char *infilename)
        int retval;
        FLAC__bool treat_as_ogg = false;
        FileFormat output_format = FORMAT_WAVE;
-       decode_options_t common_options;
+       decode_options_t decode_options;
        const char *outfilename = get_decoded_outfilename(infilename);
 
        if(0 == outfilename) {
@@ -1990,6 +2019,16 @@ int decode_file(const char *infilename)
                (strlen(outfilename) >= 5 && 0 == FLAC__STRCASECMP(outfilename+(strlen(outfilename)-5), ".aiff"))
        )
                output_format = FORMAT_AIFF;
+       else if(
+               option_values.force_rf64_format ||
+               (strlen(outfilename) >= 5 && 0 == FLAC__STRCASECMP(outfilename+(strlen(outfilename)-5), ".rf64"))
+       )
+               output_format = FORMAT_RF64;
+       else if(
+               option_values.force_wave64_format ||
+               (strlen(outfilename) >= 4 && 0 == FLAC__STRCASECMP(outfilename+(strlen(outfilename)-4), ".w64"))
+       )
+               output_format = FORMAT_WAVE64;
        else
                output_format = FORMAT_WAVE;
 
@@ -2001,8 +2040,8 @@ int decode_file(const char *infilename)
        if(option_values.keep_foreign_metadata) {
                if(0 == strcmp(infilename, "-") || 0 == strcmp(outfilename, "-"))
                        return usage_error("ERROR: --keep-foreign-metadata cannot be used when decoding from stdin or to stdout\n");
-               if(output_format != FORMAT_WAVE && output_format != FORMAT_AIFF)
-                       return usage_error("ERROR: --keep-foreign-metadata can only be used with WAVE or AIFF output\n");
+               if(output_format != FORMAT_WAVE && output_format != FORMAT_WAVE64 && output_format != FORMAT_RF64 && output_format != FORMAT_AIFF && output_format != FORMAT_AIFF_C)
+                       return usage_error("ERROR: --keep-foreign-metadata can only be used with WAVE, Wave64, RF64, or AIFF output\n");
        }
 
        if(option_values.use_ogg)
@@ -2021,63 +2060,63 @@ int decode_file(const char *infilename)
        }
 #endif
 
-       if(!flac__utils_parse_skip_until_specification(option_values.skip_specification, &common_options.skip_specification) || common_options.skip_specification.is_relative)
+       if(!flac__utils_parse_skip_until_specification(option_values.skip_specification, &decode_options.skip_specification) || decode_options.skip_specification.is_relative)
                return usage_error("ERROR: invalid value for --skip\n");
 
-       if(!flac__utils_parse_skip_until_specification(option_values.until_specification, &common_options.until_specification)) /*@@@ more checks: no + without --skip, no - unless known total_samples_to_{en,de}code */
+       if(!flac__utils_parse_skip_until_specification(option_values.until_specification, &decode_options.until_specification)) /*@@@ more checks: no + without --skip, no - unless known total_samples_to_{en,de}code */
                return usage_error("ERROR: invalid value for --until\n");
        /* if there is no "--until" we want to default to "--until=-0" */
        if(0 == option_values.until_specification)
-               common_options.until_specification.is_relative = true;
+               decode_options.until_specification.is_relative = true;
 
        if(option_values.cue_specification) {
-               if(!flac__utils_parse_cue_specification(option_values.cue_specification, &common_options.cue_specification))
+               if(!flac__utils_parse_cue_specification(option_values.cue_specification, &decode_options.cue_specification))
                        return usage_error("ERROR: invalid value for --cue\n");
-               common_options.has_cue_specification = true;
+               decode_options.has_cue_specification = true;
        }
        else
-               common_options.has_cue_specification = false;
+               decode_options.has_cue_specification = false;
 
-       common_options.treat_warnings_as_errors = option_values.treat_warnings_as_errors;
-       common_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
-       common_options.replaygain_synthesis_spec = option_values.replaygain_synthesis_spec;
+       decode_options.treat_warnings_as_errors = option_values.treat_warnings_as_errors;
+       decode_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
+       decode_options.replaygain_synthesis_spec = option_values.replaygain_synthesis_spec;
 #if FLAC__HAS_OGG
-       common_options.is_ogg = treat_as_ogg;
-       common_options.use_first_serial_number = !option_values.has_serial_number;
-       common_options.serial_number = option_values.serial_number;
+       decode_options.is_ogg = treat_as_ogg;
+       decode_options.use_first_serial_number = !option_values.has_serial_number;
+       decode_options.serial_number = option_values.serial_number;
 #endif
-       common_options.channel_map_none = option_values.channel_map_none;
+       decode_options.channel_map_none = option_values.channel_map_none;
+       decode_options.format = output_format;
 
        if(output_format == FORMAT_RAW) {
-               raw_decode_options_t options;
-
-               options.common = common_options;
-               options.is_big_endian = option_values.format_is_big_endian;
-               options.is_unsigned_samples = option_values.format_is_unsigned_samples;
+               decode_options.format_options.raw.is_big_endian = option_values.format_is_big_endian;
+               decode_options.format_options.raw.is_unsigned_samples = option_values.format_is_unsigned_samples;
 
-               retval = flac__decode_raw(infilename, option_values.test_only? 0 : outfilename, option_values.analyze, option_values.aopts, options);
+               retval = flac__decode_file(infilename, option_values.test_only? 0 : outfilename, option_values.analyze, option_values.aopts, decode_options);
        }
        else {
-               wav_decode_options_t options;
+               decode_options.format_options.iff.foreign_metadata = 0;
 
-               options.common = common_options;
-               options.foreign_metadata = 0;
-
-               /* read foreign metadata if requested */
+               /* initialize foreign metadata if requested */
                if(option_values.keep_foreign_metadata) {
-                       if(0 == (options.foreign_metadata = flac__foreign_metadata_new(output_format==FORMAT_WAVE? FOREIGN_BLOCK_TYPE__RIFF : FOREIGN_BLOCK_TYPE__AIFF))) {
+                       decode_options.format_options.iff.foreign_metadata =
+                               flac__foreign_metadata_new(
+                                       output_format==FORMAT_WAVE || output_format==FORMAT_RF64?
+                                               FOREIGN_BLOCK_TYPE__RIFF :
+                                       output_format==FORMAT_WAVE64?
+                                               FOREIGN_BLOCK_TYPE__WAVE64 :
+                                               FOREIGN_BLOCK_TYPE__AIFF
+                               );
+                       if(0 == decode_options.format_options.iff.foreign_metadata) {
                                flac__utils_printf(stderr, 1, "ERROR: creating foreign metadata object\n");
                                return 1;
                        }
                }
 
-               if(output_format == FORMAT_WAVE)
-                       retval = flac__decode_wav(infilename, option_values.test_only? 0 : outfilename, option_values.analyze, option_values.aopts, options);
-               else
-                       retval = flac__decode_aiff(infilename, option_values.test_only? 0 : outfilename, option_values.analyze, option_values.aopts, options);
+               retval = flac__decode_file(infilename, option_values.test_only? 0 : outfilename, option_values.analyze, option_values.aopts, decode_options);
 
-               if(options.foreign_metadata)
-                       flac__foreign_metadata_delete(options.foreign_metadata);
+               if(decode_options.format_options.iff.foreign_metadata)
+                       flac__foreign_metadata_delete(decode_options.format_options.iff.foreign_metadata);
        }
 
        if(retval == 0 && strcmp(infilename, "-")) {
@@ -2108,6 +2147,12 @@ const char *get_decoded_outfilename(const char *infilename)
        else if(option_values.force_aiff_format) {
                suffix = ".aiff";
        }
+       else if(option_values.force_rf64_format) {
+               suffix = ".rf64";
+       }
+       else if(option_values.force_wave64_format) {
+               suffix = ".w64";
+       }
        else {
                suffix = ".wav";
        }
@@ -2168,29 +2213,3 @@ char *local_strdup(const char *source)
                die("out of memory during strdup()");
        return ret;
 }
-
-#ifdef _MSC_VER
-/* There's no strtoll() in MSVC6 so we just write a specialized one */
-FLAC__int64 local__strtoll(const char *src, char **endptr)
-{
-       FLAC__bool neg = false;
-       FLAC__int64 ret = 0;
-       int c;
-       FLAC__ASSERT(0 != src);
-       if(*src == '-') {
-               neg = true;
-               src++;
-       }
-       while(0 != (c = *src)) {
-               c -= '0';
-               if(c >= 0 && c <= 9)
-                       ret = (ret * 10) + c;
-               else
-                       break;
-               src++;
-       }
-       if(endptr)
-               *endptr = (char*)src;
-       return neg? -ret : ret;
-}
-#endif