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

📄 dec_wbplus.c

📁 关于AMR-WB+语音压缩编码的实现代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "../lib_amr/enc_if.h"
#include "../lib_amr/dec_if.h"
#include "../include/amr_plus.h"
#include "../include/wbplus3gplib.h"

extern const UWord8 block_size[];



static void copyright(void)
{
   fprintf(stderr, "\n");
   
}
static void usage(char *argv)
{
   fprintf(stderr,
      "Usage: %s [-ff <3gp/raw> -fs <fs_Hz>][-mono] [-limiter] -if <infile.wb+> -of <outfile.wav> -fer <error.txt>\n",
      argv);
   fprintf(stderr, "\n");
   fprintf(stderr, "-ff     file format used (default is 3gp)\n");
   fprintf(stderr, "-fs     frequency sampling\n");
   fprintf(stderr, "-mono   mono output \n");
   fprintf(stderr, "-limiter    avoids output clipping (recommended)\n");
   fprintf(stderr, "-if     input AMRWB+ bitstream file\n");
   fprintf(stderr, "-of     output audio WAV file\n");
   fprintf(stderr, "-fer    frame erasures file (0/1)");
   fprintf(stderr, "\n");
}


static float get_bitrate(DecoderConfig * conf)
{
   if (conf->fscale != 0)
   {
      return (float) (get_nb_bits(conf->extension, conf->mode, conf->st_mode) * conf->fscale) / (80.0f *
         FSCALE_DENOM);
   }
   else
   {
      return (float) get_nb_bits(conf->extension, conf->mode, conf->st_mode) / 80.0f;
   }
}
static void parsecmdline(int argc,
                         char *argv[],
                         char **input_filename,
                         char **output_filename,
                         char **fer_filename,
                         DecoderConfig * conf)
{
   conf->fs = 0;
   conf->mono_dec_stereo = 0;
   conf->limiter_on = 0;
   conf->fer_sim = 0;
   conf->FileFormat = F3GP;
   if (argc < 5)
   {
      usage(*argv);
      exit(EXIT_FAILURE);
   }
   argc--;
   argv++;
   while (argc > 0)
   {
      if (!strcmp(*argv, "-fs"))
      {
         argv++;
         argc--;
         conf->fs = atoi(*argv);
      }
      else if (!strcmp(*argv, "-ff"))
      {
         argv++;
         argc--;
         if (!strcmp(*argv, "3gp"))
         {
            conf->FileFormat = F3GP;     
         }
         else
         {
            conf->FileFormat = FRAW;     
         }

      }

      else if (!strcmp(*argv, "-mono"))
      {
         conf->mono_dec_stereo = 1;
      }
      else if (!strcmp(*argv, "-limiter"))
      {
         conf->limiter_on = 1;
      }
      else if (!strcmp(*argv, "-if"))
      {
         argv++;
         argc--;
         *input_filename = *argv;
      }
      else if (!strcmp(*argv, "-of"))
      {
         argv++;
         argc--;
         *output_filename = *argv;
      }
      else if (!strcmp(*argv, "-fer"))
      {
         argv++;
         argc--;
         *fer_filename = *argv;
         conf->fer_sim=1;
      }
      else
      {
         fprintf(stderr, "Unknown option %s\n", *argv);
         exit(EXIT_FAILURE);
      }
      argv++;
      argc--;
   }

   
}

static void select_fs(DecoderConfig * conf)
{
   /* default sampling rate if undefined */
   if (conf->fs == 0)
   {
      if (conf->extension == 0)
         conf->fs = 16000;
      else if (conf->fscale == 0)
         conf->fs = 24000;
      else
         conf->fs = 48000;
   }
   /* user specified sampling rate */
   else
   {
      if (conf->extension == 0)
      {
         if (conf->fs != 16000)
         {
            fprintf(stderr, "Sampling rate not supported");
            exit(EXIT_FAILURE);
         }
      }
      else
      {
         if (conf->fscale == 0)
         {
            if ((conf->fs != 16000) && (conf->fs != 24000)
               && (conf->fs != 8000))
            {
               fprintf(stderr, "Sampling rate not supported");
               exit(EXIT_FAILURE);
            }
         }
         else
         {
            if ((conf->fs!=44100) && (conf->fs!=48000)) 
            {
               fprintf(stderr, "Sampling rate not supported");
               exit(EXIT_FAILURE);
            }
         }
      }
   }
}
static void set_frame_length(DecoderConfig * conf, int *L_frame)
{
   switch (conf->fs)
   {
   case 8000:
      *L_frame = L_FRAME8k;
      break;
   case 16000:
      *L_frame = L_FRAME16kPLUS;
      break;
   case 24000:
      *L_frame = L_FRAME24k;
      break;
   case 32000:
      *L_frame = L_FRAME32k;
      break;
#ifdef FILTER_44kHz
    case 44100:
      *L_frame = L_FRAME44k;
      break;
#endif
#ifdef FILTER_48kHz
   case 48000:
      *L_frame = L_FRAME48k;
      break;
#endif
   default:
      fprintf(stderr, "Sampling rate not supported");
      exit(1);
   }
}
static void interleave(float *right, float *left, float *out, int length)
{
   int i;
   
   for (i = length - 1; i >= 0; i--)
   {
      out[(i * 2) + 1] = right[i];
      out[i * 2] = left[i];
   }
}
static void simple_frame_limiter(float *signal, float *gain, int lg)
{
   int i;
   int pos_start, pos_end;
   float amp, slope;
   float max_amp;
   float target_gain;
   
   max_amp = -1;
   pos_start = -1;
   for (i = 0; i < lg; i++)
   {
      amp = (float)fabs(signal[i]);
      if (amp > 32767.0f)
      {
         if (pos_start < 0)
            pos_start = i;
         pos_end = i;
         if (amp > max_amp)
         {
            max_amp = amp;
         }
      }
   }
   if (max_amp > 0)
   {
      target_gain = 32767.0f / max_amp;
      slope = (target_gain - *gain) / ((float) pos_start + 1.0f);
      for (i = 0; i < lg; i++)
      {
         if (i < pos_start + 1)
            *gain += slope;
         signal[i] *= *gain;
      }
   }
   else
   {
      if (*gain != 1.0)
      {
         slope = (1.0f - *gain) / ((float) lg);
         for (i = 0; i < lg; i++)
         {
            *gain += slope;
            signal[i] *= *gain;
         }
      }
   }
}


void main(int argc, char *argv[])
{
   FILE *f_serial;              /* File of serial bits for transmission  */
   FILE *f_syn;                 /* File of synthesis speech              */
   FILE *f_fer;                  /* File of frame errasures              */

   short speech16[L_FRAME48k * 2];
   Decoder_State_Plus *st_d;
   void *stAmrwbDec;
   unsigned short serial[NBITS_MAX];     /* serial parameters.                  */
   
   short bitsPerSample = 16;
   int bfi[4] = { 0, 0, 0, 0 };
   int  L_frame;
   long frame, dataSize;
   float channel_right[4 * L_FRAME48k];
   float channel_left[2 * L_FRAME48k];
   float mem_down_right[2*L_FILT_DECIM_FS], mem_down_left[2*L_FILT_DECIM_FS];
   int frac_down_right, frac_down_left;
   int fac_up, fac_down, nb_samp_fs;
   long  fs_output;
   short nb_samp;
   short mode, extension;
   short st_mode;
   short num_channels;
   char *input_filename = "";
   char *output_filename = "";
   char *fer_filename= "";
   short fst_switched;
   short old_st_mode;

⌨️ 快捷键说明

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