decoder excitation now in 16-bit precision (was 32), which saves quite a bit
[speexdsp.git] / src / speexdec.c
index b11d548..2a3876a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2002-2003 Jean-Marc Valin 
+/* Copyright (C) 2002-2006 Jean-Marc Valin 
    File: speexdec.c
 
    Redistribution and use in source and binary forms, with or without
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <stdio.h>
 #if !defined WIN32 && !defined _WIN32
 #include <unistd.h>
@@ -40,8 +44,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "speex.h"
-#include "ogg/ogg.h"
+#include <speex/speex.h>
+#include <ogg/ogg.h>
 
 #if defined WIN32 || defined _WIN32
 #include <windows.h>
 
 #include <string.h>
 #include "wav_io.h"
-#include "speex_header.h"
-#include "speex_stereo.h"
-#include "speex_callbacks.h"
-#include "misc.h"
+#include <speex/speex_header.h>
+#include <speex/speex_stereo.h>
+#include <speex/speex_callbacks.h>
+#include "wav_io.h"
 
 #define MAX_FRAME_SIZE 2000
 
@@ -285,14 +289,14 @@ void usage()
 
 void version()
 {
-   printf ("speexdec (Speex decoder) version " VERSION " (compiled " __DATE__ ")\n");
-   printf ("Copyright (C) 2002-2003 Jean-Marc Valin\n");
+   printf ("speexdec (Speex decoder) version " SPEEX_VERSION " (compiled " __DATE__ ")\n");
+   printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n");
 }
 
 void version_short()
 {
-   printf ("speexdec version " VERSION "\n");
-   printf ("Copyright (C) 2002-2003 Jean-Marc Valin\n");
+   printf ("speexdec version " SPEEX_VERSION "\n");
+   printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n");
 }
 
 static void *process_header(ogg_packet *op, int enh_enabled, int *frame_size, int *rate, int *nframes, int forceMode, int *channels, SpeexStereoState *stereo, int *extra_headers, int quiet)
@@ -309,7 +313,7 @@ static void *process_header(ogg_packet *op, int enh_enabled, int *frame_size, in
       fprintf (stderr, "Cannot read header\n");
       return NULL;
    }
-   if (header->mode >= SPEEX_NB_MODES)
+   if (header->mode >= SPEEX_NB_MODES || header->mode<0)
    {
       fprintf (stderr, "Mode number %d does not (yet/any longer) exist in this version\n", 
                header->mode);
@@ -319,7 +323,8 @@ static void *process_header(ogg_packet *op, int enh_enabled, int *frame_size, in
    modeID = header->mode;
    if (forceMode!=-1)
       modeID = forceMode;
-   mode = speex_mode_list[modeID];
+
+   mode = speex_lib_get_mode (modeID);
    
    if (header->speex_version_id > 1)
    {
@@ -410,6 +415,8 @@ int main(int argc, char **argv)
    int packet_count=0;
    int stream_init = 0;
    int quiet = 0;
+   ogg_int64_t page_granule=0, last_granule=0;
+   int skip_samples=0, page_nb_packets;
    struct option long_options[] =
    {
       {"help", no_argument, NULL, 0},
@@ -446,6 +453,7 @@ int main(int argc, char **argv)
    int rate=0;
    int extra_headers;
    int wav_format=0;
+   int lookahead;
 
    enh_enabled = 1;
 
@@ -542,7 +550,7 @@ int main(int argc, char **argv)
       outFile = "";
    wav_format = strlen(outFile)>=4 && (
                                        strcmp(outFile+strlen(outFile)-4,".wav")==0
-                                       || strcmp(inFile+strlen(inFile)-4,".WAV")==0);
+                                       || strcmp(outFile+strlen(outFile)-4,".WAV")==0);
    /*Open input file*/
    if (strcmp(inFile, "-")==0)
    {
@@ -581,19 +589,38 @@ int main(int argc, char **argv)
       /*Loop for all complete pages we got (most likely only one)*/
       while (ogg_sync_pageout(&oy, &og)==1)
       {
+         int packet_no;
          if (stream_init == 0) {
             ogg_stream_init(&os, ogg_page_serialno(&og));
             stream_init = 1;
          }
          /*Add page to the bitstream*/
          ogg_stream_pagein(&os, &og);
+         page_granule = ogg_page_granulepos(&og);
+         page_nb_packets = ogg_page_packets(&og);
+         if (page_granule>0 && frame_size)
+         {
+            /* FIXME: shift the granule values if --force-* is specified */
+            skip_samples = page_nb_packets*frame_size*nframes - (page_granule-last_granule);
+            if (ogg_page_eos(&og))
+               skip_samples = -skip_samples;
+            /*else if (!ogg_page_bos(&og))
+               skip_samples = 0;*/
+         } else
+         {
+            skip_samples = 0;
+         }
+         /*printf ("page granulepos: %d %d %d\n", skip_samples, page_nb_packets, (int)page_granule);*/
+         last_granule = page_granule;
          /*Extract all available packets*/
+         packet_no=0;
          while (!eos && ogg_stream_packetout(&os, &op)==1)
          {
             /*If first packet, process as Speex header*/
             if (packet_count==0)
             {
                st = process_header(&op, enh_enabled, &frame_size, &rate, &nframes, forceMode, &channels, &stereo, &extra_headers, quiet);
+               speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
                if (!nframes)
                   nframes=1;
                if (!st)
@@ -608,8 +635,8 @@ int main(int argc, char **argv)
             {
                /* Ignore extra headers */
             } else {
-               
                int lost=0;
+               packet_no++;
                if (loss_percent>0 && 100*((float)rand())/RAND_MAX<loss_percent)
                   lost=1;
 
@@ -662,14 +689,39 @@ int main(int argc, char **argv)
                      for (i=0;i<frame_size*channels;i++)
                         out[i]=output[i];
                  }
+                  {
+                     int frame_offset = 0;
+                     int new_frame_size = frame_size;
+                     /*printf ("packet %d %d\n", packet_no, skip_samples);*/
+                     /*fprintf (stderr, "packet %d %d %d\n", packet_no, skip_samples, lookahead);*/
+                     if (packet_no == 1 && j==0 && skip_samples > 0)
+                     {
+                        /*printf ("chopping first packet\n");*/
+                        new_frame_size -= skip_samples+lookahead;
+                        frame_offset = skip_samples+lookahead;
+                     }
+                     if (packet_no == page_nb_packets && skip_samples < 0)
+                     {
+                        int packet_length = nframes*frame_size+skip_samples+lookahead;
+                        new_frame_size = packet_length - j*frame_size;
+                        if (new_frame_size<0)
+                           new_frame_size = 0;
+                        if (new_frame_size>frame_size)
+                           new_frame_size = frame_size;
+                        /*printf ("chopping end: %d %d %d\n", new_frame_size, packet_length, packet_no);*/
+                     }
+                     if (new_frame_size>0)
+                     {  
 #if defined WIN32 || defined _WIN32
-                  if (strlen(outFile)==0)
-                      WIN_Play_Samples (out, sizeof(short) * frame_size*channels);
-                  else
+                        if (strlen(outFile)==0)
+                           WIN_Play_Samples (out+frame_offset*channels, sizeof(short) * new_frame_size*channels);
+                        else
 #endif
-                  fwrite(out, sizeof(short), frame_size*channels, fout);
+                           fwrite(out+frame_offset*channels, sizeof(short), new_frame_size*channels, fout);
                   
-                  audio_size+=sizeof(short)*frame_size*channels;
+                        audio_size+=sizeof(short)*new_frame_size*channels;
+                     }
+                  }
                }
             }
             packet_count++;