speexenc and speexdec now use the Ogg bitstream. This is very preliminary
authorjmvalin <jmvalin@0101bb08-14d6-0310-b084-bc0e0c8e3800>
Tue, 14 May 2002 21:58:51 +0000 (21:58 +0000)
committerjmvalin <jmvalin@0101bb08-14d6-0310-b084-bc0e0c8e3800>
Tue, 14 May 2002 21:58:51 +0000 (21:58 +0000)
there are probably a couple dozens of bugs...

git-svn-id: http://svn.xiph.org/trunk/speex@3304 0101bb08-14d6-0310-b084-bc0e0c8e3800

configure.in
libspeex/nb_celp.c
src/speexdec.c
src/speexenc.c

index 9faeb24..ee3225d 100644 (file)
@@ -31,7 +31,9 @@ AC_DISABLE_SHARED
 dnl AC_DISABLE_STATIC
 AM_PROG_LIBTOOL
 
 dnl AC_DISABLE_STATIC
 AM_PROG_LIBTOOL
 
+AC_CHECK_HEADER(ogg/ogg.h)
 AC_CHECK_LIB(m, sin)
 AC_CHECK_LIB(m, sin)
+AC_CHECK_LIB(ogg, ogg_stream_init, , [ echo; echo "error: libogg is now required by speex"; echo "Get it from: http://www.ogg.org/ogg/index.html"; exit 1 ])
 AC_FUNC_ALLOCA
 dnl AC_DEFINE(VERSION, "$VERSION")
 AC_DEFINE_UNQUOTED(VERSION, "${VERSION}")
 AC_FUNC_ALLOCA
 dnl AC_DEFINE(VERSION, "$VERSION")
 AC_DEFINE_UNQUOTED(VERSION, "${VERSION}")
index b6f6a5c..2d7bc42 100644 (file)
@@ -468,7 +468,7 @@ void nb_encode(void *state, float *in, FrameBits *bits)
                qe=63;
             ener = exp(qe/7.0);
             speex_bits_pack(bits, qe, 6);
                qe=63;
             ener = exp(qe/7.0);
             speex_bits_pack(bits, qe, 6);
-            printf ("quant_energy: %d %f\n", qe, ener);
+            /*printf ("quant_energy: %d %f\n", qe, ener);*/
          }
          ener_1 = 1/ener;
          
          }
          ener_1 = 1/ener;
          
@@ -687,7 +687,7 @@ void nb_decode(void *state, FrameBits *bits, float *out)
 
          q_energy = speex_bits_unpack_unsigned(bits, 6);
          ener = exp(q_energy/7.0);
 
          q_energy = speex_bits_unpack_unsigned(bits, 6);
          ener = exp(q_energy/7.0);
-         printf ("unquant_energy: %d %f\n", q_energy, ener);
+         /*printf ("unquant_energy: %d %f\n", q_energy, ener);*/
          
          /*Fixed codebook contribution*/
          st->innovation_unquant(innov, st->innovation_params, st->subframeSize, bits, st->stack);
          
          /*Fixed codebook contribution*/
          st->innovation_unquant(innov, st->innovation_params, st->subframeSize, bits, st->stack);
index 2674d74..3d4e575 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include "speex.h"
 #include <string.h>
 
 #include "speex.h"
+#include "ogg/ogg.h"
 
 #define MAX_FRAME_SIZE 2000
 #define MAX_FRAME_BYTES 1000
 
 #define MAX_FRAME_SIZE 2000
 #define MAX_FRAME_BYTES 1000
@@ -52,20 +53,21 @@ int main(int argc, char **argv)
    SpeexMode *mode=NULL;
    void *st;
    FrameBits bits;
    SpeexMode *mode=NULL;
    void *st;
    FrameBits bits;
-   char cbits[MAX_FRAME_BYTES];
-
-   int at_end=0;
-   int narrowband=0, wideband=0;
+   int first = 1;
    struct option long_options[] =
    {
       {"help", no_argument, NULL, 0},
       {"version", no_argument, NULL, 0},
       {0, 0, 0, 0}
    };
    struct option long_options[] =
    {
       {"help", no_argument, NULL, 0},
       {"version", no_argument, NULL, 0},
       {0, 0, 0, 0}
    };
-   
+   ogg_sync_state oy;
+   ogg_page       og;
+   ogg_packet     op;
+   ogg_stream_state os;
+
    while(1)
    {
    while(1)
    {
-      c = getopt_long (argc, argv, "nwhv",
+      c = getopt_long (argc, argv, "hv",
                        long_options, &option_index);
       if (c==-1)
          break;
                        long_options, &option_index);
       if (c==-1)
          break;
@@ -127,64 +129,66 @@ int main(int argc, char **argv)
       }
    }
 
       }
    }
 
-   /*This is only the temporary header*/
-   {
-      char header[6];
-      if(fread(header, sizeof(char), 5, fin)!=5)
-      {
-         perror("cannot read header");
-         exit(1);
-      }
-      header[5]=0;
-      if (strcmp(header,"spexn")==0)
-      {
-         mode=&speex_nb_mode;
-         narrowband=1;
-      } else if (strcmp(header,"spexw")==0)
-      {
-         mode=&speex_wb_mode;
-         wideband=1;
-      } else 
-      {
-         fprintf (stderr, "This does not look like a Speex " VERSION " file\n");
-         exit(1);
-      }
-
-   }
-
-   st = decoder_init(mode);
+   ogg_sync_init(&oy);
+   ogg_stream_init(&os, 0);
+   
+   /* FIXME: Read header here...*/
+   /*st = decoder_init(mode);*/
 
 
-   frame_size=mode->frameSize;
+   /*frame_size=mode->frameSize;*/
 
    speex_bits_init(&bits);
 
    speex_bits_init(&bits);
-   while (1)
+   while (1/*||feof(fin)*/)
    {
    {
+      char *data;
       int i, nb_read;
       int i, nb_read;
-      nb_read=200-((bits.nbBits>>3)-bits.bytePtr);
-      if (nb_read>0&&!at_end)
+      data = ogg_sync_buffer(&oy, 200);
+      nb_read = fread(data, sizeof(char), 200, fin);      
+      ogg_sync_wrote(&oy, nb_read);
+      while (ogg_sync_pageout(&oy, &og)==1)
       {
       {
-         nb_read=fread(cbits, sizeof(char), nb_read, fin);
-         if (feof(fin))
-            at_end=1;
-         if (nb_read>0 && !at_end)
-            speex_bits_read_whole_bytes(&bits, cbits, nb_read);
+         ogg_stream_pagein(&os, &og);
+         while (ogg_stream_packetout(&os, &op)==1)
+         {
+            if (first)
+            {
+               if (strncmp((char *)op.packet, "speex wideband**", 12)==0)
+               {
+                  fprintf (stderr, "Wideband!!\n");
+                  mode = &speex_wb_mode;
+               } else if (strncmp((char *)op.packet, "speex narrowband", 12)==0)
+               {
+                  fprintf (stderr, "Narrowband!!\n");
+                  mode = &speex_nb_mode;
+               } else {
+                  fprintf (stderr, "This Ogg file is not a Speex bitstream\n");
+                  exit(1);
+               }
+               st = decoder_init(mode);
+               frame_size=mode->frameSize;
+               first=0;
+            } else {
+               if (strncmp((char *)op.packet, "END OF STREAM", 13)==0)
+                  break;
+               speex_bits_init_from(&bits, (char*)op.packet, op.bytes);
+               decode(st, &bits, output);
+               
+               for (i=0;i<frame_size;i++)
+               {
+                  if (output[i]>32000)
+                     output[i]=32000;
+                  else if (output[i]<-32000)
+                     output[i]=-32000;
+               }
+               for (i=0;i<frame_size;i++)
+                  out[i]=output[i];
+               fwrite(out, sizeof(short), frame_size, fout);
+            }
+         }
       }
       }
-      
-      if (((bits.nbBits>>3)-bits.bytePtr)<2)
+      if (feof(fin))
          break;
          break;
-      
-      decode(st, &bits, output);
 
 
-      for (i=0;i<frame_size;i++)
-      {
-         if (output[i]>32000)
-            output[i]=32000;
-         else if (output[i]<-32000)
-            output[i]=-32000;
-      }
-      for (i=0;i<frame_size;i++)
-         out[i]=output[i];
-      fwrite(out, sizeof(short), frame_size, fout);
    }
 
    decoder_destroy(st);
    }
 
    decoder_destroy(st);
index ea79a83..670fb4e 100644 (file)
 #include <string.h>
 
 #include "speex.h"
 #include <string.h>
 
 #include "speex.h"
+#include <ogg/ogg.h>
+
+int oe_write_page(ogg_page *page, FILE *fp)
+{
+       int written;
+       written = fwrite(page->header,1,page->header_len, fp);
+       written += fwrite(page->body,1,page->body_len, fp);
+
+       return written;
+}
 
 #define MAX_FRAME_SIZE 2000
 #define MAX_FRAME_BYTES 1000
 
 #define MAX_FRAME_SIZE 2000
 #define MAX_FRAME_BYTES 1000
@@ -66,6 +76,15 @@ int main(int argc, char **argv)
       {"version", no_argument, NULL, 0},
       {0, 0, 0, 0}
    };
       {"version", no_argument, NULL, 0},
       {0, 0, 0, 0}
    };
+   /*ogg_stream_state *ogg_state;
+   ogg_packet packet;
+   ogg_page page;*/
+
+   ogg_stream_state os;
+   ogg_page             og;
+   ogg_packet           op;
+   int bytes_written, ret, result;
+   int id=0;
 
    while(1)
    {
 
    while(1)
    {
@@ -118,6 +137,12 @@ int main(int argc, char **argv)
    inFile=argv[optind];
    outFile=argv[optind+1];
 
    inFile=argv[optind];
    outFile=argv[optind+1];
 
+   if (ogg_stream_init(&os, 0)==-1)
+   {
+      fprintf(stderr,"Stream init failed\n");
+      exit(1);
+   }
+
    if (wideband && narrowband)
    {
       fprintf (stderr,"Cannot specify both wideband and narrowband at the same time\n");
    if (wideband && narrowband)
    {
       fprintf (stderr,"Cannot specify both wideband and narrowband at the same time\n");
@@ -158,10 +183,36 @@ int main(int argc, char **argv)
    /*Temporary header*/
    {
       char *header="";
    /*Temporary header*/
    {
       char *header="";
-      if (narrowband)
+      /*if (narrowband)
          header="spexn";
       if (wideband)
          header="spexn";
       if (wideband)
-         header="spexw";
+      header="spexw";*/
+
+      if (narrowband)
+         op.packet = (unsigned char *)"speex narrowband";
+      if (wideband)
+         op.packet = (unsigned char *)"speex wideband**";
+      op.bytes = 16;
+      op.b_o_s = 1;
+      op.e_o_s = 0;
+      op.granulepos = 0;
+      op.packetno = 0;
+      ogg_stream_packetin(&os, &op);
+
+      while((result = ogg_stream_flush(&os, &og)))
+      {
+         if(!result) break;
+         ret = oe_write_page(&og, fout);
+         if(ret != og.header_len + og.body_len)
+         {
+            fprintf (stderr,"Failed writing header to output stream\n");
+            exit(1);
+         }
+         else
+            bytes_written += ret;
+      }
+      
+      
       if (fwrite(header, 1, 5, fout)!=5)
       {
          perror("Cannot write header");
       if (fwrite(header, 1, 5, fout)!=5)
       {
          perror("Cannot write header");
@@ -172,19 +223,64 @@ int main(int argc, char **argv)
 
    while (1)
    {
 
    while (1)
    {
+      id++;
       fread(in, sizeof(short), frame_size, fin);
       if (feof(fin))
          break;
       for (i=0;i<frame_size;i++)
          input[i]=in[i];
       encode(st, input, &bits);
       fread(in, sizeof(short), frame_size, fin);
       if (feof(fin))
          break;
       for (i=0;i<frame_size;i++)
          input[i]=in[i];
       encode(st, input, &bits);
-      nbBytes = speex_bits_write_whole_bytes(&bits, cbits, 200);
-      fwrite(cbits, 1, nbBytes, fout);
+
+      /*if (id%5!=0)
+        continue;*/
+      nbBytes = speex_bits_write(&bits, cbits, 500);
+      speex_bits_reset(&bits);
+      op.packet = (unsigned char *)cbits;
+      op.bytes = nbBytes;
+      op.b_o_s = 0;
+      op.e_o_s = 0;
+      op.granulepos = id;
+      op.packetno = id;
+      ogg_stream_packetin(&os, &op);
+
+      while (ogg_stream_pageout(&os,&og))
+      {
+         ret = oe_write_page(&og, fout);
+         if(ret != og.header_len + og.body_len)
+         {
+            fprintf (stderr,"Failed writing header to output stream\n");
+            exit(1);
+         }
+         else
+            bytes_written += ret;
+      }
+   }
+   
+   op.packet = (unsigned char *)"END OF STREAM";
+   op.bytes = 13;
+   op.b_o_s = 0;
+   op.e_o_s = 1;
+   op.granulepos = id+1;
+   op.packetno = id+1;
+   ogg_stream_packetin(&os, &op);
+   while (ogg_stream_flush(&os, &og))
+   {
+      ret = oe_write_page(&og, fout);
+      if(ret != og.header_len + og.body_len)
+      {
+         fprintf (stderr,"Failed writing header to output stream\n");
+         exit(1);
+      }
+      else
+         bytes_written += ret;
    }
    }
-   nbBytes = speex_bits_write(&bits, cbits, 200);
-   fwrite(cbits, 1, nbBytes, fout);
+   
 
    encoder_destroy(st);
 
    encoder_destroy(st);
+
+   ogg_stream_clear(&os);
+
    exit(0);
    return 1;
 }
    exit(0);
    return 1;
 }
+