0

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:

std::bad_alloc at memory location 0x0031D4BC

Stack overflow (parameters: 0x00000000, 0x05122000)

Access violation reading location 0x04A40000.

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?

Opus_encoder.c

#include "opusencoder.h"
#include <QtConcurrent/QtConcurrent>

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& size) {


    if (size == 0)
        return false;

    enc = (OpusEncoder *)malloc(opus_encoder_get_size(1));

    enc = opus_encoder_create(8000, 1, OPUS_APPLICATION_VOIP, &error);
    if (enc == NULL)
    {
        exit;
    }

    opus_int32 rate;
    opus_encoder_ctl(enc, OPUS_GET_BANDWIDTH(&rate));
    this->encoded_data_size = rate;


    int len;



    for (int i = 0; i < size / 2; i++)
    {

        //combine pairs of bytes in the original data into two-byte number
        //convert const char to short

         pcm= audiodata[2 * i] << 8 | audiodata[(2 * i) + 1];


    }

    qDebug() << "audiodata: " << pcm << endl;

    compressedbuffer = new (unsigned char[this->encoded_data_size]);


    len = opus_encode(enc, &pcm, 320, compressedbuffer, this->encoded_data_size);

    len = opus_packet_unpad(compressedbuffer, len);
    len++;


    if (len < 0)
    {
        qDebug() << "Failure to compress";
        return NULL;

    }


    qDebug() << "COmpressed buffer:" << compressedbuffer << endl;

    qDebug() << "opus_encode() ................................ OK.\n" << endl;

}

Opus_decoder.c

##include "opusdecoder.h"
#include <QtConcurrent/QtConcurrent>
#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, &err); 

if (dec == NULL)
{
    exit;
}


opus_int32 rate;
opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&rate));
rate = decoded_data_size;

this->num_channels = num_channels;


int decodedatanotwo;
opus_int16 *decompress = new (opus_int16[frame_size * this->num_channels]);
opus_packet_get_nb_channels(data);




decodedatanotwo= opus_decode(dec, data, this->decoded_data_size, decompress, 320, 0);



if (decodedatanotwo < 0)
{
    qDebug() << "Failure to decompress";
    return NULL;


}


qDebug() << "opus_decode() ................................ OK.\n" << decodedatanotwo << endl;

if (decodedatanotwo != frame_size)
    {
        exit;
    }



}
omg99123
  • 1
  • 2
  • Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Mar 17 '17 at 07:25
  • 2
    Your encoder leaks memory with that malloc that you immediately clobber with `opus_encoder_create`, and the way you're using `OPUS_GET_BANDWIDTH` looks very very wrong... `OpusBandwidth` is an enum and its values don't make sense to be the size of anything. – hobbs Mar 17 '17 at 07:32
  • @hobbs I did opus_encoder_destroy(enc) and opus_decoder_destroy(dec) to free the state and also declared compressedbuffer and decompress to be zero after I call out the method in Receiving end and transmitting end class. However, the error is still there – omg99123 Mar 21 '17 at 02:56

0 Answers0