2016-06-14 79 views
1

我有一小段C++代码,它试图打开一个ogg/opus编码文件,并使用opus API来使用函数opus_decode()对其进行解码。问题是几乎opus_decode()的一半调用我为同样的声音返回负数(错误)代码.. -4和-2(无效的包和缓冲区太短),我无法解决。输出是像解码Ogg/Opus文件

Ñ解码:960Ñ解码:-4 N解码:-4 N解码:960Ñ解码: -4 N解码:1920Ñ解码:960Ñ解码:-4 N解码: -4

等等。

#include <string.h> 
#include <opus/opus.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <cstdio> 
#include <iostream> 
#include <fstream> 

#define LEN 1024 
#define FREQ 48000 
#define CHANNELS 1 
#define FRAMESIZE 1920 

int main(int argc, char *argv[]) { 

    int size = opus_decoder_get_size(CHANNELS); 

    OpusDecoder *decoders = (OpusDecoder*)malloc(size); 
    int error = opus_decoder_init(decoders, FREQ, CHANNELS); 

    std::ifstream inputfile; 
    inputfile.open("/home/vir/Descargas/detodos.opus"); //48000Hz, Mono 

    char input[LEN]; 

    opus_int16 *data = (opus_int16*)calloc(CHANNELS*FRAMESIZE,sizeof(opus_int16)); 


    if(inputfile.is_open()) 
     while (!inputfile.eof()) { 

      inputfile >> input;   

      std::cerr << "N decoded: " << opus_decode(decoders, (const unsigned char*)&input[0], LEN, data, FRAMESIZE, 0) << "\n"; 

     } 


    return error; 
} 

回答

3

看来您使用的是Opus-Tools而不是OpusFile。很明显,您已链接到libopus.a库,但您还需要下载并生成OpusFile 0.7,并将您的程序与从建立OpusFile创建的libopusfile.a链接起来。在OpusFile 0.7的程序中包含opusfile.h。最后,您需要下载并构建libogg库,以从xiph.org/downloads下载libogg 1.3.2并链接到该库。

This link是解释如何打开和关闭ogg opus流进行解码的文档。

确保您有一个OGG OPUS文件和使用打开流...

OggOpusFile *file = op_open_file(inputfile, error)(inputfile is char* inputfile and error is an int pointer) 

关闭与op_free(file)流。这是实际解码ogg opus流的function documentation。致电前op_free与...

op_read(file,buffer,bufferSize,null), buffer is opus_int16 pcm[120*48*2] 

bufferSizesizeof(pcm)/sizeof(*pcm)解码音频数据。 op_read将解码更多的文件,因此将它放在for循环中,直到op_read返回0