Merge remote-tracking branch 'qatar/master'
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 6 Mar 2013 23:27:53 +0000 (00:27 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 6 Mar 2013 23:28:15 +0000 (00:28 +0100)
* qatar/master:
  avconv: Make sure the encoder exists before inspecting supported_list

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
ffmpeg_filter.c

diff --combined ffmpeg_filter.c
  /*
 - * avconv filter configuration
 + * ffmpeg filter configuration
   *
 - * This file is part of Libav.
 + * This file is part of FFmpeg.
   *
 - * Libav is free software; you can redistribute it and/or
 + * FFmpeg is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
 - * Libav is distributed in the hope that it will be useful,
 + * FFmpeg is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
 - * License along with Libav; if not, write to the Free Software
 + * License along with FFmpeg; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
 -#include "avconv.h"
 +#include "ffmpeg.h"
  
  #include "libavfilter/avfilter.h"
  #include "libavfilter/avfiltergraph.h"
 +#include "libavfilter/buffersink.h"
  
  #include "libavresample/avresample.h"
  
  #include "libavutil/avassert.h"
  #include "libavutil/avstring.h"
 +#include "libavutil/bprint.h"
  #include "libavutil/channel_layout.h"
  #include "libavutil/opt.h"
  #include "libavutil/pixdesc.h"
  #include "libavutil/pixfmt.h"
 +#include "libavutil/imgutils.h"
  #include "libavutil/samplefmt.h"
  
 +enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target)
 +{
 +    if (codec && codec->pix_fmts) {
 +        const enum AVPixelFormat *p = codec->pix_fmts;
 +        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
 +        int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
 +        enum AVPixelFormat best= AV_PIX_FMT_NONE;
 +        if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
 +            if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
 +                p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
 +            } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
 +                p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
 +                                                 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
 +            }
 +        }
 +        for (; *p != AV_PIX_FMT_NONE; p++) {
 +            best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
 +            if (*p == target)
 +                break;
 +        }
 +        if (*p == AV_PIX_FMT_NONE) {
 +            if (target != AV_PIX_FMT_NONE)
 +                av_log(NULL, AV_LOG_WARNING,
 +                       "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
 +                       av_get_pix_fmt_name(target),
 +                       codec->name,
 +                       av_get_pix_fmt_name(best));
 +            return best;
 +        }
 +    }
 +    return target;
 +}
 +
 +void choose_sample_fmt(AVStream *st, AVCodec *codec)
 +{
 +    if (codec && codec->sample_fmts) {
 +        const enum AVSampleFormat *p = codec->sample_fmts;
 +        for (; *p != -1; p++) {
 +            if (*p == st->codec->sample_fmt)
 +                break;
 +        }
 +        if (*p == -1) {
 +            if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
 +                av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
 +            if(av_get_sample_fmt_name(st->codec->sample_fmt))
 +            av_log(NULL, AV_LOG_WARNING,
 +                   "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
 +                   av_get_sample_fmt_name(st->codec->sample_fmt),
 +                   codec->name,
 +                   av_get_sample_fmt_name(codec->sample_fmts[0]));
 +            st->codec->sample_fmt = codec->sample_fmts[0];
 +        }
 +    }
 +}
 +
 +static char *choose_pix_fmts(OutputStream *ost)
 +{
 +     if (ost->keep_pix_fmt) {
 +        if (ost->filter)
 +            avfilter_graph_set_auto_convert(ost->filter->graph->graph,
 +                                            AVFILTER_AUTO_CONVERT_NONE);
 +        if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
 +            return NULL;
 +        return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
 +    }
 +    if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
 +        return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
 +    } else if (ost->enc && ost->enc->pix_fmts) {
 +        const enum AVPixelFormat *p;
 +        AVIOContext *s = NULL;
 +        uint8_t *ret;
 +        int len;
 +
 +        if (avio_open_dyn_buf(&s) < 0)
 +            exit(1);
 +
 +        p = ost->enc->pix_fmts;
 +        if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
 +            if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
 +                p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
 +            } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
 +                p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
 +                                                    AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
 +            }
 +        }
 +
 +        for (; *p != AV_PIX_FMT_NONE; p++) {
 +            const char *name = av_get_pix_fmt_name(*p);
 +            avio_printf(s, "%s:", name);
 +        }
 +        len = avio_close_dyn_buf(s, &ret);
 +        ret[len - 1] = 0;
 +        return ret;
 +    } else
 +        return NULL;
 +}
 +
  /* Define a function for building a string containing a list of
   * allowed formats. */
  #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
@@@ -141,7 -41,7 +141,7 @@@ static char *choose_ ## var ## s(Output
      if (ost->st->codec->var != none) {                                         \
          get_name(ost->st->codec->var);                                         \
          return av_strdup(name);                                                \
-     } else if (ost->enc->supported_list) {                                     \
+     } else if (ost->enc && ost->enc->supported_list) {                         \
          const type *p;                                                         \
          AVIOContext *s = NULL;                                                 \
          uint8_t *ret;                                                          \
          return NULL;                                                           \
  }
  
 -DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
 -                  GET_PIX_FMT_NAME, ":")
 +// DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
 +//                   GET_PIX_FMT_NAME, ":")
  
  DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
                    AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
@@@ -224,17 -124,14 +224,17 @@@ static void init_input_filter(FilterGra
          int file_idx = strtol(in->name, &p, 0);
  
          if (file_idx < 0 || file_idx >= nb_input_files) {
 -            av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
 +            av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
                     file_idx, fg->graph_desc);
              exit(1);
          }
          s = input_files[file_idx]->ctx;
  
          for (i = 0; i < s->nb_streams; i++) {
 -            if (s->streams[i]->codec->codec_type != type)
 +            enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
 +            if (stream_type != type &&
 +                !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
 +                  type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
                  continue;
              if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
                  st = s->streams[i];
          }
          if (i == nb_input_streams) {
              av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
 -                   "unlabeled input pad %d on filter %s", in->pad_idx,
 +                   "unlabeled input pad %d on filter %s\n", in->pad_idx,
                     in->filter_ctx->name);
              exit(1);
          }
      av_assert0(ist);
  
      ist->discard         = 0;
 -    ist->decoding_needed = 1;
 +    ist->decoding_needed++;
      ist->st->discard = AVDISCARD_NONE;
  
      GROW_ARRAY(fg->inputs, fg->nb_inputs);
@@@ -286,14 -183,11 +286,14 @@@ static int configure_output_video_filte
      int pad_idx = out->pad_idx;
      int ret;
      char name[255];
 +    AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
  
      snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
      ret = avfilter_graph_create_filter(&ofilter->filter,
 -                                       avfilter_get_by_name("buffersink"),
 +                                       avfilter_get_by_name("ffbuffersink"),
                                         name, NULL, NULL, fg->graph);
 +    av_freep(&buffersink_params);
 +
      if (ret < 0)
          return ret;
  
          av_freep(&pix_fmts);
      }
  
 -    if (ost->frame_rate.num) {
 +    if (ost->frame_rate.num && 0) {
          AVFilterContext *fps;
          char args[255];
  
@@@ -369,52 -263,15 +369,52 @@@ static int configure_output_audio_filte
      char *sample_fmts, *sample_rates, *channel_layouts;
      char name[255];
      int ret;
 +    AVABufferSinkParams *params = av_abuffersink_params_alloc();
  
 -
 +    if (!params)
 +        return AVERROR(ENOMEM);
 +    params->all_channel_counts = 1;
      snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
      ret = avfilter_graph_create_filter(&ofilter->filter,
 -                                       avfilter_get_by_name("abuffersink"),
 -                                       name, NULL, NULL, fg->graph);
 +                                       avfilter_get_by_name("ffabuffersink"),
 +                                       name, NULL, params, fg->graph);
 +    av_freep(&params);
      if (ret < 0)
          return ret;
  
 +#define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do {                 \
 +    AVFilterContext *filt_ctx;                                              \
 +                                                                            \
 +    av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi "            \
 +           "similarly to -af " filter_name "=%s.\n", arg);                  \
 +                                                                            \
 +    ret = avfilter_graph_create_filter(&filt_ctx,                           \
 +                                       avfilter_get_by_name(filter_name),   \
 +                                       filter_name, arg, NULL, fg->graph);  \
 +    if (ret < 0)                                                            \
 +        return ret;                                                         \
 +                                                                            \
 +    ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0);                 \
 +    if (ret < 0)                                                            \
 +        return ret;                                                         \
 +                                                                            \
 +    last_filter = filt_ctx;                                                 \
 +    pad_idx = 0;                                                            \
 +} while (0)
 +    if (ost->audio_channels_mapped) {
 +        int i;
 +        AVBPrint pan_buf;
 +        av_bprint_init(&pan_buf, 256, 8192);
 +        av_bprintf(&pan_buf, "0x%"PRIx64,
 +                   av_get_default_channel_layout(ost->audio_channels_mapped));
 +        for (i = 0; i < ost->audio_channels_mapped; i++)
 +            if (ost->audio_channels_map[i] != -1)
 +                av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
 +
 +        AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
 +        av_bprint_finalize(&pan_buf, NULL);
 +    }
 +
      if (codec->channels && !codec->channel_layout)
          codec->channel_layout = av_get_default_channel_layout(codec->channels);
  
      if (sample_fmts || sample_rates || channel_layouts) {
          AVFilterContext *format;
          char args[256];
 -        int len = 0;
 +        args[0] = 0;
  
          if (sample_fmts)
 -            len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
 +            av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
                              sample_fmts);
          if (sample_rates)
 -            len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
 +            av_strlcatf(args, sizeof(args), "sample_rates=%s:",
                              sample_rates);
          if (channel_layouts)
 -            len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
 +            av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
                              channel_layouts);
 -        args[len - 1] = 0;
  
          av_freep(&sample_fmts);
          av_freep(&sample_rates);
          pad_idx = 0;
      }
  
 +    if (audio_volume != 256 && 0) {
 +        char args[256];
 +
 +        snprintf(args, sizeof(args), "%f", audio_volume / 256.);
 +        AUTO_INSERT_FILTER("-vol", "volume", args);
 +    }
 +
      if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
          return ret;
  
@@@ -498,52 -349,6 +498,52 @@@ int configure_output_filter(FilterGrap
      }
  }
  
 +static int sub2video_prepare(InputStream *ist)
 +{
 +    AVFormatContext *avf = input_files[ist->file_index]->ctx;
 +    int i, ret, w, h;
 +    uint8_t *image[4];
 +    int linesize[4];
 +
 +    /* Compute the size of the canvas for the subtitles stream.
 +       If the subtitles codec has set a size, use it. Otherwise use the
 +       maximum dimensions of the video streams in the same file. */
 +    w = ist->st->codec->width;
 +    h = ist->st->codec->height;
 +    if (!(w && h)) {
 +        for (i = 0; i < avf->nb_streams; i++) {
 +            if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 +                w = FFMAX(w, avf->streams[i]->codec->width);
 +                h = FFMAX(h, avf->streams[i]->codec->height);
 +            }
 +        }
 +        if (!(w && h)) {
 +            w = FFMAX(w, 720);
 +            h = FFMAX(h, 576);
 +        }
 +        av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
 +    }
 +    ist->sub2video.w = ist->st->codec->width  = ist->resample_width  = w;
 +    ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
 +
 +    /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
 +       palettes for all rectangles are identical or compatible */
 +    ist->resample_pix_fmt = ist->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
 +
 +    ret = av_image_alloc(image, linesize, w, h, AV_PIX_FMT_RGB32, 32);
 +    if (ret < 0)
 +        return ret;
 +    memset(image[0], 0, h * linesize[0]);
 +    ist->sub2video.ref = avfilter_get_video_buffer_ref_from_arrays(
 +            image, linesize, AV_PERM_READ | AV_PERM_PRESERVE,
 +            w, h, AV_PIX_FMT_RGB32);
 +    if (!ist->sub2video.ref) {
 +        av_free(image[0]);
 +        return AVERROR(ENOMEM);
 +    }
 +    return 0;
 +}
 +
  static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
                                          AVFilterInOut *in)
  {
      InputStream *ist = ifilter->ist;
      AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
                                           ist->st->time_base;
 +    AVRational fr = ist->framerate.num ? ist->framerate :
 +                                         ist->st->r_frame_rate;
      AVRational sar;
 -    char args[255], name[255];
 +    AVBPrint args;
 +    char name[255];
      int pad_idx = in->pad_idx;
      int ret;
  
 +    if (!ist->framerate.num && ist->st->codec->ticks_per_frame>1) {
 +        AVRational codec_fr = av_inv_q(ist->st->codec->time_base);
 +        AVRational   avg_fr = ist->st->avg_frame_rate;
 +        codec_fr.den *= ist->st->codec->ticks_per_frame;
 +        if (   codec_fr.num>0 && codec_fr.den>0 && av_q2d(codec_fr) < av_q2d(fr)*0.7
 +            && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr)))>0.1)
 +            fr = codec_fr;
 +    }
 +
 +    if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
 +        ret = sub2video_prepare(ist);
 +        if (ret < 0)
 +            return ret;
 +    }
 +
      sar = ist->st->sample_aspect_ratio.num ?
            ist->st->sample_aspect_ratio :
            ist->st->codec->sample_aspect_ratio;
 -    snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
 -             ist->st->codec->height, ist->st->codec->pix_fmt,
 -             tb.num, tb.den, sar.num, sar.den);
 +    if(!sar.den)
 +        sar = (AVRational){0,1};
 +    av_bprint_init(&args, 0, 1);
 +    av_bprintf(&args,
 +             "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
 +             "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
 +             ist->resample_height, ist->resample_pix_fmt,
 +             tb.num, tb.den, sar.num, sar.den,
 +             SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
 +    if (fr.num && fr.den)
 +        av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
      snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
               ist->file_index, ist->st->index);
  
      if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
 -                                            args, NULL, fg->graph)) < 0)
 +                                            args.str, NULL, fg->graph)) < 0)
          return ret;
  
      if (ist->framerate.num) {
@@@ -626,81 -405,73 +626,81 @@@ static int configure_input_audio_filter
      AVFilter *filter = avfilter_get_by_name("abuffer");
      InputStream *ist = ifilter->ist;
      int pad_idx = in->pad_idx;
 -    char args[255], name[255];
 +    AVBPrint args;
 +    char name[255];
      int ret;
  
 -    snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
 -             ":channel_layout=0x%"PRIx64,
 +    av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
 +    av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
               1, ist->st->codec->sample_rate,
               ist->st->codec->sample_rate,
 -             av_get_sample_fmt_name(ist->st->codec->sample_fmt),
 -             ist->st->codec->channel_layout);
 +             av_get_sample_fmt_name(ist->st->codec->sample_fmt));
 +    if (ist->st->codec->channel_layout)
 +        av_bprintf(&args, ":channel_layout=0x%"PRIx64,
 +                   ist->st->codec->channel_layout);
 +    else
 +        av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
      snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
               ist->file_index, ist->st->index);
  
      if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
 -                                            name, args, NULL,
 +                                            name, args.str, NULL,
                                              fg->graph)) < 0)
          return ret;
  
 +#define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do {                 \
 +    AVFilterContext *filt_ctx;                                              \
 +                                                                            \
 +    av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi "            \
 +           "similarly to -af " filter_name "=%s.\n", arg);                  \
 +                                                                            \
 +    snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d",      \
 +                fg->index, filter_name, ist->file_index, ist->st->index);   \
 +    ret = avfilter_graph_create_filter(&filt_ctx,                           \
 +                                       avfilter_get_by_name(filter_name),   \
 +                                       name, arg, NULL, fg->graph);         \
 +    if (ret < 0)                                                            \
 +        return ret;                                                         \
 +                                                                            \
 +    ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx);                \
 +    if (ret < 0)                                                            \
 +        return ret;                                                         \
 +                                                                            \
 +    first_filter = filt_ctx;                                                  \
 +} while (0)
 +
      if (audio_sync_method > 0) {
 -        AVFilterContext *async;
 -        int  len = 0;
 -
 -        av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
 -               "asyncts audio filter instead.\n");
 -
 -        if (audio_sync_method > 1)
 -            len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
 -                            "max_comp=%d:", audio_sync_method);
 -        snprintf(args + len, sizeof(args) - len, "min_delta=%f",
 -                 audio_drift_threshold);
 -
 -        snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
 -                 fg->index, ist->file_index, ist->st->index);
 -        ret = avfilter_graph_create_filter(&async,
 -                                           avfilter_get_by_name("asyncts"),
 -                                           name, args, NULL, fg->graph);
 -        if (ret < 0)
 -            return ret;
 +        char args[256] = {0};
 +
 +        av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
 +        if (audio_drift_threshold != 0.1)
 +            av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
 +        if (!fg->reconfiguration)
 +            av_strlcatf(args, sizeof(args), ":first_pts=0");
 +        AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
 +    }
  
 -        ret = avfilter_link(async, 0, first_filter, pad_idx);
 -        if (ret < 0)
 -            return ret;
 +//     if (ost->audio_channels_mapped) {
 +//         int i;
 +//         AVBPrint pan_buf;
 +//         av_bprint_init(&pan_buf, 256, 8192);
 +//         av_bprintf(&pan_buf, "0x%"PRIx64,
 +//                    av_get_default_channel_layout(ost->audio_channels_mapped));
 +//         for (i = 0; i < ost->audio_channels_mapped; i++)
 +//             if (ost->audio_channels_map[i] != -1)
 +//                 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
 +//         AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
 +//         av_bprint_finalize(&pan_buf, NULL);
 +//     }
  
 -        first_filter = async;
 -        pad_idx = 0;
 -    }
      if (audio_volume != 256) {
 -        AVFilterContext *volume;
 +        char args[256];
  
          av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
                 "audio filter instead.\n");
  
 -        snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
 -
 -        snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
 -                 fg->index, ist->file_index, ist->st->index);
 -        ret = avfilter_graph_create_filter(&volume,
 -                                           avfilter_get_by_name("volume"),
 -                                           name, args, NULL, fg->graph);
 -        if (ret < 0)
 -            return ret;
 -
 -        ret = avfilter_link(volume, 0, first_filter, pad_idx);
 -        if (ret < 0)
 -            return ret;
 -
 -        first_filter = volume;
 -        pad_idx = 0;
 +        snprintf(args, sizeof(args), "%f", audio_volume / 256.);
 +        AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
      }
      if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
          return ret;
@@@ -740,15 -511,6 +740,15 @@@ int configure_filtergraph(FilterGraph *
          snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
          fg->graph->scale_sws_opts = av_strdup(args);
  
 +        args[0] = 0;
 +        while ((e = av_dict_get(ost->swr_opts, "", e,
 +                                AV_DICT_IGNORE_SUFFIX))) {
 +            av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
 +        }
 +        if (strlen(args))
 +            args[strlen(args)-1] = 0;
 +        av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
 +
          args[0] = '\0';
          while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
                                  AV_DICT_IGNORE_SUFFIX))) {
          }
      }
  
 +    fg->reconfiguration = 1;
      return 0;
  }