shithub: opus-tools

Download patch

ref: ee74e156f0b51d0245ec70c09cb20ab7b2153865
parent: c7c73fe5c313242e4ec4ff26c63af39ab9af276d
author: Gregory Maxwell <[email protected]>
date: Sun Jul 22 13:19:18 EDT 2012

Cleanups, finish merging win32 unicode display.

--- a/Makefile.am
+++ b/Makefile.am
@@ -37,7 +37,7 @@
 opusdec_LDADD = $(OGG_LIBS) $(Opus_LIBS) -lm
 opusdec_MANS = man/opusdec.1
 
-opusinfo_SOURCES = src/opus_header.c src/opusinfo.c src/info_opus.c
+opusinfo_SOURCES = src/opus_header.c src/opusinfo.c src/info_opus.c win32/unicode_support.c
 opusinfo_LDADD = $(OGG_LIBS)
 opusinfo_MANS = man/opusinfo.1
 
--- a/src/opusdec.c
+++ b/src/opusdec.c
@@ -59,6 +59,8 @@
 #else
 # define I64FORMAT "lld"
 # define fopen_utf8(_x,_y) fopen((_x),(_y))
+# define argc_utf8 argc
+# define argv_utf8 argv
 #endif
 
 #include <math.h>
@@ -138,6 +140,13 @@
 # define fmaxf(_x,_y) ((_x)>(_y)?(_x):(_y))
 #endif
 
+static void quit(int _x) {
+#ifdef WIN_UNICODE
+  uninit_console_utf8();
+#endif
+  exit(_x);
+}
+
 /* This implements a 16 bit quantization with full triangular dither
    and IIR noise shaping. The noise shaping filters were designed by
    Sebastian Gesemann based on the LAME ATH curves with flattening
@@ -281,7 +290,7 @@
       if (audio_fd<0)
       {
          perror("Cannot open /dev/dsp");
-         exit(1);
+         quit(1);
       }
 
       format=AFMT_S16_NE;
@@ -289,7 +298,7 @@
       {
          perror("SNDCTL_DSP_SETFMT");
          close(audio_fd);
-         exit(1);
+         quit(1);
       }
 
       stereo=0;
@@ -299,7 +308,7 @@
       {
          perror("SNDCTL_DSP_STEREO");
          close(audio_fd);
-         exit(1);
+         quit(1);
       }
       if (stereo!=0)
       {
@@ -312,13 +321,13 @@
       {
          perror("SNDCTL_DSP_SPEED");
          close(audio_fd);
-         exit(1);
+         quit(1);
       }
       fout = fdopen(audio_fd, "w");
       if(!fout)
       {
         perror("Cannot open output");
-        exit(1);
+        quit(1);
       }
 #elif defined HAVE_LIBSNDIO
       struct sio_par par;
@@ -327,7 +336,7 @@
       if (!hdl)
       {
          fprintf(stderr, "Cannot open sndio device\n");
-         exit(1);
+         quit(1);
       }
 
       sio_initpar(&par);
@@ -339,12 +348,12 @@
       if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par) ||
         par.sig != 1 || par.bits != 16 || par.rate != rate) {
           fprintf(stderr, "could not set sndio parameters\n");
-          exit(1);
+          quit(1);
       }
       *channels = par.pchan;
       if (!sio_start(hdl)) {
           fprintf(stderr, "could not start sndio\n");
-          exit(1);
+          quit(1);
       }
 #elif defined HAVE_SYS_AUDIOIO_H
       audio_info_t info;
@@ -354,7 +363,7 @@
       if (audio_fd<0)
       {
          perror("Cannot open /dev/audio");
-         exit(1);
+         quit(1);
       }
 
       AUDIO_INITINFO(&info);
@@ -369,13 +378,13 @@
       if (ioctl(audio_fd, AUDIO_SETINFO, &info) < 0)
       {
          perror ("AUDIO_SETINFO");
-         exit(1);
+         quit(1);
       }
       fout = fdopen(audio_fd, "w");
       if(!fout)
       {
         perror("Cannot open output");
-        exit(1);
+        quit(1);
       }
 #elif defined WIN32 || defined _WIN32
       {
@@ -383,12 +392,12 @@
          if (Set_WIN_Params (INVALID_FILEDESC, rate, SAMPLE_SIZE, opus_channels))
          {
             fprintf (stderr, "Can't access %s\n", "WAVE OUT");
-            exit(1);
+            quit(1);
          }
       }
 #else
       fprintf (stderr, "No soundcard support\n");
-      exit(1);
+      quit(1);
 #endif
    } else {
       if (strcmp(outFile,"-")==0)
@@ -404,7 +413,7 @@
          if (!fout)
          {
             perror(outFile);
-            exit(1);
+            quit(1);
          }
          if (*wav_format)
          {
@@ -412,7 +421,7 @@
             if (*wav_format < 0)
             {
                fprintf (stderr, "Error writing WAV header.\n");
-               exit(1);
+               quit(1);
             }
          }
       }
@@ -525,8 +534,8 @@
       fprintf(stderr, " (%d channel%s)",*channels,*channels>1?"s":"");
       if(header.version!=1)fprintf(stderr, ", Header v%d",header.version);
       fprintf(stderr, "\n");
-      if (header.gain!=0)printf("Playback gain: %f dB\n", header.gain/256.);
-      if (manual_gain!=0)printf("Manual gain: %f dB\n", manual_gain);
+      if (header.gain!=0)fprintf(stderr,"Playback gain: %f dB\n", header.gain/256.);
+      if (manual_gain!=0)fprintf(stderr,"Manual gain: %f dB\n", manual_gain);
    }
 
    return st;
@@ -600,11 +609,7 @@
    return sampout;
 }
 
-#ifdef WIN_UNICODE
-static int opusdec_main(int argc, char **argv)
-#else
 int main(int argc, char **argv)
-#endif
 {
    int c;
    int option_index = 0;
@@ -639,6 +644,7 @@
    int close_in=0;
    int eos=0;
    ogg_int64_t audio_size=0;
+   double last_coded_seconds=0;
    float loss_percent=-1;
    float manual_gain=0;
    int channels=-1;
@@ -655,7 +661,17 @@
    float gain=1;
    int streams=0;
    size_t last_spin=0;
+#ifdef WIN_UNICODE
+   int argc_utf8;
+   char **argv_utf8;
 
+   (void)argc;
+   (void)argv;
+
+   init_console_utf8();
+   init_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
+#endif
+
    output=0;
    shapemem.a_buf=0;
    shapemem.b_buf=0;
@@ -665,7 +681,7 @@
    /*Process options*/
    while(1)
    {
-      c = getopt_long (argc, argv, "hvV",
+      c = getopt_long (argc_utf8, argv_utf8, "hvV",
                        long_options, &option_index);
       if (c==-1)
          break;
@@ -676,7 +692,7 @@
          if (strcmp(long_options[option_index].name,"help")==0)
          {
             usage();
-            exit(0);
+            quit(0);
          } else if (strcmp(long_options[option_index].name,"quiet")==0)
          {
             quiet = 1;
@@ -683,11 +699,11 @@
          } else if (strcmp(long_options[option_index].name,"version")==0)
          {
             version();
-            exit(0);
+            quit(0);
          } else if (strcmp(long_options[option_index].name,"version-short")==0)
          {
             version_short();
-            exit(0);
+            quit(0);
          } else if (strcmp(long_options[option_index].name,"no-dither")==0)
          {
             dither=0;
@@ -703,7 +719,7 @@
             perror(optarg);
             fprintf(stderr,"Could not open save-range file: %s\n",optarg);
             fprintf(stderr,"Must provide a writable file name.\n");
-            exit(1);
+            quit(1);
           }
          } else if (strcmp(long_options[option_index].name,"packet-loss")==0)
          {
@@ -712,27 +728,27 @@
          break;
       case 'h':
          usage();
-         exit(0);
+         quit(0);
          break;
       case 'v':
          version();
-         exit(0);
+         quit(0);
          break;
       case '?':
          usage();
-         exit(1);
+         quit(1);
          break;
       }
    }
-   if (argc-optind!=2 && argc-optind!=1)
+   if (argc_utf8-optind!=2 && argc_utf8-optind!=1)
    {
       usage();
-      exit(1);
+      quit(1);
    }
-   inFile=argv[optind];
+   inFile=argv_utf8[optind];
 
-   if (argc-optind==2)
-      outFile=argv[optind+1];
+   if (argc_utf8-optind==2)
+      outFile=argv_utf8[optind+1];
    else
       outFile = "";
    wav_format = strlen(outFile)>=4 && (
@@ -752,7 +768,7 @@
       if (!fin)
       {
          perror(inFile);
-         exit(1);
+         quit(1);
       }
       close_in=1;
    }
@@ -810,7 +826,7 @@
             {
                st = process_header(&op, &rate, &mapping_family, &channels, &preskip, &gain, manual_gain, &streams, wav_format, quiet);
                if (!st)
-                  exit(1);
+                  quit(1);
 
                /*Remember how many samples at the front we were told to skip
                  so that we can adjust the timestamp counting.*/
@@ -878,16 +894,15 @@
 
                if(!quiet){
                   static const char spinner[]="|/-\\";
-                  if(!(last_spin % 128)) {
-                     unsigned int sec, min, hrs;
-                     unsigned int total_secs = (unsigned int)(audio_size / (((ogg_int64_t) channels) * ((ogg_int64_t) rate) * ((ogg_int64_t) 2)));
-                     hrs = total_secs / 3600;
-                     min = (total_secs % 3600) / 60;
-                     sec = (total_secs % 3600) % 60;
-                     fprintf(stderr,"[%c] %02u:%02u:%02u\r", spinner[(last_spin/128) % 4], hrs, min, sec);
+                  double coded_seconds = (double)audio_size/(channels*rate*sizeof(short));
+                  if(coded_seconds>=last_coded_seconds+1){
+                     fprintf(stderr,"\r[%c] %02d:%02d:%02d", spinner[last_spin&3],
+                             (int)(coded_seconds/3600),(int)(coded_seconds/60)%60,
+                             (int)(coded_seconds)%60);
                      fflush(stderr);
+                     last_spin++;
+                     last_coded_seconds=coded_seconds;
                   }
-                  last_spin++;
                }
 
                if (ret<0)
@@ -957,7 +972,7 @@
       }
       if (feof(fin)) {
          if(!quiet) {
-           fprintf(stderr, "Decoding complete.\n");
+           fprintf(stderr, "\rDecoding complete.        \n");
            fflush(stderr);
          }
          break;
@@ -965,7 +980,7 @@
    }
 
    /*If we were writing wav, go set the duration.*/
-   if (fout && wav_format>=0 && audio_size<0x7FFFFFFF)
+   if (strlen(outFile)!=0 && fout && wav_format>=0 && audio_size<0x7FFFFFFF)
    {
       if (fseek(fout,4,SEEK_SET)==0)
       {
@@ -1008,20 +1023,10 @@
    if (fout != NULL)
       fclose(fout);
 
-   return 0;
-}
-
 #ifdef WIN_UNICODE
-int main( int argc, char **argv )
-{
-  int my_argc;
-  char **my_argv;
-  int exit_code;
+   free_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
+   uninit_console_utf8();
+#endif
 
-  init_commandline_arguments_utf8(&my_argc, &my_argv);
-  exit_code = opusdec_main(my_argc, my_argv);
-  free_commandline_arguments_utf8(&my_argc, &my_argv);
-
-  return exit_code;
+   return 0;
 }
-#endif
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -55,6 +55,8 @@
 #else
 # define I64FORMAT "lld"
 # define fopen_utf8(_x,_y) fopen((_x),(_y))
+# define argc_utf8 argc
+# define argv_utf8 argv
 #endif
 
 #include <opus.h>
@@ -75,7 +77,6 @@
 #define VG_CHECK(x,y)
 #endif
 
-
 static void comment_init(char **comments, int* length, const char *vendor_string);
 static void comment_add(char **comments, int* length, char *tag, char *val);
 
@@ -170,11 +171,7 @@
   if(seconds>0)fprintf(stderr," %0.4g second%s",seconds,seconds!=1?"s":"");
 }
 
-#ifdef WIN_UNICODE
-static int opusenc_main(int argc, char **argv)
-#else
 int main(int argc, char **argv)
-#endif
 {
   static const input_format raw_format = {NULL, 0, raw_open, wav_close, "raw",N_("RAW file reader")};
   int option_index=0;
@@ -274,7 +271,16 @@
   opus_int32         lookahead=0;
   unsigned char      mapping[256];
   int                force_narrow=0;
+#ifdef WIN_UNICODE
+   int argc_utf8;
+   char **argv_utf8;
 
+   (void)argc;
+   (void)argv;
+
+   init_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
+#endif
+
   opt_ctls_ctlval=NULL;
   frange=NULL;
   range_file=NULL;
@@ -298,7 +304,7 @@
   /*Process command-line options*/
   while(1){
     int c;
-    c=getopt_long(argc, argv, "hv",
+    c=getopt_long(argc_utf8, argv_utf8, "hv",
                   long_options, &option_index);
     if(c==-1)
        break;
@@ -455,12 +461,12 @@
         break;
     }
   }
-  if(argc-optind!=2){
+  if(argc_utf8-optind!=2){
     usage();
     exit(1);
   }
-  inFile=argv[optind];
-  outFile=argv[optind+1];
+  inFile=argv_utf8[optind];
+  outFile=argv_utf8[optind+1];
 
   if(strcmp(inFile, "-")==0){
 #if defined WIN32 || defined _WIN32
@@ -958,6 +964,9 @@
   if(fin)fclose(fin);
   if(fout)fclose(fout);
   if(frange)fclose(frange);
+#ifdef WIN_UNICODE
+   free_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
+#endif
   return 0;
 }
 
@@ -1032,18 +1041,3 @@
 }
 #undef readint
 #undef writeint
-
-#ifdef WIN_UNICODE
-int main( int argc, char **argv )
-{
-  int my_argc;
-  char **my_argv;
-  int exit_code;
-
-  init_commandline_arguments_utf8(&my_argc, &my_argv);
-  exit_code = opusenc_main(my_argc, my_argv);
-  free_commandline_arguments_utf8(&my_argc, &my_argv);
-
-  return exit_code;
-}
-#endif
--- a/src/opusinfo.c
+++ b/src/opusinfo.c
@@ -35,6 +35,8 @@
 # include "unicode_support.h"
 #else
 # define fopen_utf8(_x,_y) fopen((_x),(_y))
+# define argc_utf8 argc
+# define argv_utf8 argv
 #endif
 
 #define CHUNK 4500
@@ -589,15 +591,22 @@
     printf (_("\t-V Output version information and exit.\n"));
 }
 
-#ifdef WIN_UNICODE
-static int opusinfo_main(int argc, char **argv)
-#else
 int main(int argc, char **argv)
-#endif
 {
     int f, ret;
 
-    if(argc < 2) {
+#ifdef WIN_UNICODE
+    int argc_utf8;
+    char **argv_utf8;
+
+    (void)argc;
+    (void)argv;
+
+    init_console_utf8();
+    init_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
+#endif
+
+    if(argc_utf8 < 2) {
         fprintf(stdout,
                 _("Usage: opusinfo [flags] file1.opus [file2.opus ... fileN.opus]\n"
                   "\n"
@@ -604,10 +613,13 @@
                   "opusinfo is a tool for printing information about Opus files\n"
                   "and for diagnosing problems with them.\n"
                   "Full help shown with \"opusinfo -h\".\n"));
+#ifdef WIN_UNICODE
+        uninit_console_utf8();
+#endif
         exit(1);
     }
 
-    while((ret = getopt(argc, argv, "hqvV")) >= 0) {
+    while((ret = getopt(argc_utf8, argv_utf8, "hqvV")) >= 0) {
         switch(ret) {
             case 'h':
                 usage();
@@ -631,7 +643,7 @@
     if(verbose < 0)
         printwarn = 0;
 
-    if(optind >= argc) {
+    if(optind >= argc_utf8) {
         fprintf(stderr,
                 _("No input files specified. \"opusinfo -h\" for help\n"));
         return 1;
@@ -639,27 +651,17 @@
 
     ret = 0;
 
-    for(f=optind; f < argc; f++) {
+    for(f=optind; f < argc_utf8; f++) {
         flawed = 0;
-        process_file(argv[f]);
+        process_file(argv_utf8[f]);
         if(flawed != 0)
             ret = flawed;
     }
 
-    return ret;
-}
-
 #ifdef WIN_UNICODE
-int main( int argc, char **argv )
-{
-  int my_argc;
-  char **my_argv;
-  int exit_code;
+   free_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
+   uninit_console_utf8();
+#endif
 
-  init_commandline_arguments_utf8(&my_argc, &my_argv);
-  exit_code = opusinfo_main(my_argc, my_argv);
-  free_commandline_arguments_utf8(&my_argc, &my_argv);
-
-  return exit_code;
+    return ret;
 }
-#endif
\ No newline at end of file