ffmpeg库G726编解码

ffmpeg g726编码器:AV_CODEC_ID_ADPCM_G726

ffmpeg g726解码器包括:AV_CODEC_ID_ADPCM_G726、AV_CODEC_ID_ADPCM_G726LE

 

AV_CODEC_ID_ADPCM_G726,无法指定码流,默认是16kps;AV_CODEC_ID_ADPCM_G726LE,可以指导码流,AVCodecContext中的bits_per_coded_sample:表示编码压缩bit值与采样率的bit值之比。如果为g726音频时,表示g726码流压缩与采样率比值。比如kbps码流压缩比为:k/8k = 5,kbps码流压缩比为k/8k = 2。

 //编码

  1. extern "C"  
  2. {  
  3. #include "libavcodec/avcodec.h"  
  4. #include "libavformat/avformat.h"  
  5. #include "libavutil/frame.h"  
  6. #include "libswscale/swscale.h"  
  7. #include "libavutil/imgutils.h"  
  8. }  
  9. //链接ffmpeg lib库  
  10.   
  11. //pcm to g726  
  12.     AVCodec *codec;  
  13.     AVCodecContext *c= NULL;  
  14.     AVPacket avpkt;  
  15.     AVFrame *decoded_frame = NULL;  
  16.   
  17.     /* register all the codecs */  
  18.     avcodec_register_all();  
  19.     av_init_packet(&avpkt);  
  20.   
  21.     avpkt.data = NULL;  
  22.     avpkt.size = 0;  
  23.     /* find the mpeg audio decoder */  
  24.     codec = avcodec_find_encoder(AV_CODEC_ID_ADPCM_G726);  
  25.     if (!codec)   
  26.     {  
  27.         fprintf(stderr, "codec not found\n");  
  28.         return;  
  29.     }  
  30.     c = avcodec_alloc_context3(codec);  
  31.     //put sample parameters   
  32.     c->bits_per_coded_sample = 2;  
  33.     c->bit_rate = 16000;    //  
  34.     c->sample_rate = 8000;    //采样率  
  35.     c->channels = 1;     //通道数  
  36.     c->sample_fmt = AV_SAMPLE_FMT_S16;//采样位数  
  37.     /* open it */  
  38.     int iRet = avcodec_open2(c, codec,NULL);  
  39.     if ( iRet < 0 )   
  40.     {  
  41.         fprintf(stderr, "could not open codec\n");  
  42.         return;  
  43.     }  
  44.   
  45.     CString filePath = "";  
  46.     CString newlFilePath = "";  
  47.     char szFilter[] = {"Pcm Files (*.pcm)|*.pcm||"};  
  48.     CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);  
  49.     if(dlg.DoModal() == IDOK)  
  50.     {  
  51.         filePath = dlg.GetPathName();  
  52.         newlFilePath = filePath;  
  53.         newlFilePath.Replace(".pcm","_ff.g726");  
  54.         FILE * fpSrc = fopen(filePath.GetBuffer(filePath.GetLength()),"rb");  
  55.         FILE * fpDst = fopen(newlFilePath.GetBuffer(newlFilePath.GetLength()),"wb+");  
  56.         char szData[320] = {0};  
  57.         char szOutData[200] = {0};  
  58.         int  nDataLen = 320;  
  59.         int  nOutDataLen = 200;  
  60.         int  nReadedSize = 0;  
  61.         int  ret = 0;  
  62.         if(fpSrc != NULL)  
  63.         {  
  64.             while(TRUE)  
  65.             {  
  66.                 nReadedSize = fread(szData,sizeof(char),nDataLen,fpSrc);  
  67.                 if(nReadedSize < nDataLen)  
  68.                 {  
  69.                     break;  
  70.                 }  
  71.                 if (!decoded_frame)   
  72.                 {  
  73.                     if (!(decoded_frame = avcodec_alloc_frame()))   
  74.                     {  
  75.                         return;  
  76.                     }  
  77.                 }   
  78.                 else  
  79.                 {  
  80.                     avcodec_get_frame_defaults(decoded_frame);  
  81.                 }  
  82.   
  83.                 decoded_frame->nb_samples = nReadedSize/(c->channels*av_get_bytes_per_sample(c->sample_fmt));  
  84.                 ret = avcodec_fill_audio_frame(decoded_frame, c->channels, c->sample_fmt, (unsigned char *)szData,nReadedSize, 1);  
  85.                 if (ret < 0)   
  86.                 {  
  87.                     return;  
  88.                 }  
  89.                 int got_packet = 0;  
  90.                 if (avcodec_encode_audio2(c, &avpkt, decoded_frame, &got_packet) < 0)   
  91.                 {  
  92.                     return;  
  93.                 }  
  94.                 if (got_packet)   
  95.                 {  
  96.                     int iiiii = fwrite( avpkt.data, 1, avpkt.size, fpDst );  
  97.                 }  
  98.             }  
  99.   
  100.             fclose(fpSrc);  
  101.             fclose(fpDst);  
  102.         }  
  103.   
  104.     }  

//解码
  1. extern "C"  
  2. {  
  3. #include "libavcodec/avcodec.h"  
  4. #include "libavformat/avformat.h"  
  5. #include "libavutil/frame.h"  
  6. #include "libswscale/swscale.h"  
  7. #include "libavutil/imgutils.h"  
  8. }  
  9. //链接ffmpeg lib库  
  10.   
  11. //g726 to pcm  
  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.     codec = avcodec_find_decoder(AV_CODEC_ID_ADPCM_G726);  
  20.     if (!codec)   
  21.     {  
  22.         fprintf(stderr, "codec not found\n");  
  23.         return;  
  24.     }  
  25.     c = avcodec_alloc_context3(codec);  
  26.     //采样率= 8000 每个采样用的bit数= 16 通道数= 1  
  27.     c->bits_per_coded_sample = 2;   //g726压缩比为8:1 编码前采样用bit数为那么编码后应该占/8 = 2  
  28.     c->channels = 1;  
  29.     c->sample_fmt = AV_SAMPLE_FMT_S16;  
  30.     c->sample_rate = 8000;  
  31.     c->codec_type = AVMEDIA_TYPE_AUDIO;  
  32.     c->bit_rate = 16000;  
  33.     int iRet = avcodec_open2(c, codec,NULL);  
  34.     if ( iRet < 0 )   
  35.     {  
  36.         fprintf(stderr, "could not open codec\n");  
  37.         return;  
  38.     }  
  39.   
  40.     CString filePath = "";  
  41.     CString newlFilePath = "";  
  42.     char szFilter[] = {"g726 Files (*.g726)|*.g726||"};  
  43.     CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);  
  44.     if(dlg.DoModal() == IDOK)  
  45.     {  
  46.         filePath = dlg.GetPathName();  
  47.         newlFilePath = filePath;  
  48.         newlFilePath.Replace(".g726",".pcm");  
  49.         BOOL bRet = 0;  
  50.         FILE * fpSrc = fopen(filePath.GetBuffer(filePath.GetLength()),"rb");  
  51.         FILE * fpDst = fopen(newlFilePath.GetBuffer(newlFilePath.GetLength()),"wb+");  
  52.         char szData[100] = {0};  
  53.         char szOutData[320] = {0};  
  54.         int  nDataLen = c->bits_per_coded_sample*20;  
  55.         int  nOutDataLen = 320;  
  56.         int  nReadedSize = 0;  
  57.         if(fpSrc != NULL)  
  58.         {  
  59.             while(TRUE)  
  60.             {  
  61.                 nDataLen = 40;  
  62.                 nReadedSize = fread(szData,sizeof(char),nDataLen,fpSrc);  
  63.                 if(nReadedSize < nDataLen)  
  64.                 {  
  65.                     break;  
  66.                 }  
  67.                 avpkt.data = (uint8_t *)szData;  
  68.                 avpkt.size = nDataLen;  
  69.                 int got_frame = 0;  
  70.                 if (!decoded_frame)   
  71.                 {  
  72.                     if (!(decoded_frame = avcodec_alloc_frame()))   
  73.                     {  
  74.                         return;  
  75.                     }  
  76.                 }   
  77.                 else  
  78.                 {  
  79.                     avcodec_get_frame_defaults(decoded_frame);  
  80.                 }  
  81.                 int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);  
  82.                 if (len < 0)   
  83.                 {  
  84.                     return;  
  85.                 }  
  86.                 if (got_frame)   
  87.                 {  
  88.                     /* if a frame has been decoded, output it */  
  89.                     int data_size = av_samples_get_buffer_size(NULL, c->channels,  
  90.                         decoded_frame->nb_samples,  
  91.                         c->sample_fmt, 1);  
  92.                     fwrite(decoded_frame->data[0], 1, data_size, fpDst);  
  93.                 }  
  94.             }  
  95.   
  96.             fclose(fpSrc);  
  97.             fclose(fpDst);  
  98.             avcodec_close(c);  
  99.             av_free(c);  
  100.             av_free(decoded_frame);  
  101.         }  
  102.   
  103.     } 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值