static int configure_video_filters(FilterGraph *fg, AVFilterContext **in_filter,
                                   AVFilterContext **out_filter)
{
    InputStream  *ist = fg->inputs[0]->ist;
    OutputStream *ost = fg->outputs[0]->ost;
    AVFilterContext *filter;
    AVCodecContext *codec = ost->st->codec;
    char *pix_fmts;
    AVRational sample_aspect_ratio;
    char args[255];
    int ret;

    if (ist->st->sample_aspect_ratio.num) {
        sample_aspect_ratio = ist->st->sample_aspect_ratio;
    } else
        sample_aspect_ratio = ist->st->codec->sample_aspect_ratio;

    snprintf(args, 255, "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
             ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE,
             sample_aspect_ratio.num, sample_aspect_ratio.den);

    ret = avfilter_graph_create_filter(&fg->inputs[0]->filter,
                                       avfilter_get_by_name("buffer"),
                                       "src", args, NULL, fg->graph);
    if (ret < 0)
        return ret;
    ret = avfilter_graph_create_filter(&fg->outputs[0]->filter,
                                       avfilter_get_by_name("buffersink"),
                                       "out", NULL, NULL, fg->graph);
    if (ret < 0)
        return ret;
    *in_filter  = fg->inputs[0]->filter;
    *out_filter = fg->outputs[0]->filter;

    if (codec->width || codec->height) {
        snprintf(args, 255, "%d:%d:flags=0x%X",
                 codec->width,
                 codec->height,
                 (unsigned)ost->sws_flags);
        if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
                                                NULL, args, NULL, fg->graph)) < 0)
            return ret;
        if ((ret = avfilter_link(*in_filter, 0, filter, 0)) < 0)
            return ret;
        *in_filter = filter;
    }

    if ((pix_fmts = choose_pix_fmts(ost))) {
        if ((ret = avfilter_graph_create_filter(&filter,
                                                avfilter_get_by_name("format"),
                                                "format", pix_fmts, NULL,
                                                fg->graph)) < 0)
            return ret;
        if ((ret = avfilter_link(filter, 0, *out_filter, 0)) < 0)
            return ret;

        *out_filter = filter;
        av_freep(&pix_fmts);
    }

    snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
    fg->graph->scale_sws_opts = av_strdup(args);

    return 0;
}