⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 speexenc.c

📁 一个开源的sip源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2002-2006 Jean-Marc Valin    File: speexenc.c   Redistribution and use in source and binary forms, with or without   modification, are permitted provided that the following conditions   are met:      - Redistributions of source code must retain the above copyright   notice, this list of conditions and the following disclaimer.      - Redistributions in binary form must reproduce the above copyright   notice, this list of conditions and the following disclaimer in the   documentation and/or other materials provided with the distribution.      - Neither the name of the Xiph.org Foundation nor the names of its   contributors may be used to endorse or promote products derived from   this software without specific prior written permission.      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS   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>#include <getopt.h>#endif#ifndef HAVE_GETOPT_LONG#include "getopt_win.h"#endif#include <stdlib.h>#include <string.h>#include <time.h>#include <speex/speex.h>#include <ogg/ogg.h>#include "wav_io.h"#include <speex/speex_header.h>#include <speex/speex_stereo.h>#include <speex/speex_preprocess.h>#if defined WIN32 || defined _WIN32#include "getopt_win.h"/* We need the following two to set stdout to binary */#include <io.h>#include <fcntl.h>#endif#include "skeleton.h"void comment_init(char **comments, int* length, char *vendor_string);void comment_add(char **comments, int* length, char *tag, char *val);/*Write an Ogg page to a file pointer*/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 2000/* Convert input audio bits, endians and channels */static int read_samples(FILE *fin,int frame_size, int bits, int channels, int lsb, short * input, char *buff, spx_int32_t *size){      unsigned char in[MAX_FRAME_BYTES*2];   int i;   short *s;   int nb_read;   if (size && *size<=0)   {      return 0;   }   /*Read input audio*/   if (size)      *size -= bits/8*channels*frame_size;   if (buff)   {      for (i=0;i<12;i++)         in[i]=buff[i];      nb_read = fread(in+12,1,bits/8*channels*frame_size-12, fin) + 12;      if (size)         *size += 12;   } else {      nb_read = fread(in,1,bits/8*channels* frame_size, fin);   }   nb_read /= bits/8*channels;   /*fprintf (stderr, "%d\n", nb_read);*/   if (nb_read==0)      return 0;   s=(short*)in;   if(bits==8)   {      /* Convert 8->16 bits */      for(i=frame_size*channels-1;i>=0;i--)      {         s[i]=(in[i]<<8)^0x8000;      }   } else   {      /* convert to our endian format */      for(i=0;i<frame_size*channels;i++)      {         if(lsb)             s[i]=le_short(s[i]);          else            s[i]=be_short(s[i]);      }   }   /* FIXME: This is probably redundent now */   /* copy to float input buffer */   for (i=0;i<frame_size*channels;i++)   {      input[i]=(short)s[i];   }   for (i=nb_read*channels;i<frame_size*channels;i++)   {      input[i]=0;   }   return nb_read;}void add_fishead_packet (ogg_stream_state *os) {   fishead_packet fp;   memset(&fp, 0, sizeof(fp));   fp.ptime_n = 0;   fp.ptime_d = 1000;   fp.btime_n = 0;   fp.btime_d = 1000;   add_fishead_to_stream(os, &fp);}/* * Adds the fishead packets in the skeleton output stream along with the e_o_s packet */void add_fisbone_packet (ogg_stream_state *os, spx_int32_t serialno, SpeexHeader *header) {   fisbone_packet fp;   memset(&fp, 0, sizeof(fp));   fp.serial_no = serialno;   fp.nr_header_packet = 2 + header->extra_headers;   fp.granule_rate_n = header->rate;   fp.granule_rate_d = 1;   fp.start_granule = 0;   fp.preroll = 3;   fp.granule_shift = 0;   add_message_header_field(&fp, "Content-Type", "audio/x-speex");   add_fisbone_to_stream(os, &fp);}void version(){   printf ("speexenc (Speex encoder) version " SPEEX_VERSION " (compiled " __DATE__ ")\n");   printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n");}void version_short(){   printf ("speexenc version " SPEEX_VERSION "\n");   printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n");}void usage(){   printf ("Usage: speexenc [options] input_file output_file\n");   printf ("\n");   printf ("Encodes input_file using Speex. It can read the WAV or raw files.\n");   printf ("\n");   printf ("input_file can be:\n");   printf ("  filename.wav      wav file\n");   printf ("  filename.*        Raw PCM file (any extension other than .wav)\n");   printf ("  -                 stdin\n");   printf ("\n");     printf ("output_file can be:\n");   printf ("  filename.spx      Speex file\n");   printf ("  -                 stdout\n");   printf ("\n");     printf ("Options:\n");   printf (" -n, --narrowband   Narrowband (8 kHz) input file\n");    printf (" -w, --wideband     Wideband (16 kHz) input file\n");    printf (" -u, --ultra-wideband \"Ultra-wideband\" (32 kHz) input file\n");    printf (" --quality n        Encoding quality (0-10), default 8\n");    printf (" --bitrate n        Encoding bit-rate (use bit-rate n or lower)\n");    printf (" --vbr              Enable variable bit-rate (VBR)\n");    printf (" --vbr-max-bitrate  Set max VBR bit-rate allowed\n");    printf (" --abr rate         Enable average bit-rate (ABR) at rate bps\n");    printf (" --vad              Enable voice activity detection (VAD)\n");    printf (" --dtx              Enable file-based discontinuous transmission (DTX)\n");    printf (" --comp n           Set encoding complexity (0-10), default 3\n");    printf (" --nframes n        Number of frames per Ogg packet (1-10), default 1\n");    printf (" --denoise          Denoise the input before encoding\n");    printf (" --agc              Apply adaptive gain control (AGC) before encoding\n");    printf (" --skeleton         Outputs ogg skeleton metadata (may cause incompatibilities)\n");   printf (" --comment          Add the given string as an extra comment. This may be\n");   printf ("                     used multiple times\n");   printf (" --author           Author of this track\n");   printf (" --title            Title for this track\n");   printf (" -h, --help         This help\n");    printf (" -v, --version      Version information\n");    printf (" -V                 Verbose mode (show bit-rate)\n");    printf ("Raw input options:\n");   printf (" --rate n           Sampling rate for raw input\n");    printf (" --stereo           Consider raw input as stereo\n");    printf (" --le               Raw input is little-endian\n");    printf (" --be               Raw input is big-endian\n");    printf (" --8bit             Raw input is 8-bit unsigned\n");    printf (" --16bit            Raw input is 16-bit signed\n");    printf ("Default raw PCM input is 16-bit, little-endian, mono\n");    printf ("\n");   printf ("More information is available from the Speex site: http://www.speex.org\n");   printf ("\n");   printf ("Please report bugs to the mailing list `speex-dev@xiph.org'.\n");}int main(int argc, char **argv){   int nb_samples, total_samples=0, nb_encoded;   int c;   int option_index = 0;   char *inFile, *outFile;   FILE *fin, *fout;   short input[MAX_FRAME_SIZE];   spx_int32_t frame_size;   int quiet=0;   spx_int32_t vbr_enabled=0;   spx_int32_t vbr_max=0;   int abr_enabled=0;   spx_int32_t vad_enabled=0;   spx_int32_t dtx_enabled=0;   int nbBytes;   const SpeexMode *mode=NULL;   int modeID = -1;   void *st;   SpeexBits bits;   char cbits[MAX_FRAME_BYTES];   int with_skeleton = 0;   struct option long_options[] =   {      {"wideband", no_argument, NULL, 0},      {"ultra-wideband", no_argument, NULL, 0},      {"narrowband", no_argument, NULL, 0},      {"vbr", no_argument, NULL, 0},      {"vbr-max-bitrate", required_argument, NULL, 0},      {"abr", required_argument, NULL, 0},      {"vad", no_argument, NULL, 0},      {"dtx", no_argument, NULL, 0},      {"quality", required_argument, NULL, 0},      {"bitrate", required_argument, NULL, 0},      {"nframes", required_argument, NULL, 0},      {"comp", required_argument, NULL, 0},      {"denoise", no_argument, NULL, 0},      {"agc", no_argument, NULL, 0},      {"skeleton",no_argument,NULL, 0},      {"help", no_argument, NULL, 0},      {"quiet", no_argument, NULL, 0},      {"le", no_argument, NULL, 0},      {"be", no_argument, NULL, 0},      {"8bit", no_argument, NULL, 0},      {"16bit", no_argument, NULL, 0},      {"stereo", no_argument, NULL, 0},      {"rate", required_argument, NULL, 0},      {"version", no_argument, NULL, 0},      {"version-short", no_argument, NULL, 0},      {"comment", required_argument, NULL, 0},      {"author", required_argument, NULL, 0},      {"title", required_argument, NULL, 0},      {0, 0, 0, 0}   };   int print_bitrate=0;   spx_int32_t rate=0;   spx_int32_t size;   int chan=1;   int fmt=16;   spx_int32_t quality=-1;   float vbr_quality=-1;   int lsb=1;   ogg_stream_state os;   ogg_stream_state so; /* ogg stream for skeleton bitstream */   ogg_page 		 og;   ogg_packet 		 op;   int bytes_written=0, ret, result;   int id=-1;   SpeexHeader header;   int nframes=1;   spx_int32_t complexity=3;   char *vendor_string = "Encoded with Speex " SPEEX_VERSION;   char *comments;   int comments_length;   int close_in=0, close_out=0;   int eos=0;   spx_int32_t bitrate=0;   double cumul_bits=0, enc_frames=0;   char first_bytes[12];   int wave_input=0;   spx_int32_t tmp;   SpeexPreprocessState *preprocess = NULL;   int denoise_enabled=0, agc_enabled=0;   spx_int32_t lookahead = 0;      comment_init(&comments, &comments_length, vendor_string);   /*Process command-line options*/   while(1)   {      c = getopt_long (argc, argv, "nwuhvV",                       long_options, &option_index);      if (c==-1)         break;            switch(c)      {      case 0:         if (strcmp(long_options[option_index].name,"narrowband")==0)         {            modeID = SPEEX_MODEID_NB;         } else if (strcmp(long_options[option_index].name,"wideband")==0)         {            modeID = SPEEX_MODEID_WB;         } else if (strcmp(long_options[option_index].name,"ultra-wideband")==0)         {            modeID = SPEEX_MODEID_UWB;         } else if (strcmp(long_options[option_index].name,"vbr")==0)         {            vbr_enabled=1;         } else if (strcmp(long_options[option_index].name,"vbr-max-bitrate")==0)         {            vbr_max=atoi(optarg);            if (vbr_max<1)            {               fprintf (stderr, "Invalid VBR max bit-rate value: %d\n", vbr_max);               exit(1);            }         } else if (strcmp(long_options[option_index].name,"abr")==0)         {            abr_enabled=atoi(optarg);            if (!abr_enabled)            {               fprintf (stderr, "Invalid ABR value: %d\n", abr_enabled);               exit(1);            }         } else if (strcmp(long_options[option_index].name,"vad")==0)         {            vad_enabled=1;         } else if (strcmp(long_options[option_index].name,"dtx")==0)         {            dtx_enabled=1;         } else if (strcmp(long_options[option_index].name,"quality")==0)         {            quality = atoi (optarg);            vbr_quality=atof(optarg);         } else if (strcmp(long_options[option_index].name,"bitrate")==0)         {            bitrate = atoi (optarg);         } else if (strcmp(long_options[option_index].name,"nframes")==0)         {            nframes = atoi (optarg);            if (nframes<1)               nframes=1;            if (nframes>10)               nframes=10;         } else if (strcmp(long_options[option_index].name,"comp")==0)         {            complexity = atoi (optarg);         } else if (strcmp(long_options[option_index].name,"denoise")==0)         {            denoise_enabled=1;         } else if (strcmp(long_options[option_index].name,"agc")==0)         {            agc_enabled=1;         } else if (strcmp(long_options[option_index].name,"skeleton")==0)         {            with_skeleton=1;         } else if (strcmp(long_options[option_index].name,"help")==0)         {            usage();            exit(0);         } else if (strcmp(long_options[option_index].name,"quiet")==0)         {            quiet = 1;         } else if (strcmp(long_options[option_index].name,"version")==0)         {            version();            exit(0);         } else if (strcmp(long_options[option_index].name,"version-short")==0)         {            version_short();            exit(0);         } else if (strcmp(long_options[option_index].name,"le")==0)         {            lsb=1;         } else if (strcmp(long_options[option_index].name,"be")==0)         {            lsb=0;         } else if (strcmp(long_options[option_index].name,"8bit")==0)         {            fmt=8;         } else if (strcmp(long_options[option_index].name,"16bit")==0)         {            fmt=16;         } else if (strcmp(long_options[option_index].name,"stereo")==0)         {            chan=2;         } else if (strcmp(long_options[option_index].name,"rate")==0)         {            rate=atoi (optarg);         } else if (strcmp(long_options[option_index].name,"comment")==0)         {	   if (!strchr(optarg, '='))	   {	     fprintf (stderr, "Invalid comment: %s\n", optarg);	     fprintf (stderr, "Comments must be of the form name=value\n");	     exit(1);	   }           comment_add(&comments, &comments_length, NULL, optarg);          } else if (strcmp(long_options[option_index].name,"author")==0)         {           comment_add(&comments, &comments_length, "author=", optarg);          } else if (strcmp(long_options[option_index].name,"title")==0)         {           comment_add(&comments, &comments_length, "title=", optarg);          }         break;      case 'n':         modeID = SPEEX_MODEID_NB;         break;      case 'h':         usage();         exit(0);         break;      case 'v':         version();         exit(0);         break;      case 'V':         print_bitrate=1;         break;      case 'w':         modeID = SPEEX_MODEID_WB;         break;      case 'u':         modeID = SPEEX_MODEID_UWB;         break;      case '?':         usage();         exit(1);         break;      }   }   if (argc-optind!=2)   {      usage();      exit(1);   }   inFile=argv[optind];   outFile=argv[optind+1];   /*Initialize Ogg stream struct*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -