电竞比分网-中国电竞赛事及体育赛事平台

分享

ios使用opus壓縮和解壓縮PCM文件

 QomoIT 2018-01-19

參照前兩篇文章編譯后,導(dǎo)入到工程,就可以使用了

具體四步:

1、opus_encoder_create創(chuàng)建

2、opus_encoder_ctl設(shè)置

3、opus_encode / opus_decode 編解碼

4、opus_encoder_destroy釋放

附上代碼

.h

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface opusCodec : NSObject  
  4. -(void)opusInit;  
  5. -(NSData*)encodePCMData:(NSData*)data;  
  6. -(NSData*)decodeOpusData:(NSData*)data;  
  7. @end  

.m


  1. #import "opusCodec.h"  
  2. #import "opus.h"  
  3.   
  4. #define WB_FRAME_SIZE 160  
  5.   
  6. @implementation opusCodec  
  7. {  
  8.     OpusEncoder *enc;  
  9.     OpusDecoder *dec;  
  10.     int opus_num;  
  11.     int pcm_num;  
  12.     unsigned char opus_data_encoder[40];  
  13. }  
  14.   
  15. -(void)opusInit  
  16. {  
  17.     int error;  
  18.     int Fs = 16000;//采樣率  
  19.     int channels = 1;  
  20.     int application = OPUS_APPLICATION_VOIP;  
  21.       
  22.     opus_int32 bitrate_bps = kDefaultSampleRate;  
  23.     int bandwidth = OPUS_AUTO;//OPUS_BANDWIDTH_NARROWBAND 寬帶窄帶  
  24.     int use_vbr = 0;  
  25.     int cvbr = 1;  
  26.     int complexity = 4; //錄制質(zhì)量 1-10  
  27.     int packet_loss_perc = 0;  
  28.       
  29.     enc = opus_encoder_create(Fs, channels, application, &error);  
  30.     dec = opus_decoder_create(Fs, channels, &error);  
  31.       
  32.     opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));  
  33.     opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bandwidth));  
  34.     opus_encoder_ctl(enc, OPUS_SET_VBR(use_vbr));  
  35.     opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cvbr));  
  36.     opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));  
  37.     opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(packet_loss_perc));  
  38.     opus_encoder_ctl(enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));//信號(hào)  
  39. }  
  40.   
  41. - (NSData *)encode:(shortshort *)pcmBuffer length:(int)lengthOfShorts  
  42. {  
  43.     NSMutableData *decodedData = [NSMutableData data];  
  44.     int frame_size = WB_FRAME_SIZE;  
  45.     short input_frame[frame_size];  
  46.     opus_int32 max_data_bytes = 2500;  
  47.       
  48.     memcpy(input_frame, pcmBuffer, frame_size * sizeof(short));  
  49.     int encodeBack = opus_encode(enc, input_frame, frame_size, opus_data_encoder, max_data_bytes);  
  50.     NSLog(@"encodeBack===%d",encodeBack);  
  51.     if (encodeBack > 0)  
  52.     {  
  53.         [decodedData appendBytes:opus_data_encoder length:encodeBack];  
  54.     }  
  55.       
  56.     return decodedData;  
  57. }  
  58.   
  59. //int decode(unsigned char* in_data, int len, short* out_data, int* out_len) {  
  60. -(int)decode:(unsigned charchar *)encodedBytes length:(int)lengthOfBytes output:(short*)decoded {  
  61.      int frame_size = WB_FRAME_SIZE;  
  62.     char cbits[frame_size + 1];  
  63.      memcpy(cbits, encodedBytes, lengthOfBytes);  
  64.     pcm_num = opus_decode(dec,  
  65.                         cbits,  
  66.                         lengthOfBytes,  
  67.                         decoded,  
  68.                         frame_size,  
  69.                         0);  
  70.   
  71.     return frame_size;  
  72.       
  73.      
  74. }  
  75. -(NSData*)encodePCMData:(NSData*)data  
  76. {  
  77.       
  78.       
  79.  return  [self encode:(shortshort *)[data bytes] length:[data length]/sizeof(short)];  
  80. }  
  81.   
  82. -(NSData*)decodeOpusData:(NSData*)data  
  83. {  
  84.     NSInteger len = [data length];  
  85.     NSLog(@"數(shù)據(jù)長(zhǎng)度===%d",len);  
  86.       
  87.     Byte *byteData = (Byte*)malloc(len);  
  88.       
  89.     memcpy(byteData, [data bytes], len);  
  90.       
  91.     short decodedBuffer[1024];  
  92.       
  93.     int nDecodedByte = sizeof(short) * [self decode:byteData length:len output:decodedBuffer];  
  94.     NSLog(@"返回的長(zhǎng)度==%d",nDecodedByte);  
  95.     NSData* PCMData = [NSData dataWithBytes:(Byte *)decodedBuffer length:nDecodedByte];  
  96.     return PCMData;  
  97. }  
  98.   
  99.   
  100. @end  


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多