You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.9 KiB
77 lines
2.9 KiB
#include "MediaAVFilter.h" |
|
|
|
MediaAVFilter::MediaAVFilter() |
|
{ |
|
} |
|
MediaAVFilter::~MediaAVFilter() |
|
{ |
|
if(m_src_filter_ctx != nullptr){ |
|
avfilter_free(m_src_filter_ctx); |
|
m_src_filter_ctx = nullptr; |
|
} |
|
if(m_sink_filter_ctx != nullptr){ |
|
avfilter_free(m_sink_filter_ctx); |
|
m_sink_filter_ctx = nullptr; |
|
} |
|
if(m_fraph != nullptr){ |
|
avfilter_graph_free(&m_fraph); |
|
m_fraph = nullptr; |
|
} |
|
} |
|
int MediaAVFilter::initAVFilter(AVCodecContext **m_avcodec_context,char *value) |
|
{ |
|
m_fraph = avfilter_graph_alloc(); |
|
// 源过滤器和格式转换过滤器参数 |
|
std::string s1 = "sample_rate=" + std::to_string((*m_avcodec_context)->sample_rate) + ":sample_fmt=" + av_get_sample_fmt_name((*m_avcodec_context)->sample_fmt) + ":channel_layout=" + std::to_string((*m_avcodec_context)->channel_layout); |
|
std::string s2 = "sample_rates=" + std::to_string((*m_avcodec_context)->sample_rate) + ":sample_fmts=" + av_get_sample_fmt_name((*m_avcodec_context)->sample_fmt) + ":channel_layouts=" + std::to_string((*m_avcodec_context)->channel_layout); |
|
//创建源过滤器 |
|
const AVFilter *src_filter=avfilter_get_by_name("buffer"); |
|
m_src_filter_ctx=avfilter_graph_alloc_filter(m_fraph,src_filter,"src"); |
|
if (avfilter_init_str(m_src_filter_ctx, s1.c_str()) < 0) { |
|
printf("init src filter fail!\n"); |
|
return -1; |
|
} |
|
//创建变速过滤器 |
|
const AVFilter *tempo_filter = avfilter_get_by_name("tempo"); |
|
AVFilterContext *tempo_filter_ctx = avfilter_graph_alloc_filter(m_fraph, tempo_filter, "tempo"); |
|
AVDictionary *args = NULL; |
|
av_dict_set(&args, "tempo", value, 0);//根据value的值调节速度 |
|
if (avfilter_init_dict(tempo_filter_ctx, &args) < 0) { |
|
printf("init speed filter fail!\n"); |
|
return -1; |
|
} |
|
//创建格式转化过滤器 |
|
const AVFilter *fmt_filter = avfilter_get_by_name("format"); |
|
AVFilterContext *fmt_filter_ctx = avfilter_graph_alloc_filter(m_fraph, fmt_filter, "format"); |
|
if (avfilter_init_str(fmt_filter_ctx, s2.c_str()) < 0) { |
|
printf("init fmt filter fail!\n"); |
|
return -1; |
|
} |
|
//创建接收过滤器 |
|
const AVFilter *sink_filter=avfilter_get_by_name("buffersink"); |
|
m_sink_filter_ctx=avfilter_graph_alloc_filter(m_fraph,sink_filter,"sink"); |
|
if (avfilter_init_dict(m_sink_filter_ctx, NULL) < 0) { |
|
printf("link filter fail!\n"); |
|
return -1; |
|
} |
|
//链接过滤器 |
|
if(avfilter_link(m_src_filter_ctx,0,tempo_filter_ctx,0) != 0){ |
|
printf("link filter fail!\n"); |
|
return -1; |
|
} |
|
if(avfilter_link(tempo_filter_ctx,0,fmt_filter_ctx,0) != 0){ |
|
printf("link filter fail!\n"); |
|
return -1; |
|
} |
|
if(avfilter_link(fmt_filter_ctx,0,m_sink_filter_ctx,0) != 0){ |
|
printf("link filter fail!\n"); |
|
return -1; |
|
} |
|
|
|
//配置图 |
|
if (avfilter_graph_config(m_fraph, NULL) < 0) { |
|
printf("config graph fail!\n"); |
|
return -1; |
|
} |
|
return 0; |
|
} |