天天看點

一起玩轉玩轉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)。單通道的幀大小 = 采樣率 * 幀時間(機關為秒)。

其他詳情參考音頻編碼:https://gitee.com/linktarget=https%3A%2F%2Fbaike.baidu.com%2Fitem%2F%25E9%259F%25B3%25E9%25A2%2591%25E7%25BC%2596%25E7%25A0%2581%2F1729208%3Ffr%3Daladdin。

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。關于元件下載下傳的詳細流程請參考:https://gitee.com/LiteOS/LiteOS_Components#%E5%9C%A8%E7%BA%BF%E7%BB%84%E4%BB%B6%E4%B8%8B%E8%BD%BD%E6%B5%81%E7%A8%8B

注:要成功下載下傳相關資源,需要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。

其他開發闆操作請參考快速入門:https://gitee.com/LiteOS/LiteOS/blob/master/doc/LiteOS_Quick_Start.md。

執行結果如下:

********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”到自己賬号下,如下圖。

一起玩轉玩轉LiteOS元件:Opus

點選關注,第一時間了解華為雲新鮮技術~

繼續閱讀