[Libav-user] avfilter_graph_config return -22

star_conn star_conn at 163.com
Wed Feb 22 11:45:19 EET 2017


This is my code which copy from doc/examples/filter_audio.c
---------------------------------------

//===============================================filter===========
//audio : abuffer -> volume -> aformat -> abuffersink
int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink){
    AVFilterGraph *filter_graph;
    AVFilterContext *abuffer_ctx;
    AVFilter        *abuffer;
    AVFilterContext *volume_ctx;
    AVFilter        *volume;
    AVFilterContext *aformat_ctx;
    AVFilter        *aformat;
    AVFilterContext *abuffersink_ctx;
    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) {
        loge("Unable to create filter graph.");
        return AVERROR(ENOMEM);
    }


    /* Create the abuffer filter;
     * it will be used for feeding the data into the graph. */
    abuffer = avfilter_get_by_name("abuffer");
    if (!abuffer) {
        loge("Could not find the abuffer filter.");
        return AVERROR_FILTER_NOT_FOUND;
    }else{
logd("find abuffer filter.");
}


    abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, "src");
    if (!abuffer_ctx) {
        loge("Could not allocate the abuffer instance.");
        return AVERROR(ENOMEM);
    }


    /* Set the filter options through the AVOptions API. */
    av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, AV_CH_LAYOUT_MONO); //TODO
    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(AV_SAMPLE_FMT_S16), AV_OPT_SEARCH_CHILDREN);//TODO
    av_opt_set_q  (abuffer_ctx, "time_base",      (AVRational){ 1, 16000 },       AV_OPT_SEARCH_CHILDREN);//TODO
    av_opt_set_int(abuffer_ctx, "sample_rate",    16000,                          AV_OPT_SEARCH_CHILDREN);//TODO


    /* 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) {
        loge("Could not initialize the abuffer filter.");
        return err;
    }


    /* Create volume filter. */
    volume = avfilter_get_by_name("volume");
    if (!volume) {
        loge("Could not find the volume filter.");
        return AVERROR_FILTER_NOT_FOUND;
    }else{
logd("find volume filter.");
}


    volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume");
    if (!volume_ctx) {
        loge("Could not allocate the volume instance.");
        return AVERROR(ENOMEM);
    }


    /* A different way of passing the options is as key/value pairs in a dictionary. */
    //av_dict_set(&options_dict, "volume", AV_STRINGIFY(1.5), 0);//150% of current volume
    av_dict_set(&options_dict, "volume", AV_STRINGIFY(0.90), 0);
    err = avfilter_init_dict(volume_ctx, &options_dict);
    av_dict_free(&options_dict);
    if (err < 0) {
        loge("Could not initialize the volume filter.");
        return err;
    }


    /* Create the aformat filter; it ensures that the output is of the format we want. */
    aformat = avfilter_get_by_name("aformat");
    if (!aformat) {
        loge("Could not find the aformat filter.");
        return AVERROR_FILTER_NOT_FOUND;
    }else{
logd("find aformat filter.");
}


    aformat_ctx = avfilter_graph_alloc_filter(filter_graph, aformat, "aformat");
    if (!aformat_ctx) {
        loge("Could not allocate the aformat instance.");
        return AVERROR(ENOMEM);
    }


    /* A third way of passing the options is in a string of the form key1=value1:key2=value2.... */
    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), 16000,
             (uint64_t)AV_CH_LAYOUT_MONO);
    err = avfilter_init_str(aformat_ctx, options_str);
    if (err < 0) {
        //av_log(NULL, AV_LOG_ERROR, "Could not initialize the aformat filter.\n");
        loge("Could not initialize the aformat filter.");
        return err;
    }


    /* Finally create the abuffersink filter; it will be used to get the filtered data out of the graph. */
    abuffersink = avfilter_get_by_name("abuffersink");
    if (!abuffersink) {
        loge("Could not find the abuffersink filter.");
        return AVERROR_FILTER_NOT_FOUND;
    }else{
logd("find abuffersink filter.");
}


    abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
    if (!abuffersink_ctx) {
        loge("Could not allocate the abuffersink instance.");
        return AVERROR(ENOMEM);
    }


    /* This filter takes no options. */
    err = avfilter_init_str(abuffersink_ctx, NULL);
    if (err < 0) {
        loge("Could not initialize the abuffersink instance.");
        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)//zero on success
        err = avfilter_link(volume_ctx, 0, aformat_ctx, 0);
    if (err >= 0)
        err = avfilter_link(aformat_ctx, 0, abuffersink_ctx, 0);
    if (err < 0) {
        loge("Error connecting filters");
        return err;
    }


    /* Configure the graph. */
    err = avfilter_graph_config(filter_graph, NULL);
    if (err < 0) {
uint8_t errstr[1024];
av_strerror(err, errstr, sizeof(errstr));
        //av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
        loge("Error configuring the filter graph:%d,%s", err, errstr);
        return err;
    }


    *graph = filter_graph;
    *src   = abuffer_ctx;
    *sink  = abuffersink_ctx;
    return 0;
}
--------------------------------------

the output log is:
 :find abuffer filter.
: find volume filter.
: find aformat filter.
: find abuffersink filter.
: Error configuring the filter graph:-22,Invalid argument


can any friends help me?thanks so much for your help.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170222/88c8ea9e/attachment.html>


More information about the Libav-user mailing list