ffmpeg中G726解码器用法 编码器用法

//解码:

本代码实现将cap1.g726文件中的g726编码帧数据进行解码,然后保存到cap1文件中


1、ffmpeg版本 :
     0.10.2

2、源码:

extern "C" { #include "./h/libavcodec/avcodec.h" };

#pragma comment(lib, "avutil.lib") //ffmpeg #pragma comment(lib, "avformat.lib") #pragma comment(lib, "avcodec.lib")

AVCodec *codec; AVCodecContext *c= NULL; AVPacket avpkt; AVFrame *decoded_frame = NULL;

//编码后一帧大小为 84字节 #define framesize 84

int main(int argc, char* argv[]) { printf("Hello World!\n"); unsigned char inbuffer[framesize] = {0}; /* register all the codecs */ avcodec_register_all(); av_init_packet(&avpkt); /* find the mpeg audio decoder */ codec = avcodec_find_decoder(CODEC_ID_ADPCM_G726); if (!codec) { fprintf(stderr, "codec not found\n"); return 0; } c = avcodec_alloc_context3(codec);

//没有如下两句 avcodec_open会返回 -22 错误

//采样率 = 8000 每个采样用的bit数 = 16 通道数 = 1 c->bits_per_coded_sample = 2; //g726压缩比为 8:1 编码前采样用bit数为16 那么编码后应该占16/8 = 2 这是我的理解

c->channels = 1; /* open it */ int iRet = avcodec_open(c, codec); if ( iRet < 0 ) { fprintf(stderr, "could not open codec\n"); return 0; } FILE *f, *outfile;

//打开存放g726音频帧的文件

f = fopen( "cap1.g726", "rb" ); if (!f) { return 0; }

//打开要存放解码后音频帧的文件 outfile = fopen("cap1", "wb"); if (!outfile)

{ av_free(c); return 0; } avpkt.data = inbuffer; while ( (avpkt.size = fread(inbuffer, 1, framesize, f) ) > 0 ) { int got_frame = 0; if (!decoded_frame) { if (!(decoded_frame = avcodec_alloc_frame())) { return 0; } } else { avcodec_get_frame_defaults(decoded_frame); } int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt); if (len < 0) { return 0; } if (got_frame) { /* if a frame has been decoded, output it */ int data_size = av_samples_get_buffer_size(NULL, c->channels, decoded_frame->nb_samples, c->sample_fmt, 1); fwrite(decoded_frame->data[0], 1, data_size, outfile); } } fclose(outfile); fclose(f); avcodec_close(c); av_free(c); av_free(decoded_frame); return 0; }

//编码

ffmpeg g726 编码  

2012-10-11 10:19:21|  分类: ffmpeg|举报|字号 订阅

一、将文件source中原始音频数据进行编码,并保存到dest中     采样率 = 8000     通道数 = 1     采样位数 = 16     波特率 = 16000 二、源码

extern "C" { #include "./h/libavcodec/avcodec.h" }; //视频相关库 #pragma comment(lib, "avutil.lib") //ffmpeg #pragma comment(lib, "avformat.lib") #pragma comment(lib, "avcodec.lib") AVCodec *codec; AVCodecContext *c= NULL; AVPacket avpkt; AVFrame *decoded_frame = NULL;

#define framesize 80 #define buffersize 640

int main(int argc, char* argv[]) {

/* register all the codecs */ avcodec_register_all(); av_init_packet(&avpkt);

//如下两句在编码中是必须的,要不avcodec_encode_audio2会失败

 avpkt.data = NULL;

avpkt.size = 0;

/* find the mpeg audio decoder */ codec = avcodec_find_encoder(CODEC_ID_ADPCM_G726); if (!codec) { fprintf(stderr, "codec not found\n"); return 0; } c = avcodec_alloc_context3(codec);

//put sample parameters c->bits_per_coded_sample = 2; c->bit_rate = 16000; // c->sample_rate = 8000; //采样率 c->channels = 1; //通道数 c->sample_fmt = AV_SAMPLE_FMT_S16; //应该是采样位数

/* open it */ int iRet = avcodec_open(c, codec); if ( iRet < 0 ) { fprintf(stderr, "could not open codec\n"); return 0; }

int buf_size = buffersize; char szBuffer[buffersize] = {0}; int ret = 0; int got_packet = 0; FILE *f; FILE *output;

//打开存放编码前音频文件

f = fopen("source", "rb");

//打开存放编码后音频文件

output = fopen( "dest", "wb" );

while( fread( szBuffer, 1, buffersize, f ) ) { if (!decoded_frame) { if (!(decoded_frame = avcodec_alloc_frame())) { return 0; } } else { avcodec_get_frame_defaults(decoded_frame); } decoded_frame->nb_samples = buf_size/(c->channels*av_get_bytes_per_sample(c->sample_fmt)); ret = avcodec_fill_audio_frame(decoded_frame, c->channels, c->sample_fmt, (unsigned char *)szBuffer,

buf_size, 1); if (ret < 0) { //av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); return 0; } if (avcodec_encode_audio2(c, &avpkt, decoded_frame, &got_packet) < 0) {

return 0; } if (got_packet) { int iiiii = fwrite( avpkt.data, 1, avpkt.size, output ); } } fclose(f); fclose(output);

}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值