ffmpeg解密海思音频

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huabiaochen/article/details/83012355

使用海思语音编解码库进行 G711、G726、ADPCM 格式的编码,编码后的码流遵循以下表格中描述的帧结构,即在每帧码流数据的净荷数据之前填充有 4 个字节的帧头;使用语音编解码库进行以上格式的解码时,需要读取相应的帧头信息。

             

      这4个字节的帧头内容即为如下数组中的值:

            static char aryHeard[4] = {0,1,160,0}; //hisi audio header

1表示音频    160表示的实际长度为160 * short  = 320

==================================================

解码流程:

1、  读取海思g726音频数据,海思g726音频会多4个字节的海思头信息。

2、选择ffmpeg g726编码器进行解码。ffmpeg g726解码器包括:AV_CODEC_ID_ADPCM_G726、AV_CODEC_ID_ADPCM_G726LE。如果海思g726码流类型为asf时,请选择AV_CODEC_ID_ADPCM_G726解码器类型;如果海思g726码流类型为RFC3551标准时,请选择AV_CODEC_ID_ADPCM_G726LE解码器类型。

 实例代码:

[cpp] view plain copy

  1. #define  Hisi_AUDIO_HERDER_LEN          4                   //hisi 音频数据头  
  2. extern "C"  
  3. {  
  4. #include "libavcodec/avcodec.h"  
  5. #include "libavformat/avformat.h"  
  6. #include "libavutil/frame.h"  
  7. #include "libswscale/swscale.h"  
  8. #include "libavutil/imgutils.h"  
  9. }  
  10. //链接 ffmpeg lib库  
  11.   
  12. AVCodec *codec;  
  13.     AVCodecContext *c= NULL;  
  14.     AVPacket avpkt;  
  15.     AVFrame *decoded_frame = NULL;  
  16.     avcodec_register_all();  
  17.     av_init_packet(&avpkt);  
  18.     /* find the mpeg audio decoder */  
  19.     /* 
  20.     ffmpeg g726编码器:AV_CODEC_ID_ADPCM_G726 
  21.     ffmpeg g726解码器包括:AV_CODEC_ID_ADPCM_G726、AV_CODEC_ID_ADPCM_G726LE 
  22.     如果海思g726码流类型为asf时,请选择AV_CODEC_ID_ADPCM_G726解码器类型 
  23.     如果海思g726码流类型为RFC3551标准时,请选择AV_CODEC_ID_ADPCM_G726LE解码器类型 
  24.     */  
  25.     codec = avcodec_find_decoder(AV_CODEC_ID_ADPCM_G726LE);  
  26.     if (!codec)   
  27.     {  
  28.         fprintf(stderr, "codec not found\n");  
  29.         return;  
  30.     }  
  31.     c = avcodec_alloc_context3(codec);  
  32.     //采样率= 8000 每个采样用的bit数= 16 通道数= 1  
  33.     /* 
  34.     bits_per_coded_sample:表示编码压缩bit值与采样率的bit值之比。 
  35.     如果为g726音频时,表示g726码流压缩与采样率比值。比如kbps码流压缩比为:k/8k = 5,kbps码流压缩比为k/8k = 2。 
  36.     */  
  37.     c->bits_per_coded_sample = 5;  
  38.     c->channels = 1;  
  39.     c->sample_fmt = AV_SAMPLE_FMT_S16;  
  40.     c->sample_rate = 8000;  
  41.     c->codec_type = AVMEDIA_TYPE_AUDIO;  
  42.     //c->bit_rate = 16000;  
  43.     int iRet = avcodec_open2(c, codec,NULL);  
  44.     if ( iRet < 0 )   
  45.     {  
  46.         fprintf(stderr, "could not open codec\n");  
  47.         return;  
  48.     }  
  49.   
  50.     CString filePath = "";  
  51.     CString newlFilePath = "";  
  52.     char szFilter[] = {"g726 Files (*.g726_hisi)|*.g726_hisi||"};  
  53.     CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);  
  54.     if(dlg.DoModal() == IDOK)  
  55.     {  
  56.         filePath = dlg.GetPathName();  
  57.         newlFilePath = filePath;  
  58.         newlFilePath.Replace(".g726_hisi",".hisi2ff.pcm");  
  59.         BOOL bRet = 0;  
  60.         FILE * fpSrc = fopen(filePath.GetBuffer(filePath.GetLength()),"rb");  
  61.         FILE * fpDst = fopen(newlFilePath.GetBuffer(newlFilePath.GetLength()),"wb+");  
  62.         char szData[100] = {0};  
  63.         char szOutData[320] = {0};  
  64.         int  nDataLen = 100;  
  65.         int  nOutDataLen = 320;  
  66.         int  nReadedSize = 0;  
  67.         unsigned short usHisiHeader[2] = {0};  
  68.         if(fpSrc != NULL)  
  69.         {  
  70.             while(TRUE)  
  71.             {  
  72.                 //读取头标记  
  73.                 nDataLen = Hisi_AUDIO_HERDER_LEN;  
  74.                 nReadedSize = fread(szData,sizeof(char),nDataLen,fpSrc);  
  75.                 if(nReadedSize < nDataLen)  
  76.                 {  
  77.                     break;  
  78.                 }  
  79.                 memcpy(usHisiHeader,szData,Hisi_AUDIO_HERDER_LEN);  
  80.                 int nAudioFrameDataLen = (usHisiHeader[1] & 0x00ff) * sizeof(unsigned short);  
  81.                 nDataLen = nAudioFrameDataLen;  
  82.                 //读取音频帧数据  
  83.                 nReadedSize = fread(szData,sizeof(char),nDataLen,fpSrc);  
  84.                 if(nReadedSize < nDataLen)  
  85.                 {  
  86.                     break;  
  87.                 }  
  88.                 avpkt.data = (uint8_t *)szData;  
  89.                 avpkt.size = nReadedSize;  
  90.                 int got_frame = 0;  
  91.                 if (!decoded_frame)   
  92.                 {  
  93.                     if (!(decoded_frame = avcodec_alloc_frame()))   
  94.                     {  
  95.                         return;  
  96.                     }  
  97.                 }   
  98.                 else  
  99.                 {  
  100.                     avcodec_get_frame_defaults(decoded_frame);  
  101.                 }  
  102.                 int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);  
  103.                 if (len < 0)   
  104.                 {  
  105.                     return;  
  106.                 }  
  107.                 if (got_frame)   
  108.                 {  
  109.                     /* if a frame has been decoded, output it */  
  110.                     int data_size = av_samples_get_buffer_size(NULL, c->channels,  
  111.                         decoded_frame->nb_samples,  
  112.                         c->sample_fmt, 1);  
  113.   
  114.                     fwrite(decoded_frame->data[0], 1, data_size, fpDst);  
  115.                       
  116.                 }  
  117.             }  
  118.   
  119.             fclose(fpSrc);  
  120.             fclose(fpDst);  
  121.             avcodec_close(c);  
  122.             av_free(c);  
  123.             av_free(decoded_frame);  
  124.         }  
  125.   
  126.     }  
  127.  

猜你喜欢

转载自blog.csdn.net/huabiaochen/article/details/83012355
今日推荐