一起玩轉玩轉LiteOS組件:Opus

摘要:Opus編碼器是一個開源的有損聲音編碼格式,適用於網絡實時聲音傳輸,標準格式爲RFC 6716,相對於其他編碼格式來說,保真性更好。

本文分享自華爲雲社區《LiteOS組件嚐鮮—玩轉Opus》,作者: Lionlace。

Opus編碼器是一個開源的有損聲音編碼格式,適用於網絡實時聲音傳輸,標準格式爲RFC 6716,相對於其他編碼格式來說,保真性更好。

Opus接口介紹

本文檔介紹一些Opus的常用接口,更多詳細接口介紹請參考components/media/opus/opus-1.3.1/include/opus.h文件。

Opus解碼的過程是將opus格式轉換爲pcm格式,編碼的過程是將pcm格式轉換爲opus格式。

解碼

創建解碼器

OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusDecoder *opus_decoder_create(
opus_int32 Fs,
int channels,
int *error
);

解碼器配置

OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...)
OPUS_ARG_NONNULL(1);

解碼

OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode(
OpusDecoder *st,
const unsigned char *data,
opus_int32 len,
opus_int16 *pcm,
int frame_size,
int decode_fec
) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);

刪除解碼器

OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st);

編碼

創建編碼器

OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusEncoder *opus_encoder_create(
opus_int32 Fs,
int channels,
int application,
int *error
);

編碼器配置

OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...)
OPUS_ARG_NONNULL(1);

編碼

OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode(
OpusEncoder *st,
const opus_int16 *pcm,
int frame_size,
unsigned char *data,
opus_int32 max_data_bytes
) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);

刪除編碼器

OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st);

Opus編解碼中常用參數

sampling rate採樣率,在opus中只能取值爲8000,12000,16000,24000,48000,單位爲赫茲(HZ)。

channel聲道,channel = 1爲單聲道,channel = 2爲立體聲。

frame size幀大小,在opus中每幀的時間取值爲2.5,5,10,20,40,60,80,100,120。單位毫秒(ms)。單通道的幀大小 = 採樣率 * 幀時間(單位爲秒)。

其他詳情參考音頻編碼:

Opus Demo解析

LiteOS社區提供了OpusDemoTask示例程序來演示如何使用opus,該Demo可以運行在LiteOS所支持的多塊開發板上。本Demo以解碼爲例,其中inputBuf爲採用opus格式編碼的一段數據,對該數據進行解碼處理,並將解碼後數據以pcm格式保存到outputBuf。

使能Opus Demo

在LiteOS源碼根目錄下根據實際使用的開發板,拷貝tools/build/config/目錄下的默認配置文件${platform}.config到根目錄,並重命名爲.config。

繼續在LiteOS源碼根目錄下執行make menuconfig命令,按如下菜單路徑使能Opus Demo。
Demos --->
Media Demo --->
[*] Enable Opus Demo

使能Opus Demo後會自動使能Opus組件。

保存退出後,LiteOS會從github上自動下載opus源代碼,並從gitee上下載適配於LiteOS系統的patch包,並打入patch。關於組件下載的詳細流程請參考:

注:要成功下載相關資源,需要Linux主機可訪問互聯網,並安裝git工具。

編譯運行Opus Demo

使能Opus Demo後,在LiteOS源碼根目錄下執行make clean; make -j命令編譯LiteOS工程,編譯成功後會在out/{platform}/lib路徑下生成libopus.a和libopus_demo.a文件,系統鏡像文件爲Huawei_LiteOS.bin。

以realview-pbx-a9爲例,運行OpusDemoTask。

其他開發板操作請參考快速入門:

執行結果如下:

********Hello Huawei LiteOS********
LiteOS Kernel Version : 5.0.0
Processor : Cortex-A9 * 4
Run Mode : SMP
GIC Rev : GICv1
build time : Dec 25 2021 22:52:50
**********************************
main core booting up...
osAppInit
releasing 3 secondary cores
cpu 0 entering scheduler
cpu 2 entering scheduler
cpu 3 entering scheduler
cpu 1 entering scheduler
app init!
Hello, welcome to liteos demo!
Opus demo task start to run.
Opus demo input data:
0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0xe0
Opus demo output data:
0x0000 0xfffd 0xfff4 0xffda 0xffaa 0xff88 0xff55 0xff03 0xff14 0xff83 0xffb4 0x001f 0x0070 0x01bb 0x024b 0x0274 0x02be 0x032a 0x0299 0x02a8 0x039c 0x05ef 0x0605 0x04fa 0x03d9 0x02b6 0x01d1 0x0113 0x0079 0xffd8 0x001d 0x000d 0xffdf 0xffbb 0xffac 0xffa0 0xff64 0xffc4 0xfff5 0xffee 0xffeb 0x006c 0x005e 0x0044 0x0045 0x02dc 0x0472 0x00db 0xff85 0x0347 0xfdc4 0xf957 0xf7c1 0xf62b 0xf3aa 0xf817 0xfb18 0xfcee 0x01b1 0x079e 0x059d 0x0aa0 0x0608 0x01c6 0xfdaf 0xfdda 0xfa18 0xf6d6 0xf701 0xfc43 0x0186 0x0387 0x0975 0xbb78 0xd771 0xd143 0xbc64 0xbcb9 0xe7f9 0xfd99 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
Opus demo decode seccussfully.
Opus demo task finished.
Huawei LiteOS #

結語

未來我們還會持續新增更多組件、開發板、架構、特性等。

感謝您的閱讀,有任何問題、建議,都可以留言給我們,讓我們一起進步:https://gitee.com/LiteOS/LiteOS/issues

爲了更容易找到“LiteOS”代碼倉,建議訪問https://gitee.com/LiteOS/LiteOS,關注“ Watch”、點贊“Star”、並“Fork”到自己賬號下,如下圖。

 

點擊關注,第一時間瞭解華爲雲新鮮技術~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章