android生成预处理文件,FFmpeg:Android利用Filter进行音频数据预处理

前言

这篇文件简单介绍下移动端Android系统下利用FFmpeg的Filter进行音频数据预处理的方法。

按照惯例先上一份源码 AndroidFFmpegFilter。

项目实现了:

FFmepg编译须知

由于需要用到Filter模块,

所以在FFmpeg编译脚本中需要开启相关编译选项。

否则会出现 avfilter_get_by_name(filter_name) 找不到对应的处理器。

--enable-filters

#or

--enable-filter=name #name 指定需要使用到的filter name

如果不想自己编译,可以使用项目编译好的 动态库。

使用FFmpeg相关动态库

接下来要将ffmpeg的动态库链接到我们的工程上面。

Filter相关只需要使用到libavfilter.so、libavformat.so、libavutil.so这三个动态库。

参考代码如下(提供CMake实现,Android.mk请自己转换):

set(LIB_DIR ${PROJECT_SOURCE_DIR}/libs)

#设置ffmpeg的头文件目录位置

include_directories(${LIB_DIR}/include/ffmpeg)

#导入avfilter动态库

add_library( avfilter

SHARED

IMPORTED )

set_target_properties( avfilter

PROPERTIES

IMPORTED_LOCATION

${LIB_DIR}/${ANDROID_ABI}/libavfilter-6.so )

#导入avformat动态库

add_library( avformat

SHARED

IMPORTED )

set_target_properties( avformat

PROPERTIES

IMPORTED_LOCATION

${LIB_DIR}/${ANDROID_ABI}/libavformat-57.so )

#导入avutil动态库

add_library( avutil

SHARED

IMPORTED )

set_target_properties( avutil

PROPERTIES

IMPORTED_LOCATION

${LIB_DIR}/${ANDROID_ABI}/libavutil-55.so )

#连接动态库

target_link_libraries(

your-lib

avfilter

avutil

avformat

)

FFmpeg Filter初始化流程

导入头文件

extern "C" {

#include

#include

#include

};

注册相关filter

avfilter_register_all();

获取一个AVFilterGraph

利用这个Graph可以对后续的AVFilter进行管理。

AVFilterGraph *graph = avfilter_graph_alloc();

对于AVFilter的处理

一般步骤都是:

1、通过filter_name获取到需要使用的AVFilter。

AVFilter filter = avfilter_get_by_name(filter_name);

2、利用AVFilter从AVFilterGraph获取到相应的上下文环境。

AVFilterContext filter_ctx = avfilter_graph_alloc_filter(graph, filter, NULL);

3、构造初始化参数配置(多种方式)

方式一

char options_str[1024];

snprintf(options_str, sizeof(options_str),

"sample_fmt=%s:sample_rate=%d:channel_layout=0x%" PRIx64 ,

av_get_sample_fmt_name(sample_format),

sample_rate,

sample_channel);

avfilter_init_str(filter_ctx, options_str);

方式二

char ch_layout[64];

av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, sample_channel);

av_opt_set(filter_ctx, "channel_layout", ch_layout, AV_OPT_SEARCH_CHILDREN);

av_opt_set(filter_ctx, "sample_fmt", av_get_sample_fmt_name(sample_format), AV_OPT_SEARCH_CHILDREN);

av_opt_set_int(filter_ctx, "sample_rate", sample_rate, AV_OPT_SEARCH_CHILDREN);

avfilter_init_str(filter_ctx, NULL);

方式三

AVDictionary *options_dict = NULL;

char ch_layout[64];

av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, sample_channel);

av_dict_set(&options_dict, "channel_layout", ch_layout, AV_OPT_SEARCH_CHILDREN);

av_dict_set(&options_dict, "sample_fmt", av_get_sample_fmt_name(sample_format), AV_OPT_SEARCH_CHILDREN);

av_dict_set(&options_dict, "sample_rate", sample_rate, AV_OPT_SEARCH_CHILDREN);

avfilter_init_dict(volume_ctx, &options_dict);

PS:以上三种方式的实现效果是一致的。

对各个Filter进行链接

连接情况一(例如音量调节):

//abuffersrc_ctx -> volume_ctx -> abuffersink_ctx

avfilter_link(abuffersrc_ctx, 0, volume_ctx, 0);

avfilter_link(volume_ctx, 0, abuffersink_ctx, 0);

连接情况二 (例如混音):

//abuffersrc1_ctx

// -> amix_ctx -> abuffersink_ctx

//abuffersrc2_ctx

avfilter_link(abuffersrc1_ctx, 0, amix_ctx, 0);

avfilter_link(abuffersrc2_ctx, 0, amix_ctx, 1);

avfilter_link(amix_ctx, 0, abuffersink_ctx, 0);

初始化整个filters链

avfilter_graph_config(graph, NULL);

以上的流程就是整个FFmpeg Filter的初始化过程。

FFmpeg Filter使用流程

源音频数据输入

1、构造一个AVFrame:

//获取一个AVFrame实例

AVFrame *avframe = av_frame_alloc();

//配置输入音频的格式、采样率、声道和采样数

avframe->sample_rate = sample_rate;

avframe->format = sample_format;

avframe->channel_layout = sample_channel;

avframe->nb_samples = nb_sample;

//根据上面设置的情况,申请音频数据缓冲区

av_frame_get_buffer(avframe, 1);

2、将源音频输入送入Filter链中:

av_buffersrc_add_frame(abuffersrc_ctx, avframe);

3、销毁AVFrame相关资源

av_frame_free(&avframe);

处理后音频数据输出

1、申请一个AVFrame实例,值得提醒的是我们不需要对这个AVFrame做任何配置

AVFrame *avframe = av_frame_alloc();

2、从Filters链中获取处理后的数据包

av_buffersink_get_frame(abuffersink_ctx, avframe);

3、提取完毕AVFrame的数据后,我们需要将其销毁

av_frame_free(&avframe);

最后说几句

对于FFmpeg Filter的使用,基本都是遵循上述流程。

注册Filters

获取一个AVFilterGraph

获取多个AVFilter和AVFilterContext并进行参数配置

连接各个AVFilterContext

初始化整个Filters链

将源数据AVFrame输入Filters链接收端

从Filters链输出端获取处理后数据AVFrame

对于音量调节,我们需要获取如下几个filter:

abuffer:提供了音频数据的输入端。

volume:提供了音频数据音量调节的模块。

aformat:提供了转换成我们期望输出格式的模块,是因为Graph会在abuffer和volume之间自动做了格式转换。

abuffersink:提供了音频数据的输出端。

对于混音,我们需要获取如下几个filter:

abuffer:提供了音频数据的输入端,我们需要获取两个,因为有两路输入。

amix:提供了多路音频数据混合的模块。

aformat:提供了转换成我们期望输出格式的模块,是因为Graph会在abuffer和amix之间自动做了格式转换。

abuffersink:提供了音频数据的输出端。

播放PCM文件可以利用Audacity这个工具可以导入pcm原始文件,并且提供了波形图查看和播放功能。

本文同步发布于简书、CSDN。

End!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值