一.初始化音频滤镜

  初始化音频滤镜的方法基本上和初始化视频滤镜的方法相同,不懂的可以看上篇博客,这里直接给出代码:

//audio_filter_core.cpp
#define INPUT_SAMPLERATE 44100
#define INPUT_FORMAT AV_SAMPLE_FMT_FLTP
#define INPUT_CHANNEL_LAYOUT AV_CH_LAYOUT_STEREO
static AVFilterGraph *filter_graph;
static AVFilterContext *abuffersrc_ctx;
static AVFilterContext *volume_ctx;
static AVFilterContext *aformat_ctx;
static AVFilterContext *abuffersink_ctx;
static AVFrame *input_frame= nullptr,*output_frame= nullptr;
int32_t init_audio_filter(const char *volume_factor){
int32_t result=0;
char ch_layout[64];
char options_str[1024];
AVDictionary *options_dict= nullptr;
//创建滤镜图
filter_graph=avfilter_graph_alloc();
if(!filter_graph){
cerr<<"Error:Unable to create filter graph."<<endl;
return -1;
}
//创建abuffer滤镜
const AVFilter *abuffer= avfilter_get_by_name("abuffer");
if(!abuffer){
cerr<<"Error:Could not find abuffer filter."<<endl;
return -1;
}
abuffersrc_ctx= avfilter_graph_alloc_filter(filter_graph,abuffer,"src");
if(!abuffersrc_ctx){
cerr<<"Error:could not allocate the abuffer instance."<<endl;
return -1;
}
av_get_channel_layout_string(ch_layout,sizeof(ch_layout),0,INPUT_CHANNEL_LAYOUT);
av_opt_set(abuffersrc_ctx,"channel_layout",ch_layout,AV_OPT_SEARCH_CHILDREN);
av_opt_set(abuffersrc_ctx,"sample_fmt",av_get_sample_fmt_name(INPUT_FORMAT),AV_OPT_SEARCH_CHILDREN);
av_opt_set_q(abuffersrc_ctx,"time_base",(AVRational){1,INPUT_SAMPLERATE},AV_OPT_SEARCH_CHILDREN);
av_opt_set_int(abuffersrc_ctx,"sample_rate",INPUT_SAMPLERATE,AV_OPT_SEARCH_CHILDREN);
result= avfilter_init_str(abuffersrc_ctx, nullptr);
if(result<0){
cerr<<"Error:could not initialize the abuffer filter."<<endl;
return -1;
}
//创建volume滤镜
const AVFilter *volume= avfilter_get_by_name("volume");
if(!volume){
cerr<<"Error:could not find volume filter."<<endl;
return -1;
}
volume_ctx= avfilter_graph_alloc_filter(filter_graph,volume,"volume");
if(!volume_ctx){
cerr<<"Error:could not allocate volume filter instance."<<endl;
return -1;
}
av_dict_set(&options_dict,"volume",volume_factor,0);
result= avfilter_init_dict(volume_ctx,&options_dict);
av_dict_free(&options_dict);
if(result<0){
cerr<<"Error:could not initialize volume filter instance."<<endl;
return -1;
}
//创建aformat滤镜
const AVFilter *aformat=avfilter_get_by_name("aformat");
if(!aformat){
cerr<<"Error:could not find aformat filter."<<endl;
return -1;
}
aformat_ctx= avfilter_graph_alloc_filter(filter_graph,aformat,"aformat");
if(!aformat_ctx){
cerr<<"Error:could not allocate aformat filter instance."<<endl;
return -1;
}
snprintf(options_str,sizeof(options_str),"sample_fmts=%s:sample_rates=%d:channel_layouts=stereo",av_get_sample_fmt_name(AV_SAMPLE_FMT_S16),44100);
result= avfilter_init_str(aformat_ctx,options_str);
if(result<0){
cerr<<"Error:could not initialize aformat filter."<<endl;
return -1;
}
//创建abuffersink滤镜
const AVFilter *abuffersink= avfilter_get_by_name("abuffersink");
if(!abuffersink){
cerr<<"Error:could not find abuffersink filter."<<endl;
return -1;
}
abuffersink_ctx= avfilter_graph_alloc_filter(filter_graph,abuffersink,"sink");
if(!abuffersink_ctx){
cerr<<"Error:could not allocate abuffersink filter instance."<<endl;
return -1;
}
result= avfilter_init_str(abuffersink_ctx, nullptr);
if(result<0){
cerr<<"Error:could not initialize abuffersink filter."<<endl;
return -1;
}
//连接创建好的滤镜
result=avfilter_link(abuffersrc_ctx,0,volume_ctx,0);
if(result>=0){
result=avfilter_link(volume_ctx,0,aformat_ctx,0);
}
if(result>=0){
result=avfilter_link(aformat_ctx,0,abuffersink_ctx,0);
}
if(result<0){
fprintf(stderr,"Error connecting filters\n");
return -1;
}
//配置滤镜图
result=avfilter_graph_config(filter_graph, nullptr);
if(result<0){
cerr<<"Error:Error configuring the filter graph."<<endl;
return -1;
}
//创建输入输出帧
input_frame=av_frame_alloc();
output_frame=av_frame_alloc();
if(!input_frame||!output_frame){
cerr<<"Error:frame allocation failed."<<endl;
return -1;
}
return 0;
}

二.初始化输入音频帧

  在这一步需要给输入音频帧设置一些参数,包括采样率,采样点个数,声道布局,音频帧格式等,然后就可以给音频帧分配内存空间了。代码如下:

//audio_filter_core.cpp
static int32_t init_frames(){
int result=0;
input_frame->sample_rate=44100;
input_frame->format=AV_SAMPLE_FMT_FLTP;
input_frame->channel_layout=AV_CH_LAYOUT_STEREO;
input_frame->nb_samples=1024;
result= av_frame_get_buffer(input_frame,0);
if(result<0){
cerr<<"Error:av_frame_get_buffer failed."<<endl;
return -1;
}
result= av_frame_make_writable(input_frame);
if(result<0){
cerr<<"Error:av_frame_make_writable failed."<<endl;
return -1;
}
return 0;
}

三.循环编辑音频帧

  在这一步需要注意的是,每次将输入音频帧放入滤镜图前,都要做一次初始化音频帧操作,否则会报错:filter context - fmt: fltp r: 44100 layout: 3 ch: 2, incoming frame - fmt: (null) r: 0 layout: 3 ch: 2 pts_time: NOPTS【Changing audio frame properties on the fly is not supported.】。注意一定是每次,不要只初始化一次,这样只有第一帧初始化了,后面的帧还是会报错,因为输入帧的格式要和滤镜上下文保持一致,如果没有每次都初始化,后面的帧的格式和采样率就识别不到,为null了。下面给出代码:

//audio_filter_core.cpp
static int32_t filter_frame(){
int32_t result= av_buffersrc_add_frame(abuffersrc_ctx,input_frame);
if(result<0){
cerr<<"Error:add frame to buffersrc failed."<<endl;
return -1;
}
while(1){
result=av_buffersink_get_frame(abuffersink_ctx,output_frame);
if(result==AVERROR(EAGAIN)||result==AVERROR_EOF){
return 1;
}
else if(result<0){
cerr<<"Error:av_buffersink_get_frame failed."<<endl;
return -1;
}
cout<<"Output channels:"<<output_frame->channels<<",nb_samples:"<<output_frame->nb_samples<<",sample_fmt:"<<output_frame->format<<endl;
write_samples_to_pcm2(output_frame);
av_frame_unref(output_frame);
}
return 0;
}
int32_t audio_filtering(){
int32_t result=0;
while(!end_of_input_file()){
init_frames();//每次都要执行
result=read_pcm_to_frame2(input_frame,INPUT_FORMAT,2);
if(result<0){
cerr<<"Error:read_pcm_to_frame2 failed."<<endl;
return -1;
}
result=filter_frame();
if(result<0){
cerr<<"Error:filter_frame failed."<<endl;
return -1;
}
}
return 0;
}

四.将编辑后的数据写入输出文件

  在这一步需要注意的是,由于在滤镜图中有一个滤镜实例将音频帧的采样格式设置为了AV_SAMPLE_FMT_S16,这是packed格式的帧,左右声道的数据交错存储在frame->data[0]指向的内存单元中,所以在写入的时候,需要注意这一点。下面给出代码:

//io_data.cpp
int32_t write_samples_to_pcm2(AVFrame* frame){
int16_t* samples = reinterpret_cast<int16_t*>(frame->data[0]);
int dataSize = frame->nb_samples * frame->channels * sizeof(int16_t);
fwrite(samples, 1, dataSize, output_file);
return 0;
}

  数据读入代码:

//io_data.cpp
static FILE* input_file= nullptr;
static FILE* output_file= nullptr;
int32_t read_pcm_to_frame2(AVFrame *frame,enum AVSampleFormat sample_fmt,int channels){
int data_size= av_get_bytes_per_sample(sample_fmt);
if(data_size<0){
cerr<<"Error:Failed to calculate data size."<<endl;
return -1;
}
for(int i=0;i<frame->nb_samples;i++){
for(int ch=0;ch<channels;ch++){
fread(frame->data[ch]+i*data_size,1,data_size,input_file);
}
}
return 0;
}
int32_t open_input_output_files(const char* input_name,const char* output_name){
if(strlen(input_name)==0||strlen(output_name)==0){
cout<<"Error:empty input or output file name."<<endl;
return -1;
}
close_input_output_files();
input_file=fopen(input_name,"rb");//rb:读取一个二进制文件,该文件必须存在
if(input_file==nullptr){
cerr<<"Error:failed to open input file."<<endl;
return -1;
}
output_file=fopen(output_name,"wb");//wb:打开或新建一个二进制文件,只允许写
if(output_file== nullptr){
cout<<"Error:failed to open output file."<<endl;
return -1;
}
return 0;
}
void close_input_output_files(){
if(input_file!= nullptr){
fclose(input_file);
input_file= nullptr;
}
if(output_file!= nullptr){
fclose(output_file);
output_file= nullptr;
}
}
int32_t end_of_input_file(){
return feof(input_file);
}

五.销毁资源

audio_filter_core.cpp
static void free_frames(){
av_frame_free(&input_frame);
av_frame_free(&output_frame);
}
void destroy_audio_filter(){
free_frames();
avfilter_graph_free(&filter_graph);
}

六.main函数实现

int main(){
const char *input_file_name="../input.pcm";
const char *output_file_name="../output.pcm";
const char *volume_factor="0.9";
int32_t result=0;
result= open_input_output_files(input_file_name,output_file_name);
if(result<0){
return -1;
}
result= init_audio_filter(volume_factor);
if(result<0){
return -1;
}
result=audio_filtering();
if(result<0){
return -1;
}
destroy_audio_filter();
close_input_output_files();
return 0;
}

  最后,可以使用下面的指令测试输出的pcm文件:

  ffplay -ac 2 -ar 44100 -f s16le -i output.pcm

 

如何使用libavfilter库给pcm音频采样数据添加音频滤镜?的更多相关文章

  1. 视音频数据处理入门:PCM音频采样数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  2. 音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系

    音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系 原创 2014年02月11日 13:56:51 4951 0 0 刚刚过完春节,受假期综合症影响脑袋有点发 ...

  3. Java中解析wav音频文件信息:音频声道数,采样频率,采样位数、声音尺寸

    前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 音频解析方法: public static int toInt(byte[] b) { return ((b[3] << 2 ...

  4. WebRTC 音频采样算法 附完整C++示例代码

    之前有大概介绍了音频采样相关的思路,详情见<简洁明了的插值音频重采样算法例子 (附完整C代码)>. 音频方面的开源项目很多很多. 最知名的莫过于谷歌开源的WebRTC, 其中的音频模块就包 ...

  5. 常用音频协议介绍&&有关音频编码的知识与技术参数

    (转载)常用音频协议介绍 会议电视常用音频协议介绍及对比白皮书 一.数字化音频原理:声音其实是一种能量波,因此也有频率和振幅的特征,频率对应于时间轴线,振幅对应于电平轴线.通常人耳可以听到的频率在20 ...

  6. iOS从零开始学习直播之音频1.播放本地音频文件

      现在直播越来越火,俨然已经成为了下一个红海.作为一个资深码农(我只喜欢这样称呼自己,不喜欢别人这样称呼我),我必须赶上时代的潮流,开始研究视频直播.发现视屏直播类的文章上来就讲拉流.推流.采集.美 ...

  7. sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)

    sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器) 我们日常使用SQL Server数据库时,经常遇到需要在实例Instance01中跨实例访问Instanc ...

  8. iOS 直播-获取音频(视频)数据

    iOS 直播-获取音频(视频)数据 // // ViewController.m // capture-test // // Created by caoxu on 16/6/3. // Copyri ...

  9. HI3516EV100 RTMP添加音频

    1.先试试只发音频 失败 2.保存音频,发送视频,成功

  10. Android Volley 库通过网络获取 JSON 数据

    本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库通过网络获取 JSON 数据 参考资料 Android 关于网络操作一般都会介绍 HttpC ...

随机推荐

  1. [VMware/CENOTS/Linux]VMware设置CentOS7共享文件夹[转载]

    0 环境信息 VMWare: Linux CENTOS: 7.9.2009(Core) CPU: x86_64 / amd64 待共享的共享文件夹的物理路径: E:\VirtualMachine\sh ...

  2. mapper接口中常见的增删改查

    前言 相信大家在使用mybatis写mapper接口的时候,最常用且简单的方法就是增删改查了.我也是刚开始做项目,在本篇文章中,我将根据自己在vhr微人力项目中的mapper接口方法为实例,记录一下接 ...

  3. 深度剖析Redis九种数据结构实现原理,建议收藏

    1. Redis介绍 Redis 是一个高性能的键值存储系统,支持多种数据结构. 包含五种基本类型 String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),和 ...

  4. Html 设置标题栏顶部固定

    如何设置标题栏一直置顶固定显示? 只需要给标题栏所在的容器,以下设置:   position: fixed;   top: 0px;   left: 0px;   width: 100%; 位置固定在 ...

  5. RocketMQ的简单使用

    大家好,我是Leo!今天来和大家分享RocketMQ的一些用法. 领域模型介绍 Producer: 用于生产消息的运行实体. Topic: 主题,用于消息传输和存储的分组容器. MessageQueu ...

  6. NC51101 Lost Cows

    题目链接 题目 题目描述 \(N (2 \leq N \leq 8,000)\) cows have unique brands in the range 1..N. In a spectacular ...

  7. abp(net core)+easyui+efcore实现仓储管理系统——供应商管理升级之下(六十四)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统--ABP总体介绍(一) abp(net core)+ ...

  8. vue3 + vite 多项目多模块打包

    vue3 + vite 多项目多模块打包 本示例基于vite-plugin-html插件,实现多个独立项目共存,共享组件和依赖,运行.打包互不干扰. npm create vite@latest 兼容 ...

  9. 一站式统一返回值封装、异常处理、异常错误码解决方案—最强的Sping Boot接口优雅响应处理器

    作者:京东物流 覃玉杰 1. 简介 Graceful Response是一个Spring Boot体系下的优雅响应处理器,提供一站式统一返回值封装.异常处理.异常错误码等功能. 使用Graceful ...

  10. #Python基础 pandas索引设置

    一:XMIND 二:设置索引 示例数据,假设我们有一个DataFrame对象,如下: import pandas as pd df = pd.DataFrame({ "name": ...