2016-06-14 77 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