OPUS解码原始PCM数据

时间:2017-03-17 07:18:20

标签: c++ decode encode pcm opus

我正在尝试使用OPUS压缩和解压缩原始PCM(16位)音频。 下面是我的opus_encoder.c代码。如果我删除了我的decoder.c,缓冲区工作正常,因为麦克风能够接收原始PCM数据。但是,一旦我实现了我的解码器类,它就给了我很多错误,比如内存分配,堆损坏等等。以下是我的一些错误:

  

std :: bad_alloc在内存位置0x0031D4BC

     

堆栈溢出(参数:0x00000000,0x05122000)

     

访问冲突读取位置0x04A40000。

根据我的理解,我认为我的解码器大小无法正确分配内存。你能看看我的代码,看看出了什么问题吗?

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;
    }



}

0 个答案:

没有答案