shithub: opus-tools

Download patch

ref: a83af858c98094dfc6fed1d16585bb5e800b456d
parent: 98f3ddc8e94b8be31ebdeac52805a93cfab395e7
parent: c571319b8e57a7971b1c87aa4d660ee3f123ebe1
author: qwx <[email protected]>
date: Sat Nov 18 23:29:36 EST 2023

sync upstream

--- /dev/null
+++ b/mkfile
@@ -1,0 +1,51 @@
+APE=/sys/src/ape
+<$APE/config
+
+TARG=opusdec opusenc opusinfo
+BIN=/$objtype/bin/audio
+
+CFLAGS=$CFLAGS -c \
+	-Iinclude \
+	-I/sys/src/cmd/audio/libogg \
+	-I/sys/include/ape/opus \
+	-D_POSIX_SOURCE \
+	-D__GNU_LIBRARY__ \
+	-D_C99_SNPRINTF_EXTENSION \
+	-DSPX_RESAMPLE_EXPORT= \
+	-DRANDOM_PREFIX=opustools \
+	-DOUTSIDE_SPEEX \
+	-DRESAMPLE_FULL_SINC_TABLE \
+	-DOPUSTOOLS \
+	-DPACKAGE_NAME="opus-tools" \
+	-DPACKAGE_VERSION="fuckoff"
+
+LIB=\
+	/$objtype/lib/ape/libopusfile.a \
+	/$objtype/lib/ape/libopusenc.a \
+	/$objtype/lib/ape/libopus.a \
+	/sys/src/cmd/audio/libogg/libogg.a$O \
+
+</sys/src/cmd/mkmany
+
+%.$O:	src/%.c
+	$CC $CFLAGS $prereq
+
+%.$O:	share/%.c
+	$CC $CFLAGS $prereq
+
+%.$O:	/sys/src/cmd/audio/libogg/%.c
+	$CC $CFLAGS $prereq
+
+OGG=`{cd /sys/src/cmd/audio/libogg; echo *.c}
+OGG=${OGG:%.c=%.$O}
+
+/sys/src/cmd/audio/libogg/libogg.a$O:	$OGG
+	ar vu $target $OGG
+
+COMMON=opus_header.$O resample.$O getopt.$O getopt1.$O
+
+$O.opusdec:	opusdec.$O wav_io.$O wave_out.$O diag_range.$O $COMMON
+$O.opusenc:	opusenc.$O audio-in.$O diag_range.$O flac.$O picture.$O $COMMON
+$O.opusinfo:	opusinfo.$O info_opus.$O picture.$O tagcompare.$O $COMMON
+
+CLEANFILES=`{$CLEANFILES echo /sys/src/cmd/audio/libogg/libogg.a[$OS]}
--- a/src/audio-in.c
+++ b/src/audio-in.c
@@ -669,8 +669,8 @@
                this length so that we do not misinterpret any additional
                chunks after this as audio.  Also use this length to report
                percent progress. */
-            wav->totalsamples = opt->total_samples_per_channel =
-                len/(format.channels*samplesize);
+            wav->totalsamples = len/(format.channels*samplesize);
+            opt->total_samples_per_channel = wav->totalsamples;
         }
         else
         {
@@ -719,7 +719,11 @@
     int sampbyte = f->samplesize / 8;
     int realsamples = f->totalsamples > 0 && samples > (f->totalsamples - f->samplesread)
         ? (int)(f->totalsamples - f->samplesread) : samples;
-    signed char *buf = alloca(realsamples*sampbyte*f->channels);
+    signed char *buf = malloc(realsamples*sampbyte*f->channels);
+    if(buf == NULL){
+        fprintf(stderr, "allocation failure\n");
+        return 0;
+    }
     int i,j;
     int *ch_permute = f->channel_permute;
 
@@ -807,9 +811,11 @@
     else {
         fprintf(stderr, _("Internal error: attempt to read unsupported "
                           "bitdepth %d\n"), f->samplesize);
+        free(buf);
         return 0;
     }
 
+    free(buf);
     return realsamples;
 }
 
@@ -818,9 +824,14 @@
     wavfile *f = (wavfile *)in;
     int realsamples = f->totalsamples > 0 && samples > (f->totalsamples - f->samplesread)
         ? (int)(f->totalsamples - f->samplesread) : samples;
-    float *buf = alloca(realsamples*4*f->channels); /* de-interleave buffer */
+    float *buf = malloc(realsamples*4*f->channels); /* de-interleave buffer */
     int i,j;
 
+    if(buf == NULL){
+        fprintf(stderr, "allocation failure\n");
+        return 0;
+    }
+
     realsamples = (int)fread(buf, 4*f->channels, realsamples, f->f);
     f->samplesread += realsamples;
 
@@ -837,6 +848,7 @@
                     get_be_float(buf + i*f->channels + f->channel_permute[j]);
     }
 
+    free(buf);
     return realsamples;
 }
 
--- a/src/opusdec.c
+++ b/src/opusdec.c
@@ -445,27 +445,13 @@
     defined HAVE_SYS_AUDIOIO_H || defined WIN32 || defined _WIN32
    printf("Usage: opusdec [options] input [output]\n");
 #else
-   printf("Usage: opusdec [options] input output\n");
+   printf("Usage: opusdec [options]\n");
 #endif
-   printf("\n");
-   printf("Decode audio in Opus format to Wave or raw PCM\n");
-   printf("\n");
-   printf("input can be:\n");
-   printf("  file:filename.opus   Opus URL\n");
-   printf("  filename.opus        Opus file\n");
-   printf("  -                    stdin\n");
-   printf("\n");
-   printf("output can be:\n");
-   printf("  filename.wav         Wave file\n");
-   printf("  filename.*           Raw PCM file (any extension other than .wav)\n");
-   printf("  -                    stdout (raw; unless --force-wav)\n");
 #if defined HAVE_LIBSNDIO || defined HAVE_SYS_SOUNDCARD_H || \
     defined HAVE_MACHINE_SOUNDCARD_H || defined HAVE_SOUNDCARD_H || \
     defined HAVE_SYS_AUDIOIO_H || defined WIN32 || defined _WIN32
    printf("  (default)            Play audio\n");
 #endif
-   printf("\n");
-   printf("Options:\n");
    printf(" -h, --help            Show this help\n");
    printf(" -V, --version         Show version information\n");
    printf(" --quiet               Suppress program output\n");
@@ -477,7 +463,7 @@
    printf(" --force-wav           Force Wave header on output\n");
    printf(" --packet-loss n       Simulate n %% random packet loss\n");
    printf(" --save-range file     Save check values for every frame to a file\n");
-   printf("\n");
+   printf(" -s, --seek seconds    Seek within the stream before decoding\n");
 }
 
 void version(void)
@@ -503,8 +489,12 @@
    short *out;
    float *buf;
    float *output;
-   out=alloca(sizeof(short)*MAX_FRAME_SIZE*channels);
-   buf=alloca(sizeof(float)*MAX_FRAME_SIZE*channels);
+   out=malloc(sizeof(short)*MAX_FRAME_SIZE*channels);
+   buf=malloc(sizeof(float)*MAX_FRAME_SIZE*channels);
+   if(out == NULL || buf == NULL){
+      fprintf(stderr, "allocation failure\n");
+      return 0;
+   }
    maxout=((link_read/48000)*rate + (link_read%48000)*rate/48000) - link_out;
    maxout=maxout<0?0:maxout;
    do {
@@ -570,6 +560,8 @@
        maxout-=ret;
      }
    } while (frame_size>0 && maxout>0);
+   free(buf);
+   free(out);
    return sampout;
 }
 
@@ -686,11 +678,12 @@
    int file_output;
    int old_li=-1;
    int li;
-   int quiet = 0;
+   int quiet = 1;
    int forcewav = 0;
    ogg_int64_t nb_read_total=0;
    ogg_int64_t link_read=0;
    ogg_int64_t link_out=0;
+   double seek=0.0;
    struct option long_options[] =
    {
       {"help", no_argument, NULL, 0},
@@ -705,6 +698,7 @@
       {"force-wav", no_argument, NULL, 0},
       {"packet-loss", required_argument, NULL, 0},
       {"save-range", required_argument, NULL, 0},
+      {"seek", required_argument, NULL, 0},
       {0, 0, 0, 0}
    };
    opus_int64 audio_size=0;
@@ -712,10 +706,10 @@
    float loss_percent=-1;
    float manual_gain=0;
    int force_rate=0;
-   int force_stereo=0;
+   int force_stereo=1;
    int requested_channels=-1;
    int channels=-1;
-   int rate=0;
+   int rate=44100;
    int wav_format=0;
    int dither=1;
    int fp=0;
@@ -743,7 +737,7 @@
    /*Process options*/
    while (1)
    {
-      c = getopt_long(argc_utf8, argv_utf8, "hV",
+      c = getopt_long(argc_utf8, argv_utf8, "hVs:",
                        long_options, &option_index);
       if (c==-1)
          break;
@@ -790,6 +784,9 @@
          } else if (strcmp(long_options[option_index].name,"packet-loss")==0)
          {
             loss_percent = atof(optarg);
+         } else if (strcmp(long_options[option_index].name,"seek")==0)
+         {
+            seek = atof(optarg);
          }
          break;
       case 'h':
@@ -798,6 +795,9 @@
       case 'V':
          version();
          goto done;
+      case 's':
+         seek = atof(optarg);
+         break;
       case '?':
          usage();
          exit_code=1;
@@ -804,13 +804,7 @@
          goto done;
       }
    }
-   if (argc_utf8-optind!=2 && argc_utf8-optind!=1)
-   {
-      usage();
-      exit_code=1;
-      goto done;
-   }
-   inFile=argv_utf8[optind];
+   inFile="-";
 
    /*Output to a file or playback?*/
    file_output=argc_utf8-optind==2;
@@ -827,7 +821,7 @@
      }
      wav_format|=forcewav;
    } else {
-     outFile=NULL;
+     outFile="-";
      wav_format=0;
      /*If playing to audio out, default the rate to 48000
        instead of the original rate. The original rate is
@@ -857,8 +851,8 @@
    }
    else
    {
-      st=op_open_url(inFile,NULL,NULL);
-      if (st==NULL)
+      //st=op_open_url(inFile,NULL,NULL);
+      //if (st==NULL)
       {
          st=op_open_file(inFile,NULL);
       }
@@ -1014,6 +1008,14 @@
       op_set_decode_callback(st, (op_decode_cb_func)decode_cb, &cb_ctx);
    }
 
+   if (seek > 0.0) {
+      if (op_pcm_seek(st, seek*48000) != 0)
+      {
+         seek = 0.0;
+      }
+      fprintf(stderr, "time: %g\n", seek);
+   }
+
    /*Main decoding loop*/
    while (1)
    {
@@ -1066,7 +1068,8 @@
             resampler=NULL;
          }
          /*We've encountered a new link.*/
-         link_read=link_out=0;
+         link_read=0;
+         link_out=0;
          head=op_head(st, li);
          if (!force_stereo && channels!=head->channel_count)
          {
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -117,20 +117,12 @@
 
 static void usage(void)
 {
-  printf("Usage: opusenc [options] input_file output_file.opus\n");
-  printf("\n");
-  printf("Encode audio using Opus.\n");
+  printf("Usage: opusenc [options]\n");
 #if defined(HAVE_LIBFLAC)
   printf("The input format can be Wave, AIFF, FLAC, Ogg/FLAC, or raw PCM.\n");
 #else
   printf("The input format can be Wave, AIFF, or raw PCM.\n");
 #endif
-  printf("\ninput_file can be:\n");
-  printf("  filename.wav      file\n");
-  printf("  -                 stdin\n");
-  printf("\noutput_file can be:\n");
-  printf("  filename.opus     compressed file\n");
-  printf("  -                 stdout\n");
   printf("\nGeneral options:\n");
   printf(" -h, --help         Show this help\n");
   printf(" -V, --version      Show version information\n");
@@ -452,7 +444,7 @@
   time_t             last_spin=0;
   int                last_spin_len=0;
   /*Settings*/
-  int                quiet=0;
+  int                quiet=1;
   opus_int32         bitrate=-1;
   opus_int32         rate=48000;
   opus_int32         frame_size=960;
@@ -497,12 +489,12 @@
   in_format=NULL;
   inopt.channels=chan;
   inopt.channels_format=CHANNELS_FORMAT_DEFAULT;
-  inopt.rate=rate;
+  inopt.rate=44100;
   /* 0 dB gain is recommended unless you know what you're doing */
   inopt.gain=0;
   inopt.samplesize=16;
   inopt.endianness=0;
-  inopt.rawmode=0;
+  inopt.rawmode=1;
   inopt.rawmode_f=0;
   inopt.ignorelength=0;
   inopt.copy_comments=1;
@@ -852,12 +844,8 @@
     fatal("Invalid bit-depth:\n"
       "--raw-bits must be 32 for float sample format\n");
   }
-  if (argc_utf8-optind!=2) {
-    usage();
-    exit(1);
-  }
-  inFile=argv_utf8[optind];
-  outFile=argv_utf8[optind+1];
+  inFile="-";
+  outFile="-";
 
   if (cline_size > 0) {
     ret = ope_comments_add(inopt.comments, "ENCODER_OPTIONS", ENCODER_string);