title: OPUS decode raw PCM data
body: <p>I am trying to compress and decompress raw PCM (16-Bit) audio, using OPUS.
  Here below is my code for opus_encoder.c. If I remove my decoder.c, the buffer works just fine as in the microphone is able to take in raw PCM data. However, once I have implemented my decoder class, it gave me a lot of errors such as memory allocation, heap corruption and so on. Here are some of my errors:</p>
  
  <blockquote>
    <p>std::bad_alloc at memory location 0x0031D4BC</p>
    
    <p>Stack overflow (parameters: 0x00000000, 0x05122000)</p>
    
    <p>Access violation reading location 0x04A40000.</p>
  </blockquote>
  
  <p>Based on my understanding, I think my decoder size cannot allocate the memory properly. Can you take a look at my codes and see what went wrong?</p>
  
  <h2>Opus_encoder.c</h2>
  
  <pre><code>#include "opusencoder.h"
  #include &lt;QtConcurrent/QtConcurrent&gt;
  
  opusencoder::opusencoder(){
  }
  
  opusencoder::~opusencoder(){
  }
  
  OpusEncoder *enc;
  int error;
  unsigned char *compressedbuffer;
  opus_uint32 enc_final_range;
  short pcm = 0;
  
  unsigned char *opusencoder::encodedata(const char *audiodata, const unsigned int&amp; size) {
  
  
      if (size == 0)
          return false;
  
      enc = (OpusEncoder *)malloc(opus_encoder_get_size(1));
  
      enc = opus_encoder_create(8000, 1, OPUS_APPLICATION_VOIP, &amp;error);
      if (enc == NULL)
      {
          exit;
      }
  
      opus_int32 rate;
      opus_encoder_ctl(enc, OPUS_GET_BANDWIDTH(&amp;rate));
      this-&gt;encoded_data_size = rate;
  
  
      int len;
  
  
  
      for (int i = 0; i &lt; size / 2; i++)
      {
  
          //combine pairs of bytes in the original data into two-byte number
          //convert const char to short
  
           pcm= audiodata[2 * i] &lt;&lt; 8 | audiodata[(2 * i) + 1];
  
  
      }
  
      qDebug() &lt;&lt; "audiodata: " &lt;&lt; pcm &lt;&lt; endl;
  
      compressedbuffer = new (unsigned char[this-&gt;encoded_data_size]);
  
  
      len = opus_encode(enc, &amp;pcm, 320, compressedbuffer, this-&gt;encoded_data_size);
  
      len = opus_packet_unpad(compressedbuffer, len);
      len++;
  
  
      if (len &lt; 0)
      {
          qDebug() &lt;&lt; "Failure to compress";
          return NULL;
  
      }
  
  
      qDebug() &lt;&lt; "COmpressed buffer:" &lt;&lt; compressedbuffer &lt;&lt; endl;
  
      qDebug() &lt;&lt; "opus_encode() ................................ OK.\n" &lt;&lt; endl;
  
  }
  </code></pre>
  
  <h2>Opus_decoder.c</h2>
  
  <pre><code>##include "opusdecoder.h"
  #include &lt;QtConcurrent/QtConcurrent&gt;
  #define OPUS_CLEAR(dst, n) (memset((dst), 0, (n)*sizeof(*(dst))))
  int num_channels = 1;
  
  opusdecoder::opusdecoder(){
  }
  opusdecoder::~opusdecoder(){
  }
  
  opus_int16* opusdecoder::decodedata(int frame_size, const unsigned char *data)
  {
   dec = opus_decoder_create(8000, 1, &amp;err); 
  
  if (dec == NULL)
  {
      exit;
  }
  
  
  opus_int32 rate;
  opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&amp;rate));
  rate = decoded_data_size;
  
  this-&gt;num_channels = num_channels;
  
  
  int decodedatanotwo;
  opus_int16 *decompress = new (opus_int16[frame_size * this-&gt;num_channels]);
  opus_packet_get_nb_channels(data);
  
  
  
  
  decodedatanotwo= opus_decode(dec, data, this-&gt;decoded_data_size, decompress, 320, 0);
  
  
  
  if (decodedatanotwo &lt; 0)
  {
      qDebug() &lt;&lt; "Failure to decompress";
      return NULL;
  
  
  }
  
  
  qDebug() &lt;&lt; "opus_decode() ................................ OK.\n" &lt;&lt; decodedatanotwo &lt;&lt; endl;
  
  if (decodedatanotwo != frame_size)
      {
          exit;
      }
  
  
  
  }
  </code></pre>