static bool recreate_graph(struct af_instance *af, struct mp_audio *config) { void *tmp = talloc_new(NULL); struct priv *p = af->priv; AVFilterContext *in = NULL, *out = NULL; int r; if (bstr0(p->cfg_graph).len == 0) { mp_msg(MSGT_AFILTER, MSGL_FATAL, "lavfi: no filter graph set\n"); return false; } destroy_graph(af); mp_msg(MSGT_AFILTER, MSGL_V, "lavfi: create graph: '%s'\n", p->cfg_graph); AVFilterGraph *graph = avfilter_graph_alloc(); if (!graph) goto error; if (parse_avopts(graph, p->cfg_avopts) < 0) { mp_msg(MSGT_VFILTER, MSGL_FATAL, "lavfi: could not set opts: '%s'\n", p->cfg_avopts); goto error; } AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); if (!outputs || !inputs) goto error; char *src_args = talloc_asprintf(tmp, "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d:" "channel_layout=0x%"PRIx64, config->rate, av_get_sample_fmt_name(af_to_avformat(config->format)), config->channels.num, 1, config->rate, mp_chmap_to_lavc(&config->channels)); if (avfilter_graph_create_filter(&in, avfilter_get_by_name("abuffer"), "src", src_args, NULL, graph) < 0) goto error; if (avfilter_graph_create_filter(&out, avfilter_get_by_name("abuffersink"), "out", NULL, NULL, graph) < 0) goto error; static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }; r = av_opt_set_int_list(out, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN); if (r < 0) goto error; r = av_opt_set_int(out, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN); if (r < 0) goto error; outputs->name = av_strdup("in"); outputs->filter_ctx = in; inputs->name = av_strdup("out"); inputs->filter_ctx = out; if (graph_parse(graph, p->cfg_graph, inputs, outputs, NULL) < 0) goto error; if (avfilter_graph_config(graph, NULL) < 0) goto error; p->in = in; p->out = out; p->graph = graph; assert(out->nb_inputs == 1); assert(in->nb_outputs == 1); talloc_free(tmp); return true; error: mp_msg(MSGT_AFILTER, MSGL_FATAL, "Can't configure libavfilter graph.\n"); avfilter_graph_free(&graph); talloc_free(tmp); return false; }
static bool recreate_graph(struct af_instance *af, struct mp_audio *config) { void *tmp = talloc_new(NULL); struct priv *p = af->priv; AVFilterContext *in = NULL, *out = NULL, *f_format = NULL; if (bstr0(p->cfg_graph).len == 0) { MP_FATAL(af, "lavfi: no filter graph set\n"); return false; } destroy_graph(af); MP_VERBOSE(af, "lavfi: create graph: '%s'\n", p->cfg_graph); AVFilterGraph *graph = avfilter_graph_alloc(); if (!graph) goto error; if (mp_set_avopts(af->log, graph, p->cfg_avopts) < 0) goto error; AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); if (!outputs || !inputs) goto error; // Build list of acceptable output sample formats. libavfilter will insert // conversion filters if needed. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }; char *fmtstr = talloc_strdup(tmp, ""); for (int n = 0; sample_fmts[n] != AV_SAMPLE_FMT_NONE; n++) { const char *name = av_get_sample_fmt_name(sample_fmts[n]); if (name) { const char *s = fmtstr[0] ? "|" : ""; fmtstr = talloc_asprintf_append_buffer(fmtstr, "%s%s", s, name); } } char *src_args = talloc_asprintf(tmp, "sample_rate=%d:sample_fmt=%s:time_base=%d/%d:" "channel_layout=0x%"PRIx64, config->rate, av_get_sample_fmt_name(af_to_avformat(config->format)), 1, config->rate, mp_chmap_to_lavc(&config->channels)); if (avfilter_graph_create_filter(&in, avfilter_get_by_name("abuffer"), "src", src_args, NULL, graph) < 0) goto error; if (avfilter_graph_create_filter(&out, avfilter_get_by_name("abuffersink"), "out", NULL, NULL, graph) < 0) goto error; if (avfilter_graph_create_filter(&f_format, avfilter_get_by_name("aformat"), "format", fmtstr, NULL, graph) < 0) goto error; if (avfilter_link(f_format, 0, out, 0) < 0) goto error; outputs->name = av_strdup("in"); outputs->filter_ctx = in; inputs->name = av_strdup("out"); inputs->filter_ctx = f_format; if (graph_parse(graph, p->cfg_graph, inputs, outputs, NULL) < 0) goto error; if (avfilter_graph_config(graph, NULL) < 0) goto error; p->in = in; p->out = out; p->graph = graph; assert(out->nb_inputs == 1); assert(in->nb_outputs == 1); talloc_free(tmp); return true; error: MP_FATAL(af, "Can't configure libavfilter graph.\n"); avfilter_graph_free(&graph); talloc_free(tmp); return false; }
int main(int argc, char *argv[]) { if(argc != 3) { std::cerr << "Usage: " << argv[0] << " <input file> <max tree depth>\n"; return 1; } /* parse command line args */ std::istringstream nd(argv[2]); std::ifstream infile(argv[1]); int maxdepth; nd >> maxdepth; // check for errors if(!infile) { std::cerr << "Unable to open " << argv[1] << " for input.\n"; return 2; } if(maxdepth <=1) { std::cerr << "Max depth must be >1. Value received was " << maxdepth << "\n"; return 2; } Graph G; if(!read_graph_from_stream(infile,G)) { std::cerr << "Error reading input graph.\n"; return 3; } // output command line to stderr std::cerr << "Command line:\t"; for(int i=0;i<argc;++i) std::cerr << argv[i] << " "; std::cerr << "\n"; struct timeval t1,t2,t3,t4; gettimeofday(&t1,NULL); /* Compute transitive reduction of G */ Graph Greduce = G.treduce(); gettimeofday(&t2,NULL); /* parse the graph into a clan tree */ ClanTree ptree; Greduce.topological_sort(); gettimeofday(&t3,NULL); graph_parse(Greduce, NULL, ptree, 5); gettimeofday(&t4,NULL); // check integrity of both graphs. if(!G.integrity_check()) { std::cerr << "Integrity check failed for " << G << ".\n"; return 4; } else { std::cerr << "Integrity check passed for " << G << ".\n"; } if(!ptree.integrity_check()) { std::cerr << "Integrity check failed for " << ptree << ".\n"; return 4; } else { std::cerr << "Integrity check passed for parse tree: " << ptree << ".\n"; } #if 0 std::cerr << ptree.nodelist().size() << " Clans found (including singletons)\n"; for(ClanTree::nodelist_c_iter_t rnode(ptree.nodelist().begin()); rnode != ptree.nodelist().end(); ++rnode) { std::cerr << clan_abbrev(rnode->first,0) << "\taddr=" << &rnode->first; std::cerr << "\tG= " << rnode->first.graph(); std::cerr << "\n\tChild nodes:\n"; for(std::set<Clanid>::const_iterator ccln(rnode->second.successors.begin()); ccln != rnode->second.successors.end(); ++ccln) std::cerr << "\t" << clan_abbrev(*ccln,0) << "\tG= " << ccln->graph() << "\n"; } #endif /* Output the clan tree */ Clanid root(ptree.find_source_node()); ClanTree::nodelist_c_iter_t rnode(ptree.nodelist().find(root)); draw_clan_tree(std::cout, rnode, ptree, 0, maxdepth); double dt1 = (t2.tv_sec - t1.tv_sec) + 1.0e-6*(t2.tv_usec - t1.tv_usec); double dt2 = (t3.tv_sec - t2.tv_sec) + 1.0e-6*(t3.tv_usec - t2.tv_usec); double dt3 = (t4.tv_sec - t3.tv_sec) + 1.0e-6*(t4.tv_usec - t3.tv_usec); std::cerr << "\nTiming report:\n\ttransitive reduction:\t" << dt1 << " s\n"; std::cerr << "\tTopological sort:\t" << dt2 << " s\n"; std::cerr << "\tGraph parse:\t" << dt3 << " s\n"; return 0; }