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


總結(jié)

本示例將生成一個正弦的音頻PCM數(shù)據(jù)舞终,然后把PCM數(shù)據(jù)經(jīng)過如下filterchain的處理。把輸出的每一幀PCM數(shù)據(jù)的MD5值打印出來.

這里就涉及了過濾器相關(guān)的

流程圖

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

其實相對好理解. 這里創(chuàng)建了拿到四個過濾器,創(chuàng)建過濾器上下文,設(shè)置參數(shù). 用三種方式是實現(xiàn),然后把四個過濾器連接起來,最后把隨機生成的幀,送入第一個過濾器.然后從最后一個過濾器中取出數(shù)據(jù).打印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 <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#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

/**
 * 創(chuàng)建了四個過濾器上下文.初始化參數(shù),然后連接到一起
 * 初始化過濾器. 這里應(yīng)該是各種轉(zhuǎn)換都是不同的過濾器效果.一層一層處理
 * @param graph
 * @param src
 * @param sink
 * @return
 * 過濾器AVFilter和過濾器上下文AVFilterContext的關(guān)系
 * 過濾器是sdk內(nèi)置的,我們直接可以使用.而過濾器上下文則是相關(guān)的環(huán)境和數(shù)據(jù).
 * 我們總是要根據(jù)過濾器來初始化過濾器上下文. 上下文是和接受的數(shù)據(jù)相關(guān)的.我們操作的數(shù)據(jù)也是操作上下文
 */
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過濾器. 看起來應(yīng)該是用來填充數(shù)據(jù)用的
    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設(shè)置參數(shù). 總共設(shè)置了 四個參數(shù).音頻相關(guān) time_base和sample_rate 是互為倒數(shù)
    /* 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. */
    //又根據(jù)上表初始化完成的 過濾器上下文來初始化過濾器
    err = avfilter_init_str(abuffer_ctx, NULL);
    if (err < 0) {
        fprintf(stderr, "Could not initialize the abuffer filter.\n");
        return err;
    }
//------2------獲取音量過濾器,然后創(chuàng)建音量過濾器上下文,在給他設(shè)置參數(shù),和上邊的過濾器類似
    /* 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. */
    //-另一種給上下文設(shè)置參數(shù)的方式.總之是先把參數(shù)設(shè)置給AVDictionary自帶,在設(shè)置給上下文
    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設(shè)置值
    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);
    //用上邊設(shè)置的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------在找到一個過濾器,下邊肯定會又初始化一個上下文,設(shè)置參數(shù). 方式都是一樣的
    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. */
    //沒有默認(rèn)參數(shù)來初始化上下文
    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. */ //配置過濾器圖形.我理解,四個過濾器上下文都是通過過濾器圖形來創(chuàng)建的.他們肯定是包含在內(nèi)
    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;
    //這兩個分別是輸入數(shù)據(jù)和輸出數(shù)據(jù)的過濾器上下文
    *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. */
//產(chǎn)生一幀數(shù)據(jù).隨機生成的
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數(shù)據(jù),然后把PCM數(shù)據(jù)經(jīng)過如下filterchain的處理奴饮。把輸出的每一幀PCM數(shù)據(jù)的MD5值打印出來
 * @param argc
 * @param argv
 * @return
 * avfilter_graph_alloc_filter: 在filtergraph中創(chuàng)建一個filter實例。
avfilter_init_str:使用提供的字符串參數(shù)初始化一個filter币绩。
avfilter_init_dict:使用提供的AVDictionary初始化一個filter。
avfilter_link:把兩個filter連接在一起峦树。

作者:smallest_one
鏈接:http://www.reibang.com/p/f677992bbde9
來源:簡書
著作權(quán)歸作者所有渡冻。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)戚扳,非商業(yè)轉(zhuǎn)載請注明出處。
 */
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 <duration>\n", argv[0]);
        return 1;
    }

    duration = atof(argv[1]);//char 轉(zhuǎn)float
    nb_frames = duration * INPUT_SAMPLERATE / FRAME_SIZE;//幀數(shù)
    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是最初的過濾器上下文.負(fù)責(zé)輸入數(shù)據(jù) sink是最后的過濾器上下文.負(fù)責(zé)輸出數(shù)據(jù)
    err = init_filter_graph(&graph, &src, &sink);
    if (err < 0) {
        fprintf(stderr, "Unable to init filter graph:");
        goto fail;
    }

    //一幀一幀的循環(huán)遍歷
    /* 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. */
        //把幀數(shù)據(jù)放到 過濾器中處理
        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;
        }

        //從最后的過濾器中把數(shù)據(jù)取出.放回到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;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末族吻,一起剝皮案震驚了整個濱河市帽借,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌超歌,老刑警劉巖砍艾,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異巍举,居然都是意外死亡脆荷,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門懊悯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜓谋,“玉大人,你說我怎么就攤上這事炭分√一溃” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵捧毛,是天一觀的道長观堂。 經(jīng)常有香客問我让网,道長,這世上最難降的妖魔是什么师痕? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任溃睹,我火速辦了婚禮,結(jié)果婚禮上七兜,老公的妹妹穿的比我還像新娘丸凭。我一直安慰自己,他們只是感情好腕铸,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布惜犀。 她就那樣靜靜地躺著,像睡著了一般狠裹。 火紅的嫁衣襯著肌膚如雪虽界。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天,我揣著相機與錄音靶瘸,去河邊找鬼且轨。 笑死,一個胖子當(dāng)著我的面吹牛礁叔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播迄薄,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼琅关,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了讥蔽?” 一聲冷哼從身側(cè)響起涣易,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冶伞,沒想到半個月后新症,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡响禽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年徒爹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片金抡。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡瀑焦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出梗肝,到底是詐尸還是另有隱情榛瓮,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布巫击,位于F島的核電站禀晓,受9級特大地震影響精续,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜粹懒,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一重付、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧凫乖,春花似錦确垫、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至导街,卻和暖如春披泪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背搬瑰。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工款票, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人泽论。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓艾少,卻偏偏與公主長得像,于是被迫代替她去往敵國和親翼悴。 傳聞我的和親對象是個殘疾皇子姆钉,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355