ffmpeg_sample解读_filter_audio


title: ffmpeg_sample解读_filter_audio
date: 2020-10-28 10:15:02
tags: [读书笔记]
typora-copy-images-to: ./imgs
typora-root-url: ./imgs


总结

本示例将生成一个正弦的音频PCM数据,然后把PCM数据经过如下filterchain的处理。把输出的每一帧PCM数据的MD5值打印出来.

这里就涉及了过滤器相关的

流程图

graph TB
 afa[av_frame_alloc]
 -->ama[av_md5_alloc]
 -->ifg[init_filter_graph]
 -->afga[avfilter_graph_alloc]
 -->afgbn[avfilter_get_by_name]
 -->afaf[avfilter_graph_alloc_filter]
 -->afis[avfilter_init_str]
 -->afl[avfilter_link]
 -->afgc[avfilter_graph_config]
 -->gi[get_input]
 -->abaf[av_buffersrc_add_frame]
 -->abgf[av_buffersink_get_frame]
 -->free[free_all]
image-20201028162609381

其实相对好理解. 这里创建了拿到四个过滤器,创建过滤器上下文,设置参数. 用三种方式是实现,然后把四个过滤器连接起来,最后把随机生成的帧,送入第一个过滤器.然后从最后一个过滤器中取出数据.打印md5

代码



/**
 * @file
 * libavfilter API usage example.
 *
 * @example filter_audio.c
 * This example will generate a sine wave audio,
 * pass it through a simple filter chain, and then compute the MD5 checksum of
 * the output data.
 *
 * The filter chain it uses is:
 * (input) -> abuffer -> volume -> aformat -> abuffersink -> (output)
 *
 * abuffer: This provides the endpoint where you can feed the decoded samples.
 * volume: In this example we hardcode it to 0.90.
 * aformat: This converts the samples to the samplefreq, channel layout,
 *          and sample format required by the audio device.
 * abuffersink: This provides the endpoint where you can read the samples after
 *              they have passed through the filter chain.
 */

#include 
#include 
#include 
#include 

#include "libavutil/channel_layout.h"
#include "libavutil/md5.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/samplefmt.h"

#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"

#define INPUT_SAMPLERATE     48000
#define INPUT_FORMAT         AV_SAMPLE_FMT_FLTP
#define INPUT_CHANNEL_LAYOUT AV_CH_LAYOUT_5POINT0

#define VOLUME_VAL 0.90

/**
 * 创建了四个过滤器上下文.初始化参数,然后连接到一起
 * 初始化过滤器. 这里应该是各种转换都是不同的过滤器效果.一层一层处理
 * @param graph
 * @param src
 * @param sink
 * @return
 * 过滤器AVFilter和过滤器上下文AVFilterContext的关系
 * 过滤器是sdk内置的,我们直接可以使用.而过滤器上下文则是相关的环境和数据.
 * 我们总是要根据过滤器来初始化过滤器上下文. 上下文是和接受的数据相关的.我们操作的数据也是操作上下文
 */
static int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src,
                             AVFilterContext **sink) {
    AVFilterGraph *filter_graph;
    AVFilterContext *abuffer_ctx;
    const AVFilter *abuffer;
    AVFilterContext *volume_ctx;
    const AVFilter *volume;
    AVFilterContext *aformat_ctx;
    const AVFilter *aformat;
    AVFilterContext *abuffersink_ctx;
    const AVFilter *abuffersink;

    AVDictionary *options_dict = NULL;
    uint8_t options_str[1024];
    uint8_t ch_layout[64];

    int err;

    /* Create a new filtergraph, which will contain all the filters. */
    filter_graph = avfilter_graph_alloc();//分配过滤器图形控件
    if (!filter_graph) {
        fprintf(stderr, "Unable to create filter graph.\n");
        return AVERROR(ENOMEM);
    }

    /* Create the abuffer filter;
     * it will be used for feeding the data into the graph. */
    //------1------通过名称初始化 abuffer过滤器. 看起来应该是用来填充数据用的
    abuffer = avfilter_get_by_name("abuffer");
    if (!abuffer) {
        fprintf(stderr, "Could not find the abuffer filter.\n");
        return AVERROR_FILTER_NOT_FOUND;
    }
    //通过已有的filter_graph 过滤器图形生成,名称是src,返回过滤器上下文
    abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, "src");
    if (!abuffer_ctx) {
        fprintf(stderr, "Could not allocate the abuffer instance.\n");
        return AVERROR(ENOMEM);
    }

    //给上边的 过滤器上下文abuffer_ctx设置参数. 总共设置了 四个参数.音频相关 time_base和sample_rate 是互为倒数
    /* Set the filter options through the AVOptions API. */
    av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, INPUT_CHANNEL_LAYOUT);
    av_opt_set(abuffer_ctx, "channel_layout", ch_layout, AV_OPT_SEARCH_CHILDREN);
    av_opt_set(abuffer_ctx, "sample_fmt", av_get_sample_fmt_name(INPUT_FORMAT),
               AV_OPT_SEARCH_CHILDREN);
    av_opt_set_q(abuffer_ctx, "time_base", (AVRational) {1, INPUT_SAMPLERATE},
                 AV_OPT_SEARCH_CHILDREN);
    av_opt_set_int(abuffer_ctx, "sample_rate", INPUT_SAMPLERATE, AV_OPT_SEARCH_CHILDREN);

    /* Now initialize the filter; we pass NULL options, since we have already
     * set all the options above. */
    //又根据上表初始化完成的 过滤器上下文来初始化过滤器
    err = avfilter_init_str(abuffer_ctx, NULL);
    if (err < 0) {
        fprintf(stderr, "Could not initialize the abuffer filter.\n");
        return err;
    }
//------2------获取音量过滤器,然后创建音量过滤器上下文,在给他设置参数,和上边的过滤器类似
    /* Create volume filter. */
    volume = avfilter_get_by_name("volume");
    if (!volume) {
        fprintf(stderr, "Could not find the volume filter.\n");
        return AVERROR_FILTER_NOT_FOUND;
    }

    //初始化一个音量过滤器上下文,名称是volume
    volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume");
    if (!volume_ctx) {
        fprintf(stderr, "Could not allocate the volume instance.\n");
        return AVERROR(ENOMEM);
    }

    /* A different way of passing the options is as key/value pairs in a
     * dictionary. */
    //-另一种给上下文设置参数的方式.总之是先把参数设置给AVDictionary自带,在设置给上下文
    av_dict_set(&options_dict, "volume", AV_STRINGIFY(VOLUME_VAL), 0);
    err = avfilter_init_dict(volume_ctx, &options_dict);
    av_dict_free(&options_dict);
    if (err < 0) {
        fprintf(stderr, "Could not initialize the volume filter.\n");
        return err;
    }

    /* Create the aformat filter;
     * it ensures that the output is of the format we want. */
    //------3------在找到格式过滤器
    aformat = avfilter_get_by_name("aformat");
    if (!aformat) {
        fprintf(stderr, "Could not find the aformat filter.\n");
        return AVERROR_FILTER_NOT_FOUND;
    }
    //初始化格式过滤器上下文,命名为aformat
    aformat_ctx = avfilter_graph_alloc_filter(filter_graph, aformat, "aformat");
    if (!aformat_ctx) {
        fprintf(stderr, "Could not allocate the aformat instance.\n");
        return AVERROR(ENOMEM);
    }

    /* A third way of passing the options is in a string of the form
     * key1=value1:key2=value2.... */
    //第三种方式,把key,value写入 options_str这个buf中, 其实就是给options_str设置值
    snprintf(options_str, sizeof(options_str),
             "sample_fmts=%s:sample_rates=%d:channel_layouts=0x%"PRIx64,
             av_get_sample_fmt_name(AV_SAMPLE_FMT_S16), 44100,
             (uint64_t) AV_CH_LAYOUT_STEREO);
    //用上边设置的str 初始化过滤器上下文
    err = avfilter_init_str(aformat_ctx, options_str);
    if (err < 0) {
        av_log(NULL, AV_LOG_ERROR, "Could not initialize the aformat filter.\n");
        return err;
    }

    /* Finally create the abuffersink filter;
     * it will be used to get the filtered data out of the graph. */
    //------4------在找到一个过滤器,下边肯定会又初始化一个上下文,设置参数. 方式都是一样的
    abuffersink = avfilter_get_by_name("abuffersink");
    if (!abuffersink) {
        fprintf(stderr, "Could not find the abuffersink filter.\n");
        return AVERROR_FILTER_NOT_FOUND;
    }
//找到过滤器上下文
    abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
    if (!abuffersink_ctx) {
        fprintf(stderr, "Could not allocate the abuffersink instance.\n");
        return AVERROR(ENOMEM);
    }

    /* This filter takes no options. */
    //没有默认参数来初始化上下文
    err = avfilter_init_str(abuffersink_ctx, NULL);
    if (err < 0) {
        fprintf(stderr, "Could not initialize the abuffersink instance.\n");
        return err;
    }

    /* Connect the filters;
     * in this simple case the filters just form a linear chain. */
    //把四个过滤器上下文进行连接,形成一个单链,上个过滤器的输出就是下个过滤器的输入
    err = avfilter_link(abuffer_ctx, 0, volume_ctx, 0);
    if (err >= 0)
        err = avfilter_link(volume_ctx, 0, aformat_ctx, 0);
    if (err >= 0)
        err = avfilter_link(aformat_ctx, 0, abuffersink_ctx, 0);
    if (err < 0) {
        fprintf(stderr, "Error connecting filters\n");
        return err;
    }

    /* Configure the graph. */ //配置过滤器图形.我理解,四个过滤器上下文都是通过过滤器图形来创建的.他们肯定是包含在内
    err = avfilter_graph_config(filter_graph, NULL);
    if (err < 0) {
        av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
        return err;
    }

    *graph = filter_graph;
    //这两个分别是输入数据和输出数据的过滤器上下文
    *src = abuffer_ctx;
    *sink = abuffersink_ctx;

    return 0;
}

/* Do something useful with the filtered data: this simple
 * example just prints the MD5 checksum of each plane to stdout. */
static int process_output(struct AVMD5 *md5, AVFrame *frame) {
    int planar = av_sample_fmt_is_planar(frame->format);
    int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
    int planes = planar ? channels : 1;
    int bps = av_get_bytes_per_sample(frame->format);
    int plane_size = bps * frame->nb_samples * (planar ? 1 : channels);
    int i, j;

    for (i = 0; i < planes; i++) {
        uint8_t checksum[16];

        av_md5_init(md5);
        av_md5_sum(checksum, frame->extended_data[i], plane_size);

        fprintf(stdout, "plane %d: 0x", i);
        for (j = 0; j < sizeof(checksum); j++)
            fprintf(stdout, "%02X", checksum[j]);
        fprintf(stdout, "\n");
    }
    fprintf(stdout, "\n");

    return 0;
}

/* Construct a frame of audio data to be filtered;
 * this simple example just synthesizes a sine wave. */
//产生一帧数据.随机生成的
static int get_input(AVFrame *frame, int frame_num) {
    int err, i, j;

#define FRAME_SIZE 1024

    /* Set up the frame properties and allocate the buffer for the data. */
    frame->sample_rate = INPUT_SAMPLERATE;
    frame->format = INPUT_FORMAT;
    frame->channel_layout = INPUT_CHANNEL_LAYOUT;
    frame->nb_samples = FRAME_SIZE;
    frame->pts = frame_num * FRAME_SIZE;

    err = av_frame_get_buffer(frame, 0);
    if (err < 0)
        return err;

    /* Fill the data for each channel. */
    for (i = 0; i < 5; i++) {
        float *data = (float *) frame->extended_data[i];

        for (j = 0; j < frame->nb_samples; j++)
            data[j] = sin(2 * M_PI * (frame_num + j) * (i + 1) / FRAME_SIZE);
    }

    return 0;
}

/**
 * 本示例将生成一个正弦的音频PCM数据,然后把PCM数据经过如下filterchain的处理。把输出的每一帧PCM数据的MD5值打印出来
 * @param argc
 * @param argv
 * @return
 * avfilter_graph_alloc_filter: 在filtergraph中创建一个filter实例。
avfilter_init_str:使用提供的字符串参数初始化一个filter。
avfilter_init_dict:使用提供的AVDictionary初始化一个filter。
avfilter_link:把两个filter连接在一起。

作者:smallest_one
链接:https://www.jianshu.com/p/f677992bbde9
来源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 */
int filter_audio_main(int argc, char *argv[]) {
    struct AVMD5 *md5;
    AVFilterGraph *graph;
    AVFilterContext *src, *sink;
    AVFrame *frame;
    uint8_t errstr[1024];
    float duration;
    int err, nb_frames, i;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s \n", argv[0]);
        return 1;
    }

    duration = atof(argv[1]);//char 转float
    nb_frames = duration * INPUT_SAMPLERATE / FRAME_SIZE;//帧数
    if (nb_frames <= 0) {
        fprintf(stderr, "Invalid duration: %s\n", argv[1]);
        return 1;
    }

    /* Allocate the frame we will be using to store the data. */
    frame = av_frame_alloc(); //初始化未压缩的帧
    if (!frame) {
        fprintf(stderr, "Error allocating the frame\n");
        return 1;
    }

    md5 = av_md5_alloc();//初始化一个AVMD5
    if (!md5) {
        fprintf(stderr, "Error allocating the MD5 context\n");
        return 1;
    }

    /* Set up the filtergraph. */
    //初始化过滤器视图,src是最初的过滤器上下文.负责输入数据 sink是最后的过滤器上下文.负责输出数据
    err = init_filter_graph(&graph, &src, &sink);
    if (err < 0) {
        fprintf(stderr, "Unable to init filter graph:");
        goto fail;
    }

    //一帧一帧的循环遍历
    /* the main filtering loop */
    for (i = 0; i < nb_frames; i++) {
        /* get an input frame to be filtered */
        err = get_input(frame, i);
        if (err < 0) {
            fprintf(stderr, "Error generating input frame:");
            goto fail;
        }

        /* Send the frame to the input of the filtergraph. */
        //把帧数据放到 过滤器中处理
        err = av_buffersrc_add_frame(src, frame);
        if (err < 0) {
            av_frame_unref(frame);
            fprintf(stderr, "Error submitting the frame to the filtergraph:");
            goto fail;
        }

        //从最后的过滤器中把数据取出.放回到frame中,这里也就是过滤器完成了帧的处理
        /* Get all the filtered output that is available. */
        while ((err = av_buffersink_get_frame(sink, frame)) >= 0) {
            /* now do something with our filtered frame */
//            打印处理完的帧的md5
            err = process_output(md5, frame);
            if (err < 0) {
                fprintf(stderr, "Error processing the filtered frame:");
                goto fail;
            }
            av_frame_unref(frame);
        }

        if (err == AVERROR(EAGAIN)) {
            /* Need to feed more frames in. */
            continue;
        } else if (err == AVERROR_EOF) {
            /* Nothing more to do, finish. */
            break;
        } else if (err < 0) {
            /* An error occurred. */
            fprintf(stderr, "Error filtering the data:");
            goto fail;
        }
    }

    avfilter_graph_free(&graph);
    av_frame_free(&frame);
    av_freep(&md5);

    return 0;

    fail:
    av_strerror(err, errstr, sizeof(errstr));
    fprintf(stderr, "%s\n", errstr);
    return 1;
}

你可能感兴趣的:(ffmpeg_sample解读_filter_audio)