filtering_audio.c/filtering_video.c 解读

filtering_audio.c/filtering_video.c 解读
************************************************************
filtering_audio.c 也演示了一个利用音频过滤器,进行音频重采样的过程.
打开一个碼流文件,
    ret = open_input_file(argv[1])
初始化一个过滤器
    ret = init_filters(filter_descr)
读取包
    ret = av_read_frame(fmt_ctx, &packet)
解包:
    ret = avcodec_receive_frame(dec_ctx, frame);
应用到过滤器
    av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF);
    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);

补充: 打开一个碼流文件需要如下的流程
    ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL);
    ret = avformat_find_stream_info(fmt_ctx, NULL);
    /* select the audio stream */
    ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
    audio_stream_index = ret;
    /* create decoding context */
    dec_ctx = avcodec_alloc_context3(dec);
    avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[audio_stream_index]->codecpar);
    /* init the audio decoder */
    ret = avcodec_open2(dec_ctx, dec, NULL);

这里的重点是如何用一个字符串去初始化过滤器链,过滤器用逗号分割.
    ret = init_filters(filter_descr)
    char *filter_descr = "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono";

1. 创建filter_graph
    filter_graph = avfilter_graph_alloc();

2. 创建源节点, 不可缺少, 即abuffer 过滤器.
    这里采用的是create_filter. 也是把参数传进去的一种方法.
    const AVFilter *abuffersrc  = avfilter_get_by_name("abuffer");
    snprintf(args, sizeof(args),
            "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
             time_base.num, time_base.den, dec_ctx->sample_rate,
             av_get_sample_fmt_name(dec_ctx->sample_fmt), dec_ctx->channel_layout);
    ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in", args, NULL, filter_graph);
    args 的意义是什么? 怎么来的?
    args snprintf 打印的字符串, 是输入流的一些信息,包括layout,sample_fmt,sample_rate 及时基信息.


3. 创建汇节点, 我们从这里获取过滤的数据.叫abuffer_sink
    ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out", NULL, NULL, filter_graph);
    并设置参数
    ret = av_opt_set_int_list(buffersink_ctx, "sample_fmts", out_sample_fmts, -1, AV_OPT_SEARCH_CHILDREN);
    ret = av_opt_set_int_list(buffersink_ctx, "channel_layouts", out_channel_layouts, -1, AV_OPT_SEARCH_CHILDREN);
    ret = av_opt_set_int_list(buffersink_ctx, "sample_rates", out_sample_rates, -1, AV_OPT_SEARCH_CHILDREN);
    各选项 的意义是什么? 怎么来的?
    三个选项是手工设定的(程序设定的),显然是为了完成一种格式转换. 这是另一种格式转换手法.

4. 分析过滤器字符串并创建过滤器连接
    ret = avfilter_graph_parse_ptr(filter_graph, filters_descr, &inputs, &outputs, NULL);
    其中inputs, outputs 是两个AVFilterInOut *, 其中inputs 是汇(sink)的inputs, outpus 是源(src)的outputs.
    这样从abuffer->outputs->filers_descr->inputs->abuffer_sink 建立连接链.
5. 配置filter_graph
    ret = avfilter_graph_config(filter_graph, NULL);


filtering_video.c
它实现了大小拉伸,反转功能, 其过滤过程与audio 是一样的.分析从略.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值