shithub: opus-tools

Download patch

ref: c571319b8e57a7971b1c87aa4d660ee3f123ebe1
parent: 86f3d8f8218acbf2c1b9bd5cc5cbdec1c5eaaa6d
author: qwx <qwx@sciops.net>
date: Mon Mar 6 17:15:41 EST 2023

implement seeking for opusdec (thanks sigrid)

--- a/src/opusdec.c
+++ b/src/opusdec.c
@@ -463,6 +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(" -s, --seek seconds    Seek within the stream before decoding\n");
 }
 
 void version(void)
@@ -682,6 +683,7 @@
    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},
@@ -696,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;
@@ -734,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;
@@ -781,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':
@@ -789,6 +795,9 @@
       case 'V':
          version();
          goto done;
+      case 's':
+         seek = atof(optarg);
+         break;
       case '?':
          usage();
          exit_code=1;
@@ -997,6 +1006,14 @@
       cb_ctx.loss_percent=loss_percent;
       cb_ctx.frange=frange;
       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*/