编解码器:Opus编解码器的接口及使用
1.源碼下載及編譯
編解碼器版本采用opus-1.3-beta,opus官網:https://www.opus-codec.org/
下載完成后解壓源碼,在VS2015上打開源碼 ?opus-1.3-beta\win32\VS2015\opus.sln,共有5個項目,其中opus項目是opus編解碼器的主要實現以及對外部提供接口,實際寫程序的時候一定是基于該項目中接口的,opus_demo是基于opus項目寫的一個命令行程序,可以通過命令行調用opus項目的編解碼功能,操作實際的音頻數據。另外三個是基于opus項目的測試程序。分別測試編碼功能、解碼功能以及API接口
右鍵點擊opus_demo,設為啟動項,并生成解決方案。生成之后就可以關閉VS了(因為主要在命令行下進行編解碼操作)。
進入 opus-1.3-beta\win32\VS2015\Win32\Debug,里面有一個opus_demo.exe,在當前文件夾下SHIFT+右鍵選擇在此處打開命令窗口,在命令臺下使用源碼目錄README文檔中的指令就可以操作。
Usage: opus_demo [-e] <application> <sampling rate (Hz)> <channels (1/2)><bits per second> [options] <input> <output>opus_demo -d <sampling rate (Hz)> <channels (1/2)> [options]<input> <output>mode: voip | audio | restricted-lowdelay options:-e : only runs the encoder (output the bit-stream)-d : only runs the decoder (reads the bit-stream as input)-cbr : enable constant bitrate; default: variable bitrate-cvbr : enable constrained variable bitrate; default:unconstrained-bandwidth <NB|MB|WB|SWB|FB>: audio bandwidth (from narrowband to fullband);default: sampling rate-framesize <2.5|5|10|20|40|60>: frame size in ms; default: 20-max_payload <bytes>: maximum payload size in bytes, default: 1024-complexity <comp>: complexity, 0 (lowest) ... 10 (highest); default: 10-inbandfec : enable SILK inband FEC-forcemono : force mono encoding, even for stereo input-dtx : enable SILK DTX-loss <perc> : simulate packet loss, in percent (0-100); default: 0input and output are little-endian signed 16-bit PCM files or opus bitstreams with simple opus_demo proprietary framing.?舉例:
opus_demo -e voip 48000 1 16000 -framesize 10 C:\Users\tengxc\Desktop\zhou.pcm c:\Users\tengxc\Desktop\zhou.opus2.opus編碼器接口
opus編解碼器接口的定義主要在opus工程源碼中的opus.h文件中。以C語言的方式提供。
編碼器接口中有一個重要的結構體變量 struct?OpusEncoder,它表示編碼器狀態,結構體中包含很多變量用來統計opus編碼器的狀態,使用時用先?typedef?struct?OpusEncoder?OpusEncoder;
創建編碼器:
opus接口中創建編碼器的方式有兩種,因此與之對應的釋放編碼器的方式也有兩種,創建編碼器時可以選用
OpusEncoder* enc;
OpusEncoder? *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error);
舉例:enc =?opus_encoder_create(16000, 1,??OPUS_APPLICATION_VOIP, &err_num);
參數:采樣率、通道數、應用類型、錯誤碼
或者
OpusEncoder *enc;
size = opus_encoder_get_size(channels);
enc = malloc(size);
err_num = opus_encoder_init(enc, Fs, channels, application);
參數:同上
兩種創建編碼器的方式差異在于表示編碼器狀態的結構體內存申請方式不一樣,第一種內存的申請方式為opus_encoder_create函數申請內存后返回一個首地址指針,第二種是自己申請一塊內存后用opus_encoder_init函數填充。
編碼音頻數據:
編碼音頻數據也分為兩種方式,一種是float型,一種是int型。
opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int frame_size, unsigned char *data, opus_int32 max_data_bytes);
舉例:opus_int32?len =?opus_encode(enc, pcm, 960, out, 4000);
參數:解碼器狀態指針、音頻原始數據、以樣本個數為單位的幀長度、編碼完成的數據、最大的編碼后數據長度
或者
opus_int32 opus_encode_float(OpusEncoder *st,?const float *pcm, int frame_size, unsigned char *data, opus_int32 max_data_bytes);
舉例:opus_int32?len =?opus_encode(enc, pcm, 960, out, 4000);
參數:同上
在這兩種編碼方式中,浮點型編碼是更加損耗CPU,但是因此獲得的編碼精度更高,所以音質更加精準,而16位in整型編碼是更多機器支持的,編碼精度稍低,對CPU的損耗也小一點。
釋放編碼器:
當采用opus_encoder_create創建編碼器時,需要用opus_encoder_destroy釋放。
opus_encoder_destroy(OpusEncoder *st);
舉例:opus_encoder_destroy(enc);
參數:編碼器狀態結構指針
當采用opus_encoder_init創建編碼器時,需要用free釋放。
舉例:free(enc);? ? ? ? //由于前面是使用malloc申請的內存
參數:編碼器狀態結構指針
修改編碼參數:
opus支持編碼器運行時的參數修改。
int opus_encoder_ctl(OpusEncoder *st, int request, ...);
舉例:opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate));
參數:編碼器狀態結構指針、設置的參數
總結
以上是生活随笔為你收集整理的编解码器:Opus编解码器的接口及使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cly的三角形 (思维+斐波那契)
- 下一篇: markdown快速插入图片技巧