|
本帖最后由 迷雾向前 于 2017-1-20 11:22 编辑 2 O2 H: t$ M8 c1 b0 E0 C6 e3 R 4 E1 D; q0 f" M+ h2 V" Z6 J, L 计划在机器上STF103VE 跑opus codec 进行编解码,中途停了,写一下做个记录。 s& k0 f6 i; s* Z" [ 首先用CUBE 快速生成代码。 Clock 配置如下,我用的是内部时钟(应该没连接外部晶振):/ j7 W4 \& i6 w2 O2 _( L clock é ç½®
Enable FreeRTOS ,heap 配置为 20K,enc 结构需要13k Ram。 FreeRTOS config
用于编解码的Task 的Stack 改为1024Word,即4K(小了会reset) Task Stack
2 d6 g, v7 J% o- A 系统heap 需要改大,$ f+ h) e. [6 j4 N( N0 [ System Heap
将OPUS 代码copy 到工程里面,将如下文件加入到编译(没权限添加更多图片,只能用文字描述了) opus-1.1.3/silk/ana_filt_bank_1.c K8 i" K, }1 S% [ opus-1.1.3/silk/bwexpander.c! g0 l: F, z* t L$ b* @% V3 i# r opus-1.1.3/silk/control_audio_bandwidth.c+ H0 v4 @, ?! ~4 q7 I- Y opus-1.1.3/silk/code_signs.c opus-1.1.3/silk/CNG.c opus-1.1.3/silk/fixed/schur64_FIX.c! r' l, K( \. ] opus-1.1.3/silk/control_codec.c7 _. i% P/ T+ b2 ^2 D! p opus-1.1.3/silk/debug.c0 G% K( u( R0 o+ N4 F opus-1.1.3/silk/control_SNR.c opus-1.1.3/silk/fixed/residual_energy16_FIX.c opus-1.1.3/silk/fixed/residual_energy_FIX.c opus-1.1.3/silk/fixed/warped_autocorrelation_FIX.c opus-1.1.3/silk/biquad_alt.c opus-1.1.3/silk/bwexpander_32.c opus-1.1.3/silk/check_control_input.c" W5 Q1 r, S0 t% X' m( y { opus-1.1.3/silk/A2NLSF.c opus-1.1.3/silk/fixed/schur_FIX.c& y0 t- i% k( X4 q# f* \0 j# }0 ] opus-1.1.3/silk/fixed/solve_LS_FIX.c opus-1.1.3/silk/fixed/vector_ops_FIX.c opus-1.1.3/silk/encode_pulses.c+ `+ S! d2 Y3 k# a opus-1.1.3/silk/log2lin.c5 u1 V3 w# k0 `; z6 m& J opus-1.1.3/silk/enc_API.c0 g9 Y6 D f4 d" \4 B/ G opus-1.1.3/silk/HP_variable_cutoff.c" S- c% Y m: R* W3 P opus-1.1.3/silk/init_decoder.c2 M# d) Z, s% F, E! Q opus-1.1.3/silk/init_encoder.c8 `8 K; P8 v7 E& t" G: S4 F opus-1.1.3/silk/decode_indices.c opus-1.1.3/silk/inner_prod_aligned.c opus-1.1.3/silk/decode_frame.c; Q; Q1 K" h3 R opus-1.1.3/silk/decode_pulses.c opus-1.1.3/silk/gain_quant.c opus-1.1.3/silk/interpolate.c opus-1.1.3/silk/lin2log.c I; @% p4 j1 P! B opus-1.1.3/silk/decode_parameters.c5 k* N2 ~! G+ A opus-1.1.3/silk/dec_API.c; g( M8 s, o: h9 q+ q opus-1.1.3/silk/decode_core.c opus-1.1.3/silk/decode_pitch.c8 a$ G( l( y1 Q0 f6 g: Q1 d opus-1.1.3/silk/decoder_set_fs.c9 I, V* ~# k( c8 p opus-1.1.3/silk/encode_indices.c opus-1.1.3/silk/quant_LTP_gains.c opus-1.1.3/silk/NSQ.c8 \7 T* {7 }% N' X. R2 ?0 D opus-1.1.3/silk/resampler.c opus-1.1.3/silk/NLSF_del_dec_quant.c" S8 ?( x5 I" Z. c# g opus-1.1.3/silk/NLSF2A.c opus-1.1.3/silk/NLSF_stabilize.c" T+ R- k( P9 S& o/ C3 f/ n. q5 f6 U opus-1.1.3/silk/NLSF_unpack.c opus-1.1.3/silk/NLSF_VQ.c opus-1.1.3/silk/LPC_inv_pred_gain.c opus-1.1.3/silk/NLSF_VQ_weights_laroia.c& j2 d' @+ t8 f* q8 w opus-1.1.3/silk/pitch_est_tables.c: C& q+ Y6 E, N" b" o) `+ W' s4 ` opus-1.1.3/silk/NSQ_del_dec.c opus-1.1.3/silk/PLC.c opus-1.1.3/silk/NLSF_decode.c5 |0 J. i! q+ J- g: v# I5 @ opus-1.1.3/silk/NLSF_encode.c: r2 I$ C3 F, z! y opus-1.1.3/silk/LP_variable_cutoff.c+ y% m1 g1 V3 L9 D" \& j' W opus-1.1.3/silk/LPC_analysis_filter.c opus-1.1.3/silk/process_NLSFs.c opus-1.1.3/silk/stereo_find_predictor.c opus-1.1.3/silk/stereo_MS_to_LR.c opus-1.1.3/silk/resampler_private_AR2.c* p, @$ s3 r- N# l( ]! N opus-1.1.3/silk/resampler_down2_3.c opus-1.1.3/silk/stereo_LR_to_MS.c7 R) y/ A0 k0 }% { z opus-1.1.3/silk/stereo_encode_pred.c opus-1.1.3/silk/resampler_down2.c opus-1.1.3/silk/sigm_Q15.c opus-1.1.3/silk/resampler_rom.c1 X" S1 s# J) l, ?! r opus-1.1.3/silk/sort.c9 }- N) w9 U; V( @ opus-1.1.3/silk/resampler_private_down_FIR.c opus-1.1.3/silk/resampler_private_up2_HQ.c opus-1.1.3/silk/stereo_decode_pred.c$ \& P6 B+ |* W; o B' k' m opus-1.1.3/silk/shell_coder.c opus-1.1.3/silk/resampler_private_IIR_FIR.c; E# g) i9 d* _ opus-1.1.3/src/repacketizer.c opus-1.1.3/silk/sum_sqr_shift.c; Q9 h7 o4 k# z- @2 c: L& D; y opus-1.1.3/silk/table_LSF_cos.c opus-1.1.3/silk/tables_NLSF_CB_WB.c5 s8 s4 s% H% v& c7 j opus-1.1.3/silk/stereo_quant_pred.c opus-1.1.3/silk/VAD.c opus-1.1.3/silk/tables_gain.c( x5 o6 I$ ?, K. n opus-1.1.3/silk/tables_pulses_per_block.c #opus-1.1.3/stm32_m3/opus_main.c: i4 g1 S4 t0 [% w% d% o opus-1.1.3/src/opus_encoder.c opus-1.1.3/silk/tables_NLSF_CB_NB_MB.c I7 s( P! j8 }( l+ v) \ opus-1.1.3/silk/tables_other.c- X9 a& m$ z) Y9 ]& { opus-1.1.3/src/opus.c opus-1.1.3/silk/tables_pitch_lag.c opus-1.1.3/src/opus_decoder.c T7 ^% F' L& C/ V; _ opus-1.1.3/silk/VQ_WMat_EC.c: ]' i4 e/ ^1 m" j opus-1.1.3/silk/tables_LTP.c opus-1.1.3/celt/entdec.c opus-1.1.3/celt/entenc.c7 L* ~9 g( S3 |1 w. \- O1 Z; H opus-1.1.3/celt/cwrs.c opus-1.1.3/celt/mathops.c( E3 O2 B/ m: q1 j" | opus-1.1.3/celt/kiss_fft.c opus-1.1.3/celt/celt_lpc.c* n) m5 F- X% K! ]% `% i opus-1.1.3/celt/mdct.c opus-1.1.3/celt/modes.c opus-1.1.3/celt/bands.c opus-1.1.3/celt/pitch.c8 r( ~( n% g' e ?% z+ b, m& } opus-1.1.3/celt/celt_encoder.c opus-1.1.3/celt/laplace.c! n. G( O+ R- B6 h! I0 M: ` B, { opus-1.1.3/celt/celt_decoder.c opus-1.1.3/celt/entcode.c! L- c1 }# L) V9 U( C& g opus-1.1.3/celt/celt.c opus-1.1.3/silk/fixed/noise_shape_analysis_FIX.c opus-1.1.3/silk/fixed/process_gains_FIX.c opus-1.1.3/silk/fixed/regularize_correlations_FIX.c$ w7 J* g4 f3 s6 [8 y1 M$ t$ R% q6 h: i opus-1.1.3/silk/fixed/autocorr_FIX.c5 ?2 X; L6 d d/ ~- u4 ? opus-1.1.3/celt/quant_bands.c opus-1.1.3/silk/fixed/encode_frame_FIX.c opus-1.1.3/silk/fixed/corrMatrix_FIX.c- R3 S9 T# L8 ^/ ~( {2 X opus-1.1.3/silk/fixed/find_LTP_FIX.c opus-1.1.3/silk/fixed/pitch_analysis_core_FIX.c: x) v1 M. T# Y4 f opus-1.1.3/silk/fixed/prefilter_FIX.c opus-1.1.3/celt/rate.c opus-1.1.3/silk/fixed/find_pitch_lags_FIX.c, g9 \0 u' _+ {+ s opus-1.1.3/silk/fixed/burg_modified_FIX.c opus-1.1.3/silk/fixed/LTP_analysis_filter_FIX.c opus-1.1.3/celt/vq.c( m0 O$ ]9 t, Y0 V3 u( c2 Q opus-1.1.3/silk/fixed/find_LPC_FIX.c opus-1.1.3/silk/fixed/find_pred_coefs_FIX.c opus-1.1.3/silk/fixed/apply_sine_window_FIX.c" E4 i9 \0 C9 o; h$ P+ x opus-1.1.3/silk/fixed/k2a_FIX.c2 `- v% H5 i! }+ R4 d opus-1.1.3/silk/fixed/LTP_scale_ctrl_FIX.c opus-1.1.3/silk/fixed/k2a_Q16_FIX.c% g1 v q! p% ?; T Q; x 我为opus-1.1.3建立了单独的group,并将这个group 的 【Override inherited settings 】勾上 opus config
在【Proprocessor】里增加如下include 目录 $PROJ_DIR$/../opus-1.1.3/include $PROJ_DIR$/../opus-1.1.3/celt $PROJ_DIR$/../opus-1.1.3/silk7 h6 t5 y) m! W2 j0 _9 J $PROJ_DIR$/../opus-1.1.3/silk/fixed* P1 i8 ~4 X4 N* H+ C $PROJ_DIR$/../opus-1.1.3/stm32_m3 增加:HAVE_CONFIG_H 到【Defined symbols】
将【Language 1 】中的【Allow VLA】勾上,目的是#define VAR_ARRAYS 1 后能便宜通过。9 H# ~% h! D, G( a9 E& h , s+ @5 ` {" |$ M' v/ R1 N: m copy opus-1.1.3/win32 到 opus-1.1.3/stm32_m3,建立config.h 和 version.h" c: @; ^0 P8 W, e5 T; ?5 W1 m+ N% | config.h:% W. Y5 K/ _( ]) v" e/ } #ifndef CONFIG_H3 k) J3 S1 ?8 L7 J1 U8 a #define CONFIG_H //#define USE_ALLOCA 1 #define VAR_ARRAYS 1 X# x/ O/ D: Z /* Comment out the next line for floating-point code */6 P6 K4 X$ _2 Z$ U/ C /*#define FIXED_POINT 1 */: w" r$ K5 V; Y7 i #define OPUS_BUILD 1- v% x% g% r" O* O; z3 x #define DISABLE_FLOAT_API 1+ `3 @2 V$ `2 k #define FIXED_POINT 11 ~' E( V. W' m6 Y' h #define REMOVE_FOR_MALLOC9 j5 a" b* l9 p9 a1 X4 o* I //以上部分内容为增加部分,其他的与win32下同名文件相同 #endif /* CONFIG_H */ version.h #ifndef __VERSION_H__6 O5 O; b: a1 S7 i" ]# } #define __VERSION_H__ #define PACKAGE_VERSION "1.1.3" #endif /*__VERSION_H__*/ ! B" m( g' O7 V9 ^" v2 W encode 和decode 代码: pusDecoder *dec;2 I% w/ O+ ~3 q static int offset = 0; int size = 0; const unsigned char *packet; int i = 0;$ k1 K1 ?4 c9 ^ int frame_size = 0; int max_size = 0;$ O& g* e* n6 B5 e) W& ` char buffer[36]; uint32_t start = osKernelSysTick(); . ~+ R6 g" x) W* i OpusEncoder *enc = NULL;$ ] e2 P, c& w$ U# j0 R0 \ static int len = 10;$ t, K) c0 c2 Y0 o2 e# K$ s size = opus_encoder_get_size(channels); enc = pvPortMalloc(size); error = opus_encoder_init(enc, Fs, channels, OPUS_APPLICATION_RESTRICTED_LOWDELAY); opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_20_MS)); V2 _, _& t. t9 | P //opus_encoder_ctl(enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); opus_encoder_ctl(enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC)); y- F: {7 G2 V" a- m; L) @2 u len = opus_encode(enc, audio_frame + offset, Fs/50/*20ms*/, opus_buffer, sizeof(opus_buffer)); offset += Fs/50;$ C: c! j: x" x* b( E& R% R, t if(offset >= sizeof(audio_frame)/sizeof(uint16_t)) offset = 0; vPortFree(enc);0 w# `4 k; z1 D. Y4 ] OpusDecoder *st = (OpusDecoder *)pvPortMalloc(opus_decoder_get_size(channels));9 n% M( o8 R" _$ k/ d. C( ? error = opus_decoder_init(st, Fs, channels); 9 _% N+ m( N+ ^9 X8 A frame_size = opus_decode(st, opus_buffer, len, pcm, PCM_BUFFER_SIZE, 0);* g5 A6 `& W c3 d( ]9 ? 对了,我改了这两个宏, #define MAX_FS_KHZ 8 /*16 --> 8 modify for memory*/+ |- W' _" D! t6 n/ ]3 u #define MAX_API_FS_KHZ 8 /*48 --> 8 modify for memory/' I, ?+ t3 w+ b/ y- o' w* ]' U 原因是,如果不修改opus_encoder_get_size(1); 返回的是27k,改完后还得13k,实在是没有那么多ram. |: `& D* s: X size = opus_encoder_get_size(1); enc = pvPortMalloc(size);# l! X! h! [5 G1 y( S# J 总结,64k ram 感觉刚刚够,还没有增加其他的功能,解压缩20ms 数据用了6-7ms,编码我忘记了,记得编解码用了33ms,有点高。 声音可以用DAC(PA4)播放出来,需要外加功放,不加功放声音很不好。经过opus 压缩解压后声音损失较大,音乐还能听出来,但很多噪声。( k# D6 M( E/ T8 D) g$ `& H, Z6 R& g 3 @" V" j1 h9 W( [ |
| 谢谢分享 |
| 楼主你好,我想问一下STM3VE 用opus编解码效果如何? |
| 楼主的项目能放出来吗 感觉opus的代码很多,你上面介绍的是很多文件都不需要的 |
| 谢谢分享! |
微信公众号
手机版