speexenc and speexdec now use the Ogg bitstream. This is very preliminary
[speexdsp.git] / src / speexenc.c
index ea79a83..670fb4e 100644 (file)
 #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
@@ -66,6 +76,15 @@ int main(int argc, char **argv)
       {"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)
    {
@@ -118,6 +137,12 @@ int main(int argc, char **argv)
    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");
@@ -158,10 +183,36 @@ int main(int argc, char **argv)
    /*Temporary header*/
    {
       char *header="";
-      if (narrowband)
+      /*if (narrowband)
          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");
@@ -172,19 +223,64 @@ int main(int argc, char **argv)
 
    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);
-      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);
+
+   ogg_stream_clear(&os);
+
    exit(0);
    return 1;
 }
+